Skip to content

Commit

Permalink
Add fuzzing
Browse files Browse the repository at this point in the history
  • Loading branch information
mborland committed Jan 9, 2025
1 parent f125ec4 commit 8e832ba
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions fuzzing/fuzz_sha512.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2024 Matt Borland
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/crypt2/hash/sha512.hpp>
#include <iostream>
#include <exception>
#include <string>

extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t* data, std::size_t size)
{
try
{
auto c_data = reinterpret_cast<const char*>(data);
std::string c_data_str {c_data, size}; // Guarantee null termination since we can't pass the size argument

boost::crypt::sha512(c_data_str);

std::string_view view {c_data_str};
boost::crypt::sha512(view);

std::span data_span {c_data, size};
boost::crypt::sha512(data_span);

// Fuzz the hasher object
boost::crypt::sha512_hasher hasher;
hasher.process_bytes(data_span);
hasher.process_bytes(data_span);
hasher.process_bytes(data_span);
hasher.finalize();
hasher.get_digest();
hasher.process_bytes(data_span); // State is invalid but should not crash
}
catch(...)
{
std::cerr << "Error with: " << data << std::endl;
std::terminate();
}

return 0;
}

0 comments on commit 8e832ba

Please sign in to comment.