blob: f7c99df5149362ead0873e37411e31e1f783311f [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 Peskine9c682e72023-03-16 17:21:33 +010033#if defined(MBEDTLS_AESNI_HAVE_CODE)
Gilles Peskined6719172023-03-10 22:37:11 +010034
Gilles Peskine9c682e72023-03-16 17:21:33 +010035#if MBEDTLS_AESNI_HAVE_CODE == 2
Tom Cosgrove02edb752023-03-13 15:32:52 +000036#if !defined(_WIN32)
Gilles Peskined6719172023-03-10 22:37:11 +010037#include <cpuid.h>
SlugFiller5ca3f0b2023-05-22 06:31:45 +030038#else
39#include <intrin.h>
Tom Cosgrove02edb752023-03-13 15:32:52 +000040#endif
Gilles Peskined6719172023-03-10 22:37:11 +010041#include <immintrin.h>
42#endif
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010043
Jerry Yu0a6272d2023-08-18 17:35:59 +080044#if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010045/*
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010046 * AES-NI support detection routine
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010047 */
Gilles Peskine449bd832023-01-11 14:50:10 +010048int mbedtls_aesni_has_support(unsigned int what)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010049{
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010050 static int done = 0;
51 static unsigned int c = 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010052
Gilles Peskine449bd832023-01-11 14:50:10 +010053 if (!done) {
Gilles Peskine9c682e72023-03-16 17:21:33 +010054#if MBEDTLS_AESNI_HAVE_CODE == 2
Gilles Peskined6719172023-03-10 22:37:11 +010055 static unsigned info[4] = { 0, 0, 0, 0 };
56#if defined(_MSC_VER)
57 __cpuid(info, 1);
58#else
59 __cpuid(1, info[0], info[1], info[2], info[3]);
60#endif
61 c = info[2];
Gilles Peskine9c682e72023-03-16 17:21:33 +010062#else /* AESNI using asm */
Gilles Peskine449bd832023-01-11 14:50:10 +010063 asm ("movl $1, %%eax \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +020064 "cpuid \n\t"
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010065 : "=c" (c)
66 :
Gilles Peskine449bd832023-01-11 14:50:10 +010067 : "eax", "ebx", "edx");
Gilles Peskine9c682e72023-03-16 17:21:33 +010068#endif /* MBEDTLS_AESNI_HAVE_CODE */
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010069 done = 1;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010070 }
71
Gilles Peskine449bd832023-01-11 14:50:10 +010072 return (c & what) != 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010073}
Jerry Yu36606232023-04-19 10:44:29 +080074#endif /* !MBEDTLS_AES_USE_HARDWARE_ONLY */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010075
Gilles Peskine9c682e72023-03-16 17:21:33 +010076#if MBEDTLS_AESNI_HAVE_CODE == 2
Gilles Peskined6719172023-03-10 22:37:11 +010077
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->buf + ctx->rk_offset);
87 unsigned nr = ctx->nr; // Number of remaining rounds
Tom Cosgrove02edb752023-03-13 15:32:52 +000088
Gilles Peskined6719172023-03-10 22:37:11 +010089 // Load round key 0
Gilles Peskinedafeee42023-03-15 20:37:57 +010090 __m128i state;
91 memcpy(&state, input, 16);
92 state = _mm_xor_si128(state, rk[0]); // state ^= *rk;
Gilles Peskined6719172023-03-10 22:37:11 +010093 ++rk;
94 --nr;
95
Yanray Wang380be5a2023-08-28 15:40:34 +080096#if !defined(MBEDTLS_CIPHER_ENCRYPT_ONLY)
97 if (mode == MBEDTLS_AES_ENCRYPT) {
Gilles Peskined6719172023-03-10 22:37:11 +010098 while (nr != 0) {
Gilles Peskinedafeee42023-03-15 20:37:57 +010099 state = _mm_aesenc_si128(state, *rk);
Gilles Peskined6719172023-03-10 22:37:11 +0100100 ++rk;
101 --nr;
102 }
Gilles Peskinedafeee42023-03-15 20:37:57 +0100103 state = _mm_aesenclast_si128(state, *rk);
Yanray Wang380be5a2023-08-28 15:40:34 +0800104 } else {
105 while (nr != 0) {
106 state = _mm_aesdec_si128(state, *rk);
107 ++rk;
108 --nr;
109 }
110 state = _mm_aesdeclast_si128(state, *rk);
Gilles Peskined6719172023-03-10 22:37:11 +0100111 }
Yanray Wang380be5a2023-08-28 15:40:34 +0800112#else
113 (void) mode;
114 while (nr != 0) {
115
116 state = _mm_aesenc_si128(state, *rk);
117 ++rk;
118 --nr;
119 }
120 state = _mm_aesenclast_si128(state, *rk);
121#endif /* !MBEDTLS_CIPHER_ENCRYPT_ONLY */
Gilles Peskined6719172023-03-10 22:37:11 +0100122
Gilles Peskinedafeee42023-03-15 20:37:57 +0100123 memcpy(output, &state, 16);
Gilles Peskined6719172023-03-10 22:37:11 +0100124 return 0;
125}
126
127/*
128 * GCM multiplication: c = a times b in GF(2^128)
129 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
130 */
131
132static void gcm_clmul(const __m128i aa, const __m128i bb,
133 __m128i *cc, __m128i *dd)
134{
135 /*
136 * Caryless multiplication dd:cc = aa * bb
137 * using [CLMUL-WP] algorithm 1 (p. 12).
138 */
139 *cc = _mm_clmulepi64_si128(aa, bb, 0x00); // a0*b0 = c1:c0
140 *dd = _mm_clmulepi64_si128(aa, bb, 0x11); // a1*b1 = d1:d0
141 __m128i ee = _mm_clmulepi64_si128(aa, bb, 0x10); // a0*b1 = e1:e0
142 __m128i ff = _mm_clmulepi64_si128(aa, bb, 0x01); // a1*b0 = f1:f0
Tom Cosgrove02edb752023-03-13 15:32:52 +0000143 ff = _mm_xor_si128(ff, ee); // e1+f1:e0+f0
Gilles Peskined6719172023-03-10 22:37:11 +0100144 ee = ff; // e1+f1:e0+f0
145 ff = _mm_srli_si128(ff, 8); // 0:e1+f1
146 ee = _mm_slli_si128(ee, 8); // e0+f0:0
Tom Cosgrove02edb752023-03-13 15:32:52 +0000147 *dd = _mm_xor_si128(*dd, ff); // d1:d0+e1+f1
Gilles Peskined0185f72023-03-16 13:08:18 +0100148 *cc = _mm_xor_si128(*cc, ee); // c1+e0+f0:c0
Gilles Peskined6719172023-03-10 22:37:11 +0100149}
150
151static void gcm_shift(__m128i *cc, __m128i *dd)
152{
Gilles Peskinedafeee42023-03-15 20:37:57 +0100153 /* [CMUCL-WP] Algorithm 5 Step 1: shift cc:dd one bit to the left,
154 * taking advantage of [CLMUL-WP] eq 27 (p. 18). */
155 // // *cc = r1:r0
156 // // *dd = r3:r2
157 __m128i cc_lo = _mm_slli_epi64(*cc, 1); // r1<<1:r0<<1
158 __m128i dd_lo = _mm_slli_epi64(*dd, 1); // r3<<1:r2<<1
159 __m128i cc_hi = _mm_srli_epi64(*cc, 63); // r1>>63:r0>>63
160 __m128i dd_hi = _mm_srli_epi64(*dd, 63); // r3>>63:r2>>63
161 __m128i xmm5 = _mm_srli_si128(cc_hi, 8); // 0:r1>>63
162 cc_hi = _mm_slli_si128(cc_hi, 8); // r0>>63:0
163 dd_hi = _mm_slli_si128(dd_hi, 8); // 0:r1>>63
Gilles Peskined6719172023-03-10 22:37:11 +0100164
Gilles Peskinedafeee42023-03-15 20:37:57 +0100165 *cc = _mm_or_si128(cc_lo, cc_hi); // r1<<1|r0>>63:r0<<1
166 *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 +0100167}
168
Gilles Peskinedafeee42023-03-15 20:37:57 +0100169static __m128i gcm_reduce(__m128i xx)
Gilles Peskined6719172023-03-10 22:37:11 +0100170{
171 // // xx = x1:x0
172 /* [CLMUL-WP] Algorithm 5 Step 2 */
173 __m128i aa = _mm_slli_epi64(xx, 63); // x1<<63:x0<<63 = stuff:a
174 __m128i bb = _mm_slli_epi64(xx, 62); // x1<<62:x0<<62 = stuff:b
175 __m128i cc = _mm_slli_epi64(xx, 57); // x1<<57:x0<<57 = stuff:c
Tom Cosgrove02edb752023-03-13 15:32:52 +0000176 __m128i dd = _mm_slli_si128(_mm_xor_si128(_mm_xor_si128(aa, bb), cc), 8); // a+b+c:0
177 return _mm_xor_si128(dd, xx); // x1+a+b+c:x0 = d:x0
Gilles Peskined6719172023-03-10 22:37:11 +0100178}
179
Gilles Peskinedafeee42023-03-15 20:37:57 +0100180static __m128i gcm_mix(__m128i dx)
Gilles Peskined6719172023-03-10 22:37:11 +0100181{
182 /* [CLMUL-WP] Algorithm 5 Steps 3 and 4 */
183 __m128i ee = _mm_srli_epi64(dx, 1); // e1:x0>>1 = e1:e0'
184 __m128i ff = _mm_srli_epi64(dx, 2); // f1:x0>>2 = f1:f0'
185 __m128i gg = _mm_srli_epi64(dx, 7); // g1:x0>>7 = g1:g0'
186
187 // e0'+f0'+g0' is almost e0+f0+g0, except for some missing
188 // bits carried from d. Now get those bits back in.
189 __m128i eh = _mm_slli_epi64(dx, 63); // d<<63:stuff
190 __m128i fh = _mm_slli_epi64(dx, 62); // d<<62:stuff
191 __m128i gh = _mm_slli_epi64(dx, 57); // d<<57:stuff
Tom Cosgrove02edb752023-03-13 15:32:52 +0000192 __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 +0100193
Tom Cosgrove02edb752023-03-13 15:32:52 +0000194 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 +0100195}
196
197void mbedtls_aesni_gcm_mult(unsigned char c[16],
198 const unsigned char a[16],
199 const unsigned char b[16])
200{
201 __m128i aa, bb, cc, dd;
202
203 /* The inputs are in big-endian order, so byte-reverse them */
204 for (size_t i = 0; i < 16; i++) {
205 ((uint8_t *) &aa)[i] = a[15 - i];
206 ((uint8_t *) &bb)[i] = b[15 - i];
207 }
208
209 gcm_clmul(aa, bb, &cc, &dd);
210 gcm_shift(&cc, &dd);
211 /*
212 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
213 * using [CLMUL-WP] algorithm 5 (p. 18).
214 * Currently dd:cc holds x3:x2:x1:x0 (already shifted).
215 */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100216 __m128i dx = gcm_reduce(cc);
217 __m128i xh = gcm_mix(dx);
Tom Cosgrove02edb752023-03-13 15:32:52 +0000218 cc = _mm_xor_si128(xh, dd); // x3+h1:x2+h0
Gilles Peskined6719172023-03-10 22:37:11 +0100219
220 /* Now byte-reverse the outputs */
221 for (size_t i = 0; i < 16; i++) {
222 c[i] = ((uint8_t *) &cc)[15 - i];
223 }
224
225 return;
226}
227
228/*
229 * Compute decryption round keys from encryption round keys
230 */
Yanray Wang380be5a2023-08-28 15:40:34 +0800231#if !defined(MBEDTLS_CIPHER_ENCRYPT_ONLY)
Gilles Peskined6719172023-03-10 22:37:11 +0100232void mbedtls_aesni_inverse_key(unsigned char *invkey,
233 const unsigned char *fwdkey, int nr)
234{
235 __m128i *ik = (__m128i *) invkey;
236 const __m128i *fk = (const __m128i *) fwdkey + nr;
237
238 *ik = *fk;
239 for (--fk, ++ik; fk > (const __m128i *) fwdkey; --fk, ++ik) {
240 *ik = _mm_aesimc_si128(*fk);
241 }
242 *ik = *fk;
243}
Yanray Wang380be5a2023-08-28 15:40:34 +0800244#endif
Gilles Peskined6719172023-03-10 22:37:11 +0100245
246/*
247 * Key expansion, 128-bit case
248 */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100249static __m128i aesni_set_rk_128(__m128i state, __m128i xword)
Gilles Peskined6719172023-03-10 22:37:11 +0100250{
251 /*
252 * Finish generating the next round key.
253 *
Gilles Peskinedafeee42023-03-15 20:37:57 +0100254 * On entry state is r3:r2:r1:r0 and xword is X:stuff:stuff:stuff
255 * with X = rot( sub( r3 ) ) ^ RCON (obtained with AESKEYGENASSIST).
Gilles Peskined6719172023-03-10 22:37:11 +0100256 *
Gilles Peskinedafeee42023-03-15 20:37:57 +0100257 * On exit, xword is r7:r6:r5:r4
Gilles Peskined6719172023-03-10 22:37:11 +0100258 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
259 * and this is returned, to be written to the round key buffer.
260 */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100261 xword = _mm_shuffle_epi32(xword, 0xff); // X:X:X:X
262 xword = _mm_xor_si128(xword, state); // X+r3:X+r2:X+r1:r4
263 state = _mm_slli_si128(state, 4); // r2:r1:r0:0
264 xword = _mm_xor_si128(xword, state); // X+r3+r2:X+r2+r1:r5:r4
265 state = _mm_slli_si128(state, 4); // r1:r0:0:0
266 xword = _mm_xor_si128(xword, state); // X+r3+r2+r1:r6:r5:r4
267 state = _mm_slli_si128(state, 4); // r0:0:0:0
268 state = _mm_xor_si128(xword, state); // r7:r6:r5:r4
269 return state;
Gilles Peskined6719172023-03-10 22:37:11 +0100270}
271
272static void aesni_setkey_enc_128(unsigned char *rk_bytes,
273 const unsigned char *key)
274{
275 __m128i *rk = (__m128i *) rk_bytes;
276
277 memcpy(&rk[0], key, 16);
278 rk[1] = aesni_set_rk_128(rk[0], _mm_aeskeygenassist_si128(rk[0], 0x01));
279 rk[2] = aesni_set_rk_128(rk[1], _mm_aeskeygenassist_si128(rk[1], 0x02));
280 rk[3] = aesni_set_rk_128(rk[2], _mm_aeskeygenassist_si128(rk[2], 0x04));
281 rk[4] = aesni_set_rk_128(rk[3], _mm_aeskeygenassist_si128(rk[3], 0x08));
282 rk[5] = aesni_set_rk_128(rk[4], _mm_aeskeygenassist_si128(rk[4], 0x10));
283 rk[6] = aesni_set_rk_128(rk[5], _mm_aeskeygenassist_si128(rk[5], 0x20));
284 rk[7] = aesni_set_rk_128(rk[6], _mm_aeskeygenassist_si128(rk[6], 0x40));
285 rk[8] = aesni_set_rk_128(rk[7], _mm_aeskeygenassist_si128(rk[7], 0x80));
286 rk[9] = aesni_set_rk_128(rk[8], _mm_aeskeygenassist_si128(rk[8], 0x1B));
287 rk[10] = aesni_set_rk_128(rk[9], _mm_aeskeygenassist_si128(rk[9], 0x36));
288}
289
290/*
291 * Key expansion, 192-bit case
292 */
Arto Kinnunen732ca322023-04-14 14:26:10 +0800293#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskinedafeee42023-03-15 20:37:57 +0100294static void aesni_set_rk_192(__m128i *state0, __m128i *state1, __m128i xword,
Gilles Peskined6719172023-03-10 22:37:11 +0100295 unsigned char *rk)
296{
297 /*
298 * Finish generating the next 6 quarter-keys.
299 *
Gilles Peskinedafeee42023-03-15 20:37:57 +0100300 * On entry state0 is r3:r2:r1:r0, state1 is stuff:stuff:r5:r4
301 * and xword is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON
302 * (obtained with AESKEYGENASSIST).
Gilles Peskined6719172023-03-10 22:37:11 +0100303 *
Gilles Peskinedafeee42023-03-15 20:37:57 +0100304 * On exit, state0 is r9:r8:r7:r6 and state1 is stuff:stuff:r11:r10
Gilles Peskined6719172023-03-10 22:37:11 +0100305 * and those are written to the round key buffer.
306 */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100307 xword = _mm_shuffle_epi32(xword, 0x55); // X:X:X:X
308 xword = _mm_xor_si128(xword, *state0); // X+r3:X+r2:X+r1:X+r0
309 *state0 = _mm_slli_si128(*state0, 4); // r2:r1:r0:0
310 xword = _mm_xor_si128(xword, *state0); // X+r3+r2:X+r2+r1:X+r1+r0:X+r0
311 *state0 = _mm_slli_si128(*state0, 4); // r1:r0:0:0
312 xword = _mm_xor_si128(xword, *state0); // X+r3+r2+r1:X+r2+r1+r0:X+r1+r0:X+r0
313 *state0 = _mm_slli_si128(*state0, 4); // r0:0:0:0
314 xword = _mm_xor_si128(xword, *state0); // X+r3+r2+r1+r0:X+r2+r1+r0:X+r1+r0:X+r0
315 *state0 = xword; // = r9:r8:r7:r6
Gilles Peskined6719172023-03-10 22:37:11 +0100316
Gilles Peskinedafeee42023-03-15 20:37:57 +0100317 xword = _mm_shuffle_epi32(xword, 0xff); // r9:r9:r9:r9
318 xword = _mm_xor_si128(xword, *state1); // stuff:stuff:r9+r5:r9+r4
319 *state1 = _mm_slli_si128(*state1, 4); // stuff:stuff:r4:0
320 xword = _mm_xor_si128(xword, *state1); // stuff:stuff:r9+r5+r4:r9+r4
321 *state1 = xword; // = stuff:stuff:r11:r10
Gilles Peskined6719172023-03-10 22:37:11 +0100322
Gilles Peskinedafeee42023-03-15 20:37:57 +0100323 /* Store state0 and the low half of state1 into rk, which is conceptually
Gilles Peskined6719172023-03-10 22:37:11 +0100324 * an array of 24-byte elements. Since 24 is not a multiple of 16,
Gilles Peskinedafeee42023-03-15 20:37:57 +0100325 * rk is not necessarily aligned so just `*rk = *state0` doesn't work. */
326 memcpy(rk, state0, 16);
Gilles Peskinedde3c652023-03-15 23:16:27 +0100327 memcpy(rk + 16, state1, 8);
Gilles Peskined6719172023-03-10 22:37:11 +0100328}
329
330static void aesni_setkey_enc_192(unsigned char *rk,
331 const unsigned char *key)
332{
333 /* First round: use original key */
334 memcpy(rk, key, 24);
335 /* aes.c guarantees that rk is aligned on a 16-byte boundary. */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100336 __m128i state0 = ((__m128i *) rk)[0];
337 __m128i state1 = _mm_loadl_epi64(((__m128i *) rk) + 1);
Gilles Peskined6719172023-03-10 22:37:11 +0100338
Gilles Peskinedafeee42023-03-15 20:37:57 +0100339 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x01), rk + 24 * 1);
340 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x02), rk + 24 * 2);
341 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x04), rk + 24 * 3);
342 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x08), rk + 24 * 4);
343 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x10), rk + 24 * 5);
344 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x20), rk + 24 * 6);
345 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x40), rk + 24 * 7);
346 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x80), rk + 24 * 8);
Gilles Peskined6719172023-03-10 22:37:11 +0100347}
Arto Kinnunen732ca322023-04-14 14:26:10 +0800348#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Gilles Peskined6719172023-03-10 22:37:11 +0100349
350/*
351 * Key expansion, 256-bit case
352 */
Arto Kinnunen732ca322023-04-14 14:26:10 +0800353#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskinedafeee42023-03-15 20:37:57 +0100354static void aesni_set_rk_256(__m128i state0, __m128i state1, __m128i xword,
Gilles Peskined6719172023-03-10 22:37:11 +0100355 __m128i *rk0, __m128i *rk1)
356{
357 /*
358 * Finish generating the next two round keys.
359 *
Gilles Peskinedafeee42023-03-15 20:37:57 +0100360 * On entry state0 is r3:r2:r1:r0, state1 is r7:r6:r5:r4 and
361 * xword is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
362 * (obtained with AESKEYGENASSIST).
Gilles Peskined6719172023-03-10 22:37:11 +0100363 *
364 * On exit, *rk0 is r11:r10:r9:r8 and *rk1 is r15:r14:r13:r12
365 */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100366 xword = _mm_shuffle_epi32(xword, 0xff);
367 xword = _mm_xor_si128(xword, state0);
368 state0 = _mm_slli_si128(state0, 4);
369 xword = _mm_xor_si128(xword, state0);
370 state0 = _mm_slli_si128(state0, 4);
371 xword = _mm_xor_si128(xword, state0);
372 state0 = _mm_slli_si128(state0, 4);
373 state0 = _mm_xor_si128(state0, xword);
374 *rk0 = state0;
Gilles Peskined6719172023-03-10 22:37:11 +0100375
Gilles Peskinedafeee42023-03-15 20:37:57 +0100376 /* Set xword to stuff:Y:stuff:stuff with Y = subword( r11 )
Gilles Peskined6719172023-03-10 22:37:11 +0100377 * and proceed to generate next round key from there */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100378 xword = _mm_aeskeygenassist_si128(state0, 0x00);
379 xword = _mm_shuffle_epi32(xword, 0xaa);
380 xword = _mm_xor_si128(xword, state1);
381 state1 = _mm_slli_si128(state1, 4);
382 xword = _mm_xor_si128(xword, state1);
383 state1 = _mm_slli_si128(state1, 4);
384 xword = _mm_xor_si128(xword, state1);
385 state1 = _mm_slli_si128(state1, 4);
386 state1 = _mm_xor_si128(state1, xword);
387 *rk1 = state1;
Gilles Peskined6719172023-03-10 22:37:11 +0100388}
389
390static void aesni_setkey_enc_256(unsigned char *rk_bytes,
391 const unsigned char *key)
392{
393 __m128i *rk = (__m128i *) rk_bytes;
394
395 memcpy(&rk[0], key, 16);
396 memcpy(&rk[1], key + 16, 16);
397
398 /*
399 * Main "loop" - Generating one more key than necessary,
400 * see definition of mbedtls_aes_context.buf
401 */
402 aesni_set_rk_256(rk[0], rk[1], _mm_aeskeygenassist_si128(rk[1], 0x01), &rk[2], &rk[3]);
403 aesni_set_rk_256(rk[2], rk[3], _mm_aeskeygenassist_si128(rk[3], 0x02), &rk[4], &rk[5]);
404 aesni_set_rk_256(rk[4], rk[5], _mm_aeskeygenassist_si128(rk[5], 0x04), &rk[6], &rk[7]);
405 aesni_set_rk_256(rk[6], rk[7], _mm_aeskeygenassist_si128(rk[7], 0x08), &rk[8], &rk[9]);
406 aesni_set_rk_256(rk[8], rk[9], _mm_aeskeygenassist_si128(rk[9], 0x10), &rk[10], &rk[11]);
407 aesni_set_rk_256(rk[10], rk[11], _mm_aeskeygenassist_si128(rk[11], 0x20), &rk[12], &rk[13]);
408 aesni_set_rk_256(rk[12], rk[13], _mm_aeskeygenassist_si128(rk[13], 0x40), &rk[14], &rk[15]);
409}
Arto Kinnunen732ca322023-04-14 14:26:10 +0800410#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Gilles Peskined6719172023-03-10 22:37:11 +0100411
Gilles Peskine9c682e72023-03-16 17:21:33 +0100412#else /* MBEDTLS_AESNI_HAVE_CODE == 1 */
Gilles Peskined6719172023-03-10 22:37:11 +0100413
Gilles Peskined0f9b0b2023-03-10 22:25:13 +0100414#if defined(__has_feature)
415#if __has_feature(memory_sanitizer)
416#warning \
417 "MBEDTLS_AESNI_C is known to cause spurious error reports with some memory sanitizers as they do not understand the assembly code."
418#endif
419#endif
420
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100421/*
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +0200422 * Binutils needs to be at least 2.19 to support AES-NI instructions.
423 * Unfortunately, a lot of users have a lower version now (2014-04).
424 * Emit bytecode directly in order to support "old" version of gas.
425 *
426 * Opcodes from the Intel architecture reference manual, vol. 3.
427 * We always use registers, so we don't need prefixes for memory operands.
428 * Operand macros are in gas order (src, dst) as opposed to Intel order
429 * (dst, src) in order to blend better into the surrounding assembly code.
430 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100431#define AESDEC(regs) ".byte 0x66,0x0F,0x38,0xDE," regs "\n\t"
432#define AESDECLAST(regs) ".byte 0x66,0x0F,0x38,0xDF," regs "\n\t"
433#define AESENC(regs) ".byte 0x66,0x0F,0x38,0xDC," regs "\n\t"
434#define AESENCLAST(regs) ".byte 0x66,0x0F,0x38,0xDD," regs "\n\t"
435#define AESIMC(regs) ".byte 0x66,0x0F,0x38,0xDB," regs "\n\t"
436#define AESKEYGENA(regs, imm) ".byte 0x66,0x0F,0x3A,0xDF," regs "," imm "\n\t"
437#define PCLMULQDQ(regs, imm) ".byte 0x66,0x0F,0x3A,0x44," regs "," imm "\n\t"
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +0200438
439#define xmm0_xmm0 "0xC0"
440#define xmm0_xmm1 "0xC8"
441#define xmm0_xmm2 "0xD0"
442#define xmm0_xmm3 "0xD8"
443#define xmm0_xmm4 "0xE0"
444#define xmm1_xmm0 "0xC1"
445#define xmm1_xmm2 "0xD1"
446
447/*
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100448 * AES-NI AES-ECB block en(de)cryption
449 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100450int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
451 int mode,
452 const unsigned char input[16],
453 unsigned char output[16])
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100454{
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 asm ("movdqu (%3), %%xmm0 \n\t" // load input
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200456 "movdqu (%1), %%xmm1 \n\t" // load round key 0
457 "pxor %%xmm1, %%xmm0 \n\t" // round 0
James Cowgill6c8edca2015-12-17 01:40:26 +0000458 "add $16, %1 \n\t" // point to next round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200459 "subl $1, %0 \n\t" // normal rounds = nr - 1
460 "test %2, %2 \n\t" // mode?
461 "jz 2f \n\t" // 0 = decrypt
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100462
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200463 "1: \n\t" // encryption loop
464 "movdqu (%1), %%xmm1 \n\t" // load round key
Gilles Peskine4e201442023-03-15 19:36:03 +0100465 AESENC(xmm1_xmm0) // do round
466 "add $16, %1 \n\t" // point to next round key
467 "subl $1, %0 \n\t" // loop
468 "jnz 1b \n\t"
469 "movdqu (%1), %%xmm1 \n\t" // load round key
470 AESENCLAST(xmm1_xmm0) // last round
Yanray Wang380be5a2023-08-28 15:40:34 +0800471#if !defined(MBEDTLS_CIPHER_ENCRYPT_ONLY)
Gilles Peskine4e201442023-03-15 19:36:03 +0100472 "jmp 3f \n\t"
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100473
Gilles Peskine4e201442023-03-15 19:36:03 +0100474 "2: \n\t" // decryption loop
475 "movdqu (%1), %%xmm1 \n\t"
476 AESDEC(xmm1_xmm0) // do round
477 "add $16, %1 \n\t"
478 "subl $1, %0 \n\t"
479 "jnz 2b \n\t"
480 "movdqu (%1), %%xmm1 \n\t" // load round key
481 AESDECLAST(xmm1_xmm0) // last round
Yanray Wang380be5a2023-08-28 15:40:34 +0800482#endif
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100483
Gilles Peskine4e201442023-03-15 19:36:03 +0100484 "3: \n\t"
485 "movdqu %%xmm0, (%4) \n\t" // export output
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100486 :
Werner Lewisdd76ef32022-05-30 12:00:21 +0100487 : "r" (ctx->nr), "r" (ctx->buf + ctx->rk_offset), "r" (mode), "r" (input), "r" (output)
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 : "memory", "cc", "xmm0", "xmm1");
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100489
490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 return 0;
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100492}
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100493
494/*
495 * GCM multiplication: c = a times b in GF(2^128)
496 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
497 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100498void mbedtls_aesni_gcm_mult(unsigned char c[16],
499 const unsigned char a[16],
500 const unsigned char b[16])
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100501{
502 unsigned char aa[16], bb[16], cc[16];
503 size_t i;
504
505 /* The inputs are in big-endian order, so byte-reverse them */
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 for (i = 0; i < 16; i++) {
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100507 aa[i] = a[15 - i];
508 bb[i] = b[15 - i];
509 }
510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 asm ("movdqu (%0), %%xmm0 \n\t" // a1:a0
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200512 "movdqu (%1), %%xmm1 \n\t" // b1:b0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100513
514 /*
515 * Caryless multiplication xmm2:xmm1 = xmm0 * xmm1
Gilles Peskined4f31c82023-03-10 22:21:47 +0100516 * using [CLMUL-WP] algorithm 1 (p. 12).
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100517 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200518 "movdqa %%xmm1, %%xmm2 \n\t" // copy of b1:b0
519 "movdqa %%xmm1, %%xmm3 \n\t" // same
520 "movdqa %%xmm1, %%xmm4 \n\t" // same
Gilles Peskine4e201442023-03-15 19:36:03 +0100521 PCLMULQDQ(xmm0_xmm1, "0x00") // a0*b0 = c1:c0
522 PCLMULQDQ(xmm0_xmm2, "0x11") // a1*b1 = d1:d0
523 PCLMULQDQ(xmm0_xmm3, "0x10") // a0*b1 = e1:e0
524 PCLMULQDQ(xmm0_xmm4, "0x01") // a1*b0 = f1:f0
525 "pxor %%xmm3, %%xmm4 \n\t" // e1+f1:e0+f0
526 "movdqa %%xmm4, %%xmm3 \n\t" // same
527 "psrldq $8, %%xmm4 \n\t" // 0:e1+f1
528 "pslldq $8, %%xmm3 \n\t" // e0+f0:0
529 "pxor %%xmm4, %%xmm2 \n\t" // d1:d0+e1+f1
530 "pxor %%xmm3, %%xmm1 \n\t" // c1+e0+f1:c0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100531
532 /*
533 * Now shift the result one bit to the left,
Gilles Peskined4f31c82023-03-10 22:21:47 +0100534 * taking advantage of [CLMUL-WP] eq 27 (p. 18)
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100535 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100536 "movdqa %%xmm1, %%xmm3 \n\t" // r1:r0
537 "movdqa %%xmm2, %%xmm4 \n\t" // r3:r2
538 "psllq $1, %%xmm1 \n\t" // r1<<1:r0<<1
539 "psllq $1, %%xmm2 \n\t" // r3<<1:r2<<1
540 "psrlq $63, %%xmm3 \n\t" // r1>>63:r0>>63
541 "psrlq $63, %%xmm4 \n\t" // r3>>63:r2>>63
542 "movdqa %%xmm3, %%xmm5 \n\t" // r1>>63:r0>>63
543 "pslldq $8, %%xmm3 \n\t" // r0>>63:0
544 "pslldq $8, %%xmm4 \n\t" // r2>>63:0
545 "psrldq $8, %%xmm5 \n\t" // 0:r1>>63
546 "por %%xmm3, %%xmm1 \n\t" // r1<<1|r0>>63:r0<<1
547 "por %%xmm4, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1
548 "por %%xmm5, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1|r1>>63
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100549
550 /*
551 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
Gilles Peskined4f31c82023-03-10 22:21:47 +0100552 * using [CLMUL-WP] algorithm 5 (p. 18).
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100553 * Currently xmm2:xmm1 holds x3:x2:x1:x0 (already shifted).
554 */
555 /* Step 2 (1) */
Gilles Peskine4e201442023-03-15 19:36:03 +0100556 "movdqa %%xmm1, %%xmm3 \n\t" // x1:x0
557 "movdqa %%xmm1, %%xmm4 \n\t" // same
558 "movdqa %%xmm1, %%xmm5 \n\t" // same
559 "psllq $63, %%xmm3 \n\t" // x1<<63:x0<<63 = stuff:a
560 "psllq $62, %%xmm4 \n\t" // x1<<62:x0<<62 = stuff:b
561 "psllq $57, %%xmm5 \n\t" // x1<<57:x0<<57 = stuff:c
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100562
563 /* Step 2 (2) */
Gilles Peskine4e201442023-03-15 19:36:03 +0100564 "pxor %%xmm4, %%xmm3 \n\t" // stuff:a+b
565 "pxor %%xmm5, %%xmm3 \n\t" // stuff:a+b+c
566 "pslldq $8, %%xmm3 \n\t" // a+b+c:0
567 "pxor %%xmm3, %%xmm1 \n\t" // x1+a+b+c:x0 = d:x0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100568
569 /* Steps 3 and 4 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100570 "movdqa %%xmm1,%%xmm0 \n\t" // d:x0
571 "movdqa %%xmm1,%%xmm4 \n\t" // same
572 "movdqa %%xmm1,%%xmm5 \n\t" // same
573 "psrlq $1, %%xmm0 \n\t" // e1:x0>>1 = e1:e0'
574 "psrlq $2, %%xmm4 \n\t" // f1:x0>>2 = f1:f0'
575 "psrlq $7, %%xmm5 \n\t" // g1:x0>>7 = g1:g0'
576 "pxor %%xmm4, %%xmm0 \n\t" // e1+f1:e0'+f0'
577 "pxor %%xmm5, %%xmm0 \n\t" // e1+f1+g1:e0'+f0'+g0'
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200578 // e0'+f0'+g0' is almost e0+f0+g0, ex\tcept for some missing
579 // bits carried from d. Now get those\t bits back in.
Gilles Peskine4e201442023-03-15 19:36:03 +0100580 "movdqa %%xmm1,%%xmm3 \n\t" // d:x0
581 "movdqa %%xmm1,%%xmm4 \n\t" // same
582 "movdqa %%xmm1,%%xmm5 \n\t" // same
583 "psllq $63, %%xmm3 \n\t" // d<<63:stuff
584 "psllq $62, %%xmm4 \n\t" // d<<62:stuff
585 "psllq $57, %%xmm5 \n\t" // d<<57:stuff
586 "pxor %%xmm4, %%xmm3 \n\t" // d<<63+d<<62:stuff
587 "pxor %%xmm5, %%xmm3 \n\t" // missing bits of d:stuff
588 "psrldq $8, %%xmm3 \n\t" // 0:missing bits of d
589 "pxor %%xmm3, %%xmm0 \n\t" // e1+f1+g1:e0+f0+g0
590 "pxor %%xmm1, %%xmm0 \n\t" // h1:h0
591 "pxor %%xmm2, %%xmm0 \n\t" // x3+h1:x2+h0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100592
Gilles Peskine4e201442023-03-15 19:36:03 +0100593 "movdqu %%xmm0, (%2) \n\t" // done
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100594 :
595 : "r" (aa), "r" (bb), "r" (cc)
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100597
598 /* Now byte-reverse the outputs */
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 for (i = 0; i < 16; i++) {
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100600 c[i] = cc[15 - i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 }
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100602
Paul Bakkere7f51332013-12-30 15:32:02 +0100603 return;
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100604}
605
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100606/*
607 * Compute decryption round keys from encryption round keys
608 */
Yanray Wang380be5a2023-08-28 15:40:34 +0800609#if !defined(MBEDTLS_CIPHER_ENCRYPT_ONLY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100610void mbedtls_aesni_inverse_key(unsigned char *invkey,
611 const unsigned char *fwdkey, int nr)
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100612{
613 unsigned char *ik = invkey;
614 const unsigned char *fk = fwdkey + 16 * nr;
615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100617
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 for (fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16) {
619 asm ("movdqu (%0), %%xmm0 \n\t"
Gilles Peskine4e201442023-03-15 19:36:03 +0100620 AESIMC(xmm0_xmm0)
621 "movdqu %%xmm0, (%1) \n\t"
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100622 :
623 : "r" (fk), "r" (ik)
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 : "memory", "xmm0");
625 }
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100626
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100628}
Yanray Wang380be5a2023-08-28 15:40:34 +0800629#endif
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100630
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100631/*
632 * Key expansion, 128-bit case
633 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100634static void aesni_setkey_enc_128(unsigned char *rk,
635 const unsigned char *key)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100636{
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 asm ("movdqu (%1), %%xmm0 \n\t" // copy the original key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200638 "movdqu %%xmm0, (%0) \n\t" // as round key 0
639 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100640
641 /*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100642 * Finish generating the next round key.
643 *
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100644 * On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff
645 * with X = rot( sub( r3 ) ) ^ RCON.
646 *
647 * On exit, xmm0 is r7:r6:r5:r4
648 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
649 * and those are written to the round key buffer.
650 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200651 "1: \n\t"
652 "pshufd $0xff, %%xmm1, %%xmm1 \n\t" // X:X:X:X
653 "pxor %%xmm0, %%xmm1 \n\t" // X+r3:X+r2:X+r1:r4
654 "pslldq $4, %%xmm0 \n\t" // r2:r1:r0:0
655 "pxor %%xmm0, %%xmm1 \n\t" // X+r3+r2:X+r2+r1:r5:r4
656 "pslldq $4, %%xmm0 \n\t" // etc
657 "pxor %%xmm0, %%xmm1 \n\t"
658 "pslldq $4, %%xmm0 \n\t"
659 "pxor %%xmm1, %%xmm0 \n\t" // update xmm0 for next time!
660 "add $16, %0 \n\t" // point to next round key
661 "movdqu %%xmm0, (%0) \n\t" // write it
662 "ret \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100663
664 /* Main "loop" */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200665 "2: \n\t"
Gilles Peskine4e201442023-03-15 19:36:03 +0100666 AESKEYGENA(xmm0_xmm1, "0x01") "call 1b \n\t"
667 AESKEYGENA(xmm0_xmm1, "0x02") "call 1b \n\t"
668 AESKEYGENA(xmm0_xmm1, "0x04") "call 1b \n\t"
669 AESKEYGENA(xmm0_xmm1, "0x08") "call 1b \n\t"
670 AESKEYGENA(xmm0_xmm1, "0x10") "call 1b \n\t"
671 AESKEYGENA(xmm0_xmm1, "0x20") "call 1b \n\t"
672 AESKEYGENA(xmm0_xmm1, "0x40") "call 1b \n\t"
673 AESKEYGENA(xmm0_xmm1, "0x80") "call 1b \n\t"
674 AESKEYGENA(xmm0_xmm1, "0x1B") "call 1b \n\t"
675 AESKEYGENA(xmm0_xmm1, "0x36") "call 1b \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100676 :
677 : "r" (rk), "r" (key)
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 : "memory", "cc", "0");
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100679}
680
681/*
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100682 * Key expansion, 192-bit case
683 */
Arto Kinnunen732ca322023-04-14 14:26:10 +0800684#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100685static void aesni_setkey_enc_192(unsigned char *rk,
686 const unsigned char *key)
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100687{
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 asm ("movdqu (%1), %%xmm0 \n\t" // copy original round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200689 "movdqu %%xmm0, (%0) \n\t"
690 "add $16, %0 \n\t"
691 "movq 16(%1), %%xmm1 \n\t"
692 "movq %%xmm1, (%0) \n\t"
693 "add $8, %0 \n\t"
694 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100695
696 /*
697 * Finish generating the next 6 quarter-keys.
698 *
699 * On entry xmm0 is r3:r2:r1:r0, xmm1 is stuff:stuff:r5:r4
700 * and xmm2 is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON.
701 *
702 * On exit, xmm0 is r9:r8:r7:r6 and xmm1 is stuff:stuff:r11:r10
703 * and those are written to the round key buffer.
704 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200705 "1: \n\t"
706 "pshufd $0x55, %%xmm2, %%xmm2 \n\t" // X:X:X:X
707 "pxor %%xmm0, %%xmm2 \n\t" // X+r3:X+r2:X+r1:r4
708 "pslldq $4, %%xmm0 \n\t" // etc
709 "pxor %%xmm0, %%xmm2 \n\t"
710 "pslldq $4, %%xmm0 \n\t"
711 "pxor %%xmm0, %%xmm2 \n\t"
712 "pslldq $4, %%xmm0 \n\t"
713 "pxor %%xmm2, %%xmm0 \n\t" // update xmm0 = r9:r8:r7:r6
714 "movdqu %%xmm0, (%0) \n\t"
715 "add $16, %0 \n\t"
716 "pshufd $0xff, %%xmm0, %%xmm2 \n\t" // r9:r9:r9:r9
717 "pxor %%xmm1, %%xmm2 \n\t" // stuff:stuff:r9+r5:r10
718 "pslldq $4, %%xmm1 \n\t" // r2:r1:r0:0
719 "pxor %%xmm2, %%xmm1 \n\t" // xmm1 = stuff:stuff:r11:r10
720 "movq %%xmm1, (%0) \n\t"
721 "add $8, %0 \n\t"
722 "ret \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100723
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200724 "2: \n\t"
Gilles Peskine4e201442023-03-15 19:36:03 +0100725 AESKEYGENA(xmm1_xmm2, "0x01") "call 1b \n\t"
726 AESKEYGENA(xmm1_xmm2, "0x02") "call 1b \n\t"
727 AESKEYGENA(xmm1_xmm2, "0x04") "call 1b \n\t"
728 AESKEYGENA(xmm1_xmm2, "0x08") "call 1b \n\t"
729 AESKEYGENA(xmm1_xmm2, "0x10") "call 1b \n\t"
730 AESKEYGENA(xmm1_xmm2, "0x20") "call 1b \n\t"
731 AESKEYGENA(xmm1_xmm2, "0x40") "call 1b \n\t"
732 AESKEYGENA(xmm1_xmm2, "0x80") "call 1b \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100733
734 :
735 : "r" (rk), "r" (key)
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 : "memory", "cc", "0");
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100737}
Arto Kinnunen732ca322023-04-14 14:26:10 +0800738#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100739
740/*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100741 * Key expansion, 256-bit case
742 */
Arto Kinnunen732ca322023-04-14 14:26:10 +0800743#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100744static void aesni_setkey_enc_256(unsigned char *rk,
745 const unsigned char *key)
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100746{
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 asm ("movdqu (%1), %%xmm0 \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200748 "movdqu %%xmm0, (%0) \n\t"
749 "add $16, %0 \n\t"
750 "movdqu 16(%1), %%xmm1 \n\t"
751 "movdqu %%xmm1, (%0) \n\t"
752 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100753
754 /*
755 * Finish generating the next two round keys.
756 *
757 * On entry xmm0 is r3:r2:r1:r0, xmm1 is r7:r6:r5:r4 and
758 * xmm2 is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
759 *
760 * On exit, xmm0 is r11:r10:r9:r8 and xmm1 is r15:r14:r13:r12
761 * and those have been written to the output buffer.
762 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200763 "1: \n\t"
764 "pshufd $0xff, %%xmm2, %%xmm2 \n\t"
765 "pxor %%xmm0, %%xmm2 \n\t"
766 "pslldq $4, %%xmm0 \n\t"
767 "pxor %%xmm0, %%xmm2 \n\t"
768 "pslldq $4, %%xmm0 \n\t"
769 "pxor %%xmm0, %%xmm2 \n\t"
770 "pslldq $4, %%xmm0 \n\t"
771 "pxor %%xmm2, %%xmm0 \n\t"
772 "add $16, %0 \n\t"
773 "movdqu %%xmm0, (%0) \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100774
775 /* Set xmm2 to stuff:Y:stuff:stuff with Y = subword( r11 )
776 * and proceed to generate next round key from there */
Gilles Peskine4e201442023-03-15 19:36:03 +0100777 AESKEYGENA(xmm0_xmm2, "0x00")
778 "pshufd $0xaa, %%xmm2, %%xmm2 \n\t"
779 "pxor %%xmm1, %%xmm2 \n\t"
780 "pslldq $4, %%xmm1 \n\t"
781 "pxor %%xmm1, %%xmm2 \n\t"
782 "pslldq $4, %%xmm1 \n\t"
783 "pxor %%xmm1, %%xmm2 \n\t"
784 "pslldq $4, %%xmm1 \n\t"
785 "pxor %%xmm2, %%xmm1 \n\t"
786 "add $16, %0 \n\t"
787 "movdqu %%xmm1, (%0) \n\t"
788 "ret \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100789
790 /*
791 * Main "loop" - Generating one more key than necessary,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 * see definition of mbedtls_aes_context.buf
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100793 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100794 "2: \n\t"
795 AESKEYGENA(xmm1_xmm2, "0x01") "call 1b \n\t"
796 AESKEYGENA(xmm1_xmm2, "0x02") "call 1b \n\t"
797 AESKEYGENA(xmm1_xmm2, "0x04") "call 1b \n\t"
798 AESKEYGENA(xmm1_xmm2, "0x08") "call 1b \n\t"
799 AESKEYGENA(xmm1_xmm2, "0x10") "call 1b \n\t"
800 AESKEYGENA(xmm1_xmm2, "0x20") "call 1b \n\t"
801 AESKEYGENA(xmm1_xmm2, "0x40") "call 1b \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100802 :
803 : "r" (rk), "r" (key)
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 : "memory", "cc", "0");
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100805}
Arto Kinnunen732ca322023-04-14 14:26:10 +0800806#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100807
Gilles Peskine9c682e72023-03-16 17:21:33 +0100808#endif /* MBEDTLS_AESNI_HAVE_CODE */
Gilles Peskined6719172023-03-10 22:37:11 +0100809
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100810/*
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100811 * Key expansion, wrapper
812 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100813int mbedtls_aesni_setkey_enc(unsigned char *rk,
814 const unsigned char *key,
815 size_t bits)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100816{
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 switch (bits) {
818 case 128: aesni_setkey_enc_128(rk, key); break;
Arto Kinnunen732ca322023-04-14 14:26:10 +0800819#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 case 192: aesni_setkey_enc_192(rk, key); break;
821 case 256: aesni_setkey_enc_256(rk, key); break;
Arto Kinnunen732ca322023-04-14 14:26:10 +0800822#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 default: return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100824 }
825
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 return 0;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100827}
828
Gilles Peskine9c682e72023-03-16 17:21:33 +0100829#endif /* MBEDTLS_AESNI_HAVE_CODE */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831#endif /* MBEDTLS_AESNI_C */