libcruft-crypto/block/tea.hpp

54 lines
1.6 KiB
C++
Raw Normal View History

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
*
* Copyright 2015-2018 Danny Robson <danny@nerdcruft.net>
2018-01-14 17:17:34 +11:00
*/
#pragma once
2018-01-14 17:17:34 +11:00
#include <array>
#include <cstdint>
#include <cstddef>
#include <cruft/util/view.hpp>
#include <cruft/util/std.hpp>
2018-01-14 17:17:34 +11:00
namespace cruft::crypto::block {
/// An implementation of the "Tiny Encryption Algorithm" block cipher.
///
/// ref: http://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm
2018-01-14 17:17:34 +11:00
class TEA {
public:
using key_t = std::array<u32,4>;
using word_t = u32;
static constexpr std::size_t block_size = sizeof (word_t) * 2;
2018-01-14 17:17:34 +11:00
explicit TEA (key_t);
/// Encrypt the contents of `src` into the buffer `dst`.
///
/// src and dst are assumed to be equal sizes, multiples of block_size
/// bytes, and suitably aligned for SIMD.
///
/// \param dst The location of the output
/// \param src The location of the input
void encrypt (cruft::view<word_t*> dst, cruft::view<word_t const*> src);
/// Decrypt the contents of `src` into the buffer `dst`.
///
/// src and dst are assumed to be equal sizes, multiples of block_size
/// bytes, and suitably aligned for SIMD.
///
/// \param dst The location of the output
/// \param src The location of the input
void decrypt (cruft::view<word_t*> dst, cruft::view<word_t const*> src);
2018-01-14 17:17:34 +11:00
private:
key_t const m_key;
2018-01-14 17:17:34 +11:00
};
}