aesni.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * AES-NI support functions
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. *
  7. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. /*
  47. * [AES-WP] http://software.intel.com/en-us/articles/intel-advanced-encryption-standard-aes-instructions-set
  48. * [CLMUL-WP] http://software.intel.com/en-us/articles/intel-carry-less-multiplication-instruction-and-its-usage-for-computing-the-gcm-mode/
  49. */
  50. #if !defined(MBEDTLS_CONFIG_FILE)
  51. #include "mbedtls/config.h"
  52. #else
  53. #include MBEDTLS_CONFIG_FILE
  54. #endif
  55. #if defined(MBEDTLS_AESNI_C)
  56. #if defined(__has_feature)
  57. #if __has_feature(memory_sanitizer)
  58. #warning "MBEDTLS_AESNI_C is known to cause spurious error reports with some memory sanitizers as they do not understand the assembly code."
  59. #endif
  60. #endif
  61. #include "mbedtls/aesni.h"
  62. #include <string.h>
  63. #ifndef asm
  64. #define asm __asm
  65. #endif
  66. #if defined(MBEDTLS_HAVE_X86_64)
  67. /*
  68. * AES-NI support detection routine
  69. */
  70. int mbedtls_aesni_has_support( unsigned int what )
  71. {
  72. static int done = 0;
  73. static unsigned int c = 0;
  74. if( ! done )
  75. {
  76. asm( "movl $1, %%eax \n\t"
  77. "cpuid \n\t"
  78. : "=c" (c)
  79. :
  80. : "eax", "ebx", "edx" );
  81. done = 1;
  82. }
  83. return( ( c & what ) != 0 );
  84. }
  85. /*
  86. * Binutils needs to be at least 2.19 to support AES-NI instructions.
  87. * Unfortunately, a lot of users have a lower version now (2014-04).
  88. * Emit bytecode directly in order to support "old" version of gas.
  89. *
  90. * Opcodes from the Intel architecture reference manual, vol. 3.
  91. * We always use registers, so we don't need prefixes for memory operands.
  92. * Operand macros are in gas order (src, dst) as opposed to Intel order
  93. * (dst, src) in order to blend better into the surrounding assembly code.
  94. */
  95. #define AESDEC ".byte 0x66,0x0F,0x38,0xDE,"
  96. #define AESDECLAST ".byte 0x66,0x0F,0x38,0xDF,"
  97. #define AESENC ".byte 0x66,0x0F,0x38,0xDC,"
  98. #define AESENCLAST ".byte 0x66,0x0F,0x38,0xDD,"
  99. #define AESIMC ".byte 0x66,0x0F,0x38,0xDB,"
  100. #define AESKEYGENA ".byte 0x66,0x0F,0x3A,0xDF,"
  101. #define PCLMULQDQ ".byte 0x66,0x0F,0x3A,0x44,"
  102. #define xmm0_xmm0 "0xC0"
  103. #define xmm0_xmm1 "0xC8"
  104. #define xmm0_xmm2 "0xD0"
  105. #define xmm0_xmm3 "0xD8"
  106. #define xmm0_xmm4 "0xE0"
  107. #define xmm1_xmm0 "0xC1"
  108. #define xmm1_xmm2 "0xD1"
  109. /*
  110. * AES-NI AES-ECB block en(de)cryption
  111. */
  112. int mbedtls_aesni_crypt_ecb( mbedtls_aes_context *ctx,
  113. int mode,
  114. const unsigned char input[16],
  115. unsigned char output[16] )
  116. {
  117. asm( "movdqu (%3), %%xmm0 \n\t" // load input
  118. "movdqu (%1), %%xmm1 \n\t" // load round key 0
  119. "pxor %%xmm1, %%xmm0 \n\t" // round 0
  120. "add $16, %1 \n\t" // point to next round key
  121. "subl $1, %0 \n\t" // normal rounds = nr - 1
  122. "test %2, %2 \n\t" // mode?
  123. "jz 2f \n\t" // 0 = decrypt
  124. "1: \n\t" // encryption loop
  125. "movdqu (%1), %%xmm1 \n\t" // load round key
  126. AESENC xmm1_xmm0 "\n\t" // do round
  127. "add $16, %1 \n\t" // point to next round key
  128. "subl $1, %0 \n\t" // loop
  129. "jnz 1b \n\t"
  130. "movdqu (%1), %%xmm1 \n\t" // load round key
  131. AESENCLAST xmm1_xmm0 "\n\t" // last round
  132. "jmp 3f \n\t"
  133. "2: \n\t" // decryption loop
  134. "movdqu (%1), %%xmm1 \n\t"
  135. AESDEC xmm1_xmm0 "\n\t" // do round
  136. "add $16, %1 \n\t"
  137. "subl $1, %0 \n\t"
  138. "jnz 2b \n\t"
  139. "movdqu (%1), %%xmm1 \n\t" // load round key
  140. AESDECLAST xmm1_xmm0 "\n\t" // last round
  141. "3: \n\t"
  142. "movdqu %%xmm0, (%4) \n\t" // export output
  143. :
  144. : "r" (ctx->nr), "r" (ctx->rk), "r" (mode), "r" (input), "r" (output)
  145. : "memory", "cc", "xmm0", "xmm1" );
  146. return( 0 );
  147. }
  148. /*
  149. * GCM multiplication: c = a times b in GF(2^128)
  150. * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
  151. */
  152. void mbedtls_aesni_gcm_mult( unsigned char c[16],
  153. const unsigned char a[16],
  154. const unsigned char b[16] )
  155. {
  156. unsigned char aa[16], bb[16], cc[16];
  157. size_t i;
  158. /* The inputs are in big-endian order, so byte-reverse them */
  159. for( i = 0; i < 16; i++ )
  160. {
  161. aa[i] = a[15 - i];
  162. bb[i] = b[15 - i];
  163. }
  164. asm( "movdqu (%0), %%xmm0 \n\t" // a1:a0
  165. "movdqu (%1), %%xmm1 \n\t" // b1:b0
  166. /*
  167. * Caryless multiplication xmm2:xmm1 = xmm0 * xmm1
  168. * using [CLMUL-WP] algorithm 1 (p. 13).
  169. */
  170. "movdqa %%xmm1, %%xmm2 \n\t" // copy of b1:b0
  171. "movdqa %%xmm1, %%xmm3 \n\t" // same
  172. "movdqa %%xmm1, %%xmm4 \n\t" // same
  173. PCLMULQDQ xmm0_xmm1 ",0x00 \n\t" // a0*b0 = c1:c0
  174. PCLMULQDQ xmm0_xmm2 ",0x11 \n\t" // a1*b1 = d1:d0
  175. PCLMULQDQ xmm0_xmm3 ",0x10 \n\t" // a0*b1 = e1:e0
  176. PCLMULQDQ xmm0_xmm4 ",0x01 \n\t" // a1*b0 = f1:f0
  177. "pxor %%xmm3, %%xmm4 \n\t" // e1+f1:e0+f0
  178. "movdqa %%xmm4, %%xmm3 \n\t" // same
  179. "psrldq $8, %%xmm4 \n\t" // 0:e1+f1
  180. "pslldq $8, %%xmm3 \n\t" // e0+f0:0
  181. "pxor %%xmm4, %%xmm2 \n\t" // d1:d0+e1+f1
  182. "pxor %%xmm3, %%xmm1 \n\t" // c1+e0+f1:c0
  183. /*
  184. * Now shift the result one bit to the left,
  185. * taking advantage of [CLMUL-WP] eq 27 (p. 20)
  186. */
  187. "movdqa %%xmm1, %%xmm3 \n\t" // r1:r0
  188. "movdqa %%xmm2, %%xmm4 \n\t" // r3:r2
  189. "psllq $1, %%xmm1 \n\t" // r1<<1:r0<<1
  190. "psllq $1, %%xmm2 \n\t" // r3<<1:r2<<1
  191. "psrlq $63, %%xmm3 \n\t" // r1>>63:r0>>63
  192. "psrlq $63, %%xmm4 \n\t" // r3>>63:r2>>63
  193. "movdqa %%xmm3, %%xmm5 \n\t" // r1>>63:r0>>63
  194. "pslldq $8, %%xmm3 \n\t" // r0>>63:0
  195. "pslldq $8, %%xmm4 \n\t" // r2>>63:0
  196. "psrldq $8, %%xmm5 \n\t" // 0:r1>>63
  197. "por %%xmm3, %%xmm1 \n\t" // r1<<1|r0>>63:r0<<1
  198. "por %%xmm4, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1
  199. "por %%xmm5, %%xmm2 \n\t" // r3<<1|r2>>62:r2<<1|r1>>63
  200. /*
  201. * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
  202. * using [CLMUL-WP] algorithm 5 (p. 20).
  203. * Currently xmm2:xmm1 holds x3:x2:x1:x0 (already shifted).
  204. */
  205. /* Step 2 (1) */
  206. "movdqa %%xmm1, %%xmm3 \n\t" // x1:x0
  207. "movdqa %%xmm1, %%xmm4 \n\t" // same
  208. "movdqa %%xmm1, %%xmm5 \n\t" // same
  209. "psllq $63, %%xmm3 \n\t" // x1<<63:x0<<63 = stuff:a
  210. "psllq $62, %%xmm4 \n\t" // x1<<62:x0<<62 = stuff:b
  211. "psllq $57, %%xmm5 \n\t" // x1<<57:x0<<57 = stuff:c
  212. /* Step 2 (2) */
  213. "pxor %%xmm4, %%xmm3 \n\t" // stuff:a+b
  214. "pxor %%xmm5, %%xmm3 \n\t" // stuff:a+b+c
  215. "pslldq $8, %%xmm3 \n\t" // a+b+c:0
  216. "pxor %%xmm3, %%xmm1 \n\t" // x1+a+b+c:x0 = d:x0
  217. /* Steps 3 and 4 */
  218. "movdqa %%xmm1,%%xmm0 \n\t" // d:x0
  219. "movdqa %%xmm1,%%xmm4 \n\t" // same
  220. "movdqa %%xmm1,%%xmm5 \n\t" // same
  221. "psrlq $1, %%xmm0 \n\t" // e1:x0>>1 = e1:e0'
  222. "psrlq $2, %%xmm4 \n\t" // f1:x0>>2 = f1:f0'
  223. "psrlq $7, %%xmm5 \n\t" // g1:x0>>7 = g1:g0'
  224. "pxor %%xmm4, %%xmm0 \n\t" // e1+f1:e0'+f0'
  225. "pxor %%xmm5, %%xmm0 \n\t" // e1+f1+g1:e0'+f0'+g0'
  226. // e0'+f0'+g0' is almost e0+f0+g0, ex\tcept for some missing
  227. // bits carried from d. Now get those\t bits back in.
  228. "movdqa %%xmm1,%%xmm3 \n\t" // d:x0
  229. "movdqa %%xmm1,%%xmm4 \n\t" // same
  230. "movdqa %%xmm1,%%xmm5 \n\t" // same
  231. "psllq $63, %%xmm3 \n\t" // d<<63:stuff
  232. "psllq $62, %%xmm4 \n\t" // d<<62:stuff
  233. "psllq $57, %%xmm5 \n\t" // d<<57:stuff
  234. "pxor %%xmm4, %%xmm3 \n\t" // d<<63+d<<62:stuff
  235. "pxor %%xmm5, %%xmm3 \n\t" // missing bits of d:stuff
  236. "psrldq $8, %%xmm3 \n\t" // 0:missing bits of d
  237. "pxor %%xmm3, %%xmm0 \n\t" // e1+f1+g1:e0+f0+g0
  238. "pxor %%xmm1, %%xmm0 \n\t" // h1:h0
  239. "pxor %%xmm2, %%xmm0 \n\t" // x3+h1:x2+h0
  240. "movdqu %%xmm0, (%2) \n\t" // done
  241. :
  242. : "r" (aa), "r" (bb), "r" (cc)
  243. : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5" );
  244. /* Now byte-reverse the outputs */
  245. for( i = 0; i < 16; i++ )
  246. c[i] = cc[15 - i];
  247. return;
  248. }
  249. /*
  250. * Compute decryption round keys from encryption round keys
  251. */
  252. void mbedtls_aesni_inverse_key( unsigned char *invkey,
  253. const unsigned char *fwdkey, int nr )
  254. {
  255. unsigned char *ik = invkey;
  256. const unsigned char *fk = fwdkey + 16 * nr;
  257. memcpy( ik, fk, 16 );
  258. for( fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16 )
  259. asm( "movdqu (%0), %%xmm0 \n\t"
  260. AESIMC xmm0_xmm0 "\n\t"
  261. "movdqu %%xmm0, (%1) \n\t"
  262. :
  263. : "r" (fk), "r" (ik)
  264. : "memory", "xmm0" );
  265. memcpy( ik, fk, 16 );
  266. }
  267. /*
  268. * Key expansion, 128-bit case
  269. */
  270. static void aesni_setkey_enc_128( unsigned char *rk,
  271. const unsigned char *key )
  272. {
  273. 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
  276. /*
  277. * Finish generating the next round key.
  278. *
  279. * On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff
  280. * with X = rot( sub( r3 ) ) ^ RCON.
  281. *
  282. * On exit, xmm0 is r7:r6:r5:r4
  283. * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
  284. * and those are written to the round key buffer.
  285. */
  286. "1: \n\t"
  287. "pshufd $0xff, %%xmm1, %%xmm1 \n\t" // X:X:X:X
  288. "pxor %%xmm0, %%xmm1 \n\t" // X+r3:X+r2:X+r1:r4
  289. "pslldq $4, %%xmm0 \n\t" // r2:r1:r0:0
  290. "pxor %%xmm0, %%xmm1 \n\t" // X+r3+r2:X+r2+r1:r5:r4
  291. "pslldq $4, %%xmm0 \n\t" // etc
  292. "pxor %%xmm0, %%xmm1 \n\t"
  293. "pslldq $4, %%xmm0 \n\t"
  294. "pxor %%xmm1, %%xmm0 \n\t" // update xmm0 for next time!
  295. "add $16, %0 \n\t" // point to next round key
  296. "movdqu %%xmm0, (%0) \n\t" // write it
  297. "ret \n\t"
  298. /* Main "loop" */
  299. "2: \n\t"
  300. AESKEYGENA xmm0_xmm1 ",0x01 \n\tcall 1b \n\t"
  301. AESKEYGENA xmm0_xmm1 ",0x02 \n\tcall 1b \n\t"
  302. AESKEYGENA xmm0_xmm1 ",0x04 \n\tcall 1b \n\t"
  303. AESKEYGENA xmm0_xmm1 ",0x08 \n\tcall 1b \n\t"
  304. AESKEYGENA xmm0_xmm1 ",0x10 \n\tcall 1b \n\t"
  305. AESKEYGENA xmm0_xmm1 ",0x20 \n\tcall 1b \n\t"
  306. AESKEYGENA xmm0_xmm1 ",0x40 \n\tcall 1b \n\t"
  307. AESKEYGENA xmm0_xmm1 ",0x80 \n\tcall 1b \n\t"
  308. AESKEYGENA xmm0_xmm1 ",0x1B \n\tcall 1b \n\t"
  309. AESKEYGENA xmm0_xmm1 ",0x36 \n\tcall 1b \n\t"
  310. :
  311. : "r" (rk), "r" (key)
  312. : "memory", "cc", "0" );
  313. }
  314. /*
  315. * Key expansion, 192-bit case
  316. */
  317. static void aesni_setkey_enc_192( unsigned char *rk,
  318. const unsigned char *key )
  319. {
  320. asm( "movdqu (%1), %%xmm0 \n\t" // copy original round key
  321. "movdqu %%xmm0, (%0) \n\t"
  322. "add $16, %0 \n\t"
  323. "movq 16(%1), %%xmm1 \n\t"
  324. "movq %%xmm1, (%0) \n\t"
  325. "add $8, %0 \n\t"
  326. "jmp 2f \n\t" // skip auxiliary routine
  327. /*
  328. * Finish generating the next 6 quarter-keys.
  329. *
  330. * On entry xmm0 is r3:r2:r1:r0, xmm1 is stuff:stuff:r5:r4
  331. * and xmm2 is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON.
  332. *
  333. * On exit, xmm0 is r9:r8:r7:r6 and xmm1 is stuff:stuff:r11:r10
  334. * and those are written to the round key buffer.
  335. */
  336. "1: \n\t"
  337. "pshufd $0x55, %%xmm2, %%xmm2 \n\t" // X:X:X:X
  338. "pxor %%xmm0, %%xmm2 \n\t" // X+r3:X+r2:X+r1:r4
  339. "pslldq $4, %%xmm0 \n\t" // etc
  340. "pxor %%xmm0, %%xmm2 \n\t"
  341. "pslldq $4, %%xmm0 \n\t"
  342. "pxor %%xmm0, %%xmm2 \n\t"
  343. "pslldq $4, %%xmm0 \n\t"
  344. "pxor %%xmm2, %%xmm0 \n\t" // update xmm0 = r9:r8:r7:r6
  345. "movdqu %%xmm0, (%0) \n\t"
  346. "add $16, %0 \n\t"
  347. "pshufd $0xff, %%xmm0, %%xmm2 \n\t" // r9:r9:r9:r9
  348. "pxor %%xmm1, %%xmm2 \n\t" // stuff:stuff:r9+r5:r10
  349. "pslldq $4, %%xmm1 \n\t" // r2:r1:r0:0
  350. "pxor %%xmm2, %%xmm1 \n\t" // xmm1 = stuff:stuff:r11:r10
  351. "movq %%xmm1, (%0) \n\t"
  352. "add $8, %0 \n\t"
  353. "ret \n\t"
  354. "2: \n\t"
  355. AESKEYGENA xmm1_xmm2 ",0x01 \n\tcall 1b \n\t"
  356. AESKEYGENA xmm1_xmm2 ",0x02 \n\tcall 1b \n\t"
  357. AESKEYGENA xmm1_xmm2 ",0x04 \n\tcall 1b \n\t"
  358. AESKEYGENA xmm1_xmm2 ",0x08 \n\tcall 1b \n\t"
  359. AESKEYGENA xmm1_xmm2 ",0x10 \n\tcall 1b \n\t"
  360. AESKEYGENA xmm1_xmm2 ",0x20 \n\tcall 1b \n\t"
  361. AESKEYGENA xmm1_xmm2 ",0x40 \n\tcall 1b \n\t"
  362. AESKEYGENA xmm1_xmm2 ",0x80 \n\tcall 1b \n\t"
  363. :
  364. : "r" (rk), "r" (key)
  365. : "memory", "cc", "0" );
  366. }
  367. /*
  368. * Key expansion, 256-bit case
  369. */
  370. static void aesni_setkey_enc_256( unsigned char *rk,
  371. const unsigned char *key )
  372. {
  373. asm( "movdqu (%1), %%xmm0 \n\t"
  374. "movdqu %%xmm0, (%0) \n\t"
  375. "add $16, %0 \n\t"
  376. "movdqu 16(%1), %%xmm1 \n\t"
  377. "movdqu %%xmm1, (%0) \n\t"
  378. "jmp 2f \n\t" // skip auxiliary routine
  379. /*
  380. * Finish generating the next two round keys.
  381. *
  382. * On entry xmm0 is r3:r2:r1:r0, xmm1 is r7:r6:r5:r4 and
  383. * xmm2 is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
  384. *
  385. * On exit, xmm0 is r11:r10:r9:r8 and xmm1 is r15:r14:r13:r12
  386. * and those have been written to the output buffer.
  387. */
  388. "1: \n\t"
  389. "pshufd $0xff, %%xmm2, %%xmm2 \n\t"
  390. "pxor %%xmm0, %%xmm2 \n\t"
  391. "pslldq $4, %%xmm0 \n\t"
  392. "pxor %%xmm0, %%xmm2 \n\t"
  393. "pslldq $4, %%xmm0 \n\t"
  394. "pxor %%xmm0, %%xmm2 \n\t"
  395. "pslldq $4, %%xmm0 \n\t"
  396. "pxor %%xmm2, %%xmm0 \n\t"
  397. "add $16, %0 \n\t"
  398. "movdqu %%xmm0, (%0) \n\t"
  399. /* Set xmm2 to stuff:Y:stuff:stuff with Y = subword( r11 )
  400. * and proceed to generate next round key from there */
  401. AESKEYGENA xmm0_xmm2 ",0x00 \n\t"
  402. "pshufd $0xaa, %%xmm2, %%xmm2 \n\t"
  403. "pxor %%xmm1, %%xmm2 \n\t"
  404. "pslldq $4, %%xmm1 \n\t"
  405. "pxor %%xmm1, %%xmm2 \n\t"
  406. "pslldq $4, %%xmm1 \n\t"
  407. "pxor %%xmm1, %%xmm2 \n\t"
  408. "pslldq $4, %%xmm1 \n\t"
  409. "pxor %%xmm2, %%xmm1 \n\t"
  410. "add $16, %0 \n\t"
  411. "movdqu %%xmm1, (%0) \n\t"
  412. "ret \n\t"
  413. /*
  414. * Main "loop" - Generating one more key than necessary,
  415. * see definition of mbedtls_aes_context.buf
  416. */
  417. "2: \n\t"
  418. AESKEYGENA xmm1_xmm2 ",0x01 \n\tcall 1b \n\t"
  419. AESKEYGENA xmm1_xmm2 ",0x02 \n\tcall 1b \n\t"
  420. AESKEYGENA xmm1_xmm2 ",0x04 \n\tcall 1b \n\t"
  421. AESKEYGENA xmm1_xmm2 ",0x08 \n\tcall 1b \n\t"
  422. AESKEYGENA xmm1_xmm2 ",0x10 \n\tcall 1b \n\t"
  423. AESKEYGENA xmm1_xmm2 ",0x20 \n\tcall 1b \n\t"
  424. AESKEYGENA xmm1_xmm2 ",0x40 \n\tcall 1b \n\t"
  425. :
  426. : "r" (rk), "r" (key)
  427. : "memory", "cc", "0" );
  428. }
  429. /*
  430. * Key expansion, wrapper
  431. */
  432. int mbedtls_aesni_setkey_enc( unsigned char *rk,
  433. const unsigned char *key,
  434. size_t bits )
  435. {
  436. switch( bits )
  437. {
  438. case 128: aesni_setkey_enc_128( rk, key ); break;
  439. case 192: aesni_setkey_enc_192( rk, key ); break;
  440. case 256: aesni_setkey_enc_256( rk, key ); break;
  441. default : return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
  442. }
  443. return( 0 );
  444. }
  445. #endif /* MBEDTLS_HAVE_X86_64 */
  446. #endif /* MBEDTLS_AESNI_C */