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.

141 lines
2.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
4 years ago
4 years ago
4 years ago
  1. #pragma once
  2. #include <stdint.h>
  3. #include <stddef.h>
  4. #include <gp/integer_math.hpp>
  5. #include <algorithm>
  6. #include <array>
  7. template<typename word_t = uint32_t, size_t r = 20, size_t b = 128, word_t P = 0xb7e15163L, word_t Q = 0x9e3779b9L>
  8. class RC6 {
  9. static constexpr size_t word_size = 8*sizeof(word_t);
  10. constexpr static word_t r_l(const word_t& w, size_t v) {
  11. return (w << v) | ( w >> (word_size-v));
  12. }
  13. constexpr static word_t r_r(const word_t& w, size_t v) {
  14. return (w >> v) | ( w << (word_size-v));
  15. }
  16. class RC6_KeySched {
  17. using sched_t = std::array<word_t, 2*r+4>;
  18. public:
  19. static constexpr size_t c = (b+word_size-1)/word_size;
  20. static constexpr size_t v_3 = std::max(c, 2*r+4);
  21. static constexpr size_t v = v_3*3;
  22. private:
  23. sched_t S;
  24. public:
  25. constexpr RC6_KeySched(std::array<word_t, c> L)
  26. {
  27. assert(r_l(r_r(13,13),13) == 13);
  28. auto it = S.begin();
  29. *(it++) = P;
  30. for(; it != S.end(); ++it)
  31. {
  32. *it = *(it-1) + Q;
  33. }
  34. word_t A = 0;
  35. word_t B = 0;
  36. word_t i = 0;
  37. word_t j = 0;
  38. for(size_t s = 0; s < v; ++s)
  39. {
  40. A = S[i] = r_l( S[i] + A + B, 3 );
  41. B = L[j] = r_l( L[j] + A + B, (A + B)%(word_size));
  42. i = s % S.size();
  43. j = s % L.size();
  44. }
  45. }
  46. const auto cbegin()
  47. {
  48. return S.cbegin();
  49. }
  50. const auto cend()
  51. {
  52. return S.cend();
  53. }
  54. const auto crbegin()
  55. {
  56. return S.crbegin();
  57. }
  58. const auto crend()
  59. {
  60. return S.crend();
  61. }
  62. };
  63. RC6_KeySched S;
  64. public:
  65. typedef std::array<word_t, RC6_KeySched::c> key_type;
  66. typedef std::array<word_t, 4> block_type;
  67. constexpr RC6(const key_type& key)
  68. : S(key)
  69. {}
  70. constexpr block_type encrypt(block_type plaintext) {
  71. using namespace gp::math;
  72. auto& A = plaintext[0];
  73. auto& B = plaintext[1];
  74. auto& C = plaintext[2];
  75. auto& D = plaintext[3];
  76. auto it = S.cbegin();
  77. B += *(it++);
  78. D += *(it++);
  79. for(size_t i = 0; i < r; ++i)
  80. {
  81. auto u = r_l( D * ( 2 * D + 1 ), msb(word_size));
  82. auto t = r_l( B * ( 2 * B + 1 ), msb(word_size));
  83. A = r_l((A ^ t), u % word_size) + *(it++);
  84. C = r_l((C ^ u), t % word_size) + *(it++);
  85. std::rotate(plaintext.begin(), plaintext.begin()+1, plaintext.end());
  86. }
  87. A += *(it++);
  88. C += *(it++);
  89. assert(it == S.cend());
  90. return plaintext;
  91. }
  92. constexpr block_type decrypt(block_type plaintext) {
  93. using namespace gp::math;
  94. auto& A = plaintext[0];
  95. auto& B = plaintext[1];
  96. auto& C = plaintext[2];
  97. auto& D = plaintext[3];
  98. auto it = S.crbegin();
  99. C -= *(it++);
  100. A -= *(it++);
  101. for(size_t i = 0; i < r; ++i)
  102. {
  103. std::rotate(plaintext.begin(), plaintext.end()-1, plaintext.end());
  104. auto u = r_l( D * ( 2 * D + 1 ), msb(word_size));
  105. auto t = r_l( B * ( 2 * B + 1 ), msb(word_size));
  106. C = r_r( (C - *(it++)) , t % word_size) ^ u ;
  107. A = r_r( (A - *(it++)) , u % word_size) ^ t ;
  108. }
  109. D -= *(it++);
  110. B -= *(it++);
  111. assert(it == S.crend());
  112. return plaintext;
  113. }
  114. };