a5ae180882dbdc374f3b16bdec98e1944d78e3db
[dragonfly.git] / crypto / openssl / crypto / modes / asm / ghash-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 # March, June 2010
11 #
12 # The module implements "4-bit" GCM GHASH function and underlying
13 # single multiplication operation in GF(2^128). "4-bit" means that
14 # it uses 256 bytes per-key table [+128 bytes shared table]. GHASH
15 # function features so called "528B" variant utilizing additional
16 # 256+16 bytes of per-key storage [+512 bytes shared table].
17 # Performance results are for this streamed GHASH subroutine and are
18 # expressed in cycles per processed byte, less is better:
19 #
20 #               gcc 3.4.x(*)    assembler
21 #
22 # P4            28.6            14.0            +100%
23 # Opteron       19.3            7.7             +150%
24 # Core2         17.8            8.1(**)         +120%
25 #
26 # (*)   comparison is not completely fair, because C results are
27 #       for vanilla "256B" implementation, while assembler results
28 #       are for "528B";-)
29 # (**)  it's mystery [to me] why Core2 result is not same as for
30 #       Opteron;
31
32 # May 2010
33 #
34 # Add PCLMULQDQ version performing at 2.02 cycles per processed byte.
35 # See ghash-x86.pl for background information and details about coding
36 # techniques.
37 #
38 # Special thanks to David Woodhouse <dwmw2@infradead.org> for
39 # providing access to a Westmere-based system on behalf of Intel
40 # Open Source Technology Centre.
41
42 $flavour = shift;
43 $output  = shift;
44 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
45
46 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
47
48 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
49 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
50 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
51 die "can't locate x86_64-xlate.pl";
52
53 open STDOUT,"| $^X $xlate $flavour $output";
54
55 # common register layout
56 $nlo="%rax";
57 $nhi="%rbx";
58 $Zlo="%r8";
59 $Zhi="%r9";
60 $tmp="%r10";
61 $rem_4bit = "%r11";
62
63 $Xi="%rdi";
64 $Htbl="%rsi";
65
66 # per-function register layout
67 $cnt="%rcx";
68 $rem="%rdx";
69
70 sub LB() { my $r=shift; $r =~ s/%[er]([a-d])x/%\1l/     or
71                         $r =~ s/%[er]([sd]i)/%\1l/      or
72                         $r =~ s/%[er](bp)/%\1l/         or
73                         $r =~ s/%(r[0-9]+)[d]?/%\1b/;   $r; }
74
75 sub AUTOLOAD()          # thunk [simplified] 32-bit style perlasm
76 { my $opcode = $AUTOLOAD; $opcode =~ s/.*:://;
77   my $arg = pop;
78     $arg = "\$$arg" if ($arg*1 eq $arg);
79     $code .= "\t$opcode\t".join(',',$arg,reverse @_)."\n";
80 }
81 \f
82 { my $N;
83   sub loop() {
84   my $inp = shift;
85
86         $N++;
87 $code.=<<___;
88         xor     $nlo,$nlo
89         xor     $nhi,$nhi
90         mov     `&LB("$Zlo")`,`&LB("$nlo")`
91         mov     `&LB("$Zlo")`,`&LB("$nhi")`
92         shl     \$4,`&LB("$nlo")`
93         mov     \$14,$cnt
94         mov     8($Htbl,$nlo),$Zlo
95         mov     ($Htbl,$nlo),$Zhi
96         and     \$0xf0,`&LB("$nhi")`
97         mov     $Zlo,$rem
98         jmp     .Loop$N
99
100 .align  16
101 .Loop$N:
102         shr     \$4,$Zlo
103         and     \$0xf,$rem
104         mov     $Zhi,$tmp
105         mov     ($inp,$cnt),`&LB("$nlo")`
106         shr     \$4,$Zhi
107         xor     8($Htbl,$nhi),$Zlo
108         shl     \$60,$tmp
109         xor     ($Htbl,$nhi),$Zhi
110         mov     `&LB("$nlo")`,`&LB("$nhi")`
111         xor     ($rem_4bit,$rem,8),$Zhi
112         mov     $Zlo,$rem
113         shl     \$4,`&LB("$nlo")`
114         xor     $tmp,$Zlo
115         dec     $cnt
116         js      .Lbreak$N
117
118         shr     \$4,$Zlo
119         and     \$0xf,$rem
120         mov     $Zhi,$tmp
121         shr     \$4,$Zhi
122         xor     8($Htbl,$nlo),$Zlo
123         shl     \$60,$tmp
124         xor     ($Htbl,$nlo),$Zhi
125         and     \$0xf0,`&LB("$nhi")`
126         xor     ($rem_4bit,$rem,8),$Zhi
127         mov     $Zlo,$rem
128         xor     $tmp,$Zlo
129         jmp     .Loop$N
130
131 .align  16
132 .Lbreak$N:
133         shr     \$4,$Zlo
134         and     \$0xf,$rem
135         mov     $Zhi,$tmp
136         shr     \$4,$Zhi
137         xor     8($Htbl,$nlo),$Zlo
138         shl     \$60,$tmp
139         xor     ($Htbl,$nlo),$Zhi
140         and     \$0xf0,`&LB("$nhi")`
141         xor     ($rem_4bit,$rem,8),$Zhi
142         mov     $Zlo,$rem
143         xor     $tmp,$Zlo
144
145         shr     \$4,$Zlo
146         and     \$0xf,$rem
147         mov     $Zhi,$tmp
148         shr     \$4,$Zhi
149         xor     8($Htbl,$nhi),$Zlo
150         shl     \$60,$tmp
151         xor     ($Htbl,$nhi),$Zhi
152         xor     $tmp,$Zlo
153         xor     ($rem_4bit,$rem,8),$Zhi
154
155         bswap   $Zlo
156         bswap   $Zhi
157 ___
158 }}
159
160 $code=<<___;
161 .text
162
163 .globl  gcm_gmult_4bit
164 .type   gcm_gmult_4bit,\@function,2
165 .align  16
166 gcm_gmult_4bit:
167         push    %rbx
168         push    %rbp            # %rbp and %r12 are pushed exclusively in
169         push    %r12            # order to reuse Win64 exception handler...
170 .Lgmult_prologue:
171
172         movzb   15($Xi),$Zlo
173         lea     .Lrem_4bit(%rip),$rem_4bit
174 ___
175         &loop   ($Xi);
176 $code.=<<___;
177         mov     $Zlo,8($Xi)
178         mov     $Zhi,($Xi)
179
180         mov     16(%rsp),%rbx
181         lea     24(%rsp),%rsp
182 .Lgmult_epilogue:
183         ret
184 .size   gcm_gmult_4bit,.-gcm_gmult_4bit
185 ___
186 \f
187 # per-function register layout
188 $inp="%rdx";
189 $len="%rcx";
190 $rem_8bit=$rem_4bit;
191
192 $code.=<<___;
193 .globl  gcm_ghash_4bit
194 .type   gcm_ghash_4bit,\@function,4
195 .align  16
196 gcm_ghash_4bit:
197         push    %rbx
198         push    %rbp
199         push    %r12
200         push    %r13
201         push    %r14
202         push    %r15
203         sub     \$280,%rsp
204 .Lghash_prologue:
205         mov     $inp,%r14               # reassign couple of args
206         mov     $len,%r15
207 ___
208 { my $inp="%r14";
209   my $dat="%edx";
210   my $len="%r15";
211   my @nhi=("%ebx","%ecx");
212   my @rem=("%r12","%r13");
213   my $Hshr4="%rbp";
214
215         &sub    ($Htbl,-128);           # size optimization
216         &lea    ($Hshr4,"16+128(%rsp)");
217         { my @lo =($nlo,$nhi);
218           my @hi =($Zlo,$Zhi);
219
220           &xor  ($dat,$dat);
221           for ($i=0,$j=-2;$i<18;$i++,$j++) {
222             &mov        ("$j(%rsp)",&LB($dat))          if ($i>1);
223             &or         ($lo[0],$tmp)                   if ($i>1);
224             &mov        (&LB($dat),&LB($lo[1]))         if ($i>0 && $i<17);
225             &shr        ($lo[1],4)                      if ($i>0 && $i<17);
226             &mov        ($tmp,$hi[1])                   if ($i>0 && $i<17);
227             &shr        ($hi[1],4)                      if ($i>0 && $i<17);
228             &mov        ("8*$j($Hshr4)",$hi[0])         if ($i>1);
229             &mov        ($hi[0],"16*$i+0-128($Htbl)")   if ($i<16);
230             &shl        (&LB($dat),4)                   if ($i>0 && $i<17);
231             &mov        ("8*$j-128($Hshr4)",$lo[0])     if ($i>1);
232             &mov        ($lo[0],"16*$i+8-128($Htbl)")   if ($i<16);
233             &shl        ($tmp,60)                       if ($i>0 && $i<17);
234
235             push        (@lo,shift(@lo));
236             push        (@hi,shift(@hi));
237           }
238         }
239         &add    ($Htbl,-128);
240         &mov    ($Zlo,"8($Xi)");
241         &mov    ($Zhi,"0($Xi)");
242         &add    ($len,$inp);            # pointer to the end of data
243         &lea    ($rem_8bit,".Lrem_8bit(%rip)");
244         &jmp    (".Louter_loop");
245
246 $code.=".align  16\n.Louter_loop:\n";
247         &xor    ($Zhi,"($inp)");
248         &mov    ("%rdx","8($inp)");
249         &lea    ($inp,"16($inp)");
250         &xor    ("%rdx",$Zlo);
251         &mov    ("($Xi)",$Zhi);
252         &mov    ("8($Xi)","%rdx");
253         &shr    ("%rdx",32);
254
255         &xor    ($nlo,$nlo);
256         &rol    ($dat,8);
257         &mov    (&LB($nlo),&LB($dat));
258         &movz   ($nhi[0],&LB($dat));
259         &shl    (&LB($nlo),4);
260         &shr    ($nhi[0],4);
261
262         for ($j=11,$i=0;$i<15;$i++) {
263             &rol        ($dat,8);
264             &xor        ($Zlo,"8($Htbl,$nlo)")                  if ($i>0);
265             &xor        ($Zhi,"($Htbl,$nlo)")                   if ($i>0);
266             &mov        ($Zlo,"8($Htbl,$nlo)")                  if ($i==0);
267             &mov        ($Zhi,"($Htbl,$nlo)")                   if ($i==0);
268
269             &mov        (&LB($nlo),&LB($dat));
270             &xor        ($Zlo,$tmp)                             if ($i>0);
271             &movzw      ($rem[1],"($rem_8bit,$rem[1],2)")       if ($i>0);
272
273             &movz       ($nhi[1],&LB($dat));
274             &shl        (&LB($nlo),4);
275             &movzb      ($rem[0],"(%rsp,$nhi[0])");
276
277             &shr        ($nhi[1],4)                             if ($i<14);
278             &and        ($nhi[1],0xf0)                          if ($i==14);
279             &shl        ($rem[1],48)                            if ($i>0);
280             &xor        ($rem[0],$Zlo);
281
282             &mov        ($tmp,$Zhi);
283             &xor        ($Zhi,$rem[1])                          if ($i>0);
284             &shr        ($Zlo,8);
285
286             &movz       ($rem[0],&LB($rem[0]));
287             &mov        ($dat,"$j($Xi)")                        if (--$j%4==0);
288             &shr        ($Zhi,8);
289
290             &xor        ($Zlo,"-128($Hshr4,$nhi[0],8)");
291             &shl        ($tmp,56);
292             &xor        ($Zhi,"($Hshr4,$nhi[0],8)");
293
294             unshift     (@nhi,pop(@nhi));               # "rotate" registers
295             unshift     (@rem,pop(@rem));
296         }
297         &movzw  ($rem[1],"($rem_8bit,$rem[1],2)");
298         &xor    ($Zlo,"8($Htbl,$nlo)");
299         &xor    ($Zhi,"($Htbl,$nlo)");
300
301         &shl    ($rem[1],48);
302         &xor    ($Zlo,$tmp);
303
304         &xor    ($Zhi,$rem[1]);
305         &movz   ($rem[0],&LB($Zlo));
306         &shr    ($Zlo,4);
307
308         &mov    ($tmp,$Zhi);
309         &shl    (&LB($rem[0]),4);
310         &shr    ($Zhi,4);
311
312         &xor    ($Zlo,"8($Htbl,$nhi[0])");
313         &movzw  ($rem[0],"($rem_8bit,$rem[0],2)");
314         &shl    ($tmp,60);
315
316         &xor    ($Zhi,"($Htbl,$nhi[0])");
317         &xor    ($Zlo,$tmp);
318         &shl    ($rem[0],48);
319
320         &bswap  ($Zlo);
321         &xor    ($Zhi,$rem[0]);
322
323         &bswap  ($Zhi);
324         &cmp    ($inp,$len);
325         &jb     (".Louter_loop");
326 }
327 $code.=<<___;
328         mov     $Zlo,8($Xi)
329         mov     $Zhi,($Xi)
330
331         lea     280(%rsp),%rsi
332         mov     0(%rsi),%r15
333         mov     8(%rsi),%r14
334         mov     16(%rsi),%r13
335         mov     24(%rsi),%r12
336         mov     32(%rsi),%rbp
337         mov     40(%rsi),%rbx
338         lea     48(%rsi),%rsp
339 .Lghash_epilogue:
340         ret
341 .size   gcm_ghash_4bit,.-gcm_ghash_4bit
342 ___
343 \f
344 ######################################################################
345 # PCLMULQDQ version.
346
347 @_4args=$win64? ("%rcx","%rdx","%r8", "%r9") :  # Win64 order
348                 ("%rdi","%rsi","%rdx","%rcx");  # Unix order
349
350 ($Xi,$Xhi)=("%xmm0","%xmm1");   $Hkey="%xmm2";
351 ($T1,$T2,$T3)=("%xmm3","%xmm4","%xmm5");
352
353 sub clmul64x64_T2 {     # minimal register pressure
354 my ($Xhi,$Xi,$Hkey,$modulo)=@_;
355
356 $code.=<<___ if (!defined($modulo));
357         movdqa          $Xi,$Xhi                #
358         pshufd          \$0b01001110,$Xi,$T1
359         pshufd          \$0b01001110,$Hkey,$T2
360         pxor            $Xi,$T1                 #
361         pxor            $Hkey,$T2
362 ___
363 $code.=<<___;
364         pclmulqdq       \$0x00,$Hkey,$Xi        #######
365         pclmulqdq       \$0x11,$Hkey,$Xhi       #######
366         pclmulqdq       \$0x00,$T2,$T1          #######
367         pxor            $Xi,$T1                 #
368         pxor            $Xhi,$T1                #
369
370         movdqa          $T1,$T2                 #
371         psrldq          \$8,$T1
372         pslldq          \$8,$T2                 #
373         pxor            $T1,$Xhi
374         pxor            $T2,$Xi                 #
375 ___
376 }
377
378 sub reduction_alg9 {    # 17/13 times faster than Intel version
379 my ($Xhi,$Xi) = @_;
380
381 $code.=<<___;
382         # 1st phase
383         movdqa          $Xi,$T1                 #
384         psllq           \$1,$Xi
385         pxor            $T1,$Xi                 #
386         psllq           \$5,$Xi                 #
387         pxor            $T1,$Xi                 #
388         psllq           \$57,$Xi                #
389         movdqa          $Xi,$T2                 #
390         pslldq          \$8,$Xi
391         psrldq          \$8,$T2                 #       
392         pxor            $T1,$Xi
393         pxor            $T2,$Xhi                #
394
395         # 2nd phase
396         movdqa          $Xi,$T2
397         psrlq           \$5,$Xi
398         pxor            $T2,$Xi                 #
399         psrlq           \$1,$Xi                 #
400         pxor            $T2,$Xi                 #
401         pxor            $Xhi,$T2
402         psrlq           \$1,$Xi                 #
403         pxor            $T2,$Xi                 #
404 ___
405 }
406 \f
407 { my ($Htbl,$Xip)=@_4args;
408
409 $code.=<<___;
410 .globl  gcm_init_clmul
411 .type   gcm_init_clmul,\@abi-omnipotent
412 .align  16
413 gcm_init_clmul:
414         movdqu          ($Xip),$Hkey
415         pshufd          \$0b01001110,$Hkey,$Hkey        # dword swap
416
417         # <<1 twist
418         pshufd          \$0b11111111,$Hkey,$T2  # broadcast uppermost dword
419         movdqa          $Hkey,$T1
420         psllq           \$1,$Hkey
421         pxor            $T3,$T3                 #
422         psrlq           \$63,$T1
423         pcmpgtd         $T2,$T3                 # broadcast carry bit
424         pslldq          \$8,$T1
425         por             $T1,$Hkey               # H<<=1
426
427         # magic reduction
428         pand            .L0x1c2_polynomial(%rip),$T3
429         pxor            $T3,$Hkey               # if(carry) H^=0x1c2_polynomial
430
431         # calculate H^2
432         movdqa          $Hkey,$Xi
433 ___
434         &clmul64x64_T2  ($Xhi,$Xi,$Hkey);
435         &reduction_alg9 ($Xhi,$Xi);
436 $code.=<<___;
437         movdqu          $Hkey,($Htbl)           # save H
438         movdqu          $Xi,16($Htbl)           # save H^2
439         ret
440 .size   gcm_init_clmul,.-gcm_init_clmul
441 ___
442 }
443
444 { my ($Xip,$Htbl)=@_4args;
445
446 $code.=<<___;
447 .globl  gcm_gmult_clmul
448 .type   gcm_gmult_clmul,\@abi-omnipotent
449 .align  16
450 gcm_gmult_clmul:
451         movdqu          ($Xip),$Xi
452         movdqa          .Lbswap_mask(%rip),$T3
453         movdqu          ($Htbl),$Hkey
454         pshufb          $T3,$Xi
455 ___
456         &clmul64x64_T2  ($Xhi,$Xi,$Hkey);
457         &reduction_alg9 ($Xhi,$Xi);
458 $code.=<<___;
459         pshufb          $T3,$Xi
460         movdqu          $Xi,($Xip)
461         ret
462 .size   gcm_gmult_clmul,.-gcm_gmult_clmul
463 ___
464 }
465 \f
466 { my ($Xip,$Htbl,$inp,$len)=@_4args;
467   my $Xn="%xmm6";
468   my $Xhn="%xmm7";
469   my $Hkey2="%xmm8";
470   my $T1n="%xmm9";
471   my $T2n="%xmm10";
472
473 $code.=<<___;
474 .globl  gcm_ghash_clmul
475 .type   gcm_ghash_clmul,\@abi-omnipotent
476 .align  16
477 gcm_ghash_clmul:
478 ___
479 $code.=<<___ if ($win64);
480 .LSEH_begin_gcm_ghash_clmul:
481         # I can't trust assembler to use specific encoding:-(
482         .byte   0x48,0x83,0xec,0x58             #sub    \$0x58,%rsp
483         .byte   0x0f,0x29,0x34,0x24             #movaps %xmm6,(%rsp)
484         .byte   0x0f,0x29,0x7c,0x24,0x10        #movdqa %xmm7,0x10(%rsp)
485         .byte   0x44,0x0f,0x29,0x44,0x24,0x20   #movaps %xmm8,0x20(%rsp)
486         .byte   0x44,0x0f,0x29,0x4c,0x24,0x30   #movaps %xmm9,0x30(%rsp)
487         .byte   0x44,0x0f,0x29,0x54,0x24,0x40   #movaps %xmm10,0x40(%rsp)
488 ___
489 $code.=<<___;
490         movdqa          .Lbswap_mask(%rip),$T3
491
492         movdqu          ($Xip),$Xi
493         movdqu          ($Htbl),$Hkey
494         pshufb          $T3,$Xi
495
496         sub             \$0x10,$len
497         jz              .Lodd_tail
498
499         movdqu          16($Htbl),$Hkey2
500         #######
501         # Xi+2 =[H*(Ii+1 + Xi+1)] mod P =
502         #       [(H*Ii+1) + (H*Xi+1)] mod P =
503         #       [(H*Ii+1) + H^2*(Ii+Xi)] mod P
504         #
505         movdqu          ($inp),$T1              # Ii
506         movdqu          16($inp),$Xn            # Ii+1
507         pshufb          $T3,$T1
508         pshufb          $T3,$Xn
509         pxor            $T1,$Xi                 # Ii+Xi
510 ___
511         &clmul64x64_T2  ($Xhn,$Xn,$Hkey);       # H*Ii+1
512 $code.=<<___;
513         movdqa          $Xi,$Xhi                #
514         pshufd          \$0b01001110,$Xi,$T1
515         pshufd          \$0b01001110,$Hkey2,$T2
516         pxor            $Xi,$T1                 #
517         pxor            $Hkey2,$T2
518
519         lea             32($inp),$inp           # i+=2
520         sub             \$0x20,$len
521         jbe             .Leven_tail
522
523 .Lmod_loop:
524 ___
525         &clmul64x64_T2  ($Xhi,$Xi,$Hkey2,1);    # H^2*(Ii+Xi)
526 $code.=<<___;
527         movdqu          ($inp),$T1              # Ii
528         pxor            $Xn,$Xi                 # (H*Ii+1) + H^2*(Ii+Xi)
529         pxor            $Xhn,$Xhi
530
531         movdqu          16($inp),$Xn            # Ii+1
532         pshufb          $T3,$T1
533         pshufb          $T3,$Xn
534
535         movdqa          $Xn,$Xhn                #
536         pshufd          \$0b01001110,$Xn,$T1n
537         pshufd          \$0b01001110,$Hkey,$T2n
538         pxor            $Xn,$T1n                #
539         pxor            $Hkey,$T2n
540          pxor           $T1,$Xhi                # "Ii+Xi", consume early
541
542           movdqa        $Xi,$T1                 # 1st phase
543           psllq         \$1,$Xi
544           pxor          $T1,$Xi                 #
545           psllq         \$5,$Xi                 #
546           pxor          $T1,$Xi                 #
547         pclmulqdq       \$0x00,$Hkey,$Xn        #######
548           psllq         \$57,$Xi                #
549           movdqa        $Xi,$T2                 #
550           pslldq        \$8,$Xi
551           psrldq        \$8,$T2                 #       
552           pxor          $T1,$Xi
553           pxor          $T2,$Xhi                #
554
555         pclmulqdq       \$0x11,$Hkey,$Xhn       #######
556           movdqa        $Xi,$T2                 # 2nd phase
557           psrlq         \$5,$Xi
558           pxor          $T2,$Xi                 #
559           psrlq         \$1,$Xi                 #
560           pxor          $T2,$Xi                 #
561           pxor          $Xhi,$T2
562           psrlq         \$1,$Xi                 #
563           pxor          $T2,$Xi                 #
564
565         pclmulqdq       \$0x00,$T2n,$T1n        #######
566          movdqa         $Xi,$Xhi                #
567          pshufd         \$0b01001110,$Xi,$T1
568          pshufd         \$0b01001110,$Hkey2,$T2
569          pxor           $Xi,$T1                 #
570          pxor           $Hkey2,$T2
571
572         pxor            $Xn,$T1n                #
573         pxor            $Xhn,$T1n               #
574         movdqa          $T1n,$T2n               #
575         psrldq          \$8,$T1n
576         pslldq          \$8,$T2n                #
577         pxor            $T1n,$Xhn
578         pxor            $T2n,$Xn                #
579
580         lea             32($inp),$inp
581         sub             \$0x20,$len
582         ja              .Lmod_loop
583
584 .Leven_tail:
585 ___
586         &clmul64x64_T2  ($Xhi,$Xi,$Hkey2,1);    # H^2*(Ii+Xi)
587 $code.=<<___;
588         pxor            $Xn,$Xi                 # (H*Ii+1) + H^2*(Ii+Xi)
589         pxor            $Xhn,$Xhi
590 ___
591         &reduction_alg9 ($Xhi,$Xi);
592 $code.=<<___;
593         test            $len,$len
594         jnz             .Ldone
595
596 .Lodd_tail:
597         movdqu          ($inp),$T1              # Ii
598         pshufb          $T3,$T1
599         pxor            $T1,$Xi                 # Ii+Xi
600 ___
601         &clmul64x64_T2  ($Xhi,$Xi,$Hkey);       # H*(Ii+Xi)
602         &reduction_alg9 ($Xhi,$Xi);
603 $code.=<<___;
604 .Ldone:
605         pshufb          $T3,$Xi
606         movdqu          $Xi,($Xip)
607 ___
608 $code.=<<___ if ($win64);
609         movaps  (%rsp),%xmm6
610         movaps  0x10(%rsp),%xmm7
611         movaps  0x20(%rsp),%xmm8
612         movaps  0x30(%rsp),%xmm9
613         movaps  0x40(%rsp),%xmm10
614         add     \$0x58,%rsp
615 ___
616 $code.=<<___;
617         ret
618 .LSEH_end_gcm_ghash_clmul:
619 .size   gcm_ghash_clmul,.-gcm_ghash_clmul
620 ___
621 }
622
623 $code.=<<___;
624 .align  64
625 .Lbswap_mask:
626         .byte   15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
627 .L0x1c2_polynomial:
628         .byte   1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xc2
629 .align  64
630 .type   .Lrem_4bit,\@object
631 .Lrem_4bit:
632         .long   0,`0x0000<<16`,0,`0x1C20<<16`,0,`0x3840<<16`,0,`0x2460<<16`
633         .long   0,`0x7080<<16`,0,`0x6CA0<<16`,0,`0x48C0<<16`,0,`0x54E0<<16`
634         .long   0,`0xE100<<16`,0,`0xFD20<<16`,0,`0xD940<<16`,0,`0xC560<<16`
635         .long   0,`0x9180<<16`,0,`0x8DA0<<16`,0,`0xA9C0<<16`,0,`0xB5E0<<16`
636 .type   .Lrem_8bit,\@object
637 .Lrem_8bit:
638         .value  0x0000,0x01C2,0x0384,0x0246,0x0708,0x06CA,0x048C,0x054E
639         .value  0x0E10,0x0FD2,0x0D94,0x0C56,0x0918,0x08DA,0x0A9C,0x0B5E
640         .value  0x1C20,0x1DE2,0x1FA4,0x1E66,0x1B28,0x1AEA,0x18AC,0x196E
641         .value  0x1230,0x13F2,0x11B4,0x1076,0x1538,0x14FA,0x16BC,0x177E
642         .value  0x3840,0x3982,0x3BC4,0x3A06,0x3F48,0x3E8A,0x3CCC,0x3D0E
643         .value  0x3650,0x3792,0x35D4,0x3416,0x3158,0x309A,0x32DC,0x331E
644         .value  0x2460,0x25A2,0x27E4,0x2626,0x2368,0x22AA,0x20EC,0x212E
645         .value  0x2A70,0x2BB2,0x29F4,0x2836,0x2D78,0x2CBA,0x2EFC,0x2F3E
646         .value  0x7080,0x7142,0x7304,0x72C6,0x7788,0x764A,0x740C,0x75CE
647         .value  0x7E90,0x7F52,0x7D14,0x7CD6,0x7998,0x785A,0x7A1C,0x7BDE
648         .value  0x6CA0,0x6D62,0x6F24,0x6EE6,0x6BA8,0x6A6A,0x682C,0x69EE
649         .value  0x62B0,0x6372,0x6134,0x60F6,0x65B8,0x647A,0x663C,0x67FE
650         .value  0x48C0,0x4902,0x4B44,0x4A86,0x4FC8,0x4E0A,0x4C4C,0x4D8E
651         .value  0x46D0,0x4712,0x4554,0x4496,0x41D8,0x401A,0x425C,0x439E
652         .value  0x54E0,0x5522,0x5764,0x56A6,0x53E8,0x522A,0x506C,0x51AE
653         .value  0x5AF0,0x5B32,0x5974,0x58B6,0x5DF8,0x5C3A,0x5E7C,0x5FBE
654         .value  0xE100,0xE0C2,0xE284,0xE346,0xE608,0xE7CA,0xE58C,0xE44E
655         .value  0xEF10,0xEED2,0xEC94,0xED56,0xE818,0xE9DA,0xEB9C,0xEA5E
656         .value  0xFD20,0xFCE2,0xFEA4,0xFF66,0xFA28,0xFBEA,0xF9AC,0xF86E
657         .value  0xF330,0xF2F2,0xF0B4,0xF176,0xF438,0xF5FA,0xF7BC,0xF67E
658         .value  0xD940,0xD882,0xDAC4,0xDB06,0xDE48,0xDF8A,0xDDCC,0xDC0E
659         .value  0xD750,0xD692,0xD4D4,0xD516,0xD058,0xD19A,0xD3DC,0xD21E
660         .value  0xC560,0xC4A2,0xC6E4,0xC726,0xC268,0xC3AA,0xC1EC,0xC02E
661         .value  0xCB70,0xCAB2,0xC8F4,0xC936,0xCC78,0xCDBA,0xCFFC,0xCE3E
662         .value  0x9180,0x9042,0x9204,0x93C6,0x9688,0x974A,0x950C,0x94CE
663         .value  0x9F90,0x9E52,0x9C14,0x9DD6,0x9898,0x995A,0x9B1C,0x9ADE
664         .value  0x8DA0,0x8C62,0x8E24,0x8FE6,0x8AA8,0x8B6A,0x892C,0x88EE
665         .value  0x83B0,0x8272,0x8034,0x81F6,0x84B8,0x857A,0x873C,0x86FE
666         .value  0xA9C0,0xA802,0xAA44,0xAB86,0xAEC8,0xAF0A,0xAD4C,0xAC8E
667         .value  0xA7D0,0xA612,0xA454,0xA596,0xA0D8,0xA11A,0xA35C,0xA29E
668         .value  0xB5E0,0xB422,0xB664,0xB7A6,0xB2E8,0xB32A,0xB16C,0xB0AE
669         .value  0xBBF0,0xBA32,0xB874,0xB9B6,0xBCF8,0xBD3A,0xBF7C,0xBEBE
670
671 .asciz  "GHASH for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
672 .align  64
673 ___
674 \f
675 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
676 #               CONTEXT *context,DISPATCHER_CONTEXT *disp)
677 if ($win64) {
678 $rec="%rcx";
679 $frame="%rdx";
680 $context="%r8";
681 $disp="%r9";
682
683 $code.=<<___;
684 .extern __imp_RtlVirtualUnwind
685 .type   se_handler,\@abi-omnipotent
686 .align  16
687 se_handler:
688         push    %rsi
689         push    %rdi
690         push    %rbx
691         push    %rbp
692         push    %r12
693         push    %r13
694         push    %r14
695         push    %r15
696         pushfq
697         sub     \$64,%rsp
698
699         mov     120($context),%rax      # pull context->Rax
700         mov     248($context),%rbx      # pull context->Rip
701
702         mov     8($disp),%rsi           # disp->ImageBase
703         mov     56($disp),%r11          # disp->HandlerData
704
705         mov     0(%r11),%r10d           # HandlerData[0]
706         lea     (%rsi,%r10),%r10        # prologue label
707         cmp     %r10,%rbx               # context->Rip<prologue label
708         jb      .Lin_prologue
709
710         mov     152($context),%rax      # pull context->Rsp
711
712         mov     4(%r11),%r10d           # HandlerData[1]
713         lea     (%rsi,%r10),%r10        # epilogue label
714         cmp     %r10,%rbx               # context->Rip>=epilogue label
715         jae     .Lin_prologue
716
717         lea     24(%rax),%rax           # adjust "rsp"
718
719         mov     -8(%rax),%rbx
720         mov     -16(%rax),%rbp
721         mov     -24(%rax),%r12
722         mov     %rbx,144($context)      # restore context->Rbx
723         mov     %rbp,160($context)      # restore context->Rbp
724         mov     %r12,216($context)      # restore context->R12
725
726 .Lin_prologue:
727         mov     8(%rax),%rdi
728         mov     16(%rax),%rsi
729         mov     %rax,152($context)      # restore context->Rsp
730         mov     %rsi,168($context)      # restore context->Rsi
731         mov     %rdi,176($context)      # restore context->Rdi
732
733         mov     40($disp),%rdi          # disp->ContextRecord
734         mov     $context,%rsi           # context
735         mov     \$`1232/8`,%ecx         # sizeof(CONTEXT)
736         .long   0xa548f3fc              # cld; rep movsq
737
738         mov     $disp,%rsi
739         xor     %rcx,%rcx               # arg1, UNW_FLAG_NHANDLER
740         mov     8(%rsi),%rdx            # arg2, disp->ImageBase
741         mov     0(%rsi),%r8             # arg3, disp->ControlPc
742         mov     16(%rsi),%r9            # arg4, disp->FunctionEntry
743         mov     40(%rsi),%r10           # disp->ContextRecord
744         lea     56(%rsi),%r11           # &disp->HandlerData
745         lea     24(%rsi),%r12           # &disp->EstablisherFrame
746         mov     %r10,32(%rsp)           # arg5
747         mov     %r11,40(%rsp)           # arg6
748         mov     %r12,48(%rsp)           # arg7
749         mov     %rcx,56(%rsp)           # arg8, (NULL)
750         call    *__imp_RtlVirtualUnwind(%rip)
751
752         mov     \$1,%eax                # ExceptionContinueSearch
753         add     \$64,%rsp
754         popfq
755         pop     %r15
756         pop     %r14
757         pop     %r13
758         pop     %r12
759         pop     %rbp
760         pop     %rbx
761         pop     %rdi
762         pop     %rsi
763         ret
764 .size   se_handler,.-se_handler
765
766 .section        .pdata
767 .align  4
768         .rva    .LSEH_begin_gcm_gmult_4bit
769         .rva    .LSEH_end_gcm_gmult_4bit
770         .rva    .LSEH_info_gcm_gmult_4bit
771
772         .rva    .LSEH_begin_gcm_ghash_4bit
773         .rva    .LSEH_end_gcm_ghash_4bit
774         .rva    .LSEH_info_gcm_ghash_4bit
775
776         .rva    .LSEH_begin_gcm_ghash_clmul
777         .rva    .LSEH_end_gcm_ghash_clmul
778         .rva    .LSEH_info_gcm_ghash_clmul
779
780 .section        .xdata
781 .align  8
782 .LSEH_info_gcm_gmult_4bit:
783         .byte   9,0,0,0
784         .rva    se_handler
785         .rva    .Lgmult_prologue,.Lgmult_epilogue       # HandlerData
786 .LSEH_info_gcm_ghash_4bit:
787         .byte   9,0,0,0
788         .rva    se_handler
789         .rva    .Lghash_prologue,.Lghash_epilogue       # HandlerData
790 .LSEH_info_gcm_ghash_clmul:
791         .byte   0x01,0x1f,0x0b,0x00
792         .byte   0x1f,0xa8,0x04,0x00     #movaps 0x40(rsp),xmm10
793         .byte   0x19,0x98,0x03,0x00     #movaps 0x30(rsp),xmm9
794         .byte   0x13,0x88,0x02,0x00     #movaps 0x20(rsp),xmm8
795         .byte   0x0d,0x78,0x01,0x00     #movaps 0x10(rsp),xmm7
796         .byte   0x08,0x68,0x00,0x00     #movaps (rsp),xmm6
797         .byte   0x04,0xa2,0x00,0x00     #sub    rsp,0x58
798 ___
799 }
800 \f
801 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
802
803 print $code;
804
805 close STDOUT;