2 # Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the OpenSSL license (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
10 # ====================================================================
11 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
12 # project. The module is, however, dual licensed under OpenSSL and
13 # CRYPTOGAMS licenses depending on where you obtain it. For further
14 # details see http://www.openssl.org/~appro/cryptogams/.
15 # ====================================================================
19 # This is AESNI-CBC+SHA1 "stitch" implementation. The idea, as spelled
20 # in http://download.intel.com/design/intarch/papers/323686.pdf, is
21 # that since AESNI-CBC encrypt exhibit *very* low instruction-level
22 # parallelism, interleaving it with another algorithm would allow to
23 # utilize processor resources better and achieve better performance.
24 # SHA1 instruction sequences(*) are taken from sha1-x86_64.pl and
25 # AESNI code is weaved into it. Below are performance numbers in
26 # cycles per processed byte, less is better, for standalone AESNI-CBC
27 # encrypt, sum of the latter and standalone SHA1, and "stitched"
30 # AES-128-CBC +SHA1 stitch gain
31 # Westmere 3.77[+5.3] 9.07 6.55 +38%
32 # Sandy Bridge 5.05[+5.0(6.1)] 10.06(11.15) 5.98(7.05) +68%(+58%)
33 # Ivy Bridge 5.05[+4.6] 9.65 5.54 +74%
34 # Haswell 4.43[+3.6(4.2)] 8.00(8.58) 4.55(5.21) +75%(+65%)
35 # Skylake 2.63[+3.5(4.1)] 6.17(6.69) 4.23(4.44) +46%(+51%)
36 # Bulldozer 5.77[+6.0] 11.72 6.37 +84%
39 # Westmere 4.51 9.81 6.80 +44%
40 # Sandy Bridge 6.05 11.06(12.15) 6.11(7.19) +81%(+69%)
41 # Ivy Bridge 6.05 10.65 6.07 +75%
42 # Haswell 5.29 8.86(9.44) 5.32(5.32) +67%(+77%)
43 # Bulldozer 6.89 12.84 6.96 +84%
46 # Westmere 5.25 10.55 7.21 +46%
47 # Sandy Bridge 7.05 12.06(13.15) 7.12(7.72) +69%(+70%)
48 # Ivy Bridge 7.05 11.65 7.12 +64%
49 # Haswell 6.19 9.76(10.34) 6.21(6.25) +57%(+65%)
50 # Skylake 3.62 7.16(7.68) 4.56(4.76) +57%(+61$)
51 # Bulldozer 8.00 13.95 8.25 +69%
53 # (*) There are two code paths: SSSE3 and AVX. See sha1-568.pl for
54 # background information. Above numbers in parentheses are SSSE3
55 # results collected on AVX-capable CPU, i.e. apply on OSes that
58 # Needless to mention that it makes no sense to implement "stitched"
59 # *decrypt* subroutine. Because *both* AESNI-CBC decrypt and SHA1
60 # fully utilize parallelism, so stitching would not give any gain
61 # anyway. Well, there might be some, e.g. because of better cache
62 # locality... For reference, here are performance results for
63 # standalone AESNI-CBC decrypt:
65 # AES-128-CBC AES-192-CBC AES-256-CBC
66 # Westmere 1.25 1.50 1.75
67 # Sandy Bridge 0.74 0.91 1.09
68 # Ivy Bridge 0.74 0.90 1.11
69 # Haswell 0.63 0.76 0.88
70 # Bulldozer 0.70 0.85 0.99
74 # AES-256-CBC +SHA1 stitch gain
75 # Westmere 1.75 7.20 6.68 +7.8%
76 # Sandy Bridge 1.09 6.09(7.22) 5.82(6.95) +4.6%(+3.9%)
77 # Ivy Bridge 1.11 5.70 5.45 +4.6%
78 # Haswell 0.88 4.45(5.00) 4.39(4.69) +1.4%(*)(+6.6%)
79 # Bulldozer 0.99 6.95 5.95 +17%(**)
81 # (*) Tiny improvement coefficient on Haswell is because we compare
82 # AVX1 stitch to sum with AVX2 SHA1.
83 # (**) Execution is fully dominated by integer code sequence and
84 # SIMD still hardly shows [in single-process benchmark;-]
88 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
90 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
92 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
93 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
94 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
95 die "can't locate x86_64-xlate.pl";
97 $avx=1 if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
98 =~ /GNU assembler version ([2-9]\.[0-9]+)/ &&
100 $avx=1 if (!$avx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
101 `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/ &&
103 $avx=1 if (!$avx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
104 `ml64 2>&1` =~ /Version ([0-9]+)\./ &&
106 $avx=1 if (!$avx && `$ENV{CC} -v 2>&1` =~ /((?:^clang|LLVM) version|.*based on LLVM) ([3-9]\.[0-9]+)/ && $2>=3.0);
108 $shaext=1; ### set to zero if compiling for 1.0.1
112 open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\"";
115 # void aesni_cbc_sha1_enc(const void *inp,
118 # const AES_KEY *key,
125 .extern OPENSSL_ia32cap_P
127 .globl aesni_cbc_sha1_enc
128 .type aesni_cbc_sha1_enc,\@abi-omnipotent
131 # caller should check for SSSE3 and AES-NI bits
132 mov OPENSSL_ia32cap_P+0(%rip),%r10d
133 mov OPENSSL_ia32cap_P+4(%rip),%r11
135 $code.=<<___ if ($shaext);
136 bt \$61,%r11 # check SHA bit
137 jc aesni_cbc_sha1_enc_shaext
139 $code.=<<___ if ($avx);
140 and \$`1<<28`,%r11d # mask AVX bit
141 and \$`1<<30`,%r10d # mask "Intel CPU" bit
143 cmp \$`1<<28|1<<30`,%r10d
144 je aesni_cbc_sha1_enc_avx
147 jmp aesni_cbc_sha1_enc_ssse3
149 .size aesni_cbc_sha1_enc,.-aesni_cbc_sha1_enc
152 my ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
155 my @X=map("%xmm$_",(4..7,0..3));
156 my @Tx=map("%xmm$_",(8..10));
157 my @V=($A,$B,$C,$D,$E)=("%eax","%ebx","%ecx","%edx","%ebp"); # size optimization
158 my @T=("%esi","%edi");
159 my $j=0; my $jj=0; my $r=0; my $sn=0; my $rx=0;
161 my ($rndkey0,$iv,$in)=map("%xmm$_",(11..13)); # for enc
162 my @rndkey=("%xmm14","%xmm15"); # for enc
163 my ($inout0,$inout1,$inout2,$inout3)=map("%xmm$_",(12..15)); # for dec
165 if (1) { # reassign for Atom Silvermont
166 # The goal is to minimize amount of instructions with more than
167 # 3 prefix bytes. Or in more practical terms to keep AES-NI *and*
168 # SSSE3 instructions to upper half of the register bank.
169 @X=map("%xmm$_",(8..11,4..7));
170 @Tx=map("%xmm$_",(12,13,3));
171 ($iv,$in,$rndkey0)=map("%xmm$_",(2,14,15));
172 @rndkey=("%xmm0","%xmm1");
175 sub AUTOLOAD() # thunk [simplified] 32-bit style perlasm
176 { my $opcode = $AUTOLOAD; $opcode =~ s/.*:://;
178 $arg = "\$$arg" if ($arg*1 eq $arg);
179 $code .= "\t$opcode\t".join(',',$arg,reverse @_)."\n";
182 my $_rol=sub { &rol(@_) };
183 my $_ror=sub { &ror(@_) };
186 .type aesni_cbc_sha1_enc_ssse3,\@function,6
188 aesni_cbc_sha1_enc_ssse3:
190 mov `($win64?56:8)`(%rsp),$inp # load 7th argument
191 #shr \$6,$len # debugging artefact
192 #jz .Lepilogue_ssse3 # debugging artefact
205 lea `-104-($win64?10*16:0)`(%rsp),%rsp
206 .cfi_adjust_cfa_offset `104+($win64?10*16:0)`
207 #mov $in0,$inp # debugging artefact
208 #lea 64(%rsp),$ctx # debugging artefact
210 $code.=<<___ if ($win64);
211 movaps %xmm6,96+0(%rsp)
212 movaps %xmm7,96+16(%rsp)
213 movaps %xmm8,96+32(%rsp)
214 movaps %xmm9,96+48(%rsp)
215 movaps %xmm10,96+64(%rsp)
216 movaps %xmm11,96+80(%rsp)
217 movaps %xmm12,96+96(%rsp)
218 movaps %xmm13,96+112(%rsp)
219 movaps %xmm14,96+128(%rsp)
220 movaps %xmm15,96+144(%rsp)
224 mov $in0,%r12 # reassign arguments
227 lea 112($key),%r15 # size optimization
228 movdqu ($ivp),$iv # load IV
229 mov $ivp,88(%rsp) # save $ivp
231 ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
232 my $rounds="${ivp}d";
236 mov 240-112($key),$rounds
237 add $inp,$len # end of input
239 lea K_XX_XX(%rip),$K_XX_XX
240 mov 0($ctx),$A # load context
244 mov $B,@T[0] # magic seed
250 movdqa 64($K_XX_XX),@Tx[2] # pbswap mask
251 movdqa 0($K_XX_XX),@Tx[1] # K_00_19
252 movdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
253 movdqu 16($inp),@X[-3&7]
254 movdqu 32($inp),@X[-2&7]
255 movdqu 48($inp),@X[-1&7]
256 pshufb @Tx[2],@X[-4&7] # byte swap
257 pshufb @Tx[2],@X[-3&7]
258 pshufb @Tx[2],@X[-2&7]
260 paddd @Tx[1],@X[-4&7] # add K_00_19
261 pshufb @Tx[2],@X[-1&7]
262 paddd @Tx[1],@X[-3&7]
263 paddd @Tx[1],@X[-2&7]
264 movdqa @X[-4&7],0(%rsp) # X[]+K xfer to IALU
265 psubd @Tx[1],@X[-4&7] # restore X[]
266 movdqa @X[-3&7],16(%rsp)
267 psubd @Tx[1],@X[-3&7]
268 movdqa @X[-2&7],32(%rsp)
269 psubd @Tx[1],@X[-2&7]
270 movups -112($key),$rndkey0 # $key[0]
271 movups 16-112($key),$rndkey[0] # forward reference
277 my ($n,$k)=($r/10,$r%10);
280 movups `16*$n`($in0),$in # load input
283 $code.=<<___ if ($n);
284 movups $iv,`16*($n-1)`($out,$in0) # write output
288 movups `32+16*$k-112`($key),$rndkey[1]
289 aesenc $rndkey[0],$iv
296 movups `32+16*($k+0)-112`($key),$rndkey[1]
297 aesenc $rndkey[0],$iv
298 movups `32+16*($k+1)-112`($key),$rndkey[0]
299 aesenc $rndkey[1],$iv
301 movups `32+16*($k+2)-112`($key),$rndkey[1]
302 aesenc $rndkey[0],$iv
303 movups `32+16*($k+3)-112`($key),$rndkey[0]
304 aesenc $rndkey[1],$iv
306 aesenclast $rndkey[0],$iv
307 movups 16-112($key),$rndkey[1] # forward reference
311 movups `32+16*$k-112`($key),$rndkey[1]
312 aesenc $rndkey[0],$iv
315 $r++; unshift(@rndkey,pop(@rndkey));
318 sub Xupdate_ssse3_16_31() # recall that $Xi starts wtih 4
321 my @insns = (&$body,&$body,&$body,&$body); # 40 instructions
324 eval(shift(@insns)); # ror
325 &pshufd (@X[0],@X[-4&7],0xee); # was &movdqa (@X[0],@X[-3&7]);
327 &movdqa (@Tx[0],@X[-1&7]);
328 &paddd (@Tx[1],@X[-1&7]);
332 &punpcklqdq(@X[0],@X[-3&7]); # compose "X[-14]" in "X[0]", was &palignr(@X[0],@X[-4&7],8);
334 eval(shift(@insns)); # rol
336 &psrldq (@Tx[0],4); # "X[-3]", 3 dwords
340 &pxor (@X[0],@X[-4&7]); # "X[0]"^="X[-16]"
342 eval(shift(@insns)); # ror
343 &pxor (@Tx[0],@X[-2&7]); # "X[-3]"^"X[-8]"
348 &pxor (@X[0],@Tx[0]); # "X[0]"^="X[-3]"^"X[-8]"
350 eval(shift(@insns)); # rol
351 &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
355 &movdqa (@Tx[2],@X[0]);
358 eval(shift(@insns)); # ror
359 &movdqa (@Tx[0],@X[0]);
362 &pslldq (@Tx[2],12); # "X[0]"<<96, extract one dword
363 &paddd (@X[0],@X[0]);
369 eval(shift(@insns)); # rol
371 &movdqa (@Tx[1],@Tx[2]);
377 eval(shift(@insns)); # ror
378 &por (@X[0],@Tx[0]); # "X[0]"<<<=1
384 &pxor (@X[0],@Tx[2]);
386 &movdqa (@Tx[2],eval(16*(($Xi)/5))."($K_XX_XX)"); # K_XX_XX
387 eval(shift(@insns)); # rol
391 &pxor (@X[0],@Tx[1]); # "X[0]"^=("X[0]">>96)<<<2
392 &pshufd (@Tx[1],@X[-1&7],0xee) if ($Xi==7); # was &movdqa (@Tx[0],@X[-1&7]) in Xupdate_ssse3_32_79
394 foreach (@insns) { eval; } # remaining instructions [if any]
396 $Xi++; push(@X,shift(@X)); # "rotate" X[]
397 push(@Tx,shift(@Tx));
400 sub Xupdate_ssse3_32_79()
403 my @insns = (&$body,&$body,&$body,&$body); # 32 to 44 instructions
406 eval(shift(@insns)) if ($Xi==8);
407 &pxor (@X[0],@X[-4&7]); # "X[0]"="X[-32]"^"X[-16]"
408 eval(shift(@insns)) if ($Xi==8);
409 eval(shift(@insns)); # body_20_39
411 eval(shift(@insns)) if (@insns[1] =~ /_ror/);
412 eval(shift(@insns)) if (@insns[0] =~ /_ror/);
413 &punpcklqdq(@Tx[0],@X[-1&7]); # compose "X[-6]", was &palignr(@Tx[0],@X[-2&7],8);
415 eval(shift(@insns)); # rol
417 &pxor (@X[0],@X[-7&7]); # "X[0]"^="X[-28]"
421 &movdqa (@Tx[2],@Tx[1]);# "perpetuate" K_XX_XX...
422 } else { # ... or load next one
423 &movdqa (@Tx[2],eval(16*($Xi/5))."($K_XX_XX)");
425 eval(shift(@insns)); # ror
426 &paddd (@Tx[1],@X[-1&7]);
429 &pxor (@X[0],@Tx[0]); # "X[0]"^="X[-6]"
430 eval(shift(@insns)); # body_20_39
433 eval(shift(@insns)); # rol
434 eval(shift(@insns)) if (@insns[0] =~ /_ror/);
436 &movdqa (@Tx[0],@X[0]);
439 &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
440 eval(shift(@insns)); # ror
442 eval(shift(@insns)); # body_20_39
448 eval(shift(@insns)) if (@insns[0] =~ /_rol/);# rol
451 eval(shift(@insns)); # ror
453 &por (@X[0],@Tx[0]); # "X[0]"<<<=2
455 eval(shift(@insns)); # body_20_39
456 eval(shift(@insns)) if (@insns[1] =~ /_rol/);
457 eval(shift(@insns)) if (@insns[0] =~ /_rol/);
458 &pshufd(@Tx[1],@X[-1&7],0xee) if ($Xi<19); # was &movdqa (@Tx[1],@X[0])
460 eval(shift(@insns)); # rol
463 eval(shift(@insns)); # rol
466 foreach (@insns) { eval; } # remaining instructions
468 $Xi++; push(@X,shift(@X)); # "rotate" X[]
469 push(@Tx,shift(@Tx));
472 sub Xuplast_ssse3_80()
475 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
482 &paddd (@Tx[1],@X[-1&7]);
486 &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer IALU
488 foreach (@insns) { eval; } # remaining instructions
493 unshift(@Tx,pop(@Tx));
495 &movdqa (@Tx[2],"64($K_XX_XX)"); # pbswap mask
496 &movdqa (@Tx[1],"0($K_XX_XX)"); # K_00_19
497 &movdqu (@X[-4&7],"0($inp)"); # load input
498 &movdqu (@X[-3&7],"16($inp)");
499 &movdqu (@X[-2&7],"32($inp)");
500 &movdqu (@X[-1&7],"48($inp)");
501 &pshufb (@X[-4&7],@Tx[2]); # byte swap
510 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
516 &pshufb (@X[($Xi-3)&7],@Tx[2]);
521 &paddd (@X[($Xi-4)&7],@Tx[1]);
526 &movdqa (eval(16*$Xi)."(%rsp)",@X[($Xi-4)&7]); # X[]+K xfer to IALU
531 &psubd (@X[($Xi-4)&7],@Tx[1]);
533 foreach (@insns) { eval; }
540 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
543 foreach (@insns) { eval; }
547 '($a,$b,$c,$d,$e)=@V;'.
548 '&$_ror ($b,$j?7:2);', # $b>>>2
550 '&mov (@T[1],$a);', # $b for next round
552 '&add ($e,eval(4*($j&15))."(%rsp)");',# X[]+K xfer
553 '&xor ($b,$c);', # $c^$d for next round
557 '&and (@T[1],$b);', # ($b&($c^$d)) for next round
559 '&xor ($b,$c);', # restore $b
560 '&add ($e,$a);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
563 sub body_00_19 () { # ((c^d)&b)^d
564 # on start @T[0]=(c^d)&b
565 return &body_20_39() if ($rx==19); $rx++;
572 $k = (($jj+1)*12/20)*20*$n/12; # 12 aesencs per these 20 rounds
573 @r[$k%$n].='&$aesenc();' if ($jj==$k/$n);
580 '($a,$b,$c,$d,$e)=@V;'.
581 '&add ($e,eval(4*($j&15))."(%rsp)");',# X[]+K xfer
582 '&xor (@T[0],$d) if($j==19);'.
583 '&xor (@T[0],$c) if($j> 19);', # ($b^$d^$c)
584 '&mov (@T[1],$a);', # $b for next round
588 '&xor (@T[1],$c) if ($j< 79);', # $b^$d for next round
590 '&$_ror ($b,7);', # $b>>>2
591 '&add ($e,$a);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
594 sub body_20_39 () { # b^d^c
596 return &body_40_59() if ($rx==39); $rx++;
603 $k = (($jj+1)*8/20)*20*$n/8; # 8 aesencs per these 20 rounds
604 @r[$k%$n].='&$aesenc();' if ($jj==$k/$n && $rx!=20);
611 '($a,$b,$c,$d,$e)=@V;'.
612 '&add ($e,eval(4*($j&15))."(%rsp)");',# X[]+K xfer
613 '&and (@T[0],$c) if ($j>=40);', # (b^c)&(c^d)
614 '&xor ($c,$d) if ($j>=40);', # restore $c
616 '&$_ror ($b,7);', # $b>>>2
617 '&mov (@T[1],$a);', # $b for next round
622 '&xor (@T[1],$c) if ($j==59);'.
623 '&xor (@T[1],$b) if ($j< 59);', # b^c for next round
625 '&xor ($b,$c) if ($j< 59);', # c^d for next round
626 '&add ($e,$a);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
629 sub body_40_59 () { # ((b^c)&(c^d))^c
630 # on entry @T[0]=(b^c), (c^=d)
638 $k=(($jj+1)*12/20)*20*$n/12; # 12 aesencs per these 20 rounds
639 @r[$k%$n].='&$aesenc();' if ($jj==$k/$n && $rx!=40);
648 &Xupdate_ssse3_16_31(\&body_00_19);
649 &Xupdate_ssse3_16_31(\&body_00_19);
650 &Xupdate_ssse3_16_31(\&body_00_19);
651 &Xupdate_ssse3_16_31(\&body_00_19);
652 &Xupdate_ssse3_32_79(\&body_00_19);
653 &Xupdate_ssse3_32_79(\&body_20_39);
654 &Xupdate_ssse3_32_79(\&body_20_39);
655 &Xupdate_ssse3_32_79(\&body_20_39);
656 &Xupdate_ssse3_32_79(\&body_20_39);
657 &Xupdate_ssse3_32_79(\&body_20_39);
658 &Xupdate_ssse3_32_79(\&body_40_59);
659 &Xupdate_ssse3_32_79(\&body_40_59);
660 &Xupdate_ssse3_32_79(\&body_40_59);
661 &Xupdate_ssse3_32_79(\&body_40_59);
662 &Xupdate_ssse3_32_79(\&body_40_59);
663 &Xupdate_ssse3_32_79(\&body_20_39);
664 &Xuplast_ssse3_80(\&body_20_39,".Ldone_ssse3"); # can jump to "done"
666 $saved_j=$j; @saved_V=@V;
667 $saved_r=$r; @saved_rndkey=@rndkey;
669 &Xloop_ssse3(\&body_20_39);
670 &Xloop_ssse3(\&body_20_39);
671 &Xloop_ssse3(\&body_20_39);
674 movups $iv,48($out,$in0) # write output
677 add 0($ctx),$A # update context
684 mov @T[0],$B # magic seed
695 $jj=$j=$saved_j; @V=@saved_V;
696 $r=$saved_r; @rndkey=@saved_rndkey;
698 &Xtail_ssse3(\&body_20_39);
699 &Xtail_ssse3(\&body_20_39);
700 &Xtail_ssse3(\&body_20_39);
703 movups $iv,48($out,$in0) # write output
704 mov 88(%rsp),$ivp # restore $ivp
706 add 0($ctx),$A # update context
716 movups $iv,($ivp) # write IV
718 $code.=<<___ if ($win64);
719 movaps 96+0(%rsp),%xmm6
720 movaps 96+16(%rsp),%xmm7
721 movaps 96+32(%rsp),%xmm8
722 movaps 96+48(%rsp),%xmm9
723 movaps 96+64(%rsp),%xmm10
724 movaps 96+80(%rsp),%xmm11
725 movaps 96+96(%rsp),%xmm12
726 movaps 96+112(%rsp),%xmm13
727 movaps 96+128(%rsp),%xmm14
728 movaps 96+144(%rsp),%xmm15
731 lea `104+($win64?10*16:0)`(%rsp),%rsi
750 .size aesni_cbc_sha1_enc_ssse3,.-aesni_cbc_sha1_enc_ssse3
753 if ($stitched_decrypt) {{{
755 ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
759 # reassign for Atom Silvermont (see above)
760 ($inout0,$inout1,$inout2,$inout3,$rndkey0)=map("%xmm$_",(0..4));
761 @X=map("%xmm$_",(8..13,6,7));
762 @Tx=map("%xmm$_",(14,15,5));
765 '&movdqu($inout0,"0x00($in0)");',
766 '&movdqu($inout1,"0x10($in0)"); &pxor ($inout0,$rndkey0);',
767 '&movdqu($inout2,"0x20($in0)"); &pxor ($inout1,$rndkey0);',
768 '&movdqu($inout3,"0x30($in0)"); &pxor ($inout2,$rndkey0);',
770 '&pxor ($inout3,$rndkey0); &movups ($rndkey0,"16-112($key)");',
771 '&movaps("64(%rsp)",@X[2]);', # save IV, originally @X[3]
774 for ($i=0;$i<13;$i++) {
776 '&aesdec ($inout0,$rndkey0);',
777 '&aesdec ($inout1,$rndkey0);',
778 '&aesdec ($inout2,$rndkey0);',
779 '&aesdec ($inout3,$rndkey0); &movups($rndkey0,"'.(16*($i+2)-112).'($key)");'
781 push (@aes256_dec,(undef,undef)) if (($i>=3 && $i<=5) || $i>=11);
782 push (@aes256_dec,(undef,undef)) if ($i==5);
785 '&aesdeclast ($inout0,$rndkey0); &movups (@X[0],"0x00($in0)");',
786 '&aesdeclast ($inout1,$rndkey0); &movups (@X[1],"0x10($in0)");',
787 '&aesdeclast ($inout2,$rndkey0); &movups (@X[2],"0x20($in0)");',
788 '&aesdeclast ($inout3,$rndkey0); &movups (@X[3],"0x30($in0)");',
790 '&xorps ($inout0,"64(%rsp)"); &movdqu ($rndkey0,"-112($key)");',
791 '&xorps ($inout1,@X[0]); &movups ("0x00($out,$in0)",$inout0);',
792 '&xorps ($inout2,@X[1]); &movups ("0x10($out,$in0)",$inout1);',
793 '&xorps ($inout3,@X[2]); &movups ("0x20($out,$in0)",$inout2);',
795 '&movups ("0x30($out,$in0)",$inout3);'
798 sub body_00_19_dec () { # ((c^d)&b)^d
799 # on start @T[0]=(c^d)&b
800 return &body_20_39_dec() if ($rx==19);
804 unshift (@r,@aes256_dec[$rx]) if (@aes256_dec[$rx]);
810 sub body_20_39_dec () { # b^d^c
812 return &body_40_59_dec() if ($rx==39);
816 unshift (@r,@aes256_dec[$rx]) if (@aes256_dec[$rx]);
822 sub body_40_59_dec () { # ((b^c)&(c^d))^c
823 # on entry @T[0]=(b^c), (c^=d)
827 unshift (@r,@aes256_dec[$rx]) if (@aes256_dec[$rx]);
834 .globl aesni256_cbc_sha1_dec
835 .type aesni256_cbc_sha1_dec,\@abi-omnipotent
837 aesni256_cbc_sha1_dec:
838 # caller should check for SSSE3 and AES-NI bits
839 mov OPENSSL_ia32cap_P+0(%rip),%r10d
840 mov OPENSSL_ia32cap_P+4(%rip),%r11d
842 $code.=<<___ if ($avx);
843 and \$`1<<28`,%r11d # mask AVX bit
844 and \$`1<<30`,%r10d # mask "Intel CPU" bit
846 cmp \$`1<<28|1<<30`,%r10d
847 je aesni256_cbc_sha1_dec_avx
850 jmp aesni256_cbc_sha1_dec_ssse3
852 .size aesni256_cbc_sha1_dec,.-aesni256_cbc_sha1_dec
854 .type aesni256_cbc_sha1_dec_ssse3,\@function,6
856 aesni256_cbc_sha1_dec_ssse3:
858 mov `($win64?56:8)`(%rsp),$inp # load 7th argument
871 lea `-104-($win64?10*16:0)`(%rsp),%rsp
872 .cfi_adjust_cfa_offset `104+($win64?10*16:0)`
874 $code.=<<___ if ($win64);
875 movaps %xmm6,96+0(%rsp)
876 movaps %xmm7,96+16(%rsp)
877 movaps %xmm8,96+32(%rsp)
878 movaps %xmm9,96+48(%rsp)
879 movaps %xmm10,96+64(%rsp)
880 movaps %xmm11,96+80(%rsp)
881 movaps %xmm12,96+96(%rsp)
882 movaps %xmm13,96+112(%rsp)
883 movaps %xmm14,96+128(%rsp)
884 movaps %xmm15,96+144(%rsp)
885 .Lprologue_dec_ssse3:
888 mov $in0,%r12 # reassign arguments
891 lea 112($key),%r15 # size optimization
892 movdqu ($ivp),@X[3] # load IV
893 #mov $ivp,88(%rsp) # save $ivp
895 ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
899 add $inp,$len # end of input
901 lea K_XX_XX(%rip),$K_XX_XX
902 mov 0($ctx),$A # load context
906 mov $B,@T[0] # magic seed
912 movdqa 64($K_XX_XX),@Tx[2] # pbswap mask
913 movdqa 0($K_XX_XX),@Tx[1] # K_00_19
914 movdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
915 movdqu 16($inp),@X[-3&7]
916 movdqu 32($inp),@X[-2&7]
917 movdqu 48($inp),@X[-1&7]
918 pshufb @Tx[2],@X[-4&7] # byte swap
920 pshufb @Tx[2],@X[-3&7]
921 pshufb @Tx[2],@X[-2&7]
922 pshufb @Tx[2],@X[-1&7]
923 paddd @Tx[1],@X[-4&7] # add K_00_19
924 paddd @Tx[1],@X[-3&7]
925 paddd @Tx[1],@X[-2&7]
926 movdqa @X[-4&7],0(%rsp) # X[]+K xfer to IALU
927 psubd @Tx[1],@X[-4&7] # restore X[]
928 movdqa @X[-3&7],16(%rsp)
929 psubd @Tx[1],@X[-3&7]
930 movdqa @X[-2&7],32(%rsp)
931 psubd @Tx[1],@X[-2&7]
932 movdqu -112($key),$rndkey0 # $key[0]
938 &Xupdate_ssse3_16_31(\&body_00_19_dec);
939 &Xupdate_ssse3_16_31(\&body_00_19_dec);
940 &Xupdate_ssse3_16_31(\&body_00_19_dec);
941 &Xupdate_ssse3_16_31(\&body_00_19_dec);
942 &Xupdate_ssse3_32_79(\&body_00_19_dec);
943 &Xupdate_ssse3_32_79(\&body_20_39_dec);
944 &Xupdate_ssse3_32_79(\&body_20_39_dec);
945 &Xupdate_ssse3_32_79(\&body_20_39_dec);
946 &Xupdate_ssse3_32_79(\&body_20_39_dec);
947 &Xupdate_ssse3_32_79(\&body_20_39_dec);
948 &Xupdate_ssse3_32_79(\&body_40_59_dec);
949 &Xupdate_ssse3_32_79(\&body_40_59_dec);
950 &Xupdate_ssse3_32_79(\&body_40_59_dec);
951 &Xupdate_ssse3_32_79(\&body_40_59_dec);
952 &Xupdate_ssse3_32_79(\&body_40_59_dec);
953 &Xupdate_ssse3_32_79(\&body_20_39_dec);
954 &Xuplast_ssse3_80(\&body_20_39_dec,".Ldone_dec_ssse3"); # can jump to "done"
956 $saved_j=$j; @saved_V=@V;
959 &Xloop_ssse3(\&body_20_39_dec);
960 &Xloop_ssse3(\&body_20_39_dec);
961 &Xloop_ssse3(\&body_20_39_dec);
963 eval(@aes256_dec[-1]); # last store
967 add 0($ctx),$A # update context
974 mov @T[0],$B # magic seed
985 $jj=$j=$saved_j; @V=@saved_V;
988 &Xtail_ssse3(\&body_20_39_dec);
989 &Xtail_ssse3(\&body_20_39_dec);
990 &Xtail_ssse3(\&body_20_39_dec);
992 eval(@aes256_dec[-1]); # last store
994 add 0($ctx),$A # update context
1004 movups @X[3],($ivp) # write IV
1006 $code.=<<___ if ($win64);
1007 movaps 96+0(%rsp),%xmm6
1008 movaps 96+16(%rsp),%xmm7
1009 movaps 96+32(%rsp),%xmm8
1010 movaps 96+48(%rsp),%xmm9
1011 movaps 96+64(%rsp),%xmm10
1012 movaps 96+80(%rsp),%xmm11
1013 movaps 96+96(%rsp),%xmm12
1014 movaps 96+112(%rsp),%xmm13
1015 movaps 96+128(%rsp),%xmm14
1016 movaps 96+144(%rsp),%xmm15
1019 lea `104+($win64?10*16:0)`(%rsp),%rsi
1020 .cfi_cfa_def %rsi,56
1035 .Lepilogue_dec_ssse3:
1038 .size aesni256_cbc_sha1_dec_ssse3,.-aesni256_cbc_sha1_dec_ssse3
1044 my ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
1047 my @X=map("%xmm$_",(4..7,0..3));
1048 my @Tx=map("%xmm$_",(8..10));
1049 my @V=($A,$B,$C,$D,$E)=("%eax","%ebx","%ecx","%edx","%ebp"); # size optimization
1050 my @T=("%esi","%edi");
1051 my ($rndkey0,$iv,$in)=map("%xmm$_",(11..13));
1052 my @rndkey=("%xmm14","%xmm15");
1053 my ($inout0,$inout1,$inout2,$inout3)=map("%xmm$_",(12..15)); # for dec
1056 my $_rol=sub { &shld(@_[0],@_) };
1057 my $_ror=sub { &shrd(@_[0],@_) };
1060 .type aesni_cbc_sha1_enc_avx,\@function,6
1062 aesni_cbc_sha1_enc_avx:
1064 mov `($win64?56:8)`(%rsp),$inp # load 7th argument
1065 #shr \$6,$len # debugging artefact
1066 #jz .Lepilogue_avx # debugging artefact
1079 lea `-104-($win64?10*16:0)`(%rsp),%rsp
1080 .cfi_adjust_cfa_offset `104+($win64?10*16:0)`
1081 #mov $in0,$inp # debugging artefact
1082 #lea 64(%rsp),$ctx # debugging artefact
1084 $code.=<<___ if ($win64);
1085 movaps %xmm6,96+0(%rsp)
1086 movaps %xmm7,96+16(%rsp)
1087 movaps %xmm8,96+32(%rsp)
1088 movaps %xmm9,96+48(%rsp)
1089 movaps %xmm10,96+64(%rsp)
1090 movaps %xmm11,96+80(%rsp)
1091 movaps %xmm12,96+96(%rsp)
1092 movaps %xmm13,96+112(%rsp)
1093 movaps %xmm14,96+128(%rsp)
1094 movaps %xmm15,96+144(%rsp)
1099 mov $in0,%r12 # reassign arguments
1102 lea 112($key),%r15 # size optimization
1103 vmovdqu ($ivp),$iv # load IV
1104 mov $ivp,88(%rsp) # save $ivp
1106 ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
1107 my $rounds="${ivp}d";
1111 mov 240-112($key),$rounds
1112 add $inp,$len # end of input
1114 lea K_XX_XX(%rip),$K_XX_XX
1115 mov 0($ctx),$A # load context
1119 mov $B,@T[0] # magic seed
1125 vmovdqa 64($K_XX_XX),@X[2] # pbswap mask
1126 vmovdqa 0($K_XX_XX),$Kx # K_00_19
1127 vmovdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
1128 vmovdqu 16($inp),@X[-3&7]
1129 vmovdqu 32($inp),@X[-2&7]
1130 vmovdqu 48($inp),@X[-1&7]
1131 vpshufb @X[2],@X[-4&7],@X[-4&7] # byte swap
1133 vpshufb @X[2],@X[-3&7],@X[-3&7]
1134 vpshufb @X[2],@X[-2&7],@X[-2&7]
1135 vpshufb @X[2],@X[-1&7],@X[-1&7]
1136 vpaddd $Kx,@X[-4&7],@X[0] # add K_00_19
1137 vpaddd $Kx,@X[-3&7],@X[1]
1138 vpaddd $Kx,@X[-2&7],@X[2]
1139 vmovdqa @X[0],0(%rsp) # X[]+K xfer to IALU
1140 vmovdqa @X[1],16(%rsp)
1141 vmovdqa @X[2],32(%rsp)
1142 vmovups -112($key),$rndkey[1] # $key[0]
1143 vmovups 16-112($key),$rndkey[0] # forward reference
1149 my ($n,$k)=($r/10,$r%10);
1152 vmovdqu `16*$n`($in0),$in # load input
1153 vpxor $rndkey[1],$in,$in
1155 $code.=<<___ if ($n);
1156 vmovups $iv,`16*($n-1)`($out,$in0) # write output
1160 vaesenc $rndkey[0],$iv,$iv
1161 vmovups `32+16*$k-112`($key),$rndkey[1]
1168 vaesenc $rndkey[0],$iv,$iv
1169 vmovups `32+16*($k+0)-112`($key),$rndkey[1]
1170 vaesenc $rndkey[1],$iv,$iv
1171 vmovups `32+16*($k+1)-112`($key),$rndkey[0]
1173 vaesenc $rndkey[0],$iv,$iv
1174 vmovups `32+16*($k+2)-112`($key),$rndkey[1]
1175 vaesenc $rndkey[1],$iv,$iv
1176 vmovups `32+16*($k+3)-112`($key),$rndkey[0]
1178 vaesenclast $rndkey[0],$iv,$iv
1179 vmovups -112($key),$rndkey[0]
1180 vmovups 16-112($key),$rndkey[1] # forward reference
1184 vaesenc $rndkey[0],$iv,$iv
1185 vmovups `32+16*$k-112`($key),$rndkey[1]
1188 $r++; unshift(@rndkey,pop(@rndkey));
1191 sub Xupdate_avx_16_31() # recall that $Xi starts wtih 4
1194 my @insns = (&$body,&$body,&$body,&$body); # 40 instructions
1195 my ($a,$b,$c,$d,$e);
1197 eval(shift(@insns));
1198 eval(shift(@insns));
1199 &vpalignr(@X[0],@X[-3&7],@X[-4&7],8); # compose "X[-14]" in "X[0]"
1200 eval(shift(@insns));
1201 eval(shift(@insns));
1203 &vpaddd (@Tx[1],$Kx,@X[-1&7]);
1204 eval(shift(@insns));
1205 eval(shift(@insns));
1206 &vpsrldq(@Tx[0],@X[-1&7],4); # "X[-3]", 3 dwords
1207 eval(shift(@insns));
1208 eval(shift(@insns));
1209 &vpxor (@X[0],@X[0],@X[-4&7]); # "X[0]"^="X[-16]"
1210 eval(shift(@insns));
1211 eval(shift(@insns));
1213 &vpxor (@Tx[0],@Tx[0],@X[-2&7]); # "X[-3]"^"X[-8]"
1214 eval(shift(@insns));
1215 eval(shift(@insns));
1216 eval(shift(@insns));
1217 eval(shift(@insns));
1219 &vpxor (@X[0],@X[0],@Tx[0]); # "X[0]"^="X[-3]"^"X[-8]"
1220 eval(shift(@insns));
1221 eval(shift(@insns));
1222 &vmovdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
1223 eval(shift(@insns));
1224 eval(shift(@insns));
1226 &vpsrld (@Tx[0],@X[0],31);
1227 eval(shift(@insns));
1228 eval(shift(@insns));
1229 eval(shift(@insns));
1230 eval(shift(@insns));
1232 &vpslldq(@Tx[1],@X[0],12); # "X[0]"<<96, extract one dword
1233 &vpaddd (@X[0],@X[0],@X[0]);
1234 eval(shift(@insns));
1235 eval(shift(@insns));
1236 eval(shift(@insns));
1237 eval(shift(@insns));
1239 &vpor (@X[0],@X[0],@Tx[0]); # "X[0]"<<<=1
1240 &vpsrld (@Tx[0],@Tx[1],30);
1241 eval(shift(@insns));
1242 eval(shift(@insns));
1243 eval(shift(@insns));
1244 eval(shift(@insns));
1246 &vpslld (@Tx[1],@Tx[1],2);
1247 &vpxor (@X[0],@X[0],@Tx[0]);
1248 eval(shift(@insns));
1249 eval(shift(@insns));
1250 eval(shift(@insns));
1251 eval(shift(@insns));
1253 &vpxor (@X[0],@X[0],@Tx[1]); # "X[0]"^=("X[0]">>96)<<<2
1254 eval(shift(@insns));
1255 eval(shift(@insns));
1256 &vmovdqa ($Kx,eval(16*(($Xi)/5))."($K_XX_XX)") if ($Xi%5==0); # K_XX_XX
1257 eval(shift(@insns));
1258 eval(shift(@insns));
1261 foreach (@insns) { eval; } # remaining instructions [if any]
1263 $Xi++; push(@X,shift(@X)); # "rotate" X[]
1266 sub Xupdate_avx_32_79()
1269 my @insns = (&$body,&$body,&$body,&$body); # 32 to 48 instructions
1270 my ($a,$b,$c,$d,$e);
1272 &vpalignr(@Tx[0],@X[-1&7],@X[-2&7],8); # compose "X[-6]"
1273 &vpxor (@X[0],@X[0],@X[-4&7]); # "X[0]"="X[-32]"^"X[-16]"
1274 eval(shift(@insns)); # body_20_39
1275 eval(shift(@insns));
1276 eval(shift(@insns));
1277 eval(shift(@insns)); # rol
1279 &vpxor (@X[0],@X[0],@X[-7&7]); # "X[0]"^="X[-28]"
1280 eval(shift(@insns));
1281 eval(shift(@insns)) if (@insns[0] !~ /&ro[rl]/);
1282 &vpaddd (@Tx[1],$Kx,@X[-1&7]);
1283 &vmovdqa ($Kx,eval(16*($Xi/5))."($K_XX_XX)") if ($Xi%5==0);
1284 eval(shift(@insns)); # ror
1285 eval(shift(@insns));
1287 &vpxor (@X[0],@X[0],@Tx[0]); # "X[0]"^="X[-6]"
1288 eval(shift(@insns)); # body_20_39
1289 eval(shift(@insns));
1290 eval(shift(@insns));
1291 eval(shift(@insns)); # rol
1293 &vpsrld (@Tx[0],@X[0],30);
1294 &vmovdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
1295 eval(shift(@insns));
1296 eval(shift(@insns));
1297 eval(shift(@insns)); # ror
1298 eval(shift(@insns));
1300 &vpslld (@X[0],@X[0],2);
1301 eval(shift(@insns)); # body_20_39
1302 eval(shift(@insns));
1303 eval(shift(@insns));
1304 eval(shift(@insns)); # rol
1305 eval(shift(@insns));
1306 eval(shift(@insns));
1307 eval(shift(@insns)); # ror
1308 eval(shift(@insns));
1310 &vpor (@X[0],@X[0],@Tx[0]); # "X[0]"<<<=2
1311 eval(shift(@insns)); # body_20_39
1312 eval(shift(@insns));
1313 eval(shift(@insns));
1314 eval(shift(@insns)); # rol
1315 eval(shift(@insns));
1316 eval(shift(@insns));
1317 eval(shift(@insns)); # rol
1318 eval(shift(@insns));
1320 foreach (@insns) { eval; } # remaining instructions
1322 $Xi++; push(@X,shift(@X)); # "rotate" X[]
1325 sub Xuplast_avx_80()
1328 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
1329 my ($a,$b,$c,$d,$e);
1331 eval(shift(@insns));
1332 &vpaddd (@Tx[1],$Kx,@X[-1&7]);
1333 eval(shift(@insns));
1334 eval(shift(@insns));
1335 eval(shift(@insns));
1336 eval(shift(@insns));
1338 &vmovdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer IALU
1340 foreach (@insns) { eval; } # remaining instructions
1345 &vmovdqa(@Tx[1],"64($K_XX_XX)"); # pbswap mask
1346 &vmovdqa($Kx,"0($K_XX_XX)"); # K_00_19
1347 &vmovdqu(@X[-4&7],"0($inp)"); # load input
1348 &vmovdqu(@X[-3&7],"16($inp)");
1349 &vmovdqu(@X[-2&7],"32($inp)");
1350 &vmovdqu(@X[-1&7],"48($inp)");
1351 &vpshufb(@X[-4&7],@X[-4&7],@Tx[1]); # byte swap
1360 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
1361 my ($a,$b,$c,$d,$e);
1363 eval(shift(@insns));
1364 eval(shift(@insns));
1365 &vpshufb(@X[($Xi-3)&7],@X[($Xi-3)&7],@Tx[1]);
1366 eval(shift(@insns));
1367 eval(shift(@insns));
1368 &vpaddd (@Tx[0],@X[($Xi-4)&7],$Kx);
1369 eval(shift(@insns));
1370 eval(shift(@insns));
1371 eval(shift(@insns));
1372 eval(shift(@insns));
1373 &vmovdqa(eval(16*$Xi)."(%rsp)",@Tx[0]); # X[]+K xfer to IALU
1374 eval(shift(@insns));
1375 eval(shift(@insns));
1377 foreach (@insns) { eval; }
1384 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
1385 my ($a,$b,$c,$d,$e);
1387 foreach (@insns) { eval; }
1394 &Xupdate_avx_16_31(\&body_00_19);
1395 &Xupdate_avx_16_31(\&body_00_19);
1396 &Xupdate_avx_16_31(\&body_00_19);
1397 &Xupdate_avx_16_31(\&body_00_19);
1398 &Xupdate_avx_32_79(\&body_00_19);
1399 &Xupdate_avx_32_79(\&body_20_39);
1400 &Xupdate_avx_32_79(\&body_20_39);
1401 &Xupdate_avx_32_79(\&body_20_39);
1402 &Xupdate_avx_32_79(\&body_20_39);
1403 &Xupdate_avx_32_79(\&body_20_39);
1404 &Xupdate_avx_32_79(\&body_40_59);
1405 &Xupdate_avx_32_79(\&body_40_59);
1406 &Xupdate_avx_32_79(\&body_40_59);
1407 &Xupdate_avx_32_79(\&body_40_59);
1408 &Xupdate_avx_32_79(\&body_40_59);
1409 &Xupdate_avx_32_79(\&body_20_39);
1410 &Xuplast_avx_80(\&body_20_39,".Ldone_avx"); # can jump to "done"
1412 $saved_j=$j; @saved_V=@V;
1413 $saved_r=$r; @saved_rndkey=@rndkey;
1415 &Xloop_avx(\&body_20_39);
1416 &Xloop_avx(\&body_20_39);
1417 &Xloop_avx(\&body_20_39);
1420 vmovups $iv,48($out,$in0) # write output
1423 add 0($ctx),$A # update context
1430 mov @T[0],$B # magic seed
1441 $jj=$j=$saved_j; @V=@saved_V;
1442 $r=$saved_r; @rndkey=@saved_rndkey;
1444 &Xtail_avx(\&body_20_39);
1445 &Xtail_avx(\&body_20_39);
1446 &Xtail_avx(\&body_20_39);
1449 vmovups $iv,48($out,$in0) # write output
1450 mov 88(%rsp),$ivp # restore $ivp
1452 add 0($ctx),$A # update context
1462 vmovups $iv,($ivp) # write IV
1465 $code.=<<___ if ($win64);
1466 movaps 96+0(%rsp),%xmm6
1467 movaps 96+16(%rsp),%xmm7
1468 movaps 96+32(%rsp),%xmm8
1469 movaps 96+48(%rsp),%xmm9
1470 movaps 96+64(%rsp),%xmm10
1471 movaps 96+80(%rsp),%xmm11
1472 movaps 96+96(%rsp),%xmm12
1473 movaps 96+112(%rsp),%xmm13
1474 movaps 96+128(%rsp),%xmm14
1475 movaps 96+144(%rsp),%xmm15
1478 lea `104+($win64?10*16:0)`(%rsp),%rsi
1479 .cfi_def_cfa %rsi,56
1497 .size aesni_cbc_sha1_enc_avx,.-aesni_cbc_sha1_enc_avx
1500 if ($stitched_decrypt) {{{
1502 ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
1508 '&vpxor ($inout0,$rndkey0,"0x00($in0)");',
1509 '&vpxor ($inout1,$rndkey0,"0x10($in0)");',
1510 '&vpxor ($inout2,$rndkey0,"0x20($in0)");',
1511 '&vpxor ($inout3,$rndkey0,"0x30($in0)");',
1513 '&vmovups($rndkey0,"16-112($key)");',
1514 '&vmovups("64(%rsp)",@X[2]);', # save IV, originally @X[3]
1517 for ($i=0;$i<13;$i++) {
1519 '&vaesdec ($inout0,$inout0,$rndkey0);',
1520 '&vaesdec ($inout1,$inout1,$rndkey0);',
1521 '&vaesdec ($inout2,$inout2,$rndkey0);',
1522 '&vaesdec ($inout3,$inout3,$rndkey0); &vmovups($rndkey0,"'.(16*($i+2)-112).'($key)");'
1524 push (@aes256_dec,(undef,undef)) if (($i>=3 && $i<=5) || $i>=11);
1525 push (@aes256_dec,(undef,undef)) if ($i==5);
1528 '&vaesdeclast ($inout0,$inout0,$rndkey0); &vmovups(@X[0],"0x00($in0)");',
1529 '&vaesdeclast ($inout1,$inout1,$rndkey0); &vmovups(@X[1],"0x10($in0)");',
1530 '&vaesdeclast ($inout2,$inout2,$rndkey0); &vmovups(@X[2],"0x20($in0)");',
1531 '&vaesdeclast ($inout3,$inout3,$rndkey0); &vmovups(@X[3],"0x30($in0)");',
1533 '&vxorps ($inout0,$inout0,"64(%rsp)"); &vmovdqu($rndkey0,"-112($key)");',
1534 '&vxorps ($inout1,$inout1,@X[0]); &vmovups("0x00($out,$in0)",$inout0);',
1535 '&vxorps ($inout2,$inout2,@X[1]); &vmovups("0x10($out,$in0)",$inout1);',
1536 '&vxorps ($inout3,$inout3,@X[2]); &vmovups("0x20($out,$in0)",$inout2);',
1538 '&vmovups ("0x30($out,$in0)",$inout3);'
1542 .type aesni256_cbc_sha1_dec_avx,\@function,6
1544 aesni256_cbc_sha1_dec_avx:
1546 mov `($win64?56:8)`(%rsp),$inp # load 7th argument
1559 lea `-104-($win64?10*16:0)`(%rsp),%rsp
1560 .cfi_adjust_cfa_offset `104+($win64?10*16:0)`
1562 $code.=<<___ if ($win64);
1563 movaps %xmm6,96+0(%rsp)
1564 movaps %xmm7,96+16(%rsp)
1565 movaps %xmm8,96+32(%rsp)
1566 movaps %xmm9,96+48(%rsp)
1567 movaps %xmm10,96+64(%rsp)
1568 movaps %xmm11,96+80(%rsp)
1569 movaps %xmm12,96+96(%rsp)
1570 movaps %xmm13,96+112(%rsp)
1571 movaps %xmm14,96+128(%rsp)
1572 movaps %xmm15,96+144(%rsp)
1577 mov $in0,%r12 # reassign arguments
1580 lea 112($key),%r15 # size optimization
1581 vmovdqu ($ivp),@X[3] # load IV
1583 ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
1587 add $inp,$len # end of input
1589 lea K_XX_XX(%rip),$K_XX_XX
1590 mov 0($ctx),$A # load context
1594 mov $B,@T[0] # magic seed
1600 vmovdqa 64($K_XX_XX),@X[2] # pbswap mask
1601 vmovdqa 0($K_XX_XX),$Kx # K_00_19
1602 vmovdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
1603 vmovdqu 16($inp),@X[-3&7]
1604 vmovdqu 32($inp),@X[-2&7]
1605 vmovdqu 48($inp),@X[-1&7]
1606 vpshufb @X[2],@X[-4&7],@X[-4&7] # byte swap
1608 vpshufb @X[2],@X[-3&7],@X[-3&7]
1609 vpshufb @X[2],@X[-2&7],@X[-2&7]
1610 vpshufb @X[2],@X[-1&7],@X[-1&7]
1611 vpaddd $Kx,@X[-4&7],@X[0] # add K_00_19
1612 vpaddd $Kx,@X[-3&7],@X[1]
1613 vpaddd $Kx,@X[-2&7],@X[2]
1614 vmovdqa @X[0],0(%rsp) # X[]+K xfer to IALU
1615 vmovdqa @X[1],16(%rsp)
1616 vmovdqa @X[2],32(%rsp)
1617 vmovups -112($key),$rndkey0 # $key[0]
1623 &Xupdate_avx_16_31(\&body_00_19_dec);
1624 &Xupdate_avx_16_31(\&body_00_19_dec);
1625 &Xupdate_avx_16_31(\&body_00_19_dec);
1626 &Xupdate_avx_16_31(\&body_00_19_dec);
1627 &Xupdate_avx_32_79(\&body_00_19_dec);
1628 &Xupdate_avx_32_79(\&body_20_39_dec);
1629 &Xupdate_avx_32_79(\&body_20_39_dec);
1630 &Xupdate_avx_32_79(\&body_20_39_dec);
1631 &Xupdate_avx_32_79(\&body_20_39_dec);
1632 &Xupdate_avx_32_79(\&body_20_39_dec);
1633 &Xupdate_avx_32_79(\&body_40_59_dec);
1634 &Xupdate_avx_32_79(\&body_40_59_dec);
1635 &Xupdate_avx_32_79(\&body_40_59_dec);
1636 &Xupdate_avx_32_79(\&body_40_59_dec);
1637 &Xupdate_avx_32_79(\&body_40_59_dec);
1638 &Xupdate_avx_32_79(\&body_20_39_dec);
1639 &Xuplast_avx_80(\&body_20_39_dec,".Ldone_dec_avx"); # can jump to "done"
1641 $saved_j=$j; @saved_V=@V;
1644 &Xloop_avx(\&body_20_39_dec);
1645 &Xloop_avx(\&body_20_39_dec);
1646 &Xloop_avx(\&body_20_39_dec);
1648 eval(@aes256_dec[-1]); # last store
1652 add 0($ctx),$A # update context
1659 mov @T[0],$B # magic seed
1670 $jj=$j=$saved_j; @V=@saved_V;
1673 &Xtail_avx(\&body_20_39_dec);
1674 &Xtail_avx(\&body_20_39_dec);
1675 &Xtail_avx(\&body_20_39_dec);
1677 eval(@aes256_dec[-1]); # last store
1680 add 0($ctx),$A # update context
1690 vmovups @X[3],($ivp) # write IV
1693 $code.=<<___ if ($win64);
1694 movaps 96+0(%rsp),%xmm6
1695 movaps 96+16(%rsp),%xmm7
1696 movaps 96+32(%rsp),%xmm8
1697 movaps 96+48(%rsp),%xmm9
1698 movaps 96+64(%rsp),%xmm10
1699 movaps 96+80(%rsp),%xmm11
1700 movaps 96+96(%rsp),%xmm12
1701 movaps 96+112(%rsp),%xmm13
1702 movaps 96+128(%rsp),%xmm14
1703 movaps 96+144(%rsp),%xmm15
1706 lea `104+($win64?10*16:0)`(%rsp),%rsi
1707 .cfi_def_cfa %rsi,56
1725 .size aesni256_cbc_sha1_dec_avx,.-aesni256_cbc_sha1_dec_avx
1732 .long 0x5a827999,0x5a827999,0x5a827999,0x5a827999 # K_00_19
1733 .long 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 # K_20_39
1734 .long 0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc # K_40_59
1735 .long 0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6 # K_60_79
1736 .long 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f # pbswap mask
1737 .byte 0xf,0xe,0xd,0xc,0xb,0xa,0x9,0x8,0x7,0x6,0x5,0x4,0x3,0x2,0x1,0x0
1739 .asciz "AESNI-CBC+SHA1 stitch for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
1743 ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
1747 ($iv,$in,$rndkey0)=map("%xmm$_",(2,14,15));
1748 @rndkey=("%xmm0","%xmm1");
1751 my ($BSWAP,$ABCD,$E,$E_,$ABCD_SAVE,$E_SAVE)=map("%xmm$_",(7..12));
1752 my @MSG=map("%xmm$_",(3..6));
1755 .type aesni_cbc_sha1_enc_shaext,\@function,6
1757 aesni_cbc_sha1_enc_shaext:
1758 mov `($win64?56:8)`(%rsp),$inp # load 7th argument
1760 $code.=<<___ if ($win64);
1761 lea `-8-10*16`(%rsp),%rsp
1762 movaps %xmm6,-8-10*16(%rax)
1763 movaps %xmm7,-8-9*16(%rax)
1764 movaps %xmm8,-8-8*16(%rax)
1765 movaps %xmm9,-8-7*16(%rax)
1766 movaps %xmm10,-8-6*16(%rax)
1767 movaps %xmm11,-8-5*16(%rax)
1768 movaps %xmm12,-8-4*16(%rax)
1769 movaps %xmm13,-8-3*16(%rax)
1770 movaps %xmm14,-8-2*16(%rax)
1771 movaps %xmm15,-8-1*16(%rax)
1777 movdqa K_XX_XX+0x50(%rip),$BSWAP # byte-n-word swap
1779 mov 240($key),$rounds
1781 movups ($key),$rndkey0 # $key[0]
1782 movups ($ivp),$iv # load IV
1783 movups 16($key),$rndkey[0] # forward reference
1784 lea 112($key),$key # size optimization
1786 pshufd \$0b00011011,$ABCD,$ABCD # flip word order
1787 pshufd \$0b00011011,$E,$E # flip word order
1795 movdqu ($inp),@MSG[0]
1796 movdqa $E,$E_SAVE # offload $E
1797 pshufb $BSWAP,@MSG[0]
1798 movdqu 0x10($inp),@MSG[1]
1799 movdqa $ABCD,$ABCD_SAVE # offload $ABCD
1803 pshufb $BSWAP,@MSG[1]
1806 movdqu 0x20($inp),@MSG[2]
1808 pxor $E_SAVE,@MSG[0] # black magic
1812 pxor $E_SAVE,@MSG[0] # black magic
1814 pshufb $BSWAP,@MSG[2]
1815 sha1rnds4 \$0,$E,$ABCD # 0-3
1816 sha1nexte @MSG[1],$E_
1820 sha1msg1 @MSG[1],@MSG[0]
1821 movdqu -0x10($inp),@MSG[3]
1823 pshufb $BSWAP,@MSG[3]
1827 sha1rnds4 \$0,$E_,$ABCD # 4-7
1828 sha1nexte @MSG[2],$E
1829 pxor @MSG[2],@MSG[0]
1830 sha1msg1 @MSG[2],@MSG[1]
1834 for($i=2;$i<20-4;$i++) {
1837 sha1rnds4 \$`int($i/5)`,$E,$ABCD # 8-11
1838 sha1nexte @MSG[3],$E_
1842 sha1msg2 @MSG[3],@MSG[0]
1843 pxor @MSG[3],@MSG[1]
1844 sha1msg1 @MSG[3],@MSG[2]
1847 push(@MSG,shift(@MSG));
1853 sha1rnds4 \$3,$E,$ABCD # 64-67
1854 sha1nexte @MSG[3],$E_
1855 sha1msg2 @MSG[3],@MSG[0]
1856 pxor @MSG[3],@MSG[1]
1861 sha1rnds4 \$3,$E_,$ABCD # 68-71
1862 sha1nexte @MSG[0],$E
1863 sha1msg2 @MSG[0],@MSG[1]
1867 movdqa $E_SAVE,@MSG[0]
1869 sha1rnds4 \$3,$E,$ABCD # 72-75
1870 sha1nexte @MSG[1],$E_
1875 sha1rnds4 \$3,$E_,$ABCD # 76-79
1876 sha1nexte $MSG[0],$E
1878 while($r<40) { &$aesenc(); } # remaining aesenc's
1882 paddd $ABCD_SAVE,$ABCD
1883 movups $iv,48($out,$in0) # write output
1887 pshufd \$0b00011011,$ABCD,$ABCD
1888 pshufd \$0b00011011,$E,$E
1889 movups $iv,($ivp) # write IV
1893 $code.=<<___ if ($win64);
1894 movaps -8-10*16(%rax),%xmm6
1895 movaps -8-9*16(%rax),%xmm7
1896 movaps -8-8*16(%rax),%xmm8
1897 movaps -8-7*16(%rax),%xmm9
1898 movaps -8-6*16(%rax),%xmm10
1899 movaps -8-5*16(%rax),%xmm11
1900 movaps -8-4*16(%rax),%xmm12
1901 movaps -8-3*16(%rax),%xmm13
1902 movaps -8-2*16(%rax),%xmm14
1903 movaps -8-1*16(%rax),%xmm15
1909 .size aesni_cbc_sha1_enc_shaext,.-aesni_cbc_sha1_enc_shaext
1912 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
1913 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
1921 .extern __imp_RtlVirtualUnwind
1922 .type ssse3_handler,\@abi-omnipotent
1936 mov 120($context),%rax # pull context->Rax
1937 mov 248($context),%rbx # pull context->Rip
1939 mov 8($disp),%rsi # disp->ImageBase
1940 mov 56($disp),%r11 # disp->HandlerData
1942 mov 0(%r11),%r10d # HandlerData[0]
1943 lea (%rsi,%r10),%r10 # prologue label
1944 cmp %r10,%rbx # context->Rip<prologue label
1945 jb .Lcommon_seh_tail
1947 mov 152($context),%rax # pull context->Rsp
1949 mov 4(%r11),%r10d # HandlerData[1]
1950 lea (%rsi,%r10),%r10 # epilogue label
1951 cmp %r10,%rbx # context->Rip>=epilogue label
1952 jae .Lcommon_seh_tail
1954 $code.=<<___ if ($shaext);
1955 lea aesni_cbc_sha1_enc_shaext(%rip),%r10
1960 lea 512($context),%rdi # &context.Xmm6
1962 .long 0xa548f3fc # cld; rep movsq
1963 lea 168(%rax),%rax # adjust stack pointer
1964 jmp .Lcommon_seh_tail
1969 lea 512($context),%rdi # &context.Xmm6
1971 .long 0xa548f3fc # cld; rep movsq
1972 lea `104+10*16`(%rax),%rax # adjust stack pointer
1981 mov %rbx,144($context) # restore context->Rbx
1982 mov %rbp,160($context) # restore context->Rbp
1983 mov %r12,216($context) # restore context->R12
1984 mov %r13,224($context) # restore context->R13
1985 mov %r14,232($context) # restore context->R14
1986 mov %r15,240($context) # restore context->R15
1991 mov %rax,152($context) # restore context->Rsp
1992 mov %rsi,168($context) # restore context->Rsi
1993 mov %rdi,176($context) # restore context->Rdi
1995 mov 40($disp),%rdi # disp->ContextRecord
1996 mov $context,%rsi # context
1997 mov \$154,%ecx # sizeof(CONTEXT)
1998 .long 0xa548f3fc # cld; rep movsq
2001 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
2002 mov 8(%rsi),%rdx # arg2, disp->ImageBase
2003 mov 0(%rsi),%r8 # arg3, disp->ControlPc
2004 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
2005 mov 40(%rsi),%r10 # disp->ContextRecord
2006 lea 56(%rsi),%r11 # &disp->HandlerData
2007 lea 24(%rsi),%r12 # &disp->EstablisherFrame
2008 mov %r10,32(%rsp) # arg5
2009 mov %r11,40(%rsp) # arg6
2010 mov %r12,48(%rsp) # arg7
2011 mov %rcx,56(%rsp) # arg8, (NULL)
2012 call *__imp_RtlVirtualUnwind(%rip)
2014 mov \$1,%eax # ExceptionContinueSearch
2026 .size ssse3_handler,.-ssse3_handler
2030 .rva .LSEH_begin_aesni_cbc_sha1_enc_ssse3
2031 .rva .LSEH_end_aesni_cbc_sha1_enc_ssse3
2032 .rva .LSEH_info_aesni_cbc_sha1_enc_ssse3
2034 $code.=<<___ if ($avx);
2035 .rva .LSEH_begin_aesni_cbc_sha1_enc_avx
2036 .rva .LSEH_end_aesni_cbc_sha1_enc_avx
2037 .rva .LSEH_info_aesni_cbc_sha1_enc_avx
2039 $code.=<<___ if ($shaext);
2040 .rva .LSEH_begin_aesni_cbc_sha1_enc_shaext
2041 .rva .LSEH_end_aesni_cbc_sha1_enc_shaext
2042 .rva .LSEH_info_aesni_cbc_sha1_enc_shaext
2047 .LSEH_info_aesni_cbc_sha1_enc_ssse3:
2050 .rva .Lprologue_ssse3,.Lepilogue_ssse3 # HandlerData[]
2052 $code.=<<___ if ($avx);
2053 .LSEH_info_aesni_cbc_sha1_enc_avx:
2056 .rva .Lprologue_avx,.Lepilogue_avx # HandlerData[]
2058 $code.=<<___ if ($shaext);
2059 .LSEH_info_aesni_cbc_sha1_enc_shaext:
2062 .rva .Lprologue_shaext,.Lepilogue_shaext # HandlerData[]
2066 ####################################################################
2068 local *opcode=shift;
2072 $rex|=0x04 if($dst>=8);
2073 $rex|=0x01 if($src>=8);
2074 unshift @opcode,$rex|0x40 if($rex);
2078 if (@_[0] =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
2079 my @opcode=(0x0f,0x3a,0xcc);
2080 rex(\@opcode,$3,$2);
2081 push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
2083 push @opcode,$c=~/^0/?oct($c):$c;
2084 return ".byte\t".join(',',@opcode);
2086 return "sha1rnds4\t".@_[0];
2093 "sha1nexte" => 0xc8,
2095 "sha1msg2" => 0xca );
2097 if (defined($opcodelet{$instr}) && @_[0] =~ /%xmm([0-9]+),\s*%xmm([0-9]+)/) {
2098 my @opcode=(0x0f,0x38);
2099 rex(\@opcode,$2,$1);
2100 push @opcode,$opcodelet{$instr};
2101 push @opcode,0xc0|($1&7)|(($2&7)<<3); # ModR/M
2102 return ".byte\t".join(',',@opcode);
2104 return $instr."\t".@_[0];
2110 my @opcode=(0x0f,0x38);
2112 if ($line=~/(aes[a-z]+)\s+%xmm([0-9]+),\s*%xmm([0-9]+)/) {
2114 "aesenc" => 0xdc, "aesenclast" => 0xdd,
2115 "aesdec" => 0xde, "aesdeclast" => 0xdf
2117 return undef if (!defined($opcodelet{$1}));
2118 rex(\@opcode,$3,$2);
2119 push @opcode,$opcodelet{$1},0xc0|($2&7)|(($3&7)<<3); # ModR/M
2120 unshift @opcode,0x66;
2121 return ".byte\t".join(',',@opcode);
2126 foreach (split("\n",$code)) {
2127 s/\`([^\`]*)\`/eval $1/geo;
2129 s/\b(sha1rnds4)\s+(.*)/sha1rnds4($2)/geo or
2130 s/\b(sha1[^\s]*)\s+(.*)/sha1op38($1,$2)/geo or
2131 s/\b(aes.*%xmm[0-9]+).*$/aesni($1)/geo;