Botan 3.14.1
Crypto and TLS for C&
hash_engine_sha2_64.cpp
Go to the documentation of this file.
1/*
2* (C) 2026 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/internal/hash_engine_sha2_64.h>
8
9#include <botan/assert.h>
10#include <botan/internal/algorithm_spec.h>
11#include <botan/internal/hash_engine_mdx.h>
12#include <botan/internal/sha2_64.h>
13
14#if defined(BOTAN_HAS_HASH_ENGINE_SHA2_64_AVX2) || defined(BOTAN_HAS_HASH_ENGINE_SHA2_64_AVX512)
15 #define BOTAN_HASH_ENGINE_SHA2_64_HAS_IMPL
16 #include <botan/internal/cpuid.h>
17#endif
18
19namespace Botan {
20
21std::unique_ptr<Hash_Engine> create_sha2_64_mb_engine(std::string_view hash_fn,
22 std::span<const uint8_t> common_prefix,
23 std::string_view provider) {
24#if defined(BOTAN_HASH_ENGINE_SHA2_64_HAS_IMPL)
25 size_t output_length = 0;
26 SHA_512::digest_type midstate;
27
28 const AlgorithmSpec req(hash_fn);
29 if(req.matches("SHA-512")) {
30 output_length = SHA_512::output_bytes;
31 SHA_512::init(midstate);
32 } else if(req.matches("SHA-384")) {
33 output_length = SHA_384::output_bytes;
34 SHA_384::init(midstate);
35 } else if(req.matches("SHA-512-256")) {
36 output_length = SHA_512_256::output_bytes;
37 SHA_512_256::init(midstate);
38 } else if(auto m = req.match("Truncated(SHA-512,{bits:int})")) {
39 const size_t bits = m->integer("bits");
40 if(bits == 0 || bits % 8 != 0 || bits > 512) {
41 return nullptr;
42 }
43 output_length = bits / 8;
44 SHA_512::init(midstate);
45 } else {
46 return nullptr;
47 }
48
49 const size_t full_bytes = common_prefix.size() - common_prefix.size() % SHA_512::block_bytes;
50 if(full_bytes > 0) {
51 SHA_512::compress_digest(midstate, common_prefix.first(full_bytes), full_bytes / SHA_512::block_bytes);
52 }
53
54 const auto make_engine = [&](std::string_view feat,
55 size_t lanes,
58 return std::make_unique<MDx_MB_Engine<uint64_t>>(hash_fn,
59 output_length,
60 common_prefix,
61 feat,
62 lanes,
65 midstate,
66 compress,
67 extract);
68 };
69
70 #if defined(BOTAN_HAS_HASH_ENGINE_SHA2_64_AVX512)
71 if(auto feat = CPUID::check(CPUID::Feature::AVX512)) {
72 if(provider.empty() || provider == *feat) {
73 return make_engine(*feat, 8, sha2_64_mb_compress_x8, sha2_64_mb_extract_x8);
74 }
75 }
76 #endif
77
78 #if defined(BOTAN_HAS_HASH_ENGINE_SHA2_64_AVX2)
79 if(auto feat = CPUID::check(CPUID::Feature::AVX2)) {
80 if(provider.empty() || provider == *feat) {
81 return make_engine(*feat, 4, sha2_64_mb_compress_x4, sha2_64_mb_extract_x4);
82 }
83 }
84 #endif
85
86 return nullptr;
87#else
88 BOTAN_UNUSED(hash_fn, common_prefix, provider);
89 return nullptr;
90#endif
91}
92
93} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:151
bool matches(const AlgorithmPattern &pattern) const
std::optional< AlgorithmMatch > match(const AlgorithmPattern &pattern) const &
static std::optional< std::string > check(CPUID::Feature feat)
Definition cpuid.h:66
void(*)(const uint8_t *states, uint8_t *digests) extract_fn
void(*)(uint8_t *states, const uint8_t *const *blocks, size_t nblocks) compress_fn
static constexpr size_t output_bytes
Definition sha2_64.h:25
static void init(digest_type &digest)
Definition sha2_64.cpp:228
static constexpr size_t output_bytes
Definition sha2_64.h:124
static void init(digest_type &digest)
Definition sha2_64.cpp:217
static constexpr size_t block_bytes
Definition sha2_64.h:64
static constexpr size_t output_bytes
Definition sha2_64.h:65
static void init(digest_type &digest)
Definition sha2_64.cpp:239
static constexpr size_t ctr_bytes
Definition sha2_64.h:66
secure_vector< uint64_t > digest_type
Definition sha2_64.h:60
static void compress_digest(digest_type &digest, std::span< const uint8_t > input, size_t blocks)
Definition sha2_64.cpp:56
std::unique_ptr< Hash_Engine > create_sha2_64_mb_engine(std::string_view hash_fn, std::span< const uint8_t > common_prefix, std::string_view provider)
BOTAN_FN_ISA_SIMD_4X64 void sha2_64_mb_extract_x4(const uint8_t *states, uint8_t *digests)
BOTAN_FN_ISA_SIMD_8X64 void sha2_64_mb_compress_x8(uint8_t *states, const uint8_t *const *blocks, size_t nblocks)
BOTAN_FN_ISA_SIMD_4X64 void sha2_64_mb_compress_x4(uint8_t *states, const uint8_t *const *blocks, size_t nblocks)
BOTAN_FN_ISA_SIMD_8X64 void sha2_64_mb_extract_x8(const uint8_t *states, uint8_t *digests)