3 # ====================================================================
4 # Written by Andy Polyakov <appro@openssl.org> 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.25 cycles processing
157 # one byte out of 8KB with 128-bit key, Sandy Bridge - 0.90. Just like
158 # in CTR mode AES instruction interleave factor was chosen to be 6x.
160 ######################################################################
161 # Current large-block performance in cycles per byte processed with
162 # 128-bit key (less is better).
164 # CBC en-/decrypt CTR XTS ECB
165 # Westmere 3.77/1.25 1.25 1.25 1.26
166 # * Bridge 5.07/0.74 0.75 0.90 0.85
167 # Haswell 4.44/0.63 0.63 0.73 0.63
168 # Atom 5.75/3.54 3.56 4.12 3.87(*)
169 # Bulldozer 5.77/0.70 0.72 0.90 0.70
171 # (*) Atom ECB result is suboptimal because of penalties incurred
172 # by operations on %xmm8-15. As ECB is not considered
173 # critical, nothing was done to mitigate the problem.
175 $PREFIX="aesni"; # if $PREFIX is set to "AES", the script
176 # generates drop-in replacement for
177 # crypto/aes/asm/aes-x86_64.pl:-)
181 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
183 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
185 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
186 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
187 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
188 die "can't locate x86_64-xlate.pl";
190 open OUT,"| \"$^X\" $xlate $flavour $output";
193 $movkey = $PREFIX eq "aesni" ? "movups" : "movups";
194 @_4args=$win64? ("%rcx","%rdx","%r8", "%r9") : # Win64 order
195 ("%rdi","%rsi","%rdx","%rcx"); # Unix order
198 $code.=".extern OPENSSL_ia32cap_P\n";
200 $rounds="%eax"; # input to and changed by aesni_[en|de]cryptN !!!
201 # this is natural Unix argument order for public $PREFIX_[ecb|cbc]_encrypt ...
205 $key="%rcx"; # input to and changed by aesni_[en|de]cryptN !!!
206 $ivp="%r8"; # cbc, ctr, ...
208 $rnds_="%r10d"; # backup copy for $rounds
209 $key_="%r11"; # backup copy for $key
211 # %xmm register layout
212 $rndkey0="%xmm0"; $rndkey1="%xmm1";
213 $inout0="%xmm2"; $inout1="%xmm3";
214 $inout2="%xmm4"; $inout3="%xmm5";
215 $inout4="%xmm6"; $inout5="%xmm7";
216 $inout6="%xmm8"; $inout7="%xmm9";
218 $in2="%xmm6"; $in1="%xmm7"; # used in CBC decrypt, CTR, ...
219 $in0="%xmm8"; $iv="%xmm9";
221 # Inline version of internal aesni_[en|de]crypt1.
223 # Why folded loop? Because aes[enc|dec] is slow enough to accommodate
224 # cycles which take care of loop variables...
226 sub aesni_generate1 {
227 my ($p,$key,$rounds,$inout,$ivec)=@_; $inout=$inout0 if (!defined($inout));
230 $movkey ($key),$rndkey0
231 $movkey 16($key),$rndkey1
233 $code.=<<___ if (defined($ivec));
238 $code.=<<___ if (!defined($ivec));
240 xorps $rndkey0,$inout
244 aes${p} $rndkey1,$inout
246 $movkey ($key),$rndkey1
248 jnz .Loop_${p}1_$sn # loop body is 16 bytes
249 aes${p}last $rndkey1,$inout
252 # void $PREFIX_[en|de]crypt (const void *inp,void *out,const AES_KEY *key);
254 { my ($inp,$out,$key) = @_4args;
257 .globl ${PREFIX}_encrypt
258 .type ${PREFIX}_encrypt,\@abi-omnipotent
261 movups ($inp),$inout0 # load input
262 mov 240($key),$rounds # key->rounds
264 &aesni_generate1("enc",$key,$rounds);
266 movups $inout0,($out) # output
268 .size ${PREFIX}_encrypt,.-${PREFIX}_encrypt
270 .globl ${PREFIX}_decrypt
271 .type ${PREFIX}_decrypt,\@abi-omnipotent
274 movups ($inp),$inout0 # load input
275 mov 240($key),$rounds # key->rounds
277 &aesni_generate1("dec",$key,$rounds);
279 movups $inout0,($out) # output
281 .size ${PREFIX}_decrypt, .-${PREFIX}_decrypt
285 # _aesni_[en|de]cryptN are private interfaces, N denotes interleave
286 # factor. Why 3x subroutine were originally used in loops? Even though
287 # aes[enc|dec] latency was originally 6, it could be scheduled only
288 # every *2nd* cycle. Thus 3x interleave was the one providing optimal
289 # utilization, i.e. when subroutine's throughput is virtually same as
290 # of non-interleaved subroutine [for number of input blocks up to 3].
291 # This is why it makes no sense to implement 2x subroutine.
292 # aes[enc|dec] latency in next processor generation is 8, but the
293 # instructions can be scheduled every cycle. Optimal interleave for
294 # new processor is therefore 8x...
295 sub aesni_generate3 {
297 # As already mentioned it takes in $key and $rounds, which are *not*
298 # preserved. $inout[0-2] is cipher/clear text...
300 .type _aesni_${dir}rypt3,\@abi-omnipotent
303 $movkey ($key),$rndkey0
305 $movkey 16($key),$rndkey1
306 xorps $rndkey0,$inout0
307 xorps $rndkey0,$inout1
308 xorps $rndkey0,$inout2
309 $movkey 32($key),$rndkey0
310 lea 32($key,$rounds),$key
315 aes${dir} $rndkey1,$inout0
316 aes${dir} $rndkey1,$inout1
317 aes${dir} $rndkey1,$inout2
318 $movkey ($key,%rax),$rndkey1
320 aes${dir} $rndkey0,$inout0
321 aes${dir} $rndkey0,$inout1
322 aes${dir} $rndkey0,$inout2
323 $movkey -16($key,%rax),$rndkey0
326 aes${dir} $rndkey1,$inout0
327 aes${dir} $rndkey1,$inout1
328 aes${dir} $rndkey1,$inout2
329 aes${dir}last $rndkey0,$inout0
330 aes${dir}last $rndkey0,$inout1
331 aes${dir}last $rndkey0,$inout2
333 .size _aesni_${dir}rypt3,.-_aesni_${dir}rypt3
336 # 4x interleave is implemented to improve small block performance,
337 # most notably [and naturally] 4 block by ~30%. One can argue that one
338 # should have implemented 5x as well, but improvement would be <20%,
339 # so it's not worth it...
340 sub aesni_generate4 {
342 # As already mentioned it takes in $key and $rounds, which are *not*
343 # preserved. $inout[0-3] is cipher/clear text...
345 .type _aesni_${dir}rypt4,\@abi-omnipotent
348 $movkey ($key),$rndkey0
350 $movkey 16($key),$rndkey1
351 xorps $rndkey0,$inout0
352 xorps $rndkey0,$inout1
353 xorps $rndkey0,$inout2
354 xorps $rndkey0,$inout3
355 $movkey 32($key),$rndkey0
356 lea 32($key,$rounds),$key
362 aes${dir} $rndkey1,$inout0
363 aes${dir} $rndkey1,$inout1
364 aes${dir} $rndkey1,$inout2
365 aes${dir} $rndkey1,$inout3
366 $movkey ($key,%rax),$rndkey1
368 aes${dir} $rndkey0,$inout0
369 aes${dir} $rndkey0,$inout1
370 aes${dir} $rndkey0,$inout2
371 aes${dir} $rndkey0,$inout3
372 $movkey -16($key,%rax),$rndkey0
375 aes${dir} $rndkey1,$inout0
376 aes${dir} $rndkey1,$inout1
377 aes${dir} $rndkey1,$inout2
378 aes${dir} $rndkey1,$inout3
379 aes${dir}last $rndkey0,$inout0
380 aes${dir}last $rndkey0,$inout1
381 aes${dir}last $rndkey0,$inout2
382 aes${dir}last $rndkey0,$inout3
384 .size _aesni_${dir}rypt4,.-_aesni_${dir}rypt4
387 sub aesni_generate6 {
389 # As already mentioned it takes in $key and $rounds, which are *not*
390 # preserved. $inout[0-5] is cipher/clear text...
392 .type _aesni_${dir}rypt6,\@abi-omnipotent
395 $movkey ($key),$rndkey0
397 $movkey 16($key),$rndkey1
398 xorps $rndkey0,$inout0
399 pxor $rndkey0,$inout1
400 pxor $rndkey0,$inout2
401 aes${dir} $rndkey1,$inout0
402 lea 32($key,$rounds),$key
404 aes${dir} $rndkey1,$inout1
405 pxor $rndkey0,$inout3
406 pxor $rndkey0,$inout4
407 aes${dir} $rndkey1,$inout2
408 pxor $rndkey0,$inout5
410 aes${dir} $rndkey1,$inout3
411 aes${dir} $rndkey1,$inout4
412 aes${dir} $rndkey1,$inout5
413 $movkey -16($key,%rax),$rndkey0
414 jmp .L${dir}_loop6_enter
417 aes${dir} $rndkey1,$inout0
418 aes${dir} $rndkey1,$inout1
419 aes${dir} $rndkey1,$inout2
420 aes${dir} $rndkey1,$inout3
421 aes${dir} $rndkey1,$inout4
422 aes${dir} $rndkey1,$inout5
423 .L${dir}_loop6_enter:
424 $movkey ($key,%rax),$rndkey1
426 aes${dir} $rndkey0,$inout0
427 aes${dir} $rndkey0,$inout1
428 aes${dir} $rndkey0,$inout2
429 aes${dir} $rndkey0,$inout3
430 aes${dir} $rndkey0,$inout4
431 aes${dir} $rndkey0,$inout5
432 $movkey -16($key,%rax),$rndkey0
435 aes${dir} $rndkey1,$inout0
436 aes${dir} $rndkey1,$inout1
437 aes${dir} $rndkey1,$inout2
438 aes${dir} $rndkey1,$inout3
439 aes${dir} $rndkey1,$inout4
440 aes${dir} $rndkey1,$inout5
441 aes${dir}last $rndkey0,$inout0
442 aes${dir}last $rndkey0,$inout1
443 aes${dir}last $rndkey0,$inout2
444 aes${dir}last $rndkey0,$inout3
445 aes${dir}last $rndkey0,$inout4
446 aes${dir}last $rndkey0,$inout5
448 .size _aesni_${dir}rypt6,.-_aesni_${dir}rypt6
451 sub aesni_generate8 {
453 # As already mentioned it takes in $key and $rounds, which are *not*
454 # preserved. $inout[0-7] is cipher/clear text...
456 .type _aesni_${dir}rypt8,\@abi-omnipotent
459 $movkey ($key),$rndkey0
461 $movkey 16($key),$rndkey1
462 xorps $rndkey0,$inout0
463 xorps $rndkey0,$inout1
464 pxor $rndkey0,$inout2
465 pxor $rndkey0,$inout3
466 pxor $rndkey0,$inout4
467 lea 32($key,$rounds),$key
469 aes${dir} $rndkey1,$inout0
471 pxor $rndkey0,$inout5
472 aes${dir} $rndkey1,$inout1
473 pxor $rndkey0,$inout6
474 pxor $rndkey0,$inout7
475 aes${dir} $rndkey1,$inout2
476 aes${dir} $rndkey1,$inout3
477 aes${dir} $rndkey1,$inout4
478 aes${dir} $rndkey1,$inout5
479 aes${dir} $rndkey1,$inout6
480 aes${dir} $rndkey1,$inout7
481 $movkey -16($key,%rax),$rndkey0
482 jmp .L${dir}_loop8_enter
485 aes${dir} $rndkey1,$inout0
486 aes${dir} $rndkey1,$inout1
487 aes${dir} $rndkey1,$inout2
488 aes${dir} $rndkey1,$inout3
489 aes${dir} $rndkey1,$inout4
490 aes${dir} $rndkey1,$inout5
491 aes${dir} $rndkey1,$inout6
492 aes${dir} $rndkey1,$inout7
493 .L${dir}_loop8_enter:
494 $movkey ($key,%rax),$rndkey1
496 aes${dir} $rndkey0,$inout0
497 aes${dir} $rndkey0,$inout1
498 aes${dir} $rndkey0,$inout2
499 aes${dir} $rndkey0,$inout3
500 aes${dir} $rndkey0,$inout4
501 aes${dir} $rndkey0,$inout5
502 aes${dir} $rndkey0,$inout6
503 aes${dir} $rndkey0,$inout7
504 $movkey -16($key,%rax),$rndkey0
507 aes${dir} $rndkey1,$inout0
508 aes${dir} $rndkey1,$inout1
509 aes${dir} $rndkey1,$inout2
510 aes${dir} $rndkey1,$inout3
511 aes${dir} $rndkey1,$inout4
512 aes${dir} $rndkey1,$inout5
513 aes${dir} $rndkey1,$inout6
514 aes${dir} $rndkey1,$inout7
515 aes${dir}last $rndkey0,$inout0
516 aes${dir}last $rndkey0,$inout1
517 aes${dir}last $rndkey0,$inout2
518 aes${dir}last $rndkey0,$inout3
519 aes${dir}last $rndkey0,$inout4
520 aes${dir}last $rndkey0,$inout5
521 aes${dir}last $rndkey0,$inout6
522 aes${dir}last $rndkey0,$inout7
524 .size _aesni_${dir}rypt8,.-_aesni_${dir}rypt8
527 &aesni_generate3("enc") if ($PREFIX eq "aesni");
528 &aesni_generate3("dec");
529 &aesni_generate4("enc") if ($PREFIX eq "aesni");
530 &aesni_generate4("dec");
531 &aesni_generate6("enc") if ($PREFIX eq "aesni");
532 &aesni_generate6("dec");
533 &aesni_generate8("enc") if ($PREFIX eq "aesni");
534 &aesni_generate8("dec");
536 if ($PREFIX eq "aesni") {
537 ########################################################################
538 # void aesni_ecb_encrypt (const void *in, void *out,
539 # size_t length, const AES_KEY *key,
542 .globl aesni_ecb_encrypt
543 .type aesni_ecb_encrypt,\@function,5
549 mov 240($key),$rounds # key->rounds
550 $movkey ($key),$rndkey0
551 mov $key,$key_ # backup $key
552 mov $rounds,$rnds_ # backup $rounds
553 test %r8d,%r8d # 5th argument
555 #--------------------------- ECB ENCRYPT ------------------------------#
559 movdqu ($inp),$inout0
560 movdqu 0x10($inp),$inout1
561 movdqu 0x20($inp),$inout2
562 movdqu 0x30($inp),$inout3
563 movdqu 0x40($inp),$inout4
564 movdqu 0x50($inp),$inout5
565 movdqu 0x60($inp),$inout6
566 movdqu 0x70($inp),$inout7
569 jmp .Lecb_enc_loop8_enter
572 movups $inout0,($out)
573 mov $key_,$key # restore $key
574 movdqu ($inp),$inout0
575 mov $rnds_,$rounds # restore $rounds
576 movups $inout1,0x10($out)
577 movdqu 0x10($inp),$inout1
578 movups $inout2,0x20($out)
579 movdqu 0x20($inp),$inout2
580 movups $inout3,0x30($out)
581 movdqu 0x30($inp),$inout3
582 movups $inout4,0x40($out)
583 movdqu 0x40($inp),$inout4
584 movups $inout5,0x50($out)
585 movdqu 0x50($inp),$inout5
586 movups $inout6,0x60($out)
587 movdqu 0x60($inp),$inout6
588 movups $inout7,0x70($out)
590 movdqu 0x70($inp),$inout7
592 .Lecb_enc_loop8_enter:
599 movups $inout0,($out)
600 mov $key_,$key # restore $key
601 movups $inout1,0x10($out)
602 mov $rnds_,$rounds # restore $rounds
603 movups $inout2,0x20($out)
604 movups $inout3,0x30($out)
605 movups $inout4,0x40($out)
606 movups $inout5,0x50($out)
607 movups $inout6,0x60($out)
608 movups $inout7,0x70($out)
614 movups ($inp),$inout0
617 movups 0x10($inp),$inout1
619 movups 0x20($inp),$inout2
622 movups 0x30($inp),$inout3
624 movups 0x40($inp),$inout4
627 movups 0x50($inp),$inout5
629 movdqu 0x60($inp),$inout6
631 movups $inout0,($out)
632 movups $inout1,0x10($out)
633 movups $inout2,0x20($out)
634 movups $inout3,0x30($out)
635 movups $inout4,0x40($out)
636 movups $inout5,0x50($out)
637 movups $inout6,0x60($out)
642 &aesni_generate1("enc",$key,$rounds);
644 movups $inout0,($out)
648 xorps $inout2,$inout2
650 movups $inout0,($out)
651 movups $inout1,0x10($out)
656 movups $inout0,($out)
657 movups $inout1,0x10($out)
658 movups $inout2,0x20($out)
663 movups $inout0,($out)
664 movups $inout1,0x10($out)
665 movups $inout2,0x20($out)
666 movups $inout3,0x30($out)
670 xorps $inout5,$inout5
672 movups $inout0,($out)
673 movups $inout1,0x10($out)
674 movups $inout2,0x20($out)
675 movups $inout3,0x30($out)
676 movups $inout4,0x40($out)
681 movups $inout0,($out)
682 movups $inout1,0x10($out)
683 movups $inout2,0x20($out)
684 movups $inout3,0x30($out)
685 movups $inout4,0x40($out)
686 movups $inout5,0x50($out)
688 \f#--------------------------- ECB DECRYPT ------------------------------#
694 movdqu ($inp),$inout0
695 movdqu 0x10($inp),$inout1
696 movdqu 0x20($inp),$inout2
697 movdqu 0x30($inp),$inout3
698 movdqu 0x40($inp),$inout4
699 movdqu 0x50($inp),$inout5
700 movdqu 0x60($inp),$inout6
701 movdqu 0x70($inp),$inout7
704 jmp .Lecb_dec_loop8_enter
707 movups $inout0,($out)
708 mov $key_,$key # restore $key
709 movdqu ($inp),$inout0
710 mov $rnds_,$rounds # restore $rounds
711 movups $inout1,0x10($out)
712 movdqu 0x10($inp),$inout1
713 movups $inout2,0x20($out)
714 movdqu 0x20($inp),$inout2
715 movups $inout3,0x30($out)
716 movdqu 0x30($inp),$inout3
717 movups $inout4,0x40($out)
718 movdqu 0x40($inp),$inout4
719 movups $inout5,0x50($out)
720 movdqu 0x50($inp),$inout5
721 movups $inout6,0x60($out)
722 movdqu 0x60($inp),$inout6
723 movups $inout7,0x70($out)
725 movdqu 0x70($inp),$inout7
727 .Lecb_dec_loop8_enter:
731 $movkey ($key_),$rndkey0
735 movups $inout0,($out)
736 mov $key_,$key # restore $key
737 movups $inout1,0x10($out)
738 mov $rnds_,$rounds # restore $rounds
739 movups $inout2,0x20($out)
740 movups $inout3,0x30($out)
741 movups $inout4,0x40($out)
742 movups $inout5,0x50($out)
743 movups $inout6,0x60($out)
744 movups $inout7,0x70($out)
750 movups ($inp),$inout0
753 movups 0x10($inp),$inout1
755 movups 0x20($inp),$inout2
758 movups 0x30($inp),$inout3
760 movups 0x40($inp),$inout4
763 movups 0x50($inp),$inout5
765 movups 0x60($inp),$inout6
766 $movkey ($key),$rndkey0
768 movups $inout0,($out)
769 movups $inout1,0x10($out)
770 movups $inout2,0x20($out)
771 movups $inout3,0x30($out)
772 movups $inout4,0x40($out)
773 movups $inout5,0x50($out)
774 movups $inout6,0x60($out)
779 &aesni_generate1("dec",$key,$rounds);
781 movups $inout0,($out)
785 xorps $inout2,$inout2
787 movups $inout0,($out)
788 movups $inout1,0x10($out)
793 movups $inout0,($out)
794 movups $inout1,0x10($out)
795 movups $inout2,0x20($out)
800 movups $inout0,($out)
801 movups $inout1,0x10($out)
802 movups $inout2,0x20($out)
803 movups $inout3,0x30($out)
807 xorps $inout5,$inout5
809 movups $inout0,($out)
810 movups $inout1,0x10($out)
811 movups $inout2,0x20($out)
812 movups $inout3,0x30($out)
813 movups $inout4,0x40($out)
818 movups $inout0,($out)
819 movups $inout1,0x10($out)
820 movups $inout2,0x20($out)
821 movups $inout3,0x30($out)
822 movups $inout4,0x40($out)
823 movups $inout5,0x50($out)
827 .size aesni_ecb_encrypt,.-aesni_ecb_encrypt
831 ######################################################################
832 # void aesni_ccm64_[en|de]crypt_blocks (const void *in, void *out,
833 # size_t blocks, const AES_KEY *key,
834 # const char *ivec,char *cmac);
836 # Handles only complete blocks, operates on 64-bit counter and
837 # does not update *ivec! Nor does it finalize CMAC value
838 # (see engine/eng_aesni.c for details)
841 my $cmac="%r9"; # 6th argument
843 my $increment="%xmm9";
845 my $bswap_mask="%xmm7";
848 .globl aesni_ccm64_encrypt_blocks
849 .type aesni_ccm64_encrypt_blocks,\@function,6
851 aesni_ccm64_encrypt_blocks:
853 $code.=<<___ if ($win64);
856 movaps %xmm7,0x10(%rsp)
857 movaps %xmm8,0x20(%rsp)
858 movaps %xmm9,0x30(%rsp)
862 mov 240($key),$rounds # key->rounds
864 movdqa .Lincrement64(%rip),$increment
865 movdqa .Lbswap_mask(%rip),$bswap_mask
870 movdqu ($cmac),$inout1
872 lea 32($key,$rounds),$key # end of key schedule
873 pshufb $bswap_mask,$iv
874 sub %rax,%r10 # twisted $rounds
875 jmp .Lccm64_enc_outer
878 $movkey ($key_),$rndkey0
880 movups ($inp),$in0 # load inp
882 xorps $rndkey0,$inout0 # counter
883 $movkey 16($key_),$rndkey1
885 xorps $rndkey0,$inout1 # cmac^=inp
886 $movkey 32($key_),$rndkey0
889 aesenc $rndkey1,$inout0
890 aesenc $rndkey1,$inout1
891 $movkey ($key,%rax),$rndkey1
893 aesenc $rndkey0,$inout0
894 aesenc $rndkey0,$inout1
895 $movkey -16($key,%rax),$rndkey0
896 jnz .Lccm64_enc2_loop
897 aesenc $rndkey1,$inout0
898 aesenc $rndkey1,$inout1
901 aesenclast $rndkey0,$inout0
902 aesenclast $rndkey0,$inout1
905 xorps $inout0,$in0 # inp ^= E(iv)
907 movups $in0,($out) # save output
908 pshufb $bswap_mask,$inout0
910 jnz .Lccm64_enc_outer
912 movups $inout1,($cmac)
914 $code.=<<___ if ($win64);
916 movaps 0x10(%rsp),%xmm7
917 movaps 0x20(%rsp),%xmm8
918 movaps 0x30(%rsp),%xmm9
924 .size aesni_ccm64_encrypt_blocks,.-aesni_ccm64_encrypt_blocks
926 ######################################################################
928 .globl aesni_ccm64_decrypt_blocks
929 .type aesni_ccm64_decrypt_blocks,\@function,6
931 aesni_ccm64_decrypt_blocks:
933 $code.=<<___ if ($win64);
936 movaps %xmm7,0x10(%rsp)
937 movaps %xmm8,0x20(%rsp)
938 movaps %xmm9,0x30(%rsp)
942 mov 240($key),$rounds # key->rounds
944 movdqu ($cmac),$inout1
945 movdqa .Lincrement64(%rip),$increment
946 movdqa .Lbswap_mask(%rip),$bswap_mask
951 pshufb $bswap_mask,$iv
953 &aesni_generate1("enc",$key,$rounds);
957 movups ($inp),$in0 # load inp
960 sub %r10,%rax # twisted $rounds
961 lea 32($key_,$rnds_),$key # end of key schedule
963 jmp .Lccm64_dec_outer
966 xorps $inout0,$in0 # inp ^= E(iv)
968 movups $in0,($out) # save output
970 pshufb $bswap_mask,$inout0
975 $movkey ($key_),$rndkey0
977 $movkey 16($key_),$rndkey1
979 xorps $rndkey0,$inout0
980 xorps $in0,$inout1 # cmac^=out
981 $movkey 32($key_),$rndkey0
982 jmp .Lccm64_dec2_loop
985 aesenc $rndkey1,$inout0
986 aesenc $rndkey1,$inout1
987 $movkey ($key,%rax),$rndkey1
989 aesenc $rndkey0,$inout0
990 aesenc $rndkey0,$inout1
991 $movkey -16($key,%rax),$rndkey0
992 jnz .Lccm64_dec2_loop
993 movups ($inp),$in0 # load inp
995 aesenc $rndkey1,$inout0
996 aesenc $rndkey1,$inout1
997 aesenclast $rndkey0,$inout0
998 aesenclast $rndkey0,$inout1
1000 jmp .Lccm64_dec_outer
1004 #xorps $in0,$inout1 # cmac^=out
1005 mov 240($key_),$rounds
1007 &aesni_generate1("enc",$key_,$rounds,$inout1,$in0);
1009 movups $inout1,($cmac)
1011 $code.=<<___ if ($win64);
1013 movaps 0x10(%rsp),%xmm7
1014 movaps 0x20(%rsp),%xmm8
1015 movaps 0x30(%rsp),%xmm9
1021 .size aesni_ccm64_decrypt_blocks,.-aesni_ccm64_decrypt_blocks
1024 ######################################################################
1025 # void aesni_ctr32_encrypt_blocks (const void *in, void *out,
1026 # size_t blocks, const AES_KEY *key,
1027 # const char *ivec);
1029 # Handles only complete blocks, operates on 32-bit counter and
1030 # does not update *ivec! (see crypto/modes/ctr128.c for details)
1032 # Overhaul based on suggestions from Shay Gueron and Vlad Krasnov,
1033 # http://rt.openssl.org/Ticket/Display.html?id=3021&user=guest&pass=guest.
1034 # Keywords are full unroll and modulo-schedule counter calculations
1035 # with zero-round key xor.
1037 my ($in0,$in1,$in2,$in3,$in4,$in5)=map("%xmm$_",(10..15));
1038 my ($key0,$ctr)=("${key_}d","${ivp}d");
1039 my $frame_size = 0x80 + ($win64?160:0);
1042 .globl aesni_ctr32_encrypt_blocks
1043 .type aesni_ctr32_encrypt_blocks,\@function,5
1045 aesni_ctr32_encrypt_blocks:
1048 sub \$$frame_size,%rsp
1049 and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
1051 $code.=<<___ if ($win64);
1052 movaps %xmm6,-0xa8(%rax)
1053 movaps %xmm7,-0x98(%rax)
1054 movaps %xmm8,-0x88(%rax)
1055 movaps %xmm9,-0x78(%rax)
1056 movaps %xmm10,-0x68(%rax)
1057 movaps %xmm11,-0x58(%rax)
1058 movaps %xmm12,-0x48(%rax)
1059 movaps %xmm13,-0x38(%rax)
1060 movaps %xmm14,-0x28(%rax)
1061 movaps %xmm15,-0x18(%rax)
1068 je .Lctr32_one_shortcut
1070 movdqu ($ivp),$inout0
1071 movdqu ($key),$rndkey0
1072 mov 12($ivp),$ctr # counter LSB
1073 pxor $rndkey0,$inout0
1074 mov 12($key),$key0 # 0-round key LSB
1075 movdqa $inout0,0x00(%rsp) # populate counter block
1077 movdqa $inout0,$inout1
1078 movdqa $inout0,$inout2
1079 movdqa $inout0,$inout3
1080 movdqa $inout0,0x40(%rsp)
1081 movdqa $inout0,0x50(%rsp)
1082 movdqa $inout0,0x60(%rsp)
1083 mov %rdx,%r10 # borrow %rdx
1084 movdqa $inout0,0x70(%rsp)
1092 pinsrd \$3,%eax,$inout1
1094 movdqa $inout1,0x10(%rsp)
1095 pinsrd \$3,%edx,$inout2
1097 mov %r10,%rdx # restore %rdx
1099 movdqa $inout2,0x20(%rsp)
1102 pinsrd \$3,%eax,$inout3
1104 movdqa $inout3,0x30(%rsp)
1106 mov %r10d,0x40+12(%rsp)
1109 mov 240($key),$rounds # key->rounds
1112 mov %r9d,0x50+12(%rsp)
1115 mov %r10d,0x60+12(%rsp)
1117 mov OPENSSL_ia32cap_P+4(%rip),%r10d
1119 and \$`1<<26|1<<22`,%r10d # isolate XSAVE+MOVBE
1120 mov %r9d,0x70+12(%rsp)
1122 $movkey 0x10($key),$rndkey1
1124 movdqa 0x40(%rsp),$inout4
1125 movdqa 0x50(%rsp),$inout5
1131 cmp \$`1<<22`,%r10d # check for MOVBE without XSAVE
1134 lea 0x80($key),$key # size optimization
1143 lea 32($key,$rounds),$key # end of key schedule
1144 sub %rax,%r10 # twisted $rounds
1150 $movkey -48($key,$rnds_),$rndkey0
1151 aesenc $rndkey1,$inout0
1154 aesenc $rndkey1,$inout1
1155 movbe %eax,`0x00+12`(%rsp)
1157 aesenc $rndkey1,$inout2
1159 movbe %eax,`0x10+12`(%rsp)
1160 aesenc $rndkey1,$inout3
1163 aesenc $rndkey1,$inout4
1164 movbe %eax,`0x20+12`(%rsp)
1166 aesenc $rndkey1,$inout5
1167 $movkey -32($key,$rnds_),$rndkey1
1170 aesenc $rndkey0,$inout0
1171 movbe %eax,`0x30+12`(%rsp)
1173 aesenc $rndkey0,$inout1
1175 movbe %eax,`0x40+12`(%rsp)
1176 aesenc $rndkey0,$inout2
1179 aesenc $rndkey0,$inout3
1180 movbe %eax,`0x50+12`(%rsp)
1181 mov %r10,%rax # mov $rnds_,$rounds
1182 aesenc $rndkey0,$inout4
1183 aesenc $rndkey0,$inout5
1184 $movkey -16($key,$rnds_),$rndkey0
1188 movdqu ($inp),$inout6
1189 movdqu 0x10($inp),$inout7
1190 movdqu 0x20($inp),$in0
1191 movdqu 0x30($inp),$in1
1192 movdqu 0x40($inp),$in2
1193 movdqu 0x50($inp),$in3
1195 $movkey -64($key,$rnds_),$rndkey1
1196 pxor $inout0,$inout6
1197 movaps 0x00(%rsp),$inout0
1198 pxor $inout1,$inout7
1199 movaps 0x10(%rsp),$inout1
1201 movaps 0x20(%rsp),$inout2
1203 movaps 0x30(%rsp),$inout3
1205 movaps 0x40(%rsp),$inout4
1207 movaps 0x50(%rsp),$inout5
1208 movdqu $inout6,($out)
1209 movdqu $inout7,0x10($out)
1210 movdqu $in0,0x20($out)
1211 movdqu $in1,0x30($out)
1212 movdqu $in2,0x40($out)
1213 movdqu $in3,0x50($out)
1222 lea -48($rnds_),$rounds
1223 lea -80($key,$rnds_),$key # restore $key
1225 shr \$4,$rounds # restore $rounds
1231 movdqa 0x60(%rsp),$inout6
1232 aesenc $rndkey1,$inout0
1234 movdqa 0x70(%rsp),$inout7
1235 aesenc $rndkey1,$inout1
1237 $movkey 0x20-0x80($key),$rndkey0
1238 aesenc $rndkey1,$inout2
1241 aesenc $rndkey1,$inout3
1242 mov %r9d,0x00+12(%rsp)
1244 aesenc $rndkey1,$inout4
1245 aesenc $rndkey1,$inout5
1246 aesenc $rndkey1,$inout6
1247 aesenc $rndkey1,$inout7
1248 $movkey 0x30-0x80($key),$rndkey1
1250 for($i=2;$i<8;$i++) {
1251 my $rndkeyx = ($i&1)?$rndkey1:$rndkey0;
1254 aesenc $rndkeyx,$inout0
1255 aesenc $rndkeyx,$inout1
1258 aesenc $rndkeyx,$inout2
1259 aesenc $rndkeyx,$inout3
1260 mov %r9d,`0x10*($i-1)`+12(%rsp)
1262 aesenc $rndkeyx,$inout4
1263 aesenc $rndkeyx,$inout5
1264 aesenc $rndkeyx,$inout6
1265 aesenc $rndkeyx,$inout7
1266 $movkey `0x20+0x10*$i`-0x80($key),$rndkeyx
1271 aesenc $rndkey0,$inout0
1272 aesenc $rndkey0,$inout1
1273 aesenc $rndkey0,$inout2
1275 movdqu 0x00($inp),$in0
1276 aesenc $rndkey0,$inout3
1277 mov %r9d,0x70+12(%rsp)
1279 aesenc $rndkey0,$inout4
1280 aesenc $rndkey0,$inout5
1281 aesenc $rndkey0,$inout6
1282 aesenc $rndkey0,$inout7
1283 $movkey 0xa0-0x80($key),$rndkey0
1287 aesenc $rndkey1,$inout0
1288 aesenc $rndkey1,$inout1
1289 aesenc $rndkey1,$inout2
1290 aesenc $rndkey1,$inout3
1291 aesenc $rndkey1,$inout4
1292 aesenc $rndkey1,$inout5
1293 aesenc $rndkey1,$inout6
1294 aesenc $rndkey1,$inout7
1295 $movkey 0xb0-0x80($key),$rndkey1
1297 aesenc $rndkey0,$inout0
1298 aesenc $rndkey0,$inout1
1299 aesenc $rndkey0,$inout2
1300 aesenc $rndkey0,$inout3
1301 aesenc $rndkey0,$inout4
1302 aesenc $rndkey0,$inout5
1303 aesenc $rndkey0,$inout6
1304 aesenc $rndkey0,$inout7
1305 $movkey 0xc0-0x80($key),$rndkey0
1308 aesenc $rndkey1,$inout0
1309 aesenc $rndkey1,$inout1
1310 aesenc $rndkey1,$inout2
1311 aesenc $rndkey1,$inout3
1312 aesenc $rndkey1,$inout4
1313 aesenc $rndkey1,$inout5
1314 aesenc $rndkey1,$inout6
1315 aesenc $rndkey1,$inout7
1316 $movkey 0xd0-0x80($key),$rndkey1
1318 aesenc $rndkey0,$inout0
1319 aesenc $rndkey0,$inout1
1320 aesenc $rndkey0,$inout2
1321 aesenc $rndkey0,$inout3
1322 aesenc $rndkey0,$inout4
1323 aesenc $rndkey0,$inout5
1324 aesenc $rndkey0,$inout6
1325 aesenc $rndkey0,$inout7
1326 $movkey 0xe0-0x80($key),$rndkey0
1327 jmp .Lctr32_enc_done
1331 movdqu 0x10($inp),$in1
1333 movdqu 0x20($inp),$in2
1335 movdqu 0x30($inp),$in3
1337 movdqu 0x40($inp),$in4
1339 movdqu 0x50($inp),$in5
1342 aesenc $rndkey1,$inout0
1343 aesenc $rndkey1,$inout1
1344 aesenc $rndkey1,$inout2
1345 aesenc $rndkey1,$inout3
1346 aesenc $rndkey1,$inout4
1347 aesenc $rndkey1,$inout5
1348 aesenc $rndkey1,$inout6
1349 aesenc $rndkey1,$inout7
1350 movdqu 0x60($inp),$rndkey1
1353 aesenclast $in0,$inout0
1354 pxor $rndkey0,$rndkey1
1355 movdqu 0x70-0x80($inp),$in0
1356 aesenclast $in1,$inout1
1358 movdqa 0x00(%rsp),$in1 # load next counter block
1359 aesenclast $in2,$inout2
1360 aesenclast $in3,$inout3
1361 movdqa 0x10(%rsp),$in2
1362 movdqa 0x20(%rsp),$in3
1363 aesenclast $in4,$inout4
1364 aesenclast $in5,$inout5
1365 movdqa 0x30(%rsp),$in4
1366 movdqa 0x40(%rsp),$in5
1367 aesenclast $rndkey1,$inout6
1368 movdqa 0x50(%rsp),$rndkey0
1369 $movkey 0x10-0x80($key),$rndkey1
1370 aesenclast $in0,$inout7
1372 movups $inout0,($out) # store output
1374 movups $inout1,0x10($out)
1376 movups $inout2,0x20($out)
1378 movups $inout3,0x30($out)
1380 movups $inout4,0x40($out)
1382 movups $inout5,0x50($out)
1383 movdqa $rndkey0,$inout5
1384 movups $inout6,0x60($out)
1385 movups $inout7,0x70($out)
1393 lea -0x80($key),$key
1402 movdqa 0x60(%rsp),$inout6
1403 pxor $inout7,$inout7
1405 $movkey 16($key),$rndkey0
1406 aesenc $rndkey1,$inout0
1407 aesenc $rndkey1,$inout1
1408 lea 32-16($key,$rounds),$key
1410 aesenc $rndkey1,$inout2
1413 aesenc $rndkey1,$inout3
1414 aesenc $rndkey1,$inout4
1415 movups 0x10($inp),$in1
1416 movups 0x20($inp),$in2
1417 aesenc $rndkey1,$inout5
1418 aesenc $rndkey1,$inout6
1420 call .Lenc_loop8_enter
1422 movdqu 0x30($inp),$in3
1424 movdqu 0x40($inp),$in0
1426 movdqu $inout0,($out)
1428 movdqu $inout1,0x10($out)
1430 movdqu $inout2,0x20($out)
1432 movdqu $inout3,0x30($out)
1433 movdqu $inout4,0x40($out)
1437 movups 0x50($inp),$in1
1439 movups $inout5,0x50($out)
1442 movups 0x60($inp),$in2
1444 movups $inout6,0x60($out)
1449 aesenc $rndkey1,$inout0
1452 aesenc $rndkey1,$inout1
1453 aesenc $rndkey1,$inout2
1454 aesenc $rndkey1,$inout3
1455 $movkey ($key),$rndkey1
1457 aesenclast $rndkey1,$inout0
1458 aesenclast $rndkey1,$inout1
1460 movups 0x10($inp),$in1
1461 aesenclast $rndkey1,$inout2
1462 aesenclast $rndkey1,$inout3
1463 movups 0x20($inp),$in2
1464 movups 0x30($inp),$in3
1467 movups $inout0,($out)
1469 movups $inout1,0x10($out)
1471 movdqu $inout2,0x20($out)
1473 movdqu $inout3,0x30($out)
1478 aesenc $rndkey1,$inout0
1481 aesenc $rndkey1,$inout1
1482 aesenc $rndkey1,$inout2
1483 $movkey ($key),$rndkey1
1485 aesenclast $rndkey1,$inout0
1486 aesenclast $rndkey1,$inout1
1487 aesenclast $rndkey1,$inout2
1491 movups $inout0,($out)
1495 movups 0x10($inp),$in1
1497 movups $inout1,0x10($out)
1500 movups 0x20($inp),$in2
1502 movups $inout2,0x20($out)
1506 .Lctr32_one_shortcut:
1507 movups ($ivp),$inout0
1509 mov 240($key),$rounds # key->rounds
1511 &aesni_generate1("enc",$key,$rounds);
1514 movups $inout0,($out)
1520 $code.=<<___ if ($win64);
1521 movaps -0xa0(%rbp),%xmm6
1522 movaps -0x90(%rbp),%xmm7
1523 movaps -0x80(%rbp),%xmm8
1524 movaps -0x70(%rbp),%xmm9
1525 movaps -0x60(%rbp),%xmm10
1526 movaps -0x50(%rbp),%xmm11
1527 movaps -0x40(%rbp),%xmm12
1528 movaps -0x30(%rbp),%xmm13
1529 movaps -0x20(%rbp),%xmm14
1530 movaps -0x10(%rbp),%xmm15
1537 .size aesni_ctr32_encrypt_blocks,.-aesni_ctr32_encrypt_blocks
1541 ######################################################################
1542 # void aesni_xts_[en|de]crypt(const char *inp,char *out,size_t len,
1543 # const AES_KEY *key1, const AES_KEY *key2
1544 # const unsigned char iv[16]);
1547 my @tweak=map("%xmm$_",(10..15));
1548 my ($twmask,$twres,$twtmp)=("%xmm8","%xmm9",@tweak[4]);
1549 my ($key2,$ivp,$len_)=("%r8","%r9","%r9");
1550 my $frame_size = 0x70 + ($win64?160:0);
1553 .globl aesni_xts_encrypt
1554 .type aesni_xts_encrypt,\@function,6
1559 sub \$$frame_size,%rsp
1560 and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
1562 $code.=<<___ if ($win64);
1563 movaps %xmm6,-0xa8(%rax)
1564 movaps %xmm7,-0x98(%rax)
1565 movaps %xmm8,-0x88(%rax)
1566 movaps %xmm9,-0x78(%rax)
1567 movaps %xmm10,-0x68(%rax)
1568 movaps %xmm11,-0x58(%rax)
1569 movaps %xmm12,-0x48(%rax)
1570 movaps %xmm13,-0x38(%rax)
1571 movaps %xmm14,-0x28(%rax)
1572 movaps %xmm15,-0x18(%rax)
1577 movups ($ivp),$inout0 # load clear-text tweak
1578 mov 240(%r8),$rounds # key2->rounds
1579 mov 240($key),$rnds_ # key1->rounds
1581 # generate the tweak
1582 &aesni_generate1("enc",$key2,$rounds,$inout0);
1584 $movkey ($key),$rndkey0 # zero round key
1585 mov $key,$key_ # backup $key
1586 mov $rnds_,$rounds # backup $rounds
1588 mov $len,$len_ # backup $len
1591 $movkey 16($key,$rnds_),$rndkey1 # last round key
1593 movdqa .Lxts_magic(%rip),$twmask
1594 movdqa $inout0,@tweak[5]
1595 pshufd \$0x5f,$inout0,$twres
1596 pxor $rndkey0,$rndkey1
1598 # alternative tweak calculation algorithm is based on suggestions
1599 # by Shay Gueron. psrad doesn't conflict with AES-NI instructions
1600 # and should help in the future...
1601 for ($i=0;$i<4;$i++) {
1603 movdqa $twres,$twtmp
1605 movdqa @tweak[5],@tweak[$i]
1606 psrad \$31,$twtmp # broadcast upper bits
1607 paddq @tweak[5],@tweak[5]
1609 pxor $rndkey0,@tweak[$i]
1610 pxor $twtmp,@tweak[5]
1614 movdqa @tweak[5],@tweak[4]
1616 paddq @tweak[5],@tweak[5]
1618 pxor $rndkey0,@tweak[4]
1619 pxor $twres,@tweak[5]
1620 movaps $rndkey1,0x60(%rsp) # save round[0]^round[last]
1626 lea 32($key_,$rnds_),$key # end of key schedule
1627 sub %r10,%rax # twisted $rounds
1628 $movkey 16($key_),$rndkey1
1629 mov %rax,%r10 # backup twisted $rounds
1630 lea .Lxts_magic(%rip),%r8
1631 jmp .Lxts_enc_grandloop
1634 .Lxts_enc_grandloop:
1635 movdqu `16*0`($inp),$inout0 # load input
1636 movdqa $rndkey0,$twmask
1637 movdqu `16*1`($inp),$inout1
1638 pxor @tweak[0],$inout0
1639 movdqu `16*2`($inp),$inout2
1640 pxor @tweak[1],$inout1
1641 aesenc $rndkey1,$inout0
1642 movdqu `16*3`($inp),$inout3
1643 pxor @tweak[2],$inout2
1644 aesenc $rndkey1,$inout1
1645 movdqu `16*4`($inp),$inout4
1646 pxor @tweak[3],$inout3
1647 aesenc $rndkey1,$inout2
1648 movdqu `16*5`($inp),$inout5
1649 pxor @tweak[5],$twmask # round[0]^=tweak[5]
1650 movdqa 0x60(%rsp),$twres # load round[0]^round[last]
1651 pxor @tweak[4],$inout4
1652 aesenc $rndkey1,$inout3
1653 $movkey 32($key_),$rndkey0
1654 lea `16*6`($inp),$inp
1655 pxor $twmask,$inout5
1657 pxor $twres,@tweak[0]
1658 aesenc $rndkey1,$inout4
1659 pxor $twres,@tweak[1]
1660 movdqa @tweak[0],`16*0`(%rsp) # put aside tweaks^last round key
1661 aesenc $rndkey1,$inout5
1662 $movkey 48($key_),$rndkey1
1663 pxor $twres,@tweak[2]
1665 aesenc $rndkey0,$inout0
1666 pxor $twres,@tweak[3]
1667 movdqa @tweak[1],`16*1`(%rsp)
1668 aesenc $rndkey0,$inout1
1669 pxor $twres,@tweak[4]
1670 movdqa @tweak[2],`16*2`(%rsp)
1671 aesenc $rndkey0,$inout2
1672 aesenc $rndkey0,$inout3
1674 movdqa @tweak[4],`16*4`(%rsp)
1675 aesenc $rndkey0,$inout4
1676 aesenc $rndkey0,$inout5
1677 $movkey 64($key_),$rndkey0
1678 movdqa $twmask,`16*5`(%rsp)
1679 pshufd \$0x5f,@tweak[5],$twres
1683 aesenc $rndkey1,$inout0
1684 aesenc $rndkey1,$inout1
1685 aesenc $rndkey1,$inout2
1686 aesenc $rndkey1,$inout3
1687 aesenc $rndkey1,$inout4
1688 aesenc $rndkey1,$inout5
1689 $movkey -64($key,%rax),$rndkey1
1692 aesenc $rndkey0,$inout0
1693 aesenc $rndkey0,$inout1
1694 aesenc $rndkey0,$inout2
1695 aesenc $rndkey0,$inout3
1696 aesenc $rndkey0,$inout4
1697 aesenc $rndkey0,$inout5
1698 $movkey -80($key,%rax),$rndkey0
1701 movdqa (%r8),$twmask
1702 movdqa $twres,$twtmp
1704 aesenc $rndkey1,$inout0
1705 paddq @tweak[5],@tweak[5]
1707 aesenc $rndkey1,$inout1
1709 $movkey ($key_),@tweak[0] # load round[0]
1710 aesenc $rndkey1,$inout2
1711 aesenc $rndkey1,$inout3
1712 aesenc $rndkey1,$inout4
1713 pxor $twtmp,@tweak[5]
1714 movaps @tweak[0],@tweak[1] # copy round[0]
1715 aesenc $rndkey1,$inout5
1716 $movkey -64($key),$rndkey1
1718 movdqa $twres,$twtmp
1719 aesenc $rndkey0,$inout0
1721 pxor @tweak[5],@tweak[0]
1722 aesenc $rndkey0,$inout1
1724 paddq @tweak[5],@tweak[5]
1725 aesenc $rndkey0,$inout2
1726 aesenc $rndkey0,$inout3
1728 movaps @tweak[1],@tweak[2]
1729 aesenc $rndkey0,$inout4
1730 pxor $twtmp,@tweak[5]
1731 movdqa $twres,$twtmp
1732 aesenc $rndkey0,$inout5
1733 $movkey -48($key),$rndkey0
1736 aesenc $rndkey1,$inout0
1737 pxor @tweak[5],@tweak[1]
1739 aesenc $rndkey1,$inout1
1740 paddq @tweak[5],@tweak[5]
1742 aesenc $rndkey1,$inout2
1743 aesenc $rndkey1,$inout3
1744 movdqa @tweak[3],`16*3`(%rsp)
1745 pxor $twtmp,@tweak[5]
1746 aesenc $rndkey1,$inout4
1747 movaps @tweak[2],@tweak[3]
1748 movdqa $twres,$twtmp
1749 aesenc $rndkey1,$inout5
1750 $movkey -32($key),$rndkey1
1753 aesenc $rndkey0,$inout0
1754 pxor @tweak[5],@tweak[2]
1756 aesenc $rndkey0,$inout1
1757 paddq @tweak[5],@tweak[5]
1759 aesenc $rndkey0,$inout2
1760 aesenc $rndkey0,$inout3
1761 aesenc $rndkey0,$inout4
1762 pxor $twtmp,@tweak[5]
1763 movaps @tweak[3],@tweak[4]
1764 aesenc $rndkey0,$inout5
1766 movdqa $twres,$rndkey0
1768 aesenc $rndkey1,$inout0
1769 pxor @tweak[5],@tweak[3]
1771 aesenc $rndkey1,$inout1
1772 paddq @tweak[5],@tweak[5]
1773 pand $twmask,$rndkey0
1774 aesenc $rndkey1,$inout2
1775 aesenc $rndkey1,$inout3
1776 pxor $rndkey0,@tweak[5]
1777 $movkey ($key_),$rndkey0
1778 aesenc $rndkey1,$inout4
1779 aesenc $rndkey1,$inout5
1780 $movkey 16($key_),$rndkey1
1782 pxor @tweak[5],@tweak[4]
1783 aesenclast `16*0`(%rsp),$inout0
1785 paddq @tweak[5],@tweak[5]
1786 aesenclast `16*1`(%rsp),$inout1
1787 aesenclast `16*2`(%rsp),$inout2
1789 mov %r10,%rax # restore $rounds
1790 aesenclast `16*3`(%rsp),$inout3
1791 aesenclast `16*4`(%rsp),$inout4
1792 aesenclast `16*5`(%rsp),$inout5
1793 pxor $twres,@tweak[5]
1795 lea `16*6`($out),$out
1796 movups $inout0,`-16*6`($out) # write output
1797 movups $inout1,`-16*5`($out)
1798 movups $inout2,`-16*4`($out)
1799 movups $inout3,`-16*3`($out)
1800 movups $inout4,`-16*2`($out)
1801 movups $inout5,`-16*1`($out)
1803 jnc .Lxts_enc_grandloop
1807 mov $key_,$key # restore $key
1808 shr \$4,$rounds # restore original value
1811 mov $rounds,$rnds_ # backup $rounds
1812 pxor $rndkey0,@tweak[0]
1816 pxor $rndkey0,@tweak[1]
1819 pxor $rndkey0,@tweak[2]
1822 pxor $rndkey0,@tweak[3]
1825 pxor $rndkey0,@tweak[4]
1828 movdqu ($inp),$inout0
1829 movdqu 16*1($inp),$inout1
1830 movdqu 16*2($inp),$inout2
1831 pxor @tweak[0],$inout0
1832 movdqu 16*3($inp),$inout3
1833 pxor @tweak[1],$inout1
1834 movdqu 16*4($inp),$inout4
1836 pxor @tweak[2],$inout2
1837 pxor @tweak[3],$inout3
1838 pxor @tweak[4],$inout4
1840 call _aesni_encrypt6
1842 xorps @tweak[0],$inout0
1843 movdqa @tweak[5],@tweak[0]
1844 xorps @tweak[1],$inout1
1845 xorps @tweak[2],$inout2
1846 movdqu $inout0,($out)
1847 xorps @tweak[3],$inout3
1848 movdqu $inout1,16*1($out)
1849 xorps @tweak[4],$inout4
1850 movdqu $inout2,16*2($out)
1851 movdqu $inout3,16*3($out)
1852 movdqu $inout4,16*4($out)
1858 movups ($inp),$inout0
1860 xorps @tweak[0],$inout0
1862 &aesni_generate1("enc",$key,$rounds);
1864 xorps @tweak[0],$inout0
1865 movdqa @tweak[1],@tweak[0]
1866 movups $inout0,($out)
1872 movups ($inp),$inout0
1873 movups 16($inp),$inout1
1875 xorps @tweak[0],$inout0
1876 xorps @tweak[1],$inout1
1878 call _aesni_encrypt3
1880 xorps @tweak[0],$inout0
1881 movdqa @tweak[2],@tweak[0]
1882 xorps @tweak[1],$inout1
1883 movups $inout0,($out)
1884 movups $inout1,16*1($out)
1890 movups ($inp),$inout0
1891 movups 16*1($inp),$inout1
1892 movups 16*2($inp),$inout2
1894 xorps @tweak[0],$inout0
1895 xorps @tweak[1],$inout1
1896 xorps @tweak[2],$inout2
1898 call _aesni_encrypt3
1900 xorps @tweak[0],$inout0
1901 movdqa @tweak[3],@tweak[0]
1902 xorps @tweak[1],$inout1
1903 xorps @tweak[2],$inout2
1904 movups $inout0,($out)
1905 movups $inout1,16*1($out)
1906 movups $inout2,16*2($out)
1912 movups ($inp),$inout0
1913 movups 16*1($inp),$inout1
1914 movups 16*2($inp),$inout2
1915 xorps @tweak[0],$inout0
1916 movups 16*3($inp),$inout3
1918 xorps @tweak[1],$inout1
1919 xorps @tweak[2],$inout2
1920 xorps @tweak[3],$inout3
1922 call _aesni_encrypt4
1924 pxor @tweak[0],$inout0
1925 movdqa @tweak[4],@tweak[0]
1926 pxor @tweak[1],$inout1
1927 pxor @tweak[2],$inout2
1928 movdqu $inout0,($out)
1929 pxor @tweak[3],$inout3
1930 movdqu $inout1,16*1($out)
1931 movdqu $inout2,16*2($out)
1932 movdqu $inout3,16*3($out)
1943 movzb ($inp),%eax # borrow $rounds ...
1944 movzb -16($out),%ecx # ... and $key
1952 sub $len_,$out # rewind $out
1953 mov $key_,$key # restore $key
1954 mov $rnds_,$rounds # restore $rounds
1956 movups -16($out),$inout0
1957 xorps @tweak[0],$inout0
1959 &aesni_generate1("enc",$key,$rounds);
1961 xorps @tweak[0],$inout0
1962 movups $inout0,-16($out)
1966 $code.=<<___ if ($win64);
1967 movaps -0xa0(%rbp),%xmm6
1968 movaps -0x90(%rbp),%xmm7
1969 movaps -0x80(%rbp),%xmm8
1970 movaps -0x70(%rbp),%xmm9
1971 movaps -0x60(%rbp),%xmm10
1972 movaps -0x50(%rbp),%xmm11
1973 movaps -0x40(%rbp),%xmm12
1974 movaps -0x30(%rbp),%xmm13
1975 movaps -0x20(%rbp),%xmm14
1976 movaps -0x10(%rbp),%xmm15
1983 .size aesni_xts_encrypt,.-aesni_xts_encrypt
1987 .globl aesni_xts_decrypt
1988 .type aesni_xts_decrypt,\@function,6
1993 sub \$$frame_size,%rsp
1994 and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
1996 $code.=<<___ if ($win64);
1997 movaps %xmm6,-0xa8(%rax)
1998 movaps %xmm7,-0x98(%rax)
1999 movaps %xmm8,-0x88(%rax)
2000 movaps %xmm9,-0x78(%rax)
2001 movaps %xmm10,-0x68(%rax)
2002 movaps %xmm11,-0x58(%rax)
2003 movaps %xmm12,-0x48(%rax)
2004 movaps %xmm13,-0x38(%rax)
2005 movaps %xmm14,-0x28(%rax)
2006 movaps %xmm15,-0x18(%rax)
2011 movups ($ivp),$inout0 # load clear-text tweak
2012 mov 240($key2),$rounds # key2->rounds
2013 mov 240($key),$rnds_ # key1->rounds
2015 # generate the tweak
2016 &aesni_generate1("enc",$key2,$rounds,$inout0);
2018 xor %eax,%eax # if ($len%16) len-=16;
2024 $movkey ($key),$rndkey0 # zero round key
2025 mov $key,$key_ # backup $key
2026 mov $rnds_,$rounds # backup $rounds
2028 mov $len,$len_ # backup $len
2031 $movkey 16($key,$rnds_),$rndkey1 # last round key
2033 movdqa .Lxts_magic(%rip),$twmask
2034 movdqa $inout0,@tweak[5]
2035 pshufd \$0x5f,$inout0,$twres
2036 pxor $rndkey0,$rndkey1
2038 for ($i=0;$i<4;$i++) {
2040 movdqa $twres,$twtmp
2042 movdqa @tweak[5],@tweak[$i]
2043 psrad \$31,$twtmp # broadcast upper bits
2044 paddq @tweak[5],@tweak[5]
2046 pxor $rndkey0,@tweak[$i]
2047 pxor $twtmp,@tweak[5]
2051 movdqa @tweak[5],@tweak[4]
2053 paddq @tweak[5],@tweak[5]
2055 pxor $rndkey0,@tweak[4]
2056 pxor $twres,@tweak[5]
2057 movaps $rndkey1,0x60(%rsp) # save round[0]^round[last]
2063 lea 32($key_,$rnds_),$key # end of key schedule
2064 sub %r10,%rax # twisted $rounds
2065 $movkey 16($key_),$rndkey1
2066 mov %rax,%r10 # backup twisted $rounds
2067 lea .Lxts_magic(%rip),%r8
2068 jmp .Lxts_dec_grandloop
2071 .Lxts_dec_grandloop:
2072 movdqu `16*0`($inp),$inout0 # load input
2073 movdqa $rndkey0,$twmask
2074 movdqu `16*1`($inp),$inout1
2075 pxor @tweak[0],$inout0
2076 movdqu `16*2`($inp),$inout2
2077 pxor @tweak[1],$inout1
2078 aesdec $rndkey1,$inout0
2079 movdqu `16*3`($inp),$inout3
2080 pxor @tweak[2],$inout2
2081 aesdec $rndkey1,$inout1
2082 movdqu `16*4`($inp),$inout4
2083 pxor @tweak[3],$inout3
2084 aesdec $rndkey1,$inout2
2085 movdqu `16*5`($inp),$inout5
2086 pxor @tweak[5],$twmask # round[0]^=tweak[5]
2087 movdqa 0x60(%rsp),$twres # load round[0]^round[last]
2088 pxor @tweak[4],$inout4
2089 aesdec $rndkey1,$inout3
2090 $movkey 32($key_),$rndkey0
2091 lea `16*6`($inp),$inp
2092 pxor $twmask,$inout5
2094 pxor $twres,@tweak[0]
2095 aesdec $rndkey1,$inout4
2096 pxor $twres,@tweak[1]
2097 movdqa @tweak[0],`16*0`(%rsp) # put aside tweaks^last round key
2098 aesdec $rndkey1,$inout5
2099 $movkey 48($key_),$rndkey1
2100 pxor $twres,@tweak[2]
2102 aesdec $rndkey0,$inout0
2103 pxor $twres,@tweak[3]
2104 movdqa @tweak[1],`16*1`(%rsp)
2105 aesdec $rndkey0,$inout1
2106 pxor $twres,@tweak[4]
2107 movdqa @tweak[2],`16*2`(%rsp)
2108 aesdec $rndkey0,$inout2
2109 aesdec $rndkey0,$inout3
2111 movdqa @tweak[4],`16*4`(%rsp)
2112 aesdec $rndkey0,$inout4
2113 aesdec $rndkey0,$inout5
2114 $movkey 64($key_),$rndkey0
2115 movdqa $twmask,`16*5`(%rsp)
2116 pshufd \$0x5f,@tweak[5],$twres
2120 aesdec $rndkey1,$inout0
2121 aesdec $rndkey1,$inout1
2122 aesdec $rndkey1,$inout2
2123 aesdec $rndkey1,$inout3
2124 aesdec $rndkey1,$inout4
2125 aesdec $rndkey1,$inout5
2126 $movkey -64($key,%rax),$rndkey1
2129 aesdec $rndkey0,$inout0
2130 aesdec $rndkey0,$inout1
2131 aesdec $rndkey0,$inout2
2132 aesdec $rndkey0,$inout3
2133 aesdec $rndkey0,$inout4
2134 aesdec $rndkey0,$inout5
2135 $movkey -80($key,%rax),$rndkey0
2138 movdqa (%r8),$twmask
2139 movdqa $twres,$twtmp
2141 aesdec $rndkey1,$inout0
2142 paddq @tweak[5],@tweak[5]
2144 aesdec $rndkey1,$inout1
2146 $movkey ($key_),@tweak[0] # load round[0]
2147 aesdec $rndkey1,$inout2
2148 aesdec $rndkey1,$inout3
2149 aesdec $rndkey1,$inout4
2150 pxor $twtmp,@tweak[5]
2151 movaps @tweak[0],@tweak[1] # copy round[0]
2152 aesdec $rndkey1,$inout5
2153 $movkey -64($key),$rndkey1
2155 movdqa $twres,$twtmp
2156 aesdec $rndkey0,$inout0
2158 pxor @tweak[5],@tweak[0]
2159 aesdec $rndkey0,$inout1
2161 paddq @tweak[5],@tweak[5]
2162 aesdec $rndkey0,$inout2
2163 aesdec $rndkey0,$inout3
2165 movaps @tweak[1],@tweak[2]
2166 aesdec $rndkey0,$inout4
2167 pxor $twtmp,@tweak[5]
2168 movdqa $twres,$twtmp
2169 aesdec $rndkey0,$inout5
2170 $movkey -48($key),$rndkey0
2173 aesdec $rndkey1,$inout0
2174 pxor @tweak[5],@tweak[1]
2176 aesdec $rndkey1,$inout1
2177 paddq @tweak[5],@tweak[5]
2179 aesdec $rndkey1,$inout2
2180 aesdec $rndkey1,$inout3
2181 movdqa @tweak[3],`16*3`(%rsp)
2182 pxor $twtmp,@tweak[5]
2183 aesdec $rndkey1,$inout4
2184 movaps @tweak[2],@tweak[3]
2185 movdqa $twres,$twtmp
2186 aesdec $rndkey1,$inout5
2187 $movkey -32($key),$rndkey1
2190 aesdec $rndkey0,$inout0
2191 pxor @tweak[5],@tweak[2]
2193 aesdec $rndkey0,$inout1
2194 paddq @tweak[5],@tweak[5]
2196 aesdec $rndkey0,$inout2
2197 aesdec $rndkey0,$inout3
2198 aesdec $rndkey0,$inout4
2199 pxor $twtmp,@tweak[5]
2200 movaps @tweak[3],@tweak[4]
2201 aesdec $rndkey0,$inout5
2203 movdqa $twres,$rndkey0
2205 aesdec $rndkey1,$inout0
2206 pxor @tweak[5],@tweak[3]
2208 aesdec $rndkey1,$inout1
2209 paddq @tweak[5],@tweak[5]
2210 pand $twmask,$rndkey0
2211 aesdec $rndkey1,$inout2
2212 aesdec $rndkey1,$inout3
2213 pxor $rndkey0,@tweak[5]
2214 $movkey ($key_),$rndkey0
2215 aesdec $rndkey1,$inout4
2216 aesdec $rndkey1,$inout5
2217 $movkey 16($key_),$rndkey1
2219 pxor @tweak[5],@tweak[4]
2220 aesdeclast `16*0`(%rsp),$inout0
2222 paddq @tweak[5],@tweak[5]
2223 aesdeclast `16*1`(%rsp),$inout1
2224 aesdeclast `16*2`(%rsp),$inout2
2226 mov %r10,%rax # restore $rounds
2227 aesdeclast `16*3`(%rsp),$inout3
2228 aesdeclast `16*4`(%rsp),$inout4
2229 aesdeclast `16*5`(%rsp),$inout5
2230 pxor $twres,@tweak[5]
2232 lea `16*6`($out),$out
2233 movups $inout0,`-16*6`($out) # write output
2234 movups $inout1,`-16*5`($out)
2235 movups $inout2,`-16*4`($out)
2236 movups $inout3,`-16*3`($out)
2237 movups $inout4,`-16*2`($out)
2238 movups $inout5,`-16*1`($out)
2240 jnc .Lxts_dec_grandloop
2244 mov $key_,$key # restore $key
2245 shr \$4,$rounds # restore original value
2248 mov $rounds,$rnds_ # backup $rounds
2249 pxor $rndkey0,@tweak[0]
2250 pxor $rndkey0,@tweak[1]
2254 pxor $rndkey0,@tweak[2]
2257 pxor $rndkey0,@tweak[3]
2260 pxor $rndkey0,@tweak[4]
2265 movdqu ($inp),$inout0
2266 movdqu 16*1($inp),$inout1
2267 movdqu 16*2($inp),$inout2
2268 pxor @tweak[0],$inout0
2269 movdqu 16*3($inp),$inout3
2270 pxor @tweak[1],$inout1
2271 movdqu 16*4($inp),$inout4
2273 pxor @tweak[2],$inout2
2274 pxor @tweak[3],$inout3
2275 pxor @tweak[4],$inout4
2277 call _aesni_decrypt6
2279 xorps @tweak[0],$inout0
2280 xorps @tweak[1],$inout1
2281 xorps @tweak[2],$inout2
2282 movdqu $inout0,($out)
2283 xorps @tweak[3],$inout3
2284 movdqu $inout1,16*1($out)
2285 xorps @tweak[4],$inout4
2286 movdqu $inout2,16*2($out)
2288 movdqu $inout3,16*3($out)
2289 pcmpgtd @tweak[5],$twtmp
2290 movdqu $inout4,16*4($out)
2292 pshufd \$0x13,$twtmp,@tweak[1] # $twres
2296 movdqa @tweak[5],@tweak[0]
2297 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
2298 pand $twmask,@tweak[1] # isolate carry and residue
2299 pxor @tweak[5],@tweak[1]
2304 movups ($inp),$inout0
2306 xorps @tweak[0],$inout0
2308 &aesni_generate1("dec",$key,$rounds);
2310 xorps @tweak[0],$inout0
2311 movdqa @tweak[1],@tweak[0]
2312 movups $inout0,($out)
2313 movdqa @tweak[2],@tweak[1]
2319 movups ($inp),$inout0
2320 movups 16($inp),$inout1
2322 xorps @tweak[0],$inout0
2323 xorps @tweak[1],$inout1
2325 call _aesni_decrypt3
2327 xorps @tweak[0],$inout0
2328 movdqa @tweak[2],@tweak[0]
2329 xorps @tweak[1],$inout1
2330 movdqa @tweak[3],@tweak[1]
2331 movups $inout0,($out)
2332 movups $inout1,16*1($out)
2338 movups ($inp),$inout0
2339 movups 16*1($inp),$inout1
2340 movups 16*2($inp),$inout2
2342 xorps @tweak[0],$inout0
2343 xorps @tweak[1],$inout1
2344 xorps @tweak[2],$inout2
2346 call _aesni_decrypt3
2348 xorps @tweak[0],$inout0
2349 movdqa @tweak[3],@tweak[0]
2350 xorps @tweak[1],$inout1
2351 movdqa @tweak[4],@tweak[1]
2352 xorps @tweak[2],$inout2
2353 movups $inout0,($out)
2354 movups $inout1,16*1($out)
2355 movups $inout2,16*2($out)
2361 movups ($inp),$inout0
2362 movups 16*1($inp),$inout1
2363 movups 16*2($inp),$inout2
2364 xorps @tweak[0],$inout0
2365 movups 16*3($inp),$inout3
2367 xorps @tweak[1],$inout1
2368 xorps @tweak[2],$inout2
2369 xorps @tweak[3],$inout3
2371 call _aesni_decrypt4
2373 pxor @tweak[0],$inout0
2374 movdqa @tweak[4],@tweak[0]
2375 pxor @tweak[1],$inout1
2376 movdqa @tweak[5],@tweak[1]
2377 pxor @tweak[2],$inout2
2378 movdqu $inout0,($out)
2379 pxor @tweak[3],$inout3
2380 movdqu $inout1,16*1($out)
2381 movdqu $inout2,16*2($out)
2382 movdqu $inout3,16*3($out)
2392 mov $key_,$key # restore $key
2393 mov $rnds_,$rounds # restore $rounds
2395 movups ($inp),$inout0
2396 xorps @tweak[1],$inout0
2398 &aesni_generate1("dec",$key,$rounds);
2400 xorps @tweak[1],$inout0
2401 movups $inout0,($out)
2404 movzb 16($inp),%eax # borrow $rounds ...
2405 movzb ($out),%ecx # ... and $key
2413 sub $len_,$out # rewind $out
2414 mov $key_,$key # restore $key
2415 mov $rnds_,$rounds # restore $rounds
2417 movups ($out),$inout0
2418 xorps @tweak[0],$inout0
2420 &aesni_generate1("dec",$key,$rounds);
2422 xorps @tweak[0],$inout0
2423 movups $inout0,($out)
2427 $code.=<<___ if ($win64);
2428 movaps -0xa0(%rbp),%xmm6
2429 movaps -0x90(%rbp),%xmm7
2430 movaps -0x80(%rbp),%xmm8
2431 movaps -0x70(%rbp),%xmm9
2432 movaps -0x60(%rbp),%xmm10
2433 movaps -0x50(%rbp),%xmm11
2434 movaps -0x40(%rbp),%xmm12
2435 movaps -0x30(%rbp),%xmm13
2436 movaps -0x20(%rbp),%xmm14
2437 movaps -0x10(%rbp),%xmm15
2444 .size aesni_xts_decrypt,.-aesni_xts_decrypt
2448 ########################################################################
2449 # void $PREFIX_cbc_encrypt (const void *inp, void *out,
2450 # size_t length, const AES_KEY *key,
2451 # unsigned char *ivp,const int enc);
2453 my $frame_size = 0x10 + ($win64?0xa0:0); # used in decrypt
2454 my ($iv,$in0,$in1,$in2,$in3,$in4)=map("%xmm$_",(10..15));
2458 .globl ${PREFIX}_cbc_encrypt
2459 .type ${PREFIX}_cbc_encrypt,\@function,6
2461 ${PREFIX}_cbc_encrypt:
2462 test $len,$len # check length
2465 mov 240($key),$rnds_ # key->rounds
2466 mov $key,$key_ # backup $key
2467 test %r9d,%r9d # 6th argument
2469 #--------------------------- CBC ENCRYPT ------------------------------#
2470 movups ($ivp),$inout0 # load iv as initial state
2478 movups ($inp),$inout1 # load input
2480 #xorps $inout1,$inout0
2482 &aesni_generate1("enc",$key,$rounds,$inout0,$inout1);
2484 mov $rnds_,$rounds # restore $rounds
2485 mov $key_,$key # restore $key
2486 movups $inout0,0($out) # store output
2492 movups $inout0,($ivp)
2496 mov $len,%rcx # zaps $key
2497 xchg $inp,$out # $inp is %rsi and $out is %rdi now
2498 .long 0x9066A4F3 # rep movsb
2499 mov \$16,%ecx # zero tail
2502 .long 0x9066AAF3 # rep stosb
2503 lea -16(%rdi),%rdi # rewind $out by 1 block
2504 mov $rnds_,$rounds # restore $rounds
2505 mov %rdi,%rsi # $inp and $out are the same
2506 mov $key_,$key # restore $key
2507 xor $len,$len # len=16
2508 jmp .Lcbc_enc_loop # one more spin
2509 \f#--------------------------- CBC DECRYPT ------------------------------#
2514 sub \$$frame_size,%rsp
2515 and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
2517 $code.=<<___ if ($win64);
2518 movaps %xmm6,0x10(%rsp)
2519 movaps %xmm7,0x20(%rsp)
2520 movaps %xmm8,0x30(%rsp)
2521 movaps %xmm9,0x40(%rsp)
2522 movaps %xmm10,0x50(%rsp)
2523 movaps %xmm11,0x60(%rsp)
2524 movaps %xmm12,0x70(%rsp)
2525 movaps %xmm13,0x80(%rsp)
2526 movaps %xmm14,0x90(%rsp)
2527 movaps %xmm15,0xa0(%rsp)
2537 $movkey ($key),$rndkey0
2538 movdqu 0x00($inp),$inout0 # load input
2539 movdqu 0x10($inp),$inout1
2541 movdqu 0x20($inp),$inout2
2543 movdqu 0x30($inp),$inout3
2545 movdqu 0x40($inp),$inout4
2547 movdqu 0x50($inp),$inout5
2549 mov OPENSSL_ia32cap_P+4(%rip),%r9d
2551 jbe .Lcbc_dec_six_or_seven
2553 and \$`1<<26|1<<22`,%r9d # isolate XSAVE+MOVBE
2555 cmp \$`1<<22`,%r9d # check for MOVBE without XSAVE
2556 je .Lcbc_dec_loop6_enter
2558 lea 0x70($key),$key # size optimization
2559 jmp .Lcbc_dec_loop8_enter
2562 movups $inout7,($out)
2564 .Lcbc_dec_loop8_enter:
2565 movdqu 0x60($inp),$inout6
2566 pxor $rndkey0,$inout0
2567 movdqu 0x70($inp),$inout7
2568 pxor $rndkey0,$inout1
2569 $movkey 0x10-0x70($key),$rndkey1
2570 pxor $rndkey0,$inout2
2572 cmp \$0x70,$len # is there at least 0x60 bytes ahead?
2573 pxor $rndkey0,$inout3
2574 pxor $rndkey0,$inout4
2575 pxor $rndkey0,$inout5
2576 pxor $rndkey0,$inout6
2578 aesdec $rndkey1,$inout0
2579 pxor $rndkey0,$inout7
2580 $movkey 0x20-0x70($key),$rndkey0
2581 aesdec $rndkey1,$inout1
2582 aesdec $rndkey1,$inout2
2583 aesdec $rndkey1,$inout3
2584 aesdec $rndkey1,$inout4
2585 aesdec $rndkey1,$inout5
2586 aesdec $rndkey1,$inout6
2589 aesdec $rndkey1,$inout7
2591 $movkey 0x30-0x70($key),$rndkey1
2593 for($i=1;$i<12;$i++) {
2594 my $rndkeyx = ($i&1)?$rndkey0:$rndkey1;
2595 $code.=<<___ if ($i==7);
2599 aesdec $rndkeyx,$inout0
2600 aesdec $rndkeyx,$inout1
2601 aesdec $rndkeyx,$inout2
2602 aesdec $rndkeyx,$inout3
2603 aesdec $rndkeyx,$inout4
2604 aesdec $rndkeyx,$inout5
2605 aesdec $rndkeyx,$inout6
2606 aesdec $rndkeyx,$inout7
2607 $movkey `0x30+0x10*$i`-0x70($key),$rndkeyx
2609 $code.=<<___ if ($i<6 || (!($i&1) && $i>7));
2612 $code.=<<___ if ($i==7);
2615 $code.=<<___ if ($i==9);
2618 $code.=<<___ if ($i==11);
2625 aesdec $rndkey1,$inout0
2626 aesdec $rndkey1,$inout1
2629 aesdec $rndkey1,$inout2
2630 aesdec $rndkey1,$inout3
2633 aesdec $rndkey1,$inout4
2634 aesdec $rndkey1,$inout5
2637 aesdec $rndkey1,$inout6
2638 aesdec $rndkey1,$inout7
2639 movdqu 0x50($inp),$rndkey1
2641 aesdeclast $iv,$inout0
2642 movdqu 0x60($inp),$iv # borrow $iv
2643 pxor $rndkey0,$rndkey1
2644 aesdeclast $in0,$inout1
2646 movdqu 0x70($inp),$rndkey0 # next IV
2647 aesdeclast $in1,$inout2
2649 movdqu 0x00($inp_),$in0
2650 aesdeclast $in2,$inout3
2651 aesdeclast $in3,$inout4
2652 movdqu 0x10($inp_),$in1
2653 movdqu 0x20($inp_),$in2
2654 aesdeclast $in4,$inout5
2655 aesdeclast $rndkey1,$inout6
2656 movdqu 0x30($inp_),$in3
2657 movdqu 0x40($inp_),$in4
2658 aesdeclast $iv,$inout7
2659 movdqa $rndkey0,$iv # return $iv
2660 movdqu 0x50($inp_),$rndkey1
2661 $movkey -0x70($key),$rndkey0
2663 movups $inout0,($out) # store output
2665 movups $inout1,0x10($out)
2667 movups $inout2,0x20($out)
2669 movups $inout3,0x30($out)
2671 movups $inout4,0x40($out)
2673 movups $inout5,0x50($out)
2674 movdqa $rndkey1,$inout5
2675 movups $inout6,0x60($out)
2681 movaps $inout7,$inout0
2682 lea -0x70($key),$key
2684 jle .Lcbc_dec_tail_collected
2685 movups $inout7,($out)
2691 .Lcbc_dec_six_or_seven:
2695 movaps $inout5,$inout6
2696 call _aesni_decrypt6
2697 pxor $iv,$inout0 # ^= IV
2700 movdqu $inout0,($out)
2702 movdqu $inout1,0x10($out)
2704 movdqu $inout2,0x20($out)
2706 movdqu $inout3,0x30($out)
2708 movdqu $inout4,0x40($out)
2710 movdqa $inout5,$inout0
2711 jmp .Lcbc_dec_tail_collected
2715 movups 0x60($inp),$inout6
2716 xorps $inout7,$inout7
2717 call _aesni_decrypt8
2718 movups 0x50($inp),$inout7
2719 pxor $iv,$inout0 # ^= IV
2720 movups 0x60($inp),$iv
2722 movdqu $inout0,($out)
2724 movdqu $inout1,0x10($out)
2726 movdqu $inout2,0x20($out)
2728 movdqu $inout3,0x30($out)
2730 movdqu $inout4,0x40($out)
2731 pxor $inout7,$inout6
2732 movdqu $inout5,0x50($out)
2734 movdqa $inout6,$inout0
2735 jmp .Lcbc_dec_tail_collected
2739 movups $inout5,($out)
2741 movdqu 0x00($inp),$inout0 # load input
2742 movdqu 0x10($inp),$inout1
2744 movdqu 0x20($inp),$inout2
2746 movdqu 0x30($inp),$inout3
2748 movdqu 0x40($inp),$inout4
2750 movdqu 0x50($inp),$inout5
2752 .Lcbc_dec_loop6_enter:
2754 movdqa $inout5,$inout6
2756 call _aesni_decrypt6
2758 pxor $iv,$inout0 # ^= IV
2761 movdqu $inout0,($out)
2763 movdqu $inout1,0x10($out)
2765 movdqu $inout2,0x20($out)
2768 movdqu $inout3,0x30($out)
2771 movdqu $inout4,0x40($out)
2776 movdqa $inout5,$inout0
2778 jle .Lcbc_dec_tail_collected
2779 movups $inout5,($out)
2783 movups ($inp),$inout0
2787 movups 0x10($inp),$inout1
2792 movups 0x20($inp),$inout2
2797 movups 0x30($inp),$inout3
2802 movups 0x40($inp),$inout4
2805 xorps $inout5,$inout5
2806 call _aesni_decrypt6
2810 movdqu $inout0,($out)
2812 movdqu $inout1,0x10($out)
2814 movdqu $inout2,0x20($out)
2816 movdqu $inout3,0x30($out)
2818 movdqa $inout4,$inout0
2820 jmp .Lcbc_dec_tail_collected
2826 &aesni_generate1("dec",$key,$rounds);
2830 jmp .Lcbc_dec_tail_collected
2834 xorps $inout2,$inout2
2835 call _aesni_decrypt3
2839 movdqu $inout0,($out)
2840 movdqa $inout1,$inout0
2842 jmp .Lcbc_dec_tail_collected
2846 call _aesni_decrypt3
2850 movdqu $inout0,($out)
2852 movdqu $inout1,0x10($out)
2853 movdqa $inout2,$inout0
2855 jmp .Lcbc_dec_tail_collected
2859 call _aesni_decrypt4
2863 movdqu $inout0,($out)
2865 movdqu $inout1,0x10($out)
2867 movdqu $inout2,0x20($out)
2868 movdqa $inout3,$inout0
2870 jmp .Lcbc_dec_tail_collected
2873 .Lcbc_dec_tail_collected:
2876 jnz .Lcbc_dec_tail_partial
2877 movups $inout0,($out)
2880 .Lcbc_dec_tail_partial:
2881 movaps $inout0,(%rsp)
2886 .long 0x9066A4F3 # rep movsb
2890 $code.=<<___ if ($win64);
2891 movaps 0x10(%rsp),%xmm6
2892 movaps 0x20(%rsp),%xmm7
2893 movaps 0x30(%rsp),%xmm8
2894 movaps 0x40(%rsp),%xmm9
2895 movaps 0x50(%rsp),%xmm10
2896 movaps 0x60(%rsp),%xmm11
2897 movaps 0x70(%rsp),%xmm12
2898 movaps 0x80(%rsp),%xmm13
2899 movaps 0x90(%rsp),%xmm14
2900 movaps 0xa0(%rsp),%xmm15
2907 .size ${PREFIX}_cbc_encrypt,.-${PREFIX}_cbc_encrypt
2910 # int $PREFIX_set_[en|de]crypt_key (const unsigned char *userKey,
2911 # int bits, AES_KEY *key)
2912 { my ($inp,$bits,$key) = @_4args;
2916 .globl ${PREFIX}_set_decrypt_key
2917 .type ${PREFIX}_set_decrypt_key,\@abi-omnipotent
2919 ${PREFIX}_set_decrypt_key:
2920 .byte 0x48,0x83,0xEC,0x08 # sub rsp,8
2921 call __aesni_set_encrypt_key
2922 shl \$4,$bits # rounds-1 after _aesni_set_encrypt_key
2925 lea 16($key,$bits),$inp # points at the end of key schedule
2927 $movkey ($key),%xmm0 # just swap
2928 $movkey ($inp),%xmm1
2929 $movkey %xmm0,($inp)
2930 $movkey %xmm1,($key)
2935 $movkey ($key),%xmm0 # swap and inverse
2936 $movkey ($inp),%xmm1
2941 $movkey %xmm0,16($inp)
2942 $movkey %xmm1,-16($key)
2944 ja .Ldec_key_inverse
2946 $movkey ($key),%xmm0 # inverse middle
2948 $movkey %xmm0,($inp)
2952 .LSEH_end_set_decrypt_key:
2953 .size ${PREFIX}_set_decrypt_key,.-${PREFIX}_set_decrypt_key
2956 # This is based on submission by
2958 # Huang Ying <ying.huang@intel.com>
2959 # Vinodh Gopal <vinodh.gopal@intel.com>
2962 # Agressively optimized in respect to aeskeygenassist's critical path
2963 # and is contained in %xmm0-5 to meet Win64 ABI requirement.
2966 .globl ${PREFIX}_set_encrypt_key
2967 .type ${PREFIX}_set_encrypt_key,\@abi-omnipotent
2969 ${PREFIX}_set_encrypt_key:
2970 __aesni_set_encrypt_key:
2971 .byte 0x48,0x83,0xEC,0x08 # sub rsp,8
2978 movups ($inp),%xmm0 # pull first 128 bits of *userKey
2979 xorps %xmm4,%xmm4 # low dword of xmm4 is assumed 0
2989 mov \$9,$bits # 10 rounds for 128-bit key
2990 $movkey %xmm0,($key) # round 0
2991 aeskeygenassist \$0x1,%xmm0,%xmm1 # round 1
2992 call .Lkey_expansion_128_cold
2993 aeskeygenassist \$0x2,%xmm0,%xmm1 # round 2
2994 call .Lkey_expansion_128
2995 aeskeygenassist \$0x4,%xmm0,%xmm1 # round 3
2996 call .Lkey_expansion_128
2997 aeskeygenassist \$0x8,%xmm0,%xmm1 # round 4
2998 call .Lkey_expansion_128
2999 aeskeygenassist \$0x10,%xmm0,%xmm1 # round 5
3000 call .Lkey_expansion_128
3001 aeskeygenassist \$0x20,%xmm0,%xmm1 # round 6
3002 call .Lkey_expansion_128
3003 aeskeygenassist \$0x40,%xmm0,%xmm1 # round 7
3004 call .Lkey_expansion_128
3005 aeskeygenassist \$0x80,%xmm0,%xmm1 # round 8
3006 call .Lkey_expansion_128
3007 aeskeygenassist \$0x1b,%xmm0,%xmm1 # round 9
3008 call .Lkey_expansion_128
3009 aeskeygenassist \$0x36,%xmm0,%xmm1 # round 10
3010 call .Lkey_expansion_128
3011 $movkey %xmm0,(%rax)
3012 mov $bits,80(%rax) # 240(%rdx)
3018 movq 16($inp),%xmm2 # remaining 1/3 of *userKey
3019 mov \$11,$bits # 12 rounds for 192
3020 $movkey %xmm0,($key) # round 0
3021 aeskeygenassist \$0x1,%xmm2,%xmm1 # round 1,2
3022 call .Lkey_expansion_192a_cold
3023 aeskeygenassist \$0x2,%xmm2,%xmm1 # round 2,3
3024 call .Lkey_expansion_192b
3025 aeskeygenassist \$0x4,%xmm2,%xmm1 # round 4,5
3026 call .Lkey_expansion_192a
3027 aeskeygenassist \$0x8,%xmm2,%xmm1 # round 5,6
3028 call .Lkey_expansion_192b
3029 aeskeygenassist \$0x10,%xmm2,%xmm1 # round 7,8
3030 call .Lkey_expansion_192a
3031 aeskeygenassist \$0x20,%xmm2,%xmm1 # round 8,9
3032 call .Lkey_expansion_192b
3033 aeskeygenassist \$0x40,%xmm2,%xmm1 # round 10,11
3034 call .Lkey_expansion_192a
3035 aeskeygenassist \$0x80,%xmm2,%xmm1 # round 11,12
3036 call .Lkey_expansion_192b
3037 $movkey %xmm0,(%rax)
3038 mov $bits,48(%rax) # 240(%rdx)
3044 movups 16($inp),%xmm2 # remaning half of *userKey
3045 mov \$13,$bits # 14 rounds for 256
3047 $movkey %xmm0,($key) # round 0
3048 $movkey %xmm2,16($key) # round 1
3049 aeskeygenassist \$0x1,%xmm2,%xmm1 # round 2
3050 call .Lkey_expansion_256a_cold
3051 aeskeygenassist \$0x1,%xmm0,%xmm1 # round 3
3052 call .Lkey_expansion_256b
3053 aeskeygenassist \$0x2,%xmm2,%xmm1 # round 4
3054 call .Lkey_expansion_256a
3055 aeskeygenassist \$0x2,%xmm0,%xmm1 # round 5
3056 call .Lkey_expansion_256b
3057 aeskeygenassist \$0x4,%xmm2,%xmm1 # round 6
3058 call .Lkey_expansion_256a
3059 aeskeygenassist \$0x4,%xmm0,%xmm1 # round 7
3060 call .Lkey_expansion_256b
3061 aeskeygenassist \$0x8,%xmm2,%xmm1 # round 8
3062 call .Lkey_expansion_256a
3063 aeskeygenassist \$0x8,%xmm0,%xmm1 # round 9
3064 call .Lkey_expansion_256b
3065 aeskeygenassist \$0x10,%xmm2,%xmm1 # round 10
3066 call .Lkey_expansion_256a
3067 aeskeygenassist \$0x10,%xmm0,%xmm1 # round 11
3068 call .Lkey_expansion_256b
3069 aeskeygenassist \$0x20,%xmm2,%xmm1 # round 12
3070 call .Lkey_expansion_256a
3071 aeskeygenassist \$0x20,%xmm0,%xmm1 # round 13
3072 call .Lkey_expansion_256b
3073 aeskeygenassist \$0x40,%xmm2,%xmm1 # round 14
3074 call .Lkey_expansion_256a
3075 $movkey %xmm0,(%rax)
3076 mov $bits,16(%rax) # 240(%rdx)
3086 .LSEH_end_set_encrypt_key:
3089 .Lkey_expansion_128:
3090 $movkey %xmm0,(%rax)
3092 .Lkey_expansion_128_cold:
3093 shufps \$0b00010000,%xmm0,%xmm4
3095 shufps \$0b10001100,%xmm0,%xmm4
3097 shufps \$0b11111111,%xmm1,%xmm1 # critical path
3102 .Lkey_expansion_192a:
3103 $movkey %xmm0,(%rax)
3105 .Lkey_expansion_192a_cold:
3107 .Lkey_expansion_192b_warm:
3108 shufps \$0b00010000,%xmm0,%xmm4
3111 shufps \$0b10001100,%xmm0,%xmm4
3114 pshufd \$0b01010101,%xmm1,%xmm1 # critical path
3117 pshufd \$0b11111111,%xmm0,%xmm3
3122 .Lkey_expansion_192b:
3124 shufps \$0b01000100,%xmm0,%xmm5
3125 $movkey %xmm5,(%rax)
3126 shufps \$0b01001110,%xmm2,%xmm3
3127 $movkey %xmm3,16(%rax)
3129 jmp .Lkey_expansion_192b_warm
3132 .Lkey_expansion_256a:
3133 $movkey %xmm2,(%rax)
3135 .Lkey_expansion_256a_cold:
3136 shufps \$0b00010000,%xmm0,%xmm4
3138 shufps \$0b10001100,%xmm0,%xmm4
3140 shufps \$0b11111111,%xmm1,%xmm1 # critical path
3145 .Lkey_expansion_256b:
3146 $movkey %xmm0,(%rax)
3149 shufps \$0b00010000,%xmm2,%xmm4
3151 shufps \$0b10001100,%xmm2,%xmm4
3153 shufps \$0b10101010,%xmm1,%xmm1 # critical path
3156 .size ${PREFIX}_set_encrypt_key,.-${PREFIX}_set_encrypt_key
3157 .size __aesni_set_encrypt_key,.-__aesni_set_encrypt_key
3164 .byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
3172 .byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
3174 .asciz "AES for Intel AES-NI, CRYPTOGAMS by <appro\@openssl.org>"
3178 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
3179 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
3187 .extern __imp_RtlVirtualUnwind
3189 $code.=<<___ if ($PREFIX eq "aesni");
3190 .type ecb_se_handler,\@abi-omnipotent
3204 mov 152($context),%rax # pull context->Rsp
3206 jmp .Lcommon_seh_tail
3207 .size ecb_se_handler,.-ecb_se_handler
3209 .type ccm64_se_handler,\@abi-omnipotent
3223 mov 120($context),%rax # pull context->Rax
3224 mov 248($context),%rbx # pull context->Rip
3226 mov 8($disp),%rsi # disp->ImageBase
3227 mov 56($disp),%r11 # disp->HandlerData
3229 mov 0(%r11),%r10d # HandlerData[0]
3230 lea (%rsi,%r10),%r10 # prologue label
3231 cmp %r10,%rbx # context->Rip<prologue label
3232 jb .Lcommon_seh_tail
3234 mov 152($context),%rax # pull context->Rsp
3236 mov 4(%r11),%r10d # HandlerData[1]
3237 lea (%rsi,%r10),%r10 # epilogue label
3238 cmp %r10,%rbx # context->Rip>=epilogue label
3239 jae .Lcommon_seh_tail
3241 lea 0(%rax),%rsi # %xmm save area
3242 lea 512($context),%rdi # &context.Xmm6
3243 mov \$8,%ecx # 4*sizeof(%xmm0)/sizeof(%rax)
3244 .long 0xa548f3fc # cld; rep movsq
3245 lea 0x58(%rax),%rax # adjust stack pointer
3247 jmp .Lcommon_seh_tail
3248 .size ccm64_se_handler,.-ccm64_se_handler
3250 .type ctr_xts_se_handler,\@abi-omnipotent
3264 mov 120($context),%rax # pull context->Rax
3265 mov 248($context),%rbx # pull context->Rip
3267 mov 8($disp),%rsi # disp->ImageBase
3268 mov 56($disp),%r11 # disp->HandlerData
3270 mov 0(%r11),%r10d # HandlerData[0]
3271 lea (%rsi,%r10),%r10 # prologue lable
3272 cmp %r10,%rbx # context->Rip<prologue label
3273 jb .Lcommon_seh_tail
3275 mov 152($context),%rax # pull context->Rsp
3277 mov 4(%r11),%r10d # HandlerData[1]
3278 lea (%rsi,%r10),%r10 # epilogue label
3279 cmp %r10,%rbx # context->Rip>=epilogue label
3280 jae .Lcommon_seh_tail
3282 mov 160($context),%rax # pull context->Rbp
3283 lea -0xa0(%rax),%rsi # %xmm save area
3284 lea 512($context),%rdi # & context.Xmm6
3285 mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
3286 .long 0xa548f3fc # cld; rep movsq
3288 jmp .Lcommon_rbp_tail
3289 .size ctr_xts_se_handler,.-ctr_xts_se_handler
3292 .type cbc_se_handler,\@abi-omnipotent
3306 mov 152($context),%rax # pull context->Rsp
3307 mov 248($context),%rbx # pull context->Rip
3309 lea .Lcbc_decrypt(%rip),%r10
3310 cmp %r10,%rbx # context->Rip<"prologue" label
3311 jb .Lcommon_seh_tail
3313 lea .Lcbc_decrypt_body(%rip),%r10
3314 cmp %r10,%rbx # context->Rip<cbc_decrypt_body
3315 jb .Lrestore_cbc_rax
3317 lea .Lcbc_ret(%rip),%r10
3318 cmp %r10,%rbx # context->Rip>="epilogue" label
3319 jae .Lcommon_seh_tail
3321 lea 16(%rax),%rsi # %xmm save area
3322 lea 512($context),%rdi # &context.Xmm6
3323 mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
3324 .long 0xa548f3fc # cld; rep movsq
3327 mov 160($context),%rax # pull context->Rbp
3328 mov (%rax),%rbp # restore saved %rbp
3329 lea 8(%rax),%rax # adjust stack pointer
3330 mov %rbp,160($context) # restore context->Rbp
3331 jmp .Lcommon_seh_tail
3334 mov 120($context),%rax
3339 mov %rax,152($context) # restore context->Rsp
3340 mov %rsi,168($context) # restore context->Rsi
3341 mov %rdi,176($context) # restore context->Rdi
3343 mov 40($disp),%rdi # disp->ContextRecord