blob: 47795ea261a8e44c233b2aa9049e78fe7c1de35d [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 Peskinee7dc21f2023-03-10 22:37:11 +010039#if defined(MBEDTLS_HAVE_AESNI_INTRINSICS) || defined(MBEDTLS_HAVE_X86_64)
40
41#if defined(MBEDTLS_HAVE_AESNI_INTRINSICS)
Tom Cosgrove790756d2023-03-13 15:32:52 +000042#if !defined(_WIN32)
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010043#include <cpuid.h>
Tom Cosgrove790756d2023-03-13 15:32:52 +000044#endif
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010045#include <immintrin.h>
46#endif
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010047
48/*
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010049 * AES-NI support detection routine
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010050 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010051int mbedtls_aesni_has_support(unsigned int what)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010052{
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010053 static int done = 0;
54 static unsigned int c = 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010055
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010056 if (!done) {
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010057#if defined(MBEDTLS_HAVE_AESNI_INTRINSICS)
58 static unsigned info[4] = { 0, 0, 0, 0 };
59#if defined(_MSC_VER)
60 __cpuid(info, 1);
61#else
62 __cpuid(1, info[0], info[1], info[2], info[3]);
63#endif
64 c = info[2];
65#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010066 asm ("movl $1, %%eax \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +020067 "cpuid \n\t"
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010068 : "=c" (c)
69 :
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010070 : "eax", "ebx", "edx");
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010071#endif
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010072 done = 1;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010073 }
74
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010075 return (c & what) != 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010076}
77
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010078#if defined(MBEDTLS_HAVE_AESNI_INTRINSICS)
79
80/*
81 * AES-NI AES-ECB block en(de)cryption
82 */
83int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
84 int mode,
85 const unsigned char input[16],
86 unsigned char output[16])
87{
88 const __m128i *rk = (const __m128i *) (ctx->rk);
89 unsigned nr = ctx->nr; // Number of remaining rounds
Tom Cosgrove790756d2023-03-13 15:32:52 +000090
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010091 // Load round key 0
Gilles Peskined4a23932023-03-15 20:37:57 +010092 __m128i state;
93 memcpy(&state, input, 16);
94 state = _mm_xor_si128(state, rk[0]); // state ^= *rk;
Gilles Peskinee7dc21f2023-03-10 22:37:11 +010095 ++rk;
96 --nr;
97
98 if (mode == 0) {
99 while (nr != 0) {
Gilles Peskined4a23932023-03-15 20:37:57 +0100100 state = _mm_aesdec_si128(state, *rk);
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100101 ++rk;
102 --nr;
103 }
Gilles Peskined4a23932023-03-15 20:37:57 +0100104 state = _mm_aesdeclast_si128(state, *rk);
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100105 } else {
106 while (nr != 0) {
Gilles Peskined4a23932023-03-15 20:37:57 +0100107 state = _mm_aesenc_si128(state, *rk);
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100108 ++rk;
109 --nr;
110 }
Gilles Peskined4a23932023-03-15 20:37:57 +0100111 state = _mm_aesenclast_si128(state, *rk);
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100112 }
113
Gilles Peskined4a23932023-03-15 20:37:57 +0100114 memcpy(output, &state, 16);
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100115 return 0;
116}
117
118/*
119 * GCM multiplication: c = a times b in GF(2^128)
120 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
121 */
122
123static void gcm_clmul(const __m128i aa, const __m128i bb,
124 __m128i *cc, __m128i *dd)
125{
126 /*
127 * Caryless multiplication dd:cc = aa * bb
128 * using [CLMUL-WP] algorithm 1 (p. 12).
129 */
130 *cc = _mm_clmulepi64_si128(aa, bb, 0x00); // a0*b0 = c1:c0
131 *dd = _mm_clmulepi64_si128(aa, bb, 0x11); // a1*b1 = d1:d0
132 __m128i ee = _mm_clmulepi64_si128(aa, bb, 0x10); // a0*b1 = e1:e0
133 __m128i ff = _mm_clmulepi64_si128(aa, bb, 0x01); // a1*b0 = f1:f0
Tom Cosgrove790756d2023-03-13 15:32:52 +0000134 ff = _mm_xor_si128(ff, ee); // e1+f1:e0+f0
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100135 ee = ff; // e1+f1:e0+f0
136 ff = _mm_srli_si128(ff, 8); // 0:e1+f1
137 ee = _mm_slli_si128(ee, 8); // e0+f0:0
Tom Cosgrove790756d2023-03-13 15:32:52 +0000138 *dd = _mm_xor_si128(*dd, ff); // d1:d0+e1+f1
139 *cc = _mm_xor_si128(*cc, ee); // c1+e0+f1:c0
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100140}
141
142static void gcm_shift(__m128i *cc, __m128i *dd)
143{
Gilles Peskined4a23932023-03-15 20:37:57 +0100144 /* [CMUCL-WP] Algorithm 5 Step 1: shift cc:dd one bit to the left,
145 * taking advantage of [CLMUL-WP] eq 27 (p. 18). */
146 // // *cc = r1:r0
147 // // *dd = r3:r2
148 __m128i cc_lo = _mm_slli_epi64(*cc, 1); // r1<<1:r0<<1
149 __m128i dd_lo = _mm_slli_epi64(*dd, 1); // r3<<1:r2<<1
150 __m128i cc_hi = _mm_srli_epi64(*cc, 63); // r1>>63:r0>>63
151 __m128i dd_hi = _mm_srli_epi64(*dd, 63); // r3>>63:r2>>63
152 __m128i xmm5 = _mm_srli_si128(cc_hi, 8); // 0:r1>>63
153 cc_hi = _mm_slli_si128(cc_hi, 8); // r0>>63:0
154 dd_hi = _mm_slli_si128(dd_hi, 8); // 0:r1>>63
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100155
Gilles Peskined4a23932023-03-15 20:37:57 +0100156 *cc = _mm_or_si128(cc_lo, cc_hi); // r1<<1|r0>>63:r0<<1
157 *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 +0100158}
159
Gilles Peskined4a23932023-03-15 20:37:57 +0100160static __m128i gcm_reduce(__m128i xx)
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100161{
162 // // xx = x1:x0
163 /* [CLMUL-WP] Algorithm 5 Step 2 */
164 __m128i aa = _mm_slli_epi64(xx, 63); // x1<<63:x0<<63 = stuff:a
165 __m128i bb = _mm_slli_epi64(xx, 62); // x1<<62:x0<<62 = stuff:b
166 __m128i cc = _mm_slli_epi64(xx, 57); // x1<<57:x0<<57 = stuff:c
Tom Cosgrove790756d2023-03-13 15:32:52 +0000167 __m128i dd = _mm_slli_si128(_mm_xor_si128(_mm_xor_si128(aa, bb), cc), 8); // a+b+c:0
168 return _mm_xor_si128(dd, xx); // x1+a+b+c:x0 = d:x0
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100169}
170
Gilles Peskined4a23932023-03-15 20:37:57 +0100171static __m128i gcm_mix(__m128i dx)
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100172{
173 /* [CLMUL-WP] Algorithm 5 Steps 3 and 4 */
174 __m128i ee = _mm_srli_epi64(dx, 1); // e1:x0>>1 = e1:e0'
175 __m128i ff = _mm_srli_epi64(dx, 2); // f1:x0>>2 = f1:f0'
176 __m128i gg = _mm_srli_epi64(dx, 7); // g1:x0>>7 = g1:g0'
177
178 // e0'+f0'+g0' is almost e0+f0+g0, except for some missing
179 // bits carried from d. Now get those bits back in.
180 __m128i eh = _mm_slli_epi64(dx, 63); // d<<63:stuff
181 __m128i fh = _mm_slli_epi64(dx, 62); // d<<62:stuff
182 __m128i gh = _mm_slli_epi64(dx, 57); // d<<57:stuff
Tom Cosgrove790756d2023-03-13 15:32:52 +0000183 __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 +0100184
Tom Cosgrove790756d2023-03-13 15:32:52 +0000185 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 +0100186}
187
188void mbedtls_aesni_gcm_mult(unsigned char c[16],
189 const unsigned char a[16],
190 const unsigned char b[16])
191{
192 __m128i aa, bb, cc, dd;
193
194 /* The inputs are in big-endian order, so byte-reverse them */
195 for (size_t i = 0; i < 16; i++) {
196 ((uint8_t *) &aa)[i] = a[15 - i];
197 ((uint8_t *) &bb)[i] = b[15 - i];
198 }
199
200 gcm_clmul(aa, bb, &cc, &dd);
201 gcm_shift(&cc, &dd);
202 /*
203 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
204 * using [CLMUL-WP] algorithm 5 (p. 18).
205 * Currently dd:cc holds x3:x2:x1:x0 (already shifted).
206 */
Gilles Peskined4a23932023-03-15 20:37:57 +0100207 __m128i dx = gcm_reduce(cc);
208 __m128i xh = gcm_mix(dx);
Tom Cosgrove790756d2023-03-13 15:32:52 +0000209 cc = _mm_xor_si128(xh, dd); // x3+h1:x2+h0
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100210
211 /* Now byte-reverse the outputs */
212 for (size_t i = 0; i < 16; i++) {
213 c[i] = ((uint8_t *) &cc)[15 - i];
214 }
215
216 return;
217}
218
219/*
220 * Compute decryption round keys from encryption round keys
221 */
222void mbedtls_aesni_inverse_key(unsigned char *invkey,
223 const unsigned char *fwdkey, int nr)
224{
225 __m128i *ik = (__m128i *) invkey;
226 const __m128i *fk = (const __m128i *) fwdkey + nr;
227
228 *ik = *fk;
229 for (--fk, ++ik; fk > (const __m128i *) fwdkey; --fk, ++ik) {
230 *ik = _mm_aesimc_si128(*fk);
231 }
232 *ik = *fk;
233}
234
235/*
236 * Key expansion, 128-bit case
237 */
Gilles Peskined4a23932023-03-15 20:37:57 +0100238static __m128i aesni_set_rk_128(__m128i state, __m128i xword)
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100239{
240 /*
241 * Finish generating the next round key.
242 *
Gilles Peskined4a23932023-03-15 20:37:57 +0100243 * On entry state is r3:r2:r1:r0 and xword is X:stuff:stuff:stuff
244 * with X = rot( sub( r3 ) ) ^ RCON (obtained with AESKEYGENASSIST).
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100245 *
Gilles Peskined4a23932023-03-15 20:37:57 +0100246 * On exit, xword is r7:r6:r5:r4
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100247 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
248 * and this is returned, to be written to the round key buffer.
249 */
Gilles Peskined4a23932023-03-15 20:37:57 +0100250 xword = _mm_shuffle_epi32(xword, 0xff); // X:X:X:X
251 xword = _mm_xor_si128(xword, state); // X+r3:X+r2:X+r1:r4
252 state = _mm_slli_si128(state, 4); // r2:r1:r0:0
253 xword = _mm_xor_si128(xword, state); // X+r3+r2:X+r2+r1:r5:r4
254 state = _mm_slli_si128(state, 4); // r1:r0:0:0
255 xword = _mm_xor_si128(xword, state); // X+r3+r2+r1:r6:r5:r4
256 state = _mm_slli_si128(state, 4); // r0:0:0:0
257 state = _mm_xor_si128(xword, state); // r7:r6:r5:r4
258 return state;
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100259}
260
261static void aesni_setkey_enc_128(unsigned char *rk_bytes,
262 const unsigned char *key)
263{
264 __m128i *rk = (__m128i *) rk_bytes;
265
266 memcpy(&rk[0], key, 16);
267 rk[1] = aesni_set_rk_128(rk[0], _mm_aeskeygenassist_si128(rk[0], 0x01));
268 rk[2] = aesni_set_rk_128(rk[1], _mm_aeskeygenassist_si128(rk[1], 0x02));
269 rk[3] = aesni_set_rk_128(rk[2], _mm_aeskeygenassist_si128(rk[2], 0x04));
270 rk[4] = aesni_set_rk_128(rk[3], _mm_aeskeygenassist_si128(rk[3], 0x08));
271 rk[5] = aesni_set_rk_128(rk[4], _mm_aeskeygenassist_si128(rk[4], 0x10));
272 rk[6] = aesni_set_rk_128(rk[5], _mm_aeskeygenassist_si128(rk[5], 0x20));
273 rk[7] = aesni_set_rk_128(rk[6], _mm_aeskeygenassist_si128(rk[6], 0x40));
274 rk[8] = aesni_set_rk_128(rk[7], _mm_aeskeygenassist_si128(rk[7], 0x80));
275 rk[9] = aesni_set_rk_128(rk[8], _mm_aeskeygenassist_si128(rk[8], 0x1B));
276 rk[10] = aesni_set_rk_128(rk[9], _mm_aeskeygenassist_si128(rk[9], 0x36));
277}
278
279/*
280 * Key expansion, 192-bit case
281 */
Gilles Peskined4a23932023-03-15 20:37:57 +0100282static void aesni_set_rk_192(__m128i *state0, __m128i *state1, __m128i xword,
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100283 unsigned char *rk)
284{
285 /*
286 * Finish generating the next 6 quarter-keys.
287 *
Gilles Peskined4a23932023-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 Peskinee7dc21f2023-03-10 22:37:11 +0100291 *
Gilles Peskined4a23932023-03-15 20:37:57 +0100292 * On exit, state0 is r9:r8:r7:r6 and state1 is stuff:stuff:r11:r10
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100293 * and those are written to the round key buffer.
294 */
Gilles Peskined4a23932023-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 Peskinee7dc21f2023-03-10 22:37:11 +0100304
Gilles Peskined4a23932023-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 Peskinee7dc21f2023-03-10 22:37:11 +0100310
Gilles Peskined4a23932023-03-15 20:37:57 +0100311 /* Store state0 and the low half of state1 into rk, which is conceptually
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100312 * an array of 24-byte elements. Since 24 is not a multiple of 16,
Gilles Peskined4a23932023-03-15 20:37:57 +0100313 * rk is not necessarily aligned so just `*rk = *state0` doesn't work. */
314 memcpy(rk, state0, 16);
Gilles Peskine2e8d8d12023-03-15 23:16:27 +0100315 memcpy(rk + 16, state1, 8);
Gilles Peskinee7dc21f2023-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 Peskined4a23932023-03-15 20:37:57 +0100324 __m128i state0 = ((__m128i *) rk)[0];
325 __m128i state1 = _mm_loadl_epi64(((__m128i *) rk) + 1);
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100326
Gilles Peskined4a23932023-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 Peskinee7dc21f2023-03-10 22:37:11 +0100335}
336
337/*
338 * Key expansion, 256-bit case
339 */
Gilles Peskined4a23932023-03-15 20:37:57 +0100340static void aesni_set_rk_256(__m128i state0, __m128i state1, __m128i xword,
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100341 __m128i *rk0, __m128i *rk1)
342{
343 /*
344 * Finish generating the next two round keys.
345 *
Gilles Peskined4a23932023-03-15 20:37:57 +0100346 * On entry state0 is r3:r2:r1:r0, state1 is r7:r6:r5:r4 and
347 * xword is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
348 * (obtained with AESKEYGENASSIST).
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100349 *
350 * On exit, *rk0 is r11:r10:r9:r8 and *rk1 is r15:r14:r13:r12
351 */
Gilles Peskined4a23932023-03-15 20:37:57 +0100352 xword = _mm_shuffle_epi32(xword, 0xff);
353 xword = _mm_xor_si128(xword, state0);
354 state0 = _mm_slli_si128(state0, 4);
355 xword = _mm_xor_si128(xword, state0);
356 state0 = _mm_slli_si128(state0, 4);
357 xword = _mm_xor_si128(xword, state0);
358 state0 = _mm_slli_si128(state0, 4);
359 state0 = _mm_xor_si128(state0, xword);
360 *rk0 = state0;
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100361
Gilles Peskined4a23932023-03-15 20:37:57 +0100362 /* Set xword to stuff:Y:stuff:stuff with Y = subword( r11 )
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100363 * and proceed to generate next round key from there */
Gilles Peskined4a23932023-03-15 20:37:57 +0100364 xword = _mm_aeskeygenassist_si128(state0, 0x00);
365 xword = _mm_shuffle_epi32(xword, 0xaa);
366 xword = _mm_xor_si128(xword, state1);
367 state1 = _mm_slli_si128(state1, 4);
368 xword = _mm_xor_si128(xword, state1);
369 state1 = _mm_slli_si128(state1, 4);
370 xword = _mm_xor_si128(xword, state1);
371 state1 = _mm_slli_si128(state1, 4);
372 state1 = _mm_xor_si128(state1, xword);
373 *rk1 = state1;
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100374}
375
376static void aesni_setkey_enc_256(unsigned char *rk_bytes,
377 const unsigned char *key)
378{
379 __m128i *rk = (__m128i *) rk_bytes;
380
381 memcpy(&rk[0], key, 16);
382 memcpy(&rk[1], key + 16, 16);
383
384 /*
385 * Main "loop" - Generating one more key than necessary,
386 * see definition of mbedtls_aes_context.buf
387 */
388 aesni_set_rk_256(rk[0], rk[1], _mm_aeskeygenassist_si128(rk[1], 0x01), &rk[2], &rk[3]);
389 aesni_set_rk_256(rk[2], rk[3], _mm_aeskeygenassist_si128(rk[3], 0x02), &rk[4], &rk[5]);
390 aesni_set_rk_256(rk[4], rk[5], _mm_aeskeygenassist_si128(rk[5], 0x04), &rk[6], &rk[7]);
391 aesni_set_rk_256(rk[6], rk[7], _mm_aeskeygenassist_si128(rk[7], 0x08), &rk[8], &rk[9]);
392 aesni_set_rk_256(rk[8], rk[9], _mm_aeskeygenassist_si128(rk[9], 0x10), &rk[10], &rk[11]);
393 aesni_set_rk_256(rk[10], rk[11], _mm_aeskeygenassist_si128(rk[11], 0x20), &rk[12], &rk[13]);
394 aesni_set_rk_256(rk[12], rk[13], _mm_aeskeygenassist_si128(rk[13], 0x40), &rk[14], &rk[15]);
395}
396
397#else /* MBEDTLS_HAVE_AESNI_INTRINSICS */
398
Gilles Peskine18d521a2023-03-10 22:25:13 +0100399#if defined(__has_feature)
400#if __has_feature(memory_sanitizer)
401#warning \
402 "MBEDTLS_AESNI_C is known to cause spurious error reports with some memory sanitizers as they do not understand the assembly code."
403#endif
404#endif
405
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100406/*
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +0200407 * Binutils needs to be at least 2.19 to support AES-NI instructions.
408 * Unfortunately, a lot of users have a lower version now (2014-04).
409 * Emit bytecode directly in order to support "old" version of gas.
410 *
411 * Opcodes from the Intel architecture reference manual, vol. 3.
412 * We always use registers, so we don't need prefixes for memory operands.
413 * Operand macros are in gas order (src, dst) as opposed to Intel order
414 * (dst, src) in order to blend better into the surrounding assembly code.
415 */
Gilles Peskine2808a602023-03-15 19:36:03 +0100416#define AESDEC(regs) ".byte 0x66,0x0F,0x38,0xDE," regs "\n\t"
417#define AESDECLAST(regs) ".byte 0x66,0x0F,0x38,0xDF," regs "\n\t"
418#define AESENC(regs) ".byte 0x66,0x0F,0x38,0xDC," regs "\n\t"
419#define AESENCLAST(regs) ".byte 0x66,0x0F,0x38,0xDD," regs "\n\t"
420#define AESIMC(regs) ".byte 0x66,0x0F,0x38,0xDB," regs "\n\t"
421#define AESKEYGENA(regs, imm) ".byte 0x66,0x0F,0x3A,0xDF," regs "," imm "\n\t"
422#define PCLMULQDQ(regs, imm) ".byte 0x66,0x0F,0x3A,0x44," regs "," imm "\n\t"
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +0200423
424#define xmm0_xmm0 "0xC0"
425#define xmm0_xmm1 "0xC8"
426#define xmm0_xmm2 "0xD0"
427#define xmm0_xmm3 "0xD8"
428#define xmm0_xmm4 "0xE0"
429#define xmm1_xmm0 "0xC1"
430#define xmm1_xmm2 "0xD1"
431
432/*
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100433 * AES-NI AES-ECB block en(de)cryption
434 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100435int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
436 int mode,
437 const unsigned char input[16],
438 unsigned char output[16])
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100439{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100440 asm ("movdqu (%3), %%xmm0 \n\t" // load input
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200441 "movdqu (%1), %%xmm1 \n\t" // load round key 0
442 "pxor %%xmm1, %%xmm0 \n\t" // round 0
James Cowgill6c8edca2015-12-17 01:40:26 +0000443 "add $16, %1 \n\t" // point to next round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200444 "subl $1, %0 \n\t" // normal rounds = nr - 1
445 "test %2, %2 \n\t" // mode?
446 "jz 2f \n\t" // 0 = decrypt
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100447
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200448 "1: \n\t" // encryption loop
449 "movdqu (%1), %%xmm1 \n\t" // load round key
Gilles Peskine2808a602023-03-15 19:36:03 +0100450 AESENC(xmm1_xmm0) // do round
451 "add $16, %1 \n\t" // point to next round key
452 "subl $1, %0 \n\t" // loop
453 "jnz 1b \n\t"
454 "movdqu (%1), %%xmm1 \n\t" // load round key
455 AESENCLAST(xmm1_xmm0) // last round
456 "jmp 3f \n\t"
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100457
Gilles Peskine2808a602023-03-15 19:36:03 +0100458 "2: \n\t" // decryption loop
459 "movdqu (%1), %%xmm1 \n\t"
460 AESDEC(xmm1_xmm0) // do round
461 "add $16, %1 \n\t"
462 "subl $1, %0 \n\t"
463 "jnz 2b \n\t"
464 "movdqu (%1), %%xmm1 \n\t" // load round key
465 AESDECLAST(xmm1_xmm0) // last round
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100466
Gilles Peskine2808a602023-03-15 19:36:03 +0100467 "3: \n\t"
468 "movdqu %%xmm0, (%4) \n\t" // export output
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100469 :
470 : "r" (ctx->nr), "r" (ctx->rk), "r" (mode), "r" (input), "r" (output)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100471 : "memory", "cc", "xmm0", "xmm1");
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100472
473
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100474 return 0;
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100475}
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100476
477/*
478 * GCM multiplication: c = a times b in GF(2^128)
479 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
480 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100481void mbedtls_aesni_gcm_mult(unsigned char c[16],
482 const unsigned char a[16],
483 const unsigned char b[16])
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100484{
485 unsigned char aa[16], bb[16], cc[16];
486 size_t i;
487
488 /* The inputs are in big-endian order, so byte-reverse them */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100489 for (i = 0; i < 16; i++) {
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100490 aa[i] = a[15 - i];
491 bb[i] = b[15 - i];
492 }
493
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100494 asm ("movdqu (%0), %%xmm0 \n\t" // a1:a0
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200495 "movdqu (%1), %%xmm1 \n\t" // b1:b0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100496
497 /*
498 * Caryless multiplication xmm2:xmm1 = xmm0 * xmm1
Gilles Peskine6055b782023-03-10 22:21:47 +0100499 * using [CLMUL-WP] algorithm 1 (p. 12).
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100500 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200501 "movdqa %%xmm1, %%xmm2 \n\t" // copy of b1:b0
502 "movdqa %%xmm1, %%xmm3 \n\t" // same
503 "movdqa %%xmm1, %%xmm4 \n\t" // same
Gilles Peskine2808a602023-03-15 19:36:03 +0100504 PCLMULQDQ(xmm0_xmm1, "0x00") // a0*b0 = c1:c0
505 PCLMULQDQ(xmm0_xmm2, "0x11") // a1*b1 = d1:d0
506 PCLMULQDQ(xmm0_xmm3, "0x10") // a0*b1 = e1:e0
507 PCLMULQDQ(xmm0_xmm4, "0x01") // a1*b0 = f1:f0
508 "pxor %%xmm3, %%xmm4 \n\t" // e1+f1:e0+f0
509 "movdqa %%xmm4, %%xmm3 \n\t" // same
510 "psrldq $8, %%xmm4 \n\t" // 0:e1+f1
511 "pslldq $8, %%xmm3 \n\t" // e0+f0:0
512 "pxor %%xmm4, %%xmm2 \n\t" // d1:d0+e1+f1
513 "pxor %%xmm3, %%xmm1 \n\t" // c1+e0+f1:c0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100514
515 /*
516 * Now shift the result one bit to the left,
Gilles Peskine6055b782023-03-10 22:21:47 +0100517 * taking advantage of [CLMUL-WP] eq 27 (p. 18)
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100518 */
Gilles Peskine2808a602023-03-15 19:36:03 +0100519 "movdqa %%xmm1, %%xmm3 \n\t" // r1:r0
520 "movdqa %%xmm2, %%xmm4 \n\t" // r3:r2
521 "psllq $1, %%xmm1 \n\t" // r1<<1:r0<<1
522 "psllq $1, %%xmm2 \n\t" // r3<<1:r2<<1
523 "psrlq $63, %%xmm3 \n\t" // r1>>63:r0>>63
524 "psrlq $63, %%xmm4 \n\t" // r3>>63:r2>>63
525 "movdqa %%xmm3, %%xmm5 \n\t" // r1>>63:r0>>63
526 "pslldq $8, %%xmm3 \n\t" // r0>>63:0
527 "pslldq $8, %%xmm4 \n\t" // r2>>63:0
528 "psrldq $8, %%xmm5 \n\t" // 0:r1>>63
529 "por %%xmm3, %%xmm1 \n\t" // r1<<1|r0>>63:r0<<1
530 "por %%xmm4, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1
531 "por %%xmm5, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1|r1>>63
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100532
533 /*
534 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
Gilles Peskine6055b782023-03-10 22:21:47 +0100535 * using [CLMUL-WP] algorithm 5 (p. 18).
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100536 * Currently xmm2:xmm1 holds x3:x2:x1:x0 (already shifted).
537 */
538 /* Step 2 (1) */
Gilles Peskine2808a602023-03-15 19:36:03 +0100539 "movdqa %%xmm1, %%xmm3 \n\t" // x1:x0
540 "movdqa %%xmm1, %%xmm4 \n\t" // same
541 "movdqa %%xmm1, %%xmm5 \n\t" // same
542 "psllq $63, %%xmm3 \n\t" // x1<<63:x0<<63 = stuff:a
543 "psllq $62, %%xmm4 \n\t" // x1<<62:x0<<62 = stuff:b
544 "psllq $57, %%xmm5 \n\t" // x1<<57:x0<<57 = stuff:c
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100545
546 /* Step 2 (2) */
Gilles Peskine2808a602023-03-15 19:36:03 +0100547 "pxor %%xmm4, %%xmm3 \n\t" // stuff:a+b
548 "pxor %%xmm5, %%xmm3 \n\t" // stuff:a+b+c
549 "pslldq $8, %%xmm3 \n\t" // a+b+c:0
550 "pxor %%xmm3, %%xmm1 \n\t" // x1+a+b+c:x0 = d:x0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100551
552 /* Steps 3 and 4 */
Gilles Peskine2808a602023-03-15 19:36:03 +0100553 "movdqa %%xmm1,%%xmm0 \n\t" // d:x0
554 "movdqa %%xmm1,%%xmm4 \n\t" // same
555 "movdqa %%xmm1,%%xmm5 \n\t" // same
556 "psrlq $1, %%xmm0 \n\t" // e1:x0>>1 = e1:e0'
557 "psrlq $2, %%xmm4 \n\t" // f1:x0>>2 = f1:f0'
558 "psrlq $7, %%xmm5 \n\t" // g1:x0>>7 = g1:g0'
559 "pxor %%xmm4, %%xmm0 \n\t" // e1+f1:e0'+f0'
560 "pxor %%xmm5, %%xmm0 \n\t" // e1+f1+g1:e0'+f0'+g0'
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200561 // e0'+f0'+g0' is almost e0+f0+g0, ex\tcept for some missing
562 // bits carried from d. Now get those\t bits back in.
Gilles Peskine2808a602023-03-15 19:36:03 +0100563 "movdqa %%xmm1,%%xmm3 \n\t" // d:x0
564 "movdqa %%xmm1,%%xmm4 \n\t" // same
565 "movdqa %%xmm1,%%xmm5 \n\t" // same
566 "psllq $63, %%xmm3 \n\t" // d<<63:stuff
567 "psllq $62, %%xmm4 \n\t" // d<<62:stuff
568 "psllq $57, %%xmm5 \n\t" // d<<57:stuff
569 "pxor %%xmm4, %%xmm3 \n\t" // d<<63+d<<62:stuff
570 "pxor %%xmm5, %%xmm3 \n\t" // missing bits of d:stuff
571 "psrldq $8, %%xmm3 \n\t" // 0:missing bits of d
572 "pxor %%xmm3, %%xmm0 \n\t" // e1+f1+g1:e0+f0+g0
573 "pxor %%xmm1, %%xmm0 \n\t" // h1:h0
574 "pxor %%xmm2, %%xmm0 \n\t" // x3+h1:x2+h0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100575
Gilles Peskine2808a602023-03-15 19:36:03 +0100576 "movdqu %%xmm0, (%2) \n\t" // done
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100577 :
578 : "r" (aa), "r" (bb), "r" (cc)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100579 : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100580
581 /* Now byte-reverse the outputs */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100582 for (i = 0; i < 16; i++) {
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100583 c[i] = cc[15 - i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100584 }
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100585
Paul Bakkere7f51332013-12-30 15:32:02 +0100586 return;
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100587}
588
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100589/*
590 * Compute decryption round keys from encryption round keys
591 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100592void mbedtls_aesni_inverse_key(unsigned char *invkey,
593 const unsigned char *fwdkey, int nr)
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100594{
595 unsigned char *ik = invkey;
596 const unsigned char *fk = fwdkey + 16 * nr;
597
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100598 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100599
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100600 for (fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16) {
601 asm ("movdqu (%0), %%xmm0 \n\t"
Gilles Peskine2808a602023-03-15 19:36:03 +0100602 AESIMC(xmm0_xmm0)
603 "movdqu %%xmm0, (%1) \n\t"
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100604 :
605 : "r" (fk), "r" (ik)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100606 : "memory", "xmm0");
607 }
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100608
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100609 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100610}
611
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100612/*
613 * Key expansion, 128-bit case
614 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100615static void aesni_setkey_enc_128(unsigned char *rk,
616 const unsigned char *key)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100617{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100618 asm ("movdqu (%1), %%xmm0 \n\t" // copy the original key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200619 "movdqu %%xmm0, (%0) \n\t" // as round key 0
620 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100621
622 /*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100623 * Finish generating the next round key.
624 *
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100625 * On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff
626 * with X = rot( sub( r3 ) ) ^ RCON.
627 *
628 * On exit, xmm0 is r7:r6:r5:r4
629 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
630 * and those are written to the round key buffer.
631 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200632 "1: \n\t"
633 "pshufd $0xff, %%xmm1, %%xmm1 \n\t" // X:X:X:X
634 "pxor %%xmm0, %%xmm1 \n\t" // X+r3:X+r2:X+r1:r4
635 "pslldq $4, %%xmm0 \n\t" // r2:r1:r0:0
636 "pxor %%xmm0, %%xmm1 \n\t" // X+r3+r2:X+r2+r1:r5:r4
637 "pslldq $4, %%xmm0 \n\t" // etc
638 "pxor %%xmm0, %%xmm1 \n\t"
639 "pslldq $4, %%xmm0 \n\t"
640 "pxor %%xmm1, %%xmm0 \n\t" // update xmm0 for next time!
641 "add $16, %0 \n\t" // point to next round key
642 "movdqu %%xmm0, (%0) \n\t" // write it
643 "ret \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100644
645 /* Main "loop" */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200646 "2: \n\t"
Gilles Peskine2808a602023-03-15 19:36:03 +0100647 AESKEYGENA(xmm0_xmm1, "0x01") "call 1b \n\t"
648 AESKEYGENA(xmm0_xmm1, "0x02") "call 1b \n\t"
649 AESKEYGENA(xmm0_xmm1, "0x04") "call 1b \n\t"
650 AESKEYGENA(xmm0_xmm1, "0x08") "call 1b \n\t"
651 AESKEYGENA(xmm0_xmm1, "0x10") "call 1b \n\t"
652 AESKEYGENA(xmm0_xmm1, "0x20") "call 1b \n\t"
653 AESKEYGENA(xmm0_xmm1, "0x40") "call 1b \n\t"
654 AESKEYGENA(xmm0_xmm1, "0x80") "call 1b \n\t"
655 AESKEYGENA(xmm0_xmm1, "0x1B") "call 1b \n\t"
656 AESKEYGENA(xmm0_xmm1, "0x36") "call 1b \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100657 :
658 : "r" (rk), "r" (key)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100659 : "memory", "cc", "0");
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100660}
661
662/*
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100663 * Key expansion, 192-bit case
664 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100665static void aesni_setkey_enc_192(unsigned char *rk,
666 const unsigned char *key)
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100667{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100668 asm ("movdqu (%1), %%xmm0 \n\t" // copy original round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200669 "movdqu %%xmm0, (%0) \n\t"
670 "add $16, %0 \n\t"
671 "movq 16(%1), %%xmm1 \n\t"
672 "movq %%xmm1, (%0) \n\t"
673 "add $8, %0 \n\t"
674 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100675
676 /*
677 * Finish generating the next 6 quarter-keys.
678 *
679 * On entry xmm0 is r3:r2:r1:r0, xmm1 is stuff:stuff:r5:r4
680 * and xmm2 is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON.
681 *
682 * On exit, xmm0 is r9:r8:r7:r6 and xmm1 is stuff:stuff:r11:r10
683 * and those are written to the round key buffer.
684 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200685 "1: \n\t"
686 "pshufd $0x55, %%xmm2, %%xmm2 \n\t" // X:X:X:X
687 "pxor %%xmm0, %%xmm2 \n\t" // X+r3:X+r2:X+r1:r4
688 "pslldq $4, %%xmm0 \n\t" // etc
689 "pxor %%xmm0, %%xmm2 \n\t"
690 "pslldq $4, %%xmm0 \n\t"
691 "pxor %%xmm0, %%xmm2 \n\t"
692 "pslldq $4, %%xmm0 \n\t"
693 "pxor %%xmm2, %%xmm0 \n\t" // update xmm0 = r9:r8:r7:r6
694 "movdqu %%xmm0, (%0) \n\t"
695 "add $16, %0 \n\t"
696 "pshufd $0xff, %%xmm0, %%xmm2 \n\t" // r9:r9:r9:r9
697 "pxor %%xmm1, %%xmm2 \n\t" // stuff:stuff:r9+r5:r10
698 "pslldq $4, %%xmm1 \n\t" // r2:r1:r0:0
699 "pxor %%xmm2, %%xmm1 \n\t" // xmm1 = stuff:stuff:r11:r10
700 "movq %%xmm1, (%0) \n\t"
701 "add $8, %0 \n\t"
702 "ret \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100703
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200704 "2: \n\t"
Gilles Peskine2808a602023-03-15 19:36:03 +0100705 AESKEYGENA(xmm1_xmm2, "0x01") "call 1b \n\t"
706 AESKEYGENA(xmm1_xmm2, "0x02") "call 1b \n\t"
707 AESKEYGENA(xmm1_xmm2, "0x04") "call 1b \n\t"
708 AESKEYGENA(xmm1_xmm2, "0x08") "call 1b \n\t"
709 AESKEYGENA(xmm1_xmm2, "0x10") "call 1b \n\t"
710 AESKEYGENA(xmm1_xmm2, "0x20") "call 1b \n\t"
711 AESKEYGENA(xmm1_xmm2, "0x40") "call 1b \n\t"
712 AESKEYGENA(xmm1_xmm2, "0x80") "call 1b \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100713
714 :
715 : "r" (rk), "r" (key)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100716 : "memory", "cc", "0");
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100717}
718
719/*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100720 * Key expansion, 256-bit case
721 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100722static void aesni_setkey_enc_256(unsigned char *rk,
723 const unsigned char *key)
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100724{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100725 asm ("movdqu (%1), %%xmm0 \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200726 "movdqu %%xmm0, (%0) \n\t"
727 "add $16, %0 \n\t"
728 "movdqu 16(%1), %%xmm1 \n\t"
729 "movdqu %%xmm1, (%0) \n\t"
730 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100731
732 /*
733 * Finish generating the next two round keys.
734 *
735 * On entry xmm0 is r3:r2:r1:r0, xmm1 is r7:r6:r5:r4 and
736 * xmm2 is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
737 *
738 * On exit, xmm0 is r11:r10:r9:r8 and xmm1 is r15:r14:r13:r12
739 * and those have been written to the output buffer.
740 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200741 "1: \n\t"
742 "pshufd $0xff, %%xmm2, %%xmm2 \n\t"
743 "pxor %%xmm0, %%xmm2 \n\t"
744 "pslldq $4, %%xmm0 \n\t"
745 "pxor %%xmm0, %%xmm2 \n\t"
746 "pslldq $4, %%xmm0 \n\t"
747 "pxor %%xmm0, %%xmm2 \n\t"
748 "pslldq $4, %%xmm0 \n\t"
749 "pxor %%xmm2, %%xmm0 \n\t"
750 "add $16, %0 \n\t"
751 "movdqu %%xmm0, (%0) \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100752
753 /* Set xmm2 to stuff:Y:stuff:stuff with Y = subword( r11 )
754 * and proceed to generate next round key from there */
Gilles Peskine2808a602023-03-15 19:36:03 +0100755 AESKEYGENA(xmm0_xmm2, "0x00")
756 "pshufd $0xaa, %%xmm2, %%xmm2 \n\t"
757 "pxor %%xmm1, %%xmm2 \n\t"
758 "pslldq $4, %%xmm1 \n\t"
759 "pxor %%xmm1, %%xmm2 \n\t"
760 "pslldq $4, %%xmm1 \n\t"
761 "pxor %%xmm1, %%xmm2 \n\t"
762 "pslldq $4, %%xmm1 \n\t"
763 "pxor %%xmm2, %%xmm1 \n\t"
764 "add $16, %0 \n\t"
765 "movdqu %%xmm1, (%0) \n\t"
766 "ret \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100767
768 /*
769 * Main "loop" - Generating one more key than necessary,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 * see definition of mbedtls_aes_context.buf
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100771 */
Gilles Peskine2808a602023-03-15 19:36:03 +0100772 "2: \n\t"
773 AESKEYGENA(xmm1_xmm2, "0x01") "call 1b \n\t"
774 AESKEYGENA(xmm1_xmm2, "0x02") "call 1b \n\t"
775 AESKEYGENA(xmm1_xmm2, "0x04") "call 1b \n\t"
776 AESKEYGENA(xmm1_xmm2, "0x08") "call 1b \n\t"
777 AESKEYGENA(xmm1_xmm2, "0x10") "call 1b \n\t"
778 AESKEYGENA(xmm1_xmm2, "0x20") "call 1b \n\t"
779 AESKEYGENA(xmm1_xmm2, "0x40") "call 1b \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100780 :
781 : "r" (rk), "r" (key)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100782 : "memory", "cc", "0");
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100783}
784
Gilles Peskinee7dc21f2023-03-10 22:37:11 +0100785#endif /* MBEDTLS_HAVE_AESNI_INTRINSICS */
786
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100787/*
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100788 * Key expansion, wrapper
789 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100790int mbedtls_aesni_setkey_enc(unsigned char *rk,
791 const unsigned char *key,
792 size_t bits)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100793{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100794 switch (bits) {
795 case 128: aesni_setkey_enc_128(rk, key); break;
796 case 192: aesni_setkey_enc_192(rk, key); break;
797 case 256: aesni_setkey_enc_256(rk, key); break;
798 default: return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100799 }
800
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100801 return 0;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100802}
803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804#endif /* MBEDTLS_HAVE_X86_64 */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806#endif /* MBEDTLS_AESNI_C */