3 # ====================================================================
4 # Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
5 # project. The module is, however, dual licensed under OpenSSL and
6 # CRYPTOGAMS licenses depending on where you obtain it. For further
7 # details see http://www.openssl.org/~appro/cryptogams/.
8 # ====================================================================
10 # This module implements support for Intel AES-NI extension. In
11 # OpenSSL context it's used with Intel engine, but can also be used as
12 # drop-in replacement for crypto/aes/asm/aes-x86_64.pl [see below for
17 # Given aes(enc|dec) instructions' latency asymptotic performance for
18 # non-parallelizable modes such as CBC encrypt is 3.75 cycles per byte
19 # processed with 128-bit key. And given their throughput asymptotic
20 # performance for parallelizable modes is 1.25 cycles per byte. Being
21 # asymptotic limit it's not something you commonly achieve in reality,
22 # but how close does one get? Below are results collected for
23 # different modes and block sized. Pairs of numbers are for en-/
26 # 16-byte 64-byte 256-byte 1-KB 8-KB
27 # ECB 4.25/4.25 1.38/1.38 1.28/1.28 1.26/1.26 1.26/1.26
28 # CTR 5.42/5.42 1.92/1.92 1.44/1.44 1.28/1.28 1.26/1.26
29 # CBC 4.38/4.43 4.15/1.43 4.07/1.32 4.07/1.29 4.06/1.28
30 # CCM 5.66/9.42 4.42/5.41 4.16/4.40 4.09/4.15 4.06/4.07
31 # OFB 5.42/5.42 4.64/4.64 4.44/4.44 4.39/4.39 4.38/4.38
32 # CFB 5.73/5.85 5.56/5.62 5.48/5.56 5.47/5.55 5.47/5.55
34 # ECB, CTR, CBC and CCM results are free from EVP overhead. This means
35 # that otherwise used 'openssl speed -evp aes-128-??? -engine aesni
36 # [-decrypt]' will exhibit 10-15% worse results for smaller blocks.
37 # The results were collected with specially crafted speed.c benchmark
38 # in order to compare them with results reported in "Intel Advanced
39 # Encryption Standard (AES) New Instruction Set" White Paper Revision
40 # 3.0 dated May 2010. All above results are consistently better. This
41 # module also provides better performance for block sizes smaller than
42 # 128 bytes in points *not* represented in the above table.
44 # Looking at the results for 8-KB buffer.
46 # CFB and OFB results are far from the limit, because implementation
47 # uses "generic" CRYPTO_[c|o]fb128_encrypt interfaces relying on
48 # single-block aesni_encrypt, which is not the most optimal way to go.
49 # CBC encrypt result is unexpectedly high and there is no documented
50 # explanation for it. Seemingly there is a small penalty for feeding
51 # the result back to AES unit the way it's done in CBC mode. There is
52 # nothing one can do and the result appears optimal. CCM result is
53 # identical to CBC, because CBC-MAC is essentially CBC encrypt without
54 # saving output. CCM CTR "stays invisible," because it's neatly
55 # interleaved wih CBC-MAC. This provides ~30% improvement over
56 # "straghtforward" CCM implementation with CTR and CBC-MAC performed
57 # disjointly. Parallelizable modes practically achieve the theoretical
60 # Looking at how results vary with buffer size.
62 # Curves are practically saturated at 1-KB buffer size. In most cases
63 # "256-byte" performance is >95%, and "64-byte" is ~90% of "8-KB" one.
64 # CTR curve doesn't follow this pattern and is "slowest" changing one
65 # with "256-byte" result being 87% of "8-KB." This is because overhead
66 # in CTR mode is most computationally intensive. Small-block CCM
67 # decrypt is slower than encrypt, because first CTR and last CBC-MAC
68 # iterations can't be interleaved.
70 # Results for 192- and 256-bit keys.
72 # EVP-free results were observed to scale perfectly with number of
73 # rounds for larger block sizes, i.e. 192-bit result being 10/12 times
74 # lower and 256-bit one - 10/14. Well, in CBC encrypt case differences
75 # are a tad smaller, because the above mentioned penalty biases all
76 # results by same constant value. In similar way function call
77 # overhead affects small-block performance, as well as OFB and CFB
78 # results. Differences are not large, most common coefficients are
79 # 10/11.7 and 10/13.4 (as opposite to 10/12.0 and 10/14.0), but one
80 # observe even 10/11.2 and 10/12.4 (CTR, OFB, CFB)...
84 # While Westmere processor features 6 cycles latency for aes[enc|dec]
85 # instructions, which can be scheduled every second cycle, Sandy
86 # Bridge spends 8 cycles per instruction, but it can schedule them
87 # every cycle. This means that code targeting Westmere would perform
88 # suboptimally on Sandy Bridge. Therefore this update.
90 # In addition, non-parallelizable CBC encrypt (as well as CCM) is
91 # optimized. Relative improvement might appear modest, 8% on Westmere,
92 # but in absolute terms it's 3.77 cycles per byte encrypted with
93 # 128-bit key on Westmere, and 5.07 - on Sandy Bridge. These numbers
94 # should be compared to asymptotic limits of 3.75 for Westmere and
95 # 5.00 for Sandy Bridge. Actually, the fact that they get this close
96 # to asymptotic limits is quite amazing. Indeed, the limit is
97 # calculated as latency times number of rounds, 10 for 128-bit key,
98 # and divided by 16, the number of bytes in block, or in other words
99 # it accounts *solely* for aesenc instructions. But there are extra
100 # instructions, and numbers so close to the asymptotic limits mean
101 # that it's as if it takes as little as *one* additional cycle to
102 # execute all of them. How is it possible? It is possible thanks to
103 # out-of-order execution logic, which manages to overlap post-
104 # processing of previous block, things like saving the output, with
105 # actual encryption of current block, as well as pre-processing of
106 # current block, things like fetching input and xor-ing it with
107 # 0-round element of the key schedule, with actual encryption of
108 # previous block. Keep this in mind...
110 # For parallelizable modes, such as ECB, CBC decrypt, CTR, higher
111 # performance is achieved by interleaving instructions working on
112 # independent blocks. In which case asymptotic limit for such modes
113 # can be obtained by dividing above mentioned numbers by AES
114 # instructions' interleave factor. Westmere can execute at most 3
115 # instructions at a time, meaning that optimal interleave factor is 3,
116 # and that's where the "magic" number of 1.25 come from. "Optimal
117 # interleave factor" means that increase of interleave factor does
118 # not improve performance. The formula has proven to reflect reality
119 # pretty well on Westmere... Sandy Bridge on the other hand can
120 # execute up to 8 AES instructions at a time, so how does varying
121 # interleave factor affect the performance? Here is table for ECB
122 # (numbers are cycles per byte processed with 128-bit key):
124 # instruction interleave factor 3x 6x 8x
125 # theoretical asymptotic limit 1.67 0.83 0.625
126 # measured performance for 8KB block 1.05 0.86 0.84
128 # "as if" interleave factor 4.7x 5.8x 6.0x
130 # Further data for other parallelizable modes:
132 # CBC decrypt 1.16 0.93 0.74
135 # Well, given 3x column it's probably inappropriate to call the limit
136 # asymptotic, if it can be surpassed, isn't it? What happens there?
137 # Rewind to CBC paragraph for the answer. Yes, out-of-order execution
138 # magic is responsible for this. Processor overlaps not only the
139 # additional instructions with AES ones, but even AES instuctions
140 # processing adjacent triplets of independent blocks. In the 6x case
141 # additional instructions still claim disproportionally small amount
142 # of additional cycles, but in 8x case number of instructions must be
143 # a tad too high for out-of-order logic to cope with, and AES unit
144 # remains underutilized... As you can see 8x interleave is hardly
145 # justifiable, so there no need to feel bad that 32-bit aesni-x86.pl
146 # utilizies 6x interleave because of limited register bank capacity.
148 # Higher interleave factors do have negative impact on Westmere
149 # performance. While for ECB mode it's negligible ~1.5%, other
150 # parallelizables perform ~5% worse, which is outweighed by ~25%
151 # improvement on Sandy Bridge. To balance regression on Westmere
152 # CTR mode was implemented with 6x aesenc interleave factor.
156 # Add aesni_xts_[en|de]crypt. Westmere spends 1.33 cycles processing
157 # one byte out of 8KB with 128-bit key, Sandy Bridge - 0.97. Just like
158 # in CTR mode AES instruction interleave factor was chosen to be 6x.
160 ######################################################################
161 # For reference, AMD Bulldozer spends 5.77 cycles per byte processed
162 # with 128-bit key in CBC encrypt and 0.70 cycles in CBC decrypt, 0.70
163 # in ECB, 0.71 in CTR, 0.95 in XTS... This means that aes[enc|dec]
164 # instruction latency is 9 cycles and that they can be issued every
167 $PREFIX="aesni"; # if $PREFIX is set to "AES", the script
168 # generates drop-in replacement for
169 # crypto/aes/asm/aes-x86_64.pl:-)
173 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
175 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
177 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
178 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
179 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
180 die "can't locate x86_64-xlate.pl";
182 open OUT,"| \"$^X\" $xlate $flavour $output";
185 $movkey = $PREFIX eq "aesni" ? "movups" : "movups";
186 @_4args=$win64? ("%rcx","%rdx","%r8", "%r9") : # Win64 order
187 ("%rdi","%rsi","%rdx","%rcx"); # Unix order
191 $rounds="%eax"; # input to and changed by aesni_[en|de]cryptN !!!
192 # this is natural Unix argument order for public $PREFIX_[ecb|cbc]_encrypt ...
196 $key="%rcx"; # input to and changed by aesni_[en|de]cryptN !!!
197 $ivp="%r8"; # cbc, ctr, ...
199 $rnds_="%r10d"; # backup copy for $rounds
200 $key_="%r11"; # backup copy for $key
202 # %xmm register layout
203 $rndkey0="%xmm0"; $rndkey1="%xmm1";
204 $inout0="%xmm2"; $inout1="%xmm3";
205 $inout2="%xmm4"; $inout3="%xmm5";
206 $inout4="%xmm6"; $inout5="%xmm7";
207 $inout6="%xmm8"; $inout7="%xmm9";
209 $in2="%xmm6"; $in1="%xmm7"; # used in CBC decrypt, CTR, ...
210 $in0="%xmm8"; $iv="%xmm9";
212 # Inline version of internal aesni_[en|de]crypt1.
214 # Why folded loop? Because aes[enc|dec] is slow enough to accommodate
215 # cycles which take care of loop variables...
217 sub aesni_generate1 {
218 my ($p,$key,$rounds,$inout,$ivec)=@_; $inout=$inout0 if (!defined($inout));
221 $movkey ($key),$rndkey0
222 $movkey 16($key),$rndkey1
224 $code.=<<___ if (defined($ivec));
229 $code.=<<___ if (!defined($ivec));
231 xorps $rndkey0,$inout
235 aes${p} $rndkey1,$inout
237 $movkey ($key),$rndkey1
239 jnz .Loop_${p}1_$sn # loop body is 16 bytes
240 aes${p}last $rndkey1,$inout
243 # void $PREFIX_[en|de]crypt (const void *inp,void *out,const AES_KEY *key);
245 { my ($inp,$out,$key) = @_4args;
248 .globl ${PREFIX}_encrypt
249 .type ${PREFIX}_encrypt,\@abi-omnipotent
252 movups ($inp),$inout0 # load input
253 mov 240($key),$rounds # key->rounds
255 &aesni_generate1("enc",$key,$rounds);
257 movups $inout0,($out) # output
259 .size ${PREFIX}_encrypt,.-${PREFIX}_encrypt
261 .globl ${PREFIX}_decrypt
262 .type ${PREFIX}_decrypt,\@abi-omnipotent
265 movups ($inp),$inout0 # load input
266 mov 240($key),$rounds # key->rounds
268 &aesni_generate1("dec",$key,$rounds);
270 movups $inout0,($out) # output
272 .size ${PREFIX}_decrypt, .-${PREFIX}_decrypt
276 # _aesni_[en|de]cryptN are private interfaces, N denotes interleave
277 # factor. Why 3x subroutine were originally used in loops? Even though
278 # aes[enc|dec] latency was originally 6, it could be scheduled only
279 # every *2nd* cycle. Thus 3x interleave was the one providing optimal
280 # utilization, i.e. when subroutine's throughput is virtually same as
281 # of non-interleaved subroutine [for number of input blocks up to 3].
282 # This is why it makes no sense to implement 2x subroutine.
283 # aes[enc|dec] latency in next processor generation is 8, but the
284 # instructions can be scheduled every cycle. Optimal interleave for
285 # new processor is therefore 8x...
286 sub aesni_generate3 {
288 # As already mentioned it takes in $key and $rounds, which are *not*
289 # preserved. $inout[0-2] is cipher/clear text...
291 .type _aesni_${dir}rypt3,\@abi-omnipotent
294 $movkey ($key),$rndkey0
296 $movkey 16($key),$rndkey1
298 xorps $rndkey0,$inout0
299 xorps $rndkey0,$inout1
300 xorps $rndkey0,$inout2
301 $movkey ($key),$rndkey0
304 aes${dir} $rndkey1,$inout0
305 aes${dir} $rndkey1,$inout1
307 aes${dir} $rndkey1,$inout2
308 $movkey 16($key),$rndkey1
309 aes${dir} $rndkey0,$inout0
310 aes${dir} $rndkey0,$inout1
312 aes${dir} $rndkey0,$inout2
313 $movkey ($key),$rndkey0
316 aes${dir} $rndkey1,$inout0
317 aes${dir} $rndkey1,$inout1
318 aes${dir} $rndkey1,$inout2
319 aes${dir}last $rndkey0,$inout0
320 aes${dir}last $rndkey0,$inout1
321 aes${dir}last $rndkey0,$inout2
323 .size _aesni_${dir}rypt3,.-_aesni_${dir}rypt3
326 # 4x interleave is implemented to improve small block performance,
327 # most notably [and naturally] 4 block by ~30%. One can argue that one
328 # should have implemented 5x as well, but improvement would be <20%,
329 # so it's not worth it...
330 sub aesni_generate4 {
332 # As already mentioned it takes in $key and $rounds, which are *not*
333 # preserved. $inout[0-3] is cipher/clear text...
335 .type _aesni_${dir}rypt4,\@abi-omnipotent
338 $movkey ($key),$rndkey0
340 $movkey 16($key),$rndkey1
342 xorps $rndkey0,$inout0
343 xorps $rndkey0,$inout1
344 xorps $rndkey0,$inout2
345 xorps $rndkey0,$inout3
346 $movkey ($key),$rndkey0
349 aes${dir} $rndkey1,$inout0
350 aes${dir} $rndkey1,$inout1
352 aes${dir} $rndkey1,$inout2
353 aes${dir} $rndkey1,$inout3
354 $movkey 16($key),$rndkey1
355 aes${dir} $rndkey0,$inout0
356 aes${dir} $rndkey0,$inout1
358 aes${dir} $rndkey0,$inout2
359 aes${dir} $rndkey0,$inout3
360 $movkey ($key),$rndkey0
363 aes${dir} $rndkey1,$inout0
364 aes${dir} $rndkey1,$inout1
365 aes${dir} $rndkey1,$inout2
366 aes${dir} $rndkey1,$inout3
367 aes${dir}last $rndkey0,$inout0
368 aes${dir}last $rndkey0,$inout1
369 aes${dir}last $rndkey0,$inout2
370 aes${dir}last $rndkey0,$inout3
372 .size _aesni_${dir}rypt4,.-_aesni_${dir}rypt4
375 sub aesni_generate6 {
377 # As already mentioned it takes in $key and $rounds, which are *not*
378 # preserved. $inout[0-5] is cipher/clear text...
380 .type _aesni_${dir}rypt6,\@abi-omnipotent
383 $movkey ($key),$rndkey0
385 $movkey 16($key),$rndkey1
387 xorps $rndkey0,$inout0
388 pxor $rndkey0,$inout1
389 aes${dir} $rndkey1,$inout0
390 pxor $rndkey0,$inout2
391 aes${dir} $rndkey1,$inout1
392 pxor $rndkey0,$inout3
393 aes${dir} $rndkey1,$inout2
394 pxor $rndkey0,$inout4
395 aes${dir} $rndkey1,$inout3
396 pxor $rndkey0,$inout5
398 aes${dir} $rndkey1,$inout4
399 $movkey ($key),$rndkey0
400 aes${dir} $rndkey1,$inout5
401 jmp .L${dir}_loop6_enter
404 aes${dir} $rndkey1,$inout0
405 aes${dir} $rndkey1,$inout1
407 aes${dir} $rndkey1,$inout2
408 aes${dir} $rndkey1,$inout3
409 aes${dir} $rndkey1,$inout4
410 aes${dir} $rndkey1,$inout5
411 .L${dir}_loop6_enter: # happens to be 16-byte aligned
412 $movkey 16($key),$rndkey1
413 aes${dir} $rndkey0,$inout0
414 aes${dir} $rndkey0,$inout1
416 aes${dir} $rndkey0,$inout2
417 aes${dir} $rndkey0,$inout3
418 aes${dir} $rndkey0,$inout4
419 aes${dir} $rndkey0,$inout5
420 $movkey ($key),$rndkey0
423 aes${dir} $rndkey1,$inout0
424 aes${dir} $rndkey1,$inout1
425 aes${dir} $rndkey1,$inout2
426 aes${dir} $rndkey1,$inout3
427 aes${dir} $rndkey1,$inout4
428 aes${dir} $rndkey1,$inout5
429 aes${dir}last $rndkey0,$inout0
430 aes${dir}last $rndkey0,$inout1
431 aes${dir}last $rndkey0,$inout2
432 aes${dir}last $rndkey0,$inout3
433 aes${dir}last $rndkey0,$inout4
434 aes${dir}last $rndkey0,$inout5
436 .size _aesni_${dir}rypt6,.-_aesni_${dir}rypt6
439 sub aesni_generate8 {
441 # As already mentioned it takes in $key and $rounds, which are *not*
442 # preserved. $inout[0-7] is cipher/clear text...
444 .type _aesni_${dir}rypt8,\@abi-omnipotent
447 $movkey ($key),$rndkey0
449 $movkey 16($key),$rndkey1
451 xorps $rndkey0,$inout0
452 xorps $rndkey0,$inout1
453 aes${dir} $rndkey1,$inout0
454 pxor $rndkey0,$inout2
455 aes${dir} $rndkey1,$inout1
456 pxor $rndkey0,$inout3
457 aes${dir} $rndkey1,$inout2
458 pxor $rndkey0,$inout4
459 aes${dir} $rndkey1,$inout3
460 pxor $rndkey0,$inout5
462 aes${dir} $rndkey1,$inout4
463 pxor $rndkey0,$inout6
464 aes${dir} $rndkey1,$inout5
465 pxor $rndkey0,$inout7
466 $movkey ($key),$rndkey0
467 aes${dir} $rndkey1,$inout6
468 aes${dir} $rndkey1,$inout7
469 $movkey 16($key),$rndkey1
470 jmp .L${dir}_loop8_enter
473 aes${dir} $rndkey1,$inout0
474 aes${dir} $rndkey1,$inout1
476 aes${dir} $rndkey1,$inout2
477 aes${dir} $rndkey1,$inout3
478 aes${dir} $rndkey1,$inout4
479 aes${dir} $rndkey1,$inout5
480 aes${dir} $rndkey1,$inout6
481 aes${dir} $rndkey1,$inout7
482 $movkey 16($key),$rndkey1
483 .L${dir}_loop8_enter: # happens to be 16-byte aligned
484 aes${dir} $rndkey0,$inout0
485 aes${dir} $rndkey0,$inout1
487 aes${dir} $rndkey0,$inout2
488 aes${dir} $rndkey0,$inout3
489 aes${dir} $rndkey0,$inout4
490 aes${dir} $rndkey0,$inout5
491 aes${dir} $rndkey0,$inout6
492 aes${dir} $rndkey0,$inout7
493 $movkey ($key),$rndkey0
496 aes${dir} $rndkey1,$inout0
497 aes${dir} $rndkey1,$inout1
498 aes${dir} $rndkey1,$inout2
499 aes${dir} $rndkey1,$inout3
500 aes${dir} $rndkey1,$inout4
501 aes${dir} $rndkey1,$inout5
502 aes${dir} $rndkey1,$inout6
503 aes${dir} $rndkey1,$inout7
504 aes${dir}last $rndkey0,$inout0
505 aes${dir}last $rndkey0,$inout1
506 aes${dir}last $rndkey0,$inout2
507 aes${dir}last $rndkey0,$inout3
508 aes${dir}last $rndkey0,$inout4
509 aes${dir}last $rndkey0,$inout5
510 aes${dir}last $rndkey0,$inout6
511 aes${dir}last $rndkey0,$inout7
513 .size _aesni_${dir}rypt8,.-_aesni_${dir}rypt8
516 &aesni_generate3("enc") if ($PREFIX eq "aesni");
517 &aesni_generate3("dec");
518 &aesni_generate4("enc") if ($PREFIX eq "aesni");
519 &aesni_generate4("dec");
520 &aesni_generate6("enc") if ($PREFIX eq "aesni");
521 &aesni_generate6("dec");
522 &aesni_generate8("enc") if ($PREFIX eq "aesni");
523 &aesni_generate8("dec");
525 if ($PREFIX eq "aesni") {
526 ########################################################################
527 # void aesni_ecb_encrypt (const void *in, void *out,
528 # size_t length, const AES_KEY *key,
531 .globl aesni_ecb_encrypt
532 .type aesni_ecb_encrypt,\@function,5
538 mov 240($key),$rounds # key->rounds
539 $movkey ($key),$rndkey0
540 mov $key,$key_ # backup $key
541 mov $rounds,$rnds_ # backup $rounds
542 test %r8d,%r8d # 5th argument
544 #--------------------------- ECB ENCRYPT ------------------------------#
548 movdqu ($inp),$inout0
549 movdqu 0x10($inp),$inout1
550 movdqu 0x20($inp),$inout2
551 movdqu 0x30($inp),$inout3
552 movdqu 0x40($inp),$inout4
553 movdqu 0x50($inp),$inout5
554 movdqu 0x60($inp),$inout6
555 movdqu 0x70($inp),$inout7
558 jmp .Lecb_enc_loop8_enter
561 movups $inout0,($out)
562 mov $key_,$key # restore $key
563 movdqu ($inp),$inout0
564 mov $rnds_,$rounds # restore $rounds
565 movups $inout1,0x10($out)
566 movdqu 0x10($inp),$inout1
567 movups $inout2,0x20($out)
568 movdqu 0x20($inp),$inout2
569 movups $inout3,0x30($out)
570 movdqu 0x30($inp),$inout3
571 movups $inout4,0x40($out)
572 movdqu 0x40($inp),$inout4
573 movups $inout5,0x50($out)
574 movdqu 0x50($inp),$inout5
575 movups $inout6,0x60($out)
576 movdqu 0x60($inp),$inout6
577 movups $inout7,0x70($out)
579 movdqu 0x70($inp),$inout7
581 .Lecb_enc_loop8_enter:
588 movups $inout0,($out)
589 mov $key_,$key # restore $key
590 movups $inout1,0x10($out)
591 mov $rnds_,$rounds # restore $rounds
592 movups $inout2,0x20($out)
593 movups $inout3,0x30($out)
594 movups $inout4,0x40($out)
595 movups $inout5,0x50($out)
596 movups $inout6,0x60($out)
597 movups $inout7,0x70($out)
603 movups ($inp),$inout0
606 movups 0x10($inp),$inout1
608 movups 0x20($inp),$inout2
611 movups 0x30($inp),$inout3
613 movups 0x40($inp),$inout4
616 movups 0x50($inp),$inout5
618 movdqu 0x60($inp),$inout6
620 movups $inout0,($out)
621 movups $inout1,0x10($out)
622 movups $inout2,0x20($out)
623 movups $inout3,0x30($out)
624 movups $inout4,0x40($out)
625 movups $inout5,0x50($out)
626 movups $inout6,0x60($out)
631 &aesni_generate1("enc",$key,$rounds);
633 movups $inout0,($out)
637 xorps $inout2,$inout2
639 movups $inout0,($out)
640 movups $inout1,0x10($out)
645 movups $inout0,($out)
646 movups $inout1,0x10($out)
647 movups $inout2,0x20($out)
652 movups $inout0,($out)
653 movups $inout1,0x10($out)
654 movups $inout2,0x20($out)
655 movups $inout3,0x30($out)
659 xorps $inout5,$inout5
661 movups $inout0,($out)
662 movups $inout1,0x10($out)
663 movups $inout2,0x20($out)
664 movups $inout3,0x30($out)
665 movups $inout4,0x40($out)
670 movups $inout0,($out)
671 movups $inout1,0x10($out)
672 movups $inout2,0x20($out)
673 movups $inout3,0x30($out)
674 movups $inout4,0x40($out)
675 movups $inout5,0x50($out)
677 \f#--------------------------- ECB DECRYPT ------------------------------#
683 movdqu ($inp),$inout0
684 movdqu 0x10($inp),$inout1
685 movdqu 0x20($inp),$inout2
686 movdqu 0x30($inp),$inout3
687 movdqu 0x40($inp),$inout4
688 movdqu 0x50($inp),$inout5
689 movdqu 0x60($inp),$inout6
690 movdqu 0x70($inp),$inout7
693 jmp .Lecb_dec_loop8_enter
696 movups $inout0,($out)
697 mov $key_,$key # restore $key
698 movdqu ($inp),$inout0
699 mov $rnds_,$rounds # restore $rounds
700 movups $inout1,0x10($out)
701 movdqu 0x10($inp),$inout1
702 movups $inout2,0x20($out)
703 movdqu 0x20($inp),$inout2
704 movups $inout3,0x30($out)
705 movdqu 0x30($inp),$inout3
706 movups $inout4,0x40($out)
707 movdqu 0x40($inp),$inout4
708 movups $inout5,0x50($out)
709 movdqu 0x50($inp),$inout5
710 movups $inout6,0x60($out)
711 movdqu 0x60($inp),$inout6
712 movups $inout7,0x70($out)
714 movdqu 0x70($inp),$inout7
716 .Lecb_dec_loop8_enter:
720 $movkey ($key_),$rndkey0
724 movups $inout0,($out)
725 mov $key_,$key # restore $key
726 movups $inout1,0x10($out)
727 mov $rnds_,$rounds # restore $rounds
728 movups $inout2,0x20($out)
729 movups $inout3,0x30($out)
730 movups $inout4,0x40($out)
731 movups $inout5,0x50($out)
732 movups $inout6,0x60($out)
733 movups $inout7,0x70($out)
739 movups ($inp),$inout0
742 movups 0x10($inp),$inout1
744 movups 0x20($inp),$inout2
747 movups 0x30($inp),$inout3
749 movups 0x40($inp),$inout4
752 movups 0x50($inp),$inout5
754 movups 0x60($inp),$inout6
755 $movkey ($key),$rndkey0
757 movups $inout0,($out)
758 movups $inout1,0x10($out)
759 movups $inout2,0x20($out)
760 movups $inout3,0x30($out)
761 movups $inout4,0x40($out)
762 movups $inout5,0x50($out)
763 movups $inout6,0x60($out)
768 &aesni_generate1("dec",$key,$rounds);
770 movups $inout0,($out)
774 xorps $inout2,$inout2
776 movups $inout0,($out)
777 movups $inout1,0x10($out)
782 movups $inout0,($out)
783 movups $inout1,0x10($out)
784 movups $inout2,0x20($out)
789 movups $inout0,($out)
790 movups $inout1,0x10($out)
791 movups $inout2,0x20($out)
792 movups $inout3,0x30($out)
796 xorps $inout5,$inout5
798 movups $inout0,($out)
799 movups $inout1,0x10($out)
800 movups $inout2,0x20($out)
801 movups $inout3,0x30($out)
802 movups $inout4,0x40($out)
807 movups $inout0,($out)
808 movups $inout1,0x10($out)
809 movups $inout2,0x20($out)
810 movups $inout3,0x30($out)
811 movups $inout4,0x40($out)
812 movups $inout5,0x50($out)
816 .size aesni_ecb_encrypt,.-aesni_ecb_encrypt
820 ######################################################################
821 # void aesni_ccm64_[en|de]crypt_blocks (const void *in, void *out,
822 # size_t blocks, const AES_KEY *key,
823 # const char *ivec,char *cmac);
825 # Handles only complete blocks, operates on 64-bit counter and
826 # does not update *ivec! Nor does it finalize CMAC value
827 # (see engine/eng_aesni.c for details)
830 my $cmac="%r9"; # 6th argument
832 my $increment="%xmm6";
833 my $bswap_mask="%xmm7";
836 .globl aesni_ccm64_encrypt_blocks
837 .type aesni_ccm64_encrypt_blocks,\@function,6
839 aesni_ccm64_encrypt_blocks:
841 $code.=<<___ if ($win64);
844 movaps %xmm7,0x10(%rsp)
845 movaps %xmm8,0x20(%rsp)
846 movaps %xmm9,0x30(%rsp)
850 mov 240($key),$rounds # key->rounds
852 movdqa .Lincrement64(%rip),$increment
853 movdqa .Lbswap_mask(%rip),$bswap_mask
857 movdqu ($cmac),$inout1
860 pshufb $bswap_mask,$iv
861 jmp .Lccm64_enc_outer
864 $movkey ($key_),$rndkey0
866 movups ($inp),$in0 # load inp
868 xorps $rndkey0,$inout0 # counter
869 $movkey 16($key_),$rndkey1
872 xorps $rndkey0,$inout1 # cmac^=inp
873 $movkey ($key),$rndkey0
876 aesenc $rndkey1,$inout0
878 aesenc $rndkey1,$inout1
879 $movkey 16($key),$rndkey1
880 aesenc $rndkey0,$inout0
882 aesenc $rndkey0,$inout1
883 $movkey 0($key),$rndkey0
884 jnz .Lccm64_enc2_loop
885 aesenc $rndkey1,$inout0
886 aesenc $rndkey1,$inout1
888 aesenclast $rndkey0,$inout0
889 aesenclast $rndkey0,$inout1
893 xorps $inout0,$in0 # inp ^= E(iv)
895 movups $in0,($out) # save output
897 pshufb $bswap_mask,$inout0
898 jnz .Lccm64_enc_outer
900 movups $inout1,($cmac)
902 $code.=<<___ if ($win64);
904 movaps 0x10(%rsp),%xmm7
905 movaps 0x20(%rsp),%xmm8
906 movaps 0x30(%rsp),%xmm9
912 .size aesni_ccm64_encrypt_blocks,.-aesni_ccm64_encrypt_blocks
914 ######################################################################
916 .globl aesni_ccm64_decrypt_blocks
917 .type aesni_ccm64_decrypt_blocks,\@function,6
919 aesni_ccm64_decrypt_blocks:
921 $code.=<<___ if ($win64);
924 movaps %xmm7,0x10(%rsp)
925 movaps %xmm8,0x20(%rsp)
926 movaps %xmm9,0x30(%rsp)
930 mov 240($key),$rounds # key->rounds
932 movdqu ($cmac),$inout1
933 movdqa .Lincrement64(%rip),$increment
934 movdqa .Lbswap_mask(%rip),$bswap_mask
939 pshufb $bswap_mask,$iv
941 &aesni_generate1("enc",$key,$rounds);
943 movups ($inp),$in0 # load inp
946 jmp .Lccm64_dec_outer
949 xorps $inout0,$in0 # inp ^= E(iv)
952 movups $in0,($out) # save output
954 pshufb $bswap_mask,$inout0
959 $movkey ($key_),$rndkey0
961 $movkey 16($key_),$rndkey1
964 xorps $rndkey0,$inout0
965 xorps $in0,$inout1 # cmac^=out
966 $movkey ($key),$rndkey0
969 aesenc $rndkey1,$inout0
971 aesenc $rndkey1,$inout1
972 $movkey 16($key),$rndkey1
973 aesenc $rndkey0,$inout0
975 aesenc $rndkey0,$inout1
976 $movkey 0($key),$rndkey0
977 jnz .Lccm64_dec2_loop
978 movups ($inp),$in0 # load inp
980 aesenc $rndkey1,$inout0
981 aesenc $rndkey1,$inout1
983 aesenclast $rndkey0,$inout0
984 aesenclast $rndkey0,$inout1
985 jmp .Lccm64_dec_outer
989 #xorps $in0,$inout1 # cmac^=out
991 &aesni_generate1("enc",$key_,$rounds,$inout1,$in0);
993 movups $inout1,($cmac)
995 $code.=<<___ if ($win64);
997 movaps 0x10(%rsp),%xmm7
998 movaps 0x20(%rsp),%xmm8
999 movaps 0x30(%rsp),%xmm9
1005 .size aesni_ccm64_decrypt_blocks,.-aesni_ccm64_decrypt_blocks
1008 ######################################################################
1009 # void aesni_ctr32_encrypt_blocks (const void *in, void *out,
1010 # size_t blocks, const AES_KEY *key,
1011 # const char *ivec);
1013 # Handles only complete blocks, operates on 32-bit counter and
1014 # does not update *ivec! (see crypto/modes/ctr128.c for details)
1016 # Overhaul based on suggestions from Shay Gueron and Vlad Krasnov,
1017 # http://rt.openssl.org/Ticket/Display.html?id=3021&user=guest&pass=guest.
1018 # Keywords are full unroll and modulo-schedule counter calculations
1019 # with zero-round key xor.
1021 my ($in0,$in1,$in2,$in3,$in4,$in5)=map("%xmm$_",(10..15));
1022 my ($key0,$ctr)=("${key_}d","${ivp}d");
1023 my $frame_size = 0x80 + ($win64?160:0);
1026 .globl aesni_ctr32_encrypt_blocks
1027 .type aesni_ctr32_encrypt_blocks,\@function,5
1029 aesni_ctr32_encrypt_blocks:
1032 sub \$$frame_size,%rsp
1033 and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
1035 $code.=<<___ if ($win64);
1036 movaps %xmm6,-0xa8(%rax)
1037 movaps %xmm7,-0x98(%rax)
1038 movaps %xmm8,-0x88(%rax)
1039 movaps %xmm9,-0x78(%rax)
1040 movaps %xmm10,-0x68(%rax)
1041 movaps %xmm11,-0x58(%rax)
1042 movaps %xmm12,-0x48(%rax)
1043 movaps %xmm13,-0x38(%rax)
1044 movaps %xmm14,-0x28(%rax)
1045 movaps %xmm15,-0x18(%rax)
1052 je .Lctr32_one_shortcut
1054 movdqu ($ivp),$inout0
1055 movdqu ($key),$rndkey0
1056 mov 12($ivp),$ctr # counter LSB
1057 pxor $rndkey0,$inout0
1058 mov 12($key),$key0 # 0-round key LSB
1059 movdqa $inout0,0x00(%rsp) # populate counter block
1061 movdqa $inout0,$inout1
1062 movdqa $inout0,$inout2
1063 movdqa $inout0,$inout3
1064 movdqa $inout0,0x40(%rsp)
1065 movdqa $inout0,0x50(%rsp)
1066 movdqa $inout0,0x60(%rsp)
1067 movdqa $inout0,0x70(%rsp)
1069 mov 240($key),$rounds # key->rounds
1077 pinsrd \$3,%r9d,$inout1
1079 movdqa $inout1,0x10(%rsp)
1080 pinsrd \$3,%r10d,$inout2
1083 movdqa $inout2,0x20(%rsp)
1086 pinsrd \$3,%r9d,$inout3
1088 movdqa $inout3,0x30(%rsp)
1090 mov %r10d,0x40+12(%rsp)
1095 mov %r9d,0x50+12(%rsp)
1098 mov %r10d,0x60+12(%rsp)
1101 mov %r9d,0x70+12(%rsp)
1103 $movkey 0x10($key),$rndkey1
1105 movdqa 0x40(%rsp),$inout4
1106 movdqa 0x50(%rsp),$inout5
1111 lea 0x80($key),$key # size optimization
1118 movdqa 0x60(%rsp),$inout6
1119 aesenc $rndkey1,$inout0
1121 movdqa 0x70(%rsp),$inout7
1122 aesenc $rndkey1,$inout1
1124 $movkey 0x20-0x80($key),$rndkey0
1125 aesenc $rndkey1,$inout2
1127 aesenc $rndkey1,$inout3
1128 mov %r9d,0x00+12(%rsp)
1130 aesenc $rndkey1,$inout4
1131 aesenc $rndkey1,$inout5
1132 aesenc $rndkey1,$inout6
1133 aesenc $rndkey1,$inout7
1134 $movkey 0x30-0x80($key),$rndkey1
1136 for($i=2;$i<8;$i++) {
1137 my $rndkeyx = ($i&1)?$rndkey1:$rndkey0;
1139 aesenc $rndkeyx,$inout0
1140 aesenc $rndkeyx,$inout1
1142 aesenc $rndkeyx,$inout2
1144 aesenc $rndkeyx,$inout3
1145 mov %r9d,`0x10*($i-1)`+12(%rsp)
1147 aesenc $rndkeyx,$inout4
1148 aesenc $rndkeyx,$inout5
1149 aesenc $rndkeyx,$inout6
1150 aesenc $rndkeyx,$inout7
1151 $movkey `0x20+0x10*$i`-0x80($key),$rndkeyx
1155 aesenc $rndkey0,$inout0
1156 aesenc $rndkey0,$inout1
1158 aesenc $rndkey0,$inout2
1160 aesenc $rndkey0,$inout3
1161 mov %r9d,0x70+12(%rsp)
1162 aesenc $rndkey0,$inout4
1163 aesenc $rndkey0,$inout5
1164 aesenc $rndkey0,$inout6
1165 movdqu 0x00($inp),$in0
1166 aesenc $rndkey0,$inout7
1167 $movkey 0xa0-0x80($key),$rndkey0
1172 aesenc $rndkey1,$inout0
1173 aesenc $rndkey1,$inout1
1174 aesenc $rndkey1,$inout2
1175 aesenc $rndkey1,$inout3
1176 aesenc $rndkey1,$inout4
1177 aesenc $rndkey1,$inout5
1178 aesenc $rndkey1,$inout6
1179 aesenc $rndkey1,$inout7
1180 $movkey 0xb0-0x80($key),$rndkey1
1182 aesenc $rndkey0,$inout0
1183 aesenc $rndkey0,$inout1
1184 aesenc $rndkey0,$inout2
1185 aesenc $rndkey0,$inout3
1186 aesenc $rndkey0,$inout4
1187 aesenc $rndkey0,$inout5
1188 aesenc $rndkey0,$inout6
1189 aesenc $rndkey0,$inout7
1190 $movkey 0xc0-0x80($key),$rndkey0
1193 aesenc $rndkey1,$inout0
1194 aesenc $rndkey1,$inout1
1195 aesenc $rndkey1,$inout2
1196 aesenc $rndkey1,$inout3
1197 aesenc $rndkey1,$inout4
1198 aesenc $rndkey1,$inout5
1199 aesenc $rndkey1,$inout6
1200 aesenc $rndkey1,$inout7
1201 $movkey 0xd0-0x80($key),$rndkey1
1203 aesenc $rndkey0,$inout0
1204 aesenc $rndkey0,$inout1
1205 aesenc $rndkey0,$inout2
1206 aesenc $rndkey0,$inout3
1207 aesenc $rndkey0,$inout4
1208 aesenc $rndkey0,$inout5
1209 aesenc $rndkey0,$inout6
1210 aesenc $rndkey0,$inout7
1211 $movkey 0xe0-0x80($key),$rndkey0
1214 aesenc $rndkey1,$inout0
1215 movdqu 0x10($inp),$in1
1217 aesenc $rndkey1,$inout1
1218 movdqu 0x20($inp),$in2
1220 aesenc $rndkey1,$inout2
1221 movdqu 0x30($inp),$in3
1223 aesenc $rndkey1,$inout3
1224 movdqu 0x40($inp),$in4
1226 aesenc $rndkey1,$inout4
1227 movdqu 0x50($inp),$in5
1229 aesenc $rndkey1,$inout5
1231 aesenc $rndkey1,$inout6
1232 aesenc $rndkey1,$inout7
1233 movdqu 0x60($inp),$rndkey1
1235 aesenclast $in0,$inout0
1236 pxor $rndkey0,$rndkey1
1237 movdqu 0x70($inp),$in0
1239 aesenclast $in1,$inout1
1241 movdqa 0x00(%rsp),$in1 # load next counter block
1242 aesenclast $in2,$inout2
1243 movdqa 0x10(%rsp),$in2
1244 aesenclast $in3,$inout3
1245 movdqa 0x20(%rsp),$in3
1246 aesenclast $in4,$inout4
1247 movdqa 0x30(%rsp),$in4
1248 aesenclast $in5,$inout5
1249 movdqa 0x40(%rsp),$in5
1250 aesenclast $rndkey1,$inout6
1251 movdqa 0x50(%rsp),$rndkey0
1252 aesenclast $in0,$inout7
1253 $movkey 0x10-0x80($key),$rndkey1
1255 movups $inout0,($out) # store output
1257 movups $inout1,0x10($out)
1259 movups $inout2,0x20($out)
1261 movups $inout3,0x30($out)
1263 movups $inout4,0x40($out)
1265 movups $inout5,0x50($out)
1266 movdqa $rndkey0,$inout5
1267 movups $inout6,0x60($out)
1268 movups $inout7,0x70($out)
1276 lea -0x80($key),$key
1284 movdqa 0x60(%rsp),$inout6
1285 pxor $inout7,$inout7
1287 $movkey 16($key),$rndkey0
1288 aesenc $rndkey1,$inout0
1290 aesenc $rndkey1,$inout1
1292 aesenc $rndkey1,$inout2
1294 aesenc $rndkey1,$inout3
1296 aesenc $rndkey1,$inout4
1297 movups 0x10($inp),$in1
1298 aesenc $rndkey1,$inout5
1299 movups 0x20($inp),$in2
1300 aesenc $rndkey1,$inout6
1301 $movkey 16($key),$rndkey1
1303 call .Lenc_loop8_enter
1305 movdqu 0x30($inp),$in3
1307 movdqu 0x40($inp),$in0
1309 movdqu $inout0,($out)
1311 movdqu $inout1,0x10($out)
1313 movdqu $inout2,0x20($out)
1315 movdqu $inout3,0x30($out)
1316 movdqu $inout4,0x40($out)
1320 movups 0x50($inp),$in1
1322 movups $inout5,0x50($out)
1325 movups 0x60($inp),$in2
1327 movups $inout6,0x60($out)
1332 aesenc $rndkey1,$inout0
1334 aesenc $rndkey1,$inout1
1335 aesenc $rndkey1,$inout2
1336 aesenc $rndkey1,$inout3
1337 $movkey ($key),$rndkey1
1340 aesenclast $rndkey1,$inout0
1342 aesenclast $rndkey1,$inout1
1343 movups 0x10($inp),$in1
1344 aesenclast $rndkey1,$inout2
1345 movups 0x20($inp),$in2
1346 aesenclast $rndkey1,$inout3
1347 movups 0x30($inp),$in3
1350 movups $inout0,($out)
1352 movups $inout1,0x10($out)
1354 movdqu $inout2,0x20($out)
1356 movdqu $inout3,0x30($out)
1361 aesenc $rndkey1,$inout0
1363 aesenc $rndkey1,$inout1
1364 aesenc $rndkey1,$inout2
1365 $movkey ($key),$rndkey1
1368 aesenclast $rndkey1,$inout0
1369 aesenclast $rndkey1,$inout1
1370 aesenclast $rndkey1,$inout2
1374 movups $inout0,($out)
1378 movups 0x10($inp),$in1
1380 movups $inout1,0x10($out)
1383 movups 0x20($inp),$in2
1385 movups $inout2,0x20($out)
1389 .Lctr32_one_shortcut:
1390 movups ($ivp),$inout0
1392 mov 240($key),$rounds # key->rounds
1394 &aesni_generate1("enc",$key,$rounds);
1397 movups $inout0,($out)
1403 $code.=<<___ if ($win64);
1404 movaps -0xa0(%rbp),%xmm6
1405 movaps -0x90(%rbp),%xmm7
1406 movaps -0x80(%rbp),%xmm8
1407 movaps -0x70(%rbp),%xmm9
1408 movaps -0x60(%rbp),%xmm10
1409 movaps -0x50(%rbp),%xmm11
1410 movaps -0x40(%rbp),%xmm12
1411 movaps -0x30(%rbp),%xmm13
1412 movaps -0x20(%rbp),%xmm14
1413 movaps -0x10(%rbp),%xmm15
1420 .size aesni_ctr32_encrypt_blocks,.-aesni_ctr32_encrypt_blocks
1424 ######################################################################
1425 # void aesni_xts_[en|de]crypt(const char *inp,char *out,size_t len,
1426 # const AES_KEY *key1, const AES_KEY *key2
1427 # const unsigned char iv[16]);
1430 my @tweak=map("%xmm$_",(10..15));
1431 my ($twmask,$twres,$twtmp)=("%xmm8","%xmm9",@tweak[4]);
1432 my ($key2,$ivp,$len_)=("%r8","%r9","%r9");
1433 my $frame_size = 0x60 + ($win64?160:0);
1436 .globl aesni_xts_encrypt
1437 .type aesni_xts_encrypt,\@function,6
1442 sub \$$frame_size,%rsp
1443 and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
1445 $code.=<<___ if ($win64);
1446 movaps %xmm6,-0xa8(%rax)
1447 movaps %xmm7,-0x98(%rax)
1448 movaps %xmm8,-0x88(%rax)
1449 movaps %xmm9,-0x78(%rax)
1450 movaps %xmm10,-0x68(%rax)
1451 movaps %xmm11,-0x58(%rax)
1452 movaps %xmm12,-0x48(%rax)
1453 movaps %xmm13,-0x38(%rax)
1454 movaps %xmm14,-0x28(%rax)
1455 movaps %xmm15,-0x18(%rax)
1460 movups ($ivp),@tweak[5] # load clear-text tweak
1461 mov 240(%r8),$rounds # key2->rounds
1462 mov 240($key),$rnds_ # key1->rounds
1464 # generate the tweak
1465 &aesni_generate1("enc",$key2,$rounds,@tweak[5]);
1467 mov $key,$key_ # backup $key
1468 mov $rnds_,$rounds # backup $rounds
1469 mov $len,$len_ # backup $len
1472 movdqa .Lxts_magic(%rip),$twmask
1474 pcmpgtd @tweak[5],$twtmp # broadcast upper bits
1476 for ($i=0;$i<4;$i++) {
1478 pshufd \$0x13,$twtmp,$twres
1480 movdqa @tweak[5],@tweak[$i]
1481 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1482 pand $twmask,$twres # isolate carry and residue
1483 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1484 pxor $twres,@tweak[5]
1494 jmp .Lxts_enc_grandloop
1497 .Lxts_enc_grandloop:
1498 pshufd \$0x13,$twtmp,$twres
1499 movdqa @tweak[5],@tweak[4]
1500 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1501 movdqu `16*0`($inp),$inout0 # load input
1502 pand $twmask,$twres # isolate carry and residue
1503 movdqu `16*1`($inp),$inout1
1504 pxor $twres,@tweak[5]
1506 movdqu `16*2`($inp),$inout2
1507 pxor @tweak[0],$inout0 # input^=tweak
1508 movdqu `16*3`($inp),$inout3
1509 pxor @tweak[1],$inout1
1510 movdqu `16*4`($inp),$inout4
1511 pxor @tweak[2],$inout2
1512 movdqu `16*5`($inp),$inout5
1513 lea `16*6`($inp),$inp
1514 pxor @tweak[3],$inout3
1515 $movkey ($key_),$rndkey0
1516 pxor @tweak[4],$inout4
1517 pxor @tweak[5],$inout5
1519 # inline _aesni_encrypt6 and interleave first and last rounds
1521 $movkey 16($key_),$rndkey1
1522 pxor $rndkey0,$inout0
1523 pxor $rndkey0,$inout1
1524 movdqa @tweak[0],`16*0`(%rsp) # put aside tweaks
1525 aesenc $rndkey1,$inout0
1527 pxor $rndkey0,$inout2
1528 movdqa @tweak[1],`16*1`(%rsp)
1529 aesenc $rndkey1,$inout1
1530 pxor $rndkey0,$inout3
1531 movdqa @tweak[2],`16*2`(%rsp)
1532 aesenc $rndkey1,$inout2
1533 pxor $rndkey0,$inout4
1534 movdqa @tweak[3],`16*3`(%rsp)
1535 aesenc $rndkey1,$inout3
1536 pxor $rndkey0,$inout5
1537 $movkey ($key),$rndkey0
1539 movdqa @tweak[4],`16*4`(%rsp)
1540 aesenc $rndkey1,$inout4
1541 movdqa @tweak[5],`16*5`(%rsp)
1542 aesenc $rndkey1,$inout5
1544 pcmpgtd @tweak[5],$twtmp
1545 jmp .Lxts_enc_loop6_enter
1549 aesenc $rndkey1,$inout0
1550 aesenc $rndkey1,$inout1
1552 aesenc $rndkey1,$inout2
1553 aesenc $rndkey1,$inout3
1554 aesenc $rndkey1,$inout4
1555 aesenc $rndkey1,$inout5
1556 .Lxts_enc_loop6_enter:
1557 $movkey 16($key),$rndkey1
1558 aesenc $rndkey0,$inout0
1559 aesenc $rndkey0,$inout1
1561 aesenc $rndkey0,$inout2
1562 aesenc $rndkey0,$inout3
1563 aesenc $rndkey0,$inout4
1564 aesenc $rndkey0,$inout5
1565 $movkey ($key),$rndkey0
1568 pshufd \$0x13,$twtmp,$twres
1570 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1571 aesenc $rndkey1,$inout0
1572 pand $twmask,$twres # isolate carry and residue
1573 aesenc $rndkey1,$inout1
1574 pcmpgtd @tweak[5],$twtmp # broadcast upper bits
1575 aesenc $rndkey1,$inout2
1576 pxor $twres,@tweak[5]
1577 aesenc $rndkey1,$inout3
1578 aesenc $rndkey1,$inout4
1579 aesenc $rndkey1,$inout5
1580 $movkey 16($key),$rndkey1
1582 pshufd \$0x13,$twtmp,$twres
1584 movdqa @tweak[5],@tweak[0]
1585 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1586 aesenc $rndkey0,$inout0
1587 pand $twmask,$twres # isolate carry and residue
1588 aesenc $rndkey0,$inout1
1589 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1590 aesenc $rndkey0,$inout2
1591 pxor $twres,@tweak[5]
1592 aesenc $rndkey0,$inout3
1593 aesenc $rndkey0,$inout4
1594 aesenc $rndkey0,$inout5
1595 $movkey 32($key),$rndkey0
1597 pshufd \$0x13,$twtmp,$twres
1599 movdqa @tweak[5],@tweak[1]
1600 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1601 aesenc $rndkey1,$inout0
1602 pand $twmask,$twres # isolate carry and residue
1603 aesenc $rndkey1,$inout1
1604 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1605 aesenc $rndkey1,$inout2
1606 pxor $twres,@tweak[5]
1607 aesenc $rndkey1,$inout3
1608 aesenc $rndkey1,$inout4
1609 aesenc $rndkey1,$inout5
1611 pshufd \$0x13,$twtmp,$twres
1613 movdqa @tweak[5],@tweak[2]
1614 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1615 aesenclast $rndkey0,$inout0
1616 pand $twmask,$twres # isolate carry and residue
1617 aesenclast $rndkey0,$inout1
1618 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1619 aesenclast $rndkey0,$inout2
1620 pxor $twres,@tweak[5]
1621 aesenclast $rndkey0,$inout3
1622 aesenclast $rndkey0,$inout4
1623 aesenclast $rndkey0,$inout5
1625 pshufd \$0x13,$twtmp,$twres
1627 movdqa @tweak[5],@tweak[3]
1628 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1629 xorps `16*0`(%rsp),$inout0 # output^=tweak
1630 pand $twmask,$twres # isolate carry and residue
1631 xorps `16*1`(%rsp),$inout1
1632 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1633 pxor $twres,@tweak[5]
1635 xorps `16*2`(%rsp),$inout2
1636 movups $inout0,`16*0`($out) # write output
1637 xorps `16*3`(%rsp),$inout3
1638 movups $inout1,`16*1`($out)
1639 xorps `16*4`(%rsp),$inout4
1640 movups $inout2,`16*2`($out)
1641 xorps `16*5`(%rsp),$inout5
1642 movups $inout3,`16*3`($out)
1643 mov $rnds_,$rounds # restore $rounds
1644 movups $inout4,`16*4`($out)
1645 movups $inout5,`16*5`($out)
1646 lea `16*6`($out),$out
1648 jnc .Lxts_enc_grandloop
1650 lea 3($rounds,$rounds),$rounds # restore original value
1651 mov $key_,$key # restore $key
1652 mov $rounds,$rnds_ # backup $rounds
1666 pshufd \$0x13,$twtmp,$twres
1667 movdqa @tweak[5],@tweak[4]
1668 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1669 movdqu ($inp),$inout0
1670 pand $twmask,$twres # isolate carry and residue
1671 movdqu 16*1($inp),$inout1
1672 pxor $twres,@tweak[5]
1674 movdqu 16*2($inp),$inout2
1675 pxor @tweak[0],$inout0
1676 movdqu 16*3($inp),$inout3
1677 pxor @tweak[1],$inout1
1678 movdqu 16*4($inp),$inout4
1680 pxor @tweak[2],$inout2
1681 pxor @tweak[3],$inout3
1682 pxor @tweak[4],$inout4
1684 call _aesni_encrypt6
1686 xorps @tweak[0],$inout0
1687 movdqa @tweak[5],@tweak[0]
1688 xorps @tweak[1],$inout1
1689 xorps @tweak[2],$inout2
1690 movdqu $inout0,($out)
1691 xorps @tweak[3],$inout3
1692 movdqu $inout1,16*1($out)
1693 xorps @tweak[4],$inout4
1694 movdqu $inout2,16*2($out)
1695 movdqu $inout3,16*3($out)
1696 movdqu $inout4,16*4($out)
1702 movups ($inp),$inout0
1704 xorps @tweak[0],$inout0
1706 &aesni_generate1("enc",$key,$rounds);
1708 xorps @tweak[0],$inout0
1709 movdqa @tweak[1],@tweak[0]
1710 movups $inout0,($out)
1716 movups ($inp),$inout0
1717 movups 16($inp),$inout1
1719 xorps @tweak[0],$inout0
1720 xorps @tweak[1],$inout1
1722 call _aesni_encrypt3
1724 xorps @tweak[0],$inout0
1725 movdqa @tweak[2],@tweak[0]
1726 xorps @tweak[1],$inout1
1727 movups $inout0,($out)
1728 movups $inout1,16*1($out)
1734 movups ($inp),$inout0
1735 movups 16*1($inp),$inout1
1736 movups 16*2($inp),$inout2
1738 xorps @tweak[0],$inout0
1739 xorps @tweak[1],$inout1
1740 xorps @tweak[2],$inout2
1742 call _aesni_encrypt3
1744 xorps @tweak[0],$inout0
1745 movdqa @tweak[3],@tweak[0]
1746 xorps @tweak[1],$inout1
1747 xorps @tweak[2],$inout2
1748 movups $inout0,($out)
1749 movups $inout1,16*1($out)
1750 movups $inout2,16*2($out)
1756 movups ($inp),$inout0
1757 movups 16*1($inp),$inout1
1758 movups 16*2($inp),$inout2
1759 xorps @tweak[0],$inout0
1760 movups 16*3($inp),$inout3
1762 xorps @tweak[1],$inout1
1763 xorps @tweak[2],$inout2
1764 xorps @tweak[3],$inout3
1766 call _aesni_encrypt4
1768 xorps @tweak[0],$inout0
1769 movdqa @tweak[5],@tweak[0]
1770 xorps @tweak[1],$inout1
1771 xorps @tweak[2],$inout2
1772 movups $inout0,($out)
1773 xorps @tweak[3],$inout3
1774 movups $inout1,16*1($out)
1775 movups $inout2,16*2($out)
1776 movups $inout3,16*3($out)
1787 movzb ($inp),%eax # borrow $rounds ...
1788 movzb -16($out),%ecx # ... and $key
1796 sub $len_,$out # rewind $out
1797 mov $key_,$key # restore $key
1798 mov $rnds_,$rounds # restore $rounds
1800 movups -16($out),$inout0
1801 xorps @tweak[0],$inout0
1803 &aesni_generate1("enc",$key,$rounds);
1805 xorps @tweak[0],$inout0
1806 movups $inout0,-16($out)
1810 $code.=<<___ if ($win64);
1811 movaps -0xa0(%rbp),%xmm6
1812 movaps -0x90(%rbp),%xmm7
1813 movaps -0x80(%rbp),%xmm8
1814 movaps -0x70(%rbp),%xmm9
1815 movaps -0x60(%rbp),%xmm10
1816 movaps -0x50(%rbp),%xmm11
1817 movaps -0x40(%rbp),%xmm12
1818 movaps -0x30(%rbp),%xmm13
1819 movaps -0x20(%rbp),%xmm14
1820 movaps -0x10(%rbp),%xmm15
1827 .size aesni_xts_encrypt,.-aesni_xts_encrypt
1831 .globl aesni_xts_decrypt
1832 .type aesni_xts_decrypt,\@function,6
1837 sub \$$frame_size,%rsp
1838 and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
1840 $code.=<<___ if ($win64);
1841 movaps %xmm6,-0xa8(%rax)
1842 movaps %xmm7,-0x98(%rax)
1843 movaps %xmm8,-0x88(%rax)
1844 movaps %xmm9,-0x78(%rax)
1845 movaps %xmm10,-0x68(%rax)
1846 movaps %xmm11,-0x58(%rax)
1847 movaps %xmm12,-0x48(%rax)
1848 movaps %xmm13,-0x38(%rax)
1849 movaps %xmm14,-0x28(%rax)
1850 movaps %xmm15,-0x18(%rax)
1855 movups ($ivp),@tweak[5] # load clear-text tweak
1856 mov 240($key2),$rounds # key2->rounds
1857 mov 240($key),$rnds_ # key1->rounds
1859 # generate the tweak
1860 &aesni_generate1("enc",$key2,$rounds,@tweak[5]);
1862 xor %eax,%eax # if ($len%16) len-=16;
1868 mov $key,$key_ # backup $key
1869 mov $rnds_,$rounds # backup $rounds
1870 mov $len,$len_ # backup $len
1873 movdqa .Lxts_magic(%rip),$twmask
1875 pcmpgtd @tweak[5],$twtmp # broadcast upper bits
1877 for ($i=0;$i<4;$i++) {
1879 pshufd \$0x13,$twtmp,$twres
1881 movdqa @tweak[5],@tweak[$i]
1882 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1883 pand $twmask,$twres # isolate carry and residue
1884 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1885 pxor $twres,@tweak[5]
1895 jmp .Lxts_dec_grandloop
1898 .Lxts_dec_grandloop:
1899 pshufd \$0x13,$twtmp,$twres
1900 movdqa @tweak[5],@tweak[4]
1901 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1902 movdqu `16*0`($inp),$inout0 # load input
1903 pand $twmask,$twres # isolate carry and residue
1904 movdqu `16*1`($inp),$inout1
1905 pxor $twres,@tweak[5]
1907 movdqu `16*2`($inp),$inout2
1908 pxor @tweak[0],$inout0 # input^=tweak
1909 movdqu `16*3`($inp),$inout3
1910 pxor @tweak[1],$inout1
1911 movdqu `16*4`($inp),$inout4
1912 pxor @tweak[2],$inout2
1913 movdqu `16*5`($inp),$inout5
1914 lea `16*6`($inp),$inp
1915 pxor @tweak[3],$inout3
1916 $movkey ($key_),$rndkey0
1917 pxor @tweak[4],$inout4
1918 pxor @tweak[5],$inout5
1920 # inline _aesni_decrypt6 and interleave first and last rounds
1922 $movkey 16($key_),$rndkey1
1923 pxor $rndkey0,$inout0
1924 pxor $rndkey0,$inout1
1925 movdqa @tweak[0],`16*0`(%rsp) # put aside tweaks
1926 aesdec $rndkey1,$inout0
1928 pxor $rndkey0,$inout2
1929 movdqa @tweak[1],`16*1`(%rsp)
1930 aesdec $rndkey1,$inout1
1931 pxor $rndkey0,$inout3
1932 movdqa @tweak[2],`16*2`(%rsp)
1933 aesdec $rndkey1,$inout2
1934 pxor $rndkey0,$inout4
1935 movdqa @tweak[3],`16*3`(%rsp)
1936 aesdec $rndkey1,$inout3
1937 pxor $rndkey0,$inout5
1938 $movkey ($key),$rndkey0
1940 movdqa @tweak[4],`16*4`(%rsp)
1941 aesdec $rndkey1,$inout4
1942 movdqa @tweak[5],`16*5`(%rsp)
1943 aesdec $rndkey1,$inout5
1945 pcmpgtd @tweak[5],$twtmp
1946 jmp .Lxts_dec_loop6_enter
1950 aesdec $rndkey1,$inout0
1951 aesdec $rndkey1,$inout1
1953 aesdec $rndkey1,$inout2
1954 aesdec $rndkey1,$inout3
1955 aesdec $rndkey1,$inout4
1956 aesdec $rndkey1,$inout5
1957 .Lxts_dec_loop6_enter:
1958 $movkey 16($key),$rndkey1
1959 aesdec $rndkey0,$inout0
1960 aesdec $rndkey0,$inout1
1962 aesdec $rndkey0,$inout2
1963 aesdec $rndkey0,$inout3
1964 aesdec $rndkey0,$inout4
1965 aesdec $rndkey0,$inout5
1966 $movkey ($key),$rndkey0
1969 pshufd \$0x13,$twtmp,$twres
1971 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1972 aesdec $rndkey1,$inout0
1973 pand $twmask,$twres # isolate carry and residue
1974 aesdec $rndkey1,$inout1
1975 pcmpgtd @tweak[5],$twtmp # broadcast upper bits
1976 aesdec $rndkey1,$inout2
1977 pxor $twres,@tweak[5]
1978 aesdec $rndkey1,$inout3
1979 aesdec $rndkey1,$inout4
1980 aesdec $rndkey1,$inout5
1981 $movkey 16($key),$rndkey1
1983 pshufd \$0x13,$twtmp,$twres
1985 movdqa @tweak[5],@tweak[0]
1986 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1987 aesdec $rndkey0,$inout0
1988 pand $twmask,$twres # isolate carry and residue
1989 aesdec $rndkey0,$inout1
1990 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1991 aesdec $rndkey0,$inout2
1992 pxor $twres,@tweak[5]
1993 aesdec $rndkey0,$inout3
1994 aesdec $rndkey0,$inout4
1995 aesdec $rndkey0,$inout5
1996 $movkey 32($key),$rndkey0
1998 pshufd \$0x13,$twtmp,$twres
2000 movdqa @tweak[5],@tweak[1]
2001 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
2002 aesdec $rndkey1,$inout0
2003 pand $twmask,$twres # isolate carry and residue
2004 aesdec $rndkey1,$inout1
2005 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
2006 aesdec $rndkey1,$inout2
2007 pxor $twres,@tweak[5]
2008 aesdec $rndkey1,$inout3
2009 aesdec $rndkey1,$inout4
2010 aesdec $rndkey1,$inout5
2012 pshufd \$0x13,$twtmp,$twres
2014 movdqa @tweak[5],@tweak[2]
2015 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
2016 aesdeclast $rndkey0,$inout0
2017 pand $twmask,$twres # isolate carry and residue
2018 aesdeclast $rndkey0,$inout1
2019 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
2020 aesdeclast $rndkey0,$inout2
2021 pxor $twres,@tweak[5]
2022 aesdeclast $rndkey0,$inout3
2023 aesdeclast $rndkey0,$inout4
2024 aesdeclast $rndkey0,$inout5
2026 pshufd \$0x13,$twtmp,$twres
2028 movdqa @tweak[5],@tweak[3]
2029 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
2030 xorps `16*0`(%rsp),$inout0 # output^=tweak
2031 pand $twmask,$twres # isolate carry and residue
2032 xorps `16*1`(%rsp),$inout1
2033 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
2034 pxor $twres,@tweak[5]
2036 xorps `16*2`(%rsp),$inout2
2037 movups $inout0,`16*0`($out) # write output
2038 xorps `16*3`(%rsp),$inout3
2039 movups $inout1,`16*1`($out)
2040 xorps `16*4`(%rsp),$inout4
2041 movups $inout2,`16*2`($out)
2042 xorps `16*5`(%rsp),$inout5
2043 movups $inout3,`16*3`($out)
2044 mov $rnds_,$rounds # restore $rounds
2045 movups $inout4,`16*4`($out)
2046 movups $inout5,`16*5`($out)
2047 lea `16*6`($out),$out
2049 jnc .Lxts_dec_grandloop
2051 lea 3($rounds,$rounds),$rounds # restore original value
2052 mov $key_,$key # restore $key
2053 mov $rounds,$rnds_ # backup $rounds
2067 pshufd \$0x13,$twtmp,$twres
2068 movdqa @tweak[5],@tweak[4]
2069 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
2070 movdqu ($inp),$inout0
2071 pand $twmask,$twres # isolate carry and residue
2072 movdqu 16*1($inp),$inout1
2073 pxor $twres,@tweak[5]
2075 movdqu 16*2($inp),$inout2
2076 pxor @tweak[0],$inout0
2077 movdqu 16*3($inp),$inout3
2078 pxor @tweak[1],$inout1
2079 movdqu 16*4($inp),$inout4
2081 pxor @tweak[2],$inout2
2082 pxor @tweak[3],$inout3
2083 pxor @tweak[4],$inout4
2085 call _aesni_decrypt6
2087 xorps @tweak[0],$inout0
2088 xorps @tweak[1],$inout1
2089 xorps @tweak[2],$inout2
2090 movdqu $inout0,($out)
2091 xorps @tweak[3],$inout3
2092 movdqu $inout1,16*1($out)
2093 xorps @tweak[4],$inout4
2094 movdqu $inout2,16*2($out)
2096 movdqu $inout3,16*3($out)
2097 pcmpgtd @tweak[5],$twtmp
2098 movdqu $inout4,16*4($out)
2100 pshufd \$0x13,$twtmp,@tweak[1] # $twres
2104 movdqa @tweak[5],@tweak[0]
2105 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
2106 pand $twmask,@tweak[1] # isolate carry and residue
2107 pxor @tweak[5],@tweak[1]
2112 movups ($inp),$inout0
2114 xorps @tweak[0],$inout0
2116 &aesni_generate1("dec",$key,$rounds);
2118 xorps @tweak[0],$inout0
2119 movdqa @tweak[1],@tweak[0]
2120 movups $inout0,($out)
2121 movdqa @tweak[2],@tweak[1]
2127 movups ($inp),$inout0
2128 movups 16($inp),$inout1
2130 xorps @tweak[0],$inout0
2131 xorps @tweak[1],$inout1
2133 call _aesni_decrypt3
2135 xorps @tweak[0],$inout0
2136 movdqa @tweak[2],@tweak[0]
2137 xorps @tweak[1],$inout1
2138 movdqa @tweak[3],@tweak[1]
2139 movups $inout0,($out)
2140 movups $inout1,16*1($out)
2146 movups ($inp),$inout0
2147 movups 16*1($inp),$inout1
2148 movups 16*2($inp),$inout2
2150 xorps @tweak[0],$inout0
2151 xorps @tweak[1],$inout1
2152 xorps @tweak[2],$inout2
2154 call _aesni_decrypt3
2156 xorps @tweak[0],$inout0
2157 movdqa @tweak[3],@tweak[0]
2158 xorps @tweak[1],$inout1
2159 movdqa @tweak[5],@tweak[1]
2160 xorps @tweak[2],$inout2
2161 movups $inout0,($out)
2162 movups $inout1,16*1($out)
2163 movups $inout2,16*2($out)
2169 pshufd \$0x13,$twtmp,$twres
2170 movdqa @tweak[5],@tweak[4]
2171 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
2172 movups ($inp),$inout0
2173 pand $twmask,$twres # isolate carry and residue
2174 movups 16*1($inp),$inout1
2175 pxor $twres,@tweak[5]
2177 movups 16*2($inp),$inout2
2178 xorps @tweak[0],$inout0
2179 movups 16*3($inp),$inout3
2181 xorps @tweak[1],$inout1
2182 xorps @tweak[2],$inout2
2183 xorps @tweak[3],$inout3
2185 call _aesni_decrypt4
2187 xorps @tweak[0],$inout0
2188 movdqa @tweak[4],@tweak[0]
2189 xorps @tweak[1],$inout1
2190 movdqa @tweak[5],@tweak[1]
2191 xorps @tweak[2],$inout2
2192 movups $inout0,($out)
2193 xorps @tweak[3],$inout3
2194 movups $inout1,16*1($out)
2195 movups $inout2,16*2($out)
2196 movups $inout3,16*3($out)
2206 mov $key_,$key # restore $key
2207 mov $rnds_,$rounds # restore $rounds
2209 movups ($inp),$inout0
2210 xorps @tweak[1],$inout0
2212 &aesni_generate1("dec",$key,$rounds);
2214 xorps @tweak[1],$inout0
2215 movups $inout0,($out)
2218 movzb 16($inp),%eax # borrow $rounds ...
2219 movzb ($out),%ecx # ... and $key
2227 sub $len_,$out # rewind $out
2228 mov $key_,$key # restore $key
2229 mov $rnds_,$rounds # restore $rounds
2231 movups ($out),$inout0
2232 xorps @tweak[0],$inout0
2234 &aesni_generate1("dec",$key,$rounds);
2236 xorps @tweak[0],$inout0
2237 movups $inout0,($out)
2241 $code.=<<___ if ($win64);
2242 movaps -0xa0(%rbp),%xmm6
2243 movaps -0x90(%rbp),%xmm7
2244 movaps -0x80(%rbp),%xmm8
2245 movaps -0x70(%rbp),%xmm9
2246 movaps -0x60(%rbp),%xmm10
2247 movaps -0x50(%rbp),%xmm11
2248 movaps -0x40(%rbp),%xmm12
2249 movaps -0x30(%rbp),%xmm13
2250 movaps -0x20(%rbp),%xmm14
2251 movaps -0x10(%rbp),%xmm15
2258 .size aesni_xts_decrypt,.-aesni_xts_decrypt
2262 ########################################################################
2263 # void $PREFIX_cbc_encrypt (const void *inp, void *out,
2264 # size_t length, const AES_KEY *key,
2265 # unsigned char *ivp,const int enc);
2267 my $frame_size = 0x10 + ($win64?0xa0:0); # used in decrypt
2268 my ($iv,$in0,$in1,$in2,$in3,$in4)=map("%xmm$_",(10..15));
2272 .globl ${PREFIX}_cbc_encrypt
2273 .type ${PREFIX}_cbc_encrypt,\@function,6
2275 ${PREFIX}_cbc_encrypt:
2276 test $len,$len # check length
2279 mov 240($key),$rnds_ # key->rounds
2280 mov $key,$key_ # backup $key
2281 test %r9d,%r9d # 6th argument
2283 #--------------------------- CBC ENCRYPT ------------------------------#
2284 movups ($ivp),$inout0 # load iv as initial state
2292 movups ($inp),$inout1 # load input
2294 #xorps $inout1,$inout0
2296 &aesni_generate1("enc",$key,$rounds,$inout0,$inout1);
2298 mov $rnds_,$rounds # restore $rounds
2299 mov $key_,$key # restore $key
2300 movups $inout0,0($out) # store output
2306 movups $inout0,($ivp)
2310 mov $len,%rcx # zaps $key
2311 xchg $inp,$out # $inp is %rsi and $out is %rdi now
2312 .long 0x9066A4F3 # rep movsb
2313 mov \$16,%ecx # zero tail
2316 .long 0x9066AAF3 # rep stosb
2317 lea -16(%rdi),%rdi # rewind $out by 1 block
2318 mov $rnds_,$rounds # restore $rounds
2319 mov %rdi,%rsi # $inp and $out are the same
2320 mov $key_,$key # restore $key
2321 xor $len,$len # len=16
2322 jmp .Lcbc_enc_loop # one more spin
2323 \f#--------------------------- CBC DECRYPT ------------------------------#
2328 sub \$$frame_size,%rsp
2329 and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
2331 $code.=<<___ if ($win64);
2332 movaps %xmm6,0x10(%rsp)
2333 movaps %xmm7,0x20(%rsp)
2334 movaps %xmm8,0x30(%rsp)
2335 movaps %xmm9,0x40(%rsp)
2336 movaps %xmm10,0x50(%rsp)
2337 movaps %xmm11,0x60(%rsp)
2338 movaps %xmm12,0x70(%rsp)
2339 movaps %xmm13,0x80(%rsp)
2340 movaps %xmm14,0x90(%rsp)
2341 movaps %xmm15,0xa0(%rsp)
2351 $movkey ($key),$rndkey0
2352 movdqu 0x00($inp),$inout0 # load input
2353 movdqu 0x10($inp),$inout1
2355 movdqu 0x20($inp),$inout2
2357 movdqu 0x30($inp),$inout3
2359 movdqu 0x40($inp),$inout4
2361 movdqu 0x50($inp),$inout5
2364 jbe .Lcbc_dec_six_or_seven
2367 lea 0x70($key),$key # size optimization
2368 jmp .Lcbc_dec_loop8_enter
2371 movups $inout7,($out)
2373 .Lcbc_dec_loop8_enter:
2374 movdqu 0x60($inp),$inout6
2375 pxor $rndkey0,$inout0
2376 movdqu 0x70($inp),$inout7
2377 pxor $rndkey0,$inout1
2378 $movkey 0x10-0x70($key),$rndkey1
2379 pxor $rndkey0,$inout2
2381 cmp \$0x70,$len # is there at least 0x60 bytes ahead?
2382 pxor $rndkey0,$inout3
2383 pxor $rndkey0,$inout4
2384 pxor $rndkey0,$inout5
2385 pxor $rndkey0,$inout6
2387 aesdec $rndkey1,$inout0
2388 pxor $rndkey0,$inout7
2389 $movkey 0x20-0x70($key),$rndkey0
2390 aesdec $rndkey1,$inout1
2391 aesdec $rndkey1,$inout2
2392 aesdec $rndkey1,$inout3
2393 aesdec $rndkey1,$inout4
2394 aesdec $rndkey1,$inout5
2396 aesdec $rndkey1,$inout6
2398 aesdec $rndkey1,$inout7
2400 $movkey 0x30-0x70($key),$rndkey1
2402 for($i=1;$i<12;$i++) {
2403 my $rndkeyx = ($i&1)?$rndkey0:$rndkey1;
2405 aesdec $rndkeyx,$inout0
2406 aesdec $rndkeyx,$inout1
2407 aesdec $rndkeyx,$inout2
2408 aesdec $rndkeyx,$inout3
2409 aesdec $rndkeyx,$inout4
2410 aesdec $rndkeyx,$inout5
2411 aesdec $rndkeyx,$inout6
2412 aesdec $rndkeyx,$inout7
2413 $movkey `0x30+0x10*$i`-0x70($key),$rndkeyx
2415 $code.=<<___ if ($i==7);
2419 $code.=<<___ if ($i==9);
2425 aesdec $rndkey1,$inout0
2427 aesdec $rndkey1,$inout1
2429 aesdec $rndkey1,$inout2
2431 aesdec $rndkey1,$inout3
2433 aesdec $rndkey1,$inout4
2435 aesdec $rndkey1,$inout5
2437 aesdec $rndkey1,$inout6
2438 aesdec $rndkey1,$inout7
2439 movdqu 0x50($inp),$rndkey1
2441 aesdeclast $iv,$inout0
2442 movdqu 0x60($inp),$iv # borrow $iv
2443 pxor $rndkey0,$rndkey1
2444 aesdeclast $in0,$inout1
2446 movdqu 0x70($inp),$rndkey0 # next IV
2448 aesdeclast $in1,$inout2
2449 movdqu 0x00($inp_),$in0
2450 aesdeclast $in2,$inout3
2451 movdqu 0x10($inp_),$in1
2452 aesdeclast $in3,$inout4
2453 movdqu 0x20($inp_),$in2
2454 aesdeclast $in4,$inout5
2455 movdqu 0x30($inp_),$in3
2456 aesdeclast $rndkey1,$inout6
2457 movdqu 0x40($inp_),$in4
2458 aesdeclast $iv,$inout7
2459 movdqa $rndkey0,$iv # return $iv
2460 movdqu 0x50($inp_),$rndkey1
2461 $movkey -0x70($key),$rndkey0
2463 movups $inout0,($out) # store output
2465 movups $inout1,0x10($out)
2467 movups $inout2,0x20($out)
2469 movups $inout3,0x30($out)
2471 movups $inout4,0x40($out)
2473 movups $inout5,0x50($out)
2474 movdqa $rndkey1,$inout5
2475 movups $inout6,0x60($out)
2481 movaps $inout7,$inout0
2482 lea -0x70($key),$key
2484 jle .Lcbc_dec_tail_collected
2485 movups $inout7,($out)
2491 .Lcbc_dec_six_or_seven:
2495 movaps $inout5,$inout6
2496 call _aesni_decrypt6
2497 pxor $iv,$inout0 # ^= IV
2500 movdqu $inout0,($out)
2502 movdqu $inout1,0x10($out)
2504 movdqu $inout2,0x20($out)
2506 movdqu $inout3,0x30($out)
2508 movdqu $inout4,0x40($out)
2510 movdqa $inout5,$inout0
2511 jmp .Lcbc_dec_tail_collected
2515 movups 0x60($inp),$inout6
2516 xorps $inout7,$inout7
2517 call _aesni_decrypt8
2518 movups 0x50($inp),$inout7
2519 pxor $iv,$inout0 # ^= IV
2520 movups 0x60($inp),$iv
2522 movdqu $inout0,($out)
2524 movdqu $inout1,0x10($out)
2526 movdqu $inout2,0x20($out)
2528 movdqu $inout3,0x30($out)
2530 movdqu $inout4,0x40($out)
2531 pxor $inout7,$inout6
2532 movdqu $inout5,0x50($out)
2534 movdqa $inout6,$inout0
2535 jmp .Lcbc_dec_tail_collected
2538 movups ($inp),$inout0
2542 movups 0x10($inp),$inout1
2547 movups 0x20($inp),$inout2
2552 movups 0x30($inp),$inout3
2557 movups 0x40($inp),$inout4
2560 xorps $inout5,$inout5
2561 call _aesni_decrypt6
2565 movdqu $inout0,($out)
2567 movdqu $inout1,0x10($out)
2569 movdqu $inout2,0x20($out)
2571 movdqu $inout3,0x30($out)
2573 movdqa $inout4,$inout0
2575 jmp .Lcbc_dec_tail_collected
2581 &aesni_generate1("dec",$key,$rounds);
2585 jmp .Lcbc_dec_tail_collected
2589 xorps $inout2,$inout2
2590 call _aesni_decrypt3
2594 movdqu $inout0,($out)
2595 movdqa $inout1,$inout0
2597 jmp .Lcbc_dec_tail_collected
2601 call _aesni_decrypt3
2605 movdqu $inout0,($out)
2607 movdqu $inout1,0x10($out)
2608 movdqa $inout2,$inout0
2610 jmp .Lcbc_dec_tail_collected
2614 call _aesni_decrypt4
2618 movdqu $inout0,($out)
2620 movdqu $inout1,0x10($out)
2622 movdqu $inout2,0x20($out)
2623 movdqa $inout3,$inout0
2625 jmp .Lcbc_dec_tail_collected
2628 .Lcbc_dec_tail_collected:
2631 jnz .Lcbc_dec_tail_partial
2632 movups $inout0,($out)
2635 .Lcbc_dec_tail_partial:
2636 movaps $inout0,(%rsp)
2641 .long 0x9066A4F3 # rep movsb
2645 $code.=<<___ if ($win64);
2646 movaps 0x10(%rsp),%xmm6
2647 movaps 0x20(%rsp),%xmm7
2648 movaps 0x30(%rsp),%xmm8
2649 movaps 0x40(%rsp),%xmm9
2650 movaps 0x50(%rsp),%xmm10
2651 movaps 0x60(%rsp),%xmm11
2652 movaps 0x70(%rsp),%xmm12
2653 movaps 0x80(%rsp),%xmm13
2654 movaps 0x90(%rsp),%xmm14
2655 movaps 0xa0(%rsp),%xmm15
2662 .size ${PREFIX}_cbc_encrypt,.-${PREFIX}_cbc_encrypt
2665 # int $PREFIX_set_[en|de]crypt_key (const unsigned char *userKey,
2666 # int bits, AES_KEY *key)
2667 { my ($inp,$bits,$key) = @_4args;
2671 .globl ${PREFIX}_set_decrypt_key
2672 .type ${PREFIX}_set_decrypt_key,\@abi-omnipotent
2674 ${PREFIX}_set_decrypt_key:
2675 .byte 0x48,0x83,0xEC,0x08 # sub rsp,8
2676 call __aesni_set_encrypt_key
2677 shl \$4,$bits # rounds-1 after _aesni_set_encrypt_key
2680 lea 16($key,$bits),$inp # points at the end of key schedule
2682 $movkey ($key),%xmm0 # just swap
2683 $movkey ($inp),%xmm1
2684 $movkey %xmm0,($inp)
2685 $movkey %xmm1,($key)
2690 $movkey ($key),%xmm0 # swap and inverse
2691 $movkey ($inp),%xmm1
2696 $movkey %xmm0,16($inp)
2697 $movkey %xmm1,-16($key)
2699 ja .Ldec_key_inverse
2701 $movkey ($key),%xmm0 # inverse middle
2703 $movkey %xmm0,($inp)
2707 .LSEH_end_set_decrypt_key:
2708 .size ${PREFIX}_set_decrypt_key,.-${PREFIX}_set_decrypt_key
2711 # This is based on submission by
2713 # Huang Ying <ying.huang@intel.com>
2714 # Vinodh Gopal <vinodh.gopal@intel.com>
2717 # Agressively optimized in respect to aeskeygenassist's critical path
2718 # and is contained in %xmm0-5 to meet Win64 ABI requirement.
2721 .globl ${PREFIX}_set_encrypt_key
2722 .type ${PREFIX}_set_encrypt_key,\@abi-omnipotent
2724 ${PREFIX}_set_encrypt_key:
2725 __aesni_set_encrypt_key:
2726 .byte 0x48,0x83,0xEC,0x08 # sub rsp,8
2733 movups ($inp),%xmm0 # pull first 128 bits of *userKey
2734 xorps %xmm4,%xmm4 # low dword of xmm4 is assumed 0
2744 mov \$9,$bits # 10 rounds for 128-bit key
2745 $movkey %xmm0,($key) # round 0
2746 aeskeygenassist \$0x1,%xmm0,%xmm1 # round 1
2747 call .Lkey_expansion_128_cold
2748 aeskeygenassist \$0x2,%xmm0,%xmm1 # round 2
2749 call .Lkey_expansion_128
2750 aeskeygenassist \$0x4,%xmm0,%xmm1 # round 3
2751 call .Lkey_expansion_128
2752 aeskeygenassist \$0x8,%xmm0,%xmm1 # round 4
2753 call .Lkey_expansion_128
2754 aeskeygenassist \$0x10,%xmm0,%xmm1 # round 5
2755 call .Lkey_expansion_128
2756 aeskeygenassist \$0x20,%xmm0,%xmm1 # round 6
2757 call .Lkey_expansion_128
2758 aeskeygenassist \$0x40,%xmm0,%xmm1 # round 7
2759 call .Lkey_expansion_128
2760 aeskeygenassist \$0x80,%xmm0,%xmm1 # round 8
2761 call .Lkey_expansion_128
2762 aeskeygenassist \$0x1b,%xmm0,%xmm1 # round 9
2763 call .Lkey_expansion_128
2764 aeskeygenassist \$0x36,%xmm0,%xmm1 # round 10
2765 call .Lkey_expansion_128
2766 $movkey %xmm0,(%rax)
2767 mov $bits,80(%rax) # 240(%rdx)
2773 movq 16($inp),%xmm2 # remaining 1/3 of *userKey
2774 mov \$11,$bits # 12 rounds for 192
2775 $movkey %xmm0,($key) # round 0
2776 aeskeygenassist \$0x1,%xmm2,%xmm1 # round 1,2
2777 call .Lkey_expansion_192a_cold
2778 aeskeygenassist \$0x2,%xmm2,%xmm1 # round 2,3
2779 call .Lkey_expansion_192b
2780 aeskeygenassist \$0x4,%xmm2,%xmm1 # round 4,5
2781 call .Lkey_expansion_192a
2782 aeskeygenassist \$0x8,%xmm2,%xmm1 # round 5,6
2783 call .Lkey_expansion_192b
2784 aeskeygenassist \$0x10,%xmm2,%xmm1 # round 7,8
2785 call .Lkey_expansion_192a
2786 aeskeygenassist \$0x20,%xmm2,%xmm1 # round 8,9
2787 call .Lkey_expansion_192b
2788 aeskeygenassist \$0x40,%xmm2,%xmm1 # round 10,11
2789 call .Lkey_expansion_192a
2790 aeskeygenassist \$0x80,%xmm2,%xmm1 # round 11,12
2791 call .Lkey_expansion_192b
2792 $movkey %xmm0,(%rax)
2793 mov $bits,48(%rax) # 240(%rdx)
2799 movups 16($inp),%xmm2 # remaning half of *userKey
2800 mov \$13,$bits # 14 rounds for 256
2802 $movkey %xmm0,($key) # round 0
2803 $movkey %xmm2,16($key) # round 1
2804 aeskeygenassist \$0x1,%xmm2,%xmm1 # round 2
2805 call .Lkey_expansion_256a_cold
2806 aeskeygenassist \$0x1,%xmm0,%xmm1 # round 3
2807 call .Lkey_expansion_256b
2808 aeskeygenassist \$0x2,%xmm2,%xmm1 # round 4
2809 call .Lkey_expansion_256a
2810 aeskeygenassist \$0x2,%xmm0,%xmm1 # round 5
2811 call .Lkey_expansion_256b
2812 aeskeygenassist \$0x4,%xmm2,%xmm1 # round 6
2813 call .Lkey_expansion_256a
2814 aeskeygenassist \$0x4,%xmm0,%xmm1 # round 7
2815 call .Lkey_expansion_256b
2816 aeskeygenassist \$0x8,%xmm2,%xmm1 # round 8
2817 call .Lkey_expansion_256a
2818 aeskeygenassist \$0x8,%xmm0,%xmm1 # round 9
2819 call .Lkey_expansion_256b
2820 aeskeygenassist \$0x10,%xmm2,%xmm1 # round 10
2821 call .Lkey_expansion_256a
2822 aeskeygenassist \$0x10,%xmm0,%xmm1 # round 11
2823 call .Lkey_expansion_256b
2824 aeskeygenassist \$0x20,%xmm2,%xmm1 # round 12
2825 call .Lkey_expansion_256a
2826 aeskeygenassist \$0x20,%xmm0,%xmm1 # round 13
2827 call .Lkey_expansion_256b
2828 aeskeygenassist \$0x40,%xmm2,%xmm1 # round 14
2829 call .Lkey_expansion_256a
2830 $movkey %xmm0,(%rax)
2831 mov $bits,16(%rax) # 240(%rdx)
2841 .LSEH_end_set_encrypt_key:
2844 .Lkey_expansion_128:
2845 $movkey %xmm0,(%rax)
2847 .Lkey_expansion_128_cold:
2848 shufps \$0b00010000,%xmm0,%xmm4
2850 shufps \$0b10001100,%xmm0,%xmm4
2852 shufps \$0b11111111,%xmm1,%xmm1 # critical path
2857 .Lkey_expansion_192a:
2858 $movkey %xmm0,(%rax)
2860 .Lkey_expansion_192a_cold:
2862 .Lkey_expansion_192b_warm:
2863 shufps \$0b00010000,%xmm0,%xmm4
2866 shufps \$0b10001100,%xmm0,%xmm4
2869 pshufd \$0b01010101,%xmm1,%xmm1 # critical path
2872 pshufd \$0b11111111,%xmm0,%xmm3
2877 .Lkey_expansion_192b:
2879 shufps \$0b01000100,%xmm0,%xmm5
2880 $movkey %xmm5,(%rax)
2881 shufps \$0b01001110,%xmm2,%xmm3
2882 $movkey %xmm3,16(%rax)
2884 jmp .Lkey_expansion_192b_warm
2887 .Lkey_expansion_256a:
2888 $movkey %xmm2,(%rax)
2890 .Lkey_expansion_256a_cold:
2891 shufps \$0b00010000,%xmm0,%xmm4
2893 shufps \$0b10001100,%xmm0,%xmm4
2895 shufps \$0b11111111,%xmm1,%xmm1 # critical path
2900 .Lkey_expansion_256b:
2901 $movkey %xmm0,(%rax)
2904 shufps \$0b00010000,%xmm2,%xmm4
2906 shufps \$0b10001100,%xmm2,%xmm4
2908 shufps \$0b10101010,%xmm1,%xmm1 # critical path
2911 .size ${PREFIX}_set_encrypt_key,.-${PREFIX}_set_encrypt_key
2912 .size __aesni_set_encrypt_key,.-__aesni_set_encrypt_key
2919 .byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
2927 .byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
2929 .asciz "AES for Intel AES-NI, CRYPTOGAMS by <appro\@openssl.org>"
2933 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
2934 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
2942 .extern __imp_RtlVirtualUnwind
2944 $code.=<<___ if ($PREFIX eq "aesni");
2945 .type ecb_se_handler,\@abi-omnipotent
2959 mov 152($context),%rax # pull context->Rsp
2961 jmp .Lcommon_seh_tail
2962 .size ecb_se_handler,.-ecb_se_handler
2964 .type ccm64_se_handler,\@abi-omnipotent
2978 mov 120($context),%rax # pull context->Rax
2979 mov 248($context),%rbx # pull context->Rip
2981 mov 8($disp),%rsi # disp->ImageBase
2982 mov 56($disp),%r11 # disp->HandlerData
2984 mov 0(%r11),%r10d # HandlerData[0]
2985 lea (%rsi,%r10),%r10 # prologue label
2986 cmp %r10,%rbx # context->Rip<prologue label
2987 jb .Lcommon_seh_tail
2989 mov 152($context),%rax # pull context->Rsp
2991 mov 4(%r11),%r10d # HandlerData[1]
2992 lea (%rsi,%r10),%r10 # epilogue label
2993 cmp %r10,%rbx # context->Rip>=epilogue label
2994 jae .Lcommon_seh_tail
2996 lea 0(%rax),%rsi # %xmm save area
2997 lea 512($context),%rdi # &context.Xmm6
2998 mov \$8,%ecx # 4*sizeof(%xmm0)/sizeof(%rax)
2999 .long 0xa548f3fc # cld; rep movsq
3000 lea 0x58(%rax),%rax # adjust stack pointer
3002 jmp .Lcommon_seh_tail
3003 .size ccm64_se_handler,.-ccm64_se_handler
3005 .type ctr_xts_se_handler,\@abi-omnipotent
3019 mov 120($context),%rax # pull context->Rax
3020 mov 248($context),%rbx # pull context->Rip
3022 mov 8($disp),%rsi # disp->ImageBase
3023 mov 56($disp),%r11 # disp->HandlerData
3025 mov 0(%r11),%r10d # HandlerData[0]
3026 lea (%rsi,%r10),%r10 # prologue lable
3027 cmp %r10,%rbx # context->Rip<prologue label
3028 jb .Lcommon_seh_tail
3030 mov 152($context),%rax # pull context->Rsp
3032 mov 4(%r11),%r10d # HandlerData[1]
3033 lea (%rsi,%r10),%r10 # epilogue label
3034 cmp %r10,%rbx # context->Rip>=epilogue label
3035 jae .Lcommon_seh_tail
3037 mov 160($context),%rax # pull context->Rbp
3038 lea -0xa0(%rax),%rsi # %xmm save area
3039 lea 512($context),%rdi # & context.Xmm6
3040 mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
3041 .long 0xa548f3fc # cld; rep movsq
3043 jmp .Lcommon_rbp_tail
3044 .size ctr_xts_se_handler,.-ctr_xts_se_handler
3047 .type cbc_se_handler,\@abi-omnipotent
3061 mov 152($context),%rax # pull context->Rsp
3062 mov 248($context),%rbx # pull context->Rip
3064 lea .Lcbc_decrypt(%rip),%r10
3065 cmp %r10,%rbx # context->Rip<"prologue" label
3066 jb .Lcommon_seh_tail
3068 lea .Lcbc_decrypt_body(%rip),%r10
3069 cmp %r10,%rbx # context->Rip<cbc_decrypt_body
3070 jb .Lrestore_cbc_rax
3072 lea .Lcbc_ret(%rip),%r10
3073 cmp %r10,%rbx # context->Rip>="epilogue" label
3074 jae .Lcommon_seh_tail
3076 lea 16(%rax),%rsi # %xmm save area
3077 lea 512($context),%rdi # &context.Xmm6
3078 mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
3079 .long 0xa548f3fc # cld; rep movsq
3082 mov 160($context),%rax # pull context->Rbp
3083 mov (%rax),%rbp # restore saved %rbp
3084 lea 8(%rax),%rax # adjust stack pointer
3085 mov %rbp,160($context) # restore context->Rbp
3086 jmp .Lcommon_seh_tail
3089 mov 120($context),%rax
3094 mov %rax,152($context) # restore context->Rsp
3095 mov %rsi,168($context) # restore context->Rsi
3096 mov %rdi,176($context) # restore context->Rdi
3098 mov 40($disp),%rdi # disp->ContextRecord
3099 mov $context,%rsi # context
3100 mov \$154,%ecx # sizeof(CONTEXT)
3101 .long 0xa548f3fc # cld; rep movsq
3104 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
3105 mov 8(%rsi),%rdx # arg2, disp->ImageBase
3106 mov 0(%rsi),%r8 # arg3, disp->ControlPc
3107 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
3108 mov 40(%rsi),%r10 # disp->ContextRecord
3109 lea 56(%rsi),%r11 # &disp->HandlerData
3110 lea 24(%rsi),%r12 # &disp->EstablisherFrame
3111 mov %r10,32(%rsp) # arg5
3112 mov %r11,40(%rsp) # arg6
3113 mov %r12,48(%rsp) # arg7
3114 mov %rcx,56(%rsp) # arg8, (NULL)
3115 call *__imp_RtlVirtualUnwind(%rip)
3117 mov \$1,%eax # ExceptionContinueSearch
3129 .size cbc_se_handler,.-cbc_se_handler
3134 $code.=<<___ if ($PREFIX eq "aesni");
3135 .rva .LSEH_begin_aesni_ecb_encrypt
3136 .rva .LSEH_end_aesni_ecb_encrypt
3139 .rva .LSEH_begin_aesni_ccm64_encrypt_blocks
3140 .rva .LSEH_end_aesni_ccm64_encrypt_blocks
3141 .rva .LSEH_info_ccm64_enc
3143 .rva .LSEH_begin_aesni_ccm64_decrypt_blocks
3144 .rva .LSEH_end_aesni_ccm64_decrypt_blocks
3145 .rva .LSEH_info_ccm64_dec
3147 .rva .LSEH_begin_aesni_ctr32_encrypt_blocks
3148 .rva .LSEH_end_aesni_ctr32_encrypt_blocks
3149 .rva .LSEH_info_ctr32
3151 .rva .LSEH_begin_aesni_xts_encrypt
3152 .rva .LSEH_end_aesni_xts_encrypt
3153 .rva .LSEH_info_xts_enc
3155 .rva .LSEH_begin_aesni_xts_decrypt
3156 .rva .LSEH_end_aesni_xts_decrypt
3157 .rva .LSEH_info_xts_dec
3160 .rva .LSEH_begin_${PREFIX}_cbc_encrypt
3161 .rva .LSEH_end_${PREFIX}_cbc_encrypt
3164 .rva ${PREFIX}_set_decrypt_key
3165 .rva .LSEH_end_set_decrypt_key
3168 .rva ${PREFIX}_set_encrypt_key
3169 .rva .LSEH_end_set_encrypt_key
3174 $code.=<<___ if ($PREFIX eq "aesni");
3178 .LSEH_info_ccm64_enc:
3180 .rva ccm64_se_handler
3181 .rva .Lccm64_enc_body,.Lccm64_enc_ret # HandlerData[]
3182 .LSEH_info_ccm64_dec:
3184 .rva ccm64_se_handler
3185 .rva .Lccm64_dec_body,.Lccm64_dec_ret # HandlerData[]
3188 .rva ctr_xts_se_handler
3189 .rva .Lctr32_body,.Lctr32_epilogue # HandlerData[]
3192 .rva ctr_xts_se_handler
3193 .rva .Lxts_enc_body,.Lxts_enc_epilogue # HandlerData[]
3196 .rva ctr_xts_se_handler
3197 .rva .Lxts_dec_body,.Lxts_dec_epilogue # HandlerData[]
3204 .byte 0x01,0x04,0x01,0x00
3205 .byte 0x04,0x02,0x00,0x00 # sub rsp,8
3210 local *opcode=shift;
3214 $rex|=0x04 if($dst>=8);
3215 $rex|=0x01 if($src>=8);
3216 push @opcode,$rex|0x40 if($rex);
3223 if ($line=~/(aeskeygenassist)\s+\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
3224 rex(\@opcode,$4,$3);
3225 push @opcode,0x0f,0x3a,0xdf;
3226 push @opcode,0xc0|($3&7)|(($4&7)<<3); # ModR/M
3228 push @opcode,$c=~/^0/?oct($c):$c;
3229 return ".byte\t".join(',',@opcode);
3231 elsif ($line=~/(aes[a-z]+)\s+%xmm([0-9]+),\s*%xmm([0-9]+)/) {
3234 "aesenc" => 0xdc, "aesenclast" => 0xdd,
3235 "aesdec" => 0xde, "aesdeclast" => 0xdf
3237 return undef if (!defined($opcodelet{$1}));
3238 rex(\@opcode,$3,$2);
3239 push @opcode,0x0f,0x38,$opcodelet{$1};
3240 push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
3241 return ".byte\t".join(',',@opcode);
3246 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
3247 $code =~ s/\b(aes.*%xmm[0-9]+).*$/aesni($1)/gem;