diff options
Diffstat (limited to 'lib/block/tea')
-rw-r--r-- | lib/block/tea/info.txt | 1 | ||||
-rw-r--r-- | lib/block/tea/tea.cpp | 78 | ||||
-rw-r--r-- | lib/block/tea/tea.h | 34 |
3 files changed, 113 insertions, 0 deletions
diff --git a/lib/block/tea/info.txt b/lib/block/tea/info.txt new file mode 100644 index 000000000..14edfdb03 --- /dev/null +++ b/lib/block/tea/info.txt @@ -0,0 +1 @@ +define TEA 20131128 diff --git a/lib/block/tea/tea.cpp b/lib/block/tea/tea.cpp new file mode 100644 index 000000000..2accab700 --- /dev/null +++ b/lib/block/tea/tea.cpp @@ -0,0 +1,78 @@ +/* +* TEA +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/tea.h> +#include <botan/loadstor.h> + +namespace Botan { + +/* +* TEA Encryption +*/ +void TEA::encrypt_n(const byte in[], byte out[], size_t blocks) const + { + for(size_t i = 0; i != blocks; ++i) + { + u32bit L = load_be<u32bit>(in, 0); + u32bit R = load_be<u32bit>(in, 1); + + u32bit S = 0; + for(size_t j = 0; j != 32; ++j) + { + S += 0x9E3779B9; + L += ((R << 4) + K[0]) ^ (R + S) ^ ((R >> 5) + K[1]); + R += ((L << 4) + K[2]) ^ (L + S) ^ ((L >> 5) + K[3]); + } + + store_be(out, L, R); + + in += BLOCK_SIZE; + out += BLOCK_SIZE; + } + } + +/* +* TEA Decryption +*/ +void TEA::decrypt_n(const byte in[], byte out[], size_t blocks) const + { + for(size_t i = 0; i != blocks; ++i) + { + u32bit L = load_be<u32bit>(in, 0); + u32bit R = load_be<u32bit>(in, 1); + + u32bit S = 0xC6EF3720; + for(size_t j = 0; j != 32; ++j) + { + R -= ((L << 4) + K[2]) ^ (L + S) ^ ((L >> 5) + K[3]); + L -= ((R << 4) + K[0]) ^ (R + S) ^ ((R >> 5) + K[1]); + S -= 0x9E3779B9; + } + + store_be(out, L, R); + + in += BLOCK_SIZE; + out += BLOCK_SIZE; + } + } + +/* +* TEA Key Schedule +*/ +void TEA::key_schedule(const byte key[], size_t) + { + K.resize(4); + for(size_t i = 0; i != 4; ++i) + K[i] = load_be<u32bit>(key, i); + } + +void TEA::clear() + { + zap(K); + } + +} diff --git a/lib/block/tea/tea.h b/lib/block/tea/tea.h new file mode 100644 index 000000000..0d203975e --- /dev/null +++ b/lib/block/tea/tea.h @@ -0,0 +1,34 @@ +/* +* TEA +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_TEA_H__ +#define BOTAN_TEA_H__ + +#include <botan/block_cipher.h> + +namespace Botan { + +/** +* TEA +*/ +class BOTAN_DLL TEA : public Block_Cipher_Fixed_Params<8, 16> + { + public: + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; + + void clear(); + std::string name() const { return "TEA"; } + BlockCipher* clone() const { return new TEA; } + private: + void key_schedule(const byte[], size_t); + secure_vector<u32bit> K; + }; + +} + +#endif |