diff options
author | Pieter Wuille <pieter@wuille.net> | 2023-01-18 16:27:06 -0500 |
---|---|---|
committer | Pieter Wuille <pieter@wuille.net> | 2023-01-30 19:12:55 -0500 |
commit | 511aa4f1c7508f15cab8d7e58007900ad6fd3d5d (patch) | |
tree | cf18e8f16fd265da78e757107b6ba0c3d9042aae /src/test/crypto_tests.cpp | |
parent | fb243d25f754da8f01793b41e2d225b917f3e5d7 (diff) |
Add unit test for ChaCha20's new caching
Diffstat (limited to 'src/test/crypto_tests.cpp')
-rw-r--r-- | src/test/crypto_tests.cpp | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/test/crypto_tests.cpp b/src/test/crypto_tests.cpp index 0ecca3ce2e..ed851b5266 100644 --- a/src/test/crypto_tests.cpp +++ b/src/test/crypto_tests.cpp @@ -160,6 +160,27 @@ static void TestChaCha20(const std::string &hex_message, const std::string &hexk } BOOST_CHECK_EQUAL(hexout, HexStr(outres)); } + + // Repeat 10x, but fragmented into 3 chunks, to exercise the ChaCha20 class's caching. + for (int i = 0; i < 10; ++i) { + size_t lens[3]; + lens[0] = InsecureRandRange(hexout.size() / 2U + 1U); + lens[1] = InsecureRandRange(hexout.size() / 2U + 1U - lens[0]); + lens[2] = hexout.size() / 2U - lens[0] - lens[1]; + + rng.Seek64(seek); + outres.assign(hexout.size() / 2U, 0); + size_t pos = 0; + for (int j = 0; j < 3; ++j) { + if (!hex_message.empty()) { + rng.Crypt(m.data() + pos, outres.data() + pos, lens[j]); + } else { + rng.Keystream(outres.data() + pos, lens[j]); + } + pos += lens[j]; + } + BOOST_CHECK_EQUAL(hexout, HexStr(outres)); + } } static void TestPoly1305(const std::string &hexmessage, const std::string &hexkey, const std::string& hextag) |