Skip to content

Commit 9c8f6be

Browse files
authored
Merge pull request #199 from cppalliance/sha3_384
Update SHA3-384
2 parents dbcd8bd + 232b892 commit 9c8f6be

File tree

9 files changed

+192
-209
lines changed

9 files changed

+192
-209
lines changed

include/boost/crypt2/hash/detail/sha3_base.hpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ class sha3_base final {
6767
[[nodiscard("Digest is the function return value")]] BOOST_CRYPT_GPU_ENABLED_CONSTEXPR
6868
auto get_digest() noexcept -> compat::expected<return_type, state>;
6969

70+
template <compat::size_t Extent>
7071
[[nodiscard]] BOOST_CRYPT_GPU_ENABLED_CONSTEXPR
71-
auto get_digest(compat::span<compat::byte> data) noexcept -> state;
72+
auto get_digest(compat::span<compat::byte, Extent> data) noexcept -> state;
7273

7374
template <concepts::writable_output_range Range>
7475
[[nodiscard]] BOOST_CRYPT_GPU_ENABLED auto get_digest(Range&& data) noexcept -> state;
@@ -357,8 +358,9 @@ auto sha3_base<digest_size, is_xof>::get_digest() noexcept -> compat::expected<r
357358
}
358359

359360
template <compat::size_t digest_size, bool is_xof>
361+
template <compat::size_t Extent>
360362
[[nodiscard]] BOOST_CRYPT_GPU_ENABLED_CONSTEXPR auto
361-
sha3_base<digest_size, is_xof>::get_digest(compat::span<compat::byte> data) noexcept -> state
363+
sha3_base<digest_size, is_xof>::get_digest(compat::span<compat::byte, Extent> data) noexcept -> state
362364
{
363365
if (!computed_ || corrupted_)
364366
{
@@ -398,6 +400,11 @@ template <concepts::writable_output_range Range>
398400
{
399401
using value_type = compat::range_value_t<Range>;
400402

403+
if (!computed_ || corrupted_)
404+
{
405+
return state::state_error;
406+
}
407+
401408
auto data_span {compat::span<value_type>(compat::forward<Range>(data))};
402409

403410
#if defined(__clang__) && __clang_major__ >= 19
@@ -407,7 +414,7 @@ template <concepts::writable_output_range Range>
407414

408415
if constexpr (is_xof)
409416
{
410-
return get_digest(compat::span<compat::byte>(compat::as_writable_bytes(data_span).data()));
417+
xof_digest_impl(compat::span<compat::byte>(compat::as_writable_bytes(data_span).data()));
411418
}
412419
else
413420
{
@@ -416,7 +423,7 @@ template <concepts::writable_output_range Range>
416423
return state::insufficient_output_length;
417424
}
418425

419-
return get_digest(
426+
sha_digest_impl(
420427
compat::span<compat::byte, digest_size>(
421428
compat::as_writable_bytes(data_span).data(),
422429
digest_size
@@ -427,6 +434,8 @@ template <concepts::writable_output_range Range>
427434
#if defined(__clang__) && __clang_major__ >= 19
428435
#pragma clang diagnostic pop
429436
#endif
437+
438+
return state::success;
430439
}
431440

432441
} // namespace boost::crypt::hash_detail
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2024 - 2025 Matt Borland
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// https://www.boost.org/LICENSE_1_0.txt
4+
5+
#ifndef BOOST_CRYPT2_HASH_SHA3_384_HPP
6+
#define BOOST_CRYPT2_HASH_SHA3_384_HPP
7+
8+
#include <boost/crypt2/hash/detail/sha3_base.hpp>
9+
#include <boost/crypt2/hash/detail/hash_file.hpp>
10+
#include <boost/crypt2/detail/compat.hpp>
11+
#include <boost/crypt2/detail/concepts.hpp>
12+
13+
namespace boost::crypt {
14+
15+
BOOST_CRYPT_EXPORT using sha3_384_hasher = hash_detail::sha3_base<48U>;
16+
17+
// One shot functions
18+
[[nodiscard]] BOOST_CRYPT_EXPORT BOOST_CRYPT_GPU_ENABLED_CONSTEXPR
19+
auto sha3_384(compat::span<const compat::byte> data) noexcept -> compat::expected<sha3_384_hasher::return_type, state>
20+
{
21+
sha3_384_hasher hasher;
22+
hasher.process_bytes(data);
23+
hasher.finalize();
24+
return hasher.get_digest();
25+
}
26+
27+
template <compat::sized_range SizedRange>
28+
[[nodiscard]] BOOST_CRYPT_EXPORT BOOST_CRYPT_GPU_ENABLED
29+
auto sha3_384(SizedRange&& data) noexcept -> compat::expected<sha3_384_hasher::return_type, state>
30+
{
31+
sha3_384_hasher hasher;
32+
hasher.process_bytes(data);
33+
hasher.finalize();
34+
return hasher.get_digest();
35+
}
36+
37+
#if !BOOST_CRYPT_HAS_CUDA
38+
39+
template <concepts::file_system_path T>
40+
[[nodiscard]] BOOST_CRYPT_EXPORT
41+
auto sha3_384_file(const T& filepath) -> compat::expected<sha3_384_hasher::return_type, state>
42+
{
43+
return hash_detail::hash_file_impl<sha3_384_hasher>(filepath);
44+
}
45+
46+
#endif // BOOST_CRYPT_HAS_CUDA
47+
48+
} // namespace boost::crypt
49+
50+
#endif // BOOST_CRYPT2_HASH_SHA3_384_HPP

include/boost/crypt2/hash/sha3_512.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Distributed under the Boost Software License, Version 1.0.
33
// https://www.boost.org/LICENSE_1_0.txt
44

5-
#ifndef BOOST_CRYPT2_HASH_SHA384_HPP
6-
#define BOOST_CRYPT2_HASH_SHA384_HPP
5+
#ifndef BOOST_CRYPT2_HASH_SHA3_512_HPP
6+
#define BOOST_CRYPT2_HASH_SHA3_512_HPP
77

88
#include <boost/crypt2/hash/detail/sha3_base.hpp>
99
#include <boost/crypt2/hash/detail/hash_file.hpp>
@@ -47,4 +47,4 @@ auto sha3_512_file(const T& filepath) -> compat::expected<sha3_512_hasher::retur
4747

4848
} // namespace boost::crypt
4949

50-
#endif //BOOST_CRYPT2_HASH_SHA384_HPP
50+
#endif //BOOST_CRYPT2_HASH_SHA3_512_HPP

test/Jamfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ run test_sha512.cpp ;
6767
run test_sha512_224.cpp ;
6868
run test_sha512_256.cpp ;
6969
run test_sha3_512.cpp ;
70-
#run test_sha3_384.cpp ;
70+
run test_sha3_384.cpp ;
7171
#run test_sha3_256.cpp ;
7272
#run test_sha3_224.cpp ;
7373
#run test_shake128.cpp ;
@@ -128,8 +128,8 @@ run test_nist_cavs_sha3_512_monte.cpp ;
128128
run test_nist_cavs_sha3_512_short_long.cpp ;
129129
#run test_nist_cavs_sha3_512_hmac.cpp ;
130130

131-
#run test_nist_cavs_sha3_384_monte.cpp ;
132-
#run test_nist_cavs_sha3_384_short_long.cpp ;
131+
run test_nist_cavs_sha3_384_monte.cpp ;
132+
run test_nist_cavs_sha3_384_short_long.cpp ;
133133
#run test_nist_cavs_sha3_384_hmac.cpp ;
134134

135135
#run test_nist_cavs_sha3_256_monte.cpp ;

test/nvcc_jamfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ run test_sha512_nvcc.cu ;
1717
run test_sha512_224_nvcc.cu ;
1818
run test_sha512_256_nvcc.cu ;
1919
run test_sha3_512_nvcc.cu ;
20-
#run test_sha3_384_nvcc.cu ;
20+
run test_sha3_384_nvcc.cu ;
2121
#run test_sha3_256_nvcc.cu ;
2222
#run test_sha3_224_nvcc.cu ;
2323
#run test_shake128_nvcc.cu ;

test/test_nist_cavs_sha3_384_monte.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
// Distributed under the Boost Software License, Version 1.0.
44
// https://www.boost.org/LICENSE_1_0.txt
55

6-
#include <boost/crypt/hash/sha3_384.hpp>
6+
#include <boost/crypt2/hash/sha3_384.hpp>
77

88
#include "test_nist_cavs_detail.hpp"
99

1010
auto main() -> int
1111
{
12-
bool result_is_ok { true };
12+
bool result_is_ok { true };
1313

1414
{
1515
nist::cavs::test_vector_container_type my_test_vectors_monte { };
@@ -27,5 +27,5 @@ auto main() -> int
2727
BOOST_TEST(result_is_ok);
2828
}
2929

30-
return boost::report_errors();
30+
return boost::report_errors();
3131
}

test/test_nist_cavs_sha3_384_short_long.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Distributed under the Boost Software License, Version 1.0.
44
// https://www.boost.org/LICENSE_1_0.txt
55

6-
#include <boost/crypt/hash/sha3_384.hpp>
6+
#include <boost/crypt2/hash/sha3_384.hpp>
77

88
#include "test_nist_cavs_detail.hpp"
99

0 commit comments

Comments
 (0)