I've just come across this piece of code in Bouncy Castle's implementation of OCB Mode:
if (N.length > 16 || (N.length == 16 && (N[0] & 0x80) != 0))
{
/*
* NOTE: We don't just ignore bit 128 because it would hide from the caller the fact
* that two nonces differing only in bit 128 are not different.
*/
throw new IllegalArgumentException("IV must be no more than 127 bits");
}
My understanding of this check is that if the nonce is longer than 16 bytes, or the nonce is 16 bytes and the first bit of the first byte of the nonce is not 0 (assuming big endian), then an error is thrown.
Do I understand this correctly? If so, what is best practice for creating the nonce (assuming I want a full 127-bit nonce). Generate 16 random bytes and unset the first bit of the first byte?
Also, regarding the code-comment about simply ignoring the 128th bit, would it be safe to do that if the full 128-bit nonce was used (not unsetting the first bit) with an authenticated mode (or encrypt-then-mac), ensuring the integrity of the nonce?