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
:
189 mov
`($win64?56:8)`(%rsp),$inp # load 7th argument
190 #shr \$6,$len # debugging artefact
191 #jz .Lepilogue_ssse3 # debugging artefact
198 lea
`-104-($win64?10*16:0)`(%rsp),%rsp
199 #mov $in0,$inp # debugging artefact
200 #lea 64(%rsp),$ctx # debugging artefact
202 $code.=<<___
if ($win64);
203 movaps
%xmm6,96+0(%rsp)
204 movaps
%xmm7,96+16(%rsp)
205 movaps
%xmm8,96+32(%rsp)
206 movaps
%xmm9,96+48(%rsp)
207 movaps
%xmm10,96+64(%rsp)
208 movaps
%xmm11,96+80(%rsp)
209 movaps
%xmm12,96+96(%rsp)
210 movaps
%xmm13,96+112(%rsp)
211 movaps
%xmm14,96+128(%rsp)
212 movaps
%xmm15,96+144(%rsp)
216 mov
$in0,%r12 # reassign arguments
219 lea
112($key),%r15 # size optimization
220 movdqu
($ivp),$iv # load IV
221 mov
$ivp,88(%rsp) # save $ivp
223 ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
224 my $rounds="${ivp}d";
228 mov
240-112($key),$rounds
229 add
$inp,$len # end of input
231 lea K_XX_XX
(%rip),$K_XX_XX
232 mov
0($ctx),$A # load context
236 mov
$B,@T[0] # magic seed
242 movdqa
64($K_XX_XX),@Tx[2] # pbswap mask
243 movdqa
0($K_XX_XX),@Tx[1] # K_00_19
244 movdqu
0($inp),@X[-4&7] # load input to %xmm[0-3]
245 movdqu
16($inp),@X[-3&7]
246 movdqu
32($inp),@X[-2&7]
247 movdqu
48($inp),@X[-1&7]
248 pshufb
@Tx[2],@X[-4&7] # byte swap
249 pshufb
@Tx[2],@X[-3&7]
250 pshufb
@Tx[2],@X[-2&7]
252 paddd
@Tx[1],@X[-4&7] # add K_00_19
253 pshufb
@Tx[2],@X[-1&7]
254 paddd
@Tx[1],@X[-3&7]
255 paddd
@Tx[1],@X[-2&7]
256 movdqa
@X[-4&7],0(%rsp) # X[]+K xfer to IALU
257 psubd
@Tx[1],@X[-4&7] # restore X[]
258 movdqa
@X[-3&7],16(%rsp)
259 psubd
@Tx[1],@X[-3&7]
260 movdqa
@X[-2&7],32(%rsp)
261 psubd
@Tx[1],@X[-2&7]
262 movups
-112($key),$rndkey0 # $key[0]
263 movups
16-112($key),$rndkey[0] # forward reference
269 my ($n,$k)=($r/10,$r%10);
272 movups
`16*$n`($in0),$in # load input
275 $code.=<<___
if ($n);
276 movups
$iv,`16*($n-1)`($out,$in0) # write output
280 movups
`32+16*$k-112`($key),$rndkey[1]
281 aesenc
$rndkey[0],$iv
288 movups
`32+16*($k+0)-112`($key),$rndkey[1]
289 aesenc
$rndkey[0],$iv
290 movups
`32+16*($k+1)-112`($key),$rndkey[0]
291 aesenc
$rndkey[1],$iv
293 movups
`32+16*($k+2)-112`($key),$rndkey[1]
294 aesenc
$rndkey[0],$iv
295 movups
`32+16*($k+3)-112`($key),$rndkey[0]
296 aesenc
$rndkey[1],$iv
298 aesenclast
$rndkey[0],$iv
299 movups
16-112($key),$rndkey[1] # forward reference
303 movups
`32+16*$k-112`($key),$rndkey[1]
304 aesenc
$rndkey[0],$iv
307 $r++; unshift(@rndkey,pop(@rndkey));
310 sub Xupdate_ssse3_16_31
() # recall that $Xi starts wtih 4
313 my @insns = (&$body,&$body,&$body,&$body); # 40 instructions
316 eval(shift(@insns)); # ror
317 &pshufd
(@X[0],@X[-4&7],0xee); # was &movdqa (@X[0],@X[-3&7]);
319 &movdqa
(@Tx[0],@X[-1&7]);
320 &paddd
(@Tx[1],@X[-1&7]);
324 &punpcklqdq
(@X[0],@X[-3&7]); # compose "X[-14]" in "X[0]", was &palignr(@X[0],@X[-4&7],8);
326 eval(shift(@insns)); # rol
328 &psrldq
(@Tx[0],4); # "X[-3]", 3 dwords
332 &pxor
(@X[0],@X[-4&7]); # "X[0]"^="X[-16]"
334 eval(shift(@insns)); # ror
335 &pxor
(@Tx[0],@X[-2&7]); # "X[-3]"^"X[-8]"
340 &pxor
(@X[0],@Tx[0]); # "X[0]"^="X[-3]"^"X[-8]"
342 eval(shift(@insns)); # rol
343 &movdqa
(eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
347 &movdqa
(@Tx[2],@X[0]);
350 eval(shift(@insns)); # ror
351 &movdqa
(@Tx[0],@X[0]);
354 &pslldq
(@Tx[2],12); # "X[0]"<<96, extract one dword
355 &paddd
(@X[0],@X[0]);
361 eval(shift(@insns)); # rol
363 &movdqa
(@Tx[1],@Tx[2]);
369 eval(shift(@insns)); # ror
370 &por
(@X[0],@Tx[0]); # "X[0]"<<<=1
376 &pxor
(@X[0],@Tx[2]);
378 &movdqa
(@Tx[2],eval(16*(($Xi)/5))."($K_XX_XX)"); # K_XX_XX
379 eval(shift(@insns)); # rol
383 &pxor
(@X[0],@Tx[1]); # "X[0]"^=("X[0]">>96)<<<2
384 &pshufd
(@Tx[1],@X[-1&7],0xee) if ($Xi==7); # was &movdqa (@Tx[0],@X[-1&7]) in Xupdate_ssse3_32_79
386 foreach (@insns) { eval; } # remaining instructions [if any]
388 $Xi++; push(@X,shift(@X)); # "rotate" X[]
389 push(@Tx,shift(@Tx));
392 sub Xupdate_ssse3_32_79
()
395 my @insns = (&$body,&$body,&$body,&$body); # 32 to 44 instructions
398 eval(shift(@insns)) if ($Xi==8);
399 &pxor
(@X[0],@X[-4&7]); # "X[0]"="X[-32]"^"X[-16]"
400 eval(shift(@insns)) if ($Xi==8);
401 eval(shift(@insns)); # body_20_39
403 eval(shift(@insns)) if (@insns[1] =~ /_ror/);
404 eval(shift(@insns)) if (@insns[0] =~ /_ror/);
405 &punpcklqdq
(@Tx[0],@X[-1&7]); # compose "X[-6]", was &palignr(@Tx[0],@X[-2&7],8);
407 eval(shift(@insns)); # rol
409 &pxor
(@X[0],@X[-7&7]); # "X[0]"^="X[-28]"
413 &movdqa
(@Tx[2],@Tx[1]);# "perpetuate" K_XX_XX...
414 } else { # ... or load next one
415 &movdqa
(@Tx[2],eval(16*($Xi/5))."($K_XX_XX)");
417 eval(shift(@insns)); # ror
418 &paddd
(@Tx[1],@X[-1&7]);
421 &pxor
(@X[0],@Tx[0]); # "X[0]"^="X[-6]"
422 eval(shift(@insns)); # body_20_39
425 eval(shift(@insns)); # rol
426 eval(shift(@insns)) if (@insns[0] =~ /_ror/);
428 &movdqa
(@Tx[0],@X[0]);
431 &movdqa
(eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
432 eval(shift(@insns)); # ror
434 eval(shift(@insns)); # body_20_39
440 eval(shift(@insns)) if (@insns[0] =~ /_rol/);# rol
443 eval(shift(@insns)); # ror
445 &por
(@X[0],@Tx[0]); # "X[0]"<<<=2
447 eval(shift(@insns)); # body_20_39
448 eval(shift(@insns)) if (@insns[1] =~ /_rol/);
449 eval(shift(@insns)) if (@insns[0] =~ /_rol/);
450 &pshufd
(@Tx[1],@X[-1&7],0xee) if ($Xi<19); # was &movdqa (@Tx[1],@X[0])
452 eval(shift(@insns)); # rol
455 eval(shift(@insns)); # rol
458 foreach (@insns) { eval; } # remaining instructions
460 $Xi++; push(@X,shift(@X)); # "rotate" X[]
461 push(@Tx,shift(@Tx));
464 sub Xuplast_ssse3_80
()
467 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
474 &paddd
(@Tx[1],@X[-1&7]);
478 &movdqa
(eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer IALU
480 foreach (@insns) { eval; } # remaining instructions
485 unshift(@Tx,pop(@Tx));
487 &movdqa
(@Tx[2],"64($K_XX_XX)"); # pbswap mask
488 &movdqa
(@Tx[1],"0($K_XX_XX)"); # K_00_19
489 &movdqu
(@X[-4&7],"0($inp)"); # load input
490 &movdqu
(@X[-3&7],"16($inp)");
491 &movdqu
(@X[-2&7],"32($inp)");
492 &movdqu
(@X[-1&7],"48($inp)");
493 &pshufb
(@X[-4&7],@Tx[2]); # byte swap
502 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
508 &pshufb
(@X[($Xi-3)&7],@Tx[2]);
513 &paddd
(@X[($Xi-4)&7],@Tx[1]);
518 &movdqa
(eval(16*$Xi)."(%rsp)",@X[($Xi-4)&7]); # X[]+K xfer to IALU
523 &psubd
(@X[($Xi-4)&7],@Tx[1]);
525 foreach (@insns) { eval; }
532 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
535 foreach (@insns) { eval; }
539 '($a,$b,$c,$d,$e)=@V;'.
540 '&$_ror ($b,$j?7:2);', # $b>>>2
542 '&mov (@T[1],$a);', # $b for next round
544 '&add ($e,eval(4*($j&15))."(%rsp)");',# X[]+K xfer
545 '&xor ($b,$c);', # $c^$d for next round
549 '&and (@T[1],$b);', # ($b&($c^$d)) for next round
551 '&xor ($b,$c);', # restore $b
552 '&add ($e,$a);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
555 sub body_00_19
() { # ((c^d)&b)^d
556 # on start @T[0]=(c^d)&b
557 return &body_20_39
() if ($rx==19); $rx++;
564 $k = (($jj+1)*12/20)*20*$n/12; # 12 aesencs per these 20 rounds
565 @r[$k%$n].='&$aesenc();' if ($jj==$k/$n);
572 '($a,$b,$c,$d,$e)=@V;'.
573 '&add ($e,eval(4*($j&15))."(%rsp)");',# X[]+K xfer
574 '&xor (@T[0],$d) if($j==19);'.
575 '&xor (@T[0],$c) if($j> 19);', # ($b^$d^$c)
576 '&mov (@T[1],$a);', # $b for next round
580 '&xor (@T[1],$c) if ($j< 79);', # $b^$d for next round
582 '&$_ror ($b,7);', # $b>>>2
583 '&add ($e,$a);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
586 sub body_20_39
() { # b^d^c
588 return &body_40_59
() if ($rx==39); $rx++;
595 $k = (($jj+1)*8/20)*20*$n/8; # 8 aesencs per these 20 rounds
596 @r[$k%$n].='&$aesenc();' if ($jj==$k/$n && $rx!=20);
603 '($a,$b,$c,$d,$e)=@V;'.
604 '&add ($e,eval(4*($j&15))."(%rsp)");',# X[]+K xfer
605 '&and (@T[0],$c) if ($j>=40);', # (b^c)&(c^d)
606 '&xor ($c,$d) if ($j>=40);', # restore $c
608 '&$_ror ($b,7);', # $b>>>2
609 '&mov (@T[1],$a);', # $b for next round
614 '&xor (@T[1],$c) if ($j==59);'.
615 '&xor (@T[1],$b) if ($j< 59);', # b^c for next round
617 '&xor ($b,$c) if ($j< 59);', # c^d for next round
618 '&add ($e,$a);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
621 sub body_40_59
() { # ((b^c)&(c^d))^c
622 # on entry @T[0]=(b^c), (c^=d)
630 $k=(($jj+1)*12/20)*20*$n/12; # 12 aesencs per these 20 rounds
631 @r[$k%$n].='&$aesenc();' if ($jj==$k/$n && $rx!=40);
640 &Xupdate_ssse3_16_31
(\
&body_00_19
);
641 &Xupdate_ssse3_16_31
(\
&body_00_19
);
642 &Xupdate_ssse3_16_31
(\
&body_00_19
);
643 &Xupdate_ssse3_16_31
(\
&body_00_19
);
644 &Xupdate_ssse3_32_79
(\
&body_00_19
);
645 &Xupdate_ssse3_32_79
(\
&body_20_39
);
646 &Xupdate_ssse3_32_79
(\
&body_20_39
);
647 &Xupdate_ssse3_32_79
(\
&body_20_39
);
648 &Xupdate_ssse3_32_79
(\
&body_20_39
);
649 &Xupdate_ssse3_32_79
(\
&body_20_39
);
650 &Xupdate_ssse3_32_79
(\
&body_40_59
);
651 &Xupdate_ssse3_32_79
(\
&body_40_59
);
652 &Xupdate_ssse3_32_79
(\
&body_40_59
);
653 &Xupdate_ssse3_32_79
(\
&body_40_59
);
654 &Xupdate_ssse3_32_79
(\
&body_40_59
);
655 &Xupdate_ssse3_32_79
(\
&body_20_39
);
656 &Xuplast_ssse3_80
(\
&body_20_39
,".Ldone_ssse3"); # can jump to "done"
658 $saved_j=$j; @saved_V=@V;
659 $saved_r=$r; @saved_rndkey=@rndkey;
661 &Xloop_ssse3
(\
&body_20_39
);
662 &Xloop_ssse3
(\
&body_20_39
);
663 &Xloop_ssse3
(\
&body_20_39
);
666 movups
$iv,48($out,$in0) # write output
669 add
0($ctx),$A # update context
676 mov
@T[0],$B # magic seed
687 $jj=$j=$saved_j; @V=@saved_V;
688 $r=$saved_r; @rndkey=@saved_rndkey;
690 &Xtail_ssse3
(\
&body_20_39
);
691 &Xtail_ssse3
(\
&body_20_39
);
692 &Xtail_ssse3
(\
&body_20_39
);
695 movups
$iv,48($out,$in0) # write output
696 mov
88(%rsp),$ivp # restore $ivp
698 add
0($ctx),$A # update context
708 movups
$iv,($ivp) # write IV
710 $code.=<<___
if ($win64);
711 movaps
96+0(%rsp),%xmm6
712 movaps
96+16(%rsp),%xmm7
713 movaps
96+32(%rsp),%xmm8
714 movaps
96+48(%rsp),%xmm9
715 movaps
96+64(%rsp),%xmm10
716 movaps
96+80(%rsp),%xmm11
717 movaps
96+96(%rsp),%xmm12
718 movaps
96+112(%rsp),%xmm13
719 movaps
96+128(%rsp),%xmm14
720 movaps
96+144(%rsp),%xmm15
723 lea
`104+($win64?10*16:0)`(%rsp),%rsi
733 .size aesni_cbc_sha1_enc_ssse3
,.-aesni_cbc_sha1_enc_ssse3
736 if ($stitched_decrypt) {{{
738 ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
742 # reassign for Atom Silvermont (see above)
743 ($inout0,$inout1,$inout2,$inout3,$rndkey0)=map("%xmm$_",(0..4));
744 @X=map("%xmm$_",(8..13,6,7));
745 @Tx=map("%xmm$_",(14,15,5));
748 '&movdqu($inout0,"0x00($in0)");',
749 '&movdqu($inout1,"0x10($in0)"); &pxor ($inout0,$rndkey0);',
750 '&movdqu($inout2,"0x20($in0)"); &pxor ($inout1,$rndkey0);',
751 '&movdqu($inout3,"0x30($in0)"); &pxor ($inout2,$rndkey0);',
753 '&pxor ($inout3,$rndkey0); &movups ($rndkey0,"16-112($key)");',
754 '&movaps("64(%rsp)",@X[2]);', # save IV, originally @X[3]
757 for ($i=0;$i<13;$i++) {
759 '&aesdec ($inout0,$rndkey0);',
760 '&aesdec ($inout1,$rndkey0);',
761 '&aesdec ($inout2,$rndkey0);',
762 '&aesdec ($inout3,$rndkey0); &movups($rndkey0,"'.(16*($i+2)-112).'($key)");'
764 push (@aes256_dec,(undef,undef)) if (($i>=3 && $i<=5) || $i>=11);
765 push (@aes256_dec,(undef,undef)) if ($i==5);
768 '&aesdeclast ($inout0,$rndkey0); &movups (@X[0],"0x00($in0)");',
769 '&aesdeclast ($inout1,$rndkey0); &movups (@X[1],"0x10($in0)");',
770 '&aesdeclast ($inout2,$rndkey0); &movups (@X[2],"0x20($in0)");',
771 '&aesdeclast ($inout3,$rndkey0); &movups (@X[3],"0x30($in0)");',
773 '&xorps ($inout0,"64(%rsp)"); &movdqu ($rndkey0,"-112($key)");',
774 '&xorps ($inout1,@X[0]); &movups ("0x00($out,$in0)",$inout0);',
775 '&xorps ($inout2,@X[1]); &movups ("0x10($out,$in0)",$inout1);',
776 '&xorps ($inout3,@X[2]); &movups ("0x20($out,$in0)",$inout2);',
778 '&movups ("0x30($out,$in0)",$inout3);'
781 sub body_00_19_dec
() { # ((c^d)&b)^d
782 # on start @T[0]=(c^d)&b
783 return &body_20_39_dec
() if ($rx==19);
787 unshift (@r,@aes256_dec[$rx]) if (@aes256_dec[$rx]);
793 sub body_20_39_dec
() { # b^d^c
795 return &body_40_59_dec
() if ($rx==39);
799 unshift (@r,@aes256_dec[$rx]) if (@aes256_dec[$rx]);
805 sub body_40_59_dec
() { # ((b^c)&(c^d))^c
806 # on entry @T[0]=(b^c), (c^=d)
810 unshift (@r,@aes256_dec[$rx]) if (@aes256_dec[$rx]);
817 .globl aesni256_cbc_sha1_dec
818 .type aesni256_cbc_sha1_dec
,\
@abi-omnipotent
820 aesni256_cbc_sha1_dec
:
821 # caller should check for SSSE3 and AES-NI bits
822 mov OPENSSL_ia32cap_P
+0(%rip),%r10d
823 mov OPENSSL_ia32cap_P
+4(%rip),%r11d
825 $code.=<<___
if ($avx);
826 and \
$`1<<28`,%r11d # mask AVX bit
827 and \
$`1<<30`,%r10d # mask "Intel CPU" bit
829 cmp \
$`1<<28|1<<30`,%r10d
830 je aesni256_cbc_sha1_dec_avx
833 jmp aesni256_cbc_sha1_dec_ssse3
835 .size aesni256_cbc_sha1_dec
,.-aesni256_cbc_sha1_dec
837 .type aesni256_cbc_sha1_dec_ssse3
,\
@function,6
839 aesni256_cbc_sha1_dec_ssse3
:
840 mov
`($win64?56:8)`(%rsp),$inp # load 7th argument
847 lea
`-104-($win64?10*16:0)`(%rsp),%rsp
849 $code.=<<___
if ($win64);
850 movaps
%xmm6,96+0(%rsp)
851 movaps
%xmm7,96+16(%rsp)
852 movaps
%xmm8,96+32(%rsp)
853 movaps
%xmm9,96+48(%rsp)
854 movaps
%xmm10,96+64(%rsp)
855 movaps
%xmm11,96+80(%rsp)
856 movaps
%xmm12,96+96(%rsp)
857 movaps
%xmm13,96+112(%rsp)
858 movaps
%xmm14,96+128(%rsp)
859 movaps
%xmm15,96+144(%rsp)
860 .Lprologue_dec_ssse3
:
863 mov
$in0,%r12 # reassign arguments
866 lea
112($key),%r15 # size optimization
867 movdqu
($ivp),@X[3] # load IV
868 #mov $ivp,88(%rsp) # save $ivp
870 ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
874 add
$inp,$len # end of input
876 lea K_XX_XX
(%rip),$K_XX_XX
877 mov
0($ctx),$A # load context
881 mov
$B,@T[0] # magic seed
887 movdqa
64($K_XX_XX),@Tx[2] # pbswap mask
888 movdqa
0($K_XX_XX),@Tx[1] # K_00_19
889 movdqu
0($inp),@X[-4&7] # load input to %xmm[0-3]
890 movdqu
16($inp),@X[-3&7]
891 movdqu
32($inp),@X[-2&7]
892 movdqu
48($inp),@X[-1&7]
893 pshufb
@Tx[2],@X[-4&7] # byte swap
895 pshufb
@Tx[2],@X[-3&7]
896 pshufb
@Tx[2],@X[-2&7]
897 pshufb
@Tx[2],@X[-1&7]
898 paddd
@Tx[1],@X[-4&7] # add K_00_19
899 paddd
@Tx[1],@X[-3&7]
900 paddd
@Tx[1],@X[-2&7]
901 movdqa
@X[-4&7],0(%rsp) # X[]+K xfer to IALU
902 psubd
@Tx[1],@X[-4&7] # restore X[]
903 movdqa
@X[-3&7],16(%rsp)
904 psubd
@Tx[1],@X[-3&7]
905 movdqa
@X[-2&7],32(%rsp)
906 psubd
@Tx[1],@X[-2&7]
907 movdqu
-112($key),$rndkey0 # $key[0]
913 &Xupdate_ssse3_16_31
(\
&body_00_19_dec
);
914 &Xupdate_ssse3_16_31
(\
&body_00_19_dec
);
915 &Xupdate_ssse3_16_31
(\
&body_00_19_dec
);
916 &Xupdate_ssse3_16_31
(\
&body_00_19_dec
);
917 &Xupdate_ssse3_32_79
(\
&body_00_19_dec
);
918 &Xupdate_ssse3_32_79
(\
&body_20_39_dec
);
919 &Xupdate_ssse3_32_79
(\
&body_20_39_dec
);
920 &Xupdate_ssse3_32_79
(\
&body_20_39_dec
);
921 &Xupdate_ssse3_32_79
(\
&body_20_39_dec
);
922 &Xupdate_ssse3_32_79
(\
&body_20_39_dec
);
923 &Xupdate_ssse3_32_79
(\
&body_40_59_dec
);
924 &Xupdate_ssse3_32_79
(\
&body_40_59_dec
);
925 &Xupdate_ssse3_32_79
(\
&body_40_59_dec
);
926 &Xupdate_ssse3_32_79
(\
&body_40_59_dec
);
927 &Xupdate_ssse3_32_79
(\
&body_40_59_dec
);
928 &Xupdate_ssse3_32_79
(\
&body_20_39_dec
);
929 &Xuplast_ssse3_80
(\
&body_20_39_dec
,".Ldone_dec_ssse3"); # can jump to "done"
931 $saved_j=$j; @saved_V=@V;
934 &Xloop_ssse3
(\
&body_20_39_dec
);
935 &Xloop_ssse3
(\
&body_20_39_dec
);
936 &Xloop_ssse3
(\
&body_20_39_dec
);
938 eval(@aes256_dec[-1]); # last store
942 add
0($ctx),$A # update context
949 mov
@T[0],$B # magic seed
960 $jj=$j=$saved_j; @V=@saved_V;
963 &Xtail_ssse3
(\
&body_20_39_dec
);
964 &Xtail_ssse3
(\
&body_20_39_dec
);
965 &Xtail_ssse3
(\
&body_20_39_dec
);
967 eval(@aes256_dec[-1]); # last store
969 add
0($ctx),$A # update context
979 movups
@X[3],($ivp) # write IV
981 $code.=<<___
if ($win64);
982 movaps
96+0(%rsp),%xmm6
983 movaps
96+16(%rsp),%xmm7
984 movaps
96+32(%rsp),%xmm8
985 movaps
96+48(%rsp),%xmm9
986 movaps
96+64(%rsp),%xmm10
987 movaps
96+80(%rsp),%xmm11
988 movaps
96+96(%rsp),%xmm12
989 movaps
96+112(%rsp),%xmm13
990 movaps
96+128(%rsp),%xmm14
991 movaps
96+144(%rsp),%xmm15
994 lea
`104+($win64?10*16:0)`(%rsp),%rsi
1002 .Lepilogue_dec_ssse3
:
1004 .size aesni256_cbc_sha1_dec_ssse3
,.-aesni256_cbc_sha1_dec_ssse3
1010 my ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
1013 my @X=map("%xmm$_",(4..7,0..3));
1014 my @Tx=map("%xmm$_",(8..10));
1015 my @V=($A,$B,$C,$D,$E)=("%eax","%ebx","%ecx","%edx","%ebp"); # size optimization
1016 my @T=("%esi","%edi");
1017 my ($rndkey0,$iv,$in)=map("%xmm$_",(11..13));
1018 my @rndkey=("%xmm14","%xmm15");
1019 my ($inout0,$inout1,$inout2,$inout3)=map("%xmm$_",(12..15)); # for dec
1022 my $_rol=sub { &shld
(@_[0],@_) };
1023 my $_ror=sub { &shrd
(@_[0],@_) };
1026 .type aesni_cbc_sha1_enc_avx
,\
@function,6
1028 aesni_cbc_sha1_enc_avx
:
1029 mov
`($win64?56:8)`(%rsp),$inp # load 7th argument
1030 #shr \$6,$len # debugging artefact
1031 #jz .Lepilogue_avx # debugging artefact
1038 lea
`-104-($win64?10*16:0)`(%rsp),%rsp
1039 #mov $in0,$inp # debugging artefact
1040 #lea 64(%rsp),$ctx # debugging artefact
1042 $code.=<<___
if ($win64);
1043 movaps
%xmm6,96+0(%rsp)
1044 movaps
%xmm7,96+16(%rsp)
1045 movaps
%xmm8,96+32(%rsp)
1046 movaps
%xmm9,96+48(%rsp)
1047 movaps
%xmm10,96+64(%rsp)
1048 movaps
%xmm11,96+80(%rsp)
1049 movaps
%xmm12,96+96(%rsp)
1050 movaps
%xmm13,96+112(%rsp)
1051 movaps
%xmm14,96+128(%rsp)
1052 movaps
%xmm15,96+144(%rsp)
1057 mov
$in0,%r12 # reassign arguments
1060 lea
112($key),%r15 # size optimization
1061 vmovdqu
($ivp),$iv # load IV
1062 mov
$ivp,88(%rsp) # save $ivp
1064 ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
1065 my $rounds="${ivp}d";
1069 mov
240-112($key),$rounds
1070 add
$inp,$len # end of input
1072 lea K_XX_XX
(%rip),$K_XX_XX
1073 mov
0($ctx),$A # load context
1077 mov
$B,@T[0] # magic seed
1083 vmovdqa
64($K_XX_XX),@X[2] # pbswap mask
1084 vmovdqa
0($K_XX_XX),$Kx # K_00_19
1085 vmovdqu
0($inp),@X[-4&7] # load input to %xmm[0-3]
1086 vmovdqu
16($inp),@X[-3&7]
1087 vmovdqu
32($inp),@X[-2&7]
1088 vmovdqu
48($inp),@X[-1&7]
1089 vpshufb
@X[2],@X[-4&7],@X[-4&7] # byte swap
1091 vpshufb
@X[2],@X[-3&7],@X[-3&7]
1092 vpshufb
@X[2],@X[-2&7],@X[-2&7]
1093 vpshufb
@X[2],@X[-1&7],@X[-1&7]
1094 vpaddd
$Kx,@X[-4&7],@X[0] # add K_00_19
1095 vpaddd
$Kx,@X[-3&7],@X[1]
1096 vpaddd
$Kx,@X[-2&7],@X[2]
1097 vmovdqa
@X[0],0(%rsp) # X[]+K xfer to IALU
1098 vmovdqa
@X[1],16(%rsp)
1099 vmovdqa
@X[2],32(%rsp)
1100 vmovups
-112($key),$rndkey[1] # $key[0]
1101 vmovups
16-112($key),$rndkey[0] # forward reference
1107 my ($n,$k)=($r/10,$r%10);
1110 vmovdqu
`16*$n`($in0),$in # load input
1111 vpxor
$rndkey[1],$in,$in
1113 $code.=<<___
if ($n);
1114 vmovups
$iv,`16*($n-1)`($out,$in0) # write output
1118 vaesenc
$rndkey[0],$iv,$iv
1119 vmovups
`32+16*$k-112`($key),$rndkey[1]
1126 vaesenc
$rndkey[0],$iv,$iv
1127 vmovups
`32+16*($k+0)-112`($key),$rndkey[1]
1128 vaesenc
$rndkey[1],$iv,$iv
1129 vmovups
`32+16*($k+1)-112`($key),$rndkey[0]
1131 vaesenc
$rndkey[0],$iv,$iv
1132 vmovups
`32+16*($k+2)-112`($key),$rndkey[1]
1133 vaesenc
$rndkey[1],$iv,$iv
1134 vmovups
`32+16*($k+3)-112`($key),$rndkey[0]
1136 vaesenclast
$rndkey[0],$iv,$iv
1137 vmovups
-112($key),$rndkey[0]
1138 vmovups
16-112($key),$rndkey[1] # forward reference
1142 vaesenc
$rndkey[0],$iv,$iv
1143 vmovups
`32+16*$k-112`($key),$rndkey[1]
1146 $r++; unshift(@rndkey,pop(@rndkey));
1149 sub Xupdate_avx_16_31
() # recall that $Xi starts wtih 4
1152 my @insns = (&$body,&$body,&$body,&$body); # 40 instructions
1153 my ($a,$b,$c,$d,$e);
1155 eval(shift(@insns));
1156 eval(shift(@insns));
1157 &vpalignr
(@X[0],@X[-3&7],@X[-4&7],8); # compose "X[-14]" in "X[0]"
1158 eval(shift(@insns));
1159 eval(shift(@insns));
1161 &vpaddd
(@Tx[1],$Kx,@X[-1&7]);
1162 eval(shift(@insns));
1163 eval(shift(@insns));
1164 &vpsrldq
(@Tx[0],@X[-1&7],4); # "X[-3]", 3 dwords
1165 eval(shift(@insns));
1166 eval(shift(@insns));
1167 &vpxor
(@X[0],@X[0],@X[-4&7]); # "X[0]"^="X[-16]"
1168 eval(shift(@insns));
1169 eval(shift(@insns));
1171 &vpxor
(@Tx[0],@Tx[0],@X[-2&7]); # "X[-3]"^"X[-8]"
1172 eval(shift(@insns));
1173 eval(shift(@insns));
1174 eval(shift(@insns));
1175 eval(shift(@insns));
1177 &vpxor
(@X[0],@X[0],@Tx[0]); # "X[0]"^="X[-3]"^"X[-8]"
1178 eval(shift(@insns));
1179 eval(shift(@insns));
1180 &vmovdqa
(eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
1181 eval(shift(@insns));
1182 eval(shift(@insns));
1184 &vpsrld
(@Tx[0],@X[0],31);
1185 eval(shift(@insns));
1186 eval(shift(@insns));
1187 eval(shift(@insns));
1188 eval(shift(@insns));
1190 &vpslldq
(@Tx[1],@X[0],12); # "X[0]"<<96, extract one dword
1191 &vpaddd
(@X[0],@X[0],@X[0]);
1192 eval(shift(@insns));
1193 eval(shift(@insns));
1194 eval(shift(@insns));
1195 eval(shift(@insns));
1197 &vpor
(@X[0],@X[0],@Tx[0]); # "X[0]"<<<=1
1198 &vpsrld
(@Tx[0],@Tx[1],30);
1199 eval(shift(@insns));
1200 eval(shift(@insns));
1201 eval(shift(@insns));
1202 eval(shift(@insns));
1204 &vpslld
(@Tx[1],@Tx[1],2);
1205 &vpxor
(@X[0],@X[0],@Tx[0]);
1206 eval(shift(@insns));
1207 eval(shift(@insns));
1208 eval(shift(@insns));
1209 eval(shift(@insns));
1211 &vpxor
(@X[0],@X[0],@Tx[1]); # "X[0]"^=("X[0]">>96)<<<2
1212 eval(shift(@insns));
1213 eval(shift(@insns));
1214 &vmovdqa
($Kx,eval(16*(($Xi)/5))."($K_XX_XX)") if ($Xi%5==0); # K_XX_XX
1215 eval(shift(@insns));
1216 eval(shift(@insns));
1219 foreach (@insns) { eval; } # remaining instructions [if any]
1221 $Xi++; push(@X,shift(@X)); # "rotate" X[]
1224 sub Xupdate_avx_32_79
()
1227 my @insns = (&$body,&$body,&$body,&$body); # 32 to 48 instructions
1228 my ($a,$b,$c,$d,$e);
1230 &vpalignr
(@Tx[0],@X[-1&7],@X[-2&7],8); # compose "X[-6]"
1231 &vpxor
(@X[0],@X[0],@X[-4&7]); # "X[0]"="X[-32]"^"X[-16]"
1232 eval(shift(@insns)); # body_20_39
1233 eval(shift(@insns));
1234 eval(shift(@insns));
1235 eval(shift(@insns)); # rol
1237 &vpxor
(@X[0],@X[0],@X[-7&7]); # "X[0]"^="X[-28]"
1238 eval(shift(@insns));
1239 eval(shift(@insns)) if (@insns[0] !~ /&ro[rl]/);
1240 &vpaddd
(@Tx[1],$Kx,@X[-1&7]);
1241 &vmovdqa
($Kx,eval(16*($Xi/5))."($K_XX_XX)") if ($Xi%5==0);
1242 eval(shift(@insns)); # ror
1243 eval(shift(@insns));
1245 &vpxor
(@X[0],@X[0],@Tx[0]); # "X[0]"^="X[-6]"
1246 eval(shift(@insns)); # body_20_39
1247 eval(shift(@insns));
1248 eval(shift(@insns));
1249 eval(shift(@insns)); # rol
1251 &vpsrld
(@Tx[0],@X[0],30);
1252 &vmovdqa
(eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
1253 eval(shift(@insns));
1254 eval(shift(@insns));
1255 eval(shift(@insns)); # ror
1256 eval(shift(@insns));
1258 &vpslld
(@X[0],@X[0],2);
1259 eval(shift(@insns)); # body_20_39
1260 eval(shift(@insns));
1261 eval(shift(@insns));
1262 eval(shift(@insns)); # rol
1263 eval(shift(@insns));
1264 eval(shift(@insns));
1265 eval(shift(@insns)); # ror
1266 eval(shift(@insns));
1268 &vpor
(@X[0],@X[0],@Tx[0]); # "X[0]"<<<=2
1269 eval(shift(@insns)); # body_20_39
1270 eval(shift(@insns));
1271 eval(shift(@insns));
1272 eval(shift(@insns)); # rol
1273 eval(shift(@insns));
1274 eval(shift(@insns));
1275 eval(shift(@insns)); # rol
1276 eval(shift(@insns));
1278 foreach (@insns) { eval; } # remaining instructions
1280 $Xi++; push(@X,shift(@X)); # "rotate" X[]
1283 sub Xuplast_avx_80
()
1286 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
1287 my ($a,$b,$c,$d,$e);
1289 eval(shift(@insns));
1290 &vpaddd
(@Tx[1],$Kx,@X[-1&7]);
1291 eval(shift(@insns));
1292 eval(shift(@insns));
1293 eval(shift(@insns));
1294 eval(shift(@insns));
1296 &vmovdqa
(eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer IALU
1298 foreach (@insns) { eval; } # remaining instructions
1303 &vmovdqa
(@Tx[1],"64($K_XX_XX)"); # pbswap mask
1304 &vmovdqa
($Kx,"0($K_XX_XX)"); # K_00_19
1305 &vmovdqu
(@X[-4&7],"0($inp)"); # load input
1306 &vmovdqu
(@X[-3&7],"16($inp)");
1307 &vmovdqu
(@X[-2&7],"32($inp)");
1308 &vmovdqu
(@X[-1&7],"48($inp)");
1309 &vpshufb
(@X[-4&7],@X[-4&7],@Tx[1]); # byte swap
1318 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
1319 my ($a,$b,$c,$d,$e);
1321 eval(shift(@insns));
1322 eval(shift(@insns));
1323 &vpshufb
(@X[($Xi-3)&7],@X[($Xi-3)&7],@Tx[1]);
1324 eval(shift(@insns));
1325 eval(shift(@insns));
1326 &vpaddd
(@Tx[0],@X[($Xi-4)&7],$Kx);
1327 eval(shift(@insns));
1328 eval(shift(@insns));
1329 eval(shift(@insns));
1330 eval(shift(@insns));
1331 &vmovdqa
(eval(16*$Xi)."(%rsp)",@Tx[0]); # X[]+K xfer to IALU
1332 eval(shift(@insns));
1333 eval(shift(@insns));
1335 foreach (@insns) { eval; }
1342 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
1343 my ($a,$b,$c,$d,$e);
1345 foreach (@insns) { eval; }
1352 &Xupdate_avx_16_31
(\
&body_00_19
);
1353 &Xupdate_avx_16_31
(\
&body_00_19
);
1354 &Xupdate_avx_16_31
(\
&body_00_19
);
1355 &Xupdate_avx_16_31
(\
&body_00_19
);
1356 &Xupdate_avx_32_79
(\
&body_00_19
);
1357 &Xupdate_avx_32_79
(\
&body_20_39
);
1358 &Xupdate_avx_32_79
(\
&body_20_39
);
1359 &Xupdate_avx_32_79
(\
&body_20_39
);
1360 &Xupdate_avx_32_79
(\
&body_20_39
);
1361 &Xupdate_avx_32_79
(\
&body_20_39
);
1362 &Xupdate_avx_32_79
(\
&body_40_59
);
1363 &Xupdate_avx_32_79
(\
&body_40_59
);
1364 &Xupdate_avx_32_79
(\
&body_40_59
);
1365 &Xupdate_avx_32_79
(\
&body_40_59
);
1366 &Xupdate_avx_32_79
(\
&body_40_59
);
1367 &Xupdate_avx_32_79
(\
&body_20_39
);
1368 &Xuplast_avx_80
(\
&body_20_39
,".Ldone_avx"); # can jump to "done"
1370 $saved_j=$j; @saved_V=@V;
1371 $saved_r=$r; @saved_rndkey=@rndkey;
1373 &Xloop_avx
(\
&body_20_39
);
1374 &Xloop_avx
(\
&body_20_39
);
1375 &Xloop_avx
(\
&body_20_39
);
1378 vmovups
$iv,48($out,$in0) # write output
1381 add
0($ctx),$A # update context
1388 mov
@T[0],$B # magic seed
1399 $jj=$j=$saved_j; @V=@saved_V;
1400 $r=$saved_r; @rndkey=@saved_rndkey;
1402 &Xtail_avx
(\
&body_20_39
);
1403 &Xtail_avx
(\
&body_20_39
);
1404 &Xtail_avx
(\
&body_20_39
);
1407 vmovups
$iv,48($out,$in0) # write output
1408 mov
88(%rsp),$ivp # restore $ivp
1410 add
0($ctx),$A # update context
1420 vmovups
$iv,($ivp) # write IV
1423 $code.=<<___
if ($win64);
1424 movaps
96+0(%rsp),%xmm6
1425 movaps
96+16(%rsp),%xmm7
1426 movaps
96+32(%rsp),%xmm8
1427 movaps
96+48(%rsp),%xmm9
1428 movaps
96+64(%rsp),%xmm10
1429 movaps
96+80(%rsp),%xmm11
1430 movaps
96+96(%rsp),%xmm12
1431 movaps
96+112(%rsp),%xmm13
1432 movaps
96+128(%rsp),%xmm14
1433 movaps
96+144(%rsp),%xmm15
1436 lea
`104+($win64?10*16:0)`(%rsp),%rsi
1446 .size aesni_cbc_sha1_enc_avx
,.-aesni_cbc_sha1_enc_avx
1449 if ($stitched_decrypt) {{{
1451 ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
1457 '&vpxor ($inout0,$rndkey0,"0x00($in0)");',
1458 '&vpxor ($inout1,$rndkey0,"0x10($in0)");',
1459 '&vpxor ($inout2,$rndkey0,"0x20($in0)");',
1460 '&vpxor ($inout3,$rndkey0,"0x30($in0)");',
1462 '&vmovups($rndkey0,"16-112($key)");',
1463 '&vmovups("64(%rsp)",@X[2]);', # save IV, originally @X[3]
1466 for ($i=0;$i<13;$i++) {
1468 '&vaesdec ($inout0,$inout0,$rndkey0);',
1469 '&vaesdec ($inout1,$inout1,$rndkey0);',
1470 '&vaesdec ($inout2,$inout2,$rndkey0);',
1471 '&vaesdec ($inout3,$inout3,$rndkey0); &vmovups($rndkey0,"'.(16*($i+2)-112).'($key)");'
1473 push (@aes256_dec,(undef,undef)) if (($i>=3 && $i<=5) || $i>=11);
1474 push (@aes256_dec,(undef,undef)) if ($i==5);
1477 '&vaesdeclast ($inout0,$inout0,$rndkey0); &vmovups(@X[0],"0x00($in0)");',
1478 '&vaesdeclast ($inout1,$inout1,$rndkey0); &vmovups(@X[1],"0x10($in0)");',
1479 '&vaesdeclast ($inout2,$inout2,$rndkey0); &vmovups(@X[2],"0x20($in0)");',
1480 '&vaesdeclast ($inout3,$inout3,$rndkey0); &vmovups(@X[3],"0x30($in0)");',
1482 '&vxorps ($inout0,$inout0,"64(%rsp)"); &vmovdqu($rndkey0,"-112($key)");',
1483 '&vxorps ($inout1,$inout1,@X[0]); &vmovups("0x00($out,$in0)",$inout0);',
1484 '&vxorps ($inout2,$inout2,@X[1]); &vmovups("0x10($out,$in0)",$inout1);',
1485 '&vxorps ($inout3,$inout3,@X[2]); &vmovups("0x20($out,$in0)",$inout2);',
1487 '&vmovups ("0x30($out,$in0)",$inout3);'
1491 .type aesni256_cbc_sha1_dec_avx
,\
@function,6
1493 aesni256_cbc_sha1_dec_avx
:
1494 mov
`($win64?56:8)`(%rsp),$inp # load 7th argument
1501 lea
`-104-($win64?10*16:0)`(%rsp),%rsp
1503 $code.=<<___
if ($win64);
1504 movaps
%xmm6,96+0(%rsp)
1505 movaps
%xmm7,96+16(%rsp)
1506 movaps
%xmm8,96+32(%rsp)
1507 movaps
%xmm9,96+48(%rsp)
1508 movaps
%xmm10,96+64(%rsp)
1509 movaps
%xmm11,96+80(%rsp)
1510 movaps
%xmm12,96+96(%rsp)
1511 movaps
%xmm13,96+112(%rsp)
1512 movaps
%xmm14,96+128(%rsp)
1513 movaps
%xmm15,96+144(%rsp)
1518 mov
$in0,%r12 # reassign arguments
1521 lea
112($key),%r15 # size optimization
1522 vmovdqu
($ivp),@X[3] # load IV
1524 ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
1528 add
$inp,$len # end of input
1530 lea K_XX_XX
(%rip),$K_XX_XX
1531 mov
0($ctx),$A # load context
1535 mov
$B,@T[0] # magic seed
1541 vmovdqa
64($K_XX_XX),@X[2] # pbswap mask
1542 vmovdqa
0($K_XX_XX),$Kx # K_00_19
1543 vmovdqu
0($inp),@X[-4&7] # load input to %xmm[0-3]
1544 vmovdqu
16($inp),@X[-3&7]
1545 vmovdqu
32($inp),@X[-2&7]
1546 vmovdqu
48($inp),@X[-1&7]
1547 vpshufb
@X[2],@X[-4&7],@X[-4&7] # byte swap
1549 vpshufb
@X[2],@X[-3&7],@X[-3&7]
1550 vpshufb
@X[2],@X[-2&7],@X[-2&7]
1551 vpshufb
@X[2],@X[-1&7],@X[-1&7]
1552 vpaddd
$Kx,@X[-4&7],@X[0] # add K_00_19
1553 vpaddd
$Kx,@X[-3&7],@X[1]
1554 vpaddd
$Kx,@X[-2&7],@X[2]
1555 vmovdqa
@X[0],0(%rsp) # X[]+K xfer to IALU
1556 vmovdqa
@X[1],16(%rsp)
1557 vmovdqa
@X[2],32(%rsp)
1558 vmovups
-112($key),$rndkey0 # $key[0]
1564 &Xupdate_avx_16_31
(\
&body_00_19_dec
);
1565 &Xupdate_avx_16_31
(\
&body_00_19_dec
);
1566 &Xupdate_avx_16_31
(\
&body_00_19_dec
);
1567 &Xupdate_avx_16_31
(\
&body_00_19_dec
);
1568 &Xupdate_avx_32_79
(\
&body_00_19_dec
);
1569 &Xupdate_avx_32_79
(\
&body_20_39_dec
);
1570 &Xupdate_avx_32_79
(\
&body_20_39_dec
);
1571 &Xupdate_avx_32_79
(\
&body_20_39_dec
);
1572 &Xupdate_avx_32_79
(\
&body_20_39_dec
);
1573 &Xupdate_avx_32_79
(\
&body_20_39_dec
);
1574 &Xupdate_avx_32_79
(\
&body_40_59_dec
);
1575 &Xupdate_avx_32_79
(\
&body_40_59_dec
);
1576 &Xupdate_avx_32_79
(\
&body_40_59_dec
);
1577 &Xupdate_avx_32_79
(\
&body_40_59_dec
);
1578 &Xupdate_avx_32_79
(\
&body_40_59_dec
);
1579 &Xupdate_avx_32_79
(\
&body_20_39_dec
);
1580 &Xuplast_avx_80
(\
&body_20_39_dec
,".Ldone_dec_avx"); # can jump to "done"
1582 $saved_j=$j; @saved_V=@V;
1585 &Xloop_avx
(\
&body_20_39_dec
);
1586 &Xloop_avx
(\
&body_20_39_dec
);
1587 &Xloop_avx
(\
&body_20_39_dec
);
1589 eval(@aes256_dec[-1]); # last store
1593 add
0($ctx),$A # update context
1600 mov
@T[0],$B # magic seed
1611 $jj=$j=$saved_j; @V=@saved_V;
1614 &Xtail_avx
(\
&body_20_39_dec
);
1615 &Xtail_avx
(\
&body_20_39_dec
);
1616 &Xtail_avx
(\
&body_20_39_dec
);
1618 eval(@aes256_dec[-1]); # last store
1621 add
0($ctx),$A # update context
1631 vmovups
@X[3],($ivp) # write IV
1634 $code.=<<___
if ($win64);
1635 movaps
96+0(%rsp),%xmm6
1636 movaps
96+16(%rsp),%xmm7
1637 movaps
96+32(%rsp),%xmm8
1638 movaps
96+48(%rsp),%xmm9
1639 movaps
96+64(%rsp),%xmm10
1640 movaps
96+80(%rsp),%xmm11
1641 movaps
96+96(%rsp),%xmm12
1642 movaps
96+112(%rsp),%xmm13
1643 movaps
96+128(%rsp),%xmm14
1644 movaps
96+144(%rsp),%xmm15
1647 lea
`104+($win64?10*16:0)`(%rsp),%rsi
1657 .size aesni256_cbc_sha1_dec_avx
,.-aesni256_cbc_sha1_dec_avx
1664 .long
0x5a827999,0x5a827999,0x5a827999,0x5a827999 # K_00_19
1665 .long
0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 # K_20_39
1666 .long
0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc # K_40_59
1667 .long
0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6 # K_60_79
1668 .long
0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f # pbswap mask
1669 .byte
0xf,0xe,0xd,0xc,0xb,0xa,0x9,0x8,0x7,0x6,0x5,0x4,0x3,0x2,0x1,0x0
1671 .asciz
"AESNI-CBC+SHA1 stitch for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
1675 ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
1679 ($iv,$in,$rndkey0)=map("%xmm$_",(2,14,15));
1680 @rndkey=("%xmm0","%xmm1");
1683 my ($BSWAP,$ABCD,$E,$E_,$ABCD_SAVE,$E_SAVE)=map("%xmm$_",(7..12));
1684 my @MSG=map("%xmm$_",(3..6));
1687 .type aesni_cbc_sha1_enc_shaext
,\
@function,6
1689 aesni_cbc_sha1_enc_shaext
:
1690 mov
`($win64?56:8)`(%rsp),$inp # load 7th argument
1692 $code.=<<___
if ($win64);
1693 lea
`-8-10*16`(%rsp),%rsp
1694 movaps
%xmm6,-8-10*16(%rax)
1695 movaps
%xmm7,-8-9*16(%rax)
1696 movaps
%xmm8,-8-8*16(%rax)
1697 movaps
%xmm9,-8-7*16(%rax)
1698 movaps
%xmm10,-8-6*16(%rax)
1699 movaps
%xmm11,-8-5*16(%rax)
1700 movaps
%xmm12,-8-4*16(%rax)
1701 movaps
%xmm13,-8-3*16(%rax)
1702 movaps
%xmm14,-8-2*16(%rax)
1703 movaps
%xmm15,-8-1*16(%rax)
1709 movdqa K_XX_XX
+0x50(%rip),$BSWAP # byte-n-word swap
1711 mov
240($key),$rounds
1713 movups
($key),$rndkey0 # $key[0]
1714 movups
16($key),$rndkey[0] # forward reference
1715 lea
112($key),$key # size optimization
1717 pshufd \
$0b00011011,$ABCD,$ABCD # flip word order
1718 pshufd \
$0b00011011,$E,$E # flip word order
1726 movdqu
($inp),@MSG[0]
1727 movdqa
$E,$E_SAVE # offload $E
1728 pshufb
$BSWAP,@MSG[0]
1729 movdqu
0x10($inp),@MSG[1]
1730 movdqa
$ABCD,$ABCD_SAVE # offload $ABCD
1734 pshufb
$BSWAP,@MSG[1]
1737 movdqu
0x20($inp),@MSG[2]
1739 pxor
$E_SAVE,@MSG[0] # black magic
1743 pxor
$E_SAVE,@MSG[0] # black magic
1745 pshufb
$BSWAP,@MSG[2]
1746 sha1rnds4 \
$0,$E,$ABCD # 0-3
1747 sha1nexte
@MSG[1],$E_
1751 sha1msg1
@MSG[1],@MSG[0]
1752 movdqu
-0x10($inp),@MSG[3]
1754 pshufb
$BSWAP,@MSG[3]
1758 sha1rnds4 \
$0,$E_,$ABCD # 4-7
1759 sha1nexte
@MSG[2],$E
1760 pxor
@MSG[2],@MSG[0]
1761 sha1msg1
@MSG[2],@MSG[1]
1765 for($i=2;$i<20-4;$i++) {
1768 sha1rnds4 \
$`int($i/5)`,$E,$ABCD # 8-11
1769 sha1nexte
@MSG[3],$E_
1773 sha1msg2
@MSG[3],@MSG[0]
1774 pxor
@MSG[3],@MSG[1]
1775 sha1msg1
@MSG[3],@MSG[2]
1778 push(@MSG,shift(@MSG));
1784 sha1rnds4 \
$3,$E,$ABCD # 64-67
1785 sha1nexte
@MSG[3],$E_
1786 sha1msg2
@MSG[3],@MSG[0]
1787 pxor
@MSG[3],@MSG[1]
1792 sha1rnds4 \
$3,$E_,$ABCD # 68-71
1793 sha1nexte
@MSG[0],$E
1794 sha1msg2
@MSG[0],@MSG[1]
1798 movdqa
$E_SAVE,@MSG[0]
1800 sha1rnds4 \
$3,$E,$ABCD # 72-75
1801 sha1nexte
@MSG[1],$E_
1806 sha1rnds4 \
$3,$E_,$ABCD # 76-79
1807 sha1nexte
$MSG[0],$E
1809 while($r<40) { &$aesenc(); } # remaining aesenc's
1813 paddd
$ABCD_SAVE,$ABCD
1814 movups
$iv,48($out,$in0) # write output
1818 pshufd \
$0b00011011,$ABCD,$ABCD
1819 pshufd \
$0b00011011,$E,$E
1820 movups
$iv,($ivp) # write IV
1824 $code.=<<___
if ($win64);
1825 movaps
-8-10*16(%rax),%xmm6
1826 movaps
-8-9*16(%rax),%xmm7
1827 movaps
-8-8*16(%rax),%xmm8
1828 movaps
-8-7*16(%rax),%xmm9
1829 movaps
-8-6*16(%rax),%xmm10
1830 movaps
-8-5*16(%rax),%xmm11
1831 movaps
-8-4*16(%rax),%xmm12
1832 movaps
-8-3*16(%rax),%xmm13
1833 movaps
-8-2*16(%rax),%xmm14
1834 movaps
-8-1*16(%rax),%xmm15
1840 .size aesni_cbc_sha1_enc_shaext
,.-aesni_cbc_sha1_enc_shaext
1843 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
1844 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
1852 .extern __imp_RtlVirtualUnwind
1853 .type ssse3_handler
,\
@abi-omnipotent
1867 mov
120($context),%rax # pull context->Rax
1868 mov
248($context),%rbx # pull context->Rip
1870 mov
8($disp),%rsi # disp->ImageBase
1871 mov
56($disp),%r11 # disp->HandlerData
1873 mov
0(%r11),%r10d # HandlerData[0]
1874 lea
(%rsi,%r10),%r10 # prologue label
1875 cmp %r10,%rbx # context->Rip<prologue label
1876 jb
.Lcommon_seh_tail
1878 mov
152($context),%rax # pull context->Rsp
1880 mov
4(%r11),%r10d # HandlerData[1]
1881 lea
(%rsi,%r10),%r10 # epilogue label
1882 cmp %r10,%rbx # context->Rip>=epilogue label
1883 jae
.Lcommon_seh_tail
1885 $code.=<<___
if ($shaext);
1886 lea aesni_cbc_sha1_enc_shaext
(%rip),%r10
1891 lea
512($context),%rdi # &context.Xmm6
1893 .long
0xa548f3fc # cld; rep movsq
1894 lea
168(%rax),%rax # adjust stack pointer
1895 jmp
.Lcommon_seh_tail
1900 lea
512($context),%rdi # &context.Xmm6
1902 .long
0xa548f3fc # cld; rep movsq
1903 lea
`104+10*16`(%rax),%rax # adjust stack pointer
1912 mov
%rbx,144($context) # restore context->Rbx
1913 mov
%rbp,160($context) # restore context->Rbp
1914 mov
%r12,216($context) # restore context->R12
1915 mov
%r13,224($context) # restore context->R13
1916 mov
%r14,232($context) # restore context->R14
1917 mov
%r15,240($context) # restore context->R15
1922 mov
%rax,152($context) # restore context->Rsp
1923 mov
%rsi,168($context) # restore context->Rsi
1924 mov
%rdi,176($context) # restore context->Rdi
1926 mov
40($disp),%rdi # disp->ContextRecord
1927 mov
$context,%rsi # context
1928 mov \
$154,%ecx # sizeof(CONTEXT)
1929 .long
0xa548f3fc # cld; rep movsq
1932 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
1933 mov
8(%rsi),%rdx # arg2, disp->ImageBase
1934 mov
0(%rsi),%r8 # arg3, disp->ControlPc
1935 mov
16(%rsi),%r9 # arg4, disp->FunctionEntry
1936 mov
40(%rsi),%r10 # disp->ContextRecord
1937 lea
56(%rsi),%r11 # &disp->HandlerData
1938 lea
24(%rsi),%r12 # &disp->EstablisherFrame
1939 mov
%r10,32(%rsp) # arg5
1940 mov
%r11,40(%rsp) # arg6
1941 mov
%r12,48(%rsp) # arg7
1942 mov
%rcx,56(%rsp) # arg8, (NULL)
1943 call
*__imp_RtlVirtualUnwind
(%rip)
1945 mov \
$1,%eax # ExceptionContinueSearch
1957 .size ssse3_handler
,.-ssse3_handler
1961 .rva
.LSEH_begin_aesni_cbc_sha1_enc_ssse3
1962 .rva
.LSEH_end_aesni_cbc_sha1_enc_ssse3
1963 .rva
.LSEH_info_aesni_cbc_sha1_enc_ssse3
1965 $code.=<<___
if ($avx);
1966 .rva
.LSEH_begin_aesni_cbc_sha1_enc_avx
1967 .rva
.LSEH_end_aesni_cbc_sha1_enc_avx
1968 .rva
.LSEH_info_aesni_cbc_sha1_enc_avx
1970 $code.=<<___
if ($shaext);
1971 .rva
.LSEH_begin_aesni_cbc_sha1_enc_shaext
1972 .rva
.LSEH_end_aesni_cbc_sha1_enc_shaext
1973 .rva
.LSEH_info_aesni_cbc_sha1_enc_shaext
1978 .LSEH_info_aesni_cbc_sha1_enc_ssse3
:
1981 .rva
.Lprologue_ssse3
,.Lepilogue_ssse3
# HandlerData[]
1983 $code.=<<___
if ($avx);
1984 .LSEH_info_aesni_cbc_sha1_enc_avx
:
1987 .rva
.Lprologue_avx
,.Lepilogue_avx
# HandlerData[]
1989 $code.=<<___
if ($shaext);
1990 .LSEH_info_aesni_cbc_sha1_enc_shaext
:
1993 .rva
.Lprologue_shaext
,.Lepilogue_shaext
# HandlerData[]
1997 ####################################################################
1999 local *opcode
=shift;
2003 $rex|=0x04 if($dst>=8);
2004 $rex|=0x01 if($src>=8);
2005 unshift @opcode,$rex|0x40 if($rex);
2009 if (@_[0] =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
2010 my @opcode=(0x0f,0x3a,0xcc);
2011 rex
(\
@opcode,$3,$2);
2012 push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
2014 push @opcode,$c=~/^0/?
oct($c):$c;
2015 return ".byte\t".join(',',@opcode);
2017 return "sha1rnds4\t".@_[0];
2024 "sha1nexte" => 0xc8,
2026 "sha1msg2" => 0xca );
2028 if (defined($opcodelet{$instr}) && @_[0] =~ /%xmm([0-9]+),\s*%xmm([0-9]+)/) {
2029 my @opcode=(0x0f,0x38);
2030 rex
(\
@opcode,$2,$1);
2031 push @opcode,$opcodelet{$instr};
2032 push @opcode,0xc0|($1&7)|(($2&7)<<3); # ModR/M
2033 return ".byte\t".join(',',@opcode);
2035 return $instr."\t".@_[0];
2041 my @opcode=(0x0f,0x38);
2043 if ($line=~/(aes[a-z]+)\s+%xmm([0-9]+),\s*%xmm([0-9]+)/) {
2045 "aesenc" => 0xdc, "aesenclast" => 0xdd,
2046 "aesdec" => 0xde, "aesdeclast" => 0xdf
2048 return undef if (!defined($opcodelet{$1}));
2049 rex
(\
@opcode,$3,$2);
2050 push @opcode,$opcodelet{$1},0xc0|($2&7)|(($3&7)<<3); # ModR/M
2051 unshift @opcode,0x66;
2052 return ".byte\t".join(',',@opcode);
2057 foreach (split("\n",$code)) {
2058 s/\`([^\`]*)\`/eval $1/geo;
2060 s/\b(sha1rnds4)\s+(.*)/sha1rnds4($2)/geo or
2061 s/\b(sha1[^\s]*)\s+(.*)/sha1op38($1,$2)/geo or
2062 s/\b(aes.*%xmm[0-9]+).*$/aesni($1)/geo;