blob: 2165de77eabcd4d59439056e04bc986a40a823b6 [file] [log] [blame]
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +01001/*
2 * AES-NI support functions
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +01005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +01007 *
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +01008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23/*
24 * [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 +010025 * [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 +010026 */
27
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#endif
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_AESNI_C)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010035
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/aesni.h"
Rich Evans00ab4702015-02-06 13:43:58 +000037
38#include <string.h>
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010039
Manuel Pégourié-Gonnardba194322015-05-29 09:47:57 +020040#ifndef asm
41#define asm __asm
42#endif
43
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_HAVE_X86_64)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010045
46/*
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010047 * AES-NI support detection routine
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010048 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +010049int mbedtls_aesni_has_support( unsigned int what )
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010050{
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010051 static int done = 0;
52 static unsigned int c = 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010053
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010054 if( ! done )
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010055 {
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +020056 asm( "movl $1, %%eax \n\t"
57 "cpuid \n\t"
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010058 : "=c" (c)
59 :
60 : "eax", "ebx", "edx" );
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010061 done = 1;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010062 }
63
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010064 return( ( c & what ) != 0 );
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010065}
66
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010067/*
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +020068 * Binutils needs to be at least 2.19 to support AES-NI instructions.
69 * Unfortunately, a lot of users have a lower version now (2014-04).
70 * Emit bytecode directly in order to support "old" version of gas.
71 *
72 * Opcodes from the Intel architecture reference manual, vol. 3.
73 * We always use registers, so we don't need prefixes for memory operands.
74 * Operand macros are in gas order (src, dst) as opposed to Intel order
75 * (dst, src) in order to blend better into the surrounding assembly code.
76 */
77#define AESDEC ".byte 0x66,0x0F,0x38,0xDE,"
78#define AESDECLAST ".byte 0x66,0x0F,0x38,0xDF,"
79#define AESENC ".byte 0x66,0x0F,0x38,0xDC,"
80#define AESENCLAST ".byte 0x66,0x0F,0x38,0xDD,"
81#define AESIMC ".byte 0x66,0x0F,0x38,0xDB,"
82#define AESKEYGENA ".byte 0x66,0x0F,0x3A,0xDF,"
83#define PCLMULQDQ ".byte 0x66,0x0F,0x3A,0x44,"
84
85#define xmm0_xmm0 "0xC0"
86#define xmm0_xmm1 "0xC8"
87#define xmm0_xmm2 "0xD0"
88#define xmm0_xmm3 "0xD8"
89#define xmm0_xmm4 "0xE0"
90#define xmm1_xmm0 "0xC1"
91#define xmm1_xmm2 "0xD1"
92
93/*
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010094 * AES-NI AES-ECB block en(de)cryption
95 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096int mbedtls_aesni_crypt_ecb( mbedtls_aes_context *ctx,
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010097 int mode,
98 const unsigned char input[16],
99 unsigned char output[16] )
100{
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200101 asm( "movdqu (%3), %%xmm0 \n\t" // load input
102 "movdqu (%1), %%xmm1 \n\t" // load round key 0
103 "pxor %%xmm1, %%xmm0 \n\t" // round 0
104 "addq $16, %1 \n\t" // point to next round key
105 "subl $1, %0 \n\t" // normal rounds = nr - 1
106 "test %2, %2 \n\t" // mode?
107 "jz 2f \n\t" // 0 = decrypt
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100108
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200109 "1: \n\t" // encryption loop
110 "movdqu (%1), %%xmm1 \n\t" // load round key
111 AESENC xmm1_xmm0 "\n\t" // do round
112 "addq $16, %1 \n\t" // point to next round key
113 "subl $1, %0 \n\t" // loop
114 "jnz 1b \n\t"
115 "movdqu (%1), %%xmm1 \n\t" // load round key
116 AESENCLAST xmm1_xmm0 "\n\t" // last round
117 "jmp 3f \n\t"
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100118
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200119 "2: \n\t" // decryption loop
120 "movdqu (%1), %%xmm1 \n\t"
121 AESDEC xmm1_xmm0 "\n\t" // do round
122 "addq $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
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200128 "3: \n\t"
129 "movdqu %%xmm0, (%4) \n\t" // export output
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100130 :
131 : "r" (ctx->nr), "r" (ctx->rk), "r" (mode), "r" (input), "r" (output)
132 : "memory", "cc", "xmm0", "xmm1" );
133
134
135 return( 0 );
136}
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100137
138/*
139 * GCM multiplication: c = a times b in GF(2^128)
140 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142void mbedtls_aesni_gcm_mult( unsigned char c[16],
Manuel Pégourié-Gonnardd4588cf2013-12-30 13:54:23 +0100143 const unsigned char a[16],
144 const unsigned char b[16] )
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100145{
146 unsigned char aa[16], bb[16], cc[16];
147 size_t i;
148
149 /* The inputs are in big-endian order, so byte-reverse them */
150 for( i = 0; i < 16; i++ )
151 {
152 aa[i] = a[15 - i];
153 bb[i] = b[15 - i];
154 }
155
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200156 asm( "movdqu (%0), %%xmm0 \n\t" // a1:a0
157 "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
170 "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 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200181 "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) */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200201 "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) */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200209 "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 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200215 "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'
223 // 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.
225 "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
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200238 "movdqu %%xmm0, (%2) \n\t" // done
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100239 :
240 : "r" (aa), "r" (bb), "r" (cc)
241 : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5" );
242
243 /* Now byte-reverse the outputs */
244 for( i = 0; i < 16; i++ )
245 c[i] = cc[15 - i];
246
Paul Bakkere7f51332013-12-30 15:32:02 +0100247 return;
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100248}
249
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100250/*
251 * Compute decryption round keys from encryption round keys
252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253void mbedtls_aesni_inverse_key( unsigned char *invkey,
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100254 const unsigned char *fwdkey, int nr )
255{
256 unsigned char *ik = invkey;
257 const unsigned char *fk = fwdkey + 16 * nr;
258
259 memcpy( ik, fk, 16 );
260
261 for( fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16 )
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200262 asm( "movdqu (%0), %%xmm0 \n\t"
263 AESIMC xmm0_xmm0 "\n\t"
264 "movdqu %%xmm0, (%1) \n\t"
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100265 :
266 : "r" (fk), "r" (ik)
267 : "memory", "xmm0" );
268
269 memcpy( ik, fk, 16 );
270}
271
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100272/*
273 * Key expansion, 128-bit case
274 */
Paul Bakker93759b02013-12-30 19:20:06 +0100275static void aesni_setkey_enc_128( unsigned char *rk,
276 const unsigned char *key )
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100277{
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200278 asm( "movdqu (%1), %%xmm0 \n\t" // copy the original key
279 "movdqu %%xmm0, (%0) \n\t" // as round key 0
280 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100281
282 /*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100283 * Finish generating the next round key.
284 *
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100285 * On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff
286 * with X = rot( sub( r3 ) ) ^ RCON.
287 *
288 * On exit, xmm0 is r7:r6:r5:r4
289 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
290 * and those are written to the round key buffer.
291 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200292 "1: \n\t"
293 "pshufd $0xff, %%xmm1, %%xmm1 \n\t" // X:X:X:X
294 "pxor %%xmm0, %%xmm1 \n\t" // X+r3:X+r2:X+r1:r4
295 "pslldq $4, %%xmm0 \n\t" // r2:r1:r0:0
296 "pxor %%xmm0, %%xmm1 \n\t" // X+r3+r2:X+r2+r1:r5:r4
297 "pslldq $4, %%xmm0 \n\t" // etc
298 "pxor %%xmm0, %%xmm1 \n\t"
299 "pslldq $4, %%xmm0 \n\t"
300 "pxor %%xmm1, %%xmm0 \n\t" // update xmm0 for next time!
301 "add $16, %0 \n\t" // point to next round key
302 "movdqu %%xmm0, (%0) \n\t" // write it
303 "ret \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100304
305 /* Main "loop" */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200306 "2: \n\t"
307 AESKEYGENA xmm0_xmm1 ",0x01 \n\tcall 1b \n\t"
308 AESKEYGENA xmm0_xmm1 ",0x02 \n\tcall 1b \n\t"
309 AESKEYGENA xmm0_xmm1 ",0x04 \n\tcall 1b \n\t"
310 AESKEYGENA xmm0_xmm1 ",0x08 \n\tcall 1b \n\t"
311 AESKEYGENA xmm0_xmm1 ",0x10 \n\tcall 1b \n\t"
312 AESKEYGENA xmm0_xmm1 ",0x20 \n\tcall 1b \n\t"
313 AESKEYGENA xmm0_xmm1 ",0x40 \n\tcall 1b \n\t"
314 AESKEYGENA xmm0_xmm1 ",0x80 \n\tcall 1b \n\t"
315 AESKEYGENA xmm0_xmm1 ",0x1B \n\tcall 1b \n\t"
316 AESKEYGENA xmm0_xmm1 ",0x36 \n\tcall 1b \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100317 :
318 : "r" (rk), "r" (key)
319 : "memory", "cc", "0" );
320}
321
322/*
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100323 * Key expansion, 192-bit case
324 */
Paul Bakker93759b02013-12-30 19:20:06 +0100325static void aesni_setkey_enc_192( unsigned char *rk,
326 const unsigned char *key )
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100327{
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200328 asm( "movdqu (%1), %%xmm0 \n\t" // copy original round key
329 "movdqu %%xmm0, (%0) \n\t"
330 "add $16, %0 \n\t"
331 "movq 16(%1), %%xmm1 \n\t"
332 "movq %%xmm1, (%0) \n\t"
333 "add $8, %0 \n\t"
334 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100335
336 /*
337 * Finish generating the next 6 quarter-keys.
338 *
339 * On entry xmm0 is r3:r2:r1:r0, xmm1 is stuff:stuff:r5:r4
340 * and xmm2 is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON.
341 *
342 * On exit, xmm0 is r9:r8:r7:r6 and xmm1 is stuff:stuff:r11:r10
343 * and those are written to the round key buffer.
344 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200345 "1: \n\t"
346 "pshufd $0x55, %%xmm2, %%xmm2 \n\t" // X:X:X:X
347 "pxor %%xmm0, %%xmm2 \n\t" // X+r3:X+r2:X+r1:r4
348 "pslldq $4, %%xmm0 \n\t" // etc
349 "pxor %%xmm0, %%xmm2 \n\t"
350 "pslldq $4, %%xmm0 \n\t"
351 "pxor %%xmm0, %%xmm2 \n\t"
352 "pslldq $4, %%xmm0 \n\t"
353 "pxor %%xmm2, %%xmm0 \n\t" // update xmm0 = r9:r8:r7:r6
354 "movdqu %%xmm0, (%0) \n\t"
355 "add $16, %0 \n\t"
356 "pshufd $0xff, %%xmm0, %%xmm2 \n\t" // r9:r9:r9:r9
357 "pxor %%xmm1, %%xmm2 \n\t" // stuff:stuff:r9+r5:r10
358 "pslldq $4, %%xmm1 \n\t" // r2:r1:r0:0
359 "pxor %%xmm2, %%xmm1 \n\t" // xmm1 = stuff:stuff:r11:r10
360 "movq %%xmm1, (%0) \n\t"
361 "add $8, %0 \n\t"
362 "ret \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100363
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200364 "2: \n\t"
365 AESKEYGENA xmm1_xmm2 ",0x01 \n\tcall 1b \n\t"
366 AESKEYGENA xmm1_xmm2 ",0x02 \n\tcall 1b \n\t"
367 AESKEYGENA xmm1_xmm2 ",0x04 \n\tcall 1b \n\t"
368 AESKEYGENA xmm1_xmm2 ",0x08 \n\tcall 1b \n\t"
369 AESKEYGENA xmm1_xmm2 ",0x10 \n\tcall 1b \n\t"
370 AESKEYGENA xmm1_xmm2 ",0x20 \n\tcall 1b \n\t"
371 AESKEYGENA xmm1_xmm2 ",0x40 \n\tcall 1b \n\t"
372 AESKEYGENA xmm1_xmm2 ",0x80 \n\tcall 1b \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100373
374 :
375 : "r" (rk), "r" (key)
376 : "memory", "cc", "0" );
377}
378
379/*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100380 * Key expansion, 256-bit case
381 */
Paul Bakker93759b02013-12-30 19:20:06 +0100382static void aesni_setkey_enc_256( unsigned char *rk,
383 const unsigned char *key )
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100384{
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200385 asm( "movdqu (%1), %%xmm0 \n\t"
386 "movdqu %%xmm0, (%0) \n\t"
387 "add $16, %0 \n\t"
388 "movdqu 16(%1), %%xmm1 \n\t"
389 "movdqu %%xmm1, (%0) \n\t"
390 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100391
392 /*
393 * Finish generating the next two round keys.
394 *
395 * On entry xmm0 is r3:r2:r1:r0, xmm1 is r7:r6:r5:r4 and
396 * xmm2 is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
397 *
398 * On exit, xmm0 is r11:r10:r9:r8 and xmm1 is r15:r14:r13:r12
399 * and those have been written to the output buffer.
400 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200401 "1: \n\t"
402 "pshufd $0xff, %%xmm2, %%xmm2 \n\t"
403 "pxor %%xmm0, %%xmm2 \n\t"
404 "pslldq $4, %%xmm0 \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 %%xmm2, %%xmm0 \n\t"
410 "add $16, %0 \n\t"
411 "movdqu %%xmm0, (%0) \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100412
413 /* Set xmm2 to stuff:Y:stuff:stuff with Y = subword( r11 )
414 * and proceed to generate next round key from there */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200415 AESKEYGENA xmm0_xmm2 ",0x00 \n\t"
416 "pshufd $0xaa, %%xmm2, %%xmm2 \n\t"
417 "pxor %%xmm1, %%xmm2 \n\t"
418 "pslldq $4, %%xmm1 \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 %%xmm2, %%xmm1 \n\t"
424 "add $16, %0 \n\t"
425 "movdqu %%xmm1, (%0) \n\t"
426 "ret \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100427
428 /*
429 * Main "loop" - Generating one more key than necessary,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 * see definition of mbedtls_aes_context.buf
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100431 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200432 "2: \n\t"
433 AESKEYGENA xmm1_xmm2 ",0x01 \n\tcall 1b \n\t"
434 AESKEYGENA xmm1_xmm2 ",0x02 \n\tcall 1b \n\t"
435 AESKEYGENA xmm1_xmm2 ",0x04 \n\tcall 1b \n\t"
436 AESKEYGENA xmm1_xmm2 ",0x08 \n\tcall 1b \n\t"
437 AESKEYGENA xmm1_xmm2 ",0x10 \n\tcall 1b \n\t"
438 AESKEYGENA xmm1_xmm2 ",0x20 \n\tcall 1b \n\t"
439 AESKEYGENA xmm1_xmm2 ",0x40 \n\tcall 1b \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100440 :
441 : "r" (rk), "r" (key)
442 : "memory", "cc", "0" );
443}
444
445/*
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100446 * Key expansion, wrapper
447 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448int mbedtls_aesni_setkey_enc( unsigned char *rk,
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100449 const unsigned char *key,
450 size_t bits )
451{
452 switch( bits )
453 {
454 case 128: aesni_setkey_enc_128( rk, key ); break;
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100455 case 192: aesni_setkey_enc_192( rk, key ); break;
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100456 case 256: aesni_setkey_enc_256( rk, key ); break;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 default : return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100458 }
459
460 return( 0 );
461}
462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463#endif /* MBEDTLS_HAVE_X86_64 */
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465#endif /* MBEDTLS_AESNI_C */