Branch data Line data Source code
1 : : // Copyright (c) 2020-present 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/poly1305.h>
6 : : #include <test/fuzz/FuzzedDataProvider.h>
7 : : #include <test/fuzz/fuzz.h>
8 : : #include <test/fuzz/util.h>
9 : : #include <util/check.h>
10 : :
11 : : #include <cstdint>
12 : : #include <vector>
13 : :
14 [ + - ]: 518 : FUZZ_TARGET(crypto_poly1305)
15 : : {
16 : 42 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
17 : :
18 : 42 : const auto key = ConsumeFixedLengthByteVector<std::byte>(fuzzed_data_provider, Poly1305::KEYLEN);
19 : 42 : const auto in = ConsumeRandomLengthByteVector<std::byte>(fuzzed_data_provider);
20 : :
21 [ + - ]: 42 : std::vector<std::byte> tag_out(Poly1305::TAGLEN);
22 [ - + - + : 42 : Poly1305{key}.Update(in).Finalize(tag_out);
- + ]
23 : 42 : }
24 : :
25 [ + - ]: 557 : FUZZ_TARGET(crypto_poly1305_split)
26 : : {
27 : 81 : FuzzedDataProvider provider{buffer.data(), buffer.size()};
28 : :
29 : : // Read key and instantiate two Poly1305 objects with it.
30 : 81 : auto key = provider.ConsumeBytes<std::byte>(Poly1305::KEYLEN);
31 [ + - ]: 81 : key.resize(Poly1305::KEYLEN);
32 [ - + - + ]: 81 : Poly1305 poly_full{key}, poly_split{key};
33 : :
34 : : // Vector that holds all bytes processed so far.
35 : 81 : std::vector<std::byte> total_input;
36 : :
37 : : // Process input in pieces.
38 [ + + + + ]: 1801 : LIMITED_WHILE (provider.remaining_bytes(), 100) {
39 : 1720 : auto in = ConsumeRandomLengthByteVector<std::byte>(provider);
40 [ - + ]: 1720 : poly_split.Update(in);
41 : : // Update total_input to match what was processed.
42 [ + - ]: 1720 : total_input.insert(total_input.end(), in.begin(), in.end());
43 : 1720 : }
44 : :
45 : : // Process entire input at once.
46 [ - + ]: 81 : poly_full.Update(total_input);
47 : :
48 : : // Verify both agree.
49 : 81 : std::array<std::byte, Poly1305::TAGLEN> tag_split, tag_full;
50 : 81 : poly_split.Finalize(tag_split);
51 : 81 : poly_full.Finalize(tag_full);
52 [ - + ]: 81 : assert(tag_full == tag_split);
53 : 81 : }
|