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
65
66
67
68
69
70
71
72
73
74
75
76
|
/*
* 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::encrypt_n(const byte in[], byte out[], u32bit blocks) const
{
for(u32bit i = 0; i != blocks; ++i)
{
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);
in += BLOCK_SIZE;
out += BLOCK_SIZE;
}
}
/*
* XTEA Decryption
*/
void XTEA::decrypt_n(const byte in[], byte out[], u32bit blocks) const
{
for(u32bit i = 0; i != blocks; ++i)
{
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);
in += BLOCK_SIZE;
out += BLOCK_SIZE;
}
}
/*
* 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];
}
}
}
|