3 # ====================================================================
4 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
5 # project. The module is, however, dual licensed under OpenSSL and
6 # CRYPTOGAMS licenses depending on where you obtain it. For further
7 # details see http://www.openssl.org/~appro/cryptogams/.
8 # ====================================================================
12 # This is AESNI-CBC+SHA1 "stitch" implementation. The idea, as spelled
13 # in http://download.intel.com/design/intarch/papers/323686.pdf, is
14 # that since AESNI-CBC encrypt exhibit *very* low instruction-level
15 # parallelism, interleaving it with another algorithm would allow to
16 # utilize processor resources better and achieve better performance.
17 # SHA1 instruction sequences(*) are taken from sha1-x86_64.pl and
18 # AESNI code is weaved into it. Below are performance numbers in
19 # cycles per processed byte, less is better, for standalone AESNI-CBC
20 # encrypt, sum of the latter and standalone SHA1, and "stitched"
23 # AES-128-CBC +SHA1 stitch gain
24 # Westmere 3.77[+5.6] 9.37 6.65 +41%
25 # Sandy Bridge 5.05[+5.2(6.3)] 10.25(11.35) 6.16(7.08) +67%(+60%)
26 # Ivy Bridge 5.05[+4.7] 9.75 5.59 +74%
27 # Bulldozer 5.77[+6.1] 11.87 6.47 +83%
30 # Westmere 4.51 10.11 6.97 +45%
31 # Sandy Bridge 6.05 11.25(12.35) 6.34(7.27) +77%(+70%)
32 # Ivy Bridge 6.05 10.75 6.07 +77%
33 # Bulldozer 6.89 12.99 7.02 +85%
36 # Westmere 5.25 10.85 7.25 +50%
37 # Sandy Bridge 7.05 12.25(13.35) 7.06(7.70) +74%(+73%)
38 # Ivy Bridge 7.05 11.75 7.12 +65%
39 # Bulldozer 8.00 14.10 8.24 +71%
41 # (*) There are two code paths: SSSE3 and AVX. See sha1-568.pl for
42 # background information. Above numbers in parentheses are SSSE3
43 # results collected on AVX-capable CPU, i.e. apply on OSes that
46 # Needless to mention that it makes no sense to implement "stitched"
47 # *decrypt* subroutine. Because *both* AESNI-CBC decrypt and SHA1
48 # fully utilize parallelism, so stitching would not give any gain
49 # anyway. Well, there might be some, e.g. because of better cache
50 # locality... For reference, here are performance results for
51 # standalone AESNI-CBC decrypt:
53 # AES-128-CBC AES-192-CBC AES-256-CBC
54 # Westmere 1.31 1.55 1.80
55 # Sandy Bridge 0.93 1.06 1.22
56 # Ivy Bridge 0.92 1.06 1.21
57 # Bulldozer 0.76 0.90 1.04
61 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
63 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
65 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
66 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
67 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
68 die "can't locate x86_64-xlate.pl";
70 $avx=1 if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
71 =~ /GNU assembler version ([2-9]\.[0-9]+)/ &&
73 $avx=1 if (!$avx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
74 `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/ &&
76 $avx=1 if (!$avx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
77 `ml64 2>&1` =~ /Version ([0-9]+)\./ &&
80 open OUT,"| \"$^X\" $xlate $flavour $output";
83 # void aesni_cbc_sha1_enc(const void *inp,
93 .extern OPENSSL_ia32cap_P
95 .globl aesni_cbc_sha1_enc
96 .type aesni_cbc_sha1_enc,\@abi-omnipotent
99 # caller should check for SSSE3 and AES-NI bits
100 mov OPENSSL_ia32cap_P+0(%rip),%r10d
101 mov OPENSSL_ia32cap_P+4(%rip),%r11d
103 $code.=<<___ if ($avx);
104 and \$`1<<28`,%r11d # mask AVX bit
105 and \$`1<<30`,%r10d # mask "Intel CPU" bit
107 cmp \$`1<<28|1<<30`,%r10d
108 je aesni_cbc_sha1_enc_avx
111 jmp aesni_cbc_sha1_enc_ssse3
113 .size aesni_cbc_sha1_enc,.-aesni_cbc_sha1_enc
116 my ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
119 my @X=map("%xmm$_",(4..7,0..3));
120 my @Tx=map("%xmm$_",(8..10));
121 my @V=($A,$B,$C,$D,$E)=("%eax","%ebx","%ecx","%edx","%ebp"); # size optimization
122 my @T=("%esi","%edi");
123 my $j=0; my $jj=0; my $r=0; my $sn=0;
125 my ($iv,$in,$rndkey0)=map("%xmm$_",(11..13));
126 my @rndkey=("%xmm14","%xmm15");
128 sub AUTOLOAD() # thunk [simplified] 32-bit style perlasm
129 { my $opcode = $AUTOLOAD; $opcode =~ s/.*:://;
131 $arg = "\$$arg" if ($arg*1 eq $arg);
132 $code .= "\t$opcode\t".join(',',$arg,reverse @_)."\n";
135 my $_rol=sub { &rol(@_) };
136 my $_ror=sub { &ror(@_) };
139 .type aesni_cbc_sha1_enc_ssse3,\@function,6
141 aesni_cbc_sha1_enc_ssse3:
142 mov `($win64?56:8)`(%rsp),$inp # load 7th argument
143 #shr \$6,$len # debugging artefact
144 #jz .Lepilogue_ssse3 # debugging artefact
151 lea `-104-($win64?10*16:0)`(%rsp),%rsp
152 #mov $in0,$inp # debugging artefact
153 #lea 64(%rsp),$ctx # debugging artefact
155 $code.=<<___ if ($win64);
156 movaps %xmm6,96+0(%rsp)
157 movaps %xmm7,96+16(%rsp)
158 movaps %xmm8,96+32(%rsp)
159 movaps %xmm9,96+48(%rsp)
160 movaps %xmm10,96+64(%rsp)
161 movaps %xmm11,96+80(%rsp)
162 movaps %xmm12,96+96(%rsp)
163 movaps %xmm13,96+112(%rsp)
164 movaps %xmm14,96+128(%rsp)
165 movaps %xmm15,96+144(%rsp)
169 mov $in0,%r12 # reassign arguments
173 movdqu ($ivp),$iv # load IV
174 mov $ivp,88(%rsp) # save $ivp
176 my ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
177 my $rounds="${ivp}d";
181 mov 240($key),$rounds
182 add $inp,$len # end of input
184 lea K_XX_XX(%rip),$K_XX_XX
185 mov 0($ctx),$A # load context
189 mov $B,@T[0] # magic seed
192 movdqa 64($K_XX_XX),@X[2] # pbswap mask
193 movdqa 0($K_XX_XX),@Tx[1] # K_00_19
194 movdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
195 movdqu 16($inp),@X[-3&7]
196 movdqu 32($inp),@X[-2&7]
197 movdqu 48($inp),@X[-1&7]
198 pshufb @X[2],@X[-4&7] # byte swap
200 pshufb @X[2],@X[-3&7]
201 pshufb @X[2],@X[-2&7]
202 pshufb @X[2],@X[-1&7]
203 paddd @Tx[1],@X[-4&7] # add K_00_19
204 paddd @Tx[1],@X[-3&7]
205 paddd @Tx[1],@X[-2&7]
206 movdqa @X[-4&7],0(%rsp) # X[]+K xfer to IALU
207 psubd @Tx[1],@X[-4&7] # restore X[]
208 movdqa @X[-3&7],16(%rsp)
209 psubd @Tx[1],@X[-3&7]
210 movdqa @X[-2&7],32(%rsp)
211 psubd @Tx[1],@X[-2&7]
212 movups ($key),$rndkey0 # $key[0]
213 movups 16($key),$rndkey[0] # forward reference
219 my ($n,$k)=($r/10,$r%10);
222 movups `16*$n`($in0),$in # load input
225 $code.=<<___ if ($n);
226 movups $iv,`16*($n-1)`($out,$in0) # write output
230 aesenc $rndkey[0],$iv
231 movups `32+16*$k`($key),$rndkey[1]
238 movups `32+16*($k+0)`($key),$rndkey[1]
239 aesenc $rndkey[0],$iv
240 movups `32+16*($k+1)`($key),$rndkey[0]
241 aesenc $rndkey[1],$iv
243 movups `32+16*($k+2)`($key),$rndkey[1]
244 aesenc $rndkey[0],$iv
245 movups `32+16*($k+3)`($key),$rndkey[0]
246 aesenc $rndkey[1],$iv
248 aesenclast $rndkey[0],$iv
249 movups 16($key),$rndkey[1] # forward reference
253 aesenc $rndkey[0],$iv
254 movups `32+16*$k`($key),$rndkey[1]
257 $r++; unshift(@rndkey,pop(@rndkey));
260 sub Xupdate_ssse3_16_31() # recall that $Xi starts wtih 4
263 my @insns = (&$body,&$body,&$body,&$body); # 40 instructions
266 &movdqa (@X[0],@X[-3&7]);
269 &movdqa (@Tx[0],@X[-1&7]);
270 &palignr(@X[0],@X[-4&7],8); # compose "X[-14]" in "X[0]"
274 &paddd (@Tx[1],@X[-1&7]);
277 &psrldq (@Tx[0],4); # "X[-3]", 3 dwords
280 &pxor (@X[0],@X[-4&7]); # "X[0]"^="X[-16]"
284 &pxor (@Tx[0],@X[-2&7]); # "X[-3]"^"X[-8]"
290 &pxor (@X[0],@Tx[0]); # "X[0]"^="X[-3]"^"X[-8]"
293 &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
297 &movdqa (@Tx[2],@X[0]);
298 &movdqa (@Tx[0],@X[0]);
304 &pslldq (@Tx[2],12); # "X[0]"<<96, extract one dword
305 &paddd (@X[0],@X[0]);
314 &movdqa (@Tx[1],@Tx[2]);
319 &por (@X[0],@Tx[0]); # "X[0]"<<<=1
326 &pxor (@X[0],@Tx[2]);
329 &movdqa (@Tx[2],eval(16*(($Xi)/5))."($K_XX_XX)"); # K_XX_XX
333 &pxor (@X[0],@Tx[1]); # "X[0]"^=("X[0]">>96)<<<2
335 foreach (@insns) { eval; } # remaining instructions [if any]
337 $Xi++; push(@X,shift(@X)); # "rotate" X[]
338 push(@Tx,shift(@Tx));
341 sub Xupdate_ssse3_32_79()
344 my @insns = (&$body,&$body,&$body,&$body); # 32 to 48 instructions
347 &movdqa (@Tx[0],@X[-1&7]) if ($Xi==8);
348 eval(shift(@insns)); # body_20_39
349 &pxor (@X[0],@X[-4&7]); # "X[0]"="X[-32]"^"X[-16]"
350 &palignr(@Tx[0],@X[-2&7],8); # compose "X[-6]"
353 eval(shift(@insns)); # rol
355 &pxor (@X[0],@X[-7&7]); # "X[0]"^="X[-28]"
357 eval(shift(@insns)) if (@insns[0] !~ /&ro[rl]/);
359 &movdqa (@Tx[2],@Tx[1]);# "perpetuate" K_XX_XX...
360 } else { # ... or load next one
361 &movdqa (@Tx[2],eval(16*($Xi/5))."($K_XX_XX)");
363 &paddd (@Tx[1],@X[-1&7]);
364 eval(shift(@insns)); # ror
367 &pxor (@X[0],@Tx[0]); # "X[0]"^="X[-6]"
368 eval(shift(@insns)); # body_20_39
371 eval(shift(@insns)); # rol
373 &movdqa (@Tx[0],@X[0]);
374 &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
377 eval(shift(@insns)); # ror
381 eval(shift(@insns)); # body_20_39
385 eval(shift(@insns)); # rol
388 eval(shift(@insns)); # ror
391 &por (@X[0],@Tx[0]); # "X[0]"<<<=2
392 eval(shift(@insns)); # body_20_39
394 &movdqa (@Tx[1],@X[0]) if ($Xi<19);
396 eval(shift(@insns)); # rol
399 eval(shift(@insns)); # rol
402 foreach (@insns) { eval; } # remaining instructions
404 $Xi++; push(@X,shift(@X)); # "rotate" X[]
405 push(@Tx,shift(@Tx));
408 sub Xuplast_ssse3_80()
411 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
415 &paddd (@Tx[1],@X[-1&7]);
421 &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer IALU
423 foreach (@insns) { eval; } # remaining instructions
426 &je (".Ldone_ssse3");
428 unshift(@Tx,pop(@Tx));
430 &movdqa (@X[2],"64($K_XX_XX)"); # pbswap mask
431 &movdqa (@Tx[1],"0($K_XX_XX)"); # K_00_19
432 &movdqu (@X[-4&7],"0($inp)"); # load input
433 &movdqu (@X[-3&7],"16($inp)");
434 &movdqu (@X[-2&7],"32($inp)");
435 &movdqu (@X[-1&7],"48($inp)");
436 &pshufb (@X[-4&7],@X[2]); # byte swap
445 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
450 &pshufb (@X[($Xi-3)&7],@X[2]);
453 &paddd (@X[($Xi-4)&7],@Tx[1]);
458 &movdqa (eval(16*$Xi)."(%rsp)",@X[($Xi-4)&7]); # X[]+K xfer to IALU
461 &psubd (@X[($Xi-4)&7],@Tx[1]);
463 foreach (@insns) { eval; }
470 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
473 foreach (@insns) { eval; }
480 '($a,$b,$c,$d,$e)=@V;'.
481 '&add ($e,eval(4*($j&15))."(%rsp)");', # X[]+K xfer
483 '&mov (@T[1],$a);', # $b in next round
485 '&and (@T[0],$c);', # ($b&($c^$d))
486 '&xor ($c,$d);', # restore $c
489 '&$_ror ($b,$j?7:2);', # $b>>>2
490 '&add ($e,@T[0]);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
493 $k = (($jj+1)*12/20)*20*$n/12; # 12 aesencs per these 20 rounds
494 @r[$k%$n].='&$aesenc();' if ($jj==$k/$n);
503 '($a,$b,$c,$d,$e)=@V;'.
504 '&add ($e,eval(4*($j++&15))."(%rsp)");', # X[]+K xfer
505 '&xor (@T[0],$d);', # ($b^$d)
506 '&mov (@T[1],$a);', # $b in next round
508 '&xor (@T[0],$c);', # ($b^$d^$c)
510 '&$_ror ($b,7);', # $b>>>2
511 '&add ($e,@T[0]);' .'unshift(@V,pop(@V)); unshift(@T,pop(@T));'
514 $k = (($jj+1)*8/20)*20*$n/8; # 8 aesencs per these 20 rounds
515 @r[$k%$n].='&$aesenc();' if ($jj==$k/$n);
524 '($a,$b,$c,$d,$e)=@V;'.
527 '&add ($e,eval(4*($j++&15))."(%rsp)");', # X[]+K xfer
529 '&and (@T[0],$c);', # ($b&($c^$d))
530 '&$_ror ($b,7);', # $b>>>2
532 '&mov (@T[1],$a);', # $b in next round
535 '&xor ($c,$d);', # restore $c
536 '&add ($e,$a);' .'unshift(@V,pop(@V)); unshift(@T,pop(@T));'
539 $k=(($jj+1)*12/20)*20*$n/12; # 12 aesencs per these 20 rounds
540 @r[$k%$n].='&$aesenc();' if ($jj==$k/$n);
548 &Xupdate_ssse3_16_31(\&body_00_19);
549 &Xupdate_ssse3_16_31(\&body_00_19);
550 &Xupdate_ssse3_16_31(\&body_00_19);
551 &Xupdate_ssse3_16_31(\&body_00_19);
552 &Xupdate_ssse3_32_79(\&body_00_19);
553 &Xupdate_ssse3_32_79(\&body_20_39);
554 &Xupdate_ssse3_32_79(\&body_20_39);
555 &Xupdate_ssse3_32_79(\&body_20_39);
556 &Xupdate_ssse3_32_79(\&body_20_39);
557 &Xupdate_ssse3_32_79(\&body_20_39);
558 &Xupdate_ssse3_32_79(\&body_40_59);
559 &Xupdate_ssse3_32_79(\&body_40_59);
560 &Xupdate_ssse3_32_79(\&body_40_59);
561 &Xupdate_ssse3_32_79(\&body_40_59);
562 &Xupdate_ssse3_32_79(\&body_40_59);
563 &Xupdate_ssse3_32_79(\&body_20_39);
564 &Xuplast_ssse3_80(\&body_20_39); # can jump to "done"
566 $saved_j=$j; @saved_V=@V;
567 $saved_r=$r; @saved_rndkey=@rndkey;
569 &Xloop_ssse3(\&body_20_39);
570 &Xloop_ssse3(\&body_20_39);
571 &Xloop_ssse3(\&body_20_39);
574 movups $iv,48($out,$in0) # write output
577 add 0($ctx),$A # update context
584 mov @T[0],$B # magic seed
593 $jj=$j=$saved_j; @V=@saved_V;
594 $r=$saved_r; @rndkey=@saved_rndkey;
596 &Xtail_ssse3(\&body_20_39);
597 &Xtail_ssse3(\&body_20_39);
598 &Xtail_ssse3(\&body_20_39);
601 movups $iv,48($out,$in0) # write output
602 mov 88(%rsp),$ivp # restore $ivp
604 add 0($ctx),$A # update context
614 movups $iv,($ivp) # write IV
616 $code.=<<___ if ($win64);
617 movaps 96+0(%rsp),%xmm6
618 movaps 96+16(%rsp),%xmm7
619 movaps 96+32(%rsp),%xmm8
620 movaps 96+48(%rsp),%xmm9
621 movaps 96+64(%rsp),%xmm10
622 movaps 96+80(%rsp),%xmm11
623 movaps 96+96(%rsp),%xmm12
624 movaps 96+112(%rsp),%xmm13
625 movaps 96+128(%rsp),%xmm14
626 movaps 96+144(%rsp),%xmm15
629 lea `104+($win64?10*16:0)`(%rsp),%rsi
639 .size aesni_cbc_sha1_enc_ssse3,.-aesni_cbc_sha1_enc_ssse3
645 my ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
648 my @X=map("%xmm$_",(4..7,0..3));
649 my @Tx=map("%xmm$_",(8..10));
650 my @V=($A,$B,$C,$D,$E)=("%eax","%ebx","%ecx","%edx","%ebp"); # size optimization
651 my @T=("%esi","%edi");
653 my $_rol=sub { &shld(@_[0],@_) };
654 my $_ror=sub { &shrd(@_[0],@_) };
657 .type aesni_cbc_sha1_enc_avx,\@function,6
659 aesni_cbc_sha1_enc_avx:
660 mov `($win64?56:8)`(%rsp),$inp # load 7th argument
661 #shr \$6,$len # debugging artefact
662 #jz .Lepilogue_avx # debugging artefact
669 lea `-104-($win64?10*16:0)`(%rsp),%rsp
670 #mov $in0,$inp # debugging artefact
671 #lea 64(%rsp),$ctx # debugging artefact
673 $code.=<<___ if ($win64);
674 movaps %xmm6,96+0(%rsp)
675 movaps %xmm7,96+16(%rsp)
676 movaps %xmm8,96+32(%rsp)
677 movaps %xmm9,96+48(%rsp)
678 movaps %xmm10,96+64(%rsp)
679 movaps %xmm11,96+80(%rsp)
680 movaps %xmm12,96+96(%rsp)
681 movaps %xmm13,96+112(%rsp)
682 movaps %xmm14,96+128(%rsp)
683 movaps %xmm15,96+144(%rsp)
688 mov $in0,%r12 # reassign arguments
692 vmovdqu ($ivp),$iv # load IV
693 mov $ivp,88(%rsp) # save $ivp
695 my ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
696 my $rounds="${ivp}d";
700 mov 240($key),$rounds
701 add \$112,$key # size optimization
702 add $inp,$len # end of input
704 lea K_XX_XX(%rip),$K_XX_XX
705 mov 0($ctx),$A # load context
709 mov $B,@T[0] # magic seed
712 vmovdqa 64($K_XX_XX),@X[2] # pbswap mask
713 vmovdqa 0($K_XX_XX),@Tx[1] # K_00_19
714 vmovdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
715 vmovdqu 16($inp),@X[-3&7]
716 vmovdqu 32($inp),@X[-2&7]
717 vmovdqu 48($inp),@X[-1&7]
718 vpshufb @X[2],@X[-4&7],@X[-4&7] # byte swap
720 vpshufb @X[2],@X[-3&7],@X[-3&7]
721 vpshufb @X[2],@X[-2&7],@X[-2&7]
722 vpshufb @X[2],@X[-1&7],@X[-1&7]
723 vpaddd @Tx[1],@X[-4&7],@X[0] # add K_00_19
724 vpaddd @Tx[1],@X[-3&7],@X[1]
725 vpaddd @Tx[1],@X[-2&7],@X[2]
726 vmovdqa @X[0],0(%rsp) # X[]+K xfer to IALU
727 vmovdqa @X[1],16(%rsp)
728 vmovdqa @X[2],32(%rsp)
729 vmovups -112($key),$rndkey0 # $key[0]
730 vmovups 16-112($key),$rndkey[0] # forward reference
736 my ($n,$k)=($r/10,$r%10);
739 vmovups `16*$n`($in0),$in # load input
740 vxorps $rndkey0,$in,$in
742 $code.=<<___ if ($n);
743 vmovups $iv,`16*($n-1)`($out,$in0) # write output
747 vaesenc $rndkey[0],$iv,$iv
748 vmovups `32+16*$k-112`($key),$rndkey[1]
755 vaesenc $rndkey[0],$iv,$iv
756 vmovups `32+16*($k+0)-112`($key),$rndkey[1]
757 vaesenc $rndkey[1],$iv,$iv
758 vmovups `32+16*($k+1)-112`($key),$rndkey[0]
760 vaesenc $rndkey[0],$iv,$iv
761 vmovups `32+16*($k+2)-112`($key),$rndkey[1]
762 vaesenc $rndkey[1],$iv,$iv
763 vmovups `32+16*($k+3)-112`($key),$rndkey[0]
765 vaesenclast $rndkey[0],$iv,$iv
766 vmovups 16-112($key),$rndkey[1] # forward reference
770 vaesenc $rndkey[0],$iv,$iv
771 vmovups `32+16*$k-112`($key),$rndkey[1]
774 $r++; unshift(@rndkey,pop(@rndkey));
777 sub Xupdate_avx_16_31() # recall that $Xi starts wtih 4
780 my @insns = (&$body,&$body,&$body,&$body); # 40 instructions
785 &vpalignr(@X[0],@X[-3&7],@X[-4&7],8); # compose "X[-14]" in "X[0]"
789 &vpaddd (@Tx[1],@Tx[1],@X[-1&7]);
792 &vpsrldq(@Tx[0],@X[-1&7],4); # "X[-3]", 3 dwords
795 &vpxor (@X[0],@X[0],@X[-4&7]); # "X[0]"^="X[-16]"
799 &vpxor (@Tx[0],@Tx[0],@X[-2&7]); # "X[-3]"^"X[-8]"
805 &vpxor (@X[0],@X[0],@Tx[0]); # "X[0]"^="X[-3]"^"X[-8]"
808 &vmovdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
812 &vpsrld (@Tx[0],@X[0],31);
818 &vpslldq(@Tx[2],@X[0],12); # "X[0]"<<96, extract one dword
819 &vpaddd (@X[0],@X[0],@X[0]);
825 &vpsrld (@Tx[1],@Tx[2],30);
826 &vpor (@X[0],@X[0],@Tx[0]); # "X[0]"<<<=1
832 &vpslld (@Tx[2],@Tx[2],2);
833 &vpxor (@X[0],@X[0],@Tx[1]);
839 &vpxor (@X[0],@X[0],@Tx[2]); # "X[0]"^=("X[0]">>96)<<<2
842 &vmovdqa (@Tx[2],eval(16*(($Xi)/5))."($K_XX_XX)"); # K_XX_XX
847 foreach (@insns) { eval; } # remaining instructions [if any]
849 $Xi++; push(@X,shift(@X)); # "rotate" X[]
850 push(@Tx,shift(@Tx));
853 sub Xupdate_avx_32_79()
856 my @insns = (&$body,&$body,&$body,&$body); # 32 to 48 instructions
859 &vpalignr(@Tx[0],@X[-1&7],@X[-2&7],8); # compose "X[-6]"
860 &vpxor (@X[0],@X[0],@X[-4&7]); # "X[0]"="X[-32]"^"X[-16]"
861 eval(shift(@insns)); # body_20_39
864 eval(shift(@insns)); # rol
866 &vpxor (@X[0],@X[0],@X[-7&7]); # "X[0]"^="X[-28]"
868 eval(shift(@insns)) if (@insns[0] !~ /&ro[rl]/);
870 &vmovdqa (@Tx[2],@Tx[1]);# "perpetuate" K_XX_XX...
871 } else { # ... or load next one
872 &vmovdqa (@Tx[2],eval(16*($Xi/5))."($K_XX_XX)");
874 &vpaddd (@Tx[1],@Tx[1],@X[-1&7]);
875 eval(shift(@insns)); # ror
878 &vpxor (@X[0],@X[0],@Tx[0]); # "X[0]"^="X[-6]"
879 eval(shift(@insns)); # body_20_39
882 eval(shift(@insns)); # rol
884 &vpsrld (@Tx[0],@X[0],30);
885 &vmovdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
888 eval(shift(@insns)); # ror
891 &vpslld (@X[0],@X[0],2);
892 eval(shift(@insns)); # body_20_39
895 eval(shift(@insns)); # rol
898 eval(shift(@insns)); # ror
901 &vpor (@X[0],@X[0],@Tx[0]); # "X[0]"<<<=2
902 eval(shift(@insns)); # body_20_39
904 &vmovdqa (@Tx[1],@X[0]) if ($Xi<19);
906 eval(shift(@insns)); # rol
909 eval(shift(@insns)); # rol
912 foreach (@insns) { eval; } # remaining instructions
914 $Xi++; push(@X,shift(@X)); # "rotate" X[]
915 push(@Tx,shift(@Tx));
921 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
925 &vpaddd (@Tx[1],@Tx[1],@X[-1&7]);
931 &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer IALU
933 foreach (@insns) { eval; } # remaining instructions
938 unshift(@Tx,pop(@Tx));
940 &vmovdqa(@X[2],"64($K_XX_XX)"); # pbswap mask
941 &vmovdqa(@Tx[1],"0($K_XX_XX)"); # K_00_19
942 &vmovdqu(@X[-4&7],"0($inp)"); # load input
943 &vmovdqu(@X[-3&7],"16($inp)");
944 &vmovdqu(@X[-2&7],"32($inp)");
945 &vmovdqu(@X[-1&7],"48($inp)");
946 &vpshufb(@X[-4&7],@X[-4&7],@X[2]); # byte swap
955 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
960 &vpshufb(@X[($Xi-3)&7],@X[($Xi-3)&7],@X[2]);
963 &vpaddd (@X[$Xi&7],@X[($Xi-4)&7],@Tx[1]);
968 &vmovdqa(eval(16*$Xi)."(%rsp)",@X[$Xi&7]); # X[]+K xfer to IALU
972 foreach (@insns) { eval; }
979 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
982 foreach (@insns) { eval; }
989 &Xupdate_avx_16_31(\&body_00_19);
990 &Xupdate_avx_16_31(\&body_00_19);
991 &Xupdate_avx_16_31(\&body_00_19);
992 &Xupdate_avx_16_31(\&body_00_19);
993 &Xupdate_avx_32_79(\&body_00_19);
994 &Xupdate_avx_32_79(\&body_20_39);
995 &Xupdate_avx_32_79(\&body_20_39);
996 &Xupdate_avx_32_79(\&body_20_39);
997 &Xupdate_avx_32_79(\&body_20_39);
998 &Xupdate_avx_32_79(\&body_20_39);
999 &Xupdate_avx_32_79(\&body_40_59);
1000 &Xupdate_avx_32_79(\&body_40_59);
1001 &Xupdate_avx_32_79(\&body_40_59);
1002 &Xupdate_avx_32_79(\&body_40_59);
1003 &Xupdate_avx_32_79(\&body_40_59);
1004 &Xupdate_avx_32_79(\&body_20_39);
1005 &Xuplast_avx_80(\&body_20_39); # can jump to "done"
1007 $saved_j=$j; @saved_V=@V;
1008 $saved_r=$r; @saved_rndkey=@rndkey;
1010 &Xloop_avx(\&body_20_39);
1011 &Xloop_avx(\&body_20_39);
1012 &Xloop_avx(\&body_20_39);
1015 vmovups $iv,48($out,$in0) # write output
1018 add 0($ctx),$A # update context
1025 mov @T[0],$B # magic seed
1034 $jj=$j=$saved_j; @V=@saved_V;
1035 $r=$saved_r; @rndkey=@saved_rndkey;
1037 &Xtail_avx(\&body_20_39);
1038 &Xtail_avx(\&body_20_39);
1039 &Xtail_avx(\&body_20_39);
1042 vmovups $iv,48($out,$in0) # write output
1043 mov 88(%rsp),$ivp # restore $ivp
1045 add 0($ctx),$A # update context
1055 vmovups $iv,($ivp) # write IV
1058 $code.=<<___ if ($win64);
1059 movaps 96+0(%rsp),%xmm6
1060 movaps 96+16(%rsp),%xmm7
1061 movaps 96+32(%rsp),%xmm8
1062 movaps 96+48(%rsp),%xmm9
1063 movaps 96+64(%rsp),%xmm10
1064 movaps 96+80(%rsp),%xmm11
1065 movaps 96+96(%rsp),%xmm12
1066 movaps 96+112(%rsp),%xmm13
1067 movaps 96+128(%rsp),%xmm14
1068 movaps 96+144(%rsp),%xmm15
1071 lea `104+($win64?10*16:0)`(%rsp),%rsi
1081 .size aesni_cbc_sha1_enc_avx,.-aesni_cbc_sha1_enc_avx
1087 .long 0x5a827999,0x5a827999,0x5a827999,0x5a827999 # K_00_19
1088 .long 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 # K_20_39
1089 .long 0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc # K_40_59
1090 .long 0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6 # K_60_79
1091 .long 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f # pbswap mask
1093 .asciz "AESNI-CBC+SHA1 stitch for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
1097 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
1098 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
1106 .extern __imp_RtlVirtualUnwind
1107 .type ssse3_handler,\@abi-omnipotent
1121 mov 120($context),%rax # pull context->Rax
1122 mov 248($context),%rbx # pull context->Rip
1124 mov 8($disp),%rsi # disp->ImageBase
1125 mov 56($disp),%r11 # disp->HandlerData
1127 mov 0(%r11),%r10d # HandlerData[0]
1128 lea (%rsi,%r10),%r10 # prologue label
1129 cmp %r10,%rbx # context->Rip<prologue label
1130 jb .Lcommon_seh_tail
1132 mov 152($context),%rax # pull context->Rsp
1134 mov 4(%r11),%r10d # HandlerData[1]
1135 lea (%rsi,%r10),%r10 # epilogue label
1136 cmp %r10,%rbx # context->Rip>=epilogue label
1137 jae .Lcommon_seh_tail
1140 lea 512($context),%rdi # &context.Xmm6
1142 .long 0xa548f3fc # cld; rep movsq
1143 lea `104+10*16`(%rax),%rax # adjust stack pointer
1152 mov %rbx,144($context) # restore context->Rbx
1153 mov %rbp,160($context) # restore context->Rbp
1154 mov %r12,216($context) # restore context->R12
1155 mov %r13,224($context) # restore context->R13
1156 mov %r14,232($context) # restore context->R14
1157 mov %r15,240($context) # restore context->R15
1162 mov %rax,152($context) # restore context->Rsp
1163 mov %rsi,168($context) # restore context->Rsi
1164 mov %rdi,176($context) # restore context->Rdi
1166 mov 40($disp),%rdi # disp->ContextRecord
1167 mov $context,%rsi # context
1168 mov \$154,%ecx # sizeof(CONTEXT)
1169 .long 0xa548f3fc # cld; rep movsq
1172 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
1173 mov 8(%rsi),%rdx # arg2, disp->ImageBase
1174 mov 0(%rsi),%r8 # arg3, disp->ControlPc
1175 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
1176 mov 40(%rsi),%r10 # disp->ContextRecord
1177 lea 56(%rsi),%r11 # &disp->HandlerData
1178 lea 24(%rsi),%r12 # &disp->EstablisherFrame
1179 mov %r10,32(%rsp) # arg5
1180 mov %r11,40(%rsp) # arg6
1181 mov %r12,48(%rsp) # arg7
1182 mov %rcx,56(%rsp) # arg8, (NULL)
1183 call *__imp_RtlVirtualUnwind(%rip)
1185 mov \$1,%eax # ExceptionContinueSearch
1197 .size ssse3_handler,.-ssse3_handler
1201 .rva .LSEH_begin_aesni_cbc_sha1_enc_ssse3
1202 .rva .LSEH_end_aesni_cbc_sha1_enc_ssse3
1203 .rva .LSEH_info_aesni_cbc_sha1_enc_ssse3
1205 $code.=<<___ if ($avx);
1206 .rva .LSEH_begin_aesni_cbc_sha1_enc_avx
1207 .rva .LSEH_end_aesni_cbc_sha1_enc_avx
1208 .rva .LSEH_info_aesni_cbc_sha1_enc_avx
1213 .LSEH_info_aesni_cbc_sha1_enc_ssse3:
1216 .rva .Lprologue_ssse3,.Lepilogue_ssse3 # HandlerData[]
1218 $code.=<<___ if ($avx);
1219 .LSEH_info_aesni_cbc_sha1_enc_avx:
1222 .rva .Lprologue_avx,.Lepilogue_avx # HandlerData[]
1226 ####################################################################
1228 local *opcode=shift;
1232 $rex|=0x04 if($dst>=8);
1233 $rex|=0x01 if($src>=8);
1234 push @opcode,$rex|0x40 if($rex);
1241 if ($line=~/(aes[a-z]+)\s+%xmm([0-9]+),\s*%xmm([0-9]+)/) {
1243 "aesenc" => 0xdc, "aesenclast" => 0xdd
1245 return undef if (!defined($opcodelet{$1}));
1246 rex(\@opcode,$3,$2);
1247 push @opcode,0x0f,0x38,$opcodelet{$1};
1248 push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
1249 return ".byte\t".join(',',@opcode);
1254 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
1255 $code =~ s/\b(aes.*%xmm[0-9]+).*$/aesni($1)/gem;