line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
/* |
2
|
|
|
|
|
|
|
* Copyright 2001 Abhijit Menon-Sen |
3
|
|
|
|
|
|
|
*/ |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
#include "tea.h" |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
#define strtonl(s) (uint32_t)(*(s)|*(s+1)<<8|*(s+2)<<16|*(s+3)<<24) |
8
|
|
|
|
|
|
|
#define nltostr(l, s) \ |
9
|
|
|
|
|
|
|
do { \ |
10
|
|
|
|
|
|
|
*(s )=(unsigned char)((l) ); \ |
11
|
|
|
|
|
|
|
*(s+1)=(unsigned char)((l) >> 8); \ |
12
|
|
|
|
|
|
|
*(s+2)=(unsigned char)((l) >> 16); \ |
13
|
|
|
|
|
|
|
*(s+3)=(unsigned char)((l) >> 24); \ |
14
|
|
|
|
|
|
|
} while (0) |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
/* TEA is a 64-bit symmetric block cipher with a 128-bit key, developed |
17
|
|
|
|
|
|
|
by David J. Wheeler and Roger M. Needham, and described in their |
18
|
|
|
|
|
|
|
paper at . |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
This implementation is based on their code in |
21
|
|
|
|
|
|
|
*/ |
22
|
|
|
|
|
|
|
|
23
|
40003
|
|
|
|
|
|
struct tea *tea_setup(unsigned char *key, int rounds) |
24
|
|
|
|
|
|
|
{ |
25
|
40003
|
|
|
|
|
|
struct tea *self = malloc(sizeof(struct tea)); |
26
|
|
|
|
|
|
|
|
27
|
40003
|
50
|
|
|
|
|
if (self) { |
28
|
40003
|
|
|
|
|
|
self->rounds = rounds; |
29
|
|
|
|
|
|
|
|
30
|
40003
|
|
|
|
|
|
self->key[0] = strtonl(key); |
31
|
40003
|
|
|
|
|
|
self->key[1] = strtonl(key+4); |
32
|
40003
|
|
|
|
|
|
self->key[2] = strtonl(key+8); |
33
|
40003
|
|
|
|
|
|
self->key[3] = strtonl(key+12); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
40003
|
|
|
|
|
|
return self; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
40003
|
|
|
|
|
|
void tea_free(struct tea *self) |
40
|
|
|
|
|
|
|
{ |
41
|
40003
|
|
|
|
|
|
free(self); |
42
|
40003
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
140002
|
|
|
|
|
|
void tea_crypt(struct tea *self, |
45
|
|
|
|
|
|
|
unsigned char *input, unsigned char *output, |
46
|
|
|
|
|
|
|
int decrypt) |
47
|
|
|
|
|
|
|
{ |
48
|
|
|
|
|
|
|
int i, rounds; |
49
|
140002
|
|
|
|
|
|
uint32_t delta = 0x9E3779B9, /* 2^31*(sqrt(5)-1) */ |
50
|
140002
|
|
|
|
|
|
*k, y, z, sum = 0; |
51
|
|
|
|
|
|
|
|
52
|
140002
|
|
|
|
|
|
k = self->key; |
53
|
140002
|
|
|
|
|
|
rounds = self->rounds; |
54
|
|
|
|
|
|
|
|
55
|
140002
|
|
|
|
|
|
y = strtonl(input); |
56
|
140002
|
|
|
|
|
|
z = strtonl(input+4); |
57
|
|
|
|
|
|
|
|
58
|
140002
|
100
|
|
|
|
|
if (!decrypt) { |
59
|
2310033
|
100
|
|
|
|
|
for (i = 0; i < rounds; i++) { |
60
|
2240032
|
|
|
|
|
|
y += ((z << 4 ^ z >> 5) + z) ^ (sum + k[sum & 3]); |
61
|
2240032
|
|
|
|
|
|
sum += delta; |
62
|
2240032
|
|
|
|
|
|
z += ((y << 4 ^ y >> 5) + y) ^ (sum + k[sum >> 11 & 3]); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
} else { |
65
|
70001
|
|
|
|
|
|
sum = delta * rounds; |
66
|
2310033
|
100
|
|
|
|
|
for (i = 0; i < rounds; i++) { |
67
|
2240032
|
|
|
|
|
|
z -= ((y << 4 ^ y >> 5) + y) ^ (sum + k[sum >> 11 & 3]); |
68
|
2240032
|
|
|
|
|
|
sum -= delta; |
69
|
2240032
|
|
|
|
|
|
y -= ((z << 4 ^ z >> 5) + z) ^ (sum + k[sum & 3]); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
140002
|
|
|
|
|
|
nltostr(y, output); |
74
|
140002
|
|
|
|
|
|
nltostr(z, output+4); |
75
|
140002
|
|
|
|
|
|
} |