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 #include "commonutil.h" // bytes_to_num etc
14 #define DELTA 0x9E3779B9
15 #define SUM 0xC6EF3720
17 void tea_encrypt(uint8_t *v
, uint8_t *key
) {
19 uint32_t a
= 0, b
= 0, c
= 0, d
= 0, y
= 0, z
= 0;
24 a
= bytes_to_num(key
, 4);
25 b
= bytes_to_num(key
+ 4, 4);
26 c
= bytes_to_num(key
+ 8, 4);
27 d
= bytes_to_num(key
+ 12, 4);
30 y
= bytes_to_num(v
, 4);
31 z
= bytes_to_num(v
+ 4, 4);
35 y
+= ((z
<< 4) + a
) ^ (z
+ sum
) ^ ((z
>> 5) + b
);
36 z
+= ((y
<< 4) + c
) ^ (y
+ sum
) ^ ((y
>> 5) + d
);
39 num_to_bytes(y
, 4, v
);
40 num_to_bytes(z
, 4, v
+ 4);
43 void tea_decrypt(uint8_t *v
, uint8_t *key
) {
45 uint32_t a
= 0, b
= 0, c
= 0, d
= 0, y
= 0, z
= 0;
50 a
= bytes_to_num(key
, 4);
51 b
= bytes_to_num(key
+ 4, 4);
52 c
= bytes_to_num(key
+ 8, 4);
53 d
= bytes_to_num(key
+ 12, 4);
56 y
= bytes_to_num(v
, 4);
57 z
= bytes_to_num(v
+ 4, 4);
59 /* sum = delta<<5, in general sum = delta * n */
61 z
-= ((y
<< 4) + c
) ^ (y
+ sum
) ^ ((y
>> 5) + d
);
62 y
-= ((z
<< 4) + a
) ^ (z
+ sum
) ^ ((z
>> 5) + b
);
65 num_to_bytes(y
, 4, v
);
66 num_to_bytes(z
, 4, v
+ 4);