]>
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
14 void tea_encrypt(uint8_t *v
, uint8_t *key
) {
16 uint32_t a
=0,b
=0,c
=0,d
=0,y
=0,z
=0;
21 a
= bytes_to_num(key
, 4);
22 b
= bytes_to_num(key
+4, 4);
23 c
= bytes_to_num(key
+8, 4);
24 d
= bytes_to_num(key
+12, 4);
27 y
= bytes_to_num(v
, 4);
28 z
= bytes_to_num(v
+4, 4);
32 y
+= ((z
<< 4) + a
) ^ (z
+ sum
) ^ ((z
>> 5) + b
);
33 z
+= ((y
<< 4) + c
) ^ (y
+ sum
) ^ ((y
>> 5) + d
);
36 num_to_bytes(y
, 4, v
);
37 num_to_bytes(z
, 4, v
+4);
40 void tea_decrypt(uint8_t *v
, uint8_t *key
) {
42 uint32_t a
=0,b
=0,c
=0,d
=0,y
=0,z
=0;
47 a
= bytes_to_num(key
, 4);
48 b
= bytes_to_num(key
+4, 4);
49 c
= bytes_to_num(key
+8, 4);
50 d
= bytes_to_num(key
+12, 4);
53 y
= bytes_to_num(v
, 4);
54 z
= bytes_to_num(v
+4, 4);
56 /* sum = delta<<5, in general sum = delta * n */
58 z
-= ((y
<< 4) + c
) ^ (y
+ sum
) ^ ((y
>> 5) + d
);
59 y
-= ((z
<< 4) + a
) ^ (z
+ sum
) ^ ((z
>> 5) + b
);
62 num_to_bytes(y
, 4, v
);
63 num_to_bytes(z
, 4, v
+4);