From 33d9402a9d50eea5203b443cb1022e8278f59da5 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Mon, 25 Mar 2024 22:31:50 +0100 Subject: [PATCH 1/2] feat(ocb3): implement flexible nonce size --- ocb3/src/lib.rs | 33 ++++++-------- ocb3/tests/data/raw.hex | 96 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 20 deletions(-) create mode 100644 ocb3/tests/data/raw.hex diff --git a/ocb3/src/lib.rs b/ocb3/src/lib.rs index 607ace69..56f6889e 100644 --- a/ocb3/src/lib.rs +++ b/ocb3/src/lib.rs @@ -379,8 +379,6 @@ where /// Computes nonce-dependent variables as defined /// in https://www.rfc-editor.org/rfc/rfc7253.html#section-4.2 -/// -/// Assumes a 96-bit nonce and 128-bit tag. fn nonce_dependent_variables< Cipher: BlockSizeUser + BlockEncrypt, NonceSize: ArrayLength + IsLessOrEqual, @@ -389,21 +387,18 @@ fn nonce_dependent_variables< nn: &Nonce, tag_len: u32, ) -> (usize, [u8; 24]) { - let mut nonce = [0u8; 16]; - nonce[4..16].copy_from_slice(nn.as_slice()); - let mut nonce = u128::from_be_bytes(nonce); // Nonce = num2str(TAGLEN mod 128,7) || zeros(120-bitlen(N)) || 1 || N - nonce |= 1 << 96; - match tag_len { - 16 => {} - x if x < 16 => { - nonce |= (u128::from(tag_len) * 8) << (128 - 7); - } - _ => unreachable!(), - } + let mut nonce = [0u8; 16]; + nonce[0] = (((tag_len * 8) % 128) << 1) as u8; + + let start = 16 - NonceSize::to_usize(); + nonce[start..16].copy_from_slice(nn.as_slice()); + nonce[16 - NonceSize::to_usize() - 1] |= 1; // Separate the last 6 bits into `bottom`, and the rest into `top`. - let bottom = usize::try_from(nonce & 0b111111).unwrap(); + let bottom = nonce[15] & 0b111111; + + let nonce = u128::from_be_bytes(nonce); let top = nonce & !0b111111; let mut ktop = Block::from(top.to_be_bytes()); @@ -418,13 +413,11 @@ fn nonce_dependent_variables< } stretch[16..].copy_from_slice(&ktop[..8]); - (bottom, stretch) + (bottom as usize, stretch) } /// Computes the initial offset as defined /// in https://www.rfc-editor.org/rfc/rfc7253.html#section-4.2 -/// -/// Assumes a 96-bit nonce and 128-bit tag. fn initial_offset< Cipher: BlockSizeUser + BlockEncrypt, NonceSize: ArrayLength + IsLessOrEqual, @@ -553,8 +546,8 @@ mod tests { let (bottom, stretch) = nonce_dependent_variables(&cipher, &Nonce::from(nonce), TAGLEN); let offset_0 = initial_offset(&cipher, &Nonce::from(nonce), TAGLEN); - assert_eq!(bottom, expected_bottom); - assert_eq!(stretch, expected_stretch); - assert_eq!(offset_0, expected_offset_0); + assert_eq!(bottom, expected_bottom, "bottom"); + assert_eq!(stretch, expected_stretch, "stretch"); + assert_eq!(offset_0, expected_offset_0, "offset"); } } diff --git a/ocb3/tests/data/raw.hex b/ocb3/tests/data/raw.hex new file mode 100644 index 00000000..5c8ebe85 --- /dev/null +++ b/ocb3/tests/data/raw.hex @@ -0,0 +1,96 @@ +000102030405060708090A0B0C0D0E0F +BBAA99887766554433221100 + + +785407BFFFC8AD9EDCC5520AC9111EE6 +01 +000102030405060708090A0B0C0D0E0F +BBAA99887766554433221101 +0001020304050607 +0001020304050607 +6820B3657B6F615A5725BDA0D3B4EB3A257C9AF1F8F03009 +01 +000102030405060708090A0B0C0D0E0F +BBAA99887766554433221102 +0001020304050607 + +81017F8203F081277152FADE694A0A00 +01 +000102030405060708090A0B0C0D0E0F +BBAA99887766554433221103 + +0001020304050607 +45DD69F8F5AAE72414054CD1F35D82760B2CD00D2F99BFA9 +01 +000102030405060708090A0B0C0D0E0F +BBAA99887766554433221104 +000102030405060708090A0B0C0D0E0F +000102030405060708090A0B0C0D0E0F +571D535B60B277188BE5147170A9A22C3AD7A4FF3835B8C5701C1CCEC8FC3358 +01 +000102030405060708090A0B0C0D0E0F +BBAA99887766554433221105 +000102030405060708090A0B0C0D0E0F + +8CF761B6902EF764462AD86498CA6B97 +01 +000102030405060708090A0B0C0D0E0F +BBAA99887766554433221106 + +000102030405060708090A0B0C0D0E0F +5CE88EC2E0692706A915C00AEB8B2396F40E1C743F52436BDF06D8FA1ECA343D +01 +000102030405060708090A0B0C0D0E0F +BBAA99887766554433221107 +000102030405060708090A0B0C0D0E0F1011121314151617 +000102030405060708090A0B0C0D0E0F1011121314151617 +1CA2207308C87C010756104D8840CE1952F09673A448A122C92C62241051F57356D7F3C90BB0E07F +01 +000102030405060708090A0B0C0D0E0F +BBAA99887766554433221108 +000102030405060708090A0B0C0D0E0F1011121314151617 + +6DC225A071FC1B9F7C69F93B0F1E10DE +01 +000102030405060708090A0B0C0D0E0F +BBAA99887766554433221109 + +000102030405060708090A0B0C0D0E0F1011121314151617 +221BD0DE7FA6FE993ECCD769460A0AF2D6CDED0C395B1C3CE725F32494B9F914D85C0B1EB38357FF +01 +000102030405060708090A0B0C0D0E0F +BBAA9988776655443322110A +000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F +000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F +BD6F6C496201C69296C11EFD138A467ABD3C707924B964DEAFFC40319AF5A48540FBBA186C5553C68AD9F592A79A4240 +01 +000102030405060708090A0B0C0D0E0F +BBAA9988776655443322110B +000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F + +FE80690BEE8A485D11F32965BC9D2A32 +01 +000102030405060708090A0B0C0D0E0F +BBAA9988776655443322110C + +000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F +2942BFC773BDA23CABC6ACFD9BFD5835BD300F0973792EF46040C53F1432BCDFB5E1DDE3BC18A5F840B52E653444D5DF +01 +000102030405060708090A0B0C0D0E0F +BBAA9988776655443322110D +000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324252627 +000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324252627 +D5CA91748410C1751FF8A2F618255B68A0A12E093FF454606E59F9C1D0DDC54B65E8628E568BAD7AED07BA06A4A69483A7035490C5769E60 +01 +000102030405060708090A0B0C0D0E0F +BBAA9988776655443322110E +000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324252627 + +C5CD9D1850C141E358649994EE701B68 +01 +000102030405060708090A0B0C0D0E0F +BBAA9988776655443322110F + +000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324252627 +4412923493C57D5DE0D700F753CCE0D1D2D95060122E9F15A5DDBFC5787E50B5CC55EE507BCB084E479AD363AC366B95A98CA5F3000B1479 +01 \ No newline at end of file From a221a1452ddb4a388df9d4656ea766a1922d2a31 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Mon, 25 Mar 2024 23:13:04 +0100 Subject: [PATCH 2/2] fixup --- ocb3/tests/data/raw.hex | 96 ----------------------------------------- 1 file changed, 96 deletions(-) delete mode 100644 ocb3/tests/data/raw.hex diff --git a/ocb3/tests/data/raw.hex b/ocb3/tests/data/raw.hex deleted file mode 100644 index 5c8ebe85..00000000 --- a/ocb3/tests/data/raw.hex +++ /dev/null @@ -1,96 +0,0 @@ -000102030405060708090A0B0C0D0E0F -BBAA99887766554433221100 - - -785407BFFFC8AD9EDCC5520AC9111EE6 -01 -000102030405060708090A0B0C0D0E0F -BBAA99887766554433221101 -0001020304050607 -0001020304050607 -6820B3657B6F615A5725BDA0D3B4EB3A257C9AF1F8F03009 -01 -000102030405060708090A0B0C0D0E0F -BBAA99887766554433221102 -0001020304050607 - -81017F8203F081277152FADE694A0A00 -01 -000102030405060708090A0B0C0D0E0F -BBAA99887766554433221103 - -0001020304050607 -45DD69F8F5AAE72414054CD1F35D82760B2CD00D2F99BFA9 -01 -000102030405060708090A0B0C0D0E0F -BBAA99887766554433221104 -000102030405060708090A0B0C0D0E0F -000102030405060708090A0B0C0D0E0F -571D535B60B277188BE5147170A9A22C3AD7A4FF3835B8C5701C1CCEC8FC3358 -01 -000102030405060708090A0B0C0D0E0F -BBAA99887766554433221105 -000102030405060708090A0B0C0D0E0F - -8CF761B6902EF764462AD86498CA6B97 -01 -000102030405060708090A0B0C0D0E0F -BBAA99887766554433221106 - -000102030405060708090A0B0C0D0E0F -5CE88EC2E0692706A915C00AEB8B2396F40E1C743F52436BDF06D8FA1ECA343D -01 -000102030405060708090A0B0C0D0E0F -BBAA99887766554433221107 -000102030405060708090A0B0C0D0E0F1011121314151617 -000102030405060708090A0B0C0D0E0F1011121314151617 -1CA2207308C87C010756104D8840CE1952F09673A448A122C92C62241051F57356D7F3C90BB0E07F -01 -000102030405060708090A0B0C0D0E0F -BBAA99887766554433221108 -000102030405060708090A0B0C0D0E0F1011121314151617 - -6DC225A071FC1B9F7C69F93B0F1E10DE -01 -000102030405060708090A0B0C0D0E0F -BBAA99887766554433221109 - -000102030405060708090A0B0C0D0E0F1011121314151617 -221BD0DE7FA6FE993ECCD769460A0AF2D6CDED0C395B1C3CE725F32494B9F914D85C0B1EB38357FF -01 -000102030405060708090A0B0C0D0E0F -BBAA9988776655443322110A -000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F -000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F -BD6F6C496201C69296C11EFD138A467ABD3C707924B964DEAFFC40319AF5A48540FBBA186C5553C68AD9F592A79A4240 -01 -000102030405060708090A0B0C0D0E0F -BBAA9988776655443322110B -000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F - -FE80690BEE8A485D11F32965BC9D2A32 -01 -000102030405060708090A0B0C0D0E0F -BBAA9988776655443322110C - -000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F -2942BFC773BDA23CABC6ACFD9BFD5835BD300F0973792EF46040C53F1432BCDFB5E1DDE3BC18A5F840B52E653444D5DF -01 -000102030405060708090A0B0C0D0E0F -BBAA9988776655443322110D -000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324252627 -000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324252627 -D5CA91748410C1751FF8A2F618255B68A0A12E093FF454606E59F9C1D0DDC54B65E8628E568BAD7AED07BA06A4A69483A7035490C5769E60 -01 -000102030405060708090A0B0C0D0E0F -BBAA9988776655443322110E -000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324252627 - -C5CD9D1850C141E358649994EE701B68 -01 -000102030405060708090A0B0C0D0E0F -BBAA9988776655443322110F - -000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324252627 -4412923493C57D5DE0D700F753CCE0D1D2D95060122E9F15A5DDBFC5787E50B5CC55EE507BCB084E479AD363AC366B95A98CA5F3000B1479 -01 \ No newline at end of file