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.

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