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
29 # Special thanks to David Woodhouse <dwmw2@infradead.org> for
30 # providing access to a Westmere-based system on behalf of Intel
31 # Open Source Technology Centre.
35 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
37 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
39 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
40 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
41 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
42 die "can't locate x86_64-xlate.pl";
44 open STDOUT,"| $^X $xlate $flavour $output";
46 # common register layout
57 # per-function register layout
61 sub lo() { my $r=shift; $r =~ s/%[er]([a-d])x/%\1l/;
62 $r =~ s/%[er]([sd]i)/%\1l/;
63 $r =~ s/%(r[0-9]+)[d]?/%\1b/; $r; }
73 mov `&lo("$Zlo")`,`&lo("$nlo")`
74 mov `&lo("$Zlo")`,`&lo("$nhi")`
77 mov 8($Htbl,$nlo),$Zlo
79 and \$0xf0,`&lo("$nhi")`
88 mov ($inp,$cnt),`&lo("$nlo")`
90 xor 8($Htbl,$nhi),$Zlo
93 mov `&lo("$nlo")`,`&lo("$nhi")`
94 xor ($rem_4bit,$rem,8),$Zhi
105 xor 8($Htbl,$nlo),$Zlo
107 xor ($Htbl,$nlo),$Zhi
108 and \$0xf0,`&lo("$nhi")`
109 xor ($rem_4bit,$rem,8),$Zhi
120 xor 8($Htbl,$nlo),$Zlo
122 xor ($Htbl,$nlo),$Zhi
123 and \$0xf0,`&lo("$nhi")`
124 xor ($rem_4bit,$rem,8),$Zhi
132 xor 8($Htbl,$nhi),$Zlo
134 xor ($Htbl,$nhi),$Zhi
136 xor ($rem_4bit,$rem,8),$Zhi
146 .globl gcm_gmult_4bit
147 .type gcm_gmult_4bit,\@function,2
151 push %rbp # %rbp and %r12 are pushed exclusively in
152 push %r12 # order to reuse Win64 exception handler...
156 lea .Lrem_4bit(%rip),$rem_4bit
167 .size gcm_gmult_4bit,.-gcm_gmult_4bit
170 # per-function register layout
178 .globl gcm_ghash_4bit
179 .type gcm_ghash_4bit,\@function,4
190 lea .Lrem_4bit(%rip),$rem_4bit
214 .size gcm_ghash_4bit,.-gcm_ghash_4bit
217 ######################################################################
220 @_4args=$win64? ("%rcx","%rdx","%r8", "%r9") : # Win64 order
221 ("%rdi","%rsi","%rdx","%rcx"); # Unix order
223 ($Xi,$Xhi)=("%xmm0","%xmm1"); $Hkey="%xmm2";
224 ($T1,$T2,$T3)=("%xmm3","%xmm4","%xmm5");
226 sub clmul64x64_T2 { # minimal register pressure
227 my ($Xhi,$Xi,$Hkey,$modulo)=@_;
229 $code.=<<___ if (!defined($modulo));
231 pshufd \$0b01001110,$Xi,$T1
232 pshufd \$0b01001110,$Hkey,$T2
237 pclmulqdq \$0x00,$Hkey,$Xi #######
238 pclmulqdq \$0x11,$Hkey,$Xhi #######
239 pclmulqdq \$0x00,$T2,$T1 #######
251 sub reduction_alg9 { # 17/13 times faster than Intel version
280 { my ($Htbl,$Xip)=@_4args;
283 .globl gcm_init_clmul
284 .type gcm_init_clmul,\@abi-omnipotent
288 pshufd \$0b01001110,$Hkey,$Hkey # dword swap
291 pshufd \$0b11111111,$Hkey,$T2 # broadcast uppermost dword
296 pcmpgtd $T2,$T3 # broadcast carry bit
298 por $T1,$Hkey # H<<=1
301 pand .L0x1c2_polynomial(%rip),$T3
302 pxor $T3,$Hkey # if(carry) H^=0x1c2_polynomial
307 &clmul64x64_T2 ($Xhi,$Xi,$Hkey);
308 &reduction_alg9 ($Xhi,$Xi);
310 movdqu $Hkey,($Htbl) # save H
311 movdqu $Xi,16($Htbl) # save H^2
313 .size gcm_init_clmul,.-gcm_init_clmul
317 { my ($Xip,$Htbl)=@_4args;
320 .globl gcm_gmult_clmul
321 .type gcm_gmult_clmul,\@abi-omnipotent
325 movdqa .Lbswap_mask(%rip),$T3
329 &clmul64x64_T2 ($Xhi,$Xi,$Hkey);
330 &reduction_alg9 ($Xhi,$Xi);
335 .size gcm_gmult_clmul,.-gcm_gmult_clmul
339 { my ($Xip,$Htbl,$inp,$len)=@_4args;
347 .globl gcm_ghash_clmul
348 .type gcm_ghash_clmul,\@abi-omnipotent
352 $code.=<<___ if ($win64);
353 .LSEH_begin_gcm_ghash_clmul:
354 # I can't trust assembler to use specific encoding:-(
355 .byte 0x48,0x83,0xec,0x58 #sub \$0x58,%rsp
356 .byte 0x0f,0x29,0x34,0x24 #movaps %xmm6,(%rsp)
357 .byte 0x0f,0x29,0x7c,0x24,0x10 #movdqa %xmm7,0x10(%rsp)
358 .byte 0x44,0x0f,0x29,0x44,0x24,0x20 #movaps %xmm8,0x20(%rsp)
359 .byte 0x44,0x0f,0x29,0x4c,0x24,0x30 #movaps %xmm9,0x30(%rsp)
360 .byte 0x44,0x0f,0x29,0x54,0x24,0x40 #movaps %xmm10,0x40(%rsp)
363 movdqa .Lbswap_mask(%rip),$T3
372 movdqu 16($Htbl),$Hkey2
374 # Xi+2 =[H*(Ii+1 + Xi+1)] mod P =
375 # [(H*Ii+1) + (H*Xi+1)] mod P =
376 # [(H*Ii+1) + H^2*(Ii+Xi)] mod P
378 movdqu ($inp),$T1 # Ii
379 movdqu 16($inp),$Xn # Ii+1
384 &clmul64x64_T2 ($Xhn,$Xn,$Hkey); # H*Ii+1
387 pshufd \$0b01001110,$Xi,$T1
388 pshufd \$0b01001110,$Hkey2,$T2
392 lea 32($inp),$inp # i+=2
398 &clmul64x64_T2 ($Xhi,$Xi,$Hkey2,1); # H^2*(Ii+Xi)
400 movdqu ($inp),$T1 # Ii
401 pxor $Xn,$Xi # (H*Ii+1) + H^2*(Ii+Xi)
404 movdqu 16($inp),$Xn # Ii+1
409 pshufd \$0b01001110,$Xn,$T1n
410 pshufd \$0b01001110,$Hkey,$T2n
413 pxor $T1,$Xhi # "Ii+Xi", consume early
415 movdqa $Xi,$T1 # 1st phase
420 pclmulqdq \$0x00,$Hkey,$Xn #######
428 pclmulqdq \$0x11,$Hkey,$Xhn #######
429 movdqa $Xi,$T2 # 2nd phase
438 pclmulqdq \$0x00,$T2n,$T1n #######
440 pshufd \$0b01001110,$Xi,$T1
441 pshufd \$0b01001110,$Hkey2,$T2
459 &clmul64x64_T2 ($Xhi,$Xi,$Hkey2,1); # H^2*(Ii+Xi)
461 pxor $Xn,$Xi # (H*Ii+1) + H^2*(Ii+Xi)
464 &reduction_alg9 ($Xhi,$Xi);
470 movdqu ($inp),$T1 # Ii
474 &clmul64x64_T2 ($Xhi,$Xi,$Hkey); # H*(Ii+Xi)
475 &reduction_alg9 ($Xhi,$Xi);
481 $code.=<<___ if ($win64);
483 movaps 0x10(%rsp),%xmm7
484 movaps 0x20(%rsp),%xmm8
485 movaps 0x30(%rsp),%xmm9
486 movaps 0x40(%rsp),%xmm10
491 .LSEH_end_gcm_ghash_clmul:
492 .size gcm_ghash_clmul,.-gcm_ghash_clmul
499 .byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
501 .byte 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xc2
503 .type .Lrem_4bit,\@object
505 .long 0,`0x0000<<16`,0,`0x1C20<<16`,0,`0x3840<<16`,0,`0x2460<<16`
506 .long 0,`0x7080<<16`,0,`0x6CA0<<16`,0,`0x48C0<<16`,0,`0x54E0<<16`
507 .long 0,`0xE100<<16`,0,`0xFD20<<16`,0,`0xD940<<16`,0,`0xC560<<16`
508 .long 0,`0x9180<<16`,0,`0x8DA0<<16`,0,`0xA9C0<<16`,0,`0xB5E0<<16`
509 .asciz "GHASH for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
513 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
514 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
522 .extern __imp_RtlVirtualUnwind
523 .type se_handler,\@abi-omnipotent
537 mov 120($context),%rax # pull context->Rax
538 mov 248($context),%rbx # pull context->Rip
540 mov 8($disp),%rsi # disp->ImageBase
541 mov 56($disp),%r11 # disp->HandlerData
543 mov 0(%r11),%r10d # HandlerData[0]
544 lea (%rsi,%r10),%r10 # prologue label
545 cmp %r10,%rbx # context->Rip<prologue label
548 mov 152($context),%rax # pull context->Rsp
550 mov 4(%r11),%r10d # HandlerData[1]
551 lea (%rsi,%r10),%r10 # epilogue label
552 cmp %r10,%rbx # context->Rip>=epilogue label
555 lea 24(%rax),%rax # adjust "rsp"
560 mov %rbx,144($context) # restore context->Rbx
561 mov %rbp,160($context) # restore context->Rbp
562 mov %r12,216($context) # restore context->R12
567 mov %rax,152($context) # restore context->Rsp
568 mov %rsi,168($context) # restore context->Rsi
569 mov %rdi,176($context) # restore context->Rdi
571 mov 40($disp),%rdi # disp->ContextRecord
572 mov $context,%rsi # context
573 mov \$`1232/8`,%ecx # sizeof(CONTEXT)
574 .long 0xa548f3fc # cld; rep movsq
577 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
578 mov 8(%rsi),%rdx # arg2, disp->ImageBase
579 mov 0(%rsi),%r8 # arg3, disp->ControlPc
580 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
581 mov 40(%rsi),%r10 # disp->ContextRecord
582 lea 56(%rsi),%r11 # &disp->HandlerData
583 lea 24(%rsi),%r12 # &disp->EstablisherFrame
584 mov %r10,32(%rsp) # arg5
585 mov %r11,40(%rsp) # arg6
586 mov %r12,48(%rsp) # arg7
587 mov %rcx,56(%rsp) # arg8, (NULL)
588 call *__imp_RtlVirtualUnwind(%rip)
590 mov \$1,%eax # ExceptionContinueSearch
602 .size se_handler,.-se_handler
606 .rva .LSEH_begin_gcm_gmult_4bit
607 .rva .LSEH_end_gcm_gmult_4bit
608 .rva .LSEH_info_gcm_gmult_4bit
610 .rva .LSEH_begin_gcm_ghash_4bit
611 .rva .LSEH_end_gcm_ghash_4bit
612 .rva .LSEH_info_gcm_ghash_4bit
614 .rva .LSEH_begin_gcm_ghash_clmul
615 .rva .LSEH_end_gcm_ghash_clmul
616 .rva .LSEH_info_gcm_ghash_clmul
620 .LSEH_info_gcm_gmult_4bit:
623 .rva .Lgmult_prologue,.Lgmult_epilogue # HandlerData
624 .LSEH_info_gcm_ghash_4bit:
627 .rva .Lghash_prologue,.Lghash_epilogue # HandlerData
628 .LSEH_info_gcm_ghash_clmul:
629 .byte 0x01,0x1f,0x0b,0x00
630 .byte 0x1f,0xa8,0x04,0x00 #movaps 0x40(rsp),xmm10
631 .byte 0x19,0x98,0x03,0x00 #movaps 0x30(rsp),xmm9
632 .byte 0x13,0x88,0x02,0x00 #movaps 0x20(rsp),xmm8
633 .byte 0x0d,0x78,0x01,0x00 #movaps 0x10(rsp),xmm7
634 .byte 0x08,0x68,0x00,0x00 #movaps (rsp),xmm6
635 .byte 0x04,0xa2,0x00,0x00 #sub rsp,0x58
643 if ($dst>=8 || $src>=8) {
645 $rex|=0x04 if($dst>=8);
646 $rex|=0x01 if($src>=8);
655 if ($arg=~/\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
657 push @opcode,0x0f,0x3a,0x44;
658 push @opcode,0xc0|($2&7)|(($3&7)<<3); # ModR/M
660 push @opcode,$c=~/^0/?oct($c):$c;
661 return ".byte\t".join(',',@opcode);
663 return "pclmulqdq\t".$arg;
666 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
667 $code =~ s/\bpclmulqdq\s+(\$.*%xmm[0-9]+).*$/pclmulqdq($1)/gem;