-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcrypto.c
68 lines (63 loc) · 1.68 KB
/
crypto.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* crypto.c - Source file
* Crypt or decrypt given block. XXTEA cipher is used.
* Based on:
* David J. Wheeler and Roger M. Needham (October 1998). "Correction to XTEA".
* Computer Laboratory, Cambridge University, England.
* Author: Vlastimil Kosar <[email protected]>
*/
#include <stdint.h>
#include "crypto.h"
/*
* Decrypt block by XXTEA.
* Params:
* block - block of encrypted data
* len - length of block
* key - 128b key
*/
void decrypt(uint32_t *block, uint32_t len, uint32_t *key)
{
uint32_t z=block[len-1], y=block[0], sum=0, e, DELTA=0x9e3779b9;
int32_t p, q;
q = 6 + 52/len;
sum = q*DELTA ;
while (sum != 0) {
e = (sum >> 2) & 3;
for (p=len-1; p>0; p--)
{
z = block[p-1];
block[p] -= (z>>5^y<<2) + (y>>3^z<<4)^(sum^y) + (key[p&3^e]^z);
y = block[p];
}
z = block[len-1];
block[0] -= (z>>5^y<<2) + (y>>3^z<<4)^(sum^y) + (key[p&3^e]^z);
y = block[0];
sum -= DELTA;
}
}
/*
* Crypt block by XXTEA.
* Params:
* block - block of input data
* len - length of block
* key - 128b key
*/
void crypt(uint32_t *block, uint32_t len, uint32_t *key)
{
uint32_t z=block[len-1], y=block[0], sum=0, e, DELTA=0x9e3779b9;
int32_t p, q;
q = 6 + 52/len;
while (q-- > 0) {
sum += DELTA;
e = (sum >> 2) & 3;
for (p=0; p<len-1; p++)
{
y = block[p+1];
block[p] += (z>>5^y<<2) + (y>>3^z<<4)^(sum^y) + (key[p&3^e]^z);
z = block[p];
}
y = block[0];
block[len-1] += (z>>5^y<<2) + (y>>3^z<<4)^(sum^y) + (key[p&3^e]^z);
z = block[len-1];
}
}