Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

clmul.cpp
Go to the documentation of this file.
1 /*
2 * CLMUL hook
3 * (C) 2013 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7
8 #include <botan/internal/clmul.h>
9 #include <immintrin.h>
10 #include <wmmintrin.h>
11
12 namespace Botan {
13
14 BOTAN_FUNC_ISA("pclmul,ssse3")
15 void gcm_multiply_clmul(uint8_t x[16], const uint8_t H[16])
16 {
17 /*
18 * Algorithms 1 and 5 from Intel's CLMUL guide
19 */
20 const __m128i BSWAP_MASK = _mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
21
22 __m128i a = _mm_loadu_si128(reinterpret_cast<const __m128i*>(x));
23 __m128i b = _mm_loadu_si128(reinterpret_cast<const __m128i*>(H));
24
25 a = _mm_shuffle_epi8(a, BSWAP_MASK);
26 b = _mm_shuffle_epi8(b, BSWAP_MASK);
27
28 __m128i T0, T1, T2, T3, T4, T5;
29
30 T0 = _mm_clmulepi64_si128(a, b, 0x00);
31 T1 = _mm_clmulepi64_si128(a, b, 0x01);
32 T2 = _mm_clmulepi64_si128(a, b, 0x10);
33 T3 = _mm_clmulepi64_si128(a, b, 0x11);
34
35 T1 = _mm_xor_si128(T1, T2);
36 T2 = _mm_slli_si128(T1, 8);
37 T1 = _mm_srli_si128(T1, 8);
38 T0 = _mm_xor_si128(T0, T2);
39 T3 = _mm_xor_si128(T3, T1);
40
41 T4 = _mm_srli_epi32(T0, 31);
42 T0 = _mm_slli_epi32(T0, 1);
43
44 T5 = _mm_srli_epi32(T3, 31);
45 T3 = _mm_slli_epi32(T3, 1);
46
47 T2 = _mm_srli_si128(T4, 12);
48 T5 = _mm_slli_si128(T5, 4);
49 T4 = _mm_slli_si128(T4, 4);
50 T0 = _mm_or_si128(T0, T4);
51 T3 = _mm_or_si128(T3, T5);
52 T3 = _mm_or_si128(T3, T2);
53
54 T4 = _mm_slli_epi32(T0, 31);
55 T5 = _mm_slli_epi32(T0, 30);
56 T2 = _mm_slli_epi32(T0, 25);
57
58 T4 = _mm_xor_si128(T4, T5);
59 T4 = _mm_xor_si128(T4, T2);
60 T5 = _mm_srli_si128(T4, 4);
61 T3 = _mm_xor_si128(T3, T5);
62 T4 = _mm_slli_si128(T4, 12);
63 T0 = _mm_xor_si128(T0, T4);
64 T3 = _mm_xor_si128(T3, T0);
65
66 T4 = _mm_srli_epi32(T0, 1);
67 T1 = _mm_srli_epi32(T0, 2);
68 T2 = _mm_srli_epi32(T0, 7);
69 T3 = _mm_xor_si128(T3, T1);
70 T3 = _mm_xor_si128(T3, T2);
71 T3 = _mm_xor_si128(T3, T4);
72
73 T3 = _mm_shuffle_epi8(T3, BSWAP_MASK);
74
75 _mm_storeu_si128(reinterpret_cast<__m128i*>(x), T3);
76 }
77
78 }
Botan::gcm_multiply_clmul
void gcm_multiply_clmul(uint8_t x[16], const uint8_t H[16])
Definition: clmul.cpp:15
BOTAN_FUNC_ISA
#define BOTAN_FUNC_ISA(isa)
Definition: compiler.h:48
Botan
Definition: alg_id.cpp:13