crypt586.pl 4.5 KB

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