Skip to content

Commit 0bf8747

Browse files
theStacksipa
authored andcommitted
test: add ChaCha20 test triggering 32-bit block counter overflow
Verify that our ChaCha20 implementation using the 96/32 split interface is compatible with >256 GiB outputs by triggering a 32-bit block counter overflow and checking that the keystream matches one created with an alternative implementation using a 64/64 split interface with the corresponding input data. The test case data was generated with the following Python script using the PyCryptodome library (version 3.15.0): ---------------------------------------------------------------------------------------------- from Crypto.Cipher import ChaCha20 key = bytes(list(range(32))); nonce = 0xdeadbeef12345678; pos = 2**32 - 1 c = ChaCha20.new(key=key, nonce=nonce.to_bytes(8, 'little')) c.seek(pos * 64); stream = c.encrypt(bytes([0])*128) print(f"Key: {key.hex()}\nNonce: {hex(nonce)}\nPos: {hex(pos)}\nStream: {stream.hex()}") ----------------------------------------------------------------------------------------------
1 parent 7f2a985 commit 0bf8747

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/test/crypto_tests.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,19 @@ BOOST_AUTO_TEST_CASE(chacha20_testvector)
650650
"d3ce042c566ab2c507b138db853e3d6959660996546cc9c4a6eafdc777c040d7"
651651
"0eaf46f76dad3979e5c5360c3317166a1c894c94a371876a94df7628fe4eaaf2"
652652
"ccb27d5aaae0ad7ad0f9d4b6ad3b54098746d4524d38407a6deb3ab78fab78c9");
653+
654+
// Test overflow of 32-bit block counter, should increment the first 32-bit
655+
// part of the nonce to retain compatibility with >256 GiB output.
656+
// The test data was generated with an implementation that uses a 64-bit
657+
// counter and a 64-bit initialization vector (PyCryptodome's ChaCha20 class
658+
// with 8 bytes nonce length).
659+
TestChaCha20("",
660+
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
661+
{0, 0xdeadbeef12345678}, 0xffffffff,
662+
"2d292c880513397b91221c3a647cfb0765a4815894715f411e3df5e0dd0ba9df"
663+
"fd565dea5addbdb914208fde7950f23e0385f9a727143f6a6ac51d84b1c0fb3e"
664+
"2e3b00b63d6841a1cc6d1538b1d3a74bef1eb2f54c7b7281e36e484dba89b351"
665+
"c8f572617e61e342879f211b0e4c515df50ea9d0771518fad96cd0baee62deb6");
653666
}
654667

655668
BOOST_AUTO_TEST_CASE(chacha20_midblock)

0 commit comments

Comments
 (0)