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 # ====================================================================
10 # The module implements "4-bit" Galois field multiplication and
11 # streamed GHASH function. "4-bit" means that it uses 256 bytes
12 # per-key table [+128 bytes fixed table]. Performance results are for
13 # streamed GHASH subroutine and are expressed in cycles per processed
14 # byte, less is better:
18 # Opteron 18.5 10.2 +80%
19 # Core2 26.0 16.4 +58%
23 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
25 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
27 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
28 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
29 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
30 die "can't locate x86_64-xlate.pl";
32 open STDOUT,"| $^X $xlate $flavour $output";
34 # common register layout
42 # per-function register layout
49 sub lo() { my $r=shift; $r =~ s/%[er]([a-d])x/%\1l/;
50 $r =~ s/%[er]([sd]i)/%\1l/;
51 $r =~ s/%(r[0-9]+)[d]?/%\1b/; $r; }
61 mov `&lo("$Zlo")`,`&lo("$nlo")`
62 mov `&lo("$Zlo")`,`&lo("$nhi")`
65 mov 8($Htbl,$nlo),$Zlo
67 and \$0xf0,`&lo("$nhi")`
76 mov ($inp,$cnt),`&lo("$nlo")`
78 xor 8($Htbl,$nhi),$Zlo
81 mov `&lo("$nlo")`,`&lo("$nhi")`
82 xor ($rem_4bit,$rem,8),$Zhi
93 xor 8($Htbl,$nlo),$Zlo
96 and \$0xf0,`&lo("$nhi")`
97 xor ($rem_4bit,$rem,8),$Zhi
108 xor 8($Htbl,$nlo),$Zlo
110 xor ($Htbl,$nlo),$Zhi
111 and \$0xf0,`&lo("$nhi")`
112 xor ($rem_4bit,$rem,8),$Zhi
120 xor 8($Htbl,$nhi),$Zlo
122 xor ($Htbl,$nhi),$Zhi
124 xor ($rem_4bit,$rem,8),$Zhi
134 .globl gcm_gmult_4bit
135 .type gcm_gmult_4bit,\@function,2
145 lea .Lrem_4bit(%rip),$rem_4bit
156 .size gcm_gmult_4bit,.-gcm_gmult_4bit
160 # per-function register layout
170 .globl gcm_ghash_4bit
171 .type gcm_ghash_4bit,\@function,4
183 lea .Lrem_4bit(%rip),$rem_4bit
207 .size gcm_ghash_4bit,.-gcm_ghash_4bit
210 .type rem_4bit,\@object
212 .long 0,`0x0000<<16`,0,`0x1C20<<16`,0,`0x3840<<16`,0,`0x2460<<16`
213 .long 0,`0x7080<<16`,0,`0x6CA0<<16`,0,`0x48C0<<16`,0,`0x54E0<<16`
214 .long 0,`0xE100<<16`,0,`0xFD20<<16`,0,`0xD940<<16`,0,`0xC560<<16`
215 .long 0,`0x9180<<16`,0,`0x8DA0<<16`,0,`0xA9C0<<16`,0,`0xB5E0<<16`
216 .asciz "GHASH for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
220 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
221 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
229 .extern __imp_RtlVirtualUnwind
230 .type se_handler,\@abi-omnipotent
244 mov 120($context),%rax # pull context->Rax
245 mov 248($context),%rbx # pull context->Rip
247 mov 8($disp),%rsi # disp->ImageBase
248 mov 56($disp),%r11 # disp->HandlerData
250 mov 0(%r11),%r10d # HandlerData[0]
251 lea (%rsi,%r10),%r10 # prologue label
252 cmp %r10,%rbx # context->Rip<prologue label
255 mov 152($context),%rax # pull context->Rsp
257 mov 4(%r11),%r10d # HandlerData[1]
258 lea (%rsi,%r10),%r10 # epilogue label
259 cmp %r10,%rbx # context->Rip>=epilogue label
262 lea 40(%rax),%rax # adjust "rsp"
267 mov %rbx,144($context) # restore context->Rbx
268 mov %rbp,160($context) # restore context->Rbp
269 mov %r12,216($context) # restore context->R12
274 mov %rax,152($context) # restore context->Rsp
275 mov %rsi,168($context) # restore context->Rsi
276 mov %rdi,176($context) # restore context->Rdi
278 mov 40($disp),%rdi # disp->ContextRecord
279 mov $context,%rsi # context
280 mov \$`1232/8`,%ecx # sizeof(CONTEXT)
281 .long 0xa548f3fc # cld; rep movsq
284 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
285 mov 8(%rsi),%rdx # arg2, disp->ImageBase
286 mov 0(%rsi),%r8 # arg3, disp->ControlPc
287 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
288 mov 40(%rsi),%r10 # disp->ContextRecord
289 lea 56(%rsi),%r11 # &disp->HandlerData
290 lea 24(%rsi),%r12 # &disp->EstablisherFrame
291 mov %r10,32(%rsp) # arg5
292 mov %r11,40(%rsp) # arg6
293 mov %r12,48(%rsp) # arg7
294 mov %rcx,56(%rsp) # arg8, (NULL)
295 call *__imp_RtlVirtualUnwind(%rip)
297 mov \$1,%eax # ExceptionContinueSearch
309 .size se_handler,.-se_handler
313 .rva .LSEH_begin_gcm_gmult_4bit
314 .rva .LSEH_end_gcm_gmult_4bit
315 .rva .LSEH_info_gcm_gmult_4bit
317 .rva .LSEH_begin_gcm_ghash_4bit
318 .rva .LSEH_end_gcm_ghash_4bit
319 .rva .LSEH_info_gcm_ghash_4bit
323 .LSEH_info_gcm_gmult_4bit:
326 .rva .Lgmult_prologue,.Lgmult_epilogue # HandlerData
327 .LSEH_info_gcm_ghash_4bit:
330 .rva .Lghash_prologue,.Lghash_epilogue # HandlerData
333 $code =~ s/\`([^\`]*)\`/eval($1)/gem;