General Purpose library for Freestanding C++ and POSIX systems
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 line
2.7 KiB

  1. #pragma once
  2. #include <stdint.h>
  3. #include <stddef.h>
  4. #include <algorithm>
  5. #include <array>
  6. template<typename word_t>
  7. size_t lg(word_t v);
  8. /**
  9. Sean Eron Anderson
  10. seander@cs.stanford.edu
  11. **/
  12. template<>
  13. size_t lg<uint32_t>(uint32_t v)
  14. {
  15. static const int MultiplyDeBruijnBitPosition[32] =
  16. {
  17. 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
  18. 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
  19. };
  20. v |= v >> 1;
  21. v |= v >> 2;
  22. v |= v >> 4;
  23. v |= v >> 8;
  24. v |= v >> 16;
  25. return MultiplyDeBruijnBitPosition[(uint32_t)(v * 0x07C4ACDDU) >> 27];
  26. }
  27. template<>
  28. size_t lg<uint64_t>(uint64_t v)
  29. {
  30. static const int MultiplyDeBruijnBitPosition[64] =
  31. {
  32. 0, 58, 1, 59, 47, 53, 2, 60, 39, 48, 27, 54, 33, 42, 3, 61,
  33. 51, 37, 40, 49, 18, 28, 20, 55, 30, 34, 11, 43, 14, 22, 4, 62,
  34. 57, 46, 52, 38, 26, 32, 41, 50, 36, 17, 19, 29, 10, 13, 21, 56,
  35. 45, 25, 31, 35, 16, 9, 12, 44, 24, 15, 8, 23, 7, 6, 5, 63
  36. };
  37. v |= v >> 1;
  38. v |= v >> 2;
  39. v |= v >> 4;
  40. v |= v >> 8;
  41. v |= v >> 16;
  42. v |= v >> 32;
  43. return MultiplyDeBruijnBitPosition[(uint64_t)(v * 0x03f6eaf2cd271461) >> 58];
  44. }
  45. template<typename word_t = uint32_t, size_t r = 20, size_t b = 128, word_t P = 0xB7E15163, word_t Q = 0x9E3779B9>
  46. class RC6 {
  47. static word_t r_l(const word_t& w, size_t v) {
  48. return (w << v) | ( w >> (sizeof(w)-v));
  49. }
  50. static word_t r_r(const word_t& w, size_t v) {
  51. return (w >> v) | ( w << (sizeof(w)-v));
  52. }
  53. class RC6_KeySched {
  54. public:
  55. static constexpr size_t c = b/sizeof(word_t)/8;
  56. static constexpr size_t v_3 = std::max(c, 2*r+4);
  57. static constexpr size_t v = v_3*3;
  58. private:
  59. std::array<word_t, v_3> S;
  60. public:
  61. RC6_KeySched(std::array<word_t, c> L)
  62. {
  63. S[0] = P;
  64. for(size_t i = 1; i < 2*r+3; ++i)
  65. {
  66. S[i] = S[i - 1] + Q;
  67. }
  68. word_t A = 0;
  69. word_t B = 0;
  70. word_t i = 0;
  71. word_t j = 0;
  72. for(size_t s = 1; s < v; ++s)
  73. {
  74. A = S[i] = r_l( S[i] + A + B, 3 );
  75. B = L[j] = r_l( L[j] + A + B, (A + B)%(8*sizeof(word_t)));
  76. i = (i + 1) % (2*r+4);
  77. j = (j + 1) % c;
  78. }
  79. }
  80. const word_t& operator[](const size_t pos) {
  81. return S[pos];
  82. }
  83. };
  84. RC6_KeySched S;
  85. public:
  86. typedef std::array<word_t, RC6_KeySched::c> key_type;
  87. typedef std::array<word_t, 4> block_type;
  88. RC6(const key_type& key)
  89. : S(key)
  90. {}
  91. void encrypt(block_type& plaintext) {
  92. auto& A = plaintext[0];
  93. auto& B = plaintext[1];
  94. auto& C = plaintext[2];
  95. auto& D = plaintext[3];
  96. B += S[0];
  97. D += S[1];
  98. for(size_t i = 1; i < r; ++i)
  99. {
  100. auto t = r_l( B * ( 2 * B + 1 ), 5);
  101. auto u = r_l( D * ( 2 * D + 1 ), 5);
  102. A = ((A ^ t) << u%(8*sizeof(word_t))) + S[2*i];
  103. C = ((C ^ u) << t%(8*sizeof(word_t))) + S[2*i+1];
  104. std::rotate(plaintext.begin(), plaintext.begin()+1, plaintext.end());
  105. }
  106. A += S[2*r+3];
  107. C += S[2*r+2];
  108. }
  109. };