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 (!GCIPHER_C(me)->ops->setiv) VALERR("`setiv' not supported");
581 if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode");
582 if (sz != GC_CLASS(GCIPHER_C(me))->blksz) VALERR("bad IV length");
583 GC_SETIV(GCIPHER_C(me), p);
589 static PyObject *gcmeth_bdry(PyObject *me, PyObject *arg)
591 if (!PyArg_ParseTuple(arg, ":bdry")) goto end;
592 if (!GCIPHER_C(me)->ops->bdry) VALERR("`bdry' not supported");
593 if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode");
594 GC_BDRY(GCIPHER_C(me));
600 static PyGetSetDef gccipher_pygetset[] = {
601 #define GETSETNAME(op, name) gcc##op##_##name
602 GET (keysz, "CC.keysz -> acceptable key sizes")
603 GET (blksz, "CC.blksz -> block size, or zero")
604 GET (name, "CC.name -> name of this kind of cipher")
609 static PyMethodDef gcipher_pymethods[] = {
610 #define METHNAME(name) gcmeth_##name
611 METH (encrypt, "C.encrypt(PT) -> CT")
612 METH (enczero, "C.enczero(N) -> CT")
613 METH (decrypt, "C.decrypt(CT) -> PT")
614 METH (deczero, "C.deczero(N) -> PT")
615 METH (setiv, "C.setiv(IV)")
616 METH (bdry, "C.bdry()")
621 static PyTypeObject gccipher_pytype_skel = {
622 PyObject_HEAD_INIT(0) 0, /* Header */
623 "GCCipher", /* @tp_name@ */
624 sizeof(gccipher_pyobj), /* @tp_basicsize@ */
625 0, /* @tp_itemsize@ */
627 0, /* @tp_dealloc@ */
629 0, /* @tp_getattr@ */
630 0, /* @tp_setattr@ */
631 0, /* @tp_compare@ */
633 0, /* @tp_as_number@ */
634 0, /* @tp_as_sequence@ */
635 0, /* @tp_as_mapping@ */
639 0, /* @tp_getattro@ */
640 0, /* @tp_setattro@ */
641 0, /* @tp_as_buffer@ */
642 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
646 "Symmetric cipher metaclass.",
648 0, /* @tp_traverse@ */
650 0, /* @tp_richcompare@ */
651 0, /* @tp_weaklistoffset@ */
653 0, /* @tp_iternext@ */
654 0, /* @tp_methods@ */
655 0, /* @tp_members@ */
656 gccipher_pygetset, /* @tp_getset@ */
659 0, /* @tp_descr_get@ */
660 0, /* @tp_descr_set@ */
661 0, /* @tp_dictoffset@ */
663 PyType_GenericAlloc, /* @tp_alloc@ */
664 abstract_pynew, /* @tp_new@ */
669 static PyTypeObject gcipher_pytype_skel = {
670 PyObject_HEAD_INIT(0) 0, /* Header */
671 "GCipher", /* @tp_name@ */
672 sizeof(gcipher_pyobj), /* @tp_basicsize@ */
673 0, /* @tp_itemsize@ */
675 gcipher_pydealloc, /* @tp_dealloc@ */
677 0, /* @tp_getattr@ */
678 0, /* @tp_setattr@ */
679 0, /* @tp_compare@ */
681 0, /* @tp_as_number@ */
682 0, /* @tp_as_sequence@ */
683 0, /* @tp_as_mapping@ */
687 0, /* @tp_getattro@ */
688 0, /* @tp_setattro@ */
689 0, /* @tp_as_buffer@ */
690 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
694 "Symmetric cipher, abstract base class.",
696 0, /* @tp_traverse@ */
698 0, /* @tp_richcompare@ */
699 0, /* @tp_weaklistoffset@ */
701 0, /* @tp_iternext@ */
702 gcipher_pymethods, /* @tp_methods@ */
703 0, /* @tp_members@ */
707 0, /* @tp_descr_get@ */
708 0, /* @tp_descr_set@ */
709 0, /* @tp_dictoffset@ */
711 PyType_GenericAlloc, /* @tp_alloc@ */
712 abstract_pynew, /* @tp_new@ */
717 /*----- Hash functions ----------------------------------------------------*/
719 PyTypeObject *gchash_pytype, *ghash_pytype;
721 CONVFUNC(gchash, gchash *, GCHASH_CH)
722 CONVFUNC(ghash, ghash *, GHASH_H)
724 static PyObject *ghash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
726 char *kwlist[] = { 0 };
727 if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", kwlist))
729 return (ghash_pywrap((PyObject *)ty, GH_INIT(GCHASH_CH(ty)), f_freeme));
734 PyObject *gchash_pywrap(gchash *ch)
736 gchash_pyobj *g = newtype(gchash_pytype, 0, ch->name);
738 g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj);
739 g->ty.ht_type.tp_base = ghash_pytype;
740 Py_INCREF(ghash_pytype);
741 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
742 Py_TPFLAGS_BASETYPE |
743 Py_TPFLAGS_HEAPTYPE);
744 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
745 g->ty.ht_type.tp_free = 0;
746 g->ty.ht_type.tp_new = ghash_pynew;
747 typeready(&g->ty.ht_type);
748 return ((PyObject *)g);
751 PyObject *ghash_pywrap(PyObject *cobj, ghash *h, unsigned f)
754 if (!cobj) cobj = gchash_pywrap((/*unconst*/ gchash *)GH_CLASS(h));
755 else Py_INCREF(cobj);
756 g = PyObject_NEW(ghash_pyobj, (PyTypeObject *)cobj);
759 return ((PyObject *)g);
762 static void ghash_pydealloc(PyObject *me)
764 if (GHASH_F(me) & f_freeme)
765 GH_DESTROY(GHASH_H(me));
766 Py_DECREF(me->ob_type);
770 static PyObject *gchget_name(PyObject *me, void *hunoz)
771 { return (PyString_FromString(GCHASH_CH(me)->name)); }
773 static PyObject *gchget_hashsz(PyObject *me, void *hunoz)
774 { return (PyInt_FromLong(GCHASH_CH(me)->hashsz)); }
776 static PyObject *gchget_bufsz(PyObject *me, void *hunoz)
777 { return (PyInt_FromLong(GCHASH_CH(me)->bufsz)); }
779 static PyObject *ghmeth_hash(PyObject *me, PyObject *arg)
783 if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0);
784 GH_HASH(GHASH_H(me), p, sz);
788 #define GHMETH_HASHU_(n, W, w) \
789 static PyObject *ghmeth_hashu##w(PyObject *me, PyObject *arg) \
792 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \
793 GH_HASHU##W(GHASH_H(me), x); \
798 DOUINTCONV(GHMETH_HASHU_)
800 #define GHMETH_HASHBUF_(n, W, w) \
801 static PyObject *ghmeth_hashbuf##w(PyObject *me, PyObject *arg) \
805 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
806 if (sz > MASK##n) TYERR("string too long"); \
807 GH_HASHBUF##W(GHASH_H(me), p, sz); \
812 DOUINTCONV(GHMETH_HASHBUF_)
814 static PyObject *ghmeth_hashstrz(PyObject *me, PyObject *arg)
817 if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0);
818 GH_HASHSTRZ(GHASH_H(me), p);
822 static PyObject *ghmeth_done(PyObject *me, PyObject *arg)
826 if (!PyArg_ParseTuple(arg, ":done")) return (0);
827 g = GH_COPY(GHASH_H(me));
828 rc = bytestring_pywrap(0, g->ops->c->hashsz);
829 GH_DONE(g, PyString_AS_STRING(rc));
834 static PyGetSetDef gchash_pygetset[] = {
835 #define GETSETNAME(op, name) gch##op##_##name
836 GET (bufsz, "CH.bufsz -> hash buffer size, or zero")
837 GET (hashsz, "CH.hashsz -> hash output size")
838 GET (name, "CH.name -> name of this kind of hash")
843 static PyMethodDef ghash_pymethods[] = {
844 #define METHNAME(name) ghmeth_##name
845 METH (hash, "H.hash(M)")
846 #define METHU_(n, W, w) METH(hashu##w, "H.hashu" #w "(WORD)")
849 #define METHBUF_(n, W, w) METH(hashbuf##w, "H.hashbuf" #w "(BYTES)")
852 METH (hashstrz, "H.hashstrz(STRING)")
853 METH (done, "H.done() -> HASH")
858 static PyTypeObject gchash_pytype_skel = {
859 PyObject_HEAD_INIT(0) 0, /* Header */
860 "GCHash", /* @tp_name@ */
861 sizeof(gchash_pyobj), /* @tp_basicsize@ */
862 0, /* @tp_itemsize@ */
864 0, /* @tp_dealloc@ */
866 0, /* @tp_getattr@ */
867 0, /* @tp_setattr@ */
868 0, /* @tp_compare@ */
870 0, /* @tp_as_number@ */
871 0, /* @tp_as_sequence@ */
872 0, /* @tp_as_mapping@ */
876 0, /* @tp_getattro@ */
877 0, /* @tp_setattro@ */
878 0, /* @tp_as_buffer@ */
879 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
883 "Hash function metaclass.",
885 0, /* @tp_traverse@ */
887 0, /* @tp_richcompare@ */
888 0, /* @tp_weaklistoffset@ */
890 0, /* @tp_iternext@ */
891 0, /* @tp_methods@ */
892 0, /* @tp_members@ */
893 gchash_pygetset, /* @tp_getset@ */
896 0, /* @tp_descr_get@ */
897 0, /* @tp_descr_set@ */
898 0, /* @tp_dictoffset@ */
900 PyType_GenericAlloc, /* @tp_alloc@ */
901 abstract_pynew, /* @tp_new@ */
906 static PyTypeObject ghash_pytype_skel = {
907 PyObject_HEAD_INIT(0) 0, /* Header */
908 "GHash", /* @tp_name@ */
909 sizeof(ghash_pyobj), /* @tp_basicsize@ */
910 0, /* @tp_itemsize@ */
912 ghash_pydealloc, /* @tp_dealloc@ */
914 0, /* @tp_getattr@ */
915 0, /* @tp_setattr@ */
916 0, /* @tp_compare@ */
918 0, /* @tp_as_number@ */
919 0, /* @tp_as_sequence@ */
920 0, /* @tp_as_mapping@ */
924 0, /* @tp_getattro@ */
925 0, /* @tp_setattro@ */
926 0, /* @tp_as_buffer@ */
927 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
931 "Hash function, abstract base class.",
933 0, /* @tp_traverse@ */
935 0, /* @tp_richcompare@ */
936 0, /* @tp_weaklistoffset@ */
938 0, /* @tp_iternext@ */
939 ghash_pymethods, /* @tp_methods@ */
940 0, /* @tp_members@ */
944 0, /* @tp_descr_get@ */
945 0, /* @tp_descr_set@ */
946 0, /* @tp_dictoffset@ */
948 PyType_GenericAlloc, /* @tp_alloc@ */
949 abstract_pynew, /* @tp_new@ */
954 /*----- Message authentication --------------------------------------------*/
956 PyTypeObject *gcmac_pytype, *gmac_pytype, *gmhash_pytype;
958 CONVFUNC(gcmac, gcmac *, GCMAC_CM)
959 CONVFUNC(gmac, gmac *, GMAC_M)
960 CONVFUNC(gmhash, ghash *, GHASH_H)
962 static PyObject *gmac_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
964 char *kwlist[] = { "k", 0 };
968 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz))
970 if (keysz(sz, GCMAC_CM(ty)->keysz) != sz) VALERR("bad key length");
971 return (gmac_pywrap((PyObject *)ty,
972 GM_KEY(GCMAC_CM(ty), k, sz),
978 static PyObject *gmhash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
980 char *kwlist[] = { 0 };
983 if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", kwlist)) return (0);
984 g = PyObject_NEW(ghash_pyobj, ty);
985 g->h = GM_INIT(GMAC_M(ty));
988 return ((PyObject *)g);
991 PyObject *gcmac_pywrap(gcmac *cm)
993 gcmac_pyobj *g = newtype(gcmac_pytype, 0, cm->name);
995 g->ty.ht_type.tp_basicsize = sizeof(gmac_pyobj);
996 g->ty.ht_type.tp_base = gmac_pytype;
997 Py_INCREF(gmac_pytype);
998 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
999 Py_TPFLAGS_BASETYPE |
1000 Py_TPFLAGS_HEAPTYPE);
1001 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
1002 g->ty.ht_type.tp_free = 0;
1003 g->ty.ht_type.tp_new = gmac_pynew;
1004 typeready(&g->ty.ht_type);
1005 return ((PyObject *)g);
1008 PyObject *gmac_pywrap(PyObject *cobj, gmac *m, unsigned f)
1011 if (!cobj) cobj = gcmac_pywrap((/*unconst*/ gcmac *)GM_CLASS(m));
1012 else Py_INCREF(cobj);
1013 g = newtype((PyTypeObject *)cobj, 0, 0);
1014 g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj);
1015 g->ty.ht_name = PyString_FromFormat("%s(keyed)", m->ops->c->name);
1016 g->ty.ht_type.tp_name = PyString_AS_STRING(g->ty.ht_name);
1017 g->ty.ht_type.tp_base = gmhash_pytype;
1018 Py_INCREF(gmac_pytype);
1019 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
1020 Py_TPFLAGS_BASETYPE |
1021 Py_TPFLAGS_HEAPTYPE);
1022 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
1023 g->ty.ht_type.tp_free = 0;
1024 g->ty.ht_type.tp_new = gmhash_pynew;
1025 typeready(&g->ty.ht_type);
1028 return ((PyObject *)g);
1031 static void gmac_pydealloc(PyObject *me)
1033 if (GMAC_F(me) & f_freeme)
1034 GM_DESTROY(GMAC_M(me));
1035 Py_DECREF(me->ob_type);
1036 PyType_Type.tp_dealloc(me);
1039 static PyObject *gcmget_name(PyObject *me, void *hunoz)
1040 { return (PyString_FromString(GCMAC_CM(me)->name)); }
1042 static PyObject *gcmget_keysz(PyObject *me, void *hunoz)
1043 { return (keysz_pywrap(GCMAC_CM(me)->keysz)); }
1045 static PyObject *gcmget_tagsz(PyObject *me, void *hunoz)
1046 { return (PyInt_FromLong(GCMAC_CM(me)->hashsz)); }
1048 static PyGetSetDef gcmac_pygetset[] = {
1049 #define GETSETNAME(op, name) gcm##op##_##name
1050 GET (keysz, "CM.keysz -> acceptable key sizes")
1051 GET (tagsz, "CM.tagsz -> MAC output size")
1052 GET (name, "CM.name -> name of this kind of MAC")
1057 static PyTypeObject gcmac_pytype_skel = {
1058 PyObject_HEAD_INIT(0) 0, /* Header */
1059 "GCMAC", /* @tp_name@ */
1060 sizeof(gchash_pyobj), /* @tp_basicsize@ */
1061 0, /* @tp_itemsize@ */
1063 0, /* @tp_dealloc@ */
1065 0, /* @tp_getattr@ */
1066 0, /* @tp_setattr@ */
1067 0, /* @tp_compare@ */
1069 0, /* @tp_as_number@ */
1070 0, /* @tp_as_sequence@ */
1071 0, /* @tp_as_mapping@ */
1075 0, /* @tp_getattro@ */
1076 0, /* @tp_setattro@ */
1077 0, /* @tp_as_buffer@ */
1078 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1079 Py_TPFLAGS_BASETYPE,
1082 "Message authentication code metametaclass.",
1084 0, /* @tp_traverse@ */
1086 0, /* @tp_richcompare@ */
1087 0, /* @tp_weaklistoffset@ */
1089 0, /* @tp_iternext@ */
1090 0, /* @tp_methods@ */
1091 0, /* @tp_members@ */
1092 gcmac_pygetset, /* @tp_getset@ */
1095 0, /* @tp_descr_get@ */
1096 0, /* @tp_descr_set@ */
1097 0, /* @tp_dictoffset@ */
1099 PyType_GenericAlloc, /* @tp_alloc@ */
1100 abstract_pynew, /* @tp_new@ */
1105 static PyTypeObject gmac_pytype_skel = {
1106 PyObject_HEAD_INIT(0) 0, /* Header */
1107 "GMAC", /* @tp_name@ */
1108 sizeof(gmac_pyobj), /* @tp_basicsize@ */
1109 0, /* @tp_itemsize@ */
1111 gmac_pydealloc, /* @tp_dealloc@ */
1113 0, /* @tp_getattr@ */
1114 0, /* @tp_setattr@ */
1115 0, /* @tp_compare@ */
1117 0, /* @tp_as_number@ */
1118 0, /* @tp_as_sequence@ */
1119 0, /* @tp_as_mapping@ */
1123 0, /* @tp_getattro@ */
1124 0, /* @tp_setattro@ */
1125 0, /* @tp_as_buffer@ */
1126 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1127 Py_TPFLAGS_BASETYPE,
1130 "Message authentication code metaclass, abstract base class.",
1132 0, /* @tp_traverse@ */
1134 0, /* @tp_richcompare@ */
1135 0, /* @tp_weaklistoffset@ */
1137 0, /* @tp_iternext@ */
1138 0, /* @tp_methods@ */
1139 0, /* @tp_members@ */
1140 0, /* @tp_getset@ */
1143 0, /* @tp_descr_get@ */
1144 0, /* @tp_descr_set@ */
1145 0, /* @tp_dictoffset@ */
1147 PyType_GenericAlloc, /* @tp_alloc@ */
1148 abstract_pynew, /* @tp_new@ */
1153 static PyTypeObject gmhash_pytype_skel = {
1154 PyObject_HEAD_INIT(0) 0, /* Header */
1155 "GMACHash", /* @tp_name@ */
1156 sizeof(ghash_pyobj), /* @tp_basicsize@ */
1157 0, /* @tp_itemsize@ */
1159 ghash_pydealloc, /* @tp_dealloc@ */
1161 0, /* @tp_getattr@ */
1162 0, /* @tp_setattr@ */
1163 0, /* @tp_compare@ */
1165 0, /* @tp_as_number@ */
1166 0, /* @tp_as_sequence@ */
1167 0, /* @tp_as_mapping@ */
1171 0, /* @tp_getattro@ */
1172 0, /* @tp_setattro@ */
1173 0, /* @tp_as_buffer@ */
1174 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1175 Py_TPFLAGS_BASETYPE,
1178 "Message authentication code, abstract base class.",
1180 0, /* @tp_traverse@ */
1182 0, /* @tp_richcompare@ */
1183 0, /* @tp_weaklistoffset@ */
1185 0, /* @tp_iternext@ */
1186 0, /* @tp_methods@ */
1187 0, /* @tp_members@ */
1188 0, /* @tp_getset@ */
1191 0, /* @tp_descr_get@ */
1192 0, /* @tp_descr_set@ */
1193 0, /* @tp_dictoffset@ */
1195 PyType_GenericAlloc, /* @tp_alloc@ */
1196 abstract_pynew, /* @tp_new@ */
1201 /*----- Special snowflake for Poly1305 ------------------------------------*/
1203 PyTypeObject *poly1305cls_pytype, *poly1305key_pytype, *poly1305hash_pytype;
1205 typedef struct poly1305key_pyobj {
1206 PyHeapTypeObject ty;
1208 } poly1305key_pyobj;
1210 typedef struct poly1305hash_pyobj {
1215 } poly1305hash_pyobj;
1217 #define P1305_F(o) (((poly1305hash_pyobj *)(o))->f)
1218 #define P1305_CTX(o) (&((poly1305hash_pyobj *)(o))->ctx)
1219 CONVFUNC(poly1305hash, poly1305_ctx *, P1305_CTX)
1221 static PyObject *poly1305hash_pynew(PyTypeObject *ty,
1222 PyObject *arg, PyObject *kw)
1224 char *kwlist[] = { "mask", 0 };
1225 poly1305key_pyobj *pk = (poly1305key_pyobj *)ty;
1226 poly1305hash_pyobj *ph;
1230 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|s#:new", kwlist, &m, &sz))
1232 if (m && sz != POLY1305_MASKSZ) VALERR("bad mask length");
1233 ph = PyObject_NEW(poly1305hash_pyobj, ty);
1235 if (m) ph->f |= f_mask;
1236 poly1305_macinit(&ph->ctx, &pk->k, m);
1238 return ((PyObject *)ph);
1243 static PyObject *poly1305key_pynew(PyTypeObject *ty,
1244 PyObject *arg, PyObject *kw)
1246 char *kwlist[] = { "k", 0 };
1247 poly1305key_pyobj *pk;
1251 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz))
1253 if (keysz(sz, poly1305_keysz) != sz) VALERR("bad key length");
1255 pk = newtype(ty, 0, 0);
1256 pk->ty.ht_name = PyString_FromString("poly1305(keyed)");
1257 pk->ty.ht_type.tp_basicsize = sizeof(poly1305hash_pyobj);
1258 pk->ty.ht_type.tp_name = PyString_AS_STRING(pk->ty.ht_name);
1259 pk->ty.ht_type.tp_base = poly1305hash_pytype;
1260 Py_INCREF(poly1305key_pytype);
1261 pk->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
1262 Py_TPFLAGS_BASETYPE |
1263 Py_TPFLAGS_HEAPTYPE);
1264 pk->ty.ht_type.tp_alloc = PyType_GenericAlloc;
1265 pk->ty.ht_type.tp_free = 0;
1266 pk->ty.ht_type.tp_new = poly1305hash_pynew;
1267 typeready(&pk->ty.ht_type);
1269 poly1305_keyinit(&pk->k, k, sz);
1270 return ((PyObject *)pk);
1276 static PyObject *poly1305clsget_name(PyObject *me, void *hunoz)
1277 { return (PyString_FromString("poly1305")); }
1279 static PyObject *poly1305clsget_keysz(PyObject *me, void *hunoz)
1280 { return (keysz_pywrap(poly1305_keysz)); }
1282 static PyObject *poly1305clsget_masksz(PyObject *me, void *hunoz)
1283 { return (PyInt_FromLong(POLY1305_MASKSZ)); }
1285 static PyObject *poly1305clsget_tagsz(PyObject *me, void *hunoz)
1286 { return (PyInt_FromLong(POLY1305_TAGSZ)); }
1288 static PyObject *polymeth_copy(PyObject *me, PyObject *arg)
1290 poly1305hash_pyobj *ph;
1291 if (!PyArg_ParseTuple(arg, ":copy")) return (0);
1292 ph = PyObject_NEW(poly1305hash_pyobj, me->ob_type);
1293 poly1305_copy(&ph->ctx, P1305_CTX(me));
1294 Py_INCREF(me->ob_type);
1295 return ((PyObject *)ph);
1298 static PyObject *polymeth_hash(PyObject *me, PyObject *arg)
1302 if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0);
1303 poly1305_hash(P1305_CTX(me), p, sz);
1307 #define POLYMETH_HASHU_(n, W, w) \
1308 static PyObject *polymeth_hashu##w(PyObject *me, PyObject *arg) \
1312 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \
1313 STORE##W(b, x); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \
1318 DOUINTCONV(POLYMETH_HASHU_)
1320 #define POLYMETH_HASHBUF_(n, W, w) \
1321 static PyObject *polymeth_hashbuf##w(PyObject *me, PyObject *arg) \
1326 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
1327 if (sz > MASK##n) TYERR("string too long"); \
1328 STORE##W(b, sz); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \
1329 poly1305_hash(P1305_CTX(me), p, sz); \
1334 DOUINTCONV(POLYMETH_HASHBUF_)
1336 static PyObject *polymeth_hashstrz(PyObject *me, PyObject *arg)
1339 if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0);
1340 poly1305_hash(P1305_CTX(me), p, strlen(p) + 1);
1344 static PyObject *polymeth_flush(PyObject *me, PyObject *arg)
1346 if (!PyArg_ParseTuple(arg, ":flush")) return (0);
1347 poly1305_flush(P1305_CTX(me));
1351 static PyObject *polymeth_flushzero(PyObject *me, PyObject *arg)
1353 if (!PyArg_ParseTuple(arg, ":flushzero")) return (0);
1354 poly1305_flushzero(P1305_CTX(me));
1358 static PyObject *polymeth_concat(PyObject *me, PyObject *arg)
1360 PyObject *pre, *suff;
1361 if (!PyArg_ParseTuple(arg, "OO:concat", &pre, &suff)) return (0);
1362 if (!PyObject_TypeCheck(pre, poly1305hash_pytype) ||
1363 !PyObject_TypeCheck(suff, poly1305hash_pytype))
1364 TYERR("wanted a poly1305hash");
1365 if (me->ob_type != pre->ob_type || me->ob_type != suff->ob_type)
1366 TYERR("key mismatch");
1367 if (P1305_CTX(pre)->nbuf) VALERR("prefix is not block-aligned");
1368 poly1305_concat(P1305_CTX(me), P1305_CTX(pre), P1305_CTX(suff));
1374 static PyObject *polymeth_done(PyObject *me, PyObject *arg)
1377 if (!PyArg_ParseTuple(arg, ":done")) return (0);
1378 if (!(P1305_F(me) & f_mask)) VALERR("no mask");
1379 rc = bytestring_pywrap(0, POLY1305_TAGSZ);
1380 poly1305_done(P1305_CTX(me), PyString_AS_STRING(rc));
1386 static PyGetSetDef poly1305cls_pygetset[] = {
1387 #define GETSETNAME(op, name) poly1305cls##op##_##name
1388 GET (keysz, "PC.keysz -> acceptable key sizes")
1389 GET (masksz, "PC.masksz -> mask size")
1390 GET (tagsz, "PC.tagsz -> MAC output size")
1391 GET (name, "PC.name -> name of this kind of MAC")
1396 static PyMethodDef poly1305hash_pymethods[] = {
1397 #define METHNAME(name) polymeth_##name
1398 METH (copy, "P.copy() -> PP")
1399 METH (hash, "P.hash(M)")
1400 #define METHU_(n, W, w) METH(hashu##w, "P.hashu" #w "(WORD)")
1403 #define METHBUF_(n, W, w) METH(hashbuf##w, "P.hashbuf" #w "(BYTES)")
1404 DOUINTCONV(METHBUF_)
1406 METH (hashstrz, "P.hashstrz(STRING)")
1407 METH (flush, "P.flush()")
1408 METH (flushzero, "P.flushzero()")
1409 METH (concat, "P.concat(PREFIX, SUFFIX)")
1410 METH (done, "P.done() -> TAG")
1415 static PyTypeObject poly1305cls_pytype_skel = {
1416 PyObject_HEAD_INIT(0) 0, /* Header */
1417 "Poly1305Class", /* @tp_name@ */
1418 sizeof(PyHeapTypeObject), /* @tp_basicsize@ */
1419 0, /* @tp_itemsize@ */
1421 0, /* @tp_dealloc@ */
1423 0, /* @tp_getattr@ */
1424 0, /* @tp_setattr@ */
1425 0, /* @tp_compare@ */
1427 0, /* @tp_as_number@ */
1428 0, /* @tp_as_sequence@ */
1429 0, /* @tp_as_mapping@ */
1433 0, /* @tp_getattro@ */
1434 0, /* @tp_setattro@ */
1435 0, /* @tp_as_buffer@ */
1436 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1437 Py_TPFLAGS_BASETYPE,
1440 "Poly1305 metametaclass. Best not to ask.",
1442 0, /* @tp_traverse@ */
1444 0, /* @tp_richcompare@ */
1445 0, /* @tp_weaklistoffset@ */
1447 0, /* @tp_iternext@ */
1448 0, /* @tp_methods@ */
1449 0, /* @tp_members@ */
1450 poly1305cls_pygetset, /* @tp_getset@ */
1453 0, /* @tp_descr_get@ */
1454 0, /* @tp_descr_set@ */
1455 0, /* @tp_dictoffset@ */
1457 PyType_GenericAlloc, /* @tp_alloc@ */
1458 abstract_pynew, /* @tp_new@ */
1463 static PyTypeObject poly1305key_pytype_skel = {
1464 PyObject_HEAD_INIT(0) 0, /* Header */
1465 "poly1305", /* @tp_name@ */
1466 sizeof(poly1305key_pyobj), /* @tp_basicsize@ */
1467 0, /* @tp_itemsize@ */
1469 0, /* @tp_dealloc@ */
1471 0, /* @tp_getattr@ */
1472 0, /* @tp_setattr@ */
1473 0, /* @tp_compare@ */
1475 0, /* @tp_as_number@ */
1476 0, /* @tp_as_sequence@ */
1477 0, /* @tp_as_mapping@ */
1481 0, /* @tp_getattro@ */
1482 0, /* @tp_setattro@ */
1483 0, /* @tp_as_buffer@ */
1484 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1485 Py_TPFLAGS_BASETYPE,
1490 0, /* @tp_traverse@ */
1492 0, /* @tp_richcompare@ */
1493 0, /* @tp_weaklistoffset@ */
1495 0, /* @tp_iternext@ */
1496 0, /* @tp_methods@ */
1497 0, /* @tp_members@ */
1498 0, /* @tp_getset@ */
1501 0, /* @tp_descr_get@ */
1502 0, /* @tp_descr_set@ */
1503 0, /* @tp_dictoffset@ */
1505 PyType_GenericAlloc, /* @tp_alloc@ */
1506 poly1305key_pynew, /* @tp_new@ */
1511 static PyTypeObject poly1305hash_pytype_skel = {
1512 PyObject_HEAD_INIT(0) 0, /* Header */
1513 "Poly1305Hash", /* @tp_name@ */
1514 sizeof(poly1305hash_pyobj), /* @tp_basicsize@ */
1515 0, /* @tp_itemsize@ */
1517 0, /* @tp_dealloc@ */
1519 0, /* @tp_getattr@ */
1520 0, /* @tp_setattr@ */
1521 0, /* @tp_compare@ */
1523 0, /* @tp_as_number@ */
1524 0, /* @tp_as_sequence@ */
1525 0, /* @tp_as_mapping@ */
1529 0, /* @tp_getattro@ */
1530 0, /* @tp_setattro@ */
1531 0, /* @tp_as_buffer@ */
1532 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1533 Py_TPFLAGS_BASETYPE,
1536 "Poly1305 MAC context base class.",
1538 0, /* @tp_traverse@ */
1540 0, /* @tp_richcompare@ */
1541 0, /* @tp_weaklistoffset@ */
1543 0, /* @tp_iternext@ */
1544 poly1305hash_pymethods, /* @tp_methods@ */
1545 0, /* @tp_members@ */
1546 0, /* @tp_getset@ */
1549 0, /* @tp_descr_get@ */
1550 0, /* @tp_descr_set@ */
1551 0, /* @tp_dictoffset@ */
1553 PyType_GenericAlloc, /* @tp_alloc@ */
1554 abstract_pynew, /* @tp_new@ */
1559 /*----- Special snowflake for HSalsa and HChaCha --------------------------*/
1561 #define DEF_HDANCE(DANCE, HDANCE, dance, hdance) \
1562 static PyObject *meth_##hdance##_prf(PyObject *me, PyObject *arg) \
1564 dance##_ctx dance; \
1566 Py_ssize_t ksz, nsz; \
1568 if (!PyArg_ParseTuple(arg, "s#s#:" #hdance "_prf", \
1569 &k, &ksz, &n, &nsz)) \
1571 if (ksz != keysz(ksz, dance##_keysz)) VALERR("bad key length"); \
1572 if (nsz != HDANCE##_INSZ) VALERR("bad input length"); \
1573 rc = bytestring_pywrap(0, HSALSA20_OUTSZ); \
1574 dance##_init(&dance, k, ksz, 0); \
1575 hdance##_prf(&dance, n, PyString_AS_STRING(rc)); \
1581 DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa20)
1582 DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa2012)
1583 DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa208)
1585 DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha20)
1586 DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha12)
1587 DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha8)
1589 /*----- Keccak-p[1600, n] -------------------------------------------------*/
1591 static PyTypeObject *kxvik_pytype;
1593 typedef struct kxvik_pyobj {
1599 static PyObject *kxvik_pynew(PyTypeObject *ty,
1600 PyObject *arg, PyObject *kw)
1603 kxvik_pyobj *rc = 0;
1604 char *kwlist[] = { "nround", 0 };
1605 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", kwlist,
1608 rc = (kxvik_pyobj *)ty->tp_alloc(ty, 0);
1610 keccak1600_init(&rc->s);
1612 return ((PyObject *)rc);
1615 static PyObject *kxvikmeth_mix(PyObject *me, PyObject *arg)
1617 kxvik_pyobj *k = (kxvik_pyobj *)me;
1622 char *p; Py_ssize_t n;
1624 if (!PyArg_ParseTuple(arg, "s#:mix", &p, &n)) goto end;
1625 if (n > 200) VALERR("out of range");
1626 q = (const octet *)p;
1628 while (n > 8) { LOAD64_L_(t[i], q); i++; q += 8; n -= 8; }
1630 memcpy(buf, q, n); memset(buf + n, 0, 8 - n);
1631 LOAD64_L_(t[i], buf); i++;
1633 keccak1600_mix(&k->s, t, i);
1639 static PyObject *kxvikmeth_extract(PyObject *me, PyObject *arg)
1641 kxvik_pyobj *k = (kxvik_pyobj *)me;
1648 if (!PyArg_ParseTuple(arg, "O&:mix", convuint, &n)) goto end;
1649 if (n > 200) VALERR("out of range");
1650 rc = bytestring_pywrap(0, n);
1651 q = (octet *)PyString_AS_STRING(rc);
1652 keccak1600_extract(&k->s, t, (n + 7)/8);
1654 while (n > 8) { STORE64_L_(q, t[i]); i++; q += 8; n -= 8; }
1655 if (n) { STORE64_L_(buf, t[i]); memcpy(q, buf, n); }
1660 static PyObject *kxvikmeth_step(PyObject *me, PyObject *arg)
1662 kxvik_pyobj *k = (kxvik_pyobj *)me;
1663 if (!PyArg_ParseTuple(arg, ":step")) return (0);
1664 keccak1600_p(&k->s, &k->s, k->n);
1668 static PyObject *kxvikget_nround(PyObject *me, void *hunoz)
1670 kxvik_pyobj *k = (kxvik_pyobj *)me;
1671 return (PyInt_FromLong(k->n));
1674 static int kxvikset_nround(PyObject *me, PyObject *val, void *hunoz)
1676 kxvik_pyobj *k = (kxvik_pyobj *)me;
1680 if (!val) NIERR("__del__");
1681 if (!convuint(val, &n)) goto end;
1688 static PyGetSetDef kxvik_pygetset[] = {
1689 #define GETSETNAME(op, name) kxvik##op##_##name
1690 GETSET(nround, "KECCAK.nround -> number of rounds")
1695 static PyMethodDef kxvik_pymethods[] = {
1696 #define METHNAME(func) kxvikmeth_##func
1697 METH (mix, "KECCAK.mix(DATA)")
1698 METH (extract, "KECCAK.extract(NOCTETS)")
1699 METH (step, "KECCAK.step()")
1704 static PyTypeObject kxvik_pytype_skel = {
1705 PyObject_HEAD_INIT(0) 0, /* Header */
1706 "Keccak1600", /* @tp_name@ */
1707 sizeof(kxvik_pyobj), /* @tp_basicsize@ */
1708 0, /* @tp_itemsize@ */
1710 0, /* @tp_dealloc@ */
1712 0, /* @tp_getattr@ */
1713 0, /* @tp_setattr@ */
1714 0, /* @tp_compare@ */
1716 0, /* @tp_as_number@ */
1717 0, /* @tp_as_sequence@ */
1718 0, /* @tp_as_mapping@ */
1722 0, /* @tp_getattro@ */
1723 0, /* @tp_setattro@ */
1724 0, /* @tp_as_buffer@ */
1725 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1726 Py_TPFLAGS_BASETYPE,
1729 "Keccak-p[1600, n] state.",
1731 0, /* @tp_traverse@ */
1733 0, /* @tp_richcompare@ */
1734 0, /* @tp_weaklistoffset@ */
1736 0, /* @tp_iternext@ */
1737 kxvik_pymethods, /* @tp_methods@ */
1738 0, /* @tp_members@ */
1739 kxvik_pygetset, /* @tp_getset@ */
1742 0, /* @tp_descr_get@ */
1743 0, /* @tp_descr_set@ */
1744 0, /* @tp_dictoffset@ */
1746 PyType_GenericAlloc, /* @tp_alloc@ */
1747 kxvik_pynew, /* @tp_new@ */
1752 static PyTypeObject *shake_pytype, *shake128_pytype, *shake256_pytype;
1754 typedef struct shake_pyobj {
1760 #define SHAKE_H(o) (&((shake_pyobj *)(o))->h)
1761 #define SHAKE_ST(o) (((shake_pyobj *)(o))->st)
1763 static PyObject *shake_dopynew(void (*initfn)(shake_ctx *,
1764 const void *, size_t,
1765 const void *, size_t),
1767 PyObject *arg, PyObject *kw)
1769 shake_pyobj *rc = 0;
1770 char *p = 0, *f = 0;
1771 Py_ssize_t psz = 0, fsz = 0;
1772 char *kwlist[] = { "perso", "func", 0 };
1774 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|s#s#:new", kwlist,
1775 &p, &psz, &f, &fsz))
1777 rc = (shake_pyobj *)ty->tp_alloc(ty, 0);
1778 initfn(&rc->h, f, fsz, p, psz);
1781 return ((PyObject *)rc);
1784 static PyObject *shake128_pynew(PyTypeObject *ty,
1785 PyObject *arg, PyObject *kw)
1786 { return (shake_dopynew(cshake128_init, ty, arg, kw)); }
1788 static PyObject *shake256_pynew(PyTypeObject *ty,
1789 PyObject *arg, PyObject *kw)
1790 { return (shake_dopynew(cshake256_init, ty, arg, kw)); }
1792 static int shake_check(PyObject *me, int st)
1794 if (SHAKE_ST(me) != st) VALERR("wrong state");
1800 static PyObject *shakemeth_hash(PyObject *me, PyObject *arg)
1804 if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0);
1805 if (shake_check(me, 0)) return (0);
1806 shake_hash(SHAKE_H(me), p, sz);
1810 #define SHAKEMETH_HASHU_(n, W, w) \
1811 static PyObject *shakemeth_hashu##w(PyObject *me, PyObject *arg) \
1815 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \
1816 if (shake_check(me, 0)) goto end; \
1817 STORE##W(b, x); shake_hash(SHAKE_H(me), b, sizeof(b)); \
1822 DOUINTCONV(SHAKEMETH_HASHU_)
1824 #define SHAKEMETH_HASHBUF_(n, W, w) \
1825 static PyObject *shakemeth_hashbuf##w(PyObject *me, PyObject *arg) \
1830 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
1831 if (sz > MASK##n) TYERR("string too long"); \
1832 if (shake_check(me, 0)) goto end; \
1833 STORE##W(b, sz); shake_hash(SHAKE_H(me), b, sizeof(b)); \
1834 shake_hash(SHAKE_H(me), p, sz); \
1839 DOUINTCONV(SHAKEMETH_HASHBUF_)
1841 static PyObject *shakemeth_hashstrz(PyObject *me, PyObject *arg)
1844 if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0);
1845 if (shake_check(me, 0)) return (0);
1846 shake_hash(SHAKE_H(me), p, strlen(p) + 1);
1850 static PyObject *shakemeth_xof(PyObject *me, PyObject *arg)
1852 if (!PyArg_ParseTuple(arg, ":xof")) goto end;
1853 if (shake_check(me, 0)) goto end;
1854 shake_xof(SHAKE_H(me));
1861 static PyObject *shakemeth_done(PyObject *me, PyObject *arg)
1865 if (!PyArg_ParseTuple(arg, "O&:done", convszt, &n)) goto end;
1866 if (shake_check(me, 0)) goto end;
1867 rc = bytestring_pywrap(0, n);
1868 shake_done(SHAKE_H(me), PyString_AS_STRING(rc), n);
1874 static PyObject *shakemeth_copy(PyObject *me, PyObject *arg)
1876 shake_pyobj *rc = 0;
1878 if (!PyArg_ParseTuple(arg, ":copy")) goto end;
1879 rc = PyObject_NEW(shake_pyobj, me->ob_type);
1880 rc->h = *SHAKE_H(me);
1881 rc->st = SHAKE_ST(me);
1883 return ((PyObject *)me);
1886 static PyObject *shakemeth_get(PyObject *me, PyObject *arg)
1891 if (!PyArg_ParseTuple(arg, "O&:get", convszt, &sz)) goto end;
1892 if (shake_check(me, 1)) goto end;
1893 rc = bytestring_pywrap(0, sz);
1894 shake_get(SHAKE_H(me), PyString_AS_STRING(rc), sz);
1899 static PyObject *shakemeth_mask(PyObject *me, PyObject *arg)
1902 char *p; Py_ssize_t sz;
1904 if (!PyArg_ParseTuple(arg, "s#:mask", &p, &sz)) goto end;
1905 if (shake_check(me, 1)) goto end;
1906 rc = bytestring_pywrap(0, sz);
1907 shake_mask(SHAKE_H(me), p, PyString_AS_STRING(rc), sz);
1912 static PyObject *shakeget_rate(PyObject *me, void *hunoz)
1913 { return (PyInt_FromLong(SHAKE_H(me)->h.r)); }
1915 static PyObject *shakeget_buffered(PyObject *me, void *hunoz)
1916 { return (PyInt_FromLong(SHAKE_H(me)->h.n)); }
1918 static PyObject *shakeget_state(PyObject *me, void *hunoz)
1920 int st = SHAKE_ST(me);
1921 return (PyString_FromString(st == 0 ? "absorb" :
1922 st == 1 ? "squeeze" : "dead"));
1925 static PyGetSetDef shake_pygetset[] = {
1926 #define GETSETNAME(op, name) shake##op##_##name
1927 GET (rate, "S.rate -> rate, in bytes")
1928 GET (buffered, "S.buffered -> amount currently buffered")
1929 GET (state, "S.state -> `absorb', `squeeze', `dead'")
1934 static PyMethodDef shake_pymethods[] = {
1935 #define METHNAME(func) shakemeth_##func
1936 METH (copy, "S.copy() -> SS")
1937 METH (hash, "S.hash(M)")
1938 #define METHU_(n, W, w) METH(hashu##w, "S.hashu" #w "(WORD)")
1941 #define METHBUF_(n, W, w) METH(hashbuf##w, "S.hashbuf" #w "(BYTES)")
1942 DOUINTCONV(METHBUF_)
1944 METH (hashstrz, "S.hashstrz(STRING)")
1945 METH (xof, "S.xof()")
1946 METH (done, "S.done(LEN) ->H")
1947 METH (get, "S.get(LEN) -> H")
1948 METH (mask, "S.mask(M) -> C")
1953 static PyTypeObject shake_pytype_skel = {
1954 PyObject_HEAD_INIT(0) 0, /* Header */
1955 "Shake", /* @tp_name@ */
1956 sizeof(shake_pyobj), /* @tp_basicsize@ */
1957 0, /* @tp_itemsize@ */
1959 0, /* @tp_dealloc@ */
1961 0, /* @tp_getattr@ */
1962 0, /* @tp_setattr@ */
1963 0, /* @tp_compare@ */
1965 0, /* @tp_as_number@ */
1966 0, /* @tp_as_sequence@ */
1967 0, /* @tp_as_mapping@ */
1971 0, /* @tp_getattro@ */
1972 0, /* @tp_setattro@ */
1973 0, /* @tp_as_buffer@ */
1974 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1975 Py_TPFLAGS_BASETYPE,
1978 "SHAKE/cSHAKE base class.",
1980 0, /* @tp_traverse@ */
1982 0, /* @tp_richcompare@ */
1983 0, /* @tp_weaklistoffset@ */
1985 0, /* @tp_iternext@ */
1986 shake_pymethods, /* @tp_methods@ */
1987 0, /* @tp_members@ */
1988 shake_pygetset, /* @tp_getset@ */
1991 0, /* @tp_descr_get@ */
1992 0, /* @tp_descr_set@ */
1993 0, /* @tp_dictoffset@ */
1995 PyType_GenericAlloc, /* @tp_alloc@ */
1996 abstract_pynew, /* @tp_new@ */
2001 static PyTypeObject shake128_pytype_skel = {
2002 PyObject_HEAD_INIT(0) 0, /* Header */
2003 "Shake128", /* @tp_name@ */
2004 0, /* @tp_basicsize@ */
2005 0, /* @tp_itemsize@ */
2007 0, /* @tp_dealloc@ */
2009 0, /* @tp_getattr@ */
2010 0, /* @tp_setattr@ */
2011 0, /* @tp_compare@ */
2013 0, /* @tp_as_number@ */
2014 0, /* @tp_as_sequence@ */
2015 0, /* @tp_as_mapping@ */
2019 0, /* @tp_getattro@ */
2020 0, /* @tp_setattro@ */
2021 0, /* @tp_as_buffer@ */
2022 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2023 Py_TPFLAGS_BASETYPE,
2026 "SHAKE128/cSHAKE128 XOF.",
2028 0, /* @tp_traverse@ */
2030 0, /* @tp_richcompare@ */
2031 0, /* @tp_weaklistoffset@ */
2033 0, /* @tp_iternext@ */
2034 0, /* @tp_methods@ */
2035 0, /* @tp_members@ */
2036 0, /* @tp_getset@ */
2039 0, /* @tp_descr_get@ */
2040 0, /* @tp_descr_set@ */
2041 0, /* @tp_dictoffset@ */
2043 PyType_GenericAlloc, /* @tp_alloc@ */
2044 shake128_pynew, /* @tp_new@ */
2049 static PyTypeObject shake256_pytype_skel = {
2050 PyObject_HEAD_INIT(0) 0, /* Header */
2051 "Shake256", /* @tp_name@ */
2052 0, /* @tp_basicsize@ */
2053 0, /* @tp_itemsize@ */
2055 0, /* @tp_dealloc@ */
2057 0, /* @tp_getattr@ */
2058 0, /* @tp_setattr@ */
2059 0, /* @tp_compare@ */
2061 0, /* @tp_as_number@ */
2062 0, /* @tp_as_sequence@ */
2063 0, /* @tp_as_mapping@ */
2067 0, /* @tp_getattro@ */
2068 0, /* @tp_setattro@ */
2069 0, /* @tp_as_buffer@ */
2070 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2071 Py_TPFLAGS_BASETYPE,
2074 "SHAKE256/cSHAKE256 XOF.",
2076 0, /* @tp_traverse@ */
2078 0, /* @tp_richcompare@ */
2079 0, /* @tp_weaklistoffset@ */
2081 0, /* @tp_iternext@ */
2082 0, /* @tp_methods@ */
2083 0, /* @tp_members@ */
2084 0, /* @tp_getset@ */
2087 0, /* @tp_descr_get@ */
2088 0, /* @tp_descr_set@ */
2089 0, /* @tp_dictoffset@ */
2091 PyType_GenericAlloc, /* @tp_alloc@ */
2092 shake256_pynew, /* @tp_new@ */
2097 /*----- Pseudorandom permutations -----------------------------------------*/
2099 static PyTypeObject *gcprp_pytype, *gprp_pytype;
2101 typedef struct prpinfo {
2106 void (*init)(void *, const void *, size_t);
2107 void (*eblk)(void *, const void *, void *);
2108 void (*dblk)(void *, const void *, void *);
2111 #define PRP_DEF(PRE, pre) \
2112 static void pre##_prpinit(void *ctx, const void *k, size_t ksz) \
2113 { pre##_init(ctx, k, ksz); } \
2114 static void pre##_prpeblk(void *ctx, const void *in, void *out) \
2116 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
2117 pre##_eblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
2119 static void pre##_prpdblk(void *ctx, const void *in, void *out) \
2121 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
2122 pre##_dblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
2124 static const prpinfo pre##_prpinfo = { \
2125 #pre, pre##_keysz, sizeof(pre##_ctx), PRE##_BLKSZ, \
2126 pre##_prpinit, pre##_prpeblk, pre##_prpdblk \
2130 static const struct prpinfo *const gprptab[] = {
2131 #define PRP_ENTRY(PRE, pre) &pre##_prpinfo,
2136 typedef struct gcprp_pyobj {
2137 PyHeapTypeObject ty;
2140 #define GCPRP_PRP(o) (((gcprp_pyobj *)(o))->prp)
2142 typedef struct gprp_pyobj {
2146 #define GPRP_PRP(o) (((gprp_pyobj *)(o))->prp)
2147 #define GPRP_CTX(o) (((gprp_pyobj *)(o)) + 1)
2149 typedef struct prp {
2154 static PyObject *gprp_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
2156 char *kwlist[] = { "key", 0 };
2159 const prpinfo *prp = GCPRP_PRP(ty);
2162 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz))
2164 if (keysz(sz, prp->keysz) != sz) VALERR("bad key length");
2165 me = (PyObject *)ty->tp_alloc(ty, 0);
2167 prp->init(GPRP_CTX(me), k, sz);
2174 static void gprp_pydealloc(PyObject *me)
2175 { Py_DECREF(me->ob_type); FREEOBJ(me); }
2177 static PyObject *gcprp_pywrap(const prpinfo *prp)
2179 gcprp_pyobj *g = newtype(gcprp_pytype, 0, prp->name);
2181 g->ty.ht_type.tp_basicsize = sizeof(gprp_pyobj) + prp->ctxsz;
2182 g->ty.ht_type.tp_base = gprp_pytype;
2183 Py_INCREF(gprp_pytype);
2184 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
2185 Py_TPFLAGS_BASETYPE |
2186 Py_TPFLAGS_HEAPTYPE);
2187 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
2188 g->ty.ht_type.tp_free = 0;
2189 g->ty.ht_type.tp_new = gprp_pynew;
2190 typeready(&g->ty.ht_type);
2191 return ((PyObject *)g);
2194 static PyObject *gcpget_name(PyObject *me, void *hunoz)
2195 { return (PyString_FromString(GCPRP_PRP(me)->name)); }
2196 static PyObject *gcpget_keysz(PyObject *me, void *hunoz)
2197 { return (keysz_pywrap(GCPRP_PRP(me)->keysz)); }
2198 static PyObject *gcpget_blksz(PyObject *me, void *hunoz)
2199 { return (PyInt_FromLong(GCPRP_PRP(me)->blksz)); }
2201 static PyObject *gpmeth_encrypt(PyObject *me, PyObject *arg)
2207 if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &n)) goto end;
2208 if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length");
2209 rc = bytestring_pywrap(0, n);
2210 GPRP_PRP(me)->eblk(GPRP_CTX(me), p, PyString_AS_STRING(rc));
2215 static PyObject *gpmeth_decrypt(PyObject *me, PyObject *arg)
2221 if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &n)) goto end;
2222 if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length");
2223 rc = bytestring_pywrap(0, n);
2224 GPRP_PRP(me)->dblk(GPRP_CTX(me), p, PyString_AS_STRING(rc));
2229 static PyGetSetDef gcprp_pygetset[] = {
2230 #define GETSETNAME(op, name) gcp##op##_##name
2231 GET (keysz, "CP.keysz -> acceptable key sizes")
2232 GET (blksz, "CP.blksz -> block size")
2233 GET (name, "CP.name -> name of this kind of PRP")
2238 static PyMethodDef gprp_pymethods[] = {
2239 #define METHNAME(name) gpmeth_##name
2240 METH (encrypt, "P.encrypt(PT) -> CT")
2241 METH (decrypt, "P.decrypt(CT) -> PT")
2246 static PyTypeObject gcprp_pytype_skel = {
2247 PyObject_HEAD_INIT(0) 0, /* Header */
2248 "GCPRP", /* @tp_name@ */
2249 sizeof(gcprp_pyobj), /* @tp_basicsize@ */
2250 0, /* @tp_itemsize@ */
2252 0, /* @tp_dealloc@ */
2254 0, /* @tp_getattr@ */
2255 0, /* @tp_setattr@ */
2256 0, /* @tp_compare@ */
2258 0, /* @tp_as_number@ */
2259 0, /* @tp_as_sequence@ */
2260 0, /* @tp_as_mapping@ */
2264 0, /* @tp_getattro@ */
2265 0, /* @tp_setattro@ */
2266 0, /* @tp_as_buffer@ */
2267 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2268 Py_TPFLAGS_BASETYPE,
2271 "Pseudorandom permutation metaclass.",
2273 0, /* @tp_traverse@ */
2275 0, /* @tp_richcompare@ */
2276 0, /* @tp_weaklistoffset@ */
2278 0, /* @tp_iternext@ */
2279 0, /* @tp_methods@ */
2280 0, /* @tp_members@ */
2281 gcprp_pygetset, /* @tp_getset@ */
2284 0, /* @tp_descr_get@ */
2285 0, /* @tp_descr_set@ */
2286 0, /* @tp_dictoffset@ */
2288 PyType_GenericAlloc, /* @tp_alloc@ */
2289 abstract_pynew, /* @tp_new@ */
2294 static PyTypeObject gprp_pytype_skel = {
2295 PyObject_HEAD_INIT(0) 0, /* Header */
2296 "GPRP", /* @tp_name@ */
2297 sizeof(gprp_pyobj), /* @tp_basicsize@ */
2298 0, /* @tp_itemsize@ */
2300 gprp_pydealloc, /* @tp_dealloc@ */
2302 0, /* @tp_getattr@ */
2303 0, /* @tp_setattr@ */
2304 0, /* @tp_compare@ */
2306 0, /* @tp_as_number@ */
2307 0, /* @tp_as_sequence@ */
2308 0, /* @tp_as_mapping@ */
2312 0, /* @tp_getattro@ */
2313 0, /* @tp_setattro@ */
2314 0, /* @tp_as_buffer@ */
2315 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2316 Py_TPFLAGS_BASETYPE,
2319 "Pseudorandom permutation, abstract base class.",
2321 0, /* @tp_traverse@ */
2323 0, /* @tp_richcompare@ */
2324 0, /* @tp_weaklistoffset@ */
2326 0, /* @tp_iternext@ */
2327 gprp_pymethods, /* @tp_methods@ */
2328 0, /* @tp_members@ */
2329 0, /* @tp_getset@ */
2332 0, /* @tp_descr_get@ */
2333 0, /* @tp_descr_set@ */
2334 0, /* @tp_dictoffset@ */
2336 PyType_GenericAlloc, /* @tp_alloc@ */
2337 abstract_pynew, /* @tp_new@ */
2342 /*----- Main code ---------------------------------------------------------*/
2344 static PyMethodDef methods[] = {
2345 #define METHNAME(func) meth_##func
2346 METH (_KeySZ_fromdl, "\
2347 fromdl(N) -> M: convert integer discrete log field size to work factor")
2348 METH (_KeySZ_fromschnorr, "\
2349 fromschnorr(N) -> M: convert Schnorr group order to work factor")
2350 METH (_KeySZ_fromif, "\
2351 fromif(N) -> M: convert integer factorization problem size to work factor")
2352 METH (_KeySZ_fromec, "\
2353 fromec(N) -> M: convert elliptic curve group order to work factor")
2354 METH (_KeySZ_todl, "\
2355 todl(N) -> M: convert work factor to integer discrete log field size")
2356 METH (_KeySZ_toschnorr, "\
2357 toschnorr(N) -> M: convert work factor to Schnorr group order")
2358 METH (_KeySZ_toif, "\
2359 toif(N) -> M: convert work factor to integer factorization problem size")
2360 METH (_KeySZ_toec, "\
2361 toec(N) -> M: convert work factor to elliptic curve group order")
2362 METH (_KeySZ_toec, "\
2363 toec(N) -> M: convert work factor to elliptic curve group order")
2364 #define METH_HDANCE(hdance, HDance) METH(hdance##_prf, "\
2365 " #hdance "_prf(K, N) -> H: calculate " HDance " hash of N with K")
2366 METH_HDANCE(hsalsa20, "HSalsa20")
2367 METH_HDANCE(hsalsa2012, "HSalsa20/12")
2368 METH_HDANCE(hsalsa208, "HSalsa20/8")
2369 METH_HDANCE(hchacha20, "HChaCha20")
2370 METH_HDANCE(hchacha12, "HChaCha12")
2371 METH_HDANCE(hchacha8, "HChaCha8")
2377 void algorithms_pyinit(void)
2379 INITTYPE(keysz, root);
2380 INITTYPE(keyszany, keysz);
2381 INITTYPE(keyszrange, keysz);
2382 INITTYPE(keyszset, keysz);
2383 INITTYPE(gccipher, type);
2384 INITTYPE(gcipher, root);
2385 INITTYPE(gchash, type);
2386 INITTYPE(ghash, root);
2387 INITTYPE(gcmac, type);
2388 INITTYPE(gmac, type);
2389 INITTYPE(gmhash, ghash);
2390 INITTYPE(poly1305cls, type);
2391 INITTYPE_META(poly1305key, type, poly1305cls);
2392 INITTYPE(poly1305hash, root);
2393 INITTYPE(kxvik, root);
2394 INITTYPE(shake, root);
2395 INITTYPE(shake128, shake);
2396 INITTYPE(shake256, shake);
2397 INITTYPE(gcprp, type);
2398 INITTYPE(gprp, root);
2399 addmethods(methods);
2402 GEN(gcciphers, cipher)
2405 #define gcprp prpinfo
2408 void algorithms_pyinsert(PyObject *mod)
2411 INSERT("KeySZ", keysz_pytype);
2412 INSERT("KeySZAny", keyszany_pytype);
2413 INSERT("KeySZRange", keyszrange_pytype);
2414 INSERT("KeySZSet", keyszset_pytype);
2415 INSERT("GCCipher", gccipher_pytype);
2416 INSERT("GCipher", gcipher_pytype);
2417 INSERT("gcciphers", gcciphers());
2418 INSERT("GCHash", gchash_pytype);
2419 INSERT("GHash", ghash_pytype);
2420 INSERT("gchashes", d = gchashes());
2421 sha_pyobj = PyDict_GetItemString(d, "sha"); Py_INCREF(sha_pyobj);
2422 has160_pyobj = PyDict_GetItemString(d, "has160"); Py_INCREF(has160_pyobj);
2423 INSERT("GCMAC", gcmac_pytype);
2424 INSERT("GMAC", gmac_pytype);
2425 INSERT("GMACHash", gmhash_pytype);
2426 INSERT("gcmacs", gcmacs());
2427 INSERT("Poly1305Class", poly1305cls_pytype);
2428 INSERT("poly1305", poly1305key_pytype);
2429 INSERT("Poly1305Hash", poly1305hash_pytype);
2430 INSERT("Keccak1600", kxvik_pytype);
2431 INSERT("Shake", shake_pytype);
2432 INSERT("Shake128", shake128_pytype);
2433 INSERT("Shake256", shake256_pytype);
2434 INSERT("GCPRP", gcprp_pytype);
2435 INSERT("GPRP", gprp_pytype);
2436 INSERT("gcprps", gcprps());
2439 /*----- That's all, folks -------------------------------------------------*/