Crypto++  8.4
Free C++ class library of cryptographic schemes
simon128_simd.cpp
1 // simon_simd.cpp - written and placed in the public domain by Jeffrey Walton
2 //
3 // This source file uses intrinsics and built-ins to gain access to
4 // SSSE3, ARM NEON and ARMv8a, and Altivec instructions. A separate
5 // source file is needed because additional CXXFLAGS are required to enable
6 // the appropriate instructions sets in some build configurations.
7 
8 #include "pch.h"
9 #include "config.h"
10 
11 #include "simon.h"
12 #include "misc.h"
13 
14 // Uncomment for benchmarking C++ against SSE or NEON.
15 // Do so in both simon.cpp and simon_simd.cpp.
16 // #undef CRYPTOPP_SSSE3_AVAILABLE
17 // #undef CRYPTOPP_ARM_NEON_AVAILABLE
18 
19 #if (CRYPTOPP_SSSE3_AVAILABLE)
20 # include "adv_simd.h"
21 # include <pmmintrin.h>
22 # include <tmmintrin.h>
23 #endif
24 
25 #if defined(__XOP__)
26 # include <ammintrin.h>
27 # if defined(__GNUC__)
28 # include <x86intrin.h>
29 # endif
30 #endif
31 
32 #if (CRYPTOPP_ARM_NEON_HEADER)
33 # include "adv_simd.h"
34 # include <arm_neon.h>
35 #endif
36 
37 #if (CRYPTOPP_ARM_ACLE_HEADER)
38 # include <stdint.h>
39 # include <arm_acle.h>
40 #endif
41 
42 #if defined(_M_ARM64)
43 # include "adv_simd.h"
44 #endif
45 
46 #if (CRYPTOPP_ALTIVEC_AVAILABLE)
47 # include "adv_simd.h"
48 # include "ppc_simd.h"
49 #endif
50 
51 // Squash MS LNK4221 and libtool warnings
52 extern const char SIMON128_SIMD_FNAME[] = __FILE__;
53 
54 ANONYMOUS_NAMESPACE_BEGIN
55 
56 using CryptoPP::byte;
57 using CryptoPP::word32;
58 using CryptoPP::word64;
59 using CryptoPP::vec_swap; // SunCC
60 
61 // *************************** ARM NEON ************************** //
62 
63 #if (CRYPTOPP_ARM_NEON_AVAILABLE)
64 
65 // Missing from Microsoft's ARM A-32 implementation
66 #if defined(_MSC_VER) && !defined(_M_ARM64)
67 inline uint64x2_t vld1q_dup_u64(const uint64_t* ptr)
68 {
69  return vmovq_n_u64(*ptr);
70 }
71 #endif
72 
73 template <class T>
74 inline T UnpackHigh64(const T& a, const T& b)
75 {
76  const uint64x1_t x(vget_high_u64((uint64x2_t)a));
77  const uint64x1_t y(vget_high_u64((uint64x2_t)b));
78  return (T)vcombine_u64(x, y);
79 }
80 
81 template <class T>
82 inline T UnpackLow64(const T& a, const T& b)
83 {
84  const uint64x1_t x(vget_low_u64((uint64x2_t)a));
85  const uint64x1_t y(vget_low_u64((uint64x2_t)b));
86  return (T)vcombine_u64(x, y);
87 }
88 
89 template <unsigned int R>
90 inline uint64x2_t RotateLeft64(const uint64x2_t& val)
91 {
92  const uint64x2_t a(vshlq_n_u64(val, R));
93  const uint64x2_t b(vshrq_n_u64(val, 64 - R));
94  return vorrq_u64(a, b);
95 }
96 
97 template <unsigned int R>
98 inline uint64x2_t RotateRight64(const uint64x2_t& val)
99 {
100  const uint64x2_t a(vshlq_n_u64(val, 64 - R));
101  const uint64x2_t b(vshrq_n_u64(val, R));
102  return vorrq_u64(a, b);
103 }
104 
105 #if defined(__aarch32__) || defined(__aarch64__)
106 // Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks.
107 template <>
108 inline uint64x2_t RotateLeft64<8>(const uint64x2_t& val)
109 {
110  const uint8_t maskb[16] = { 7,0,1,2, 3,4,5,6, 15,8,9,10, 11,12,13,14 };
111  const uint8x16_t mask = vld1q_u8(maskb);
112 
113  return vreinterpretq_u64_u8(
114  vqtbl1q_u8(vreinterpretq_u8_u64(val), mask));
115 }
116 
117 // Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks.
118 template <>
119 inline uint64x2_t RotateRight64<8>(const uint64x2_t& val)
120 {
121  const uint8_t maskb[16] = { 1,2,3,4, 5,6,7,0, 9,10,11,12, 13,14,15,8 };
122  const uint8x16_t mask = vld1q_u8(maskb);
123 
124  return vreinterpretq_u64_u8(
125  vqtbl1q_u8(vreinterpretq_u8_u64(val), mask));
126 }
127 #endif
128 
129 inline uint64x2_t SIMON128_f(const uint64x2_t& val)
130 {
131  return veorq_u64(RotateLeft64<2>(val),
132  vandq_u64(RotateLeft64<1>(val), RotateLeft64<8>(val)));
133 }
134 
135 inline void SIMON128_Enc_Block(uint64x2_t &block0, uint64x2_t &block1,
136  const word64 *subkeys, unsigned int rounds)
137 {
138  // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ...
139  uint64x2_t x1 = UnpackHigh64(block0, block1);
140  uint64x2_t y1 = UnpackLow64(block0, block1);
141 
142  for (size_t i = 0; i < static_cast<size_t>(rounds & ~1)-1; i += 2)
143  {
144  const uint64x2_t rk1 = vld1q_dup_u64(subkeys+i);
145  y1 = veorq_u64(veorq_u64(y1, SIMON128_f(x1)), rk1);
146 
147  const uint64x2_t rk2 = vld1q_dup_u64(subkeys+i+1);
148  x1 = veorq_u64(veorq_u64(x1, SIMON128_f(y1)), rk2);
149  }
150 
151  if (rounds & 1)
152  {
153  const uint64x2_t rk = vld1q_dup_u64(subkeys+rounds-1);
154 
155  y1 = veorq_u64(veorq_u64(y1, SIMON128_f(x1)), rk);
156  std::swap(x1, y1);
157  }
158 
159  // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ...
160  block0 = UnpackLow64(y1, x1);
161  block1 = UnpackHigh64(y1, x1);
162 }
163 
164 inline void SIMON128_Enc_6_Blocks(uint64x2_t &block0, uint64x2_t &block1,
165  uint64x2_t &block2, uint64x2_t &block3, uint64x2_t &block4, uint64x2_t &block5,
166  const word64 *subkeys, unsigned int rounds)
167 {
168  // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ...
169  uint64x2_t x1 = UnpackHigh64(block0, block1);
170  uint64x2_t y1 = UnpackLow64(block0, block1);
171  uint64x2_t x2 = UnpackHigh64(block2, block3);
172  uint64x2_t y2 = UnpackLow64(block2, block3);
173  uint64x2_t x3 = UnpackHigh64(block4, block5);
174  uint64x2_t y3 = UnpackLow64(block4, block5);
175 
176  for (size_t i = 0; i < static_cast<size_t>(rounds & ~1) - 1; i += 2)
177  {
178  const uint64x2_t rk1 = vld1q_dup_u64(subkeys+i);
179  y1 = veorq_u64(veorq_u64(y1, SIMON128_f(x1)), rk1);
180  y2 = veorq_u64(veorq_u64(y2, SIMON128_f(x2)), rk1);
181  y3 = veorq_u64(veorq_u64(y3, SIMON128_f(x3)), rk1);
182 
183  const uint64x2_t rk2 = vld1q_dup_u64(subkeys+i+1);
184  x1 = veorq_u64(veorq_u64(x1, SIMON128_f(y1)), rk2);
185  x2 = veorq_u64(veorq_u64(x2, SIMON128_f(y2)), rk2);
186  x3 = veorq_u64(veorq_u64(x3, SIMON128_f(y3)), rk2);
187  }
188 
189  if (rounds & 1)
190  {
191  const uint64x2_t rk = vld1q_dup_u64(subkeys + rounds - 1);
192 
193  y1 = veorq_u64(veorq_u64(y1, SIMON128_f(x1)), rk);
194  y2 = veorq_u64(veorq_u64(y2, SIMON128_f(x2)), rk);
195  y3 = veorq_u64(veorq_u64(y3, SIMON128_f(x3)), rk);
196  std::swap(x1, y1); std::swap(x2, y2); std::swap(x3, y3);
197  }
198 
199  // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ...
200  block0 = UnpackLow64(y1, x1);
201  block1 = UnpackHigh64(y1, x1);
202  block2 = UnpackLow64(y2, x2);
203  block3 = UnpackHigh64(y2, x2);
204  block4 = UnpackLow64(y3, x3);
205  block5 = UnpackHigh64(y3, x3);
206 }
207 
208 inline void SIMON128_Dec_Block(uint64x2_t &block0, uint64x2_t &block1,
209  const word64 *subkeys, unsigned int rounds)
210 {
211  // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ...
212  uint64x2_t x1 = UnpackHigh64(block0, block1);
213  uint64x2_t y1 = UnpackLow64(block0, block1);
214 
215  if (rounds & 1)
216  {
217  std::swap(x1, y1);
218  const uint64x2_t rk = vld1q_dup_u64(subkeys + rounds - 1);
219 
220  y1 = veorq_u64(veorq_u64(y1, rk), SIMON128_f(x1));
221  rounds--;
222  }
223 
224  for (int i = static_cast<int>(rounds-2); i >= 0; i -= 2)
225  {
226  const uint64x2_t rk1 = vld1q_dup_u64(subkeys+i+1);
227  x1 = veorq_u64(veorq_u64(x1, SIMON128_f(y1)), rk1);
228 
229  const uint64x2_t rk2 = vld1q_dup_u64(subkeys+i);
230  y1 = veorq_u64(veorq_u64(y1, SIMON128_f(x1)), rk2);
231  }
232 
233  // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ...
234  block0 = UnpackLow64(y1, x1);
235  block1 = UnpackHigh64(y1, x1);
236 }
237 
238 inline void SIMON128_Dec_6_Blocks(uint64x2_t &block0, uint64x2_t &block1,
239  uint64x2_t &block2, uint64x2_t &block3, uint64x2_t &block4, uint64x2_t &block5,
240  const word64 *subkeys, unsigned int rounds)
241 {
242  // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ...
243  uint64x2_t x1 = UnpackHigh64(block0, block1);
244  uint64x2_t y1 = UnpackLow64(block0, block1);
245  uint64x2_t x2 = UnpackHigh64(block2, block3);
246  uint64x2_t y2 = UnpackLow64(block2, block3);
247  uint64x2_t x3 = UnpackHigh64(block4, block5);
248  uint64x2_t y3 = UnpackLow64(block4, block5);
249 
250  if (rounds & 1)
251  {
252  std::swap(x1, y1); std::swap(x2, y2); std::swap(x3, y3);
253  const uint64x2_t rk = vld1q_dup_u64(subkeys + rounds - 1);
254 
255  y1 = veorq_u64(veorq_u64(y1, rk), SIMON128_f(x1));
256  y2 = veorq_u64(veorq_u64(y2, rk), SIMON128_f(x2));
257  y3 = veorq_u64(veorq_u64(y3, rk), SIMON128_f(x3));
258  rounds--;
259  }
260 
261  for (int i = static_cast<int>(rounds-2); i >= 0; i -= 2)
262  {
263  const uint64x2_t rk1 = vld1q_dup_u64(subkeys + i + 1);
264  x1 = veorq_u64(veorq_u64(x1, SIMON128_f(y1)), rk1);
265  x2 = veorq_u64(veorq_u64(x2, SIMON128_f(y2)), rk1);
266  x3 = veorq_u64(veorq_u64(x3, SIMON128_f(y3)), rk1);
267 
268  const uint64x2_t rk2 = vld1q_dup_u64(subkeys + i);
269  y1 = veorq_u64(veorq_u64(y1, SIMON128_f(x1)), rk2);
270  y2 = veorq_u64(veorq_u64(y2, SIMON128_f(x2)), rk2);
271  y3 = veorq_u64(veorq_u64(y3, SIMON128_f(x3)), rk2);
272  }
273 
274  // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ...
275  block0 = UnpackLow64(y1, x1);
276  block1 = UnpackHigh64(y1, x1);
277  block2 = UnpackLow64(y2, x2);
278  block3 = UnpackHigh64(y2, x2);
279  block4 = UnpackLow64(y3, x3);
280  block5 = UnpackHigh64(y3, x3);
281 }
282 
283 #endif // CRYPTOPP_ARM_NEON_AVAILABLE
284 
285 // ***************************** IA-32 ***************************** //
286 
287 #if (CRYPTOPP_SSSE3_AVAILABLE)
288 
289 // Clang intrinsic casts, http://bugs.llvm.org/show_bug.cgi?id=20670
290 #ifndef M128_CAST
291 # define M128_CAST(x) ((__m128i *)(void *)(x))
292 #endif
293 #ifndef CONST_M128_CAST
294 # define CONST_M128_CAST(x) ((const __m128i *)(const void *)(x))
295 #endif
296 
297 // GCC double casts, https://www.spinics.net/lists/gcchelp/msg47735.html
298 #ifndef DOUBLE_CAST
299 # define DOUBLE_CAST(x) ((double *)(void *)(x))
300 #endif
301 #ifndef CONST_DOUBLE_CAST
302 # define CONST_DOUBLE_CAST(x) ((const double *)(const void *)(x))
303 #endif
304 
305 inline void Swap128(__m128i& a,__m128i& b)
306 {
307 #if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x5120)
308  // __m128i is an unsigned long long[2], and support for swapping it was not added until C++11.
309  // SunCC 12.1 - 12.3 fail to consume the swap; while SunCC 12.4 consumes it without -std=c++11.
310  vec_swap(a, b);
311 #else
312  std::swap(a, b);
313 #endif
314 }
315 
316 template <unsigned int R>
317 inline __m128i RotateLeft64(const __m128i& val)
318 {
319 #if defined(__XOP__)
320  return _mm_roti_epi64(val, R);
321 #else
322  return _mm_or_si128(
323  _mm_slli_epi64(val, R), _mm_srli_epi64(val, 64-R));
324 #endif
325 }
326 
327 template <unsigned int R>
328 inline __m128i RotateRight64(const __m128i& val)
329 {
330 #if defined(__XOP__)
331  return _mm_roti_epi64(val, 64-R);
332 #else
333  return _mm_or_si128(
334  _mm_slli_epi64(val, 64-R), _mm_srli_epi64(val, R));
335 #endif
336 }
337 
338 // Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks.
339 template <>
340 __m128i RotateLeft64<8>(const __m128i& val)
341 {
342 #if defined(__XOP__)
343  return _mm_roti_epi64(val, 8);
344 #else
345  const __m128i mask = _mm_set_epi8(14,13,12,11, 10,9,8,15, 6,5,4,3, 2,1,0,7);
346  return _mm_shuffle_epi8(val, mask);
347 #endif
348 }
349 
350 // Faster than two Shifts and an Or. Thanks to Louis Wingers and Bryan Weeks.
351 template <>
352 __m128i RotateRight64<8>(const __m128i& val)
353 {
354 #if defined(__XOP__)
355  return _mm_roti_epi64(val, 64-8);
356 #else
357  const __m128i mask = _mm_set_epi8(8,15,14,13, 12,11,10,9, 0,7,6,5, 4,3,2,1);
358  return _mm_shuffle_epi8(val, mask);
359 #endif
360 }
361 
362 inline __m128i SIMON128_f(const __m128i& v)
363 {
364  return _mm_xor_si128(RotateLeft64<2>(v),
365  _mm_and_si128(RotateLeft64<1>(v), RotateLeft64<8>(v)));
366 }
367 
368 inline void SIMON128_Enc_Block(__m128i &block0, __m128i &block1,
369  const word64 *subkeys, unsigned int rounds)
370 {
371  // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ...
372  __m128i x1 = _mm_unpackhi_epi64(block0, block1);
373  __m128i y1 = _mm_unpacklo_epi64(block0, block1);
374 
375  for (size_t i = 0; i < static_cast<size_t>(rounds & ~1)-1; i += 2)
376  {
377  // Round keys are pre-splated in forward direction
378  const __m128i rk1 = _mm_load_si128(CONST_M128_CAST(subkeys+i*2));
379  y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON128_f(x1)), rk1);
380 
381  const __m128i rk2 = _mm_load_si128(CONST_M128_CAST(subkeys+(i+1)*2));
382  x1 = _mm_xor_si128(_mm_xor_si128(x1, SIMON128_f(y1)), rk2);
383  }
384 
385  if (rounds & 1)
386  {
387  // Round keys are pre-splated in forward direction
388  const __m128i rk = _mm_load_si128(CONST_M128_CAST(subkeys+(rounds-1)*2));
389 
390  y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON128_f(x1)), rk);
391  Swap128(x1, y1);
392  }
393 
394  // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ...
395  block0 = _mm_unpacklo_epi64(y1, x1);
396  block1 = _mm_unpackhi_epi64(y1, x1);
397 }
398 
399 inline void SIMON128_Enc_6_Blocks(__m128i &block0, __m128i &block1,
400  __m128i &block2, __m128i &block3, __m128i &block4, __m128i &block5,
401  const word64 *subkeys, unsigned int rounds)
402 {
403  // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ...
404  __m128i x1 = _mm_unpackhi_epi64(block0, block1);
405  __m128i y1 = _mm_unpacklo_epi64(block0, block1);
406  __m128i x2 = _mm_unpackhi_epi64(block2, block3);
407  __m128i y2 = _mm_unpacklo_epi64(block2, block3);
408  __m128i x3 = _mm_unpackhi_epi64(block4, block5);
409  __m128i y3 = _mm_unpacklo_epi64(block4, block5);
410 
411  for (size_t i = 0; i < static_cast<size_t>(rounds & ~1) - 1; i += 2)
412  {
413  // Round keys are pre-splated in forward direction
414  const __m128i rk1 = _mm_load_si128(CONST_M128_CAST(subkeys+i*2));
415  y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON128_f(x1)), rk1);
416  y2 = _mm_xor_si128(_mm_xor_si128(y2, SIMON128_f(x2)), rk1);
417  y3 = _mm_xor_si128(_mm_xor_si128(y3, SIMON128_f(x3)), rk1);
418 
419  // Round keys are pre-splated in forward direction
420  const __m128i rk2 = _mm_load_si128(CONST_M128_CAST(subkeys+(i+1)*2));
421  x1 = _mm_xor_si128(_mm_xor_si128(x1, SIMON128_f(y1)), rk2);
422  x2 = _mm_xor_si128(_mm_xor_si128(x2, SIMON128_f(y2)), rk2);
423  x3 = _mm_xor_si128(_mm_xor_si128(x3, SIMON128_f(y3)), rk2);
424  }
425 
426  if (rounds & 1)
427  {
428  // Round keys are pre-splated in forward direction
429  const __m128i rk = _mm_load_si128(CONST_M128_CAST(subkeys+(rounds-1)*2));
430  y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON128_f(x1)), rk);
431  y2 = _mm_xor_si128(_mm_xor_si128(y2, SIMON128_f(x2)), rk);
432  y3 = _mm_xor_si128(_mm_xor_si128(y3, SIMON128_f(x3)), rk);
433  Swap128(x1, y1); Swap128(x2, y2); Swap128(x3, y3);
434  }
435 
436  // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ...
437  block0 = _mm_unpacklo_epi64(y1, x1);
438  block1 = _mm_unpackhi_epi64(y1, x1);
439  block2 = _mm_unpacklo_epi64(y2, x2);
440  block3 = _mm_unpackhi_epi64(y2, x2);
441  block4 = _mm_unpacklo_epi64(y3, x3);
442  block5 = _mm_unpackhi_epi64(y3, x3);
443 }
444 
445 inline void SIMON128_Dec_Block(__m128i &block0, __m128i &block1,
446  const word64 *subkeys, unsigned int rounds)
447 {
448  // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ...
449  __m128i x1 = _mm_unpackhi_epi64(block0, block1);
450  __m128i y1 = _mm_unpacklo_epi64(block0, block1);
451 
452  if (rounds & 1)
453  {
454  const __m128i rk = _mm_castpd_si128(
455  _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys + rounds - 1)));
456 
457  Swap128(x1, y1);
458  y1 = _mm_xor_si128(_mm_xor_si128(y1, rk), SIMON128_f(x1));
459  rounds--;
460  }
461 
462  for (int i = static_cast<int>(rounds-2); i >= 0; i -= 2)
463  {
464  const __m128i rk1 = _mm_castpd_si128(
465  _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys+i+1)));
466  x1 = _mm_xor_si128(_mm_xor_si128(x1, SIMON128_f(y1)), rk1);
467 
468  const __m128i rk2 = _mm_castpd_si128(
469  _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys+i)));
470  y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON128_f(x1)), rk2);
471  }
472 
473  // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ...
474  block0 = _mm_unpacklo_epi64(y1, x1);
475  block1 = _mm_unpackhi_epi64(y1, x1);
476 }
477 
478 inline void SIMON128_Dec_6_Blocks(__m128i &block0, __m128i &block1,
479  __m128i &block2, __m128i &block3, __m128i &block4, __m128i &block5,
480  const word64 *subkeys, unsigned int rounds)
481 {
482  // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ...
483  __m128i x1 = _mm_unpackhi_epi64(block0, block1);
484  __m128i y1 = _mm_unpacklo_epi64(block0, block1);
485  __m128i x2 = _mm_unpackhi_epi64(block2, block3);
486  __m128i y2 = _mm_unpacklo_epi64(block2, block3);
487  __m128i x3 = _mm_unpackhi_epi64(block4, block5);
488  __m128i y3 = _mm_unpacklo_epi64(block4, block5);
489 
490  if (rounds & 1)
491  {
492  const __m128i rk = _mm_castpd_si128(
493  _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys + rounds - 1)));
494 
495  Swap128(x1, y1); Swap128(x2, y2); Swap128(x3, y3);
496  y1 = _mm_xor_si128(_mm_xor_si128(y1, rk), SIMON128_f(x1));
497  y2 = _mm_xor_si128(_mm_xor_si128(y2, rk), SIMON128_f(x2));
498  y3 = _mm_xor_si128(_mm_xor_si128(y3, rk), SIMON128_f(x3));
499  rounds--;
500  }
501 
502  for (int i = static_cast<int>(rounds-2); i >= 0; i -= 2)
503  {
504  const __m128i rk1 = _mm_castpd_si128(
505  _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys + i + 1)));
506  x1 = _mm_xor_si128(_mm_xor_si128(x1, SIMON128_f(y1)), rk1);
507  x2 = _mm_xor_si128(_mm_xor_si128(x2, SIMON128_f(y2)), rk1);
508  x3 = _mm_xor_si128(_mm_xor_si128(x3, SIMON128_f(y3)), rk1);
509 
510  const __m128i rk2 = _mm_castpd_si128(
511  _mm_loaddup_pd(CONST_DOUBLE_CAST(subkeys + i)));
512  y1 = _mm_xor_si128(_mm_xor_si128(y1, SIMON128_f(x1)), rk2);
513  y2 = _mm_xor_si128(_mm_xor_si128(y2, SIMON128_f(x2)), rk2);
514  y3 = _mm_xor_si128(_mm_xor_si128(y3, SIMON128_f(x3)), rk2);
515  }
516 
517  // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ...
518  block0 = _mm_unpacklo_epi64(y1, x1);
519  block1 = _mm_unpackhi_epi64(y1, x1);
520  block2 = _mm_unpacklo_epi64(y2, x2);
521  block3 = _mm_unpackhi_epi64(y2, x2);
522  block4 = _mm_unpacklo_epi64(y3, x3);
523  block5 = _mm_unpackhi_epi64(y3, x3);
524 }
525 
526 #endif // CRYPTOPP_SSSE3_AVAILABLE
527 
528 // ***************************** Altivec ***************************** //
529 
530 #if (CRYPTOPP_ALTIVEC_AVAILABLE)
531 
532 // Altivec uses native 64-bit types on 64-bit environments, or 32-bit types
533 // in 32-bit environments. Speck128 will use the appropriate type for the
534 // environment. Functions like VecAdd64 have two overloads, one for each
535 // environment. The 32-bit overload treats uint32x4_p like a 64-bit type,
536 // and does things like perform a add with carry or subtract with borrow.
537 
538 // Speck128 on Power8 performed as expected because of 64-bit environment.
539 // Performance sucked on old PowerPC machines because of 32-bit environments.
540 // At Crypto++ 8.3 we added an implementation that operated on 32-bit words.
541 // Native 64-bit Speck128 performance dropped from about 4.1 to 6.3 cpb, but
542 // 32-bit Speck128 improved from 66.5 cpb to 10.4 cpb. Overall it was a
543 // good win even though we lost some performance in 64-bit environments.
544 
547 #if defined(_ARCH_PWR8)
549 #endif
550 
551 using CryptoPP::VecAdd64;
552 using CryptoPP::VecSub64;
553 using CryptoPP::VecAnd64;
554 using CryptoPP::VecOr64;
555 using CryptoPP::VecXor64;
559 using CryptoPP::VecLoad;
562 
563 #if defined(_ARCH_PWR8)
564 #define simon128_t uint64x2_p
565 #else
566 #define simon128_t uint32x4_p
567 #endif
568 
569 inline simon128_t SIMON128_f(const simon128_t val)
570 {
571  return (simon128_t)VecXor64(VecRotateLeft64<2>(val),
572  VecAnd64(VecRotateLeft64<1>(val), VecRotateLeft64<8>(val)));
573 }
574 
575 inline void SIMON128_Enc_Block(uint32x4_p &block, const word64 *subkeys, unsigned int rounds)
576 {
577 #if (CRYPTOPP_BIG_ENDIAN)
578  const uint8x16_p m1 = {31,30,29,28,27,26,25,24, 15,14,13,12,11,10,9,8};
579  const uint8x16_p m2 = {23,22,21,20,19,18,17,16, 7,6,5,4,3,2,1,0};
580 #else
581  const uint8x16_p m1 = {7,6,5,4,3,2,1,0, 23,22,21,20,19,18,17,16};
582  const uint8x16_p m2 = {15,14,13,12,11,10,9,8, 31,30,29,28,27,26,25,24};
583 #endif
584 
585  // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ...
586  simon128_t x1 = (simon128_t)VecPermute(block, block, m1);
587  simon128_t y1 = (simon128_t)VecPermute(block, block, m2);
588 
589  for (size_t i = 0; i < static_cast<size_t>(rounds & ~1)-1; i += 2)
590  {
591  // Round keys are pre-splated in forward direction
592  const word32* ptr1 = reinterpret_cast<const word32*>(subkeys+i*2);
593  const simon128_t rk1 = (simon128_t)VecLoadAligned(ptr1);
594  const word32* ptr2 = reinterpret_cast<const word32*>(subkeys+(i+1)*2);
595  const simon128_t rk2 = (simon128_t)VecLoadAligned(ptr2);
596 
597  y1 = VecXor64(VecXor64(y1, SIMON128_f(x1)), rk1);
598  x1 = VecXor64(VecXor64(x1, SIMON128_f(y1)), rk2);
599  }
600 
601  if (rounds & 1)
602  {
603  // Round keys are pre-splated in forward direction
604  const word32* ptr = reinterpret_cast<const word32*>(subkeys+(rounds-1)*2);
605  const simon128_t rk = (simon128_t)VecLoadAligned(ptr);
606 
607  y1 = VecXor64(VecXor64(y1, SIMON128_f(x1)), rk);
608 
609  std::swap(x1, y1);
610  }
611 
612 #if (CRYPTOPP_BIG_ENDIAN)
613  const uint8x16_p m3 = {31,30,29,28,27,26,25,24, 15,14,13,12,11,10,9,8};
614  //const uint8x16_p m4 = {23,22,21,20,19,18,17,16, 7,6,5,4,3,2,1,0};
615 #else
616  const uint8x16_p m3 = {7,6,5,4,3,2,1,0, 23,22,21,20,19,18,17,16};
617  //const uint8x16_p m4 = {15,14,13,12,11,10,9,8, 31,30,29,28,27,26,25,24};
618 #endif
619 
620  // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ...
621  block = (uint32x4_p)VecPermute(x1, y1, m3);
622 }
623 
624 inline void SIMON128_Dec_Block(uint32x4_p &block, const word64 *subkeys, unsigned int rounds)
625 {
626 #if (CRYPTOPP_BIG_ENDIAN)
627  const uint8x16_p m1 = {31,30,29,28,27,26,25,24, 15,14,13,12,11,10,9,8};
628  const uint8x16_p m2 = {23,22,21,20,19,18,17,16, 7,6,5,4,3,2,1,0};
629 #else
630  const uint8x16_p m1 = {7,6,5,4,3,2,1,0, 23,22,21,20,19,18,17,16};
631  const uint8x16_p m2 = {15,14,13,12,11,10,9,8, 31,30,29,28,27,26,25,24};
632 #endif
633 
634  // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ...
635  simon128_t x1 = (simon128_t)VecPermute(block, block, m1);
636  simon128_t y1 = (simon128_t)VecPermute(block, block, m2);
637 
638  if (rounds & 1)
639  {
640  std::swap(x1, y1);
641 
642  const word32* ptr = reinterpret_cast<const word32*>(subkeys+rounds-1);
643  const simon128_t tk = (simon128_t)VecLoad(ptr);
644  const simon128_t rk = (simon128_t)VecSplatElement64<0>(tk);
645 
646  y1 = VecXor64(VecXor64(y1, rk), SIMON128_f(x1));
647  rounds--;
648  }
649 
650  for (int i = static_cast<int>(rounds-2); i >= 0; i -= 2)
651  {
652  const word32* ptr = reinterpret_cast<const word32*>(subkeys+i);
653  const simon128_t tk = (simon128_t)VecLoad(ptr);
654  const simon128_t rk1 = (simon128_t)VecSplatElement64<1>(tk);
655  const simon128_t rk2 = (simon128_t)VecSplatElement64<0>(tk);
656 
657  x1 = VecXor64(VecXor64(x1, SIMON128_f(y1)), rk1);
658  y1 = VecXor64(VecXor64(y1, SIMON128_f(x1)), rk2);
659  }
660 
661 #if (CRYPTOPP_BIG_ENDIAN)
662  const uint8x16_p m3 = {31,30,29,28,27,26,25,24, 15,14,13,12,11,10,9,8};
663  //const uint8x16_p m4 = {23,22,21,20,19,18,17,16, 7,6,5,4,3,2,1,0};
664 #else
665  const uint8x16_p m3 = {7,6,5,4,3,2,1,0, 23,22,21,20,19,18,17,16};
666  //const uint8x16_p m4 = {15,14,13,12,11,10,9,8, 31,30,29,28,27,26,25,24};
667 #endif
668 
669  // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ...
670  block = (uint32x4_p)VecPermute(x1, y1, m3);
671 }
672 
673 inline void SIMON128_Enc_6_Blocks(uint32x4_p &block0, uint32x4_p &block1,
674  uint32x4_p &block2, uint32x4_p &block3, uint32x4_p &block4,
675  uint32x4_p &block5, const word64 *subkeys, unsigned int rounds)
676 {
677 #if (CRYPTOPP_BIG_ENDIAN)
678  const uint8x16_p m1 = {31,30,29,28,27,26,25,24, 15,14,13,12,11,10,9,8};
679  const uint8x16_p m2 = {23,22,21,20,19,18,17,16, 7,6,5,4,3,2,1,0};
680 #else
681  const uint8x16_p m1 = {7,6,5,4,3,2,1,0, 23,22,21,20,19,18,17,16};
682  const uint8x16_p m2 = {15,14,13,12,11,10,9,8, 31,30,29,28,27,26,25,24};
683 #endif
684 
685  // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ...
686  simon128_t x1 = (simon128_t)VecPermute(block0, block1, m1);
687  simon128_t y1 = (simon128_t)VecPermute(block0, block1, m2);
688  simon128_t x2 = (simon128_t)VecPermute(block2, block3, m1);
689  simon128_t y2 = (simon128_t)VecPermute(block2, block3, m2);
690  simon128_t x3 = (simon128_t)VecPermute(block4, block5, m1);
691  simon128_t y3 = (simon128_t)VecPermute(block4, block5, m2);
692 
693  for (size_t i = 0; i < static_cast<size_t>(rounds & ~1)-1; i += 2)
694  {
695  // Round keys are pre-splated in forward direction
696  const word32* ptr1 = reinterpret_cast<const word32*>(subkeys+i*2);
697  const simon128_t rk1 = (simon128_t)VecLoadAligned(ptr1);
698 
699  const word32* ptr2 = reinterpret_cast<const word32*>(subkeys+(i+1)*2);
700  const simon128_t rk2 = (simon128_t)VecLoadAligned(ptr2);
701 
702  y1 = VecXor64(VecXor64(y1, SIMON128_f(x1)), rk1);
703  y2 = VecXor64(VecXor64(y2, SIMON128_f(x2)), rk1);
704  y3 = VecXor64(VecXor64(y3, SIMON128_f(x3)), rk1);
705 
706  x1 = VecXor64(VecXor64(x1, SIMON128_f(y1)), rk2);
707  x2 = VecXor64(VecXor64(x2, SIMON128_f(y2)), rk2);
708  x3 = VecXor64(VecXor64(x3, SIMON128_f(y3)), rk2);
709  }
710 
711  if (rounds & 1)
712  {
713  // Round keys are pre-splated in forward direction
714  const word32* ptr = reinterpret_cast<const word32*>(subkeys+(rounds-1)*2);
715  const simon128_t rk = (simon128_t)VecLoadAligned(ptr);
716 
717  y1 = VecXor64(VecXor64(y1, SIMON128_f(x1)), rk);
718  y2 = VecXor64(VecXor64(y2, SIMON128_f(x2)), rk);
719  y3 = VecXor64(VecXor64(y3, SIMON128_f(x3)), rk);
720 
721  std::swap(x1, y1); std::swap(x2, y2); std::swap(x3, y3);
722  }
723 
724 #if (CRYPTOPP_BIG_ENDIAN)
725  const uint8x16_p m3 = {31,30,29,28,27,26,25,24, 15,14,13,12,11,10,9,8};
726  const uint8x16_p m4 = {23,22,21,20,19,18,17,16, 7,6,5,4,3,2,1,0};
727 #else
728  const uint8x16_p m3 = {7,6,5,4,3,2,1,0, 23,22,21,20,19,18,17,16};
729  const uint8x16_p m4 = {15,14,13,12,11,10,9,8, 31,30,29,28,27,26,25,24};
730 #endif
731 
732  // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ...
733  block0 = (uint32x4_p)VecPermute(x1, y1, m3);
734  block1 = (uint32x4_p)VecPermute(x1, y1, m4);
735  block2 = (uint32x4_p)VecPermute(x2, y2, m3);
736  block3 = (uint32x4_p)VecPermute(x2, y2, m4);
737  block4 = (uint32x4_p)VecPermute(x3, y3, m3);
738  block5 = (uint32x4_p)VecPermute(x3, y3, m4);
739 }
740 
741 inline void SIMON128_Dec_6_Blocks(uint32x4_p &block0, uint32x4_p &block1,
742  uint32x4_p &block2, uint32x4_p &block3, uint32x4_p &block4,
743  uint32x4_p &block5, const word64 *subkeys, unsigned int rounds)
744 {
745 #if (CRYPTOPP_BIG_ENDIAN)
746  const uint8x16_p m1 = {31,30,29,28,27,26,25,24, 15,14,13,12,11,10,9,8};
747  const uint8x16_p m2 = {23,22,21,20,19,18,17,16, 7,6,5,4,3,2,1,0};
748 #else
749  const uint8x16_p m1 = {7,6,5,4,3,2,1,0, 23,22,21,20,19,18,17,16};
750  const uint8x16_p m2 = {15,14,13,12,11,10,9,8, 31,30,29,28,27,26,25,24};
751 #endif
752 
753  // [A1 A2][B1 B2] ... => [A1 B1][A2 B2] ...
754  simon128_t x1 = (simon128_t)VecPermute(block0, block1, m1);
755  simon128_t y1 = (simon128_t)VecPermute(block0, block1, m2);
756  simon128_t x2 = (simon128_t)VecPermute(block2, block3, m1);
757  simon128_t y2 = (simon128_t)VecPermute(block2, block3, m2);
758  simon128_t x3 = (simon128_t)VecPermute(block4, block5, m1);
759  simon128_t y3 = (simon128_t)VecPermute(block4, block5, m2);
760 
761  if (rounds & 1)
762  {
763  std::swap(x1, y1); std::swap(x2, y2); std::swap(x3, y3);
764 
765  const word32* ptr = reinterpret_cast<const word32*>(subkeys+rounds-1);
766  const simon128_t tk = (simon128_t)VecLoad(ptr);
767  const simon128_t rk = (simon128_t)VecSplatElement64<0>(tk);
768 
769  y1 = VecXor64(VecXor64(y1, rk), SIMON128_f(x1));
770  y2 = VecXor64(VecXor64(y2, rk), SIMON128_f(x2));
771  y3 = VecXor64(VecXor64(y3, rk), SIMON128_f(x3));
772  rounds--;
773  }
774 
775  for (int i = static_cast<int>(rounds-2); i >= 0; i -= 2)
776  {
777  const word32* ptr = reinterpret_cast<const word32*>(subkeys+i);
778  const simon128_t tk = (simon128_t)VecLoad(ptr);
779  const simon128_t rk1 = (simon128_t)VecSplatElement64<1>(tk);
780  const simon128_t rk2 = (simon128_t)VecSplatElement64<0>(tk);
781 
782  x1 = VecXor64(VecXor64(x1, SIMON128_f(y1)), rk1);
783  x2 = VecXor64(VecXor64(x2, SIMON128_f(y2)), rk1);
784  x3 = VecXor64(VecXor64(x3, SIMON128_f(y3)), rk1);
785 
786  y1 = VecXor64(VecXor64(y1, SIMON128_f(x1)), rk2);
787  y2 = VecXor64(VecXor64(y2, SIMON128_f(x2)), rk2);
788  y3 = VecXor64(VecXor64(y3, SIMON128_f(x3)), rk2);
789  }
790 
791 #if (CRYPTOPP_BIG_ENDIAN)
792  const uint8x16_p m3 = {31,30,29,28,27,26,25,24, 15,14,13,12,11,10,9,8};
793  const uint8x16_p m4 = {23,22,21,20,19,18,17,16, 7,6,5,4,3,2,1,0};
794 #else
795  const uint8x16_p m3 = {7,6,5,4,3,2,1,0, 23,22,21,20,19,18,17,16};
796  const uint8x16_p m4 = {15,14,13,12,11,10,9,8, 31,30,29,28,27,26,25,24};
797 #endif
798 
799  // [A1 B1][A2 B2] ... => [A1 A2][B1 B2] ...
800  block0 = (uint32x4_p)VecPermute(x1, y1, m3);
801  block1 = (uint32x4_p)VecPermute(x1, y1, m4);
802  block2 = (uint32x4_p)VecPermute(x2, y2, m3);
803  block3 = (uint32x4_p)VecPermute(x2, y2, m4);
804  block4 = (uint32x4_p)VecPermute(x3, y3, m3);
805  block5 = (uint32x4_p)VecPermute(x3, y3, m4);
806 }
807 
808 #endif // CRYPTOPP_ALTIVEC_AVAILABLE
809 
810 ANONYMOUS_NAMESPACE_END
811 
812 ///////////////////////////////////////////////////////////////////////
813 
814 NAMESPACE_BEGIN(CryptoPP)
815 
816 // *************************** ARM NEON **************************** //
817 
818 #if (CRYPTOPP_ARM_NEON_AVAILABLE)
819 size_t SIMON128_Enc_AdvancedProcessBlocks_NEON(const word64* subKeys, size_t rounds,
820  const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags)
821 {
822  return AdvancedProcessBlocks128_6x2_NEON(SIMON128_Enc_Block, SIMON128_Enc_6_Blocks,
823  subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags);
824 }
825 
826 size_t SIMON128_Dec_AdvancedProcessBlocks_NEON(const word64* subKeys, size_t rounds,
827  const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags)
828 {
829  return AdvancedProcessBlocks128_6x2_NEON(SIMON128_Dec_Block, SIMON128_Dec_6_Blocks,
830  subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags);
831 }
832 #endif // CRYPTOPP_ARM_NEON_AVAILABLE
833 
834 // ***************************** IA-32 ***************************** //
835 
836 #if (CRYPTOPP_SSSE3_AVAILABLE)
837 size_t SIMON128_Enc_AdvancedProcessBlocks_SSSE3(const word64* subKeys, size_t rounds,
838  const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags)
839 {
840  return AdvancedProcessBlocks128_6x2_SSE(SIMON128_Enc_Block, SIMON128_Enc_6_Blocks,
841  subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags);
842 }
843 
844 size_t SIMON128_Dec_AdvancedProcessBlocks_SSSE3(const word64* subKeys, size_t rounds,
845  const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags)
846 {
847  return AdvancedProcessBlocks128_6x2_SSE(SIMON128_Dec_Block, SIMON128_Dec_6_Blocks,
848  subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags);
849 }
850 #endif // CRYPTOPP_SSSE3_AVAILABLE
851 
852 // ***************************** Altivec ***************************** //
853 
854 #if (CRYPTOPP_ALTIVEC_AVAILABLE)
855 size_t SIMON128_Enc_AdvancedProcessBlocks_ALTIVEC(const word64* subKeys, size_t rounds,
856  const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags)
857 {
858  return AdvancedProcessBlocks128_6x1_ALTIVEC(SIMON128_Enc_Block, SIMON128_Enc_6_Blocks,
859  subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags);
860 }
861 
862 size_t SIMON128_Dec_AdvancedProcessBlocks_ALTIVEC(const word64* subKeys, size_t rounds,
863  const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags)
864 {
865  return AdvancedProcessBlocks128_6x1_ALTIVEC(SIMON128_Dec_Block, SIMON128_Dec_6_Blocks,
866  subKeys, rounds, inBlocks, xorBlocks, outBlocks, length, flags);
867 }
868 #endif // CRYPTOPP_ALTIVEC_AVAILABLE
869 
870 NAMESPACE_END
Template for AdvancedProcessBlocks and SIMD processing.
size_t AdvancedProcessBlocks128_6x2_NEON(F2 func2, F6 func6, const W *subKeys, size_t rounds, const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags)
AdvancedProcessBlocks for 2 and 6 blocks.
Definition: adv_simd.h:388
size_t AdvancedProcessBlocks128_6x1_ALTIVEC(F1 func1, F6 func6, const W *subKeys, size_t rounds, const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags)
AdvancedProcessBlocks for 1 and 6 blocks.
Definition: adv_simd.h:1116
size_t AdvancedProcessBlocks128_6x2_SSE(F2 func2, F6 func6, const W *subKeys, size_t rounds, const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags)
AdvancedProcessBlocks for 2 and 6 blocks.
Definition: adv_simd.h:635
#define CONST_M128_CAST(x)
Clang workaround.
Definition: adv_simd.h:614
Library configuration file.
unsigned char byte
8-bit unsigned datatype
Definition: config_int.h:56
unsigned int word32
32-bit unsigned datatype
Definition: config_int.h:62
unsigned long long word64
64-bit unsigned datatype
Definition: config_int.h:91
Utility functions for the Crypto++ library.
void vec_swap(T &a, T &b)
Swaps two variables which are arrays.
Definition: misc.h:596
Crypto++ library namespace.
Precompiled header file.
Support functions for PowerPC and vector operations.
T1 VecOr64(const T1 vec1, const T2 vec2)
OR two vectors as if uint64x2_p.
Definition: ppc_simd.h:2362
uint32x4_p VecLoadAligned(const byte src[16])
Loads a vector from an aligned byte array.
Definition: ppc_simd.h:560
__vector unsigned int uint32x4_p
Vector of 32-bit elements.
Definition: ppc_simd.h:202
uint32x4_p VecSub64(const uint32x4_p &vec1, const uint32x4_p &vec2)
Subtract two vectors as if uint64x2_p.
Definition: ppc_simd.h:2077
T1 VecPermute(const T1 vec, const T2 mask)
Permutes a vector.
Definition: ppc_simd.h:1478
__vector unsigned char uint8x16_p
Vector of 8-bit elements.
Definition: ppc_simd.h:192
__vector unsigned long long uint64x2_p
Vector of 64-bit elements.
Definition: ppc_simd.h:212
uint32x4_p VecSplatElement64(const uint32x4_p val)
Broadcast 64-bit element to a vector as if uint64x2_p.
Definition: ppc_simd.h:2411
T1 VecXor64(const T1 vec1, const T2 vec2)
XOR two vectors as if uint64x2_p.
Definition: ppc_simd.h:2381
uint32x4_p VecRotateRight64(const uint32x4_p vec)
Rotate a vector right as if uint64x2_p.
Definition: ppc_simd.h:2240
uint32x4_p VecAdd64(const uint32x4_p &vec1, const uint32x4_p &vec2)
Add two vectors as if uint64x2_p.
Definition: ppc_simd.h:2014
uint32x4_p VecLoad(const byte src[16])
Loads a vector from a byte array.
Definition: ppc_simd.h:369
uint32x4_p VecRotateLeft64(const uint32x4_p vec)
Rotate a vector left as if uint64x2_p.
Definition: ppc_simd.h:2142
uint32x4_p VecRotateLeft64< 8 >(const uint32x4_p vec)
Rotate a vector left as if uint64x2_p.
Definition: ppc_simd.h:2191
T1 VecAnd64(const T1 vec1, const T2 vec2)
AND two vectors as if uint64x2_p.
Definition: ppc_simd.h:2343
void swap(::SecBlock< T, A > &a, ::SecBlock< T, A > &b)
Swap two SecBlocks.
Definition: secblock.h:1177
Classes for the Simon block cipher.