Skip to content

Commit 511aa4f

Browse files
committed
Add unit test for ChaCha20's new caching
1 parent fb243d2 commit 511aa4f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/test/crypto_tests.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,27 @@ static void TestChaCha20(const std::string &hex_message, const std::string &hexk
160160
}
161161
BOOST_CHECK_EQUAL(hexout, HexStr(outres));
162162
}
163+
164+
// Repeat 10x, but fragmented into 3 chunks, to exercise the ChaCha20 class's caching.
165+
for (int i = 0; i < 10; ++i) {
166+
size_t lens[3];
167+
lens[0] = InsecureRandRange(hexout.size() / 2U + 1U);
168+
lens[1] = InsecureRandRange(hexout.size() / 2U + 1U - lens[0]);
169+
lens[2] = hexout.size() / 2U - lens[0] - lens[1];
170+
171+
rng.Seek64(seek);
172+
outres.assign(hexout.size() / 2U, 0);
173+
size_t pos = 0;
174+
for (int j = 0; j < 3; ++j) {
175+
if (!hex_message.empty()) {
176+
rng.Crypt(m.data() + pos, outres.data() + pos, lens[j]);
177+
} else {
178+
rng.Keystream(outres.data() + pos, lens[j]);
179+
}
180+
pos += lens[j];
181+
}
182+
BOOST_CHECK_EQUAL(hexout, HexStr(outres));
183+
}
163184
}
164185

165186
static void TestPoly1305(const std::string &hexmessage, const std::string &hexkey, const std::string& hextag)

0 commit comments

Comments
 (0)