2
0

cast-586.pl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. # This flag makes the inner loop one cycle longer, but generates
  9. # code that runs %30 faster on the pentium pro/II, 44% faster
  10. # of PIII, while only %7 slower on the pentium.
  11. # By default, this flag is on.
  12. $ppro=1;
  13. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  14. push(@INC,"${dir}","${dir}../../perlasm");
  15. require "x86asm.pl";
  16. require "cbc.pl";
  17. $output=pop;
  18. open STDOUT,">$output";
  19. &asm_init($ARGV[0],$ARGV[$#ARGV] eq "386");
  20. $CAST_ROUNDS=16;
  21. $L="edi";
  22. $R="esi";
  23. $K="ebp";
  24. $tmp1="ecx";
  25. $tmp2="ebx";
  26. $tmp3="eax";
  27. $tmp4="edx";
  28. $S1="CAST_S_table0";
  29. $S2="CAST_S_table1";
  30. $S3="CAST_S_table2";
  31. $S4="CAST_S_table3";
  32. @F1=("add","xor","sub");
  33. @F2=("xor","sub","add");
  34. @F3=("sub","add","xor");
  35. &CAST_encrypt("CAST_encrypt",1);
  36. &CAST_encrypt("CAST_decrypt",0);
  37. &cbc("CAST_cbc_encrypt","CAST_encrypt","CAST_decrypt",1,4,5,3,-1,-1);
  38. &asm_finish();
  39. close STDOUT or die "error closing STDOUT: $!";
  40. sub CAST_encrypt {
  41. local($name,$enc)=@_;
  42. local($win_ex)=<<"EOF";
  43. EXTERN _CAST_S_table0:DWORD
  44. EXTERN _CAST_S_table1:DWORD
  45. EXTERN _CAST_S_table2:DWORD
  46. EXTERN _CAST_S_table3:DWORD
  47. EOF
  48. &main::external_label(
  49. "CAST_S_table0",
  50. "CAST_S_table1",
  51. "CAST_S_table2",
  52. "CAST_S_table3",
  53. );
  54. &function_begin_B($name,$win_ex);
  55. &comment("");
  56. &push("ebp");
  57. &push("ebx");
  58. &mov($tmp2,&wparam(0));
  59. &mov($K,&wparam(1));
  60. &push("esi");
  61. &push("edi");
  62. &comment("Load the 2 words");
  63. &mov($L,&DWP(0,$tmp2,"",0));
  64. &mov($R,&DWP(4,$tmp2,"",0));
  65. &comment('Get short key flag');
  66. &mov($tmp3,&DWP(128,$K,"",0));
  67. if($enc) {
  68. &push($tmp3);
  69. } else {
  70. &or($tmp3,$tmp3);
  71. &jnz(&label('cast_dec_skip'));
  72. }
  73. &xor($tmp3, $tmp3);
  74. # encrypting part
  75. if ($enc) {
  76. &E_CAST( 0,$S,$L,$R,$K,@F1,$tmp1,$tmp2,$tmp3,$tmp4);
  77. &E_CAST( 1,$S,$R,$L,$K,@F2,$tmp1,$tmp2,$tmp3,$tmp4);
  78. &E_CAST( 2,$S,$L,$R,$K,@F3,$tmp1,$tmp2,$tmp3,$tmp4);
  79. &E_CAST( 3,$S,$R,$L,$K,@F1,$tmp1,$tmp2,$tmp3,$tmp4);
  80. &E_CAST( 4,$S,$L,$R,$K,@F2,$tmp1,$tmp2,$tmp3,$tmp4);
  81. &E_CAST( 5,$S,$R,$L,$K,@F3,$tmp1,$tmp2,$tmp3,$tmp4);
  82. &E_CAST( 6,$S,$L,$R,$K,@F1,$tmp1,$tmp2,$tmp3,$tmp4);
  83. &E_CAST( 7,$S,$R,$L,$K,@F2,$tmp1,$tmp2,$tmp3,$tmp4);
  84. &E_CAST( 8,$S,$L,$R,$K,@F3,$tmp1,$tmp2,$tmp3,$tmp4);
  85. &E_CAST( 9,$S,$R,$L,$K,@F1,$tmp1,$tmp2,$tmp3,$tmp4);
  86. &E_CAST(10,$S,$L,$R,$K,@F2,$tmp1,$tmp2,$tmp3,$tmp4);
  87. &E_CAST(11,$S,$R,$L,$K,@F3,$tmp1,$tmp2,$tmp3,$tmp4);
  88. &comment('test short key flag');
  89. &pop($tmp4);
  90. &or($tmp4,$tmp4);
  91. &jnz(&label('cast_enc_done'));
  92. &E_CAST(12,$S,$L,$R,$K,@F1,$tmp1,$tmp2,$tmp3,$tmp4);
  93. &E_CAST(13,$S,$R,$L,$K,@F2,$tmp1,$tmp2,$tmp3,$tmp4);
  94. &E_CAST(14,$S,$L,$R,$K,@F3,$tmp1,$tmp2,$tmp3,$tmp4);
  95. &E_CAST(15,$S,$R,$L,$K,@F1,$tmp1,$tmp2,$tmp3,$tmp4);
  96. } else {
  97. &E_CAST(15,$S,$L,$R,$K,@F1,$tmp1,$tmp2,$tmp3,$tmp4);
  98. &E_CAST(14,$S,$R,$L,$K,@F3,$tmp1,$tmp2,$tmp3,$tmp4);
  99. &E_CAST(13,$S,$L,$R,$K,@F2,$tmp1,$tmp2,$tmp3,$tmp4);
  100. &E_CAST(12,$S,$R,$L,$K,@F1,$tmp1,$tmp2,$tmp3,$tmp4);
  101. &set_label('cast_dec_skip');
  102. &E_CAST(11,$S,$L,$R,$K,@F3,$tmp1,$tmp2,$tmp3,$tmp4);
  103. &E_CAST(10,$S,$R,$L,$K,@F2,$tmp1,$tmp2,$tmp3,$tmp4);
  104. &E_CAST( 9,$S,$L,$R,$K,@F1,$tmp1,$tmp2,$tmp3,$tmp4);
  105. &E_CAST( 8,$S,$R,$L,$K,@F3,$tmp1,$tmp2,$tmp3,$tmp4);
  106. &E_CAST( 7,$S,$L,$R,$K,@F2,$tmp1,$tmp2,$tmp3,$tmp4);
  107. &E_CAST( 6,$S,$R,$L,$K,@F1,$tmp1,$tmp2,$tmp3,$tmp4);
  108. &E_CAST( 5,$S,$L,$R,$K,@F3,$tmp1,$tmp2,$tmp3,$tmp4);
  109. &E_CAST( 4,$S,$R,$L,$K,@F2,$tmp1,$tmp2,$tmp3,$tmp4);
  110. &E_CAST( 3,$S,$L,$R,$K,@F1,$tmp1,$tmp2,$tmp3,$tmp4);
  111. &E_CAST( 2,$S,$R,$L,$K,@F3,$tmp1,$tmp2,$tmp3,$tmp4);
  112. &E_CAST( 1,$S,$L,$R,$K,@F2,$tmp1,$tmp2,$tmp3,$tmp4);
  113. &E_CAST( 0,$S,$R,$L,$K,@F1,$tmp1,$tmp2,$tmp3,$tmp4);
  114. }
  115. &set_label('cast_enc_done') if $enc;
  116. # Why the nop? - Ben 17/1/99
  117. &nop();
  118. &mov($tmp3,&wparam(0));
  119. &mov(&DWP(4,$tmp3,"",0),$L);
  120. &mov(&DWP(0,$tmp3,"",0),$R);
  121. &function_end($name);
  122. }
  123. sub E_CAST {
  124. local($i,$S,$L,$R,$K,$OP1,$OP2,$OP3,$tmp1,$tmp2,$tmp3,$tmp4)=@_;
  125. # Ri needs to have 16 pre added.
  126. &comment("round $i");
  127. &mov( $tmp4, &DWP($i*8,$K,"",1));
  128. &mov( $tmp1, &DWP($i*8+4,$K,"",1));
  129. &$OP1( $tmp4, $R);
  130. &rotl( $tmp4, &LB($tmp1));
  131. if ($ppro) {
  132. &xor( $tmp1, $tmp1);
  133. &mov( $tmp2, 0xff);
  134. &movb( &LB($tmp1), &HB($tmp4)); # A
  135. &and( $tmp2, $tmp4);
  136. &shr( $tmp4, 16); #
  137. &xor( $tmp3, $tmp3);
  138. } else {
  139. &mov( $tmp2, $tmp4); # B
  140. &movb( &LB($tmp1), &HB($tmp4)); # A # BAD BAD BAD
  141. &shr( $tmp4, 16); #
  142. &and( $tmp2, 0xff);
  143. }
  144. &movb( &LB($tmp3), &HB($tmp4)); # C # BAD BAD BAD
  145. &and( $tmp4, 0xff); # D
  146. &mov( $tmp1, &DWP($S1,"",$tmp1,4));
  147. &mov( $tmp2, &DWP($S2,"",$tmp2,4));
  148. &$OP2( $tmp1, $tmp2);
  149. &mov( $tmp2, &DWP($S3,"",$tmp3,4));
  150. &$OP3( $tmp1, $tmp2);
  151. &mov( $tmp2, &DWP($S4,"",$tmp4,4));
  152. &$OP1( $tmp1, $tmp2);
  153. # XXX
  154. &xor( $L, $tmp1);
  155. # XXX
  156. }