Botan  1.10.9
tea.cpp
Go to the documentation of this file.
1 /*
2 * TEA
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #include <botan/tea.h>
9 #include <botan/loadstor.h>
10 
11 namespace Botan {
12 
13 /*
14 * TEA Encryption
15 */
16 void TEA::encrypt_n(const byte in[], byte out[], size_t blocks) const
17  {
18  for(size_t i = 0; i != blocks; ++i)
19  {
20  u32bit L = load_be<u32bit>(in, 0);
21  u32bit R = load_be<u32bit>(in, 1);
22 
23  u32bit S = 0;
24  for(size_t j = 0; j != 32; ++j)
25  {
26  S += 0x9E3779B9;
27  L += ((R << 4) + K[0]) ^ (R + S) ^ ((R >> 5) + K[1]);
28  R += ((L << 4) + K[2]) ^ (L + S) ^ ((L >> 5) + K[3]);
29  }
30 
31  store_be(out, L, R);
32 
33  in += BLOCK_SIZE;
34  out += BLOCK_SIZE;
35  }
36  }
37 
38 /*
39 * TEA Decryption
40 */
41 void TEA::decrypt_n(const byte in[], byte out[], size_t blocks) const
42  {
43  for(size_t i = 0; i != blocks; ++i)
44  {
45  u32bit L = load_be<u32bit>(in, 0);
46  u32bit R = load_be<u32bit>(in, 1);
47 
48  u32bit S = 0xC6EF3720;
49  for(size_t j = 0; j != 32; ++j)
50  {
51  R -= ((L << 4) + K[2]) ^ (L + S) ^ ((L >> 5) + K[3]);
52  L -= ((R << 4) + K[0]) ^ (R + S) ^ ((R >> 5) + K[1]);
53  S -= 0x9E3779B9;
54  }
55 
56  store_be(out, L, R);
57 
58  in += BLOCK_SIZE;
59  out += BLOCK_SIZE;
60  }
61  }
62 
63 /*
64 * TEA Key Schedule
65 */
66 void TEA::key_schedule(const byte key[], size_t)
67  {
68  for(size_t i = 0; i != 4; ++i)
69  K[i] = load_be<u32bit>(key, i);
70  }
71 
72 }
void encrypt_n(const byte in[], byte out[], size_t blocks) const
Definition: tea.cpp:16
unsigned char byte
Definition: types.h:22
u32bit load_be< u32bit >(const byte in[], size_t off)
Definition: loadstor.h:166
void decrypt_n(const byte in[], byte out[], size_t blocks) const
Definition: tea.cpp:41
void store_be(u16bit in, byte out[2])
Definition: loadstor.h:412
unsigned int u32bit
Definition: types.h:32