Coverage Report

Created: 2024-10-29 12:10

/root/bitcoin/src/test/fuzz/crypto.cpp
Line
Count
Source (jump to first uncovered line)
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>
13
#include <test/fuzz/FuzzedDataProvider.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
0
{
22
0
    FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
23
0
    std::vector<uint8_t> data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
24
0
    if (data.empty()) {
25
0
        auto new_size = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 4096);
26
0
        auto x = fuzzed_data_provider.ConsumeIntegral<uint8_t>();
27
0
        data.resize(new_size, x);
28
0
    }
29
30
0
    CHash160 hash160;
31
0
    CHash256 hash256;
32
0
    CHMAC_SHA256 hmac_sha256{data.data(), data.size()};
33
0
    CHMAC_SHA512 hmac_sha512{data.data(), data.size()};
34
0
    CRIPEMD160 ripemd160;
35
0
    CSHA1 sha1;
36
0
    CSHA256 sha256;
37
0
    CSHA512 sha512;
38
0
    SHA3_256 sha3;
39
0
    CSipHasher sip_hasher{fuzzed_data_provider.ConsumeIntegral<uint64_t>(), fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
40
41
0
    LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 30)
42
0
    {
43
0
        CallOneOf(
44
0
            fuzzed_data_provider,
45
0
            [&] {
46
0
                if (fuzzed_data_provider.ConsumeBool()) {
47
0
                    data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
48
0
                    if (data.empty()) {
49
0
                        auto new_size = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 4096);
50
0
                        auto x = fuzzed_data_provider.ConsumeIntegral<uint8_t>();
51
0
                        data.resize(new_size, x);
52
0
                    }
53
0
                }
54
55
0
                (void)hash160.Write(data);
56
0
                (void)hash256.Write(data);
57
0
                (void)hmac_sha256.Write(data.data(), data.size());
58
0
                (void)hmac_sha512.Write(data.data(), data.size());
59
0
                (void)ripemd160.Write(data.data(), data.size());
60
0
                (void)sha1.Write(data.data(), data.size());
61
0
                (void)sha256.Write(data.data(), data.size());
62
0
                (void)sha3.Write(data);
63
0
                (void)sha512.Write(data.data(), data.size());
64
0
                (void)sip_hasher.Write(data);
65
66
0
                (void)Hash(data);
67
0
                (void)Hash160(data);
68
0
                (void)sha512.Size();
69
0
            },
70
0
            [&] {
71
0
                (void)hash160.Reset();
72
0
                (void)hash256.Reset();
73
0
                (void)ripemd160.Reset();
74
0
                (void)sha1.Reset();
75
0
                (void)sha256.Reset();
76
0
                (void)sha3.Reset();
77
0
                (void)sha512.Reset();
78
0
            },
79
0
            [&] {
80
0
                CallOneOf(
81
0
                    fuzzed_data_provider,
82
0
                    [&] {
83
0
                        data.resize(CHash160::OUTPUT_SIZE);
84
0
                        hash160.Finalize(data);
85
0
                    },
86
0
                    [&] {
87
0
                        data.resize(CHash256::OUTPUT_SIZE);
88
0
                        hash256.Finalize(data);
89
0
                    },
90
0
                    [&] {
91
0
                        data.resize(CHMAC_SHA256::OUTPUT_SIZE);
92
0
                        hmac_sha256.Finalize(data.data());
93
0
                    },
94
0
                    [&] {
95
0
                        data.resize(CHMAC_SHA512::OUTPUT_SIZE);
96
0
                        hmac_sha512.Finalize(data.data());
97
0
                    },
98
0
                    [&] {
99
0
                        data.resize(CRIPEMD160::OUTPUT_SIZE);
100
0
                        ripemd160.Finalize(data.data());
101
0
                    },
102
0
                    [&] {
103
0
                        data.resize(CSHA1::OUTPUT_SIZE);
104
0
                        sha1.Finalize(data.data());
105
0
                    },
106
0
                    [&] {
107
0
                        data.resize(CSHA256::OUTPUT_SIZE);
108
0
                        sha256.Finalize(data.data());
109
0
                    },
110
0
                    [&] {
111
0
                        data.resize(CSHA512::OUTPUT_SIZE);
112
0
                        sha512.Finalize(data.data());
113
0
                    },
114
0
                    [&] {
115
0
                        data.resize(1);
116
0
                        data[0] = sip_hasher.Finalize() % 256;
117
0
                    },
118
0
                    [&] {
119
0
                        data.resize(SHA3_256::OUTPUT_SIZE);
120
0
                        sha3.Finalize(data);
121
0
                    });
122
0
            });
123
0
    }
124
0
    if (fuzzed_data_provider.ConsumeBool()) {
125
0
        uint64_t state[25];
126
0
        for (size_t i = 0; i < 25; ++i) {
127
0
            state[i] = fuzzed_data_provider.ConsumeIntegral<uint64_t>();
128
0
        }
129
0
        KeccakF(state);
130
0
    }
131
0
}