3 # ====================================================================
4 # Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
5 # project. The module is, however, dual licensed under OpenSSL and
6 # CRYPTOGAMS licenses depending on where you obtain it. For further
7 # details see http://www.openssl.org/~appro/cryptogams/.
8 # ====================================================================
10 # This module implements support for Intel AES-NI extension. In
11 # OpenSSL context it's used with Intel engine, but can also be used as
12 # drop-in replacement for crypto/aes/asm/aes-x86_64.pl [see below for
17 # Given aes(enc|dec) instructions' latency asymptotic performance for
18 # non-parallelizable modes such as CBC encrypt is 3.75 cycles per byte
19 # processed with 128-bit key. And given their throughput asymptotic
20 # performance for parallelizable modes is 1.25 cycles per byte. Being
21 # asymptotic limit it's not something you commonly achieve in reality,
22 # but how close does one get? Below are results collected for
23 # different modes and block sized. Pairs of numbers are for en-/
26 # 16-byte 64-byte 256-byte 1-KB 8-KB
27 # ECB 4.25/4.25 1.38/1.38 1.28/1.28 1.26/1.26 1.26/1.26
28 # CTR 5.42/5.42 1.92/1.92 1.44/1.44 1.28/1.28 1.26/1.26
29 # CBC 4.38/4.43 4.15/1.43 4.07/1.32 4.07/1.29 4.06/1.28
30 # CCM 5.66/9.42 4.42/5.41 4.16/4.40 4.09/4.15 4.06/4.07
31 # OFB 5.42/5.42 4.64/4.64 4.44/4.44 4.39/4.39 4.38/4.38
32 # CFB 5.73/5.85 5.56/5.62 5.48/5.56 5.47/5.55 5.47/5.55
34 # ECB, CTR, CBC and CCM results are free from EVP overhead. This means
35 # that otherwise used 'openssl speed -evp aes-128-??? -engine aesni
36 # [-decrypt]' will exhibit 10-15% worse results for smaller blocks.
37 # The results were collected with specially crafted speed.c benchmark
38 # in order to compare them with results reported in "Intel Advanced
39 # Encryption Standard (AES) New Instruction Set" White Paper Revision
40 # 3.0 dated May 2010. All above results are consistently better. This
41 # module also provides better performance for block sizes smaller than
42 # 128 bytes in points *not* represented in the above table.
44 # Looking at the results for 8-KB buffer.
46 # CFB and OFB results are far from the limit, because implementation
47 # uses "generic" CRYPTO_[c|o]fb128_encrypt interfaces relying on
48 # single-block aesni_encrypt, which is not the most optimal way to go.
49 # CBC encrypt result is unexpectedly high and there is no documented
50 # explanation for it. Seemingly there is a small penalty for feeding
51 # the result back to AES unit the way it's done in CBC mode. There is
52 # nothing one can do and the result appears optimal. CCM result is
53 # identical to CBC, because CBC-MAC is essentially CBC encrypt without
54 # saving output. CCM CTR "stays invisible," because it's neatly
55 # interleaved wih CBC-MAC. This provides ~30% improvement over
56 # "straghtforward" CCM implementation with CTR and CBC-MAC performed
57 # disjointly. Parallelizable modes practically achieve the theoretical
60 # Looking at how results vary with buffer size.
62 # Curves are practically saturated at 1-KB buffer size. In most cases
63 # "256-byte" performance is >95%, and "64-byte" is ~90% of "8-KB" one.
64 # CTR curve doesn't follow this pattern and is "slowest" changing one
65 # with "256-byte" result being 87% of "8-KB." This is because overhead
66 # in CTR mode is most computationally intensive. Small-block CCM
67 # decrypt is slower than encrypt, because first CTR and last CBC-MAC
68 # iterations can't be interleaved.
70 # Results for 192- and 256-bit keys.
72 # EVP-free results were observed to scale perfectly with number of
73 # rounds for larger block sizes, i.e. 192-bit result being 10/12 times
74 # lower and 256-bit one - 10/14. Well, in CBC encrypt case differences
75 # are a tad smaller, because the above mentioned penalty biases all
76 # results by same constant value. In similar way function call
77 # overhead affects small-block performance, as well as OFB and CFB
78 # results. Differences are not large, most common coefficients are
79 # 10/11.7 and 10/13.4 (as opposite to 10/12.0 and 10/14.0), but one
80 # observe even 10/11.2 and 10/12.4 (CTR, OFB, CFB)...
84 # While Westmere processor features 6 cycles latency for aes[enc|dec]
85 # instructions, which can be scheduled every second cycle, Sandy
86 # Bridge spends 8 cycles per instruction, but it can schedule them
87 # every cycle. This means that code targeting Westmere would perform
88 # suboptimally on Sandy Bridge. Therefore this update.
90 # In addition, non-parallelizable CBC encrypt (as well as CCM) is
91 # optimized. Relative improvement might appear modest, 8% on Westmere,
92 # but in absolute terms it's 3.77 cycles per byte encrypted with
93 # 128-bit key on Westmere, and 5.07 - on Sandy Bridge. These numbers
94 # should be compared to asymptotic limits of 3.75 for Westmere and
95 # 5.00 for Sandy Bridge. Actually, the fact that they get this close
96 # to asymptotic limits is quite amazing. Indeed, the limit is
97 # calculated as latency times number of rounds, 10 for 128-bit key,
98 # and divided by 16, the number of bytes in block, or in other words
99 # it accounts *solely* for aesenc instructions. But there are extra
100 # instructions, and numbers so close to the asymptotic limits mean
101 # that it's as if it takes as little as *one* additional cycle to
102 # execute all of them. How is it possible? It is possible thanks to
103 # out-of-order execution logic, which manages to overlap post-
104 # processing of previous block, things like saving the output, with
105 # actual encryption of current block, as well as pre-processing of
106 # current block, things like fetching input and xor-ing it with
107 # 0-round element of the key schedule, with actual encryption of
108 # previous block. Keep this in mind...
110 # For parallelizable modes, such as ECB, CBC decrypt, CTR, higher
111 # performance is achieved by interleaving instructions working on
112 # independent blocks. In which case asymptotic limit for such modes
113 # can be obtained by dividing above mentioned numbers by AES
114 # instructions' interleave factor. Westmere can execute at most 3
115 # instructions at a time, meaning that optimal interleave factor is 3,
116 # and that's where the "magic" number of 1.25 come from. "Optimal
117 # interleave factor" means that increase of interleave factor does
118 # not improve performance. The formula has proven to reflect reality
119 # pretty well on Westmere... Sandy Bridge on the other hand can
120 # execute up to 8 AES instructions at a time, so how does varying
121 # interleave factor affect the performance? Here is table for ECB
122 # (numbers are cycles per byte processed with 128-bit key):
124 # instruction interleave factor 3x 6x 8x
125 # theoretical asymptotic limit 1.67 0.83 0.625
126 # measured performance for 8KB block 1.05 0.86 0.84
128 # "as if" interleave factor 4.7x 5.8x 6.0x
130 # Further data for other parallelizable modes:
132 # CBC decrypt 1.16 0.93 0.93
135 # Well, given 3x column it's probably inappropriate to call the limit
136 # asymptotic, if it can be surpassed, isn't it? What happens there?
137 # Rewind to CBC paragraph for the answer. Yes, out-of-order execution
138 # magic is responsible for this. Processor overlaps not only the
139 # additional instructions with AES ones, but even AES instuctions
140 # processing adjacent triplets of independent blocks. In the 6x case
141 # additional instructions still claim disproportionally small amount
142 # of additional cycles, but in 8x case number of instructions must be
143 # a tad too high for out-of-order logic to cope with, and AES unit
144 # remains underutilized... As you can see 8x interleave is hardly
145 # justifiable, so there no need to feel bad that 32-bit aesni-x86.pl
146 # utilizies 6x interleave because of limited register bank capacity.
148 # Higher interleave factors do have negative impact on Westmere
149 # performance. While for ECB mode it's negligible ~1.5%, other
150 # parallelizables perform ~5% worse, which is outweighed by ~25%
151 # improvement on Sandy Bridge. To balance regression on Westmere
152 # CTR mode was implemented with 6x aesenc interleave factor.
156 # Add aesni_xts_[en|de]crypt. Westmere spends 1.33 cycles processing
157 # one byte out of 8KB with 128-bit key, Sandy Bridge - 0.97. Just like
158 # in CTR mode AES instruction interleave factor was chosen to be 6x.
160 $PREFIX="aesni"; # if $PREFIX is set to "AES", the script
161 # generates drop-in replacement for
162 # crypto/aes/asm/aes-x86_64.pl:-)
166 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
168 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
170 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
171 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
172 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
173 die "can't locate x86_64-xlate.pl";
175 open STDOUT,"| $^X $xlate $flavour $output";
177 $movkey = $PREFIX eq "aesni" ? "movups" : "movups";
178 @_4args=$win64? ("%rcx","%rdx","%r8", "%r9") : # Win64 order
179 ("%rdi","%rsi","%rdx","%rcx"); # Unix order
183 $rounds="%eax"; # input to and changed by aesni_[en|de]cryptN !!!
184 # this is natural Unix argument order for public $PREFIX_[ecb|cbc]_encrypt ...
188 $key="%rcx"; # input to and changed by aesni_[en|de]cryptN !!!
189 $ivp="%r8"; # cbc, ctr, ...
191 $rnds_="%r10d"; # backup copy for $rounds
192 $key_="%r11"; # backup copy for $key
194 # %xmm register layout
195 $rndkey0="%xmm0"; $rndkey1="%xmm1";
196 $inout0="%xmm2"; $inout1="%xmm3";
197 $inout2="%xmm4"; $inout3="%xmm5";
198 $inout4="%xmm6"; $inout5="%xmm7";
199 $inout6="%xmm8"; $inout7="%xmm9";
201 $in2="%xmm6"; $in1="%xmm7"; # used in CBC decrypt, CTR, ...
202 $in0="%xmm8"; $iv="%xmm9";
204 # Inline version of internal aesni_[en|de]crypt1.
206 # Why folded loop? Because aes[enc|dec] is slow enough to accommodate
207 # cycles which take care of loop variables...
209 sub aesni_generate1 {
210 my ($p,$key,$rounds,$inout,$ivec)=@_; $inout=$inout0 if (!defined($inout));
213 $movkey ($key),$rndkey0
214 $movkey 16($key),$rndkey1
216 $code.=<<___ if (defined($ivec));
221 $code.=<<___ if (!defined($ivec));
223 xorps $rndkey0,$inout
227 aes${p} $rndkey1,$inout
229 $movkey ($key),$rndkey1
231 jnz .Loop_${p}1_$sn # loop body is 16 bytes
232 aes${p}last $rndkey1,$inout
235 # void $PREFIX_[en|de]crypt (const void *inp,void *out,const AES_KEY *key);
237 { my ($inp,$out,$key) = @_4args;
240 .globl ${PREFIX}_encrypt
241 .type ${PREFIX}_encrypt,\@abi-omnipotent
244 movups ($inp),$inout0 # load input
245 mov 240($key),$rounds # key->rounds
247 &aesni_generate1("enc",$key,$rounds);
249 movups $inout0,($out) # output
251 .size ${PREFIX}_encrypt,.-${PREFIX}_encrypt
253 .globl ${PREFIX}_decrypt
254 .type ${PREFIX}_decrypt,\@abi-omnipotent
257 movups ($inp),$inout0 # load input
258 mov 240($key),$rounds # key->rounds
260 &aesni_generate1("dec",$key,$rounds);
262 movups $inout0,($out) # output
264 .size ${PREFIX}_decrypt, .-${PREFIX}_decrypt
268 # _aesni_[en|de]cryptN are private interfaces, N denotes interleave
269 # factor. Why 3x subroutine were originally used in loops? Even though
270 # aes[enc|dec] latency was originally 6, it could be scheduled only
271 # every *2nd* cycle. Thus 3x interleave was the one providing optimal
272 # utilization, i.e. when subroutine's throughput is virtually same as
273 # of non-interleaved subroutine [for number of input blocks up to 3].
274 # This is why it makes no sense to implement 2x subroutine.
275 # aes[enc|dec] latency in next processor generation is 8, but the
276 # instructions can be scheduled every cycle. Optimal interleave for
277 # new processor is therefore 8x...
278 sub aesni_generate3 {
280 # As already mentioned it takes in $key and $rounds, which are *not*
281 # preserved. $inout[0-2] is cipher/clear text...
283 .type _aesni_${dir}rypt3,\@abi-omnipotent
286 $movkey ($key),$rndkey0
288 $movkey 16($key),$rndkey1
290 xorps $rndkey0,$inout0
291 xorps $rndkey0,$inout1
292 xorps $rndkey0,$inout2
293 $movkey ($key),$rndkey0
296 aes${dir} $rndkey1,$inout0
297 aes${dir} $rndkey1,$inout1
299 aes${dir} $rndkey1,$inout2
300 $movkey 16($key),$rndkey1
301 aes${dir} $rndkey0,$inout0
302 aes${dir} $rndkey0,$inout1
304 aes${dir} $rndkey0,$inout2
305 $movkey ($key),$rndkey0
308 aes${dir} $rndkey1,$inout0
309 aes${dir} $rndkey1,$inout1
310 aes${dir} $rndkey1,$inout2
311 aes${dir}last $rndkey0,$inout0
312 aes${dir}last $rndkey0,$inout1
313 aes${dir}last $rndkey0,$inout2
315 .size _aesni_${dir}rypt3,.-_aesni_${dir}rypt3
318 # 4x interleave is implemented to improve small block performance,
319 # most notably [and naturally] 4 block by ~30%. One can argue that one
320 # should have implemented 5x as well, but improvement would be <20%,
321 # so it's not worth it...
322 sub aesni_generate4 {
324 # As already mentioned it takes in $key and $rounds, which are *not*
325 # preserved. $inout[0-3] is cipher/clear text...
327 .type _aesni_${dir}rypt4,\@abi-omnipotent
330 $movkey ($key),$rndkey0
332 $movkey 16($key),$rndkey1
334 xorps $rndkey0,$inout0
335 xorps $rndkey0,$inout1
336 xorps $rndkey0,$inout2
337 xorps $rndkey0,$inout3
338 $movkey ($key),$rndkey0
341 aes${dir} $rndkey1,$inout0
342 aes${dir} $rndkey1,$inout1
344 aes${dir} $rndkey1,$inout2
345 aes${dir} $rndkey1,$inout3
346 $movkey 16($key),$rndkey1
347 aes${dir} $rndkey0,$inout0
348 aes${dir} $rndkey0,$inout1
350 aes${dir} $rndkey0,$inout2
351 aes${dir} $rndkey0,$inout3
352 $movkey ($key),$rndkey0
355 aes${dir} $rndkey1,$inout0
356 aes${dir} $rndkey1,$inout1
357 aes${dir} $rndkey1,$inout2
358 aes${dir} $rndkey1,$inout3
359 aes${dir}last $rndkey0,$inout0
360 aes${dir}last $rndkey0,$inout1
361 aes${dir}last $rndkey0,$inout2
362 aes${dir}last $rndkey0,$inout3
364 .size _aesni_${dir}rypt4,.-_aesni_${dir}rypt4
367 sub aesni_generate6 {
369 # As already mentioned it takes in $key and $rounds, which are *not*
370 # preserved. $inout[0-5] is cipher/clear text...
372 .type _aesni_${dir}rypt6,\@abi-omnipotent
375 $movkey ($key),$rndkey0
377 $movkey 16($key),$rndkey1
379 xorps $rndkey0,$inout0
380 pxor $rndkey0,$inout1
381 aes${dir} $rndkey1,$inout0
382 pxor $rndkey0,$inout2
383 aes${dir} $rndkey1,$inout1
384 pxor $rndkey0,$inout3
385 aes${dir} $rndkey1,$inout2
386 pxor $rndkey0,$inout4
387 aes${dir} $rndkey1,$inout3
388 pxor $rndkey0,$inout5
390 aes${dir} $rndkey1,$inout4
391 $movkey ($key),$rndkey0
392 aes${dir} $rndkey1,$inout5
393 jmp .L${dir}_loop6_enter
396 aes${dir} $rndkey1,$inout0
397 aes${dir} $rndkey1,$inout1
399 aes${dir} $rndkey1,$inout2
400 aes${dir} $rndkey1,$inout3
401 aes${dir} $rndkey1,$inout4
402 aes${dir} $rndkey1,$inout5
403 .L${dir}_loop6_enter: # happens to be 16-byte aligned
404 $movkey 16($key),$rndkey1
405 aes${dir} $rndkey0,$inout0
406 aes${dir} $rndkey0,$inout1
408 aes${dir} $rndkey0,$inout2
409 aes${dir} $rndkey0,$inout3
410 aes${dir} $rndkey0,$inout4
411 aes${dir} $rndkey0,$inout5
412 $movkey ($key),$rndkey0
415 aes${dir} $rndkey1,$inout0
416 aes${dir} $rndkey1,$inout1
417 aes${dir} $rndkey1,$inout2
418 aes${dir} $rndkey1,$inout3
419 aes${dir} $rndkey1,$inout4
420 aes${dir} $rndkey1,$inout5
421 aes${dir}last $rndkey0,$inout0
422 aes${dir}last $rndkey0,$inout1
423 aes${dir}last $rndkey0,$inout2
424 aes${dir}last $rndkey0,$inout3
425 aes${dir}last $rndkey0,$inout4
426 aes${dir}last $rndkey0,$inout5
428 .size _aesni_${dir}rypt6,.-_aesni_${dir}rypt6
431 sub aesni_generate8 {
433 # As already mentioned it takes in $key and $rounds, which are *not*
434 # preserved. $inout[0-7] is cipher/clear text...
436 .type _aesni_${dir}rypt8,\@abi-omnipotent
439 $movkey ($key),$rndkey0
441 $movkey 16($key),$rndkey1
443 xorps $rndkey0,$inout0
444 xorps $rndkey0,$inout1
445 aes${dir} $rndkey1,$inout0
446 pxor $rndkey0,$inout2
447 aes${dir} $rndkey1,$inout1
448 pxor $rndkey0,$inout3
449 aes${dir} $rndkey1,$inout2
450 pxor $rndkey0,$inout4
451 aes${dir} $rndkey1,$inout3
452 pxor $rndkey0,$inout5
454 aes${dir} $rndkey1,$inout4
455 pxor $rndkey0,$inout6
456 aes${dir} $rndkey1,$inout5
457 pxor $rndkey0,$inout7
458 $movkey ($key),$rndkey0
459 aes${dir} $rndkey1,$inout6
460 aes${dir} $rndkey1,$inout7
461 $movkey 16($key),$rndkey1
462 jmp .L${dir}_loop8_enter
465 aes${dir} $rndkey1,$inout0
466 aes${dir} $rndkey1,$inout1
468 aes${dir} $rndkey1,$inout2
469 aes${dir} $rndkey1,$inout3
470 aes${dir} $rndkey1,$inout4
471 aes${dir} $rndkey1,$inout5
472 aes${dir} $rndkey1,$inout6
473 aes${dir} $rndkey1,$inout7
474 $movkey 16($key),$rndkey1
475 .L${dir}_loop8_enter: # happens to be 16-byte aligned
476 aes${dir} $rndkey0,$inout0
477 aes${dir} $rndkey0,$inout1
479 aes${dir} $rndkey0,$inout2
480 aes${dir} $rndkey0,$inout3
481 aes${dir} $rndkey0,$inout4
482 aes${dir} $rndkey0,$inout5
483 aes${dir} $rndkey0,$inout6
484 aes${dir} $rndkey0,$inout7
485 $movkey ($key),$rndkey0
488 aes${dir} $rndkey1,$inout0
489 aes${dir} $rndkey1,$inout1
490 aes${dir} $rndkey1,$inout2
491 aes${dir} $rndkey1,$inout3
492 aes${dir} $rndkey1,$inout4
493 aes${dir} $rndkey1,$inout5
494 aes${dir} $rndkey1,$inout6
495 aes${dir} $rndkey1,$inout7
496 aes${dir}last $rndkey0,$inout0
497 aes${dir}last $rndkey0,$inout1
498 aes${dir}last $rndkey0,$inout2
499 aes${dir}last $rndkey0,$inout3
500 aes${dir}last $rndkey0,$inout4
501 aes${dir}last $rndkey0,$inout5
502 aes${dir}last $rndkey0,$inout6
503 aes${dir}last $rndkey0,$inout7
505 .size _aesni_${dir}rypt8,.-_aesni_${dir}rypt8
508 &aesni_generate3("enc") if ($PREFIX eq "aesni");
509 &aesni_generate3("dec");
510 &aesni_generate4("enc") if ($PREFIX eq "aesni");
511 &aesni_generate4("dec");
512 &aesni_generate6("enc") if ($PREFIX eq "aesni");
513 &aesni_generate6("dec");
514 &aesni_generate8("enc") if ($PREFIX eq "aesni");
515 &aesni_generate8("dec");
517 if ($PREFIX eq "aesni") {
518 ########################################################################
519 # void aesni_ecb_encrypt (const void *in, void *out,
520 # size_t length, const AES_KEY *key,
523 .globl aesni_ecb_encrypt
524 .type aesni_ecb_encrypt,\@function,5
530 mov 240($key),$rounds # key->rounds
531 $movkey ($key),$rndkey0
532 mov $key,$key_ # backup $key
533 mov $rounds,$rnds_ # backup $rounds
534 test %r8d,%r8d # 5th argument
536 #--------------------------- ECB ENCRYPT ------------------------------#
540 movdqu ($inp),$inout0
541 movdqu 0x10($inp),$inout1
542 movdqu 0x20($inp),$inout2
543 movdqu 0x30($inp),$inout3
544 movdqu 0x40($inp),$inout4
545 movdqu 0x50($inp),$inout5
546 movdqu 0x60($inp),$inout6
547 movdqu 0x70($inp),$inout7
550 jmp .Lecb_enc_loop8_enter
553 movups $inout0,($out)
554 mov $key_,$key # restore $key
555 movdqu ($inp),$inout0
556 mov $rnds_,$rounds # restore $rounds
557 movups $inout1,0x10($out)
558 movdqu 0x10($inp),$inout1
559 movups $inout2,0x20($out)
560 movdqu 0x20($inp),$inout2
561 movups $inout3,0x30($out)
562 movdqu 0x30($inp),$inout3
563 movups $inout4,0x40($out)
564 movdqu 0x40($inp),$inout4
565 movups $inout5,0x50($out)
566 movdqu 0x50($inp),$inout5
567 movups $inout6,0x60($out)
568 movdqu 0x60($inp),$inout6
569 movups $inout7,0x70($out)
571 movdqu 0x70($inp),$inout7
573 .Lecb_enc_loop8_enter:
580 movups $inout0,($out)
581 mov $key_,$key # restore $key
582 movups $inout1,0x10($out)
583 mov $rnds_,$rounds # restore $rounds
584 movups $inout2,0x20($out)
585 movups $inout3,0x30($out)
586 movups $inout4,0x40($out)
587 movups $inout5,0x50($out)
588 movups $inout6,0x60($out)
589 movups $inout7,0x70($out)
595 movups ($inp),$inout0
598 movups 0x10($inp),$inout1
600 movups 0x20($inp),$inout2
603 movups 0x30($inp),$inout3
605 movups 0x40($inp),$inout4
608 movups 0x50($inp),$inout5
610 movdqu 0x60($inp),$inout6
612 movups $inout0,($out)
613 movups $inout1,0x10($out)
614 movups $inout2,0x20($out)
615 movups $inout3,0x30($out)
616 movups $inout4,0x40($out)
617 movups $inout5,0x50($out)
618 movups $inout6,0x60($out)
623 &aesni_generate1("enc",$key,$rounds);
625 movups $inout0,($out)
629 xorps $inout2,$inout2
631 movups $inout0,($out)
632 movups $inout1,0x10($out)
637 movups $inout0,($out)
638 movups $inout1,0x10($out)
639 movups $inout2,0x20($out)
644 movups $inout0,($out)
645 movups $inout1,0x10($out)
646 movups $inout2,0x20($out)
647 movups $inout3,0x30($out)
651 xorps $inout5,$inout5
653 movups $inout0,($out)
654 movups $inout1,0x10($out)
655 movups $inout2,0x20($out)
656 movups $inout3,0x30($out)
657 movups $inout4,0x40($out)
662 movups $inout0,($out)
663 movups $inout1,0x10($out)
664 movups $inout2,0x20($out)
665 movups $inout3,0x30($out)
666 movups $inout4,0x40($out)
667 movups $inout5,0x50($out)
669 \f#--------------------------- ECB DECRYPT ------------------------------#
675 movdqu ($inp),$inout0
676 movdqu 0x10($inp),$inout1
677 movdqu 0x20($inp),$inout2
678 movdqu 0x30($inp),$inout3
679 movdqu 0x40($inp),$inout4
680 movdqu 0x50($inp),$inout5
681 movdqu 0x60($inp),$inout6
682 movdqu 0x70($inp),$inout7
685 jmp .Lecb_dec_loop8_enter
688 movups $inout0,($out)
689 mov $key_,$key # restore $key
690 movdqu ($inp),$inout0
691 mov $rnds_,$rounds # restore $rounds
692 movups $inout1,0x10($out)
693 movdqu 0x10($inp),$inout1
694 movups $inout2,0x20($out)
695 movdqu 0x20($inp),$inout2
696 movups $inout3,0x30($out)
697 movdqu 0x30($inp),$inout3
698 movups $inout4,0x40($out)
699 movdqu 0x40($inp),$inout4
700 movups $inout5,0x50($out)
701 movdqu 0x50($inp),$inout5
702 movups $inout6,0x60($out)
703 movdqu 0x60($inp),$inout6
704 movups $inout7,0x70($out)
706 movdqu 0x70($inp),$inout7
708 .Lecb_dec_loop8_enter:
712 $movkey ($key_),$rndkey0
716 movups $inout0,($out)
717 mov $key_,$key # restore $key
718 movups $inout1,0x10($out)
719 mov $rnds_,$rounds # restore $rounds
720 movups $inout2,0x20($out)
721 movups $inout3,0x30($out)
722 movups $inout4,0x40($out)
723 movups $inout5,0x50($out)
724 movups $inout6,0x60($out)
725 movups $inout7,0x70($out)
731 movups ($inp),$inout0
734 movups 0x10($inp),$inout1
736 movups 0x20($inp),$inout2
739 movups 0x30($inp),$inout3
741 movups 0x40($inp),$inout4
744 movups 0x50($inp),$inout5
746 movups 0x60($inp),$inout6
747 $movkey ($key),$rndkey0
749 movups $inout0,($out)
750 movups $inout1,0x10($out)
751 movups $inout2,0x20($out)
752 movups $inout3,0x30($out)
753 movups $inout4,0x40($out)
754 movups $inout5,0x50($out)
755 movups $inout6,0x60($out)
760 &aesni_generate1("dec",$key,$rounds);
762 movups $inout0,($out)
766 xorps $inout2,$inout2
768 movups $inout0,($out)
769 movups $inout1,0x10($out)
774 movups $inout0,($out)
775 movups $inout1,0x10($out)
776 movups $inout2,0x20($out)
781 movups $inout0,($out)
782 movups $inout1,0x10($out)
783 movups $inout2,0x20($out)
784 movups $inout3,0x30($out)
788 xorps $inout5,$inout5
790 movups $inout0,($out)
791 movups $inout1,0x10($out)
792 movups $inout2,0x20($out)
793 movups $inout3,0x30($out)
794 movups $inout4,0x40($out)
799 movups $inout0,($out)
800 movups $inout1,0x10($out)
801 movups $inout2,0x20($out)
802 movups $inout3,0x30($out)
803 movups $inout4,0x40($out)
804 movups $inout5,0x50($out)
808 .size aesni_ecb_encrypt,.-aesni_ecb_encrypt
812 ######################################################################
813 # void aesni_ccm64_[en|de]crypt_blocks (const void *in, void *out,
814 # size_t blocks, const AES_KEY *key,
815 # const char *ivec,char *cmac);
817 # Handles only complete blocks, operates on 64-bit counter and
818 # does not update *ivec! Nor does it finalize CMAC value
819 # (see engine/eng_aesni.c for details)
822 my $cmac="%r9"; # 6th argument
824 my $increment="%xmm8";
825 my $bswap_mask="%xmm9";
828 .globl aesni_ccm64_encrypt_blocks
829 .type aesni_ccm64_encrypt_blocks,\@function,6
831 aesni_ccm64_encrypt_blocks:
833 $code.=<<___ if ($win64);
836 movaps %xmm7,0x10(%rsp)
837 movaps %xmm8,0x20(%rsp)
838 movaps %xmm9,0x30(%rsp)
843 movdqu ($cmac),$inout1
844 movdqa .Lincrement64(%rip),$increment
845 movdqa .Lbswap_mask(%rip),$bswap_mask
846 pshufb $bswap_mask,$iv # keep iv in reverse order
848 mov 240($key),$rounds # key->rounds
854 movups ($inp),$in0 # load inp
855 pshufb $bswap_mask,$inout0
859 $movkey ($key),$rndkey0
861 $movkey 16($key),$rndkey1
864 xorps $rndkey0,$inout0
865 xorps $inout1,$in0 # cmac^=inp
866 $movkey ($key),$rndkey0
869 aesenc $rndkey1,$inout0
871 aesenc $rndkey1,$inout1
872 $movkey 16($key),$rndkey1
873 aesenc $rndkey0,$inout0
875 aesenc $rndkey0,$inout1
876 $movkey 0($key),$rndkey0
877 jnz .Lccm64_enc2_loop
878 aesenc $rndkey1,$inout0
879 aesenc $rndkey1,$inout1
880 aesenclast $rndkey0,$inout0
881 aesenclast $rndkey0,$inout1
886 xorps $inout0,$in0 # inp ^= E(iv)
888 movups $in0,($out) # save output
890 jnz .Lccm64_enc_outer
892 movups $inout1,($cmac)
894 $code.=<<___ if ($win64);
896 movaps 0x10(%rsp),%xmm7
897 movaps 0x20(%rsp),%xmm8
898 movaps 0x30(%rsp),%xmm9
904 .size aesni_ccm64_encrypt_blocks,.-aesni_ccm64_encrypt_blocks
906 ######################################################################
908 .globl aesni_ccm64_decrypt_blocks
909 .type aesni_ccm64_decrypt_blocks,\@function,6
911 aesni_ccm64_decrypt_blocks:
913 $code.=<<___ if ($win64);
916 movaps %xmm7,0x10(%rsp)
917 movaps %xmm8,0x20(%rsp)
918 movaps %xmm9,0x30(%rsp)
923 movdqu ($cmac),$inout1
924 movdqa .Lincrement64(%rip),$increment
925 movdqa .Lbswap_mask(%rip),$bswap_mask
927 mov 240($key),$rounds # key->rounds
929 pshufb $bswap_mask,$iv # keep iv in reverse order
933 &aesni_generate1("enc",$key,$rounds);
937 movups ($inp),$in0 # load inp
941 pshufb $bswap_mask,$inout0
950 $movkey ($key),$rndkey0
952 $movkey 16($key),$rndkey1
955 xorps $rndkey0,$inout0
956 xorps $in0,$inout1 # cmac^=out
957 $movkey ($key),$rndkey0
960 aesenc $rndkey1,$inout0
962 aesenc $rndkey1,$inout1
963 $movkey 16($key),$rndkey1
964 aesenc $rndkey0,$inout0
966 aesenc $rndkey0,$inout1
967 $movkey 0($key),$rndkey0
968 jnz .Lccm64_dec2_loop
969 aesenc $rndkey1,$inout0
970 aesenc $rndkey1,$inout1
971 aesenclast $rndkey0,$inout0
972 jmp .Lccm64_dec_outer
977 &aesni_generate1("enc",$key,$rounds,$inout1);
979 movups $inout1,($cmac)
981 $code.=<<___ if ($win64);
983 movaps 0x10(%rsp),%xmm7
984 movaps 0x20(%rsp),%xmm8
985 movaps 0x30(%rsp),%xmm9
991 .size aesni_ccm64_decrypt_blocks,.-aesni_ccm64_decrypt_blocks
994 ######################################################################
995 # void aesni_ctr32_encrypt_blocks (const void *in, void *out,
996 # size_t blocks, const AES_KEY *key,
999 # Handles only complete blocks, operates on 32-bit counter and
1000 # does not update *ivec! (see engine/eng_aesni.c for details)
1003 my $reserved = $win64?0:-0x28;
1004 my ($in0,$in1,$in2,$in3)=map("%xmm$_",(8..11));
1005 my ($iv0,$iv1,$ivec)=("%xmm12","%xmm13","%xmm14");
1006 my $bswap_mask="%xmm15";
1009 .globl aesni_ctr32_encrypt_blocks
1010 .type aesni_ctr32_encrypt_blocks,\@function,5
1012 aesni_ctr32_encrypt_blocks:
1014 $code.=<<___ if ($win64);
1015 lea -0xc8(%rsp),%rsp
1016 movaps %xmm6,0x20(%rsp)
1017 movaps %xmm7,0x30(%rsp)
1018 movaps %xmm8,0x40(%rsp)
1019 movaps %xmm9,0x50(%rsp)
1020 movaps %xmm10,0x60(%rsp)
1021 movaps %xmm11,0x70(%rsp)
1022 movaps %xmm12,0x80(%rsp)
1023 movaps %xmm13,0x90(%rsp)
1024 movaps %xmm14,0xa0(%rsp)
1025 movaps %xmm15,0xb0(%rsp)
1030 je .Lctr32_one_shortcut
1033 movdqa .Lbswap_mask(%rip),$bswap_mask
1035 pextrd \$3,$ivec,$rnds_ # pull 32-bit counter
1036 pinsrd \$3,$rounds,$ivec # wipe 32-bit counter
1038 mov 240($key),$rounds # key->rounds
1040 pxor $iv0,$iv0 # vector of 3 32-bit counters
1041 pxor $iv1,$iv1 # vector of 3 32-bit counters
1042 pinsrd \$0,$rnds_,$iv0
1044 pinsrd \$0,$key_,$iv1
1046 pinsrd \$1,$rnds_,$iv0
1048 pinsrd \$1,$key_,$iv1
1050 pinsrd \$2,$rnds_,$iv0
1052 pinsrd \$2,$key_,$iv1
1053 movdqa $iv0,$reserved(%rsp)
1054 pshufb $bswap_mask,$iv0
1055 movdqa $iv1,`$reserved+0x10`(%rsp)
1056 pshufb $bswap_mask,$iv1
1058 pshufd \$`3<<6`,$iv0,$inout0 # place counter to upper dword
1059 pshufd \$`2<<6`,$iv0,$inout1
1060 pshufd \$`1<<6`,$iv0,$inout2
1064 mov $key,$key_ # backup $key
1065 mov $rounds,$rnds_ # backup $rounds
1071 pshufd \$`3<<6`,$iv1,$inout3
1072 por $ivec,$inout0 # merge counter-less ivec
1073 $movkey ($key_),$rndkey0
1074 pshufd \$`2<<6`,$iv1,$inout4
1076 $movkey 16($key_),$rndkey1
1077 pshufd \$`1<<6`,$iv1,$inout5
1080 xorps $rndkey0,$inout0
1084 # inline _aesni_encrypt6 and interleave last rounds
1087 pxor $rndkey0,$inout1
1088 aesenc $rndkey1,$inout0
1090 pxor $rndkey0,$inout2
1091 aesenc $rndkey1,$inout1
1092 movdqa .Lincrement32(%rip),$iv1
1093 pxor $rndkey0,$inout3
1094 aesenc $rndkey1,$inout2
1095 movdqa $reserved(%rsp),$iv0
1096 pxor $rndkey0,$inout4
1097 aesenc $rndkey1,$inout3
1098 pxor $rndkey0,$inout5
1099 $movkey ($key),$rndkey0
1101 aesenc $rndkey1,$inout4
1102 aesenc $rndkey1,$inout5
1103 jmp .Lctr32_enc_loop6_enter
1106 aesenc $rndkey1,$inout0
1107 aesenc $rndkey1,$inout1
1109 aesenc $rndkey1,$inout2
1110 aesenc $rndkey1,$inout3
1111 aesenc $rndkey1,$inout4
1112 aesenc $rndkey1,$inout5
1113 .Lctr32_enc_loop6_enter:
1114 $movkey 16($key),$rndkey1
1115 aesenc $rndkey0,$inout0
1116 aesenc $rndkey0,$inout1
1118 aesenc $rndkey0,$inout2
1119 aesenc $rndkey0,$inout3
1120 aesenc $rndkey0,$inout4
1121 aesenc $rndkey0,$inout5
1122 $movkey ($key),$rndkey0
1123 jnz .Lctr32_enc_loop6
1125 aesenc $rndkey1,$inout0
1126 paddd $iv1,$iv0 # increment counter vector
1127 aesenc $rndkey1,$inout1
1128 paddd `$reserved+0x10`(%rsp),$iv1
1129 aesenc $rndkey1,$inout2
1130 movdqa $iv0,$reserved(%rsp) # save counter vector
1131 aesenc $rndkey1,$inout3
1132 movdqa $iv1,`$reserved+0x10`(%rsp)
1133 aesenc $rndkey1,$inout4
1134 pshufb $bswap_mask,$iv0 # byte swap
1135 aesenc $rndkey1,$inout5
1136 pshufb $bswap_mask,$iv1
1138 aesenclast $rndkey0,$inout0
1139 movups ($inp),$in0 # load input
1140 aesenclast $rndkey0,$inout1
1141 movups 0x10($inp),$in1
1142 aesenclast $rndkey0,$inout2
1143 movups 0x20($inp),$in2
1144 aesenclast $rndkey0,$inout3
1145 movups 0x30($inp),$in3
1146 aesenclast $rndkey0,$inout4
1147 movups 0x40($inp),$rndkey1
1148 aesenclast $rndkey0,$inout5
1149 movups 0x50($inp),$rndkey0
1152 xorps $inout0,$in0 # xor
1153 pshufd \$`3<<6`,$iv0,$inout0
1155 pshufd \$`2<<6`,$iv0,$inout1
1156 movups $in0,($out) # store output
1158 pshufd \$`1<<6`,$iv0,$inout2
1159 movups $in1,0x10($out)
1161 movups $in2,0x20($out)
1162 xorps $inout4,$rndkey1
1163 movups $in3,0x30($out)
1164 xorps $inout5,$rndkey0
1165 movups $rndkey1,0x40($out)
1166 movups $rndkey0,0x50($out)
1174 mov $key_,$key # restore $key
1175 lea 1($rounds,$rounds),$rounds # restore original value
1184 movups 0x10($inp),$in1
1187 pshufd \$`3<<6`,$iv1,$inout3
1189 movups 0x20($inp),$in2
1193 pshufd \$`2<<6`,$iv1,$inout4
1195 movups 0x30($inp),$in3
1199 xorps $inout5,$inout5
1201 call _aesni_encrypt6
1203 movups 0x40($inp),$rndkey1
1208 movups $in1,0x10($out)
1210 movups $in2,0x20($out)
1211 xorps $inout4,$rndkey1
1212 movups $in3,0x30($out)
1213 movups $rndkey1,0x40($out)
1217 .Lctr32_one_shortcut:
1218 movups ($ivp),$inout0
1220 mov 240($key),$rounds # key->rounds
1223 &aesni_generate1("enc",$key,$rounds);
1231 xorps $inout2,$inout2
1232 call _aesni_encrypt3
1236 movups $in1,0x10($out)
1241 call _aesni_encrypt3
1246 movups $in1,0x10($out)
1247 movups $in2,0x20($out)
1252 call _aesni_encrypt4
1257 movups $in1,0x10($out)
1259 movups $in2,0x20($out)
1260 movups $in3,0x30($out)
1264 $code.=<<___ if ($win64);
1265 movaps 0x20(%rsp),%xmm6
1266 movaps 0x30(%rsp),%xmm7
1267 movaps 0x40(%rsp),%xmm8
1268 movaps 0x50(%rsp),%xmm9
1269 movaps 0x60(%rsp),%xmm10
1270 movaps 0x70(%rsp),%xmm11
1271 movaps 0x80(%rsp),%xmm12
1272 movaps 0x90(%rsp),%xmm13
1273 movaps 0xa0(%rsp),%xmm14
1274 movaps 0xb0(%rsp),%xmm15
1280 .size aesni_ctr32_encrypt_blocks,.-aesni_ctr32_encrypt_blocks
1284 ######################################################################
1285 # void aesni_xts_[en|de]crypt(const char *inp,char *out,size_t len,
1286 # const AES_KEY *key1, const AES_KEY *key2
1287 # const unsigned char iv[16]);
1290 my @tweak=map("%xmm$_",(10..15));
1291 my ($twmask,$twres,$twtmp)=("%xmm8","%xmm9",@tweak[4]);
1292 my ($key2,$ivp,$len_)=("%r8","%r9","%r9");
1293 my $frame_size = 0x68 + ($win64?160:0);
1296 .globl aesni_xts_encrypt
1297 .type aesni_xts_encrypt,\@function,6
1300 lea -$frame_size(%rsp),%rsp
1302 $code.=<<___ if ($win64);
1303 movaps %xmm6,0x60(%rsp)
1304 movaps %xmm7,0x70(%rsp)
1305 movaps %xmm8,0x80(%rsp)
1306 movaps %xmm9,0x90(%rsp)
1307 movaps %xmm10,0xa0(%rsp)
1308 movaps %xmm11,0xb0(%rsp)
1309 movaps %xmm12,0xc0(%rsp)
1310 movaps %xmm13,0xd0(%rsp)
1311 movaps %xmm14,0xe0(%rsp)
1312 movaps %xmm15,0xf0(%rsp)
1316 movups ($ivp),@tweak[5] # load clear-text tweak
1317 mov 240(%r8),$rounds # key2->rounds
1318 mov 240($key),$rnds_ # key1->rounds
1320 # generate the tweak
1321 &aesni_generate1("enc",$key2,$rounds,@tweak[5]);
1323 mov $key,$key_ # backup $key
1324 mov $rnds_,$rounds # backup $rounds
1325 mov $len,$len_ # backup $len
1328 movdqa .Lxts_magic(%rip),$twmask
1330 pcmpgtd @tweak[5],$twtmp # broadcast upper bits
1332 for ($i=0;$i<4;$i++) {
1334 pshufd \$0x13,$twtmp,$twres
1336 movdqa @tweak[5],@tweak[$i]
1337 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1338 pand $twmask,$twres # isolate carry and residue
1339 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1340 pxor $twres,@tweak[5]
1350 jmp .Lxts_enc_grandloop
1353 .Lxts_enc_grandloop:
1354 pshufd \$0x13,$twtmp,$twres
1355 movdqa @tweak[5],@tweak[4]
1356 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1357 movdqu `16*0`($inp),$inout0 # load input
1358 pand $twmask,$twres # isolate carry and residue
1359 movdqu `16*1`($inp),$inout1
1360 pxor $twres,@tweak[5]
1362 movdqu `16*2`($inp),$inout2
1363 pxor @tweak[0],$inout0 # input^=tweak
1364 movdqu `16*3`($inp),$inout3
1365 pxor @tweak[1],$inout1
1366 movdqu `16*4`($inp),$inout4
1367 pxor @tweak[2],$inout2
1368 movdqu `16*5`($inp),$inout5
1369 lea `16*6`($inp),$inp
1370 pxor @tweak[3],$inout3
1371 $movkey ($key_),$rndkey0
1372 pxor @tweak[4],$inout4
1373 pxor @tweak[5],$inout5
1375 # inline _aesni_encrypt6 and interleave first and last rounds
1377 $movkey 16($key_),$rndkey1
1378 pxor $rndkey0,$inout0
1379 pxor $rndkey0,$inout1
1380 movdqa @tweak[0],`16*0`(%rsp) # put aside tweaks
1381 aesenc $rndkey1,$inout0
1383 pxor $rndkey0,$inout2
1384 movdqa @tweak[1],`16*1`(%rsp)
1385 aesenc $rndkey1,$inout1
1386 pxor $rndkey0,$inout3
1387 movdqa @tweak[2],`16*2`(%rsp)
1388 aesenc $rndkey1,$inout2
1389 pxor $rndkey0,$inout4
1390 movdqa @tweak[3],`16*3`(%rsp)
1391 aesenc $rndkey1,$inout3
1392 pxor $rndkey0,$inout5
1393 $movkey ($key),$rndkey0
1395 movdqa @tweak[4],`16*4`(%rsp)
1396 aesenc $rndkey1,$inout4
1397 movdqa @tweak[5],`16*5`(%rsp)
1398 aesenc $rndkey1,$inout5
1400 pcmpgtd @tweak[5],$twtmp
1401 jmp .Lxts_enc_loop6_enter
1405 aesenc $rndkey1,$inout0
1406 aesenc $rndkey1,$inout1
1408 aesenc $rndkey1,$inout2
1409 aesenc $rndkey1,$inout3
1410 aesenc $rndkey1,$inout4
1411 aesenc $rndkey1,$inout5
1412 .Lxts_enc_loop6_enter:
1413 $movkey 16($key),$rndkey1
1414 aesenc $rndkey0,$inout0
1415 aesenc $rndkey0,$inout1
1417 aesenc $rndkey0,$inout2
1418 aesenc $rndkey0,$inout3
1419 aesenc $rndkey0,$inout4
1420 aesenc $rndkey0,$inout5
1421 $movkey ($key),$rndkey0
1424 pshufd \$0x13,$twtmp,$twres
1426 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1427 aesenc $rndkey1,$inout0
1428 pand $twmask,$twres # isolate carry and residue
1429 aesenc $rndkey1,$inout1
1430 pcmpgtd @tweak[5],$twtmp # broadcast upper bits
1431 aesenc $rndkey1,$inout2
1432 pxor $twres,@tweak[5]
1433 aesenc $rndkey1,$inout3
1434 aesenc $rndkey1,$inout4
1435 aesenc $rndkey1,$inout5
1436 $movkey 16($key),$rndkey1
1438 pshufd \$0x13,$twtmp,$twres
1440 movdqa @tweak[5],@tweak[0]
1441 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1442 aesenc $rndkey0,$inout0
1443 pand $twmask,$twres # isolate carry and residue
1444 aesenc $rndkey0,$inout1
1445 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1446 aesenc $rndkey0,$inout2
1447 pxor $twres,@tweak[5]
1448 aesenc $rndkey0,$inout3
1449 aesenc $rndkey0,$inout4
1450 aesenc $rndkey0,$inout5
1451 $movkey 32($key),$rndkey0
1453 pshufd \$0x13,$twtmp,$twres
1455 movdqa @tweak[5],@tweak[1]
1456 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1457 aesenc $rndkey1,$inout0
1458 pand $twmask,$twres # isolate carry and residue
1459 aesenc $rndkey1,$inout1
1460 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1461 aesenc $rndkey1,$inout2
1462 pxor $twres,@tweak[5]
1463 aesenc $rndkey1,$inout3
1464 aesenc $rndkey1,$inout4
1465 aesenc $rndkey1,$inout5
1467 pshufd \$0x13,$twtmp,$twres
1469 movdqa @tweak[5],@tweak[2]
1470 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1471 aesenclast $rndkey0,$inout0
1472 pand $twmask,$twres # isolate carry and residue
1473 aesenclast $rndkey0,$inout1
1474 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1475 aesenclast $rndkey0,$inout2
1476 pxor $twres,@tweak[5]
1477 aesenclast $rndkey0,$inout3
1478 aesenclast $rndkey0,$inout4
1479 aesenclast $rndkey0,$inout5
1481 pshufd \$0x13,$twtmp,$twres
1483 movdqa @tweak[5],@tweak[3]
1484 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1485 xorps `16*0`(%rsp),$inout0 # output^=tweak
1486 pand $twmask,$twres # isolate carry and residue
1487 xorps `16*1`(%rsp),$inout1
1488 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1489 pxor $twres,@tweak[5]
1491 xorps `16*2`(%rsp),$inout2
1492 movups $inout0,`16*0`($out) # write output
1493 xorps `16*3`(%rsp),$inout3
1494 movups $inout1,`16*1`($out)
1495 xorps `16*4`(%rsp),$inout4
1496 movups $inout2,`16*2`($out)
1497 xorps `16*5`(%rsp),$inout5
1498 movups $inout3,`16*3`($out)
1499 mov $rnds_,$rounds # restore $rounds
1500 movups $inout4,`16*4`($out)
1501 movups $inout5,`16*5`($out)
1502 lea `16*6`($out),$out
1504 jnc .Lxts_enc_grandloop
1506 lea 3($rounds,$rounds),$rounds # restore original value
1507 mov $key_,$key # restore $key
1508 mov $rounds,$rnds_ # backup $rounds
1522 pshufd \$0x13,$twtmp,$twres
1523 movdqa @tweak[5],@tweak[4]
1524 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1525 movdqu ($inp),$inout0
1526 pand $twmask,$twres # isolate carry and residue
1527 movdqu 16*1($inp),$inout1
1528 pxor $twres,@tweak[5]
1530 movdqu 16*2($inp),$inout2
1531 pxor @tweak[0],$inout0
1532 movdqu 16*3($inp),$inout3
1533 pxor @tweak[1],$inout1
1534 movdqu 16*4($inp),$inout4
1536 pxor @tweak[2],$inout2
1537 pxor @tweak[3],$inout3
1538 pxor @tweak[4],$inout4
1540 call _aesni_encrypt6
1542 xorps @tweak[0],$inout0
1543 movdqa @tweak[5],@tweak[0]
1544 xorps @tweak[1],$inout1
1545 xorps @tweak[2],$inout2
1546 movdqu $inout0,($out)
1547 xorps @tweak[3],$inout3
1548 movdqu $inout1,16*1($out)
1549 xorps @tweak[4],$inout4
1550 movdqu $inout2,16*2($out)
1551 movdqu $inout3,16*3($out)
1552 movdqu $inout4,16*4($out)
1558 movups ($inp),$inout0
1560 xorps @tweak[0],$inout0
1562 &aesni_generate1("enc",$key,$rounds);
1564 xorps @tweak[0],$inout0
1565 movdqa @tweak[1],@tweak[0]
1566 movups $inout0,($out)
1572 movups ($inp),$inout0
1573 movups 16($inp),$inout1
1575 xorps @tweak[0],$inout0
1576 xorps @tweak[1],$inout1
1578 call _aesni_encrypt3
1580 xorps @tweak[0],$inout0
1581 movdqa @tweak[2],@tweak[0]
1582 xorps @tweak[1],$inout1
1583 movups $inout0,($out)
1584 movups $inout1,16*1($out)
1590 movups ($inp),$inout0
1591 movups 16*1($inp),$inout1
1592 movups 16*2($inp),$inout2
1594 xorps @tweak[0],$inout0
1595 xorps @tweak[1],$inout1
1596 xorps @tweak[2],$inout2
1598 call _aesni_encrypt3
1600 xorps @tweak[0],$inout0
1601 movdqa @tweak[3],@tweak[0]
1602 xorps @tweak[1],$inout1
1603 xorps @tweak[2],$inout2
1604 movups $inout0,($out)
1605 movups $inout1,16*1($out)
1606 movups $inout2,16*2($out)
1612 movups ($inp),$inout0
1613 movups 16*1($inp),$inout1
1614 movups 16*2($inp),$inout2
1615 xorps @tweak[0],$inout0
1616 movups 16*3($inp),$inout3
1618 xorps @tweak[1],$inout1
1619 xorps @tweak[2],$inout2
1620 xorps @tweak[3],$inout3
1622 call _aesni_encrypt4
1624 xorps @tweak[0],$inout0
1625 movdqa @tweak[5],@tweak[0]
1626 xorps @tweak[1],$inout1
1627 xorps @tweak[2],$inout2
1628 movups $inout0,($out)
1629 xorps @tweak[3],$inout3
1630 movups $inout1,16*1($out)
1631 movups $inout2,16*2($out)
1632 movups $inout3,16*3($out)
1643 movzb ($inp),%eax # borrow $rounds ...
1644 movzb -16($out),%ecx # ... and $key
1652 sub $len_,$out # rewind $out
1653 mov $key_,$key # restore $key
1654 mov $rnds_,$rounds # restore $rounds
1656 movups -16($out),$inout0
1657 xorps @tweak[0],$inout0
1659 &aesni_generate1("enc",$key,$rounds);
1661 xorps @tweak[0],$inout0
1662 movups $inout0,-16($out)
1666 $code.=<<___ if ($win64);
1667 movaps 0x60(%rsp),%xmm6
1668 movaps 0x70(%rsp),%xmm7
1669 movaps 0x80(%rsp),%xmm8
1670 movaps 0x90(%rsp),%xmm9
1671 movaps 0xa0(%rsp),%xmm10
1672 movaps 0xb0(%rsp),%xmm11
1673 movaps 0xc0(%rsp),%xmm12
1674 movaps 0xd0(%rsp),%xmm13
1675 movaps 0xe0(%rsp),%xmm14
1676 movaps 0xf0(%rsp),%xmm15
1679 lea $frame_size(%rsp),%rsp
1682 .size aesni_xts_encrypt,.-aesni_xts_encrypt
1686 .globl aesni_xts_decrypt
1687 .type aesni_xts_decrypt,\@function,6
1690 lea -$frame_size(%rsp),%rsp
1692 $code.=<<___ if ($win64);
1693 movaps %xmm6,0x60(%rsp)
1694 movaps %xmm7,0x70(%rsp)
1695 movaps %xmm8,0x80(%rsp)
1696 movaps %xmm9,0x90(%rsp)
1697 movaps %xmm10,0xa0(%rsp)
1698 movaps %xmm11,0xb0(%rsp)
1699 movaps %xmm12,0xc0(%rsp)
1700 movaps %xmm13,0xd0(%rsp)
1701 movaps %xmm14,0xe0(%rsp)
1702 movaps %xmm15,0xf0(%rsp)
1706 movups ($ivp),@tweak[5] # load clear-text tweak
1707 mov 240($key2),$rounds # key2->rounds
1708 mov 240($key),$rnds_ # key1->rounds
1710 # generate the tweak
1711 &aesni_generate1("enc",$key2,$rounds,@tweak[5]);
1713 xor %eax,%eax # if ($len%16) len-=16;
1719 mov $key,$key_ # backup $key
1720 mov $rnds_,$rounds # backup $rounds
1721 mov $len,$len_ # backup $len
1724 movdqa .Lxts_magic(%rip),$twmask
1726 pcmpgtd @tweak[5],$twtmp # broadcast upper bits
1728 for ($i=0;$i<4;$i++) {
1730 pshufd \$0x13,$twtmp,$twres
1732 movdqa @tweak[5],@tweak[$i]
1733 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1734 pand $twmask,$twres # isolate carry and residue
1735 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1736 pxor $twres,@tweak[5]
1746 jmp .Lxts_dec_grandloop
1749 .Lxts_dec_grandloop:
1750 pshufd \$0x13,$twtmp,$twres
1751 movdqa @tweak[5],@tweak[4]
1752 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1753 movdqu `16*0`($inp),$inout0 # load input
1754 pand $twmask,$twres # isolate carry and residue
1755 movdqu `16*1`($inp),$inout1
1756 pxor $twres,@tweak[5]
1758 movdqu `16*2`($inp),$inout2
1759 pxor @tweak[0],$inout0 # input^=tweak
1760 movdqu `16*3`($inp),$inout3
1761 pxor @tweak[1],$inout1
1762 movdqu `16*4`($inp),$inout4
1763 pxor @tweak[2],$inout2
1764 movdqu `16*5`($inp),$inout5
1765 lea `16*6`($inp),$inp
1766 pxor @tweak[3],$inout3
1767 $movkey ($key_),$rndkey0
1768 pxor @tweak[4],$inout4
1769 pxor @tweak[5],$inout5
1771 # inline _aesni_decrypt6 and interleave first and last rounds
1773 $movkey 16($key_),$rndkey1
1774 pxor $rndkey0,$inout0
1775 pxor $rndkey0,$inout1
1776 movdqa @tweak[0],`16*0`(%rsp) # put aside tweaks
1777 aesdec $rndkey1,$inout0
1779 pxor $rndkey0,$inout2
1780 movdqa @tweak[1],`16*1`(%rsp)
1781 aesdec $rndkey1,$inout1
1782 pxor $rndkey0,$inout3
1783 movdqa @tweak[2],`16*2`(%rsp)
1784 aesdec $rndkey1,$inout2
1785 pxor $rndkey0,$inout4
1786 movdqa @tweak[3],`16*3`(%rsp)
1787 aesdec $rndkey1,$inout3
1788 pxor $rndkey0,$inout5
1789 $movkey ($key),$rndkey0
1791 movdqa @tweak[4],`16*4`(%rsp)
1792 aesdec $rndkey1,$inout4
1793 movdqa @tweak[5],`16*5`(%rsp)
1794 aesdec $rndkey1,$inout5
1796 pcmpgtd @tweak[5],$twtmp
1797 jmp .Lxts_dec_loop6_enter
1801 aesdec $rndkey1,$inout0
1802 aesdec $rndkey1,$inout1
1804 aesdec $rndkey1,$inout2
1805 aesdec $rndkey1,$inout3
1806 aesdec $rndkey1,$inout4
1807 aesdec $rndkey1,$inout5
1808 .Lxts_dec_loop6_enter:
1809 $movkey 16($key),$rndkey1
1810 aesdec $rndkey0,$inout0
1811 aesdec $rndkey0,$inout1
1813 aesdec $rndkey0,$inout2
1814 aesdec $rndkey0,$inout3
1815 aesdec $rndkey0,$inout4
1816 aesdec $rndkey0,$inout5
1817 $movkey ($key),$rndkey0
1820 pshufd \$0x13,$twtmp,$twres
1822 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1823 aesdec $rndkey1,$inout0
1824 pand $twmask,$twres # isolate carry and residue
1825 aesdec $rndkey1,$inout1
1826 pcmpgtd @tweak[5],$twtmp # broadcast upper bits
1827 aesdec $rndkey1,$inout2
1828 pxor $twres,@tweak[5]
1829 aesdec $rndkey1,$inout3
1830 aesdec $rndkey1,$inout4
1831 aesdec $rndkey1,$inout5
1832 $movkey 16($key),$rndkey1
1834 pshufd \$0x13,$twtmp,$twres
1836 movdqa @tweak[5],@tweak[0]
1837 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1838 aesdec $rndkey0,$inout0
1839 pand $twmask,$twres # isolate carry and residue
1840 aesdec $rndkey0,$inout1
1841 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1842 aesdec $rndkey0,$inout2
1843 pxor $twres,@tweak[5]
1844 aesdec $rndkey0,$inout3
1845 aesdec $rndkey0,$inout4
1846 aesdec $rndkey0,$inout5
1847 $movkey 32($key),$rndkey0
1849 pshufd \$0x13,$twtmp,$twres
1851 movdqa @tweak[5],@tweak[1]
1852 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1853 aesdec $rndkey1,$inout0
1854 pand $twmask,$twres # isolate carry and residue
1855 aesdec $rndkey1,$inout1
1856 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1857 aesdec $rndkey1,$inout2
1858 pxor $twres,@tweak[5]
1859 aesdec $rndkey1,$inout3
1860 aesdec $rndkey1,$inout4
1861 aesdec $rndkey1,$inout5
1863 pshufd \$0x13,$twtmp,$twres
1865 movdqa @tweak[5],@tweak[2]
1866 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1867 aesdeclast $rndkey0,$inout0
1868 pand $twmask,$twres # isolate carry and residue
1869 aesdeclast $rndkey0,$inout1
1870 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1871 aesdeclast $rndkey0,$inout2
1872 pxor $twres,@tweak[5]
1873 aesdeclast $rndkey0,$inout3
1874 aesdeclast $rndkey0,$inout4
1875 aesdeclast $rndkey0,$inout5
1877 pshufd \$0x13,$twtmp,$twres
1879 movdqa @tweak[5],@tweak[3]
1880 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1881 xorps `16*0`(%rsp),$inout0 # output^=tweak
1882 pand $twmask,$twres # isolate carry and residue
1883 xorps `16*1`(%rsp),$inout1
1884 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1885 pxor $twres,@tweak[5]
1887 xorps `16*2`(%rsp),$inout2
1888 movups $inout0,`16*0`($out) # write output
1889 xorps `16*3`(%rsp),$inout3
1890 movups $inout1,`16*1`($out)
1891 xorps `16*4`(%rsp),$inout4
1892 movups $inout2,`16*2`($out)
1893 xorps `16*5`(%rsp),$inout5
1894 movups $inout3,`16*3`($out)
1895 mov $rnds_,$rounds # restore $rounds
1896 movups $inout4,`16*4`($out)
1897 movups $inout5,`16*5`($out)
1898 lea `16*6`($out),$out
1900 jnc .Lxts_dec_grandloop
1902 lea 3($rounds,$rounds),$rounds # restore original value
1903 mov $key_,$key # restore $key
1904 mov $rounds,$rnds_ # backup $rounds
1918 pshufd \$0x13,$twtmp,$twres
1919 movdqa @tweak[5],@tweak[4]
1920 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1921 movdqu ($inp),$inout0
1922 pand $twmask,$twres # isolate carry and residue
1923 movdqu 16*1($inp),$inout1
1924 pxor $twres,@tweak[5]
1926 movdqu 16*2($inp),$inout2
1927 pxor @tweak[0],$inout0
1928 movdqu 16*3($inp),$inout3
1929 pxor @tweak[1],$inout1
1930 movdqu 16*4($inp),$inout4
1932 pxor @tweak[2],$inout2
1933 pxor @tweak[3],$inout3
1934 pxor @tweak[4],$inout4
1936 call _aesni_decrypt6
1938 xorps @tweak[0],$inout0
1939 xorps @tweak[1],$inout1
1940 xorps @tweak[2],$inout2
1941 movdqu $inout0,($out)
1942 xorps @tweak[3],$inout3
1943 movdqu $inout1,16*1($out)
1944 xorps @tweak[4],$inout4
1945 movdqu $inout2,16*2($out)
1947 movdqu $inout3,16*3($out)
1948 pcmpgtd @tweak[5],$twtmp
1949 movdqu $inout4,16*4($out)
1951 pshufd \$0x13,$twtmp,@tweak[1] # $twres
1955 movdqa @tweak[5],@tweak[0]
1956 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1957 pand $twmask,@tweak[1] # isolate carry and residue
1958 pxor @tweak[5],@tweak[1]
1963 movups ($inp),$inout0
1965 xorps @tweak[0],$inout0
1967 &aesni_generate1("dec",$key,$rounds);
1969 xorps @tweak[0],$inout0
1970 movdqa @tweak[1],@tweak[0]
1971 movups $inout0,($out)
1972 movdqa @tweak[2],@tweak[1]
1978 movups ($inp),$inout0
1979 movups 16($inp),$inout1
1981 xorps @tweak[0],$inout0
1982 xorps @tweak[1],$inout1
1984 call _aesni_decrypt3
1986 xorps @tweak[0],$inout0
1987 movdqa @tweak[2],@tweak[0]
1988 xorps @tweak[1],$inout1
1989 movdqa @tweak[3],@tweak[1]
1990 movups $inout0,($out)
1991 movups $inout1,16*1($out)
1997 movups ($inp),$inout0
1998 movups 16*1($inp),$inout1
1999 movups 16*2($inp),$inout2
2001 xorps @tweak[0],$inout0
2002 xorps @tweak[1],$inout1
2003 xorps @tweak[2],$inout2
2005 call _aesni_decrypt3
2007 xorps @tweak[0],$inout0
2008 movdqa @tweak[3],@tweak[0]
2009 xorps @tweak[1],$inout1
2010 movdqa @tweak[5],@tweak[1]
2011 xorps @tweak[2],$inout2
2012 movups $inout0,($out)
2013 movups $inout1,16*1($out)
2014 movups $inout2,16*2($out)
2020 pshufd \$0x13,$twtmp,$twres
2021 movdqa @tweak[5],@tweak[4]
2022 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
2023 movups ($inp),$inout0
2024 pand $twmask,$twres # isolate carry and residue
2025 movups 16*1($inp),$inout1
2026 pxor $twres,@tweak[5]
2028 movups 16*2($inp),$inout2
2029 xorps @tweak[0],$inout0
2030 movups 16*3($inp),$inout3
2032 xorps @tweak[1],$inout1
2033 xorps @tweak[2],$inout2
2034 xorps @tweak[3],$inout3
2036 call _aesni_decrypt4
2038 xorps @tweak[0],$inout0
2039 movdqa @tweak[4],@tweak[0]
2040 xorps @tweak[1],$inout1
2041 movdqa @tweak[5],@tweak[1]
2042 xorps @tweak[2],$inout2
2043 movups $inout0,($out)
2044 xorps @tweak[3],$inout3
2045 movups $inout1,16*1($out)
2046 movups $inout2,16*2($out)
2047 movups $inout3,16*3($out)
2057 mov $key_,$key # restore $key
2058 mov $rnds_,$rounds # restore $rounds
2060 movups ($inp),$inout0
2061 xorps @tweak[1],$inout0
2063 &aesni_generate1("dec",$key,$rounds);
2065 xorps @tweak[1],$inout0
2066 movups $inout0,($out)
2069 movzb 16($inp),%eax # borrow $rounds ...
2070 movzb ($out),%ecx # ... and $key
2078 sub $len_,$out # rewind $out
2079 mov $key_,$key # restore $key
2080 mov $rnds_,$rounds # restore $rounds
2082 movups ($out),$inout0
2083 xorps @tweak[0],$inout0
2085 &aesni_generate1("dec",$key,$rounds);
2087 xorps @tweak[0],$inout0
2088 movups $inout0,($out)
2092 $code.=<<___ if ($win64);
2093 movaps 0x60(%rsp),%xmm6
2094 movaps 0x70(%rsp),%xmm7
2095 movaps 0x80(%rsp),%xmm8
2096 movaps 0x90(%rsp),%xmm9
2097 movaps 0xa0(%rsp),%xmm10
2098 movaps 0xb0(%rsp),%xmm11
2099 movaps 0xc0(%rsp),%xmm12
2100 movaps 0xd0(%rsp),%xmm13
2101 movaps 0xe0(%rsp),%xmm14
2102 movaps 0xf0(%rsp),%xmm15
2105 lea $frame_size(%rsp),%rsp
2108 .size aesni_xts_decrypt,.-aesni_xts_decrypt
2112 ########################################################################
2113 # void $PREFIX_cbc_encrypt (const void *inp, void *out,
2114 # size_t length, const AES_KEY *key,
2115 # unsigned char *ivp,const int enc);
2117 my $reserved = $win64?0x40:-0x18; # used in decrypt
2119 .globl ${PREFIX}_cbc_encrypt
2120 .type ${PREFIX}_cbc_encrypt,\@function,6
2122 ${PREFIX}_cbc_encrypt:
2123 test $len,$len # check length
2126 mov 240($key),$rnds_ # key->rounds
2127 mov $key,$key_ # backup $key
2128 test %r9d,%r9d # 6th argument
2130 #--------------------------- CBC ENCRYPT ------------------------------#
2131 movups ($ivp),$inout0 # load iv as initial state
2139 movups ($inp),$inout1 # load input
2141 #xorps $inout1,$inout0
2143 &aesni_generate1("enc",$key,$rounds,$inout0,$inout1);
2145 mov $rnds_,$rounds # restore $rounds
2146 mov $key_,$key # restore $key
2147 movups $inout0,0($out) # store output
2153 movups $inout0,($ivp)
2157 mov $len,%rcx # zaps $key
2158 xchg $inp,$out # $inp is %rsi and $out is %rdi now
2159 .long 0x9066A4F3 # rep movsb
2160 mov \$16,%ecx # zero tail
2163 .long 0x9066AAF3 # rep stosb
2164 lea -16(%rdi),%rdi # rewind $out by 1 block
2165 mov $rnds_,$rounds # restore $rounds
2166 mov %rdi,%rsi # $inp and $out are the same
2167 mov $key_,$key # restore $key
2168 xor $len,$len # len=16
2169 jmp .Lcbc_enc_loop # one more spin
2170 \f#--------------------------- CBC DECRYPT ------------------------------#
2174 $code.=<<___ if ($win64);
2175 lea -0x58(%rsp),%rsp
2177 movaps %xmm7,0x10(%rsp)
2178 movaps %xmm8,0x20(%rsp)
2179 movaps %xmm9,0x30(%rsp)
2190 movaps $iv,$reserved(%rsp)
2191 jmp .Lcbc_dec_loop8_enter
2194 movaps $rndkey0,$reserved(%rsp) # save IV
2195 movups $inout7,($out)
2197 .Lcbc_dec_loop8_enter:
2198 $movkey ($key),$rndkey0
2199 movups ($inp),$inout0 # load input
2200 movups 0x10($inp),$inout1
2201 $movkey 16($key),$rndkey1
2204 movdqu 0x20($inp),$inout2
2205 xorps $rndkey0,$inout0
2206 movdqu 0x30($inp),$inout3
2207 xorps $rndkey0,$inout1
2208 movdqu 0x40($inp),$inout4
2209 aesdec $rndkey1,$inout0
2210 pxor $rndkey0,$inout2
2211 movdqu 0x50($inp),$inout5
2212 aesdec $rndkey1,$inout1
2213 pxor $rndkey0,$inout3
2214 movdqu 0x60($inp),$inout6
2215 aesdec $rndkey1,$inout2
2216 pxor $rndkey0,$inout4
2217 movdqu 0x70($inp),$inout7
2218 aesdec $rndkey1,$inout3
2219 pxor $rndkey0,$inout5
2221 aesdec $rndkey1,$inout4
2222 pxor $rndkey0,$inout6
2223 aesdec $rndkey1,$inout5
2224 pxor $rndkey0,$inout7
2225 $movkey ($key),$rndkey0
2226 aesdec $rndkey1,$inout6
2227 aesdec $rndkey1,$inout7
2228 $movkey 16($key),$rndkey1
2230 call .Ldec_loop8_enter
2232 movups ($inp),$rndkey1 # re-load input
2233 movups 0x10($inp),$rndkey0
2234 xorps $reserved(%rsp),$inout0 # ^= IV
2235 xorps $rndkey1,$inout1
2236 movups 0x20($inp),$rndkey1
2237 xorps $rndkey0,$inout2
2238 movups 0x30($inp),$rndkey0
2239 xorps $rndkey1,$inout3
2240 movups 0x40($inp),$rndkey1
2241 xorps $rndkey0,$inout4
2242 movups 0x50($inp),$rndkey0
2243 xorps $rndkey1,$inout5
2244 movups 0x60($inp),$rndkey1
2245 xorps $rndkey0,$inout6
2246 movups 0x70($inp),$rndkey0 # IV
2247 xorps $rndkey1,$inout7
2248 movups $inout0,($out)
2249 movups $inout1,0x10($out)
2250 movups $inout2,0x20($out)
2251 movups $inout3,0x30($out)
2252 mov $rnds_,$rounds # restore $rounds
2253 movups $inout4,0x40($out)
2254 mov $key_,$key # restore $key
2255 movups $inout5,0x50($out)
2257 movups $inout6,0x60($out)
2262 movaps $inout7,$inout0
2265 jle .Lcbc_dec_tail_collected
2266 movups $inout0,($out)
2267 lea 1($rnds_,$rnds_),$rounds
2270 movups ($inp),$inout0
2275 movups 0x10($inp),$inout1
2280 movups 0x20($inp),$inout2
2285 movups 0x30($inp),$inout3
2289 movups 0x40($inp),$inout4
2293 movups 0x50($inp),$inout5
2297 movups 0x60($inp),$inout6
2298 movaps $iv,$reserved(%rsp) # save IV
2299 call _aesni_decrypt8
2300 movups ($inp),$rndkey1
2301 movups 0x10($inp),$rndkey0
2302 xorps $reserved(%rsp),$inout0 # ^= IV
2303 xorps $rndkey1,$inout1
2304 movups 0x20($inp),$rndkey1
2305 xorps $rndkey0,$inout2
2306 movups 0x30($inp),$rndkey0
2307 xorps $rndkey1,$inout3
2308 movups 0x40($inp),$rndkey1
2309 xorps $rndkey0,$inout4
2310 movups 0x50($inp),$rndkey0
2311 xorps $rndkey1,$inout5
2312 movups 0x60($inp),$iv # IV
2313 xorps $rndkey0,$inout6
2314 movups $inout0,($out)
2315 movups $inout1,0x10($out)
2316 movups $inout2,0x20($out)
2317 movups $inout3,0x30($out)
2318 movups $inout4,0x40($out)
2319 movups $inout5,0x50($out)
2321 movaps $inout6,$inout0
2323 jmp .Lcbc_dec_tail_collected
2327 &aesni_generate1("dec",$key,$rounds);
2332 jmp .Lcbc_dec_tail_collected
2335 xorps $inout2,$inout2
2336 call _aesni_decrypt3
2339 movups $inout0,($out)
2341 movaps $inout1,$inout0
2344 jmp .Lcbc_dec_tail_collected
2347 call _aesni_decrypt3
2350 movups $inout0,($out)
2352 movups $inout1,0x10($out)
2354 movaps $inout2,$inout0
2357 jmp .Lcbc_dec_tail_collected
2360 call _aesni_decrypt4
2362 movups 0x30($inp),$iv
2364 movups $inout0,($out)
2366 movups $inout1,0x10($out)
2368 movups $inout2,0x20($out)
2369 movaps $inout3,$inout0
2372 jmp .Lcbc_dec_tail_collected
2375 xorps $inout5,$inout5
2376 call _aesni_decrypt6
2377 movups 0x10($inp),$rndkey1
2378 movups 0x20($inp),$rndkey0
2381 xorps $rndkey1,$inout2
2382 movups 0x30($inp),$rndkey1
2383 xorps $rndkey0,$inout3
2384 movups 0x40($inp),$iv
2385 xorps $rndkey1,$inout4
2386 movups $inout0,($out)
2387 movups $inout1,0x10($out)
2388 movups $inout2,0x20($out)
2389 movups $inout3,0x30($out)
2391 movaps $inout4,$inout0
2393 jmp .Lcbc_dec_tail_collected
2396 call _aesni_decrypt6
2397 movups 0x10($inp),$rndkey1
2398 movups 0x20($inp),$rndkey0
2401 xorps $rndkey1,$inout2
2402 movups 0x30($inp),$rndkey1
2403 xorps $rndkey0,$inout3
2404 movups 0x40($inp),$rndkey0
2405 xorps $rndkey1,$inout4
2406 movups 0x50($inp),$iv
2407 xorps $rndkey0,$inout5
2408 movups $inout0,($out)
2409 movups $inout1,0x10($out)
2410 movups $inout2,0x20($out)
2411 movups $inout3,0x30($out)
2412 movups $inout4,0x40($out)
2414 movaps $inout5,$inout0
2416 jmp .Lcbc_dec_tail_collected
2418 .Lcbc_dec_tail_collected:
2421 jnz .Lcbc_dec_tail_partial
2422 movups $inout0,($out)
2425 .Lcbc_dec_tail_partial:
2426 movaps $inout0,$reserved(%rsp)
2430 lea $reserved(%rsp),%rsi
2431 .long 0x9066A4F3 # rep movsb
2435 $code.=<<___ if ($win64);
2437 movaps 0x10(%rsp),%xmm7
2438 movaps 0x20(%rsp),%xmm8
2439 movaps 0x30(%rsp),%xmm9
2445 .size ${PREFIX}_cbc_encrypt,.-${PREFIX}_cbc_encrypt
2448 # int $PREFIX_set_[en|de]crypt_key (const unsigned char *userKey,
2449 # int bits, AES_KEY *key)
2450 { my ($inp,$bits,$key) = @_4args;
2454 .globl ${PREFIX}_set_decrypt_key
2455 .type ${PREFIX}_set_decrypt_key,\@abi-omnipotent
2457 ${PREFIX}_set_decrypt_key:
2458 .byte 0x48,0x83,0xEC,0x08 # sub rsp,8
2459 call __aesni_set_encrypt_key
2460 shl \$4,$bits # rounds-1 after _aesni_set_encrypt_key
2463 lea 16($key,$bits),$inp # points at the end of key schedule
2465 $movkey ($key),%xmm0 # just swap
2466 $movkey ($inp),%xmm1
2467 $movkey %xmm0,($inp)
2468 $movkey %xmm1,($key)
2473 $movkey ($key),%xmm0 # swap and inverse
2474 $movkey ($inp),%xmm1
2479 $movkey %xmm0,16($inp)
2480 $movkey %xmm1,-16($key)
2482 ja .Ldec_key_inverse
2484 $movkey ($key),%xmm0 # inverse middle
2486 $movkey %xmm0,($inp)
2490 .LSEH_end_set_decrypt_key:
2491 .size ${PREFIX}_set_decrypt_key,.-${PREFIX}_set_decrypt_key
2494 # This is based on submission by
2496 # Huang Ying <ying.huang@intel.com>
2497 # Vinodh Gopal <vinodh.gopal@intel.com>
2500 # Agressively optimized in respect to aeskeygenassist's critical path
2501 # and is contained in %xmm0-5 to meet Win64 ABI requirement.
2504 .globl ${PREFIX}_set_encrypt_key
2505 .type ${PREFIX}_set_encrypt_key,\@abi-omnipotent
2507 ${PREFIX}_set_encrypt_key:
2508 __aesni_set_encrypt_key:
2509 .byte 0x48,0x83,0xEC,0x08 # sub rsp,8
2516 movups ($inp),%xmm0 # pull first 128 bits of *userKey
2517 xorps %xmm4,%xmm4 # low dword of xmm4 is assumed 0
2527 mov \$9,$bits # 10 rounds for 128-bit key
2528 $movkey %xmm0,($key) # round 0
2529 aeskeygenassist \$0x1,%xmm0,%xmm1 # round 1
2530 call .Lkey_expansion_128_cold
2531 aeskeygenassist \$0x2,%xmm0,%xmm1 # round 2
2532 call .Lkey_expansion_128
2533 aeskeygenassist \$0x4,%xmm0,%xmm1 # round 3
2534 call .Lkey_expansion_128
2535 aeskeygenassist \$0x8,%xmm0,%xmm1 # round 4
2536 call .Lkey_expansion_128
2537 aeskeygenassist \$0x10,%xmm0,%xmm1 # round 5
2538 call .Lkey_expansion_128
2539 aeskeygenassist \$0x20,%xmm0,%xmm1 # round 6
2540 call .Lkey_expansion_128
2541 aeskeygenassist \$0x40,%xmm0,%xmm1 # round 7
2542 call .Lkey_expansion_128
2543 aeskeygenassist \$0x80,%xmm0,%xmm1 # round 8
2544 call .Lkey_expansion_128
2545 aeskeygenassist \$0x1b,%xmm0,%xmm1 # round 9
2546 call .Lkey_expansion_128
2547 aeskeygenassist \$0x36,%xmm0,%xmm1 # round 10
2548 call .Lkey_expansion_128
2549 $movkey %xmm0,(%rax)
2550 mov $bits,80(%rax) # 240(%rdx)
2556 movq 16($inp),%xmm2 # remaining 1/3 of *userKey
2557 mov \$11,$bits # 12 rounds for 192
2558 $movkey %xmm0,($key) # round 0
2559 aeskeygenassist \$0x1,%xmm2,%xmm1 # round 1,2
2560 call .Lkey_expansion_192a_cold
2561 aeskeygenassist \$0x2,%xmm2,%xmm1 # round 2,3
2562 call .Lkey_expansion_192b
2563 aeskeygenassist \$0x4,%xmm2,%xmm1 # round 4,5
2564 call .Lkey_expansion_192a
2565 aeskeygenassist \$0x8,%xmm2,%xmm1 # round 5,6
2566 call .Lkey_expansion_192b
2567 aeskeygenassist \$0x10,%xmm2,%xmm1 # round 7,8
2568 call .Lkey_expansion_192a
2569 aeskeygenassist \$0x20,%xmm2,%xmm1 # round 8,9
2570 call .Lkey_expansion_192b
2571 aeskeygenassist \$0x40,%xmm2,%xmm1 # round 10,11
2572 call .Lkey_expansion_192a
2573 aeskeygenassist \$0x80,%xmm2,%xmm1 # round 11,12
2574 call .Lkey_expansion_192b
2575 $movkey %xmm0,(%rax)
2576 mov $bits,48(%rax) # 240(%rdx)
2582 movups 16($inp),%xmm2 # remaning half of *userKey
2583 mov \$13,$bits # 14 rounds for 256
2585 $movkey %xmm0,($key) # round 0
2586 $movkey %xmm2,16($key) # round 1
2587 aeskeygenassist \$0x1,%xmm2,%xmm1 # round 2
2588 call .Lkey_expansion_256a_cold
2589 aeskeygenassist \$0x1,%xmm0,%xmm1 # round 3
2590 call .Lkey_expansion_256b
2591 aeskeygenassist \$0x2,%xmm2,%xmm1 # round 4
2592 call .Lkey_expansion_256a
2593 aeskeygenassist \$0x2,%xmm0,%xmm1 # round 5
2594 call .Lkey_expansion_256b
2595 aeskeygenassist \$0x4,%xmm2,%xmm1 # round 6
2596 call .Lkey_expansion_256a
2597 aeskeygenassist \$0x4,%xmm0,%xmm1 # round 7
2598 call .Lkey_expansion_256b
2599 aeskeygenassist \$0x8,%xmm2,%xmm1 # round 8
2600 call .Lkey_expansion_256a
2601 aeskeygenassist \$0x8,%xmm0,%xmm1 # round 9
2602 call .Lkey_expansion_256b
2603 aeskeygenassist \$0x10,%xmm2,%xmm1 # round 10
2604 call .Lkey_expansion_256a
2605 aeskeygenassist \$0x10,%xmm0,%xmm1 # round 11
2606 call .Lkey_expansion_256b
2607 aeskeygenassist \$0x20,%xmm2,%xmm1 # round 12
2608 call .Lkey_expansion_256a
2609 aeskeygenassist \$0x20,%xmm0,%xmm1 # round 13
2610 call .Lkey_expansion_256b
2611 aeskeygenassist \$0x40,%xmm2,%xmm1 # round 14
2612 call .Lkey_expansion_256a
2613 $movkey %xmm0,(%rax)
2614 mov $bits,16(%rax) # 240(%rdx)
2624 .LSEH_end_set_encrypt_key:
2627 .Lkey_expansion_128:
2628 $movkey %xmm0,(%rax)
2630 .Lkey_expansion_128_cold:
2631 shufps \$0b00010000,%xmm0,%xmm4
2633 shufps \$0b10001100,%xmm0,%xmm4
2635 shufps \$0b11111111,%xmm1,%xmm1 # critical path
2640 .Lkey_expansion_192a:
2641 $movkey %xmm0,(%rax)
2643 .Lkey_expansion_192a_cold:
2645 .Lkey_expansion_192b_warm:
2646 shufps \$0b00010000,%xmm0,%xmm4
2649 shufps \$0b10001100,%xmm0,%xmm4
2652 pshufd \$0b01010101,%xmm1,%xmm1 # critical path
2655 pshufd \$0b11111111,%xmm0,%xmm3
2660 .Lkey_expansion_192b:
2662 shufps \$0b01000100,%xmm0,%xmm5
2663 $movkey %xmm5,(%rax)
2664 shufps \$0b01001110,%xmm2,%xmm3
2665 $movkey %xmm3,16(%rax)
2667 jmp .Lkey_expansion_192b_warm
2670 .Lkey_expansion_256a:
2671 $movkey %xmm2,(%rax)
2673 .Lkey_expansion_256a_cold:
2674 shufps \$0b00010000,%xmm0,%xmm4
2676 shufps \$0b10001100,%xmm0,%xmm4
2678 shufps \$0b11111111,%xmm1,%xmm1 # critical path
2683 .Lkey_expansion_256b:
2684 $movkey %xmm0,(%rax)
2687 shufps \$0b00010000,%xmm2,%xmm4
2689 shufps \$0b10001100,%xmm2,%xmm4
2691 shufps \$0b10101010,%xmm1,%xmm1 # critical path
2694 .size ${PREFIX}_set_encrypt_key,.-${PREFIX}_set_encrypt_key
2695 .size __aesni_set_encrypt_key,.-__aesni_set_encrypt_key
2702 .byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
2710 .asciz "AES for Intel AES-NI, CRYPTOGAMS by <appro\@openssl.org>"
2714 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
2715 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
2723 .extern __imp_RtlVirtualUnwind
2725 $code.=<<___ if ($PREFIX eq "aesni");
2726 .type ecb_se_handler,\@abi-omnipotent
2740 mov 152($context),%rax # pull context->Rsp
2742 jmp .Lcommon_seh_tail
2743 .size ecb_se_handler,.-ecb_se_handler
2745 .type ccm64_se_handler,\@abi-omnipotent
2759 mov 120($context),%rax # pull context->Rax
2760 mov 248($context),%rbx # pull context->Rip
2762 mov 8($disp),%rsi # disp->ImageBase
2763 mov 56($disp),%r11 # disp->HandlerData
2765 mov 0(%r11),%r10d # HandlerData[0]
2766 lea (%rsi,%r10),%r10 # prologue label
2767 cmp %r10,%rbx # context->Rip<prologue label
2768 jb .Lcommon_seh_tail
2770 mov 152($context),%rax # pull context->Rsp
2772 mov 4(%r11),%r10d # HandlerData[1]
2773 lea (%rsi,%r10),%r10 # epilogue label
2774 cmp %r10,%rbx # context->Rip>=epilogue label
2775 jae .Lcommon_seh_tail
2777 lea 0(%rax),%rsi # %xmm save area
2778 lea 512($context),%rdi # &context.Xmm6
2779 mov \$8,%ecx # 4*sizeof(%xmm0)/sizeof(%rax)
2780 .long 0xa548f3fc # cld; rep movsq
2781 lea 0x58(%rax),%rax # adjust stack pointer
2783 jmp .Lcommon_seh_tail
2784 .size ccm64_se_handler,.-ccm64_se_handler
2786 .type ctr32_se_handler,\@abi-omnipotent
2800 mov 120($context),%rax # pull context->Rax
2801 mov 248($context),%rbx # pull context->Rip
2803 lea .Lctr32_body(%rip),%r10
2804 cmp %r10,%rbx # context->Rip<"prologue" label
2805 jb .Lcommon_seh_tail
2807 mov 152($context),%rax # pull context->Rsp
2809 lea .Lctr32_ret(%rip),%r10
2811 jae .Lcommon_seh_tail
2813 lea 0x20(%rax),%rsi # %xmm save area
2814 lea 512($context),%rdi # &context.Xmm6
2815 mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
2816 .long 0xa548f3fc # cld; rep movsq
2817 lea 0xc8(%rax),%rax # adjust stack pointer
2819 jmp .Lcommon_seh_tail
2820 .size ctr32_se_handler,.-ctr32_se_handler
2822 .type xts_se_handler,\@abi-omnipotent
2836 mov 120($context),%rax # pull context->Rax
2837 mov 248($context),%rbx # pull context->Rip
2839 mov 8($disp),%rsi # disp->ImageBase
2840 mov 56($disp),%r11 # disp->HandlerData
2842 mov 0(%r11),%r10d # HandlerData[0]
2843 lea (%rsi,%r10),%r10 # prologue lable
2844 cmp %r10,%rbx # context->Rip<prologue label
2845 jb .Lcommon_seh_tail
2847 mov 152($context),%rax # pull context->Rsp
2849 mov 4(%r11),%r10d # HandlerData[1]
2850 lea (%rsi,%r10),%r10 # epilogue label
2851 cmp %r10,%rbx # context->Rip>=epilogue label
2852 jae .Lcommon_seh_tail
2854 lea 0x60(%rax),%rsi # %xmm save area
2855 lea 512($context),%rdi # & context.Xmm6
2856 mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
2857 .long 0xa548f3fc # cld; rep movsq
2858 lea 0x68+160(%rax),%rax # adjust stack pointer
2860 jmp .Lcommon_seh_tail
2861 .size xts_se_handler,.-xts_se_handler
2864 .type cbc_se_handler,\@abi-omnipotent
2878 mov 152($context),%rax # pull context->Rsp
2879 mov 248($context),%rbx # pull context->Rip
2881 lea .Lcbc_decrypt(%rip),%r10
2882 cmp %r10,%rbx # context->Rip<"prologue" label
2883 jb .Lcommon_seh_tail
2885 lea .Lcbc_decrypt_body(%rip),%r10
2886 cmp %r10,%rbx # context->Rip<cbc_decrypt_body
2887 jb .Lrestore_cbc_rax
2889 lea .Lcbc_ret(%rip),%r10
2890 cmp %r10,%rbx # context->Rip>="epilogue" label
2891 jae .Lcommon_seh_tail
2893 lea 0(%rax),%rsi # top of stack
2894 lea 512($context),%rdi # &context.Xmm6
2895 mov \$8,%ecx # 4*sizeof(%xmm0)/sizeof(%rax)
2896 .long 0xa548f3fc # cld; rep movsq
2897 lea 0x58(%rax),%rax # adjust stack pointer
2898 jmp .Lcommon_seh_tail
2901 mov 120($context),%rax
2906 mov %rax,152($context) # restore context->Rsp
2907 mov %rsi,168($context) # restore context->Rsi
2908 mov %rdi,176($context) # restore context->Rdi
2910 mov 40($disp),%rdi # disp->ContextRecord
2911 mov $context,%rsi # context
2912 mov \$154,%ecx # sizeof(CONTEXT)
2913 .long 0xa548f3fc # cld; rep movsq
2916 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
2917 mov 8(%rsi),%rdx # arg2, disp->ImageBase
2918 mov 0(%rsi),%r8 # arg3, disp->ControlPc
2919 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
2920 mov 40(%rsi),%r10 # disp->ContextRecord
2921 lea 56(%rsi),%r11 # &disp->HandlerData
2922 lea 24(%rsi),%r12 # &disp->EstablisherFrame
2923 mov %r10,32(%rsp) # arg5
2924 mov %r11,40(%rsp) # arg6
2925 mov %r12,48(%rsp) # arg7
2926 mov %rcx,56(%rsp) # arg8, (NULL)
2927 call *__imp_RtlVirtualUnwind(%rip)
2929 mov \$1,%eax # ExceptionContinueSearch
2941 .size cbc_se_handler,.-cbc_se_handler
2946 $code.=<<___ if ($PREFIX eq "aesni");
2947 .rva .LSEH_begin_aesni_ecb_encrypt
2948 .rva .LSEH_end_aesni_ecb_encrypt
2951 .rva .LSEH_begin_aesni_ccm64_encrypt_blocks
2952 .rva .LSEH_end_aesni_ccm64_encrypt_blocks
2953 .rva .LSEH_info_ccm64_enc
2955 .rva .LSEH_begin_aesni_ccm64_decrypt_blocks
2956 .rva .LSEH_end_aesni_ccm64_decrypt_blocks
2957 .rva .LSEH_info_ccm64_dec
2959 .rva .LSEH_begin_aesni_ctr32_encrypt_blocks
2960 .rva .LSEH_end_aesni_ctr32_encrypt_blocks
2961 .rva .LSEH_info_ctr32
2963 .rva .LSEH_begin_aesni_xts_encrypt
2964 .rva .LSEH_end_aesni_xts_encrypt
2965 .rva .LSEH_info_xts_enc
2967 .rva .LSEH_begin_aesni_xts_decrypt
2968 .rva .LSEH_end_aesni_xts_decrypt
2969 .rva .LSEH_info_xts_dec
2972 .rva .LSEH_begin_${PREFIX}_cbc_encrypt
2973 .rva .LSEH_end_${PREFIX}_cbc_encrypt
2976 .rva ${PREFIX}_set_decrypt_key
2977 .rva .LSEH_end_set_decrypt_key
2980 .rva ${PREFIX}_set_encrypt_key
2981 .rva .LSEH_end_set_encrypt_key
2986 $code.=<<___ if ($PREFIX eq "aesni");
2990 .LSEH_info_ccm64_enc:
2992 .rva ccm64_se_handler
2993 .rva .Lccm64_enc_body,.Lccm64_enc_ret # HandlerData[]
2994 .LSEH_info_ccm64_dec:
2996 .rva ccm64_se_handler
2997 .rva .Lccm64_dec_body,.Lccm64_dec_ret # HandlerData[]
3000 .rva ctr32_se_handler
3004 .rva .Lxts_enc_body,.Lxts_enc_epilogue # HandlerData[]
3008 .rva .Lxts_dec_body,.Lxts_dec_epilogue # HandlerData[]
3015 .byte 0x01,0x04,0x01,0x00
3016 .byte 0x04,0x02,0x00,0x00 # sub rsp,8
3021 local *opcode=shift;
3024 if ($dst>=8 || $src>=8) {
3026 $rex|=0x04 if($dst>=8);
3027 $rex|=0x01 if($src>=8);
3036 if ($line=~/(aeskeygenassist)\s+\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
3037 rex(\@opcode,$4,$3);
3038 push @opcode,0x0f,0x3a,0xdf;
3039 push @opcode,0xc0|($3&7)|(($4&7)<<3); # ModR/M
3041 push @opcode,$c=~/^0/?oct($c):$c;
3042 return ".byte\t".join(',',@opcode);
3044 elsif ($line=~/(aes[a-z]+)\s+%xmm([0-9]+),\s*%xmm([0-9]+)/) {
3047 "aesenc" => 0xdc, "aesenclast" => 0xdd,
3048 "aesdec" => 0xde, "aesdeclast" => 0xdf
3050 return undef if (!defined($opcodelet{$1}));
3051 rex(\@opcode,$3,$2);
3052 push @opcode,0x0f,0x38,$opcodelet{$1};
3053 push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
3054 return ".byte\t".join(',',@opcode);
3059 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
3060 $code =~ s/\b(aes.*%xmm[0-9]+).*$/aesni($1)/gem;