]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - common/tea.c
1 //-----------------------------------------------------------------------------
2 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
3 // at your option, any later version. See the LICENSE.txt file for the text of
5 //-----------------------------------------------------------------------------
6 // Generic TEA crypto code.
7 // ref: http://143.53.36.235:8080/source.htm#ansi
8 //-----------------------------------------------------------------------------
11 #define DELTA 0x9E3779B9
12 #define SUM 0xC6EF3720
13 #define SWAPENDIAN(x)\
14 (x = (x >> 8 & 0xff00ff) | (x & 0xff00ff) << 8, x = x >> 16 | x << 16)
16 void tea_encrypt(uint8_t *v
, uint8_t *key
) {
18 uint32_t a
=0,b
=0,c
=0,d
=0,y
=0,z
=0;
23 a
= bytes_to_num(key
, 4);
24 b
= bytes_to_num(key
+4, 4);
25 c
= bytes_to_num(key
+8, 4);
26 d
= bytes_to_num(key
+12, 4);
29 y
= bytes_to_num(v
, 4);
30 z
= bytes_to_num(v
+4, 4);
41 y
+= ((z
<< 4) + a
) ^ (z
+ sum
) ^ ((z
>> 5) + b
);
42 z
+= ((y
<< 4) + c
) ^ (y
+ sum
) ^ ((y
>> 5) + d
);
48 num_to_bytes(y
, 4, v
);
49 num_to_bytes(z
, 4, v
+4);
52 void tea_decrypt(uint8_t *v
, uint8_t *key
) {
54 uint32_t a
=0,b
=0,c
=0,d
=0,y
=0,z
=0;
59 a
= bytes_to_num(key
, 4);
60 b
= bytes_to_num(key
+4, 4);
61 c
= bytes_to_num(key
+8, 4);
62 d
= bytes_to_num(key
+12, 4);
65 y
= bytes_to_num(v
, 4);
66 z
= bytes_to_num(v
+4, 4);
75 /* sum = delta<<5, in general sum = delta * n */
77 z
-= ((y
<< 4) + c
) ^ (y
+ sum
) ^ ((y
>> 5) + d
);
78 y
-= ((z
<< 4) + a
) ^ (z
+ sum
) ^ ((z
>> 5) + b
);
84 num_to_bytes(y
, 4, v
);
85 num_to_bytes(z
, 4, v
+4);