From 0ef4c578d372f00d0eedac154e279acbd2646f8a Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Wed, 5 Feb 2020 18:54:27 +0100 Subject: [PATCH] More RC6 fixes --- include/rc6_generic.hpp | 46 ++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/include/rc6_generic.hpp b/include/rc6_generic.hpp index 5322583..3a804e2 100644 --- a/include/rc6_generic.hpp +++ b/include/rc6_generic.hpp @@ -66,18 +66,21 @@ class RC6 { class RC6_KeySched { + using sched_t = std::array; public: 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 = v_3*3; private: - std::array S; + sched_t S; public: - RC6_KeySched(std::array L) + constexpr RC6_KeySched(std::array L) { 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; } @@ -88,10 +91,11 @@ class RC6 { 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 ); 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]; } - 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 key_type; typedef std::array block_type; - RC6(const key_type& key) + constexpr RC6(const key_type& key) : S(key) {} - block_type encrypt(block_type plaintext) { + constexpr const block_type encrypt(block_type plaintext) { auto& A = plaintext[0]; auto& B = plaintext[1]; auto& C = plaintext[2]; auto& D = plaintext[3]; - auto it = S.begin(); + auto it = S.cbegin(); B += *(it++); D += *(it++); @@ -153,16 +157,16 @@ public: A += *(it++); C += *(it++); - assert(it == S.end()); + assert(it == S.cend()); return plaintext; } - block_type decrypt(block_type plaintext) { + constexpr const block_type decrypt(block_type plaintext) { auto& A = plaintext[0]; auto& B = plaintext[1]; auto& C = plaintext[2]; auto& D = plaintext[3]; - auto it = S.rbegin(); + auto it = S.crbegin(); C -= *(it++); A -= *(it++); @@ -178,7 +182,7 @@ public: D -= *(it++); B -= *(it++); - assert(it == S.rend()); + assert(it == S.crend()); return plaintext; }