Botan 3.14.1
Crypto and TLS for C&
sha2_32_x86.cpp
Go to the documentation of this file.
1/*
2* Based on public domain code by Sean Gulley
3*
4* Further changes
5*
6* (C) 2017,2020,2025,2026 Jack Lloyd
7*
8* Botan is released under the Simplified BSD License (see license.txt)
9*/
10
11#include <botan/internal/sha2_32.h>
12
13#include <botan/internal/isa_extn.h>
14#include <botan/internal/sha2_32_f.h>
15#include <botan/internal/simd_4x32.h>
16#include <botan/internal/stack_scrubbing.h>
17#include <immintrin.h>
18
19namespace Botan {
20
21namespace {
22
23// NOLINTBEGIN(portability-simd-intrinsics)
24
25BOTAN_FORCE_INLINE BOTAN_FN_ISA_SHANI void sha256_rnds4(SIMD_4x32& S0,
26 SIMD_4x32& S1,
27 const SIMD_4x32& msg,
28 const SIMD_4x32& k) {
29 const auto mk = msg + k;
30 S1 = SIMD_4x32(_mm_sha256rnds2_epu32(S1.raw(), S0.raw(), mk.raw()));
31 S0 = SIMD_4x32(_mm_sha256rnds2_epu32(S0.raw(), S1.raw(), mk.shift_elems_right<2>().raw()));
32}
33
34BOTAN_FORCE_INLINE BOTAN_FN_ISA_SHANI void sha256_msg_exp(SIMD_4x32& W0, SIMD_4x32& W1, SIMD_4x32& W2, SIMD_4x32& W3) {
35 W2 += SIMD_4x32::alignr4(W1, W0);
36 W0 = SIMD_4x32(_mm_sha256msg1_epu32(W0.raw(), W1.raw()));
37 W2 = SIMD_4x32(_mm_sha256msg2_epu32(W2.raw(), W1.raw()));
38
39 W3 += SIMD_4x32::alignr4(W2, W1);
40 W1 = SIMD_4x32(_mm_sha256msg1_epu32(W1.raw(), W2.raw()));
41 W3 = SIMD_4x32(_mm_sha256msg2_epu32(W3.raw(), W2.raw()));
42}
43
44BOTAN_FORCE_INLINE BOTAN_FN_ISA_SHANI void sha256_permute_state(SIMD_4x32& S0, SIMD_4x32& S1) {
45 S0 = SIMD_4x32(_mm_shuffle_epi32(S0.raw(), 0b10110001)); // CDAB
46 S1 = SIMD_4x32(_mm_shuffle_epi32(S1.raw(), 0b00011011)); // EFGH
47
48 const auto T = SIMD_4x32::alignr8(S0, S1); // ABEF
49 S1 = SIMD_4x32(_mm_blend_epi16(S1.raw(), S0.raw(), 0xF0)); // CDGH
50 S0 = T;
51}
52
53// NOLINTEND(portability-simd-intrinsics)
54
55} // namespace
56
58 std::span<const uint8_t> input_span,
59 size_t blocks) {
60 const uint8_t* input = input_span.data();
61
62 SIMD_4x32 S0 = SIMD_4x32::load_le(&digest[0]); // NOLINT(*container-data-pointer)
63 SIMD_4x32 S1 = SIMD_4x32::load_le(&digest[4]);
64
65 sha256_permute_state(S0, S1);
66
67 while(blocks > 0) {
68 const auto S0_SAVE = S0;
69 const auto S1_SAVE = S1;
70
71 auto W0 = SIMD_4x32::load_be(input);
72 auto W1 = SIMD_4x32::load_be(input + 16);
73 auto W2 = SIMD_4x32::load_be(input + 32);
74 auto W3 = SIMD_4x32::load_be(input + 48);
75
76 sha256_rnds4(S0, S1, W0, SIMD_4x32::load_le(&SHA256_K[0]));
77 sha256_rnds4(S0, S1, W1, SIMD_4x32::load_le(&SHA256_K[4]));
78 sha256_rnds4(S0, S1, W2, SIMD_4x32::load_le(&SHA256_K[8]));
79 sha256_rnds4(S0, S1, W3, SIMD_4x32::load_le(&SHA256_K[12]));
80
81 W0 = SIMD_4x32(_mm_sha256msg1_epu32(W0.raw(), W1.raw()));
82 W1 = SIMD_4x32(_mm_sha256msg1_epu32(W1.raw(), W2.raw()));
83
84 sha256_msg_exp(W2, W3, W0, W1);
85
86 sha256_rnds4(S0, S1, W0, SIMD_4x32::load_le(&SHA256_K[4 * 4]));
87 sha256_rnds4(S0, S1, W1, SIMD_4x32::load_le(&SHA256_K[4 * 5]));
88
89 sha256_msg_exp(W0, W1, W2, W3);
90
91 sha256_rnds4(S0, S1, W2, SIMD_4x32::load_le(&SHA256_K[4 * 6]));
92 sha256_rnds4(S0, S1, W3, SIMD_4x32::load_le(&SHA256_K[4 * 7]));
93
94 sha256_msg_exp(W2, W3, W0, W1);
95
96 sha256_rnds4(S0, S1, W0, SIMD_4x32::load_le(&SHA256_K[4 * 8]));
97 sha256_rnds4(S0, S1, W1, SIMD_4x32::load_le(&SHA256_K[4 * 9]));
98
99 sha256_msg_exp(W0, W1, W2, W3);
100
101 sha256_rnds4(S0, S1, W2, SIMD_4x32::load_le(&SHA256_K[4 * 10]));
102 sha256_rnds4(S0, S1, W3, SIMD_4x32::load_le(&SHA256_K[4 * 11]));
103
104 sha256_msg_exp(W2, W3, W0, W1);
105
106 sha256_rnds4(S0, S1, W0, SIMD_4x32::load_le(&SHA256_K[4 * 12]));
107 sha256_rnds4(S0, S1, W1, SIMD_4x32::load_le(&SHA256_K[4 * 13]));
108
109 sha256_msg_exp(W0, W1, W2, W3);
110
111 sha256_rnds4(S0, S1, W2, SIMD_4x32::load_le(&SHA256_K[4 * 14]));
112 sha256_rnds4(S0, S1, W3, SIMD_4x32::load_le(&SHA256_K[4 * 15]));
113
114 // Add values back to state
115 S0 += S0_SAVE;
116 S1 += S1_SAVE;
117
118 input += 64;
119 blocks--;
120 }
121
122 sha256_permute_state(S1, S0);
123
124 S0.store_le(&digest[0]); // NOLINT(*container-data-pointer)
125 S1.store_le(&digest[4]);
126}
127
128} // namespace Botan
secure_vector< uint32_t > digest_type
Definition sha2_32.h:61
static void compress_digest_x86(digest_type &digest, std::span< const uint8_t > input, size_t blocks)
void BOTAN_FN_ISA_SIMD_4X32 store_le(uint32_t out[4]) const noexcept
Definition simd_4x32.h:210
static SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 load_be(const void *in) noexcept
Definition simd_4x32.h:194
static SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 load_le(const void *in) noexcept
Definition simd_4x32.h:183
static SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 alignr4(const SIMD_4x32 &a, const SIMD_4x32 &b)
Definition simd_4x32.h:865
static SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 alignr8(const SIMD_4x32 &a, const SIMD_4x32 &b)
Definition simd_4x32.h:882
#define BOTAN_FORCE_INLINE
Definition compiler.h:86
constexpr uint32_t SHA256_K[64]
Definition sha2_32_f.h:19
#define BOTAN_SCRUB_STACK_AFTER_RETURN