blob: c68b081deaa0c7595e709d59be3f1ef3a86264f4 [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 Wang0d76b6e2023-11-02 11:54:39 +080096#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
Yanray Wang111159b2023-11-10 13:41:12 +080097 if (mode == MBEDTLS_AES_DECRYPT) {
Yanray Wang380be5a2023-08-28 15:40:34 +080098 while (nr != 0) {
99 state = _mm_aesdec_si128(state, *rk);
100 ++rk;
101 --nr;
102 }
103 state = _mm_aesdeclast_si128(state, *rk);
Yanray Wang111159b2023-11-10 13:41:12 +0800104 } else
Yanray Wang380be5a2023-08-28 15:40:34 +0800105#else
Yanray Wang111159b2023-11-10 13:41:12 +0800106 (void) mode;
Yanray Wang0d76b6e2023-11-02 11:54:39 +0800107#endif
Yanray Wang111159b2023-11-10 13:41:12 +0800108 {
109 while (nr != 0) {
110 state = _mm_aesenc_si128(state, *rk);
111 ++rk;
112 --nr;
113 }
114 state = _mm_aesenclast_si128(state, *rk);
Yanray Wang380be5a2023-08-28 15:40:34 +0800115 }
Gilles Peskined6719172023-03-10 22:37:11 +0100116
Gilles Peskinedafeee42023-03-15 20:37:57 +0100117 memcpy(output, &state, 16);
Gilles Peskined6719172023-03-10 22:37:11 +0100118 return 0;
119}
120
121/*
122 * GCM multiplication: c = a times b in GF(2^128)
123 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
124 */
125
126static void gcm_clmul(const __m128i aa, const __m128i bb,
127 __m128i *cc, __m128i *dd)
128{
129 /*
130 * Caryless multiplication dd:cc = aa * bb
131 * using [CLMUL-WP] algorithm 1 (p. 12).
132 */
133 *cc = _mm_clmulepi64_si128(aa, bb, 0x00); // a0*b0 = c1:c0
134 *dd = _mm_clmulepi64_si128(aa, bb, 0x11); // a1*b1 = d1:d0
135 __m128i ee = _mm_clmulepi64_si128(aa, bb, 0x10); // a0*b1 = e1:e0
136 __m128i ff = _mm_clmulepi64_si128(aa, bb, 0x01); // a1*b0 = f1:f0
Tom Cosgrove02edb752023-03-13 15:32:52 +0000137 ff = _mm_xor_si128(ff, ee); // e1+f1:e0+f0
Gilles Peskined6719172023-03-10 22:37:11 +0100138 ee = ff; // e1+f1:e0+f0
139 ff = _mm_srli_si128(ff, 8); // 0:e1+f1
140 ee = _mm_slli_si128(ee, 8); // e0+f0:0
Tom Cosgrove02edb752023-03-13 15:32:52 +0000141 *dd = _mm_xor_si128(*dd, ff); // d1:d0+e1+f1
Gilles Peskined0185f72023-03-16 13:08:18 +0100142 *cc = _mm_xor_si128(*cc, ee); // c1+e0+f0:c0
Gilles Peskined6719172023-03-10 22:37:11 +0100143}
144
145static void gcm_shift(__m128i *cc, __m128i *dd)
146{
Gilles Peskinedafeee42023-03-15 20:37:57 +0100147 /* [CMUCL-WP] Algorithm 5 Step 1: shift cc:dd one bit to the left,
148 * taking advantage of [CLMUL-WP] eq 27 (p. 18). */
149 // // *cc = r1:r0
150 // // *dd = r3:r2
151 __m128i cc_lo = _mm_slli_epi64(*cc, 1); // r1<<1:r0<<1
152 __m128i dd_lo = _mm_slli_epi64(*dd, 1); // r3<<1:r2<<1
153 __m128i cc_hi = _mm_srli_epi64(*cc, 63); // r1>>63:r0>>63
154 __m128i dd_hi = _mm_srli_epi64(*dd, 63); // r3>>63:r2>>63
155 __m128i xmm5 = _mm_srli_si128(cc_hi, 8); // 0:r1>>63
156 cc_hi = _mm_slli_si128(cc_hi, 8); // r0>>63:0
157 dd_hi = _mm_slli_si128(dd_hi, 8); // 0:r1>>63
Gilles Peskined6719172023-03-10 22:37:11 +0100158
Gilles Peskinedafeee42023-03-15 20:37:57 +0100159 *cc = _mm_or_si128(cc_lo, cc_hi); // r1<<1|r0>>63:r0<<1
160 *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 +0100161}
162
Gilles Peskinedafeee42023-03-15 20:37:57 +0100163static __m128i gcm_reduce(__m128i xx)
Gilles Peskined6719172023-03-10 22:37:11 +0100164{
165 // // xx = x1:x0
166 /* [CLMUL-WP] Algorithm 5 Step 2 */
167 __m128i aa = _mm_slli_epi64(xx, 63); // x1<<63:x0<<63 = stuff:a
168 __m128i bb = _mm_slli_epi64(xx, 62); // x1<<62:x0<<62 = stuff:b
169 __m128i cc = _mm_slli_epi64(xx, 57); // x1<<57:x0<<57 = stuff:c
Tom Cosgrove02edb752023-03-13 15:32:52 +0000170 __m128i dd = _mm_slli_si128(_mm_xor_si128(_mm_xor_si128(aa, bb), cc), 8); // a+b+c:0
171 return _mm_xor_si128(dd, xx); // x1+a+b+c:x0 = d:x0
Gilles Peskined6719172023-03-10 22:37:11 +0100172}
173
Gilles Peskinedafeee42023-03-15 20:37:57 +0100174static __m128i gcm_mix(__m128i dx)
Gilles Peskined6719172023-03-10 22:37:11 +0100175{
176 /* [CLMUL-WP] Algorithm 5 Steps 3 and 4 */
177 __m128i ee = _mm_srli_epi64(dx, 1); // e1:x0>>1 = e1:e0'
178 __m128i ff = _mm_srli_epi64(dx, 2); // f1:x0>>2 = f1:f0'
179 __m128i gg = _mm_srli_epi64(dx, 7); // g1:x0>>7 = g1:g0'
180
181 // e0'+f0'+g0' is almost e0+f0+g0, except for some missing
182 // bits carried from d. Now get those bits back in.
183 __m128i eh = _mm_slli_epi64(dx, 63); // d<<63:stuff
184 __m128i fh = _mm_slli_epi64(dx, 62); // d<<62:stuff
185 __m128i gh = _mm_slli_epi64(dx, 57); // d<<57:stuff
Tom Cosgrove02edb752023-03-13 15:32:52 +0000186 __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 +0100187
Tom Cosgrove02edb752023-03-13 15:32:52 +0000188 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 +0100189}
190
191void mbedtls_aesni_gcm_mult(unsigned char c[16],
192 const unsigned char a[16],
193 const unsigned char b[16])
194{
195 __m128i aa, bb, cc, dd;
196
197 /* The inputs are in big-endian order, so byte-reverse them */
198 for (size_t i = 0; i < 16; i++) {
199 ((uint8_t *) &aa)[i] = a[15 - i];
200 ((uint8_t *) &bb)[i] = b[15 - i];
201 }
202
203 gcm_clmul(aa, bb, &cc, &dd);
204 gcm_shift(&cc, &dd);
205 /*
206 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
207 * using [CLMUL-WP] algorithm 5 (p. 18).
208 * Currently dd:cc holds x3:x2:x1:x0 (already shifted).
209 */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100210 __m128i dx = gcm_reduce(cc);
211 __m128i xh = gcm_mix(dx);
Tom Cosgrove02edb752023-03-13 15:32:52 +0000212 cc = _mm_xor_si128(xh, dd); // x3+h1:x2+h0
Gilles Peskined6719172023-03-10 22:37:11 +0100213
214 /* Now byte-reverse the outputs */
215 for (size_t i = 0; i < 16; i++) {
216 c[i] = ((uint8_t *) &cc)[15 - i];
217 }
218
219 return;
220}
221
222/*
223 * Compute decryption round keys from encryption round keys
224 */
Yanray Wangb67b4742023-10-31 17:10:32 +0800225#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
Gilles Peskined6719172023-03-10 22:37:11 +0100226void mbedtls_aesni_inverse_key(unsigned char *invkey,
227 const unsigned char *fwdkey, int nr)
228{
229 __m128i *ik = (__m128i *) invkey;
230 const __m128i *fk = (const __m128i *) fwdkey + nr;
231
232 *ik = *fk;
233 for (--fk, ++ik; fk > (const __m128i *) fwdkey; --fk, ++ik) {
234 *ik = _mm_aesimc_si128(*fk);
235 }
236 *ik = *fk;
237}
Yanray Wang380be5a2023-08-28 15:40:34 +0800238#endif
Gilles Peskined6719172023-03-10 22:37:11 +0100239
240/*
241 * Key expansion, 128-bit case
242 */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100243static __m128i aesni_set_rk_128(__m128i state, __m128i xword)
Gilles Peskined6719172023-03-10 22:37:11 +0100244{
245 /*
246 * Finish generating the next round key.
247 *
Gilles Peskinedafeee42023-03-15 20:37:57 +0100248 * On entry state is r3:r2:r1:r0 and xword is X:stuff:stuff:stuff
249 * with X = rot( sub( r3 ) ) ^ RCON (obtained with AESKEYGENASSIST).
Gilles Peskined6719172023-03-10 22:37:11 +0100250 *
Gilles Peskinedafeee42023-03-15 20:37:57 +0100251 * On exit, xword is r7:r6:r5:r4
Gilles Peskined6719172023-03-10 22:37:11 +0100252 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
253 * and this is returned, to be written to the round key buffer.
254 */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100255 xword = _mm_shuffle_epi32(xword, 0xff); // X:X:X:X
256 xword = _mm_xor_si128(xword, state); // X+r3:X+r2:X+r1:r4
257 state = _mm_slli_si128(state, 4); // r2:r1:r0:0
258 xword = _mm_xor_si128(xword, state); // X+r3+r2:X+r2+r1:r5:r4
259 state = _mm_slli_si128(state, 4); // r1:r0:0:0
260 xword = _mm_xor_si128(xword, state); // X+r3+r2+r1:r6:r5:r4
261 state = _mm_slli_si128(state, 4); // r0:0:0:0
262 state = _mm_xor_si128(xword, state); // r7:r6:r5:r4
263 return state;
Gilles Peskined6719172023-03-10 22:37:11 +0100264}
265
266static void aesni_setkey_enc_128(unsigned char *rk_bytes,
267 const unsigned char *key)
268{
269 __m128i *rk = (__m128i *) rk_bytes;
270
271 memcpy(&rk[0], key, 16);
272 rk[1] = aesni_set_rk_128(rk[0], _mm_aeskeygenassist_si128(rk[0], 0x01));
273 rk[2] = aesni_set_rk_128(rk[1], _mm_aeskeygenassist_si128(rk[1], 0x02));
274 rk[3] = aesni_set_rk_128(rk[2], _mm_aeskeygenassist_si128(rk[2], 0x04));
275 rk[4] = aesni_set_rk_128(rk[3], _mm_aeskeygenassist_si128(rk[3], 0x08));
276 rk[5] = aesni_set_rk_128(rk[4], _mm_aeskeygenassist_si128(rk[4], 0x10));
277 rk[6] = aesni_set_rk_128(rk[5], _mm_aeskeygenassist_si128(rk[5], 0x20));
278 rk[7] = aesni_set_rk_128(rk[6], _mm_aeskeygenassist_si128(rk[6], 0x40));
279 rk[8] = aesni_set_rk_128(rk[7], _mm_aeskeygenassist_si128(rk[7], 0x80));
280 rk[9] = aesni_set_rk_128(rk[8], _mm_aeskeygenassist_si128(rk[8], 0x1B));
281 rk[10] = aesni_set_rk_128(rk[9], _mm_aeskeygenassist_si128(rk[9], 0x36));
282}
283
284/*
285 * Key expansion, 192-bit case
286 */
Arto Kinnunen732ca322023-04-14 14:26:10 +0800287#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskinedafeee42023-03-15 20:37:57 +0100288static void aesni_set_rk_192(__m128i *state0, __m128i *state1, __m128i xword,
Gilles Peskined6719172023-03-10 22:37:11 +0100289 unsigned char *rk)
290{
291 /*
292 * Finish generating the next 6 quarter-keys.
293 *
Gilles Peskinedafeee42023-03-15 20:37:57 +0100294 * On entry state0 is r3:r2:r1:r0, state1 is stuff:stuff:r5:r4
295 * and xword is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON
296 * (obtained with AESKEYGENASSIST).
Gilles Peskined6719172023-03-10 22:37:11 +0100297 *
Gilles Peskinedafeee42023-03-15 20:37:57 +0100298 * On exit, state0 is r9:r8:r7:r6 and state1 is stuff:stuff:r11:r10
Gilles Peskined6719172023-03-10 22:37:11 +0100299 * and those are written to the round key buffer.
300 */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100301 xword = _mm_shuffle_epi32(xword, 0x55); // X:X:X:X
302 xword = _mm_xor_si128(xword, *state0); // X+r3:X+r2:X+r1:X+r0
303 *state0 = _mm_slli_si128(*state0, 4); // r2:r1:r0:0
304 xword = _mm_xor_si128(xword, *state0); // X+r3+r2:X+r2+r1:X+r1+r0:X+r0
305 *state0 = _mm_slli_si128(*state0, 4); // r1:r0:0:0
306 xword = _mm_xor_si128(xword, *state0); // X+r3+r2+r1:X+r2+r1+r0:X+r1+r0:X+r0
307 *state0 = _mm_slli_si128(*state0, 4); // r0:0:0:0
308 xword = _mm_xor_si128(xword, *state0); // X+r3+r2+r1+r0:X+r2+r1+r0:X+r1+r0:X+r0
309 *state0 = xword; // = r9:r8:r7:r6
Gilles Peskined6719172023-03-10 22:37:11 +0100310
Gilles Peskinedafeee42023-03-15 20:37:57 +0100311 xword = _mm_shuffle_epi32(xword, 0xff); // r9:r9:r9:r9
312 xword = _mm_xor_si128(xword, *state1); // stuff:stuff:r9+r5:r9+r4
313 *state1 = _mm_slli_si128(*state1, 4); // stuff:stuff:r4:0
314 xword = _mm_xor_si128(xword, *state1); // stuff:stuff:r9+r5+r4:r9+r4
315 *state1 = xword; // = stuff:stuff:r11:r10
Gilles Peskined6719172023-03-10 22:37:11 +0100316
Gilles Peskinedafeee42023-03-15 20:37:57 +0100317 /* Store state0 and the low half of state1 into rk, which is conceptually
Gilles Peskined6719172023-03-10 22:37:11 +0100318 * an array of 24-byte elements. Since 24 is not a multiple of 16,
Gilles Peskinedafeee42023-03-15 20:37:57 +0100319 * rk is not necessarily aligned so just `*rk = *state0` doesn't work. */
320 memcpy(rk, state0, 16);
Gilles Peskinedde3c652023-03-15 23:16:27 +0100321 memcpy(rk + 16, state1, 8);
Gilles Peskined6719172023-03-10 22:37:11 +0100322}
323
324static void aesni_setkey_enc_192(unsigned char *rk,
325 const unsigned char *key)
326{
327 /* First round: use original key */
328 memcpy(rk, key, 24);
329 /* aes.c guarantees that rk is aligned on a 16-byte boundary. */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100330 __m128i state0 = ((__m128i *) rk)[0];
331 __m128i state1 = _mm_loadl_epi64(((__m128i *) rk) + 1);
Gilles Peskined6719172023-03-10 22:37:11 +0100332
Gilles Peskinedafeee42023-03-15 20:37:57 +0100333 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x01), rk + 24 * 1);
334 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x02), rk + 24 * 2);
335 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x04), rk + 24 * 3);
336 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x08), rk + 24 * 4);
337 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x10), rk + 24 * 5);
338 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x20), rk + 24 * 6);
339 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x40), rk + 24 * 7);
340 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x80), rk + 24 * 8);
Gilles Peskined6719172023-03-10 22:37:11 +0100341}
Arto Kinnunen732ca322023-04-14 14:26:10 +0800342#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Gilles Peskined6719172023-03-10 22:37:11 +0100343
344/*
345 * Key expansion, 256-bit case
346 */
Arto Kinnunen732ca322023-04-14 14:26:10 +0800347#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskinedafeee42023-03-15 20:37:57 +0100348static void aesni_set_rk_256(__m128i state0, __m128i state1, __m128i xword,
Gilles Peskined6719172023-03-10 22:37:11 +0100349 __m128i *rk0, __m128i *rk1)
350{
351 /*
352 * Finish generating the next two round keys.
353 *
Gilles Peskinedafeee42023-03-15 20:37:57 +0100354 * On entry state0 is r3:r2:r1:r0, state1 is r7:r6:r5:r4 and
355 * xword is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
356 * (obtained with AESKEYGENASSIST).
Gilles Peskined6719172023-03-10 22:37:11 +0100357 *
358 * On exit, *rk0 is r11:r10:r9:r8 and *rk1 is r15:r14:r13:r12
359 */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100360 xword = _mm_shuffle_epi32(xword, 0xff);
361 xword = _mm_xor_si128(xword, state0);
362 state0 = _mm_slli_si128(state0, 4);
363 xword = _mm_xor_si128(xword, state0);
364 state0 = _mm_slli_si128(state0, 4);
365 xword = _mm_xor_si128(xword, state0);
366 state0 = _mm_slli_si128(state0, 4);
367 state0 = _mm_xor_si128(state0, xword);
368 *rk0 = state0;
Gilles Peskined6719172023-03-10 22:37:11 +0100369
Gilles Peskinedafeee42023-03-15 20:37:57 +0100370 /* Set xword to stuff:Y:stuff:stuff with Y = subword( r11 )
Gilles Peskined6719172023-03-10 22:37:11 +0100371 * and proceed to generate next round key from there */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100372 xword = _mm_aeskeygenassist_si128(state0, 0x00);
373 xword = _mm_shuffle_epi32(xword, 0xaa);
374 xword = _mm_xor_si128(xword, state1);
375 state1 = _mm_slli_si128(state1, 4);
376 xword = _mm_xor_si128(xword, state1);
377 state1 = _mm_slli_si128(state1, 4);
378 xword = _mm_xor_si128(xword, state1);
379 state1 = _mm_slli_si128(state1, 4);
380 state1 = _mm_xor_si128(state1, xword);
381 *rk1 = state1;
Gilles Peskined6719172023-03-10 22:37:11 +0100382}
383
384static void aesni_setkey_enc_256(unsigned char *rk_bytes,
385 const unsigned char *key)
386{
387 __m128i *rk = (__m128i *) rk_bytes;
388
389 memcpy(&rk[0], key, 16);
390 memcpy(&rk[1], key + 16, 16);
391
392 /*
393 * Main "loop" - Generating one more key than necessary,
394 * see definition of mbedtls_aes_context.buf
395 */
396 aesni_set_rk_256(rk[0], rk[1], _mm_aeskeygenassist_si128(rk[1], 0x01), &rk[2], &rk[3]);
397 aesni_set_rk_256(rk[2], rk[3], _mm_aeskeygenassist_si128(rk[3], 0x02), &rk[4], &rk[5]);
398 aesni_set_rk_256(rk[4], rk[5], _mm_aeskeygenassist_si128(rk[5], 0x04), &rk[6], &rk[7]);
399 aesni_set_rk_256(rk[6], rk[7], _mm_aeskeygenassist_si128(rk[7], 0x08), &rk[8], &rk[9]);
400 aesni_set_rk_256(rk[8], rk[9], _mm_aeskeygenassist_si128(rk[9], 0x10), &rk[10], &rk[11]);
401 aesni_set_rk_256(rk[10], rk[11], _mm_aeskeygenassist_si128(rk[11], 0x20), &rk[12], &rk[13]);
402 aesni_set_rk_256(rk[12], rk[13], _mm_aeskeygenassist_si128(rk[13], 0x40), &rk[14], &rk[15]);
403}
Arto Kinnunen732ca322023-04-14 14:26:10 +0800404#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Gilles Peskined6719172023-03-10 22:37:11 +0100405
Gilles Peskine9c682e72023-03-16 17:21:33 +0100406#else /* MBEDTLS_AESNI_HAVE_CODE == 1 */
Gilles Peskined6719172023-03-10 22:37:11 +0100407
Gilles Peskined0f9b0b2023-03-10 22:25:13 +0100408#if defined(__has_feature)
409#if __has_feature(memory_sanitizer)
410#warning \
411 "MBEDTLS_AESNI_C is known to cause spurious error reports with some memory sanitizers as they do not understand the assembly code."
412#endif
413#endif
414
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100415/*
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +0200416 * Binutils needs to be at least 2.19 to support AES-NI instructions.
417 * Unfortunately, a lot of users have a lower version now (2014-04).
418 * Emit bytecode directly in order to support "old" version of gas.
419 *
420 * Opcodes from the Intel architecture reference manual, vol. 3.
421 * We always use registers, so we don't need prefixes for memory operands.
422 * Operand macros are in gas order (src, dst) as opposed to Intel order
423 * (dst, src) in order to blend better into the surrounding assembly code.
424 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100425#define AESDEC(regs) ".byte 0x66,0x0F,0x38,0xDE," regs "\n\t"
426#define AESDECLAST(regs) ".byte 0x66,0x0F,0x38,0xDF," regs "\n\t"
427#define AESENC(regs) ".byte 0x66,0x0F,0x38,0xDC," regs "\n\t"
428#define AESENCLAST(regs) ".byte 0x66,0x0F,0x38,0xDD," regs "\n\t"
429#define AESIMC(regs) ".byte 0x66,0x0F,0x38,0xDB," regs "\n\t"
430#define AESKEYGENA(regs, imm) ".byte 0x66,0x0F,0x3A,0xDF," regs "," imm "\n\t"
431#define PCLMULQDQ(regs, imm) ".byte 0x66,0x0F,0x3A,0x44," regs "," imm "\n\t"
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +0200432
433#define xmm0_xmm0 "0xC0"
434#define xmm0_xmm1 "0xC8"
435#define xmm0_xmm2 "0xD0"
436#define xmm0_xmm3 "0xD8"
437#define xmm0_xmm4 "0xE0"
438#define xmm1_xmm0 "0xC1"
439#define xmm1_xmm2 "0xD1"
440
441/*
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100442 * AES-NI AES-ECB block en(de)cryption
443 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100444int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
445 int mode,
446 const unsigned char input[16],
447 unsigned char output[16])
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100448{
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 asm ("movdqu (%3), %%xmm0 \n\t" // load input
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200450 "movdqu (%1), %%xmm1 \n\t" // load round key 0
451 "pxor %%xmm1, %%xmm0 \n\t" // round 0
James Cowgill6c8edca2015-12-17 01:40:26 +0000452 "add $16, %1 \n\t" // point to next round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200453 "subl $1, %0 \n\t" // normal rounds = nr - 1
454 "test %2, %2 \n\t" // mode?
455 "jz 2f \n\t" // 0 = decrypt
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100456
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200457 "1: \n\t" // encryption loop
458 "movdqu (%1), %%xmm1 \n\t" // load round key
Gilles Peskine4e201442023-03-15 19:36:03 +0100459 AESENC(xmm1_xmm0) // do round
460 "add $16, %1 \n\t" // point to next round key
461 "subl $1, %0 \n\t" // loop
462 "jnz 1b \n\t"
463 "movdqu (%1), %%xmm1 \n\t" // load round key
464 AESENCLAST(xmm1_xmm0) // last round
Yanray Wangb67b4742023-10-31 17:10:32 +0800465#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
Gilles Peskine4e201442023-03-15 19:36:03 +0100466 "jmp 3f \n\t"
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100467
Gilles Peskine4e201442023-03-15 19:36:03 +0100468 "2: \n\t" // decryption loop
469 "movdqu (%1), %%xmm1 \n\t"
470 AESDEC(xmm1_xmm0) // do round
471 "add $16, %1 \n\t"
472 "subl $1, %0 \n\t"
473 "jnz 2b \n\t"
474 "movdqu (%1), %%xmm1 \n\t" // load round key
475 AESDECLAST(xmm1_xmm0) // last round
Yanray Wang380be5a2023-08-28 15:40:34 +0800476#endif
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100477
Gilles Peskine4e201442023-03-15 19:36:03 +0100478 "3: \n\t"
479 "movdqu %%xmm0, (%4) \n\t" // export output
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100480 :
Werner Lewisdd76ef32022-05-30 12:00:21 +0100481 : "r" (ctx->nr), "r" (ctx->buf + ctx->rk_offset), "r" (mode), "r" (input), "r" (output)
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 : "memory", "cc", "xmm0", "xmm1");
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100483
484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 return 0;
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100486}
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100487
488/*
489 * GCM multiplication: c = a times b in GF(2^128)
490 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
491 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100492void mbedtls_aesni_gcm_mult(unsigned char c[16],
493 const unsigned char a[16],
494 const unsigned char b[16])
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100495{
496 unsigned char aa[16], bb[16], cc[16];
497 size_t i;
498
499 /* The inputs are in big-endian order, so byte-reverse them */
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 for (i = 0; i < 16; i++) {
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100501 aa[i] = a[15 - i];
502 bb[i] = b[15 - i];
503 }
504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 asm ("movdqu (%0), %%xmm0 \n\t" // a1:a0
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200506 "movdqu (%1), %%xmm1 \n\t" // b1:b0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100507
508 /*
509 * Caryless multiplication xmm2:xmm1 = xmm0 * xmm1
Gilles Peskined4f31c82023-03-10 22:21:47 +0100510 * using [CLMUL-WP] algorithm 1 (p. 12).
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100511 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200512 "movdqa %%xmm1, %%xmm2 \n\t" // copy of b1:b0
513 "movdqa %%xmm1, %%xmm3 \n\t" // same
514 "movdqa %%xmm1, %%xmm4 \n\t" // same
Gilles Peskine4e201442023-03-15 19:36:03 +0100515 PCLMULQDQ(xmm0_xmm1, "0x00") // a0*b0 = c1:c0
516 PCLMULQDQ(xmm0_xmm2, "0x11") // a1*b1 = d1:d0
517 PCLMULQDQ(xmm0_xmm3, "0x10") // a0*b1 = e1:e0
518 PCLMULQDQ(xmm0_xmm4, "0x01") // a1*b0 = f1:f0
519 "pxor %%xmm3, %%xmm4 \n\t" // e1+f1:e0+f0
520 "movdqa %%xmm4, %%xmm3 \n\t" // same
521 "psrldq $8, %%xmm4 \n\t" // 0:e1+f1
522 "pslldq $8, %%xmm3 \n\t" // e0+f0:0
523 "pxor %%xmm4, %%xmm2 \n\t" // d1:d0+e1+f1
524 "pxor %%xmm3, %%xmm1 \n\t" // c1+e0+f1:c0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100525
526 /*
527 * Now shift the result one bit to the left,
Gilles Peskined4f31c82023-03-10 22:21:47 +0100528 * taking advantage of [CLMUL-WP] eq 27 (p. 18)
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100529 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100530 "movdqa %%xmm1, %%xmm3 \n\t" // r1:r0
531 "movdqa %%xmm2, %%xmm4 \n\t" // r3:r2
532 "psllq $1, %%xmm1 \n\t" // r1<<1:r0<<1
533 "psllq $1, %%xmm2 \n\t" // r3<<1:r2<<1
534 "psrlq $63, %%xmm3 \n\t" // r1>>63:r0>>63
535 "psrlq $63, %%xmm4 \n\t" // r3>>63:r2>>63
536 "movdqa %%xmm3, %%xmm5 \n\t" // r1>>63:r0>>63
537 "pslldq $8, %%xmm3 \n\t" // r0>>63:0
538 "pslldq $8, %%xmm4 \n\t" // r2>>63:0
539 "psrldq $8, %%xmm5 \n\t" // 0:r1>>63
540 "por %%xmm3, %%xmm1 \n\t" // r1<<1|r0>>63:r0<<1
541 "por %%xmm4, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1
542 "por %%xmm5, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1|r1>>63
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100543
544 /*
545 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
Gilles Peskined4f31c82023-03-10 22:21:47 +0100546 * using [CLMUL-WP] algorithm 5 (p. 18).
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100547 * Currently xmm2:xmm1 holds x3:x2:x1:x0 (already shifted).
548 */
549 /* Step 2 (1) */
Gilles Peskine4e201442023-03-15 19:36:03 +0100550 "movdqa %%xmm1, %%xmm3 \n\t" // x1:x0
551 "movdqa %%xmm1, %%xmm4 \n\t" // same
552 "movdqa %%xmm1, %%xmm5 \n\t" // same
553 "psllq $63, %%xmm3 \n\t" // x1<<63:x0<<63 = stuff:a
554 "psllq $62, %%xmm4 \n\t" // x1<<62:x0<<62 = stuff:b
555 "psllq $57, %%xmm5 \n\t" // x1<<57:x0<<57 = stuff:c
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100556
557 /* Step 2 (2) */
Gilles Peskine4e201442023-03-15 19:36:03 +0100558 "pxor %%xmm4, %%xmm3 \n\t" // stuff:a+b
559 "pxor %%xmm5, %%xmm3 \n\t" // stuff:a+b+c
560 "pslldq $8, %%xmm3 \n\t" // a+b+c:0
561 "pxor %%xmm3, %%xmm1 \n\t" // x1+a+b+c:x0 = d:x0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100562
563 /* Steps 3 and 4 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100564 "movdqa %%xmm1,%%xmm0 \n\t" // d:x0
565 "movdqa %%xmm1,%%xmm4 \n\t" // same
566 "movdqa %%xmm1,%%xmm5 \n\t" // same
567 "psrlq $1, %%xmm0 \n\t" // e1:x0>>1 = e1:e0'
568 "psrlq $2, %%xmm4 \n\t" // f1:x0>>2 = f1:f0'
569 "psrlq $7, %%xmm5 \n\t" // g1:x0>>7 = g1:g0'
570 "pxor %%xmm4, %%xmm0 \n\t" // e1+f1:e0'+f0'
571 "pxor %%xmm5, %%xmm0 \n\t" // e1+f1+g1:e0'+f0'+g0'
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200572 // e0'+f0'+g0' is almost e0+f0+g0, ex\tcept for some missing
573 // bits carried from d. Now get those\t bits back in.
Gilles Peskine4e201442023-03-15 19:36:03 +0100574 "movdqa %%xmm1,%%xmm3 \n\t" // d:x0
575 "movdqa %%xmm1,%%xmm4 \n\t" // same
576 "movdqa %%xmm1,%%xmm5 \n\t" // same
577 "psllq $63, %%xmm3 \n\t" // d<<63:stuff
578 "psllq $62, %%xmm4 \n\t" // d<<62:stuff
579 "psllq $57, %%xmm5 \n\t" // d<<57:stuff
580 "pxor %%xmm4, %%xmm3 \n\t" // d<<63+d<<62:stuff
581 "pxor %%xmm5, %%xmm3 \n\t" // missing bits of d:stuff
582 "psrldq $8, %%xmm3 \n\t" // 0:missing bits of d
583 "pxor %%xmm3, %%xmm0 \n\t" // e1+f1+g1:e0+f0+g0
584 "pxor %%xmm1, %%xmm0 \n\t" // h1:h0
585 "pxor %%xmm2, %%xmm0 \n\t" // x3+h1:x2+h0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100586
Gilles Peskine4e201442023-03-15 19:36:03 +0100587 "movdqu %%xmm0, (%2) \n\t" // done
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100588 :
589 : "r" (aa), "r" (bb), "r" (cc)
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100591
592 /* Now byte-reverse the outputs */
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 for (i = 0; i < 16; i++) {
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100594 c[i] = cc[15 - i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 }
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100596
Paul Bakkere7f51332013-12-30 15:32:02 +0100597 return;
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100598}
599
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100600/*
601 * Compute decryption round keys from encryption round keys
602 */
Yanray Wangb67b4742023-10-31 17:10:32 +0800603#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100604void mbedtls_aesni_inverse_key(unsigned char *invkey,
605 const unsigned char *fwdkey, int nr)
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100606{
607 unsigned char *ik = invkey;
608 const unsigned char *fk = fwdkey + 16 * nr;
609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 for (fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16) {
613 asm ("movdqu (%0), %%xmm0 \n\t"
Gilles Peskine4e201442023-03-15 19:36:03 +0100614 AESIMC(xmm0_xmm0)
615 "movdqu %%xmm0, (%1) \n\t"
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100616 :
617 : "r" (fk), "r" (ik)
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 : "memory", "xmm0");
619 }
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100620
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100622}
Yanray Wang380be5a2023-08-28 15:40:34 +0800623#endif
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100624
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100625/*
626 * Key expansion, 128-bit case
627 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100628static void aesni_setkey_enc_128(unsigned char *rk,
629 const unsigned char *key)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100630{
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 asm ("movdqu (%1), %%xmm0 \n\t" // copy the original key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200632 "movdqu %%xmm0, (%0) \n\t" // as round key 0
633 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100634
635 /*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100636 * Finish generating the next round key.
637 *
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100638 * On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff
639 * with X = rot( sub( r3 ) ) ^ RCON.
640 *
641 * On exit, xmm0 is r7:r6:r5:r4
642 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
643 * and those are written to the round key buffer.
644 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200645 "1: \n\t"
646 "pshufd $0xff, %%xmm1, %%xmm1 \n\t" // X:X:X:X
647 "pxor %%xmm0, %%xmm1 \n\t" // X+r3:X+r2:X+r1:r4
648 "pslldq $4, %%xmm0 \n\t" // r2:r1:r0:0
649 "pxor %%xmm0, %%xmm1 \n\t" // X+r3+r2:X+r2+r1:r5:r4
650 "pslldq $4, %%xmm0 \n\t" // etc
651 "pxor %%xmm0, %%xmm1 \n\t"
652 "pslldq $4, %%xmm0 \n\t"
653 "pxor %%xmm1, %%xmm0 \n\t" // update xmm0 for next time!
654 "add $16, %0 \n\t" // point to next round key
655 "movdqu %%xmm0, (%0) \n\t" // write it
656 "ret \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100657
658 /* Main "loop" */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200659 "2: \n\t"
Gilles Peskine4e201442023-03-15 19:36:03 +0100660 AESKEYGENA(xmm0_xmm1, "0x01") "call 1b \n\t"
661 AESKEYGENA(xmm0_xmm1, "0x02") "call 1b \n\t"
662 AESKEYGENA(xmm0_xmm1, "0x04") "call 1b \n\t"
663 AESKEYGENA(xmm0_xmm1, "0x08") "call 1b \n\t"
664 AESKEYGENA(xmm0_xmm1, "0x10") "call 1b \n\t"
665 AESKEYGENA(xmm0_xmm1, "0x20") "call 1b \n\t"
666 AESKEYGENA(xmm0_xmm1, "0x40") "call 1b \n\t"
667 AESKEYGENA(xmm0_xmm1, "0x80") "call 1b \n\t"
668 AESKEYGENA(xmm0_xmm1, "0x1B") "call 1b \n\t"
669 AESKEYGENA(xmm0_xmm1, "0x36") "call 1b \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100670 :
671 : "r" (rk), "r" (key)
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 : "memory", "cc", "0");
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100673}
674
675/*
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100676 * Key expansion, 192-bit case
677 */
Arto Kinnunen732ca322023-04-14 14:26:10 +0800678#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100679static void aesni_setkey_enc_192(unsigned char *rk,
680 const unsigned char *key)
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100681{
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 asm ("movdqu (%1), %%xmm0 \n\t" // copy original round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200683 "movdqu %%xmm0, (%0) \n\t"
684 "add $16, %0 \n\t"
685 "movq 16(%1), %%xmm1 \n\t"
686 "movq %%xmm1, (%0) \n\t"
687 "add $8, %0 \n\t"
688 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100689
690 /*
691 * Finish generating the next 6 quarter-keys.
692 *
693 * On entry xmm0 is r3:r2:r1:r0, xmm1 is stuff:stuff:r5:r4
694 * and xmm2 is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON.
695 *
696 * On exit, xmm0 is r9:r8:r7:r6 and xmm1 is stuff:stuff:r11:r10
697 * and those are written to the round key buffer.
698 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200699 "1: \n\t"
700 "pshufd $0x55, %%xmm2, %%xmm2 \n\t" // X:X:X:X
701 "pxor %%xmm0, %%xmm2 \n\t" // X+r3:X+r2:X+r1:r4
702 "pslldq $4, %%xmm0 \n\t" // etc
703 "pxor %%xmm0, %%xmm2 \n\t"
704 "pslldq $4, %%xmm0 \n\t"
705 "pxor %%xmm0, %%xmm2 \n\t"
706 "pslldq $4, %%xmm0 \n\t"
707 "pxor %%xmm2, %%xmm0 \n\t" // update xmm0 = r9:r8:r7:r6
708 "movdqu %%xmm0, (%0) \n\t"
709 "add $16, %0 \n\t"
710 "pshufd $0xff, %%xmm0, %%xmm2 \n\t" // r9:r9:r9:r9
711 "pxor %%xmm1, %%xmm2 \n\t" // stuff:stuff:r9+r5:r10
712 "pslldq $4, %%xmm1 \n\t" // r2:r1:r0:0
713 "pxor %%xmm2, %%xmm1 \n\t" // xmm1 = stuff:stuff:r11:r10
714 "movq %%xmm1, (%0) \n\t"
715 "add $8, %0 \n\t"
716 "ret \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100717
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200718 "2: \n\t"
Gilles Peskine4e201442023-03-15 19:36:03 +0100719 AESKEYGENA(xmm1_xmm2, "0x01") "call 1b \n\t"
720 AESKEYGENA(xmm1_xmm2, "0x02") "call 1b \n\t"
721 AESKEYGENA(xmm1_xmm2, "0x04") "call 1b \n\t"
722 AESKEYGENA(xmm1_xmm2, "0x08") "call 1b \n\t"
723 AESKEYGENA(xmm1_xmm2, "0x10") "call 1b \n\t"
724 AESKEYGENA(xmm1_xmm2, "0x20") "call 1b \n\t"
725 AESKEYGENA(xmm1_xmm2, "0x40") "call 1b \n\t"
726 AESKEYGENA(xmm1_xmm2, "0x80") "call 1b \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100727
728 :
729 : "r" (rk), "r" (key)
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 : "memory", "cc", "0");
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100731}
Arto Kinnunen732ca322023-04-14 14:26:10 +0800732#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100733
734/*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100735 * Key expansion, 256-bit case
736 */
Arto Kinnunen732ca322023-04-14 14:26:10 +0800737#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100738static void aesni_setkey_enc_256(unsigned char *rk,
739 const unsigned char *key)
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100740{
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 asm ("movdqu (%1), %%xmm0 \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200742 "movdqu %%xmm0, (%0) \n\t"
743 "add $16, %0 \n\t"
744 "movdqu 16(%1), %%xmm1 \n\t"
745 "movdqu %%xmm1, (%0) \n\t"
746 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100747
748 /*
749 * Finish generating the next two round keys.
750 *
751 * On entry xmm0 is r3:r2:r1:r0, xmm1 is r7:r6:r5:r4 and
752 * xmm2 is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
753 *
754 * On exit, xmm0 is r11:r10:r9:r8 and xmm1 is r15:r14:r13:r12
755 * and those have been written to the output buffer.
756 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200757 "1: \n\t"
758 "pshufd $0xff, %%xmm2, %%xmm2 \n\t"
759 "pxor %%xmm0, %%xmm2 \n\t"
760 "pslldq $4, %%xmm0 \n\t"
761 "pxor %%xmm0, %%xmm2 \n\t"
762 "pslldq $4, %%xmm0 \n\t"
763 "pxor %%xmm0, %%xmm2 \n\t"
764 "pslldq $4, %%xmm0 \n\t"
765 "pxor %%xmm2, %%xmm0 \n\t"
766 "add $16, %0 \n\t"
767 "movdqu %%xmm0, (%0) \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100768
769 /* Set xmm2 to stuff:Y:stuff:stuff with Y = subword( r11 )
770 * and proceed to generate next round key from there */
Gilles Peskine4e201442023-03-15 19:36:03 +0100771 AESKEYGENA(xmm0_xmm2, "0x00")
772 "pshufd $0xaa, %%xmm2, %%xmm2 \n\t"
773 "pxor %%xmm1, %%xmm2 \n\t"
774 "pslldq $4, %%xmm1 \n\t"
775 "pxor %%xmm1, %%xmm2 \n\t"
776 "pslldq $4, %%xmm1 \n\t"
777 "pxor %%xmm1, %%xmm2 \n\t"
778 "pslldq $4, %%xmm1 \n\t"
779 "pxor %%xmm2, %%xmm1 \n\t"
780 "add $16, %0 \n\t"
781 "movdqu %%xmm1, (%0) \n\t"
782 "ret \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100783
784 /*
785 * Main "loop" - Generating one more key than necessary,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 * see definition of mbedtls_aes_context.buf
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100787 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100788 "2: \n\t"
789 AESKEYGENA(xmm1_xmm2, "0x01") "call 1b \n\t"
790 AESKEYGENA(xmm1_xmm2, "0x02") "call 1b \n\t"
791 AESKEYGENA(xmm1_xmm2, "0x04") "call 1b \n\t"
792 AESKEYGENA(xmm1_xmm2, "0x08") "call 1b \n\t"
793 AESKEYGENA(xmm1_xmm2, "0x10") "call 1b \n\t"
794 AESKEYGENA(xmm1_xmm2, "0x20") "call 1b \n\t"
795 AESKEYGENA(xmm1_xmm2, "0x40") "call 1b \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100796 :
797 : "r" (rk), "r" (key)
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 : "memory", "cc", "0");
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100799}
Arto Kinnunen732ca322023-04-14 14:26:10 +0800800#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100801
Gilles Peskine9c682e72023-03-16 17:21:33 +0100802#endif /* MBEDTLS_AESNI_HAVE_CODE */
Gilles Peskined6719172023-03-10 22:37:11 +0100803
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100804/*
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100805 * Key expansion, wrapper
806 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100807int mbedtls_aesni_setkey_enc(unsigned char *rk,
808 const unsigned char *key,
809 size_t bits)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100810{
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 switch (bits) {
812 case 128: aesni_setkey_enc_128(rk, key); break;
Arto Kinnunen732ca322023-04-14 14:26:10 +0800813#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 case 192: aesni_setkey_enc_192(rk, key); break;
815 case 256: aesni_setkey_enc_256(rk, key); break;
Arto Kinnunen732ca322023-04-14 14:26:10 +0800816#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 default: return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100818 }
819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 return 0;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100821}
822
Gilles Peskine9c682e72023-03-16 17:21:33 +0100823#endif /* MBEDTLS_AESNI_HAVE_CODE */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825#endif /* MBEDTLS_AESNI_C */