rc5-586.pl 2.3 KB

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