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 static const char *const 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 static const char *const 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) VALERR("key size cannot be negative");
111 if (min > dfl || (max && dfl > max)) VALERR("bad key size bounds");
112 if (mod <= 0 || dfl%mod || min%mod || max%mod)
113 VALERR("bad key size modulus");
114 o = (keyszrange_pyobj *)ty->tp_alloc(ty, 0);
119 return ((PyObject *)o);
124 static PyObject *keyszset_pynew(PyTypeObject *ty,
125 PyObject *arg, PyObject *kw)
127 static const char *const kwlist[] = { "default", "set", 0 };
130 PyObject *x = 0, *l = 0;
131 keyszset_pyobj *o = 0;
133 if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|O:new", KWLIST, &dfl, &set))
135 if (!set) set = PyTuple_New(0);
137 if (!PySequence_Check(set)) TYERR("want a sequence");
138 n = PySequence_Size(set);
140 if (PyErr_Occurred()) goto end;
141 if (dfl < 0) VALERR("key size cannot be negative");
142 x = PyInt_FromLong(dfl);
146 for (i = 0; i < n; i++) {
147 if ((x = PySequence_GetItem(set, i)) == 0) goto end;
148 xx = PyInt_AsLong(x);
149 if (PyErr_Occurred()) goto end;
150 if (xx == dfl) continue;
151 if (xx < 0) VALERR("key size cannot be negative");
157 if ((set = PySequence_Tuple(l)) == 0) goto end;
158 o = (keyszset_pyobj *)ty->tp_alloc(ty, 0);
166 return ((PyObject *)o);
169 static PyObject *kaget_min(PyObject *me, void *hunoz)
170 { return (PyInt_FromLong(0)); }
171 #define kaget_max kaget_min
173 static PyObject *ksget_min(PyObject *me, void *hunoz)
175 PyObject *set = ((keyszset_pyobj *)me)->set;
177 n = PyTuple_Size(set);
178 for (i = 0; i < n; i++) {
179 y = PyInt_AsLong(PyTuple_GetItem(set, i));
180 if (x == -1 || y < x) x = y;
182 return (PyInt_FromLong(x));
185 static PyObject *ksget_max(PyObject *me, void *hunoz)
187 PyObject *set = ((keyszset_pyobj *)me)->set;
189 n = PyTuple_Size(set);
190 for (i = 0; i < n; i++) {
191 y = PyInt_AsLong(PyTuple_GetItem(set, i));
194 return (PyInt_FromLong(x));
197 static PyMemberDef keysz_pymembers[] = {
198 #define MEMBERSTRUCT keysz_pyobj
199 #define default dfl /* ugh! */
200 MEMBER(default, T_INT, READONLY, "KSZ.default -> default key size")
206 static PyGetSetDef keyszany_pygetset[] = {
207 #define GETSETNAME(op, name) ka##op##_##name
208 GET (min, "KSZ.min -> smallest allowed key size")
209 GET (max, "KSZ.min -> largest allowed key size")
214 static PyMemberDef keyszrange_pymembers[] = {
215 #define MEMBERSTRUCT keyszrange_pyobj
216 MEMBER(min, T_INT, READONLY, "KSZ.min -> smallest allowed key size")
217 MEMBER(max, T_INT, READONLY, "KSZ.min -> largest allowed key size")
218 MEMBER(mod, T_INT, READONLY,
219 "KSZ.mod -> key size must be a multiple of this")
224 static PyGetSetDef keyszset_pygetset[] = {
225 #define GETSETNAME(op, name) ks##op##_##name
226 GET (min, "KSZ.min -> smallest allowed key size")
227 GET (max, "KSZ.min -> largest allowed key size")
232 static PyMemberDef keyszset_pymembers[] = {
233 #define MEMBERSTRUCT keyszset_pyobj
234 MEMBER(set, T_OBJECT, READONLY, "KSZ.set -> allowed key sizes")
239 static PyTypeObject keysz_pytype_skel = {
240 PyObject_HEAD_INIT(0) 0, /* Header */
241 "KeySZ", /* @tp_name@ */
242 sizeof(keysz_pyobj), /* @tp_basicsize@ */
243 0, /* @tp_itemsize@ */
245 0, /* @tp_dealloc@ */
247 0, /* @tp_getattr@ */
248 0, /* @tp_setattr@ */
249 0, /* @tp_compare@ */
251 0, /* @tp_as_number@ */
252 0, /* @tp_as_sequence@ */
253 0, /* @tp_as_mapping@ */
257 0, /* @tp_getattro@ */
258 0, /* @tp_setattro@ */
259 0, /* @tp_as_buffer@ */
260 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
264 "Key size constraints. Abstract.",
266 0, /* @tp_traverse@ */
268 0, /* @tp_richcompare@ */
269 0, /* @tp_weaklistoffset@ */
271 0, /* @tp_iternext@ */
272 0, /* @tp_methods@ */
273 keysz_pymembers, /* @tp_members@ */
277 0, /* @tp_descr_get@ */
278 0, /* @tp_descr_set@ */
279 0, /* @tp_dictoffset@ */
281 PyType_GenericAlloc, /* @tp_alloc@ */
282 abstract_pynew, /* @tp_new@ */
287 static PyTypeObject keyszany_pytype_skel = {
288 PyObject_HEAD_INIT(0) 0, /* Header */
289 "KeySZAny", /* @tp_name@ */
290 sizeof(keysz_pyobj), /* @tp_basicsize@ */
291 0, /* @tp_itemsize@ */
293 0, /* @tp_dealloc@ */
295 0, /* @tp_getattr@ */
296 0, /* @tp_setattr@ */
297 0, /* @tp_compare@ */
299 0, /* @tp_as_number@ */
300 0, /* @tp_as_sequence@ */
301 0, /* @tp_as_mapping@ */
305 0, /* @tp_getattro@ */
306 0, /* @tp_setattro@ */
307 0, /* @tp_as_buffer@ */
308 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
312 "KeySZAny(DEFAULT)\n\
313 Key size constraints. This object imposes no constraints on size.",
315 0, /* @tp_traverse@ */
317 0, /* @tp_richcompare@ */
318 0, /* @tp_weaklistoffset@ */
320 0, /* @tp_iternext@ */
321 0, /* @tp_methods@ */
322 0, /* @tp_members@ */
323 keyszany_pygetset, /* @tp_getset@ */
326 0, /* @tp_descr_get@ */
327 0, /* @tp_descr_set@ */
328 0, /* @tp_dictoffset@ */
330 PyType_GenericAlloc, /* @tp_alloc@ */
331 keyszany_pynew, /* @tp_new@ */
336 static PyTypeObject keyszrange_pytype_skel = {
337 PyObject_HEAD_INIT(0) 0, /* Header */
338 "KeySZRange", /* @tp_name@ */
339 sizeof(keyszrange_pyobj), /* @tp_basicsize@ */
340 0, /* @tp_itemsize@ */
342 0, /* @tp_dealloc@ */
344 0, /* @tp_getattr@ */
345 0, /* @tp_setattr@ */
346 0, /* @tp_compare@ */
348 0, /* @tp_as_number@ */
349 0, /* @tp_as_sequence@ */
350 0, /* @tp_as_mapping@ */
354 0, /* @tp_getattro@ */
355 0, /* @tp_setattro@ */
356 0, /* @tp_as_buffer@ */
357 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
361 "KeySZRange(DEFAULT, [min = 0], [max = 0], [mod = 1])\n\
362 Key size constraints. Key size must be between MIN and MAX inclusive,\n\
363 and be a multiple of MOD.",
365 0, /* @tp_traverse@ */
367 0, /* @tp_richcompare@ */
368 0, /* @tp_weaklistoffset@ */
370 0, /* @tp_iternext@ */
371 0, /* @tp_methods@ */
372 keyszrange_pymembers, /* @tp_members@ */
376 0, /* @tp_descr_get@ */
377 0, /* @tp_descr_set@ */
378 0, /* @tp_dictoffset@ */
380 PyType_GenericAlloc, /* @tp_alloc@ */
381 keyszrange_pynew, /* @tp_new@ */
386 static PyTypeObject keyszset_pytype_skel = {
387 PyObject_HEAD_INIT(0) 0, /* Header */
388 "KeySZSet", /* @tp_name@ */
389 sizeof(keyszset_pyobj), /* @tp_basicsize@ */
390 0, /* @tp_itemsize@ */
392 0, /* @tp_dealloc@ */
394 0, /* @tp_getattr@ */
395 0, /* @tp_setattr@ */
396 0, /* @tp_compare@ */
398 0, /* @tp_as_number@ */
399 0, /* @tp_as_sequence@ */
400 0, /* @tp_as_mapping@ */
404 0, /* @tp_getattro@ */
405 0, /* @tp_setattro@ */
406 0, /* @tp_as_buffer@ */
407 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
411 "KeySZSet(DEFAULT, SEQ)\n\
412 Key size constraints. Key size must be DEFAULT or one in SEQ.",
414 0, /* @tp_traverse@ */
416 0, /* @tp_richcompare@ */
417 0, /* @tp_weaklistoffset@ */
419 0, /* @tp_iternext@ */
420 0, /* @tp_methods@ */
421 keyszset_pymembers, /* @tp_members@ */
422 keyszset_pygetset, /* @tp_getset@ */
425 0, /* @tp_descr_get@ */
426 0, /* @tp_descr_set@ */
427 0, /* @tp_dictoffset@ */
429 PyType_GenericAlloc, /* @tp_alloc@ */
430 keyszset_pynew, /* @tp_new@ */
435 #define KSZCONVOP(op) \
436 static PyObject *meth__KeySZ_##op(PyObject *me, PyObject *arg) \
439 if (!PyArg_ParseTuple(arg, "Od:" #op, &me, &x)) return (0); \
441 return (PyFloat_FromDouble(y)); \
444 KSZCONVOP(fromschnorr)
453 /*----- Symmetric encryption ----------------------------------------------*/
455 PyTypeObject *gccipher_pytype, *gcipher_pytype;
457 CONVFUNC(gccipher, gccipher *, GCCIPHER_CC)
458 CONVFUNC(gcipher, gcipher *, GCIPHER_C)
460 PyObject *gcipher_pywrap(PyObject *cobj, gcipher *c)
463 if (!cobj) cobj = gccipher_pywrap((/*unconst*/ gccipher *)GC_CLASS(c));
464 else Py_INCREF(cobj);
465 g = PyObject_NEW(gcipher_pyobj, (PyTypeObject *)cobj);
467 return ((PyObject *)g);
470 static PyObject *gcipher_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
472 static const char *const kwlist[] = { "k", 0 };
476 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &k, &sz))
478 if (keysz(sz, GCCIPHER_CC(ty)->keysz) != sz) VALERR("bad key length");
479 return (gcipher_pywrap((PyObject *)ty,
480 GC_INIT(GCCIPHER_CC(ty), k, sz)));
485 PyObject *gccipher_pywrap(gccipher *cc)
487 gccipher_pyobj *g = newtype(gccipher_pytype, 0, cc->name);
489 g->ty.ht_type.tp_basicsize = sizeof(gcipher_pyobj);
490 g->ty.ht_type.tp_base = gcipher_pytype;
491 Py_INCREF(gcipher_pytype);
492 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
493 Py_TPFLAGS_BASETYPE |
494 Py_TPFLAGS_HEAPTYPE);
495 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
496 g->ty.ht_type.tp_free = 0;
497 g->ty.ht_type.tp_new = gcipher_pynew;
498 typeready(&g->ty.ht_type);
499 return ((PyObject *)g);
502 static void gcipher_pydealloc(PyObject *me)
504 GC_DESTROY(GCIPHER_C(me));
505 Py_DECREF(me->ob_type);
509 static PyObject *gccget_name(PyObject *me, void *hunoz)
510 { return (PyString_FromString(GCCIPHER_CC(me)->name)); }
512 static PyObject *gccget_keysz(PyObject *me, void *hunoz)
513 { return (keysz_pywrap(GCCIPHER_CC(me)->keysz)); }
515 static PyObject *gccget_blksz(PyObject *me, void *hunoz)
516 { return (PyInt_FromLong(GCCIPHER_CC(me)->blksz)); }
518 static PyObject *gcmeth_encrypt(PyObject *me, PyObject *arg)
524 if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &sz)) return (0);
525 rc = bytestring_pywrap(0, sz);
526 GC_ENCRYPT(GCIPHER_C(me), p, PyString_AS_STRING(rc), sz);
530 static PyObject *gcmeth_enczero(PyObject *me, PyObject *arg)
536 if (!PyArg_ParseTuple(arg, "i:enczero", &sz)) return (0);
537 rc = bytestring_pywrap(0, sz);
538 p = PyString_AS_STRING(rc);
540 GC_ENCRYPT(GCIPHER_C(me), p, p, sz);
544 static PyObject *gcmeth_decrypt(PyObject *me, PyObject *arg)
550 if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &sz)) return (0);
551 rc = bytestring_pywrap(0, sz);
552 GC_DECRYPT(GCIPHER_C(me), p, PyString_AS_STRING(rc), sz);
556 static PyObject *gcmeth_deczero(PyObject *me, PyObject *arg)
562 if (!PyArg_ParseTuple(arg, "i:deczero", &sz)) return (0);
563 rc = bytestring_pywrap(0, sz);
564 p = PyString_AS_STRING(rc);
566 GC_DECRYPT(GCIPHER_C(me), p, p, sz);
570 static PyObject *gcmeth_setiv(PyObject *me, PyObject *arg)
575 if (!PyArg_ParseTuple(arg, "s#:setiv", &p, &sz)) goto end;
576 if (!GCIPHER_C(me)->ops->setiv) VALERR("`setiv' not supported");
577 if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode");
578 if (sz != GC_CLASS(GCIPHER_C(me))->blksz) VALERR("bad IV length");
579 GC_SETIV(GCIPHER_C(me), p);
585 static PyObject *gcmeth_bdry(PyObject *me, PyObject *arg)
587 if (!PyArg_ParseTuple(arg, ":bdry")) goto end;
588 if (!GCIPHER_C(me)->ops->bdry) VALERR("`bdry' not supported");
589 if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode");
590 GC_BDRY(GCIPHER_C(me));
596 static PyGetSetDef gccipher_pygetset[] = {
597 #define GETSETNAME(op, name) gcc##op##_##name
598 GET (keysz, "CC.keysz -> acceptable key sizes")
599 GET (blksz, "CC.blksz -> block size, or zero")
600 GET (name, "CC.name -> name of this kind of cipher")
605 static PyMethodDef gcipher_pymethods[] = {
606 #define METHNAME(name) gcmeth_##name
607 METH (encrypt, "C.encrypt(PT) -> CT")
608 METH (enczero, "C.enczero(N) -> CT")
609 METH (decrypt, "C.decrypt(CT) -> PT")
610 METH (deczero, "C.deczero(N) -> PT")
611 METH (setiv, "C.setiv(IV)")
612 METH (bdry, "C.bdry()")
617 static PyTypeObject gccipher_pytype_skel = {
618 PyObject_HEAD_INIT(0) 0, /* Header */
619 "GCCipher", /* @tp_name@ */
620 sizeof(gccipher_pyobj), /* @tp_basicsize@ */
621 0, /* @tp_itemsize@ */
623 0, /* @tp_dealloc@ */
625 0, /* @tp_getattr@ */
626 0, /* @tp_setattr@ */
627 0, /* @tp_compare@ */
629 0, /* @tp_as_number@ */
630 0, /* @tp_as_sequence@ */
631 0, /* @tp_as_mapping@ */
635 0, /* @tp_getattro@ */
636 0, /* @tp_setattro@ */
637 0, /* @tp_as_buffer@ */
638 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
642 "Symmetric cipher metaclass.",
644 0, /* @tp_traverse@ */
646 0, /* @tp_richcompare@ */
647 0, /* @tp_weaklistoffset@ */
649 0, /* @tp_iternext@ */
650 0, /* @tp_methods@ */
651 0, /* @tp_members@ */
652 gccipher_pygetset, /* @tp_getset@ */
655 0, /* @tp_descr_get@ */
656 0, /* @tp_descr_set@ */
657 0, /* @tp_dictoffset@ */
659 PyType_GenericAlloc, /* @tp_alloc@ */
660 abstract_pynew, /* @tp_new@ */
665 static PyTypeObject gcipher_pytype_skel = {
666 PyObject_HEAD_INIT(0) 0, /* Header */
667 "GCipher", /* @tp_name@ */
668 sizeof(gcipher_pyobj), /* @tp_basicsize@ */
669 0, /* @tp_itemsize@ */
671 gcipher_pydealloc, /* @tp_dealloc@ */
673 0, /* @tp_getattr@ */
674 0, /* @tp_setattr@ */
675 0, /* @tp_compare@ */
677 0, /* @tp_as_number@ */
678 0, /* @tp_as_sequence@ */
679 0, /* @tp_as_mapping@ */
683 0, /* @tp_getattro@ */
684 0, /* @tp_setattro@ */
685 0, /* @tp_as_buffer@ */
686 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
690 "Symmetric cipher, abstract base class.",
692 0, /* @tp_traverse@ */
694 0, /* @tp_richcompare@ */
695 0, /* @tp_weaklistoffset@ */
697 0, /* @tp_iternext@ */
698 gcipher_pymethods, /* @tp_methods@ */
699 0, /* @tp_members@ */
703 0, /* @tp_descr_get@ */
704 0, /* @tp_descr_set@ */
705 0, /* @tp_dictoffset@ */
707 PyType_GenericAlloc, /* @tp_alloc@ */
708 abstract_pynew, /* @tp_new@ */
713 /*----- Hash functions ----------------------------------------------------*/
715 PyTypeObject *gchash_pytype, *ghash_pytype;
717 CONVFUNC(gchash, gchash *, GCHASH_CH)
718 CONVFUNC(ghash, ghash *, GHASH_H)
720 static PyObject *ghash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
722 static const char *const kwlist[] = { 0 };
723 if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", KWLIST))
725 return (ghash_pywrap((PyObject *)ty, GH_INIT(GCHASH_CH(ty))));
730 PyObject *gchash_pywrap(gchash *ch)
732 gchash_pyobj *g = newtype(gchash_pytype, 0, ch->name);
734 g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj);
735 g->ty.ht_type.tp_base = ghash_pytype;
736 Py_INCREF(ghash_pytype);
737 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
738 Py_TPFLAGS_BASETYPE |
739 Py_TPFLAGS_HEAPTYPE);
740 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
741 g->ty.ht_type.tp_free = 0;
742 g->ty.ht_type.tp_new = ghash_pynew;
743 typeready(&g->ty.ht_type);
744 return ((PyObject *)g);
747 PyObject *ghash_pywrap(PyObject *cobj, ghash *h)
750 if (!cobj) cobj = gchash_pywrap((/*unconst*/ gchash *)GH_CLASS(h));
751 else Py_INCREF(cobj);
752 g = PyObject_NEW(ghash_pyobj, (PyTypeObject *)cobj);
754 return ((PyObject *)g);
757 static void ghash_pydealloc(PyObject *me)
759 GH_DESTROY(GHASH_H(me));
760 Py_DECREF(me->ob_type);
764 static PyObject *gchget_name(PyObject *me, void *hunoz)
765 { return (PyString_FromString(GCHASH_CH(me)->name)); }
767 static PyObject *gchget_hashsz(PyObject *me, void *hunoz)
768 { return (PyInt_FromLong(GCHASH_CH(me)->hashsz)); }
770 static PyObject *gchget_bufsz(PyObject *me, void *hunoz)
771 { return (PyInt_FromLong(GCHASH_CH(me)->bufsz)); }
773 static PyObject *ghmeth_copy(PyObject *me, PyObject *arg)
775 if (!PyArg_ParseTuple(arg, ":copy")) return (0);
776 return (ghash_pywrap((PyObject *)me->ob_type, GH_COPY(GHASH_H(me))));
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)) return (0); \
793 GH_HASHU##W(GHASH_H(me), x); \
796 DOUINTCONV(GHMETH_HASHU_)
798 #define GHMETH_HASHBUF_(n, W, w) \
799 static PyObject *ghmeth_hashbuf##w(PyObject *me, PyObject *arg) \
803 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
804 if (sz > MASK##n) TYERR("string too long"); \
805 GH_HASHBUF##W(GHASH_H(me), p, sz); \
810 DOUINTCONV(GHMETH_HASHBUF_)
812 static PyObject *ghmeth_hashstrz(PyObject *me, PyObject *arg)
815 if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0);
816 GH_HASHSTRZ(GHASH_H(me), p);
820 static PyObject *ghmeth_done(PyObject *me, PyObject *arg)
824 if (!PyArg_ParseTuple(arg, ":done")) return (0);
825 g = GH_COPY(GHASH_H(me));
826 rc = bytestring_pywrap(0, g->ops->c->hashsz);
827 GH_DONE(g, PyString_AS_STRING(rc));
832 static PyGetSetDef gchash_pygetset[] = {
833 #define GETSETNAME(op, name) gch##op##_##name
834 GET (bufsz, "CH.bufsz -> hash buffer size, or zero")
835 GET (hashsz, "CH.hashsz -> hash output size")
836 GET (name, "CH.name -> name of this kind of hash")
841 static PyMethodDef ghash_pymethods[] = {
842 #define METHNAME(name) ghmeth_##name
843 METH (copy, "H.copy() -> HH")
844 METH (hash, "H.hash(M)")
845 #define METHU_(n, W, w) METH(hashu##w, "H.hashu" #w "(WORD)")
848 #define METHBUF_(n, W, w) METH(hashbuf##w, "H.hashbuf" #w "(BYTES)")
851 METH (hashstrz, "H.hashstrz(STRING)")
852 METH (done, "H.done() -> HASH")
857 static PyTypeObject gchash_pytype_skel = {
858 PyObject_HEAD_INIT(0) 0, /* Header */
859 "GCHash", /* @tp_name@ */
860 sizeof(gchash_pyobj), /* @tp_basicsize@ */
861 0, /* @tp_itemsize@ */
863 0, /* @tp_dealloc@ */
865 0, /* @tp_getattr@ */
866 0, /* @tp_setattr@ */
867 0, /* @tp_compare@ */
869 0, /* @tp_as_number@ */
870 0, /* @tp_as_sequence@ */
871 0, /* @tp_as_mapping@ */
875 0, /* @tp_getattro@ */
876 0, /* @tp_setattro@ */
877 0, /* @tp_as_buffer@ */
878 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
882 "Hash function metaclass.",
884 0, /* @tp_traverse@ */
886 0, /* @tp_richcompare@ */
887 0, /* @tp_weaklistoffset@ */
889 0, /* @tp_iternext@ */
890 0, /* @tp_methods@ */
891 0, /* @tp_members@ */
892 gchash_pygetset, /* @tp_getset@ */
895 0, /* @tp_descr_get@ */
896 0, /* @tp_descr_set@ */
897 0, /* @tp_dictoffset@ */
899 PyType_GenericAlloc, /* @tp_alloc@ */
900 abstract_pynew, /* @tp_new@ */
905 static PyTypeObject ghash_pytype_skel = {
906 PyObject_HEAD_INIT(0) 0, /* Header */
907 "GHash", /* @tp_name@ */
908 sizeof(ghash_pyobj), /* @tp_basicsize@ */
909 0, /* @tp_itemsize@ */
911 ghash_pydealloc, /* @tp_dealloc@ */
913 0, /* @tp_getattr@ */
914 0, /* @tp_setattr@ */
915 0, /* @tp_compare@ */
917 0, /* @tp_as_number@ */
918 0, /* @tp_as_sequence@ */
919 0, /* @tp_as_mapping@ */
923 0, /* @tp_getattro@ */
924 0, /* @tp_setattro@ */
925 0, /* @tp_as_buffer@ */
926 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
930 "Hash function, abstract base class.",
932 0, /* @tp_traverse@ */
934 0, /* @tp_richcompare@ */
935 0, /* @tp_weaklistoffset@ */
937 0, /* @tp_iternext@ */
938 ghash_pymethods, /* @tp_methods@ */
939 0, /* @tp_members@ */
943 0, /* @tp_descr_get@ */
944 0, /* @tp_descr_set@ */
945 0, /* @tp_dictoffset@ */
947 PyType_GenericAlloc, /* @tp_alloc@ */
948 abstract_pynew, /* @tp_new@ */
953 /*----- Message authentication --------------------------------------------*/
955 PyTypeObject *gcmac_pytype, *gmac_pytype, *gmhash_pytype;
957 CONVFUNC(gcmac, gcmac *, GCMAC_CM)
958 CONVFUNC(gmac, gmac *, GMAC_M)
959 CONVFUNC(gmhash, ghash *, GHASH_H)
961 static PyObject *gmac_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
963 static const char *const kwlist[] = { "k", 0 };
967 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &k, &sz))
969 if (keysz(sz, GCMAC_CM(ty)->keysz) != sz) VALERR("bad key length");
970 return (gmac_pywrap((PyObject *)ty,
971 GM_KEY(GCMAC_CM(ty), k, sz)));
976 static PyObject *gmhash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
978 static const char *const kwlist[] = { 0 };
981 if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", KWLIST)) return (0);
982 g = PyObject_NEW(ghash_pyobj, ty);
983 g->h = GM_INIT(GMAC_M(ty));
985 return ((PyObject *)g);
988 PyObject *gcmac_pywrap(gcmac *cm)
990 gcmac_pyobj *g = newtype(gcmac_pytype, 0, cm->name);
992 g->ty.ht_type.tp_basicsize = sizeof(gmac_pyobj);
993 g->ty.ht_type.tp_base = gmac_pytype;
994 Py_INCREF(gmac_pytype);
995 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
996 Py_TPFLAGS_BASETYPE |
997 Py_TPFLAGS_HEAPTYPE);
998 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
999 g->ty.ht_type.tp_free = 0;
1000 g->ty.ht_type.tp_new = gmac_pynew;
1001 typeready(&g->ty.ht_type);
1002 return ((PyObject *)g);
1005 PyObject *gmac_pywrap(PyObject *cobj, gmac *m)
1008 if (!cobj) cobj = gcmac_pywrap((/*unconst*/ gcmac *)GM_CLASS(m));
1009 else Py_INCREF(cobj);
1010 g = newtype((PyTypeObject *)cobj, 0, 0);
1011 g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj);
1012 g->ty.ht_name = PyString_FromFormat("%s(keyed)", m->ops->c->name);
1013 g->ty.ht_type.tp_name = PyString_AS_STRING(g->ty.ht_name);
1014 g->ty.ht_type.tp_base = gmhash_pytype;
1015 Py_INCREF(gmac_pytype);
1016 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
1017 Py_TPFLAGS_BASETYPE |
1018 Py_TPFLAGS_HEAPTYPE);
1019 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
1020 g->ty.ht_type.tp_free = 0;
1021 g->ty.ht_type.tp_new = gmhash_pynew;
1022 typeready(&g->ty.ht_type);
1024 return ((PyObject *)g);
1027 static void gmac_pydealloc(PyObject *me)
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 /*----- Special snowflake for Poly1305 ------------------------------------*/
1198 PyTypeObject *poly1305cls_pytype, *poly1305key_pytype, *poly1305hash_pytype;
1200 typedef struct poly1305key_pyobj {
1201 PyHeapTypeObject ty;
1203 } poly1305key_pyobj;
1205 typedef struct poly1305hash_pyobj {
1210 } poly1305hash_pyobj;
1212 #define P1305_F(o) (((poly1305hash_pyobj *)(o))->f)
1213 #define P1305_CTX(o) (&((poly1305hash_pyobj *)(o))->ctx)
1214 CONVFUNC(poly1305hash, poly1305_ctx *, P1305_CTX)
1216 static PyObject *poly1305hash_pynew(PyTypeObject *ty,
1217 PyObject *arg, PyObject *kw)
1219 static const char *const kwlist[] = { "mask", 0 };
1220 poly1305key_pyobj *pk = (poly1305key_pyobj *)ty;
1221 poly1305hash_pyobj *ph;
1225 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|s#:new", KWLIST, &m, &sz))
1227 if (m && sz != POLY1305_MASKSZ) VALERR("bad mask length");
1228 ph = PyObject_NEW(poly1305hash_pyobj, ty);
1230 if (m) ph->f |= f_mask;
1231 poly1305_macinit(&ph->ctx, &pk->k, m);
1233 return ((PyObject *)ph);
1238 static PyObject *poly1305key_pynew(PyTypeObject *ty,
1239 PyObject *arg, PyObject *kw)
1241 static const char *const kwlist[] = { "k", 0 };
1242 poly1305key_pyobj *pk;
1246 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &k, &sz))
1248 if (keysz(sz, poly1305_keysz) != sz) VALERR("bad key length");
1250 pk = newtype(ty, 0, 0);
1251 pk->ty.ht_name = PyString_FromString("poly1305(keyed)");
1252 pk->ty.ht_type.tp_basicsize = sizeof(poly1305hash_pyobj);
1253 pk->ty.ht_type.tp_name = PyString_AS_STRING(pk->ty.ht_name);
1254 pk->ty.ht_type.tp_base = poly1305hash_pytype;
1255 Py_INCREF(poly1305key_pytype);
1256 pk->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
1257 Py_TPFLAGS_BASETYPE |
1258 Py_TPFLAGS_HEAPTYPE);
1259 pk->ty.ht_type.tp_alloc = PyType_GenericAlloc;
1260 pk->ty.ht_type.tp_free = 0;
1261 pk->ty.ht_type.tp_new = poly1305hash_pynew;
1262 typeready(&pk->ty.ht_type);
1264 poly1305_keyinit(&pk->k, k, sz);
1265 return ((PyObject *)pk);
1271 static PyObject *poly1305clsget_name(PyObject *me, void *hunoz)
1272 { return (PyString_FromString("poly1305")); }
1274 static PyObject *poly1305clsget_keysz(PyObject *me, void *hunoz)
1275 { return (keysz_pywrap(poly1305_keysz)); }
1277 static PyObject *poly1305clsget_masksz(PyObject *me, void *hunoz)
1278 { return (PyInt_FromLong(POLY1305_MASKSZ)); }
1280 static PyObject *poly1305clsget_tagsz(PyObject *me, void *hunoz)
1281 { return (PyInt_FromLong(POLY1305_TAGSZ)); }
1283 static PyObject *polymeth_copy(PyObject *me, PyObject *arg)
1285 poly1305hash_pyobj *ph;
1286 if (!PyArg_ParseTuple(arg, ":copy")) return (0);
1287 ph = PyObject_NEW(poly1305hash_pyobj, me->ob_type);
1288 poly1305_copy(&ph->ctx, P1305_CTX(me));
1289 Py_INCREF(me->ob_type);
1290 return ((PyObject *)ph);
1293 static PyObject *polymeth_hash(PyObject *me, PyObject *arg)
1297 if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0);
1298 poly1305_hash(P1305_CTX(me), p, sz);
1302 #define POLYMETH_HASHU_(n, W, w) \
1303 static PyObject *polymeth_hashu##w(PyObject *me, PyObject *arg) \
1307 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \
1308 STORE##W(b, x); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \
1311 DOUINTCONV(POLYMETH_HASHU_)
1313 #define POLYMETH_HASHBUF_(n, W, w) \
1314 static PyObject *polymeth_hashbuf##w(PyObject *me, PyObject *arg) \
1319 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
1320 if (sz > MASK##n) TYERR("string too long"); \
1321 STORE##W(b, sz); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \
1322 poly1305_hash(P1305_CTX(me), p, sz); \
1327 DOUINTCONV(POLYMETH_HASHBUF_)
1329 static PyObject *polymeth_hashstrz(PyObject *me, PyObject *arg)
1332 if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0);
1333 poly1305_hash(P1305_CTX(me), p, strlen(p) + 1);
1337 static PyObject *polymeth_flush(PyObject *me, PyObject *arg)
1339 if (!PyArg_ParseTuple(arg, ":flush")) return (0);
1340 poly1305_flush(P1305_CTX(me));
1344 static PyObject *polymeth_flushzero(PyObject *me, PyObject *arg)
1346 if (!PyArg_ParseTuple(arg, ":flushzero")) return (0);
1347 poly1305_flushzero(P1305_CTX(me));
1351 static PyObject *polymeth_concat(PyObject *me, PyObject *arg)
1353 PyObject *pre, *suff;
1354 if (!PyArg_ParseTuple(arg, "OO:concat", &pre, &suff)) return (0);
1355 if (!PyObject_TypeCheck(pre, poly1305hash_pytype) ||
1356 !PyObject_TypeCheck(suff, poly1305hash_pytype))
1357 TYERR("wanted a poly1305hash");
1358 if (me->ob_type != pre->ob_type || me->ob_type != suff->ob_type)
1359 TYERR("key mismatch");
1360 if (P1305_CTX(pre)->nbuf) VALERR("prefix is not block-aligned");
1361 poly1305_concat(P1305_CTX(me), P1305_CTX(pre), P1305_CTX(suff));
1367 static PyObject *polymeth_done(PyObject *me, PyObject *arg)
1370 if (!PyArg_ParseTuple(arg, ":done")) return (0);
1371 if (!(P1305_F(me) & f_mask)) VALERR("no mask");
1372 rc = bytestring_pywrap(0, POLY1305_TAGSZ);
1373 poly1305_done(P1305_CTX(me), PyString_AS_STRING(rc));
1379 static PyGetSetDef poly1305cls_pygetset[] = {
1380 #define GETSETNAME(op, name) poly1305cls##op##_##name
1381 GET (keysz, "PC.keysz -> acceptable key sizes")
1382 GET (masksz, "PC.masksz -> mask size")
1383 GET (tagsz, "PC.tagsz -> MAC output size")
1384 GET (name, "PC.name -> name of this kind of MAC")
1389 static PyMethodDef poly1305hash_pymethods[] = {
1390 #define METHNAME(name) polymeth_##name
1391 METH (copy, "P.copy() -> PP")
1392 METH (hash, "P.hash(M)")
1393 #define METHU_(n, W, w) METH(hashu##w, "P.hashu" #w "(WORD)")
1396 #define METHBUF_(n, W, w) METH(hashbuf##w, "P.hashbuf" #w "(BYTES)")
1397 DOUINTCONV(METHBUF_)
1399 METH (hashstrz, "P.hashstrz(STRING)")
1400 METH (flush, "P.flush()")
1401 METH (flushzero, "P.flushzero()")
1402 METH (concat, "P.concat(PREFIX, SUFFIX)")
1403 METH (done, "P.done() -> TAG")
1408 static PyTypeObject poly1305cls_pytype_skel = {
1409 PyObject_HEAD_INIT(0) 0, /* Header */
1410 "Poly1305Class", /* @tp_name@ */
1411 sizeof(PyHeapTypeObject), /* @tp_basicsize@ */
1412 0, /* @tp_itemsize@ */
1414 0, /* @tp_dealloc@ */
1416 0, /* @tp_getattr@ */
1417 0, /* @tp_setattr@ */
1418 0, /* @tp_compare@ */
1420 0, /* @tp_as_number@ */
1421 0, /* @tp_as_sequence@ */
1422 0, /* @tp_as_mapping@ */
1426 0, /* @tp_getattro@ */
1427 0, /* @tp_setattro@ */
1428 0, /* @tp_as_buffer@ */
1429 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1430 Py_TPFLAGS_BASETYPE,
1433 "Poly1305 metametaclass. Best not to ask.",
1435 0, /* @tp_traverse@ */
1437 0, /* @tp_richcompare@ */
1438 0, /* @tp_weaklistoffset@ */
1440 0, /* @tp_iternext@ */
1441 0, /* @tp_methods@ */
1442 0, /* @tp_members@ */
1443 poly1305cls_pygetset, /* @tp_getset@ */
1446 0, /* @tp_descr_get@ */
1447 0, /* @tp_descr_set@ */
1448 0, /* @tp_dictoffset@ */
1450 PyType_GenericAlloc, /* @tp_alloc@ */
1451 abstract_pynew, /* @tp_new@ */
1456 static PyTypeObject poly1305key_pytype_skel = {
1457 PyObject_HEAD_INIT(0) 0, /* Header */
1458 "poly1305", /* @tp_name@ */
1459 sizeof(poly1305key_pyobj), /* @tp_basicsize@ */
1460 0, /* @tp_itemsize@ */
1462 0, /* @tp_dealloc@ */
1464 0, /* @tp_getattr@ */
1465 0, /* @tp_setattr@ */
1466 0, /* @tp_compare@ */
1468 0, /* @tp_as_number@ */
1469 0, /* @tp_as_sequence@ */
1470 0, /* @tp_as_mapping@ */
1474 0, /* @tp_getattro@ */
1475 0, /* @tp_setattro@ */
1476 0, /* @tp_as_buffer@ */
1477 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1478 Py_TPFLAGS_BASETYPE,
1481 "poly1305(K): Poly1305 key.",
1483 0, /* @tp_traverse@ */
1485 0, /* @tp_richcompare@ */
1486 0, /* @tp_weaklistoffset@ */
1488 0, /* @tp_iternext@ */
1489 0, /* @tp_methods@ */
1490 0, /* @tp_members@ */
1491 0, /* @tp_getset@ */
1494 0, /* @tp_descr_get@ */
1495 0, /* @tp_descr_set@ */
1496 0, /* @tp_dictoffset@ */
1498 PyType_GenericAlloc, /* @tp_alloc@ */
1499 poly1305key_pynew, /* @tp_new@ */
1504 static PyTypeObject poly1305hash_pytype_skel = {
1505 PyObject_HEAD_INIT(0) 0, /* Header */
1506 "Poly1305Hash", /* @tp_name@ */
1507 sizeof(poly1305hash_pyobj), /* @tp_basicsize@ */
1508 0, /* @tp_itemsize@ */
1510 0, /* @tp_dealloc@ */
1512 0, /* @tp_getattr@ */
1513 0, /* @tp_setattr@ */
1514 0, /* @tp_compare@ */
1516 0, /* @tp_as_number@ */
1517 0, /* @tp_as_sequence@ */
1518 0, /* @tp_as_mapping@ */
1522 0, /* @tp_getattro@ */
1523 0, /* @tp_setattro@ */
1524 0, /* @tp_as_buffer@ */
1525 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1526 Py_TPFLAGS_BASETYPE,
1529 "Poly1305 MAC context base class.",
1531 0, /* @tp_traverse@ */
1533 0, /* @tp_richcompare@ */
1534 0, /* @tp_weaklistoffset@ */
1536 0, /* @tp_iternext@ */
1537 poly1305hash_pymethods, /* @tp_methods@ */
1538 0, /* @tp_members@ */
1539 0, /* @tp_getset@ */
1542 0, /* @tp_descr_get@ */
1543 0, /* @tp_descr_set@ */
1544 0, /* @tp_dictoffset@ */
1546 PyType_GenericAlloc, /* @tp_alloc@ */
1547 abstract_pynew, /* @tp_new@ */
1552 /*----- Special snowflake for HSalsa and HChaCha --------------------------*/
1554 #define DEF_HDANCE(DANCE, HDANCE, dance, hdance) \
1555 static PyObject *meth_##hdance##_prf(PyObject *me, PyObject *arg) \
1557 dance##_ctx dance; \
1559 Py_ssize_t ksz, nsz; \
1561 if (!PyArg_ParseTuple(arg, "s#s#:" #hdance "_prf", \
1562 &k, &ksz, &n, &nsz)) \
1564 if (ksz != DANCE##_KEYSZ) VALERR("bad key length"); \
1565 if (nsz != HDANCE##_INSZ) VALERR("bad input length"); \
1566 rc = bytestring_pywrap(0, HSALSA20_OUTSZ); \
1567 dance##_init(&dance, k, ksz, 0); \
1568 hdance##_prf(&dance, n, PyString_AS_STRING(rc)); \
1574 DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa20)
1575 DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa2012)
1576 DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa208)
1578 DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha20)
1579 DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha12)
1580 DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha8)
1582 /*----- Keccak-p[1600, n] -------------------------------------------------*/
1584 static PyTypeObject *kxvik_pytype;
1586 typedef struct kxvik_pyobj {
1592 static PyObject *kxvik_pynew(PyTypeObject *ty,
1593 PyObject *arg, PyObject *kw)
1596 kxvik_pyobj *rc = 0;
1597 static const char *const kwlist[] = { "nround", 0 };
1598 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", KWLIST,
1601 rc = (kxvik_pyobj *)ty->tp_alloc(ty, 0);
1603 keccak1600_init(&rc->s);
1605 return ((PyObject *)rc);
1608 static PyObject *kxvikmeth_copy(PyObject *me, PyObject *arg)
1610 kxvik_pyobj *k = (kxvik_pyobj *)me, *rc = 0;
1611 if (!PyArg_ParseTuple(arg, ":copy")) goto end;
1612 rc = (kxvik_pyobj *)k->ob_type->tp_alloc(k->ob_type, 0);
1613 rc->s = k->s; rc->n = k->n;
1615 return ((PyObject *)rc);
1618 static PyObject *kxvikmeth_mix(PyObject *me, PyObject *arg)
1620 kxvik_pyobj *k = (kxvik_pyobj *)me;
1625 char *p; Py_ssize_t n;
1627 if (!PyArg_ParseTuple(arg, "s#:mix", &p, &n)) goto end;
1628 if (n > 200) VALERR("out of range");
1629 q = (const octet *)p;
1631 while (n > 8) { LOAD64_L_(t[i], q); i++; q += 8; n -= 8; }
1633 memcpy(buf, q, n); memset(buf + n, 0, 8 - n);
1634 LOAD64_L_(t[i], buf); i++;
1636 keccak1600_mix(&k->s, t, i);
1642 static PyObject *kxvikmeth_extract(PyObject *me, PyObject *arg)
1644 kxvik_pyobj *k = (kxvik_pyobj *)me;
1651 if (!PyArg_ParseTuple(arg, "O&:mix", convuint, &n)) goto end;
1652 if (n > 200) VALERR("out of range");
1653 rc = bytestring_pywrap(0, n);
1654 q = (octet *)PyString_AS_STRING(rc);
1655 keccak1600_extract(&k->s, t, (n + 7)/8);
1657 while (n > 8) { STORE64_L_(q, t[i]); i++; q += 8; n -= 8; }
1658 if (n) { STORE64_L_(buf, t[i]); memcpy(q, buf, n); }
1663 static PyObject *kxvikmeth_step(PyObject *me, PyObject *arg)
1665 kxvik_pyobj *k = (kxvik_pyobj *)me;
1666 if (!PyArg_ParseTuple(arg, ":step")) return (0);
1667 keccak1600_p(&k->s, &k->s, k->n);
1671 static PyObject *kxvikget_nround(PyObject *me, void *hunoz)
1673 kxvik_pyobj *k = (kxvik_pyobj *)me;
1674 return (PyInt_FromLong(k->n));
1677 static int kxvikset_nround(PyObject *me, PyObject *val, void *hunoz)
1679 kxvik_pyobj *k = (kxvik_pyobj *)me;
1682 if (!convuint(val, &n)) return (-1);
1687 static PyGetSetDef kxvik_pygetset[] = {
1688 #define GETSETNAME(op, name) kxvik##op##_##name
1689 GETSET(nround, "KECCAK.nround -> number of rounds")
1694 static PyMethodDef kxvik_pymethods[] = {
1695 #define METHNAME(func) kxvikmeth_##func
1696 METH (copy, "KECCAK.copy() -> KECCAK'")
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 "Keccak1600([nround = 24]): 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 static const char *const 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)) return (0); \
1816 if (shake_check(me, 0)) return (0); \
1817 STORE##W(b, x); shake_hash(SHAKE_H(me), b, sizeof(b)); \
1820 DOUINTCONV(SHAKEMETH_HASHU_)
1822 #define SHAKEMETH_HASHBUF_(n, W, w) \
1823 static PyObject *shakemeth_hashbuf##w(PyObject *me, PyObject *arg) \
1828 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
1829 if (sz > MASK##n) TYERR("string too long"); \
1830 if (shake_check(me, 0)) goto end; \
1831 STORE##W(b, sz); shake_hash(SHAKE_H(me), b, sizeof(b)); \
1832 shake_hash(SHAKE_H(me), p, sz); \
1837 DOUINTCONV(SHAKEMETH_HASHBUF_)
1839 static PyObject *shakemeth_hashstrz(PyObject *me, PyObject *arg)
1842 if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0);
1843 if (shake_check(me, 0)) return (0);
1844 shake_hash(SHAKE_H(me), p, strlen(p) + 1);
1848 static PyObject *shakemeth_xof(PyObject *me, PyObject *arg)
1850 if (!PyArg_ParseTuple(arg, ":xof")) goto end;
1851 if (shake_check(me, 0)) goto end;
1852 shake_xof(SHAKE_H(me));
1859 static PyObject *shakemeth_done(PyObject *me, PyObject *arg)
1863 if (!PyArg_ParseTuple(arg, "O&:done", convszt, &n)) goto end;
1864 if (shake_check(me, 0)) goto end;
1865 rc = bytestring_pywrap(0, n);
1866 shake_done(SHAKE_H(me), PyString_AS_STRING(rc), n);
1872 static PyObject *shakemeth_copy(PyObject *me, PyObject *arg)
1874 shake_pyobj *rc = 0;
1876 if (!PyArg_ParseTuple(arg, ":copy")) goto end;
1877 rc = PyObject_NEW(shake_pyobj, me->ob_type);
1878 rc->h = *SHAKE_H(me);
1879 rc->st = SHAKE_ST(me);
1881 return ((PyObject *)me);
1884 static PyObject *shakemeth_get(PyObject *me, PyObject *arg)
1889 if (!PyArg_ParseTuple(arg, "O&:get", convszt, &sz)) goto end;
1890 if (shake_check(me, 1)) goto end;
1891 rc = bytestring_pywrap(0, sz);
1892 shake_get(SHAKE_H(me), PyString_AS_STRING(rc), sz);
1897 static PyObject *shakemeth_mask(PyObject *me, PyObject *arg)
1900 char *p; Py_ssize_t sz;
1902 if (!PyArg_ParseTuple(arg, "s#:mask", &p, &sz)) goto end;
1903 if (shake_check(me, 1)) goto end;
1904 rc = bytestring_pywrap(0, sz);
1905 shake_mask(SHAKE_H(me), p, PyString_AS_STRING(rc), sz);
1910 static PyObject *shakeget_rate(PyObject *me, void *hunoz)
1911 { return (PyInt_FromLong(SHAKE_H(me)->h.r)); }
1913 static PyObject *shakeget_buffered(PyObject *me, void *hunoz)
1914 { return (PyInt_FromLong(SHAKE_H(me)->h.n)); }
1916 static PyObject *shakeget_state(PyObject *me, void *hunoz)
1918 int st = SHAKE_ST(me);
1919 return (PyString_FromString(st == 0 ? "absorb" :
1920 st == 1 ? "squeeze" : "dead"));
1923 static PyGetSetDef shake_pygetset[] = {
1924 #define GETSETNAME(op, name) shake##op##_##name
1925 GET (rate, "S.rate -> rate, in bytes")
1926 GET (buffered, "S.buffered -> amount currently buffered")
1927 GET (state, "S.state -> `absorb', `squeeze', `dead'")
1932 static PyMethodDef shake_pymethods[] = {
1933 #define METHNAME(func) shakemeth_##func
1934 METH (copy, "S.copy() -> SS")
1935 METH (hash, "S.hash(M)")
1936 #define METHU_(n, W, w) METH(hashu##w, "S.hashu" #w "(WORD)")
1939 #define METHBUF_(n, W, w) METH(hashbuf##w, "S.hashbuf" #w "(BYTES)")
1940 DOUINTCONV(METHBUF_)
1942 METH (hashstrz, "S.hashstrz(STRING)")
1943 METH (xof, "S.xof()")
1944 METH (done, "S.done(LEN) ->H")
1945 METH (get, "S.get(LEN) -> H")
1946 METH (mask, "S.mask(M) -> C")
1951 static PyTypeObject shake_pytype_skel = {
1952 PyObject_HEAD_INIT(0) 0, /* Header */
1953 "Shake", /* @tp_name@ */
1954 sizeof(shake_pyobj), /* @tp_basicsize@ */
1955 0, /* @tp_itemsize@ */
1957 0, /* @tp_dealloc@ */
1959 0, /* @tp_getattr@ */
1960 0, /* @tp_setattr@ */
1961 0, /* @tp_compare@ */
1963 0, /* @tp_as_number@ */
1964 0, /* @tp_as_sequence@ */
1965 0, /* @tp_as_mapping@ */
1969 0, /* @tp_getattro@ */
1970 0, /* @tp_setattro@ */
1971 0, /* @tp_as_buffer@ */
1972 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1973 Py_TPFLAGS_BASETYPE,
1976 "SHAKE/cSHAKE base class.",
1978 0, /* @tp_traverse@ */
1980 0, /* @tp_richcompare@ */
1981 0, /* @tp_weaklistoffset@ */
1983 0, /* @tp_iternext@ */
1984 shake_pymethods, /* @tp_methods@ */
1985 0, /* @tp_members@ */
1986 shake_pygetset, /* @tp_getset@ */
1989 0, /* @tp_descr_get@ */
1990 0, /* @tp_descr_set@ */
1991 0, /* @tp_dictoffset@ */
1993 PyType_GenericAlloc, /* @tp_alloc@ */
1994 abstract_pynew, /* @tp_new@ */
1999 static PyTypeObject shake128_pytype_skel = {
2000 PyObject_HEAD_INIT(0) 0, /* Header */
2001 "Shake128", /* @tp_name@ */
2002 0, /* @tp_basicsize@ */
2003 0, /* @tp_itemsize@ */
2005 0, /* @tp_dealloc@ */
2007 0, /* @tp_getattr@ */
2008 0, /* @tp_setattr@ */
2009 0, /* @tp_compare@ */
2011 0, /* @tp_as_number@ */
2012 0, /* @tp_as_sequence@ */
2013 0, /* @tp_as_mapping@ */
2017 0, /* @tp_getattro@ */
2018 0, /* @tp_setattro@ */
2019 0, /* @tp_as_buffer@ */
2020 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2021 Py_TPFLAGS_BASETYPE,
2024 "Shake128([perso = STR], [func = STR]): SHAKE128/cSHAKE128 XOF.",
2026 0, /* @tp_traverse@ */
2028 0, /* @tp_richcompare@ */
2029 0, /* @tp_weaklistoffset@ */
2031 0, /* @tp_iternext@ */
2032 0, /* @tp_methods@ */
2033 0, /* @tp_members@ */
2034 0, /* @tp_getset@ */
2037 0, /* @tp_descr_get@ */
2038 0, /* @tp_descr_set@ */
2039 0, /* @tp_dictoffset@ */
2041 PyType_GenericAlloc, /* @tp_alloc@ */
2042 shake128_pynew, /* @tp_new@ */
2047 static PyTypeObject shake256_pytype_skel = {
2048 PyObject_HEAD_INIT(0) 0, /* Header */
2049 "Shake256", /* @tp_name@ */
2050 0, /* @tp_basicsize@ */
2051 0, /* @tp_itemsize@ */
2053 0, /* @tp_dealloc@ */
2055 0, /* @tp_getattr@ */
2056 0, /* @tp_setattr@ */
2057 0, /* @tp_compare@ */
2059 0, /* @tp_as_number@ */
2060 0, /* @tp_as_sequence@ */
2061 0, /* @tp_as_mapping@ */
2065 0, /* @tp_getattro@ */
2066 0, /* @tp_setattro@ */
2067 0, /* @tp_as_buffer@ */
2068 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2069 Py_TPFLAGS_BASETYPE,
2072 "Shake256([perso = STR], [func = STR]): SHAKE256/cSHAKE256 XOF.",
2074 0, /* @tp_traverse@ */
2076 0, /* @tp_richcompare@ */
2077 0, /* @tp_weaklistoffset@ */
2079 0, /* @tp_iternext@ */
2080 0, /* @tp_methods@ */
2081 0, /* @tp_members@ */
2082 0, /* @tp_getset@ */
2085 0, /* @tp_descr_get@ */
2086 0, /* @tp_descr_set@ */
2087 0, /* @tp_dictoffset@ */
2089 PyType_GenericAlloc, /* @tp_alloc@ */
2090 shake256_pynew, /* @tp_new@ */
2095 /*----- Pseudorandom permutations -----------------------------------------*/
2097 static PyTypeObject *gcprp_pytype, *gprp_pytype;
2099 typedef struct prpinfo {
2104 void (*init)(void *, const void *, size_t);
2105 void (*eblk)(void *, const void *, void *);
2106 void (*dblk)(void *, const void *, void *);
2109 #define PRP_DEF(PRE, pre) \
2110 static void pre##_prpinit(void *ctx, const void *k, size_t ksz) \
2111 { pre##_init(ctx, k, ksz); } \
2112 static void pre##_prpeblk(void *ctx, const void *in, void *out) \
2114 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
2115 pre##_eblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
2117 static void pre##_prpdblk(void *ctx, const void *in, void *out) \
2119 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
2120 pre##_dblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
2122 static const prpinfo pre##_prpinfo = { \
2123 #pre, pre##_keysz, sizeof(pre##_ctx), PRE##_BLKSZ, \
2124 pre##_prpinit, pre##_prpeblk, pre##_prpdblk \
2128 static const struct prpinfo *const gprptab[] = {
2129 #define PRP_ENTRY(PRE, pre) &pre##_prpinfo,
2134 typedef struct gcprp_pyobj {
2135 PyHeapTypeObject ty;
2138 #define GCPRP_PRP(o) (((gcprp_pyobj *)(o))->prp)
2140 typedef struct gprp_pyobj {
2144 #define GPRP_PRP(o) (((gprp_pyobj *)(o))->prp)
2145 #define GPRP_CTX(o) (((gprp_pyobj *)(o)) + 1)
2147 typedef struct prp {
2152 static PyObject *gprp_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
2154 static const char *const kwlist[] = { "key", 0 };
2157 const prpinfo *prp = GCPRP_PRP(ty);
2160 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &k, &sz))
2162 if (keysz(sz, prp->keysz) != sz) VALERR("bad key length");
2163 me = (PyObject *)ty->tp_alloc(ty, 0);
2165 prp->init(GPRP_CTX(me), k, sz);
2172 static void gprp_pydealloc(PyObject *me)
2173 { Py_DECREF(me->ob_type); FREEOBJ(me); }
2175 static PyObject *gcprp_pywrap(const prpinfo *prp)
2177 gcprp_pyobj *g = newtype(gcprp_pytype, 0, prp->name);
2179 g->ty.ht_type.tp_basicsize = sizeof(gprp_pyobj) + prp->ctxsz;
2180 g->ty.ht_type.tp_base = gprp_pytype;
2181 Py_INCREF(gprp_pytype);
2182 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
2183 Py_TPFLAGS_BASETYPE |
2184 Py_TPFLAGS_HEAPTYPE);
2185 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
2186 g->ty.ht_type.tp_free = 0;
2187 g->ty.ht_type.tp_new = gprp_pynew;
2188 typeready(&g->ty.ht_type);
2189 return ((PyObject *)g);
2192 static PyObject *gcpget_name(PyObject *me, void *hunoz)
2193 { return (PyString_FromString(GCPRP_PRP(me)->name)); }
2194 static PyObject *gcpget_keysz(PyObject *me, void *hunoz)
2195 { return (keysz_pywrap(GCPRP_PRP(me)->keysz)); }
2196 static PyObject *gcpget_blksz(PyObject *me, void *hunoz)
2197 { return (PyInt_FromLong(GCPRP_PRP(me)->blksz)); }
2199 static PyObject *gpmeth_encrypt(PyObject *me, PyObject *arg)
2205 if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &n)) goto end;
2206 if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length");
2207 rc = bytestring_pywrap(0, n);
2208 GPRP_PRP(me)->eblk(GPRP_CTX(me), p, PyString_AS_STRING(rc));
2213 static PyObject *gpmeth_decrypt(PyObject *me, PyObject *arg)
2219 if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &n)) goto end;
2220 if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length");
2221 rc = bytestring_pywrap(0, n);
2222 GPRP_PRP(me)->dblk(GPRP_CTX(me), p, PyString_AS_STRING(rc));
2227 static PyGetSetDef gcprp_pygetset[] = {
2228 #define GETSETNAME(op, name) gcp##op##_##name
2229 GET (keysz, "CP.keysz -> acceptable key sizes")
2230 GET (blksz, "CP.blksz -> block size")
2231 GET (name, "CP.name -> name of this kind of PRP")
2236 static PyMethodDef gprp_pymethods[] = {
2237 #define METHNAME(name) gpmeth_##name
2238 METH (encrypt, "P.encrypt(PT) -> CT")
2239 METH (decrypt, "P.decrypt(CT) -> PT")
2244 static PyTypeObject gcprp_pytype_skel = {
2245 PyObject_HEAD_INIT(0) 0, /* Header */
2246 "GCPRP", /* @tp_name@ */
2247 sizeof(gcprp_pyobj), /* @tp_basicsize@ */
2248 0, /* @tp_itemsize@ */
2250 0, /* @tp_dealloc@ */
2252 0, /* @tp_getattr@ */
2253 0, /* @tp_setattr@ */
2254 0, /* @tp_compare@ */
2256 0, /* @tp_as_number@ */
2257 0, /* @tp_as_sequence@ */
2258 0, /* @tp_as_mapping@ */
2262 0, /* @tp_getattro@ */
2263 0, /* @tp_setattro@ */
2264 0, /* @tp_as_buffer@ */
2265 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2266 Py_TPFLAGS_BASETYPE,
2269 "Pseudorandom permutation metaclass.",
2271 0, /* @tp_traverse@ */
2273 0, /* @tp_richcompare@ */
2274 0, /* @tp_weaklistoffset@ */
2276 0, /* @tp_iternext@ */
2277 0, /* @tp_methods@ */
2278 0, /* @tp_members@ */
2279 gcprp_pygetset, /* @tp_getset@ */
2282 0, /* @tp_descr_get@ */
2283 0, /* @tp_descr_set@ */
2284 0, /* @tp_dictoffset@ */
2286 PyType_GenericAlloc, /* @tp_alloc@ */
2287 abstract_pynew, /* @tp_new@ */
2292 static PyTypeObject gprp_pytype_skel = {
2293 PyObject_HEAD_INIT(0) 0, /* Header */
2294 "GPRP", /* @tp_name@ */
2295 sizeof(gprp_pyobj), /* @tp_basicsize@ */
2296 0, /* @tp_itemsize@ */
2298 gprp_pydealloc, /* @tp_dealloc@ */
2300 0, /* @tp_getattr@ */
2301 0, /* @tp_setattr@ */
2302 0, /* @tp_compare@ */
2304 0, /* @tp_as_number@ */
2305 0, /* @tp_as_sequence@ */
2306 0, /* @tp_as_mapping@ */
2310 0, /* @tp_getattro@ */
2311 0, /* @tp_setattro@ */
2312 0, /* @tp_as_buffer@ */
2313 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2314 Py_TPFLAGS_BASETYPE,
2317 "Pseudorandom permutation, abstract base class.",
2319 0, /* @tp_traverse@ */
2321 0, /* @tp_richcompare@ */
2322 0, /* @tp_weaklistoffset@ */
2324 0, /* @tp_iternext@ */
2325 gprp_pymethods, /* @tp_methods@ */
2326 0, /* @tp_members@ */
2327 0, /* @tp_getset@ */
2330 0, /* @tp_descr_get@ */
2331 0, /* @tp_descr_set@ */
2332 0, /* @tp_dictoffset@ */
2334 PyType_GenericAlloc, /* @tp_alloc@ */
2335 abstract_pynew, /* @tp_new@ */
2340 /*----- Main code ---------------------------------------------------------*/
2342 static PyMethodDef methods[] = {
2343 #define METHNAME(func) meth_##func
2344 METH (_KeySZ_fromdl, "\
2345 fromdl(N) -> M: convert integer discrete log field size to work factor")
2346 METH (_KeySZ_fromschnorr, "\
2347 fromschnorr(N) -> M: convert Schnorr group order to work factor")
2348 METH (_KeySZ_fromif, "\
2349 fromif(N) -> M: convert integer factorization problem size to work factor")
2350 METH (_KeySZ_fromec, "\
2351 fromec(N) -> M: convert elliptic curve group order to work factor")
2352 METH (_KeySZ_todl, "\
2353 todl(N) -> M: convert work factor to integer discrete log field size")
2354 METH (_KeySZ_toschnorr, "\
2355 toschnorr(N) -> M: convert work factor to Schnorr group order")
2356 METH (_KeySZ_toif, "\
2357 toif(N) -> M: convert work factor to integer factorization problem size")
2358 METH (_KeySZ_toec, "\
2359 toec(N) -> M: convert work factor to elliptic curve group order")
2360 METH (_KeySZ_toec, "\
2361 toec(N) -> M: convert work factor to elliptic curve group order")
2362 #define METH_HDANCE(hdance, HDance) METH(hdance##_prf, "\
2363 " #hdance "_prf(K, N) -> H: calculate " HDance " hash of N with K")
2364 METH_HDANCE(hsalsa20, "HSalsa20")
2365 METH_HDANCE(hsalsa2012, "HSalsa20/12")
2366 METH_HDANCE(hsalsa208, "HSalsa20/8")
2367 METH_HDANCE(hchacha20, "HChaCha20")
2368 METH_HDANCE(hchacha12, "HChaCha12")
2369 METH_HDANCE(hchacha8, "HChaCha8")
2375 void algorithms_pyinit(void)
2377 INITTYPE(keysz, root);
2378 INITTYPE(keyszany, keysz);
2379 INITTYPE(keyszrange, keysz);
2380 INITTYPE(keyszset, keysz);
2381 INITTYPE(gccipher, type);
2382 INITTYPE(gcipher, root);
2383 INITTYPE(gchash, type);
2384 INITTYPE(ghash, root);
2385 INITTYPE(gcmac, type);
2386 INITTYPE(gmac, type);
2387 INITTYPE(gmhash, ghash);
2388 INITTYPE(poly1305cls, type);
2389 INITTYPE_META(poly1305key, type, poly1305cls);
2390 INITTYPE(poly1305hash, root);
2391 INITTYPE(kxvik, root);
2392 INITTYPE(shake, root);
2393 INITTYPE(shake128, shake);
2394 INITTYPE(shake256, shake);
2395 INITTYPE(gcprp, type);
2396 INITTYPE(gprp, root);
2397 addmethods(methods);
2400 GEN(gcciphers, cipher)
2403 #define gcprp prpinfo
2406 void algorithms_pyinsert(PyObject *mod)
2409 INSERT("KeySZ", keysz_pytype);
2410 INSERT("KeySZAny", keyszany_pytype);
2411 INSERT("KeySZRange", keyszrange_pytype);
2412 INSERT("KeySZSet", keyszset_pytype);
2413 INSERT("GCCipher", gccipher_pytype);
2414 INSERT("GCipher", gcipher_pytype);
2415 INSERT("gcciphers", gcciphers());
2416 INSERT("GCHash", gchash_pytype);
2417 INSERT("GHash", ghash_pytype);
2418 INSERT("gchashes", d = gchashes());
2419 sha_pyobj = PyDict_GetItemString(d, "sha"); Py_INCREF(sha_pyobj);
2420 has160_pyobj = PyDict_GetItemString(d, "has160"); Py_INCREF(has160_pyobj);
2421 INSERT("GCMAC", gcmac_pytype);
2422 INSERT("GMAC", gmac_pytype);
2423 INSERT("GMACHash", gmhash_pytype);
2424 INSERT("gcmacs", gcmacs());
2425 INSERT("Poly1305Class", poly1305cls_pytype);
2426 INSERT("poly1305", poly1305key_pytype);
2427 INSERT("Poly1305Hash", poly1305hash_pytype);
2428 INSERT("Keccak1600", kxvik_pytype);
2429 INSERT("Shake", shake_pytype);
2430 INSERT("Shake128", shake128_pytype);
2431 INSERT("Shake256", shake256_pytype);
2432 INSERT("GCPRP", gcprp_pytype);
2433 INSERT("GPRP", gprp_pytype);
2434 INSERT("gcprps", gcprps());
2437 /*----- That's all, folks -------------------------------------------------*/