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%)
28 # Westmere 4.51 10.11 6.97 +45%
29 # Sandy Bridge 6.05 11.25(12.35) 6.34(7.27) +77%(+70%)
32 # Westmere 5.25 10.85 7.25 +50%
33 # Sandy Bridge 7.05 12.25(13.35) 7.06(7.70) +74%(+73%)
35 # (*) There are two code paths: SSSE3 and AVX. See sha1-568.pl for
36 # background information. Above numbers in parentheses are SSSE3
37 # results collected on AVX-capable CPU, i.e. apply on OSes that
40 # Needless to mention that it makes no sense to implement "stitched"
41 # *decrypt* subroutine. Because *both* AESNI-CBC decrypt and SHA1
42 # fully utilize parallelism, so stitching would not give any gain
43 # anyway. Well, there might be some, e.g. because of better cache
44 # locality... For reference, here are performance results for
45 # standalone AESNI-CBC decrypt:
47 # AES-128-CBC AES-192-CBC AES-256-CBC
48 # Westmere 1.31 1.55 1.80
49 # Sandy Bridge 0.93 1.06 1.22
53 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
55 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
57 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
58 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
59 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
60 die "can't locate x86_64-xlate.pl";
62 $avx=1 if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
63 =~ /GNU assembler version ([2-9]\.[0-9]+)/ &&
65 $avx=1 if (!$avx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
66 `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/ &&
68 $avx=1 if (!$avx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
69 `ml64 2>&1` =~ /Version ([0-9]+)\./ &&
72 open OUT,"| \"$^X\" $xlate $flavour $output";
75 # void aesni_cbc_sha1_enc(const void *inp,
85 .extern OPENSSL_ia32cap_P
87 .globl aesni_cbc_sha1_enc
88 .type aesni_cbc_sha1_enc,\@abi-omnipotent
91 # caller should check for SSSE3 and AES-NI bits
92 mov OPENSSL_ia32cap_P+0(%rip),%r10d
93 mov OPENSSL_ia32cap_P+4(%rip),%r11d
95 $code.=<<___ if ($avx);
96 and \$`1<<28`,%r11d # mask AVX bit
97 and \$`1<<30`,%r10d # mask "Intel CPU" bit
99 cmp \$`1<<28|1<<30`,%r10d
100 je aesni_cbc_sha1_enc_avx
103 jmp aesni_cbc_sha1_enc_ssse3
105 .size aesni_cbc_sha1_enc,.-aesni_cbc_sha1_enc
108 my ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
111 my @X=map("%xmm$_",(4..7,0..3));
112 my @Tx=map("%xmm$_",(8..10));
113 my @V=($A,$B,$C,$D,$E)=("%eax","%ebx","%ecx","%edx","%ebp"); # size optimization
114 my @T=("%esi","%edi");
115 my $j=0; my $jj=0; my $r=0; my $sn=0;
117 my ($iv,$in,$rndkey0)=map("%xmm$_",(11..13));
118 my @rndkey=("%xmm14","%xmm15");
120 sub AUTOLOAD() # thunk [simplified] 32-bit style perlasm
121 { my $opcode = $AUTOLOAD; $opcode =~ s/.*:://;
123 $arg = "\$$arg" if ($arg*1 eq $arg);
124 $code .= "\t$opcode\t".join(',',$arg,reverse @_)."\n";
127 my $_rol=sub { &rol(@_) };
128 my $_ror=sub { &ror(@_) };
131 .type aesni_cbc_sha1_enc_ssse3,\@function,6
133 aesni_cbc_sha1_enc_ssse3:
134 mov `($win64?56:8)`(%rsp),$inp # load 7th argument
135 #shr \$6,$len # debugging artefact
136 #jz .Lepilogue_ssse3 # debugging artefact
143 lea `-104-($win64?10*16:0)`(%rsp),%rsp
144 #mov $in0,$inp # debugging artefact
145 #lea 64(%rsp),$ctx # debugging artefact
147 $code.=<<___ if ($win64);
148 movaps %xmm6,96+0(%rsp)
149 movaps %xmm7,96+16(%rsp)
150 movaps %xmm8,96+32(%rsp)
151 movaps %xmm9,96+48(%rsp)
152 movaps %xmm10,96+64(%rsp)
153 movaps %xmm11,96+80(%rsp)
154 movaps %xmm12,96+96(%rsp)
155 movaps %xmm13,96+112(%rsp)
156 movaps %xmm14,96+128(%rsp)
157 movaps %xmm15,96+144(%rsp)
161 mov $in0,%r12 # reassign arguments
165 movdqu ($ivp),$iv # load IV
166 mov $ivp,88(%rsp) # save $ivp
168 my ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
169 my $rounds="${ivp}d";
173 mov 240($key),$rounds
174 add $inp,$len # end of input
176 lea K_XX_XX(%rip),$K_XX_XX
177 mov 0($ctx),$A # load context
181 mov $B,@T[0] # magic seed
184 movdqa 64($K_XX_XX),@X[2] # pbswap mask
185 movdqa 0($K_XX_XX),@Tx[1] # K_00_19
186 movdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
187 movdqu 16($inp),@X[-3&7]
188 movdqu 32($inp),@X[-2&7]
189 movdqu 48($inp),@X[-1&7]
190 pshufb @X[2],@X[-4&7] # byte swap
192 pshufb @X[2],@X[-3&7]
193 pshufb @X[2],@X[-2&7]
194 pshufb @X[2],@X[-1&7]
195 paddd @Tx[1],@X[-4&7] # add K_00_19
196 paddd @Tx[1],@X[-3&7]
197 paddd @Tx[1],@X[-2&7]
198 movdqa @X[-4&7],0(%rsp) # X[]+K xfer to IALU
199 psubd @Tx[1],@X[-4&7] # restore X[]
200 movdqa @X[-3&7],16(%rsp)
201 psubd @Tx[1],@X[-3&7]
202 movdqa @X[-2&7],32(%rsp)
203 psubd @Tx[1],@X[-2&7]
204 movups ($key),$rndkey0 # $key[0]
205 movups 16($key),$rndkey[0] # forward reference
211 my ($n,$k)=($r/10,$r%10);
214 movups `16*$n`($in0),$in # load input
217 $code.=<<___ if ($n);
218 movups $iv,`16*($n-1)`($out,$in0) # write output
222 aesenc $rndkey[0],$iv
223 movups `32+16*$k`($key),$rndkey[1]
230 movups `32+16*($k+0)`($key),$rndkey[1]
231 aesenc $rndkey[0],$iv
232 movups `32+16*($k+1)`($key),$rndkey[0]
233 aesenc $rndkey[1],$iv
235 movups `32+16*($k+2)`($key),$rndkey[1]
236 aesenc $rndkey[0],$iv
237 movups `32+16*($k+3)`($key),$rndkey[0]
238 aesenc $rndkey[1],$iv
240 aesenclast $rndkey[0],$iv
241 movups 16($key),$rndkey[1] # forward reference
245 aesenc $rndkey[0],$iv
246 movups `32+16*$k`($key),$rndkey[1]
249 $r++; unshift(@rndkey,pop(@rndkey));
252 sub Xupdate_ssse3_16_31() # recall that $Xi starts wtih 4
255 my @insns = (&$body,&$body,&$body,&$body); # 40 instructions
258 &movdqa (@X[0],@X[-3&7]);
261 &movdqa (@Tx[0],@X[-1&7]);
262 &palignr(@X[0],@X[-4&7],8); # compose "X[-14]" in "X[0]"
266 &paddd (@Tx[1],@X[-1&7]);
269 &psrldq (@Tx[0],4); # "X[-3]", 3 dwords
272 &pxor (@X[0],@X[-4&7]); # "X[0]"^="X[-16]"
276 &pxor (@Tx[0],@X[-2&7]); # "X[-3]"^"X[-8]"
282 &pxor (@X[0],@Tx[0]); # "X[0]"^="X[-3]"^"X[-8]"
285 &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
289 &movdqa (@Tx[2],@X[0]);
290 &movdqa (@Tx[0],@X[0]);
296 &pslldq (@Tx[2],12); # "X[0]"<<96, extract one dword
297 &paddd (@X[0],@X[0]);
306 &movdqa (@Tx[1],@Tx[2]);
311 &por (@X[0],@Tx[0]); # "X[0]"<<<=1
318 &pxor (@X[0],@Tx[2]);
321 &movdqa (@Tx[2],eval(16*(($Xi)/5))."($K_XX_XX)"); # K_XX_XX
325 &pxor (@X[0],@Tx[1]); # "X[0]"^=("X[0]">>96)<<<2
327 foreach (@insns) { eval; } # remaining instructions [if any]
329 $Xi++; push(@X,shift(@X)); # "rotate" X[]
330 push(@Tx,shift(@Tx));
333 sub Xupdate_ssse3_32_79()
336 my @insns = (&$body,&$body,&$body,&$body); # 32 to 48 instructions
339 &movdqa (@Tx[0],@X[-1&7]) if ($Xi==8);
340 eval(shift(@insns)); # body_20_39
341 &pxor (@X[0],@X[-4&7]); # "X[0]"="X[-32]"^"X[-16]"
342 &palignr(@Tx[0],@X[-2&7],8); # compose "X[-6]"
345 eval(shift(@insns)); # rol
347 &pxor (@X[0],@X[-7&7]); # "X[0]"^="X[-28]"
349 eval(shift(@insns)) if (@insns[0] !~ /&ro[rl]/);
351 &movdqa (@Tx[2],@Tx[1]);# "perpetuate" K_XX_XX...
352 } else { # ... or load next one
353 &movdqa (@Tx[2],eval(16*($Xi/5))."($K_XX_XX)");
355 &paddd (@Tx[1],@X[-1&7]);
356 eval(shift(@insns)); # ror
359 &pxor (@X[0],@Tx[0]); # "X[0]"^="X[-6]"
360 eval(shift(@insns)); # body_20_39
363 eval(shift(@insns)); # rol
365 &movdqa (@Tx[0],@X[0]);
366 &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
369 eval(shift(@insns)); # ror
373 eval(shift(@insns)); # body_20_39
377 eval(shift(@insns)); # rol
380 eval(shift(@insns)); # ror
383 &por (@X[0],@Tx[0]); # "X[0]"<<<=2
384 eval(shift(@insns)); # body_20_39
386 &movdqa (@Tx[1],@X[0]) if ($Xi<19);
388 eval(shift(@insns)); # rol
391 eval(shift(@insns)); # rol
394 foreach (@insns) { eval; } # remaining instructions
396 $Xi++; push(@X,shift(@X)); # "rotate" X[]
397 push(@Tx,shift(@Tx));
400 sub Xuplast_ssse3_80()
403 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
407 &paddd (@Tx[1],@X[-1&7]);
413 &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer IALU
415 foreach (@insns) { eval; } # remaining instructions
418 &je (".Ldone_ssse3");
420 unshift(@Tx,pop(@Tx));
422 &movdqa (@X[2],"64($K_XX_XX)"); # pbswap mask
423 &movdqa (@Tx[1],"0($K_XX_XX)"); # K_00_19
424 &movdqu (@X[-4&7],"0($inp)"); # load input
425 &movdqu (@X[-3&7],"16($inp)");
426 &movdqu (@X[-2&7],"32($inp)");
427 &movdqu (@X[-1&7],"48($inp)");
428 &pshufb (@X[-4&7],@X[2]); # byte swap
437 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
442 &pshufb (@X[($Xi-3)&7],@X[2]);
445 &paddd (@X[($Xi-4)&7],@Tx[1]);
450 &movdqa (eval(16*$Xi)."(%rsp)",@X[($Xi-4)&7]); # X[]+K xfer to IALU
453 &psubd (@X[($Xi-4)&7],@Tx[1]);
455 foreach (@insns) { eval; }
462 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
465 foreach (@insns) { eval; }
472 '($a,$b,$c,$d,$e)=@V;'.
473 '&add ($e,eval(4*($j&15))."(%rsp)");', # X[]+K xfer
475 '&mov (@T[1],$a);', # $b in next round
477 '&and (@T[0],$c);', # ($b&($c^$d))
478 '&xor ($c,$d);', # restore $c
481 '&$_ror ($b,$j?7:2);', # $b>>>2
482 '&add ($e,@T[0]);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
485 $k = (($jj+1)*12/20)*20*$n/12; # 12 aesencs per these 20 rounds
486 @r[$k%$n].='&$aesenc();' if ($jj==$k/$n);
495 '($a,$b,$c,$d,$e)=@V;'.
496 '&add ($e,eval(4*($j++&15))."(%rsp)");', # X[]+K xfer
497 '&xor (@T[0],$d);', # ($b^$d)
498 '&mov (@T[1],$a);', # $b in next round
500 '&xor (@T[0],$c);', # ($b^$d^$c)
502 '&$_ror ($b,7);', # $b>>>2
503 '&add ($e,@T[0]);' .'unshift(@V,pop(@V)); unshift(@T,pop(@T));'
506 $k = (($jj+1)*8/20)*20*$n/8; # 8 aesencs per these 20 rounds
507 @r[$k%$n].='&$aesenc();' if ($jj==$k/$n);
516 '($a,$b,$c,$d,$e)=@V;'.
519 '&add ($e,eval(4*($j++&15))."(%rsp)");', # X[]+K xfer
521 '&and (@T[0],$c);', # ($b&($c^$d))
522 '&$_ror ($b,7);', # $b>>>2
524 '&mov (@T[1],$a);', # $b in next round
527 '&xor ($c,$d);', # restore $c
528 '&add ($e,$a);' .'unshift(@V,pop(@V)); unshift(@T,pop(@T));'
531 $k=(($jj+1)*12/20)*20*$n/12; # 12 aesencs per these 20 rounds
532 @r[$k%$n].='&$aesenc();' if ($jj==$k/$n);
540 &Xupdate_ssse3_16_31(\&body_00_19);
541 &Xupdate_ssse3_16_31(\&body_00_19);
542 &Xupdate_ssse3_16_31(\&body_00_19);
543 &Xupdate_ssse3_16_31(\&body_00_19);
544 &Xupdate_ssse3_32_79(\&body_00_19);
545 &Xupdate_ssse3_32_79(\&body_20_39);
546 &Xupdate_ssse3_32_79(\&body_20_39);
547 &Xupdate_ssse3_32_79(\&body_20_39);
548 &Xupdate_ssse3_32_79(\&body_20_39);
549 &Xupdate_ssse3_32_79(\&body_20_39);
550 &Xupdate_ssse3_32_79(\&body_40_59);
551 &Xupdate_ssse3_32_79(\&body_40_59);
552 &Xupdate_ssse3_32_79(\&body_40_59);
553 &Xupdate_ssse3_32_79(\&body_40_59);
554 &Xupdate_ssse3_32_79(\&body_40_59);
555 &Xupdate_ssse3_32_79(\&body_20_39);
556 &Xuplast_ssse3_80(\&body_20_39); # can jump to "done"
558 $saved_j=$j; @saved_V=@V;
559 $saved_r=$r; @saved_rndkey=@rndkey;
561 &Xloop_ssse3(\&body_20_39);
562 &Xloop_ssse3(\&body_20_39);
563 &Xloop_ssse3(\&body_20_39);
566 movups $iv,48($out,$in0) # write output
569 add 0($ctx),$A # update context
576 mov @T[0],$B # magic seed
585 $jj=$j=$saved_j; @V=@saved_V;
586 $r=$saved_r; @rndkey=@saved_rndkey;
588 &Xtail_ssse3(\&body_20_39);
589 &Xtail_ssse3(\&body_20_39);
590 &Xtail_ssse3(\&body_20_39);
593 movups $iv,48($out,$in0) # write output
594 mov 88(%rsp),$ivp # restore $ivp
596 add 0($ctx),$A # update context
606 movups $iv,($ivp) # write IV
608 $code.=<<___ if ($win64);
609 movaps 96+0(%rsp),%xmm6
610 movaps 96+16(%rsp),%xmm7
611 movaps 96+32(%rsp),%xmm8
612 movaps 96+48(%rsp),%xmm9
613 movaps 96+64(%rsp),%xmm10
614 movaps 96+80(%rsp),%xmm11
615 movaps 96+96(%rsp),%xmm12
616 movaps 96+112(%rsp),%xmm13
617 movaps 96+128(%rsp),%xmm14
618 movaps 96+144(%rsp),%xmm15
621 lea `104+($win64?10*16:0)`(%rsp),%rsi
631 .size aesni_cbc_sha1_enc_ssse3,.-aesni_cbc_sha1_enc_ssse3
637 my ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
640 my @X=map("%xmm$_",(4..7,0..3));
641 my @Tx=map("%xmm$_",(8..10));
642 my @V=($A,$B,$C,$D,$E)=("%eax","%ebx","%ecx","%edx","%ebp"); # size optimization
643 my @T=("%esi","%edi");
645 my $_rol=sub { &shld(@_[0],@_) };
646 my $_ror=sub { &shrd(@_[0],@_) };
649 .type aesni_cbc_sha1_enc_avx,\@function,6
651 aesni_cbc_sha1_enc_avx:
652 mov `($win64?56:8)`(%rsp),$inp # load 7th argument
653 #shr \$6,$len # debugging artefact
654 #jz .Lepilogue_avx # debugging artefact
661 lea `-104-($win64?10*16:0)`(%rsp),%rsp
662 #mov $in0,$inp # debugging artefact
663 #lea 64(%rsp),$ctx # debugging artefact
665 $code.=<<___ if ($win64);
666 movaps %xmm6,96+0(%rsp)
667 movaps %xmm7,96+16(%rsp)
668 movaps %xmm8,96+32(%rsp)
669 movaps %xmm9,96+48(%rsp)
670 movaps %xmm10,96+64(%rsp)
671 movaps %xmm11,96+80(%rsp)
672 movaps %xmm12,96+96(%rsp)
673 movaps %xmm13,96+112(%rsp)
674 movaps %xmm14,96+128(%rsp)
675 movaps %xmm15,96+144(%rsp)
680 mov $in0,%r12 # reassign arguments
684 vmovdqu ($ivp),$iv # load IV
685 mov $ivp,88(%rsp) # save $ivp
687 my ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
688 my $rounds="${ivp}d";
692 mov 240($key),$rounds
693 add \$112,$key # size optimization
694 add $inp,$len # end of input
696 lea K_XX_XX(%rip),$K_XX_XX
697 mov 0($ctx),$A # load context
701 mov $B,@T[0] # magic seed
704 vmovdqa 64($K_XX_XX),@X[2] # pbswap mask
705 vmovdqa 0($K_XX_XX),@Tx[1] # K_00_19
706 vmovdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
707 vmovdqu 16($inp),@X[-3&7]
708 vmovdqu 32($inp),@X[-2&7]
709 vmovdqu 48($inp),@X[-1&7]
710 vpshufb @X[2],@X[-4&7],@X[-4&7] # byte swap
712 vpshufb @X[2],@X[-3&7],@X[-3&7]
713 vpshufb @X[2],@X[-2&7],@X[-2&7]
714 vpshufb @X[2],@X[-1&7],@X[-1&7]
715 vpaddd @Tx[1],@X[-4&7],@X[0] # add K_00_19
716 vpaddd @Tx[1],@X[-3&7],@X[1]
717 vpaddd @Tx[1],@X[-2&7],@X[2]
718 vmovdqa @X[0],0(%rsp) # X[]+K xfer to IALU
719 vmovdqa @X[1],16(%rsp)
720 vmovdqa @X[2],32(%rsp)
721 vmovups -112($key),$rndkey0 # $key[0]
722 vmovups 16-112($key),$rndkey[0] # forward reference
728 my ($n,$k)=($r/10,$r%10);
731 vmovups `16*$n`($in0),$in # load input
732 vxorps $rndkey0,$in,$in
734 $code.=<<___ if ($n);
735 vmovups $iv,`16*($n-1)`($out,$in0) # write output
739 vaesenc $rndkey[0],$iv,$iv
740 vmovups `32+16*$k-112`($key),$rndkey[1]
747 vaesenc $rndkey[0],$iv,$iv
748 vmovups `32+16*($k+0)-112`($key),$rndkey[1]
749 vaesenc $rndkey[1],$iv,$iv
750 vmovups `32+16*($k+1)-112`($key),$rndkey[0]
752 vaesenc $rndkey[0],$iv,$iv
753 vmovups `32+16*($k+2)-112`($key),$rndkey[1]
754 vaesenc $rndkey[1],$iv,$iv
755 vmovups `32+16*($k+3)-112`($key),$rndkey[0]
757 vaesenclast $rndkey[0],$iv,$iv
758 vmovups 16-112($key),$rndkey[1] # forward reference
762 vaesenc $rndkey[0],$iv,$iv
763 vmovups `32+16*$k-112`($key),$rndkey[1]
766 $r++; unshift(@rndkey,pop(@rndkey));
769 sub Xupdate_avx_16_31() # recall that $Xi starts wtih 4
772 my @insns = (&$body,&$body,&$body,&$body); # 40 instructions
777 &vpalignr(@X[0],@X[-3&7],@X[-4&7],8); # compose "X[-14]" in "X[0]"
781 &vpaddd (@Tx[1],@Tx[1],@X[-1&7]);
784 &vpsrldq(@Tx[0],@X[-1&7],4); # "X[-3]", 3 dwords
787 &vpxor (@X[0],@X[0],@X[-4&7]); # "X[0]"^="X[-16]"
791 &vpxor (@Tx[0],@Tx[0],@X[-2&7]); # "X[-3]"^"X[-8]"
797 &vpxor (@X[0],@X[0],@Tx[0]); # "X[0]"^="X[-3]"^"X[-8]"
800 &vmovdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
804 &vpsrld (@Tx[0],@X[0],31);
810 &vpslldq(@Tx[2],@X[0],12); # "X[0]"<<96, extract one dword
811 &vpaddd (@X[0],@X[0],@X[0]);
817 &vpsrld (@Tx[1],@Tx[2],30);
818 &vpor (@X[0],@X[0],@Tx[0]); # "X[0]"<<<=1
824 &vpslld (@Tx[2],@Tx[2],2);
825 &vpxor (@X[0],@X[0],@Tx[1]);
831 &vpxor (@X[0],@X[0],@Tx[2]); # "X[0]"^=("X[0]">>96)<<<2
834 &vmovdqa (@Tx[2],eval(16*(($Xi)/5))."($K_XX_XX)"); # K_XX_XX
839 foreach (@insns) { eval; } # remaining instructions [if any]
841 $Xi++; push(@X,shift(@X)); # "rotate" X[]
842 push(@Tx,shift(@Tx));
845 sub Xupdate_avx_32_79()
848 my @insns = (&$body,&$body,&$body,&$body); # 32 to 48 instructions
851 &vpalignr(@Tx[0],@X[-1&7],@X[-2&7],8); # compose "X[-6]"
852 &vpxor (@X[0],@X[0],@X[-4&7]); # "X[0]"="X[-32]"^"X[-16]"
853 eval(shift(@insns)); # body_20_39
856 eval(shift(@insns)); # rol
858 &vpxor (@X[0],@X[0],@X[-7&7]); # "X[0]"^="X[-28]"
860 eval(shift(@insns)) if (@insns[0] !~ /&ro[rl]/);
862 &vmovdqa (@Tx[2],@Tx[1]);# "perpetuate" K_XX_XX...
863 } else { # ... or load next one
864 &vmovdqa (@Tx[2],eval(16*($Xi/5))."($K_XX_XX)");
866 &vpaddd (@Tx[1],@Tx[1],@X[-1&7]);
867 eval(shift(@insns)); # ror
870 &vpxor (@X[0],@X[0],@Tx[0]); # "X[0]"^="X[-6]"
871 eval(shift(@insns)); # body_20_39
874 eval(shift(@insns)); # rol
876 &vpsrld (@Tx[0],@X[0],30);
877 &vmovdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
880 eval(shift(@insns)); # ror
883 &vpslld (@X[0],@X[0],2);
884 eval(shift(@insns)); # body_20_39
887 eval(shift(@insns)); # rol
890 eval(shift(@insns)); # ror
893 &vpor (@X[0],@X[0],@Tx[0]); # "X[0]"<<<=2
894 eval(shift(@insns)); # body_20_39
896 &vmovdqa (@Tx[1],@X[0]) if ($Xi<19);
898 eval(shift(@insns)); # rol
901 eval(shift(@insns)); # rol
904 foreach (@insns) { eval; } # remaining instructions
906 $Xi++; push(@X,shift(@X)); # "rotate" X[]
907 push(@Tx,shift(@Tx));
913 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
917 &vpaddd (@Tx[1],@Tx[1],@X[-1&7]);
923 &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer IALU
925 foreach (@insns) { eval; } # remaining instructions
930 unshift(@Tx,pop(@Tx));
932 &vmovdqa(@X[2],"64($K_XX_XX)"); # pbswap mask
933 &vmovdqa(@Tx[1],"0($K_XX_XX)"); # K_00_19
934 &vmovdqu(@X[-4&7],"0($inp)"); # load input
935 &vmovdqu(@X[-3&7],"16($inp)");
936 &vmovdqu(@X[-2&7],"32($inp)");
937 &vmovdqu(@X[-1&7],"48($inp)");
938 &vpshufb(@X[-4&7],@X[-4&7],@X[2]); # byte swap
947 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
952 &vpshufb(@X[($Xi-3)&7],@X[($Xi-3)&7],@X[2]);
955 &vpaddd (@X[$Xi&7],@X[($Xi-4)&7],@Tx[1]);
960 &vmovdqa(eval(16*$Xi)."(%rsp)",@X[$Xi&7]); # X[]+K xfer to IALU
964 foreach (@insns) { eval; }
971 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
974 foreach (@insns) { eval; }
981 &Xupdate_avx_16_31(\&body_00_19);
982 &Xupdate_avx_16_31(\&body_00_19);
983 &Xupdate_avx_16_31(\&body_00_19);
984 &Xupdate_avx_16_31(\&body_00_19);
985 &Xupdate_avx_32_79(\&body_00_19);
986 &Xupdate_avx_32_79(\&body_20_39);
987 &Xupdate_avx_32_79(\&body_20_39);
988 &Xupdate_avx_32_79(\&body_20_39);
989 &Xupdate_avx_32_79(\&body_20_39);
990 &Xupdate_avx_32_79(\&body_20_39);
991 &Xupdate_avx_32_79(\&body_40_59);
992 &Xupdate_avx_32_79(\&body_40_59);
993 &Xupdate_avx_32_79(\&body_40_59);
994 &Xupdate_avx_32_79(\&body_40_59);
995 &Xupdate_avx_32_79(\&body_40_59);
996 &Xupdate_avx_32_79(\&body_20_39);
997 &Xuplast_avx_80(\&body_20_39); # can jump to "done"
999 $saved_j=$j; @saved_V=@V;
1000 $saved_r=$r; @saved_rndkey=@rndkey;
1002 &Xloop_avx(\&body_20_39);
1003 &Xloop_avx(\&body_20_39);
1004 &Xloop_avx(\&body_20_39);
1007 vmovups $iv,48($out,$in0) # write output
1010 add 0($ctx),$A # update context
1017 mov @T[0],$B # magic seed
1026 $jj=$j=$saved_j; @V=@saved_V;
1027 $r=$saved_r; @rndkey=@saved_rndkey;
1029 &Xtail_avx(\&body_20_39);
1030 &Xtail_avx(\&body_20_39);
1031 &Xtail_avx(\&body_20_39);
1034 vmovups $iv,48($out,$in0) # write output
1035 mov 88(%rsp),$ivp # restore $ivp
1037 add 0($ctx),$A # update context
1047 vmovups $iv,($ivp) # write IV
1050 $code.=<<___ if ($win64);
1051 movaps 96+0(%rsp),%xmm6
1052 movaps 96+16(%rsp),%xmm7
1053 movaps 96+32(%rsp),%xmm8
1054 movaps 96+48(%rsp),%xmm9
1055 movaps 96+64(%rsp),%xmm10
1056 movaps 96+80(%rsp),%xmm11
1057 movaps 96+96(%rsp),%xmm12
1058 movaps 96+112(%rsp),%xmm13
1059 movaps 96+128(%rsp),%xmm14
1060 movaps 96+144(%rsp),%xmm15
1063 lea `104+($win64?10*16:0)`(%rsp),%rsi
1073 .size aesni_cbc_sha1_enc_avx,.-aesni_cbc_sha1_enc_avx
1079 .long 0x5a827999,0x5a827999,0x5a827999,0x5a827999 # K_00_19
1080 .long 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 # K_20_39
1081 .long 0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc # K_40_59
1082 .long 0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6 # K_60_79
1083 .long 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f # pbswap mask
1085 .asciz "AESNI-CBC+SHA1 stitch for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
1089 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
1090 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
1098 .extern __imp_RtlVirtualUnwind
1099 .type ssse3_handler,\@abi-omnipotent
1113 mov 120($context),%rax # pull context->Rax
1114 mov 248($context),%rbx # pull context->Rip
1116 mov 8($disp),%rsi # disp->ImageBase
1117 mov 56($disp),%r11 # disp->HandlerData
1119 mov 0(%r11),%r10d # HandlerData[0]
1120 lea (%rsi,%r10),%r10 # prologue label
1121 cmp %r10,%rbx # context->Rip<prologue label
1122 jb .Lcommon_seh_tail
1124 mov 152($context),%rax # pull context->Rsp
1126 mov 4(%r11),%r10d # HandlerData[1]
1127 lea (%rsi,%r10),%r10 # epilogue label
1128 cmp %r10,%rbx # context->Rip>=epilogue label
1129 jae .Lcommon_seh_tail
1132 lea 512($context),%rdi # &context.Xmm6
1134 .long 0xa548f3fc # cld; rep movsq
1135 lea `104+10*16`(%rax),%rax # adjust stack pointer
1144 mov %rbx,144($context) # restore context->Rbx
1145 mov %rbp,160($context) # restore context->Rbp
1146 mov %r12,216($context) # restore context->R12
1147 mov %r13,224($context) # restore context->R13
1148 mov %r14,232($context) # restore context->R14
1149 mov %r15,240($context) # restore context->R15
1154 mov %rax,152($context) # restore context->Rsp
1155 mov %rsi,168($context) # restore context->Rsi
1156 mov %rdi,176($context) # restore context->Rdi
1158 mov 40($disp),%rdi # disp->ContextRecord
1159 mov $context,%rsi # context
1160 mov \$154,%ecx # sizeof(CONTEXT)
1161 .long 0xa548f3fc # cld; rep movsq
1164 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
1165 mov 8(%rsi),%rdx # arg2, disp->ImageBase
1166 mov 0(%rsi),%r8 # arg3, disp->ControlPc
1167 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
1168 mov 40(%rsi),%r10 # disp->ContextRecord
1169 lea 56(%rsi),%r11 # &disp->HandlerData
1170 lea 24(%rsi),%r12 # &disp->EstablisherFrame
1171 mov %r10,32(%rsp) # arg5
1172 mov %r11,40(%rsp) # arg6
1173 mov %r12,48(%rsp) # arg7
1174 mov %rcx,56(%rsp) # arg8, (NULL)
1175 call *__imp_RtlVirtualUnwind(%rip)
1177 mov \$1,%eax # ExceptionContinueSearch
1189 .size ssse3_handler,.-ssse3_handler
1193 .rva .LSEH_begin_aesni_cbc_sha1_enc_ssse3
1194 .rva .LSEH_end_aesni_cbc_sha1_enc_ssse3
1195 .rva .LSEH_info_aesni_cbc_sha1_enc_ssse3
1197 $code.=<<___ if ($avx);
1198 .rva .LSEH_begin_aesni_cbc_sha1_enc_avx
1199 .rva .LSEH_end_aesni_cbc_sha1_enc_avx
1200 .rva .LSEH_info_aesni_cbc_sha1_enc_avx
1205 .LSEH_info_aesni_cbc_sha1_enc_ssse3:
1208 .rva .Lprologue_ssse3,.Lepilogue_ssse3 # HandlerData[]
1210 $code.=<<___ if ($avx);
1211 .LSEH_info_aesni_cbc_sha1_enc_avx:
1214 .rva .Lprologue_avx,.Lepilogue_avx # HandlerData[]
1218 ####################################################################
1220 local *opcode=shift;
1224 $rex|=0x04 if($dst>=8);
1225 $rex|=0x01 if($src>=8);
1226 push @opcode,$rex|0x40 if($rex);
1233 if ($line=~/(aes[a-z]+)\s+%xmm([0-9]+),\s*%xmm([0-9]+)/) {
1235 "aesenc" => 0xdc, "aesenclast" => 0xdd
1237 return undef if (!defined($opcodelet{$1}));
1238 rex(\@opcode,$3,$2);
1239 push @opcode,0x0f,0x38,$opcodelet{$1};
1240 push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
1241 return ".byte\t".join(',',@opcode);
1246 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
1247 $code =~ s/\b(aes.*%xmm[0-9]+).*$/aesni($1)/gem;