Bitcoin Core  22.99.0
P2P Digital Currency
crypto.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 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/sha3.h>
11 #include <crypto/sha512.h>
12 #include <hash.h>
14 #include <test/fuzz/fuzz.h>
15 #include <test/fuzz/util.h>
16 
17 #include <cstdint>
18 #include <vector>
19 
20 FUZZ_TARGET(crypto)
21 {
22  FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
23  std::vector<uint8_t> data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
24  if (data.empty()) {
25  data.resize(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 4096), fuzzed_data_provider.ConsumeIntegral<uint8_t>());
26  }
27 
28  CHash160 hash160;
29  CHash256 hash256;
30  CHMAC_SHA256 hmac_sha256{data.data(), data.size()};
31  CHMAC_SHA512 hmac_sha512{data.data(), data.size()};
33  CSHA1 sha1;
36  SHA3_256 sha3;
37  CSipHasher sip_hasher{fuzzed_data_provider.ConsumeIntegral<uint64_t>(), fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
38 
39  LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 30)
40  {
41  CallOneOf(
42  fuzzed_data_provider,
43  [&] {
44  if (fuzzed_data_provider.ConsumeBool()) {
45  data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
46  if (data.empty()) {
47  data.resize(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 4096), fuzzed_data_provider.ConsumeIntegral<uint8_t>());
48  }
49  }
50 
51  (void)hash160.Write(data);
52  (void)hash256.Write(data);
53  (void)hmac_sha256.Write(data.data(), data.size());
54  (void)hmac_sha512.Write(data.data(), data.size());
55  (void)ripemd160.Write(data.data(), data.size());
56  (void)sha1.Write(data.data(), data.size());
57  (void)sha256.Write(data.data(), data.size());
58  (void)sha3.Write(data);
59  (void)sha512.Write(data.data(), data.size());
60  (void)sip_hasher.Write(data.data(), data.size());
61 
62  (void)Hash(data);
63  (void)Hash160(data);
64  (void)sha512.Size();
65  },
66  [&] {
67  (void)hash160.Reset();
68  (void)hash256.Reset();
69  (void)ripemd160.Reset();
70  (void)sha1.Reset();
71  (void)sha256.Reset();
72  (void)sha3.Reset();
73  (void)sha512.Reset();
74  },
75  [&] {
76  CallOneOf(
77  fuzzed_data_provider,
78  [&] {
79  data.resize(CHash160::OUTPUT_SIZE);
80  hash160.Finalize(data);
81  },
82  [&] {
83  data.resize(CHash256::OUTPUT_SIZE);
84  hash256.Finalize(data);
85  },
86  [&] {
87  data.resize(CHMAC_SHA256::OUTPUT_SIZE);
88  hmac_sha256.Finalize(data.data());
89  },
90  [&] {
91  data.resize(CHMAC_SHA512::OUTPUT_SIZE);
92  hmac_sha512.Finalize(data.data());
93  },
94  [&] {
95  data.resize(CRIPEMD160::OUTPUT_SIZE);
96  ripemd160.Finalize(data.data());
97  },
98  [&] {
99  data.resize(CSHA1::OUTPUT_SIZE);
100  sha1.Finalize(data.data());
101  },
102  [&] {
103  data.resize(CSHA256::OUTPUT_SIZE);
104  sha256.Finalize(data.data());
105  },
106  [&] {
107  data.resize(CSHA512::OUTPUT_SIZE);
108  sha512.Finalize(data.data());
109  },
110  [&] {
111  data.resize(1);
112  data[0] = sip_hasher.Finalize() % 256;
113  },
114  [&] {
115  data.resize(SHA3_256::OUTPUT_SIZE);
116  sha3.Finalize(data);
117  });
118  });
119  }
120  if (fuzzed_data_provider.ConsumeBool()) {
121  uint64_t state[25];
122  for (size_t i = 0; i < 25; ++i) {
123  state[i] = fuzzed_data_provider.ConsumeIntegral<uint64_t>();
124  }
125  KeccakF(state);
126  }
127 }
SHA3_256::OUTPUT_SIZE
static constexpr size_t OUTPUT_SIZE
Definition: sha3.h:33
hmac_sha256.h
CRIPEMD160
A hasher class for RIPEMD-160.
Definition: ripemd160.h:12
CHash256::Write
CHash256 & Write(Span< const unsigned char > input)
Definition: hash.h:37
sha1.h
CHash256::Reset
CHash256 & Reset()
Definition: hash.h:42
SHA3_256
Definition: sha3.h:16
sha512
Internal SHA-512 implementation.
Definition: sha512.cpp:15
CHMAC_SHA256
A hasher class for HMAC-SHA-256.
Definition: hmac_sha256.h:14
KeccakF
void KeccakF(uint64_t(&st)[25])
The Keccak-f[1600] transform.
Definition: sha3.cpp:23
sha512.h
util.h
sha256
Internal SHA-256 implementation.
Definition: sha256.cpp:46
CRIPEMD160::OUTPUT_SIZE
static const size_t OUTPUT_SIZE
Definition: ripemd160.h:20
CHMAC_SHA512::OUTPUT_SIZE
static const size_t OUTPUT_SIZE
Definition: hmac_sha512.h:21
hmac_sha512.h
CSipHasher
SipHash-2-4.
Definition: siphash.h:13
SHA3_256::Reset
SHA3_256 & Reset()
Definition: sha3.cpp:155
Hash
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:75
CSHA512::OUTPUT_SIZE
static constexpr size_t OUTPUT_SIZE
Definition: sha512.h:20
CallOneOf
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:40
CHash256::OUTPUT_SIZE
static const size_t OUTPUT_SIZE
Definition: hash.h:28
CHash160
A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160).
Definition: hash.h:49
FuzzedDataProvider.h
CHash160::Finalize
void Finalize(Span< unsigned char > output)
Definition: hash.h:55
ConsumeRandomLengthByteVector
std::vector< uint8_t > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:61
CHash256::Finalize
void Finalize(Span< unsigned char > output)
Definition: hash.h:30
LIMITED_WHILE
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:18
SHA3_256::Write
SHA3_256 & Write(Span< const unsigned char > data)
Definition: sha3.cpp:111
CHMAC_SHA256::OUTPUT_SIZE
static const size_t OUTPUT_SIZE
Definition: hmac_sha256.h:21
ripemd160.h
CSHA1
A hasher class for SHA1.
Definition: sha1.h:12
CSHA256::OUTPUT_SIZE
static const size_t OUTPUT_SIZE
Definition: sha256.h:21
sha256.h
ripemd160
Internal RIPEMD-160 implementation.
Definition: ripemd160.cpp:15
Hash160
uint160 Hash160(const T1 &in1)
Compute the 160-bit hash an object.
Definition: hash.h:92
fuzz.h
SHA3_256::Finalize
SHA3_256 & Finalize(Span< unsigned char > output)
Definition: sha3.cpp:141
CSHA1::OUTPUT_SIZE
static const size_t OUTPUT_SIZE
Definition: sha1.h:20
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
CSHA512
A hasher class for SHA-512.
Definition: sha512.h:12
FUZZ_TARGET
FUZZ_TARGET(crypto)
Definition: crypto.cpp:20
CHash160::Write
CHash160 & Write(Span< const unsigned char > input)
Definition: hash.h:62
CSHA256
A hasher class for SHA-256.
Definition: sha256.h:13
sha1
Internal SHA-1 implementation.
Definition: sha1.cpp:15
CHash256
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
Definition: hash.h:24
sha3.h
CHash160::Reset
CHash160 & Reset()
Definition: hash.h:67
CHash160::OUTPUT_SIZE
static const size_t OUTPUT_SIZE
Definition: hash.h:53
CHMAC_SHA512
A hasher class for HMAC-SHA-512.
Definition: hmac_sha512.h:14