-
Notifications
You must be signed in to change notification settings - Fork 0
/
tea2.c
154 lines (108 loc) · 4.36 KB
/
tea2.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
/**********************************************************
TEA2 - (Tiny Encryption Algorithm 2)
TEA Feistel cipher by David Wheeler & Roger M. Needham
TEA2 by Alexander PUKALL 2006
128-bit block cipher (like AES) 256-bit key 128 rounds
Code free for all, even for commercial software
Compile with gcc : gcc tea2.c -o tea2
**********************************************************/
/**********************************************************
Input values: k[4] 256-bit key
v[2] 128-bit plaintext block
Output values: v[2] 128-bit ciphertext block
**********************************************************/
#include <stdint.h>
#include <stdio.h>
void encrypt (uint64_t v[2], const uint64_t k[4]) {
uint64_t v0=v[0], v1=v[1], sum=0, i; /* set up */
uint64_t delta=0x9E3779B97F4A7C15;
uint64_t k0=k[0], k1=k[1], k2=k[2], k3=k[3];
for (i=0; i<64; i++) { /* basic cycle start */
sum += delta;
v0 += ((v1<<14) + k0) ^ (v1 + sum) ^ ((v1>>15) + k1);
v1 += ((v0<<14) + k2) ^ (v0 + sum) ^ ((v0>>15) + k3);
} /* end cycle */
v[0]=v0; v[1]=v1;
}
void decrypt (uint64_t v[2], const uint64_t k[4]) {
uint64_t v0=v[0], v1=v[1], sum=0x8DDE6E5FD29F0540, i; /* set up; sum is (delta << 6) & 0xFFFFFFFFFFFFFFFF */
uint64_t delta=0x9E3779B97F4A7C15;
uint64_t k0=k[0], k1=k[1], k2=k[2], k3=k[3]; /* cache key */
for (i=0; i<64; i++) { /* each iteration of the loop does two Feistel-cipher rounds */
v1 -= ((v0<<14) + k2) ^ (v0 + sum) ^ ((v0>>15) + k3);
v0 -= ((v1<<14) + k0) ^ (v1 + sum) ^ ((v1>>15) + k1);
sum -= delta;
} /* end cycle */
v[0]=v0; v[1]=v1;
}
void main()
{
uint64_t v[2];
uint64_t k[4];
/* 256-bit key */
k[0]=0x0000000000000000;
k[1]=0x0000000000000000;
k[2]=0x0000000000000000;
k[3]=0x0000000000000001;
/* 128-bit plaintext block */
v[0]=0x0000000000000000;
v[1]=0x0000000000000000;
printf("TEA2 by Alexander PUKALL 2006 \n 128-bit block 256-bit key 128 rounds\n");
printf("Code can be freely use even for commercial software\n");
printf("Based on TEA by David Wheeler & Roger M. Needham\n\n");
printf("Encryption 1\n");
printf("Key: %0.16llX %0.16llX %0.16llX %0.16llX\n",k[0],k[1],k[2],k[3]);
printf("Plaintext: %0.16llX %0.16llX\n",v[0],v[1]);
encrypt(v,k);
printf("Ciphertext:%0.16llX %0.16llX\n",v[0],v[1]);
decrypt(v,k);
printf("Decrypted: %0.16llX %0.16llX\n\n",v[0],v[1]);
/* 256-bit key */
k[0]=0x0000000000000000;
k[1]=0x0000000000000000;
k[2]=0x0000000000000000;
k[3]=0x0000000000000001;
/* 128-bit plaintext block */
v[0]=0x0000000000000000;
v[1]=0x0000000000000001;
printf("Encryption 2\n");
printf("Key: %0.16llX %0.16llX %0.16llX %0.16llX\n",k[0],k[1],k[2],k[3]);
printf("Plaintext: %0.16llX %0.16llX\n",v[0],v[1]);
encrypt(v,k);
printf("Ciphertext:%0.16llX %0.16llX\n",v[0],v[1]);
decrypt(v,k);
printf("Decrypted: %0.16llX %0.16llX\n\n",v[0],v[1]);
/* 256-bit key */
k[0]=0x0000000000000000;
k[1]=0x0000000000000000;
k[2]=0x0000000000000000;
k[3]=0x0000000000000001;
/* 128-bit plaintext block */
v[0]=0x0000000000000001;
v[1]=0x0000000000000001;
printf("Encryption 3\n");
printf("Key: %0.16llX %0.16llX %0.16llX %0.16llX\n",k[0],k[1],k[2],k[3]);
printf("Plaintext: %0.16llX %0.16llX\n",v[0],v[1]);
encrypt(v,k);
printf("Ciphertext:%0.16llX %0.16llX\n",v[0],v[1]);
decrypt(v,k);
printf("Decrypted: %0.16llX %0.16llX\n\n",v[0],v[1]);
}
/*
Encryption 1
Key: 0000000000000000 0000000000000000 0000000000000000 0000000000000001
Plaintext: 0000000000000000 0000000000000000
Ciphertext:D713374DD796B948 93E198C8BF480EEA
Decrypted: 0000000000000000 0000000000000000
Encryption 2
Key: 0000000000000000 0000000000000000 0000000000000000 0000000000000001
Plaintext: 0000000000000000 0000000000000001
Ciphertext:85B25256E406EF80 88B6D9C61E7C08F1
Decrypted: 0000000000000000 0000000000000001
Encryption 3
Key: 0000000000000000 0000000000000000 0000000000000000 0000000000000001
Plaintext: 0000000000000001 0000000000000001
Ciphertext:9F6CCED0EAF20C18 CA4F15379C175F5C
Decrypted: 0000000000000001 0000000000000001
*/