Botan  2.6.0
Crypto and TLS for C++11
clmul_ssse3.cpp
Go to the documentation of this file.
1 /*
2 * (C) 2017 Jack Lloyd
3 *
4 * Botan is released under the Simplified BSD License (see license.txt)
5 */
6 
7 #include <botan/internal/clmul_ssse3.h>
8 #include <immintrin.h>
9 
10 namespace Botan {
11 
12 BOTAN_FUNC_ISA("ssse3")
13 void gcm_multiply_ssse3(uint8_t x[16],
14  const uint64_t HM[256],
15  const uint8_t input_bytes[], size_t blocks)
16  {
17  const __m128i BSWAP_MASK = _mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
18 
19  const __m128i* HM_mm = reinterpret_cast<const __m128i*>(HM);
20 
21  __m128i X = _mm_loadu_si128(reinterpret_cast<__m128i*>(x));
22  X = _mm_shuffle_epi8(X, BSWAP_MASK);
23 
24  const __m128i ones = _mm_set1_epi8(-1);
25 
26  for(size_t b = 0; b != blocks; ++b)
27  {
28  __m128i M = _mm_loadu_si128(reinterpret_cast<const __m128i*>(input_bytes) + b);
29  M = _mm_shuffle_epi8(M, BSWAP_MASK);
30 
31  X = _mm_xor_si128(X, M);
32 
33  __m128i Z = _mm_setzero_si128();
34 
35  for(size_t i = 0; i != 64; i += 2)
36  {
37  const __m128i HM0 = _mm_loadu_si128(HM_mm + 2*i);
38  const __m128i HM1 = _mm_loadu_si128(HM_mm + 2*i + 1);
39  const __m128i HM2 = _mm_loadu_si128(HM_mm + 2*i + 2);
40  const __m128i HM3 = _mm_loadu_si128(HM_mm + 2*i + 3);
41 
42  const __m128i XMASK1 = _mm_add_epi64(_mm_srli_epi64(X, 63), ones);
43  X = _mm_slli_epi64(X, 1);
44  const __m128i XMASK2 = _mm_add_epi64(_mm_srli_epi64(X, 63), ones);
45  X = _mm_slli_epi64(X, 1);
46 
47  Z = _mm_xor_si128(Z, _mm_andnot_si128(_mm_unpackhi_epi64(XMASK1, XMASK1), HM0));
48  Z = _mm_xor_si128(Z, _mm_andnot_si128(_mm_unpacklo_epi64(XMASK1, XMASK1), HM1));
49  Z = _mm_xor_si128(Z, _mm_andnot_si128(_mm_unpackhi_epi64(XMASK2, XMASK2), HM2));
50  Z = _mm_xor_si128(Z, _mm_andnot_si128(_mm_unpacklo_epi64(XMASK2, XMASK2), HM3));
51  }
52 
53  X = _mm_shuffle_epi32(Z, _MM_SHUFFLE(1, 0, 3, 2));
54  }
55 
56  X = _mm_shuffle_epi8(X, BSWAP_MASK);
57  _mm_storeu_si128(reinterpret_cast<__m128i*>(x), X);
58  }
59 
60 }
fe X
Definition: ge.cpp:27
void gcm_multiply_ssse3(uint8_t x[16], const uint64_t HM[256], const uint8_t input_bytes[], size_t blocks)
Definition: clmul_ssse3.cpp:13
#define BOTAN_FUNC_ISA(isa)
Definition: compiler.h:75
Definition: alg_id.cpp:13
fe Z
Definition: ge.cpp:29