Skip to content

Commit 4cee53b

Browse files
tests: Add fuzzing harness for AES256CBCEncrypt/AES256CBCDecrypt
1 parent 9352c32 commit 4cee53b

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/Makefile.test.include

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ FUZZ_TARGETS = \
3434
test/fuzz/coins_view \
3535
test/fuzz/crypto \
3636
test/fuzz/crypto_aes256 \
37+
test/fuzz/crypto_aes256cbc \
3738
test/fuzz/crypto_common \
3839
test/fuzz/cuckoocache \
3940
test/fuzz/decode_tx \
@@ -493,6 +494,12 @@ test_fuzz_crypto_aes256_LDADD = $(FUZZ_SUITE_LD_COMMON)
493494
test_fuzz_crypto_aes256_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
494495
test_fuzz_crypto_aes256_SOURCES = test/fuzz/crypto_aes256.cpp
495496

497+
test_fuzz_crypto_aes256cbc_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
498+
test_fuzz_crypto_aes256cbc_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
499+
test_fuzz_crypto_aes256cbc_LDADD = $(FUZZ_SUITE_LD_COMMON)
500+
test_fuzz_crypto_aes256cbc_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
501+
test_fuzz_crypto_aes256cbc_SOURCES = test/fuzz/crypto_aes256cbc.cpp
502+
496503
test_fuzz_crypto_common_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
497504
test_fuzz_crypto_common_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
498505
test_fuzz_crypto_common_LDADD = $(FUZZ_SUITE_LD_COMMON)

src/test/fuzz/crypto_aes256cbc.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
const std::vector<uint8_t> iv = ConsumeFixedLengthByteVector(fuzzed_data_provider, AES_BLOCKSIZE);
19+
const bool pad = fuzzed_data_provider.ConsumeBool();
20+
21+
AES256CBCEncrypt encrypt{key.data(), iv.data(), pad};
22+
AES256CBCDecrypt decrypt{key.data(), iv.data(), pad};
23+
24+
while (fuzzed_data_provider.ConsumeBool()) {
25+
const std::vector<uint8_t> plaintext = ConsumeRandomLengthByteVector(fuzzed_data_provider);
26+
std::vector<uint8_t> ciphertext(plaintext.size() + AES_BLOCKSIZE);
27+
const int encrypt_ret = encrypt.Encrypt(plaintext.data(), plaintext.size(), ciphertext.data());
28+
ciphertext.resize(encrypt_ret);
29+
std::vector<uint8_t> decrypted_plaintext(ciphertext.size());
30+
const int decrypt_ret = decrypt.Decrypt(ciphertext.data(), ciphertext.size(), decrypted_plaintext.data());
31+
decrypted_plaintext.resize(decrypt_ret);
32+
assert(decrypted_plaintext == plaintext || (!pad && plaintext.size() % AES_BLOCKSIZE != 0 && encrypt_ret == 0 && decrypt_ret == 0));
33+
}
34+
}

0 commit comments

Comments
 (0)