2018-01-14 17:17:34 +11:00
|
|
|
/*
|
2018-08-04 15:18:16 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-01-14 17:17:34 +11:00
|
|
|
*
|
2018-12-02 13:45:26 +11:00
|
|
|
* Copyright 2015-2018 Danny Robson <danny@nerdcruft.net>
|
2018-01-14 17:17:34 +11:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tea.hpp"
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
using cruft::crypto::block::TEA;
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-12-02 14:20:16 +11:00
|
|
|
static u32 constexpr MAGIC = 0x9E3779B9;
|
2018-12-02 13:45:26 +11:00
|
|
|
|
2018-01-14 17:17:34 +11:00
|
|
|
|
2018-12-02 14:20:16 +11:00
|
|
|
static int constexpr ITERATIONS = 64;
|
2018-01-14 17:17:34 +11:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-12-02 13:45:26 +11:00
|
|
|
TEA::TEA (key_t _key)
|
|
|
|
: m_key (_key)
|
2018-01-14 17:17:34 +11:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2018-12-02 13:45:26 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-01-14 17:17:34 +11:00
|
|
|
void
|
2018-12-02 14:20:16 +11:00
|
|
|
TEA::encrypt (cruft::view<word_t*> dst, cruft::view<word_t const*> src)
|
2018-01-14 17:17:34 +11:00
|
|
|
{
|
2018-12-02 14:20:16 +11:00
|
|
|
if (dst.size () != src.size ())
|
|
|
|
throw std::invalid_argument ("mismatching encode/decode buffer sizes");
|
|
|
|
if (src.size () % 2)
|
2018-01-14 17:17:34 +11:00
|
|
|
throw std::invalid_argument ("TEA requires even data count");
|
|
|
|
|
2018-12-02 14:20:16 +11:00
|
|
|
for (size_t i = 0, last = src.size (); i < last; i += 2) {
|
2018-12-02 13:45:26 +11:00
|
|
|
word_t sum = 0;
|
2018-12-02 14:20:16 +11:00
|
|
|
word_t v0 = src[i + 0];
|
|
|
|
word_t v1 = src[i + 1];
|
2018-01-14 17:17:34 +11:00
|
|
|
|
2018-12-02 14:20:16 +11:00
|
|
|
for (int j = 0; j < ITERATIONS; j += 2) {
|
2018-01-14 17:17:34 +11:00
|
|
|
sum += MAGIC;
|
2018-12-02 14:20:16 +11:00
|
|
|
|
2018-01-14 17:17:34 +11:00
|
|
|
v0 += ((v1 << 4) + m_key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + m_key[1]);
|
|
|
|
v1 += ((v0 << 4) + m_key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + m_key[3]);
|
|
|
|
}
|
|
|
|
|
2018-12-02 14:20:16 +11:00
|
|
|
dst[i + 0] = v0;
|
|
|
|
dst[i + 1] = v1;
|
2018-01-14 17:17:34 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
2018-12-02 14:20:16 +11:00
|
|
|
TEA::decrypt (cruft::view<word_t*> dst, cruft::view<word_t const*> src)
|
2018-01-14 17:17:34 +11:00
|
|
|
{
|
2018-12-02 14:20:16 +11:00
|
|
|
if (dst.size () != src.size ())
|
|
|
|
throw std::invalid_argument ("mismatching encode/decode buffer sizes");
|
|
|
|
if (src.size () % 2)
|
2018-01-14 17:17:34 +11:00
|
|
|
throw std::invalid_argument ("TEA requires even data count");
|
|
|
|
|
2018-12-02 14:20:16 +11:00
|
|
|
for (size_t i = 0, last = src.size (); i < last; i += 2) {
|
2018-12-02 13:45:26 +11:00
|
|
|
word_t sum = MAGIC << 5;
|
2018-12-02 14:20:16 +11:00
|
|
|
word_t v0 = src[i + 0];
|
|
|
|
word_t v1 = src[i + 1];
|
2018-01-14 17:17:34 +11:00
|
|
|
|
2018-12-02 14:20:16 +11:00
|
|
|
for (int j = 0; j < ITERATIONS; j += 2) {
|
2018-01-14 17:17:34 +11:00
|
|
|
v1 -= ((v0 << 4) + m_key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + m_key[3]);
|
|
|
|
v0 -= ((v1 << 4) + m_key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + m_key[1]);
|
|
|
|
sum -= MAGIC;
|
|
|
|
}
|
|
|
|
|
2018-12-02 14:20:16 +11:00
|
|
|
dst[i + 0] = v0;
|
|
|
|
dst[i + 1] = v1;
|
2018-01-14 17:17:34 +11:00
|
|
|
}
|
|
|
|
}
|