blob: d4abb4d6cb482d40267a9d170a5050ee1740ac01 [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/*
21 * [AES-WP] http://software.intel.com/en-us/articles/intel-advanced-encryption-standard-aes-instructions-set
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +010022 * [CLMUL-WP] http://software.intel.com/en-us/articles/intel-carry-less-multiplication-instruction-and-its-usage-for-computing-the-gcm-mode/
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
Gilles Peskine5053efd2018-04-05 15:25:50 +020029#if defined(__has_feature)
30#if __has_feature(memory_sanitizer)
Gilles Peskine449bd832023-01-11 14:50:10 +010031#warning \
32 "MBEDTLS_AESNI_C is known to cause spurious error reports with some memory sanitizers as they do not understand the assembly code."
Gilles Peskine5053efd2018-04-05 15:25:50 +020033#endif
34#endif
35
Chris Jones187782f2021-03-09 17:28:35 +000036#include "aesni.h"
Rich Evans00ab4702015-02-06 13:43:58 +000037
38#include <string.h>
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010039
David Horstmanne3d8f312023-01-03 11:07:09 +000040/* *INDENT-OFF* */
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020041#ifndef asm
42#define asm __asm
43#endif
David Horstmanne3d8f312023-01-03 11:07:09 +000044/* *INDENT-ON* */
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_HAVE_X86_64)
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 Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +010056 if (!done) {
57 asm ("movl $1, %%eax \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +020058 "cpuid \n\t"
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010059 : "=c" (c)
60 :
Gilles Peskine449bd832023-01-11 14:50:10 +010061 : "eax", "ebx", "edx");
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010062 done = 1;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010063 }
64
Gilles Peskine449bd832023-01-11 14:50:10 +010065 return (c & what) != 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010066}
67
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010068/*
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +020069 * Binutils needs to be at least 2.19 to support AES-NI instructions.
70 * Unfortunately, a lot of users have a lower version now (2014-04).
71 * Emit bytecode directly in order to support "old" version of gas.
72 *
73 * Opcodes from the Intel architecture reference manual, vol. 3.
74 * We always use registers, so we don't need prefixes for memory operands.
75 * Operand macros are in gas order (src, dst) as opposed to Intel order
76 * (dst, src) in order to blend better into the surrounding assembly code.
77 */
78#define AESDEC ".byte 0x66,0x0F,0x38,0xDE,"
79#define AESDECLAST ".byte 0x66,0x0F,0x38,0xDF,"
80#define AESENC ".byte 0x66,0x0F,0x38,0xDC,"
81#define AESENCLAST ".byte 0x66,0x0F,0x38,0xDD,"
82#define AESIMC ".byte 0x66,0x0F,0x38,0xDB,"
83#define AESKEYGENA ".byte 0x66,0x0F,0x3A,0xDF,"
84#define PCLMULQDQ ".byte 0x66,0x0F,0x3A,0x44,"
85
86#define xmm0_xmm0 "0xC0"
87#define xmm0_xmm1 "0xC8"
88#define xmm0_xmm2 "0xD0"
89#define xmm0_xmm3 "0xD8"
90#define xmm0_xmm4 "0xE0"
91#define xmm1_xmm0 "0xC1"
92#define xmm1_xmm2 "0xD1"
93
94/*
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010095 * AES-NI AES-ECB block en(de)cryption
96 */
Gilles Peskine449bd832023-01-11 14:50:10 +010097int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
98 int mode,
99 const unsigned char input[16],
100 unsigned char output[16])
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100101{
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 asm ("movdqu (%3), %%xmm0 \n\t" // load input
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200103 "movdqu (%1), %%xmm1 \n\t" // load round key 0
104 "pxor %%xmm1, %%xmm0 \n\t" // round 0
James Cowgill6c8edca2015-12-17 01:40:26 +0000105 "add $16, %1 \n\t" // point to next round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200106 "subl $1, %0 \n\t" // normal rounds = nr - 1
107 "test %2, %2 \n\t" // mode?
108 "jz 2f \n\t" // 0 = decrypt
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100109
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200110 "1: \n\t" // encryption loop
111 "movdqu (%1), %%xmm1 \n\t" // load round key
112 AESENC xmm1_xmm0 "\n\t" // do round
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 "add $16, %1 \n\t" // point to next round key
114 "subl $1, %0 \n\t" // loop
115 "jnz 1b \n\t"
116 "movdqu (%1), %%xmm1 \n\t" // load round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200117 AESENCLAST xmm1_xmm0 "\n\t" // last round
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 "jmp 3f \n\t"
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 "2: \n\t" // decryption loop
121 "movdqu (%1), %%xmm1 \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200122 AESDEC xmm1_xmm0 "\n\t" // do round
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 "add $16, %1 \n\t"
124 "subl $1, %0 \n\t"
125 "jnz 2b \n\t"
126 "movdqu (%1), %%xmm1 \n\t" // load round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200127 AESDECLAST xmm1_xmm0 "\n\t" // last round
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 "3: \n\t"
130 "movdqu %%xmm0, (%4) \n\t" // export output
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100131 :
Werner Lewisdd76ef32022-05-30 12:00:21 +0100132 : "r" (ctx->nr), "r" (ctx->buf + ctx->rk_offset), "r" (mode), "r" (input), "r" (output)
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 : "memory", "cc", "xmm0", "xmm1");
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100134
135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 return 0;
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100137}
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100138
139/*
140 * GCM multiplication: c = a times b in GF(2^128)
141 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
142 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100143void mbedtls_aesni_gcm_mult(unsigned char c[16],
144 const unsigned char a[16],
145 const unsigned char b[16])
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100146{
147 unsigned char aa[16], bb[16], cc[16];
148 size_t i;
149
150 /* The inputs are in big-endian order, so byte-reverse them */
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 for (i = 0; i < 16; i++) {
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100152 aa[i] = a[15 - i];
153 bb[i] = b[15 - i];
154 }
155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 asm ("movdqu (%0), %%xmm0 \n\t" // a1:a0
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200157 "movdqu (%1), %%xmm1 \n\t" // b1:b0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100158
159 /*
160 * Caryless multiplication xmm2:xmm1 = xmm0 * xmm1
161 * using [CLMUL-WP] algorithm 1 (p. 13).
162 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200163 "movdqa %%xmm1, %%xmm2 \n\t" // copy of b1:b0
164 "movdqa %%xmm1, %%xmm3 \n\t" // same
165 "movdqa %%xmm1, %%xmm4 \n\t" // same
166 PCLMULQDQ xmm0_xmm1 ",0x00 \n\t" // a0*b0 = c1:c0
167 PCLMULQDQ xmm0_xmm2 ",0x11 \n\t" // a1*b1 = d1:d0
168 PCLMULQDQ xmm0_xmm3 ",0x10 \n\t" // a0*b1 = e1:e0
169 PCLMULQDQ xmm0_xmm4 ",0x01 \n\t" // a1*b0 = f1:f0
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 "pxor %%xmm3, %%xmm4 \n\t" // e1+f1:e0+f0
171 "movdqa %%xmm4, %%xmm3 \n\t" // same
172 "psrldq $8, %%xmm4 \n\t" // 0:e1+f1
173 "pslldq $8, %%xmm3 \n\t" // e0+f0:0
174 "pxor %%xmm4, %%xmm2 \n\t" // d1:d0+e1+f1
175 "pxor %%xmm3, %%xmm1 \n\t" // c1+e0+f1:c0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100176
177 /*
178 * Now shift the result one bit to the left,
179 * taking advantage of [CLMUL-WP] eq 27 (p. 20)
180 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 "movdqa %%xmm1, %%xmm3 \n\t" // r1:r0
182 "movdqa %%xmm2, %%xmm4 \n\t" // r3:r2
183 "psllq $1, %%xmm1 \n\t" // r1<<1:r0<<1
184 "psllq $1, %%xmm2 \n\t" // r3<<1:r2<<1
185 "psrlq $63, %%xmm3 \n\t" // r1>>63:r0>>63
186 "psrlq $63, %%xmm4 \n\t" // r3>>63:r2>>63
187 "movdqa %%xmm3, %%xmm5 \n\t" // r1>>63:r0>>63
188 "pslldq $8, %%xmm3 \n\t" // r0>>63:0
189 "pslldq $8, %%xmm4 \n\t" // r2>>63:0
190 "psrldq $8, %%xmm5 \n\t" // 0:r1>>63
191 "por %%xmm3, %%xmm1 \n\t" // r1<<1|r0>>63:r0<<1
192 "por %%xmm4, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1
193 "por %%xmm5, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1|r1>>63
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100194
195 /*
196 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
197 * using [CLMUL-WP] algorithm 5 (p. 20).
198 * Currently xmm2:xmm1 holds x3:x2:x1:x0 (already shifted).
199 */
200 /* Step 2 (1) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 "movdqa %%xmm1, %%xmm3 \n\t" // x1:x0
202 "movdqa %%xmm1, %%xmm4 \n\t" // same
203 "movdqa %%xmm1, %%xmm5 \n\t" // same
204 "psllq $63, %%xmm3 \n\t" // x1<<63:x0<<63 = stuff:a
205 "psllq $62, %%xmm4 \n\t" // x1<<62:x0<<62 = stuff:b
206 "psllq $57, %%xmm5 \n\t" // x1<<57:x0<<57 = stuff:c
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100207
208 /* Step 2 (2) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 "pxor %%xmm4, %%xmm3 \n\t" // stuff:a+b
210 "pxor %%xmm5, %%xmm3 \n\t" // stuff:a+b+c
211 "pslldq $8, %%xmm3 \n\t" // a+b+c:0
212 "pxor %%xmm3, %%xmm1 \n\t" // x1+a+b+c:x0 = d:x0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100213
214 /* Steps 3 and 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 "movdqa %%xmm1,%%xmm0 \n\t" // d:x0
216 "movdqa %%xmm1,%%xmm4 \n\t" // same
217 "movdqa %%xmm1,%%xmm5 \n\t" // same
218 "psrlq $1, %%xmm0 \n\t" // e1:x0>>1 = e1:e0'
219 "psrlq $2, %%xmm4 \n\t" // f1:x0>>2 = f1:f0'
220 "psrlq $7, %%xmm5 \n\t" // g1:x0>>7 = g1:g0'
221 "pxor %%xmm4, %%xmm0 \n\t" // e1+f1:e0'+f0'
222 "pxor %%xmm5, %%xmm0 \n\t" // e1+f1+g1:e0'+f0'+g0'
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200223 // e0'+f0'+g0' is almost e0+f0+g0, ex\tcept for some missing
224 // bits carried from d. Now get those\t bits back in.
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 "movdqa %%xmm1,%%xmm3 \n\t" // d:x0
226 "movdqa %%xmm1,%%xmm4 \n\t" // same
227 "movdqa %%xmm1,%%xmm5 \n\t" // same
228 "psllq $63, %%xmm3 \n\t" // d<<63:stuff
229 "psllq $62, %%xmm4 \n\t" // d<<62:stuff
230 "psllq $57, %%xmm5 \n\t" // d<<57:stuff
231 "pxor %%xmm4, %%xmm3 \n\t" // d<<63+d<<62:stuff
232 "pxor %%xmm5, %%xmm3 \n\t" // missing bits of d:stuff
233 "psrldq $8, %%xmm3 \n\t" // 0:missing bits of d
234 "pxor %%xmm3, %%xmm0 \n\t" // e1+f1+g1:e0+f0+g0
235 "pxor %%xmm1, %%xmm0 \n\t" // h1:h0
236 "pxor %%xmm2, %%xmm0 \n\t" // x3+h1:x2+h0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 "movdqu %%xmm0, (%2) \n\t" // done
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100239 :
240 : "r" (aa), "r" (bb), "r" (cc)
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100242
243 /* Now byte-reverse the outputs */
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 for (i = 0; i < 16; i++) {
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100245 c[i] = cc[15 - i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 }
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100247
Paul Bakkere7f51332013-12-30 15:32:02 +0100248 return;
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100249}
250
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100251/*
252 * Compute decryption round keys from encryption round keys
253 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100254void mbedtls_aesni_inverse_key(unsigned char *invkey,
255 const unsigned char *fwdkey, int nr)
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100256{
257 unsigned char *ik = invkey;
258 const unsigned char *fk = fwdkey + 16 * nr;
259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 for (fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16) {
263 asm ("movdqu (%0), %%xmm0 \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200264 AESIMC xmm0_xmm0 "\n\t"
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 "movdqu %%xmm0, (%1) \n\t"
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100266 :
267 : "r" (fk), "r" (ik)
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 : "memory", "xmm0");
269 }
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100272}
273
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100274/*
275 * Key expansion, 128-bit case
276 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100277static void aesni_setkey_enc_128(unsigned char *rk,
278 const unsigned char *key)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100279{
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 asm ("movdqu (%1), %%xmm0 \n\t" // copy the original key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200281 "movdqu %%xmm0, (%0) \n\t" // as round key 0
282 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100283
284 /*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100285 * Finish generating the next round key.
286 *
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100287 * On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff
288 * with X = rot( sub( r3 ) ) ^ RCON.
289 *
290 * On exit, xmm0 is r7:r6:r5:r4
291 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
292 * and those are written to the round key buffer.
293 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200294 "1: \n\t"
295 "pshufd $0xff, %%xmm1, %%xmm1 \n\t" // X:X:X:X
296 "pxor %%xmm0, %%xmm1 \n\t" // X+r3:X+r2:X+r1:r4
297 "pslldq $4, %%xmm0 \n\t" // r2:r1:r0:0
298 "pxor %%xmm0, %%xmm1 \n\t" // X+r3+r2:X+r2+r1:r5:r4
299 "pslldq $4, %%xmm0 \n\t" // etc
300 "pxor %%xmm0, %%xmm1 \n\t"
301 "pslldq $4, %%xmm0 \n\t"
302 "pxor %%xmm1, %%xmm0 \n\t" // update xmm0 for next time!
303 "add $16, %0 \n\t" // point to next round key
304 "movdqu %%xmm0, (%0) \n\t" // write it
305 "ret \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100306
307 /* Main "loop" */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200308 "2: \n\t"
309 AESKEYGENA xmm0_xmm1 ",0x01 \n\tcall 1b \n\t"
310 AESKEYGENA xmm0_xmm1 ",0x02 \n\tcall 1b \n\t"
311 AESKEYGENA xmm0_xmm1 ",0x04 \n\tcall 1b \n\t"
312 AESKEYGENA xmm0_xmm1 ",0x08 \n\tcall 1b \n\t"
313 AESKEYGENA xmm0_xmm1 ",0x10 \n\tcall 1b \n\t"
314 AESKEYGENA xmm0_xmm1 ",0x20 \n\tcall 1b \n\t"
315 AESKEYGENA xmm0_xmm1 ",0x40 \n\tcall 1b \n\t"
316 AESKEYGENA xmm0_xmm1 ",0x80 \n\tcall 1b \n\t"
317 AESKEYGENA xmm0_xmm1 ",0x1B \n\tcall 1b \n\t"
318 AESKEYGENA xmm0_xmm1 ",0x36 \n\tcall 1b \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100319 :
320 : "r" (rk), "r" (key)
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 : "memory", "cc", "0");
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100322}
323
324/*
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100325 * Key expansion, 192-bit case
326 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100327static void aesni_setkey_enc_192(unsigned char *rk,
328 const unsigned char *key)
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100329{
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 asm ("movdqu (%1), %%xmm0 \n\t" // copy original round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200331 "movdqu %%xmm0, (%0) \n\t"
332 "add $16, %0 \n\t"
333 "movq 16(%1), %%xmm1 \n\t"
334 "movq %%xmm1, (%0) \n\t"
335 "add $8, %0 \n\t"
336 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100337
338 /*
339 * Finish generating the next 6 quarter-keys.
340 *
341 * On entry xmm0 is r3:r2:r1:r0, xmm1 is stuff:stuff:r5:r4
342 * and xmm2 is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON.
343 *
344 * On exit, xmm0 is r9:r8:r7:r6 and xmm1 is stuff:stuff:r11:r10
345 * and those are written to the round key buffer.
346 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200347 "1: \n\t"
348 "pshufd $0x55, %%xmm2, %%xmm2 \n\t" // X:X:X:X
349 "pxor %%xmm0, %%xmm2 \n\t" // X+r3:X+r2:X+r1:r4
350 "pslldq $4, %%xmm0 \n\t" // etc
351 "pxor %%xmm0, %%xmm2 \n\t"
352 "pslldq $4, %%xmm0 \n\t"
353 "pxor %%xmm0, %%xmm2 \n\t"
354 "pslldq $4, %%xmm0 \n\t"
355 "pxor %%xmm2, %%xmm0 \n\t" // update xmm0 = r9:r8:r7:r6
356 "movdqu %%xmm0, (%0) \n\t"
357 "add $16, %0 \n\t"
358 "pshufd $0xff, %%xmm0, %%xmm2 \n\t" // r9:r9:r9:r9
359 "pxor %%xmm1, %%xmm2 \n\t" // stuff:stuff:r9+r5:r10
360 "pslldq $4, %%xmm1 \n\t" // r2:r1:r0:0
361 "pxor %%xmm2, %%xmm1 \n\t" // xmm1 = stuff:stuff:r11:r10
362 "movq %%xmm1, (%0) \n\t"
363 "add $8, %0 \n\t"
364 "ret \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100365
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200366 "2: \n\t"
367 AESKEYGENA xmm1_xmm2 ",0x01 \n\tcall 1b \n\t"
368 AESKEYGENA xmm1_xmm2 ",0x02 \n\tcall 1b \n\t"
369 AESKEYGENA xmm1_xmm2 ",0x04 \n\tcall 1b \n\t"
370 AESKEYGENA xmm1_xmm2 ",0x08 \n\tcall 1b \n\t"
371 AESKEYGENA xmm1_xmm2 ",0x10 \n\tcall 1b \n\t"
372 AESKEYGENA xmm1_xmm2 ",0x20 \n\tcall 1b \n\t"
373 AESKEYGENA xmm1_xmm2 ",0x40 \n\tcall 1b \n\t"
374 AESKEYGENA xmm1_xmm2 ",0x80 \n\tcall 1b \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100375
376 :
377 : "r" (rk), "r" (key)
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 : "memory", "cc", "0");
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100379}
380
381/*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100382 * Key expansion, 256-bit case
383 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100384static void aesni_setkey_enc_256(unsigned char *rk,
385 const unsigned char *key)
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100386{
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 asm ("movdqu (%1), %%xmm0 \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200388 "movdqu %%xmm0, (%0) \n\t"
389 "add $16, %0 \n\t"
390 "movdqu 16(%1), %%xmm1 \n\t"
391 "movdqu %%xmm1, (%0) \n\t"
392 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100393
394 /*
395 * Finish generating the next two round keys.
396 *
397 * On entry xmm0 is r3:r2:r1:r0, xmm1 is r7:r6:r5:r4 and
398 * xmm2 is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
399 *
400 * On exit, xmm0 is r11:r10:r9:r8 and xmm1 is r15:r14:r13:r12
401 * and those have been written to the output buffer.
402 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200403 "1: \n\t"
404 "pshufd $0xff, %%xmm2, %%xmm2 \n\t"
405 "pxor %%xmm0, %%xmm2 \n\t"
406 "pslldq $4, %%xmm0 \n\t"
407 "pxor %%xmm0, %%xmm2 \n\t"
408 "pslldq $4, %%xmm0 \n\t"
409 "pxor %%xmm0, %%xmm2 \n\t"
410 "pslldq $4, %%xmm0 \n\t"
411 "pxor %%xmm2, %%xmm0 \n\t"
412 "add $16, %0 \n\t"
413 "movdqu %%xmm0, (%0) \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100414
415 /* Set xmm2 to stuff:Y:stuff:stuff with Y = subword( r11 )
416 * and proceed to generate next round key from there */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200417 AESKEYGENA xmm0_xmm2 ",0x00 \n\t"
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 "pshufd $0xaa, %%xmm2, %%xmm2 \n\t"
419 "pxor %%xmm1, %%xmm2 \n\t"
420 "pslldq $4, %%xmm1 \n\t"
421 "pxor %%xmm1, %%xmm2 \n\t"
422 "pslldq $4, %%xmm1 \n\t"
423 "pxor %%xmm1, %%xmm2 \n\t"
424 "pslldq $4, %%xmm1 \n\t"
425 "pxor %%xmm2, %%xmm1 \n\t"
426 "add $16, %0 \n\t"
427 "movdqu %%xmm1, (%0) \n\t"
428 "ret \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100429
430 /*
431 * Main "loop" - Generating one more key than necessary,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 * see definition of mbedtls_aes_context.buf
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100433 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 "2: \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200435 AESKEYGENA xmm1_xmm2 ",0x01 \n\tcall 1b \n\t"
436 AESKEYGENA xmm1_xmm2 ",0x02 \n\tcall 1b \n\t"
437 AESKEYGENA xmm1_xmm2 ",0x04 \n\tcall 1b \n\t"
438 AESKEYGENA xmm1_xmm2 ",0x08 \n\tcall 1b \n\t"
439 AESKEYGENA xmm1_xmm2 ",0x10 \n\tcall 1b \n\t"
440 AESKEYGENA xmm1_xmm2 ",0x20 \n\tcall 1b \n\t"
441 AESKEYGENA xmm1_xmm2 ",0x40 \n\tcall 1b \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100442 :
443 : "r" (rk), "r" (key)
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 : "memory", "cc", "0");
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100445}
446
447/*
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100448 * Key expansion, wrapper
449 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100450int mbedtls_aesni_setkey_enc(unsigned char *rk,
451 const unsigned char *key,
452 size_t bits)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100453{
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 switch (bits) {
455 case 128: aesni_setkey_enc_128(rk, key); break;
456 case 192: aesni_setkey_enc_192(rk, key); break;
457 case 256: aesni_setkey_enc_256(rk, key); break;
458 default: return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100459 }
460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 return 0;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100462}
463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464#endif /* MBEDTLS_HAVE_X86_64 */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466#endif /* MBEDTLS_AESNI_C */