2 # Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the OpenSSL license (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
10 # ====================================================================
11 # Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
12 # project. The module is, however, dual licensed under OpenSSL and
13 # CRYPTOGAMS licenses depending on where you obtain it. For further
14 # details see http://www.openssl.org/~appro/cryptogams/.
15 # ====================================================================
17 # Eternal question is what's wrong with compiler generated code? The
18 # trick is that it's possible to reduce the number of shifts required
19 # to perform rotations by maintaining copy of 32-bit value in upper
20 # bits of 64-bit register. Just follow mux2 and shrp instructions...
21 # Performance under big-endian OS such as HP-UX is 179MBps*1GHz, which
22 # is >50% better than HP C and >2x better than gcc.
27 .ident \"sha1-ia64.s, version 1.3\"
28 .ident \"IA-64 ISA artwork by Andy Polyakov <appro\@fy.chalmers.se>\"
36 for (@ARGV) { $ADDP="add" if (/[\+DD|\-mlp]64/); }
37 } else { $ADDP="add"; }
40 if ($human) { # useful for visual code auditing...
41 ($A,$B,$C,$D,$E) = ("A","B","C","D","E");
42 ($h0,$h1,$h2,$h3,$h4) = ("h0","h1","h2","h3","h4");
43 ($K_00_19, $K_20_39, $K_40_59, $K_60_79) =
44 ( "K_00_19","K_20_39","K_40_59","K_60_79" );
45 @X= ( "X0", "X1", "X2", "X3", "X4", "X5", "X6", "X7",
46 "X8", "X9","X10","X11","X12","X13","X14","X15" );
49 ($A,$B,$C,$D,$E) = ("loc0","loc1","loc2","loc3","loc4");
50 ($h0,$h1,$h2,$h3,$h4) = ("loc5","loc6","loc7","loc8","loc9");
51 ($K_00_19, $K_20_39, $K_40_59, $K_60_79) =
52 ( "r14", "r15", "loc10", "loc11" );
53 @X= ( "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
54 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31" );
59 my ($i,$a,$b,$c,$d,$e)=@_;
63 $code.=<<___ if ($i==0);
64 { .mmi; ld1 $X[$i]=[inp],2 // MSB
66 { .mmi; ld1 tmp0=[inp],2
67 ld1 tmp4=[tmp3],2 // LSB
68 dep $X[$i]=$X[$i],tmp2,8,8 };;
72 { .mmi; ld1 $Xn=[inp],2 // forward Xload
74 dep tmp1=tmp0,tmp4,8,8 };;
75 { .mmi; ld1 tmp2=[tmp3],2 // forward Xload
77 dep $X[$i]=$X[$i],tmp1,16,16} //;;
78 { .mmi; add $e=$e,$K_00_19 // e+=K_00_19
80 dep.z tmp5=$a,5,27 };; // a<<5
81 { .mmi; add $e=$e,$X[$i] // e+=Xload
82 or tmp4=tmp4,tmp1 // F_00_19(b,c,d)=(b&c)|(~b&d)
83 extr.u tmp1=$a,27,5 };; // a>>27
84 { .mmi; ld1 tmp0=[inp],2 // forward Xload
85 add $e=$e,tmp4 // e+=F_00_19(b,c,d)
86 shrp $b=tmp6,tmp6,2 } // b=ROTATE(b,30)
87 { .mmi; ld1 tmp4=[tmp3],2 // forward Xload
88 or tmp5=tmp1,tmp5 // ROTATE(a,5)
89 mux2 tmp6=$a,0x44 };; // see b in next iteration
90 { .mii; add $e=$e,tmp5 // e+=ROTATE(a,5)
91 dep $Xn=$Xn,tmp2,8,8 // forward Xload
92 mux2 $X[$i]=$X[$i],0x44 } //;;
98 { .mii; and tmp3=$c,$b
99 dep tmp1=tmp0,tmp4,8,8;;
100 dep $X[$i]=$X[$i],tmp1,16,16} //;;
101 { .mmi; add $e=$e,$K_00_19 // e+=K_00_19
103 dep.z tmp5=$a,5,27 };; // a<<5
104 { .mmi; add $e=$e,$X[$i] // e+=Xupdate
105 or tmp4=tmp3,tmp1 // F_00_19(b,c,d)=(b&c)|(~b&d)
106 extr.u tmp1=$a,27,5 } // a>>27
107 { .mmi; xor $Xn=$Xn,$X[($j+2)%16] // forward Xupdate
108 xor tmp3=$X[($j+8)%16],$X[($j+13)%16] // forward Xupdate
110 { .mmi; add $e=$e,tmp4 // e+=F_00_19(b,c,d)
111 xor $Xn=$Xn,tmp3 // forward Xupdate
112 shrp $b=tmp6,tmp6,2 } // b=ROTATE(b,30)
113 { .mmi; or tmp1=tmp1,tmp5 // ROTATE(a,5)
114 mux2 tmp6=$a,0x44 };; // see b in next iteration
115 { .mii; add $e=$e,tmp1 // e+=ROTATE(a,5)
116 shrp $Xn=$Xn,$Xn,31 // ROTATE(x[0]^x[2]^x[8]^x[13],1)
117 mux2 $X[$i]=$X[$i],0x44 };;
125 my ($i,$a,$b,$c,$d,$e)=@_;
130 { .mib; add $e=$e,$K_00_19 // e+=K_00_19
131 dep.z tmp5=$a,5,27 } // a<<5
132 { .mib; andcm tmp1=$d,$b
134 { .mmi; add $e=$e,$X[$i%16] // e+=Xupdate
135 or tmp0=tmp0,tmp1 // F_00_19(b,c,d)=(b&c)|(~b&d)
136 extr.u tmp1=$a,27,5 } // a>>27
137 { .mmi; xor $Xn=$Xn,$X[($j+2)%16] // forward Xupdate
138 xor tmp3=$X[($j+8)%16],$X[($j+13)%16] // forward Xupdate
140 { .mmi; add $e=$e,tmp0 // f+=F_00_19(b,c,d)
141 xor $Xn=$Xn,tmp3 // forward Xupdate
142 shrp $b=tmp6,tmp6,2 } // b=ROTATE(b,30)
143 { .mmi; or tmp1=tmp1,tmp5 // ROTATE(a,5)
144 mux2 tmp6=$a,0x44 };; // see b in next iteration
145 { .mii; add $e=$e,tmp1 // e+=ROTATE(a,5)
146 shrp $Xn=$Xn,$Xn,31 // ROTATE(x[0]^x[2]^x[8]^x[13],1)
154 my ($i,$a,$b,$c,$d,$e,$Konst)=@_;
155 $Konst = $K_20_39 if (!defined($Konst));
161 { .mib; add $e=$e,$Konst // e+=K_XX_XX
162 dep.z tmp5=$a,5,27 } // a<<5
163 { .mib; xor tmp0=$c,$b
164 xor $Xn=$Xn,$X[($j+2)%16] };; // forward Xupdate
165 { .mib; add $e=$e,$X[$i%16] // e+=Xupdate
166 extr.u tmp1=$a,27,5 } // a>>27
167 { .mib; xor tmp0=tmp0,$d // F_20_39(b,c,d)=b^c^d
168 xor $Xn=$Xn,$X[($j+8)%16] };; // forward Xupdate
169 { .mmi; add $e=$e,tmp0 // e+=F_20_39(b,c,d)
170 xor $Xn=$Xn,$X[($j+13)%16] // forward Xupdate
171 shrp $b=tmp6,tmp6,2 } // b=ROTATE(b,30)
172 { .mmi; or tmp1=tmp1,tmp5 // ROTATE(a,5)
173 mux2 tmp6=$a,0x44 };; // see b in next iteration
174 { .mii; add $e=$e,tmp1 // e+=ROTATE(a,5)
175 shrp $Xn=$Xn,$Xn,31 // ROTATE(x[0]^x[2]^x[8]^x[13],1)
182 { .mib; add $e=$e,$Konst // e+=K_60_79
183 dep.z tmp5=$a,5,27 } // a<<5
184 { .mib; xor tmp0=$c,$b
185 add $h1=$h1,$a };; // wrap up
186 { .mib; add $e=$e,$X[$i%16] // e+=Xupdate
187 extr.u tmp1=$a,27,5 } // a>>27
188 { .mib; xor tmp0=tmp0,$d // F_20_39(b,c,d)=b^c^d
189 add $h3=$h3,$c };; // wrap up
190 { .mmi; add $e=$e,tmp0 // e+=F_20_39(b,c,d)
191 or tmp1=tmp1,tmp5 // ROTATE(a,5)
192 shrp $b=tmp6,tmp6,2 };; // b=ROTATE(b,30) ;;?
193 { .mmi; add $e=$e,tmp1 // e+=ROTATE(a,5)
194 add tmp3=1,inp // used in unaligned codepath
195 add $h4=$h4,$d };; // wrap up
203 my ($i,$a,$b,$c,$d,$e)=@_;
208 { .mib; add $e=$e,$K_40_59 // e+=K_40_59
209 dep.z tmp5=$a,5,27 } // a<<5
210 { .mib; and tmp1=$c,$d
212 { .mmi; add $e=$e,$X[$i%16] // e+=Xupdate
213 add tmp5=tmp5,tmp1 // a<<5+(c&d)
214 extr.u tmp1=$a,27,5 } // a>>27
215 { .mmi; and tmp0=tmp0,$b
216 xor $Xn=$Xn,$X[($j+2)%16] // forward Xupdate
217 xor tmp3=$X[($j+8)%16],$X[($j+13)%16] };; // forward Xupdate
218 { .mmi; add $e=$e,tmp0 // e+=b&(c^d)
219 add tmp5=tmp5,tmp1 // ROTATE(a,5)+(c&d)
220 shrp $b=tmp6,tmp6,2 } // b=ROTATE(b,30)
221 { .mmi; xor $Xn=$Xn,tmp3
222 mux2 tmp6=$a,0x44 };; // see b in next iteration
223 { .mii; add $e=$e,tmp5 // e+=ROTATE(a,5)+(c&d)
224 shrp $Xn=$Xn,$Xn,31 // ROTATE(x[0]^x[2]^x[8]^x[13],1)
229 sub BODY_60_79 { &BODY_20_39(@_,$K_60_79); }
241 // void sha1_block_data_order(SHA_CTX *c,const void *p,size_t num);
242 .global sha1_block_data_order#
243 .proc sha1_block_data_order#
245 sha1_block_data_order:
247 { .mmi; alloc tmp1=ar.pfs,3,14,0,0
251 { .mmi; $ADDP ctx=0,ctx
258 { .mlx; ld4 $h0=[ctx],8
259 movl $K_00_19=0x5a827999 }
260 { .mlx; ld4 $h1=[tmp0],8
261 movl $K_20_39=0x6ed9eba1 };;
262 { .mlx; ld4 $h2=[ctx],8
263 movl $K_40_59=0x8f1bbcdc }
264 { .mlx; ld4 $h3=[tmp0]
265 movl $K_60_79=0xca62c1d6 };;
266 { .mmi; ld4 $h4=[ctx],-16
267 add in2=-1,in2 // adjust num for ar.lc
271 mov ar.lc=in2 };; // brp.loop.imp: too far
284 my @V=($A,$B,$C,$D,$E);
286 for($i=0;$i<16;$i++) { &BODY_00_15(\$code,$i,@V); unshift(@V,pop(@V)); }
287 for(;$i<20;$i++) { &BODY_16_19(\$code,$i,@V); unshift(@V,pop(@V)); }
288 for(;$i<40;$i++) { &BODY_20_39(\$code,$i,@V); unshift(@V,pop(@V)); }
289 for(;$i<60;$i++) { &BODY_40_59(\$code,$i,@V); unshift(@V,pop(@V)); }
290 for(;$i<80;$i++) { &BODY_60_79(\$code,$i,@V); unshift(@V,pop(@V)); }
292 (($V[0] eq $A) and ($V[4] eq $E)) or die; # double-check
296 { .mmb; add $h0=$h0,$A
298 br.ctop.dptk.many .Ldtop };;
300 { .mmi; add tmp0=4,ctx
302 { .mmi; st4 [ctx]=$h0,8
304 { .mmi; st4 [ctx]=$h2,8
306 { .mib; st4 [ctx]=$h4,-16
308 br.ret.sptk.many b0 };;
309 .endp sha1_block_data_order#
310 stringz "SHA1 block transform for IA64, CRYPTOGAMS by <appro\@openssl.org>"
313 open STDOUT,">$output" if $output;