diff --git a/crypto/tea.cpp b/crypto/tea.cpp index 514ab3f0..429fc442 100644 --- a/crypto/tea.cpp +++ b/crypto/tea.cpp @@ -37,16 +37,16 @@ TEA::TEA (key_t _key): //----------------------------------------------------------------------------- void -TEA::encrypt (uint32_t *restrict dst, const uint32_t *restrict src, size_t count) +TEA::encrypt (uint32_t *restrict data, size_t count) { if (count % 2) throw std::invalid_argument ("TEA requires even data count"); - auto last = src + count; - while (src < last) { + auto last = data + count; + while (data < last) { uint32_t sum = 0; - uint32_t v0 = src[0]; - uint32_t v1 = src[1]; + uint32_t v0 = data[0]; + uint32_t v1 = data[1]; for (unsigned i = 0; i < ITERATIONS; ++i) { sum += MAGIC; @@ -54,28 +54,25 @@ TEA::encrypt (uint32_t *restrict dst, const uint32_t *restrict src, size_t count v1 += ((v0 << 4) + m_key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + m_key[3]); } - dst[0] = v0; - dst[1] = v1; - - src += 2; - dst += 2; + *data++ = v0; + *data++ = v1; } } //----------------------------------------------------------------------------- void -TEA::decrypt (uint32_t *restrict dst, const uint32_t *restrict src, size_t count) +TEA::decrypt (uint32_t *restrict data, size_t count) { if (count % 2) throw std::invalid_argument ("TEA requires even data count"); - auto last = src + count; + auto last = data + count; - while (src < last) { + while (data < last) { uint32_t sum = MAGIC << 5; - uint32_t v0 = src[0]; - uint32_t v1 = src[1]; + uint32_t v0 = data[0]; + uint32_t v1 = data[1]; for (unsigned i = 0; i < ITERATIONS; ++i) { v1 -= ((v0 << 4) + m_key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + m_key[3]); @@ -83,10 +80,7 @@ TEA::decrypt (uint32_t *restrict dst, const uint32_t *restrict src, size_t count sum -= MAGIC; } - dst[0] = v0; - dst[1] = v1; - - src += 2; - dst += 2; + *data++ = v0; + *data++ = v1; } } diff --git a/crypto/tea.hpp b/crypto/tea.hpp index 96ad2a1e..eda963fb 100644 --- a/crypto/tea.hpp +++ b/crypto/tea.hpp @@ -29,8 +29,8 @@ namespace util { namespace crypto { TEA (key_t); - void encrypt (uint32_t *restrict dst, const uint32_t *restrict src, size_t count); - void decrypt (uint32_t *restrict dst, const uint32_t *restrict src, size_t count); + void encrypt (uint32_t *restrict data, size_t count); + void decrypt (uint32_t *restrict data, size_t count); private: key_t m_key; diff --git a/crypto/xtea.cpp b/crypto/xtea.cpp index 1337a627..8b689fec 100644 --- a/crypto/xtea.cpp +++ b/crypto/xtea.cpp @@ -34,17 +34,17 @@ XTEA::XTEA (key_t _key): //----------------------------------------------------------------------------- void -XTEA::encrypt (uint32_t *restrict dst, const uint32_t *restrict src, size_t count) +XTEA::encrypt (uint32_t *restrict data, size_t count) { if (count % 2) throw std::invalid_argument ("XTEA requires even data count"); - auto last = src + count; + auto last = data + count; - while (src < last) { + while (data < last) { uint32_t sum = 0; - uint32_t v0 = src[0]; - uint32_t v1 = src[1]; + uint32_t v0 = data[0]; + uint32_t v1 = data[1]; for (unsigned i = 0; i < ITERATIONS; ++i) { v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + m_key[sum & 3]); @@ -52,28 +52,25 @@ XTEA::encrypt (uint32_t *restrict dst, const uint32_t *restrict src, size_t coun v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + m_key[(sum >> 11) & 3]); } - dst[0] = v0; - dst[1] = v1; - - src += 2; - dst += 2; + *data++ = v0; + *data++ = v1; } } //----------------------------------------------------------------------------- void -XTEA::decrypt (uint32_t *restrict dst, const uint32_t *restrict src, size_t count) +XTEA::decrypt (uint32_t *restrict data, size_t count) { if (count % 2) throw std::invalid_argument ("XTEA requires even data count"); - auto last = src + count; + auto last = data + count; - while (src < last) { + while (data < last) { uint32_t sum = ITERATIONS * MAGIC; - uint32_t v0 = src[0]; - uint32_t v1 = src[1]; + uint32_t v0 = data[0]; + uint32_t v1 = data[1]; for (unsigned i = 0; i < ITERATIONS; ++i) { v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + m_key[(sum >> 11) & 3]); @@ -81,9 +78,7 @@ XTEA::decrypt (uint32_t *restrict dst, const uint32_t *restrict src, size_t coun v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + m_key[sum & 3]); } - dst[0] = v0; - dst[1] = v1; - - src += 2; + *data++ = v0; + *data++ = v1; } } diff --git a/crypto/xtea.hpp b/crypto/xtea.hpp index 6670a871..7582213a 100644 --- a/crypto/xtea.hpp +++ b/crypto/xtea.hpp @@ -29,8 +29,8 @@ namespace util { namespace crypto { XTEA (key_t); - void encrypt (uint32_t *restrict dst, const uint32_t *restrict src, size_t count); - void decrypt (uint32_t *restrict dst, const uint32_t *restrict src, size_t count); + void encrypt (uint32_t *restrict data, size_t count); + void decrypt (uint32_t *restrict data, size_t count); private: key_t m_key; diff --git a/test/crypto/tea.cpp b/test/crypto/tea.cpp index 2a437840..3eef45ac 100644 --- a/test/crypto/tea.cpp +++ b/test/crypto/tea.cpp @@ -7,7 +7,7 @@ int main () { // test vectors from 'TeaCrypt', by Logan J. Drews. - struct { + static const struct { std::array key; std::array dec; std::array enc; @@ -44,10 +44,11 @@ main () const auto &t = TESTS[i]; util::crypto::TEA gen (t.key); - std::array enc, dec; + std::array enc (t.dec); + gen.encrypt (enc.data (), enc.size ()); - gen.encrypt (enc.data (), t.dec.data (), t.dec.size ()); - gen.decrypt (dec.data (), t.enc.data (), t.enc.size ()); + std::array dec (t.enc); + gen.decrypt (dec.data (), dec.size ()); { std::ostringstream os; diff --git a/test/crypto/xtea.cpp b/test/crypto/xtea.cpp index eb0a9bdb..2263080b 100644 --- a/test/crypto/xtea.cpp +++ b/test/crypto/xtea.cpp @@ -6,7 +6,7 @@ int main () { // test vectors from 'TeaCrypt', by Logan J. Drews. - struct { + static const struct { std::array key; std::array dec; std::array enc; @@ -43,20 +43,21 @@ main () const auto &t = TESTS[i]; util::crypto::XTEA gen (t.key); - std::array enc, dec; + std::array enc (t.dec); + gen.encrypt (enc.data (), enc.size ()); - gen.encrypt (enc.data (), t.dec.data (), t.dec.size ()); - gen.decrypt (dec.data (), t.enc.data (), t.enc.size ()); + std::array dec (t.enc); + gen.decrypt (dec.data (), dec.size ()); { std::ostringstream os; - os << "TEA_enc " << i; + os << "XTEA_enc " << i; tap.expect (enc == t.enc, os.str ()); } { std::ostringstream os; - os << "TEA_dec " << i; + os << "XTEA_dec " << i; tap.expect (dec == t.dec, os.str ()); } }