Browse Source

More RC6 fixes

devel
Ludovic 'Archivist' Lagouardette 4 years ago
parent
commit
0ef4c578d3
1 changed files with 25 additions and 21 deletions
  1. +25
    -21
      include/rc6_generic.hpp

+ 25
- 21
include/rc6_generic.hpp View File

@ -66,18 +66,21 @@ class RC6 {
class RC6_KeySched { class RC6_KeySched {
using sched_t = std::array<word_t, 2*r+4>;
public: public:
static constexpr size_t c = (b+word_size-1)/word_size; static constexpr size_t c = (b+word_size-1)/word_size;
static constexpr size_t v_3 = std::max(c, 2*r+4); static constexpr size_t v_3 = std::max(c, 2*r+4);
static constexpr size_t v = v_3*3; static constexpr size_t v = v_3*3;
private: private:
std::array<word_t, 2*r+4> S;
sched_t S;
public: public:
RC6_KeySched(std::array<word_t, c> L)
k">constexpr RC6_KeySched(std::array<word_t, c> L)
{ {
assert(r_l(r_r(13,13),13) == 13); assert(r_l(r_r(13,13),13) == 13);
S[0] = P;
for(auto it = S.begin()+1; it < S.end(); ++it)
auto it = S.begin();
*(it++) = P;
for(; it != S.end(); ++it)
{ {
*it = *(it-1) + Q; *it = *(it-1) + Q;
} }
@ -88,10 +91,11 @@ class RC6 {
for(size_t s = 0; s < v; ++s) for(size_t s = 0; s < v; ++s)
{ {
i = s % (2*r+4);
j = s % c;
A = S[i] = r_l( S[i] + A + B, 3 ); A = S[i] = r_l( S[i] + A + B, 3 );
B = L[j] = r_l( L[j] + A + B, (A + B)%(word_size)); B = L[j] = r_l( L[j] + A + B, (A + B)%(word_size));
i = s % S.size();
j = s % L.size();
} }
} }
@ -99,24 +103,24 @@ class RC6 {
return S[pos]; return S[pos];
} }
auto begin()
const auto cbegin()
{ {
return S.begin();
return S.cbegin();
} }
auto end()
const auto cend()
{ {
return S.end();
return S.cend();
} }
auto rbegin()
const auto crbegin()
{ {
return S.rbegin();
return S.crbegin();
} }
auto rend()
const auto crend()
{ {
return S.rend();
return S.crend();
} }
}; };
@ -127,17 +131,17 @@ public:
typedef std::array<word_t, RC6_KeySched::c> key_type; typedef std::array<word_t, RC6_KeySched::c> key_type;
typedef std::array<word_t, 4> block_type; typedef std::array<word_t, 4> block_type;
RC6(const key_type& key)
k">constexpr RC6(const key_type& key)
: S(key) : S(key)
{} {}
block_type encrypt(block_type plaintext) {
k">constexpr const block_type encrypt(block_type plaintext) {
auto& A = plaintext[0]; auto& A = plaintext[0];
auto& B = plaintext[1]; auto& B = plaintext[1];
auto& C = plaintext[2]; auto& C = plaintext[2];
auto& D = plaintext[3]; auto& D = plaintext[3];
auto it = S.begin();
auto it = S.cbegin();
B += *(it++); B += *(it++);
D += *(it++); D += *(it++);
@ -153,16 +157,16 @@ public:
A += *(it++); A += *(it++);
C += *(it++); C += *(it++);
assert(it == S.end());
assert(it == S.cend());
return plaintext; return plaintext;
} }
block_type decrypt(block_type plaintext) {
k">constexpr const block_type decrypt(block_type plaintext) {
auto& A = plaintext[0]; auto& A = plaintext[0];
auto& B = plaintext[1]; auto& B = plaintext[1];
auto& C = plaintext[2]; auto& C = plaintext[2];
auto& D = plaintext[3]; auto& D = plaintext[3];
auto it = S.rbegin();
auto it = S.crbegin();
C -= *(it++); C -= *(it++);
A -= *(it++); A -= *(it++);
@ -178,7 +182,7 @@ public:
D -= *(it++); D -= *(it++);
B -= *(it++); B -= *(it++);
assert(it == S.rend());
assert(it == S.crend());
return plaintext; return plaintext;
} }

Loading…
Cancel
Save