MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / TestChaCha20Poly1305

Method TestChaCha20Poly1305

src/test/crypto_tests.cpp:255–294  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

253}
254
255void TestChaCha20Poly1305(const std::string& plain_hex, const std::string& aad_hex, const std::string& key_hex, ChaCha20::Nonce96 nonce, const std::string& cipher_hex)
256{
257 auto plain = ParseHex<std::byte>(plain_hex);
258 auto aad = ParseHex<std::byte>(aad_hex);
259 auto key = ParseHex<std::byte>(key_hex);
260 auto expected_cipher = ParseHex<std::byte>(cipher_hex);
261
262 for (int i = 0; i < 10; ++i) {
263 // During i=0, use single-plain Encrypt/Decrypt; others use a split at prefix.
264 size_t prefix = i ? m_rng.randrange(plain.size() + 1) : plain.size();
265 // Encrypt.
266 std::vector<std::byte> cipher(plain.size() + AEADChaCha20Poly1305::EXPANSION);
267 AEADChaCha20Poly1305 aead{key};
268 if (i == 0) {
269 aead.Encrypt(plain, aad, nonce, cipher);
270 } else {
271 aead.Encrypt(std::span{plain}.first(prefix), std::span{plain}.subspan(prefix), aad, nonce, cipher);
272 }
273 BOOST_CHECK(cipher == expected_cipher);
274
275 // Decrypt.
276 std::vector<std::byte> decipher(cipher.size() - AEADChaCha20Poly1305::EXPANSION);
277 bool ret{false};
278 if (i == 0) {
279 ret = aead.Decrypt(cipher, aad, nonce, decipher);
280 } else {
281 ret = aead.Decrypt(cipher, aad, nonce, std::span{decipher}.first(prefix), std::span{decipher}.subspan(prefix));
282 }
283 BOOST_CHECK(ret);
284 BOOST_CHECK(decipher == plain);
285 }
286
287 // Test Keystream output.
288 std::vector<std::byte> keystream(plain.size());
289 AEADChaCha20Poly1305 aead{key};
290 aead.Keystream(nonce, keystream);
291 for (size_t i = 0; i < plain.size(); ++i) {
292 BOOST_CHECK_EQUAL(plain[i] ^ keystream[i], expected_cipher[i]);
293 }
294}
295
296void TestFSChaCha20Poly1305(const std::string& plain_hex, const std::string& aad_hex, const std::string& key_hex, uint64_t msg_idx, const std::string& cipher_hex)
297{

Callers

nothing calls this directly

Calls 5

randrangeMethod · 0.80
sizeMethod · 0.45
EncryptMethod · 0.45
DecryptMethod · 0.45
KeystreamMethod · 0.45

Tested by

no test coverage detected