blob: 77c1f9024be4c2b410e385069c6bd28a5d8aad9f [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 Peskine6055b782023-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
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/aesni.h"
Rich Evans00ab4702015-02-06 13:43:58 +000030
31#include <string.h>
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010032
David Horstmannb6bf5f52023-01-03 11:07:09 +000033/* *INDENT-OFF* */
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020034#ifndef asm
35#define asm __asm
36#endif
David Horstmannb6bf5f52023-01-03 11:07:09 +000037/* *INDENT-ON* */
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020038
Gilles Peskine6dec5412023-03-16 17:21:33 +010039#if defined(MBEDTLS_AESNI_HAVE_CODE)
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010040
Gilles Peskine6dec5412023-03-16 17:21:33 +010041#if MBEDTLS_AESNI_HAVE_CODE == 2
Pengyu Lvf3c6e2e2023-10-11 10:36:55 +080042#if defined(__GNUC__)
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010043#include <cpuid.h>
Pengyu Lvf3c6e2e2023-10-11 10:36:55 +080044#elif defined(_MSC_VER)
SlugFillere2d06142023-06-23 06:24:49 +030045#include <intrin.h>
Pengyu Lvf3c6e2e2023-10-11 10:36:55 +080046#else
47#error "`__cpuid` required by MBEDTLS_AESNI_C is not supported by the compiler"
Tom Cosgrove790756d2023-03-13 15:32:52 +000048#endif
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010049#include <immintrin.h>
50#endif
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010051
52/*
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010053 * AES-NI support detection routine
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010054 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010055int mbedtls_aesni_has_support(unsigned int what)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010056{
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010057 static int done = 0;
58 static unsigned int c = 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010059
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010060 if (!done) {
Gilles Peskine6dec5412023-03-16 17:21:33 +010061#if MBEDTLS_AESNI_HAVE_CODE == 2
Pengyu Lv79d7faf2023-10-10 18:12:43 +080062 static int info[4] = { 0, 0, 0, 0 };
Pengyu Lvf3c6e2e2023-10-11 10:36:55 +080063#if defined(_MSC_VER)
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010064 __cpuid(info, 1);
65#else
66 __cpuid(1, info[0], info[1], info[2], info[3]);
67#endif
68 c = info[2];
Gilles Peskine6dec5412023-03-16 17:21:33 +010069#else /* AESNI using asm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010070 asm ("movl $1, %%eax \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +020071 "cpuid \n\t"
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010072 : "=c" (c)
73 :
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 : "eax", "ebx", "edx");
Gilles Peskine6dec5412023-03-16 17:21:33 +010075#endif /* MBEDTLS_AESNI_HAVE_CODE */
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010076 done = 1;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010077 }
78
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010079 return (c & what) != 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010080}
81
Gilles Peskine6dec5412023-03-16 17:21:33 +010082#if MBEDTLS_AESNI_HAVE_CODE == 2
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010083
84/*
85 * AES-NI AES-ECB block en(de)cryption
86 */
87int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
88 int mode,
89 const unsigned char input[16],
90 unsigned char output[16])
91{
92 const __m128i *rk = (const __m128i *) (ctx->rk);
93 unsigned nr = ctx->nr; // Number of remaining rounds
Tom Cosgrove790756d2023-03-13 15:32:52 +000094
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010095 // Load round key 0
Gilles Peskined4a23932023-03-15 20:37:57 +010096 __m128i state;
97 memcpy(&state, input, 16);
98 state = _mm_xor_si128(state, rk[0]); // state ^= *rk;
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010099 ++rk;
100 --nr;
101
102 if (mode == 0) {
103 while (nr != 0) {
Gilles Peskined4a23932023-03-15 20:37:57 +0100104 state = _mm_aesdec_si128(state, *rk);
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100105 ++rk;
106 --nr;
107 }
Gilles Peskined4a23932023-03-15 20:37:57 +0100108 state = _mm_aesdeclast_si128(state, *rk);
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100109 } else {
110 while (nr != 0) {
Gilles Peskined4a23932023-03-15 20:37:57 +0100111 state = _mm_aesenc_si128(state, *rk);
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100112 ++rk;
113 --nr;
114 }
Gilles Peskined4a23932023-03-15 20:37:57 +0100115 state = _mm_aesenclast_si128(state, *rk);
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100116 }
117
Gilles Peskined4a23932023-03-15 20:37:57 +0100118 memcpy(output, &state, 16);
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100119 return 0;
120}
121
122/*
123 * GCM multiplication: c = a times b in GF(2^128)
124 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
125 */
126
127static void gcm_clmul(const __m128i aa, const __m128i bb,
128 __m128i *cc, __m128i *dd)
129{
130 /*
131 * Caryless multiplication dd:cc = aa * bb
132 * using [CLMUL-WP] algorithm 1 (p. 12).
133 */
134 *cc = _mm_clmulepi64_si128(aa, bb, 0x00); // a0*b0 = c1:c0
135 *dd = _mm_clmulepi64_si128(aa, bb, 0x11); // a1*b1 = d1:d0
136 __m128i ee = _mm_clmulepi64_si128(aa, bb, 0x10); // a0*b1 = e1:e0
137 __m128i ff = _mm_clmulepi64_si128(aa, bb, 0x01); // a1*b0 = f1:f0
Tom Cosgrove790756d2023-03-13 15:32:52 +0000138 ff = _mm_xor_si128(ff, ee); // e1+f1:e0+f0
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100139 ee = ff; // e1+f1:e0+f0
140 ff = _mm_srli_si128(ff, 8); // 0:e1+f1
141 ee = _mm_slli_si128(ee, 8); // e0+f0:0
Tom Cosgrove790756d2023-03-13 15:32:52 +0000142 *dd = _mm_xor_si128(*dd, ff); // d1:d0+e1+f1
Gilles Peskine5f1677f2023-03-16 13:08:18 +0100143 *cc = _mm_xor_si128(*cc, ee); // c1+e0+f0:c0
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100144}
145
146static void gcm_shift(__m128i *cc, __m128i *dd)
147{
Gilles Peskined4a23932023-03-15 20:37:57 +0100148 /* [CMUCL-WP] Algorithm 5 Step 1: shift cc:dd one bit to the left,
149 * taking advantage of [CLMUL-WP] eq 27 (p. 18). */
150 // // *cc = r1:r0
151 // // *dd = r3:r2
152 __m128i cc_lo = _mm_slli_epi64(*cc, 1); // r1<<1:r0<<1
153 __m128i dd_lo = _mm_slli_epi64(*dd, 1); // r3<<1:r2<<1
154 __m128i cc_hi = _mm_srli_epi64(*cc, 63); // r1>>63:r0>>63
155 __m128i dd_hi = _mm_srli_epi64(*dd, 63); // r3>>63:r2>>63
156 __m128i xmm5 = _mm_srli_si128(cc_hi, 8); // 0:r1>>63
157 cc_hi = _mm_slli_si128(cc_hi, 8); // r0>>63:0
158 dd_hi = _mm_slli_si128(dd_hi, 8); // 0:r1>>63
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100159
Gilles Peskined4a23932023-03-15 20:37:57 +0100160 *cc = _mm_or_si128(cc_lo, cc_hi); // r1<<1|r0>>63:r0<<1
161 *dd = _mm_or_si128(_mm_or_si128(dd_lo, dd_hi), xmm5); // r3<<1|r2>>62:r2<<1|r1>>63
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100162}
163
Gilles Peskined4a23932023-03-15 20:37:57 +0100164static __m128i gcm_reduce(__m128i xx)
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100165{
166 // // xx = x1:x0
167 /* [CLMUL-WP] Algorithm 5 Step 2 */
168 __m128i aa = _mm_slli_epi64(xx, 63); // x1<<63:x0<<63 = stuff:a
169 __m128i bb = _mm_slli_epi64(xx, 62); // x1<<62:x0<<62 = stuff:b
170 __m128i cc = _mm_slli_epi64(xx, 57); // x1<<57:x0<<57 = stuff:c
Tom Cosgrove790756d2023-03-13 15:32:52 +0000171 __m128i dd = _mm_slli_si128(_mm_xor_si128(_mm_xor_si128(aa, bb), cc), 8); // a+b+c:0
172 return _mm_xor_si128(dd, xx); // x1+a+b+c:x0 = d:x0
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100173}
174
Gilles Peskined4a23932023-03-15 20:37:57 +0100175static __m128i gcm_mix(__m128i dx)
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100176{
177 /* [CLMUL-WP] Algorithm 5 Steps 3 and 4 */
178 __m128i ee = _mm_srli_epi64(dx, 1); // e1:x0>>1 = e1:e0'
179 __m128i ff = _mm_srli_epi64(dx, 2); // f1:x0>>2 = f1:f0'
180 __m128i gg = _mm_srli_epi64(dx, 7); // g1:x0>>7 = g1:g0'
181
182 // e0'+f0'+g0' is almost e0+f0+g0, except for some missing
183 // bits carried from d. Now get those bits back in.
184 __m128i eh = _mm_slli_epi64(dx, 63); // d<<63:stuff
185 __m128i fh = _mm_slli_epi64(dx, 62); // d<<62:stuff
186 __m128i gh = _mm_slli_epi64(dx, 57); // d<<57:stuff
Tom Cosgrove790756d2023-03-13 15:32:52 +0000187 __m128i hh = _mm_srli_si128(_mm_xor_si128(_mm_xor_si128(eh, fh), gh), 8); // 0:missing bits of d
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100188
Tom Cosgrove790756d2023-03-13 15:32:52 +0000189 return _mm_xor_si128(_mm_xor_si128(_mm_xor_si128(_mm_xor_si128(ee, ff), gg), hh), dx);
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100190}
191
192void mbedtls_aesni_gcm_mult(unsigned char c[16],
193 const unsigned char a[16],
194 const unsigned char b[16])
195{
196 __m128i aa, bb, cc, dd;
197
198 /* The inputs are in big-endian order, so byte-reverse them */
199 for (size_t i = 0; i < 16; i++) {
200 ((uint8_t *) &aa)[i] = a[15 - i];
201 ((uint8_t *) &bb)[i] = b[15 - i];
202 }
203
204 gcm_clmul(aa, bb, &cc, &dd);
205 gcm_shift(&cc, &dd);
206 /*
207 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
208 * using [CLMUL-WP] algorithm 5 (p. 18).
209 * Currently dd:cc holds x3:x2:x1:x0 (already shifted).
210 */
Gilles Peskined4a23932023-03-15 20:37:57 +0100211 __m128i dx = gcm_reduce(cc);
212 __m128i xh = gcm_mix(dx);
Tom Cosgrove790756d2023-03-13 15:32:52 +0000213 cc = _mm_xor_si128(xh, dd); // x3+h1:x2+h0
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100214
215 /* Now byte-reverse the outputs */
216 for (size_t i = 0; i < 16; i++) {
217 c[i] = ((uint8_t *) &cc)[15 - i];
218 }
219
220 return;
221}
222
223/*
224 * Compute decryption round keys from encryption round keys
225 */
226void 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}
238
239/*
240 * Key expansion, 128-bit case
241 */
Gilles Peskined4a23932023-03-15 20:37:57 +0100242static __m128i aesni_set_rk_128(__m128i state, __m128i xword)
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100243{
244 /*
245 * Finish generating the next round key.
246 *
Gilles Peskined4a23932023-03-15 20:37:57 +0100247 * On entry state is r3:r2:r1:r0 and xword is X:stuff:stuff:stuff
248 * with X = rot( sub( r3 ) ) ^ RCON (obtained with AESKEYGENASSIST).
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100249 *
Gilles Peskined4a23932023-03-15 20:37:57 +0100250 * On exit, xword is r7:r6:r5:r4
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100251 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
252 * and this is returned, to be written to the round key buffer.
253 */
Gilles Peskined4a23932023-03-15 20:37:57 +0100254 xword = _mm_shuffle_epi32(xword, 0xff); // X:X:X:X
255 xword = _mm_xor_si128(xword, state); // X+r3:X+r2:X+r1:r4
256 state = _mm_slli_si128(state, 4); // r2:r1:r0:0
257 xword = _mm_xor_si128(xword, state); // X+r3+r2:X+r2+r1:r5:r4
258 state = _mm_slli_si128(state, 4); // r1:r0:0:0
259 xword = _mm_xor_si128(xword, state); // X+r3+r2+r1:r6:r5:r4
260 state = _mm_slli_si128(state, 4); // r0:0:0:0
261 state = _mm_xor_si128(xword, state); // r7:r6:r5:r4
262 return state;
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100263}
264
265static void aesni_setkey_enc_128(unsigned char *rk_bytes,
266 const unsigned char *key)
267{
268 __m128i *rk = (__m128i *) rk_bytes;
269
270 memcpy(&rk[0], key, 16);
271 rk[1] = aesni_set_rk_128(rk[0], _mm_aeskeygenassist_si128(rk[0], 0x01));
272 rk[2] = aesni_set_rk_128(rk[1], _mm_aeskeygenassist_si128(rk[1], 0x02));
273 rk[3] = aesni_set_rk_128(rk[2], _mm_aeskeygenassist_si128(rk[2], 0x04));
274 rk[4] = aesni_set_rk_128(rk[3], _mm_aeskeygenassist_si128(rk[3], 0x08));
275 rk[5] = aesni_set_rk_128(rk[4], _mm_aeskeygenassist_si128(rk[4], 0x10));
276 rk[6] = aesni_set_rk_128(rk[5], _mm_aeskeygenassist_si128(rk[5], 0x20));
277 rk[7] = aesni_set_rk_128(rk[6], _mm_aeskeygenassist_si128(rk[6], 0x40));
278 rk[8] = aesni_set_rk_128(rk[7], _mm_aeskeygenassist_si128(rk[7], 0x80));
279 rk[9] = aesni_set_rk_128(rk[8], _mm_aeskeygenassist_si128(rk[8], 0x1B));
280 rk[10] = aesni_set_rk_128(rk[9], _mm_aeskeygenassist_si128(rk[9], 0x36));
281}
282
283/*
284 * Key expansion, 192-bit case
285 */
Gilles Peskined4a23932023-03-15 20:37:57 +0100286static void aesni_set_rk_192(__m128i *state0, __m128i *state1, __m128i xword,
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100287 unsigned char *rk)
288{
289 /*
290 * Finish generating the next 6 quarter-keys.
291 *
Gilles Peskined4a23932023-03-15 20:37:57 +0100292 * On entry state0 is r3:r2:r1:r0, state1 is stuff:stuff:r5:r4
293 * and xword is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON
294 * (obtained with AESKEYGENASSIST).
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100295 *
Gilles Peskined4a23932023-03-15 20:37:57 +0100296 * On exit, state0 is r9:r8:r7:r6 and state1 is stuff:stuff:r11:r10
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100297 * and those are written to the round key buffer.
298 */
Gilles Peskined4a23932023-03-15 20:37:57 +0100299 xword = _mm_shuffle_epi32(xword, 0x55); // X:X:X:X
300 xword = _mm_xor_si128(xword, *state0); // X+r3:X+r2:X+r1:X+r0
301 *state0 = _mm_slli_si128(*state0, 4); // r2:r1:r0:0
302 xword = _mm_xor_si128(xword, *state0); // X+r3+r2:X+r2+r1:X+r1+r0:X+r0
303 *state0 = _mm_slli_si128(*state0, 4); // r1:r0:0:0
304 xword = _mm_xor_si128(xword, *state0); // X+r3+r2+r1:X+r2+r1+r0:X+r1+r0:X+r0
305 *state0 = _mm_slli_si128(*state0, 4); // r0:0:0:0
306 xword = _mm_xor_si128(xword, *state0); // X+r3+r2+r1+r0:X+r2+r1+r0:X+r1+r0:X+r0
307 *state0 = xword; // = r9:r8:r7:r6
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100308
Gilles Peskined4a23932023-03-15 20:37:57 +0100309 xword = _mm_shuffle_epi32(xword, 0xff); // r9:r9:r9:r9
310 xword = _mm_xor_si128(xword, *state1); // stuff:stuff:r9+r5:r9+r4
311 *state1 = _mm_slli_si128(*state1, 4); // stuff:stuff:r4:0
312 xword = _mm_xor_si128(xword, *state1); // stuff:stuff:r9+r5+r4:r9+r4
313 *state1 = xword; // = stuff:stuff:r11:r10
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100314
Gilles Peskined4a23932023-03-15 20:37:57 +0100315 /* Store state0 and the low half of state1 into rk, which is conceptually
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100316 * an array of 24-byte elements. Since 24 is not a multiple of 16,
Gilles Peskined4a23932023-03-15 20:37:57 +0100317 * rk is not necessarily aligned so just `*rk = *state0` doesn't work. */
318 memcpy(rk, state0, 16);
Gilles Peskine2e8d8d12023-03-15 23:16:27 +0100319 memcpy(rk + 16, state1, 8);
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100320}
321
322static void aesni_setkey_enc_192(unsigned char *rk,
323 const unsigned char *key)
324{
325 /* First round: use original key */
326 memcpy(rk, key, 24);
327 /* aes.c guarantees that rk is aligned on a 16-byte boundary. */
Gilles Peskined4a23932023-03-15 20:37:57 +0100328 __m128i state0 = ((__m128i *) rk)[0];
329 __m128i state1 = _mm_loadl_epi64(((__m128i *) rk) + 1);
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100330
Gilles Peskined4a23932023-03-15 20:37:57 +0100331 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x01), rk + 24 * 1);
332 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x02), rk + 24 * 2);
333 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x04), rk + 24 * 3);
334 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x08), rk + 24 * 4);
335 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x10), rk + 24 * 5);
336 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x20), rk + 24 * 6);
337 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x40), rk + 24 * 7);
338 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x80), rk + 24 * 8);
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100339}
340
341/*
342 * Key expansion, 256-bit case
343 */
Gilles Peskined4a23932023-03-15 20:37:57 +0100344static void aesni_set_rk_256(__m128i state0, __m128i state1, __m128i xword,
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100345 __m128i *rk0, __m128i *rk1)
346{
347 /*
348 * Finish generating the next two round keys.
349 *
Gilles Peskined4a23932023-03-15 20:37:57 +0100350 * On entry state0 is r3:r2:r1:r0, state1 is r7:r6:r5:r4 and
351 * xword is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
352 * (obtained with AESKEYGENASSIST).
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100353 *
354 * On exit, *rk0 is r11:r10:r9:r8 and *rk1 is r15:r14:r13:r12
355 */
Gilles Peskined4a23932023-03-15 20:37:57 +0100356 xword = _mm_shuffle_epi32(xword, 0xff);
357 xword = _mm_xor_si128(xword, state0);
358 state0 = _mm_slli_si128(state0, 4);
359 xword = _mm_xor_si128(xword, state0);
360 state0 = _mm_slli_si128(state0, 4);
361 xword = _mm_xor_si128(xword, state0);
362 state0 = _mm_slli_si128(state0, 4);
363 state0 = _mm_xor_si128(state0, xword);
364 *rk0 = state0;
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100365
Gilles Peskined4a23932023-03-15 20:37:57 +0100366 /* Set xword to stuff:Y:stuff:stuff with Y = subword( r11 )
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100367 * and proceed to generate next round key from there */
Gilles Peskined4a23932023-03-15 20:37:57 +0100368 xword = _mm_aeskeygenassist_si128(state0, 0x00);
369 xword = _mm_shuffle_epi32(xword, 0xaa);
370 xword = _mm_xor_si128(xword, state1);
371 state1 = _mm_slli_si128(state1, 4);
372 xword = _mm_xor_si128(xword, state1);
373 state1 = _mm_slli_si128(state1, 4);
374 xword = _mm_xor_si128(xword, state1);
375 state1 = _mm_slli_si128(state1, 4);
376 state1 = _mm_xor_si128(state1, xword);
377 *rk1 = state1;
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100378}
379
380static void aesni_setkey_enc_256(unsigned char *rk_bytes,
381 const unsigned char *key)
382{
383 __m128i *rk = (__m128i *) rk_bytes;
384
385 memcpy(&rk[0], key, 16);
386 memcpy(&rk[1], key + 16, 16);
387
388 /*
389 * Main "loop" - Generating one more key than necessary,
390 * see definition of mbedtls_aes_context.buf
391 */
392 aesni_set_rk_256(rk[0], rk[1], _mm_aeskeygenassist_si128(rk[1], 0x01), &rk[2], &rk[3]);
393 aesni_set_rk_256(rk[2], rk[3], _mm_aeskeygenassist_si128(rk[3], 0x02), &rk[4], &rk[5]);
394 aesni_set_rk_256(rk[4], rk[5], _mm_aeskeygenassist_si128(rk[5], 0x04), &rk[6], &rk[7]);
395 aesni_set_rk_256(rk[6], rk[7], _mm_aeskeygenassist_si128(rk[7], 0x08), &rk[8], &rk[9]);
396 aesni_set_rk_256(rk[8], rk[9], _mm_aeskeygenassist_si128(rk[9], 0x10), &rk[10], &rk[11]);
397 aesni_set_rk_256(rk[10], rk[11], _mm_aeskeygenassist_si128(rk[11], 0x20), &rk[12], &rk[13]);
398 aesni_set_rk_256(rk[12], rk[13], _mm_aeskeygenassist_si128(rk[13], 0x40), &rk[14], &rk[15]);
399}
400
Gilles Peskine6dec5412023-03-16 17:21:33 +0100401#else /* MBEDTLS_AESNI_HAVE_CODE == 1 */
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100402
Gilles Peskine18d521a2023-03-10 22:25:13 +0100403#if defined(__has_feature)
404#if __has_feature(memory_sanitizer)
405#warning \
406 "MBEDTLS_AESNI_C is known to cause spurious error reports with some memory sanitizers as they do not understand the assembly code."
407#endif
408#endif
409
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100410/*
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +0200411 * Binutils needs to be at least 2.19 to support AES-NI instructions.
412 * Unfortunately, a lot of users have a lower version now (2014-04).
413 * Emit bytecode directly in order to support "old" version of gas.
414 *
415 * Opcodes from the Intel architecture reference manual, vol. 3.
416 * We always use registers, so we don't need prefixes for memory operands.
417 * Operand macros are in gas order (src, dst) as opposed to Intel order
418 * (dst, src) in order to blend better into the surrounding assembly code.
419 */
Gilles Peskine2808a602023-03-15 19:36:03 +0100420#define AESDEC(regs) ".byte 0x66,0x0F,0x38,0xDE," regs "\n\t"
421#define AESDECLAST(regs) ".byte 0x66,0x0F,0x38,0xDF," regs "\n\t"
422#define AESENC(regs) ".byte 0x66,0x0F,0x38,0xDC," regs "\n\t"
423#define AESENCLAST(regs) ".byte 0x66,0x0F,0x38,0xDD," regs "\n\t"
424#define AESIMC(regs) ".byte 0x66,0x0F,0x38,0xDB," regs "\n\t"
425#define AESKEYGENA(regs, imm) ".byte 0x66,0x0F,0x3A,0xDF," regs "," imm "\n\t"
426#define PCLMULQDQ(regs, imm) ".byte 0x66,0x0F,0x3A,0x44," regs "," imm "\n\t"
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +0200427
428#define xmm0_xmm0 "0xC0"
429#define xmm0_xmm1 "0xC8"
430#define xmm0_xmm2 "0xD0"
431#define xmm0_xmm3 "0xD8"
432#define xmm0_xmm4 "0xE0"
433#define xmm1_xmm0 "0xC1"
434#define xmm1_xmm2 "0xD1"
435
436/*
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100437 * AES-NI AES-ECB block en(de)cryption
438 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100439int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
440 int mode,
441 const unsigned char input[16],
442 unsigned char output[16])
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100443{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100444 asm ("movdqu (%3), %%xmm0 \n\t" // load input
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200445 "movdqu (%1), %%xmm1 \n\t" // load round key 0
446 "pxor %%xmm1, %%xmm0 \n\t" // round 0
James Cowgill6c8edca2015-12-17 01:40:26 +0000447 "add $16, %1 \n\t" // point to next round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200448 "subl $1, %0 \n\t" // normal rounds = nr - 1
449 "test %2, %2 \n\t" // mode?
450 "jz 2f \n\t" // 0 = decrypt
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100451
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200452 "1: \n\t" // encryption loop
453 "movdqu (%1), %%xmm1 \n\t" // load round key
Gilles Peskine2808a602023-03-15 19:36:03 +0100454 AESENC(xmm1_xmm0) // do round
455 "add $16, %1 \n\t" // point to next round key
456 "subl $1, %0 \n\t" // loop
457 "jnz 1b \n\t"
458 "movdqu (%1), %%xmm1 \n\t" // load round key
459 AESENCLAST(xmm1_xmm0) // last round
460 "jmp 3f \n\t"
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100461
Gilles Peskine2808a602023-03-15 19:36:03 +0100462 "2: \n\t" // decryption loop
463 "movdqu (%1), %%xmm1 \n\t"
464 AESDEC(xmm1_xmm0) // do round
465 "add $16, %1 \n\t"
466 "subl $1, %0 \n\t"
467 "jnz 2b \n\t"
468 "movdqu (%1), %%xmm1 \n\t" // load round key
469 AESDECLAST(xmm1_xmm0) // last round
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100470
Gilles Peskine2808a602023-03-15 19:36:03 +0100471 "3: \n\t"
472 "movdqu %%xmm0, (%4) \n\t" // export output
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100473 :
474 : "r" (ctx->nr), "r" (ctx->rk), "r" (mode), "r" (input), "r" (output)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100475 : "memory", "cc", "xmm0", "xmm1");
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100476
477
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100478 return 0;
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100479}
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100480
481/*
482 * GCM multiplication: c = a times b in GF(2^128)
483 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
484 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100485void mbedtls_aesni_gcm_mult(unsigned char c[16],
486 const unsigned char a[16],
487 const unsigned char b[16])
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100488{
489 unsigned char aa[16], bb[16], cc[16];
490 size_t i;
491
492 /* The inputs are in big-endian order, so byte-reverse them */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100493 for (i = 0; i < 16; i++) {
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100494 aa[i] = a[15 - i];
495 bb[i] = b[15 - i];
496 }
497
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100498 asm ("movdqu (%0), %%xmm0 \n\t" // a1:a0
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200499 "movdqu (%1), %%xmm1 \n\t" // b1:b0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100500
501 /*
502 * Caryless multiplication xmm2:xmm1 = xmm0 * xmm1
Gilles Peskine6055b782023-03-10 22:21:47 +0100503 * using [CLMUL-WP] algorithm 1 (p. 12).
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100504 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200505 "movdqa %%xmm1, %%xmm2 \n\t" // copy of b1:b0
506 "movdqa %%xmm1, %%xmm3 \n\t" // same
507 "movdqa %%xmm1, %%xmm4 \n\t" // same
Gilles Peskine2808a602023-03-15 19:36:03 +0100508 PCLMULQDQ(xmm0_xmm1, "0x00") // a0*b0 = c1:c0
509 PCLMULQDQ(xmm0_xmm2, "0x11") // a1*b1 = d1:d0
510 PCLMULQDQ(xmm0_xmm3, "0x10") // a0*b1 = e1:e0
511 PCLMULQDQ(xmm0_xmm4, "0x01") // a1*b0 = f1:f0
512 "pxor %%xmm3, %%xmm4 \n\t" // e1+f1:e0+f0
513 "movdqa %%xmm4, %%xmm3 \n\t" // same
514 "psrldq $8, %%xmm4 \n\t" // 0:e1+f1
515 "pslldq $8, %%xmm3 \n\t" // e0+f0:0
516 "pxor %%xmm4, %%xmm2 \n\t" // d1:d0+e1+f1
517 "pxor %%xmm3, %%xmm1 \n\t" // c1+e0+f1:c0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100518
519 /*
520 * Now shift the result one bit to the left,
Gilles Peskine6055b782023-03-10 22:21:47 +0100521 * taking advantage of [CLMUL-WP] eq 27 (p. 18)
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100522 */
Gilles Peskine2808a602023-03-15 19:36:03 +0100523 "movdqa %%xmm1, %%xmm3 \n\t" // r1:r0
524 "movdqa %%xmm2, %%xmm4 \n\t" // r3:r2
525 "psllq $1, %%xmm1 \n\t" // r1<<1:r0<<1
526 "psllq $1, %%xmm2 \n\t" // r3<<1:r2<<1
527 "psrlq $63, %%xmm3 \n\t" // r1>>63:r0>>63
528 "psrlq $63, %%xmm4 \n\t" // r3>>63:r2>>63
529 "movdqa %%xmm3, %%xmm5 \n\t" // r1>>63:r0>>63
530 "pslldq $8, %%xmm3 \n\t" // r0>>63:0
531 "pslldq $8, %%xmm4 \n\t" // r2>>63:0
532 "psrldq $8, %%xmm5 \n\t" // 0:r1>>63
533 "por %%xmm3, %%xmm1 \n\t" // r1<<1|r0>>63:r0<<1
534 "por %%xmm4, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1
535 "por %%xmm5, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1|r1>>63
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100536
537 /*
538 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
Gilles Peskine6055b782023-03-10 22:21:47 +0100539 * using [CLMUL-WP] algorithm 5 (p. 18).
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100540 * Currently xmm2:xmm1 holds x3:x2:x1:x0 (already shifted).
541 */
542 /* Step 2 (1) */
Gilles Peskine2808a602023-03-15 19:36:03 +0100543 "movdqa %%xmm1, %%xmm3 \n\t" // x1:x0
544 "movdqa %%xmm1, %%xmm4 \n\t" // same
545 "movdqa %%xmm1, %%xmm5 \n\t" // same
546 "psllq $63, %%xmm3 \n\t" // x1<<63:x0<<63 = stuff:a
547 "psllq $62, %%xmm4 \n\t" // x1<<62:x0<<62 = stuff:b
548 "psllq $57, %%xmm5 \n\t" // x1<<57:x0<<57 = stuff:c
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100549
550 /* Step 2 (2) */
Gilles Peskine2808a602023-03-15 19:36:03 +0100551 "pxor %%xmm4, %%xmm3 \n\t" // stuff:a+b
552 "pxor %%xmm5, %%xmm3 \n\t" // stuff:a+b+c
553 "pslldq $8, %%xmm3 \n\t" // a+b+c:0
554 "pxor %%xmm3, %%xmm1 \n\t" // x1+a+b+c:x0 = d:x0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100555
556 /* Steps 3 and 4 */
Gilles Peskine2808a602023-03-15 19:36:03 +0100557 "movdqa %%xmm1,%%xmm0 \n\t" // d:x0
558 "movdqa %%xmm1,%%xmm4 \n\t" // same
559 "movdqa %%xmm1,%%xmm5 \n\t" // same
560 "psrlq $1, %%xmm0 \n\t" // e1:x0>>1 = e1:e0'
561 "psrlq $2, %%xmm4 \n\t" // f1:x0>>2 = f1:f0'
562 "psrlq $7, %%xmm5 \n\t" // g1:x0>>7 = g1:g0'
563 "pxor %%xmm4, %%xmm0 \n\t" // e1+f1:e0'+f0'
564 "pxor %%xmm5, %%xmm0 \n\t" // e1+f1+g1:e0'+f0'+g0'
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200565 // e0'+f0'+g0' is almost e0+f0+g0, ex\tcept for some missing
566 // bits carried from d. Now get those\t bits back in.
Gilles Peskine2808a602023-03-15 19:36:03 +0100567 "movdqa %%xmm1,%%xmm3 \n\t" // d:x0
568 "movdqa %%xmm1,%%xmm4 \n\t" // same
569 "movdqa %%xmm1,%%xmm5 \n\t" // same
570 "psllq $63, %%xmm3 \n\t" // d<<63:stuff
571 "psllq $62, %%xmm4 \n\t" // d<<62:stuff
572 "psllq $57, %%xmm5 \n\t" // d<<57:stuff
573 "pxor %%xmm4, %%xmm3 \n\t" // d<<63+d<<62:stuff
574 "pxor %%xmm5, %%xmm3 \n\t" // missing bits of d:stuff
575 "psrldq $8, %%xmm3 \n\t" // 0:missing bits of d
576 "pxor %%xmm3, %%xmm0 \n\t" // e1+f1+g1:e0+f0+g0
577 "pxor %%xmm1, %%xmm0 \n\t" // h1:h0
578 "pxor %%xmm2, %%xmm0 \n\t" // x3+h1:x2+h0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100579
Gilles Peskine2808a602023-03-15 19:36:03 +0100580 "movdqu %%xmm0, (%2) \n\t" // done
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100581 :
582 : "r" (aa), "r" (bb), "r" (cc)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100583 : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100584
585 /* Now byte-reverse the outputs */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100586 for (i = 0; i < 16; i++) {
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100587 c[i] = cc[15 - i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100588 }
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100589
Paul Bakkere7f51332013-12-30 15:32:02 +0100590 return;
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100591}
592
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100593/*
594 * Compute decryption round keys from encryption round keys
595 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100596void mbedtls_aesni_inverse_key(unsigned char *invkey,
597 const unsigned char *fwdkey, int nr)
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100598{
599 unsigned char *ik = invkey;
600 const unsigned char *fk = fwdkey + 16 * nr;
601
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100602 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100603
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100604 for (fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16) {
605 asm ("movdqu (%0), %%xmm0 \n\t"
Gilles Peskine2808a602023-03-15 19:36:03 +0100606 AESIMC(xmm0_xmm0)
607 "movdqu %%xmm0, (%1) \n\t"
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100608 :
609 : "r" (fk), "r" (ik)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100610 : "memory", "xmm0");
611 }
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100612
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100613 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100614}
615
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100616/*
617 * Key expansion, 128-bit case
618 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100619static void aesni_setkey_enc_128(unsigned char *rk,
620 const unsigned char *key)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100621{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100622 asm ("movdqu (%1), %%xmm0 \n\t" // copy the original key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200623 "movdqu %%xmm0, (%0) \n\t" // as round key 0
624 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100625
626 /*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100627 * Finish generating the next round key.
628 *
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100629 * On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff
630 * with X = rot( sub( r3 ) ) ^ RCON.
631 *
632 * On exit, xmm0 is r7:r6:r5:r4
633 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
634 * and those are written to the round key buffer.
635 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200636 "1: \n\t"
637 "pshufd $0xff, %%xmm1, %%xmm1 \n\t" // X:X:X:X
638 "pxor %%xmm0, %%xmm1 \n\t" // X+r3:X+r2:X+r1:r4
639 "pslldq $4, %%xmm0 \n\t" // r2:r1:r0:0
640 "pxor %%xmm0, %%xmm1 \n\t" // X+r3+r2:X+r2+r1:r5:r4
641 "pslldq $4, %%xmm0 \n\t" // etc
642 "pxor %%xmm0, %%xmm1 \n\t"
643 "pslldq $4, %%xmm0 \n\t"
644 "pxor %%xmm1, %%xmm0 \n\t" // update xmm0 for next time!
645 "add $16, %0 \n\t" // point to next round key
646 "movdqu %%xmm0, (%0) \n\t" // write it
647 "ret \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100648
649 /* Main "loop" */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200650 "2: \n\t"
Gilles Peskine2808a602023-03-15 19:36:03 +0100651 AESKEYGENA(xmm0_xmm1, "0x01") "call 1b \n\t"
652 AESKEYGENA(xmm0_xmm1, "0x02") "call 1b \n\t"
653 AESKEYGENA(xmm0_xmm1, "0x04") "call 1b \n\t"
654 AESKEYGENA(xmm0_xmm1, "0x08") "call 1b \n\t"
655 AESKEYGENA(xmm0_xmm1, "0x10") "call 1b \n\t"
656 AESKEYGENA(xmm0_xmm1, "0x20") "call 1b \n\t"
657 AESKEYGENA(xmm0_xmm1, "0x40") "call 1b \n\t"
658 AESKEYGENA(xmm0_xmm1, "0x80") "call 1b \n\t"
659 AESKEYGENA(xmm0_xmm1, "0x1B") "call 1b \n\t"
660 AESKEYGENA(xmm0_xmm1, "0x36") "call 1b \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100661 :
662 : "r" (rk), "r" (key)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100663 : "memory", "cc", "0");
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100664}
665
666/*
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100667 * Key expansion, 192-bit case
668 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100669static void aesni_setkey_enc_192(unsigned char *rk,
670 const unsigned char *key)
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100671{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100672 asm ("movdqu (%1), %%xmm0 \n\t" // copy original round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200673 "movdqu %%xmm0, (%0) \n\t"
674 "add $16, %0 \n\t"
675 "movq 16(%1), %%xmm1 \n\t"
676 "movq %%xmm1, (%0) \n\t"
677 "add $8, %0 \n\t"
678 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100679
680 /*
681 * Finish generating the next 6 quarter-keys.
682 *
683 * On entry xmm0 is r3:r2:r1:r0, xmm1 is stuff:stuff:r5:r4
684 * and xmm2 is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON.
685 *
686 * On exit, xmm0 is r9:r8:r7:r6 and xmm1 is stuff:stuff:r11:r10
687 * and those are written to the round key buffer.
688 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200689 "1: \n\t"
690 "pshufd $0x55, %%xmm2, %%xmm2 \n\t" // X:X:X:X
691 "pxor %%xmm0, %%xmm2 \n\t" // X+r3:X+r2:X+r1:r4
692 "pslldq $4, %%xmm0 \n\t" // etc
693 "pxor %%xmm0, %%xmm2 \n\t"
694 "pslldq $4, %%xmm0 \n\t"
695 "pxor %%xmm0, %%xmm2 \n\t"
696 "pslldq $4, %%xmm0 \n\t"
697 "pxor %%xmm2, %%xmm0 \n\t" // update xmm0 = r9:r8:r7:r6
698 "movdqu %%xmm0, (%0) \n\t"
699 "add $16, %0 \n\t"
700 "pshufd $0xff, %%xmm0, %%xmm2 \n\t" // r9:r9:r9:r9
701 "pxor %%xmm1, %%xmm2 \n\t" // stuff:stuff:r9+r5:r10
702 "pslldq $4, %%xmm1 \n\t" // r2:r1:r0:0
703 "pxor %%xmm2, %%xmm1 \n\t" // xmm1 = stuff:stuff:r11:r10
704 "movq %%xmm1, (%0) \n\t"
705 "add $8, %0 \n\t"
706 "ret \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100707
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200708 "2: \n\t"
Gilles Peskine2808a602023-03-15 19:36:03 +0100709 AESKEYGENA(xmm1_xmm2, "0x01") "call 1b \n\t"
710 AESKEYGENA(xmm1_xmm2, "0x02") "call 1b \n\t"
711 AESKEYGENA(xmm1_xmm2, "0x04") "call 1b \n\t"
712 AESKEYGENA(xmm1_xmm2, "0x08") "call 1b \n\t"
713 AESKEYGENA(xmm1_xmm2, "0x10") "call 1b \n\t"
714 AESKEYGENA(xmm1_xmm2, "0x20") "call 1b \n\t"
715 AESKEYGENA(xmm1_xmm2, "0x40") "call 1b \n\t"
716 AESKEYGENA(xmm1_xmm2, "0x80") "call 1b \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100717
718 :
719 : "r" (rk), "r" (key)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100720 : "memory", "cc", "0");
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100721}
722
723/*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100724 * Key expansion, 256-bit case
725 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine2808a602023-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 Peskine2808a602023-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 Peskine1b6c09a2023-01-11 14:52:35 +0100786 : "memory", "cc", "0");
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100787}
788
Gilles Peskine6dec5412023-03-16 17:21:33 +0100789#endif /* MBEDTLS_AESNI_HAVE_CODE */
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100790
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100791/*
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100792 * Key expansion, wrapper
793 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100794int mbedtls_aesni_setkey_enc(unsigned char *rk,
795 const unsigned char *key,
796 size_t bits)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100797{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100798 switch (bits) {
799 case 128: aesni_setkey_enc_128(rk, key); break;
800 case 192: aesni_setkey_enc_192(rk, key); break;
801 case 256: aesni_setkey_enc_256(rk, key); break;
802 default: return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100803 }
804
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100805 return 0;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100806}
807
Gilles Peskine6dec5412023-03-16 17:21:33 +0100808#endif /* MBEDTLS_AESNI_HAVE_CODE */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810#endif /* MBEDTLS_AESNI_C */