crypt586.pl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #!/usr/local/bin/perl
  2. #
  3. # The inner loop instruction sequence and the IP/FP modifications are from
  4. # Svend Olaf Mikkelsen <svolaf@inet.uni-c.dk>
  5. # I've added the stuff needed for crypt() but I've not worried about making
  6. # things perfect.
  7. #
  8. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  9. push(@INC,"${dir}","${dir}../../perlasm");
  10. require "x86asm.pl";
  11. &asm_init($ARGV[0],"crypt586.pl");
  12. $L="edi";
  13. $R="esi";
  14. &external_label("DES_SPtrans");
  15. &fcrypt_body("fcrypt_body");
  16. &asm_finish();
  17. sub fcrypt_body
  18. {
  19. local($name,$do_ip)=@_;
  20. &function_begin($name);
  21. &comment("");
  22. &comment("Load the 2 words");
  23. $trans="ebp";
  24. &xor( $L, $L);
  25. &xor( $R, $R);
  26. # PIC-ification:-)
  27. &picmeup("edx","DES_SPtrans");
  28. #if ($cpp) { &picmeup("edx","DES_SPtrans"); }
  29. #else { &lea("edx",&DWP("DES_SPtrans")); }
  30. &push("edx"); # becomes &swtmp(1)
  31. #
  32. &mov($trans,&wparam(1)); # reloaded with DES_SPtrans in D_ENCRYPT
  33. &push(&DWC(25)); # add a variable
  34. &set_label("start");
  35. for ($i=0; $i<16; $i+=2)
  36. {
  37. &comment("");
  38. &comment("Round $i");
  39. &D_ENCRYPT($i,$L,$R,$i*2,$trans,"eax","ebx","ecx","edx");
  40. &comment("");
  41. &comment("Round ".sprintf("%d",$i+1));
  42. &D_ENCRYPT($i+1,$R,$L,($i+1)*2,$trans,"eax","ebx","ecx","edx");
  43. }
  44. &mov("ebx", &swtmp(0));
  45. &mov("eax", $L);
  46. &dec("ebx");
  47. &mov($L, $R);
  48. &mov($R, "eax");
  49. &mov(&swtmp(0), "ebx");
  50. &jnz(&label("start"));
  51. &comment("");
  52. &comment("FP");
  53. &mov("edx",&wparam(0));
  54. &FP_new($R,$L,"eax",3);
  55. &mov(&DWP(0,"edx","",0),"eax");
  56. &mov(&DWP(4,"edx","",0),$L);
  57. &add("esp",8); # remove variables
  58. &function_end($name);
  59. }
  60. sub D_ENCRYPT
  61. {
  62. local($r,$L,$R,$S,$trans,$u,$tmp1,$tmp2,$t)=@_;
  63. &mov( $u, &wparam(2)); # 2
  64. &mov( $t, $R);
  65. &shr( $t, 16); # 1
  66. &mov( $tmp2, &wparam(3)); # 2
  67. &xor( $t, $R); # 1
  68. &and( $u, $t); # 2
  69. &and( $t, $tmp2); # 2
  70. &mov( $tmp1, $u);
  71. &shl( $tmp1, 16); # 1
  72. &mov( $tmp2, $t);
  73. &shl( $tmp2, 16); # 1
  74. &xor( $u, $tmp1); # 2
  75. &xor( $t, $tmp2); # 2
  76. &mov( $tmp1, &DWP(&n2a($S*4),$trans,"",0)); # 2
  77. &xor( $u, $tmp1);
  78. &mov( $tmp2, &DWP(&n2a(($S+1)*4),$trans,"",0)); # 2
  79. &xor( $u, $R);
  80. &xor( $t, $R);
  81. &xor( $t, $tmp2);
  82. &and( $u, "0xfcfcfcfc" ); # 2
  83. &xor( $tmp1, $tmp1); # 1
  84. &and( $t, "0xcfcfcfcf" ); # 2
  85. &xor( $tmp2, $tmp2);
  86. &movb( &LB($tmp1), &LB($u) );
  87. &movb( &LB($tmp2), &HB($u) );
  88. &rotr( $t, 4 );
  89. &mov( $trans, &swtmp(1));
  90. &xor( $L, &DWP(" ",$trans,$tmp1,0));
  91. &movb( &LB($tmp1), &LB($t) );
  92. &xor( $L, &DWP("0x200",$trans,$tmp2,0));
  93. &movb( &LB($tmp2), &HB($t) );
  94. &shr( $u, 16);
  95. &xor( $L, &DWP("0x100",$trans,$tmp1,0));
  96. &movb( &LB($tmp1), &HB($u) );
  97. &shr( $t, 16);
  98. &xor( $L, &DWP("0x300",$trans,$tmp2,0));
  99. &movb( &LB($tmp2), &HB($t) );
  100. &and( $u, "0xff" );
  101. &and( $t, "0xff" );
  102. &mov( $tmp1, &DWP("0x600",$trans,$tmp1,0));
  103. &xor( $L, $tmp1);
  104. &mov( $tmp1, &DWP("0x700",$trans,$tmp2,0));
  105. &xor( $L, $tmp1);
  106. &mov( $tmp1, &DWP("0x400",$trans,$u,0));
  107. &xor( $L, $tmp1);
  108. &mov( $tmp1, &DWP("0x500",$trans,$t,0));
  109. &xor( $L, $tmp1);
  110. &mov( $trans, &wparam(1));
  111. }
  112. sub n2a
  113. {
  114. sprintf("%d",$_[0]);
  115. }
  116. # now has a side affect of rotating $a by $shift
  117. sub R_PERM_OP
  118. {
  119. local($a,$b,$tt,$shift,$mask,$last)=@_;
  120. &rotl( $a, $shift ) if ($shift != 0);
  121. &mov( $tt, $a );
  122. &xor( $a, $b );
  123. &and( $a, $mask );
  124. if ($notlast eq $b)
  125. {
  126. &xor( $b, $a );
  127. &xor( $tt, $a );
  128. }
  129. else
  130. {
  131. &xor( $tt, $a );
  132. &xor( $b, $a );
  133. }
  134. &comment("");
  135. }
  136. sub IP_new
  137. {
  138. local($l,$r,$tt,$lr)=@_;
  139. &R_PERM_OP($l,$r,$tt, 4,"0xf0f0f0f0",$l);
  140. &R_PERM_OP($r,$tt,$l,20,"0xfff0000f",$l);
  141. &R_PERM_OP($l,$tt,$r,14,"0x33333333",$r);
  142. &R_PERM_OP($tt,$r,$l,22,"0x03fc03fc",$r);
  143. &R_PERM_OP($l,$r,$tt, 9,"0xaaaaaaaa",$r);
  144. if ($lr != 3)
  145. {
  146. if (($lr-3) < 0)
  147. { &rotr($tt, 3-$lr); }
  148. else { &rotl($tt, $lr-3); }
  149. }
  150. if ($lr != 2)
  151. {
  152. if (($lr-2) < 0)
  153. { &rotr($r, 2-$lr); }
  154. else { &rotl($r, $lr-2); }
  155. }
  156. }
  157. sub FP_new
  158. {
  159. local($l,$r,$tt,$lr)=@_;
  160. if ($lr != 2)
  161. {
  162. if (($lr-2) < 0)
  163. { &rotl($r, 2-$lr); }
  164. else { &rotr($r, $lr-2); }
  165. }
  166. if ($lr != 3)
  167. {
  168. if (($lr-3) < 0)
  169. { &rotl($l, 3-$lr); }
  170. else { &rotr($l, $lr-3); }
  171. }
  172. &R_PERM_OP($l,$r,$tt, 0,"0xaaaaaaaa",$r);
  173. &R_PERM_OP($tt,$r,$l,23,"0x03fc03fc",$r);
  174. &R_PERM_OP($l,$r,$tt,10,"0x33333333",$l);
  175. &R_PERM_OP($r,$tt,$l,18,"0xfff0000f",$l);
  176. &R_PERM_OP($l,$tt,$r,12,"0xf0f0f0f0",$r);
  177. &rotr($tt , 4);
  178. }