TEA: add "Tiny Encryption Algorithm"

This commit is contained in:
Danny Robson 2015-05-06 23:49:45 +10:00
parent 717644acde
commit 9d3c058873
4 changed files with 202 additions and 0 deletions

View File

@ -25,6 +25,8 @@ UTIL_FILES = \
coord/names.hpp \
coord/ops.hpp \
coord/store.hpp \
crypto/tea.cpp \
crypto/tea.hpp \
crypto/arc4.cpp \
crypto/arc4.hpp \
debug.cpp \
@ -264,6 +266,7 @@ TEST_BIN = \
test/checksum \
test/colour \
test/crypto/arc4 \
test/crypto/tea \
test/extent \
test/fixed \
test/float \

92
crypto/tea.cpp Normal file
View File

@ -0,0 +1,92 @@
/*
* 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
TEA::encrypt (uint32_t *restrict dst, const uint32_t *restrict src, size_t count)
{
if (count % 2)
throw std::invalid_argument ("TEA requires even data count");
auto last = src + count;
while (src < last) {
uint32_t sum = 0;
uint32_t v0 = src[0];
uint32_t v1 = src[1];
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]);
}
dst[0] = v0;
dst[1] = v1;
src += 2;
dst += 2;
}
}
//-----------------------------------------------------------------------------
void
TEA::decrypt (uint32_t *restrict dst, const uint32_t *restrict src, size_t count)
{
if (count % 2)
throw std::invalid_argument ("TEA requires even data count");
auto last = src + count;
while (src < last) {
uint32_t sum = MAGIC << 5;
uint32_t v0 = src[0];
uint32_t v1 = src[1];
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;
}
dst[0] = v0;
dst[1] = v1;
src += 2;
dst += 2;
}
}

40
crypto/tea.hpp Normal file
View File

@ -0,0 +1,40 @@
/*
* 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>
*/
#ifndef __UTIL_TEA_HPP
#define __UTIL_TEA_HPP
#include <array>
#include <cstdint>
#include <cstddef>
namespace util { namespace crypto {
// http://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm
class TEA {
public:
using key_t = std::array<uint32_t,4>;
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);
private:
key_t m_key;
};
} }
#endif

67
test/crypto/tea.cpp Normal file
View File

@ -0,0 +1,67 @@
#include "tap.hpp"
#include "types.hpp"
#include "crypto/tea.hpp"
int
main ()
{
// test vectors from 'TeaCrypt', by Logan J. Drews.
struct {
std::array<uint32_t,4> key;
std::vector<uint32_t> dec;
std::vector<uint32_t> enc;
} TESTS[] = {
{
{ 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
{ 0x00000000, 0x00000000 },
{ 0x41EA3A0A, 0x94BAA940 },
},
{
{ 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
{ 0x01020304, 0x05060708 },
{ 0x6A2F9CF3, 0xFCCF3C55 },
},
{
{ 0x00112233, 0x44556677, 0x8899AABB, 0xCCDDEEFF },
{ 0x01020304, 0x05060708 },
{ 0xDEB1C0A2, 0x7E745DB3 },
},
{
{ 0x00112233, 0x44556677, 0x8899AABB, 0xCCDDEEFF },
{ 0x01234567, 0x89ABCDEF },
{ 0x126C6B92, 0xC0653A3E },
},
};
util::TAP::logger tap;
for (size_t i = 0; i < elems (TESTS); ++i) {
const auto &t = TESTS[i];
util::crypto::TEA gen (t.key);
std::vector<uint32_t> enc (t.dec.size ()),
dec (t.enc.size ());
gen.encrypt (enc.data (), t.dec.data (), t.dec.size ());
gen.decrypt (dec.data (), t.enc.data (), t.enc.size ());
{
std::ostringstream os;
os << "TEA_enc " << i;
tap.expect (std::equal (enc.begin (), enc.end (), t.enc.begin ()), os.str ());
}
{
std::ostringstream os;
os << "TEA_dec " << i;
tap.expect (std::equal (dec.begin (), dec.end (), t.dec.begin ()), os.str ());
}
}
return tap.status ();
}