19 if (fuzzed_data_provider.ConsumeBool()) {
31 chacha20.
SetIV(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
34 chacha20.
Seek64(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
37 std::vector<uint8_t> output(fuzzed_data_provider.ConsumeIntegralInRange<
size_t>(0, 4096));
38 chacha20.
Keystream(output.data(), output.size());
41 std::vector<uint8_t> output(fuzzed_data_provider.ConsumeIntegralInRange<
size_t>(0, 4096));
43 chacha20.
Crypt(input.data(), output.data(), input.size());
58 template<
bool UseCrypt>
62 unsigned char key[32] = {0};
63 auto key_bytes = provider.
ConsumeBytes<
unsigned char>(32);
64 std::copy(key_bytes.begin(), key_bytes.end(), key);
79 std::vector<unsigned char> data1, data2;
80 data1.resize(total_bytes);
81 data2.resize(total_bytes);
85 if constexpr (UseCrypt) {
86 uint64_t seed = provider.ConsumeIntegral<uint64_t>();
89 while (bytes < (total_bytes & ~uint64_t{7})) {
95 if (bytes < total_bytes) {
96 unsigned char valbytes[8];
99 std::copy(valbytes, valbytes + (total_bytes - bytes), data1.data() + bytes);
100 std::copy(valbytes, valbytes + (total_bytes - bytes), data2.data() + bytes);
108 if constexpr (UseCrypt) {
109 crypt1.Crypt(data1.data(), data1.data(), total_bytes);
111 crypt1.Keystream(data1.data(), total_bytes);
118 bool is_last = (iter == 255) || (bytes2 == total_bytes) || provider.ConsumeBool();
122 uint64_t now = is_last ? total_bytes - bytes2 :
123 provider.ConsumeIntegralInRange<uint64_t>(0, total_bytes - bytes2);
127 if (UseCrypt || provider.ConsumeBool()) {
128 crypt2.Crypt(data2.data() + bytes2, data2.data() + bytes2, now);
130 crypt2.Keystream(data2.data() + bytes2, now);
136 assert(bytes2 == total_bytes);
146 ChaCha20SplitFuzz<true>(provider);
152 ChaCha20SplitFuzz<false>(provider);
Unrestricted ChaCha20 cipher.
void SetKey32(const unsigned char *key32)
set 32-byte key.
void Keystream(unsigned char *c, size_t bytes)
outputs the keystream of size <bytes> into
void SetIV(uint64_t iv)
set the 64-bit nonce.
void Seek64(uint64_t pos)
set the 64bit block counter (pos seeks to byte position 64*pos).
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>...
std::vector< T > ConsumeBytes(size_t num_bytes)
T ConsumeIntegralInRange(T min, T max)
static void WriteLE64(unsigned char *ptr, uint64_t x)
FUZZ_TARGET(crypto_chacha20)
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
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...
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)