root/camellia/trunk/ccamellia.c

リビジョン 15, 5.0 kB (コミッタ: sgk, コミット時期: 3 年 前)

とりあえず、実装終わり。やっぱ、速いね。

Line 
1 #include <Python.h>
2 #include <string.h>
3 #include <assert.h>
4 #include "camellia.h"
5
6 /*
7  * ccamellia.c v 0.0
8  *
9  * http://omake.accense.com/wiki/PythonCamellia
10  *
11  * Copyright (c) 2006, Accense Technology, Inc.
12  * All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are met:
16  *
17  * o Redistributions of source code must retain the above copyright notice, this
18  *   list of conditions and the following disclaimer.
19  * o Redistributions in binary form must reproduce the above copyright notice,
20  *   this list of conditions and the following disclaimer in the documentation
21  *   and/or other materials provided with the distribution.
22  * o Neither the name of the Accense Technology, Inc. nor the names of its
23  *   contributors may be used to endorse or promote products derived from this
24  *   software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  *
38  */
39
40
41 /*
42  * def Ekeygen(rawKey):
43  *   return keyTable
44  *
45  * rawKey: 16 characters string
46  * keyTable: 204 or 272 characters string
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     /* raise ValueError, 'rawKey must be 16, 24 or 32 characters length.' */
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  * def EncryptBlock(plainText, keyTable):
79  *   return cipherText
80  *
81  * plainText: 16 characters string
82  * keyTable: 204 or 272 characters string
83  * cipherText: 16 characters string
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     /* keyBitLength 128 */
107     Camellia_EncryptBlock(128, plainText, keyTable, cipherText);
108     return o;
109
110   case 68*4:
111     /* keyBitLength 192 or 256; don't worry about '256' below. */
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  * def DecryptBlock(cipherText, keyTable):
125  *   return plainText
126  *
127  * cipherText: 16 characters string
128  * keyTable: 204 or 272 characters string
129  * plainText: 16 characters string
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     /* keyBitLength 128 */
153     Camellia_DecryptBlock(128, cipherText, keyTable, plainText);
154     return o;
155
156   case 68*4:
157     /* keyBitLength 192 or 256; don't worry about '256' below. */
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   /* XXX help string */
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 }
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。