bf-686.pl 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/usr/local/bin/perl
  2. push(@INC,"perlasm","../../perlasm");
  3. require "x86asm.pl";
  4. require "cbc.pl";
  5. &asm_init($ARGV[0],"bf-686.pl");
  6. $BF_ROUNDS=16;
  7. $BF_OFF=($BF_ROUNDS+2)*4;
  8. $L="ecx";
  9. $R="edx";
  10. $P="edi";
  11. $tot="esi";
  12. $tmp1="eax";
  13. $tmp2="ebx";
  14. $tmp3="ebp";
  15. &des_encrypt("BF_encrypt",1);
  16. &des_encrypt("BF_decrypt",0);
  17. &cbc("BF_cbc_encrypt","BF_encrypt","BF_decrypt",1,4,5,3,-1,-1);
  18. &asm_finish();
  19. &file_end();
  20. sub des_encrypt
  21. {
  22. local($name,$enc)=@_;
  23. &function_begin($name,"");
  24. &comment("");
  25. &comment("Load the 2 words");
  26. &mov("eax",&wparam(0));
  27. &mov($L,&DWP(0,"eax","",0));
  28. &mov($R,&DWP(4,"eax","",0));
  29. &comment("");
  30. &comment("P pointer, s and enc flag");
  31. &mov($P,&wparam(1));
  32. &xor( $tmp1, $tmp1);
  33. &xor( $tmp2, $tmp2);
  34. # encrypting part
  35. if ($enc)
  36. {
  37. &xor($L,&DWP(0,$P,"",0));
  38. for ($i=0; $i<$BF_ROUNDS; $i+=2)
  39. {
  40. &comment("");
  41. &comment("Round $i");
  42. &BF_ENCRYPT($i+1,$R,$L,$P,$tot,$tmp1,$tmp2,$tmp3);
  43. &comment("");
  44. &comment("Round ".sprintf("%d",$i+1));
  45. &BF_ENCRYPT($i+2,$L,$R,$P,$tot,$tmp1,$tmp2,$tmp3);
  46. }
  47. &xor($R,&DWP(($BF_ROUNDS+1)*4,$P,"",0));
  48. &mov("eax",&wparam(0));
  49. &mov(&DWP(0,"eax","",0),$R);
  50. &mov(&DWP(4,"eax","",0),$L);
  51. &function_end_A($name);
  52. }
  53. else
  54. {
  55. &xor($L,&DWP(($BF_ROUNDS+1)*4,$P,"",0));
  56. for ($i=$BF_ROUNDS; $i>0; $i-=2)
  57. {
  58. &comment("");
  59. &comment("Round $i");
  60. &BF_ENCRYPT($i,$R,$L,$P,$tot,$tmp1,$tmp2,$tmp3);
  61. &comment("");
  62. &comment("Round ".sprintf("%d",$i-1));
  63. &BF_ENCRYPT($i-1,$L,$R,$P,$tot,$tmp1,$tmp2,$tmp3);
  64. }
  65. &xor($R,&DWP(0,$P,"",0));
  66. &mov("eax",&wparam(0));
  67. &mov(&DWP(0,"eax","",0),$R);
  68. &mov(&DWP(4,"eax","",0),$L);
  69. &function_end_A($name);
  70. }
  71. &function_end_B($name);
  72. }
  73. sub BF_ENCRYPT
  74. {
  75. local($i,$L,$R,$P,$tot,$tmp1,$tmp2,$tmp3)=@_;
  76. &rotr( $R, 16);
  77. &mov( $tot, &DWP(&n2a($i*4),$P,"",0));
  78. &movb( &LB($tmp1), &HB($R));
  79. &movb( &LB($tmp2), &LB($R));
  80. &rotr( $R, 16);
  81. &xor( $L, $tot);
  82. &mov( $tot, &DWP(&n2a($BF_OFF+0x0000),$P,$tmp1,4));
  83. &mov( $tmp3, &DWP(&n2a($BF_OFF+0x0400),$P,$tmp2,4));
  84. &movb( &LB($tmp1), &HB($R));
  85. &movb( &LB($tmp2), &LB($R));
  86. &add( $tot, $tmp3);
  87. &mov( $tmp1, &DWP(&n2a($BF_OFF+0x0800),$P,$tmp1,4)); # delay
  88. &xor( $tot, $tmp1);
  89. &mov( $tmp3, &DWP(&n2a($BF_OFF+0x0C00),$P,$tmp2,4));
  90. &add( $tot, $tmp3);
  91. &xor( $tmp1, $tmp1);
  92. &xor( $L, $tot);
  93. # delay
  94. }
  95. sub n2a
  96. {
  97. sprintf("%d",$_[0]);
  98. }