49 bool use_splits = mode & 1;
50 bool damage = mode & 4;
51 unsigned aad_length_bits = 3 * ((mode >> 3) & 3);
53 unsigned length_bits = 2 * ((mode >> 5) & 7);
62 if (use_splits && length > 0) {
64 aead.
Encrypt(std::span{plain}.first(split_index), std::span{plain}.subspan(split_index), aad, nonce, cipher);
66 aead.
Encrypt(plain, aad, nonce, cipher);
70 std::vector<std::byte> keystream(length);
72 for (
size_t i = 0; i < length; ++i) {
73 assert((plain[i] ^ keystream[i]) == cipher[i]);
76 std::vector<std::byte> decrypted_contents(length);
81 std::byte damage_val{(uint8_t)(1U << (key_position & 7))};
82 std::vector<std::byte> bad_key = key;
83 bad_key[key_position] ^= damage_val;
86 ok = bad_aead.
Decrypt(cipher, aad, nonce, decrypted_contents);
93 unsigned damage_pos = damage_bit >> 3;
94 std::byte damage_val{(uint8_t)(1U << (damage_bit & 7))};
95 if (damage_pos >= cipher.size()) {
96 aad[damage_pos - cipher.size()] ^= damage_val;
98 cipher[damage_pos] ^= damage_val;
102 if (use_splits && length > 0) {
104 ok = aead.
Decrypt(cipher, aad, nonce, std::span{decrypted_contents}.first(split_index), std::span{decrypted_contents}.subspan(split_index));
106 ok = aead.
Decrypt(cipher, aad, nonce, decrypted_contents);
112 assert(decrypted_contents == plain);
139 bool use_splits = mode & 1;
140 bool damage = mode & 4;
141 unsigned aad_length_bits = 3 * ((mode >> 3) & 3);
143 unsigned length_bits = 2 * ((mode >> 5) & 7);
151 if (use_splits && length > 0) {
153 enc_aead.
Encrypt(std::span{plain}.first(split_index), std::span{plain}.subspan(split_index), aad, cipher);
155 enc_aead.
Encrypt(plain, aad, cipher);
158 std::vector<std::byte> decrypted_contents(length);
163 std::byte damage_val{(uint8_t)(1U << (key_position & 7))};
164 std::vector<std::byte> bad_key = key;
165 bad_key[key_position] ^= damage_val;
169 ok = bad_fs_aead.
Decrypt(cipher, aad, decrypted_contents);
176 unsigned damage_pos = damage_bit >> 3;
177 std::byte damage_val{(uint8_t)(1U << (damage_bit & 7))};
178 if (damage_pos >= cipher.size()) {
179 aad[damage_pos - cipher.size()] ^= damage_val;
181 cipher[damage_pos] ^= damage_val;
186 if (use_splits && length > 0) {
188 ok = dec_aead.
Decrypt(cipher, aad, std::span{decrypted_contents}.first(split_index), std::span{decrypted_contents}.subspan(split_index));
190 ok = dec_aead.
Decrypt(cipher, aad, decrypted_contents);
196 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.