Skip to content

Commit 8df92c2

Browse files
committed
merge bitcoin#19286: Add fuzzing harness for CHash{160,256}, C{HMAC_,}SHA{1,256,512}, CRIPEMD160, CSipHasher, etc.
1 parent 5cac560 commit 8df92c2

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

src/Makefile.test.include

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ FUZZ_TARGETS = \
3636
test/fuzz/checkqueue \
3737
test/fuzz/coins_deserialize \
3838
test/fuzz/coins_view \
39+
test/fuzz/crypto \
3940
test/fuzz/crypto_common \
4041
test/fuzz/cuckoocache \
4142
test/fuzz/decode_tx \
@@ -488,6 +489,12 @@ test_fuzz_coins_view_LDADD = $(FUZZ_SUITE_LD_COMMON)
488489
test_fuzz_coins_view_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS) $(LDFLAGS_WRAP_EXCEPTIONS)
489490
test_fuzz_coins_view_SOURCES = $(FUZZ_SUITE) test/fuzz/coins_view.cpp
490491

492+
test_fuzz_crypto_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
493+
test_fuzz_crypto_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
494+
test_fuzz_crypto_LDADD = $(FUZZ_SUITE_LD_COMMON)
495+
test_fuzz_crypto_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS) $(LDFLAGS_WRAP_EXCEPTIONS)
496+
test_fuzz_crypto_SOURCES = $(FUZZ_SUITE) test/fuzz/crypto.cpp
497+
491498
test_fuzz_crypto_common_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
492499
test_fuzz_crypto_common_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
493500
test_fuzz_crypto_common_LDADD = $(FUZZ_SUITE_LD_COMMON)

src/test/fuzz/crypto.cpp

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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/hmac_sha256.h>
6+
#include <crypto/hmac_sha512.h>
7+
#include <crypto/ripemd160.h>
8+
#include <crypto/sha1.h>
9+
#include <crypto/sha256.h>
10+
#include <crypto/sha512.h>
11+
#include <hash.h>
12+
#include <test/fuzz/FuzzedDataProvider.h>
13+
#include <test/fuzz/fuzz.h>
14+
#include <test/fuzz/util.h>
15+
16+
#include <cstdint>
17+
#include <vector>
18+
19+
void test_one_input(const std::vector<uint8_t>& buffer)
20+
{
21+
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
22+
std::vector<uint8_t> data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
23+
if (data.empty()) {
24+
data.resize(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 4096), fuzzed_data_provider.ConsumeIntegral<uint8_t>());
25+
}
26+
27+
CHash160 hash160;
28+
CHash256 hash256;
29+
CHMAC_SHA256 hmac_sha256{data.data(), data.size()};
30+
CHMAC_SHA512 hmac_sha512{data.data(), data.size()};
31+
CRIPEMD160 ripemd160;
32+
CSHA1 sha1;
33+
CSHA256 sha256;
34+
CSHA512 sha512;
35+
CSipHasher sip_hasher{fuzzed_data_provider.ConsumeIntegral<uint64_t>(), fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
36+
37+
while (fuzzed_data_provider.ConsumeBool()) {
38+
switch (fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 2)) {
39+
case 0: {
40+
if (fuzzed_data_provider.ConsumeBool()) {
41+
data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
42+
if (data.empty()) {
43+
data.resize(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 4096), fuzzed_data_provider.ConsumeIntegral<uint8_t>());
44+
}
45+
}
46+
47+
(void)hash160.Write(data);
48+
(void)hash256.Write(data);
49+
(void)hmac_sha256.Write(data.data(), data.size());
50+
(void)hmac_sha512.Write(data.data(), data.size());
51+
(void)ripemd160.Write(data.data(), data.size());
52+
(void)sha1.Write(data.data(), data.size());
53+
(void)sha256.Write(data.data(), data.size());
54+
(void)sha512.Write(data.data(), data.size());
55+
(void)sip_hasher.Write(data.data(), data.size());
56+
57+
(void)Hash160(data);
58+
(void)Hash160(data.begin(), data.end());
59+
(void)sha512.Size();
60+
break;
61+
}
62+
case 1: {
63+
(void)hash160.Reset();
64+
(void)hash256.Reset();
65+
(void)ripemd160.Reset();
66+
(void)sha1.Reset();
67+
(void)sha256.Reset();
68+
(void)sha512.Reset();
69+
break;
70+
}
71+
case 2: {
72+
switch (fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 8)) {
73+
case 0: {
74+
data.resize(CHash160::OUTPUT_SIZE);
75+
hash160.Finalize(data);
76+
break;
77+
}
78+
case 1: {
79+
data.resize(CHash256::OUTPUT_SIZE);
80+
hash256.Finalize(data);
81+
break;
82+
}
83+
case 2: {
84+
data.resize(CHMAC_SHA256::OUTPUT_SIZE);
85+
hmac_sha256.Finalize(data.data());
86+
break;
87+
}
88+
case 3: {
89+
data.resize(CHMAC_SHA512::OUTPUT_SIZE);
90+
hmac_sha512.Finalize(data.data());
91+
break;
92+
}
93+
case 4: {
94+
data.resize(CRIPEMD160::OUTPUT_SIZE);
95+
ripemd160.Finalize(data.data());
96+
break;
97+
}
98+
case 5: {
99+
data.resize(CSHA1::OUTPUT_SIZE);
100+
sha1.Finalize(data.data());
101+
break;
102+
}
103+
case 6: {
104+
data.resize(CSHA256::OUTPUT_SIZE);
105+
sha256.Finalize(data.data());
106+
break;
107+
}
108+
case 7: {
109+
data.resize(CSHA512::OUTPUT_SIZE);
110+
sha512.Finalize(data.data());
111+
break;
112+
}
113+
case 8: {
114+
data.resize(1);
115+
data[0] = sip_hasher.Finalize() % 256;
116+
break;
117+
}
118+
}
119+
break;
120+
}
121+
}
122+
}
123+
}

0 commit comments

Comments
 (0)