2015-05-06 23:49:45 +10:00
|
|
|
/*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tea.hpp"
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
using util::crypto::TEA;
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
static const std::uint32_t MAGIC = 0x9E3779B9;
|
|
|
|
|
|
|
|
// each iteration performs two feistel rounds, for a total of 64
|
|
|
|
static const unsigned ITERATIONS = 32;
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
TEA::TEA (key_t _key):
|
|
|
|
m_key (_key)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
2015-05-15 18:26:45 +10:00
|
|
|
TEA::encrypt (uint32_t *restrict data, size_t count)
|
2015-05-06 23:49:45 +10:00
|
|
|
{
|
|
|
|
if (count % 2)
|
|
|
|
throw std::invalid_argument ("TEA requires even data count");
|
|
|
|
|
2015-05-15 18:26:45 +10:00
|
|
|
auto last = data + count;
|
|
|
|
while (data < last) {
|
2015-05-06 23:49:45 +10:00
|
|
|
uint32_t sum = 0;
|
2015-05-15 18:26:45 +10:00
|
|
|
uint32_t v0 = data[0];
|
|
|
|
uint32_t v1 = data[1];
|
2015-05-06 23:49:45 +10:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < ITERATIONS; ++i) {
|
|
|
|
sum += MAGIC;
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
2015-05-15 18:26:45 +10:00
|
|
|
*data++ = v0;
|
|
|
|
*data++ = v1;
|
2015-05-06 23:49:45 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
2015-05-15 18:26:45 +10:00
|
|
|
TEA::decrypt (uint32_t *restrict data, size_t count)
|
2015-05-06 23:49:45 +10:00
|
|
|
{
|
|
|
|
if (count % 2)
|
|
|
|
throw std::invalid_argument ("TEA requires even data count");
|
|
|
|
|
2015-05-15 18:26:45 +10:00
|
|
|
auto last = data + count;
|
2015-05-06 23:49:45 +10:00
|
|
|
|
2015-05-15 18:26:45 +10:00
|
|
|
while (data < last) {
|
2015-05-06 23:49:45 +10:00
|
|
|
uint32_t sum = MAGIC << 5;
|
2015-05-15 18:26:45 +10:00
|
|
|
uint32_t v0 = data[0];
|
|
|
|
uint32_t v1 = data[1];
|
2015-05-06 23:49:45 +10:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < ITERATIONS; ++i) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-05-15 18:26:45 +10:00
|
|
|
*data++ = v0;
|
|
|
|
*data++ = v1;
|
2015-05-06 23:49:45 +10:00
|
|
|
}
|
|
|
|
}
|