50 bool use_splits = mode & 1;
51 bool damage = mode & 4;
52 unsigned aad_length_bits = 3 * ((mode >> 3) & 3);
54 unsigned length_bits = 2 * ((mode >> 5) & 7);
57 auto aad = rng.
randbytes<std::byte>(aad_length);
58 auto plain = rng.
randbytes<std::byte>(length);
63 if (use_splits && length > 0) {
65 aead.
Encrypt(std::span{plain}.first(split_index), std::span{plain}.subspan(split_index), aad,
nonce, cipher);
71 std::vector<std::byte> keystream(length);
73 for (
size_t i = 0; i < length; ++i) {
74 assert((plain[i] ^ keystream[i]) == cipher[i]);
77 std::vector<std::byte> decrypted_contents(length);
82 std::byte damage_val{(uint8_t)(1U << (key_position & 7))};
83 std::vector<std::byte> bad_key = key;
84 bad_key[key_position] ^= damage_val;
87 ok = bad_aead.
Decrypt(cipher, aad,
nonce, decrypted_contents);
93 unsigned damage_bit = provider.
ConsumeIntegralInRange<
unsigned>(0, (cipher.size() + aad.size()) * 8U - 1U);
94 unsigned damage_pos = damage_bit >> 3;
95 std::byte damage_val{(uint8_t)(1U << (damage_bit & 7))};
96 if (damage_pos >= cipher.size()) {
97 aad[damage_pos - cipher.size()] ^= damage_val;
99 cipher[damage_pos] ^= damage_val;
103 if (use_splits && length > 0) {
105 ok = aead.
Decrypt(cipher, aad,
nonce, std::span{decrypted_contents}.first(split_index), std::span{decrypted_contents}.subspan(split_index));
107 ok = aead.
Decrypt(cipher, aad,
nonce, decrypted_contents);
113 assert(decrypted_contents == plain);
141 bool use_splits = mode & 1;
142 bool damage = mode & 4;
143 unsigned aad_length_bits = 3 * ((mode >> 3) & 3);
145 unsigned length_bits = 2 * ((mode >> 5) & 7);
148 auto aad = rng.
randbytes<std::byte>(aad_length);
149 auto plain = rng.
randbytes<std::byte>(length);
153 if (use_splits && length > 0) {
155 enc_aead.
Encrypt(std::span{plain}.first(split_index), std::span{plain}.subspan(split_index), aad, cipher);
157 enc_aead.
Encrypt(plain, aad, cipher);
160 std::vector<std::byte> decrypted_contents(length);
165 std::byte damage_val{(uint8_t)(1U << (key_position & 7))};
166 std::vector<std::byte> bad_key = key;
167 bad_key[key_position] ^= damage_val;
171 ok = bad_fs_aead.
Decrypt(cipher, aad, decrypted_contents);
177 unsigned damage_bit = provider.
ConsumeIntegralInRange<
unsigned>(0, (cipher.size() + aad.size()) * 8U - 1U);
178 unsigned damage_pos = damage_bit >> 3;
179 std::byte damage_val{(uint8_t)(1U << (damage_bit & 7))};
180 if (damage_pos >= cipher.size()) {
181 aad[damage_pos - cipher.size()] ^= damage_val;
183 cipher[damage_pos] ^= damage_val;
188 if (use_splits && length > 0) {
190 ok = dec_aead.
Decrypt(cipher, aad, std::span{decrypted_contents}.first(split_index), std::span{decrypted_contents}.subspan(split_index));
192 ok = dec_aead.
Decrypt(cipher, aad, decrypted_contents);
198 assert(decrypted_contents == plain);
void Encrypt(std::span< const std::byte > plain, std::span< const std::byte > aad, Nonce96 nonce, std::span< std::byte > cipher) noexcept
Encrypt a message with a specified 96-bit nonce and aad.
bool Decrypt(std::span< const std::byte > cipher, std::span< const std::byte > aad, Nonce96 nonce, std::span< std::byte > plain) noexcept
Decrypt a message with a specified 96-bit nonce and aad.