md5-586.pl 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #!/usr/local/bin/perl
  2. # Normal is the
  3. # md5_block_x86(MD5_CTX *c, ULONG *X);
  4. # version, non-normal is the
  5. # md5_block_x86(MD5_CTX *c, ULONG *X,int blocks);
  6. $normal=0;
  7. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  8. push(@INC,"${dir}","${dir}../../perlasm");
  9. require "x86asm.pl";
  10. &asm_init($ARGV[0],$0);
  11. $A="eax";
  12. $B="ebx";
  13. $C="ecx";
  14. $D="edx";
  15. $tmp1="edi";
  16. $tmp2="ebp";
  17. $X="esi";
  18. # What we need to load into $tmp for the next round
  19. %Ltmp1=("R0",&Np($C), "R1",&Np($C), "R2",&Np($C), "R3",&Np($D));
  20. @xo=(
  21. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, # R0
  22. 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, # R1
  23. 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2, # R2
  24. 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9, # R3
  25. );
  26. &md5_block("md5_block_asm_data_order");
  27. &asm_finish();
  28. sub Np
  29. {
  30. local($p)=@_;
  31. local(%n)=($A,$D,$B,$A,$C,$B,$D,$C);
  32. return($n{$p});
  33. }
  34. sub R0
  35. {
  36. local($pos,$a,$b,$c,$d,$K,$ki,$s,$t)=@_;
  37. &mov($tmp1,$C) if $pos < 0;
  38. &mov($tmp2,&DWP($xo[$ki]*4,$K,"",0)) if $pos < 0; # very first one
  39. # body proper
  40. &comment("R0 $ki");
  41. &xor($tmp1,$d); # F function - part 2
  42. &and($tmp1,$b); # F function - part 3
  43. &lea($a,&DWP($t,$a,$tmp2,1));
  44. &xor($tmp1,$d); # F function - part 4
  45. &add($a,$tmp1);
  46. &mov($tmp1,&Np($c)) if $pos < 1; # next tmp1 for R0
  47. &mov($tmp1,&Np($c)) if $pos == 1; # next tmp1 for R1
  48. &rotl($a,$s);
  49. &mov($tmp2,&DWP($xo[$ki+1]*4,$K,"",0)) if ($pos != 2);
  50. &add($a,$b);
  51. }
  52. sub R1
  53. {
  54. local($pos,$a,$b,$c,$d,$K,$ki,$s,$t)=@_;
  55. &comment("R1 $ki");
  56. &lea($a,&DWP($t,$a,$tmp2,1));
  57. &xor($tmp1,$b); # G function - part 2
  58. &and($tmp1,$d); # G function - part 3
  59. &mov($tmp2,&DWP($xo[$ki+1]*4,$K,"",0)) if ($pos != 2);
  60. &xor($tmp1,$c); # G function - part 4
  61. &add($a,$tmp1);
  62. &mov($tmp1,&Np($c)) if $pos < 1; # G function - part 1
  63. &mov($tmp1,&Np($c)) if $pos == 1; # G function - part 1
  64. &rotl($a,$s);
  65. &add($a,$b);
  66. }
  67. sub R2
  68. {
  69. local($n,$pos,$a,$b,$c,$d,$K,$ki,$s,$t)=@_;
  70. # This one is different, only 3 logical operations
  71. if (($n & 1) == 0)
  72. {
  73. &comment("R2 $ki");
  74. # make sure to do 'D' first, not 'B', else we clash with
  75. # the last add from the previous round.
  76. &xor($tmp1,$d); # H function - part 2
  77. &xor($tmp1,$b); # H function - part 3
  78. &lea($a,&DWP($t,$a,$tmp2,1));
  79. &add($a,$tmp1);
  80. &rotl($a,$s);
  81. &mov($tmp2,&DWP($xo[$ki+1]*4,$K,"",0));
  82. &mov($tmp1,&Np($c));
  83. }
  84. else
  85. {
  86. &comment("R2 $ki");
  87. # make sure to do 'D' first, not 'B', else we clash with
  88. # the last add from the previous round.
  89. &lea($a,&DWP($t,$a,$tmp2,1));
  90. &add($b,$c); # MOVED FORWARD
  91. &xor($tmp1,$d); # H function - part 2
  92. &xor($tmp1,$b); # H function - part 3
  93. &mov($tmp2,&DWP($xo[$ki+1]*4,$K,"",0)) if ($pos != 2);
  94. &add($a,$tmp1);
  95. &mov($tmp1,&Np($c)) if $pos < 1; # H function - part 1
  96. &mov($tmp1,-1) if $pos == 1; # I function - part 1
  97. &rotl($a,$s);
  98. &add($a,$b);
  99. }
  100. }
  101. sub R3
  102. {
  103. local($pos,$a,$b,$c,$d,$K,$ki,$s,$t)=@_;
  104. &comment("R3 $ki");
  105. # &not($tmp1)
  106. &xor($tmp1,$d) if $pos < 0; # I function - part 2
  107. &or($tmp1,$b); # I function - part 3
  108. &lea($a,&DWP($t,$a,$tmp2,1));
  109. &xor($tmp1,$c); # I function - part 4
  110. &mov($tmp2,&DWP($xo[$ki+1]*4,$K,"",0)) if $pos != 2; # load X/k value
  111. &mov($tmp2,&wparam(0)) if $pos == 2;
  112. &add($a,$tmp1);
  113. &mov($tmp1,-1) if $pos < 1; # H function - part 1
  114. &add($K,64) if $pos >=1 && !$normal;
  115. &rotl($a,$s);
  116. &xor($tmp1,&Np($d)) if $pos <= 0; # I function - part = first time
  117. &mov($tmp1,&DWP( 0,$tmp2,"",0)) if $pos > 0;
  118. &add($a,$b);
  119. }
  120. sub md5_block
  121. {
  122. local($name)=@_;
  123. &function_begin_B($name,"",3);
  124. # parameter 1 is the MD5_CTX structure.
  125. # A 0
  126. # B 4
  127. # C 8
  128. # D 12
  129. &push("esi");
  130. &push("edi");
  131. &mov($tmp1, &wparam(0)); # edi
  132. &mov($X, &wparam(1)); # esi
  133. &mov($C, &wparam(2));
  134. &push("ebp");
  135. &shl($C, 6);
  136. &push("ebx");
  137. &add($C, $X); # offset we end at
  138. &sub($C, 64);
  139. &mov($A, &DWP( 0,$tmp1,"",0));
  140. &push($C); # Put on the TOS
  141. &mov($B, &DWP( 4,$tmp1,"",0));
  142. &mov($C, &DWP( 8,$tmp1,"",0));
  143. &mov($D, &DWP(12,$tmp1,"",0));
  144. &set_label("start") unless $normal;
  145. &comment("");
  146. &comment("R0 section");
  147. &R0(-2,$A,$B,$C,$D,$X, 0, 7,0xd76aa478);
  148. &R0( 0,$D,$A,$B,$C,$X, 1,12,0xe8c7b756);
  149. &R0( 0,$C,$D,$A,$B,$X, 2,17,0x242070db);
  150. &R0( 0,$B,$C,$D,$A,$X, 3,22,0xc1bdceee);
  151. &R0( 0,$A,$B,$C,$D,$X, 4, 7,0xf57c0faf);
  152. &R0( 0,$D,$A,$B,$C,$X, 5,12,0x4787c62a);
  153. &R0( 0,$C,$D,$A,$B,$X, 6,17,0xa8304613);
  154. &R0( 0,$B,$C,$D,$A,$X, 7,22,0xfd469501);
  155. &R0( 0,$A,$B,$C,$D,$X, 8, 7,0x698098d8);
  156. &R0( 0,$D,$A,$B,$C,$X, 9,12,0x8b44f7af);
  157. &R0( 0,$C,$D,$A,$B,$X,10,17,0xffff5bb1);
  158. &R0( 0,$B,$C,$D,$A,$X,11,22,0x895cd7be);
  159. &R0( 0,$A,$B,$C,$D,$X,12, 7,0x6b901122);
  160. &R0( 0,$D,$A,$B,$C,$X,13,12,0xfd987193);
  161. &R0( 0,$C,$D,$A,$B,$X,14,17,0xa679438e);
  162. &R0( 1,$B,$C,$D,$A,$X,15,22,0x49b40821);
  163. &comment("");
  164. &comment("R1 section");
  165. &R1(-1,$A,$B,$C,$D,$X,16, 5,0xf61e2562);
  166. &R1( 0,$D,$A,$B,$C,$X,17, 9,0xc040b340);
  167. &R1( 0,$C,$D,$A,$B,$X,18,14,0x265e5a51);
  168. &R1( 0,$B,$C,$D,$A,$X,19,20,0xe9b6c7aa);
  169. &R1( 0,$A,$B,$C,$D,$X,20, 5,0xd62f105d);
  170. &R1( 0,$D,$A,$B,$C,$X,21, 9,0x02441453);
  171. &R1( 0,$C,$D,$A,$B,$X,22,14,0xd8a1e681);
  172. &R1( 0,$B,$C,$D,$A,$X,23,20,0xe7d3fbc8);
  173. &R1( 0,$A,$B,$C,$D,$X,24, 5,0x21e1cde6);
  174. &R1( 0,$D,$A,$B,$C,$X,25, 9,0xc33707d6);
  175. &R1( 0,$C,$D,$A,$B,$X,26,14,0xf4d50d87);
  176. &R1( 0,$B,$C,$D,$A,$X,27,20,0x455a14ed);
  177. &R1( 0,$A,$B,$C,$D,$X,28, 5,0xa9e3e905);
  178. &R1( 0,$D,$A,$B,$C,$X,29, 9,0xfcefa3f8);
  179. &R1( 0,$C,$D,$A,$B,$X,30,14,0x676f02d9);
  180. &R1( 1,$B,$C,$D,$A,$X,31,20,0x8d2a4c8a);
  181. &comment("");
  182. &comment("R2 section");
  183. &R2( 0,-1,$A,$B,$C,$D,$X,32, 4,0xfffa3942);
  184. &R2( 1, 0,$D,$A,$B,$C,$X,33,11,0x8771f681);
  185. &R2( 2, 0,$C,$D,$A,$B,$X,34,16,0x6d9d6122);
  186. &R2( 3, 0,$B,$C,$D,$A,$X,35,23,0xfde5380c);
  187. &R2( 4, 0,$A,$B,$C,$D,$X,36, 4,0xa4beea44);
  188. &R2( 5, 0,$D,$A,$B,$C,$X,37,11,0x4bdecfa9);
  189. &R2( 6, 0,$C,$D,$A,$B,$X,38,16,0xf6bb4b60);
  190. &R2( 7, 0,$B,$C,$D,$A,$X,39,23,0xbebfbc70);
  191. &R2( 8, 0,$A,$B,$C,$D,$X,40, 4,0x289b7ec6);
  192. &R2( 9, 0,$D,$A,$B,$C,$X,41,11,0xeaa127fa);
  193. &R2(10, 0,$C,$D,$A,$B,$X,42,16,0xd4ef3085);
  194. &R2(11, 0,$B,$C,$D,$A,$X,43,23,0x04881d05);
  195. &R2(12, 0,$A,$B,$C,$D,$X,44, 4,0xd9d4d039);
  196. &R2(13, 0,$D,$A,$B,$C,$X,45,11,0xe6db99e5);
  197. &R2(14, 0,$C,$D,$A,$B,$X,46,16,0x1fa27cf8);
  198. &R2(15, 1,$B,$C,$D,$A,$X,47,23,0xc4ac5665);
  199. &comment("");
  200. &comment("R3 section");
  201. &R3(-1,$A,$B,$C,$D,$X,48, 6,0xf4292244);
  202. &R3( 0,$D,$A,$B,$C,$X,49,10,0x432aff97);
  203. &R3( 0,$C,$D,$A,$B,$X,50,15,0xab9423a7);
  204. &R3( 0,$B,$C,$D,$A,$X,51,21,0xfc93a039);
  205. &R3( 0,$A,$B,$C,$D,$X,52, 6,0x655b59c3);
  206. &R3( 0,$D,$A,$B,$C,$X,53,10,0x8f0ccc92);
  207. &R3( 0,$C,$D,$A,$B,$X,54,15,0xffeff47d);
  208. &R3( 0,$B,$C,$D,$A,$X,55,21,0x85845dd1);
  209. &R3( 0,$A,$B,$C,$D,$X,56, 6,0x6fa87e4f);
  210. &R3( 0,$D,$A,$B,$C,$X,57,10,0xfe2ce6e0);
  211. &R3( 0,$C,$D,$A,$B,$X,58,15,0xa3014314);
  212. &R3( 0,$B,$C,$D,$A,$X,59,21,0x4e0811a1);
  213. &R3( 0,$A,$B,$C,$D,$X,60, 6,0xf7537e82);
  214. &R3( 0,$D,$A,$B,$C,$X,61,10,0xbd3af235);
  215. &R3( 0,$C,$D,$A,$B,$X,62,15,0x2ad7d2bb);
  216. &R3( 2,$B,$C,$D,$A,$X,63,21,0xeb86d391);
  217. # &mov($tmp2,&wparam(0)); # done in the last R3
  218. # &mov($tmp1, &DWP( 0,$tmp2,"",0)); # done is the last R3
  219. &add($A,$tmp1);
  220. &mov($tmp1, &DWP( 4,$tmp2,"",0));
  221. &add($B,$tmp1);
  222. &mov($tmp1, &DWP( 8,$tmp2,"",0));
  223. &add($C,$tmp1);
  224. &mov($tmp1, &DWP(12,$tmp2,"",0));
  225. &add($D,$tmp1);
  226. &mov(&DWP( 0,$tmp2,"",0),$A);
  227. &mov(&DWP( 4,$tmp2,"",0),$B);
  228. &mov($tmp1,&swtmp(0)) unless $normal;
  229. &mov(&DWP( 8,$tmp2,"",0),$C);
  230. &mov(&DWP(12,$tmp2,"",0),$D);
  231. &cmp($tmp1,$X) unless $normal; # check count
  232. &jae(&label("start")) unless $normal;
  233. &pop("eax"); # pop the temp variable off the stack
  234. &pop("ebx");
  235. &pop("ebp");
  236. &pop("edi");
  237. &pop("esi");
  238. &ret();
  239. &function_end_B($name);
  240. }