bf-586.pl 3.0 KB

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