rc5-586.pl 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/local/bin/perl
  2. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  3. push(@INC,"${dir}","${dir}../../perlasm");
  4. require "x86asm.pl";
  5. require "cbc.pl";
  6. &asm_init($ARGV[0],"rc5-586.pl");
  7. $RC5_MAX_ROUNDS=16;
  8. $RC5_32_OFF=($RC5_MAX_ROUNDS+2)*4;
  9. $A="edi";
  10. $B="esi";
  11. $S="ebp";
  12. $tmp1="eax";
  13. $r="ebx";
  14. $tmpc="ecx";
  15. $tmp4="edx";
  16. &RC5_32_encrypt("RC5_32_encrypt",1);
  17. &RC5_32_encrypt("RC5_32_decrypt",0);
  18. &cbc("RC5_32_cbc_encrypt","RC5_32_encrypt","RC5_32_decrypt",0,4,5,3,-1,-1);
  19. &asm_finish();
  20. sub RC5_32_encrypt
  21. {
  22. local($name,$enc)=@_;
  23. &function_begin_B($name,"");
  24. &comment("");
  25. &push("ebp");
  26. &push("esi");
  27. &push("edi");
  28. &mov($tmp4,&wparam(0));
  29. &mov($S,&wparam(1));
  30. &comment("Load the 2 words");
  31. &mov($A,&DWP(0,$tmp4,"",0));
  32. &mov($B,&DWP(4,$tmp4,"",0));
  33. &push($r);
  34. &mov($r, &DWP(0,$S,"",0));
  35. # encrypting part
  36. if ($enc)
  37. {
  38. &add($A, &DWP(4+0,$S,"",0));
  39. &add($B, &DWP(4+4,$S,"",0));
  40. for ($i=0; $i<$RC5_MAX_ROUNDS; $i++)
  41. {
  42. &xor($A, $B);
  43. &mov($tmp1, &DWP(12+$i*8,$S,"",0));
  44. &mov($tmpc, $B);
  45. &rotl($A, &LB("ecx"));
  46. &add($A, $tmp1);
  47. &xor($B, $A);
  48. &mov($tmp1, &DWP(16+$i*8,$S,"",0));
  49. &mov($tmpc, $A);
  50. &rotl($B, &LB("ecx"));
  51. &add($B, $tmp1);
  52. if (($i == 7) || ($i == 11))
  53. {
  54. &cmp($r, $i+1);
  55. &je(&label("rc5_exit"));
  56. }
  57. }
  58. }
  59. else
  60. {
  61. &cmp($r, 12);
  62. &je(&label("rc5_dec_12"));
  63. &cmp($r, 8);
  64. &je(&label("rc5_dec_8"));
  65. for ($i=$RC5_MAX_ROUNDS; $i > 0; $i--)
  66. {
  67. &set_label("rc5_dec_$i") if ($i == 12) || ($i == 8);
  68. &mov($tmp1, &DWP($i*8+8,$S,"",0));
  69. &sub($B, $tmp1);
  70. &mov($tmpc, $A);
  71. &rotr($B, &LB("ecx"));
  72. &xor($B, $A);
  73. &mov($tmp1, &DWP($i*8+4,$S,"",0));
  74. &sub($A, $tmp1);
  75. &mov($tmpc, $B);
  76. &rotr($A, &LB("ecx"));
  77. &xor($A, $B);
  78. }
  79. &sub($B, &DWP(4+4,$S,"",0));
  80. &sub($A, &DWP(4+0,$S,"",0));
  81. }
  82. &set_label("rc5_exit");
  83. &mov(&DWP(0,$tmp4,"",0),$A);
  84. &mov(&DWP(4,$tmp4,"",0),$B);
  85. &pop("ebx");
  86. &pop("edi");
  87. &pop("esi");
  88. &pop("ebp");
  89. &ret();
  90. &function_end_B($name);
  91. }