3 # ====================================================================
4 # Written by Andy Polyakov <appro@fy.chalmers.se> 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 # sha1_block procedure for x86_64.
12 # It was brought to my attention that on EM64T compiler-generated code
13 # was far behind 32-bit assembler implementation. This is unlike on
14 # Opteron where compiler-generated code was only 15% behind 32-bit
15 # assembler, which originally made it hard to motivate the effort.
16 # There was suggestion to mechanically translate 32-bit code, but I
17 # dismissed it, reasoning that x86_64 offers enough register bank
18 # capacity to fully utilize SHA-1 parallelism. Therefore this fresh
19 # implementation:-) However! While 64-bit code does performs better
20 # on Opteron, I failed to beat 32-bit assembler on EM64T core. Well,
21 # x86_64 does offer larger *addressable* bank, but out-of-order core
22 # reaches for even more registers through dynamic aliasing, and EM64T
23 # core must have managed to run-time optimize even 32-bit code just as
24 # good as 64-bit one. Performance improvement is summarized in the
27 # gcc 3.4 32-bit asm cycles/byte
28 # Opteron +45% +20% 6.8
29 # Xeon P4 +65% +0% 9.9
33 # OpenSolaris OS modifications
35 # Sun elects to use this software under the BSD license.
37 # This source originates from OpenSSL file sha1-x86_64.pl at
38 # ftp://ftp.openssl.org/snapshot/openssl-0.9.8-stable-SNAP-20080131.tar.gz
39 # (presumably for future OpenSSL release 0.9.8h), with these changes:
41 # 1. Added perl "use strict" and declared variables.
43 # 2. Added OpenSolaris ENTRY_NP/SET_SIZE macros from
44 # /usr/include/sys/asm_linkage.h, .ident keywords, and lint(1B) guards.
46 # 3. Removed x86_64-xlate.pl script (not needed for as(1) or gas(1) assemblers).
50 my ($code, $ctx, $inp, $num, $xi, $t0, $t1, $i, @V, $A, $B, $C, $D, $E, $T);
52 open STDOUT
,">$output";
56 # void sha1_block_data_order(SHA1_CTX *ctx, const void *inpp, size_t blocks);
60 $ctx="%rdi"; # 1st arg
61 $inp="%rsi"; # 2nd arg
62 $num="%rdx"; # 3rd arg
64 # reassign arguments in order to produce more compact code
73 # State information from SHA-1 context:
82 @V=($A,$B,$C,$D,$E,$T);
92 mov
%rdi,$ctx # reassigned argument
94 mov
%rsi,$inp # reassigned argument
96 mov
%rdx,$num # reassigned argument
110 mov
`16*4`(%rsp),%rsp
120 my ($i,$a,$b,$c,$d,$e,$f,$host)=@_;
122 $code.=<<___
if ($i==0);
124 `"bswap $xi" if(!defined($host))`
127 $code.=<<___
if ($i<15);
128 lea
0x5a827999($xi,$e),$f
133 `"bswap $xi" if(!defined($host))`
142 $code.=<<___
if ($i>=15);
143 lea
0x5a827999($xi,$e),$f
144 mov
`4*($j%16)`(%rsp),$xi
147 xor `4*(($j+2)%16)`(%rsp),$xi
150 xor `4*(($j+8)%16)`(%rsp),$xi
153 xor `4*(($j+13)%16)`(%rsp),$xi
158 mov
$xi,`4*($j%16)`(%rsp)
163 my ($i,$a,$b,$c,$d,$e,$f)=@_;
165 my $K=($i<40)?
"0x6ed9eba1":"-0x359d3e2a";
166 $code.=<<___
if ($i<79);
168 mov
`4*($j%16)`(%rsp),$xi
171 xor `4*(($j+2)%16)`(%rsp),$xi
174 xor `4*(($j+8)%16)`(%rsp),$xi
177 xor `4*(($j+13)%16)`(%rsp),$xi
182 $code.=<<___
if ($i<76);
183 mov
$xi,`4*($j%16)`(%rsp)
185 $code.=<<___
if ($i==79);
199 my ($i,$a,$b,$c,$d,$e,$f)=@_;
202 lea
-0x70e44324($xi,$e),$f
203 mov
`4*($j%16)`(%rsp),$xi
206 xor `4*(($j+2)%16)`(%rsp),$xi
209 xor `4*(($j+8)%16)`(%rsp),$xi
212 xor `4*(($j+13)%16)`(%rsp),$xi
218 mov
$xi,`4*($j%16)`(%rsp)
225 # Execution begins here
229 #include <sys/asm_linkage.h>
233 &PROLOGUE
("sha1_block_data_order");
234 $code.=".align 4\n.Lloop:\n";
235 for($i=0;$i<20;$i++) { &BODY_00_19
($i,@V); unshift(@V,pop(@V)); }
236 for(;$i<40;$i++) { &BODY_20_39
($i,@V); unshift(@V,pop(@V)); }
237 for(;$i<60;$i++) { &BODY_40_59
($i,@V); unshift(@V,pop(@V)); }
238 for(;$i<80;$i++) { &BODY_20_39
($i,@V); unshift(@V,pop(@V)); }
240 / Update
and save
state information
in SHA
-1 context
252 xchg
$E,$A # mov $E,$A
253 xchg
$T,$B # mov $T,$B
254 xchg
$E,$C # mov $A,$C
255 xchg
$T,$D # mov $B,$D
257 lea
`16*4`($inp),$inp
261 &EPILOGUE
("sha1_block_data_order");
263 .asciz
"SHA1 block transform for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
267 ####################################################################
269 $code =~ s/\`([^\`]*)\`/eval $1/gem;