blob: dd84c2b4ea62c4dcba9b4f17dffaa23e82132287 [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 Rodgman0f2971a2023-11-03 12:04:52 +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 Peskine6055b782023-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
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000017#include "mbedtls/aesni.h"
Rich Evans00ab4702015-02-06 13:43:58 +000018
19#include <string.h>
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010020
David Horstmannb6bf5f52023-01-03 11:07:09 +000021/* *INDENT-OFF* */
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020022#ifndef asm
23#define asm __asm
24#endif
David Horstmannb6bf5f52023-01-03 11:07:09 +000025/* *INDENT-ON* */
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020026
Gilles Peskine6dec5412023-03-16 17:21:33 +010027#if defined(MBEDTLS_AESNI_HAVE_CODE)
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010028
Gilles Peskine6dec5412023-03-16 17:21:33 +010029#if MBEDTLS_AESNI_HAVE_CODE == 2
Tom Cosgrove790756d2023-03-13 15:32:52 +000030#if !defined(_WIN32)
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010031#include <cpuid.h>
SlugFillere2d06142023-06-23 06:24:49 +030032#else
33#include <intrin.h>
Tom Cosgrove790756d2023-03-13 15:32:52 +000034#endif
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010035#include <immintrin.h>
36#endif
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010037
38/*
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010039 * AES-NI support detection routine
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010040 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010041int mbedtls_aesni_has_support(unsigned int what)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010042{
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010043 static int done = 0;
44 static unsigned int c = 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010045
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010046 if (!done) {
Gilles Peskine6dec5412023-03-16 17:21:33 +010047#if MBEDTLS_AESNI_HAVE_CODE == 2
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010048 static unsigned info[4] = { 0, 0, 0, 0 };
49#if defined(_MSC_VER)
50 __cpuid(info, 1);
51#else
52 __cpuid(1, info[0], info[1], info[2], info[3]);
53#endif
54 c = info[2];
Gilles Peskine6dec5412023-03-16 17:21:33 +010055#else /* AESNI using asm */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010056 asm ("movl $1, %%eax \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +020057 "cpuid \n\t"
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010058 : "=c" (c)
59 :
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010060 : "eax", "ebx", "edx");
Gilles Peskine6dec5412023-03-16 17:21:33 +010061#endif /* MBEDTLS_AESNI_HAVE_CODE */
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010062 done = 1;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010063 }
64
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010065 return (c & what) != 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010066}
67
Gilles Peskine6dec5412023-03-16 17:21:33 +010068#if MBEDTLS_AESNI_HAVE_CODE == 2
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010069
70/*
71 * AES-NI AES-ECB block en(de)cryption
72 */
73int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
74 int mode,
75 const unsigned char input[16],
76 unsigned char output[16])
77{
78 const __m128i *rk = (const __m128i *) (ctx->rk);
79 unsigned nr = ctx->nr; // Number of remaining rounds
Tom Cosgrove790756d2023-03-13 15:32:52 +000080
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010081 // Load round key 0
Gilles Peskined4a23932023-03-15 20:37:57 +010082 __m128i state;
83 memcpy(&state, input, 16);
84 state = _mm_xor_si128(state, rk[0]); // state ^= *rk;
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010085 ++rk;
86 --nr;
87
88 if (mode == 0) {
89 while (nr != 0) {
Gilles Peskined4a23932023-03-15 20:37:57 +010090 state = _mm_aesdec_si128(state, *rk);
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010091 ++rk;
92 --nr;
93 }
Gilles Peskined4a23932023-03-15 20:37:57 +010094 state = _mm_aesdeclast_si128(state, *rk);
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010095 } else {
96 while (nr != 0) {
Gilles Peskined4a23932023-03-15 20:37:57 +010097 state = _mm_aesenc_si128(state, *rk);
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010098 ++rk;
99 --nr;
100 }
Gilles Peskined4a23932023-03-15 20:37:57 +0100101 state = _mm_aesenclast_si128(state, *rk);
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100102 }
103
Gilles Peskined4a23932023-03-15 20:37:57 +0100104 memcpy(output, &state, 16);
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100105 return 0;
106}
107
108/*
109 * GCM multiplication: c = a times b in GF(2^128)
110 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
111 */
112
113static void gcm_clmul(const __m128i aa, const __m128i bb,
114 __m128i *cc, __m128i *dd)
115{
116 /*
117 * Caryless multiplication dd:cc = aa * bb
118 * using [CLMUL-WP] algorithm 1 (p. 12).
119 */
120 *cc = _mm_clmulepi64_si128(aa, bb, 0x00); // a0*b0 = c1:c0
121 *dd = _mm_clmulepi64_si128(aa, bb, 0x11); // a1*b1 = d1:d0
122 __m128i ee = _mm_clmulepi64_si128(aa, bb, 0x10); // a0*b1 = e1:e0
123 __m128i ff = _mm_clmulepi64_si128(aa, bb, 0x01); // a1*b0 = f1:f0
Tom Cosgrove790756d2023-03-13 15:32:52 +0000124 ff = _mm_xor_si128(ff, ee); // e1+f1:e0+f0
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100125 ee = ff; // e1+f1:e0+f0
126 ff = _mm_srli_si128(ff, 8); // 0:e1+f1
127 ee = _mm_slli_si128(ee, 8); // e0+f0:0
Tom Cosgrove790756d2023-03-13 15:32:52 +0000128 *dd = _mm_xor_si128(*dd, ff); // d1:d0+e1+f1
Gilles Peskine5f1677f2023-03-16 13:08:18 +0100129 *cc = _mm_xor_si128(*cc, ee); // c1+e0+f0:c0
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100130}
131
132static void gcm_shift(__m128i *cc, __m128i *dd)
133{
Gilles Peskined4a23932023-03-15 20:37:57 +0100134 /* [CMUCL-WP] Algorithm 5 Step 1: shift cc:dd one bit to the left,
135 * taking advantage of [CLMUL-WP] eq 27 (p. 18). */
136 // // *cc = r1:r0
137 // // *dd = r3:r2
138 __m128i cc_lo = _mm_slli_epi64(*cc, 1); // r1<<1:r0<<1
139 __m128i dd_lo = _mm_slli_epi64(*dd, 1); // r3<<1:r2<<1
140 __m128i cc_hi = _mm_srli_epi64(*cc, 63); // r1>>63:r0>>63
141 __m128i dd_hi = _mm_srli_epi64(*dd, 63); // r3>>63:r2>>63
142 __m128i xmm5 = _mm_srli_si128(cc_hi, 8); // 0:r1>>63
143 cc_hi = _mm_slli_si128(cc_hi, 8); // r0>>63:0
144 dd_hi = _mm_slli_si128(dd_hi, 8); // 0:r1>>63
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100145
Gilles Peskined4a23932023-03-15 20:37:57 +0100146 *cc = _mm_or_si128(cc_lo, cc_hi); // r1<<1|r0>>63:r0<<1
147 *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 +0100148}
149
Gilles Peskined4a23932023-03-15 20:37:57 +0100150static __m128i gcm_reduce(__m128i xx)
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100151{
152 // // xx = x1:x0
153 /* [CLMUL-WP] Algorithm 5 Step 2 */
154 __m128i aa = _mm_slli_epi64(xx, 63); // x1<<63:x0<<63 = stuff:a
155 __m128i bb = _mm_slli_epi64(xx, 62); // x1<<62:x0<<62 = stuff:b
156 __m128i cc = _mm_slli_epi64(xx, 57); // x1<<57:x0<<57 = stuff:c
Tom Cosgrove790756d2023-03-13 15:32:52 +0000157 __m128i dd = _mm_slli_si128(_mm_xor_si128(_mm_xor_si128(aa, bb), cc), 8); // a+b+c:0
158 return _mm_xor_si128(dd, xx); // x1+a+b+c:x0 = d:x0
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100159}
160
Gilles Peskined4a23932023-03-15 20:37:57 +0100161static __m128i gcm_mix(__m128i dx)
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100162{
163 /* [CLMUL-WP] Algorithm 5 Steps 3 and 4 */
164 __m128i ee = _mm_srli_epi64(dx, 1); // e1:x0>>1 = e1:e0'
165 __m128i ff = _mm_srli_epi64(dx, 2); // f1:x0>>2 = f1:f0'
166 __m128i gg = _mm_srli_epi64(dx, 7); // g1:x0>>7 = g1:g0'
167
168 // e0'+f0'+g0' is almost e0+f0+g0, except for some missing
169 // bits carried from d. Now get those bits back in.
170 __m128i eh = _mm_slli_epi64(dx, 63); // d<<63:stuff
171 __m128i fh = _mm_slli_epi64(dx, 62); // d<<62:stuff
172 __m128i gh = _mm_slli_epi64(dx, 57); // d<<57:stuff
Tom Cosgrove790756d2023-03-13 15:32:52 +0000173 __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 +0100174
Tom Cosgrove790756d2023-03-13 15:32:52 +0000175 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 +0100176}
177
178void mbedtls_aesni_gcm_mult(unsigned char c[16],
179 const unsigned char a[16],
180 const unsigned char b[16])
181{
182 __m128i aa, bb, cc, dd;
183
184 /* The inputs are in big-endian order, so byte-reverse them */
185 for (size_t i = 0; i < 16; i++) {
186 ((uint8_t *) &aa)[i] = a[15 - i];
187 ((uint8_t *) &bb)[i] = b[15 - i];
188 }
189
190 gcm_clmul(aa, bb, &cc, &dd);
191 gcm_shift(&cc, &dd);
192 /*
193 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
194 * using [CLMUL-WP] algorithm 5 (p. 18).
195 * Currently dd:cc holds x3:x2:x1:x0 (already shifted).
196 */
Gilles Peskined4a23932023-03-15 20:37:57 +0100197 __m128i dx = gcm_reduce(cc);
198 __m128i xh = gcm_mix(dx);
Tom Cosgrove790756d2023-03-13 15:32:52 +0000199 cc = _mm_xor_si128(xh, dd); // x3+h1:x2+h0
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100200
201 /* Now byte-reverse the outputs */
202 for (size_t i = 0; i < 16; i++) {
203 c[i] = ((uint8_t *) &cc)[15 - i];
204 }
205
206 return;
207}
208
209/*
210 * Compute decryption round keys from encryption round keys
211 */
212void mbedtls_aesni_inverse_key(unsigned char *invkey,
213 const unsigned char *fwdkey, int nr)
214{
215 __m128i *ik = (__m128i *) invkey;
216 const __m128i *fk = (const __m128i *) fwdkey + nr;
217
218 *ik = *fk;
219 for (--fk, ++ik; fk > (const __m128i *) fwdkey; --fk, ++ik) {
220 *ik = _mm_aesimc_si128(*fk);
221 }
222 *ik = *fk;
223}
224
225/*
226 * Key expansion, 128-bit case
227 */
Gilles Peskined4a23932023-03-15 20:37:57 +0100228static __m128i aesni_set_rk_128(__m128i state, __m128i xword)
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100229{
230 /*
231 * Finish generating the next round key.
232 *
Gilles Peskined4a23932023-03-15 20:37:57 +0100233 * On entry state is r3:r2:r1:r0 and xword is X:stuff:stuff:stuff
234 * with X = rot( sub( r3 ) ) ^ RCON (obtained with AESKEYGENASSIST).
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100235 *
Gilles Peskined4a23932023-03-15 20:37:57 +0100236 * On exit, xword is r7:r6:r5:r4
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100237 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
238 * and this is returned, to be written to the round key buffer.
239 */
Gilles Peskined4a23932023-03-15 20:37:57 +0100240 xword = _mm_shuffle_epi32(xword, 0xff); // X:X:X:X
241 xword = _mm_xor_si128(xword, state); // X+r3:X+r2:X+r1:r4
242 state = _mm_slli_si128(state, 4); // r2:r1:r0:0
243 xword = _mm_xor_si128(xword, state); // X+r3+r2:X+r2+r1:r5:r4
244 state = _mm_slli_si128(state, 4); // r1:r0:0:0
245 xword = _mm_xor_si128(xword, state); // X+r3+r2+r1:r6:r5:r4
246 state = _mm_slli_si128(state, 4); // r0:0:0:0
247 state = _mm_xor_si128(xword, state); // r7:r6:r5:r4
248 return state;
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100249}
250
251static void aesni_setkey_enc_128(unsigned char *rk_bytes,
252 const unsigned char *key)
253{
254 __m128i *rk = (__m128i *) rk_bytes;
255
256 memcpy(&rk[0], key, 16);
257 rk[1] = aesni_set_rk_128(rk[0], _mm_aeskeygenassist_si128(rk[0], 0x01));
258 rk[2] = aesni_set_rk_128(rk[1], _mm_aeskeygenassist_si128(rk[1], 0x02));
259 rk[3] = aesni_set_rk_128(rk[2], _mm_aeskeygenassist_si128(rk[2], 0x04));
260 rk[4] = aesni_set_rk_128(rk[3], _mm_aeskeygenassist_si128(rk[3], 0x08));
261 rk[5] = aesni_set_rk_128(rk[4], _mm_aeskeygenassist_si128(rk[4], 0x10));
262 rk[6] = aesni_set_rk_128(rk[5], _mm_aeskeygenassist_si128(rk[5], 0x20));
263 rk[7] = aesni_set_rk_128(rk[6], _mm_aeskeygenassist_si128(rk[6], 0x40));
264 rk[8] = aesni_set_rk_128(rk[7], _mm_aeskeygenassist_si128(rk[7], 0x80));
265 rk[9] = aesni_set_rk_128(rk[8], _mm_aeskeygenassist_si128(rk[8], 0x1B));
266 rk[10] = aesni_set_rk_128(rk[9], _mm_aeskeygenassist_si128(rk[9], 0x36));
267}
268
269/*
270 * Key expansion, 192-bit case
271 */
Gilles Peskined4a23932023-03-15 20:37:57 +0100272static void aesni_set_rk_192(__m128i *state0, __m128i *state1, __m128i xword,
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100273 unsigned char *rk)
274{
275 /*
276 * Finish generating the next 6 quarter-keys.
277 *
Gilles Peskined4a23932023-03-15 20:37:57 +0100278 * On entry state0 is r3:r2:r1:r0, state1 is stuff:stuff:r5:r4
279 * and xword is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON
280 * (obtained with AESKEYGENASSIST).
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100281 *
Gilles Peskined4a23932023-03-15 20:37:57 +0100282 * On exit, state0 is r9:r8:r7:r6 and state1 is stuff:stuff:r11:r10
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100283 * and those are written to the round key buffer.
284 */
Gilles Peskined4a23932023-03-15 20:37:57 +0100285 xword = _mm_shuffle_epi32(xword, 0x55); // X:X:X:X
286 xword = _mm_xor_si128(xword, *state0); // X+r3:X+r2:X+r1:X+r0
287 *state0 = _mm_slli_si128(*state0, 4); // r2:r1:r0:0
288 xword = _mm_xor_si128(xword, *state0); // X+r3+r2:X+r2+r1:X+r1+r0:X+r0
289 *state0 = _mm_slli_si128(*state0, 4); // r1:r0:0:0
290 xword = _mm_xor_si128(xword, *state0); // X+r3+r2+r1:X+r2+r1+r0:X+r1+r0:X+r0
291 *state0 = _mm_slli_si128(*state0, 4); // r0:0:0:0
292 xword = _mm_xor_si128(xword, *state0); // X+r3+r2+r1+r0:X+r2+r1+r0:X+r1+r0:X+r0
293 *state0 = xword; // = r9:r8:r7:r6
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100294
Gilles Peskined4a23932023-03-15 20:37:57 +0100295 xword = _mm_shuffle_epi32(xword, 0xff); // r9:r9:r9:r9
296 xword = _mm_xor_si128(xword, *state1); // stuff:stuff:r9+r5:r9+r4
297 *state1 = _mm_slli_si128(*state1, 4); // stuff:stuff:r4:0
298 xword = _mm_xor_si128(xword, *state1); // stuff:stuff:r9+r5+r4:r9+r4
299 *state1 = xword; // = stuff:stuff:r11:r10
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100300
Gilles Peskined4a23932023-03-15 20:37:57 +0100301 /* Store state0 and the low half of state1 into rk, which is conceptually
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100302 * an array of 24-byte elements. Since 24 is not a multiple of 16,
Gilles Peskined4a23932023-03-15 20:37:57 +0100303 * rk is not necessarily aligned so just `*rk = *state0` doesn't work. */
304 memcpy(rk, state0, 16);
Gilles Peskine2e8d8d12023-03-15 23:16:27 +0100305 memcpy(rk + 16, state1, 8);
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100306}
307
308static void aesni_setkey_enc_192(unsigned char *rk,
309 const unsigned char *key)
310{
311 /* First round: use original key */
312 memcpy(rk, key, 24);
313 /* aes.c guarantees that rk is aligned on a 16-byte boundary. */
Gilles Peskined4a23932023-03-15 20:37:57 +0100314 __m128i state0 = ((__m128i *) rk)[0];
315 __m128i state1 = _mm_loadl_epi64(((__m128i *) rk) + 1);
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100316
Gilles Peskined4a23932023-03-15 20:37:57 +0100317 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x01), rk + 24 * 1);
318 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x02), rk + 24 * 2);
319 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x04), rk + 24 * 3);
320 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x08), rk + 24 * 4);
321 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x10), rk + 24 * 5);
322 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x20), rk + 24 * 6);
323 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x40), rk + 24 * 7);
324 aesni_set_rk_192(&state0, &state1, _mm_aeskeygenassist_si128(state1, 0x80), rk + 24 * 8);
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100325}
326
327/*
328 * Key expansion, 256-bit case
329 */
Gilles Peskined4a23932023-03-15 20:37:57 +0100330static void aesni_set_rk_256(__m128i state0, __m128i state1, __m128i xword,
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100331 __m128i *rk0, __m128i *rk1)
332{
333 /*
334 * Finish generating the next two round keys.
335 *
Gilles Peskined4a23932023-03-15 20:37:57 +0100336 * On entry state0 is r3:r2:r1:r0, state1 is r7:r6:r5:r4 and
337 * xword is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
338 * (obtained with AESKEYGENASSIST).
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100339 *
340 * On exit, *rk0 is r11:r10:r9:r8 and *rk1 is r15:r14:r13:r12
341 */
Gilles Peskined4a23932023-03-15 20:37:57 +0100342 xword = _mm_shuffle_epi32(xword, 0xff);
343 xword = _mm_xor_si128(xword, state0);
344 state0 = _mm_slli_si128(state0, 4);
345 xword = _mm_xor_si128(xword, state0);
346 state0 = _mm_slli_si128(state0, 4);
347 xword = _mm_xor_si128(xword, state0);
348 state0 = _mm_slli_si128(state0, 4);
349 state0 = _mm_xor_si128(state0, xword);
350 *rk0 = state0;
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100351
Gilles Peskined4a23932023-03-15 20:37:57 +0100352 /* Set xword to stuff:Y:stuff:stuff with Y = subword( r11 )
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100353 * and proceed to generate next round key from there */
Gilles Peskined4a23932023-03-15 20:37:57 +0100354 xword = _mm_aeskeygenassist_si128(state0, 0x00);
355 xword = _mm_shuffle_epi32(xword, 0xaa);
356 xword = _mm_xor_si128(xword, state1);
357 state1 = _mm_slli_si128(state1, 4);
358 xword = _mm_xor_si128(xword, state1);
359 state1 = _mm_slli_si128(state1, 4);
360 xword = _mm_xor_si128(xword, state1);
361 state1 = _mm_slli_si128(state1, 4);
362 state1 = _mm_xor_si128(state1, xword);
363 *rk1 = state1;
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100364}
365
366static void aesni_setkey_enc_256(unsigned char *rk_bytes,
367 const unsigned char *key)
368{
369 __m128i *rk = (__m128i *) rk_bytes;
370
371 memcpy(&rk[0], key, 16);
372 memcpy(&rk[1], key + 16, 16);
373
374 /*
375 * Main "loop" - Generating one more key than necessary,
376 * see definition of mbedtls_aes_context.buf
377 */
378 aesni_set_rk_256(rk[0], rk[1], _mm_aeskeygenassist_si128(rk[1], 0x01), &rk[2], &rk[3]);
379 aesni_set_rk_256(rk[2], rk[3], _mm_aeskeygenassist_si128(rk[3], 0x02), &rk[4], &rk[5]);
380 aesni_set_rk_256(rk[4], rk[5], _mm_aeskeygenassist_si128(rk[5], 0x04), &rk[6], &rk[7]);
381 aesni_set_rk_256(rk[6], rk[7], _mm_aeskeygenassist_si128(rk[7], 0x08), &rk[8], &rk[9]);
382 aesni_set_rk_256(rk[8], rk[9], _mm_aeskeygenassist_si128(rk[9], 0x10), &rk[10], &rk[11]);
383 aesni_set_rk_256(rk[10], rk[11], _mm_aeskeygenassist_si128(rk[11], 0x20), &rk[12], &rk[13]);
384 aesni_set_rk_256(rk[12], rk[13], _mm_aeskeygenassist_si128(rk[13], 0x40), &rk[14], &rk[15]);
385}
386
Gilles Peskine6dec5412023-03-16 17:21:33 +0100387#else /* MBEDTLS_AESNI_HAVE_CODE == 1 */
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100388
Gilles Peskine18d521a2023-03-10 22:25:13 +0100389#if defined(__has_feature)
390#if __has_feature(memory_sanitizer)
391#warning \
392 "MBEDTLS_AESNI_C is known to cause spurious error reports with some memory sanitizers as they do not understand the assembly code."
393#endif
394#endif
395
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100396/*
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +0200397 * Binutils needs to be at least 2.19 to support AES-NI instructions.
398 * Unfortunately, a lot of users have a lower version now (2014-04).
399 * Emit bytecode directly in order to support "old" version of gas.
400 *
401 * Opcodes from the Intel architecture reference manual, vol. 3.
402 * We always use registers, so we don't need prefixes for memory operands.
403 * Operand macros are in gas order (src, dst) as opposed to Intel order
404 * (dst, src) in order to blend better into the surrounding assembly code.
405 */
Gilles Peskine2808a602023-03-15 19:36:03 +0100406#define AESDEC(regs) ".byte 0x66,0x0F,0x38,0xDE," regs "\n\t"
407#define AESDECLAST(regs) ".byte 0x66,0x0F,0x38,0xDF," regs "\n\t"
408#define AESENC(regs) ".byte 0x66,0x0F,0x38,0xDC," regs "\n\t"
409#define AESENCLAST(regs) ".byte 0x66,0x0F,0x38,0xDD," regs "\n\t"
410#define AESIMC(regs) ".byte 0x66,0x0F,0x38,0xDB," regs "\n\t"
411#define AESKEYGENA(regs, imm) ".byte 0x66,0x0F,0x3A,0xDF," regs "," imm "\n\t"
412#define PCLMULQDQ(regs, imm) ".byte 0x66,0x0F,0x3A,0x44," regs "," imm "\n\t"
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +0200413
414#define xmm0_xmm0 "0xC0"
415#define xmm0_xmm1 "0xC8"
416#define xmm0_xmm2 "0xD0"
417#define xmm0_xmm3 "0xD8"
418#define xmm0_xmm4 "0xE0"
419#define xmm1_xmm0 "0xC1"
420#define xmm1_xmm2 "0xD1"
421
422/*
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100423 * AES-NI AES-ECB block en(de)cryption
424 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100425int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
426 int mode,
427 const unsigned char input[16],
428 unsigned char output[16])
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100429{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100430 asm ("movdqu (%3), %%xmm0 \n\t" // load input
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200431 "movdqu (%1), %%xmm1 \n\t" // load round key 0
432 "pxor %%xmm1, %%xmm0 \n\t" // round 0
James Cowgill6c8edca2015-12-17 01:40:26 +0000433 "add $16, %1 \n\t" // point to next round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200434 "subl $1, %0 \n\t" // normal rounds = nr - 1
435 "test %2, %2 \n\t" // mode?
436 "jz 2f \n\t" // 0 = decrypt
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100437
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200438 "1: \n\t" // encryption loop
439 "movdqu (%1), %%xmm1 \n\t" // load round key
Gilles Peskine2808a602023-03-15 19:36:03 +0100440 AESENC(xmm1_xmm0) // do round
441 "add $16, %1 \n\t" // point to next round key
442 "subl $1, %0 \n\t" // loop
443 "jnz 1b \n\t"
444 "movdqu (%1), %%xmm1 \n\t" // load round key
445 AESENCLAST(xmm1_xmm0) // last round
446 "jmp 3f \n\t"
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100447
Gilles Peskine2808a602023-03-15 19:36:03 +0100448 "2: \n\t" // decryption loop
449 "movdqu (%1), %%xmm1 \n\t"
450 AESDEC(xmm1_xmm0) // do round
451 "add $16, %1 \n\t"
452 "subl $1, %0 \n\t"
453 "jnz 2b \n\t"
454 "movdqu (%1), %%xmm1 \n\t" // load round key
455 AESDECLAST(xmm1_xmm0) // last round
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100456
Gilles Peskine2808a602023-03-15 19:36:03 +0100457 "3: \n\t"
458 "movdqu %%xmm0, (%4) \n\t" // export output
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100459 :
460 : "r" (ctx->nr), "r" (ctx->rk), "r" (mode), "r" (input), "r" (output)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100461 : "memory", "cc", "xmm0", "xmm1");
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100462
463
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100464 return 0;
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100465}
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100466
467/*
468 * GCM multiplication: c = a times b in GF(2^128)
469 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
470 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100471void mbedtls_aesni_gcm_mult(unsigned char c[16],
472 const unsigned char a[16],
473 const unsigned char b[16])
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100474{
475 unsigned char aa[16], bb[16], cc[16];
476 size_t i;
477
478 /* The inputs are in big-endian order, so byte-reverse them */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100479 for (i = 0; i < 16; i++) {
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100480 aa[i] = a[15 - i];
481 bb[i] = b[15 - i];
482 }
483
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100484 asm ("movdqu (%0), %%xmm0 \n\t" // a1:a0
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200485 "movdqu (%1), %%xmm1 \n\t" // b1:b0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100486
487 /*
488 * Caryless multiplication xmm2:xmm1 = xmm0 * xmm1
Gilles Peskine6055b782023-03-10 22:21:47 +0100489 * using [CLMUL-WP] algorithm 1 (p. 12).
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100490 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200491 "movdqa %%xmm1, %%xmm2 \n\t" // copy of b1:b0
492 "movdqa %%xmm1, %%xmm3 \n\t" // same
493 "movdqa %%xmm1, %%xmm4 \n\t" // same
Gilles Peskine2808a602023-03-15 19:36:03 +0100494 PCLMULQDQ(xmm0_xmm1, "0x00") // a0*b0 = c1:c0
495 PCLMULQDQ(xmm0_xmm2, "0x11") // a1*b1 = d1:d0
496 PCLMULQDQ(xmm0_xmm3, "0x10") // a0*b1 = e1:e0
497 PCLMULQDQ(xmm0_xmm4, "0x01") // a1*b0 = f1:f0
498 "pxor %%xmm3, %%xmm4 \n\t" // e1+f1:e0+f0
499 "movdqa %%xmm4, %%xmm3 \n\t" // same
500 "psrldq $8, %%xmm4 \n\t" // 0:e1+f1
501 "pslldq $8, %%xmm3 \n\t" // e0+f0:0
502 "pxor %%xmm4, %%xmm2 \n\t" // d1:d0+e1+f1
503 "pxor %%xmm3, %%xmm1 \n\t" // c1+e0+f1:c0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100504
505 /*
506 * Now shift the result one bit to the left,
Gilles Peskine6055b782023-03-10 22:21:47 +0100507 * taking advantage of [CLMUL-WP] eq 27 (p. 18)
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100508 */
Gilles Peskine2808a602023-03-15 19:36:03 +0100509 "movdqa %%xmm1, %%xmm3 \n\t" // r1:r0
510 "movdqa %%xmm2, %%xmm4 \n\t" // r3:r2
511 "psllq $1, %%xmm1 \n\t" // r1<<1:r0<<1
512 "psllq $1, %%xmm2 \n\t" // r3<<1:r2<<1
513 "psrlq $63, %%xmm3 \n\t" // r1>>63:r0>>63
514 "psrlq $63, %%xmm4 \n\t" // r3>>63:r2>>63
515 "movdqa %%xmm3, %%xmm5 \n\t" // r1>>63:r0>>63
516 "pslldq $8, %%xmm3 \n\t" // r0>>63:0
517 "pslldq $8, %%xmm4 \n\t" // r2>>63:0
518 "psrldq $8, %%xmm5 \n\t" // 0:r1>>63
519 "por %%xmm3, %%xmm1 \n\t" // r1<<1|r0>>63:r0<<1
520 "por %%xmm4, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1
521 "por %%xmm5, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1|r1>>63
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100522
523 /*
524 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
Gilles Peskine6055b782023-03-10 22:21:47 +0100525 * using [CLMUL-WP] algorithm 5 (p. 18).
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100526 * Currently xmm2:xmm1 holds x3:x2:x1:x0 (already shifted).
527 */
528 /* Step 2 (1) */
Gilles Peskine2808a602023-03-15 19:36:03 +0100529 "movdqa %%xmm1, %%xmm3 \n\t" // x1:x0
530 "movdqa %%xmm1, %%xmm4 \n\t" // same
531 "movdqa %%xmm1, %%xmm5 \n\t" // same
532 "psllq $63, %%xmm3 \n\t" // x1<<63:x0<<63 = stuff:a
533 "psllq $62, %%xmm4 \n\t" // x1<<62:x0<<62 = stuff:b
534 "psllq $57, %%xmm5 \n\t" // x1<<57:x0<<57 = stuff:c
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100535
536 /* Step 2 (2) */
Gilles Peskine2808a602023-03-15 19:36:03 +0100537 "pxor %%xmm4, %%xmm3 \n\t" // stuff:a+b
538 "pxor %%xmm5, %%xmm3 \n\t" // stuff:a+b+c
539 "pslldq $8, %%xmm3 \n\t" // a+b+c:0
540 "pxor %%xmm3, %%xmm1 \n\t" // x1+a+b+c:x0 = d:x0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100541
542 /* Steps 3 and 4 */
Gilles Peskine2808a602023-03-15 19:36:03 +0100543 "movdqa %%xmm1,%%xmm0 \n\t" // d:x0
544 "movdqa %%xmm1,%%xmm4 \n\t" // same
545 "movdqa %%xmm1,%%xmm5 \n\t" // same
546 "psrlq $1, %%xmm0 \n\t" // e1:x0>>1 = e1:e0'
547 "psrlq $2, %%xmm4 \n\t" // f1:x0>>2 = f1:f0'
548 "psrlq $7, %%xmm5 \n\t" // g1:x0>>7 = g1:g0'
549 "pxor %%xmm4, %%xmm0 \n\t" // e1+f1:e0'+f0'
550 "pxor %%xmm5, %%xmm0 \n\t" // e1+f1+g1:e0'+f0'+g0'
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200551 // e0'+f0'+g0' is almost e0+f0+g0, ex\tcept for some missing
552 // bits carried from d. Now get those\t bits back in.
Gilles Peskine2808a602023-03-15 19:36:03 +0100553 "movdqa %%xmm1,%%xmm3 \n\t" // d:x0
554 "movdqa %%xmm1,%%xmm4 \n\t" // same
555 "movdqa %%xmm1,%%xmm5 \n\t" // same
556 "psllq $63, %%xmm3 \n\t" // d<<63:stuff
557 "psllq $62, %%xmm4 \n\t" // d<<62:stuff
558 "psllq $57, %%xmm5 \n\t" // d<<57:stuff
559 "pxor %%xmm4, %%xmm3 \n\t" // d<<63+d<<62:stuff
560 "pxor %%xmm5, %%xmm3 \n\t" // missing bits of d:stuff
561 "psrldq $8, %%xmm3 \n\t" // 0:missing bits of d
562 "pxor %%xmm3, %%xmm0 \n\t" // e1+f1+g1:e0+f0+g0
563 "pxor %%xmm1, %%xmm0 \n\t" // h1:h0
564 "pxor %%xmm2, %%xmm0 \n\t" // x3+h1:x2+h0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100565
Gilles Peskine2808a602023-03-15 19:36:03 +0100566 "movdqu %%xmm0, (%2) \n\t" // done
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100567 :
568 : "r" (aa), "r" (bb), "r" (cc)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100569 : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100570
571 /* Now byte-reverse the outputs */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100572 for (i = 0; i < 16; i++) {
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100573 c[i] = cc[15 - i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100574 }
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100575
Paul Bakkere7f51332013-12-30 15:32:02 +0100576 return;
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100577}
578
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100579/*
580 * Compute decryption round keys from encryption round keys
581 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100582void mbedtls_aesni_inverse_key(unsigned char *invkey,
583 const unsigned char *fwdkey, int nr)
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100584{
585 unsigned char *ik = invkey;
586 const unsigned char *fk = fwdkey + 16 * nr;
587
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100588 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100589
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100590 for (fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16) {
591 asm ("movdqu (%0), %%xmm0 \n\t"
Gilles Peskine2808a602023-03-15 19:36:03 +0100592 AESIMC(xmm0_xmm0)
593 "movdqu %%xmm0, (%1) \n\t"
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100594 :
595 : "r" (fk), "r" (ik)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100596 : "memory", "xmm0");
597 }
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100598
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100599 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100600}
601
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100602/*
603 * Key expansion, 128-bit case
604 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100605static void aesni_setkey_enc_128(unsigned char *rk,
606 const unsigned char *key)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100607{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100608 asm ("movdqu (%1), %%xmm0 \n\t" // copy the original key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200609 "movdqu %%xmm0, (%0) \n\t" // as round key 0
610 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100611
612 /*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100613 * Finish generating the next round key.
614 *
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100615 * On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff
616 * with X = rot( sub( r3 ) ) ^ RCON.
617 *
618 * On exit, xmm0 is r7:r6:r5:r4
619 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
620 * and those are written to the round key buffer.
621 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200622 "1: \n\t"
623 "pshufd $0xff, %%xmm1, %%xmm1 \n\t" // X:X:X:X
624 "pxor %%xmm0, %%xmm1 \n\t" // X+r3:X+r2:X+r1:r4
625 "pslldq $4, %%xmm0 \n\t" // r2:r1:r0:0
626 "pxor %%xmm0, %%xmm1 \n\t" // X+r3+r2:X+r2+r1:r5:r4
627 "pslldq $4, %%xmm0 \n\t" // etc
628 "pxor %%xmm0, %%xmm1 \n\t"
629 "pslldq $4, %%xmm0 \n\t"
630 "pxor %%xmm1, %%xmm0 \n\t" // update xmm0 for next time!
631 "add $16, %0 \n\t" // point to next round key
632 "movdqu %%xmm0, (%0) \n\t" // write it
633 "ret \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100634
635 /* Main "loop" */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200636 "2: \n\t"
Gilles Peskine2808a602023-03-15 19:36:03 +0100637 AESKEYGENA(xmm0_xmm1, "0x01") "call 1b \n\t"
638 AESKEYGENA(xmm0_xmm1, "0x02") "call 1b \n\t"
639 AESKEYGENA(xmm0_xmm1, "0x04") "call 1b \n\t"
640 AESKEYGENA(xmm0_xmm1, "0x08") "call 1b \n\t"
641 AESKEYGENA(xmm0_xmm1, "0x10") "call 1b \n\t"
642 AESKEYGENA(xmm0_xmm1, "0x20") "call 1b \n\t"
643 AESKEYGENA(xmm0_xmm1, "0x40") "call 1b \n\t"
644 AESKEYGENA(xmm0_xmm1, "0x80") "call 1b \n\t"
645 AESKEYGENA(xmm0_xmm1, "0x1B") "call 1b \n\t"
646 AESKEYGENA(xmm0_xmm1, "0x36") "call 1b \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100647 :
648 : "r" (rk), "r" (key)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100649 : "memory", "cc", "0");
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100650}
651
652/*
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100653 * Key expansion, 192-bit case
654 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100655static void aesni_setkey_enc_192(unsigned char *rk,
656 const unsigned char *key)
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100657{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100658 asm ("movdqu (%1), %%xmm0 \n\t" // copy original round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200659 "movdqu %%xmm0, (%0) \n\t"
660 "add $16, %0 \n\t"
661 "movq 16(%1), %%xmm1 \n\t"
662 "movq %%xmm1, (%0) \n\t"
663 "add $8, %0 \n\t"
664 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100665
666 /*
667 * Finish generating the next 6 quarter-keys.
668 *
669 * On entry xmm0 is r3:r2:r1:r0, xmm1 is stuff:stuff:r5:r4
670 * and xmm2 is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON.
671 *
672 * On exit, xmm0 is r9:r8:r7:r6 and xmm1 is stuff:stuff:r11:r10
673 * and those are written to the round key buffer.
674 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200675 "1: \n\t"
676 "pshufd $0x55, %%xmm2, %%xmm2 \n\t" // X:X:X:X
677 "pxor %%xmm0, %%xmm2 \n\t" // X+r3:X+r2:X+r1:r4
678 "pslldq $4, %%xmm0 \n\t" // etc
679 "pxor %%xmm0, %%xmm2 \n\t"
680 "pslldq $4, %%xmm0 \n\t"
681 "pxor %%xmm0, %%xmm2 \n\t"
682 "pslldq $4, %%xmm0 \n\t"
683 "pxor %%xmm2, %%xmm0 \n\t" // update xmm0 = r9:r8:r7:r6
684 "movdqu %%xmm0, (%0) \n\t"
685 "add $16, %0 \n\t"
686 "pshufd $0xff, %%xmm0, %%xmm2 \n\t" // r9:r9:r9:r9
687 "pxor %%xmm1, %%xmm2 \n\t" // stuff:stuff:r9+r5:r10
688 "pslldq $4, %%xmm1 \n\t" // r2:r1:r0:0
689 "pxor %%xmm2, %%xmm1 \n\t" // xmm1 = stuff:stuff:r11:r10
690 "movq %%xmm1, (%0) \n\t"
691 "add $8, %0 \n\t"
692 "ret \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100693
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200694 "2: \n\t"
Gilles Peskine2808a602023-03-15 19:36:03 +0100695 AESKEYGENA(xmm1_xmm2, "0x01") "call 1b \n\t"
696 AESKEYGENA(xmm1_xmm2, "0x02") "call 1b \n\t"
697 AESKEYGENA(xmm1_xmm2, "0x04") "call 1b \n\t"
698 AESKEYGENA(xmm1_xmm2, "0x08") "call 1b \n\t"
699 AESKEYGENA(xmm1_xmm2, "0x10") "call 1b \n\t"
700 AESKEYGENA(xmm1_xmm2, "0x20") "call 1b \n\t"
701 AESKEYGENA(xmm1_xmm2, "0x40") "call 1b \n\t"
702 AESKEYGENA(xmm1_xmm2, "0x80") "call 1b \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100703
704 :
705 : "r" (rk), "r" (key)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100706 : "memory", "cc", "0");
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100707}
708
709/*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100710 * Key expansion, 256-bit case
711 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100712static void aesni_setkey_enc_256(unsigned char *rk,
713 const unsigned char *key)
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100714{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100715 asm ("movdqu (%1), %%xmm0 \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200716 "movdqu %%xmm0, (%0) \n\t"
717 "add $16, %0 \n\t"
718 "movdqu 16(%1), %%xmm1 \n\t"
719 "movdqu %%xmm1, (%0) \n\t"
720 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100721
722 /*
723 * Finish generating the next two round keys.
724 *
725 * On entry xmm0 is r3:r2:r1:r0, xmm1 is r7:r6:r5:r4 and
726 * xmm2 is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
727 *
728 * On exit, xmm0 is r11:r10:r9:r8 and xmm1 is r15:r14:r13:r12
729 * and those have been written to the output buffer.
730 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200731 "1: \n\t"
732 "pshufd $0xff, %%xmm2, %%xmm2 \n\t"
733 "pxor %%xmm0, %%xmm2 \n\t"
734 "pslldq $4, %%xmm0 \n\t"
735 "pxor %%xmm0, %%xmm2 \n\t"
736 "pslldq $4, %%xmm0 \n\t"
737 "pxor %%xmm0, %%xmm2 \n\t"
738 "pslldq $4, %%xmm0 \n\t"
739 "pxor %%xmm2, %%xmm0 \n\t"
740 "add $16, %0 \n\t"
741 "movdqu %%xmm0, (%0) \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100742
743 /* Set xmm2 to stuff:Y:stuff:stuff with Y = subword( r11 )
744 * and proceed to generate next round key from there */
Gilles Peskine2808a602023-03-15 19:36:03 +0100745 AESKEYGENA(xmm0_xmm2, "0x00")
746 "pshufd $0xaa, %%xmm2, %%xmm2 \n\t"
747 "pxor %%xmm1, %%xmm2 \n\t"
748 "pslldq $4, %%xmm1 \n\t"
749 "pxor %%xmm1, %%xmm2 \n\t"
750 "pslldq $4, %%xmm1 \n\t"
751 "pxor %%xmm1, %%xmm2 \n\t"
752 "pslldq $4, %%xmm1 \n\t"
753 "pxor %%xmm2, %%xmm1 \n\t"
754 "add $16, %0 \n\t"
755 "movdqu %%xmm1, (%0) \n\t"
756 "ret \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100757
758 /*
759 * Main "loop" - Generating one more key than necessary,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 * see definition of mbedtls_aes_context.buf
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100761 */
Gilles Peskine2808a602023-03-15 19:36:03 +0100762 "2: \n\t"
763 AESKEYGENA(xmm1_xmm2, "0x01") "call 1b \n\t"
764 AESKEYGENA(xmm1_xmm2, "0x02") "call 1b \n\t"
765 AESKEYGENA(xmm1_xmm2, "0x04") "call 1b \n\t"
766 AESKEYGENA(xmm1_xmm2, "0x08") "call 1b \n\t"
767 AESKEYGENA(xmm1_xmm2, "0x10") "call 1b \n\t"
768 AESKEYGENA(xmm1_xmm2, "0x20") "call 1b \n\t"
769 AESKEYGENA(xmm1_xmm2, "0x40") "call 1b \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100770 :
771 : "r" (rk), "r" (key)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100772 : "memory", "cc", "0");
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100773}
774
Gilles Peskine6dec5412023-03-16 17:21:33 +0100775#endif /* MBEDTLS_AESNI_HAVE_CODE */
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100776
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100777/*
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100778 * Key expansion, wrapper
779 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100780int mbedtls_aesni_setkey_enc(unsigned char *rk,
781 const unsigned char *key,
782 size_t bits)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100783{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100784 switch (bits) {
785 case 128: aesni_setkey_enc_128(rk, key); break;
786 case 192: aesni_setkey_enc_192(rk, key); break;
787 case 256: aesni_setkey_enc_256(rk, key); break;
788 default: return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100789 }
790
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100791 return 0;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100792}
793
Gilles Peskine6dec5412023-03-16 17:21:33 +0100794#endif /* MBEDTLS_AESNI_HAVE_CODE */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796#endif /* MBEDTLS_AESNI_C */