Skip to content

Commit bdcbc85

Browse files
committed
fuzz: support std::byte in Consume{Fixed,Variable}LengthByteVector
1 parent 7d1cd93 commit bdcbc85

File tree

3 files changed

+26
-25
lines changed

3 files changed

+26
-25
lines changed

src/test/fuzz/crypto_chacha20.cpp

+8-9
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ FUZZ_TARGET(crypto_chacha20)
1717
{
1818
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
1919

20-
const std::vector<unsigned char> key = ConsumeFixedLengthByteVector(fuzzed_data_provider, 32);
21-
ChaCha20 chacha20{MakeByteSpan(key)};
20+
const auto key = ConsumeFixedLengthByteVector<std::byte>(fuzzed_data_provider, ChaCha20::KEYLEN);
21+
ChaCha20 chacha20{key};
2222

2323
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
2424
CallOneOf(
2525
fuzzed_data_provider,
2626
[&] {
27-
std::vector<unsigned char> key = ConsumeFixedLengthByteVector(fuzzed_data_provider, 32);
28-
chacha20.SetKey(MakeByteSpan(key));
27+
auto key = ConsumeFixedLengthByteVector<std::byte>(fuzzed_data_provider, ChaCha20::KEYLEN);
28+
chacha20.SetKey(key);
2929
},
3030
[&] {
3131
chacha20.Seek(
@@ -39,9 +39,9 @@ FUZZ_TARGET(crypto_chacha20)
3939
chacha20.Keystream(MakeWritableByteSpan(output));
4040
},
4141
[&] {
42-
std::vector<uint8_t> output(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096));
43-
const std::vector<uint8_t> input = ConsumeFixedLengthByteVector(fuzzed_data_provider, output.size());
44-
chacha20.Crypt(MakeByteSpan(input), MakeWritableByteSpan(output));
42+
std::vector<std::byte> output(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096));
43+
const auto input = ConsumeFixedLengthByteVector<std::byte>(fuzzed_data_provider, output.size());
44+
chacha20.Crypt(input, output);
4545
});
4646
}
4747
}
@@ -60,8 +60,7 @@ template<bool UseCrypt>
6060
void ChaCha20SplitFuzz(FuzzedDataProvider& provider)
6161
{
6262
// Determine key, iv, start position, length.
63-
auto key_bytes = provider.ConsumeBytes<std::byte>(ChaCha20::KEYLEN);
64-
key_bytes.resize(ChaCha20::KEYLEN);
63+
auto key_bytes = ConsumeFixedLengthByteVector<std::byte>(provider, ChaCha20::KEYLEN);
6564
uint64_t iv = provider.ConsumeIntegral<uint64_t>();
6665
uint32_t iv_prefix = provider.ConsumeIntegral<uint32_t>();
6766
uint64_t total_bytes = provider.ConsumeIntegralInRange<uint64_t>(0, 1000000);

src/test/fuzz/crypto_poly1305.cpp

+6-7
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@ FUZZ_TARGET(crypto_poly1305)
1414
{
1515
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
1616

17-
const std::vector<uint8_t> key = ConsumeFixedLengthByteVector(fuzzed_data_provider, Poly1305::KEYLEN);
18-
const std::vector<uint8_t> in = ConsumeRandomLengthByteVector(fuzzed_data_provider);
17+
const auto key = ConsumeFixedLengthByteVector<std::byte>(fuzzed_data_provider, Poly1305::KEYLEN);
18+
const auto in = ConsumeRandomLengthByteVector<std::byte>(fuzzed_data_provider);
1919

2020
std::vector<std::byte> tag_out(Poly1305::TAGLEN);
21-
Poly1305{MakeByteSpan(key)}.Update(MakeByteSpan(in)).Finalize(tag_out);
21+
Poly1305{key}.Update(in).Finalize(tag_out);
2222
}
2323

24-
2524
FUZZ_TARGET(crypto_poly1305_split)
2625
{
2726
FuzzedDataProvider provider{buffer.data(), buffer.size()};
@@ -36,10 +35,10 @@ FUZZ_TARGET(crypto_poly1305_split)
3635

3736
// Process input in pieces.
3837
LIMITED_WHILE(provider.remaining_bytes(), 100) {
39-
auto in = provider.ConsumeRandomLengthString();
40-
poly_split.Update(MakeByteSpan(in));
38+
auto in = ConsumeRandomLengthByteVector<std::byte>(provider);
39+
poly_split.Update(in);
4140
// Update total_input to match what was processed.
42-
total_input.insert(total_input.end(), MakeByteSpan(in).begin(), MakeByteSpan(in).end());
41+
total_input.insert(total_input.end(), in.begin(), in.end());
4342
}
4443

4544
// Process entire input at once.

src/test/fuzz/util.h

+12-9
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,16 @@ auto& PickValue(FuzzedDataProvider& fuzzed_data_provider, Collection& col)
5353
return *it;
5454
}
5555

56-
[[nodiscard]] inline std::vector<uint8_t> ConsumeRandomLengthByteVector(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
56+
template<typename B = uint8_t>
57+
[[nodiscard]] inline std::vector<B> ConsumeRandomLengthByteVector(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
5758
{
59+
static_assert(sizeof(B) == 1);
5860
const std::string s = max_length ?
5961
fuzzed_data_provider.ConsumeRandomLengthString(*max_length) :
6062
fuzzed_data_provider.ConsumeRandomLengthString();
61-
return {s.begin(), s.end()};
63+
std::vector<B> ret(s.size());
64+
std::copy(s.begin(), s.end(), reinterpret_cast<char*>(ret.data()));
65+
return ret;
6266
}
6367

6468
[[nodiscard]] inline std::vector<bool> ConsumeRandomLengthBitVector(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
@@ -209,14 +213,13 @@ inline void SetFuzzedErrNo(FuzzedDataProvider& fuzzed_data_provider) noexcept
209213
* Returns a byte vector of specified size regardless of the number of remaining bytes available
210214
* from the fuzzer. Pads with zero value bytes if needed to achieve the specified size.
211215
*/
212-
[[nodiscard]] inline std::vector<uint8_t> ConsumeFixedLengthByteVector(FuzzedDataProvider& fuzzed_data_provider, const size_t length) noexcept
216+
template<typename B = uint8_t>
217+
[[nodiscard]] inline std::vector<B> ConsumeFixedLengthByteVector(FuzzedDataProvider& fuzzed_data_provider, const size_t length) noexcept
213218
{
214-
std::vector<uint8_t> result(length);
215-
const std::vector<uint8_t> random_bytes = fuzzed_data_provider.ConsumeBytes<uint8_t>(length);
216-
if (!random_bytes.empty()) {
217-
std::memcpy(result.data(), random_bytes.data(), random_bytes.size());
218-
}
219-
return result;
219+
static_assert(sizeof(B) == 1);
220+
auto random_bytes = fuzzed_data_provider.ConsumeBytes<B>(length);
221+
random_bytes.resize(length);
222+
return random_bytes;
220223
}
221224

222225
class FuzzedFileProvider

0 commit comments

Comments
 (0)