Botan 3.14.1
Crypto and TLS for C&
hash_engine_sha2_32.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_32.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_32.h>
13
14#if defined(BOTAN_HAS_HASH_ENGINE_SHA2_32_AVX2) || defined(BOTAN_HAS_HASH_ENGINE_SHA2_32_AVX512)
15 #define BOTAN_HASH_ENGINE_SHA2_32_HAS_IMPL
16 #include <botan/internal/cpuid.h>
17#endif
18
19namespace Botan {
20
21std::unique_ptr<Hash_Engine> create_sha2_32_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_32_HAS_IMPL)
25 size_t output_length = 0;
26
27 const AlgorithmSpec req(hash_fn);
28 if(req.matches("SHA-256")) {
29 output_length = 32;
30 } else if(auto m = req.match("Truncated(SHA-256,{bits:int})")) {
31 const size_t bits = m->integer("bits");
32 if(bits == 0 || bits % 8 != 0 || bits > 256) {
33 return nullptr;
34 }
35 output_length = bits / 8;
36 } else {
37 return nullptr;
38 }
39
40 SHA_256::digest_type midstate;
41 SHA_256::init(midstate);
42 const size_t full_bytes = common_prefix.size() - common_prefix.size() % SHA_256::block_bytes;
43 if(full_bytes > 0) {
44 SHA_256::compress_digest(midstate, common_prefix.first(full_bytes), full_bytes / SHA_256::block_bytes);
45 }
46
47 const auto make_engine = [&](std::string_view feat,
48 size_t lanes,
51 return std::make_unique<MDx_MB_Engine<uint32_t>>(hash_fn,
52 output_length,
53 common_prefix,
54 feat,
55 lanes,
58 midstate,
59 compress,
60 extract);
61 };
62
63 #if defined(BOTAN_HAS_HASH_ENGINE_SHA2_32_AVX512)
64 if(auto feat = CPUID::check(CPUID::Feature::AVX512)) {
65 if(provider.empty() || provider == *feat) {
66 return make_engine(*feat, 16, sha2_32_mb_compress_x16, sha2_32_mb_extract_x16);
67 }
68 }
69 #endif
70
71 #if defined(BOTAN_HAS_HASH_ENGINE_SHA2_32_AVX2)
72 if(auto feat = CPUID::check(CPUID::Feature::AVX2)) {
73 if(provider.empty() || provider == *feat) {
74 return make_engine(*feat, 8, sha2_32_mb_compress_x8, sha2_32_mb_extract_x8);
75 }
76 }
77 #endif
78
79 return nullptr;
80#else
81 BOTAN_UNUSED(hash_fn, common_prefix, provider);
82 return nullptr;
83#endif
84}
85
86} // 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
secure_vector< uint32_t > digest_type
Definition sha2_32.h:61
static constexpr size_t ctr_bytes
Definition sha2_32.h:67
static void compress_digest(digest_type &digest, std::span< const uint8_t > input, size_t blocks)
Definition sha2_32.cpp:57
static void init(digest_type &digest)
Definition sha2_32.cpp:219
static constexpr size_t block_bytes
Definition sha2_32.h:65
std::unique_ptr< Hash_Engine > create_sha2_32_mb_engine(std::string_view hash_fn, std::span< const uint8_t > common_prefix, std::string_view provider)
BOTAN_FN_ISA_AVX512 void sha2_32_mb_extract_x16(const uint8_t *states, uint8_t *digests)
BOTAN_FN_ISA_AVX512 void sha2_32_mb_compress_x16(uint8_t *states, const uint8_t *const *blocks, size_t nblocks)
BOTAN_FN_ISA_AVX2 void sha2_32_mb_extract_x8(const uint8_t *states, uint8_t *digests)
BOTAN_FN_ISA_AVX2 void sha2_32_mb_compress_x8(uint8_t *states, const uint8_t *const *blocks, size_t nblocks)