Bitcoin Core  22.99.0
P2P Digital Currency
crypto_common.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 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/common.h>
7 #include <test/fuzz/fuzz.h>
8 #include <test/fuzz/util.h>
9 
10 #include <array>
11 #include <cassert>
12 #include <cstdint>
13 #include <cstring>
14 #include <vector>
15 
16 FUZZ_TARGET(crypto_common)
17 {
18  FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
19  const uint16_t random_u16 = fuzzed_data_provider.ConsumeIntegral<uint16_t>();
20  const uint32_t random_u32 = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
21  const uint64_t random_u64 = fuzzed_data_provider.ConsumeIntegral<uint64_t>();
22  const std::vector<uint8_t> random_bytes_2 = ConsumeFixedLengthByteVector(fuzzed_data_provider, 2);
23  const std::vector<uint8_t> random_bytes_4 = ConsumeFixedLengthByteVector(fuzzed_data_provider, 4);
24  const std::vector<uint8_t> random_bytes_8 = ConsumeFixedLengthByteVector(fuzzed_data_provider, 8);
25 
26  std::array<uint8_t, 2> writele16_arr;
27  WriteLE16(writele16_arr.data(), random_u16);
28  assert(ReadLE16(writele16_arr.data()) == random_u16);
29 
30  std::array<uint8_t, 4> writele32_arr;
31  WriteLE32(writele32_arr.data(), random_u32);
32  assert(ReadLE32(writele32_arr.data()) == random_u32);
33 
34  std::array<uint8_t, 8> writele64_arr;
35  WriteLE64(writele64_arr.data(), random_u64);
36  assert(ReadLE64(writele64_arr.data()) == random_u64);
37 
38  std::array<uint8_t, 4> writebe32_arr;
39  WriteBE32(writebe32_arr.data(), random_u32);
40  assert(ReadBE32(writebe32_arr.data()) == random_u32);
41 
42  std::array<uint8_t, 8> writebe64_arr;
43  WriteBE64(writebe64_arr.data(), random_u64);
44  assert(ReadBE64(writebe64_arr.data()) == random_u64);
45 
46  const uint16_t readle16_result = ReadLE16(random_bytes_2.data());
47  std::array<uint8_t, 2> readle16_arr;
48  WriteLE16(readle16_arr.data(), readle16_result);
49  assert(std::memcmp(random_bytes_2.data(), readle16_arr.data(), 2) == 0);
50 
51  const uint32_t readle32_result = ReadLE32(random_bytes_4.data());
52  std::array<uint8_t, 4> readle32_arr;
53  WriteLE32(readle32_arr.data(), readle32_result);
54  assert(std::memcmp(random_bytes_4.data(), readle32_arr.data(), 4) == 0);
55 
56  const uint64_t readle64_result = ReadLE64(random_bytes_8.data());
57  std::array<uint8_t, 8> readle64_arr;
58  WriteLE64(readle64_arr.data(), readle64_result);
59  assert(std::memcmp(random_bytes_8.data(), readle64_arr.data(), 8) == 0);
60 
61  const uint32_t readbe32_result = ReadBE32(random_bytes_4.data());
62  std::array<uint8_t, 4> readbe32_arr;
63  WriteBE32(readbe32_arr.data(), readbe32_result);
64  assert(std::memcmp(random_bytes_4.data(), readbe32_arr.data(), 4) == 0);
65 
66  const uint64_t readbe64_result = ReadBE64(random_bytes_8.data());
67  std::array<uint8_t, 8> readbe64_arr;
68  WriteBE64(readbe64_arr.data(), readbe64_result);
69  assert(std::memcmp(random_bytes_8.data(), readbe64_arr.data(), 8) == 0);
70 }
FUZZ_TARGET
FUZZ_TARGET(crypto_common)
Definition: crypto_common.cpp:16
ReadLE64
static uint64_t ReadLE64(const unsigned char *ptr)
Definition: common.h:31
ReadLE32
static uint32_t ReadLE32(const unsigned char *ptr)
Definition: common.h:24
assert
assert(!tx.IsCoinBase())
WriteLE32
static void WriteLE32(unsigned char *ptr, uint32_t x)
Definition: common.h:44
util.h
WriteLE16
static void WriteLE16(unsigned char *ptr, uint16_t x)
Definition: common.h:38
ReadBE32
static uint32_t ReadBE32(const unsigned char *ptr)
Definition: common.h:63
common.h
FuzzedDataProvider.h
WriteBE32
static void WriteBE32(unsigned char *ptr, uint32_t x)
Definition: common.h:77
ReadLE16
static uint16_t ReadLE16(const unsigned char *ptr)
Definition: common.h:17
WriteBE64
static void WriteBE64(unsigned char *ptr, uint64_t x)
Definition: common.h:83
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
FuzzedDataProvider::ConsumeIntegral
T ConsumeIntegral()
Definition: FuzzedDataProvider.h:194
ReadBE64
static uint64_t ReadBE64(const unsigned char *ptr)
Definition: common.h:70
WriteLE64
static void WriteLE64(unsigned char *ptr, uint64_t x)
Definition: common.h:50
ConsumeFixedLengthByteVector
std::vector< uint8_t > ConsumeFixedLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const size_t length) noexcept
Returns a byte vector of specified size regardless of the number of remaining bytes available from th...
Definition: util.h:230