blob: 346a05b175ed2174038c47a016bd280a0e9080e2 [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é-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.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"
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010037#include <stdio.h>
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010038
39#if defined(POLARSSL_HAVE_X86_64)
40
41/*
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010042 * AES-NI support detection routine
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010043 */
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010044int aesni_supports( unsigned int what )
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010045{
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010046 static int done = 0;
47 static unsigned int c = 0;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010048
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010049 if( ! done )
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010050 {
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +020051 asm( "movl $1, %%eax \n\t"
52 "cpuid \n\t"
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010053 : "=c" (c)
54 :
55 : "eax", "ebx", "edx" );
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010056 done = 1;
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010057 }
58
Manuel Pégourié-Gonnard8eaf20b2013-12-18 19:14:53 +010059 return( ( c & what ) != 0 );
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +010060}
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 */
91int aesni_crypt_ecb( aes_context *ctx,
92 int mode,
93 const unsigned char input[16],
94 unsigned char output[16] )
95{
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +020096 asm( "movdqu (%3), %%xmm0 \n\t" // load input
97 "movdqu (%1), %%xmm1 \n\t" // load round key 0
98 "pxor %%xmm1, %%xmm0 \n\t" // round 0
99 "addq $16, %1 \n\t" // point to next round key
100 "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
107 "addq $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
111 AESENCLAST xmm1_xmm0 "\n\t" // last round
112 "jmp 3f \n\t"
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100113
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200114 "2: \n\t" // decryption loop
115 "movdqu (%1), %%xmm1 \n\t"
116 AESDEC xmm1_xmm0 "\n\t" // do round
117 "addq $16, %1 \n\t"
118 "subl $1, %0 \n\t"
119 "jnz 2b \n\t"
120 "movdqu (%1), %%xmm1 \n\t" // load round key
121 AESDECLAST xmm1_xmm0 "\n\t" // last round
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100122
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200123 "3: \n\t"
124 "movdqu %%xmm0, (%4) \n\t" // export output
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +0100125 :
126 : "r" (ctx->nr), "r" (ctx->rk), "r" (mode), "r" (input), "r" (output)
127 : "memory", "cc", "xmm0", "xmm1" );
128
129
130 return( 0 );
131}
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 */
Manuel Pégourié-Gonnardd4588cf2013-12-30 13:54:23 +0100137void 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 */
145 for( i = 0; i < 16; i++ )
146 {
147 aa[i] = a[15 - i];
148 bb[i] = b[15 - i];
149 }
150
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200151 asm( "movdqu (%0), %%xmm0 \n\t" // a1:a0
152 "movdqu (%1), %%xmm1 \n\t" // b1:b0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100153
154 /*
155 * Caryless multiplication xmm2:xmm1 = xmm0 * xmm1
156 * using [CLMUL-WP] algorithm 1 (p. 13).
157 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200158 "movdqa %%xmm1, %%xmm2 \n\t" // copy of b1:b0
159 "movdqa %%xmm1, %%xmm3 \n\t" // same
160 "movdqa %%xmm1, %%xmm4 \n\t" // same
161 PCLMULQDQ xmm0_xmm1 ",0x00 \n\t" // a0*b0 = c1:c0
162 PCLMULQDQ xmm0_xmm2 ",0x11 \n\t" // a1*b1 = d1:d0
163 PCLMULQDQ xmm0_xmm3 ",0x10 \n\t" // a0*b1 = e1:e0
164 PCLMULQDQ xmm0_xmm4 ",0x01 \n\t" // a1*b0 = f1:f0
165 "pxor %%xmm3, %%xmm4 \n\t" // e1+f1:e0+f0
166 "movdqa %%xmm4, %%xmm3 \n\t" // same
167 "psrldq $8, %%xmm4 \n\t" // 0:e1+f1
168 "pslldq $8, %%xmm3 \n\t" // e0+f0:0
169 "pxor %%xmm4, %%xmm2 \n\t" // d1:d0+e1+f1
170 "pxor %%xmm3, %%xmm1 \n\t" // c1+e0+f1:c0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100171
172 /*
173 * Now shift the result one bit to the left,
174 * taking advantage of [CLMUL-WP] eq 27 (p. 20)
175 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200176 "movdqa %%xmm1, %%xmm3 \n\t" // r1:r0
177 "movdqa %%xmm2, %%xmm4 \n\t" // r3:r2
178 "psllq $1, %%xmm1 \n\t" // r1<<1:r0<<1
179 "psllq $1, %%xmm2 \n\t" // r3<<1:r2<<1
180 "psrlq $63, %%xmm3 \n\t" // r1>>63:r0>>63
181 "psrlq $63, %%xmm4 \n\t" // r3>>63:r2>>63
182 "movdqa %%xmm3, %%xmm5 \n\t" // r1>>63:r0>>63
183 "pslldq $8, %%xmm3 \n\t" // r0>>63:0
184 "pslldq $8, %%xmm4 \n\t" // r2>>63:0
185 "psrldq $8, %%xmm5 \n\t" // 0:r1>>63
186 "por %%xmm3, %%xmm1 \n\t" // r1<<1|r0>>63:r0<<1
187 "por %%xmm4, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1
188 "por %%xmm5, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1|r1>>63
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100189
190 /*
191 * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
192 * using [CLMUL-WP] algorithm 5 (p. 20).
193 * Currently xmm2:xmm1 holds x3:x2:x1:x0 (already shifted).
194 */
195 /* Step 2 (1) */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200196 "movdqa %%xmm1, %%xmm3 \n\t" // x1:x0
197 "movdqa %%xmm1, %%xmm4 \n\t" // same
198 "movdqa %%xmm1, %%xmm5 \n\t" // same
199 "psllq $63, %%xmm3 \n\t" // x1<<63:x0<<63 = stuff:a
200 "psllq $62, %%xmm4 \n\t" // x1<<62:x0<<62 = stuff:b
201 "psllq $57, %%xmm5 \n\t" // x1<<57:x0<<57 = stuff:c
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100202
203 /* Step 2 (2) */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200204 "pxor %%xmm4, %%xmm3 \n\t" // stuff:a+b
205 "pxor %%xmm5, %%xmm3 \n\t" // stuff:a+b+c
206 "pslldq $8, %%xmm3 \n\t" // a+b+c:0
207 "pxor %%xmm3, %%xmm1 \n\t" // x1+a+b+c:x0 = d:x0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100208
209 /* Steps 3 and 4 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200210 "movdqa %%xmm1,%%xmm0 \n\t" // d:x0
211 "movdqa %%xmm1,%%xmm4 \n\t" // same
212 "movdqa %%xmm1,%%xmm5 \n\t" // same
213 "psrlq $1, %%xmm0 \n\t" // e1:x0>>1 = e1:e0'
214 "psrlq $2, %%xmm4 \n\t" // f1:x0>>2 = f1:f0'
215 "psrlq $7, %%xmm5 \n\t" // g1:x0>>7 = g1:g0'
216 "pxor %%xmm4, %%xmm0 \n\t" // e1+f1:e0'+f0'
217 "pxor %%xmm5, %%xmm0 \n\t" // e1+f1+g1:e0'+f0'+g0'
218 // e0'+f0'+g0' is almost e0+f0+g0, ex\tcept for some missing
219 // bits carried from d. Now get those\t bits back in.
220 "movdqa %%xmm1,%%xmm3 \n\t" // d:x0
221 "movdqa %%xmm1,%%xmm4 \n\t" // same
222 "movdqa %%xmm1,%%xmm5 \n\t" // same
223 "psllq $63, %%xmm3 \n\t" // d<<63:stuff
224 "psllq $62, %%xmm4 \n\t" // d<<62:stuff
225 "psllq $57, %%xmm5 \n\t" // d<<57:stuff
226 "pxor %%xmm4, %%xmm3 \n\t" // d<<63+d<<62:stuff
227 "pxor %%xmm5, %%xmm3 \n\t" // missing bits of d:stuff
228 "psrldq $8, %%xmm3 \n\t" // 0:missing bits of d
229 "pxor %%xmm3, %%xmm0 \n\t" // e1+f1+g1:e0+f0+g0
230 "pxor %%xmm1, %%xmm0 \n\t" // h1:h0
231 "pxor %%xmm2, %%xmm0 \n\t" // x3+h1:x2+h0
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100232
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200233 "movdqu %%xmm0, (%2) \n\t" // done
Manuel Pégourié-Gonnardd333f672013-12-26 11:44:46 +0100234 :
235 : "r" (aa), "r" (bb), "r" (cc)
236 : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5" );
237
238 /* Now byte-reverse the outputs */
239 for( i = 0; i < 16; i++ )
240 c[i] = cc[15 - i];
241
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 */
248void aesni_inverse_key( unsigned char *invkey,
249 const unsigned char *fwdkey, int nr )
250{
251 unsigned char *ik = invkey;
252 const unsigned char *fk = fwdkey + 16 * nr;
253
254 memcpy( ik, fk, 16 );
255
256 for( fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16 )
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200257 asm( "movdqu (%0), %%xmm0 \n\t"
258 AESIMC xmm0_xmm0 "\n\t"
259 "movdqu %%xmm0, (%1) \n\t"
Manuel Pégourié-Gonnard01e31bb2013-12-28 15:58:30 +0100260 :
261 : "r" (fk), "r" (ik)
262 : "memory", "xmm0" );
263
264 memcpy( ik, fk, 16 );
265}
266
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100267/*
268 * Key expansion, 128-bit case
269 */
Paul Bakker93759b02013-12-30 19:20:06 +0100270static void aesni_setkey_enc_128( unsigned char *rk,
271 const unsigned char *key )
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100272{
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200273 asm( "movdqu (%1), %%xmm0 \n\t" // copy the original key
274 "movdqu %%xmm0, (%0) \n\t" // as round key 0
275 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100276
277 /*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100278 * Finish generating the next round key.
279 *
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100280 * On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff
281 * with X = rot( sub( r3 ) ) ^ RCON.
282 *
283 * On exit, xmm0 is r7:r6:r5:r4
284 * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
285 * and those are written to the round key buffer.
286 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200287 "1: \n\t"
288 "pshufd $0xff, %%xmm1, %%xmm1 \n\t" // X:X:X:X
289 "pxor %%xmm0, %%xmm1 \n\t" // X+r3:X+r2:X+r1:r4
290 "pslldq $4, %%xmm0 \n\t" // r2:r1:r0:0
291 "pxor %%xmm0, %%xmm1 \n\t" // X+r3+r2:X+r2+r1:r5:r4
292 "pslldq $4, %%xmm0 \n\t" // etc
293 "pxor %%xmm0, %%xmm1 \n\t"
294 "pslldq $4, %%xmm0 \n\t"
295 "pxor %%xmm1, %%xmm0 \n\t" // update xmm0 for next time!
296 "add $16, %0 \n\t" // point to next round key
297 "movdqu %%xmm0, (%0) \n\t" // write it
298 "ret \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100299
300 /* Main "loop" */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200301 "2: \n\t"
302 AESKEYGENA xmm0_xmm1 ",0x01 \n\tcall 1b \n\t"
303 AESKEYGENA xmm0_xmm1 ",0x02 \n\tcall 1b \n\t"
304 AESKEYGENA xmm0_xmm1 ",0x04 \n\tcall 1b \n\t"
305 AESKEYGENA xmm0_xmm1 ",0x08 \n\tcall 1b \n\t"
306 AESKEYGENA xmm0_xmm1 ",0x10 \n\tcall 1b \n\t"
307 AESKEYGENA xmm0_xmm1 ",0x20 \n\tcall 1b \n\t"
308 AESKEYGENA xmm0_xmm1 ",0x40 \n\tcall 1b \n\t"
309 AESKEYGENA xmm0_xmm1 ",0x80 \n\tcall 1b \n\t"
310 AESKEYGENA xmm0_xmm1 ",0x1B \n\tcall 1b \n\t"
311 AESKEYGENA xmm0_xmm1 ",0x36 \n\tcall 1b \n\t"
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100312 :
313 : "r" (rk), "r" (key)
314 : "memory", "cc", "0" );
315}
316
317/*
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100318 * Key expansion, 192-bit case
319 */
Paul Bakker93759b02013-12-30 19:20:06 +0100320static void aesni_setkey_enc_192( unsigned char *rk,
321 const unsigned char *key )
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100322{
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200323 asm( "movdqu (%1), %%xmm0 \n\t" // copy original round key
324 "movdqu %%xmm0, (%0) \n\t"
325 "add $16, %0 \n\t"
326 "movq 16(%1), %%xmm1 \n\t"
327 "movq %%xmm1, (%0) \n\t"
328 "add $8, %0 \n\t"
329 "jmp 2f \n\t" // skip auxiliary routine
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100330
331 /*
332 * Finish generating the next 6 quarter-keys.
333 *
334 * On entry xmm0 is r3:r2:r1:r0, xmm1 is stuff:stuff:r5:r4
335 * and xmm2 is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON.
336 *
337 * On exit, xmm0 is r9:r8:r7:r6 and xmm1 is stuff:stuff:r11:r10
338 * and those are written to the round key buffer.
339 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200340 "1: \n\t"
341 "pshufd $0x55, %%xmm2, %%xmm2 \n\t" // X:X:X:X
342 "pxor %%xmm0, %%xmm2 \n\t" // X+r3:X+r2:X+r1:r4
343 "pslldq $4, %%xmm0 \n\t" // etc
344 "pxor %%xmm0, %%xmm2 \n\t"
345 "pslldq $4, %%xmm0 \n\t"
346 "pxor %%xmm0, %%xmm2 \n\t"
347 "pslldq $4, %%xmm0 \n\t"
348 "pxor %%xmm2, %%xmm0 \n\t" // update xmm0 = r9:r8:r7:r6
349 "movdqu %%xmm0, (%0) \n\t"
350 "add $16, %0 \n\t"
351 "pshufd $0xff, %%xmm0, %%xmm2 \n\t" // r9:r9:r9:r9
352 "pxor %%xmm1, %%xmm2 \n\t" // stuff:stuff:r9+r5:r10
353 "pslldq $4, %%xmm1 \n\t" // r2:r1:r0:0
354 "pxor %%xmm2, %%xmm1 \n\t" // xmm1 = stuff:stuff:r11:r10
355 "movq %%xmm1, (%0) \n\t"
356 "add $8, %0 \n\t"
357 "ret \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100358
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200359 "2: \n\t"
360 AESKEYGENA xmm1_xmm2 ",0x01 \n\tcall 1b \n\t"
361 AESKEYGENA xmm1_xmm2 ",0x02 \n\tcall 1b \n\t"
362 AESKEYGENA xmm1_xmm2 ",0x04 \n\tcall 1b \n\t"
363 AESKEYGENA xmm1_xmm2 ",0x08 \n\tcall 1b \n\t"
364 AESKEYGENA xmm1_xmm2 ",0x10 \n\tcall 1b \n\t"
365 AESKEYGENA xmm1_xmm2 ",0x20 \n\tcall 1b \n\t"
366 AESKEYGENA xmm1_xmm2 ",0x40 \n\tcall 1b \n\t"
367 AESKEYGENA xmm1_xmm2 ",0x80 \n\tcall 1b \n\t"
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100368
369 :
370 : "r" (rk), "r" (key)
371 : "memory", "cc", "0" );
372}
373
374/*
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100375 * Key expansion, 256-bit case
376 */
Paul Bakker93759b02013-12-30 19:20:06 +0100377static void aesni_setkey_enc_256( unsigned char *rk,
378 const unsigned char *key )
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100379{
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +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
387 /*
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 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200396 "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
408 /* Set xmm2 to stuff:Y:stuff:stuff with Y = subword( r11 )
409 * and proceed to generate next round key from there */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200410 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
423 /*
424 * Main "loop" - Generating one more key than necessary,
425 * see definition of aes_context.buf
426 */
Manuel Pégourié-Gonnard0534fd42014-06-23 12:35:42 +0200427 "2: \n\t"
428 AESKEYGENA xmm1_xmm2 ",0x01 \n\tcall 1b \n\t"
429 AESKEYGENA xmm1_xmm2 ",0x02 \n\tcall 1b \n\t"
430 AESKEYGENA xmm1_xmm2 ",0x04 \n\tcall 1b \n\t"
431 AESKEYGENA xmm1_xmm2 ",0x08 \n\tcall 1b \n\t"
432 AESKEYGENA xmm1_xmm2 ",0x10 \n\tcall 1b \n\t"
433 AESKEYGENA xmm1_xmm2 ",0x20 \n\tcall 1b \n\t"
434 AESKEYGENA xmm1_xmm2 ",0x40 \n\tcall 1b \n\t"
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100435 :
436 : "r" (rk), "r" (key)
437 : "memory", "cc", "0" );
438}
439
440/*
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100441 * Key expansion, wrapper
442 */
443int aesni_setkey_enc( unsigned char *rk,
444 const unsigned char *key,
445 size_t bits )
446{
447 switch( bits )
448 {
449 case 128: aesni_setkey_enc_128( rk, key ); break;
Manuel Pégourié-Gonnard23c2f6f2013-12-29 16:05:22 +0100450 case 192: aesni_setkey_enc_192( rk, key ); break;
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +0100451 case 256: aesni_setkey_enc_256( rk, key ); break;
Manuel Pégourié-Gonnard47a35362013-12-28 20:45:04 +0100452 default : return( POLARSSL_ERR_AES_INVALID_KEY_LENGTH );
453 }
454
455 return( 0 );
456}
457
Manuel Pégourié-Gonnard92ac76f2013-12-16 17:12:53 +0100458#endif /* POLARSSL_HAVE_X86_64 */
459
460#endif /* POLARSSL_AESNI_C */