blob: 864d0d613460710a75269505dca60300446d8333 [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
Pengyu Lv0ecb6352023-10-11 10:36:55 +080036#if defined(__GNUC__)
Gilles Peskined6719172023-03-10 22:37:11 +010037#include <cpuid.h>
Pengyu Lv0ecb6352023-10-11 10:36:55 +080038#elif defined(_MSC_VER)
SlugFiller5ca3f0b2023-05-22 06:31:45 +030039#include <intrin.h>
Pengyu Lv0ecb6352023-10-11 10:36:55 +080040#else
41#error "`__cpuid` required by MBEDTLS_AESNI_C is not supported by the compiler"
Tom Cosgrove02edb752023-03-13 15:32:52 +000042#endif
Gilles Peskined6719172023-03-10 22:37:11 +010043#include <immintrin.h>
44#endif
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010045
Beniamin Sandu800f2b72023-10-27 16:58:00 +010046#if defined(MBEDTLS_ARCH_IS_X86)
47#if defined(MBEDTLS_COMPILER_IS_GCC)
48#pragma GCC push_options
49#pragma GCC target ("pclmul,sse2,aes")
50#define MBEDTLS_POP_TARGET_PRAGMA
51#elif defined(__clang__)
52#pragma clang attribute push (__attribute__((target("pclmul,sse2,aes"))), apply_to=function)
53#define MBEDTLS_POP_TARGET_PRAGMA
54#endif
55#endif
56
Jerry Yu0a6272d2023-08-18 17:35:59 +080057#if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010058/*
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010059 * AES-NI support detection routine
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010060 */
Gilles Peskine449bd832023-01-11 14:50:10 +010061int mbedtls_aesni_has_support(unsigned int what)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010062{
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010063 static int done = 0;
64 static unsigned int c = 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010065
Gilles Peskine449bd832023-01-11 14:50:10 +010066 if (!done) {
Gilles Peskine9c682e72023-03-16 17:21:33 +010067#if MBEDTLS_AESNI_HAVE_CODE == 2
Pengyu Lve8c4bf12023-10-10 18:12:43 +080068 static int info[4] = { 0, 0, 0, 0 };
Pengyu Lv0ecb6352023-10-11 10:36:55 +080069#if defined(_MSC_VER)
Gilles Peskined6719172023-03-10 22:37:11 +010070 __cpuid(info, 1);
71#else
72 __cpuid(1, info[0], info[1], info[2], info[3]);
73#endif
74 c = info[2];
Gilles Peskine9c682e72023-03-16 17:21:33 +010075#else /* AESNI using asm */
Gilles Peskine449bd832023-01-11 14:50:10 +010076 asm ("movl $1, %%eax \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +020077 "cpuid \n\t"
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010078 : "=c" (c)
79 :
Gilles Peskine449bd832023-01-11 14:50:10 +010080 : "eax", "ebx", "edx");
Gilles Peskine9c682e72023-03-16 17:21:33 +010081#endif /* MBEDTLS_AESNI_HAVE_CODE */
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010082 done = 1;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010083 }
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085 return (c & what) != 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010086}
Jerry Yu36606232023-04-19 10:44:29 +080087#endif /* !MBEDTLS_AES_USE_HARDWARE_ONLY */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010088
Gilles Peskine9c682e72023-03-16 17:21:33 +010089#if MBEDTLS_AESNI_HAVE_CODE == 2
Gilles Peskined6719172023-03-10 22:37:11 +010090
91/*
92 * AES-NI AES-ECB block en(de)cryption
93 */
94int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
95 int mode,
96 const unsigned char input[16],
97 unsigned char output[16])
98{
99 const __m128i *rk = (const __m128i *) (ctx->buf + ctx->rk_offset);
100 unsigned nr = ctx->nr; // Number of remaining rounds
Tom Cosgrove02edb752023-03-13 15:32:52 +0000101
Gilles Peskined6719172023-03-10 22:37:11 +0100102 // Load round key 0
Gilles Peskinedafeee42023-03-15 20:37:57 +0100103 __m128i state;
104 memcpy(&state, input, 16);
105 state = _mm_xor_si128(state, rk[0]); // state ^= *rk;
Gilles Peskined6719172023-03-10 22:37:11 +0100106 ++rk;
107 --nr;
108
109 if (mode == 0) {
110 while (nr != 0) {
Gilles Peskinedafeee42023-03-15 20:37:57 +0100111 state = _mm_aesdec_si128(state, *rk);
Gilles Peskined6719172023-03-10 22:37:11 +0100112 ++rk;
113 --nr;
114 }
Gilles Peskinedafeee42023-03-15 20:37:57 +0100115 state = _mm_aesdeclast_si128(state, *rk);
Gilles Peskined6719172023-03-10 22:37:11 +0100116 } else {
117 while (nr != 0) {
Gilles Peskinedafeee42023-03-15 20:37:57 +0100118 state = _mm_aesenc_si128(state, *rk);
Gilles Peskined6719172023-03-10 22:37:11 +0100119 ++rk;
120 --nr;
121 }
Gilles Peskinedafeee42023-03-15 20:37:57 +0100122 state = _mm_aesenclast_si128(state, *rk);
Gilles Peskined6719172023-03-10 22:37:11 +0100123 }
124
Gilles Peskinedafeee42023-03-15 20:37:57 +0100125 memcpy(output, &state, 16);
Gilles Peskined6719172023-03-10 22:37:11 +0100126 return 0;
127}
128
129/*
130 * GCM multiplication: c = a times b in GF(2^128)
131 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
132 */
133
134static void gcm_clmul(const __m128i aa, const __m128i bb,
135 __m128i *cc, __m128i *dd)
136{
137 /*
138 * Caryless multiplication dd:cc = aa * bb
139 * using [CLMUL-WP] algorithm 1 (p. 12).
140 */
141 *cc = _mm_clmulepi64_si128(aa, bb, 0x00); // a0*b0 = c1:c0
142 *dd = _mm_clmulepi64_si128(aa, bb, 0x11); // a1*b1 = d1:d0
143 __m128i ee = _mm_clmulepi64_si128(aa, bb, 0x10); // a0*b1 = e1:e0
144 __m128i ff = _mm_clmulepi64_si128(aa, bb, 0x01); // a1*b0 = f1:f0
Tom Cosgrove02edb752023-03-13 15:32:52 +0000145 ff = _mm_xor_si128(ff, ee); // e1+f1:e0+f0
Gilles Peskined6719172023-03-10 22:37:11 +0100146 ee = ff; // e1+f1:e0+f0
147 ff = _mm_srli_si128(ff, 8); // 0:e1+f1
148 ee = _mm_slli_si128(ee, 8); // e0+f0:0
Tom Cosgrove02edb752023-03-13 15:32:52 +0000149 *dd = _mm_xor_si128(*dd, ff); // d1:d0+e1+f1
Gilles Peskined0185f72023-03-16 13:08:18 +0100150 *cc = _mm_xor_si128(*cc, ee); // c1+e0+f0:c0
Gilles Peskined6719172023-03-10 22:37:11 +0100151}
152
153static void gcm_shift(__m128i *cc, __m128i *dd)
154{
Gilles Peskinedafeee42023-03-15 20:37:57 +0100155 /* [CMUCL-WP] Algorithm 5 Step 1: shift cc:dd one bit to the left,
156 * taking advantage of [CLMUL-WP] eq 27 (p. 18). */
157 // // *cc = r1:r0
158 // // *dd = r3:r2
159 __m128i cc_lo = _mm_slli_epi64(*cc, 1); // r1<<1:r0<<1
160 __m128i dd_lo = _mm_slli_epi64(*dd, 1); // r3<<1:r2<<1
161 __m128i cc_hi = _mm_srli_epi64(*cc, 63); // r1>>63:r0>>63
162 __m128i dd_hi = _mm_srli_epi64(*dd, 63); // r3>>63:r2>>63
163 __m128i xmm5 = _mm_srli_si128(cc_hi, 8); // 0:r1>>63
164 cc_hi = _mm_slli_si128(cc_hi, 8); // r0>>63:0
165 dd_hi = _mm_slli_si128(dd_hi, 8); // 0:r1>>63
Gilles Peskined6719172023-03-10 22:37:11 +0100166
Gilles Peskinedafeee42023-03-15 20:37:57 +0100167 *cc = _mm_or_si128(cc_lo, cc_hi); // r1<<1|r0>>63:r0<<1
168 *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 +0100169}
170
Gilles Peskinedafeee42023-03-15 20:37:57 +0100171static __m128i gcm_reduce(__m128i xx)
Gilles Peskined6719172023-03-10 22:37:11 +0100172{
173 // // xx = x1:x0
174 /* [CLMUL-WP] Algorithm 5 Step 2 */
175 __m128i aa = _mm_slli_epi64(xx, 63); // x1<<63:x0<<63 = stuff:a
176 __m128i bb = _mm_slli_epi64(xx, 62); // x1<<62:x0<<62 = stuff:b
177 __m128i cc = _mm_slli_epi64(xx, 57); // x1<<57:x0<<57 = stuff:c
Tom Cosgrove02edb752023-03-13 15:32:52 +0000178 __m128i dd = _mm_slli_si128(_mm_xor_si128(_mm_xor_si128(aa, bb), cc), 8); // a+b+c:0
179 return _mm_xor_si128(dd, xx); // x1+a+b+c:x0 = d:x0
Gilles Peskined6719172023-03-10 22:37:11 +0100180}
181
Gilles Peskinedafeee42023-03-15 20:37:57 +0100182static __m128i gcm_mix(__m128i dx)
Gilles Peskined6719172023-03-10 22:37:11 +0100183{
184 /* [CLMUL-WP] Algorithm 5 Steps 3 and 4 */
185 __m128i ee = _mm_srli_epi64(dx, 1); // e1:x0>>1 = e1:e0'
186 __m128i ff = _mm_srli_epi64(dx, 2); // f1:x0>>2 = f1:f0'
187 __m128i gg = _mm_srli_epi64(dx, 7); // g1:x0>>7 = g1:g0'
188
189 // e0'+f0'+g0' is almost e0+f0+g0, except for some missing
190 // bits carried from d. Now get those bits back in.
191 __m128i eh = _mm_slli_epi64(dx, 63); // d<<63:stuff
192 __m128i fh = _mm_slli_epi64(dx, 62); // d<<62:stuff
193 __m128i gh = _mm_slli_epi64(dx, 57); // d<<57:stuff
Tom Cosgrove02edb752023-03-13 15:32:52 +0000194 __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 +0100195
Tom Cosgrove02edb752023-03-13 15:32:52 +0000196 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 +0100197}
198
199void mbedtls_aesni_gcm_mult(unsigned char c[16],
200 const unsigned char a[16],
201 const unsigned char b[16])
202{
Sergey Markelov3898f102023-10-16 12:54:48 -0700203 __m128i aa = { 0 }, bb = { 0 }, cc, dd;
Gilles Peskined6719172023-03-10 22:37:11 +0100204
205 /* The inputs are in big-endian order, so byte-reverse them */
206 for (size_t i = 0; i < 16; i++) {
207 ((uint8_t *) &aa)[i] = a[15 - i];
208 ((uint8_t *) &bb)[i] = b[15 - i];
209 }
210
211 gcm_clmul(aa, bb, &cc, &dd);
212 gcm_shift(&cc, &dd);
213 /*
214 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
215 * using [CLMUL-WP] algorithm 5 (p. 18).
216 * Currently dd:cc holds x3:x2:x1:x0 (already shifted).
217 */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100218 __m128i dx = gcm_reduce(cc);
219 __m128i xh = gcm_mix(dx);
Tom Cosgrove02edb752023-03-13 15:32:52 +0000220 cc = _mm_xor_si128(xh, dd); // x3+h1:x2+h0
Gilles Peskined6719172023-03-10 22:37:11 +0100221
222 /* Now byte-reverse the outputs */
223 for (size_t i = 0; i < 16; i++) {
224 c[i] = ((uint8_t *) &cc)[15 - i];
225 }
226
227 return;
228}
229
230/*
231 * Compute decryption round keys from encryption round keys
232 */
233void mbedtls_aesni_inverse_key(unsigned char *invkey,
234 const unsigned char *fwdkey, int nr)
235{
236 __m128i *ik = (__m128i *) invkey;
237 const __m128i *fk = (const __m128i *) fwdkey + nr;
238
239 *ik = *fk;
240 for (--fk, ++ik; fk > (const __m128i *) fwdkey; --fk, ++ik) {
241 *ik = _mm_aesimc_si128(*fk);
242 }
243 *ik = *fk;
244}
245
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
Beniamin Sandu800f2b72023-10-27 16:58:00 +0100412#if defined(MBEDTLS_POP_TARGET_PRAGMA)
413#if defined(__clang__)
414#pragma clang attribute pop
415#elif defined(__GNUC__)
416#pragma GCC pop_options
417#endif
418#undef MBEDTLS_POP_TARGET_PRAGMA
419#endif
420
Gilles Peskine9c682e72023-03-16 17:21:33 +0100421#else /* MBEDTLS_AESNI_HAVE_CODE == 1 */
Gilles Peskined6719172023-03-10 22:37:11 +0100422
Gilles Peskined0f9b0b2023-03-10 22:25:13 +0100423#if defined(__has_feature)
424#if __has_feature(memory_sanitizer)
425#warning \
426 "MBEDTLS_AESNI_C is known to cause spurious error reports with some memory sanitizers as they do not understand the assembly code."
427#endif
428#endif
429
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100430/*
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +0200431 * Binutils needs to be at least 2.19 to support AES-NI instructions.
432 * Unfortunately, a lot of users have a lower version now (2014-04).
433 * Emit bytecode directly in order to support "old" version of gas.
434 *
435 * Opcodes from the Intel architecture reference manual, vol. 3.
436 * We always use registers, so we don't need prefixes for memory operands.
437 * Operand macros are in gas order (src, dst) as opposed to Intel order
438 * (dst, src) in order to blend better into the surrounding assembly code.
439 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100440#define AESDEC(regs) ".byte 0x66,0x0F,0x38,0xDE," regs "\n\t"
441#define AESDECLAST(regs) ".byte 0x66,0x0F,0x38,0xDF," regs "\n\t"
442#define AESENC(regs) ".byte 0x66,0x0F,0x38,0xDC," regs "\n\t"
443#define AESENCLAST(regs) ".byte 0x66,0x0F,0x38,0xDD," regs "\n\t"
444#define AESIMC(regs) ".byte 0x66,0x0F,0x38,0xDB," regs "\n\t"
445#define AESKEYGENA(regs, imm) ".byte 0x66,0x0F,0x3A,0xDF," regs "," imm "\n\t"
446#define PCLMULQDQ(regs, imm) ".byte 0x66,0x0F,0x3A,0x44," regs "," imm "\n\t"
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +0200447
448#define xmm0_xmm0 "0xC0"
449#define xmm0_xmm1 "0xC8"
450#define xmm0_xmm2 "0xD0"
451#define xmm0_xmm3 "0xD8"
452#define xmm0_xmm4 "0xE0"
453#define xmm1_xmm0 "0xC1"
454#define xmm1_xmm2 "0xD1"
455
456/*
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100457 * AES-NI AES-ECB block en(de)cryption
458 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100459int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
460 int mode,
461 const unsigned char input[16],
462 unsigned char output[16])
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100463{
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 asm ("movdqu (%3), %%xmm0 \n\t" // load input
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200465 "movdqu (%1), %%xmm1 \n\t" // load round key 0
466 "pxor %%xmm1, %%xmm0 \n\t" // round 0
James Cowgill6c8edca2015-12-17 01:40:26 +0000467 "add $16, %1 \n\t" // point to next round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200468 "subl $1, %0 \n\t" // normal rounds = nr - 1
469 "test %2, %2 \n\t" // mode?
470 "jz 2f \n\t" // 0 = decrypt
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100471
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200472 "1: \n\t" // encryption loop
473 "movdqu (%1), %%xmm1 \n\t" // load round key
Gilles Peskine4e201442023-03-15 19:36:03 +0100474 AESENC(xmm1_xmm0) // do round
475 "add $16, %1 \n\t" // point to next round key
476 "subl $1, %0 \n\t" // loop
477 "jnz 1b \n\t"
478 "movdqu (%1), %%xmm1 \n\t" // load round key
479 AESENCLAST(xmm1_xmm0) // last round
480 "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
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100490
Gilles Peskine4e201442023-03-15 19:36:03 +0100491 "3: \n\t"
492 "movdqu %%xmm0, (%4) \n\t" // export output
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100493 :
Werner Lewisdd76ef32022-05-30 12:00:21 +0100494 : "r" (ctx->nr), "r" (ctx->buf + ctx->rk_offset), "r" (mode), "r" (input), "r" (output)
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 : "memory", "cc", "xmm0", "xmm1");
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100496
497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 return 0;
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100499}
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100500
501/*
502 * GCM multiplication: c = a times b in GF(2^128)
503 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
504 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100505void mbedtls_aesni_gcm_mult(unsigned char c[16],
506 const unsigned char a[16],
507 const unsigned char b[16])
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100508{
509 unsigned char aa[16], bb[16], cc[16];
510 size_t i;
511
512 /* The inputs are in big-endian order, so byte-reverse them */
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 for (i = 0; i < 16; i++) {
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100514 aa[i] = a[15 - i];
515 bb[i] = b[15 - i];
516 }
517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 asm ("movdqu (%0), %%xmm0 \n\t" // a1:a0
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200519 "movdqu (%1), %%xmm1 \n\t" // b1:b0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100520
521 /*
522 * Caryless multiplication xmm2:xmm1 = xmm0 * xmm1
Gilles Peskined4f31c82023-03-10 22:21:47 +0100523 * using [CLMUL-WP] algorithm 1 (p. 12).
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100524 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200525 "movdqa %%xmm1, %%xmm2 \n\t" // copy of b1:b0
526 "movdqa %%xmm1, %%xmm3 \n\t" // same
527 "movdqa %%xmm1, %%xmm4 \n\t" // same
Gilles Peskine4e201442023-03-15 19:36:03 +0100528 PCLMULQDQ(xmm0_xmm1, "0x00") // a0*b0 = c1:c0
529 PCLMULQDQ(xmm0_xmm2, "0x11") // a1*b1 = d1:d0
530 PCLMULQDQ(xmm0_xmm3, "0x10") // a0*b1 = e1:e0
531 PCLMULQDQ(xmm0_xmm4, "0x01") // a1*b0 = f1:f0
532 "pxor %%xmm3, %%xmm4 \n\t" // e1+f1:e0+f0
533 "movdqa %%xmm4, %%xmm3 \n\t" // same
534 "psrldq $8, %%xmm4 \n\t" // 0:e1+f1
535 "pslldq $8, %%xmm3 \n\t" // e0+f0:0
536 "pxor %%xmm4, %%xmm2 \n\t" // d1:d0+e1+f1
537 "pxor %%xmm3, %%xmm1 \n\t" // c1+e0+f1:c0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100538
539 /*
540 * Now shift the result one bit to the left,
Gilles Peskined4f31c82023-03-10 22:21:47 +0100541 * taking advantage of [CLMUL-WP] eq 27 (p. 18)
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100542 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100543 "movdqa %%xmm1, %%xmm3 \n\t" // r1:r0
544 "movdqa %%xmm2, %%xmm4 \n\t" // r3:r2
545 "psllq $1, %%xmm1 \n\t" // r1<<1:r0<<1
546 "psllq $1, %%xmm2 \n\t" // r3<<1:r2<<1
547 "psrlq $63, %%xmm3 \n\t" // r1>>63:r0>>63
548 "psrlq $63, %%xmm4 \n\t" // r3>>63:r2>>63
549 "movdqa %%xmm3, %%xmm5 \n\t" // r1>>63:r0>>63
550 "pslldq $8, %%xmm3 \n\t" // r0>>63:0
551 "pslldq $8, %%xmm4 \n\t" // r2>>63:0
552 "psrldq $8, %%xmm5 \n\t" // 0:r1>>63
553 "por %%xmm3, %%xmm1 \n\t" // r1<<1|r0>>63:r0<<1
554 "por %%xmm4, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1
555 "por %%xmm5, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1|r1>>63
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100556
557 /*
558 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
Gilles Peskined4f31c82023-03-10 22:21:47 +0100559 * using [CLMUL-WP] algorithm 5 (p. 18).
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100560 * Currently xmm2:xmm1 holds x3:x2:x1:x0 (already shifted).
561 */
562 /* Step 2 (1) */
Gilles Peskine4e201442023-03-15 19:36:03 +0100563 "movdqa %%xmm1, %%xmm3 \n\t" // x1:x0
564 "movdqa %%xmm1, %%xmm4 \n\t" // same
565 "movdqa %%xmm1, %%xmm5 \n\t" // same
566 "psllq $63, %%xmm3 \n\t" // x1<<63:x0<<63 = stuff:a
567 "psllq $62, %%xmm4 \n\t" // x1<<62:x0<<62 = stuff:b
568 "psllq $57, %%xmm5 \n\t" // x1<<57:x0<<57 = stuff:c
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100569
570 /* Step 2 (2) */
Gilles Peskine4e201442023-03-15 19:36:03 +0100571 "pxor %%xmm4, %%xmm3 \n\t" // stuff:a+b
572 "pxor %%xmm5, %%xmm3 \n\t" // stuff:a+b+c
573 "pslldq $8, %%xmm3 \n\t" // a+b+c:0
574 "pxor %%xmm3, %%xmm1 \n\t" // x1+a+b+c:x0 = d:x0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100575
576 /* Steps 3 and 4 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100577 "movdqa %%xmm1,%%xmm0 \n\t" // d:x0
578 "movdqa %%xmm1,%%xmm4 \n\t" // same
579 "movdqa %%xmm1,%%xmm5 \n\t" // same
580 "psrlq $1, %%xmm0 \n\t" // e1:x0>>1 = e1:e0'
581 "psrlq $2, %%xmm4 \n\t" // f1:x0>>2 = f1:f0'
582 "psrlq $7, %%xmm5 \n\t" // g1:x0>>7 = g1:g0'
583 "pxor %%xmm4, %%xmm0 \n\t" // e1+f1:e0'+f0'
584 "pxor %%xmm5, %%xmm0 \n\t" // e1+f1+g1:e0'+f0'+g0'
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200585 // e0'+f0'+g0' is almost e0+f0+g0, ex\tcept for some missing
586 // bits carried from d. Now get those\t bits back in.
Gilles Peskine4e201442023-03-15 19:36:03 +0100587 "movdqa %%xmm1,%%xmm3 \n\t" // d:x0
588 "movdqa %%xmm1,%%xmm4 \n\t" // same
589 "movdqa %%xmm1,%%xmm5 \n\t" // same
590 "psllq $63, %%xmm3 \n\t" // d<<63:stuff
591 "psllq $62, %%xmm4 \n\t" // d<<62:stuff
592 "psllq $57, %%xmm5 \n\t" // d<<57:stuff
593 "pxor %%xmm4, %%xmm3 \n\t" // d<<63+d<<62:stuff
594 "pxor %%xmm5, %%xmm3 \n\t" // missing bits of d:stuff
595 "psrldq $8, %%xmm3 \n\t" // 0:missing bits of d
596 "pxor %%xmm3, %%xmm0 \n\t" // e1+f1+g1:e0+f0+g0
597 "pxor %%xmm1, %%xmm0 \n\t" // h1:h0
598 "pxor %%xmm2, %%xmm0 \n\t" // x3+h1:x2+h0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100599
Gilles Peskine4e201442023-03-15 19:36:03 +0100600 "movdqu %%xmm0, (%2) \n\t" // done
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100601 :
602 : "r" (aa), "r" (bb), "r" (cc)
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100604
605 /* Now byte-reverse the outputs */
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 for (i = 0; i < 16; i++) {
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100607 c[i] = cc[15 - i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 }
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100609
Paul Bakkere7f51332013-12-30 15:32:02 +0100610 return;
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100611}
612
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100613/*
614 * Compute decryption round keys from encryption round keys
615 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100616void mbedtls_aesni_inverse_key(unsigned char *invkey,
617 const unsigned char *fwdkey, int nr)
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100618{
619 unsigned char *ik = invkey;
620 const unsigned char *fk = fwdkey + 16 * nr;
621
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100623
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 for (fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16) {
625 asm ("movdqu (%0), %%xmm0 \n\t"
Gilles Peskine4e201442023-03-15 19:36:03 +0100626 AESIMC(xmm0_xmm0)
627 "movdqu %%xmm0, (%1) \n\t"
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100628 :
629 : "r" (fk), "r" (ik)
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 : "memory", "xmm0");
631 }
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100634}
635
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100636/*
637 * Key expansion, 128-bit case
638 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100639static void aesni_setkey_enc_128(unsigned char *rk,
640 const unsigned char *key)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100641{
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 asm ("movdqu (%1), %%xmm0 \n\t" // copy the original key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200643 "movdqu %%xmm0, (%0) \n\t" // as round key 0
644 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100645
646 /*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100647 * Finish generating the next round key.
648 *
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100649 * On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff
650 * with X = rot( sub( r3 ) ) ^ RCON.
651 *
652 * On exit, xmm0 is r7:r6:r5:r4
653 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
654 * and those are written to the round key buffer.
655 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200656 "1: \n\t"
657 "pshufd $0xff, %%xmm1, %%xmm1 \n\t" // X:X:X:X
658 "pxor %%xmm0, %%xmm1 \n\t" // X+r3:X+r2:X+r1:r4
659 "pslldq $4, %%xmm0 \n\t" // r2:r1:r0:0
660 "pxor %%xmm0, %%xmm1 \n\t" // X+r3+r2:X+r2+r1:r5:r4
661 "pslldq $4, %%xmm0 \n\t" // etc
662 "pxor %%xmm0, %%xmm1 \n\t"
663 "pslldq $4, %%xmm0 \n\t"
664 "pxor %%xmm1, %%xmm0 \n\t" // update xmm0 for next time!
665 "add $16, %0 \n\t" // point to next round key
666 "movdqu %%xmm0, (%0) \n\t" // write it
667 "ret \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100668
669 /* Main "loop" */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200670 "2: \n\t"
Gilles Peskine4e201442023-03-15 19:36:03 +0100671 AESKEYGENA(xmm0_xmm1, "0x01") "call 1b \n\t"
672 AESKEYGENA(xmm0_xmm1, "0x02") "call 1b \n\t"
673 AESKEYGENA(xmm0_xmm1, "0x04") "call 1b \n\t"
674 AESKEYGENA(xmm0_xmm1, "0x08") "call 1b \n\t"
675 AESKEYGENA(xmm0_xmm1, "0x10") "call 1b \n\t"
676 AESKEYGENA(xmm0_xmm1, "0x20") "call 1b \n\t"
677 AESKEYGENA(xmm0_xmm1, "0x40") "call 1b \n\t"
678 AESKEYGENA(xmm0_xmm1, "0x80") "call 1b \n\t"
679 AESKEYGENA(xmm0_xmm1, "0x1B") "call 1b \n\t"
680 AESKEYGENA(xmm0_xmm1, "0x36") "call 1b \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100681 :
682 : "r" (rk), "r" (key)
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 : "memory", "cc", "0");
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100684}
685
686/*
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100687 * Key expansion, 192-bit case
688 */
Arto Kinnunen732ca322023-04-14 14:26:10 +0800689#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100690static void aesni_setkey_enc_192(unsigned char *rk,
691 const unsigned char *key)
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100692{
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 asm ("movdqu (%1), %%xmm0 \n\t" // copy original round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200694 "movdqu %%xmm0, (%0) \n\t"
695 "add $16, %0 \n\t"
696 "movq 16(%1), %%xmm1 \n\t"
697 "movq %%xmm1, (%0) \n\t"
698 "add $8, %0 \n\t"
699 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100700
701 /*
702 * Finish generating the next 6 quarter-keys.
703 *
704 * On entry xmm0 is r3:r2:r1:r0, xmm1 is stuff:stuff:r5:r4
705 * and xmm2 is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON.
706 *
707 * On exit, xmm0 is r9:r8:r7:r6 and xmm1 is stuff:stuff:r11:r10
708 * and those are written to the round key buffer.
709 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200710 "1: \n\t"
711 "pshufd $0x55, %%xmm2, %%xmm2 \n\t" // X:X:X:X
712 "pxor %%xmm0, %%xmm2 \n\t" // X+r3:X+r2:X+r1:r4
713 "pslldq $4, %%xmm0 \n\t" // etc
714 "pxor %%xmm0, %%xmm2 \n\t"
715 "pslldq $4, %%xmm0 \n\t"
716 "pxor %%xmm0, %%xmm2 \n\t"
717 "pslldq $4, %%xmm0 \n\t"
718 "pxor %%xmm2, %%xmm0 \n\t" // update xmm0 = r9:r8:r7:r6
719 "movdqu %%xmm0, (%0) \n\t"
720 "add $16, %0 \n\t"
721 "pshufd $0xff, %%xmm0, %%xmm2 \n\t" // r9:r9:r9:r9
722 "pxor %%xmm1, %%xmm2 \n\t" // stuff:stuff:r9+r5:r10
723 "pslldq $4, %%xmm1 \n\t" // r2:r1:r0:0
724 "pxor %%xmm2, %%xmm1 \n\t" // xmm1 = stuff:stuff:r11:r10
725 "movq %%xmm1, (%0) \n\t"
726 "add $8, %0 \n\t"
727 "ret \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100728
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200729 "2: \n\t"
Gilles Peskine4e201442023-03-15 19:36:03 +0100730 AESKEYGENA(xmm1_xmm2, "0x01") "call 1b \n\t"
731 AESKEYGENA(xmm1_xmm2, "0x02") "call 1b \n\t"
732 AESKEYGENA(xmm1_xmm2, "0x04") "call 1b \n\t"
733 AESKEYGENA(xmm1_xmm2, "0x08") "call 1b \n\t"
734 AESKEYGENA(xmm1_xmm2, "0x10") "call 1b \n\t"
735 AESKEYGENA(xmm1_xmm2, "0x20") "call 1b \n\t"
736 AESKEYGENA(xmm1_xmm2, "0x40") "call 1b \n\t"
737 AESKEYGENA(xmm1_xmm2, "0x80") "call 1b \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100738
739 :
740 : "r" (rk), "r" (key)
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 : "memory", "cc", "0");
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100742}
Arto Kinnunen732ca322023-04-14 14:26:10 +0800743#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100744
745/*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100746 * Key expansion, 256-bit case
747 */
Arto Kinnunen732ca322023-04-14 14:26:10 +0800748#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100749static void aesni_setkey_enc_256(unsigned char *rk,
750 const unsigned char *key)
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100751{
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 asm ("movdqu (%1), %%xmm0 \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200753 "movdqu %%xmm0, (%0) \n\t"
754 "add $16, %0 \n\t"
755 "movdqu 16(%1), %%xmm1 \n\t"
756 "movdqu %%xmm1, (%0) \n\t"
757 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100758
759 /*
760 * Finish generating the next two round keys.
761 *
762 * On entry xmm0 is r3:r2:r1:r0, xmm1 is r7:r6:r5:r4 and
763 * xmm2 is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
764 *
765 * On exit, xmm0 is r11:r10:r9:r8 and xmm1 is r15:r14:r13:r12
766 * and those have been written to the output buffer.
767 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200768 "1: \n\t"
769 "pshufd $0xff, %%xmm2, %%xmm2 \n\t"
770 "pxor %%xmm0, %%xmm2 \n\t"
771 "pslldq $4, %%xmm0 \n\t"
772 "pxor %%xmm0, %%xmm2 \n\t"
773 "pslldq $4, %%xmm0 \n\t"
774 "pxor %%xmm0, %%xmm2 \n\t"
775 "pslldq $4, %%xmm0 \n\t"
776 "pxor %%xmm2, %%xmm0 \n\t"
777 "add $16, %0 \n\t"
778 "movdqu %%xmm0, (%0) \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100779
780 /* Set xmm2 to stuff:Y:stuff:stuff with Y = subword( r11 )
781 * and proceed to generate next round key from there */
Gilles Peskine4e201442023-03-15 19:36:03 +0100782 AESKEYGENA(xmm0_xmm2, "0x00")
783 "pshufd $0xaa, %%xmm2, %%xmm2 \n\t"
784 "pxor %%xmm1, %%xmm2 \n\t"
785 "pslldq $4, %%xmm1 \n\t"
786 "pxor %%xmm1, %%xmm2 \n\t"
787 "pslldq $4, %%xmm1 \n\t"
788 "pxor %%xmm1, %%xmm2 \n\t"
789 "pslldq $4, %%xmm1 \n\t"
790 "pxor %%xmm2, %%xmm1 \n\t"
791 "add $16, %0 \n\t"
792 "movdqu %%xmm1, (%0) \n\t"
793 "ret \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100794
795 /*
796 * Main "loop" - Generating one more key than necessary,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 * see definition of mbedtls_aes_context.buf
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100798 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100799 "2: \n\t"
800 AESKEYGENA(xmm1_xmm2, "0x01") "call 1b \n\t"
801 AESKEYGENA(xmm1_xmm2, "0x02") "call 1b \n\t"
802 AESKEYGENA(xmm1_xmm2, "0x04") "call 1b \n\t"
803 AESKEYGENA(xmm1_xmm2, "0x08") "call 1b \n\t"
804 AESKEYGENA(xmm1_xmm2, "0x10") "call 1b \n\t"
805 AESKEYGENA(xmm1_xmm2, "0x20") "call 1b \n\t"
806 AESKEYGENA(xmm1_xmm2, "0x40") "call 1b \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100807 :
808 : "r" (rk), "r" (key)
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 : "memory", "cc", "0");
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100810}
Arto Kinnunen732ca322023-04-14 14:26:10 +0800811#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100812
Gilles Peskine9c682e72023-03-16 17:21:33 +0100813#endif /* MBEDTLS_AESNI_HAVE_CODE */
Gilles Peskined6719172023-03-10 22:37:11 +0100814
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100815/*
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100816 * Key expansion, wrapper
817 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100818int mbedtls_aesni_setkey_enc(unsigned char *rk,
819 const unsigned char *key,
820 size_t bits)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100821{
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 switch (bits) {
823 case 128: aesni_setkey_enc_128(rk, key); break;
Arto Kinnunen732ca322023-04-14 14:26:10 +0800824#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 case 192: aesni_setkey_enc_192(rk, key); break;
826 case 256: aesni_setkey_enc_256(rk, key); break;
Arto Kinnunen732ca322023-04-14 14:26:10 +0800827#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 default: return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100829 }
830
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 return 0;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100832}
833
Gilles Peskine9c682e72023-03-16 17:21:33 +0100834#endif /* MBEDTLS_AESNI_HAVE_CODE */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836#endif /* MBEDTLS_AESNI_C */