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 STDOUT,"| $^X $xlate $flavour $output";
74 # void aesni_cbc_sha1_enc(const void *inp,
84 .extern OPENSSL_ia32cap_P
86 .globl aesni_cbc_sha1_enc
87 .type aesni_cbc_sha1_enc,\@abi-omnipotent
90 # caller should check for SSSE3 and AES-NI bits
91 mov OPENSSL_ia32cap_P+0(%rip),%r10d
92 mov OPENSSL_ia32cap_P+4(%rip),%r11d
94 $code.=<<___ if ($avx);
95 and \$`1<<28`,%r11d # mask AVX bit
96 and \$`1<<30`,%r10d # mask "Intel CPU" bit
98 cmp \$`1<<28|1<<30`,%r10d
99 je aesni_cbc_sha1_enc_avx
102 jmp aesni_cbc_sha1_enc_ssse3
104 .size aesni_cbc_sha1_enc,.-aesni_cbc_sha1_enc
107 my ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
110 my @X=map("%xmm$_",(4..7,0..3));
111 my @Tx=map("%xmm$_",(8..10));
112 my @V=($A,$B,$C,$D,$E)=("%eax","%ebx","%ecx","%edx","%ebp"); # size optimization
113 my @T=("%esi","%edi");
114 my $j=0; my $jj=0; my $r=0; my $sn=0;
116 my ($iv,$in,$rndkey0)=map("%xmm$_",(11..13));
117 my @rndkey=("%xmm14","%xmm15");
119 sub AUTOLOAD() # thunk [simplified] 32-bit style perlasm
120 { my $opcode = $AUTOLOAD; $opcode =~ s/.*:://;
122 $arg = "\$$arg" if ($arg*1 eq $arg);
123 $code .= "\t$opcode\t".join(',',$arg,reverse @_)."\n";
126 my $_rol=sub { &rol(@_) };
127 my $_ror=sub { &ror(@_) };
130 .type aesni_cbc_sha1_enc_ssse3,\@function,6
132 aesni_cbc_sha1_enc_ssse3:
133 mov `($win64?56:8)`(%rsp),$inp # load 7th argument
134 #shr \$6,$len # debugging artefact
135 #jz .Lepilogue_ssse3 # debugging artefact
142 lea `-104-($win64?10*16:0)`(%rsp),%rsp
143 #mov $in0,$inp # debugging artefact
144 #lea 64(%rsp),$ctx # debugging artefact
146 $code.=<<___ if ($win64);
147 movaps %xmm6,96+0(%rsp)
148 movaps %xmm7,96+16(%rsp)
149 movaps %xmm8,96+32(%rsp)
150 movaps %xmm9,96+48(%rsp)
151 movaps %xmm10,96+64(%rsp)
152 movaps %xmm11,96+80(%rsp)
153 movaps %xmm12,96+96(%rsp)
154 movaps %xmm13,96+112(%rsp)
155 movaps %xmm14,96+128(%rsp)
156 movaps %xmm15,96+144(%rsp)
160 mov $in0,%r12 # reassign arguments
164 movdqu ($ivp),$iv # load IV
165 mov $ivp,88(%rsp) # save $ivp
167 my ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
168 my $rounds="${ivp}d";
172 mov 240($key),$rounds
173 add $inp,$len # end of input
175 lea K_XX_XX(%rip),$K_XX_XX
176 mov 0($ctx),$A # load context
180 mov $B,@T[0] # magic seed
183 movdqa 64($K_XX_XX),@X[2] # pbswap mask
184 movdqa 0($K_XX_XX),@Tx[1] # K_00_19
185 movdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
186 movdqu 16($inp),@X[-3&7]
187 movdqu 32($inp),@X[-2&7]
188 movdqu 48($inp),@X[-1&7]
189 pshufb @X[2],@X[-4&7] # byte swap
191 pshufb @X[2],@X[-3&7]
192 pshufb @X[2],@X[-2&7]
193 pshufb @X[2],@X[-1&7]
194 paddd @Tx[1],@X[-4&7] # add K_00_19
195 paddd @Tx[1],@X[-3&7]
196 paddd @Tx[1],@X[-2&7]
197 movdqa @X[-4&7],0(%rsp) # X[]+K xfer to IALU
198 psubd @Tx[1],@X[-4&7] # restore X[]
199 movdqa @X[-3&7],16(%rsp)
200 psubd @Tx[1],@X[-3&7]
201 movdqa @X[-2&7],32(%rsp)
202 psubd @Tx[1],@X[-2&7]
203 movups ($key),$rndkey0 # $key[0]
204 movups 16($key),$rndkey[0] # forward reference
210 my ($n,$k)=($r/10,$r%10);
213 movups `16*$n`($in0),$in # load input
216 $code.=<<___ if ($n);
217 movups $iv,`16*($n-1)`($out,$in0) # write output
221 aesenc $rndkey[0],$iv
222 movups `32+16*$k`($key),$rndkey[1]
229 movups `32+16*($k+0)`($key),$rndkey[1]
230 aesenc $rndkey[0],$iv
231 movups `32+16*($k+1)`($key),$rndkey[0]
232 aesenc $rndkey[1],$iv
234 movups `32+16*($k+2)`($key),$rndkey[1]
235 aesenc $rndkey[0],$iv
236 movups `32+16*($k+3)`($key),$rndkey[0]
237 aesenc $rndkey[1],$iv
239 aesenclast $rndkey[0],$iv
240 movups 16($key),$rndkey[1] # forward reference
244 aesenc $rndkey[0],$iv
245 movups `32+16*$k`($key),$rndkey[1]
248 $r++; unshift(@rndkey,pop(@rndkey));
251 sub Xupdate_ssse3_16_31() # recall that $Xi starts wtih 4
254 my @insns = (&$body,&$body,&$body,&$body); # 40 instructions
257 &movdqa (@X[0],@X[-3&7]);
260 &movdqa (@Tx[0],@X[-1&7]);
261 &palignr(@X[0],@X[-4&7],8); # compose "X[-14]" in "X[0]"
265 &paddd (@Tx[1],@X[-1&7]);
268 &psrldq (@Tx[0],4); # "X[-3]", 3 dwords
271 &pxor (@X[0],@X[-4&7]); # "X[0]"^="X[-16]"
275 &pxor (@Tx[0],@X[-2&7]); # "X[-3]"^"X[-8]"
281 &pxor (@X[0],@Tx[0]); # "X[0]"^="X[-3]"^"X[-8]"
284 &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
288 &movdqa (@Tx[2],@X[0]);
289 &movdqa (@Tx[0],@X[0]);
295 &pslldq (@Tx[2],12); # "X[0]"<<96, extract one dword
296 &paddd (@X[0],@X[0]);
305 &movdqa (@Tx[1],@Tx[2]);
310 &por (@X[0],@Tx[0]); # "X[0]"<<<=1
317 &pxor (@X[0],@Tx[2]);
320 &movdqa (@Tx[2],eval(16*(($Xi)/5))."($K_XX_XX)"); # K_XX_XX
324 &pxor (@X[0],@Tx[1]); # "X[0]"^=("X[0]">>96)<<<2
326 foreach (@insns) { eval; } # remaining instructions [if any]
328 $Xi++; push(@X,shift(@X)); # "rotate" X[]
329 push(@Tx,shift(@Tx));
332 sub Xupdate_ssse3_32_79()
335 my @insns = (&$body,&$body,&$body,&$body); # 32 to 48 instructions
338 &movdqa (@Tx[0],@X[-1&7]) if ($Xi==8);
339 eval(shift(@insns)); # body_20_39
340 &pxor (@X[0],@X[-4&7]); # "X[0]"="X[-32]"^"X[-16]"
341 &palignr(@Tx[0],@X[-2&7],8); # compose "X[-6]"
344 eval(shift(@insns)); # rol
346 &pxor (@X[0],@X[-7&7]); # "X[0]"^="X[-28]"
348 eval(shift(@insns)) if (@insns[0] !~ /&ro[rl]/);
350 &movdqa (@Tx[2],@Tx[1]);# "perpetuate" K_XX_XX...
351 } else { # ... or load next one
352 &movdqa (@Tx[2],eval(16*($Xi/5))."($K_XX_XX)");
354 &paddd (@Tx[1],@X[-1&7]);
355 eval(shift(@insns)); # ror
358 &pxor (@X[0],@Tx[0]); # "X[0]"^="X[-6]"
359 eval(shift(@insns)); # body_20_39
362 eval(shift(@insns)); # rol
364 &movdqa (@Tx[0],@X[0]);
365 &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
368 eval(shift(@insns)); # ror
372 eval(shift(@insns)); # body_20_39
376 eval(shift(@insns)); # rol
379 eval(shift(@insns)); # ror
382 &por (@X[0],@Tx[0]); # "X[0]"<<<=2
383 eval(shift(@insns)); # body_20_39
385 &movdqa (@Tx[1],@X[0]) if ($Xi<19);
387 eval(shift(@insns)); # rol
390 eval(shift(@insns)); # rol
393 foreach (@insns) { eval; } # remaining instructions
395 $Xi++; push(@X,shift(@X)); # "rotate" X[]
396 push(@Tx,shift(@Tx));
399 sub Xuplast_ssse3_80()
402 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
406 &paddd (@Tx[1],@X[-1&7]);
412 &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer IALU
414 foreach (@insns) { eval; } # remaining instructions
417 &je (".Ldone_ssse3");
419 unshift(@Tx,pop(@Tx));
421 &movdqa (@X[2],"64($K_XX_XX)"); # pbswap mask
422 &movdqa (@Tx[1],"0($K_XX_XX)"); # K_00_19
423 &movdqu (@X[-4&7],"0($inp)"); # load input
424 &movdqu (@X[-3&7],"16($inp)");
425 &movdqu (@X[-2&7],"32($inp)");
426 &movdqu (@X[-1&7],"48($inp)");
427 &pshufb (@X[-4&7],@X[2]); # byte swap
436 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
441 &pshufb (@X[($Xi-3)&7],@X[2]);
444 &paddd (@X[($Xi-4)&7],@Tx[1]);
449 &movdqa (eval(16*$Xi)."(%rsp)",@X[($Xi-4)&7]); # X[]+K xfer to IALU
452 &psubd (@X[($Xi-4)&7],@Tx[1]);
454 foreach (@insns) { eval; }
461 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
464 foreach (@insns) { eval; }
471 '($a,$b,$c,$d,$e)=@V;'.
472 '&add ($e,eval(4*($j&15))."(%rsp)");', # X[]+K xfer
474 '&mov (@T[1],$a);', # $b in next round
476 '&and (@T[0],$c);', # ($b&($c^$d))
477 '&xor ($c,$d);', # restore $c
480 '&$_ror ($b,$j?7:2);', # $b>>>2
481 '&add ($e,@T[0]);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
484 $k = (($jj+1)*12/20)*20*$n/12; # 12 aesencs per these 20 rounds
485 @r[$k%$n].='&$aesenc();' if ($jj==$k/$n);
494 '($a,$b,$c,$d,$e)=@V;'.
495 '&add ($e,eval(4*($j++&15))."(%rsp)");', # X[]+K xfer
496 '&xor (@T[0],$d);', # ($b^$d)
497 '&mov (@T[1],$a);', # $b in next round
499 '&xor (@T[0],$c);', # ($b^$d^$c)
501 '&$_ror ($b,7);', # $b>>>2
502 '&add ($e,@T[0]);' .'unshift(@V,pop(@V)); unshift(@T,pop(@T));'
505 $k = (($jj+1)*8/20)*20*$n/8; # 8 aesencs per these 20 rounds
506 @r[$k%$n].='&$aesenc();' if ($jj==$k/$n);
515 '($a,$b,$c,$d,$e)=@V;'.
518 '&add ($e,eval(4*($j++&15))."(%rsp)");', # X[]+K xfer
520 '&and (@T[0],$c);', # ($b&($c^$d))
521 '&$_ror ($b,7);', # $b>>>2
523 '&mov (@T[1],$a);', # $b in next round
526 '&xor ($c,$d);', # restore $c
527 '&add ($e,$a);' .'unshift(@V,pop(@V)); unshift(@T,pop(@T));'
530 $k=(($jj+1)*12/20)*20*$n/12; # 12 aesencs per these 20 rounds
531 @r[$k%$n].='&$aesenc();' if ($jj==$k/$n);
539 &Xupdate_ssse3_16_31(\&body_00_19);
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_32_79(\&body_00_19);
544 &Xupdate_ssse3_32_79(\&body_20_39);
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_40_59);
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_20_39);
555 &Xuplast_ssse3_80(\&body_20_39); # can jump to "done"
557 $saved_j=$j; @saved_V=@V;
558 $saved_r=$r; @saved_rndkey=@rndkey;
560 &Xloop_ssse3(\&body_20_39);
561 &Xloop_ssse3(\&body_20_39);
562 &Xloop_ssse3(\&body_20_39);
565 movups $iv,48($out,$in0) # write output
568 add 0($ctx),$A # update context
575 mov @T[0],$B # magic seed
584 $jj=$j=$saved_j; @V=@saved_V;
585 $r=$saved_r; @rndkey=@saved_rndkey;
587 &Xtail_ssse3(\&body_20_39);
588 &Xtail_ssse3(\&body_20_39);
589 &Xtail_ssse3(\&body_20_39);
592 movups $iv,48($out,$in0) # write output
593 mov 88(%rsp),$ivp # restore $ivp
595 add 0($ctx),$A # update context
605 movups $iv,($ivp) # write IV
607 $code.=<<___ if ($win64);
608 movaps 96+0(%rsp),%xmm6
609 movaps 96+16(%rsp),%xmm7
610 movaps 96+32(%rsp),%xmm8
611 movaps 96+48(%rsp),%xmm9
612 movaps 96+64(%rsp),%xmm10
613 movaps 96+80(%rsp),%xmm11
614 movaps 96+96(%rsp),%xmm12
615 movaps 96+112(%rsp),%xmm13
616 movaps 96+128(%rsp),%xmm14
617 movaps 96+144(%rsp),%xmm15
620 lea `104+($win64?10*16:0)`(%rsp),%rsi
630 .size aesni_cbc_sha1_enc_ssse3,.-aesni_cbc_sha1_enc_ssse3
636 my ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
639 my @X=map("%xmm$_",(4..7,0..3));
640 my @Tx=map("%xmm$_",(8..10));
641 my @V=($A,$B,$C,$D,$E)=("%eax","%ebx","%ecx","%edx","%ebp"); # size optimization
642 my @T=("%esi","%edi");
644 my $_rol=sub { &shld(@_[0],@_) };
645 my $_ror=sub { &shrd(@_[0],@_) };
648 .type aesni_cbc_sha1_enc_avx,\@function,6
650 aesni_cbc_sha1_enc_avx:
651 mov `($win64?56:8)`(%rsp),$inp # load 7th argument
652 #shr \$6,$len # debugging artefact
653 #jz .Lepilogue_avx # debugging artefact
660 lea `-104-($win64?10*16:0)`(%rsp),%rsp
661 #mov $in0,$inp # debugging artefact
662 #lea 64(%rsp),$ctx # debugging artefact
664 $code.=<<___ if ($win64);
665 movaps %xmm6,96+0(%rsp)
666 movaps %xmm7,96+16(%rsp)
667 movaps %xmm8,96+32(%rsp)
668 movaps %xmm9,96+48(%rsp)
669 movaps %xmm10,96+64(%rsp)
670 movaps %xmm11,96+80(%rsp)
671 movaps %xmm12,96+96(%rsp)
672 movaps %xmm13,96+112(%rsp)
673 movaps %xmm14,96+128(%rsp)
674 movaps %xmm15,96+144(%rsp)
679 mov $in0,%r12 # reassign arguments
683 vmovdqu ($ivp),$iv # load IV
684 mov $ivp,88(%rsp) # save $ivp
686 my ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
687 my $rounds="${ivp}d";
691 mov 240($key),$rounds
692 add \$112,$key # size optimization
693 add $inp,$len # end of input
695 lea K_XX_XX(%rip),$K_XX_XX
696 mov 0($ctx),$A # load context
700 mov $B,@T[0] # magic seed
703 vmovdqa 64($K_XX_XX),@X[2] # pbswap mask
704 vmovdqa 0($K_XX_XX),@Tx[1] # K_00_19
705 vmovdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
706 vmovdqu 16($inp),@X[-3&7]
707 vmovdqu 32($inp),@X[-2&7]
708 vmovdqu 48($inp),@X[-1&7]
709 vpshufb @X[2],@X[-4&7],@X[-4&7] # byte swap
711 vpshufb @X[2],@X[-3&7],@X[-3&7]
712 vpshufb @X[2],@X[-2&7],@X[-2&7]
713 vpshufb @X[2],@X[-1&7],@X[-1&7]
714 vpaddd @Tx[1],@X[-4&7],@X[0] # add K_00_19
715 vpaddd @Tx[1],@X[-3&7],@X[1]
716 vpaddd @Tx[1],@X[-2&7],@X[2]
717 vmovdqa @X[0],0(%rsp) # X[]+K xfer to IALU
718 vmovdqa @X[1],16(%rsp)
719 vmovdqa @X[2],32(%rsp)
720 vmovups -112($key),$rndkey0 # $key[0]
721 vmovups 16-112($key),$rndkey[0] # forward reference
727 my ($n,$k)=($r/10,$r%10);
730 vmovups `16*$n`($in0),$in # load input
731 vxorps $rndkey0,$in,$in
733 $code.=<<___ if ($n);
734 vmovups $iv,`16*($n-1)`($out,$in0) # write output
738 vaesenc $rndkey[0],$iv,$iv
739 vmovups `32+16*$k-112`($key),$rndkey[1]
746 vaesenc $rndkey[0],$iv,$iv
747 vmovups `32+16*($k+0)-112`($key),$rndkey[1]
748 vaesenc $rndkey[1],$iv,$iv
749 vmovups `32+16*($k+1)-112`($key),$rndkey[0]
751 vaesenc $rndkey[0],$iv,$iv
752 vmovups `32+16*($k+2)-112`($key),$rndkey[1]
753 vaesenc $rndkey[1],$iv,$iv
754 vmovups `32+16*($k+3)-112`($key),$rndkey[0]
756 vaesenclast $rndkey[0],$iv,$iv
757 vmovups 16-112($key),$rndkey[1] # forward reference
761 vaesenc $rndkey[0],$iv,$iv
762 vmovups `32+16*$k-112`($key),$rndkey[1]
765 $r++; unshift(@rndkey,pop(@rndkey));
768 sub Xupdate_avx_16_31() # recall that $Xi starts wtih 4
771 my @insns = (&$body,&$body,&$body,&$body); # 40 instructions
776 &vpalignr(@X[0],@X[-3&7],@X[-4&7],8); # compose "X[-14]" in "X[0]"
780 &vpaddd (@Tx[1],@Tx[1],@X[-1&7]);
783 &vpsrldq(@Tx[0],@X[-1&7],4); # "X[-3]", 3 dwords
786 &vpxor (@X[0],@X[0],@X[-4&7]); # "X[0]"^="X[-16]"
790 &vpxor (@Tx[0],@Tx[0],@X[-2&7]); # "X[-3]"^"X[-8]"
796 &vpxor (@X[0],@X[0],@Tx[0]); # "X[0]"^="X[-3]"^"X[-8]"
799 &vmovdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
803 &vpsrld (@Tx[0],@X[0],31);
809 &vpslldq(@Tx[2],@X[0],12); # "X[0]"<<96, extract one dword
810 &vpaddd (@X[0],@X[0],@X[0]);
816 &vpsrld (@Tx[1],@Tx[2],30);
817 &vpor (@X[0],@X[0],@Tx[0]); # "X[0]"<<<=1
823 &vpslld (@Tx[2],@Tx[2],2);
824 &vpxor (@X[0],@X[0],@Tx[1]);
830 &vpxor (@X[0],@X[0],@Tx[2]); # "X[0]"^=("X[0]">>96)<<<2
833 &vmovdqa (@Tx[2],eval(16*(($Xi)/5))."($K_XX_XX)"); # K_XX_XX
838 foreach (@insns) { eval; } # remaining instructions [if any]
840 $Xi++; push(@X,shift(@X)); # "rotate" X[]
841 push(@Tx,shift(@Tx));
844 sub Xupdate_avx_32_79()
847 my @insns = (&$body,&$body,&$body,&$body); # 32 to 48 instructions
850 &vpalignr(@Tx[0],@X[-1&7],@X[-2&7],8); # compose "X[-6]"
851 &vpxor (@X[0],@X[0],@X[-4&7]); # "X[0]"="X[-32]"^"X[-16]"
852 eval(shift(@insns)); # body_20_39
855 eval(shift(@insns)); # rol
857 &vpxor (@X[0],@X[0],@X[-7&7]); # "X[0]"^="X[-28]"
859 eval(shift(@insns)) if (@insns[0] !~ /&ro[rl]/);
861 &vmovdqa (@Tx[2],@Tx[1]);# "perpetuate" K_XX_XX...
862 } else { # ... or load next one
863 &vmovdqa (@Tx[2],eval(16*($Xi/5))."($K_XX_XX)");
865 &vpaddd (@Tx[1],@Tx[1],@X[-1&7]);
866 eval(shift(@insns)); # ror
869 &vpxor (@X[0],@X[0],@Tx[0]); # "X[0]"^="X[-6]"
870 eval(shift(@insns)); # body_20_39
873 eval(shift(@insns)); # rol
875 &vpsrld (@Tx[0],@X[0],30);
876 &vmovdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
879 eval(shift(@insns)); # ror
882 &vpslld (@X[0],@X[0],2);
883 eval(shift(@insns)); # body_20_39
886 eval(shift(@insns)); # rol
889 eval(shift(@insns)); # ror
892 &vpor (@X[0],@X[0],@Tx[0]); # "X[0]"<<<=2
893 eval(shift(@insns)); # body_20_39
895 &vmovdqa (@Tx[1],@X[0]) if ($Xi<19);
897 eval(shift(@insns)); # rol
900 eval(shift(@insns)); # rol
903 foreach (@insns) { eval; } # remaining instructions
905 $Xi++; push(@X,shift(@X)); # "rotate" X[]
906 push(@Tx,shift(@Tx));
912 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
916 &vpaddd (@Tx[1],@Tx[1],@X[-1&7]);
922 &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer IALU
924 foreach (@insns) { eval; } # remaining instructions
929 unshift(@Tx,pop(@Tx));
931 &vmovdqa(@X[2],"64($K_XX_XX)"); # pbswap mask
932 &vmovdqa(@Tx[1],"0($K_XX_XX)"); # K_00_19
933 &vmovdqu(@X[-4&7],"0($inp)"); # load input
934 &vmovdqu(@X[-3&7],"16($inp)");
935 &vmovdqu(@X[-2&7],"32($inp)");
936 &vmovdqu(@X[-1&7],"48($inp)");
937 &vpshufb(@X[-4&7],@X[-4&7],@X[2]); # byte swap
946 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
951 &vpshufb(@X[($Xi-3)&7],@X[($Xi-3)&7],@X[2]);
954 &vpaddd (@X[$Xi&7],@X[($Xi-4)&7],@Tx[1]);
959 &vmovdqa(eval(16*$Xi)."(%rsp)",@X[$Xi&7]); # X[]+K xfer to IALU
963 foreach (@insns) { eval; }
970 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
973 foreach (@insns) { eval; }
980 &Xupdate_avx_16_31(\&body_00_19);
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_32_79(\&body_00_19);
985 &Xupdate_avx_32_79(\&body_20_39);
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_40_59);
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_20_39);
996 &Xuplast_avx_80(\&body_20_39); # can jump to "done"
998 $saved_j=$j; @saved_V=@V;
999 $saved_r=$r; @saved_rndkey=@rndkey;
1001 &Xloop_avx(\&body_20_39);
1002 &Xloop_avx(\&body_20_39);
1003 &Xloop_avx(\&body_20_39);
1006 vmovups $iv,48($out,$in0) # write output
1009 add 0($ctx),$A # update context
1016 mov @T[0],$B # magic seed
1025 $jj=$j=$saved_j; @V=@saved_V;
1026 $r=$saved_r; @rndkey=@saved_rndkey;
1028 &Xtail_avx(\&body_20_39);
1029 &Xtail_avx(\&body_20_39);
1030 &Xtail_avx(\&body_20_39);
1033 vmovups $iv,48($out,$in0) # write output
1034 mov 88(%rsp),$ivp # restore $ivp
1036 add 0($ctx),$A # update context
1046 vmovups $iv,($ivp) # write IV
1049 $code.=<<___ if ($win64);
1050 movaps 96+0(%rsp),%xmm6
1051 movaps 96+16(%rsp),%xmm7
1052 movaps 96+32(%rsp),%xmm8
1053 movaps 96+48(%rsp),%xmm9
1054 movaps 96+64(%rsp),%xmm10
1055 movaps 96+80(%rsp),%xmm11
1056 movaps 96+96(%rsp),%xmm12
1057 movaps 96+112(%rsp),%xmm13
1058 movaps 96+128(%rsp),%xmm14
1059 movaps 96+144(%rsp),%xmm15
1062 lea `104+($win64?10*16:0)`(%rsp),%rsi
1072 .size aesni_cbc_sha1_enc_avx,.-aesni_cbc_sha1_enc_avx
1078 .long 0x5a827999,0x5a827999,0x5a827999,0x5a827999 # K_00_19
1079 .long 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 # K_20_39
1080 .long 0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc # K_40_59
1081 .long 0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6 # K_60_79
1082 .long 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f # pbswap mask
1084 .asciz "AESNI-CBC+SHA1 stitch for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
1088 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
1089 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
1097 .extern __imp_RtlVirtualUnwind
1098 .type ssse3_handler,\@abi-omnipotent
1112 mov 120($context),%rax # pull context->Rax
1113 mov 248($context),%rbx # pull context->Rip
1115 mov 8($disp),%rsi # disp->ImageBase
1116 mov 56($disp),%r11 # disp->HandlerData
1118 mov 0(%r11),%r10d # HandlerData[0]
1119 lea (%rsi,%r10),%r10 # prologue label
1120 cmp %r10,%rbx # context->Rip<prologue label
1121 jb .Lcommon_seh_tail
1123 mov 152($context),%rax # pull context->Rsp
1125 mov 4(%r11),%r10d # HandlerData[1]
1126 lea (%rsi,%r10),%r10 # epilogue label
1127 cmp %r10,%rbx # context->Rip>=epilogue label
1128 jae .Lcommon_seh_tail
1131 lea 512($context),%rdi # &context.Xmm6
1133 .long 0xa548f3fc # cld; rep movsq
1134 lea `104+10*16`(%rax),%rax # adjust stack pointer
1143 mov %rbx,144($context) # restore context->Rbx
1144 mov %rbp,160($context) # restore context->Rbp
1145 mov %r12,216($context) # restore context->R12
1146 mov %r13,224($context) # restore context->R13
1147 mov %r14,232($context) # restore context->R14
1148 mov %r15,240($context) # restore context->R15
1153 mov %rax,152($context) # restore context->Rsp
1154 mov %rsi,168($context) # restore context->Rsi
1155 mov %rdi,176($context) # restore context->Rdi
1157 mov 40($disp),%rdi # disp->ContextRecord
1158 mov $context,%rsi # context
1159 mov \$154,%ecx # sizeof(CONTEXT)
1160 .long 0xa548f3fc # cld; rep movsq
1163 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
1164 mov 8(%rsi),%rdx # arg2, disp->ImageBase
1165 mov 0(%rsi),%r8 # arg3, disp->ControlPc
1166 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
1167 mov 40(%rsi),%r10 # disp->ContextRecord
1168 lea 56(%rsi),%r11 # &disp->HandlerData
1169 lea 24(%rsi),%r12 # &disp->EstablisherFrame
1170 mov %r10,32(%rsp) # arg5
1171 mov %r11,40(%rsp) # arg6
1172 mov %r12,48(%rsp) # arg7
1173 mov %rcx,56(%rsp) # arg8, (NULL)
1174 call *__imp_RtlVirtualUnwind(%rip)
1176 mov \$1,%eax # ExceptionContinueSearch
1188 .size ssse3_handler,.-ssse3_handler
1192 .rva .LSEH_begin_aesni_cbc_sha1_enc_ssse3
1193 .rva .LSEH_end_aesni_cbc_sha1_enc_ssse3
1194 .rva .LSEH_info_aesni_cbc_sha1_enc_ssse3
1196 $code.=<<___ if ($avx);
1197 .rva .LSEH_begin_aesni_cbc_sha1_enc_avx
1198 .rva .LSEH_end_aesni_cbc_sha1_enc_avx
1199 .rva .LSEH_info_aesni_cbc_sha1_enc_avx
1204 .LSEH_info_aesni_cbc_sha1_enc_ssse3:
1207 .rva .Lprologue_ssse3,.Lepilogue_ssse3 # HandlerData[]
1209 $code.=<<___ if ($avx);
1210 .LSEH_info_aesni_cbc_sha1_enc_avx:
1213 .rva .Lprologue_avx,.Lepilogue_avx # HandlerData[]
1217 ####################################################################
1219 local *opcode=shift;
1223 $rex|=0x04 if($dst>=8);
1224 $rex|=0x01 if($src>=8);
1225 push @opcode,$rex|0x40 if($rex);
1232 if ($line=~/(aes[a-z]+)\s+%xmm([0-9]+),\s*%xmm([0-9]+)/) {
1234 "aesenc" => 0xdc, "aesenclast" => 0xdd
1236 return undef if (!defined($opcodelet{$1}));
1237 rex(\@opcode,$3,$2);
1238 push @opcode,0x0f,0x38,$opcodelet{$1};
1239 push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
1240 return ".byte\t".join(',',@opcode);
1245 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
1246 $code =~ s/\b(aes.*%xmm[0-9]+).*$/aesni($1)/gem;