Skip to content

Commit c807f33

Browse files
committed
Add fuzz test for AEADChacha20Poly1305
1 parent bd5d168 commit c807f33

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ test_fuzz_fuzz_SOURCES = \
309309
test/fuzz/crypto_aes256.cpp \
310310
test/fuzz/crypto_aes256cbc.cpp \
311311
test/fuzz/crypto_chacha20.cpp \
312+
test/fuzz/crypto_chacha20poly1305.cpp \
312313
test/fuzz/crypto_common.cpp \
313314
test/fuzz/crypto_diff_fuzz_chacha20.cpp \
314315
test/fuzz/crypto_hkdf_hmac_sha256_l32.cpp \
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright (c) 2020-2021 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <crypto/chacha20poly1305.h>
6+
#include <random.h>
7+
#include <span.h>
8+
#include <test/fuzz/FuzzedDataProvider.h>
9+
#include <test/fuzz/fuzz.h>
10+
#include <test/fuzz/util.h>
11+
12+
#include <cstddef>
13+
#include <cstdint>
14+
#include <vector>
15+
16+
FUZZ_TARGET(crypto_aeadchacha20poly1305)
17+
{
18+
FuzzedDataProvider provider{buffer.data(), buffer.size()};
19+
20+
auto key = provider.ConsumeBytes<std::byte>(32);
21+
key.resize(32);
22+
AEADChaCha20Poly1305 aead(key);
23+
24+
// Initialize RNG deterministically, to generate contents and AAD. We assume that there are no
25+
// (potentially buggy) edge cases triggered by specific values of contents/AAD, so we can avoid
26+
// reading the actual data for those from the fuzzer input (which would need large amounts of
27+
// data).
28+
InsecureRandomContext rng(provider.ConsumeIntegral<uint64_t>());
29+
30+
LIMITED_WHILE(provider.ConsumeBool(), 10000)
31+
{
32+
// Mode:
33+
// - Bit 0: whether to use single-plain Encrypt/Decrypt; otherwise use a split at prefix.
34+
// - Bit 2: whether this ciphertext will be corrupted (making it the last sent one)
35+
// - Bit 3-4: controls the maximum aad length (max 511 bytes)
36+
// - Bit 5-7: controls the maximum content length (max 16383 bytes, for performance reasons)
37+
unsigned mode = provider.ConsumeIntegral<uint8_t>();
38+
bool use_splits = mode & 1;
39+
bool damage = mode & 4;
40+
unsigned aad_length_bits = 3 * ((mode >> 3) & 3);
41+
unsigned aad_length = provider.ConsumeIntegralInRange<unsigned>(0, (1 << aad_length_bits) - 1);
42+
unsigned length_bits = 2 * ((mode >> 5) & 7);
43+
unsigned length = provider.ConsumeIntegralInRange<unsigned>(0, (1 << length_bits) - 1);
44+
// Generate aad and content.
45+
auto aad = rng.randbytes<std::byte>(aad_length);
46+
auto plain = rng.randbytes<std::byte>(length);
47+
std::vector<std::byte> cipher(length + AEADChaCha20Poly1305::EXPANSION);
48+
// Generate nonce
49+
AEADChaCha20Poly1305::Nonce96 nonce = {(uint32_t)rng(), rng()};
50+
51+
if (use_splits && length > 0) {
52+
size_t split_index = provider.ConsumeIntegralInRange<size_t>(1, length);
53+
aead.Encrypt(Span{plain}.first(split_index), Span{plain}.subspan(split_index), aad, nonce, cipher);
54+
} else {
55+
aead.Encrypt(plain, aad, nonce, cipher);
56+
}
57+
58+
// Test Keystream output
59+
std::vector<std::byte> keystream(length);
60+
aead.Keystream(nonce, keystream);
61+
for (size_t i = 0; i < length; ++i) {
62+
assert((plain[i] ^ keystream[i]) == cipher[i]);
63+
}
64+
65+
std::vector<std::byte> decrypted_contents(length);
66+
bool ok{false};
67+
68+
// damage the key
69+
unsigned key_position = provider.ConsumeIntegralInRange<unsigned>(0, 31);
70+
std::byte damage_val{(uint8_t)(1U << (key_position & 7))};
71+
std::vector<std::byte> bad_key = key;
72+
bad_key[key_position] ^= damage_val;
73+
74+
AEADChaCha20Poly1305 bad_aead(bad_key);
75+
ok = bad_aead.Decrypt(cipher, aad, nonce, decrypted_contents);
76+
assert(!ok);
77+
78+
// Optionally damage 1 bit in either the cipher (corresponding to a change in transit)
79+
// or the aad (to make sure that decryption will fail if the AAD mismatches).
80+
if (damage) {
81+
unsigned damage_bit = provider.ConsumeIntegralInRange<unsigned>(0, (cipher.size() + aad.size()) * 8U - 1U);
82+
unsigned damage_pos = damage_bit >> 3;
83+
std::byte damage_val{(uint8_t)(1U << (damage_bit & 7))};
84+
if (damage_pos >= cipher.size()) {
85+
aad[damage_pos - cipher.size()] ^= damage_val;
86+
} else {
87+
cipher[damage_pos] ^= damage_val;
88+
}
89+
}
90+
91+
if (use_splits && length > 0) {
92+
size_t split_index = provider.ConsumeIntegralInRange<size_t>(1, length);
93+
ok = aead.Decrypt(cipher, aad, nonce, Span{decrypted_contents}.first(split_index), Span{decrypted_contents}.subspan(split_index));
94+
} else {
95+
ok = aead.Decrypt(cipher, aad, nonce, decrypted_contents);
96+
}
97+
98+
// Decryption *must* fail if the packet was damaged, and succeed if it wasn't.
99+
assert(!ok == damage);
100+
if (!ok) break;
101+
assert(decrypted_contents == plain);
102+
}
103+
}

0 commit comments

Comments
 (0)