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