Skip to content

Commit 9352c32

Browse files
tests: Add fuzzing harness for AES256Encrypt/AES256Decrypt
1 parent c8fa03d commit 9352c32

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/Makefile.test.include

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ FUZZ_TARGETS = \
3333
test/fuzz/coins_deserialize \
3434
test/fuzz/coins_view \
3535
test/fuzz/crypto \
36+
test/fuzz/crypto_aes256 \
3637
test/fuzz/crypto_common \
3738
test/fuzz/cuckoocache \
3839
test/fuzz/decode_tx \
@@ -486,6 +487,12 @@ test_fuzz_crypto_LDADD = $(FUZZ_SUITE_LD_COMMON)
486487
test_fuzz_crypto_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
487488
test_fuzz_crypto_SOURCES = test/fuzz/crypto.cpp
488489

490+
test_fuzz_crypto_aes256_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
491+
test_fuzz_crypto_aes256_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
492+
test_fuzz_crypto_aes256_LDADD = $(FUZZ_SUITE_LD_COMMON)
493+
test_fuzz_crypto_aes256_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
494+
test_fuzz_crypto_aes256_SOURCES = test/fuzz/crypto_aes256.cpp
495+
489496
test_fuzz_crypto_common_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
490497
test_fuzz_crypto_common_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
491498
test_fuzz_crypto_common_LDADD = $(FUZZ_SUITE_LD_COMMON)

src/test/fuzz/crypto_aes256.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) 2020 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/aes.h>
6+
#include <test/fuzz/FuzzedDataProvider.h>
7+
#include <test/fuzz/fuzz.h>
8+
#include <test/fuzz/util.h>
9+
10+
#include <cassert>
11+
#include <cstdint>
12+
#include <vector>
13+
14+
void test_one_input(const std::vector<uint8_t>& buffer)
15+
{
16+
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
17+
const std::vector<uint8_t> key = ConsumeFixedLengthByteVector(fuzzed_data_provider, AES256_KEYSIZE);
18+
19+
AES256Encrypt encrypt{key.data()};
20+
AES256Decrypt decrypt{key.data()};
21+
22+
while (fuzzed_data_provider.ConsumeBool()) {
23+
const std::vector<uint8_t> plaintext = ConsumeFixedLengthByteVector(fuzzed_data_provider, AES_BLOCKSIZE);
24+
std::vector<uint8_t> ciphertext(AES_BLOCKSIZE);
25+
encrypt.Encrypt(ciphertext.data(), plaintext.data());
26+
std::vector<uint8_t> decrypted_plaintext(AES_BLOCKSIZE);
27+
decrypt.Decrypt(decrypted_plaintext.data(), ciphertext.data());
28+
assert(decrypted_plaintext == plaintext);
29+
}
30+
}

0 commit comments

Comments
 (0)