-
Notifications
You must be signed in to change notification settings - Fork 4
/
xtea.c
73 lines (55 loc) · 1.98 KB
/
xtea.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
69
70
71
72
73
//#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <stdint.h>
#define DELTA (0x9E3779B9)
#define DEFAULT_CYCLES (32)
// Signature: *k[4], *k[2], num_cycles
static PyObject * xtea_encrypt_int(PyObject *self, PyObject *args, PyObject *kwargs) {
const uint32_t k[4];
uint32_t v0, v1, sum=0;
unsigned int num_cycles = DEFAULT_CYCLES;
unsigned int i;
if(!PyArg_ParseTuple(args, "(IIII)(II)|I",
&k[0], &k[1], &k[2], &k[3], &v0, &v1, &num_cycles)) return NULL;
for (i=0; i < num_cycles; i++) {
v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
sum += DELTA;
v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
}
PyObject *v0r = PyLong_FromUnsignedLongLong(v0);
PyObject *v1r = PyLong_FromUnsignedLongLong(v1);
return PyTuple_Pack(2, v0r, v1r);
};
static PyObject* xtea_decrypt_int(PyObject *self, PyObject *args) {
const uint32_t k[4];
uint32_t v0, v1, sum;
unsigned int num_cycles = DEFAULT_CYCLES;
unsigned int i;
if(!PyArg_ParseTuple(args, "(IIII)(II)|I",
&k[0], &k[1], &k[2], &k[3], &v0, &v1, &num_cycles)) return NULL;
sum = DELTA * num_cycles;
for (i=0; i < num_cycles; i++) {
v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
sum -= DELTA;
v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
}
PyObject *v0r = PyLong_FromUnsignedLong(v0);
PyObject *v1r = PyLong_FromUnsignedLong(v1);
return PyTuple_Pack(2, v0r, v1r);
};
static PyMethodDef XteaMethods[] = {
{"encrypt_int", (PyCFunction) xtea_encrypt_int, METH_VARARGS, "Encrypt a single xtea block."},
{"decrypt_int", (PyCFunction) xtea_decrypt_int, METH_VARARGS, "Decrypt a single xtea block."},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef xteamodule = {
PyModuleDef_HEAD_INIT,
"_xtea",
NULL,
-1,
XteaMethods
};
PyMODINIT_FUNC
PyInit__xtea() {
return PyModule_Create(&xteamodule);
}