blob: 0398d8aae44210ea2b80aeda590cdbc65015f86f [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 Peskine6055b782023-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
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/aesni.h"
Rich Evans00ab4702015-02-06 13:43:58 +000030
31#include <string.h>
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010032
David Horstmannb6bf5f52023-01-03 11:07:09 +000033/* *INDENT-OFF* */
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020034#ifndef asm
35#define asm __asm
36#endif
David Horstmannb6bf5f52023-01-03 11:07:09 +000037/* *INDENT-ON* */
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020038
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010039#if defined(MBEDTLS_HAVE_AESNI_INTRINSICS) || defined(MBEDTLS_HAVE_X86_64)
40
41#if defined(MBEDTLS_HAVE_AESNI_INTRINSICS)
42#include <cpuid.h>
43#include <immintrin.h>
44#endif
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010045
46/*
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010047 * AES-NI support detection routine
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010048 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010049int mbedtls_aesni_has_support(unsigned int what)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010050{
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010051 static int done = 0;
52 static unsigned int c = 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010053
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010054 if (!done) {
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010055#if defined(MBEDTLS_HAVE_AESNI_INTRINSICS)
56 static unsigned info[4] = { 0, 0, 0, 0 };
57#if defined(_MSC_VER)
58 __cpuid(info, 1);
59#else
60 __cpuid(1, info[0], info[1], info[2], info[3]);
61#endif
62 c = info[2];
63#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010064 asm ("movl $1, %%eax \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +020065 "cpuid \n\t"
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010066 : "=c" (c)
67 :
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010068 : "eax", "ebx", "edx");
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010069#endif
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010070 done = 1;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010071 }
72
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073 return (c & what) != 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010074}
75
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010076#if defined(MBEDTLS_HAVE_AESNI_INTRINSICS)
77
78/*
79 * AES-NI AES-ECB block en(de)cryption
80 */
81int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
82 int mode,
83 const unsigned char input[16],
84 unsigned char output[16])
85{
86 const __m128i *rk = (const __m128i *) (ctx->rk);
87 unsigned nr = ctx->nr; // Number of remaining rounds
88 // Load round key 0
89 __m128i xmm0;
90 memcpy(&xmm0, input, 16);
91 xmm0 ^= *rk;
92 ++rk;
93 --nr;
94
95 if (mode == 0) {
96 while (nr != 0) {
97 xmm0 = _mm_aesdec_si128(xmm0, *rk);
98 ++rk;
99 --nr;
100 }
101 xmm0 = _mm_aesdeclast_si128(xmm0, *rk);
102 } else {
103 while (nr != 0) {
104 xmm0 = _mm_aesenc_si128(xmm0, *rk);
105 ++rk;
106 --nr;
107 }
108 xmm0 = _mm_aesenclast_si128(xmm0, *rk);
109 }
110
111 memcpy(output, &xmm0, 16);
112 return 0;
113}
114
115/*
116 * GCM multiplication: c = a times b in GF(2^128)
117 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
118 */
119
120static void gcm_clmul(const __m128i aa, const __m128i bb,
121 __m128i *cc, __m128i *dd)
122{
123 /*
124 * Caryless multiplication dd:cc = aa * bb
125 * using [CLMUL-WP] algorithm 1 (p. 12).
126 */
127 *cc = _mm_clmulepi64_si128(aa, bb, 0x00); // a0*b0 = c1:c0
128 *dd = _mm_clmulepi64_si128(aa, bb, 0x11); // a1*b1 = d1:d0
129 __m128i ee = _mm_clmulepi64_si128(aa, bb, 0x10); // a0*b1 = e1:e0
130 __m128i ff = _mm_clmulepi64_si128(aa, bb, 0x01); // a1*b0 = f1:f0
131 ff ^= ee; // e1+f1:e0+f0
132 ee = ff; // e1+f1:e0+f0
133 ff = _mm_srli_si128(ff, 8); // 0:e1+f1
134 ee = _mm_slli_si128(ee, 8); // e0+f0:0
135 *dd ^= ff; // d1:d0+e1+f1
136 *cc ^= ee; // c1+e0+f1:c0
137}
138
139static void gcm_shift(__m128i *cc, __m128i *dd)
140{
141 /*
142 * Now shift the result one bit to the left,
143 * taking advantage of [CLMUL-WP] eq 27 (p. 18)
144 */
145 // // *cc = r1:r0
146 // // *dd = r3:r2
147 __m128i xmm1 = _mm_slli_epi64(*cc, 1); // r1<<1:r0<<1
148 __m128i xmm2 = _mm_slli_epi64(*dd, 1); // r3<<1:r2<<1
149 __m128i xmm3 = _mm_srli_epi64(*cc, 63); // r1>>63:r0>>63
150 __m128i xmm4 = _mm_srli_epi64(*dd, 63); // r3>>63:r2>>63
151 __m128i xmm5 = _mm_srli_si128(xmm3, 8); // 0:r1>>63
152 xmm3 = _mm_slli_si128(xmm3, 8); // r0>>63:0
153 xmm4 = _mm_slli_si128(xmm4, 8); // 0:r1>>63
154
155 *cc = xmm1 | xmm3; // r1<<1|r0>>63:r0<<1
156 *dd = xmm2 | xmm4 | xmm5; // r3<<1|r2>>62:r2<<1|r1>>63
157}
158
159static __m128i gcm_reduce1(__m128i xx)
160{
161 // // xx = x1:x0
162 /* [CLMUL-WP] Algorithm 5 Step 2 */
163 __m128i aa = _mm_slli_epi64(xx, 63); // x1<<63:x0<<63 = stuff:a
164 __m128i bb = _mm_slli_epi64(xx, 62); // x1<<62:x0<<62 = stuff:b
165 __m128i cc = _mm_slli_epi64(xx, 57); // x1<<57:x0<<57 = stuff:c
166 __m128i dd = _mm_slli_si128(aa ^ bb ^ cc, 8); // a+b+c:0
167 return dd ^ xx; // x1+a+b+c:x0 = d:x0
168}
169
170static __m128i gcm_reduce2(__m128i dx)
171{
172 /* [CLMUL-WP] Algorithm 5 Steps 3 and 4 */
173 __m128i ee = _mm_srli_epi64(dx, 1); // e1:x0>>1 = e1:e0'
174 __m128i ff = _mm_srli_epi64(dx, 2); // f1:x0>>2 = f1:f0'
175 __m128i gg = _mm_srli_epi64(dx, 7); // g1:x0>>7 = g1:g0'
176
177 // e0'+f0'+g0' is almost e0+f0+g0, except for some missing
178 // bits carried from d. Now get those bits back in.
179 __m128i eh = _mm_slli_epi64(dx, 63); // d<<63:stuff
180 __m128i fh = _mm_slli_epi64(dx, 62); // d<<62:stuff
181 __m128i gh = _mm_slli_epi64(dx, 57); // d<<57:stuff
182 __m128i hh = _mm_srli_si128(eh ^ fh ^ gh, 8); // 0:missing bits of d
183
184 return ee ^ ff ^ gg ^ hh ^ dx;
185}
186
187void mbedtls_aesni_gcm_mult(unsigned char c[16],
188 const unsigned char a[16],
189 const unsigned char b[16])
190{
191 __m128i aa, bb, cc, dd;
192
193 /* The inputs are in big-endian order, so byte-reverse them */
194 for (size_t i = 0; i < 16; i++) {
195 ((uint8_t *) &aa)[i] = a[15 - i];
196 ((uint8_t *) &bb)[i] = b[15 - i];
197 }
198
199 gcm_clmul(aa, bb, &cc, &dd);
200 gcm_shift(&cc, &dd);
201 /*
202 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
203 * using [CLMUL-WP] algorithm 5 (p. 18).
204 * Currently dd:cc holds x3:x2:x1:x0 (already shifted).
205 */
206 __m128i dx = gcm_reduce1(cc);
207 __m128i xh = gcm_reduce2(dx);
208 cc = xh ^ dd; // x3+h1:x2+h0
209
210 /* Now byte-reverse the outputs */
211 for (size_t i = 0; i < 16; i++) {
212 c[i] = ((uint8_t *) &cc)[15 - i];
213 }
214
215 return;
216}
217
218/*
219 * Compute decryption round keys from encryption round keys
220 */
221void mbedtls_aesni_inverse_key(unsigned char *invkey,
222 const unsigned char *fwdkey, int nr)
223{
224 __m128i *ik = (__m128i *) invkey;
225 const __m128i *fk = (const __m128i *) fwdkey + nr;
226
227 *ik = *fk;
228 for (--fk, ++ik; fk > (const __m128i *) fwdkey; --fk, ++ik) {
229 *ik = _mm_aesimc_si128(*fk);
230 }
231 *ik = *fk;
232}
233
234/*
235 * Key expansion, 128-bit case
236 */
237static __m128i aesni_set_rk_128(__m128i xmm0, __m128i xmm1)
238{
239 /*
240 * Finish generating the next round key.
241 *
242 * On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff
243 * with X = rot( sub( r3 ) ) ^ RCON.
244 *
245 * On exit, xmm1 is r7:r6:r5:r4
246 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
247 * and this is returned, to be written to the round key buffer.
248 */
249 xmm1 = _mm_shuffle_epi32(xmm1, 0xff); // X:X:X:X
250 xmm1 ^= xmm0; // X+r3:X+r2:X+r1:r4
251 xmm0 = _mm_slli_si128(xmm0, 4); // r2:r1:r0:0
252 xmm1 ^= xmm0; // X+r3+r2:X+r2+r1:r5:r4
253 xmm0 = _mm_slli_si128(xmm0, 4); // r1:r0:0:0
254 xmm1 ^= xmm0; // X+r3+r2+r1:r6:r5:r4
255 xmm0 = _mm_slli_si128(xmm0, 4); // r0:0:0:0
256 xmm1 ^= xmm0; // r7:r6:r5:r4
257 return xmm1;
258}
259
260static void aesni_setkey_enc_128(unsigned char *rk_bytes,
261 const unsigned char *key)
262{
263 __m128i *rk = (__m128i *) rk_bytes;
264
265 memcpy(&rk[0], key, 16);
266 rk[1] = aesni_set_rk_128(rk[0], _mm_aeskeygenassist_si128(rk[0], 0x01));
267 rk[2] = aesni_set_rk_128(rk[1], _mm_aeskeygenassist_si128(rk[1], 0x02));
268 rk[3] = aesni_set_rk_128(rk[2], _mm_aeskeygenassist_si128(rk[2], 0x04));
269 rk[4] = aesni_set_rk_128(rk[3], _mm_aeskeygenassist_si128(rk[3], 0x08));
270 rk[5] = aesni_set_rk_128(rk[4], _mm_aeskeygenassist_si128(rk[4], 0x10));
271 rk[6] = aesni_set_rk_128(rk[5], _mm_aeskeygenassist_si128(rk[5], 0x20));
272 rk[7] = aesni_set_rk_128(rk[6], _mm_aeskeygenassist_si128(rk[6], 0x40));
273 rk[8] = aesni_set_rk_128(rk[7], _mm_aeskeygenassist_si128(rk[7], 0x80));
274 rk[9] = aesni_set_rk_128(rk[8], _mm_aeskeygenassist_si128(rk[8], 0x1B));
275 rk[10] = aesni_set_rk_128(rk[9], _mm_aeskeygenassist_si128(rk[9], 0x36));
276}
277
278/*
279 * Key expansion, 192-bit case
280 */
281static void aesni_set_rk_192(__m128i *xmm0, __m128i *xmm1, __m128i xmm2,
282 unsigned char *rk)
283{
284 /*
285 * Finish generating the next 6 quarter-keys.
286 *
287 * On entry xmm0 is r3:r2:r1:r0, xmm1 is stuff:stuff:r5:r4
288 * and xmm2 is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON.
289 *
290 * On exit, xmm0 is r9:r8:r7:r6 and xmm1 is stuff:stuff:r11:r10
291 * and those are written to the round key buffer.
292 */
293 xmm2 = _mm_shuffle_epi32(xmm2, 0x55); // X:X:X:X
294 xmm2 = _mm_xor_si128(xmm2, *xmm0); // X+r3:X+r2:X+r1:X+r0
295 *xmm0 = _mm_slli_si128(*xmm0, 4); // r2:r1:r0:0
296 xmm2 = _mm_xor_si128(xmm2, *xmm0); // X+r3+r2:X+r2+r1:X+r1+r0:X+r0
297 *xmm0 = _mm_slli_si128(*xmm0, 4); // r1:r0:0:0
298 xmm2 = _mm_xor_si128(xmm2, *xmm0); // X+r3+r2+r1:X+r2+r1+r0:X+r1+r0:X+r0
299 *xmm0 = _mm_slli_si128(*xmm0, 4); // r0:0:0:0
300 xmm2 = _mm_xor_si128(xmm2, *xmm0); // X+r3+r2+r1+r0:X+r2+r1+r0:X+r1+r0:X+r0
301 *xmm0 = xmm2; // = r9:r8:r7:r6
302
303 xmm2 = _mm_shuffle_epi32(xmm2, 0xff); // r9:r9:r9:r9
304 xmm2 = _mm_xor_si128(xmm2, *xmm1); // stuff:stuff:r9+r5:r9+r4
305 *xmm1 = _mm_slli_si128(*xmm1, 4); // stuff:stuff:r4:0
306 xmm2 = _mm_xor_si128(xmm2, *xmm1); // stuff:stuff:r9+r5+r4:r9+r4
307 *xmm1 = xmm2; // = stuff:stuff:r11:r10
308
309 /* Store xmm0 and the low half of xmm1 into rk, which is conceptually
310 * an array of 24-byte elements. Since 24 is not a multiple of 16,
311 * rk is not necessarily aligned so just `*rk = *xmm0` doesn't work. */
312 memcpy(rk, xmm0, 16);
313 _mm_storeu_si64(rk + 16, *xmm1);
314}
315
316static void aesni_setkey_enc_192(unsigned char *rk,
317 const unsigned char *key)
318{
319 /* First round: use original key */
320 memcpy(rk, key, 24);
321 /* aes.c guarantees that rk is aligned on a 16-byte boundary. */
322 __m128i xmm0 = ((__m128i *) rk)[0];
323 __m128i xmm1 = _mm_loadl_epi64(((__m128i *) rk) + 1);
324
325 aesni_set_rk_192(&xmm0, &xmm1, _mm_aeskeygenassist_si128(xmm1, 0x01), rk + 24 * 1);
326 aesni_set_rk_192(&xmm0, &xmm1, _mm_aeskeygenassist_si128(xmm1, 0x02), rk + 24 * 2);
327 aesni_set_rk_192(&xmm0, &xmm1, _mm_aeskeygenassist_si128(xmm1, 0x04), rk + 24 * 3);
328 aesni_set_rk_192(&xmm0, &xmm1, _mm_aeskeygenassist_si128(xmm1, 0x08), rk + 24 * 4);
329 aesni_set_rk_192(&xmm0, &xmm1, _mm_aeskeygenassist_si128(xmm1, 0x10), rk + 24 * 5);
330 aesni_set_rk_192(&xmm0, &xmm1, _mm_aeskeygenassist_si128(xmm1, 0x20), rk + 24 * 6);
331 aesni_set_rk_192(&xmm0, &xmm1, _mm_aeskeygenassist_si128(xmm1, 0x40), rk + 24 * 7);
332 aesni_set_rk_192(&xmm0, &xmm1, _mm_aeskeygenassist_si128(xmm1, 0x80), rk + 24 * 8);
333}
334
335/*
336 * Key expansion, 256-bit case
337 */
338static void aesni_set_rk_256(__m128i xmm0, __m128i xmm1, __m128i xmm2,
339 __m128i *rk0, __m128i *rk1)
340{
341 /*
342 * Finish generating the next two round keys.
343 *
344 * On entry xmm0 is r3:r2:r1:r0, xmm1 is r7:r6:r5:r4 and
345 * xmm2 is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
346 *
347 * On exit, *rk0 is r11:r10:r9:r8 and *rk1 is r15:r14:r13:r12
348 */
349 xmm2 = _mm_shuffle_epi32(xmm2, 0xff);
350 xmm2 ^= xmm0;
351 xmm0 = _mm_slli_si128(xmm0, 4);
352 xmm2 ^= xmm0;
353 xmm0 = _mm_slli_si128(xmm0, 4);
354 xmm2 ^= xmm0;
355 xmm0 = _mm_slli_si128(xmm0, 4);
356 xmm0 ^= xmm2;
357 *rk0 = xmm0;
358
359 /* Set xmm2 to stuff:Y:stuff:stuff with Y = subword( r11 )
360 * and proceed to generate next round key from there */
361 xmm2 = _mm_aeskeygenassist_si128(xmm0, 0x00);
362 xmm2 = _mm_shuffle_epi32(xmm2, 0xaa);
363 xmm2 ^= xmm1;
364 xmm1 = _mm_slli_si128(xmm1, 4);
365 xmm2 ^= xmm1;
366 xmm1 = _mm_slli_si128(xmm1, 4);
367 xmm2 ^= xmm1;
368 xmm1 = _mm_slli_si128(xmm1, 4);
369 xmm1 ^= xmm2;
370 *rk1 = xmm1;
371}
372
373static void aesni_setkey_enc_256(unsigned char *rk_bytes,
374 const unsigned char *key)
375{
376 __m128i *rk = (__m128i *) rk_bytes;
377
378 memcpy(&rk[0], key, 16);
379 memcpy(&rk[1], key + 16, 16);
380
381 /*
382 * Main "loop" - Generating one more key than necessary,
383 * see definition of mbedtls_aes_context.buf
384 */
385 aesni_set_rk_256(rk[0], rk[1], _mm_aeskeygenassist_si128(rk[1], 0x01), &rk[2], &rk[3]);
386 aesni_set_rk_256(rk[2], rk[3], _mm_aeskeygenassist_si128(rk[3], 0x02), &rk[4], &rk[5]);
387 aesni_set_rk_256(rk[4], rk[5], _mm_aeskeygenassist_si128(rk[5], 0x04), &rk[6], &rk[7]);
388 aesni_set_rk_256(rk[6], rk[7], _mm_aeskeygenassist_si128(rk[7], 0x08), &rk[8], &rk[9]);
389 aesni_set_rk_256(rk[8], rk[9], _mm_aeskeygenassist_si128(rk[9], 0x10), &rk[10], &rk[11]);
390 aesni_set_rk_256(rk[10], rk[11], _mm_aeskeygenassist_si128(rk[11], 0x20), &rk[12], &rk[13]);
391 aesni_set_rk_256(rk[12], rk[13], _mm_aeskeygenassist_si128(rk[13], 0x40), &rk[14], &rk[15]);
392}
393
394#else /* MBEDTLS_HAVE_AESNI_INTRINSICS */
395
Gilles Peskine18d521a2023-03-10 22:25:13 +0100396#if defined(__has_feature)
397#if __has_feature(memory_sanitizer)
398#warning \
399 "MBEDTLS_AESNI_C is known to cause spurious error reports with some memory sanitizers as they do not understand the assembly code."
400#endif
401#endif
402
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100403/*
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +0200404 * Binutils needs to be at least 2.19 to support AES-NI instructions.
405 * Unfortunately, a lot of users have a lower version now (2014-04).
406 * Emit bytecode directly in order to support "old" version of gas.
407 *
408 * Opcodes from the Intel architecture reference manual, vol. 3.
409 * We always use registers, so we don't need prefixes for memory operands.
410 * Operand macros are in gas order (src, dst) as opposed to Intel order
411 * (dst, src) in order to blend better into the surrounding assembly code.
412 */
Gilles Peskine2808a602023-03-15 19:36:03 +0100413#define AESDEC(regs) ".byte 0x66,0x0F,0x38,0xDE," regs "\n\t"
414#define AESDECLAST(regs) ".byte 0x66,0x0F,0x38,0xDF," regs "\n\t"
415#define AESENC(regs) ".byte 0x66,0x0F,0x38,0xDC," regs "\n\t"
416#define AESENCLAST(regs) ".byte 0x66,0x0F,0x38,0xDD," regs "\n\t"
417#define AESIMC(regs) ".byte 0x66,0x0F,0x38,0xDB," regs "\n\t"
418#define AESKEYGENA(regs, imm) ".byte 0x66,0x0F,0x3A,0xDF," regs "," imm "\n\t"
419#define PCLMULQDQ(regs, imm) ".byte 0x66,0x0F,0x3A,0x44," regs "," imm "\n\t"
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +0200420
421#define xmm0_xmm0 "0xC0"
422#define xmm0_xmm1 "0xC8"
423#define xmm0_xmm2 "0xD0"
424#define xmm0_xmm3 "0xD8"
425#define xmm0_xmm4 "0xE0"
426#define xmm1_xmm0 "0xC1"
427#define xmm1_xmm2 "0xD1"
428
429/*
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100430 * AES-NI AES-ECB block en(de)cryption
431 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100432int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
433 int mode,
434 const unsigned char input[16],
435 unsigned char output[16])
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100436{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100437 asm ("movdqu (%3), %%xmm0 \n\t" // load input
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200438 "movdqu (%1), %%xmm1 \n\t" // load round key 0
439 "pxor %%xmm1, %%xmm0 \n\t" // round 0
James Cowgill6c8edca2015-12-17 01:40:26 +0000440 "add $16, %1 \n\t" // point to next round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200441 "subl $1, %0 \n\t" // normal rounds = nr - 1
442 "test %2, %2 \n\t" // mode?
443 "jz 2f \n\t" // 0 = decrypt
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100444
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200445 "1: \n\t" // encryption loop
446 "movdqu (%1), %%xmm1 \n\t" // load round key
Gilles Peskine2808a602023-03-15 19:36:03 +0100447 AESENC(xmm1_xmm0) // do round
448 "add $16, %1 \n\t" // point to next round key
449 "subl $1, %0 \n\t" // loop
450 "jnz 1b \n\t"
451 "movdqu (%1), %%xmm1 \n\t" // load round key
452 AESENCLAST(xmm1_xmm0) // last round
453 "jmp 3f \n\t"
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100454
Gilles Peskine2808a602023-03-15 19:36:03 +0100455 "2: \n\t" // decryption loop
456 "movdqu (%1), %%xmm1 \n\t"
457 AESDEC(xmm1_xmm0) // do round
458 "add $16, %1 \n\t"
459 "subl $1, %0 \n\t"
460 "jnz 2b \n\t"
461 "movdqu (%1), %%xmm1 \n\t" // load round key
462 AESDECLAST(xmm1_xmm0) // last round
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100463
Gilles Peskine2808a602023-03-15 19:36:03 +0100464 "3: \n\t"
465 "movdqu %%xmm0, (%4) \n\t" // export output
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100466 :
467 : "r" (ctx->nr), "r" (ctx->rk), "r" (mode), "r" (input), "r" (output)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100468 : "memory", "cc", "xmm0", "xmm1");
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100469
470
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100471 return 0;
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100472}
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100473
474/*
475 * GCM multiplication: c = a times b in GF(2^128)
476 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
477 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100478void mbedtls_aesni_gcm_mult(unsigned char c[16],
479 const unsigned char a[16],
480 const unsigned char b[16])
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100481{
482 unsigned char aa[16], bb[16], cc[16];
483 size_t i;
484
485 /* The inputs are in big-endian order, so byte-reverse them */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100486 for (i = 0; i < 16; i++) {
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100487 aa[i] = a[15 - i];
488 bb[i] = b[15 - i];
489 }
490
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100491 asm ("movdqu (%0), %%xmm0 \n\t" // a1:a0
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200492 "movdqu (%1), %%xmm1 \n\t" // b1:b0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100493
494 /*
495 * Caryless multiplication xmm2:xmm1 = xmm0 * xmm1
Gilles Peskine6055b782023-03-10 22:21:47 +0100496 * using [CLMUL-WP] algorithm 1 (p. 12).
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100497 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200498 "movdqa %%xmm1, %%xmm2 \n\t" // copy of b1:b0
499 "movdqa %%xmm1, %%xmm3 \n\t" // same
500 "movdqa %%xmm1, %%xmm4 \n\t" // same
Gilles Peskine2808a602023-03-15 19:36:03 +0100501 PCLMULQDQ(xmm0_xmm1, "0x00") // a0*b0 = c1:c0
502 PCLMULQDQ(xmm0_xmm2, "0x11") // a1*b1 = d1:d0
503 PCLMULQDQ(xmm0_xmm3, "0x10") // a0*b1 = e1:e0
504 PCLMULQDQ(xmm0_xmm4, "0x01") // a1*b0 = f1:f0
505 "pxor %%xmm3, %%xmm4 \n\t" // e1+f1:e0+f0
506 "movdqa %%xmm4, %%xmm3 \n\t" // same
507 "psrldq $8, %%xmm4 \n\t" // 0:e1+f1
508 "pslldq $8, %%xmm3 \n\t" // e0+f0:0
509 "pxor %%xmm4, %%xmm2 \n\t" // d1:d0+e1+f1
510 "pxor %%xmm3, %%xmm1 \n\t" // c1+e0+f1:c0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100511
512 /*
513 * Now shift the result one bit to the left,
Gilles Peskine6055b782023-03-10 22:21:47 +0100514 * taking advantage of [CLMUL-WP] eq 27 (p. 18)
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100515 */
Gilles Peskine2808a602023-03-15 19:36:03 +0100516 "movdqa %%xmm1, %%xmm3 \n\t" // r1:r0
517 "movdqa %%xmm2, %%xmm4 \n\t" // r3:r2
518 "psllq $1, %%xmm1 \n\t" // r1<<1:r0<<1
519 "psllq $1, %%xmm2 \n\t" // r3<<1:r2<<1
520 "psrlq $63, %%xmm3 \n\t" // r1>>63:r0>>63
521 "psrlq $63, %%xmm4 \n\t" // r3>>63:r2>>63
522 "movdqa %%xmm3, %%xmm5 \n\t" // r1>>63:r0>>63
523 "pslldq $8, %%xmm3 \n\t" // r0>>63:0
524 "pslldq $8, %%xmm4 \n\t" // r2>>63:0
525 "psrldq $8, %%xmm5 \n\t" // 0:r1>>63
526 "por %%xmm3, %%xmm1 \n\t" // r1<<1|r0>>63:r0<<1
527 "por %%xmm4, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1
528 "por %%xmm5, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1|r1>>63
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100529
530 /*
531 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
Gilles Peskine6055b782023-03-10 22:21:47 +0100532 * using [CLMUL-WP] algorithm 5 (p. 18).
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100533 * Currently xmm2:xmm1 holds x3:x2:x1:x0 (already shifted).
534 */
535 /* Step 2 (1) */
Gilles Peskine2808a602023-03-15 19:36:03 +0100536 "movdqa %%xmm1, %%xmm3 \n\t" // x1:x0
537 "movdqa %%xmm1, %%xmm4 \n\t" // same
538 "movdqa %%xmm1, %%xmm5 \n\t" // same
539 "psllq $63, %%xmm3 \n\t" // x1<<63:x0<<63 = stuff:a
540 "psllq $62, %%xmm4 \n\t" // x1<<62:x0<<62 = stuff:b
541 "psllq $57, %%xmm5 \n\t" // x1<<57:x0<<57 = stuff:c
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100542
543 /* Step 2 (2) */
Gilles Peskine2808a602023-03-15 19:36:03 +0100544 "pxor %%xmm4, %%xmm3 \n\t" // stuff:a+b
545 "pxor %%xmm5, %%xmm3 \n\t" // stuff:a+b+c
546 "pslldq $8, %%xmm3 \n\t" // a+b+c:0
547 "pxor %%xmm3, %%xmm1 \n\t" // x1+a+b+c:x0 = d:x0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100548
549 /* Steps 3 and 4 */
Gilles Peskine2808a602023-03-15 19:36:03 +0100550 "movdqa %%xmm1,%%xmm0 \n\t" // d:x0
551 "movdqa %%xmm1,%%xmm4 \n\t" // same
552 "movdqa %%xmm1,%%xmm5 \n\t" // same
553 "psrlq $1, %%xmm0 \n\t" // e1:x0>>1 = e1:e0'
554 "psrlq $2, %%xmm4 \n\t" // f1:x0>>2 = f1:f0'
555 "psrlq $7, %%xmm5 \n\t" // g1:x0>>7 = g1:g0'
556 "pxor %%xmm4, %%xmm0 \n\t" // e1+f1:e0'+f0'
557 "pxor %%xmm5, %%xmm0 \n\t" // e1+f1+g1:e0'+f0'+g0'
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200558 // e0'+f0'+g0' is almost e0+f0+g0, ex\tcept for some missing
559 // bits carried from d. Now get those\t bits back in.
Gilles Peskine2808a602023-03-15 19:36:03 +0100560 "movdqa %%xmm1,%%xmm3 \n\t" // d:x0
561 "movdqa %%xmm1,%%xmm4 \n\t" // same
562 "movdqa %%xmm1,%%xmm5 \n\t" // same
563 "psllq $63, %%xmm3 \n\t" // d<<63:stuff
564 "psllq $62, %%xmm4 \n\t" // d<<62:stuff
565 "psllq $57, %%xmm5 \n\t" // d<<57:stuff
566 "pxor %%xmm4, %%xmm3 \n\t" // d<<63+d<<62:stuff
567 "pxor %%xmm5, %%xmm3 \n\t" // missing bits of d:stuff
568 "psrldq $8, %%xmm3 \n\t" // 0:missing bits of d
569 "pxor %%xmm3, %%xmm0 \n\t" // e1+f1+g1:e0+f0+g0
570 "pxor %%xmm1, %%xmm0 \n\t" // h1:h0
571 "pxor %%xmm2, %%xmm0 \n\t" // x3+h1:x2+h0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100572
Gilles Peskine2808a602023-03-15 19:36:03 +0100573 "movdqu %%xmm0, (%2) \n\t" // done
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100574 :
575 : "r" (aa), "r" (bb), "r" (cc)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100576 : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100577
578 /* Now byte-reverse the outputs */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100579 for (i = 0; i < 16; i++) {
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100580 c[i] = cc[15 - i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100581 }
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100582
Paul Bakkere7f51332013-12-30 15:32:02 +0100583 return;
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100584}
585
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100586/*
587 * Compute decryption round keys from encryption round keys
588 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100589void mbedtls_aesni_inverse_key(unsigned char *invkey,
590 const unsigned char *fwdkey, int nr)
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100591{
592 unsigned char *ik = invkey;
593 const unsigned char *fk = fwdkey + 16 * nr;
594
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100595 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100596
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100597 for (fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16) {
598 asm ("movdqu (%0), %%xmm0 \n\t"
Gilles Peskine2808a602023-03-15 19:36:03 +0100599 AESIMC(xmm0_xmm0)
600 "movdqu %%xmm0, (%1) \n\t"
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100601 :
602 : "r" (fk), "r" (ik)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100603 : "memory", "xmm0");
604 }
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100605
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100606 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100607}
608
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100609/*
610 * Key expansion, 128-bit case
611 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100612static void aesni_setkey_enc_128(unsigned char *rk,
613 const unsigned char *key)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100614{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100615 asm ("movdqu (%1), %%xmm0 \n\t" // copy the original key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200616 "movdqu %%xmm0, (%0) \n\t" // as round key 0
617 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100618
619 /*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100620 * Finish generating the next round key.
621 *
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100622 * On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff
623 * with X = rot( sub( r3 ) ) ^ RCON.
624 *
625 * On exit, xmm0 is r7:r6:r5:r4
626 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
627 * and those are written to the round key buffer.
628 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200629 "1: \n\t"
630 "pshufd $0xff, %%xmm1, %%xmm1 \n\t" // X:X:X:X
631 "pxor %%xmm0, %%xmm1 \n\t" // X+r3:X+r2:X+r1:r4
632 "pslldq $4, %%xmm0 \n\t" // r2:r1:r0:0
633 "pxor %%xmm0, %%xmm1 \n\t" // X+r3+r2:X+r2+r1:r5:r4
634 "pslldq $4, %%xmm0 \n\t" // etc
635 "pxor %%xmm0, %%xmm1 \n\t"
636 "pslldq $4, %%xmm0 \n\t"
637 "pxor %%xmm1, %%xmm0 \n\t" // update xmm0 for next time!
638 "add $16, %0 \n\t" // point to next round key
639 "movdqu %%xmm0, (%0) \n\t" // write it
640 "ret \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100641
642 /* Main "loop" */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200643 "2: \n\t"
Gilles Peskine2808a602023-03-15 19:36:03 +0100644 AESKEYGENA(xmm0_xmm1, "0x01") "call 1b \n\t"
645 AESKEYGENA(xmm0_xmm1, "0x02") "call 1b \n\t"
646 AESKEYGENA(xmm0_xmm1, "0x04") "call 1b \n\t"
647 AESKEYGENA(xmm0_xmm1, "0x08") "call 1b \n\t"
648 AESKEYGENA(xmm0_xmm1, "0x10") "call 1b \n\t"
649 AESKEYGENA(xmm0_xmm1, "0x20") "call 1b \n\t"
650 AESKEYGENA(xmm0_xmm1, "0x40") "call 1b \n\t"
651 AESKEYGENA(xmm0_xmm1, "0x80") "call 1b \n\t"
652 AESKEYGENA(xmm0_xmm1, "0x1B") "call 1b \n\t"
653 AESKEYGENA(xmm0_xmm1, "0x36") "call 1b \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100654 :
655 : "r" (rk), "r" (key)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100656 : "memory", "cc", "0");
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100657}
658
659/*
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100660 * Key expansion, 192-bit case
661 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100662static void aesni_setkey_enc_192(unsigned char *rk,
663 const unsigned char *key)
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100664{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100665 asm ("movdqu (%1), %%xmm0 \n\t" // copy original round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200666 "movdqu %%xmm0, (%0) \n\t"
667 "add $16, %0 \n\t"
668 "movq 16(%1), %%xmm1 \n\t"
669 "movq %%xmm1, (%0) \n\t"
670 "add $8, %0 \n\t"
671 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100672
673 /*
674 * Finish generating the next 6 quarter-keys.
675 *
676 * On entry xmm0 is r3:r2:r1:r0, xmm1 is stuff:stuff:r5:r4
677 * and xmm2 is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON.
678 *
679 * On exit, xmm0 is r9:r8:r7:r6 and xmm1 is stuff:stuff:r11:r10
680 * and those are written to the round key buffer.
681 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200682 "1: \n\t"
683 "pshufd $0x55, %%xmm2, %%xmm2 \n\t" // X:X:X:X
684 "pxor %%xmm0, %%xmm2 \n\t" // X+r3:X+r2:X+r1:r4
685 "pslldq $4, %%xmm0 \n\t" // etc
686 "pxor %%xmm0, %%xmm2 \n\t"
687 "pslldq $4, %%xmm0 \n\t"
688 "pxor %%xmm0, %%xmm2 \n\t"
689 "pslldq $4, %%xmm0 \n\t"
690 "pxor %%xmm2, %%xmm0 \n\t" // update xmm0 = r9:r8:r7:r6
691 "movdqu %%xmm0, (%0) \n\t"
692 "add $16, %0 \n\t"
693 "pshufd $0xff, %%xmm0, %%xmm2 \n\t" // r9:r9:r9:r9
694 "pxor %%xmm1, %%xmm2 \n\t" // stuff:stuff:r9+r5:r10
695 "pslldq $4, %%xmm1 \n\t" // r2:r1:r0:0
696 "pxor %%xmm2, %%xmm1 \n\t" // xmm1 = stuff:stuff:r11:r10
697 "movq %%xmm1, (%0) \n\t"
698 "add $8, %0 \n\t"
699 "ret \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100700
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200701 "2: \n\t"
Gilles Peskine2808a602023-03-15 19:36:03 +0100702 AESKEYGENA(xmm1_xmm2, "0x01") "call 1b \n\t"
703 AESKEYGENA(xmm1_xmm2, "0x02") "call 1b \n\t"
704 AESKEYGENA(xmm1_xmm2, "0x04") "call 1b \n\t"
705 AESKEYGENA(xmm1_xmm2, "0x08") "call 1b \n\t"
706 AESKEYGENA(xmm1_xmm2, "0x10") "call 1b \n\t"
707 AESKEYGENA(xmm1_xmm2, "0x20") "call 1b \n\t"
708 AESKEYGENA(xmm1_xmm2, "0x40") "call 1b \n\t"
709 AESKEYGENA(xmm1_xmm2, "0x80") "call 1b \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100710
711 :
712 : "r" (rk), "r" (key)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100713 : "memory", "cc", "0");
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100714}
715
716/*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100717 * Key expansion, 256-bit case
718 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100719static void aesni_setkey_enc_256(unsigned char *rk,
720 const unsigned char *key)
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100721{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100722 asm ("movdqu (%1), %%xmm0 \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200723 "movdqu %%xmm0, (%0) \n\t"
724 "add $16, %0 \n\t"
725 "movdqu 16(%1), %%xmm1 \n\t"
726 "movdqu %%xmm1, (%0) \n\t"
727 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100728
729 /*
730 * Finish generating the next two round keys.
731 *
732 * On entry xmm0 is r3:r2:r1:r0, xmm1 is r7:r6:r5:r4 and
733 * xmm2 is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
734 *
735 * On exit, xmm0 is r11:r10:r9:r8 and xmm1 is r15:r14:r13:r12
736 * and those have been written to the output buffer.
737 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200738 "1: \n\t"
739 "pshufd $0xff, %%xmm2, %%xmm2 \n\t"
740 "pxor %%xmm0, %%xmm2 \n\t"
741 "pslldq $4, %%xmm0 \n\t"
742 "pxor %%xmm0, %%xmm2 \n\t"
743 "pslldq $4, %%xmm0 \n\t"
744 "pxor %%xmm0, %%xmm2 \n\t"
745 "pslldq $4, %%xmm0 \n\t"
746 "pxor %%xmm2, %%xmm0 \n\t"
747 "add $16, %0 \n\t"
748 "movdqu %%xmm0, (%0) \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100749
750 /* Set xmm2 to stuff:Y:stuff:stuff with Y = subword( r11 )
751 * and proceed to generate next round key from there */
Gilles Peskine2808a602023-03-15 19:36:03 +0100752 AESKEYGENA(xmm0_xmm2, "0x00")
753 "pshufd $0xaa, %%xmm2, %%xmm2 \n\t"
754 "pxor %%xmm1, %%xmm2 \n\t"
755 "pslldq $4, %%xmm1 \n\t"
756 "pxor %%xmm1, %%xmm2 \n\t"
757 "pslldq $4, %%xmm1 \n\t"
758 "pxor %%xmm1, %%xmm2 \n\t"
759 "pslldq $4, %%xmm1 \n\t"
760 "pxor %%xmm2, %%xmm1 \n\t"
761 "add $16, %0 \n\t"
762 "movdqu %%xmm1, (%0) \n\t"
763 "ret \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100764
765 /*
766 * Main "loop" - Generating one more key than necessary,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 * see definition of mbedtls_aes_context.buf
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100768 */
Gilles Peskine2808a602023-03-15 19:36:03 +0100769 "2: \n\t"
770 AESKEYGENA(xmm1_xmm2, "0x01") "call 1b \n\t"
771 AESKEYGENA(xmm1_xmm2, "0x02") "call 1b \n\t"
772 AESKEYGENA(xmm1_xmm2, "0x04") "call 1b \n\t"
773 AESKEYGENA(xmm1_xmm2, "0x08") "call 1b \n\t"
774 AESKEYGENA(xmm1_xmm2, "0x10") "call 1b \n\t"
775 AESKEYGENA(xmm1_xmm2, "0x20") "call 1b \n\t"
776 AESKEYGENA(xmm1_xmm2, "0x40") "call 1b \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100777 :
778 : "r" (rk), "r" (key)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100779 : "memory", "cc", "0");
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100780}
781
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100782#endif /* MBEDTLS_HAVE_AESNI_INTRINSICS */
783
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100784/*
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100785 * Key expansion, wrapper
786 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100787int mbedtls_aesni_setkey_enc(unsigned char *rk,
788 const unsigned char *key,
789 size_t bits)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100790{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100791 switch (bits) {
792 case 128: aesni_setkey_enc_128(rk, key); break;
793 case 192: aesni_setkey_enc_192(rk, key); break;
794 case 256: aesni_setkey_enc_256(rk, key); break;
795 default: return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100796 }
797
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100798 return 0;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100799}
800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801#endif /* MBEDTLS_HAVE_X86_64 */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803#endif /* MBEDTLS_AESNI_C */