|
13 | 13 | #include <cstdint>
|
14 | 14 | #include <vector>
|
15 | 15 |
|
| 16 | +constexpr static inline void crypt_till_rekey(FSChaCha20Poly1305& aead, int rekey_interval, bool encrypt) |
| 17 | +{ |
| 18 | + for (int i = 0; i < rekey_interval; ++i) { |
| 19 | + std::byte dummy_tag[FSChaCha20Poly1305::EXPANSION] = {{}}; |
| 20 | + if (encrypt) { |
| 21 | + aead.Encrypt(Span{dummy_tag}.first(0), Span{dummy_tag}.first(0), dummy_tag); |
| 22 | + } else { |
| 23 | + aead.Decrypt(dummy_tag, Span{dummy_tag}.first(0), Span{dummy_tag}.first(0)); |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
16 | 28 | FUZZ_TARGET(crypto_aeadchacha20poly1305)
|
17 | 29 | {
|
18 | 30 | FuzzedDataProvider provider{buffer.data(), buffer.size()};
|
@@ -103,3 +115,90 @@ FUZZ_TARGET(crypto_aeadchacha20poly1305)
|
103 | 115 | assert(decrypted_contents == plain);
|
104 | 116 | }
|
105 | 117 | }
|
| 118 | + |
| 119 | +FUZZ_TARGET(crypto_fschacha20poly1305) |
| 120 | +{ |
| 121 | + FuzzedDataProvider provider{buffer.data(), buffer.size()}; |
| 122 | + |
| 123 | + uint32_t rekey_interval = provider.ConsumeIntegralInRange<size_t>(32, 512); |
| 124 | + auto key = provider.ConsumeBytes<std::byte>(32); |
| 125 | + key.resize(32); |
| 126 | + FSChaCha20Poly1305 enc_aead(key, rekey_interval); |
| 127 | + FSChaCha20Poly1305 dec_aead(key, rekey_interval); |
| 128 | + |
| 129 | + // Initialize RNG deterministically, to generate contents and AAD. We assume that there are no |
| 130 | + // (potentially buggy) edge cases triggered by specific values of contents/AAD, so we can avoid |
| 131 | + // reading the actual data for those from the fuzzer input (which would need large amounts of |
| 132 | + // data). |
| 133 | + XoRoShiRo128PlusPlus rng(provider.ConsumeIntegral<uint64_t>()); |
| 134 | + |
| 135 | + LIMITED_WHILE(provider.ConsumeBool(), 10000) |
| 136 | + { |
| 137 | + // Mode: |
| 138 | + // - Bit 0: whether to use single-plain Encrypt/Decrypt; otherwise use a split at prefix. |
| 139 | + // - Bit 2: whether this ciphertext will be corrupted (making it the last sent one) |
| 140 | + // - Bit 3-4: controls the maximum aad length (max 511 bytes) |
| 141 | + // - Bit 5-7: controls the maximum content length (max 16383 bytes, for performance reasons) |
| 142 | + unsigned mode = provider.ConsumeIntegral<uint8_t>(); |
| 143 | + bool use_splits = mode & 1; |
| 144 | + bool damage = mode & 4; |
| 145 | + unsigned aad_length_bits = 3 * ((mode >> 3) & 3); |
| 146 | + unsigned aad_length = provider.ConsumeIntegralInRange<unsigned>(0, (1 << aad_length_bits) - 1); |
| 147 | + unsigned length_bits = 2 * ((mode >> 5) & 7); |
| 148 | + unsigned length = provider.ConsumeIntegralInRange<unsigned>(0, (1 << length_bits) - 1); |
| 149 | + // Generate aad and content. |
| 150 | + std::vector<std::byte> aad(aad_length); |
| 151 | + for (auto& val : aad) val = std::byte{(uint8_t)rng()}; |
| 152 | + std::vector<std::byte> plain(length); |
| 153 | + for (auto& val : plain) val = std::byte{(uint8_t)rng()}; |
| 154 | + std::vector<std::byte> cipher(length + FSChaCha20Poly1305::EXPANSION); |
| 155 | + |
| 156 | + crypt_till_rekey(enc_aead, rekey_interval, true); |
| 157 | + if (use_splits && length > 0) { |
| 158 | + size_t split_index = provider.ConsumeIntegralInRange<size_t>(1, length); |
| 159 | + enc_aead.Encrypt(Span{plain}.first(split_index), Span{plain}.subspan(split_index), aad, cipher); |
| 160 | + } else { |
| 161 | + enc_aead.Encrypt(plain, aad, cipher); |
| 162 | + } |
| 163 | + |
| 164 | + std::vector<std::byte> decrypted_contents(length); |
| 165 | + bool ok{false}; |
| 166 | + |
| 167 | + // damage the key |
| 168 | + unsigned key_position = provider.ConsumeIntegralInRange<unsigned>(0, 31); |
| 169 | + std::byte damage_val{(uint8_t)(1U << (key_position & 7))}; |
| 170 | + std::vector<std::byte> bad_key = key; |
| 171 | + bad_key[key_position] ^= damage_val; |
| 172 | + |
| 173 | + FSChaCha20Poly1305 bad_fs_aead(bad_key, rekey_interval); |
| 174 | + crypt_till_rekey(bad_fs_aead, rekey_interval, false); |
| 175 | + ok = bad_fs_aead.Decrypt(cipher, aad, decrypted_contents); |
| 176 | + assert(!ok); |
| 177 | + |
| 178 | + // Optionally damage 1 bit in either the cipher (corresponding to a change in transit) |
| 179 | + // or the aad (to make sure that decryption will fail if the AAD mismatches). |
| 180 | + if (damage) { |
| 181 | + unsigned damage_bit = provider.ConsumeIntegralInRange<unsigned>(0, (cipher.size() + aad.size()) * 8U - 1U); |
| 182 | + unsigned damage_pos = damage_bit >> 3; |
| 183 | + std::byte damage_val{(uint8_t)(1U << (damage_bit & 7))}; |
| 184 | + if (damage_pos >= cipher.size()) { |
| 185 | + aad[damage_pos - cipher.size()] ^= damage_val; |
| 186 | + } else { |
| 187 | + cipher[damage_pos] ^= damage_val; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + crypt_till_rekey(dec_aead, rekey_interval, false); |
| 192 | + if (use_splits && length > 0) { |
| 193 | + size_t split_index = provider.ConsumeIntegralInRange<size_t>(1, length); |
| 194 | + ok = dec_aead.Decrypt(cipher, aad, Span{decrypted_contents}.first(split_index), Span{decrypted_contents}.subspan(split_index)); |
| 195 | + } else { |
| 196 | + ok = dec_aead.Decrypt(cipher, aad, decrypted_contents); |
| 197 | + } |
| 198 | + |
| 199 | + // Decryption *must* fail if the packet was damaged, and succeed if it wasn't. |
| 200 | + assert(!ok == damage); |
| 201 | + if (!ok) break; |
| 202 | + assert(decrypted_contents == plain); |
| 203 | + } |
| 204 | +} |
0 commit comments