blob: b92c73c29e3d4c1b272376099ba7737666740059 [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
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +01006 */
7
8/*
Gilles Peskined4f31c82023-03-10 22:21:47 +01009 * [AES-WP] https://www.intel.com/content/www/us/en/developer/articles/tool/intel-advanced-encryption-standard-aes-instructions-set.html
10 * [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 +010011 */
12
Gilles Peskinedb09ef62020-06-03 01:43:33 +020013#include "common.h"
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020015#if defined(MBEDTLS_AESNI_C)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010016
Chris Jones187782f2021-03-09 17:28:35 +000017#include "aesni.h"
Rich Evans00ab4702015-02-06 13:43:58 +000018
19#include <string.h>
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010020
Gilles Peskine9c682e72023-03-16 17:21:33 +010021#if defined(MBEDTLS_AESNI_HAVE_CODE)
Gilles Peskined6719172023-03-10 22:37:11 +010022
Gilles Peskine9c682e72023-03-16 17:21:33 +010023#if MBEDTLS_AESNI_HAVE_CODE == 2
Pengyu Lv0ecb6352023-10-11 10:36:55 +080024#if defined(__GNUC__)
Gilles Peskined6719172023-03-10 22:37:11 +010025#include <cpuid.h>
Pengyu Lv0ecb6352023-10-11 10:36:55 +080026#elif defined(_MSC_VER)
SlugFiller5ca3f0b2023-05-22 06:31:45 +030027#include <intrin.h>
Pengyu Lv0ecb6352023-10-11 10:36:55 +080028#else
29#error "`__cpuid` required by MBEDTLS_AESNI_C is not supported by the compiler"
Tom Cosgrove02edb752023-03-13 15:32:52 +000030#endif
Gilles Peskined6719172023-03-10 22:37:11 +010031#include <immintrin.h>
32#endif
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010033
Beniamin Sandu800f2b72023-10-27 16:58:00 +010034#if defined(MBEDTLS_ARCH_IS_X86)
35#if defined(MBEDTLS_COMPILER_IS_GCC)
36#pragma GCC push_options
37#pragma GCC target ("pclmul,sse2,aes")
38#define MBEDTLS_POP_TARGET_PRAGMA
39#elif defined(__clang__)
40#pragma clang attribute push (__attribute__((target("pclmul,sse2,aes"))), apply_to=function)
41#define MBEDTLS_POP_TARGET_PRAGMA
42#endif
43#endif
44
Jerry Yu0a6272d2023-08-18 17:35:59 +080045#if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010046/*
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010047 * AES-NI support detection routine
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010048 */
Gilles Peskine449bd832023-01-11 14:50:10 +010049int mbedtls_aesni_has_support(unsigned int what)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010050{
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010051 static int done = 0;
52 static unsigned int c = 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010053
Gilles Peskine449bd832023-01-11 14:50:10 +010054 if (!done) {
Gilles Peskine9c682e72023-03-16 17:21:33 +010055#if MBEDTLS_AESNI_HAVE_CODE == 2
Pengyu Lve8c4bf12023-10-10 18:12:43 +080056 static int info[4] = { 0, 0, 0, 0 };
Pengyu Lv0ecb6352023-10-11 10:36:55 +080057#if defined(_MSC_VER)
Gilles Peskined6719172023-03-10 22:37:11 +010058 __cpuid(info, 1);
59#else
60 __cpuid(1, info[0], info[1], info[2], info[3]);
61#endif
62 c = info[2];
Gilles Peskine9c682e72023-03-16 17:21:33 +010063#else /* AESNI using asm */
Gilles Peskine449bd832023-01-11 14:50:10 +010064 asm ("movl $1, %%eax \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +020065 "cpuid \n\t"
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010066 : "=c" (c)
67 :
Gilles Peskine449bd832023-01-11 14:50:10 +010068 : "eax", "ebx", "edx");
Gilles Peskine9c682e72023-03-16 17:21:33 +010069#endif /* MBEDTLS_AESNI_HAVE_CODE */
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010070 done = 1;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010071 }
72
Gilles Peskine449bd832023-01-11 14:50:10 +010073 return (c & what) != 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010074}
Jerry Yu36606232023-04-19 10:44:29 +080075#endif /* !MBEDTLS_AES_USE_HARDWARE_ONLY */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010076
Gilles Peskine9c682e72023-03-16 17:21:33 +010077#if MBEDTLS_AESNI_HAVE_CODE == 2
Gilles Peskined6719172023-03-10 22:37:11 +010078
79/*
80 * AES-NI AES-ECB block en(de)cryption
81 */
82int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
83 int mode,
84 const unsigned char input[16],
85 unsigned char output[16])
86{
87 const __m128i *rk = (const __m128i *) (ctx->buf + ctx->rk_offset);
88 unsigned nr = ctx->nr; // Number of remaining rounds
Tom Cosgrove02edb752023-03-13 15:32:52 +000089
Gilles Peskined6719172023-03-10 22:37:11 +010090 // Load round key 0
Gilles Peskinedafeee42023-03-15 20:37:57 +010091 __m128i state;
92 memcpy(&state, input, 16);
93 state = _mm_xor_si128(state, rk[0]); // state ^= *rk;
Gilles Peskined6719172023-03-10 22:37:11 +010094 ++rk;
95 --nr;
96
97 if (mode == 0) {
98 while (nr != 0) {
Gilles Peskinedafeee42023-03-15 20:37:57 +010099 state = _mm_aesdec_si128(state, *rk);
Gilles Peskined6719172023-03-10 22:37:11 +0100100 ++rk;
101 --nr;
102 }
Gilles Peskinedafeee42023-03-15 20:37:57 +0100103 state = _mm_aesdeclast_si128(state, *rk);
Gilles Peskined6719172023-03-10 22:37:11 +0100104 } else {
105 while (nr != 0) {
Gilles Peskinedafeee42023-03-15 20:37:57 +0100106 state = _mm_aesenc_si128(state, *rk);
Gilles Peskined6719172023-03-10 22:37:11 +0100107 ++rk;
108 --nr;
109 }
Gilles Peskinedafeee42023-03-15 20:37:57 +0100110 state = _mm_aesenclast_si128(state, *rk);
Gilles Peskined6719172023-03-10 22:37:11 +0100111 }
112
Gilles Peskinedafeee42023-03-15 20:37:57 +0100113 memcpy(output, &state, 16);
Gilles Peskined6719172023-03-10 22:37:11 +0100114 return 0;
115}
116
117/*
118 * GCM multiplication: c = a times b in GF(2^128)
119 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
120 */
121
122static void gcm_clmul(const __m128i aa, const __m128i bb,
123 __m128i *cc, __m128i *dd)
124{
125 /*
126 * Caryless multiplication dd:cc = aa * bb
127 * using [CLMUL-WP] algorithm 1 (p. 12).
128 */
129 *cc = _mm_clmulepi64_si128(aa, bb, 0x00); // a0*b0 = c1:c0
130 *dd = _mm_clmulepi64_si128(aa, bb, 0x11); // a1*b1 = d1:d0
131 __m128i ee = _mm_clmulepi64_si128(aa, bb, 0x10); // a0*b1 = e1:e0
132 __m128i ff = _mm_clmulepi64_si128(aa, bb, 0x01); // a1*b0 = f1:f0
Tom Cosgrove02edb752023-03-13 15:32:52 +0000133 ff = _mm_xor_si128(ff, ee); // e1+f1:e0+f0
Gilles Peskined6719172023-03-10 22:37:11 +0100134 ee = ff; // e1+f1:e0+f0
135 ff = _mm_srli_si128(ff, 8); // 0:e1+f1
136 ee = _mm_slli_si128(ee, 8); // e0+f0:0
Tom Cosgrove02edb752023-03-13 15:32:52 +0000137 *dd = _mm_xor_si128(*dd, ff); // d1:d0+e1+f1
Gilles Peskined0185f72023-03-16 13:08:18 +0100138 *cc = _mm_xor_si128(*cc, ee); // c1+e0+f0:c0
Gilles Peskined6719172023-03-10 22:37:11 +0100139}
140
141static void gcm_shift(__m128i *cc, __m128i *dd)
142{
Gilles Peskinedafeee42023-03-15 20:37:57 +0100143 /* [CMUCL-WP] Algorithm 5 Step 1: shift cc:dd one bit to the left,
144 * taking advantage of [CLMUL-WP] eq 27 (p. 18). */
145 // // *cc = r1:r0
146 // // *dd = r3:r2
147 __m128i cc_lo = _mm_slli_epi64(*cc, 1); // r1<<1:r0<<1
148 __m128i dd_lo = _mm_slli_epi64(*dd, 1); // r3<<1:r2<<1
149 __m128i cc_hi = _mm_srli_epi64(*cc, 63); // r1>>63:r0>>63
150 __m128i dd_hi = _mm_srli_epi64(*dd, 63); // r3>>63:r2>>63
151 __m128i xmm5 = _mm_srli_si128(cc_hi, 8); // 0:r1>>63
152 cc_hi = _mm_slli_si128(cc_hi, 8); // r0>>63:0
153 dd_hi = _mm_slli_si128(dd_hi, 8); // 0:r1>>63
Gilles Peskined6719172023-03-10 22:37:11 +0100154
Gilles Peskinedafeee42023-03-15 20:37:57 +0100155 *cc = _mm_or_si128(cc_lo, cc_hi); // r1<<1|r0>>63:r0<<1
156 *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 +0100157}
158
Gilles Peskinedafeee42023-03-15 20:37:57 +0100159static __m128i gcm_reduce(__m128i xx)
Gilles Peskined6719172023-03-10 22:37:11 +0100160{
161 // // xx = x1:x0
162 /* [CLMUL-WP] Algorithm 5 Step 2 */
163 __m128i aa = _mm_slli_epi64(xx, 63); // x1<<63:x0<<63 = stuff:a
164 __m128i bb = _mm_slli_epi64(xx, 62); // x1<<62:x0<<62 = stuff:b
165 __m128i cc = _mm_slli_epi64(xx, 57); // x1<<57:x0<<57 = stuff:c
Tom Cosgrove02edb752023-03-13 15:32:52 +0000166 __m128i dd = _mm_slli_si128(_mm_xor_si128(_mm_xor_si128(aa, bb), cc), 8); // a+b+c:0
167 return _mm_xor_si128(dd, xx); // x1+a+b+c:x0 = d:x0
Gilles Peskined6719172023-03-10 22:37:11 +0100168}
169
Gilles Peskinedafeee42023-03-15 20:37:57 +0100170static __m128i gcm_mix(__m128i dx)
Gilles Peskined6719172023-03-10 22:37:11 +0100171{
172 /* [CLMUL-WP] Algorithm 5 Steps 3 and 4 */
173 __m128i ee = _mm_srli_epi64(dx, 1); // e1:x0>>1 = e1:e0'
174 __m128i ff = _mm_srli_epi64(dx, 2); // f1:x0>>2 = f1:f0'
175 __m128i gg = _mm_srli_epi64(dx, 7); // g1:x0>>7 = g1:g0'
176
177 // e0'+f0'+g0' is almost e0+f0+g0, except for some missing
178 // bits carried from d. Now get those bits back in.
179 __m128i eh = _mm_slli_epi64(dx, 63); // d<<63:stuff
180 __m128i fh = _mm_slli_epi64(dx, 62); // d<<62:stuff
181 __m128i gh = _mm_slli_epi64(dx, 57); // d<<57:stuff
Tom Cosgrove02edb752023-03-13 15:32:52 +0000182 __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 +0100183
Tom Cosgrove02edb752023-03-13 15:32:52 +0000184 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 +0100185}
186
187void mbedtls_aesni_gcm_mult(unsigned char c[16],
188 const unsigned char a[16],
189 const unsigned char b[16])
190{
Sergey Markelov3898f102023-10-16 12:54:48 -0700191 __m128i aa = { 0 }, bb = { 0 }, cc, dd;
Gilles Peskined6719172023-03-10 22:37:11 +0100192
193 /* The inputs are in big-endian order, so byte-reverse them */
194 for (size_t i = 0; i < 16; i++) {
195 ((uint8_t *) &aa)[i] = a[15 - i];
196 ((uint8_t *) &bb)[i] = b[15 - i];
197 }
198
199 gcm_clmul(aa, bb, &cc, &dd);
200 gcm_shift(&cc, &dd);
201 /*
202 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
203 * using [CLMUL-WP] algorithm 5 (p. 18).
204 * Currently dd:cc holds x3:x2:x1:x0 (already shifted).
205 */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100206 __m128i dx = gcm_reduce(cc);
207 __m128i xh = gcm_mix(dx);
Tom Cosgrove02edb752023-03-13 15:32:52 +0000208 cc = _mm_xor_si128(xh, dd); // x3+h1:x2+h0
Gilles Peskined6719172023-03-10 22:37:11 +0100209
210 /* Now byte-reverse the outputs */
211 for (size_t i = 0; i < 16; i++) {
212 c[i] = ((uint8_t *) &cc)[15 - i];
213 }
214
215 return;
216}
217
218/*
219 * Compute decryption round keys from encryption round keys
220 */
221void mbedtls_aesni_inverse_key(unsigned char *invkey,
222 const unsigned char *fwdkey, int nr)
223{
224 __m128i *ik = (__m128i *) invkey;
225 const __m128i *fk = (const __m128i *) fwdkey + nr;
226
227 *ik = *fk;
228 for (--fk, ++ik; fk > (const __m128i *) fwdkey; --fk, ++ik) {
229 *ik = _mm_aesimc_si128(*fk);
230 }
231 *ik = *fk;
232}
233
234/*
235 * Key expansion, 128-bit case
236 */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100237static __m128i aesni_set_rk_128(__m128i state, __m128i xword)
Gilles Peskined6719172023-03-10 22:37:11 +0100238{
239 /*
240 * Finish generating the next round key.
241 *
Gilles Peskinedafeee42023-03-15 20:37:57 +0100242 * On entry state is r3:r2:r1:r0 and xword is X:stuff:stuff:stuff
243 * with X = rot( sub( r3 ) ) ^ RCON (obtained with AESKEYGENASSIST).
Gilles Peskined6719172023-03-10 22:37:11 +0100244 *
Gilles Peskinedafeee42023-03-15 20:37:57 +0100245 * On exit, xword is r7:r6:r5:r4
Gilles Peskined6719172023-03-10 22:37:11 +0100246 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
247 * and this is returned, to be written to the round key buffer.
248 */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100249 xword = _mm_shuffle_epi32(xword, 0xff); // X:X:X:X
250 xword = _mm_xor_si128(xword, state); // X+r3:X+r2:X+r1:r4
251 state = _mm_slli_si128(state, 4); // r2:r1:r0:0
252 xword = _mm_xor_si128(xword, state); // X+r3+r2:X+r2+r1:r5:r4
253 state = _mm_slli_si128(state, 4); // r1:r0:0:0
254 xword = _mm_xor_si128(xword, state); // X+r3+r2+r1:r6:r5:r4
255 state = _mm_slli_si128(state, 4); // r0:0:0:0
256 state = _mm_xor_si128(xword, state); // r7:r6:r5:r4
257 return state;
Gilles Peskined6719172023-03-10 22:37:11 +0100258}
259
260static void aesni_setkey_enc_128(unsigned char *rk_bytes,
261 const unsigned char *key)
262{
263 __m128i *rk = (__m128i *) rk_bytes;
264
265 memcpy(&rk[0], key, 16);
266 rk[1] = aesni_set_rk_128(rk[0], _mm_aeskeygenassist_si128(rk[0], 0x01));
267 rk[2] = aesni_set_rk_128(rk[1], _mm_aeskeygenassist_si128(rk[1], 0x02));
268 rk[3] = aesni_set_rk_128(rk[2], _mm_aeskeygenassist_si128(rk[2], 0x04));
269 rk[4] = aesni_set_rk_128(rk[3], _mm_aeskeygenassist_si128(rk[3], 0x08));
270 rk[5] = aesni_set_rk_128(rk[4], _mm_aeskeygenassist_si128(rk[4], 0x10));
271 rk[6] = aesni_set_rk_128(rk[5], _mm_aeskeygenassist_si128(rk[5], 0x20));
272 rk[7] = aesni_set_rk_128(rk[6], _mm_aeskeygenassist_si128(rk[6], 0x40));
273 rk[8] = aesni_set_rk_128(rk[7], _mm_aeskeygenassist_si128(rk[7], 0x80));
274 rk[9] = aesni_set_rk_128(rk[8], _mm_aeskeygenassist_si128(rk[8], 0x1B));
275 rk[10] = aesni_set_rk_128(rk[9], _mm_aeskeygenassist_si128(rk[9], 0x36));
276}
277
278/*
279 * Key expansion, 192-bit case
280 */
Arto Kinnunen732ca322023-04-14 14:26:10 +0800281#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskinedafeee42023-03-15 20:37:57 +0100282static void aesni_set_rk_192(__m128i *state0, __m128i *state1, __m128i xword,
Gilles Peskined6719172023-03-10 22:37:11 +0100283 unsigned char *rk)
284{
285 /*
286 * Finish generating the next 6 quarter-keys.
287 *
Gilles Peskinedafeee42023-03-15 20:37:57 +0100288 * On entry state0 is r3:r2:r1:r0, state1 is stuff:stuff:r5:r4
289 * and xword is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON
290 * (obtained with AESKEYGENASSIST).
Gilles Peskined6719172023-03-10 22:37:11 +0100291 *
Gilles Peskinedafeee42023-03-15 20:37:57 +0100292 * On exit, state0 is r9:r8:r7:r6 and state1 is stuff:stuff:r11:r10
Gilles Peskined6719172023-03-10 22:37:11 +0100293 * and those are written to the round key buffer.
294 */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100295 xword = _mm_shuffle_epi32(xword, 0x55); // X:X:X:X
296 xword = _mm_xor_si128(xword, *state0); // X+r3:X+r2:X+r1:X+r0
297 *state0 = _mm_slli_si128(*state0, 4); // r2:r1:r0:0
298 xword = _mm_xor_si128(xword, *state0); // X+r3+r2:X+r2+r1:X+r1+r0:X+r0
299 *state0 = _mm_slli_si128(*state0, 4); // r1:r0:0:0
300 xword = _mm_xor_si128(xword, *state0); // X+r3+r2+r1:X+r2+r1+r0:X+r1+r0:X+r0
301 *state0 = _mm_slli_si128(*state0, 4); // r0:0:0:0
302 xword = _mm_xor_si128(xword, *state0); // X+r3+r2+r1+r0:X+r2+r1+r0:X+r1+r0:X+r0
303 *state0 = xword; // = r9:r8:r7:r6
Gilles Peskined6719172023-03-10 22:37:11 +0100304
Gilles Peskinedafeee42023-03-15 20:37:57 +0100305 xword = _mm_shuffle_epi32(xword, 0xff); // r9:r9:r9:r9
306 xword = _mm_xor_si128(xword, *state1); // stuff:stuff:r9+r5:r9+r4
307 *state1 = _mm_slli_si128(*state1, 4); // stuff:stuff:r4:0
308 xword = _mm_xor_si128(xword, *state1); // stuff:stuff:r9+r5+r4:r9+r4
309 *state1 = xword; // = stuff:stuff:r11:r10
Gilles Peskined6719172023-03-10 22:37:11 +0100310
Gilles Peskinedafeee42023-03-15 20:37:57 +0100311 /* Store state0 and the low half of state1 into rk, which is conceptually
Gilles Peskined6719172023-03-10 22:37:11 +0100312 * an array of 24-byte elements. Since 24 is not a multiple of 16,
Gilles Peskinedafeee42023-03-15 20:37:57 +0100313 * rk is not necessarily aligned so just `*rk = *state0` doesn't work. */
314 memcpy(rk, state0, 16);
Gilles Peskinedde3c652023-03-15 23:16:27 +0100315 memcpy(rk + 16, state1, 8);
Gilles Peskined6719172023-03-10 22:37:11 +0100316}
317
318static void aesni_setkey_enc_192(unsigned char *rk,
319 const unsigned char *key)
320{
321 /* First round: use original key */
322 memcpy(rk, key, 24);
323 /* aes.c guarantees that rk is aligned on a 16-byte boundary. */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100324 __m128i state0 = ((__m128i *) rk)[0];
325 __m128i state1 = _mm_loadl_epi64(((__m128i *) rk) + 1);
Gilles Peskined6719172023-03-10 22:37:11 +0100326
Gilles Peskinedafeee42023-03-15 20:37:57 +0100327 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x01), rk + 24 * 1);
328 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x02), rk + 24 * 2);
329 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x04), rk + 24 * 3);
330 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x08), rk + 24 * 4);
331 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x10), rk + 24 * 5);
332 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x20), rk + 24 * 6);
333 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x40), rk + 24 * 7);
334 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x80), rk + 24 * 8);
Gilles Peskined6719172023-03-10 22:37:11 +0100335}
Arto Kinnunen732ca322023-04-14 14:26:10 +0800336#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Gilles Peskined6719172023-03-10 22:37:11 +0100337
338/*
339 * Key expansion, 256-bit case
340 */
Arto Kinnunen732ca322023-04-14 14:26:10 +0800341#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskinedafeee42023-03-15 20:37:57 +0100342static void aesni_set_rk_256(__m128i state0, __m128i state1, __m128i xword,
Gilles Peskined6719172023-03-10 22:37:11 +0100343 __m128i *rk0, __m128i *rk1)
344{
345 /*
346 * Finish generating the next two round keys.
347 *
Gilles Peskinedafeee42023-03-15 20:37:57 +0100348 * On entry state0 is r3:r2:r1:r0, state1 is r7:r6:r5:r4 and
349 * xword is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
350 * (obtained with AESKEYGENASSIST).
Gilles Peskined6719172023-03-10 22:37:11 +0100351 *
352 * On exit, *rk0 is r11:r10:r9:r8 and *rk1 is r15:r14:r13:r12
353 */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100354 xword = _mm_shuffle_epi32(xword, 0xff);
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 xword = _mm_xor_si128(xword, state0);
360 state0 = _mm_slli_si128(state0, 4);
361 state0 = _mm_xor_si128(state0, xword);
362 *rk0 = state0;
Gilles Peskined6719172023-03-10 22:37:11 +0100363
Gilles Peskinedafeee42023-03-15 20:37:57 +0100364 /* Set xword to stuff:Y:stuff:stuff with Y = subword( r11 )
Gilles Peskined6719172023-03-10 22:37:11 +0100365 * and proceed to generate next round key from there */
Gilles Peskinedafeee42023-03-15 20:37:57 +0100366 xword = _mm_aeskeygenassist_si128(state0, 0x00);
367 xword = _mm_shuffle_epi32(xword, 0xaa);
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 xword = _mm_xor_si128(xword, state1);
373 state1 = _mm_slli_si128(state1, 4);
374 state1 = _mm_xor_si128(state1, xword);
375 *rk1 = state1;
Gilles Peskined6719172023-03-10 22:37:11 +0100376}
377
378static void aesni_setkey_enc_256(unsigned char *rk_bytes,
379 const unsigned char *key)
380{
381 __m128i *rk = (__m128i *) rk_bytes;
382
383 memcpy(&rk[0], key, 16);
384 memcpy(&rk[1], key + 16, 16);
385
386 /*
387 * Main "loop" - Generating one more key than necessary,
388 * see definition of mbedtls_aes_context.buf
389 */
390 aesni_set_rk_256(rk[0], rk[1], _mm_aeskeygenassist_si128(rk[1], 0x01), &rk[2], &rk[3]);
391 aesni_set_rk_256(rk[2], rk[3], _mm_aeskeygenassist_si128(rk[3], 0x02), &rk[4], &rk[5]);
392 aesni_set_rk_256(rk[4], rk[5], _mm_aeskeygenassist_si128(rk[5], 0x04), &rk[6], &rk[7]);
393 aesni_set_rk_256(rk[6], rk[7], _mm_aeskeygenassist_si128(rk[7], 0x08), &rk[8], &rk[9]);
394 aesni_set_rk_256(rk[8], rk[9], _mm_aeskeygenassist_si128(rk[9], 0x10), &rk[10], &rk[11]);
395 aesni_set_rk_256(rk[10], rk[11], _mm_aeskeygenassist_si128(rk[11], 0x20), &rk[12], &rk[13]);
396 aesni_set_rk_256(rk[12], rk[13], _mm_aeskeygenassist_si128(rk[13], 0x40), &rk[14], &rk[15]);
397}
Arto Kinnunen732ca322023-04-14 14:26:10 +0800398#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Gilles Peskined6719172023-03-10 22:37:11 +0100399
Beniamin Sandu800f2b72023-10-27 16:58:00 +0100400#if defined(MBEDTLS_POP_TARGET_PRAGMA)
401#if defined(__clang__)
402#pragma clang attribute pop
403#elif defined(__GNUC__)
404#pragma GCC pop_options
405#endif
406#undef MBEDTLS_POP_TARGET_PRAGMA
407#endif
408
Gilles Peskine9c682e72023-03-16 17:21:33 +0100409#else /* MBEDTLS_AESNI_HAVE_CODE == 1 */
Gilles Peskined6719172023-03-10 22:37:11 +0100410
Gilles Peskined0f9b0b2023-03-10 22:25:13 +0100411#if defined(__has_feature)
412#if __has_feature(memory_sanitizer)
413#warning \
414 "MBEDTLS_AESNI_C is known to cause spurious error reports with some memory sanitizers as they do not understand the assembly code."
415#endif
416#endif
417
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100418/*
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +0200419 * Binutils needs to be at least 2.19 to support AES-NI instructions.
420 * Unfortunately, a lot of users have a lower version now (2014-04).
421 * Emit bytecode directly in order to support "old" version of gas.
422 *
423 * Opcodes from the Intel architecture reference manual, vol. 3.
424 * We always use registers, so we don't need prefixes for memory operands.
425 * Operand macros are in gas order (src, dst) as opposed to Intel order
426 * (dst, src) in order to blend better into the surrounding assembly code.
427 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100428#define AESDEC(regs) ".byte 0x66,0x0F,0x38,0xDE," regs "\n\t"
429#define AESDECLAST(regs) ".byte 0x66,0x0F,0x38,0xDF," regs "\n\t"
430#define AESENC(regs) ".byte 0x66,0x0F,0x38,0xDC," regs "\n\t"
431#define AESENCLAST(regs) ".byte 0x66,0x0F,0x38,0xDD," regs "\n\t"
432#define AESIMC(regs) ".byte 0x66,0x0F,0x38,0xDB," regs "\n\t"
433#define AESKEYGENA(regs, imm) ".byte 0x66,0x0F,0x3A,0xDF," regs "," imm "\n\t"
434#define PCLMULQDQ(regs, imm) ".byte 0x66,0x0F,0x3A,0x44," regs "," imm "\n\t"
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +0200435
436#define xmm0_xmm0 "0xC0"
437#define xmm0_xmm1 "0xC8"
438#define xmm0_xmm2 "0xD0"
439#define xmm0_xmm3 "0xD8"
440#define xmm0_xmm4 "0xE0"
441#define xmm1_xmm0 "0xC1"
442#define xmm1_xmm2 "0xD1"
443
444/*
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100445 * AES-NI AES-ECB block en(de)cryption
446 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100447int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
448 int mode,
449 const unsigned char input[16],
450 unsigned char output[16])
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100451{
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 asm ("movdqu (%3), %%xmm0 \n\t" // load input
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200453 "movdqu (%1), %%xmm1 \n\t" // load round key 0
454 "pxor %%xmm1, %%xmm0 \n\t" // round 0
James Cowgill6c8edca2015-12-17 01:40:26 +0000455 "add $16, %1 \n\t" // point to next round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200456 "subl $1, %0 \n\t" // normal rounds = nr - 1
457 "test %2, %2 \n\t" // mode?
458 "jz 2f \n\t" // 0 = decrypt
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100459
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200460 "1: \n\t" // encryption loop
461 "movdqu (%1), %%xmm1 \n\t" // load round key
Gilles Peskine4e201442023-03-15 19:36:03 +0100462 AESENC(xmm1_xmm0) // do round
463 "add $16, %1 \n\t" // point to next round key
464 "subl $1, %0 \n\t" // loop
465 "jnz 1b \n\t"
466 "movdqu (%1), %%xmm1 \n\t" // load round key
467 AESENCLAST(xmm1_xmm0) // last round
468 "jmp 3f \n\t"
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100469
Gilles Peskine4e201442023-03-15 19:36:03 +0100470 "2: \n\t" // decryption loop
471 "movdqu (%1), %%xmm1 \n\t"
472 AESDEC(xmm1_xmm0) // do round
473 "add $16, %1 \n\t"
474 "subl $1, %0 \n\t"
475 "jnz 2b \n\t"
476 "movdqu (%1), %%xmm1 \n\t" // load round key
477 AESDECLAST(xmm1_xmm0) // last round
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100478
Gilles Peskine4e201442023-03-15 19:36:03 +0100479 "3: \n\t"
480 "movdqu %%xmm0, (%4) \n\t" // export output
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100481 :
Werner Lewisdd76ef32022-05-30 12:00:21 +0100482 : "r" (ctx->nr), "r" (ctx->buf + ctx->rk_offset), "r" (mode), "r" (input), "r" (output)
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 : "memory", "cc", "xmm0", "xmm1");
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100484
485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 return 0;
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100487}
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100488
489/*
490 * GCM multiplication: c = a times b in GF(2^128)
491 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
492 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100493void mbedtls_aesni_gcm_mult(unsigned char c[16],
494 const unsigned char a[16],
495 const unsigned char b[16])
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100496{
497 unsigned char aa[16], bb[16], cc[16];
498 size_t i;
499
500 /* The inputs are in big-endian order, so byte-reverse them */
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 for (i = 0; i < 16; i++) {
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100502 aa[i] = a[15 - i];
503 bb[i] = b[15 - i];
504 }
505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 asm ("movdqu (%0), %%xmm0 \n\t" // a1:a0
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200507 "movdqu (%1), %%xmm1 \n\t" // b1:b0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100508
509 /*
510 * Caryless multiplication xmm2:xmm1 = xmm0 * xmm1
Gilles Peskined4f31c82023-03-10 22:21:47 +0100511 * using [CLMUL-WP] algorithm 1 (p. 12).
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100512 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200513 "movdqa %%xmm1, %%xmm2 \n\t" // copy of b1:b0
514 "movdqa %%xmm1, %%xmm3 \n\t" // same
515 "movdqa %%xmm1, %%xmm4 \n\t" // same
Gilles Peskine4e201442023-03-15 19:36:03 +0100516 PCLMULQDQ(xmm0_xmm1, "0x00") // a0*b0 = c1:c0
517 PCLMULQDQ(xmm0_xmm2, "0x11") // a1*b1 = d1:d0
518 PCLMULQDQ(xmm0_xmm3, "0x10") // a0*b1 = e1:e0
519 PCLMULQDQ(xmm0_xmm4, "0x01") // a1*b0 = f1:f0
520 "pxor %%xmm3, %%xmm4 \n\t" // e1+f1:e0+f0
521 "movdqa %%xmm4, %%xmm3 \n\t" // same
522 "psrldq $8, %%xmm4 \n\t" // 0:e1+f1
523 "pslldq $8, %%xmm3 \n\t" // e0+f0:0
524 "pxor %%xmm4, %%xmm2 \n\t" // d1:d0+e1+f1
525 "pxor %%xmm3, %%xmm1 \n\t" // c1+e0+f1:c0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100526
527 /*
528 * Now shift the result one bit to the left,
Gilles Peskined4f31c82023-03-10 22:21:47 +0100529 * taking advantage of [CLMUL-WP] eq 27 (p. 18)
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100530 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100531 "movdqa %%xmm1, %%xmm3 \n\t" // r1:r0
532 "movdqa %%xmm2, %%xmm4 \n\t" // r3:r2
533 "psllq $1, %%xmm1 \n\t" // r1<<1:r0<<1
534 "psllq $1, %%xmm2 \n\t" // r3<<1:r2<<1
535 "psrlq $63, %%xmm3 \n\t" // r1>>63:r0>>63
536 "psrlq $63, %%xmm4 \n\t" // r3>>63:r2>>63
537 "movdqa %%xmm3, %%xmm5 \n\t" // r1>>63:r0>>63
538 "pslldq $8, %%xmm3 \n\t" // r0>>63:0
539 "pslldq $8, %%xmm4 \n\t" // r2>>63:0
540 "psrldq $8, %%xmm5 \n\t" // 0:r1>>63
541 "por %%xmm3, %%xmm1 \n\t" // r1<<1|r0>>63:r0<<1
542 "por %%xmm4, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1
543 "por %%xmm5, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1|r1>>63
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100544
545 /*
546 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
Gilles Peskined4f31c82023-03-10 22:21:47 +0100547 * using [CLMUL-WP] algorithm 5 (p. 18).
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100548 * Currently xmm2:xmm1 holds x3:x2:x1:x0 (already shifted).
549 */
550 /* Step 2 (1) */
Gilles Peskine4e201442023-03-15 19:36:03 +0100551 "movdqa %%xmm1, %%xmm3 \n\t" // x1:x0
552 "movdqa %%xmm1, %%xmm4 \n\t" // same
553 "movdqa %%xmm1, %%xmm5 \n\t" // same
554 "psllq $63, %%xmm3 \n\t" // x1<<63:x0<<63 = stuff:a
555 "psllq $62, %%xmm4 \n\t" // x1<<62:x0<<62 = stuff:b
556 "psllq $57, %%xmm5 \n\t" // x1<<57:x0<<57 = stuff:c
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100557
558 /* Step 2 (2) */
Gilles Peskine4e201442023-03-15 19:36:03 +0100559 "pxor %%xmm4, %%xmm3 \n\t" // stuff:a+b
560 "pxor %%xmm5, %%xmm3 \n\t" // stuff:a+b+c
561 "pslldq $8, %%xmm3 \n\t" // a+b+c:0
562 "pxor %%xmm3, %%xmm1 \n\t" // x1+a+b+c:x0 = d:x0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100563
564 /* Steps 3 and 4 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100565 "movdqa %%xmm1,%%xmm0 \n\t" // d:x0
566 "movdqa %%xmm1,%%xmm4 \n\t" // same
567 "movdqa %%xmm1,%%xmm5 \n\t" // same
568 "psrlq $1, %%xmm0 \n\t" // e1:x0>>1 = e1:e0'
569 "psrlq $2, %%xmm4 \n\t" // f1:x0>>2 = f1:f0'
570 "psrlq $7, %%xmm5 \n\t" // g1:x0>>7 = g1:g0'
571 "pxor %%xmm4, %%xmm0 \n\t" // e1+f1:e0'+f0'
572 "pxor %%xmm5, %%xmm0 \n\t" // e1+f1+g1:e0'+f0'+g0'
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200573 // e0'+f0'+g0' is almost e0+f0+g0, ex\tcept for some missing
574 // bits carried from d. Now get those\t bits back in.
Gilles Peskine4e201442023-03-15 19:36:03 +0100575 "movdqa %%xmm1,%%xmm3 \n\t" // d:x0
576 "movdqa %%xmm1,%%xmm4 \n\t" // same
577 "movdqa %%xmm1,%%xmm5 \n\t" // same
578 "psllq $63, %%xmm3 \n\t" // d<<63:stuff
579 "psllq $62, %%xmm4 \n\t" // d<<62:stuff
580 "psllq $57, %%xmm5 \n\t" // d<<57:stuff
581 "pxor %%xmm4, %%xmm3 \n\t" // d<<63+d<<62:stuff
582 "pxor %%xmm5, %%xmm3 \n\t" // missing bits of d:stuff
583 "psrldq $8, %%xmm3 \n\t" // 0:missing bits of d
584 "pxor %%xmm3, %%xmm0 \n\t" // e1+f1+g1:e0+f0+g0
585 "pxor %%xmm1, %%xmm0 \n\t" // h1:h0
586 "pxor %%xmm2, %%xmm0 \n\t" // x3+h1:x2+h0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100587
Gilles Peskine4e201442023-03-15 19:36:03 +0100588 "movdqu %%xmm0, (%2) \n\t" // done
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100589 :
590 : "r" (aa), "r" (bb), "r" (cc)
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100592
593 /* Now byte-reverse the outputs */
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 for (i = 0; i < 16; i++) {
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100595 c[i] = cc[15 - i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 }
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100597
Paul Bakkere7f51332013-12-30 15:32:02 +0100598 return;
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100599}
600
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100601/*
602 * Compute decryption round keys from encryption round keys
603 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100604void mbedtls_aesni_inverse_key(unsigned char *invkey,
605 const unsigned char *fwdkey, int nr)
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100606{
607 unsigned char *ik = invkey;
608 const unsigned char *fk = fwdkey + 16 * nr;
609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 for (fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16) {
613 asm ("movdqu (%0), %%xmm0 \n\t"
Gilles Peskine4e201442023-03-15 19:36:03 +0100614 AESIMC(xmm0_xmm0)
615 "movdqu %%xmm0, (%1) \n\t"
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100616 :
617 : "r" (fk), "r" (ik)
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 : "memory", "xmm0");
619 }
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100620
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100622}
623
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100624/*
625 * Key expansion, 128-bit case
626 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100627static void aesni_setkey_enc_128(unsigned char *rk,
628 const unsigned char *key)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100629{
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 asm ("movdqu (%1), %%xmm0 \n\t" // copy the original key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200631 "movdqu %%xmm0, (%0) \n\t" // as round key 0
632 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100633
634 /*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100635 * Finish generating the next round key.
636 *
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100637 * On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff
638 * with X = rot( sub( r3 ) ) ^ RCON.
639 *
640 * On exit, xmm0 is r7:r6:r5:r4
641 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
642 * and those are written to the round key buffer.
643 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200644 "1: \n\t"
645 "pshufd $0xff, %%xmm1, %%xmm1 \n\t" // X:X:X:X
646 "pxor %%xmm0, %%xmm1 \n\t" // X+r3:X+r2:X+r1:r4
647 "pslldq $4, %%xmm0 \n\t" // r2:r1:r0:0
648 "pxor %%xmm0, %%xmm1 \n\t" // X+r3+r2:X+r2+r1:r5:r4
649 "pslldq $4, %%xmm0 \n\t" // etc
650 "pxor %%xmm0, %%xmm1 \n\t"
651 "pslldq $4, %%xmm0 \n\t"
652 "pxor %%xmm1, %%xmm0 \n\t" // update xmm0 for next time!
653 "add $16, %0 \n\t" // point to next round key
654 "movdqu %%xmm0, (%0) \n\t" // write it
655 "ret \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100656
657 /* Main "loop" */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200658 "2: \n\t"
Gilles Peskine4e201442023-03-15 19:36:03 +0100659 AESKEYGENA(xmm0_xmm1, "0x01") "call 1b \n\t"
660 AESKEYGENA(xmm0_xmm1, "0x02") "call 1b \n\t"
661 AESKEYGENA(xmm0_xmm1, "0x04") "call 1b \n\t"
662 AESKEYGENA(xmm0_xmm1, "0x08") "call 1b \n\t"
663 AESKEYGENA(xmm0_xmm1, "0x10") "call 1b \n\t"
664 AESKEYGENA(xmm0_xmm1, "0x20") "call 1b \n\t"
665 AESKEYGENA(xmm0_xmm1, "0x40") "call 1b \n\t"
666 AESKEYGENA(xmm0_xmm1, "0x80") "call 1b \n\t"
667 AESKEYGENA(xmm0_xmm1, "0x1B") "call 1b \n\t"
668 AESKEYGENA(xmm0_xmm1, "0x36") "call 1b \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100669 :
670 : "r" (rk), "r" (key)
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 : "memory", "cc", "0");
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100672}
673
674/*
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100675 * Key expansion, 192-bit case
676 */
Arto Kinnunen732ca322023-04-14 14:26:10 +0800677#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100678static void aesni_setkey_enc_192(unsigned char *rk,
679 const unsigned char *key)
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100680{
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 asm ("movdqu (%1), %%xmm0 \n\t" // copy original round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200682 "movdqu %%xmm0, (%0) \n\t"
683 "add $16, %0 \n\t"
684 "movq 16(%1), %%xmm1 \n\t"
685 "movq %%xmm1, (%0) \n\t"
686 "add $8, %0 \n\t"
687 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100688
689 /*
690 * Finish generating the next 6 quarter-keys.
691 *
692 * On entry xmm0 is r3:r2:r1:r0, xmm1 is stuff:stuff:r5:r4
693 * and xmm2 is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON.
694 *
695 * On exit, xmm0 is r9:r8:r7:r6 and xmm1 is stuff:stuff:r11:r10
696 * and those are written to the round key buffer.
697 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200698 "1: \n\t"
699 "pshufd $0x55, %%xmm2, %%xmm2 \n\t" // X:X:X:X
700 "pxor %%xmm0, %%xmm2 \n\t" // X+r3:X+r2:X+r1:r4
701 "pslldq $4, %%xmm0 \n\t" // etc
702 "pxor %%xmm0, %%xmm2 \n\t"
703 "pslldq $4, %%xmm0 \n\t"
704 "pxor %%xmm0, %%xmm2 \n\t"
705 "pslldq $4, %%xmm0 \n\t"
706 "pxor %%xmm2, %%xmm0 \n\t" // update xmm0 = r9:r8:r7:r6
707 "movdqu %%xmm0, (%0) \n\t"
708 "add $16, %0 \n\t"
709 "pshufd $0xff, %%xmm0, %%xmm2 \n\t" // r9:r9:r9:r9
710 "pxor %%xmm1, %%xmm2 \n\t" // stuff:stuff:r9+r5:r10
711 "pslldq $4, %%xmm1 \n\t" // r2:r1:r0:0
712 "pxor %%xmm2, %%xmm1 \n\t" // xmm1 = stuff:stuff:r11:r10
713 "movq %%xmm1, (%0) \n\t"
714 "add $8, %0 \n\t"
715 "ret \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100716
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200717 "2: \n\t"
Gilles Peskine4e201442023-03-15 19:36:03 +0100718 AESKEYGENA(xmm1_xmm2, "0x01") "call 1b \n\t"
719 AESKEYGENA(xmm1_xmm2, "0x02") "call 1b \n\t"
720 AESKEYGENA(xmm1_xmm2, "0x04") "call 1b \n\t"
721 AESKEYGENA(xmm1_xmm2, "0x08") "call 1b \n\t"
722 AESKEYGENA(xmm1_xmm2, "0x10") "call 1b \n\t"
723 AESKEYGENA(xmm1_xmm2, "0x20") "call 1b \n\t"
724 AESKEYGENA(xmm1_xmm2, "0x40") "call 1b \n\t"
725 AESKEYGENA(xmm1_xmm2, "0x80") "call 1b \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100726
727 :
728 : "r" (rk), "r" (key)
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 : "memory", "cc", "0");
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100730}
Arto Kinnunen732ca322023-04-14 14:26:10 +0800731#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100732
733/*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100734 * Key expansion, 256-bit case
735 */
Arto Kinnunen732ca322023-04-14 14:26:10 +0800736#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100737static void aesni_setkey_enc_256(unsigned char *rk,
738 const unsigned char *key)
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100739{
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 asm ("movdqu (%1), %%xmm0 \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200741 "movdqu %%xmm0, (%0) \n\t"
742 "add $16, %0 \n\t"
743 "movdqu 16(%1), %%xmm1 \n\t"
744 "movdqu %%xmm1, (%0) \n\t"
745 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100746
747 /*
748 * Finish generating the next two round keys.
749 *
750 * On entry xmm0 is r3:r2:r1:r0, xmm1 is r7:r6:r5:r4 and
751 * xmm2 is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
752 *
753 * On exit, xmm0 is r11:r10:r9:r8 and xmm1 is r15:r14:r13:r12
754 * and those have been written to the output buffer.
755 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200756 "1: \n\t"
757 "pshufd $0xff, %%xmm2, %%xmm2 \n\t"
758 "pxor %%xmm0, %%xmm2 \n\t"
759 "pslldq $4, %%xmm0 \n\t"
760 "pxor %%xmm0, %%xmm2 \n\t"
761 "pslldq $4, %%xmm0 \n\t"
762 "pxor %%xmm0, %%xmm2 \n\t"
763 "pslldq $4, %%xmm0 \n\t"
764 "pxor %%xmm2, %%xmm0 \n\t"
765 "add $16, %0 \n\t"
766 "movdqu %%xmm0, (%0) \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100767
768 /* Set xmm2 to stuff:Y:stuff:stuff with Y = subword( r11 )
769 * and proceed to generate next round key from there */
Gilles Peskine4e201442023-03-15 19:36:03 +0100770 AESKEYGENA(xmm0_xmm2, "0x00")
771 "pshufd $0xaa, %%xmm2, %%xmm2 \n\t"
772 "pxor %%xmm1, %%xmm2 \n\t"
773 "pslldq $4, %%xmm1 \n\t"
774 "pxor %%xmm1, %%xmm2 \n\t"
775 "pslldq $4, %%xmm1 \n\t"
776 "pxor %%xmm1, %%xmm2 \n\t"
777 "pslldq $4, %%xmm1 \n\t"
778 "pxor %%xmm2, %%xmm1 \n\t"
779 "add $16, %0 \n\t"
780 "movdqu %%xmm1, (%0) \n\t"
781 "ret \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100782
783 /*
784 * Main "loop" - Generating one more key than necessary,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 * see definition of mbedtls_aes_context.buf
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100786 */
Gilles Peskine4e201442023-03-15 19:36:03 +0100787 "2: \n\t"
788 AESKEYGENA(xmm1_xmm2, "0x01") "call 1b \n\t"
789 AESKEYGENA(xmm1_xmm2, "0x02") "call 1b \n\t"
790 AESKEYGENA(xmm1_xmm2, "0x04") "call 1b \n\t"
791 AESKEYGENA(xmm1_xmm2, "0x08") "call 1b \n\t"
792 AESKEYGENA(xmm1_xmm2, "0x10") "call 1b \n\t"
793 AESKEYGENA(xmm1_xmm2, "0x20") "call 1b \n\t"
794 AESKEYGENA(xmm1_xmm2, "0x40") "call 1b \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100795 :
796 : "r" (rk), "r" (key)
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 : "memory", "cc", "0");
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100798}
Arto Kinnunen732ca322023-04-14 14:26:10 +0800799#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100800
Gilles Peskine9c682e72023-03-16 17:21:33 +0100801#endif /* MBEDTLS_AESNI_HAVE_CODE */
Gilles Peskined6719172023-03-10 22:37:11 +0100802
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100803/*
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100804 * Key expansion, wrapper
805 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100806int mbedtls_aesni_setkey_enc(unsigned char *rk,
807 const unsigned char *key,
808 size_t bits)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100809{
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 switch (bits) {
811 case 128: aesni_setkey_enc_128(rk, key); break;
Arto Kinnunen732ca322023-04-14 14:26:10 +0800812#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 case 192: aesni_setkey_enc_192(rk, key); break;
814 case 256: aesni_setkey_enc_256(rk, key); break;
Arto Kinnunen732ca322023-04-14 14:26:10 +0800815#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 default: return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100817 }
818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 return 0;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100820}
821
Gilles Peskine9c682e72023-03-16 17:21:33 +0100822#endif /* MBEDTLS_AESNI_HAVE_CODE */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824#endif /* MBEDTLS_AESNI_C */