aboutsummaryrefslogtreecommitdiffstats
path: root/src/block/xtea/xtea.cpp
blob: 5047f659454f20861850942edc5ff801497dae09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* XTEA
* (C) 1999-2009 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

#include <botan/xtea.h>
#include <botan/loadstor.h>
#include <botan/parsing.h>

namespace Botan {

/*
* XTEA Encryption
*/
void XTEA::enc(const byte in[], byte out[]) const
   {
   u32bit L = load_be<u32bit>(in, 0), R = load_be<u32bit>(in, 1);

   for(u32bit j = 0; j != 32; ++j)
      {
      L += (((R << 4) ^ (R >> 5)) + R) ^ EK[2*j];
      R += (((L << 4) ^ (L >> 5)) + L) ^ EK[2*j+1];
      }

   store_be(out, L, R);
   }

/*
* XTEA Decryption
*/
void XTEA::dec(const byte in[], byte out[]) const
   {
   u32bit L = load_be<u32bit>(in, 0), R = load_be<u32bit>(in, 1);

   for(u32bit j = 0; j != 32; ++j)
      {
      R -= (((L << 4) ^ (L >> 5)) + L) ^ EK[63 - 2*j];
      L -= (((R << 4) ^ (R >> 5)) + R) ^ EK[62 - 2*j];
      }

   store_be(out, L, R);
   }

/*
* XTEA Key Schedule
*/
void XTEA::key_schedule(const byte key[], u32bit)
   {
   SecureBuffer<u32bit, 4> UK;
   for(u32bit i = 0; i != 4; ++i)
      UK[i] = load_be<u32bit>(key, i);

   u32bit D = 0;
   for(u32bit i = 0; i != 64; i += 2)
      {
      EK[i  ] = D + UK[D % 4];
      D += 0x9E3779B9;
      EK[i+1] = D + UK[(D >> 11) % 4];
      }
   }

}