nisse@lysator.liu.se (Niels Möller) writes:
Justus Winter justus@sequoia-pgp.org writes:
I've given it a try, see branch ocb-mode. Based on RFC 7253. Passes tests, but not particularly optimized.
I have wrapped it in our Rust bindings, glued Sequoia to it, and did some interop testing. Looks all good.
Based on the functions declared in ocb.h, or the struct nettle_ocb_aesxxx?
I'm thinking that it might make sense to add the latter as part of the public api for next release, but leave all other functions as internal to let dust settle a bit?
Based on the ocb.h interface. The latter is not a good fit for our bindings, which let you combine AEAD modes with block ciphers, something OpenPGP also allows (for better or worse). Also, the latter is not useful for OpenPGP as specified in both the RFC4880bis draft and the crypto-refresh draft, as both use 120 bit nonces, whereas the interface uses 96 bit nonces.
I have opened an issue about the nonce size, and we decided to seek clarification from the authors of RFC7253:
https://gitlab.com/openpgp-wg/rfc4880bis/-/issues/83
The processing of the authenticated data doesn't depend on the nonce in any way. That means that if one processes several messages with the same key and associated data, the associated data can be processed once, with the same sum reused for all messages.
Is that something that is useful in practice, and which nettle interfaces should support?
That is an interesting question. Currently, the OpenPGP drafts that include AEAD do include the chunk index in the authenticated data and would therefore not benefit from this optimization. However, I've raised this point in our issue tracker:
This choice can affect both API and ABI.
We optimistically changed our scheme to not change the associated data between chunks (except for the last chunk):
] For each chunk, the AEAD construction is given the Packet Tag in new ] format encoding (bits 7 and 6 set, bits 5-0 carry the packet tag), ] version number, cipher algorithm octet, AEAD algorithm octet, and chunk ] size octet as additional data. For example, the additional data of the ] first chunk using EAX and AES-128 with a chunk size of 2**16 octets ] consists of the octets 0xD2, 0x02, 0x07, 0x01, and 0x10. ] ] After the final chunk, the AEAD algorithm is used to produce a final ] authentication tag encrypting the empty string. This AEAD instance is ] given the additional data specified above, plus an eight-octet, ] big-endian value specifying the total number of plaintext octets ] encrypted. This allows detection of a truncated ciphertext.
The way the nonce is used seems designed to allow cheap incrementing of the nonce. The nonce is used to determine
Offset_0 = Stretch[1+bottom..128+bottom]
where "bottom" is the least significant 6 bits of the nonce, acting as a shift, and "Stretch" is independent of those nonce bits, so unchanged on all but one out of 64 nonce increments.
Should nettle support some kind of auto-incrementing nonce that takes advantage of this? Nettle does something similar for UMAC (not sure if there are others).
That is also interesting. I have raised the point in our issue tracker, and Daniel Huigens observed that at least their Go implementation simply compares the top-most bits with the ones provided for the previous chunk. Botan does the same.
For any kind of optimization of this, one needs to store previous nonce and relatede values in the context.
To generalize it to more than auto-increment, one get the problem that for the first set_nonce, there is no previous nonce to compare to. So one would need an extra flag just for that, which I don't think is so nice. Alternatively, use a zero nonce by default at initialization. Then there's another slight complication: To set the nonce, one needs to know the "tag_length". In the current version, that is passed as an argument to set_nonce. It could perhaps be passed with set_key instead, it's now some time since I read the RFC, but I don't think it is proper use to use OCB with the same key, but change tag_length from message to message.
This has the benefit of working for how OpenPGP currently constructs the nonce, which does not result in monotonically incrementing nonces (currently, we take an IV and xor in the chunk index). But, we may change the scheme.
I think it would be nice to stick to a simply incrementing nonce value.
We optimistically changed the scheme so that it is a counter, albeit one not starting from zero:
] The nonce for AEAD mode consists of two parts. Let N be the size of the ] nonce. The left-most N - 64 bits are the initialization vector derived ] using HKDF. The right-most 64 bits are the chunk index as big-endian ] value. The index of the first chunk is zero.
Cheers, Justus