Bitcoin Core  22.99.0
P2P Digital Currency
crypto_chacha20.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/chacha20.h>
7 #include <test/fuzz/fuzz.h>
8 #include <test/fuzz/util.h>
9 
10 #include <cstdint>
11 #include <vector>
12 
13 FUZZ_TARGET(crypto_chacha20)
14 {
15  FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
16 
17  ChaCha20 chacha20;
18  if (fuzzed_data_provider.ConsumeBool()) {
19  const std::vector<unsigned char> key = ConsumeFixedLengthByteVector(fuzzed_data_provider, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(16, 32));
20  chacha20 = ChaCha20{key.data(), key.size()};
21  }
22  while (fuzzed_data_provider.ConsumeBool()) {
23  CallOneOf(
24  fuzzed_data_provider,
25  [&] {
26  const std::vector<unsigned char> key = ConsumeFixedLengthByteVector(fuzzed_data_provider, fuzzed_data_provider.ConsumeIntegralInRange<size_t>(16, 32));
27  chacha20.SetKey(key.data(), key.size());
28  },
29  [&] {
30  chacha20.SetIV(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
31  },
32  [&] {
33  chacha20.Seek(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
34  },
35  [&] {
36  std::vector<uint8_t> output(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096));
37  chacha20.Keystream(output.data(), output.size());
38  },
39  [&] {
40  std::vector<uint8_t> output(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096));
41  const std::vector<uint8_t> input = ConsumeFixedLengthByteVector(fuzzed_data_provider, output.size());
42  chacha20.Crypt(input.data(), output.data(), input.size());
43  });
44  }
45 }
ChaCha20::SetKey
void SetKey(const unsigned char *key, size_t keylen)
set key with flexible keylength; 256bit recommended *‍/
Definition: chacha20.cpp:24
util.h
chacha20.h
ChaCha20::Keystream
void Keystream(unsigned char *c, size_t bytes)
outputs the keystream of size <bytes> into
Definition: chacha20.cpp:74
CallOneOf
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:40
ChaCha20::Crypt
void Crypt(const unsigned char *input, unsigned char *output, size_t bytes)
enciphers the message <input> of length <bytes> and write the enciphered representation into <output>...
Definition: chacha20.cpp:182
ChaCha20::Seek
void Seek(uint64_t pos)
Definition: chacha20.cpp:68
FuzzedDataProvider.h
FUZZ_TARGET
FUZZ_TARGET(crypto_chacha20)
Definition: crypto_chacha20.cpp:13
ChaCha20::SetIV
void SetIV(uint64_t iv)
Definition: chacha20.cpp:62
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
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
ChaCha20
A class for ChaCha20 256-bit stream cipher developed by Daniel J.
Definition: chacha20.h:13