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