1 /* This files defines _aes_encrypt_hw and _aes_decrypt_hw --- Intel Westmere HW AES-based implementation
2 of _aes_encrypt and _aes_decrypt.
4 These 2 functions SHOULD BE entried ONLY after the AES HW is verified to be available.
5 They SHOULD NOT be called without AES HW detection. It might cause xnu to crash.
7 The AES HW is detected 1st thing in
8 _aes_encrypt (EncryptDecrypt.s)
9 _aes_decrypt (EncryptDecrypt.s)
10 and, if AES HW is detected, branch without link (ie, jump) to the functions here.
12 The implementation here follows the examples in an Intel White Paper
13 "Intel Advanced Encryption Standard (AES) Instruction Set" Rev.2 01
15 Note: Rev. 03 Final 2010 01 26 is available. Looks like some code change from Rev.2 01
22 .globl _aes_encrypt_hw
26 movl 4(%esp), %eax // in
27 movl 12(%esp), %edx // ctx
28 movl 8(%esp), %ecx // out
30 #define LOCAL_SIZE (12+16+16) // 16-byte align (-4 for return address) + 16 (xmm0) + 16 (xmm1)
38 #define LOCAL_SIZE (8+16+16) // 16-byte align (-8 for return address) + 16 (xmm0) + 16 (xmm1)
44 #endif // i386 or x86_64
52 // key length identification
53 movl 240(ctx), %eax // key length
60 mov $-1, %eax // return ERROR
68 testb $15, %dl // check whether expanded key is 16-byte aligned
69 jne 0f // if not 16-byte aligned, aesenc xmm, m128 won't work
77 aesenc 112(ctx), %xmm0
78 aesenc 128(ctx), %xmm0
79 aesenc 144(ctx), %xmm0
80 aesenclast 160(ctx), %xmm0
88 0: // special case expanded key is not 16-byte aligned
90 movaps %xmm1, 16(r13) // save xmm1 into stack
100 movups 64(ctx), %xmm1
102 movups 80(ctx), %xmm1
104 movups 96(ctx), %xmm1
106 movups 112(ctx), %xmm1
108 movups 128(ctx), %xmm1
110 movups 144(ctx), %xmm1
112 movups 160(ctx), %xmm1
113 aesenclast %xmm1, %xmm0
118 movaps 16(r13), %xmm1
124 testb $15, %dl // check whether expanded key is 16-byte aligned
125 jne 0f // if not 16-byte aligned, aesenc xmm, m128 won't work
127 aesenc 16(ctx), %xmm0
128 aesenc 32(ctx), %xmm0
129 aesenc 48(ctx), %xmm0
130 aesenc 64(ctx), %xmm0
131 aesenc 80(ctx), %xmm0
132 aesenc 96(ctx), %xmm0
133 aesenc 112(ctx), %xmm0
134 aesenc 128(ctx), %xmm0
135 aesenc 144(ctx), %xmm0
136 aesenc 160(ctx), %xmm0
137 aesenc 176(ctx), %xmm0
138 aesenclast 192(ctx), %xmm0
146 0: // special case expanded key is not 16-byte aligned
148 movaps %xmm1, 16(r13) // save xmm1 into stack
152 movups 16(ctx), %xmm1
154 movups 32(ctx), %xmm1
156 movups 48(ctx), %xmm1
158 movups 64(ctx), %xmm1
160 movups 80(ctx), %xmm1
162 movups 96(ctx), %xmm1
164 movups 112(ctx), %xmm1
166 movups 128(ctx), %xmm1
168 movups 144(ctx), %xmm1
170 movups 160(ctx), %xmm1
172 movups 176(ctx), %xmm1
174 movups 192(ctx), %xmm1
175 aesenclast %xmm1, %xmm0
180 movaps 16(r13), %xmm1
186 testb $15, %dl // check whether expanded key is 16-byte aligned
187 jne 0f // if not 16-byte aligned, aesenc xmm, m128 won't work
189 aesenc 16(ctx), %xmm0
190 aesenc 32(ctx), %xmm0
191 aesenc 48(ctx), %xmm0
192 aesenc 64(ctx), %xmm0
193 aesenc 80(ctx), %xmm0
194 aesenc 96(ctx), %xmm0
195 aesenc 112(ctx), %xmm0
196 aesenc 128(ctx), %xmm0
197 aesenc 144(ctx), %xmm0
198 aesenc 160(ctx), %xmm0
199 aesenc 176(ctx), %xmm0
200 aesenc 192(ctx), %xmm0
201 aesenc 208(ctx), %xmm0
202 aesenclast 224(ctx), %xmm0
210 0: // special case expanded key is not 16-byte aligned
212 movaps %xmm1, 16(r13) // save xmm1 into stack
216 movups 16(ctx), %xmm1
218 movups 32(ctx), %xmm1
220 movups 48(ctx), %xmm1
222 movups 64(ctx), %xmm1
224 movups 80(ctx), %xmm1
226 movups 96(ctx), %xmm1
228 movups 112(ctx), %xmm1
230 movups 128(ctx), %xmm1
232 movups 144(ctx), %xmm1
234 movups 160(ctx), %xmm1
236 movups 176(ctx), %xmm1
238 movups 192(ctx), %xmm1
240 movups 208(ctx), %xmm1
242 movups 224(ctx), %xmm1
243 aesenclast %xmm1, %xmm0
248 movaps 16(r13), %xmm1
256 .globl _aes_decrypt_hw
260 movl 4(%esp), %eax // in
261 movl 12(%esp), %edx // ctx
262 movl 8(%esp), %ecx // out
272 // key length identification
273 movl 240(ctx), %eax // key length
280 mov $-1, %eax // return ERROR
288 testb $15, %dl // check whether expanded key is 16-byte aligned
289 jne 9f // if not 16-byte aligned, aesenc xmm, m128 won't work
291 aesdec 144(ctx), %xmm0
292 aesdec 128(ctx), %xmm0
293 aesdec 112(ctx), %xmm0
294 aesdec 96(ctx), %xmm0
295 aesdec 80(ctx), %xmm0
296 aesdec 64(ctx), %xmm0
297 aesdec 48(ctx), %xmm0
298 aesdec 32(ctx), %xmm0
299 aesdec 16(ctx), %xmm0
300 aesdeclast (ctx), %xmm0
308 9: // AES-128 Decrypt : special case expanded key is not 16-byte aligned
310 movaps %xmm1, 16(r13) // save xmm1 into stack
312 movups 160(ctx), %xmm1
314 movups 144(ctx), %xmm1
316 movups 128(ctx), %xmm1
318 movups 112(ctx), %xmm1
320 movups 96(ctx), %xmm1
322 movups 80(ctx), %xmm1
324 movups 64(ctx), %xmm1
326 movups 48(ctx), %xmm1
328 movups 32(ctx), %xmm1
330 movups 16(ctx), %xmm1
333 aesdeclast %xmm1, %xmm0
338 movaps 16(r13), %xmm1
344 testb $15, %dl // check whether expanded key is 16-byte aligned
345 jne 9f // if not 16-byte aligned, aesenc xmm, m128 won't work
347 aesdec 176(ctx), %xmm0
348 aesdec 160(ctx), %xmm0
349 aesdec 144(ctx), %xmm0
350 aesdec 128(ctx), %xmm0
351 aesdec 112(ctx), %xmm0
352 aesdec 96(ctx), %xmm0
353 aesdec 80(ctx), %xmm0
354 aesdec 64(ctx), %xmm0
355 aesdec 48(ctx), %xmm0
356 aesdec 32(ctx), %xmm0
357 aesdec 16(ctx), %xmm0
358 aesdeclast (ctx), %xmm0
366 9: // AES-192 Decrypt : special case expanded key is not 16-byte aligned
368 movaps %xmm1, 16(r13) // save xmm1 into stack
370 movups 192(ctx), %xmm1
372 movups 176(ctx), %xmm1
374 movups 160(ctx), %xmm1
376 movups 144(ctx), %xmm1
378 movups 128(ctx), %xmm1
380 movups 112(ctx), %xmm1
382 movups 96(ctx), %xmm1
384 movups 80(ctx), %xmm1
386 movups 64(ctx), %xmm1
388 movups 48(ctx), %xmm1
390 movups 32(ctx), %xmm1
392 movups 16(ctx), %xmm1
395 aesdeclast %xmm1, %xmm0
400 movaps 16(r13), %xmm1
406 testb $15, %dl // check whether expanded key is 16-byte aligned
407 jne 9f // if not 16-byte aligned, aesenc xmm, m128 won't work
409 aesdec 208(ctx), %xmm0
410 aesdec 192(ctx), %xmm0
411 aesdec 176(ctx), %xmm0
412 aesdec 160(ctx), %xmm0
413 aesdec 144(ctx), %xmm0
414 aesdec 128(ctx), %xmm0
415 aesdec 112(ctx), %xmm0
416 aesdec 96(ctx), %xmm0
417 aesdec 80(ctx), %xmm0
418 aesdec 64(ctx), %xmm0
419 aesdec 48(ctx), %xmm0
420 aesdec 32(ctx), %xmm0
421 aesdec 16(ctx), %xmm0
422 aesdeclast (ctx), %xmm0
430 9: // AES-256 Decrypt : special case expanded key is not 16-byte aligned
432 movaps %xmm1, 16(r13) // save xmm1 into stack
434 movups 224(ctx), %xmm1
436 movups 208(ctx), %xmm1
438 movups 192(ctx), %xmm1
440 movups 176(ctx), %xmm1
442 movups 160(ctx), %xmm1
444 movups 144(ctx), %xmm1
446 movups 128(ctx), %xmm1
448 movups 112(ctx), %xmm1
450 movups 96(ctx), %xmm1
452 movups 80(ctx), %xmm1
454 movups 64(ctx), %xmm1
456 movups 48(ctx), %xmm1
458 movups 32(ctx), %xmm1
460 movups 16(ctx), %xmm1
463 aesdeclast %xmm1, %xmm0
468 movaps 16(r13), %xmm1