Botan
1.11.15
|
00001 /* 00002 * TEA 00003 * (C) 1999-2007 Jack Lloyd 00004 * 00005 * Botan is released under the Simplified BSD License (see license.txt) 00006 */ 00007 00008 #include <botan/internal/block_utils.h> 00009 #include <botan/tea.h> 00010 00011 namespace Botan { 00012 00013 BOTAN_REGISTER_BLOCK_CIPHER_NOARGS(TEA); 00014 00015 /* 00016 * TEA Encryption 00017 */ 00018 void TEA::encrypt_n(const byte in[], byte out[], size_t blocks) const 00019 { 00020 for(size_t i = 0; i != blocks; ++i) 00021 { 00022 u32bit L = load_be<u32bit>(in, 0); 00023 u32bit R = load_be<u32bit>(in, 1); 00024 00025 u32bit S = 0; 00026 for(size_t j = 0; j != 32; ++j) 00027 { 00028 S += 0x9E3779B9; 00029 L += ((R << 4) + K[0]) ^ (R + S) ^ ((R >> 5) + K[1]); 00030 R += ((L << 4) + K[2]) ^ (L + S) ^ ((L >> 5) + K[3]); 00031 } 00032 00033 store_be(out, L, R); 00034 00035 in += BLOCK_SIZE; 00036 out += BLOCK_SIZE; 00037 } 00038 } 00039 00040 /* 00041 * TEA Decryption 00042 */ 00043 void TEA::decrypt_n(const byte in[], byte out[], size_t blocks) const 00044 { 00045 for(size_t i = 0; i != blocks; ++i) 00046 { 00047 u32bit L = load_be<u32bit>(in, 0); 00048 u32bit R = load_be<u32bit>(in, 1); 00049 00050 u32bit S = 0xC6EF3720; 00051 for(size_t j = 0; j != 32; ++j) 00052 { 00053 R -= ((L << 4) + K[2]) ^ (L + S) ^ ((L >> 5) + K[3]); 00054 L -= ((R << 4) + K[0]) ^ (R + S) ^ ((R >> 5) + K[1]); 00055 S -= 0x9E3779B9; 00056 } 00057 00058 store_be(out, L, R); 00059 00060 in += BLOCK_SIZE; 00061 out += BLOCK_SIZE; 00062 } 00063 } 00064 00065 /* 00066 * TEA Key Schedule 00067 */ 00068 void TEA::key_schedule(const byte key[], size_t) 00069 { 00070 K.resize(4); 00071 for(size_t i = 0; i != 4; ++i) 00072 K[i] = load_be<u32bit>(key, i); 00073 } 00074 00075 void TEA::clear() 00076 { 00077 zap(K); 00078 } 00079 00080 }