Skip to content

Commit 67bb7be

Browse files
tests: Add fuzzing harness for CHash{160,256}, C{HMAC_,}SHA{1,256,512}, CRIPEMD160, CSipHasher, etc.
1 parent 1c86ed4 commit 67bb7be

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

src/Makefile.test.include

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ FUZZ_TARGETS = \
3232
test/fuzz/checkqueue \
3333
test/fuzz/coins_deserialize \
3434
test/fuzz/coins_view \
35+
test/fuzz/crypto \
3536
test/fuzz/crypto_common \
3637
test/fuzz/cuckoocache \
3738
test/fuzz/decode_tx \
@@ -479,6 +480,12 @@ test_fuzz_coins_view_LDADD = $(FUZZ_SUITE_LD_COMMON)
479480
test_fuzz_coins_view_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
480481
test_fuzz_coins_view_SOURCES = test/fuzz/coins_view.cpp
481482

483+
test_fuzz_crypto_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
484+
test_fuzz_crypto_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
485+
test_fuzz_crypto_LDADD = $(FUZZ_SUITE_LD_COMMON)
486+
test_fuzz_crypto_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
487+
test_fuzz_crypto_SOURCES = test/fuzz/crypto.cpp
488+
482489
test_fuzz_crypto_common_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
483490
test_fuzz_crypto_common_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
484491
test_fuzz_crypto_common_LDADD = $(FUZZ_SUITE_LD_COMMON)

src/test/fuzz/crypto.cpp

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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.data(), data.size());
48+
(void)hash256.Write(data.data(), data.size());
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)Hash(data.begin(), data.end());
58+
(void)Hash160(data);
59+
(void)Hash160(data.begin(), data.end());
60+
(void)sha512.Size();
61+
break;
62+
}
63+
case 1: {
64+
(void)hash160.Reset();
65+
(void)hash256.Reset();
66+
(void)ripemd160.Reset();
67+
(void)sha1.Reset();
68+
(void)sha256.Reset();
69+
(void)sha512.Reset();
70+
break;
71+
}
72+
case 2: {
73+
switch (fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 8)) {
74+
case 0: {
75+
data.resize(CHash160::OUTPUT_SIZE);
76+
hash160.Finalize(data.data());
77+
break;
78+
}
79+
case 1: {
80+
data.resize(CHash256::OUTPUT_SIZE);
81+
hash256.Finalize(data.data());
82+
break;
83+
}
84+
case 2: {
85+
data.resize(CHMAC_SHA256::OUTPUT_SIZE);
86+
hmac_sha256.Finalize(data.data());
87+
break;
88+
}
89+
case 3: {
90+
data.resize(CHMAC_SHA512::OUTPUT_SIZE);
91+
hmac_sha512.Finalize(data.data());
92+
break;
93+
}
94+
case 4: {
95+
data.resize(CRIPEMD160::OUTPUT_SIZE);
96+
ripemd160.Finalize(data.data());
97+
break;
98+
}
99+
case 5: {
100+
data.resize(CSHA1::OUTPUT_SIZE);
101+
sha1.Finalize(data.data());
102+
break;
103+
}
104+
case 6: {
105+
data.resize(CSHA256::OUTPUT_SIZE);
106+
sha256.Finalize(data.data());
107+
break;
108+
}
109+
case 7: {
110+
data.resize(CSHA512::OUTPUT_SIZE);
111+
sha512.Finalize(data.data());
112+
break;
113+
}
114+
case 8: {
115+
data.resize(1);
116+
data[0] = sip_hasher.Finalize() % 256;
117+
break;
118+
}
119+
}
120+
break;
121+
}
122+
}
123+
}
124+
}

0 commit comments

Comments
 (0)