2 # Copyright 2009-2016 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the OpenSSL license (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
10 # ====================================================================
11 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
12 # project. The module is, however, dual licensed under OpenSSL and
13 # CRYPTOGAMS licenses depending on where you obtain it. For further
14 # details see http://www.openssl.org/~appro/cryptogams/.
15 # ====================================================================
17 # This module implements support for Intel AES-NI extension. In
18 # OpenSSL context it's used with Intel engine, but can also be used as
19 # drop-in replacement for crypto/aes/asm/aes-x86_64.pl [see below for
24 # Given aes(enc|dec) instructions' latency asymptotic performance for
25 # non-parallelizable modes such as CBC encrypt is 3.75 cycles per byte
26 # processed with 128-bit key. And given their throughput asymptotic
27 # performance for parallelizable modes is 1.25 cycles per byte. Being
28 # asymptotic limit it's not something you commonly achieve in reality,
29 # but how close does one get? Below are results collected for
30 # different modes and block sized. Pairs of numbers are for en-/
33 # 16-byte 64-byte 256-byte 1-KB 8-KB
34 # ECB 4.25/4.25 1.38/1.38 1.28/1.28 1.26/1.26 1.26/1.26
35 # CTR 5.42/5.42 1.92/1.92 1.44/1.44 1.28/1.28 1.26/1.26
36 # CBC 4.38/4.43 4.15/1.43 4.07/1.32 4.07/1.29 4.06/1.28
37 # CCM 5.66/9.42 4.42/5.41 4.16/4.40 4.09/4.15 4.06/4.07
38 # OFB 5.42/5.42 4.64/4.64 4.44/4.44 4.39/4.39 4.38/4.38
39 # CFB 5.73/5.85 5.56/5.62 5.48/5.56 5.47/5.55 5.47/5.55
41 # ECB, CTR, CBC and CCM results are free from EVP overhead. This means
42 # that otherwise used 'openssl speed -evp aes-128-??? -engine aesni
43 # [-decrypt]' will exhibit 10-15% worse results for smaller blocks.
44 # The results were collected with specially crafted speed.c benchmark
45 # in order to compare them with results reported in "Intel Advanced
46 # Encryption Standard (AES) New Instruction Set" White Paper Revision
47 # 3.0 dated May 2010. All above results are consistently better. This
48 # module also provides better performance for block sizes smaller than
49 # 128 bytes in points *not* represented in the above table.
51 # Looking at the results for 8-KB buffer.
53 # CFB and OFB results are far from the limit, because implementation
54 # uses "generic" CRYPTO_[c|o]fb128_encrypt interfaces relying on
55 # single-block aesni_encrypt, which is not the most optimal way to go.
56 # CBC encrypt result is unexpectedly high and there is no documented
57 # explanation for it. Seemingly there is a small penalty for feeding
58 # the result back to AES unit the way it's done in CBC mode. There is
59 # nothing one can do and the result appears optimal. CCM result is
60 # identical to CBC, because CBC-MAC is essentially CBC encrypt without
61 # saving output. CCM CTR "stays invisible," because it's neatly
62 # interleaved wih CBC-MAC. This provides ~30% improvement over
63 # "straghtforward" CCM implementation with CTR and CBC-MAC performed
64 # disjointly. Parallelizable modes practically achieve the theoretical
67 # Looking at how results vary with buffer size.
69 # Curves are practically saturated at 1-KB buffer size. In most cases
70 # "256-byte" performance is >95%, and "64-byte" is ~90% of "8-KB" one.
71 # CTR curve doesn't follow this pattern and is "slowest" changing one
72 # with "256-byte" result being 87% of "8-KB." This is because overhead
73 # in CTR mode is most computationally intensive. Small-block CCM
74 # decrypt is slower than encrypt, because first CTR and last CBC-MAC
75 # iterations can't be interleaved.
77 # Results for 192- and 256-bit keys.
79 # EVP-free results were observed to scale perfectly with number of
80 # rounds for larger block sizes, i.e. 192-bit result being 10/12 times
81 # lower and 256-bit one - 10/14. Well, in CBC encrypt case differences
82 # are a tad smaller, because the above mentioned penalty biases all
83 # results by same constant value. In similar way function call
84 # overhead affects small-block performance, as well as OFB and CFB
85 # results. Differences are not large, most common coefficients are
86 # 10/11.7 and 10/13.4 (as opposite to 10/12.0 and 10/14.0), but one
87 # observe even 10/11.2 and 10/12.4 (CTR, OFB, CFB)...
91 # While Westmere processor features 6 cycles latency for aes[enc|dec]
92 # instructions, which can be scheduled every second cycle, Sandy
93 # Bridge spends 8 cycles per instruction, but it can schedule them
94 # every cycle. This means that code targeting Westmere would perform
95 # suboptimally on Sandy Bridge. Therefore this update.
97 # In addition, non-parallelizable CBC encrypt (as well as CCM) is
98 # optimized. Relative improvement might appear modest, 8% on Westmere,
99 # but in absolute terms it's 3.77 cycles per byte encrypted with
100 # 128-bit key on Westmere, and 5.07 - on Sandy Bridge. These numbers
101 # should be compared to asymptotic limits of 3.75 for Westmere and
102 # 5.00 for Sandy Bridge. Actually, the fact that they get this close
103 # to asymptotic limits is quite amazing. Indeed, the limit is
104 # calculated as latency times number of rounds, 10 for 128-bit key,
105 # and divided by 16, the number of bytes in block, or in other words
106 # it accounts *solely* for aesenc instructions. But there are extra
107 # instructions, and numbers so close to the asymptotic limits mean
108 # that it's as if it takes as little as *one* additional cycle to
109 # execute all of them. How is it possible? It is possible thanks to
110 # out-of-order execution logic, which manages to overlap post-
111 # processing of previous block, things like saving the output, with
112 # actual encryption of current block, as well as pre-processing of
113 # current block, things like fetching input and xor-ing it with
114 # 0-round element of the key schedule, with actual encryption of
115 # previous block. Keep this in mind...
117 # For parallelizable modes, such as ECB, CBC decrypt, CTR, higher
118 # performance is achieved by interleaving instructions working on
119 # independent blocks. In which case asymptotic limit for such modes
120 # can be obtained by dividing above mentioned numbers by AES
121 # instructions' interleave factor. Westmere can execute at most 3
122 # instructions at a time, meaning that optimal interleave factor is 3,
123 # and that's where the "magic" number of 1.25 come from. "Optimal
124 # interleave factor" means that increase of interleave factor does
125 # not improve performance. The formula has proven to reflect reality
126 # pretty well on Westmere... Sandy Bridge on the other hand can
127 # execute up to 8 AES instructions at a time, so how does varying
128 # interleave factor affect the performance? Here is table for ECB
129 # (numbers are cycles per byte processed with 128-bit key):
131 # instruction interleave factor 3x 6x 8x
132 # theoretical asymptotic limit 1.67 0.83 0.625
133 # measured performance for 8KB block 1.05 0.86 0.84
135 # "as if" interleave factor 4.7x 5.8x 6.0x
137 # Further data for other parallelizable modes:
139 # CBC decrypt 1.16 0.93 0.74
142 # Well, given 3x column it's probably inappropriate to call the limit
143 # asymptotic, if it can be surpassed, isn't it? What happens there?
144 # Rewind to CBC paragraph for the answer. Yes, out-of-order execution
145 # magic is responsible for this. Processor overlaps not only the
146 # additional instructions with AES ones, but even AES instuctions
147 # processing adjacent triplets of independent blocks. In the 6x case
148 # additional instructions still claim disproportionally small amount
149 # of additional cycles, but in 8x case number of instructions must be
150 # a tad too high for out-of-order logic to cope with, and AES unit
151 # remains underutilized... As you can see 8x interleave is hardly
152 # justifiable, so there no need to feel bad that 32-bit aesni-x86.pl
153 # utilizies 6x interleave because of limited register bank capacity.
155 # Higher interleave factors do have negative impact on Westmere
156 # performance. While for ECB mode it's negligible ~1.5%, other
157 # parallelizables perform ~5% worse, which is outweighed by ~25%
158 # improvement on Sandy Bridge. To balance regression on Westmere
159 # CTR mode was implemented with 6x aesenc interleave factor.
163 # Add aesni_xts_[en|de]crypt. Westmere spends 1.25 cycles processing
164 # one byte out of 8KB with 128-bit key, Sandy Bridge - 0.90. Just like
165 # in CTR mode AES instruction interleave factor was chosen to be 6x.
169 # Add aesni_ocb_[en|de]crypt. AES instruction interleave factor was
172 ######################################################################
173 # Current large-block performance in cycles per byte processed with
174 # 128-bit key (less is better).
176 # CBC en-/decrypt CTR XTS ECB OCB
177 # Westmere 3.77/1.25 1.25 1.25 1.26
178 # * Bridge 5.07/0.74 0.75 0.90 0.85 0.98
179 # Haswell 4.44/0.63 0.63 0.73 0.63 0.70
180 # Skylake 2.62/0.63 0.63 0.63 0.63
181 # Silvermont 5.75/3.54 3.56 4.12 3.87(*) 4.11
182 # Goldmont 3.82/1.26 1.26 1.29 1.29 1.50
183 # Bulldozer 5.77/0.70 0.72 0.90 0.70 0.95
185 # (*) Atom Silvermont ECB result is suboptimal because of penalties
186 # incurred by operations on %xmm8-15. As ECB is not considered
187 # critical, nothing was done to mitigate the problem.
189 $PREFIX="aesni"; # if $PREFIX is set to "AES", the script
190 # generates drop-in replacement for
191 # crypto/aes/asm/aes-x86_64.pl:-)
195 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
197 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
199 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
200 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
201 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
202 die "can't locate x86_64-xlate.pl";
204 open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\"";
207 $movkey = $PREFIX eq "aesni" ? "movups" : "movups";
208 @_4args=$win64? ("%rcx","%rdx","%r8", "%r9") : # Win64 order
209 ("%rdi","%rsi","%rdx","%rcx"); # Unix order
212 $code.=".extern OPENSSL_ia32cap_P\n";
214 $rounds="%eax"; # input to and changed by aesni_[en|de]cryptN !!!
215 # this is natural Unix argument order for public $PREFIX_[ecb|cbc]_encrypt ...
219 $key="%rcx"; # input to and changed by aesni_[en|de]cryptN !!!
220 $ivp="%r8"; # cbc, ctr, ...
222 $rnds_="%r10d"; # backup copy for $rounds
223 $key_="%r11"; # backup copy for $key
225 # %xmm register layout
226 $rndkey0="%xmm0"; $rndkey1="%xmm1";
227 $inout0="%xmm2"; $inout1="%xmm3";
228 $inout2="%xmm4"; $inout3="%xmm5";
229 $inout4="%xmm6"; $inout5="%xmm7";
230 $inout6="%xmm8"; $inout7="%xmm9";
232 $in2="%xmm6"; $in1="%xmm7"; # used in CBC decrypt, CTR, ...
233 $in0="%xmm8"; $iv="%xmm9";
235 # Inline version of internal aesni_[en|de]crypt1.
237 # Why folded loop? Because aes[enc|dec] is slow enough to accommodate
238 # cycles which take care of loop variables...
240 sub aesni_generate1 {
241 my ($p,$key,$rounds,$inout,$ivec)=@_; $inout=$inout0 if (!defined($inout));
244 $movkey ($key),$rndkey0
245 $movkey 16($key),$rndkey1
247 $code.=<<___ if (defined($ivec));
252 $code.=<<___ if (!defined($ivec));
254 xorps $rndkey0,$inout
258 aes${p} $rndkey1,$inout
260 $movkey ($key),$rndkey1
262 jnz .Loop_${p}1_$sn # loop body is 16 bytes
263 aes${p}last $rndkey1,$inout
266 # void $PREFIX_[en|de]crypt (const void *inp,void *out,const AES_KEY *key);
268 { my ($inp,$out,$key) = @_4args;
271 .globl ${PREFIX}_encrypt
272 .type ${PREFIX}_encrypt,\@abi-omnipotent
275 movups ($inp),$inout0 # load input
276 mov 240($key),$rounds # key->rounds
278 &aesni_generate1("enc",$key,$rounds);
280 pxor $rndkey0,$rndkey0 # clear register bank
281 pxor $rndkey1,$rndkey1
282 movups $inout0,($out) # output
285 .size ${PREFIX}_encrypt,.-${PREFIX}_encrypt
287 .globl ${PREFIX}_decrypt
288 .type ${PREFIX}_decrypt,\@abi-omnipotent
291 movups ($inp),$inout0 # load input
292 mov 240($key),$rounds # key->rounds
294 &aesni_generate1("dec",$key,$rounds);
296 pxor $rndkey0,$rndkey0 # clear register bank
297 pxor $rndkey1,$rndkey1
298 movups $inout0,($out) # output
301 .size ${PREFIX}_decrypt, .-${PREFIX}_decrypt
305 # _aesni_[en|de]cryptN are private interfaces, N denotes interleave
306 # factor. Why 3x subroutine were originally used in loops? Even though
307 # aes[enc|dec] latency was originally 6, it could be scheduled only
308 # every *2nd* cycle. Thus 3x interleave was the one providing optimal
309 # utilization, i.e. when subroutine's throughput is virtually same as
310 # of non-interleaved subroutine [for number of input blocks up to 3].
311 # This is why it originally made no sense to implement 2x subroutine.
312 # But times change and it became appropriate to spend extra 192 bytes
313 # on 2x subroutine on Atom Silvermont account. For processors that
314 # can schedule aes[enc|dec] every cycle optimal interleave factor
315 # equals to corresponding instructions latency. 8x is optimal for
316 # * Bridge and "super-optimal" for other Intel CPUs...
318 sub aesni_generate2 {
320 # As already mentioned it takes in $key and $rounds, which are *not*
321 # preserved. $inout[0-1] is cipher/clear text...
323 .type _aesni_${dir}rypt2,\@abi-omnipotent
326 $movkey ($key),$rndkey0
328 $movkey 16($key),$rndkey1
329 xorps $rndkey0,$inout0
330 xorps $rndkey0,$inout1
331 $movkey 32($key),$rndkey0
332 lea 32($key,$rounds),$key
337 aes${dir} $rndkey1,$inout0
338 aes${dir} $rndkey1,$inout1
339 $movkey ($key,%rax),$rndkey1
341 aes${dir} $rndkey0,$inout0
342 aes${dir} $rndkey0,$inout1
343 $movkey -16($key,%rax),$rndkey0
346 aes${dir} $rndkey1,$inout0
347 aes${dir} $rndkey1,$inout1
348 aes${dir}last $rndkey0,$inout0
349 aes${dir}last $rndkey0,$inout1
351 .size _aesni_${dir}rypt2,.-_aesni_${dir}rypt2
354 sub aesni_generate3 {
356 # As already mentioned it takes in $key and $rounds, which are *not*
357 # preserved. $inout[0-2] is cipher/clear text...
359 .type _aesni_${dir}rypt3,\@abi-omnipotent
362 $movkey ($key),$rndkey0
364 $movkey 16($key),$rndkey1
365 xorps $rndkey0,$inout0
366 xorps $rndkey0,$inout1
367 xorps $rndkey0,$inout2
368 $movkey 32($key),$rndkey0
369 lea 32($key,$rounds),$key
374 aes${dir} $rndkey1,$inout0
375 aes${dir} $rndkey1,$inout1
376 aes${dir} $rndkey1,$inout2
377 $movkey ($key,%rax),$rndkey1
379 aes${dir} $rndkey0,$inout0
380 aes${dir} $rndkey0,$inout1
381 aes${dir} $rndkey0,$inout2
382 $movkey -16($key,%rax),$rndkey0
385 aes${dir} $rndkey1,$inout0
386 aes${dir} $rndkey1,$inout1
387 aes${dir} $rndkey1,$inout2
388 aes${dir}last $rndkey0,$inout0
389 aes${dir}last $rndkey0,$inout1
390 aes${dir}last $rndkey0,$inout2
392 .size _aesni_${dir}rypt3,.-_aesni_${dir}rypt3
395 # 4x interleave is implemented to improve small block performance,
396 # most notably [and naturally] 4 block by ~30%. One can argue that one
397 # should have implemented 5x as well, but improvement would be <20%,
398 # so it's not worth it...
399 sub aesni_generate4 {
401 # As already mentioned it takes in $key and $rounds, which are *not*
402 # preserved. $inout[0-3] is cipher/clear text...
404 .type _aesni_${dir}rypt4,\@abi-omnipotent
407 $movkey ($key),$rndkey0
409 $movkey 16($key),$rndkey1
410 xorps $rndkey0,$inout0
411 xorps $rndkey0,$inout1
412 xorps $rndkey0,$inout2
413 xorps $rndkey0,$inout3
414 $movkey 32($key),$rndkey0
415 lea 32($key,$rounds),$key
421 aes${dir} $rndkey1,$inout0
422 aes${dir} $rndkey1,$inout1
423 aes${dir} $rndkey1,$inout2
424 aes${dir} $rndkey1,$inout3
425 $movkey ($key,%rax),$rndkey1
427 aes${dir} $rndkey0,$inout0
428 aes${dir} $rndkey0,$inout1
429 aes${dir} $rndkey0,$inout2
430 aes${dir} $rndkey0,$inout3
431 $movkey -16($key,%rax),$rndkey0
434 aes${dir} $rndkey1,$inout0
435 aes${dir} $rndkey1,$inout1
436 aes${dir} $rndkey1,$inout2
437 aes${dir} $rndkey1,$inout3
438 aes${dir}last $rndkey0,$inout0
439 aes${dir}last $rndkey0,$inout1
440 aes${dir}last $rndkey0,$inout2
441 aes${dir}last $rndkey0,$inout3
443 .size _aesni_${dir}rypt4,.-_aesni_${dir}rypt4
446 sub aesni_generate6 {
448 # As already mentioned it takes in $key and $rounds, which are *not*
449 # preserved. $inout[0-5] is cipher/clear text...
451 .type _aesni_${dir}rypt6,\@abi-omnipotent
454 $movkey ($key),$rndkey0
456 $movkey 16($key),$rndkey1
457 xorps $rndkey0,$inout0
458 pxor $rndkey0,$inout1
459 pxor $rndkey0,$inout2
460 aes${dir} $rndkey1,$inout0
461 lea 32($key,$rounds),$key
463 aes${dir} $rndkey1,$inout1
464 pxor $rndkey0,$inout3
465 pxor $rndkey0,$inout4
466 aes${dir} $rndkey1,$inout2
467 pxor $rndkey0,$inout5
468 $movkey ($key,%rax),$rndkey0
470 jmp .L${dir}_loop6_enter
473 aes${dir} $rndkey1,$inout0
474 aes${dir} $rndkey1,$inout1
475 aes${dir} $rndkey1,$inout2
476 .L${dir}_loop6_enter:
477 aes${dir} $rndkey1,$inout3
478 aes${dir} $rndkey1,$inout4
479 aes${dir} $rndkey1,$inout5
480 $movkey ($key,%rax),$rndkey1
482 aes${dir} $rndkey0,$inout0
483 aes${dir} $rndkey0,$inout1
484 aes${dir} $rndkey0,$inout2
485 aes${dir} $rndkey0,$inout3
486 aes${dir} $rndkey0,$inout4
487 aes${dir} $rndkey0,$inout5
488 $movkey -16($key,%rax),$rndkey0
491 aes${dir} $rndkey1,$inout0
492 aes${dir} $rndkey1,$inout1
493 aes${dir} $rndkey1,$inout2
494 aes${dir} $rndkey1,$inout3
495 aes${dir} $rndkey1,$inout4
496 aes${dir} $rndkey1,$inout5
497 aes${dir}last $rndkey0,$inout0
498 aes${dir}last $rndkey0,$inout1
499 aes${dir}last $rndkey0,$inout2
500 aes${dir}last $rndkey0,$inout3
501 aes${dir}last $rndkey0,$inout4
502 aes${dir}last $rndkey0,$inout5
504 .size _aesni_${dir}rypt6,.-_aesni_${dir}rypt6
507 sub aesni_generate8 {
509 # As already mentioned it takes in $key and $rounds, which are *not*
510 # preserved. $inout[0-7] is cipher/clear text...
512 .type _aesni_${dir}rypt8,\@abi-omnipotent
515 $movkey ($key),$rndkey0
517 $movkey 16($key),$rndkey1
518 xorps $rndkey0,$inout0
519 xorps $rndkey0,$inout1
520 pxor $rndkey0,$inout2
521 pxor $rndkey0,$inout3
522 pxor $rndkey0,$inout4
523 lea 32($key,$rounds),$key
525 aes${dir} $rndkey1,$inout0
526 pxor $rndkey0,$inout5
527 pxor $rndkey0,$inout6
528 aes${dir} $rndkey1,$inout1
529 pxor $rndkey0,$inout7
530 $movkey ($key,%rax),$rndkey0
532 jmp .L${dir}_loop8_inner
535 aes${dir} $rndkey1,$inout0
536 aes${dir} $rndkey1,$inout1
537 .L${dir}_loop8_inner:
538 aes${dir} $rndkey1,$inout2
539 aes${dir} $rndkey1,$inout3
540 aes${dir} $rndkey1,$inout4
541 aes${dir} $rndkey1,$inout5
542 aes${dir} $rndkey1,$inout6
543 aes${dir} $rndkey1,$inout7
544 .L${dir}_loop8_enter:
545 $movkey ($key,%rax),$rndkey1
547 aes${dir} $rndkey0,$inout0
548 aes${dir} $rndkey0,$inout1
549 aes${dir} $rndkey0,$inout2
550 aes${dir} $rndkey0,$inout3
551 aes${dir} $rndkey0,$inout4
552 aes${dir} $rndkey0,$inout5
553 aes${dir} $rndkey0,$inout6
554 aes${dir} $rndkey0,$inout7
555 $movkey -16($key,%rax),$rndkey0
558 aes${dir} $rndkey1,$inout0
559 aes${dir} $rndkey1,$inout1
560 aes${dir} $rndkey1,$inout2
561 aes${dir} $rndkey1,$inout3
562 aes${dir} $rndkey1,$inout4
563 aes${dir} $rndkey1,$inout5
564 aes${dir} $rndkey1,$inout6
565 aes${dir} $rndkey1,$inout7
566 aes${dir}last $rndkey0,$inout0
567 aes${dir}last $rndkey0,$inout1
568 aes${dir}last $rndkey0,$inout2
569 aes${dir}last $rndkey0,$inout3
570 aes${dir}last $rndkey0,$inout4
571 aes${dir}last $rndkey0,$inout5
572 aes${dir}last $rndkey0,$inout6
573 aes${dir}last $rndkey0,$inout7
575 .size _aesni_${dir}rypt8,.-_aesni_${dir}rypt8
578 &aesni_generate2("enc") if ($PREFIX eq "aesni");
579 &aesni_generate2("dec");
580 &aesni_generate3("enc") if ($PREFIX eq "aesni");
581 &aesni_generate3("dec");
582 &aesni_generate4("enc") if ($PREFIX eq "aesni");
583 &aesni_generate4("dec");
584 &aesni_generate6("enc") if ($PREFIX eq "aesni");
585 &aesni_generate6("dec");
586 &aesni_generate8("enc") if ($PREFIX eq "aesni");
587 &aesni_generate8("dec");
589 if ($PREFIX eq "aesni") {
590 ########################################################################
591 # void aesni_ecb_encrypt (const void *in, void *out,
592 # size_t length, const AES_KEY *key,
595 .globl aesni_ecb_encrypt
596 .type aesni_ecb_encrypt,\@function,5
600 $code.=<<___ if ($win64);
602 movaps %xmm6,(%rsp) # offload $inout4..7
603 movaps %xmm7,0x10(%rsp)
604 movaps %xmm8,0x20(%rsp)
605 movaps %xmm9,0x30(%rsp)
609 and \$-16,$len # if ($len<16)
610 jz .Lecb_ret # return
612 mov 240($key),$rounds # key->rounds
613 $movkey ($key),$rndkey0
614 mov $key,$key_ # backup $key
615 mov $rounds,$rnds_ # backup $rounds
616 test %r8d,%r8d # 5th argument
618 #--------------------------- ECB ENCRYPT ------------------------------#
619 cmp \$0x80,$len # if ($len<8*16)
620 jb .Lecb_enc_tail # short input
622 movdqu ($inp),$inout0 # load 8 input blocks
623 movdqu 0x10($inp),$inout1
624 movdqu 0x20($inp),$inout2
625 movdqu 0x30($inp),$inout3
626 movdqu 0x40($inp),$inout4
627 movdqu 0x50($inp),$inout5
628 movdqu 0x60($inp),$inout6
629 movdqu 0x70($inp),$inout7
630 lea 0x80($inp),$inp # $inp+=8*16
631 sub \$0x80,$len # $len-=8*16 (can be zero)
632 jmp .Lecb_enc_loop8_enter
635 movups $inout0,($out) # store 8 output blocks
636 mov $key_,$key # restore $key
637 movdqu ($inp),$inout0 # load 8 input blocks
638 mov $rnds_,$rounds # restore $rounds
639 movups $inout1,0x10($out)
640 movdqu 0x10($inp),$inout1
641 movups $inout2,0x20($out)
642 movdqu 0x20($inp),$inout2
643 movups $inout3,0x30($out)
644 movdqu 0x30($inp),$inout3
645 movups $inout4,0x40($out)
646 movdqu 0x40($inp),$inout4
647 movups $inout5,0x50($out)
648 movdqu 0x50($inp),$inout5
649 movups $inout6,0x60($out)
650 movdqu 0x60($inp),$inout6
651 movups $inout7,0x70($out)
652 lea 0x80($out),$out # $out+=8*16
653 movdqu 0x70($inp),$inout7
654 lea 0x80($inp),$inp # $inp+=8*16
655 .Lecb_enc_loop8_enter:
660 jnc .Lecb_enc_loop8 # loop if $len-=8*16 didn't borrow
662 movups $inout0,($out) # store 8 output blocks
663 mov $key_,$key # restore $key
664 movups $inout1,0x10($out)
665 mov $rnds_,$rounds # restore $rounds
666 movups $inout2,0x20($out)
667 movups $inout3,0x30($out)
668 movups $inout4,0x40($out)
669 movups $inout5,0x50($out)
670 movups $inout6,0x60($out)
671 movups $inout7,0x70($out)
672 lea 0x80($out),$out # $out+=8*16
673 add \$0x80,$len # restore real remaining $len
674 jz .Lecb_ret # done if ($len==0)
676 .Lecb_enc_tail: # $len is less than 8*16
677 movups ($inp),$inout0
680 movups 0x10($inp),$inout1
682 movups 0x20($inp),$inout2
685 movups 0x30($inp),$inout3
687 movups 0x40($inp),$inout4
690 movups 0x50($inp),$inout5
692 movdqu 0x60($inp),$inout6
693 xorps $inout7,$inout7
695 movups $inout0,($out) # store 7 output blocks
696 movups $inout1,0x10($out)
697 movups $inout2,0x20($out)
698 movups $inout3,0x30($out)
699 movups $inout4,0x40($out)
700 movups $inout5,0x50($out)
701 movups $inout6,0x60($out)
706 &aesni_generate1("enc",$key,$rounds);
708 movups $inout0,($out) # store one output block
713 movups $inout0,($out) # store 2 output blocks
714 movups $inout1,0x10($out)
719 movups $inout0,($out) # store 3 output blocks
720 movups $inout1,0x10($out)
721 movups $inout2,0x20($out)
726 movups $inout0,($out) # store 4 output blocks
727 movups $inout1,0x10($out)
728 movups $inout2,0x20($out)
729 movups $inout3,0x30($out)
733 xorps $inout5,$inout5
735 movups $inout0,($out) # store 5 output blocks
736 movups $inout1,0x10($out)
737 movups $inout2,0x20($out)
738 movups $inout3,0x30($out)
739 movups $inout4,0x40($out)
744 movups $inout0,($out) # store 6 output blocks
745 movups $inout1,0x10($out)
746 movups $inout2,0x20($out)
747 movups $inout3,0x30($out)
748 movups $inout4,0x40($out)
749 movups $inout5,0x50($out)
751 \f#--------------------------- ECB DECRYPT ------------------------------#
754 cmp \$0x80,$len # if ($len<8*16)
755 jb .Lecb_dec_tail # short input
757 movdqu ($inp),$inout0 # load 8 input blocks
758 movdqu 0x10($inp),$inout1
759 movdqu 0x20($inp),$inout2
760 movdqu 0x30($inp),$inout3
761 movdqu 0x40($inp),$inout4
762 movdqu 0x50($inp),$inout5
763 movdqu 0x60($inp),$inout6
764 movdqu 0x70($inp),$inout7
765 lea 0x80($inp),$inp # $inp+=8*16
766 sub \$0x80,$len # $len-=8*16 (can be zero)
767 jmp .Lecb_dec_loop8_enter
770 movups $inout0,($out) # store 8 output blocks
771 mov $key_,$key # restore $key
772 movdqu ($inp),$inout0 # load 8 input blocks
773 mov $rnds_,$rounds # restore $rounds
774 movups $inout1,0x10($out)
775 movdqu 0x10($inp),$inout1
776 movups $inout2,0x20($out)
777 movdqu 0x20($inp),$inout2
778 movups $inout3,0x30($out)
779 movdqu 0x30($inp),$inout3
780 movups $inout4,0x40($out)
781 movdqu 0x40($inp),$inout4
782 movups $inout5,0x50($out)
783 movdqu 0x50($inp),$inout5
784 movups $inout6,0x60($out)
785 movdqu 0x60($inp),$inout6
786 movups $inout7,0x70($out)
787 lea 0x80($out),$out # $out+=8*16
788 movdqu 0x70($inp),$inout7
789 lea 0x80($inp),$inp # $inp+=8*16
790 .Lecb_dec_loop8_enter:
794 $movkey ($key_),$rndkey0
796 jnc .Lecb_dec_loop8 # loop if $len-=8*16 didn't borrow
798 movups $inout0,($out) # store 8 output blocks
799 pxor $inout0,$inout0 # clear register bank
800 mov $key_,$key # restore $key
801 movups $inout1,0x10($out)
803 mov $rnds_,$rounds # restore $rounds
804 movups $inout2,0x20($out)
806 movups $inout3,0x30($out)
808 movups $inout4,0x40($out)
810 movups $inout5,0x50($out)
812 movups $inout6,0x60($out)
814 movups $inout7,0x70($out)
816 lea 0x80($out),$out # $out+=8*16
817 add \$0x80,$len # restore real remaining $len
818 jz .Lecb_ret # done if ($len==0)
821 movups ($inp),$inout0
824 movups 0x10($inp),$inout1
826 movups 0x20($inp),$inout2
829 movups 0x30($inp),$inout3
831 movups 0x40($inp),$inout4
834 movups 0x50($inp),$inout5
836 movups 0x60($inp),$inout6
837 $movkey ($key),$rndkey0
838 xorps $inout7,$inout7
840 movups $inout0,($out) # store 7 output blocks
841 pxor $inout0,$inout0 # clear register bank
842 movups $inout1,0x10($out)
844 movups $inout2,0x20($out)
846 movups $inout3,0x30($out)
848 movups $inout4,0x40($out)
850 movups $inout5,0x50($out)
852 movups $inout6,0x60($out)
859 &aesni_generate1("dec",$key,$rounds);
861 movups $inout0,($out) # store one output block
862 pxor $inout0,$inout0 # clear register bank
867 movups $inout0,($out) # store 2 output blocks
868 pxor $inout0,$inout0 # clear register bank
869 movups $inout1,0x10($out)
875 movups $inout0,($out) # store 3 output blocks
876 pxor $inout0,$inout0 # clear register bank
877 movups $inout1,0x10($out)
879 movups $inout2,0x20($out)
885 movups $inout0,($out) # store 4 output blocks
886 pxor $inout0,$inout0 # clear register bank
887 movups $inout1,0x10($out)
889 movups $inout2,0x20($out)
891 movups $inout3,0x30($out)
896 xorps $inout5,$inout5
898 movups $inout0,($out) # store 5 output blocks
899 pxor $inout0,$inout0 # clear register bank
900 movups $inout1,0x10($out)
902 movups $inout2,0x20($out)
904 movups $inout3,0x30($out)
906 movups $inout4,0x40($out)
913 movups $inout0,($out) # store 6 output blocks
914 pxor $inout0,$inout0 # clear register bank
915 movups $inout1,0x10($out)
917 movups $inout2,0x20($out)
919 movups $inout3,0x30($out)
921 movups $inout4,0x40($out)
923 movups $inout5,0x50($out)
927 xorps $rndkey0,$rndkey0 # %xmm0
928 pxor $rndkey1,$rndkey1
930 $code.=<<___ if ($win64);
932 movaps %xmm0,(%rsp) # clear stack
933 movaps 0x10(%rsp),%xmm7
934 movaps %xmm0,0x10(%rsp)
935 movaps 0x20(%rsp),%xmm8
936 movaps %xmm0,0x20(%rsp)
937 movaps 0x30(%rsp),%xmm9
938 movaps %xmm0,0x30(%rsp)
944 .size aesni_ecb_encrypt,.-aesni_ecb_encrypt
948 ######################################################################
949 # void aesni_ccm64_[en|de]crypt_blocks (const void *in, void *out,
950 # size_t blocks, const AES_KEY *key,
951 # const char *ivec,char *cmac);
953 # Handles only complete blocks, operates on 64-bit counter and
954 # does not update *ivec! Nor does it finalize CMAC value
955 # (see engine/eng_aesni.c for details)
958 my $cmac="%r9"; # 6th argument
960 my $increment="%xmm9";
962 my $bswap_mask="%xmm7";
965 .globl aesni_ccm64_encrypt_blocks
966 .type aesni_ccm64_encrypt_blocks,\@function,6
968 aesni_ccm64_encrypt_blocks:
970 $code.=<<___ if ($win64);
972 movaps %xmm6,(%rsp) # $iv
973 movaps %xmm7,0x10(%rsp) # $bswap_mask
974 movaps %xmm8,0x20(%rsp) # $in0
975 movaps %xmm9,0x30(%rsp) # $increment
979 mov 240($key),$rounds # key->rounds
981 movdqa .Lincrement64(%rip),$increment
982 movdqa .Lbswap_mask(%rip),$bswap_mask
987 movdqu ($cmac),$inout1
989 lea 32($key,$rounds),$key # end of key schedule
990 pshufb $bswap_mask,$iv
991 sub %rax,%r10 # twisted $rounds
992 jmp .Lccm64_enc_outer
995 $movkey ($key_),$rndkey0
997 movups ($inp),$in0 # load inp
999 xorps $rndkey0,$inout0 # counter
1000 $movkey 16($key_),$rndkey1
1002 xorps $rndkey0,$inout1 # cmac^=inp
1003 $movkey 32($key_),$rndkey0
1006 aesenc $rndkey1,$inout0
1007 aesenc $rndkey1,$inout1
1008 $movkey ($key,%rax),$rndkey1
1010 aesenc $rndkey0,$inout0
1011 aesenc $rndkey0,$inout1
1012 $movkey -16($key,%rax),$rndkey0
1013 jnz .Lccm64_enc2_loop
1014 aesenc $rndkey1,$inout0
1015 aesenc $rndkey1,$inout1
1016 paddq $increment,$iv
1017 dec $len # $len-- ($len is in blocks)
1018 aesenclast $rndkey0,$inout0
1019 aesenclast $rndkey0,$inout1
1022 xorps $inout0,$in0 # inp ^= E(iv)
1024 movups $in0,($out) # save output
1025 pshufb $bswap_mask,$inout0
1026 lea 16($out),$out # $out+=16
1027 jnz .Lccm64_enc_outer # loop if ($len!=0)
1029 pxor $rndkey0,$rndkey0 # clear register bank
1030 pxor $rndkey1,$rndkey1
1031 pxor $inout0,$inout0
1032 movups $inout1,($cmac) # store resulting mac
1033 pxor $inout1,$inout1
1037 $code.=<<___ if ($win64);
1039 movaps %xmm0,(%rsp) # clear stack
1040 movaps 0x10(%rsp),%xmm7
1041 movaps %xmm0,0x10(%rsp)
1042 movaps 0x20(%rsp),%xmm8
1043 movaps %xmm0,0x20(%rsp)
1044 movaps 0x30(%rsp),%xmm9
1045 movaps %xmm0,0x30(%rsp)
1051 .size aesni_ccm64_encrypt_blocks,.-aesni_ccm64_encrypt_blocks
1053 ######################################################################
1055 .globl aesni_ccm64_decrypt_blocks
1056 .type aesni_ccm64_decrypt_blocks,\@function,6
1058 aesni_ccm64_decrypt_blocks:
1060 $code.=<<___ if ($win64);
1061 lea -0x58(%rsp),%rsp
1062 movaps %xmm6,(%rsp) # $iv
1063 movaps %xmm7,0x10(%rsp) # $bswap_mask
1064 movaps %xmm8,0x20(%rsp) # $in8
1065 movaps %xmm9,0x30(%rsp) # $increment
1069 mov 240($key),$rounds # key->rounds
1071 movdqu ($cmac),$inout1
1072 movdqa .Lincrement64(%rip),$increment
1073 movdqa .Lbswap_mask(%rip),$bswap_mask
1078 pshufb $bswap_mask,$iv
1080 &aesni_generate1("enc",$key,$rounds);
1084 movups ($inp),$in0 # load inp
1085 paddq $increment,$iv
1086 lea 16($inp),$inp # $inp+=16
1087 sub %r10,%rax # twisted $rounds
1088 lea 32($key_,$rnds_),$key # end of key schedule
1090 jmp .Lccm64_dec_outer
1093 xorps $inout0,$in0 # inp ^= E(iv)
1095 movups $in0,($out) # save output
1096 lea 16($out),$out # $out+=16
1097 pshufb $bswap_mask,$inout0
1099 sub \$1,$len # $len-- ($len is in blocks)
1100 jz .Lccm64_dec_break # if ($len==0) break
1102 $movkey ($key_),$rndkey0
1104 $movkey 16($key_),$rndkey1
1106 xorps $rndkey0,$inout0
1107 xorps $in0,$inout1 # cmac^=out
1108 $movkey 32($key_),$rndkey0
1109 jmp .Lccm64_dec2_loop
1112 aesenc $rndkey1,$inout0
1113 aesenc $rndkey1,$inout1
1114 $movkey ($key,%rax),$rndkey1
1116 aesenc $rndkey0,$inout0
1117 aesenc $rndkey0,$inout1
1118 $movkey -16($key,%rax),$rndkey0
1119 jnz .Lccm64_dec2_loop
1120 movups ($inp),$in0 # load input
1121 paddq $increment,$iv
1122 aesenc $rndkey1,$inout0
1123 aesenc $rndkey1,$inout1
1124 aesenclast $rndkey0,$inout0
1125 aesenclast $rndkey0,$inout1
1126 lea 16($inp),$inp # $inp+=16
1127 jmp .Lccm64_dec_outer
1131 #xorps $in0,$inout1 # cmac^=out
1132 mov 240($key_),$rounds
1134 &aesni_generate1("enc",$key_,$rounds,$inout1,$in0);
1136 pxor $rndkey0,$rndkey0 # clear register bank
1137 pxor $rndkey1,$rndkey1
1138 pxor $inout0,$inout0
1139 movups $inout1,($cmac) # store resulting mac
1140 pxor $inout1,$inout1
1144 $code.=<<___ if ($win64);
1146 movaps %xmm0,(%rsp) # clear stack
1147 movaps 0x10(%rsp),%xmm7
1148 movaps %xmm0,0x10(%rsp)
1149 movaps 0x20(%rsp),%xmm8
1150 movaps %xmm0,0x20(%rsp)
1151 movaps 0x30(%rsp),%xmm9
1152 movaps %xmm0,0x30(%rsp)
1158 .size aesni_ccm64_decrypt_blocks,.-aesni_ccm64_decrypt_blocks
1161 ######################################################################
1162 # void aesni_ctr32_encrypt_blocks (const void *in, void *out,
1163 # size_t blocks, const AES_KEY *key,
1164 # const char *ivec);
1166 # Handles only complete blocks, operates on 32-bit counter and
1167 # does not update *ivec! (see crypto/modes/ctr128.c for details)
1169 # Overhaul based on suggestions from Shay Gueron and Vlad Krasnov,
1170 # http://rt.openssl.org/Ticket/Display.html?id=3021&user=guest&pass=guest.
1171 # Keywords are full unroll and modulo-schedule counter calculations
1172 # with zero-round key xor.
1174 my ($in0,$in1,$in2,$in3,$in4,$in5)=map("%xmm$_",(10..15));
1175 my ($key0,$ctr)=("${key_}d","${ivp}d");
1176 my $frame_size = 0x80 + ($win64?160:0);
1179 .globl aesni_ctr32_encrypt_blocks
1180 .type aesni_ctr32_encrypt_blocks,\@function,5
1182 aesni_ctr32_encrypt_blocks:
1186 # handle single block without allocating stack frame,
1187 # useful when handling edges
1188 movups ($ivp),$inout0
1189 movups ($inp),$inout1
1190 mov 240($key),%edx # key->rounds
1192 &aesni_generate1("enc",$key,"%edx");
1194 pxor $rndkey0,$rndkey0 # clear register bank
1195 pxor $rndkey1,$rndkey1
1196 xorps $inout1,$inout0
1197 pxor $inout1,$inout1
1198 movups $inout0,($out)
1199 xorps $inout0,$inout0
1200 jmp .Lctr32_epilogue
1206 sub \$$frame_size,%rsp
1207 and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
1209 $code.=<<___ if ($win64);
1210 movaps %xmm6,-0xa8(%rax) # offload everything
1211 movaps %xmm7,-0x98(%rax)
1212 movaps %xmm8,-0x88(%rax)
1213 movaps %xmm9,-0x78(%rax)
1214 movaps %xmm10,-0x68(%rax)
1215 movaps %xmm11,-0x58(%rax)
1216 movaps %xmm12,-0x48(%rax)
1217 movaps %xmm13,-0x38(%rax)
1218 movaps %xmm14,-0x28(%rax)
1219 movaps %xmm15,-0x18(%rax)
1225 # 8 16-byte words on top of stack are counter values
1226 # xor-ed with zero-round key
1228 movdqu ($ivp),$inout0
1229 movdqu ($key),$rndkey0
1230 mov 12($ivp),$ctr # counter LSB
1231 pxor $rndkey0,$inout0
1232 mov 12($key),$key0 # 0-round key LSB
1233 movdqa $inout0,0x00(%rsp) # populate counter block
1235 movdqa $inout0,$inout1
1236 movdqa $inout0,$inout2
1237 movdqa $inout0,$inout3
1238 movdqa $inout0,0x40(%rsp)
1239 movdqa $inout0,0x50(%rsp)
1240 movdqa $inout0,0x60(%rsp)
1241 mov %rdx,%r10 # about to borrow %rdx
1242 movdqa $inout0,0x70(%rsp)
1250 pinsrd \$3,%eax,$inout1
1252 movdqa $inout1,0x10(%rsp)
1253 pinsrd \$3,%edx,$inout2
1255 mov %r10,%rdx # restore %rdx
1257 movdqa $inout2,0x20(%rsp)
1260 pinsrd \$3,%eax,$inout3
1262 movdqa $inout3,0x30(%rsp)
1264 mov %r10d,0x40+12(%rsp)
1267 mov 240($key),$rounds # key->rounds
1270 mov %r9d,0x50+12(%rsp)
1273 mov %r10d,0x60+12(%rsp)
1275 mov OPENSSL_ia32cap_P+4(%rip),%r10d
1277 and \$`1<<26|1<<22`,%r10d # isolate XSAVE+MOVBE
1278 mov %r9d,0x70+12(%rsp)
1280 $movkey 0x10($key),$rndkey1
1282 movdqa 0x40(%rsp),$inout4
1283 movdqa 0x50(%rsp),$inout5
1285 cmp \$8,$len # $len is in blocks
1286 jb .Lctr32_tail # short input if ($len<8)
1288 sub \$6,$len # $len is biased by -6
1289 cmp \$`1<<22`,%r10d # check for MOVBE without XSAVE
1290 je .Lctr32_6x # [which denotes Atom Silvermont]
1292 lea 0x80($key),$key # size optimization
1293 sub \$2,$len # $len is biased by -8
1301 lea 32($key,$rounds),$key # end of key schedule
1302 sub %rax,%r10 # twisted $rounds
1307 add \$6,$ctr # next counter value
1308 $movkey -48($key,$rnds_),$rndkey0
1309 aesenc $rndkey1,$inout0
1312 aesenc $rndkey1,$inout1
1313 movbe %eax,`0x00+12`(%rsp) # store next counter value
1315 aesenc $rndkey1,$inout2
1317 movbe %eax,`0x10+12`(%rsp)
1318 aesenc $rndkey1,$inout3
1321 aesenc $rndkey1,$inout4
1322 movbe %eax,`0x20+12`(%rsp)
1324 aesenc $rndkey1,$inout5
1325 $movkey -32($key,$rnds_),$rndkey1
1328 aesenc $rndkey0,$inout0
1329 movbe %eax,`0x30+12`(%rsp)
1331 aesenc $rndkey0,$inout1
1333 movbe %eax,`0x40+12`(%rsp)
1334 aesenc $rndkey0,$inout2
1337 aesenc $rndkey0,$inout3
1338 movbe %eax,`0x50+12`(%rsp)
1339 mov %r10,%rax # mov $rnds_,$rounds
1340 aesenc $rndkey0,$inout4
1341 aesenc $rndkey0,$inout5
1342 $movkey -16($key,$rnds_),$rndkey0
1346 movdqu ($inp),$inout6 # load 6 input blocks
1347 movdqu 0x10($inp),$inout7
1348 movdqu 0x20($inp),$in0
1349 movdqu 0x30($inp),$in1
1350 movdqu 0x40($inp),$in2
1351 movdqu 0x50($inp),$in3
1352 lea 0x60($inp),$inp # $inp+=6*16
1353 $movkey -64($key,$rnds_),$rndkey1
1354 pxor $inout0,$inout6 # inp^=E(ctr)
1355 movaps 0x00(%rsp),$inout0 # load next counter [xor-ed with 0 round]
1356 pxor $inout1,$inout7
1357 movaps 0x10(%rsp),$inout1
1359 movaps 0x20(%rsp),$inout2
1361 movaps 0x30(%rsp),$inout3
1363 movaps 0x40(%rsp),$inout4
1365 movaps 0x50(%rsp),$inout5
1366 movdqu $inout6,($out) # store 6 output blocks
1367 movdqu $inout7,0x10($out)
1368 movdqu $in0,0x20($out)
1369 movdqu $in1,0x30($out)
1370 movdqu $in2,0x40($out)
1371 movdqu $in3,0x50($out)
1372 lea 0x60($out),$out # $out+=6*16
1375 jnc .Lctr32_loop6 # loop if $len-=6 didn't borrow
1377 add \$6,$len # restore real remaining $len
1378 jz .Lctr32_done # done if ($len==0)
1380 lea -48($rnds_),$rounds
1381 lea -80($key,$rnds_),$key # restore $key
1383 shr \$4,$rounds # restore $rounds
1388 add \$8,$ctr # next counter value
1389 movdqa 0x60(%rsp),$inout6
1390 aesenc $rndkey1,$inout0
1392 movdqa 0x70(%rsp),$inout7
1393 aesenc $rndkey1,$inout1
1395 $movkey 0x20-0x80($key),$rndkey0
1396 aesenc $rndkey1,$inout2
1399 aesenc $rndkey1,$inout3
1400 mov %r9d,0x00+12(%rsp) # store next counter value
1402 aesenc $rndkey1,$inout4
1403 aesenc $rndkey1,$inout5
1404 aesenc $rndkey1,$inout6
1405 aesenc $rndkey1,$inout7
1406 $movkey 0x30-0x80($key),$rndkey1
1408 for($i=2;$i<8;$i++) {
1409 my $rndkeyx = ($i&1)?$rndkey1:$rndkey0;
1412 aesenc $rndkeyx,$inout0
1413 aesenc $rndkeyx,$inout1
1416 aesenc $rndkeyx,$inout2
1417 aesenc $rndkeyx,$inout3
1418 mov %r9d,`0x10*($i-1)`+12(%rsp)
1420 aesenc $rndkeyx,$inout4
1421 aesenc $rndkeyx,$inout5
1422 aesenc $rndkeyx,$inout6
1423 aesenc $rndkeyx,$inout7
1424 $movkey `0x20+0x10*$i`-0x80($key),$rndkeyx
1429 aesenc $rndkey0,$inout0
1430 aesenc $rndkey0,$inout1
1431 aesenc $rndkey0,$inout2
1433 movdqu 0x00($inp),$in0 # start loading input
1434 aesenc $rndkey0,$inout3
1435 mov %r9d,0x70+12(%rsp)
1437 aesenc $rndkey0,$inout4
1438 aesenc $rndkey0,$inout5
1439 aesenc $rndkey0,$inout6
1440 aesenc $rndkey0,$inout7
1441 $movkey 0xa0-0x80($key),$rndkey0
1445 aesenc $rndkey1,$inout0
1446 aesenc $rndkey1,$inout1
1447 aesenc $rndkey1,$inout2
1448 aesenc $rndkey1,$inout3
1449 aesenc $rndkey1,$inout4
1450 aesenc $rndkey1,$inout5
1451 aesenc $rndkey1,$inout6
1452 aesenc $rndkey1,$inout7
1453 $movkey 0xb0-0x80($key),$rndkey1
1455 aesenc $rndkey0,$inout0
1456 aesenc $rndkey0,$inout1
1457 aesenc $rndkey0,$inout2
1458 aesenc $rndkey0,$inout3
1459 aesenc $rndkey0,$inout4
1460 aesenc $rndkey0,$inout5
1461 aesenc $rndkey0,$inout6
1462 aesenc $rndkey0,$inout7
1463 $movkey 0xc0-0x80($key),$rndkey0
1466 aesenc $rndkey1,$inout0
1467 aesenc $rndkey1,$inout1
1468 aesenc $rndkey1,$inout2
1469 aesenc $rndkey1,$inout3
1470 aesenc $rndkey1,$inout4
1471 aesenc $rndkey1,$inout5
1472 aesenc $rndkey1,$inout6
1473 aesenc $rndkey1,$inout7
1474 $movkey 0xd0-0x80($key),$rndkey1
1476 aesenc $rndkey0,$inout0
1477 aesenc $rndkey0,$inout1
1478 aesenc $rndkey0,$inout2
1479 aesenc $rndkey0,$inout3
1480 aesenc $rndkey0,$inout4
1481 aesenc $rndkey0,$inout5
1482 aesenc $rndkey0,$inout6
1483 aesenc $rndkey0,$inout7
1484 $movkey 0xe0-0x80($key),$rndkey0
1485 jmp .Lctr32_enc_done
1489 movdqu 0x10($inp),$in1
1490 pxor $rndkey0,$in0 # input^=round[last]
1491 movdqu 0x20($inp),$in2
1493 movdqu 0x30($inp),$in3
1495 movdqu 0x40($inp),$in4
1497 movdqu 0x50($inp),$in5
1500 aesenc $rndkey1,$inout0
1501 aesenc $rndkey1,$inout1
1502 aesenc $rndkey1,$inout2
1503 aesenc $rndkey1,$inout3
1504 aesenc $rndkey1,$inout4
1505 aesenc $rndkey1,$inout5
1506 aesenc $rndkey1,$inout6
1507 aesenc $rndkey1,$inout7
1508 movdqu 0x60($inp),$rndkey1 # borrow $rndkey1 for inp[6]
1509 lea 0x80($inp),$inp # $inp+=8*16
1511 aesenclast $in0,$inout0 # $inN is inp[N]^round[last]
1512 pxor $rndkey0,$rndkey1 # borrowed $rndkey
1513 movdqu 0x70-0x80($inp),$in0
1514 aesenclast $in1,$inout1
1516 movdqa 0x00(%rsp),$in1 # load next counter block
1517 aesenclast $in2,$inout2
1518 aesenclast $in3,$inout3
1519 movdqa 0x10(%rsp),$in2
1520 movdqa 0x20(%rsp),$in3
1521 aesenclast $in4,$inout4
1522 aesenclast $in5,$inout5
1523 movdqa 0x30(%rsp),$in4
1524 movdqa 0x40(%rsp),$in5
1525 aesenclast $rndkey1,$inout6
1526 movdqa 0x50(%rsp),$rndkey0
1527 $movkey 0x10-0x80($key),$rndkey1#real 1st-round key
1528 aesenclast $in0,$inout7
1530 movups $inout0,($out) # store 8 output blocks
1532 movups $inout1,0x10($out)
1534 movups $inout2,0x20($out)
1536 movups $inout3,0x30($out)
1538 movups $inout4,0x40($out)
1540 movups $inout5,0x50($out)
1541 movdqa $rndkey0,$inout5
1542 movups $inout6,0x60($out)
1543 movups $inout7,0x70($out)
1544 lea 0x80($out),$out # $out+=8*16
1547 jnc .Lctr32_loop8 # loop if $len-=8 didn't borrow
1549 add \$8,$len # restore real remainig $len
1550 jz .Lctr32_done # done if ($len==0)
1551 lea -0x80($key),$key
1554 # note that at this point $inout0..5 are populated with
1555 # counter values xor-ed with 0-round key
1561 # if ($len>4) compute 7 E(counter)
1563 movdqa 0x60(%rsp),$inout6
1564 pxor $inout7,$inout7
1566 $movkey 16($key),$rndkey0
1567 aesenc $rndkey1,$inout0
1568 aesenc $rndkey1,$inout1
1569 lea 32-16($key,$rounds),$key# prepare for .Lenc_loop8_enter
1571 aesenc $rndkey1,$inout2
1572 add \$16,%rax # prepare for .Lenc_loop8_enter
1574 aesenc $rndkey1,$inout3
1575 aesenc $rndkey1,$inout4
1576 movups 0x10($inp),$in1 # pre-load input
1577 movups 0x20($inp),$in2
1578 aesenc $rndkey1,$inout5
1579 aesenc $rndkey1,$inout6
1581 call .Lenc_loop8_enter
1583 movdqu 0x30($inp),$in3
1585 movdqu 0x40($inp),$in0
1587 movdqu $inout0,($out) # store output
1589 movdqu $inout1,0x10($out)
1591 movdqu $inout2,0x20($out)
1593 movdqu $inout3,0x30($out)
1594 movdqu $inout4,0x40($out)
1596 jb .Lctr32_done # $len was 5, stop store
1598 movups 0x50($inp),$in1
1600 movups $inout5,0x50($out)
1601 je .Lctr32_done # $len was 6, stop store
1603 movups 0x60($inp),$in2
1605 movups $inout6,0x60($out)
1606 jmp .Lctr32_done # $len was 7, stop store
1610 aesenc $rndkey1,$inout0
1613 aesenc $rndkey1,$inout1
1614 aesenc $rndkey1,$inout2
1615 aesenc $rndkey1,$inout3
1616 $movkey ($key),$rndkey1
1618 aesenclast $rndkey1,$inout0
1619 aesenclast $rndkey1,$inout1
1620 movups ($inp),$in0 # load input
1621 movups 0x10($inp),$in1
1622 aesenclast $rndkey1,$inout2
1623 aesenclast $rndkey1,$inout3
1624 movups 0x20($inp),$in2
1625 movups 0x30($inp),$in3
1628 movups $inout0,($out) # store output
1630 movups $inout1,0x10($out)
1632 movdqu $inout2,0x20($out)
1634 movdqu $inout3,0x30($out)
1635 jmp .Lctr32_done # $len was 4, stop store
1639 aesenc $rndkey1,$inout0
1642 aesenc $rndkey1,$inout1
1643 aesenc $rndkey1,$inout2
1644 $movkey ($key),$rndkey1
1646 aesenclast $rndkey1,$inout0
1647 aesenclast $rndkey1,$inout1
1648 aesenclast $rndkey1,$inout2
1650 movups ($inp),$in0 # load input
1652 movups $inout0,($out) # store output
1654 jb .Lctr32_done # $len was 1, stop store
1656 movups 0x10($inp),$in1
1658 movups $inout1,0x10($out)
1659 je .Lctr32_done # $len was 2, stop store
1661 movups 0x20($inp),$in2
1663 movups $inout2,0x20($out) # $len was 3, stop store
1666 xorps %xmm0,%xmm0 # clear regiser bank
1674 $code.=<<___ if (!$win64);
1677 movaps %xmm0,0x00(%rsp) # clear stack
1679 movaps %xmm0,0x10(%rsp)
1681 movaps %xmm0,0x20(%rsp)
1683 movaps %xmm0,0x30(%rsp)
1685 movaps %xmm0,0x40(%rsp)
1687 movaps %xmm0,0x50(%rsp)
1689 movaps %xmm0,0x60(%rsp)
1691 movaps %xmm0,0x70(%rsp)
1694 $code.=<<___ if ($win64);
1695 movaps -0xa0(%rbp),%xmm6
1696 movaps %xmm0,-0xa0(%rbp) # clear stack
1697 movaps -0x90(%rbp),%xmm7
1698 movaps %xmm0,-0x90(%rbp)
1699 movaps -0x80(%rbp),%xmm8
1700 movaps %xmm0,-0x80(%rbp)
1701 movaps -0x70(%rbp),%xmm9
1702 movaps %xmm0,-0x70(%rbp)
1703 movaps -0x60(%rbp),%xmm10
1704 movaps %xmm0,-0x60(%rbp)
1705 movaps -0x50(%rbp),%xmm11
1706 movaps %xmm0,-0x50(%rbp)
1707 movaps -0x40(%rbp),%xmm12
1708 movaps %xmm0,-0x40(%rbp)
1709 movaps -0x30(%rbp),%xmm13
1710 movaps %xmm0,-0x30(%rbp)
1711 movaps -0x20(%rbp),%xmm14
1712 movaps %xmm0,-0x20(%rbp)
1713 movaps -0x10(%rbp),%xmm15
1714 movaps %xmm0,-0x10(%rbp)
1715 movaps %xmm0,0x00(%rsp)
1716 movaps %xmm0,0x10(%rsp)
1717 movaps %xmm0,0x20(%rsp)
1718 movaps %xmm0,0x30(%rsp)
1719 movaps %xmm0,0x40(%rsp)
1720 movaps %xmm0,0x50(%rsp)
1721 movaps %xmm0,0x60(%rsp)
1722 movaps %xmm0,0x70(%rsp)
1729 .size aesni_ctr32_encrypt_blocks,.-aesni_ctr32_encrypt_blocks
1733 ######################################################################
1734 # void aesni_xts_[en|de]crypt(const char *inp,char *out,size_t len,
1735 # const AES_KEY *key1, const AES_KEY *key2
1736 # const unsigned char iv[16]);
1739 my @tweak=map("%xmm$_",(10..15));
1740 my ($twmask,$twres,$twtmp)=("%xmm8","%xmm9",@tweak[4]);
1741 my ($key2,$ivp,$len_)=("%r8","%r9","%r9");
1742 my $frame_size = 0x70 + ($win64?160:0);
1745 .globl aesni_xts_encrypt
1746 .type aesni_xts_encrypt,\@function,6
1751 sub \$$frame_size,%rsp
1752 and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
1754 $code.=<<___ if ($win64);
1755 movaps %xmm6,-0xa8(%rax) # offload everything
1756 movaps %xmm7,-0x98(%rax)
1757 movaps %xmm8,-0x88(%rax)
1758 movaps %xmm9,-0x78(%rax)
1759 movaps %xmm10,-0x68(%rax)
1760 movaps %xmm11,-0x58(%rax)
1761 movaps %xmm12,-0x48(%rax)
1762 movaps %xmm13,-0x38(%rax)
1763 movaps %xmm14,-0x28(%rax)
1764 movaps %xmm15,-0x18(%rax)
1769 movups ($ivp),$inout0 # load clear-text tweak
1770 mov 240(%r8),$rounds # key2->rounds
1771 mov 240($key),$rnds_ # key1->rounds
1773 # generate the tweak
1774 &aesni_generate1("enc",$key2,$rounds,$inout0);
1776 $movkey ($key),$rndkey0 # zero round key
1777 mov $key,$key_ # backup $key
1778 mov $rnds_,$rounds # backup $rounds
1780 mov $len,$len_ # backup $len
1783 $movkey 16($key,$rnds_),$rndkey1 # last round key
1785 movdqa .Lxts_magic(%rip),$twmask
1786 movdqa $inout0,@tweak[5]
1787 pshufd \$0x5f,$inout0,$twres
1788 pxor $rndkey0,$rndkey1
1790 # alternative tweak calculation algorithm is based on suggestions
1791 # by Shay Gueron. psrad doesn't conflict with AES-NI instructions
1792 # and should help in the future...
1793 for ($i=0;$i<4;$i++) {
1795 movdqa $twres,$twtmp
1797 movdqa @tweak[5],@tweak[$i]
1798 psrad \$31,$twtmp # broadcast upper bits
1799 paddq @tweak[5],@tweak[5]
1801 pxor $rndkey0,@tweak[$i]
1802 pxor $twtmp,@tweak[5]
1806 movdqa @tweak[5],@tweak[4]
1808 paddq @tweak[5],@tweak[5]
1810 pxor $rndkey0,@tweak[4]
1811 pxor $twres,@tweak[5]
1812 movaps $rndkey1,0x60(%rsp) # save round[0]^round[last]
1815 jc .Lxts_enc_short # if $len-=6*16 borrowed
1818 lea 32($key_,$rnds_),$key # end of key schedule
1819 sub %r10,%rax # twisted $rounds
1820 $movkey 16($key_),$rndkey1
1821 mov %rax,%r10 # backup twisted $rounds
1822 lea .Lxts_magic(%rip),%r8
1823 jmp .Lxts_enc_grandloop
1826 .Lxts_enc_grandloop:
1827 movdqu `16*0`($inp),$inout0 # load input
1828 movdqa $rndkey0,$twmask
1829 movdqu `16*1`($inp),$inout1
1830 pxor @tweak[0],$inout0 # input^=tweak^round[0]
1831 movdqu `16*2`($inp),$inout2
1832 pxor @tweak[1],$inout1
1833 aesenc $rndkey1,$inout0
1834 movdqu `16*3`($inp),$inout3
1835 pxor @tweak[2],$inout2
1836 aesenc $rndkey1,$inout1
1837 movdqu `16*4`($inp),$inout4
1838 pxor @tweak[3],$inout3
1839 aesenc $rndkey1,$inout2
1840 movdqu `16*5`($inp),$inout5
1841 pxor @tweak[5],$twmask # round[0]^=tweak[5]
1842 movdqa 0x60(%rsp),$twres # load round[0]^round[last]
1843 pxor @tweak[4],$inout4
1844 aesenc $rndkey1,$inout3
1845 $movkey 32($key_),$rndkey0
1846 lea `16*6`($inp),$inp
1847 pxor $twmask,$inout5
1849 pxor $twres,@tweak[0] # calclulate tweaks^round[last]
1850 aesenc $rndkey1,$inout4
1851 pxor $twres,@tweak[1]
1852 movdqa @tweak[0],`16*0`(%rsp) # put aside tweaks^round[last]
1853 aesenc $rndkey1,$inout5
1854 $movkey 48($key_),$rndkey1
1855 pxor $twres,@tweak[2]
1857 aesenc $rndkey0,$inout0
1858 pxor $twres,@tweak[3]
1859 movdqa @tweak[1],`16*1`(%rsp)
1860 aesenc $rndkey0,$inout1
1861 pxor $twres,@tweak[4]
1862 movdqa @tweak[2],`16*2`(%rsp)
1863 aesenc $rndkey0,$inout2
1864 aesenc $rndkey0,$inout3
1866 movdqa @tweak[4],`16*4`(%rsp)
1867 aesenc $rndkey0,$inout4
1868 aesenc $rndkey0,$inout5
1869 $movkey 64($key_),$rndkey0
1870 movdqa $twmask,`16*5`(%rsp)
1871 pshufd \$0x5f,@tweak[5],$twres
1875 aesenc $rndkey1,$inout0
1876 aesenc $rndkey1,$inout1
1877 aesenc $rndkey1,$inout2
1878 aesenc $rndkey1,$inout3
1879 aesenc $rndkey1,$inout4
1880 aesenc $rndkey1,$inout5
1881 $movkey -64($key,%rax),$rndkey1
1884 aesenc $rndkey0,$inout0
1885 aesenc $rndkey0,$inout1
1886 aesenc $rndkey0,$inout2
1887 aesenc $rndkey0,$inout3
1888 aesenc $rndkey0,$inout4
1889 aesenc $rndkey0,$inout5
1890 $movkey -80($key,%rax),$rndkey0
1893 movdqa (%r8),$twmask # start calculating next tweak
1894 movdqa $twres,$twtmp
1896 aesenc $rndkey1,$inout0
1897 paddq @tweak[5],@tweak[5]
1899 aesenc $rndkey1,$inout1
1901 $movkey ($key_),@tweak[0] # load round[0]
1902 aesenc $rndkey1,$inout2
1903 aesenc $rndkey1,$inout3
1904 aesenc $rndkey1,$inout4
1905 pxor $twtmp,@tweak[5]
1906 movaps @tweak[0],@tweak[1] # copy round[0]
1907 aesenc $rndkey1,$inout5
1908 $movkey -64($key),$rndkey1
1910 movdqa $twres,$twtmp
1911 aesenc $rndkey0,$inout0
1913 pxor @tweak[5],@tweak[0]
1914 aesenc $rndkey0,$inout1
1916 paddq @tweak[5],@tweak[5]
1917 aesenc $rndkey0,$inout2
1918 aesenc $rndkey0,$inout3
1920 movaps @tweak[1],@tweak[2]
1921 aesenc $rndkey0,$inout4
1922 pxor $twtmp,@tweak[5]
1923 movdqa $twres,$twtmp
1924 aesenc $rndkey0,$inout5
1925 $movkey -48($key),$rndkey0
1928 aesenc $rndkey1,$inout0
1929 pxor @tweak[5],@tweak[1]
1931 aesenc $rndkey1,$inout1
1932 paddq @tweak[5],@tweak[5]
1934 aesenc $rndkey1,$inout2
1935 aesenc $rndkey1,$inout3
1936 movdqa @tweak[3],`16*3`(%rsp)
1937 pxor $twtmp,@tweak[5]
1938 aesenc $rndkey1,$inout4
1939 movaps @tweak[2],@tweak[3]
1940 movdqa $twres,$twtmp
1941 aesenc $rndkey1,$inout5
1942 $movkey -32($key),$rndkey1
1945 aesenc $rndkey0,$inout0
1946 pxor @tweak[5],@tweak[2]
1948 aesenc $rndkey0,$inout1
1949 paddq @tweak[5],@tweak[5]
1951 aesenc $rndkey0,$inout2
1952 aesenc $rndkey0,$inout3
1953 aesenc $rndkey0,$inout4
1954 pxor $twtmp,@tweak[5]
1955 movaps @tweak[3],@tweak[4]
1956 aesenc $rndkey0,$inout5
1958 movdqa $twres,$rndkey0
1960 aesenc $rndkey1,$inout0
1961 pxor @tweak[5],@tweak[3]
1963 aesenc $rndkey1,$inout1
1964 paddq @tweak[5],@tweak[5]
1965 pand $twmask,$rndkey0
1966 aesenc $rndkey1,$inout2
1967 aesenc $rndkey1,$inout3
1968 pxor $rndkey0,@tweak[5]
1969 $movkey ($key_),$rndkey0
1970 aesenc $rndkey1,$inout4
1971 aesenc $rndkey1,$inout5
1972 $movkey 16($key_),$rndkey1
1974 pxor @tweak[5],@tweak[4]
1975 aesenclast `16*0`(%rsp),$inout0
1977 paddq @tweak[5],@tweak[5]
1978 aesenclast `16*1`(%rsp),$inout1
1979 aesenclast `16*2`(%rsp),$inout2
1981 mov %r10,%rax # restore $rounds
1982 aesenclast `16*3`(%rsp),$inout3
1983 aesenclast `16*4`(%rsp),$inout4
1984 aesenclast `16*5`(%rsp),$inout5
1985 pxor $twres,@tweak[5]
1987 lea `16*6`($out),$out # $out+=6*16
1988 movups $inout0,`-16*6`($out) # store 6 output blocks
1989 movups $inout1,`-16*5`($out)
1990 movups $inout2,`-16*4`($out)
1991 movups $inout3,`-16*3`($out)
1992 movups $inout4,`-16*2`($out)
1993 movups $inout5,`-16*1`($out)
1995 jnc .Lxts_enc_grandloop # loop if $len-=6*16 didn't borrow
1999 mov $key_,$key # restore $key
2000 shr \$4,$rounds # restore original value
2003 # at the point @tweak[0..5] are populated with tweak values
2004 mov $rounds,$rnds_ # backup $rounds
2005 pxor $rndkey0,@tweak[0]
2006 add \$16*6,$len # restore real remaining $len
2007 jz .Lxts_enc_done # done if ($len==0)
2009 pxor $rndkey0,@tweak[1]
2011 jb .Lxts_enc_one # $len is 1*16
2012 pxor $rndkey0,@tweak[2]
2013 je .Lxts_enc_two # $len is 2*16
2015 pxor $rndkey0,@tweak[3]
2017 jb .Lxts_enc_three # $len is 3*16
2018 pxor $rndkey0,@tweak[4]
2019 je .Lxts_enc_four # $len is 4*16
2021 movdqu ($inp),$inout0 # $len is 5*16
2022 movdqu 16*1($inp),$inout1
2023 movdqu 16*2($inp),$inout2
2024 pxor @tweak[0],$inout0
2025 movdqu 16*3($inp),$inout3
2026 pxor @tweak[1],$inout1
2027 movdqu 16*4($inp),$inout4
2028 lea 16*5($inp),$inp # $inp+=5*16
2029 pxor @tweak[2],$inout2
2030 pxor @tweak[3],$inout3
2031 pxor @tweak[4],$inout4
2032 pxor $inout5,$inout5
2034 call _aesni_encrypt6
2036 xorps @tweak[0],$inout0
2037 movdqa @tweak[5],@tweak[0]
2038 xorps @tweak[1],$inout1
2039 xorps @tweak[2],$inout2
2040 movdqu $inout0,($out) # store 5 output blocks
2041 xorps @tweak[3],$inout3
2042 movdqu $inout1,16*1($out)
2043 xorps @tweak[4],$inout4
2044 movdqu $inout2,16*2($out)
2045 movdqu $inout3,16*3($out)
2046 movdqu $inout4,16*4($out)
2047 lea 16*5($out),$out # $out+=5*16
2052 movups ($inp),$inout0
2053 lea 16*1($inp),$inp # inp+=1*16
2054 xorps @tweak[0],$inout0
2056 &aesni_generate1("enc",$key,$rounds);
2058 xorps @tweak[0],$inout0
2059 movdqa @tweak[1],@tweak[0]
2060 movups $inout0,($out) # store one output block
2061 lea 16*1($out),$out # $out+=1*16
2066 movups ($inp),$inout0
2067 movups 16($inp),$inout1
2068 lea 32($inp),$inp # $inp+=2*16
2069 xorps @tweak[0],$inout0
2070 xorps @tweak[1],$inout1
2072 call _aesni_encrypt2
2074 xorps @tweak[0],$inout0
2075 movdqa @tweak[2],@tweak[0]
2076 xorps @tweak[1],$inout1
2077 movups $inout0,($out) # store 2 output blocks
2078 movups $inout1,16*1($out)
2079 lea 16*2($out),$out # $out+=2*16
2084 movups ($inp),$inout0
2085 movups 16*1($inp),$inout1
2086 movups 16*2($inp),$inout2
2087 lea 16*3($inp),$inp # $inp+=3*16
2088 xorps @tweak[0],$inout0
2089 xorps @tweak[1],$inout1
2090 xorps @tweak[2],$inout2
2092 call _aesni_encrypt3
2094 xorps @tweak[0],$inout0
2095 movdqa @tweak[3],@tweak[0]
2096 xorps @tweak[1],$inout1
2097 xorps @tweak[2],$inout2
2098 movups $inout0,($out) # store 3 output blocks
2099 movups $inout1,16*1($out)
2100 movups $inout2,16*2($out)
2101 lea 16*3($out),$out # $out+=3*16
2106 movups ($inp),$inout0
2107 movups 16*1($inp),$inout1
2108 movups 16*2($inp),$inout2
2109 xorps @tweak[0],$inout0
2110 movups 16*3($inp),$inout3
2111 lea 16*4($inp),$inp # $inp+=4*16
2112 xorps @tweak[1],$inout1
2113 xorps @tweak[2],$inout2
2114 xorps @tweak[3],$inout3
2116 call _aesni_encrypt4
2118 pxor @tweak[0],$inout0
2119 movdqa @tweak[4],@tweak[0]
2120 pxor @tweak[1],$inout1
2121 pxor @tweak[2],$inout2
2122 movdqu $inout0,($out) # store 4 output blocks
2123 pxor @tweak[3],$inout3
2124 movdqu $inout1,16*1($out)
2125 movdqu $inout2,16*2($out)
2126 movdqu $inout3,16*3($out)
2127 lea 16*4($out),$out # $out+=4*16
2132 and \$15,$len_ # see if $len%16 is 0
2137 movzb ($inp),%eax # borrow $rounds ...
2138 movzb -16($out),%ecx # ... and $key
2146 sub $len_,$out # rewind $out
2147 mov $key_,$key # restore $key
2148 mov $rnds_,$rounds # restore $rounds
2150 movups -16($out),$inout0
2151 xorps @tweak[0],$inout0
2153 &aesni_generate1("enc",$key,$rounds);
2155 xorps @tweak[0],$inout0
2156 movups $inout0,-16($out)
2159 xorps %xmm0,%xmm0 # clear register bank
2166 $code.=<<___ if (!$win64);
2169 movaps %xmm0,0x00(%rsp) # clear stack
2171 movaps %xmm0,0x10(%rsp)
2173 movaps %xmm0,0x20(%rsp)
2175 movaps %xmm0,0x30(%rsp)
2177 movaps %xmm0,0x40(%rsp)
2179 movaps %xmm0,0x50(%rsp)
2181 movaps %xmm0,0x60(%rsp)
2185 $code.=<<___ if ($win64);
2186 movaps -0xa0(%rbp),%xmm6
2187 movaps %xmm0,-0xa0(%rbp) # clear stack
2188 movaps -0x90(%rbp),%xmm7
2189 movaps %xmm0,-0x90(%rbp)
2190 movaps -0x80(%rbp),%xmm8
2191 movaps %xmm0,-0x80(%rbp)
2192 movaps -0x70(%rbp),%xmm9
2193 movaps %xmm0,-0x70(%rbp)
2194 movaps -0x60(%rbp),%xmm10
2195 movaps %xmm0,-0x60(%rbp)
2196 movaps -0x50(%rbp),%xmm11
2197 movaps %xmm0,-0x50(%rbp)
2198 movaps -0x40(%rbp),%xmm12
2199 movaps %xmm0,-0x40(%rbp)
2200 movaps -0x30(%rbp),%xmm13
2201 movaps %xmm0,-0x30(%rbp)
2202 movaps -0x20(%rbp),%xmm14
2203 movaps %xmm0,-0x20(%rbp)
2204 movaps -0x10(%rbp),%xmm15
2205 movaps %xmm0,-0x10(%rbp)
2206 movaps %xmm0,0x00(%rsp)
2207 movaps %xmm0,0x10(%rsp)
2208 movaps %xmm0,0x20(%rsp)
2209 movaps %xmm0,0x30(%rsp)
2210 movaps %xmm0,0x40(%rsp)
2211 movaps %xmm0,0x50(%rsp)
2212 movaps %xmm0,0x60(%rsp)
2219 .size aesni_xts_encrypt,.-aesni_xts_encrypt
2223 .globl aesni_xts_decrypt
2224 .type aesni_xts_decrypt,\@function,6
2229 sub \$$frame_size,%rsp
2230 and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
2232 $code.=<<___ if ($win64);
2233 movaps %xmm6,-0xa8(%rax) # offload everything
2234 movaps %xmm7,-0x98(%rax)
2235 movaps %xmm8,-0x88(%rax)
2236 movaps %xmm9,-0x78(%rax)
2237 movaps %xmm10,-0x68(%rax)
2238 movaps %xmm11,-0x58(%rax)
2239 movaps %xmm12,-0x48(%rax)
2240 movaps %xmm13,-0x38(%rax)
2241 movaps %xmm14,-0x28(%rax)
2242 movaps %xmm15,-0x18(%rax)
2247 movups ($ivp),$inout0 # load clear-text tweak
2248 mov 240($key2),$rounds # key2->rounds
2249 mov 240($key),$rnds_ # key1->rounds
2251 # generate the tweak
2252 &aesni_generate1("enc",$key2,$rounds,$inout0);
2254 xor %eax,%eax # if ($len%16) len-=16;
2260 $movkey ($key),$rndkey0 # zero round key
2261 mov $key,$key_ # backup $key
2262 mov $rnds_,$rounds # backup $rounds
2264 mov $len,$len_ # backup $len
2267 $movkey 16($key,$rnds_),$rndkey1 # last round key
2269 movdqa .Lxts_magic(%rip),$twmask
2270 movdqa $inout0,@tweak[5]
2271 pshufd \$0x5f,$inout0,$twres
2272 pxor $rndkey0,$rndkey1
2274 for ($i=0;$i<4;$i++) {
2276 movdqa $twres,$twtmp
2278 movdqa @tweak[5],@tweak[$i]
2279 psrad \$31,$twtmp # broadcast upper bits
2280 paddq @tweak[5],@tweak[5]
2282 pxor $rndkey0,@tweak[$i]
2283 pxor $twtmp,@tweak[5]
2287 movdqa @tweak[5],@tweak[4]
2289 paddq @tweak[5],@tweak[5]
2291 pxor $rndkey0,@tweak[4]
2292 pxor $twres,@tweak[5]
2293 movaps $rndkey1,0x60(%rsp) # save round[0]^round[last]
2296 jc .Lxts_dec_short # if $len-=6*16 borrowed
2299 lea 32($key_,$rnds_),$key # end of key schedule
2300 sub %r10,%rax # twisted $rounds
2301 $movkey 16($key_),$rndkey1
2302 mov %rax,%r10 # backup twisted $rounds
2303 lea .Lxts_magic(%rip),%r8
2304 jmp .Lxts_dec_grandloop
2307 .Lxts_dec_grandloop:
2308 movdqu `16*0`($inp),$inout0 # load input
2309 movdqa $rndkey0,$twmask
2310 movdqu `16*1`($inp),$inout1
2311 pxor @tweak[0],$inout0 # intput^=tweak^round[0]
2312 movdqu `16*2`($inp),$inout2
2313 pxor @tweak[1],$inout1
2314 aesdec $rndkey1,$inout0
2315 movdqu `16*3`($inp),$inout3
2316 pxor @tweak[2],$inout2
2317 aesdec $rndkey1,$inout1
2318 movdqu `16*4`($inp),$inout4
2319 pxor @tweak[3],$inout3
2320 aesdec $rndkey1,$inout2
2321 movdqu `16*5`($inp),$inout5
2322 pxor @tweak[5],$twmask # round[0]^=tweak[5]
2323 movdqa 0x60(%rsp),$twres # load round[0]^round[last]
2324 pxor @tweak[4],$inout4
2325 aesdec $rndkey1,$inout3
2326 $movkey 32($key_),$rndkey0
2327 lea `16*6`($inp),$inp
2328 pxor $twmask,$inout5
2330 pxor $twres,@tweak[0] # calclulate tweaks^round[last]
2331 aesdec $rndkey1,$inout4
2332 pxor $twres,@tweak[1]
2333 movdqa @tweak[0],`16*0`(%rsp) # put aside tweaks^last round key
2334 aesdec $rndkey1,$inout5
2335 $movkey 48($key_),$rndkey1
2336 pxor $twres,@tweak[2]
2338 aesdec $rndkey0,$inout0
2339 pxor $twres,@tweak[3]
2340 movdqa @tweak[1],`16*1`(%rsp)
2341 aesdec $rndkey0,$inout1
2342 pxor $twres,@tweak[4]
2343 movdqa @tweak[2],`16*2`(%rsp)
2344 aesdec $rndkey0,$inout2
2345 aesdec $rndkey0,$inout3
2347 movdqa @tweak[4],`16*4`(%rsp)
2348 aesdec $rndkey0,$inout4
2349 aesdec $rndkey0,$inout5
2350 $movkey 64($key_),$rndkey0
2351 movdqa $twmask,`16*5`(%rsp)
2352 pshufd \$0x5f,@tweak[5],$twres
2356 aesdec $rndkey1,$inout0
2357 aesdec $rndkey1,$inout1
2358 aesdec $rndkey1,$inout2
2359 aesdec $rndkey1,$inout3
2360 aesdec $rndkey1,$inout4
2361 aesdec $rndkey1,$inout5
2362 $movkey -64($key,%rax),$rndkey1
2365 aesdec $rndkey0,$inout0
2366 aesdec $rndkey0,$inout1
2367 aesdec $rndkey0,$inout2
2368 aesdec $rndkey0,$inout3
2369 aesdec $rndkey0,$inout4
2370 aesdec $rndkey0,$inout5
2371 $movkey -80($key,%rax),$rndkey0
2374 movdqa (%r8),$twmask # start calculating next tweak
2375 movdqa $twres,$twtmp
2377 aesdec $rndkey1,$inout0
2378 paddq @tweak[5],@tweak[5]
2380 aesdec $rndkey1,$inout1
2382 $movkey ($key_),@tweak[0] # load round[0]
2383 aesdec $rndkey1,$inout2
2384 aesdec $rndkey1,$inout3
2385 aesdec $rndkey1,$inout4
2386 pxor $twtmp,@tweak[5]
2387 movaps @tweak[0],@tweak[1] # copy round[0]
2388 aesdec $rndkey1,$inout5
2389 $movkey -64($key),$rndkey1
2391 movdqa $twres,$twtmp
2392 aesdec $rndkey0,$inout0
2394 pxor @tweak[5],@tweak[0]
2395 aesdec $rndkey0,$inout1
2397 paddq @tweak[5],@tweak[5]
2398 aesdec $rndkey0,$inout2
2399 aesdec $rndkey0,$inout3
2401 movaps @tweak[1],@tweak[2]
2402 aesdec $rndkey0,$inout4
2403 pxor $twtmp,@tweak[5]
2404 movdqa $twres,$twtmp
2405 aesdec $rndkey0,$inout5
2406 $movkey -48($key),$rndkey0
2409 aesdec $rndkey1,$inout0
2410 pxor @tweak[5],@tweak[1]
2412 aesdec $rndkey1,$inout1
2413 paddq @tweak[5],@tweak[5]
2415 aesdec $rndkey1,$inout2
2416 aesdec $rndkey1,$inout3
2417 movdqa @tweak[3],`16*3`(%rsp)
2418 pxor $twtmp,@tweak[5]
2419 aesdec $rndkey1,$inout4
2420 movaps @tweak[2],@tweak[3]
2421 movdqa $twres,$twtmp
2422 aesdec $rndkey1,$inout5
2423 $movkey -32($key),$rndkey1
2426 aesdec $rndkey0,$inout0
2427 pxor @tweak[5],@tweak[2]
2429 aesdec $rndkey0,$inout1
2430 paddq @tweak[5],@tweak[5]
2432 aesdec $rndkey0,$inout2
2433 aesdec $rndkey0,$inout3
2434 aesdec $rndkey0,$inout4
2435 pxor $twtmp,@tweak[5]
2436 movaps @tweak[3],@tweak[4]
2437 aesdec $rndkey0,$inout5
2439 movdqa $twres,$rndkey0
2441 aesdec $rndkey1,$inout0
2442 pxor @tweak[5],@tweak[3]
2444 aesdec $rndkey1,$inout1
2445 paddq @tweak[5],@tweak[5]
2446 pand $twmask,$rndkey0
2447 aesdec $rndkey1,$inout2
2448 aesdec $rndkey1,$inout3
2449 pxor $rndkey0,@tweak[5]
2450 $movkey ($key_),$rndkey0
2451 aesdec $rndkey1,$inout4
2452 aesdec $rndkey1,$inout5
2453 $movkey 16($key_),$rndkey1
2455 pxor @tweak[5],@tweak[4]
2456 aesdeclast `16*0`(%rsp),$inout0
2458 paddq @tweak[5],@tweak[5]
2459 aesdeclast `16*1`(%rsp),$inout1
2460 aesdeclast `16*2`(%rsp),$inout2
2462 mov %r10,%rax # restore $rounds
2463 aesdeclast `16*3`(%rsp),$inout3
2464 aesdeclast `16*4`(%rsp),$inout4
2465 aesdeclast `16*5`(%rsp),$inout5
2466 pxor $twres,@tweak[5]
2468 lea `16*6`($out),$out # $out+=6*16
2469 movups $inout0,`-16*6`($out) # store 6 output blocks
2470 movups $inout1,`-16*5`($out)
2471 movups $inout2,`-16*4`($out)
2472 movups $inout3,`-16*3`($out)
2473 movups $inout4,`-16*2`($out)
2474 movups $inout5,`-16*1`($out)
2476 jnc .Lxts_dec_grandloop # loop if $len-=6*16 didn't borrow
2480 mov $key_,$key # restore $key
2481 shr \$4,$rounds # restore original value
2484 # at the point @tweak[0..5] are populated with tweak values
2485 mov $rounds,$rnds_ # backup $rounds
2486 pxor $rndkey0,@tweak[0]
2487 pxor $rndkey0,@tweak[1]
2488 add \$16*6,$len # restore real remaining $len
2489 jz .Lxts_dec_done # done if ($len==0)
2491 pxor $rndkey0,@tweak[2]
2493 jb .Lxts_dec_one # $len is 1*16
2494 pxor $rndkey0,@tweak[3]
2495 je .Lxts_dec_two # $len is 2*16
2497 pxor $rndkey0,@tweak[4]
2499 jb .Lxts_dec_three # $len is 3*16
2500 je .Lxts_dec_four # $len is 4*16
2502 movdqu ($inp),$inout0 # $len is 5*16
2503 movdqu 16*1($inp),$inout1
2504 movdqu 16*2($inp),$inout2
2505 pxor @tweak[0],$inout0
2506 movdqu 16*3($inp),$inout3
2507 pxor @tweak[1],$inout1
2508 movdqu 16*4($inp),$inout4
2509 lea 16*5($inp),$inp # $inp+=5*16
2510 pxor @tweak[2],$inout2
2511 pxor @tweak[3],$inout3
2512 pxor @tweak[4],$inout4
2514 call _aesni_decrypt6
2516 xorps @tweak[0],$inout0
2517 xorps @tweak[1],$inout1
2518 xorps @tweak[2],$inout2
2519 movdqu $inout0,($out) # store 5 output blocks
2520 xorps @tweak[3],$inout3
2521 movdqu $inout1,16*1($out)
2522 xorps @tweak[4],$inout4
2523 movdqu $inout2,16*2($out)
2525 movdqu $inout3,16*3($out)
2526 pcmpgtd @tweak[5],$twtmp
2527 movdqu $inout4,16*4($out)
2528 lea 16*5($out),$out # $out+=5*16
2529 pshufd \$0x13,$twtmp,@tweak[1] # $twres
2533 movdqa @tweak[5],@tweak[0]
2534 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
2535 pand $twmask,@tweak[1] # isolate carry and residue
2536 pxor @tweak[5],@tweak[1]
2541 movups ($inp),$inout0
2542 lea 16*1($inp),$inp # $inp+=1*16
2543 xorps @tweak[0],$inout0
2545 &aesni_generate1("dec",$key,$rounds);
2547 xorps @tweak[0],$inout0
2548 movdqa @tweak[1],@tweak[0]
2549 movups $inout0,($out) # store one output block
2550 movdqa @tweak[2],@tweak[1]
2551 lea 16*1($out),$out # $out+=1*16
2556 movups ($inp),$inout0
2557 movups 16($inp),$inout1
2558 lea 32($inp),$inp # $inp+=2*16
2559 xorps @tweak[0],$inout0
2560 xorps @tweak[1],$inout1
2562 call _aesni_decrypt2
2564 xorps @tweak[0],$inout0
2565 movdqa @tweak[2],@tweak[0]
2566 xorps @tweak[1],$inout1
2567 movdqa @tweak[3],@tweak[1]
2568 movups $inout0,($out) # store 2 output blocks
2569 movups $inout1,16*1($out)
2570 lea 16*2($out),$out # $out+=2*16
2575 movups ($inp),$inout0
2576 movups 16*1($inp),$inout1
2577 movups 16*2($inp),$inout2
2578 lea 16*3($inp),$inp # $inp+=3*16
2579 xorps @tweak[0],$inout0
2580 xorps @tweak[1],$inout1
2581 xorps @tweak[2],$inout2
2583 call _aesni_decrypt3
2585 xorps @tweak[0],$inout0
2586 movdqa @tweak[3],@tweak[0]
2587 xorps @tweak[1],$inout1
2588 movdqa @tweak[4],@tweak[1]
2589 xorps @tweak[2],$inout2
2590 movups $inout0,($out) # store 3 output blocks
2591 movups $inout1,16*1($out)
2592 movups $inout2,16*2($out)
2593 lea 16*3($out),$out # $out+=3*16
2598 movups ($inp),$inout0
2599 movups 16*1($inp),$inout1
2600 movups 16*2($inp),$inout2
2601 xorps @tweak[0],$inout0
2602 movups 16*3($inp),$inout3
2603 lea 16*4($inp),$inp # $inp+=4*16
2604 xorps @tweak[1],$inout1
2605 xorps @tweak[2],$inout2
2606 xorps @tweak[3],$inout3
2608 call _aesni_decrypt4
2610 pxor @tweak[0],$inout0
2611 movdqa @tweak[4],@tweak[0]
2612 pxor @tweak[1],$inout1
2613 movdqa @tweak[5],@tweak[1]
2614 pxor @tweak[2],$inout2
2615 movdqu $inout0,($out) # store 4 output blocks
2616 pxor @tweak[3],$inout3
2617 movdqu $inout1,16*1($out)
2618 movdqu $inout2,16*2($out)
2619 movdqu $inout3,16*3($out)
2620 lea 16*4($out),$out # $out+=4*16
2625 and \$15,$len_ # see if $len%16 is 0
2629 mov $key_,$key # restore $key
2630 mov $rnds_,$rounds # restore $rounds
2632 movups ($inp),$inout0
2633 xorps @tweak[1],$inout0
2635 &aesni_generate1("dec",$key,$rounds);
2637 xorps @tweak[1],$inout0
2638 movups $inout0,($out)
2641 movzb 16($inp),%eax # borrow $rounds ...
2642 movzb ($out),%ecx # ... and $key
2650 sub $len_,$out # rewind $out
2651 mov $key_,$key # restore $key
2652 mov $rnds_,$rounds # restore $rounds
2654 movups ($out),$inout0
2655 xorps @tweak[0],$inout0
2657 &aesni_generate1("dec",$key,$rounds);
2659 xorps @tweak[0],$inout0
2660 movups $inout0,($out)
2663 xorps %xmm0,%xmm0 # clear register bank
2670 $code.=<<___ if (!$win64);
2673 movaps %xmm0,0x00(%rsp) # clear stack
2675 movaps %xmm0,0x10(%rsp)
2677 movaps %xmm0,0x20(%rsp)
2679 movaps %xmm0,0x30(%rsp)
2681 movaps %xmm0,0x40(%rsp)
2683 movaps %xmm0,0x50(%rsp)
2685 movaps %xmm0,0x60(%rsp)
2689 $code.=<<___ if ($win64);
2690 movaps -0xa0(%rbp),%xmm6
2691 movaps %xmm0,-0xa0(%rbp) # clear stack
2692 movaps -0x90(%rbp),%xmm7
2693 movaps %xmm0,-0x90(%rbp)
2694 movaps -0x80(%rbp),%xmm8
2695 movaps %xmm0,-0x80(%rbp)
2696 movaps -0x70(%rbp),%xmm9
2697 movaps %xmm0,-0x70(%rbp)
2698 movaps -0x60(%rbp),%xmm10
2699 movaps %xmm0,-0x60(%rbp)
2700 movaps -0x50(%rbp),%xmm11
2701 movaps %xmm0,-0x50(%rbp)
2702 movaps -0x40(%rbp),%xmm12
2703 movaps %xmm0,-0x40(%rbp)
2704 movaps -0x30(%rbp),%xmm13
2705 movaps %xmm0,-0x30(%rbp)
2706 movaps -0x20(%rbp),%xmm14
2707 movaps %xmm0,-0x20(%rbp)
2708 movaps -0x10(%rbp),%xmm15
2709 movaps %xmm0,-0x10(%rbp)
2710 movaps %xmm0,0x00(%rsp)
2711 movaps %xmm0,0x10(%rsp)
2712 movaps %xmm0,0x20(%rsp)
2713 movaps %xmm0,0x30(%rsp)
2714 movaps %xmm0,0x40(%rsp)
2715 movaps %xmm0,0x50(%rsp)
2716 movaps %xmm0,0x60(%rsp)
2723 .size aesni_xts_decrypt,.-aesni_xts_decrypt
2727 ######################################################################
2728 # void aesni_ocb_[en|de]crypt(const char *inp, char *out, size_t blocks,
2729 # const AES_KEY *key, unsigned int start_block_num,
2730 # unsigned char offset_i[16], const unsigned char L_[][16],
2731 # unsigned char checksum[16]);
2734 my @offset=map("%xmm$_",(10..15));
2735 my ($checksum,$rndkey0l)=("%xmm8","%xmm9");
2736 my ($block_num,$offset_p)=("%r8","%r9"); # 5th and 6th arguments
2737 my ($L_p,$checksum_p) = ("%rbx","%rbp");
2738 my ($i1,$i3,$i5) = ("%r12","%r13","%r14");
2739 my $seventh_arg = $win64 ? 56 : 8;
2743 .globl aesni_ocb_encrypt
2744 .type aesni_ocb_encrypt,\@function,6
2754 $code.=<<___ if ($win64);
2755 lea -0xa0(%rsp),%rsp
2756 movaps %xmm6,0x00(%rsp) # offload everything
2757 movaps %xmm7,0x10(%rsp)
2758 movaps %xmm8,0x20(%rsp)
2759 movaps %xmm9,0x30(%rsp)
2760 movaps %xmm10,0x40(%rsp)
2761 movaps %xmm11,0x50(%rsp)
2762 movaps %xmm12,0x60(%rsp)
2763 movaps %xmm13,0x70(%rsp)
2764 movaps %xmm14,0x80(%rsp)
2765 movaps %xmm15,0x90(%rsp)
2769 mov $seventh_arg(%rax),$L_p # 7th argument
2770 mov $seventh_arg+8(%rax),$checksum_p# 8th argument
2772 mov 240($key),$rnds_
2775 $movkey ($key),$rndkey0l # round[0]
2776 $movkey 16($key,$rnds_),$rndkey1 # round[last]
2778 movdqu ($offset_p),@offset[5] # load last offset_i
2779 pxor $rndkey1,$rndkey0l # round[0] ^ round[last]
2780 pxor $rndkey1,@offset[5] # offset_i ^ round[last]
2783 lea 32($key_,$rnds_),$key
2784 $movkey 16($key_),$rndkey1 # round[1]
2785 sub %r10,%rax # twisted $rounds
2786 mov %rax,%r10 # backup twisted $rounds
2788 movdqu ($L_p),@offset[0] # L_0 for all odd-numbered blocks
2789 movdqu ($checksum_p),$checksum # load checksum
2791 test \$1,$block_num # is first block number odd?
2797 movdqu ($L_p,$i1),$inout5 # borrow
2798 movdqu ($inp),$inout0
2803 movdqa $inout5,@offset[5]
2804 movups $inout0,($out)
2810 lea 1($block_num),$i1 # even-numbered blocks
2811 lea 3($block_num),$i3
2812 lea 5($block_num),$i5
2813 lea 6($block_num),$block_num
2814 bsf $i1,$i1 # ntz(block)
2817 shl \$4,$i1 # ntz(block) -> table offset
2823 jmp .Locb_enc_grandloop
2826 .Locb_enc_grandloop:
2827 movdqu `16*0`($inp),$inout0 # load input
2828 movdqu `16*1`($inp),$inout1
2829 movdqu `16*2`($inp),$inout2
2830 movdqu `16*3`($inp),$inout3
2831 movdqu `16*4`($inp),$inout4
2832 movdqu `16*5`($inp),$inout5
2833 lea `16*6`($inp),$inp
2837 movups $inout0,`16*0`($out) # store output
2838 movups $inout1,`16*1`($out)
2839 movups $inout2,`16*2`($out)
2840 movups $inout3,`16*3`($out)
2841 movups $inout4,`16*4`($out)
2842 movups $inout5,`16*5`($out)
2843 lea `16*6`($out),$out
2845 jnc .Locb_enc_grandloop
2851 movdqu `16*0`($inp),$inout0
2854 movdqu `16*1`($inp),$inout1
2857 movdqu `16*2`($inp),$inout2
2860 movdqu `16*3`($inp),$inout3
2863 movdqu `16*4`($inp),$inout4
2864 pxor $inout5,$inout5
2868 movdqa @offset[4],@offset[5]
2869 movups $inout0,`16*0`($out)
2870 movups $inout1,`16*1`($out)
2871 movups $inout2,`16*2`($out)
2872 movups $inout3,`16*3`($out)
2873 movups $inout4,`16*4`($out)
2879 movdqa @offset[0],$inout5 # borrow
2883 movdqa $inout5,@offset[5]
2884 movups $inout0,`16*0`($out)
2889 pxor $inout2,$inout2
2890 pxor $inout3,$inout3
2894 movdqa @offset[1],@offset[5]
2895 movups $inout0,`16*0`($out)
2896 movups $inout1,`16*1`($out)
2902 pxor $inout3,$inout3
2906 movdqa @offset[2],@offset[5]
2907 movups $inout0,`16*0`($out)
2908 movups $inout1,`16*1`($out)
2909 movups $inout2,`16*2`($out)
2917 movdqa @offset[3],@offset[5]
2918 movups $inout0,`16*0`($out)
2919 movups $inout1,`16*1`($out)
2920 movups $inout2,`16*2`($out)
2921 movups $inout3,`16*3`($out)
2924 pxor $rndkey0,@offset[5] # "remove" round[last]
2925 movdqu $checksum,($checksum_p) # store checksum
2926 movdqu @offset[5],($offset_p) # store last offset_i
2928 xorps %xmm0,%xmm0 # clear register bank
2935 $code.=<<___ if (!$win64);
2947 $code.=<<___ if ($win64);
2948 movaps 0x00(%rsp),%xmm6
2949 movaps %xmm0,0x00(%rsp) # clear stack
2950 movaps 0x10(%rsp),%xmm7
2951 movaps %xmm0,0x10(%rsp)
2952 movaps 0x20(%rsp),%xmm8
2953 movaps %xmm0,0x20(%rsp)
2954 movaps 0x30(%rsp),%xmm9
2955 movaps %xmm0,0x30(%rsp)
2956 movaps 0x40(%rsp),%xmm10
2957 movaps %xmm0,0x40(%rsp)
2958 movaps 0x50(%rsp),%xmm11
2959 movaps %xmm0,0x50(%rsp)
2960 movaps 0x60(%rsp),%xmm12
2961 movaps %xmm0,0x60(%rsp)
2962 movaps 0x70(%rsp),%xmm13
2963 movaps %xmm0,0x70(%rsp)
2964 movaps 0x80(%rsp),%xmm14
2965 movaps %xmm0,0x80(%rsp)
2966 movaps 0x90(%rsp),%xmm15
2967 movaps %xmm0,0x90(%rsp)
2968 lea 0xa0+0x28(%rsp),%rax
2980 .size aesni_ocb_encrypt,.-aesni_ocb_encrypt
2982 .type __ocb_encrypt6,\@abi-omnipotent
2985 pxor $rndkey0l,@offset[5] # offset_i ^ round[0]
2986 movdqu ($L_p,$i1),@offset[1]
2987 movdqa @offset[0],@offset[2]
2988 movdqu ($L_p,$i3),@offset[3]
2989 movdqa @offset[0],@offset[4]
2990 pxor @offset[5],@offset[0]
2991 movdqu ($L_p,$i5),@offset[5]
2992 pxor @offset[0],@offset[1]
2993 pxor $inout0,$checksum # accumulate checksum
2994 pxor @offset[0],$inout0 # input ^ round[0] ^ offset_i
2995 pxor @offset[1],@offset[2]
2996 pxor $inout1,$checksum
2997 pxor @offset[1],$inout1
2998 pxor @offset[2],@offset[3]
2999 pxor $inout2,$checksum
3000 pxor @offset[2],$inout2
3001 pxor @offset[3],@offset[4]
3002 pxor $inout3,$checksum
3003 pxor @offset[3],$inout3
3004 pxor @offset[4],@offset[5]
3005 pxor $inout4,$checksum
3006 pxor @offset[4],$inout4
3007 pxor $inout5,$checksum
3008 pxor @offset[5],$inout5
3009 $movkey 32($key_),$rndkey0
3011 lea 1($block_num),$i1 # even-numbered blocks
3012 lea 3($block_num),$i3
3013 lea 5($block_num),$i5
3015 pxor $rndkey0l,@offset[0] # offset_i ^ round[last]
3016 bsf $i1,$i1 # ntz(block)
3020 aesenc $rndkey1,$inout0
3021 aesenc $rndkey1,$inout1
3022 aesenc $rndkey1,$inout2
3023 aesenc $rndkey1,$inout3
3024 pxor $rndkey0l,@offset[1]
3025 pxor $rndkey0l,@offset[2]
3026 aesenc $rndkey1,$inout4
3027 pxor $rndkey0l,@offset[3]
3028 pxor $rndkey0l,@offset[4]
3029 aesenc $rndkey1,$inout5
3030 $movkey 48($key_),$rndkey1
3031 pxor $rndkey0l,@offset[5]
3033 aesenc $rndkey0,$inout0
3034 aesenc $rndkey0,$inout1
3035 aesenc $rndkey0,$inout2
3036 aesenc $rndkey0,$inout3
3037 aesenc $rndkey0,$inout4
3038 aesenc $rndkey0,$inout5
3039 $movkey 64($key_),$rndkey0
3040 shl \$4,$i1 # ntz(block) -> table offset
3046 aesenc $rndkey1,$inout0
3047 aesenc $rndkey1,$inout1
3048 aesenc $rndkey1,$inout2
3049 aesenc $rndkey1,$inout3
3050 aesenc $rndkey1,$inout4
3051 aesenc $rndkey1,$inout5
3052 $movkey ($key,%rax),$rndkey1
3055 aesenc $rndkey0,$inout0
3056 aesenc $rndkey0,$inout1
3057 aesenc $rndkey0,$inout2
3058 aesenc $rndkey0,$inout3
3059 aesenc $rndkey0,$inout4
3060 aesenc $rndkey0,$inout5
3061 $movkey -16($key,%rax),$rndkey0
3064 aesenc $rndkey1,$inout0
3065 aesenc $rndkey1,$inout1
3066 aesenc $rndkey1,$inout2
3067 aesenc $rndkey1,$inout3
3068 aesenc $rndkey1,$inout4
3069 aesenc $rndkey1,$inout5
3070 $movkey 16($key_),$rndkey1
3073 aesenclast @offset[0],$inout0
3074 movdqu ($L_p),@offset[0] # L_0 for all odd-numbered blocks
3075 mov %r10,%rax # restore twisted rounds
3076 aesenclast @offset[1],$inout1
3077 aesenclast @offset[2],$inout2
3078 aesenclast @offset[3],$inout3
3079 aesenclast @offset[4],$inout4
3080 aesenclast @offset[5],$inout5
3082 .size __ocb_encrypt6,.-__ocb_encrypt6
3084 .type __ocb_encrypt4,\@abi-omnipotent
3087 pxor $rndkey0l,@offset[5] # offset_i ^ round[0]
3088 movdqu ($L_p,$i1),@offset[1]
3089 movdqa @offset[0],@offset[2]
3090 movdqu ($L_p,$i3),@offset[3]
3091 pxor @offset[5],@offset[0]
3092 pxor @offset[0],@offset[1]
3093 pxor $inout0,$checksum # accumulate checksum
3094 pxor @offset[0],$inout0 # input ^ round[0] ^ offset_i
3095 pxor @offset[1],@offset[2]
3096 pxor $inout1,$checksum
3097 pxor @offset[1],$inout1
3098 pxor @offset[2],@offset[3]
3099 pxor $inout2,$checksum
3100 pxor @offset[2],$inout2
3101 pxor $inout3,$checksum
3102 pxor @offset[3],$inout3
3103 $movkey 32($key_),$rndkey0
3105 pxor $rndkey0l,@offset[0] # offset_i ^ round[last]
3106 pxor $rndkey0l,@offset[1]
3107 pxor $rndkey0l,@offset[2]
3108 pxor $rndkey0l,@offset[3]
3110 aesenc $rndkey1,$inout0
3111 aesenc $rndkey1,$inout1
3112 aesenc $rndkey1,$inout2
3113 aesenc $rndkey1,$inout3
3114 $movkey 48($key_),$rndkey1
3116 aesenc $rndkey0,$inout0
3117 aesenc $rndkey0,$inout1
3118 aesenc $rndkey0,$inout2
3119 aesenc $rndkey0,$inout3
3120 $movkey 64($key_),$rndkey0
3125 aesenc $rndkey1,$inout0
3126 aesenc $rndkey1,$inout1
3127 aesenc $rndkey1,$inout2
3128 aesenc $rndkey1,$inout3
3129 $movkey ($key,%rax),$rndkey1
3132 aesenc $rndkey0,$inout0
3133 aesenc $rndkey0,$inout1
3134 aesenc $rndkey0,$inout2
3135 aesenc $rndkey0,$inout3
3136 $movkey -16($key,%rax),$rndkey0
3139 aesenc $rndkey1,$inout0
3140 aesenc $rndkey1,$inout1
3141 aesenc $rndkey1,$inout2
3142 aesenc $rndkey1,$inout3
3143 $movkey 16($key_),$rndkey1
3144 mov %r10,%rax # restore twisted rounds
3146 aesenclast @offset[0],$inout0
3147 aesenclast @offset[1],$inout1
3148 aesenclast @offset[2],$inout2
3149 aesenclast @offset[3],$inout3
3151 .size __ocb_encrypt4,.-__ocb_encrypt4
3153 .type __ocb_encrypt1,\@abi-omnipotent
3156 pxor @offset[5],$inout5 # offset_i
3157 pxor $rndkey0l,$inout5 # offset_i ^ round[0]
3158 pxor $inout0,$checksum # accumulate checksum
3159 pxor $inout5,$inout0 # input ^ round[0] ^ offset_i
3160 $movkey 32($key_),$rndkey0
3162 aesenc $rndkey1,$inout0
3163 $movkey 48($key_),$rndkey1
3164 pxor $rndkey0l,$inout5 # offset_i ^ round[last]
3166 aesenc $rndkey0,$inout0
3167 $movkey 64($key_),$rndkey0
3172 aesenc $rndkey1,$inout0
3173 $movkey ($key,%rax),$rndkey1
3176 aesenc $rndkey0,$inout0
3177 $movkey -16($key,%rax),$rndkey0
3180 aesenc $rndkey1,$inout0
3181 $movkey 16($key_),$rndkey1 # redundant in tail
3182 mov %r10,%rax # restore twisted rounds
3184 aesenclast $inout5,$inout0
3186 .size __ocb_encrypt1,.-__ocb_encrypt1
3188 .globl aesni_ocb_decrypt
3189 .type aesni_ocb_decrypt,\@function,6
3199 $code.=<<___ if ($win64);
3200 lea -0xa0(%rsp),%rsp
3201 movaps %xmm6,0x00(%rsp) # offload everything
3202 movaps %xmm7,0x10(%rsp)
3203 movaps %xmm8,0x20(%rsp)
3204 movaps %xmm9,0x30(%rsp)
3205 movaps %xmm10,0x40(%rsp)
3206 movaps %xmm11,0x50(%rsp)
3207 movaps %xmm12,0x60(%rsp)
3208 movaps %xmm13,0x70(%rsp)
3209 movaps %xmm14,0x80(%rsp)
3210 movaps %xmm15,0x90(%rsp)
3214 mov $seventh_arg(%rax),$L_p # 7th argument
3215 mov $seventh_arg+8(%rax),$checksum_p# 8th argument
3217 mov 240($key),$rnds_
3220 $movkey ($key),$rndkey0l # round[0]
3221 $movkey 16($key,$rnds_),$rndkey1 # round[last]
3223 movdqu ($offset_p),@offset[5] # load last offset_i
3224 pxor $rndkey1,$rndkey0l # round[0] ^ round[last]
3225 pxor $rndkey1,@offset[5] # offset_i ^ round[last]
3228 lea 32($key_,$rnds_),$key
3229 $movkey 16($key_),$rndkey1 # round[1]
3230 sub %r10,%rax # twisted $rounds
3231 mov %rax,%r10 # backup twisted $rounds
3233 movdqu ($L_p),@offset[0] # L_0 for all odd-numbered blocks
3234 movdqu ($checksum_p),$checksum # load checksum
3236 test \$1,$block_num # is first block number odd?
3242 movdqu ($L_p,$i1),$inout5 # borrow
3243 movdqu ($inp),$inout0
3248 movdqa $inout5,@offset[5]
3249 movups $inout0,($out)
3250 xorps $inout0,$checksum # accumulate checksum
3256 lea 1($block_num),$i1 # even-numbered blocks
3257 lea 3($block_num),$i3
3258 lea 5($block_num),$i5
3259 lea 6($block_num),$block_num
3260 bsf $i1,$i1 # ntz(block)
3263 shl \$4,$i1 # ntz(block) -> table offset
3269 jmp .Locb_dec_grandloop
3272 .Locb_dec_grandloop:
3273 movdqu `16*0`($inp),$inout0 # load input
3274 movdqu `16*1`($inp),$inout1
3275 movdqu `16*2`($inp),$inout2
3276 movdqu `16*3`($inp),$inout3
3277 movdqu `16*4`($inp),$inout4
3278 movdqu `16*5`($inp),$inout5
3279 lea `16*6`($inp),$inp
3283 movups $inout0,`16*0`($out) # store output
3284 pxor $inout0,$checksum # accumulate checksum
3285 movups $inout1,`16*1`($out)
3286 pxor $inout1,$checksum
3287 movups $inout2,`16*2`($out)
3288 pxor $inout2,$checksum
3289 movups $inout3,`16*3`($out)
3290 pxor $inout3,$checksum
3291 movups $inout4,`16*4`($out)
3292 pxor $inout4,$checksum
3293 movups $inout5,`16*5`($out)
3294 pxor $inout5,$checksum
3295 lea `16*6`($out),$out
3297 jnc .Locb_dec_grandloop
3303 movdqu `16*0`($inp),$inout0
3306 movdqu `16*1`($inp),$inout1
3309 movdqu `16*2`($inp),$inout2
3312 movdqu `16*3`($inp),$inout3
3315 movdqu `16*4`($inp),$inout4
3316 pxor $inout5,$inout5
3320 movdqa @offset[4],@offset[5]
3321 movups $inout0,`16*0`($out) # store output
3322 pxor $inout0,$checksum # accumulate checksum
3323 movups $inout1,`16*1`($out)
3324 pxor $inout1,$checksum
3325 movups $inout2,`16*2`($out)
3326 pxor $inout2,$checksum
3327 movups $inout3,`16*3`($out)
3328 pxor $inout3,$checksum
3329 movups $inout4,`16*4`($out)
3330 pxor $inout4,$checksum
3336 movdqa @offset[0],$inout5 # borrow
3340 movdqa $inout5,@offset[5]
3341 movups $inout0,`16*0`($out) # store output
3342 xorps $inout0,$checksum # accumulate checksum
3347 pxor $inout2,$inout2
3348 pxor $inout3,$inout3
3352 movdqa @offset[1],@offset[5]
3353 movups $inout0,`16*0`($out) # store output
3354 xorps $inout0,$checksum # accumulate checksum
3355 movups $inout1,`16*1`($out)
3356 xorps $inout1,$checksum
3362 pxor $inout3,$inout3
3366 movdqa @offset[2],@offset[5]
3367 movups $inout0,`16*0`($out) # store output
3368 xorps $inout0,$checksum # accumulate checksum
3369 movups $inout1,`16*1`($out)
3370 xorps $inout1,$checksum
3371 movups $inout2,`16*2`($out)
3372 xorps $inout2,$checksum
3380 movdqa @offset[3],@offset[5]
3381 movups $inout0,`16*0`($out) # store output
3382 pxor $inout0,$checksum # accumulate checksum
3383 movups $inout1,`16*1`($out)
3384 pxor $inout1,$checksum
3385 movups $inout2,`16*2`($out)
3386 pxor $inout2,$checksum
3387 movups $inout3,`16*3`($out)
3388 pxor $inout3,$checksum
3391 pxor $rndkey0,@offset[5] # "remove" round[last]
3392 movdqu $checksum,($checksum_p) # store checksum
3393 movdqu @offset[5],($offset_p) # store last offset_i
3395 xorps %xmm0,%xmm0 # clear register bank
3402 $code.=<<___ if (!$win64);
3414 $code.=<<___ if ($win64);
3415 movaps 0x00(%rsp),%xmm6
3416 movaps %xmm0,0x00(%rsp) # clear stack
3417 movaps 0x10(%rsp),%xmm7
3418 movaps %xmm0,0x10(%rsp)
3419 movaps 0x20(%rsp),%xmm8
3420 movaps %xmm0,0x20(%rsp)
3421 movaps 0x30(%rsp),%xmm9
3422 movaps %xmm0,0x30(%rsp)
3423 movaps 0x40(%rsp),%xmm10
3424 movaps %xmm0,0x40(%rsp)
3425 movaps 0x50(%rsp),%xmm11
3426 movaps %xmm0,0x50(%rsp)
3427 movaps 0x60(%rsp),%xmm12
3428 movaps %xmm0,0x60(%rsp)
3429 movaps 0x70(%rsp),%xmm13
3430 movaps %xmm0,0x70(%rsp)
3431 movaps 0x80(%rsp),%xmm14
3432 movaps %xmm0,0x80(%rsp)
3433 movaps 0x90(%rsp),%xmm15
3434 movaps %xmm0,0x90(%rsp)
3435 lea 0xa0+0x28(%rsp),%rax
3447 .size aesni_ocb_decrypt,.-aesni_ocb_decrypt
3449 .type __ocb_decrypt6,\@abi-omnipotent
3452 pxor $rndkey0l,@offset[5] # offset_i ^ round[0]
3453 movdqu ($L_p,$i1),@offset[1]
3454 movdqa @offset[0],@offset[2]
3455 movdqu ($L_p,$i3),@offset[3]
3456 movdqa @offset[0],@offset[4]
3457 pxor @offset[5],@offset[0]
3458 movdqu ($L_p,$i5),@offset[5]
3459 pxor @offset[0],@offset[1]
3460 pxor @offset[0],$inout0 # input ^ round[0] ^ offset_i
3461 pxor @offset[1],@offset[2]
3462 pxor @offset[1],$inout1
3463 pxor @offset[2],@offset[3]
3464 pxor @offset[2],$inout2
3465 pxor @offset[3],@offset[4]
3466 pxor @offset[3],$inout3
3467 pxor @offset[4],@offset[5]
3468 pxor @offset[4],$inout4
3469 pxor @offset[5],$inout5
3470 $movkey 32($key_),$rndkey0
3472 lea 1($block_num),$i1 # even-numbered blocks
3473 lea 3($block_num),$i3
3474 lea 5($block_num),$i5
3476 pxor $rndkey0l,@offset[0] # offset_i ^ round[last]
3477 bsf $i1,$i1 # ntz(block)
3481 aesdec $rndkey1,$inout0
3482 aesdec $rndkey1,$inout1
3483 aesdec $rndkey1,$inout2
3484 aesdec $rndkey1,$inout3
3485 pxor $rndkey0l,@offset[1]
3486 pxor $rndkey0l,@offset[2]
3487 aesdec $rndkey1,$inout4
3488 pxor $rndkey0l,@offset[3]
3489 pxor $rndkey0l,@offset[4]
3490 aesdec $rndkey1,$inout5
3491 $movkey 48($key_),$rndkey1
3492 pxor $rndkey0l,@offset[5]
3494 aesdec $rndkey0,$inout0
3495 aesdec $rndkey0,$inout1
3496 aesdec $rndkey0,$inout2
3497 aesdec $rndkey0,$inout3
3498 aesdec $rndkey0,$inout4
3499 aesdec $rndkey0,$inout5
3500 $movkey 64($key_),$rndkey0
3501 shl \$4,$i1 # ntz(block) -> table offset
3507 aesdec $rndkey1,$inout0
3508 aesdec $rndkey1,$inout1
3509 aesdec $rndkey1,$inout2
3510 aesdec $rndkey1,$inout3
3511 aesdec $rndkey1,$inout4
3512 aesdec $rndkey1,$inout5
3513 $movkey ($key,%rax),$rndkey1
3516 aesdec $rndkey0,$inout0
3517 aesdec $rndkey0,$inout1
3518 aesdec $rndkey0,$inout2
3519 aesdec $rndkey0,$inout3
3520 aesdec $rndkey0,$inout4
3521 aesdec $rndkey0,$inout5
3522 $movkey -16($key,%rax),$rndkey0
3525 aesdec $rndkey1,$inout0
3526 aesdec $rndkey1,$inout1
3527 aesdec $rndkey1,$inout2
3528 aesdec $rndkey1,$inout3
3529 aesdec $rndkey1,$inout4
3530 aesdec $rndkey1,$inout5
3531 $movkey 16($key_),$rndkey1
3534 aesdeclast @offset[0],$inout0
3535 movdqu ($L_p),@offset[0] # L_0 for all odd-numbered blocks
3536 mov %r10,%rax # restore twisted rounds
3537 aesdeclast @offset[1],$inout1
3538 aesdeclast @offset[2],$inout2
3539 aesdeclast @offset[3],$inout3
3540 aesdeclast @offset[4],$inout4
3541 aesdeclast @offset[5],$inout5
3543 .size __ocb_decrypt6,.-__ocb_decrypt6
3545 .type __ocb_decrypt4,\@abi-omnipotent
3548 pxor $rndkey0l,@offset[5] # offset_i ^ round[0]
3549 movdqu ($L_p,$i1),@offset[1]
3550 movdqa @offset[0],@offset[2]
3551 movdqu ($L_p,$i3),@offset[3]
3552 pxor @offset[5],@offset[0]
3553 pxor @offset[0],@offset[1]
3554 pxor @offset[0],$inout0 # input ^ round[0] ^ offset_i
3555 pxor @offset[1],@offset[2]
3556 pxor @offset[1],$inout1
3557 pxor @offset[2],@offset[3]
3558 pxor @offset[2],$inout2
3559 pxor @offset[3],$inout3
3560 $movkey 32($key_),$rndkey0
3562 pxor $rndkey0l,@offset[0] # offset_i ^ round[last]
3563 pxor $rndkey0l,@offset[1]
3564 pxor $rndkey0l,@offset[2]
3565 pxor $rndkey0l,@offset[3]
3567 aesdec $rndkey1,$inout0
3568 aesdec $rndkey1,$inout1
3569 aesdec $rndkey1,$inout2
3570 aesdec $rndkey1,$inout3
3571 $movkey 48($key_),$rndkey1
3573 aesdec $rndkey0,$inout0
3574 aesdec $rndkey0,$inout1
3575 aesdec $rndkey0,$inout2
3576 aesdec $rndkey0,$inout3
3577 $movkey 64($key_),$rndkey0
3582 aesdec $rndkey1,$inout0
3583 aesdec $rndkey1,$inout1
3584 aesdec $rndkey1,$inout2
3585 aesdec $rndkey1,$inout3
3586 $movkey ($key,%rax),$rndkey1
3589 aesdec $rndkey0,$inout0
3590 aesdec $rndkey0,$inout1
3591 aesdec $rndkey0,$inout2
3592 aesdec $rndkey0,$inout3
3593 $movkey -16($key,%rax),$rndkey0
3596 aesdec $rndkey1,$inout0
3597 aesdec $rndkey1,$inout1
3598 aesdec $rndkey1,$inout2
3599 aesdec $rndkey1,$inout3
3600 $movkey 16($key_),$rndkey1
3601 mov %r10,%rax # restore twisted rounds
3603 aesdeclast @offset[0],$inout0
3604 aesdeclast @offset[1],$inout1
3605 aesdeclast @offset[2],$inout2
3606 aesdeclast @offset[3],$inout3
3608 .size __ocb_decrypt4,.-__ocb_decrypt4
3610 .type __ocb_decrypt1,\@abi-omnipotent
3613 pxor @offset[5],$inout5 # offset_i
3614 pxor $rndkey0l,$inout5 # offset_i ^ round[0]
3615 pxor $inout5,$inout0 # input ^ round[0] ^ offset_i
3616 $movkey 32($key_),$rndkey0
3618 aesdec $rndkey1,$inout0
3619 $movkey 48($key_),$rndkey1
3620 pxor $rndkey0l,$inout5 # offset_i ^ round[last]
3622 aesdec $rndkey0,$inout0
3623 $movkey 64($key_),$rndkey0
3628 aesdec $rndkey1,$inout0
3629 $movkey ($key,%rax),$rndkey1
3632 aesdec $rndkey0,$inout0
3633 $movkey -16($key,%rax),$rndkey0
3636 aesdec $rndkey1,$inout0
3637 $movkey 16($key_),$rndkey1 # redundant in tail
3638 mov %r10,%rax # restore twisted rounds
3640 aesdeclast $inout5,$inout0
3642 .size __ocb_decrypt1,.-__ocb_decrypt1
3646 ########################################################################
3647 # void $PREFIX_cbc_encrypt (const void *inp, void *out,
3648 # size_t length, const AES_KEY *key,
3649 # unsigned char *ivp,const int enc);
3651 my $frame_size = 0x10 + ($win64?0xa0:0); # used in decrypt
3652 my ($iv,$in0,$in1,$in2,$in3,$in4)=map("%xmm$_",(10..15));
3656 .globl ${PREFIX}_cbc_encrypt
3657 .type ${PREFIX}_cbc_encrypt,\@function,6
3659 ${PREFIX}_cbc_encrypt:
3660 test $len,$len # check length
3663 mov 240($key),$rnds_ # key->rounds
3664 mov $key,$key_ # backup $key
3665 test %r9d,%r9d # 6th argument
3667 #--------------------------- CBC ENCRYPT ------------------------------#
3668 movups ($ivp),$inout0 # load iv as initial state
3676 movups ($inp),$inout1 # load input
3678 #xorps $inout1,$inout0
3680 &aesni_generate1("enc",$key,$rounds,$inout0,$inout1);
3682 mov $rnds_,$rounds # restore $rounds
3683 mov $key_,$key # restore $key
3684 movups $inout0,0($out) # store output
3690 pxor $rndkey0,$rndkey0 # clear register bank
3691 pxor $rndkey1,$rndkey1
3692 movups $inout0,($ivp)
3693 pxor $inout0,$inout0
3694 pxor $inout1,$inout1
3698 mov $len,%rcx # zaps $key
3699 xchg $inp,$out # $inp is %rsi and $out is %rdi now
3700 .long 0x9066A4F3 # rep movsb
3701 mov \$16,%ecx # zero tail
3704 .long 0x9066AAF3 # rep stosb
3705 lea -16(%rdi),%rdi # rewind $out by 1 block
3706 mov $rnds_,$rounds # restore $rounds
3707 mov %rdi,%rsi # $inp and $out are the same
3708 mov $key_,$key # restore $key
3709 xor $len,$len # len=16
3710 jmp .Lcbc_enc_loop # one more spin
3711 \f#--------------------------- CBC DECRYPT ------------------------------#
3715 jne .Lcbc_decrypt_bulk
3717 # handle single block without allocating stack frame,
3718 # useful in ciphertext stealing mode
3719 movdqu ($inp),$inout0 # load input
3720 movdqu ($ivp),$inout1 # load iv
3721 movdqa $inout0,$inout2 # future iv
3723 &aesni_generate1("dec",$key,$rnds_);
3725 pxor $rndkey0,$rndkey0 # clear register bank
3726 pxor $rndkey1,$rndkey1
3727 movdqu $inout2,($ivp) # store iv
3728 xorps $inout1,$inout0 # ^=iv
3729 pxor $inout1,$inout1
3730 movups $inout0,($out) # store output
3731 pxor $inout0,$inout0
3737 sub \$$frame_size,%rsp
3738 and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
3740 $code.=<<___ if ($win64);
3741 movaps %xmm6,0x10(%rsp)
3742 movaps %xmm7,0x20(%rsp)
3743 movaps %xmm8,0x30(%rsp)
3744 movaps %xmm9,0x40(%rsp)
3745 movaps %xmm10,0x50(%rsp)
3746 movaps %xmm11,0x60(%rsp)
3747 movaps %xmm12,0x70(%rsp)
3748 movaps %xmm13,0x80(%rsp)
3749 movaps %xmm14,0x90(%rsp)
3750 movaps %xmm15,0xa0(%rsp)
3760 $movkey ($key),$rndkey0
3761 movdqu 0x00($inp),$inout0 # load input
3762 movdqu 0x10($inp),$inout1
3764 movdqu 0x20($inp),$inout2
3766 movdqu 0x30($inp),$inout3
3768 movdqu 0x40($inp),$inout4
3770 movdqu 0x50($inp),$inout5
3772 mov OPENSSL_ia32cap_P+4(%rip),%r9d
3774 jbe .Lcbc_dec_six_or_seven
3776 and \$`1<<26|1<<22`,%r9d # isolate XSAVE+MOVBE
3777 sub \$0x50,$len # $len is biased by -5*16
3778 cmp \$`1<<22`,%r9d # check for MOVBE without XSAVE
3779 je .Lcbc_dec_loop6_enter # [which denotes Atom Silvermont]
3780 sub \$0x20,$len # $len is biased by -7*16
3781 lea 0x70($key),$key # size optimization
3782 jmp .Lcbc_dec_loop8_enter
3785 movups $inout7,($out)
3787 .Lcbc_dec_loop8_enter:
3788 movdqu 0x60($inp),$inout6
3789 pxor $rndkey0,$inout0
3790 movdqu 0x70($inp),$inout7
3791 pxor $rndkey0,$inout1
3792 $movkey 0x10-0x70($key),$rndkey1
3793 pxor $rndkey0,$inout2
3795 cmp \$0x70,$len # is there at least 0x60 bytes ahead?
3796 pxor $rndkey0,$inout3
3797 pxor $rndkey0,$inout4
3798 pxor $rndkey0,$inout5
3799 pxor $rndkey0,$inout6
3801 aesdec $rndkey1,$inout0
3802 pxor $rndkey0,$inout7
3803 $movkey 0x20-0x70($key),$rndkey0
3804 aesdec $rndkey1,$inout1
3805 aesdec $rndkey1,$inout2
3806 aesdec $rndkey1,$inout3
3807 aesdec $rndkey1,$inout4
3808 aesdec $rndkey1,$inout5
3809 aesdec $rndkey1,$inout6
3812 aesdec $rndkey1,$inout7
3814 $movkey 0x30-0x70($key),$rndkey1
3816 for($i=1;$i<12;$i++) {
3817 my $rndkeyx = ($i&1)?$rndkey0:$rndkey1;
3818 $code.=<<___ if ($i==7);
3822 aesdec $rndkeyx,$inout0
3823 aesdec $rndkeyx,$inout1
3824 aesdec $rndkeyx,$inout2
3825 aesdec $rndkeyx,$inout3
3826 aesdec $rndkeyx,$inout4
3827 aesdec $rndkeyx,$inout5
3828 aesdec $rndkeyx,$inout6
3829 aesdec $rndkeyx,$inout7
3830 $movkey `0x30+0x10*$i`-0x70($key),$rndkeyx
3832 $code.=<<___ if ($i<6 || (!($i&1) && $i>7));
3835 $code.=<<___ if ($i==7);
3838 $code.=<<___ if ($i==9);
3841 $code.=<<___ if ($i==11);
3848 aesdec $rndkey1,$inout0
3849 aesdec $rndkey1,$inout1
3852 aesdec $rndkey1,$inout2
3853 aesdec $rndkey1,$inout3
3856 aesdec $rndkey1,$inout4
3857 aesdec $rndkey1,$inout5
3860 aesdec $rndkey1,$inout6
3861 aesdec $rndkey1,$inout7
3862 movdqu 0x50($inp),$rndkey1
3864 aesdeclast $iv,$inout0
3865 movdqu 0x60($inp),$iv # borrow $iv
3866 pxor $rndkey0,$rndkey1
3867 aesdeclast $in0,$inout1
3869 movdqu 0x70($inp),$rndkey0 # next IV
3870 aesdeclast $in1,$inout2
3872 movdqu 0x00($inp_),$in0
3873 aesdeclast $in2,$inout3
3874 aesdeclast $in3,$inout4
3875 movdqu 0x10($inp_),$in1
3876 movdqu 0x20($inp_),$in2
3877 aesdeclast $in4,$inout5
3878 aesdeclast $rndkey1,$inout6
3879 movdqu 0x30($inp_),$in3
3880 movdqu 0x40($inp_),$in4
3881 aesdeclast $iv,$inout7
3882 movdqa $rndkey0,$iv # return $iv
3883 movdqu 0x50($inp_),$rndkey1
3884 $movkey -0x70($key),$rndkey0
3886 movups $inout0,($out) # store output
3888 movups $inout1,0x10($out)
3890 movups $inout2,0x20($out)
3892 movups $inout3,0x30($out)
3894 movups $inout4,0x40($out)
3896 movups $inout5,0x50($out)
3897 movdqa $rndkey1,$inout5
3898 movups $inout6,0x60($out)
3904 movaps $inout7,$inout0
3905 lea -0x70($key),$key
3907 jle .Lcbc_dec_clear_tail_collected
3908 movups $inout7,($out)
3914 .Lcbc_dec_six_or_seven:
3918 movaps $inout5,$inout6
3919 call _aesni_decrypt6
3920 pxor $iv,$inout0 # ^= IV
3923 movdqu $inout0,($out)
3925 movdqu $inout1,0x10($out)
3926 pxor $inout1,$inout1 # clear register bank
3928 movdqu $inout2,0x20($out)
3929 pxor $inout2,$inout2
3931 movdqu $inout3,0x30($out)
3932 pxor $inout3,$inout3
3934 movdqu $inout4,0x40($out)
3935 pxor $inout4,$inout4
3937 movdqa $inout5,$inout0
3938 pxor $inout5,$inout5
3939 jmp .Lcbc_dec_tail_collected
3943 movups 0x60($inp),$inout6
3944 xorps $inout7,$inout7
3945 call _aesni_decrypt8
3946 movups 0x50($inp),$inout7
3947 pxor $iv,$inout0 # ^= IV
3948 movups 0x60($inp),$iv
3950 movdqu $inout0,($out)
3952 movdqu $inout1,0x10($out)
3953 pxor $inout1,$inout1 # clear register bank
3955 movdqu $inout2,0x20($out)
3956 pxor $inout2,$inout2
3958 movdqu $inout3,0x30($out)
3959 pxor $inout3,$inout3
3961 movdqu $inout4,0x40($out)
3962 pxor $inout4,$inout4
3963 pxor $inout7,$inout6
3964 movdqu $inout5,0x50($out)
3965 pxor $inout5,$inout5
3967 movdqa $inout6,$inout0
3968 pxor $inout6,$inout6
3969 pxor $inout7,$inout7
3970 jmp .Lcbc_dec_tail_collected
3974 movups $inout5,($out)
3976 movdqu 0x00($inp),$inout0 # load input
3977 movdqu 0x10($inp),$inout1
3979 movdqu 0x20($inp),$inout2
3981 movdqu 0x30($inp),$inout3
3983 movdqu 0x40($inp),$inout4
3985 movdqu 0x50($inp),$inout5
3987 .Lcbc_dec_loop6_enter:
3989 movdqa $inout5,$inout6
3991 call _aesni_decrypt6
3993 pxor $iv,$inout0 # ^= IV
3996 movdqu $inout0,($out)
3998 movdqu $inout1,0x10($out)
4000 movdqu $inout2,0x20($out)
4003 movdqu $inout3,0x30($out)
4006 movdqu $inout4,0x40($out)
4011 movdqa $inout5,$inout0
4013 jle .Lcbc_dec_clear_tail_collected
4014 movups $inout5,($out)
4018 movups ($inp),$inout0
4020 jbe .Lcbc_dec_one # $len is 1*16 or less
4022 movups 0x10($inp),$inout1
4025 jbe .Lcbc_dec_two # $len is 2*16 or less
4027 movups 0x20($inp),$inout2
4030 jbe .Lcbc_dec_three # $len is 3*16 or less
4032 movups 0x30($inp),$inout3
4035 jbe .Lcbc_dec_four # $len is 4*16 or less
4037 movups 0x40($inp),$inout4 # $len is 5*16 or less
4040 xorps $inout5,$inout5
4041 call _aesni_decrypt6
4045 movdqu $inout0,($out)
4047 movdqu $inout1,0x10($out)
4048 pxor $inout1,$inout1 # clear register bank
4050 movdqu $inout2,0x20($out)
4051 pxor $inout2,$inout2
4053 movdqu $inout3,0x30($out)
4054 pxor $inout3,$inout3
4056 movdqa $inout4,$inout0
4057 pxor $inout4,$inout4
4058 pxor $inout5,$inout5
4060 jmp .Lcbc_dec_tail_collected
4066 &aesni_generate1("dec",$key,$rounds);
4070 jmp .Lcbc_dec_tail_collected
4074 call _aesni_decrypt2
4078 movdqu $inout0,($out)
4079 movdqa $inout1,$inout0
4080 pxor $inout1,$inout1 # clear register bank
4082 jmp .Lcbc_dec_tail_collected
4086 call _aesni_decrypt3
4090 movdqu $inout0,($out)
4092 movdqu $inout1,0x10($out)
4093 pxor $inout1,$inout1 # clear register bank
4094 movdqa $inout2,$inout0
4095 pxor $inout2,$inout2
4097 jmp .Lcbc_dec_tail_collected
4101 call _aesni_decrypt4
4105 movdqu $inout0,($out)
4107 movdqu $inout1,0x10($out)
4108 pxor $inout1,$inout1 # clear register bank
4110 movdqu $inout2,0x20($out)
4111 pxor $inout2,$inout2
4112 movdqa $inout3,$inout0
4113 pxor $inout3,$inout3
4115 jmp .Lcbc_dec_tail_collected
4118 .Lcbc_dec_clear_tail_collected:
4119 pxor $inout1,$inout1 # clear register bank
4120 pxor $inout2,$inout2
4121 pxor $inout3,$inout3
4123 $code.=<<___ if (!$win64);
4124 pxor $inout4,$inout4 # %xmm6..9
4125 pxor $inout5,$inout5
4126 pxor $inout6,$inout6
4127 pxor $inout7,$inout7
4130 .Lcbc_dec_tail_collected:
4133 jnz .Lcbc_dec_tail_partial
4134 movups $inout0,($out)
4135 pxor $inout0,$inout0
4138 .Lcbc_dec_tail_partial:
4139 movaps $inout0,(%rsp)
4140 pxor $inout0,$inout0
4145 .long 0x9066A4F3 # rep movsb
4146 movdqa $inout0,(%rsp)
4149 xorps $rndkey0,$rndkey0 # %xmm0
4150 pxor $rndkey1,$rndkey1
4152 $code.=<<___ if ($win64);
4153 movaps 0x10(%rsp),%xmm6
4154 movaps %xmm0,0x10(%rsp) # clear stack
4155 movaps 0x20(%rsp),%xmm7
4156 movaps %xmm0,0x20(%rsp)
4157 movaps 0x30(%rsp),%xmm8
4158 movaps %xmm0,0x30(%rsp)
4159 movaps 0x40(%rsp),%xmm9
4160 movaps %xmm0,0x40(%rsp)
4161 movaps 0x50(%rsp),%xmm10
4162 movaps %xmm0,0x50(%rsp)
4163 movaps 0x60(%rsp),%xmm11
4164 movaps %xmm0,0x60(%rsp)
4165 movaps 0x70(%rsp),%xmm12
4166 movaps %xmm0,0x70(%rsp)
4167 movaps 0x80(%rsp),%xmm13
4168 movaps %xmm0,0x80(%rsp)
4169 movaps 0x90(%rsp),%xmm14
4170 movaps %xmm0,0x90(%rsp)
4171 movaps 0xa0(%rsp),%xmm15
4172 movaps %xmm0,0xa0(%rsp)
4179 .size ${PREFIX}_cbc_encrypt,.-${PREFIX}_cbc_encrypt
4182 # int ${PREFIX}_set_decrypt_key(const unsigned char *inp,
4183 # int bits, AES_KEY *key)
4185 # input: $inp user-supplied key
4186 # $bits $inp length in bits
4187 # $key pointer to key schedule
4188 # output: %eax 0 denoting success, -1 or -2 - failure (see C)
4189 # *$key key schedule
4191 { my ($inp,$bits,$key) = @_4args;
4195 .globl ${PREFIX}_set_decrypt_key
4196 .type ${PREFIX}_set_decrypt_key,\@abi-omnipotent
4198 ${PREFIX}_set_decrypt_key:
4199 .byte 0x48,0x83,0xEC,0x08 # sub rsp,8
4200 call __aesni_set_encrypt_key
4201 shl \$4,$bits # rounds-1 after _aesni_set_encrypt_key
4204 lea 16($key,$bits),$inp # points at the end of key schedule
4206 $movkey ($key),%xmm0 # just swap
4207 $movkey ($inp),%xmm1
4208 $movkey %xmm0,($inp)
4209 $movkey %xmm1,($key)
4214 $movkey ($key),%xmm0 # swap and inverse
4215 $movkey ($inp),%xmm1
4220 $movkey %xmm0,16($inp)
4221 $movkey %xmm1,-16($key)
4223 ja .Ldec_key_inverse
4225 $movkey ($key),%xmm0 # inverse middle
4228 $movkey %xmm0,($inp)
4233 .LSEH_end_set_decrypt_key:
4234 .size ${PREFIX}_set_decrypt_key,.-${PREFIX}_set_decrypt_key
4237 # This is based on submission by
4239 # Huang Ying <ying.huang@intel.com>
4240 # Vinodh Gopal <vinodh.gopal@intel.com>
4243 # Aggressively optimized in respect to aeskeygenassist's critical path
4244 # and is contained in %xmm0-5 to meet Win64 ABI requirement.
4246 # int ${PREFIX}_set_encrypt_key(const unsigned char *inp,
4247 # int bits, AES_KEY * const key);
4249 # input: $inp user-supplied key
4250 # $bits $inp length in bits
4251 # $key pointer to key schedule
4252 # output: %eax 0 denoting success, -1 or -2 - failure (see C)
4253 # $bits rounds-1 (used in aesni_set_decrypt_key)
4254 # *$key key schedule
4255 # $key pointer to key schedule (used in
4256 # aesni_set_decrypt_key)
4258 # Subroutine is frame-less, which means that only volatile registers
4259 # are used. Note that it's declared "abi-omnipotent", which means that
4260 # amount of volatile registers is smaller on Windows.
4263 .globl ${PREFIX}_set_encrypt_key
4264 .type ${PREFIX}_set_encrypt_key,\@abi-omnipotent
4266 ${PREFIX}_set_encrypt_key:
4267 __aesni_set_encrypt_key:
4268 .byte 0x48,0x83,0xEC,0x08 # sub rsp,8
4275 mov \$`1<<28|1<<11`,%r10d # AVX and XOP bits
4276 movups ($inp),%xmm0 # pull first 128 bits of *userKey
4277 xorps %xmm4,%xmm4 # low dword of xmm4 is assumed 0
4278 and OPENSSL_ia32cap_P+4(%rip),%r10d
4279 lea 16($key),%rax # %rax is used as modifiable copy of $key
4288 mov \$9,$bits # 10 rounds for 128-bit key
4289 cmp \$`1<<28`,%r10d # AVX, bit no XOP
4292 $movkey %xmm0,($key) # round 0
4293 aeskeygenassist \$0x1,%xmm0,%xmm1 # round 1
4294 call .Lkey_expansion_128_cold
4295 aeskeygenassist \$0x2,%xmm0,%xmm1 # round 2
4296 call .Lkey_expansion_128
4297 aeskeygenassist \$0x4,%xmm0,%xmm1 # round 3
4298 call .Lkey_expansion_128
4299 aeskeygenassist \$0x8,%xmm0,%xmm1 # round 4
4300 call .Lkey_expansion_128
4301 aeskeygenassist \$0x10,%xmm0,%xmm1 # round 5
4302 call .Lkey_expansion_128
4303 aeskeygenassist \$0x20,%xmm0,%xmm1 # round 6
4304 call .Lkey_expansion_128
4305 aeskeygenassist \$0x40,%xmm0,%xmm1 # round 7
4306 call .Lkey_expansion_128
4307 aeskeygenassist \$0x80,%xmm0,%xmm1 # round 8
4308 call .Lkey_expansion_128
4309 aeskeygenassist \$0x1b,%xmm0,%xmm1 # round 9
4310 call .Lkey_expansion_128
4311 aeskeygenassist \$0x36,%xmm0,%xmm1 # round 10
4312 call .Lkey_expansion_128
4313 $movkey %xmm0,(%rax)
4314 mov $bits,80(%rax) # 240(%rdx)
4320 movdqa .Lkey_rotate(%rip),%xmm5
4322 movdqa .Lkey_rcon1(%rip),%xmm4
4330 aesenclast %xmm4,%xmm0
4343 movdqu %xmm0,-16(%rax)
4349 movdqa .Lkey_rcon1b(%rip),%xmm4
4352 aesenclast %xmm4,%xmm0
4368 aesenclast %xmm4,%xmm0
4379 movdqu %xmm0,16(%rax)
4381 mov $bits,96(%rax) # 240($key)
4387 movq 16($inp),%xmm2 # remaining 1/3 of *userKey
4388 mov \$11,$bits # 12 rounds for 192
4389 cmp \$`1<<28`,%r10d # AVX, but no XOP
4392 $movkey %xmm0,($key) # round 0
4393 aeskeygenassist \$0x1,%xmm2,%xmm1 # round 1,2
4394 call .Lkey_expansion_192a_cold
4395 aeskeygenassist \$0x2,%xmm2,%xmm1 # round 2,3
4396 call .Lkey_expansion_192b
4397 aeskeygenassist \$0x4,%xmm2,%xmm1 # round 4,5
4398 call .Lkey_expansion_192a
4399 aeskeygenassist \$0x8,%xmm2,%xmm1 # round 5,6
4400 call .Lkey_expansion_192b
4401 aeskeygenassist \$0x10,%xmm2,%xmm1 # round 7,8
4402 call .Lkey_expansion_192a
4403 aeskeygenassist \$0x20,%xmm2,%xmm1 # round 8,9
4404 call .Lkey_expansion_192b
4405 aeskeygenassist \$0x40,%xmm2,%xmm1 # round 10,11
4406 call .Lkey_expansion_192a
4407 aeskeygenassist \$0x80,%xmm2,%xmm1 # round 11,12
4408 call .Lkey_expansion_192b
4409 $movkey %xmm0,(%rax)
4410 mov $bits,48(%rax) # 240(%rdx)
4416 movdqa .Lkey_rotate192(%rip),%xmm5
4417 movdqa .Lkey_rcon1(%rip),%xmm4
4427 aesenclast %xmm4,%xmm2
4439 pshufd \$0xff,%xmm0,%xmm3
4446 movdqu %xmm0,-16(%rax)
4451 mov $bits,32(%rax) # 240($key)
4457 movups 16($inp),%xmm2 # remaning half of *userKey
4458 mov \$13,$bits # 14 rounds for 256
4460 cmp \$`1<<28`,%r10d # AVX, but no XOP
4463 $movkey %xmm0,($key) # round 0
4464 $movkey %xmm2,16($key) # round 1
4465 aeskeygenassist \$0x1,%xmm2,%xmm1 # round 2
4466 call .Lkey_expansion_256a_cold
4467 aeskeygenassist \$0x1,%xmm0,%xmm1 # round 3
4468 call .Lkey_expansion_256b
4469 aeskeygenassist \$0x2,%xmm2,%xmm1 # round 4
4470 call .Lkey_expansion_256a
4471 aeskeygenassist \$0x2,%xmm0,%xmm1 # round 5
4472 call .Lkey_expansion_256b
4473 aeskeygenassist \$0x4,%xmm2,%xmm1 # round 6
4474 call .Lkey_expansion_256a
4475 aeskeygenassist \$0x4,%xmm0,%xmm1 # round 7
4476 call .Lkey_expansion_256b
4477 aeskeygenassist \$0x8,%xmm2,%xmm1 # round 8
4478 call .Lkey_expansion_256a
4479 aeskeygenassist \$0x8,%xmm0,%xmm1 # round 9
4480 call .Lkey_expansion_256b
4481 aeskeygenassist \$0x10,%xmm2,%xmm1 # round 10
4482 call .Lkey_expansion_256a
4483 aeskeygenassist \$0x10,%xmm0,%xmm1 # round 11
4484 call .Lkey_expansion_256b
4485 aeskeygenassist \$0x20,%xmm2,%xmm1 # round 12
4486 call .Lkey_expansion_256a
4487 aeskeygenassist \$0x20,%xmm0,%xmm1 # round 13
4488 call .Lkey_expansion_256b
4489 aeskeygenassist \$0x40,%xmm2,%xmm1 # round 14
4490 call .Lkey_expansion_256a
4491 $movkey %xmm0,(%rax)
4492 mov $bits,16(%rax) # 240(%rdx)
4498 movdqa .Lkey_rotate(%rip),%xmm5
4499 movdqa .Lkey_rcon1(%rip),%xmm4
4501 movdqu %xmm0,0($key)
4503 movdqu %xmm2,16($key)
4509 aesenclast %xmm4,%xmm2
4526 pshufd \$0xff,%xmm0,%xmm2
4528 aesenclast %xmm3,%xmm2
4539 movdqu %xmm2,16(%rax)
4546 mov $bits,16(%rax) # 240($key)
4562 .LSEH_end_set_encrypt_key:
4565 .Lkey_expansion_128:
4566 $movkey %xmm0,(%rax)
4568 .Lkey_expansion_128_cold:
4569 shufps \$0b00010000,%xmm0,%xmm4
4571 shufps \$0b10001100,%xmm0,%xmm4
4573 shufps \$0b11111111,%xmm1,%xmm1 # critical path
4578 .Lkey_expansion_192a:
4579 $movkey %xmm0,(%rax)
4581 .Lkey_expansion_192a_cold:
4583 .Lkey_expansion_192b_warm:
4584 shufps \$0b00010000,%xmm0,%xmm4
4587 shufps \$0b10001100,%xmm0,%xmm4
4590 pshufd \$0b01010101,%xmm1,%xmm1 # critical path
4593 pshufd \$0b11111111,%xmm0,%xmm3
4598 .Lkey_expansion_192b:
4600 shufps \$0b01000100,%xmm0,%xmm5
4601 $movkey %xmm5,(%rax)
4602 shufps \$0b01001110,%xmm2,%xmm3
4603 $movkey %xmm3,16(%rax)
4605 jmp .Lkey_expansion_192b_warm
4608 .Lkey_expansion_256a:
4609 $movkey %xmm2,(%rax)
4611 .Lkey_expansion_256a_cold:
4612 shufps \$0b00010000,%xmm0,%xmm4
4614 shufps \$0b10001100,%xmm0,%xmm4
4616 shufps \$0b11111111,%xmm1,%xmm1 # critical path
4621 .Lkey_expansion_256b:
4622 $movkey %xmm0,(%rax)
4625 shufps \$0b00010000,%xmm2,%xmm4
4627 shufps \$0b10001100,%xmm2,%xmm4
4629 shufps \$0b10101010,%xmm1,%xmm1 # critical path
4632 .size ${PREFIX}_set_encrypt_key,.-${PREFIX}_set_encrypt_key
4633 .size __aesni_set_encrypt_key,.-__aesni_set_encrypt_key
4640 .byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
4648 .byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
4650 .long 0x0c0f0e0d,0x0c0f0e0d,0x0c0f0e0d,0x0c0f0e0d
4652 .long 0x04070605,0x04070605,0x04070605,0x04070605
4656 .long 0x1b,0x1b,0x1b,0x1b
4658 .asciz "AES for Intel AES-NI, CRYPTOGAMS by <appro\@openssl.org>"
4662 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
4663 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
4671 .extern __imp_RtlVirtualUnwind
4673 $code.=<<___ if ($PREFIX eq "aesni");
4674 .type ecb_ccm64_se_handler,\@abi-omnipotent
4676 ecb_ccm64_se_handler:
4688 mov 120($context),%rax # pull context->Rax
4689 mov 248($context),%rbx # pull context->Rip
4691 mov 8($disp),%rsi # disp->ImageBase
4692 mov 56($disp),%r11 # disp->HandlerData
4694 mov 0(%r11),%r10d # HandlerData[0]
4695 lea (%rsi,%r10),%r10 # prologue label
4696 cmp %r10,%rbx # context->Rip<prologue label
4697 jb .Lcommon_seh_tail
4699 mov 152($context),%rax # pull context->Rsp
4701 mov 4(%r11),%r10d # HandlerData[1]
4702 lea (%rsi,%r10),%r10 # epilogue label
4703 cmp %r10,%rbx # context->Rip>=epilogue label
4704 jae .Lcommon_seh_tail
4706 lea 0(%rax),%rsi # %xmm save area
4707 lea 512($context),%rdi # &context.Xmm6
4708 mov \$8,%ecx # 4*sizeof(%xmm0)/sizeof(%rax)
4709 .long 0xa548f3fc # cld; rep movsq
4710 lea 0x58(%rax),%rax # adjust stack pointer
4712 jmp .Lcommon_seh_tail
4713 .size ecb_ccm64_se_handler,.-ecb_ccm64_se_handler
4715 .type ctr_xts_se_handler,\@abi-omnipotent
4729 mov 120($context),%rax # pull context->Rax
4730 mov 248($context),%rbx # pull context->Rip
4732 mov 8($disp),%rsi # disp->ImageBase
4733 mov 56($disp),%r11 # disp->HandlerData
4735 mov 0(%r11),%r10d # HandlerData[0]
4736 lea (%rsi,%r10),%r10 # prologue lable
4737 cmp %r10,%rbx # context->Rip<prologue label
4738 jb .Lcommon_seh_tail
4740 mov 152($context),%rax # pull context->Rsp
4742 mov 4(%r11),%r10d # HandlerData[1]
4743 lea (%rsi,%r10),%r10 # epilogue label
4744 cmp %r10,%rbx # context->Rip>=epilogue label
4745 jae .Lcommon_seh_tail
4747 mov 160($context),%rax # pull context->Rbp
4748 lea -0xa0(%rax),%rsi # %xmm save area
4749 lea 512($context),%rdi # & context.Xmm6
4750 mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
4751 .long 0xa548f3fc # cld; rep movsq
4753 jmp .Lcommon_rbp_tail
4754 .size ctr_xts_se_handler,.-ctr_xts_se_handler
4756 .type ocb_se_handler,\@abi-omnipotent
4770 mov 120($context),%rax # pull context->Rax
4771 mov 248($context),%rbx # pull context->Rip
4773 mov 8($disp),%rsi # disp->ImageBase
4774 mov 56($disp),%r11 # disp->HandlerData
4776 mov 0(%r11),%r10d # HandlerData[0]
4777 lea (%rsi,%r10),%r10 # prologue lable
4778 cmp %r10,%rbx # context->Rip<prologue label
4779 jb .Lcommon_seh_tail
4781 mov 4(%r11),%r10d # HandlerData[1]
4782 lea (%rsi,%r10),%r10 # epilogue label
4783 cmp %r10,%rbx # context->Rip>=epilogue label
4784 jae .Lcommon_seh_tail
4786 mov 8(%r11),%r10d # HandlerData[2]
4787 lea (%rsi,%r10),%r10
4788 cmp %r10,%rbx # context->Rip>=pop label
4791 mov 152($context),%rax # pull context->Rsp
4793 lea (%rax),%rsi # %xmm save area
4794 lea 512($context),%rdi # & context.Xmm6
4795 mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
4796 .long 0xa548f3fc # cld; rep movsq
4797 lea 0xa0+0x28(%rax),%rax
4806 mov %rbx,144($context) # restore context->Rbx
4807 mov %rbp,160($context) # restore context->Rbp
4808 mov %r12,216($context) # restore context->R12
4809 mov %r13,224($context) # restore context->R13
4810 mov %r14,232($context) # restore context->R14
4812 jmp .Lcommon_seh_tail
4813 .size ocb_se_handler,.-ocb_se_handler
4816 .type cbc_se_handler,\@abi-omnipotent
4830 mov 152($context),%rax # pull context->Rsp
4831 mov 248($context),%rbx # pull context->Rip
4833 lea .Lcbc_decrypt_bulk(%rip),%r10
4834 cmp %r10,%rbx # context->Rip<"prologue" label
4835 jb .Lcommon_seh_tail
4837 lea .Lcbc_decrypt_body(%rip),%r10
4838 cmp %r10,%rbx # context->Rip<cbc_decrypt_body
4839 jb .Lrestore_cbc_rax
4841 lea .Lcbc_ret(%rip),%r10
4842 cmp %r10,%rbx # context->Rip>="epilogue" label
4843 jae .Lcommon_seh_tail
4845 lea 16(%rax),%rsi # %xmm save area
4846 lea 512($context),%rdi # &context.Xmm6
4847 mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
4848 .long 0xa548f3fc # cld; rep movsq
4851 mov 160($context),%rax # pull context->Rbp
4852 mov (%rax),%rbp # restore saved %rbp
4853 lea 8(%rax),%rax # adjust stack pointer
4854 mov %rbp,160($context) # restore context->Rbp
4855 jmp .Lcommon_seh_tail
4858 mov 120($context),%rax
4863 mov %rax,152($context) # restore context->Rsp
4864 mov %rsi,168($context) # restore context->Rsi
4865 mov %rdi,176($context) # restore context->Rdi
4867 mov 40($disp),%rdi # disp->ContextRecord
4868 mov $context,%rsi # context
4869 mov \$154,%ecx # sizeof(CONTEXT)
4870 .long 0xa548f3fc # cld; rep movsq
4873 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
4874 mov 8(%rsi),%rdx # arg2, disp->ImageBase
4875 mov 0(%rsi),%r8 # arg3, disp->ControlPc
4876 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
4877 mov 40(%rsi),%r10 # disp->ContextRecord
4878 lea 56(%rsi),%r11 # &disp->HandlerData
4879 lea 24(%rsi),%r12 # &disp->EstablisherFrame
4880 mov %r10,32(%rsp) # arg5
4881 mov %r11,40(%rsp) # arg6
4882 mov %r12,48(%rsp) # arg7
4883 mov %rcx,56(%rsp) # arg8, (NULL)
4884 call *__imp_RtlVirtualUnwind(%rip)
4886 mov \$1,%eax # ExceptionContinueSearch
4898 .size cbc_se_handler,.-cbc_se_handler
4903 $code.=<<___ if ($PREFIX eq "aesni");
4904 .rva .LSEH_begin_aesni_ecb_encrypt
4905 .rva .LSEH_end_aesni_ecb_encrypt
4908 .rva .LSEH_begin_aesni_ccm64_encrypt_blocks
4909 .rva .LSEH_end_aesni_ccm64_encrypt_blocks
4910 .rva .LSEH_info_ccm64_enc
4912 .rva .LSEH_begin_aesni_ccm64_decrypt_blocks
4913 .rva .LSEH_end_aesni_ccm64_decrypt_blocks
4914 .rva .LSEH_info_ccm64_dec
4916 .rva .LSEH_begin_aesni_ctr32_encrypt_blocks
4917 .rva .LSEH_end_aesni_ctr32_encrypt_blocks
4918 .rva .LSEH_info_ctr32
4920 .rva .LSEH_begin_aesni_xts_encrypt
4921 .rva .LSEH_end_aesni_xts_encrypt
4922 .rva .LSEH_info_xts_enc
4924 .rva .LSEH_begin_aesni_xts_decrypt
4925 .rva .LSEH_end_aesni_xts_decrypt
4926 .rva .LSEH_info_xts_dec
4928 .rva .LSEH_begin_aesni_ocb_encrypt
4929 .rva .LSEH_end_aesni_ocb_encrypt
4930 .rva .LSEH_info_ocb_enc
4932 .rva .LSEH_begin_aesni_ocb_decrypt
4933 .rva .LSEH_end_aesni_ocb_decrypt
4934 .rva .LSEH_info_ocb_dec
4937 .rva .LSEH_begin_${PREFIX}_cbc_encrypt
4938 .rva .LSEH_end_${PREFIX}_cbc_encrypt
4941 .rva ${PREFIX}_set_decrypt_key
4942 .rva .LSEH_end_set_decrypt_key
4945 .rva ${PREFIX}_set_encrypt_key
4946 .rva .LSEH_end_set_encrypt_key
4951 $code.=<<___ if ($PREFIX eq "aesni");
4954 .rva ecb_ccm64_se_handler
4955 .rva .Lecb_enc_body,.Lecb_enc_ret # HandlerData[]
4956 .LSEH_info_ccm64_enc:
4958 .rva ecb_ccm64_se_handler
4959 .rva .Lccm64_enc_body,.Lccm64_enc_ret # HandlerData[]
4960 .LSEH_info_ccm64_dec:
4962 .rva ecb_ccm64_se_handler
4963 .rva .Lccm64_dec_body,.Lccm64_dec_ret # HandlerData[]
4966 .rva ctr_xts_se_handler
4967 .rva .Lctr32_body,.Lctr32_epilogue # HandlerData[]
4970 .rva ctr_xts_se_handler
4971 .rva .Lxts_enc_body,.Lxts_enc_epilogue # HandlerData[]
4974 .rva ctr_xts_se_handler
4975 .rva .Lxts_dec_body,.Lxts_dec_epilogue # HandlerData[]
4979 .rva .Locb_enc_body,.Locb_enc_epilogue # HandlerData[]
4985 .rva .Locb_dec_body,.Locb_dec_epilogue # HandlerData[]
4994 .byte 0x01,0x04,0x01,0x00
4995 .byte 0x04,0x02,0x00,0x00 # sub rsp,8
5000 local *opcode=shift;
5004 $rex|=0x04 if($dst>=8);
5005 $rex|=0x01 if($src>=8);
5006 push @opcode,$rex|0x40 if($rex);
5013 if ($line=~/(aeskeygenassist)\s+\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
5014 rex(\@opcode,$4,$3);
5015 push @opcode,0x0f,0x3a,0xdf;
5016 push @opcode,0xc0|($3&7)|(($4&7)<<3); # ModR/M
5018 push @opcode,$c=~/^0/?oct($c):$c;
5019 return ".byte\t".join(',',@opcode);
5021 elsif ($line=~/(aes[a-z]+)\s+%xmm([0-9]+),\s*%xmm([0-9]+)/) {
5024 "aesenc" => 0xdc, "aesenclast" => 0xdd,
5025 "aesdec" => 0xde, "aesdeclast" => 0xdf
5027 return undef if (!defined($opcodelet{$1}));
5028 rex(\@opcode,$3,$2);
5029 push @opcode,0x0f,0x38,$opcodelet{$1};
5030 push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
5031 return ".byte\t".join(',',@opcode);
5033 elsif ($line=~/(aes[a-z]+)\s+([0x1-9a-fA-F]*)\(%rsp\),\s*%xmm([0-9]+)/) {
5035 "aesenc" => 0xdc, "aesenclast" => 0xdd,
5036 "aesdec" => 0xde, "aesdeclast" => 0xdf
5038 return undef if (!defined($opcodelet{$1}));
5040 push @opcode,0x44 if ($3>=8);
5041 push @opcode,0x0f,0x38,$opcodelet{$1};
5042 push @opcode,0x44|(($3&7)<<3),0x24; # ModR/M
5043 push @opcode,($off=~/^0/?oct($off):$off)&0xff;
5044 return ".byte\t".join(',',@opcode);
5050 ".byte 0x0f,0x38,0xf1,0x44,0x24,".shift;
5053 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
5054 $code =~ s/\b(aes.*%xmm[0-9]+).*$/aesni($1)/gem;
5055 #$code =~ s/\bmovbe\s+%eax/bswap %eax; mov %eax/gm; # debugging artefact
5056 $code =~ s/\bmovbe\s+%eax,\s*([0-9]+)\(%rsp\)/movbe($1)/gem;