blob: a235904ee6111294bb614007823b34e124c75c38 [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é-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.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é-Gonnardcef4ad22014-04-29 12:39:06 +020028#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010029#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#else
31#include POLARSSL_CONFIG_FILE
32#endif
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010033
34#if defined(POLARSSL_AESNI_C)
35
36#include "polarssl/aesni.h"
Rich Evans00ab4702015-02-06 13:43:58 +000037
38#include <string.h>
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010039
40#if defined(POLARSSL_HAVE_X86_64)
41
42/*
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010043 * AES-NI support detection routine
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010044 */
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010045int aesni_supports( unsigned int what )
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010046{
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010047 static int done = 0;
48 static unsigned int c = 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010049
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010050 if( ! done )
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010051 {
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +020052 asm( "movl $1, %%eax \n\t"
53 "cpuid \n\t"
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010054 : "=c" (c)
55 :
56 : "eax", "ebx", "edx" );
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010057 done = 1;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010058 }
59
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010060 return( ( c & what ) != 0 );
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010061}
62
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010063/*
Manuel Pégourié-Gonnardb1fd3972014-04-26 17:17:31 +020064 * Binutils needs to be at least 2.19 to support AES-NI instructions.
65 * Unfortunately, a lot of users have a lower version now (2014-04).
66 * Emit bytecode directly in order to support "old" version of gas.
67 *
68 * Opcodes from the Intel architecture reference manual, vol. 3.
69 * We always use registers, so we don't need prefixes for memory operands.
70 * Operand macros are in gas order (src, dst) as opposed to Intel order
71 * (dst, src) in order to blend better into the surrounding assembly code.
72 */
73#define AESDEC ".byte 0x66,0x0F,0x38,0xDE,"
74#define AESDECLAST ".byte 0x66,0x0F,0x38,0xDF,"
75#define AESENC ".byte 0x66,0x0F,0x38,0xDC,"
76#define AESENCLAST ".byte 0x66,0x0F,0x38,0xDD,"
77#define AESIMC ".byte 0x66,0x0F,0x38,0xDB,"
78#define AESKEYGENA ".byte 0x66,0x0F,0x3A,0xDF,"
79#define PCLMULQDQ ".byte 0x66,0x0F,0x3A,0x44,"
80
81#define xmm0_xmm0 "0xC0"
82#define xmm0_xmm1 "0xC8"
83#define xmm0_xmm2 "0xD0"
84#define xmm0_xmm3 "0xD8"
85#define xmm0_xmm4 "0xE0"
86#define xmm1_xmm0 "0xC1"
87#define xmm1_xmm2 "0xD1"
88
89/*
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010090 * AES-NI AES-ECB block en(de)cryption
91 */
92int aesni_crypt_ecb( aes_context *ctx,
93 int mode,
94 const unsigned char input[16],
95 unsigned char output[16] )
96{
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +020097 asm( "movdqu (%3), %%xmm0 \n\t" // load input
98 "movdqu (%1), %%xmm1 \n\t" // load round key 0
99 "pxor %%xmm1, %%xmm0 \n\t" // round 0
100 "addq $16, %1 \n\t" // point to next round key
101 "subl $1, %0 \n\t" // normal rounds = nr - 1
102 "test %2, %2 \n\t" // mode?
103 "jz 2f \n\t" // 0 = decrypt
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100104
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200105 "1: \n\t" // encryption loop
106 "movdqu (%1), %%xmm1 \n\t" // load round key
107 AESENC xmm1_xmm0 "\n\t" // do round
108 "addq $16, %1 \n\t" // point to next round key
109 "subl $1, %0 \n\t" // loop
110 "jnz 1b \n\t"
111 "movdqu (%1), %%xmm1 \n\t" // load round key
112 AESENCLAST xmm1_xmm0 "\n\t" // last round
113 "jmp 3f \n\t"
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100114
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200115 "2: \n\t" // decryption loop
116 "movdqu (%1), %%xmm1 \n\t"
117 AESDEC xmm1_xmm0 "\n\t" // do round
118 "addq $16, %1 \n\t"
119 "subl $1, %0 \n\t"
120 "jnz 2b \n\t"
121 "movdqu (%1), %%xmm1 \n\t" // load round key
122 AESDECLAST xmm1_xmm0 "\n\t" // last round
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100123
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200124 "3: \n\t"
125 "movdqu %%xmm0, (%4) \n\t" // export output
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100126 :
127 : "r" (ctx->nr), "r" (ctx->rk), "r" (mode), "r" (input), "r" (output)
128 : "memory", "cc", "xmm0", "xmm1" );
129
130
131 return( 0 );
132}
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100133
134/*
135 * GCM multiplication: c = a times b in GF(2^128)
136 * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
137 */
Manuel Pégourié-Gonnardd4588cf2013-12-30 13:54:23 +0100138void aesni_gcm_mult( unsigned char c[16],
139 const unsigned char a[16],
140 const unsigned char b[16] )
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100141{
142 unsigned char aa[16], bb[16], cc[16];
143 size_t i;
144
145 /* The inputs are in big-endian order, so byte-reverse them */
146 for( i = 0; i < 16; i++ )
147 {
148 aa[i] = a[15 - i];
149 bb[i] = b[15 - i];
150 }
151
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200152 asm( "movdqu (%0), %%xmm0 \n\t" // a1:a0
153 "movdqu (%1), %%xmm1 \n\t" // b1:b0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100154
155 /*
156 * Caryless multiplication xmm2:xmm1 = xmm0 * xmm1
157 * using [CLMUL-WP] algorithm 1 (p. 13).
158 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200159 "movdqa %%xmm1, %%xmm2 \n\t" // copy of b1:b0
160 "movdqa %%xmm1, %%xmm3 \n\t" // same
161 "movdqa %%xmm1, %%xmm4 \n\t" // same
162 PCLMULQDQ xmm0_xmm1 ",0x00 \n\t" // a0*b0 = c1:c0
163 PCLMULQDQ xmm0_xmm2 ",0x11 \n\t" // a1*b1 = d1:d0
164 PCLMULQDQ xmm0_xmm3 ",0x10 \n\t" // a0*b1 = e1:e0
165 PCLMULQDQ xmm0_xmm4 ",0x01 \n\t" // a1*b0 = f1:f0
166 "pxor %%xmm3, %%xmm4 \n\t" // e1+f1:e0+f0
167 "movdqa %%xmm4, %%xmm3 \n\t" // same
168 "psrldq $8, %%xmm4 \n\t" // 0:e1+f1
169 "pslldq $8, %%xmm3 \n\t" // e0+f0:0
170 "pxor %%xmm4, %%xmm2 \n\t" // d1:d0+e1+f1
171 "pxor %%xmm3, %%xmm1 \n\t" // c1+e0+f1:c0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100172
173 /*
174 * Now shift the result one bit to the left,
175 * taking advantage of [CLMUL-WP] eq 27 (p. 20)
176 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200177 "movdqa %%xmm1, %%xmm3 \n\t" // r1:r0
178 "movdqa %%xmm2, %%xmm4 \n\t" // r3:r2
179 "psllq $1, %%xmm1 \n\t" // r1<<1:r0<<1
180 "psllq $1, %%xmm2 \n\t" // r3<<1:r2<<1
181 "psrlq $63, %%xmm3 \n\t" // r1>>63:r0>>63
182 "psrlq $63, %%xmm4 \n\t" // r3>>63:r2>>63
183 "movdqa %%xmm3, %%xmm5 \n\t" // r1>>63:r0>>63
184 "pslldq $8, %%xmm3 \n\t" // r0>>63:0
185 "pslldq $8, %%xmm4 \n\t" // r2>>63:0
186 "psrldq $8, %%xmm5 \n\t" // 0:r1>>63
187 "por %%xmm3, %%xmm1 \n\t" // r1<<1|r0>>63:r0<<1
188 "por %%xmm4, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1
189 "por %%xmm5, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1|r1>>63
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100190
191 /*
192 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
193 * using [CLMUL-WP] algorithm 5 (p. 20).
194 * Currently xmm2:xmm1 holds x3:x2:x1:x0 (already shifted).
195 */
196 /* Step 2 (1) */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200197 "movdqa %%xmm1, %%xmm3 \n\t" // x1:x0
198 "movdqa %%xmm1, %%xmm4 \n\t" // same
199 "movdqa %%xmm1, %%xmm5 \n\t" // same
200 "psllq $63, %%xmm3 \n\t" // x1<<63:x0<<63 = stuff:a
201 "psllq $62, %%xmm4 \n\t" // x1<<62:x0<<62 = stuff:b
202 "psllq $57, %%xmm5 \n\t" // x1<<57:x0<<57 = stuff:c
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100203
204 /* Step 2 (2) */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200205 "pxor %%xmm4, %%xmm3 \n\t" // stuff:a+b
206 "pxor %%xmm5, %%xmm3 \n\t" // stuff:a+b+c
207 "pslldq $8, %%xmm3 \n\t" // a+b+c:0
208 "pxor %%xmm3, %%xmm1 \n\t" // x1+a+b+c:x0 = d:x0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100209
210 /* Steps 3 and 4 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200211 "movdqa %%xmm1,%%xmm0 \n\t" // d:x0
212 "movdqa %%xmm1,%%xmm4 \n\t" // same
213 "movdqa %%xmm1,%%xmm5 \n\t" // same
214 "psrlq $1, %%xmm0 \n\t" // e1:x0>>1 = e1:e0'
215 "psrlq $2, %%xmm4 \n\t" // f1:x0>>2 = f1:f0'
216 "psrlq $7, %%xmm5 \n\t" // g1:x0>>7 = g1:g0'
217 "pxor %%xmm4, %%xmm0 \n\t" // e1+f1:e0'+f0'
218 "pxor %%xmm5, %%xmm0 \n\t" // e1+f1+g1:e0'+f0'+g0'
219 // e0'+f0'+g0' is almost e0+f0+g0, ex\tcept for some missing
220 // bits carried from d. Now get those\t bits back in.
221 "movdqa %%xmm1,%%xmm3 \n\t" // d:x0
222 "movdqa %%xmm1,%%xmm4 \n\t" // same
223 "movdqa %%xmm1,%%xmm5 \n\t" // same
224 "psllq $63, %%xmm3 \n\t" // d<<63:stuff
225 "psllq $62, %%xmm4 \n\t" // d<<62:stuff
226 "psllq $57, %%xmm5 \n\t" // d<<57:stuff
227 "pxor %%xmm4, %%xmm3 \n\t" // d<<63+d<<62:stuff
228 "pxor %%xmm5, %%xmm3 \n\t" // missing bits of d:stuff
229 "psrldq $8, %%xmm3 \n\t" // 0:missing bits of d
230 "pxor %%xmm3, %%xmm0 \n\t" // e1+f1+g1:e0+f0+g0
231 "pxor %%xmm1, %%xmm0 \n\t" // h1:h0
232 "pxor %%xmm2, %%xmm0 \n\t" // x3+h1:x2+h0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100233
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200234 "movdqu %%xmm0, (%2) \n\t" // done
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100235 :
236 : "r" (aa), "r" (bb), "r" (cc)
237 : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5" );
238
239 /* Now byte-reverse the outputs */
240 for( i = 0; i < 16; i++ )
241 c[i] = cc[15 - i];
242
Paul Bakkere7f51332013-12-30 15:32:02 +0100243 return;
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100244}
245
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100246/*
247 * Compute decryption round keys from encryption round keys
248 */
249void aesni_inverse_key( unsigned char *invkey,
250 const unsigned char *fwdkey, int nr )
251{
252 unsigned char *ik = invkey;
253 const unsigned char *fk = fwdkey + 16 * nr;
254
255 memcpy( ik, fk, 16 );
256
257 for( fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16 )
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200258 asm( "movdqu (%0), %%xmm0 \n\t"
259 AESIMC xmm0_xmm0 "\n\t"
260 "movdqu %%xmm0, (%1) \n\t"
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100261 :
262 : "r" (fk), "r" (ik)
263 : "memory", "xmm0" );
264
265 memcpy( ik, fk, 16 );
266}
267
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100268/*
269 * Key expansion, 128-bit case
270 */
Paul Bakker93759b02013-12-30 19:20:06 +0100271static void aesni_setkey_enc_128( unsigned char *rk,
272 const unsigned char *key )
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100273{
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200274 asm( "movdqu (%1), %%xmm0 \n\t" // copy the original key
275 "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)
315 : "memory", "cc", "0" );
316}
317
318/*
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100319 * Key expansion, 192-bit case
320 */
Paul Bakker93759b02013-12-30 19:20:06 +0100321static void aesni_setkey_enc_192( unsigned char *rk,
322 const unsigned char *key )
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100323{
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +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
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)
372 : "memory", "cc", "0" );
373}
374
375/*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100376 * Key expansion, 256-bit case
377 */
Paul Bakker93759b02013-12-30 19:20:06 +0100378static void aesni_setkey_enc_256( unsigned char *rk,
379 const unsigned char *key )
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100380{
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200381 asm( "movdqu (%1), %%xmm0 \n\t"
382 "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"
412 "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,
426 * see definition of aes_context.buf
427 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200428 "2: \n\t"
429 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)
438 : "memory", "cc", "0" );
439}
440
441/*
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100442 * Key expansion, wrapper
443 */
444int aesni_setkey_enc( unsigned char *rk,
445 const unsigned char *key,
446 size_t bits )
447{
448 switch( bits )
449 {
450 case 128: aesni_setkey_enc_128( rk, key ); break;
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100451 case 192: aesni_setkey_enc_192( rk, key ); break;
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100452 case 256: aesni_setkey_enc_256( rk, key ); break;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100453 default : return( POLARSSL_ERR_AES_INVALID_KEY_LENGTH );
454 }
455
456 return( 0 );
457}
458
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100459#endif /* POLARSSL_HAVE_X86_64 */
460
461#endif /* POLARSSL_AESNI_C */