Botan 3.14.1
Crypto and TLS for C&
sha2_32_simd.cpp
Go to the documentation of this file.
1/*
2* (C) 2025 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/internal/sha2_32.h>
8
9#include <botan/internal/sha2_32_f.h>
10#include <botan/internal/simd_4x32.h>
11#include <botan/internal/stack_scrubbing.h>
12
13namespace Botan {
14
15namespace {
16
17BOTAN_FN_ISA_SIMD_4X32 BOTAN_FORCE_INLINE SIMD_4x32 sha256_simd_next_w(SIMD_4x32 x[4]) {
18 const SIMD_4x32 lo_mask = SIMD_4x32(0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000);
19 const SIMD_4x32 hi_mask = SIMD_4x32(0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF);
20
21 const SIMD_4x32 lo_word_shuf = SIMD_4x32(0x03020100, 0x07060504, 0x03020100, 0x07060504);
22 const SIMD_4x32 hi_word_shuf = SIMD_4x32(0x0B0A0908, 0x0F0E0D0C, 0x0B0A0908, 0x0F0E0D0C);
23
24 auto t0 = SIMD_4x32::alignr4(x[1], x[0]);
25 x[0] += SIMD_4x32::alignr4(x[3], x[2]);
26
27 x[0] += t0.rotr<7>() ^ t0.rotr<18>() ^ t0.shr<3>();
28
29 t0 = SIMD_4x32::byte_shuffle(x[3], hi_word_shuf);
30 auto s1 = t0.rotr<17>() ^ t0.rotr<19>() ^ t0.shr<10>();
31 x[0] += s1 & lo_mask;
32
33 t0 = SIMD_4x32::byte_shuffle(x[0], lo_word_shuf);
34 s1 = t0.rotr<17>() ^ t0.rotr<19>() ^ t0.shr<10>();
35 x[0] += s1 & hi_mask;
36
37 const auto tmp = x[0];
38 x[0] = x[1];
39 x[1] = x[2];
40 x[2] = x[3];
41 x[3] = tmp;
42
43 return x[3];
44}
45
46} // namespace
47
48void BOTAN_FN_ISA_SIMD_4X32 BOTAN_SCRUB_STACK_AFTER_RETURN
49SHA_256::compress_digest_x86_simd(digest_type& digest, std::span<const uint8_t> input, size_t blocks) {
50 alignas(64) uint32_t W[16];
51
52 uint32_t A = digest[0];
53 uint32_t B = digest[1];
54 uint32_t C = digest[2];
55 uint32_t D = digest[3];
56 uint32_t E = digest[4];
57 uint32_t F = digest[5];
58 uint32_t G = digest[6];
59 uint32_t H = digest[7];
60
61 const uint8_t* data = input.data();
62
63 while(blocks > 0) {
64 SIMD_4x32 WS[4];
65
66 for(size_t i = 0; i < 4; i++) {
67 WS[i] = SIMD_4x32::load_be(&data[16 * i]);
68 const auto WK = WS[i] + SIMD_4x32::load_le(&SHA256_K[4 * i]);
69 WK.store_le(&W[4 * i]);
70 }
71
72 data += 64;
73 blocks -= 1;
74
75 for(size_t r = 0; r != 48; r += 16) {
76 auto w = sha256_simd_next_w(WS) + SIMD_4x32::load_le(&SHA256_K[r + 16]);
77
78 SHA2_32_F(A, B, C, D, E, F, G, H, W[0]);
79 SHA2_32_F(H, A, B, C, D, E, F, G, W[1]);
80 SHA2_32_F(G, H, A, B, C, D, E, F, W[2]);
81 SHA2_32_F(F, G, H, A, B, C, D, E, W[3]);
82
83 w.store_le(&W[0]);
84
85 w = sha256_simd_next_w(WS) + SIMD_4x32::load_le(&SHA256_K[r + 20]);
86
87 SHA2_32_F(E, F, G, H, A, B, C, D, W[4]);
88 SHA2_32_F(D, E, F, G, H, A, B, C, W[5]);
89 SHA2_32_F(C, D, E, F, G, H, A, B, W[6]);
90 SHA2_32_F(B, C, D, E, F, G, H, A, W[7]);
91
92 w.store_le(&W[4]);
93
94 w = sha256_simd_next_w(WS) + SIMD_4x32::load_le(&SHA256_K[r + 24]);
95
96 SHA2_32_F(A, B, C, D, E, F, G, H, W[8]);
97 SHA2_32_F(H, A, B, C, D, E, F, G, W[9]);
98 SHA2_32_F(G, H, A, B, C, D, E, F, W[10]);
99 SHA2_32_F(F, G, H, A, B, C, D, E, W[11]);
100
101 w.store_le(&W[8]);
102
103 w = sha256_simd_next_w(WS) + SIMD_4x32::load_le(&SHA256_K[r + 28]);
104
105 SHA2_32_F(E, F, G, H, A, B, C, D, W[12]);
106 SHA2_32_F(D, E, F, G, H, A, B, C, W[13]);
107 SHA2_32_F(C, D, E, F, G, H, A, B, W[14]);
108 SHA2_32_F(B, C, D, E, F, G, H, A, W[15]);
109
110 w.store_le(&W[12]);
111 }
112
113 SHA2_32_F(A, B, C, D, E, F, G, H, W[0]);
114 SHA2_32_F(H, A, B, C, D, E, F, G, W[1]);
115 SHA2_32_F(G, H, A, B, C, D, E, F, W[2]);
116 SHA2_32_F(F, G, H, A, B, C, D, E, W[3]);
117 SHA2_32_F(E, F, G, H, A, B, C, D, W[4]);
118 SHA2_32_F(D, E, F, G, H, A, B, C, W[5]);
119 SHA2_32_F(C, D, E, F, G, H, A, B, W[6]);
120 SHA2_32_F(B, C, D, E, F, G, H, A, W[7]);
121 SHA2_32_F(A, B, C, D, E, F, G, H, W[8]);
122 SHA2_32_F(H, A, B, C, D, E, F, G, W[9]);
123 SHA2_32_F(G, H, A, B, C, D, E, F, W[10]);
124 SHA2_32_F(F, G, H, A, B, C, D, E, W[11]);
125 SHA2_32_F(E, F, G, H, A, B, C, D, W[12]);
126 SHA2_32_F(D, E, F, G, H, A, B, C, W[13]);
127 SHA2_32_F(C, D, E, F, G, H, A, B, W[14]);
128 SHA2_32_F(B, C, D, E, F, G, H, A, W[15]);
129
130 A = (digest[0] += A);
131 B = (digest[1] += B);
132 C = (digest[2] += C);
133 D = (digest[3] += D);
134 E = (digest[4] += E);
135 F = (digest[5] += F);
136 G = (digest[6] += G);
137 H = (digest[7] += H);
138 }
139}
140
141} // namespace Botan
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 byte_shuffle(const SIMD_4x32 &tbl, const SIMD_4x32 &idx)
Definition simd_4x32.h:796
static SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 alignr4(const SIMD_4x32 &a, const SIMD_4x32 &b)
Definition simd_4x32.h:865
#define BOTAN_FORCE_INLINE
Definition compiler.h:86
BOTAN_FORCE_INLINE void SHA2_32_F(uint32_t A, uint32_t B, uint32_t C, uint32_t &D, uint32_t E, uint32_t F, uint32_t G, uint32_t &H, uint32_t &M1, uint32_t M2, uint32_t M3, uint32_t M4, uint32_t magic)
Definition sha2_32_f.h:33
constexpr uint32_t SHA256_K[64]
Definition sha2_32_f.h:19
#define BOTAN_SCRUB_STACK_AFTER_RETURN