3 # ====================================================================
4 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
5 # project. The module is, however, dual licensed under OpenSSL and
6 # CRYPTOGAMS licenses depending on where you obtain it. For further
7 # details see http://www.openssl.org/~appro/cryptogams/.
8 # ====================================================================
12 # The module implements "4-bit" GCM GHASH function and underlying
13 # single multiplication operation in GF(2^128). "4-bit" means that it
14 # uses 256 bytes per-key table [+128 bytes shared table]. Performance
15 # results are for streamed GHASH subroutine and are expressed in
16 # cycles per processed byte, less is better:
20 # Opteron 18.5 10.2 +80%
21 # Core2 17.5 11.0 +59%
25 # Add PCLMULQDQ version performing at 2.07 cycles per processed byte.
26 # See ghash-x86.pl for background information and details about coding
31 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
33 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
35 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
36 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
37 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
38 die "can't locate x86_64-xlate.pl";
40 open STDOUT,"| $^X $xlate $flavour $output";
42 # common register layout
53 # per-function register layout
57 sub lo() { my $r=shift; $r =~ s/%[er]([a-d])x/%\1l/;
58 $r =~ s/%[er]([sd]i)/%\1l/;
59 $r =~ s/%(r[0-9]+)[d]?/%\1b/; $r; }
69 mov `&lo("$Zlo")`,`&lo("$nlo")`
70 mov `&lo("$Zlo")`,`&lo("$nhi")`
73 mov 8($Htbl,$nlo),$Zlo
75 and \$0xf0,`&lo("$nhi")`
84 mov ($inp,$cnt),`&lo("$nlo")`
86 xor 8($Htbl,$nhi),$Zlo
89 mov `&lo("$nlo")`,`&lo("$nhi")`
90 xor ($rem_4bit,$rem,8),$Zhi
101 xor 8($Htbl,$nlo),$Zlo
103 xor ($Htbl,$nlo),$Zhi
104 and \$0xf0,`&lo("$nhi")`
105 xor ($rem_4bit,$rem,8),$Zhi
116 xor 8($Htbl,$nlo),$Zlo
118 xor ($Htbl,$nlo),$Zhi
119 and \$0xf0,`&lo("$nhi")`
120 xor ($rem_4bit,$rem,8),$Zhi
128 xor 8($Htbl,$nhi),$Zlo
130 xor ($Htbl,$nhi),$Zhi
132 xor ($rem_4bit,$rem,8),$Zhi
142 .globl gcm_gmult_4bit
143 .type gcm_gmult_4bit,\@function,2
147 push %rbp # %rbp and %r12 are pushed exclusively in
148 push %r12 # order to reuse Win64 exception handler...
152 lea .Lrem_4bit(%rip),$rem_4bit
163 .size gcm_gmult_4bit,.-gcm_gmult_4bit
166 # per-function register layout
174 .globl gcm_ghash_4bit
175 .type gcm_ghash_4bit,\@function,4
186 lea .Lrem_4bit(%rip),$rem_4bit
210 .size gcm_ghash_4bit,.-gcm_ghash_4bit
213 ######################################################################
216 @_4args=$win64? ("%rcx","%rdx","%r8", "%r9") : # Win64 order
217 ("%rdi","%rsi","%rdx","%rcx"); # Unix order
219 ($Xi,$Xhi)=("%xmm0","%xmm1"); $Hkey="%xmm2";
220 ($T1,$T2,$T3)=("%xmm3","%xmm4","%xmm5");
222 sub clmul64x64_T2 { # minimal register pressure
223 my ($Xhi,$Xi,$Hkey,$modulo)=@_;
225 $code.=<<___ if (!defined($modulo));
227 pshufd \$0b01001110,$Xi,$T1
228 pshufd \$0b01001110,$Hkey,$T2
233 pclmulqdq \$0x00,$Hkey,$Xi #######
234 pclmulqdq \$0x11,$Hkey,$Xhi #######
235 pclmulqdq \$0x00,$T2,$T1 #######
247 sub reduction_alg9 { # 17/13 times faster than Intel version
276 { my ($Htbl,$Xip)=@_4args;
279 .globl gcm_init_clmul
280 .type gcm_init_clmul,\@abi-omnipotent
284 pshufd \$0b01001110,$Hkey,$Hkey # dword swap
287 pshufd \$0b11111111,$Hkey,$T2 # broadcast uppermost dword
292 pcmpgtd $T2,$T3 # broadcast carry bit
294 por $T1,$Hkey # H<<=1
297 pand .L0x1c2_polynomial(%rip),$T3
298 pxor $T3,$Hkey # if(carry) H^=0x1c2_polynomial
303 &clmul64x64_T2 ($Xhi,$Xi,$Hkey);
304 &reduction_alg9 ($Xhi,$Xi);
306 movdqu $Hkey,($Htbl) # save H
307 movdqu $Xi,16($Htbl) # save H^2
309 .size gcm_init_clmul,.-gcm_init_clmul
313 { my ($Xip,$Htbl)=@_4args;
316 .globl gcm_gmult_clmul
317 .type gcm_gmult_clmul,\@abi-omnipotent
321 movdqa .Lbswap_mask(%rip),$T3
325 &clmul64x64_T2 ($Xhi,$Xi,$Hkey);
326 &reduction_alg9 ($Xhi,$Xi);
331 .size gcm_gmult_clmul,.-gcm_gmult_clmul
335 { my ($Xip,$Htbl,$inp,$len)=@_4args;
343 .globl gcm_ghash_clmul
344 .type gcm_ghash_clmul,\@abi-omnipotent
348 $code.=<<___ if ($win64);
349 .LSEH_begin_gcm_ghash_clmul:
350 # I can't trust assembler to use specific encoding:-(
351 .byte 0x48,0x83,0xec,0x58 #sub \$0x58,%rsp
352 .byte 0x0f,0x29,0x34,0x24 #movaps %xmm6,(%rsp)
353 .byte 0x0f,0x29,0x7c,0x24,0x10 #movdqa %xmm7,0x10(%rsp)
354 .byte 0x44,0x0f,0x29,0x44,0x24,0x20 #movaps %xmm8,0x20(%rsp)
355 .byte 0x44,0x0f,0x29,0x4c,0x24,0x30 #movaps %xmm9,0x30(%rsp)
356 .byte 0x44,0x0f,0x29,0x54,0x24,0x40 #movaps %xmm10,0x40(%rsp)
359 movdqa .Lbswap_mask(%rip),$T3
368 movdqu 16($Htbl),$Hkey2
370 # Xi+2 =[H*(Ii+1 + Xi+1)] mod P =
371 # [(H*Ii+1) + (H*Xi+1)] mod P =
372 # [(H*Ii+1) + H^2*(Ii+Xi)] mod P
374 movdqu ($inp),$T1 # Ii
375 movdqu 16($inp),$Xn # Ii+1
380 &clmul64x64_T2 ($Xhn,$Xn,$Hkey); # H*Ii+1
383 pshufd \$0b01001110,$Xi,$T1
384 pshufd \$0b01001110,$Hkey2,$T2
388 lea 32($inp),$inp # i+=2
394 &clmul64x64_T2 ($Xhi,$Xi,$Hkey2,1); # H^2*(Ii+Xi)
396 movdqu ($inp),$T1 # Ii
397 pxor $Xn,$Xi # (H*Ii+1) + H^2*(Ii+Xi)
400 movdqu 16($inp),$Xn # Ii+1
405 pshufd \$0b01001110,$Xn,$T1n
406 pshufd \$0b01001110,$Hkey,$T2n
409 pxor $T1,$Xhi # "Ii+Xi", consume early
411 movdqa $Xi,$T1 # 1st phase
416 pclmulqdq \$0x00,$Hkey,$Xn #######
424 pclmulqdq \$0x11,$Hkey,$Xhn #######
425 movdqa $Xi,$T2 # 2nd phase
434 pclmulqdq \$0x00,$T2n,$T1n #######
436 pshufd \$0b01001110,$Xi,$T1
437 pshufd \$0b01001110,$Hkey2,$T2
455 &clmul64x64_T2 ($Xhi,$Xi,$Hkey2,1); # H^2*(Ii+Xi)
457 pxor $Xn,$Xi # (H*Ii+1) + H^2*(Ii+Xi)
460 &reduction_alg9 ($Xhi,$Xi);
466 movdqu ($inp),$T1 # Ii
470 &clmul64x64_T2 ($Xhi,$Xi,$Hkey); # H*(Ii+Xi)
471 &reduction_alg9 ($Xhi,$Xi);
477 $code.=<<___ if ($win64);
479 movaps 0x10(%rsp),%xmm7
480 movaps 0x20(%rsp),%xmm8
481 movaps 0x30(%rsp),%xmm9
482 movaps 0x40(%rsp),%xmm10
487 .LSEH_end_gcm_ghash_clmul:
488 .size gcm_ghash_clmul,.-gcm_ghash_clmul
495 .byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
497 .byte 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xc2
499 .type .Lrem_4bit,\@object
501 .long 0,`0x0000<<16`,0,`0x1C20<<16`,0,`0x3840<<16`,0,`0x2460<<16`
502 .long 0,`0x7080<<16`,0,`0x6CA0<<16`,0,`0x48C0<<16`,0,`0x54E0<<16`
503 .long 0,`0xE100<<16`,0,`0xFD20<<16`,0,`0xD940<<16`,0,`0xC560<<16`
504 .long 0,`0x9180<<16`,0,`0x8DA0<<16`,0,`0xA9C0<<16`,0,`0xB5E0<<16`
505 .asciz "GHASH for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
509 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
510 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
518 .extern __imp_RtlVirtualUnwind
519 .type se_handler,\@abi-omnipotent
533 mov 120($context),%rax # pull context->Rax
534 mov 248($context),%rbx # pull context->Rip
536 mov 8($disp),%rsi # disp->ImageBase
537 mov 56($disp),%r11 # disp->HandlerData
539 mov 0(%r11),%r10d # HandlerData[0]
540 lea (%rsi,%r10),%r10 # prologue label
541 cmp %r10,%rbx # context->Rip<prologue label
544 mov 152($context),%rax # pull context->Rsp
546 mov 4(%r11),%r10d # HandlerData[1]
547 lea (%rsi,%r10),%r10 # epilogue label
548 cmp %r10,%rbx # context->Rip>=epilogue label
551 lea 24(%rax),%rax # adjust "rsp"
556 mov %rbx,144($context) # restore context->Rbx
557 mov %rbp,160($context) # restore context->Rbp
558 mov %r12,216($context) # restore context->R12
563 mov %rax,152($context) # restore context->Rsp
564 mov %rsi,168($context) # restore context->Rsi
565 mov %rdi,176($context) # restore context->Rdi
567 mov 40($disp),%rdi # disp->ContextRecord
568 mov $context,%rsi # context
569 mov \$`1232/8`,%ecx # sizeof(CONTEXT)
570 .long 0xa548f3fc # cld; rep movsq
573 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
574 mov 8(%rsi),%rdx # arg2, disp->ImageBase
575 mov 0(%rsi),%r8 # arg3, disp->ControlPc
576 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
577 mov 40(%rsi),%r10 # disp->ContextRecord
578 lea 56(%rsi),%r11 # &disp->HandlerData
579 lea 24(%rsi),%r12 # &disp->EstablisherFrame
580 mov %r10,32(%rsp) # arg5
581 mov %r11,40(%rsp) # arg6
582 mov %r12,48(%rsp) # arg7
583 mov %rcx,56(%rsp) # arg8, (NULL)
584 call *__imp_RtlVirtualUnwind(%rip)
586 mov \$1,%eax # ExceptionContinueSearch
598 .size se_handler,.-se_handler
602 .rva .LSEH_begin_gcm_gmult_4bit
603 .rva .LSEH_end_gcm_gmult_4bit
604 .rva .LSEH_info_gcm_gmult_4bit
606 .rva .LSEH_begin_gcm_ghash_4bit
607 .rva .LSEH_end_gcm_ghash_4bit
608 .rva .LSEH_info_gcm_ghash_4bit
610 .rva .LSEH_begin_gcm_ghash_clmul
611 .rva .LSEH_end_gcm_ghash_clmul
612 .rva .LSEH_info_gcm_ghash_clmul
616 .LSEH_info_gcm_gmult_4bit:
619 .rva .Lgmult_prologue,.Lgmult_epilogue # HandlerData
620 .LSEH_info_gcm_ghash_4bit:
623 .rva .Lghash_prologue,.Lghash_epilogue # HandlerData
624 .LSEH_info_gcm_ghash_clmul:
625 .byte 0x01,0x1f,0x0b,0x00
626 .byte 0x1f,0xa8,0x04,0x00 #movaps 0x40(rsp),xmm10
627 .byte 0x19,0x98,0x03,0x00 #movaps 0x30(rsp),xmm9
628 .byte 0x13,0x88,0x02,0x00 #movaps 0x20(rsp),xmm8
629 .byte 0x0d,0x78,0x01,0x00 #movaps 0x10(rsp),xmm7
630 .byte 0x08,0x68,0x00,0x00 #movaps (rsp),xmm6
631 .byte 0x04,0xa2,0x00,0x00 #sub rsp,0x58
639 if ($dst>=8 || $src>=8) {
641 $rex|=0x04 if($dst>=8);
642 $rex|=0x01 if($src>=8);
651 if ($arg=~/\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
653 push @opcode,0x0f,0x3a,0x44;
654 push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
656 push @opcode,$c=~/^0/?oct($c):$c;
657 return ".byte\t".join(',',@opcode);
659 return "pclmulqdq\t".$arg;
662 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
663 $code =~ s/\bpclmulqdq\s+(\$.*%xmm[0-9]+).*$/pclmulqdq($1)/gem;