2
0

crypt586.pl 4.2 KB

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