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 # Skylake 2.62/0.63 0.63 0.63 0.63
169 # Silvermont 5.75/3.54 3.56 4.12 3.87(*)
170 # Bulldozer 5.77/0.70 0.72 0.90 0.70
172 # (*) Atom Silvermont ECB result is suboptimal because of penalties
173 # incurred by operations on %xmm8-15. As ECB is not considered
174 # critical, nothing was done to mitigate the problem.
176 $PREFIX="aesni"; # if $PREFIX is set to "AES", the script
177 # generates drop-in replacement for
178 # crypto/aes/asm/aes-x86_64.pl:-)
182 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
184 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
186 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
187 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
188 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
189 die "can't locate x86_64-xlate.pl";
191 open OUT,"| \"$^X\" $xlate $flavour $output";
194 $movkey = $PREFIX eq "aesni" ? "movups" : "movups";
195 @_4args=$win64? ("%rcx","%rdx","%r8", "%r9") : # Win64 order
196 ("%rdi","%rsi","%rdx","%rcx"); # Unix order
199 $code.=".extern OPENSSL_ia32cap_P\n";
201 $rounds="%eax"; # input to and changed by aesni_[en|de]cryptN !!!
202 # this is natural Unix argument order for public $PREFIX_[ecb|cbc]_encrypt ...
206 $key="%rcx"; # input to and changed by aesni_[en|de]cryptN !!!
207 $ivp="%r8"; # cbc, ctr, ...
209 $rnds_="%r10d"; # backup copy for $rounds
210 $key_="%r11"; # backup copy for $key
212 # %xmm register layout
213 $rndkey0="%xmm0"; $rndkey1="%xmm1";
214 $inout0="%xmm2"; $inout1="%xmm3";
215 $inout2="%xmm4"; $inout3="%xmm5";
216 $inout4="%xmm6"; $inout5="%xmm7";
217 $inout6="%xmm8"; $inout7="%xmm9";
219 $in2="%xmm6"; $in1="%xmm7"; # used in CBC decrypt, CTR, ...
220 $in0="%xmm8"; $iv="%xmm9";
222 # Inline version of internal aesni_[en|de]crypt1.
224 # Why folded loop? Because aes[enc|dec] is slow enough to accommodate
225 # cycles which take care of loop variables...
227 sub aesni_generate1 {
228 my ($p,$key,$rounds,$inout,$ivec)=@_; $inout=$inout0 if (!defined($inout));
231 $movkey ($key),$rndkey0
232 $movkey 16($key),$rndkey1
234 $code.=<<___ if (defined($ivec));
239 $code.=<<___ if (!defined($ivec));
241 xorps $rndkey0,$inout
245 aes${p} $rndkey1,$inout
247 $movkey ($key),$rndkey1
249 jnz .Loop_${p}1_$sn # loop body is 16 bytes
250 aes${p}last $rndkey1,$inout
253 # void $PREFIX_[en|de]crypt (const void *inp,void *out,const AES_KEY *key);
255 { my ($inp,$out,$key) = @_4args;
258 .globl ${PREFIX}_encrypt
259 .type ${PREFIX}_encrypt,\@abi-omnipotent
262 movups ($inp),$inout0 # load input
263 mov 240($key),$rounds # key->rounds
265 &aesni_generate1("enc",$key,$rounds);
267 pxor $rndkey0,$rndkey0 # clear register bank
268 pxor $rndkey1,$rndkey1
269 movups $inout0,($out) # output
272 .size ${PREFIX}_encrypt,.-${PREFIX}_encrypt
274 .globl ${PREFIX}_decrypt
275 .type ${PREFIX}_decrypt,\@abi-omnipotent
278 movups ($inp),$inout0 # load input
279 mov 240($key),$rounds # key->rounds
281 &aesni_generate1("dec",$key,$rounds);
283 pxor $rndkey0,$rndkey0 # clear register bank
284 pxor $rndkey1,$rndkey1
285 movups $inout0,($out) # output
288 .size ${PREFIX}_decrypt, .-${PREFIX}_decrypt
292 # _aesni_[en|de]cryptN are private interfaces, N denotes interleave
293 # factor. Why 3x subroutine were originally used in loops? Even though
294 # aes[enc|dec] latency was originally 6, it could be scheduled only
295 # every *2nd* cycle. Thus 3x interleave was the one providing optimal
296 # utilization, i.e. when subroutine's throughput is virtually same as
297 # of non-interleaved subroutine [for number of input blocks up to 3].
298 # This is why it originally made no sense to implement 2x subroutine.
299 # But times change and it became appropriate to spend extra 192 bytes
300 # on 2x subroutine on Atom Silvermont account. For processors that
301 # can schedule aes[enc|dec] every cycle optimal interleave factor
302 # equals to corresponding instructions latency. 8x is optimal for
303 # * Bridge and "super-optimal" for other Intel CPUs...
305 sub aesni_generate2 {
307 # As already mentioned it takes in $key and $rounds, which are *not*
308 # preserved. $inout[0-1] is cipher/clear text...
310 .type _aesni_${dir}rypt2,\@abi-omnipotent
313 $movkey ($key),$rndkey0
315 $movkey 16($key),$rndkey1
316 xorps $rndkey0,$inout0
317 xorps $rndkey0,$inout1
318 $movkey 32($key),$rndkey0
319 lea 32($key,$rounds),$key
324 aes${dir} $rndkey1,$inout0
325 aes${dir} $rndkey1,$inout1
326 $movkey ($key,%rax),$rndkey1
328 aes${dir} $rndkey0,$inout0
329 aes${dir} $rndkey0,$inout1
330 $movkey -16($key,%rax),$rndkey0
333 aes${dir} $rndkey1,$inout0
334 aes${dir} $rndkey1,$inout1
335 aes${dir}last $rndkey0,$inout0
336 aes${dir}last $rndkey0,$inout1
338 .size _aesni_${dir}rypt2,.-_aesni_${dir}rypt2
341 sub aesni_generate3 {
343 # As already mentioned it takes in $key and $rounds, which are *not*
344 # preserved. $inout[0-2] is cipher/clear text...
346 .type _aesni_${dir}rypt3,\@abi-omnipotent
349 $movkey ($key),$rndkey0
351 $movkey 16($key),$rndkey1
352 xorps $rndkey0,$inout0
353 xorps $rndkey0,$inout1
354 xorps $rndkey0,$inout2
355 $movkey 32($key),$rndkey0
356 lea 32($key,$rounds),$key
361 aes${dir} $rndkey1,$inout0
362 aes${dir} $rndkey1,$inout1
363 aes${dir} $rndkey1,$inout2
364 $movkey ($key,%rax),$rndkey1
366 aes${dir} $rndkey0,$inout0
367 aes${dir} $rndkey0,$inout1
368 aes${dir} $rndkey0,$inout2
369 $movkey -16($key,%rax),$rndkey0
372 aes${dir} $rndkey1,$inout0
373 aes${dir} $rndkey1,$inout1
374 aes${dir} $rndkey1,$inout2
375 aes${dir}last $rndkey0,$inout0
376 aes${dir}last $rndkey0,$inout1
377 aes${dir}last $rndkey0,$inout2
379 .size _aesni_${dir}rypt3,.-_aesni_${dir}rypt3
382 # 4x interleave is implemented to improve small block performance,
383 # most notably [and naturally] 4 block by ~30%. One can argue that one
384 # should have implemented 5x as well, but improvement would be <20%,
385 # so it's not worth it...
386 sub aesni_generate4 {
388 # As already mentioned it takes in $key and $rounds, which are *not*
389 # preserved. $inout[0-3] is cipher/clear text...
391 .type _aesni_${dir}rypt4,\@abi-omnipotent
394 $movkey ($key),$rndkey0
396 $movkey 16($key),$rndkey1
397 xorps $rndkey0,$inout0
398 xorps $rndkey0,$inout1
399 xorps $rndkey0,$inout2
400 xorps $rndkey0,$inout3
401 $movkey 32($key),$rndkey0
402 lea 32($key,$rounds),$key
408 aes${dir} $rndkey1,$inout0
409 aes${dir} $rndkey1,$inout1
410 aes${dir} $rndkey1,$inout2
411 aes${dir} $rndkey1,$inout3
412 $movkey ($key,%rax),$rndkey1
414 aes${dir} $rndkey0,$inout0
415 aes${dir} $rndkey0,$inout1
416 aes${dir} $rndkey0,$inout2
417 aes${dir} $rndkey0,$inout3
418 $movkey -16($key,%rax),$rndkey0
421 aes${dir} $rndkey1,$inout0
422 aes${dir} $rndkey1,$inout1
423 aes${dir} $rndkey1,$inout2
424 aes${dir} $rndkey1,$inout3
425 aes${dir}last $rndkey0,$inout0
426 aes${dir}last $rndkey0,$inout1
427 aes${dir}last $rndkey0,$inout2
428 aes${dir}last $rndkey0,$inout3
430 .size _aesni_${dir}rypt4,.-_aesni_${dir}rypt4
433 sub aesni_generate6 {
435 # As already mentioned it takes in $key and $rounds, which are *not*
436 # preserved. $inout[0-5] is cipher/clear text...
438 .type _aesni_${dir}rypt6,\@abi-omnipotent
441 $movkey ($key),$rndkey0
443 $movkey 16($key),$rndkey1
444 xorps $rndkey0,$inout0
445 pxor $rndkey0,$inout1
446 pxor $rndkey0,$inout2
447 aes${dir} $rndkey1,$inout0
448 lea 32($key,$rounds),$key
450 aes${dir} $rndkey1,$inout1
451 pxor $rndkey0,$inout3
452 pxor $rndkey0,$inout4
453 aes${dir} $rndkey1,$inout2
454 pxor $rndkey0,$inout5
455 $movkey ($key,%rax),$rndkey0
457 jmp .L${dir}_loop6_enter
460 aes${dir} $rndkey1,$inout0
461 aes${dir} $rndkey1,$inout1
462 aes${dir} $rndkey1,$inout2
463 .L${dir}_loop6_enter:
464 aes${dir} $rndkey1,$inout3
465 aes${dir} $rndkey1,$inout4
466 aes${dir} $rndkey1,$inout5
467 $movkey ($key,%rax),$rndkey1
469 aes${dir} $rndkey0,$inout0
470 aes${dir} $rndkey0,$inout1
471 aes${dir} $rndkey0,$inout2
472 aes${dir} $rndkey0,$inout3
473 aes${dir} $rndkey0,$inout4
474 aes${dir} $rndkey0,$inout5
475 $movkey -16($key,%rax),$rndkey0
478 aes${dir} $rndkey1,$inout0
479 aes${dir} $rndkey1,$inout1
480 aes${dir} $rndkey1,$inout2
481 aes${dir} $rndkey1,$inout3
482 aes${dir} $rndkey1,$inout4
483 aes${dir} $rndkey1,$inout5
484 aes${dir}last $rndkey0,$inout0
485 aes${dir}last $rndkey0,$inout1
486 aes${dir}last $rndkey0,$inout2
487 aes${dir}last $rndkey0,$inout3
488 aes${dir}last $rndkey0,$inout4
489 aes${dir}last $rndkey0,$inout5
491 .size _aesni_${dir}rypt6,.-_aesni_${dir}rypt6
494 sub aesni_generate8 {
496 # As already mentioned it takes in $key and $rounds, which are *not*
497 # preserved. $inout[0-7] is cipher/clear text...
499 .type _aesni_${dir}rypt8,\@abi-omnipotent
502 $movkey ($key),$rndkey0
504 $movkey 16($key),$rndkey1
505 xorps $rndkey0,$inout0
506 xorps $rndkey0,$inout1
507 pxor $rndkey0,$inout2
508 pxor $rndkey0,$inout3
509 pxor $rndkey0,$inout4
510 lea 32($key,$rounds),$key
512 aes${dir} $rndkey1,$inout0
513 pxor $rndkey0,$inout5
514 pxor $rndkey0,$inout6
515 aes${dir} $rndkey1,$inout1
516 pxor $rndkey0,$inout7
517 $movkey ($key,%rax),$rndkey0
519 jmp .L${dir}_loop8_inner
522 aes${dir} $rndkey1,$inout0
523 aes${dir} $rndkey1,$inout1
524 .L${dir}_loop8_inner:
525 aes${dir} $rndkey1,$inout2
526 aes${dir} $rndkey1,$inout3
527 aes${dir} $rndkey1,$inout4
528 aes${dir} $rndkey1,$inout5
529 aes${dir} $rndkey1,$inout6
530 aes${dir} $rndkey1,$inout7
531 .L${dir}_loop8_enter:
532 $movkey ($key,%rax),$rndkey1
534 aes${dir} $rndkey0,$inout0
535 aes${dir} $rndkey0,$inout1
536 aes${dir} $rndkey0,$inout2
537 aes${dir} $rndkey0,$inout3
538 aes${dir} $rndkey0,$inout4
539 aes${dir} $rndkey0,$inout5
540 aes${dir} $rndkey0,$inout6
541 aes${dir} $rndkey0,$inout7
542 $movkey -16($key,%rax),$rndkey0
545 aes${dir} $rndkey1,$inout0
546 aes${dir} $rndkey1,$inout1
547 aes${dir} $rndkey1,$inout2
548 aes${dir} $rndkey1,$inout3
549 aes${dir} $rndkey1,$inout4
550 aes${dir} $rndkey1,$inout5
551 aes${dir} $rndkey1,$inout6
552 aes${dir} $rndkey1,$inout7
553 aes${dir}last $rndkey0,$inout0
554 aes${dir}last $rndkey0,$inout1
555 aes${dir}last $rndkey0,$inout2
556 aes${dir}last $rndkey0,$inout3
557 aes${dir}last $rndkey0,$inout4
558 aes${dir}last $rndkey0,$inout5
559 aes${dir}last $rndkey0,$inout6
560 aes${dir}last $rndkey0,$inout7
562 .size _aesni_${dir}rypt8,.-_aesni_${dir}rypt8
565 &aesni_generate2("enc") if ($PREFIX eq "aesni");
566 &aesni_generate2("dec");
567 &aesni_generate3("enc") if ($PREFIX eq "aesni");
568 &aesni_generate3("dec");
569 &aesni_generate4("enc") if ($PREFIX eq "aesni");
570 &aesni_generate4("dec");
571 &aesni_generate6("enc") if ($PREFIX eq "aesni");
572 &aesni_generate6("dec");
573 &aesni_generate8("enc") if ($PREFIX eq "aesni");
574 &aesni_generate8("dec");
576 if ($PREFIX eq "aesni") {
577 ########################################################################
578 # void aesni_ecb_encrypt (const void *in, void *out,
579 # size_t length, const AES_KEY *key,
582 .globl aesni_ecb_encrypt
583 .type aesni_ecb_encrypt,\@function,5
587 $code.=<<___ if ($win64);
589 movaps %xmm6,(%rsp) # offload $inout4..7
590 movaps %xmm7,0x10(%rsp)
591 movaps %xmm8,0x20(%rsp)
592 movaps %xmm9,0x30(%rsp)
596 and \$-16,$len # if ($len<16)
597 jz .Lecb_ret # return
599 mov 240($key),$rounds # key->rounds
600 $movkey ($key),$rndkey0
601 mov $key,$key_ # backup $key
602 mov $rounds,$rnds_ # backup $rounds
603 test %r8d,%r8d # 5th argument
605 #--------------------------- ECB ENCRYPT ------------------------------#
606 cmp \$0x80,$len # if ($len<8*16)
607 jb .Lecb_enc_tail # short input
609 movdqu ($inp),$inout0 # load 8 input blocks
610 movdqu 0x10($inp),$inout1
611 movdqu 0x20($inp),$inout2
612 movdqu 0x30($inp),$inout3
613 movdqu 0x40($inp),$inout4
614 movdqu 0x50($inp),$inout5
615 movdqu 0x60($inp),$inout6
616 movdqu 0x70($inp),$inout7
617 lea 0x80($inp),$inp # $inp+=8*16
618 sub \$0x80,$len # $len-=8*16 (can be zero)
619 jmp .Lecb_enc_loop8_enter
622 movups $inout0,($out) # store 8 output blocks
623 mov $key_,$key # restore $key
624 movdqu ($inp),$inout0 # load 8 input blocks
625 mov $rnds_,$rounds # restore $rounds
626 movups $inout1,0x10($out)
627 movdqu 0x10($inp),$inout1
628 movups $inout2,0x20($out)
629 movdqu 0x20($inp),$inout2
630 movups $inout3,0x30($out)
631 movdqu 0x30($inp),$inout3
632 movups $inout4,0x40($out)
633 movdqu 0x40($inp),$inout4
634 movups $inout5,0x50($out)
635 movdqu 0x50($inp),$inout5
636 movups $inout6,0x60($out)
637 movdqu 0x60($inp),$inout6
638 movups $inout7,0x70($out)
639 lea 0x80($out),$out # $out+=8*16
640 movdqu 0x70($inp),$inout7
641 lea 0x80($inp),$inp # $inp+=8*16
642 .Lecb_enc_loop8_enter:
647 jnc .Lecb_enc_loop8 # loop if $len-=8*16 didn't borrow
649 movups $inout0,($out) # store 8 output blocks
650 mov $key_,$key # restore $key
651 movups $inout1,0x10($out)
652 mov $rnds_,$rounds # restore $rounds
653 movups $inout2,0x20($out)
654 movups $inout3,0x30($out)
655 movups $inout4,0x40($out)
656 movups $inout5,0x50($out)
657 movups $inout6,0x60($out)
658 movups $inout7,0x70($out)
659 lea 0x80($out),$out # $out+=8*16
660 add \$0x80,$len # restore real remaining $len
661 jz .Lecb_ret # done if ($len==0)
663 .Lecb_enc_tail: # $len is less than 8*16
664 movups ($inp),$inout0
667 movups 0x10($inp),$inout1
669 movups 0x20($inp),$inout2
672 movups 0x30($inp),$inout3
674 movups 0x40($inp),$inout4
677 movups 0x50($inp),$inout5
679 movdqu 0x60($inp),$inout6
680 xorps $inout7,$inout7
682 movups $inout0,($out) # store 7 output blocks
683 movups $inout1,0x10($out)
684 movups $inout2,0x20($out)
685 movups $inout3,0x30($out)
686 movups $inout4,0x40($out)
687 movups $inout5,0x50($out)
688 movups $inout6,0x60($out)
693 &aesni_generate1("enc",$key,$rounds);
695 movups $inout0,($out) # store one output block
700 movups $inout0,($out) # store 2 output blocks
701 movups $inout1,0x10($out)
706 movups $inout0,($out) # store 3 output blocks
707 movups $inout1,0x10($out)
708 movups $inout2,0x20($out)
713 movups $inout0,($out) # store 4 output blocks
714 movups $inout1,0x10($out)
715 movups $inout2,0x20($out)
716 movups $inout3,0x30($out)
720 xorps $inout5,$inout5
722 movups $inout0,($out) # store 5 output blocks
723 movups $inout1,0x10($out)
724 movups $inout2,0x20($out)
725 movups $inout3,0x30($out)
726 movups $inout4,0x40($out)
731 movups $inout0,($out) # store 6 output blocks
732 movups $inout1,0x10($out)
733 movups $inout2,0x20($out)
734 movups $inout3,0x30($out)
735 movups $inout4,0x40($out)
736 movups $inout5,0x50($out)
738 \f#--------------------------- ECB DECRYPT ------------------------------#
741 cmp \$0x80,$len # if ($len<8*16)
742 jb .Lecb_dec_tail # short input
744 movdqu ($inp),$inout0 # load 8 input blocks
745 movdqu 0x10($inp),$inout1
746 movdqu 0x20($inp),$inout2
747 movdqu 0x30($inp),$inout3
748 movdqu 0x40($inp),$inout4
749 movdqu 0x50($inp),$inout5
750 movdqu 0x60($inp),$inout6
751 movdqu 0x70($inp),$inout7
752 lea 0x80($inp),$inp # $inp+=8*16
753 sub \$0x80,$len # $len-=8*16 (can be zero)
754 jmp .Lecb_dec_loop8_enter
757 movups $inout0,($out) # store 8 output blocks
758 mov $key_,$key # restore $key
759 movdqu ($inp),$inout0 # load 8 input blocks
760 mov $rnds_,$rounds # restore $rounds
761 movups $inout1,0x10($out)
762 movdqu 0x10($inp),$inout1
763 movups $inout2,0x20($out)
764 movdqu 0x20($inp),$inout2
765 movups $inout3,0x30($out)
766 movdqu 0x30($inp),$inout3
767 movups $inout4,0x40($out)
768 movdqu 0x40($inp),$inout4
769 movups $inout5,0x50($out)
770 movdqu 0x50($inp),$inout5
771 movups $inout6,0x60($out)
772 movdqu 0x60($inp),$inout6
773 movups $inout7,0x70($out)
774 lea 0x80($out),$out # $out+=8*16
775 movdqu 0x70($inp),$inout7
776 lea 0x80($inp),$inp # $inp+=8*16
777 .Lecb_dec_loop8_enter:
781 $movkey ($key_),$rndkey0
783 jnc .Lecb_dec_loop8 # loop if $len-=8*16 didn't borrow
785 movups $inout0,($out) # store 8 output blocks
786 pxor $inout0,$inout0 # clear register bank
787 mov $key_,$key # restore $key
788 movups $inout1,0x10($out)
790 mov $rnds_,$rounds # restore $rounds
791 movups $inout2,0x20($out)
793 movups $inout3,0x30($out)
795 movups $inout4,0x40($out)
797 movups $inout5,0x50($out)
799 movups $inout6,0x60($out)
801 movups $inout7,0x70($out)
803 lea 0x80($out),$out # $out+=8*16
804 add \$0x80,$len # restore real remaining $len
805 jz .Lecb_ret # done if ($len==0)
808 movups ($inp),$inout0
811 movups 0x10($inp),$inout1
813 movups 0x20($inp),$inout2
816 movups 0x30($inp),$inout3
818 movups 0x40($inp),$inout4
821 movups 0x50($inp),$inout5
823 movups 0x60($inp),$inout6
824 $movkey ($key),$rndkey0
825 xorps $inout7,$inout7
827 movups $inout0,($out) # store 7 output blocks
828 pxor $inout0,$inout0 # clear register bank
829 movups $inout1,0x10($out)
831 movups $inout2,0x20($out)
833 movups $inout3,0x30($out)
835 movups $inout4,0x40($out)
837 movups $inout5,0x50($out)
839 movups $inout6,0x60($out)
846 &aesni_generate1("dec",$key,$rounds);
848 movups $inout0,($out) # store one output block
849 pxor $inout0,$inout0 # clear register bank
854 movups $inout0,($out) # store 2 output blocks
855 pxor $inout0,$inout0 # clear register bank
856 movups $inout1,0x10($out)
862 movups $inout0,($out) # store 3 output blocks
863 pxor $inout0,$inout0 # clear register bank
864 movups $inout1,0x10($out)
866 movups $inout2,0x20($out)
872 movups $inout0,($out) # store 4 output blocks
873 pxor $inout0,$inout0 # clear register bank
874 movups $inout1,0x10($out)
876 movups $inout2,0x20($out)
878 movups $inout3,0x30($out)
883 xorps $inout5,$inout5
885 movups $inout0,($out) # store 5 output blocks
886 pxor $inout0,$inout0 # clear register bank
887 movups $inout1,0x10($out)
889 movups $inout2,0x20($out)
891 movups $inout3,0x30($out)
893 movups $inout4,0x40($out)
900 movups $inout0,($out) # store 6 output blocks
901 pxor $inout0,$inout0 # clear register bank
902 movups $inout1,0x10($out)
904 movups $inout2,0x20($out)
906 movups $inout3,0x30($out)
908 movups $inout4,0x40($out)
910 movups $inout5,0x50($out)
914 xorps $rndkey0,$rndkey0 # %xmm0
915 pxor $rndkey1,$rndkey1
917 $code.=<<___ if ($win64);
919 movaps %xmm0,(%rsp) # clear stack
920 movaps 0x10(%rsp),%xmm7
921 movaps %xmm0,0x10(%rsp)
922 movaps 0x20(%rsp),%xmm8
923 movaps %xmm0,0x20(%rsp)
924 movaps 0x30(%rsp),%xmm9
925 movaps %xmm0,0x30(%rsp)
931 .size aesni_ecb_encrypt,.-aesni_ecb_encrypt
935 ######################################################################
936 # void aesni_ccm64_[en|de]crypt_blocks (const void *in, void *out,
937 # size_t blocks, const AES_KEY *key,
938 # const char *ivec,char *cmac);
940 # Handles only complete blocks, operates on 64-bit counter and
941 # does not update *ivec! Nor does it finalize CMAC value
942 # (see engine/eng_aesni.c for details)
945 my $cmac="%r9"; # 6th argument
947 my $increment="%xmm9";
949 my $bswap_mask="%xmm7";
952 .globl aesni_ccm64_encrypt_blocks
953 .type aesni_ccm64_encrypt_blocks,\@function,6
955 aesni_ccm64_encrypt_blocks:
957 $code.=<<___ if ($win64);
959 movaps %xmm6,(%rsp) # $iv
960 movaps %xmm7,0x10(%rsp) # $bswap_mask
961 movaps %xmm8,0x20(%rsp) # $in0
962 movaps %xmm9,0x30(%rsp) # $increment
966 mov 240($key),$rounds # key->rounds
968 movdqa .Lincrement64(%rip),$increment
969 movdqa .Lbswap_mask(%rip),$bswap_mask
974 movdqu ($cmac),$inout1
976 lea 32($key,$rounds),$key # end of key schedule
977 pshufb $bswap_mask,$iv
978 sub %rax,%r10 # twisted $rounds
979 jmp .Lccm64_enc_outer
982 $movkey ($key_),$rndkey0
984 movups ($inp),$in0 # load inp
986 xorps $rndkey0,$inout0 # counter
987 $movkey 16($key_),$rndkey1
989 xorps $rndkey0,$inout1 # cmac^=inp
990 $movkey 32($key_),$rndkey0
993 aesenc $rndkey1,$inout0
994 aesenc $rndkey1,$inout1
995 $movkey ($key,%rax),$rndkey1
997 aesenc $rndkey0,$inout0
998 aesenc $rndkey0,$inout1
999 $movkey -16($key,%rax),$rndkey0
1000 jnz .Lccm64_enc2_loop
1001 aesenc $rndkey1,$inout0
1002 aesenc $rndkey1,$inout1
1003 paddq $increment,$iv
1004 dec $len # $len-- ($len is in blocks)
1005 aesenclast $rndkey0,$inout0
1006 aesenclast $rndkey0,$inout1
1009 xorps $inout0,$in0 # inp ^= E(iv)
1011 movups $in0,($out) # save output
1012 pshufb $bswap_mask,$inout0
1013 lea 16($out),$out # $out+=16
1014 jnz .Lccm64_enc_outer # loop if ($len!=0)
1016 pxor $rndkey0,$rndkey0 # clear register bank
1017 pxor $rndkey1,$rndkey1
1018 pxor $inout0,$inout0
1019 movups $inout1,($cmac) # store resulting mac
1020 pxor $inout1,$inout1
1024 $code.=<<___ if ($win64);
1026 movaps %xmm0,(%rsp) # clear stack
1027 movaps 0x10(%rsp),%xmm7
1028 movaps %xmm0,0x10(%rsp)
1029 movaps 0x20(%rsp),%xmm8
1030 movaps %xmm0,0x20(%rsp)
1031 movaps 0x30(%rsp),%xmm9
1032 movaps %xmm0,0x30(%rsp)
1038 .size aesni_ccm64_encrypt_blocks,.-aesni_ccm64_encrypt_blocks
1040 ######################################################################
1042 .globl aesni_ccm64_decrypt_blocks
1043 .type aesni_ccm64_decrypt_blocks,\@function,6
1045 aesni_ccm64_decrypt_blocks:
1047 $code.=<<___ if ($win64);
1048 lea -0x58(%rsp),%rsp
1049 movaps %xmm6,(%rsp) # $iv
1050 movaps %xmm7,0x10(%rsp) # $bswap_mask
1051 movaps %xmm8,0x20(%rsp) # $in8
1052 movaps %xmm9,0x30(%rsp) # $increment
1056 mov 240($key),$rounds # key->rounds
1058 movdqu ($cmac),$inout1
1059 movdqa .Lincrement64(%rip),$increment
1060 movdqa .Lbswap_mask(%rip),$bswap_mask
1065 pshufb $bswap_mask,$iv
1067 &aesni_generate1("enc",$key,$rounds);
1071 movups ($inp),$in0 # load inp
1072 paddq $increment,$iv
1073 lea 16($inp),$inp # $inp+=16
1074 sub %r10,%rax # twisted $rounds
1075 lea 32($key_,$rnds_),$key # end of key schedule
1077 jmp .Lccm64_dec_outer
1080 xorps $inout0,$in0 # inp ^= E(iv)
1082 movups $in0,($out) # save output
1083 lea 16($out),$out # $out+=16
1084 pshufb $bswap_mask,$inout0
1086 sub \$1,$len # $len-- ($len is in blocks)
1087 jz .Lccm64_dec_break # if ($len==0) break
1089 $movkey ($key_),$rndkey0
1091 $movkey 16($key_),$rndkey1
1093 xorps $rndkey0,$inout0
1094 xorps $in0,$inout1 # cmac^=out
1095 $movkey 32($key_),$rndkey0
1096 jmp .Lccm64_dec2_loop
1099 aesenc $rndkey1,$inout0
1100 aesenc $rndkey1,$inout1
1101 $movkey ($key,%rax),$rndkey1
1103 aesenc $rndkey0,$inout0
1104 aesenc $rndkey0,$inout1
1105 $movkey -16($key,%rax),$rndkey0
1106 jnz .Lccm64_dec2_loop
1107 movups ($inp),$in0 # load input
1108 paddq $increment,$iv
1109 aesenc $rndkey1,$inout0
1110 aesenc $rndkey1,$inout1
1111 aesenclast $rndkey0,$inout0
1112 aesenclast $rndkey0,$inout1
1113 lea 16($inp),$inp # $inp+=16
1114 jmp .Lccm64_dec_outer
1118 #xorps $in0,$inout1 # cmac^=out
1119 mov 240($key_),$rounds
1121 &aesni_generate1("enc",$key_,$rounds,$inout1,$in0);
1123 pxor $rndkey0,$rndkey0 # clear register bank
1124 pxor $rndkey1,$rndkey1
1125 pxor $inout0,$inout0
1126 movups $inout1,($cmac) # store resulting mac
1127 pxor $inout1,$inout1
1131 $code.=<<___ if ($win64);
1133 movaps %xmm0,(%rsp) # clear stack
1134 movaps 0x10(%rsp),%xmm7
1135 movaps %xmm0,0x10(%rsp)
1136 movaps 0x20(%rsp),%xmm8
1137 movaps %xmm0,0x20(%rsp)
1138 movaps 0x30(%rsp),%xmm9
1139 movaps %xmm0,0x30(%rsp)
1145 .size aesni_ccm64_decrypt_blocks,.-aesni_ccm64_decrypt_blocks
1148 ######################################################################
1149 # void aesni_ctr32_encrypt_blocks (const void *in, void *out,
1150 # size_t blocks, const AES_KEY *key,
1151 # const char *ivec);
1153 # Handles only complete blocks, operates on 32-bit counter and
1154 # does not update *ivec! (see crypto/modes/ctr128.c for details)
1156 # Overhaul based on suggestions from Shay Gueron and Vlad Krasnov,
1157 # http://rt.openssl.org/Ticket/Display.html?id=3021&user=guest&pass=guest.
1158 # Keywords are full unroll and modulo-schedule counter calculations
1159 # with zero-round key xor.
1161 my ($in0,$in1,$in2,$in3,$in4,$in5)=map("%xmm$_",(10..15));
1162 my ($key0,$ctr)=("${key_}d","${ivp}d");
1163 my $frame_size = 0x80 + ($win64?160:0);
1166 .globl aesni_ctr32_encrypt_blocks
1167 .type aesni_ctr32_encrypt_blocks,\@function,5
1169 aesni_ctr32_encrypt_blocks:
1173 # handle single block without allocating stack frame,
1174 # useful when handling edges
1175 movups ($ivp),$inout0
1176 movups ($inp),$inout1
1177 mov 240($key),%edx # key->rounds
1179 &aesni_generate1("enc",$key,"%edx");
1181 pxor $rndkey0,$rndkey0 # clear register bank
1182 pxor $rndkey1,$rndkey1
1183 xorps $inout1,$inout0
1184 pxor $inout1,$inout1
1185 movups $inout0,($out)
1186 xorps $inout0,$inout0
1187 jmp .Lctr32_epilogue
1193 sub \$$frame_size,%rsp
1194 and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
1196 $code.=<<___ if ($win64);
1197 movaps %xmm6,-0xa8(%rax) # offload everything
1198 movaps %xmm7,-0x98(%rax)
1199 movaps %xmm8,-0x88(%rax)
1200 movaps %xmm9,-0x78(%rax)
1201 movaps %xmm10,-0x68(%rax)
1202 movaps %xmm11,-0x58(%rax)
1203 movaps %xmm12,-0x48(%rax)
1204 movaps %xmm13,-0x38(%rax)
1205 movaps %xmm14,-0x28(%rax)
1206 movaps %xmm15,-0x18(%rax)
1212 # 8 16-byte words on top of stack are counter values
1213 # xor-ed with zero-round key
1215 movdqu ($ivp),$inout0
1216 movdqu ($key),$rndkey0
1217 mov 12($ivp),$ctr # counter LSB
1218 pxor $rndkey0,$inout0
1219 mov 12($key),$key0 # 0-round key LSB
1220 movdqa $inout0,0x00(%rsp) # populate counter block
1222 movdqa $inout0,$inout1
1223 movdqa $inout0,$inout2
1224 movdqa $inout0,$inout3
1225 movdqa $inout0,0x40(%rsp)
1226 movdqa $inout0,0x50(%rsp)
1227 movdqa $inout0,0x60(%rsp)
1228 mov %rdx,%r10 # about to borrow %rdx
1229 movdqa $inout0,0x70(%rsp)
1237 pinsrd \$3,%eax,$inout1
1239 movdqa $inout1,0x10(%rsp)
1240 pinsrd \$3,%edx,$inout2
1242 mov %r10,%rdx # restore %rdx
1244 movdqa $inout2,0x20(%rsp)
1247 pinsrd \$3,%eax,$inout3
1249 movdqa $inout3,0x30(%rsp)
1251 mov %r10d,0x40+12(%rsp)
1254 mov 240($key),$rounds # key->rounds
1257 mov %r9d,0x50+12(%rsp)
1260 mov %r10d,0x60+12(%rsp)
1262 mov OPENSSL_ia32cap_P+4(%rip),%r10d
1264 and \$`1<<26|1<<22`,%r10d # isolate XSAVE+MOVBE
1265 mov %r9d,0x70+12(%rsp)
1267 $movkey 0x10($key),$rndkey1
1269 movdqa 0x40(%rsp),$inout4
1270 movdqa 0x50(%rsp),$inout5
1272 cmp \$8,$len # $len is in blocks
1273 jb .Lctr32_tail # short input if ($len<8)
1275 sub \$6,$len # $len is biased by -6
1276 cmp \$`1<<22`,%r10d # check for MOVBE without XSAVE
1277 je .Lctr32_6x # [which denotes Atom Silvermont]
1279 lea 0x80($key),$key # size optimization
1280 sub \$2,$len # $len is biased by -8
1288 lea 32($key,$rounds),$key # end of key schedule
1289 sub %rax,%r10 # twisted $rounds
1294 add \$6,$ctr # next counter value
1295 $movkey -48($key,$rnds_),$rndkey0
1296 aesenc $rndkey1,$inout0
1299 aesenc $rndkey1,$inout1
1300 movbe %eax,`0x00+12`(%rsp) # store next counter value
1302 aesenc $rndkey1,$inout2
1304 movbe %eax,`0x10+12`(%rsp)
1305 aesenc $rndkey1,$inout3
1308 aesenc $rndkey1,$inout4
1309 movbe %eax,`0x20+12`(%rsp)
1311 aesenc $rndkey1,$inout5
1312 $movkey -32($key,$rnds_),$rndkey1
1315 aesenc $rndkey0,$inout0
1316 movbe %eax,`0x30+12`(%rsp)
1318 aesenc $rndkey0,$inout1
1320 movbe %eax,`0x40+12`(%rsp)
1321 aesenc $rndkey0,$inout2
1324 aesenc $rndkey0,$inout3
1325 movbe %eax,`0x50+12`(%rsp)
1326 mov %r10,%rax # mov $rnds_,$rounds
1327 aesenc $rndkey0,$inout4
1328 aesenc $rndkey0,$inout5
1329 $movkey -16($key,$rnds_),$rndkey0
1333 movdqu ($inp),$inout6 # load 6 input blocks
1334 movdqu 0x10($inp),$inout7
1335 movdqu 0x20($inp),$in0
1336 movdqu 0x30($inp),$in1
1337 movdqu 0x40($inp),$in2
1338 movdqu 0x50($inp),$in3
1339 lea 0x60($inp),$inp # $inp+=6*16
1340 $movkey -64($key,$rnds_),$rndkey1
1341 pxor $inout0,$inout6 # inp^=E(ctr)
1342 movaps 0x00(%rsp),$inout0 # load next counter [xor-ed with 0 round]
1343 pxor $inout1,$inout7
1344 movaps 0x10(%rsp),$inout1
1346 movaps 0x20(%rsp),$inout2
1348 movaps 0x30(%rsp),$inout3
1350 movaps 0x40(%rsp),$inout4
1352 movaps 0x50(%rsp),$inout5
1353 movdqu $inout6,($out) # store 6 output blocks
1354 movdqu $inout7,0x10($out)
1355 movdqu $in0,0x20($out)
1356 movdqu $in1,0x30($out)
1357 movdqu $in2,0x40($out)
1358 movdqu $in3,0x50($out)
1359 lea 0x60($out),$out # $out+=6*16
1362 jnc .Lctr32_loop6 # loop if $len-=6 didn't borrow
1364 add \$6,$len # restore real remaining $len
1365 jz .Lctr32_done # done if ($len==0)
1367 lea -48($rnds_),$rounds
1368 lea -80($key,$rnds_),$key # restore $key
1370 shr \$4,$rounds # restore $rounds
1375 add \$8,$ctr # next counter value
1376 movdqa 0x60(%rsp),$inout6
1377 aesenc $rndkey1,$inout0
1379 movdqa 0x70(%rsp),$inout7
1380 aesenc $rndkey1,$inout1
1382 $movkey 0x20-0x80($key),$rndkey0
1383 aesenc $rndkey1,$inout2
1386 aesenc $rndkey1,$inout3
1387 mov %r9d,0x00+12(%rsp) # store next counter value
1389 aesenc $rndkey1,$inout4
1390 aesenc $rndkey1,$inout5
1391 aesenc $rndkey1,$inout6
1392 aesenc $rndkey1,$inout7
1393 $movkey 0x30-0x80($key),$rndkey1
1395 for($i=2;$i<8;$i++) {
1396 my $rndkeyx = ($i&1)?$rndkey1:$rndkey0;
1399 aesenc $rndkeyx,$inout0
1400 aesenc $rndkeyx,$inout1
1403 aesenc $rndkeyx,$inout2
1404 aesenc $rndkeyx,$inout3
1405 mov %r9d,`0x10*($i-1)`+12(%rsp)
1407 aesenc $rndkeyx,$inout4
1408 aesenc $rndkeyx,$inout5
1409 aesenc $rndkeyx,$inout6
1410 aesenc $rndkeyx,$inout7
1411 $movkey `0x20+0x10*$i`-0x80($key),$rndkeyx
1416 aesenc $rndkey0,$inout0
1417 aesenc $rndkey0,$inout1
1418 aesenc $rndkey0,$inout2
1420 movdqu 0x00($inp),$in0 # start loading input
1421 aesenc $rndkey0,$inout3
1422 mov %r9d,0x70+12(%rsp)
1424 aesenc $rndkey0,$inout4
1425 aesenc $rndkey0,$inout5
1426 aesenc $rndkey0,$inout6
1427 aesenc $rndkey0,$inout7
1428 $movkey 0xa0-0x80($key),$rndkey0
1432 aesenc $rndkey1,$inout0
1433 aesenc $rndkey1,$inout1
1434 aesenc $rndkey1,$inout2
1435 aesenc $rndkey1,$inout3
1436 aesenc $rndkey1,$inout4
1437 aesenc $rndkey1,$inout5
1438 aesenc $rndkey1,$inout6
1439 aesenc $rndkey1,$inout7
1440 $movkey 0xb0-0x80($key),$rndkey1
1442 aesenc $rndkey0,$inout0
1443 aesenc $rndkey0,$inout1
1444 aesenc $rndkey0,$inout2
1445 aesenc $rndkey0,$inout3
1446 aesenc $rndkey0,$inout4
1447 aesenc $rndkey0,$inout5
1448 aesenc $rndkey0,$inout6
1449 aesenc $rndkey0,$inout7
1450 $movkey 0xc0-0x80($key),$rndkey0
1453 aesenc $rndkey1,$inout0
1454 aesenc $rndkey1,$inout1
1455 aesenc $rndkey1,$inout2
1456 aesenc $rndkey1,$inout3
1457 aesenc $rndkey1,$inout4
1458 aesenc $rndkey1,$inout5
1459 aesenc $rndkey1,$inout6
1460 aesenc $rndkey1,$inout7
1461 $movkey 0xd0-0x80($key),$rndkey1
1463 aesenc $rndkey0,$inout0
1464 aesenc $rndkey0,$inout1
1465 aesenc $rndkey0,$inout2
1466 aesenc $rndkey0,$inout3
1467 aesenc $rndkey0,$inout4
1468 aesenc $rndkey0,$inout5
1469 aesenc $rndkey0,$inout6
1470 aesenc $rndkey0,$inout7
1471 $movkey 0xe0-0x80($key),$rndkey0
1472 jmp .Lctr32_enc_done
1476 movdqu 0x10($inp),$in1
1477 pxor $rndkey0,$in0 # input^=round[last]
1478 movdqu 0x20($inp),$in2
1480 movdqu 0x30($inp),$in3
1482 movdqu 0x40($inp),$in4
1484 movdqu 0x50($inp),$in5
1487 aesenc $rndkey1,$inout0
1488 aesenc $rndkey1,$inout1
1489 aesenc $rndkey1,$inout2
1490 aesenc $rndkey1,$inout3
1491 aesenc $rndkey1,$inout4
1492 aesenc $rndkey1,$inout5
1493 aesenc $rndkey1,$inout6
1494 aesenc $rndkey1,$inout7
1495 movdqu 0x60($inp),$rndkey1 # borrow $rndkey1 for inp[6]
1496 lea 0x80($inp),$inp # $inp+=8*16
1498 aesenclast $in0,$inout0 # $inN is inp[N]^round[last]
1499 pxor $rndkey0,$rndkey1 # borrowed $rndkey
1500 movdqu 0x70-0x80($inp),$in0
1501 aesenclast $in1,$inout1
1503 movdqa 0x00(%rsp),$in1 # load next counter block
1504 aesenclast $in2,$inout2
1505 aesenclast $in3,$inout3
1506 movdqa 0x10(%rsp),$in2
1507 movdqa 0x20(%rsp),$in3
1508 aesenclast $in4,$inout4
1509 aesenclast $in5,$inout5
1510 movdqa 0x30(%rsp),$in4
1511 movdqa 0x40(%rsp),$in5
1512 aesenclast $rndkey1,$inout6
1513 movdqa 0x50(%rsp),$rndkey0
1514 $movkey 0x10-0x80($key),$rndkey1#real 1st-round key
1515 aesenclast $in0,$inout7
1517 movups $inout0,($out) # store 8 output blocks
1519 movups $inout1,0x10($out)
1521 movups $inout2,0x20($out)
1523 movups $inout3,0x30($out)
1525 movups $inout4,0x40($out)
1527 movups $inout5,0x50($out)
1528 movdqa $rndkey0,$inout5
1529 movups $inout6,0x60($out)
1530 movups $inout7,0x70($out)
1531 lea 0x80($out),$out # $out+=8*16
1534 jnc .Lctr32_loop8 # loop if $len-=8 didn't borrow
1536 add \$8,$len # restore real remainig $len
1537 jz .Lctr32_done # done if ($len==0)
1538 lea -0x80($key),$key
1541 # note that at this point $inout0..5 are populated with
1542 # counter values xor-ed with 0-round key
1548 # if ($len>4) compute 7 E(counter)
1550 movdqa 0x60(%rsp),$inout6
1551 pxor $inout7,$inout7
1553 $movkey 16($key),$rndkey0
1554 aesenc $rndkey1,$inout0
1555 aesenc $rndkey1,$inout1
1556 lea 32-16($key,$rounds),$key# prepare for .Lenc_loop8_enter
1558 aesenc $rndkey1,$inout2
1559 add \$16,%rax # prepare for .Lenc_loop8_enter
1561 aesenc $rndkey1,$inout3
1562 aesenc $rndkey1,$inout4
1563 movups 0x10($inp),$in1 # pre-load input
1564 movups 0x20($inp),$in2
1565 aesenc $rndkey1,$inout5
1566 aesenc $rndkey1,$inout6
1568 call .Lenc_loop8_enter
1570 movdqu 0x30($inp),$in3
1572 movdqu 0x40($inp),$in0
1574 movdqu $inout0,($out) # store output
1576 movdqu $inout1,0x10($out)
1578 movdqu $inout2,0x20($out)
1580 movdqu $inout3,0x30($out)
1581 movdqu $inout4,0x40($out)
1583 jb .Lctr32_done # $len was 5, stop store
1585 movups 0x50($inp),$in1
1587 movups $inout5,0x50($out)
1588 je .Lctr32_done # $len was 6, stop store
1590 movups 0x60($inp),$in2
1592 movups $inout6,0x60($out)
1593 jmp .Lctr32_done # $len was 7, stop store
1597 aesenc $rndkey1,$inout0
1600 aesenc $rndkey1,$inout1
1601 aesenc $rndkey1,$inout2
1602 aesenc $rndkey1,$inout3
1603 $movkey ($key),$rndkey1
1605 aesenclast $rndkey1,$inout0
1606 aesenclast $rndkey1,$inout1
1607 movups ($inp),$in0 # load input
1608 movups 0x10($inp),$in1
1609 aesenclast $rndkey1,$inout2
1610 aesenclast $rndkey1,$inout3
1611 movups 0x20($inp),$in2
1612 movups 0x30($inp),$in3
1615 movups $inout0,($out) # store output
1617 movups $inout1,0x10($out)
1619 movdqu $inout2,0x20($out)
1621 movdqu $inout3,0x30($out)
1622 jmp .Lctr32_done # $len was 4, stop store
1626 aesenc $rndkey1,$inout0
1629 aesenc $rndkey1,$inout1
1630 aesenc $rndkey1,$inout2
1631 $movkey ($key),$rndkey1
1633 aesenclast $rndkey1,$inout0
1634 aesenclast $rndkey1,$inout1
1635 aesenclast $rndkey1,$inout2
1637 movups ($inp),$in0 # load input
1639 movups $inout0,($out) # store output
1641 jb .Lctr32_done # $len was 1, stop store
1643 movups 0x10($inp),$in1
1645 movups $inout1,0x10($out)
1646 je .Lctr32_done # $len was 2, stop store
1648 movups 0x20($inp),$in2
1650 movups $inout2,0x20($out) # $len was 3, stop store
1653 xorps %xmm0,%xmm0 # clear regiser bank
1661 $code.=<<___ if (!$win64);
1664 movaps %xmm0,0x00(%rsp) # clear stack
1666 movaps %xmm0,0x10(%rsp)
1668 movaps %xmm0,0x20(%rsp)
1670 movaps %xmm0,0x30(%rsp)
1672 movaps %xmm0,0x40(%rsp)
1674 movaps %xmm0,0x50(%rsp)
1676 movaps %xmm0,0x60(%rsp)
1678 movaps %xmm0,0x70(%rsp)
1681 $code.=<<___ if ($win64);
1682 movaps -0xa0(%rbp),%xmm6
1683 movaps %xmm0,-0xa0(%rbp) # clear stack
1684 movaps -0x90(%rbp),%xmm7
1685 movaps %xmm0,-0x90(%rbp)
1686 movaps -0x80(%rbp),%xmm8
1687 movaps %xmm0,-0x80(%rbp)
1688 movaps -0x70(%rbp),%xmm9
1689 movaps %xmm0,-0x70(%rbp)
1690 movaps -0x60(%rbp),%xmm10
1691 movaps %xmm0,-0x60(%rbp)
1692 movaps -0x50(%rbp),%xmm11
1693 movaps %xmm0,-0x50(%rbp)
1694 movaps -0x40(%rbp),%xmm12
1695 movaps %xmm0,-0x40(%rbp)
1696 movaps -0x30(%rbp),%xmm13
1697 movaps %xmm0,-0x30(%rbp)
1698 movaps -0x20(%rbp),%xmm14
1699 movaps %xmm0,-0x20(%rbp)
1700 movaps -0x10(%rbp),%xmm15
1701 movaps %xmm0,-0x10(%rbp)
1702 movaps %xmm0,0x00(%rsp)
1703 movaps %xmm0,0x10(%rsp)
1704 movaps %xmm0,0x20(%rsp)
1705 movaps %xmm0,0x30(%rsp)
1706 movaps %xmm0,0x40(%rsp)
1707 movaps %xmm0,0x50(%rsp)
1708 movaps %xmm0,0x60(%rsp)
1709 movaps %xmm0,0x70(%rsp)
1716 .size aesni_ctr32_encrypt_blocks,.-aesni_ctr32_encrypt_blocks
1720 ######################################################################
1721 # void aesni_xts_[en|de]crypt(const char *inp,char *out,size_t len,
1722 # const AES_KEY *key1, const AES_KEY *key2
1723 # const unsigned char iv[16]);
1726 my @tweak=map("%xmm$_",(10..15));
1727 my ($twmask,$twres,$twtmp)=("%xmm8","%xmm9",@tweak[4]);
1728 my ($key2,$ivp,$len_)=("%r8","%r9","%r9");
1729 my $frame_size = 0x70 + ($win64?160:0);
1732 .globl aesni_xts_encrypt
1733 .type aesni_xts_encrypt,\@function,6
1738 sub \$$frame_size,%rsp
1739 and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
1741 $code.=<<___ if ($win64);
1742 movaps %xmm6,-0xa8(%rax) # offload everything
1743 movaps %xmm7,-0x98(%rax)
1744 movaps %xmm8,-0x88(%rax)
1745 movaps %xmm9,-0x78(%rax)
1746 movaps %xmm10,-0x68(%rax)
1747 movaps %xmm11,-0x58(%rax)
1748 movaps %xmm12,-0x48(%rax)
1749 movaps %xmm13,-0x38(%rax)
1750 movaps %xmm14,-0x28(%rax)
1751 movaps %xmm15,-0x18(%rax)
1756 movups ($ivp),$inout0 # load clear-text tweak
1757 mov 240(%r8),$rounds # key2->rounds
1758 mov 240($key),$rnds_ # key1->rounds
1760 # generate the tweak
1761 &aesni_generate1("enc",$key2,$rounds,$inout0);
1763 $movkey ($key),$rndkey0 # zero round key
1764 mov $key,$key_ # backup $key
1765 mov $rnds_,$rounds # backup $rounds
1767 mov $len,$len_ # backup $len
1770 $movkey 16($key,$rnds_),$rndkey1 # last round key
1772 movdqa .Lxts_magic(%rip),$twmask
1773 movdqa $inout0,@tweak[5]
1774 pshufd \$0x5f,$inout0,$twres
1775 pxor $rndkey0,$rndkey1
1777 # alternative tweak calculation algorithm is based on suggestions
1778 # by Shay Gueron. psrad doesn't conflict with AES-NI instructions
1779 # and should help in the future...
1780 for ($i=0;$i<4;$i++) {
1782 movdqa $twres,$twtmp
1784 movdqa @tweak[5],@tweak[$i]
1785 psrad \$31,$twtmp # broadcast upper bits
1786 paddq @tweak[5],@tweak[5]
1788 pxor $rndkey0,@tweak[$i]
1789 pxor $twtmp,@tweak[5]
1793 movdqa @tweak[5],@tweak[4]
1795 paddq @tweak[5],@tweak[5]
1797 pxor $rndkey0,@tweak[4]
1798 pxor $twres,@tweak[5]
1799 movaps $rndkey1,0x60(%rsp) # save round[0]^round[last]
1802 jc .Lxts_enc_short # if $len-=6*16 borrowed
1805 lea 32($key_,$rnds_),$key # end of key schedule
1806 sub %r10,%rax # twisted $rounds
1807 $movkey 16($key_),$rndkey1
1808 mov %rax,%r10 # backup twisted $rounds
1809 lea .Lxts_magic(%rip),%r8
1810 jmp .Lxts_enc_grandloop
1813 .Lxts_enc_grandloop:
1814 movdqu `16*0`($inp),$inout0 # load input
1815 movdqa $rndkey0,$twmask
1816 movdqu `16*1`($inp),$inout1
1817 pxor @tweak[0],$inout0 # input^=tweak^round[0]
1818 movdqu `16*2`($inp),$inout2
1819 pxor @tweak[1],$inout1
1820 aesenc $rndkey1,$inout0
1821 movdqu `16*3`($inp),$inout3
1822 pxor @tweak[2],$inout2
1823 aesenc $rndkey1,$inout1
1824 movdqu `16*4`($inp),$inout4
1825 pxor @tweak[3],$inout3
1826 aesenc $rndkey1,$inout2
1827 movdqu `16*5`($inp),$inout5
1828 pxor @tweak[5],$twmask # round[0]^=tweak[5]
1829 movdqa 0x60(%rsp),$twres # load round[0]^round[last]
1830 pxor @tweak[4],$inout4
1831 aesenc $rndkey1,$inout3
1832 $movkey 32($key_),$rndkey0
1833 lea `16*6`($inp),$inp
1834 pxor $twmask,$inout5
1836 pxor $twres,@tweak[0] # calclulate tweaks^round[last]
1837 aesenc $rndkey1,$inout4
1838 pxor $twres,@tweak[1]
1839 movdqa @tweak[0],`16*0`(%rsp) # put aside tweaks^round[last]
1840 aesenc $rndkey1,$inout5
1841 $movkey 48($key_),$rndkey1
1842 pxor $twres,@tweak[2]
1844 aesenc $rndkey0,$inout0
1845 pxor $twres,@tweak[3]
1846 movdqa @tweak[1],`16*1`(%rsp)
1847 aesenc $rndkey0,$inout1
1848 pxor $twres,@tweak[4]
1849 movdqa @tweak[2],`16*2`(%rsp)
1850 aesenc $rndkey0,$inout2
1851 aesenc $rndkey0,$inout3
1853 movdqa @tweak[4],`16*4`(%rsp)
1854 aesenc $rndkey0,$inout4
1855 aesenc $rndkey0,$inout5
1856 $movkey 64($key_),$rndkey0
1857 movdqa $twmask,`16*5`(%rsp)
1858 pshufd \$0x5f,@tweak[5],$twres
1862 aesenc $rndkey1,$inout0
1863 aesenc $rndkey1,$inout1
1864 aesenc $rndkey1,$inout2
1865 aesenc $rndkey1,$inout3
1866 aesenc $rndkey1,$inout4
1867 aesenc $rndkey1,$inout5
1868 $movkey -64($key,%rax),$rndkey1
1871 aesenc $rndkey0,$inout0
1872 aesenc $rndkey0,$inout1
1873 aesenc $rndkey0,$inout2
1874 aesenc $rndkey0,$inout3
1875 aesenc $rndkey0,$inout4
1876 aesenc $rndkey0,$inout5
1877 $movkey -80($key,%rax),$rndkey0
1880 movdqa (%r8),$twmask # start calculating next tweak
1881 movdqa $twres,$twtmp
1883 aesenc $rndkey1,$inout0
1884 paddq @tweak[5],@tweak[5]
1886 aesenc $rndkey1,$inout1
1888 $movkey ($key_),@tweak[0] # load round[0]
1889 aesenc $rndkey1,$inout2
1890 aesenc $rndkey1,$inout3
1891 aesenc $rndkey1,$inout4
1892 pxor $twtmp,@tweak[5]
1893 movaps @tweak[0],@tweak[1] # copy round[0]
1894 aesenc $rndkey1,$inout5
1895 $movkey -64($key),$rndkey1
1897 movdqa $twres,$twtmp
1898 aesenc $rndkey0,$inout0
1900 pxor @tweak[5],@tweak[0]
1901 aesenc $rndkey0,$inout1
1903 paddq @tweak[5],@tweak[5]
1904 aesenc $rndkey0,$inout2
1905 aesenc $rndkey0,$inout3
1907 movaps @tweak[1],@tweak[2]
1908 aesenc $rndkey0,$inout4
1909 pxor $twtmp,@tweak[5]
1910 movdqa $twres,$twtmp
1911 aesenc $rndkey0,$inout5
1912 $movkey -48($key),$rndkey0
1915 aesenc $rndkey1,$inout0
1916 pxor @tweak[5],@tweak[1]
1918 aesenc $rndkey1,$inout1
1919 paddq @tweak[5],@tweak[5]
1921 aesenc $rndkey1,$inout2
1922 aesenc $rndkey1,$inout3
1923 movdqa @tweak[3],`16*3`(%rsp)
1924 pxor $twtmp,@tweak[5]
1925 aesenc $rndkey1,$inout4
1926 movaps @tweak[2],@tweak[3]
1927 movdqa $twres,$twtmp
1928 aesenc $rndkey1,$inout5
1929 $movkey -32($key),$rndkey1
1932 aesenc $rndkey0,$inout0
1933 pxor @tweak[5],@tweak[2]
1935 aesenc $rndkey0,$inout1
1936 paddq @tweak[5],@tweak[5]
1938 aesenc $rndkey0,$inout2
1939 aesenc $rndkey0,$inout3
1940 aesenc $rndkey0,$inout4
1941 pxor $twtmp,@tweak[5]
1942 movaps @tweak[3],@tweak[4]
1943 aesenc $rndkey0,$inout5
1945 movdqa $twres,$rndkey0
1947 aesenc $rndkey1,$inout0
1948 pxor @tweak[5],@tweak[3]
1950 aesenc $rndkey1,$inout1
1951 paddq @tweak[5],@tweak[5]
1952 pand $twmask,$rndkey0
1953 aesenc $rndkey1,$inout2
1954 aesenc $rndkey1,$inout3
1955 pxor $rndkey0,@tweak[5]
1956 $movkey ($key_),$rndkey0
1957 aesenc $rndkey1,$inout4
1958 aesenc $rndkey1,$inout5
1959 $movkey 16($key_),$rndkey1
1961 pxor @tweak[5],@tweak[4]
1962 aesenclast `16*0`(%rsp),$inout0
1964 paddq @tweak[5],@tweak[5]
1965 aesenclast `16*1`(%rsp),$inout1
1966 aesenclast `16*2`(%rsp),$inout2
1968 mov %r10,%rax # restore $rounds
1969 aesenclast `16*3`(%rsp),$inout3
1970 aesenclast `16*4`(%rsp),$inout4
1971 aesenclast `16*5`(%rsp),$inout5
1972 pxor $twres,@tweak[5]
1974 lea `16*6`($out),$out # $out+=6*16
1975 movups $inout0,`-16*6`($out) # store 6 output blocks
1976 movups $inout1,`-16*5`($out)
1977 movups $inout2,`-16*4`($out)
1978 movups $inout3,`-16*3`($out)
1979 movups $inout4,`-16*2`($out)
1980 movups $inout5,`-16*1`($out)
1982 jnc .Lxts_enc_grandloop # loop if $len-=6*16 didn't borrow
1986 mov $key_,$key # restore $key
1987 shr \$4,$rounds # restore original value
1990 # at the point @tweak[0..5] are populated with tweak values
1991 mov $rounds,$rnds_ # backup $rounds
1992 pxor $rndkey0,@tweak[0]
1993 add \$16*6,$len # restore real remaining $len
1994 jz .Lxts_enc_done # done if ($len==0)
1996 pxor $rndkey0,@tweak[1]
1998 jb .Lxts_enc_one # $len is 1*16
1999 pxor $rndkey0,@tweak[2]
2000 je .Lxts_enc_two # $len is 2*16
2002 pxor $rndkey0,@tweak[3]
2004 jb .Lxts_enc_three # $len is 3*16
2005 pxor $rndkey0,@tweak[4]
2006 je .Lxts_enc_four # $len is 4*16
2008 movdqu ($inp),$inout0 # $len is 5*16
2009 movdqu 16*1($inp),$inout1
2010 movdqu 16*2($inp),$inout2
2011 pxor @tweak[0],$inout0
2012 movdqu 16*3($inp),$inout3
2013 pxor @tweak[1],$inout1
2014 movdqu 16*4($inp),$inout4
2015 lea 16*5($inp),$inp # $inp+=5*16
2016 pxor @tweak[2],$inout2
2017 pxor @tweak[3],$inout3
2018 pxor @tweak[4],$inout4
2019 pxor $inout5,$inout5
2021 call _aesni_encrypt6
2023 xorps @tweak[0],$inout0
2024 movdqa @tweak[5],@tweak[0]
2025 xorps @tweak[1],$inout1
2026 xorps @tweak[2],$inout2
2027 movdqu $inout0,($out) # store 5 output blocks
2028 xorps @tweak[3],$inout3
2029 movdqu $inout1,16*1($out)
2030 xorps @tweak[4],$inout4
2031 movdqu $inout2,16*2($out)
2032 movdqu $inout3,16*3($out)
2033 movdqu $inout4,16*4($out)
2034 lea 16*5($out),$out # $out+=5*16
2039 movups ($inp),$inout0
2040 lea 16*1($inp),$inp # inp+=1*16
2041 xorps @tweak[0],$inout0
2043 &aesni_generate1("enc",$key,$rounds);
2045 xorps @tweak[0],$inout0
2046 movdqa @tweak[1],@tweak[0]
2047 movups $inout0,($out) # store one output block
2048 lea 16*1($out),$out # $out+=1*16
2053 movups ($inp),$inout0
2054 movups 16($inp),$inout1
2055 lea 32($inp),$inp # $inp+=2*16
2056 xorps @tweak[0],$inout0
2057 xorps @tweak[1],$inout1
2059 call _aesni_encrypt2
2061 xorps @tweak[0],$inout0
2062 movdqa @tweak[2],@tweak[0]
2063 xorps @tweak[1],$inout1
2064 movups $inout0,($out) # store 2 output blocks
2065 movups $inout1,16*1($out)
2066 lea 16*2($out),$out # $out+=2*16
2071 movups ($inp),$inout0
2072 movups 16*1($inp),$inout1
2073 movups 16*2($inp),$inout2
2074 lea 16*3($inp),$inp # $inp+=3*16
2075 xorps @tweak[0],$inout0
2076 xorps @tweak[1],$inout1
2077 xorps @tweak[2],$inout2
2079 call _aesni_encrypt3
2081 xorps @tweak[0],$inout0
2082 movdqa @tweak[3],@tweak[0]
2083 xorps @tweak[1],$inout1
2084 xorps @tweak[2],$inout2
2085 movups $inout0,($out) # store 3 output blocks
2086 movups $inout1,16*1($out)
2087 movups $inout2,16*2($out)
2088 lea 16*3($out),$out # $out+=3*16
2093 movups ($inp),$inout0
2094 movups 16*1($inp),$inout1
2095 movups 16*2($inp),$inout2
2096 xorps @tweak[0],$inout0
2097 movups 16*3($inp),$inout3
2098 lea 16*4($inp),$inp # $inp+=4*16
2099 xorps @tweak[1],$inout1
2100 xorps @tweak[2],$inout2
2101 xorps @tweak[3],$inout3
2103 call _aesni_encrypt4
2105 pxor @tweak[0],$inout0
2106 movdqa @tweak[4],@tweak[0]
2107 pxor @tweak[1],$inout1
2108 pxor @tweak[2],$inout2
2109 movdqu $inout0,($out) # store 4 output blocks
2110 pxor @tweak[3],$inout3
2111 movdqu $inout1,16*1($out)
2112 movdqu $inout2,16*2($out)
2113 movdqu $inout3,16*3($out)
2114 lea 16*4($out),$out # $out+=4*16
2119 and \$15,$len_ # see if $len%16 is 0
2124 movzb ($inp),%eax # borrow $rounds ...
2125 movzb -16($out),%ecx # ... and $key
2133 sub $len_,$out # rewind $out
2134 mov $key_,$key # restore $key
2135 mov $rnds_,$rounds # restore $rounds
2137 movups -16($out),$inout0
2138 xorps @tweak[0],$inout0
2140 &aesni_generate1("enc",$key,$rounds);
2142 xorps @tweak[0],$inout0
2143 movups $inout0,-16($out)
2146 xorps %xmm0,%xmm0 # clear register bank
2153 $code.=<<___ if (!$win64);
2156 movaps %xmm0,0x00(%rsp) # clear stack
2158 movaps %xmm0,0x10(%rsp)
2160 movaps %xmm0,0x20(%rsp)
2162 movaps %xmm0,0x30(%rsp)
2164 movaps %xmm0,0x40(%rsp)
2166 movaps %xmm0,0x50(%rsp)
2168 movaps %xmm0,0x60(%rsp)
2172 $code.=<<___ if ($win64);
2173 movaps -0xa0(%rbp),%xmm6
2174 movaps %xmm0,-0xa0(%rbp) # clear stack
2175 movaps -0x90(%rbp),%xmm7
2176 movaps %xmm0,-0x90(%rbp)
2177 movaps -0x80(%rbp),%xmm8
2178 movaps %xmm0,-0x80(%rbp)
2179 movaps -0x70(%rbp),%xmm9
2180 movaps %xmm0,-0x70(%rbp)
2181 movaps -0x60(%rbp),%xmm10
2182 movaps %xmm0,-0x60(%rbp)
2183 movaps -0x50(%rbp),%xmm11
2184 movaps %xmm0,-0x50(%rbp)
2185 movaps -0x40(%rbp),%xmm12
2186 movaps %xmm0,-0x40(%rbp)
2187 movaps -0x30(%rbp),%xmm13
2188 movaps %xmm0,-0x30(%rbp)
2189 movaps -0x20(%rbp),%xmm14
2190 movaps %xmm0,-0x20(%rbp)
2191 movaps -0x10(%rbp),%xmm15
2192 movaps %xmm0,-0x10(%rbp)
2193 movaps %xmm0,0x00(%rsp)
2194 movaps %xmm0,0x10(%rsp)
2195 movaps %xmm0,0x20(%rsp)
2196 movaps %xmm0,0x30(%rsp)
2197 movaps %xmm0,0x40(%rsp)
2198 movaps %xmm0,0x50(%rsp)
2199 movaps %xmm0,0x60(%rsp)
2206 .size aesni_xts_encrypt,.-aesni_xts_encrypt
2210 .globl aesni_xts_decrypt
2211 .type aesni_xts_decrypt,\@function,6
2216 sub \$$frame_size,%rsp
2217 and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
2219 $code.=<<___ if ($win64);
2220 movaps %xmm6,-0xa8(%rax) # offload everything
2221 movaps %xmm7,-0x98(%rax)
2222 movaps %xmm8,-0x88(%rax)
2223 movaps %xmm9,-0x78(%rax)
2224 movaps %xmm10,-0x68(%rax)
2225 movaps %xmm11,-0x58(%rax)
2226 movaps %xmm12,-0x48(%rax)
2227 movaps %xmm13,-0x38(%rax)
2228 movaps %xmm14,-0x28(%rax)
2229 movaps %xmm15,-0x18(%rax)
2234 movups ($ivp),$inout0 # load clear-text tweak
2235 mov 240($key2),$rounds # key2->rounds
2236 mov 240($key),$rnds_ # key1->rounds
2238 # generate the tweak
2239 &aesni_generate1("enc",$key2,$rounds,$inout0);
2241 xor %eax,%eax # if ($len%16) len-=16;
2247 $movkey ($key),$rndkey0 # zero round key
2248 mov $key,$key_ # backup $key
2249 mov $rnds_,$rounds # backup $rounds
2251 mov $len,$len_ # backup $len
2254 $movkey 16($key,$rnds_),$rndkey1 # last round key
2256 movdqa .Lxts_magic(%rip),$twmask
2257 movdqa $inout0,@tweak[5]
2258 pshufd \$0x5f,$inout0,$twres
2259 pxor $rndkey0,$rndkey1
2261 for ($i=0;$i<4;$i++) {
2263 movdqa $twres,$twtmp
2265 movdqa @tweak[5],@tweak[$i]
2266 psrad \$31,$twtmp # broadcast upper bits
2267 paddq @tweak[5],@tweak[5]
2269 pxor $rndkey0,@tweak[$i]
2270 pxor $twtmp,@tweak[5]
2274 movdqa @tweak[5],@tweak[4]
2276 paddq @tweak[5],@tweak[5]
2278 pxor $rndkey0,@tweak[4]
2279 pxor $twres,@tweak[5]
2280 movaps $rndkey1,0x60(%rsp) # save round[0]^round[last]
2283 jc .Lxts_dec_short # if $len-=6*16 borrowed
2286 lea 32($key_,$rnds_),$key # end of key schedule
2287 sub %r10,%rax # twisted $rounds
2288 $movkey 16($key_),$rndkey1
2289 mov %rax,%r10 # backup twisted $rounds
2290 lea .Lxts_magic(%rip),%r8
2291 jmp .Lxts_dec_grandloop
2294 .Lxts_dec_grandloop:
2295 movdqu `16*0`($inp),$inout0 # load input
2296 movdqa $rndkey0,$twmask
2297 movdqu `16*1`($inp),$inout1
2298 pxor @tweak[0],$inout0 # intput^=tweak^round[0]
2299 movdqu `16*2`($inp),$inout2
2300 pxor @tweak[1],$inout1
2301 aesdec $rndkey1,$inout0
2302 movdqu `16*3`($inp),$inout3
2303 pxor @tweak[2],$inout2
2304 aesdec $rndkey1,$inout1
2305 movdqu `16*4`($inp),$inout4
2306 pxor @tweak[3],$inout3
2307 aesdec $rndkey1,$inout2
2308 movdqu `16*5`($inp),$inout5
2309 pxor @tweak[5],$twmask # round[0]^=tweak[5]
2310 movdqa 0x60(%rsp),$twres # load round[0]^round[last]
2311 pxor @tweak[4],$inout4
2312 aesdec $rndkey1,$inout3
2313 $movkey 32($key_),$rndkey0
2314 lea `16*6`($inp),$inp
2315 pxor $twmask,$inout5
2317 pxor $twres,@tweak[0] # calclulate tweaks^round[last]
2318 aesdec $rndkey1,$inout4
2319 pxor $twres,@tweak[1]
2320 movdqa @tweak[0],`16*0`(%rsp) # put aside tweaks^last round key
2321 aesdec $rndkey1,$inout5
2322 $movkey 48($key_),$rndkey1
2323 pxor $twres,@tweak[2]
2325 aesdec $rndkey0,$inout0
2326 pxor $twres,@tweak[3]
2327 movdqa @tweak[1],`16*1`(%rsp)
2328 aesdec $rndkey0,$inout1
2329 pxor $twres,@tweak[4]
2330 movdqa @tweak[2],`16*2`(%rsp)
2331 aesdec $rndkey0,$inout2
2332 aesdec $rndkey0,$inout3
2334 movdqa @tweak[4],`16*4`(%rsp)
2335 aesdec $rndkey0,$inout4
2336 aesdec $rndkey0,$inout5
2337 $movkey 64($key_),$rndkey0
2338 movdqa $twmask,`16*5`(%rsp)
2339 pshufd \$0x5f,@tweak[5],$twres
2343 aesdec $rndkey1,$inout0
2344 aesdec $rndkey1,$inout1
2345 aesdec $rndkey1,$inout2
2346 aesdec $rndkey1,$inout3
2347 aesdec $rndkey1,$inout4
2348 aesdec $rndkey1,$inout5
2349 $movkey -64($key,%rax),$rndkey1
2352 aesdec $rndkey0,$inout0
2353 aesdec $rndkey0,$inout1
2354 aesdec $rndkey0,$inout2
2355 aesdec $rndkey0,$inout3
2356 aesdec $rndkey0,$inout4
2357 aesdec $rndkey0,$inout5
2358 $movkey -80($key,%rax),$rndkey0
2361 movdqa (%r8),$twmask # start calculating next tweak
2362 movdqa $twres,$twtmp
2364 aesdec $rndkey1,$inout0
2365 paddq @tweak[5],@tweak[5]
2367 aesdec $rndkey1,$inout1
2369 $movkey ($key_),@tweak[0] # load round[0]
2370 aesdec $rndkey1,$inout2
2371 aesdec $rndkey1,$inout3
2372 aesdec $rndkey1,$inout4
2373 pxor $twtmp,@tweak[5]
2374 movaps @tweak[0],@tweak[1] # copy round[0]
2375 aesdec $rndkey1,$inout5
2376 $movkey -64($key),$rndkey1
2378 movdqa $twres,$twtmp
2379 aesdec $rndkey0,$inout0
2381 pxor @tweak[5],@tweak[0]
2382 aesdec $rndkey0,$inout1
2384 paddq @tweak[5],@tweak[5]
2385 aesdec $rndkey0,$inout2
2386 aesdec $rndkey0,$inout3
2388 movaps @tweak[1],@tweak[2]
2389 aesdec $rndkey0,$inout4
2390 pxor $twtmp,@tweak[5]
2391 movdqa $twres,$twtmp
2392 aesdec $rndkey0,$inout5
2393 $movkey -48($key),$rndkey0
2396 aesdec $rndkey1,$inout0
2397 pxor @tweak[5],@tweak[1]
2399 aesdec $rndkey1,$inout1
2400 paddq @tweak[5],@tweak[5]
2402 aesdec $rndkey1,$inout2
2403 aesdec $rndkey1,$inout3
2404 movdqa @tweak[3],`16*3`(%rsp)
2405 pxor $twtmp,@tweak[5]
2406 aesdec $rndkey1,$inout4
2407 movaps @tweak[2],@tweak[3]
2408 movdqa $twres,$twtmp
2409 aesdec $rndkey1,$inout5
2410 $movkey -32($key),$rndkey1
2413 aesdec $rndkey0,$inout0
2414 pxor @tweak[5],@tweak[2]
2416 aesdec $rndkey0,$inout1
2417 paddq @tweak[5],@tweak[5]
2419 aesdec $rndkey0,$inout2
2420 aesdec $rndkey0,$inout3
2421 aesdec $rndkey0,$inout4
2422 pxor $twtmp,@tweak[5]
2423 movaps @tweak[3],@tweak[4]
2424 aesdec $rndkey0,$inout5
2426 movdqa $twres,$rndkey0
2428 aesdec $rndkey1,$inout0
2429 pxor @tweak[5],@tweak[3]
2431 aesdec $rndkey1,$inout1
2432 paddq @tweak[5],@tweak[5]
2433 pand $twmask,$rndkey0
2434 aesdec $rndkey1,$inout2
2435 aesdec $rndkey1,$inout3
2436 pxor $rndkey0,@tweak[5]
2437 $movkey ($key_),$rndkey0
2438 aesdec $rndkey1,$inout4
2439 aesdec $rndkey1,$inout5
2440 $movkey 16($key_),$rndkey1
2442 pxor @tweak[5],@tweak[4]
2443 aesdeclast `16*0`(%rsp),$inout0
2445 paddq @tweak[5],@tweak[5]
2446 aesdeclast `16*1`(%rsp),$inout1
2447 aesdeclast `16*2`(%rsp),$inout2
2449 mov %r10,%rax # restore $rounds
2450 aesdeclast `16*3`(%rsp),$inout3
2451 aesdeclast `16*4`(%rsp),$inout4
2452 aesdeclast `16*5`(%rsp),$inout5
2453 pxor $twres,@tweak[5]
2455 lea `16*6`($out),$out # $out+=6*16
2456 movups $inout0,`-16*6`($out) # store 6 output blocks
2457 movups $inout1,`-16*5`($out)
2458 movups $inout2,`-16*4`($out)
2459 movups $inout3,`-16*3`($out)
2460 movups $inout4,`-16*2`($out)
2461 movups $inout5,`-16*1`($out)
2463 jnc .Lxts_dec_grandloop # loop if $len-=6*16 didn't borrow
2467 mov $key_,$key # restore $key
2468 shr \$4,$rounds # restore original value
2471 # at the point @tweak[0..5] are populated with tweak values
2472 mov $rounds,$rnds_ # backup $rounds
2473 pxor $rndkey0,@tweak[0]
2474 pxor $rndkey0,@tweak[1]
2475 add \$16*6,$len # restore real remaining $len
2476 jz .Lxts_dec_done # done if ($len==0)
2478 pxor $rndkey0,@tweak[2]
2480 jb .Lxts_dec_one # $len is 1*16
2481 pxor $rndkey0,@tweak[3]
2482 je .Lxts_dec_two # $len is 2*16
2484 pxor $rndkey0,@tweak[4]
2486 jb .Lxts_dec_three # $len is 3*16
2487 je .Lxts_dec_four # $len is 4*16
2489 movdqu ($inp),$inout0 # $len is 5*16
2490 movdqu 16*1($inp),$inout1
2491 movdqu 16*2($inp),$inout2
2492 pxor @tweak[0],$inout0
2493 movdqu 16*3($inp),$inout3
2494 pxor @tweak[1],$inout1
2495 movdqu 16*4($inp),$inout4
2496 lea 16*5($inp),$inp # $inp+=5*16
2497 pxor @tweak[2],$inout2
2498 pxor @tweak[3],$inout3
2499 pxor @tweak[4],$inout4
2501 call _aesni_decrypt6
2503 xorps @tweak[0],$inout0
2504 xorps @tweak[1],$inout1
2505 xorps @tweak[2],$inout2
2506 movdqu $inout0,($out) # store 5 output blocks
2507 xorps @tweak[3],$inout3
2508 movdqu $inout1,16*1($out)
2509 xorps @tweak[4],$inout4
2510 movdqu $inout2,16*2($out)
2512 movdqu $inout3,16*3($out)
2513 pcmpgtd @tweak[5],$twtmp
2514 movdqu $inout4,16*4($out)
2515 lea 16*5($out),$out # $out+=5*16
2516 pshufd \$0x13,$twtmp,@tweak[1] # $twres
2520 movdqa @tweak[5],@tweak[0]
2521 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
2522 pand $twmask,@tweak[1] # isolate carry and residue
2523 pxor @tweak[5],@tweak[1]
2528 movups ($inp),$inout0
2529 lea 16*1($inp),$inp # $inp+=1*16
2530 xorps @tweak[0],$inout0
2532 &aesni_generate1("dec",$key,$rounds);
2534 xorps @tweak[0],$inout0
2535 movdqa @tweak[1],@tweak[0]
2536 movups $inout0,($out) # store one output block
2537 movdqa @tweak[2],@tweak[1]
2538 lea 16*1($out),$out # $out+=1*16
2543 movups ($inp),$inout0
2544 movups 16($inp),$inout1
2545 lea 32($inp),$inp # $inp+=2*16
2546 xorps @tweak[0],$inout0
2547 xorps @tweak[1],$inout1
2549 call _aesni_decrypt2
2551 xorps @tweak[0],$inout0
2552 movdqa @tweak[2],@tweak[0]
2553 xorps @tweak[1],$inout1
2554 movdqa @tweak[3],@tweak[1]
2555 movups $inout0,($out) # store 2 output blocks
2556 movups $inout1,16*1($out)
2557 lea 16*2($out),$out # $out+=2*16
2562 movups ($inp),$inout0
2563 movups 16*1($inp),$inout1
2564 movups 16*2($inp),$inout2
2565 lea 16*3($inp),$inp # $inp+=3*16
2566 xorps @tweak[0],$inout0
2567 xorps @tweak[1],$inout1
2568 xorps @tweak[2],$inout2
2570 call _aesni_decrypt3
2572 xorps @tweak[0],$inout0
2573 movdqa @tweak[3],@tweak[0]
2574 xorps @tweak[1],$inout1
2575 movdqa @tweak[4],@tweak[1]
2576 xorps @tweak[2],$inout2
2577 movups $inout0,($out) # store 3 output blocks
2578 movups $inout1,16*1($out)
2579 movups $inout2,16*2($out)
2580 lea 16*3($out),$out # $out+=3*16
2585 movups ($inp),$inout0
2586 movups 16*1($inp),$inout1
2587 movups 16*2($inp),$inout2
2588 xorps @tweak[0],$inout0
2589 movups 16*3($inp),$inout3
2590 lea 16*4($inp),$inp # $inp+=4*16
2591 xorps @tweak[1],$inout1
2592 xorps @tweak[2],$inout2
2593 xorps @tweak[3],$inout3
2595 call _aesni_decrypt4
2597 pxor @tweak[0],$inout0
2598 movdqa @tweak[4],@tweak[0]
2599 pxor @tweak[1],$inout1
2600 movdqa @tweak[5],@tweak[1]
2601 pxor @tweak[2],$inout2
2602 movdqu $inout0,($out) # store 4 output blocks
2603 pxor @tweak[3],$inout3
2604 movdqu $inout1,16*1($out)
2605 movdqu $inout2,16*2($out)
2606 movdqu $inout3,16*3($out)
2607 lea 16*4($out),$out # $out+=4*16
2612 and \$15,$len_ # see if $len%16 is 0
2616 mov $key_,$key # restore $key
2617 mov $rnds_,$rounds # restore $rounds
2619 movups ($inp),$inout0
2620 xorps @tweak[1],$inout0
2622 &aesni_generate1("dec",$key,$rounds);
2624 xorps @tweak[1],$inout0
2625 movups $inout0,($out)
2628 movzb 16($inp),%eax # borrow $rounds ...
2629 movzb ($out),%ecx # ... and $key
2637 sub $len_,$out # rewind $out
2638 mov $key_,$key # restore $key
2639 mov $rnds_,$rounds # restore $rounds
2641 movups ($out),$inout0
2642 xorps @tweak[0],$inout0
2644 &aesni_generate1("dec",$key,$rounds);
2646 xorps @tweak[0],$inout0
2647 movups $inout0,($out)
2650 xorps %xmm0,%xmm0 # clear register bank
2657 $code.=<<___ if (!$win64);
2660 movaps %xmm0,0x00(%rsp) # clear stack
2662 movaps %xmm0,0x10(%rsp)
2664 movaps %xmm0,0x20(%rsp)
2666 movaps %xmm0,0x30(%rsp)
2668 movaps %xmm0,0x40(%rsp)
2670 movaps %xmm0,0x50(%rsp)
2672 movaps %xmm0,0x60(%rsp)
2676 $code.=<<___ if ($win64);
2677 movaps -0xa0(%rbp),%xmm6
2678 movaps %xmm0,-0xa0(%rbp) # clear stack
2679 movaps -0x90(%rbp),%xmm7
2680 movaps %xmm0,-0x90(%rbp)
2681 movaps -0x80(%rbp),%xmm8
2682 movaps %xmm0,-0x80(%rbp)
2683 movaps -0x70(%rbp),%xmm9
2684 movaps %xmm0,-0x70(%rbp)
2685 movaps -0x60(%rbp),%xmm10
2686 movaps %xmm0,-0x60(%rbp)
2687 movaps -0x50(%rbp),%xmm11
2688 movaps %xmm0,-0x50(%rbp)
2689 movaps -0x40(%rbp),%xmm12
2690 movaps %xmm0,-0x40(%rbp)
2691 movaps -0x30(%rbp),%xmm13
2692 movaps %xmm0,-0x30(%rbp)
2693 movaps -0x20(%rbp),%xmm14
2694 movaps %xmm0,-0x20(%rbp)
2695 movaps -0x10(%rbp),%xmm15
2696 movaps %xmm0,-0x10(%rbp)
2697 movaps %xmm0,0x00(%rsp)
2698 movaps %xmm0,0x10(%rsp)
2699 movaps %xmm0,0x20(%rsp)
2700 movaps %xmm0,0x30(%rsp)
2701 movaps %xmm0,0x40(%rsp)
2702 movaps %xmm0,0x50(%rsp)
2703 movaps %xmm0,0x60(%rsp)
2710 .size aesni_xts_decrypt,.-aesni_xts_decrypt
2714 ########################################################################
2715 # void $PREFIX_cbc_encrypt (const void *inp, void *out,
2716 # size_t length, const AES_KEY *key,
2717 # unsigned char *ivp,const int enc);
2719 my $frame_size = 0x10 + ($win64?0xa0:0); # used in decrypt
2720 my ($iv,$in0,$in1,$in2,$in3,$in4)=map("%xmm$_",(10..15));
2724 .globl ${PREFIX}_cbc_encrypt
2725 .type ${PREFIX}_cbc_encrypt,\@function,6
2727 ${PREFIX}_cbc_encrypt:
2728 test $len,$len # check length
2731 mov 240($key),$rnds_ # key->rounds
2732 mov $key,$key_ # backup $key
2733 test %r9d,%r9d # 6th argument
2735 #--------------------------- CBC ENCRYPT ------------------------------#
2736 movups ($ivp),$inout0 # load iv as initial state
2744 movups ($inp),$inout1 # load input
2746 #xorps $inout1,$inout0
2748 &aesni_generate1("enc",$key,$rounds,$inout0,$inout1);
2750 mov $rnds_,$rounds # restore $rounds
2751 mov $key_,$key # restore $key
2752 movups $inout0,0($out) # store output
2758 pxor $rndkey0,$rndkey0 # clear register bank
2759 pxor $rndkey1,$rndkey1
2760 movups $inout0,($ivp)
2761 pxor $inout0,$inout0
2762 pxor $inout1,$inout1
2766 mov $len,%rcx # zaps $key
2767 xchg $inp,$out # $inp is %rsi and $out is %rdi now
2768 .long 0x9066A4F3 # rep movsb
2769 mov \$16,%ecx # zero tail
2772 .long 0x9066AAF3 # rep stosb
2773 lea -16(%rdi),%rdi # rewind $out by 1 block
2774 mov $rnds_,$rounds # restore $rounds
2775 mov %rdi,%rsi # $inp and $out are the same
2776 mov $key_,$key # restore $key
2777 xor $len,$len # len=16
2778 jmp .Lcbc_enc_loop # one more spin
2779 \f#--------------------------- CBC DECRYPT ------------------------------#
2783 jne .Lcbc_decrypt_bulk
2785 # handle single block without allocating stack frame,
2786 # useful in ciphertext stealing mode
2787 movdqu ($inp),$inout0 # load input
2788 movdqu ($ivp),$inout1 # load iv
2789 movdqa $inout0,$inout2 # future iv
2791 &aesni_generate1("dec",$key,$rnds_);
2793 pxor $rndkey0,$rndkey0 # clear register bank
2794 pxor $rndkey1,$rndkey1
2795 movdqu $inout2,($ivp) # store iv
2796 xorps $inout1,$inout0 # ^=iv
2797 pxor $inout1,$inout1
2798 movups $inout0,($out) # store output
2799 pxor $inout0,$inout0
2805 sub \$$frame_size,%rsp
2806 and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
2808 $code.=<<___ if ($win64);
2809 movaps %xmm6,0x10(%rsp)
2810 movaps %xmm7,0x20(%rsp)
2811 movaps %xmm8,0x30(%rsp)
2812 movaps %xmm9,0x40(%rsp)
2813 movaps %xmm10,0x50(%rsp)
2814 movaps %xmm11,0x60(%rsp)
2815 movaps %xmm12,0x70(%rsp)
2816 movaps %xmm13,0x80(%rsp)
2817 movaps %xmm14,0x90(%rsp)
2818 movaps %xmm15,0xa0(%rsp)
2828 $movkey ($key),$rndkey0
2829 movdqu 0x00($inp),$inout0 # load input
2830 movdqu 0x10($inp),$inout1
2832 movdqu 0x20($inp),$inout2
2834 movdqu 0x30($inp),$inout3
2836 movdqu 0x40($inp),$inout4
2838 movdqu 0x50($inp),$inout5
2840 mov OPENSSL_ia32cap_P+4(%rip),%r9d
2842 jbe .Lcbc_dec_six_or_seven
2844 and \$`1<<26|1<<22`,%r9d # isolate XSAVE+MOVBE
2845 sub \$0x50,$len # $len is biased by -5*16
2846 cmp \$`1<<22`,%r9d # check for MOVBE without XSAVE
2847 je .Lcbc_dec_loop6_enter # [which denotes Atom Silvermont]
2848 sub \$0x20,$len # $len is biased by -7*16
2849 lea 0x70($key),$key # size optimization
2850 jmp .Lcbc_dec_loop8_enter
2853 movups $inout7,($out)
2855 .Lcbc_dec_loop8_enter:
2856 movdqu 0x60($inp),$inout6
2857 pxor $rndkey0,$inout0
2858 movdqu 0x70($inp),$inout7
2859 pxor $rndkey0,$inout1
2860 $movkey 0x10-0x70($key),$rndkey1
2861 pxor $rndkey0,$inout2
2863 cmp \$0x70,$len # is there at least 0x60 bytes ahead?
2864 pxor $rndkey0,$inout3
2865 pxor $rndkey0,$inout4
2866 pxor $rndkey0,$inout5
2867 pxor $rndkey0,$inout6
2869 aesdec $rndkey1,$inout0
2870 pxor $rndkey0,$inout7
2871 $movkey 0x20-0x70($key),$rndkey0
2872 aesdec $rndkey1,$inout1
2873 aesdec $rndkey1,$inout2
2874 aesdec $rndkey1,$inout3
2875 aesdec $rndkey1,$inout4
2876 aesdec $rndkey1,$inout5
2877 aesdec $rndkey1,$inout6
2880 aesdec $rndkey1,$inout7
2882 $movkey 0x30-0x70($key),$rndkey1
2884 for($i=1;$i<12;$i++) {
2885 my $rndkeyx = ($i&1)?$rndkey0:$rndkey1;
2886 $code.=<<___ if ($i==7);
2890 aesdec $rndkeyx,$inout0
2891 aesdec $rndkeyx,$inout1
2892 aesdec $rndkeyx,$inout2
2893 aesdec $rndkeyx,$inout3
2894 aesdec $rndkeyx,$inout4
2895 aesdec $rndkeyx,$inout5
2896 aesdec $rndkeyx,$inout6
2897 aesdec $rndkeyx,$inout7
2898 $movkey `0x30+0x10*$i`-0x70($key),$rndkeyx
2900 $code.=<<___ if ($i<6 || (!($i&1) && $i>7));
2903 $code.=<<___ if ($i==7);
2906 $code.=<<___ if ($i==9);
2909 $code.=<<___ if ($i==11);
2916 aesdec $rndkey1,$inout0
2917 aesdec $rndkey1,$inout1
2920 aesdec $rndkey1,$inout2
2921 aesdec $rndkey1,$inout3
2924 aesdec $rndkey1,$inout4
2925 aesdec $rndkey1,$inout5
2928 aesdec $rndkey1,$inout6
2929 aesdec $rndkey1,$inout7
2930 movdqu 0x50($inp),$rndkey1
2932 aesdeclast $iv,$inout0
2933 movdqu 0x60($inp),$iv # borrow $iv
2934 pxor $rndkey0,$rndkey1
2935 aesdeclast $in0,$inout1
2937 movdqu 0x70($inp),$rndkey0 # next IV
2938 aesdeclast $in1,$inout2
2940 movdqu 0x00($inp_),$in0
2941 aesdeclast $in2,$inout3
2942 aesdeclast $in3,$inout4
2943 movdqu 0x10($inp_),$in1
2944 movdqu 0x20($inp_),$in2
2945 aesdeclast $in4,$inout5
2946 aesdeclast $rndkey1,$inout6
2947 movdqu 0x30($inp_),$in3
2948 movdqu 0x40($inp_),$in4
2949 aesdeclast $iv,$inout7
2950 movdqa $rndkey0,$iv # return $iv
2951 movdqu 0x50($inp_),$rndkey1
2952 $movkey -0x70($key),$rndkey0
2954 movups $inout0,($out) # store output
2956 movups $inout1,0x10($out)
2958 movups $inout2,0x20($out)
2960 movups $inout3,0x30($out)
2962 movups $inout4,0x40($out)
2964 movups $inout5,0x50($out)
2965 movdqa $rndkey1,$inout5
2966 movups $inout6,0x60($out)
2972 movaps $inout7,$inout0
2973 lea -0x70($key),$key
2975 jle .Lcbc_dec_clear_tail_collected
2976 movups $inout7,($out)
2982 .Lcbc_dec_six_or_seven:
2986 movaps $inout5,$inout6
2987 call _aesni_decrypt6
2988 pxor $iv,$inout0 # ^= IV
2991 movdqu $inout0,($out)
2993 movdqu $inout1,0x10($out)
2994 pxor $inout1,$inout1 # clear register bank
2996 movdqu $inout2,0x20($out)
2997 pxor $inout2,$inout2
2999 movdqu $inout3,0x30($out)
3000 pxor $inout3,$inout3
3002 movdqu $inout4,0x40($out)
3003 pxor $inout4,$inout4
3005 movdqa $inout5,$inout0
3006 pxor $inout5,$inout5
3007 jmp .Lcbc_dec_tail_collected
3011 movups 0x60($inp),$inout6
3012 xorps $inout7,$inout7
3013 call _aesni_decrypt8
3014 movups 0x50($inp),$inout7
3015 pxor $iv,$inout0 # ^= IV
3016 movups 0x60($inp),$iv
3018 movdqu $inout0,($out)
3020 movdqu $inout1,0x10($out)
3021 pxor $inout1,$inout1 # clear register bank
3023 movdqu $inout2,0x20($out)
3024 pxor $inout2,$inout2
3026 movdqu $inout3,0x30($out)
3027 pxor $inout3,$inout3
3029 movdqu $inout4,0x40($out)
3030 pxor $inout4,$inout4
3031 pxor $inout7,$inout6
3032 movdqu $inout5,0x50($out)
3033 pxor $inout5,$inout5
3035 movdqa $inout6,$inout0
3036 pxor $inout6,$inout6
3037 pxor $inout7,$inout7
3038 jmp .Lcbc_dec_tail_collected
3042 movups $inout5,($out)
3044 movdqu 0x00($inp),$inout0 # load input
3045 movdqu 0x10($inp),$inout1
3047 movdqu 0x20($inp),$inout2
3049 movdqu 0x30($inp),$inout3
3051 movdqu 0x40($inp),$inout4
3053 movdqu 0x50($inp),$inout5
3055 .Lcbc_dec_loop6_enter:
3057 movdqa $inout5,$inout6
3059 call _aesni_decrypt6
3061 pxor $iv,$inout0 # ^= IV
3064 movdqu $inout0,($out)
3066 movdqu $inout1,0x10($out)
3068 movdqu $inout2,0x20($out)
3071 movdqu $inout3,0x30($out)
3074 movdqu $inout4,0x40($out)
3079 movdqa $inout5,$inout0
3081 jle .Lcbc_dec_clear_tail_collected
3082 movups $inout5,($out)
3086 movups ($inp),$inout0
3088 jbe .Lcbc_dec_one # $len is 1*16 or less
3090 movups 0x10($inp),$inout1
3093 jbe .Lcbc_dec_two # $len is 2*16 or less
3095 movups 0x20($inp),$inout2
3098 jbe .Lcbc_dec_three # $len is 3*16 or less
3100 movups 0x30($inp),$inout3
3103 jbe .Lcbc_dec_four # $len is 4*16 or less
3105 movups 0x40($inp),$inout4 # $len is 5*16 or less
3108 xorps $inout5,$inout5
3109 call _aesni_decrypt6
3113 movdqu $inout0,($out)
3115 movdqu $inout1,0x10($out)
3116 pxor $inout1,$inout1 # clear register bank
3118 movdqu $inout2,0x20($out)
3119 pxor $inout2,$inout2
3121 movdqu $inout3,0x30($out)
3122 pxor $inout3,$inout3
3124 movdqa $inout4,$inout0
3125 pxor $inout4,$inout4
3126 pxor $inout5,$inout5
3128 jmp .Lcbc_dec_tail_collected
3134 &aesni_generate1("dec",$key,$rounds);
3138 jmp .Lcbc_dec_tail_collected
3142 call _aesni_decrypt2
3146 movdqu $inout0,($out)
3147 movdqa $inout1,$inout0
3148 pxor $inout1,$inout1 # clear register bank
3150 jmp .Lcbc_dec_tail_collected
3154 call _aesni_decrypt3
3158 movdqu $inout0,($out)
3160 movdqu $inout1,0x10($out)
3161 pxor $inout1,$inout1 # clear register bank
3162 movdqa $inout2,$inout0
3163 pxor $inout2,$inout2
3165 jmp .Lcbc_dec_tail_collected
3169 call _aesni_decrypt4
3173 movdqu $inout0,($out)
3175 movdqu $inout1,0x10($out)
3176 pxor $inout1,$inout1 # clear register bank
3178 movdqu $inout2,0x20($out)
3179 pxor $inout2,$inout2
3180 movdqa $inout3,$inout0
3181 pxor $inout3,$inout3
3183 jmp .Lcbc_dec_tail_collected
3186 .Lcbc_dec_clear_tail_collected:
3187 pxor $inout1,$inout1 # clear register bank
3188 pxor $inout2,$inout2
3189 pxor $inout3,$inout3
3191 $code.=<<___ if (!$win64);
3192 pxor $inout4,$inout4 # %xmm6..9
3193 pxor $inout5,$inout5
3194 pxor $inout6,$inout6
3195 pxor $inout7,$inout7
3198 .Lcbc_dec_tail_collected:
3201 jnz .Lcbc_dec_tail_partial
3202 movups $inout0,($out)
3203 pxor $inout0,$inout0
3206 .Lcbc_dec_tail_partial:
3207 movaps $inout0,(%rsp)
3208 pxor $inout0,$inout0
3213 .long 0x9066A4F3 # rep movsb
3214 movdqa $inout0,(%rsp)
3217 xorps $rndkey0,$rndkey0 # %xmm0
3218 pxor $rndkey1,$rndkey1
3220 $code.=<<___ if ($win64);
3221 movaps 0x10(%rsp),%xmm6
3222 movaps %xmm0,0x10(%rsp) # clear stack
3223 movaps 0x20(%rsp),%xmm7
3224 movaps %xmm0,0x20(%rsp)
3225 movaps 0x30(%rsp),%xmm8
3226 movaps %xmm0,0x30(%rsp)
3227 movaps 0x40(%rsp),%xmm9
3228 movaps %xmm0,0x40(%rsp)
3229 movaps 0x50(%rsp),%xmm10
3230 movaps %xmm0,0x50(%rsp)
3231 movaps 0x60(%rsp),%xmm11
3232 movaps %xmm0,0x60(%rsp)
3233 movaps 0x70(%rsp),%xmm12
3234 movaps %xmm0,0x70(%rsp)
3235 movaps 0x80(%rsp),%xmm13
3236 movaps %xmm0,0x80(%rsp)
3237 movaps 0x90(%rsp),%xmm14
3238 movaps %xmm0,0x90(%rsp)
3239 movaps 0xa0(%rsp),%xmm15
3240 movaps %xmm0,0xa0(%rsp)
3247 .size ${PREFIX}_cbc_encrypt,.-${PREFIX}_cbc_encrypt
3250 # int ${PREFIX}_set_decrypt_key(const unsigned char *inp,
3251 # int bits, AES_KEY *key)
3253 # input: $inp user-supplied key
3254 # $bits $inp length in bits
3255 # $key pointer to key schedule
3256 # output: %eax 0 denoting success, -1 or -2 - failure (see C)
3257 # *$key key schedule
3259 { my ($inp,$bits,$key) = @_4args;
3263 .globl ${PREFIX}_set_decrypt_key
3264 .type ${PREFIX}_set_decrypt_key,\@abi-omnipotent
3266 ${PREFIX}_set_decrypt_key:
3267 .byte 0x48,0x83,0xEC,0x08 # sub rsp,8
3268 call __aesni_set_encrypt_key
3269 shl \$4,$bits # rounds-1 after _aesni_set_encrypt_key
3272 lea 16($key,$bits),$inp # points at the end of key schedule
3274 $movkey ($key),%xmm0 # just swap
3275 $movkey ($inp),%xmm1
3276 $movkey %xmm0,($inp)
3277 $movkey %xmm1,($key)
3282 $movkey ($key),%xmm0 # swap and inverse
3283 $movkey ($inp),%xmm1
3288 $movkey %xmm0,16($inp)
3289 $movkey %xmm1,-16($key)
3291 ja .Ldec_key_inverse
3293 $movkey ($key),%xmm0 # inverse middle
3296 $movkey %xmm0,($inp)
3301 .LSEH_end_set_decrypt_key:
3302 .size ${PREFIX}_set_decrypt_key,.-${PREFIX}_set_decrypt_key
3305 # This is based on submission by
3307 # Huang Ying <ying.huang@intel.com>
3308 # Vinodh Gopal <vinodh.gopal@intel.com>
3311 # Agressively optimized in respect to aeskeygenassist's critical path
3312 # and is contained in %xmm0-5 to meet Win64 ABI requirement.
3314 # int ${PREFIX}_set_encrypt_key(const unsigned char *inp,
3315 # int bits, AES_KEY * const key);
3317 # input: $inp user-supplied key
3318 # $bits $inp length in bits
3319 # $key pointer to key schedule
3320 # output: %eax 0 denoting success, -1 or -2 - failure (see C)
3321 # $bits rounds-1 (used in aesni_set_decrypt_key)
3322 # *$key key schedule
3323 # $key pointer to key schedule (used in
3324 # aesni_set_decrypt_key)
3326 # Subroutine is frame-less, which means that only volatile registers
3327 # are used. Note that it's declared "abi-omnipotent", which means that
3328 # amount of volatile registers is smaller on Windows.
3331 .globl ${PREFIX}_set_encrypt_key
3332 .type ${PREFIX}_set_encrypt_key,\@abi-omnipotent
3334 ${PREFIX}_set_encrypt_key:
3335 __aesni_set_encrypt_key:
3336 .byte 0x48,0x83,0xEC,0x08 # sub rsp,8
3343 mov \$`1<<28|1<<11`,%r10d # AVX and XOP bits
3344 movups ($inp),%xmm0 # pull first 128 bits of *userKey
3345 xorps %xmm4,%xmm4 # low dword of xmm4 is assumed 0
3346 and OPENSSL_ia32cap_P+4(%rip),%r10d
3347 lea 16($key),%rax # %rax is used as modifiable copy of $key
3356 mov \$9,$bits # 10 rounds for 128-bit key
3357 cmp \$`1<<28`,%r10d # AVX, bit no XOP
3360 $movkey %xmm0,($key) # round 0
3361 aeskeygenassist \$0x1,%xmm0,%xmm1 # round 1
3362 call .Lkey_expansion_128_cold
3363 aeskeygenassist \$0x2,%xmm0,%xmm1 # round 2
3364 call .Lkey_expansion_128
3365 aeskeygenassist \$0x4,%xmm0,%xmm1 # round 3
3366 call .Lkey_expansion_128
3367 aeskeygenassist \$0x8,%xmm0,%xmm1 # round 4
3368 call .Lkey_expansion_128
3369 aeskeygenassist \$0x10,%xmm0,%xmm1 # round 5
3370 call .Lkey_expansion_128
3371 aeskeygenassist \$0x20,%xmm0,%xmm1 # round 6
3372 call .Lkey_expansion_128
3373 aeskeygenassist \$0x40,%xmm0,%xmm1 # round 7
3374 call .Lkey_expansion_128
3375 aeskeygenassist \$0x80,%xmm0,%xmm1 # round 8
3376 call .Lkey_expansion_128
3377 aeskeygenassist \$0x1b,%xmm0,%xmm1 # round 9
3378 call .Lkey_expansion_128
3379 aeskeygenassist \$0x36,%xmm0,%xmm1 # round 10
3380 call .Lkey_expansion_128
3381 $movkey %xmm0,(%rax)
3382 mov $bits,80(%rax) # 240(%rdx)
3388 movdqa .Lkey_rotate(%rip),%xmm5
3390 movdqa .Lkey_rcon1(%rip),%xmm4
3398 aesenclast %xmm4,%xmm0
3411 movdqu %xmm0,-16(%rax)
3417 movdqa .Lkey_rcon1b(%rip),%xmm4
3420 aesenclast %xmm4,%xmm0
3436 aesenclast %xmm4,%xmm0
3447 movdqu %xmm0,16(%rax)
3449 mov $bits,96(%rax) # 240($key)
3455 movq 16($inp),%xmm2 # remaining 1/3 of *userKey
3456 mov \$11,$bits # 12 rounds for 192
3457 cmp \$`1<<28`,%r10d # AVX, but no XOP
3460 $movkey %xmm0,($key) # round 0
3461 aeskeygenassist \$0x1,%xmm2,%xmm1 # round 1,2
3462 call .Lkey_expansion_192a_cold
3463 aeskeygenassist \$0x2,%xmm2,%xmm1 # round 2,3
3464 call .Lkey_expansion_192b
3465 aeskeygenassist \$0x4,%xmm2,%xmm1 # round 4,5
3466 call .Lkey_expansion_192a
3467 aeskeygenassist \$0x8,%xmm2,%xmm1 # round 5,6
3468 call .Lkey_expansion_192b
3469 aeskeygenassist \$0x10,%xmm2,%xmm1 # round 7,8
3470 call .Lkey_expansion_192a
3471 aeskeygenassist \$0x20,%xmm2,%xmm1 # round 8,9
3472 call .Lkey_expansion_192b
3473 aeskeygenassist \$0x40,%xmm2,%xmm1 # round 10,11
3474 call .Lkey_expansion_192a
3475 aeskeygenassist \$0x80,%xmm2,%xmm1 # round 11,12
3476 call .Lkey_expansion_192b
3477 $movkey %xmm0,(%rax)
3478 mov $bits,48(%rax) # 240(%rdx)
3484 movdqa .Lkey_rotate192(%rip),%xmm5
3485 movdqa .Lkey_rcon1(%rip),%xmm4
3495 aesenclast %xmm4,%xmm2
3507 pshufd \$0xff,%xmm0,%xmm3
3514 movdqu %xmm0,-16(%rax)
3519 mov $bits,32(%rax) # 240($key)
3525 movups 16($inp),%xmm2 # remaning half of *userKey
3526 mov \$13,$bits # 14 rounds for 256
3528 cmp \$`1<<28`,%r10d # AVX, but no XOP
3531 $movkey %xmm0,($key) # round 0
3532 $movkey %xmm2,16($key) # round 1
3533 aeskeygenassist \$0x1,%xmm2,%xmm1 # round 2
3534 call .Lkey_expansion_256a_cold
3535 aeskeygenassist \$0x1,%xmm0,%xmm1 # round 3
3536 call .Lkey_expansion_256b
3537 aeskeygenassist \$0x2,%xmm2,%xmm1 # round 4
3538 call .Lkey_expansion_256a
3539 aeskeygenassist \$0x2,%xmm0,%xmm1 # round 5
3540 call .Lkey_expansion_256b
3541 aeskeygenassist \$0x4,%xmm2,%xmm1 # round 6
3542 call .Lkey_expansion_256a
3543 aeskeygenassist \$0x4,%xmm0,%xmm1 # round 7
3544 call .Lkey_expansion_256b
3545 aeskeygenassist \$0x8,%xmm2,%xmm1 # round 8
3546 call .Lkey_expansion_256a
3547 aeskeygenassist \$0x8,%xmm0,%xmm1 # round 9
3548 call .Lkey_expansion_256b
3549 aeskeygenassist \$0x10,%xmm2,%xmm1 # round 10
3550 call .Lkey_expansion_256a
3551 aeskeygenassist \$0x10,%xmm0,%xmm1 # round 11
3552 call .Lkey_expansion_256b
3553 aeskeygenassist \$0x20,%xmm2,%xmm1 # round 12
3554 call .Lkey_expansion_256a
3555 aeskeygenassist \$0x20,%xmm0,%xmm1 # round 13
3556 call .Lkey_expansion_256b
3557 aeskeygenassist \$0x40,%xmm2,%xmm1 # round 14
3558 call .Lkey_expansion_256a
3559 $movkey %xmm0,(%rax)
3560 mov $bits,16(%rax) # 240(%rdx)
3566 movdqa .Lkey_rotate(%rip),%xmm5
3567 movdqa .Lkey_rcon1(%rip),%xmm4
3569 movdqu %xmm0,0($key)
3571 movdqu %xmm2,16($key)
3577 aesenclast %xmm4,%xmm2
3594 pshufd \$0xff,%xmm0,%xmm2
3596 aesenclast %xmm3,%xmm2
3607 movdqu %xmm2,16(%rax)
3614 mov $bits,16(%rax) # 240($key)
3630 .LSEH_end_set_encrypt_key:
3633 .Lkey_expansion_128:
3634 $movkey %xmm0,(%rax)
3636 .Lkey_expansion_128_cold:
3637 shufps \$0b00010000,%xmm0,%xmm4
3639 shufps \$0b10001100,%xmm0,%xmm4
3641 shufps \$0b11111111,%xmm1,%xmm1 # critical path
3646 .Lkey_expansion_192a:
3647 $movkey %xmm0,(%rax)
3649 .Lkey_expansion_192a_cold:
3651 .Lkey_expansion_192b_warm:
3652 shufps \$0b00010000,%xmm0,%xmm4
3655 shufps \$0b10001100,%xmm0,%xmm4
3658 pshufd \$0b01010101,%xmm1,%xmm1 # critical path
3661 pshufd \$0b11111111,%xmm0,%xmm3
3666 .Lkey_expansion_192b:
3668 shufps \$0b01000100,%xmm0,%xmm5
3669 $movkey %xmm5,(%rax)
3670 shufps \$0b01001110,%xmm2,%xmm3
3671 $movkey %xmm3,16(%rax)
3673 jmp .Lkey_expansion_192b_warm
3676 .Lkey_expansion_256a:
3677 $movkey %xmm2,(%rax)
3679 .Lkey_expansion_256a_cold:
3680 shufps \$0b00010000,%xmm0,%xmm4
3682 shufps \$0b10001100,%xmm0,%xmm4
3684 shufps \$0b11111111,%xmm1,%xmm1 # critical path
3689 .Lkey_expansion_256b:
3690 $movkey %xmm0,(%rax)
3693 shufps \$0b00010000,%xmm2,%xmm4
3695 shufps \$0b10001100,%xmm2,%xmm4
3697 shufps \$0b10101010,%xmm1,%xmm1 # critical path
3700 .size ${PREFIX}_set_encrypt_key,.-${PREFIX}_set_encrypt_key
3701 .size __aesni_set_encrypt_key,.-__aesni_set_encrypt_key
3708 .byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
3716 .byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
3718 .long 0x0c0f0e0d,0x0c0f0e0d,0x0c0f0e0d,0x0c0f0e0d
3720 .long 0x04070605,0x04070605,0x04070605,0x04070605
3724 .long 0x1b,0x1b,0x1b,0x1b
3726 .asciz "AES for Intel AES-NI, CRYPTOGAMS by <appro\@openssl.org>"
3730 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
3731 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
3739 .extern __imp_RtlVirtualUnwind
3741 $code.=<<___ if ($PREFIX eq "aesni");
3742 .type ecb_ccm64_se_handler,\@abi-omnipotent
3744 ecb_ccm64_se_handler:
3756 mov 120($context),%rax # pull context->Rax
3757 mov 248($context),%rbx # pull context->Rip
3759 mov 8($disp),%rsi # disp->ImageBase
3760 mov 56($disp),%r11 # disp->HandlerData
3762 mov 0(%r11),%r10d # HandlerData[0]
3763 lea (%rsi,%r10),%r10 # prologue label
3764 cmp %r10,%rbx # context->Rip<prologue label
3765 jb .Lcommon_seh_tail
3767 mov 152($context),%rax # pull context->Rsp
3769 mov 4(%r11),%r10d # HandlerData[1]
3770 lea (%rsi,%r10),%r10 # epilogue label
3771 cmp %r10,%rbx # context->Rip>=epilogue label
3772 jae .Lcommon_seh_tail
3774 lea 0(%rax),%rsi # %xmm save area
3775 lea 512($context),%rdi # &context.Xmm6
3776 mov \$8,%ecx # 4*sizeof(%xmm0)/sizeof(%rax)
3777 .long 0xa548f3fc # cld; rep movsq
3778 lea 0x58(%rax),%rax # adjust stack pointer
3780 jmp .Lcommon_seh_tail
3781 .size ecb_ccm64_se_handler,.-ecb_ccm64_se_handler
3783 .type ctr_xts_se_handler,\@abi-omnipotent
3797 mov 120($context),%rax # pull context->Rax
3798 mov 248($context),%rbx # pull context->Rip
3800 mov 8($disp),%rsi # disp->ImageBase
3801 mov 56($disp),%r11 # disp->HandlerData
3803 mov 0(%r11),%r10d # HandlerData[0]
3804 lea (%rsi,%r10),%r10 # prologue lable