blob: 786dbd92fc75b1160a561d40e0a0a4aa421427d3 [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 Peskined4f31c82023-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
Chris Jones187782f2021-03-09 17:28:35 +000029#include "aesni.h"
Rich Evans00ab4702015-02-06 13:43:58 +000030
31#include <string.h>
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_HAVE_X86_64)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010034
35/*
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010036 * AES-NI support detection routine
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010037 */
Gilles Peskine449bd832023-01-11 14:50:10 +010038int mbedtls_aesni_has_support(unsigned int what)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010039{
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010040 static int done = 0;
41 static unsigned int c = 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010042
Gilles Peskine449bd832023-01-11 14:50:10 +010043 if (!done) {
44 asm ("movl $1, %%eax \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +020045 "cpuid \n\t"
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010046 : "=c" (c)
47 :
Gilles Peskine449bd832023-01-11 14:50:10 +010048 : "eax", "ebx", "edx");
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010049 done = 1;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010050 }
51
Gilles Peskine449bd832023-01-11 14:50:10 +010052 return (c & what) != 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010053}
54
Gilles Peskined0f9b0b2023-03-10 22:25:13 +010055#if defined(__has_feature)
56#if __has_feature(memory_sanitizer)
57#warning \
58 "MBEDTLS_AESNI_C is known to cause spurious error reports with some memory sanitizers as they do not understand the assembly code."
59#endif
60#endif
61
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010062/*
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +020063 * Binutils needs to be at least 2.19 to support AES-NI instructions.
64 * Unfortunately, a lot of users have a lower version now (2014-04).
65 * Emit bytecode directly in order to support "old" version of gas.
66 *
67 * Opcodes from the Intel architecture reference manual, vol. 3.
68 * We always use registers, so we don't need prefixes for memory operands.
69 * Operand macros are in gas order (src, dst) as opposed to Intel order
70 * (dst, src) in order to blend better into the surrounding assembly code.
71 */
72#define AESDEC ".byte 0x66,0x0F,0x38,0xDE,"
73#define AESDECLAST ".byte 0x66,0x0F,0x38,0xDF,"
74#define AESENC ".byte 0x66,0x0F,0x38,0xDC,"
75#define AESENCLAST ".byte 0x66,0x0F,0x38,0xDD,"
76#define AESIMC ".byte 0x66,0x0F,0x38,0xDB,"
77#define AESKEYGENA ".byte 0x66,0x0F,0x3A,0xDF,"
78#define PCLMULQDQ ".byte 0x66,0x0F,0x3A,0x44,"
79
80#define xmm0_xmm0 "0xC0"
81#define xmm0_xmm1 "0xC8"
82#define xmm0_xmm2 "0xD0"
83#define xmm0_xmm3 "0xD8"
84#define xmm0_xmm4 "0xE0"
85#define xmm1_xmm0 "0xC1"
86#define xmm1_xmm2 "0xD1"
87
88/*
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010089 * AES-NI AES-ECB block en(de)cryption
90 */
Gilles Peskine449bd832023-01-11 14:50:10 +010091int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
92 int mode,
93 const unsigned char input[16],
94 unsigned char output[16])
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010095{
Gilles Peskine449bd832023-01-11 14:50:10 +010096 asm ("movdqu (%3), %%xmm0 \n\t" // load input
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +020097 "movdqu (%1), %%xmm1 \n\t" // load round key 0
98 "pxor %%xmm1, %%xmm0 \n\t" // round 0
James Cowgill6c8edca2015-12-17 01:40:26 +000099 "add $16, %1 \n\t" // point to next round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200100 "subl $1, %0 \n\t" // normal rounds = nr - 1
101 "test %2, %2 \n\t" // mode?
102 "jz 2f \n\t" // 0 = decrypt
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100103
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200104 "1: \n\t" // encryption loop
105 "movdqu (%1), %%xmm1 \n\t" // load round key
106 AESENC xmm1_xmm0 "\n\t" // do round
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 "add $16, %1 \n\t" // point to next round key
108 "subl $1, %0 \n\t" // loop
109 "jnz 1b \n\t"
110 "movdqu (%1), %%xmm1 \n\t" // load round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200111 AESENCLAST xmm1_xmm0 "\n\t" // last round
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 "jmp 3f \n\t"
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 "2: \n\t" // decryption loop
115 "movdqu (%1), %%xmm1 \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200116 AESDEC xmm1_xmm0 "\n\t" // do round
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 "add $16, %1 \n\t"
118 "subl $1, %0 \n\t"
119 "jnz 2b \n\t"
120 "movdqu (%1), %%xmm1 \n\t" // load round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200121 AESDECLAST xmm1_xmm0 "\n\t" // last round
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 "3: \n\t"
124 "movdqu %%xmm0, (%4) \n\t" // export output
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100125 :
Werner Lewisdd76ef32022-05-30 12:00:21 +0100126 : "r" (ctx->nr), "r" (ctx->buf + ctx->rk_offset), "r" (mode), "r" (input), "r" (output)
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 : "memory", "cc", "xmm0", "xmm1");
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100128
129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 return 0;
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100131}
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100132
133/*
134 * GCM multiplication: c = a times b in GF(2^128)
135 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
136 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100137void mbedtls_aesni_gcm_mult(unsigned char c[16],
138 const unsigned char a[16],
139 const unsigned char b[16])
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100140{
141 unsigned char aa[16], bb[16], cc[16];
142 size_t i;
143
144 /* The inputs are in big-endian order, so byte-reverse them */
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 for (i = 0; i < 16; i++) {
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100146 aa[i] = a[15 - i];
147 bb[i] = b[15 - i];
148 }
149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 asm ("movdqu (%0), %%xmm0 \n\t" // a1:a0
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200151 "movdqu (%1), %%xmm1 \n\t" // b1:b0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100152
153 /*
154 * Caryless multiplication xmm2:xmm1 = xmm0 * xmm1
Gilles Peskined4f31c82023-03-10 22:21:47 +0100155 * using [CLMUL-WP] algorithm 1 (p. 12).
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100156 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200157 "movdqa %%xmm1, %%xmm2 \n\t" // copy of b1:b0
158 "movdqa %%xmm1, %%xmm3 \n\t" // same
159 "movdqa %%xmm1, %%xmm4 \n\t" // same
160 PCLMULQDQ xmm0_xmm1 ",0x00 \n\t" // a0*b0 = c1:c0
161 PCLMULQDQ xmm0_xmm2 ",0x11 \n\t" // a1*b1 = d1:d0
162 PCLMULQDQ xmm0_xmm3 ",0x10 \n\t" // a0*b1 = e1:e0
163 PCLMULQDQ xmm0_xmm4 ",0x01 \n\t" // a1*b0 = f1:f0
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 "pxor %%xmm3, %%xmm4 \n\t" // e1+f1:e0+f0
165 "movdqa %%xmm4, %%xmm3 \n\t" // same
166 "psrldq $8, %%xmm4 \n\t" // 0:e1+f1
167 "pslldq $8, %%xmm3 \n\t" // e0+f0:0
168 "pxor %%xmm4, %%xmm2 \n\t" // d1:d0+e1+f1
169 "pxor %%xmm3, %%xmm1 \n\t" // c1+e0+f1:c0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100170
171 /*
172 * Now shift the result one bit to the left,
Gilles Peskined4f31c82023-03-10 22:21:47 +0100173 * taking advantage of [CLMUL-WP] eq 27 (p. 18)
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100174 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 "movdqa %%xmm1, %%xmm3 \n\t" // r1:r0
176 "movdqa %%xmm2, %%xmm4 \n\t" // r3:r2
177 "psllq $1, %%xmm1 \n\t" // r1<<1:r0<<1
178 "psllq $1, %%xmm2 \n\t" // r3<<1:r2<<1
179 "psrlq $63, %%xmm3 \n\t" // r1>>63:r0>>63
180 "psrlq $63, %%xmm4 \n\t" // r3>>63:r2>>63
181 "movdqa %%xmm3, %%xmm5 \n\t" // r1>>63:r0>>63
182 "pslldq $8, %%xmm3 \n\t" // r0>>63:0
183 "pslldq $8, %%xmm4 \n\t" // r2>>63:0
184 "psrldq $8, %%xmm5 \n\t" // 0:r1>>63
185 "por %%xmm3, %%xmm1 \n\t" // r1<<1|r0>>63:r0<<1
186 "por %%xmm4, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1
187 "por %%xmm5, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1|r1>>63
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100188
189 /*
190 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
Gilles Peskined4f31c82023-03-10 22:21:47 +0100191 * using [CLMUL-WP] algorithm 5 (p. 18).
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100192 * Currently xmm2:xmm1 holds x3:x2:x1:x0 (already shifted).
193 */
194 /* Step 2 (1) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 "movdqa %%xmm1, %%xmm3 \n\t" // x1:x0
196 "movdqa %%xmm1, %%xmm4 \n\t" // same
197 "movdqa %%xmm1, %%xmm5 \n\t" // same
198 "psllq $63, %%xmm3 \n\t" // x1<<63:x0<<63 = stuff:a
199 "psllq $62, %%xmm4 \n\t" // x1<<62:x0<<62 = stuff:b
200 "psllq $57, %%xmm5 \n\t" // x1<<57:x0<<57 = stuff:c
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100201
202 /* Step 2 (2) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 "pxor %%xmm4, %%xmm3 \n\t" // stuff:a+b
204 "pxor %%xmm5, %%xmm3 \n\t" // stuff:a+b+c
205 "pslldq $8, %%xmm3 \n\t" // a+b+c:0
206 "pxor %%xmm3, %%xmm1 \n\t" // x1+a+b+c:x0 = d:x0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100207
208 /* Steps 3 and 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 "movdqa %%xmm1,%%xmm0 \n\t" // d:x0
210 "movdqa %%xmm1,%%xmm4 \n\t" // same
211 "movdqa %%xmm1,%%xmm5 \n\t" // same
212 "psrlq $1, %%xmm0 \n\t" // e1:x0>>1 = e1:e0'
213 "psrlq $2, %%xmm4 \n\t" // f1:x0>>2 = f1:f0'
214 "psrlq $7, %%xmm5 \n\t" // g1:x0>>7 = g1:g0'
215 "pxor %%xmm4, %%xmm0 \n\t" // e1+f1:e0'+f0'
216 "pxor %%xmm5, %%xmm0 \n\t" // e1+f1+g1:e0'+f0'+g0'
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200217 // e0'+f0'+g0' is almost e0+f0+g0, ex\tcept for some missing
218 // bits carried from d. Now get those\t bits back in.
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 "movdqa %%xmm1,%%xmm3 \n\t" // d:x0
220 "movdqa %%xmm1,%%xmm4 \n\t" // same
221 "movdqa %%xmm1,%%xmm5 \n\t" // same
222 "psllq $63, %%xmm3 \n\t" // d<<63:stuff
223 "psllq $62, %%xmm4 \n\t" // d<<62:stuff
224 "psllq $57, %%xmm5 \n\t" // d<<57:stuff
225 "pxor %%xmm4, %%xmm3 \n\t" // d<<63+d<<62:stuff
226 "pxor %%xmm5, %%xmm3 \n\t" // missing bits of d:stuff
227 "psrldq $8, %%xmm3 \n\t" // 0:missing bits of d
228 "pxor %%xmm3, %%xmm0 \n\t" // e1+f1+g1:e0+f0+g0
229 "pxor %%xmm1, %%xmm0 \n\t" // h1:h0
230 "pxor %%xmm2, %%xmm0 \n\t" // x3+h1:x2+h0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 "movdqu %%xmm0, (%2) \n\t" // done
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100233 :
234 : "r" (aa), "r" (bb), "r" (cc)
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5");
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100236
237 /* Now byte-reverse the outputs */
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 for (i = 0; i < 16; i++) {
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100239 c[i] = cc[15 - i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 }
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100241
Paul Bakkere7f51332013-12-30 15:32:02 +0100242 return;
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100243}
244
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100245/*
246 * Compute decryption round keys from encryption round keys
247 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100248void mbedtls_aesni_inverse_key(unsigned char *invkey,
249 const unsigned char *fwdkey, int nr)
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100250{
251 unsigned char *ik = invkey;
252 const unsigned char *fk = fwdkey + 16 * nr;
253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 for (fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16) {
257 asm ("movdqu (%0), %%xmm0 \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200258 AESIMC xmm0_xmm0 "\n\t"
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 "movdqu %%xmm0, (%1) \n\t"
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100260 :
261 : "r" (fk), "r" (ik)
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 : "memory", "xmm0");
263 }
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 memcpy(ik, fk, 16);
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100266}
267
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100268/*
269 * Key expansion, 128-bit case
270 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100271static void aesni_setkey_enc_128(unsigned char *rk,
272 const unsigned char *key)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100273{
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 asm ("movdqu (%1), %%xmm0 \n\t" // copy the original key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200275 "movdqu %%xmm0, (%0) \n\t" // as round key 0
276 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100277
278 /*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100279 * Finish generating the next round key.
280 *
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100281 * On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff
282 * with X = rot( sub( r3 ) ) ^ RCON.
283 *
284 * On exit, xmm0 is r7:r6:r5:r4
285 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
286 * and those are written to the round key buffer.
287 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200288 "1: \n\t"
289 "pshufd $0xff, %%xmm1, %%xmm1 \n\t" // X:X:X:X
290 "pxor %%xmm0, %%xmm1 \n\t" // X+r3:X+r2:X+r1:r4
291 "pslldq $4, %%xmm0 \n\t" // r2:r1:r0:0
292 "pxor %%xmm0, %%xmm1 \n\t" // X+r3+r2:X+r2+r1:r5:r4
293 "pslldq $4, %%xmm0 \n\t" // etc
294 "pxor %%xmm0, %%xmm1 \n\t"
295 "pslldq $4, %%xmm0 \n\t"
296 "pxor %%xmm1, %%xmm0 \n\t" // update xmm0 for next time!
297 "add $16, %0 \n\t" // point to next round key
298 "movdqu %%xmm0, (%0) \n\t" // write it
299 "ret \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100300
301 /* Main "loop" */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200302 "2: \n\t"
303 AESKEYGENA xmm0_xmm1 ",0x01 \n\tcall 1b \n\t"
304 AESKEYGENA xmm0_xmm1 ",0x02 \n\tcall 1b \n\t"
305 AESKEYGENA xmm0_xmm1 ",0x04 \n\tcall 1b \n\t"
306 AESKEYGENA xmm0_xmm1 ",0x08 \n\tcall 1b \n\t"
307 AESKEYGENA xmm0_xmm1 ",0x10 \n\tcall 1b \n\t"
308 AESKEYGENA xmm0_xmm1 ",0x20 \n\tcall 1b \n\t"
309 AESKEYGENA xmm0_xmm1 ",0x40 \n\tcall 1b \n\t"
310 AESKEYGENA xmm0_xmm1 ",0x80 \n\tcall 1b \n\t"
311 AESKEYGENA xmm0_xmm1 ",0x1B \n\tcall 1b \n\t"
312 AESKEYGENA xmm0_xmm1 ",0x36 \n\tcall 1b \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100313 :
314 : "r" (rk), "r" (key)
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 : "memory", "cc", "0");
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100316}
317
318/*
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100319 * Key expansion, 192-bit case
320 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100321static void aesni_setkey_enc_192(unsigned char *rk,
322 const unsigned char *key)
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100323{
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 asm ("movdqu (%1), %%xmm0 \n\t" // copy original round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200325 "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
332 /*
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 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200341 "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
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200360 "2: \n\t"
361 AESKEYGENA xmm1_xmm2 ",0x01 \n\tcall 1b \n\t"
362 AESKEYGENA xmm1_xmm2 ",0x02 \n\tcall 1b \n\t"
363 AESKEYGENA xmm1_xmm2 ",0x04 \n\tcall 1b \n\t"
364 AESKEYGENA xmm1_xmm2 ",0x08 \n\tcall 1b \n\t"
365 AESKEYGENA xmm1_xmm2 ",0x10 \n\tcall 1b \n\t"
366 AESKEYGENA xmm1_xmm2 ",0x20 \n\tcall 1b \n\t"
367 AESKEYGENA xmm1_xmm2 ",0x40 \n\tcall 1b \n\t"
368 AESKEYGENA xmm1_xmm2 ",0x80 \n\tcall 1b \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100369
370 :
371 : "r" (rk), "r" (key)
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 : "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 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100378static void aesni_setkey_enc_256(unsigned char *rk,
379 const unsigned char *key)
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100380{
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 asm ("movdqu (%1), %%xmm0 \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200382 "movdqu %%xmm0, (%0) \n\t"
383 "add $16, %0 \n\t"
384 "movdqu 16(%1), %%xmm1 \n\t"
385 "movdqu %%xmm1, (%0) \n\t"
386 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100387
388 /*
389 * Finish generating the next two round keys.
390 *
391 * On entry xmm0 is r3:r2:r1:r0, xmm1 is r7:r6:r5:r4 and
392 * xmm2 is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
393 *
394 * On exit, xmm0 is r11:r10:r9:r8 and xmm1 is r15:r14:r13:r12
395 * and those have been written to the output buffer.
396 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200397 "1: \n\t"
398 "pshufd $0xff, %%xmm2, %%xmm2 \n\t"
399 "pxor %%xmm0, %%xmm2 \n\t"
400 "pslldq $4, %%xmm0 \n\t"
401 "pxor %%xmm0, %%xmm2 \n\t"
402 "pslldq $4, %%xmm0 \n\t"
403 "pxor %%xmm0, %%xmm2 \n\t"
404 "pslldq $4, %%xmm0 \n\t"
405 "pxor %%xmm2, %%xmm0 \n\t"
406 "add $16, %0 \n\t"
407 "movdqu %%xmm0, (%0) \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100408
409 /* Set xmm2 to stuff:Y:stuff:stuff with Y = subword( r11 )
410 * and proceed to generate next round key from there */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200411 AESKEYGENA xmm0_xmm2 ",0x00 \n\t"
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 "pshufd $0xaa, %%xmm2, %%xmm2 \n\t"
413 "pxor %%xmm1, %%xmm2 \n\t"
414 "pslldq $4, %%xmm1 \n\t"
415 "pxor %%xmm1, %%xmm2 \n\t"
416 "pslldq $4, %%xmm1 \n\t"
417 "pxor %%xmm1, %%xmm2 \n\t"
418 "pslldq $4, %%xmm1 \n\t"
419 "pxor %%xmm2, %%xmm1 \n\t"
420 "add $16, %0 \n\t"
421 "movdqu %%xmm1, (%0) \n\t"
422 "ret \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100423
424 /*
425 * Main "loop" - Generating one more key than necessary,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 * see definition of mbedtls_aes_context.buf
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100427 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 "2: \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200429 AESKEYGENA xmm1_xmm2 ",0x01 \n\tcall 1b \n\t"
430 AESKEYGENA xmm1_xmm2 ",0x02 \n\tcall 1b \n\t"
431 AESKEYGENA xmm1_xmm2 ",0x04 \n\tcall 1b \n\t"
432 AESKEYGENA xmm1_xmm2 ",0x08 \n\tcall 1b \n\t"
433 AESKEYGENA xmm1_xmm2 ",0x10 \n\tcall 1b \n\t"
434 AESKEYGENA xmm1_xmm2 ",0x20 \n\tcall 1b \n\t"
435 AESKEYGENA xmm1_xmm2 ",0x40 \n\tcall 1b \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100436 :
437 : "r" (rk), "r" (key)
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 : "memory", "cc", "0");
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100439}
440
441/*
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100442 * Key expansion, wrapper
443 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100444int mbedtls_aesni_setkey_enc(unsigned char *rk,
445 const unsigned char *key,
446 size_t bits)
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100447{
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 switch (bits) {
449 case 128: aesni_setkey_enc_128(rk, key); break;
450 case 192: aesni_setkey_enc_192(rk, key); break;
451 case 256: aesni_setkey_enc_256(rk, key); break;
452 default: return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100453 }
454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 return 0;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100456}
457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458#endif /* MBEDTLS_HAVE_X86_64 */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460#endif /* MBEDTLS_AESNI_C */