Botan 3.14.1
Crypto and TLS for C&
hash_engine_sha2_64_avx2.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/internal/isa_extn.h>
10#include <botan/internal/sha2_64_f.h>
11#include <botan/internal/simd_4x64.h>
12
13namespace Botan {
14
15namespace {
16
17BOTAN_FN_ISA_SIMD_4X64 BOTAN_FORCE_INLINE void rnd(const SIMD_4x64& A,
18 const SIMD_4x64& B,
19 const SIMD_4x64& C,
20 SIMD_4x64& D,
21 const SIMD_4x64& E,
22 const SIMD_4x64& F,
23 const SIMD_4x64& G,
24 SIMD_4x64& H,
25 const SIMD_4x64& W,
26 uint64_t K) {
27 const auto T1 = H + E.sigma1() + SIMD_4x64::choose(E, F, G) + SIMD_4x64::splat(K) + W;
28 const auto T2 = A.sigma0() + SIMD_4x64::majority(A, B, C);
29 D += T1;
30 H = T1 + T2;
31}
32
33BOTAN_FN_ISA_SIMD_4X64 BOTAN_FORCE_INLINE SIMD_4x64 next_w(SIMD_4x64 W[16], size_t i) {
34 const auto& w15 = W[(i + 1) % 16];
35 const auto& w2 = W[(i + 14) % 16];
36 const auto s0 = w15.rotr<1>() ^ w15.rotr<8>() ^ w15.shr<7>();
37 const auto s1 = w2.rotr<19>() ^ w2.rotr<61>() ^ w2.shr<6>();
38 W[i] += s0 + s1 + W[(i + 9) % 16];
39 return W[i];
40}
41
42} // namespace
43
44BOTAN_FN_ISA_SIMD_4X64 void sha2_64_mb_compress_x4(uint8_t* states, const uint8_t* const* blocks, size_t nblocks) {
45 SIMD_4x64 S[8];
46 for(size_t j = 0; j != 8; ++j) {
47 S[j] = SIMD_4x64::load_le(states + 32 * j);
48 }
49
50 for(size_t n = 0; n != nblocks; ++n) {
51 // Initialized directly from the loads, since default construction
52 // would zero the whole array first
53 SIMD_4x64 W[16] = {SIMD_4x64::load_be<128>(blocks, 0, n, 0),
54 SIMD_4x64::load_be<128>(blocks, 1, n, 0),
55 SIMD_4x64::load_be<128>(blocks, 2, n, 0),
56 SIMD_4x64::load_be<128>(blocks, 3, n, 0),
57 SIMD_4x64::load_be<128>(blocks, 0, n, 32),
58 SIMD_4x64::load_be<128>(blocks, 1, n, 32),
59 SIMD_4x64::load_be<128>(blocks, 2, n, 32),
60 SIMD_4x64::load_be<128>(blocks, 3, n, 32),
61 SIMD_4x64::load_be<128>(blocks, 0, n, 64),
62 SIMD_4x64::load_be<128>(blocks, 1, n, 64),
63 SIMD_4x64::load_be<128>(blocks, 2, n, 64),
64 SIMD_4x64::load_be<128>(blocks, 3, n, 64),
65 SIMD_4x64::load_be<128>(blocks, 0, n, 96),
66 SIMD_4x64::load_be<128>(blocks, 1, n, 96),
67 SIMD_4x64::load_be<128>(blocks, 2, n, 96),
68 SIMD_4x64::load_be<128>(blocks, 3, n, 96)};
69 for(size_t j = 0; j != 4; ++j) {
70 SIMD_4x64::transpose(W[4 * j + 0], W[4 * j + 1], W[4 * j + 2], W[4 * j + 3]);
71 }
72
73 SIMD_4x64 A = S[0];
74 SIMD_4x64 B = S[1];
75 SIMD_4x64 C = S[2];
76 SIMD_4x64 D = S[3];
77 SIMD_4x64 E = S[4];
78 SIMD_4x64 F = S[5];
79 SIMD_4x64 G = S[6];
80 SIMD_4x64 H = S[7];
81
82 for(size_t t = 0; t != 16; t += 8) {
83 rnd(A, B, C, D, E, F, G, H, W[t + 0], SHA512_K[t + 0]);
84 rnd(H, A, B, C, D, E, F, G, W[t + 1], SHA512_K[t + 1]);
85 rnd(G, H, A, B, C, D, E, F, W[t + 2], SHA512_K[t + 2]);
86 rnd(F, G, H, A, B, C, D, E, W[t + 3], SHA512_K[t + 3]);
87 rnd(E, F, G, H, A, B, C, D, W[t + 4], SHA512_K[t + 4]);
88 rnd(D, E, F, G, H, A, B, C, W[t + 5], SHA512_K[t + 5]);
89 rnd(C, D, E, F, G, H, A, B, W[t + 6], SHA512_K[t + 6]);
90 rnd(B, C, D, E, F, G, H, A, W[t + 7], SHA512_K[t + 7]);
91 }
92
93 for(size_t t = 16; t != 80; t += 8) {
94 rnd(A, B, C, D, E, F, G, H, next_w(W, (t + 0) % 16), SHA512_K[t + 0]);
95 rnd(H, A, B, C, D, E, F, G, next_w(W, (t + 1) % 16), SHA512_K[t + 1]);
96 rnd(G, H, A, B, C, D, E, F, next_w(W, (t + 2) % 16), SHA512_K[t + 2]);
97 rnd(F, G, H, A, B, C, D, E, next_w(W, (t + 3) % 16), SHA512_K[t + 3]);
98 rnd(E, F, G, H, A, B, C, D, next_w(W, (t + 4) % 16), SHA512_K[t + 4]);
99 rnd(D, E, F, G, H, A, B, C, next_w(W, (t + 5) % 16), SHA512_K[t + 5]);
100 rnd(C, D, E, F, G, H, A, B, next_w(W, (t + 6) % 16), SHA512_K[t + 6]);
101 rnd(B, C, D, E, F, G, H, A, next_w(W, (t + 7) % 16), SHA512_K[t + 7]);
102 }
103
104 S[0] += A;
105 S[1] += B;
106 S[2] += C;
107 S[3] += D;
108 S[4] += E;
109 S[5] += F;
110 S[6] += G;
111 S[7] += H;
112 }
113
114 for(size_t j = 0; j != 8; ++j) {
115 S[j].store_le(states + 32 * j);
116 }
117}
118
119BOTAN_FN_ISA_SIMD_4X64 void sha2_64_mb_extract_x4(const uint8_t* states, uint8_t* digests) {
120 SIMD_4x64 S[8];
121 for(size_t j = 0; j != 8; ++j) {
122 S[j] = SIMD_4x64::load_le(states + 32 * j);
123 }
124
125 // Each S[j] holds word j of all 4 lanes. After the transposes S[k]
126 // holds words 0..3 of lane k and S[4+k] its words 4..7
127 SIMD_4x64::transpose(S[0], S[1], S[2], S[3]);
128 SIMD_4x64::transpose(S[4], S[5], S[6], S[7]);
129
130 for(size_t l = 0; l != 4; ++l) {
131 S[l].store_be(digests + 64 * l);
132 S[4 + l].store_be(digests + 64 * l + 32);
133 }
134}
135
136} // namespace Botan
void BOTAN_FN_ISA_SIMD_4X64 store_le(uint64_t out[4]) const
Definition simd_4x64.h:72
static BOTAN_FN_ISA_SIMD_4X64 SIMD_4x64 majority(const SIMD_4x64 &x, const SIMD_4x64 &y, const SIMD_4x64 &z)
Definition simd_4x64.h:202
BOTAN_FN_ISA_SIMD_4X64 void store_be(uint8_t out[]) const
Definition simd_4x64.h:82
SIMD_4x64 BOTAN_FN_ISA_SIMD_4X64 sigma0() const
Definition simd_4x64.h:166
static BOTAN_FN_ISA_SIMD_4X64 SIMD_4x64 choose(const SIMD_4x64 &mask, const SIMD_4x64 &a, const SIMD_4x64 &b)
Definition simd_4x64.h:193
static BOTAN_FN_ISA_SIMD_4X64 SIMD_4x64 load_be(const void *in)
Definition simd_4x64.h:49
static BOTAN_FN_ISA_SIMD_4X64 void transpose(SIMD_4x64 &B0, SIMD_4x64 &B1, SIMD_4x64 &B2, SIMD_4x64 &B3)
Definition simd_4x64.h:211
static BOTAN_FN_ISA_SIMD_4X64 SIMD_4x64 load_le(const void *in)
Definition simd_4x64.h:45
SIMD_4x64 BOTAN_FN_ISA_SIMD_4X64 shr() const noexcept
Definition simd_4x64.h:224
static BOTAN_FN_ISA_SIMD_4X64 SIMD_4x64 splat(uint64_t v)
Definition simd_4x64.h:263
BOTAN_FN_ISA_SIMD_4X64 SIMD_4x64 rotr() const
Definition simd_4x64.h:128
#define BOTAN_FORCE_INLINE
Definition compiler.h:86
constexpr uint64_t SHA512_K[80]
Definition sha2_64_f.h:19
BOTAN_FN_ISA_SIMD_4X64 void sha2_64_mb_extract_x4(const uint8_t *states, uint8_t *digests)
BOTAN_FN_ISA_SIMD_4X64 void sha2_64_mb_compress_x4(uint8_t *states, const uint8_t *const *blocks, size_t nblocks)