| 1 |
#include <Python.h> |
|---|
| 2 |
#include <string.h> |
|---|
| 3 |
#include <assert.h> |
|---|
| 4 |
#include "camellia.h" |
|---|
| 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 |
static PyObject * |
|---|
| 49 |
Ekeygen(PyObject* self, PyObject* args) { |
|---|
| 50 |
const char* rawKey; |
|---|
| 51 |
int length; |
|---|
| 52 |
unsigned int keyTable[68]; |
|---|
| 53 |
|
|---|
| 54 |
if (!PyArg_ParseTuple(args, "s#", &rawKey, &length)) |
|---|
| 55 |
return 0; |
|---|
| 56 |
|
|---|
| 57 |
switch (length) { |
|---|
| 58 |
case 16: |
|---|
| 59 |
case 24: |
|---|
| 60 |
case 32: |
|---|
| 61 |
break; |
|---|
| 62 |
|
|---|
| 63 |
default: |
|---|
| 64 |
|
|---|
| 65 |
PyErr_SetString(PyExc_ValueError, |
|---|
| 66 |
"rawKey must be 16, 24 or 32 characters length."); |
|---|
| 67 |
return 0; |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
memset(keyTable, 0, sizeof (keyTable)); |
|---|
| 71 |
Camellia_Ekeygen(length * 8, rawKey, keyTable); |
|---|
| 72 |
return PyString_FromStringAndSize( |
|---|
| 73 |
(const char*)&keyTable[0], (length == 16 ? 52*4 : 68*4)); |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
static PyObject * |
|---|
| 86 |
EncryptBlock(PyObject* self, PyObject* args) { |
|---|
| 87 |
const char* plainText; |
|---|
| 88 |
const unsigned int* keyTable; |
|---|
| 89 |
int len1, len2; |
|---|
| 90 |
char* cipherText; |
|---|
| 91 |
PyObject* o; |
|---|
| 92 |
|
|---|
| 93 |
if (!PyArg_ParseTuple(args, "s#s#", &plainText, &len1, &keyTable, &len2)) |
|---|
| 94 |
return 0; |
|---|
| 95 |
if (len1 != 16) { |
|---|
| 96 |
PyErr_SetString(PyExc_ValueError, "plainText must be 16 characters"); |
|---|
| 97 |
return 0; |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
o = PyString_FromStringAndSize(0, 16); |
|---|
| 101 |
PyString_AsStringAndSize(o, &cipherText, &len1); |
|---|
| 102 |
assert(len1 == 16); |
|---|
| 103 |
|
|---|
| 104 |
switch (len2) { |
|---|
| 105 |
case 52*4: |
|---|
| 106 |
|
|---|
| 107 |
Camellia_EncryptBlock(128, plainText, keyTable, cipherText); |
|---|
| 108 |
return o; |
|---|
| 109 |
|
|---|
| 110 |
case 68*4: |
|---|
| 111 |
|
|---|
| 112 |
Camellia_EncryptBlock(256, plainText, keyTable, cipherText); |
|---|
| 113 |
return o; |
|---|
| 114 |
|
|---|
| 115 |
default: |
|---|
| 116 |
Py_DECREF(o); |
|---|
| 117 |
PyErr_SetString(PyExc_ValueError, "keyTable must be 52 or 68 characters"); |
|---|
| 118 |
return 0; |
|---|
| 119 |
} |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
static PyObject * |
|---|
| 132 |
DecryptBlock(PyObject* self, PyObject* args) { |
|---|
| 133 |
const char* cipherText; |
|---|
| 134 |
const unsigned int* keyTable; |
|---|
| 135 |
int len1, len2; |
|---|
| 136 |
char* plainText; |
|---|
| 137 |
PyObject* o; |
|---|
| 138 |
|
|---|
| 139 |
if (!PyArg_ParseTuple(args, "s#s#", &cipherText, &len1, &keyTable, &len2)) |
|---|
| 140 |
return 0; |
|---|
| 141 |
if (len1 != 16) { |
|---|
| 142 |
PyErr_SetString(PyExc_ValueError, "cipherText must be 16 characters"); |
|---|
| 143 |
return 0; |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
o = PyString_FromStringAndSize(0, 16); |
|---|
| 147 |
PyString_AsStringAndSize(o, &plainText, &len1); |
|---|
| 148 |
assert(len1 == 16); |
|---|
| 149 |
|
|---|
| 150 |
switch (len2) { |
|---|
| 151 |
case 52*4: |
|---|
| 152 |
|
|---|
| 153 |
Camellia_DecryptBlock(128, cipherText, keyTable, plainText); |
|---|
| 154 |
return o; |
|---|
| 155 |
|
|---|
| 156 |
case 68*4: |
|---|
| 157 |
|
|---|
| 158 |
Camellia_DecryptBlock(256, cipherText, keyTable, plainText); |
|---|
| 159 |
return o; |
|---|
| 160 |
|
|---|
| 161 |
default: |
|---|
| 162 |
Py_DECREF(o); |
|---|
| 163 |
PyErr_SetString(PyExc_ValueError, "keyTable must be 52 or 68 characters"); |
|---|
| 164 |
return 0; |
|---|
| 165 |
} |
|---|
| 166 |
} |
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
static PyMethodDef methodTable[] = { |
|---|
| 170 |
|
|---|
| 171 |
{ "Ekeygen", Ekeygen, METH_VARARGS, "Generate keyTable from key string." }, |
|---|
| 172 |
{ "EncryptBlock", EncryptBlock, METH_VARARGS, "EncryptBlock" }, |
|---|
| 173 |
{ "DecryptBlock", DecryptBlock, METH_VARARGS, "DecryptBlock" }, |
|---|
| 174 |
{ 0, 0, 0, 0 } |
|---|
| 175 |
}; |
|---|
| 176 |
|
|---|
| 177 |
PyMODINIT_FUNC |
|---|
| 178 |
initccamellia(void) { |
|---|
| 179 |
(void)Py_InitModule("ccamellia", methodTable); |
|---|
| 180 |
} |
|---|