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