Skip to content

Commit aa8f5cb

Browse files
committed
Add fuzz test for AEADChacha20Poly1305
1 parent dbd2000 commit aa8f5cb

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

src/Makefile.test.include

+1
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ test_fuzz_fuzz_SOURCES = \
305305
test/fuzz/crypto_aes256.cpp \
306306
test/fuzz/crypto_aes256cbc.cpp \
307307
test/fuzz/crypto_chacha20.cpp \
308+
test/fuzz/crypto_chacha20poly1305.cpp \
308309
test/fuzz/crypto_common.cpp \
309310
test/fuzz/crypto_diff_fuzz_chacha20.cpp \
310311
test/fuzz/crypto_hkdf_hmac_sha256_l32.cpp \
+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 <span.h>
7+
#include <test/fuzz/FuzzedDataProvider.h>
8+
#include <test/fuzz/fuzz.h>
9+
#include <test/fuzz/util.h>
10+
#include <test/util/xoroshiro128plusplus.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+
XoRoShiRo128PlusPlus 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+
std::vector<std::byte> aad(aad_length);
46+
for (auto& val : aad) val = std::byte{(uint8_t)rng()};
47+
std::vector<std::byte> plain(length);
48+
for (auto& val : plain) val = std::byte{(uint8_t)rng()};
49+
std::vector<std::byte> cipher(length + AEADChaCha20Poly1305::EXPANSION);
50+
// Generate nonce
51+
AEADChaCha20Poly1305::Nonce96 nonce = {(uint32_t)rng(), rng()};
52+
53+
if (use_splits && length > 0) {
54+
size_t split_index = provider.ConsumeIntegralInRange<size_t>(1, length);
55+
aead.Encrypt(Span{plain}.first(split_index), Span{plain}.subspan(split_index), aad, nonce, cipher);
56+
} else {
57+
aead.Encrypt(plain, aad, nonce, cipher);
58+
}
59+
60+
// Test Keystream output
61+
std::vector<std::byte> keystream(length);
62+
aead.Keystream(nonce, keystream);
63+
for (size_t i = 0; i < length; ++i) {
64+
assert((plain[i] ^ keystream[i]) == cipher[i]);
65+
}
66+
67+
std::vector<std::byte> decrypted_contents(length);
68+
bool ok{false};
69+
70+
// damage the key
71+
unsigned key_position = provider.ConsumeIntegralInRange<unsigned>(0, 31);
72+
std::byte damage_val{(uint8_t)(1U << (key_position & 7))};
73+
std::vector<std::byte> bad_key = key;
74+
bad_key[key_position] ^= damage_val;
75+
76+
AEADChaCha20Poly1305 bad_aead(bad_key);
77+
ok = bad_aead.Decrypt(cipher, aad, nonce, decrypted_contents);
78+
assert(!ok);
79+
80+
// Optionally damage 1 bit in either the cipher (corresponding to a change in transit)
81+
// or the aad (to make sure that decryption will fail if the AAD mismatches).
82+
if (damage) {
83+
unsigned damage_bit = provider.ConsumeIntegralInRange<unsigned>(0, (cipher.size() + aad.size()) * 8U - 1U);
84+
unsigned damage_pos = damage_bit >> 3;
85+
std::byte damage_val{(uint8_t)(1U << (damage_bit & 7))};
86+
if (damage_pos >= cipher.size()) {
87+
aad[damage_pos - cipher.size()] ^= damage_val;
88+
} else {
89+
cipher[damage_pos] ^= damage_val;
90+
}
91+
}
92+
93+
if (use_splits && length > 0) {
94+
size_t split_index = provider.ConsumeIntegralInRange<size_t>(1, length);
95+
ok = aead.Decrypt(cipher, aad, nonce, Span{decrypted_contents}.first(split_index), Span{decrypted_contents}.subspan(split_index));
96+
} else {
97+
ok = aead.Decrypt(cipher, aad, nonce, decrypted_contents);
98+
}
99+
100+
// Decryption *must* fail if the packet was damaged, and succeed if it wasn't.
101+
assert(!ok == damage);
102+
if (!ok) break;
103+
assert(decrypted_contents == plain);
104+
}
105+
}

0 commit comments

Comments
 (0)