Skip to content

Commit

Permalink
Merge pull request #49 from cppalliance/SHA384
Browse files Browse the repository at this point in the history
Implement SHA384
  • Loading branch information
mborland authored Oct 25, 2024
2 parents 9ea5bb0 + efd8b76 commit 04c459d
Show file tree
Hide file tree
Showing 19 changed files with 2,624 additions and 1 deletion.
16 changes: 16 additions & 0 deletions .drone.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,22 @@ local windows_pipeline(name, image, environment, arch = "amd64") =
["deb http://apt.llvm.org/noble/ llvm-toolchain-noble-19 main"],
),

linux_pipeline(
"Linux 24.04 Clang 19 UBSAN",
"cppalliance/droneubuntu2404:1",
{ TOOLSET: 'clang', COMPILER: 'clang++-19', CXXSTD: '03,11,14,17,20,2b' } + ubsan,
"clang-19",
["deb http://apt.llvm.org/noble/ llvm-toolchain-noble-19 main"],
),

linux_pipeline(
"Linux 24.04 Clang 19 ASAN",
"cppalliance/droneubuntu2404:1",
{ TOOLSET: 'clang', COMPILER: 'clang++-19', CXXSTD: '03,11,14,17,20,2b' } + asan,
"clang-19",
["deb http://apt.llvm.org/noble/ llvm-toolchain-noble-19 main"],
),

macos_pipeline(
"MacOS 12.4 Xcode 13.4.1 UBSAN",
{ TOOLSET: 'clang', COMPILER: 'clang++', CXXSTD: '03,11,14,17,20,2b' } + ubsan,
Expand Down
2 changes: 2 additions & 0 deletions doc/crypt.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ include::crypt/sha1.adoc[]

include::crypt/sha256.adoc[]

include::crypt/sha384.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 @@ -17,6 +17,7 @@ https://www.boost.org/LICENSE_1_0.txt
- <<md5_hasher, `md5_hasher`>>
- <<sha1_hasher, `sha1_hasher`>>
- <<sha256_hasher, `sha256_hasher`>>
- <<sha384_hasher, `sha384_hasher`>>

== Enums

Expand Down
131 changes: 131 additions & 0 deletions doc/crypt/sha384.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
////

[#sha384]
:idprefix: sha384_

= SHA384

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

== Hashing Functions

[source, c++]
----
namespace boost {
namespace crypt {
uisng return_type = boost::crypt::array<uint8_t, 48>;
BOOST_CRYPT_GPU_ENABLED inline auto sha384(const char* str) noexcept -> return_type;
BOOST_CRYPT_GPU_ENABLED inline auto sha384(const char* str, size_t len) noexcept -> return_type;
BOOST_CRYPT_GPU_ENABLED inline auto sha384(const unsigned char* str) noexcept -> return_type;
BOOST_CRYPT_GPU_ENABLED inline auto sha384(const unsigned char* str, size_t len) noexcept -> return_type;
BOOST_CRYPT_GPU_ENABLED inline auto sha384(const char16_t* str) noexcept -> return_type;
BOOST_CRYPT_GPU_ENABLED inline auto sha384(const char16_t* str, size_t len) noexcept -> return_type;
BOOST_CRYPT_GPU_ENABLED inline auto sha384(const char32_t* str) noexcept -> return_type;
BOOST_CRYPT_GPU_ENABLED inline auto sha384(const char32_t* str, size_t len) noexcept -> return_type;
BOOST_CRYPT_GPU_ENABLED inline auto sha384(const wchar_t* str) noexcept -> return_type;
BOOST_CRYPT_GPU_ENABLED inline auto sha384(const wchar_t* str, size_t len) noexcept -> return_type;
inline auto sha384(const std::string& str) noexcept -> return_type;
inline auto sha384(const std::u16string& str) noexcept -> return_type;
inline auto sha384(const std::u32string& str) noexcept -> return_type;
inline auto sha384(const std::wstring& str) noexcept -> return_type;
#ifdef BOOST_CRYPT_HAS_STRING_VIEW
inline auto sha384(std::string_view str) noexcept -> return_type;
inline auto sha384(std::u16string_view str) noexcept -> return_type;
inline auto sha384(std::u32string_view str) noexcept -> return_type;
inline auto sha384(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 sha384 value:

[source, c++]
----
namespace boost {
namespace crypt {
uisng return_type = boost::crypt::array<uint8_t, 16>;
inline auto sha384_file(const char* filepath) noexcept -> return_type;
inline auto sha384_file(const std::string& filepath) noexcept -> return_type;
inline auto sha384_file(std::string_view filepath) noexcept -> return_type;
} // namespace crypt
} // namespace boost
----

== Hashing Object

[#sha384_hasher]
Lastly, there is also the ability to create a sha384 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 sha384_hasher
{
uisng return_type = boost::crypt::array<uint8_t, 48>;
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
----
2 changes: 1 addition & 1 deletion fuzzing/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ local all_fuzzers = [ regex.replace-list

for local fuzzer in $(all_fuzzers)
{
local fuzz_time = 120 ;
local fuzz_time = 60 ;

# Create the output corpus directories
make /tmp/corpus/$(fuzzer) : : common.MkDir ;
Expand Down
46 changes: 46 additions & 0 deletions fuzzing/fuzz_sha384.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/sha384.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::sha384(c_data_str);
boost::crypt::sha384(c_data, size);
boost::crypt::sha384(data, size);

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

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

// Fuzz the hasher object
boost::crypt::sha384_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_sha384/sha384.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;"
Loading

0 comments on commit 04c459d

Please sign in to comment.