Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SHA512/256 #53

Merged
merged 12 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/crypt.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ include::crypt/sha512.adoc[]

include::crypt/sha512_224.adoc[]

include::crypt/sha512_256.adoc[]

include::crypt/config.adoc[]

include::crypt/reference.adoc[]
Expand Down
1 change: 1 addition & 0 deletions doc/crypt/api_reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ https://www.boost.org/LICENSE_1_0.txt
- <<sha384_hasher, `sha384_hasher`>>
- <<sha512_hasher, `sha512_hasher`>>
- <<sha512_224_hasher, `sha512_224_hasher`>>
- <<sha512_256_hasher, `sha512_256_hasher`>>

== Enums

Expand Down
131 changes: 131 additions & 0 deletions doc/crypt/sha512_256.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
////
Copyright 2024 Matt Borland
Distributed under the Boost Software License, Version 1.0.
https://www.boost.org/LICENSE_1_0.txt
////

[#sha512_256]
:idprefix: sha512_256_

= SHA512_256

This library supports sha512_256 as described in https://datatracker.ietf.org/doc/html/rfc6234[RFC 6234].
There is a wide range of acceptable inputs for the base sha512_256 function:

== Hashing Functions

[source, c++]
----
namespace boost {
namespace crypt {

uisng return_type = boost::crypt::array<uint8_t, 32>;

BOOST_CRYPT_GPU_ENABLED inline auto sha512_256(const char* str) noexcept -> return_type;

BOOST_CRYPT_GPU_ENABLED inline auto sha512_256(const char* str, size_t len) noexcept -> return_type;

BOOST_CRYPT_GPU_ENABLED inline auto sha512_256(const unsigned char* str) noexcept -> return_type;

BOOST_CRYPT_GPU_ENABLED inline auto sha512_256(const unsigned char* str, size_t len) noexcept -> return_type;

BOOST_CRYPT_GPU_ENABLED inline auto sha512_256(const char16_t* str) noexcept -> return_type;

BOOST_CRYPT_GPU_ENABLED inline auto sha512_256(const char16_t* str, size_t len) noexcept -> return_type;

BOOST_CRYPT_GPU_ENABLED inline auto sha512_256(const char32_t* str) noexcept -> return_type;

BOOST_CRYPT_GPU_ENABLED inline auto sha512_256(const char32_t* str, size_t len) noexcept -> return_type;

BOOST_CRYPT_GPU_ENABLED inline auto sha512_256(const wchar_t* str) noexcept -> return_type;

BOOST_CRYPT_GPU_ENABLED inline auto sha512_256(const wchar_t* str, size_t len) noexcept -> return_type;

inline auto sha512_256(const std::string& str) noexcept -> return_type;

inline auto sha512_256(const std::u16string& str) noexcept -> return_type;

inline auto sha512_256(const std::u32string& str) noexcept -> return_type;

inline auto sha512_256(const std::wstring& str) noexcept -> return_type;

#ifdef BOOST_CRYPT_HAS_STRING_VIEW

inline auto sha512_256(std::string_view str) noexcept -> return_type;

inline auto sha512_256(std::u16string_view str) noexcept -> return_type;

inline auto sha512_256(std::u32string_view str) noexcept -> return_type;

inline auto sha512_256(std::wstring_view str) noexcept -> return_type;

#endif // BOOST_CRYPT_HAS_STRING_VIEW

#ifdef BOOST_CRYPT_HAS_SPAN

template <typename T, std::size_t extent>
inline auto md5(std::span<T, extent> data) noexcept -> return_type;

#endif // BOOST_CRYPT_HAS_SPAN

#ifdef BOOST_CRYPT_HAS_CUDA

template <typename T, boost::crypt::size_t extent>
BOOST_CRYPT_GPU_ENABLED inline auto md5(cuda::std::span<T, extent> data) noexcept -> return_type;

#endif // BOOST_CRYPT_HAS_CUDA

} //namespace crypt
} //namespace boost
----

== File Hashing Functions

We also have the ability to scan files and return the sha512_256 value:

[source, c++]
----
namespace boost {
namespace crypt {

uisng return_type = boost::crypt::array<uint8_t, 32>;

inline auto sha512_256_file(const char* filepath) noexcept -> return_type;

inline auto sha512_256_file(const std::string& filepath) noexcept -> return_type;

inline auto sha512_256_file(std::string_view filepath) noexcept -> return_type;

} // namespace crypt
} // namespace boost
----

== Hashing Object

[#sha512_256_hasher]
Lastly, there is also the ability to create a sha512_256 hashing object and feed it bytes as the user parses them.
This class does not use any dynamic memory allocation.

[source, c++]
----
namespace boost {
namespace crypt {

class sha512_256_hasher
{
uisng return_type = boost::crypt::array<uint8_t, 32>;

void init();

template <typename ByteType>
BOOST_CRYPT_GPU_ENABLED inline auto process_byte(ByteType byte) noexcept -> hasher_state;

template <typename ForwardIter>
BOOST_CRYPT_GPU_ENABLED inline auto process_bytes(ForwardIter buffer, size_t byte_count) noexcept -> hasher_state;

inline auto get_digest() noexcept -> return_type;
};

} // namespace crypt
} // namespace boost
----
46 changes: 46 additions & 0 deletions fuzzing/fuzz_sha512_256.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2024 Matt Borland
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/crypt/hash/sha512_256.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_256(c_data_str);
boost::crypt::sha512_256(c_data, size);
boost::crypt::sha512_256(data, size);

#ifdef BOOST_CRYPT_HAS_STRING_VIEW
std::string_view view {c_data_str};
boost::crypt::sha512_256(view);
#endif

#ifdef BOOST_CRYPT_HAS_SPAN
std::span data_span {c_data, size};
boost::crypt::sha512_256(data_span);
#endif

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

return 0;
}
18 changes: 18 additions & 0 deletions fuzzing/seedcorpus/fuzz_sha512_256/sha512_256.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"The quick brown fox jumps over the lazy dog"
"The quick brown fox jumps over the lazy dog."
""
"aB3$x9Yz"
"12345"
"!@#$%^&*()"
"FuzzTest123"
" "
"Lorem ipsum dolor sit amet"
"a"
"9876543210"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"ñÑáéíóúÁÉÍÓÚ"
"\n\r\t"
"0"
"ThisIsAVeryLongStringWithNoSpacesOrPunctuationToTestEdgeCases"
"<?php echo 'test'; ?>"
"SELECT * FROM users;"
2 changes: 1 addition & 1 deletion include/boost/crypt/hash/detail/sha224_256_hasher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace crypt {
namespace hash_detail {

template <boost::crypt::size_t digest_size>
class sha_224_256_hasher : public hasher_base_512<digest_size, 8U>
class sha_224_256_hasher final : public hasher_base_512<digest_size, 8U>
{
private:
static_assert(digest_size == 28U || digest_size == 32U, "Digest size must be 28 (SHA224) or 32 (SHA256)");
Expand Down
2 changes: 1 addition & 1 deletion include/boost/crypt/hash/detail/sha512_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace crypt {
namespace hash_detail {

template <boost::crypt::size_t digest_size>
class sha512_base
class sha512_base final
{
private:

Expand Down
2 changes: 1 addition & 1 deletion include/boost/crypt/hash/sha224.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace boost {
namespace crypt {

BOOST_CRYPT_EXPORT class sha224_hasher final : public hash_detail::sha_224_256_hasher<28U> {};
BOOST_CRYPT_EXPORT using sha224_hasher = hash_detail::sha_224_256_hasher<28U>;

namespace detail {

Expand Down
2 changes: 1 addition & 1 deletion include/boost/crypt/hash/sha256.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace boost {
namespace crypt {

BOOST_CRYPT_EXPORT class sha256_hasher final : public hash_detail::sha_224_256_hasher<32U> {};
BOOST_CRYPT_EXPORT using sha256_hasher = hash_detail::sha_224_256_hasher<32U>;

namespace detail {

Expand Down
8 changes: 4 additions & 4 deletions include/boost/crypt/hash/sha384.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
// See: https://datatracker.ietf.org/doc/html/rfc6234
// See: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf#page=31

#ifndef BOOST_CRYPT_HASH_SHA512_HPP
#define BOOST_CRYPT_HASH_SHA512_HPP
#ifndef BOOST_CRYPT_HASH_SHA384_HPP
#define BOOST_CRYPT_HASH_SHA384_HPP

#include <boost/crypt/hash/detail/sha512_base.hpp>

namespace boost {
namespace crypt {

BOOST_CRYPT_EXPORT class sha384_hasher final : public hash_detail::sha512_base<48U> {};
BOOST_CRYPT_EXPORT using sha384_hasher = hash_detail::sha512_base<48U>;

namespace detail {

Expand Down Expand Up @@ -299,4 +299,4 @@ BOOST_CRYPT_GPU_ENABLED inline auto sha384(cuda::std::span<T, extent> data) noex
} // namespace crypt
} // namespace boost

#endif // BOOST_CRYPT_HASH_SHA512_HPP
#endif // BOOST_CRYPT_HASH_SHA384_HPP
2 changes: 1 addition & 1 deletion include/boost/crypt/hash/sha512.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace boost {
namespace crypt {

BOOST_CRYPT_EXPORT class sha512_hasher final : public hash_detail::sha512_base<64U> {};
BOOST_CRYPT_EXPORT using sha512_hasher = hash_detail::sha512_base<64U>;

namespace detail {

Expand Down
8 changes: 4 additions & 4 deletions include/boost/crypt/hash/sha512_224.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
// See: https://datatracker.ietf.org/doc/html/rfc6234
// See: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf#page=31

#ifndef BOOST_CRYPT_HASH_SHA512_HPP
#define BOOST_CRYPT_HASH_SHA512_HPP
#ifndef BOOST_CRYPT_HASH_SHA512_224_HPP
#define BOOST_CRYPT_HASH_SHA512_224_HPP

#include <boost/crypt/hash/detail/sha512_base.hpp>

namespace boost {
namespace crypt {

BOOST_CRYPT_EXPORT class sha512_224_hasher final : public hash_detail::sha512_base<28U> {};
BOOST_CRYPT_EXPORT using sha512_224_hasher = hash_detail::sha512_base<28U>;

namespace detail {

Expand Down Expand Up @@ -297,4 +297,4 @@ BOOST_CRYPT_GPU_ENABLED inline auto sha512_224(cuda::std::span<T, extent> data)
} // namespace crypt
} // namespace boost

#endif // BOOST_CRYPT_HASH_SHA512_HPP
#endif // BOOST_CRYPT_HASH_SHA512_224_HPP
Loading
Loading