2
0

sha1-ia64.pl 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #! /usr/bin/env perl
  2. # Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  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
  8. #
  9. # ====================================================================
  10. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  11. # project. The module is, however, dual licensed under OpenSSL and
  12. # CRYPTOGAMS licenses depending on where you obtain it. For further
  13. # details see http://www.openssl.org/~appro/cryptogams/.
  14. # ====================================================================
  15. #
  16. # Eternal question is what's wrong with compiler generated code? The
  17. # trick is that it's possible to reduce the number of shifts required
  18. # to perform rotations by maintaining copy of 32-bit value in upper
  19. # bits of 64-bit register. Just follow mux2 and shrp instructions...
  20. # Performance under big-endian OS such as HP-UX is 179MBps*1GHz, which
  21. # is >50% better than HP C and >2x better than gcc.
  22. $output = pop;
  23. $code=<<___;
  24. .ident \"sha1-ia64.s, version 1.3\"
  25. .ident \"IA-64 ISA artwork by Andy Polyakov <appro\@fy.chalmers.se>\"
  26. .explicit
  27. ___
  28. if ($^O eq "hpux") {
  29. $ADDP="addp4";
  30. for (@ARGV) { $ADDP="add" if (/[\+DD|\-mlp]64/); }
  31. } else { $ADDP="add"; }
  32. #$human=1;
  33. if ($human) { # useful for visual code auditing...
  34. ($A,$B,$C,$D,$E) = ("A","B","C","D","E");
  35. ($h0,$h1,$h2,$h3,$h4) = ("h0","h1","h2","h3","h4");
  36. ($K_00_19, $K_20_39, $K_40_59, $K_60_79) =
  37. ( "K_00_19","K_20_39","K_40_59","K_60_79" );
  38. @X= ( "X0", "X1", "X2", "X3", "X4", "X5", "X6", "X7",
  39. "X8", "X9","X10","X11","X12","X13","X14","X15" );
  40. }
  41. else {
  42. ($A,$B,$C,$D,$E) = ("loc0","loc1","loc2","loc3","loc4");
  43. ($h0,$h1,$h2,$h3,$h4) = ("loc5","loc6","loc7","loc8","loc9");
  44. ($K_00_19, $K_20_39, $K_40_59, $K_60_79) =
  45. ( "r14", "r15", "loc10", "loc11" );
  46. @X= ( "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  47. "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31" );
  48. }
  49. sub BODY_00_15 {
  50. local *code=shift;
  51. my ($i,$a,$b,$c,$d,$e)=@_;
  52. my $j=$i+1;
  53. my $Xn=@X[$j%16];
  54. $code.=<<___ if ($i==0);
  55. { .mmi; ld1 $X[$i]=[inp],2 // MSB
  56. ld1 tmp2=[tmp3],2 };;
  57. { .mmi; ld1 tmp0=[inp],2
  58. ld1 tmp4=[tmp3],2 // LSB
  59. dep $X[$i]=$X[$i],tmp2,8,8 };;
  60. ___
  61. if ($i<15) {
  62. $code.=<<___;
  63. { .mmi; ld1 $Xn=[inp],2 // forward Xload
  64. nop.m 0x0
  65. dep tmp1=tmp0,tmp4,8,8 };;
  66. { .mmi; ld1 tmp2=[tmp3],2 // forward Xload
  67. and tmp4=$c,$b
  68. dep $X[$i]=$X[$i],tmp1,16,16} //;;
  69. { .mmi; add $e=$e,$K_00_19 // e+=K_00_19
  70. andcm tmp1=$d,$b
  71. dep.z tmp5=$a,5,27 };; // a<<5
  72. { .mmi; add $e=$e,$X[$i] // e+=Xload
  73. or tmp4=tmp4,tmp1 // F_00_19(b,c,d)=(b&c)|(~b&d)
  74. extr.u tmp1=$a,27,5 };; // a>>27
  75. { .mmi; ld1 tmp0=[inp],2 // forward Xload
  76. add $e=$e,tmp4 // e+=F_00_19(b,c,d)
  77. shrp $b=tmp6,tmp6,2 } // b=ROTATE(b,30)
  78. { .mmi; ld1 tmp4=[tmp3],2 // forward Xload
  79. or tmp5=tmp1,tmp5 // ROTATE(a,5)
  80. mux2 tmp6=$a,0x44 };; // see b in next iteration
  81. { .mii; add $e=$e,tmp5 // e+=ROTATE(a,5)
  82. dep $Xn=$Xn,tmp2,8,8 // forward Xload
  83. mux2 $X[$i]=$X[$i],0x44 } //;;
  84. ___
  85. }
  86. else {
  87. $code.=<<___;
  88. { .mii; and tmp3=$c,$b
  89. dep tmp1=tmp0,tmp4,8,8;;
  90. dep $X[$i]=$X[$i],tmp1,16,16} //;;
  91. { .mmi; add $e=$e,$K_00_19 // e+=K_00_19
  92. andcm tmp1=$d,$b
  93. dep.z tmp5=$a,5,27 };; // a<<5
  94. { .mmi; add $e=$e,$X[$i] // e+=Xupdate
  95. or tmp4=tmp3,tmp1 // F_00_19(b,c,d)=(b&c)|(~b&d)
  96. extr.u tmp1=$a,27,5 } // a>>27
  97. { .mmi; xor $Xn=$Xn,$X[($j+2)%16] // forward Xupdate
  98. xor tmp3=$X[($j+8)%16],$X[($j+13)%16] // forward Xupdate
  99. nop.i 0 };;
  100. { .mmi; add $e=$e,tmp4 // e+=F_00_19(b,c,d)
  101. xor $Xn=$Xn,tmp3 // forward Xupdate
  102. shrp $b=tmp6,tmp6,2 } // b=ROTATE(b,30)
  103. { .mmi; or tmp1=tmp1,tmp5 // ROTATE(a,5)
  104. mux2 tmp6=$a,0x44 };; // see b in next iteration
  105. { .mii; add $e=$e,tmp1 // e+=ROTATE(a,5)
  106. shrp $Xn=$Xn,$Xn,31 // ROTATE(x[0]^x[2]^x[8]^x[13],1)
  107. mux2 $X[$i]=$X[$i],0x44 };;
  108. ___
  109. }
  110. }
  111. sub BODY_16_19 {
  112. local *code=shift;
  113. my ($i,$a,$b,$c,$d,$e)=@_;
  114. my $j=$i+1;
  115. my $Xn=@X[$j%16];
  116. $code.=<<___;
  117. { .mib; add $e=$e,$K_00_19 // e+=K_00_19
  118. dep.z tmp5=$a,5,27 } // a<<5
  119. { .mib; andcm tmp1=$d,$b
  120. and tmp0=$c,$b };;
  121. { .mmi; add $e=$e,$X[$i%16] // e+=Xupdate
  122. or tmp0=tmp0,tmp1 // F_00_19(b,c,d)=(b&c)|(~b&d)
  123. extr.u tmp1=$a,27,5 } // a>>27
  124. { .mmi; xor $Xn=$Xn,$X[($j+2)%16] // forward Xupdate
  125. xor tmp3=$X[($j+8)%16],$X[($j+13)%16] // forward Xupdate
  126. nop.i 0 };;
  127. { .mmi; add $e=$e,tmp0 // f+=F_00_19(b,c,d)
  128. xor $Xn=$Xn,tmp3 // forward Xupdate
  129. shrp $b=tmp6,tmp6,2 } // b=ROTATE(b,30)
  130. { .mmi; or tmp1=tmp1,tmp5 // ROTATE(a,5)
  131. mux2 tmp6=$a,0x44 };; // see b in next iteration
  132. { .mii; add $e=$e,tmp1 // e+=ROTATE(a,5)
  133. shrp $Xn=$Xn,$Xn,31 // ROTATE(x[0]^x[2]^x[8]^x[13],1)
  134. nop.i 0 };;
  135. ___
  136. }
  137. sub BODY_20_39 {
  138. local *code=shift;
  139. my ($i,$a,$b,$c,$d,$e,$Konst)=@_;
  140. $Konst = $K_20_39 if (!defined($Konst));
  141. my $j=$i+1;
  142. my $Xn=@X[$j%16];
  143. if ($i<79) {
  144. $code.=<<___;
  145. { .mib; add $e=$e,$Konst // e+=K_XX_XX
  146. dep.z tmp5=$a,5,27 } // a<<5
  147. { .mib; xor tmp0=$c,$b
  148. xor $Xn=$Xn,$X[($j+2)%16] };; // forward Xupdate
  149. { .mib; add $e=$e,$X[$i%16] // e+=Xupdate
  150. extr.u tmp1=$a,27,5 } // a>>27
  151. { .mib; xor tmp0=tmp0,$d // F_20_39(b,c,d)=b^c^d
  152. xor $Xn=$Xn,$X[($j+8)%16] };; // forward Xupdate
  153. { .mmi; add $e=$e,tmp0 // e+=F_20_39(b,c,d)
  154. xor $Xn=$Xn,$X[($j+13)%16] // forward Xupdate
  155. shrp $b=tmp6,tmp6,2 } // b=ROTATE(b,30)
  156. { .mmi; or tmp1=tmp1,tmp5 // ROTATE(a,5)
  157. mux2 tmp6=$a,0x44 };; // see b in next iteration
  158. { .mii; add $e=$e,tmp1 // e+=ROTATE(a,5)
  159. shrp $Xn=$Xn,$Xn,31 // ROTATE(x[0]^x[2]^x[8]^x[13],1)
  160. nop.i 0 };;
  161. ___
  162. }
  163. else {
  164. $code.=<<___;
  165. { .mib; add $e=$e,$Konst // e+=K_60_79
  166. dep.z tmp5=$a,5,27 } // a<<5
  167. { .mib; xor tmp0=$c,$b
  168. add $h1=$h1,$a };; // wrap up
  169. { .mib; add $e=$e,$X[$i%16] // e+=Xupdate
  170. extr.u tmp1=$a,27,5 } // a>>27
  171. { .mib; xor tmp0=tmp0,$d // F_20_39(b,c,d)=b^c^d
  172. add $h3=$h3,$c };; // wrap up
  173. { .mmi; add $e=$e,tmp0 // e+=F_20_39(b,c,d)
  174. or tmp1=tmp1,tmp5 // ROTATE(a,5)
  175. shrp $b=tmp6,tmp6,2 };; // b=ROTATE(b,30) ;;?
  176. { .mmi; add $e=$e,tmp1 // e+=ROTATE(a,5)
  177. add tmp3=1,inp // used in unaligned codepath
  178. add $h4=$h4,$d };; // wrap up
  179. ___
  180. }
  181. }
  182. sub BODY_40_59 {
  183. local *code=shift;
  184. my ($i,$a,$b,$c,$d,$e)=@_;
  185. my $j=$i+1;
  186. my $Xn=@X[$j%16];
  187. $code.=<<___;
  188. { .mib; add $e=$e,$K_40_59 // e+=K_40_59
  189. dep.z tmp5=$a,5,27 } // a<<5
  190. { .mib; and tmp1=$c,$d
  191. xor tmp0=$c,$d };;
  192. { .mmi; add $e=$e,$X[$i%16] // e+=Xupdate
  193. add tmp5=tmp5,tmp1 // a<<5+(c&d)
  194. extr.u tmp1=$a,27,5 } // a>>27
  195. { .mmi; and tmp0=tmp0,$b
  196. xor $Xn=$Xn,$X[($j+2)%16] // forward Xupdate
  197. xor tmp3=$X[($j+8)%16],$X[($j+13)%16] };; // forward Xupdate
  198. { .mmi; add $e=$e,tmp0 // e+=b&(c^d)
  199. add tmp5=tmp5,tmp1 // ROTATE(a,5)+(c&d)
  200. shrp $b=tmp6,tmp6,2 } // b=ROTATE(b,30)
  201. { .mmi; xor $Xn=$Xn,tmp3
  202. mux2 tmp6=$a,0x44 };; // see b in next iteration
  203. { .mii; add $e=$e,tmp5 // e+=ROTATE(a,5)+(c&d)
  204. shrp $Xn=$Xn,$Xn,31 // ROTATE(x[0]^x[2]^x[8]^x[13],1)
  205. nop.i 0x0 };;
  206. ___
  207. }
  208. sub BODY_60_79 { &BODY_20_39(@_,$K_60_79); }
  209. $code.=<<___;
  210. .text
  211. tmp0=r8;
  212. tmp1=r9;
  213. tmp2=r10;
  214. tmp3=r11;
  215. ctx=r32; // in0
  216. inp=r33; // in1
  217. // void sha1_block_data_order(SHA_CTX *c,const void *p,size_t num);
  218. .global sha1_block_data_order#
  219. .proc sha1_block_data_order#
  220. .align 32
  221. sha1_block_data_order:
  222. .prologue
  223. { .mmi; alloc tmp1=ar.pfs,3,14,0,0
  224. $ADDP tmp0=4,ctx
  225. .save ar.lc,r3
  226. mov r3=ar.lc }
  227. { .mmi; $ADDP ctx=0,ctx
  228. $ADDP inp=0,inp
  229. mov r2=pr };;
  230. tmp4=in2;
  231. tmp5=loc12;
  232. tmp6=loc13;
  233. .body
  234. { .mlx; ld4 $h0=[ctx],8
  235. movl $K_00_19=0x5a827999 }
  236. { .mlx; ld4 $h1=[tmp0],8
  237. movl $K_20_39=0x6ed9eba1 };;
  238. { .mlx; ld4 $h2=[ctx],8
  239. movl $K_40_59=0x8f1bbcdc }
  240. { .mlx; ld4 $h3=[tmp0]
  241. movl $K_60_79=0xca62c1d6 };;
  242. { .mmi; ld4 $h4=[ctx],-16
  243. add in2=-1,in2 // adjust num for ar.lc
  244. mov ar.ec=1 };;
  245. { .mmi; nop.m 0
  246. add tmp3=1,inp
  247. mov ar.lc=in2 };; // brp.loop.imp: too far
  248. .Ldtop:
  249. { .mmi; mov $A=$h0
  250. mov $B=$h1
  251. mux2 tmp6=$h1,0x44 }
  252. { .mmi; mov $C=$h2
  253. mov $D=$h3
  254. mov $E=$h4 };;
  255. ___
  256. { my $i;
  257. my @V=($A,$B,$C,$D,$E);
  258. for($i=0;$i<16;$i++) { &BODY_00_15(\$code,$i,@V); unshift(@V,pop(@V)); }
  259. for(;$i<20;$i++) { &BODY_16_19(\$code,$i,@V); unshift(@V,pop(@V)); }
  260. for(;$i<40;$i++) { &BODY_20_39(\$code,$i,@V); unshift(@V,pop(@V)); }
  261. for(;$i<60;$i++) { &BODY_40_59(\$code,$i,@V); unshift(@V,pop(@V)); }
  262. for(;$i<80;$i++) { &BODY_60_79(\$code,$i,@V); unshift(@V,pop(@V)); }
  263. (($V[0] eq $A) and ($V[4] eq $E)) or die; # double-check
  264. }
  265. $code.=<<___;
  266. { .mmb; add $h0=$h0,$A
  267. add $h2=$h2,$C
  268. br.ctop.dptk.many .Ldtop };;
  269. .Ldend:
  270. { .mmi; add tmp0=4,ctx
  271. mov ar.lc=r3 };;
  272. { .mmi; st4 [ctx]=$h0,8
  273. st4 [tmp0]=$h1,8 };;
  274. { .mmi; st4 [ctx]=$h2,8
  275. st4 [tmp0]=$h3 };;
  276. { .mib; st4 [ctx]=$h4,-16
  277. mov pr=r2,0x1ffff
  278. br.ret.sptk.many b0 };;
  279. .endp sha1_block_data_order#
  280. stringz "SHA1 block transform for IA64, CRYPTOGAMS by <appro\@openssl.org>"
  281. ___
  282. open STDOUT,">$output" if $output;
  283. print $code;