Botan 3.5.0
Crypto and TLS for C&
argon2_avx2.cpp
Go to the documentation of this file.
1/**
2* (C) 2023 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/argon2.h>
8#include <immintrin.h>
9
10namespace Botan {
11
12namespace {
13
14class SIMD_4x64 final {
15 public:
16 SIMD_4x64& operator=(const SIMD_4x64& other) = default;
17 SIMD_4x64(const SIMD_4x64& other) = default;
18
19 SIMD_4x64& operator=(SIMD_4x64&& other) = default;
20 SIMD_4x64(SIMD_4x64&& other) = default;
21
22 ~SIMD_4x64() = default;
23
24 BOTAN_FUNC_ISA("avx2")
25 SIMD_4x64() // zero initialized
26 {
27 m_simd = _mm256_setzero_si256();
28 }
29
30 // Load two halves at different addresses
31 static BOTAN_FUNC_ISA("avx2") SIMD_4x64 load_le2(const void* inl, const void* inh) {
32 return SIMD_4x64(
33 _mm256_loadu2_m128i(reinterpret_cast<const __m128i*>(inl), reinterpret_cast<const __m128i*>(inh)));
34 }
35
36 static BOTAN_FUNC_ISA("avx2") SIMD_4x64 load_le(const void* in) {
37 return SIMD_4x64(_mm256_loadu_si256(reinterpret_cast<const __m256i*>(in)));
38 }
39
40 void store_le(uint64_t out[4]) const { this->store_le(reinterpret_cast<uint8_t*>(out)); }
41
42 BOTAN_FUNC_ISA("avx2") void store_le(uint8_t out[]) const {
43 _mm256_storeu_si256(reinterpret_cast<__m256i*>(out), m_simd);
44 }
45
46 BOTAN_FUNC_ISA("avx2") void store_le2(void* outh, void* outl) {
47 _mm256_storeu2_m128i(reinterpret_cast<__m128i*>(outh), reinterpret_cast<__m128i*>(outl), m_simd);
48 }
49
50 SIMD_4x64 operator+(const SIMD_4x64& other) const {
51 SIMD_4x64 retval(*this);
52 retval += other;
53 return retval;
54 }
55
56 SIMD_4x64 operator^(const SIMD_4x64& other) const {
57 SIMD_4x64 retval(*this);
58 retval ^= other;
59 return retval;
60 }
61
62 BOTAN_FUNC_ISA("avx2") void operator+=(const SIMD_4x64& other) {
63 m_simd = _mm256_add_epi64(m_simd, other.m_simd);
64 }
65
66 BOTAN_FUNC_ISA("avx2") void operator^=(const SIMD_4x64& other) {
67 m_simd = _mm256_xor_si256(m_simd, other.m_simd);
68 }
69
70 template <size_t ROT>
71 BOTAN_FUNC_ISA("avx2")
72 SIMD_4x64 rotr() const
73 requires(ROT > 0 && ROT < 64)
74 {
75 if constexpr(ROT == 16) {
76 auto shuf_rot_16 =
77 _mm256_set_epi64x(0x09080f0e0d0c0b0a, 0x0100070605040302, 0x09080f0e0d0c0b0a, 0x0100070605040302);
78
79 return SIMD_4x64(_mm256_shuffle_epi8(m_simd, shuf_rot_16));
80 } else if constexpr(ROT == 24) {
81 auto shuf_rot_24 =
82 _mm256_set_epi64x(0x0a09080f0e0d0c0b, 0x0201000706050403, 0x0a09080f0e0d0c0b, 0x0201000706050403);
83
84 return SIMD_4x64(_mm256_shuffle_epi8(m_simd, shuf_rot_24));
85 } else if constexpr(ROT == 32) {
86 auto shuf_rot_32 =
87 _mm256_set_epi64x(0x0b0a09080f0e0d0c, 0x0302010007060504, 0x0b0a09080f0e0d0c, 0x0302010007060504);
88
89 return SIMD_4x64(_mm256_shuffle_epi8(m_simd, shuf_rot_32));
90 } else {
91 return SIMD_4x64(_mm256_or_si256(_mm256_srli_epi64(m_simd, static_cast<int>(ROT)),
92 _mm256_slli_epi64(m_simd, static_cast<int>(64 - ROT))));
93 }
94 }
95
96 template <size_t ROT>
97 SIMD_4x64 rotl() const {
98 return this->rotr<64 - ROT>();
99 }
100
101 // Argon2 specific operation
102 static BOTAN_FUNC_ISA("avx2") SIMD_4x64 mul2_32(SIMD_4x64 x, SIMD_4x64 y) {
103 const __m256i m = _mm256_mul_epu32(x.m_simd, y.m_simd);
104 return SIMD_4x64(_mm256_add_epi64(m, m));
105 }
106
107 template <uint8_t CTRL>
108 static BOTAN_FUNC_ISA("avx2") SIMD_4x64 permute_4x64(SIMD_4x64 x) {
109 return SIMD_4x64(_mm256_permute4x64_epi64(x.m_simd, CTRL));
110 }
111
112 // Argon2 specific
113 static void twist(SIMD_4x64& B, SIMD_4x64& C, SIMD_4x64& D) {
114 B = SIMD_4x64::permute_4x64<0b00'11'10'01>(B);
115 C = SIMD_4x64::permute_4x64<0b01'00'11'10>(C);
116 D = SIMD_4x64::permute_4x64<0b10'01'00'11>(D);
117 }
118
119 // Argon2 specific
120 static void untwist(SIMD_4x64& B, SIMD_4x64& C, SIMD_4x64& D) {
121 B = SIMD_4x64::permute_4x64<0b10'01'00'11>(B);
122 C = SIMD_4x64::permute_4x64<0b01'00'11'10>(C);
123 D = SIMD_4x64::permute_4x64<0b00'11'10'01>(D);
124 }
125
126 explicit BOTAN_FUNC_ISA("avx2") SIMD_4x64(__m256i x) : m_simd(x) {}
127
128 private:
129 __m256i m_simd;
130};
131
132BOTAN_FORCE_INLINE void blamka_G(SIMD_4x64& A, SIMD_4x64& B, SIMD_4x64& C, SIMD_4x64& D) {
133 A += B + SIMD_4x64::mul2_32(A, B);
134 D ^= A;
135 D = D.rotr<32>();
136
137 C += D + SIMD_4x64::mul2_32(C, D);
138 B ^= C;
139 B = B.rotr<24>();
140
141 A += B + SIMD_4x64::mul2_32(A, B);
142 D ^= A;
143 D = D.rotr<16>();
144
145 C += D + SIMD_4x64::mul2_32(C, D);
146 B ^= C;
147 B = B.rotr<63>();
148}
149
150BOTAN_FORCE_INLINE void blamka_R(SIMD_4x64& A, SIMD_4x64& B, SIMD_4x64& C, SIMD_4x64& D) {
151 blamka_G(A, B, C, D);
152
153 SIMD_4x64::twist(B, C, D);
154 blamka_G(A, B, C, D);
155 SIMD_4x64::untwist(B, C, D);
156}
157
158} // namespace
159
160BOTAN_FUNC_ISA("avx2") void Argon2::blamka_avx2(uint64_t N[128], uint64_t T[128]) {
161 for(size_t i = 0; i != 8; ++i) {
162 SIMD_4x64 A = SIMD_4x64::load_le(&N[16 * i + 4 * 0]);
163 SIMD_4x64 B = SIMD_4x64::load_le(&N[16 * i + 4 * 1]);
164 SIMD_4x64 C = SIMD_4x64::load_le(&N[16 * i + 4 * 2]);
165 SIMD_4x64 D = SIMD_4x64::load_le(&N[16 * i + 4 * 3]);
166
167 blamka_R(A, B, C, D);
168
169 A.store_le(&T[16 * i + 4 * 0]);
170 B.store_le(&T[16 * i + 4 * 1]);
171 C.store_le(&T[16 * i + 4 * 2]);
172 D.store_le(&T[16 * i + 4 * 3]);
173 }
174
175 for(size_t i = 0; i != 8; ++i) {
176 SIMD_4x64 A = SIMD_4x64::load_le2(&T[2 * i + 32 * 0], &T[2 * i + 32 * 0 + 16]);
177 SIMD_4x64 B = SIMD_4x64::load_le2(&T[2 * i + 32 * 1], &T[2 * i + 32 * 1 + 16]);
178 SIMD_4x64 C = SIMD_4x64::load_le2(&T[2 * i + 32 * 2], &T[2 * i + 32 * 2 + 16]);
179 SIMD_4x64 D = SIMD_4x64::load_le2(&T[2 * i + 32 * 3], &T[2 * i + 32 * 3 + 16]);
180
181 blamka_R(A, B, C, D);
182
183 A.store_le2(&T[2 * i + 32 * 0], &T[2 * i + 32 * 0 + 16]);
184 B.store_le2(&T[2 * i + 32 * 1], &T[2 * i + 32 * 1 + 16]);
185 C.store_le2(&T[2 * i + 32 * 2], &T[2 * i + 32 * 2 + 16]);
186 D.store_le2(&T[2 * i + 32 * 3], &T[2 * i + 32 * 3 + 16]);
187 }
188
189 for(size_t i = 0; i != 128 / 8; ++i) {
190 SIMD_4x64 n0 = SIMD_4x64::load_le(&N[8 * i]);
191 SIMD_4x64 n1 = SIMD_4x64::load_le(&N[8 * i + 4]);
192 SIMD_4x64 t0 = SIMD_4x64::load_le(&T[8 * i]);
193 SIMD_4x64 t1 = SIMD_4x64::load_le(&T[8 * i + 4]);
194
195 n0 ^= t0;
196 n1 ^= t1;
197 n0.store_le(&N[8 * i]);
198 n1.store_le(&N[8 * i + 4]);
199 }
200}
201
202} // namespace Botan
static const void * inh
void * outl
int(* final)(unsigned char *, CTX *)
#define BOTAN_FUNC_ISA(isa)
Definition compiler.h:92
#define BOTAN_FORCE_INLINE
Definition compiler.h:165
FE_25519 T
Definition ge.cpp:34
constexpr T rotl(T input)
Definition rotate.h:21
OctetString operator^(const OctetString &k1, const OctetString &k2)
Definition symkey.cpp:109
OctetString operator+(const OctetString &k1, const OctetString &k2)
Definition symkey.cpp:99
constexpr T rotr(T input)
Definition rotate.h:33
constexpr auto store_le(ParamTs &&... params)
Definition loadstor.h:698
constexpr auto load_le(ParamTs &&... params)
Definition loadstor.h:458