blob: 3acc3297128d8e286b499da5ebb16ed74d1e1026 [file] [log] [blame]
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +01001/*
2 * AES-NI support functions
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010018 */
19
20/*
Gilles Peskined4f31c82023-03-10 22:21:47 +010021 * [AES-WP] https://www.intel.com/content/www/us/en/developer/articles/tool/intel-advanced-encryption-standard-aes-instructions-set.html
22 * [CLMUL-WP] https://www.intel.com/content/www/us/en/develop/download/intel-carry-less-multiplication-instruction-and-its-usage-for-computing-the-gcm-mode.html
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010023 */
24
Gilles Peskinedb09ef62020-06-03 01:43:33 +020025#include "common.h"
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if defined(MBEDTLS_AESNI_C)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010028
Chris Jones187782f2021-03-09 17:28:35 +000029#include "aesni.h"
Rich Evans00ab4702015-02-06 13:43:58 +000030
31#include <string.h>
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010032
Gilles Peskined6719172023-03-10 22:37:11 +010033#if defined(MBEDTLS_HAVE_AESNI_INTRINSICS) || defined(MBEDTLS_HAVE_X86_64)
34
35#if defined(MBEDTLS_HAVE_AESNI_INTRINSICS)
36#include <cpuid.h>
37#include <immintrin.h>
38#endif
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010039
40/*
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010041 * AES-NI support detection routine
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010042 */
Gilles Peskine449bd832023-01-11 14:50:10 +010043int mbedtls_aesni_has_support(unsigned int what)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010044{
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010045 static int done = 0;
46 static unsigned int c = 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010047
Gilles Peskine449bd832023-01-11 14:50:10 +010048 if (!done) {
Gilles Peskined6719172023-03-10 22:37:11 +010049#if defined(MBEDTLS_HAVE_AESNI_INTRINSICS)
50 static unsigned info[4] = { 0, 0, 0, 0 };
51#if defined(_MSC_VER)
52 __cpuid(info, 1);
53#else
54 __cpuid(1, info[0], info[1], info[2], info[3]);
55#endif
56 c = info[2];
57#else
Gilles Peskine449bd832023-01-11 14:50:10 +010058 asm ("movl $1, %%eax \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +020059 "cpuid \n\t"
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010060 : "=c" (c)
61 :
Gilles Peskine449bd832023-01-11 14:50:10 +010062 : "eax", "ebx", "edx");
Gilles Peskined6719172023-03-10 22:37:11 +010063#endif
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010064 done = 1;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010065 }
66
Gilles Peskine449bd832023-01-11 14:50:10 +010067 return (c & what) != 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010068}
69
Gilles Peskined6719172023-03-10 22:37:11 +010070#if defined(MBEDTLS_HAVE_AESNI_INTRINSICS)
71
72/*
73 * AES-NI AES-ECB block en(de)cryption
74 */
75int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
76 int mode,
77 const unsigned char input[16],
78 unsigned char output[16])
79{
80 const __m128i *rk = (const __m128i *) (ctx->buf + ctx->rk_offset);
81 unsigned nr = ctx->nr; // Number of remaining rounds
82 // Load round key 0
83 __m128i xmm0;
84 memcpy(&xmm0, input, 16);
85 xmm0 ^= *rk;
86 ++rk;
87 --nr;
88
89 if (mode == 0) {
90 while (nr != 0) {
91 xmm0 = _mm_aesdec_si128(xmm0, *rk);
92 ++rk;
93 --nr;
94 }
95 xmm0 = _mm_aesdeclast_si128(xmm0, *rk);
96 } else {
97 while (nr != 0) {
98 xmm0 = _mm_aesenc_si128(xmm0, *rk);
99 ++rk;
100 --nr;
101 }
102 xmm0 = _mm_aesenclast_si128(xmm0, *rk);
103 }
104
105 memcpy(output, &xmm0, 16);
106 return 0;
107}
108
109/*
110 * GCM multiplication: c = a times b in GF(2^128)
111 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
112 */
113
114static void gcm_clmul(const __m128i aa, const __m128i bb,
115 __m128i *cc, __m128i *dd)
116{
117 /*
118 * Caryless multiplication dd:cc = aa * bb
119 * using [CLMUL-WP] algorithm 1 (p. 12).
120 */
121 *cc = _mm_clmulepi64_si128(aa, bb, 0x00); // a0*b0 = c1:c0
122 *dd = _mm_clmulepi64_si128(aa, bb, 0x11); // a1*b1 = d1:d0
123 __m128i ee = _mm_clmulepi64_si128(aa, bb, 0x10); // a0*b1 = e1:e0
124 __m128i ff = _mm_clmulepi64_si128(aa, bb, 0x01); // a1*b0 = f1:f0
125 ff ^= ee; // e1+f1:e0+f0
126 ee = ff; // e1+f1:e0+f0
127 ff = _mm_srli_si128(ff, 8); // 0:e1+f1
128 ee = _mm_slli_si128(ee, 8); // e0+f0:0
129 *dd ^= ff; // d1:d0+e1+f1
130 *cc ^= ee; // c1+e0+f1:c0
131}
132
133static void gcm_shift(__m128i *cc, __m128i *dd)
134{
135 /*
136 * Now shift the result one bit to the left,
137 * taking advantage of [CLMUL-WP] eq 27 (p. 18)
138 */
139 // // *cc = r1:r0
140 // // *dd = r3:r2
141 __m128i xmm1 = _mm_slli_epi64(*cc, 1); // r1<<1:r0<<1
142 __m128i xmm2 = _mm_slli_epi64(*dd, 1); // r3<<1:r2<<1
143 __m128i xmm3 = _mm_srli_epi64(*cc, 63); // r1>>63:r0>>63
144 __m128i xmm4 = _mm_srli_epi64(*dd, 63); // r3>>63:r2>>63
145 __m128i xmm5 = _mm_srli_si128(xmm3, 8); // 0:r1>>63
146 xmm3 = _mm_slli_si128(xmm3, 8); // r0>>63:0
147 xmm4 = _mm_slli_si128(xmm4, 8); // 0:r1>>63
148
149 *cc = xmm1 | xmm3; // r1<<1|r0>>63:r0<<1
150 *dd = xmm2 | xmm4 | xmm5; // r3<<1|r2>>62:r2<<1|r1>>63
151}
152
153static __m128i gcm_reduce1(__m128i xx)
154{
155 // // xx = x1:x0
156 /* [CLMUL-WP] Algorithm 5 Step 2 */
157 __m128i aa = _mm_slli_epi64(xx, 63); // x1<<63:x0<<63 = stuff:a
158 __m128i bb = _mm_slli_epi64(xx, 62); // x1<<62:x0<<62 = stuff:b
159 __m128i cc = _mm_slli_epi64(xx, 57); // x1<<57:x0<<57 = stuff:c
160 __m128i dd = _mm_slli_si128(aa ^ bb ^ cc, 8); // a+b+c:0
161 return dd ^ xx; // x1+a+b+c:x0 = d:x0
162}
163
164static __m128i gcm_reduce2(__m128i dx)
165{
166 /* [CLMUL-WP] Algorithm 5 Steps 3 and 4 */
167 __m128i ee = _mm_srli_epi64(dx, 1); // e1:x0>>1 = e1:e0'
168 __m128i ff = _mm_srli_epi64(dx, 2); // f1:x0>>2 = f1:f0'
169 __m128i gg = _mm_srli_epi64(dx, 7); // g1:x0>>7 = g1:g0'
170
171 // e0'+f0'+g0' is almost e0+f0+g0, except for some missing
172 // bits carried from d. Now get those bits back in.
173 __m128i eh = _mm_slli_epi64(dx, 63); // d<<63:stuff
174 __m128i fh = _mm_slli_epi64(dx, 62); // d<<62:stuff
175 __m128i gh = _mm_slli_epi64(dx, 57); // d<<57:stuff
176 __m128i hh = _mm_srli_si128(eh ^ fh ^ gh, 8); // 0:missing bits of d
177
178 return ee ^ ff ^ gg ^ hh ^ dx;
179}
180
181void mbedtls_aesni_gcm_mult(unsigned char c[16],
182 const unsigned char a[16],
183 const unsigned char b[16])
184{
185 __m128i aa, bb, cc, dd;
186
187 /* The inputs are in big-endian order, so byte-reverse them */
188 for (size_t i = 0; i < 16; i++) {
189 ((uint8_t *) &aa)[i] = a[15 - i];
190 ((uint8_t *) &bb)[i] = b[15 - i];
191 }
192
193 gcm_clmul(aa, bb, &cc, &dd);
194 gcm_shift(&cc, &dd);
195 /*
196 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
197 * using [CLMUL-WP] algorithm 5 (p. 18).
198 * Currently dd:cc holds x3:x2:x1:x0 (already shifted).
199 */
200 __m128i dx = gcm_reduce1(cc);
201 __m128i xh = gcm_reduce2(dx);
202 cc = xh ^ dd; // x3+h1:x2+h0
203
204 /* Now byte-reverse the outputs */
205 for (size_t i = 0; i < 16; i++) {
206 c[i] = ((uint8_t *) &cc)[15 - i];
207 }
208
209 return;
210}
211
212/*
213 * Compute decryption round keys from encryption round keys
214 */
215void mbedtls_aesni_inverse_key(unsigned char *invkey,
216 const unsigned char *fwdkey, int nr)
217{
218 __m128i *ik = (__m128i *) invkey;
219 const __m128i *fk = (const __m128i *) fwdkey + nr;
220
221 *ik = *fk;
222 for (--fk, ++ik; fk > (const __m128i *) fwdkey; --fk, ++ik) {
223 *ik = _mm_aesimc_si128(*fk);
224 }
225 *ik = *fk;
226}
227
228/*
229 * Key expansion, 128-bit case
230 */
231static __m128i aesni_set_rk_128(__m128i xmm0, __m128i xmm1)
232{
233 /*
234 * Finish generating the next round key.
235 *
236 * On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff
237 * with X = rot( sub( r3 ) ) ^ RCON.
238 *
239 * On exit, xmm1 is r7:r6:r5:r4
240 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
241 * and this is returned, to be written to the round key buffer.
242 */
243 xmm1 = _mm_shuffle_epi32(xmm1, 0xff); // X:X:X:X
244 xmm1 ^= xmm0; // X+r3:X+r2:X+r1:r4
245 xmm0 = _mm_slli_si128(xmm0, 4); // r2:r1:r0:0
246 xmm1 ^= xmm0; // X+r3+r2:X+r2+r1:r5:r4
247 xmm0 = _mm_slli_si128(xmm0, 4); // r1:r0:0:0
248 xmm1 ^= xmm0; // X+r3+r2+r1:r6:r5:r4
249 xmm0 = _mm_slli_si128(xmm0, 4); // r0:0:0:0
250 xmm1 ^= xmm0; // r7:r6:r5:r4
251 return xmm1;
252}
253
254static void aesni_setkey_enc_128(unsigned char *rk_bytes,
255 const unsigned char *key)
256{
257 __m128i *rk = (__m128i *) rk_bytes;
258
259 memcpy(&rk[0], key, 16);
260 rk[1] = aesni_set_rk_128(rk[0], _mm_aeskeygenassist_si128(rk[0], 0x01));
261 rk[2] = aesni_set_rk_128(rk[1], _mm_aeskeygenassist_si128(rk[1], 0x02));
262 rk[3] = aesni_set_rk_128(rk[2], _mm_aeskeygenassist_si128(rk[2], 0x04));
263 rk[4] = aesni_set_rk_128(rk[3], _mm_aeskeygenassist_si128(rk[3], 0x08));
264 rk[5] = aesni_set_rk_128(rk[4], _mm_aeskeygenassist_si128(rk[4], 0x10));
265 rk[6] = aesni_set_rk_128(rk[5], _mm_aeskeygenassist_si128(rk[5], 0x20));
266 rk[7] = aesni_set_rk_128(rk[6], _mm_aeskeygenassist_si128(rk[6], 0x40));
267 rk[8] = aesni_set_rk_128(rk[7], _mm_aeskeygenassist_si128(rk[7], 0x80));
268 rk[9] = aesni_set_rk_128(rk[8], _mm_aeskeygenassist_si128(rk[8], 0x1B));
269 rk[10] = aesni_set_rk_128(rk[9], _mm_aeskeygenassist_si128(rk[9], 0x36));
270}
271
272/*
273 * Key expansion, 192-bit case
274 */
275static void aesni_set_rk_192(__m128i *xmm0, __m128i *xmm1, __m128i xmm2,
276 unsigned char *rk)
277{
278 /*
279 * Finish generating the next 6 quarter-keys.
280 *
281 * On entry xmm0 is r3:r2:r1:r0, xmm1 is stuff:stuff:r5:r4
282 * and xmm2 is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON.
283 *
284 * On exit, xmm0 is r9:r8:r7:r6 and xmm1 is stuff:stuff:r11:r10
285 * and those are written to the round key buffer.
286 */
287 xmm2 = _mm_shuffle_epi32(xmm2, 0x55); // X:X:X:X
288 xmm2 = _mm_xor_si128(xmm2, *xmm0); // X+r3:X+r2:X+r1:X+r0
289 *xmm0 = _mm_slli_si128(*xmm0, 4); // r2:r1:r0:0
290 xmm2 = _mm_xor_si128(xmm2, *xmm0); // X+r3+r2:X+r2+r1:X+r1+r0:X+r0
291 *xmm0 = _mm_slli_si128(*xmm0, 4); // r1:r0:0:0
292 xmm2 = _mm_xor_si128(xmm2, *xmm0); // X+r3+r2+r1:X+r2+r1+r0:X+r1+r0:X+r0
293 *xmm0 = _mm_slli_si128(*xmm0, 4); // r0:0:0:0
294 xmm2 = _mm_xor_si128(xmm2, *xmm0); // X+r3+r2+r1+r0:X+r2+r1+r0:X+r1+r0:X+r0
295 *xmm0 = xmm2; // = r9:r8:r7:r6
296
297 xmm2 = _mm_shuffle_epi32(xmm2, 0xff); // r9:r9:r9:r9
298 xmm2 = _mm_xor_si128(xmm2, *xmm1); // stuff:stuff:r9+r5:r9+r4
299 *xmm1 = _mm_slli_si128(*xmm1, 4); // stuff:stuff:r4:0
300 xmm2 = _mm_xor_si128(xmm2, *xmm1); // stuff:stuff:r9+r5+r4:r9+r4
301 *xmm1 = xmm2; // = stuff:stuff:r11:r10
302
303 /* Store xmm0 and the low half of xmm1 into rk, which is conceptually
304 * an array of 24-byte elements. Since 24 is not a multiple of 16,
305 * rk is not necessarily aligned so just `*rk = *xmm0` doesn't work. */
306 memcpy(rk, xmm0, 16);
307 _mm_storeu_si64(rk + 16, *xmm1);
308}
309
310static void aesni_setkey_enc_192(unsigned char *rk,
311 const unsigned char *key)
312{
313 /* First round: use original key */
314 memcpy(rk, key, 24);
315 /* aes.c guarantees that rk is aligned on a 16-byte boundary. */
316 __m128i xmm0 = ((__m128i *) rk)[0];
317 __m128i xmm1 = _mm_loadl_epi64(((__m128i *) rk) + 1);
318
319 aesni_set_rk_192(&xmm0, &xmm1, _mm_aeskeygenassist_si128(xmm1, 0x01), rk + 24 * 1);
320 aesni_set_rk_192(&xmm0, &xmm1, _mm_aeskeygenassist_si128(xmm1, 0x02), rk + 24 * 2);
321 aesni_set_rk_192(&xmm0, &xmm1, _mm_aeskeygenassist_si128(xmm1, 0x04), rk + 24 * 3);
322 aesni_set_rk_192(&xmm0, &xmm1, _mm_aeskeygenassist_si128(xmm1, 0x08), rk + 24 * 4);
323 aesni_set_rk_192(&xmm0, &xmm1, _mm_aeskeygenassist_si128(xmm1, 0x10), rk + 24 * 5);
324 aesni_set_rk_192(&xmm0, &xmm1, _mm_aeskeygenassist_si128(xmm1, 0x20), rk + 24 * 6);
325 aesni_set_rk_192(&xmm0, &xmm1, _mm_aeskeygenassist_si128(xmm1, 0x40), rk + 24 * 7);
326 aesni_set_rk_192(&xmm0, &xmm1, _mm_aeskeygenassist_si128(xmm1, 0x80), rk + 24 * 8);
327}
328
329/*
330 * Key expansion, 256-bit case
331 */
332static void aesni_set_rk_256(__m128i xmm0, __m128i xmm1, __m128i xmm2,
333 __m128i *rk0, __m128i *rk1)
334{
335 /*
336 * Finish generating the next two round keys.
337 *
338 * On entry xmm0 is r3:r2:r1:r0, xmm1 is r7:r6:r5:r4 and
339 * xmm2 is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
340 *
341 * On exit, *rk0 is r11:r10:r9:r8 and *rk1 is r15:r14:r13:r12
342 */
343 xmm2 = _mm_shuffle_epi32(xmm2, 0xff);
344 xmm2 ^= xmm0;
345 xmm0 = _mm_slli_si128(xmm0, 4);
346 xmm2 ^= xmm0;
347 xmm0 = _mm_slli_si128(xmm0, 4);
348 xmm2 ^= xmm0;
349 xmm0 = _mm_slli_si128(xmm0, 4);
350 xmm0 ^= xmm2;
351 *rk0 = xmm0;
352
353 /* Set xmm2 to stuff:Y:stuff:stuff with Y = subword( r11 )
354 * and proceed to generate next round key from there */
355 xmm2 = _mm_aeskeygenassist_si128(xmm0, 0x00);
356 xmm2 = _mm_shuffle_epi32(xmm2, 0xaa);
357 xmm2 ^= xmm1;
358 xmm1 = _mm_slli_si128(xmm1, 4);
359 xmm2 ^= xmm1;
360 xmm1 = _mm_slli_si128(xmm1, 4);
361 xmm2 ^= xmm1;
362 xmm1 = _mm_slli_si128(xmm1, 4);
363 xmm1 ^= xmm2;
364 *rk1 = xmm1;
365}
366
367static void aesni_setkey_enc_256(unsigned char *rk_bytes,
368 const unsigned char *key)
369{
370 __m128i *rk = (__m128i *) rk_bytes;
371
372 memcpy(&rk[0], key, 16);
373 memcpy(&rk[1], key + 16, 16);
374
375 /*
376 * Main "loop" - Generating one more key than necessary,
377 * see definition of mbedtls_aes_context.buf
378 */
379 aesni_set_rk_256(rk[0], rk[1], _mm_aeskeygenassist_si128(rk[1], 0x01), &rk[2], &rk[3]);
380 aesni_set_rk_256(rk[2], rk[3], _mm_aeskeygenassist_si128(rk[3], 0x02), &rk[4], &rk[5]);
381 aesni_set_rk_256(rk[4], rk[5], _mm_aeskeygenassist_si128(rk[5], 0x04), &rk[6], &rk[7]);
382 aesni_set_rk_256(rk[6], rk[7], _mm_aeskeygenassist_si128(rk[7], 0x08), &rk[8], &rk[9]);
383 aesni_set_rk_256(rk[8], rk[9], _mm_aeskeygenassist_si128(rk[9], 0x10), &rk[10], &rk[11]);
384 aesni_set_rk_256(rk[10], rk[11], _mm_aeskeygenassist_si128(rk[11], 0x20), &rk[12], &rk[13]);
385 aesni_set_rk_256(rk[12], rk[13], _mm_aeskeygenassist_si128(rk[13], 0x40), &rk[14], &rk[15]);
386}
387
388#else /* MBEDTLS_HAVE_AESNI_INTRINSICS */
389
Gilles Peskined0f9b0b2023-03-10 22:25:13 +0100390#if defined(__has_feature)
391#if __has_feature(memory_sanitizer)
392#warning \
393 "MBEDTLS_AESNI_C is known to cause spurious error reports with some memory sanitizers as they do not understand the assembly code."
394#endif
395#endif
396
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100397/*
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +0200398 * Binutils needs to be at least 2.19 to support AES-NI instructions.
399 * Unfortunately, a lot of users have a lower version now (2014-04).
400 * Emit bytecode directly in order to support "old" version of gas.
401 *
402 * Opcodes from the Intel architecture reference manual, vol. 3.
403 * We always use registers, so we don't need prefixes for memory operands.
404 * Operand macros are in gas order (src, dst) as opposed to Intel order
405 * (dst, src) in order to blend better into the surrounding assembly code.
406 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100407#define AESDEC(regs) ".byte 0x66,0x0F,0x38,0xDE," regs "\n\t"
408#define AESDECLAST(regs) ".byte 0x66,0x0F,0x38,0xDF," regs "\n\t"
409#define AESENC(regs) ".byte 0x66,0x0F,0x38,0xDC," regs "\n\t"
410#define AESENCLAST(regs) ".byte 0x66,0x0F,0x38,0xDD," regs "\n\t"
411#define AESIMC(regs) ".byte 0x66,0x0F,0x38,0xDB," regs "\n\t"
412#define AESKEYGENA(regs, imm) ".byte 0x66,0x0F,0x3A,0xDF," regs "," imm "\n\t"
413#define PCLMULQDQ(regs, imm) ".byte 0x66,0x0F,0x3A,0x44," regs "," imm "\n\t"
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +0200414
415#define xmm0_xmm0 "0xC0"
416#define xmm0_xmm1 "0xC8"
417#define xmm0_xmm2 "0xD0"
418#define xmm0_xmm3 "0xD8"
419#define xmm0_xmm4 "0xE0"
420#define xmm1_xmm0 "0xC1"
421#define xmm1_xmm2 "0xD1"
422
423/*
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100424 * AES-NI AES-ECB block en(de)cryption
425 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100426int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
427 int mode,
428 const unsigned char input[16],
429 unsigned char output[16])
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100430{
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 asm ("movdqu (%3), %%xmm0 \n\t" // load input
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200432 "movdqu (%1), %%xmm1 \n\t" // load round key 0
433 "pxor %%xmm1, %%xmm0 \n\t" // round 0
James Cowgill6c8edca2015-12-17 01:40:26 +0000434 "add $16, %1 \n\t" // point to next round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200435 "subl $1, %0 \n\t" // normal rounds = nr - 1
436 "test %2, %2 \n\t" // mode?
437 "jz 2f \n\t" // 0 = decrypt
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100438
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200439 "1: \n\t" // encryption loop
440 "movdqu (%1), %%xmm1 \n\t" // load round key
Gilles Peskine4e201442023-03-15 19:36:03 +0100441 AESENC(xmm1_xmm0) // do round
442 "add $16, %1 \n\t" // point to next round key
443 "subl $1, %0 \n\t" // loop
444 "jnz 1b \n\t"
445 "movdqu (%1), %%xmm1 \n\t" // load round key
446 AESENCLAST(xmm1_xmm0) // last round
447 "jmp 3f \n\t"
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100448
Gilles Peskine4e201442023-03-15 19:36:03 +0100449 "2: \n\t" // decryption loop
450 "movdqu (%1), %%xmm1 \n\t"
451 AESDEC(xmm1_xmm0) // do round
452 "add $16, %1 \n\t"
453 "subl $1, %0 \n\t"
454 "jnz 2b \n\t"
455 "movdqu (%1), %%xmm1 \n\t" // load round key
456 AESDECLAST(xmm1_xmm0) // last round
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100457
Gilles Peskine4e201442023-03-15 19:36:03 +0100458 "3: \n\t"
459 "movdqu %%xmm0, (%4) \n\t" // export output
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100460 :
Werner Lewisdd76ef32022-05-30 12:00:21 +0100461 : "r" (ctx->nr), "r" (ctx->buf + ctx->rk_offset), "r" (mode), "r" (input), "r" (output)
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 : "memory", "cc", "xmm0", "xmm1");
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100463
464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 return 0;
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100466}
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100467
468/*
469 * GCM multiplication: c = a times b in GF(2^128)
470 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
471 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100472void mbedtls_aesni_gcm_mult(unsigned char c[16],
473 const unsigned char a[16],
474 const unsigned char b[16])
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100475{
476 unsigned char aa[16], bb[16], cc[16];
477 size_t i;
478
479 /* The inputs are in big-endian order, so byte-reverse them */
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 for (i = 0; i < 16; i++) {
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100481 aa[i] = a[15 - i];
482 bb[i] = b[15 - i];
483 }
484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 asm ("movdqu (%0), %%xmm0 \n\t" // a1:a0
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200486 "movdqu (%1), %%xmm1 \n\t" // b1:b0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100487
488 /*
489 * Caryless multiplication xmm2:xmm1 = xmm0 * xmm1
Gilles Peskined4f31c82023-03-10 22:21:47 +0100490 * using [CLMUL-WP] algorithm 1 (p. 12).
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100491 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200492 "movdqa %%xmm1, %%xmm2 \n\t" // copy of b1:b0
493 "movdqa %%xmm1, %%xmm3 \n\t" // same
494 "movdqa %%xmm1, %%xmm4 \n\t" // same
Gilles Peskine4e201442023-03-15 19:36:03 +0100495 PCLMULQDQ(xmm0_xmm1, "0x00") // a0*b0 = c1:c0
496 PCLMULQDQ(xmm0_xmm2, "0x11") // a1*b1 = d1:d0
497 PCLMULQDQ(xmm0_xmm3, "0x10") // a0*b1 = e1:e0
498 PCLMULQDQ(xmm0_xmm4, "0x01") // a1*b0 = f1:f0
499 "pxor %%xmm3, %%xmm4 \n\t" // e1+f1:e0+f0
500 "movdqa %%xmm4, %%xmm3 \n\t" // same
501 "psrldq $8, %%xmm4 \n\t" // 0:e1+f1
502 "pslldq $8, %%xmm3 \n\t" // e0+f0:0
503 "pxor %%xmm4, %%xmm2 \n\t" // d1:d0+e1+f1
504 "pxor %%xmm3, %%xmm1 \n\t" // c1+e0+f1:c0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100505
506 /*
507 * Now shift the result one bit to the left,
Gilles Peskined4f31c82023-03-10 22:21:47 +0100508 * taking advantage of [CLMUL-WP] eq 27 (p. 18)
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100509 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100510 "movdqa %%xmm1, %%xmm3 \n\t" // r1:r0
511 "movdqa %%xmm2, %%xmm4 \n\t" // r3:r2
512 "psllq $1, %%xmm1 \n\t" // r1<<1:r0<<1
513 "psllq $1, %%xmm2 \n\t" // r3<<1:r2<<1
514 "psrlq $63, %%xmm3 \n\t" // r1>>63:r0>>63
515 "psrlq $63, %%xmm4 \n\t" // r3>>63:r2>>63
516 "movdqa %%xmm3, %%xmm5 \n\t" // r1>>63:r0>>63
517 "pslldq $8, %%xmm3 \n\t" // r0>>63:0
518 "pslldq $8, %%xmm4 \n\t" // r2>>63:0
519 "psrldq $8, %%xmm5 \n\t" // 0:r1>>63
520 "por %%xmm3, %%xmm1 \n\t" // r1<<1|r0>>63:r0<<1
521 "por %%xmm4, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1
522 "por %%xmm5, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1|r1>>63
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100523
524 /*
525 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
Gilles Peskined4f31c82023-03-10 22:21:47 +0100526 * using [CLMUL-WP] algorithm 5 (p. 18).
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100527 * Currently xmm2:xmm1 holds x3:x2:x1:x0 (already shifted).
528 */
529 /* Step 2 (1) */
Gilles Peskine4e201442023-03-15 19:36:03 +0100530 "movdqa %%xmm1, %%xmm3 \n\t" // x1:x0
531 "movdqa %%xmm1, %%xmm4 \n\t" // same
532 "movdqa %%xmm1, %%xmm5 \n\t" // same
533 "psllq $63, %%xmm3 \n\t" // x1<<63:x0<<63 = stuff:a
534 "psllq $62, %%xmm4 \n\t" // x1<<62:x0<<62 = stuff:b
535 "psllq $57, %%xmm5 \n\t" // x1<<57:x0<<57 = stuff:c
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100536
537 /* Step 2 (2) */
Gilles Peskine4e201442023-03-15 19:36:03 +0100538 "pxor %%xmm4, %%xmm3 \n\t" // stuff:a+b
539 "pxor %%xmm5, %%xmm3 \n\t" // stuff:a+b+c
540 "pslldq $8, %%xmm3 \n\t" // a+b+c:0
541 "pxor %%xmm3, %%xmm1 \n\t" // x1+a+b+c:x0 = d:x0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100542
543 /* Steps 3 and 4 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100544 "movdqa %%xmm1,%%xmm0 \n\t" // d:x0
545 "movdqa %%xmm1,%%xmm4 \n\t" // same
546 "movdqa %%xmm1,%%xmm5 \n\t" // same
547 "psrlq $1, %%xmm0 \n\t" // e1:x0>>1 = e1:e0'
548 "psrlq $2, %%xmm4 \n\t" // f1:x0>>2 = f1:f0'
549 "psrlq $7, %%xmm5 \n\t" // g1:x0>>7 = g1:g0'
550 "pxor %%xmm4, %%xmm0 \n\t" // e1+f1:e0'+f0'
551 "pxor %%xmm5, %%xmm0 \n\t" // e1+f1+g1:e0'+f0'+g0'
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200552 // e0'+f0'+g0' is almost e0+f0+g0, ex\tcept for some missing
553 // bits carried from d. Now get those\t bits back in.
Gilles Peskine4e201442023-03-15 19:36:03 +0100554 "movdqa %%xmm1,%%xmm3 \n\t" // d:x0
555 "movdqa %%xmm1,%%xmm4 \n\t" // same
556 "movdqa %%xmm1,%%xmm5 \n\t" // same
557 "psllq $63, %%xmm3 \n\t" // d<<63:stuff
558 "psllq $62, %%xmm4 \n\t" // d<<62:stuff
559 "psllq $57, %%xmm5 \n\t" // d<<57:stuff
560 "pxor %%xmm4, %%xmm3 \n\t" // d<<63+d<<62:stuff
561 "pxor %%xmm5, %%xmm3 \n\t" // missing bits of d:stuff
562 "psrldq $8, %%xmm3 \n\t" // 0:missing bits of d
563 "pxor %%xmm3, %%xmm0 \n\t" // e1+f1+g1:e0+f0+g0
564 "pxor %%xmm1, %%xmm0 \n\t" // h1:h0
565 "pxor %%xmm2, %%xmm0 \n\t" // x3+h1:x2+h0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100566
Gilles Peskine4e201442023-03-15 19:36:03 +0100567 "movdqu %%xmm0, (%2) \n\t" // done
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100568 :
569 : "r" (aa), "r" (bb), "r" (cc)
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100571
572 /* Now byte-reverse the outputs */
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 for (i = 0; i < 16; i++) {
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100574 c[i] = cc[15 - i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 }
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100576
Paul Bakkere7f51332013-12-30 15:32:02 +0100577 return;
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100578}
579
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100580/*
581 * Compute decryption round keys from encryption round keys
582 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100583void mbedtls_aesni_inverse_key(unsigned char *invkey,
584 const unsigned char *fwdkey, int nr)
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100585{
586 unsigned char *ik = invkey;
587 const unsigned char *fk = fwdkey + 16 * nr;
588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 for (fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16) {
592 asm ("movdqu (%0), %%xmm0 \n\t"
Gilles Peskine4e201442023-03-15 19:36:03 +0100593 AESIMC(xmm0_xmm0)
594 "movdqu %%xmm0, (%1) \n\t"
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100595 :
596 : "r" (fk), "r" (ik)
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 : "memory", "xmm0");
598 }
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100601}
602
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100603/*
604 * Key expansion, 128-bit case
605 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100606static void aesni_setkey_enc_128(unsigned char *rk,
607 const unsigned char *key)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100608{
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 asm ("movdqu (%1), %%xmm0 \n\t" // copy the original key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200610 "movdqu %%xmm0, (%0) \n\t" // as round key 0
611 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100612
613 /*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100614 * Finish generating the next round key.
615 *
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100616 * On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff
617 * with X = rot( sub( r3 ) ) ^ RCON.
618 *
619 * On exit, xmm0 is r7:r6:r5:r4
620 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
621 * and those are written to the round key buffer.
622 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200623 "1: \n\t"
624 "pshufd $0xff, %%xmm1, %%xmm1 \n\t" // X:X:X:X
625 "pxor %%xmm0, %%xmm1 \n\t" // X+r3:X+r2:X+r1:r4
626 "pslldq $4, %%xmm0 \n\t" // r2:r1:r0:0
627 "pxor %%xmm0, %%xmm1 \n\t" // X+r3+r2:X+r2+r1:r5:r4
628 "pslldq $4, %%xmm0 \n\t" // etc
629 "pxor %%xmm0, %%xmm1 \n\t"
630 "pslldq $4, %%xmm0 \n\t"
631 "pxor %%xmm1, %%xmm0 \n\t" // update xmm0 for next time!
632 "add $16, %0 \n\t" // point to next round key
633 "movdqu %%xmm0, (%0) \n\t" // write it
634 "ret \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100635
636 /* Main "loop" */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200637 "2: \n\t"
Gilles Peskine4e201442023-03-15 19:36:03 +0100638 AESKEYGENA(xmm0_xmm1, "0x01") "call 1b \n\t"
639 AESKEYGENA(xmm0_xmm1, "0x02") "call 1b \n\t"
640 AESKEYGENA(xmm0_xmm1, "0x04") "call 1b \n\t"
641 AESKEYGENA(xmm0_xmm1, "0x08") "call 1b \n\t"
642 AESKEYGENA(xmm0_xmm1, "0x10") "call 1b \n\t"
643 AESKEYGENA(xmm0_xmm1, "0x20") "call 1b \n\t"
644 AESKEYGENA(xmm0_xmm1, "0x40") "call 1b \n\t"
645 AESKEYGENA(xmm0_xmm1, "0x80") "call 1b \n\t"
646 AESKEYGENA(xmm0_xmm1, "0x1B") "call 1b \n\t"
647 AESKEYGENA(xmm0_xmm1, "0x36") "call 1b \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100648 :
649 : "r" (rk), "r" (key)
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 : "memory", "cc", "0");
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100651}
652
653/*
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100654 * Key expansion, 192-bit case
655 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100656static void aesni_setkey_enc_192(unsigned char *rk,
657 const unsigned char *key)
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100658{
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 asm ("movdqu (%1), %%xmm0 \n\t" // copy original round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200660 "movdqu %%xmm0, (%0) \n\t"
661 "add $16, %0 \n\t"
662 "movq 16(%1), %%xmm1 \n\t"
663 "movq %%xmm1, (%0) \n\t"
664 "add $8, %0 \n\t"
665 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100666
667 /*
668 * Finish generating the next 6 quarter-keys.
669 *
670 * On entry xmm0 is r3:r2:r1:r0, xmm1 is stuff:stuff:r5:r4
671 * and xmm2 is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON.
672 *
673 * On exit, xmm0 is r9:r8:r7:r6 and xmm1 is stuff:stuff:r11:r10
674 * and those are written to the round key buffer.
675 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200676 "1: \n\t"
677 "pshufd $0x55, %%xmm2, %%xmm2 \n\t" // X:X:X:X
678 "pxor %%xmm0, %%xmm2 \n\t" // X+r3:X+r2:X+r1:r4
679 "pslldq $4, %%xmm0 \n\t" // etc
680 "pxor %%xmm0, %%xmm2 \n\t"
681 "pslldq $4, %%xmm0 \n\t"
682 "pxor %%xmm0, %%xmm2 \n\t"
683 "pslldq $4, %%xmm0 \n\t"
684 "pxor %%xmm2, %%xmm0 \n\t" // update xmm0 = r9:r8:r7:r6
685 "movdqu %%xmm0, (%0) \n\t"
686 "add $16, %0 \n\t"
687 "pshufd $0xff, %%xmm0, %%xmm2 \n\t" // r9:r9:r9:r9
688 "pxor %%xmm1, %%xmm2 \n\t" // stuff:stuff:r9+r5:r10
689 "pslldq $4, %%xmm1 \n\t" // r2:r1:r0:0
690 "pxor %%xmm2, %%xmm1 \n\t" // xmm1 = stuff:stuff:r11:r10
691 "movq %%xmm1, (%0) \n\t"
692 "add $8, %0 \n\t"
693 "ret \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100694
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200695 "2: \n\t"
Gilles Peskine4e201442023-03-15 19:36:03 +0100696 AESKEYGENA(xmm1_xmm2, "0x01") "call 1b \n\t"
697 AESKEYGENA(xmm1_xmm2, "0x02") "call 1b \n\t"
698 AESKEYGENA(xmm1_xmm2, "0x04") "call 1b \n\t"
699 AESKEYGENA(xmm1_xmm2, "0x08") "call 1b \n\t"
700 AESKEYGENA(xmm1_xmm2, "0x10") "call 1b \n\t"
701 AESKEYGENA(xmm1_xmm2, "0x20") "call 1b \n\t"
702 AESKEYGENA(xmm1_xmm2, "0x40") "call 1b \n\t"
703 AESKEYGENA(xmm1_xmm2, "0x80") "call 1b \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100704
705 :
706 : "r" (rk), "r" (key)
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 : "memory", "cc", "0");
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100708}
709
710/*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100711 * Key expansion, 256-bit case
712 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100713static void aesni_setkey_enc_256(unsigned char *rk,
714 const unsigned char *key)
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100715{
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 asm ("movdqu (%1), %%xmm0 \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200717 "movdqu %%xmm0, (%0) \n\t"
718 "add $16, %0 \n\t"
719 "movdqu 16(%1), %%xmm1 \n\t"
720 "movdqu %%xmm1, (%0) \n\t"
721 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100722
723 /*
724 * Finish generating the next two round keys.
725 *
726 * On entry xmm0 is r3:r2:r1:r0, xmm1 is r7:r6:r5:r4 and
727 * xmm2 is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
728 *
729 * On exit, xmm0 is r11:r10:r9:r8 and xmm1 is r15:r14:r13:r12
730 * and those have been written to the output buffer.
731 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200732 "1: \n\t"
733 "pshufd $0xff, %%xmm2, %%xmm2 \n\t"
734 "pxor %%xmm0, %%xmm2 \n\t"
735 "pslldq $4, %%xmm0 \n\t"
736 "pxor %%xmm0, %%xmm2 \n\t"
737 "pslldq $4, %%xmm0 \n\t"
738 "pxor %%xmm0, %%xmm2 \n\t"
739 "pslldq $4, %%xmm0 \n\t"
740 "pxor %%xmm2, %%xmm0 \n\t"
741 "add $16, %0 \n\t"
742 "movdqu %%xmm0, (%0) \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100743
744 /* Set xmm2 to stuff:Y:stuff:stuff with Y = subword( r11 )
745 * and proceed to generate next round key from there */
Gilles Peskine4e201442023-03-15 19:36:03 +0100746 AESKEYGENA(xmm0_xmm2, "0x00")
747 "pshufd $0xaa, %%xmm2, %%xmm2 \n\t"
748 "pxor %%xmm1, %%xmm2 \n\t"
749 "pslldq $4, %%xmm1 \n\t"
750 "pxor %%xmm1, %%xmm2 \n\t"
751 "pslldq $4, %%xmm1 \n\t"
752 "pxor %%xmm1, %%xmm2 \n\t"
753 "pslldq $4, %%xmm1 \n\t"
754 "pxor %%xmm2, %%xmm1 \n\t"
755 "add $16, %0 \n\t"
756 "movdqu %%xmm1, (%0) \n\t"
757 "ret \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100758
759 /*
760 * Main "loop" - Generating one more key than necessary,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 * see definition of mbedtls_aes_context.buf
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100762 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100763 "2: \n\t"
764 AESKEYGENA(xmm1_xmm2, "0x01") "call 1b \n\t"
765 AESKEYGENA(xmm1_xmm2, "0x02") "call 1b \n\t"
766 AESKEYGENA(xmm1_xmm2, "0x04") "call 1b \n\t"
767 AESKEYGENA(xmm1_xmm2, "0x08") "call 1b \n\t"
768 AESKEYGENA(xmm1_xmm2, "0x10") "call 1b \n\t"
769 AESKEYGENA(xmm1_xmm2, "0x20") "call 1b \n\t"
770 AESKEYGENA(xmm1_xmm2, "0x40") "call 1b \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100771 :
772 : "r" (rk), "r" (key)
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 : "memory", "cc", "0");
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100774}
775
Gilles Peskined6719172023-03-10 22:37:11 +0100776#endif /* MBEDTLS_HAVE_AESNI_INTRINSICS */
777
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100778/*
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100779 * Key expansion, wrapper
780 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100781int mbedtls_aesni_setkey_enc(unsigned char *rk,
782 const unsigned char *key,
783 size_t bits)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100784{
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 switch (bits) {
786 case 128: aesni_setkey_enc_128(rk, key); break;
787 case 192: aesni_setkey_enc_192(rk, key); break;
788 case 256: aesni_setkey_enc_256(rk, key); break;
789 default: return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100790 }
791
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 return 0;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100793}
794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795#endif /* MBEDTLS_HAVE_X86_64 */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797#endif /* MBEDTLS_AESNI_C */