18 for (
int i = 0; i < rekey_interval; ++i) {
21 aead.
Encrypt(std::span{dummy_tag}.first(0), std::span{dummy_tag}.first(0), dummy_tag);
23 aead.
Decrypt(dummy_tag, std::span{dummy_tag}.first(0), std::span{dummy_tag}.first(0));
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);
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);
The AEAD_CHACHA20_POLY1305 authenticated encryption algorithm from RFC8439 section 2....
ChaCha20::Nonce96 Nonce96
96-bit nonce type.
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.
static constexpr unsigned EXPANSION
Expansion when encrypting.
void Keystream(Nonce96 nonce, std::span< std::byte > keystream) noexcept
Get a number of keystream bytes from the underlying stream cipher.
Forward-secure wrapper around AEADChaCha20Poly1305.
bool Decrypt(std::span< const std::byte > cipher, std::span< const std::byte > aad, std::span< std::byte > plain) noexcept
Decrypt a message with a specified aad.
void Encrypt(std::span< const std::byte > plain, std::span< const std::byte > aad, std::span< std::byte > cipher) noexcept
Encrypt a message with a specified aad.
static constexpr auto EXPANSION
Expansion when encrypting.
std::vector< T > ConsumeBytes(size_t num_bytes)
T ConsumeIntegralInRange(T min, T max)
std::vector< B > randbytes(size_t len) noexcept
Generate random bytes.
LIMITED_WHILE(provider.remaining_bytes(), 10000)
static constexpr void crypt_till_rekey(FSChaCha20Poly1305 &aead, int rekey_interval, bool encrypt)
FUZZ_TARGET(crypto_aeadchacha20poly1305)
FuzzedDataProvider provider