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