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