-
Notifications
You must be signed in to change notification settings - Fork 1
/
cast6ecb.c
137 lines (111 loc) · 3.12 KB
/
cast6ecb.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <mcrypt.h>
#define CAST6ECB_ENCRYPT 0
#define CAST6ECB_DECRYPT 1
static MCRYPT td;
static PyObject *
mcrypt_do_cast6ecb(
const char *key,
const size_t klen,
const char *data,
const size_t dlen,
const short dencrypt
){
PyObject *ret;
char *mdata;
size_t mdlen, bsize;
if (mcrypt_generic_init(td, (void *)key, klen, NULL) < 0)
return PyErr_Format(PyExc_ValueError, "key is invalid");
bsize = mcrypt_enc_get_block_size(td);
mdlen = (((dlen - 1) / bsize) + 1) * bsize;
mdata = PyMem_Malloc(mdlen + 1);
if (mdata == NULL)
return PyErr_NoMemory();
memset(mdata, 0, mdlen);
memcpy(mdata, data, dlen);
if (dencrypt == CAST6ECB_ENCRYPT)
mcrypt_generic(td, mdata, (int)mdlen);
else
mdecrypt_generic(td, mdata, (int)mdlen);
mdata[mdlen] = 0;
ret = PyBytes_FromStringAndSize(mdata, mdlen);
PyMem_Free(mdata);
return ret;
}
static char
cast6ecb_block_size__doc__[] =
"block_size() -> blocksize\n\
\n\
This is the function that report the block size for CAST-256-ECB.\n\
";
static PyObject *
cast6ecb_block_size(
PyObject *self,
PyObject *args
){
return PyLong_FromLong(mcrypt_enc_get_block_size(td));
}
static char
cast6ecb_encrypt__doc__[] =
"encrypt(key, plaintext) -> secret\n\
\n\
This is the CAST-256-ECB encryption function.\n\
";
static PyObject *
cast6ecb_encrypt(
PyObject *self,
PyObject *args
){
char *key;
Py_ssize_t klen = 0;
char *plain;
Py_ssize_t plen = 0;
if (!PyArg_ParseTuple(args, "s#s#:encrypt", &key, &klen, &plain, &plen))
return NULL;
return mcrypt_do_cast6ecb(key, klen, plain, plen, CAST6ECB_ENCRYPT);
}
static char
cast6ecb_decrypt__doc__[] =
"decrypt(key, secret) -> plaintext\n\
\n\
This is the CAST-256-ECB decryption function.\n\
";
static PyObject *
cast6ecb_decrypt(
PyObject *self,
PyObject *args
){
char *key;
Py_ssize_t klen = 0;
char *secret;
Py_ssize_t slen = 0;
if (!PyArg_ParseTuple(args, "s#s#:decrypt", &key, &klen, &secret, &slen))
return NULL;
return mcrypt_do_cast6ecb(key, klen, secret, slen, CAST6ECB_DECRYPT);
}
static PyMethodDef
cast6ecb_methods[] = {
{"encrypt", (PyCFunction)cast6ecb_encrypt, METH_VARARGS, cast6ecb_encrypt__doc__},
{"decrypt", (PyCFunction)cast6ecb_decrypt, METH_VARARGS, cast6ecb_decrypt__doc__},
{"block_size", (PyCFunction)cast6ecb_block_size, METH_NOARGS, cast6ecb_block_size__doc__},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef
cast6ecb_module = {
PyModuleDef_HEAD_INIT,
"cast6ecb", /* name of module */
NULL, /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module,
or -1 if the module keeps state in global variables. */
cast6ecb_methods
};
PyMODINIT_FUNC
PyInit_cast6ecb(
void
){
td = mcrypt_module_open(MCRYPT_CAST_256, NULL, MCRYPT_ECB, NULL);
if (td == MCRYPT_FAILED)
return PyErr_Format(PyExc_RuntimeError, "could not open CAST-256-ECB module");
return PyModule_Create(&cast6ecb_module);
}