sha1-586.pl 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #!/usr/bin/env perl
  2. # ====================================================================
  3. # [Re]written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
  4. # project. The module is, however, dual licensed under OpenSSL and
  5. # CRYPTOGAMS licenses depending on where you obtain it. For further
  6. # details see http://www.openssl.org/~appro/cryptogams/.
  7. # ====================================================================
  8. # "[Re]written" was achieved in two major overhauls. In 2004 BODY_*
  9. # functions were re-implemented to address P4 performance issue [see
  10. # commentary below], and in 2006 the rest was rewritten in order to
  11. # gain freedom to liberate licensing terms.
  12. # It was noted that Intel IA-32 C compiler generates code which
  13. # performs ~30% *faster* on P4 CPU than original *hand-coded*
  14. # SHA1 assembler implementation. To address this problem (and
  15. # prove that humans are still better than machines:-), the
  16. # original code was overhauled, which resulted in following
  17. # performance changes:
  18. #
  19. # compared with original compared with Intel cc
  20. # assembler impl. generated code
  21. # Pentium -16% +48%
  22. # PIII/AMD +8% +16%
  23. # P4 +85%(!) +45%
  24. #
  25. # As you can see Pentium came out as looser:-( Yet I reckoned that
  26. # improvement on P4 outweights the loss and incorporate this
  27. # re-tuned code to 0.9.7 and later.
  28. # ----------------------------------------------------------------
  29. # <appro@fy.chalmers.se>
  30. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  31. push(@INC,"${dir}","${dir}../../perlasm");
  32. require "x86asm.pl";
  33. &asm_init($ARGV[0],"sha1-586.pl",$ARGV[$#ARGV] eq "386");
  34. $A="eax";
  35. $B="ebx";
  36. $C="ecx";
  37. $D="edx";
  38. $E="edi";
  39. $T="esi";
  40. $tmp1="ebp";
  41. @V=($A,$B,$C,$D,$E,$T);
  42. sub BODY_00_15
  43. {
  44. local($n,$a,$b,$c,$d,$e,$f)=@_;
  45. &comment("00_15 $n");
  46. &mov($f,$c); # f to hold F_00_19(b,c,d)
  47. if ($n==0) { &mov($tmp1,$a); }
  48. else { &mov($a,$tmp1); }
  49. &rotl($tmp1,5); # tmp1=ROTATE(a,5)
  50. &xor($f,$d);
  51. &add($tmp1,$e); # tmp1+=e;
  52. &and($f,$b);
  53. &mov($e,&swtmp($n%16)); # e becomes volatile and is loaded
  54. # with xi, also note that e becomes
  55. # f in next round...
  56. &xor($f,$d); # f holds F_00_19(b,c,d)
  57. &rotr($b,2); # b=ROTATE(b,30)
  58. &lea($tmp1,&DWP(0x5a827999,$tmp1,$e)); # tmp1+=K_00_19+xi
  59. if ($n==15) { &add($f,$tmp1); } # f+=tmp1
  60. else { &add($tmp1,$f); } # f becomes a in next round
  61. }
  62. sub BODY_16_19
  63. {
  64. local($n,$a,$b,$c,$d,$e,$f)=@_;
  65. &comment("16_19 $n");
  66. &mov($f,&swtmp($n%16)); # f to hold Xupdate(xi,xa,xb,xc,xd)
  67. &mov($tmp1,$c); # tmp1 to hold F_00_19(b,c,d)
  68. &xor($f,&swtmp(($n+2)%16));
  69. &xor($tmp1,$d);
  70. &xor($f,&swtmp(($n+8)%16));
  71. &and($tmp1,$b); # tmp1 holds F_00_19(b,c,d)
  72. &rotr($b,2); # b=ROTATE(b,30)
  73. &xor($f,&swtmp(($n+13)%16)); # f holds xa^xb^xc^xd
  74. &rotl($f,1); # f=ROTATE(f,1)
  75. &xor($tmp1,$d); # tmp1=F_00_19(b,c,d)
  76. &mov(&swtmp($n%16),$f); # xi=f
  77. &lea($f,&DWP(0x5a827999,$f,$e));# f+=K_00_19+e
  78. &mov($e,$a); # e becomes volatile
  79. &rotl($e,5); # e=ROTATE(a,5)
  80. &add($f,$tmp1); # f+=F_00_19(b,c,d)
  81. &add($f,$e); # f+=ROTATE(a,5)
  82. }
  83. sub BODY_20_39
  84. {
  85. local($n,$a,$b,$c,$d,$e,$f)=@_;
  86. local $K=($n<40)?0x6ed9eba1:0xca62c1d6;
  87. &comment("20_39 $n");
  88. &mov($tmp1,$b); # tmp1 to hold F_20_39(b,c,d)
  89. &mov($f,&swtmp($n%16)); # f to hold Xupdate(xi,xa,xb,xc,xd)
  90. &rotr($b,2); # b=ROTATE(b,30)
  91. &xor($f,&swtmp(($n+2)%16));
  92. &xor($tmp1,$c);
  93. &xor($f,&swtmp(($n+8)%16));
  94. &xor($tmp1,$d); # tmp1 holds F_20_39(b,c,d)
  95. &xor($f,&swtmp(($n+13)%16)); # f holds xa^xb^xc^xd
  96. &rotl($f,1); # f=ROTATE(f,1)
  97. &add($tmp1,$e);
  98. &mov(&swtmp($n%16),$f); # xi=f
  99. &mov($e,$a); # e becomes volatile
  100. &rotl($e,5); # e=ROTATE(a,5)
  101. &lea($f,&DWP($K,$f,$tmp1)); # f+=K_20_39+e
  102. &add($f,$e); # f+=ROTATE(a,5)
  103. }
  104. sub BODY_40_59
  105. {
  106. local($n,$a,$b,$c,$d,$e,$f)=@_;
  107. &comment("40_59 $n");
  108. &mov($f,&swtmp($n%16)); # f to hold Xupdate(xi,xa,xb,xc,xd)
  109. &mov($tmp1,&swtmp(($n+2)%16));
  110. &xor($f,$tmp1);
  111. &mov($tmp1,&swtmp(($n+8)%16));
  112. &xor($f,$tmp1);
  113. &mov($tmp1,&swtmp(($n+13)%16));
  114. &xor($f,$tmp1); # f holds xa^xb^xc^xd
  115. &mov($tmp1,$b); # tmp1 to hold F_40_59(b,c,d)
  116. &rotl($f,1); # f=ROTATE(f,1)
  117. &or($tmp1,$c);
  118. &mov(&swtmp($n%16),$f); # xi=f
  119. &and($tmp1,$d);
  120. &lea($f,&DWP(0x8f1bbcdc,$f,$e));# f+=K_40_59+e
  121. &mov($e,$b); # e becomes volatile and is used
  122. # to calculate F_40_59(b,c,d)
  123. &rotr($b,2); # b=ROTATE(b,30)
  124. &and($e,$c);
  125. &or($tmp1,$e); # tmp1 holds F_40_59(b,c,d)
  126. &mov($e,$a);
  127. &rotl($e,5); # e=ROTATE(a,5)
  128. &add($f,$tmp1); # f+=tmp1;
  129. &add($f,$e); # f+=ROTATE(a,5)
  130. }
  131. &function_begin("sha1_block_data_order");
  132. &mov($tmp1,&wparam(0)); # SHA_CTX *c
  133. &mov($T,&wparam(1)); # const void *input
  134. &mov($A,&wparam(2)); # size_t num
  135. &stack_push(16); # allocate X[16]
  136. &shl($A,6);
  137. &add($A,$T);
  138. &mov(&wparam(2),$A); # pointer beyond the end of input
  139. &mov($E,&DWP(16,$tmp1));# pre-load E
  140. &set_label("loop",16);
  141. # copy input chunk to X, but reversing byte order!
  142. for ($i=0; $i<16; $i+=4)
  143. {
  144. &mov($A,&DWP(4*($i+0),$T));
  145. &mov($B,&DWP(4*($i+1),$T));
  146. &mov($C,&DWP(4*($i+2),$T));
  147. &mov($D,&DWP(4*($i+3),$T));
  148. &bswap($A);
  149. &bswap($B);
  150. &bswap($C);
  151. &bswap($D);
  152. &mov(&swtmp($i+0),$A);
  153. &mov(&swtmp($i+1),$B);
  154. &mov(&swtmp($i+2),$C);
  155. &mov(&swtmp($i+3),$D);
  156. }
  157. &mov(&wparam(1),$T); # redundant in 1st spin
  158. &mov($A,&DWP(0,$tmp1)); # load SHA_CTX
  159. &mov($B,&DWP(4,$tmp1));
  160. &mov($C,&DWP(8,$tmp1));
  161. &mov($D,&DWP(12,$tmp1));
  162. # E is pre-loaded
  163. for($i=0;$i<16;$i++) { &BODY_00_15($i,@V); unshift(@V,pop(@V)); }
  164. for(;$i<20;$i++) { &BODY_16_19($i,@V); unshift(@V,pop(@V)); }
  165. for(;$i<40;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
  166. for(;$i<60;$i++) { &BODY_40_59($i,@V); unshift(@V,pop(@V)); }
  167. for(;$i<80;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
  168. (($V[5] eq $D) and ($V[0] eq $E)) or die; # double-check
  169. &mov($tmp1,&wparam(0)); # re-load SHA_CTX*
  170. &mov($D,&wparam(1)); # D is last "T" and is discarded
  171. &add($E,&DWP(0,$tmp1)); # E is last "A"...
  172. &add($T,&DWP(4,$tmp1));
  173. &add($A,&DWP(8,$tmp1));
  174. &add($B,&DWP(12,$tmp1));
  175. &add($C,&DWP(16,$tmp1));
  176. &mov(&DWP(0,$tmp1),$E); # update SHA_CTX
  177. &add($D,64); # advance input pointer
  178. &mov(&DWP(4,$tmp1),$T);
  179. &cmp($D,&wparam(2)); # have we reached the end yet?
  180. &mov(&DWP(8,$tmp1),$A);
  181. &mov($E,$C); # C is last "E" which needs to be "pre-loaded"
  182. &mov(&DWP(12,$tmp1),$B);
  183. &mov($T,$D); # input pointer
  184. &mov(&DWP(16,$tmp1),$C);
  185. &jb(&label("loop"));
  186. &stack_pop(16);
  187. &function_end("sha1_block_data_order");
  188. &asm_finish();