crypt586.pl 4.5 KB

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