|
| 1 | +// Copyright 2025 Matt Borland |
| 2 | +// Distributed under the Boost Software License, Version 1.0. |
| 3 | +// https://www.boost.org/LICENSE_1_0.txt |
| 4 | + |
| 5 | +#include <boost/crypt2/drbg/sha1_drbg.hpp> |
| 6 | +#include <boost/crypt2/drbg/sha512_drbg.hpp> |
| 7 | +#include <boost/crypt2/drbg/sha3_256_drbg.hpp> |
| 8 | +#include <iostream> |
| 9 | +#include <exception> |
| 10 | +#include <string> |
| 11 | +#include <vector> |
| 12 | +#include <cstdint> |
| 13 | +#include <string> |
| 14 | +#include <span> |
| 15 | +#include <string_view> |
| 16 | +#include <vector> |
| 17 | +#include <type_traits> |
| 18 | + |
| 19 | +using namespace boost::crypt; |
| 20 | + |
| 21 | +// Type list to store hasher types |
| 22 | +template<typename... Ts> |
| 23 | +struct type_list {}; |
| 24 | + |
| 25 | +// Helper to iterate over types |
| 26 | +template<typename TypeList, template<typename> class F> |
| 27 | +struct for_each_type; |
| 28 | + |
| 29 | +template<template<typename> class F, typename... Ts> |
| 30 | +struct for_each_type<type_list<Ts...>, F> { |
| 31 | + static void apply(const std::uint8_t* data, std::size_t size) { |
| 32 | + (F<Ts>::apply(data, size), ...); |
| 33 | + } |
| 34 | +}; |
| 35 | + |
| 36 | +// Functor to process each hash type |
| 37 | +template<typename DRBGType> |
| 38 | +struct process_hash { |
| 39 | + static void apply(const std::uint8_t* data, std::size_t size) { |
| 40 | + auto c_data = reinterpret_cast<const char*>(data); |
| 41 | + std::string c_data_str{c_data, size}; |
| 42 | + std::span<const std::uint8_t> c_data_span{data, size}; |
| 43 | + std::string_view c_data_str_view{c_data_str}; |
| 44 | + |
| 45 | + DRBGType drbg_tester; |
| 46 | + drbg_tester.init(c_data_span, c_data_span, c_data_span); |
| 47 | + std::vector<std::byte> return_vector(size); |
| 48 | + [[maybe_unused]] const auto code = drbg_tester.generate(return_vector, size); |
| 49 | + drbg_tester.reseed(c_data_str, c_data_str_view); |
| 50 | + drbg_tester.generate(return_vector, size); |
| 51 | + } |
| 52 | +}; |
| 53 | + |
| 54 | +extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t* data, std::size_t size) { |
| 55 | + if (data == nullptr || size == 0) { |
| 56 | + return 0; |
| 57 | + } |
| 58 | + |
| 59 | + try { |
| 60 | + using hasher_types = type_list< |
| 61 | + sha1_hmac_drbg, |
| 62 | + sha512_hmac_drbg, |
| 63 | + sha3_256_hmac_drbg, |
| 64 | + sha1_hash_drbg, |
| 65 | + sha512_hash_drbg, |
| 66 | + sha3_256_hash_drbg |
| 67 | + >; |
| 68 | + |
| 69 | + for_each_type<hasher_types, process_hash>::apply(data, size); |
| 70 | + } |
| 71 | + catch (...) { |
| 72 | + return 0; // Silent failure for fuzzing |
| 73 | + } |
| 74 | + |
| 75 | + return 0; |
| 76 | +} |
0 commit comments