blob: c51957f6efb861697bc22303c1d0056f68a30927 [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
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +01006 */
7
8/*
Gilles Peskined4f31c82023-03-10 22:21:47 +01009 * [AES-WP] https://www.intel.com/content/www/us/en/developer/articles/tool/intel-advanced-encryption-standard-aes-instructions-set.html
10 * [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 +010011 */
12
Gilles Peskinedb09ef62020-06-03 01:43:33 +020013#include "common.h"
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020015#if defined(MBEDTLS_AESNI_C)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010016
Chris Jones187782f2021-03-09 17:28:35 +000017#include "aesni.h"
Rich Evans00ab4702015-02-06 13:43:58 +000018
19#include <string.h>
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010020
Gilles Peskine9c682e72023-03-16 17:21:33 +010021#if defined(MBEDTLS_AESNI_HAVE_CODE)
Gilles Peskined6719172023-03-10 22:37:11 +010022
Gilles Peskine9c682e72023-03-16 17:21:33 +010023#if MBEDTLS_AESNI_HAVE_CODE == 2
Pengyu Lv0ecb6352023-10-11 10:36:55 +080024#if defined(__GNUC__)
Gilles Peskined6719172023-03-10 22:37:11 +010025#include <cpuid.h>
Pengyu Lv0ecb6352023-10-11 10:36:55 +080026#elif defined(_MSC_VER)
SlugFiller5ca3f0b2023-05-22 06:31:45 +030027#include <intrin.h>
Pengyu Lv0ecb6352023-10-11 10:36:55 +080028#else
29#error "`__cpuid` required by MBEDTLS_AESNI_C is not supported by the compiler"
Tom Cosgrove02edb752023-03-13 15:32:52 +000030#endif
Gilles Peskined6719172023-03-10 22:37:11 +010031#include <immintrin.h>
32#endif
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010033
Beniamin Sandu800f2b72023-10-27 16:58:00 +010034#if defined(MBEDTLS_ARCH_IS_X86)
35#if defined(MBEDTLS_COMPILER_IS_GCC)
36#pragma GCC push_options
37#pragma GCC target ("pclmul,sse2,aes")
38#define MBEDTLS_POP_TARGET_PRAGMA
Dave Rodgmand47186d2023-12-19 13:11:08 +000039#elif defined(__clang__) && (__clang_major__ >= 5)
Beniamin Sandu800f2b72023-10-27 16:58:00 +010040#pragma clang attribute push (__attribute__((target("pclmul,sse2,aes"))), apply_to=function)
41#define MBEDTLS_POP_TARGET_PRAGMA
42#endif
43#endif
44
Jerry Yu0a6272d2023-08-18 17:35:59 +080045#if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010046/*
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 Peskine449bd832023-01-11 14:50:10 +010049int mbedtls_aesni_has_support(unsigned int what)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010050{
Gilles Peskine8c67ac02025-06-09 23:34:59 +020051 /* To avoid a race condition, tell the compiler that the assignment
52 * `done = 1` and the assignment to `c` may not be reordered.
53 * https://github.com/Mbed-TLS/mbedtls/issues/9840
54 */
55 static volatile int done = 0;
56 static volatile unsigned int c = 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010057
Gilles Peskine449bd832023-01-11 14:50:10 +010058 if (!done) {
Gilles Peskine9c682e72023-03-16 17:21:33 +010059#if MBEDTLS_AESNI_HAVE_CODE == 2
Pengyu Lve8c4bf12023-10-10 18:12:43 +080060 static int info[4] = { 0, 0, 0, 0 };
Gilles Peskined6719172023-03-10 22:37:11 +010061#if defined(_MSC_VER)
62 __cpuid(info, 1);
63#else
64 __cpuid(1, info[0], info[1], info[2], info[3]);
65#endif
66 c = info[2];
Gilles Peskine9c682e72023-03-16 17:21:33 +010067#else /* AESNI using asm */
Gilles Peskine449bd832023-01-11 14:50:10 +010068 asm ("movl $1, %%eax \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +020069 "cpuid \n\t"
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010070 : "=c" (c)
71 :
Gilles Peskine449bd832023-01-11 14:50:10 +010072 : "eax", "ebx", "edx");
Gilles Peskine9c682e72023-03-16 17:21:33 +010073#endif /* MBEDTLS_AESNI_HAVE_CODE */
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010074 done = 1;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010075 }
76
Gilles Peskine449bd832023-01-11 14:50:10 +010077 return (c & what) != 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010078}
Jerry Yu36606232023-04-19 10:44:29 +080079#endif /* !MBEDTLS_AES_USE_HARDWARE_ONLY */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010080
Gilles Peskine9c682e72023-03-16 17:21:33 +010081#if MBEDTLS_AESNI_HAVE_CODE == 2
Gilles Peskined6719172023-03-10 22:37:11 +010082
83/*
84 * AES-NI AES-ECB block en(de)cryption
85 */
86int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
87 int mode,
88 const unsigned char input[16],
89 unsigned char output[16])
90{
91 const __m128i *rk = (const __m128i *) (ctx->buf + ctx->rk_offset);
92 unsigned nr = ctx->nr; // Number of remaining rounds
Tom Cosgrove02edb752023-03-13 15:32:52 +000093
Gilles Peskined6719172023-03-10 22:37:11 +010094 // Load round key 0
Gilles Peskinedafeee42023-03-15 20:37:57 +010095 __m128i state;
96 memcpy(&state, input, 16);
97 state = _mm_xor_si128(state, rk[0]); // state ^= *rk;
Gilles Peskined6719172023-03-10 22:37:11 +010098 ++rk;
99 --nr;
100
Yanray Wang0d76b6e2023-11-02 11:54:39 +0800101#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
Yanray Wang111159b2023-11-10 13:41:12 +0800102 if (mode == MBEDTLS_AES_DECRYPT) {
Yanray Wang380be5a2023-08-28 15:40:34 +0800103 while (nr != 0) {
104 state = _mm_aesdec_si128(state, *rk);
105 ++rk;
106 --nr;
107 }
108 state = _mm_aesdeclast_si128(state, *rk);
Yanray Wang111159b2023-11-10 13:41:12 +0800109 } else
Yanray Wang380be5a2023-08-28 15:40:34 +0800110#else
Yanray Wang111159b2023-11-10 13:41:12 +0800111 (void) mode;
Yanray Wang0d76b6e2023-11-02 11:54:39 +0800112#endif
Yanray Wang111159b2023-11-10 13:41:12 +0800113 {
114 while (nr != 0) {
115 state = _mm_aesenc_si128(state, *rk);
116 ++rk;
117 --nr;
118 }
119 state = _mm_aesenclast_si128(state, *rk);
Yanray Wang380be5a2023-08-28 15:40:34 +0800120 }
Gilles Peskined6719172023-03-10 22:37:11 +0100121
Gilles Peskinedafeee42023-03-15 20:37:57 +0100122 memcpy(output, &state, 16);
Gilles Peskined6719172023-03-10 22:37:11 +0100123 return 0;
124}
125
126/*
127 * GCM multiplication: c = a times b in GF(2^128)
128 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
129 */
130
131static void gcm_clmul(const __m128i aa, const __m128i bb,
132 __m128i *cc, __m128i *dd)
133{
134 /*
135 * Caryless multiplication dd:cc = aa * bb
136 * using [CLMUL-WP] algorithm 1 (p. 12).
137 */
138 *cc = _mm_clmulepi64_si128(aa, bb, 0x00); // a0*b0 = c1:c0
139 *dd = _mm_clmulepi64_si128(aa, bb, 0x11); // a1*b1 = d1:d0
140 __m128i ee = _mm_clmulepi64_si128(aa, bb, 0x10); // a0*b1 = e1:e0
141 __m128i ff = _mm_clmulepi64_si128(aa, bb, 0x01); // a1*b0 = f1:f0
Tom Cosgrove02edb752023-03-13 15:32:52 +0000142 ff = _mm_xor_si128(ff, ee); // e1+f1:e0+f0
Gilles Peskined6719172023-03-10 22:37:11 +0100143 ee = ff; // e1+f1:e0+f0
144 ff = _mm_srli_si128(ff, 8); // 0:e1+f1
145 ee = _mm_slli_si128(ee, 8); // e0+f0:0
Tom Cosgrove02edb752023-03-13 15:32:52 +0000146 *dd = _mm_xor_si128(*dd, ff); // d1:d0+e1+f1
Gilles Peskined0185f72023-03-16 13:08:18 +0100147 *cc = _mm_xor_si128(*cc, ee); // c1+e0+f0:c0
Gilles Peskined6719172023-03-10 22:37:11 +0100148}
149
150static void gcm_shift(__m128i *cc, __m128i *dd)
151{
Gilles Peskinedafeee42023-03-15 20:37:57 +0100152 /* [CMUCL-WP] Algorithm 5 Step 1: shift cc:dd one bit to the left,
153 * taking advantage of [CLMUL-WP] eq 27 (p. 18). */
154 // // *cc = r1:r0
155 // // *dd = r3:r2
156 __m128i cc_lo = _mm_slli_epi64(*cc, 1); // r1<<1:r0<<1
157 __m128i dd_lo = _mm_slli_epi64(*dd, 1); // r3<<1:r2<<1
158 __m128i cc_hi = _mm_srli_epi64(*cc, 63); // r1>>63:r0>>63
159 __m128i dd_hi = _mm_srli_epi64(*dd, 63); // r3>>63:r2>>63
160 __m128i xmm5 = _mm_srli_si128(cc_hi, 8); // 0:r1>>63
161 cc_hi = _mm_slli_si128(cc_hi, 8); // r0>>63:0
162 dd_hi = _mm_slli_si128(dd_hi, 8); // 0:r1>>63
Gilles Peskined6719172023-03-10 22:37:11 +0100163
Gilles Peskinedafeee42023-03-15 20:37:57 +0100164 *cc = _mm_or_si128(cc_lo, cc_hi); // r1<<1|r0>>63:r0<<1
165 *dd = _mm_or_si128(_mm_or_si128(dd_lo, dd_hi), xmm5); // r3<<1|r2>>62:r2<<1|r1>>63
Gilles Peskined6719172023-03-10 22:37:11 +0100166}
167
Gilles Peskinedafeee42023-03-15 20:37:57 +0100168static __m128i gcm_reduce(__m128i xx)
Gilles Peskined6719172023-03-10 22:37:11 +0100169{
170 // // xx = x1:x0
171 /* [CLMUL-WP] Algorithm 5 Step 2 */
172 __m128i aa = _mm_slli_epi64(xx, 63); // x1<<63:x0<<63 = stuff:a
173 __m128i bb = _mm_slli_epi64(xx, 62); // x1<<62:x0<<62 = stuff:b
174 __m128i cc = _mm_slli_epi64(xx, 57); // x1<<57:x0<<57 = stuff:c
Tom Cosgrove02edb752023-03-13 15:32:52 +0000175 __m128i dd = _mm_slli_si128(_mm_xor_si128(_mm_xor_si128(aa, bb), cc), 8); // a+b+c:0
176 return _mm_xor_si128(dd, xx); // x1+a+b+c:x0 = d:x0
Gilles Peskined6719172023-03-10 22:37:11 +0100177}
178
Gilles Peskinedafeee42023-03-15 20:37:57 +0100179static __m128i gcm_mix(__m128i dx)
Gilles Peskined6719172023-03-10 22:37:11 +0100180{
181 /* [CLMUL-WP] Algorithm 5 Steps 3 and 4 */
182 __m128i ee = _mm_srli_epi64(dx, 1); // e1:x0>>1 = e1:e0'
183 __m128i ff = _mm_srli_epi64(dx, 2); // f1:x0>>2 = f1:f0'
184 __m128i gg = _mm_srli_epi64(dx, 7); // g1:x0>>7 = g1:g0'
185
186 // e0'+f0'+g0' is almost e0+f0+g0, except for some missing
187 // bits carried from d. Now get those bits back in.
188 __m128i eh = _mm_slli_epi64(dx, 63); // d<<63:stuff
189 __m128i fh = _mm_slli_epi64(dx, 62); // d<<62:stuff
190 __m128i gh = _mm_slli_epi64(dx, 57); // d<<57:stuff
Tom Cosgrove02edb752023-03-13 15:32:52 +0000191 __m128i hh = _mm_srli_si128(_mm_xor_si128(_mm_xor_si128(eh, fh), gh), 8); // 0:missing bits of d
Gilles Peskined6719172023-03-10 22:37:11 +0100192
Tom Cosgrove02edb752023-03-13 15:32:52 +0000193 return _mm_xor_si128(_mm_xor_si128(_mm_xor_si128(_mm_xor_si128(ee, ff), gg), hh), dx);
Gilles Peskined6719172023-03-10 22:37:11 +0100194}
195
196void mbedtls_aesni_gcm_mult(unsigned char c[16],
197 const unsigned char a[16],
198 const unsigned char b[16])
199{
Sergey Markelov3898f102023-10-16 12:54:48 -0700200 __m128i aa = { 0 }, bb = { 0 }, cc, dd;
Gilles Peskined6719172023-03-10 22:37:11 +0100201
202 /* The inputs are in big-endian order, so byte-reverse them */
203 for (size_t i = 0; i < 16; i++) {
204 ((uint8_t *) &aa)[i] = a[15 - i];
205 ((uint8_t *) &bb)[i] = b[15 - i];
206 }
207
208 gcm_clmul(aa, bb, &cc, &dd);
209 gcm_shift(&cc, &dd);
210 /*
211 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
212 * using [CLMUL-WP] algorithm 5 (p. 18).
213 * Currently dd:cc holds x3:x2:x1:x0 (already shifted).
214 */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100215 __m128i dx = gcm_reduce(cc);
216 __m128i xh = gcm_mix(dx);
Tom Cosgrove02edb752023-03-13 15:32:52 +0000217 cc = _mm_xor_si128(xh, dd); // x3+h1:x2+h0
Gilles Peskined6719172023-03-10 22:37:11 +0100218
219 /* Now byte-reverse the outputs */
220 for (size_t i = 0; i < 16; i++) {
221 c[i] = ((uint8_t *) &cc)[15 - i];
222 }
223
224 return;
225}
226
227/*
228 * Compute decryption round keys from encryption round keys
229 */
Yanray Wangb67b4742023-10-31 17:10:32 +0800230#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
Gilles Peskined6719172023-03-10 22:37:11 +0100231void mbedtls_aesni_inverse_key(unsigned char *invkey,
232 const unsigned char *fwdkey, int nr)
233{
234 __m128i *ik = (__m128i *) invkey;
235 const __m128i *fk = (const __m128i *) fwdkey + nr;
236
237 *ik = *fk;
238 for (--fk, ++ik; fk > (const __m128i *) fwdkey; --fk, ++ik) {
239 *ik = _mm_aesimc_si128(*fk);
240 }
241 *ik = *fk;
242}
Yanray Wang380be5a2023-08-28 15:40:34 +0800243#endif
Gilles Peskined6719172023-03-10 22:37:11 +0100244
245/*
246 * Key expansion, 128-bit case
247 */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100248static __m128i aesni_set_rk_128(__m128i state, __m128i xword)
Gilles Peskined6719172023-03-10 22:37:11 +0100249{
250 /*
251 * Finish generating the next round key.
252 *
Gilles Peskinedafeee42023-03-15 20:37:57 +0100253 * On entry state is r3:r2:r1:r0 and xword is X:stuff:stuff:stuff
254 * with X = rot( sub( r3 ) ) ^ RCON (obtained with AESKEYGENASSIST).
Gilles Peskined6719172023-03-10 22:37:11 +0100255 *
Gilles Peskinedafeee42023-03-15 20:37:57 +0100256 * On exit, xword is r7:r6:r5:r4
Gilles Peskined6719172023-03-10 22:37:11 +0100257 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
258 * and this is returned, to be written to the round key buffer.
259 */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100260 xword = _mm_shuffle_epi32(xword, 0xff); // X:X:X:X
261 xword = _mm_xor_si128(xword, state); // X+r3:X+r2:X+r1:r4
262 state = _mm_slli_si128(state, 4); // r2:r1:r0:0
263 xword = _mm_xor_si128(xword, state); // X+r3+r2:X+r2+r1:r5:r4
264 state = _mm_slli_si128(state, 4); // r1:r0:0:0
265 xword = _mm_xor_si128(xword, state); // X+r3+r2+r1:r6:r5:r4
266 state = _mm_slli_si128(state, 4); // r0:0:0:0
267 state = _mm_xor_si128(xword, state); // r7:r6:r5:r4
268 return state;
Gilles Peskined6719172023-03-10 22:37:11 +0100269}
270
271static void aesni_setkey_enc_128(unsigned char *rk_bytes,
272 const unsigned char *key)
273{
274 __m128i *rk = (__m128i *) rk_bytes;
275
276 memcpy(&rk[0], key, 16);
277 rk[1] = aesni_set_rk_128(rk[0], _mm_aeskeygenassist_si128(rk[0], 0x01));
278 rk[2] = aesni_set_rk_128(rk[1], _mm_aeskeygenassist_si128(rk[1], 0x02));
279 rk[3] = aesni_set_rk_128(rk[2], _mm_aeskeygenassist_si128(rk[2], 0x04));
280 rk[4] = aesni_set_rk_128(rk[3], _mm_aeskeygenassist_si128(rk[3], 0x08));
281 rk[5] = aesni_set_rk_128(rk[4], _mm_aeskeygenassist_si128(rk[4], 0x10));
282 rk[6] = aesni_set_rk_128(rk[5], _mm_aeskeygenassist_si128(rk[5], 0x20));
283 rk[7] = aesni_set_rk_128(rk[6], _mm_aeskeygenassist_si128(rk[6], 0x40));
284 rk[8] = aesni_set_rk_128(rk[7], _mm_aeskeygenassist_si128(rk[7], 0x80));
285 rk[9] = aesni_set_rk_128(rk[8], _mm_aeskeygenassist_si128(rk[8], 0x1B));
286 rk[10] = aesni_set_rk_128(rk[9], _mm_aeskeygenassist_si128(rk[9], 0x36));
287}
288
289/*
290 * Key expansion, 192-bit case
291 */
Arto Kinnunen732ca322023-04-14 14:26:10 +0800292#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskinedafeee42023-03-15 20:37:57 +0100293static void aesni_set_rk_192(__m128i *state0, __m128i *state1, __m128i xword,
Gilles Peskined6719172023-03-10 22:37:11 +0100294 unsigned char *rk)
295{
296 /*
297 * Finish generating the next 6 quarter-keys.
298 *
Gilles Peskinedafeee42023-03-15 20:37:57 +0100299 * On entry state0 is r3:r2:r1:r0, state1 is stuff:stuff:r5:r4
300 * and xword is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON
301 * (obtained with AESKEYGENASSIST).
Gilles Peskined6719172023-03-10 22:37:11 +0100302 *
Gilles Peskinedafeee42023-03-15 20:37:57 +0100303 * On exit, state0 is r9:r8:r7:r6 and state1 is stuff:stuff:r11:r10
Gilles Peskined6719172023-03-10 22:37:11 +0100304 * and those are written to the round key buffer.
305 */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100306 xword = _mm_shuffle_epi32(xword, 0x55); // X:X:X:X
307 xword = _mm_xor_si128(xword, *state0); // X+r3:X+r2:X+r1:X+r0
308 *state0 = _mm_slli_si128(*state0, 4); // r2:r1:r0:0
309 xword = _mm_xor_si128(xword, *state0); // X+r3+r2:X+r2+r1:X+r1+r0:X+r0
310 *state0 = _mm_slli_si128(*state0, 4); // r1:r0:0:0
311 xword = _mm_xor_si128(xword, *state0); // X+r3+r2+r1:X+r2+r1+r0:X+r1+r0:X+r0
312 *state0 = _mm_slli_si128(*state0, 4); // r0:0:0:0
313 xword = _mm_xor_si128(xword, *state0); // X+r3+r2+r1+r0:X+r2+r1+r0:X+r1+r0:X+r0
314 *state0 = xword; // = r9:r8:r7:r6
Gilles Peskined6719172023-03-10 22:37:11 +0100315
Gilles Peskinedafeee42023-03-15 20:37:57 +0100316 xword = _mm_shuffle_epi32(xword, 0xff); // r9:r9:r9:r9
317 xword = _mm_xor_si128(xword, *state1); // stuff:stuff:r9+r5:r9+r4
318 *state1 = _mm_slli_si128(*state1, 4); // stuff:stuff:r4:0
319 xword = _mm_xor_si128(xword, *state1); // stuff:stuff:r9+r5+r4:r9+r4
320 *state1 = xword; // = stuff:stuff:r11:r10
Gilles Peskined6719172023-03-10 22:37:11 +0100321
Gilles Peskinedafeee42023-03-15 20:37:57 +0100322 /* Store state0 and the low half of state1 into rk, which is conceptually
Gilles Peskined6719172023-03-10 22:37:11 +0100323 * an array of 24-byte elements. Since 24 is not a multiple of 16,
Gilles Peskinedafeee42023-03-15 20:37:57 +0100324 * rk is not necessarily aligned so just `*rk = *state0` doesn't work. */
325 memcpy(rk, state0, 16);
Gilles Peskinedde3c652023-03-15 23:16:27 +0100326 memcpy(rk + 16, state1, 8);
Gilles Peskined6719172023-03-10 22:37:11 +0100327}
328
329static void aesni_setkey_enc_192(unsigned char *rk,
330 const unsigned char *key)
331{
332 /* First round: use original key */
333 memcpy(rk, key, 24);
334 /* aes.c guarantees that rk is aligned on a 16-byte boundary. */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100335 __m128i state0 = ((__m128i *) rk)[0];
336 __m128i state1 = _mm_loadl_epi64(((__m128i *) rk) + 1);
Gilles Peskined6719172023-03-10 22:37:11 +0100337
Gilles Peskinedafeee42023-03-15 20:37:57 +0100338 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x01), rk + 24 * 1);
339 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x02), rk + 24 * 2);
340 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x04), rk + 24 * 3);
341 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x08), rk + 24 * 4);
342 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x10), rk + 24 * 5);
343 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x20), rk + 24 * 6);
344 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x40), rk + 24 * 7);
345 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x80), rk + 24 * 8);
Gilles Peskined6719172023-03-10 22:37:11 +0100346}
Arto Kinnunen732ca322023-04-14 14:26:10 +0800347#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Gilles Peskined6719172023-03-10 22:37:11 +0100348
349/*
350 * Key expansion, 256-bit case
351 */
Arto Kinnunen732ca322023-04-14 14:26:10 +0800352#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskinedafeee42023-03-15 20:37:57 +0100353static void aesni_set_rk_256(__m128i state0, __m128i state1, __m128i xword,
Gilles Peskined6719172023-03-10 22:37:11 +0100354 __m128i *rk0, __m128i *rk1)
355{
356 /*
357 * Finish generating the next two round keys.
358 *
Gilles Peskinedafeee42023-03-15 20:37:57 +0100359 * On entry state0 is r3:r2:r1:r0, state1 is r7:r6:r5:r4 and
360 * xword is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
361 * (obtained with AESKEYGENASSIST).
Gilles Peskined6719172023-03-10 22:37:11 +0100362 *
363 * On exit, *rk0 is r11:r10:r9:r8 and *rk1 is r15:r14:r13:r12
364 */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100365 xword = _mm_shuffle_epi32(xword, 0xff);
366 xword = _mm_xor_si128(xword, state0);
367 state0 = _mm_slli_si128(state0, 4);
368 xword = _mm_xor_si128(xword, state0);
369 state0 = _mm_slli_si128(state0, 4);
370 xword = _mm_xor_si128(xword, state0);
371 state0 = _mm_slli_si128(state0, 4);
372 state0 = _mm_xor_si128(state0, xword);
373 *rk0 = state0;
Gilles Peskined6719172023-03-10 22:37:11 +0100374
Gilles Peskinedafeee42023-03-15 20:37:57 +0100375 /* Set xword to stuff:Y:stuff:stuff with Y = subword( r11 )
Gilles Peskined6719172023-03-10 22:37:11 +0100376 * and proceed to generate next round key from there */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100377 xword = _mm_aeskeygenassist_si128(state0, 0x00);
378 xword = _mm_shuffle_epi32(xword, 0xaa);
379 xword = _mm_xor_si128(xword, state1);
380 state1 = _mm_slli_si128(state1, 4);
381 xword = _mm_xor_si128(xword, state1);
382 state1 = _mm_slli_si128(state1, 4);
383 xword = _mm_xor_si128(xword, state1);
384 state1 = _mm_slli_si128(state1, 4);
385 state1 = _mm_xor_si128(state1, xword);
386 *rk1 = state1;
Gilles Peskined6719172023-03-10 22:37:11 +0100387}
388
389static void aesni_setkey_enc_256(unsigned char *rk_bytes,
390 const unsigned char *key)
391{
392 __m128i *rk = (__m128i *) rk_bytes;
393
394 memcpy(&rk[0], key, 16);
395 memcpy(&rk[1], key + 16, 16);
396
397 /*
398 * Main "loop" - Generating one more key than necessary,
399 * see definition of mbedtls_aes_context.buf
400 */
401 aesni_set_rk_256(rk[0], rk[1], _mm_aeskeygenassist_si128(rk[1], 0x01), &rk[2], &rk[3]);
402 aesni_set_rk_256(rk[2], rk[3], _mm_aeskeygenassist_si128(rk[3], 0x02), &rk[4], &rk[5]);
403 aesni_set_rk_256(rk[4], rk[5], _mm_aeskeygenassist_si128(rk[5], 0x04), &rk[6], &rk[7]);
404 aesni_set_rk_256(rk[6], rk[7], _mm_aeskeygenassist_si128(rk[7], 0x08), &rk[8], &rk[9]);
405 aesni_set_rk_256(rk[8], rk[9], _mm_aeskeygenassist_si128(rk[9], 0x10), &rk[10], &rk[11]);
406 aesni_set_rk_256(rk[10], rk[11], _mm_aeskeygenassist_si128(rk[11], 0x20), &rk[12], &rk[13]);
407 aesni_set_rk_256(rk[12], rk[13], _mm_aeskeygenassist_si128(rk[13], 0x40), &rk[14], &rk[15]);
408}
Arto Kinnunen732ca322023-04-14 14:26:10 +0800409#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Gilles Peskined6719172023-03-10 22:37:11 +0100410
Beniamin Sandu800f2b72023-10-27 16:58:00 +0100411#if defined(MBEDTLS_POP_TARGET_PRAGMA)
412#if defined(__clang__)
413#pragma clang attribute pop
414#elif defined(__GNUC__)
415#pragma GCC pop_options
416#endif
417#undef MBEDTLS_POP_TARGET_PRAGMA
418#endif
419
Gilles Peskine9c682e72023-03-16 17:21:33 +0100420#else /* MBEDTLS_AESNI_HAVE_CODE == 1 */
Gilles Peskined6719172023-03-10 22:37:11 +0100421
Gilles Peskined0f9b0b2023-03-10 22:25:13 +0100422#if defined(__has_feature)
423#if __has_feature(memory_sanitizer)
424#warning \
425 "MBEDTLS_AESNI_C is known to cause spurious error reports with some memory sanitizers as they do not understand the assembly code."
426#endif
427#endif
428
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100429/*
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +0200430 * Binutils needs to be at least 2.19 to support AES-NI instructions.
431 * Unfortunately, a lot of users have a lower version now (2014-04).
432 * Emit bytecode directly in order to support "old" version of gas.
433 *
434 * Opcodes from the Intel architecture reference manual, vol. 3.
435 * We always use registers, so we don't need prefixes for memory operands.
436 * Operand macros are in gas order (src, dst) as opposed to Intel order
437 * (dst, src) in order to blend better into the surrounding assembly code.
438 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100439#define AESDEC(regs) ".byte 0x66,0x0F,0x38,0xDE," regs "\n\t"
440#define AESDECLAST(regs) ".byte 0x66,0x0F,0x38,0xDF," regs "\n\t"
441#define AESENC(regs) ".byte 0x66,0x0F,0x38,0xDC," regs "\n\t"
442#define AESENCLAST(regs) ".byte 0x66,0x0F,0x38,0xDD," regs "\n\t"
443#define AESIMC(regs) ".byte 0x66,0x0F,0x38,0xDB," regs "\n\t"
444#define AESKEYGENA(regs, imm) ".byte 0x66,0x0F,0x3A,0xDF," regs "," imm "\n\t"
445#define PCLMULQDQ(regs, imm) ".byte 0x66,0x0F,0x3A,0x44," regs "," imm "\n\t"
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +0200446
447#define xmm0_xmm0 "0xC0"
448#define xmm0_xmm1 "0xC8"
449#define xmm0_xmm2 "0xD0"
450#define xmm0_xmm3 "0xD8"
451#define xmm0_xmm4 "0xE0"
452#define xmm1_xmm0 "0xC1"
453#define xmm1_xmm2 "0xD1"
454
455/*
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100456 * AES-NI AES-ECB block en(de)cryption
457 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100458int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
459 int mode,
460 const unsigned char input[16],
461 unsigned char output[16])
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100462{
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 asm ("movdqu (%3), %%xmm0 \n\t" // load input
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200464 "movdqu (%1), %%xmm1 \n\t" // load round key 0
465 "pxor %%xmm1, %%xmm0 \n\t" // round 0
James Cowgill6c8edca2015-12-17 01:40:26 +0000466 "add $16, %1 \n\t" // point to next round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200467 "subl $1, %0 \n\t" // normal rounds = nr - 1
468 "test %2, %2 \n\t" // mode?
469 "jz 2f \n\t" // 0 = decrypt
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100470
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200471 "1: \n\t" // encryption loop
472 "movdqu (%1), %%xmm1 \n\t" // load round key
Gilles Peskine4e201442023-03-15 19:36:03 +0100473 AESENC(xmm1_xmm0) // do round
474 "add $16, %1 \n\t" // point to next round key
475 "subl $1, %0 \n\t" // loop
476 "jnz 1b \n\t"
477 "movdqu (%1), %%xmm1 \n\t" // load round key
478 AESENCLAST(xmm1_xmm0) // last round
Yanray Wangb67b4742023-10-31 17:10:32 +0800479#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
Gilles Peskine4e201442023-03-15 19:36:03 +0100480 "jmp 3f \n\t"
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100481
Gilles Peskine4e201442023-03-15 19:36:03 +0100482 "2: \n\t" // decryption loop
483 "movdqu (%1), %%xmm1 \n\t"
484 AESDEC(xmm1_xmm0) // do round
485 "add $16, %1 \n\t"
486 "subl $1, %0 \n\t"
487 "jnz 2b \n\t"
488 "movdqu (%1), %%xmm1 \n\t" // load round key
489 AESDECLAST(xmm1_xmm0) // last round
Yanray Wang380be5a2023-08-28 15:40:34 +0800490#endif
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100491
Gilles Peskine4e201442023-03-15 19:36:03 +0100492 "3: \n\t"
493 "movdqu %%xmm0, (%4) \n\t" // export output
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100494 :
Werner Lewisdd76ef32022-05-30 12:00:21 +0100495 : "r" (ctx->nr), "r" (ctx->buf + ctx->rk_offset), "r" (mode), "r" (input), "r" (output)
Solar Designer98910a12024-11-30 04:42:47 +0100496 : "memory", "cc", "xmm0", "xmm1", "0", "1");
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100497
498
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 return 0;
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100500}
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100501
502/*
503 * GCM multiplication: c = a times b in GF(2^128)
504 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
505 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100506void mbedtls_aesni_gcm_mult(unsigned char c[16],
507 const unsigned char a[16],
508 const unsigned char b[16])
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100509{
510 unsigned char aa[16], bb[16], cc[16];
511 size_t i;
512
513 /* The inputs are in big-endian order, so byte-reverse them */
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 for (i = 0; i < 16; i++) {
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100515 aa[i] = a[15 - i];
516 bb[i] = b[15 - i];
517 }
518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 asm ("movdqu (%0), %%xmm0 \n\t" // a1:a0
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200520 "movdqu (%1), %%xmm1 \n\t" // b1:b0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100521
522 /*
523 * Caryless multiplication xmm2:xmm1 = xmm0 * xmm1
Gilles Peskined4f31c82023-03-10 22:21:47 +0100524 * using [CLMUL-WP] algorithm 1 (p. 12).
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100525 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200526 "movdqa %%xmm1, %%xmm2 \n\t" // copy of b1:b0
527 "movdqa %%xmm1, %%xmm3 \n\t" // same
528 "movdqa %%xmm1, %%xmm4 \n\t" // same
Gilles Peskine4e201442023-03-15 19:36:03 +0100529 PCLMULQDQ(xmm0_xmm1, "0x00") // a0*b0 = c1:c0
530 PCLMULQDQ(xmm0_xmm2, "0x11") // a1*b1 = d1:d0
531 PCLMULQDQ(xmm0_xmm3, "0x10") // a0*b1 = e1:e0
532 PCLMULQDQ(xmm0_xmm4, "0x01") // a1*b0 = f1:f0
533 "pxor %%xmm3, %%xmm4 \n\t" // e1+f1:e0+f0
534 "movdqa %%xmm4, %%xmm3 \n\t" // same
535 "psrldq $8, %%xmm4 \n\t" // 0:e1+f1
536 "pslldq $8, %%xmm3 \n\t" // e0+f0:0
537 "pxor %%xmm4, %%xmm2 \n\t" // d1:d0+e1+f1
538 "pxor %%xmm3, %%xmm1 \n\t" // c1+e0+f1:c0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100539
540 /*
541 * Now shift the result one bit to the left,
Gilles Peskined4f31c82023-03-10 22:21:47 +0100542 * taking advantage of [CLMUL-WP] eq 27 (p. 18)
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100543 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100544 "movdqa %%xmm1, %%xmm3 \n\t" // r1:r0
545 "movdqa %%xmm2, %%xmm4 \n\t" // r3:r2
546 "psllq $1, %%xmm1 \n\t" // r1<<1:r0<<1
547 "psllq $1, %%xmm2 \n\t" // r3<<1:r2<<1
548 "psrlq $63, %%xmm3 \n\t" // r1>>63:r0>>63
549 "psrlq $63, %%xmm4 \n\t" // r3>>63:r2>>63
550 "movdqa %%xmm3, %%xmm5 \n\t" // r1>>63:r0>>63
551 "pslldq $8, %%xmm3 \n\t" // r0>>63:0
552 "pslldq $8, %%xmm4 \n\t" // r2>>63:0
553 "psrldq $8, %%xmm5 \n\t" // 0:r1>>63
554 "por %%xmm3, %%xmm1 \n\t" // r1<<1|r0>>63:r0<<1
555 "por %%xmm4, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1
556 "por %%xmm5, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1|r1>>63
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100557
558 /*
559 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
Gilles Peskined4f31c82023-03-10 22:21:47 +0100560 * using [CLMUL-WP] algorithm 5 (p. 18).
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100561 * Currently xmm2:xmm1 holds x3:x2:x1:x0 (already shifted).
562 */
563 /* Step 2 (1) */
Gilles Peskine4e201442023-03-15 19:36:03 +0100564 "movdqa %%xmm1, %%xmm3 \n\t" // x1:x0
565 "movdqa %%xmm1, %%xmm4 \n\t" // same
566 "movdqa %%xmm1, %%xmm5 \n\t" // same
567 "psllq $63, %%xmm3 \n\t" // x1<<63:x0<<63 = stuff:a
568 "psllq $62, %%xmm4 \n\t" // x1<<62:x0<<62 = stuff:b
569 "psllq $57, %%xmm5 \n\t" // x1<<57:x0<<57 = stuff:c
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100570
571 /* Step 2 (2) */
Gilles Peskine4e201442023-03-15 19:36:03 +0100572 "pxor %%xmm4, %%xmm3 \n\t" // stuff:a+b
573 "pxor %%xmm5, %%xmm3 \n\t" // stuff:a+b+c
574 "pslldq $8, %%xmm3 \n\t" // a+b+c:0
575 "pxor %%xmm3, %%xmm1 \n\t" // x1+a+b+c:x0 = d:x0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100576
577 /* Steps 3 and 4 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100578 "movdqa %%xmm1,%%xmm0 \n\t" // d:x0
579 "movdqa %%xmm1,%%xmm4 \n\t" // same
580 "movdqa %%xmm1,%%xmm5 \n\t" // same
581 "psrlq $1, %%xmm0 \n\t" // e1:x0>>1 = e1:e0'
582 "psrlq $2, %%xmm4 \n\t" // f1:x0>>2 = f1:f0'
583 "psrlq $7, %%xmm5 \n\t" // g1:x0>>7 = g1:g0'
584 "pxor %%xmm4, %%xmm0 \n\t" // e1+f1:e0'+f0'
585 "pxor %%xmm5, %%xmm0 \n\t" // e1+f1+g1:e0'+f0'+g0'
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200586 // e0'+f0'+g0' is almost e0+f0+g0, ex\tcept for some missing
587 // bits carried from d. Now get those\t bits back in.
Gilles Peskine4e201442023-03-15 19:36:03 +0100588 "movdqa %%xmm1,%%xmm3 \n\t" // d:x0
589 "movdqa %%xmm1,%%xmm4 \n\t" // same
590 "movdqa %%xmm1,%%xmm5 \n\t" // same
591 "psllq $63, %%xmm3 \n\t" // d<<63:stuff
592 "psllq $62, %%xmm4 \n\t" // d<<62:stuff
593 "psllq $57, %%xmm5 \n\t" // d<<57:stuff
594 "pxor %%xmm4, %%xmm3 \n\t" // d<<63+d<<62:stuff
595 "pxor %%xmm5, %%xmm3 \n\t" // missing bits of d:stuff
596 "psrldq $8, %%xmm3 \n\t" // 0:missing bits of d
597 "pxor %%xmm3, %%xmm0 \n\t" // e1+f1+g1:e0+f0+g0
598 "pxor %%xmm1, %%xmm0 \n\t" // h1:h0
599 "pxor %%xmm2, %%xmm0 \n\t" // x3+h1:x2+h0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100600
Gilles Peskine4e201442023-03-15 19:36:03 +0100601 "movdqu %%xmm0, (%2) \n\t" // done
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100602 :
603 : "r" (aa), "r" (bb), "r" (cc)
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100605
606 /* Now byte-reverse the outputs */
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 for (i = 0; i < 16; i++) {
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100608 c[i] = cc[15 - i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 }
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100610
Paul Bakkere7f51332013-12-30 15:32:02 +0100611 return;
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100612}
613
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100614/*
615 * Compute decryption round keys from encryption round keys
616 */
Yanray Wangb67b4742023-10-31 17:10:32 +0800617#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100618void mbedtls_aesni_inverse_key(unsigned char *invkey,
619 const unsigned char *fwdkey, int nr)
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100620{
621 unsigned char *ik = invkey;
622 const unsigned char *fk = fwdkey + 16 * nr;
623
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100625
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 for (fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16) {
627 asm ("movdqu (%0), %%xmm0 \n\t"
Gilles Peskine4e201442023-03-15 19:36:03 +0100628 AESIMC(xmm0_xmm0)
629 "movdqu %%xmm0, (%1) \n\t"
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100630 :
631 : "r" (fk), "r" (ik)
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 : "memory", "xmm0");
633 }
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100634
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100636}
Yanray Wang380be5a2023-08-28 15:40:34 +0800637#endif
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100638
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100639/*
640 * Key expansion, 128-bit case
641 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100642static void aesni_setkey_enc_128(unsigned char *rk,
643 const unsigned char *key)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100644{
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 asm ("movdqu (%1), %%xmm0 \n\t" // copy the original key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200646 "movdqu %%xmm0, (%0) \n\t" // as round key 0
647 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100648
649 /*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100650 * Finish generating the next round key.
651 *
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100652 * On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff
653 * with X = rot( sub( r3 ) ) ^ RCON.
654 *
655 * On exit, xmm0 is r7:r6:r5:r4
656 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
657 * and those are written to the round key buffer.
658 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200659 "1: \n\t"
660 "pshufd $0xff, %%xmm1, %%xmm1 \n\t" // X:X:X:X
661 "pxor %%xmm0, %%xmm1 \n\t" // X+r3:X+r2:X+r1:r4
662 "pslldq $4, %%xmm0 \n\t" // r2:r1:r0:0
663 "pxor %%xmm0, %%xmm1 \n\t" // X+r3+r2:X+r2+r1:r5:r4
664 "pslldq $4, %%xmm0 \n\t" // etc
665 "pxor %%xmm0, %%xmm1 \n\t"
666 "pslldq $4, %%xmm0 \n\t"
667 "pxor %%xmm1, %%xmm0 \n\t" // update xmm0 for next time!
668 "add $16, %0 \n\t" // point to next round key
669 "movdqu %%xmm0, (%0) \n\t" // write it
670 "ret \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100671
672 /* Main "loop" */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200673 "2: \n\t"
Gilles Peskine4e201442023-03-15 19:36:03 +0100674 AESKEYGENA(xmm0_xmm1, "0x01") "call 1b \n\t"
675 AESKEYGENA(xmm0_xmm1, "0x02") "call 1b \n\t"
676 AESKEYGENA(xmm0_xmm1, "0x04") "call 1b \n\t"
677 AESKEYGENA(xmm0_xmm1, "0x08") "call 1b \n\t"
678 AESKEYGENA(xmm0_xmm1, "0x10") "call 1b \n\t"
679 AESKEYGENA(xmm0_xmm1, "0x20") "call 1b \n\t"
680 AESKEYGENA(xmm0_xmm1, "0x40") "call 1b \n\t"
681 AESKEYGENA(xmm0_xmm1, "0x80") "call 1b \n\t"
682 AESKEYGENA(xmm0_xmm1, "0x1B") "call 1b \n\t"
683 AESKEYGENA(xmm0_xmm1, "0x36") "call 1b \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100684 :
685 : "r" (rk), "r" (key)
Solar Designere65428e2024-12-08 18:55:53 +0100686 : "memory", "cc", "xmm0", "xmm1", "0");
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100687}
688
689/*
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100690 * Key expansion, 192-bit case
691 */
Arto Kinnunen732ca322023-04-14 14:26:10 +0800692#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100693static void aesni_setkey_enc_192(unsigned char *rk,
694 const unsigned char *key)
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100695{
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 asm ("movdqu (%1), %%xmm0 \n\t" // copy original round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200697 "movdqu %%xmm0, (%0) \n\t"
698 "add $16, %0 \n\t"
699 "movq 16(%1), %%xmm1 \n\t"
700 "movq %%xmm1, (%0) \n\t"
701 "add $8, %0 \n\t"
702 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100703
704 /*
705 * Finish generating the next 6 quarter-keys.
706 *
707 * On entry xmm0 is r3:r2:r1:r0, xmm1 is stuff:stuff:r5:r4
708 * and xmm2 is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON.
709 *
710 * On exit, xmm0 is r9:r8:r7:r6 and xmm1 is stuff:stuff:r11:r10
711 * and those are written to the round key buffer.
712 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200713 "1: \n\t"
714 "pshufd $0x55, %%xmm2, %%xmm2 \n\t" // X:X:X:X
715 "pxor %%xmm0, %%xmm2 \n\t" // X+r3:X+r2:X+r1:r4
716 "pslldq $4, %%xmm0 \n\t" // etc
717 "pxor %%xmm0, %%xmm2 \n\t"
718 "pslldq $4, %%xmm0 \n\t"
719 "pxor %%xmm0, %%xmm2 \n\t"
720 "pslldq $4, %%xmm0 \n\t"
721 "pxor %%xmm2, %%xmm0 \n\t" // update xmm0 = r9:r8:r7:r6
722 "movdqu %%xmm0, (%0) \n\t"
723 "add $16, %0 \n\t"
724 "pshufd $0xff, %%xmm0, %%xmm2 \n\t" // r9:r9:r9:r9
725 "pxor %%xmm1, %%xmm2 \n\t" // stuff:stuff:r9+r5:r10
726 "pslldq $4, %%xmm1 \n\t" // r2:r1:r0:0
727 "pxor %%xmm2, %%xmm1 \n\t" // xmm1 = stuff:stuff:r11:r10
728 "movq %%xmm1, (%0) \n\t"
729 "add $8, %0 \n\t"
730 "ret \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100731
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200732 "2: \n\t"
Gilles Peskine4e201442023-03-15 19:36:03 +0100733 AESKEYGENA(xmm1_xmm2, "0x01") "call 1b \n\t"
734 AESKEYGENA(xmm1_xmm2, "0x02") "call 1b \n\t"
735 AESKEYGENA(xmm1_xmm2, "0x04") "call 1b \n\t"
736 AESKEYGENA(xmm1_xmm2, "0x08") "call 1b \n\t"
737 AESKEYGENA(xmm1_xmm2, "0x10") "call 1b \n\t"
738 AESKEYGENA(xmm1_xmm2, "0x20") "call 1b \n\t"
739 AESKEYGENA(xmm1_xmm2, "0x40") "call 1b \n\t"
740 AESKEYGENA(xmm1_xmm2, "0x80") "call 1b \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100741
742 :
743 : "r" (rk), "r" (key)
Solar Designere65428e2024-12-08 18:55:53 +0100744 : "memory", "cc", "xmm0", "xmm1", "xmm2", "0");
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100745}
Arto Kinnunen732ca322023-04-14 14:26:10 +0800746#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100747
748/*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100749 * Key expansion, 256-bit case
750 */
Arto Kinnunen732ca322023-04-14 14:26:10 +0800751#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100752static void aesni_setkey_enc_256(unsigned char *rk,
753 const unsigned char *key)
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100754{
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 asm ("movdqu (%1), %%xmm0 \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200756 "movdqu %%xmm0, (%0) \n\t"
757 "add $16, %0 \n\t"
758 "movdqu 16(%1), %%xmm1 \n\t"
759 "movdqu %%xmm1, (%0) \n\t"
760 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100761
762 /*
763 * Finish generating the next two round keys.
764 *
765 * On entry xmm0 is r3:r2:r1:r0, xmm1 is r7:r6:r5:r4 and
766 * xmm2 is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
767 *
768 * On exit, xmm0 is r11:r10:r9:r8 and xmm1 is r15:r14:r13:r12
769 * and those have been written to the output buffer.
770 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200771 "1: \n\t"
772 "pshufd $0xff, %%xmm2, %%xmm2 \n\t"
773 "pxor %%xmm0, %%xmm2 \n\t"
774 "pslldq $4, %%xmm0 \n\t"
775 "pxor %%xmm0, %%xmm2 \n\t"
776 "pslldq $4, %%xmm0 \n\t"
777 "pxor %%xmm0, %%xmm2 \n\t"
778 "pslldq $4, %%xmm0 \n\t"
779 "pxor %%xmm2, %%xmm0 \n\t"
780 "add $16, %0 \n\t"
781 "movdqu %%xmm0, (%0) \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100782
783 /* Set xmm2 to stuff:Y:stuff:stuff with Y = subword( r11 )
784 * and proceed to generate next round key from there */
Gilles Peskine4e201442023-03-15 19:36:03 +0100785 AESKEYGENA(xmm0_xmm2, "0x00")
786 "pshufd $0xaa, %%xmm2, %%xmm2 \n\t"
787 "pxor %%xmm1, %%xmm2 \n\t"
788 "pslldq $4, %%xmm1 \n\t"
789 "pxor %%xmm1, %%xmm2 \n\t"
790 "pslldq $4, %%xmm1 \n\t"
791 "pxor %%xmm1, %%xmm2 \n\t"
792 "pslldq $4, %%xmm1 \n\t"
793 "pxor %%xmm2, %%xmm1 \n\t"
794 "add $16, %0 \n\t"
795 "movdqu %%xmm1, (%0) \n\t"
796 "ret \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100797
798 /*
799 * Main "loop" - Generating one more key than necessary,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 * see definition of mbedtls_aes_context.buf
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100801 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100802 "2: \n\t"
803 AESKEYGENA(xmm1_xmm2, "0x01") "call 1b \n\t"
804 AESKEYGENA(xmm1_xmm2, "0x02") "call 1b \n\t"
805 AESKEYGENA(xmm1_xmm2, "0x04") "call 1b \n\t"
806 AESKEYGENA(xmm1_xmm2, "0x08") "call 1b \n\t"
807 AESKEYGENA(xmm1_xmm2, "0x10") "call 1b \n\t"
808 AESKEYGENA(xmm1_xmm2, "0x20") "call 1b \n\t"
809 AESKEYGENA(xmm1_xmm2, "0x40") "call 1b \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100810 :
811 : "r" (rk), "r" (key)
Solar Designere65428e2024-12-08 18:55:53 +0100812 : "memory", "cc", "xmm0", "xmm1", "xmm2", "0");
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100813}
Arto Kinnunen732ca322023-04-14 14:26:10 +0800814#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100815
Gilles Peskine9c682e72023-03-16 17:21:33 +0100816#endif /* MBEDTLS_AESNI_HAVE_CODE */
Gilles Peskined6719172023-03-10 22:37:11 +0100817
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100818/*
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100819 * Key expansion, wrapper
820 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100821int mbedtls_aesni_setkey_enc(unsigned char *rk,
822 const unsigned char *key,
823 size_t bits)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100824{
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 switch (bits) {
826 case 128: aesni_setkey_enc_128(rk, key); break;
Arto Kinnunen732ca322023-04-14 14:26:10 +0800827#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 case 192: aesni_setkey_enc_192(rk, key); break;
829 case 256: aesni_setkey_enc_256(rk, key); break;
Arto Kinnunen732ca322023-04-14 14:26:10 +0800830#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 default: return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100832 }
833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 return 0;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100835}
836
Gilles Peskine9c682e72023-03-16 17:21:33 +0100837#endif /* MBEDTLS_AESNI_HAVE_CODE */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839#endif /* MBEDTLS_AESNI_C */