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 MIPS.
12 # Performance improvement is 30% on unaligned input. The "secret" is
13 # to deploy lwl/lwr pair to load unaligned input. One could have
14 # vectorized Xupdate on MIPSIII/IV, but the goal was to code MIPS32-
15 # compatible subroutine. There is room for minor optimization on
16 # little-endian platforms...
18 ######################################################################
19 # There is a number of MIPS ABI in use, O32 and N32/64 are most
20 # widely used. Then there is a new contender: NUBI. It appears that if
21 # one picks the latter, it's possible to arrange code in ABI neutral
22 # manner. Therefore let's stick to NUBI register layout:
24 ($zero,$at,$t0,$t1,$t2)=map("\$$_",(0..2,24,25));
25 ($a0,$a1,$a2,$a3,$a4,$a5,$a6,$a7)=map("\$$_",(4..11));
26 ($s0,$s1,$s2,$s3,$s4,$s5,$s6,$s7,$s8,$s9,$s10,$s11)=map("\$$_",(12..23));
27 ($gp,$tp,$sp,$fp,$ra)=map("\$$_",(3,28..31));
29 # The return value is placed in $a0. Following coding rules facilitate
32 # - never ever touch $tp, "thread pointer", former $gp;
33 # - copy return value to $t0, former $v0 [or to $a0 if you're adapting
35 # - on O32 populate $a4-$a7 with 'lw $aN,4*N($sp)' if necessary;
37 # For reference here is register layout for N32/64 MIPS ABIs:
39 # ($zero,$at,$v0,$v1)=map("\$$_",(0..3));
40 # ($a0,$a1,$a2,$a3,$a4,$a5,$a6,$a7)=map("\$$_",(4..11));
41 # ($t0,$t1,$t2,$t3,$t8,$t9)=map("\$$_",(12..15,24,25));
42 # ($s0,$s1,$s2,$s3,$s4,$s5,$s6,$s7)=map("\$$_",(16..23));
43 # ($gp,$sp,$fp,$ra)=map("\$$_",(28..31));
45 $flavour = shift; # supported flavours are o32,n32,64,nubi32,nubi64
47 if ($flavour =~ /64|n32/i) {
48 $PTR_ADD="dadd"; # incidentally works even on n32
49 $PTR_SUB="dsub"; # incidentally works even on n32
52 $PTR_SLL="dsll"; # incidentally works even on n32
65 ######################################################################
67 $big_endian=(`echo MIPSEL | $ENV{CC} -E -P -`=~/MIPSEL/)?
1:0;
69 for (@ARGV) { $output=$_ if (/^\w[\w\-]*\.\w+$/); }
70 open STDOUT
,">$output";
72 if (!defined($big_endian))
73 { $big_endian=(unpack('L',pack('N',1))==1); }
75 # offsets of the Most and Least Significant Bytes
79 @X=map("\$$_",(8..23)); # a4-a7,s0-s11
88 $E="\$24"; @V=($A,$B,$C,$D,$E);
90 $t1=$num; # $num is offloaded to stack
95 my ($i,$a,$b,$c,$d,$e)=@_;
97 $code.=<<___
if (!$big_endian);
98 srl
$t0,@X[$i],24 # byte swap($i)
100 andi
$t2,@X[$i],0xFF00
109 lwl
@X[$j],$j*4+$MSB($inp)
112 lwr
@X[$j],$j*4+$LSB($inp)
128 my ($i,$a,$b,$c,$d,$e)=@_;
131 $code.=<<___
if (!$big_endian && $i==15);
132 srl
$t0,@X[$i],24 # byte swap($i)
134 andi
$t2,@X[$i],0xFF00
143 xor @X[$j%16],@X[($j+2)%16]
148 xor @X[$j%16],@X[($j+8)%16]
151 xor @X[$j%16],@X[($j+13)%16]
155 addu
@X[$j%16],@X[$j%16]
166 my ($i,$a,$b,$c,$d,$e)=@_;
168 $code.=<<___
if ($i<79);
169 xor @X[$j%16],@X[($j+2)%16]
174 xor @X[$j%16],@X[($j+8)%16]
177 xor @X[$j%16],@X[($j+13)%16]
181 addu
@X[$j%16],@X[$j%16]
188 $code.=<<___
if ($i==79);
210 my ($i,$a,$b,$c,$d,$e)=@_;
212 $code.=<<___
if ($i<79);
213 xor @X[$j%16],@X[($j+2)%16]
218 xor @X[$j%16],@X[($j+8)%16]
221 xor @X[$j%16],@X[($j+13)%16]
226 addu
@X[$j%16],@X[$j%16]
236 $FRAMESIZE=16; # large enough to accomodate NUBI saved registers
237 $SAVED_REGS_MASK = ($flavour =~ /nubi/i) ?
0xc0fff008 : 0xc0ff0000;
240 #ifdef OPENSSL_FIPSCANISTER
241 # include <openssl/fipssyms.h>
249 .globl sha1_block_data_order
250 .ent sha1_block_data_order
251 sha1_block_data_order
:
252 .frame
$sp,$FRAMESIZE*$SZREG,$ra
253 .mask
$SAVED_REGS_MASK,-$SZREG
255 $PTR_SUB $sp,$FRAMESIZE*$SZREG
256 $REG_S $ra,($FRAMESIZE-1)*$SZREG($sp)
257 $REG_S $fp,($FRAMESIZE-2)*$SZREG($sp)
258 $REG_S $s11,($FRAMESIZE-3)*$SZREG($sp)
259 $REG_S $s10,($FRAMESIZE-4)*$SZREG($sp)
260 $REG_S $s9,($FRAMESIZE-5)*$SZREG($sp)
261 $REG_S $s8,($FRAMESIZE-6)*$SZREG($sp)
262 $REG_S $s7,($FRAMESIZE-7)*$SZREG($sp)
263 $REG_S $s6,($FRAMESIZE-8)*$SZREG($sp)
264 $REG_S $s5,($FRAMESIZE-9)*$SZREG($sp)
265 $REG_S $s4,($FRAMESIZE-10)*$SZREG($sp)
267 $code.=<<___
if ($flavour =~ /nubi/i); # optimize non-nubi prologue
268 $REG_S $s3,($FRAMESIZE-11)*$SZREG($sp)
269 $REG_S $s2,($FRAMESIZE-12)*$SZREG($sp)
270 $REG_S $s1,($FRAMESIZE-13)*$SZREG($sp)
271 $REG_S $s0,($FRAMESIZE-14)*$SZREG($sp)
272 $REG_S $gp,($FRAMESIZE-15)*$SZREG($sp)
290 ori
$K,0x7999 # K_00_19
292 for ($i=0;$i<15;$i++) { &BODY_00_14
($i,@V); unshift(@V,pop(@V)); }
293 for (;$i<20;$i++) { &BODY_15_19
($i,@V); unshift(@V,pop(@V)); }
296 ori
$K,0xeba1 # K_20_39
298 for (;$i<40;$i++) { &BODY_20_39
($i,@V); unshift(@V,pop(@V)); }
301 ori
$K,0xbcdc # K_40_59
303 for (;$i<60;$i++) { &BODY_40_59
($i,@V); unshift(@V,pop(@V)); }
306 ori
$K,0xc1d6 # K_60_79
308 for (;$i<80;$i++) { &BODY_20_39
($i,@V); unshift(@V,pop(@V)); }
328 $REG_L $ra,($FRAMESIZE-1)*$SZREG($sp)
329 $REG_L $fp,($FRAMESIZE-2)*$SZREG($sp)
330 $REG_L $s11,($FRAMESIZE-3)*$SZREG($sp)
331 $REG_L $s10,($FRAMESIZE-4)*$SZREG($sp)
332 $REG_L $s9,($FRAMESIZE-5)*$SZREG($sp)
333 $REG_L $s8,($FRAMESIZE-6)*$SZREG($sp)
334 $REG_L $s7,($FRAMESIZE-7)*$SZREG($sp)
335 $REG_L $s6,($FRAMESIZE-8)*$SZREG($sp)
336 $REG_L $s5,($FRAMESIZE-9)*$SZREG($sp)
337 $REG_L $s4,($FRAMESIZE-10)*$SZREG($sp)
339 $code.=<<___
if ($flavour =~ /nubi/i);
340 $REG_L $s3,($FRAMESIZE-11)*$SZREG($sp)
341 $REG_L $s2,($FRAMESIZE-12)*$SZREG($sp)
342 $REG_L $s1,($FRAMESIZE-13)*$SZREG($sp)
343 $REG_L $s0,($FRAMESIZE-14)*$SZREG($sp)
344 $REG_L $gp,($FRAMESIZE-15)*$SZREG($sp)
348 $PTR_ADD $sp,$FRAMESIZE*$SZREG
349 .end sha1_block_data_order
351 .asciiz
"SHA1 for MIPS, CRYPTOGAMS by <appro\@openssl.org>"