52#if !defined(MBEDTLS_CONFIG_FILE)
55#include MBEDTLS_CONFIG_FILE
58#if defined(MBEDTLS_AESNI_C)
60#if defined(__has_feature)
61#if __has_feature(memory_sanitizer)
62#warning "MBEDTLS_AESNI_C is known to cause spurious error reports with some memory sanitizers as they do not understand the assembly code."
74#if defined(MBEDTLS_HAVE_X86_64)
79int mbedtls_aesni_has_support(
unsigned int what )
82 static unsigned int c = 0;
86 asm(
"movl $1, %%eax \n\t"
90 :
"eax",
"ebx",
"edx" );
94 return( (
c & what ) != 0 );
107#define AESDEC ".byte 0x66,0x0F,0x38,0xDE,"
108#define AESDECLAST ".byte 0x66,0x0F,0x38,0xDF,"
109#define AESENC ".byte 0x66,0x0F,0x38,0xDC,"
110#define AESENCLAST ".byte 0x66,0x0F,0x38,0xDD,"
111#define AESIMC ".byte 0x66,0x0F,0x38,0xDB,"
112#define AESKEYGENA ".byte 0x66,0x0F,0x3A,0xDF,"
113#define PCLMULQDQ ".byte 0x66,0x0F,0x3A,0x44,"
115#define xmm0_xmm0 "0xC0"
116#define xmm0_xmm1 "0xC8"
117#define xmm0_xmm2 "0xD0"
118#define xmm0_xmm3 "0xD8"
119#define xmm0_xmm4 "0xE0"
120#define xmm1_xmm0 "0xC1"
121#define xmm1_xmm2 "0xD1"
128 const unsigned char input[16],
129 unsigned char output[16] )
131 asm(
"movdqu (%3), %%xmm0 \n\t"
132 "movdqu (%1), %%xmm1 \n\t"
133 "pxor %%xmm1, %%xmm0 \n\t"
140 "movdqu (%1), %%xmm1 \n\t"
141 AESENC xmm1_xmm0
"\n\t"
145 "movdqu (%1), %%xmm1 \n\t"
146 AESENCLAST xmm1_xmm0
"\n\t"
150 "movdqu (%1), %%xmm1 \n\t"
151 AESDEC xmm1_xmm0
"\n\t"
155 "movdqu (%1), %%xmm1 \n\t"
156 AESDECLAST xmm1_xmm0
"\n\t"
159 "movdqu %%xmm0, (%4) \n\t"
162 :
"memory",
"cc",
"xmm0",
"xmm1" );
172void mbedtls_aesni_gcm_mult(
unsigned char c[16],
173 const unsigned char a[16],
174 const unsigned char b[16] )
176 unsigned char aa[16], bb[16],
cc[16];
180 for(
i = 0;
i < 16;
i++ )
186 asm(
"movdqu (%0), %%xmm0 \n\t"
187 "movdqu (%1), %%xmm1 \n\t"
193 "movdqa %%xmm1, %%xmm2 \n\t"
194 "movdqa %%xmm1, %%xmm3 \n\t"
195 "movdqa %%xmm1, %%xmm4 \n\t"
196 PCLMULQDQ xmm0_xmm1
",0x00 \n\t"
197 PCLMULQDQ xmm0_xmm2
",0x11 \n\t"
198 PCLMULQDQ xmm0_xmm3
",0x10 \n\t"
199 PCLMULQDQ xmm0_xmm4
",0x01 \n\t"
200 "pxor %%xmm3, %%xmm4 \n\t"
201 "movdqa %%xmm4, %%xmm3 \n\t"
202 "psrldq $8, %%xmm4 \n\t"
203 "pslldq $8, %%xmm3 \n\t"
204 "pxor %%xmm4, %%xmm2 \n\t"
205 "pxor %%xmm3, %%xmm1 \n\t"
211 "movdqa %%xmm1, %%xmm3 \n\t"
212 "movdqa %%xmm2, %%xmm4 \n\t"
213 "psllq $1, %%xmm1 \n\t"
214 "psllq $1, %%xmm2 \n\t"
215 "psrlq $63, %%xmm3 \n\t"
216 "psrlq $63, %%xmm4 \n\t"
217 "movdqa %%xmm3, %%xmm5 \n\t"
218 "pslldq $8, %%xmm3 \n\t"
219 "pslldq $8, %%xmm4 \n\t"
220 "psrldq $8, %%xmm5 \n\t"
221 "por %%xmm3, %%xmm1 \n\t"
222 "por %%xmm4, %%xmm2 \n\t"
223 "por %%xmm5, %%xmm2 \n\t"
231 "movdqa %%xmm1, %%xmm3 \n\t"
232 "movdqa %%xmm1, %%xmm4 \n\t"
233 "movdqa %%xmm1, %%xmm5 \n\t"
234 "psllq $63, %%xmm3 \n\t"
235 "psllq $62, %%xmm4 \n\t"
236 "psllq $57, %%xmm5 \n\t"
239 "pxor %%xmm4, %%xmm3 \n\t"
240 "pxor %%xmm5, %%xmm3 \n\t"
241 "pslldq $8, %%xmm3 \n\t"
242 "pxor %%xmm3, %%xmm1 \n\t"
245 "movdqa %%xmm1,%%xmm0 \n\t"
246 "movdqa %%xmm1,%%xmm4 \n\t"
247 "movdqa %%xmm1,%%xmm5 \n\t"
248 "psrlq $1, %%xmm0 \n\t"
249 "psrlq $2, %%xmm4 \n\t"
250 "psrlq $7, %%xmm5 \n\t"
251 "pxor %%xmm4, %%xmm0 \n\t"
252 "pxor %%xmm5, %%xmm0 \n\t"
255 "movdqa %%xmm1,%%xmm3 \n\t"
256 "movdqa %%xmm1,%%xmm4 \n\t"
257 "movdqa %%xmm1,%%xmm5 \n\t"
258 "psllq $63, %%xmm3 \n\t"
259 "psllq $62, %%xmm4 \n\t"
260 "psllq $57, %%xmm5 \n\t"
261 "pxor %%xmm4, %%xmm3 \n\t"
262 "pxor %%xmm5, %%xmm3 \n\t"
263 "psrldq $8, %%xmm3 \n\t"
264 "pxor %%xmm3, %%xmm0 \n\t"
265 "pxor %%xmm1, %%xmm0 \n\t"
266 "pxor %%xmm2, %%xmm0 \n\t"
268 "movdqu %%xmm0, (%2) \n\t"
270 :
"r" (aa),
"r" (bb),
"r" (
cc)
271 :
"memory",
"cc",
"xmm0",
"xmm1",
"xmm2",
"xmm3",
"xmm4",
"xmm5" );
274 for(
i = 0;
i < 16;
i++ )
283void mbedtls_aesni_inverse_key(
unsigned char *invkey,
284 const unsigned char *fwdkey,
int nr )
286 unsigned char *ik = invkey;
287 const unsigned char *fk = fwdkey + 16 *
nr;
291 for( fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16 )
292 asm(
"movdqu (%0), %%xmm0 \n\t"
293 AESIMC xmm0_xmm0
"\n\t"
294 "movdqu %%xmm0, (%1) \n\t"
297 :
"memory",
"xmm0" );
305static void aesni_setkey_enc_128(
unsigned char *rk,
306 const unsigned char *
key )
308 asm(
"movdqu (%1), %%xmm0 \n\t"
309 "movdqu %%xmm0, (%0) \n\t"
323 "pshufd $0xff, %%xmm1, %%xmm1 \n\t"
324 "pxor %%xmm0, %%xmm1 \n\t"
325 "pslldq $4, %%xmm0 \n\t"
326 "pxor %%xmm0, %%xmm1 \n\t"
327 "pslldq $4, %%xmm0 \n\t"
328 "pxor %%xmm0, %%xmm1 \n\t"
329 "pslldq $4, %%xmm0 \n\t"
330 "pxor %%xmm1, %%xmm0 \n\t"
332 "movdqu %%xmm0, (%0) \n\t"
337 AESKEYGENA xmm0_xmm1
",0x01 \n\tcall 1b \n\t"
338 AESKEYGENA xmm0_xmm1
",0x02 \n\tcall 1b \n\t"
339 AESKEYGENA xmm0_xmm1
",0x04 \n\tcall 1b \n\t"
340 AESKEYGENA xmm0_xmm1
",0x08 \n\tcall 1b \n\t"
341 AESKEYGENA xmm0_xmm1
",0x10 \n\tcall 1b \n\t"
342 AESKEYGENA xmm0_xmm1
",0x20 \n\tcall 1b \n\t"
343 AESKEYGENA xmm0_xmm1
",0x40 \n\tcall 1b \n\t"
344 AESKEYGENA xmm0_xmm1
",0x80 \n\tcall 1b \n\t"
345 AESKEYGENA xmm0_xmm1
",0x1B \n\tcall 1b \n\t"
346 AESKEYGENA xmm0_xmm1
",0x36 \n\tcall 1b \n\t"
348 :
"r" (rk),
"r" (
key)
349 :
"memory",
"cc",
"0" );
355static void aesni_setkey_enc_192(
unsigned char *rk,
356 const unsigned char *
key )
358 asm(
"movdqu (%1), %%xmm0 \n\t"
359 "movdqu %%xmm0, (%0) \n\t"
361 "movq 16(%1), %%xmm1 \n\t"
362 "movq %%xmm1, (%0) \n\t"
376 "pshufd $0x55, %%xmm2, %%xmm2 \n\t"
377 "pxor %%xmm0, %%xmm2 \n\t"
378 "pslldq $4, %%xmm0 \n\t"
379 "pxor %%xmm0, %%xmm2 \n\t"
380 "pslldq $4, %%xmm0 \n\t"
381 "pxor %%xmm0, %%xmm2 \n\t"
382 "pslldq $4, %%xmm0 \n\t"
383 "pxor %%xmm2, %%xmm0 \n\t"
384 "movdqu %%xmm0, (%0) \n\t"
386 "pshufd $0xff, %%xmm0, %%xmm2 \n\t"
387 "pxor %%xmm1, %%xmm2 \n\t"
388 "pslldq $4, %%xmm1 \n\t"
389 "pxor %%xmm2, %%xmm1 \n\t"
390 "movq %%xmm1, (%0) \n\t"
395 AESKEYGENA xmm1_xmm2
",0x01 \n\tcall 1b \n\t"
396 AESKEYGENA xmm1_xmm2
",0x02 \n\tcall 1b \n\t"
397 AESKEYGENA xmm1_xmm2
",0x04 \n\tcall 1b \n\t"
398 AESKEYGENA xmm1_xmm2
",0x08 \n\tcall 1b \n\t"
399 AESKEYGENA xmm1_xmm2
",0x10 \n\tcall 1b \n\t"
400 AESKEYGENA xmm1_xmm2
",0x20 \n\tcall 1b \n\t"
401 AESKEYGENA xmm1_xmm2
",0x40 \n\tcall 1b \n\t"
402 AESKEYGENA xmm1_xmm2
",0x80 \n\tcall 1b \n\t"
405 :
"r" (rk),
"r" (
key)
406 :
"memory",
"cc",
"0" );
412static void aesni_setkey_enc_256(
unsigned char *rk,
413 const unsigned char *
key )
415 asm(
"movdqu (%1), %%xmm0 \n\t"
416 "movdqu %%xmm0, (%0) \n\t"
418 "movdqu 16(%1), %%xmm1 \n\t"
419 "movdqu %%xmm1, (%0) \n\t"
432 "pshufd $0xff, %%xmm2, %%xmm2 \n\t"
433 "pxor %%xmm0, %%xmm2 \n\t"
434 "pslldq $4, %%xmm0 \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 %%xmm2, %%xmm0 \n\t"
441 "movdqu %%xmm0, (%0) \n\t"
445 AESKEYGENA xmm0_xmm2
",0x00 \n\t"
446 "pshufd $0xaa, %%xmm2, %%xmm2 \n\t"
447 "pxor %%xmm1, %%xmm2 \n\t"
448 "pslldq $4, %%xmm1 \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 %%xmm2, %%xmm1 \n\t"
455 "movdqu %%xmm1, (%0) \n\t"
463 AESKEYGENA xmm1_xmm2
",0x01 \n\tcall 1b \n\t"
464 AESKEYGENA xmm1_xmm2
",0x02 \n\tcall 1b \n\t"
465 AESKEYGENA xmm1_xmm2
",0x04 \n\tcall 1b \n\t"
466 AESKEYGENA xmm1_xmm2
",0x08 \n\tcall 1b \n\t"
467 AESKEYGENA xmm1_xmm2
",0x10 \n\tcall 1b \n\t"
468 AESKEYGENA xmm1_xmm2
",0x20 \n\tcall 1b \n\t"
469 AESKEYGENA xmm1_xmm2
",0x40 \n\tcall 1b \n\t"
471 :
"r" (rk),
"r" (
key)
472 :
"memory",
"cc",
"0" );
478int mbedtls_aesni_setkey_enc(
unsigned char *rk,
479 const unsigned char *
key,
484 case 128: aesni_setkey_enc_128( rk,
key );
break;
485 case 192: aesni_setkey_enc_192( rk,
key );
break;
486 case 256: aesni_setkey_enc_256( rk,
key );
break;
#define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
AES-NI for hardware AES acceleration on some Intel processors.
GLboolean GLboolean GLboolean b
GLenum GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * bits
GLboolean GLboolean GLboolean GLboolean a
GLenum GLenum GLenum input
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
#define memcpy(s1, s2, n)
Configuration options (set of defines)
The AES context-type definition.