/src/BearSSL/src/symcipher/aes_x86ni.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org> |
3 | | * |
4 | | * Permission is hereby granted, free of charge, to any person obtaining |
5 | | * a copy of this software and associated documentation files (the |
6 | | * "Software"), to deal in the Software without restriction, including |
7 | | * without limitation the rights to use, copy, modify, merge, publish, |
8 | | * distribute, sublicense, and/or sell copies of the Software, and to |
9 | | * permit persons to whom the Software is furnished to do so, subject to |
10 | | * the following conditions: |
11 | | * |
12 | | * The above copyright notice and this permission notice shall be |
13 | | * included in all copies or substantial portions of the Software. |
14 | | * |
15 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
16 | | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
17 | | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
18 | | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
19 | | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
20 | | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
21 | | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
22 | | * SOFTWARE. |
23 | | */ |
24 | | |
25 | | #define BR_ENABLE_INTRINSICS 1 |
26 | | #include "inner.h" |
27 | | |
28 | | /* |
29 | | * This code contains the AES key schedule implementation using the |
30 | | * AES-NI opcodes. |
31 | | */ |
32 | | |
33 | | #if BR_AES_X86NI |
34 | | |
35 | | /* see inner.h */ |
36 | | int |
37 | | br_aes_x86ni_supported(void) |
38 | 533 | { |
39 | | /* |
40 | | * Bit mask for features in ECX: |
41 | | * 19 SSE4.1 (used for _mm_insert_epi32(), for AES-CTR) |
42 | | * 25 AES-NI |
43 | | */ |
44 | 533 | return br_cpuid(0, 0, 0x02080000, 0); |
45 | 533 | } |
46 | | |
47 | | BR_TARGETS_X86_UP |
48 | | |
49 | | BR_TARGET("sse2,aes") |
50 | | static inline __m128i |
51 | | expand_step128(__m128i k, __m128i k2) |
52 | 1.41k | { |
53 | 1.41k | k = _mm_xor_si128(k, _mm_slli_si128(k, 4)); |
54 | 1.41k | k = _mm_xor_si128(k, _mm_slli_si128(k, 4)); |
55 | 1.41k | k = _mm_xor_si128(k, _mm_slli_si128(k, 4)); |
56 | 1.41k | k2 = _mm_shuffle_epi32(k2, 0xFF); |
57 | 1.41k | return _mm_xor_si128(k, k2); |
58 | 1.41k | } |
59 | | |
60 | | BR_TARGET("sse2,aes") |
61 | | static inline void |
62 | | expand_step192(__m128i *t1, __m128i *t2, __m128i *t3) |
63 | 1.40k | { |
64 | 1.40k | __m128i t4; |
65 | | |
66 | 1.40k | *t2 = _mm_shuffle_epi32(*t2, 0x55); |
67 | 1.40k | t4 = _mm_slli_si128(*t1, 0x4); |
68 | 1.40k | *t1 = _mm_xor_si128(*t1, t4); |
69 | 1.40k | t4 = _mm_slli_si128(t4, 0x4); |
70 | 1.40k | *t1 = _mm_xor_si128(*t1, t4); |
71 | 1.40k | t4 = _mm_slli_si128(t4, 0x4); |
72 | 1.40k | *t1 = _mm_xor_si128(*t1, t4); |
73 | 1.40k | *t1 = _mm_xor_si128(*t1, *t2); |
74 | 1.40k | *t2 = _mm_shuffle_epi32(*t1, 0xFF); |
75 | 1.40k | t4 = _mm_slli_si128(*t3, 0x4); |
76 | 1.40k | *t3 = _mm_xor_si128(*t3, t4); |
77 | 1.40k | *t3 = _mm_xor_si128(*t3, *t2); |
78 | 1.40k | } |
79 | | |
80 | | BR_TARGET("sse2,aes") |
81 | | static inline void |
82 | | expand_step256_1(__m128i *t1, __m128i *t2) |
83 | 1.47k | { |
84 | 1.47k | __m128i t4; |
85 | | |
86 | 1.47k | *t2 = _mm_shuffle_epi32(*t2, 0xFF); |
87 | 1.47k | t4 = _mm_slli_si128(*t1, 0x4); |
88 | 1.47k | *t1 = _mm_xor_si128(*t1, t4); |
89 | 1.47k | t4 = _mm_slli_si128(t4, 0x4); |
90 | 1.47k | *t1 = _mm_xor_si128(*t1, t4); |
91 | 1.47k | t4 = _mm_slli_si128(t4, 0x4); |
92 | 1.47k | *t1 = _mm_xor_si128(*t1, t4); |
93 | 1.47k | *t1 = _mm_xor_si128(*t1, *t2); |
94 | 1.47k | } |
95 | | |
96 | | BR_TARGET("sse2,aes") |
97 | | static inline void |
98 | | expand_step256_2(__m128i *t1, __m128i *t3) |
99 | 1.26k | { |
100 | 1.26k | __m128i t2, t4; |
101 | | |
102 | 1.26k | t4 = _mm_aeskeygenassist_si128(*t1, 0x0); |
103 | 1.26k | t2 = _mm_shuffle_epi32(t4, 0xAA); |
104 | 1.26k | t4 = _mm_slli_si128(*t3, 0x4); |
105 | 1.26k | *t3 = _mm_xor_si128(*t3, t4); |
106 | 1.26k | t4 = _mm_slli_si128(t4, 0x4); |
107 | 1.26k | *t3 = _mm_xor_si128(*t3, t4); |
108 | 1.26k | t4 = _mm_slli_si128(t4, 0x4); |
109 | 1.26k | *t3 = _mm_xor_si128(*t3, t4); |
110 | 1.26k | *t3 = _mm_xor_si128(*t3, t2); |
111 | 1.26k | } |
112 | | |
113 | | /* |
114 | | * Perform key schedule for AES, encryption direction. Subkeys are written |
115 | | * in sk[], and the number of rounds is returned. Key length MUST be 16, |
116 | | * 24 or 32 bytes. |
117 | | */ |
118 | | BR_TARGET("sse2,aes") |
119 | | static unsigned |
120 | | x86ni_keysched(__m128i *sk, const void *key, size_t len) |
121 | 527 | { |
122 | 527 | const unsigned char *kb; |
123 | | |
124 | 1.41k | #define KEXP128(k, i, rcon) do { \ |
125 | 1.41k | k = expand_step128(k, _mm_aeskeygenassist_si128(k, rcon)); \ |
126 | 1.41k | sk[i] = k; \ |
127 | 1.41k | } while (0) |
128 | | |
129 | 700 | #define KEXP192(i, rcon1, rcon2) do { \ |
130 | 700 | sk[(i) + 0] = t1; \ |
131 | 700 | sk[(i) + 1] = t3; \ |
132 | 700 | t2 = _mm_aeskeygenassist_si128(t3, rcon1); \ |
133 | 700 | expand_step192(&t1, &t2, &t3); \ |
134 | 700 | sk[(i) + 1] = _mm_castpd_si128(_mm_shuffle_pd( \ |
135 | 700 | _mm_castsi128_pd(sk[(i) + 1]), \ |
136 | 700 | _mm_castsi128_pd(t1), 0)); \ |
137 | 700 | sk[(i) + 2] = _mm_castpd_si128(_mm_shuffle_pd( \ |
138 | 700 | _mm_castsi128_pd(t1), \ |
139 | 700 | _mm_castsi128_pd(t3), 1)); \ |
140 | 700 | t2 = _mm_aeskeygenassist_si128(t3, rcon2); \ |
141 | 700 | expand_step192(&t1, &t2, &t3); \ |
142 | 700 | } while (0) |
143 | | |
144 | 1.26k | #define KEXP256(i, rcon) do { \ |
145 | 1.26k | sk[(i) + 0] = t3; \ |
146 | 1.26k | t2 = _mm_aeskeygenassist_si128(t3, rcon); \ |
147 | 1.26k | expand_step256_1(&t1, &t2); \ |
148 | 1.26k | sk[(i) + 1] = t1; \ |
149 | 1.26k | expand_step256_2(&t1, &t3); \ |
150 | 1.26k | } while (0) |
151 | | |
152 | 527 | kb = key; |
153 | 527 | switch (len) { |
154 | 0 | __m128i t1, t2, t3; |
155 | | |
156 | 141 | case 16: |
157 | 141 | t1 = _mm_loadu_si128((const void *)kb); |
158 | 141 | sk[0] = t1; |
159 | 141 | KEXP128(t1, 1, 0x01); |
160 | 141 | KEXP128(t1, 2, 0x02); |
161 | 141 | KEXP128(t1, 3, 0x04); |
162 | 141 | KEXP128(t1, 4, 0x08); |
163 | 141 | KEXP128(t1, 5, 0x10); |
164 | 141 | KEXP128(t1, 6, 0x20); |
165 | 141 | KEXP128(t1, 7, 0x40); |
166 | 141 | KEXP128(t1, 8, 0x80); |
167 | 141 | KEXP128(t1, 9, 0x1B); |
168 | 141 | KEXP128(t1, 10, 0x36); |
169 | 141 | return 10; |
170 | | |
171 | 175 | case 24: |
172 | 175 | t1 = _mm_loadu_si128((const void *)kb); |
173 | 175 | t3 = _mm_loadu_si128((const void *)(kb + 8)); |
174 | 175 | t3 = _mm_shuffle_epi32(t3, 0x4E); |
175 | 175 | KEXP192(0, 0x01, 0x02); |
176 | 175 | KEXP192(3, 0x04, 0x08); |
177 | 175 | KEXP192(6, 0x10, 0x20); |
178 | 175 | KEXP192(9, 0x40, 0x80); |
179 | 175 | sk[12] = t1; |
180 | 175 | return 12; |
181 | | |
182 | 211 | case 32: |
183 | 211 | t1 = _mm_loadu_si128((const void *)kb); |
184 | 211 | t3 = _mm_loadu_si128((const void *)(kb + 16)); |
185 | 211 | sk[0] = t1; |
186 | 211 | KEXP256( 1, 0x01); |
187 | 211 | KEXP256( 3, 0x02); |
188 | 211 | KEXP256( 5, 0x04); |
189 | 211 | KEXP256( 7, 0x08); |
190 | 211 | KEXP256( 9, 0x10); |
191 | 211 | KEXP256(11, 0x20); |
192 | 211 | sk[13] = t3; |
193 | 211 | t2 = _mm_aeskeygenassist_si128(t3, 0x40); |
194 | 211 | expand_step256_1(&t1, &t2); |
195 | 211 | sk[14] = t1; |
196 | 211 | return 14; |
197 | | |
198 | 0 | default: |
199 | 0 | return 0; |
200 | 527 | } |
201 | | |
202 | 527 | #undef KEXP128 |
203 | 527 | #undef KEXP192 |
204 | 527 | #undef KEXP256 |
205 | 527 | } |
206 | | |
207 | | /* see inner.h */ |
208 | | BR_TARGET("sse2,aes") |
209 | | unsigned |
210 | | br_aes_x86ni_keysched_enc(unsigned char *skni, const void *key, size_t len) |
211 | 527 | { |
212 | 527 | __m128i sk[15]; |
213 | 527 | unsigned num_rounds; |
214 | | |
215 | 527 | num_rounds = x86ni_keysched(sk, key, len); |
216 | 527 | memcpy(skni, sk, (num_rounds + 1) << 4); |
217 | 527 | return num_rounds; |
218 | 527 | } |
219 | | |
220 | | /* see inner.h */ |
221 | | BR_TARGET("sse2,aes") |
222 | | unsigned |
223 | | br_aes_x86ni_keysched_dec(unsigned char *skni, const void *key, size_t len) |
224 | 0 | { |
225 | 0 | __m128i sk[15]; |
226 | 0 | unsigned u, num_rounds; |
227 | |
|
228 | 0 | num_rounds = x86ni_keysched(sk, key, len); |
229 | 0 | _mm_storeu_si128((void *)skni, sk[num_rounds]); |
230 | 0 | for (u = 1; u < num_rounds; u ++) { |
231 | 0 | _mm_storeu_si128((void *)(skni + (u << 4)), |
232 | 0 | _mm_aesimc_si128(sk[num_rounds - u])); |
233 | 0 | } |
234 | 0 | _mm_storeu_si128((void *)(skni + (num_rounds << 4)), sk[0]); |
235 | 0 | return num_rounds; |
236 | 0 | } |
237 | | |
238 | | BR_TARGETS_X86_DOWN |
239 | | |
240 | | #endif |