31 #if !defined(POLARSSL_CONFIG_FILE)
34 #include POLARSSL_CONFIG_FILE
37 #if defined(POLARSSL_AESNI_C)
42 #if defined(POLARSSL_HAVE_X86_64)
47 int aesni_supports(
unsigned int what )
50 static unsigned int c = 0;
54 asm(
"movl $1, %%eax \n\t"
58 :
"eax",
"ebx",
"edx" );
62 return( ( c & what ) != 0 );
75 #define AESDEC ".byte 0x66,0x0F,0x38,0xDE,"
76 #define AESDECLAST ".byte 0x66,0x0F,0x38,0xDF,"
77 #define AESENC ".byte 0x66,0x0F,0x38,0xDC,"
78 #define AESENCLAST ".byte 0x66,0x0F,0x38,0xDD,"
79 #define AESIMC ".byte 0x66,0x0F,0x38,0xDB,"
80 #define AESKEYGENA ".byte 0x66,0x0F,0x3A,0xDF,"
81 #define PCLMULQDQ ".byte 0x66,0x0F,0x3A,0x44,"
83 #define xmm0_xmm0 "0xC0"
84 #define xmm0_xmm1 "0xC8"
85 #define xmm0_xmm2 "0xD0"
86 #define xmm0_xmm3 "0xD8"
87 #define xmm0_xmm4 "0xE0"
88 #define xmm1_xmm0 "0xC1"
89 #define xmm1_xmm2 "0xD1"
96 const unsigned char input[16],
97 unsigned char output[16] )
99 asm(
"movdqu (%3), %%xmm0 \n\t"
100 "movdqu (%1), %%xmm1 \n\t"
101 "pxor %%xmm1, %%xmm0 \n\t"
112 "movdqu (%1), %%xmm1 \n\t"
113 AESENC xmm1_xmm0
"\n\t"
121 "movdqu (%1), %%xmm1 \n\t"
122 AESENCLAST xmm1_xmm0
"\n\t"
126 "movdqu (%1), %%xmm1 \n\t"
127 AESDEC xmm1_xmm0
"\n\t"
135 "movdqu (%1), %%xmm1 \n\t"
136 AESDECLAST xmm1_xmm0
"\n\t"
139 "movdqu %%xmm0, (%4) \n\t"
141 :
"r" (ctx->
nr),
"r" (ctx->
rk),
"r" (mode),
"r" (input),
"r" (output)
142 :
"memory",
"cc",
"xmm0",
"xmm1" );
152 void aesni_gcm_mult(
unsigned char c[16],
153 const unsigned char a[16],
154 const unsigned char b[16] )
156 unsigned char aa[16], bb[16], cc[16];
160 for( i = 0; i < 16; i++ )
166 asm(
"movdqu (%0), %%xmm0 \n\t"
167 "movdqu (%1), %%xmm1 \n\t"
173 "movdqa %%xmm1, %%xmm2 \n\t"
174 "movdqa %%xmm1, %%xmm3 \n\t"
175 "movdqa %%xmm1, %%xmm4 \n\t"
176 PCLMULQDQ xmm0_xmm1
",0x00 \n\t"
177 PCLMULQDQ xmm0_xmm2
",0x11 \n\t"
178 PCLMULQDQ xmm0_xmm3
",0x10 \n\t"
179 PCLMULQDQ xmm0_xmm4
",0x01 \n\t"
180 "pxor %%xmm3, %%xmm4 \n\t"
181 "movdqa %%xmm4, %%xmm3 \n\t"
182 "psrldq $8, %%xmm4 \n\t"
183 "pslldq $8, %%xmm3 \n\t"
184 "pxor %%xmm4, %%xmm2 \n\t"
185 "pxor %%xmm3, %%xmm1 \n\t"
191 "movdqa %%xmm1, %%xmm3 \n\t"
192 "movdqa %%xmm2, %%xmm4 \n\t"
193 "psllq $1, %%xmm1 \n\t"
194 "psllq $1, %%xmm2 \n\t"
195 "psrlq $63, %%xmm3 \n\t"
196 "psrlq $63, %%xmm4 \n\t"
197 "movdqa %%xmm3, %%xmm5 \n\t"
198 "pslldq $8, %%xmm3 \n\t"
199 "pslldq $8, %%xmm4 \n\t"
200 "psrldq $8, %%xmm5 \n\t"
201 "por %%xmm3, %%xmm1 \n\t"
202 "por %%xmm4, %%xmm2 \n\t"
203 "por %%xmm5, %%xmm2 \n\t"
211 "movdqa %%xmm1, %%xmm3 \n\t"
212 "movdqa %%xmm1, %%xmm4 \n\t"
213 "movdqa %%xmm1, %%xmm5 \n\t"
214 "psllq $63, %%xmm3 \n\t"
215 "psllq $62, %%xmm4 \n\t"
216 "psllq $57, %%xmm5 \n\t"
219 "pxor %%xmm4, %%xmm3 \n\t"
220 "pxor %%xmm5, %%xmm3 \n\t"
221 "pslldq $8, %%xmm3 \n\t"
222 "pxor %%xmm3, %%xmm1 \n\t"
225 "movdqa %%xmm1,%%xmm0 \n\t"
226 "movdqa %%xmm1,%%xmm4 \n\t"
227 "movdqa %%xmm1,%%xmm5 \n\t"
228 "psrlq $1, %%xmm0 \n\t"
229 "psrlq $2, %%xmm4 \n\t"
230 "psrlq $7, %%xmm5 \n\t"
231 "pxor %%xmm4, %%xmm0 \n\t"
232 "pxor %%xmm5, %%xmm0 \n\t"
235 "movdqa %%xmm1,%%xmm3 \n\t"
236 "movdqa %%xmm1,%%xmm4 \n\t"
237 "movdqa %%xmm1,%%xmm5 \n\t"
238 "psllq $63, %%xmm3 \n\t"
239 "psllq $62, %%xmm4 \n\t"
240 "psllq $57, %%xmm5 \n\t"
241 "pxor %%xmm4, %%xmm3 \n\t"
242 "pxor %%xmm5, %%xmm3 \n\t"
243 "psrldq $8, %%xmm3 \n\t"
244 "pxor %%xmm3, %%xmm0 \n\t"
245 "pxor %%xmm1, %%xmm0 \n\t"
246 "pxor %%xmm2, %%xmm0 \n\t"
248 "movdqu %%xmm0, (%2) \n\t"
250 :
"r" (aa),
"r" (bb),
"r" (cc)
251 :
"memory",
"cc",
"xmm0",
"xmm1",
"xmm2",
"xmm3",
"xmm4",
"xmm5" );
254 for( i = 0; i < 16; i++ )
263 void aesni_inverse_key(
unsigned char *invkey,
264 const unsigned char *fwdkey,
int nr )
266 unsigned char *ik = invkey;
267 const unsigned char *fk = fwdkey + 16 * nr;
269 memcpy( ik, fk, 16 );
271 for( fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16 )
272 asm(
"movdqu (%0), %%xmm0 \n\t"
273 AESIMC xmm0_xmm0
"\n\t"
274 "movdqu %%xmm0, (%1) \n\t"
277 :
"memory",
"xmm0" );
279 memcpy( ik, fk, 16 );
285 static void aesni_setkey_enc_128(
unsigned char *rk,
286 const unsigned char *key )
288 asm(
"movdqu (%1), %%xmm0 \n\t"
289 "movdqu %%xmm0, (%0) \n\t"
303 "pshufd $0xff, %%xmm1, %%xmm1 \n\t"
304 "pxor %%xmm0, %%xmm1 \n\t"
305 "pslldq $4, %%xmm0 \n\t"
306 "pxor %%xmm0, %%xmm1 \n\t"
307 "pslldq $4, %%xmm0 \n\t"
308 "pxor %%xmm0, %%xmm1 \n\t"
309 "pslldq $4, %%xmm0 \n\t"
310 "pxor %%xmm1, %%xmm0 \n\t"
312 "movdqu %%xmm0, (%0) \n\t"
317 AESKEYGENA xmm0_xmm1
",0x01 \n\tcall 1b \n\t"
318 AESKEYGENA xmm0_xmm1
",0x02 \n\tcall 1b \n\t"
319 AESKEYGENA xmm0_xmm1
",0x04 \n\tcall 1b \n\t"
320 AESKEYGENA xmm0_xmm1
",0x08 \n\tcall 1b \n\t"
321 AESKEYGENA xmm0_xmm1
",0x10 \n\tcall 1b \n\t"
322 AESKEYGENA xmm0_xmm1
",0x20 \n\tcall 1b \n\t"
323 AESKEYGENA xmm0_xmm1
",0x40 \n\tcall 1b \n\t"
324 AESKEYGENA xmm0_xmm1
",0x80 \n\tcall 1b \n\t"
325 AESKEYGENA xmm0_xmm1
",0x1B \n\tcall 1b \n\t"
326 AESKEYGENA xmm0_xmm1
",0x36 \n\tcall 1b \n\t"
328 :
"r" (rk),
"r" (key)
329 :
"memory",
"cc",
"0" );
335 static void aesni_setkey_enc_192(
unsigned char *rk,
336 const unsigned char *key )
338 asm(
"movdqu (%1), %%xmm0 \n\t"
339 "movdqu %%xmm0, (%0) \n\t"
341 "movq 16(%1), %%xmm1 \n\t"
342 "movq %%xmm1, (%0) \n\t"
356 "pshufd $0x55, %%xmm2, %%xmm2 \n\t"
357 "pxor %%xmm0, %%xmm2 \n\t"
358 "pslldq $4, %%xmm0 \n\t"
359 "pxor %%xmm0, %%xmm2 \n\t"
360 "pslldq $4, %%xmm0 \n\t"
361 "pxor %%xmm0, %%xmm2 \n\t"
362 "pslldq $4, %%xmm0 \n\t"
363 "pxor %%xmm2, %%xmm0 \n\t"
364 "movdqu %%xmm0, (%0) \n\t"
366 "pshufd $0xff, %%xmm0, %%xmm2 \n\t"
367 "pxor %%xmm1, %%xmm2 \n\t"
368 "pslldq $4, %%xmm1 \n\t"
369 "pxor %%xmm2, %%xmm1 \n\t"
370 "movq %%xmm1, (%0) \n\t"
375 AESKEYGENA xmm1_xmm2
",0x01 \n\tcall 1b \n\t"
376 AESKEYGENA xmm1_xmm2
",0x02 \n\tcall 1b \n\t"
377 AESKEYGENA xmm1_xmm2
",0x04 \n\tcall 1b \n\t"
378 AESKEYGENA xmm1_xmm2
",0x08 \n\tcall 1b \n\t"
379 AESKEYGENA xmm1_xmm2
",0x10 \n\tcall 1b \n\t"
380 AESKEYGENA xmm1_xmm2
",0x20 \n\tcall 1b \n\t"
381 AESKEYGENA xmm1_xmm2
",0x40 \n\tcall 1b \n\t"
382 AESKEYGENA xmm1_xmm2
",0x80 \n\tcall 1b \n\t"
385 :
"r" (rk),
"r" (key)
386 :
"memory",
"cc",
"0" );
392 static void aesni_setkey_enc_256(
unsigned char *rk,
393 const unsigned char *key )
395 asm(
"movdqu (%1), %%xmm0 \n\t"
396 "movdqu %%xmm0, (%0) \n\t"
398 "movdqu 16(%1), %%xmm1 \n\t"
399 "movdqu %%xmm1, (%0) \n\t"
412 "pshufd $0xff, %%xmm2, %%xmm2 \n\t"
413 "pxor %%xmm0, %%xmm2 \n\t"
414 "pslldq $4, %%xmm0 \n\t"
415 "pxor %%xmm0, %%xmm2 \n\t"
416 "pslldq $4, %%xmm0 \n\t"
417 "pxor %%xmm0, %%xmm2 \n\t"
418 "pslldq $4, %%xmm0 \n\t"
419 "pxor %%xmm2, %%xmm0 \n\t"
421 "movdqu %%xmm0, (%0) \n\t"
425 AESKEYGENA xmm0_xmm2
",0x00 \n\t"
426 "pshufd $0xaa, %%xmm2, %%xmm2 \n\t"
427 "pxor %%xmm1, %%xmm2 \n\t"
428 "pslldq $4, %%xmm1 \n\t"
429 "pxor %%xmm1, %%xmm2 \n\t"
430 "pslldq $4, %%xmm1 \n\t"
431 "pxor %%xmm1, %%xmm2 \n\t"
432 "pslldq $4, %%xmm1 \n\t"
433 "pxor %%xmm2, %%xmm1 \n\t"
435 "movdqu %%xmm1, (%0) \n\t"
443 AESKEYGENA xmm1_xmm2
",0x01 \n\tcall 1b \n\t"
444 AESKEYGENA xmm1_xmm2
",0x02 \n\tcall 1b \n\t"
445 AESKEYGENA xmm1_xmm2
",0x04 \n\tcall 1b \n\t"
446 AESKEYGENA xmm1_xmm2
",0x08 \n\tcall 1b \n\t"
447 AESKEYGENA xmm1_xmm2
",0x10 \n\tcall 1b \n\t"
448 AESKEYGENA xmm1_xmm2
",0x20 \n\tcall 1b \n\t"
449 AESKEYGENA xmm1_xmm2
",0x40 \n\tcall 1b \n\t"
451 :
"r" (rk),
"r" (key)
452 :
"memory",
"cc",
"0" );
458 int aesni_setkey_enc(
unsigned char *rk,
459 const unsigned char *key,
464 case 128: aesni_setkey_enc_128( rk, key );
break;
465 case 192: aesni_setkey_enc_192( rk, key );
break;
466 case 256: aesni_setkey_enc_256( rk, key );
break;
Configuration options (set of defines)
AES-NI for hardware AES acceleration on some Intel processors.
#define POLARSSL_ERR_AES_INVALID_KEY_LENGTH
Invalid key length.