Skip to content

Commit fc7f84a

Browse files
tests: Add fuzzing harness for Keccak and SHA3_256
1 parent a47e596 commit fc7f84a

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/test/fuzz/crypto.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <crypto/ripemd160.h>
88
#include <crypto/sha1.h>
99
#include <crypto/sha256.h>
10+
#include <crypto/sha3.h>
1011
#include <crypto/sha512.h>
1112
#include <hash.h>
1213
#include <test/fuzz/FuzzedDataProvider.h>
@@ -32,6 +33,7 @@ void test_one_input(const std::vector<uint8_t>& buffer)
3233
CSHA1 sha1;
3334
CSHA256 sha256;
3435
CSHA512 sha512;
36+
SHA3_256 sha3;
3537
CSipHasher sip_hasher{fuzzed_data_provider.ConsumeIntegral<uint64_t>(), fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
3638

3739
while (fuzzed_data_provider.ConsumeBool()) {
@@ -51,6 +53,7 @@ void test_one_input(const std::vector<uint8_t>& buffer)
5153
(void)ripemd160.Write(data.data(), data.size());
5254
(void)sha1.Write(data.data(), data.size());
5355
(void)sha256.Write(data.data(), data.size());
56+
(void)sha3.Write(data);
5457
(void)sha512.Write(data.data(), data.size());
5558
(void)sip_hasher.Write(data.data(), data.size());
5659

@@ -65,11 +68,12 @@ void test_one_input(const std::vector<uint8_t>& buffer)
6568
(void)ripemd160.Reset();
6669
(void)sha1.Reset();
6770
(void)sha256.Reset();
71+
(void)sha3.Reset();
6872
(void)sha512.Reset();
6973
break;
7074
}
7175
case 2: {
72-
switch (fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 8)) {
76+
switch (fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 9)) {
7377
case 0: {
7478
data.resize(CHash160::OUTPUT_SIZE);
7579
hash160.Finalize(data);
@@ -115,9 +119,21 @@ void test_one_input(const std::vector<uint8_t>& buffer)
115119
data[0] = sip_hasher.Finalize() % 256;
116120
break;
117121
}
122+
case 9: {
123+
data.resize(SHA3_256::OUTPUT_SIZE);
124+
sha3.Finalize(data);
125+
break;
126+
}
118127
}
119128
break;
120129
}
121130
}
122131
}
132+
if (fuzzed_data_provider.ConsumeBool()) {
133+
uint64_t state[25];
134+
for (size_t i = 0; i < 25; ++i) {
135+
state[i] = fuzzed_data_provider.ConsumeIntegral<uint64_t>();
136+
}
137+
KeccakF(state);
138+
}
123139
}

0 commit comments

Comments
 (0)