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 ######################################################################
161 # For reference, AMD Bulldozer spends 5.77 cycles per byte processed
162 # with 128-bit key in CBC encrypt and 0.76 cycles in CBC decrypt, 0.70
163 # in ECB, 0.76 in CTR, 0.95 in XTS... This means that aes[enc|dec]
164 # instruction latency is 9 cycles and that they can be issued every
167 $PREFIX="aesni"; # if $PREFIX is set to "AES", the script
168 # generates drop-in replacement for
169 # crypto/aes/asm/aes-x86_64.pl:-)
173 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
175 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
177 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
178 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
179 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
180 die "can't locate x86_64-xlate.pl";
182 open STDOUT,"| \"$^X\" $xlate $flavour $output";
184 $movkey = $PREFIX eq "aesni" ? "movups" : "movups";
185 @_4args=$win64? ("%rcx","%rdx","%r8", "%r9") : # Win64 order
186 ("%rdi","%rsi","%rdx","%rcx"); # Unix order
190 $rounds="%eax"; # input to and changed by aesni_[en|de]cryptN !!!
191 # this is natural Unix argument order for public $PREFIX_[ecb|cbc]_encrypt ...
195 $key="%rcx"; # input to and changed by aesni_[en|de]cryptN !!!
196 $ivp="%r8"; # cbc, ctr, ...
198 $rnds_="%r10d"; # backup copy for $rounds
199 $key_="%r11"; # backup copy for $key
201 # %xmm register layout
202 $rndkey0="%xmm0"; $rndkey1="%xmm1";
203 $inout0="%xmm2"; $inout1="%xmm3";
204 $inout2="%xmm4"; $inout3="%xmm5";
205 $inout4="%xmm6"; $inout5="%xmm7";
206 $inout6="%xmm8"; $inout7="%xmm9";
208 $in2="%xmm6"; $in1="%xmm7"; # used in CBC decrypt, CTR, ...
209 $in0="%xmm8"; $iv="%xmm9";
211 # Inline version of internal aesni_[en|de]crypt1.
213 # Why folded loop? Because aes[enc|dec] is slow enough to accommodate
214 # cycles which take care of loop variables...
216 sub aesni_generate1 {
217 my ($p,$key,$rounds,$inout,$ivec)=@_; $inout=$inout0 if (!defined($inout));
220 $movkey ($key),$rndkey0
221 $movkey 16($key),$rndkey1
223 $code.=<<___ if (defined($ivec));
228 $code.=<<___ if (!defined($ivec));
230 xorps $rndkey0,$inout
234 aes${p} $rndkey1,$inout
236 $movkey ($key),$rndkey1
238 jnz .Loop_${p}1_$sn # loop body is 16 bytes
239 aes${p}last $rndkey1,$inout
242 # void $PREFIX_[en|de]crypt (const void *inp,void *out,const AES_KEY *key);
244 { my ($inp,$out,$key) = @_4args;
247 .globl ${PREFIX}_encrypt
248 .type ${PREFIX}_encrypt,\@abi-omnipotent
251 movups ($inp),$inout0 # load input
252 mov 240($key),$rounds # key->rounds
254 &aesni_generate1("enc",$key,$rounds);
256 movups $inout0,($out) # output
258 .size ${PREFIX}_encrypt,.-${PREFIX}_encrypt
260 .globl ${PREFIX}_decrypt
261 .type ${PREFIX}_decrypt,\@abi-omnipotent
264 movups ($inp),$inout0 # load input
265 mov 240($key),$rounds # key->rounds
267 &aesni_generate1("dec",$key,$rounds);
269 movups $inout0,($out) # output
271 .size ${PREFIX}_decrypt, .-${PREFIX}_decrypt
275 # _aesni_[en|de]cryptN are private interfaces, N denotes interleave
276 # factor. Why 3x subroutine were originally used in loops? Even though
277 # aes[enc|dec] latency was originally 6, it could be scheduled only
278 # every *2nd* cycle. Thus 3x interleave was the one providing optimal
279 # utilization, i.e. when subroutine's throughput is virtually same as
280 # of non-interleaved subroutine [for number of input blocks up to 3].
281 # This is why it makes no sense to implement 2x subroutine.
282 # aes[enc|dec] latency in next processor generation is 8, but the
283 # instructions can be scheduled every cycle. Optimal interleave for
284 # new processor is therefore 8x...
285 sub aesni_generate3 {
287 # As already mentioned it takes in $key and $rounds, which are *not*
288 # preserved. $inout[0-2] is cipher/clear text...
290 .type _aesni_${dir}rypt3,\@abi-omnipotent
293 $movkey ($key),$rndkey0
295 $movkey 16($key),$rndkey1
297 xorps $rndkey0,$inout0
298 xorps $rndkey0,$inout1
299 xorps $rndkey0,$inout2
300 $movkey ($key),$rndkey0
303 aes${dir} $rndkey1,$inout0
304 aes${dir} $rndkey1,$inout1
306 aes${dir} $rndkey1,$inout2
307 $movkey 16($key),$rndkey1
308 aes${dir} $rndkey0,$inout0
309 aes${dir} $rndkey0,$inout1
311 aes${dir} $rndkey0,$inout2
312 $movkey ($key),$rndkey0
315 aes${dir} $rndkey1,$inout0
316 aes${dir} $rndkey1,$inout1
317 aes${dir} $rndkey1,$inout2
318 aes${dir}last $rndkey0,$inout0
319 aes${dir}last $rndkey0,$inout1
320 aes${dir}last $rndkey0,$inout2
322 .size _aesni_${dir}rypt3,.-_aesni_${dir}rypt3
325 # 4x interleave is implemented to improve small block performance,
326 # most notably [and naturally] 4 block by ~30%. One can argue that one
327 # should have implemented 5x as well, but improvement would be <20%,
328 # so it's not worth it...
329 sub aesni_generate4 {
331 # As already mentioned it takes in $key and $rounds, which are *not*
332 # preserved. $inout[0-3] is cipher/clear text...
334 .type _aesni_${dir}rypt4,\@abi-omnipotent
337 $movkey ($key),$rndkey0
339 $movkey 16($key),$rndkey1
341 xorps $rndkey0,$inout0
342 xorps $rndkey0,$inout1
343 xorps $rndkey0,$inout2
344 xorps $rndkey0,$inout3
345 $movkey ($key),$rndkey0
348 aes${dir} $rndkey1,$inout0
349 aes${dir} $rndkey1,$inout1
351 aes${dir} $rndkey1,$inout2
352 aes${dir} $rndkey1,$inout3
353 $movkey 16($key),$rndkey1
354 aes${dir} $rndkey0,$inout0
355 aes${dir} $rndkey0,$inout1
357 aes${dir} $rndkey0,$inout2
358 aes${dir} $rndkey0,$inout3
359 $movkey ($key),$rndkey0
362 aes${dir} $rndkey1,$inout0
363 aes${dir} $rndkey1,$inout1
364 aes${dir} $rndkey1,$inout2
365 aes${dir} $rndkey1,$inout3
366 aes${dir}last $rndkey0,$inout0
367 aes${dir}last $rndkey0,$inout1
368 aes${dir}last $rndkey0,$inout2
369 aes${dir}last $rndkey0,$inout3
371 .size _aesni_${dir}rypt4,.-_aesni_${dir}rypt4
374 sub aesni_generate6 {
376 # As already mentioned it takes in $key and $rounds, which are *not*
377 # preserved. $inout[0-5] is cipher/clear text...
379 .type _aesni_${dir}rypt6,\@abi-omnipotent
382 $movkey ($key),$rndkey0
384 $movkey 16($key),$rndkey1
386 xorps $rndkey0,$inout0
387 pxor $rndkey0,$inout1
388 aes${dir} $rndkey1,$inout0
389 pxor $rndkey0,$inout2
390 aes${dir} $rndkey1,$inout1
391 pxor $rndkey0,$inout3
392 aes${dir} $rndkey1,$inout2
393 pxor $rndkey0,$inout4
394 aes${dir} $rndkey1,$inout3
395 pxor $rndkey0,$inout5
397 aes${dir} $rndkey1,$inout4
398 $movkey ($key),$rndkey0
399 aes${dir} $rndkey1,$inout5
400 jmp .L${dir}_loop6_enter
403 aes${dir} $rndkey1,$inout0
404 aes${dir} $rndkey1,$inout1
406 aes${dir} $rndkey1,$inout2
407 aes${dir} $rndkey1,$inout3
408 aes${dir} $rndkey1,$inout4
409 aes${dir} $rndkey1,$inout5
410 .L${dir}_loop6_enter: # happens to be 16-byte aligned
411 $movkey 16($key),$rndkey1
412 aes${dir} $rndkey0,$inout0
413 aes${dir} $rndkey0,$inout1
415 aes${dir} $rndkey0,$inout2
416 aes${dir} $rndkey0,$inout3
417 aes${dir} $rndkey0,$inout4
418 aes${dir} $rndkey0,$inout5
419 $movkey ($key),$rndkey0
422 aes${dir} $rndkey1,$inout0
423 aes${dir} $rndkey1,$inout1
424 aes${dir} $rndkey1,$inout2
425 aes${dir} $rndkey1,$inout3
426 aes${dir} $rndkey1,$inout4
427 aes${dir} $rndkey1,$inout5
428 aes${dir}last $rndkey0,$inout0
429 aes${dir}last $rndkey0,$inout1
430 aes${dir}last $rndkey0,$inout2
431 aes${dir}last $rndkey0,$inout3
432 aes${dir}last $rndkey0,$inout4
433 aes${dir}last $rndkey0,$inout5
435 .size _aesni_${dir}rypt6,.-_aesni_${dir}rypt6
438 sub aesni_generate8 {
440 # As already mentioned it takes in $key and $rounds, which are *not*
441 # preserved. $inout[0-7] is cipher/clear text...
443 .type _aesni_${dir}rypt8,\@abi-omnipotent
446 $movkey ($key),$rndkey0
448 $movkey 16($key),$rndkey1
450 xorps $rndkey0,$inout0
451 xorps $rndkey0,$inout1
452 aes${dir} $rndkey1,$inout0
453 pxor $rndkey0,$inout2
454 aes${dir} $rndkey1,$inout1
455 pxor $rndkey0,$inout3
456 aes${dir} $rndkey1,$inout2
457 pxor $rndkey0,$inout4
458 aes${dir} $rndkey1,$inout3
459 pxor $rndkey0,$inout5
461 aes${dir} $rndkey1,$inout4
462 pxor $rndkey0,$inout6
463 aes${dir} $rndkey1,$inout5
464 pxor $rndkey0,$inout7
465 $movkey ($key),$rndkey0
466 aes${dir} $rndkey1,$inout6
467 aes${dir} $rndkey1,$inout7
468 $movkey 16($key),$rndkey1
469 jmp .L${dir}_loop8_enter
472 aes${dir} $rndkey1,$inout0
473 aes${dir} $rndkey1,$inout1
475 aes${dir} $rndkey1,$inout2
476 aes${dir} $rndkey1,$inout3
477 aes${dir} $rndkey1,$inout4
478 aes${dir} $rndkey1,$inout5
479 aes${dir} $rndkey1,$inout6
480 aes${dir} $rndkey1,$inout7
481 $movkey 16($key),$rndkey1
482 .L${dir}_loop8_enter: # happens to be 16-byte aligned
483 aes${dir} $rndkey0,$inout0
484 aes${dir} $rndkey0,$inout1
486 aes${dir} $rndkey0,$inout2
487 aes${dir} $rndkey0,$inout3
488 aes${dir} $rndkey0,$inout4
489 aes${dir} $rndkey0,$inout5
490 aes${dir} $rndkey0,$inout6
491 aes${dir} $rndkey0,$inout7
492 $movkey ($key),$rndkey0
495 aes${dir} $rndkey1,$inout0
496 aes${dir} $rndkey1,$inout1
497 aes${dir} $rndkey1,$inout2
498 aes${dir} $rndkey1,$inout3
499 aes${dir} $rndkey1,$inout4
500 aes${dir} $rndkey1,$inout5
501 aes${dir} $rndkey1,$inout6
502 aes${dir} $rndkey1,$inout7
503 aes${dir}last $rndkey0,$inout0
504 aes${dir}last $rndkey0,$inout1
505 aes${dir}last $rndkey0,$inout2
506 aes${dir}last $rndkey0,$inout3
507 aes${dir}last $rndkey0,$inout4
508 aes${dir}last $rndkey0,$inout5
509 aes${dir}last $rndkey0,$inout6
510 aes${dir}last $rndkey0,$inout7
512 .size _aesni_${dir}rypt8,.-_aesni_${dir}rypt8
515 &aesni_generate3("enc") if ($PREFIX eq "aesni");
516 &aesni_generate3("dec");
517 &aesni_generate4("enc") if ($PREFIX eq "aesni");
518 &aesni_generate4("dec");
519 &aesni_generate6("enc") if ($PREFIX eq "aesni");
520 &aesni_generate6("dec");
521 &aesni_generate8("enc") if ($PREFIX eq "aesni");
522 &aesni_generate8("dec");
524 if ($PREFIX eq "aesni") {
525 ########################################################################
526 # void aesni_ecb_encrypt (const void *in, void *out,
527 # size_t length, const AES_KEY *key,
530 .globl aesni_ecb_encrypt
531 .type aesni_ecb_encrypt,\@function,5
537 mov 240($key),$rounds # key->rounds
538 $movkey ($key),$rndkey0
539 mov $key,$key_ # backup $key
540 mov $rounds,$rnds_ # backup $rounds
541 test %r8d,%r8d # 5th argument
543 #--------------------------- ECB ENCRYPT ------------------------------#
547 movdqu ($inp),$inout0
548 movdqu 0x10($inp),$inout1
549 movdqu 0x20($inp),$inout2
550 movdqu 0x30($inp),$inout3
551 movdqu 0x40($inp),$inout4
552 movdqu 0x50($inp),$inout5
553 movdqu 0x60($inp),$inout6
554 movdqu 0x70($inp),$inout7
557 jmp .Lecb_enc_loop8_enter
560 movups $inout0,($out)
561 mov $key_,$key # restore $key
562 movdqu ($inp),$inout0
563 mov $rnds_,$rounds # restore $rounds
564 movups $inout1,0x10($out)
565 movdqu 0x10($inp),$inout1
566 movups $inout2,0x20($out)
567 movdqu 0x20($inp),$inout2
568 movups $inout3,0x30($out)
569 movdqu 0x30($inp),$inout3
570 movups $inout4,0x40($out)
571 movdqu 0x40($inp),$inout4
572 movups $inout5,0x50($out)
573 movdqu 0x50($inp),$inout5
574 movups $inout6,0x60($out)
575 movdqu 0x60($inp),$inout6
576 movups $inout7,0x70($out)
578 movdqu 0x70($inp),$inout7
580 .Lecb_enc_loop8_enter:
587 movups $inout0,($out)
588 mov $key_,$key # restore $key
589 movups $inout1,0x10($out)
590 mov $rnds_,$rounds # restore $rounds
591 movups $inout2,0x20($out)
592 movups $inout3,0x30($out)
593 movups $inout4,0x40($out)
594 movups $inout5,0x50($out)
595 movups $inout6,0x60($out)
596 movups $inout7,0x70($out)
602 movups ($inp),$inout0
605 movups 0x10($inp),$inout1
607 movups 0x20($inp),$inout2
610 movups 0x30($inp),$inout3
612 movups 0x40($inp),$inout4
615 movups 0x50($inp),$inout5
617 movdqu 0x60($inp),$inout6
619 movups $inout0,($out)
620 movups $inout1,0x10($out)
621 movups $inout2,0x20($out)
622 movups $inout3,0x30($out)
623 movups $inout4,0x40($out)
624 movups $inout5,0x50($out)
625 movups $inout6,0x60($out)
630 &aesni_generate1("enc",$key,$rounds);
632 movups $inout0,($out)
636 xorps $inout2,$inout2
638 movups $inout0,($out)
639 movups $inout1,0x10($out)
644 movups $inout0,($out)
645 movups $inout1,0x10($out)
646 movups $inout2,0x20($out)
651 movups $inout0,($out)
652 movups $inout1,0x10($out)
653 movups $inout2,0x20($out)
654 movups $inout3,0x30($out)
658 xorps $inout5,$inout5
660 movups $inout0,($out)
661 movups $inout1,0x10($out)
662 movups $inout2,0x20($out)
663 movups $inout3,0x30($out)
664 movups $inout4,0x40($out)
669 movups $inout0,($out)
670 movups $inout1,0x10($out)
671 movups $inout2,0x20($out)
672 movups $inout3,0x30($out)
673 movups $inout4,0x40($out)
674 movups $inout5,0x50($out)
676 \f#--------------------------- ECB DECRYPT ------------------------------#
682 movdqu ($inp),$inout0
683 movdqu 0x10($inp),$inout1
684 movdqu 0x20($inp),$inout2
685 movdqu 0x30($inp),$inout3
686 movdqu 0x40($inp),$inout4
687 movdqu 0x50($inp),$inout5
688 movdqu 0x60($inp),$inout6
689 movdqu 0x70($inp),$inout7
692 jmp .Lecb_dec_loop8_enter
695 movups $inout0,($out)
696 mov $key_,$key # restore $key
697 movdqu ($inp),$inout0
698 mov $rnds_,$rounds # restore $rounds
699 movups $inout1,0x10($out)
700 movdqu 0x10($inp),$inout1
701 movups $inout2,0x20($out)
702 movdqu 0x20($inp),$inout2
703 movups $inout3,0x30($out)
704 movdqu 0x30($inp),$inout3
705 movups $inout4,0x40($out)
706 movdqu 0x40($inp),$inout4
707 movups $inout5,0x50($out)
708 movdqu 0x50($inp),$inout5
709 movups $inout6,0x60($out)
710 movdqu 0x60($inp),$inout6
711 movups $inout7,0x70($out)
713 movdqu 0x70($inp),$inout7
715 .Lecb_dec_loop8_enter:
719 $movkey ($key_),$rndkey0
723 movups $inout0,($out)
724 mov $key_,$key # restore $key
725 movups $inout1,0x10($out)
726 mov $rnds_,$rounds # restore $rounds
727 movups $inout2,0x20($out)
728 movups $inout3,0x30($out)
729 movups $inout4,0x40($out)
730 movups $inout5,0x50($out)
731 movups $inout6,0x60($out)
732 movups $inout7,0x70($out)
738 movups ($inp),$inout0
741 movups 0x10($inp),$inout1
743 movups 0x20($inp),$inout2
746 movups 0x30($inp),$inout3
748 movups 0x40($inp),$inout4
751 movups 0x50($inp),$inout5
753 movups 0x60($inp),$inout6
754 $movkey ($key),$rndkey0
756 movups $inout0,($out)
757 movups $inout1,0x10($out)
758 movups $inout2,0x20($out)
759 movups $inout3,0x30($out)
760 movups $inout4,0x40($out)
761 movups $inout5,0x50($out)
762 movups $inout6,0x60($out)
767 &aesni_generate1("dec",$key,$rounds);
769 movups $inout0,($out)
773 xorps $inout2,$inout2
775 movups $inout0,($out)
776 movups $inout1,0x10($out)
781 movups $inout0,($out)
782 movups $inout1,0x10($out)
783 movups $inout2,0x20($out)
788 movups $inout0,($out)
789 movups $inout1,0x10($out)
790 movups $inout2,0x20($out)
791 movups $inout3,0x30($out)
795 xorps $inout5,$inout5
797 movups $inout0,($out)
798 movups $inout1,0x10($out)
799 movups $inout2,0x20($out)
800 movups $inout3,0x30($out)
801 movups $inout4,0x40($out)
806 movups $inout0,($out)
807 movups $inout1,0x10($out)
808 movups $inout2,0x20($out)
809 movups $inout3,0x30($out)
810 movups $inout4,0x40($out)
811 movups $inout5,0x50($out)
815 .size aesni_ecb_encrypt,.-aesni_ecb_encrypt
819 ######################################################################
820 # void aesni_ccm64_[en|de]crypt_blocks (const void *in, void *out,
821 # size_t blocks, const AES_KEY *key,
822 # const char *ivec,char *cmac);
824 # Handles only complete blocks, operates on 64-bit counter and
825 # does not update *ivec! Nor does it finalize CMAC value
826 # (see engine/eng_aesni.c for details)
829 my $cmac="%r9"; # 6th argument
831 my $increment="%xmm6";
832 my $bswap_mask="%xmm7";
835 .globl aesni_ccm64_encrypt_blocks
836 .type aesni_ccm64_encrypt_blocks,\@function,6
838 aesni_ccm64_encrypt_blocks:
840 $code.=<<___ if ($win64);
843 movaps %xmm7,0x10(%rsp)
844 movaps %xmm8,0x20(%rsp)
845 movaps %xmm9,0x30(%rsp)
849 mov 240($key),$rounds # key->rounds
851 movdqa .Lincrement64(%rip),$increment
852 movdqa .Lbswap_mask(%rip),$bswap_mask
856 movdqu ($cmac),$inout1
859 pshufb $bswap_mask,$iv
860 jmp .Lccm64_enc_outer
863 $movkey ($key_),$rndkey0
865 movups ($inp),$in0 # load inp
867 xorps $rndkey0,$inout0 # counter
868 $movkey 16($key_),$rndkey1
871 xorps $rndkey0,$inout1 # cmac^=inp
872 $movkey ($key),$rndkey0
875 aesenc $rndkey1,$inout0
877 aesenc $rndkey1,$inout1
878 $movkey 16($key),$rndkey1
879 aesenc $rndkey0,$inout0
881 aesenc $rndkey0,$inout1
882 $movkey 0($key),$rndkey0
883 jnz .Lccm64_enc2_loop
884 aesenc $rndkey1,$inout0
885 aesenc $rndkey1,$inout1
887 aesenclast $rndkey0,$inout0
888 aesenclast $rndkey0,$inout1
892 xorps $inout0,$in0 # inp ^= E(iv)
894 movups $in0,($out) # save output
896 pshufb $bswap_mask,$inout0
897 jnz .Lccm64_enc_outer
899 movups $inout1,($cmac)
901 $code.=<<___ if ($win64);
903 movaps 0x10(%rsp),%xmm7
904 movaps 0x20(%rsp),%xmm8
905 movaps 0x30(%rsp),%xmm9
911 .size aesni_ccm64_encrypt_blocks,.-aesni_ccm64_encrypt_blocks
913 ######################################################################
915 .globl aesni_ccm64_decrypt_blocks
916 .type aesni_ccm64_decrypt_blocks,\@function,6
918 aesni_ccm64_decrypt_blocks:
920 $code.=<<___ if ($win64);
923 movaps %xmm7,0x10(%rsp)
924 movaps %xmm8,0x20(%rsp)
925 movaps %xmm9,0x30(%rsp)
929 mov 240($key),$rounds # key->rounds
931 movdqu ($cmac),$inout1
932 movdqa .Lincrement64(%rip),$increment
933 movdqa .Lbswap_mask(%rip),$bswap_mask
938 pshufb $bswap_mask,$iv
940 &aesni_generate1("enc",$key,$rounds);
942 movups ($inp),$in0 # load inp
945 jmp .Lccm64_dec_outer
948 xorps $inout0,$in0 # inp ^= E(iv)
951 movups $in0,($out) # save output
953 pshufb $bswap_mask,$inout0
958 $movkey ($key_),$rndkey0
960 $movkey 16($key_),$rndkey1
963 xorps $rndkey0,$inout0
964 xorps $in0,$inout1 # cmac^=out
965 $movkey ($key),$rndkey0
968 aesenc $rndkey1,$inout0
970 aesenc $rndkey1,$inout1
971 $movkey 16($key),$rndkey1
972 aesenc $rndkey0,$inout0
974 aesenc $rndkey0,$inout1
975 $movkey 0($key),$rndkey0
976 jnz .Lccm64_dec2_loop
977 movups ($inp),$in0 # load inp
979 aesenc $rndkey1,$inout0
980 aesenc $rndkey1,$inout1
982 aesenclast $rndkey0,$inout0
983 aesenclast $rndkey0,$inout1
984 jmp .Lccm64_dec_outer
988 #xorps $in0,$inout1 # cmac^=out
990 &aesni_generate1("enc",$key_,$rounds,$inout1,$in0);
992 movups $inout1,($cmac)
994 $code.=<<___ if ($win64);
996 movaps 0x10(%rsp),%xmm7
997 movaps 0x20(%rsp),%xmm8
998 movaps 0x30(%rsp),%xmm9
1004 .size aesni_ccm64_decrypt_blocks,.-aesni_ccm64_decrypt_blocks
1007 ######################################################################
1008 # void aesni_ctr32_encrypt_blocks (const void *in, void *out,
1009 # size_t blocks, const AES_KEY *key,
1010 # const char *ivec);
1012 # Handles only complete blocks, operates on 32-bit counter and
1013 # does not update *ivec! (see engine/eng_aesni.c for details)
1016 my ($in0,$in1,$in2,$in3,$one,$ivec)=map("%xmm$_",(10..15));
1020 .globl aesni_ctr32_encrypt_blocks
1021 .type aesni_ctr32_encrypt_blocks,\@function,5
1023 aesni_ctr32_encrypt_blocks:
1025 $code.=<<___ if ($win64);
1026 lea -0xa8(%rsp),%rsp
1027 movaps %xmm6,0x00(%rsp)
1028 movaps %xmm7,0x10(%rsp)
1029 movaps %xmm8,0x20(%rsp)
1030 movaps %xmm9,0x30(%rsp)
1031 movaps %xmm10,0x40(%rsp)
1032 movaps %xmm11,0x50(%rsp)
1033 movaps %xmm12,0x60(%rsp)
1034 movaps %xmm13,0x70(%rsp)
1035 movaps %xmm14,0x80(%rsp)
1036 movaps %xmm15,0x90(%rsp)
1041 je .Lctr32_one_shortcut
1043 movzb 15($ivp),%rax # counter LSB
1044 mov $len,$len_ # backup $len
1045 mov 240($key),$rnds_ # key->rounds
1046 mov $key,$key_ # backup $key
1049 movdqa .Lincrement1(%rip),$one
1050 add \$256,%rax # steps to closest overflow
1055 mov $rnds_,$rounds # restore $rounds
1061 $movkey ($key_),$rndkey0
1069 $movkey 16($key_),$rndkey1
1070 movdqa $rndkey0,$inout0
1071 movdqa $rndkey0,$inout1
1074 movdqa $rndkey0,$inout2
1075 aesenc $rndkey1,$inout0
1079 movdqa $rndkey0,$inout3
1080 aesenc $rndkey1,$inout1
1083 movdqa $rndkey0,$inout4
1084 aesenc $rndkey1,$inout2
1087 movdqa $rndkey0,$inout5
1088 aesenc $rndkey1,$inout3
1091 movdqa $rndkey0,$inout6
1092 aesenc $rndkey1,$inout4
1095 movdqa $rndkey0,$inout7
1096 aesenc $rndkey1,$inout5
1099 $movkey ($key),$rndkey0
1100 aesenc $rndkey1,$inout6
1104 aesenc $rndkey1,$inout7
1105 $movkey 16($key),$rndkey1
1106 movups ($inp),$in0 # load input
1107 movups 0x10($inp),$in1
1108 movups 0x20($inp),$in2
1109 movups 0x30($inp),$in3
1111 call .Lenc_loop8_enter
1113 xorps $in0,$inout0 # xor
1114 movups 0x40($inp),$in0
1116 movups 0x50($inp),$in1
1118 movups 0x60($inp),$in2
1120 movups 0x70($inp),$in3
1123 movups $inout0,($out) # store output
1125 movups $inout1,0x10($out)
1127 movups $inout2,0x20($out)
1129 movups $inout3,0x30($out)
1130 movups $inout4,0x40($out)
1131 movups $inout5,0x50($out)
1132 movups $inout6,0x60($out)
1133 movups $inout7,0x70($out)
1136 $movkey ($key_),$rndkey0
1141 lea 1($rounds,$rounds),$rounds # restore original value
1142 lea 1($rnds_,$rnds_),$rnds_ # restore original value
1147 mov $key_,$key # restore $key
1148 movdqa $ivec,$inout0
1154 movdqa $ivec,$inout1
1156 movups 0x10($inp),$in1
1159 movdqa $ivec,$inout2
1161 movups 0x20($inp),$in2
1165 movdqa $ivec,$inout3
1167 movups 0x30($inp),$in3
1170 movdqa $ivec,$inout4
1175 movdqa $ivec,$inout5
1179 movdqa $ivec,$inout6
1181 xorps $inout7,$inout7
1183 call _aesni_encrypt8
1185 xorps $in0,$inout0 # xor
1186 movups 0x40($inp),$in0
1188 movups 0x50($inp),$in1
1190 movups 0x60($inp),$in2
1193 movups $inout0,($out) # store output
1195 movups $inout1,0x10($out)
1197 movups $inout2,0x20($out)
1199 movups $inout3,0x30($out)
1200 movups $inout4,0x40($out)
1201 movups $inout5,0x50($out)
1202 movups $inout6,0x60($out)
1207 .Lctr32_one_shortcut:
1208 movups ($ivp),$inout0
1211 mov 240($key),$rounds # key->rounds
1214 &aesni_generate1("enc",$key,$rounds);
1218 movups $inout0,($out)
1224 xorps $inout2,$inout2
1225 call _aesni_encrypt3
1226 xorps $in0,$inout0 # xor
1229 movups $inout0,($out) # store output
1230 movups $inout1,0x10($out)
1236 call _aesni_encrypt3
1237 xorps $in0,$inout0 # xor
1240 movups $inout0,($out) # store output
1242 movups $inout1,0x10($out)
1243 movups $inout2,0x20($out)
1249 call _aesni_encrypt4
1250 xorps $in0,$inout0 # xor
1253 movups $inout0,($out) # store output
1255 movups $inout1,0x10($out)
1257 movups $inout2,0x20($out)
1258 movups $inout3,0x30($out)
1264 xorps $inout5,$inout5
1265 call _aesni_encrypt6
1266 xorps $in0,$inout0 # xor
1267 movups 0x40($inp),$in0
1270 movups $inout0,($out) # store output
1272 movups $inout1,0x10($out)
1274 movups $inout2,0x20($out)
1276 movups $inout3,0x30($out)
1277 movups $inout4,0x40($out)
1283 call _aesni_encrypt6
1284 xorps $in0,$inout0 # xor
1285 movups 0x40($inp),$in0
1287 movups 0x50($inp),$in1
1290 movups $inout0,($out) # store output
1292 movups $inout1,0x10($out)
1294 movups $inout2,0x20($out)
1296 movups $inout3,0x30($out)
1297 movups $inout4,0x40($out)
1298 movups $inout5,0x50($out)
1303 jz .Lctr32_really_done
1305 movdqa .Lbswap_mask(%rip),$rndkey1
1306 pshufb $rndkey1,$ivec
1307 psrldq \$14,$one # 256
1310 pshufb $rndkey1,$ivec
1313 jmp .Lctr32_grandloop
1315 .Lctr32_really_done:
1317 $code.=<<___ if ($win64);
1318 movaps 0x00(%rsp),%xmm6
1319 movaps 0x10(%rsp),%xmm7
1320 movaps 0x20(%rsp),%xmm8
1321 movaps 0x30(%rsp),%xmm9
1322 movaps 0x40(%rsp),%xmm10
1323 movaps 0x50(%rsp),%xmm11
1324 movaps 0x60(%rsp),%xmm12
1325 movaps 0x70(%rsp),%xmm13
1326 movaps 0x80(%rsp),%xmm14
1327 movaps 0x90(%rsp),%xmm15
1333 .size aesni_ctr32_encrypt_blocks,.-aesni_ctr32_encrypt_blocks
1337 ######################################################################
1338 # void aesni_xts_[en|de]crypt(const char *inp,char *out,size_t len,
1339 # const AES_KEY *key1, const AES_KEY *key2
1340 # const unsigned char iv[16]);
1343 my @tweak=map("%xmm$_",(10..15));
1344 my ($twmask,$twres,$twtmp)=("%xmm8","%xmm9",@tweak[4]);
1345 my ($key2,$ivp,$len_)=("%r8","%r9","%r9");
1346 my $frame_size = 0x60 + ($win64?160:0);
1349 .globl aesni_xts_encrypt
1350 .type aesni_xts_encrypt,\@function,6
1355 sub \$$frame_size,%rsp
1356 and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
1358 $code.=<<___ if ($win64);
1359 movaps %xmm6,0x60(%rsp)
1360 movaps %xmm7,0x70(%rsp)
1361 movaps %xmm8,0x80(%rsp)
1362 movaps %xmm9,0x90(%rsp)
1363 movaps %xmm10,0xa0(%rsp)
1364 movaps %xmm11,0xb0(%rsp)
1365 movaps %xmm12,0xc0(%rsp)
1366 movaps %xmm13,0xd0(%rsp)
1367 movaps %xmm14,0xe0(%rsp)
1368 movaps %xmm15,0xf0(%rsp)
1373 movups ($ivp),@tweak[5] # load clear-text tweak
1374 mov 240(%r8),$rounds # key2->rounds
1375 mov 240($key),$rnds_ # key1->rounds
1377 # generate the tweak
1378 &aesni_generate1("enc",$key2,$rounds,@tweak[5]);
1380 mov $key,$key_ # backup $key
1381 mov $rnds_,$rounds # backup $rounds
1382 mov $len,$len_ # backup $len
1385 movdqa .Lxts_magic(%rip),$twmask
1387 pcmpgtd @tweak[5],$twtmp # broadcast upper bits
1389 for ($i=0;$i<4;$i++) {
1391 pshufd \$0x13,$twtmp,$twres
1393 movdqa @tweak[5],@tweak[$i]
1394 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1395 pand $twmask,$twres # isolate carry and residue
1396 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1397 pxor $twres,@tweak[5]
1407 jmp .Lxts_enc_grandloop
1410 .Lxts_enc_grandloop:
1411 pshufd \$0x13,$twtmp,$twres
1412 movdqa @tweak[5],@tweak[4]
1413 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1414 movdqu `16*0`($inp),$inout0 # load input
1415 pand $twmask,$twres # isolate carry and residue
1416 movdqu `16*1`($inp),$inout1
1417 pxor $twres,@tweak[5]
1419 movdqu `16*2`($inp),$inout2
1420 pxor @tweak[0],$inout0 # input^=tweak
1421 movdqu `16*3`($inp),$inout3
1422 pxor @tweak[1],$inout1
1423 movdqu `16*4`($inp),$inout4
1424 pxor @tweak[2],$inout2
1425 movdqu `16*5`($inp),$inout5
1426 lea `16*6`($inp),$inp
1427 pxor @tweak[3],$inout3
1428 $movkey ($key_),$rndkey0
1429 pxor @tweak[4],$inout4
1430 pxor @tweak[5],$inout5
1432 # inline _aesni_encrypt6 and interleave first and last rounds
1434 $movkey 16($key_),$rndkey1
1435 pxor $rndkey0,$inout0
1436 pxor $rndkey0,$inout1
1437 movdqa @tweak[0],`16*0`(%rsp) # put aside tweaks
1438 aesenc $rndkey1,$inout0
1440 pxor $rndkey0,$inout2
1441 movdqa @tweak[1],`16*1`(%rsp)
1442 aesenc $rndkey1,$inout1
1443 pxor $rndkey0,$inout3
1444 movdqa @tweak[2],`16*2`(%rsp)
1445 aesenc $rndkey1,$inout2
1446 pxor $rndkey0,$inout4
1447 movdqa @tweak[3],`16*3`(%rsp)
1448 aesenc $rndkey1,$inout3
1449 pxor $rndkey0,$inout5
1450 $movkey ($key),$rndkey0
1452 movdqa @tweak[4],`16*4`(%rsp)
1453 aesenc $rndkey1,$inout4
1454 movdqa @tweak[5],`16*5`(%rsp)
1455 aesenc $rndkey1,$inout5
1457 pcmpgtd @tweak[5],$twtmp
1458 jmp .Lxts_enc_loop6_enter
1462 aesenc $rndkey1,$inout0
1463 aesenc $rndkey1,$inout1
1465 aesenc $rndkey1,$inout2
1466 aesenc $rndkey1,$inout3
1467 aesenc $rndkey1,$inout4
1468 aesenc $rndkey1,$inout5
1469 .Lxts_enc_loop6_enter:
1470 $movkey 16($key),$rndkey1
1471 aesenc $rndkey0,$inout0
1472 aesenc $rndkey0,$inout1
1474 aesenc $rndkey0,$inout2
1475 aesenc $rndkey0,$inout3
1476 aesenc $rndkey0,$inout4
1477 aesenc $rndkey0,$inout5
1478 $movkey ($key),$rndkey0
1481 pshufd \$0x13,$twtmp,$twres
1483 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1484 aesenc $rndkey1,$inout0
1485 pand $twmask,$twres # isolate carry and residue
1486 aesenc $rndkey1,$inout1
1487 pcmpgtd @tweak[5],$twtmp # broadcast upper bits
1488 aesenc $rndkey1,$inout2
1489 pxor $twres,@tweak[5]
1490 aesenc $rndkey1,$inout3
1491 aesenc $rndkey1,$inout4
1492 aesenc $rndkey1,$inout5
1493 $movkey 16($key),$rndkey1
1495 pshufd \$0x13,$twtmp,$twres
1497 movdqa @tweak[5],@tweak[0]
1498 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1499 aesenc $rndkey0,$inout0
1500 pand $twmask,$twres # isolate carry and residue
1501 aesenc $rndkey0,$inout1
1502 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1503 aesenc $rndkey0,$inout2
1504 pxor $twres,@tweak[5]
1505 aesenc $rndkey0,$inout3
1506 aesenc $rndkey0,$inout4
1507 aesenc $rndkey0,$inout5
1508 $movkey 32($key),$rndkey0
1510 pshufd \$0x13,$twtmp,$twres
1512 movdqa @tweak[5],@tweak[1]
1513 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1514 aesenc $rndkey1,$inout0
1515 pand $twmask,$twres # isolate carry and residue
1516 aesenc $rndkey1,$inout1
1517 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1518 aesenc $rndkey1,$inout2
1519 pxor $twres,@tweak[5]
1520 aesenc $rndkey1,$inout3
1521 aesenc $rndkey1,$inout4
1522 aesenc $rndkey1,$inout5
1524 pshufd \$0x13,$twtmp,$twres
1526 movdqa @tweak[5],@tweak[2]
1527 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1528 aesenclast $rndkey0,$inout0
1529 pand $twmask,$twres # isolate carry and residue
1530 aesenclast $rndkey0,$inout1
1531 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1532 aesenclast $rndkey0,$inout2
1533 pxor $twres,@tweak[5]
1534 aesenclast $rndkey0,$inout3
1535 aesenclast $rndkey0,$inout4
1536 aesenclast $rndkey0,$inout5
1538 pshufd \$0x13,$twtmp,$twres
1540 movdqa @tweak[5],@tweak[3]
1541 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1542 xorps `16*0`(%rsp),$inout0 # output^=tweak
1543 pand $twmask,$twres # isolate carry and residue
1544 xorps `16*1`(%rsp),$inout1
1545 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1546 pxor $twres,@tweak[5]
1548 xorps `16*2`(%rsp),$inout2
1549 movups $inout0,`16*0`($out) # write output
1550 xorps `16*3`(%rsp),$inout3
1551 movups $inout1,`16*1`($out)
1552 xorps `16*4`(%rsp),$inout4
1553 movups $inout2,`16*2`($out)
1554 xorps `16*5`(%rsp),$inout5
1555 movups $inout3,`16*3`($out)
1556 mov $rnds_,$rounds # restore $rounds
1557 movups $inout4,`16*4`($out)
1558 movups $inout5,`16*5`($out)
1559 lea `16*6`($out),$out
1561 jnc .Lxts_enc_grandloop
1563 lea 3($rounds,$rounds),$rounds # restore original value
1564 mov $key_,$key # restore $key
1565 mov $rounds,$rnds_ # backup $rounds
1579 pshufd \$0x13,$twtmp,$twres
1580 movdqa @tweak[5],@tweak[4]
1581 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1582 movdqu ($inp),$inout0
1583 pand $twmask,$twres # isolate carry and residue
1584 movdqu 16*1($inp),$inout1
1585 pxor $twres,@tweak[5]
1587 movdqu 16*2($inp),$inout2
1588 pxor @tweak[0],$inout0
1589 movdqu 16*3($inp),$inout3
1590 pxor @tweak[1],$inout1
1591 movdqu 16*4($inp),$inout4
1593 pxor @tweak[2],$inout2
1594 pxor @tweak[3],$inout3
1595 pxor @tweak[4],$inout4
1597 call _aesni_encrypt6
1599 xorps @tweak[0],$inout0
1600 movdqa @tweak[5],@tweak[0]
1601 xorps @tweak[1],$inout1
1602 xorps @tweak[2],$inout2
1603 movdqu $inout0,($out)
1604 xorps @tweak[3],$inout3
1605 movdqu $inout1,16*1($out)
1606 xorps @tweak[4],$inout4
1607 movdqu $inout2,16*2($out)
1608 movdqu $inout3,16*3($out)
1609 movdqu $inout4,16*4($out)
1615 movups ($inp),$inout0
1617 xorps @tweak[0],$inout0
1619 &aesni_generate1("enc",$key,$rounds);
1621 xorps @tweak[0],$inout0
1622 movdqa @tweak[1],@tweak[0]
1623 movups $inout0,($out)
1629 movups ($inp),$inout0
1630 movups 16($inp),$inout1
1632 xorps @tweak[0],$inout0
1633 xorps @tweak[1],$inout1
1635 call _aesni_encrypt3
1637 xorps @tweak[0],$inout0
1638 movdqa @tweak[2],@tweak[0]
1639 xorps @tweak[1],$inout1
1640 movups $inout0,($out)
1641 movups $inout1,16*1($out)
1647 movups ($inp),$inout0
1648 movups 16*1($inp),$inout1
1649 movups 16*2($inp),$inout2
1651 xorps @tweak[0],$inout0
1652 xorps @tweak[1],$inout1
1653 xorps @tweak[2],$inout2
1655 call _aesni_encrypt3
1657 xorps @tweak[0],$inout0
1658 movdqa @tweak[3],@tweak[0]
1659 xorps @tweak[1],$inout1
1660 xorps @tweak[2],$inout2
1661 movups $inout0,($out)
1662 movups $inout1,16*1($out)
1663 movups $inout2,16*2($out)
1669 movups ($inp),$inout0
1670 movups 16*1($inp),$inout1
1671 movups 16*2($inp),$inout2
1672 xorps @tweak[0],$inout0
1673 movups 16*3($inp),$inout3
1675 xorps @tweak[1],$inout1
1676 xorps @tweak[2],$inout2
1677 xorps @tweak[3],$inout3
1679 call _aesni_encrypt4
1681 xorps @tweak[0],$inout0
1682 movdqa @tweak[5],@tweak[0]
1683 xorps @tweak[1],$inout1
1684 xorps @tweak[2],$inout2
1685 movups $inout0,($out)
1686 xorps @tweak[3],$inout3
1687 movups $inout1,16*1($out)
1688 movups $inout2,16*2($out)
1689 movups $inout3,16*3($out)
1700 movzb ($inp),%eax # borrow $rounds ...
1701 movzb -16($out),%ecx # ... and $key
1709 sub $len_,$out # rewind $out
1710 mov $key_,$key # restore $key
1711 mov $rnds_,$rounds # restore $rounds
1713 movups -16($out),$inout0
1714 xorps @tweak[0],$inout0
1716 &aesni_generate1("enc",$key,$rounds);
1718 xorps @tweak[0],$inout0
1719 movups $inout0,-16($out)
1723 $code.=<<___ if ($win64);
1724 movaps 0x60(%rsp),%xmm6
1725 movaps 0x70(%rsp),%xmm7
1726 movaps 0x80(%rsp),%xmm8
1727 movaps 0x90(%rsp),%xmm9
1728 movaps 0xa0(%rsp),%xmm10
1729 movaps 0xb0(%rsp),%xmm11
1730 movaps 0xc0(%rsp),%xmm12
1731 movaps 0xd0(%rsp),%xmm13
1732 movaps 0xe0(%rsp),%xmm14
1733 movaps 0xf0(%rsp),%xmm15
1740 .size aesni_xts_encrypt,.-aesni_xts_encrypt
1744 .globl aesni_xts_decrypt
1745 .type aesni_xts_decrypt,\@function,6
1750 sub \$$frame_size,%rsp
1751 and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
1753 $code.=<<___ if ($win64);
1754 movaps %xmm6,0x60(%rsp)
1755 movaps %xmm7,0x70(%rsp)
1756 movaps %xmm8,0x80(%rsp)
1757 movaps %xmm9,0x90(%rsp)
1758 movaps %xmm10,0xa0(%rsp)
1759 movaps %xmm11,0xb0(%rsp)
1760 movaps %xmm12,0xc0(%rsp)
1761 movaps %xmm13,0xd0(%rsp)
1762 movaps %xmm14,0xe0(%rsp)
1763 movaps %xmm15,0xf0(%rsp)
1768 movups ($ivp),@tweak[5] # load clear-text tweak
1769 mov 240($key2),$rounds # key2->rounds
1770 mov 240($key),$rnds_ # key1->rounds
1772 # generate the tweak
1773 &aesni_generate1("enc",$key2,$rounds,@tweak[5]);
1775 xor %eax,%eax # if ($len%16) len-=16;
1781 mov $key,$key_ # backup $key
1782 mov $rnds_,$rounds # backup $rounds
1783 mov $len,$len_ # backup $len
1786 movdqa .Lxts_magic(%rip),$twmask
1788 pcmpgtd @tweak[5],$twtmp # broadcast upper bits
1790 for ($i=0;$i<4;$i++) {
1792 pshufd \$0x13,$twtmp,$twres
1794 movdqa @tweak[5],@tweak[$i]
1795 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1796 pand $twmask,$twres # isolate carry and residue
1797 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1798 pxor $twres,@tweak[5]
1808 jmp .Lxts_dec_grandloop
1811 .Lxts_dec_grandloop:
1812 pshufd \$0x13,$twtmp,$twres
1813 movdqa @tweak[5],@tweak[4]
1814 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1815 movdqu `16*0`($inp),$inout0 # load input
1816 pand $twmask,$twres # isolate carry and residue
1817 movdqu `16*1`($inp),$inout1
1818 pxor $twres,@tweak[5]
1820 movdqu `16*2`($inp),$inout2
1821 pxor @tweak[0],$inout0 # input^=tweak
1822 movdqu `16*3`($inp),$inout3
1823 pxor @tweak[1],$inout1
1824 movdqu `16*4`($inp),$inout4
1825 pxor @tweak[2],$inout2
1826 movdqu `16*5`($inp),$inout5
1827 lea `16*6`($inp),$inp
1828 pxor @tweak[3],$inout3
1829 $movkey ($key_),$rndkey0
1830 pxor @tweak[4],$inout4
1831 pxor @tweak[5],$inout5
1833 # inline _aesni_decrypt6 and interleave first and last rounds
1835 $movkey 16($key_),$rndkey1
1836 pxor $rndkey0,$inout0
1837 pxor $rndkey0,$inout1
1838 movdqa @tweak[0],`16*0`(%rsp) # put aside tweaks
1839 aesdec $rndkey1,$inout0
1841 pxor $rndkey0,$inout2
1842 movdqa @tweak[1],`16*1`(%rsp)
1843 aesdec $rndkey1,$inout1
1844 pxor $rndkey0,$inout3
1845 movdqa @tweak[2],`16*2`(%rsp)
1846 aesdec $rndkey1,$inout2
1847 pxor $rndkey0,$inout4
1848 movdqa @tweak[3],`16*3`(%rsp)
1849 aesdec $rndkey1,$inout3
1850 pxor $rndkey0,$inout5
1851 $movkey ($key),$rndkey0
1853 movdqa @tweak[4],`16*4`(%rsp)
1854 aesdec $rndkey1,$inout4
1855 movdqa @tweak[5],`16*5`(%rsp)
1856 aesdec $rndkey1,$inout5
1858 pcmpgtd @tweak[5],$twtmp
1859 jmp .Lxts_dec_loop6_enter
1863 aesdec $rndkey1,$inout0
1864 aesdec $rndkey1,$inout1
1866 aesdec $rndkey1,$inout2
1867 aesdec $rndkey1,$inout3
1868 aesdec $rndkey1,$inout4
1869 aesdec $rndkey1,$inout5
1870 .Lxts_dec_loop6_enter:
1871 $movkey 16($key),$rndkey1
1872 aesdec $rndkey0,$inout0
1873 aesdec $rndkey0,$inout1
1875 aesdec $rndkey0,$inout2
1876 aesdec $rndkey0,$inout3
1877 aesdec $rndkey0,$inout4
1878 aesdec $rndkey0,$inout5
1879 $movkey ($key),$rndkey0
1882 pshufd \$0x13,$twtmp,$twres
1884 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1885 aesdec $rndkey1,$inout0
1886 pand $twmask,$twres # isolate carry and residue
1887 aesdec $rndkey1,$inout1
1888 pcmpgtd @tweak[5],$twtmp # broadcast upper bits
1889 aesdec $rndkey1,$inout2
1890 pxor $twres,@tweak[5]
1891 aesdec $rndkey1,$inout3
1892 aesdec $rndkey1,$inout4
1893 aesdec $rndkey1,$inout5
1894 $movkey 16($key),$rndkey1
1896 pshufd \$0x13,$twtmp,$twres
1898 movdqa @tweak[5],@tweak[0]
1899 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1900 aesdec $rndkey0,$inout0
1901 pand $twmask,$twres # isolate carry and residue
1902 aesdec $rndkey0,$inout1
1903 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1904 aesdec $rndkey0,$inout2
1905 pxor $twres,@tweak[5]
1906 aesdec $rndkey0,$inout3
1907 aesdec $rndkey0,$inout4
1908 aesdec $rndkey0,$inout5
1909 $movkey 32($key),$rndkey0
1911 pshufd \$0x13,$twtmp,$twres
1913 movdqa @tweak[5],@tweak[1]
1914 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1915 aesdec $rndkey1,$inout0
1916 pand $twmask,$twres # isolate carry and residue
1917 aesdec $rndkey1,$inout1
1918 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1919 aesdec $rndkey1,$inout2
1920 pxor $twres,@tweak[5]
1921 aesdec $rndkey1,$inout3
1922 aesdec $rndkey1,$inout4
1923 aesdec $rndkey1,$inout5
1925 pshufd \$0x13,$twtmp,$twres
1927 movdqa @tweak[5],@tweak[2]
1928 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1929 aesdeclast $rndkey0,$inout0
1930 pand $twmask,$twres # isolate carry and residue
1931 aesdeclast $rndkey0,$inout1
1932 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1933 aesdeclast $rndkey0,$inout2
1934 pxor $twres,@tweak[5]
1935 aesdeclast $rndkey0,$inout3
1936 aesdeclast $rndkey0,$inout4
1937 aesdeclast $rndkey0,$inout5
1939 pshufd \$0x13,$twtmp,$twres
1941 movdqa @tweak[5],@tweak[3]
1942 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1943 xorps `16*0`(%rsp),$inout0 # output^=tweak
1944 pand $twmask,$twres # isolate carry and residue
1945 xorps `16*1`(%rsp),$inout1
1946 pcmpgtd @tweak[5],$twtmp # broadcat upper bits
1947 pxor $twres,@tweak[5]
1949 xorps `16*2`(%rsp),$inout2
1950 movups $inout0,`16*0`($out) # write output
1951 xorps `16*3`(%rsp),$inout3
1952 movups $inout1,`16*1`($out)
1953 xorps `16*4`(%rsp),$inout4
1954 movups $inout2,`16*2`($out)
1955 xorps `16*5`(%rsp),$inout5
1956 movups $inout3,`16*3`($out)
1957 mov $rnds_,$rounds # restore $rounds
1958 movups $inout4,`16*4`($out)
1959 movups $inout5,`16*5`($out)
1960 lea `16*6`($out),$out
1962 jnc .Lxts_dec_grandloop
1964 lea 3($rounds,$rounds),$rounds # restore original value
1965 mov $key_,$key # restore $key
1966 mov $rounds,$rnds_ # backup $rounds
1980 pshufd \$0x13,$twtmp,$twres
1981 movdqa @tweak[5],@tweak[4]
1982 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
1983 movdqu ($inp),$inout0
1984 pand $twmask,$twres # isolate carry and residue
1985 movdqu 16*1($inp),$inout1
1986 pxor $twres,@tweak[5]
1988 movdqu 16*2($inp),$inout2
1989 pxor @tweak[0],$inout0
1990 movdqu 16*3($inp),$inout3
1991 pxor @tweak[1],$inout1
1992 movdqu 16*4($inp),$inout4
1994 pxor @tweak[2],$inout2
1995 pxor @tweak[3],$inout3
1996 pxor @tweak[4],$inout4
1998 call _aesni_decrypt6
2000 xorps @tweak[0],$inout0
2001 xorps @tweak[1],$inout1
2002 xorps @tweak[2],$inout2
2003 movdqu $inout0,($out)
2004 xorps @tweak[3],$inout3
2005 movdqu $inout1,16*1($out)
2006 xorps @tweak[4],$inout4
2007 movdqu $inout2,16*2($out)
2009 movdqu $inout3,16*3($out)
2010 pcmpgtd @tweak[5],$twtmp
2011 movdqu $inout4,16*4($out)
2013 pshufd \$0x13,$twtmp,@tweak[1] # $twres
2017 movdqa @tweak[5],@tweak[0]
2018 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
2019 pand $twmask,@tweak[1] # isolate carry and residue
2020 pxor @tweak[5],@tweak[1]
2025 movups ($inp),$inout0
2027 xorps @tweak[0],$inout0
2029 &aesni_generate1("dec",$key,$rounds);
2031 xorps @tweak[0],$inout0
2032 movdqa @tweak[1],@tweak[0]
2033 movups $inout0,($out)
2034 movdqa @tweak[2],@tweak[1]
2040 movups ($inp),$inout0
2041 movups 16($inp),$inout1
2043 xorps @tweak[0],$inout0
2044 xorps @tweak[1],$inout1
2046 call _aesni_decrypt3
2048 xorps @tweak[0],$inout0
2049 movdqa @tweak[2],@tweak[0]
2050 xorps @tweak[1],$inout1
2051 movdqa @tweak[3],@tweak[1]
2052 movups $inout0,($out)
2053 movups $inout1,16*1($out)
2059 movups ($inp),$inout0
2060 movups 16*1($inp),$inout1
2061 movups 16*2($inp),$inout2
2063 xorps @tweak[0],$inout0
2064 xorps @tweak[1],$inout1
2065 xorps @tweak[2],$inout2
2067 call _aesni_decrypt3
2069 xorps @tweak[0],$inout0
2070 movdqa @tweak[3],@tweak[0]
2071 xorps @tweak[1],$inout1
2072 movdqa @tweak[5],@tweak[1]
2073 xorps @tweak[2],$inout2
2074 movups $inout0,($out)
2075 movups $inout1,16*1($out)
2076 movups $inout2,16*2($out)
2082 pshufd \$0x13,$twtmp,$twres
2083 movdqa @tweak[5],@tweak[4]
2084 paddq @tweak[5],@tweak[5] # psllq 1,$tweak
2085 movups ($inp),$inout0
2086 pand $twmask,$twres # isolate carry and residue
2087 movups 16*1($inp),$inout1
2088 pxor $twres,@tweak[5]
2090 movups 16*2($inp),$inout2
2091 xorps @tweak[0],$inout0
2092 movups 16*3($inp),$inout3
2094 xorps @tweak[1],$inout1
2095 xorps @tweak[2],$inout2
2096 xorps @tweak[3],$inout3
2098 call _aesni_decrypt4
2100 xorps @tweak[0],$inout0
2101 movdqa @tweak[4],@tweak[0]
2102 xorps @tweak[1],$inout1
2103 movdqa @tweak[5],@tweak[1]
2104 xorps @tweak[2],$inout2
2105 movups $inout0,($out)
2106 xorps @tweak[3],$inout3
2107 movups $inout1,16*1($out)
2108 movups $inout2,16*2($out)
2109 movups $inout3,16*3($out)
2119 mov $key_,$key # restore $key
2120 mov $rnds_,$rounds # restore $rounds
2122 movups ($inp),$inout0
2123 xorps @tweak[1],$inout0
2125 &aesni_generate1("dec",$key,$rounds);
2127 xorps @tweak[1],$inout0
2128 movups $inout0,($out)
2131 movzb 16($inp),%eax # borrow $rounds ...
2132 movzb ($out),%ecx # ... and $key
2140 sub $len_,$out # rewind $out
2141 mov $key_,$key # restore $key
2142 mov $rnds_,$rounds # restore $rounds
2144 movups ($out),$inout0
2145 xorps @tweak[0],$inout0
2147 &aesni_generate1("dec",$key,$rounds);
2149 xorps @tweak[0],$inout0
2150 movups $inout0,($out)
2154 $code.=<<___ if ($win64);
2155 movaps 0x60(%rsp),%xmm6
2156 movaps 0x70(%rsp),%xmm7
2157 movaps 0x80(%rsp),%xmm8
2158 movaps 0x90(%rsp),%xmm9
2159 movaps 0xa0(%rsp),%xmm10
2160 movaps 0xb0(%rsp),%xmm11
2161 movaps 0xc0(%rsp),%xmm12
2162 movaps 0xd0(%rsp),%xmm13
2163 movaps 0xe0(%rsp),%xmm14
2164 movaps 0xf0(%rsp),%xmm15
2171 .size aesni_xts_decrypt,.-aesni_xts_decrypt
2175 ########################################################################
2176 # void $PREFIX_cbc_encrypt (const void *inp, void *out,
2177 # size_t length, const AES_KEY *key,
2178 # unsigned char *ivp,const int enc);
2180 my $frame_size = 0x10 + ($win64?0x40:0); # used in decrypt
2182 .globl ${PREFIX}_cbc_encrypt
2183 .type ${PREFIX}_cbc_encrypt,\@function,6
2185 ${PREFIX}_cbc_encrypt:
2186 test $len,$len # check length
2189 mov 240($key),$rnds_ # key->rounds
2190 mov $key,$key_ # backup $key
2191 test %r9d,%r9d # 6th argument
2193 #--------------------------- CBC ENCRYPT ------------------------------#
2194 movups ($ivp),$inout0 # load iv as initial state
2202 movups ($inp),$inout1 # load input
2204 #xorps $inout1,$inout0
2206 &aesni_generate1("enc",$key,$rounds,$inout0,$inout1);
2208 mov $rnds_,$rounds # restore $rounds
2209 mov $key_,$key # restore $key
2210 movups $inout0,0($out) # store output
2216 movups $inout0,($ivp)
2220 mov $len,%rcx # zaps $key
2221 xchg $inp,$out # $inp is %rsi and $out is %rdi now
2222 .long 0x9066A4F3 # rep movsb
2223 mov \$16,%ecx # zero tail
2226 .long 0x9066AAF3 # rep stosb
2227 lea -16(%rdi),%rdi # rewind $out by 1 block
2228 mov $rnds_,$rounds # restore $rounds
2229 mov %rdi,%rsi # $inp and $out are the same
2230 mov $key_,$key # restore $key
2231 xor $len,$len # len=16
2232 jmp .Lcbc_enc_loop # one more spin
2233 \f#--------------------------- CBC DECRYPT ------------------------------#
2238 sub \$$frame_size,%rsp
2239 and \$-16,%rsp # Linux kernel stack can be incorrectly seeded
2241 $code.=<<___ if ($win64);
2242 movaps %xmm6,0x10(%rsp)
2243 movaps %xmm7,0x20(%rsp)
2244 movaps %xmm8,0x30(%rsp)
2245 movaps %xmm9,0x40(%rsp)
2258 jmp .Lcbc_dec_loop8_enter
2261 movaps $rndkey0,(%rsp) # save IV
2262 movups $inout7,($out)
2264 .Lcbc_dec_loop8_enter:
2265 $movkey ($key),$rndkey0
2266 movups ($inp),$inout0 # load input
2267 movups 0x10($inp),$inout1
2268 $movkey 16($key),$rndkey1
2271 movdqu 0x20($inp),$inout2
2272 xorps $rndkey0,$inout0
2273 movdqu 0x30($inp),$inout3
2274 xorps $rndkey0,$inout1
2275 movdqu 0x40($inp),$inout4
2276 aesdec $rndkey1,$inout0
2277 pxor $rndkey0,$inout2
2278 movdqu 0x50($inp),$inout5
2279 aesdec $rndkey1,$inout1
2280 pxor $rndkey0,$inout3
2281 movdqu 0x60($inp),$inout6
2282 aesdec $rndkey1,$inout2
2283 pxor $rndkey0,$inout4
2284 movdqu 0x70($inp),$inout7
2285 aesdec $rndkey1,$inout3
2286 pxor $rndkey0,$inout5
2288 aesdec $rndkey1,$inout4
2289 pxor $rndkey0,$inout6
2290 aesdec $rndkey1,$inout5
2291 pxor $rndkey0,$inout7
2292 $movkey ($key),$rndkey0
2293 aesdec $rndkey1,$inout6
2294 aesdec $rndkey1,$inout7
2295 $movkey 16($key),$rndkey1
2297 call .Ldec_loop8_enter
2299 movups ($inp),$rndkey1 # re-load input
2300 movups 0x10($inp),$rndkey0
2301 xorps (%rsp),$inout0 # ^= IV
2302 xorps $rndkey1,$inout1
2303 movups 0x20($inp),$rndkey1
2304 xorps $rndkey0,$inout2
2305 movups 0x30($inp),$rndkey0
2306 xorps $rndkey1,$inout3
2307 movups 0x40($inp),$rndkey1
2308 xorps $rndkey0,$inout4
2309 movups 0x50($inp),$rndkey0
2310 xorps $rndkey1,$inout5
2311 movups 0x60($inp),$rndkey1
2312 xorps $rndkey0,$inout6
2313 movups 0x70($inp),$rndkey0 # IV
2314 xorps $rndkey1,$inout7
2315 movups $inout0,($out)
2316 movups $inout1,0x10($out)
2317 movups $inout2,0x20($out)
2318 movups $inout3,0x30($out)
2319 mov $rnds_,$rounds # restore $rounds
2320 movups $inout4,0x40($out)
2321 mov $key_,$key # restore $key
2322 movups $inout5,0x50($out)
2324 movups $inout6,0x60($out)
2329 movaps $inout7,$inout0
2332 jle .Lcbc_dec_tail_collected
2333 movups $inout0,($out)
2334 lea 1($rnds_,$rnds_),$rounds
2337 movups ($inp),$inout0
2342 movups 0x10($inp),$inout1
2347 movups 0x20($inp),$inout2
2352 movups 0x30($inp),$inout3
2356 movups 0x40($inp),$inout4
2360 movups 0x50($inp),$inout5
2364 movups 0x60($inp),$inout6
2365 movaps $iv,(%rsp) # save IV
2366 call _aesni_decrypt8
2367 movups ($inp),$rndkey1
2368 movups 0x10($inp),$rndkey0
2369 xorps (%rsp),$inout0 # ^= IV
2370 xorps $rndkey1,$inout1
2371 movups 0x20($inp),$rndkey1
2372 xorps $rndkey0,$inout2
2373 movups 0x30($inp),$rndkey0
2374 xorps $rndkey1,$inout3
2375 movups 0x40($inp),$rndkey1
2376 xorps $rndkey0,$inout4
2377 movups 0x50($inp),$rndkey0
2378 xorps $rndkey1,$inout5
2379 movups 0x60($inp),$iv # IV
2380 xorps $rndkey0,$inout6
2381 movups $inout0,($out)
2382 movups $inout1,0x10($out)
2383 movups $inout2,0x20($out)
2384 movups $inout3,0x30($out)
2385 movups $inout4,0x40($out)
2386 movups $inout5,0x50($out)
2388 movaps $inout6,$inout0
2390 jmp .Lcbc_dec_tail_collected
2394 &aesni_generate1("dec",$key,$rounds);
2399 jmp .Lcbc_dec_tail_collected
2402 xorps $inout2,$inout2
2403 call _aesni_decrypt3
2406 movups $inout0,($out)
2408 movaps $inout1,$inout0
2411 jmp .Lcbc_dec_tail_collected
2414 call _aesni_decrypt3
2417 movups $inout0,($out)
2419 movups $inout1,0x10($out)
2421 movaps $inout2,$inout0
2424 jmp .Lcbc_dec_tail_collected
2427 call _aesni_decrypt4
2429 movups 0x30($inp),$iv
2431 movups $inout0,($out)
2433 movups $inout1,0x10($out)
2435 movups $inout2,0x20($out)
2436 movaps $inout3,$inout0
2439 jmp .Lcbc_dec_tail_collected
2442 xorps $inout5,$inout5
2443 call _aesni_decrypt6
2444 movups 0x10($inp),$rndkey1
2445 movups 0x20($inp),$rndkey0
2448 xorps $rndkey1,$inout2
2449 movups 0x30($inp),$rndkey1
2450 xorps $rndkey0,$inout3
2451 movups 0x40($inp),$iv
2452 xorps $rndkey1,$inout4
2453 movups $inout0,($out)
2454 movups $inout1,0x10($out)
2455 movups $inout2,0x20($out)
2456 movups $inout3,0x30($out)
2458 movaps $inout4,$inout0
2460 jmp .Lcbc_dec_tail_collected
2463 call _aesni_decrypt6
2464 movups 0x10($inp),$rndkey1
2465 movups 0x20($inp),$rndkey0
2468 xorps $rndkey1,$inout2
2469 movups 0x30($inp),$rndkey1
2470 xorps $rndkey0,$inout3
2471 movups 0x40($inp),$rndkey0
2472 xorps $rndkey1,$inout4
2473 movups 0x50($inp),$iv
2474 xorps $rndkey0,$inout5
2475 movups $inout0,($out)
2476 movups $inout1,0x10($out)
2477 movups $inout2,0x20($out)
2478 movups $inout3,0x30($out)
2479 movups $inout4,0x40($out)
2481 movaps $inout5,$inout0
2483 jmp .Lcbc_dec_tail_collected
2485 .Lcbc_dec_tail_collected:
2488 jnz .Lcbc_dec_tail_partial
2489 movups $inout0,($out)
2492 .Lcbc_dec_tail_partial:
2493 movaps $inout0,(%rsp)
2498 .long 0x9066A4F3 # rep movsb
2502 $code.=<<___ if ($win64);
2503 movaps 0x10(%rsp),%xmm6
2504 movaps 0x20(%rsp),%xmm7
2505 movaps 0x30(%rsp),%xmm8
2506 movaps 0x40(%rsp),%xmm9
2513 .size ${PREFIX}_cbc_encrypt,.-${PREFIX}_cbc_encrypt
2516 # int $PREFIX_set_[en|de]crypt_key (const unsigned char *userKey,
2517 # int bits, AES_KEY *key)
2518 { my ($inp,$bits,$key) = @_4args;
2522 .globl ${PREFIX}_set_decrypt_key
2523 .type ${PREFIX}_set_decrypt_key,\@abi-omnipotent
2525 ${PREFIX}_set_decrypt_key:
2526 .byte 0x48,0x83,0xEC,0x08 # sub rsp,8
2527 call __aesni_set_encrypt_key
2528 shl \$4,$bits # rounds-1 after _aesni_set_encrypt_key
2531 lea 16($key,$bits),$inp # points at the end of key schedule
2533 $movkey ($key),%xmm0 # just swap
2534 $movkey ($inp),%xmm1
2535 $movkey %xmm0,($inp)
2536 $movkey %xmm1,($key)
2541 $movkey ($key),%xmm0 # swap and inverse
2542 $movkey ($inp),%xmm1
2547 $movkey %xmm0,16($inp)
2548 $movkey %xmm1,-16($key)
2550 ja .Ldec_key_inverse
2552 $movkey ($key),%xmm0 # inverse middle
2554 $movkey %xmm0,($inp)
2558 .LSEH_end_set_decrypt_key:
2559 .size ${PREFIX}_set_decrypt_key,.-${PREFIX}_set_decrypt_key
2562 # This is based on submission by
2564 # Huang Ying <ying.huang@intel.com>
2565 # Vinodh Gopal <vinodh.gopal@intel.com>
2568 # Agressively optimized in respect to aeskeygenassist's critical path
2569 # and is contained in %xmm0-5 to meet Win64 ABI requirement.
2572 .globl ${PREFIX}_set_encrypt_key
2573 .type ${PREFIX}_set_encrypt_key,\@abi-omnipotent
2575 ${PREFIX}_set_encrypt_key:
2576 __aesni_set_encrypt_key:
2577 .byte 0x48,0x83,0xEC,0x08 # sub rsp,8
2584 movups ($inp),%xmm0 # pull first 128 bits of *userKey
2585 xorps %xmm4,%xmm4 # low dword of xmm4 is assumed 0
2595 mov \$9,$bits # 10 rounds for 128-bit key
2596 $movkey %xmm0,($key) # round 0
2597 aeskeygenassist \$0x1,%xmm0,%xmm1 # round 1
2598 call .Lkey_expansion_128_cold
2599 aeskeygenassist \$0x2,%xmm0,%xmm1 # round 2
2600 call .Lkey_expansion_128
2601 aeskeygenassist \$0x4,%xmm0,%xmm1 # round 3
2602 call .Lkey_expansion_128
2603 aeskeygenassist \$0x8,%xmm0,%xmm1 # round 4
2604 call .Lkey_expansion_128
2605 aeskeygenassist \$0x10,%xmm0,%xmm1 # round 5
2606 call .Lkey_expansion_128
2607 aeskeygenassist \$0x20,%xmm0,%xmm1 # round 6
2608 call .Lkey_expansion_128
2609 aeskeygenassist \$0x40,%xmm0,%xmm1 # round 7
2610 call .Lkey_expansion_128
2611 aeskeygenassist \$0x80,%xmm0,%xmm1 # round 8
2612 call .Lkey_expansion_128
2613 aeskeygenassist \$0x1b,%xmm0,%xmm1 # round 9
2614 call .Lkey_expansion_128
2615 aeskeygenassist \$0x36,%xmm0,%xmm1 # round 10
2616 call .Lkey_expansion_128
2617 $movkey %xmm0,(%rax)
2618 mov $bits,80(%rax) # 240(%rdx)
2624 movq 16($inp),%xmm2 # remaining 1/3 of *userKey
2625 mov \$11,$bits # 12 rounds for 192
2626 $movkey %xmm0,($key) # round 0
2627 aeskeygenassist \$0x1,%xmm2,%xmm1 # round 1,2
2628 call .Lkey_expansion_192a_cold
2629 aeskeygenassist \$0x2,%xmm2,%xmm1 # round 2,3
2630 call .Lkey_expansion_192b
2631 aeskeygenassist \$0x4,%xmm2,%xmm1 # round 4,5
2632 call .Lkey_expansion_192a
2633 aeskeygenassist \$0x8,%xmm2,%xmm1 # round 5,6
2634 call .Lkey_expansion_192b
2635 aeskeygenassist \$0x10,%xmm2,%xmm1 # round 7,8
2636 call .Lkey_expansion_192a
2637 aeskeygenassist \$0x20,%xmm2,%xmm1 # round 8,9
2638 call .Lkey_expansion_192b
2639 aeskeygenassist \$0x40,%xmm2,%xmm1 # round 10,11
2640 call .Lkey_expansion_192a
2641 aeskeygenassist \$0x80,%xmm2,%xmm1 # round 11,12
2642 call .Lkey_expansion_192b
2643 $movkey %xmm0,(%rax)
2644 mov $bits,48(%rax) # 240(%rdx)
2650 movups 16($inp),%xmm2 # remaning half of *userKey
2651 mov \$13,$bits # 14 rounds for 256
2653 $movkey %xmm0,($key) # round 0
2654 $movkey %xmm2,16($key) # round 1
2655 aeskeygenassist \$0x1,%xmm2,%xmm1 # round 2
2656 call .Lkey_expansion_256a_cold
2657 aeskeygenassist \$0x1,%xmm0,%xmm1 # round 3
2658 call .Lkey_expansion_256b
2659 aeskeygenassist \$0x2,%xmm2,%xmm1 # round 4
2660 call .Lkey_expansion_256a
2661 aeskeygenassist \$0x2,%xmm0,%xmm1 # round 5
2662 call .Lkey_expansion_256b
2663 aeskeygenassist \$0x4,%xmm2,%xmm1 # round 6
2664 call .Lkey_expansion_256a
2665 aeskeygenassist \$0x4,%xmm0,%xmm1 # round 7
2666 call .Lkey_expansion_256b
2667 aeskeygenassist \$0x8,%xmm2,%xmm1 # round 8
2668 call .Lkey_expansion_256a
2669 aeskeygenassist \$0x8,%xmm0,%xmm1 # round 9
2670 call .Lkey_expansion_256b
2671 aeskeygenassist \$0x10,%xmm2,%xmm1 # round 10
2672 call .Lkey_expansion_256a
2673 aeskeygenassist \$0x10,%xmm0,%xmm1 # round 11
2674 call .Lkey_expansion_256b
2675 aeskeygenassist \$0x20,%xmm2,%xmm1 # round 12
2676 call .Lkey_expansion_256a
2677 aeskeygenassist \$0x20,%xmm0,%xmm1 # round 13
2678 call .Lkey_expansion_256b
2679 aeskeygenassist \$0x40,%xmm2,%xmm1 # round 14
2680 call .Lkey_expansion_256a
2681 $movkey %xmm0,(%rax)
2682 mov $bits,16(%rax) # 240(%rdx)
2692 .LSEH_end_set_encrypt_key:
2695 .Lkey_expansion_128:
2696 $movkey %xmm0,(%rax)
2698 .Lkey_expansion_128_cold:
2699 shufps \$0b00010000,%xmm0,%xmm4
2701 shufps \$0b10001100,%xmm0,%xmm4
2703 shufps \$0b11111111,%xmm1,%xmm1 # critical path
2708 .Lkey_expansion_192a:
2709 $movkey %xmm0,(%rax)
2711 .Lkey_expansion_192a_cold:
2713 .Lkey_expansion_192b_warm:
2714 shufps \$0b00010000,%xmm0,%xmm4
2717 shufps \$0b10001100,%xmm0,%xmm4
2720 pshufd \$0b01010101,%xmm1,%xmm1 # critical path
2723 pshufd \$0b11111111,%xmm0,%xmm3
2728 .Lkey_expansion_192b:
2730 shufps \$0b01000100,%xmm0,%xmm5
2731 $movkey %xmm5,(%rax)
2732 shufps \$0b01001110,%xmm2,%xmm3
2733 $movkey %xmm3,16(%rax)
2735 jmp .Lkey_expansion_192b_warm
2738 .Lkey_expansion_256a:
2739 $movkey %xmm2,(%rax)
2741 .Lkey_expansion_256a_cold:
2742 shufps \$0b00010000,%xmm0,%xmm4
2744 shufps \$0b10001100,%xmm0,%xmm4
2746 shufps \$0b11111111,%xmm1,%xmm1 # critical path
2751 .Lkey_expansion_256b:
2752 $movkey %xmm0,(%rax)
2755 shufps \$0b00010000,%xmm2,%xmm4
2757 shufps \$0b10001100,%xmm2,%xmm4
2759 shufps \$0b10101010,%xmm1,%xmm1 # critical path
2762 .size ${PREFIX}_set_encrypt_key,.-${PREFIX}_set_encrypt_key
2763 .size __aesni_set_encrypt_key,.-__aesni_set_encrypt_key
2770 .byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
2778 .byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
2780 .asciz "AES for Intel AES-NI, CRYPTOGAMS by <appro\@openssl.org>"
2784 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
2785 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
2793 .extern __imp_RtlVirtualUnwind
2795 $code.=<<___ if ($PREFIX eq "aesni");
2796 .type ecb_se_handler,\@abi-omnipotent
2810 mov 152($context),%rax # pull context->Rsp
2812 jmp .Lcommon_seh_tail
2813 .size ecb_se_handler,.-ecb_se_handler
2815 .type ccm64_se_handler,\@abi-omnipotent
2829 mov 120($context),%rax # pull context->Rax
2830 mov 248($context),%rbx # pull context->Rip
2832 mov 8($disp),%rsi # disp->ImageBase
2833 mov 56($disp),%r11 # disp->HandlerData
2835 mov 0(%r11),%r10d # HandlerData[0]
2836 lea (%rsi,%r10),%r10 # prologue label
2837 cmp %r10,%rbx # context->Rip<prologue label
2838 jb .Lcommon_seh_tail
2840 mov 152($context),%rax # pull context->Rsp
2842 mov 4(%r11),%r10d # HandlerData[1]
2843 lea (%rsi,%r10),%r10 # epilogue label
2844 cmp %r10,%rbx # context->Rip>=epilogue label
2845 jae .Lcommon_seh_tail
2847 lea 0(%rax),%rsi # %xmm save area
2848 lea 512($context),%rdi # &context.Xmm6
2849 mov \$8,%ecx # 4*sizeof(%xmm0)/sizeof(%rax)
2850 .long 0xa548f3fc # cld; rep movsq
2851 lea 0x58(%rax),%rax # adjust stack pointer
2853 jmp .Lcommon_seh_tail
2854 .size ccm64_se_handler,.-ccm64_se_handler
2856 .type ctr32_se_handler,\@abi-omnipotent
2870 mov 120($context),%rax # pull context->Rax
2871 mov 248($context),%rbx # pull context->Rip
2873 lea .Lctr32_body(%rip),%r10
2874 cmp %r10,%rbx # context->Rip<"prologue" label
2875 jb .Lcommon_seh_tail
2877 mov 152($context),%rax # pull context->Rsp
2879 lea .Lctr32_ret(%rip),%r10
2881 jae .Lcommon_seh_tail
2883 lea (%rax),%rsi # %xmm save area
2884 lea 512($context),%rdi # &context.Xmm6
2885 mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
2886 .long 0xa548f3fc # cld; rep movsq
2887 lea 0xa8(%rax),%rax # adjust stack pointer
2889 jmp .Lcommon_seh_tail
2890 .size ctr32_se_handler,.-ctr32_se_handler
2892 .type xts_se_handler,\@abi-omnipotent
2906 mov 120($context),%rax # pull context->Rax
2907 mov 248($context),%rbx # pull context->Rip
2909 mov 8($disp),%rsi # disp->ImageBase
2910 mov 56($disp),%r11 # disp->HandlerData
2912 mov 0(%r11),%r10d # HandlerData[0]
2913 lea (%rsi,%r10),%r10 # prologue lable
2914 cmp %r10,%rbx # context->Rip<prologue label
2915 jb .Lcommon_seh_tail
2917 mov 152($context),%rax # pull context->Rsp
2919 mov 4(%r11),%r10d # HandlerData[1]
2920 lea (%rsi,%r10),%r10 # epilogue label
2921 cmp %r10,%rbx # context->Rip>=epilogue label
2922 jae .Lcommon_seh_tail
2924 lea 0x60(%rax),%rsi # %xmm save area
2925 lea 512($context),%rdi # & context.Xmm6
2926 mov \$20,%ecx # 10*sizeof(%xmm0)/sizeof(%rax)
2927 .long 0xa548f3fc # cld; rep movsq
2929 jmp .Lcommon_rbp_tail
2930 .size xts_se_handler,.-xts_se_handler
2933 .type cbc_se_handler,\@abi-omnipotent
2947 mov 152($context),%rax # pull context->Rsp
2948 mov 248($context),%rbx # pull context->Rip
2950 lea .Lcbc_decrypt(%rip),%r10
2951 cmp %r10,%rbx # context->Rip<"prologue" label
2952 jb .Lcommon_seh_tail
2954 lea .Lcbc_decrypt_body(%rip),%r10
2955 cmp %r10,%rbx # context->Rip<cbc_decrypt_body
2956 jb .Lrestore_cbc_rax
2958 lea .Lcbc_ret(%rip),%r10
2959 cmp %r10,%rbx # context->Rip>="epilogue" label
2960 jae .Lcommon_seh_tail
2962 lea 16(%rax),%rsi # %xmm save area
2963 lea 512($context),%rdi # &context.Xmm6
2964 mov \$8,%ecx # 4*sizeof(%xmm0)/sizeof(%rax)
2965 .long 0xa548f3fc # cld; rep movsq
2968 mov 160($context),%rax # pull context->Rbp
2969 mov (%rax),%rbp # restore saved %rbp
2970 lea 8(%rax),%rax # adjust stack pointer
2971 mov %rbp,160($context) # restore context->Rbp
2972 jmp .Lcommon_seh_tail
2975 mov 120($context),%rax
2980 mov %rax,152($context) # restore context->Rsp
2981 mov %rsi,168($context) # restore context->Rsi
2982 mov %rdi,176($context) # restore context->Rdi
2984 mov 40($disp),%rdi # disp->ContextRecord
2985 mov $context,%rsi # context
2986 mov \$154,%ecx # sizeof(CONTEXT)
2987 .long 0xa548f3fc # cld; rep movsq
2990 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
2991 mov 8(%rsi),%rdx # arg2, disp->ImageBase
2992 mov 0(%rsi),%r8 # arg3, disp->ControlPc
2993 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
2994 mov 40(%rsi),%r10 # disp->ContextRecord
2995 lea 56(%rsi),%r11 # &disp->HandlerData
2996 lea 24(%rsi),%r12 # &disp->EstablisherFrame
2997 mov %r10,32(%rsp) # arg5
2998 mov %r11,40(%rsp) # arg6
2999 mov %r12,48(%rsp) # arg7
3000 mov %rcx,56(%rsp) # arg8, (NULL)
3001 call *__imp_RtlVirtualUnwind(%rip)
3003 mov \$1,%eax # ExceptionContinueSearch
3015 .size cbc_se_handler,.-cbc_se_handler
3020 $code.=<<___ if ($PREFIX eq "aesni");
3021 .rva .LSEH_begin_aesni_ecb_encrypt
3022 .rva .LSEH_end_aesni_ecb_encrypt
3025 .rva .LSEH_begin_aesni_ccm64_encrypt_blocks
3026 .rva .LSEH_end_aesni_ccm64_encrypt_blocks
3027 .rva .LSEH_info_ccm64_enc
3029 .rva .LSEH_begin_aesni_ccm64_decrypt_blocks
3030 .rva .LSEH_end_aesni_ccm64_decrypt_blocks
3031 .rva .LSEH_info_ccm64_dec
3033 .rva .LSEH_begin_aesni_ctr32_encrypt_blocks
3034 .rva .LSEH_end_aesni_ctr32_encrypt_blocks
3035 .rva .LSEH_info_ctr32
3037 .rva .LSEH_begin_aesni_xts_encrypt
3038 .rva .LSEH_end_aesni_xts_encrypt
3039 .rva .LSEH_info_xts_enc
3041 .rva .LSEH_begin_aesni_xts_decrypt
3042 .rva .LSEH_end_aesni_xts_decrypt
3043 .rva .LSEH_info_xts_dec
3046 .rva .LSEH_begin_${PREFIX}_cbc_encrypt
3047 .rva .LSEH_end_${PREFIX}_cbc_encrypt
3050 .rva ${PREFIX}_set_decrypt_key
3051 .rva .LSEH_end_set_decrypt_key
3054 .rva ${PREFIX}_set_encrypt_key
3055 .rva .LSEH_end_set_encrypt_key
3060 $code.=<<___ if ($PREFIX eq "aesni");
3064 .LSEH_info_ccm64_enc:
3066 .rva ccm64_se_handler
3067 .rva .Lccm64_enc_body,.Lccm64_enc_ret # HandlerData[]
3068 .LSEH_info_ccm64_dec:
3070 .rva ccm64_se_handler
3071 .rva .Lccm64_dec_body,.Lccm64_dec_ret # HandlerData[]
3074 .rva ctr32_se_handler
3078 .rva .Lxts_enc_body,.Lxts_enc_epilogue # HandlerData[]
3082 .rva .Lxts_dec_body,.Lxts_dec_epilogue # HandlerData[]
3089 .byte 0x01,0x04,0x01,0x00
3090 .byte 0x04,0x02,0x00,0x00 # sub rsp,8
3095 local *opcode=shift;
3099 $rex|=0x04 if($dst>=8);
3100 $rex|=0x01 if($src>=8);
3101 push @opcode,$rex|0x40 if($rex);
3108 if ($line=~/(aeskeygenassist)\s+\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
3109 rex(\@opcode,$4,$3);
3110 push @opcode,0x0f,0x3a,0xdf;
3111 push @opcode,0xc0|($3&7)|(($4&7)<<3); # ModR/M
3113 push @opcode,$c=~/^0/?oct($c):$c;
3114 return ".byte\t".join(',',@opcode);
3116 elsif ($line=~/(aes[a-z]+)\s+%xmm([0-9]+),\s*%xmm([0-9]+)/) {
3119 "aesenc" => 0xdc, "aesenclast" => 0xdd,
3120 "aesdec" => 0xde, "aesdeclast" => 0xdf
3122 return undef if (!defined($opcodelet{$1}));
3123 rex(\@opcode,$3,$2);
3124 push @opcode,0x0f,0x38,$opcodelet{$1};
3125 push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
3126 return ".byte\t".join(',',@opcode);
3131 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
3132 $code =~ s/\b(aes.*%xmm[0-9]+).*$/aesni($1)/gem;