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;
39 # define KSZ_OPMASK 0x1f
43 # define KSZ_16BIT 0x20
46 PyObject *keysz_pywrap(const octet *k)
49 #define ARG(i) (op&KSZ_16BIT ? LOAD16(k + 2*(i)) : k[i])
50 switch (op&KSZ_OPMASK) {
52 keysz_pyobj *o = PyObject_New(keysz_pyobj, keyszany_pytype);
54 return ((PyObject *)o);
58 PyObject_New(keyszrange_pyobj, keyszrange_pytype);
63 if (!o->mod) o->mod = 1;
64 return ((PyObject *)o);
68 PyObject_New(keyszset_pyobj, keyszset_pytype);
71 for (i = 0; ARG(i); i++) ;
72 n = i; o->set = PyTuple_New(n);
73 for (i = 0; i < n; i++)
74 PyTuple_SET_ITEM(o->set, i, PyInt_FromLong(ARG(i)));
75 return ((PyObject *)o);
83 static PyObject *keyszany_pynew(PyTypeObject *ty,
84 PyObject *arg, PyObject *kw)
86 char *kwlist[] = { "default", 0 };
90 if (!PyArg_ParseTupleAndKeywords(arg, kw, "i:new", kwlist, &dfl))
92 if (dfl < 0) VALERR("key size cannot be negative");
93 o = (keysz_pyobj *)ty->tp_alloc(ty, 0);
95 return ((PyObject *)o);
100 static PyObject *keyszrange_pynew(PyTypeObject *ty,
101 PyObject *arg, PyObject *kw)
103 char *kwlist[] = { "default", "min", "max", "mod", 0 };
104 int dfl, min = 0, max = 0, mod = 1;
107 if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|iii:new", kwlist,
108 &dfl, &min, &max, &mod))
110 if (dfl < 0 || min < 0 || max < 0)
111 VALERR("key size cannot be negative");
112 if (min > dfl || (max && dfl > max))
113 VALERR("bad key size bounds");
114 if (mod <= 0 || dfl % mod || min % mod || max % mod)
115 VALERR("bad key size modulus");
116 o = (keyszrange_pyobj *)ty->tp_alloc(ty, 0);
121 return ((PyObject *)o);
126 static PyObject *keyszset_pynew(PyTypeObject *ty,
127 PyObject *arg, PyObject *kw)
129 char *kwlist[] = { "default", "set", 0 };
132 PyObject *x = 0, *l = 0;
133 keyszset_pyobj *o = 0;
135 if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|O:new", kwlist,
138 if (!set) set = PyTuple_New(0);
140 if (!PySequence_Check(set)) TYERR("want a sequence");
141 n = PySequence_Size(set);
143 if (PyErr_Occurred()) goto end;
144 if (dfl < 0) VALERR("key size cannot be negative");
145 x = PyInt_FromLong(dfl);
149 for (i = 0; i < n; i++) {
150 if ((x = PySequence_GetItem(set, i)) == 0) goto end;
151 xx = PyInt_AsLong(x);
152 if (PyErr_Occurred()) goto end;
153 if (xx == dfl) continue;
154 if (xx < 0) VALERR("key size cannot be negative");
160 if ((set = PySequence_Tuple(l)) == 0) goto end;
161 o = (keyszset_pyobj *)ty->tp_alloc(ty, 0);
169 return ((PyObject *)o);
172 static PyObject *kaget_min(PyObject *me, void *hunoz)
173 { return (PyInt_FromLong(0)); }
174 #define kaget_max kaget_min
176 static PyObject *ksget_min(PyObject *me, void *hunoz)
178 PyObject *set = ((keyszset_pyobj *)me)->set;
180 n = PyTuple_Size(set);
181 for (i = 0; i < n; i++) {
182 y = PyInt_AsLong(PyTuple_GetItem(set, i));
183 if (x == -1 || y < x) x = y;
185 return (PyInt_FromLong(x));
188 static PyObject *ksget_max(PyObject *me, void *hunoz)
190 PyObject *set = ((keyszset_pyobj *)me)->set;
192 n = PyTuple_Size(set);
193 for (i = 0; i < n; i++) {
194 y = PyInt_AsLong(PyTuple_GetItem(set, i));
197 return (PyInt_FromLong(x));
200 static PyMemberDef keysz_pymembers[] = {
201 #define MEMBERSTRUCT keysz_pyobj
202 #define default dfl /* ugh! */
203 MEMBER(default, T_INT, READONLY, "KSZ.default -> default key size")
209 static PyGetSetDef keyszany_pygetset[] = {
210 #define GETSETNAME(op, name) ka##op##_##name
211 GET (min, "KSZ.min -> smallest allowed key size")
212 GET (max, "KSZ.min -> largest allowed key size")
217 static PyMemberDef keyszrange_pymembers[] = {
218 #define MEMBERSTRUCT keyszrange_pyobj
219 MEMBER(min, T_INT, READONLY, "KSZ.min -> smallest allowed key size")
220 MEMBER(max, T_INT, READONLY, "KSZ.min -> largest allowed key size")
221 MEMBER(mod, T_INT, READONLY,
222 "KSZ.mod -> key size must be a multiple of this")
227 static PyGetSetDef keyszset_pygetset[] = {
228 #define GETSETNAME(op, name) ks##op##_##name
229 GET (min, "KSZ.min -> smallest allowed key size")
230 GET (max, "KSZ.min -> largest allowed key size")
235 static PyMemberDef keyszset_pymembers[] = {
236 #define MEMBERSTRUCT keyszset_pyobj
237 MEMBER(set, T_OBJECT, READONLY, "KSZ.set -> allowed key sizes")
242 static PyTypeObject keysz_pytype_skel = {
243 PyObject_HEAD_INIT(0) 0, /* Header */
244 "KeySZ", /* @tp_name@ */
245 sizeof(keysz_pyobj), /* @tp_basicsize@ */
246 0, /* @tp_itemsize@ */
248 0, /* @tp_dealloc@ */
250 0, /* @tp_getattr@ */
251 0, /* @tp_setattr@ */
252 0, /* @tp_compare@ */
254 0, /* @tp_as_number@ */
255 0, /* @tp_as_sequence@ */
256 0, /* @tp_as_mapping@ */
260 0, /* @tp_getattro@ */
261 0, /* @tp_setattro@ */
262 0, /* @tp_as_buffer@ */
263 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
267 "Key size constraints.",
269 0, /* @tp_traverse@ */
271 0, /* @tp_richcompare@ */
272 0, /* @tp_weaklistoffset@ */
274 0, /* @tp_iternext@ */
275 0, /* @tp_methods@ */
276 keysz_pymembers, /* @tp_members@ */
280 0, /* @tp_descr_get@ */
281 0, /* @tp_descr_set@ */
282 0, /* @tp_dictoffset@ */
284 PyType_GenericAlloc, /* @tp_alloc@ */
285 abstract_pynew, /* @tp_new@ */
290 static PyTypeObject keyszany_pytype_skel = {
291 PyObject_HEAD_INIT(0) 0, /* Header */
292 "KeySZAny", /* @tp_name@ */
293 sizeof(keysz_pyobj), /* @tp_basicsize@ */
294 0, /* @tp_itemsize@ */
296 0, /* @tp_dealloc@ */
298 0, /* @tp_getattr@ */
299 0, /* @tp_setattr@ */
300 0, /* @tp_compare@ */
302 0, /* @tp_as_number@ */
303 0, /* @tp_as_sequence@ */
304 0, /* @tp_as_mapping@ */
308 0, /* @tp_getattro@ */
309 0, /* @tp_setattro@ */
310 0, /* @tp_as_buffer@ */
311 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
315 "Key size constraints. This object imposes no constraints on size.",
317 0, /* @tp_traverse@ */
319 0, /* @tp_richcompare@ */
320 0, /* @tp_weaklistoffset@ */
322 0, /* @tp_iternext@ */
323 0, /* @tp_methods@ */
324 0, /* @tp_members@ */
325 keyszany_pygetset, /* @tp_getset@ */
328 0, /* @tp_descr_get@ */
329 0, /* @tp_descr_set@ */
330 0, /* @tp_dictoffset@ */
332 PyType_GenericAlloc, /* @tp_alloc@ */
333 keyszany_pynew, /* @tp_new@ */
338 static PyTypeObject keyszrange_pytype_skel = {
339 PyObject_HEAD_INIT(0) 0, /* Header */
340 "KeySZRange", /* @tp_name@ */
341 sizeof(keyszrange_pyobj), /* @tp_basicsize@ */
342 0, /* @tp_itemsize@ */
344 0, /* @tp_dealloc@ */
346 0, /* @tp_getattr@ */
347 0, /* @tp_setattr@ */
348 0, /* @tp_compare@ */
350 0, /* @tp_as_number@ */
351 0, /* @tp_as_sequence@ */
352 0, /* @tp_as_mapping@ */
356 0, /* @tp_getattro@ */
357 0, /* @tp_setattro@ */
358 0, /* @tp_as_buffer@ */
359 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
363 "Key size constraints. This object asserts minimum and maximum (if\n\
364 sizes, and requires the key length to be a multiple of some value.",
366 0, /* @tp_traverse@ */
368 0, /* @tp_richcompare@ */
369 0, /* @tp_weaklistoffset@ */
371 0, /* @tp_iternext@ */
372 0, /* @tp_methods@ */
373 keyszrange_pymembers, /* @tp_members@ */
377 0, /* @tp_descr_get@ */
378 0, /* @tp_descr_set@ */
379 0, /* @tp_dictoffset@ */
381 PyType_GenericAlloc, /* @tp_alloc@ */
382 keyszrange_pynew, /* @tp_new@ */
387 static PyTypeObject keyszset_pytype_skel = {
388 PyObject_HEAD_INIT(0) 0, /* Header */
389 "KeySZSet", /* @tp_name@ */
390 sizeof(keyszset_pyobj), /* @tp_basicsize@ */
391 0, /* @tp_itemsize@ */
393 0, /* @tp_dealloc@ */
395 0, /* @tp_getattr@ */
396 0, /* @tp_setattr@ */
397 0, /* @tp_compare@ */
399 0, /* @tp_as_number@ */
400 0, /* @tp_as_sequence@ */
401 0, /* @tp_as_mapping@ */
405 0, /* @tp_getattro@ */
406 0, /* @tp_setattro@ */
407 0, /* @tp_as_buffer@ */
408 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
412 "Key size constraints. This object requires the key to be one of a\n\
415 0, /* @tp_traverse@ */
417 0, /* @tp_richcompare@ */
418 0, /* @tp_weaklistoffset@ */
420 0, /* @tp_iternext@ */
421 0, /* @tp_methods@ */
422 keyszset_pymembers, /* @tp_members@ */
423 keyszset_pygetset, /* @tp_getset@ */
426 0, /* @tp_descr_get@ */
427 0, /* @tp_descr_set@ */
428 0, /* @tp_dictoffset@ */
430 PyType_GenericAlloc, /* @tp_alloc@ */
431 keyszset_pynew, /* @tp_new@ */
436 #define KSZCONVOP(op) \
437 static PyObject *meth__KeySZ_##op(PyObject *me, PyObject *arg) \
440 if (!PyArg_ParseTuple(arg, "Od:" #op, &me, &x)) return (0); \
442 return (PyFloat_FromDouble(y)); \
445 KSZCONVOP(fromschnorr)
454 /*----- Symmetric encryption ----------------------------------------------*/
456 PyTypeObject *gccipher_pytype, *gcipher_pytype;
458 CONVFUNC(gccipher, gccipher *, GCCIPHER_CC)
459 CONVFUNC(gcipher, gcipher *, GCIPHER_C)
461 PyObject *gcipher_pywrap(PyObject *cobj, gcipher *c, unsigned f)
464 if (!cobj) cobj = gccipher_pywrap((/*unconst*/ gccipher *)GC_CLASS(c));
465 else Py_INCREF(cobj);
466 g = PyObject_NEW(gcipher_pyobj, (PyTypeObject *)cobj);
469 return ((PyObject *)g);
472 static PyObject *gcipher_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
474 char *kwlist[] = { "k", 0 };
478 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz))
480 if (keysz(sz, GCCIPHER_CC(ty)->keysz) != sz) VALERR("bad key length");
481 return (gcipher_pywrap((PyObject *)ty,
482 GC_INIT(GCCIPHER_CC(ty), k, sz),
488 PyObject *gccipher_pywrap(gccipher *cc)
490 gccipher_pyobj *g = newtype(gccipher_pytype, 0, cc->name);
492 g->ty.ht_type.tp_basicsize = sizeof(gcipher_pyobj);
493 g->ty.ht_type.tp_base = gcipher_pytype;
494 Py_INCREF(gcipher_pytype);
495 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
496 Py_TPFLAGS_BASETYPE |
497 Py_TPFLAGS_HEAPTYPE);
498 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
499 g->ty.ht_type.tp_free = 0;
500 g->ty.ht_type.tp_new = gcipher_pynew;
501 typeready(&g->ty.ht_type);
502 return ((PyObject *)g);
505 static void gcipher_pydealloc(PyObject *me)
507 if (GCIPHER_F(me) & f_freeme)
508 GC_DESTROY(GCIPHER_C(me));
509 Py_DECREF(me->ob_type);
513 static PyObject *gccget_name(PyObject *me, void *hunoz)
514 { return (PyString_FromString(GCCIPHER_CC(me)->name)); }
516 static PyObject *gccget_keysz(PyObject *me, void *hunoz)
517 { return (keysz_pywrap(GCCIPHER_CC(me)->keysz)); }
519 static PyObject *gccget_blksz(PyObject *me, void *hunoz)
520 { return (PyInt_FromLong(GCCIPHER_CC(me)->blksz)); }
522 static PyObject *gcmeth_encrypt(PyObject *me, PyObject *arg)
528 if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &sz)) return (0);
529 rc = bytestring_pywrap(0, sz);
530 GC_ENCRYPT(GCIPHER_C(me), p, PyString_AS_STRING(rc), sz);
534 static PyObject *gcmeth_enczero(PyObject *me, PyObject *arg)
540 if (!PyArg_ParseTuple(arg, "i:enczero", &sz)) return (0);
541 rc = bytestring_pywrap(0, sz);
542 p = PyString_AS_STRING(rc);
544 GC_ENCRYPT(GCIPHER_C(me), p, p, sz);
548 static PyObject *gcmeth_decrypt(PyObject *me, PyObject *arg)
554 if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &sz)) return (0);
555 rc = bytestring_pywrap(0, sz);
556 GC_DECRYPT(GCIPHER_C(me), p, PyString_AS_STRING(rc), sz);
560 static PyObject *gcmeth_deczero(PyObject *me, PyObject *arg)
566 if (!PyArg_ParseTuple(arg, "i:deczero", &sz)) return (0);
567 rc = bytestring_pywrap(0, sz);
568 p = PyString_AS_STRING(rc);
570 GC_DECRYPT(GCIPHER_C(me), p, p, sz);
574 static PyObject *gcmeth_setiv(PyObject *me, PyObject *arg)
579 if (!PyArg_ParseTuple(arg, "s#:setiv", &p, &sz)) goto end;
580 if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode");
581 if (sz != GC_CLASS(GCIPHER_C(me))->blksz) VALERR("bad IV length");
582 GC_SETIV(GCIPHER_C(me), p);
588 static PyObject *gcmeth_bdry(PyObject *me, PyObject *arg)
590 if (!PyArg_ParseTuple(arg, ":bdry")) goto end;
591 if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode");
592 GC_BDRY(GCIPHER_C(me));
598 static PyGetSetDef gccipher_pygetset[] = {
599 #define GETSETNAME(op, name) gcc##op##_##name
600 GET (keysz, "CC.keysz -> acceptable key sizes")
601 GET (blksz, "CC.blksz -> block size, or zero")
602 GET (name, "CC.name -> name of this kind of cipher")
607 static PyMethodDef gcipher_pymethods[] = {
608 #define METHNAME(name) gcmeth_##name
609 METH (encrypt, "C.encrypt(PT) -> CT")
610 METH (enczero, "C.enczero(N) -> CT")
611 METH (decrypt, "C.decrypt(CT) -> PT")
612 METH (deczero, "C.deczero(N) -> PT")
613 METH (setiv, "C.setiv(IV)")
614 METH (bdry, "C.bdry()")
619 static PyTypeObject gccipher_pytype_skel = {
620 PyObject_HEAD_INIT(0) 0, /* Header */
621 "GCCipher", /* @tp_name@ */
622 sizeof(gccipher_pyobj), /* @tp_basicsize@ */
623 0, /* @tp_itemsize@ */
625 0, /* @tp_dealloc@ */
627 0, /* @tp_getattr@ */
628 0, /* @tp_setattr@ */
629 0, /* @tp_compare@ */
631 0, /* @tp_as_number@ */
632 0, /* @tp_as_sequence@ */
633 0, /* @tp_as_mapping@ */
637 0, /* @tp_getattro@ */
638 0, /* @tp_setattro@ */
639 0, /* @tp_as_buffer@ */
640 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
644 "Symmetric cipher metaclass.",
646 0, /* @tp_traverse@ */
648 0, /* @tp_richcompare@ */
649 0, /* @tp_weaklistoffset@ */
651 0, /* @tp_iternext@ */
652 0, /* @tp_methods@ */
653 0, /* @tp_members@ */
654 gccipher_pygetset, /* @tp_getset@ */
657 0, /* @tp_descr_get@ */
658 0, /* @tp_descr_set@ */
659 0, /* @tp_dictoffset@ */
661 PyType_GenericAlloc, /* @tp_alloc@ */
662 abstract_pynew, /* @tp_new@ */
667 static PyTypeObject gcipher_pytype_skel = {
668 PyObject_HEAD_INIT(0) 0, /* Header */
669 "GCipher", /* @tp_name@ */
670 sizeof(gcipher_pyobj), /* @tp_basicsize@ */
671 0, /* @tp_itemsize@ */
673 gcipher_pydealloc, /* @tp_dealloc@ */
675 0, /* @tp_getattr@ */
676 0, /* @tp_setattr@ */
677 0, /* @tp_compare@ */
679 0, /* @tp_as_number@ */
680 0, /* @tp_as_sequence@ */
681 0, /* @tp_as_mapping@ */
685 0, /* @tp_getattro@ */
686 0, /* @tp_setattro@ */
687 0, /* @tp_as_buffer@ */
688 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
692 "Symmetric cipher, abstract base class.",
694 0, /* @tp_traverse@ */
696 0, /* @tp_richcompare@ */
697 0, /* @tp_weaklistoffset@ */
699 0, /* @tp_iternext@ */
700 gcipher_pymethods, /* @tp_methods@ */
701 0, /* @tp_members@ */
705 0, /* @tp_descr_get@ */
706 0, /* @tp_descr_set@ */
707 0, /* @tp_dictoffset@ */
709 PyType_GenericAlloc, /* @tp_alloc@ */
710 abstract_pynew, /* @tp_new@ */
715 /*----- Hash functions ----------------------------------------------------*/
717 PyTypeObject *gchash_pytype, *ghash_pytype;
719 CONVFUNC(gchash, gchash *, GCHASH_CH)
720 CONVFUNC(ghash, ghash *, GHASH_H)
722 static PyObject *ghash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
724 char *kwlist[] = { 0 };
725 if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", kwlist))
727 return (ghash_pywrap((PyObject *)ty, GH_INIT(GCHASH_CH(ty)), f_freeme));
732 PyObject *gchash_pywrap(gchash *ch)
734 gchash_pyobj *g = newtype(gchash_pytype, 0, ch->name);
736 g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj);
737 g->ty.ht_type.tp_base = ghash_pytype;
738 Py_INCREF(ghash_pytype);
739 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
740 Py_TPFLAGS_BASETYPE |
741 Py_TPFLAGS_HEAPTYPE);
742 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
743 g->ty.ht_type.tp_free = 0;
744 g->ty.ht_type.tp_new = ghash_pynew;
745 typeready(&g->ty.ht_type);
746 return ((PyObject *)g);
749 PyObject *ghash_pywrap(PyObject *cobj, ghash *h, unsigned f)
752 if (!cobj) cobj = gchash_pywrap((/*unconst*/ gchash *)GH_CLASS(h));
753 else Py_INCREF(cobj);
754 g = PyObject_NEW(ghash_pyobj, (PyTypeObject *)cobj);
757 return ((PyObject *)g);
760 static void ghash_pydealloc(PyObject *me)
762 if (GHASH_F(me) & f_freeme)
763 GH_DESTROY(GHASH_H(me));
764 Py_DECREF(me->ob_type);
768 static PyObject *gchget_name(PyObject *me, void *hunoz)
769 { return (PyString_FromString(GCHASH_CH(me)->name)); }
771 static PyObject *gchget_hashsz(PyObject *me, void *hunoz)
772 { return (PyInt_FromLong(GCHASH_CH(me)->hashsz)); }
774 static PyObject *gchget_bufsz(PyObject *me, void *hunoz)
775 { return (PyInt_FromLong(GCHASH_CH(me)->bufsz)); }
777 static PyObject *ghmeth_hash(PyObject *me, PyObject *arg)
781 if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0);
782 GH_HASH(GHASH_H(me), p, sz);
786 static PyObject *ghmeth_done(PyObject *me, PyObject *arg)
790 if (!PyArg_ParseTuple(arg, ":done")) return (0);
791 g = GH_COPY(GHASH_H(me));
792 rc = bytestring_pywrap(0, g->ops->c->hashsz);
793 GH_DONE(g, PyString_AS_STRING(rc));
798 static PyGetSetDef gchash_pygetset[] = {
799 #define GETSETNAME(op, name) gch##op##_##name
800 GET (bufsz, "CH.bufsz -> hash buffer size, or zero")
801 GET (hashsz, "CH.blksz -> hash output size")
802 GET (name, "CH.name -> name of this kind of hash")
807 #define GHMETH_HASHU_(n, W, w) \
808 static PyObject *ghmeth_hashu##w(PyObject *me, PyObject *arg) \
811 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \
812 GH_HASHU##W(GHASH_H(me), x); \
817 DOUINTCONV(GHMETH_HASHU_)
819 #define GHMETH_HASHBUF_(n, W, w) \
820 static PyObject *ghmeth_hashbuf##w(PyObject *me, PyObject *arg) \
824 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
825 if (sz > MASK##n) TYERR("string too long"); \
826 GH_HASHBUF##W(GHASH_H(me), p, sz); \
831 DOUINTCONV(GHMETH_HASHBUF_)
833 static PyObject *ghmeth_hashstrz(PyObject *me, PyObject *arg)
836 if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0);
837 GH_HASHSTRZ(GHASH_H(me), p);
841 static PyMethodDef ghash_pymethods[] = {
842 #define METHNAME(name) ghmeth_##name
843 METH (hash, "H.hash(M)")
844 #define METHU_(n, W, w) METH(hashu##w, "H.hashu" #w "(WORD)")
846 #define METHBUF_(n, W, w) METH(hashbuf##w, "H.hashbuf" #w "(BYTES)")
848 METH (hashstrz, "H.hashstrz(STRING)")
849 METH (done, "H.done() -> HASH")
854 static PyTypeObject gchash_pytype_skel = {
855 PyObject_HEAD_INIT(0) 0, /* Header */
856 "GCHash", /* @tp_name@ */
857 sizeof(gchash_pyobj), /* @tp_basicsize@ */
858 0, /* @tp_itemsize@ */
860 0, /* @tp_dealloc@ */
862 0, /* @tp_getattr@ */
863 0, /* @tp_setattr@ */
864 0, /* @tp_compare@ */
866 0, /* @tp_as_number@ */
867 0, /* @tp_as_sequence@ */
868 0, /* @tp_as_mapping@ */
872 0, /* @tp_getattro@ */
873 0, /* @tp_setattro@ */
874 0, /* @tp_as_buffer@ */
875 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
879 "Hash function metaclass.",
881 0, /* @tp_traverse@ */
883 0, /* @tp_richcompare@ */
884 0, /* @tp_weaklistoffset@ */
886 0, /* @tp_iternext@ */
887 0, /* @tp_methods@ */
888 0, /* @tp_members@ */
889 gchash_pygetset, /* @tp_getset@ */
892 0, /* @tp_descr_get@ */
893 0, /* @tp_descr_set@ */
894 0, /* @tp_dictoffset@ */
896 PyType_GenericAlloc, /* @tp_alloc@ */
897 abstract_pynew, /* @tp_new@ */
902 static PyTypeObject ghash_pytype_skel = {
903 PyObject_HEAD_INIT(0) 0, /* Header */
904 "GHash", /* @tp_name@ */
905 sizeof(ghash_pyobj), /* @tp_basicsize@ */
906 0, /* @tp_itemsize@ */
908 ghash_pydealloc, /* @tp_dealloc@ */
910 0, /* @tp_getattr@ */
911 0, /* @tp_setattr@ */
912 0, /* @tp_compare@ */
914 0, /* @tp_as_number@ */
915 0, /* @tp_as_sequence@ */
916 0, /* @tp_as_mapping@ */
920 0, /* @tp_getattro@ */
921 0, /* @tp_setattro@ */
922 0, /* @tp_as_buffer@ */
923 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
927 "Hash function, abstract base class.",
929 0, /* @tp_traverse@ */
931 0, /* @tp_richcompare@ */
932 0, /* @tp_weaklistoffset@ */
934 0, /* @tp_iternext@ */
935 ghash_pymethods, /* @tp_methods@ */
936 0, /* @tp_members@ */
940 0, /* @tp_descr_get@ */
941 0, /* @tp_descr_set@ */
942 0, /* @tp_dictoffset@ */
944 PyType_GenericAlloc, /* @tp_alloc@ */
945 abstract_pynew, /* @tp_new@ */
950 /*----- Message authentication --------------------------------------------*/
952 PyTypeObject *gcmac_pytype, *gmac_pytype, *gmhash_pytype;
954 CONVFUNC(gcmac, gcmac *, GCMAC_CM)
955 CONVFUNC(gmac, gmac *, GMAC_M)
956 CONVFUNC(gmhash, ghash *, GHASH_H)
958 static PyObject *gmac_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
960 char *kwlist[] = { "k", 0 };
964 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz))
966 if (keysz(sz, GCMAC_CM(ty)->keysz) != sz) VALERR("bad key length");
967 return (gmac_pywrap((PyObject *)ty,
968 GM_KEY(GCMAC_CM(ty), k, sz),
974 static PyObject *gmhash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
976 char *kwlist[] = { 0 };
979 if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", kwlist)) return (0);
980 g = PyObject_NEW(ghash_pyobj, ty);
981 g->h = GM_INIT(GMAC_M(ty));
984 return ((PyObject *)g);
987 PyObject *gcmac_pywrap(gcmac *cm)
989 gcmac_pyobj *g = newtype(gcmac_pytype, 0, cm->name);
991 g->ty.ht_type.tp_basicsize = sizeof(gmac_pyobj);
992 g->ty.ht_type.tp_base = gmac_pytype;
993 Py_INCREF(gmac_pytype);
994 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
995 Py_TPFLAGS_BASETYPE |
996 Py_TPFLAGS_HEAPTYPE);
997 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
998 g->ty.ht_type.tp_free = 0;
999 g->ty.ht_type.tp_new = gmac_pynew;
1000 typeready(&g->ty.ht_type);
1001 return ((PyObject *)g);
1004 PyObject *gmac_pywrap(PyObject *cobj, gmac *m, unsigned f)
1007 if (!cobj) cobj = gcmac_pywrap((/*unconst*/ gcmac *)GM_CLASS(m));
1008 else Py_INCREF(cobj);
1009 g = newtype((PyTypeObject *)cobj, 0, 0);
1010 g->ty.ht_name = PyString_FromFormat("%s(keyed)", m->ops->c->name);
1011 g->ty.ht_type.tp_name = PyString_AS_STRING(g->ty.ht_name);
1012 g->ty.ht_type.tp_base = gmhash_pytype;
1013 Py_INCREF(gmac_pytype);
1014 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
1015 Py_TPFLAGS_BASETYPE |
1016 Py_TPFLAGS_HEAPTYPE);
1017 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
1018 g->ty.ht_type.tp_free = 0;
1019 g->ty.ht_type.tp_new = gmhash_pynew;
1020 typeready(&g->ty.ht_type);
1023 return ((PyObject *)g);
1026 static void gmac_pydealloc(PyObject *me)
1028 if (GMAC_F(me) & f_freeme)
1029 GM_DESTROY(GMAC_M(me));
1030 Py_DECREF(me->ob_type);
1031 PyType_Type.tp_dealloc(me);
1034 static PyObject *gcmget_name(PyObject *me, void *hunoz)
1035 { return (PyString_FromString(GCMAC_CM(me)->name)); }
1037 static PyObject *gcmget_keysz(PyObject *me, void *hunoz)
1038 { return (keysz_pywrap(GCMAC_CM(me)->keysz)); }
1040 static PyObject *gcmget_tagsz(PyObject *me, void *hunoz)
1041 { return (PyInt_FromLong(GCMAC_CM(me)->hashsz)); }
1043 static PyGetSetDef gcmac_pygetset[] = {
1044 #define GETSETNAME(op, name) gcm##op##_##name
1045 GET (keysz, "CM.keysz -> acceptable key sizes")
1046 GET (tagsz, "CM.tagsz -> MAC output size")
1047 GET (name, "CM.name -> name of this kind of MAC")
1052 static PyTypeObject gcmac_pytype_skel = {
1053 PyObject_HEAD_INIT(0) 0, /* Header */
1054 "GCMAC", /* @tp_name@ */
1055 sizeof(gchash_pyobj), /* @tp_basicsize@ */
1056 0, /* @tp_itemsize@ */
1058 0, /* @tp_dealloc@ */
1060 0, /* @tp_getattr@ */
1061 0, /* @tp_setattr@ */
1062 0, /* @tp_compare@ */
1064 0, /* @tp_as_number@ */
1065 0, /* @tp_as_sequence@ */
1066 0, /* @tp_as_mapping@ */
1070 0, /* @tp_getattro@ */
1071 0, /* @tp_setattro@ */
1072 0, /* @tp_as_buffer@ */
1073 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1074 Py_TPFLAGS_BASETYPE,
1077 "Message authentication code metametaclass.",
1079 0, /* @tp_traverse@ */
1081 0, /* @tp_richcompare@ */
1082 0, /* @tp_weaklistoffset@ */
1084 0, /* @tp_iternext@ */
1085 0, /* @tp_methods@ */
1086 0, /* @tp_members@ */
1087 gcmac_pygetset, /* @tp_getset@ */
1090 0, /* @tp_descr_get@ */
1091 0, /* @tp_descr_set@ */
1092 0, /* @tp_dictoffset@ */
1094 PyType_GenericAlloc, /* @tp_alloc@ */
1095 abstract_pynew, /* @tp_new@ */
1100 static PyTypeObject gmac_pytype_skel = {
1101 PyObject_HEAD_INIT(0) 0, /* Header */
1102 "GMAC", /* @tp_name@ */
1103 sizeof(gmac_pyobj), /* @tp_basicsize@ */
1104 0, /* @tp_itemsize@ */
1106 gmac_pydealloc, /* @tp_dealloc@ */
1108 0, /* @tp_getattr@ */
1109 0, /* @tp_setattr@ */
1110 0, /* @tp_compare@ */
1112 0, /* @tp_as_number@ */
1113 0, /* @tp_as_sequence@ */
1114 0, /* @tp_as_mapping@ */
1118 0, /* @tp_getattro@ */
1119 0, /* @tp_setattro@ */
1120 0, /* @tp_as_buffer@ */
1121 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1122 Py_TPFLAGS_BASETYPE,
1125 "Message authentication code metaclass, abstract base class.",
1127 0, /* @tp_traverse@ */
1129 0, /* @tp_richcompare@ */
1130 0, /* @tp_weaklistoffset@ */
1132 0, /* @tp_iternext@ */
1133 0, /* @tp_methods@ */
1134 0, /* @tp_members@ */
1135 0, /* @tp_getset@ */
1138 0, /* @tp_descr_get@ */
1139 0, /* @tp_descr_set@ */
1140 0, /* @tp_dictoffset@ */
1142 PyType_GenericAlloc, /* @tp_alloc@ */
1143 abstract_pynew, /* @tp_new@ */
1148 static PyTypeObject gmhash_pytype_skel = {
1149 PyObject_HEAD_INIT(0) 0, /* Header */
1150 "GMACHash", /* @tp_name@ */
1151 sizeof(ghash_pyobj), /* @tp_basicsize@ */
1152 0, /* @tp_itemsize@ */
1154 ghash_pydealloc, /* @tp_dealloc@ */
1156 0, /* @tp_getattr@ */
1157 0, /* @tp_setattr@ */
1158 0, /* @tp_compare@ */
1160 0, /* @tp_as_number@ */
1161 0, /* @tp_as_sequence@ */
1162 0, /* @tp_as_mapping@ */
1166 0, /* @tp_getattro@ */
1167 0, /* @tp_setattro@ */
1168 0, /* @tp_as_buffer@ */
1169 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1170 Py_TPFLAGS_BASETYPE,
1173 "Message authentication code, abstract base class.",
1175 0, /* @tp_traverse@ */
1177 0, /* @tp_richcompare@ */
1178 0, /* @tp_weaklistoffset@ */
1180 0, /* @tp_iternext@ */
1181 0, /* @tp_methods@ */
1182 0, /* @tp_members@ */
1183 0, /* @tp_getset@ */
1186 0, /* @tp_descr_get@ */
1187 0, /* @tp_descr_set@ */
1188 0, /* @tp_dictoffset@ */
1190 PyType_GenericAlloc, /* @tp_alloc@ */
1191 abstract_pynew, /* @tp_new@ */
1196 /*----- Pseudorandom permutations -----------------------------------------*/
1198 static PyTypeObject *gcprp_pytype, *gprp_pytype;
1200 typedef struct prpinfo {
1205 void (*init)(void *, const void *, size_t);
1206 void (*eblk)(void *, const void *, void *);
1207 void (*dblk)(void *, const void *, void *);
1210 #define PRP_DEF(PRE, pre) \
1211 static void pre##_prpinit(void *ctx, const void *k, size_t ksz) \
1212 { pre##_init(ctx, k, ksz); } \
1213 static void pre##_prpeblk(void *ctx, const void *in, void *out) \
1215 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
1216 pre##_eblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
1218 static void pre##_prpdblk(void *ctx, const void *in, void *out) \
1220 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
1221 pre##_dblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
1223 static const prpinfo pre##_prpinfo = { \
1224 #pre, pre##_keysz, sizeof(pre##_ctx), PRE##_BLKSZ, \
1225 pre##_prpinit, pre##_prpeblk, pre##_prpdblk \
1229 static const struct prpinfo *const gprptab[] = {
1230 #define PRP_ENTRY(PRE, pre) &pre##_prpinfo,
1235 typedef struct gcprp_pyobj {
1236 PyHeapTypeObject ty;
1239 #define GCPRP_PRP(o) (((gcprp_pyobj *)(o))->prp)
1241 typedef struct gprp_pyobj {
1245 #define GPRP_PRP(o) (((gprp_pyobj *)(o))->prp)
1246 #define GPRP_CTX(o) (((gprp_pyobj *)(o)) + 1)
1248 typedef struct prp {
1253 static PyObject *gprp_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1255 char *kwlist[] = { "key", 0 };
1258 const prpinfo *prp = GCPRP_PRP(ty);
1261 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz))
1263 if (keysz(sz, prp->keysz) != sz) VALERR("bad key length");
1264 me = (PyObject *)ty->tp_alloc(ty, 0);
1266 prp->init(GPRP_CTX(me), k, sz);
1273 static void gprp_pydealloc(PyObject *me)
1274 { Py_DECREF(me->ob_type); FREEOBJ(me); }
1276 static PyObject *gcprp_pywrap(const prpinfo *prp)
1278 gcprp_pyobj *g = newtype(gcprp_pytype, 0, prp->name);
1280 g->ty.ht_type.tp_basicsize = sizeof(gprp_pyobj) + prp->ctxsz;
1281 g->ty.ht_type.tp_base = gprp_pytype;
1282 Py_INCREF(gprp_pytype);
1283 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
1284 Py_TPFLAGS_BASETYPE |
1285 Py_TPFLAGS_HEAPTYPE);
1286 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
1287 g->ty.ht_type.tp_free = 0;
1288 g->ty.ht_type.tp_new = gprp_pynew;
1289 typeready(&g->ty.ht_type);
1290 return ((PyObject *)g);
1293 static PyObject *gcpget_name(PyObject *me, void *hunoz)
1294 { return (PyString_FromString(GCPRP_PRP(me)->name)); }
1295 static PyObject *gcpget_keysz(PyObject *me, void *hunoz)
1296 { return (keysz_pywrap(GCPRP_PRP(me)->keysz)); }
1297 static PyObject *gcpget_blksz(PyObject *me, void *hunoz)
1298 { return (PyInt_FromLong(GCPRP_PRP(me)->blksz)); }
1300 static PyObject *gpmeth_encrypt(PyObject *me, PyObject *arg)
1306 if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &n)) goto end;
1307 if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length");
1308 rc = bytestring_pywrap(0, n);
1309 GPRP_PRP(me)->eblk(GPRP_CTX(me), p, PyString_AS_STRING(rc));
1314 static PyObject *gpmeth_decrypt(PyObject *me, PyObject *arg)
1320 if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &n)) goto end;
1321 if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length");
1322 rc = bytestring_pywrap(0, n);
1323 GPRP_PRP(me)->dblk(GPRP_CTX(me), p, PyString_AS_STRING(rc));
1328 static PyGetSetDef gcprp_pygetset[] = {
1329 #define GETSETNAME(op, name) gcp##op##_##name
1330 GET (keysz, "CP.keysz -> acceptable key sizes")
1331 GET (blksz, "CP.blksz -> block size")
1332 GET (name, "CP.name -> name of this kind of PRP")
1337 static PyMethodDef gprp_pymethods[] = {
1338 #define METHNAME(name) gpmeth_##name
1339 METH (encrypt, "P.encrypt(PT) -> CT")
1340 METH (decrypt, "P.decrypt(CT) -> PT")
1345 static PyTypeObject gcprp_pytype_skel = {
1346 PyObject_HEAD_INIT(0) 0, /* Header */
1347 "GCPRP", /* @tp_name@ */
1348 sizeof(gcprp_pyobj), /* @tp_basicsize@ */
1349 0, /* @tp_itemsize@ */
1351 0, /* @tp_dealloc@ */
1353 0, /* @tp_getattr@ */
1354 0, /* @tp_setattr@ */
1355 0, /* @tp_compare@ */
1357 0, /* @tp_as_number@ */
1358 0, /* @tp_as_sequence@ */
1359 0, /* @tp_as_mapping@ */
1363 0, /* @tp_getattro@ */
1364 0, /* @tp_setattro@ */
1365 0, /* @tp_as_buffer@ */
1366 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1367 Py_TPFLAGS_BASETYPE,
1370 "Pseudorandom permutation metaclass.",
1372 0, /* @tp_traverse@ */
1374 0, /* @tp_richcompare@ */
1375 0, /* @tp_weaklistoffset@ */
1377 0, /* @tp_iternext@ */
1378 0, /* @tp_methods@ */
1379 0, /* @tp_members@ */
1380 gcprp_pygetset, /* @tp_getset@ */
1383 0, /* @tp_descr_get@ */
1384 0, /* @tp_descr_set@ */
1385 0, /* @tp_dictoffset@ */
1387 PyType_GenericAlloc, /* @tp_alloc@ */
1388 abstract_pynew, /* @tp_new@ */
1393 static PyTypeObject gprp_pytype_skel = {
1394 PyObject_HEAD_INIT(0) 0, /* Header */
1395 "GPRP", /* @tp_name@ */
1396 sizeof(gprp_pyobj), /* @tp_basicsize@ */
1397 0, /* @tp_itemsize@ */
1399 gprp_pydealloc, /* @tp_dealloc@ */
1401 0, /* @tp_getattr@ */
1402 0, /* @tp_setattr@ */
1403 0, /* @tp_compare@ */
1405 0, /* @tp_as_number@ */
1406 0, /* @tp_as_sequence@ */
1407 0, /* @tp_as_mapping@ */
1411 0, /* @tp_getattro@ */
1412 0, /* @tp_setattro@ */
1413 0, /* @tp_as_buffer@ */
1414 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1415 Py_TPFLAGS_BASETYPE,
1418 "Pseudorandom permutation, abstract base class.",
1420 0, /* @tp_traverse@ */
1422 0, /* @tp_richcompare@ */
1423 0, /* @tp_weaklistoffset@ */
1425 0, /* @tp_iternext@ */
1426 gprp_pymethods, /* @tp_methods@ */
1427 0, /* @tp_members@ */
1428 0, /* @tp_getset@ */
1431 0, /* @tp_descr_get@ */
1432 0, /* @tp_descr_set@ */
1433 0, /* @tp_dictoffset@ */
1435 PyType_GenericAlloc, /* @tp_alloc@ */
1436 abstract_pynew, /* @tp_new@ */
1441 /*----- Main code ---------------------------------------------------------*/
1443 static PyMethodDef methods[] = {
1444 #define METHNAME(func) meth_##func
1445 METH (_KeySZ_fromdl, "\
1446 fromdl(N) -> M: convert integer discrete log field size to work factor")
1447 METH (_KeySZ_fromschnorr, "\
1448 fromschnorr(N) -> M: convert Schnorr group order to work factor")
1449 METH (_KeySZ_fromif, "\
1450 fromif(N) -> M: convert integer factorization problem size to work factor")
1451 METH (_KeySZ_fromec, "\
1452 fromec(N) -> M: convert elliptic curve group order to work factor")
1453 METH (_KeySZ_todl, "\
1454 todl(N) -> M: convert work factor to integer discrete log field size")
1455 METH (_KeySZ_toschnorr, "\
1456 toschnorr(N) -> M: convert work factor to Schnorr group order")
1457 METH (_KeySZ_toif, "\
1458 toif(N) -> M: convert work factor to integer factorization problem size")
1459 METH (_KeySZ_toec, "\
1460 toec(N) -> M: convert work factor to elliptic curve group order")
1465 void algorithms_pyinit(void)
1467 INITTYPE(keysz, root);
1468 INITTYPE(keyszany, keysz);
1469 INITTYPE(keyszrange, keysz);
1470 INITTYPE(keyszset, keysz);
1471 INITTYPE(gccipher, type);
1472 INITTYPE(gcipher, root);
1473 INITTYPE(gchash, type);
1474 INITTYPE(ghash, root);
1475 INITTYPE(gcmac, type);
1476 INITTYPE(gmac, type);
1477 INITTYPE(gmhash, ghash);
1478 INITTYPE(gcprp, type);
1479 INITTYPE(gprp, root);
1480 addmethods(methods);
1483 GEN(gcciphers, cipher)
1486 #define gcprp prpinfo
1489 void algorithms_pyinsert(PyObject *mod)
1492 INSERT("KeySZ", keysz_pytype);
1493 INSERT("KeySZAny", keyszany_pytype);
1494 INSERT("KeySZRange", keyszrange_pytype);
1495 INSERT("KeySZSet", keyszset_pytype);
1496 INSERT("GCCipher", gccipher_pytype);
1497 INSERT("GCipher", gcipher_pytype);
1498 INSERT("gcciphers", gcciphers());
1499 INSERT("GCHash", gchash_pytype);
1500 INSERT("GHash", ghash_pytype);
1501 INSERT("gchashes", d = gchashes());
1502 sha_pyobj = PyDict_GetItemString(d, "sha"); Py_INCREF(sha_pyobj);
1503 has160_pyobj = PyDict_GetItemString(d, "has160"); Py_INCREF(has160_pyobj);
1504 INSERT("GCMAC", gcmac_pytype);
1505 INSERT("GMAC", gmac_pytype);
1506 INSERT("GMACHash", gmhash_pytype);
1507 INSERT("gcmacs", gcmacs());
1508 INSERT("GCPRP", gcprp_pytype);
1509 INSERT("GPRP", gprp_pytype);
1510 INSERT("gcprps", gcprps());
1513 /*----- That's all, folks -------------------------------------------------*/