x86_64 assembly pack: make Windows build more robust.
[oweals/openssl.git] / crypto / aes / asm / aesni-sha1-x86_64.pl
1 #!/usr/bin/env perl
2 #
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 # ====================================================================
9 #
10 # June 2011
11 #
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"
21 # subroutine:
22 #
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%
28 #
29 #               AES-192-CBC
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%
34 #
35 #               AES-256-CBC
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%
40 #
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
44 #       don't support AVX.
45 #
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:
52 #
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
58
59 $flavour = shift;
60 $output  = shift;
61 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
62
63 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
64
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";
69
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]+)/ &&
72            $1>=2.19);
73 $avx=1 if (!$avx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
74            `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/ &&
75            $1>=2.09);
76 $avx=1 if (!$avx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
77            `ml64 2>&1` =~ /Version ([0-9]+)\./ &&
78            $1>=10);
79
80 open OUT,"| \"$^X\" $xlate $flavour $output";
81 *STDOUT=*OUT;
82
83 # void aesni_cbc_sha1_enc(const void *inp,
84 #                       void *out,
85 #                       size_t length,
86 #                       const AES_KEY *key,
87 #                       unsigned char *iv,
88 #                       SHA_CTX *ctx,
89 #                       const void *in0);
90
91 $code.=<<___;
92 .text
93 .extern OPENSSL_ia32cap_P
94
95 .globl  aesni_cbc_sha1_enc
96 .type   aesni_cbc_sha1_enc,\@abi-omnipotent
97 .align  16
98 aesni_cbc_sha1_enc:
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
102 ___
103 $code.=<<___ if ($avx);
104         and     \$`1<<28`,%r11d         # mask AVX bit
105         and     \$`1<<30`,%r10d         # mask "Intel CPU" bit
106         or      %r11d,%r10d
107         cmp     \$`1<<28|1<<30`,%r10d
108         je      aesni_cbc_sha1_enc_avx
109 ___
110 $code.=<<___;
111         jmp     aesni_cbc_sha1_enc_ssse3
112         ret
113 .size   aesni_cbc_sha1_enc,.-aesni_cbc_sha1_enc
114 ___
115
116 my ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
117
118 my $Xi=4;
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;
124 my $K_XX_XX="%r11";
125 my ($iv,$in,$rndkey0)=map("%xmm$_",(11..13));
126 my @rndkey=("%xmm14","%xmm15");
127
128 sub AUTOLOAD()          # thunk [simplified] 32-bit style perlasm
129 { my $opcode = $AUTOLOAD; $opcode =~ s/.*:://;
130   my $arg = pop;
131     $arg = "\$$arg" if ($arg*1 eq $arg);
132     $code .= "\t$opcode\t".join(',',$arg,reverse @_)."\n";
133 }
134
135 my $_rol=sub { &rol(@_) };
136 my $_ror=sub { &ror(@_) };
137
138 $code.=<<___;
139 .type   aesni_cbc_sha1_enc_ssse3,\@function,6
140 .align  16
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
145         push    %rbx
146         push    %rbp
147         push    %r12
148         push    %r13
149         push    %r14
150         push    %r15
151         lea     `-104-($win64?10*16:0)`(%rsp),%rsp
152         #mov    $in0,$inp                       # debugging artefact
153         #lea    64(%rsp),$ctx                   # debugging artefact
154 ___
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)
166 .Lprologue_ssse3:
167 ___
168 $code.=<<___;
169         mov     $in0,%r12                       # reassign arguments
170         mov     $out,%r13
171         mov     $len,%r14
172         mov     $key,%r15
173         movdqu  ($ivp),$iv                      # load IV
174         mov     $ivp,88(%rsp)                   # save $ivp
175 ___
176 my ($in0,$out,$len,$key)=map("%r$_",(12..15));  # reassign arguments
177 my $rounds="${ivp}d";
178 $code.=<<___;
179         shl     \$6,$len
180         sub     $in0,$out
181         mov     240($key),$rounds
182         add     $inp,$len               # end of input
183
184         lea     K_XX_XX(%rip),$K_XX_XX
185         mov     0($ctx),$A              # load context
186         mov     4($ctx),$B
187         mov     8($ctx),$C
188         mov     12($ctx),$D
189         mov     $B,@T[0]                # magic seed
190         mov     16($ctx),$E
191
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
199         add     \$64,$inp
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
214         jmp     .Loop_ssse3
215 ___
216
217 my $aesenc=sub {
218   use integer;
219   my ($n,$k)=($r/10,$r%10);
220     if ($k==0) {
221       $code.=<<___;
222         movups          `16*$n`($in0),$in               # load input
223         xorps           $rndkey0,$in
224 ___
225       $code.=<<___ if ($n);
226         movups          $iv,`16*($n-1)`($out,$in0)      # write output
227 ___
228       $code.=<<___;
229         xorps           $in,$iv
230         aesenc          $rndkey[0],$iv
231         movups          `32+16*$k`($key),$rndkey[1]
232 ___
233     } elsif ($k==9) {
234       $sn++;
235       $code.=<<___;
236         cmp             \$11,$rounds
237         jb              .Laesenclast$sn
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
242         je              .Laesenclast$sn
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
247 .Laesenclast$sn:
248         aesenclast      $rndkey[0],$iv
249         movups          16($key),$rndkey[1]             # forward reference
250 ___
251     } else {
252       $code.=<<___;
253         aesenc          $rndkey[0],$iv
254         movups          `32+16*$k`($key),$rndkey[1]
255 ___
256     }
257     $r++;       unshift(@rndkey,pop(@rndkey));
258 };
259
260 sub Xupdate_ssse3_16_31()               # recall that $Xi starts wtih 4
261 { use integer;
262   my $body = shift;
263   my @insns = (&$body,&$body,&$body,&$body);    # 40 instructions
264   my ($a,$b,$c,$d,$e);
265
266         &movdqa (@X[0],@X[-3&7]);
267          eval(shift(@insns));
268          eval(shift(@insns));
269         &movdqa (@Tx[0],@X[-1&7]);
270         &palignr(@X[0],@X[-4&7],8);     # compose "X[-14]" in "X[0]"
271          eval(shift(@insns));
272          eval(shift(@insns));
273
274           &paddd        (@Tx[1],@X[-1&7]);
275          eval(shift(@insns));
276          eval(shift(@insns));
277         &psrldq (@Tx[0],4);             # "X[-3]", 3 dwords
278          eval(shift(@insns));
279          eval(shift(@insns));
280         &pxor   (@X[0],@X[-4&7]);       # "X[0]"^="X[-16]"
281          eval(shift(@insns));
282          eval(shift(@insns));
283
284         &pxor   (@Tx[0],@X[-2&7]);      # "X[-3]"^"X[-8]"
285          eval(shift(@insns));
286          eval(shift(@insns));
287          eval(shift(@insns));
288          eval(shift(@insns));
289
290         &pxor   (@X[0],@Tx[0]);         # "X[0]"^="X[-3]"^"X[-8]"
291          eval(shift(@insns));
292          eval(shift(@insns));
293           &movdqa       (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
294          eval(shift(@insns));
295          eval(shift(@insns));
296
297         &movdqa (@Tx[2],@X[0]);
298         &movdqa (@Tx[0],@X[0]);
299          eval(shift(@insns));
300          eval(shift(@insns));
301          eval(shift(@insns));
302          eval(shift(@insns));
303
304         &pslldq (@Tx[2],12);            # "X[0]"<<96, extract one dword
305         &paddd  (@X[0],@X[0]);
306          eval(shift(@insns));
307          eval(shift(@insns));
308          eval(shift(@insns));
309          eval(shift(@insns));
310
311         &psrld  (@Tx[0],31);
312          eval(shift(@insns));
313          eval(shift(@insns));
314         &movdqa (@Tx[1],@Tx[2]);
315          eval(shift(@insns));
316          eval(shift(@insns));
317
318         &psrld  (@Tx[2],30);
319         &por    (@X[0],@Tx[0]);         # "X[0]"<<<=1
320          eval(shift(@insns));
321          eval(shift(@insns));
322          eval(shift(@insns));
323          eval(shift(@insns));
324
325         &pslld  (@Tx[1],2);
326         &pxor   (@X[0],@Tx[2]);
327          eval(shift(@insns));
328          eval(shift(@insns));
329           &movdqa       (@Tx[2],eval(16*(($Xi)/5))."($K_XX_XX)");       # K_XX_XX
330          eval(shift(@insns));
331          eval(shift(@insns));
332
333         &pxor   (@X[0],@Tx[1]);         # "X[0]"^=("X[0]">>96)<<<2
334
335          foreach (@insns) { eval; }     # remaining instructions [if any]
336
337   $Xi++;        push(@X,shift(@X));     # "rotate" X[]
338                 push(@Tx,shift(@Tx));
339 }
340
341 sub Xupdate_ssse3_32_79()
342 { use integer;
343   my $body = shift;
344   my @insns = (&$body,&$body,&$body,&$body);    # 32 to 48 instructions
345   my ($a,$b,$c,$d,$e);
346
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]"
351          eval(shift(@insns));
352          eval(shift(@insns));
353          eval(shift(@insns));           # rol
354
355         &pxor   (@X[0],@X[-7&7]);       # "X[0]"^="X[-28]"
356          eval(shift(@insns));
357          eval(shift(@insns))    if (@insns[0] !~ /&ro[rl]/);
358         if ($Xi%5) {
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)");
362         }
363           &paddd        (@Tx[1],@X[-1&7]);
364          eval(shift(@insns));           # ror
365          eval(shift(@insns));
366
367         &pxor   (@X[0],@Tx[0]);         # "X[0]"^="X[-6]"
368          eval(shift(@insns));           # body_20_39
369          eval(shift(@insns));
370          eval(shift(@insns));
371          eval(shift(@insns));           # rol
372
373         &movdqa (@Tx[0],@X[0]);
374           &movdqa       (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
375          eval(shift(@insns));
376          eval(shift(@insns));
377          eval(shift(@insns));           # ror
378          eval(shift(@insns));
379
380         &pslld  (@X[0],2);
381          eval(shift(@insns));           # body_20_39
382          eval(shift(@insns));
383         &psrld  (@Tx[0],30);
384          eval(shift(@insns));
385          eval(shift(@insns));           # rol
386          eval(shift(@insns));
387          eval(shift(@insns));
388          eval(shift(@insns));           # ror
389          eval(shift(@insns));
390
391         &por    (@X[0],@Tx[0]);         # "X[0]"<<<=2
392          eval(shift(@insns));           # body_20_39
393          eval(shift(@insns));
394           &movdqa       (@Tx[1],@X[0])  if ($Xi<19);
395          eval(shift(@insns));
396          eval(shift(@insns));           # rol
397          eval(shift(@insns));
398          eval(shift(@insns));
399          eval(shift(@insns));           # rol
400          eval(shift(@insns));
401
402          foreach (@insns) { eval; }     # remaining instructions
403
404   $Xi++;        push(@X,shift(@X));     # "rotate" X[]
405                 push(@Tx,shift(@Tx));
406 }
407
408 sub Xuplast_ssse3_80()
409 { use integer;
410   my $body = shift;
411   my @insns = (&$body,&$body,&$body,&$body);    # 32 instructions
412   my ($a,$b,$c,$d,$e);
413
414          eval(shift(@insns));
415           &paddd        (@Tx[1],@X[-1&7]);
416          eval(shift(@insns));
417          eval(shift(@insns));
418          eval(shift(@insns));
419          eval(shift(@insns));
420
421           &movdqa       (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer IALU
422
423          foreach (@insns) { eval; }             # remaining instructions
424
425         &cmp    ($inp,$len);
426         &je     (".Ldone_ssse3");
427
428         unshift(@Tx,pop(@Tx));
429
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
437         &add    ($inp,64);
438
439   $Xi=0;
440 }
441
442 sub Xloop_ssse3()
443 { use integer;
444   my $body = shift;
445   my @insns = (&$body,&$body,&$body,&$body);    # 32 instructions
446   my ($a,$b,$c,$d,$e);
447
448          eval(shift(@insns));
449          eval(shift(@insns));
450         &pshufb (@X[($Xi-3)&7],@X[2]);
451          eval(shift(@insns));
452          eval(shift(@insns));
453         &paddd  (@X[($Xi-4)&7],@Tx[1]);
454          eval(shift(@insns));
455          eval(shift(@insns));
456          eval(shift(@insns));
457          eval(shift(@insns));
458         &movdqa (eval(16*$Xi)."(%rsp)",@X[($Xi-4)&7]);  # X[]+K xfer to IALU
459          eval(shift(@insns));
460          eval(shift(@insns));
461         &psubd  (@X[($Xi-4)&7],@Tx[1]);
462
463         foreach (@insns) { eval; }
464   $Xi++;
465 }
466
467 sub Xtail_ssse3()
468 { use integer;
469   my $body = shift;
470   my @insns = (&$body,&$body,&$body,&$body);    # 32 instructions
471   my ($a,$b,$c,$d,$e);
472
473         foreach (@insns) { eval; }
474 }
475
476 sub body_00_19 () {
477   use integer;
478   my ($k,$n);
479   my @r=(
480         '($a,$b,$c,$d,$e)=@V;'.
481         '&add   ($e,eval(4*($j&15))."(%rsp)");',        # X[]+K xfer
482         '&xor   ($c,$d);',
483         '&mov   (@T[1],$a);',   # $b in next round
484         '&$_rol ($a,5);',
485         '&and   (@T[0],$c);',   # ($b&($c^$d))
486         '&xor   ($c,$d);',      # restore $c
487         '&xor   (@T[0],$d);',
488         '&add   ($e,$a);',
489         '&$_ror ($b,$j?7:2);',  # $b>>>2
490         '&add   ($e,@T[0]);'    .'$j++; unshift(@V,pop(@V)); unshift(@T,pop(@T));'
491         );
492         $n = scalar(@r);
493         $k = (($jj+1)*12/20)*20*$n/12;  # 12 aesencs per these 20 rounds
494         @r[$k%$n].='&$aesenc();'        if ($jj==$k/$n);
495         $jj++;
496     return @r;
497 }
498
499 sub body_20_39 () {
500   use integer;
501   my ($k,$n);
502   my @r=(
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
507         '&$_rol ($a,5);',
508         '&xor   (@T[0],$c);',   # ($b^$d^$c)
509         '&add   ($e,$a);',
510         '&$_ror ($b,7);',       # $b>>>2
511         '&add   ($e,@T[0]);'    .'unshift(@V,pop(@V)); unshift(@T,pop(@T));'
512         );
513         $n = scalar(@r);
514         $k = (($jj+1)*8/20)*20*$n/8;    # 8 aesencs per these 20 rounds
515         @r[$k%$n].='&$aesenc();'        if ($jj==$k/$n);
516         $jj++;
517     return @r;
518 }
519
520 sub body_40_59 () {
521   use integer;
522   my ($k,$n);
523   my @r=(
524         '($a,$b,$c,$d,$e)=@V;'.
525         '&mov   (@T[1],$c);',
526         '&xor   ($c,$d);',
527         '&add   ($e,eval(4*($j++&15))."(%rsp)");',      # X[]+K xfer
528         '&and   (@T[1],$d);',
529         '&and   (@T[0],$c);',   # ($b&($c^$d))
530         '&$_ror ($b,7);',       # $b>>>2
531         '&add   ($e,@T[1]);',
532         '&mov   (@T[1],$a);',   # $b in next round
533         '&$_rol ($a,5);',
534         '&add   ($e,@T[0]);',
535         '&xor   ($c,$d);',      # restore $c
536         '&add   ($e,$a);'       .'unshift(@V,pop(@V)); unshift(@T,pop(@T));'
537         );
538         $n = scalar(@r);
539         $k=(($jj+1)*12/20)*20*$n/12;    # 12 aesencs per these 20 rounds
540         @r[$k%$n].='&$aesenc();'        if ($jj==$k/$n);
541         $jj++;
542     return @r;
543 }
544 $code.=<<___;
545 .align  16
546 .Loop_ssse3:
547 ___
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"
565
566                                 $saved_j=$j; @saved_V=@V;
567                                 $saved_r=$r; @saved_rndkey=@rndkey;
568
569         &Xloop_ssse3(\&body_20_39);
570         &Xloop_ssse3(\&body_20_39);
571         &Xloop_ssse3(\&body_20_39);
572
573 $code.=<<___;
574         movups  $iv,48($out,$in0)               # write output
575         lea     64($in0),$in0
576
577         add     0($ctx),$A                      # update context
578         add     4($ctx),@T[0]
579         add     8($ctx),$C
580         add     12($ctx),$D
581         mov     $A,0($ctx)
582         add     16($ctx),$E
583         mov     @T[0],4($ctx)
584         mov     @T[0],$B                        # magic seed
585         mov     $C,8($ctx)
586         mov     $D,12($ctx)
587         mov     $E,16($ctx)
588         jmp     .Loop_ssse3
589
590 .align  16
591 .Ldone_ssse3:
592 ___
593                                 $jj=$j=$saved_j; @V=@saved_V;
594                                 $r=$saved_r;     @rndkey=@saved_rndkey;
595
596         &Xtail_ssse3(\&body_20_39);
597         &Xtail_ssse3(\&body_20_39);
598         &Xtail_ssse3(\&body_20_39);
599
600 $code.=<<___;
601         movups  $iv,48($out,$in0)               # write output
602         mov     88(%rsp),$ivp                   # restore $ivp
603
604         add     0($ctx),$A                      # update context
605         add     4($ctx),@T[0]
606         add     8($ctx),$C
607         mov     $A,0($ctx)
608         add     12($ctx),$D
609         mov     @T[0],4($ctx)
610         add     16($ctx),$E
611         mov     $C,8($ctx)
612         mov     $D,12($ctx)
613         mov     $E,16($ctx)
614         movups  $iv,($ivp)                      # write IV
615 ___
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
627 ___
628 $code.=<<___;
629         lea     `104+($win64?10*16:0)`(%rsp),%rsi
630         mov     0(%rsi),%r15
631         mov     8(%rsi),%r14
632         mov     16(%rsi),%r13
633         mov     24(%rsi),%r12
634         mov     32(%rsi),%rbp
635         mov     40(%rsi),%rbx
636         lea     48(%rsi),%rsp
637 .Lepilogue_ssse3:
638         ret
639 .size   aesni_cbc_sha1_enc_ssse3,.-aesni_cbc_sha1_enc_ssse3
640 ___
641
642 $j=$jj=$r=$sn=0;
643
644 if ($avx) {
645 my ($in0,$out,$len,$key,$ivp,$ctx,$inp)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9","%r10");
646
647 my $Xi=4;
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");
652
653 my $_rol=sub { &shld(@_[0],@_) };
654 my $_ror=sub { &shrd(@_[0],@_) };
655
656 $code.=<<___;
657 .type   aesni_cbc_sha1_enc_avx,\@function,6
658 .align  16
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
663         push    %rbx
664         push    %rbp
665         push    %r12
666         push    %r13
667         push    %r14
668         push    %r15
669         lea     `-104-($win64?10*16:0)`(%rsp),%rsp
670         #mov    $in0,$inp                       # debugging artefact
671         #lea    64(%rsp),$ctx                   # debugging artefact
672 ___
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)
684 .Lprologue_avx:
685 ___
686 $code.=<<___;
687         vzeroall
688         mov     $in0,%r12                       # reassign arguments
689         mov     $out,%r13
690         mov     $len,%r14
691         mov     $key,%r15
692         vmovdqu ($ivp),$iv                      # load IV
693         mov     $ivp,88(%rsp)                   # save $ivp
694 ___
695 my ($in0,$out,$len,$key)=map("%r$_",(12..15));  # reassign arguments
696 my $rounds="${ivp}d";
697 $code.=<<___;
698         shl     \$6,$len
699         sub     $in0,$out
700         mov     240($key),$rounds
701         add     \$112,$key              # size optimization
702         add     $inp,$len               # end of input
703
704         lea     K_XX_XX(%rip),$K_XX_XX
705         mov     0($ctx),$A              # load context
706         mov     4($ctx),$B
707         mov     8($ctx),$C
708         mov     12($ctx),$D
709         mov     $B,@T[0]                # magic seed
710         mov     16($ctx),$E
711
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
719         add     \$64,$inp
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
731         jmp     .Loop_avx
732 ___
733
734 my $aesenc=sub {
735   use integer;
736   my ($n,$k)=($r/10,$r%10);
737     if ($k==0) {
738       $code.=<<___;
739         vmovups         `16*$n`($in0),$in               # load input
740         vxorps          $rndkey0,$in,$in
741 ___
742       $code.=<<___ if ($n);
743         vmovups         $iv,`16*($n-1)`($out,$in0)      # write output
744 ___
745       $code.=<<___;
746         vxorps          $in,$iv,$iv
747         vaesenc         $rndkey[0],$iv,$iv
748         vmovups         `32+16*$k-112`($key),$rndkey[1]
749 ___
750     } elsif ($k==9) {
751       $sn++;
752       $code.=<<___;
753         cmp             \$11,$rounds
754         jb              .Lvaesenclast$sn
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]
759         je              .Lvaesenclast$sn
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]
764 .Lvaesenclast$sn:
765         vaesenclast     $rndkey[0],$iv,$iv
766         vmovups         16-112($key),$rndkey[1]         # forward reference
767 ___
768     } else {
769       $code.=<<___;
770         vaesenc         $rndkey[0],$iv,$iv
771         vmovups         `32+16*$k-112`($key),$rndkey[1]
772 ___
773     }
774     $r++;       unshift(@rndkey,pop(@rndkey));
775 };
776
777 sub Xupdate_avx_16_31()         # recall that $Xi starts wtih 4
778 { use integer;
779   my $body = shift;
780   my @insns = (&$body,&$body,&$body,&$body);    # 40 instructions
781   my ($a,$b,$c,$d,$e);
782
783          eval(shift(@insns));
784          eval(shift(@insns));
785         &vpalignr(@X[0],@X[-3&7],@X[-4&7],8);   # compose "X[-14]" in "X[0]"
786          eval(shift(@insns));
787          eval(shift(@insns));
788
789           &vpaddd       (@Tx[1],@Tx[1],@X[-1&7]);
790          eval(shift(@insns));
791          eval(shift(@insns));
792         &vpsrldq(@Tx[0],@X[-1&7],4);    # "X[-3]", 3 dwords
793          eval(shift(@insns));
794          eval(shift(@insns));
795         &vpxor  (@X[0],@X[0],@X[-4&7]);         # "X[0]"^="X[-16]"
796          eval(shift(@insns));
797          eval(shift(@insns));
798
799         &vpxor  (@Tx[0],@Tx[0],@X[-2&7]);       # "X[-3]"^"X[-8]"
800          eval(shift(@insns));
801          eval(shift(@insns));
802          eval(shift(@insns));
803          eval(shift(@insns));
804
805         &vpxor  (@X[0],@X[0],@Tx[0]);           # "X[0]"^="X[-3]"^"X[-8]"
806          eval(shift(@insns));
807          eval(shift(@insns));
808           &vmovdqa      (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
809          eval(shift(@insns));
810          eval(shift(@insns));
811
812         &vpsrld (@Tx[0],@X[0],31);
813          eval(shift(@insns));
814          eval(shift(@insns));
815          eval(shift(@insns));
816          eval(shift(@insns));
817
818         &vpslldq(@Tx[2],@X[0],12);              # "X[0]"<<96, extract one dword
819         &vpaddd (@X[0],@X[0],@X[0]);
820          eval(shift(@insns));
821          eval(shift(@insns));
822          eval(shift(@insns));
823          eval(shift(@insns));
824
825         &vpsrld (@Tx[1],@Tx[2],30);
826         &vpor   (@X[0],@X[0],@Tx[0]);           # "X[0]"<<<=1
827          eval(shift(@insns));
828          eval(shift(@insns));
829          eval(shift(@insns));
830          eval(shift(@insns));
831
832         &vpslld (@Tx[2],@Tx[2],2);
833         &vpxor  (@X[0],@X[0],@Tx[1]);
834          eval(shift(@insns));
835          eval(shift(@insns));
836          eval(shift(@insns));
837          eval(shift(@insns));
838
839         &vpxor  (@X[0],@X[0],@Tx[2]);           # "X[0]"^=("X[0]">>96)<<<2
840          eval(shift(@insns));
841          eval(shift(@insns));
842           &vmovdqa      (@Tx[2],eval(16*(($Xi)/5))."($K_XX_XX)");       # K_XX_XX
843          eval(shift(@insns));
844          eval(shift(@insns));
845
846
847          foreach (@insns) { eval; }     # remaining instructions [if any]
848
849   $Xi++;        push(@X,shift(@X));     # "rotate" X[]
850                 push(@Tx,shift(@Tx));
851 }
852
853 sub Xupdate_avx_32_79()
854 { use integer;
855   my $body = shift;
856   my @insns = (&$body,&$body,&$body,&$body);    # 32 to 48 instructions
857   my ($a,$b,$c,$d,$e);
858
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
862          eval(shift(@insns));
863          eval(shift(@insns));
864          eval(shift(@insns));           # rol
865
866         &vpxor  (@X[0],@X[0],@X[-7&7]);         # "X[0]"^="X[-28]"
867          eval(shift(@insns));
868          eval(shift(@insns))    if (@insns[0] !~ /&ro[rl]/);
869         if ($Xi%5) {
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)");
873         }
874           &vpaddd       (@Tx[1],@Tx[1],@X[-1&7]);
875          eval(shift(@insns));           # ror
876          eval(shift(@insns));
877
878         &vpxor  (@X[0],@X[0],@Tx[0]);           # "X[0]"^="X[-6]"
879          eval(shift(@insns));           # body_20_39
880          eval(shift(@insns));
881          eval(shift(@insns));
882          eval(shift(@insns));           # rol
883
884         &vpsrld (@Tx[0],@X[0],30);
885           &vmovdqa      (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer to IALU
886          eval(shift(@insns));
887          eval(shift(@insns));
888          eval(shift(@insns));           # ror
889          eval(shift(@insns));
890
891         &vpslld (@X[0],@X[0],2);
892          eval(shift(@insns));           # body_20_39
893          eval(shift(@insns));
894          eval(shift(@insns));
895          eval(shift(@insns));           # rol
896          eval(shift(@insns));
897          eval(shift(@insns));
898          eval(shift(@insns));           # ror
899          eval(shift(@insns));
900
901         &vpor   (@X[0],@X[0],@Tx[0]);           # "X[0]"<<<=2
902          eval(shift(@insns));           # body_20_39
903          eval(shift(@insns));
904           &vmovdqa      (@Tx[1],@X[0])  if ($Xi<19);
905          eval(shift(@insns));
906          eval(shift(@insns));           # rol
907          eval(shift(@insns));
908          eval(shift(@insns));
909          eval(shift(@insns));           # rol
910          eval(shift(@insns));
911
912          foreach (@insns) { eval; }     # remaining instructions
913
914   $Xi++;        push(@X,shift(@X));     # "rotate" X[]
915                 push(@Tx,shift(@Tx));
916 }
917
918 sub Xuplast_avx_80()
919 { use integer;
920   my $body = shift;
921   my @insns = (&$body,&$body,&$body,&$body);    # 32 instructions
922   my ($a,$b,$c,$d,$e);
923
924          eval(shift(@insns));
925           &vpaddd       (@Tx[1],@Tx[1],@X[-1&7]);
926          eval(shift(@insns));
927          eval(shift(@insns));
928          eval(shift(@insns));
929          eval(shift(@insns));
930
931           &movdqa       (eval(16*(($Xi-1)&3))."(%rsp)",@Tx[1]); # X[]+K xfer IALU
932
933          foreach (@insns) { eval; }             # remaining instructions
934
935         &cmp    ($inp,$len);
936         &je     (".Ldone_avx");
937
938         unshift(@Tx,pop(@Tx));
939
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
947         &add    ($inp,64);
948
949   $Xi=0;
950 }
951
952 sub Xloop_avx()
953 { use integer;
954   my $body = shift;
955   my @insns = (&$body,&$body,&$body,&$body);    # 32 instructions
956   my ($a,$b,$c,$d,$e);
957
958          eval(shift(@insns));
959          eval(shift(@insns));
960         &vpshufb(@X[($Xi-3)&7],@X[($Xi-3)&7],@X[2]);
961          eval(shift(@insns));
962          eval(shift(@insns));
963         &vpaddd (@X[$Xi&7],@X[($Xi-4)&7],@Tx[1]);
964          eval(shift(@insns));
965          eval(shift(@insns));
966          eval(shift(@insns));
967          eval(shift(@insns));
968         &vmovdqa(eval(16*$Xi)."(%rsp)",@X[$Xi&7]);      # X[]+K xfer to IALU
969          eval(shift(@insns));
970          eval(shift(@insns));
971
972         foreach (@insns) { eval; }
973   $Xi++;
974 }
975
976 sub Xtail_avx()
977 { use integer;
978   my $body = shift;
979   my @insns = (&$body,&$body,&$body,&$body);    # 32 instructions
980   my ($a,$b,$c,$d,$e);
981
982         foreach (@insns) { eval; }
983 }
984
985 $code.=<<___;
986 .align  16
987 .Loop_avx:
988 ___
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"
1006
1007                                 $saved_j=$j; @saved_V=@V;
1008                                 $saved_r=$r; @saved_rndkey=@rndkey;
1009
1010         &Xloop_avx(\&body_20_39);
1011         &Xloop_avx(\&body_20_39);
1012         &Xloop_avx(\&body_20_39);
1013
1014 $code.=<<___;
1015         vmovups $iv,48($out,$in0)               # write output
1016         lea     64($in0),$in0
1017
1018         add     0($ctx),$A                      # update context
1019         add     4($ctx),@T[0]
1020         add     8($ctx),$C
1021         add     12($ctx),$D
1022         mov     $A,0($ctx)
1023         add     16($ctx),$E
1024         mov     @T[0],4($ctx)
1025         mov     @T[0],$B                        # magic seed
1026         mov     $C,8($ctx)
1027         mov     $D,12($ctx)
1028         mov     $E,16($ctx)
1029         jmp     .Loop_avx
1030
1031 .align  16
1032 .Ldone_avx:
1033 ___
1034                                 $jj=$j=$saved_j; @V=@saved_V;
1035                                 $r=$saved_r;     @rndkey=@saved_rndkey;
1036
1037         &Xtail_avx(\&body_20_39);
1038         &Xtail_avx(\&body_20_39);
1039         &Xtail_avx(\&body_20_39);
1040
1041 $code.=<<___;
1042         vmovups $iv,48($out,$in0)               # write output
1043         mov     88(%rsp),$ivp                   # restore $ivp
1044
1045         add     0($ctx),$A                      # update context
1046         add     4($ctx),@T[0]
1047         add     8($ctx),$C
1048         mov     $A,0($ctx)
1049         add     12($ctx),$D
1050         mov     @T[0],4($ctx)
1051         add     16($ctx),$E
1052         mov     $C,8($ctx)
1053         mov     $D,12($ctx)
1054         mov     $E,16($ctx)
1055         vmovups $iv,($ivp)                      # write IV
1056         vzeroall
1057 ___
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
1069 ___
1070 $code.=<<___;
1071         lea     `104+($win64?10*16:0)`(%rsp),%rsi
1072         mov     0(%rsi),%r15
1073         mov     8(%rsi),%r14
1074         mov     16(%rsi),%r13
1075         mov     24(%rsi),%r12
1076         mov     32(%rsi),%rbp
1077         mov     40(%rsi),%rbx
1078         lea     48(%rsi),%rsp
1079 .Lepilogue_avx:
1080         ret
1081 .size   aesni_cbc_sha1_enc_avx,.-aesni_cbc_sha1_enc_avx
1082 ___
1083 }
1084 $code.=<<___;
1085 .align  64
1086 K_XX_XX:
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
1092
1093 .asciz  "AESNI-CBC+SHA1 stitch for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
1094 .align  64
1095 ___
1096
1097 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
1098 #               CONTEXT *context,DISPATCHER_CONTEXT *disp)
1099 if ($win64) {
1100 $rec="%rcx";
1101 $frame="%rdx";
1102 $context="%r8";
1103 $disp="%r9";
1104
1105 $code.=<<___;
1106 .extern __imp_RtlVirtualUnwind
1107 .type   ssse3_handler,\@abi-omnipotent
1108 .align  16
1109 ssse3_handler:
1110         push    %rsi
1111         push    %rdi
1112         push    %rbx
1113         push    %rbp
1114         push    %r12
1115         push    %r13
1116         push    %r14
1117         push    %r15
1118         pushfq
1119         sub     \$64,%rsp
1120
1121         mov     120($context),%rax      # pull context->Rax
1122         mov     248($context),%rbx      # pull context->Rip
1123
1124         mov     8($disp),%rsi           # disp->ImageBase
1125         mov     56($disp),%r11          # disp->HandlerData
1126
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
1131
1132         mov     152($context),%rax      # pull context->Rsp
1133
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
1138
1139         lea     96(%rax),%rsi
1140         lea     512($context),%rdi      # &context.Xmm6
1141         mov     \$20,%ecx
1142         .long   0xa548f3fc              # cld; rep movsq
1143         lea     `104+10*16`(%rax),%rax  # adjust stack pointer
1144
1145         mov     0(%rax),%r15
1146         mov     8(%rax),%r14
1147         mov     16(%rax),%r13
1148         mov     24(%rax),%r12
1149         mov     32(%rax),%rbp
1150         mov     40(%rax),%rbx
1151         lea     48(%rax),%rax
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
1158
1159 .Lcommon_seh_tail:
1160         mov     8(%rax),%rdi
1161         mov     16(%rax),%rsi
1162         mov     %rax,152($context)      # restore context->Rsp
1163         mov     %rsi,168($context)      # restore context->Rsi
1164         mov     %rdi,176($context)      # restore context->Rdi
1165
1166         mov     40($disp),%rdi          # disp->ContextRecord
1167         mov     $context,%rsi           # context
1168         mov     \$154,%ecx              # sizeof(CONTEXT)
1169         .long   0xa548f3fc              # cld; rep movsq
1170
1171         mov     $disp,%rsi
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)
1184
1185         mov     \$1,%eax                # ExceptionContinueSearch
1186         add     \$64,%rsp
1187         popfq
1188         pop     %r15
1189         pop     %r14
1190         pop     %r13
1191         pop     %r12
1192         pop     %rbp
1193         pop     %rbx
1194         pop     %rdi
1195         pop     %rsi
1196         ret
1197 .size   ssse3_handler,.-ssse3_handler
1198
1199 .section        .pdata
1200 .align  4
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
1204 ___
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
1209 ___
1210 $code.=<<___;
1211 .section        .xdata
1212 .align  8
1213 .LSEH_info_aesni_cbc_sha1_enc_ssse3:
1214         .byte   9,0,0,0
1215         .rva    ssse3_handler
1216         .rva    .Lprologue_ssse3,.Lepilogue_ssse3       # HandlerData[]
1217 ___
1218 $code.=<<___ if ($avx);
1219 .LSEH_info_aesni_cbc_sha1_enc_avx:
1220         .byte   9,0,0,0
1221         .rva    ssse3_handler
1222         .rva    .Lprologue_avx,.Lepilogue_avx           # HandlerData[]
1223 ___
1224 }
1225
1226 ####################################################################
1227 sub rex {
1228   local *opcode=shift;
1229   my ($dst,$src)=@_;
1230   my $rex=0;
1231
1232     $rex|=0x04                  if($dst>=8);
1233     $rex|=0x01                  if($src>=8);
1234     push @opcode,$rex|0x40      if($rex);
1235 }
1236
1237 sub aesni {
1238   my $line=shift;
1239   my @opcode=(0x66);
1240
1241     if ($line=~/(aes[a-z]+)\s+%xmm([0-9]+),\s*%xmm([0-9]+)/) {
1242         my %opcodelet = (
1243                 "aesenc" => 0xdc,       "aesenclast" => 0xdd
1244         );
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);
1250     }
1251     return $line;
1252 }
1253
1254 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
1255 $code =~ s/\b(aes.*%xmm[0-9]+).*$/aesni($1)/gem;
1256
1257 print $code;
1258 close STDOUT;