Skip to content

Commit

Permalink
Add unit test for ChaCha20's new caching
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa committed Jan 31, 2023
1 parent fb243d2 commit 511aa4f
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/test/crypto_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 511aa4f

Please sign in to comment.