Botan 3.14.1
Crypto and TLS for C&
whirlpool_simd8x32.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/whirlpool.h>
8
9#include <botan/internal/isa_extn.h>
10#include <botan/internal/simd_8x32.h>
11#include <botan/internal/whirlpool_consts.h>
12
13namespace Botan {
14
16
17namespace {
18
19class WhirlpoolState final {
20 public:
21 BOTAN_FN_ISA_SIMD_8X32
22 WhirlpoolState() = default;
23
24 BOTAN_FN_ISA_SIMD_8X32
25 WhirlpoolState(SIMD_8x32 lo, SIMD_8x32 hi) : m_lo(lo), m_hi(hi) {}
26
27 WhirlpoolState(const WhirlpoolState& other) = default;
28 WhirlpoolState(WhirlpoolState&& other) = default;
29 WhirlpoolState& operator=(const WhirlpoolState& other) = default;
30 WhirlpoolState& operator=(WhirlpoolState&& other) = default;
31 ~WhirlpoolState() = default;
32
33 BOTAN_FN_ISA_SIMD_8X32
34 static WhirlpoolState load_bytes(const uint8_t src[64]) {
35 return WhirlpoolState(SIMD_8x32::load_le(src), SIMD_8x32::load_le(src + 32));
36 }
37
38 BOTAN_FN_ISA_SIMD_8X32
39 static WhirlpoolState load_be(const uint64_t src[8]) {
40 return WhirlpoolState(SIMD_8x32::load_le(reinterpret_cast<const uint8_t*>(src)),
41 SIMD_8x32::load_le(reinterpret_cast<const uint8_t*>(src + 4)))
42 .bswap();
43 }
44
45 BOTAN_FN_ISA_SIMD_8X32
46 void store_be(uint64_t dst[8]) const {
47 const auto s = bswap();
48 s.m_lo.store_le(reinterpret_cast<uint8_t*>(dst));
49 s.m_hi.store_le(reinterpret_cast<uint8_t*>(dst + 4));
50 }
51
52 BOTAN_FN_ISA_SIMD_8X32
53 inline friend WhirlpoolState operator^(WhirlpoolState a, WhirlpoolState b) {
54 return WhirlpoolState(a.m_lo ^ b.m_lo, a.m_hi ^ b.m_hi);
55 }
56
57 BOTAN_FN_ISA_SIMD_8X32
58 inline friend WhirlpoolState operator^(WhirlpoolState a, uint64_t rc) {
59 const SIMD_8x32 rc_v(static_cast<uint32_t>(rc), static_cast<uint32_t>(rc >> 32), 0, 0, 0, 0, 0, 0);
60 return WhirlpoolState(a.m_lo ^ rc_v, a.m_hi);
61 }
62
63 BOTAN_FN_ISA_SIMD_8X32
64 inline WhirlpoolState& operator^=(WhirlpoolState other) {
65 m_lo ^= other.m_lo;
66 m_hi ^= other.m_hi;
67 return *this;
68 }
69
70 BOTAN_FN_ISA_SIMD_8X32
71 inline WhirlpoolState sub_bytes() const { return WhirlpoolState(sub_bytes(m_lo), sub_bytes(m_hi)); }
72
73 BOTAN_FN_ISA_SIMD_8X32
74 inline WhirlpoolState shift_columns() const {
75 /*
76 * This is a lot more complicated than the AVX-512 version since first we have
77 * the state split between two registers and also the byte shuffles only work
78 * within 128 bit halves
79 */
80
81 // Byte indices with the top bit set are zeroed by the masked shuffle
82 constexpr uint32_t non = 0xFF;
83
84 constexpr auto idx = [](uint32_t b0, uint32_t b1, uint32_t b2, uint32_t b3) {
85 return (b3 << 24) | (b2 << 16) | (b1 << 8) | b0;
86 };
87
88 // Which bytes are taken from a source depends on both which register and which half it is
89 const SIMD_8x32 idx_same_half(
90 idx(0x0, non, non, non), idx(non, non, non, 0xF), idx(0x8, 0x1, non, non), idx(non, non, non, non));
91 const SIMD_8x32 idx_other_reg(
92 idx(non, non, non, 0xB), idx(0x4, non, non, non), idx(non, non, non, non), idx(0xC, 0x5, non, non));
93 const SIMD_8x32 idx_other_half(idx(non, non, non, non),
94 idx(non, 0xD, 0x6, non),
95 idx(non, non, non, non),
96 idx(non, non, 0xE, 0x7),
97 idx(non, 0x9, 0x2, non),
98 idx(non, non, non, non),
99 idx(non, non, 0xA, 0x3),
100 idx(non, non, non, non));
101 const SIMD_8x32 idx_other_both(idx(non, 0x9, 0x2, non),
102 idx(non, non, non, non),
103 idx(non, non, 0xA, 0x3),
104 idx(non, non, non, non),
105 idx(non, non, non, non),
106 idx(non, 0xD, 0x6, non),
107 idx(non, non, non, non),
108 idx(non, non, 0xE, 0x7));
109
110 // Swap the two halves within the registers so we can get at the values we need via in-half shuffles
111 const auto r_lo = m_lo.swap_halves();
112 const auto r_hi = m_hi.swap_halves();
113
114 /*
115 * Compute the shift column output by shuffling all 4 input halves (lo[0], lo[1], hi[0], hi[1])
116 * to select out the values we want from each source half, placing them in the
117 * index we want, and OR each into the result.
118 */
119 SIMD_8x32 new_lo = SIMD_8x32::masked_byte_shuffle(m_lo, idx_same_half);
120 new_lo |= SIMD_8x32::masked_byte_shuffle(r_lo, idx_other_half);
121 new_lo |= SIMD_8x32::masked_byte_shuffle(m_hi, idx_other_reg);
122 new_lo |= SIMD_8x32::masked_byte_shuffle(r_hi, idx_other_both);
123
124 // Same as above just with hi/lo swapped
125 SIMD_8x32 new_hi = SIMD_8x32::masked_byte_shuffle(m_hi, idx_same_half);
126 new_hi |= SIMD_8x32::masked_byte_shuffle(r_hi, idx_other_half);
127 new_hi |= SIMD_8x32::masked_byte_shuffle(m_lo, idx_other_reg);
128 new_hi |= SIMD_8x32::masked_byte_shuffle(r_lo, idx_other_both);
129
130 return WhirlpoolState(new_lo, new_hi);
131 }
132
133 BOTAN_FN_ISA_SIMD_8X32
134 BOTAN_FORCE_INLINE WhirlpoolState mix_rows() const { return WhirlpoolState(mix_rows(m_lo), mix_rows(m_hi)); }
135
136 BOTAN_FN_ISA_SIMD_8X32
137 BOTAN_FORCE_INLINE WhirlpoolState round() const { return sub_bytes().shift_columns().mix_rows(); }
138
139 private:
140 /*
141 * The Whirlpool 8-bit Sbox is built out of 4-bit sboxes, which can be
142 * individually computed using byte shuffles.
143 */
144 BOTAN_FORCE_INLINE BOTAN_FN_ISA_SIMD_8X32 static SIMD_8x32 sub_bytes(SIMD_8x32 v) {
145 const SIMD_8x32 Ebox(0x0C090B01, 0x030F060D, 0x0407080E, 0x0005020A);
146 const SIMD_8x32 Eibox(0x070D000F, 0x0A050E0B, 0x010C0209, 0x06080403);
147 const SIMD_8x32 Rbox(0x0D0B0C07, 0x0F09040E, 0x0A080306, 0x00010502);
148
149 const auto lo_mask = SIMD_8x32::splat(0x0F0F0F0F);
150
151 const auto lo_nib = v & lo_mask;
152 const auto hi_nib = v.shr<4>() & lo_mask;
153
154 // L = Ebox[hi], R = Eibox[lo], T = Rbox[L ^ R]
155 const auto L = SIMD_8x32::byte_shuffle(Ebox, hi_nib);
156 const auto R = SIMD_8x32::byte_shuffle(Eibox, lo_nib);
157 const auto T = SIMD_8x32::byte_shuffle(Rbox, L ^ R);
158
159 // result = (Ebox[L ^ T] << 4) | Eibox[R ^ T]
160 const auto out_hi = SIMD_8x32::byte_shuffle(Ebox, L ^ T);
161 const auto out_lo = SIMD_8x32::byte_shuffle(Eibox, R ^ T);
162
163 return out_hi.shl<4>() | out_lo;
164 }
165
166 BOTAN_FORCE_INLINE BOTAN_FN_ISA_SIMD_8X32 static SIMD_8x32 mix_rows(SIMD_8x32 v) {
167 // Shuffles for 64-bit rotations by multiples of 8 bits
168 const SIMD_8x32 rot1(0x02010007, 0x06050403, 0x0A09080F, 0x0E0D0C0B);
169 const SIMD_8x32 rot2(0x01000706, 0x05040302, 0x09080F0E, 0x0D0C0B0A);
170 const SIMD_8x32 rot3(0x00070605, 0x04030201, 0x080F0E0D, 0x0C0B0A09);
171 const SIMD_8x32 rot4(0x07060504, 0x03020100, 0x0F0E0D0C, 0x0B0A0908);
172 const SIMD_8x32 rot5(0x06050403, 0x02010007, 0x0E0D0C0B, 0x0A09080F);
173 const SIMD_8x32 rot6(0x05040302, 0x01000706, 0x0D0C0B0A, 0x09080F0E);
174 const SIMD_8x32 rot7(0x04030201, 0x00070605, 0x0C0B0A09, 0x080F0E0D);
175
176 const auto x2 = v.xtime<0x1D>();
177 const auto x4 = x2.xtime<0x1D>();
178 const auto x8 = x4.xtime<0x1D>();
179 const auto x5 = x4 ^ v;
180 const auto x9 = x8 ^ v;
181
182 const auto t01 = v ^ SIMD_8x32::byte_shuffle(v, rot1);
183 const auto t23 = SIMD_8x32::byte_shuffle(x4, rot2) ^ SIMD_8x32::byte_shuffle(v, rot3);
184 const auto t45 = SIMD_8x32::byte_shuffle(x8, rot4) ^ SIMD_8x32::byte_shuffle(x5, rot5);
185 const auto t67 = SIMD_8x32::byte_shuffle(x2, rot6) ^ SIMD_8x32::byte_shuffle(x9, rot7);
186
187 return (t01 ^ t23) ^ (t45 ^ t67);
188 }
189
190 BOTAN_FN_ISA_SIMD_8X32
191 WhirlpoolState bswap() const {
192 // 64-bit byteswap
193 const SIMD_8x32 tbl(0x04050607, 0x00010203, 0x0C0D0E0F, 0x08090A0B);
194 return WhirlpoolState(SIMD_8x32::byte_shuffle(m_lo, tbl), SIMD_8x32::byte_shuffle(m_hi, tbl));
195 }
196
197 SIMD_8x32 m_lo;
198 SIMD_8x32 m_hi;
199};
200
201} // namespace
202
203} // namespace WhirlpoolSIMD8x32
204
205BOTAN_FN_ISA_SIMD_8X32
206void Whirlpool::compress_n_simd8x32(digest_type& digest, std::span<const uint8_t> input, size_t blocks) {
207 using WhirlpoolSIMD8x32::WhirlpoolState;
208
209 constexpr auto WHIRL_RC = whirlpool_rc<true>();
210
211 auto H = WhirlpoolState::load_be(digest.data());
212
213 for(size_t i = 0; i != blocks; ++i) {
214 const auto M = WhirlpoolState::load_bytes(input.data() + i * 64);
215
216 auto K = H;
217 H ^= M;
218 auto B = H; // B = M ^ K
219
220 for(size_t r = 0; r != 10; ++r) {
221 K = K.round() ^ WHIRL_RC[r];
222 B = B.round() ^ K;
223 }
224
225 H ^= B;
226 }
227
228 H.store_be(digest.data());
229}
230
231} // namespace Botan
BOTAN_FN_ISA_SIMD_8X32 SIMD_8x32 shr() const noexcept
Definition simd_8x32.h:232
static BOTAN_FN_ISA_SIMD_8X32 SIMD_8x32 splat(uint32_t B) noexcept
Definition simd_8x32.h:58
static BOTAN_FN_ISA_SIMD_8X32 SIMD_8x32 load_le(const uint8_t *in) noexcept
Definition simd_8x32.h:61
static SIMD_8x32 BOTAN_FN_ISA_SIMD_8X32 byte_shuffle(const SIMD_8x32 &tbl, const SIMD_8x32 &idx)
Definition simd_8x32.h:351
SIMD_8x32 BOTAN_FN_ISA_SIMD_8X32 xtime() const noexcept
Definition simd_8x32.h:377
static SIMD_8x32 BOTAN_FN_ISA_SIMD_8X32 masked_byte_shuffle(const SIMD_8x32 &tbl, const SIMD_8x32 &idx)
Definition simd_8x32.h:361
#define BOTAN_FORCE_INLINE
Definition compiler.h:86
OctetString operator^(const OctetString &k1, const OctetString &k2)
Definition symkey.cpp:109
std::vector< uint8_t, Alloc > & operator^=(std::vector< uint8_t, Alloc > &out, const std::vector< uint8_t, Alloc2 > &in)
Definition mem_ops.h:532
consteval std::array< uint64_t, 10 > whirlpool_rc() noexcept
constexpr auto store_be(ParamTs &&... params)
Definition loadstor.h:745
constexpr auto load_be(ParamTs &&... params)
Definition loadstor.h:504