forked from danielroehrborn/LEGO-Dimensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tea.c
98 lines (87 loc) · 1.99 KB
/
tea.c
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "tea.h"
/*************************************************/
void encryptBlock(uint8_t * data, uint32_t * key)
{
uint32_t i, blocks;
uint32_t* data32;
data32 = (uint32_t *) data;
flipBytes(data32);
for (i = 0; i < 2; i++)
{
encrypt(&data32[i * 2], key);
}
}
/*************************************************/
void decryptBlock(uint8_t * data, uint32_t * key)
{
uint32_t i, blocks;
uint32_t* data32;
data32 = (uint32_t *) data;
flipBytes(data32);
for (i = 0; i < 2; i++)
{
decrypt(&data32[i * 2], key);
}
}
/*************************************************/
/* encrypt
* Encrypt 64 bits with a 128 bit key using TEA
* From http://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm
* Arguments:
* v - array of two 32 bit uints to be encoded in place
* k - array of four 32 bit uints to act as key
* Returns:
* v - encrypted result
* Side effects:
* None
*/
void encrypt (uint32_t* v, uint32_t* k)
{
/* set up */
uint32_t v0 = v[0];
uint32_t v1 = v[1];
uint32_t sum = 0;
uint32_t i;
/* a key schedule constant */
uint32_t delta = 0x9e3779b9;
/* cache key */
uint32_t k0 = k[0];
uint32_t k1 = k[1];
uint32_t k2 = k[2];
uint32_t k3 = k[3];
/* basic cycle start */
for (i = 0; i < 32; i++)
{
sum += delta;
v0 += ((v1 << 4) + k0) ^ (v1 + sum) ^ ((v1 >> 5) + k1);
v1 += ((v0 << 4) + k2) ^ (v0 + sum) ^ ((v0 >> 5) + k3);
}
/* end cycle */
v[0] = v0;
v[1] = v1;
}
void decrypt (uint32_t* v, uint32_t* k)
{
/* set up */
uint32_t v0 = v[0];
uint32_t v1 = v[1];
uint32_t sum = 0xC6EF3720;
uint32_t i;
/* a key schedule constant */
uint32_t delta = 0x9e3779b9;
/* cache key */
uint32_t k0 = k[0];
uint32_t k1 = k[1];
uint32_t k2 = k[2];
uint32_t k3 = k[3];
/* basic cycle start */
for (i = 0; i < 32; i++)
{
v1 -= ((v0 << 4) + k2) ^ (v0 + sum) ^ ((v0 >> 5) + k3);
v0 -= ((v1 << 4) + k0) ^ (v1 + sum) ^ ((v1 >> 5) + k1);
sum -= delta;
}
/* end cycle */
v[0] = v0;
v[1] = v1;
}