blob: 427c2fdc6d7bb25f4c7cae44a14aa53f2e1746b7 [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>
Tom Cosgrove02edb752023-03-13 15:32:52 +000038#endif
Gilles Peskined6719172023-03-10 22:37:11 +010039#include <immintrin.h>
40#endif
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010041
Jerry Yuc6284862023-08-16 16:08:42 +080042#if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY) || \
43 (defined(MBEDTLS_HAVE_X86) && defined(MBEDTLS_PADLOCK_C))
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010044/*
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010045 * AES-NI support detection routine
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010046 */
Gilles Peskine449bd832023-01-11 14:50:10 +010047int mbedtls_aesni_has_support(unsigned int what)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010048{
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010049 static int done = 0;
50 static unsigned int c = 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010051
Gilles Peskine449bd832023-01-11 14:50:10 +010052 if (!done) {
Gilles Peskine9c682e72023-03-16 17:21:33 +010053#if MBEDTLS_AESNI_HAVE_CODE == 2
Gilles Peskined6719172023-03-10 22:37:11 +010054 static unsigned info[4] = { 0, 0, 0, 0 };
55#if defined(_MSC_VER)
56 __cpuid(info, 1);
57#else
58 __cpuid(1, info[0], info[1], info[2], info[3]);
59#endif
60 c = info[2];
Gilles Peskine9c682e72023-03-16 17:21:33 +010061#else /* AESNI using asm */
Gilles Peskine449bd832023-01-11 14:50:10 +010062 asm ("movl $1, %%eax \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +020063 "cpuid \n\t"
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010064 : "=c" (c)
65 :
Gilles Peskine449bd832023-01-11 14:50:10 +010066 : "eax", "ebx", "edx");
Gilles Peskine9c682e72023-03-16 17:21:33 +010067#endif /* MBEDTLS_AESNI_HAVE_CODE */
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010068 done = 1;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010069 }
70
Gilles Peskine449bd832023-01-11 14:50:10 +010071 return (c & what) != 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010072}
Jerry Yu36606232023-04-19 10:44:29 +080073#endif /* !MBEDTLS_AES_USE_HARDWARE_ONLY */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010074
Gilles Peskine9c682e72023-03-16 17:21:33 +010075#if MBEDTLS_AESNI_HAVE_CODE == 2
Gilles Peskined6719172023-03-10 22:37:11 +010076
77/*
78 * AES-NI AES-ECB block en(de)cryption
79 */
80int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
81 int mode,
82 const unsigned char input[16],
83 unsigned char output[16])
84{
85 const __m128i *rk = (const __m128i *) (ctx->buf + ctx->rk_offset);
86 unsigned nr = ctx->nr; // Number of remaining rounds
Tom Cosgrove02edb752023-03-13 15:32:52 +000087
Gilles Peskined6719172023-03-10 22:37:11 +010088 // Load round key 0
Gilles Peskinedafeee42023-03-15 20:37:57 +010089 __m128i state;
90 memcpy(&state, input, 16);
91 state = _mm_xor_si128(state, rk[0]); // state ^= *rk;
Gilles Peskined6719172023-03-10 22:37:11 +010092 ++rk;
93 --nr;
94
95 if (mode == 0) {
96 while (nr != 0) {
Gilles Peskinedafeee42023-03-15 20:37:57 +010097 state = _mm_aesdec_si128(state, *rk);
Gilles Peskined6719172023-03-10 22:37:11 +010098 ++rk;
99 --nr;
100 }
Gilles Peskinedafeee42023-03-15 20:37:57 +0100101 state = _mm_aesdeclast_si128(state, *rk);
Gilles Peskined6719172023-03-10 22:37:11 +0100102 } else {
103 while (nr != 0) {
Gilles Peskinedafeee42023-03-15 20:37:57 +0100104 state = _mm_aesenc_si128(state, *rk);
Gilles Peskined6719172023-03-10 22:37:11 +0100105 ++rk;
106 --nr;
107 }
Gilles Peskinedafeee42023-03-15 20:37:57 +0100108 state = _mm_aesenclast_si128(state, *rk);
Gilles Peskined6719172023-03-10 22:37:11 +0100109 }
110
Gilles Peskinedafeee42023-03-15 20:37:57 +0100111 memcpy(output, &state, 16);
Gilles Peskined6719172023-03-10 22:37:11 +0100112 return 0;
113}
114
115/*
116 * GCM multiplication: c = a times b in GF(2^128)
117 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
118 */
119
120static void gcm_clmul(const __m128i aa, const __m128i bb,
121 __m128i *cc, __m128i *dd)
122{
123 /*
124 * Caryless multiplication dd:cc = aa * bb
125 * using [CLMUL-WP] algorithm 1 (p. 12).
126 */
127 *cc = _mm_clmulepi64_si128(aa, bb, 0x00); // a0*b0 = c1:c0
128 *dd = _mm_clmulepi64_si128(aa, bb, 0x11); // a1*b1 = d1:d0
129 __m128i ee = _mm_clmulepi64_si128(aa, bb, 0x10); // a0*b1 = e1:e0
130 __m128i ff = _mm_clmulepi64_si128(aa, bb, 0x01); // a1*b0 = f1:f0
Tom Cosgrove02edb752023-03-13 15:32:52 +0000131 ff = _mm_xor_si128(ff, ee); // e1+f1:e0+f0
Gilles Peskined6719172023-03-10 22:37:11 +0100132 ee = ff; // e1+f1:e0+f0
133 ff = _mm_srli_si128(ff, 8); // 0:e1+f1
134 ee = _mm_slli_si128(ee, 8); // e0+f0:0
Tom Cosgrove02edb752023-03-13 15:32:52 +0000135 *dd = _mm_xor_si128(*dd, ff); // d1:d0+e1+f1
Gilles Peskined0185f72023-03-16 13:08:18 +0100136 *cc = _mm_xor_si128(*cc, ee); // c1+e0+f0:c0
Gilles Peskined6719172023-03-10 22:37:11 +0100137}
138
139static void gcm_shift(__m128i *cc, __m128i *dd)
140{
Gilles Peskinedafeee42023-03-15 20:37:57 +0100141 /* [CMUCL-WP] Algorithm 5 Step 1: shift cc:dd one bit to the left,
142 * taking advantage of [CLMUL-WP] eq 27 (p. 18). */
143 // // *cc = r1:r0
144 // // *dd = r3:r2
145 __m128i cc_lo = _mm_slli_epi64(*cc, 1); // r1<<1:r0<<1
146 __m128i dd_lo = _mm_slli_epi64(*dd, 1); // r3<<1:r2<<1
147 __m128i cc_hi = _mm_srli_epi64(*cc, 63); // r1>>63:r0>>63
148 __m128i dd_hi = _mm_srli_epi64(*dd, 63); // r3>>63:r2>>63
149 __m128i xmm5 = _mm_srli_si128(cc_hi, 8); // 0:r1>>63
150 cc_hi = _mm_slli_si128(cc_hi, 8); // r0>>63:0
151 dd_hi = _mm_slli_si128(dd_hi, 8); // 0:r1>>63
Gilles Peskined6719172023-03-10 22:37:11 +0100152
Gilles Peskinedafeee42023-03-15 20:37:57 +0100153 *cc = _mm_or_si128(cc_lo, cc_hi); // r1<<1|r0>>63:r0<<1
154 *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 +0100155}
156
Gilles Peskinedafeee42023-03-15 20:37:57 +0100157static __m128i gcm_reduce(__m128i xx)
Gilles Peskined6719172023-03-10 22:37:11 +0100158{
159 // // xx = x1:x0
160 /* [CLMUL-WP] Algorithm 5 Step 2 */
161 __m128i aa = _mm_slli_epi64(xx, 63); // x1<<63:x0<<63 = stuff:a
162 __m128i bb = _mm_slli_epi64(xx, 62); // x1<<62:x0<<62 = stuff:b
163 __m128i cc = _mm_slli_epi64(xx, 57); // x1<<57:x0<<57 = stuff:c
Tom Cosgrove02edb752023-03-13 15:32:52 +0000164 __m128i dd = _mm_slli_si128(_mm_xor_si128(_mm_xor_si128(aa, bb), cc), 8); // a+b+c:0
165 return _mm_xor_si128(dd, xx); // x1+a+b+c:x0 = d:x0
Gilles Peskined6719172023-03-10 22:37:11 +0100166}
167
Gilles Peskinedafeee42023-03-15 20:37:57 +0100168static __m128i gcm_mix(__m128i dx)
Gilles Peskined6719172023-03-10 22:37:11 +0100169{
170 /* [CLMUL-WP] Algorithm 5 Steps 3 and 4 */
171 __m128i ee = _mm_srli_epi64(dx, 1); // e1:x0>>1 = e1:e0'
172 __m128i ff = _mm_srli_epi64(dx, 2); // f1:x0>>2 = f1:f0'
173 __m128i gg = _mm_srli_epi64(dx, 7); // g1:x0>>7 = g1:g0'
174
175 // e0'+f0'+g0' is almost e0+f0+g0, except for some missing
176 // bits carried from d. Now get those bits back in.
177 __m128i eh = _mm_slli_epi64(dx, 63); // d<<63:stuff
178 __m128i fh = _mm_slli_epi64(dx, 62); // d<<62:stuff
179 __m128i gh = _mm_slli_epi64(dx, 57); // d<<57:stuff
Tom Cosgrove02edb752023-03-13 15:32:52 +0000180 __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 +0100181
Tom Cosgrove02edb752023-03-13 15:32:52 +0000182 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 +0100183}
184
185void mbedtls_aesni_gcm_mult(unsigned char c[16],
186 const unsigned char a[16],
187 const unsigned char b[16])
188{
189 __m128i aa, bb, cc, dd;
190
191 /* The inputs are in big-endian order, so byte-reverse them */
192 for (size_t i = 0; i < 16; i++) {
193 ((uint8_t *) &aa)[i] = a[15 - i];
194 ((uint8_t *) &bb)[i] = b[15 - i];
195 }
196
197 gcm_clmul(aa, bb, &cc, &dd);
198 gcm_shift(&cc, &dd);
199 /*
200 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
201 * using [CLMUL-WP] algorithm 5 (p. 18).
202 * Currently dd:cc holds x3:x2:x1:x0 (already shifted).
203 */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100204 __m128i dx = gcm_reduce(cc);
205 __m128i xh = gcm_mix(dx);
Tom Cosgrove02edb752023-03-13 15:32:52 +0000206 cc = _mm_xor_si128(xh, dd); // x3+h1:x2+h0
Gilles Peskined6719172023-03-10 22:37:11 +0100207
208 /* Now byte-reverse the outputs */
209 for (size_t i = 0; i < 16; i++) {
210 c[i] = ((uint8_t *) &cc)[15 - i];
211 }
212
213 return;
214}
215
216/*
217 * Compute decryption round keys from encryption round keys
218 */
219void mbedtls_aesni_inverse_key(unsigned char *invkey,
220 const unsigned char *fwdkey, int nr)
221{
222 __m128i *ik = (__m128i *) invkey;
223 const __m128i *fk = (const __m128i *) fwdkey + nr;
224
225 *ik = *fk;
226 for (--fk, ++ik; fk > (const __m128i *) fwdkey; --fk, ++ik) {
227 *ik = _mm_aesimc_si128(*fk);
228 }
229 *ik = *fk;
230}
231
232/*
233 * Key expansion, 128-bit case
234 */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100235static __m128i aesni_set_rk_128(__m128i state, __m128i xword)
Gilles Peskined6719172023-03-10 22:37:11 +0100236{
237 /*
238 * Finish generating the next round key.
239 *
Gilles Peskinedafeee42023-03-15 20:37:57 +0100240 * On entry state is r3:r2:r1:r0 and xword is X:stuff:stuff:stuff
241 * with X = rot( sub( r3 ) ) ^ RCON (obtained with AESKEYGENASSIST).
Gilles Peskined6719172023-03-10 22:37:11 +0100242 *
Gilles Peskinedafeee42023-03-15 20:37:57 +0100243 * On exit, xword is r7:r6:r5:r4
Gilles Peskined6719172023-03-10 22:37:11 +0100244 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
245 * and this is returned, to be written to the round key buffer.
246 */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100247 xword = _mm_shuffle_epi32(xword, 0xff); // X:X:X:X
248 xword = _mm_xor_si128(xword, state); // X+r3:X+r2:X+r1:r4
249 state = _mm_slli_si128(state, 4); // r2:r1:r0:0
250 xword = _mm_xor_si128(xword, state); // X+r3+r2:X+r2+r1:r5:r4
251 state = _mm_slli_si128(state, 4); // r1:r0:0:0
252 xword = _mm_xor_si128(xword, state); // X+r3+r2+r1:r6:r5:r4
253 state = _mm_slli_si128(state, 4); // r0:0:0:0
254 state = _mm_xor_si128(xword, state); // r7:r6:r5:r4
255 return state;
Gilles Peskined6719172023-03-10 22:37:11 +0100256}
257
258static void aesni_setkey_enc_128(unsigned char *rk_bytes,
259 const unsigned char *key)
260{
261 __m128i *rk = (__m128i *) rk_bytes;
262
263 memcpy(&rk[0], key, 16);
264 rk[1] = aesni_set_rk_128(rk[0], _mm_aeskeygenassist_si128(rk[0], 0x01));
265 rk[2] = aesni_set_rk_128(rk[1], _mm_aeskeygenassist_si128(rk[1], 0x02));
266 rk[3] = aesni_set_rk_128(rk[2], _mm_aeskeygenassist_si128(rk[2], 0x04));
267 rk[4] = aesni_set_rk_128(rk[3], _mm_aeskeygenassist_si128(rk[3], 0x08));
268 rk[5] = aesni_set_rk_128(rk[4], _mm_aeskeygenassist_si128(rk[4], 0x10));
269 rk[6] = aesni_set_rk_128(rk[5], _mm_aeskeygenassist_si128(rk[5], 0x20));
270 rk[7] = aesni_set_rk_128(rk[6], _mm_aeskeygenassist_si128(rk[6], 0x40));
271 rk[8] = aesni_set_rk_128(rk[7], _mm_aeskeygenassist_si128(rk[7], 0x80));
272 rk[9] = aesni_set_rk_128(rk[8], _mm_aeskeygenassist_si128(rk[8], 0x1B));
273 rk[10] = aesni_set_rk_128(rk[9], _mm_aeskeygenassist_si128(rk[9], 0x36));
274}
275
276/*
277 * Key expansion, 192-bit case
278 */
Arto Kinnunen732ca322023-04-14 14:26:10 +0800279#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskinedafeee42023-03-15 20:37:57 +0100280static void aesni_set_rk_192(__m128i *state0, __m128i *state1, __m128i xword,
Gilles Peskined6719172023-03-10 22:37:11 +0100281 unsigned char *rk)
282{
283 /*
284 * Finish generating the next 6 quarter-keys.
285 *
Gilles Peskinedafeee42023-03-15 20:37:57 +0100286 * On entry state0 is r3:r2:r1:r0, state1 is stuff:stuff:r5:r4
287 * and xword is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON
288 * (obtained with AESKEYGENASSIST).
Gilles Peskined6719172023-03-10 22:37:11 +0100289 *
Gilles Peskinedafeee42023-03-15 20:37:57 +0100290 * On exit, state0 is r9:r8:r7:r6 and state1 is stuff:stuff:r11:r10
Gilles Peskined6719172023-03-10 22:37:11 +0100291 * and those are written to the round key buffer.
292 */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100293 xword = _mm_shuffle_epi32(xword, 0x55); // X:X:X:X
294 xword = _mm_xor_si128(xword, *state0); // X+r3:X+r2:X+r1:X+r0
295 *state0 = _mm_slli_si128(*state0, 4); // r2:r1:r0:0
296 xword = _mm_xor_si128(xword, *state0); // X+r3+r2:X+r2+r1:X+r1+r0:X+r0
297 *state0 = _mm_slli_si128(*state0, 4); // r1:r0:0:0
298 xword = _mm_xor_si128(xword, *state0); // X+r3+r2+r1:X+r2+r1+r0:X+r1+r0:X+r0
299 *state0 = _mm_slli_si128(*state0, 4); // r0:0:0:0
300 xword = _mm_xor_si128(xword, *state0); // X+r3+r2+r1+r0:X+r2+r1+r0:X+r1+r0:X+r0
301 *state0 = xword; // = r9:r8:r7:r6
Gilles Peskined6719172023-03-10 22:37:11 +0100302
Gilles Peskinedafeee42023-03-15 20:37:57 +0100303 xword = _mm_shuffle_epi32(xword, 0xff); // r9:r9:r9:r9
304 xword = _mm_xor_si128(xword, *state1); // stuff:stuff:r9+r5:r9+r4
305 *state1 = _mm_slli_si128(*state1, 4); // stuff:stuff:r4:0
306 xword = _mm_xor_si128(xword, *state1); // stuff:stuff:r9+r5+r4:r9+r4
307 *state1 = xword; // = stuff:stuff:r11:r10
Gilles Peskined6719172023-03-10 22:37:11 +0100308
Gilles Peskinedafeee42023-03-15 20:37:57 +0100309 /* Store state0 and the low half of state1 into rk, which is conceptually
Gilles Peskined6719172023-03-10 22:37:11 +0100310 * an array of 24-byte elements. Since 24 is not a multiple of 16,
Gilles Peskinedafeee42023-03-15 20:37:57 +0100311 * rk is not necessarily aligned so just `*rk = *state0` doesn't work. */
312 memcpy(rk, state0, 16);
Gilles Peskinedde3c652023-03-15 23:16:27 +0100313 memcpy(rk + 16, state1, 8);
Gilles Peskined6719172023-03-10 22:37:11 +0100314}
315
316static void aesni_setkey_enc_192(unsigned char *rk,
317 const unsigned char *key)
318{
319 /* First round: use original key */
320 memcpy(rk, key, 24);
321 /* aes.c guarantees that rk is aligned on a 16-byte boundary. */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100322 __m128i state0 = ((__m128i *) rk)[0];
323 __m128i state1 = _mm_loadl_epi64(((__m128i *) rk) + 1);
Gilles Peskined6719172023-03-10 22:37:11 +0100324
Gilles Peskinedafeee42023-03-15 20:37:57 +0100325 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x01), rk + 24 * 1);
326 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x02), rk + 24 * 2);
327 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x04), rk + 24 * 3);
328 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x08), rk + 24 * 4);
329 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x10), rk + 24 * 5);
330 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x20), rk + 24 * 6);
331 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x40), rk + 24 * 7);
332 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x80), rk + 24 * 8);
Gilles Peskined6719172023-03-10 22:37:11 +0100333}
Arto Kinnunen732ca322023-04-14 14:26:10 +0800334#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Gilles Peskined6719172023-03-10 22:37:11 +0100335
336/*
337 * Key expansion, 256-bit case
338 */
Arto Kinnunen732ca322023-04-14 14:26:10 +0800339#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskinedafeee42023-03-15 20:37:57 +0100340static void aesni_set_rk_256(__m128i state0, __m128i state1, __m128i xword,
Gilles Peskined6719172023-03-10 22:37:11 +0100341 __m128i *rk0, __m128i *rk1)
342{
343 /*
344 * Finish generating the next two round keys.
345 *
Gilles Peskinedafeee42023-03-15 20:37:57 +0100346 * On entry state0 is r3:r2:r1:r0, state1 is r7:r6:r5:r4 and
347 * xword is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
348 * (obtained with AESKEYGENASSIST).
Gilles Peskined6719172023-03-10 22:37:11 +0100349 *
350 * On exit, *rk0 is r11:r10:r9:r8 and *rk1 is r15:r14:r13:r12
351 */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100352 xword = _mm_shuffle_epi32(xword, 0xff);
353 xword = _mm_xor_si128(xword, state0);
354 state0 = _mm_slli_si128(state0, 4);
355 xword = _mm_xor_si128(xword, state0);
356 state0 = _mm_slli_si128(state0, 4);
357 xword = _mm_xor_si128(xword, state0);
358 state0 = _mm_slli_si128(state0, 4);
359 state0 = _mm_xor_si128(state0, xword);
360 *rk0 = state0;
Gilles Peskined6719172023-03-10 22:37:11 +0100361
Gilles Peskinedafeee42023-03-15 20:37:57 +0100362 /* Set xword to stuff:Y:stuff:stuff with Y = subword( r11 )
Gilles Peskined6719172023-03-10 22:37:11 +0100363 * and proceed to generate next round key from there */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100364 xword = _mm_aeskeygenassist_si128(state0, 0x00);
365 xword = _mm_shuffle_epi32(xword, 0xaa);
366 xword = _mm_xor_si128(xword, state1);
367 state1 = _mm_slli_si128(state1, 4);
368 xword = _mm_xor_si128(xword, state1);
369 state1 = _mm_slli_si128(state1, 4);
370 xword = _mm_xor_si128(xword, state1);
371 state1 = _mm_slli_si128(state1, 4);
372 state1 = _mm_xor_si128(state1, xword);
373 *rk1 = state1;
Gilles Peskined6719172023-03-10 22:37:11 +0100374}
375
376static void aesni_setkey_enc_256(unsigned char *rk_bytes,
377 const unsigned char *key)
378{
379 __m128i *rk = (__m128i *) rk_bytes;
380
381 memcpy(&rk[0], key, 16);
382 memcpy(&rk[1], key + 16, 16);
383
384 /*
385 * Main "loop" - Generating one more key than necessary,
386 * see definition of mbedtls_aes_context.buf
387 */
388 aesni_set_rk_256(rk[0], rk[1], _mm_aeskeygenassist_si128(rk[1], 0x01), &rk[2], &rk[3]);
389 aesni_set_rk_256(rk[2], rk[3], _mm_aeskeygenassist_si128(rk[3], 0x02), &rk[4], &rk[5]);
390 aesni_set_rk_256(rk[4], rk[5], _mm_aeskeygenassist_si128(rk[5], 0x04), &rk[6], &rk[7]);
391 aesni_set_rk_256(rk[6], rk[7], _mm_aeskeygenassist_si128(rk[7], 0x08), &rk[8], &rk[9]);
392 aesni_set_rk_256(rk[8], rk[9], _mm_aeskeygenassist_si128(rk[9], 0x10), &rk[10], &rk[11]);
393 aesni_set_rk_256(rk[10], rk[11], _mm_aeskeygenassist_si128(rk[11], 0x20), &rk[12], &rk[13]);
394 aesni_set_rk_256(rk[12], rk[13], _mm_aeskeygenassist_si128(rk[13], 0x40), &rk[14], &rk[15]);
395}
Arto Kinnunen732ca322023-04-14 14:26:10 +0800396#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Gilles Peskined6719172023-03-10 22:37:11 +0100397
Gilles Peskine9c682e72023-03-16 17:21:33 +0100398#else /* MBEDTLS_AESNI_HAVE_CODE == 1 */
Gilles Peskined6719172023-03-10 22:37:11 +0100399
Gilles Peskined0f9b0b2023-03-10 22:25:13 +0100400#if defined(__has_feature)
401#if __has_feature(memory_sanitizer)
402#warning \
403 "MBEDTLS_AESNI_C is known to cause spurious error reports with some memory sanitizers as they do not understand the assembly code."
404#endif
405#endif
406
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100407/*
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +0200408 * Binutils needs to be at least 2.19 to support AES-NI instructions.
409 * Unfortunately, a lot of users have a lower version now (2014-04).
410 * Emit bytecode directly in order to support "old" version of gas.
411 *
412 * Opcodes from the Intel architecture reference manual, vol. 3.
413 * We always use registers, so we don't need prefixes for memory operands.
414 * Operand macros are in gas order (src, dst) as opposed to Intel order
415 * (dst, src) in order to blend better into the surrounding assembly code.
416 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100417#define AESDEC(regs) ".byte 0x66,0x0F,0x38,0xDE," regs "\n\t"
418#define AESDECLAST(regs) ".byte 0x66,0x0F,0x38,0xDF," regs "\n\t"
419#define AESENC(regs) ".byte 0x66,0x0F,0x38,0xDC," regs "\n\t"
420#define AESENCLAST(regs) ".byte 0x66,0x0F,0x38,0xDD," regs "\n\t"
421#define AESIMC(regs) ".byte 0x66,0x0F,0x38,0xDB," regs "\n\t"
422#define AESKEYGENA(regs, imm) ".byte 0x66,0x0F,0x3A,0xDF," regs "," imm "\n\t"
423#define PCLMULQDQ(regs, imm) ".byte 0x66,0x0F,0x3A,0x44," regs "," imm "\n\t"
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +0200424
425#define xmm0_xmm0 "0xC0"
426#define xmm0_xmm1 "0xC8"
427#define xmm0_xmm2 "0xD0"
428#define xmm0_xmm3 "0xD8"
429#define xmm0_xmm4 "0xE0"
430#define xmm1_xmm0 "0xC1"
431#define xmm1_xmm2 "0xD1"
432
433/*
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100434 * AES-NI AES-ECB block en(de)cryption
435 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100436int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
437 int mode,
438 const unsigned char input[16],
439 unsigned char output[16])
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100440{
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 asm ("movdqu (%3), %%xmm0 \n\t" // load input
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200442 "movdqu (%1), %%xmm1 \n\t" // load round key 0
443 "pxor %%xmm1, %%xmm0 \n\t" // round 0
James Cowgill6c8edca2015-12-17 01:40:26 +0000444 "add $16, %1 \n\t" // point to next round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200445 "subl $1, %0 \n\t" // normal rounds = nr - 1
446 "test %2, %2 \n\t" // mode?
447 "jz 2f \n\t" // 0 = decrypt
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100448
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200449 "1: \n\t" // encryption loop
450 "movdqu (%1), %%xmm1 \n\t" // load round key
Gilles Peskine4e201442023-03-15 19:36:03 +0100451 AESENC(xmm1_xmm0) // do round
452 "add $16, %1 \n\t" // point to next round key
453 "subl $1, %0 \n\t" // loop
454 "jnz 1b \n\t"
455 "movdqu (%1), %%xmm1 \n\t" // load round key
456 AESENCLAST(xmm1_xmm0) // last round
457 "jmp 3f \n\t"
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100458
Gilles Peskine4e201442023-03-15 19:36:03 +0100459 "2: \n\t" // decryption loop
460 "movdqu (%1), %%xmm1 \n\t"
461 AESDEC(xmm1_xmm0) // do round
462 "add $16, %1 \n\t"
463 "subl $1, %0 \n\t"
464 "jnz 2b \n\t"
465 "movdqu (%1), %%xmm1 \n\t" // load round key
466 AESDECLAST(xmm1_xmm0) // last round
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100467
Gilles Peskine4e201442023-03-15 19:36:03 +0100468 "3: \n\t"
469 "movdqu %%xmm0, (%4) \n\t" // export output
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100470 :
Werner Lewisdd76ef32022-05-30 12:00:21 +0100471 : "r" (ctx->nr), "r" (ctx->buf + ctx->rk_offset), "r" (mode), "r" (input), "r" (output)
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 : "memory", "cc", "xmm0", "xmm1");
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100473
474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 return 0;
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100476}
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100477
478/*
479 * GCM multiplication: c = a times b in GF(2^128)
480 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
481 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100482void mbedtls_aesni_gcm_mult(unsigned char c[16],
483 const unsigned char a[16],
484 const unsigned char b[16])
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100485{
486 unsigned char aa[16], bb[16], cc[16];
487 size_t i;
488
489 /* The inputs are in big-endian order, so byte-reverse them */
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 for (i = 0; i < 16; i++) {
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100491 aa[i] = a[15 - i];
492 bb[i] = b[15 - i];
493 }
494
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 asm ("movdqu (%0), %%xmm0 \n\t" // a1:a0
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200496 "movdqu (%1), %%xmm1 \n\t" // b1:b0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100497
498 /*
499 * Caryless multiplication xmm2:xmm1 = xmm0 * xmm1
Gilles Peskined4f31c82023-03-10 22:21:47 +0100500 * using [CLMUL-WP] algorithm 1 (p. 12).
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100501 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200502 "movdqa %%xmm1, %%xmm2 \n\t" // copy of b1:b0
503 "movdqa %%xmm1, %%xmm3 \n\t" // same
504 "movdqa %%xmm1, %%xmm4 \n\t" // same
Gilles Peskine4e201442023-03-15 19:36:03 +0100505 PCLMULQDQ(xmm0_xmm1, "0x00") // a0*b0 = c1:c0
506 PCLMULQDQ(xmm0_xmm2, "0x11") // a1*b1 = d1:d0
507 PCLMULQDQ(xmm0_xmm3, "0x10") // a0*b1 = e1:e0
508 PCLMULQDQ(xmm0_xmm4, "0x01") // a1*b0 = f1:f0
509 "pxor %%xmm3, %%xmm4 \n\t" // e1+f1:e0+f0
510 "movdqa %%xmm4, %%xmm3 \n\t" // same
511 "psrldq $8, %%xmm4 \n\t" // 0:e1+f1
512 "pslldq $8, %%xmm3 \n\t" // e0+f0:0
513 "pxor %%xmm4, %%xmm2 \n\t" // d1:d0+e1+f1
514 "pxor %%xmm3, %%xmm1 \n\t" // c1+e0+f1:c0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100515
516 /*
517 * Now shift the result one bit to the left,
Gilles Peskined4f31c82023-03-10 22:21:47 +0100518 * taking advantage of [CLMUL-WP] eq 27 (p. 18)
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100519 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100520 "movdqa %%xmm1, %%xmm3 \n\t" // r1:r0
521 "movdqa %%xmm2, %%xmm4 \n\t" // r3:r2
522 "psllq $1, %%xmm1 \n\t" // r1<<1:r0<<1
523 "psllq $1, %%xmm2 \n\t" // r3<<1:r2<<1
524 "psrlq $63, %%xmm3 \n\t" // r1>>63:r0>>63
525 "psrlq $63, %%xmm4 \n\t" // r3>>63:r2>>63
526 "movdqa %%xmm3, %%xmm5 \n\t" // r1>>63:r0>>63
527 "pslldq $8, %%xmm3 \n\t" // r0>>63:0
528 "pslldq $8, %%xmm4 \n\t" // r2>>63:0
529 "psrldq $8, %%xmm5 \n\t" // 0:r1>>63
530 "por %%xmm3, %%xmm1 \n\t" // r1<<1|r0>>63:r0<<1
531 "por %%xmm4, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1
532 "por %%xmm5, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1|r1>>63
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100533
534 /*
535 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
Gilles Peskined4f31c82023-03-10 22:21:47 +0100536 * using [CLMUL-WP] algorithm 5 (p. 18).
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100537 * Currently xmm2:xmm1 holds x3:x2:x1:x0 (already shifted).
538 */
539 /* Step 2 (1) */
Gilles Peskine4e201442023-03-15 19:36:03 +0100540 "movdqa %%xmm1, %%xmm3 \n\t" // x1:x0
541 "movdqa %%xmm1, %%xmm4 \n\t" // same
542 "movdqa %%xmm1, %%xmm5 \n\t" // same
543 "psllq $63, %%xmm3 \n\t" // x1<<63:x0<<63 = stuff:a
544 "psllq $62, %%xmm4 \n\t" // x1<<62:x0<<62 = stuff:b
545 "psllq $57, %%xmm5 \n\t" // x1<<57:x0<<57 = stuff:c
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100546
547 /* Step 2 (2) */
Gilles Peskine4e201442023-03-15 19:36:03 +0100548 "pxor %%xmm4, %%xmm3 \n\t" // stuff:a+b
549 "pxor %%xmm5, %%xmm3 \n\t" // stuff:a+b+c
550 "pslldq $8, %%xmm3 \n\t" // a+b+c:0
551 "pxor %%xmm3, %%xmm1 \n\t" // x1+a+b+c:x0 = d:x0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100552
553 /* Steps 3 and 4 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100554 "movdqa %%xmm1,%%xmm0 \n\t" // d:x0
555 "movdqa %%xmm1,%%xmm4 \n\t" // same
556 "movdqa %%xmm1,%%xmm5 \n\t" // same
557 "psrlq $1, %%xmm0 \n\t" // e1:x0>>1 = e1:e0'
558 "psrlq $2, %%xmm4 \n\t" // f1:x0>>2 = f1:f0'
559 "psrlq $7, %%xmm5 \n\t" // g1:x0>>7 = g1:g0'
560 "pxor %%xmm4, %%xmm0 \n\t" // e1+f1:e0'+f0'
561 "pxor %%xmm5, %%xmm0 \n\t" // e1+f1+g1:e0'+f0'+g0'
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200562 // e0'+f0'+g0' is almost e0+f0+g0, ex\tcept for some missing
563 // bits carried from d. Now get those\t bits back in.
Gilles Peskine4e201442023-03-15 19:36:03 +0100564 "movdqa %%xmm1,%%xmm3 \n\t" // d:x0
565 "movdqa %%xmm1,%%xmm4 \n\t" // same
566 "movdqa %%xmm1,%%xmm5 \n\t" // same
567 "psllq $63, %%xmm3 \n\t" // d<<63:stuff
568 "psllq $62, %%xmm4 \n\t" // d<<62:stuff
569 "psllq $57, %%xmm5 \n\t" // d<<57:stuff
570 "pxor %%xmm4, %%xmm3 \n\t" // d<<63+d<<62:stuff
571 "pxor %%xmm5, %%xmm3 \n\t" // missing bits of d:stuff
572 "psrldq $8, %%xmm3 \n\t" // 0:missing bits of d
573 "pxor %%xmm3, %%xmm0 \n\t" // e1+f1+g1:e0+f0+g0
574 "pxor %%xmm1, %%xmm0 \n\t" // h1:h0
575 "pxor %%xmm2, %%xmm0 \n\t" // x3+h1:x2+h0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100576
Gilles Peskine4e201442023-03-15 19:36:03 +0100577 "movdqu %%xmm0, (%2) \n\t" // done
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100578 :
579 : "r" (aa), "r" (bb), "r" (cc)
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100581
582 /* Now byte-reverse the outputs */
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 for (i = 0; i < 16; i++) {
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100584 c[i] = cc[15 - i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 }
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100586
Paul Bakkere7f51332013-12-30 15:32:02 +0100587 return;
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100588}
589
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100590/*
591 * Compute decryption round keys from encryption round keys
592 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100593void mbedtls_aesni_inverse_key(unsigned char *invkey,
594 const unsigned char *fwdkey, int nr)
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100595{
596 unsigned char *ik = invkey;
597 const unsigned char *fk = fwdkey + 16 * nr;
598
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100600
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 for (fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16) {
602 asm ("movdqu (%0), %%xmm0 \n\t"
Gilles Peskine4e201442023-03-15 19:36:03 +0100603 AESIMC(xmm0_xmm0)
604 "movdqu %%xmm0, (%1) \n\t"
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100605 :
606 : "r" (fk), "r" (ik)
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 : "memory", "xmm0");
608 }
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100611}
612
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100613/*
614 * Key expansion, 128-bit case
615 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100616static void aesni_setkey_enc_128(unsigned char *rk,
617 const unsigned char *key)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100618{
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 asm ("movdqu (%1), %%xmm0 \n\t" // copy the original key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200620 "movdqu %%xmm0, (%0) \n\t" // as round key 0
621 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100622
623 /*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100624 * Finish generating the next round key.
625 *
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100626 * On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff
627 * with X = rot( sub( r3 ) ) ^ RCON.
628 *
629 * On exit, xmm0 is r7:r6:r5:r4
630 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
631 * and those are written to the round key buffer.
632 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200633 "1: \n\t"
634 "pshufd $0xff, %%xmm1, %%xmm1 \n\t" // X:X:X:X
635 "pxor %%xmm0, %%xmm1 \n\t" // X+r3:X+r2:X+r1:r4
636 "pslldq $4, %%xmm0 \n\t" // r2:r1:r0:0
637 "pxor %%xmm0, %%xmm1 \n\t" // X+r3+r2:X+r2+r1:r5:r4
638 "pslldq $4, %%xmm0 \n\t" // etc
639 "pxor %%xmm0, %%xmm1 \n\t"
640 "pslldq $4, %%xmm0 \n\t"
641 "pxor %%xmm1, %%xmm0 \n\t" // update xmm0 for next time!
642 "add $16, %0 \n\t" // point to next round key
643 "movdqu %%xmm0, (%0) \n\t" // write it
644 "ret \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100645
646 /* Main "loop" */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200647 "2: \n\t"
Gilles Peskine4e201442023-03-15 19:36:03 +0100648 AESKEYGENA(xmm0_xmm1, "0x01") "call 1b \n\t"
649 AESKEYGENA(xmm0_xmm1, "0x02") "call 1b \n\t"
650 AESKEYGENA(xmm0_xmm1, "0x04") "call 1b \n\t"
651 AESKEYGENA(xmm0_xmm1, "0x08") "call 1b \n\t"
652 AESKEYGENA(xmm0_xmm1, "0x10") "call 1b \n\t"
653 AESKEYGENA(xmm0_xmm1, "0x20") "call 1b \n\t"
654 AESKEYGENA(xmm0_xmm1, "0x40") "call 1b \n\t"
655 AESKEYGENA(xmm0_xmm1, "0x80") "call 1b \n\t"
656 AESKEYGENA(xmm0_xmm1, "0x1B") "call 1b \n\t"
657 AESKEYGENA(xmm0_xmm1, "0x36") "call 1b \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100658 :
659 : "r" (rk), "r" (key)
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 : "memory", "cc", "0");
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100661}
662
663/*
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100664 * Key expansion, 192-bit case
665 */
Arto Kinnunen732ca322023-04-14 14:26:10 +0800666#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100667static void aesni_setkey_enc_192(unsigned char *rk,
668 const unsigned char *key)
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100669{
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 asm ("movdqu (%1), %%xmm0 \n\t" // copy original round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200671 "movdqu %%xmm0, (%0) \n\t"
672 "add $16, %0 \n\t"
673 "movq 16(%1), %%xmm1 \n\t"
674 "movq %%xmm1, (%0) \n\t"
675 "add $8, %0 \n\t"
676 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100677
678 /*
679 * Finish generating the next 6 quarter-keys.
680 *
681 * On entry xmm0 is r3:r2:r1:r0, xmm1 is stuff:stuff:r5:r4
682 * and xmm2 is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON.
683 *
684 * On exit, xmm0 is r9:r8:r7:r6 and xmm1 is stuff:stuff:r11:r10
685 * and those are written to the round key buffer.
686 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200687 "1: \n\t"
688 "pshufd $0x55, %%xmm2, %%xmm2 \n\t" // X:X:X:X
689 "pxor %%xmm0, %%xmm2 \n\t" // X+r3:X+r2:X+r1:r4
690 "pslldq $4, %%xmm0 \n\t" // etc
691 "pxor %%xmm0, %%xmm2 \n\t"
692 "pslldq $4, %%xmm0 \n\t"
693 "pxor %%xmm0, %%xmm2 \n\t"
694 "pslldq $4, %%xmm0 \n\t"
695 "pxor %%xmm2, %%xmm0 \n\t" // update xmm0 = r9:r8:r7:r6
696 "movdqu %%xmm0, (%0) \n\t"
697 "add $16, %0 \n\t"
698 "pshufd $0xff, %%xmm0, %%xmm2 \n\t" // r9:r9:r9:r9
699 "pxor %%xmm1, %%xmm2 \n\t" // stuff:stuff:r9+r5:r10
700 "pslldq $4, %%xmm1 \n\t" // r2:r1:r0:0
701 "pxor %%xmm2, %%xmm1 \n\t" // xmm1 = stuff:stuff:r11:r10
702 "movq %%xmm1, (%0) \n\t"
703 "add $8, %0 \n\t"
704 "ret \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100705
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200706 "2: \n\t"
Gilles Peskine4e201442023-03-15 19:36:03 +0100707 AESKEYGENA(xmm1_xmm2, "0x01") "call 1b \n\t"
708 AESKEYGENA(xmm1_xmm2, "0x02") "call 1b \n\t"
709 AESKEYGENA(xmm1_xmm2, "0x04") "call 1b \n\t"
710 AESKEYGENA(xmm1_xmm2, "0x08") "call 1b \n\t"
711 AESKEYGENA(xmm1_xmm2, "0x10") "call 1b \n\t"
712 AESKEYGENA(xmm1_xmm2, "0x20") "call 1b \n\t"
713 AESKEYGENA(xmm1_xmm2, "0x40") "call 1b \n\t"
714 AESKEYGENA(xmm1_xmm2, "0x80") "call 1b \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100715
716 :
717 : "r" (rk), "r" (key)
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 : "memory", "cc", "0");
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100719}
Arto Kinnunen732ca322023-04-14 14:26:10 +0800720#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100721
722/*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100723 * Key expansion, 256-bit case
724 */
Arto Kinnunen732ca322023-04-14 14:26:10 +0800725#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100726static void aesni_setkey_enc_256(unsigned char *rk,
727 const unsigned char *key)
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100728{
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 asm ("movdqu (%1), %%xmm0 \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200730 "movdqu %%xmm0, (%0) \n\t"
731 "add $16, %0 \n\t"
732 "movdqu 16(%1), %%xmm1 \n\t"
733 "movdqu %%xmm1, (%0) \n\t"
734 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100735
736 /*
737 * Finish generating the next two round keys.
738 *
739 * On entry xmm0 is r3:r2:r1:r0, xmm1 is r7:r6:r5:r4 and
740 * xmm2 is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
741 *
742 * On exit, xmm0 is r11:r10:r9:r8 and xmm1 is r15:r14:r13:r12
743 * and those have been written to the output buffer.
744 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200745 "1: \n\t"
746 "pshufd $0xff, %%xmm2, %%xmm2 \n\t"
747 "pxor %%xmm0, %%xmm2 \n\t"
748 "pslldq $4, %%xmm0 \n\t"
749 "pxor %%xmm0, %%xmm2 \n\t"
750 "pslldq $4, %%xmm0 \n\t"
751 "pxor %%xmm0, %%xmm2 \n\t"
752 "pslldq $4, %%xmm0 \n\t"
753 "pxor %%xmm2, %%xmm0 \n\t"
754 "add $16, %0 \n\t"
755 "movdqu %%xmm0, (%0) \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100756
757 /* Set xmm2 to stuff:Y:stuff:stuff with Y = subword( r11 )
758 * and proceed to generate next round key from there */
Gilles Peskine4e201442023-03-15 19:36:03 +0100759 AESKEYGENA(xmm0_xmm2, "0x00")
760 "pshufd $0xaa, %%xmm2, %%xmm2 \n\t"
761 "pxor %%xmm1, %%xmm2 \n\t"
762 "pslldq $4, %%xmm1 \n\t"
763 "pxor %%xmm1, %%xmm2 \n\t"
764 "pslldq $4, %%xmm1 \n\t"
765 "pxor %%xmm1, %%xmm2 \n\t"
766 "pslldq $4, %%xmm1 \n\t"
767 "pxor %%xmm2, %%xmm1 \n\t"
768 "add $16, %0 \n\t"
769 "movdqu %%xmm1, (%0) \n\t"
770 "ret \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100771
772 /*
773 * Main "loop" - Generating one more key than necessary,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 * see definition of mbedtls_aes_context.buf
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100775 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100776 "2: \n\t"
777 AESKEYGENA(xmm1_xmm2, "0x01") "call 1b \n\t"
778 AESKEYGENA(xmm1_xmm2, "0x02") "call 1b \n\t"
779 AESKEYGENA(xmm1_xmm2, "0x04") "call 1b \n\t"
780 AESKEYGENA(xmm1_xmm2, "0x08") "call 1b \n\t"
781 AESKEYGENA(xmm1_xmm2, "0x10") "call 1b \n\t"
782 AESKEYGENA(xmm1_xmm2, "0x20") "call 1b \n\t"
783 AESKEYGENA(xmm1_xmm2, "0x40") "call 1b \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100784 :
785 : "r" (rk), "r" (key)
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 : "memory", "cc", "0");
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100787}
Arto Kinnunen732ca322023-04-14 14:26:10 +0800788#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100789
Gilles Peskine9c682e72023-03-16 17:21:33 +0100790#endif /* MBEDTLS_AESNI_HAVE_CODE */
Gilles Peskined6719172023-03-10 22:37:11 +0100791
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100792/*
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100793 * Key expansion, wrapper
794 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100795int mbedtls_aesni_setkey_enc(unsigned char *rk,
796 const unsigned char *key,
797 size_t bits)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100798{
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 switch (bits) {
800 case 128: aesni_setkey_enc_128(rk, key); break;
Arto Kinnunen732ca322023-04-14 14:26:10 +0800801#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 case 192: aesni_setkey_enc_192(rk, key); break;
803 case 256: aesni_setkey_enc_256(rk, key); break;
Arto Kinnunen732ca322023-04-14 14:26:10 +0800804#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 default: return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100806 }
807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 return 0;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100809}
810
Gilles Peskine9c682e72023-03-16 17:21:33 +0100811#endif /* MBEDTLS_AESNI_HAVE_CODE */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813#endif /* MBEDTLS_AESNI_C */