| 294 | } |
| 295 | |
| 296 | void 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 | { |
| 298 | auto plain = ParseHex<std::byte>(plain_hex); |
| 299 | auto aad = ParseHex<std::byte>(aad_hex); |
| 300 | auto key = ParseHex<std::byte>(key_hex); |
| 301 | auto expected_cipher = ParseHex<std::byte>(cipher_hex); |
| 302 | std::vector<std::byte> cipher(plain.size() + FSChaCha20Poly1305::EXPANSION); |
| 303 | |
| 304 | for (int it = 0; it < 10; ++it) { |
| 305 | // During it==0 we use the single-plain Encrypt/Decrypt; others use a split at prefix. |
| 306 | size_t prefix = it ? m_rng.randrange(plain.size() + 1) : plain.size(); |
| 307 | std::byte dummy_tag[FSChaCha20Poly1305::EXPANSION] = {{}}; |
| 308 | |
| 309 | // Do msg_idx dummy encryptions to seek to the correct packet. |
| 310 | FSChaCha20Poly1305 enc_aead{key, 224}; |
| 311 | for (uint64_t i = 0; i < msg_idx; ++i) { |
| 312 | enc_aead.Encrypt(std::span{dummy_tag}.first(0), std::span{dummy_tag}.first(0), dummy_tag); |
| 313 | } |
| 314 | |
| 315 | // Invoke single-plain or plain1/plain2 Encrypt. |
| 316 | if (it == 0) { |
| 317 | enc_aead.Encrypt(plain, aad, cipher); |
| 318 | } else { |
| 319 | enc_aead.Encrypt(std::span{plain}.first(prefix), std::span{plain}.subspan(prefix), aad, cipher); |
| 320 | } |
| 321 | BOOST_CHECK(cipher == expected_cipher); |
| 322 | |
| 323 | // Do msg_idx dummy decryptions to seek to the correct packet. |
| 324 | FSChaCha20Poly1305 dec_aead{key, 224}; |
| 325 | for (uint64_t i = 0; i < msg_idx; ++i) { |
| 326 | dec_aead.Decrypt(dummy_tag, std::span{dummy_tag}.first(0), std::span{dummy_tag}.first(0)); |
| 327 | } |
| 328 | |
| 329 | // Invoke single-plain or plain1/plain2 Decrypt. |
| 330 | std::vector<std::byte> decipher(cipher.size() - AEADChaCha20Poly1305::EXPANSION); |
| 331 | bool ret{false}; |
| 332 | if (it == 0) { |
| 333 | ret = dec_aead.Decrypt(cipher, aad, decipher); |
| 334 | } else { |
| 335 | ret = dec_aead.Decrypt(cipher, aad, std::span{decipher}.first(prefix), std::span{decipher}.subspan(prefix)); |
| 336 | } |
| 337 | BOOST_CHECK(ret); |
| 338 | BOOST_CHECK(decipher == plain); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | void TestHKDF_SHA256_32(const std::string &ikm_hex, const std::string &salt_hex, const std::string &info_hex, const std::string &okm_check_hex) { |
| 343 | std::vector<unsigned char> initial_key_material = ParseHex(ikm_hex); |