mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-25 18:53:23 -03:00
tests: Add fuzzing harness for ChaCha20Poly1305AEAD
This commit is contained in:
parent
2fc4e5916c
commit
cca7c577d5
2 changed files with 79 additions and 0 deletions
|
@ -36,6 +36,7 @@ FUZZ_TARGETS = \
|
|||
test/fuzz/crypto_aes256 \
|
||||
test/fuzz/crypto_aes256cbc \
|
||||
test/fuzz/crypto_chacha20 \
|
||||
test/fuzz/crypto_chacha20_poly1305_aead \
|
||||
test/fuzz/crypto_common \
|
||||
test/fuzz/crypto_hkdf_hmac_sha256_l32 \
|
||||
test/fuzz/crypto_poly1305 \
|
||||
|
@ -509,6 +510,12 @@ test_fuzz_crypto_chacha20_LDADD = $(FUZZ_SUITE_LD_COMMON)
|
|||
test_fuzz_crypto_chacha20_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
|
||||
test_fuzz_crypto_chacha20_SOURCES = test/fuzz/crypto_chacha20.cpp
|
||||
|
||||
test_fuzz_crypto_chacha20_poly1305_aead_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
|
||||
test_fuzz_crypto_chacha20_poly1305_aead_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
|
||||
test_fuzz_crypto_chacha20_poly1305_aead_LDADD = $(FUZZ_SUITE_LD_COMMON)
|
||||
test_fuzz_crypto_chacha20_poly1305_aead_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
|
||||
test_fuzz_crypto_chacha20_poly1305_aead_SOURCES = test/fuzz/crypto_chacha20_poly1305_aead.cpp
|
||||
|
||||
test_fuzz_crypto_common_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
|
||||
test_fuzz_crypto_common_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
|
||||
test_fuzz_crypto_common_LDADD = $(FUZZ_SUITE_LD_COMMON)
|
||||
|
|
72
src/test/fuzz/crypto_chacha20_poly1305_aead.cpp
Normal file
72
src/test/fuzz/crypto_chacha20_poly1305_aead.cpp
Normal file
|
@ -0,0 +1,72 @@
|
|||
// Copyright (c) 2020 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <crypto/chacha_poly_aead.h>
|
||||
#include <crypto/poly1305.h>
|
||||
#include <test/fuzz/FuzzedDataProvider.h>
|
||||
#include <test/fuzz/fuzz.h>
|
||||
#include <test/fuzz/util.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <vector>
|
||||
|
||||
void test_one_input(const std::vector<uint8_t>& buffer)
|
||||
{
|
||||
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
|
||||
|
||||
const std::vector<uint8_t> k1 = ConsumeFixedLengthByteVector(fuzzed_data_provider, CHACHA20_POLY1305_AEAD_KEY_LEN);
|
||||
const std::vector<uint8_t> k2 = ConsumeFixedLengthByteVector(fuzzed_data_provider, CHACHA20_POLY1305_AEAD_KEY_LEN);
|
||||
|
||||
ChaCha20Poly1305AEAD aead(k1.data(), k1.size(), k2.data(), k2.size());
|
||||
uint64_t seqnr_payload = 0;
|
||||
uint64_t seqnr_aad = 0;
|
||||
int aad_pos = 0;
|
||||
size_t buffer_size = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096);
|
||||
std::vector<uint8_t> in(buffer_size + CHACHA20_POLY1305_AEAD_AAD_LEN + POLY1305_TAGLEN, 0);
|
||||
std::vector<uint8_t> out(buffer_size + CHACHA20_POLY1305_AEAD_AAD_LEN + POLY1305_TAGLEN, 0);
|
||||
bool is_encrypt = fuzzed_data_provider.ConsumeBool();
|
||||
while (fuzzed_data_provider.ConsumeBool()) {
|
||||
switch (fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 6)) {
|
||||
case 0: {
|
||||
buffer_size = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(64, 4096);
|
||||
in = std::vector<uint8_t>(buffer_size + CHACHA20_POLY1305_AEAD_AAD_LEN + POLY1305_TAGLEN, 0);
|
||||
out = std::vector<uint8_t>(buffer_size + CHACHA20_POLY1305_AEAD_AAD_LEN + POLY1305_TAGLEN, 0);
|
||||
break;
|
||||
}
|
||||
case 1: {
|
||||
(void)aead.Crypt(seqnr_payload, seqnr_aad, aad_pos, out.data(), out.size(), in.data(), buffer_size, is_encrypt);
|
||||
break;
|
||||
}
|
||||
case 2: {
|
||||
uint32_t len = 0;
|
||||
const bool ok = aead.GetLength(&len, seqnr_aad, aad_pos, in.data());
|
||||
assert(ok);
|
||||
break;
|
||||
}
|
||||
case 3: {
|
||||
seqnr_payload += 1;
|
||||
aad_pos += CHACHA20_POLY1305_AEAD_AAD_LEN;
|
||||
if (aad_pos + CHACHA20_POLY1305_AEAD_AAD_LEN > CHACHA20_ROUND_OUTPUT) {
|
||||
aad_pos = 0;
|
||||
seqnr_aad += 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 4: {
|
||||
seqnr_payload = fuzzed_data_provider.ConsumeIntegral<int>();
|
||||
break;
|
||||
}
|
||||
case 5: {
|
||||
seqnr_aad = fuzzed_data_provider.ConsumeIntegral<int>();
|
||||
break;
|
||||
}
|
||||
case 6: {
|
||||
is_encrypt = fuzzed_data_provider.ConsumeBool();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue