General Purpose library for Freestanding C++ and POSIX systems
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

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