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.5] 9.26 6.58 +41%
25 # Sandy Bridge 5.05[+5.0(6.2)] 10.06(11.21) 6.09(7.05) +65%(+59%)
26 # Ivy Bridge 5.05[+4.6] 9.65 5.54 +74%
27 # Bulldozer 5.77[+6.0] 11.72 6.37 +84%
30 # Westmere 4.51 10.00 6.87 +46%
31 # Sandy Bridge 6.05 11.06(12.21) 6.11(7.20) +81%(+70%)
32 # Ivy Bridge 6.05 10.65 6.07 +75%
33 # Bulldozer 6.89 12.84 6.96 +84%
36 # Westmere 5.25 10.74 7.19 +49%
37 # Sandy Bridge 7.05 12.06(13.21) 7.12(7.68) +69%(+72%)
38 # Ivy Bridge 7.05 11.65 7.12 +64%
39 # Bulldozer 8.00 13.95 8.25 +69%
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; my $rx=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
195 movdqa 64($K_XX_XX),@X[2] # pbswap mask
196 movdqa 0($K_XX_XX),@Tx[1] # K_00_19
197 movdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
198 movdqu 16($inp),@X[-3&7]
199 movdqu 32($inp),@X[-2&7]
200 movdqu 48($inp),@X[-1&7]
201 pshufb @X[2],@X[-4&7] # byte swap
203 pshufb @X[2],@X[-3&7]
204 pshufb @X[2],@X[-2&7]
205 pshufb @X[2],@X[-1&7]
206 paddd @Tx[1],@X[-4&7] # add K_00_19
207 paddd @Tx[1],@X[-3&7]
208 paddd @Tx[1],@X[-2&7]
209 movdqa @X[-4&7],0(%rsp) # X[]+K xfer to IALU
210 psubd @Tx[1],@X[-4&7] # restore X[]
211 movdqa @X[-3&7],16(%rsp)
212 psubd @Tx[1],@X[-3&7]
213 movdqa @X[-2&7],32(%rsp)
214 psubd @Tx[1],@X[-2&7]
215 movups ($key),$rndkey0 # $key[0]
216 movups 16($key),$rndkey[0] # forward reference
222 my ($n,$k)=($r/10,$r%10);
225 movups `16*$n`($in0),$in # load input
228 $code.=<<___ if ($n);
229 movups $iv,`16*($n-1)`($out,$in0) # write output
233 aesenc $rndkey[0],$iv
234 movups `32+16*$k`($key),$rndkey[1]
241 movups `32+16*($k+0)`($key),$rndkey[1]
242 aesenc $rndkey[0],$iv
243 movups `32+16*($k+1)`($key),$rndkey[0]
244 aesenc $rndkey[1],$iv
246 movups `32+16*($k+2)`($key),$rndkey[1]
247 aesenc $rndkey[0],$iv
248 movups `32+16*($k+3)`($key),$rndkey[0]
249 aesenc $rndkey[1],$iv
251 aesenclast $rndkey[0],$iv
252 movups 16($key),$rndkey[1] # forward reference
256 aesenc $rndkey[0],$iv
257 movups `32+16*$k`($key),$rndkey[1]
260 $r++; unshift(@rndkey,pop(@rndkey));
263 sub Xupdate_ssse3_16_31() # recall that $Xi starts wtih 4
266 my @insns = (&$body,&$body,&$body,&$body); # 40 instructions
269 &movdqa (@X[0],@X[-3&7]);
272 &movdqa (@Tx[0],@X[-1&7]);
273 &palignr(@X[0],@X[-4&7],8); # compose "X[-14]" in "X[0]"
277 &paddd (@Tx[1],@X[-1&7]);
280 &psrldq (@Tx[0],4); # "X[-3]", 3 dwords
283 &pxor (@X[0],@X[-4&7]); # "X[0]"^="X[-16]"
287 &pxor (@Tx[0],@X[-2&7]); # "X[-3]"^"X[-8]"
293 &pxor (@X[0],@Tx[0]); # "X[0]"^="X[-3]"^"X[-8]"
296 &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
300 &movdqa (@Tx[2],@X[0]);
301 &movdqa (@Tx[0],@X[0]);
307 &pslldq (@Tx[2],12); # "X[0]"<<96, extract one dword
308 &paddd (@X[0],@X[0]);
317 &movdqa (@Tx[1],@Tx[2]);
322 &por (@X[0],@Tx[0]); # "X[0]"<<<=1
329 &pxor (@X[0],@Tx[2]);
332 &movdqa (@Tx[2],eval(16*(($Xi)/5))."($K_XX_XX)"); # K_XX_XX
336 &pxor (@X[0],@Tx[1]); # "X[0]"^=("X[0]">>96)<<<2
338 foreach (@insns) { eval; } # remaining instructions [if any]
340 $Xi++; push(@X,shift(@X)); # "rotate" X[]
341 push(@Tx,shift(@Tx));
344 sub Xupdate_ssse3_32_79()
347 my @insns = (&$body,&$body,&$body,&$body); # 32 to 48 instructions
350 &movdqa (@Tx[0],@X[-1&7]) if ($Xi==8);
351 eval(shift(@insns)); # body_20_39
352 &pxor (@X[0],@X[-4&7]); # "X[0]"="X[-32]"^"X[-16]"
353 &palignr(@Tx[0],@X[-2&7],8); # compose "X[-6]"
356 eval(shift(@insns)); # rol
358 &pxor (@X[0],@X[-7&7]); # "X[0]"^="X[-28]"
360 eval(shift(@insns)) if (@insns[0] !~ /&ro[rl]/);
362 &movdqa (@Tx[2],@Tx[1]);# "perpetuate" K_XX_XX...
363 } else { # ... or load next one
364 &movdqa (@Tx[2],eval(16*($Xi/5))."($K_XX_XX)");
366 &paddd (@Tx[1],@X[-1&7]);
367 eval(shift(@insns)); # ror
370 &pxor (@X[0],@Tx[0]); # "X[0]"^="X[-6]"
371 eval(shift(@insns)); # body_20_39
374 eval(shift(@insns)); # rol
376 &movdqa (@Tx[0],@X[0]);
377 &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
380 eval(shift(@insns)); # ror
384 eval(shift(@insns)); # body_20_39
388 eval(shift(@insns)); # rol
391 eval(shift(@insns)); # ror
394 &por (@X[0],@Tx[0]); # "X[0]"<<<=2
395 eval(shift(@insns)); # body_20_39
397 &movdqa (@Tx[1],@X[0]) if ($Xi<19);
399 eval(shift(@insns)); # rol
402 eval(shift(@insns)); # rol
405 foreach (@insns) { eval; } # remaining instructions
407 $Xi++; push(@X,shift(@X)); # "rotate" X[]
408 push(@Tx,shift(@Tx));
411 sub Xuplast_ssse3_80()
414 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
418 &paddd (@Tx[1],@X[-1&7]);
424 &movdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer IALU
426 foreach (@insns) { eval; } # remaining instructions
429 &je (".Ldone_ssse3");
431 unshift(@Tx,pop(@Tx));
433 &movdqa (@X[2],"64($K_XX_XX)"); # pbswap mask
434 &movdqa (@Tx[1],"0($K_XX_XX)"); # K_00_19
435 &movdqu (@X[-4&7],"0($inp)"); # load input
436 &movdqu (@X[-3&7],"16($inp)");
437 &movdqu (@X[-2&7],"32($inp)");
438 &movdqu (@X[-1&7],"48($inp)");
439 &pshufb (@X[-4&7],@X[2]); # byte swap
448 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
453 &pshufb (@X[($Xi-3)&7],@X[2]);
456 &paddd (@X[($Xi-4)&7],@Tx[1]);
461 &movdqa (eval(16*$Xi)."(%rsp)",@X[($Xi-4)&7]); # X[]+K xfer to IALU
464 &psubd (@X[($Xi-4)&7],@Tx[1]);
466 foreach (@insns) { eval; }
473 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
476 foreach (@insns) { eval; }
479 sub body_00_19 () { # ((c^d)&b)^d
480 # on start @T[0]=(c^d)&b
481 return &body_20_39() if ($rx==19); $rx++;
486 '($a,$b,$c,$d,$e)=@V;'.
487 '&$_ror ($b,$j?7:2);', # $b>>>2
489 '&mov (@T[1],$a);', # $b for next round
491 '&add ($e,eval(4*($j&15))."(%rsp)");',# X[]+K xfer
492 '&xor ($b,$c);', # $c^$d for next round
496 '&and (@T[1],$b);', # ($b&($c^$d)) for next round
498 '&xor ($b,$c);', # restore $b
499 '&add ($e,$a);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
502 $k = (($jj+1)*12/20)*20*$n/12; # 12 aesencs per these 20 rounds
503 @r[$k%$n].='&$aesenc();' if ($jj==$k/$n);
508 sub body_20_39 () { # b^d^c
510 return &body_40_59() if ($rx==39); $rx++;
515 '($a,$b,$c,$d,$e)=@V;'.
516 '&add ($e,eval(4*($j&15))."(%rsp)");',# X[]+K xfer
517 '&xor (@T[0],$d) if($j==19);'.
518 '&xor (@T[0],$c) if($j> 19);', # ($b^$d^$c)
519 '&mov (@T[1],$a);', # $b for next round
523 '&xor (@T[1],$c) if ($j< 79);', # $b^$d for next round
525 '&$_ror ($b,7);', # $b>>>2
526 '&add ($e,$a);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
529 $k = (($jj+1)*8/20)*20*$n/8; # 8 aesencs per these 20 rounds
530 @r[$k%$n].='&$aesenc();' if ($jj==$k/$n && $rx!=20);
535 sub body_40_59 () { # ((b^c)&(c^d))^c
536 # on entry @T[0]=(b^c), (c^=d)
542 '($a,$b,$c,$d,$e)=@V;'.
543 '&add ($e,eval(4*($j&15))."(%rsp)");',# X[]+K xfer
544 '&and (@T[0],$c) if ($j>=40);', # (b^c)&(c^d)
545 '&xor ($c,$d) if ($j>=40);', # restore $c
547 '&$_ror ($b,7);', # $b>>>2
548 '&mov (@T[1],$a);', # $b for next round
553 '&xor (@T[1],$c) if ($j==59);'.
554 '&xor (@T[1],$b) if ($j< 59);', # b^c for next round
556 '&xor ($b,$c) if ($j< 59);', # c^d for next round
557 '&add ($e,$a);' .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
560 $k=(($jj+1)*12/20)*20*$n/12; # 12 aesencs per these 20 rounds
561 @r[$k%$n].='&$aesenc();' if ($jj==$k/$n && $rx!=40);
569 &Xupdate_ssse3_16_31(\&body_00_19);
570 &Xupdate_ssse3_16_31(\&body_00_19);
571 &Xupdate_ssse3_16_31(\&body_00_19);
572 &Xupdate_ssse3_16_31(\&body_00_19);
573 &Xupdate_ssse3_32_79(\&body_00_19);
574 &Xupdate_ssse3_32_79(\&body_20_39);
575 &Xupdate_ssse3_32_79(\&body_20_39);
576 &Xupdate_ssse3_32_79(\&body_20_39);
577 &Xupdate_ssse3_32_79(\&body_20_39);
578 &Xupdate_ssse3_32_79(\&body_20_39);
579 &Xupdate_ssse3_32_79(\&body_40_59);
580 &Xupdate_ssse3_32_79(\&body_40_59);
581 &Xupdate_ssse3_32_79(\&body_40_59);
582 &Xupdate_ssse3_32_79(\&body_40_59);
583 &Xupdate_ssse3_32_79(\&body_40_59);
584 &Xupdate_ssse3_32_79(\&body_20_39);
585 &Xuplast_ssse3_80(\&body_20_39); # can jump to "done"
587 $saved_j=$j; @saved_V=@V;
588 $saved_r=$r; @saved_rndkey=@rndkey;
590 &Xloop_ssse3(\&body_20_39);
591 &Xloop_ssse3(\&body_20_39);
592 &Xloop_ssse3(\&body_20_39);
595 movups $iv,48($out,$in0) # write output
598 add 0($ctx),$A # update context
605 mov @T[0],$B # magic seed
617 $jj=$j=$saved_j; @V=@saved_V;
618 $r=$saved_r; @rndkey=@saved_rndkey;
620 &Xtail_ssse3(\&body_20_39);
621 &Xtail_ssse3(\&body_20_39);
622 &Xtail_ssse3(\&body_20_39);
625 movups $iv,48($out,$in0) # write output
626 mov 88(%rsp),$ivp # restore $ivp
628 add 0($ctx),$A # update context
638 movups $iv,($ivp) # write IV
640 $code.=<<___ if ($win64);
641 movaps 96+0(%rsp),%xmm6
642 movaps 96+16(%rsp),%xmm7
643 movaps 96+32(%rsp),%xmm8
644 movaps 96+48(%rsp),%xmm9
645 movaps 96+64(%rsp),%xmm10
646 movaps 96+80(%rsp),%xmm11
647 movaps 96+96(%rsp),%xmm12
648 movaps 96+112(%rsp),%xmm13
649 movaps 96+128(%rsp),%xmm14
650 movaps 96+144(%rsp),%xmm15
653 lea `104+($win64?10*16:0)`(%rsp),%rsi
663 .size aesni_cbc_sha1_enc_ssse3,.-aesni_cbc_sha1_enc_ssse3
669 my ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
672 my @X=map("%xmm$_",(4..7,0..3));
673 my @Tx=map("%xmm$_",(8..10));
675 my @V=($A,$B,$C,$D,$E)=("%eax","%ebx","%ecx","%edx","%ebp"); # size optimization
676 my @T=("%esi","%edi");
678 my $_rol=sub { &shld(@_[0],@_) };
679 my $_ror=sub { &shrd(@_[0],@_) };
682 .type aesni_cbc_sha1_enc_avx,\@function,6
684 aesni_cbc_sha1_enc_avx:
685 mov `($win64?56:8)`(%rsp),$inp # load 7th argument
686 #shr \$6,$len # debugging artefact
687 #jz .Lepilogue_avx # debugging artefact
694 lea `-104-($win64?10*16:0)`(%rsp),%rsp
695 #mov $in0,$inp # debugging artefact
696 #lea 64(%rsp),$ctx # debugging artefact
698 $code.=<<___ if ($win64);
699 movaps %xmm6,96+0(%rsp)
700 movaps %xmm7,96+16(%rsp)
701 movaps %xmm8,96+32(%rsp)
702 movaps %xmm9,96+48(%rsp)
703 movaps %xmm10,96+64(%rsp)
704 movaps %xmm11,96+80(%rsp)
705 movaps %xmm12,96+96(%rsp)
706 movaps %xmm13,96+112(%rsp)
707 movaps %xmm14,96+128(%rsp)
708 movaps %xmm15,96+144(%rsp)
713 mov $in0,%r12 # reassign arguments
717 vmovdqu ($ivp),$iv # load IV
718 mov $ivp,88(%rsp) # save $ivp
720 my ($in0,$out,$len,$key)=map("%r$_",(12..15)); # reassign arguments
721 my $rounds="${ivp}d";
725 mov 240($key),$rounds
726 add \$112,$key # size optimization
727 add $inp,$len # end of input
729 lea K_XX_XX(%rip),$K_XX_XX
730 mov 0($ctx),$A # load context
734 mov $B,@T[0] # magic seed
740 vmovdqa 64($K_XX_XX),@X[2] # pbswap mask
741 vmovdqa 0($K_XX_XX),$Kx # K_00_19
742 vmovdqu 0($inp),@X[-4&7] # load input to %xmm[0-3]
743 vmovdqu 16($inp),@X[-3&7]
744 vmovdqu 32($inp),@X[-2&7]
745 vmovdqu 48($inp),@X[-1&7]
746 vpshufb @X[2],@X[-4&7],@X[-4&7] # byte swap
748 vpshufb @X[2],@X[-3&7],@X[-3&7]
749 vpshufb @X[2],@X[-2&7],@X[-2&7]
750 vpshufb @X[2],@X[-1&7],@X[-1&7]
751 vpaddd $Kx,@X[-4&7],@X[0] # add K_00_19
752 vpaddd $Kx,@X[-3&7],@X[1]
753 vpaddd $Kx,@X[-2&7],@X[2]
754 vmovdqa @X[0],0(%rsp) # X[]+K xfer to IALU
755 vmovdqa @X[1],16(%rsp)
756 vmovdqa @X[2],32(%rsp)
757 vmovups -112($key),$rndkey[1] # $key[0]
758 vmovups 16-112($key),$rndkey[0] # forward reference
764 my ($n,$k)=($r/10,$r%10);
767 vmovups `16*$n`($in0),$in # load input
768 vxorps $rndkey[1],$in,$in
770 $code.=<<___ if ($n);
771 vmovups $iv,`16*($n-1)`($out,$in0) # write output
775 vaesenc $rndkey[0],$iv,$iv
776 vmovups `32+16*$k-112`($key),$rndkey[1]
783 vaesenc $rndkey[0],$iv,$iv
784 vmovups `32+16*($k+0)-112`($key),$rndkey[1]
785 vaesenc $rndkey[1],$iv,$iv
786 vmovups `32+16*($k+1)-112`($key),$rndkey[0]
788 vaesenc $rndkey[0],$iv,$iv
789 vmovups `32+16*($k+2)-112`($key),$rndkey[1]
790 vaesenc $rndkey[1],$iv,$iv
791 vmovups `32+16*($k+3)-112`($key),$rndkey[0]
793 vaesenclast $rndkey[0],$iv,$iv
794 vmovups -112($key),$rndkey[0]
795 vmovups 16-112($key),$rndkey[1] # forward reference
799 vaesenc $rndkey[0],$iv,$iv
800 vmovups `32+16*$k-112`($key),$rndkey[1]
803 $r++; unshift(@rndkey,pop(@rndkey));
806 sub Xupdate_avx_16_31() # recall that $Xi starts wtih 4
809 my @insns = (&$body,&$body,&$body,&$body); # 40 instructions
814 &vpalignr(@X[0],@X[-3&7],@X[-4&7],8); # compose "X[-14]" in "X[0]"
818 &vpaddd (@Tx[1],$Kx,@X[-1&7]);
821 &vpsrldq(@Tx[0],@X[-1&7],4); # "X[-3]", 3 dwords
824 &vpxor (@X[0],@X[0],@X[-4&7]); # "X[0]"^="X[-16]"
828 &vpxor (@Tx[0],@Tx[0],@X[-2&7]); # "X[-3]"^"X[-8]"
834 &vpxor (@X[0],@X[0],@Tx[0]); # "X[0]"^="X[-3]"^"X[-8]"
837 &vmovdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
841 &vpsrld (@Tx[0],@X[0],31);
847 &vpslldq(@Tx[2],@X[0],12); # "X[0]"<<96, extract one dword
848 &vpaddd (@X[0],@X[0],@X[0]);
854 &vpsrld (@Tx[1],@Tx[2],30);
855 &vpor (@X[0],@X[0],@Tx[0]); # "X[0]"<<<=1
861 &vpslld (@Tx[2],@Tx[2],2);
862 &vpxor (@X[0],@X[0],@Tx[1]);
868 &vpxor (@X[0],@X[0],@Tx[2]); # "X[0]"^=("X[0]">>96)<<<2
871 &vmovdqa ($Kx,eval(16*(($Xi)/5))."($K_XX_XX)") if ($Xi%5==0); # K_XX_XX
876 foreach (@insns) { eval; } # remaining instructions [if any]
878 $Xi++; push(@X,shift(@X)); # "rotate" X[]
881 sub Xupdate_avx_32_79()
884 my @insns = (&$body,&$body,&$body,&$body); # 32 to 48 instructions
887 &vpalignr(@Tx[0],@X[-1&7],@X[-2&7],8); # compose "X[-6]"
888 &vpxor (@X[0],@X[0],@X[-4&7]); # "X[0]"="X[-32]"^"X[-16]"
889 eval(shift(@insns)); # body_20_39
892 eval(shift(@insns)); # rol
894 &vpxor (@X[0],@X[0],@X[-7&7]); # "X[0]"^="X[-28]"
896 eval(shift(@insns)) if (@insns[0] !~ /&ro[rl]/);
897 &vpaddd (@Tx[1],$Kx,@X[-1&7]);
898 &vmovdqa ($Kx,eval(16*($Xi/5))."($K_XX_XX)") if ($Xi%5==0);
899 eval(shift(@insns)); # ror
902 &vpxor (@X[0],@X[0],@Tx[0]); # "X[0]"^="X[-6]"
903 eval(shift(@insns)); # body_20_39
906 eval(shift(@insns)); # rol
908 &vpsrld (@Tx[0],@X[0],30);
909 &vmovdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
912 eval(shift(@insns)); # ror
915 &vpslld (@X[0],@X[0],2);
916 eval(shift(@insns)); # body_20_39
919 eval(shift(@insns)); # rol
922 eval(shift(@insns)); # ror
925 &vpor (@X[0],@X[0],@Tx[0]); # "X[0]"<<<=2
926 eval(shift(@insns)); # body_20_39
929 eval(shift(@insns)); # rol
932 eval(shift(@insns)); # rol
935 foreach (@insns) { eval; } # remaining instructions
937 $Xi++; push(@X,shift(@X)); # "rotate" X[]
943 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
947 &vpaddd (@Tx[1],$Kx,@X[-1&7]);
953 &vmovdqa (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer IALU
955 foreach (@insns) { eval; } # remaining instructions
960 unshift(@Tx,pop(@Tx));
962 &vmovdqa(@X[2],"64($K_XX_XX)"); # pbswap mask
963 &vmovdqa($Kx,"0($K_XX_XX)"); # K_00_19
964 &vmovdqu(@X[-4&7],"0($inp)"); # load input
965 &vmovdqu(@X[-3&7],"16($inp)");
966 &vmovdqu(@X[-2&7],"32($inp)");
967 &vmovdqu(@X[-1&7],"48($inp)");
968 &vpshufb(@X[-4&7],@X[-4&7],@X[2]); # byte swap
977 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
982 &vpshufb(@X[($Xi-3)&7],@X[($Xi-3)&7],@X[2]);
985 &vpaddd (@X[$Xi&7],@X[($Xi-4)&7],$Kx);
990 &vmovdqa(eval(16*$Xi)."(%rsp)",@X[$Xi&7]); # X[]+K xfer to IALU
994 foreach (@insns) { eval; }
1001 my @insns = (&$body,&$body,&$body,&$body); # 32 instructions
1002 my ($a,$b,$c,$d,$e);
1004 foreach (@insns) { eval; }
1011 &Xupdate_avx_16_31(\&body_00_19);
1012 &Xupdate_avx_16_31(\&body_00_19);
1013 &Xupdate_avx_16_31(\&body_00_19);
1014 &Xupdate_avx_16_31(\&body_00_19);
1015 &Xupdate_avx_32_79(\&body_00_19);
1016 &Xupdate_avx_32_79(\&body_20_39);
1017 &Xupdate_avx_32_79(\&body_20_39);
1018 &Xupdate_avx_32_79(\&body_20_39);
1019 &Xupdate_avx_32_79(\&body_20_39);
1020 &Xupdate_avx_32_79(\&body_20_39);
1021 &Xupdate_avx_32_79(\&body_40_59);
1022 &Xupdate_avx_32_79(\&body_40_59);
1023 &Xupdate_avx_32_79(\&body_40_59);
1024 &Xupdate_avx_32_79(\&body_40_59);
1025 &Xupdate_avx_32_79(\&body_40_59);
1026 &Xupdate_avx_32_79(\&body_20_39);
1027 &Xuplast_avx_80(\&body_20_39); # can jump to "done"
1029 $saved_j=$j; @saved_V=@V;
1030 $saved_r=$r; @saved_rndkey=@rndkey;
1032 &Xloop_avx(\&body_20_39);
1033 &Xloop_avx(\&body_20_39);
1034 &Xloop_avx(\&body_20_39);
1037 vmovups $iv,48($out,$in0) # write output
1040 add 0($ctx),$A # update context
1047 mov @T[0],$B # magic seed
1059 $jj=$j=$saved_j; @V=@saved_V;
1060 $r=$saved_r; @rndkey=@saved_rndkey;
1062 &Xtail_avx(\&body_20_39);
1063 &Xtail_avx(\&body_20_39);
1064 &Xtail_avx(\&body_20_39);
1067 vmovups $iv,48($out,$in0) # write output
1068 mov 88(%rsp),$ivp # restore $ivp
1070 add 0($ctx),$A # update context
1080 vmovups $iv,($ivp) # write IV
1083 $code.=<<___ if ($win64);
1084 movaps 96+0(%rsp),%xmm6
1085 movaps 96+16(%rsp),%xmm7
1086 movaps 96+32(%rsp),%xmm8
1087 movaps 96+48(%rsp),%xmm9
1088 movaps 96+64(%rsp),%xmm10
1089 movaps 96+80(%rsp),%xmm11
1090 movaps 96+96(%rsp),%xmm12
1091 movaps 96+112(%rsp),%xmm13
1092 movaps 96+128(%rsp),%xmm14
1093 movaps 96+144(%rsp),%xmm15
1096 lea `104+($win64?10*16:0)`(%rsp),%rsi
1106 .size aesni_cbc_sha1_enc_avx,.-aesni_cbc_sha1_enc_avx
1112 .long 0x5a827999,0x5a827999,0x5a827999,0x5a827999 # K_00_19
1113 .long 0x6ed9eba1,0x6ed9eba1,0x6ed9eba1,0x6ed9eba1 # K_20_39
1114 .long 0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc,0x8f1bbcdc # K_40_59
1115 .long 0xca62c1d6,0xca62c1d6,0xca62c1d6,0xca62c1d6 # K_60_79
1116 .long 0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f # pbswap mask
1118 .asciz "AESNI-CBC+SHA1 stitch for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
1122 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
1123 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
1131 .extern __imp_RtlVirtualUnwind
1132 .type ssse3_handler,\@abi-omnipotent
1146 mov 120($context),%rax # pull context->Rax
1147 mov 248($context),%rbx # pull context->Rip
1149 mov 8($disp),%rsi # disp->ImageBase
1150 mov 56($disp),%r11 # disp->HandlerData
1152 mov 0(%r11),%r10d # HandlerData[0]
1153 lea (%rsi,%r10),%r10 # prologue label
1154 cmp %r10,%rbx # context->Rip<prologue label
1155 jb .Lcommon_seh_tail
1157 mov 152($context),%rax # pull context->Rsp
1159 mov 4(%r11),%r10d # HandlerData[1]
1160 lea (%rsi,%r10),%r10 # epilogue label
1161 cmp %r10,%rbx # context->Rip>=epilogue label
1162 jae .Lcommon_seh_tail
1165 lea 512($context),%rdi # &context.Xmm6
1167 .long 0xa548f3fc # cld; rep movsq
1168 lea `104+10*16`(%rax),%rax # adjust stack pointer
1177 mov %rbx,144($context) # restore context->Rbx
1178 mov %rbp,160($context) # restore context->Rbp
1179 mov %r12,216($context) # restore context->R12
1180 mov %r13,224($context) # restore context->R13
1181 mov %r14,232($context) # restore context->R14
1182 mov %r15,240($context) # restore context->R15
1187 mov %rax,152($context) # restore context->Rsp
1188 mov %rsi,168($context) # restore context->Rsi
1189 mov %rdi,176($context) # restore context->Rdi
1191 mov 40($disp),%rdi # disp->ContextRecord
1192 mov $context,%rsi # context
1193 mov \$154,%ecx # sizeof(CONTEXT)
1194 .long 0xa548f3fc # cld; rep movsq
1197 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
1198 mov 8(%rsi),%rdx # arg2, disp->ImageBase
1199 mov 0(%rsi),%r8 # arg3, disp->ControlPc
1200 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
1201 mov 40(%rsi),%r10 # disp->ContextRecord
1202 lea 56(%rsi),%r11 # &disp->HandlerData
1203 lea 24(%rsi),%r12 # &disp->EstablisherFrame
1204 mov %r10,32(%rsp) # arg5
1205 mov %r11,40(%rsp) # arg6
1206 mov %r12,48(%rsp) # arg7
1207 mov %rcx,56(%rsp) # arg8, (NULL)
1208 call *__imp_RtlVirtualUnwind(%rip)
1210 mov \$1,%eax # ExceptionContinueSearch
1222 .size ssse3_handler,.-ssse3_handler
1226 .rva .LSEH_begin_aesni_cbc_sha1_enc_ssse3
1227 .rva .LSEH_end_aesni_cbc_sha1_enc_ssse3
1228 .rva .LSEH_info_aesni_cbc_sha1_enc_ssse3
1230 $code.=<<___ if ($avx);
1231 .rva .LSEH_begin_aesni_cbc_sha1_enc_avx
1232 .rva .LSEH_end_aesni_cbc_sha1_enc_avx
1233 .rva .LSEH_info_aesni_cbc_sha1_enc_avx
1238 .LSEH_info_aesni_cbc_sha1_enc_ssse3:
1241 .rva .Lprologue_ssse3,.Lepilogue_ssse3 # HandlerData[]
1243 $code.=<<___ if ($avx);
1244 .LSEH_info_aesni_cbc_sha1_enc_avx:
1247 .rva .Lprologue_avx,.Lepilogue_avx # HandlerData[]
1251 ####################################################################
1253 local *opcode=shift;
1257 $rex|=0x04 if($dst>=8);
1258 $rex|=0x01 if($src>=8);
1259 push @opcode,$rex|0x40 if($rex);
1266 if ($line=~/(aes[a-z]+)\s+%xmm([0-9]+),\s*%xmm([0-9]+)/) {
1268 "aesenc" => 0xdc, "aesenclast" => 0xdd
1270 return undef if (!defined($opcodelet{$1}));
1271 rex(\@opcode,$3,$2);
1272 push @opcode,0x0f,0x38,$opcodelet{$1};
1273 push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
1274 return ".byte\t".join(',',@opcode);
1279 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
1280 $code =~ s/\b(aes.*%xmm[0-9]+).*$/aesni($1)/gem;