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); if (n < 0) goto end;
139 l = PyList_New(0); if (!l) goto end;
140 if (dfl < 0) VALERR("key size cannot be negative");
141 x = PyInt_FromLong(dfl);
145 for (i = 0; i < n; i++) {
146 if ((x = PySequence_GetItem(set, i)) == 0) goto end;
147 xx = PyInt_AsLong(x);
148 if (PyErr_Occurred()) goto end;
149 if (xx == dfl) continue;
150 if (xx < 0) VALERR("key size cannot be negative");
156 if ((set = PySequence_Tuple(l)) == 0) goto end;
157 o = (keyszset_pyobj *)ty->tp_alloc(ty, 0);
165 return ((PyObject *)o);
168 static PyObject *kaget_min(PyObject *me, void *hunoz)
169 { return (PyInt_FromLong(0)); }
170 #define kaget_max kaget_min
172 static PyObject *ksget_min(PyObject *me, void *hunoz)
174 PyObject *set = ((keyszset_pyobj *)me)->set;
176 n = PyTuple_Size(set);
177 for (i = 0; i < n; i++) {
178 y = PyInt_AsLong(PyTuple_GetItem(set, i));
179 if (x == -1 || y < x) x = y;
181 return (PyInt_FromLong(x));
184 static PyObject *ksget_max(PyObject *me, void *hunoz)
186 PyObject *set = ((keyszset_pyobj *)me)->set;
188 n = PyTuple_Size(set);
189 for (i = 0; i < n; i++) {
190 y = PyInt_AsLong(PyTuple_GetItem(set, i));
193 return (PyInt_FromLong(x));
196 static PyMemberDef keysz_pymembers[] = {
197 #define MEMBERSTRUCT keysz_pyobj
198 #define default dfl /* ugh! */
199 MEMBER(default, T_INT, READONLY, "KSZ.default -> default key size")
205 static PyGetSetDef keyszany_pygetset[] = {
206 #define GETSETNAME(op, name) ka##op##_##name
207 GET (min, "KSZ.min -> smallest allowed key size")
208 GET (max, "KSZ.max -> largest allowed key size")
213 static PyMemberDef keyszrange_pymembers[] = {
214 #define MEMBERSTRUCT keyszrange_pyobj
215 MEMBER(min, T_INT, READONLY, "KSZ.min -> smallest allowed key size")
216 MEMBER(max, T_INT, READONLY, "KSZ.max -> largest allowed key size")
217 MEMBER(mod, T_INT, READONLY,
218 "KSZ.mod -> key size must be a multiple of this")
223 static PyGetSetDef keyszset_pygetset[] = {
224 #define GETSETNAME(op, name) ks##op##_##name
225 GET (min, "KSZ.min -> smallest allowed key size")
226 GET (max, "KSZ.max -> largest allowed key size")
231 static PyMemberDef keyszset_pymembers[] = {
232 #define MEMBERSTRUCT keyszset_pyobj
233 MEMBER(set, T_OBJECT, READONLY, "KSZ.set -> allowed key sizes")
238 static PyTypeObject keysz_pytype_skel = {
239 PyObject_HEAD_INIT(0) 0, /* Header */
240 "KeySZ", /* @tp_name@ */
241 sizeof(keysz_pyobj), /* @tp_basicsize@ */
242 0, /* @tp_itemsize@ */
244 0, /* @tp_dealloc@ */
246 0, /* @tp_getattr@ */
247 0, /* @tp_setattr@ */
248 0, /* @tp_compare@ */
250 0, /* @tp_as_number@ */
251 0, /* @tp_as_sequence@ */
252 0, /* @tp_as_mapping@ */
256 0, /* @tp_getattro@ */
257 0, /* @tp_setattro@ */
258 0, /* @tp_as_buffer@ */
259 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
263 "Key size constraints. Abstract.",
265 0, /* @tp_traverse@ */
267 0, /* @tp_richcompare@ */
268 0, /* @tp_weaklistoffset@ */
270 0, /* @tp_iternext@ */
271 0, /* @tp_methods@ */
272 keysz_pymembers, /* @tp_members@ */
276 0, /* @tp_descr_get@ */
277 0, /* @tp_descr_set@ */
278 0, /* @tp_dictoffset@ */
280 PyType_GenericAlloc, /* @tp_alloc@ */
281 abstract_pynew, /* @tp_new@ */
286 static PyTypeObject keyszany_pytype_skel = {
287 PyObject_HEAD_INIT(0) 0, /* Header */
288 "KeySZAny", /* @tp_name@ */
289 sizeof(keysz_pyobj), /* @tp_basicsize@ */
290 0, /* @tp_itemsize@ */
292 0, /* @tp_dealloc@ */
294 0, /* @tp_getattr@ */
295 0, /* @tp_setattr@ */
296 0, /* @tp_compare@ */
298 0, /* @tp_as_number@ */
299 0, /* @tp_as_sequence@ */
300 0, /* @tp_as_mapping@ */
304 0, /* @tp_getattro@ */
305 0, /* @tp_setattro@ */
306 0, /* @tp_as_buffer@ */
307 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
311 "KeySZAny(DEFAULT)\n\
312 Key size constraints. This object imposes no constraints on size.",
314 0, /* @tp_traverse@ */
316 0, /* @tp_richcompare@ */
317 0, /* @tp_weaklistoffset@ */
319 0, /* @tp_iternext@ */
320 0, /* @tp_methods@ */
321 0, /* @tp_members@ */
322 keyszany_pygetset, /* @tp_getset@ */
325 0, /* @tp_descr_get@ */
326 0, /* @tp_descr_set@ */
327 0, /* @tp_dictoffset@ */
329 PyType_GenericAlloc, /* @tp_alloc@ */
330 keyszany_pynew, /* @tp_new@ */
335 static PyTypeObject keyszrange_pytype_skel = {
336 PyObject_HEAD_INIT(0) 0, /* Header */
337 "KeySZRange", /* @tp_name@ */
338 sizeof(keyszrange_pyobj), /* @tp_basicsize@ */
339 0, /* @tp_itemsize@ */
341 0, /* @tp_dealloc@ */
343 0, /* @tp_getattr@ */
344 0, /* @tp_setattr@ */
345 0, /* @tp_compare@ */
347 0, /* @tp_as_number@ */
348 0, /* @tp_as_sequence@ */
349 0, /* @tp_as_mapping@ */
353 0, /* @tp_getattro@ */
354 0, /* @tp_setattro@ */
355 0, /* @tp_as_buffer@ */
356 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
360 "KeySZRange(DEFAULT, [min = 0], [max = 0], [mod = 1])\n\
361 Key size constraints. Key size must be between MIN and MAX inclusive,\n\
362 and be a multiple of MOD.",
364 0, /* @tp_traverse@ */
366 0, /* @tp_richcompare@ */
367 0, /* @tp_weaklistoffset@ */
369 0, /* @tp_iternext@ */
370 0, /* @tp_methods@ */
371 keyszrange_pymembers, /* @tp_members@ */
375 0, /* @tp_descr_get@ */
376 0, /* @tp_descr_set@ */
377 0, /* @tp_dictoffset@ */
379 PyType_GenericAlloc, /* @tp_alloc@ */
380 keyszrange_pynew, /* @tp_new@ */
385 static PyTypeObject keyszset_pytype_skel = {
386 PyObject_HEAD_INIT(0) 0, /* Header */
387 "KeySZSet", /* @tp_name@ */
388 sizeof(keyszset_pyobj), /* @tp_basicsize@ */
389 0, /* @tp_itemsize@ */
391 0, /* @tp_dealloc@ */
393 0, /* @tp_getattr@ */
394 0, /* @tp_setattr@ */
395 0, /* @tp_compare@ */
397 0, /* @tp_as_number@ */
398 0, /* @tp_as_sequence@ */
399 0, /* @tp_as_mapping@ */
403 0, /* @tp_getattro@ */
404 0, /* @tp_setattro@ */
405 0, /* @tp_as_buffer@ */
406 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
410 "KeySZSet(DEFAULT, SEQ)\n\
411 Key size constraints. Key size must be DEFAULT or one in SEQ.",
413 0, /* @tp_traverse@ */
415 0, /* @tp_richcompare@ */
416 0, /* @tp_weaklistoffset@ */
418 0, /* @tp_iternext@ */
419 0, /* @tp_methods@ */
420 keyszset_pymembers, /* @tp_members@ */
421 keyszset_pygetset, /* @tp_getset@ */
424 0, /* @tp_descr_get@ */
425 0, /* @tp_descr_set@ */
426 0, /* @tp_dictoffset@ */
428 PyType_GenericAlloc, /* @tp_alloc@ */
429 keyszset_pynew, /* @tp_new@ */
434 #define KSZCONVOP(op) \
435 static PyObject *meth__KeySZ_##op(PyObject *me, PyObject *arg) \
438 if (!PyArg_ParseTuple(arg, "Od:" #op, &me, &x)) return (0); \
440 return (PyFloat_FromDouble(y)); \
443 KSZCONVOP(fromschnorr)
452 /*----- Symmetric encryption ----------------------------------------------*/
454 PyTypeObject *gccipher_pytype, *gcipher_pytype;
456 CONVFUNC(gccipher, gccipher *, GCCIPHER_CC)
457 CONVFUNC(gcipher, gcipher *, GCIPHER_C)
459 PyObject *gcipher_pywrap(PyObject *cobj, gcipher *c)
462 if (!cobj) cobj = gccipher_pywrap((/*unconst*/ gccipher *)GC_CLASS(c));
463 else Py_INCREF(cobj);
464 g = PyObject_NEW(gcipher_pyobj, (PyTypeObject *)cobj);
466 return ((PyObject *)g);
469 static PyObject *gcipher_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
471 static const char *const kwlist[] = { "k", 0 };
475 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &k, &sz))
477 if (keysz(sz, GCCIPHER_CC(ty)->keysz) != sz) VALERR("bad key length");
478 return (gcipher_pywrap((PyObject *)ty,
479 GC_INIT(GCCIPHER_CC(ty), k, sz)));
484 PyObject *gccipher_pywrap(gccipher *cc)
486 gccipher_pyobj *g = newtype(gccipher_pytype, 0, cc->name);
488 g->ty.ht_type.tp_basicsize = sizeof(gcipher_pyobj);
489 g->ty.ht_type.tp_base = gcipher_pytype;
490 Py_INCREF(gcipher_pytype);
491 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
492 Py_TPFLAGS_BASETYPE |
493 Py_TPFLAGS_HEAPTYPE);
494 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
495 g->ty.ht_type.tp_free = 0;
496 g->ty.ht_type.tp_new = gcipher_pynew;
497 typeready(&g->ty.ht_type);
498 return ((PyObject *)g);
501 static void gcipher_pydealloc(PyObject *me)
503 GC_DESTROY(GCIPHER_C(me));
504 Py_DECREF(me->ob_type);
508 static PyObject *gccget_name(PyObject *me, void *hunoz)
509 { return (PyString_FromString(GCCIPHER_CC(me)->name)); }
511 static PyObject *gccget_keysz(PyObject *me, void *hunoz)
512 { return (keysz_pywrap(GCCIPHER_CC(me)->keysz)); }
514 static PyObject *gccget_blksz(PyObject *me, void *hunoz)
515 { return (PyInt_FromLong(GCCIPHER_CC(me)->blksz)); }
517 static PyObject *gcmeth_encrypt(PyObject *me, PyObject *arg)
523 if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &sz)) return (0);
524 rc = bytestring_pywrap(0, sz);
525 GC_ENCRYPT(GCIPHER_C(me), p, PyString_AS_STRING(rc), sz);
529 static PyObject *gcmeth_enczero(PyObject *me, PyObject *arg)
535 if (!PyArg_ParseTuple(arg, "i:enczero", &sz)) return (0);
536 rc = bytestring_pywrap(0, sz);
537 p = PyString_AS_STRING(rc);
539 GC_ENCRYPT(GCIPHER_C(me), p, p, sz);
543 static PyObject *gcmeth_decrypt(PyObject *me, PyObject *arg)
549 if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &sz)) return (0);
550 rc = bytestring_pywrap(0, sz);
551 GC_DECRYPT(GCIPHER_C(me), p, PyString_AS_STRING(rc), sz);
555 static PyObject *gcmeth_deczero(PyObject *me, PyObject *arg)
561 if (!PyArg_ParseTuple(arg, "i:deczero", &sz)) return (0);
562 rc = bytestring_pywrap(0, sz);
563 p = PyString_AS_STRING(rc);
565 GC_DECRYPT(GCIPHER_C(me), p, p, sz);
569 static PyObject *gcmeth_setiv(PyObject *me, PyObject *arg)
574 if (!PyArg_ParseTuple(arg, "s#:setiv", &p, &sz)) goto end;
575 if (!GCIPHER_C(me)->ops->setiv) VALERR("`setiv' not supported");
576 if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode");
577 if (sz != GC_CLASS(GCIPHER_C(me))->blksz) VALERR("bad IV length");
578 GC_SETIV(GCIPHER_C(me), p);
584 static PyObject *gcmeth_bdry(PyObject *me, PyObject *arg)
586 if (!PyArg_ParseTuple(arg, ":bdry")) goto end;
587 if (!GCIPHER_C(me)->ops->bdry) VALERR("`bdry' not supported");
588 if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode");
589 GC_BDRY(GCIPHER_C(me));
595 static PyGetSetDef gccipher_pygetset[] = {
596 #define GETSETNAME(op, name) gcc##op##_##name
597 GET (keysz, "CC.keysz -> acceptable key sizes")
598 GET (blksz, "CC.blksz -> block size, or zero")
599 GET (name, "CC.name -> name of this kind of cipher")
604 static PyMethodDef gcipher_pymethods[] = {
605 #define METHNAME(name) gcmeth_##name
606 METH (encrypt, "C.encrypt(PT) -> CT")
607 METH (enczero, "C.enczero(N) -> CT")
608 METH (decrypt, "C.decrypt(CT) -> PT")
609 METH (deczero, "C.deczero(N) -> PT")
610 METH (setiv, "C.setiv(IV)")
611 METH (bdry, "C.bdry()")
616 static PyTypeObject gccipher_pytype_skel = {
617 PyObject_HEAD_INIT(0) 0, /* Header */
618 "GCCipher", /* @tp_name@ */
619 sizeof(gccipher_pyobj), /* @tp_basicsize@ */
620 0, /* @tp_itemsize@ */
622 0, /* @tp_dealloc@ */
624 0, /* @tp_getattr@ */
625 0, /* @tp_setattr@ */
626 0, /* @tp_compare@ */
628 0, /* @tp_as_number@ */
629 0, /* @tp_as_sequence@ */
630 0, /* @tp_as_mapping@ */
634 0, /* @tp_getattro@ */
635 0, /* @tp_setattro@ */
636 0, /* @tp_as_buffer@ */
637 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
641 "Symmetric cipher metaclass.",
643 0, /* @tp_traverse@ */
645 0, /* @tp_richcompare@ */
646 0, /* @tp_weaklistoffset@ */
648 0, /* @tp_iternext@ */
649 0, /* @tp_methods@ */
650 0, /* @tp_members@ */
651 gccipher_pygetset, /* @tp_getset@ */
654 0, /* @tp_descr_get@ */
655 0, /* @tp_descr_set@ */
656 0, /* @tp_dictoffset@ */
658 PyType_GenericAlloc, /* @tp_alloc@ */
659 abstract_pynew, /* @tp_new@ */
664 static PyTypeObject gcipher_pytype_skel = {
665 PyObject_HEAD_INIT(0) 0, /* Header */
666 "GCipher", /* @tp_name@ */
667 sizeof(gcipher_pyobj), /* @tp_basicsize@ */
668 0, /* @tp_itemsize@ */
670 gcipher_pydealloc, /* @tp_dealloc@ */
672 0, /* @tp_getattr@ */
673 0, /* @tp_setattr@ */
674 0, /* @tp_compare@ */
676 0, /* @tp_as_number@ */
677 0, /* @tp_as_sequence@ */
678 0, /* @tp_as_mapping@ */
682 0, /* @tp_getattro@ */
683 0, /* @tp_setattro@ */
684 0, /* @tp_as_buffer@ */
685 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
689 "Symmetric cipher, abstract base class.",
691 0, /* @tp_traverse@ */
693 0, /* @tp_richcompare@ */
694 0, /* @tp_weaklistoffset@ */
696 0, /* @tp_iternext@ */
697 gcipher_pymethods, /* @tp_methods@ */
698 0, /* @tp_members@ */
702 0, /* @tp_descr_get@ */
703 0, /* @tp_descr_set@ */
704 0, /* @tp_dictoffset@ */
706 PyType_GenericAlloc, /* @tp_alloc@ */
707 abstract_pynew, /* @tp_new@ */
712 /*----- Authenticated encryption ------------------------------------------*/
714 PyTypeObject *gcaead_pytype, *gaeadkey_pytype;
715 PyTypeObject *gcaeadaad_pytype, *gaeadaad_pytype;
716 PyTypeObject *gcaeadenc_pytype, *gaeadenc_pytype;
717 PyTypeObject *gcaeaddec_pytype, *gaeaddec_pytype;
719 CONVFUNC(gcaead, gcaead *, GCAEAD_AEC)
720 CONVFUNC(gaeadkey, gaead_key *, GAEADKEY_K)
722 PyObject *gaeadkey_pywrap(PyObject *cobj, gaead_key *k)
726 if (!cobj) cobj = gcaead_pywrap((/*unconst*/ gcaead *)GAEAD_CLASS(k));
727 else Py_INCREF(cobj);
728 gk = PyObject_NEW(gaeadkey_pyobj, (PyTypeObject *)cobj);
730 return ((PyObject *)gk);
733 static PyObject *gaeadkey_pynew(PyTypeObject *ty,
734 PyObject *arg, PyObject *kw)
736 static const char *const kwlist[] = { "k", 0 };
740 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &k, &sz))
742 if (keysz(sz, GCAEAD_AEC(ty)->keysz) != sz) VALERR("bad key length");
743 return (gaeadkey_pywrap((PyObject *)ty,
744 GAEAD_KEY(GCAEAD_AEC(ty), k, sz)));
749 PyObject *gcaead_pywrap(gcaead *aec)
752 gcaeadaad_pyobj *gca;
753 gcaeadenc_pyobj *gce;
754 gcaeaddec_pyobj *gcd;
756 #define MKTYPE(obj, thing, newfn, namefmt) do { \
757 (obj) = newtype(gcaead_pytype, 0, 0); \
758 (obj)->ty.ht_name = PyString_FromFormat(namefmt, aec->name); \
759 (obj)->ty.ht_type.tp_name = PyString_AS_STRING((obj)->ty.ht_name); \
760 (obj)->ty.ht_type.tp_basicsize = sizeof(gaead##thing##_pyobj); \
761 (obj)->ty.ht_type.tp_base = gaead##thing##_pytype; \
762 Py_INCREF(gaead##thing##_pytype); \
763 (obj)->ty.ht_type.tp_flags = \
764 (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE); \
765 (obj)->ty.ht_type.tp_alloc = PyType_GenericAlloc; \
766 (obj)->ty.ht_type.tp_free = 0; \
767 (obj)->ty.ht_type.tp_new = newfn; \
768 typeready(&(obj)->ty.ht_type); \
771 MKTYPE(gck, key, gaeadkey_pynew, "%s(key)");
772 MKTYPE(gca, aad, abstract_pynew, "%s(aad-hash)");
773 MKTYPE(gce, enc, abstract_pynew, "%s(encrypt)");
774 MKTYPE(gcd, dec, abstract_pynew, "%s(decrypt)");
778 gck->aec = aec; gck->aad = gca; gck->enc = gce; gck->dec = gcd;
779 gca->key = gce->key = gcd->key = gck;
780 return ((PyObject *)gck);
783 static void gaeadkey_pydealloc(PyObject *me)
784 { GAEAD_DESTROY(GAEADKEY_K(me)); Py_DECREF(me->ob_type); FREEOBJ(me); }
786 static PyObject *gcaeget_name(PyObject *me, void *hunoz)
787 { return (PyString_FromString(GCAEAD_AEC(me)->name)); }
789 static PyObject *gcaeget_keysz(PyObject *me, void *hunoz)
790 { return (keysz_pywrap(GCAEAD_AEC(me)->keysz)); }
792 static PyObject *gcaeget_noncesz(PyObject *me, void *hunoz)
793 { return (keysz_pywrap(GCAEAD_AEC(me)->noncesz)); }
795 static PyObject *gcaeget_tagsz(PyObject *me, void *hunoz)
796 { return (keysz_pywrap(GCAEAD_AEC(me)->tagsz)); }
798 static PyObject *gcaeget_blksz(PyObject *me, void *hunoz)
799 { return (PyInt_FromLong(GCAEAD_AEC(me)->blksz)); }
801 static PyObject *gcaeget_bufsz(PyObject *me, void *hunoz)
802 { return (PyInt_FromLong(GCAEAD_AEC(me)->bufsz)); }
804 static PyObject *gcaeget_ohd(PyObject *me, void *hunoz)
805 { return (PyInt_FromLong(GCAEAD_AEC(me)->ohd)); }
807 static PyObject *gcaeget_flags(PyObject *me, void *hunoz)
808 { return (PyInt_FromLong(GCAEAD_AEC(me)->f)); }
810 static PyGetSetDef gcaead_pygetset[] = {
811 #define GETSETNAME(op, name) gcae##op##_##name
812 GET (keysz, "AEC.keysz -> acceptable key sizes")
813 GET (noncesz, "AEC.noncesz -> acceptable nonce sizes")
814 GET (tagsz, "AEC.tagsz -> acceptable tag sizes")
815 GET (blksz, "AEC.blksz -> block size, or zero")
816 GET (bufsz, "AEC.bufsz -> amount of data buffered internally")
817 GET (ohd, "AEC.ohd -> maximum encryption overhead")
818 GET (name, "AEC.name -> name of this kind of AEAD scheme")
819 GET (flags, "AEC.flags -> mask of `AEADF_...' flags")
824 static PyObject *gaekmeth_aad(PyObject *me, PyObject *arg)
826 const gaead_key *k = GAEADKEY_K(me);
829 if (!PyArg_ParseTuple(arg, ":aad")) return (0);
830 if (k->ops->c->f&AEADF_AADNDEP)
831 VALERR("aad must be associated with enc/dec op");
832 rc = gaeadaad_pywrap((PyObject *)GCAEAD_AAD(me->ob_type),
838 static int check_aead_encdec(const gcaead *aec, unsigned *f_out, size_t nsz,
839 PyObject *hszobj, size_t *hsz_out,
840 PyObject *mszobj, size_t *msz_out,
841 PyObject *tszobj, size_t *tsz_out)
843 unsigned f = 0, miss;
846 if (hszobj != Py_None)
847 { f |= AEADF_PCHSZ; if (!convszt(hszobj, hsz_out)) goto end; }
848 if (mszobj != Py_None)
849 { f |= AEADF_PCMSZ; if (!convszt(mszobj, msz_out)) goto end; }
850 if (tszobj != Py_None)
851 { f |= AEADF_PCTSZ; if (!convszt(tszobj, tsz_out)) goto end; }
853 if (miss&AEADF_PCHSZ) VALERR("header length precommitment required");
854 if (miss&AEADF_PCMSZ) VALERR("message length precommitment required");
855 if (miss&AEADF_PCTSZ) VALERR("tag length precommitment required");
856 if (keysz(nsz, aec->noncesz) != nsz) VALERR("bad nonce length");
857 if (tszobj != Py_None && keysz(*tsz_out, aec->tagsz) != *tsz_out)
858 VALERR("bad tag length");
859 *f_out = f | aec->f; rc = 0;
864 static PyObject *gaekmeth_enc(PyObject *me, PyObject *arg, PyObject *kw)
866 static const char *const kwlist[] = { "nonce", "hsz", "msz", "tsz", 0 };
867 const gaead_key *k = GAEADKEY_K(me);
870 char *n; Py_ssize_t nsz;
871 PyObject *hszobj = Py_None, *mszobj = Py_None, *tszobj = Py_None;
872 size_t hsz = 0, msz = 0, tsz = 0;
875 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|OOO:enc", KWLIST,
876 &n, &nsz, &hszobj, &mszobj, &tszobj))
878 if (check_aead_encdec(k->ops->c, &f, nsz,
879 hszobj, &hsz, mszobj, &msz, tszobj, &tsz))
881 e = GAEAD_ENC(GAEADKEY_K(me), n, nsz, hsz, msz, tsz);
882 if (!e) VALERR("bad aead parameter combination");
883 rc = gaeadenc_pywrap((PyObject *)GCAEAD_ENC(me->ob_type),
884 e, f, hsz, msz, tsz);
889 static PyObject *gaekmeth_dec(PyObject *me, PyObject *arg, PyObject *kw)
891 static const char *const kwlist[] = { "nonce", "hsz", "csz", "tsz", 0 };
892 const gaead_key *k = GAEADKEY_K(me);
895 char *n; Py_ssize_t nsz;
896 PyObject *hszobj = Py_None, *cszobj = Py_None, *tszobj = Py_None;
897 size_t hsz = 0, csz = 0, tsz = 0;
900 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|OOO:dec", KWLIST,
901 &n, &nsz, &hszobj, &cszobj, &tszobj))
903 if (check_aead_encdec(k->ops->c, &f, nsz,
904 hszobj, &hsz, cszobj, &csz, tszobj, &tsz))
906 d = GAEAD_DEC(GAEADKEY_K(me), n, nsz, hsz, csz, tsz);
907 if (!d) VALERR("bad aead parameter combination");
908 rc = gaeaddec_pywrap((PyObject *)GCAEAD_DEC(me->ob_type),
909 d, f, hsz, csz, tsz);
914 static PyMethodDef gaeadkey_pymethods[] = {
915 #define METHNAME(name) gaekmeth_##name
916 METH (aad, "KEY.aad() -> AAD")
917 KWMETH(enc, "KEY.enc(NONCE, [hsz], [msz], [tsz]) -> ENC")
918 KWMETH(dec, "KEY.dec(NONCE, [hsz], [csz], [tsz]) -> DEC")
923 PyObject *gaeadaad_pywrap(PyObject *cobj, gaead_aad *a,
924 unsigned f, size_t hsz)
928 assert(cobj); Py_INCREF(cobj);
929 ga = PyObject_NEW(gaeadaad_pyobj, (PyTypeObject *)cobj);
930 ga->a = a; ga->f = f; ga->hsz = hsz; ga->hlen = 0;
931 return ((PyObject *)ga);
934 static void gaeadaad_pydealloc(PyObject *me)
936 gaeadaad_pyobj *ga = (gaeadaad_pyobj *)me;
938 if (ga->a) GAEAD_DESTROY(ga->a);
939 Py_DECREF(me->ob_type); FREEOBJ(me);
942 static int gaea_check(PyObject *me)
944 gaeadaad_pyobj *ga = (gaeadaad_pyobj *)me;
947 if ((ga->f&AEADF_DEAD) || !ga->a) VALERR("aad object no longer active");
953 static void gaea_invalidate(gaeadaad_pyobj *ga)
954 { if (ga) ga->f |= AEADF_DEAD; }
956 static void gaea_sever(gaeadaad_pyobj **ga_inout)
958 gaeadaad_pyobj *ga = *ga_inout;
959 if (ga) { ga->f |= AEADF_DEAD; ga->a = 0; Py_DECREF(ga); *ga_inout = 0; }
962 static PyObject *gaeaget_hsz(PyObject *me, void *hunoz)
964 if (gaea_check(me)) return (0);
965 else if (GAEADAAD_F(me)&AEADF_PCHSZ) return getulong(GAEADAAD_HSZ(me));
969 static PyObject *gaeaget_hlen(PyObject *me, void *hunoz)
970 { return (gaea_check(me) ? 0 : getulong(GAEADAAD_HLEN(me))); }
972 static PyGetSetDef gaeadaad_pygetset[] = {
973 #define GETSETNAME(op, name) gaea##op##_##name
974 GET (hsz, "AAD.hsz -> precommitted header length or `None'")
975 GET (hlen, "AAD.hlen -> header length so far")
980 static PyObject *gaeameth_copy(PyObject *me, PyObject *arg)
984 if (!PyArg_ParseTuple(arg, ":copy")) goto end;
985 if (gaea_check(me)) goto end;
986 if (GAEADAAD_F(me)&AEADF_AADNDEP)
987 VALERR("can't duplicate nonce-dependent aad");
988 rc = gaeadaad_pywrap((PyObject *)me->ob_type,
989 GAEAD_DUP(GAEADAAD_A(me)), 0, 0);
994 static int gaeadaad_hash(PyObject *me, const void *h, size_t hsz)
996 gaeadaad_pyobj *ga = (gaeadaad_pyobj *)me;
999 if (gaea_check(me)) goto end;
1000 if ((ga->f&AEADF_NOAAD) && hsz)
1001 VALERR("header data not permitted");
1002 if ((ga->f&AEADF_PCHSZ) && hsz > ga->hsz - ga->hlen)
1003 VALERR("too large for precommitted header length");
1004 GAEAD_HASH(ga->a, h, hsz); ga->hlen += hsz;
1011 static PyObject *gaeameth_hash(PyObject *me, PyObject *arg)
1013 char *h; Py_ssize_t hsz;
1015 if (!PyArg_ParseTuple(arg, "s#:hash", &h, &hsz)) return (0);
1016 if (gaeadaad_hash(me, h, hsz)) return (0);
1020 #define GAEAMETH_HASHU_(n, W, w) \
1021 static PyObject *gaeameth_hashu##w(PyObject *me, PyObject *arg) \
1023 uint##n x; octet b[SZ_##W]; \
1024 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \
1025 STORE##W(b, x); if (gaeadaad_hash(me, b, sizeof(b))) return (0); \
1028 DOUINTCONV(GAEAMETH_HASHU_)
1030 #define GAEAMETH_HASHBUF_(n, W, w) \
1031 static PyObject *gaeameth_hashbuf##w(PyObject *me, PyObject *arg) \
1033 char *p; Py_ssize_t sz; octet b[SZ_##W]; \
1034 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
1035 if (sz > MASK##n) TYERR("string too long"); \
1036 STORE##W(b, sz); if (gaeadaad_hash(me, b, sizeof(b))) goto end; \
1037 if (gaeadaad_hash(me, p, sz)) goto end; \
1042 DOUINTCONV(GAEAMETH_HASHBUF_)
1044 static PyObject *gaeameth_hashstrz(PyObject *me, PyObject *arg)
1047 if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0);
1048 if (gaeadaad_hash(me, p, strlen(p) + 1)) return (0);
1052 static PyMethodDef gaeadaad_pymethods[] = {
1053 #define METHNAME(name) gaeameth_##name
1054 METH (copy, "AAD.copy() -> AAD'")
1055 METH (hash, "AAD.hash(H)")
1056 #define METHU_(n, W, w) METH(hashu##w, "AAD.hashu" #w "(WORD)")
1059 #define METHBUF_(n, W, w) METH(hashbuf##w, "AAD.hashbuf" #w "(BYTES)")
1060 DOUINTCONV(METHBUF_)
1062 METH (hashstrz, "AAD.hashstrz(STRING)")
1067 PyObject *gaeadenc_pywrap(PyObject *cobj, gaead_enc *e, unsigned f,
1068 size_t hsz, size_t msz, size_t tsz)
1072 assert(cobj); Py_INCREF(cobj);
1073 ge = PyObject_NEW(gaeadenc_pyobj, (PyTypeObject *)cobj);
1074 ge->e = e; ge->f = f; ge->hsz = hsz; ge->msz = msz; ge->tsz = tsz;
1075 ge->aad = 0; ge->mlen = 0;
1076 return ((PyObject *)ge);
1079 static void gaeadenc_pydealloc(PyObject *me)
1081 gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me;
1083 gaea_sever(&ge->aad); GAEAD_DESTROY(ge->e);
1084 Py_DECREF(me->ob_type); FREEOBJ(me);
1087 static PyObject *gaeeget_hsz(PyObject *me, void *hunoz)
1089 if (GAEADENC_F(me)&AEADF_PCHSZ) return getulong(GAEADENC_HSZ(me));
1093 static PyObject *gaeeget_msz(PyObject *me, void *hunoz)
1095 if (GAEADENC_F(me)&AEADF_PCMSZ) return getulong(GAEADENC_MSZ(me));
1099 static PyObject *gaeeget_tsz(PyObject *me, void *hunoz)
1101 if (GAEADENC_F(me)&AEADF_PCTSZ) return getulong(GAEADENC_TSZ(me));
1105 static PyObject *gaeeget_mlen(PyObject *me, void *hunoz)
1106 { return getulong(GAEADENC_MLEN(me)); }
1108 static PyGetSetDef gaeadenc_pygetset[] = {
1109 #define GETSETNAME(op, name) gaee##op##_##name
1110 GET (hsz, "ENC.hsz -> precommitted header length or `None'")
1111 GET (msz, "ENC.msz -> precommitted message length or `None'")
1112 GET (tsz, "ENC.tsz -> precommitted tag length or `None'")
1113 GET (mlen, "ENC.mlen -> message length so far")
1118 static PyObject *gaeemeth_aad(PyObject *me, PyObject *arg)
1120 gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me;
1123 if (!PyArg_ParseTuple(arg, ":aad")) return (0);
1124 if (!(ge->f&AEADF_AADNDEP))
1125 rc = gaeadaad_pywrap((PyObject *)GCAEADENC_KEY(ge->ob_type)->aad,
1126 GAEAD_AAD(ge->e), 0, 0);
1128 if ((ge->f&AEADF_AADFIRST) && ge->mlen)
1129 VALERR("too late for aad");
1131 ge->aad = (gaeadaad_pyobj *)
1132 gaeadaad_pywrap((PyObject *)GCAEADENC_KEY(ge->ob_type)->aad,
1133 GAEAD_AAD(ge->e), ge->f&AEADF_PCHSZ, ge->hsz);
1135 rc = (PyObject *)ge->aad;
1141 static PyObject *gaeemeth_reinit(PyObject *me, PyObject *arg, PyObject *kw)
1143 static const char *const kwlist[] = { "nonce", "hsz", "msz", "tsz", 0 };
1144 gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me;
1145 char *n; Py_ssize_t nsz;
1146 PyObject *hszobj = Py_None, *mszobj = Py_None, *tszobj = Py_None;
1147 size_t hsz = 0, msz = 0, tsz = 0;
1150 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|OOO:enc", KWLIST,
1151 &n, &nsz, &hszobj, &mszobj, &tszobj))
1153 if (check_aead_encdec(ge->e->ops->c, &f, nsz,
1154 hszobj, &hsz, mszobj, &msz, tszobj, &tsz))
1156 if (GAEAD_REINIT(ge->e, n, nsz, hsz, msz, tsz))
1157 VALERR("bad aead parameter combination");
1158 gaea_sever(&ge->aad);
1159 ge->f = f; ge->hsz = hsz; ge->msz = msz; ge->tsz = tsz;
1164 static PyObject *gaeemeth_encrypt(PyObject *me, PyObject *arg)
1166 gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me;
1167 char *m; Py_ssize_t msz;
1168 char *c = 0; size_t csz; buf b;
1172 if (!PyArg_ParseTuple(arg, "s#:encrypt", &m, &msz)) goto end;
1173 if (ge->f&AEADF_AADFIRST) {
1174 if ((ge->f&AEADF_PCHSZ) && (ge->aad ? ge->aad->hlen : 0) != ge->hsz)
1175 VALERR("header doesn't match precommitted length");
1176 gaea_invalidate(ge->aad);
1178 if ((ge->f&AEADF_PCMSZ) && msz > ge->msz - ge->mlen)
1179 VALERR("too large for precommitted message length");
1180 csz = msz + ge->e->ops->c->bufsz; c = xmalloc(csz); buf_init(&b, c, csz);
1181 err = GAEAD_ENCRYPT(ge->e, m, msz, &b); assert(!err); (void)err;
1182 buf_flip(&b); rc = bytestring_pywrapbuf(&b); ge->mlen += msz;
1188 static PyObject *gaeemeth_done(PyObject *me, PyObject *arg, PyObject *kw)
1190 static const char *const kwlist[] = { "tsz", "aad", 0 };
1191 gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me;
1192 PyObject *aad = Py_None;
1193 char *c = 0; size_t csz; buf b;
1194 PyObject *tszobj = Py_None; PyObject *tag; size_t tsz;
1198 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|OO:done", KWLIST,
1201 if (tszobj != Py_None && !convszt(tszobj, &tsz)) goto end;
1202 if (aad != Py_None &&
1203 !PyObject_TypeCheck(aad,
1204 (PyTypeObject *)GCAEADENC_KEY(me->ob_type)->aad))
1205 TYERR("wanted aad");
1206 if ((ge->f&AEADF_AADNDEP) && aad != Py_None && aad != (PyObject *)ge->aad)
1207 VALERR("mismatched aad");
1208 if ((ge->f&AEADF_PCHSZ) &&
1209 (aad == Py_None ? 0 : GAEADAAD_HLEN(aad)) != ge->hsz)
1210 VALERR("header doesn't match precommitted length");
1211 if ((ge->f&AEADF_PCMSZ) && ge->mlen != ge->msz)
1212 VALERR("message doesn't match precommitted length");
1213 if (tszobj == Py_None) {
1214 if (ge->f&AEADF_PCTSZ) tsz = ge->tsz;
1215 else tsz = keysz(0, ge->e->ops->c->tagsz);
1217 if ((ge->f&AEADF_PCTSZ) && tsz != ge->tsz)
1218 VALERR("tag length doesn't match precommitted value");
1219 if (keysz(tsz, ge->e->ops->c->tagsz) != tsz) VALERR("bad tag length");
1221 csz = ge->e->ops->c->bufsz; c = xmalloc(csz); buf_init(&b, c, csz);
1222 tag = bytestring_pywrap(0, tsz);
1223 err = GAEAD_DONE(ge->e, aad == Py_None ? 0 : GAEADAAD_A(aad), &b,
1224 PyString_AS_STRING(tag), tsz);
1225 assert(!err); (void)err;
1226 buf_flip(&b); rc = Py_BuildValue("NN", bytestring_pywrapbuf(&b), tag);
1232 static PyMethodDef gaeadenc_pymethods[] = {
1233 #define METHNAME(name) gaeemeth_##name
1234 METH (aad, "ENC.aad() -> AAD")
1235 KWMETH(reinit, "ENC.reinit(NONCE, [hsz], [msz], [tsz])")
1236 METH (encrypt, "ENC.encrypt(MSG) -> CT")
1237 KWMETH(done, "ENC.done([tsz], [aad]) -> CT, TAG")
1242 PyObject *gaeaddec_pywrap(PyObject *cobj, gaead_dec *d, unsigned f,
1243 size_t hsz, size_t csz, size_t tsz)
1246 assert(cobj); Py_INCREF(cobj);
1247 gd = PyObject_NEW(gaeaddec_pyobj, (PyTypeObject *)cobj);
1248 gd->d = d; gd->f = f; gd->hsz = hsz; gd->csz = csz; gd->tsz = tsz;
1249 gd->aad = 0; gd->clen = 0;
1250 return ((PyObject *)gd);
1253 static void gaeaddec_pydealloc(PyObject *me)
1255 gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me;
1257 gaea_sever(&gd->aad); GAEAD_DESTROY(GAEADDEC_D(me));
1258 Py_DECREF(me->ob_type); FREEOBJ(me);
1261 static PyObject *gaedget_hsz(PyObject *me, void *hunoz)
1263 if (GAEADDEC_F(me)&AEADF_PCHSZ) return getulong(GAEADDEC_HSZ(me));
1267 static PyObject *gaedget_csz(PyObject *me, void *hunoz)
1269 if (GAEADDEC_F(me)&AEADF_PCMSZ) return getulong(GAEADDEC_CSZ(me));
1273 static PyObject *gaedget_tsz(PyObject *me, void *hunoz)
1275 if (GAEADDEC_F(me)&AEADF_PCTSZ) return getulong(GAEADDEC_TSZ(me));
1279 static PyObject *gaedget_clen(PyObject *me, void *hunoz)
1280 { return getulong(GAEADDEC_CLEN(me)); }
1282 static PyGetSetDef gaeaddec_pygetset[] = {
1283 #define GETSETNAME(op, name) gaed##op##_##name
1284 GET (hsz, "DEC.hsz -> precommitted header length or `None'")
1285 GET (csz, "DEC.csz -> precommitted ciphertext length or `None'")
1286 GET (tsz, "DEC.tsz -> precommitted tag length or `None'")
1287 GET (clen, "DEC.clen -> ciphertext length so far")
1292 static PyObject *gaedmeth_aad(PyObject *me, PyObject *arg)
1294 gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me;
1296 if (!PyArg_ParseTuple(arg, ":aad")) return (0);
1297 if (!(gd->f&AEADF_AADNDEP))
1298 return (gaeadaad_pywrap((PyObject *)GCAEADDEC_KEY(gd->ob_type)->aad,
1299 GAEAD_AAD(gd->d), 0, 0));
1302 gd->aad = (gaeadaad_pyobj *)
1303 gaeadaad_pywrap((PyObject *)GCAEADENC_KEY(gd->ob_type)->aad,
1304 GAEAD_AAD(gd->d), gd->f&AEADF_PCHSZ, gd->hsz);
1306 return ((PyObject *)gd->aad);
1310 static PyObject *gaedmeth_reinit(PyObject *me, PyObject *arg, PyObject *kw)
1312 static const char *const kwlist[] = { "nonce", "hsz", "csz", "tsz", 0 };
1313 gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me;
1314 char *n; Py_ssize_t nsz;
1315 PyObject *hszobj = Py_None, *cszobj = Py_None, *tszobj = Py_None;
1316 size_t hsz = 0, csz = 0, tsz = 0;
1319 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|OOO:enc", KWLIST,
1320 &n, &nsz, &hszobj, &cszobj, &tszobj))
1322 if (check_aead_encdec(gd->d->ops->c, &f, nsz,
1323 hszobj, &hsz, cszobj, &csz, tszobj, &tsz))
1325 if (GAEAD_REINIT(gd->d, n, nsz, hsz, csz, tsz))
1326 VALERR("bad aead parameter combination");
1327 gaea_sever(&gd->aad);
1328 gd->f = f; gd->hsz = hsz; gd->csz = csz; gd->tsz = tsz;
1333 static PyObject *gaedmeth_decrypt(PyObject *me, PyObject *arg)
1335 gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me;
1336 char *c; Py_ssize_t csz;
1337 char *m = 0; size_t msz; buf b;
1341 if (!PyArg_ParseTuple(arg, "s#:decrypt", &c, &csz)) goto end;
1342 if (gd->f&AEADF_AADFIRST) {
1343 if ((gd->f&AEADF_PCHSZ) && (gd->aad ? gd->aad->hlen : 0) != gd->hsz)
1344 VALERR("header doesn't match precommitted length");
1345 gaea_invalidate(gd->aad);
1347 if ((gd->f&AEADF_PCMSZ) && csz > gd->csz - gd->clen)
1348 VALERR("too large for precommitted message length");
1349 msz = csz + gd->d->ops->c->bufsz; m = xmalloc(msz); buf_init(&b, m, msz);
1350 err = GAEAD_DECRYPT(gd->d, c, csz, &b); assert(!err); (void)err;
1351 buf_flip(&b); rc = bytestring_pywrapbuf(&b); gd->clen += csz;
1357 static PyObject *gaedmeth_done(PyObject *me, PyObject *arg, PyObject *kw)
1359 static const char *const kwlist[] = { "tag", "aad", 0 };
1360 gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me;
1361 PyObject *aad = Py_None;
1362 char *t; Py_ssize_t tsz;
1363 char *m = 0; size_t msz; buf b;
1367 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|O:done", KWLIST,
1370 if (aad != Py_None &&
1371 !PyObject_TypeCheck(aad,
1372 (PyTypeObject *)GCAEADENC_KEY(me->ob_type)->aad))
1373 TYERR("wanted aad");
1374 if ((gd->f&AEADF_AADNDEP) && aad != Py_None && aad != (PyObject *)gd->aad)
1375 VALERR("mismatched aad");
1376 if ((gd->f&AEADF_PCHSZ) &&
1377 (aad == Py_None ? 0 : GAEADAAD_HLEN(aad)) != gd->hsz)
1378 VALERR("header doesn't match precommitted length");
1379 if ((gd->f&AEADF_PCMSZ) && gd->clen != gd->csz)
1380 VALERR("message doesn't match precommitted length");
1381 if ((gd->f&AEADF_PCTSZ) && tsz != gd->tsz)
1382 VALERR("tag length doesn't match precommitted value");
1383 if (keysz(tsz, gd->d->ops->c->tagsz) != tsz) VALERR("bad tag length");
1384 msz = gd->d->ops->c->bufsz; m = xmalloc(msz); buf_init(&b, m, msz);
1385 err = GAEAD_DONE(gd->d, aad == Py_None ? 0 : GAEADAAD_A(aad), &b, t, tsz);
1387 if (!err) VALERR("decryption failed");
1388 buf_flip(&b); rc = bytestring_pywrapbuf(&b);
1394 static PyMethodDef gaeaddec_pymethods[] = {
1395 #define METHNAME(name) gaedmeth_##name
1396 METH (aad, "DEC.aad() -> AAD")
1397 KWMETH(reinit, "DEC.reinit(NONCE, [hsz], [csz], [tsz])")
1398 METH (decrypt, "DEC.decrypt(CT) -> MSG")
1399 KWMETH(done, "DEC.done(TAG, [aad]) -> MSG | None")
1404 static PyTypeObject gcaead_pytype_skel = {
1405 PyObject_HEAD_INIT(0) 0, /* Header */
1406 "GCAEAD", /* @tp_name@ */
1407 sizeof(gcaead_pyobj), /* @tp_basicsize@ */
1408 0, /* @tp_itemsize@ */
1410 0, /* @tp_dealloc@ */
1412 0, /* @tp_getattr@ */
1413 0, /* @tp_setattr@ */
1414 0, /* @tp_compare@ */
1416 0, /* @tp_as_number@ */
1417 0, /* @tp_as_sequence@ */
1418 0, /* @tp_as_mapping@ */
1422 0, /* @tp_getattro@ */
1423 0, /* @tp_setattro@ */
1424 0, /* @tp_as_buffer@ */
1425 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1426 Py_TPFLAGS_BASETYPE,
1429 "Authenticated encryption (key) metaclass.",
1431 0, /* @tp_traverse@ */
1433 0, /* @tp_richcompare@ */
1434 0, /* @tp_weaklistoffset@ */
1436 0, /* @tp_iternext@ */
1437 0, /* @tp_methods@ */
1438 0, /* @tp_members@ */
1439 gcaead_pygetset, /* @tp_getset@ */
1442 0, /* @tp_descr_get@ */
1443 0, /* @tp_descr_set@ */
1444 0, /* @tp_dictoffset@ */
1446 PyType_GenericAlloc, /* @tp_alloc@ */
1447 abstract_pynew, /* @tp_new@ */
1452 static PyTypeObject gaeadkey_pytype_skel = {
1453 PyObject_HEAD_INIT(0) 0, /* Header */
1454 "GAEKey", /* @tp_name@ */
1455 sizeof(gaeadkey_pyobj), /* @tp_basicsize@ */
1456 0, /* @tp_itemsize@ */
1458 gaeadkey_pydealloc, /* @tp_dealloc@ */
1460 0, /* @tp_getattr@ */
1461 0, /* @tp_setattr@ */
1462 0, /* @tp_compare@ */
1464 0, /* @tp_as_number@ */
1465 0, /* @tp_as_sequence@ */
1466 0, /* @tp_as_mapping@ */
1470 0, /* @tp_getattro@ */
1471 0, /* @tp_setattro@ */
1472 0, /* @tp_as_buffer@ */
1473 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1474 Py_TPFLAGS_BASETYPE,
1477 "Authenticated encryption key.",
1479 0, /* @tp_traverse@ */
1481 0, /* @tp_richcompare@ */
1482 0, /* @tp_weaklistoffset@ */
1484 0, /* @tp_iternext@ */
1485 gaeadkey_pymethods, /* @tp_methods@ */
1486 0, /* @tp_members@ */
1487 0, /* @tp_getset@ */
1490 0, /* @tp_descr_get@ */
1491 0, /* @tp_descr_set@ */
1492 0, /* @tp_dictoffset@ */
1494 PyType_GenericAlloc, /* @tp_alloc@ */
1495 abstract_pynew, /* @tp_new@ */
1500 static PyTypeObject gcaeadaad_pytype_skel = {
1501 PyObject_HEAD_INIT(0) 0, /* Header */
1502 "GAEAADClass", /* @tp_name@ */
1503 sizeof(gcaeadaad_pyobj), /* @tp_basicsize@ */
1504 0, /* @tp_itemsize@ */
1506 0, /* @tp_dealloc@ */
1508 0, /* @tp_getattr@ */
1509 0, /* @tp_setattr@ */
1510 0, /* @tp_compare@ */
1512 0, /* @tp_as_number@ */
1513 0, /* @tp_as_sequence@ */
1514 0, /* @tp_as_mapping@ */
1518 0, /* @tp_getattro@ */
1519 0, /* @tp_setattro@ */
1520 0, /* @tp_as_buffer@ */
1521 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1522 Py_TPFLAGS_BASETYPE,
1525 "Authenticated encryption additional-data hash metaclass.",
1527 0, /* @tp_traverse@ */
1529 0, /* @tp_richcompare@ */
1530 0, /* @tp_weaklistoffset@ */
1532 0, /* @tp_iternext@ */
1533 0, /* @tp_methods@ */
1534 0, /* @tp_members@ */
1535 0, /* @tp_getset@ */
1538 0, /* @tp_descr_get@ */
1539 0, /* @tp_descr_set@ */
1540 0, /* @tp_dictoffset@ */
1542 PyType_GenericAlloc, /* @tp_alloc@ */
1543 abstract_pynew, /* @tp_new@ */
1548 static PyTypeObject gaeadaad_pytype_skel = {
1549 PyObject_HEAD_INIT(0) 0, /* Header */
1550 "GAEAAD", /* @tp_name@ */
1551 sizeof(gaeadaad_pyobj), /* @tp_basicsize@ */
1552 0, /* @tp_itemsize@ */
1554 gaeadaad_pydealloc, /* @tp_dealloc@ */
1556 0, /* @tp_getattr@ */
1557 0, /* @tp_setattr@ */
1558 0, /* @tp_compare@ */
1560 0, /* @tp_as_number@ */
1561 0, /* @tp_as_sequence@ */
1562 0, /* @tp_as_mapping@ */
1566 0, /* @tp_getattro@ */
1567 0, /* @tp_setattro@ */
1568 0, /* @tp_as_buffer@ */
1569 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1570 Py_TPFLAGS_BASETYPE,
1573 "Authenticated encryption AAD hash.",
1575 0, /* @tp_traverse@ */
1577 0, /* @tp_richcompare@ */
1578 0, /* @tp_weaklistoffset@ */
1580 0, /* @tp_iternext@ */
1581 gaeadaad_pymethods, /* @tp_methods@ */
1582 0, /* @tp_members@ */
1583 gaeadaad_pygetset, /* @tp_getset@ */
1586 0, /* @tp_descr_get@ */
1587 0, /* @tp_descr_set@ */
1588 0, /* @tp_dictoffset@ */
1590 PyType_GenericAlloc, /* @tp_alloc@ */
1591 abstract_pynew, /* @tp_new@ */
1596 static PyTypeObject gcaeadenc_pytype_skel = {
1597 PyObject_HEAD_INIT(0) 0, /* Header */
1598 "GAEEncClass", /* @tp_name@ */
1599 sizeof(gcaeadenc_pyobj), /* @tp_basicsize@ */
1600 0, /* @tp_itemsize@ */
1602 0, /* @tp_dealloc@ */
1604 0, /* @tp_getattr@ */
1605 0, /* @tp_setattr@ */
1606 0, /* @tp_compare@ */
1608 0, /* @tp_as_number@ */
1609 0, /* @tp_as_sequence@ */
1610 0, /* @tp_as_mapping@ */
1614 0, /* @tp_getattro@ */
1615 0, /* @tp_setattro@ */
1616 0, /* @tp_as_buffer@ */
1617 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1618 Py_TPFLAGS_BASETYPE,
1621 "Authenticated encryption operation metaclass.",
1623 0, /* @tp_traverse@ */
1625 0, /* @tp_richcompare@ */
1626 0, /* @tp_weaklistoffset@ */
1628 0, /* @tp_iternext@ */
1629 0, /* @tp_methods@ */
1630 0, /* @tp_members@ */
1631 0, /* @tp_getset@ */
1634 0, /* @tp_descr_get@ */
1635 0, /* @tp_descr_set@ */
1636 0, /* @tp_dictoffset@ */
1638 PyType_GenericAlloc, /* @tp_alloc@ */
1639 abstract_pynew, /* @tp_new@ */
1644 static PyTypeObject gaeadenc_pytype_skel = {
1645 PyObject_HEAD_INIT(0) 0, /* Header */
1646 "GAEEnc", /* @tp_name@ */
1647 sizeof(gaeadenc_pyobj), /* @tp_basicsize@ */
1648 0, /* @tp_itemsize@ */
1650 gaeadenc_pydealloc, /* @tp_dealloc@ */
1652 0, /* @tp_getattr@ */
1653 0, /* @tp_setattr@ */
1654 0, /* @tp_compare@ */
1656 0, /* @tp_as_number@ */
1657 0, /* @tp_as_sequence@ */
1658 0, /* @tp_as_mapping@ */
1662 0, /* @tp_getattro@ */
1663 0, /* @tp_setattro@ */
1664 0, /* @tp_as_buffer@ */
1665 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1666 Py_TPFLAGS_BASETYPE,
1669 "Authenticated encryption operation.",
1671 0, /* @tp_traverse@ */
1673 0, /* @tp_richcompare@ */
1674 0, /* @tp_weaklistoffset@ */
1676 0, /* @tp_iternext@ */
1677 gaeadenc_pymethods, /* @tp_methods@ */
1678 0, /* @tp_members@ */
1679 gaeadenc_pygetset, /* @tp_getset@ */
1682 0, /* @tp_descr_get@ */
1683 0, /* @tp_descr_set@ */
1684 0, /* @tp_dictoffset@ */
1686 PyType_GenericAlloc, /* @tp_alloc@ */
1687 abstract_pynew, /* @tp_new@ */
1692 static PyTypeObject gcaeaddec_pytype_skel = {
1693 PyObject_HEAD_INIT(0) 0, /* Header */
1694 "GAEDecClass", /* @tp_name@ */
1695 sizeof(gcaeaddec_pyobj), /* @tp_basicsize@ */
1696 0, /* @tp_itemsize@ */
1698 0, /* @tp_dealloc@ */
1700 0, /* @tp_getattr@ */
1701 0, /* @tp_setattr@ */
1702 0, /* @tp_compare@ */
1704 0, /* @tp_as_number@ */
1705 0, /* @tp_as_sequence@ */
1706 0, /* @tp_as_mapping@ */
1710 0, /* @tp_getattro@ */
1711 0, /* @tp_setattro@ */
1712 0, /* @tp_as_buffer@ */
1713 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1714 Py_TPFLAGS_BASETYPE,
1717 "Authenticated decryption operation metaclass.",
1719 0, /* @tp_traverse@ */
1721 0, /* @tp_richcompare@ */
1722 0, /* @tp_weaklistoffset@ */
1724 0, /* @tp_iternext@ */
1725 0, /* @tp_methods@ */
1726 0, /* @tp_members@ */
1727 0, /* @tp_getset@ */
1730 0, /* @tp_descr_get@ */
1731 0, /* @tp_descr_set@ */
1732 0, /* @tp_dictoffset@ */
1734 PyType_GenericAlloc, /* @tp_alloc@ */
1735 abstract_pynew, /* @tp_new@ */
1740 static PyTypeObject gaeaddec_pytype_skel = {
1741 PyObject_HEAD_INIT(0) 0, /* Header */
1742 "GAEDec", /* @tp_name@ */
1743 sizeof(gaeaddec_pyobj), /* @tp_basicsize@ */
1744 0, /* @tp_itemsize@ */
1746 gaeaddec_pydealloc, /* @tp_dealloc@ */
1748 0, /* @tp_getattr@ */
1749 0, /* @tp_setattr@ */
1750 0, /* @tp_compare@ */
1752 0, /* @tp_as_number@ */
1753 0, /* @tp_as_sequence@ */
1754 0, /* @tp_as_mapping@ */
1758 0, /* @tp_getattro@ */
1759 0, /* @tp_setattro@ */
1760 0, /* @tp_as_buffer@ */
1761 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1762 Py_TPFLAGS_BASETYPE,
1765 "Authenticated decryption operation.",
1767 0, /* @tp_traverse@ */
1769 0, /* @tp_richcompare@ */
1770 0, /* @tp_weaklistoffset@ */
1772 0, /* @tp_iternext@ */
1773 gaeaddec_pymethods, /* @tp_methods@ */
1774 0, /* @tp_members@ */
1775 gaeaddec_pygetset, /* @tp_getset@ */
1778 0, /* @tp_descr_get@ */
1779 0, /* @tp_descr_set@ */
1780 0, /* @tp_dictoffset@ */
1782 PyType_GenericAlloc, /* @tp_alloc@ */
1783 abstract_pynew, /* @tp_new@ */
1788 /*----- Hash functions ----------------------------------------------------*/
1790 PyTypeObject *gchash_pytype, *ghash_pytype;
1792 CONVFUNC(gchash, gchash *, GCHASH_CH)
1793 CONVFUNC(ghash, ghash *, GHASH_H)
1795 static PyObject *ghash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1797 static const char *const kwlist[] = { 0 };
1798 if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", KWLIST))
1800 return (ghash_pywrap((PyObject *)ty, GH_INIT(GCHASH_CH(ty))));
1805 PyObject *gchash_pywrap(gchash *ch)
1807 gchash_pyobj *g = newtype(gchash_pytype, 0, ch->name);
1809 g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj);
1810 g->ty.ht_type.tp_base = ghash_pytype;
1811 Py_INCREF(ghash_pytype);
1812 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
1813 Py_TPFLAGS_BASETYPE |
1814 Py_TPFLAGS_HEAPTYPE);
1815 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
1816 g->ty.ht_type.tp_free = 0;
1817 g->ty.ht_type.tp_new = ghash_pynew;
1818 typeready(&g->ty.ht_type);
1819 return ((PyObject *)g);
1822 PyObject *ghash_pywrap(PyObject *cobj, ghash *h)
1825 if (!cobj) cobj = gchash_pywrap((/*unconst*/ gchash *)GH_CLASS(h));
1826 else Py_INCREF(cobj);
1827 g = PyObject_NEW(ghash_pyobj, (PyTypeObject *)cobj);
1829 return ((PyObject *)g);
1832 static void ghash_pydealloc(PyObject *me)
1834 GH_DESTROY(GHASH_H(me));
1835 Py_DECREF(me->ob_type);
1839 static PyObject *gchget_name(PyObject *me, void *hunoz)
1840 { return (PyString_FromString(GCHASH_CH(me)->name)); }
1842 static PyObject *gchget_hashsz(PyObject *me, void *hunoz)
1843 { return (PyInt_FromLong(GCHASH_CH(me)->hashsz)); }
1845 static PyObject *gchget_bufsz(PyObject *me, void *hunoz)
1846 { return (PyInt_FromLong(GCHASH_CH(me)->bufsz)); }
1848 static PyObject *ghmeth_copy(PyObject *me, PyObject *arg)
1850 if (!PyArg_ParseTuple(arg, ":copy")) return (0);
1851 return (ghash_pywrap((PyObject *)me->ob_type, GH_COPY(GHASH_H(me))));
1854 static PyObject *ghmeth_hash(PyObject *me, PyObject *arg)
1858 if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0);
1859 GH_HASH(GHASH_H(me), p, sz);
1863 #define GHMETH_HASHU_(n, W, w) \
1864 static PyObject *ghmeth_hashu##w(PyObject *me, PyObject *arg) \
1867 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \
1868 GH_HASHU##W(GHASH_H(me), x); \
1871 DOUINTCONV(GHMETH_HASHU_)
1873 #define GHMETH_HASHBUF_(n, W, w) \
1874 static PyObject *ghmeth_hashbuf##w(PyObject *me, PyObject *arg) \
1878 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
1879 if (sz > MASK##n) TYERR("string too long"); \
1880 GH_HASHBUF##W(GHASH_H(me), p, sz); \
1885 DOUINTCONV(GHMETH_HASHBUF_)
1887 static PyObject *ghmeth_hashstrz(PyObject *me, PyObject *arg)
1890 if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0);
1891 GH_HASHSTRZ(GHASH_H(me), p);
1895 static PyObject *ghmeth_done(PyObject *me, PyObject *arg)
1899 if (!PyArg_ParseTuple(arg, ":done")) return (0);
1900 g = GH_COPY(GHASH_H(me));
1901 rc = bytestring_pywrap(0, g->ops->c->hashsz);
1902 GH_DONE(g, PyString_AS_STRING(rc));
1907 static PyGetSetDef gchash_pygetset[] = {
1908 #define GETSETNAME(op, name) gch##op##_##name
1909 GET (bufsz, "CH.bufsz -> hash buffer size, or zero")
1910 GET (hashsz, "CH.hashsz -> hash output size")
1911 GET (name, "CH.name -> name of this kind of hash")
1916 static PyMethodDef ghash_pymethods[] = {
1917 #define METHNAME(name) ghmeth_##name
1918 METH (copy, "H.copy() -> HH")
1919 METH (hash, "H.hash(M)")
1920 #define METHU_(n, W, w) METH(hashu##w, "H.hashu" #w "(WORD)")
1923 #define METHBUF_(n, W, w) METH(hashbuf##w, "H.hashbuf" #w "(BYTES)")
1924 DOUINTCONV(METHBUF_)
1926 METH (hashstrz, "H.hashstrz(STRING)")
1927 METH (done, "H.done() -> HASH")
1932 static PyTypeObject gchash_pytype_skel = {
1933 PyObject_HEAD_INIT(0) 0, /* Header */
1934 "GCHash", /* @tp_name@ */
1935 sizeof(gchash_pyobj), /* @tp_basicsize@ */
1936 0, /* @tp_itemsize@ */
1938 0, /* @tp_dealloc@ */
1940 0, /* @tp_getattr@ */
1941 0, /* @tp_setattr@ */
1942 0, /* @tp_compare@ */
1944 0, /* @tp_as_number@ */
1945 0, /* @tp_as_sequence@ */
1946 0, /* @tp_as_mapping@ */
1950 0, /* @tp_getattro@ */
1951 0, /* @tp_setattro@ */
1952 0, /* @tp_as_buffer@ */
1953 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1954 Py_TPFLAGS_BASETYPE,
1957 "Hash function metaclass.",
1959 0, /* @tp_traverse@ */
1961 0, /* @tp_richcompare@ */
1962 0, /* @tp_weaklistoffset@ */
1964 0, /* @tp_iternext@ */
1965 0, /* @tp_methods@ */
1966 0, /* @tp_members@ */
1967 gchash_pygetset, /* @tp_getset@ */
1970 0, /* @tp_descr_get@ */
1971 0, /* @tp_descr_set@ */
1972 0, /* @tp_dictoffset@ */
1974 PyType_GenericAlloc, /* @tp_alloc@ */
1975 abstract_pynew, /* @tp_new@ */
1980 static PyTypeObject ghash_pytype_skel = {
1981 PyObject_HEAD_INIT(0) 0, /* Header */
1982 "GHash", /* @tp_name@ */
1983 sizeof(ghash_pyobj), /* @tp_basicsize@ */
1984 0, /* @tp_itemsize@ */
1986 ghash_pydealloc, /* @tp_dealloc@ */
1988 0, /* @tp_getattr@ */
1989 0, /* @tp_setattr@ */
1990 0, /* @tp_compare@ */
1992 0, /* @tp_as_number@ */
1993 0, /* @tp_as_sequence@ */
1994 0, /* @tp_as_mapping@ */
1998 0, /* @tp_getattro@ */
1999 0, /* @tp_setattro@ */
2000 0, /* @tp_as_buffer@ */
2001 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2002 Py_TPFLAGS_BASETYPE,
2005 "Hash function, abstract base class.",
2007 0, /* @tp_traverse@ */
2009 0, /* @tp_richcompare@ */
2010 0, /* @tp_weaklistoffset@ */
2012 0, /* @tp_iternext@ */
2013 ghash_pymethods, /* @tp_methods@ */
2014 0, /* @tp_members@ */
2015 0, /* @tp_getset@ */
2018 0, /* @tp_descr_get@ */
2019 0, /* @tp_descr_set@ */
2020 0, /* @tp_dictoffset@ */
2022 PyType_GenericAlloc, /* @tp_alloc@ */
2023 abstract_pynew, /* @tp_new@ */
2028 /*----- Message authentication --------------------------------------------*/
2030 PyTypeObject *gcmac_pytype, *gmac_pytype, *gmhash_pytype;
2032 CONVFUNC(gcmac, gcmac *, GCMAC_CM)
2033 CONVFUNC(gmac, gmac *, GMAC_M)
2034 CONVFUNC(gmhash, ghash *, GHASH_H)
2036 static PyObject *gmac_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
2038 static const char *const kwlist[] = { "k", 0 };
2042 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &k, &sz))
2044 if (keysz(sz, GCMAC_CM(ty)->keysz) != sz) VALERR("bad key length");
2045 return (gmac_pywrap((PyObject *)ty,
2046 GM_KEY(GCMAC_CM(ty), k, sz)));
2051 static PyObject *gmhash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
2053 static const char *const kwlist[] = { 0 };
2056 if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", KWLIST)) return (0);
2057 g = PyObject_NEW(ghash_pyobj, ty);
2058 g->h = GM_INIT(GMAC_M(ty));
2060 return ((PyObject *)g);
2063 PyObject *gcmac_pywrap(gcmac *cm)
2065 gcmac_pyobj *g = newtype(gcmac_pytype, 0, cm->name);
2067 g->ty.ht_type.tp_basicsize = sizeof(gmac_pyobj);
2068 g->ty.ht_type.tp_base = gmac_pytype;
2069 Py_INCREF(gmac_pytype);
2070 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
2071 Py_TPFLAGS_BASETYPE |
2072 Py_TPFLAGS_HEAPTYPE);
2073 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
2074 g->ty.ht_type.tp_free = 0;
2075 g->ty.ht_type.tp_new = gmac_pynew;
2076 typeready(&g->ty.ht_type);
2077 return ((PyObject *)g);
2080 PyObject *gmac_pywrap(PyObject *cobj, gmac *m)
2083 if (!cobj) cobj = gcmac_pywrap((/*unconst*/ gcmac *)GM_CLASS(m));
2084 else Py_INCREF(cobj);
2085 g = newtype((PyTypeObject *)cobj, 0, 0);
2086 g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj);
2087 g->ty.ht_name = PyString_FromFormat("%s(keyed)", m->ops->c->name);
2088 g->ty.ht_type.tp_name = PyString_AS_STRING(g->ty.ht_name);
2089 g->ty.ht_type.tp_base = gmhash_pytype;
2090 Py_INCREF(gmac_pytype);
2091 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
2092 Py_TPFLAGS_BASETYPE |
2093 Py_TPFLAGS_HEAPTYPE);
2094 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
2095 g->ty.ht_type.tp_free = 0;
2096 g->ty.ht_type.tp_new = gmhash_pynew;
2097 typeready(&g->ty.ht_type);
2099 return ((PyObject *)g);
2102 static void gmac_pydealloc(PyObject *me)
2104 GM_DESTROY(GMAC_M(me));
2105 Py_DECREF(me->ob_type);
2106 PyType_Type.tp_dealloc(me);
2109 static PyObject *gcmget_name(PyObject *me, void *hunoz)
2110 { return (PyString_FromString(GCMAC_CM(me)->name)); }
2112 static PyObject *gcmget_keysz(PyObject *me, void *hunoz)
2113 { return (keysz_pywrap(GCMAC_CM(me)->keysz)); }
2115 static PyObject *gcmget_tagsz(PyObject *me, void *hunoz)
2116 { return (PyInt_FromLong(GCMAC_CM(me)->hashsz)); }
2118 static PyGetSetDef gcmac_pygetset[] = {
2119 #define GETSETNAME(op, name) gcm##op##_##name
2120 GET (keysz, "CM.keysz -> acceptable key sizes")
2121 GET (tagsz, "CM.tagsz -> MAC output size")
2122 GET (name, "CM.name -> name of this kind of MAC")
2127 static PyTypeObject gcmac_pytype_skel = {
2128 PyObject_HEAD_INIT(0) 0, /* Header */
2129 "GCMAC", /* @tp_name@ */
2130 sizeof(gchash_pyobj), /* @tp_basicsize@ */
2131 0, /* @tp_itemsize@ */
2133 0, /* @tp_dealloc@ */
2135 0, /* @tp_getattr@ */
2136 0, /* @tp_setattr@ */
2137 0, /* @tp_compare@ */
2139 0, /* @tp_as_number@ */
2140 0, /* @tp_as_sequence@ */
2141 0, /* @tp_as_mapping@ */
2145 0, /* @tp_getattro@ */
2146 0, /* @tp_setattro@ */
2147 0, /* @tp_as_buffer@ */
2148 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2149 Py_TPFLAGS_BASETYPE,
2152 "Message authentication code metametaclass.",
2154 0, /* @tp_traverse@ */
2156 0, /* @tp_richcompare@ */
2157 0, /* @tp_weaklistoffset@ */
2159 0, /* @tp_iternext@ */
2160 0, /* @tp_methods@ */
2161 0, /* @tp_members@ */
2162 gcmac_pygetset, /* @tp_getset@ */
2165 0, /* @tp_descr_get@ */
2166 0, /* @tp_descr_set@ */
2167 0, /* @tp_dictoffset@ */
2169 PyType_GenericAlloc, /* @tp_alloc@ */
2170 abstract_pynew, /* @tp_new@ */
2175 static PyTypeObject gmac_pytype_skel = {
2176 PyObject_HEAD_INIT(0) 0, /* Header */
2177 "GMAC", /* @tp_name@ */
2178 sizeof(gmac_pyobj), /* @tp_basicsize@ */
2179 0, /* @tp_itemsize@ */
2181 gmac_pydealloc, /* @tp_dealloc@ */
2183 0, /* @tp_getattr@ */
2184 0, /* @tp_setattr@ */
2185 0, /* @tp_compare@ */
2187 0, /* @tp_as_number@ */
2188 0, /* @tp_as_sequence@ */
2189 0, /* @tp_as_mapping@ */
2193 0, /* @tp_getattro@ */
2194 0, /* @tp_setattro@ */
2195 0, /* @tp_as_buffer@ */
2196 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2197 Py_TPFLAGS_BASETYPE,
2200 "Message authentication code metaclass, abstract base class.",
2202 0, /* @tp_traverse@ */
2204 0, /* @tp_richcompare@ */
2205 0, /* @tp_weaklistoffset@ */
2207 0, /* @tp_iternext@ */
2208 0, /* @tp_methods@ */
2209 0, /* @tp_members@ */
2210 0, /* @tp_getset@ */
2213 0, /* @tp_descr_get@ */
2214 0, /* @tp_descr_set@ */
2215 0, /* @tp_dictoffset@ */
2217 PyType_GenericAlloc, /* @tp_alloc@ */
2218 abstract_pynew, /* @tp_new@ */
2223 static PyTypeObject gmhash_pytype_skel = {
2224 PyObject_HEAD_INIT(0) 0, /* Header */
2225 "GMACHash", /* @tp_name@ */
2226 sizeof(ghash_pyobj), /* @tp_basicsize@ */
2227 0, /* @tp_itemsize@ */
2229 ghash_pydealloc, /* @tp_dealloc@ */
2231 0, /* @tp_getattr@ */
2232 0, /* @tp_setattr@ */
2233 0, /* @tp_compare@ */
2235 0, /* @tp_as_number@ */
2236 0, /* @tp_as_sequence@ */
2237 0, /* @tp_as_mapping@ */
2241 0, /* @tp_getattro@ */
2242 0, /* @tp_setattro@ */
2243 0, /* @tp_as_buffer@ */
2244 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2245 Py_TPFLAGS_BASETYPE,
2248 "Message authentication code, abstract base class.",
2250 0, /* @tp_traverse@ */
2252 0, /* @tp_richcompare@ */
2253 0, /* @tp_weaklistoffset@ */
2255 0, /* @tp_iternext@ */
2256 0, /* @tp_methods@ */
2257 0, /* @tp_members@ */
2258 0, /* @tp_getset@ */
2261 0, /* @tp_descr_get@ */
2262 0, /* @tp_descr_set@ */
2263 0, /* @tp_dictoffset@ */
2265 PyType_GenericAlloc, /* @tp_alloc@ */
2266 abstract_pynew, /* @tp_new@ */
2271 /*----- Special snowflake for Poly1305 ------------------------------------*/
2273 PyTypeObject *poly1305cls_pytype, *poly1305key_pytype, *poly1305hash_pytype;
2275 typedef struct poly1305key_pyobj {
2276 PyHeapTypeObject ty;
2278 } poly1305key_pyobj;
2280 typedef struct poly1305hash_pyobj {
2285 } poly1305hash_pyobj;
2287 #define P1305_F(o) (((poly1305hash_pyobj *)(o))->f)
2288 #define P1305_CTX(o) (&((poly1305hash_pyobj *)(o))->ctx)
2289 CONVFUNC(poly1305hash, poly1305_ctx *, P1305_CTX)
2291 static PyObject *poly1305hash_pynew(PyTypeObject *ty,
2292 PyObject *arg, PyObject *kw)
2294 static const char *const kwlist[] = { "mask", 0 };
2295 poly1305key_pyobj *pk = (poly1305key_pyobj *)ty;
2296 poly1305hash_pyobj *ph;
2300 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|s#:new", KWLIST, &m, &sz))
2302 if (m && sz != POLY1305_MASKSZ) VALERR("bad mask length");
2303 ph = PyObject_NEW(poly1305hash_pyobj, ty);
2305 if (m) ph->f |= f_mask;
2306 poly1305_macinit(&ph->ctx, &pk->k, m);
2308 return ((PyObject *)ph);
2313 static PyObject *poly1305key_pynew(PyTypeObject *ty,
2314 PyObject *arg, PyObject *kw)
2316 static const char *const kwlist[] = { "k", 0 };
2317 poly1305key_pyobj *pk;
2321 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &k, &sz))
2323 if (keysz(sz, poly1305_keysz) != sz) VALERR("bad key length");
2325 pk = newtype(ty, 0, 0);
2326 pk->ty.ht_name = PyString_FromString("poly1305(keyed)");
2327 pk->ty.ht_type.tp_basicsize = sizeof(poly1305hash_pyobj);
2328 pk->ty.ht_type.tp_name = PyString_AS_STRING(pk->ty.ht_name);
2329 pk->ty.ht_type.tp_base = poly1305hash_pytype;
2330 Py_INCREF(poly1305key_pytype);
2331 pk->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
2332 Py_TPFLAGS_BASETYPE |
2333 Py_TPFLAGS_HEAPTYPE);
2334 pk->ty.ht_type.tp_alloc = PyType_GenericAlloc;
2335 pk->ty.ht_type.tp_free = 0;
2336 pk->ty.ht_type.tp_new = poly1305hash_pynew;
2337 typeready(&pk->ty.ht_type);
2339 poly1305_keyinit(&pk->k, k, sz);
2340 return ((PyObject *)pk);
2346 static PyObject *poly1305clsget_name(PyObject *me, void *hunoz)
2347 { return (PyString_FromString("poly1305")); }
2349 static PyObject *poly1305clsget_keysz(PyObject *me, void *hunoz)
2350 { return (keysz_pywrap(poly1305_keysz)); }
2352 static PyObject *poly1305clsget_masksz(PyObject *me, void *hunoz)
2353 { return (PyInt_FromLong(POLY1305_MASKSZ)); }
2355 static PyObject *poly1305clsget_tagsz(PyObject *me, void *hunoz)
2356 { return (PyInt_FromLong(POLY1305_TAGSZ)); }
2358 static PyObject *polymeth_copy(PyObject *me, PyObject *arg)
2360 poly1305hash_pyobj *ph;
2361 if (!PyArg_ParseTuple(arg, ":copy")) return (0);
2362 ph = PyObject_NEW(poly1305hash_pyobj, me->ob_type);
2363 poly1305_copy(&ph->ctx, P1305_CTX(me));
2364 Py_INCREF(me->ob_type);
2365 return ((PyObject *)ph);
2368 static PyObject *polymeth_hash(PyObject *me, PyObject *arg)
2372 if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0);
2373 poly1305_hash(P1305_CTX(me), p, sz);
2377 #define POLYMETH_HASHU_(n, W, w) \
2378 static PyObject *polymeth_hashu##w(PyObject *me, PyObject *arg) \
2382 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \
2383 STORE##W(b, x); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \
2386 DOUINTCONV(POLYMETH_HASHU_)
2388 #define POLYMETH_HASHBUF_(n, W, w) \
2389 static PyObject *polymeth_hashbuf##w(PyObject *me, PyObject *arg) \
2394 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
2395 if (sz > MASK##n) TYERR("string too long"); \
2396 STORE##W(b, sz); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \
2397 poly1305_hash(P1305_CTX(me), p, sz); \
2402 DOUINTCONV(POLYMETH_HASHBUF_)
2404 static PyObject *polymeth_hashstrz(PyObject *me, PyObject *arg)
2407 if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0);
2408 poly1305_hash(P1305_CTX(me), p, strlen(p) + 1);
2412 static PyObject *polymeth_flush(PyObject *me, PyObject *arg)
2414 if (!PyArg_ParseTuple(arg, ":flush")) return (0);
2415 poly1305_flush(P1305_CTX(me));
2419 static PyObject *polymeth_flushzero(PyObject *me, PyObject *arg)
2421 if (!PyArg_ParseTuple(arg, ":flushzero")) return (0);
2422 poly1305_flushzero(P1305_CTX(me));
2426 static PyObject *polymeth_concat(PyObject *me, PyObject *arg)
2428 PyObject *pre, *suff;
2429 if (!PyArg_ParseTuple(arg, "OO:concat", &pre, &suff)) return (0);
2430 if (!PyObject_TypeCheck(pre, poly1305hash_pytype) ||
2431 !PyObject_TypeCheck(suff, poly1305hash_pytype))
2432 TYERR("wanted a poly1305hash");
2433 if (me->ob_type != pre->ob_type || me->ob_type != suff->ob_type)
2434 TYERR("key mismatch");
2435 if (P1305_CTX(pre)->nbuf) VALERR("prefix is not block-aligned");
2436 poly1305_concat(P1305_CTX(me), P1305_CTX(pre), P1305_CTX(suff));
2442 static PyObject *polymeth_done(PyObject *me, PyObject *arg)
2445 if (!PyArg_ParseTuple(arg, ":done")) return (0);
2446 if (!(P1305_F(me) & f_mask)) VALERR("no mask");
2447 rc = bytestring_pywrap(0, POLY1305_TAGSZ);
2448 poly1305_done(P1305_CTX(me), PyString_AS_STRING(rc));
2454 static PyGetSetDef poly1305cls_pygetset[] = {
2455 #define GETSETNAME(op, name) poly1305cls##op##_##name
2456 GET (keysz, "PC.keysz -> acceptable key sizes")
2457 GET (masksz, "PC.masksz -> mask size")
2458 GET (tagsz, "PC.tagsz -> MAC output size")
2459 GET (name, "PC.name -> name of this kind of MAC")
2464 static PyMethodDef poly1305hash_pymethods[] = {
2465 #define METHNAME(name) polymeth_##name
2466 METH (copy, "P.copy() -> PP")
2467 METH (hash, "P.hash(M)")
2468 #define METHU_(n, W, w) METH(hashu##w, "P.hashu" #w "(WORD)")
2471 #define METHBUF_(n, W, w) METH(hashbuf##w, "P.hashbuf" #w "(BYTES)")
2472 DOUINTCONV(METHBUF_)
2474 METH (hashstrz, "P.hashstrz(STRING)")
2475 METH (flush, "P.flush()")
2476 METH (flushzero, "P.flushzero()")
2477 METH (concat, "P.concat(PREFIX, SUFFIX)")
2478 METH (done, "P.done() -> TAG")
2483 static PyTypeObject poly1305cls_pytype_skel = {
2484 PyObject_HEAD_INIT(0) 0, /* Header */
2485 "Poly1305Class", /* @tp_name@ */
2486 sizeof(PyHeapTypeObject), /* @tp_basicsize@ */
2487 0, /* @tp_itemsize@ */
2489 0, /* @tp_dealloc@ */
2491 0, /* @tp_getattr@ */
2492 0, /* @tp_setattr@ */
2493 0, /* @tp_compare@ */
2495 0, /* @tp_as_number@ */
2496 0, /* @tp_as_sequence@ */
2497 0, /* @tp_as_mapping@ */
2501 0, /* @tp_getattro@ */
2502 0, /* @tp_setattro@ */
2503 0, /* @tp_as_buffer@ */
2504 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2505 Py_TPFLAGS_BASETYPE,
2508 "Poly1305 metametaclass. Best not to ask.",
2510 0, /* @tp_traverse@ */
2512 0, /* @tp_richcompare@ */
2513 0, /* @tp_weaklistoffset@ */
2515 0, /* @tp_iternext@ */
2516 0, /* @tp_methods@ */
2517 0, /* @tp_members@ */
2518 poly1305cls_pygetset, /* @tp_getset@ */
2521 0, /* @tp_descr_get@ */
2522 0, /* @tp_descr_set@ */
2523 0, /* @tp_dictoffset@ */
2525 PyType_GenericAlloc, /* @tp_alloc@ */
2526 abstract_pynew, /* @tp_new@ */
2531 static PyTypeObject poly1305key_pytype_skel = {
2532 PyObject_HEAD_INIT(0) 0, /* Header */
2533 "poly1305", /* @tp_name@ */
2534 sizeof(poly1305key_pyobj), /* @tp_basicsize@ */
2535 0, /* @tp_itemsize@ */
2537 0, /* @tp_dealloc@ */
2539 0, /* @tp_getattr@ */
2540 0, /* @tp_setattr@ */
2541 0, /* @tp_compare@ */
2543 0, /* @tp_as_number@ */
2544 0, /* @tp_as_sequence@ */
2545 0, /* @tp_as_mapping@ */
2549 0, /* @tp_getattro@ */
2550 0, /* @tp_setattro@ */
2551 0, /* @tp_as_buffer@ */
2552 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2553 Py_TPFLAGS_BASETYPE,
2556 "poly1305(K): Poly1305 key.",
2558 0, /* @tp_traverse@ */
2560 0, /* @tp_richcompare@ */
2561 0, /* @tp_weaklistoffset@ */
2563 0, /* @tp_iternext@ */
2564 0, /* @tp_methods@ */
2565 0, /* @tp_members@ */
2566 0, /* @tp_getset@ */
2569 0, /* @tp_descr_get@ */
2570 0, /* @tp_descr_set@ */
2571 0, /* @tp_dictoffset@ */
2573 PyType_GenericAlloc, /* @tp_alloc@ */
2574 poly1305key_pynew, /* @tp_new@ */
2579 static PyTypeObject poly1305hash_pytype_skel = {
2580 PyObject_HEAD_INIT(0) 0, /* Header */
2581 "Poly1305Hash", /* @tp_name@ */
2582 sizeof(poly1305hash_pyobj), /* @tp_basicsize@ */
2583 0, /* @tp_itemsize@ */
2585 0, /* @tp_dealloc@ */
2587 0, /* @tp_getattr@ */
2588 0, /* @tp_setattr@ */
2589 0, /* @tp_compare@ */
2591 0, /* @tp_as_number@ */
2592 0, /* @tp_as_sequence@ */
2593 0, /* @tp_as_mapping@ */
2597 0, /* @tp_getattro@ */
2598 0, /* @tp_setattro@ */
2599 0, /* @tp_as_buffer@ */
2600 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2601 Py_TPFLAGS_BASETYPE,
2604 "Poly1305 MAC context base class.",
2606 0, /* @tp_traverse@ */
2608 0, /* @tp_richcompare@ */
2609 0, /* @tp_weaklistoffset@ */
2611 0, /* @tp_iternext@ */
2612 poly1305hash_pymethods, /* @tp_methods@ */
2613 0, /* @tp_members@ */
2614 0, /* @tp_getset@ */
2617 0, /* @tp_descr_get@ */
2618 0, /* @tp_descr_set@ */
2619 0, /* @tp_dictoffset@ */
2621 PyType_GenericAlloc, /* @tp_alloc@ */
2622 abstract_pynew, /* @tp_new@ */
2627 /*----- Special snowflake for HSalsa and HChaCha --------------------------*/
2629 #define DEF_HDANCE(DANCE, HDANCE, dance, hdance) \
2630 static PyObject *meth_##hdance##_prf(PyObject *me, PyObject *arg) \
2632 dance##_ctx dance; \
2634 Py_ssize_t ksz, nsz; \
2636 if (!PyArg_ParseTuple(arg, "s#s#:" #hdance "_prf", \
2637 &k, &ksz, &n, &nsz)) \
2639 if (ksz != keysz(ksz, dance##_keysz)) VALERR("bad key length"); \
2640 if (nsz != HDANCE##_INSZ) VALERR("bad input length"); \
2641 rc = bytestring_pywrap(0, HSALSA20_OUTSZ); \
2642 dance##_init(&dance, k, ksz, 0); \
2643 hdance##_prf(&dance, n, PyString_AS_STRING(rc)); \
2649 DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa20)
2650 DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa2012)
2651 DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa208)
2653 DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha20)
2654 DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha12)
2655 DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha8)
2657 /*----- Keccak-p[1600, n] -------------------------------------------------*/
2659 static PyTypeObject *kxvik_pytype;
2661 typedef struct kxvik_pyobj {
2667 static PyObject *kxvik_pynew(PyTypeObject *ty,
2668 PyObject *arg, PyObject *kw)
2671 kxvik_pyobj *rc = 0;
2672 static const char *const kwlist[] = { "nround", 0 };
2673 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", KWLIST,
2676 rc = (kxvik_pyobj *)ty->tp_alloc(ty, 0);
2678 keccak1600_init(&rc->s);
2680 return ((PyObject *)rc);
2683 static PyObject *kxvikmeth_copy(PyObject *me, PyObject *arg)
2685 kxvik_pyobj *k = (kxvik_pyobj *)me, *rc = 0;
2686 if (!PyArg_ParseTuple(arg, ":copy")) goto end;
2687 rc = (kxvik_pyobj *)k->ob_type->tp_alloc(k->ob_type, 0);
2688 rc->s = k->s; rc->n = k->n;
2690 return ((PyObject *)rc);
2693 static PyObject *kxvikmeth_mix(PyObject *me, PyObject *arg)
2695 kxvik_pyobj *k = (kxvik_pyobj *)me;
2700 char *p; Py_ssize_t n;
2702 if (!PyArg_ParseTuple(arg, "s#:mix", &p, &n)) goto end;
2703 if (n > 200) VALERR("out of range");
2704 q = (const octet *)p;
2706 while (n > 8) { LOAD64_L_(t[i], q); i++; q += 8; n -= 8; }
2708 memcpy(buf, q, n); memset(buf + n, 0, 8 - n);
2709 LOAD64_L_(t[i], buf); i++;
2711 keccak1600_mix(&k->s, t, i);
2717 static PyObject *kxvikmeth_extract(PyObject *me, PyObject *arg)
2719 kxvik_pyobj *k = (kxvik_pyobj *)me;
2726 if (!PyArg_ParseTuple(arg, "O&:extract", convuint, &n)) goto end;
2727 if (n > 200) VALERR("out of range");
2728 rc = bytestring_pywrap(0, n);
2729 q = (octet *)PyString_AS_STRING(rc);
2730 keccak1600_extract(&k->s, t, (n + 7)/8);
2732 while (n > 8) { STORE64_L_(q, t[i]); i++; q += 8; n -= 8; }
2733 if (n) { STORE64_L_(buf, t[i]); memcpy(q, buf, n); }
2738 static PyObject *kxvikmeth_step(PyObject *me, PyObject *arg)
2740 kxvik_pyobj *k = (kxvik_pyobj *)me;
2741 if (!PyArg_ParseTuple(arg, ":step")) return (0);
2742 keccak1600_p(&k->s, &k->s, k->n);
2746 static PyObject *kxvikget_nround(PyObject *me, void *hunoz)
2748 kxvik_pyobj *k = (kxvik_pyobj *)me;
2749 return (PyInt_FromLong(k->n));
2752 static int kxvikset_nround(PyObject *me, PyObject *val, void *hunoz)
2754 kxvik_pyobj *k = (kxvik_pyobj *)me;
2758 if (!val) NIERR("__del__");
2759 if (!convuint(val, &n)) goto end;
2766 static PyGetSetDef kxvik_pygetset[] = {
2767 #define GETSETNAME(op, name) kxvik##op##_##name
2768 GETSET(nround, "KECCAK.nround -> number of rounds")
2773 static PyMethodDef kxvik_pymethods[] = {
2774 #define METHNAME(func) kxvikmeth_##func
2775 METH (copy, "KECCAK.copy() -> KECCAK'")
2776 METH (mix, "KECCAK.mix(DATA)")
2777 METH (extract, "KECCAK.extract(NOCTETS)")
2778 METH (step, "KECCAK.step()")
2783 static PyTypeObject kxvik_pytype_skel = {
2784 PyObject_HEAD_INIT(0) 0, /* Header */
2785 "Keccak1600", /* @tp_name@ */
2786 sizeof(kxvik_pyobj), /* @tp_basicsize@ */
2787 0, /* @tp_itemsize@ */
2789 0, /* @tp_dealloc@ */
2791 0, /* @tp_getattr@ */
2792 0, /* @tp_setattr@ */
2793 0, /* @tp_compare@ */
2795 0, /* @tp_as_number@ */
2796 0, /* @tp_as_sequence@ */
2797 0, /* @tp_as_mapping@ */
2801 0, /* @tp_getattro@ */
2802 0, /* @tp_setattro@ */
2803 0, /* @tp_as_buffer@ */
2804 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2805 Py_TPFLAGS_BASETYPE,
2808 "Keccak1600([nround = 24]): Keccak-p[1600, n] state.",
2810 0, /* @tp_traverse@ */
2812 0, /* @tp_richcompare@ */
2813 0, /* @tp_weaklistoffset@ */
2815 0, /* @tp_iternext@ */
2816 kxvik_pymethods, /* @tp_methods@ */
2817 0, /* @tp_members@ */
2818 kxvik_pygetset, /* @tp_getset@ */
2821 0, /* @tp_descr_get@ */
2822 0, /* @tp_descr_set@ */
2823 0, /* @tp_dictoffset@ */
2825 PyType_GenericAlloc, /* @tp_alloc@ */
2826 kxvik_pynew, /* @tp_new@ */
2831 static PyTypeObject *shake_pytype, *shake128_pytype, *shake256_pytype;
2833 typedef struct shake_pyobj {
2839 #define SHAKE_H(o) (&((shake_pyobj *)(o))->h)
2840 #define SHAKE_ST(o) (((shake_pyobj *)(o))->st)
2842 static PyObject *shake_dopynew(void (*initfn)(shake_ctx *,
2843 const void *, size_t,
2844 const void *, size_t),
2846 PyObject *arg, PyObject *kw)
2848 shake_pyobj *rc = 0;
2849 char *p = 0, *f = 0;
2850 Py_ssize_t psz = 0, fsz = 0;
2851 static const char *const kwlist[] = { "perso", "func", 0 };
2853 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|s#s#:new", KWLIST,
2854 &p, &psz, &f, &fsz))
2856 rc = (shake_pyobj *)ty->tp_alloc(ty, 0);
2857 initfn(&rc->h, f, fsz, p, psz);
2860 return ((PyObject *)rc);
2863 static PyObject *shake128_pynew(PyTypeObject *ty,
2864 PyObject *arg, PyObject *kw)
2865 { return (shake_dopynew(cshake128_init, ty, arg, kw)); }
2867 static PyObject *shake256_pynew(PyTypeObject *ty,
2868 PyObject *arg, PyObject *kw)
2869 { return (shake_dopynew(cshake256_init, ty, arg, kw)); }
2871 static int shake_check(PyObject *me, int st)
2873 if (SHAKE_ST(me) != st) VALERR("wrong state");
2879 static PyObject *shakemeth_hash(PyObject *me, PyObject *arg)
2883 if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0);
2884 if (shake_check(me, 0)) return (0);
2885 shake_hash(SHAKE_H(me), p, sz);
2889 #define SHAKEMETH_HASHU_(n, W, w) \
2890 static PyObject *shakemeth_hashu##w(PyObject *me, PyObject *arg) \
2894 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \
2895 if (shake_check(me, 0)) return (0); \
2896 STORE##W(b, x); shake_hash(SHAKE_H(me), b, sizeof(b)); \
2899 DOUINTCONV(SHAKEMETH_HASHU_)
2901 #define SHAKEMETH_HASHBUF_(n, W, w) \
2902 static PyObject *shakemeth_hashbuf##w(PyObject *me, PyObject *arg) \
2907 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
2908 if (sz > MASK##n) TYERR("string too long"); \
2909 if (shake_check(me, 0)) goto end; \
2910 STORE##W(b, sz); shake_hash(SHAKE_H(me), b, sizeof(b)); \
2911 shake_hash(SHAKE_H(me), p, sz); \
2916 DOUINTCONV(SHAKEMETH_HASHBUF_)
2918 static PyObject *shakemeth_hashstrz(PyObject *me, PyObject *arg)
2921 if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0);
2922 if (shake_check(me, 0)) return (0);
2923 shake_hash(SHAKE_H(me), p, strlen(p) + 1);
2927 static PyObject *shakemeth_xof(PyObject *me, PyObject *arg)
2929 if (!PyArg_ParseTuple(arg, ":xof")) goto end;
2930 if (shake_check(me, 0)) goto end;
2931 shake_xof(SHAKE_H(me));
2938 static PyObject *shakemeth_done(PyObject *me, PyObject *arg)
2942 if (!PyArg_ParseTuple(arg, "O&:done", convszt, &n)) goto end;
2943 if (shake_check(me, 0)) goto end;
2944 rc = bytestring_pywrap(0, n);
2945 shake_done(SHAKE_H(me), PyString_AS_STRING(rc), n);
2951 static PyObject *shakemeth_copy(PyObject *me, PyObject *arg)
2953 shake_pyobj *rc = 0;
2955 if (!PyArg_ParseTuple(arg, ":copy")) goto end;
2956 rc = PyObject_NEW(shake_pyobj, me->ob_type);
2957 rc->h = *SHAKE_H(me);
2958 rc->st = SHAKE_ST(me);
2960 return ((PyObject *)rc);
2963 static PyObject *shakemeth_get(PyObject *me, PyObject *arg)
2968 if (!PyArg_ParseTuple(arg, "O&:get", convszt, &sz)) goto end;
2969 if (shake_check(me, 1)) goto end;
2970 rc = bytestring_pywrap(0, sz);
2971 shake_get(SHAKE_H(me), PyString_AS_STRING(rc), sz);
2976 static PyObject *shakemeth_mask(PyObject *me, PyObject *arg)
2979 char *p; Py_ssize_t sz;
2981 if (!PyArg_ParseTuple(arg, "s#:mask", &p, &sz)) goto end;
2982 if (shake_check(me, 1)) goto end;
2983 rc = bytestring_pywrap(0, sz);
2984 shake_mask(SHAKE_H(me), p, PyString_AS_STRING(rc), sz);
2989 static PyObject *shakeget_rate(PyObject *me, void *hunoz)
2990 { return (PyInt_FromLong(SHAKE_H(me)->h.r)); }
2992 static PyObject *shakeget_buffered(PyObject *me, void *hunoz)
2993 { return (PyInt_FromLong(SHAKE_H(me)->h.n)); }
2995 static PyObject *shakeget_state(PyObject *me, void *hunoz)
2997 int st = SHAKE_ST(me);
2998 return (PyString_FromString(st == 0 ? "absorb" :
2999 st == 1 ? "squeeze" : "dead"));
3002 static PyGetSetDef shake_pygetset[] = {
3003 #define GETSETNAME(op, name) shake##op##_##name
3004 GET (rate, "S.rate -> rate, in bytes")
3005 GET (buffered, "S.buffered -> amount currently buffered")
3006 GET (state, "S.state -> `absorb', `squeeze', `dead'")
3011 static PyMethodDef shake_pymethods[] = {
3012 #define METHNAME(func) shakemeth_##func
3013 METH (copy, "S.copy() -> SS")
3014 METH (hash, "S.hash(M)")
3015 #define METHU_(n, W, w) METH(hashu##w, "S.hashu" #w "(WORD)")
3018 #define METHBUF_(n, W, w) METH(hashbuf##w, "S.hashbuf" #w "(BYTES)")
3019 DOUINTCONV(METHBUF_)
3021 METH (hashstrz, "S.hashstrz(STRING)")
3022 METH (xof, "S.xof()")
3023 METH (done, "S.done(LEN) ->H")
3024 METH (get, "S.get(LEN) -> H")
3025 METH (mask, "S.mask(M) -> C")
3030 static PyTypeObject shake_pytype_skel = {
3031 PyObject_HEAD_INIT(0) 0, /* Header */
3032 "Shake", /* @tp_name@ */
3033 sizeof(shake_pyobj), /* @tp_basicsize@ */
3034 0, /* @tp_itemsize@ */
3036 0, /* @tp_dealloc@ */
3038 0, /* @tp_getattr@ */
3039 0, /* @tp_setattr@ */
3040 0, /* @tp_compare@ */
3042 0, /* @tp_as_number@ */
3043 0, /* @tp_as_sequence@ */
3044 0, /* @tp_as_mapping@ */
3048 0, /* @tp_getattro@ */
3049 0, /* @tp_setattro@ */
3050 0, /* @tp_as_buffer@ */
3051 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
3052 Py_TPFLAGS_BASETYPE,
3055 "SHAKE/cSHAKE base class.",
3057 0, /* @tp_traverse@ */
3059 0, /* @tp_richcompare@ */
3060 0, /* @tp_weaklistoffset@ */
3062 0, /* @tp_iternext@ */
3063 shake_pymethods, /* @tp_methods@ */
3064 0, /* @tp_members@ */
3065 shake_pygetset, /* @tp_getset@ */
3068 0, /* @tp_descr_get@ */
3069 0, /* @tp_descr_set@ */
3070 0, /* @tp_dictoffset@ */
3072 PyType_GenericAlloc, /* @tp_alloc@ */
3073 abstract_pynew, /* @tp_new@ */
3078 static PyTypeObject shake128_pytype_skel = {
3079 PyObject_HEAD_INIT(0) 0, /* Header */
3080 "Shake128", /* @tp_name@ */
3081 0, /* @tp_basicsize@ */
3082 0, /* @tp_itemsize@ */
3084 0, /* @tp_dealloc@ */
3086 0, /* @tp_getattr@ */
3087 0, /* @tp_setattr@ */
3088 0, /* @tp_compare@ */
3090 0, /* @tp_as_number@ */
3091 0, /* @tp_as_sequence@ */
3092 0, /* @tp_as_mapping@ */
3096 0, /* @tp_getattro@ */
3097 0, /* @tp_setattro@ */
3098 0, /* @tp_as_buffer@ */
3099 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
3100 Py_TPFLAGS_BASETYPE,
3103 "Shake128([perso = STR], [func = STR]): SHAKE128/cSHAKE128 XOF.",
3105 0, /* @tp_traverse@ */
3107 0, /* @tp_richcompare@ */
3108 0, /* @tp_weaklistoffset@ */
3110 0, /* @tp_iternext@ */
3111 0, /* @tp_methods@ */
3112 0, /* @tp_members@ */
3113 0, /* @tp_getset@ */
3116 0, /* @tp_descr_get@ */
3117 0, /* @tp_descr_set@ */
3118 0, /* @tp_dictoffset@ */
3120 PyType_GenericAlloc, /* @tp_alloc@ */
3121 shake128_pynew, /* @tp_new@ */
3126 static PyTypeObject shake256_pytype_skel = {
3127 PyObject_HEAD_INIT(0) 0, /* Header */
3128 "Shake256", /* @tp_name@ */
3129 0, /* @tp_basicsize@ */
3130 0, /* @tp_itemsize@ */
3132 0, /* @tp_dealloc@ */
3134 0, /* @tp_getattr@ */
3135 0, /* @tp_setattr@ */
3136 0, /* @tp_compare@ */
3138 0, /* @tp_as_number@ */
3139 0, /* @tp_as_sequence@ */
3140 0, /* @tp_as_mapping@ */
3144 0, /* @tp_getattro@ */
3145 0, /* @tp_setattro@ */
3146 0, /* @tp_as_buffer@ */
3147 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
3148 Py_TPFLAGS_BASETYPE,
3151 "Shake256([perso = STR], [func = STR]): SHAKE256/cSHAKE256 XOF.",
3153 0, /* @tp_traverse@ */
3155 0, /* @tp_richcompare@ */
3156 0, /* @tp_weaklistoffset@ */
3158 0, /* @tp_iternext@ */
3159 0, /* @tp_methods@ */
3160 0, /* @tp_members@ */
3161 0, /* @tp_getset@ */
3164 0, /* @tp_descr_get@ */
3165 0, /* @tp_descr_set@ */
3166 0, /* @tp_dictoffset@ */
3168 PyType_GenericAlloc, /* @tp_alloc@ */
3169 shake256_pynew, /* @tp_new@ */
3174 /*----- Pseudorandom permutations -----------------------------------------*/
3176 static PyTypeObject *gcprp_pytype, *gprp_pytype;
3178 typedef struct prpinfo {
3183 void (*init)(void *, const void *, size_t);
3184 void (*eblk)(void *, const void *, void *);
3185 void (*dblk)(void *, const void *, void *);
3188 #define PRP_DEF(PRE, pre) \
3189 static void pre##_prpinit(void *ctx, const void *k, size_t ksz) \
3190 { pre##_init(ctx, k, ksz); } \
3191 static void pre##_prpeblk(void *ctx, const void *in, void *out) \
3193 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
3194 pre##_eblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
3196 static void pre##_prpdblk(void *ctx, const void *in, void *out) \
3198 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
3199 pre##_dblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
3201 static const prpinfo pre##_prpinfo = { \
3202 #pre, pre##_keysz, sizeof(pre##_ctx), PRE##_BLKSZ, \
3203 pre##_prpinit, pre##_prpeblk, pre##_prpdblk \
3207 static const struct prpinfo *const gprptab[] = {
3208 #define PRP_ENTRY(PRE, pre) &pre##_prpinfo,
3213 typedef struct gcprp_pyobj {
3214 PyHeapTypeObject ty;
3217 #define GCPRP_PRP(o) (((gcprp_pyobj *)(o))->prp)
3219 typedef struct gprp_pyobj {
3223 #define GPRP_PRP(o) (((gprp_pyobj *)(o))->prp)
3224 #define GPRP_CTX(o) (((gprp_pyobj *)(o)) + 1)
3226 typedef struct prp {
3231 static PyObject *gprp_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
3233 static const char *const kwlist[] = { "key", 0 };
3236 const prpinfo *prp = GCPRP_PRP(ty);
3239 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &k, &sz))
3241 if (keysz(sz, prp->keysz) != sz) VALERR("bad key length");
3242 me = (PyObject *)ty->tp_alloc(ty, 0);
3244 prp->init(GPRP_CTX(me), k, sz);
3251 static void gprp_pydealloc(PyObject *me)
3252 { Py_DECREF(me->ob_type); FREEOBJ(me); }
3254 static PyObject *gcprp_pywrap(const prpinfo *prp)
3256 gcprp_pyobj *g = newtype(gcprp_pytype, 0, prp->name);
3258 g->ty.ht_type.tp_basicsize = sizeof(gprp_pyobj) + prp->ctxsz;
3259 g->ty.ht_type.tp_base = gprp_pytype;
3260 Py_INCREF(gprp_pytype);
3261 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
3262 Py_TPFLAGS_BASETYPE |
3263 Py_TPFLAGS_HEAPTYPE);
3264 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
3265 g->ty.ht_type.tp_free = 0;
3266 g->ty.ht_type.tp_new = gprp_pynew;
3267 typeready(&g->ty.ht_type);
3268 return ((PyObject *)g);
3271 static PyObject *gcpget_name(PyObject *me, void *hunoz)
3272 { return (PyString_FromString(GCPRP_PRP(me)->name)); }
3273 static PyObject *gcpget_keysz(PyObject *me, void *hunoz)
3274 { return (keysz_pywrap(GCPRP_PRP(me)->keysz)); }
3275 static PyObject *gcpget_blksz(PyObject *me, void *hunoz)
3276 { return (PyInt_FromLong(GCPRP_PRP(me)->blksz)); }
3278 static PyObject *gpmeth_encrypt(PyObject *me, PyObject *arg)
3284 if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &n)) goto end;
3285 if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length");
3286 rc = bytestring_pywrap(0, n);
3287 GPRP_PRP(me)->eblk(GPRP_CTX(me), p, PyString_AS_STRING(rc));
3292 static PyObject *gpmeth_decrypt(PyObject *me, PyObject *arg)
3298 if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &n)) goto end;
3299 if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length");
3300 rc = bytestring_pywrap(0, n);
3301 GPRP_PRP(me)->dblk(GPRP_CTX(me), p, PyString_AS_STRING(rc));
3306 static PyGetSetDef gcprp_pygetset[] = {
3307 #define GETSETNAME(op, name) gcp##op##_##name
3308 GET (keysz, "CP.keysz -> acceptable key sizes")
3309 GET (blksz, "CP.blksz -> block size")
3310 GET (name, "CP.name -> name of this kind of PRP")
3315 static PyMethodDef gprp_pymethods[] = {
3316 #define METHNAME(name) gpmeth_##name
3317 METH (encrypt, "P.encrypt(PT) -> CT")
3318 METH (decrypt, "P.decrypt(CT) -> PT")
3323 static PyTypeObject gcprp_pytype_skel = {
3324 PyObject_HEAD_INIT(0) 0, /* Header */
3325 "GCPRP", /* @tp_name@ */
3326 sizeof(gcprp_pyobj), /* @tp_basicsize@ */
3327 0, /* @tp_itemsize@ */
3329 0, /* @tp_dealloc@ */
3331 0, /* @tp_getattr@ */
3332 0, /* @tp_setattr@ */
3333 0, /* @tp_compare@ */
3335 0, /* @tp_as_number@ */
3336 0, /* @tp_as_sequence@ */
3337 0, /* @tp_as_mapping@ */
3341 0, /* @tp_getattro@ */
3342 0, /* @tp_setattro@ */
3343 0, /* @tp_as_buffer@ */
3344 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
3345 Py_TPFLAGS_BASETYPE,
3348 "Pseudorandom permutation metaclass.",
3350 0, /* @tp_traverse@ */
3352 0, /* @tp_richcompare@ */
3353 0, /* @tp_weaklistoffset@ */
3355 0, /* @tp_iternext@ */
3356 0, /* @tp_methods@ */
3357 0, /* @tp_members@ */
3358 gcprp_pygetset, /* @tp_getset@ */
3361 0, /* @tp_descr_get@ */
3362 0, /* @tp_descr_set@ */
3363 0, /* @tp_dictoffset@ */
3365 PyType_GenericAlloc, /* @tp_alloc@ */
3366 abstract_pynew, /* @tp_new@ */
3371 static PyTypeObject gprp_pytype_skel = {
3372 PyObject_HEAD_INIT(0) 0, /* Header */
3373 "GPRP", /* @tp_name@ */
3374 sizeof(gprp_pyobj), /* @tp_basicsize@ */
3375 0, /* @tp_itemsize@ */
3377 gprp_pydealloc, /* @tp_dealloc@ */
3379 0, /* @tp_getattr@ */
3380 0, /* @tp_setattr@ */
3381 0, /* @tp_compare@ */
3383 0, /* @tp_as_number@ */
3384 0, /* @tp_as_sequence@ */
3385 0, /* @tp_as_mapping@ */
3389 0, /* @tp_getattro@ */
3390 0, /* @tp_setattro@ */
3391 0, /* @tp_as_buffer@ */
3392 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
3393 Py_TPFLAGS_BASETYPE,
3396 "Pseudorandom permutation, abstract base class.",
3398 0, /* @tp_traverse@ */
3400 0, /* @tp_richcompare@ */
3401 0, /* @tp_weaklistoffset@ */
3403 0, /* @tp_iternext@ */
3404 gprp_pymethods, /* @tp_methods@ */
3405 0, /* @tp_members@ */
3406 0, /* @tp_getset@ */
3409 0, /* @tp_descr_get@ */
3410 0, /* @tp_descr_set@ */
3411 0, /* @tp_dictoffset@ */
3413 PyType_GenericAlloc, /* @tp_alloc@ */
3414 abstract_pynew, /* @tp_new@ */
3419 /*----- Main code ---------------------------------------------------------*/
3421 static PyMethodDef methods[] = {
3422 #define METHNAME(func) meth_##func
3423 METH (_KeySZ_fromdl, "\
3424 fromdl(N) -> M: convert integer discrete log field size to work factor")
3425 METH (_KeySZ_fromschnorr, "\
3426 fromschnorr(N) -> M: convert Schnorr group order to work factor")
3427 METH (_KeySZ_fromif, "\
3428 fromif(N) -> M: convert integer factorization problem size to work factor")
3429 METH (_KeySZ_fromec, "\
3430 fromec(N) -> M: convert elliptic curve group order to work factor")
3431 METH (_KeySZ_todl, "\
3432 todl(N) -> M: convert work factor to integer discrete log field size")
3433 METH (_KeySZ_toschnorr, "\
3434 toschnorr(N) -> M: convert work factor to Schnorr group order")
3435 METH (_KeySZ_toif, "\
3436 toif(N) -> M: convert work factor to integer factorization problem size")
3437 METH (_KeySZ_toec, "\
3438 toec(N) -> M: convert work factor to elliptic curve group order")
3439 METH (_KeySZ_toec, "\
3440 toec(N) -> M: convert work factor to elliptic curve group order")
3441 #define METH_HDANCE(hdance, HDance) METH(hdance##_prf, "\
3442 " #hdance "_prf(K, N) -> H: calculate " HDance " hash of N with K")
3443 METH_HDANCE(hsalsa20, "HSalsa20")
3444 METH_HDANCE(hsalsa2012, "HSalsa20/12")
3445 METH_HDANCE(hsalsa208, "HSalsa20/8")
3446 METH_HDANCE(hchacha20, "HChaCha20")
3447 METH_HDANCE(hchacha12, "HChaCha12")
3448 METH_HDANCE(hchacha8, "HChaCha8")
3454 void algorithms_pyinit(void)
3456 INITTYPE(keysz, root);
3457 INITTYPE(keyszany, keysz);
3458 INITTYPE(keyszrange, keysz);
3459 INITTYPE(keyszset, keysz);
3460 INITTYPE(gccipher, type);
3461 INITTYPE(gcipher, root);
3462 INITTYPE(gcaead, type);
3463 INITTYPE(gaeadkey, root);
3464 INITTYPE(gcaeadaad, type);
3465 INITTYPE(gaeadaad, root);
3466 INITTYPE(gcaeadenc, type);
3467 INITTYPE(gaeadenc, root);
3468 INITTYPE(gcaeaddec, type);
3469 INITTYPE(gaeaddec, root);
3470 INITTYPE(gchash, type);
3471 INITTYPE(ghash, root);
3472 INITTYPE(gcmac, type);
3473 INITTYPE(gmac, type);
3474 INITTYPE(gmhash, ghash);
3475 INITTYPE(poly1305cls, type);
3476 INITTYPE_META(poly1305key, type, poly1305cls);
3477 INITTYPE(poly1305hash, root);
3478 INITTYPE(kxvik, root);
3479 INITTYPE(shake, root);
3480 INITTYPE(shake128, shake);
3481 INITTYPE(shake256, shake);
3482 INITTYPE(gcprp, type);
3483 INITTYPE(gprp, root);
3484 addmethods(methods);
3487 GEN(gcciphers, cipher)
3491 #define gcprp prpinfo
3494 void algorithms_pyinsert(PyObject *mod)
3497 INSERT("KeySZ", keysz_pytype);
3498 INSERT("KeySZAny", keyszany_pytype);
3499 INSERT("KeySZRange", keyszrange_pytype);
3500 INSERT("KeySZSet", keyszset_pytype);
3501 INSERT("GCCipher", gccipher_pytype);
3502 INSERT("GCipher", gcipher_pytype);
3503 INSERT("gcciphers", gcciphers());
3504 INSERT("GCAEAD", gcaead_pytype);
3505 INSERT("GAEKey", gaeadkey_pytype);
3506 INSERT("GAEAADClass", gcaeadaad_pytype);
3507 INSERT("GAEAAD", gaeadaad_pytype);
3508 INSERT("GAEEncClass", gcaeadenc_pytype);
3509 INSERT("GAEEnc", gaeadenc_pytype);
3510 INSERT("GAEDecClass", gcaeaddec_pytype);
3511 INSERT("GAEDec", gaeaddec_pytype);
3512 INSERT("gcaeads", gcaeads());
3513 INSERT("GCHash", gchash_pytype);
3514 INSERT("GHash", ghash_pytype);
3515 INSERT("gchashes", d = gchashes());
3516 sha_pyobj = PyDict_GetItemString(d, "sha"); Py_INCREF(sha_pyobj);
3517 has160_pyobj = PyDict_GetItemString(d, "has160"); Py_INCREF(has160_pyobj);
3518 INSERT("GCMAC", gcmac_pytype);
3519 INSERT("GMAC", gmac_pytype);
3520 INSERT("GMACHash", gmhash_pytype);
3521 INSERT("gcmacs", gcmacs());
3522 INSERT("Poly1305Class", poly1305cls_pytype);
3523 INSERT("poly1305", poly1305key_pytype);
3524 INSERT("Poly1305Hash", poly1305hash_pytype);
3525 INSERT("Keccak1600", kxvik_pytype);
3526 INSERT("Shake", shake_pytype);
3527 INSERT("Shake128", shake128_pytype);
3528 INSERT("Shake256", shake256_pytype);
3529 INSERT("GCPRP", gcprp_pytype);
3530 INSERT("GPRP", gprp_pytype);
3531 INSERT("gcprps", gcprps());
3534 /*----- That's all, folks -------------------------------------------------*/