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
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:
20 # gcc 3.4.x(*) assembler
23 # Opteron 19.3 7.7 +150%
24 # Core2 17.8 8.1(**) +120%
26 # (*) comparison is not completely fair, because C results are
27 # for vanilla "256B" implementation, while assembler results
29 # (**) it's mystery [to me] why Core2 result is not same as for
34 # Add PCLMULQDQ version performing at 2.02 cycles per processed byte.
35 # See ghash-x86.pl for background information and details about coding
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.
44 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
46 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
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";
53 open OUT
,"| \"$^X\" $xlate $flavour $output";
56 # common register layout
67 # per-function register layout
71 sub LB
() { my $r=shift; $r =~ s/%[er]([a-d])x/%\1l/ or
72 $r =~ s/%[er]([sd]i)/%\1l/ or
73 $r =~ s/%[er](bp)/%\1l/ or
74 $r =~ s/%(r[0-9]+)[d]?/%\1b/; $r; }
76 sub AUTOLOAD
() # thunk [simplified] 32-bit style perlasm
77 { my $opcode = $AUTOLOAD; $opcode =~ s/.*:://;
79 $arg = "\$$arg" if ($arg*1 eq $arg);
80 $code .= "\t$opcode\t".join(',',$arg,reverse @_)."\n";
91 mov
`&LB("$Zlo")`,`&LB("$nlo")`
92 mov
`&LB("$Zlo")`,`&LB("$nhi")`
95 mov
8($Htbl,$nlo),$Zlo
97 and \
$0xf0,`&LB("$nhi")`
106 mov
($inp,$cnt),`&LB("$nlo")`
108 xor 8($Htbl,$nhi),$Zlo
110 xor ($Htbl,$nhi),$Zhi
111 mov
`&LB("$nlo")`,`&LB("$nhi")`
112 xor ($rem_4bit,$rem,8),$Zhi
114 shl \
$4,`&LB("$nlo")`
123 xor 8($Htbl,$nlo),$Zlo
125 xor ($Htbl,$nlo),$Zhi
126 and \
$0xf0,`&LB("$nhi")`
127 xor ($rem_4bit,$rem,8),$Zhi
138 xor 8($Htbl,$nlo),$Zlo
140 xor ($Htbl,$nlo),$Zhi
141 and \
$0xf0,`&LB("$nhi")`
142 xor ($rem_4bit,$rem,8),$Zhi
150 xor 8($Htbl,$nhi),$Zlo
152 xor ($Htbl,$nhi),$Zhi
154 xor ($rem_4bit,$rem,8),$Zhi
164 .globl gcm_gmult_4bit
165 .type gcm_gmult_4bit
,\
@function,2
169 push %rbp # %rbp and %r12 are pushed exclusively in
170 push %r12 # order to reuse Win64 exception handler...
174 lea
.Lrem_4bit
(%rip),$rem_4bit
185 .size gcm_gmult_4bit
,.-gcm_gmult_4bit
188 # per-function register layout
194 .globl gcm_ghash_4bit
195 .type gcm_ghash_4bit
,\
@function,4
206 mov
$inp,%r14 # reassign couple of args
212 my @nhi=("%ebx","%ecx");
213 my @rem=("%r12","%r13");
216 &sub ($Htbl,-128); # size optimization
217 &lea
($Hshr4,"16+128(%rsp)");
218 { my @lo =($nlo,$nhi);
222 for ($i=0,$j=-2;$i<18;$i++,$j++) {
223 &mov
("$j(%rsp)",&LB
($dat)) if ($i>1);
224 &or ($lo[0],$tmp) if ($i>1);
225 &mov
(&LB
($dat),&LB
($lo[1])) if ($i>0 && $i<17);
226 &shr
($lo[1],4) if ($i>0 && $i<17);
227 &mov
($tmp,$hi[1]) if ($i>0 && $i<17);
228 &shr
($hi[1],4) if ($i>0 && $i<17);
229 &mov
("8*$j($Hshr4)",$hi[0]) if ($i>1);
230 &mov
($hi[0],"16*$i+0-128($Htbl)") if ($i<16);
231 &shl
(&LB
($dat),4) if ($i>0 && $i<17);
232 &mov
("8*$j-128($Hshr4)",$lo[0]) if ($i>1);
233 &mov
($lo[0],"16*$i+8-128($Htbl)") if ($i<16);
234 &shl
($tmp,60) if ($i>0 && $i<17);
236 push (@lo,shift(@lo));
237 push (@hi,shift(@hi));
241 &mov
($Zlo,"8($Xi)");
242 &mov
($Zhi,"0($Xi)");
243 &add
($len,$inp); # pointer to the end of data
244 &lea
($rem_8bit,".Lrem_8bit(%rip)");
245 &jmp
(".Louter_loop");
247 $code.=".align 16\n.Louter_loop:\n";
248 &xor ($Zhi,"($inp)");
249 &mov
("%rdx","8($inp)");
250 &lea
($inp,"16($inp)");
253 &mov
("8($Xi)","%rdx");
258 &mov
(&LB
($nlo),&LB
($dat));
259 &movz
($nhi[0],&LB
($dat));
263 for ($j=11,$i=0;$i<15;$i++) {
265 &xor ($Zlo,"8($Htbl,$nlo)") if ($i>0);
266 &xor ($Zhi,"($Htbl,$nlo)") if ($i>0);
267 &mov
($Zlo,"8($Htbl,$nlo)") if ($i==0);
268 &mov
($Zhi,"($Htbl,$nlo)") if ($i==0);
270 &mov
(&LB
($nlo),&LB
($dat));
271 &xor ($Zlo,$tmp) if ($i>0);
272 &movzw
($rem[1],"($rem_8bit,$rem[1],2)") if ($i>0);
274 &movz
($nhi[1],&LB
($dat));
276 &movzb
($rem[0],"(%rsp,$nhi[0])");
278 &shr
($nhi[1],4) if ($i<14);
279 &and ($nhi[1],0xf0) if ($i==14);
280 &shl
($rem[1],48) if ($i>0);
284 &xor ($Zhi,$rem[1]) if ($i>0);
287 &movz
($rem[0],&LB
($rem[0]));
288 &mov
($dat,"$j($Xi)") if (--$j%4==0);
291 &xor ($Zlo,"-128($Hshr4,$nhi[0],8)");
293 &xor ($Zhi,"($Hshr4,$nhi[0],8)");
295 unshift (@nhi,pop(@nhi)); # "rotate" registers
296 unshift (@rem,pop(@rem));
298 &movzw
($rem[1],"($rem_8bit,$rem[1],2)");
299 &xor ($Zlo,"8($Htbl,$nlo)");
300 &xor ($Zhi,"($Htbl,$nlo)");
306 &movz
($rem[0],&LB
($Zlo));
310 &shl
(&LB
($rem[0]),4);
313 &xor ($Zlo,"8($Htbl,$nhi[0])");
314 &movzw
($rem[0],"($rem_8bit,$rem[0],2)");
317 &xor ($Zhi,"($Htbl,$nhi[0])");
326 &jb
(".Louter_loop");
342 .size gcm_ghash_4bit
,.-gcm_ghash_4bit
345 ######################################################################
348 @_4args=$win64?
("%rcx","%rdx","%r8", "%r9") : # Win64 order
349 ("%rdi","%rsi","%rdx","%rcx"); # Unix order
351 ($Xi,$Xhi)=("%xmm0","%xmm1"); $Hkey="%xmm2";
352 ($T1,$T2,$T3)=("%xmm3","%xmm4","%xmm5");
354 sub clmul64x64_T2
{ # minimal register pressure
355 my ($Xhi,$Xi,$Hkey,$modulo)=@_;
357 $code.=<<___
if (!defined($modulo));
359 pshufd \
$0b01001110,$Xi,$T1
360 pshufd \
$0b01001110,$Hkey,$T2
365 pclmulqdq \
$0x00,$Hkey,$Xi #######
366 pclmulqdq \
$0x11,$Hkey,$Xhi #######
367 pclmulqdq \
$0x00,$T2,$T1 #######
379 sub reduction_alg9
{ # 17/13 times faster than Intel version
408 { my ($Htbl,$Xip)=@_4args;
411 .globl gcm_init_clmul
412 .type gcm_init_clmul
,\
@abi-omnipotent
416 pshufd \
$0b01001110,$Hkey,$Hkey # dword swap
419 pshufd \
$0b11111111,$Hkey,$T2 # broadcast uppermost dword
424 pcmpgtd
$T2,$T3 # broadcast carry bit
426 por
$T1,$Hkey # H<<=1
429 pand
.L0x1c2_polynomial
(%rip),$T3
430 pxor
$T3,$Hkey # if(carry) H^=0x1c2_polynomial
435 &clmul64x64_T2
($Xhi,$Xi,$Hkey);
436 &reduction_alg9
($Xhi,$Xi);
438 movdqu
$Hkey,($Htbl) # save H
439 movdqu
$Xi,16($Htbl) # save H^2
441 .size gcm_init_clmul
,.-gcm_init_clmul
445 { my ($Xip,$Htbl)=@_4args;
448 .globl gcm_gmult_clmul
449 .type gcm_gmult_clmul
,\
@abi-omnipotent
453 movdqa
.Lbswap_mask
(%rip),$T3
457 &clmul64x64_T2
($Xhi,$Xi,$Hkey);
458 &reduction_alg9
($Xhi,$Xi);
463 .size gcm_gmult_clmul
,.-gcm_gmult_clmul
467 { my ($Xip,$Htbl,$inp,$len)=@_4args;
475 .globl gcm_ghash_clmul
476 .type gcm_ghash_clmul
,\
@abi-omnipotent
480 $code.=<<___
if ($win64);
481 .LSEH_begin_gcm_ghash_clmul
:
482 # I can't trust assembler to use specific encoding:-(
483 .byte
0x48,0x83,0xec,0x58 #sub \$0x58,%rsp
484 .byte
0x0f,0x29,0x34,0x24 #movaps %xmm6,(%rsp)
485 .byte
0x0f,0x29,0x7c,0x24,0x10 #movdqa %xmm7,0x10(%rsp)
486 .byte
0x44,0x0f,0x29,0x44,0x24,0x20 #movaps %xmm8,0x20(%rsp)
487 .byte
0x44,0x0f,0x29,0x4c,0x24,0x30 #movaps %xmm9,0x30(%rsp)
488 .byte
0x44,0x0f,0x29,0x54,0x24,0x40 #movaps %xmm10,0x40(%rsp)
491 movdqa
.Lbswap_mask
(%rip),$T3
500 movdqu
16($Htbl),$Hkey2
502 # Xi+2 =[H*(Ii+1 + Xi+1)] mod P =
503 # [(H*Ii+1) + (H*Xi+1)] mod P =
504 # [(H*Ii+1) + H^2*(Ii+Xi)] mod P
506 movdqu
($inp),$T1 # Ii
507 movdqu
16($inp),$Xn # Ii+1
512 &clmul64x64_T2
($Xhn,$Xn,$Hkey); # H*Ii+1
515 pshufd \
$0b01001110,$Xi,$T1
516 pshufd \
$0b01001110,$Hkey2,$T2
520 lea
32($inp),$inp # i+=2
526 &clmul64x64_T2
($Xhi,$Xi,$Hkey2,1); # H^2*(Ii+Xi)
528 movdqu
($inp),$T1 # Ii
529 pxor
$Xn,$Xi # (H*Ii+1) + H^2*(Ii+Xi)
532 movdqu
16($inp),$Xn # Ii+1
537 pshufd \
$0b01001110,$Xn,$T1n
538 pshufd \
$0b01001110,$Hkey,$T2n
541 pxor
$T1,$Xhi # "Ii+Xi", consume early
543 movdqa
$Xi,$T1 # 1st phase
548 pclmulqdq \
$0x00,$Hkey,$Xn #######
556 pclmulqdq \
$0x11,$Hkey,$Xhn #######
557 movdqa
$Xi,$T2 # 2nd phase
566 pclmulqdq \
$0x00,$T2n,$T1n #######
568 pshufd \
$0b01001110,$Xi,$T1
569 pshufd \
$0b01001110,$Hkey2,$T2
587 &clmul64x64_T2
($Xhi,$Xi,$Hkey2,1); # H^2*(Ii+Xi)
589 pxor
$Xn,$Xi # (H*Ii+1) + H^2*(Ii+Xi)
592 &reduction_alg9
($Xhi,$Xi);
598 movdqu
($inp),$T1 # Ii
602 &clmul64x64_T2
($Xhi,$Xi,$Hkey); # H*(Ii+Xi)
603 &reduction_alg9
($Xhi,$Xi);
609 $code.=<<___
if ($win64);
611 movaps
0x10(%rsp),%xmm7
612 movaps
0x20(%rsp),%xmm8
613 movaps
0x30(%rsp),%xmm9
614 movaps
0x40(%rsp),%xmm10
619 .LSEH_end_gcm_ghash_clmul
:
620 .size gcm_ghash_clmul
,.-gcm_ghash_clmul
627 .byte
15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
629 .byte
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xc2
631 .type
.Lrem_4bit
,\
@object
633 .long
0,`0x0000<<16`,0,`0x1C20<<16`,0,`0x3840<<16`,0,`0x2460<<16`
634 .long
0,`0x7080<<16`,0,`0x6CA0<<16`,0,`0x48C0<<16`,0,`0x54E0<<16`
635 .long
0,`0xE100<<16`,0,`0xFD20<<16`,0,`0xD940<<16`,0,`0xC560<<16`
636 .long
0,`0x9180<<16`,0,`0x8DA0<<16`,0,`0xA9C0<<16`,0,`0xB5E0<<16`
637 .type
.Lrem_8bit
,\
@object
639 .value
0x0000,0x01C2,0x0384,0x0246,0x0708,0x06CA,0x048C,0x054E
640 .value
0x0E10,0x0FD2,0x0D94,0x0C56,0x0918,0x08DA,0x0A9C,0x0B5E
641 .value
0x1C20,0x1DE2,0x1FA4,0x1E66,0x1B28,0x1AEA,0x18AC,0x196E
642 .value
0x1230,0x13F2,0x11B4,0x1076,0x1538,0x14FA,0x16BC,0x177E
643 .value
0x3840,0x3982,0x3BC4,0x3A06,0x3F48,0x3E8A,0x3CCC,0x3D0E
644 .value
0x3650,0x3792,0x35D4,0x3416,0x3158,0x309A,0x32DC,0x331E
645 .value
0x2460,0x25A2,0x27E4,0x2626,0x2368,0x22AA,0x20EC,0x212E
646 .value
0x2A70,0x2BB2,0x29F4,0x2836,0x2D78,0x2CBA,0x2EFC,0x2F3E
647 .value
0x7080,0x7142,0x7304,0x72C6,0x7788,0x764A,0x740C,0x75CE
648 .value
0x7E90,0x7F52,0x7D14,0x7CD6,0x7998,0x785A,0x7A1C,0x7BDE
649 .value
0x6CA0,0x6D62,0x6F24,0x6EE6,0x6BA8,0x6A6A,0x682C,0x69EE
650 .value
0x62B0,0x6372,0x6134,0x60F6,0x65B8,0x647A,0x663C,0x67FE
651 .value
0x48C0,0x4902,0x4B44,0x4A86,0x4FC8,0x4E0A,0x4C4C,0x4D8E
652 .value
0x46D0,0x4712,0x4554,0x4496,0x41D8,0x401A,0x425C,0x439E
653 .value
0x54E0,0x5522,0x5764,0x56A6,0x53E8,0x522A,0x506C,0x51AE
654 .value
0x5AF0,0x5B32,0x5974,0x58B6,0x5DF8,0x5C3A,0x5E7C,0x5FBE
655 .value
0xE100,0xE0C2,0xE284,0xE346,0xE608,0xE7CA,0xE58C,0xE44E
656 .value
0xEF10,0xEED2,0xEC94,0xED56,0xE818,0xE9DA,0xEB9C,0xEA5E
657 .value
0xFD20,0xFCE2,0xFEA4,0xFF66,0xFA28,0xFBEA,0xF9AC,0xF86E
658 .value
0xF330,0xF2F2,0xF0B4,0xF176,0xF438,0xF5FA,0xF7BC,0xF67E
659 .value
0xD940,0xD882,0xDAC4,0xDB06,0xDE48,0xDF8A,0xDDCC,0xDC0E
660 .value
0xD750,0xD692,0xD4D4,0xD516,0xD058,0xD19A,0xD3DC,0xD21E
661 .value
0xC560,0xC4A2,0xC6E4,0xC726,0xC268,0xC3AA,0xC1EC,0xC02E
662 .value
0xCB70,0xCAB2,0xC8F4,0xC936,0xCC78,0xCDBA,0xCFFC,0xCE3E
663 .value
0x9180,0x9042,0x9204,0x93C6,0x9688,0x974A,0x950C,0x94CE
664 .value
0x9F90,0x9E52,0x9C14,0x9DD6,0x9898,0x995A,0x9B1C,0x9ADE
665 .value
0x8DA0,0x8C62,0x8E24,0x8FE6,0x8AA8,0x8B6A,0x892C,0x88EE
666 .value
0x83B0,0x8272,0x8034,0x81F6,0x84B8,0x857A,0x873C,0x86FE
667 .value
0xA9C0,0xA802,0xAA44,0xAB86,0xAEC8,0xAF0A,0xAD4C,0xAC8E
668 .value
0xA7D0,0xA612,0xA454,0xA596,0xA0D8,0xA11A,0xA35C,0xA29E
669 .value
0xB5E0,0xB422,0xB664,0xB7A6,0xB2E8,0xB32A,0xB16C,0xB0AE
670 .value
0xBBF0,0xBA32,0xB874,0xB9B6,0xBCF8,0xBD3A,0xBF7C,0xBEBE
672 .asciz
"GHASH for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
676 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
677 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
685 .extern __imp_RtlVirtualUnwind
686 .type se_handler
,\
@abi-omnipotent
700 mov
120($context),%rax # pull context->Rax
701 mov
248($context),%rbx # pull context->Rip
703 mov
8($disp),%rsi # disp->ImageBase
704 mov
56($disp),%r11 # disp->HandlerData
706 mov
0(%r11),%r10d # HandlerData[0]
707 lea
(%rsi,%r10),%r10 # prologue label
708 cmp %r10,%rbx # context->Rip<prologue label
711 mov
152($context),%rax # pull context->Rsp
713 mov
4(%r11),%r10d # HandlerData[1]
714 lea
(%rsi,%r10),%r10 # epilogue label
715 cmp %r10,%rbx # context->Rip>=epilogue label
718 lea
24(%rax),%rax # adjust "rsp"
723 mov
%rbx,144($context) # restore context->Rbx
724 mov
%rbp,160($context) # restore context->Rbp
725 mov
%r12,216($context) # restore context->R12
730 mov
%rax,152($context) # restore context->Rsp
731 mov
%rsi,168($context) # restore context->Rsi
732 mov
%rdi,176($context) # restore context->Rdi
734 mov
40($disp),%rdi # disp->ContextRecord
735 mov
$context,%rsi # context
736 mov \
$`1232/8`,%ecx # sizeof(CONTEXT)
737 .long
0xa548f3fc # cld; rep movsq
740 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
741 mov
8(%rsi),%rdx # arg2, disp->ImageBase
742 mov
0(%rsi),%r8 # arg3, disp->ControlPc
743 mov
16(%rsi),%r9 # arg4, disp->FunctionEntry
744 mov
40(%rsi),%r10 # disp->ContextRecord
745 lea
56(%rsi),%r11 # &disp->HandlerData
746 lea
24(%rsi),%r12 # &disp->EstablisherFrame
747 mov
%r10,32(%rsp) # arg5
748 mov
%r11,40(%rsp) # arg6
749 mov
%r12,48(%rsp) # arg7
750 mov
%rcx,56(%rsp) # arg8, (NULL)
751 call
*__imp_RtlVirtualUnwind
(%rip)
753 mov \
$1,%eax # ExceptionContinueSearch
765 .size se_handler
,.-se_handler
769 .rva
.LSEH_begin_gcm_gmult_4bit
770 .rva
.LSEH_end_gcm_gmult_4bit
771 .rva
.LSEH_info_gcm_gmult_4bit
773 .rva
.LSEH_begin_gcm_ghash_4bit
774 .rva
.LSEH_end_gcm_ghash_4bit
775 .rva
.LSEH_info_gcm_ghash_4bit
777 .rva
.LSEH_begin_gcm_ghash_clmul
778 .rva
.LSEH_end_gcm_ghash_clmul
779 .rva
.LSEH_info_gcm_ghash_clmul
783 .LSEH_info_gcm_gmult_4bit
:
786 .rva
.Lgmult_prologue
,.Lgmult_epilogue
# HandlerData
787 .LSEH_info_gcm_ghash_4bit
:
790 .rva
.Lghash_prologue
,.Lghash_epilogue
# HandlerData
791 .LSEH_info_gcm_ghash_clmul
:
792 .byte
0x01,0x1f,0x0b,0x00
793 .byte
0x1f,0xa8,0x04,0x00 #movaps 0x40(rsp),xmm10
794 .byte
0x19,0x98,0x03,0x00 #movaps 0x30(rsp),xmm9
795 .byte
0x13,0x88,0x02,0x00 #movaps 0x20(rsp),xmm8
796 .byte
0x0d,0x78,0x01,0x00 #movaps 0x10(rsp),xmm7
797 .byte
0x08,0x68,0x00,0x00 #movaps (rsp),xmm6
798 .byte
0x04,0xa2,0x00,0x00 #sub rsp,0x58
802 $code =~ s/\`([^\`]*)\`/eval($1)/gem;