blob: b0168d34b3bd98b5d0ae254d480fc0c812a99d38 [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/*
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020021 * [AES-WP]
22 * http://software.intel.com/en-us/articles/intel-advanced-encryption-standard-aes-instructions-set
23 * [CLMUL-WP]
24 * 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 +010025 */
26
Gilles Peskinedb09ef62020-06-03 01:43:33 +020027#include "common.h"
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_AESNI_C)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010030
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020031# if defined(__has_feature)
32# if __has_feature(memory_sanitizer)
33# warning \
34 "MBEDTLS_AESNI_C is known to cause spurious error reports with some memory sanitizers as they do not understand the assembly code."
35# endif
36# endif
Gilles Peskine5053efd2018-04-05 15:25:50 +020037
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020038# include "aesni.h"
Rich Evans00ab4702015-02-06 13:43:58 +000039
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020040# include <string.h>
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010041
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020042# ifndef asm
43# define asm __asm
44# endif
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020045
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020051int 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
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020056 if (!done) {
57 asm("movl $1, %%eax \n\t"
58 "cpuid \n\t"
59 : "=c"(c)
60 :
61 : "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
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020065 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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020078# 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,"
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +020085
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020086# 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"
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +020093
94/*
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010095 * AES-NI AES-ECB block en(de)cryption
96 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020097int 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{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200102 asm("movdqu (%3), %%xmm0 \n\t" // load input
103 "movdqu (%1), %%xmm1 \n\t" // load round key 0
104 "pxor %%xmm1, %%xmm0 \n\t" // round 0
105 "add $16, %1 \n\t" // point to next round key
106 "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
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200110 "1: \n\t" // encryption loop
111 "movdqu (%1), %%xmm1 \n\t" // load round key
112 AESENC xmm1_xmm0 "\n\t" // do round
113 "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
117 AESENCLAST xmm1_xmm0 "\n\t" // last round
118 "jmp 3f \n\t"
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100119
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200120 "2: \n\t" // decryption loop
121 "movdqu (%1), %%xmm1 \n\t" AESDEC xmm1_xmm0 "\n\t" // do round
122 "add $16, %1 \n\t"
123 "subl $1, %0 \n\t"
124 "jnz 2b \n\t"
125 "movdqu (%1), %%xmm1 \n\t" // load round key
126 AESDECLAST xmm1_xmm0 "\n\t" // last round
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100127
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200128 "3: \n\t"
129 "movdqu %%xmm0, (%4) \n\t" // export output
130 :
131 : "r"(ctx->nr), "r"(ctx->rk), "r"(mode), "r"(input), "r"(output)
132 : "memory", "cc", "xmm0", "xmm1");
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100133
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200134 return 0;
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100135}
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100136
137/*
138 * GCM multiplication: c = a times b in GF(2^128)
139 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
140 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200141void mbedtls_aesni_gcm_mult(unsigned char c[16],
142 const unsigned char a[16],
143 const unsigned char b[16])
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100144{
145 unsigned char aa[16], bb[16], cc[16];
146 size_t i;
147
148 /* The inputs are in big-endian order, so byte-reverse them */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200149 for (i = 0; i < 16; i++) {
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100150 aa[i] = a[15 - i];
151 bb[i] = b[15 - i];
152 }
153
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200154 asm("movdqu (%0), %%xmm0 \n\t" // a1:a0
155 "movdqu (%1), %%xmm1 \n\t" // b1:b0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100156
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200157 /*
158 * Caryless multiplication xmm2:xmm1 = xmm0 * xmm1
159 * using [CLMUL-WP] algorithm 1 (p. 13).
160 */
161 "movdqa %%xmm1, %%xmm2 \n\t" // copy of b1:b0
162 "movdqa %%xmm1, %%xmm3 \n\t" // same
163 "movdqa %%xmm1, %%xmm4 \n\t" // same
164 PCLMULQDQ xmm0_xmm1 ",0x00 \n\t" // a0*b0 = c1:c0
165 PCLMULQDQ xmm0_xmm2 ",0x11 \n\t" // a1*b1 = d1:d0
166 PCLMULQDQ xmm0_xmm3 ",0x10 \n\t" // a0*b1 = e1:e0
167 PCLMULQDQ xmm0_xmm4 ",0x01 \n\t" // a1*b0 = f1:f0
168 "pxor %%xmm3, %%xmm4 \n\t" // e1+f1:e0+f0
169 "movdqa %%xmm4, %%xmm3 \n\t" // same
170 "psrldq $8, %%xmm4 \n\t" // 0:e1+f1
171 "pslldq $8, %%xmm3 \n\t" // e0+f0:0
172 "pxor %%xmm4, %%xmm2 \n\t" // d1:d0+e1+f1
173 "pxor %%xmm3, %%xmm1 \n\t" // c1+e0+f1:c0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100174
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200175 /*
176 * Now shift the result one bit to the left,
177 * taking advantage of [CLMUL-WP] eq 27 (p. 20)
178 */
179 "movdqa %%xmm1, %%xmm3 \n\t" // r1:r0
180 "movdqa %%xmm2, %%xmm4 \n\t" // r3:r2
181 "psllq $1, %%xmm1 \n\t" // r1<<1:r0<<1
182 "psllq $1, %%xmm2 \n\t" // r3<<1:r2<<1
183 "psrlq $63, %%xmm3 \n\t" // r1>>63:r0>>63
184 "psrlq $63, %%xmm4 \n\t" // r3>>63:r2>>63
185 "movdqa %%xmm3, %%xmm5 \n\t" // r1>>63:r0>>63
186 "pslldq $8, %%xmm3 \n\t" // r0>>63:0
187 "pslldq $8, %%xmm4 \n\t" // r2>>63:0
188 "psrldq $8, %%xmm5 \n\t" // 0:r1>>63
189 "por %%xmm3, %%xmm1 \n\t" // r1<<1|r0>>63:r0<<1
190 "por %%xmm4, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1
191 "por %%xmm5, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1|r1>>63
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100192
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200193 /*
194 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
195 * using [CLMUL-WP] algorithm 5 (p. 20).
196 * Currently xmm2:xmm1 holds x3:x2:x1:x0 (already shifted).
197 */
198 /* Step 2 (1) */
199 "movdqa %%xmm1, %%xmm3 \n\t" // x1:x0
200 "movdqa %%xmm1, %%xmm4 \n\t" // same
201 "movdqa %%xmm1, %%xmm5 \n\t" // same
202 "psllq $63, %%xmm3 \n\t" // x1<<63:x0<<63 = stuff:a
203 "psllq $62, %%xmm4 \n\t" // x1<<62:x0<<62 = stuff:b
204 "psllq $57, %%xmm5 \n\t" // x1<<57:x0<<57 = stuff:c
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100205
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200206 /* Step 2 (2) */
207 "pxor %%xmm4, %%xmm3 \n\t" // stuff:a+b
208 "pxor %%xmm5, %%xmm3 \n\t" // stuff:a+b+c
209 "pslldq $8, %%xmm3 \n\t" // a+b+c:0
210 "pxor %%xmm3, %%xmm1 \n\t" // x1+a+b+c:x0 = d:x0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100211
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200212 /* Steps 3 and 4 */
213 "movdqa %%xmm1,%%xmm0 \n\t" // d:x0
214 "movdqa %%xmm1,%%xmm4 \n\t" // same
215 "movdqa %%xmm1,%%xmm5 \n\t" // same
216 "psrlq $1, %%xmm0 \n\t" // e1:x0>>1 = e1:e0'
217 "psrlq $2, %%xmm4 \n\t" // f1:x0>>2 = f1:f0'
218 "psrlq $7, %%xmm5 \n\t" // g1:x0>>7 = g1:g0'
219 "pxor %%xmm4, %%xmm0 \n\t" // e1+f1:e0'+f0'
220 "pxor %%xmm5, %%xmm0 \n\t" // e1+f1+g1:e0'+f0'+g0'
221 // e0'+f0'+g0' is almost e0+f0+g0, ex\tcept for some missing
222 // bits carried from d. Now get those\t bits back in.
223 "movdqa %%xmm1,%%xmm3 \n\t" // d:x0
224 "movdqa %%xmm1,%%xmm4 \n\t" // same
225 "movdqa %%xmm1,%%xmm5 \n\t" // same
226 "psllq $63, %%xmm3 \n\t" // d<<63:stuff
227 "psllq $62, %%xmm4 \n\t" // d<<62:stuff
228 "psllq $57, %%xmm5 \n\t" // d<<57:stuff
229 "pxor %%xmm4, %%xmm3 \n\t" // d<<63+d<<62:stuff
230 "pxor %%xmm5, %%xmm3 \n\t" // missing bits of d:stuff
231 "psrldq $8, %%xmm3 \n\t" // 0:missing bits of d
232 "pxor %%xmm3, %%xmm0 \n\t" // e1+f1+g1:e0+f0+g0
233 "pxor %%xmm1, %%xmm0 \n\t" // h1:h0
234 "pxor %%xmm2, %%xmm0 \n\t" // x3+h1:x2+h0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100235
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200236 "movdqu %%xmm0, (%2) \n\t" // done
237 :
238 : "r"(aa), "r"(bb), "r"(cc)
239 : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100240
241 /* Now byte-reverse the outputs */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200242 for (i = 0; i < 16; i++)
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100243 c[i] = cc[15 - i];
244
Paul Bakkere7f51332013-12-30 15:32:02 +0100245 return;
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100246}
247
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100248/*
249 * Compute decryption round keys from encryption round keys
250 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200251void mbedtls_aesni_inverse_key(unsigned char *invkey,
252 const unsigned char *fwdkey,
253 int nr)
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100254{
255 unsigned char *ik = invkey;
256 const unsigned char *fk = fwdkey + 16 * nr;
257
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200258 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100259
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200260 for (fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16)
261 asm("movdqu (%0), %%xmm0 \n\t" AESIMC xmm0_xmm0 "\n\t"
262 "movdqu %%xmm0, (%1) \n\t"
263 :
264 : "r"(fk), "r"(ik)
265 : "memory", "xmm0");
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100266
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200267 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100268}
269
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100270/*
271 * Key expansion, 128-bit case
272 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200273static void aesni_setkey_enc_128(unsigned char *rk, const unsigned char *key)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100274{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200275 asm("movdqu (%1), %%xmm0 \n\t" // copy the original key
276 "movdqu %%xmm0, (%0) \n\t" // as round key 0
277 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100278
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200279 /*
280 * Finish generating the next round key.
281 *
282 * On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff
283 * with X = rot( sub( r3 ) ) ^ RCON.
284 *
285 * On exit, xmm0 is r7:r6:r5:r4
286 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
287 * and those are written to the round key buffer.
288 */
289 "1: \n\t"
290 "pshufd $0xff, %%xmm1, %%xmm1 \n\t" // X:X:X:X
291 "pxor %%xmm0, %%xmm1 \n\t" // X+r3:X+r2:X+r1:r4
292 "pslldq $4, %%xmm0 \n\t" // r2:r1:r0:0
293 "pxor %%xmm0, %%xmm1 \n\t" // X+r3+r2:X+r2+r1:r5:r4
294 "pslldq $4, %%xmm0 \n\t" // etc
295 "pxor %%xmm0, %%xmm1 \n\t"
296 "pslldq $4, %%xmm0 \n\t"
297 "pxor %%xmm1, %%xmm0 \n\t" // update xmm0 for next time!
298 "add $16, %0 \n\t" // point to next round key
299 "movdqu %%xmm0, (%0) \n\t" // write it
300 "ret \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100301
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200302 /* Main "loop" */
303 "2: \n\t" AESKEYGENA xmm0_xmm1
304 ",0x01 \n\tcall 1b \n\t" AESKEYGENA xmm0_xmm1
305 ",0x02 \n\tcall 1b \n\t" AESKEYGENA xmm0_xmm1
306 ",0x04 \n\tcall 1b \n\t" AESKEYGENA xmm0_xmm1
307 ",0x08 \n\tcall 1b \n\t" AESKEYGENA xmm0_xmm1
308 ",0x10 \n\tcall 1b \n\t" AESKEYGENA xmm0_xmm1
309 ",0x20 \n\tcall 1b \n\t" AESKEYGENA xmm0_xmm1
310 ",0x40 \n\tcall 1b \n\t" AESKEYGENA xmm0_xmm1
311 ",0x80 \n\tcall 1b \n\t" AESKEYGENA xmm0_xmm1
312 ",0x1B \n\tcall 1b \n\t" AESKEYGENA xmm0_xmm1
313 ",0x36 \n\tcall 1b \n\t"
314 :
315 : "r"(rk), "r"(key)
316 : "memory", "cc", "0");
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100317}
318
319/*
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100320 * Key expansion, 192-bit case
321 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200322static void aesni_setkey_enc_192(unsigned char *rk, const unsigned char *key)
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100323{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200324 asm("movdqu (%1), %%xmm0 \n\t" // copy original round key
325 "movdqu %%xmm0, (%0) \n\t"
326 "add $16, %0 \n\t"
327 "movq 16(%1), %%xmm1 \n\t"
328 "movq %%xmm1, (%0) \n\t"
329 "add $8, %0 \n\t"
330 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100331
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200332 /*
333 * Finish generating the next 6 quarter-keys.
334 *
335 * On entry xmm0 is r3:r2:r1:r0, xmm1 is stuff:stuff:r5:r4
336 * and xmm2 is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON.
337 *
338 * On exit, xmm0 is r9:r8:r7:r6 and xmm1 is stuff:stuff:r11:r10
339 * and those are written to the round key buffer.
340 */
341 "1: \n\t"
342 "pshufd $0x55, %%xmm2, %%xmm2 \n\t" // X:X:X:X
343 "pxor %%xmm0, %%xmm2 \n\t" // X+r3:X+r2:X+r1:r4
344 "pslldq $4, %%xmm0 \n\t" // etc
345 "pxor %%xmm0, %%xmm2 \n\t"
346 "pslldq $4, %%xmm0 \n\t"
347 "pxor %%xmm0, %%xmm2 \n\t"
348 "pslldq $4, %%xmm0 \n\t"
349 "pxor %%xmm2, %%xmm0 \n\t" // update xmm0 = r9:r8:r7:r6
350 "movdqu %%xmm0, (%0) \n\t"
351 "add $16, %0 \n\t"
352 "pshufd $0xff, %%xmm0, %%xmm2 \n\t" // r9:r9:r9:r9
353 "pxor %%xmm1, %%xmm2 \n\t" // stuff:stuff:r9+r5:r10
354 "pslldq $4, %%xmm1 \n\t" // r2:r1:r0:0
355 "pxor %%xmm2, %%xmm1 \n\t" // xmm1 = stuff:stuff:r11:r10
356 "movq %%xmm1, (%0) \n\t"
357 "add $8, %0 \n\t"
358 "ret \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100359
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200360 "2: \n\t" AESKEYGENA xmm1_xmm2
361 ",0x01 \n\tcall 1b \n\t" AESKEYGENA xmm1_xmm2
362 ",0x02 \n\tcall 1b \n\t" AESKEYGENA xmm1_xmm2
363 ",0x04 \n\tcall 1b \n\t" AESKEYGENA xmm1_xmm2
364 ",0x08 \n\tcall 1b \n\t" AESKEYGENA xmm1_xmm2
365 ",0x10 \n\tcall 1b \n\t" AESKEYGENA xmm1_xmm2
366 ",0x20 \n\tcall 1b \n\t" AESKEYGENA xmm1_xmm2
367 ",0x40 \n\tcall 1b \n\t" AESKEYGENA xmm1_xmm2
368 ",0x80 \n\tcall 1b \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100369
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200370 :
371 : "r"(rk), "r"(key)
372 : "memory", "cc", "0");
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100373}
374
375/*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100376 * Key expansion, 256-bit case
377 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200378static void aesni_setkey_enc_256(unsigned char *rk, const unsigned char *key)
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100379{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200380 asm("movdqu (%1), %%xmm0 \n\t"
381 "movdqu %%xmm0, (%0) \n\t"
382 "add $16, %0 \n\t"
383 "movdqu 16(%1), %%xmm1 \n\t"
384 "movdqu %%xmm1, (%0) \n\t"
385 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100386
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200387 /*
388 * Finish generating the next two round keys.
389 *
390 * On entry xmm0 is r3:r2:r1:r0, xmm1 is r7:r6:r5:r4 and
391 * xmm2 is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
392 *
393 * On exit, xmm0 is r11:r10:r9:r8 and xmm1 is r15:r14:r13:r12
394 * and those have been written to the output buffer.
395 */
396 "1: \n\t"
397 "pshufd $0xff, %%xmm2, %%xmm2 \n\t"
398 "pxor %%xmm0, %%xmm2 \n\t"
399 "pslldq $4, %%xmm0 \n\t"
400 "pxor %%xmm0, %%xmm2 \n\t"
401 "pslldq $4, %%xmm0 \n\t"
402 "pxor %%xmm0, %%xmm2 \n\t"
403 "pslldq $4, %%xmm0 \n\t"
404 "pxor %%xmm2, %%xmm0 \n\t"
405 "add $16, %0 \n\t"
406 "movdqu %%xmm0, (%0) \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100407
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200408 /* Set xmm2 to stuff:Y:stuff:stuff with Y = subword( r11 )
409 * and proceed to generate next round key from there */
410 AESKEYGENA xmm0_xmm2 ",0x00 \n\t"
411 "pshufd $0xaa, %%xmm2, %%xmm2 \n\t"
412 "pxor %%xmm1, %%xmm2 \n\t"
413 "pslldq $4, %%xmm1 \n\t"
414 "pxor %%xmm1, %%xmm2 \n\t"
415 "pslldq $4, %%xmm1 \n\t"
416 "pxor %%xmm1, %%xmm2 \n\t"
417 "pslldq $4, %%xmm1 \n\t"
418 "pxor %%xmm2, %%xmm1 \n\t"
419 "add $16, %0 \n\t"
420 "movdqu %%xmm1, (%0) \n\t"
421 "ret \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100422
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200423 /*
424 * Main "loop" - Generating one more key than necessary,
425 * see definition of mbedtls_aes_context.buf
426 */
427 "2: \n\t" AESKEYGENA xmm1_xmm2
428 ",0x01 \n\tcall 1b \n\t" AESKEYGENA xmm1_xmm2
429 ",0x02 \n\tcall 1b \n\t" AESKEYGENA xmm1_xmm2
430 ",0x04 \n\tcall 1b \n\t" AESKEYGENA xmm1_xmm2
431 ",0x08 \n\tcall 1b \n\t" AESKEYGENA xmm1_xmm2
432 ",0x10 \n\tcall 1b \n\t" AESKEYGENA xmm1_xmm2
433 ",0x20 \n\tcall 1b \n\t" AESKEYGENA xmm1_xmm2
434 ",0x40 \n\tcall 1b \n\t"
435 :
436 : "r"(rk), "r"(key)
437 : "memory", "cc", "0");
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100438}
439
440/*
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100441 * Key expansion, wrapper
442 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200443int mbedtls_aesni_setkey_enc(unsigned char *rk,
444 const unsigned char *key,
445 size_t bits)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100446{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200447 switch (bits) {
448 case 128:
449 aesni_setkey_enc_128(rk, key);
450 break;
451 case 192:
452 aesni_setkey_enc_192(rk, key);
453 break;
454 case 256:
455 aesni_setkey_enc_256(rk, key);
456 break;
457 default:
458 return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100459 }
460
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200461 return 0;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100462}
463
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +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 */