aesni.c 18 KB

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