]> cvs.zerfleddert.de Git - proxmark3-svn/blame_incremental - common/tea.c
FIX: @Aczid suggested this to fix a Coverity scan warning.
[proxmark3-svn] / common / tea.c
... / ...
CommitLineData
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
14void tea_encrypt(uint8_t *v, uint8_t *key) {
15
16 uint32_t a=0,b=0,c=0,d=0,y=0,z=0;
17 uint32_t sum = 0;
18 uint8_t n = ROUNDS;
19
20 //key
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);
25
26 //input
27 y = bytes_to_num(v, 4);
28 z = bytes_to_num(v+4, 4);
29
30 while ( n-- > 0 ) {
31 sum += DELTA;
32 y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
33 z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
34 }
35
36 num_to_bytes(y, 4, v);
37 num_to_bytes(z, 4, v+4);
38}
39
40void tea_decrypt(uint8_t *v, uint8_t *key) {
41
42 uint32_t a=0,b=0,c=0,d=0,y=0,z=0;
43 uint32_t sum = SUM;
44 uint8_t n = ROUNDS;
45
46 //key
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);
51
52 //input
53 y = bytes_to_num(v, 4);
54 z = bytes_to_num(v+4, 4);
55
56 /* sum = delta<<5, in general sum = delta * n */
57 while ( n-- > 0 ) {
58 z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
59 y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
60 sum -= DELTA;
61 }
62 num_to_bytes(y, 4, v);
63 num_to_bytes(z, 4, v+4);
64}
Impressum, Datenschutz