rc5-586.pl 2.3 KB

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