catacomb/pwsafe.py: New Git-friendly `DirectoryStorageBackend'.
[catacomb-python] / algorithms.c
1 /* -*-c-*-
2 *
3 * Symmetric cryptography
4 *
5 * (c) 2004 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
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.
16 *
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.
21 *
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 =
48 PyObject_New(keyszrange_pyobj, keyszrange_pytype);
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 =
58 PyObject_New(keyszset_pyobj, keyszset_pytype);
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);
146 x = 0;
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 = {
232 PyObject_HEAD_INIT(0) 0, /* Header */
233 "catacomb.KeySZ", /* @tp_name@ */
234 sizeof(keysz_pyobj), /* @tp_basicsize@ */
235 0, /* @tp_itemsize@ */
236
237 0, /* @tp_dealloc@ */
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@ */
263 0, /* @tp_iternext@ */
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@ */
275 0, /* @tp_free@ */
276 0 /* @tp_is_gc@ */
277 };
278
279 static PyTypeObject keyszany_pytype_skel = {
280 PyObject_HEAD_INIT(0) 0, /* Header */
281 "catacomb.KeySZAny", /* @tp_name@ */
282 sizeof(keysz_pyobj), /* @tp_basicsize@ */
283 0, /* @tp_itemsize@ */
284
285 0, /* @tp_dealloc@ */
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@ */
311 0, /* @tp_iternext@ */
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@ */
323 0, /* @tp_free@ */
324 0 /* @tp_is_gc@ */
325 };
326
327 static PyTypeObject keyszrange_pytype_skel = {
328 PyObject_HEAD_INIT(0) 0, /* Header */
329 "catacomb.KeySZRange", /* @tp_name@ */
330 sizeof(keyszrange_pyobj), /* @tp_basicsize@ */
331 0, /* @tp_itemsize@ */
332
333 0, /* @tp_dealloc@ */
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@ */
360 0, /* @tp_iternext@ */
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@ */
372 0, /* @tp_free@ */
373 0 /* @tp_is_gc@ */
374 };
375
376 static PyTypeObject keyszset_pytype_skel = {
377 PyObject_HEAD_INIT(0) 0, /* Header */
378 "catacomb.KeySZSet", /* @tp_name@ */
379 sizeof(keyszset_pyobj), /* @tp_basicsize@ */
380 0, /* @tp_itemsize@ */
381
382 0, /* @tp_dealloc@ */
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@ */
409 0, /* @tp_iternext@ */
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@ */
421 0, /* @tp_free@ */
422 0 /* @tp_is_gc@ */
423 };
424
425 /*----- Symmetric encryption ----------------------------------------------*/
426
427 PyTypeObject *gccipher_pytype, *gcipher_pytype;
428
429 CONVFUNC(gccipher, gccipher *, GCCIPHER_CC)
430 CONVFUNC(gcipher, gcipher *, GCIPHER_C)
431
432 PyObject *gcipher_pywrap(PyObject *cobj, gcipher *c, unsigned f)
433 {
434 gcipher_pyobj *g;
435 if (!cobj) cobj = gccipher_pywrap((/*unconst*/ gccipher *)GC_CLASS(c));
436 else Py_INCREF(cobj);
437 g = PyObject_NEW(gcipher_pyobj, (PyTypeObject *)cobj);
438 g->c = c;
439 g->f = f;
440 return ((PyObject *)g);
441 }
442
443 static PyObject *gcipher_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
444 {
445 char *kwlist[] = { "k", 0 };
446 char *k;
447 int sz;
448
449 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz))
450 goto end;
451 if (keysz(sz, GCCIPHER_CC(ty)->keysz) != sz) VALERR("bad key length");
452 return (gcipher_pywrap((PyObject *)ty,
453 GC_INIT(GCCIPHER_CC(ty), k, sz),
454 f_freeme));
455 end:
456 return (0);
457 }
458
459 PyObject *gccipher_pywrap(gccipher *cc)
460 {
461 gccipher_pyobj *g = newtype(gccipher_pytype, 0, cc->name);
462 g->cc = cc;
463 g->ty.ht_type.tp_basicsize = sizeof(gcipher_pyobj);
464 g->ty.ht_type.tp_base = gcipher_pytype;
465 Py_INCREF(gcipher_pytype);
466 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
467 Py_TPFLAGS_BASETYPE |
468 Py_TPFLAGS_HEAPTYPE);
469 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
470 g->ty.ht_type.tp_free = 0;
471 g->ty.ht_type.tp_new = gcipher_pynew;
472 PyType_Ready(&g->ty.ht_type);
473 return ((PyObject *)g);
474 }
475
476 static void gcipher_pydealloc(PyObject *me)
477 {
478 if (GCIPHER_F(me) & f_freeme)
479 GC_DESTROY(GCIPHER_C(me));
480 Py_DECREF(me->ob_type);
481 FREEOBJ(me);
482 }
483
484 static PyObject *gccget_name(PyObject *me, void *hunoz)
485 { return (PyString_FromString(GCCIPHER_CC(me)->name)); }
486
487 static PyObject *gccget_keysz(PyObject *me, void *hunoz)
488 { return (keysz_pywrap(GCCIPHER_CC(me)->keysz)); }
489
490 static PyObject *gccget_blksz(PyObject *me, void *hunoz)
491 { return (PyInt_FromLong(GCCIPHER_CC(me)->blksz)); }
492
493 static PyObject *gcmeth_encrypt(PyObject *me, PyObject *arg)
494 {
495 char *p;
496 int sz;
497 PyObject *rc = 0;
498
499 if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &sz)) return (0);
500 rc = bytestring_pywrap(0, sz);
501 GC_ENCRYPT(GCIPHER_C(me), p, PyString_AS_STRING(rc), sz);
502 return (rc);
503 }
504
505 static PyObject *gcmeth_enczero(PyObject *me, PyObject *arg)
506 {
507 char *p;
508 int sz;
509 PyObject *rc = 0;
510
511 if (!PyArg_ParseTuple(arg, "i:enczero", &sz)) return (0);
512 rc = bytestring_pywrap(0, sz);
513 p = PyString_AS_STRING(rc);
514 memset(p, 0, sz);
515 GC_ENCRYPT(GCIPHER_C(me), p, p, sz);
516 return (rc);
517 }
518
519 static PyObject *gcmeth_decrypt(PyObject *me, PyObject *arg)
520 {
521 char *p;
522 int sz;
523 PyObject *rc = 0;
524
525 if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &sz)) return (0);
526 rc = bytestring_pywrap(0, sz);
527 GC_DECRYPT(GCIPHER_C(me), p, PyString_AS_STRING(rc), sz);
528 return (rc);
529 }
530
531 static PyObject *gcmeth_deczero(PyObject *me, PyObject *arg)
532 {
533 char *p;
534 int sz;
535 PyObject *rc = 0;
536
537 if (!PyArg_ParseTuple(arg, "i:deczero", &sz)) return (0);
538 rc = bytestring_pywrap(0, sz);
539 p = PyString_AS_STRING(rc);
540 memset(p, 0, sz);
541 GC_DECRYPT(GCIPHER_C(me), p, p, sz);
542 return (rc);
543 }
544
545 static PyObject *gcmeth_setiv(PyObject *me, PyObject *arg)
546 {
547 char *p;
548 int sz;
549
550 if (!PyArg_ParseTuple(arg, "s#:setiv", &p, &sz)) goto end;
551 if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode");
552 if (sz != GC_CLASS(GCIPHER_C(me))->blksz) VALERR("bad IV length");
553 GC_SETIV(GCIPHER_C(me), p);
554 RETURN_ME;
555 end:
556 return (0);
557 }
558
559 static PyObject *gcmeth_bdry(PyObject *me, PyObject *arg)
560 {
561 if (!PyArg_ParseTuple(arg, ":bdry")) goto end;
562 if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode");
563 GC_BDRY(GCIPHER_C(me));
564 RETURN_ME;
565 end:
566 return (0);
567 }
568
569 static PyGetSetDef gccipher_pygetset[] = {
570 #define GETSETNAME(op, name) gcc##op##_##name
571 GET (keysz, "CC.keysz -> acceptable key sizes")
572 GET (blksz, "CC.blksz -> block size, or zero")
573 GET (name, "CC.name -> name of this kind of cipher")
574 #undef GETSETNAME
575 { 0 }
576 };
577
578 static PyMethodDef gcipher_pymethods[] = {
579 #define METHNAME(name) gcmeth_##name
580 METH (encrypt, "C.encrypt(PT) -> CT")
581 METH (enczero, "C.enczero(N) -> CT")
582 METH (decrypt, "C.decrypt(CT) -> PT")
583 METH (deczero, "C.deczero(N) -> PT")
584 METH (setiv, "C.setiv(IV)")
585 METH (bdry, "C.bdry()")
586 #undef METHNAME
587 { 0 }
588 };
589
590 static PyTypeObject gccipher_pytype_skel = {
591 PyObject_HEAD_INIT(0) 0, /* Header */
592 "catacomb.GCCipher", /* @tp_name@ */
593 sizeof(gccipher_pyobj), /* @tp_basicsize@ */
594 0, /* @tp_itemsize@ */
595
596 0, /* @tp_dealloc@ */
597 0, /* @tp_print@ */
598 0, /* @tp_getattr@ */
599 0, /* @tp_setattr@ */
600 0, /* @tp_compare@ */
601 0, /* @tp_repr@ */
602 0, /* @tp_as_number@ */
603 0, /* @tp_as_sequence@ */
604 0, /* @tp_as_mapping@ */
605 0, /* @tp_hash@ */
606 0, /* @tp_call@ */
607 0, /* @tp_str@ */
608 0, /* @tp_getattro@ */
609 0, /* @tp_setattro@ */
610 0, /* @tp_as_buffer@ */
611 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
612 Py_TPFLAGS_BASETYPE,
613
614 /* @tp_doc@ */
615 "Symmetric cipher metaclass.",
616
617 0, /* @tp_traverse@ */
618 0, /* @tp_clear@ */
619 0, /* @tp_richcompare@ */
620 0, /* @tp_weaklistoffset@ */
621 0, /* @tp_iter@ */
622 0, /* @tp_iternext@ */
623 0, /* @tp_methods@ */
624 0, /* @tp_members@ */
625 gccipher_pygetset, /* @tp_getset@ */
626 0, /* @tp_base@ */
627 0, /* @tp_dict@ */
628 0, /* @tp_descr_get@ */
629 0, /* @tp_descr_set@ */
630 0, /* @tp_dictoffset@ */
631 0, /* @tp_init@ */
632 PyType_GenericAlloc, /* @tp_alloc@ */
633 abstract_pynew, /* @tp_new@ */
634 0, /* @tp_free@ */
635 0 /* @tp_is_gc@ */
636 };
637
638 static PyTypeObject gcipher_pytype_skel = {
639 PyObject_HEAD_INIT(0) 0, /* Header */
640 "catacomb.GCipher", /* @tp_name@ */
641 sizeof(gcipher_pyobj), /* @tp_basicsize@ */
642 0, /* @tp_itemsize@ */
643
644 gcipher_pydealloc, /* @tp_dealloc@ */
645 0, /* @tp_print@ */
646 0, /* @tp_getattr@ */
647 0, /* @tp_setattr@ */
648 0, /* @tp_compare@ */
649 0, /* @tp_repr@ */
650 0, /* @tp_as_number@ */
651 0, /* @tp_as_sequence@ */
652 0, /* @tp_as_mapping@ */
653 0, /* @tp_hash@ */
654 0, /* @tp_call@ */
655 0, /* @tp_str@ */
656 0, /* @tp_getattro@ */
657 0, /* @tp_setattro@ */
658 0, /* @tp_as_buffer@ */
659 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
660 Py_TPFLAGS_BASETYPE,
661
662 /* @tp_doc@ */
663 "Symmetric cipher, abstract base class.",
664
665 0, /* @tp_traverse@ */
666 0, /* @tp_clear@ */
667 0, /* @tp_richcompare@ */
668 0, /* @tp_weaklistoffset@ */
669 0, /* @tp_iter@ */
670 0, /* @tp_iternext@ */
671 gcipher_pymethods, /* @tp_methods@ */
672 0, /* @tp_members@ */
673 0, /* @tp_getset@ */
674 0, /* @tp_base@ */
675 0, /* @tp_dict@ */
676 0, /* @tp_descr_get@ */
677 0, /* @tp_descr_set@ */
678 0, /* @tp_dictoffset@ */
679 0, /* @tp_init@ */
680 PyType_GenericAlloc, /* @tp_alloc@ */
681 abstract_pynew, /* @tp_new@ */
682 0, /* @tp_free@ */
683 0 /* @tp_is_gc@ */
684 };
685
686 /*----- Hash functions ----------------------------------------------------*/
687
688 PyTypeObject *gchash_pytype, *ghash_pytype;
689
690 CONVFUNC(gchash, gchash *, GCHASH_CH)
691 CONVFUNC(ghash, ghash *, GHASH_H)
692
693 static PyObject *ghash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
694 {
695 char *kwlist[] = { 0 };
696 if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", kwlist))
697 goto end;
698 return (ghash_pywrap((PyObject *)ty, GH_INIT(GCHASH_CH(ty)), f_freeme));
699 end:
700 return (0);
701 }
702
703 PyObject *gchash_pywrap(gchash *ch)
704 {
705 gchash_pyobj *g = newtype(gchash_pytype, 0, ch->name);
706 g->ch = ch;
707 g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj);
708 g->ty.ht_type.tp_base = ghash_pytype;
709 Py_INCREF(ghash_pytype);
710 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
711 Py_TPFLAGS_BASETYPE |
712 Py_TPFLAGS_HEAPTYPE);
713 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
714 g->ty.ht_type.tp_free = 0;
715 g->ty.ht_type.tp_new = ghash_pynew;
716 PyType_Ready(&g->ty.ht_type);
717 return ((PyObject *)g);
718 }
719
720 PyObject *ghash_pywrap(PyObject *cobj, ghash *h, unsigned f)
721 {
722 ghash_pyobj *g;
723 if (!cobj) cobj = gchash_pywrap((/*unconst*/ gchash *)GH_CLASS(h));
724 else Py_INCREF(cobj);
725 g = PyObject_NEW(ghash_pyobj, (PyTypeObject *)cobj);
726 g->h = h;
727 g->f = f;
728 return ((PyObject *)g);
729 }
730
731 static void ghash_pydealloc(PyObject *me)
732 {
733 if (GHASH_F(me) & f_freeme)
734 GH_DESTROY(GHASH_H(me));
735 Py_DECREF(me->ob_type);
736 FREEOBJ(me);
737 }
738
739 static PyObject *gchget_name(PyObject *me, void *hunoz)
740 { return (PyString_FromString(GCHASH_CH(me)->name)); }
741
742 static PyObject *gchget_hashsz(PyObject *me, void *hunoz)
743 { return (PyInt_FromLong(GCHASH_CH(me)->hashsz)); }
744
745 static PyObject *gchget_bufsz(PyObject *me, void *hunoz)
746 { return (PyInt_FromLong(GCHASH_CH(me)->bufsz)); }
747
748 static PyObject *ghmeth_hash(PyObject *me, PyObject *arg)
749 {
750 char *p;
751 int sz;
752 if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0);
753 GH_HASH(GHASH_H(me), p, sz);
754 RETURN_ME;
755 }
756
757 static PyObject *ghmeth_done(PyObject *me, PyObject *arg)
758 {
759 ghash *g;
760 PyObject *rc;
761 if (!PyArg_ParseTuple(arg, ":done")) return (0);
762 g = GH_COPY(GHASH_H(me));
763 rc = bytestring_pywrap(0, g->ops->c->hashsz);
764 GH_DONE(g, PyString_AS_STRING(rc));
765 GH_DESTROY(g);
766 return (rc);
767 }
768
769 static PyGetSetDef gchash_pygetset[] = {
770 #define GETSETNAME(op, name) gch##op##_##name
771 GET (bufsz, "CH.bufsz -> hash buffer size, or zero")
772 GET (hashsz, "CH.blksz -> hash output size")
773 GET (name, "CH.name -> name of this kind of hash")
774 #undef GETSETNAME
775 { 0 }
776 };
777
778 #define GHMETH_HASHU_(n, W, w) \
779 static PyObject *ghmeth_hashu##w(PyObject *me, PyObject *arg) \
780 { \
781 uint##n x; \
782 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \
783 GH_HASHU##W(GHASH_H(me), x); \
784 RETURN_ME; \
785 end: \
786 return (0); \
787 }
788 DOUINTCONV(GHMETH_HASHU_)
789
790 #define GHMETH_HASHBUF_(n, W, w) \
791 static PyObject *ghmeth_hashbuf##w(PyObject *me, PyObject *arg) \
792 { \
793 char *p; \
794 int sz; \
795 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
796 if (sz > MASK##n) TYERR("string too long"); \
797 GH_HASHBUF##W(GHASH_H(me), p, sz); \
798 RETURN_ME; \
799 end: \
800 return (0); \
801 }
802 DOUINTCONV(GHMETH_HASHBUF_)
803
804 static PyObject *ghmeth_hashstrz(PyObject *me, PyObject *arg)
805 {
806 char *p;
807 if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0);
808 GH_HASHSTRZ(GHASH_H(me), p);
809 RETURN_ME;
810 }
811
812 static PyMethodDef ghash_pymethods[] = {
813 #define METHNAME(name) ghmeth_##name
814 METH (hash, "H.hash(M)")
815 #define METHU_(n, W, w) METH(hashu##w, "H.hashu" #w "(WORD)")
816 DOUINTCONV(METHU_)
817 #define METHBUF_(n, W, w) METH(hashbuf##w, "H.hashbuf" #w "(BYTES)")
818 DOUINTCONV(METHBUF_)
819 METH (hashstrz, "H.hashstrz(STRING)")
820 METH (done, "H.done() -> HASH")
821 #undef METHNAME
822 { 0 }
823 };
824
825 static PyTypeObject gchash_pytype_skel = {
826 PyObject_HEAD_INIT(0) 0, /* Header */
827 "catacomb.GCHash", /* @tp_name@ */
828 sizeof(gchash_pyobj), /* @tp_basicsize@ */
829 0, /* @tp_itemsize@ */
830
831 0, /* @tp_dealloc@ */
832 0, /* @tp_print@ */
833 0, /* @tp_getattr@ */
834 0, /* @tp_setattr@ */
835 0, /* @tp_compare@ */
836 0, /* @tp_repr@ */
837 0, /* @tp_as_number@ */
838 0, /* @tp_as_sequence@ */
839 0, /* @tp_as_mapping@ */
840 0, /* @tp_hash@ */
841 0, /* @tp_call@ */
842 0, /* @tp_str@ */
843 0, /* @tp_getattro@ */
844 0, /* @tp_setattro@ */
845 0, /* @tp_as_buffer@ */
846 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
847 Py_TPFLAGS_BASETYPE,
848
849 /* @tp_doc@ */
850 "Hash function metaclass.",
851
852 0, /* @tp_traverse@ */
853 0, /* @tp_clear@ */
854 0, /* @tp_richcompare@ */
855 0, /* @tp_weaklistoffset@ */
856 0, /* @tp_iter@ */
857 0, /* @tp_iternext@ */
858 0, /* @tp_methods@ */
859 0, /* @tp_members@ */
860 gchash_pygetset, /* @tp_getset@ */
861 0, /* @tp_base@ */
862 0, /* @tp_dict@ */
863 0, /* @tp_descr_get@ */
864 0, /* @tp_descr_set@ */
865 0, /* @tp_dictoffset@ */
866 0, /* @tp_init@ */
867 PyType_GenericAlloc, /* @tp_alloc@ */
868 abstract_pynew, /* @tp_new@ */
869 0, /* @tp_free@ */
870 0 /* @tp_is_gc@ */
871 };
872
873 static PyTypeObject ghash_pytype_skel = {
874 PyObject_HEAD_INIT(0) 0, /* Header */
875 "catacomb.GHash", /* @tp_name@ */
876 sizeof(ghash_pyobj), /* @tp_basicsize@ */
877 0, /* @tp_itemsize@ */
878
879 ghash_pydealloc, /* @tp_dealloc@ */
880 0, /* @tp_print@ */
881 0, /* @tp_getattr@ */
882 0, /* @tp_setattr@ */
883 0, /* @tp_compare@ */
884 0, /* @tp_repr@ */
885 0, /* @tp_as_number@ */
886 0, /* @tp_as_sequence@ */
887 0, /* @tp_as_mapping@ */
888 0, /* @tp_hash@ */
889 0, /* @tp_call@ */
890 0, /* @tp_str@ */
891 0, /* @tp_getattro@ */
892 0, /* @tp_setattro@ */
893 0, /* @tp_as_buffer@ */
894 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
895 Py_TPFLAGS_BASETYPE,
896
897 /* @tp_doc@ */
898 "Hash function, abstract base class.",
899
900 0, /* @tp_traverse@ */
901 0, /* @tp_clear@ */
902 0, /* @tp_richcompare@ */
903 0, /* @tp_weaklistoffset@ */
904 0, /* @tp_iter@ */
905 0, /* @tp_iternext@ */
906 ghash_pymethods, /* @tp_methods@ */
907 0, /* @tp_members@ */
908 0, /* @tp_getset@ */
909 0, /* @tp_base@ */
910 0, /* @tp_dict@ */
911 0, /* @tp_descr_get@ */
912 0, /* @tp_descr_set@ */
913 0, /* @tp_dictoffset@ */
914 0, /* @tp_init@ */
915 PyType_GenericAlloc, /* @tp_alloc@ */
916 abstract_pynew, /* @tp_new@ */
917 0, /* @tp_free@ */
918 0 /* @tp_is_gc@ */
919 };
920
921 /*----- Message authentication --------------------------------------------*/
922
923 PyTypeObject *gcmac_pytype, *gmac_pytype, *gmhash_pytype;
924
925 CONVFUNC(gcmac, gcmac *, GCMAC_CM)
926 CONVFUNC(gmac, gmac *, GMAC_M)
927 CONVFUNC(gmhash, ghash *, GHASH_H)
928
929 static PyObject *gmac_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
930 {
931 char *kwlist[] = { "k", 0 };
932 char *k;
933 int sz;
934
935 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz))
936 goto end;
937 if (keysz(sz, GCMAC_CM(ty)->keysz) != sz) VALERR("bad key length");
938 return (gmac_pywrap((PyObject *)ty,
939 GM_KEY(GCMAC_CM(ty), k, sz),
940 f_freeme));
941 end:
942 return (0);
943 }
944
945 static PyObject *gmhash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
946 {
947 char *kwlist[] = { 0 };
948 ghash_pyobj *g;
949
950 if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", kwlist)) return (0);
951 g = PyObject_NEW(ghash_pyobj, ty);
952 g->h = GM_INIT(GMAC_M(ty));
953 g->f = f_freeme;
954 Py_INCREF(ty);
955 return ((PyObject *)g);
956 }
957
958 PyObject *gcmac_pywrap(gcmac *cm)
959 {
960 gcmac_pyobj *g = newtype(gcmac_pytype, 0, cm->name);
961 g->cm = cm;
962 g->ty.ht_type.tp_basicsize = sizeof(gmac_pyobj);
963 g->ty.ht_type.tp_base = gmac_pytype;
964 Py_INCREF(gmac_pytype);
965 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
966 Py_TPFLAGS_BASETYPE |
967 Py_TPFLAGS_HEAPTYPE);
968 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
969 g->ty.ht_type.tp_free = 0;
970 g->ty.ht_type.tp_new = gmac_pynew;
971 PyType_Ready(&g->ty.ht_type);
972 return ((PyObject *)g);
973 }
974
975 PyObject *gmac_pywrap(PyObject *cobj, gmac *m, unsigned f)
976 {
977 gmac_pyobj *g;
978 if (!cobj) cobj = gcmac_pywrap((/*unconst*/ gcmac *)GM_CLASS(m));
979 else Py_INCREF(cobj);
980 g = newtype((PyTypeObject *)cobj, 0, 0);
981 g->ty.ht_name = PyString_FromFormat("%s(keyed)", m->ops->c->name);
982 g->ty.ht_type.tp_name = PyString_AS_STRING(g->ty.ht_name);
983 g->ty.ht_type.tp_base = gmhash_pytype;
984 Py_INCREF(gmac_pytype);
985 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
986 Py_TPFLAGS_BASETYPE |
987 Py_TPFLAGS_HEAPTYPE);
988 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
989 g->ty.ht_type.tp_free = 0;
990 g->ty.ht_type.tp_new = gmhash_pynew;
991 PyType_Ready(&g->ty.ht_type);
992 g->m = m;
993 g->f = f;
994 return ((PyObject *)g);
995 }
996
997 static void gmac_pydealloc(PyObject *me)
998 {
999 if (GMAC_F(me) & f_freeme)
1000 GM_DESTROY(GMAC_M(me));
1001 Py_DECREF(me->ob_type);
1002 PyType_Type.tp_dealloc(me);
1003 }
1004
1005 static PyObject *gcmget_name(PyObject *me, void *hunoz)
1006 { return (PyString_FromString(GCMAC_CM(me)->name)); }
1007
1008 static PyObject *gcmget_keysz(PyObject *me, void *hunoz)
1009 { return (keysz_pywrap(GCMAC_CM(me)->keysz)); }
1010
1011 static PyObject *gcmget_tagsz(PyObject *me, void *hunoz)
1012 { return (PyInt_FromLong(GCMAC_CM(me)->hashsz)); }
1013
1014 static PyGetSetDef gcmac_pygetset[] = {
1015 #define GETSETNAME(op, name) gcm##op##_##name
1016 GET (keysz, "CM.keysz -> acceptable key sizes")
1017 GET (tagsz, "CM.tagsz -> MAC output size")
1018 GET (name, "CM.name -> name of this kind of MAC")
1019 #undef GETSETNAME
1020 { 0 }
1021 };
1022
1023 static PyTypeObject gcmac_pytype_skel = {
1024 PyObject_HEAD_INIT(0) 0, /* Header */
1025 "catacomb.GCMAC", /* @tp_name@ */
1026 sizeof(gchash_pyobj), /* @tp_basicsize@ */
1027 0, /* @tp_itemsize@ */
1028
1029 0, /* @tp_dealloc@ */
1030 0, /* @tp_print@ */
1031 0, /* @tp_getattr@ */
1032 0, /* @tp_setattr@ */
1033 0, /* @tp_compare@ */
1034 0, /* @tp_repr@ */
1035 0, /* @tp_as_number@ */
1036 0, /* @tp_as_sequence@ */
1037 0, /* @tp_as_mapping@ */
1038 0, /* @tp_hash@ */
1039 0, /* @tp_call@ */
1040 0, /* @tp_str@ */
1041 0, /* @tp_getattro@ */
1042 0, /* @tp_setattro@ */
1043 0, /* @tp_as_buffer@ */
1044 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1045 Py_TPFLAGS_BASETYPE,
1046
1047 /* @tp_doc@ */
1048 "Message authentication code metametaclass.",
1049
1050 0, /* @tp_traverse@ */
1051 0, /* @tp_clear@ */
1052 0, /* @tp_richcompare@ */
1053 0, /* @tp_weaklistoffset@ */
1054 0, /* @tp_iter@ */
1055 0, /* @tp_iternext@ */
1056 0, /* @tp_methods@ */
1057 0, /* @tp_members@ */
1058 gcmac_pygetset, /* @tp_getset@ */
1059 0, /* @tp_base@ */
1060 0, /* @tp_dict@ */
1061 0, /* @tp_descr_get@ */
1062 0, /* @tp_descr_set@ */
1063 0, /* @tp_dictoffset@ */
1064 0, /* @tp_init@ */
1065 PyType_GenericAlloc, /* @tp_alloc@ */
1066 abstract_pynew, /* @tp_new@ */
1067 0, /* @tp_free@ */
1068 0 /* @tp_is_gc@ */
1069 };
1070
1071 static PyTypeObject gmac_pytype_skel = {
1072 PyObject_HEAD_INIT(0) 0, /* Header */
1073 "catacomb.GMAC", /* @tp_name@ */
1074 sizeof(gmac_pyobj), /* @tp_basicsize@ */
1075 0, /* @tp_itemsize@ */
1076
1077 gmac_pydealloc, /* @tp_dealloc@ */
1078 0, /* @tp_print@ */
1079 0, /* @tp_getattr@ */
1080 0, /* @tp_setattr@ */
1081 0, /* @tp_compare@ */
1082 0, /* @tp_repr@ */
1083 0, /* @tp_as_number@ */
1084 0, /* @tp_as_sequence@ */
1085 0, /* @tp_as_mapping@ */
1086 0, /* @tp_hash@ */
1087 0, /* @tp_call@ */
1088 0, /* @tp_str@ */
1089 0, /* @tp_getattro@ */
1090 0, /* @tp_setattro@ */
1091 0, /* @tp_as_buffer@ */
1092 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1093 Py_TPFLAGS_BASETYPE,
1094
1095 /* @tp_doc@ */
1096 "Message authentication code metaclass, abstract base class.",
1097
1098 0, /* @tp_traverse@ */
1099 0, /* @tp_clear@ */
1100 0, /* @tp_richcompare@ */
1101 0, /* @tp_weaklistoffset@ */
1102 0, /* @tp_iter@ */
1103 0, /* @tp_iternext@ */
1104 0, /* @tp_methods@ */
1105 0, /* @tp_members@ */
1106 0, /* @tp_getset@ */
1107 0, /* @tp_base@ */
1108 0, /* @tp_dict@ */
1109 0, /* @tp_descr_get@ */
1110 0, /* @tp_descr_set@ */
1111 0, /* @tp_dictoffset@ */
1112 0, /* @tp_init@ */
1113 PyType_GenericAlloc, /* @tp_alloc@ */
1114 abstract_pynew, /* @tp_new@ */
1115 0, /* @tp_free@ */
1116 0 /* @tp_is_gc@ */
1117 };
1118
1119 static PyTypeObject gmhash_pytype_skel = {
1120 PyObject_HEAD_INIT(0) 0, /* Header */
1121 "catacomb.GMACHash", /* @tp_name@ */
1122 sizeof(ghash_pyobj), /* @tp_basicsize@ */
1123 0, /* @tp_itemsize@ */
1124
1125 ghash_pydealloc, /* @tp_dealloc@ */
1126 0, /* @tp_print@ */
1127 0, /* @tp_getattr@ */
1128 0, /* @tp_setattr@ */
1129 0, /* @tp_compare@ */
1130 0, /* @tp_repr@ */
1131 0, /* @tp_as_number@ */
1132 0, /* @tp_as_sequence@ */
1133 0, /* @tp_as_mapping@ */
1134 0, /* @tp_hash@ */
1135 0, /* @tp_call@ */
1136 0, /* @tp_str@ */
1137 0, /* @tp_getattro@ */
1138 0, /* @tp_setattro@ */
1139 0, /* @tp_as_buffer@ */
1140 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1141 Py_TPFLAGS_BASETYPE,
1142
1143 /* @tp_doc@ */
1144 "Message authentication code, abstract base class.",
1145
1146 0, /* @tp_traverse@ */
1147 0, /* @tp_clear@ */
1148 0, /* @tp_richcompare@ */
1149 0, /* @tp_weaklistoffset@ */
1150 0, /* @tp_iter@ */
1151 0, /* @tp_iternext@ */
1152 0, /* @tp_methods@ */
1153 0, /* @tp_members@ */
1154 0, /* @tp_getset@ */
1155 0, /* @tp_base@ */
1156 0, /* @tp_dict@ */
1157 0, /* @tp_descr_get@ */
1158 0, /* @tp_descr_set@ */
1159 0, /* @tp_dictoffset@ */
1160 0, /* @tp_init@ */
1161 PyType_GenericAlloc, /* @tp_alloc@ */
1162 abstract_pynew, /* @tp_new@ */
1163 0, /* @tp_free@ */
1164 0 /* @tp_is_gc@ */
1165 };
1166
1167 /*----- Pseudorandom permutations -----------------------------------------*/
1168
1169 static PyTypeObject *gcprp_pytype, *gprp_pytype;
1170
1171 typedef struct prpinfo {
1172 const char *name;
1173 const octet *keysz;
1174 size_t ctxsz;
1175 size_t blksz;
1176 void (*init)(void *, const void *, size_t);
1177 void (*eblk)(void *, const void *, void *);
1178 void (*dblk)(void *, const void *, void *);
1179 } prpinfo;
1180
1181 #define PRP_DEF(PRE, pre) \
1182 static void pre##_prpinit(void *ctx, const void *k, size_t ksz) \
1183 { pre##_init(ctx, k, ksz); } \
1184 static void pre##_prpeblk(void *ctx, const void *in, void *out) \
1185 { \
1186 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
1187 pre##_eblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
1188 } \
1189 static void pre##_prpdblk(void *ctx, const void *in, void *out) \
1190 { \
1191 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
1192 pre##_dblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
1193 } \
1194 static const prpinfo pre##_prpinfo = { \
1195 #pre, pre##_keysz, sizeof(pre##_ctx), PRE##_BLKSZ, \
1196 pre##_prpinit, pre##_prpeblk, pre##_prpdblk \
1197 };
1198 PRPS(PRP_DEF)
1199
1200 static const struct prpinfo *const gprptab[] = {
1201 #define PRP_ENTRY(PRE, pre) &pre##_prpinfo,
1202 PRPS(PRP_ENTRY)
1203 0
1204 };
1205
1206 typedef struct gcprp_pyobj {
1207 PyHeapTypeObject ty;
1208 const prpinfo *prp;
1209 } gcprp_pyobj;
1210 #define GCPRP_PRP(o) (((gcprp_pyobj *)(o))->prp)
1211
1212 typedef struct gprp_pyobj {
1213 PyObject_HEAD
1214 const prpinfo *prp;
1215 } gprp_pyobj;
1216 #define GPRP_PRP(o) (((gprp_pyobj *)(o))->prp)
1217 #define GPRP_CTX(o) (((gprp_pyobj *)(o)) + 1)
1218
1219 typedef struct prp {
1220 const prpinfo *prp;
1221 void *ctx;
1222 } prp;
1223
1224 static PyObject *gprp_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1225 {
1226 char *kwlist[] = { "key", 0 };
1227 char *k;
1228 int sz;
1229 const prpinfo *prp = GCPRP_PRP(ty);
1230 PyObject *me;
1231
1232 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz))
1233 goto end;
1234 if (keysz(sz, prp->keysz) != sz) VALERR("bad key length");
1235 me = (PyObject *)ty->tp_alloc(ty, 0);
1236 GPRP_PRP(me) = prp;
1237 prp->init(GPRP_CTX(me), k, sz);
1238 Py_INCREF(me);
1239 return (me);
1240 end:
1241 return (0);
1242 }
1243
1244 static void gprp_pydealloc(PyObject *me)
1245 { Py_DECREF(me->ob_type); FREEOBJ(me); }
1246
1247 static PyObject *gcprp_pywrap(const prpinfo *prp)
1248 {
1249 gcprp_pyobj *g = newtype(gcprp_pytype, 0, prp->name);
1250 g->prp = prp;
1251 g->ty.ht_type.tp_basicsize = sizeof(gprp_pyobj) + prp->ctxsz;
1252 g->ty.ht_type.tp_base = gprp_pytype;
1253 Py_INCREF(gprp_pytype);
1254 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
1255 Py_TPFLAGS_BASETYPE |
1256 Py_TPFLAGS_HEAPTYPE);
1257 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
1258 g->ty.ht_type.tp_free = 0;
1259 g->ty.ht_type.tp_new = gprp_pynew;
1260 PyType_Ready(&g->ty.ht_type);
1261 return ((PyObject *)g);
1262 }
1263
1264 static PyObject *gcpget_name(PyObject *me, void *hunoz)
1265 { return (PyString_FromString(GCPRP_PRP(me)->name)); }
1266 static PyObject *gcpget_keysz(PyObject *me, void *hunoz)
1267 { return (keysz_pywrap(GCPRP_PRP(me)->keysz)); }
1268 static PyObject *gcpget_blksz(PyObject *me, void *hunoz)
1269 { return (PyInt_FromLong(GCPRP_PRP(me)->blksz)); }
1270
1271 static PyObject *gpmeth_encrypt(PyObject *me, PyObject *arg)
1272 {
1273 char *p;
1274 int n;
1275 PyObject *rc = 0;
1276
1277 if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &n)) goto end;
1278 if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length");
1279 rc = bytestring_pywrap(0, n);
1280 GPRP_PRP(me)->eblk(GPRP_CTX(me), p, PyString_AS_STRING(rc));
1281 end:
1282 return (rc);
1283 }
1284
1285 static PyObject *gpmeth_decrypt(PyObject *me, PyObject *arg)
1286 {
1287 char *p;
1288 int n;
1289 PyObject *rc = 0;
1290
1291 if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &n)) goto end;
1292 if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length");
1293 rc = bytestring_pywrap(0, n);
1294 GPRP_PRP(me)->dblk(GPRP_CTX(me), p, PyString_AS_STRING(rc));
1295 end:
1296 return (rc);
1297 }
1298
1299 static PyGetSetDef gcprp_pygetset[] = {
1300 #define GETSETNAME(op, name) gcp##op##_##name
1301 GET (keysz, "CP.keysz -> acceptable key sizes")
1302 GET (blksz, "CP.blksz -> block size")
1303 GET (name, "CP.name -> name of this kind of PRP")
1304 #undef GETSETNAME
1305 { 0 }
1306 };
1307
1308 static PyMethodDef gprp_pymethods[] = {
1309 #define METHNAME(name) gpmeth_##name
1310 METH (encrypt, "P.encrypt(PT) -> CT")
1311 METH (decrypt, "P.decrypt(CT) -> PT")
1312 #undef METHNAME
1313 { 0 }
1314 };
1315
1316 static PyTypeObject gcprp_pytype_skel = {
1317 PyObject_HEAD_INIT(0) 0, /* Header */
1318 "catacomb.GCPRP", /* @tp_name@ */
1319 sizeof(gcprp_pyobj), /* @tp_basicsize@ */
1320 0, /* @tp_itemsize@ */
1321
1322 0, /* @tp_dealloc@ */
1323 0, /* @tp_print@ */
1324 0, /* @tp_getattr@ */
1325 0, /* @tp_setattr@ */
1326 0, /* @tp_compare@ */
1327 0, /* @tp_repr@ */
1328 0, /* @tp_as_number@ */
1329 0, /* @tp_as_sequence@ */
1330 0, /* @tp_as_mapping@ */
1331 0, /* @tp_hash@ */
1332 0, /* @tp_call@ */
1333 0, /* @tp_str@ */
1334 0, /* @tp_getattro@ */
1335 0, /* @tp_setattro@ */
1336 0, /* @tp_as_buffer@ */
1337 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1338 Py_TPFLAGS_BASETYPE,
1339
1340 /* @tp_doc@ */
1341 "Pseudorandom permutation metaclass.",
1342
1343 0, /* @tp_traverse@ */
1344 0, /* @tp_clear@ */
1345 0, /* @tp_richcompare@ */
1346 0, /* @tp_weaklistoffset@ */
1347 0, /* @tp_iter@ */
1348 0, /* @tp_iternext@ */
1349 0, /* @tp_methods@ */
1350 0, /* @tp_members@ */
1351 gcprp_pygetset, /* @tp_getset@ */
1352 0, /* @tp_base@ */
1353 0, /* @tp_dict@ */
1354 0, /* @tp_descr_get@ */
1355 0, /* @tp_descr_set@ */
1356 0, /* @tp_dictoffset@ */
1357 0, /* @tp_init@ */
1358 PyType_GenericAlloc, /* @tp_alloc@ */
1359 abstract_pynew, /* @tp_new@ */
1360 0, /* @tp_free@ */
1361 0 /* @tp_is_gc@ */
1362 };
1363
1364 static PyTypeObject gprp_pytype_skel = {
1365 PyObject_HEAD_INIT(0) 0, /* Header */
1366 "catacomb.GPRP", /* @tp_name@ */
1367 sizeof(gprp_pyobj), /* @tp_basicsize@ */
1368 0, /* @tp_itemsize@ */
1369
1370 gprp_pydealloc, /* @tp_dealloc@ */
1371 0, /* @tp_print@ */
1372 0, /* @tp_getattr@ */
1373 0, /* @tp_setattr@ */
1374 0, /* @tp_compare@ */
1375 0, /* @tp_repr@ */
1376 0, /* @tp_as_number@ */
1377 0, /* @tp_as_sequence@ */
1378 0, /* @tp_as_mapping@ */
1379 0, /* @tp_hash@ */
1380 0, /* @tp_call@ */
1381 0, /* @tp_str@ */
1382 0, /* @tp_getattro@ */
1383 0, /* @tp_setattro@ */
1384 0, /* @tp_as_buffer@ */
1385 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1386 Py_TPFLAGS_BASETYPE,
1387
1388 /* @tp_doc@ */
1389 "Pseudorandom permutation, abstract base class.",
1390
1391 0, /* @tp_traverse@ */
1392 0, /* @tp_clear@ */
1393 0, /* @tp_richcompare@ */
1394 0, /* @tp_weaklistoffset@ */
1395 0, /* @tp_iter@ */
1396 0, /* @tp_iternext@ */
1397 gprp_pymethods, /* @tp_methods@ */
1398 0, /* @tp_members@ */
1399 0, /* @tp_getset@ */
1400 0, /* @tp_base@ */
1401 0, /* @tp_dict@ */
1402 0, /* @tp_descr_get@ */
1403 0, /* @tp_descr_set@ */
1404 0, /* @tp_dictoffset@ */
1405 0, /* @tp_init@ */
1406 PyType_GenericAlloc, /* @tp_alloc@ */
1407 abstract_pynew, /* @tp_new@ */
1408 0, /* @tp_free@ */
1409 0 /* @tp_is_gc@ */
1410 };
1411
1412 /*----- Main code ---------------------------------------------------------*/
1413
1414 void algorithms_pyinit(void)
1415 {
1416 INITTYPE(keysz, root);
1417 INITTYPE(keyszany, keysz);
1418 INITTYPE(keyszrange, keysz);
1419 INITTYPE(keyszset, keysz);
1420 INITTYPE(gccipher, type);
1421 INITTYPE(gcipher, root);
1422 INITTYPE(gchash, type);
1423 INITTYPE(ghash, root);
1424 INITTYPE(gcmac, type);
1425 INITTYPE(gmac, type);
1426 INITTYPE(gmhash, ghash);
1427 INITTYPE(gcprp, type);
1428 INITTYPE(gprp, root);
1429 }
1430
1431 GEN(gcciphers, cipher)
1432 GEN(gchashes, hash)
1433 GEN(gcmacs, mac)
1434 #define gcprp prpinfo
1435 GEN(gcprps, prp)
1436
1437 void algorithms_pyinsert(PyObject *mod)
1438 {
1439 PyObject *d;
1440 INSERT("KeySZ", keysz_pytype);
1441 INSERT("KeySZAny", keyszany_pytype);
1442 INSERT("KeySZRange", keyszrange_pytype);
1443 INSERT("KeySZSet", keyszset_pytype);
1444 INSERT("GCCipher", gccipher_pytype);
1445 INSERT("GCipher", gcipher_pytype);
1446 INSERT("gcciphers", gcciphers());
1447 INSERT("GCHash", gchash_pytype);
1448 INSERT("GHash", ghash_pytype);
1449 INSERT("gchashes", d = gchashes());
1450 sha_pyobj = PyDict_GetItemString(d, "sha"); Py_INCREF(sha_pyobj);
1451 has160_pyobj = PyDict_GetItemString(d, "has160"); Py_INCREF(has160_pyobj);
1452 INSERT("GCMAC", gcmac_pytype);
1453 INSERT("GMAC", gmac_pytype);
1454 INSERT("GMACHash", gmhash_pytype);
1455 INSERT("gcmacs", gcmacs());
1456 INSERT("GCPRP", gcprp_pytype);
1457 INSERT("GPRP", gprp_pytype);
1458 INSERT("gcprps", gcprps());
1459 }
1460
1461 /*----- That's all, folks -------------------------------------------------*/