3 * Symmetric cryptography
5 * (c) 2004 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Python interface to Catacomb.
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.
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.
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.
27 /*----- Header files ------------------------------------------------------*/
29 #include "catacomb-python.h"
30 #include "algorithms.h"
32 /*----- Key sizes ---------------------------------------------------------*/
34 PyTypeObject *keysz_pytype;
35 PyTypeObject *keyszany_pytype, *keyszrange_pytype, *keyszset_pytype;
36 PyObject *sha_pyobj, *has160_pyobj;
38 PyObject *keysz_pywrap(const octet *k)
42 keysz_pyobj *o = PyObject_New(keysz_pyobj, keyszany_pytype);
44 return ((PyObject *)o);
48 PyObject_New(keyszrange_pyobj, keyszrange_pytype);
53 if (!o->mod) o->mod = 1;
54 return ((PyObject *)o);
58 PyObject_New(keyszset_pyobj, keyszset_pytype);
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);
72 static PyObject *keyszany_pynew(PyTypeObject *ty,
73 PyObject *arg, PyObject *kw)
75 char *kwlist[] = { "default", 0 };
79 if (!PyArg_ParseTupleAndKeywords(arg, kw, "i:new", kwlist, &dfl))
81 if (dfl < 0) VALERR("key size cannot be negative");
82 o = (keysz_pyobj *)ty->tp_alloc(ty, 0);
84 return ((PyObject *)o);
89 static PyObject *keyszrange_pynew(PyTypeObject *ty,
90 PyObject *arg, PyObject *kw)
92 char *kwlist[] = { "default", "min", "max", "mod", 0 };
93 int dfl, min = 0, max = 0, mod = 1;
96 if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|iii:new", kwlist,
97 &dfl, &min, &max, &mod))
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);
110 return ((PyObject *)o);
115 static PyObject *keyszset_pynew(PyTypeObject *ty,
116 PyObject *arg, PyObject *kw)
118 char *kwlist[] = { "default", "set", 0 };
121 PyObject *x = 0, *l = 0;
122 keyszset_pyobj *o = 0;
124 if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|O:new", kwlist,
127 if (!set) set = PyTuple_New(0);
129 if (!PySequence_Check(set)) TYERR("want a sequence");
130 n = PySequence_Size(set);
132 if (PyErr_Occurred()) goto end;
133 if (dfl < 0) VALERR("key size cannot be negative");
134 x = PyInt_FromLong(dfl);
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");
149 if ((set = PySequence_Tuple(l)) == 0) goto end;
150 o = (keyszset_pyobj *)ty->tp_alloc(ty, 0);
158 return ((PyObject *)o);
161 static PyObject *kaget_min(PyObject *me, void *hunoz)
162 { return (PyInt_FromLong(0)); }
163 #define kaget_max kaget_min
165 static PyObject *ksget_min(PyObject *me, void *hunoz)
167 PyObject *set = ((keyszset_pyobj *)me)->set;
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;
174 return (PyInt_FromLong(x));
177 static PyObject *ksget_max(PyObject *me, void *hunoz)
179 PyObject *set = ((keyszset_pyobj *)me)->set;
181 n = PyTuple_Size(set);
182 for (i = 0; i < n; i++) {
183 y = PyInt_AsLong(PyTuple_GetItem(set, i));
186 return (PyInt_FromLong(x));
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")
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")
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")
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")
224 static PyMemberDef keyszset_pymembers[] = {
225 #define MEMBERSTRUCT keyszset_pyobj
226 MEMBER(set, T_OBJECT, READONLY, "KSZ.set -> allowed key sizes")
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@ */
237 0, /* @tp_dealloc@ */
239 0, /* @tp_getattr@ */
240 0, /* @tp_setattr@ */
241 0, /* @tp_compare@ */
243 0, /* @tp_as_number@ */
244 0, /* @tp_as_sequence@ */
245 0, /* @tp_as_mapping@ */
249 0, /* @tp_getattro@ */
250 0, /* @tp_setattro@ */
251 0, /* @tp_as_buffer@ */
252 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
256 "Key size constraints.",
258 0, /* @tp_traverse@ */
260 0, /* @tp_richcompare@ */
261 0, /* @tp_weaklistoffset@ */
263 0, /* @tp_iternext@ */
264 0, /* @tp_methods@ */
265 keysz_pymembers, /* @tp_members@ */
269 0, /* @tp_descr_get@ */
270 0, /* @tp_descr_set@ */
271 0, /* @tp_dictoffset@ */
273 PyType_GenericAlloc, /* @tp_alloc@ */
274 abstract_pynew, /* @tp_new@ */
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@ */
285 0, /* @tp_dealloc@ */
287 0, /* @tp_getattr@ */
288 0, /* @tp_setattr@ */
289 0, /* @tp_compare@ */
291 0, /* @tp_as_number@ */
292 0, /* @tp_as_sequence@ */
293 0, /* @tp_as_mapping@ */
297 0, /* @tp_getattro@ */
298 0, /* @tp_setattro@ */
299 0, /* @tp_as_buffer@ */
300 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
304 "Key size constraints. This object imposes no constraints on size.",
306 0, /* @tp_traverse@ */
308 0, /* @tp_richcompare@ */
309 0, /* @tp_weaklistoffset@ */
311 0, /* @tp_iternext@ */
312 0, /* @tp_methods@ */
313 0, /* @tp_members@ */
314 keyszany_pygetset, /* @tp_getset@ */
317 0, /* @tp_descr_get@ */
318 0, /* @tp_descr_set@ */
319 0, /* @tp_dictoffset@ */
321 PyType_GenericAlloc, /* @tp_alloc@ */
322 keyszany_pynew, /* @tp_new@ */
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@ */
333 0, /* @tp_dealloc@ */
335 0, /* @tp_getattr@ */
336 0, /* @tp_setattr@ */
337 0, /* @tp_compare@ */
339 0, /* @tp_as_number@ */
340 0, /* @tp_as_sequence@ */
341 0, /* @tp_as_mapping@ */
345 0, /* @tp_getattro@ */
346 0, /* @tp_setattro@ */
347 0, /* @tp_as_buffer@ */
348 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
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.",
355 0, /* @tp_traverse@ */
357 0, /* @tp_richcompare@ */
358 0, /* @tp_weaklistoffset@ */
360 0, /* @tp_iternext@ */
361 0, /* @tp_methods@ */
362 keyszrange_pymembers, /* @tp_members@ */
366 0, /* @tp_descr_get@ */
367 0, /* @tp_descr_set@ */
368 0, /* @tp_dictoffset@ */
370 PyType_GenericAlloc, /* @tp_alloc@ */
371 keyszrange_pynew, /* @tp_new@ */
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@ */
382 0, /* @tp_dealloc@ */
384 0, /* @tp_getattr@ */
385 0, /* @tp_setattr@ */
386 0, /* @tp_compare@ */
388 0, /* @tp_as_number@ */
389 0, /* @tp_as_sequence@ */
390 0, /* @tp_as_mapping@ */
394 0, /* @tp_getattro@ */
395 0, /* @tp_setattro@ */
396 0, /* @tp_as_buffer@ */
397 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
401 "Key size constraints. This object requires the key to be one of a\n\
404 0, /* @tp_traverse@ */
406 0, /* @tp_richcompare@ */
407 0, /* @tp_weaklistoffset@ */
409 0, /* @tp_iternext@ */
410 0, /* @tp_methods@ */
411 keyszset_pymembers, /* @tp_members@ */
412 keyszset_pygetset, /* @tp_getset@ */
415 0, /* @tp_descr_get@ */
416 0, /* @tp_descr_set@ */
417 0, /* @tp_dictoffset@ */
419 PyType_GenericAlloc, /* @tp_alloc@ */
420 keyszset_pynew, /* @tp_new@ */
425 /*----- Symmetric encryption ----------------------------------------------*/
427 PyTypeObject *gccipher_pytype, *gcipher_pytype;
429 CONVFUNC(gccipher, gccipher *, GCCIPHER_CC)
430 CONVFUNC(gcipher, gcipher *, GCIPHER_C)
432 PyObject *gcipher_pywrap(PyObject *cobj, gcipher *c, unsigned f)
435 if (!cobj) cobj = gccipher_pywrap((/*unconst*/ gccipher *)GC_CLASS(c));
436 else Py_INCREF(cobj);
437 g = PyObject_NEW(gcipher_pyobj, (PyTypeObject *)cobj);
440 return ((PyObject *)g);
443 static PyObject *gcipher_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
445 char *kwlist[] = { "k", 0 };
449 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz))
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),
459 PyObject *gccipher_pywrap(gccipher *cc)
461 gccipher_pyobj *g = newtype(gccipher_pytype, 0, cc->name);
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);
476 static void gcipher_pydealloc(PyObject *me)
478 if (GCIPHER_F(me) & f_freeme)
479 GC_DESTROY(GCIPHER_C(me));
480 Py_DECREF(me->ob_type);
484 static PyObject *gccget_name(PyObject *me, void *hunoz)
485 { return (PyString_FromString(GCCIPHER_CC(me)->name)); }
487 static PyObject *gccget_keysz(PyObject *me, void *hunoz)
488 { return (keysz_pywrap(GCCIPHER_CC(me)->keysz)); }
490 static PyObject *gccget_blksz(PyObject *me, void *hunoz)
491 { return (PyInt_FromLong(GCCIPHER_CC(me)->blksz)); }
493 static PyObject *gcmeth_encrypt(PyObject *me, PyObject *arg)
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);
505 static PyObject *gcmeth_enczero(PyObject *me, PyObject *arg)
511 if (!PyArg_ParseTuple(arg, "i:enczero", &sz)) return (0);
512 rc = bytestring_pywrap(0, sz);
513 p = PyString_AS_STRING(rc);
515 GC_ENCRYPT(GCIPHER_C(me), p, p, sz);
519 static PyObject *gcmeth_decrypt(PyObject *me, PyObject *arg)
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);
531 static PyObject *gcmeth_deczero(PyObject *me, PyObject *arg)
537 if (!PyArg_ParseTuple(arg, "i:deczero", &sz)) return (0);
538 rc = bytestring_pywrap(0, sz);
539 p = PyString_AS_STRING(rc);
541 GC_DECRYPT(GCIPHER_C(me), p, p, sz);
545 static PyObject *gcmeth_setiv(PyObject *me, PyObject *arg)
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);
559 static PyObject *gcmeth_bdry(PyObject *me, PyObject *arg)
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));
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")
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()")
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@ */
596 0, /* @tp_dealloc@ */
598 0, /* @tp_getattr@ */
599 0, /* @tp_setattr@ */
600 0, /* @tp_compare@ */
602 0, /* @tp_as_number@ */
603 0, /* @tp_as_sequence@ */
604 0, /* @tp_as_mapping@ */
608 0, /* @tp_getattro@ */
609 0, /* @tp_setattro@ */
610 0, /* @tp_as_buffer@ */
611 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
615 "Symmetric cipher metaclass.",
617 0, /* @tp_traverse@ */
619 0, /* @tp_richcompare@ */
620 0, /* @tp_weaklistoffset@ */
622 0, /* @tp_iternext@ */
623 0, /* @tp_methods@ */
624 0, /* @tp_members@ */
625 gccipher_pygetset, /* @tp_getset@ */
628 0, /* @tp_descr_get@ */
629 0, /* @tp_descr_set@ */
630 0, /* @tp_dictoffset@ */
632 PyType_GenericAlloc, /* @tp_alloc@ */
633 abstract_pynew, /* @tp_new@ */
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@ */
644 gcipher_pydealloc, /* @tp_dealloc@ */
646 0, /* @tp_getattr@ */
647 0, /* @tp_setattr@ */
648 0, /* @tp_compare@ */
650 0, /* @tp_as_number@ */
651 0, /* @tp_as_sequence@ */
652 0, /* @tp_as_mapping@ */
656 0, /* @tp_getattro@ */
657 0, /* @tp_setattro@ */
658 0, /* @tp_as_buffer@ */
659 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
663 "Symmetric cipher, abstract base class.",
665 0, /* @tp_traverse@ */
667 0, /* @tp_richcompare@ */
668 0, /* @tp_weaklistoffset@ */
670 0, /* @tp_iternext@ */
671 gcipher_pymethods, /* @tp_methods@ */
672 0, /* @tp_members@ */
676 0, /* @tp_descr_get@ */
677 0, /* @tp_descr_set@ */
678 0, /* @tp_dictoffset@ */
680 PyType_GenericAlloc, /* @tp_alloc@ */
681 abstract_pynew, /* @tp_new@ */
686 /*----- Hash functions ----------------------------------------------------*/
688 PyTypeObject *gchash_pytype, *ghash_pytype;
690 CONVFUNC(gchash, gchash *, GCHASH_CH)
691 CONVFUNC(ghash, ghash *, GHASH_H)
693 static PyObject *ghash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
695 char *kwlist[] = { 0 };
696 if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", kwlist))
698 return (ghash_pywrap((PyObject *)ty, GH_INIT(GCHASH_CH(ty)), f_freeme));
703 PyObject *gchash_pywrap(gchash *ch)
705 gchash_pyobj *g = newtype(gchash_pytype, 0, ch->name);
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);
720 PyObject *ghash_pywrap(PyObject *cobj, ghash *h, unsigned f)
723 if (!cobj) cobj = gchash_pywrap((/*unconst*/ gchash *)GH_CLASS(h));
724 else Py_INCREF(cobj);
725 g = PyObject_NEW(ghash_pyobj, (PyTypeObject *)cobj);
728 return ((PyObject *)g);
731 static void ghash_pydealloc(PyObject *me)
733 if (GHASH_F(me) & f_freeme)
734 GH_DESTROY(GHASH_H(me));
735 Py_DECREF(me->ob_type);
739 static PyObject *gchget_name(PyObject *me, void *hunoz)
740 { return (PyString_FromString(GCHASH_CH(me)->name)); }
742 static PyObject *gchget_hashsz(PyObject *me, void *hunoz)
743 { return (PyInt_FromLong(GCHASH_CH(me)->hashsz)); }
745 static PyObject *gchget_bufsz(PyObject *me, void *hunoz)
746 { return (PyInt_FromLong(GCHASH_CH(me)->bufsz)); }
748 static PyObject *ghmeth_hash(PyObject *me, PyObject *arg)
752 if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0);
753 GH_HASH(GHASH_H(me), p, sz);
757 static PyObject *ghmeth_done(PyObject *me, PyObject *arg)
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));
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")
778 #define GHMETH_HASHU_(n, W, w) \
779 static PyObject *ghmeth_hashu##w(PyObject *me, PyObject *arg) \
782 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \
783 GH_HASHU##W(GHASH_H(me), x); \
788 DOUINTCONV(GHMETH_HASHU_)
790 #define GHMETH_HASHBUF_(n, W, w) \
791 static PyObject *ghmeth_hashbuf##w(PyObject *me, PyObject *arg) \
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); \
802 DOUINTCONV(GHMETH_HASHBUF_)
804 static PyObject *ghmeth_hashstrz(PyObject *me, PyObject *arg)
807 if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0);
808 GH_HASHSTRZ(GHASH_H(me), p);
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)")
817 #define METHBUF_(n, W, w) METH(hashbuf##w, "H.hashbuf" #w "(BYTES)")
819 METH (hashstrz, "H.hashstrz(STRING)")
820 METH (done, "H.done() -> HASH")
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@ */
831 0, /* @tp_dealloc@ */
833 0, /* @tp_getattr@ */
834 0, /* @tp_setattr@ */
835 0, /* @tp_compare@ */
837 0, /* @tp_as_number@ */
838 0, /* @tp_as_sequence@ */
839 0, /* @tp_as_mapping@ */
843 0, /* @tp_getattro@ */
844 0, /* @tp_setattro@ */
845 0, /* @tp_as_buffer@ */
846 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
850 "Hash function metaclass.",
852 0, /* @tp_traverse@ */
854 0, /* @tp_richcompare@ */
855 0, /* @tp_weaklistoffset@ */
857 0, /* @tp_iternext@ */
858 0, /* @tp_methods@ */
859 0, /* @tp_members@ */
860 gchash_pygetset, /* @tp_getset@ */
863 0, /* @tp_descr_get@ */
864 0, /* @tp_descr_set@ */
865 0, /* @tp_dictoffset@ */
867 PyType_GenericAlloc, /* @tp_alloc@ */
868 abstract_pynew, /* @tp_new@ */
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@ */
879 ghash_pydealloc, /* @tp_dealloc@ */
881 0, /* @tp_getattr@ */
882 0, /* @tp_setattr@ */
883 0, /* @tp_compare@ */
885 0, /* @tp_as_number@ */
886 0, /* @tp_as_sequence@ */
887 0, /* @tp_as_mapping@ */
891 0, /* @tp_getattro@ */
892 0, /* @tp_setattro@ */
893 0, /* @tp_as_buffer@ */
894 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
898 "Hash function, abstract base class.",
900 0, /* @tp_traverse@ */
902 0, /* @tp_richcompare@ */
903 0, /* @tp_weaklistoffset@ */
905 0, /* @tp_iternext@ */
906 ghash_pymethods, /* @tp_methods@ */
907 0, /* @tp_members@ */
911 0, /* @tp_descr_get@ */
912 0, /* @tp_descr_set@ */
913 0, /* @tp_dictoffset@ */
915 PyType_GenericAlloc, /* @tp_alloc@ */
916 abstract_pynew, /* @tp_new@ */
921 /*----- Message authentication --------------------------------------------*/
923 PyTypeObject *gcmac_pytype, *gmac_pytype, *gmhash_pytype;
925 CONVFUNC(gcmac, gcmac *, GCMAC_CM)
926 CONVFUNC(gmac, gmac *, GMAC_M)
927 CONVFUNC(gmhash, ghash *, GHASH_H)
929 static PyObject *gmac_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
931 char *kwlist[] = { "k", 0 };
935 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz))
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),
945 static PyObject *gmhash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
947 char *kwlist[] = { 0 };
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));
955 return ((PyObject *)g);
958 PyObject *gcmac_pywrap(gcmac *cm)
960 gcmac_pyobj *g = newtype(gcmac_pytype, 0, cm->name);
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);
975 PyObject *gmac_pywrap(PyObject *cobj, gmac *m, unsigned f)
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);
994 return ((PyObject *)g);
997 static void gmac_pydealloc(PyObject *me)
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);
1005 static PyObject *gcmget_name(PyObject *me, void *hunoz)
1006 { return (PyString_FromString(GCMAC_CM(me)->name)); }
1008 static PyObject *gcmget_keysz(PyObject *me, void *hunoz)
1009 { return (keysz_pywrap(GCMAC_CM(me)->keysz)); }
1011 static PyObject *gcmget_tagsz(PyObject *me, void *hunoz)
1012 { return (PyInt_FromLong(GCMAC_CM(me)->hashsz)); }
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")
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@ */
1029 0, /* @tp_dealloc@ */
1031 0, /* @tp_getattr@ */
1032 0, /* @tp_setattr@ */
1033 0, /* @tp_compare@ */
1035 0, /* @tp_as_number@ */
1036 0, /* @tp_as_sequence@ */
1037 0, /* @tp_as_mapping@ */
1041 0, /* @tp_getattro@ */
1042 0, /* @tp_setattro@ */
1043 0, /* @tp_as_buffer@ */
1044 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1045 Py_TPFLAGS_BASETYPE,
1048 "Message authentication code metametaclass.",
1050 0, /* @tp_traverse@ */
1052 0, /* @tp_richcompare@ */
1053 0, /* @tp_weaklistoffset@ */
1055 0, /* @tp_iternext@ */
1056 0, /* @tp_methods@ */
1057 0, /* @tp_members@ */
1058 gcmac_pygetset, /* @tp_getset@ */
1061 0, /* @tp_descr_get@ */
1062 0, /* @tp_descr_set@ */
1063 0, /* @tp_dictoffset@ */
1065 PyType_GenericAlloc, /* @tp_alloc@ */
1066 abstract_pynew, /* @tp_new@ */
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@ */
1077 gmac_pydealloc, /* @tp_dealloc@ */
1079 0, /* @tp_getattr@ */
1080 0, /* @tp_setattr@ */
1081 0, /* @tp_compare@ */
1083 0, /* @tp_as_number@ */
1084 0, /* @tp_as_sequence@ */
1085 0, /* @tp_as_mapping@ */
1089 0, /* @tp_getattro@ */
1090 0, /* @tp_setattro@ */
1091 0, /* @tp_as_buffer@ */
1092 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1093 Py_TPFLAGS_BASETYPE,
1096 "Message authentication code metaclass, abstract base class.",
1098 0, /* @tp_traverse@ */
1100 0, /* @tp_richcompare@ */
1101 0, /* @tp_weaklistoffset@ */
1103 0, /* @tp_iternext@ */
1104 0, /* @tp_methods@ */
1105 0, /* @tp_members@ */
1106 0, /* @tp_getset@ */
1109 0, /* @tp_descr_get@ */
1110 0, /* @tp_descr_set@ */
1111 0, /* @tp_dictoffset@ */
1113 PyType_GenericAlloc, /* @tp_alloc@ */
1114 abstract_pynew, /* @tp_new@ */
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@ */
1125 ghash_pydealloc, /* @tp_dealloc@ */
1127 0, /* @tp_getattr@ */
1128 0, /* @tp_setattr@ */
1129 0, /* @tp_compare@ */
1131 0, /* @tp_as_number@ */
1132 0, /* @tp_as_sequence@ */
1133 0, /* @tp_as_mapping@ */
1137 0, /* @tp_getattro@ */
1138 0, /* @tp_setattro@ */
1139 0, /* @tp_as_buffer@ */
1140 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1141 Py_TPFLAGS_BASETYPE,
1144 "Message authentication code, abstract base class.",
1146 0, /* @tp_traverse@ */
1148 0, /* @tp_richcompare@ */
1149 0, /* @tp_weaklistoffset@ */
1151 0, /* @tp_iternext@ */
1152 0, /* @tp_methods@ */
1153 0, /* @tp_members@ */
1154 0, /* @tp_getset@ */
1157 0, /* @tp_descr_get@ */
1158 0, /* @tp_descr_set@ */
1159 0, /* @tp_dictoffset@ */
1161 PyType_GenericAlloc, /* @tp_alloc@ */
1162 abstract_pynew, /* @tp_new@ */
1167 /*----- Pseudorandom permutations -----------------------------------------*/
1169 static PyTypeObject *gcprp_pytype, *gprp_pytype;
1171 typedef struct prpinfo {
1176 void (*init)(void *, const void *, size_t);
1177 void (*eblk)(void *, const void *, void *);
1178 void (*dblk)(void *, const void *, void *);
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) \
1186 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
1187 pre##_eblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
1189 static void pre##_prpdblk(void *ctx, const void *in, void *out) \
1191 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
1192 pre##_dblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
1194 static const prpinfo pre##_prpinfo = { \
1195 #pre, pre##_keysz, sizeof(pre##_ctx), PRE##_BLKSZ, \
1196 pre##_prpinit, pre##_prpeblk, pre##_prpdblk \
1200 static const struct prpinfo *const gprptab[] = {
1201 #define PRP_ENTRY(PRE, pre) &pre##_prpinfo,
1206 typedef struct gcprp_pyobj {
1207 PyHeapTypeObject ty;
1210 #define GCPRP_PRP(o) (((gcprp_pyobj *)(o))->prp)
1212 typedef struct gprp_pyobj {
1216 #define GPRP_PRP(o) (((gprp_pyobj *)(o))->prp)
1217 #define GPRP_CTX(o) (((gprp_pyobj *)(o)) + 1)
1219 typedef struct prp {
1224 static PyObject *gprp_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1226 char *kwlist[] = { "key", 0 };
1229 const prpinfo *prp = GCPRP_PRP(ty);
1232 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz))
1234 if (keysz(sz, prp->keysz) != sz) VALERR("bad key length");
1235 me = (PyObject *)ty->tp_alloc(ty, 0);
1237 prp->init(GPRP_CTX(me), k, sz);
1244 static void gprp_pydealloc(PyObject *me)
1245 { Py_DECREF(me->ob_type); FREEOBJ(me); }
1247 static PyObject *gcprp_pywrap(const prpinfo *prp)
1249 gcprp_pyobj *g = newtype(gcprp_pytype, 0, prp->name);
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);
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)); }
1271 static PyObject *gpmeth_encrypt(PyObject *me, PyObject *arg)
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));
1285 static PyObject *gpmeth_decrypt(PyObject *me, PyObject *arg)
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));
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")
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")
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@ */
1322 0, /* @tp_dealloc@ */
1324 0, /* @tp_getattr@ */
1325 0, /* @tp_setattr@ */
1326 0, /* @tp_compare@ */
1328 0, /* @tp_as_number@ */
1329 0, /* @tp_as_sequence@ */
1330 0, /* @tp_as_mapping@ */
1334 0, /* @tp_getattro@ */
1335 0, /* @tp_setattro@ */
1336 0, /* @tp_as_buffer@ */
1337 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1338 Py_TPFLAGS_BASETYPE,
1341 "Pseudorandom permutation metaclass.",
1343 0, /* @tp_traverse@ */
1345 0, /* @tp_richcompare@ */
1346 0, /* @tp_weaklistoffset@ */
1348 0, /* @tp_iternext@ */
1349 0, /* @tp_methods@ */
1350 0, /* @tp_members@ */
1351 gcprp_pygetset, /* @tp_getset@ */
1354 0, /* @tp_descr_get@ */
1355 0, /* @tp_descr_set@ */
1356 0, /* @tp_dictoffset@ */
1358 PyType_GenericAlloc, /* @tp_alloc@ */
1359 abstract_pynew, /* @tp_new@ */
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@ */
1370 gprp_pydealloc, /* @tp_dealloc@ */
1372 0, /* @tp_getattr@ */
1373 0, /* @tp_setattr@ */
1374 0, /* @tp_compare@ */
1376 0, /* @tp_as_number@ */
1377 0, /* @tp_as_sequence@ */
1378 0, /* @tp_as_mapping@ */
1382 0, /* @tp_getattro@ */
1383 0, /* @tp_setattro@ */
1384 0, /* @tp_as_buffer@ */
1385 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1386 Py_TPFLAGS_BASETYPE,
1389 "Pseudorandom permutation, abstract base class.",
1391 0, /* @tp_traverse@ */
1393 0, /* @tp_richcompare@ */
1394 0, /* @tp_weaklistoffset@ */
1396 0, /* @tp_iternext@ */
1397 gprp_pymethods, /* @tp_methods@ */
1398 0, /* @tp_members@ */
1399 0, /* @tp_getset@ */
1402 0, /* @tp_descr_get@ */
1403 0, /* @tp_descr_set@ */
1404 0, /* @tp_dictoffset@ */
1406 PyType_GenericAlloc, /* @tp_alloc@ */
1407 abstract_pynew, /* @tp_new@ */
1412 /*----- Main code ---------------------------------------------------------*/
1414 void algorithms_pyinit(void)
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);
1431 GEN(gcciphers, cipher)
1434 #define gcprp prpinfo
1437 void algorithms_pyinsert(PyObject *mod)
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());
1461 /*----- That's all, folks -------------------------------------------------*/