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="%xmm6";
825 my $bswap_mask="%xmm7";
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)
842 mov 240($key),$rounds # key->rounds
844 movdqa .Lincrement64(%rip),$increment
845 movdqa .Lbswap_mask(%rip),$bswap_mask
849 movdqu ($cmac),$inout1
852 pshufb $bswap_mask,$iv
853 jmp .Lccm64_enc_outer
856 $movkey ($key_),$rndkey0
858 movups ($inp),$in0 # load inp
860 xorps $rndkey0,$inout0 # counter
861 $movkey 16($key_),$rndkey1
864 xorps $rndkey0,$inout1 # cmac^=inp
865 $movkey ($key),$rndkey0
868 aesenc $rndkey1,$inout0
870 aesenc $rndkey1,$inout1
871 $movkey 16($key),$rndkey1
872 aesenc $rndkey0,$inout0
874 aesenc $rndkey0,$inout1
875 $movkey 0($key),$rndkey0
876 jnz .Lccm64_enc2_loop
877 aesenc $rndkey1,$inout0
878 aesenc $rndkey1,$inout1
880 aesenclast $rndkey0,$inout0
881 aesenclast $rndkey0,$inout1
885 xorps $inout0,$in0 # inp ^= E(iv)
887 movups $in0,($out) # save output
889 pshufb $bswap_mask,$inout0
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)
922 mov 240($key),$rounds # key->rounds
924 movdqu ($cmac),$inout1
925 movdqa .Lincrement64(%rip),$increment
926 movdqa .Lbswap_mask(%rip),$bswap_mask
931 pshufb $bswap_mask,$iv
933 &aesni_generate1("enc",$key,$rounds);
935 movups ($inp),$in0 # load inp
938 jmp .Lccm64_dec_outer
941 xorps $inout0,$in0 # inp ^= E(iv)
944 movups $in0,($out) # save output
946 pshufb $bswap_mask,$inout0
951 $movkey ($key_),$rndkey0
953 $movkey 16($key_),$rndkey1
956 xorps $rndkey0,$inout0
957 xorps $in0,$inout1 # cmac^=out
958 $movkey ($key),$rndkey0
961 aesenc $rndkey1,$inout0
963 aesenc $rndkey1,$inout1
964 $movkey 16($key),$rndkey1
965 aesenc $rndkey0,$inout0
967 aesenc $rndkey0,$inout1
968 $movkey 0($key),$rndkey0
969 jnz .Lccm64_dec2_loop
970 movups ($inp),$in0 # load inp
972 aesenc $rndkey1,$inout0
973 aesenc $rndkey1,$inout1
975 aesenclast $rndkey0,$inout0
976 aesenclast $rndkey0,$inout1
977 jmp .Lccm64_dec_outer
981 #xorps $in0,$inout1 # cmac^=out
983 &aesni_generate1("enc",$key_,$rounds,$inout1,$in0);
985 movups $inout1,($cmac)
987 $code.=<<___ if ($win64);
989 movaps 0x10(%rsp),%xmm7
990 movaps 0x20(%rsp),%xmm8
991 movaps 0x30(%rsp),%xmm9
997 .size aesni_ccm64_decrypt_blocks,.-aesni_ccm64_decrypt_blocks
1000 ######################################################################
1001 # void aesni_ctr32_encrypt_blocks (const void *in, void *out,
1002 # size_t blocks, const AES_KEY *key,
1003 # const char *ivec);
1005 # Handles only complete blocks, operates on 32-bit counter and
1006 # does not update *ivec! (see engine/eng_aesni.c for details)
1009 my $frame_size = 0x20+($win64?160:0);
1010 my ($in0,$in1,$in2,$in3)=map("%xmm$_",(8..11));
1011 my ($iv0,$iv1,$ivec)=("%xmm12","%xmm13","%xmm14");
1012 my $bswap_mask="%xmm15";
1015 .globl aesni_ctr32_encrypt_blocks
1016 .type aesni_ctr32_encrypt_blocks,\@function,5
1018 aesni_ctr32_encrypt_blocks:
1021 sub \$$frame_size,%rsp
1022 and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
1024 $code.=<<___ if ($win64);
1025 movaps %xmm6,0x20(%rsp)
1026 movaps %xmm7,0x30(%rsp)
1027 movaps %xmm8,0x40(%rsp)
1028 movaps %xmm9,0x50(%rsp)
1029 movaps %xmm10,0x60(%rsp)
1030 movaps %xmm11,0x70(%rsp)
1031 movaps %xmm12,0x80(%rsp)
1032 movaps %xmm13,0x90(%rsp)
1033 movaps %xmm14,0xa0(%rsp)
1034 movaps %xmm15,0xb0(%rsp)
1040 je .Lctr32_one_shortcut
1043 movdqa .Lbswap_mask(%rip),$bswap_mask
1045 pextrd \$3,$ivec,$rnds_ # pull 32-bit counter
1046 pinsrd \$3,$rounds,$ivec # wipe 32-bit counter
1048 mov 240($key),$rounds # key->rounds
1050 pxor $iv0,$iv0 # vector of 3 32-bit counters
1051 pxor $iv1,$iv1 # vector of 3 32-bit counters
1052 pinsrd \$0,$rnds_,$iv0
1054 pinsrd \$0,$key_,$iv1
1056 pinsrd \$1,$rnds_,$iv0
1058 pinsrd \$1,$key_,$iv1
1060 pinsrd \$2,$rnds_,$iv0
1062 pinsrd \$2,$key_,$iv1
1063 movdqa $iv0,0x00(%rsp)
1064 pshufb $bswap_mask,$iv0
1065 movdqa $iv1,0x10(%rsp)
1066 pshufb $bswap_mask,$iv1
1068 pshufd \$`3<<6`,$iv0,$inout0 # place counter to upper dword
1069 pshufd \$`2<<6`,$iv0,$inout1
1070 pshufd \$`1<<6`,$iv0,$inout2
1074 mov $key,$key_ # backup $key
1075 mov $rounds,$rnds_ # backup $rounds
1081 pshufd \$`3<<6`,$iv1,$inout3
1082 por $ivec,$inout0 # merge counter-less ivec
1083 $movkey ($key_),$rndkey0
1084 pshufd \$`2<<6`,$iv1,$inout4
1086 $movkey 16($key_),$rndkey1
1087 pshufd \$`1<<6`,$iv1,$inout5
1090 xorps $rndkey0,$inout0
1094 # inline _aesni_encrypt6 and interleave last rounds
1097 pxor $rndkey0,$inout1
1098 aesenc $rndkey1,$inout0
1100 pxor $rndkey0,$inout2
1101 aesenc $rndkey1,$inout1
1102 movdqa .Lincrement32(%rip),$iv1
1103 pxor $rndkey0,$inout3
1104 aesenc $rndkey1,$inout2
1106 pxor $rndkey0,$inout4
1107 aesenc $rndkey1,$inout3
1108 pxor $rndkey0,$inout5
1109 $movkey ($key),$rndkey0
1111 aesenc $rndkey1,$inout4
1112 aesenc $rndkey1,$inout5
1113 jmp .Lctr32_enc_loop6_enter
1116 aesenc $rndkey1,$inout0
1117 aesenc $rndkey1,$inout1
1119 aesenc $rndkey1,$inout2
1120 aesenc $rndkey1,$inout3
1121 aesenc $rndkey1,$inout4
1122 aesenc $rndkey1,$inout5
1123 .Lctr32_enc_loop6_enter:
1124 $movkey 16($key),$rndkey1
1125 aesenc $rndkey0,$inout0
1126 aesenc $rndkey0,$inout1
1128 aesenc $rndkey0,$inout2
1129 aesenc $rndkey0,$inout3
1130 aesenc $rndkey0,$inout4
1131 aesenc $rndkey0,$inout5
1132 $movkey ($key),$rndkey0
1133 jnz .Lctr32_enc_loop6
1135 aesenc $rndkey1,$inout0
1136 paddd $iv1,$iv0 # increment counter vector
1137 aesenc $rndkey1,$inout1
1138 paddd 0x10(%rsp),$iv1
1139 aesenc $rndkey1,$inout2
1140 movdqa $iv0,0x00(%rsp) # save counter vector
1141 aesenc $rndkey1,$inout3
1142 movdqa $iv1,0x10(%rsp)
1143 aesenc $rndkey1,$inout4
1144 pshufb $bswap_mask,$iv0 # byte swap
1145 aesenc $rndkey1,$inout5
1146 pshufb $bswap_mask,$iv1
1148 aesenclast $rndkey0,$inout0
1149 movups ($inp),$in0 # load input
1150 aesenclast $rndkey0,$inout1
1151 movups 0x10($inp),$in1
1152 aesenclast $rndkey0,$inout2
1153 movups 0x20($inp),$in2
1154 aesenclast $rndkey0,$inout3
1155 movups 0x30($inp),$in3
1156 aesenclast $rndkey0,$inout4
1157 movups 0x40($inp),$rndkey1
1158 aesenclast $rndkey0,$inout5
1159 movups 0x50($inp),$rndkey0
1162 xorps $inout0,$in0 # xor
1163 pshufd \$`3<<6`,$iv0,$inout0
1165 pshufd \$`2<<6`,$iv0,$inout1
1166 movups $in0,($out) # store output
1168 pshufd \$`1<<6`,$iv0,$inout2
1169 movups $in1,0x10($out)
1171 movups $in2,0x20($out)
1172 xorps $inout4,$rndkey1
1173 movups $in3,0x30($out)
1174 xorps $inout5,$rndkey0
1175 movups $rndkey1,0x40($out)
1176 movups $rndkey0,0x50($out)
1184 mov $key_,$key # restore $key
1185 lea 1($rounds,$rounds),$rounds # restore original value
1194 movups 0x10($inp),$in1
1197 pshufd \$`3<<6`,$iv1,$inout3
1199 movups 0x20($inp),$in2
1203 pshufd \$`2<<6`,$iv1,$inout4
1205 movups 0x30($inp),$in3
1209 xorps $inout5,$inout5
1211 call _aesni_encrypt6
1213 movups 0x40($inp),$rndkey1
1218 movups $in1,0x10($out)
1220 movups $in2,0x20($out)
1221 xorps $inout4,$rndkey1
1222 movups $in3,0x30($out)
1223 movups $rndkey1,0x40($out)
1227 .Lctr32_one_shortcut:
1228 movups ($ivp),$inout0
1230 mov 240($key),$rounds # key->rounds
1233 &aesni_generate1("enc",$key,$rounds);
1241 xorps $inout2,$inout2
1242 call _aesni_encrypt3
1246 movups $in1,0x10($out)
1251 call _aesni_encrypt3
1256 movups $in1,0x10($out)
1257 movups $in2,0x20($out)
1262 call _aesni_encrypt4
1267 movups $in1,0x10($out)
1269 movups $in2,0x20($out)
1270 movups $in3,0x30($out)
1274 $code.=<<___ if ($win64);
1275 movaps 0x20(%rsp),%xmm6
1276 movaps 0x30(%rsp),%xmm7
1277 movaps 0x40(%rsp),%xmm8
1278 movaps 0x50(%rsp),%xmm9
1279 movaps 0x60(%rsp),%xmm10
1280 movaps 0x70(%rsp),%xmm11
1281 movaps 0x80(%rsp),%xmm12
1282 movaps 0x90(%rsp),%xmm13
1283 movaps 0xa0(%rsp),%xmm14
1284 movaps 0xb0(%rsp),%xmm15
1291 .size aesni_ctr32_encrypt_blocks,.-aesni_ctr32_encrypt_blocks
1295 ######################################################################
1296 # void aesni_xts_[en|de]crypt(const char *inp,char *out,size_t len,
1297 # const AES_KEY *key1, const AES_KEY *key2
1298 # const unsigned char iv[16]);
1301 my @tweak=map("%xmm$_",(10..15));
1302 my ($twmask,$twres,$twtmp)=("%xmm8","%xmm9",@tweak[4]);
1303 my ($key2,$ivp,$len_)=("%r8","%r9","%r9");
1304 my $frame_size = 0x60 + ($win64?160:0);
1307 .globl aesni_xts_encrypt
1308 .type aesni_xts_encrypt,\@function,6
1313 sub \$$frame_size,%rsp
1314 and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
1316 $code.=<<___ if ($win64);
1317 movaps %xmm6,0x60(%rsp)
1318 movaps %xmm7,0x70(%rsp)
1319 movaps %xmm8,0x80(%rsp)
1320 movaps %xmm9,0x90(%rsp)
1321 movaps %xmm10,0xa0(%rsp)
1322 movaps %xmm11,0xb0(%rsp)
1323 movaps %xmm12,0xc0(%rsp)
1324 movaps %xmm13,0xd0(%rsp)
1325 movaps %xmm14,0xe0(%rsp)
1326 movaps %xmm15,0xf0(%rsp)
1331 movups ($ivp),@tweak[5] # load clear-text tweak
1332 mov 240(%r8),$rounds # key2->rounds
1333 mov 240($key),$rnds_ # key1->rounds
1335 # generate the tweak
1336 &aesni_generate1("enc",$key2,$rounds,@tweak[5]);
1338 mov $key,$key_ # backup $key
1339 mov $rnds_,$rounds # backup $rounds
1340 mov $len,$len_ # backup $len
1343 movdqa .Lxts_magic(%rip),$twmask
1345 pcmpgtd @tweak[5],$twtmp # broadcast upper bits
1347 for ($i=0;$i<4;$i++) {
1349 pshufd \$0x13,$twtmp,$twres
1351 movdqa @tweak[5],@tweak[$i]
1352 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1353 pand $twmask,$twres # isolate carry and residue
1354 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1355 pxor $twres,@tweak[5]
1365 jmp .Lxts_enc_grandloop
1368 .Lxts_enc_grandloop:
1369 pshufd \$0x13,$twtmp,$twres
1370 movdqa @tweak[5],@tweak[4]
1371 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1372 movdqu `16*0`($inp),$inout0 # load input
1373 pand $twmask,$twres # isolate carry and residue
1374 movdqu `16*1`($inp),$inout1
1375 pxor $twres,@tweak[5]
1377 movdqu `16*2`($inp),$inout2
1378 pxor @tweak[0],$inout0 # input^=tweak
1379 movdqu `16*3`($inp),$inout3
1380 pxor @tweak[1],$inout1
1381 movdqu `16*4`($inp),$inout4
1382 pxor @tweak[2],$inout2
1383 movdqu `16*5`($inp),$inout5
1384 lea `16*6`($inp),$inp
1385 pxor @tweak[3],$inout3
1386 $movkey ($key_),$rndkey0
1387 pxor @tweak[4],$inout4
1388 pxor @tweak[5],$inout5
1390 # inline _aesni_encrypt6 and interleave first and last rounds
1392 $movkey 16($key_),$rndkey1
1393 pxor $rndkey0,$inout0
1394 pxor $rndkey0,$inout1
1395 movdqa @tweak[0],`16*0`(%rsp) # put aside tweaks
1396 aesenc $rndkey1,$inout0
1398 pxor $rndkey0,$inout2
1399 movdqa @tweak[1],`16*1`(%rsp)
1400 aesenc $rndkey1,$inout1
1401 pxor $rndkey0,$inout3
1402 movdqa @tweak[2],`16*2`(%rsp)
1403 aesenc $rndkey1,$inout2
1404 pxor $rndkey0,$inout4
1405 movdqa @tweak[3],`16*3`(%rsp)
1406 aesenc $rndkey1,$inout3
1407 pxor $rndkey0,$inout5
1408 $movkey ($key),$rndkey0
1410 movdqa @tweak[4],`16*4`(%rsp)
1411 aesenc $rndkey1,$inout4
1412 movdqa @tweak[5],`16*5`(%rsp)
1413 aesenc $rndkey1,$inout5
1415 pcmpgtd @tweak[5],$twtmp
1416 jmp .Lxts_enc_loop6_enter
1420 aesenc $rndkey1,$inout0
1421 aesenc $rndkey1,$inout1
1423 aesenc $rndkey1,$inout2
1424 aesenc $rndkey1,$inout3
1425 aesenc $rndkey1,$inout4
1426 aesenc $rndkey1,$inout5
1427 .Lxts_enc_loop6_enter:
1428 $movkey 16($key),$rndkey1
1429 aesenc $rndkey0,$inout0
1430 aesenc $rndkey0,$inout1
1432 aesenc $rndkey0,$inout2
1433 aesenc $rndkey0,$inout3
1434 aesenc $rndkey0,$inout4
1435 aesenc $rndkey0,$inout5
1436 $movkey ($key),$rndkey0
1439 pshufd \$0x13,$twtmp,$twres
1441 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1442 aesenc $rndkey1,$inout0
1443 pand $twmask,$twres # isolate carry and residue
1444 aesenc $rndkey1,$inout1
1445 pcmpgtd @tweak[5],$twtmp # broadcast upper bits
1446 aesenc $rndkey1,$inout2
1447 pxor $twres,@tweak[5]
1448 aesenc $rndkey1,$inout3
1449 aesenc $rndkey1,$inout4
1450 aesenc $rndkey1,$inout5
1451 $movkey 16($key),$rndkey1
1453 pshufd \$0x13,$twtmp,$twres
1455 movdqa @tweak[5],@tweak[0]
1456 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1457 aesenc $rndkey0,$inout0
1458 pand $twmask,$twres # isolate carry and residue
1459 aesenc $rndkey0,$inout1
1460 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1461 aesenc $rndkey0,$inout2
1462 pxor $twres,@tweak[5]
1463 aesenc $rndkey0,$inout3
1464 aesenc $rndkey0,$inout4
1465 aesenc $rndkey0,$inout5
1466 $movkey 32($key),$rndkey0
1468 pshufd \$0x13,$twtmp,$twres
1470 movdqa @tweak[5],@tweak[1]
1471 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1472 aesenc $rndkey1,$inout0
1473 pand $twmask,$twres # isolate carry and residue
1474 aesenc $rndkey1,$inout1
1475 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1476 aesenc $rndkey1,$inout2
1477 pxor $twres,@tweak[5]
1478 aesenc $rndkey1,$inout3
1479 aesenc $rndkey1,$inout4
1480 aesenc $rndkey1,$inout5
1482 pshufd \$0x13,$twtmp,$twres
1484 movdqa @tweak[5],@tweak[2]
1485 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1486 aesenclast $rndkey0,$inout0
1487 pand $twmask,$twres # isolate carry and residue
1488 aesenclast $rndkey0,$inout1
1489 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1490 aesenclast $rndkey0,$inout2
1491 pxor $twres,@tweak[5]
1492 aesenclast $rndkey0,$inout3
1493 aesenclast $rndkey0,$inout4
1494 aesenclast $rndkey0,$inout5
1496 pshufd \$0x13,$twtmp,$twres
1498 movdqa @tweak[5],@tweak[3]
1499 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1500 xorps `16*0`(%rsp),$inout0 # output^=tweak
1501 pand $twmask,$twres # isolate carry and residue
1502 xorps `16*1`(%rsp),$inout1
1503 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1504 pxor $twres,@tweak[5]
1506 xorps `16*2`(%rsp),$inout2
1507 movups $inout0,`16*0`($out) # write output
1508 xorps `16*3`(%rsp),$inout3
1509 movups $inout1,`16*1`($out)
1510 xorps `16*4`(%rsp),$inout4
1511 movups $inout2,`16*2`($out)
1512 xorps `16*5`(%rsp),$inout5
1513 movups $inout3,`16*3`($out)
1514 mov $rnds_,$rounds # restore $rounds
1515 movups $inout4,`16*4`($out)
1516 movups $inout5,`16*5`($out)
1517 lea `16*6`($out),$out
1519 jnc .Lxts_enc_grandloop
1521 lea 3($rounds,$rounds),$rounds # restore original value
1522 mov $key_,$key # restore $key
1523 mov $rounds,$rnds_ # backup $rounds
1537 pshufd \$0x13,$twtmp,$twres
1538 movdqa @tweak[5],@tweak[4]
1539 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1540 movdqu ($inp),$inout0
1541 pand $twmask,$twres # isolate carry and residue
1542 movdqu 16*1($inp),$inout1
1543 pxor $twres,@tweak[5]
1545 movdqu 16*2($inp),$inout2
1546 pxor @tweak[0],$inout0
1547 movdqu 16*3($inp),$inout3
1548 pxor @tweak[1],$inout1
1549 movdqu 16*4($inp),$inout4
1551 pxor @tweak[2],$inout2
1552 pxor @tweak[3],$inout3
1553 pxor @tweak[4],$inout4
1555 call _aesni_encrypt6
1557 xorps @tweak[0],$inout0
1558 movdqa @tweak[5],@tweak[0]
1559 xorps @tweak[1],$inout1
1560 xorps @tweak[2],$inout2
1561 movdqu $inout0,($out)
1562 xorps @tweak[3],$inout3
1563 movdqu $inout1,16*1($out)
1564 xorps @tweak[4],$inout4
1565 movdqu $inout2,16*2($out)
1566 movdqu $inout3,16*3($out)
1567 movdqu $inout4,16*4($out)
1573 movups ($inp),$inout0
1575 xorps @tweak[0],$inout0
1577 &aesni_generate1("enc",$key,$rounds);
1579 xorps @tweak[0],$inout0
1580 movdqa @tweak[1],@tweak[0]
1581 movups $inout0,($out)
1587 movups ($inp),$inout0
1588 movups 16($inp),$inout1
1590 xorps @tweak[0],$inout0
1591 xorps @tweak[1],$inout1
1593 call _aesni_encrypt3
1595 xorps @tweak[0],$inout0
1596 movdqa @tweak[2],@tweak[0]
1597 xorps @tweak[1],$inout1
1598 movups $inout0,($out)
1599 movups $inout1,16*1($out)
1605 movups ($inp),$inout0
1606 movups 16*1($inp),$inout1
1607 movups 16*2($inp),$inout2
1609 xorps @tweak[0],$inout0
1610 xorps @tweak[1],$inout1
1611 xorps @tweak[2],$inout2
1613 call _aesni_encrypt3
1615 xorps @tweak[0],$inout0
1616 movdqa @tweak[3],@tweak[0]
1617 xorps @tweak[1],$inout1
1618 xorps @tweak[2],$inout2
1619 movups $inout0,($out)
1620 movups $inout1,16*1($out)
1621 movups $inout2,16*2($out)
1627 movups ($inp),$inout0
1628 movups 16*1($inp),$inout1
1629 movups 16*2($inp),$inout2
1630 xorps @tweak[0],$inout0
1631 movups 16*3($inp),$inout3
1633 xorps @tweak[1],$inout1
1634 xorps @tweak[2],$inout2
1635 xorps @tweak[3],$inout3
1637 call _aesni_encrypt4
1639 xorps @tweak[0],$inout0
1640 movdqa @tweak[5],@tweak[0]
1641 xorps @tweak[1],$inout1
1642 xorps @tweak[2],$inout2
1643 movups $inout0,($out)
1644 xorps @tweak[3],$inout3
1645 movups $inout1,16*1($out)
1646 movups $inout2,16*2($out)
1647 movups $inout3,16*3($out)
1658 movzb ($inp),%eax # borrow $rounds ...
1659 movzb -16($out),%ecx # ... and $key
1667 sub $len_,$out # rewind $out
1668 mov $key_,$key # restore $key
1669 mov $rnds_,$rounds # restore $rounds
1671 movups -16($out),$inout0
1672 xorps @tweak[0],$inout0
1674 &aesni_generate1("enc",$key,$rounds);
1676 xorps @tweak[0],$inout0
1677 movups $inout0,-16($out)
1681 $code.=<<___ if ($win64);
1682 movaps 0x60(%rsp),%xmm6
1683 movaps 0x70(%rsp),%xmm7
1684 movaps 0x80(%rsp),%xmm8
1685 movaps 0x90(%rsp),%xmm9
1686 movaps 0xa0(%rsp),%xmm10
1687 movaps 0xb0(%rsp),%xmm11
1688 movaps 0xc0(%rsp),%xmm12
1689 movaps 0xd0(%rsp),%xmm13
1690 movaps 0xe0(%rsp),%xmm14
1691 movaps 0xf0(%rsp),%xmm15
1698 .size aesni_xts_encrypt,.-aesni_xts_encrypt
1702 .globl aesni_xts_decrypt
1703 .type aesni_xts_decrypt,\@function,6
1708 sub \$$frame_size,%rsp
1709 and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
1711 $code.=<<___ if ($win64);
1712 movaps %xmm6,0x60(%rsp)
1713 movaps %xmm7,0x70(%rsp)
1714 movaps %xmm8,0x80(%rsp)
1715 movaps %xmm9,0x90(%rsp)
1716 movaps %xmm10,0xa0(%rsp)
1717 movaps %xmm11,0xb0(%rsp)
1718 movaps %xmm12,0xc0(%rsp)
1719 movaps %xmm13,0xd0(%rsp)
1720 movaps %xmm14,0xe0(%rsp)
1721 movaps %xmm15,0xf0(%rsp)
1726 movups ($ivp),@tweak[5] # load clear-text tweak
1727 mov 240($key2),$rounds # key2->rounds
1728 mov 240($key),$rnds_ # key1->rounds
1730 # generate the tweak
1731 &aesni_generate1("enc",$key2,$rounds,@tweak[5]);
1733 xor %eax,%eax # if ($len%16) len-=16;
1739 mov $key,$key_ # backup $key
1740 mov $rnds_,$rounds # backup $rounds
1741 mov $len,$len_ # backup $len
1744 movdqa .Lxts_magic(%rip),$twmask
1746 pcmpgtd @tweak[5],$twtmp # broadcast upper bits
1748 for ($i=0;$i<4;$i++) {
1750 pshufd \$0x13,$twtmp,$twres
1752 movdqa @tweak[5],@tweak[$i]
1753 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1754 pand $twmask,$twres # isolate carry and residue
1755 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1756 pxor $twres,@tweak[5]
1766 jmp .Lxts_dec_grandloop
1769 .Lxts_dec_grandloop:
1770 pshufd \$0x13,$twtmp,$twres
1771 movdqa @tweak[5],@tweak[4]
1772 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1773 movdqu `16*0`($inp),$inout0 # load input
1774 pand $twmask,$twres # isolate carry and residue
1775 movdqu `16*1`($inp),$inout1
1776 pxor $twres,@tweak[5]
1778 movdqu `16*2`($inp),$inout2
1779 pxor @tweak[0],$inout0 # input^=tweak
1780 movdqu `16*3`($inp),$inout3
1781 pxor @tweak[1],$inout1
1782 movdqu `16*4`($inp),$inout4
1783 pxor @tweak[2],$inout2
1784 movdqu `16*5`($inp),$inout5
1785 lea `16*6`($inp),$inp
1786 pxor @tweak[3],$inout3
1787 $movkey ($key_),$rndkey0
1788 pxor @tweak[4],$inout4
1789 pxor @tweak[5],$inout5
1791 # inline _aesni_decrypt6 and interleave first and last rounds
1793 $movkey 16($key_),$rndkey1
1794 pxor $rndkey0,$inout0
1795 pxor $rndkey0,$inout1
1796 movdqa @tweak[0],`16*0`(%rsp) # put aside tweaks
1797 aesdec $rndkey1,$inout0
1799 pxor $rndkey0,$inout2
1800 movdqa @tweak[1],`16*1`(%rsp)
1801 aesdec $rndkey1,$inout1
1802 pxor $rndkey0,$inout3
1803 movdqa @tweak[2],`16*2`(%rsp)
1804 aesdec $rndkey1,$inout2
1805 pxor $rndkey0,$inout4
1806 movdqa @tweak[3],`16*3`(%rsp)
1807 aesdec $rndkey1,$inout3
1808 pxor $rndkey0,$inout5
1809 $movkey ($key),$rndkey0
1811 movdqa @tweak[4],`16*4`(%rsp)
1812 aesdec $rndkey1,$inout4
1813 movdqa @tweak[5],`16*5`(%rsp)
1814 aesdec $rndkey1,$inout5
1816 pcmpgtd @tweak[5],$twtmp
1817 jmp .Lxts_dec_loop6_enter
1821 aesdec $rndkey1,$inout0
1822 aesdec $rndkey1,$inout1
1824 aesdec $rndkey1,$inout2
1825 aesdec $rndkey1,$inout3
1826 aesdec $rndkey1,$inout4
1827 aesdec $rndkey1,$inout5
1828 .Lxts_dec_loop6_enter:
1829 $movkey 16($key),$rndkey1
1830 aesdec $rndkey0,$inout0
1831 aesdec $rndkey0,$inout1
1833 aesdec $rndkey0,$inout2
1834 aesdec $rndkey0,$inout3
1835 aesdec $rndkey0,$inout4
1836 aesdec $rndkey0,$inout5
1837 $movkey ($key),$rndkey0
1840 pshufd \$0x13,$twtmp,$twres
1842 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1843 aesdec $rndkey1,$inout0
1844 pand $twmask,$twres # isolate carry and residue
1845 aesdec $rndkey1,$inout1
1846 pcmpgtd @tweak[5],$twtmp # broadcast upper bits
1847 aesdec $rndkey1,$inout2
1848 pxor $twres,@tweak[5]
1849 aesdec $rndkey1,$inout3
1850 aesdec $rndkey1,$inout4
1851 aesdec $rndkey1,$inout5
1852 $movkey 16($key),$rndkey1
1854 pshufd \$0x13,$twtmp,$twres
1856 movdqa @tweak[5],@tweak[0]
1857 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1858 aesdec $rndkey0,$inout0
1859 pand $twmask,$twres # isolate carry and residue
1860 aesdec $rndkey0,$inout1
1861 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1862 aesdec $rndkey0,$inout2
1863 pxor $twres,@tweak[5]
1864 aesdec $rndkey0,$inout3
1865 aesdec $rndkey0,$inout4
1866 aesdec $rndkey0,$inout5
1867 $movkey 32($key),$rndkey0
1869 pshufd \$0x13,$twtmp,$twres
1871 movdqa @tweak[5],@tweak[1]
1872 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1873 aesdec $rndkey1,$inout0
1874 pand $twmask,$twres # isolate carry and residue
1875 aesdec $rndkey1,$inout1
1876 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1877 aesdec $rndkey1,$inout2
1878 pxor $twres,@tweak[5]
1879 aesdec $rndkey1,$inout3
1880 aesdec $rndkey1,$inout4
1881 aesdec $rndkey1,$inout5
1883 pshufd \$0x13,$twtmp,$twres
1885 movdqa @tweak[5],@tweak[2]
1886 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1887 aesdeclast $rndkey0,$inout0
1888 pand $twmask,$twres # isolate carry and residue
1889 aesdeclast $rndkey0,$inout1
1890 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1891 aesdeclast $rndkey0,$inout2
1892 pxor $twres,@tweak[5]
1893 aesdeclast $rndkey0,$inout3
1894 aesdeclast $rndkey0,$inout4
1895 aesdeclast $rndkey0,$inout5
1897 pshufd \$0x13,$twtmp,$twres
1899 movdqa @tweak[5],@tweak[3]
1900 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1901 xorps `16*0`(%rsp),$inout0 # output^=tweak
1902 pand $twmask,$twres # isolate carry and residue
1903 xorps `16*1`(%rsp),$inout1
1904 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1905 pxor $twres,@tweak[5]
1907 xorps `16*2`(%rsp),$inout2
1908 movups $inout0,`16*0`($out) # write output
1909 xorps `16*3`(%rsp),$inout3
1910 movups $inout1,`16*1`($out)
1911 xorps `16*4`(%rsp),$inout4
1912 movups $inout2,`16*2`($out)
1913 xorps `16*5`(%rsp),$inout5
1914 movups $inout3,`16*3`($out)
1915 mov $rnds_,$rounds # restore $rounds
1916 movups $inout4,`16*4`($out)
1917 movups $inout5,`16*5`($out)
1918 lea `16*6`($out),$out
1920 jnc .Lxts_dec_grandloop
1922 lea 3($rounds,$rounds),$rounds # restore original value
1923 mov $key_,$key # restore $key
1924 mov $rounds,$rnds_ # backup $rounds
1938 pshufd \$0x13,$twtmp,$twres
1939 movdqa @tweak[5],@tweak[4]
1940 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1941 movdqu ($inp),$inout0
1942 pand $twmask,$twres # isolate carry and residue
1943 movdqu 16*1($inp),$inout1
1944 pxor $twres,@tweak[5]
1946 movdqu 16*2($inp),$inout2
1947 pxor @tweak[0],$inout0
1948 movdqu 16*3($inp),$inout3
1949 pxor @tweak[1],$inout1
1950 movdqu 16*4($inp),$inout4
1952 pxor @tweak[2],$inout2
1953 pxor @tweak[3],$inout3
1954 pxor @tweak[4],$inout4
1956 call _aesni_decrypt6
1958 xorps @tweak[0],$inout0
1959 xorps @tweak[1],$inout1
1960 xorps @tweak[2],$inout2
1961 movdqu $inout0,($out)
1962 xorps @tweak[3],$inout3
1963 movdqu $inout1,16*1($out)
1964 xorps @tweak[4],$inout4
1965 movdqu $inout2,16*2($out)
1967 movdqu $inout3,16*3($out)
1968 pcmpgtd @tweak[5],$twtmp
1969 movdqu $inout4,16*4($out)
1971 pshufd \$0x13,$twtmp,@tweak[1] # $twres
1975 movdqa @tweak[5],@tweak[0]
1976 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1977 pand $twmask,@tweak[1] # isolate carry and residue
1978 pxor @tweak[5],@tweak[1]
1983 movups ($inp),$inout0
1985 xorps @tweak[0],$inout0
1987 &aesni_generate1("dec",$key,$rounds);
1989 xorps @tweak[0],$inout0
1990 movdqa @tweak[1],@tweak[0]
1991 movups $inout0,($out)
1992 movdqa @tweak[2],@tweak[1]
1998 movups ($inp),$inout0
1999 movups 16($inp),$inout1
2001 xorps @tweak[0],$inout0
2002 xorps @tweak[1],$inout1
2004 call _aesni_decrypt3
2006 xorps @tweak[0],$inout0
2007 movdqa @tweak[2],@tweak[0]
2008 xorps @tweak[1],$inout1
2009 movdqa @tweak[3],@tweak[1]
2010 movups $inout0,($out)
2011 movups $inout1,16*1($out)
2017 movups ($inp),$inout0
2018 movups 16*1($inp),$inout1
2019 movups 16*2($inp),$inout2
2021 xorps @tweak[0],$inout0
2022 xorps @tweak[1],$inout1
2023 xorps @tweak[2],$inout2
2025 call _aesni_decrypt3
2027 xorps @tweak[0],$inout0
2028 movdqa @tweak[3],@tweak[0]
2029 xorps @tweak[1],$inout1
2030 movdqa @tweak[5],@tweak[1]
2031 xorps @tweak[2],$inout2
2032 movups $inout0,($out)
2033 movups $inout1,16*1($out)
2034 movups $inout2,16*2($out)
2040 pshufd \$0x13,$twtmp,$twres
2041 movdqa @tweak[5],@tweak[4]
2042 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
2043 movups ($inp),$inout0
2044 pand $twmask,$twres # isolate carry and residue
2045 movups 16*1($inp),$inout1
2046 pxor $twres,@tweak[5]
2048 movups 16*2($inp),$inout2
2049 xorps @tweak[0],$inout0
2050 movups 16*3($inp),$inout3
2052 xorps @tweak[1],$inout1
2053 xorps @tweak[2],$inout2
2054 xorps @tweak[3],$inout3
2056 call _aesni_decrypt4
2058 xorps @tweak[0],$inout0
2059 movdqa @tweak[4],@tweak[0]
2060 xorps @tweak[1],$inout1
2061 movdqa @tweak[5],@tweak[1]
2062 xorps @tweak[2],$inout2
2063 movups $inout0,($out)
2064 xorps @tweak[3],$inout3
2065 movups $inout1,16*1($out)
2066 movups $inout2,16*2($out)
2067 movups $inout3,16*3($out)
2077 mov $key_,$key # restore $key
2078 mov $rnds_,$rounds # restore $rounds
2080 movups ($inp),$inout0
2081 xorps @tweak[1],$inout0
2083 &aesni_generate1("dec",$key,$rounds);
2085 xorps @tweak[1],$inout0
2086 movups $inout0,($out)
2089 movzb 16($inp),%eax # borrow $rounds ...
2090 movzb ($out),%ecx # ... and $key
2098 sub $len_,$out # rewind $out
2099 mov $key_,$key # restore $key
2100 mov $rnds_,$rounds # restore $rounds
2102 movups ($out),$inout0
2103 xorps @tweak[0],$inout0
2105 &aesni_generate1("dec",$key,$rounds);
2107 xorps @tweak[0],$inout0
2108 movups $inout0,($out)
2112 $code.=<<___ if ($win64);
2113 movaps 0x60(%rsp),%xmm6
2114 movaps 0x70(%rsp),%xmm7
2115 movaps 0x80(%rsp),%xmm8
2116 movaps 0x90(%rsp),%xmm9
2117 movaps 0xa0(%rsp),%xmm10
2118 movaps 0xb0(%rsp),%xmm11
2119 movaps 0xc0(%rsp),%xmm12
2120 movaps 0xd0(%rsp),%xmm13
2121 movaps 0xe0(%rsp),%xmm14
2122 movaps 0xf0(%rsp),%xmm15
2129 .size aesni_xts_decrypt,.-aesni_xts_decrypt
2133 ########################################################################
2134 # void $PREFIX_cbc_encrypt (const void *inp, void *out,
2135 # size_t length, const AES_KEY *key,
2136 # unsigned char *ivp,const int enc);
2138 my $frame_size = 0x10 + ($win64?0x40:0); # used in decrypt
2140 .globl ${PREFIX}_cbc_encrypt
2141 .type ${PREFIX}_cbc_encrypt,\@function,6
2143 ${PREFIX}_cbc_encrypt:
2144 test $len,$len # check length
2147 mov 240($key),$rnds_ # key->rounds
2148 mov $key,$key_ # backup $key
2149 test %r9d,%r9d # 6th argument
2151 #--------------------------- CBC ENCRYPT ------------------------------#
2152 movups ($ivp),$inout0 # load iv as initial state
2160 movups ($inp),$inout1 # load input
2162 #xorps $inout1,$inout0
2164 &aesni_generate1("enc",$key,$rounds,$inout0,$inout1);
2166 mov $rnds_,$rounds # restore $rounds
2167 mov $key_,$key # restore $key
2168 movups $inout0,0($out) # store output
2174 movups $inout0,($ivp)
2178 mov $len,%rcx # zaps $key
2179 xchg $inp,$out # $inp is %rsi and $out is %rdi now
2180 .long 0x9066A4F3 # rep movsb
2181 mov \$16,%ecx # zero tail
2184 .long 0x9066AAF3 # rep stosb
2185 lea -16(%rdi),%rdi # rewind $out by 1 block
2186 mov $rnds_,$rounds # restore $rounds
2187 mov %rdi,%rsi # $inp and $out are the same
2188 mov $key_,$key # restore $key
2189 xor $len,$len # len=16
2190 jmp .Lcbc_enc_loop # one more spin
2191 \f#--------------------------- CBC DECRYPT ------------------------------#
2196 sub \$$frame_size,%rsp
2197 and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
2199 $code.=<<___ if ($win64);
2200 movaps %xmm6,0x10(%rsp)
2201 movaps %xmm7,0x20(%rsp)
2202 movaps %xmm8,0x30(%rsp)
2203 movaps %xmm9,0x40(%rsp)
2216 jmp .Lcbc_dec_loop8_enter
2219 movaps $rndkey0,(%rsp) # save IV
2220 movups $inout7,($out)
2222 .Lcbc_dec_loop8_enter:
2223 $movkey ($key),$rndkey0
2224 movups ($inp),$inout0 # load input
2225 movups 0x10($inp),$inout1
2226 $movkey 16($key),$rndkey1
2229 movdqu 0x20($inp),$inout2
2230 xorps $rndkey0,$inout0
2231 movdqu 0x30($inp),$inout3
2232 xorps $rndkey0,$inout1
2233 movdqu 0x40($inp),$inout4
2234 aesdec $rndkey1,$inout0
2235 pxor $rndkey0,$inout2
2236 movdqu 0x50($inp),$inout5
2237 aesdec $rndkey1,$inout1
2238 pxor $rndkey0,$inout3
2239 movdqu 0x60($inp),$inout6
2240 aesdec $rndkey1,$inout2
2241 pxor $rndkey0,$inout4
2242 movdqu 0x70($inp),$inout7
2243 aesdec $rndkey1,$inout3
2244 pxor $rndkey0,$inout5
2246 aesdec $rndkey1,$inout4
2247 pxor $rndkey0,$inout6
2248 aesdec $rndkey1,$inout5
2249 pxor $rndkey0,$inout7
2250 $movkey ($key),$rndkey0
2251 aesdec $rndkey1,$inout6
2252 aesdec $rndkey1,$inout7
2253 $movkey 16($key),$rndkey1
2255 call .Ldec_loop8_enter
2257 movups ($inp),$rndkey1 # re-load input
2258 movups 0x10($inp),$rndkey0
2259 xorps (%rsp),$inout0 # ^= IV
2260 xorps $rndkey1,$inout1
2261 movups 0x20($inp),$rndkey1
2262 xorps $rndkey0,$inout2
2263 movups 0x30($inp),$rndkey0
2264 xorps $rndkey1,$inout3
2265 movups 0x40($inp),$rndkey1
2266 xorps $rndkey0,$inout4
2267 movups 0x50($inp),$rndkey0
2268 xorps $rndkey1,$inout5
2269 movups 0x60($inp),$rndkey1
2270 xorps $rndkey0,$inout6
2271 movups 0x70($inp),$rndkey0 # IV
2272 xorps $rndkey1,$inout7
2273 movups $inout0,($out)
2274 movups $inout1,0x10($out)
2275 movups $inout2,0x20($out)
2276 movups $inout3,0x30($out)
2277 mov $rnds_,$rounds # restore $rounds
2278 movups $inout4,0x40($out)
2279 mov $key_,$key # restore $key
2280 movups $inout5,0x50($out)
2282 movups $inout6,0x60($out)
2287 movaps $inout7,$inout0
2290 jle .Lcbc_dec_tail_collected
2291 movups $inout0,($out)
2292 lea 1($rnds_,$rnds_),$rounds
2295 movups ($inp),$inout0
2300 movups 0x10($inp),$inout1
2305 movups 0x20($inp),$inout2
2310 movups 0x30($inp),$inout3
2314 movups 0x40($inp),$inout4
2318 movups 0x50($inp),$inout5
2322 movups 0x60($inp),$inout6
2323 movaps $iv,(%rsp) # save IV
2324 call _aesni_decrypt8
2325 movups ($inp),$rndkey1
2326 movups 0x10($inp),$rndkey0
2327 xorps (%rsp),$inout0 # ^= IV
2328 xorps $rndkey1,$inout1
2329 movups 0x20($inp),$rndkey1
2330 xorps $rndkey0,$inout2
2331 movups 0x30($inp),$rndkey0
2332 xorps $rndkey1,$inout3
2333 movups 0x40($inp),$rndkey1
2334 xorps $rndkey0,$inout4
2335 movups 0x50($inp),$rndkey0
2336 xorps $rndkey1,$inout5
2337 movups 0x60($inp),$iv # IV
2338 xorps $rndkey0,$inout6
2339 movups $inout0,($out)
2340 movups $inout1,0x10($out)
2341 movups $inout2,0x20($out)
2342 movups $inout3,0x30($out)
2343 movups $inout4,0x40($out)
2344 movups $inout5,0x50($out)
2346 movaps $inout6,$inout0
2348 jmp .Lcbc_dec_tail_collected
2352 &aesni_generate1("dec",$key,$rounds);
2357 jmp .Lcbc_dec_tail_collected
2360 xorps $inout2,$inout2
2361 call _aesni_decrypt3
2364 movups $inout0,($out)
2366 movaps $inout1,$inout0
2369 jmp .Lcbc_dec_tail_collected
2372 call _aesni_decrypt3
2375 movups $inout0,($out)
2377 movups $inout1,0x10($out)
2379 movaps $inout2,$inout0
2382 jmp .Lcbc_dec_tail_collected
2385 call _aesni_decrypt4
2387 movups 0x30($inp),$iv
2389 movups $inout0,($out)
2391 movups $inout1,0x10($out)
2393 movups $inout2,0x20($out)
2394 movaps $inout3,$inout0
2397 jmp .Lcbc_dec_tail_collected
2400 xorps $inout5,$inout5
2401 call _aesni_decrypt6
2402 movups 0x10($inp),$rndkey1
2403 movups 0x20($inp),$rndkey0
2406 xorps $rndkey1,$inout2
2407 movups 0x30($inp),$rndkey1
2408 xorps $rndkey0,$inout3
2409 movups 0x40($inp),$iv
2410 xorps $rndkey1,$inout4
2411 movups $inout0,($out)
2412 movups $inout1,0x10($out)
2413 movups $inout2,0x20($out)
2414 movups $inout3,0x30($out)
2416 movaps $inout4,$inout0
2418 jmp .Lcbc_dec_tail_collected
2421 call _aesni_decrypt6
2422 movups 0x10($inp),$rndkey1
2423 movups 0x20($inp),$rndkey0
2426 xorps $rndkey1,$inout2
2427 movups 0x30($inp),$rndkey1
2428 xorps $rndkey0,$inout3
2429 movups 0x40($inp),$rndkey0
2430 xorps $rndkey1,$inout4
2431 movups 0x50($inp),$iv
2432 xorps $rndkey0,$inout5
2433 movups $inout0,($out)
2434 movups $inout1,0x10($out)
2435 movups $inout2,0x20($out)
2436 movups $inout3,0x30($out)
2437 movups $inout4,0x40($out)
2439 movaps $inout5,$inout0
2441 jmp .Lcbc_dec_tail_collected
2443 .Lcbc_dec_tail_collected:
2446 jnz .Lcbc_dec_tail_partial
2447 movups $inout0,($out)
2450 .Lcbc_dec_tail_partial:
2451 movaps $inout0,(%rsp)
2456 .long 0x9066A4F3 # rep movsb
2460 $code.=<<___ if ($win64);
2461 movaps 0x10(%rsp),%xmm6
2462 movaps 0x20(%rsp),%xmm7
2463 movaps 0x30(%rsp),%xmm8
2464 movaps 0x40(%rsp),%xmm9
2471 .size ${PREFIX}_cbc_encrypt,.-${PREFIX}_cbc_encrypt
2474 # int $PREFIX_set_[en|de]crypt_key (const unsigned char *userKey,
2475 # int bits, AES_KEY *key)
2476 { my ($inp,$bits,$key) = @_4args;
2480 .globl ${PREFIX}_set_decrypt_key
2481 .type ${PREFIX}_set_decrypt_key,\@abi-omnipotent
2483 ${PREFIX}_set_decrypt_key:
2484 .byte 0x48,0x83,0xEC,0x08 # sub rsp,8
2485 call __aesni_set_encrypt_key
2486 shl \$4,$bits # rounds-1 after _aesni_set_encrypt_key
2489 lea 16($key,$bits),$inp # points at the end of key schedule
2491 $movkey ($key),%xmm0 # just swap
2492 $movkey ($inp),%xmm1
2493 $movkey %xmm0,($inp)
2494 $movkey %xmm1,($key)
2499 $movkey ($key),%xmm0 # swap and inverse
2500 $movkey ($inp),%xmm1
2505 $movkey %xmm0,16($inp)
2506 $movkey %xmm1,-16($key)
2508 ja .Ldec_key_inverse
2510 $movkey ($key),%xmm0 # inverse middle
2512 $movkey %xmm0,($inp)
2516 .LSEH_end_set_decrypt_key:
2517 .size ${PREFIX}_set_decrypt_key,.-${PREFIX}_set_decrypt_key
2520 # This is based on submission by
2522 # Huang Ying <ying.huang@intel.com>
2523 # Vinodh Gopal <vinodh.gopal@intel.com>
2526 # Agressively optimized in respect to aeskeygenassist's critical path
2527 # and is contained in %xmm0-5 to meet Win64 ABI requirement.
2530 .globl ${PREFIX}_set_encrypt_key
2531 .type ${PREFIX}_set_encrypt_key,\@abi-omnipotent
2533 ${PREFIX}_set_encrypt_key:
2534 __aesni_set_encrypt_key:
2535 .byte 0x48,0x83,0xEC,0x08 # sub rsp,8
2542 movups ($inp),%xmm0 # pull first 128 bits of *userKey
2543 xorps %xmm4,%xmm4 # low dword of xmm4 is assumed 0
2553 mov \$9,$bits # 10 rounds for 128-bit key
2554 $movkey %xmm0,($key) # round 0
2555 aeskeygenassist \$0x1,%xmm0,%xmm1 # round 1
2556 call .Lkey_expansion_128_cold
2557 aeskeygenassist \$0x2,%xmm0,%xmm1 # round 2
2558 call .Lkey_expansion_128
2559 aeskeygenassist \$0x4,%xmm0,%xmm1 # round 3
2560 call .Lkey_expansion_128
2561 aeskeygenassist \$0x8,%xmm0,%xmm1 # round 4
2562 call .Lkey_expansion_128
2563 aeskeygenassist \$0x10,%xmm0,%xmm1 # round 5
2564 call .Lkey_expansion_128
2565 aeskeygenassist \$0x20,%xmm0,%xmm1 # round 6
2566 call .Lkey_expansion_128
2567 aeskeygenassist \$0x40,%xmm0,%xmm1 # round 7
2568 call .Lkey_expansion_128
2569 aeskeygenassist \$0x80,%xmm0,%xmm1 # round 8
2570 call .Lkey_expansion_128
2571 aeskeygenassist \$0x1b,%xmm0,%xmm1 # round 9
2572 call .Lkey_expansion_128
2573 aeskeygenassist \$0x36,%xmm0,%xmm1 # round 10
2574 call .Lkey_expansion_128
2575 $movkey %xmm0,(%rax)
2576 mov $bits,80(%rax) # 240(%rdx)
2582 movq 16($inp),%xmm2 # remaining 1/3 of *userKey
2583 mov \$11,$bits # 12 rounds for 192
2584 $movkey %xmm0,($key) # round 0
2585 aeskeygenassist \$0x1,%xmm2,%xmm1 # round 1,2
2586 call .Lkey_expansion_192a_cold
2587 aeskeygenassist \$0x2,%xmm2,%xmm1 # round 2,3
2588 call .Lkey_expansion_192b
2589 aeskeygenassist \$0x4,%xmm2,%xmm1 # round 4,5
2590 call .Lkey_expansion_192a
2591 aeskeygenassist \$0x8,%xmm2,%xmm1 # round 5,6
2592 call .Lkey_expansion_192b
2593 aeskeygenassist \$0x10,%xmm2,%xmm1 # round 7,8
2594 call .Lkey_expansion_192a
2595 aeskeygenassist \$0x20,%xmm2,%xmm1 # round 8,9
2596 call .Lkey_expansion_192b
2597 aeskeygenassist \$0x40,%xmm2,%xmm1 # round 10,11
2598 call .Lkey_expansion_192a
2599 aeskeygenassist \$0x80,%xmm2,%xmm1 # round 11,12
2600 call .Lkey_expansion_192b
2601 $movkey %xmm0,(%rax)
2602 mov $bits,48(%rax) # 240(%rdx)
2608 movups 16($inp),%xmm2 # remaning half of *userKey
2609 mov \$13,$bits # 14 rounds for 256
2611 $movkey %xmm0,($key) # round 0
2612 $movkey %xmm2,16($key) # round 1
2613 aeskeygenassist \$0x1,%xmm2,%xmm1 # round 2
2614 call .Lkey_expansion_256a_cold
2615 aeskeygenassist \$0x1,%xmm0,%xmm1 # round 3
2616 call .Lkey_expansion_256b
2617 aeskeygenassist \$0x2,%xmm2,%xmm1 # round 4
2618 call .Lkey_expansion_256a
2619 aeskeygenassist \$0x2,%xmm0,%xmm1 # round 5
2620 call .Lkey_expansion_256b
2621 aeskeygenassist \$0x4,%xmm2,%xmm1 # round 6
2622 call .Lkey_expansion_256a
2623 aeskeygenassist \$0x4,%xmm0,%xmm1 # round 7
2624 call .Lkey_expansion_256b
2625 aeskeygenassist \$0x8,%xmm2,%xmm1 # round 8
2626 call .Lkey_expansion_256a
2627 aeskeygenassist \$0x8,%xmm0,%xmm1 # round 9
2628 call .Lkey_expansion_256b
2629 aeskeygenassist \$0x10,%xmm2,%xmm1 # round 10
2630 call .Lkey_expansion_256a
2631 aeskeygenassist \$0x10,%xmm0,%xmm1 # round 11
2632 call .Lkey_expansion_256b
2633 aeskeygenassist \$0x20,%xmm2,%xmm1 # round 12
2634 call .Lkey_expansion_256a
2635 aeskeygenassist \$0x20,%xmm0,%xmm1 # round 13
2636 call .Lkey_expansion_256b
2637 aeskeygenassist \$0x40,%xmm2,%xmm1 # round 14
2638 call .Lkey_expansion_256a
2639 $movkey %xmm0,(%rax)
2640 mov $bits,16(%rax) # 240(%rdx)
2650 .LSEH_end_set_encrypt_key:
2653 .Lkey_expansion_128:
2654 $movkey %xmm0,(%rax)
2656 .Lkey_expansion_128_cold:
2657 shufps \$0b00010000,%xmm0,%xmm4
2659 shufps \$0b10001100,%xmm0,%xmm4
2661 shufps \$0b11111111,%xmm1,%xmm1 # critical path
2666 .Lkey_expansion_192a:
2667 $movkey %xmm0,(%rax)
2669 .Lkey_expansion_192a_cold:
2671 .Lkey_expansion_192b_warm:
2672 shufps \$0b00010000,%xmm0,%xmm4
2675 shufps \$0b10001100,%xmm0,%xmm4
2678 pshufd \$0b01010101,%xmm1,%xmm1 # critical path
2681 pshufd \$0b11111111,%xmm0,%xmm3
2686 .Lkey_expansion_192b:
2688 shufps \$0b01000100,%xmm0,%xmm5
2689 $movkey %xmm5,(%rax)
2690 shufps \$0b01001110,%xmm2,%xmm3
2691 $movkey %xmm3,16(%rax)
2693 jmp .Lkey_expansion_192b_warm
2696 .Lkey_expansion_256a:
2697 $movkey %xmm2,(%rax)
2699 .Lkey_expansion_256a_cold:
2700 shufps \$0b00010000,%xmm0,%xmm4
2702 shufps \$0b10001100,%xmm0,%xmm4
2704 shufps \$0b11111111,%xmm1,%xmm1 # critical path
2709 .Lkey_expansion_256b:
2710 $movkey %xmm0,(%rax)
2713 shufps \$0b00010000,%xmm2,%xmm4
2715 shufps \$0b10001100,%xmm2,%xmm4
2717 shufps \$0b10101010,%xmm1,%xmm1 # critical path
2720 .size ${PREFIX}_set_encrypt_key,.-${PREFIX}_set_encrypt_key
2721 .size __aesni_set_encrypt_key,.-__aesni_set_encrypt_key
2728 .byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
2736 .asciz "AES for Intel AES-NI, CRYPTOGAMS by <appro\@openssl.org>"
2740 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
2741 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
2749 .extern __imp_RtlVirtualUnwind
2751 $code.=<<___ if ($PREFIX eq "aesni");
2752 .type ecb_se_handler,\@abi-omnipotent
2766 mov 152($context),%rax # pull context->Rsp
2768 jmp .Lcommon_seh_tail
2769 .size ecb_se_handler,.-ecb_se_handler
2771 .type ccm64_se_handler,\@abi-omnipotent
2785 mov 120($context),%rax # pull context->Rax
2786 mov 248($context),%rbx # pull context->Rip
2788 mov 8($disp),%rsi # disp->ImageBase
2789 mov 56($disp),%r11 # disp->HandlerData
2791 mov 0(%r11),%r10d # HandlerData[0]
2792 lea (%rsi,%r10),%r10 # prologue label
2793 cmp %r10,%rbx # context->Rip<prologue label
2794 jb .Lcommon_seh_tail
2796 mov 152($context),%rax # pull context->Rsp
2798 mov 4(%r11),%r10d # HandlerData[1]
2799 lea (%rsi,%r10),%r10 # epilogue label
2800 cmp %r10,%rbx # context->Rip>=epilogue label
2801 jae .Lcommon_seh_tail
2803 lea 0(%rax),%rsi # %xmm save area
2804 lea 512($context),%rdi # &context.Xmm6
2805 mov \$8,%ecx # 4*sizeof(%xmm0)/sizeof(%rax)
2806 .long 0xa548f3fc # cld; rep movsq
2807 lea 0x58(%rax),%rax # adjust stack pointer
2809 jmp .Lcommon_seh_tail
2810 .size ccm64_se_handler,.-ccm64_se_handler
2812 .type ctr32_se_handler,\@abi-omnipotent
2826 mov 120($context),%rax # pull context->Rax
2827 mov 248($context),%rbx # pull context->Rip
2829 lea .Lctr32_body(%rip),%r10
2830 cmp %r10,%rbx # context->Rip<"prologue" label
2831 jb .Lcommon_seh_tail
2833 mov 152($context),%rax # pull context->Rsp
2835 lea .Lctr32_ret(%rip),%r10
2837 jae .Lcommon_seh_tail
2839 lea 0x20(%rax),%rsi # %xmm save area
2840 lea 512($context),%rdi # &context.Xmm6
2841 mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
2842 .long 0xa548f3fc # cld; rep movsq
2844 jmp .Lcommon_rbp_tail
2845 .size ctr32_se_handler,.-ctr32_se_handler
2847 .type xts_se_handler,\@abi-omnipotent
2861 mov 120($context),%rax # pull context->Rax
2862 mov 248($context),%rbx # pull context->Rip
2864 mov 8($disp),%rsi # disp->ImageBase
2865 mov 56($disp),%r11 # disp->HandlerData
2867 mov 0(%r11),%r10d # HandlerData[0]
2868 lea (%rsi,%r10),%r10 # prologue lable
2869 cmp %r10,%rbx # context->Rip<prologue label
2870 jb .Lcommon_seh_tail
2872 mov 152($context),%rax # pull context->Rsp
2874 mov 4(%r11),%r10d # HandlerData[1]
2875 lea (%rsi,%r10),%r10 # epilogue label
2876 cmp %r10,%rbx # context->Rip>=epilogue label
2877 jae .Lcommon_seh_tail
2879 lea 0x60(%rax),%rsi # %xmm save area
2880 lea 512($context),%rdi # & context.Xmm6
2881 mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
2882 .long 0xa548f3fc # cld; rep movsq
2884 jmp .Lcommon_rbp_tail
2885 .size xts_se_handler,.-xts_se_handler
2888 .type cbc_se_handler,\@abi-omnipotent
2902 mov 152($context),%rax # pull context->Rsp
2903 mov 248($context),%rbx # pull context->Rip
2905 lea .Lcbc_decrypt(%rip),%r10
2906 cmp %r10,%rbx # context->Rip<"prologue" label
2907 jb .Lcommon_seh_tail
2909 lea .Lcbc_decrypt_body(%rip),%r10
2910 cmp %r10,%rbx # context->Rip<cbc_decrypt_body
2911 jb .Lrestore_cbc_rax
2913 lea .Lcbc_ret(%rip),%r10
2914 cmp %r10,%rbx # context->Rip>="epilogue" label
2915 jae .Lcommon_seh_tail
2917 lea 16(%rax),%rsi # %xmm save area
2918 lea 512($context),%rdi # &context.Xmm6
2919 mov \$8,%ecx # 4*sizeof(%xmm0)/sizeof(%rax)
2920 .long 0xa548f3fc # cld; rep movsq
2923 mov 160($context),%rax # pull context->Rbp
2924 mov (%rax),%rbp # restore saved %rbp
2925 lea 8(%rax),%rax # adjust stack pointer
2926 mov %rbp,160($context) # restore context->Rbp
2927 jmp .Lcommon_seh_tail
2930 mov 120($context),%rax
2935 mov %rax,152($context) # restore context->Rsp
2936 mov %rsi,168($context) # restore context->Rsi
2937 mov %rdi,176($context) # restore context->Rdi
2939 mov 40($disp),%rdi # disp->ContextRecord
2940 mov $context,%rsi # context
2941 mov \$154,%ecx # sizeof(CONTEXT)
2942 .long 0xa548f3fc # cld; rep movsq
2945 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
2946 mov 8(%rsi),%rdx # arg2, disp->ImageBase
2947 mov 0(%rsi),%r8 # arg3, disp->ControlPc
2948 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
2949 mov 40(%rsi),%r10 # disp->ContextRecord
2950 lea 56(%rsi),%r11 # &disp->HandlerData
2951 lea 24(%rsi),%r12 # &disp->EstablisherFrame
2952 mov %r10,32(%rsp) # arg5
2953 mov %r11,40(%rsp) # arg6
2954 mov %r12,48(%rsp) # arg7
2955 mov %rcx,56(%rsp) # arg8, (NULL)
2956 call *__imp_RtlVirtualUnwind(%rip)
2958 mov \$1,%eax # ExceptionContinueSearch
2970 .size cbc_se_handler,.-cbc_se_handler
2975 $code.=<<___ if ($PREFIX eq "aesni");
2976 .rva .LSEH_begin_aesni_ecb_encrypt
2977 .rva .LSEH_end_aesni_ecb_encrypt
2980 .rva .LSEH_begin_aesni_ccm64_encrypt_blocks
2981 .rva .LSEH_end_aesni_ccm64_encrypt_blocks
2982 .rva .LSEH_info_ccm64_enc
2984 .rva .LSEH_begin_aesni_ccm64_decrypt_blocks
2985 .rva .LSEH_end_aesni_ccm64_decrypt_blocks
2986 .rva .LSEH_info_ccm64_dec
2988 .rva .LSEH_begin_aesni_ctr32_encrypt_blocks
2989 .rva .LSEH_end_aesni_ctr32_encrypt_blocks
2990 .rva .LSEH_info_ctr32
2992 .rva .LSEH_begin_aesni_xts_encrypt
2993 .rva .LSEH_end_aesni_xts_encrypt
2994 .rva .LSEH_info_xts_enc
2996 .rva .LSEH_begin_aesni_xts_decrypt
2997 .rva .LSEH_end_aesni_xts_decrypt
2998 .rva .LSEH_info_xts_dec
3001 .rva .LSEH_begin_${PREFIX}_cbc_encrypt
3002 .rva .LSEH_end_${PREFIX}_cbc_encrypt
3005 .rva ${PREFIX}_set_decrypt_key
3006 .rva .LSEH_end_set_decrypt_key
3009 .rva ${PREFIX}_set_encrypt_key
3010 .rva .LSEH_end_set_encrypt_key
3015 $code.=<<___ if ($PREFIX eq "aesni");
3019 .LSEH_info_ccm64_enc:
3021 .rva ccm64_se_handler
3022 .rva .Lccm64_enc_body,.Lccm64_enc_ret # HandlerData[]
3023 .LSEH_info_ccm64_dec:
3025 .rva ccm64_se_handler
3026 .rva .Lccm64_dec_body,.Lccm64_dec_ret # HandlerData[]
3029 .rva ctr32_se_handler
3033 .rva .Lxts_enc_body,.Lxts_enc_epilogue # HandlerData[]
3037 .rva .Lxts_dec_body,.Lxts_dec_epilogue # HandlerData[]
3044 .byte 0x01,0x04,0x01,0x00
3045 .byte 0x04,0x02,0x00,0x00 # sub rsp,8
3050 local *opcode=shift;
3054 $rex|=0x04 if($dst>=8);
3055 $rex|=0x01 if($src>=8);
3056 push @opcode,$rex|0x40 if($rex);
3063 if ($line=~/(aeskeygenassist)\s+\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
3064 rex(\@opcode,$4,$3);
3065 push @opcode,0x0f,0x3a,0xdf;
3066 push @opcode,0xc0|($3&7)|(($4&7)<<3); # ModR/M
3068 push @opcode,$c=~/^0/?oct($c):$c;
3069 return ".byte\t".join(',',@opcode);
3071 elsif ($line=~/(aes[a-z]+)\s+%xmm([0-9]+),\s*%xmm([0-9]+)/) {
3074 "aesenc" => 0xdc, "aesenclast" => 0xdd,
3075 "aesdec" => 0xde, "aesdeclast" => 0xdf
3077 return undef if (!defined($opcodelet{$1}));
3078 rex(\@opcode,$3,$2);
3079 push @opcode,0x0f,0x38,$opcodelet{$1};
3080 push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
3081 return ".byte\t".join(',',@opcode);
3086 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
3087 $code =~ s/\b(aes.*%xmm[0-9]+).*$/aesni($1)/gem;