Skip to content

Commit 8df4b09

Browse files
author
Shigeki Ohtsu
committedMay 5, 2016
deps: separate sha256/sha512-x86_64.pl for openssl
sha256-x86_64.pl does not exist in the origin openssl distribution. It was copied from sha512-x86_64.pl and both sha256/sha512 scripts were modified so as to generates only one asm file specified as its key hash length. PR: #9451 PR-URL: nodejs/node-v0.x-archive#9451 Reviewed-By: Julien Gilli <[email protected]> PR: #25523 PR-URL: nodejs/node-v0.x-archive#25523 Reviewed-By: Julien Gilli <[email protected]> PR: #25654 PR-URL: nodejs/node-v0.x-archive#25654 Reviewed-By: Julien Gilli <[email protected]>
1 parent 11eefef commit 8df4b09

File tree

2 files changed

+449
-25
lines changed

2 files changed

+449
-25
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,438 @@
1+
#!/usr/bin/env perl
2+
#
3+
# ====================================================================
4+
# Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
5+
# project. Rights for redistribution and usage in source and binary
6+
# forms are granted according to the OpenSSL license.
7+
# ====================================================================
8+
#
9+
# sha256/512_block procedure for x86_64.
10+
#
11+
# 40% improvement over compiler-generated code on Opteron. On EM64T
12+
# sha256 was observed to run >80% faster and sha512 - >40%. No magical
13+
# tricks, just straight implementation... I really wonder why gcc
14+
# [being armed with inline assembler] fails to generate as fast code.
15+
# The only thing which is cool about this module is that it's very
16+
# same instruction sequence used for both SHA-256 and SHA-512. In
17+
# former case the instructions operate on 32-bit operands, while in
18+
# latter - on 64-bit ones. All I had to do is to get one flavor right,
19+
# the other one passed the test right away:-)
20+
#
21+
# sha256_block runs in ~1005 cycles on Opteron, which gives you
22+
# asymptotic performance of 64*1000/1005=63.7MBps times CPU clock
23+
# frequency in GHz. sha512_block runs in ~1275 cycles, which results
24+
# in 128*1000/1275=100MBps per GHz. Is there room for improvement?
25+
# Well, if you compare it to IA-64 implementation, which maintains
26+
# X[16] in register bank[!], tends to 4 instructions per CPU clock
27+
# cycle and runs in 1003 cycles, 1275 is very good result for 3-way
28+
# issue Opteron pipeline and X[16] maintained in memory. So that *if*
29+
# there is a way to improve it, *then* the only way would be to try to
30+
# offload X[16] updates to SSE unit, but that would require "deeper"
31+
# loop unroll, which in turn would naturally cause size blow-up, not
32+
# to mention increased complexity! And once again, only *if* it's
33+
# actually possible to noticeably improve overall ILP, instruction
34+
# level parallelism, on a given CPU implementation in this case.
35+
#
36+
# Special note on Intel EM64T. While Opteron CPU exhibits perfect
37+
# perfromance ratio of 1.5 between 64- and 32-bit flavors [see above],
38+
# [currently available] EM64T CPUs apparently are far from it. On the
39+
# contrary, 64-bit version, sha512_block, is ~30% *slower* than 32-bit
40+
# sha256_block:-( This is presumably because 64-bit shifts/rotates
41+
# apparently are not atomic instructions, but implemented in microcode.
42+
43+
$flavour = shift;
44+
$output = shift;
45+
if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
46+
47+
$win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
48+
49+
$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
50+
( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
51+
( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
52+
die "can't locate x86_64-xlate.pl";
53+
54+
open OUT,"| \"$^X\" $xlate $flavour $output";
55+
*STDOUT=*OUT;
56+
57+
$func="sha256_block_data_order";
58+
$TABLE="K256";
59+
$SZ=4;
60+
@ROT=($A,$B,$C,$D,$E,$F,$G,$H)=("%eax","%ebx","%ecx","%edx",
61+
"%r8d","%r9d","%r10d","%r11d");
62+
($T1,$a0,$a1,$a2)=("%r12d","%r13d","%r14d","%r15d");
63+
@Sigma0=( 2,13,22);
64+
@Sigma1=( 6,11,25);
65+
@sigma0=( 7,18, 3);
66+
@sigma1=(17,19,10);
67+
$rounds=64;
68+
69+
70+
$ctx="%rdi"; # 1st arg
71+
$round="%rdi"; # zaps $ctx
72+
$inp="%rsi"; # 2nd arg
73+
$Tbl="%rbp";
74+
75+
$_ctx="16*$SZ+0*8(%rsp)";
76+
$_inp="16*$SZ+1*8(%rsp)";
77+
$_end="16*$SZ+2*8(%rsp)";
78+
$_rsp="16*$SZ+3*8(%rsp)";
79+
$framesz="16*$SZ+4*8";
80+
81+
82+
sub ROUND_00_15()
83+
{ my ($i,$a,$b,$c,$d,$e,$f,$g,$h) = @_;
84+
85+
$code.=<<___;
86+
ror \$`$Sigma1[2]-$Sigma1[1]`,$a0
87+
mov $f,$a2
88+
mov $T1,`$SZ*($i&0xf)`(%rsp)
89+
90+
ror \$`$Sigma0[2]-$Sigma0[1]`,$a1
91+
xor $e,$a0
92+
xor $g,$a2 # f^g
93+
94+
ror \$`$Sigma1[1]-$Sigma1[0]`,$a0
95+
add $h,$T1 # T1+=h
96+
xor $a,$a1
97+
98+
add ($Tbl,$round,$SZ),$T1 # T1+=K[round]
99+
and $e,$a2 # (f^g)&e
100+
mov $b,$h
101+
102+
ror \$`$Sigma0[1]-$Sigma0[0]`,$a1
103+
xor $e,$a0
104+
xor $g,$a2 # Ch(e,f,g)=((f^g)&e)^g
105+
106+
xor $c,$h # b^c
107+
xor $a,$a1
108+
add $a2,$T1 # T1+=Ch(e,f,g)
109+
mov $b,$a2
110+
111+
ror \$$Sigma1[0],$a0 # Sigma1(e)
112+
and $a,$h # h=(b^c)&a
113+
and $c,$a2 # b&c
114+
115+
ror \$$Sigma0[0],$a1 # Sigma0(a)
116+
add $a0,$T1 # T1+=Sigma1(e)
117+
add $a2,$h # h+=b&c (completes +=Maj(a,b,c)
118+
119+
add $T1,$d # d+=T1
120+
add $T1,$h # h+=T1
121+
lea 1($round),$round # round++
122+
add $a1,$h # h+=Sigma0(a)
123+
124+
___
125+
}
126+
127+
sub ROUND_16_XX()
128+
{ my ($i,$a,$b,$c,$d,$e,$f,$g,$h) = @_;
129+
130+
$code.=<<___;
131+
mov `$SZ*(($i+1)&0xf)`(%rsp),$a0
132+
mov `$SZ*(($i+14)&0xf)`(%rsp),$a1
133+
mov $a0,$T1
134+
mov $a1,$a2
135+
136+
ror \$`$sigma0[1]-$sigma0[0]`,$T1
137+
xor $a0,$T1
138+
shr \$$sigma0[2],$a0
139+
140+
ror \$$sigma0[0],$T1
141+
xor $T1,$a0 # sigma0(X[(i+1)&0xf])
142+
mov `$SZ*(($i+9)&0xf)`(%rsp),$T1
143+
144+
ror \$`$sigma1[1]-$sigma1[0]`,$a2
145+
xor $a1,$a2
146+
shr \$$sigma1[2],$a1
147+
148+
ror \$$sigma1[0],$a2
149+
add $a0,$T1
150+
xor $a2,$a1 # sigma1(X[(i+14)&0xf])
151+
152+
add `$SZ*($i&0xf)`(%rsp),$T1
153+
mov $e,$a0
154+
add $a1,$T1
155+
mov $a,$a1
156+
___
157+
&ROUND_00_15(@_);
158+
}
159+
160+
$code=<<___;
161+
.text
162+
163+
.globl $func
164+
.type $func,\@function,4
165+
.align 16
166+
$func:
167+
push %rbx
168+
push %rbp
169+
push %r12
170+
push %r13
171+
push %r14
172+
push %r15
173+
mov %rsp,%r11 # copy %rsp
174+
shl \$4,%rdx # num*16
175+
sub \$$framesz,%rsp
176+
lea ($inp,%rdx,$SZ),%rdx # inp+num*16*$SZ
177+
and \$-64,%rsp # align stack frame
178+
mov $ctx,$_ctx # save ctx, 1st arg
179+
mov $inp,$_inp # save inp, 2nd arh
180+
mov %rdx,$_end # save end pointer, "3rd" arg
181+
mov %r11,$_rsp # save copy of %rsp
182+
.Lprologue:
183+
184+
lea $TABLE(%rip),$Tbl
185+
186+
mov $SZ*0($ctx),$A
187+
mov $SZ*1($ctx),$B
188+
mov $SZ*2($ctx),$C
189+
mov $SZ*3($ctx),$D
190+
mov $SZ*4($ctx),$E
191+
mov $SZ*5($ctx),$F
192+
mov $SZ*6($ctx),$G
193+
mov $SZ*7($ctx),$H
194+
jmp .Lloop
195+
196+
.align 16
197+
.Lloop:
198+
xor $round,$round
199+
___
200+
for($i=0;$i<16;$i++) {
201+
$code.=" mov $SZ*$i($inp),$T1\n";
202+
$code.=" mov @ROT[4],$a0\n";
203+
$code.=" mov @ROT[0],$a1\n";
204+
$code.=" bswap $T1\n";
205+
&ROUND_00_15($i,@ROT);
206+
unshift(@ROT,pop(@ROT));
207+
}
208+
$code.=<<___;
209+
jmp .Lrounds_16_xx
210+
.align 16
211+
.Lrounds_16_xx:
212+
___
213+
for(;$i<32;$i++) {
214+
&ROUND_16_XX($i,@ROT);
215+
unshift(@ROT,pop(@ROT));
216+
}
217+
218+
$code.=<<___;
219+
cmp \$$rounds,$round
220+
jb .Lrounds_16_xx
221+
222+
mov $_ctx,$ctx
223+
lea 16*$SZ($inp),$inp
224+
225+
add $SZ*0($ctx),$A
226+
add $SZ*1($ctx),$B
227+
add $SZ*2($ctx),$C
228+
add $SZ*3($ctx),$D
229+
add $SZ*4($ctx),$E
230+
add $SZ*5($ctx),$F
231+
add $SZ*6($ctx),$G
232+
add $SZ*7($ctx),$H
233+
234+
cmp $_end,$inp
235+
236+
mov $A,$SZ*0($ctx)
237+
mov $B,$SZ*1($ctx)
238+
mov $C,$SZ*2($ctx)
239+
mov $D,$SZ*3($ctx)
240+
mov $E,$SZ*4($ctx)
241+
mov $F,$SZ*5($ctx)
242+
mov $G,$SZ*6($ctx)
243+
mov $H,$SZ*7($ctx)
244+
jb .Lloop
245+
246+
mov $_rsp,%rsi
247+
mov (%rsi),%r15
248+
mov 8(%rsi),%r14
249+
mov 16(%rsi),%r13
250+
mov 24(%rsi),%r12
251+
mov 32(%rsi),%rbp
252+
mov 40(%rsi),%rbx
253+
lea 48(%rsi),%rsp
254+
.Lepilogue:
255+
ret
256+
.size $func,.-$func
257+
___
258+
259+
if ($SZ==4) {
260+
$code.=<<___;
261+
.align 64
262+
.type $TABLE,\@object
263+
$TABLE:
264+
.long 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5
265+
.long 0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5
266+
.long 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3
267+
.long 0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174
268+
.long 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc
269+
.long 0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da
270+
.long 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7
271+
.long 0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967
272+
.long 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13
273+
.long 0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85
274+
.long 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3
275+
.long 0xd192e819,0xd6990624,0xf40e3585,0x106aa070
276+
.long 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5
277+
.long 0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3
278+
.long 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208
279+
.long 0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
280+
___
281+
} else {
282+
$code.=<<___;
283+
.align 64
284+
.type $TABLE,\@object
285+
$TABLE:
286+
.quad 0x428a2f98d728ae22,0x7137449123ef65cd
287+
.quad 0xb5c0fbcfec4d3b2f,0xe9b5dba58189dbbc
288+
.quad 0x3956c25bf348b538,0x59f111f1b605d019
289+
.quad 0x923f82a4af194f9b,0xab1c5ed5da6d8118
290+
.quad 0xd807aa98a3030242,0x12835b0145706fbe
291+
.quad 0x243185be4ee4b28c,0x550c7dc3d5ffb4e2
292+
.quad 0x72be5d74f27b896f,0x80deb1fe3b1696b1
293+
.quad 0x9bdc06a725c71235,0xc19bf174cf692694
294+
.quad 0xe49b69c19ef14ad2,0xefbe4786384f25e3
295+
.quad 0x0fc19dc68b8cd5b5,0x240ca1cc77ac9c65
296+
.quad 0x2de92c6f592b0275,0x4a7484aa6ea6e483
297+
.quad 0x5cb0a9dcbd41fbd4,0x76f988da831153b5
298+
.quad 0x983e5152ee66dfab,0xa831c66d2db43210
299+
.quad 0xb00327c898fb213f,0xbf597fc7beef0ee4
300+
.quad 0xc6e00bf33da88fc2,0xd5a79147930aa725
301+
.quad 0x06ca6351e003826f,0x142929670a0e6e70
302+
.quad 0x27b70a8546d22ffc,0x2e1b21385c26c926
303+
.quad 0x4d2c6dfc5ac42aed,0x53380d139d95b3df
304+
.quad 0x650a73548baf63de,0x766a0abb3c77b2a8
305+
.quad 0x81c2c92e47edaee6,0x92722c851482353b
306+
.quad 0xa2bfe8a14cf10364,0xa81a664bbc423001
307+
.quad 0xc24b8b70d0f89791,0xc76c51a30654be30
308+
.quad 0xd192e819d6ef5218,0xd69906245565a910
309+
.quad 0xf40e35855771202a,0x106aa07032bbd1b8
310+
.quad 0x19a4c116b8d2d0c8,0x1e376c085141ab53
311+
.quad 0x2748774cdf8eeb99,0x34b0bcb5e19b48a8
312+
.quad 0x391c0cb3c5c95a63,0x4ed8aa4ae3418acb
313+
.quad 0x5b9cca4f7763e373,0x682e6ff3d6b2b8a3
314+
.quad 0x748f82ee5defb2fc,0x78a5636f43172f60
315+
.quad 0x84c87814a1f0ab72,0x8cc702081a6439ec
316+
.quad 0x90befffa23631e28,0xa4506cebde82bde9
317+
.quad 0xbef9a3f7b2c67915,0xc67178f2e372532b
318+
.quad 0xca273eceea26619c,0xd186b8c721c0c207
319+
.quad 0xeada7dd6cde0eb1e,0xf57d4f7fee6ed178
320+
.quad 0x06f067aa72176fba,0x0a637dc5a2c898a6
321+
.quad 0x113f9804bef90dae,0x1b710b35131c471b
322+
.quad 0x28db77f523047d84,0x32caab7b40c72493
323+
.quad 0x3c9ebe0a15c9bebc,0x431d67c49c100d4c
324+
.quad 0x4cc5d4becb3e42b6,0x597f299cfc657e2a
325+
.quad 0x5fcb6fab3ad6faec,0x6c44198c4a475817
326+
___
327+
}
328+
329+
# EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
330+
# CONTEXT *context,DISPATCHER_CONTEXT *disp)
331+
if ($win64) {
332+
$rec="%rcx";
333+
$frame="%rdx";
334+
$context="%r8";
335+
$disp="%r9";
336+
337+
$code.=<<___;
338+
.extern __imp_RtlVirtualUnwind
339+
.type se_handler,\@abi-omnipotent
340+
.align 16
341+
se_handler:
342+
push %rsi
343+
push %rdi
344+
push %rbx
345+
push %rbp
346+
push %r12
347+
push %r13
348+
push %r14
349+
push %r15
350+
pushfq
351+
sub \$64,%rsp
352+
353+
mov 120($context),%rax # pull context->Rax
354+
mov 248($context),%rbx # pull context->Rip
355+
356+
lea .Lprologue(%rip),%r10
357+
cmp %r10,%rbx # context->Rip<.Lprologue
358+
jb .Lin_prologue
359+
360+
mov 152($context),%rax # pull context->Rsp
361+
362+
lea .Lepilogue(%rip),%r10
363+
cmp %r10,%rbx # context->Rip>=.Lepilogue
364+
jae .Lin_prologue
365+
366+
mov 16*$SZ+3*8(%rax),%rax # pull $_rsp
367+
lea 48(%rax),%rax
368+
369+
mov -8(%rax),%rbx
370+
mov -16(%rax),%rbp
371+
mov -24(%rax),%r12
372+
mov -32(%rax),%r13
373+
mov -40(%rax),%r14
374+
mov -48(%rax),%r15
375+
mov %rbx,144($context) # restore context->Rbx
376+
mov %rbp,160($context) # restore context->Rbp
377+
mov %r12,216($context) # restore context->R12
378+
mov %r13,224($context) # restore context->R13
379+
mov %r14,232($context) # restore context->R14
380+
mov %r15,240($context) # restore context->R15
381+
382+
.Lin_prologue:
383+
mov 8(%rax),%rdi
384+
mov 16(%rax),%rsi
385+
mov %rax,152($context) # restore context->Rsp
386+
mov %rsi,168($context) # restore context->Rsi
387+
mov %rdi,176($context) # restore context->Rdi
388+
389+
mov 40($disp),%rdi # disp->ContextRecord
390+
mov $context,%rsi # context
391+
mov \$154,%ecx # sizeof(CONTEXT)
392+
.long 0xa548f3fc # cld; rep movsq
393+
394+
mov $disp,%rsi
395+
xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
396+
mov 8(%rsi),%rdx # arg2, disp->ImageBase
397+
mov 0(%rsi),%r8 # arg3, disp->ControlPc
398+
mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
399+
mov 40(%rsi),%r10 # disp->ContextRecord
400+
lea 56(%rsi),%r11 # &disp->HandlerData
401+
lea 24(%rsi),%r12 # &disp->EstablisherFrame
402+
mov %r10,32(%rsp) # arg5
403+
mov %r11,40(%rsp) # arg6
404+
mov %r12,48(%rsp) # arg7
405+
mov %rcx,56(%rsp) # arg8, (NULL)
406+
call *__imp_RtlVirtualUnwind(%rip)
407+
408+
mov \$1,%eax # ExceptionContinueSearch
409+
add \$64,%rsp
410+
popfq
411+
pop %r15
412+
pop %r14
413+
pop %r13
414+
pop %r12
415+
pop %rbp
416+
pop %rbx
417+
pop %rdi
418+
pop %rsi
419+
ret
420+
.size se_handler,.-se_handler
421+
422+
.section .pdata
423+
.align 4
424+
.rva .LSEH_begin_$func
425+
.rva .LSEH_end_$func
426+
.rva .LSEH_info_$func
427+
428+
.section .xdata
429+
.align 8
430+
.LSEH_info_$func:
431+
.byte 9,0,0,0
432+
.rva se_handler
433+
___
434+
}
435+
436+
$code =~ s/\`([^\`]*)\`/eval $1/gem;
437+
print $code;
438+
close STDOUT;

‎deps/openssl/openssl/crypto/sha/asm/sha512-x86_64.pl

+11-25
Original file line numberDiff line numberDiff line change
@@ -54,31 +54,17 @@
5454
open OUT,"| \"$^X\" $xlate $flavour $output";
5555
*STDOUT=*OUT;
5656

57-
if ($output =~ /512/) {
58-
$func="sha512_block_data_order";
59-
$TABLE="K512";
60-
$SZ=8;
61-
@ROT=($A,$B,$C,$D,$E,$F,$G,$H)=("%rax","%rbx","%rcx","%rdx",
62-
"%r8", "%r9", "%r10","%r11");
63-
($T1,$a0,$a1,$a2)=("%r12","%r13","%r14","%r15");
64-
@Sigma0=(28,34,39);
65-
@Sigma1=(14,18,41);
66-
@sigma0=(1, 8, 7);
67-
@sigma1=(19,61, 6);
68-
$rounds=80;
69-
} else {
70-
$func="sha256_block_data_order";
71-
$TABLE="K256";
72-
$SZ=4;
73-
@ROT=($A,$B,$C,$D,$E,$F,$G,$H)=("%eax","%ebx","%ecx","%edx",
74-
"%r8d","%r9d","%r10d","%r11d");
75-
($T1,$a0,$a1,$a2)=("%r12d","%r13d","%r14d","%r15d");
76-
@Sigma0=( 2,13,22);
77-
@Sigma1=( 6,11,25);
78-
@sigma0=( 7,18, 3);
79-
@sigma1=(17,19,10);
80-
$rounds=64;
81-
}
57+
$func="sha512_block_data_order";
58+
$TABLE="K512";
59+
$SZ=8;
60+
@ROT=($A,$B,$C,$D,$E,$F,$G,$H)=("%rax","%rbx","%rcx","%rdx",
61+
"%r8", "%r9", "%r10","%r11");
62+
($T1,$a0,$a1,$a2)=("%r12","%r13","%r14","%r15");
63+
@Sigma0=(28,34,39);
64+
@Sigma1=(14,18,41);
65+
@sigma0=(1, 8, 7);
66+
@sigma1=(19,61, 6);
67+
$rounds=80;
8268

8369
$ctx="%rdi"; # 1st arg
8470
$round="%rdi"; # zaps $ctx

0 commit comments

Comments
 (0)
Please sign in to comment.