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 originally made no sense to implement 2x subroutine.
292 # But times change and it became appropriate to spend extra 192 bytes
293 # on 2x subroutine on Atom Silvermont account. For processors that
294 # can schedule aes[enc|dec] every cycle optimal interleave factor
295 # equals to corresponding instructions latency. 8x is optimal for
296 # * Bridge and "super-optimal" for other Intel CPUs...
298 sub aesni_generate2 {
300 # As already mentioned it takes in $key and $rounds, which are *not*
301 # preserved. $inout[0-1] is cipher/clear text...
303 .type _aesni_${dir}rypt2,\@abi-omnipotent
306 $movkey ($key),$rndkey0
308 $movkey 16($key),$rndkey1
309 xorps $rndkey0,$inout0
310 xorps $rndkey0,$inout1
311 $movkey 32($key),$rndkey0
312 lea 32($key,$rounds),$key
317 aes${dir} $rndkey1,$inout0
318 aes${dir} $rndkey1,$inout1
319 $movkey ($key,%rax),$rndkey1
321 aes${dir} $rndkey0,$inout0
322 aes${dir} $rndkey0,$inout1
323 $movkey -16($key,%rax),$rndkey0
326 aes${dir} $rndkey1,$inout0
327 aes${dir} $rndkey1,$inout1
328 aes${dir}last $rndkey0,$inout0
329 aes${dir}last $rndkey0,$inout1
331 .size _aesni_${dir}rypt2,.-_aesni_${dir}rypt2
334 sub aesni_generate3 {
336 # As already mentioned it takes in $key and $rounds, which are *not*
337 # preserved. $inout[0-2] is cipher/clear text...
339 .type _aesni_${dir}rypt3,\@abi-omnipotent
342 $movkey ($key),$rndkey0
344 $movkey 16($key),$rndkey1
345 xorps $rndkey0,$inout0
346 xorps $rndkey0,$inout1
347 xorps $rndkey0,$inout2
348 $movkey 32($key),$rndkey0
349 lea 32($key,$rounds),$key
354 aes${dir} $rndkey1,$inout0
355 aes${dir} $rndkey1,$inout1
356 aes${dir} $rndkey1,$inout2
357 $movkey ($key,%rax),$rndkey1
359 aes${dir} $rndkey0,$inout0
360 aes${dir} $rndkey0,$inout1
361 aes${dir} $rndkey0,$inout2
362 $movkey -16($key,%rax),$rndkey0
365 aes${dir} $rndkey1,$inout0
366 aes${dir} $rndkey1,$inout1
367 aes${dir} $rndkey1,$inout2
368 aes${dir}last $rndkey0,$inout0
369 aes${dir}last $rndkey0,$inout1
370 aes${dir}last $rndkey0,$inout2
372 .size _aesni_${dir}rypt3,.-_aesni_${dir}rypt3
375 # 4x interleave is implemented to improve small block performance,
376 # most notably [and naturally] 4 block by ~30%. One can argue that one
377 # should have implemented 5x as well, but improvement would be <20%,
378 # so it's not worth it...
379 sub aesni_generate4 {
381 # As already mentioned it takes in $key and $rounds, which are *not*
382 # preserved. $inout[0-3] is cipher/clear text...
384 .type _aesni_${dir}rypt4,\@abi-omnipotent
387 $movkey ($key),$rndkey0
389 $movkey 16($key),$rndkey1
390 xorps $rndkey0,$inout0
391 xorps $rndkey0,$inout1
392 xorps $rndkey0,$inout2
393 xorps $rndkey0,$inout3
394 $movkey 32($key),$rndkey0
395 lea 32($key,$rounds),$key
401 aes${dir} $rndkey1,$inout0
402 aes${dir} $rndkey1,$inout1
403 aes${dir} $rndkey1,$inout2
404 aes${dir} $rndkey1,$inout3
405 $movkey ($key,%rax),$rndkey1
407 aes${dir} $rndkey0,$inout0
408 aes${dir} $rndkey0,$inout1
409 aes${dir} $rndkey0,$inout2
410 aes${dir} $rndkey0,$inout3
411 $movkey -16($key,%rax),$rndkey0
414 aes${dir} $rndkey1,$inout0
415 aes${dir} $rndkey1,$inout1
416 aes${dir} $rndkey1,$inout2
417 aes${dir} $rndkey1,$inout3
418 aes${dir}last $rndkey0,$inout0
419 aes${dir}last $rndkey0,$inout1
420 aes${dir}last $rndkey0,$inout2
421 aes${dir}last $rndkey0,$inout3
423 .size _aesni_${dir}rypt4,.-_aesni_${dir}rypt4
426 sub aesni_generate6 {
428 # As already mentioned it takes in $key and $rounds, which are *not*
429 # preserved. $inout[0-5] is cipher/clear text...
431 .type _aesni_${dir}rypt6,\@abi-omnipotent
434 $movkey ($key),$rndkey0
436 $movkey 16($key),$rndkey1
437 xorps $rndkey0,$inout0
438 pxor $rndkey0,$inout1
439 pxor $rndkey0,$inout2
440 aes${dir} $rndkey1,$inout0
441 lea 32($key,$rounds),$key
443 aes${dir} $rndkey1,$inout1
444 pxor $rndkey0,$inout3
445 pxor $rndkey0,$inout4
446 aes${dir} $rndkey1,$inout2
447 pxor $rndkey0,$inout5
449 aes${dir} $rndkey1,$inout3
450 aes${dir} $rndkey1,$inout4
451 aes${dir} $rndkey1,$inout5
452 $movkey -16($key,%rax),$rndkey0
453 jmp .L${dir}_loop6_enter
456 aes${dir} $rndkey1,$inout0
457 aes${dir} $rndkey1,$inout1
458 aes${dir} $rndkey1,$inout2
459 aes${dir} $rndkey1,$inout3
460 aes${dir} $rndkey1,$inout4
461 aes${dir} $rndkey1,$inout5
462 .L${dir}_loop6_enter:
463 $movkey ($key,%rax),$rndkey1
465 aes${dir} $rndkey0,$inout0
466 aes${dir} $rndkey0,$inout1
467 aes${dir} $rndkey0,$inout2
468 aes${dir} $rndkey0,$inout3
469 aes${dir} $rndkey0,$inout4
470 aes${dir} $rndkey0,$inout5
471 $movkey -16($key,%rax),$rndkey0
474 aes${dir} $rndkey1,$inout0
475 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}last $rndkey0,$inout0
481 aes${dir}last $rndkey0,$inout1
482 aes${dir}last $rndkey0,$inout2
483 aes${dir}last $rndkey0,$inout3
484 aes${dir}last $rndkey0,$inout4
485 aes${dir}last $rndkey0,$inout5
487 .size _aesni_${dir}rypt6,.-_aesni_${dir}rypt6
490 sub aesni_generate8 {
492 # As already mentioned it takes in $key and $rounds, which are *not*
493 # preserved. $inout[0-7] is cipher/clear text...
495 .type _aesni_${dir}rypt8,\@abi-omnipotent
498 $movkey ($key),$rndkey0
500 $movkey 16($key),$rndkey1
501 xorps $rndkey0,$inout0
502 xorps $rndkey0,$inout1
503 pxor $rndkey0,$inout2
504 pxor $rndkey0,$inout3
505 pxor $rndkey0,$inout4
506 lea 32($key,$rounds),$key
508 aes${dir} $rndkey1,$inout0
510 pxor $rndkey0,$inout5
511 aes${dir} $rndkey1,$inout1
512 pxor $rndkey0,$inout6
513 pxor $rndkey0,$inout7
514 aes${dir} $rndkey1,$inout2
515 aes${dir} $rndkey1,$inout3
516 aes${dir} $rndkey1,$inout4
517 aes${dir} $rndkey1,$inout5
518 aes${dir} $rndkey1,$inout6
519 aes${dir} $rndkey1,$inout7
520 $movkey -16($key,%rax),$rndkey0
521 jmp .L${dir}_loop8_enter
524 aes${dir} $rndkey1,$inout0
525 aes${dir} $rndkey1,$inout1
526 aes${dir} $rndkey1,$inout2
527 aes${dir} $rndkey1,$inout3
528 aes${dir} $rndkey1,$inout4
529 aes${dir} $rndkey1,$inout5
530 aes${dir} $rndkey1,$inout6
531 aes${dir} $rndkey1,$inout7
532 .L${dir}_loop8_enter:
533 $movkey ($key,%rax),$rndkey1
535 aes${dir} $rndkey0,$inout0
536 aes${dir} $rndkey0,$inout1
537 aes${dir} $rndkey0,$inout2
538 aes${dir} $rndkey0,$inout3
539 aes${dir} $rndkey0,$inout4
540 aes${dir} $rndkey0,$inout5
541 aes${dir} $rndkey0,$inout6
542 aes${dir} $rndkey0,$inout7
543 $movkey -16($key,%rax),$rndkey0
546 aes${dir} $rndkey1,$inout0
547 aes${dir} $rndkey1,$inout1
548 aes${dir} $rndkey1,$inout2
549 aes${dir} $rndkey1,$inout3
550 aes${dir} $rndkey1,$inout4
551 aes${dir} $rndkey1,$inout5
552 aes${dir} $rndkey1,$inout6
553 aes${dir} $rndkey1,$inout7
554 aes${dir}last $rndkey0,$inout0
555 aes${dir}last $rndkey0,$inout1
556 aes${dir}last $rndkey0,$inout2
557 aes${dir}last $rndkey0,$inout3
558 aes${dir}last $rndkey0,$inout4
559 aes${dir}last $rndkey0,$inout5
560 aes${dir}last $rndkey0,$inout6
561 aes${dir}last $rndkey0,$inout7
563 .size _aesni_${dir}rypt8,.-_aesni_${dir}rypt8
566 &aesni_generate2("enc") if ($PREFIX eq "aesni");
567 &aesni_generate2("dec");
568 &aesni_generate3("enc") if ($PREFIX eq "aesni");
569 &aesni_generate3("dec");
570 &aesni_generate4("enc") if ($PREFIX eq "aesni");
571 &aesni_generate4("dec");
572 &aesni_generate6("enc") if ($PREFIX eq "aesni");
573 &aesni_generate6("dec");
574 &aesni_generate8("enc") if ($PREFIX eq "aesni");
575 &aesni_generate8("dec");
577 if ($PREFIX eq "aesni") {
578 ########################################################################
579 # void aesni_ecb_encrypt (const void *in, void *out,
580 # size_t length, const AES_KEY *key,
583 .globl aesni_ecb_encrypt
584 .type aesni_ecb_encrypt,\@function,5
590 mov 240($key),$rounds # key->rounds
591 $movkey ($key),$rndkey0
592 mov $key,$key_ # backup $key
593 mov $rounds,$rnds_ # backup $rounds
594 test %r8d,%r8d # 5th argument
596 #--------------------------- ECB ENCRYPT ------------------------------#
600 movdqu ($inp),$inout0
601 movdqu 0x10($inp),$inout1
602 movdqu 0x20($inp),$inout2
603 movdqu 0x30($inp),$inout3
604 movdqu 0x40($inp),$inout4
605 movdqu 0x50($inp),$inout5
606 movdqu 0x60($inp),$inout6
607 movdqu 0x70($inp),$inout7
610 jmp .Lecb_enc_loop8_enter
613 movups $inout0,($out)
614 mov $key_,$key # restore $key
615 movdqu ($inp),$inout0
616 mov $rnds_,$rounds # restore $rounds
617 movups $inout1,0x10($out)
618 movdqu 0x10($inp),$inout1
619 movups $inout2,0x20($out)
620 movdqu 0x20($inp),$inout2
621 movups $inout3,0x30($out)
622 movdqu 0x30($inp),$inout3
623 movups $inout4,0x40($out)
624 movdqu 0x40($inp),$inout4
625 movups $inout5,0x50($out)
626 movdqu 0x50($inp),$inout5
627 movups $inout6,0x60($out)
628 movdqu 0x60($inp),$inout6
629 movups $inout7,0x70($out)
631 movdqu 0x70($inp),$inout7
633 .Lecb_enc_loop8_enter:
640 movups $inout0,($out)
641 mov $key_,$key # restore $key
642 movups $inout1,0x10($out)
643 mov $rnds_,$rounds # restore $rounds
644 movups $inout2,0x20($out)
645 movups $inout3,0x30($out)
646 movups $inout4,0x40($out)
647 movups $inout5,0x50($out)
648 movups $inout6,0x60($out)
649 movups $inout7,0x70($out)
655 movups ($inp),$inout0
658 movups 0x10($inp),$inout1
660 movups 0x20($inp),$inout2
663 movups 0x30($inp),$inout3
665 movups 0x40($inp),$inout4
668 movups 0x50($inp),$inout5
670 movdqu 0x60($inp),$inout6
672 movups $inout0,($out)
673 movups $inout1,0x10($out)
674 movups $inout2,0x20($out)
675 movups $inout3,0x30($out)
676 movups $inout4,0x40($out)
677 movups $inout5,0x50($out)
678 movups $inout6,0x60($out)
683 &aesni_generate1("enc",$key,$rounds);
685 movups $inout0,($out)
690 movups $inout0,($out)
691 movups $inout1,0x10($out)
696 movups $inout0,($out)
697 movups $inout1,0x10($out)
698 movups $inout2,0x20($out)
703 movups $inout0,($out)
704 movups $inout1,0x10($out)
705 movups $inout2,0x20($out)
706 movups $inout3,0x30($out)
710 xorps $inout5,$inout5
712 movups $inout0,($out)
713 movups $inout1,0x10($out)
714 movups $inout2,0x20($out)
715 movups $inout3,0x30($out)
716 movups $inout4,0x40($out)
721 movups $inout0,($out)
722 movups $inout1,0x10($out)
723 movups $inout2,0x20($out)
724 movups $inout3,0x30($out)
725 movups $inout4,0x40($out)
726 movups $inout5,0x50($out)
728 \f#--------------------------- ECB DECRYPT ------------------------------#
734 movdqu ($inp),$inout0
735 movdqu 0x10($inp),$inout1
736 movdqu 0x20($inp),$inout2
737 movdqu 0x30($inp),$inout3
738 movdqu 0x40($inp),$inout4
739 movdqu 0x50($inp),$inout5
740 movdqu 0x60($inp),$inout6
741 movdqu 0x70($inp),$inout7
744 jmp .Lecb_dec_loop8_enter
747 movups $inout0,($out)
748 mov $key_,$key # restore $key
749 movdqu ($inp),$inout0
750 mov $rnds_,$rounds # restore $rounds
751 movups $inout1,0x10($out)
752 movdqu 0x10($inp),$inout1
753 movups $inout2,0x20($out)
754 movdqu 0x20($inp),$inout2
755 movups $inout3,0x30($out)
756 movdqu 0x30($inp),$inout3
757 movups $inout4,0x40($out)
758 movdqu 0x40($inp),$inout4
759 movups $inout5,0x50($out)
760 movdqu 0x50($inp),$inout5
761 movups $inout6,0x60($out)
762 movdqu 0x60($inp),$inout6
763 movups $inout7,0x70($out)
765 movdqu 0x70($inp),$inout7
767 .Lecb_dec_loop8_enter:
771 $movkey ($key_),$rndkey0
775 movups $inout0,($out)
776 mov $key_,$key # restore $key
777 movups $inout1,0x10($out)
778 mov $rnds_,$rounds # restore $rounds
779 movups $inout2,0x20($out)
780 movups $inout3,0x30($out)
781 movups $inout4,0x40($out)
782 movups $inout5,0x50($out)
783 movups $inout6,0x60($out)
784 movups $inout7,0x70($out)
790 movups ($inp),$inout0
793 movups 0x10($inp),$inout1
795 movups 0x20($inp),$inout2
798 movups 0x30($inp),$inout3
800 movups 0x40($inp),$inout4
803 movups 0x50($inp),$inout5
805 movups 0x60($inp),$inout6
806 $movkey ($key),$rndkey0
808 movups $inout0,($out)
809 movups $inout1,0x10($out)
810 movups $inout2,0x20($out)
811 movups $inout3,0x30($out)
812 movups $inout4,0x40($out)
813 movups $inout5,0x50($out)
814 movups $inout6,0x60($out)
819 &aesni_generate1("dec",$key,$rounds);
821 movups $inout0,($out)
826 movups $inout0,($out)
827 movups $inout1,0x10($out)
832 movups $inout0,($out)
833 movups $inout1,0x10($out)
834 movups $inout2,0x20($out)
839 movups $inout0,($out)
840 movups $inout1,0x10($out)
841 movups $inout2,0x20($out)
842 movups $inout3,0x30($out)
846 xorps $inout5,$inout5
848 movups $inout0,($out)
849 movups $inout1,0x10($out)
850 movups $inout2,0x20($out)
851 movups $inout3,0x30($out)
852 movups $inout4,0x40($out)
857 movups $inout0,($out)
858 movups $inout1,0x10($out)
859 movups $inout2,0x20($out)
860 movups $inout3,0x30($out)
861 movups $inout4,0x40($out)
862 movups $inout5,0x50($out)
866 .size aesni_ecb_encrypt,.-aesni_ecb_encrypt
870 ######################################################################
871 # void aesni_ccm64_[en|de]crypt_blocks (const void *in, void *out,
872 # size_t blocks, const AES_KEY *key,
873 # const char *ivec,char *cmac);
875 # Handles only complete blocks, operates on 64-bit counter and
876 # does not update *ivec! Nor does it finalize CMAC value
877 # (see engine/eng_aesni.c for details)
880 my $cmac="%r9"; # 6th argument
882 my $increment="%xmm9";
884 my $bswap_mask="%xmm7";
887 .globl aesni_ccm64_encrypt_blocks
888 .type aesni_ccm64_encrypt_blocks,\@function,6
890 aesni_ccm64_encrypt_blocks:
892 $code.=<<___ if ($win64);
895 movaps %xmm7,0x10(%rsp)
896 movaps %xmm8,0x20(%rsp)
897 movaps %xmm9,0x30(%rsp)
901 mov 240($key),$rounds # key->rounds
903 movdqa .Lincrement64(%rip),$increment
904 movdqa .Lbswap_mask(%rip),$bswap_mask
909 movdqu ($cmac),$inout1
911 lea 32($key,$rounds),$key # end of key schedule
912 pshufb $bswap_mask,$iv
913 sub %rax,%r10 # twisted $rounds
914 jmp .Lccm64_enc_outer
917 $movkey ($key_),$rndkey0
919 movups ($inp),$in0 # load inp
921 xorps $rndkey0,$inout0 # counter
922 $movkey 16($key_),$rndkey1
924 xorps $rndkey0,$inout1 # cmac^=inp
925 $movkey 32($key_),$rndkey0
928 aesenc $rndkey1,$inout0
929 aesenc $rndkey1,$inout1
930 $movkey ($key,%rax),$rndkey1
932 aesenc $rndkey0,$inout0
933 aesenc $rndkey0,$inout1
934 $movkey -16($key,%rax),$rndkey0
935 jnz .Lccm64_enc2_loop
936 aesenc $rndkey1,$inout0
937 aesenc $rndkey1,$inout1
940 aesenclast $rndkey0,$inout0
941 aesenclast $rndkey0,$inout1
944 xorps $inout0,$in0 # inp ^= E(iv)
946 movups $in0,($out) # save output
947 pshufb $bswap_mask,$inout0
949 jnz .Lccm64_enc_outer
951 movups $inout1,($cmac)
953 $code.=<<___ if ($win64);
955 movaps 0x10(%rsp),%xmm7
956 movaps 0x20(%rsp),%xmm8
957 movaps 0x30(%rsp),%xmm9
963 .size aesni_ccm64_encrypt_blocks,.-aesni_ccm64_encrypt_blocks
965 ######################################################################
967 .globl aesni_ccm64_decrypt_blocks
968 .type aesni_ccm64_decrypt_blocks,\@function,6
970 aesni_ccm64_decrypt_blocks:
972 $code.=<<___ if ($win64);
975 movaps %xmm7,0x10(%rsp)
976 movaps %xmm8,0x20(%rsp)
977 movaps %xmm9,0x30(%rsp)
981 mov 240($key),$rounds # key->rounds
983 movdqu ($cmac),$inout1
984 movdqa .Lincrement64(%rip),$increment
985 movdqa .Lbswap_mask(%rip),$bswap_mask
990 pshufb $bswap_mask,$iv
992 &aesni_generate1("enc",$key,$rounds);
996 movups ($inp),$in0 # load inp
999 sub %r10,%rax # twisted $rounds
1000 lea 32($key_,$rnds_),$key # end of key schedule
1002 jmp .Lccm64_dec_outer
1005 xorps $inout0,$in0 # inp ^= E(iv)
1007 movups $in0,($out) # save output
1009 pshufb $bswap_mask,$inout0
1012 jz .Lccm64_dec_break
1014 $movkey ($key_),$rndkey0
1016 $movkey 16($key_),$rndkey1
1018 xorps $rndkey0,$inout0
1019 xorps $in0,$inout1 # cmac^=out
1020 $movkey 32($key_),$rndkey0
1021 jmp .Lccm64_dec2_loop
1024 aesenc $rndkey1,$inout0
1025 aesenc $rndkey1,$inout1
1026 $movkey ($key,%rax),$rndkey1
1028 aesenc $rndkey0,$inout0
1029 aesenc $rndkey0,$inout1
1030 $movkey -16($key,%rax),$rndkey0
1031 jnz .Lccm64_dec2_loop
1032 movups ($inp),$in0 # load inp
1033 paddq $increment,$iv
1034 aesenc $rndkey1,$inout0
1035 aesenc $rndkey1,$inout1
1036 aesenclast $rndkey0,$inout0
1037 aesenclast $rndkey0,$inout1
1039 jmp .Lccm64_dec_outer
1043 #xorps $in0,$inout1 # cmac^=out
1044 mov 240($key_),$rounds
1046 &aesni_generate1("enc",$key_,$rounds,$inout1,$in0);
1048 movups $inout1,($cmac)
1050 $code.=<<___ if ($win64);
1052 movaps 0x10(%rsp),%xmm7
1053 movaps 0x20(%rsp),%xmm8
1054 movaps 0x30(%rsp),%xmm9
1060 .size aesni_ccm64_decrypt_blocks,.-aesni_ccm64_decrypt_blocks
1063 ######################################################################
1064 # void aesni_ctr32_encrypt_blocks (const void *in, void *out,
1065 # size_t blocks, const AES_KEY *key,
1066 # const char *ivec);
1068 # Handles only complete blocks, operates on 32-bit counter and
1069 # does not update *ivec! (see crypto/modes/ctr128.c for details)
1071 # Overhaul based on suggestions from Shay Gueron and Vlad Krasnov,
1072 # http://rt.openssl.org/Ticket/Display.html?id=3021&user=guest&pass=guest.
1073 # Keywords are full unroll and modulo-schedule counter calculations
1074 # with zero-round key xor.
1076 my ($in0,$in1,$in2,$in3,$in4,$in5)=map("%xmm$_",(10..15));
1077 my ($key0,$ctr)=("${key_}d","${ivp}d");
1078 my $frame_size = 0x80 + ($win64?160:0);
1081 .globl aesni_ctr32_encrypt_blocks
1082 .type aesni_ctr32_encrypt_blocks,\@function,5
1084 aesni_ctr32_encrypt_blocks:
1087 sub \$$frame_size,%rsp
1088 and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
1090 $code.=<<___ if ($win64);
1091 movaps %xmm6,-0xa8(%rax)
1092 movaps %xmm7,-0x98(%rax)
1093 movaps %xmm8,-0x88(%rax)
1094 movaps %xmm9,-0x78(%rax)
1095 movaps %xmm10,-0x68(%rax)
1096 movaps %xmm11,-0x58(%rax)
1097 movaps %xmm12,-0x48(%rax)
1098 movaps %xmm13,-0x38(%rax)
1099 movaps %xmm14,-0x28(%rax)
1100 movaps %xmm15,-0x18(%rax)
1107 je .Lctr32_one_shortcut
1109 movdqu ($ivp),$inout0
1110 movdqu ($key),$rndkey0
1111 mov 12($ivp),$ctr # counter LSB
1112 pxor $rndkey0,$inout0
1113 mov 12($key),$key0 # 0-round key LSB
1114 movdqa $inout0,0x00(%rsp) # populate counter block
1116 movdqa $inout0,$inout1
1117 movdqa $inout0,$inout2
1118 movdqa $inout0,$inout3
1119 movdqa $inout0,0x40(%rsp)
1120 movdqa $inout0,0x50(%rsp)
1121 movdqa $inout0,0x60(%rsp)
1122 mov %rdx,%r10 # borrow %rdx
1123 movdqa $inout0,0x70(%rsp)
1131 pinsrd \$3,%eax,$inout1
1133 movdqa $inout1,0x10(%rsp)
1134 pinsrd \$3,%edx,$inout2
1136 mov %r10,%rdx # restore %rdx
1138 movdqa $inout2,0x20(%rsp)
1141 pinsrd \$3,%eax,$inout3
1143 movdqa $inout3,0x30(%rsp)
1145 mov %r10d,0x40+12(%rsp)
1148 mov 240($key),$rounds # key->rounds
1151 mov %r9d,0x50+12(%rsp)
1154 mov %r10d,0x60+12(%rsp)
1156 mov OPENSSL_ia32cap_P+4(%rip),%r10d
1158 and \$`1<<26|1<<22`,%r10d # isolate XSAVE+MOVBE
1159 mov %r9d,0x70+12(%rsp)
1161 $movkey 0x10($key),$rndkey1
1163 movdqa 0x40(%rsp),$inout4
1164 movdqa 0x50(%rsp),$inout5
1170 cmp \$`1<<22`,%r10d # check for MOVBE without XSAVE
1173 lea 0x80($key),$key # size optimization
1182 lea 32($key,$rounds),$key # end of key schedule
1183 sub %rax,%r10 # twisted $rounds
1189 $movkey -48($key,$rnds_),$rndkey0
1190 aesenc $rndkey1,$inout0
1193 aesenc $rndkey1,$inout1
1194 movbe %eax,`0x00+12`(%rsp)
1196 aesenc $rndkey1,$inout2
1198 movbe %eax,`0x10+12`(%rsp)
1199 aesenc $rndkey1,$inout3
1202 aesenc $rndkey1,$inout4
1203 movbe %eax,`0x20+12`(%rsp)
1205 aesenc $rndkey1,$inout5
1206 $movkey -32($key,$rnds_),$rndkey1
1209 aesenc $rndkey0,$inout0
1210 movbe %eax,`0x30+12`(%rsp)
1212 aesenc $rndkey0,$inout1
1214 movbe %eax,`0x40+12`(%rsp)
1215 aesenc $rndkey0,$inout2
1218 aesenc $rndkey0,$inout3
1219 movbe %eax,`0x50+12`(%rsp)
1220 mov %r10,%rax # mov $rnds_,$rounds
1221 aesenc $rndkey0,$inout4
1222 aesenc $rndkey0,$inout5
1223 $movkey -16($key,$rnds_),$rndkey0
1227 movdqu ($inp),$inout6
1228 movdqu 0x10($inp),$inout7
1229 movdqu 0x20($inp),$in0
1230 movdqu 0x30($inp),$in1
1231 movdqu 0x40($inp),$in2
1232 movdqu 0x50($inp),$in3
1234 $movkey -64($key,$rnds_),$rndkey1
1235 pxor $inout0,$inout6
1236 movaps 0x00(%rsp),$inout0
1237 pxor $inout1,$inout7
1238 movaps 0x10(%rsp),$inout1
1240 movaps 0x20(%rsp),$inout2
1242 movaps 0x30(%rsp),$inout3
1244 movaps 0x40(%rsp),$inout4
1246 movaps 0x50(%rsp),$inout5
1247 movdqu $inout6,($out)
1248 movdqu $inout7,0x10($out)
1249 movdqu $in0,0x20($out)
1250 movdqu $in1,0x30($out)
1251 movdqu $in2,0x40($out)
1252 movdqu $in3,0x50($out)
1261 lea -48($rnds_),$rounds
1262 lea -80($key,$rnds_),$key # restore $key
1264 shr \$4,$rounds # restore $rounds
1270 movdqa 0x60(%rsp),$inout6
1271 aesenc $rndkey1,$inout0
1273 movdqa 0x70(%rsp),$inout7
1274 aesenc $rndkey1,$inout1
1276 $movkey 0x20-0x80($key),$rndkey0
1277 aesenc $rndkey1,$inout2
1280 aesenc $rndkey1,$inout3
1281 mov %r9d,0x00+12(%rsp)
1283 aesenc $rndkey1,$inout4
1284 aesenc $rndkey1,$inout5
1285 aesenc $rndkey1,$inout6
1286 aesenc $rndkey1,$inout7
1287 $movkey 0x30-0x80($key),$rndkey1
1289 for($i=2;$i<8;$i++) {
1290 my $rndkeyx = ($i&1)?$rndkey1:$rndkey0;
1293 aesenc $rndkeyx,$inout0
1294 aesenc $rndkeyx,$inout1
1297 aesenc $rndkeyx,$inout2
1298 aesenc $rndkeyx,$inout3
1299 mov %r9d,`0x10*($i-1)`+12(%rsp)
1301 aesenc $rndkeyx,$inout4
1302 aesenc $rndkeyx,$inout5
1303 aesenc $rndkeyx,$inout6
1304 aesenc $rndkeyx,$inout7
1305 $movkey `0x20+0x10*$i`-0x80($key),$rndkeyx
1310 aesenc $rndkey0,$inout0
1311 aesenc $rndkey0,$inout1
1312 aesenc $rndkey0,$inout2
1314 movdqu 0x00($inp),$in0
1315 aesenc $rndkey0,$inout3
1316 mov %r9d,0x70+12(%rsp)
1318 aesenc $rndkey0,$inout4
1319 aesenc $rndkey0,$inout5
1320 aesenc $rndkey0,$inout6
1321 aesenc $rndkey0,$inout7
1322 $movkey 0xa0-0x80($key),$rndkey0
1326 aesenc $rndkey1,$inout0
1327 aesenc $rndkey1,$inout1
1328 aesenc $rndkey1,$inout2
1329 aesenc $rndkey1,$inout3
1330 aesenc $rndkey1,$inout4
1331 aesenc $rndkey1,$inout5
1332 aesenc $rndkey1,$inout6
1333 aesenc $rndkey1,$inout7
1334 $movkey 0xb0-0x80($key),$rndkey1
1336 aesenc $rndkey0,$inout0
1337 aesenc $rndkey0,$inout1
1338 aesenc $rndkey0,$inout2
1339 aesenc $rndkey0,$inout3
1340 aesenc $rndkey0,$inout4
1341 aesenc $rndkey0,$inout5
1342 aesenc $rndkey0,$inout6
1343 aesenc $rndkey0,$inout7
1344 $movkey 0xc0-0x80($key),$rndkey0
1347 aesenc $rndkey1,$inout0
1348 aesenc $rndkey1,$inout1
1349 aesenc $rndkey1,$inout2
1350 aesenc $rndkey1,$inout3
1351 aesenc $rndkey1,$inout4
1352 aesenc $rndkey1,$inout5
1353 aesenc $rndkey1,$inout6
1354 aesenc $rndkey1,$inout7
1355 $movkey 0xd0-0x80($key),$rndkey1
1357 aesenc $rndkey0,$inout0
1358 aesenc $rndkey0,$inout1
1359 aesenc $rndkey0,$inout2
1360 aesenc $rndkey0,$inout3
1361 aesenc $rndkey0,$inout4
1362 aesenc $rndkey0,$inout5
1363 aesenc $rndkey0,$inout6
1364 aesenc $rndkey0,$inout7
1365 $movkey 0xe0-0x80($key),$rndkey0
1366 jmp .Lctr32_enc_done
1370 movdqu 0x10($inp),$in1
1372 movdqu 0x20($inp),$in2
1374 movdqu 0x30($inp),$in3
1376 movdqu 0x40($inp),$in4
1378 movdqu 0x50($inp),$in5
1381 aesenc $rndkey1,$inout0
1382 aesenc $rndkey1,$inout1
1383 aesenc $rndkey1,$inout2
1384 aesenc $rndkey1,$inout3
1385 aesenc $rndkey1,$inout4
1386 aesenc $rndkey1,$inout5
1387 aesenc $rndkey1,$inout6
1388 aesenc $rndkey1,$inout7
1389 movdqu 0x60($inp),$rndkey1
1392 aesenclast $in0,$inout0
1393 pxor $rndkey0,$rndkey1
1394 movdqu 0x70-0x80($inp),$in0
1395 aesenclast $in1,$inout1
1397 movdqa 0x00(%rsp),$in1 # load next counter block
1398 aesenclast $in2,$inout2
1399 aesenclast $in3,$inout3
1400 movdqa 0x10(%rsp),$in2
1401 movdqa 0x20(%rsp),$in3
1402 aesenclast $in4,$inout4
1403 aesenclast $in5,$inout5
1404 movdqa 0x30(%rsp),$in4
1405 movdqa 0x40(%rsp),$in5
1406 aesenclast $rndkey1,$inout6
1407 movdqa 0x50(%rsp),$rndkey0
1408 $movkey 0x10-0x80($key),$rndkey1
1409 aesenclast $in0,$inout7
1411 movups $inout0,($out) # store output
1413 movups $inout1,0x10($out)
1415 movups $inout2,0x20($out)
1417 movups $inout3,0x30($out)
1419 movups $inout4,0x40($out)
1421 movups $inout5,0x50($out)
1422 movdqa $rndkey0,$inout5
1423 movups $inout6,0x60($out)
1424 movups $inout7,0x70($out)
1432 lea -0x80($key),$key
1441 movdqa 0x60(%rsp),$inout6
1442 pxor $inout7,$inout7
1444 $movkey 16($key),$rndkey0
1445 aesenc $rndkey1,$inout0
1446 aesenc $rndkey1,$inout1
1447 lea 32-16($key,$rounds),$key
1449 aesenc $rndkey1,$inout2
1452 aesenc $rndkey1,$inout3
1453 aesenc $rndkey1,$inout4
1454 movups 0x10($inp),$in1
1455 movups 0x20($inp),$in2
1456 aesenc $rndkey1,$inout5
1457 aesenc $rndkey1,$inout6
1459 call .Lenc_loop8_enter
1461 movdqu 0x30($inp),$in3
1463 movdqu 0x40($inp),$in0
1465 movdqu $inout0,($out)
1467 movdqu $inout1,0x10($out)
1469 movdqu $inout2,0x20($out)
1471 movdqu $inout3,0x30($out)
1472 movdqu $inout4,0x40($out)
1476 movups 0x50($inp),$in1
1478 movups $inout5,0x50($out)
1481 movups 0x60($inp),$in2
1483 movups $inout6,0x60($out)
1488 aesenc $rndkey1,$inout0
1491 aesenc $rndkey1,$inout1
1492 aesenc $rndkey1,$inout2
1493 aesenc $rndkey1,$inout3
1494 $movkey ($key),$rndkey1
1496 aesenclast $rndkey1,$inout0
1497 aesenclast $rndkey1,$inout1
1499 movups 0x10($inp),$in1
1500 aesenclast $rndkey1,$inout2
1501 aesenclast $rndkey1,$inout3
1502 movups 0x20($inp),$in2
1503 movups 0x30($inp),$in3
1506 movups $inout0,($out)
1508 movups $inout1,0x10($out)
1510 movdqu $inout2,0x20($out)
1512 movdqu $inout3,0x30($out)
1517 aesenc $rndkey1,$inout0
1520 aesenc $rndkey1,$inout1
1521 aesenc $rndkey1,$inout2
1522 $movkey ($key),$rndkey1
1524 aesenclast $rndkey1,$inout0
1525 aesenclast $rndkey1,$inout1
1526 aesenclast $rndkey1,$inout2
1530 movups $inout0,($out)
1534 movups 0x10($inp),$in1
1536 movups $inout1,0x10($out)
1539 movups 0x20($inp),$in2
1541 movups $inout2,0x20($out)
1545 .Lctr32_one_shortcut:
1546 movups ($ivp),$inout0
1548 mov 240($key),$rounds # key->rounds
1550 &aesni_generate1("enc",$key,$rounds);
1553 movups $inout0,($out)
1559 $code.=<<___ if ($win64);
1560 movaps -0xa0(%rbp),%xmm6
1561 movaps -0x90(%rbp),%xmm7
1562 movaps -0x80(%rbp),%xmm8
1563 movaps -0x70(%rbp),%xmm9
1564 movaps -0x60(%rbp),%xmm10
1565 movaps -0x50(%rbp),%xmm11
1566 movaps -0x40(%rbp),%xmm12
1567 movaps -0x30(%rbp),%xmm13
1568 movaps -0x20(%rbp),%xmm14
1569 movaps -0x10(%rbp),%xmm15
1576 .size aesni_ctr32_encrypt_blocks,.-aesni_ctr32_encrypt_blocks
1580 ######################################################################
1581 # void aesni_xts_[en|de]crypt(const char *inp,char *out,size_t len,
1582 # const AES_KEY *key1, const AES_KEY *key2
1583 # const unsigned char iv[16]);
1586 my @tweak=map("%xmm$_",(10..15));
1587 my ($twmask,$twres,$twtmp)=("%xmm8","%xmm9",@tweak[4]);
1588 my ($key2,$ivp,$len_)=("%r8","%r9","%r9");
1589 my $frame_size = 0x70 + ($win64?160:0);
1592 .globl aesni_xts_encrypt
1593 .type aesni_xts_encrypt,\@function,6
1598 sub \$$frame_size,%rsp
1599 and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
1601 $code.=<<___ if ($win64);
1602 movaps %xmm6,-0xa8(%rax)
1603 movaps %xmm7,-0x98(%rax)
1604 movaps %xmm8,-0x88(%rax)
1605 movaps %xmm9,-0x78(%rax)
1606 movaps %xmm10,-0x68(%rax)
1607 movaps %xmm11,-0x58(%rax)
1608 movaps %xmm12,-0x48(%rax)
1609 movaps %xmm13,-0x38(%rax)
1610 movaps %xmm14,-0x28(%rax)
1611 movaps %xmm15,-0x18(%rax)
1616 movups ($ivp),$inout0 # load clear-text tweak
1617 mov 240(%r8),$rounds # key2->rounds
1618 mov 240($key),$rnds_ # key1->rounds
1620 # generate the tweak
1621 &aesni_generate1("enc",$key2,$rounds,$inout0);
1623 $movkey ($key),$rndkey0 # zero round key
1624 mov $key,$key_ # backup $key
1625 mov $rnds_,$rounds # backup $rounds
1627 mov $len,$len_ # backup $len
1630 $movkey 16($key,$rnds_),$rndkey1 # last round key
1632 movdqa .Lxts_magic(%rip),$twmask
1633 movdqa $inout0,@tweak[5]
1634 pshufd \$0x5f,$inout0,$twres
1635 pxor $rndkey0,$rndkey1
1637 # alternative tweak calculation algorithm is based on suggestions
1638 # by Shay Gueron. psrad doesn't conflict with AES-NI instructions
1639 # and should help in the future...
1640 for ($i=0;$i<4;$i++) {
1642 movdqa $twres,$twtmp
1644 movdqa @tweak[5],@tweak[$i]
1645 psrad \$31,$twtmp # broadcast upper bits
1646 paddq @tweak[5],@tweak[5]
1648 pxor $rndkey0,@tweak[$i]
1649 pxor $twtmp,@tweak[5]
1653 movdqa @tweak[5],@tweak[4]
1655 paddq @tweak[5],@tweak[5]
1657 pxor $rndkey0,@tweak[4]
1658 pxor $twres,@tweak[5]
1659 movaps $rndkey1,0x60(%rsp) # save round[0]^round[last]
1665 lea 32($key_,$rnds_),$key # end of key schedule
1666 sub %r10,%rax # twisted $rounds
1667 $movkey 16($key_),$rndkey1
1668 mov %rax,%r10 # backup twisted $rounds
1669 lea .Lxts_magic(%rip),%r8
1670 jmp .Lxts_enc_grandloop
1673 .Lxts_enc_grandloop:
1674 movdqu `16*0`($inp),$inout0 # load input
1675 movdqa $rndkey0,$twmask
1676 movdqu `16*1`($inp),$inout1
1677 pxor @tweak[0],$inout0
1678 movdqu `16*2`($inp),$inout2
1679 pxor @tweak[1],$inout1
1680 aesenc $rndkey1,$inout0
1681 movdqu `16*3`($inp),$inout3
1682 pxor @tweak[2],$inout2
1683 aesenc $rndkey1,$inout1
1684 movdqu `16*4`($inp),$inout4
1685 pxor @tweak[3],$inout3
1686 aesenc $rndkey1,$inout2
1687 movdqu `16*5`($inp),$inout5
1688 pxor @tweak[5],$twmask # round[0]^=tweak[5]
1689 movdqa 0x60(%rsp),$twres # load round[0]^round[last]
1690 pxor @tweak[4],$inout4
1691 aesenc $rndkey1,$inout3
1692 $movkey 32($key_),$rndkey0
1693 lea `16*6`($inp),$inp
1694 pxor $twmask,$inout5
1696 pxor $twres,@tweak[0]
1697 aesenc $rndkey1,$inout4
1698 pxor $twres,@tweak[1]
1699 movdqa @tweak[0],`16*0`(%rsp) # put aside tweaks^last round key
1700 aesenc $rndkey1,$inout5
1701 $movkey 48($key_),$rndkey1
1702 pxor $twres,@tweak[2]
1704 aesenc $rndkey0,$inout0
1705 pxor $twres,@tweak[3]
1706 movdqa @tweak[1],`16*1`(%rsp)
1707 aesenc $rndkey0,$inout1
1708 pxor $twres,@tweak[4]
1709 movdqa @tweak[2],`16*2`(%rsp)
1710 aesenc $rndkey0,$inout2
1711 aesenc $rndkey0,$inout3
1713 movdqa @tweak[4],`16*4`(%rsp)
1714 aesenc $rndkey0,$inout4
1715 aesenc $rndkey0,$inout5
1716 $movkey 64($key_),$rndkey0
1717 movdqa $twmask,`16*5`(%rsp)
1718 pshufd \$0x5f,@tweak[5],$twres
1722 aesenc $rndkey1,$inout0
1723 aesenc $rndkey1,$inout1
1724 aesenc $rndkey1,$inout2
1725 aesenc $rndkey1,$inout3
1726 aesenc $rndkey1,$inout4
1727 aesenc $rndkey1,$inout5
1728 $movkey -64($key,%rax),$rndkey1
1731 aesenc $rndkey0,$inout0
1732 aesenc $rndkey0,$inout1
1733 aesenc $rndkey0,$inout2
1734 aesenc $rndkey0,$inout3
1735 aesenc $rndkey0,$inout4
1736 aesenc $rndkey0,$inout5
1737 $movkey -80($key,%rax),$rndkey0
1740 movdqa (%r8),$twmask
1741 movdqa $twres,$twtmp
1743 aesenc $rndkey1,$inout0
1744 paddq @tweak[5],@tweak[5]
1746 aesenc $rndkey1,$inout1
1748 $movkey ($key_),@tweak[0] # load round[0]
1749 aesenc $rndkey1,$inout2
1750 aesenc $rndkey1,$inout3
1751 aesenc $rndkey1,$inout4
1752 pxor $twtmp,@tweak[5]
1753 movaps @tweak[0],@tweak[1] # copy round[0]
1754 aesenc $rndkey1,$inout5
1755 $movkey -64($key),$rndkey1
1757 movdqa $twres,$twtmp
1758 aesenc $rndkey0,$inout0
1760 pxor @tweak[5],@tweak[0]
1761 aesenc $rndkey0,$inout1
1763 paddq @tweak[5],@tweak[5]
1764 aesenc $rndkey0,$inout2
1765 aesenc $rndkey0,$inout3
1767 movaps @tweak[1],@tweak[2]
1768 aesenc $rndkey0,$inout4
1769 pxor $twtmp,@tweak[5]
1770 movdqa $twres,$twtmp
1771 aesenc $rndkey0,$inout5
1772 $movkey -48($key),$rndkey0
1775 aesenc $rndkey1,$inout0
1776 pxor @tweak[5],@tweak[1]
1778 aesenc $rndkey1,$inout1
1779 paddq @tweak[5],@tweak[5]
1781 aesenc $rndkey1,$inout2
1782 aesenc $rndkey1,$inout3
1783 movdqa @tweak[3],`16*3`(%rsp)
1784 pxor $twtmp,@tweak[5]
1785 aesenc $rndkey1,$inout4
1786 movaps @tweak[2],@tweak[3]
1787 movdqa $twres,$twtmp
1788 aesenc $rndkey1,$inout5
1789 $movkey -32($key),$rndkey1
1792 aesenc $rndkey0,$inout0
1793 pxor @tweak[5],@tweak[2]
1795 aesenc $rndkey0,$inout1
1796 paddq @tweak[5],@tweak[5]
1798 aesenc $rndkey0,$inout2
1799 aesenc $rndkey0,$inout3
1800 aesenc $rndkey0,$inout4
1801 pxor $twtmp,@tweak[5]
1802 movaps @tweak[3],@tweak[4]
1803 aesenc $rndkey0,$inout5
1805 movdqa $twres,$rndkey0
1807 aesenc $rndkey1,$inout0
1808 pxor @tweak[5],@tweak[3]
1810 aesenc $rndkey1,$inout1
1811 paddq @tweak[5],@tweak[5]
1812 pand $twmask,$rndkey0
1813 aesenc $rndkey1,$inout2
1814 aesenc $rndkey1,$inout3
1815 pxor $rndkey0,@tweak[5]
1816 $movkey ($key_),$rndkey0
1817 aesenc $rndkey1,$inout4
1818 aesenc $rndkey1,$inout5
1819 $movkey 16($key_),$rndkey1
1821 pxor @tweak[5],@tweak[4]
1822 aesenclast `16*0`(%rsp),$inout0
1824 paddq @tweak[5],@tweak[5]
1825 aesenclast `16*1`(%rsp),$inout1
1826 aesenclast `16*2`(%rsp),$inout2
1828 mov %r10,%rax # restore $rounds
1829 aesenclast `16*3`(%rsp),$inout3
1830 aesenclast `16*4`(%rsp),$inout4
1831 aesenclast `16*5`(%rsp),$inout5
1832 pxor $twres,@tweak[5]
1834 lea `16*6`($out),$out
1835 movups $inout0,`-16*6`($out) # write output
1836 movups $inout1,`-16*5`($out)
1837 movups $inout2,`-16*4`($out)
1838 movups $inout3,`-16*3`($out)
1839 movups $inout4,`-16*2`($out)
1840 movups $inout5,`-16*1`($out)
1842 jnc .Lxts_enc_grandloop
1846 mov $key_,$key # restore $key
1847 shr \$4,$rounds # restore original value
1850 mov $rounds,$rnds_ # backup $rounds
1851 pxor $rndkey0,@tweak[0]
1855 pxor $rndkey0,@tweak[1]
1858 pxor $rndkey0,@tweak[2]
1861 pxor $rndkey0,@tweak[3]
1864 pxor $rndkey0,@tweak[4]
1867 movdqu ($inp),$inout0
1868 movdqu 16*1($inp),$inout1
1869 movdqu 16*2($inp),$inout2
1870 pxor @tweak[0],$inout0
1871 movdqu 16*3($inp),$inout3
1872 pxor @tweak[1],$inout1
1873 movdqu 16*4($inp),$inout4
1875 pxor @tweak[2],$inout2
1876 pxor @tweak[3],$inout3
1877 pxor @tweak[4],$inout4
1879 call _aesni_encrypt6
1881 xorps @tweak[0],$inout0
1882 movdqa @tweak[5],@tweak[0]
1883 xorps @tweak[1],$inout1
1884 xorps @tweak[2],$inout2
1885 movdqu $inout0,($out)
1886 xorps @tweak[3],$inout3
1887 movdqu $inout1,16*1($out)
1888 xorps @tweak[4],$inout4
1889 movdqu $inout2,16*2($out)
1890 movdqu $inout3,16*3($out)
1891 movdqu $inout4,16*4($out)
1897 movups ($inp),$inout0
1899 xorps @tweak[0],$inout0
1901 &aesni_generate1("enc",$key,$rounds);
1903 xorps @tweak[0],$inout0
1904 movdqa @tweak[1],@tweak[0]
1905 movups $inout0,($out)
1911 movups ($inp),$inout0
1912 movups 16($inp),$inout1
1914 xorps @tweak[0],$inout0
1915 xorps @tweak[1],$inout1
1917 call _aesni_encrypt2
1919 xorps @tweak[0],$inout0
1920 movdqa @tweak[2],@tweak[0]
1921 xorps @tweak[1],$inout1
1922 movups $inout0,($out)
1923 movups $inout1,16*1($out)
1929 movups ($inp),$inout0
1930 movups 16*1($inp),$inout1
1931 movups 16*2($inp),$inout2
1933 xorps @tweak[0],$inout0
1934 xorps @tweak[1],$inout1
1935 xorps @tweak[2],$inout2
1937 call _aesni_encrypt3
1939 xorps @tweak[0],$inout0
1940 movdqa @tweak[3],@tweak[0]
1941 xorps @tweak[1],$inout1
1942 xorps @tweak[2],$inout2
1943 movups $inout0,($out)
1944 movups $inout1,16*1($out)
1945 movups $inout2,16*2($out)
1951 movups ($inp),$inout0
1952 movups 16*1($inp),$inout1
1953 movups 16*2($inp),$inout2
1954 xorps @tweak[0],$inout0
1955 movups 16*3($inp),$inout3
1957 xorps @tweak[1],$inout1
1958 xorps @tweak[2],$inout2
1959 xorps @tweak[3],$inout3
1961 call _aesni_encrypt4
1963 pxor @tweak[0],$inout0
1964 movdqa @tweak[4],@tweak[0]
1965 pxor @tweak[1],$inout1
1966 pxor @tweak[2],$inout2
1967 movdqu $inout0,($out)
1968 pxor @tweak[3],$inout3
1969 movdqu $inout1,16*1($out)
1970 movdqu $inout2,16*2($out)
1971 movdqu $inout3,16*3($out)
1982 movzb ($inp),%eax # borrow $rounds ...
1983 movzb -16($out),%ecx # ... and $key
1991 sub $len_,$out # rewind $out
1992 mov $key_,$key # restore $key
1993 mov $rnds_,$rounds # restore $rounds
1995 movups -16($out),$inout0
1996 xorps @tweak[0],$inout0
1998 &aesni_generate1("enc",$key,$rounds);
2000 xorps @tweak[0],$inout0
2001 movups $inout0,-16($out)
2005 $code.=<<___ if ($win64);
2006 movaps -0xa0(%rbp),%xmm6
2007 movaps -0x90(%rbp),%xmm7
2008 movaps -0x80(%rbp),%xmm8
2009 movaps -0x70(%rbp),%xmm9
2010 movaps -0x60(%rbp),%xmm10
2011 movaps -0x50(%rbp),%xmm11
2012 movaps -0x40(%rbp),%xmm12
2013 movaps -0x30(%rbp),%xmm13
2014 movaps -0x20(%rbp),%xmm14
2015 movaps -0x10(%rbp),%xmm15
2022 .size aesni_xts_encrypt,.-aesni_xts_encrypt
2026 .globl aesni_xts_decrypt
2027 .type aesni_xts_decrypt,\@function,6
2032 sub \$$frame_size,%rsp
2033 and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
2035 $code.=<<___ if ($win64);
2036 movaps %xmm6,-0xa8(%rax)
2037 movaps %xmm7,-0x98(%rax)
2038 movaps %xmm8,-0x88(%rax)
2039 movaps %xmm9,-0x78(%rax)
2040 movaps %xmm10,-0x68(%rax)
2041 movaps %xmm11,-0x58(%rax)
2042 movaps %xmm12,-0x48(%rax)
2043 movaps %xmm13,-0x38(%rax)
2044 movaps %xmm14,-0x28(%rax)
2045 movaps %xmm15,-0x18(%rax)
2050 movups ($ivp),$inout0 # load clear-text tweak
2051 mov 240($key2),$rounds # key2->rounds
2052 mov 240($key),$rnds_ # key1->rounds
2054 # generate the tweak
2055 &aesni_generate1("enc",$key2,$rounds,$inout0);
2057 xor %eax,%eax # if ($len%16) len-=16;
2063 $movkey ($key),$rndkey0 # zero round key
2064 mov $key,$key_ # backup $key
2065 mov $rnds_,$rounds # backup $rounds
2067 mov $len,$len_ # backup $len
2070 $movkey 16($key,$rnds_),$rndkey1 # last round key
2072 movdqa .Lxts_magic(%rip),$twmask
2073 movdqa $inout0,@tweak[5]
2074 pshufd \$0x5f,$inout0,$twres
2075 pxor $rndkey0,$rndkey1
2077 for ($i=0;$i<4;$i++) {
2079 movdqa $twres,$twtmp
2081 movdqa @tweak[5],@tweak[$i]
2082 psrad \$31,$twtmp # broadcast upper bits
2083 paddq @tweak[5],@tweak[5]
2085 pxor $rndkey0,@tweak[$i]
2086 pxor $twtmp,@tweak[5]
2090 movdqa @tweak[5],@tweak[4]
2092 paddq @tweak[5],@tweak[5]
2094 pxor $rndkey0,@tweak[4]
2095 pxor $twres,@tweak[5]
2096 movaps $rndkey1,0x60(%rsp) # save round[0]^round[last]
2102 lea 32($key_,$rnds_),$key # end of key schedule
2103 sub %r10,%rax # twisted $rounds
2104 $movkey 16($key_),$rndkey1
2105 mov %rax,%r10 # backup twisted $rounds
2106 lea .Lxts_magic(%rip),%r8
2107 jmp .Lxts_dec_grandloop
2110 .Lxts_dec_grandloop:
2111 movdqu `16*0`($inp),$inout0 # load input
2112 movdqa $rndkey0,$twmask
2113 movdqu `16*1`($inp),$inout1
2114 pxor @tweak[0],$inout0
2115 movdqu `16*2`($inp),$inout2
2116 pxor @tweak[1],$inout1
2117 aesdec $rndkey1,$inout0
2118 movdqu `16*3`($inp),$inout3
2119 pxor @tweak[2],$inout2
2120 aesdec $rndkey1,$inout1
2121 movdqu `16*4`($inp),$inout4
2122 pxor @tweak[3],$inout3
2123 aesdec $rndkey1,$inout2
2124 movdqu `16*5`($inp),$inout5
2125 pxor @tweak[5],$twmask # round[0]^=tweak[5]
2126 movdqa 0x60(%rsp),$twres # load round[0]^round[last]
2127 pxor @tweak[4],$inout4
2128 aesdec $rndkey1,$inout3
2129 $movkey 32($key_),$rndkey0
2130 lea `16*6`($inp),$inp
2131 pxor $twmask,$inout5
2133 pxor $twres,@tweak[0]
2134 aesdec $rndkey1,$inout4
2135 pxor $twres,@tweak[1]
2136 movdqa @tweak[0],`16*0`(%rsp) # put aside tweaks^last round key
2137 aesdec $rndkey1,$inout5
2138 $movkey 48($key_),$rndkey1
2139 pxor $twres,@tweak[2]
2141 aesdec $rndkey0,$inout0
2142 pxor $twres,@tweak[3]
2143 movdqa @tweak[1],`16*1`(%rsp)
2144 aesdec $rndkey0,$inout1
2145 pxor $twres,@tweak[4]
2146 movdqa @tweak[2],`16*2`(%rsp)
2147 aesdec $rndkey0,$inout2
2148 aesdec $rndkey0,$inout3
2150 movdqa @tweak[4],`16*4`(%rsp)
2151 aesdec $rndkey0,$inout4
2152 aesdec $rndkey0,$inout5
2153 $movkey 64($key_),$rndkey0
2154 movdqa $twmask,`16*5`(%rsp)
2155 pshufd \$0x5f,@tweak[5],$twres
2159 aesdec $rndkey1,$inout0
2160 aesdec $rndkey1,$inout1
2161 aesdec $rndkey1,$inout2
2162 aesdec $rndkey1,$inout3
2163 aesdec $rndkey1,$inout4
2164 aesdec $rndkey1,$inout5
2165 $movkey -64($key,%rax),$rndkey1
2168 aesdec $rndkey0,$inout0
2169 aesdec $rndkey0,$inout1
2170 aesdec $rndkey0,$inout2
2171 aesdec $rndkey0,$inout3
2172 aesdec $rndkey0,$inout4
2173 aesdec $rndkey0,$inout5
2174 $movkey -80($key,%rax),$rndkey0
2177 movdqa (%r8),$twmask
2178 movdqa $twres,$twtmp
2180 aesdec $rndkey1,$inout0
2181 paddq @tweak[5],@tweak[5]
2183 aesdec $rndkey1,$inout1
2185 $movkey ($key_),@tweak[0] # load round[0]
2186 aesdec $rndkey1,$inout2
2187 aesdec $rndkey1,$inout3
2188 aesdec $rndkey1,$inout4
2189 pxor $twtmp,@tweak[5]
2190 movaps @tweak[0],@tweak[1] # copy round[0]
2191 aesdec $rndkey1,$inout5
2192 $movkey -64($key),$rndkey1
2194 movdqa $twres,$twtmp
2195 aesdec $rndkey0,$inout0
2197 pxor @tweak[5],@tweak[0]
2198 aesdec $rndkey0,$inout1
2200 paddq @tweak[5],@tweak[5]
2201 aesdec $rndkey0,$inout2
2202 aesdec $rndkey0,$inout3
2204 movaps @tweak[1],@tweak[2]
2205 aesdec $rndkey0,$inout4
2206 pxor $twtmp,@tweak[5]
2207 movdqa $twres,$twtmp
2208 aesdec $rndkey0,$inout5
2209 $movkey -48($key),$rndkey0
2212 aesdec $rndkey1,$inout0
2213 pxor @tweak[5],@tweak[1]
2215 aesdec $rndkey1,$inout1
2216 paddq @tweak[5],@tweak[5]
2218 aesdec $rndkey1,$inout2
2219 aesdec $rndkey1,$inout3
2220 movdqa @tweak[3],`16*3`(%rsp)
2221 pxor $twtmp,@tweak[5]
2222 aesdec $rndkey1,$inout4
2223 movaps @tweak[2],@tweak[3]
2224 movdqa $twres,$twtmp
2225 aesdec $rndkey1,$inout5
2226 $movkey -32($key),$rndkey1
2229 aesdec $rndkey0,$inout0
2230 pxor @tweak[5],@tweak[2]
2232 aesdec $rndkey0,$inout1
2233 paddq @tweak[5],@tweak[5]
2235 aesdec $rndkey0,$inout2
2236 aesdec $rndkey0,$inout3
2237 aesdec $rndkey0,$inout4
2238 pxor $twtmp,@tweak[5]
2239 movaps @tweak[3],@tweak[4]
2240 aesdec $rndkey0,$inout5
2242 movdqa $twres,$rndkey0
2244 aesdec $rndkey1,$inout0
2245 pxor @tweak[5],@tweak[3]
2247 aesdec $rndkey1,$inout1
2248 paddq @tweak[5],@tweak[5]
2249 pand $twmask,$rndkey0
2250 aesdec $rndkey1,$inout2
2251 aesdec $rndkey1,$inout3
2252 pxor $rndkey0,@tweak[5]
2253 $movkey ($key_),$rndkey0
2254 aesdec $rndkey1,$inout4
2255 aesdec $rndkey1,$inout5
2256 $movkey 16($key_),$rndkey1
2258 pxor @tweak[5],@tweak[4]
2259 aesdeclast `16*0`(%rsp),$inout0
2261 paddq @tweak[5],@tweak[5]
2262 aesdeclast `16*1`(%rsp),$inout1
2263 aesdeclast `16*2`(%rsp),$inout2
2265 mov %r10,%rax # restore $rounds
2266 aesdeclast `16*3`(%rsp),$inout3
2267 aesdeclast `16*4`(%rsp),$inout4
2268 aesdeclast `16*5`(%rsp),$inout5
2269 pxor $twres,@tweak[5]
2271 lea `16*6`($out),$out
2272 movups $inout0,`-16*6`($out) # write output
2273 movups $inout1,`-16*5`($out)
2274 movups $inout2,`-16*4`($out)
2275 movups $inout3,`-16*3`($out)
2276 movups $inout4,`-16*2`($out)
2277 movups $inout5,`-16*1`($out)
2279 jnc .Lxts_dec_grandloop
2283 mov $key_,$key # restore $key
2284 shr \$4,$rounds # restore original value
2287 mov $rounds,$rnds_ # backup $rounds
2288 pxor $rndkey0,@tweak[0]
2289 pxor $rndkey0,@tweak[1]
2293 pxor $rndkey0,@tweak[2]
2296 pxor $rndkey0,@tweak[3]
2299 pxor $rndkey0,@tweak[4]
2304 movdqu ($inp),$inout0
2305 movdqu 16*1($inp),$inout1
2306 movdqu 16*2($inp),$inout2
2307 pxor @tweak[0],$inout0
2308 movdqu 16*3($inp),$inout3
2309 pxor @tweak[1],$inout1
2310 movdqu 16*4($inp),$inout4
2312 pxor @tweak[2],$inout2
2313 pxor @tweak[3],$inout3
2314 pxor @tweak[4],$inout4
2316 call _aesni_decrypt6
2318 xorps @tweak[0],$inout0
2319 xorps @tweak[1],$inout1
2320 xorps @tweak[2],$inout2
2321 movdqu $inout0,($out)
2322 xorps @tweak[3],$inout3
2323 movdqu $inout1,16*1($out)
2324 xorps @tweak[4],$inout4
2325 movdqu $inout2,16*2($out)
2327 movdqu $inout3,16*3($out)
2328 pcmpgtd @tweak[5],$twtmp
2329 movdqu $inout4,16*4($out)
2331 pshufd \$0x13,$twtmp,@tweak[1] # $twres
2335 movdqa @tweak[5],@tweak[0]
2336 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
2337 pand $twmask,@tweak[1] # isolate carry and residue
2338 pxor @tweak[5],@tweak[1]
2343 movups ($inp),$inout0
2345 xorps @tweak[0],$inout0
2347 &aesni_generate1("dec",$key,$rounds);
2349 xorps @tweak[0],$inout0
2350 movdqa @tweak[1],@tweak[0]
2351 movups $inout0,($out)
2352 movdqa @tweak[2],@tweak[1]
2358 movups ($inp),$inout0
2359 movups 16($inp),$inout1
2361 xorps @tweak[0],$inout0
2362 xorps @tweak[1],$inout1
2364 call _aesni_decrypt2
2366 xorps @tweak[0],$inout0
2367 movdqa @tweak[2],@tweak[0]
2368 xorps @tweak[1],$inout1
2369 movdqa @tweak[3],@tweak[1]
2370 movups $inout0,($out)
2371 movups $inout1,16*1($out)
2377 movups ($inp),$inout0
2378 movups 16*1($inp),$inout1
2379 movups 16*2($inp),$inout2
2381 xorps @tweak[0],$inout0
2382 xorps @tweak[1],$inout1
2383 xorps @tweak[2],$inout2
2385 call _aesni_decrypt3
2387 xorps @tweak[0],$inout0
2388 movdqa @tweak[3],@tweak[0]
2389 xorps @tweak[1],$inout1
2390 movdqa @tweak[4],@tweak[1]
2391 xorps @tweak[2],$inout2
2392 movups $inout0,($out)
2393 movups $inout1,16*1($out)
2394 movups $inout2,16*2($out)
2400 movups ($inp),$inout0
2401 movups 16*1($inp),$inout1
2402 movups 16*2($inp),$inout2
2403 xorps @tweak[0],$inout0
2404 movups 16*3($inp),$inout3
2406 xorps @tweak[1],$inout1
2407 xorps @tweak[2],$inout2
2408 xorps @tweak[3],$inout3
2410 call _aesni_decrypt4
2412 pxor @tweak[0],$inout0
2413 movdqa @tweak[4],@tweak[0]
2414 pxor @tweak[1],$inout1
2415 movdqa @tweak[5],@tweak[1]
2416 pxor @tweak[2],$inout2
2417 movdqu $inout0,($out)
2418 pxor @tweak[3],$inout3
2419 movdqu $inout1,16*1($out)
2420 movdqu $inout2,16*2($out)
2421 movdqu $inout3,16*3($out)
2431 mov $key_,$key # restore $key
2432 mov $rnds_,$rounds # restore $rounds
2434 movups ($inp),$inout0
2435 xorps @tweak[1],$inout0
2437 &aesni_generate1("dec",$key,$rounds);
2439 xorps @tweak[1],$inout0
2440 movups $inout0,($out)
2443 movzb 16($inp),%eax # borrow $rounds ...
2444 movzb ($out),%ecx # ... and $key
2452 sub $len_,$out # rewind $out
2453 mov $key_,$key # restore $key
2454 mov $rnds_,$rounds # restore $rounds
2456 movups ($out),$inout0
2457 xorps @tweak[0],$inout0
2459 &aesni_generate1("dec",$key,$rounds);
2461 xorps @tweak[0],$inout0
2462 movups $inout0,($out)
2466 $code.=<<___ if ($win64);
2467 movaps -0xa0(%rbp),%xmm6
2468 movaps -0x90(%rbp),%xmm7
2469 movaps -0x80(%rbp),%xmm8
2470 movaps -0x70(%rbp),%xmm9
2471 movaps -0x60(%rbp),%xmm10
2472 movaps -0x50(%rbp),%xmm11
2473 movaps -0x40(%rbp),%xmm12
2474 movaps -0x30(%rbp),%xmm13
2475 movaps -0x20(%rbp),%xmm14
2476 movaps -0x10(%rbp),%xmm15
2483 .size aesni_xts_decrypt,.-aesni_xts_decrypt
2487 ########################################################################
2488 # void $PREFIX_cbc_encrypt (const void *inp, void *out,
2489 # size_t length, const AES_KEY *key,
2490 # unsigned char *ivp,const int enc);
2492 my $frame_size = 0x10 + ($win64?0xa0:0); # used in decrypt
2493 my ($iv,$in0,$in1,$in2,$in3,$in4)=map("%xmm$_",(10..15));
2497 .globl ${PREFIX}_cbc_encrypt
2498 .type ${PREFIX}_cbc_encrypt,\@function,6
2500 ${PREFIX}_cbc_encrypt:
2501 test $len,$len # check length
2504 mov 240($key),$rnds_ # key->rounds
2505 mov $key,$key_ # backup $key
2506 test %r9d,%r9d # 6th argument
2508 #--------------------------- CBC ENCRYPT ------------------------------#
2509 movups ($ivp),$inout0 # load iv as initial state
2517 movups ($inp),$inout1 # load input
2519 #xorps $inout1,$inout0
2521 &aesni_generate1("enc",$key,$rounds,$inout0,$inout1);
2523 mov $rnds_,$rounds # restore $rounds
2524 mov $key_,$key # restore $key
2525 movups $inout0,0($out) # store output
2531 movups $inout0,($ivp)
2535 mov $len,%rcx # zaps $key
2536 xchg $inp,$out # $inp is %rsi and $out is %rdi now
2537 .long 0x9066A4F3 # rep movsb
2538 mov \$16,%ecx # zero tail
2541 .long 0x9066AAF3 # rep stosb
2542 lea -16(%rdi),%rdi # rewind $out by 1 block
2543 mov $rnds_,$rounds # restore $rounds
2544 mov %rdi,%rsi # $inp and $out are the same
2545 mov $key_,$key # restore $key
2546 xor $len,$len # len=16
2547 jmp .Lcbc_enc_loop # one more spin
2548 \f#--------------------------- CBC DECRYPT ------------------------------#
2553 sub \$$frame_size,%rsp
2554 and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
2556 $code.=<<___ if ($win64);
2557 movaps %xmm6,0x10(%rsp)
2558 movaps %xmm7,0x20(%rsp)
2559 movaps %xmm8,0x30(%rsp)
2560 movaps %xmm9,0x40(%rsp)
2561 movaps %xmm10,0x50(%rsp)
2562 movaps %xmm11,0x60(%rsp)
2563 movaps %xmm12,0x70(%rsp)
2564 movaps %xmm13,0x80(%rsp)
2565 movaps %xmm14,0x90(%rsp)
2566 movaps %xmm15,0xa0(%rsp)
2576 $movkey ($key),$rndkey0
2577 movdqu 0x00($inp),$inout0 # load input
2578 movdqu 0x10($inp),$inout1
2580 movdqu 0x20($inp),$inout2
2582 movdqu 0x30($inp),$inout3
2584 movdqu 0x40($inp),$inout4
2586 movdqu 0x50($inp),$inout5
2588 mov OPENSSL_ia32cap_P+4(%rip),%r9d
2590 jbe .Lcbc_dec_six_or_seven
2592 and \$`1<<26|1<<22`,%r9d # isolate XSAVE+MOVBE
2594 cmp \$`1<<22`,%r9d # check for MOVBE without XSAVE
2595 je .Lcbc_dec_loop6_enter
2597 lea 0x70($key),$key # size optimization
2598 jmp .Lcbc_dec_loop8_enter
2601 movups $inout7,($out)
2603 .Lcbc_dec_loop8_enter:
2604 movdqu 0x60($inp),$inout6
2605 pxor $rndkey0,$inout0
2606 movdqu 0x70($inp),$inout7
2607 pxor $rndkey0,$inout1
2608 $movkey 0x10-0x70($key),$rndkey1
2609 pxor $rndkey0,$inout2
2611 cmp \$0x70,$len # is there at least 0x60 bytes ahead?
2612 pxor $rndkey0,$inout3
2613 pxor $rndkey0,$inout4
2614 pxor $rndkey0,$inout5
2615 pxor $rndkey0,$inout6
2617 aesdec $rndkey1,$inout0
2618 pxor $rndkey0,$inout7
2619 $movkey 0x20-0x70($key),$rndkey0
2620 aesdec $rndkey1,$inout1
2621 aesdec $rndkey1,$inout2
2622 aesdec $rndkey1,$inout3
2623 aesdec $rndkey1,$inout4
2624 aesdec $rndkey1,$inout5
2625 aesdec $rndkey1,$inout6
2628 aesdec $rndkey1,$inout7
2630 $movkey 0x30-0x70($key),$rndkey1
2632 for($i=1;$i<12;$i++) {
2633 my $rndkeyx = ($i&1)?$rndkey0:$rndkey1;
2634 $code.=<<___ if ($i==7);
2638 aesdec $rndkeyx,$inout0
2639 aesdec $rndkeyx,$inout1
2640 aesdec $rndkeyx,$inout2
2641 aesdec $rndkeyx,$inout3
2642 aesdec $rndkeyx,$inout4
2643 aesdec $rndkeyx,$inout5
2644 aesdec $rndkeyx,$inout6
2645 aesdec $rndkeyx,$inout7
2646 $movkey `0x30+0x10*$i`-0x70($key),$rndkeyx
2648 $code.=<<___ if ($i<6 || (!($i&1) && $i>7));
2651 $code.=<<___ if ($i==7);
2654 $code.=<<___ if ($i==9);
2657 $code.=<<___ if ($i==11);
2664 aesdec $rndkey1,$inout0
2665 aesdec $rndkey1,$inout1
2668 aesdec $rndkey1,$inout2
2669 aesdec $rndkey1,$inout3
2672 aesdec $rndkey1,$inout4
2673 aesdec $rndkey1,$inout5
2676 aesdec $rndkey1,$inout6
2677 aesdec $rndkey1,$inout7
2678 movdqu 0x50($inp),$rndkey1
2680 aesdeclast $iv,$inout0
2681 movdqu 0x60($inp),$iv # borrow $iv
2682 pxor $rndkey0,$rndkey1
2683 aesdeclast $in0,$inout1
2685 movdqu 0x70($inp),$rndkey0 # next IV
2686 aesdeclast $in1,$inout2
2688 movdqu 0x00($inp_),$in0
2689 aesdeclast $in2,$inout3
2690 aesdeclast $in3,$inout4
2691 movdqu 0x10($inp_),$in1
2692 movdqu 0x20($inp_),$in2
2693 aesdeclast $in4,$inout5
2694 aesdeclast $rndkey1,$inout6
2695 movdqu 0x30($inp_),$in3
2696 movdqu 0x40($inp_),$in4
2697 aesdeclast $iv,$inout7
2698 movdqa $rndkey0,$iv # return $iv
2699 movdqu 0x50($inp_),$rndkey1
2700 $movkey -0x70($key),$rndkey0
2702 movups $inout0,($out) # store output
2704 movups $inout1,0x10($out)
2706 movups $inout2,0x20($out)
2708 movups $inout3,0x30($out)
2710 movups $inout4,0x40($out)
2712 movups $inout5,0x50($out)
2713 movdqa $rndkey1,$inout5
2714 movups $inout6,0x60($out)
2720 movaps $inout7,$inout0
2721 lea -0x70($key),$key
2723 jle .Lcbc_dec_tail_collected
2724 movups $inout7,($out)
2730 .Lcbc_dec_six_or_seven:
2734 movaps $inout5,$inout6
2735 call _aesni_decrypt6
2736 pxor $iv,$inout0 # ^= IV
2739 movdqu $inout0,($out)
2741 movdqu $inout1,0x10($out)
2743 movdqu $inout2,0x20($out)
2745 movdqu $inout3,0x30($out)
2747 movdqu $inout4,0x40($out)
2749 movdqa $inout5,$inout0
2750 jmp .Lcbc_dec_tail_collected
2754 movups 0x60($inp),$inout6
2755 xorps $inout7,$inout7
2756 call _aesni_decrypt8
2757 movups 0x50($inp),$inout7
2758 pxor $iv,$inout0 # ^= IV
2759 movups 0x60($inp),$iv
2761 movdqu $inout0,($out)
2763 movdqu $inout1,0x10($out)
2765 movdqu $inout2,0x20($out)
2767 movdqu $inout3,0x30($out)
2769 movdqu $inout4,0x40($out)
2770 pxor $inout7,$inout6
2771 movdqu $inout5,0x50($out)
2773 movdqa $inout6,$inout0
2774 jmp .Lcbc_dec_tail_collected
2778 movups $inout5,($out)
2780 movdqu 0x00($inp),$inout0 # load input
2781 movdqu 0x10($inp),$inout1
2783 movdqu 0x20($inp),$inout2
2785 movdqu 0x30($inp),$inout3
2787 movdqu 0x40($inp),$inout4
2789 movdqu 0x50($inp),$inout5
2791 .Lcbc_dec_loop6_enter:
2793 movdqa $inout5,$inout6
2795 call _aesni_decrypt6
2797 pxor $iv,$inout0 # ^= IV
2800 movdqu $inout0,($out)
2802 movdqu $inout1,0x10($out)
2804 movdqu $inout2,0x20($out)
2807 movdqu $inout3,0x30($out)
2810 movdqu $inout4,0x40($out)
2815 movdqa $inout5,$inout0
2817 jle .Lcbc_dec_tail_collected
2818 movups $inout5,($out)
2822 movups ($inp),$inout0
2826 movups 0x10($inp),$inout1
2831 movups 0x20($inp),$inout2
2836 movups 0x30($inp),$inout3
2841 movups 0x40($inp),$inout4
2844 xorps $inout5,$inout5
2845 call _aesni_decrypt6
2849 movdqu $inout0,($out)
2851 movdqu $inout1,0x10($out)
2853 movdqu $inout2,0x20($out)
2855 movdqu $inout3,0x30($out)
2857 movdqa $inout4,$inout0
2859 jmp .Lcbc_dec_tail_collected
2865 &aesni_generate1("dec",$key,$rounds);
2869 jmp .Lcbc_dec_tail_collected
2873 call _aesni_decrypt2
2877 movdqu $inout0,($out)
2878 movdqa $inout1,$inout0
2880 jmp .Lcbc_dec_tail_collected
2884 call _aesni_decrypt3
2888 movdqu $inout0,($out)
2890 movdqu $inout1,0x10($out)
2891 movdqa $inout2,$inout0
2893 jmp .Lcbc_dec_tail_collected
2897 call _aesni_decrypt4
2901 movdqu $inout0,($out)
2903 movdqu $inout1,0x10($out)
2905 movdqu $inout2,0x20($out)
2906 movdqa $inout3,$inout0
2908 jmp .Lcbc_dec_tail_collected
2911 .Lcbc_dec_tail_collected:
2914 jnz .Lcbc_dec_tail_partial
2915 movups $inout0,($out)
2918 .Lcbc_dec_tail_partial:
2919 movaps $inout0,(%rsp)
2924 .long 0x9066A4F3 # rep movsb
2928 $code.=<<___ if ($win64);
2929 movaps 0x10(%rsp),%xmm6
2930 movaps 0x20(%rsp),%xmm7
2931 movaps 0x30(%rsp),%xmm8
2932 movaps 0x40(%rsp),%xmm9
2933 movaps 0x50(%rsp),%xmm10
2934 movaps 0x60(%rsp),%xmm11
2935 movaps 0x70(%rsp),%xmm12
2936 movaps 0x80(%rsp),%xmm13
2937 movaps 0x90(%rsp),%xmm14
2938 movaps 0xa0(%rsp),%xmm15
2945 .size ${PREFIX}_cbc_encrypt,.-${PREFIX}_cbc_encrypt
2948 # int $PREFIX_set_[en|de]crypt_key (const unsigned char *userKey,
2949 # int bits, AES_KEY *key)
2950 { my ($inp,$bits,$key) = @_4args;
2954 .globl ${PREFIX}_set_decrypt_key
2955 .type ${PREFIX}_set_decrypt_key,\@abi-omnipotent
2957 ${PREFIX}_set_decrypt_key:
2958 .byte 0x48,0x83,0xEC,0x08 # sub rsp,8
2959 call __aesni_set_encrypt_key
2960 shl \$4,$bits # rounds-1 after _aesni_set_encrypt_key
2963 lea 16($key,$bits),$inp # points at the end of key schedule
2965 $movkey ($key),%xmm0 # just swap
2966 $movkey ($inp),%xmm1
2967 $movkey %xmm0,($inp)
2968 $movkey %xmm1,($key)
2973 $movkey ($key),%xmm0 # swap and inverse
2974 $movkey ($inp),%xmm1
2979 $movkey %xmm0,16($inp)
2980 $movkey %xmm1,-16($key)
2982 ja .Ldec_key_inverse
2984 $movkey ($key),%xmm0 # inverse middle
2986 $movkey %xmm0,($inp)
2990 .LSEH_end_set_decrypt_key:
2991 .size ${PREFIX}_set_decrypt_key,.-${PREFIX}_set_decrypt_key
2994 # This is based on submission by
2996 # Huang Ying <ying.huang@intel.com>
2997 # Vinodh Gopal <vinodh.gopal@intel.com>
3000 # Agressively optimized in respect to aeskeygenassist's critical path
3001 # and is contained in %xmm0-5 to meet Win64 ABI requirement.
3004 .globl ${PREFIX}_set_encrypt_key
3005 .type ${PREFIX}_set_encrypt_key,\@abi-omnipotent
3007 ${PREFIX}_set_encrypt_key:
3008 __aesni_set_encrypt_key:
3009 .byte 0x48,0x83,0xEC,0x08 # sub rsp,8
3016 movups ($inp),%xmm0 # pull first 128 bits of *userKey
3017 xorps %xmm4,%xmm4 # low dword of xmm4 is assumed 0
3027 mov \$9,$bits # 10 rounds for 128-bit key
3028 $movkey %xmm0,($key) # round 0
3029 aeskeygenassist \$0x1,%xmm0,%xmm1 # round 1
3030 call .Lkey_expansion_128_cold
3031 aeskeygenassist \$0x2,%xmm0,%xmm1 # round 2
3032 call .Lkey_expansion_128
3033 aeskeygenassist \$0x4,%xmm0,%xmm1 # round 3
3034 call .Lkey_expansion_128
3035 aeskeygenassist \$0x8,%xmm0,%xmm1 # round 4
3036 call .Lkey_expansion_128
3037 aeskeygenassist \$0x10,%xmm0,%xmm1 # round 5
3038 call .Lkey_expansion_128
3039 aeskeygenassist \$0x20,%xmm0,%xmm1 # round 6
3040 call .Lkey_expansion_128
3041 aeskeygenassist \$0x40,%xmm0,%xmm1 # round 7
3042 call .Lkey_expansion_128
3043 aeskeygenassist \$0x80,%xmm0,%xmm1 # round 8
3044 call .Lkey_expansion_128
3045 aeskeygenassist \$0x1b,%xmm0,%xmm1 # round 9
3046 call .Lkey_expansion_128
3047 aeskeygenassist \$0x36,%xmm0,%xmm1 # round 10
3048 call .Lkey_expansion_128
3049 $movkey %xmm0,(%rax)
3050 mov $bits,80(%rax) # 240(%rdx)
3056 movq 16($inp),%xmm2 # remaining 1/3 of *userKey
3057 mov \$11,$bits # 12 rounds for 192
3058 $movkey %xmm0,($key) # round 0
3059 aeskeygenassist \$0x1,%xmm2,%xmm1 # round 1,2
3060 call .Lkey_expansion_192a_cold
3061 aeskeygenassist \$0x2,%xmm2,%xmm1 # round 2,3
3062 call .Lkey_expansion_192b
3063 aeskeygenassist \$0x4,%xmm2,%xmm1 # round 4,5
3064 call .Lkey_expansion_192a
3065 aeskeygenassist \$0x8,%xmm2,%xmm1 # round 5,6
3066 call .Lkey_expansion_192b
3067 aeskeygenassist \$0x10,%xmm2,%xmm1 # round 7,8
3068 call .Lkey_expansion_192a
3069 aeskeygenassist \$0x20,%xmm2,%xmm1 # round 8,9
3070 call .Lkey_expansion_192b
3071 aeskeygenassist \$0x40,%xmm2,%xmm1 # round 10,11
3072 call .Lkey_expansion_192a
3073 aeskeygenassist \$0x80,%xmm2,%xmm1 # round 11,12
3074 call .Lkey_expansion_192b
3075 $movkey %xmm0,(%rax)
3076 mov $bits,48(%rax) # 240(%rdx)
3082 movups 16($inp),%xmm2 # remaning half of *userKey
3083 mov \$13,$bits # 14 rounds for 256
3085 $movkey %xmm0,($key) # round 0
3086 $movkey %xmm2,16($key) # round 1
3087 aeskeygenassist \$0x1,%xmm2,%xmm1 # round 2
3088 call .Lkey_expansion_256a_cold
3089 aeskeygenassist \$0x1,%xmm0,%xmm1 # round 3
3090 call .Lkey_expansion_256b
3091 aeskeygenassist \$0x2,%xmm2,%xmm1 # round 4
3092 call .Lkey_expansion_256a
3093 aeskeygenassist \$0x2,%xmm0,%xmm1 # round 5
3094 call .Lkey_expansion_256b
3095 aeskeygenassist \$0x4,%xmm2,%xmm1 # round 6
3096 call .Lkey_expansion_256a
3097 aeskeygenassist \$0x4,%xmm0,%xmm1 # round 7
3098 call .Lkey_expansion_256b
3099 aeskeygenassist \$0x8,%xmm2,%xmm1 # round 8
3100 call .Lkey_expansion_256a
3101 aeskeygenassist \$0x8,%xmm0,%xmm1 # round 9
3102 call .Lkey_expansion_256b
3103 aeskeygenassist \$0x10,%xmm2,%xmm1 # round 10
3104 call .Lkey_expansion_256a
3105 aeskeygenassist \$0x10,%xmm0,%xmm1 # round 11
3106 call .Lkey_expansion_256b
3107 aeskeygenassist \$0x20,%xmm2,%xmm1 # round 12
3108 call .Lkey_expansion_256a
3109 aeskeygenassist \$0x20,%xmm0,%xmm1 # round 13
3110 call .Lkey_expansion_256b
3111 aeskeygenassist \$0x40,%xmm2,%xmm1 # round 14
3112 call .Lkey_expansion_256a
3113 $movkey %xmm0,(%rax)
3114 mov $bits,16(%rax) # 240(%rdx)
3124 .LSEH_end_set_encrypt_key:
3127 .Lkey_expansion_128:
3128 $movkey %xmm0,(%rax)
3130 .Lkey_expansion_128_cold:
3131 shufps \$0b00010000,%xmm0,%xmm4
3133 shufps \$0b10001100,%xmm0,%xmm4
3135 shufps \$0b11111111,%xmm1,%xmm1 # critical path
3140 .Lkey_expansion_192a:
3141 $movkey %xmm0,(%rax)
3143 .Lkey_expansion_192a_cold:
3145 .Lkey_expansion_192b_warm:
3146 shufps \$0b00010000,%xmm0,%xmm4
3149 shufps \$0b10001100,%xmm0,%xmm4
3152 pshufd \$0b01010101,%xmm1,%xmm1 # critical path
3155 pshufd \$0b11111111,%xmm0,%xmm3
3160 .Lkey_expansion_192b:
3162 shufps \$0b01000100,%xmm0,%xmm5
3163 $movkey %xmm5,(%rax)
3164 shufps \$0b01001110,%xmm2,%xmm3
3165 $movkey %xmm3,16(%rax)
3167 jmp .Lkey_expansion_192b_warm
3170 .Lkey_expansion_256a:
3171 $movkey %xmm2,(%rax)
3173 .Lkey_expansion_256a_cold:
3174 shufps \$0b00010000,%xmm0,%xmm4
3176 shufps \$0b10001100,%xmm0,%xmm4
3178 shufps \$0b11111111,%xmm1,%xmm1 # critical path
3183 .Lkey_expansion_256b:
3184 $movkey %xmm0,(%rax)
3187 shufps \$0b00010000,%xmm2,%xmm4
3189 shufps \$0b10001100,%xmm2,%xmm4
3191 shufps \$0b10101010,%xmm1,%xmm1 # critical path
3194 .size ${PREFIX}_set_encrypt_key,.-${PREFIX}_set_encrypt_key
3195 .size __aesni_set_encrypt_key,.-__aesni_set_encrypt_key
3202 .byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
3210 .byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
3212 .asciz "AES for Intel AES-NI, CRYPTOGAMS by <appro\@openssl.org>"
3216 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
3217 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
3225 .extern __imp_RtlVirtualUnwind
3227 $code.=<<___ if ($PREFIX eq "aesni");
3228 .type ecb_se_handler,\@abi-omnipotent
3242 mov 152($context),%rax # pull context->Rsp
3244 jmp .Lcommon_seh_tail
3245 .size ecb_se_handler,.-ecb_se_handler
3247 .type ccm64_se_handler,\@abi-omnipotent
3261 mov 120($context),%rax # pull context->Rax
3262 mov 248($context),%rbx # pull context->Rip
3264 mov 8($disp),%rsi # disp->ImageBase
3265 mov 56($disp),%r11 # disp->HandlerData
3267 mov 0(%r11),%r10d # HandlerData[0]
3268 lea (%rsi,%r10),%r10 # prologue label
3269 cmp %r10,%rbx # context->Rip<prologue label
3270 jb .Lcommon_seh_tail
3272 mov 152($context),%rax # pull context->Rsp
3274 mov 4(%r11),%r10d # HandlerData[1]
3275 lea (%rsi,%r10),%r10 # epilogue label
3276 cmp %r10,%rbx # context->Rip>=epilogue label
3277 jae .Lcommon_seh_tail
3279 lea 0(%rax),%rsi # %xmm save area
3280 lea 512($context),%rdi # &context.Xmm6
3281 mov \$8,%ecx # 4*sizeof(%xmm0)/sizeof(%rax)
3282 .long 0xa548f3fc # cld; rep movsq
3283 lea 0x58(%rax),%rax # adjust stack pointer
3285 jmp .Lcommon_seh_tail
3286 .size ccm64_se_handler,.-ccm64_se_handler
3288 .type ctr_xts_se_handler,\@abi-omnipotent
3302 mov 120($context),%rax # pull context->Rax
3303 mov 248($context),%rbx # pull context->Rip
3305 mov 8($disp),%rsi # disp->ImageBase
3306 mov 56($disp),%r11 # disp->HandlerData
3308 mov 0(%r11),%r10d # HandlerData[0]
3309 lea (%rsi,%r10),%r10 # prologue lable
3310 cmp %r10,%rbx # context->Rip<prologue label
3311 jb .Lcommon_seh_tail
3313 mov 152($context),%rax # pull context->Rsp
3315 mov 4(%r11),%r10d # HandlerData[1]
3316 lea (%rsi,%r10),%r10 # epilogue label
3317 cmp %r10,%rbx # context->Rip>=epilogue label
3318 jae .Lcommon_seh_tail
3320 mov 160($context),%rax # pull context->Rbp
3321 lea -0xa0(%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
3326 jmp .Lcommon_rbp_tail
3327 .size ctr_xts_se_handler,.-ctr_xts_se_handler
3330 .type cbc_se_handler,\@abi-omnipotent
3344 mov 152($context),%rax # pull context->Rsp
3345 mov 248($context),%rbx # pull context->Rip
3347 lea .Lcbc_decrypt(%rip),%r10
3348 cmp %r10,%rbx # context->Rip<"prologue" label
3349 jb .Lcommon_seh_tail
3351 lea .Lcbc_decrypt_body(%rip),%r10
3352 cmp %r10,%rbx # context->Rip<cbc_decrypt_body
3353 jb .Lrestore_cbc_rax
3355 lea .Lcbc_ret(%rip),%r10
3356 cmp %r10,%rbx # context->Rip>="epilogue" label
3357 jae .Lcommon_seh_tail
3359 lea 16(%rax),%rsi # %xmm save area
3360 lea 512($context),%rdi # &context.Xmm6
3361 mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
3362 .long 0xa548f3fc # cld; rep movsq
3365 mov 160($context),%rax # pull context->Rbp
3366 mov (%rax),%rbp # restore saved %rbp
3367 lea 8(%rax),%rax # adjust stack pointer
3368 mov %rbp,160($context) # restore context->Rbp
3369 jmp .Lcommon_seh_tail
3372 mov 120($context),%rax
3377 mov %rax,152($context) # restore context->Rsp
3378 mov %rsi,168($context) # restore context->Rsi
3379 mov %rdi,176($context) # restore context->Rdi
3381 mov 40($disp),%rdi # disp->ContextRecord
3382 mov $context,%rsi # context
3383 mov \$154,%ecx # sizeof(CONTEXT)
3384 .long 0xa548f3fc # cld; rep movsq
3387 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
3388 mov 8(%rsi),%rdx # arg2, disp->ImageBase
3389 mov 0(%rsi),%r8 # arg3, disp->ControlPc
3390 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
3391 mov 40(%rsi),%r10 # disp->ContextRecord
3392 lea 56(%rsi),%r11 # &disp->HandlerData
3393 lea 24(%rsi),%r12 # &disp->EstablisherFrame
3394 mov %r10,32(%rsp) # arg5
3395 mov %r11,40(%rsp) # arg6
3396 mov %r12,48(%rsp) # arg7
3397 mov %rcx,56(%rsp) # arg8, (NULL)
3398 call *__imp_RtlVirtualUnwind(%rip)
3400 mov \$1,%eax # ExceptionContinueSearch
3412 .size cbc_se_handler,.-cbc_se_handler
3417 $code.=<<___ if ($PREFIX eq "aesni");
3418 .rva .LSEH_begin_aesni_ecb_encrypt
3419 .rva .LSEH_end_aesni_ecb_encrypt
3422 .rva .LSEH_begin_aesni_ccm64_encrypt_blocks
3423 .rva .LSEH_end_aesni_ccm64_encrypt_blocks
3424 .rva .LSEH_info_ccm64_enc
3426 .rva .LSEH_begin_aesni_ccm64_decrypt_blocks
3427 .rva .LSEH_end_aesni_ccm64_decrypt_blocks
3428 .rva .LSEH_info_ccm64_dec
3430 .rva .LSEH_begin_aesni_ctr32_encrypt_blocks
3431 .rva .LSEH_end_aesni_ctr32_encrypt_blocks
3432 .rva .LSEH_info_ctr32
3434 .rva .LSEH_begin_aesni_xts_encrypt
3435 .rva .LSEH_end_aesni_xts_encrypt
3436 .rva .LSEH_info_xts_enc
3438 .rva .LSEH_begin_aesni_xts_decrypt
3439 .rva .LSEH_end_aesni_xts_decrypt
3440 .rva .LSEH_info_xts_dec
3443 .rva .LSEH_begin_${PREFIX}_cbc_encrypt
3444 .rva .LSEH_end_${PREFIX}_cbc_encrypt
3447 .rva ${PREFIX}_set_decrypt_key
3448 .rva .LSEH_end_set_decrypt_key
3451 .rva ${PREFIX}_set_encrypt_key
3452 .rva .LSEH_end_set_encrypt_key
3457 $code.=<<___ if ($PREFIX eq "aesni");
3461 .LSEH_info_ccm64_enc:
3463 .rva ccm64_se_handler
3464 .rva .Lccm64_enc_body,.Lccm64_enc_ret # HandlerData[]
3465 .LSEH_info_ccm64_dec:
3467 .rva ccm64_se_handler
3468 .rva .Lccm64_dec_body,.Lccm64_dec_ret # HandlerData[]
3471 .rva ctr_xts_se_handler
3472 .rva .Lctr32_body,.Lctr32_epilogue # HandlerData[]
3475 .rva ctr_xts_se_handler
3476 .rva .Lxts_enc_body,.Lxts_enc_epilogue # HandlerData[]
3479 .rva ctr_xts_se_handler
3480 .rva .Lxts_dec_body,.Lxts_dec_epilogue # HandlerData[]
3487 .byte 0x01,0x04,0x01,0x00
3488 .byte 0x04,0x02,0x00,0x00 # sub rsp,8
3493 local *opcode=shift;
3497 $rex|=0x04 if($dst>=8);
3498 $rex|=0x01 if($src>=8);
3499 push @opcode,$rex|0x40 if($rex);
3506 if ($line=~/(aeskeygenassist)\s+\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
3507 rex(\@opcode,$4,$3);
3508 push @opcode,0x0f,0x3a,0xdf;
3509 push @opcode,0xc0|($3&7)|(($4&7)<<3); # ModR/M
3511 push @opcode,$c=~/^0/?oct($c):$c;
3512 return ".byte\t".join(',',@opcode);
3514 elsif ($line=~/(aes[a-z]+)\s+%xmm([0-9]+),\s*%xmm([0-9]+)/) {
3517 "aesenc" => 0xdc, "aesenclast" => 0xdd,
3518 "aesdec" => 0xde, "aesdeclast" => 0xdf
3520 return undef if (!defined($opcodelet{$1}));
3521 rex(\@opcode,$3,$2);
3522 push @opcode,0x0f,0x38,$opcodelet{$1};
3523 push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
3524 return ".byte\t".join(',',@opcode);
3526 elsif ($line=~/(aes[a-z]+)\s+([0x1-9a-fA-F]*)\(%rsp\),\s*%xmm([0-9]+)/) {
3528 "aesenc" => 0xdc, "aesenclast" => 0xdd,
3529 "aesdec" => 0xde, "aesdeclast" => 0xdf
3531 return undef if (!defined($opcodelet{$1}));
3533 push @opcode,0x44 if ($3>=8);
3534 push @opcode,0x0f,0x38,$opcodelet{$1};
3535 push @opcode,0x44|(($3&7)<<3),0x24; # ModR/M
3536 push @opcode,($off=~/^0/?oct($off):$off)&0xff;
3537 return ".byte\t".join(',',@opcode);
3543 ".byte 0x0f,0x38,0xf1,0x44,0x24,".shift;
3546 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
3547 $code =~ s/\b(aes.*%xmm[0-9]+).*$/aesni($1)/gem;
3548 #$code =~ s/\bmovbe\s+%eax/bswap %eax; mov %eax/gm; # debugging artefact
3549 $code =~ s/\bmovbe\s+%eax,\s*([0-9]+)\(%rsp\)/movbe($1)/gem;