Crypto++ 8.1 Free C++ class library of cryptographic schemes

blake2s_simd.cpp
1 // blake2-simd.cpp - written and placed in the public domain by
2 // Samuel Neves, Jeffrey Walton, Uri Blumenthal
3 // and Marcel Raad.
4 //
5 // This source file uses intrinsics to gain access to ARMv7a/ARMv8a
6 // NEON, Power7 and SSE4.1 instructions. A separate source file is
7 // needed because additional CXXFLAGS are required to enable the
8 // appropriate instructions sets in some build configurations.
9
10 // The BLAKE2b and BLAKE2s numbers are consistent with the BLAKE2 team's
11 // numbers. However, we have an Altivec/POWER7 implementation of BLAKE2s,
12 // and a POWER8 implementation of BLAKE2b (BLAKE2 is missing them). The
13 // Altivec/POWER7 code is about 2x faster than C++ when using GCC 5.0 or
14 // above. The POWER8 code is about 2.5x faster than C++ when using GCC 5.0
15 // or above. If you use GCC 4.0 (PowerMac) or GCC 4.8 (GCC Compile Farm)
16 // then the PowerPC code will be slower than C++. Be sure to use GCC 5.0
17 // or above for PowerPC builds or disable Altivec for BLAKE2b and BLAKE2s
18 // if using the old compilers.
19
20 #include "pch.h"
21 #include "config.h"
22 #include "misc.h"
23 #include "blake2.h"
24
25 // Uncomment for benchmarking C++ against SSE2 or NEON.
26 // Do so in both blake2.cpp and blake2-simd.cpp.
27 // #undef CRYPTOPP_SSE41_AVAILABLE
28 // #undef CRYPTOPP_ARM_NEON_AVAILABLE
29 // #undef CRYPTOPP_ALTIVEC_AVAILABLE
30
31 // Disable NEON/ASIMD for Cortex-A53 and A57. The shifts are too slow and C/C++ is about
32 // 3 cpb faster than NEON/ASIMD. Also see http://github.com/weidai11/cryptopp/issues/367.
33 #if (defined(__aarch32__) || defined(__aarch64__)) && defined(CRYPTOPP_SLOW_ARMV8_SHIFT)
34 # undef CRYPTOPP_ARM_NEON_AVAILABLE
35 #endif
36
37 // BLAKE2s bug on AIX 7.1 (POWER7) with XLC 12.01
38 // https://github.com/weidai11/cryptopp/issues/743
39 #if defined(__xlC__) && (__xlC__ < 0x0d01)
40 # define CRYPTOPP_DISABLE_ALTIVEC 1
41 # define CRYPTOPP_POWER7_ALTIVEC 1
42 # undef CRYPTOPP_POWER7_AVAILABLE
43 # undef CRYPTOPP_ALTIVEC_AVAILABLE
44 #endif
45
46 #if (CRYPTOPP_SSE41_AVAILABLE)
47 # include <emmintrin.h>
48 # include <tmmintrin.h>
49 # include <smmintrin.h>
50 #endif
51
52 // C1189: error: This header is specific to ARM targets
53 #if (CRYPTOPP_ARM_NEON_AVAILABLE) && !defined(_M_ARM64)
54 # include <arm_neon.h>
55 #endif
56
57 #if (CRYPTOPP_ARM_ACLE_AVAILABLE)
58 # include <stdint.h>
59 # include <arm_acle.h>
60 #endif
61
62 #if (CRYPTOPP_ALTIVEC_AVAILABLE)
63 # include "ppc_simd.h"
64 #endif
65
66 // Squash MS LNK4221 and libtool warnings
67 extern const char BLAKE2S_SIMD_FNAME[] = __FILE__;
68
69 NAMESPACE_BEGIN(CryptoPP)
70
71 // Exported by blake2.cpp
72 extern const word32 BLAKE2S_IV[8];
73 extern const word64 BLAKE2B_IV[8];
74
75 #if CRYPTOPP_SSE41_AVAILABLE
76
77 #define LOADU(p) _mm_loadu_si128((const __m128i *)(const void*)(p))
78 #define STOREU(p,r) _mm_storeu_si128((__m128i *)(void*)(p), r)
79 #define TOF(reg) _mm_castsi128_ps((reg))
80 #define TOI(reg) _mm_castps_si128((reg))
81
82 void BLAKE2_Compress32_SSE4(const byte* input, BLAKE2s_State& state)
83 {
84 #define BLAKE2S_LOAD_MSG_0_1(buf) \
85 buf = TOI(_mm_shuffle_ps(TOF(m0), TOF(m1), _MM_SHUFFLE(2,0,2,0)));
86
87 #define BLAKE2S_LOAD_MSG_0_2(buf) \
88 buf = TOI(_mm_shuffle_ps(TOF(m0), TOF(m1), _MM_SHUFFLE(3,1,3,1)));
89
90 #define BLAKE2S_LOAD_MSG_0_3(buf) \
91 buf = TOI(_mm_shuffle_ps(TOF(m2), TOF(m3), _MM_SHUFFLE(2,0,2,0)));
92
93 #define BLAKE2S_LOAD_MSG_0_4(buf) \
94 buf = TOI(_mm_shuffle_ps(TOF(m2), TOF(m3), _MM_SHUFFLE(3,1,3,1)));
95
96 #define BLAKE2S_LOAD_MSG_1_1(buf) \
97 t0 = _mm_blend_epi16(m1, m2, 0x0C); \
98 t1 = _mm_slli_si128(m3, 4); \
99 t2 = _mm_blend_epi16(t0, t1, 0xF0); \
100 buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(2,1,0,3));
101
102 #define BLAKE2S_LOAD_MSG_1_2(buf) \
103 t0 = _mm_shuffle_epi32(m2,_MM_SHUFFLE(0,0,2,0)); \
104 t1 = _mm_blend_epi16(m1,m3,0xC0); \
105 t2 = _mm_blend_epi16(t0, t1, 0xF0); \
106 buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(2,3,0,1));
107
108 #define BLAKE2S_LOAD_MSG_1_3(buf) \
109 t0 = _mm_slli_si128(m1, 4); \
110 t1 = _mm_blend_epi16(m2, t0, 0x30); \
111 t2 = _mm_blend_epi16(m0, t1, 0xF0); \
112 buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(2,3,0,1));
113
114 #define BLAKE2S_LOAD_MSG_1_4(buf) \
115 t0 = _mm_unpackhi_epi32(m0,m1); \
116 t1 = _mm_slli_si128(m3, 4); \
117 t2 = _mm_blend_epi16(t0, t1, 0x0C); \
118 buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(2,3,0,1));
119
120 #define BLAKE2S_LOAD_MSG_2_1(buf) \
121 t0 = _mm_unpackhi_epi32(m2,m3); \
122 t1 = _mm_blend_epi16(m3,m1,0x0C); \
123 t2 = _mm_blend_epi16(t0, t1, 0x0F); \
124 buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(3,1,0,2));
125
126 #define BLAKE2S_LOAD_MSG_2_2(buf) \
127 t0 = _mm_unpacklo_epi32(m2,m0); \
128 t1 = _mm_blend_epi16(t0, m0, 0xF0); \
129 t2 = _mm_slli_si128(m3, 8); \
130 buf = _mm_blend_epi16(t1, t2, 0xC0);
131
132 #define BLAKE2S_LOAD_MSG_2_3(buf) \
133 t0 = _mm_blend_epi16(m0, m2, 0x3C); \
134 t1 = _mm_srli_si128(m1, 12); \
135 t2 = _mm_blend_epi16(t0,t1,0x03); \
136 buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(1,0,3,2));
137
138 #define BLAKE2S_LOAD_MSG_2_4(buf) \
139 t0 = _mm_slli_si128(m3, 4); \
140 t1 = _mm_blend_epi16(m0, m1, 0x33); \
141 t2 = _mm_blend_epi16(t1, t0, 0xC0); \
142 buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(0,1,2,3));
143
144 #define BLAKE2S_LOAD_MSG_3_1(buf) \
145 t0 = _mm_unpackhi_epi32(m0,m1); \
146 t1 = _mm_unpackhi_epi32(t0, m2); \
147 t2 = _mm_blend_epi16(t1, m3, 0x0C); \
148 buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(3,1,0,2));
149
150 #define BLAKE2S_LOAD_MSG_3_2(buf) \
151 t0 = _mm_slli_si128(m2, 8); \
152 t1 = _mm_blend_epi16(m3,m0,0x0C); \
153 t2 = _mm_blend_epi16(t1, t0, 0xC0); \
154 buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(2,0,1,3));
155
156 #define BLAKE2S_LOAD_MSG_3_3(buf) \
157 t0 = _mm_blend_epi16(m0,m1,0x0F); \
158 t1 = _mm_blend_epi16(t0, m3, 0xC0); \
159 buf = _mm_shuffle_epi32(t1, _MM_SHUFFLE(3,0,1,2));
160
161 #define BLAKE2S_LOAD_MSG_3_4(buf) \
162 t0 = _mm_unpacklo_epi32(m0,m2); \
163 t1 = _mm_unpackhi_epi32(m1,m2); \
164 buf = _mm_unpacklo_epi64(t1,t0);
165
166 #define BLAKE2S_LOAD_MSG_4_1(buf) \
167 t0 = _mm_unpacklo_epi64(m1,m2); \
168 t1 = _mm_unpackhi_epi64(m0,m2); \
169 t2 = _mm_blend_epi16(t0,t1,0x33); \
170 buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(2,0,1,3));
171
172 #define BLAKE2S_LOAD_MSG_4_2(buf) \
173 t0 = _mm_unpackhi_epi64(m1,m3); \
174 t1 = _mm_unpacklo_epi64(m0,m1); \
175 buf = _mm_blend_epi16(t0,t1,0x33);
176
177 #define BLAKE2S_LOAD_MSG_4_3(buf) \
178 t0 = _mm_unpackhi_epi64(m3,m1); \
179 t1 = _mm_unpackhi_epi64(m2,m0); \
180 buf = _mm_blend_epi16(t1,t0,0x33);
181
182 #define BLAKE2S_LOAD_MSG_4_4(buf) \
183 t0 = _mm_blend_epi16(m0,m2,0x03); \
184 t1 = _mm_slli_si128(t0, 8); \
185 t2 = _mm_blend_epi16(t1,m3,0x0F); \
186 buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(1,2,0,3));
187
188 #define BLAKE2S_LOAD_MSG_5_1(buf) \
189 t0 = _mm_unpackhi_epi32(m0,m1); \
190 t1 = _mm_unpacklo_epi32(m0,m2); \
191 buf = _mm_unpacklo_epi64(t0,t1);
192
193 #define BLAKE2S_LOAD_MSG_5_2(buf) \
194 t0 = _mm_srli_si128(m2, 4); \
195 t1 = _mm_blend_epi16(m0,m3,0x03); \
196 buf = _mm_blend_epi16(t1,t0,0x3C);
197
198 #define BLAKE2S_LOAD_MSG_5_3(buf) \
199 t0 = _mm_blend_epi16(m1,m0,0x0C); \
200 t1 = _mm_srli_si128(m3, 4); \
201 t2 = _mm_blend_epi16(t0,t1,0x30); \
202 buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(1,2,3,0));
203
204 #define BLAKE2S_LOAD_MSG_5_4(buf) \
205 t0 = _mm_unpacklo_epi64(m1,m2); \
206 t1= _mm_shuffle_epi32(m3, _MM_SHUFFLE(0,2,0,1)); \
207 buf = _mm_blend_epi16(t0,t1,0x33);
208
209 #define BLAKE2S_LOAD_MSG_6_1(buf) \
210 t0 = _mm_slli_si128(m1, 12); \
211 t1 = _mm_blend_epi16(m0,m3,0x33); \
212 buf = _mm_blend_epi16(t1,t0,0xC0);
213
214 #define BLAKE2S_LOAD_MSG_6_2(buf) \
215 t0 = _mm_blend_epi16(m3,m2,0x30); \
216 t1 = _mm_srli_si128(m1, 4); \
217 t2 = _mm_blend_epi16(t0,t1,0x03); \
218 buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(2,1,3,0));
219
220 #define BLAKE2S_LOAD_MSG_6_3(buf) \
221 t0 = _mm_unpacklo_epi64(m0,m2); \
222 t1 = _mm_srli_si128(m1, 4); \
223 buf = _mm_shuffle_epi32(_mm_blend_epi16(t0,t1,0x0C), _MM_SHUFFLE(2,3,1,0));
224
225 #define BLAKE2S_LOAD_MSG_6_4(buf) \
226 t0 = _mm_unpackhi_epi32(m1,m2); \
227 t1 = _mm_unpackhi_epi64(m0,t0); \
228 buf = _mm_shuffle_epi32(t1, _MM_SHUFFLE(3,0,1,2));
229
230 #define BLAKE2S_LOAD_MSG_7_1(buf) \
231 t0 = _mm_unpackhi_epi32(m0,m1); \
232 t1 = _mm_blend_epi16(t0,m3,0x0F); \
233 buf = _mm_shuffle_epi32(t1,_MM_SHUFFLE(2,0,3,1));
234
235 #define BLAKE2S_LOAD_MSG_7_2(buf) \
236 t0 = _mm_blend_epi16(m2,m3,0x30); \
237 t1 = _mm_srli_si128(m0,4); \
238 t2 = _mm_blend_epi16(t0,t1,0x03); \
239 buf = _mm_shuffle_epi32(t2, _MM_SHUFFLE(1,0,2,3));
240
241 #define BLAKE2S_LOAD_MSG_7_3(buf) \
242 t0 = _mm_unpackhi_epi64(m0,m3); \
243 t1 = _mm_unpacklo_epi64(m1,m2); \
244 t2 = _mm_blend_epi16(t0,t1,0x3C); \
245 buf = _mm_shuffle_epi32(t2,_MM_SHUFFLE(0,2,3,1));
246
247 #define BLAKE2S_LOAD_MSG_7_4(buf) \
248 t0 = _mm_unpacklo_epi32(m0,m1); \
249 t1 = _mm_unpackhi_epi32(m1,m2); \
250 buf = _mm_unpacklo_epi64(t0,t1);
251
252 #define BLAKE2S_LOAD_MSG_8_1(buf) \
253 t0 = _mm_unpackhi_epi32(m1,m3); \
254 t1 = _mm_unpacklo_epi64(t0,m0); \
255 t2 = _mm_blend_epi16(t1,m2,0xC0); \
256 buf = _mm_shufflehi_epi16(t2,_MM_SHUFFLE(1,0,3,2));
257
258 #define BLAKE2S_LOAD_MSG_8_2(buf) \
259 t0 = _mm_unpackhi_epi32(m0,m3); \
260 t1 = _mm_blend_epi16(m2,t0,0xF0); \
261 buf = _mm_shuffle_epi32(t1,_MM_SHUFFLE(0,2,1,3));
262
263 #define BLAKE2S_LOAD_MSG_8_3(buf) \
264 t0 = _mm_blend_epi16(m2,m0,0x0C); \
265 t1 = _mm_slli_si128(t0,4); \
266 buf = _mm_blend_epi16(t1,m3,0x0F);
267
268 #define BLAKE2S_LOAD_MSG_8_4(buf) \
269 t0 = _mm_blend_epi16(m1,m0,0x30); \
270 buf = _mm_shuffle_epi32(t0,_MM_SHUFFLE(1,0,3,2));
271
272 #define BLAKE2S_LOAD_MSG_9_1(buf) \
273 t0 = _mm_blend_epi16(m0,m2,0x03); \
274 t1 = _mm_blend_epi16(m1,m2,0x30); \
275 t2 = _mm_blend_epi16(t1,t0,0x0F); \
276 buf = _mm_shuffle_epi32(t2,_MM_SHUFFLE(1,3,0,2));
277
278 #define BLAKE2S_LOAD_MSG_9_2(buf) \
279 t0 = _mm_slli_si128(m0,4); \
280 t1 = _mm_blend_epi16(m1,t0,0xC0); \
281 buf = _mm_shuffle_epi32(t1,_MM_SHUFFLE(1,2,0,3));
282
283 #define BLAKE2S_LOAD_MSG_9_3(buf) \
284 t0 = _mm_unpackhi_epi32(m0,m3); \
285 t1 = _mm_unpacklo_epi32(m2,m3); \
286 t2 = _mm_unpackhi_epi64(t0,t1); \
287 buf = _mm_shuffle_epi32(t2,_MM_SHUFFLE(3,0,2,1));
288
289 #define BLAKE2S_LOAD_MSG_9_4(buf) \
290 t0 = _mm_blend_epi16(m3,m2,0xC0); \
291 t1 = _mm_unpacklo_epi32(m0,m3); \
292 t2 = _mm_blend_epi16(t0,t1,0x0F); \
293 buf = _mm_shuffle_epi32(t2,_MM_SHUFFLE(0,1,2,3));
294
295 #ifdef __XOP__
296 # define MM_ROTI_EPI32(r, c) \
297 _mm_roti_epi32(r, c)
298 #else
299 # define MM_ROTI_EPI32(r, c) ( \
300 (8==-(c)) ? _mm_shuffle_epi8(r,r8) \
301 : (16==-(c)) ? _mm_shuffle_epi8(r,r16) \
302 : _mm_xor_si128(_mm_srli_epi32((r), -(c)), \
303 _mm_slli_epi32((r), 32-(-(c)))))
304 #endif
305
306 #define BLAKE2S_G1(row1,row2,row3,row4,buf) \
307 row1 = _mm_add_epi32(_mm_add_epi32(row1, buf), row2); \
308 row4 = _mm_xor_si128(row4, row1); \
309 row4 = MM_ROTI_EPI32(row4, -16); \
310 row3 = _mm_add_epi32(row3, row4); \
311 row2 = _mm_xor_si128(row2, row3); \
312 row2 = MM_ROTI_EPI32(row2, -12);
313
314 #define BLAKE2S_G2(row1,row2,row3,row4,buf) \
315 row1 = _mm_add_epi32(_mm_add_epi32(row1, buf), row2); \
316 row4 = _mm_xor_si128(row4, row1); \
317 row4 = MM_ROTI_EPI32(row4, -8); \
318 row3 = _mm_add_epi32(row3, row4); \
319 row2 = _mm_xor_si128(row2, row3); \
320 row2 = MM_ROTI_EPI32(row2, -7);
321
322 #define DIAGONALIZE(row1,row2,row3,row4) \
323 row4 = _mm_shuffle_epi32(row4, _MM_SHUFFLE(2,1,0,3)); \
324 row3 = _mm_shuffle_epi32(row3, _MM_SHUFFLE(1,0,3,2)); \
325 row2 = _mm_shuffle_epi32(row2, _MM_SHUFFLE(0,3,2,1));
326
327 #define UNDIAGONALIZE(row1,row2,row3,row4) \
328 row4 = _mm_shuffle_epi32(row4, _MM_SHUFFLE(0,3,2,1)); \
329 row3 = _mm_shuffle_epi32(row3, _MM_SHUFFLE(1,0,3,2)); \
330 row2 = _mm_shuffle_epi32(row2, _MM_SHUFFLE(2,1,0,3));
331
332 #define BLAKE2S_ROUND(r) \
333 BLAKE2S_LOAD_MSG_ ##r ##_1(buf1); \
334 BLAKE2S_G1(row1,row2,row3,row4,buf1); \
335 BLAKE2S_LOAD_MSG_ ##r ##_2(buf2); \
336 BLAKE2S_G2(row1,row2,row3,row4,buf2); \
337 DIAGONALIZE(row1,row2,row3,row4); \
338 BLAKE2S_LOAD_MSG_ ##r ##_3(buf3); \
339 BLAKE2S_G1(row1,row2,row3,row4,buf3); \
340 BLAKE2S_LOAD_MSG_ ##r ##_4(buf4); \
341 BLAKE2S_G2(row1,row2,row3,row4,buf4); \
342 UNDIAGONALIZE(row1,row2,row3,row4);
343
344 __m128i row1, row2, row3, row4;
345 __m128i buf1, buf2, buf3, buf4;
346 __m128i t0, t1, t2, ff0, ff1;
347
348 const __m128i r8 = _mm_set_epi8(12, 15, 14, 13, 8, 11, 10, 9, 4, 7, 6, 5, 0, 3, 2, 1);
349 const __m128i r16 = _mm_set_epi8(13, 12, 15, 14, 9, 8, 11, 10, 5, 4, 7, 6, 1, 0, 3, 2);
350
351 const __m128i m0 = LOADU(input + 00);
352 const __m128i m1 = LOADU(input + 16);
353 const __m128i m2 = LOADU(input + 32);
354 const __m128i m3 = LOADU(input + 48);
355
356 row1 = ff0 = LOADU(state.h()+0);
357 row2 = ff1 = LOADU(state.h()+4);
358 row3 = LOADU(BLAKE2S_IV+0);
359 row4 = _mm_xor_si128(LOADU(BLAKE2S_IV+4), LOADU(state.t()+0));
360
361 BLAKE2S_ROUND(0);
362 BLAKE2S_ROUND(1);
363 BLAKE2S_ROUND(2);
364 BLAKE2S_ROUND(3);
365 BLAKE2S_ROUND(4);
366 BLAKE2S_ROUND(5);
367 BLAKE2S_ROUND(6);
368 BLAKE2S_ROUND(7);
369 BLAKE2S_ROUND(8);
370 BLAKE2S_ROUND(9);
371
372 STOREU(state.h()+0, _mm_xor_si128(ff0, _mm_xor_si128(row1, row3)));
373 STOREU(state.h()+4, _mm_xor_si128(ff1, _mm_xor_si128(row2, row4)));
374 }
375 #endif // CRYPTOPP_SSE41_AVAILABLE
376
377 #if CRYPTOPP_ARM_NEON_AVAILABLE
378 void BLAKE2_Compress32_NEON(const byte* input, BLAKE2s_State& state)
379 {
380 #define BLAKE2S_LOAD_MSG_0_1(buf) \
381 do { uint32x2_t t0, t1; \
382 t0 = vzip_u32(vget_low_u32(m0), vget_high_u32(m0)).val[0]; \
383 t1 = vzip_u32(vget_low_u32(m1), vget_high_u32(m1)).val[0]; \
384 buf = vcombine_u32(t0, t1); } while(0)
385
386 #define BLAKE2S_LOAD_MSG_0_2(buf) \
387 do { uint32x2_t t0, t1; \
388 t0 = vzip_u32(vget_low_u32(m0), vget_high_u32(m0)).val[1]; \
389 t1 = vzip_u32(vget_low_u32(m1), vget_high_u32(m1)).val[1]; \
390 buf = vcombine_u32(t0, t1); } while(0)
391
392 #define BLAKE2S_LOAD_MSG_0_3(buf) \
393 do { uint32x2_t t0, t1; \
394 t0 = vzip_u32(vget_low_u32(m2), vget_high_u32(m2)).val[0]; \
395 t1 = vzip_u32(vget_low_u32(m3), vget_high_u32(m3)).val[0]; \
396 buf = vcombine_u32(t0, t1); } while(0)
397
398 #define BLAKE2S_LOAD_MSG_0_4(buf) \
399 do { uint32x2_t t0, t1; \
400 t0 = vzip_u32(vget_low_u32(m2), vget_high_u32(m2)).val[1]; \
401 t1 = vzip_u32(vget_low_u32(m3), vget_high_u32(m3)).val[1]; \
402 buf = vcombine_u32(t0, t1); } while(0)
403
404 #define BLAKE2S_LOAD_MSG_1_1(buf) \
405 do { uint32x2_t t0, t1; \
406 t0 = vzip_u32(vget_high_u32(m3), vget_low_u32(m1)).val[0]; \
407 t1 = vzip_u32(vget_low_u32(m2), vget_low_u32(m3)).val[1]; \
408 buf = vcombine_u32(t0, t1); } while(0)
409
410 #define BLAKE2S_LOAD_MSG_1_2(buf) \
411 do { uint32x2_t t0, t1; \
412 t0 = vzip_u32(vget_high_u32(m2), vget_low_u32(m2)).val[0]; \
413 t1 = vext_u32(vget_high_u32(m3), vget_high_u32(m1), 1); \
414 buf = vcombine_u32(t0, t1); } while(0)
415
416 #define BLAKE2S_LOAD_MSG_1_3(buf) \
417 do { uint32x2_t t0, t1; \
418 t0 = vext_u32(vget_low_u32(m0), vget_low_u32(m0), 1); \
419 t1 = vzip_u32(vget_high_u32(m2), vget_low_u32(m1)).val[1]; \
420 buf = vcombine_u32(t0, t1); } while(0)
421
422 #define BLAKE2S_LOAD_MSG_1_4(buf) \
423 do { uint32x2_t t0, t1; \
424 t0 = vzip_u32(vget_low_u32(m3), vget_high_u32(m0)).val[0]; \
425 t1 = vzip_u32(vget_high_u32(m1), vget_high_u32(m0)).val[1]; \
426 buf = vcombine_u32(t0, t1); } while(0)
427
428 #define BLAKE2S_LOAD_MSG_2_1(buf) \
429 do { uint32x2_t t0, t1; \
430 t0 = vext_u32(vget_high_u32(m2), vget_low_u32(m3), 1); \
431 t1 = vzip_u32(vget_low_u32(m1), vget_high_u32(m3)).val[1]; \
432 buf = vcombine_u32(t0, t1); } while(0)
433
434 #define BLAKE2S_LOAD_MSG_2_2(buf) \
435 do { uint32x2_t t0, t1; \
436 t0 = vzip_u32(vget_low_u32(m2), vget_low_u32(m0)).val[0]; \
437 t1 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_high_u32(m0), vget_low_u32(m3)); \
438 buf = vcombine_u32(t0, t1); } while(0)
439
440 #define BLAKE2S_LOAD_MSG_2_3(buf) \
441 do { uint32x2_t t0, t1; \
442 t0 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_high_u32(m2), vget_high_u32(m0)); \
443 t1 = vzip_u32(vget_high_u32(m1), vget_low_u32(m2)).val[1]; \
444 buf = vcombine_u32(t0, t1); } while(0)
445
446 #define BLAKE2S_LOAD_MSG_2_4(buf) \
447 do { uint32x2_t t0, t1; \
448 t0 = vzip_u32(vget_high_u32(m3), vget_high_u32(m1)).val[0]; \
449 t1 = vext_u32(vget_low_u32(m0), vget_low_u32(m1), 1); \
450 buf = vcombine_u32(t0, t1); } while(0)
451
452 #define BLAKE2S_LOAD_MSG_3_1(buf) \
453 do { uint32x2_t t0, t1; \
454 t0 = vzip_u32(vget_high_u32(m1), vget_high_u32(m0)).val[1]; \
455 t1 = vzip_u32(vget_low_u32(m3), vget_high_u32(m2)).val[1]; \
456 buf = vcombine_u32(t0, t1); } while(0)
457
458 #define BLAKE2S_LOAD_MSG_3_2(buf) \
459 do { uint32x2_t t0, t1; \
460 t0 = vzip_u32(vget_low_u32(m2), vget_low_u32(m0)).val[1]; \
461 t1 = vzip_u32(vget_low_u32(m3), vget_high_u32(m3)).val[0]; \
462 buf = vcombine_u32(t0, t1); } while(0)
463
464 #define BLAKE2S_LOAD_MSG_3_3(buf) \
465 do { uint32x2_t t0, t1; \
466 t0 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_high_u32(m0), vget_low_u32(m1)); \
467 t1 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_low_u32(m1), vget_high_u32(m3)); \
468 buf = vcombine_u32(t0, t1); } while(0)
469
470 #define BLAKE2S_LOAD_MSG_3_4(buf) \
471 do { uint32x2_t t0, t1; \
472 t0 = vzip_u32(vget_high_u32(m1), vget_high_u32(m2)).val[0]; \
473 t1 = vzip_u32(vget_low_u32(m0), vget_low_u32(m2)).val[0]; \
474 buf = vcombine_u32(t0, t1); } while(0)
475
476 #define BLAKE2S_LOAD_MSG_4_1(buf) \
477 do { uint32x2_t t0, t1; \
478 t0 = vzip_u32(vget_low_u32(m2), vget_low_u32(m1)).val[1]; \
479 t1 = vzip_u32((vget_high_u32(m0)), vget_high_u32(m2)).val[0]; \
480 buf = vcombine_u32(t0, t1); } while(0)
481
482 #define BLAKE2S_LOAD_MSG_4_2(buf) \
483 do { uint32x2_t t0, t1; \
484 t0 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_low_u32(m0), vget_high_u32(m1)); \
485 t1 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_low_u32(m1), vget_high_u32(m3)); \
486 buf = vcombine_u32(t0, t1); } while(0)
487
488 #define BLAKE2S_LOAD_MSG_4_3(buf) \
489 do { uint32x2_t t0, t1; \
490 t0 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_high_u32(m3), vget_high_u32(m2)); \
491 t1 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_high_u32(m1), vget_high_u32(m0)); \
492 buf = vcombine_u32(t0, t1); } while(0)
493
494 #define BLAKE2S_LOAD_MSG_4_4(buf) \
495 do { uint32x2_t t0, t1; \
496 t0 = vext_u32(vget_low_u32(m0), vget_low_u32(m3), 1); \
497 t1 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_low_u32(m2), vget_low_u32(m3)); \
498 buf = vcombine_u32(t0, t1); } while(0)
499
500 #define BLAKE2S_LOAD_MSG_5_1(buf) \
501 do { uint32x2_t t0, t1; \
502 t0 = vzip_u32((vget_high_u32(m0)), vget_high_u32(m1)).val[0]; \
503 t1 = vzip_u32(vget_low_u32(m0), vget_low_u32(m2)).val[0]; \
504 buf = vcombine_u32(t0, t1); } while(0)
505
506 #define BLAKE2S_LOAD_MSG_5_2(buf) \
507 do { uint32x2_t t0, t1; \
508 t0 = vzip_u32(vget_low_u32(m3), vget_high_u32(m2)).val[0]; \
509 t1 = vzip_u32(vget_high_u32(m2), vget_high_u32(m0)).val[1]; \
510 buf = vcombine_u32(t0, t1); } while(0)
511
512 #define BLAKE2S_LOAD_MSG_5_3(buf) \
513 do { uint32x2_t t0, t1; \
514 t0 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_low_u32(m1), vget_high_u32(m1)); \
515 t1 = vzip_u32(vget_high_u32(m3), vget_low_u32(m0)).val[1]; \
516 buf = vcombine_u32(t0, t1); } while(0)
517
518 #define BLAKE2S_LOAD_MSG_5_4(buf) \
519 do { uint32x2_t t0, t1; \
520 t0 = vzip_u32(vget_low_u32(m3), vget_low_u32(m1)).val[1]; \
521 t1 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_high_u32(m3), vget_low_u32(m2)); \
522 buf = vcombine_u32(t0, t1); } while(0)
523
524 #define BLAKE2S_LOAD_MSG_6_1(buf) \
525 do { uint32x2_t t0, t1; \
526 t0 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_low_u32(m3), vget_low_u32(m0)); \
527 t1 = vzip_u32(vget_high_u32(m3), vget_low_u32(m1)).val[0]; \
528 buf = vcombine_u32(t0, t1); } while(0)
529
530 #define BLAKE2S_LOAD_MSG_6_2(buf) \
531 do { uint32x2_t t0, t1; \
532 t0 = vzip_u32(vget_low_u32(m1), vget_high_u32(m3)).val[1]; \
533 t1 = vext_u32(vget_low_u32(m3), vget_high_u32(m2), 1); \
534 buf = vcombine_u32(t0, t1); } while(0)
535
536 #define BLAKE2S_LOAD_MSG_6_3(buf) \
537 do { uint32x2_t t0, t1; \
538 t0 = vzip_u32(vget_low_u32(m0), vget_high_u32(m1)).val[0]; \
539 t1 = vext_u32(vget_low_u32(m2), vget_low_u32(m2), 1); \
540 buf = vcombine_u32(t0, t1); } while(0)
541
542 #define BLAKE2S_LOAD_MSG_6_4(buf) \
543 do { uint32x2_t t0, t1; \
544 t0 = vzip_u32(vget_high_u32(m1), vget_high_u32(m0)).val[1]; \
545 t1 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_high_u32(m0), vget_high_u32(m2)); \
546 buf = vcombine_u32(t0, t1); } while(0)
547
548 #define BLAKE2S_LOAD_MSG_7_1(buf) \
549 do { uint32x2_t t0, t1; \
550 t0 = vzip_u32(vget_low_u32(m3), vget_high_u32(m1)).val[1]; \
551 t1 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_low_u32(m3), vget_high_u32(m0)); \
552 buf = vcombine_u32(t0, t1); } while(0)
553
554 #define BLAKE2S_LOAD_MSG_7_2(buf) \
555 do { uint32x2_t t0, t1; \
556 t0 = vext_u32(vget_high_u32(m2), vget_high_u32(m3), 1); \
557 t1 = vzip_u32(vget_low_u32(m0), vget_low_u32(m2)).val[1]; \
558 buf = vcombine_u32(t0, t1); } while(0)
559
560 #define BLAKE2S_LOAD_MSG_7_3(buf) \
561 do { uint32x2_t t0, t1; \
562 t0 = vzip_u32(vget_low_u32(m1), vget_high_u32(m3)).val[1]; \
563 t1 = vzip_u32(vget_low_u32(m2), vget_high_u32(m0)).val[0]; \
564 buf = vcombine_u32(t0, t1); } while(0)
565
566 #define BLAKE2S_LOAD_MSG_7_4(buf) \
567 do { uint32x2_t t0, t1; \
568 t0 = vzip_u32(vget_low_u32(m0), vget_low_u32(m1)).val[0]; \
569 t1 = vzip_u32(vget_high_u32(m1), vget_high_u32(m2)).val[0]; \
570 buf = vcombine_u32(t0, t1); } while(0)
571
572 #define BLAKE2S_LOAD_MSG_8_1(buf) \
573 do { uint32x2_t t0, t1; \
574 t0 = vzip_u32(vget_high_u32(m1), vget_high_u32(m3)).val[0]; \
575 t1 = vext_u32(vget_high_u32(m2), vget_low_u32(m0), 1); \
576 buf = vcombine_u32(t0, t1); } while(0)
577
578 #define BLAKE2S_LOAD_MSG_8_2(buf) \
579 do { uint32x2_t t0, t1; \
580 t0 = vzip_u32(vget_high_u32(m3), vget_low_u32(m2)).val[1]; \
581 t1 = vext_u32(vget_high_u32(m0), vget_low_u32(m2), 1); \
582 buf = vcombine_u32(t0, t1); } while(0)
583
584 #define BLAKE2S_LOAD_MSG_8_3(buf) \
585 do { uint32x2_t t0, t1; \
586 t0 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_low_u32(m3), vget_low_u32(m3)); \
587 t1 = vext_u32(vget_low_u32(m0), vget_high_u32(m2), 1); \
588 buf = vcombine_u32(t0, t1); } while(0)
589
590 #define BLAKE2S_LOAD_MSG_8_4(buf) \
591 do { uint32x2_t t0, t1; \
592 t0 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_high_u32(m0), vget_high_u32(m1)); \
593 t1 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_low_u32(m1), vget_low_u32(m1)); \
594 buf = vcombine_u32(t0, t1); } while(0)
595
596 #define BLAKE2S_LOAD_MSG_9_1(buf) \
597 do { uint32x2_t t0, t1; \
598 t0 = vzip_u32(vget_high_u32(m2), vget_low_u32(m2)).val[0]; \
599 t1 = vzip_u32(vget_high_u32(m1), vget_low_u32(m0)).val[1]; \
600 buf = vcombine_u32(t0, t1); } while(0)
601
602 #define BLAKE2S_LOAD_MSG_9_2(buf) \
603 do { uint32x2_t t0, t1; \
604 t0 = vzip_u32((vget_high_u32(m0)), vget_low_u32(m1)).val[0]; \
605 t1 = vbsl_u32(vcreate_u32(0xFFFFFFFF), vget_high_u32(m1), vget_low_u32(m1)); \
606 buf = vcombine_u32(t0, t1); } while(0)
607
608 #define BLAKE2S_LOAD_MSG_9_3(buf) \
609 do { uint32x2_t t0, t1; \
610 t0 = vzip_u32(vget_high_u32(m3), vget_low_u32(m2)).val[1]; \
611 t1 = vzip_u32((vget_high_u32(m0)), vget_low_u32(m3)).val[1]; \
612 buf = vcombine_u32(t0, t1); } while(0)
613
614 #define BLAKE2S_LOAD_MSG_9_4(buf) \
615 do { uint32x2_t t0, t1; \
616 t0 = vext_u32(vget_high_u32(m2), vget_high_u32(m3), 1); \
617 t1 = vzip_u32(vget_low_u32(m3), vget_low_u32(m0)).val[0]; \
618 buf = vcombine_u32(t0, t1); } while(0)
619
620 #define vrorq_n_u32_16(x) vreinterpretq_u32_u16(vrev32q_u16(vreinterpretq_u16_u32(x)))
621
622 #define vrorq_n_u32_8(x) vsriq_n_u32(vshlq_n_u32((x), 24), (x), 8)
623
624 #define vrorq_n_u32(x, c) vsriq_n_u32(vshlq_n_u32((x), 32-(c)), (x), (c))
625
626 #define BLAKE2S_G1(row1,row2,row3,row4,buf) \
627 do { \
628 row1 = vaddq_u32(vaddq_u32(row1, buf), row2); row4 = veorq_u32(row4, row1); \
629 row4 = vrorq_n_u32_16(row4); row3 = vaddq_u32(row3, row4); \
630 row2 = veorq_u32(row2, row3); row2 = vrorq_n_u32(row2, 12); \
631 } while(0)
632
633 #define BLAKE2S_G2(row1,row2,row3,row4,buf) \
634 do { \
635 row1 = vaddq_u32(vaddq_u32(row1, buf), row2); row4 = veorq_u32(row4, row1); \
636 row4 = vrorq_n_u32_8(row4); row3 = vaddq_u32(row3, row4); \
637 row2 = veorq_u32(row2, row3); row2 = vrorq_n_u32(row2, 7); \
638 } while(0)
639
640 #define BLAKE2S_DIAGONALIZE(row1,row2,row3,row4) \
641 do { \
642 row4 = vextq_u32(row4, row4, 3); row3 = vextq_u32(row3, row3, 2); row2 = vextq_u32(row2, row2, 1); \
643 } while(0)
644
645 #define BLAKE2S_UNDIAGONALIZE(row1,row2,row3,row4) \
646 do { \
647 row4 = vextq_u32(row4, row4, 1); \
648 row3 = vextq_u32(row3, row3, 2); \
649 row2 = vextq_u32(row2, row2, 3); \
650 } while(0)
651
652 #define BLAKE2S_ROUND(r) \
653 do { \
654 uint32x4_t buf1, buf2, buf3, buf4; \
655 BLAKE2S_LOAD_MSG_ ##r ##_1(buf1); \
656 BLAKE2S_G1(row1,row2,row3,row4,buf1); \
657 BLAKE2S_LOAD_MSG_ ##r ##_2(buf2); \
658 BLAKE2S_G2(row1,row2,row3,row4,buf2); \
659 BLAKE2S_DIAGONALIZE(row1,row2,row3,row4); \
660 BLAKE2S_LOAD_MSG_ ##r ##_3(buf3); \
661 BLAKE2S_G1(row1,row2,row3,row4,buf3); \
662 BLAKE2S_LOAD_MSG_ ##r ##_4(buf4); \
663 BLAKE2S_G2(row1,row2,row3,row4,buf4); \
664 BLAKE2S_UNDIAGONALIZE(row1,row2,row3,row4); \
665 } while(0)
666
667 const uint32x4_t m0 = vreinterpretq_u32_u8(vld1q_u8(input + 00));
668 const uint32x4_t m1 = vreinterpretq_u32_u8(vld1q_u8(input + 16));
669 const uint32x4_t m2 = vreinterpretq_u32_u8(vld1q_u8(input + 32));
670 const uint32x4_t m3 = vreinterpretq_u32_u8(vld1q_u8(input + 48));
671
672 uint32x4_t row1, row2, row3, row4;
673
674 const uint32x4_t f0 = row1 = vld1q_u32(state.h()+0);
675 const uint32x4_t f1 = row2 = vld1q_u32(state.h()+4);
676 row3 = vld1q_u32(BLAKE2S_IV+0);
677 row4 = veorq_u32(vld1q_u32(BLAKE2S_IV+4), vld1q_u32(state.t()+0));
678
679 BLAKE2S_ROUND(0);
680 BLAKE2S_ROUND(1);
681 BLAKE2S_ROUND(2);
682 BLAKE2S_ROUND(3);
683 BLAKE2S_ROUND(4);
684 BLAKE2S_ROUND(5);
685 BLAKE2S_ROUND(6);
686 BLAKE2S_ROUND(7);
687 BLAKE2S_ROUND(8);
688 BLAKE2S_ROUND(9);
689
690 vst1q_u32(state.h()+0, veorq_u32(f0, veorq_u32(row1, row3)));
691 vst1q_u32(state.h()+4, veorq_u32(f1, veorq_u32(row2, row4)));
692 }
693 #endif // CRYPTOPP_ARM_NEON_AVAILABLE
694
695 #if (CRYPTOPP_POWER7_AVAILABLE || CRYPTOPP_ALTIVEC_AVAILABLE)
696
697 inline uint32x4_p VecLoad32(const void* p)
698 {
699 return VecLoad((const word32*)p);
700 }
701
702 inline uint32x4_p VecLoad32LE(const void* p)
703 {
704 #if __BIG_ENDIAN__
705 const uint8x16_p m = {3,2,1,0, 7,6,5,4, 11,10,9,8, 15,14,13,12};
706 const uint32x4_p v = VecLoad((const word32*)p);
707 return VecPermute(v, v, m);
708 #else
709 return VecLoad((const word32*)p);
710 #endif
711 }
712
713 inline void VecStore32(void* p, const uint32x4_p x)
714 {
715 VecStore(x, (word32*)p);
716 }
717
718 inline void VecStore32LE(void* p, const uint32x4_p x)
719 {
720 #if __BIG_ENDIAN__
721 const uint8x16_p m = {3,2,1,0, 7,6,5,4, 11,10,9,8, 15,14,13,12};
722 VecStore(VecPermute(x, x, m), (word32*)p);
723 #else
724 VecStore(x, (word32*)p);
725 #endif
726 }
727
728 template <unsigned int E1, unsigned int E2>
729 inline uint32x4_p VectorSet32(const uint32x4_p a, const uint32x4_p b)
730 {
731 // Re-index. I'd like to use something like Z=Y*4 and then
732 // VecShiftLeftOctet<Z>(b) but it crashes early Red Hat
733 // GCC compilers.
734 enum {X=E1&3, Y=E2&3};
735
736 // Don't care element
737 const unsigned int DC = 31;
738
739 // Element 0 combinations
740 if (X == 0 && Y == 0)
741 {
742 const uint8x16_p mask = {0,1,2,3, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};
743 return VecPermute(a, b, mask);
744 }
745 else if (X == 0 && Y == 1)
746 {
747 const uint8x16_p mask = {0,1,2,3, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};
748 return VecPermute(a, VecShiftLeftOctet<4>(b), mask);
749 }
750 else if (X == 0 && Y == 2)
751 {
752 const uint8x16_p mask = {0,1,2,3, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};
753 return VecPermute(a, VecShiftLeftOctet<8>(b), mask);
754 }
755 else if (X == 0 && Y == 3)
756 {
757 const uint8x16_p mask = {0,1,2,3, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};
758 return VecPermute(a, VecShiftLeftOctet<12>(b), mask);
759 }
760
761 // Element 1 combinations
762 else if (X == 1 && Y == 0)
763 {
764 const uint8x16_p mask = {4,5,6,7, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};
765 return VecPermute(a, b, mask);
766 }
767 else if (X == 1 && Y == 1)
768 {
769 const uint8x16_p mask = {4,5,6,7, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};
770 return VecPermute(a, VecShiftLeftOctet<4>(b), mask);
771 }
772 else if (X == 1 && Y == 2)
773 {
774 const uint8x16_p mask = {4,5,6,7, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};
775 return VecPermute(a, VecShiftLeftOctet<8>(b), mask);
776 }
777 else if (X == 1 && Y == 3)
778 {
779 const uint8x16_p mask = {4,5,6,7, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};
780 return VecPermute(a, VecShiftLeftOctet<12>(b), mask);
781 }
782
783 // Element 2 combinations
784 else if (X == 2 && Y == 0)
785 {
786 const uint8x16_p mask = {8,9,10,11, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};
787 return VecPermute(a, b, mask);
788 }
789 else if (X == 2 && Y == 1)
790 {
791 const uint8x16_p mask = {8,9,10,11, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};
792 return VecPermute(a, VecShiftLeftOctet<4>(b), mask);
793 }
794 else if (X == 2 && Y == 2)
795 {
796 const uint8x16_p mask = {8,9,10,11, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};
797 return VecPermute(a, VecShiftLeftOctet<8>(b), mask);
798 }
799 else if (X == 2 && Y == 3)
800 {
801 const uint8x16_p mask = {8,9,10,11, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};
802 return VecPermute(a, VecShiftLeftOctet<12>(b), mask);
803 }
804
805 // Element 3 combinations
806 else if (X == 3 && Y == 0)
807 {
808 const uint8x16_p mask = {12,13,14,15, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};
809 return VecPermute(a, b, mask);
810 }
811 else if (X == 3 && Y == 1)
812 {
813 const uint8x16_p mask = {12,13,14,15, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};
814 return VecPermute(a, VecShiftLeftOctet<4>(b), mask);
815 }
816 else if (X == 3 && Y == 2)
817 {
818 const uint8x16_p mask = {12,13,14,15, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};
819 return VecPermute(a, VecShiftLeftOctet<8>(b), mask);
820 }
821 else if (X == 3 && Y == 3)
822 {
823 const uint8x16_p mask = {12,13,14,15, 16,17,18,19, DC,DC,DC,DC, DC,DC,DC,DC};
824 return VecPermute(a, VecShiftLeftOctet<12>(b), mask);
825 }
826
827 // Quiet IBM XLC warning
828 return VecXor(a, a);
829 }
830
831 template <unsigned int E1, unsigned int E2, unsigned int E3, unsigned int E4>
832 inline uint32x4_p VectorSet32(const uint32x4_p a, const uint32x4_p b,
833 const uint32x4_p c, const uint32x4_p d)
834 {
835 // Re-index
836 enum {W=E1&3, X=E2&3, Y=E3&3, Z=E4&3};
837
838 const uint32x4_p t0 = VectorSet32<W,X>(a, b);
839 const uint32x4_p t1 = VectorSet32<Y,Z>(c, d);
840
841 // Power7 follows SSE2's implementation, and this is _mm_set_epi32.
842 const uint8x16_p mask = {20,21,22,23, 16,17,18,19, 4,5,6,7, 0,1,2,3};
843 return VecPermute(t0, t1, mask);
844 }
845
846 template<>
847 uint32x4_p VectorSet32<2,0,2,0>(const uint32x4_p a, const uint32x4_p b,
848 const uint32x4_p c, const uint32x4_p d)
849 {
850 // a=b, c=d, mask is {2,0, 2,0}
851 CRYPTOPP_UNUSED(b); CRYPTOPP_UNUSED(d);
852 const uint8x16_p mask = {16,17,18,19, 24,25,26,27, 0,1,2,3, 8,9,10,11};
853 return VecPermute(a, c, mask);
854 }
855
856 template<>
857 uint32x4_p VectorSet32<3,1,3,1>(const uint32x4_p a, const uint32x4_p b,
858 const uint32x4_p c, const uint32x4_p d)
859 {
860 // a=b, c=d, mask is {3,1, 3,1}
861 CRYPTOPP_UNUSED(b); CRYPTOPP_UNUSED(d);
862 const uint8x16_p mask = {20,21,22,23, 28,29,30,31, 4,5,6,7, 12,13,14,15};
863 return VecPermute(a, c, mask);
864 }
865
866 // BLAKE2_Compress32_CORE will use either POWER7 or ALTIVEC,
867 // depending on the flags used to compile this source file. The
868 // abstractions are handled in VecLoad, VecStore and friends. In
869 // the future we may to provide both POWER7 or ALTIVEC at the same
870 // time to better support distros.
871 void BLAKE2_Compress32_CORE(const byte* input, BLAKE2s_State& state)
872 {
873 # define m1 m0
874 # define m2 m0
875 # define m3 m0
876
877 # define m5 m4
878 # define m6 m4
879 # define m7 m4
880
881 # define m9 m8
882 # define m10 m8
883 # define m11 m8
884
885 # define m13 m12
886 # define m14 m12
887 # define m15 m12
888
889 // #define BLAKE2S_LOAD_MSG_0_1(buf) buf = VectorSet32<6,4,2,0>(m6,m4,m2,m0);
890 #define BLAKE2S_LOAD_MSG_0_1(buf) buf = VectorSet32<2,0,2,0>(m6,m4,m2,m0);
891 // #define BLAKE2S_LOAD_MSG_0_2(buf) buf = VectorSet32<7,5,3,1>(m7,m5,m3,m1);
892 #define BLAKE2S_LOAD_MSG_0_2(buf) buf = VectorSet32<3,1,3,1>(m7,m5,m3,m1);
893 // #define BLAKE2S_LOAD_MSG_0_3(buf) buf = VectorSet32<14,12,10,8>(m14,m12,m10,m8);
894 #define BLAKE2S_LOAD_MSG_0_3(buf) buf = VectorSet32<2,0,2,0>(m14,m12,m10,m8);
895 // #define BLAKE2S_LOAD_MSG_0_4(buf) buf = VectorSet32<15,13,11,9>(m15,m13,m11,m9);
896 #define BLAKE2S_LOAD_MSG_0_4(buf) buf = VectorSet32<3,1,3,1>(m15,m13,m11,m9);
897
898 #define BLAKE2S_LOAD_MSG_1_1(buf) buf = VectorSet32<13,9,4,14>(m13,m9,m4,m14);
899 #define BLAKE2S_LOAD_MSG_1_2(buf) buf = VectorSet32<6,15,8,10>(m6,m15,m8,m10)
900 #define BLAKE2S_LOAD_MSG_1_3(buf) buf = VectorSet32<5,11,0,1>(m5,m11,m0,m1)
901 #define BLAKE2S_LOAD_MSG_1_4(buf) buf = VectorSet32<3,7,2,12>(m3,m7,m2,m12)
902
903 #define BLAKE2S_LOAD_MSG_2_1(buf) buf = VectorSet32<15,5,12,11>(m15,m5,m12,m11)
904 #define BLAKE2S_LOAD_MSG_2_2(buf) buf = VectorSet32<13,2,0,8>(m13,m2,m0,m8)
905 #define BLAKE2S_LOAD_MSG_2_3(buf) buf = VectorSet32<9,7,3,10>(m9,m7,m3,m10)
906 #define BLAKE2S_LOAD_MSG_2_4(buf) buf = VectorSet32<4,1,6,14>(m4,m1,m6,m14)
907
908 #define BLAKE2S_LOAD_MSG_3_1(buf) buf = VectorSet32<11,13,3,7>(m11,m13,m3,m7)
909 #define BLAKE2S_LOAD_MSG_3_2(buf) buf = VectorSet32<14,12,1,9>(m14,m12,m1,m9)
910 #define BLAKE2S_LOAD_MSG_3_3(buf) buf = VectorSet32<15,4,5,2>(m15,m4,m5,m2)
911 #define BLAKE2S_LOAD_MSG_3_4(buf) buf = VectorSet32<8,0,10,6>(m8,m0,m10,m6)
912
913 #define BLAKE2S_LOAD_MSG_4_1(buf) buf = VectorSet32<10,2,5,9>(m10,m2,m5,m9)
914 #define BLAKE2S_LOAD_MSG_4_2(buf) buf = VectorSet32<15,4,7,0>(m15,m4,m7,m0)
915 #define BLAKE2S_LOAD_MSG_4_3(buf) buf = VectorSet32<3,6,11,14>(m3,m6,m11,m14)
916 #define BLAKE2S_LOAD_MSG_4_4(buf) buf = VectorSet32<13,8,12,1>(m13,m8,m12,m1)
917
918 #define BLAKE2S_LOAD_MSG_5_1(buf) buf = VectorSet32<8,0,6,2>(m8,m0,m6,m2)
919 #define BLAKE2S_LOAD_MSG_5_2(buf) buf = VectorSet32<3,11,10,12>(m3,m11,m10,m12)
920 #define BLAKE2S_LOAD_MSG_5_3(buf) buf = VectorSet32<1,15,7,4>(m1,m15,m7,m4)
921 #define BLAKE2S_LOAD_MSG_5_4(buf) buf = VectorSet32<9,14,5,13>(m9,m14,m5,m13)
922
923 #define BLAKE2S_LOAD_MSG_6_1(buf) buf = VectorSet32<4,14,1,12>(m4,m14,m1,m12)
924 #define BLAKE2S_LOAD_MSG_6_2(buf) buf = VectorSet32<10,13,15,5>(m10,m13,m15,m5)
925 #define BLAKE2S_LOAD_MSG_6_3(buf) buf = VectorSet32<8,9,6,0>(m8,m9,m6,m0)
926 #define BLAKE2S_LOAD_MSG_6_4(buf) buf = VectorSet32<11,2,3,7>(m11,m2,m3,m7)
927
928 #define BLAKE2S_LOAD_MSG_7_1(buf) buf = VectorSet32<3,12,7,13>(m3,m12,m7,m13)
929 #define BLAKE2S_LOAD_MSG_7_2(buf) buf = VectorSet32<9,1,14,11>(m9,m1,m14,m11)
930 #define BLAKE2S_LOAD_MSG_7_3(buf) buf = VectorSet32<2,8,15,5>(m2,m8,m15,m5)
931 #define BLAKE2S_LOAD_MSG_7_4(buf) buf = VectorSet32<10,6,4,0>(m10,m6,m4,m0)
932
933 #define BLAKE2S_LOAD_MSG_8_1(buf) buf = VectorSet32<0,11,14,6>(m0,m11,m14,m6)
934 #define BLAKE2S_LOAD_MSG_8_2(buf) buf = VectorSet32<8,3,9,15>(m8,m3,m9,m15)
935 #define BLAKE2S_LOAD_MSG_8_3(buf) buf = VectorSet32<10,1,13,12>(m10,m1,m13,m12)
936 #define BLAKE2S_LOAD_MSG_8_4(buf) buf = VectorSet32<5,4,7,2>(m5,m4,m7,m2)
937
938 #define BLAKE2S_LOAD_MSG_9_1(buf) buf = VectorSet32<1,7,8,10>(m1,m7,m8,m10)
939 #define BLAKE2S_LOAD_MSG_9_2(buf) buf = VectorSet32<5,6,4,2>(m5,m6,m4,m2)
940 #define BLAKE2S_LOAD_MSG_9_3(buf) buf = VectorSet32<13,3,9,15>(m13,m3,m9,m15)
941 #define BLAKE2S_LOAD_MSG_9_4(buf) buf = VectorSet32<0,12,14,11>(m0,m12,m14,m11)
942
943 #define vec_ror_16(x) VecRotateRight<16>(x)
944 #define vec_ror_12(x) VecRotateRight<12>(x)
945 #define vec_ror_8(x) VecRotateRight<8>(x)
946 #define vec_ror_7(x) VecRotateRight<7>(x)
947
948 #define BLAKE2S_G1(row1,row2,row3,row4,buf) \
949 row1 = VecAdd(VecAdd(row1, buf), row2); \
950 row4 = VecXor(row4, row1); \
951 row4 = vec_ror_16(row4); \
952 row3 = VecAdd(row3, row4); \
953 row2 = VecXor(row2, row3); \
954 row2 = vec_ror_12(row2);
955
956 #define BLAKE2S_G2(row1,row2,row3,row4,buf) \
957 row1 = VecAdd(VecAdd(row1, buf), row2); \
958 row4 = VecXor(row4, row1); \
959 row4 = vec_ror_8(row4); \
960 row3 = VecAdd(row3, row4); \
961 row2 = VecXor(row2, row3); \
962 row2 = vec_ror_7(row2);
963
964 const uint8x16_p D2103_MASK = {12,13,14,15, 0,1,2,3, 4,5,6,7, 8,9,10,11};
965 const uint8x16_p D1032_MASK = {8,9,10,11, 12,13,14,15, 0,1,2,3, 4,5,6,7};
966 const uint8x16_p D0321_MASK = {4,5,6,7, 8,9,10,11, 12,13,14,15, 0,1,2,3};
967
968 #define BLAKE2S_DIAGONALIZE(row1,row2,row3,row4) \
969 row4 = VecPermute(row4, row4, D2103_MASK); \
970 row3 = VecPermute(row3, row3, D1032_MASK); \
971 row2 = VecPermute(row2, row2, D0321_MASK);
972
973 #define BLAKE2S_UNDIAGONALIZE(row1,row2,row3,row4) \
974 row4 = VecPermute(row4, row4, D0321_MASK); \
975 row3 = VecPermute(row3, row3, D1032_MASK); \
976 row2 = VecPermute(row2, row2, D2103_MASK);
977
978 #define BLAKE2S_ROUND(r) \
979 BLAKE2S_LOAD_MSG_ ##r ##_1(buf1); \
980 BLAKE2S_G1(row1,row2,row3,row4,buf1); \
981 BLAKE2S_LOAD_MSG_ ##r ##_2(buf2); \
982 BLAKE2S_G2(row1,row2,row3,row4,buf2); \
983 BLAKE2S_DIAGONALIZE(row1,row2,row3,row4); \
984 BLAKE2S_LOAD_MSG_ ##r ##_3(buf3); \
985 BLAKE2S_G1(row1,row2,row3,row4,buf3); \
986 BLAKE2S_LOAD_MSG_ ##r ##_4(buf4); \
987 BLAKE2S_G2(row1,row2,row3,row4,buf4); \
988 BLAKE2S_UNDIAGONALIZE(row1,row2,row3,row4);
989
990 uint32x4_p row1, row2, row3, row4;
991 uint32x4_p buf1, buf2, buf3, buf4;
992 uint32x4_p ff0, ff1;
993
994 const uint32x4_p m0 = VecLoad32LE(input + 0);
995 const uint32x4_p m4 = VecLoad32LE(input + 16);
996 const uint32x4_p m8 = VecLoad32LE(input + 32);
997 const uint32x4_p m12 = VecLoad32LE(input + 48);
998
999 row1 = ff0 = VecLoad32LE(state.h()+0);
1000 row2 = ff1 = VecLoad32LE(state.h()+4);
1001 row3 = VecLoad32(BLAKE2S_IV+0);
1002 row4 = VecXor(VecLoad32(BLAKE2S_IV+4), VecLoad32(state.t()+0));
1003
1004 BLAKE2S_ROUND(0);
1005 BLAKE2S_ROUND(1);
1006 BLAKE2S_ROUND(2);
1007 BLAKE2S_ROUND(3);
1008 BLAKE2S_ROUND(4);
1009 BLAKE2S_ROUND(5);
1010 BLAKE2S_ROUND(6);
1011 BLAKE2S_ROUND(7);
1012 BLAKE2S_ROUND(8);
1013 BLAKE2S_ROUND(9);
1014
1015 VecStore32LE(state.h()+0, VecXor(ff0, VecXor(row1, row3)));
1016 VecStore32LE(state.h()+4, VecXor(ff1, VecXor(row2, row4)));
1017 }
1018 #endif // CRYPTOPP_POWER7_AVAILABLE || CRYPTOPP_ALTIVEC_AVAILABLE
1019
1020 #if (CRYPTOPP_POWER7_AVAILABLE)
1021
1022 void BLAKE2_Compress32_POWER7(const byte* input, BLAKE2s_State& state)
1023 {
1024 BLAKE2_Compress32_CORE(input, state);
1025 }
1026
1027 #elif (CRYPTOPP_ALTIVEC_AVAILABLE)
1028
1029 void BLAKE2_Compress32_ALTIVEC(const byte* input, BLAKE2s_State& state)
1030 {
1031 BLAKE2_Compress32_CORE(input, state);
1032 }
1033
1034 #endif
1035
1036 NAMESPACE_END
misc.h
Utility functions for the Crypto++ library.
config.h
Library configuration file.
VecPermute
T1 VecPermute(const T1 vec, const T2 mask)
Permutes a vector.
Definition: ppc_simd.h:1010
uint32x4_p
__vector unsigned int uint32x4_p
Vector of 32-bit elements.
Definition: ppc_simd.h:129
ppc_simd.h
Support functions for PowerPC and vector operations.
pch.h
Precompiled header file.
blake2.h
Classes for BLAKE2b and BLAKE2s message digests and keyed message digests.
VecStore
void VecStore(const T data, byte dest[16])
Stores a vector to a byte array.
Definition: ppc_simd.h:605
VecXor
T1 VecXor(const T1 vec1, const T2 vec2)
XOR two vectors.
Definition: ppc_simd.h:916
BLAKE2s_State
BLAKE2s state information.
Definition: blake2.h:163
CryptoPP
Crypto++ library namespace.
VecLoad
uint32x4_p VecLoad(const byte src[16])
Loads a vector from a byte array.
Definition: ppc_simd.h:253
uint8x16_p
__vector unsigned char uint8x16_p
Vector of 8-bit elements.
Definition: ppc_simd.h:119