From fc7f84a9ca98ee0d9c2d1f092be6b5dba3f2a582 Mon Sep 17 00:00:00 2001 From: practicalswift Date: Thu, 10 Sep 2020 05:42:51 +0000 Subject: [PATCH] tests: Add fuzzing harness for Keccak and SHA3_256 --- src/test/fuzz/crypto.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/test/fuzz/crypto.cpp b/src/test/fuzz/crypto.cpp index 3edcf964959..664e65accc2 100644 --- a/src/test/fuzz/crypto.cpp +++ b/src/test/fuzz/crypto.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -32,6 +33,7 @@ void test_one_input(const std::vector& buffer) CSHA1 sha1; CSHA256 sha256; CSHA512 sha512; + SHA3_256 sha3; CSipHasher sip_hasher{fuzzed_data_provider.ConsumeIntegral(), fuzzed_data_provider.ConsumeIntegral()}; while (fuzzed_data_provider.ConsumeBool()) { @@ -51,6 +53,7 @@ void test_one_input(const std::vector& buffer) (void)ripemd160.Write(data.data(), data.size()); (void)sha1.Write(data.data(), data.size()); (void)sha256.Write(data.data(), data.size()); + (void)sha3.Write(data); (void)sha512.Write(data.data(), data.size()); (void)sip_hasher.Write(data.data(), data.size()); @@ -65,11 +68,12 @@ void test_one_input(const std::vector& buffer) (void)ripemd160.Reset(); (void)sha1.Reset(); (void)sha256.Reset(); + (void)sha3.Reset(); (void)sha512.Reset(); break; } case 2: { - switch (fuzzed_data_provider.ConsumeIntegralInRange(0, 8)) { + switch (fuzzed_data_provider.ConsumeIntegralInRange(0, 9)) { case 0: { data.resize(CHash160::OUTPUT_SIZE); hash160.Finalize(data); @@ -115,9 +119,21 @@ void test_one_input(const std::vector& buffer) data[0] = sip_hasher.Finalize() % 256; break; } + case 9: { + data.resize(SHA3_256::OUTPUT_SIZE); + sha3.Finalize(data); + break; + } } break; } } } + if (fuzzed_data_provider.ConsumeBool()) { + uint64_t state[25]; + for (size_t i = 0; i < 25; ++i) { + state[i] = fuzzed_data_provider.ConsumeIntegral(); + } + KeccakF(state); + } }