Commit | Line | Data |
---|---|---|
d7ab1bab | 1 | /* -*-c-*- |
d7ab1bab | 2 | * |
3 | * Symmetric cryptography | |
4 | * | |
5 | * (c) 2004 Straylight/Edgeware | |
6 | */ | |
7 | ||
b2687a0a | 8 | /*----- Licensing notice --------------------------------------------------* |
d7ab1bab | 9 | * |
10 | * This file is part of the Python interface to Catacomb. | |
11 | * | |
12 | * Catacomb/Python is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU General Public License as published by | |
14 | * the Free Software Foundation; either version 2 of the License, or | |
15 | * (at your option) any later version. | |
b2687a0a | 16 | * |
d7ab1bab | 17 | * Catacomb/Python is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU General Public License for more details. | |
b2687a0a | 21 | * |
d7ab1bab | 22 | * You should have received a copy of the GNU General Public License |
23 | * along with Catacomb/Python; if not, write to the Free Software Foundation, | |
24 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
25 | */ | |
26 | ||
27 | /*----- Header files ------------------------------------------------------*/ | |
28 | ||
29 | #include "catacomb-python.h" | |
30 | #include "algorithms.h" | |
31 | ||
32 | /*----- Key sizes ---------------------------------------------------------*/ | |
33 | ||
34 | PyTypeObject *keysz_pytype; | |
35 | PyTypeObject *keyszany_pytype, *keyszrange_pytype, *keyszset_pytype; | |
36 | PyObject *sha_pyobj, *has160_pyobj; | |
37 | ||
38 | PyObject *keysz_pywrap(const octet *k) | |
39 | { | |
40 | switch (k[0]) { | |
41 | case KSZ_ANY: { | |
42 | keysz_pyobj *o = PyObject_New(keysz_pyobj, keyszany_pytype); | |
43 | o->dfl = k[1]; | |
44 | return ((PyObject *)o); | |
45 | } break; | |
46 | case KSZ_RANGE: { | |
47 | keyszrange_pyobj *o = | |
b2687a0a | 48 | PyObject_New(keyszrange_pyobj, keyszrange_pytype); |
d7ab1bab | 49 | o->dfl = k[1]; |
50 | o->min = k[2]; | |
51 | o->max = k[3]; | |
52 | o->mod = k[4]; | |
53 | if (!o->mod) o->mod = 1; | |
54 | return ((PyObject *)o); | |
55 | } break; | |
56 | case KSZ_SET: { | |
57 | keyszset_pyobj *o = | |
b2687a0a | 58 | PyObject_New(keyszset_pyobj, keyszset_pytype); |
d7ab1bab | 59 | int i, n; |
60 | o->dfl = k[1]; | |
61 | for (i = 0; k[i + 1]; i++) ; | |
62 | n = i; o->set = PyTuple_New(n); | |
63 | for (i = 0; i < n; i++) | |
64 | PyTuple_SET_ITEM(o->set, i, PyInt_FromLong(k[i + 1])); | |
65 | return ((PyObject *)o); | |
66 | } break; | |
67 | default: | |
68 | abort(); | |
69 | } | |
70 | } | |
71 | ||
72 | static PyObject *keyszany_pynew(PyTypeObject *ty, | |
73 | PyObject *arg, PyObject *kw) | |
74 | { | |
75 | char *kwlist[] = { "default", 0 }; | |
76 | int dfl; | |
77 | keysz_pyobj *o; | |
78 | ||
79 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "i:new", kwlist, &dfl)) | |
80 | goto end; | |
81 | if (dfl < 0) VALERR("key size cannot be negative"); | |
82 | o = (keysz_pyobj *)ty->tp_alloc(ty, 0); | |
83 | o->dfl = dfl; | |
84 | return ((PyObject *)o); | |
85 | end: | |
86 | return (0); | |
87 | } | |
88 | ||
89 | static PyObject *keyszrange_pynew(PyTypeObject *ty, | |
90 | PyObject *arg, PyObject *kw) | |
91 | { | |
92 | char *kwlist[] = { "default", "min", "max", "mod", 0 }; | |
93 | int dfl, min = 0, max = 0, mod = 1; | |
94 | keyszrange_pyobj *o; | |
95 | ||
96 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|iii:new", kwlist, | |
97 | &dfl, &min, &max, &mod)) | |
98 | goto end; | |
99 | if (dfl < 0 || min < 0 || max < 0) | |
100 | VALERR("key size cannot be negative"); | |
101 | if (min > dfl || (max && dfl > max)) | |
102 | VALERR("bad key size bounds"); | |
103 | if (mod <= 0 || dfl % mod || min % mod || max % mod) | |
104 | VALERR("bad key size modulus"); | |
105 | o = (keyszrange_pyobj *)ty->tp_alloc(ty, 0); | |
106 | o->dfl = dfl; | |
107 | o->min = min; | |
108 | o->max = max; | |
109 | o->mod = mod; | |
110 | return ((PyObject *)o); | |
111 | end: | |
112 | return (0); | |
113 | } | |
114 | ||
115 | static PyObject *keyszset_pynew(PyTypeObject *ty, | |
116 | PyObject *arg, PyObject *kw) | |
117 | { | |
118 | char *kwlist[] = { "default", "set", 0 }; | |
119 | int dfl, i, n, xx; | |
120 | PyObject *set = 0; | |
121 | PyObject *x = 0, *l = 0; | |
122 | keyszset_pyobj *o = 0; | |
123 | ||
124 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|O:new", kwlist, | |
125 | &dfl, &set)) | |
126 | goto end; | |
127 | if (!set) set = PyTuple_New(0); | |
128 | else Py_INCREF(set); | |
129 | if (!PySequence_Check(set)) TYERR("want a sequence"); | |
130 | n = PySequence_Size(set); | |
131 | l = PyList_New(0); | |
132 | if (PyErr_Occurred()) goto end; | |
133 | if (dfl < 0) VALERR("key size cannot be negative"); | |
134 | x = PyInt_FromLong(dfl); | |
135 | PyList_Append(l, x); | |
136 | Py_DECREF(x); | |
137 | x = 0; | |
138 | for (i = 0; i < n; i++) { | |
139 | if ((x = PySequence_GetItem(set, i)) == 0) goto end; | |
140 | xx = PyInt_AsLong(x); | |
141 | if (PyErr_Occurred()) goto end; | |
142 | if (xx == dfl) continue; | |
143 | if (xx < 0) VALERR("key size cannot be negative"); | |
144 | PyList_Append(l, x); | |
145 | Py_DECREF(x); | |
b2687a0a | 146 | x = 0; |
d7ab1bab | 147 | } |
148 | Py_DECREF(set); | |
149 | if ((set = PySequence_Tuple(l)) == 0) goto end; | |
150 | o = (keyszset_pyobj *)ty->tp_alloc(ty, 0); | |
151 | o->dfl = dfl; | |
152 | o->set = set; | |
153 | Py_INCREF(set); | |
154 | end: | |
155 | Py_XDECREF(set); | |
156 | Py_XDECREF(l); | |
157 | Py_XDECREF(x); | |
158 | return ((PyObject *)o); | |
159 | } | |
160 | ||
161 | static PyObject *kaget_min(PyObject *me, void *hunoz) | |
162 | { return (PyInt_FromLong(0)); } | |
163 | #define kaget_max kaget_min | |
164 | ||
165 | static PyObject *ksget_min(PyObject *me, void *hunoz) | |
166 | { | |
167 | PyObject *set = ((keyszset_pyobj *)me)->set; | |
168 | int i, n, y, x = -1; | |
169 | n = PyTuple_Size(set); | |
170 | for (i = 0; i < n; i++) { | |
171 | y = PyInt_AsLong(PyTuple_GetItem(set, i)); | |
172 | if (x == -1 || y < x) x = y; | |
173 | } | |
174 | return (PyInt_FromLong(x)); | |
175 | } | |
176 | ||
177 | static PyObject *ksget_max(PyObject *me, void *hunoz) | |
178 | { | |
179 | PyObject *set = ((keyszset_pyobj *)me)->set; | |
180 | int i, n, y, x = -1; | |
181 | n = PyTuple_Size(set); | |
182 | for (i = 0; i < n; i++) { | |
183 | y = PyInt_AsLong(PyTuple_GetItem(set, i)); | |
184 | if (y > x) x = y; | |
185 | } | |
186 | return (PyInt_FromLong(x)); | |
187 | } | |
188 | ||
189 | static PyMemberDef keysz_pymembers[] = { | |
190 | #define MEMBERSTRUCT keysz_pyobj | |
191 | #define default dfl /* ugh! */ | |
192 | MEMBER(default, T_INT, READONLY, "KSZ.default -> default key size") | |
193 | #undef default | |
194 | #undef MEMBERSTRUCT | |
195 | { 0 } | |
196 | }; | |
197 | ||
198 | static PyGetSetDef keyszany_pygetset[] = { | |
199 | #define GETSETNAME(op, name) ka##op##_##name | |
200 | GET (min, "KSZ.min -> smallest allowed key size") | |
201 | GET (max, "KSZ.min -> largest allowed key size") | |
202 | #undef GETSETNAME | |
203 | { 0 } | |
204 | }; | |
205 | ||
206 | static PyMemberDef keyszrange_pymembers[] = { | |
207 | #define MEMBERSTRUCT keyszrange_pyobj | |
208 | MEMBER(min, T_INT, READONLY, "KSZ.min -> smallest allowed key size") | |
209 | MEMBER(max, T_INT, READONLY, "KSZ.min -> largest allowed key size") | |
210 | MEMBER(mod, T_INT, READONLY, | |
211 | "KSZ.mod -> key size must be a multiple of this") | |
212 | #undef MEMBERSTRUCT | |
213 | { 0 } | |
214 | }; | |
215 | ||
216 | static PyGetSetDef keyszset_pygetset[] = { | |
217 | #define GETSETNAME(op, name) ks##op##_##name | |
218 | GET (min, "KSZ.min -> smallest allowed key size") | |
219 | GET (max, "KSZ.min -> largest allowed key size") | |
220 | #undef GETSETNAME | |
221 | { 0 } | |
222 | }; | |
223 | ||
224 | static PyMemberDef keyszset_pymembers[] = { | |
225 | #define MEMBERSTRUCT keyszset_pyobj | |
226 | MEMBER(set, T_OBJECT, READONLY, "KSZ.set -> allowed key sizes") | |
227 | #undef MEMBERSTRUCT | |
228 | { 0 } | |
229 | }; | |
230 | ||
231 | static PyTypeObject keysz_pytype_skel = { | |
6d4db0bf | 232 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 233 | "KeySZ", /* @tp_name@ */ |
d7ab1bab | 234 | sizeof(keysz_pyobj), /* @tp_basicsize@ */ |
235 | 0, /* @tp_itemsize@ */ | |
236 | ||
3aa33042 | 237 | 0, /* @tp_dealloc@ */ |
d7ab1bab | 238 | 0, /* @tp_print@ */ |
239 | 0, /* @tp_getattr@ */ | |
240 | 0, /* @tp_setattr@ */ | |
241 | 0, /* @tp_compare@ */ | |
242 | 0, /* @tp_repr@ */ | |
243 | 0, /* @tp_as_number@ */ | |
244 | 0, /* @tp_as_sequence@ */ | |
245 | 0, /* @tp_as_mapping@ */ | |
246 | 0, /* @tp_hash@ */ | |
247 | 0, /* @tp_call@ */ | |
248 | 0, /* @tp_str@ */ | |
249 | 0, /* @tp_getattro@ */ | |
250 | 0, /* @tp_setattro@ */ | |
251 | 0, /* @tp_as_buffer@ */ | |
252 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
253 | Py_TPFLAGS_BASETYPE, | |
254 | ||
255 | /* @tp_doc@ */ | |
256 | "Key size constraints.", | |
257 | ||
258 | 0, /* @tp_traverse@ */ | |
259 | 0, /* @tp_clear@ */ | |
260 | 0, /* @tp_richcompare@ */ | |
261 | 0, /* @tp_weaklistoffset@ */ | |
262 | 0, /* @tp_iter@ */ | |
963a6148 | 263 | 0, /* @tp_iternext@ */ |
d7ab1bab | 264 | 0, /* @tp_methods@ */ |
265 | keysz_pymembers, /* @tp_members@ */ | |
266 | 0, /* @tp_getset@ */ | |
267 | 0, /* @tp_base@ */ | |
268 | 0, /* @tp_dict@ */ | |
269 | 0, /* @tp_descr_get@ */ | |
270 | 0, /* @tp_descr_set@ */ | |
271 | 0, /* @tp_dictoffset@ */ | |
272 | 0, /* @tp_init@ */ | |
273 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
274 | abstract_pynew, /* @tp_new@ */ | |
3aa33042 | 275 | 0, /* @tp_free@ */ |
d7ab1bab | 276 | 0 /* @tp_is_gc@ */ |
277 | }; | |
278 | ||
279 | static PyTypeObject keyszany_pytype_skel = { | |
6d4db0bf | 280 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 281 | "KeySZAny", /* @tp_name@ */ |
d7ab1bab | 282 | sizeof(keysz_pyobj), /* @tp_basicsize@ */ |
283 | 0, /* @tp_itemsize@ */ | |
284 | ||
3aa33042 | 285 | 0, /* @tp_dealloc@ */ |
d7ab1bab | 286 | 0, /* @tp_print@ */ |
287 | 0, /* @tp_getattr@ */ | |
288 | 0, /* @tp_setattr@ */ | |
289 | 0, /* @tp_compare@ */ | |
290 | 0, /* @tp_repr@ */ | |
291 | 0, /* @tp_as_number@ */ | |
292 | 0, /* @tp_as_sequence@ */ | |
293 | 0, /* @tp_as_mapping@ */ | |
294 | 0, /* @tp_hash@ */ | |
295 | 0, /* @tp_call@ */ | |
296 | 0, /* @tp_str@ */ | |
297 | 0, /* @tp_getattro@ */ | |
298 | 0, /* @tp_setattro@ */ | |
299 | 0, /* @tp_as_buffer@ */ | |
300 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
301 | Py_TPFLAGS_BASETYPE, | |
302 | ||
303 | /* @tp_doc@ */ | |
304 | "Key size constraints. This object imposes no constraints on size.", | |
305 | ||
306 | 0, /* @tp_traverse@ */ | |
307 | 0, /* @tp_clear@ */ | |
308 | 0, /* @tp_richcompare@ */ | |
309 | 0, /* @tp_weaklistoffset@ */ | |
310 | 0, /* @tp_iter@ */ | |
963a6148 | 311 | 0, /* @tp_iternext@ */ |
d7ab1bab | 312 | 0, /* @tp_methods@ */ |
313 | 0, /* @tp_members@ */ | |
314 | keyszany_pygetset, /* @tp_getset@ */ | |
315 | 0, /* @tp_base@ */ | |
316 | 0, /* @tp_dict@ */ | |
317 | 0, /* @tp_descr_get@ */ | |
318 | 0, /* @tp_descr_set@ */ | |
319 | 0, /* @tp_dictoffset@ */ | |
320 | 0, /* @tp_init@ */ | |
321 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
322 | keyszany_pynew, /* @tp_new@ */ | |
3aa33042 | 323 | 0, /* @tp_free@ */ |
d7ab1bab | 324 | 0 /* @tp_is_gc@ */ |
325 | }; | |
326 | ||
327 | static PyTypeObject keyszrange_pytype_skel = { | |
6d4db0bf | 328 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 329 | "KeySZRange", /* @tp_name@ */ |
d7ab1bab | 330 | sizeof(keyszrange_pyobj), /* @tp_basicsize@ */ |
331 | 0, /* @tp_itemsize@ */ | |
332 | ||
3aa33042 | 333 | 0, /* @tp_dealloc@ */ |
d7ab1bab | 334 | 0, /* @tp_print@ */ |
335 | 0, /* @tp_getattr@ */ | |
336 | 0, /* @tp_setattr@ */ | |
337 | 0, /* @tp_compare@ */ | |
338 | 0, /* @tp_repr@ */ | |
339 | 0, /* @tp_as_number@ */ | |
340 | 0, /* @tp_as_sequence@ */ | |
341 | 0, /* @tp_as_mapping@ */ | |
342 | 0, /* @tp_hash@ */ | |
343 | 0, /* @tp_call@ */ | |
344 | 0, /* @tp_str@ */ | |
345 | 0, /* @tp_getattro@ */ | |
346 | 0, /* @tp_setattro@ */ | |
347 | 0, /* @tp_as_buffer@ */ | |
348 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
349 | Py_TPFLAGS_BASETYPE, | |
350 | ||
351 | /* @tp_doc@ */ | |
352 | "Key size constraints. This object asserts minimum and maximum (if\n\ | |
353 | sizes, and requires the key length to be a multiple of some value.", | |
354 | ||
355 | 0, /* @tp_traverse@ */ | |
356 | 0, /* @tp_clear@ */ | |
357 | 0, /* @tp_richcompare@ */ | |
358 | 0, /* @tp_weaklistoffset@ */ | |
359 | 0, /* @tp_iter@ */ | |
963a6148 | 360 | 0, /* @tp_iternext@ */ |
d7ab1bab | 361 | 0, /* @tp_methods@ */ |
362 | keyszrange_pymembers, /* @tp_members@ */ | |
363 | 0, /* @tp_getset@ */ | |
364 | 0, /* @tp_base@ */ | |
365 | 0, /* @tp_dict@ */ | |
366 | 0, /* @tp_descr_get@ */ | |
367 | 0, /* @tp_descr_set@ */ | |
368 | 0, /* @tp_dictoffset@ */ | |
369 | 0, /* @tp_init@ */ | |
370 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
371 | keyszrange_pynew, /* @tp_new@ */ | |
3aa33042 | 372 | 0, /* @tp_free@ */ |
d7ab1bab | 373 | 0 /* @tp_is_gc@ */ |
374 | }; | |
375 | ||
376 | static PyTypeObject keyszset_pytype_skel = { | |
6d4db0bf | 377 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 378 | "KeySZSet", /* @tp_name@ */ |
d7ab1bab | 379 | sizeof(keyszset_pyobj), /* @tp_basicsize@ */ |
380 | 0, /* @tp_itemsize@ */ | |
381 | ||
3aa33042 | 382 | 0, /* @tp_dealloc@ */ |
d7ab1bab | 383 | 0, /* @tp_print@ */ |
384 | 0, /* @tp_getattr@ */ | |
385 | 0, /* @tp_setattr@ */ | |
386 | 0, /* @tp_compare@ */ | |
387 | 0, /* @tp_repr@ */ | |
388 | 0, /* @tp_as_number@ */ | |
389 | 0, /* @tp_as_sequence@ */ | |
390 | 0, /* @tp_as_mapping@ */ | |
391 | 0, /* @tp_hash@ */ | |
392 | 0, /* @tp_call@ */ | |
393 | 0, /* @tp_str@ */ | |
394 | 0, /* @tp_getattro@ */ | |
395 | 0, /* @tp_setattro@ */ | |
396 | 0, /* @tp_as_buffer@ */ | |
397 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
398 | Py_TPFLAGS_BASETYPE, | |
399 | ||
400 | /* @tp_doc@ */ | |
401 | "Key size constraints. This object requires the key to be one of a\n\ | |
402 | few listed sizes.", | |
403 | ||
404 | 0, /* @tp_traverse@ */ | |
405 | 0, /* @tp_clear@ */ | |
406 | 0, /* @tp_richcompare@ */ | |
407 | 0, /* @tp_weaklistoffset@ */ | |
408 | 0, /* @tp_iter@ */ | |
963a6148 | 409 | 0, /* @tp_iternext@ */ |
d7ab1bab | 410 | 0, /* @tp_methods@ */ |
411 | keyszset_pymembers, /* @tp_members@ */ | |
412 | keyszset_pygetset, /* @tp_getset@ */ | |
413 | 0, /* @tp_base@ */ | |
414 | 0, /* @tp_dict@ */ | |
415 | 0, /* @tp_descr_get@ */ | |
416 | 0, /* @tp_descr_set@ */ | |
417 | 0, /* @tp_dictoffset@ */ | |
418 | 0, /* @tp_init@ */ | |
419 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
420 | keyszset_pynew, /* @tp_new@ */ | |
3aa33042 | 421 | 0, /* @tp_free@ */ |
d7ab1bab | 422 | 0 /* @tp_is_gc@ */ |
423 | }; | |
424 | ||
89157adc MW |
425 | #define KSZCONVOP(op) \ |
426 | static PyObject *meth__KeySZ_##op(PyObject *me, PyObject *arg) \ | |
427 | { \ | |
428 | double x, y; \ | |
429 | if (!PyArg_ParseTuple(arg, "Od:" #op, &me, &x)) return (0); \ | |
430 | y = keysz_##op(x); \ | |
431 | return (PyFloat_FromDouble(y)); \ | |
432 | } | |
433 | KSZCONVOP(fromdl) | |
434 | KSZCONVOP(fromschnorr) | |
435 | KSZCONVOP(fromif) | |
436 | KSZCONVOP(fromec) | |
437 | KSZCONVOP(todl) | |
438 | KSZCONVOP(toschnorr) | |
439 | KSZCONVOP(toif) | |
440 | KSZCONVOP(toec) | |
441 | #undef KSZCONVOP | |
442 | ||
d7ab1bab | 443 | /*----- Symmetric encryption ----------------------------------------------*/ |
444 | ||
445 | PyTypeObject *gccipher_pytype, *gcipher_pytype; | |
446 | ||
447 | CONVFUNC(gccipher, gccipher *, GCCIPHER_CC) | |
448 | CONVFUNC(gcipher, gcipher *, GCIPHER_C) | |
449 | ||
450 | PyObject *gcipher_pywrap(PyObject *cobj, gcipher *c, unsigned f) | |
451 | { | |
452 | gcipher_pyobj *g; | |
453 | if (!cobj) cobj = gccipher_pywrap((/*unconst*/ gccipher *)GC_CLASS(c)); | |
454 | else Py_INCREF(cobj); | |
455 | g = PyObject_NEW(gcipher_pyobj, (PyTypeObject *)cobj); | |
456 | g->c = c; | |
457 | g->f = f; | |
b2687a0a | 458 | return ((PyObject *)g); |
d7ab1bab | 459 | } |
460 | ||
461 | static PyObject *gcipher_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
462 | { | |
463 | char *kwlist[] = { "k", 0 }; | |
464 | char *k; | |
465 | int sz; | |
466 | ||
467 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz)) | |
468 | goto end; | |
469 | if (keysz(sz, GCCIPHER_CC(ty)->keysz) != sz) VALERR("bad key length"); | |
470 | return (gcipher_pywrap((PyObject *)ty, | |
471 | GC_INIT(GCCIPHER_CC(ty), k, sz), | |
472 | f_freeme)); | |
473 | end: | |
b2687a0a | 474 | return (0); |
d7ab1bab | 475 | } |
476 | ||
477 | PyObject *gccipher_pywrap(gccipher *cc) | |
478 | { | |
df9f8366 | 479 | gccipher_pyobj *g = newtype(gccipher_pytype, 0, cc->name); |
d7ab1bab | 480 | g->cc = cc; |
24b3d57b MW |
481 | g->ty.ht_type.tp_basicsize = sizeof(gcipher_pyobj); |
482 | g->ty.ht_type.tp_base = gcipher_pytype; | |
d7ab1bab | 483 | Py_INCREF(gcipher_pytype); |
24b3d57b MW |
484 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
485 | Py_TPFLAGS_BASETYPE | | |
486 | Py_TPFLAGS_HEAPTYPE); | |
487 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; | |
488 | g->ty.ht_type.tp_free = 0; | |
489 | g->ty.ht_type.tp_new = gcipher_pynew; | |
dc075750 | 490 | typeready(&g->ty.ht_type); |
d7ab1bab | 491 | return ((PyObject *)g); |
492 | } | |
493 | ||
494 | static void gcipher_pydealloc(PyObject *me) | |
495 | { | |
496 | if (GCIPHER_F(me) & f_freeme) | |
497 | GC_DESTROY(GCIPHER_C(me)); | |
498 | Py_DECREF(me->ob_type); | |
3aa33042 | 499 | FREEOBJ(me); |
d7ab1bab | 500 | } |
501 | ||
502 | static PyObject *gccget_name(PyObject *me, void *hunoz) | |
503 | { return (PyString_FromString(GCCIPHER_CC(me)->name)); } | |
504 | ||
505 | static PyObject *gccget_keysz(PyObject *me, void *hunoz) | |
506 | { return (keysz_pywrap(GCCIPHER_CC(me)->keysz)); } | |
507 | ||
508 | static PyObject *gccget_blksz(PyObject *me, void *hunoz) | |
509 | { return (PyInt_FromLong(GCCIPHER_CC(me)->blksz)); } | |
510 | ||
511 | static PyObject *gcmeth_encrypt(PyObject *me, PyObject *arg) | |
512 | { | |
513 | char *p; | |
514 | int sz; | |
515 | PyObject *rc = 0; | |
516 | ||
517 | if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &sz)) return (0); | |
518 | rc = bytestring_pywrap(0, sz); | |
519 | GC_ENCRYPT(GCIPHER_C(me), p, PyString_AS_STRING(rc), sz); | |
520 | return (rc); | |
521 | } | |
522 | ||
523 | static PyObject *gcmeth_enczero(PyObject *me, PyObject *arg) | |
524 | { | |
525 | char *p; | |
526 | int sz; | |
527 | PyObject *rc = 0; | |
528 | ||
529 | if (!PyArg_ParseTuple(arg, "i:enczero", &sz)) return (0); | |
530 | rc = bytestring_pywrap(0, sz); | |
531 | p = PyString_AS_STRING(rc); | |
532 | memset(p, 0, sz); | |
533 | GC_ENCRYPT(GCIPHER_C(me), p, p, sz); | |
534 | return (rc); | |
535 | } | |
536 | ||
537 | static PyObject *gcmeth_decrypt(PyObject *me, PyObject *arg) | |
538 | { | |
539 | char *p; | |
540 | int sz; | |
541 | PyObject *rc = 0; | |
542 | ||
543 | if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &sz)) return (0); | |
544 | rc = bytestring_pywrap(0, sz); | |
545 | GC_DECRYPT(GCIPHER_C(me), p, PyString_AS_STRING(rc), sz); | |
546 | return (rc); | |
547 | } | |
548 | ||
549 | static PyObject *gcmeth_deczero(PyObject *me, PyObject *arg) | |
550 | { | |
551 | char *p; | |
552 | int sz; | |
553 | PyObject *rc = 0; | |
554 | ||
555 | if (!PyArg_ParseTuple(arg, "i:deczero", &sz)) return (0); | |
556 | rc = bytestring_pywrap(0, sz); | |
557 | p = PyString_AS_STRING(rc); | |
558 | memset(p, 0, sz); | |
559 | GC_DECRYPT(GCIPHER_C(me), p, p, sz); | |
560 | return (rc); | |
561 | } | |
562 | ||
563 | static PyObject *gcmeth_setiv(PyObject *me, PyObject *arg) | |
564 | { | |
565 | char *p; | |
566 | int sz; | |
567 | ||
568 | if (!PyArg_ParseTuple(arg, "s#:setiv", &p, &sz)) goto end; | |
569 | if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode"); | |
570 | if (sz != GC_CLASS(GCIPHER_C(me))->blksz) VALERR("bad IV length"); | |
571 | GC_SETIV(GCIPHER_C(me), p); | |
572 | RETURN_ME; | |
573 | end: | |
574 | return (0); | |
575 | } | |
576 | ||
577 | static PyObject *gcmeth_bdry(PyObject *me, PyObject *arg) | |
578 | { | |
579 | if (!PyArg_ParseTuple(arg, ":bdry")) goto end; | |
580 | if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode"); | |
581 | GC_BDRY(GCIPHER_C(me)); | |
582 | RETURN_ME; | |
583 | end: | |
584 | return (0); | |
585 | } | |
586 | ||
587 | static PyGetSetDef gccipher_pygetset[] = { | |
588 | #define GETSETNAME(op, name) gcc##op##_##name | |
589 | GET (keysz, "CC.keysz -> acceptable key sizes") | |
590 | GET (blksz, "CC.blksz -> block size, or zero") | |
591 | GET (name, "CC.name -> name of this kind of cipher") | |
592 | #undef GETSETNAME | |
593 | { 0 } | |
594 | }; | |
595 | ||
596 | static PyMethodDef gcipher_pymethods[] = { | |
597 | #define METHNAME(name) gcmeth_##name | |
598 | METH (encrypt, "C.encrypt(PT) -> CT") | |
599 | METH (enczero, "C.enczero(N) -> CT") | |
600 | METH (decrypt, "C.decrypt(CT) -> PT") | |
601 | METH (deczero, "C.deczero(N) -> PT") | |
602 | METH (setiv, "C.setiv(IV)") | |
603 | METH (bdry, "C.bdry()") | |
604 | #undef METHNAME | |
605 | { 0 } | |
606 | }; | |
607 | ||
608 | static PyTypeObject gccipher_pytype_skel = { | |
6d4db0bf | 609 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 610 | "GCCipher", /* @tp_name@ */ |
d7ab1bab | 611 | sizeof(gccipher_pyobj), /* @tp_basicsize@ */ |
612 | 0, /* @tp_itemsize@ */ | |
613 | ||
614 | 0, /* @tp_dealloc@ */ | |
615 | 0, /* @tp_print@ */ | |
616 | 0, /* @tp_getattr@ */ | |
617 | 0, /* @tp_setattr@ */ | |
618 | 0, /* @tp_compare@ */ | |
619 | 0, /* @tp_repr@ */ | |
620 | 0, /* @tp_as_number@ */ | |
621 | 0, /* @tp_as_sequence@ */ | |
622 | 0, /* @tp_as_mapping@ */ | |
623 | 0, /* @tp_hash@ */ | |
624 | 0, /* @tp_call@ */ | |
625 | 0, /* @tp_str@ */ | |
626 | 0, /* @tp_getattro@ */ | |
627 | 0, /* @tp_setattro@ */ | |
628 | 0, /* @tp_as_buffer@ */ | |
629 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
630 | Py_TPFLAGS_BASETYPE, | |
631 | ||
632 | /* @tp_doc@ */ | |
633 | "Symmetric cipher metaclass.", | |
634 | ||
635 | 0, /* @tp_traverse@ */ | |
636 | 0, /* @tp_clear@ */ | |
637 | 0, /* @tp_richcompare@ */ | |
638 | 0, /* @tp_weaklistoffset@ */ | |
639 | 0, /* @tp_iter@ */ | |
963a6148 | 640 | 0, /* @tp_iternext@ */ |
d7ab1bab | 641 | 0, /* @tp_methods@ */ |
642 | 0, /* @tp_members@ */ | |
643 | gccipher_pygetset, /* @tp_getset@ */ | |
644 | 0, /* @tp_base@ */ | |
645 | 0, /* @tp_dict@ */ | |
646 | 0, /* @tp_descr_get@ */ | |
647 | 0, /* @tp_descr_set@ */ | |
648 | 0, /* @tp_dictoffset@ */ | |
649 | 0, /* @tp_init@ */ | |
650 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
651 | abstract_pynew, /* @tp_new@ */ | |
3aa33042 | 652 | 0, /* @tp_free@ */ |
d7ab1bab | 653 | 0 /* @tp_is_gc@ */ |
654 | }; | |
655 | ||
656 | static PyTypeObject gcipher_pytype_skel = { | |
6d4db0bf | 657 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 658 | "GCipher", /* @tp_name@ */ |
d7ab1bab | 659 | sizeof(gcipher_pyobj), /* @tp_basicsize@ */ |
660 | 0, /* @tp_itemsize@ */ | |
661 | ||
662 | gcipher_pydealloc, /* @tp_dealloc@ */ | |
663 | 0, /* @tp_print@ */ | |
664 | 0, /* @tp_getattr@ */ | |
665 | 0, /* @tp_setattr@ */ | |
666 | 0, /* @tp_compare@ */ | |
667 | 0, /* @tp_repr@ */ | |
668 | 0, /* @tp_as_number@ */ | |
669 | 0, /* @tp_as_sequence@ */ | |
670 | 0, /* @tp_as_mapping@ */ | |
671 | 0, /* @tp_hash@ */ | |
672 | 0, /* @tp_call@ */ | |
673 | 0, /* @tp_str@ */ | |
674 | 0, /* @tp_getattro@ */ | |
675 | 0, /* @tp_setattro@ */ | |
676 | 0, /* @tp_as_buffer@ */ | |
677 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
678 | Py_TPFLAGS_BASETYPE, | |
679 | ||
680 | /* @tp_doc@ */ | |
681 | "Symmetric cipher, abstract base class.", | |
682 | ||
683 | 0, /* @tp_traverse@ */ | |
684 | 0, /* @tp_clear@ */ | |
685 | 0, /* @tp_richcompare@ */ | |
686 | 0, /* @tp_weaklistoffset@ */ | |
687 | 0, /* @tp_iter@ */ | |
963a6148 | 688 | 0, /* @tp_iternext@ */ |
d7ab1bab | 689 | gcipher_pymethods, /* @tp_methods@ */ |
690 | 0, /* @tp_members@ */ | |
691 | 0, /* @tp_getset@ */ | |
692 | 0, /* @tp_base@ */ | |
693 | 0, /* @tp_dict@ */ | |
694 | 0, /* @tp_descr_get@ */ | |
695 | 0, /* @tp_descr_set@ */ | |
696 | 0, /* @tp_dictoffset@ */ | |
697 | 0, /* @tp_init@ */ | |
698 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
699 | abstract_pynew, /* @tp_new@ */ | |
3aa33042 | 700 | 0, /* @tp_free@ */ |
d7ab1bab | 701 | 0 /* @tp_is_gc@ */ |
702 | }; | |
703 | ||
704 | /*----- Hash functions ----------------------------------------------------*/ | |
705 | ||
706 | PyTypeObject *gchash_pytype, *ghash_pytype; | |
707 | ||
708 | CONVFUNC(gchash, gchash *, GCHASH_CH) | |
709 | CONVFUNC(ghash, ghash *, GHASH_H) | |
710 | ||
711 | static PyObject *ghash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
712 | { | |
713 | char *kwlist[] = { 0 }; | |
714 | if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", kwlist)) | |
715 | goto end; | |
716 | return (ghash_pywrap((PyObject *)ty, GH_INIT(GCHASH_CH(ty)), f_freeme)); | |
717 | end: | |
b2687a0a | 718 | return (0); |
d7ab1bab | 719 | } |
720 | ||
721 | PyObject *gchash_pywrap(gchash *ch) | |
722 | { | |
df9f8366 | 723 | gchash_pyobj *g = newtype(gchash_pytype, 0, ch->name); |
d7ab1bab | 724 | g->ch = ch; |
24b3d57b MW |
725 | g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj); |
726 | g->ty.ht_type.tp_base = ghash_pytype; | |
d7ab1bab | 727 | Py_INCREF(ghash_pytype); |
24b3d57b MW |
728 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
729 | Py_TPFLAGS_BASETYPE | | |
730 | Py_TPFLAGS_HEAPTYPE); | |
731 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; | |
732 | g->ty.ht_type.tp_free = 0; | |
733 | g->ty.ht_type.tp_new = ghash_pynew; | |
dc075750 | 734 | typeready(&g->ty.ht_type); |
d7ab1bab | 735 | return ((PyObject *)g); |
736 | } | |
737 | ||
738 | PyObject *ghash_pywrap(PyObject *cobj, ghash *h, unsigned f) | |
739 | { | |
740 | ghash_pyobj *g; | |
741 | if (!cobj) cobj = gchash_pywrap((/*unconst*/ gchash *)GH_CLASS(h)); | |
742 | else Py_INCREF(cobj); | |
743 | g = PyObject_NEW(ghash_pyobj, (PyTypeObject *)cobj); | |
744 | g->h = h; | |
745 | g->f = f; | |
b2687a0a | 746 | return ((PyObject *)g); |
d7ab1bab | 747 | } |
748 | ||
749 | static void ghash_pydealloc(PyObject *me) | |
750 | { | |
751 | if (GHASH_F(me) & f_freeme) | |
752 | GH_DESTROY(GHASH_H(me)); | |
753 | Py_DECREF(me->ob_type); | |
3aa33042 | 754 | FREEOBJ(me); |
d7ab1bab | 755 | } |
756 | ||
757 | static PyObject *gchget_name(PyObject *me, void *hunoz) | |
758 | { return (PyString_FromString(GCHASH_CH(me)->name)); } | |
759 | ||
760 | static PyObject *gchget_hashsz(PyObject *me, void *hunoz) | |
761 | { return (PyInt_FromLong(GCHASH_CH(me)->hashsz)); } | |
762 | ||
763 | static PyObject *gchget_bufsz(PyObject *me, void *hunoz) | |
764 | { return (PyInt_FromLong(GCHASH_CH(me)->bufsz)); } | |
765 | ||
766 | static PyObject *ghmeth_hash(PyObject *me, PyObject *arg) | |
767 | { | |
768 | char *p; | |
769 | int sz; | |
770 | if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0); | |
771 | GH_HASH(GHASH_H(me), p, sz); | |
772 | RETURN_ME; | |
773 | } | |
774 | ||
775 | static PyObject *ghmeth_done(PyObject *me, PyObject *arg) | |
776 | { | |
777 | ghash *g; | |
778 | PyObject *rc; | |
779 | if (!PyArg_ParseTuple(arg, ":done")) return (0); | |
780 | g = GH_COPY(GHASH_H(me)); | |
781 | rc = bytestring_pywrap(0, g->ops->c->hashsz); | |
782 | GH_DONE(g, PyString_AS_STRING(rc)); | |
783 | GH_DESTROY(g); | |
784 | return (rc); | |
785 | } | |
786 | ||
787 | static PyGetSetDef gchash_pygetset[] = { | |
788 | #define GETSETNAME(op, name) gch##op##_##name | |
789 | GET (bufsz, "CH.bufsz -> hash buffer size, or zero") | |
790 | GET (hashsz, "CH.blksz -> hash output size") | |
791 | GET (name, "CH.name -> name of this kind of hash") | |
792 | #undef GETSETNAME | |
793 | { 0 } | |
794 | }; | |
795 | ||
46e6ad89 | 796 | #define GHMETH_HASHU_(n, W, w) \ |
797 | static PyObject *ghmeth_hashu##w(PyObject *me, PyObject *arg) \ | |
798 | { \ | |
799 | uint##n x; \ | |
800 | if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \ | |
801 | GH_HASHU##W(GHASH_H(me), x); \ | |
802 | RETURN_ME; \ | |
803 | end: \ | |
804 | return (0); \ | |
805 | } | |
806 | DOUINTCONV(GHMETH_HASHU_) | |
807 | ||
808 | #define GHMETH_HASHBUF_(n, W, w) \ | |
809 | static PyObject *ghmeth_hashbuf##w(PyObject *me, PyObject *arg) \ | |
810 | { \ | |
811 | char *p; \ | |
812 | int sz; \ | |
813 | if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \ | |
814 | if (sz > MASK##n) TYERR("string too long"); \ | |
815 | GH_HASHBUF##W(GHASH_H(me), p, sz); \ | |
816 | RETURN_ME; \ | |
817 | end: \ | |
818 | return (0); \ | |
819 | } | |
820 | DOUINTCONV(GHMETH_HASHBUF_) | |
821 | ||
822 | static PyObject *ghmeth_hashstrz(PyObject *me, PyObject *arg) | |
823 | { | |
824 | char *p; | |
825 | if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0); | |
826 | GH_HASHSTRZ(GHASH_H(me), p); | |
827 | RETURN_ME; | |
828 | } | |
829 | ||
d7ab1bab | 830 | static PyMethodDef ghash_pymethods[] = { |
831 | #define METHNAME(name) ghmeth_##name | |
832 | METH (hash, "H.hash(M)") | |
46e6ad89 | 833 | #define METHU_(n, W, w) METH(hashu##w, "H.hashu" #w "(WORD)") |
834 | DOUINTCONV(METHU_) | |
835 | #define METHBUF_(n, W, w) METH(hashbuf##w, "H.hashbuf" #w "(BYTES)") | |
836 | DOUINTCONV(METHBUF_) | |
837 | METH (hashstrz, "H.hashstrz(STRING)") | |
d7ab1bab | 838 | METH (done, "H.done() -> HASH") |
839 | #undef METHNAME | |
840 | { 0 } | |
841 | }; | |
842 | ||
843 | static PyTypeObject gchash_pytype_skel = { | |
6d4db0bf | 844 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 845 | "GCHash", /* @tp_name@ */ |
d7ab1bab | 846 | sizeof(gchash_pyobj), /* @tp_basicsize@ */ |
847 | 0, /* @tp_itemsize@ */ | |
848 | ||
849 | 0, /* @tp_dealloc@ */ | |
850 | 0, /* @tp_print@ */ | |
851 | 0, /* @tp_getattr@ */ | |
852 | 0, /* @tp_setattr@ */ | |
853 | 0, /* @tp_compare@ */ | |
854 | 0, /* @tp_repr@ */ | |
855 | 0, /* @tp_as_number@ */ | |
856 | 0, /* @tp_as_sequence@ */ | |
857 | 0, /* @tp_as_mapping@ */ | |
858 | 0, /* @tp_hash@ */ | |
859 | 0, /* @tp_call@ */ | |
860 | 0, /* @tp_str@ */ | |
861 | 0, /* @tp_getattro@ */ | |
862 | 0, /* @tp_setattro@ */ | |
863 | 0, /* @tp_as_buffer@ */ | |
864 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
865 | Py_TPFLAGS_BASETYPE, | |
866 | ||
867 | /* @tp_doc@ */ | |
868 | "Hash function metaclass.", | |
869 | ||
870 | 0, /* @tp_traverse@ */ | |
871 | 0, /* @tp_clear@ */ | |
872 | 0, /* @tp_richcompare@ */ | |
873 | 0, /* @tp_weaklistoffset@ */ | |
874 | 0, /* @tp_iter@ */ | |
963a6148 | 875 | 0, /* @tp_iternext@ */ |
d7ab1bab | 876 | 0, /* @tp_methods@ */ |
877 | 0, /* @tp_members@ */ | |
878 | gchash_pygetset, /* @tp_getset@ */ | |
879 | 0, /* @tp_base@ */ | |
880 | 0, /* @tp_dict@ */ | |
881 | 0, /* @tp_descr_get@ */ | |
882 | 0, /* @tp_descr_set@ */ | |
883 | 0, /* @tp_dictoffset@ */ | |
884 | 0, /* @tp_init@ */ | |
885 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
886 | abstract_pynew, /* @tp_new@ */ | |
3aa33042 | 887 | 0, /* @tp_free@ */ |
d7ab1bab | 888 | 0 /* @tp_is_gc@ */ |
889 | }; | |
890 | ||
891 | static PyTypeObject ghash_pytype_skel = { | |
6d4db0bf | 892 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 893 | "GHash", /* @tp_name@ */ |
d7ab1bab | 894 | sizeof(ghash_pyobj), /* @tp_basicsize@ */ |
895 | 0, /* @tp_itemsize@ */ | |
896 | ||
897 | ghash_pydealloc, /* @tp_dealloc@ */ | |
898 | 0, /* @tp_print@ */ | |
899 | 0, /* @tp_getattr@ */ | |
900 | 0, /* @tp_setattr@ */ | |
901 | 0, /* @tp_compare@ */ | |
902 | 0, /* @tp_repr@ */ | |
903 | 0, /* @tp_as_number@ */ | |
904 | 0, /* @tp_as_sequence@ */ | |
905 | 0, /* @tp_as_mapping@ */ | |
906 | 0, /* @tp_hash@ */ | |
907 | 0, /* @tp_call@ */ | |
908 | 0, /* @tp_str@ */ | |
909 | 0, /* @tp_getattro@ */ | |
910 | 0, /* @tp_setattro@ */ | |
911 | 0, /* @tp_as_buffer@ */ | |
912 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
913 | Py_TPFLAGS_BASETYPE, | |
914 | ||
915 | /* @tp_doc@ */ | |
916 | "Hash function, abstract base class.", | |
917 | ||
918 | 0, /* @tp_traverse@ */ | |
919 | 0, /* @tp_clear@ */ | |
920 | 0, /* @tp_richcompare@ */ | |
921 | 0, /* @tp_weaklistoffset@ */ | |
922 | 0, /* @tp_iter@ */ | |
963a6148 | 923 | 0, /* @tp_iternext@ */ |
d7ab1bab | 924 | ghash_pymethods, /* @tp_methods@ */ |
925 | 0, /* @tp_members@ */ | |
926 | 0, /* @tp_getset@ */ | |
927 | 0, /* @tp_base@ */ | |
928 | 0, /* @tp_dict@ */ | |
929 | 0, /* @tp_descr_get@ */ | |
930 | 0, /* @tp_descr_set@ */ | |
931 | 0, /* @tp_dictoffset@ */ | |
932 | 0, /* @tp_init@ */ | |
933 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
934 | abstract_pynew, /* @tp_new@ */ | |
3aa33042 | 935 | 0, /* @tp_free@ */ |
d7ab1bab | 936 | 0 /* @tp_is_gc@ */ |
937 | }; | |
938 | ||
939 | /*----- Message authentication --------------------------------------------*/ | |
940 | ||
941 | PyTypeObject *gcmac_pytype, *gmac_pytype, *gmhash_pytype; | |
942 | ||
943 | CONVFUNC(gcmac, gcmac *, GCMAC_CM) | |
944 | CONVFUNC(gmac, gmac *, GMAC_M) | |
945 | CONVFUNC(gmhash, ghash *, GHASH_H) | |
946 | ||
947 | static PyObject *gmac_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
948 | { | |
949 | char *kwlist[] = { "k", 0 }; | |
950 | char *k; | |
951 | int sz; | |
952 | ||
953 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz)) | |
954 | goto end; | |
955 | if (keysz(sz, GCMAC_CM(ty)->keysz) != sz) VALERR("bad key length"); | |
956 | return (gmac_pywrap((PyObject *)ty, | |
957 | GM_KEY(GCMAC_CM(ty), k, sz), | |
958 | f_freeme)); | |
959 | end: | |
b2687a0a | 960 | return (0); |
d7ab1bab | 961 | } |
962 | ||
963 | static PyObject *gmhash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
964 | { | |
965 | char *kwlist[] = { 0 }; | |
966 | ghash_pyobj *g; | |
967 | ||
968 | if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", kwlist)) return (0); | |
969 | g = PyObject_NEW(ghash_pyobj, ty); | |
970 | g->h = GM_INIT(GMAC_M(ty)); | |
971 | g->f = f_freeme; | |
972 | Py_INCREF(ty); | |
973 | return ((PyObject *)g); | |
974 | } | |
975 | ||
976 | PyObject *gcmac_pywrap(gcmac *cm) | |
977 | { | |
df9f8366 | 978 | gcmac_pyobj *g = newtype(gcmac_pytype, 0, cm->name); |
d7ab1bab | 979 | g->cm = cm; |
24b3d57b MW |
980 | g->ty.ht_type.tp_basicsize = sizeof(gmac_pyobj); |
981 | g->ty.ht_type.tp_base = gmac_pytype; | |
d7ab1bab | 982 | Py_INCREF(gmac_pytype); |
24b3d57b MW |
983 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
984 | Py_TPFLAGS_BASETYPE | | |
985 | Py_TPFLAGS_HEAPTYPE); | |
986 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; | |
987 | g->ty.ht_type.tp_free = 0; | |
988 | g->ty.ht_type.tp_new = gmac_pynew; | |
dc075750 | 989 | typeready(&g->ty.ht_type); |
d7ab1bab | 990 | return ((PyObject *)g); |
991 | } | |
992 | ||
993 | PyObject *gmac_pywrap(PyObject *cobj, gmac *m, unsigned f) | |
994 | { | |
995 | gmac_pyobj *g; | |
996 | if (!cobj) cobj = gcmac_pywrap((/*unconst*/ gcmac *)GM_CLASS(m)); | |
997 | else Py_INCREF(cobj); | |
df9f8366 | 998 | g = newtype((PyTypeObject *)cobj, 0, 0); |
24b3d57b MW |
999 | g->ty.ht_name = PyString_FromFormat("%s(keyed)", m->ops->c->name); |
1000 | g->ty.ht_type.tp_name = PyString_AS_STRING(g->ty.ht_name); | |
1001 | g->ty.ht_type.tp_base = gmhash_pytype; | |
d7ab1bab | 1002 | Py_INCREF(gmac_pytype); |
24b3d57b MW |
1003 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
1004 | Py_TPFLAGS_BASETYPE | | |
1005 | Py_TPFLAGS_HEAPTYPE); | |
1006 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; | |
1007 | g->ty.ht_type.tp_free = 0; | |
1008 | g->ty.ht_type.tp_new = gmhash_pynew; | |
dc075750 | 1009 | typeready(&g->ty.ht_type); |
d7ab1bab | 1010 | g->m = m; |
1011 | g->f = f; | |
b2687a0a | 1012 | return ((PyObject *)g); |
d7ab1bab | 1013 | } |
1014 | ||
1015 | static void gmac_pydealloc(PyObject *me) | |
1016 | { | |
1017 | if (GMAC_F(me) & f_freeme) | |
1018 | GM_DESTROY(GMAC_M(me)); | |
1019 | Py_DECREF(me->ob_type); | |
d7ab1bab | 1020 | PyType_Type.tp_dealloc(me); |
1021 | } | |
1022 | ||
1023 | static PyObject *gcmget_name(PyObject *me, void *hunoz) | |
1024 | { return (PyString_FromString(GCMAC_CM(me)->name)); } | |
1025 | ||
1026 | static PyObject *gcmget_keysz(PyObject *me, void *hunoz) | |
1027 | { return (keysz_pywrap(GCMAC_CM(me)->keysz)); } | |
1028 | ||
1029 | static PyObject *gcmget_tagsz(PyObject *me, void *hunoz) | |
1030 | { return (PyInt_FromLong(GCMAC_CM(me)->hashsz)); } | |
1031 | ||
1032 | static PyGetSetDef gcmac_pygetset[] = { | |
1033 | #define GETSETNAME(op, name) gcm##op##_##name | |
1034 | GET (keysz, "CM.keysz -> acceptable key sizes") | |
1035 | GET (tagsz, "CM.tagsz -> MAC output size") | |
1036 | GET (name, "CM.name -> name of this kind of MAC") | |
1037 | #undef GETSETNAME | |
1038 | { 0 } | |
1039 | }; | |
1040 | ||
1041 | static PyTypeObject gcmac_pytype_skel = { | |
6d4db0bf | 1042 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 1043 | "GCMAC", /* @tp_name@ */ |
d7ab1bab | 1044 | sizeof(gchash_pyobj), /* @tp_basicsize@ */ |
1045 | 0, /* @tp_itemsize@ */ | |
1046 | ||
1047 | 0, /* @tp_dealloc@ */ | |
1048 | 0, /* @tp_print@ */ | |
1049 | 0, /* @tp_getattr@ */ | |
1050 | 0, /* @tp_setattr@ */ | |
1051 | 0, /* @tp_compare@ */ | |
1052 | 0, /* @tp_repr@ */ | |
1053 | 0, /* @tp_as_number@ */ | |
1054 | 0, /* @tp_as_sequence@ */ | |
1055 | 0, /* @tp_as_mapping@ */ | |
1056 | 0, /* @tp_hash@ */ | |
1057 | 0, /* @tp_call@ */ | |
1058 | 0, /* @tp_str@ */ | |
1059 | 0, /* @tp_getattro@ */ | |
1060 | 0, /* @tp_setattro@ */ | |
1061 | 0, /* @tp_as_buffer@ */ | |
1062 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1063 | Py_TPFLAGS_BASETYPE, | |
1064 | ||
1065 | /* @tp_doc@ */ | |
1066 | "Message authentication code metametaclass.", | |
1067 | ||
1068 | 0, /* @tp_traverse@ */ | |
1069 | 0, /* @tp_clear@ */ | |
1070 | 0, /* @tp_richcompare@ */ | |
1071 | 0, /* @tp_weaklistoffset@ */ | |
1072 | 0, /* @tp_iter@ */ | |
963a6148 | 1073 | 0, /* @tp_iternext@ */ |
d7ab1bab | 1074 | 0, /* @tp_methods@ */ |
1075 | 0, /* @tp_members@ */ | |
1076 | gcmac_pygetset, /* @tp_getset@ */ | |
1077 | 0, /* @tp_base@ */ | |
1078 | 0, /* @tp_dict@ */ | |
1079 | 0, /* @tp_descr_get@ */ | |
1080 | 0, /* @tp_descr_set@ */ | |
1081 | 0, /* @tp_dictoffset@ */ | |
1082 | 0, /* @tp_init@ */ | |
1083 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1084 | abstract_pynew, /* @tp_new@ */ | |
3aa33042 | 1085 | 0, /* @tp_free@ */ |
d7ab1bab | 1086 | 0 /* @tp_is_gc@ */ |
1087 | }; | |
1088 | ||
1089 | static PyTypeObject gmac_pytype_skel = { | |
6d4db0bf | 1090 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 1091 | "GMAC", /* @tp_name@ */ |
d7ab1bab | 1092 | sizeof(gmac_pyobj), /* @tp_basicsize@ */ |
1093 | 0, /* @tp_itemsize@ */ | |
1094 | ||
1095 | gmac_pydealloc, /* @tp_dealloc@ */ | |
1096 | 0, /* @tp_print@ */ | |
1097 | 0, /* @tp_getattr@ */ | |
1098 | 0, /* @tp_setattr@ */ | |
1099 | 0, /* @tp_compare@ */ | |
1100 | 0, /* @tp_repr@ */ | |
1101 | 0, /* @tp_as_number@ */ | |
1102 | 0, /* @tp_as_sequence@ */ | |
1103 | 0, /* @tp_as_mapping@ */ | |
1104 | 0, /* @tp_hash@ */ | |
1105 | 0, /* @tp_call@ */ | |
1106 | 0, /* @tp_str@ */ | |
1107 | 0, /* @tp_getattro@ */ | |
1108 | 0, /* @tp_setattro@ */ | |
1109 | 0, /* @tp_as_buffer@ */ | |
1110 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1111 | Py_TPFLAGS_BASETYPE, | |
1112 | ||
1113 | /* @tp_doc@ */ | |
1114 | "Message authentication code metaclass, abstract base class.", | |
1115 | ||
1116 | 0, /* @tp_traverse@ */ | |
1117 | 0, /* @tp_clear@ */ | |
1118 | 0, /* @tp_richcompare@ */ | |
1119 | 0, /* @tp_weaklistoffset@ */ | |
1120 | 0, /* @tp_iter@ */ | |
963a6148 | 1121 | 0, /* @tp_iternext@ */ |
d7ab1bab | 1122 | 0, /* @tp_methods@ */ |
1123 | 0, /* @tp_members@ */ | |
1124 | 0, /* @tp_getset@ */ | |
1125 | 0, /* @tp_base@ */ | |
1126 | 0, /* @tp_dict@ */ | |
1127 | 0, /* @tp_descr_get@ */ | |
1128 | 0, /* @tp_descr_set@ */ | |
1129 | 0, /* @tp_dictoffset@ */ | |
1130 | 0, /* @tp_init@ */ | |
1131 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1132 | abstract_pynew, /* @tp_new@ */ | |
3aa33042 | 1133 | 0, /* @tp_free@ */ |
d7ab1bab | 1134 | 0 /* @tp_is_gc@ */ |
1135 | }; | |
1136 | ||
1137 | static PyTypeObject gmhash_pytype_skel = { | |
6d4db0bf | 1138 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 1139 | "GMACHash", /* @tp_name@ */ |
d7ab1bab | 1140 | sizeof(ghash_pyobj), /* @tp_basicsize@ */ |
1141 | 0, /* @tp_itemsize@ */ | |
1142 | ||
1143 | ghash_pydealloc, /* @tp_dealloc@ */ | |
1144 | 0, /* @tp_print@ */ | |
1145 | 0, /* @tp_getattr@ */ | |
1146 | 0, /* @tp_setattr@ */ | |
1147 | 0, /* @tp_compare@ */ | |
1148 | 0, /* @tp_repr@ */ | |
1149 | 0, /* @tp_as_number@ */ | |
1150 | 0, /* @tp_as_sequence@ */ | |
1151 | 0, /* @tp_as_mapping@ */ | |
1152 | 0, /* @tp_hash@ */ | |
1153 | 0, /* @tp_call@ */ | |
1154 | 0, /* @tp_str@ */ | |
1155 | 0, /* @tp_getattro@ */ | |
1156 | 0, /* @tp_setattro@ */ | |
1157 | 0, /* @tp_as_buffer@ */ | |
1158 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1159 | Py_TPFLAGS_BASETYPE, | |
1160 | ||
1161 | /* @tp_doc@ */ | |
1162 | "Message authentication code, abstract base class.", | |
1163 | ||
1164 | 0, /* @tp_traverse@ */ | |
1165 | 0, /* @tp_clear@ */ | |
1166 | 0, /* @tp_richcompare@ */ | |
1167 | 0, /* @tp_weaklistoffset@ */ | |
1168 | 0, /* @tp_iter@ */ | |
963a6148 | 1169 | 0, /* @tp_iternext@ */ |
d7ab1bab | 1170 | 0, /* @tp_methods@ */ |
1171 | 0, /* @tp_members@ */ | |
1172 | 0, /* @tp_getset@ */ | |
1173 | 0, /* @tp_base@ */ | |
1174 | 0, /* @tp_dict@ */ | |
1175 | 0, /* @tp_descr_get@ */ | |
1176 | 0, /* @tp_descr_set@ */ | |
1177 | 0, /* @tp_dictoffset@ */ | |
1178 | 0, /* @tp_init@ */ | |
1179 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1180 | abstract_pynew, /* @tp_new@ */ | |
3aa33042 | 1181 | 0, /* @tp_free@ */ |
d7ab1bab | 1182 | 0 /* @tp_is_gc@ */ |
1183 | }; | |
1184 | ||
03ed9abb MW |
1185 | /*----- Pseudorandom permutations -----------------------------------------*/ |
1186 | ||
1187 | static PyTypeObject *gcprp_pytype, *gprp_pytype; | |
1188 | ||
1189 | typedef struct prpinfo { | |
1190 | const char *name; | |
1191 | const octet *keysz; | |
1192 | size_t ctxsz; | |
1193 | size_t blksz; | |
1194 | void (*init)(void *, const void *, size_t); | |
1195 | void (*eblk)(void *, const void *, void *); | |
1196 | void (*dblk)(void *, const void *, void *); | |
1197 | } prpinfo; | |
1198 | ||
1199 | #define PRP_DEF(PRE, pre) \ | |
1200 | static void pre##_prpinit(void *ctx, const void *k, size_t ksz) \ | |
1201 | { pre##_init(ctx, k, ksz); } \ | |
1202 | static void pre##_prpeblk(void *ctx, const void *in, void *out) \ | |
1203 | { \ | |
1204 | uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \ | |
1205 | pre##_eblk(ctx, w, w); BLKC_STORE(PRE, out, w); \ | |
1206 | } \ | |
1207 | static void pre##_prpdblk(void *ctx, const void *in, void *out) \ | |
1208 | { \ | |
1209 | uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \ | |
1210 | pre##_dblk(ctx, w, w); BLKC_STORE(PRE, out, w); \ | |
1211 | } \ | |
1212 | static const prpinfo pre##_prpinfo = { \ | |
1213 | #pre, pre##_keysz, sizeof(pre##_ctx), PRE##_BLKSZ, \ | |
1214 | pre##_prpinit, pre##_prpeblk, pre##_prpdblk \ | |
1215 | }; | |
1216 | PRPS(PRP_DEF) | |
1217 | ||
1218 | static const struct prpinfo *const gprptab[] = { | |
1219 | #define PRP_ENTRY(PRE, pre) &pre##_prpinfo, | |
1220 | PRPS(PRP_ENTRY) | |
1221 | 0 | |
b2687a0a | 1222 | }; |
03ed9abb MW |
1223 | |
1224 | typedef struct gcprp_pyobj { | |
1225 | PyHeapTypeObject ty; | |
1226 | const prpinfo *prp; | |
1227 | } gcprp_pyobj; | |
1228 | #define GCPRP_PRP(o) (((gcprp_pyobj *)(o))->prp) | |
1229 | ||
1230 | typedef struct gprp_pyobj { | |
1231 | PyObject_HEAD | |
1232 | const prpinfo *prp; | |
1233 | } gprp_pyobj; | |
1234 | #define GPRP_PRP(o) (((gprp_pyobj *)(o))->prp) | |
1235 | #define GPRP_CTX(o) (((gprp_pyobj *)(o)) + 1) | |
1236 | ||
1237 | typedef struct prp { | |
1238 | const prpinfo *prp; | |
1239 | void *ctx; | |
1240 | } prp; | |
1241 | ||
1242 | static PyObject *gprp_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
1243 | { | |
1244 | char *kwlist[] = { "key", 0 }; | |
1245 | char *k; | |
1246 | int sz; | |
1247 | const prpinfo *prp = GCPRP_PRP(ty); | |
1248 | PyObject *me; | |
1249 | ||
1250 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz)) | |
1251 | goto end; | |
1252 | if (keysz(sz, prp->keysz) != sz) VALERR("bad key length"); | |
1253 | me = (PyObject *)ty->tp_alloc(ty, 0); | |
1254 | GPRP_PRP(me) = prp; | |
1255 | prp->init(GPRP_CTX(me), k, sz); | |
1256 | Py_INCREF(me); | |
1257 | return (me); | |
1258 | end: | |
b2687a0a | 1259 | return (0); |
03ed9abb MW |
1260 | } |
1261 | ||
1262 | static void gprp_pydealloc(PyObject *me) | |
1263 | { Py_DECREF(me->ob_type); FREEOBJ(me); } | |
1264 | ||
1265 | static PyObject *gcprp_pywrap(const prpinfo *prp) | |
1266 | { | |
1267 | gcprp_pyobj *g = newtype(gcprp_pytype, 0, prp->name); | |
1268 | g->prp = prp; | |
24b3d57b MW |
1269 | g->ty.ht_type.tp_basicsize = sizeof(gprp_pyobj) + prp->ctxsz; |
1270 | g->ty.ht_type.tp_base = gprp_pytype; | |
03ed9abb | 1271 | Py_INCREF(gprp_pytype); |
24b3d57b MW |
1272 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
1273 | Py_TPFLAGS_BASETYPE | | |
1274 | Py_TPFLAGS_HEAPTYPE); | |
1275 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; | |
1276 | g->ty.ht_type.tp_free = 0; | |
1277 | g->ty.ht_type.tp_new = gprp_pynew; | |
dc075750 | 1278 | typeready(&g->ty.ht_type); |
03ed9abb MW |
1279 | return ((PyObject *)g); |
1280 | } | |
1281 | ||
1282 | static PyObject *gcpget_name(PyObject *me, void *hunoz) | |
1283 | { return (PyString_FromString(GCPRP_PRP(me)->name)); } | |
1284 | static PyObject *gcpget_keysz(PyObject *me, void *hunoz) | |
1285 | { return (keysz_pywrap(GCPRP_PRP(me)->keysz)); } | |
1286 | static PyObject *gcpget_blksz(PyObject *me, void *hunoz) | |
1287 | { return (PyInt_FromLong(GCPRP_PRP(me)->blksz)); } | |
1288 | ||
1289 | static PyObject *gpmeth_encrypt(PyObject *me, PyObject *arg) | |
1290 | { | |
1291 | char *p; | |
1292 | int n; | |
1293 | PyObject *rc = 0; | |
1294 | ||
1295 | if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &n)) goto end; | |
1296 | if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length"); | |
1297 | rc = bytestring_pywrap(0, n); | |
1298 | GPRP_PRP(me)->eblk(GPRP_CTX(me), p, PyString_AS_STRING(rc)); | |
1299 | end: | |
1300 | return (rc); | |
1301 | } | |
1302 | ||
1303 | static PyObject *gpmeth_decrypt(PyObject *me, PyObject *arg) | |
1304 | { | |
1305 | char *p; | |
1306 | int n; | |
1307 | PyObject *rc = 0; | |
1308 | ||
1309 | if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &n)) goto end; | |
1310 | if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length"); | |
1311 | rc = bytestring_pywrap(0, n); | |
1312 | GPRP_PRP(me)->dblk(GPRP_CTX(me), p, PyString_AS_STRING(rc)); | |
1313 | end: | |
1314 | return (rc); | |
1315 | } | |
1316 | ||
1317 | static PyGetSetDef gcprp_pygetset[] = { | |
1318 | #define GETSETNAME(op, name) gcp##op##_##name | |
1319 | GET (keysz, "CP.keysz -> acceptable key sizes") | |
1320 | GET (blksz, "CP.blksz -> block size") | |
1321 | GET (name, "CP.name -> name of this kind of PRP") | |
1322 | #undef GETSETNAME | |
1323 | { 0 } | |
1324 | }; | |
1325 | ||
1326 | static PyMethodDef gprp_pymethods[] = { | |
1327 | #define METHNAME(name) gpmeth_##name | |
1328 | METH (encrypt, "P.encrypt(PT) -> CT") | |
1329 | METH (decrypt, "P.decrypt(CT) -> PT") | |
1330 | #undef METHNAME | |
1331 | { 0 } | |
1332 | }; | |
1333 | ||
1334 | static PyTypeObject gcprp_pytype_skel = { | |
1335 | PyObject_HEAD_INIT(0) 0, /* Header */ | |
c461c9b3 | 1336 | "GCPRP", /* @tp_name@ */ |
03ed9abb MW |
1337 | sizeof(gcprp_pyobj), /* @tp_basicsize@ */ |
1338 | 0, /* @tp_itemsize@ */ | |
1339 | ||
1340 | 0, /* @tp_dealloc@ */ | |
1341 | 0, /* @tp_print@ */ | |
1342 | 0, /* @tp_getattr@ */ | |
1343 | 0, /* @tp_setattr@ */ | |
1344 | 0, /* @tp_compare@ */ | |
1345 | 0, /* @tp_repr@ */ | |
1346 | 0, /* @tp_as_number@ */ | |
1347 | 0, /* @tp_as_sequence@ */ | |
1348 | 0, /* @tp_as_mapping@ */ | |
1349 | 0, /* @tp_hash@ */ | |
1350 | 0, /* @tp_call@ */ | |
1351 | 0, /* @tp_str@ */ | |
1352 | 0, /* @tp_getattro@ */ | |
1353 | 0, /* @tp_setattro@ */ | |
1354 | 0, /* @tp_as_buffer@ */ | |
1355 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1356 | Py_TPFLAGS_BASETYPE, | |
1357 | ||
1358 | /* @tp_doc@ */ | |
1359 | "Pseudorandom permutation metaclass.", | |
1360 | ||
1361 | 0, /* @tp_traverse@ */ | |
1362 | 0, /* @tp_clear@ */ | |
1363 | 0, /* @tp_richcompare@ */ | |
1364 | 0, /* @tp_weaklistoffset@ */ | |
1365 | 0, /* @tp_iter@ */ | |
1366 | 0, /* @tp_iternext@ */ | |
1367 | 0, /* @tp_methods@ */ | |
1368 | 0, /* @tp_members@ */ | |
1369 | gcprp_pygetset, /* @tp_getset@ */ | |
1370 | 0, /* @tp_base@ */ | |
1371 | 0, /* @tp_dict@ */ | |
1372 | 0, /* @tp_descr_get@ */ | |
1373 | 0, /* @tp_descr_set@ */ | |
1374 | 0, /* @tp_dictoffset@ */ | |
1375 | 0, /* @tp_init@ */ | |
1376 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1377 | abstract_pynew, /* @tp_new@ */ | |
1378 | 0, /* @tp_free@ */ | |
1379 | 0 /* @tp_is_gc@ */ | |
1380 | }; | |
1381 | ||
1382 | static PyTypeObject gprp_pytype_skel = { | |
1383 | PyObject_HEAD_INIT(0) 0, /* Header */ | |
c461c9b3 | 1384 | "GPRP", /* @tp_name@ */ |
03ed9abb MW |
1385 | sizeof(gprp_pyobj), /* @tp_basicsize@ */ |
1386 | 0, /* @tp_itemsize@ */ | |
1387 | ||
1388 | gprp_pydealloc, /* @tp_dealloc@ */ | |
1389 | 0, /* @tp_print@ */ | |
1390 | 0, /* @tp_getattr@ */ | |
1391 | 0, /* @tp_setattr@ */ | |
1392 | 0, /* @tp_compare@ */ | |
1393 | 0, /* @tp_repr@ */ | |
1394 | 0, /* @tp_as_number@ */ | |
1395 | 0, /* @tp_as_sequence@ */ | |
1396 | 0, /* @tp_as_mapping@ */ | |
1397 | 0, /* @tp_hash@ */ | |
1398 | 0, /* @tp_call@ */ | |
1399 | 0, /* @tp_str@ */ | |
1400 | 0, /* @tp_getattro@ */ | |
1401 | 0, /* @tp_setattro@ */ | |
1402 | 0, /* @tp_as_buffer@ */ | |
1403 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1404 | Py_TPFLAGS_BASETYPE, | |
1405 | ||
1406 | /* @tp_doc@ */ | |
1407 | "Pseudorandom permutation, abstract base class.", | |
1408 | ||
1409 | 0, /* @tp_traverse@ */ | |
1410 | 0, /* @tp_clear@ */ | |
1411 | 0, /* @tp_richcompare@ */ | |
1412 | 0, /* @tp_weaklistoffset@ */ | |
1413 | 0, /* @tp_iter@ */ | |
1414 | 0, /* @tp_iternext@ */ | |
1415 | gprp_pymethods, /* @tp_methods@ */ | |
1416 | 0, /* @tp_members@ */ | |
1417 | 0, /* @tp_getset@ */ | |
1418 | 0, /* @tp_base@ */ | |
1419 | 0, /* @tp_dict@ */ | |
1420 | 0, /* @tp_descr_get@ */ | |
1421 | 0, /* @tp_descr_set@ */ | |
1422 | 0, /* @tp_dictoffset@ */ | |
1423 | 0, /* @tp_init@ */ | |
1424 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1425 | abstract_pynew, /* @tp_new@ */ | |
1426 | 0, /* @tp_free@ */ | |
1427 | 0 /* @tp_is_gc@ */ | |
1428 | }; | |
1429 | ||
d7ab1bab | 1430 | /*----- Main code ---------------------------------------------------------*/ |
1431 | ||
89157adc MW |
1432 | static PyMethodDef methods[] = { |
1433 | #define METHNAME(func) meth_##func | |
1434 | METH (_KeySZ_fromdl, "\ | |
1435 | fromdl(N) -> M: convert integer discrete log field size to work factor") | |
1436 | METH (_KeySZ_fromschnorr, "\ | |
1437 | fromschnorr(N) -> M: convert Schnorr group order to work factor") | |
1438 | METH (_KeySZ_fromif, "\ | |
1439 | fromif(N) -> M: convert integer factorization problem size to work factor") | |
1440 | METH (_KeySZ_fromec, "\ | |
1441 | fromec(N) -> M: convert elliptic curve group order to work factor") | |
1442 | METH (_KeySZ_todl, "\ | |
1443 | todl(N) -> M: convert work factor to integer discrete log field size") | |
1444 | METH (_KeySZ_toschnorr, "\ | |
1445 | toschnorr(N) -> M: convert work factor to Schnorr group order") | |
1446 | METH (_KeySZ_toif, "\ | |
1447 | toif(N) -> M: convert work factor to integer factorization problem size") | |
1448 | METH (_KeySZ_toec, "\ | |
1449 | toec(N) -> M: convert work factor to elliptic curve group order") | |
1450 | #undef METHNAME | |
1451 | { 0 } | |
1452 | }; | |
1453 | ||
d7ab1bab | 1454 | void algorithms_pyinit(void) |
1455 | { | |
1456 | INITTYPE(keysz, root); | |
1457 | INITTYPE(keyszany, keysz); | |
1458 | INITTYPE(keyszrange, keysz); | |
1459 | INITTYPE(keyszset, keysz); | |
1460 | INITTYPE(gccipher, type); | |
1461 | INITTYPE(gcipher, root); | |
1462 | INITTYPE(gchash, type); | |
1463 | INITTYPE(ghash, root); | |
1464 | INITTYPE(gcmac, type); | |
1465 | INITTYPE(gmac, type); | |
1466 | INITTYPE(gmhash, ghash); | |
03ed9abb MW |
1467 | INITTYPE(gcprp, type); |
1468 | INITTYPE(gprp, root); | |
89157adc | 1469 | addmethods(methods); |
d7ab1bab | 1470 | } |
1471 | ||
d7ab1bab | 1472 | GEN(gcciphers, cipher) |
1473 | GEN(gchashes, hash) | |
1474 | GEN(gcmacs, mac) | |
03ed9abb MW |
1475 | #define gcprp prpinfo |
1476 | GEN(gcprps, prp) | |
d7ab1bab | 1477 | |
1478 | void algorithms_pyinsert(PyObject *mod) | |
1479 | { | |
1480 | PyObject *d; | |
1481 | INSERT("KeySZ", keysz_pytype); | |
1482 | INSERT("KeySZAny", keyszany_pytype); | |
1483 | INSERT("KeySZRange", keyszrange_pytype); | |
1484 | INSERT("KeySZSet", keyszset_pytype); | |
1485 | INSERT("GCCipher", gccipher_pytype); | |
1486 | INSERT("GCipher", gcipher_pytype); | |
1487 | INSERT("gcciphers", gcciphers()); | |
1488 | INSERT("GCHash", gchash_pytype); | |
1489 | INSERT("GHash", ghash_pytype); | |
1490 | INSERT("gchashes", d = gchashes()); | |
1491 | sha_pyobj = PyDict_GetItemString(d, "sha"); Py_INCREF(sha_pyobj); | |
1492 | has160_pyobj = PyDict_GetItemString(d, "has160"); Py_INCREF(has160_pyobj); | |
1493 | INSERT("GCMAC", gcmac_pytype); | |
1494 | INSERT("GMAC", gmac_pytype); | |
1495 | INSERT("GMACHash", gmhash_pytype); | |
1496 | INSERT("gcmacs", gcmacs()); | |
03ed9abb MW |
1497 | INSERT("GCPRP", gcprp_pytype); |
1498 | INSERT("GPRP", gprp_pytype); | |
1499 | INSERT("gcprps", gcprps()); | |
d7ab1bab | 1500 | } |
1501 | ||
1502 | /*----- That's all, folks -------------------------------------------------*/ |