bf-586.pl 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  9. push(@INC,"${dir}","${dir}../../perlasm");
  10. require "x86asm.pl";
  11. require "cbc.pl";
  12. $output = pop;
  13. open STDOUT,">$output";
  14. &asm_init($ARGV[0],$ARGV[$#ARGV] eq "386");
  15. $BF_ROUNDS=16;
  16. $BF_OFF=($BF_ROUNDS+2)*4;
  17. $L="edi";
  18. $R="esi";
  19. $P="ebp";
  20. $tmp1="eax";
  21. $tmp2="ebx";
  22. $tmp3="ecx";
  23. $tmp4="edx";
  24. &BF_encrypt("BF_encrypt",1);
  25. &BF_encrypt("BF_decrypt",0);
  26. &cbc("BF_cbc_encrypt","BF_encrypt","BF_decrypt",1,4,5,3,-1,-1);
  27. &asm_finish();
  28. close STDOUT or die "error closing STDOUT: $!";
  29. sub BF_encrypt
  30. {
  31. local($name,$enc)=@_;
  32. &function_begin_B($name,"");
  33. &comment("");
  34. &push("ebp");
  35. &push("ebx");
  36. &mov($tmp2,&wparam(0));
  37. &mov($P,&wparam(1));
  38. &push("esi");
  39. &push("edi");
  40. &comment("Load the 2 words");
  41. &mov($L,&DWP(0,$tmp2,"",0));
  42. &mov($R,&DWP(4,$tmp2,"",0));
  43. &xor( $tmp1, $tmp1);
  44. # encrypting part
  45. if ($enc)
  46. {
  47. &mov($tmp2,&DWP(0,$P,"",0));
  48. &xor( $tmp3, $tmp3);
  49. &xor($L,$tmp2);
  50. for ($i=0; $i<$BF_ROUNDS; $i+=2)
  51. {
  52. &comment("");
  53. &comment("Round $i");
  54. &BF_ENCRYPT($i+1,$R,$L,$P,$tmp1,$tmp2,$tmp3,$tmp4,1);
  55. &comment("");
  56. &comment("Round ".sprintf("%d",$i+1));
  57. &BF_ENCRYPT($i+2,$L,$R,$P,$tmp1,$tmp2,$tmp3,$tmp4,1);
  58. }
  59. # &mov($tmp1,&wparam(0)); In last loop
  60. &mov($tmp4,&DWP(($BF_ROUNDS+1)*4,$P,"",0));
  61. }
  62. else
  63. {
  64. &mov($tmp2,&DWP(($BF_ROUNDS+1)*4,$P,"",0));
  65. &xor( $tmp3, $tmp3);
  66. &xor($L,$tmp2);
  67. for ($i=$BF_ROUNDS; $i>0; $i-=2)
  68. {
  69. &comment("");
  70. &comment("Round $i");
  71. &BF_ENCRYPT($i,$R,$L,$P,$tmp1,$tmp2,$tmp3,$tmp4,0);
  72. &comment("");
  73. &comment("Round ".sprintf("%d",$i-1));
  74. &BF_ENCRYPT($i-1,$L,$R,$P,$tmp1,$tmp2,$tmp3,$tmp4,0);
  75. }
  76. # &mov($tmp1,&wparam(0)); In last loop
  77. &mov($tmp4,&DWP(0,$P,"",0));
  78. }
  79. &xor($R,$tmp4);
  80. &mov(&DWP(4,$tmp1,"",0),$L);
  81. &mov(&DWP(0,$tmp1,"",0),$R);
  82. &function_end($name);
  83. }
  84. sub BF_ENCRYPT
  85. {
  86. local($i,$L,$R,$P,$tmp1,$tmp2,$tmp3,$tmp4,$enc)=@_;
  87. &mov( $tmp4, &DWP(&n2a($i*4),$P,"",0)); # for next round
  88. &mov( $tmp2, $R);
  89. &xor( $L, $tmp4);
  90. &shr( $tmp2, 16);
  91. &mov( $tmp4, $R);
  92. &movb( &LB($tmp1), &HB($tmp2)); # A
  93. &and( $tmp2, 0xff); # B
  94. &movb( &LB($tmp3), &HB($tmp4)); # C
  95. &and( $tmp4, 0xff); # D
  96. &mov( $tmp1, &DWP(&n2a($BF_OFF+0x0000),$P,$tmp1,4));
  97. &mov( $tmp2, &DWP(&n2a($BF_OFF+0x0400),$P,$tmp2,4));
  98. &add( $tmp2, $tmp1);
  99. &mov( $tmp1, &DWP(&n2a($BF_OFF+0x0800),$P,$tmp3,4));
  100. &xor( $tmp2, $tmp1);
  101. &mov( $tmp4, &DWP(&n2a($BF_OFF+0x0C00),$P,$tmp4,4));
  102. &add( $tmp2, $tmp4);
  103. if (($enc && ($i != 16)) || ((!$enc) && ($i != 1)))
  104. { &xor( $tmp1, $tmp1); }
  105. else
  106. {
  107. &comment("Load parameter 0 ($i) enc=$enc");
  108. &mov($tmp1,&wparam(0));
  109. } # In last loop
  110. &xor( $L, $tmp2);
  111. # delay
  112. }
  113. sub n2a
  114. {
  115. sprintf("%d",$_[0]);
  116. }