-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathxtea.c
More file actions
38 lines (34 loc) · 1.01 KB
/
xtea.c
File metadata and controls
38 lines (34 loc) · 1.01 KB
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
/*
* XTEA block cipher, by David Wheeler and Roger Needham.
* See https://en.wikipedia.org/wiki/XTEA
*/
#include "xtea.h"
/* take 64 bits of data in v[0] and v[1] and 128 bits of key[0] - key[3] */
void
xtea_encipher(uint32_t v[2], uint32_t const key[4])
{
const unsigned int num_rounds = 64;
unsigned int i;
uint32_t v0=v[0], v1=v[1], sum=0, delta=0x9E3779B9;
for (i=0; i < num_rounds; i++)
{
v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
sum += delta;
v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]);
}
v[0]=v0; v[1]=v1;
}
void
xtea_decipher(uint32_t v[2], uint32_t const key[4])
{
const unsigned int num_rounds = 64;
unsigned int i;
uint32_t v0=v[0], v1=v[1], delta=0x9E3779B9, sum=delta*num_rounds;
for (i=0; i < num_rounds; i++)
{
v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]);
sum -= delta;
v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
}
v[0]=v0; v[1]=v1;
}