blob: 44bd89cba9cbb4fb68b566405bb3a6f4ad0459f9 [file] [log] [blame]
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +01001/*
2 * AES-NI support functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010047 */
48
49/*
50 * [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 +010051 * [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 +010052 */
53
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020056#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020058#endif
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_AESNI_C)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010061
Gilles Peskine5053efd2018-04-05 15:25:50 +020062#if defined(__has_feature)
63#if __has_feature(memory_sanitizer)
64#warning "MBEDTLS_AESNI_C is known to cause spurious error reports with some memory sanitizers as they do not understand the assembly code."
65#endif
66#endif
67
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000068#include "mbedtls/aesni.h"
Rich Evans00ab4702015-02-06 13:43:58 +000069
70#include <string.h>
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010071
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020072#ifndef asm
73#define asm __asm
74#endif
75
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#if defined(MBEDTLS_HAVE_X86_64)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010077
78/*
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010079 * AES-NI support detection routine
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010080 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +010081int mbedtls_aesni_has_support( unsigned int what )
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010082{
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010083 static int done = 0;
84 static unsigned int c = 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010085
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010086 if( ! done )
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010087 {
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +020088 asm( "movl $1, %%eax \n\t"
89 "cpuid \n\t"
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010090 : "=c" (c)
91 :
92 : "eax", "ebx", "edx" );
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010093 done = 1;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010094 }
95
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010096 return( ( c & what ) != 0 );
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010097}
98
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010099/*
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +0200100 * Binutils needs to be at least 2.19 to support AES-NI instructions.
101 * Unfortunately, a lot of users have a lower version now (2014-04).
102 * Emit bytecode directly in order to support "old" version of gas.
103 *
104 * Opcodes from the Intel architecture reference manual, vol. 3.
105 * We always use registers, so we don't need prefixes for memory operands.
106 * Operand macros are in gas order (src, dst) as opposed to Intel order
107 * (dst, src) in order to blend better into the surrounding assembly code.
108 */
109#define AESDEC ".byte 0x66,0x0F,0x38,0xDE,"
110#define AESDECLAST ".byte 0x66,0x0F,0x38,0xDF,"
111#define AESENC ".byte 0x66,0x0F,0x38,0xDC,"
112#define AESENCLAST ".byte 0x66,0x0F,0x38,0xDD,"
113#define AESIMC ".byte 0x66,0x0F,0x38,0xDB,"
114#define AESKEYGENA ".byte 0x66,0x0F,0x3A,0xDF,"
115#define PCLMULQDQ ".byte 0x66,0x0F,0x3A,0x44,"
116
117#define xmm0_xmm0 "0xC0"
118#define xmm0_xmm1 "0xC8"
119#define xmm0_xmm2 "0xD0"
120#define xmm0_xmm3 "0xD8"
121#define xmm0_xmm4 "0xE0"
122#define xmm1_xmm0 "0xC1"
123#define xmm1_xmm2 "0xD1"
124
125/*
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100126 * AES-NI AES-ECB block en(de)cryption
127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128int mbedtls_aesni_crypt_ecb( mbedtls_aes_context *ctx,
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100129 int mode,
130 const unsigned char input[16],
131 unsigned char output[16] )
132{
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200133 asm( "movdqu (%3), %%xmm0 \n\t" // load input
134 "movdqu (%1), %%xmm1 \n\t" // load round key 0
135 "pxor %%xmm1, %%xmm0 \n\t" // round 0
James Cowgill6c8edca2015-12-17 01:40:26 +0000136 "add $16, %1 \n\t" // point to next round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200137 "subl $1, %0 \n\t" // normal rounds = nr - 1
138 "test %2, %2 \n\t" // mode?
139 "jz 2f \n\t" // 0 = decrypt
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100140
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200141 "1: \n\t" // encryption loop
142 "movdqu (%1), %%xmm1 \n\t" // load round key
143 AESENC xmm1_xmm0 "\n\t" // do round
James Cowgill6c8edca2015-12-17 01:40:26 +0000144 "add $16, %1 \n\t" // point to next round key
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200145 "subl $1, %0 \n\t" // loop
146 "jnz 1b \n\t"
147 "movdqu (%1), %%xmm1 \n\t" // load round key
148 AESENCLAST xmm1_xmm0 "\n\t" // last round
149 "jmp 3f \n\t"
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100150
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200151 "2: \n\t" // decryption loop
152 "movdqu (%1), %%xmm1 \n\t"
153 AESDEC xmm1_xmm0 "\n\t" // do round
James Cowgill6c8edca2015-12-17 01:40:26 +0000154 "add $16, %1 \n\t"
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200155 "subl $1, %0 \n\t"
156 "jnz 2b \n\t"
157 "movdqu (%1), %%xmm1 \n\t" // load round key
158 AESDECLAST xmm1_xmm0 "\n\t" // last round
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100159
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200160 "3: \n\t"
161 "movdqu %%xmm0, (%4) \n\t" // export output
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100162 :
163 : "r" (ctx->nr), "r" (ctx->rk), "r" (mode), "r" (input), "r" (output)
164 : "memory", "cc", "xmm0", "xmm1" );
165
166
167 return( 0 );
168}
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100169
170/*
171 * GCM multiplication: c = a times b in GF(2^128)
172 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174void mbedtls_aesni_gcm_mult( unsigned char c[16],
Manuel Pégourié-Gonnardd4588cf2013-12-30 13:54:23 +0100175 const unsigned char a[16],
176 const unsigned char b[16] )
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100177{
178 unsigned char aa[16], bb[16], cc[16];
179 size_t i;
180
181 /* The inputs are in big-endian order, so byte-reverse them */
182 for( i = 0; i < 16; i++ )
183 {
184 aa[i] = a[15 - i];
185 bb[i] = b[15 - i];
186 }
187
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200188 asm( "movdqu (%0), %%xmm0 \n\t" // a1:a0
189 "movdqu (%1), %%xmm1 \n\t" // b1:b0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100190
191 /*
192 * Caryless multiplication xmm2:xmm1 = xmm0 * xmm1
193 * using [CLMUL-WP] algorithm 1 (p. 13).
194 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200195 "movdqa %%xmm1, %%xmm2 \n\t" // copy of b1:b0
196 "movdqa %%xmm1, %%xmm3 \n\t" // same
197 "movdqa %%xmm1, %%xmm4 \n\t" // same
198 PCLMULQDQ xmm0_xmm1 ",0x00 \n\t" // a0*b0 = c1:c0
199 PCLMULQDQ xmm0_xmm2 ",0x11 \n\t" // a1*b1 = d1:d0
200 PCLMULQDQ xmm0_xmm3 ",0x10 \n\t" // a0*b1 = e1:e0
201 PCLMULQDQ xmm0_xmm4 ",0x01 \n\t" // a1*b0 = f1:f0
202 "pxor %%xmm3, %%xmm4 \n\t" // e1+f1:e0+f0
203 "movdqa %%xmm4, %%xmm3 \n\t" // same
204 "psrldq $8, %%xmm4 \n\t" // 0:e1+f1
205 "pslldq $8, %%xmm3 \n\t" // e0+f0:0
206 "pxor %%xmm4, %%xmm2 \n\t" // d1:d0+e1+f1
207 "pxor %%xmm3, %%xmm1 \n\t" // c1+e0+f1:c0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100208
209 /*
210 * Now shift the result one bit to the left,
211 * taking advantage of [CLMUL-WP] eq 27 (p. 20)
212 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200213 "movdqa %%xmm1, %%xmm3 \n\t" // r1:r0
214 "movdqa %%xmm2, %%xmm4 \n\t" // r3:r2
215 "psllq $1, %%xmm1 \n\t" // r1<<1:r0<<1
216 "psllq $1, %%xmm2 \n\t" // r3<<1:r2<<1
217 "psrlq $63, %%xmm3 \n\t" // r1>>63:r0>>63
218 "psrlq $63, %%xmm4 \n\t" // r3>>63:r2>>63
219 "movdqa %%xmm3, %%xmm5 \n\t" // r1>>63:r0>>63
220 "pslldq $8, %%xmm3 \n\t" // r0>>63:0
221 "pslldq $8, %%xmm4 \n\t" // r2>>63:0
222 "psrldq $8, %%xmm5 \n\t" // 0:r1>>63
223 "por %%xmm3, %%xmm1 \n\t" // r1<<1|r0>>63:r0<<1
224 "por %%xmm4, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1
225 "por %%xmm5, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1|r1>>63
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100226
227 /*
228 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
229 * using [CLMUL-WP] algorithm 5 (p. 20).
230 * Currently xmm2:xmm1 holds x3:x2:x1:x0 (already shifted).
231 */
232 /* Step 2 (1) */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200233 "movdqa %%xmm1, %%xmm3 \n\t" // x1:x0
234 "movdqa %%xmm1, %%xmm4 \n\t" // same
235 "movdqa %%xmm1, %%xmm5 \n\t" // same
236 "psllq $63, %%xmm3 \n\t" // x1<<63:x0<<63 = stuff:a
237 "psllq $62, %%xmm4 \n\t" // x1<<62:x0<<62 = stuff:b
238 "psllq $57, %%xmm5 \n\t" // x1<<57:x0<<57 = stuff:c
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100239
240 /* Step 2 (2) */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200241 "pxor %%xmm4, %%xmm3 \n\t" // stuff:a+b
242 "pxor %%xmm5, %%xmm3 \n\t" // stuff:a+b+c
243 "pslldq $8, %%xmm3 \n\t" // a+b+c:0
244 "pxor %%xmm3, %%xmm1 \n\t" // x1+a+b+c:x0 = d:x0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100245
246 /* Steps 3 and 4 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200247 "movdqa %%xmm1,%%xmm0 \n\t" // d:x0
248 "movdqa %%xmm1,%%xmm4 \n\t" // same
249 "movdqa %%xmm1,%%xmm5 \n\t" // same
250 "psrlq $1, %%xmm0 \n\t" // e1:x0>>1 = e1:e0'
251 "psrlq $2, %%xmm4 \n\t" // f1:x0>>2 = f1:f0'
252 "psrlq $7, %%xmm5 \n\t" // g1:x0>>7 = g1:g0'
253 "pxor %%xmm4, %%xmm0 \n\t" // e1+f1:e0'+f0'
254 "pxor %%xmm5, %%xmm0 \n\t" // e1+f1+g1:e0'+f0'+g0'
255 // e0'+f0'+g0' is almost e0+f0+g0, ex\tcept for some missing
256 // bits carried from d. Now get those\t bits back in.
257 "movdqa %%xmm1,%%xmm3 \n\t" // d:x0
258 "movdqa %%xmm1,%%xmm4 \n\t" // same
259 "movdqa %%xmm1,%%xmm5 \n\t" // same
260 "psllq $63, %%xmm3 \n\t" // d<<63:stuff
261 "psllq $62, %%xmm4 \n\t" // d<<62:stuff
262 "psllq $57, %%xmm5 \n\t" // d<<57:stuff
263 "pxor %%xmm4, %%xmm3 \n\t" // d<<63+d<<62:stuff
264 "pxor %%xmm5, %%xmm3 \n\t" // missing bits of d:stuff
265 "psrldq $8, %%xmm3 \n\t" // 0:missing bits of d
266 "pxor %%xmm3, %%xmm0 \n\t" // e1+f1+g1:e0+f0+g0
267 "pxor %%xmm1, %%xmm0 \n\t" // h1:h0
268 "pxor %%xmm2, %%xmm0 \n\t" // x3+h1:x2+h0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100269
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200270 "movdqu %%xmm0, (%2) \n\t" // done
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100271 :
272 : "r" (aa), "r" (bb), "r" (cc)
273 : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5" );
274
275 /* Now byte-reverse the outputs */
276 for( i = 0; i < 16; i++ )
277 c[i] = cc[15 - i];
278
Paul Bakkere7f51332013-12-30 15:32:02 +0100279 return;
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100280}
281
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100282/*
283 * Compute decryption round keys from encryption round keys
284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285void mbedtls_aesni_inverse_key( unsigned char *invkey,
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100286 const unsigned char *fwdkey, int nr )
287{
288 unsigned char *ik = invkey;
289 const unsigned char *fk = fwdkey + 16 * nr;
290
291 memcpy( ik, fk, 16 );
292
293 for( fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16 )
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200294 asm( "movdqu (%0), %%xmm0 \n\t"
295 AESIMC xmm0_xmm0 "\n\t"
296 "movdqu %%xmm0, (%1) \n\t"
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100297 :
298 : "r" (fk), "r" (ik)
299 : "memory", "xmm0" );
300
301 memcpy( ik, fk, 16 );
302}
303
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100304/*
305 * Key expansion, 128-bit case
306 */
Paul Bakker93759b02013-12-30 19:20:06 +0100307static void aesni_setkey_enc_128( unsigned char *rk,
308 const unsigned char *key )
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100309{
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200310 asm( "movdqu (%1), %%xmm0 \n\t" // copy the original key
311 "movdqu %%xmm0, (%0) \n\t" // as round key 0
312 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100313
314 /*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100315 * Finish generating the next round key.
316 *
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100317 * On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff
318 * with X = rot( sub( r3 ) ) ^ RCON.
319 *
320 * On exit, xmm0 is r7:r6:r5:r4
321 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
322 * and those are written to the round key buffer.
323 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200324 "1: \n\t"
325 "pshufd $0xff, %%xmm1, %%xmm1 \n\t" // X:X:X:X
326 "pxor %%xmm0, %%xmm1 \n\t" // X+r3:X+r2:X+r1:r4
327 "pslldq $4, %%xmm0 \n\t" // r2:r1:r0:0
328 "pxor %%xmm0, %%xmm1 \n\t" // X+r3+r2:X+r2+r1:r5:r4
329 "pslldq $4, %%xmm0 \n\t" // etc
330 "pxor %%xmm0, %%xmm1 \n\t"
331 "pslldq $4, %%xmm0 \n\t"
332 "pxor %%xmm1, %%xmm0 \n\t" // update xmm0 for next time!
333 "add $16, %0 \n\t" // point to next round key
334 "movdqu %%xmm0, (%0) \n\t" // write it
335 "ret \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100336
337 /* Main "loop" */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200338 "2: \n\t"
339 AESKEYGENA xmm0_xmm1 ",0x01 \n\tcall 1b \n\t"
340 AESKEYGENA xmm0_xmm1 ",0x02 \n\tcall 1b \n\t"
341 AESKEYGENA xmm0_xmm1 ",0x04 \n\tcall 1b \n\t"
342 AESKEYGENA xmm0_xmm1 ",0x08 \n\tcall 1b \n\t"
343 AESKEYGENA xmm0_xmm1 ",0x10 \n\tcall 1b \n\t"
344 AESKEYGENA xmm0_xmm1 ",0x20 \n\tcall 1b \n\t"
345 AESKEYGENA xmm0_xmm1 ",0x40 \n\tcall 1b \n\t"
346 AESKEYGENA xmm0_xmm1 ",0x80 \n\tcall 1b \n\t"
347 AESKEYGENA xmm0_xmm1 ",0x1B \n\tcall 1b \n\t"
348 AESKEYGENA xmm0_xmm1 ",0x36 \n\tcall 1b \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100349 :
350 : "r" (rk), "r" (key)
351 : "memory", "cc", "0" );
352}
353
354/*
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100355 * Key expansion, 192-bit case
356 */
Paul Bakker93759b02013-12-30 19:20:06 +0100357static void aesni_setkey_enc_192( unsigned char *rk,
358 const unsigned char *key )
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100359{
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200360 asm( "movdqu (%1), %%xmm0 \n\t" // copy original round key
361 "movdqu %%xmm0, (%0) \n\t"
362 "add $16, %0 \n\t"
363 "movq 16(%1), %%xmm1 \n\t"
364 "movq %%xmm1, (%0) \n\t"
365 "add $8, %0 \n\t"
366 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100367
368 /*
369 * Finish generating the next 6 quarter-keys.
370 *
371 * On entry xmm0 is r3:r2:r1:r0, xmm1 is stuff:stuff:r5:r4
372 * and xmm2 is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON.
373 *
374 * On exit, xmm0 is r9:r8:r7:r6 and xmm1 is stuff:stuff:r11:r10
375 * and those are written to the round key buffer.
376 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200377 "1: \n\t"
378 "pshufd $0x55, %%xmm2, %%xmm2 \n\t" // X:X:X:X
379 "pxor %%xmm0, %%xmm2 \n\t" // X+r3:X+r2:X+r1:r4
380 "pslldq $4, %%xmm0 \n\t" // etc
381 "pxor %%xmm0, %%xmm2 \n\t"
382 "pslldq $4, %%xmm0 \n\t"
383 "pxor %%xmm0, %%xmm2 \n\t"
384 "pslldq $4, %%xmm0 \n\t"
385 "pxor %%xmm2, %%xmm0 \n\t" // update xmm0 = r9:r8:r7:r6
386 "movdqu %%xmm0, (%0) \n\t"
387 "add $16, %0 \n\t"
388 "pshufd $0xff, %%xmm0, %%xmm2 \n\t" // r9:r9:r9:r9
389 "pxor %%xmm1, %%xmm2 \n\t" // stuff:stuff:r9+r5:r10
390 "pslldq $4, %%xmm1 \n\t" // r2:r1:r0:0
391 "pxor %%xmm2, %%xmm1 \n\t" // xmm1 = stuff:stuff:r11:r10
392 "movq %%xmm1, (%0) \n\t"
393 "add $8, %0 \n\t"
394 "ret \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100395
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200396 "2: \n\t"
397 AESKEYGENA xmm1_xmm2 ",0x01 \n\tcall 1b \n\t"
398 AESKEYGENA xmm1_xmm2 ",0x02 \n\tcall 1b \n\t"
399 AESKEYGENA xmm1_xmm2 ",0x04 \n\tcall 1b \n\t"
400 AESKEYGENA xmm1_xmm2 ",0x08 \n\tcall 1b \n\t"
401 AESKEYGENA xmm1_xmm2 ",0x10 \n\tcall 1b \n\t"
402 AESKEYGENA xmm1_xmm2 ",0x20 \n\tcall 1b \n\t"
403 AESKEYGENA xmm1_xmm2 ",0x40 \n\tcall 1b \n\t"
404 AESKEYGENA xmm1_xmm2 ",0x80 \n\tcall 1b \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100405
406 :
407 : "r" (rk), "r" (key)
408 : "memory", "cc", "0" );
409}
410
411/*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100412 * Key expansion, 256-bit case
413 */
Paul Bakker93759b02013-12-30 19:20:06 +0100414static void aesni_setkey_enc_256( unsigned char *rk,
415 const unsigned char *key )
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100416{
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200417 asm( "movdqu (%1), %%xmm0 \n\t"
418 "movdqu %%xmm0, (%0) \n\t"
419 "add $16, %0 \n\t"
420 "movdqu 16(%1), %%xmm1 \n\t"
421 "movdqu %%xmm1, (%0) \n\t"
422 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100423
424 /*
425 * Finish generating the next two round keys.
426 *
427 * On entry xmm0 is r3:r2:r1:r0, xmm1 is r7:r6:r5:r4 and
428 * xmm2 is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
429 *
430 * On exit, xmm0 is r11:r10:r9:r8 and xmm1 is r15:r14:r13:r12
431 * and those have been written to the output buffer.
432 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200433 "1: \n\t"
434 "pshufd $0xff, %%xmm2, %%xmm2 \n\t"
435 "pxor %%xmm0, %%xmm2 \n\t"
436 "pslldq $4, %%xmm0 \n\t"
437 "pxor %%xmm0, %%xmm2 \n\t"
438 "pslldq $4, %%xmm0 \n\t"
439 "pxor %%xmm0, %%xmm2 \n\t"
440 "pslldq $4, %%xmm0 \n\t"
441 "pxor %%xmm2, %%xmm0 \n\t"
442 "add $16, %0 \n\t"
443 "movdqu %%xmm0, (%0) \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100444
445 /* Set xmm2 to stuff:Y:stuff:stuff with Y = subword( r11 )
446 * and proceed to generate next round key from there */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200447 AESKEYGENA xmm0_xmm2 ",0x00 \n\t"
448 "pshufd $0xaa, %%xmm2, %%xmm2 \n\t"
449 "pxor %%xmm1, %%xmm2 \n\t"
450 "pslldq $4, %%xmm1 \n\t"
451 "pxor %%xmm1, %%xmm2 \n\t"
452 "pslldq $4, %%xmm1 \n\t"
453 "pxor %%xmm1, %%xmm2 \n\t"
454 "pslldq $4, %%xmm1 \n\t"
455 "pxor %%xmm2, %%xmm1 \n\t"
456 "add $16, %0 \n\t"
457 "movdqu %%xmm1, (%0) \n\t"
458 "ret \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100459
460 /*
461 * Main "loop" - Generating one more key than necessary,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 * see definition of mbedtls_aes_context.buf
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100463 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200464 "2: \n\t"
465 AESKEYGENA xmm1_xmm2 ",0x01 \n\tcall 1b \n\t"
466 AESKEYGENA xmm1_xmm2 ",0x02 \n\tcall 1b \n\t"
467 AESKEYGENA xmm1_xmm2 ",0x04 \n\tcall 1b \n\t"
468 AESKEYGENA xmm1_xmm2 ",0x08 \n\tcall 1b \n\t"
469 AESKEYGENA xmm1_xmm2 ",0x10 \n\tcall 1b \n\t"
470 AESKEYGENA xmm1_xmm2 ",0x20 \n\tcall 1b \n\t"
471 AESKEYGENA xmm1_xmm2 ",0x40 \n\tcall 1b \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100472 :
473 : "r" (rk), "r" (key)
474 : "memory", "cc", "0" );
475}
476
477/*
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100478 * Key expansion, wrapper
479 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480int mbedtls_aesni_setkey_enc( unsigned char *rk,
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100481 const unsigned char *key,
482 size_t bits )
483{
484 switch( bits )
485 {
486 case 128: aesni_setkey_enc_128( rk, key ); break;
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100487 case 192: aesni_setkey_enc_192( rk, key ); break;
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100488 case 256: aesni_setkey_enc_256( rk, key ); break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 default : return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100490 }
491
492 return( 0 );
493}
494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495#endif /* MBEDTLS_HAVE_X86_64 */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497#endif /* MBEDTLS_AESNI_C */