]> git.zerfleddert.de Git - proxmark3-svn/blame - common/tea.c
ADD: a TEA crypto algorithm implemention.
[proxmark3-svn] / common / tea.c
CommitLineData
0a886a1d 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
4// the license.
5//-----------------------------------------------------------------------------
6// Generic TEA crypto code.
7// ref: http://143.53.36.235:8080/source.htm#ansi
8//-----------------------------------------------------------------------------
9#include "tea.h"
10#define ROUNDS 32
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)
15
16void tea_encrypt(uint8_t *v, uint8_t *key) {
17
18 uint32_t a=0,b=0,c=0,d=0,y=0,z=0;
19 uint32_t sum = 0;
20 uint8_t n = ROUNDS;
21
22 //key
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);
27
28 //input
29 y = bytes_to_num(v, 4);
30 z = bytes_to_num(v+4, 4);
31
32 // SWAPENDIAN(a);
33 // SWAPENDIAN(b);
34 // SWAPENDIAN(c);
35 // SWAPENDIAN(d);
36 // SWAPENDIAN(y);
37 // SWAPENDIAN(z);
38
39 while ( n-- > 0 ) {
40 sum += DELTA;
41 y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
42 z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
43 }
44
45 // SWAPENDIAN(y);
46 // SWAPENDIAN(z);
47
48 num_to_bytes(y, 4, v);
49 num_to_bytes(z, 4, v+4);
50}
51
52void tea_decrypt(uint8_t *v, uint8_t *key) {
53
54 uint32_t a=0,b=0,c=0,d=0,y=0,z=0;
55 uint32_t sum = SUM;
56 uint8_t n = ROUNDS;
57
58 //key
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);
63
64 //input
65 y = bytes_to_num(v, 4);
66 z = bytes_to_num(v+4, 4);
67
68 // SWAPENDIAN(a);
69 // SWAPENDIAN(b);
70 // SWAPENDIAN(c);
71 // SWAPENDIAN(d);
72 // SWAPENDIAN(y);
73 // SWAPENDIAN(z);
74
75 /* sum = delta<<5, in general sum = delta * n */
76 while ( n-- > 0 ) {
77 z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
78 y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
79 sum -= DELTA;
80 }
81
82 // SWAPENDIAN(y);
83 // SWAPENDIAN(z);
84 num_to_bytes(y, 4, v);
85 num_to_bytes(z, 4, v+4);
86}
Impressum, Datenschutz