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.

188 lines
3.8 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  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 = 0xb7e15163L, word_t Q = 0x9e3779b9L>
  46. class RC6 {
  47. static constexpr size_t word_size = 8*sizeof(word_t);
  48. constexpr static word_t r_l(const word_t& w, size_t v) {
  49. return (w << v) | ( w >> (word_size-v));
  50. }
  51. constexpr static word_t r_r(const word_t& w, size_t v) {
  52. return (w >> v) | ( w << (word_size-v));
  53. }
  54. class RC6_KeySched {
  55. using sched_t = std::array<word_t, 2*r+4>;
  56. public:
  57. static constexpr size_t c = (b+word_size-1)/word_size;
  58. static constexpr size_t v_3 = std::max(c, 2*r+4);
  59. static constexpr size_t v = v_3*3;
  60. private:
  61. sched_t S;
  62. public:
  63. constexpr RC6_KeySched(std::array<word_t, c> L)
  64. {
  65. assert(r_l(r_r(13,13),13) == 13);
  66. auto it = S.begin();
  67. *(it++) = P;
  68. for(; it != S.end(); ++it)
  69. {
  70. *it = *(it-1) + Q;
  71. }
  72. word_t A = 0;
  73. word_t B = 0;
  74. word_t i = 0;
  75. word_t j = 0;
  76. for(size_t s = 0; s < v; ++s)
  77. {
  78. A = S[i] = r_l( S[i] + A + B, 3 );
  79. B = L[j] = r_l( L[j] + A + B, (A + B)%(word_size));
  80. i = s % S.size();
  81. j = s % L.size();
  82. }
  83. }
  84. const word_t& operator[](const size_t pos) {
  85. return S[pos];
  86. }
  87. const auto cbegin()
  88. {
  89. return S.cbegin();
  90. }
  91. const auto cend()
  92. {
  93. return S.cend();
  94. }
  95. const auto crbegin()
  96. {
  97. return S.crbegin();
  98. }
  99. const auto crend()
  100. {
  101. return S.crend();
  102. }
  103. };
  104. RC6_KeySched S;
  105. public:
  106. typedef std::array<word_t, RC6_KeySched::c> key_type;
  107. typedef std::array<word_t, 4> block_type;
  108. constexpr RC6(const key_type& key)
  109. : S(key)
  110. {}
  111. constexpr const block_type encrypt(block_type plaintext) {
  112. auto& A = plaintext[0];
  113. auto& B = plaintext[1];
  114. auto& C = plaintext[2];
  115. auto& D = plaintext[3];
  116. auto it = S.cbegin();
  117. B += *(it++);
  118. D += *(it++);
  119. for(size_t i = 0; i < r; ++i)
  120. {
  121. auto u = r_l( D * ( 2 * D + 1 ), 5);
  122. auto t = r_l( B * ( 2 * B + 1 ), 5);
  123. A = r_l((A ^ t), u % word_size) + *(it++);
  124. C = r_l((C ^ u), t % word_size) + *(it++);
  125. std::rotate(plaintext.begin(), plaintext.begin()+1, plaintext.end());
  126. }
  127. A += *(it++);
  128. C += *(it++);
  129. assert(it == S.cend());
  130. return plaintext;
  131. }
  132. constexpr const block_type decrypt(block_type plaintext) {
  133. auto& A = plaintext[0];
  134. auto& B = plaintext[1];
  135. auto& C = plaintext[2];
  136. auto& D = plaintext[3];
  137. auto it = S.crbegin();
  138. C -= *(it++);
  139. A -= *(it++);
  140. for(size_t i = 0; i < r; ++i)
  141. {
  142. std::rotate(plaintext.begin(), plaintext.end()-1, plaintext.end());
  143. auto u = r_l( D * ( 2 * D + 1 ), 5);
  144. auto t = r_l( B * ( 2 * B + 1 ), 5);
  145. C = r_r( (C - *(it++)) , t % word_size) ^ u ;
  146. A = r_r( (A - *(it++)) , u % word_size) ^ t ;
  147. }
  148. D -= *(it++);
  149. B -= *(it++);
  150. assert(it == S.crend());
  151. return plaintext;
  152. }
  153. };