2 # Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the Apache License 2.0 (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%
37 # Ryzen(**) 2.71[+1.93] 4.64 2.74 +69%
38 # Goldmont(**) 3.82[+1.70] 5.52 4.20 +31%
41 # Westmere 4.51 9.81 6.80 +44%
42 # Sandy Bridge 6.05 11.06(12.15) 6.11(7.19) +81%(+69%)
43 # Ivy Bridge 6.05 10.65 6.07 +75%
44 # Haswell 5.29 8.86(9.44) 5.32(5.32) +67%(+77%)
45 # Bulldozer 6.89 12.84 6.96 +84%
48 # Westmere 5.25 10.55 7.21 +46%
49 # Sandy Bridge 7.05 12.06(13.15) 7.12(7.72) +69%(+70%)
50 # Ivy Bridge 7.05 11.65 7.12 +64%
51 # Haswell 6.19 9.76(10.34) 6.21(6.25) +57%(+65%)
52 # Skylake 3.62 7.16(7.68) 4.56(4.76) +57%(+61%)
53 # Bulldozer 8.00 13.95 8.25 +69%
54 # Ryzen(**) 3.71 5.64 3.72 +52%
55 # Goldmont(**) 5.35 7.05 5.76 +22%
57 # (*) There are two code paths: SSSE3 and AVX. See sha1-568.pl for
58 # background information. Above numbers in parentheses are SSSE3
59 # results collected on AVX-capable CPU, i.e. apply on OSes that
61 # (**) SHAEXT results.
63 # Needless to mention that it makes no sense to implement "stitched"
64 # *decrypt* subroutine. Because *both* AESNI-CBC decrypt and SHA1
65 # fully utilize parallelism, so stitching would not give any gain
66 # anyway. Well, there might be some, e.g. because of better cache
67 # locality... For reference, here are performance results for
68 # standalone AESNI-CBC decrypt:
70 # AES-128-CBC AES-192-CBC AES-256-CBC
71 # Westmere 1.25 1.50 1.75
72 # Sandy Bridge 0.74 0.91 1.09
73 # Ivy Bridge 0.74 0.90 1.11
74 # Haswell 0.63 0.76 0.88
75 # Bulldozer 0.70 0.85 0.99
79 # AES-256-CBC +SHA1 stitch gain
80 # Westmere 1.75 7.20 6.68 +7.8%
81 # Sandy Bridge 1.09 6.09(7.22) 5.82(6.95) +4.6%(+3.9%)
82 # Ivy Bridge 1.11 5.70 5.45 +4.6%
83 # Haswell 0.88 4.45(5.00) 4.39(4.69) +1.4%(*)(+6.6%)
84 # Bulldozer 0.99 6.95 5.95 +17%(**)
86 # (*) Tiny improvement coefficient on Haswell is because we compare
87 # AVX1 stitch to sum with AVX2 SHA1.
88 # (**) Execution is fully dominated by integer code sequence and
89 # SIMD still hardly shows [in single-process benchmark;-]
93 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
95 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
97 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
98 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
99 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
100 die "can't locate x86_64-xlate.pl";
102 $avx=1 if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
103 =~ /GNU assembler version ([2-9]\.[0-9]+)/ &&
105 $avx=1 if (!$avx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
106 `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/ &&
108 $avx=1 if (!$avx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
109 `ml64 2>&1` =~ /Version ([0-9]+)\./ &&
111 $avx=1 if (!$avx && `$ENV{CC} -v 2>&1` =~ /((?:^clang|LLVM) version|.*based on LLVM) ([3-9]\.[0-9]+)/ && $2>=3.0);
113 $shaext=1; ### set to zero if compiling for 1.0.1
117 open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\"";
120 # void aesni_cbc_sha1_enc(const void *inp,
123 # const AES_KEY *key,
130 .extern OPENSSL_ia32cap_P
132 .globl aesni_cbc_sha1_enc
133 .type aesni_cbc_sha1_enc,\@abi-omnipotent
136 # caller should check for SSSE3 and AES-NI bits
137 mov OPENSSL_ia32cap_P+0(%rip),%r10d
138 mov OPENSSL_ia32cap_P+4(%rip),%r11
140 $code.=<<___ if ($shaext);
141 bt \$61,%r11 # check SHA bit
142 jc aesni_cbc_sha1_enc_shaext
144 $code.=<<___ if ($avx);
145 and \$`1<<28`,%r11d # mask AVX bit
146 and \$`1<<30`,%r10d # mask "Intel CPU" bit
148 cmp \$`1<<28|1<<30`,%r10d
149 je aesni_cbc_sha1_enc_avx
152 jmp aesni_cbc_sha1_enc_ssse3
154 .size aesni_cbc_sha1_enc,.-aesni_cbc_sha1_enc
157 my ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
160 my @X=map("%xmm$_",(4..7,0..3));
161 my @Tx=map("%xmm$_",(8..10));
162 my @V=($A,$B,$C,$D,$E)=("%eax","%ebx","%ecx","%edx","%ebp"); # size optimization
163 my @T=("%esi","%edi");
164 my $j=0; my $jj=0; my $r=0; my $sn=0; my $rx=0;
166 my ($rndkey0,$iv,$in)=map("%xmm$_",(11..13)); # for enc
167 my @rndkey=("%xmm14","%xmm15"); # for enc
168 my ($inout0,$inout1,$inout2,$inout3)=map("%xmm$_",(12..15)); # for dec
170 if (1) { # reassign for Atom Silvermont
171 # The goal is to minimize amount of instructions with more than
172 # 3 prefix bytes. Or in more practical terms to keep AES-NI *and*
173 # SSSE3 instructions to upper half of the register bank.
174 @X=map("%xmm$_",(8..11,4..7));
175 @Tx=map("%xmm$_",(12,13,3));
176 ($iv,$in,$rndkey0)=map("%xmm$_",(2,14,15));
177 @rndkey=("%xmm0","%xmm1");
180 sub AUTOLOAD() # thunk [simplified] 32-bit style perlasm
181 { my $opcode = $AUTOLOAD; $opcode =~ s/.*:://;
183 $arg = "\$$arg" if ($arg*1 eq $arg);
184 $code .= "\t$opcode\t".join(',',$arg,reverse @_)."\n";
187 my $_rol=sub { &rol(@_) };
188 my $_ror=sub { &ror(@_) };
191 .type aesni_cbc_sha1_enc_ssse3,\@function,6
193 aesni_cbc_sha1_enc_ssse3:
195 mov `($win64?56:8)`(%rsp),$inp # load 7th argument
196 #shr \$6,$len # debugging artefact
197 #jz .Lepilogue_ssse3 # debugging artefact
210 lea `-104-($win64?10*16:0)`(%rsp),%rsp
211 .cfi_adjust_cfa_offset `104+($win64?10*16:0)`
212 #mov $in0,$inp # debugging artefact
213 #lea 64(%rsp),$ctx # debugging artefact
215 $code.=<<___ if ($win64);
216 movaps %xmm6,96+0(%rsp)
217 movaps %xmm7,96+16(%rsp)
218 movaps %xmm8,96+32(%rsp)
219 movaps %xmm9,96+48(%rsp)
220 movaps %xmm10,96+64(%rsp)
221 movaps %xmm11,96+80(%rsp)
222 movaps %xmm12,96+96(%rsp)
223 movaps %xmm13,96+112(%rsp)
224 movaps %xmm14,96+128(%rsp)
225 movaps %xmm15,96+144(%rsp)
229 mov $in0,%r12 # reassign arguments
232 lea 112($key),%r15 # size optimization
233 movdqu ($ivp),$iv # load IV
234 mov $ivp,88(%rsp) # save $ivp
236 ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
237 my $rounds="${ivp}d";
241 mov 240-112($key),$rounds
242 add $inp,$len # end of input
244 lea K_XX_XX(%rip),$K_XX_XX
245 mov 0($ctx),$A # load context
249 mov $B,@T[0] # magic seed
255 movdqa 64($K_XX_XX),@Tx[2] # pbswap mask
256 movdqa 0($K_XX_XX),@Tx[1] # K_00_19
257 movdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
258 movdqu 16($inp),@X[-3&7]
259 movdqu 32($inp),@X[-2&7]
260 movdqu 48($inp),@X[-1&7]
261 pshufb @Tx[2],@X[-4&7] # byte swap
262 pshufb @Tx[2],@X[-3&7]
263 pshufb @Tx[2],@X[-2&7]
265 paddd @Tx[1],@X[-4&7] # add K_00_19
266 pshufb @Tx[2],@X[-1&7]
267 paddd @Tx[1],@X[-3&7]
268 paddd @Tx[1],@X[-2&7]
269 movdqa @X[-4&7],0(%rsp) # X[]+K xfer to IALU
270 psubd @Tx[1],@X[-4&7] # restore X[]
271 movdqa @X[-3&7],16(%rsp)
272 psubd @Tx[1],@X[-3&7]
273 movdqa @X[-2&7],32(%rsp)
274 psubd @Tx[1],@X[-2&7]
275 movups -112($key),$rndkey0 # $key[0]
276 movups 16-112($key),$rndkey[0] # forward reference
282 my ($n,$k)=($r/10,$r%10);
285 movups `16*$n`($in0),$in # load input
288 $code.=<<___ if ($n);
289 movups $iv,`16*($n-1)`($out,$in0) # write output
293 movups `32+16*$k-112`($key),$rndkey[1]
294 aesenc $rndkey[0],$iv
301 movups `32+16*($k+0)-112`($key),$rndkey[1]
302 aesenc $rndkey[0],$iv
303 movups `32+16*($k+1)-112`($key),$rndkey[0]
304 aesenc $rndkey[1],$iv
306 movups `32+16*($k+2)-112`($key),$rndkey[1]
307 aesenc $rndkey[0],$iv
308 movups `32+16*($k+3)-112`($key),$rndkey[0]
309 aesenc $rndkey[1],$iv
311 aesenclast $rndkey[0],$iv
312 movups 16-112($key),$rndkey[1] # forward reference
316 movups `32+16*$k-112`($key),$rndkey[1]
317 aesenc $rndkey[0],$iv
320 $r++; unshift(@rndkey,pop(@rndkey));
323 sub Xupdate_ssse3_16_31() # recall that $Xi starts with 4
326 my @insns = (&$body,&$body,&$body,&$body); # 40 instructions
329 eval(shift(@insns)); # ror
330 &pshufd (@X[0],@X[-4&7],0xee); # was &movdqa (@X[0],@X[-3&7]);
332 &movdqa (@Tx[0],@X[-1&7]);
333 &paddd (@Tx[1],@X[-1&7]);
337 &punpcklqdq(@X[0],@X[-3&7]); # compose "X[-14]" in "X[0]", was &palignr(@X[0],@X[-4&7],8);
339 eval(shift(@insns)); # rol
341 &psrldq (@Tx[0],4); # "X[-3]", 3 dwords
345 &pxor (@X[0],@X[-4&7]); # "X[0]"^="X[-16]"
347 eval(shift(@insns)); # ror
348 &pxor (@Tx[0],@X[-2&7]); # "X[-3]"^"X[-8]"
353 &pxor (@X[0],@Tx[0]); # "X[0]"^="X[-3]"^"X[-8]"
355 eval(shift(@insns)); # rol
356 &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
360 &movdqa (@Tx[2],@X[0]);
363 eval(shift(@insns)); # ror
364 &movdqa (@Tx[0],@X[0]);
367 &pslldq (@Tx[2],12); # "X[0]"<<96, extract one dword
368 &paddd (@X[0],@X[0]);
374 eval(shift(@insns)); # rol
376 &movdqa (@Tx[1],@Tx[2]);
382 eval(shift(@insns)); # ror
383 &por (@X[0],@Tx[0]); # "X[0]"<<<=1
389 &pxor (@X[0],@Tx[2]);
391 &movdqa (@Tx[2],eval(16*(($Xi)/5))."($K_XX_XX)"); # K_XX_XX
392 eval(shift(@insns)); # rol
396 &pxor (@X[0],@Tx[1]); # "X[0]"^=("X[0]">>96)<<<2
397 &pshufd (@Tx[1],@X[-1&7],0xee) if ($Xi==7); # was &movdqa (@Tx[0],@X[-1&7]) in Xupdate_ssse3_32_79
399 foreach (@insns) { eval; } # remaining instructions [if any]
401 $Xi++; push(@X,shift(@X)); # "rotate" X[]
402 push(@Tx,shift(@Tx));
405 sub Xupdate_ssse3_32_79()
408 my @insns = (&$body,&$body,&$body,&$body); # 32 to 44 instructions
411 eval(shift(@insns)) if ($Xi==8);
412 &pxor (@X[0],@X[-4&7]); # "X[0]"="X[-32]"^"X[-16]"
413 eval(shift(@insns)) if ($Xi==8);
414 eval(shift(@insns)); # body_20_39
416 eval(shift(@insns)) if (@insns[1] =~ /_ror/);
417 eval(shift(@insns)) if (@insns[0] =~ /_ror/);
418 &punpcklqdq(@Tx[0],@X[-1&7]); # compose "X[-6]", was &palignr(@Tx[0],@X[-2&7],8);
420 eval(shift(@insns)); # rol
422 &pxor (@X[0],@X[-7&7]); # "X[0]"^="X[-28]"
426 &movdqa (@Tx[2],@Tx[1]);# "perpetuate" K_XX_XX...
427 } else { # ... or load next one
428 &movdqa (@Tx[2],eval(16*($Xi/5))."($K_XX_XX)");
430 eval(shift(@insns)); # ror
431 &paddd (@Tx[1],@X[-1&7]);
434 &pxor (@X[0],@Tx[0]); # "X[0]"^="X[-6]"
435 eval(shift(@insns)); # body_20_39
438 eval(shift(@insns)); # rol
439 eval(shift(@insns)) if (@insns[0] =~ /_ror/);
441 &movdqa (@Tx[0],@X[0]);
444 &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
445 eval(shift(@insns)); # ror
447 eval(shift(@insns)); # body_20_39
453 eval(shift(@insns)) if (@insns[0] =~ /_rol/);# rol
456 eval(shift(@insns)); # ror
458 &por (@X[0],@Tx[0]); # "X[0]"<<<=2
460 eval(shift(@insns)); # body_20_39
461 eval(shift(@insns)) if (@insns[1] =~ /_rol/);
462 eval(shift(@insns)) if (@insns[0] =~ /_rol/);
463 &pshufd(@Tx[1],@X[-1&7],0xee) if ($Xi<19); # was &movdqa (@Tx[1],@X[0])
465 eval(shift(@insns)); # rol
468 eval(shift(@insns)); # rol
471 foreach (@insns) { eval; } # remaining instructions
473 $Xi++; push(@X,shift(@X)); # "rotate" X[]
474 push(@Tx,shift(@Tx));
477 sub Xuplast_ssse3_80()
480 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
487 &paddd (@Tx[1],@X[-1&7]);
491 &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer IALU
493 foreach (@insns) { eval; } # remaining instructions
498 unshift(@Tx,pop(@Tx));
500 &movdqa (@Tx[2],"64($K_XX_XX)"); # pbswap mask
501 &movdqa (@Tx[1],"0($K_XX_XX)"); # K_00_19
502 &movdqu (@X[-4&7],"0($inp)"); # load input
503 &movdqu (@X[-3&7],"16($inp)");
504 &movdqu (@X[-2&7],"32($inp)");
505 &movdqu (@X[-1&7],"48($inp)");
506 &pshufb (@X[-4&7],@Tx[2]); # byte swap
515 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
521 &pshufb (@X[($Xi-3)&7],@Tx[2]);
526 &paddd (@X[($Xi-4)&7],@Tx[1]);
531 &movdqa (eval(16*$Xi)."(%rsp)",@X[($Xi-4)&7]); # X[]+K xfer to IALU
536 &psubd (@X[($Xi-4)&7],@Tx[1]);
538 foreach (@insns) { eval; }
545 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
548 foreach (@insns) { eval; }
552 '($a,$b,$c,$d,$e)=@V;'.
553 '&$_ror ($b,$j?7:2);', # $b>>>2
555 '&mov (@T[1],$a);', # $b for next round
557 '&add ($e,eval(4*($j&15))."(%rsp)");',# X[]+K xfer
558 '&xor ($b,$c);', # $c^$d for next round
562 '&and (@T[1],$b);', # ($b&($c^$d)) for next round
564 '&xor ($b,$c);', # restore $b
565 '&add ($e,$a);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
568 sub body_00_19 () { # ((c^d)&b)^d
569 # on start @T[0]=(c^d)&b
570 return &body_20_39() if ($rx==19); $rx++;
577 $k = (($jj+1)*12/20)*20*$n/12; # 12 aesencs per these 20 rounds
578 @r[$k%$n].='&$aesenc();' if ($jj==$k/$n);
585 '($a,$b,$c,$d,$e)=@V;'.
586 '&add ($e,eval(4*($j&15))."(%rsp)");',# X[]+K xfer
587 '&xor (@T[0],$d) if($j==19);'.
588 '&xor (@T[0],$c) if($j> 19);', # ($b^$d^$c)
589 '&mov (@T[1],$a);', # $b for next round
593 '&xor (@T[1],$c) if ($j< 79);', # $b^$d for next round
595 '&$_ror ($b,7);', # $b>>>2
596 '&add ($e,$a);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
599 sub body_20_39 () { # b^d^c
601 return &body_40_59() if ($rx==39); $rx++;
608 $k = (($jj+1)*8/20)*20*$n/8; # 8 aesencs per these 20 rounds
609 @r[$k%$n].='&$aesenc();' if ($jj==$k/$n && $rx!=20);
616 '($a,$b,$c,$d,$e)=@V;'.
617 '&add ($e,eval(4*($j&15))."(%rsp)");',# X[]+K xfer
618 '&and (@T[0],$c) if ($j>=40);', # (b^c)&(c^d)
619 '&xor ($c,$d) if ($j>=40);', # restore $c
621 '&$_ror ($b,7);', # $b>>>2
622 '&mov (@T[1],$a);', # $b for next round
627 '&xor (@T[1],$c) if ($j==59);'.
628 '&xor (@T[1],$b) if ($j< 59);', # b^c for next round
630 '&xor ($b,$c) if ($j< 59);', # c^d for next round
631 '&add ($e,$a);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
634 sub body_40_59 () { # ((b^c)&(c^d))^c
635 # on entry @T[0]=(b^c), (c^=d)
643 $k=(($jj+1)*12/20)*20*$n/12; # 12 aesencs per these 20 rounds
644 @r[$k%$n].='&$aesenc();' if ($jj==$k/$n && $rx!=40);
653 &Xupdate_ssse3_16_31(\&body_00_19);
654 &Xupdate_ssse3_16_31(\&body_00_19);
655 &Xupdate_ssse3_16_31(\&body_00_19);
656 &Xupdate_ssse3_16_31(\&body_00_19);
657 &Xupdate_ssse3_32_79(\&body_00_19);
658 &Xupdate_ssse3_32_79(\&body_20_39);
659 &Xupdate_ssse3_32_79(\&body_20_39);
660 &Xupdate_ssse3_32_79(\&body_20_39);
661 &Xupdate_ssse3_32_79(\&body_20_39);
662 &Xupdate_ssse3_32_79(\&body_20_39);
663 &Xupdate_ssse3_32_79(\&body_40_59);
664 &Xupdate_ssse3_32_79(\&body_40_59);
665 &Xupdate_ssse3_32_79(\&body_40_59);
666 &Xupdate_ssse3_32_79(\&body_40_59);
667 &Xupdate_ssse3_32_79(\&body_40_59);
668 &Xupdate_ssse3_32_79(\&body_20_39);
669 &Xuplast_ssse3_80(\&body_20_39,".Ldone_ssse3"); # can jump to "done"
671 $saved_j=$j; @saved_V=@V;
672 $saved_r=$r; @saved_rndkey=@rndkey;
674 &Xloop_ssse3(\&body_20_39);
675 &Xloop_ssse3(\&body_20_39);
676 &Xloop_ssse3(\&body_20_39);
679 movups $iv,48($out,$in0) # write output
682 add 0($ctx),$A # update context
689 mov @T[0],$B # magic seed
700 $jj=$j=$saved_j; @V=@saved_V;
701 $r=$saved_r; @rndkey=@saved_rndkey;
703 &Xtail_ssse3(\&body_20_39);
704 &Xtail_ssse3(\&body_20_39);
705 &Xtail_ssse3(\&body_20_39);
708 movups $iv,48($out,$in0) # write output
709 mov 88(%rsp),$ivp # restore $ivp
711 add 0($ctx),$A # update context
721 movups $iv,($ivp) # write IV
723 $code.=<<___ if ($win64);
724 movaps 96+0(%rsp),%xmm6
725 movaps 96+16(%rsp),%xmm7
726 movaps 96+32(%rsp),%xmm8
727 movaps 96+48(%rsp),%xmm9
728 movaps 96+64(%rsp),%xmm10
729 movaps 96+80(%rsp),%xmm11
730 movaps 96+96(%rsp),%xmm12
731 movaps 96+112(%rsp),%xmm13
732 movaps 96+128(%rsp),%xmm14
733 movaps 96+144(%rsp),%xmm15
736 lea `104+($win64?10*16:0)`(%rsp),%rsi
755 .size aesni_cbc_sha1_enc_ssse3,.-aesni_cbc_sha1_enc_ssse3
758 if ($stitched_decrypt) {{{
760 ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
764 # reassign for Atom Silvermont (see above)
765 ($inout0,$inout1,$inout2,$inout3,$rndkey0)=map("%xmm$_",(0..4));
766 @X=map("%xmm$_",(8..13,6,7));
767 @Tx=map("%xmm$_",(14,15,5));
770 '&movdqu($inout0,"0x00($in0)");',
771 '&movdqu($inout1,"0x10($in0)"); &pxor ($inout0,$rndkey0);',
772 '&movdqu($inout2,"0x20($in0)"); &pxor ($inout1,$rndkey0);',
773 '&movdqu($inout3,"0x30($in0)"); &pxor ($inout2,$rndkey0);',
775 '&pxor ($inout3,$rndkey0); &movups ($rndkey0,"16-112($key)");',
776 '&movaps("64(%rsp)",@X[2]);', # save IV, originally @X[3]
779 for ($i=0;$i<13;$i++) {
781 '&aesdec ($inout0,$rndkey0);',
782 '&aesdec ($inout1,$rndkey0);',
783 '&aesdec ($inout2,$rndkey0);',
784 '&aesdec ($inout3,$rndkey0); &movups($rndkey0,"'.(16*($i+2)-112).'($key)");'
786 push (@aes256_dec,(undef,undef)) if (($i>=3 && $i<=5) || $i>=11);
787 push (@aes256_dec,(undef,undef)) if ($i==5);
790 '&aesdeclast ($inout0,$rndkey0); &movups (@X[0],"0x00($in0)");',
791 '&aesdeclast ($inout1,$rndkey0); &movups (@X[1],"0x10($in0)");',
792 '&aesdeclast ($inout2,$rndkey0); &movups (@X[2],"0x20($in0)");',
793 '&aesdeclast ($inout3,$rndkey0); &movups (@X[3],"0x30($in0)");',
795 '&xorps ($inout0,"64(%rsp)"); &movdqu ($rndkey0,"-112($key)");',
796 '&xorps ($inout1,@X[0]); &movups ("0x00($out,$in0)",$inout0);',
797 '&xorps ($inout2,@X[1]); &movups ("0x10($out,$in0)",$inout1);',
798 '&xorps ($inout3,@X[2]); &movups ("0x20($out,$in0)",$inout2);',
800 '&movups ("0x30($out,$in0)",$inout3);'
803 sub body_00_19_dec () { # ((c^d)&b)^d
804 # on start @T[0]=(c^d)&b
805 return &body_20_39_dec() if ($rx==19);
809 unshift (@r,@aes256_dec[$rx]) if (@aes256_dec[$rx]);
815 sub body_20_39_dec () { # b^d^c
817 return &body_40_59_dec() if ($rx==39);
821 unshift (@r,@aes256_dec[$rx]) if (@aes256_dec[$rx]);
827 sub body_40_59_dec () { # ((b^c)&(c^d))^c
828 # on entry @T[0]=(b^c), (c^=d)
832 unshift (@r,@aes256_dec[$rx]) if (@aes256_dec[$rx]);
839 .globl aesni256_cbc_sha1_dec
840 .type aesni256_cbc_sha1_dec,\@abi-omnipotent
842 aesni256_cbc_sha1_dec:
843 # caller should check for SSSE3 and AES-NI bits
844 mov OPENSSL_ia32cap_P+0(%rip),%r10d
845 mov OPENSSL_ia32cap_P+4(%rip),%r11d
847 $code.=<<___ if ($avx);
848 and \$`1<<28`,%r11d # mask AVX bit
849 and \$`1<<30`,%r10d # mask "Intel CPU" bit
851 cmp \$`1<<28|1<<30`,%r10d
852 je aesni256_cbc_sha1_dec_avx
855 jmp aesni256_cbc_sha1_dec_ssse3
857 .size aesni256_cbc_sha1_dec,.-aesni256_cbc_sha1_dec
859 .type aesni256_cbc_sha1_dec_ssse3,\@function,6
861 aesni256_cbc_sha1_dec_ssse3:
863 mov `($win64?56:8)`(%rsp),$inp # load 7th argument
876 lea `-104-($win64?10*16:0)`(%rsp),%rsp
877 .cfi_adjust_cfa_offset `104+($win64?10*16:0)`
879 $code.=<<___ if ($win64);
880 movaps %xmm6,96+0(%rsp)
881 movaps %xmm7,96+16(%rsp)
882 movaps %xmm8,96+32(%rsp)
883 movaps %xmm9,96+48(%rsp)
884 movaps %xmm10,96+64(%rsp)
885 movaps %xmm11,96+80(%rsp)
886 movaps %xmm12,96+96(%rsp)
887 movaps %xmm13,96+112(%rsp)
888 movaps %xmm14,96+128(%rsp)
889 movaps %xmm15,96+144(%rsp)
890 .Lprologue_dec_ssse3:
893 mov $in0,%r12 # reassign arguments
896 lea 112($key),%r15 # size optimization
897 movdqu ($ivp),@X[3] # load IV
898 #mov $ivp,88(%rsp) # save $ivp
900 ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
904 add $inp,$len # end of input
906 lea K_XX_XX(%rip),$K_XX_XX
907 mov 0($ctx),$A # load context
911 mov $B,@T[0] # magic seed
917 movdqa 64($K_XX_XX),@Tx[2] # pbswap mask
918 movdqa 0($K_XX_XX),@Tx[1] # K_00_19
919 movdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
920 movdqu 16($inp),@X[-3&7]
921 movdqu 32($inp),@X[-2&7]
922 movdqu 48($inp),@X[-1&7]
923 pshufb @Tx[2],@X[-4&7] # byte swap
925 pshufb @Tx[2],@X[-3&7]
926 pshufb @Tx[2],@X[-2&7]
927 pshufb @Tx[2],@X[-1&7]
928 paddd @Tx[1],@X[-4&7] # add K_00_19
929 paddd @Tx[1],@X[-3&7]
930 paddd @Tx[1],@X[-2&7]
931 movdqa @X[-4&7],0(%rsp) # X[]+K xfer to IALU
932 psubd @Tx[1],@X[-4&7] # restore X[]
933 movdqa @X[-3&7],16(%rsp)
934 psubd @Tx[1],@X[-3&7]
935 movdqa @X[-2&7],32(%rsp)
936 psubd @Tx[1],@X[-2&7]
937 movdqu -112($key),$rndkey0 # $key[0]
943 &Xupdate_ssse3_16_31(\&body_00_19_dec);
944 &Xupdate_ssse3_16_31(\&body_00_19_dec);
945 &Xupdate_ssse3_16_31(\&body_00_19_dec);
946 &Xupdate_ssse3_16_31(\&body_00_19_dec);
947 &Xupdate_ssse3_32_79(\&body_00_19_dec);
948 &Xupdate_ssse3_32_79(\&body_20_39_dec);
949 &Xupdate_ssse3_32_79(\&body_20_39_dec);
950 &Xupdate_ssse3_32_79(\&body_20_39_dec);
951 &Xupdate_ssse3_32_79(\&body_20_39_dec);
952 &Xupdate_ssse3_32_79(\&body_20_39_dec);
953 &Xupdate_ssse3_32_79(\&body_40_59_dec);
954 &Xupdate_ssse3_32_79(\&body_40_59_dec);
955 &Xupdate_ssse3_32_79(\&body_40_59_dec);
956 &Xupdate_ssse3_32_79(\&body_40_59_dec);
957 &Xupdate_ssse3_32_79(\&body_40_59_dec);
958 &Xupdate_ssse3_32_79(\&body_20_39_dec);
959 &Xuplast_ssse3_80(\&body_20_39_dec,".Ldone_dec_ssse3"); # can jump to "done"
961 $saved_j=$j; @saved_V=@V;
964 &Xloop_ssse3(\&body_20_39_dec);
965 &Xloop_ssse3(\&body_20_39_dec);
966 &Xloop_ssse3(\&body_20_39_dec);
968 eval(@aes256_dec[-1]); # last store
972 add 0($ctx),$A # update context
979 mov @T[0],$B # magic seed
990 $jj=$j=$saved_j; @V=@saved_V;
993 &Xtail_ssse3(\&body_20_39_dec);
994 &Xtail_ssse3(\&body_20_39_dec);
995 &Xtail_ssse3(\&body_20_39_dec);
997 eval(@aes256_dec[-1]); # last store
999 add 0($ctx),$A # update context
1009 movups @X[3],($ivp) # write IV
1011 $code.=<<___ if ($win64);
1012 movaps 96+0(%rsp),%xmm6
1013 movaps 96+16(%rsp),%xmm7
1014 movaps 96+32(%rsp),%xmm8
1015 movaps 96+48(%rsp),%xmm9
1016 movaps 96+64(%rsp),%xmm10
1017 movaps 96+80(%rsp),%xmm11
1018 movaps 96+96(%rsp),%xmm12
1019 movaps 96+112(%rsp),%xmm13
1020 movaps 96+128(%rsp),%xmm14
1021 movaps 96+144(%rsp),%xmm15
1024 lea `104+($win64?10*16:0)`(%rsp),%rsi
1025 .cfi_cfa_def %rsi,56
1040 .Lepilogue_dec_ssse3:
1043 .size aesni256_cbc_sha1_dec_ssse3,.-aesni256_cbc_sha1_dec_ssse3
1049 my ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
1052 my @X=map("%xmm$_",(4..7,0..3));
1053 my @Tx=map("%xmm$_",(8..10));
1054 my @V=($A,$B,$C,$D,$E)=("%eax","%ebx","%ecx","%edx","%ebp"); # size optimization
1055 my @T=("%esi","%edi");
1056 my ($rndkey0,$iv,$in)=map("%xmm$_",(11..13));
1057 my @rndkey=("%xmm14","%xmm15");
1058 my ($inout0,$inout1,$inout2,$inout3)=map("%xmm$_",(12..15)); # for dec
1061 my $_rol=sub { &shld(@_[0],@_) };
1062 my $_ror=sub { &shrd(@_[0],@_) };
1065 .type aesni_cbc_sha1_enc_avx,\@function,6
1067 aesni_cbc_sha1_enc_avx:
1069 mov `($win64?56:8)`(%rsp),$inp # load 7th argument
1070 #shr \$6,$len # debugging artefact
1071 #jz .Lepilogue_avx # debugging artefact
1084 lea `-104-($win64?10*16:0)`(%rsp),%rsp
1085 .cfi_adjust_cfa_offset `104+($win64?10*16:0)`
1086 #mov $in0,$inp # debugging artefact
1087 #lea 64(%rsp),$ctx # debugging artefact
1089 $code.=<<___ if ($win64);
1090 movaps %xmm6,96+0(%rsp)
1091 movaps %xmm7,96+16(%rsp)
1092 movaps %xmm8,96+32(%rsp)
1093 movaps %xmm9,96+48(%rsp)
1094 movaps %xmm10,96+64(%rsp)
1095 movaps %xmm11,96+80(%rsp)
1096 movaps %xmm12,96+96(%rsp)
1097 movaps %xmm13,96+112(%rsp)
1098 movaps %xmm14,96+128(%rsp)
1099 movaps %xmm15,96+144(%rsp)
1104 mov $in0,%r12 # reassign arguments
1107 lea 112($key),%r15 # size optimization
1108 vmovdqu ($ivp),$iv # load IV
1109 mov $ivp,88(%rsp) # save $ivp
1111 ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
1112 my $rounds="${ivp}d";
1116 mov 240-112($key),$rounds
1117 add $inp,$len # end of input
1119 lea K_XX_XX(%rip),$K_XX_XX
1120 mov 0($ctx),$A # load context
1124 mov $B,@T[0] # magic seed
1130 vmovdqa 64($K_XX_XX),@X[2] # pbswap mask
1131 vmovdqa 0($K_XX_XX),$Kx # K_00_19
1132 vmovdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
1133 vmovdqu 16($inp),@X[-3&7]
1134 vmovdqu 32($inp),@X[-2&7]
1135 vmovdqu 48($inp),@X[-1&7]
1136 vpshufb @X[2],@X[-4&7],@X[-4&7] # byte swap
1138 vpshufb @X[2],@X[-3&7],@X[-3&7]
1139 vpshufb @X[2],@X[-2&7],@X[-2&7]
1140 vpshufb @X[2],@X[-1&7],@X[-1&7]
1141 vpaddd $Kx,@X[-4&7],@X[0] # add K_00_19
1142 vpaddd $Kx,@X[-3&7],@X[1]
1143 vpaddd $Kx,@X[-2&7],@X[2]
1144 vmovdqa @X[0],0(%rsp) # X[]+K xfer to IALU
1145 vmovdqa @X[1],16(%rsp)
1146 vmovdqa @X[2],32(%rsp)
1147 vmovups -112($key),$rndkey[1] # $key[0]
1148 vmovups 16-112($key),$rndkey[0] # forward reference
1154 my ($n,$k)=($r/10,$r%10);
1157 vmovdqu `16*$n`($in0),$in # load input
1158 vpxor $rndkey[1],$in,$in
1160 $code.=<<___ if ($n);
1161 vmovups $iv,`16*($n-1)`($out,$in0) # write output
1165 vaesenc $rndkey[0],$iv,$iv
1166 vmovups `32+16*$k-112`($key),$rndkey[1]
1173 vaesenc $rndkey[0],$iv,$iv
1174 vmovups `32+16*($k+0)-112`($key),$rndkey[1]
1175 vaesenc $rndkey[1],$iv,$iv
1176 vmovups `32+16*($k+1)-112`($key),$rndkey[0]
1178 vaesenc $rndkey[0],$iv,$iv
1179 vmovups `32+16*($k+2)-112`($key),$rndkey[1]
1180 vaesenc $rndkey[1],$iv,$iv
1181 vmovups `32+16*($k+3)-112`($key),$rndkey[0]
1183 vaesenclast $rndkey[0],$iv,$iv
1184 vmovups -112($key),$rndkey[0]
1185 vmovups 16-112($key),$rndkey[1] # forward reference
1189 vaesenc $rndkey[0],$iv,$iv
1190 vmovups `32+16*$k-112`($key),$rndkey[1]
1193 $r++; unshift(@rndkey,pop(@rndkey));
1196 sub Xupdate_avx_16_31() # recall that $Xi starts with 4
1199 my @insns = (&$body,&$body,&$body,&$body); # 40 instructions
1200 my ($a,$b,$c,$d,$e);
1202 eval(shift(@insns));
1203 eval(shift(@insns));
1204 &vpalignr(@X[0],@X[-3&7],@X[-4&7],8); # compose "X[-14]" in "X[0]"
1205 eval(shift(@insns));
1206 eval(shift(@insns));
1208 &vpaddd (@Tx[1],$Kx,@X[-1&7]);
1209 eval(shift(@insns));
1210 eval(shift(@insns));
1211 &vpsrldq(@Tx[0],@X[-1&7],4); # "X[-3]", 3 dwords
1212 eval(shift(@insns));
1213 eval(shift(@insns));
1214 &vpxor (@X[0],@X[0],@X[-4&7]); # "X[0]"^="X[-16]"
1215 eval(shift(@insns));
1216 eval(shift(@insns));
1218 &vpxor (@Tx[0],@Tx[0],@X[-2&7]); # "X[-3]"^"X[-8]"
1219 eval(shift(@insns));
1220 eval(shift(@insns));
1221 eval(shift(@insns));
1222 eval(shift(@insns));
1224 &vpxor (@X[0],@X[0],@Tx[0]); # "X[0]"^="X[-3]"^"X[-8]"
1225 eval(shift(@insns));
1226 eval(shift(@insns));
1227 &vmovdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
1228 eval(shift(@insns));
1229 eval(shift(@insns));
1231 &vpsrld (@Tx[0],@X[0],31);
1232 eval(shift(@insns));
1233 eval(shift(@insns));
1234 eval(shift(@insns));
1235 eval(shift(@insns));
1237 &vpslldq(@Tx[1],@X[0],12); # "X[0]"<<96, extract one dword
1238 &vpaddd (@X[0],@X[0],@X[0]);
1239 eval(shift(@insns));
1240 eval(shift(@insns));
1241 eval(shift(@insns));
1242 eval(shift(@insns));
1244 &vpor (@X[0],@X[0],@Tx[0]); # "X[0]"<<<=1
1245 &vpsrld (@Tx[0],@Tx[1],30);
1246 eval(shift(@insns));
1247 eval(shift(@insns));
1248 eval(shift(@insns));
1249 eval(shift(@insns));
1251 &vpslld (@Tx[1],@Tx[1],2);
1252 &vpxor (@X[0],@X[0],@Tx[0]);
1253 eval(shift(@insns));
1254 eval(shift(@insns));
1255 eval(shift(@insns));
1256 eval(shift(@insns));
1258 &vpxor (@X[0],@X[0],@Tx[1]); # "X[0]"^=("X[0]">>96)<<<2
1259 eval(shift(@insns));
1260 eval(shift(@insns));
1261 &vmovdqa ($Kx,eval(16*(($Xi)/5))."($K_XX_XX)") if ($Xi%5==0); # K_XX_XX
1262 eval(shift(@insns));
1263 eval(shift(@insns));
1266 foreach (@insns) { eval; } # remaining instructions [if any]
1268 $Xi++; push(@X,shift(@X)); # "rotate" X[]
1271 sub Xupdate_avx_32_79()
1274 my @insns = (&$body,&$body,&$body,&$body); # 32 to 48 instructions
1275 my ($a,$b,$c,$d,$e);
1277 &vpalignr(@Tx[0],@X[-1&7],@X[-2&7],8); # compose "X[-6]"
1278 &vpxor (@X[0],@X[0],@X[-4&7]); # "X[0]"="X[-32]"^"X[-16]"
1279 eval(shift(@insns)); # body_20_39
1280 eval(shift(@insns));
1281 eval(shift(@insns));
1282 eval(shift(@insns)); # rol
1284 &vpxor (@X[0],@X[0],@X[-7&7]); # "X[0]"^="X[-28]"
1285 eval(shift(@insns));
1286 eval(shift(@insns)) if (@insns[0] !~ /&ro[rl]/);
1287 &vpaddd (@Tx[1],$Kx,@X[-1&7]);
1288 &vmovdqa ($Kx,eval(16*($Xi/5))."($K_XX_XX)") if ($Xi%5==0);
1289 eval(shift(@insns)); # ror
1290 eval(shift(@insns));
1292 &vpxor (@X[0],@X[0],@Tx[0]); # "X[0]"^="X[-6]"
1293 eval(shift(@insns)); # body_20_39
1294 eval(shift(@insns));
1295 eval(shift(@insns));
1296 eval(shift(@insns)); # rol
1298 &vpsrld (@Tx[0],@X[0],30);
1299 &vmovdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
1300 eval(shift(@insns));
1301 eval(shift(@insns));
1302 eval(shift(@insns)); # ror
1303 eval(shift(@insns));
1305 &vpslld (@X[0],@X[0],2);
1306 eval(shift(@insns)); # body_20_39
1307 eval(shift(@insns));
1308 eval(shift(@insns));
1309 eval(shift(@insns)); # rol
1310 eval(shift(@insns));
1311 eval(shift(@insns));
1312 eval(shift(@insns)); # ror
1313 eval(shift(@insns));
1315 &vpor (@X[0],@X[0],@Tx[0]); # "X[0]"<<<=2
1316 eval(shift(@insns)); # body_20_39
1317 eval(shift(@insns));
1318 eval(shift(@insns));
1319 eval(shift(@insns)); # rol
1320 eval(shift(@insns));
1321 eval(shift(@insns));
1322 eval(shift(@insns)); # rol
1323 eval(shift(@insns));
1325 foreach (@insns) { eval; } # remaining instructions
1327 $Xi++; push(@X,shift(@X)); # "rotate" X[]
1330 sub Xuplast_avx_80()
1333 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
1334 my ($a,$b,$c,$d,$e);
1336 eval(shift(@insns));
1337 &vpaddd (@Tx[1],$Kx,@X[-1&7]);
1338 eval(shift(@insns));
1339 eval(shift(@insns));
1340 eval(shift(@insns));
1341 eval(shift(@insns));
1343 &vmovdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer IALU
1345 foreach (@insns) { eval; } # remaining instructions
1350 &vmovdqa(@Tx[1],"64($K_XX_XX)"); # pbswap mask
1351 &vmovdqa($Kx,"0($K_XX_XX)"); # K_00_19
1352 &vmovdqu(@X[-4&7],"0($inp)"); # load input
1353 &vmovdqu(@X[-3&7],"16($inp)");
1354 &vmovdqu(@X[-2&7],"32($inp)");
1355 &vmovdqu(@X[-1&7],"48($inp)");
1356 &vpshufb(@X[-4&7],@X[-4&7],@Tx[1]); # byte swap
1365 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
1366 my ($a,$b,$c,$d,$e);
1368 eval(shift(@insns));
1369 eval(shift(@insns));
1370 &vpshufb(@X[($Xi-3)&7],@X[($Xi-3)&7],@Tx[1]);
1371 eval(shift(@insns));
1372 eval(shift(@insns));
1373 &vpaddd (@Tx[0],@X[($Xi-4)&7],$Kx);
1374 eval(shift(@insns));
1375 eval(shift(@insns));
1376 eval(shift(@insns));
1377 eval(shift(@insns));
1378 &vmovdqa(eval(16*$Xi)."(%rsp)",@Tx[0]); # X[]+K xfer to IALU
1379 eval(shift(@insns));
1380 eval(shift(@insns));
1382 foreach (@insns) { eval; }
1389 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
1390 my ($a,$b,$c,$d,$e);
1392 foreach (@insns) { eval; }
1399 &Xupdate_avx_16_31(\&body_00_19);
1400 &Xupdate_avx_16_31(\&body_00_19);
1401 &Xupdate_avx_16_31(\&body_00_19);
1402 &Xupdate_avx_16_31(\&body_00_19);
1403 &Xupdate_avx_32_79(\&body_00_19);
1404 &Xupdate_avx_32_79(\&body_20_39);
1405 &Xupdate_avx_32_79(\&body_20_39);
1406 &Xupdate_avx_32_79(\&body_20_39);
1407 &Xupdate_avx_32_79(\&body_20_39);
1408 &Xupdate_avx_32_79(\&body_20_39);
1409 &Xupdate_avx_32_79(\&body_40_59);
1410 &Xupdate_avx_32_79(\&body_40_59);
1411 &Xupdate_avx_32_79(\&body_40_59);
1412 &Xupdate_avx_32_79(\&body_40_59);
1413 &Xupdate_avx_32_79(\&body_40_59);
1414 &Xupdate_avx_32_79(\&body_20_39);
1415 &Xuplast_avx_80(\&body_20_39,".Ldone_avx"); # can jump to "done"
1417 $saved_j=$j; @saved_V=@V;
1418 $saved_r=$r; @saved_rndkey=@rndkey;
1420 &Xloop_avx(\&body_20_39);
1421 &Xloop_avx(\&body_20_39);
1422 &Xloop_avx(\&body_20_39);
1425 vmovups $iv,48($out,$in0) # write output
1428 add 0($ctx),$A # update context
1435 mov @T[0],$B # magic seed
1446 $jj=$j=$saved_j; @V=@saved_V;
1447 $r=$saved_r; @rndkey=@saved_rndkey;
1449 &Xtail_avx(\&body_20_39);
1450 &Xtail_avx(\&body_20_39);
1451 &Xtail_avx(\&body_20_39);
1454 vmovups $iv,48($out,$in0) # write output
1455 mov 88(%rsp),$ivp # restore $ivp
1457 add 0($ctx),$A # update context
1467 vmovups $iv,($ivp) # write IV
1470 $code.=<<___ if ($win64);
1471 movaps 96+0(%rsp),%xmm6
1472 movaps 96+16(%rsp),%xmm7
1473 movaps 96+32(%rsp),%xmm8
1474 movaps 96+48(%rsp),%xmm9
1475 movaps 96+64(%rsp),%xmm10
1476 movaps 96+80(%rsp),%xmm11
1477 movaps 96+96(%rsp),%xmm12
1478 movaps 96+112(%rsp),%xmm13
1479 movaps 96+128(%rsp),%xmm14
1480 movaps 96+144(%rsp),%xmm15
1483 lea `104+($win64?10*16:0)`(%rsp),%rsi
1484 .cfi_def_cfa %rsi,56
1502 .size aesni_cbc_sha1_enc_avx,.-aesni_cbc_sha1_enc_avx
1505 if ($stitched_decrypt) {{{
1507 ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
1513 '&vpxor ($inout0,$rndkey0,"0x00($in0)");',
1514 '&vpxor ($inout1,$rndkey0,"0x10($in0)");',
1515 '&vpxor ($inout2,$rndkey0,"0x20($in0)");',
1516 '&vpxor ($inout3,$rndkey0,"0x30($in0)");',
1518 '&vmovups($rndkey0,"16-112($key)");',
1519 '&vmovups("64(%rsp)",@X[2]);', # save IV, originally @X[3]
1522 for ($i=0;$i<13;$i++) {
1524 '&vaesdec ($inout0,$inout0,$rndkey0);',
1525 '&vaesdec ($inout1,$inout1,$rndkey0);',
1526 '&vaesdec ($inout2,$inout2,$rndkey0);',
1527 '&vaesdec ($inout3,$inout3,$rndkey0); &vmovups($rndkey0,"'.(16*($i+2)-112).'($key)");'
1529 push (@aes256_dec,(undef,undef)) if (($i>=3 && $i<=5) || $i>=11);
1530 push (@aes256_dec,(undef,undef)) if ($i==5);
1533 '&vaesdeclast ($inout0,$inout0,$rndkey0); &vmovups(@X[0],"0x00($in0)");',
1534 '&vaesdeclast ($inout1,$inout1,$rndkey0); &vmovups(@X[1],"0x10($in0)");',
1535 '&vaesdeclast ($inout2,$inout2,$rndkey0); &vmovups(@X[2],"0x20($in0)");',
1536 '&vaesdeclast ($inout3,$inout3,$rndkey0); &vmovups(@X[3],"0x30($in0)");',
1538 '&vxorps ($inout0,$inout0,"64(%rsp)"); &vmovdqu($rndkey0,"-112($key)");',
1539 '&vxorps ($inout1,$inout1,@X[0]); &vmovups("0x00($out,$in0)",$inout0);',
1540 '&vxorps ($inout2,$inout2,@X[1]); &vmovups("0x10($out,$in0)",$inout1);',
1541 '&vxorps ($inout3,$inout3,@X[2]); &vmovups("0x20($out,$in0)",$inout2);',
1543 '&vmovups ("0x30($out,$in0)",$inout3);'
1547 .type aesni256_cbc_sha1_dec_avx,\@function,6
1549 aesni256_cbc_sha1_dec_avx:
1551 mov `($win64?56:8)`(%rsp),$inp # load 7th argument
1564 lea `-104-($win64?10*16:0)`(%rsp),%rsp
1565 .cfi_adjust_cfa_offset `104+($win64?10*16:0)`
1567 $code.=<<___ if ($win64);
1568 movaps %xmm6,96+0(%rsp)
1569 movaps %xmm7,96+16(%rsp)
1570 movaps %xmm8,96+32(%rsp)
1571 movaps %xmm9,96+48(%rsp)
1572 movaps %xmm10,96+64(%rsp)
1573 movaps %xmm11,96+80(%rsp)
1574 movaps %xmm12,96+96(%rsp)
1575 movaps %xmm13,96+112(%rsp)
1576 movaps %xmm14,96+128(%rsp)
1577 movaps %xmm15,96+144(%rsp)
1582 mov $in0,%r12 # reassign arguments
1585 lea 112($key),%r15 # size optimization
1586 vmovdqu ($ivp),@X[3] # load IV
1588 ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
1592 add $inp,$len # end of input
1594 lea K_XX_XX(%rip),$K_XX_XX
1595 mov 0($ctx),$A # load context
1599 mov $B,@T[0] # magic seed
1605 vmovdqa 64($K_XX_XX),@X[2] # pbswap mask
1606 vmovdqa 0($K_XX_XX),$Kx # K_00_19
1607 vmovdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
1608 vmovdqu 16($inp),@X[-3&7]
1609 vmovdqu 32($inp),@X[-2&7]
1610 vmovdqu 48($inp),@X[-1&7]
1611 vpshufb @X[2],@X[-4&7],@X[-4&7] # byte swap
1613 vpshufb @X[2],@X[-3&7],@X[-3&7]
1614 vpshufb @X[2],@X[-2&7],@X[-2&7]
1615 vpshufb @X[2],@X[-1&7],@X[-1&7]
1616 vpaddd $Kx,@X[-4&7],@X[0] # add K_00_19
1617 vpaddd $Kx,@X[-3&7],@X[1]
1618 vpaddd $Kx,@X[-2&7],@X[2]
1619 vmovdqa @X[0],0(%rsp) # X[]+K xfer to IALU
1620 vmovdqa @X[1],16(%rsp)
1621 vmovdqa @X[2],32(%rsp)
1622 vmovups -112($key),$rndkey0 # $key[0]
1628 &Xupdate_avx_16_31(\&body_00_19_dec);
1629 &Xupdate_avx_16_31(\&body_00_19_dec);
1630 &Xupdate_avx_16_31(\&body_00_19_dec);
1631 &Xupdate_avx_16_31(\&body_00_19_dec);
1632 &Xupdate_avx_32_79(\&body_00_19_dec);
1633 &Xupdate_avx_32_79(\&body_20_39_dec);
1634 &Xupdate_avx_32_79(\&body_20_39_dec);
1635 &Xupdate_avx_32_79(\&body_20_39_dec);
1636 &Xupdate_avx_32_79(\&body_20_39_dec);
1637 &Xupdate_avx_32_79(\&body_20_39_dec);
1638 &Xupdate_avx_32_79(\&body_40_59_dec);
1639 &Xupdate_avx_32_79(\&body_40_59_dec);
1640 &Xupdate_avx_32_79(\&body_40_59_dec);
1641 &Xupdate_avx_32_79(\&body_40_59_dec);
1642 &Xupdate_avx_32_79(\&body_40_59_dec);
1643 &Xupdate_avx_32_79(\&body_20_39_dec);
1644 &Xuplast_avx_80(\&body_20_39_dec,".Ldone_dec_avx"); # can jump to "done"
1646 $saved_j=$j; @saved_V=@V;
1649 &Xloop_avx(\&body_20_39_dec);
1650 &Xloop_avx(\&body_20_39_dec);
1651 &Xloop_avx(\&body_20_39_dec);
1653 eval(@aes256_dec[-1]); # last store
1657 add 0($ctx),$A # update context
1664 mov @T[0],$B # magic seed
1675 $jj=$j=$saved_j; @V=@saved_V;
1678 &Xtail_avx(\&body_20_39_dec);
1679 &Xtail_avx(\&body_20_39_dec);
1680 &Xtail_avx(\&body_20_39_dec);
1682 eval(@aes256_dec[-1]); # last store
1685 add 0($ctx),$A # update context
1695 vmovups @X[3],($ivp) # write IV
1698 $code.=<<___ if ($win64);
1699 movaps 96+0(%rsp),%xmm6
1700 movaps 96+16(%rsp),%xmm7
1701 movaps 96+32(%rsp),%xmm8
1702 movaps 96+48(%rsp),%xmm9
1703 movaps 96+64(%rsp),%xmm10
1704 movaps 96+80(%rsp),%xmm11
1705 movaps 96+96(%rsp),%xmm12
1706 movaps 96+112(%rsp),%xmm13
1707 movaps 96+128(%rsp),%xmm14
1708 movaps 96+144(%rsp),%xmm15
1711 lea `104+($win64?10*16:0)`(%rsp),%rsi
1712 .cfi_def_cfa %rsi,56
1730 .size aesni256_cbc_sha1_dec_avx,.-aesni256_cbc_sha1_dec_avx
1737 .long 0x5a827999,0x5a827999,0x5a827999,0x5a827999 # K_00_19
1738 .long 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 # K_20_39
1739 .long 0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc # K_40_59
1740 .long 0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6 # K_60_79
1741 .long 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f # pbswap mask
1742 .byte 0xf,0xe,0xd,0xc,0xb,0xa,0x9,0x8,0x7,0x6,0x5,0x4,0x3,0x2,0x1,0x0
1744 .asciz "AESNI-CBC+SHA1 stitch for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
1748 ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
1752 ($iv,$in,$rndkey0)=map("%xmm$_",(2,14,15));
1753 @rndkey=("%xmm0","%xmm1");
1756 my ($BSWAP,$ABCD,$E,$E_,$ABCD_SAVE,$E_SAVE)=map("%xmm$_",(7..12));
1757 my @MSG=map("%xmm$_",(3..6));
1760 .type aesni_cbc_sha1_enc_shaext,\@function,6
1762 aesni_cbc_sha1_enc_shaext:
1763 mov `($win64?56:8)`(%rsp),$inp # load 7th argument
1765 $code.=<<___ if ($win64);
1766 lea `-8-10*16`(%rsp),%rsp
1767 movaps %xmm6,-8-10*16(%rax)
1768 movaps %xmm7,-8-9*16(%rax)
1769 movaps %xmm8,-8-8*16(%rax)
1770 movaps %xmm9,-8-7*16(%rax)
1771 movaps %xmm10,-8-6*16(%rax)
1772 movaps %xmm11,-8-5*16(%rax)
1773 movaps %xmm12,-8-4*16(%rax)
1774 movaps %xmm13,-8-3*16(%rax)
1775 movaps %xmm14,-8-2*16(%rax)
1776 movaps %xmm15,-8-1*16(%rax)
1782 movdqa K_XX_XX+0x50(%rip),$BSWAP # byte-n-word swap
1784 mov 240($key),$rounds
1786 movups ($key),$rndkey0 # $key[0]
1787 movups ($ivp),$iv # load IV
1788 movups 16($key),$rndkey[0] # forward reference
1789 lea 112($key),$key # size optimization
1791 pshufd \$0b00011011,$ABCD,$ABCD # flip word order
1792 pshufd \$0b00011011,$E,$E # flip word order
1800 movdqu ($inp),@MSG[0]
1801 movdqa $E,$E_SAVE # offload $E
1802 pshufb $BSWAP,@MSG[0]
1803 movdqu 0x10($inp),@MSG[1]
1804 movdqa $ABCD,$ABCD_SAVE # offload $ABCD
1808 pshufb $BSWAP,@MSG[1]
1811 movdqu 0x20($inp),@MSG[2]
1813 pxor $E_SAVE,@MSG[0] # black magic
1817 pxor $E_SAVE,@MSG[0] # black magic
1819 pshufb $BSWAP,@MSG[2]
1820 sha1rnds4 \$0,$E,$ABCD # 0-3
1821 sha1nexte @MSG[1],$E_
1825 sha1msg1 @MSG[1],@MSG[0]
1826 movdqu -0x10($inp),@MSG[3]
1828 pshufb $BSWAP,@MSG[3]
1832 sha1rnds4 \$0,$E_,$ABCD # 4-7
1833 sha1nexte @MSG[2],$E
1834 pxor @MSG[2],@MSG[0]
1835 sha1msg1 @MSG[2],@MSG[1]
1839 for($i=2;$i<20-4;$i++) {
1842 sha1rnds4 \$`int($i/5)`,$E,$ABCD # 8-11
1843 sha1nexte @MSG[3],$E_
1847 sha1msg2 @MSG[3],@MSG[0]
1848 pxor @MSG[3],@MSG[1]
1849 sha1msg1 @MSG[3],@MSG[2]
1852 push(@MSG,shift(@MSG));
1858 sha1rnds4 \$3,$E,$ABCD # 64-67
1859 sha1nexte @MSG[3],$E_
1860 sha1msg2 @MSG[3],@MSG[0]
1861 pxor @MSG[3],@MSG[1]
1866 sha1rnds4 \$3,$E_,$ABCD # 68-71
1867 sha1nexte @MSG[0],$E
1868 sha1msg2 @MSG[0],@MSG[1]
1872 movdqa $E_SAVE,@MSG[0]
1874 sha1rnds4 \$3,$E,$ABCD # 72-75
1875 sha1nexte @MSG[1],$E_
1880 sha1rnds4 \$3,$E_,$ABCD # 76-79
1881 sha1nexte $MSG[0],$E
1883 while($r<40) { &$aesenc(); } # remaining aesenc's
1887 paddd $ABCD_SAVE,$ABCD
1888 movups $iv,48($out,$in0) # write output
1892 pshufd \$0b00011011,$ABCD,$ABCD
1893 pshufd \$0b00011011,$E,$E
1894 movups $iv,($ivp) # write IV
1898 $code.=<<___ if ($win64);
1899 movaps -8-10*16(%rax),%xmm6
1900 movaps -8-9*16(%rax),%xmm7
1901 movaps -8-8*16(%rax),%xmm8
1902 movaps -8-7*16(%rax),%xmm9
1903 movaps -8-6*16(%rax),%xmm10
1904 movaps -8-5*16(%rax),%xmm11
1905 movaps -8-4*16(%rax),%xmm12
1906 movaps -8-3*16(%rax),%xmm13
1907 movaps -8-2*16(%rax),%xmm14
1908 movaps -8-1*16(%rax),%xmm15
1914 .size aesni_cbc_sha1_enc_shaext,.-aesni_cbc_sha1_enc_shaext
1917 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
1918 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
1926 .extern __imp_RtlVirtualUnwind
1927 .type ssse3_handler,\@abi-omnipotent
1941 mov 120($context),%rax # pull context->Rax
1942 mov 248($context),%rbx # pull context->Rip
1944 mov 8($disp),%rsi # disp->ImageBase
1945 mov 56($disp),%r11 # disp->HandlerData
1947 mov 0(%r11),%r10d # HandlerData[0]
1948 lea (%rsi,%r10),%r10 # prologue label
1949 cmp %r10,%rbx # context->Rip<prologue label
1950 jb .Lcommon_seh_tail
1952 mov 152($context),%rax # pull context->Rsp
1954 mov 4(%r11),%r10d # HandlerData[1]
1955 lea (%rsi,%r10),%r10 # epilogue label
1956 cmp %r10,%rbx # context->Rip>=epilogue label
1957 jae .Lcommon_seh_tail
1959 $code.=<<___ if ($shaext);
1960 lea aesni_cbc_sha1_enc_shaext(%rip),%r10
1965 lea 512($context),%rdi # &context.Xmm6
1967 .long 0xa548f3fc # cld; rep movsq
1968 lea 168(%rax),%rax # adjust stack pointer
1969 jmp .Lcommon_seh_tail
1974 lea 512($context),%rdi # &context.Xmm6
1976 .long 0xa548f3fc # cld; rep movsq
1977 lea `104+10*16`(%rax),%rax # adjust stack pointer
1986 mov %rbx,144($context) # restore context->Rbx
1987 mov %rbp,160($context) # restore context->Rbp
1988 mov %r12,216($context) # restore context->R12
1989 mov %r13,224($context) # restore context->R13
1990 mov %r14,232($context) # restore context->R14
1991 mov %r15,240($context) # restore context->R15
1996 mov %rax,152($context) # restore context->Rsp
1997 mov %rsi,168($context) # restore context->Rsi
1998 mov %rdi,176($context) # restore context->Rdi
2000 mov 40($disp),%rdi # disp->ContextRecord
2001 mov $context,%rsi # context
2002 mov \$154,%ecx # sizeof(CONTEXT)
2003 .long 0xa548f3fc # cld; rep movsq
2006 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
2007 mov 8(%rsi),%rdx # arg2, disp->ImageBase
2008 mov 0(%rsi),%r8 # arg3, disp->ControlPc
2009 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
2010 mov 40(%rsi),%r10 # disp->ContextRecord
2011 lea 56(%rsi),%r11 # &disp->HandlerData
2012 lea 24(%rsi),%r12 # &disp->EstablisherFrame
2013 mov %r10,32(%rsp) # arg5
2014 mov %r11,40(%rsp) # arg6
2015 mov %r12,48(%rsp) # arg7
2016 mov %rcx,56(%rsp) # arg8, (NULL)
2017 call *__imp_RtlVirtualUnwind(%rip)
2019 mov \$1,%eax # ExceptionContinueSearch
2031 .size ssse3_handler,.-ssse3_handler
2035 .rva .LSEH_begin_aesni_cbc_sha1_enc_ssse3
2036 .rva .LSEH_end_aesni_cbc_sha1_enc_ssse3
2037 .rva .LSEH_info_aesni_cbc_sha1_enc_ssse3
2039 $code.=<<___ if ($avx);
2040 .rva .LSEH_begin_aesni_cbc_sha1_enc_avx
2041 .rva .LSEH_end_aesni_cbc_sha1_enc_avx
2042 .rva .LSEH_info_aesni_cbc_sha1_enc_avx
2044 $code.=<<___ if ($shaext);
2045 .rva .LSEH_begin_aesni_cbc_sha1_enc_shaext
2046 .rva .LSEH_end_aesni_cbc_sha1_enc_shaext
2047 .rva .LSEH_info_aesni_cbc_sha1_enc_shaext
2052 .LSEH_info_aesni_cbc_sha1_enc_ssse3:
2055 .rva .Lprologue_ssse3,.Lepilogue_ssse3 # HandlerData[]
2057 $code.=<<___ if ($avx);
2058 .LSEH_info_aesni_cbc_sha1_enc_avx:
2061 .rva .Lprologue_avx,.Lepilogue_avx # HandlerData[]
2063 $code.=<<___ if ($shaext);
2064 .LSEH_info_aesni_cbc_sha1_enc_shaext:
2067 .rva .Lprologue_shaext,.Lepilogue_shaext # HandlerData[]
2071 ####################################################################
2073 local *opcode=shift;
2077 $rex|=0x04 if($dst>=8);
2078 $rex|=0x01 if($src>=8);
2079 unshift @opcode,$rex|0x40 if($rex);
2083 if (@_[0] =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
2084 my @opcode=(0x0f,0x3a,0xcc);
2085 rex(\@opcode,$3,$2);
2086 push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
2088 push @opcode,$c=~/^0/?oct($c):$c;
2089 return ".byte\t".join(',',@opcode);
2091 return "sha1rnds4\t".@_[0];
2098 "sha1nexte" => 0xc8,
2100 "sha1msg2" => 0xca );
2102 if (defined($opcodelet{$instr}) && @_[0] =~ /%xmm([0-9]+),\s*%xmm([0-9]+)/) {
2103 my @opcode=(0x0f,0x38);
2104 rex(\@opcode,$2,$1);
2105 push @opcode,$opcodelet{$instr};
2106 push @opcode,0xc0|($1&7)|(($2&7)<<3); # ModR/M
2107 return ".byte\t".join(',',@opcode);
2109 return $instr."\t".@_[0];
2115 my @opcode=(0x0f,0x38);
2117 if ($line=~/(aes[a-z]+)\s+%xmm([0-9]+),\s*%xmm([0-9]+)/) {
2119 "aesenc" => 0xdc, "aesenclast" => 0xdd,
2120 "aesdec" => 0xde, "aesdeclast" => 0xdf
2122 return undef if (!defined($opcodelet{$1}));
2123 rex(\@opcode,$3,$2);
2124 push @opcode,$opcodelet{$1},0xc0|($2&7)|(($3&7)<<3); # ModR/M
2125 unshift @opcode,0x66;
2126 return ".byte\t".join(',',@opcode);
2131 foreach (split("\n",$code)) {
2132 s/\`([^\`]*)\`/eval $1/geo;
2134 s/\b(sha1rnds4)\s+(.*)/sha1rnds4($2)/geo or
2135 s/\b(sha1[^\s]*)\s+(.*)/sha1op38($1,$2)/geo or
2136 s/\b(aes.*%xmm[0-9]+).*$/aesni($1)/geo;