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"
31 #include "algorithms.h"
34 /*----- Key sizes ---------------------------------------------------------*/
36 static PyTypeObject
*keysz_pytype
;
37 static PyTypeObject
*keyszany_pytype
, *keyszrange_pytype
, *keyszset_pytype
;
39 typedef struct keysz_pyobj
{
44 typedef struct keyszrange_pyobj
{
50 typedef struct keyszset_pyobj
{
56 #define KEYSZ_PYCHECK(o) PyObject_TypeCheck((o), keysz_pytype)
59 # define KSZ_OPMASK 0x1f
63 # define KSZ_16BIT 0x20
66 PyObject
*keysz_pywrap(const octet
*k
)
69 #define ARG(i) (op&KSZ_16BIT ? LOAD16(k + 2*(i)) : k[i])
70 switch (op
&KSZ_OPMASK
) {
72 keysz_pyobj
*o
= PyObject_New(keysz_pyobj
, keyszany_pytype
);
74 return ((PyObject
*)o
);
78 PyObject_New(keyszrange_pyobj
, keyszrange_pytype
);
83 if (!o
->mod
) o
->mod
= 1;
84 return ((PyObject
*)o
);
88 PyObject_New(keyszset_pyobj
, keyszset_pytype
);
91 for (i
= 0; ARG(i
); i
++) ;
92 n
= i
; o
->set
= PyTuple_New(n
);
93 for (i
= 0; i
< n
; i
++)
94 PyTuple_SET_ITEM(o
->set
, i
, PyInt_FromLong(ARG(i
)));
95 return ((PyObject
*)o
);
103 static PyObject
*keyszany_pynew(PyTypeObject
*ty
,
104 PyObject
*arg
, PyObject
*kw
)
106 static const char *const kwlist
[] = { "default", 0 };
110 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "i:new", KWLIST
, &dfl
))
112 if (dfl
< 0) VALERR("key size cannot be negative");
113 o
= (keysz_pyobj
*)ty
->tp_alloc(ty
, 0);
115 return ((PyObject
*)o
);
120 static PyObject
*keyszrange_pynew(PyTypeObject
*ty
,
121 PyObject
*arg
, PyObject
*kw
)
123 static const char *const kwlist
[] = { "default", "min", "max", "mod", 0 };
124 int dfl
, min
= 0, max
= 0, mod
= 1;
127 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "i|iii:new", KWLIST
,
128 &dfl
, &min
, &max
, &mod
))
130 if (dfl
< 0 || min
< 0) VALERR("key size cannot be negative");
131 if (min
> dfl
|| (max
&& dfl
> max
)) VALERR("bad key size bounds");
132 if (mod
<= 0 || dfl
%mod
|| min
%mod
|| max
%mod
)
133 VALERR("bad key size modulus");
134 o
= (keyszrange_pyobj
*)ty
->tp_alloc(ty
, 0);
139 return ((PyObject
*)o
);
144 static PyObject
*keyszset_pynew(PyTypeObject
*ty
,
145 PyObject
*arg
, PyObject
*kw
)
147 static const char *const kwlist
[] = { "default", "set", 0 };
150 PyObject
*x
= 0, *l
= 0;
151 keyszset_pyobj
*o
= 0;
153 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "i|O:new", KWLIST
, &dfl
, &set
))
155 if (!set
) set
= PyTuple_New(0);
157 if (!PySequence_Check(set
)) TYERR("want a sequence");
158 n
= PySequence_Size(set
); if (n
< 0) goto end
;
159 l
= PyList_New(0); if (!l
) goto end
;
160 if (dfl
< 0) VALERR("key size cannot be negative");
161 x
= PyInt_FromLong(dfl
);
165 for (i
= 0; i
< n
; i
++) {
166 if ((x
= PySequence_GetItem(set
, i
)) == 0) goto end
;
167 xx
= PyInt_AsLong(x
);
168 if (PyErr_Occurred()) goto end
;
169 if (xx
== dfl
) continue;
170 if (xx
< 0) VALERR("key size cannot be negative");
176 if ((set
= PySequence_Tuple(l
)) == 0) goto end
;
177 o
= (keyszset_pyobj
*)ty
->tp_alloc(ty
, 0);
185 return ((PyObject
*)o
);
188 static PyObject
*kaget_min(PyObject
*me
, void *hunoz
)
189 { return (PyInt_FromLong(0)); }
190 #define kaget_max kaget_min
192 static PyObject
*ksget_min(PyObject
*me
, void *hunoz
)
194 PyObject
*set
= ((keyszset_pyobj
*)me
)->set
;
196 n
= PyTuple_GET_SIZE(set
);
197 for (i
= 0; i
< n
; i
++) {
198 y
= PyInt_AS_LONG(PyTuple_GET_ITEM(set
, i
));
199 if (x
== -1 || y
< x
) x
= y
;
201 return (PyInt_FromLong(x
));
204 static PyObject
*ksget_max(PyObject
*me
, void *hunoz
)
206 PyObject
*set
= ((keyszset_pyobj
*)me
)->set
;
208 n
= PyTuple_GET_SIZE(set
);
209 for (i
= 0; i
< n
; i
++) {
210 y
= PyInt_AS_LONG(PyTuple_GET_ITEM(set
, i
));
213 return (PyInt_FromLong(x
));
216 static const PyMemberDef keysz_pymembers
[] = {
217 #define MEMBERSTRUCT keysz_pyobj
218 #define default dfl /* ugh! */
219 MEMBER(default, T_INT
, READONLY
, "KSZ.default -> default key size")
225 #define KSZCONVOP(op) \
226 static PyObject *kszmeth_##op(PyObject *me, PyObject *arg) \
229 if (!PyArg_ParseTuple(arg, "d:" #op, &x)) return (0); \
231 return (PyFloat_FromDouble(y)); \
234 KSZCONVOP(fromschnorr
)
243 static const PyMethodDef keysz_pymethods
[] = {
244 #define METHNAME(name) kszmeth_##name
245 SMTH (fromdl
, "fromdl(N) -> M: "
246 "convert integer discrete log field size to work factor")
247 SMTH (fromschnorr
, "fromschnorr(N) -> M: "
248 "convert Schnorr group order to work factor")
249 SMTH (fromif
, "fromif(N) -> M: "
250 "convert integer factorization problem size to work factor")
251 SMTH (fromec
, "fromec(N) -> M: "
252 "convert elliptic curve group order to work factor")
253 SMTH (todl
, "todl(N) -> M: "
254 "convert work factor to integer discrete log field size")
255 SMTH (toschnorr
, "toschnorr(N) -> M: "
256 "convert work factor to Schnorr group order")
257 SMTH (toif
, "toif(N) -> M: "
258 "convert work factor to integer factorization problem size")
259 SMTH (toec
, "toec(N) -> M: "
260 "convert work factor to elliptic curve group order")
261 SMTH (toec
, "toec(N) -> M: "
262 "convert work factor to elliptic curve group order")
267 static const PyGetSetDef keyszany_pygetset
[] = {
268 #define GETSETNAME(op, name) ka##op##_##name
269 GET (min
, "KSZ.min -> smallest allowed key size")
270 GET (max
, "KSZ.max -> largest allowed key size")
275 static const PyMemberDef keyszrange_pymembers
[] = {
276 #define MEMBERSTRUCT keyszrange_pyobj
277 MEMBER(min
, T_INT
, READONLY
, "KSZ.min -> smallest allowed key size")
278 MEMBER(max
, T_INT
, READONLY
, "KSZ.max -> largest allowed key size")
279 MEMBER(mod
, T_INT
, READONLY
,
280 "KSZ.mod -> key size must be a multiple of this")
285 static const PyGetSetDef keyszset_pygetset
[] = {
286 #define GETSETNAME(op, name) ks##op##_##name
287 GET (min
, "KSZ.min -> smallest allowed key size")
288 GET (max
, "KSZ.max -> largest allowed key size")
293 static const PyMemberDef keyszset_pymembers
[] = {
294 #define MEMBERSTRUCT keyszset_pyobj
295 MEMBER(set
, T_OBJECT
, READONLY
, "KSZ.set -> allowed key sizes")
300 static PyTypeObject keysz_pytype_skel
= {
301 PyObject_HEAD_INIT(0) 0, /* Header */
302 "KeySZ", /* @tp_name@ */
303 sizeof(keysz_pyobj
), /* @tp_basicsize@ */
304 0, /* @tp_itemsize@ */
306 0, /* @tp_dealloc@ */
308 0, /* @tp_getattr@ */
309 0, /* @tp_setattr@ */
310 0, /* @tp_compare@ */
312 0, /* @tp_as_number@ */
313 0, /* @tp_as_sequence@ */
314 0, /* @tp_as_mapping@ */
318 0, /* @tp_getattro@ */
319 0, /* @tp_setattro@ */
320 0, /* @tp_as_buffer@ */
321 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
325 "Key size constraints. Abstract.",
327 0, /* @tp_traverse@ */
329 0, /* @tp_richcompare@ */
330 0, /* @tp_weaklistoffset@ */
332 0, /* @tp_iternext@ */
333 PYMETHODS(keysz
), /* @tp_methods@ */
334 PYMEMBERS(keysz
), /* @tp_members@ */
338 0, /* @tp_descr_get@ */
339 0, /* @tp_descr_set@ */
340 0, /* @tp_dictoffset@ */
342 PyType_GenericAlloc
, /* @tp_alloc@ */
343 abstract_pynew
, /* @tp_new@ */
348 static PyTypeObject keyszany_pytype_skel
= {
349 PyObject_HEAD_INIT(0) 0, /* Header */
350 "KeySZAny", /* @tp_name@ */
351 sizeof(keysz_pyobj
), /* @tp_basicsize@ */
352 0, /* @tp_itemsize@ */
354 0, /* @tp_dealloc@ */
356 0, /* @tp_getattr@ */
357 0, /* @tp_setattr@ */
358 0, /* @tp_compare@ */
360 0, /* @tp_as_number@ */
361 0, /* @tp_as_sequence@ */
362 0, /* @tp_as_mapping@ */
366 0, /* @tp_getattro@ */
367 0, /* @tp_setattro@ */
368 0, /* @tp_as_buffer@ */
369 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
373 "KeySZAny(DEFAULT)\n"
374 " Key size constraints. This object imposes no constraints on size.",
376 0, /* @tp_traverse@ */
378 0, /* @tp_richcompare@ */
379 0, /* @tp_weaklistoffset@ */
381 0, /* @tp_iternext@ */
382 0, /* @tp_methods@ */
383 0, /* @tp_members@ */
384 PYGETSET(keyszany
), /* @tp_getset@ */
387 0, /* @tp_descr_get@ */
388 0, /* @tp_descr_set@ */
389 0, /* @tp_dictoffset@ */
391 PyType_GenericAlloc
, /* @tp_alloc@ */
392 keyszany_pynew
, /* @tp_new@ */
397 static PyTypeObject keyszrange_pytype_skel
= {
398 PyObject_HEAD_INIT(0) 0, /* Header */
399 "KeySZRange", /* @tp_name@ */
400 sizeof(keyszrange_pyobj
), /* @tp_basicsize@ */
401 0, /* @tp_itemsize@ */
403 0, /* @tp_dealloc@ */
405 0, /* @tp_getattr@ */
406 0, /* @tp_setattr@ */
407 0, /* @tp_compare@ */
409 0, /* @tp_as_number@ */
410 0, /* @tp_as_sequence@ */
411 0, /* @tp_as_mapping@ */
415 0, /* @tp_getattro@ */
416 0, /* @tp_setattro@ */
417 0, /* @tp_as_buffer@ */
418 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
422 "KeySZRange(DEFAULT, [min = 0], [max = 0], [mod = 1])\n"
423 " Key size constraints: size must be between MIN and MAX inclusive, and\n"
424 " be a multiple of MOD.",
426 0, /* @tp_traverse@ */
428 0, /* @tp_richcompare@ */
429 0, /* @tp_weaklistoffset@ */
431 0, /* @tp_iternext@ */
432 0, /* @tp_methods@ */
433 PYMEMBERS(keyszrange
), /* @tp_members@ */
437 0, /* @tp_descr_get@ */
438 0, /* @tp_descr_set@ */
439 0, /* @tp_dictoffset@ */
441 PyType_GenericAlloc
, /* @tp_alloc@ */
442 keyszrange_pynew
, /* @tp_new@ */
447 static PyTypeObject keyszset_pytype_skel
= {
448 PyObject_HEAD_INIT(0) 0, /* Header */
449 "KeySZSet", /* @tp_name@ */
450 sizeof(keyszset_pyobj
), /* @tp_basicsize@ */
451 0, /* @tp_itemsize@ */
453 0, /* @tp_dealloc@ */
455 0, /* @tp_getattr@ */
456 0, /* @tp_setattr@ */
457 0, /* @tp_compare@ */
459 0, /* @tp_as_number@ */
460 0, /* @tp_as_sequence@ */
461 0, /* @tp_as_mapping@ */
465 0, /* @tp_getattro@ */
466 0, /* @tp_setattro@ */
467 0, /* @tp_as_buffer@ */
468 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
472 "KeySZSet(DEFAULT, SEQ)\n"
473 " Key size constraints: size must be DEFAULT or an element of SEQ.",
475 0, /* @tp_traverse@ */
477 0, /* @tp_richcompare@ */
478 0, /* @tp_weaklistoffset@ */
480 0, /* @tp_iternext@ */
481 0, /* @tp_methods@ */
482 PYMEMBERS(keyszset
), /* @tp_members@ */
483 PYGETSET(keyszset
), /* @tp_getset@ */
486 0, /* @tp_descr_get@ */
487 0, /* @tp_descr_set@ */
488 0, /* @tp_dictoffset@ */
490 PyType_GenericAlloc
, /* @tp_alloc@ */
491 keyszset_pynew
, /* @tp_new@ */
496 /*----- Symmetric encryption ----------------------------------------------*/
498 static PyTypeObject
*gccipher_pytype
, *gcipher_pytype
;
500 typedef struct gccipher_pyobj
{
505 #define GCCIPHER_PYCHECK(o) PyObject_TypeCheck((o), gccipher_pytype)
506 #define GCCIPHER_CC(o) (((gccipher_pyobj *)(o))->cc)
508 typedef struct gcipher_pyobj
{
513 #define GCIPHER_PYCHECK(o) PyObject_TypeCheck((o), gcipher_pytype)
514 #define GCIPHER_C(o) (((gcipher_pyobj *)(o))->c)
516 CONVFUNC(gccipher
, gccipher
*, GCCIPHER_CC
)
518 static PyObject
*gcipher_pywrap(PyObject
*cobj
, gcipher
*c
)
521 if (!cobj
) cobj
= gccipher_pywrap((/*unconst*/ gccipher
*)GC_CLASS(c
));
522 else Py_INCREF(cobj
);
523 g
= PyObject_NEW(gcipher_pyobj
, (PyTypeObject
*)cobj
);
525 return ((PyObject
*)g
);
528 static PyObject
*gcipher_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
530 static const char *const kwlist
[] = { "k", 0 };
534 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", KWLIST
, &k
, &sz
))
536 if (keysz(sz
, GCCIPHER_CC(ty
)->keysz
) != sz
) VALERR("bad key length");
537 return (gcipher_pywrap((PyObject
*)ty
,
538 GC_INIT(GCCIPHER_CC(ty
), k
, sz
)));
543 PyObject
*gccipher_pywrap(gccipher
*cc
)
545 gccipher_pyobj
*g
= newtype(gccipher_pytype
, 0, cc
->name
);
547 g
->ty
.ht_type
.tp_basicsize
= sizeof(gcipher_pyobj
);
548 g
->ty
.ht_type
.tp_base
= gcipher_pytype
;
549 Py_INCREF(gcipher_pytype
);
550 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
551 Py_TPFLAGS_BASETYPE
|
552 Py_TPFLAGS_HEAPTYPE
);
553 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
554 g
->ty
.ht_type
.tp_free
= 0;
555 g
->ty
.ht_type
.tp_new
= gcipher_pynew
;
556 typeready(&g
->ty
.ht_type
);
557 return ((PyObject
*)g
);
560 static void gcipher_pydealloc(PyObject
*me
)
562 GC_DESTROY(GCIPHER_C(me
));
563 Py_DECREF(me
->ob_type
);
567 static PyObject
*gccget_name(PyObject
*me
, void *hunoz
)
568 { return (PyString_FromString(GCCIPHER_CC(me
)->name
)); }
570 static PyObject
*gccget_keysz(PyObject
*me
, void *hunoz
)
571 { return (keysz_pywrap(GCCIPHER_CC(me
)->keysz
)); }
573 static PyObject
*gccget_blksz(PyObject
*me
, void *hunoz
)
574 { return (PyInt_FromLong(GCCIPHER_CC(me
)->blksz
)); }
576 static PyObject
*gcmeth_encrypt(PyObject
*me
, PyObject
*arg
)
582 if (!PyArg_ParseTuple(arg
, "s#:encrypt", &p
, &sz
)) return (0);
583 rc
= bytestring_pywrap(0, sz
);
584 GC_ENCRYPT(GCIPHER_C(me
), p
, PyString_AS_STRING(rc
), sz
);
588 static PyObject
*gcmeth_enczero(PyObject
*me
, PyObject
*arg
)
594 if (!PyArg_ParseTuple(arg
, "i:enczero", &sz
)) return (0);
595 rc
= bytestring_pywrap(0, sz
);
596 p
= PyString_AS_STRING(rc
);
598 GC_ENCRYPT(GCIPHER_C(me
), p
, p
, sz
);
602 static PyObject
*gcmeth_decrypt(PyObject
*me
, PyObject
*arg
)
608 if (!PyArg_ParseTuple(arg
, "s#:decrypt", &p
, &sz
)) return (0);
609 rc
= bytestring_pywrap(0, sz
);
610 GC_DECRYPT(GCIPHER_C(me
), p
, PyString_AS_STRING(rc
), sz
);
614 static PyObject
*gcmeth_deczero(PyObject
*me
, PyObject
*arg
)
620 if (!PyArg_ParseTuple(arg
, "i:deczero", &sz
)) return (0);
621 rc
= bytestring_pywrap(0, sz
);
622 p
= PyString_AS_STRING(rc
);
624 GC_DECRYPT(GCIPHER_C(me
), p
, p
, sz
);
628 static PyObject
*gcmeth_setiv(PyObject
*me
, PyObject
*arg
)
633 if (!PyArg_ParseTuple(arg
, "s#:setiv", &p
, &sz
)) goto end
;
634 if (!GCIPHER_C(me
)->ops
->setiv
) VALERR("`setiv' not supported");
635 if (!GC_CLASS(GCIPHER_C(me
))->blksz
) VALERR("not a block cipher mode");
636 if (sz
!= GC_CLASS(GCIPHER_C(me
))->blksz
) VALERR("bad IV length");
637 GC_SETIV(GCIPHER_C(me
), p
);
643 static PyObject
*gcmeth_bdry(PyObject
*me
)
645 if (!GCIPHER_C(me
)->ops
->bdry
) VALERR("`bdry' not supported");
646 if (!GC_CLASS(GCIPHER_C(me
))->blksz
) VALERR("not a block cipher mode");
647 GC_BDRY(GCIPHER_C(me
));
653 static const PyGetSetDef gccipher_pygetset
[] = {
654 #define GETSETNAME(op, name) gcc##op##_##name
655 GET (keysz
, "CC.keysz -> acceptable key sizes")
656 GET (blksz
, "CC.blksz -> block size, or zero")
657 GET (name
, "CC.name -> name of this kind of cipher")
662 static const PyMethodDef gcipher_pymethods
[] = {
663 #define METHNAME(name) gcmeth_##name
664 METH (encrypt
, "C.encrypt(PT) -> CT")
665 METH (enczero
, "C.enczero(N) -> CT")
666 METH (decrypt
, "C.decrypt(CT) -> PT")
667 METH (deczero
, "C.deczero(N) -> PT")
668 METH (setiv
, "C.setiv(IV)")
669 NAMETH(bdry
, "C.bdry()")
674 static PyTypeObject gccipher_pytype_skel
= {
675 PyObject_HEAD_INIT(0) 0, /* Header */
676 "GCCipher", /* @tp_name@ */
677 sizeof(gccipher_pyobj
), /* @tp_basicsize@ */
678 0, /* @tp_itemsize@ */
680 0, /* @tp_dealloc@ */
682 0, /* @tp_getattr@ */
683 0, /* @tp_setattr@ */
684 0, /* @tp_compare@ */
686 0, /* @tp_as_number@ */
687 0, /* @tp_as_sequence@ */
688 0, /* @tp_as_mapping@ */
692 0, /* @tp_getattro@ */
693 0, /* @tp_setattro@ */
694 0, /* @tp_as_buffer@ */
695 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
699 "Symmetric cipher metaclass.",
701 0, /* @tp_traverse@ */
703 0, /* @tp_richcompare@ */
704 0, /* @tp_weaklistoffset@ */
706 0, /* @tp_iternext@ */
707 0, /* @tp_methods@ */
708 0, /* @tp_members@ */
709 PYGETSET(gccipher
), /* @tp_getset@ */
712 0, /* @tp_descr_get@ */
713 0, /* @tp_descr_set@ */
714 0, /* @tp_dictoffset@ */
716 PyType_GenericAlloc
, /* @tp_alloc@ */
717 abstract_pynew
, /* @tp_new@ */
722 static PyTypeObject gcipher_pytype_skel
= {
723 PyObject_HEAD_INIT(0) 0, /* Header */
724 "GCipher", /* @tp_name@ */
725 sizeof(gcipher_pyobj
), /* @tp_basicsize@ */
726 0, /* @tp_itemsize@ */
728 gcipher_pydealloc
, /* @tp_dealloc@ */
730 0, /* @tp_getattr@ */
731 0, /* @tp_setattr@ */
732 0, /* @tp_compare@ */
734 0, /* @tp_as_number@ */
735 0, /* @tp_as_sequence@ */
736 0, /* @tp_as_mapping@ */
740 0, /* @tp_getattro@ */
741 0, /* @tp_setattro@ */
742 0, /* @tp_as_buffer@ */
743 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
747 "Symmetric cipher, abstract base class.",
749 0, /* @tp_traverse@ */
751 0, /* @tp_richcompare@ */
752 0, /* @tp_weaklistoffset@ */
754 0, /* @tp_iternext@ */
755 PYMETHODS(gcipher
), /* @tp_methods@ */
756 0, /* @tp_members@ */
760 0, /* @tp_descr_get@ */
761 0, /* @tp_descr_set@ */
762 0, /* @tp_dictoffset@ */
764 PyType_GenericAlloc
, /* @tp_alloc@ */
765 abstract_pynew
, /* @tp_new@ */
770 /*----- Authenticated encryption ------------------------------------------*/
772 static PyTypeObject
*gcaead_pytype
, *gaeadkey_pytype
;
773 static PyTypeObject
*gcaeadaad_pytype
, *gaeadaad_pytype
;
774 static PyTypeObject
*gcaeadenc_pytype
, *gaeadenc_pytype
;
775 static PyTypeObject
*gcaeaddec_pytype
, *gaeaddec_pytype
;
777 typedef struct gcaead_pyobj
{
780 struct gcaeadaad_pyobj
*aad
;
781 struct gcaeadenc_pyobj
*enc
;
782 struct gcaeaddec_pyobj
*dec
;
785 #define GCAEAD_PYCHECK(o) PyObject_TypeCheck((o), gcaead_pytype)
786 #define GCAEAD_AEC(o) (((gcaead_pyobj *)(o))->aec)
787 #define GCAEAD_AAD(o) (((gcaead_pyobj *)(o))->aad)
788 #define GCAEAD_ENC(o) (((gcaead_pyobj *)(o))->enc)
789 #define GCAEAD_DEC(o) (((gcaead_pyobj *)(o))->dec)
790 static PyObject
*gcaead_pywrap(gcaead
*);
792 typedef struct gaeadkey_pyobj
{
797 #define GAEADKEY_PYCHECK(o) PyObject_TypeCheck((o), gaeadkey_pytype)
798 #define GAEADKEY_K(o) (((gaeadkey_pyobj *)(o))->k)
800 typedef struct gcaeadaad_pyobj
{
805 #define GCAEADAAD_KEY(o) (((gcaeadaad_pyobj *)(o))->key)
806 static PyObject
*gaeadaad_pywrap(PyObject
*, gaead_aad
*, unsigned, size_t);
808 typedef struct gaeadaad_pyobj
{
812 #define AEADF_DEAD 32768u
816 #define GAEADAAD_PYCHECK(o) PyObject_TypeCheck((o), gaeadaad_pytype)
817 #define GAEADAAD_A(o) (((gaeadaad_pyobj *)(o))->a)
818 #define GAEADAAD_F(o) (((gaeadaad_pyobj *)(o))->f)
819 #define GAEADAAD_HSZ(o) (((gaeadaad_pyobj *)(o))->hsz)
820 #define GAEADAAD_HLEN(o) (((gaeadaad_pyobj *)(o))->hlen)
822 typedef struct gcaeadenc_pyobj
{
827 #define GCAEADENC_KEY(o) (((gcaeadenc_pyobj *)(o))->key)
828 static PyObject
*gaeadenc_pywrap(PyObject
*, gaead_enc
*, unsigned,
829 size_t, size_t, size_t);
831 typedef struct gaeadenc_pyobj
{
836 size_t hsz
, msz
, tsz
;
840 #define GAEADENC_PYCHECK(o) PyObject_TypeCheck((o), gaeadenc_pytype)
841 #define GAEADENC_AAD(o) (((gaeadenc_pyobj *)(o))->aad)
842 #define GAEADENC_E(o) (((gaeadenc_pyobj *)(o))->e)
843 #define GAEADENC_F(o) (((gaeadenc_pyobj *)(o))->f)
844 #define GAEADENC_HSZ(o) (((gaeadenc_pyobj *)(o))->hsz)
845 #define GAEADENC_MSZ(o) (((gaeadenc_pyobj *)(o))->msz)
846 #define GAEADENC_TSZ(o) (((gaeadenc_pyobj *)(o))->tsz)
847 #define GAEADENC_MLEN(o) (((gaeadenc_pyobj *)(o))->mlen)
849 typedef struct gcaeaddec_pyobj
{
854 #define GCAEADDEC_KEY(o) (((gcaeaddec_pyobj *)(o))->key)
855 static PyObject
*gaeaddec_pywrap(PyObject
*, gaead_dec
*, unsigned,
856 size_t, size_t, size_t);
858 typedef struct gaeaddec_pyobj
{
863 size_t hsz
, csz
, tsz
;
867 #define GAEADDEC_PYCHECK(o) PyObject_TypeCheck((o), gaeaddec_pytype)
868 #define GAEADDEC_AAD(o) (((gaeaddec_pyobj *)(o))->aad)
869 #define GAEADDEC_D(o) (((gaeaddec_pyobj *)(o))->d)
870 #define GAEADDEC_F(o) (((gaeaddec_pyobj *)(o))->f)
871 #define GAEADDEC_HSZ(o) (((gaeaddec_pyobj *)(o))->hsz)
872 #define GAEADDEC_CSZ(o) (((gaeaddec_pyobj *)(o))->csz)
873 #define GAEADDEC_TSZ(o) (((gaeaddec_pyobj *)(o))->tsz)
874 #define GAEADDEC_CLEN(o) (((gaeaddec_pyobj *)(o))->clen)
876 static PyObject
*gaeadkey_pywrap(PyObject
*cobj
, gaead_key
*k
)
880 if (!cobj
) cobj
= gcaead_pywrap((/*unconst*/ gcaead
*)GAEAD_CLASS(k
));
881 else Py_INCREF(cobj
);
882 gk
= PyObject_NEW(gaeadkey_pyobj
, (PyTypeObject
*)cobj
);
884 return ((PyObject
*)gk
);
887 static PyObject
*gaeadkey_pynew(PyTypeObject
*ty
,
888 PyObject
*arg
, PyObject
*kw
)
890 static const char *const kwlist
[] = { "k", 0 };
894 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", KWLIST
, &k
, &sz
))
896 if (keysz(sz
, GCAEAD_AEC(ty
)->keysz
) != sz
) VALERR("bad key length");
897 return (gaeadkey_pywrap((PyObject
*)ty
,
898 GAEAD_KEY(GCAEAD_AEC(ty
), k
, sz
)));
903 static PyObject
*gcaead_pywrap(gcaead
*aec
)
906 gcaeadaad_pyobj
*gca
;
907 gcaeadenc_pyobj
*gce
;
908 gcaeaddec_pyobj
*gcd
;
910 #define MKTYPE(obj, thing, newfn, namefmt) do { \
911 (obj) = newtype(gcaead_pytype, 0, 0); \
912 (obj)->ty.ht_name = PyString_FromFormat(namefmt, aec->name); \
913 (obj)->ty.ht_type.tp_name = PyString_AS_STRING((obj)->ty.ht_name); \
914 (obj)->ty.ht_type.tp_basicsize = sizeof(gaead##thing##_pyobj); \
915 (obj)->ty.ht_type.tp_base = gaead##thing##_pytype; \
916 Py_INCREF(gaead##thing##_pytype); \
917 (obj)->ty.ht_type.tp_flags = \
918 (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE); \
919 (obj)->ty.ht_type.tp_alloc = PyType_GenericAlloc; \
920 (obj)->ty.ht_type.tp_free = 0; \
921 (obj)->ty.ht_type.tp_new = newfn; \
922 typeready(&(obj)->ty.ht_type); \
925 MKTYPE(gck
, key
, gaeadkey_pynew
, "%s(key)");
926 MKTYPE(gca
, aad
, abstract_pynew
, "%s(aad-hash)");
927 MKTYPE(gce
, enc
, abstract_pynew
, "%s(encrypt)");
928 MKTYPE(gcd
, dec
, abstract_pynew
, "%s(decrypt)");
932 gck
->aec
= aec
; gck
->aad
= gca
; gck
->enc
= gce
; gck
->dec
= gcd
;
933 gca
->key
= gce
->key
= gcd
->key
= gck
;
934 return ((PyObject
*)gck
);
937 static void gaeadkey_pydealloc(PyObject
*me
)
938 { GAEAD_DESTROY(GAEADKEY_K(me
)); Py_DECREF(me
->ob_type
); FREEOBJ(me
); }
940 static PyObject
*gcaeget_name(PyObject
*me
, void *hunoz
)
941 { return (PyString_FromString(GCAEAD_AEC(me
)->name
)); }
943 static PyObject
*gcaeget_keysz(PyObject
*me
, void *hunoz
)
944 { return (keysz_pywrap(GCAEAD_AEC(me
)->keysz
)); }
946 static PyObject
*gcaeget_noncesz(PyObject
*me
, void *hunoz
)
947 { return (keysz_pywrap(GCAEAD_AEC(me
)->noncesz
)); }
949 static PyObject
*gcaeget_tagsz(PyObject
*me
, void *hunoz
)
950 { return (keysz_pywrap(GCAEAD_AEC(me
)->tagsz
)); }
952 static PyObject
*gcaeget_blksz(PyObject
*me
, void *hunoz
)
953 { return (PyInt_FromLong(GCAEAD_AEC(me
)->blksz
)); }
955 static PyObject
*gcaeget_bufsz(PyObject
*me
, void *hunoz
)
956 { return (PyInt_FromLong(GCAEAD_AEC(me
)->bufsz
)); }
958 static PyObject
*gcaeget_ohd(PyObject
*me
, void *hunoz
)
959 { return (PyInt_FromLong(GCAEAD_AEC(me
)->ohd
)); }
961 static PyObject
*gcaeget_flags(PyObject
*me
, void *hunoz
)
962 { return (PyInt_FromLong(GCAEAD_AEC(me
)->f
)); }
964 static const PyGetSetDef gcaead_pygetset
[] = {
965 #define GETSETNAME(op, name) gcae##op##_##name
966 GET (keysz
, "AEC.keysz -> acceptable key sizes")
967 GET (noncesz
, "AEC.noncesz -> acceptable nonce sizes")
968 GET (tagsz
, "AEC.tagsz -> acceptable tag sizes")
969 GET (blksz
, "AEC.blksz -> block size, or zero")
970 GET (bufsz
, "AEC.bufsz -> amount of data buffered internally")
971 GET (ohd
, "AEC.ohd -> maximum encryption overhead")
972 GET (name
, "AEC.name -> name of this kind of AEAD scheme")
973 GET (flags
, "AEC.flags -> mask of `AEADF_...' flags")
978 static PyObject
*gaekmeth_aad(PyObject
*me
)
980 const gaead_key
*k
= GAEADKEY_K(me
);
983 if (k
->ops
->c
->f
&AEADF_AADNDEP
)
984 VALERR("aad must be associated with enc/dec op");
985 rc
= gaeadaad_pywrap((PyObject
*)GCAEAD_AAD(me
->ob_type
),
991 static int check_aead_encdec(const gcaead
*aec
, unsigned *f_out
, size_t nsz
,
992 PyObject
*hszobj
, size_t *hsz_out
,
993 PyObject
*mszobj
, size_t *msz_out
,
994 PyObject
*tszobj
, size_t *tsz_out
)
996 unsigned f
= 0, miss
;
999 if (hszobj
!= Py_None
)
1000 { f
|= AEADF_PCHSZ
; if (!convszt(hszobj
, hsz_out
)) goto end
; }
1001 if (mszobj
!= Py_None
)
1002 { f
|= AEADF_PCMSZ
; if (!convszt(mszobj
, msz_out
)) goto end
; }
1003 if (tszobj
!= Py_None
)
1004 { f
|= AEADF_PCTSZ
; if (!convszt(tszobj
, tsz_out
)) goto end
; }
1006 if (miss
&AEADF_PCHSZ
) VALERR("header length precommitment required");
1007 if (miss
&AEADF_PCMSZ
) VALERR("message length precommitment required");
1008 if (miss
&AEADF_PCTSZ
) VALERR("tag length precommitment required");
1009 if (keysz(nsz
, aec
->noncesz
) != nsz
) VALERR("bad nonce length");
1010 if (tszobj
!= Py_None
&& keysz(*tsz_out
, aec
->tagsz
) != *tsz_out
)
1011 VALERR("bad tag length");
1012 *f_out
= f
| aec
->f
; rc
= 0;
1017 static PyObject
*gaekmeth_enc(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
1019 static const char *const kwlist
[] = { "nonce", "hsz", "msz", "tsz", 0 };
1020 const gaead_key
*k
= GAEADKEY_K(me
);
1023 char *n
; Py_ssize_t nsz
;
1024 PyObject
*hszobj
= Py_None
, *mszobj
= Py_None
, *tszobj
= Py_None
;
1025 size_t hsz
= 0, msz
= 0, tsz
= 0;
1028 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#|OOO:enc", KWLIST
,
1029 &n
, &nsz
, &hszobj
, &mszobj
, &tszobj
))
1031 if (check_aead_encdec(k
->ops
->c
, &f
, nsz
,
1032 hszobj
, &hsz
, mszobj
, &msz
, tszobj
, &tsz
))
1034 e
= GAEAD_ENC(GAEADKEY_K(me
), n
, nsz
, hsz
, msz
, tsz
);
1035 if (!e
) VALERR("bad aead parameter combination");
1036 rc
= gaeadenc_pywrap((PyObject
*)GCAEAD_ENC(me
->ob_type
),
1037 e
, f
, hsz
, msz
, tsz
);
1042 static PyObject
*gaekmeth_dec(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
1044 static const char *const kwlist
[] = { "nonce", "hsz", "csz", "tsz", 0 };
1045 const gaead_key
*k
= GAEADKEY_K(me
);
1048 char *n
; Py_ssize_t nsz
;
1049 PyObject
*hszobj
= Py_None
, *cszobj
= Py_None
, *tszobj
= Py_None
;
1050 size_t hsz
= 0, csz
= 0, tsz
= 0;
1053 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#|OOO:dec", KWLIST
,
1054 &n
, &nsz
, &hszobj
, &cszobj
, &tszobj
))
1056 if (check_aead_encdec(k
->ops
->c
, &f
, nsz
,
1057 hszobj
, &hsz
, cszobj
, &csz
, tszobj
, &tsz
))
1059 d
= GAEAD_DEC(GAEADKEY_K(me
), n
, nsz
, hsz
, csz
, tsz
);
1060 if (!d
) VALERR("bad aead parameter combination");
1061 rc
= gaeaddec_pywrap((PyObject
*)GCAEAD_DEC(me
->ob_type
),
1062 d
, f
, hsz
, csz
, tsz
);
1067 static const PyMethodDef gaeadkey_pymethods
[] = {
1068 #define METHNAME(name) gaekmeth_##name
1069 NAMETH(aad
, "KEY.aad() -> AAD")
1070 KWMETH(enc
, "KEY.enc(NONCE, [hsz], [msz], [tsz]) -> ENC")
1071 KWMETH(dec
, "KEY.dec(NONCE, [hsz], [csz], [tsz]) -> DEC")
1076 static PyObject
*gaeadaad_pywrap(PyObject
*cobj
, gaead_aad
*a
,
1077 unsigned f
, size_t hsz
)
1081 assert(cobj
); Py_INCREF(cobj
);
1082 ga
= PyObject_NEW(gaeadaad_pyobj
, (PyTypeObject
*)cobj
);
1083 ga
->a
= a
; ga
->f
= f
; ga
->hsz
= hsz
; ga
->hlen
= 0;
1084 return ((PyObject
*)ga
);
1087 static void gaeadaad_pydealloc(PyObject
*me
)
1089 gaeadaad_pyobj
*ga
= (gaeadaad_pyobj
*)me
;
1091 if (ga
->a
) GAEAD_DESTROY(ga
->a
);
1092 Py_DECREF(me
->ob_type
); FREEOBJ(me
);
1095 static int gaea_check(PyObject
*me
)
1097 gaeadaad_pyobj
*ga
= (gaeadaad_pyobj
*)me
;
1100 if ((ga
->f
&AEADF_DEAD
) || !ga
->a
) VALERR("aad object no longer active");
1106 static void gaea_invalidate(gaeadaad_pyobj
*ga
)
1107 { if (ga
) ga
->f
|= AEADF_DEAD
; }
1109 static void gaea_sever(gaeadaad_pyobj
**ga_inout
)
1111 gaeadaad_pyobj
*ga
= *ga_inout
;
1112 if (ga
) { ga
->f
|= AEADF_DEAD
; ga
->a
= 0; Py_DECREF(ga
); *ga_inout
= 0; }
1115 static PyObject
*gaeaget_hsz(PyObject
*me
, void *hunoz
)
1117 if (gaea_check(me
)) return (0);
1118 else if (GAEADAAD_F(me
)&AEADF_PCHSZ
) return getulong(GAEADAAD_HSZ(me
));
1122 static PyObject
*gaeaget_hlen(PyObject
*me
, void *hunoz
)
1123 { return (gaea_check(me
) ?
0 : getulong(GAEADAAD_HLEN(me
))); }
1125 static const PyGetSetDef gaeadaad_pygetset
[] = {
1126 #define GETSETNAME(op, name) gaea##op##_##name
1127 GET (hsz
, "AAD.hsz -> precommitted header length or `None'")
1128 GET (hlen
, "AAD.hlen -> header length so far")
1133 static PyObject
*gaeameth_copy(PyObject
*me
)
1137 if (gaea_check(me
)) goto end
;
1138 if (GAEADAAD_F(me
)&AEADF_AADNDEP
)
1139 VALERR("can't duplicate nonce-dependent aad");
1140 rc
= gaeadaad_pywrap((PyObject
*)me
->ob_type
,
1141 GAEAD_DUP(GAEADAAD_A(me
)), 0, 0);
1142 GAEADAAD_HLEN(rc
) = GAEADAAD_HLEN(me
);
1147 static int gaeadaad_hash(PyObject
*me
, const void *h
, size_t hsz
)
1149 gaeadaad_pyobj
*ga
= (gaeadaad_pyobj
*)me
;
1152 if (gaea_check(me
)) goto end
;
1153 if ((ga
->f
&AEADF_NOAAD
) && hsz
)
1154 VALERR("header data not permitted");
1155 if ((ga
->f
&AEADF_PCHSZ
) && hsz
> ga
->hsz
- ga
->hlen
)
1156 VALERR("too large for precommitted header length");
1157 GAEAD_HASH(ga
->a
, h
, hsz
); ga
->hlen
+= hsz
;
1164 static PyObject
*gaeameth_hash(PyObject
*me
, PyObject
*arg
)
1166 char *h
; Py_ssize_t hsz
;
1168 if (!PyArg_ParseTuple(arg
, "s#:hash", &h
, &hsz
)) return (0);
1169 if (gaeadaad_hash(me
, h
, hsz
)) return (0);
1173 #define GAEAMETH_HASHU_(n, W, w) \
1174 static PyObject *gaeameth_hashu##w(PyObject *me, PyObject *arg) \
1176 uint##n x; octet b[SZ_##W]; \
1177 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \
1178 STORE##W(b, x); if (gaeadaad_hash(me, b, sizeof(b))) return (0); \
1181 DOUINTCONV(GAEAMETH_HASHU_
)
1183 #define GAEAMETH_HASHBUF_(n, W, w) \
1184 static PyObject *gaeameth_hashbuf##w(PyObject *me, PyObject *arg) \
1186 char *p; Py_ssize_t sz; octet b[SZ_##W]; \
1187 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
1188 if (sz > MASK##n) TYERR("string too long"); \
1189 STORE##W(b, sz); if (gaeadaad_hash(me, b, sizeof(b))) goto end; \
1190 if (gaeadaad_hash(me, p, sz)) goto end; \
1195 DOUINTCONV(GAEAMETH_HASHBUF_
)
1197 static PyObject
*gaeameth_hashstrz(PyObject
*me
, PyObject
*arg
)
1200 if (!PyArg_ParseTuple(arg
, "s:hashstrz", &p
)) return (0);
1201 if (gaeadaad_hash(me
, p
, strlen(p
) + 1)) return (0);
1205 static const PyMethodDef gaeadaad_pymethods
[] = {
1206 #define METHNAME(name) gaeameth_##name
1207 NAMETH(copy
, "AAD.copy() -> AAD'")
1208 METH (hash
, "AAD.hash(H)")
1209 #define METHU_(n, W, w) METH(hashu##w, "AAD.hashu" #w "(WORD)")
1212 #define METHBUF_(n, W, w) METH(hashbuf##w, "AAD.hashbuf" #w "(BYTES)")
1213 DOUINTCONV(METHBUF_
)
1215 METH (hashstrz
, "AAD.hashstrz(STRING)")
1220 static PyObject
*gaeadenc_pywrap(PyObject
*cobj
, gaead_enc
*e
, unsigned f
,
1221 size_t hsz
, size_t msz
, size_t tsz
)
1225 assert(cobj
); Py_INCREF(cobj
);
1226 ge
= PyObject_NEW(gaeadenc_pyobj
, (PyTypeObject
*)cobj
);
1227 ge
->e
= e
; ge
->f
= f
; ge
->hsz
= hsz
; ge
->msz
= msz
; ge
->tsz
= tsz
;
1228 ge
->aad
= 0; ge
->mlen
= 0;
1229 return ((PyObject
*)ge
);
1232 static void gaeadenc_pydealloc(PyObject
*me
)
1234 gaeadenc_pyobj
*ge
= (gaeadenc_pyobj
*)me
;
1236 gaea_sever(&ge
->aad
); GAEAD_DESTROY(ge
->e
);
1237 Py_DECREF(me
->ob_type
); FREEOBJ(me
);
1240 static PyObject
*gaeeget_hsz(PyObject
*me
, void *hunoz
)
1242 if (GAEADENC_F(me
)&AEADF_PCHSZ
) return getulong(GAEADENC_HSZ(me
));
1246 static PyObject
*gaeeget_msz(PyObject
*me
, void *hunoz
)
1248 if (GAEADENC_F(me
)&AEADF_PCMSZ
) return getulong(GAEADENC_MSZ(me
));
1252 static PyObject
*gaeeget_tsz(PyObject
*me
, void *hunoz
)
1254 if (GAEADENC_F(me
)&AEADF_PCTSZ
) return getulong(GAEADENC_TSZ(me
));
1258 static PyObject
*gaeeget_mlen(PyObject
*me
, void *hunoz
)
1259 { return getulong(GAEADENC_MLEN(me
)); }
1261 static const PyGetSetDef gaeadenc_pygetset
[] = {
1262 #define GETSETNAME(op, name) gaee##op##_##name
1263 GET (hsz
, "ENC.hsz -> precommitted header length or `None'")
1264 GET (msz
, "ENC.msz -> precommitted message length or `None'")
1265 GET (tsz
, "ENC.tsz -> precommitted tag length or `None'")
1266 GET (mlen
, "ENC.mlen -> message length so far")
1271 static PyObject
*gaeemeth_aad(PyObject
*me
)
1273 gaeadenc_pyobj
*ge
= (gaeadenc_pyobj
*)me
;
1276 if (!(ge
->f
&AEADF_AADNDEP
))
1277 rc
= gaeadaad_pywrap((PyObject
*)GCAEADENC_KEY(ge
->ob_type
)->aad
,
1278 GAEAD_AAD(ge
->e
), 0, 0);
1280 if ((ge
->f
&AEADF_AADFIRST
) && ge
->mlen
)
1281 VALERR("too late for aad");
1283 ge
->aad
= (gaeadaad_pyobj
*)
1284 gaeadaad_pywrap((PyObject
*)GCAEADENC_KEY(ge
->ob_type
)->aad
,
1285 GAEAD_AAD(ge
->e
), ge
->f
&(AEADF_PCHSZ
| AEADF_NOAAD
),
1288 rc
= (PyObject
*)ge
->aad
;
1294 static PyObject
*gaeemeth_reinit(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
1296 static const char *const kwlist
[] = { "nonce", "hsz", "msz", "tsz", 0 };
1297 gaeadenc_pyobj
*ge
= (gaeadenc_pyobj
*)me
;
1298 char *n
; Py_ssize_t nsz
;
1299 PyObject
*hszobj
= Py_None
, *mszobj
= Py_None
, *tszobj
= Py_None
;
1300 size_t hsz
= 0, msz
= 0, tsz
= 0;
1303 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#|OOO:enc", KWLIST
,
1304 &n
, &nsz
, &hszobj
, &mszobj
, &tszobj
))
1306 if (check_aead_encdec(ge
->e
->ops
->c
, &f
, nsz
,
1307 hszobj
, &hsz
, mszobj
, &msz
, tszobj
, &tsz
))
1309 if (GAEAD_REINIT(ge
->e
, n
, nsz
, hsz
, msz
, tsz
))
1310 VALERR("bad aead parameter combination");
1311 gaea_sever(&ge
->aad
);
1312 ge
->f
= f
; ge
->hsz
= hsz
; ge
->msz
= msz
; ge
->tsz
= tsz
;
1317 static PyObject
*gaeemeth_encrypt(PyObject
*me
, PyObject
*arg
)
1319 gaeadenc_pyobj
*ge
= (gaeadenc_pyobj
*)me
;
1320 char *m
; Py_ssize_t msz
;
1321 char *c
= 0; size_t csz
; buf b
;
1325 if (!PyArg_ParseTuple(arg
, "s#:encrypt", &m
, &msz
)) goto end
;
1326 if (ge
->f
&AEADF_AADFIRST
) {
1327 if ((ge
->f
&AEADF_PCHSZ
) && (ge
->aad ? ge
->aad
->hlen
: 0) != ge
->hsz
)
1328 VALERR("header doesn't match precommitted length");
1329 gaea_invalidate(ge
->aad
);
1331 if ((ge
->f
&AEADF_PCMSZ
) && msz
> ge
->msz
- ge
->mlen
)
1332 VALERR("too large for precommitted message length");
1333 csz
= msz
+ ge
->e
->ops
->c
->bufsz
; c
= xmalloc(csz
); buf_init(&b
, c
, csz
);
1334 err
= GAEAD_ENCRYPT(ge
->e
, m
, msz
, &b
); assert(!err
); (void)err
;
1335 buf_flip(&b
); rc
= bytestring_pywrapbuf(&b
); ge
->mlen
+= msz
;
1341 static PyObject
*gaeemeth_done(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
1343 static const char *const kwlist
[] = { "tsz", "aad", 0 };
1344 gaeadenc_pyobj
*ge
= (gaeadenc_pyobj
*)me
;
1345 PyObject
*aad
= Py_None
;
1346 char *c
= 0; size_t csz
; buf b
;
1347 PyObject
*tszobj
= Py_None
; PyObject
*tag
; size_t tsz
;
1351 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|OO:done", KWLIST
,
1354 if (tszobj
!= Py_None
&& !convszt(tszobj
, &tsz
)) goto end
;
1355 if (aad
!= Py_None
&&
1356 !PyObject_TypeCheck(aad
,
1357 (PyTypeObject
*)GCAEADENC_KEY(me
->ob_type
)->aad
))
1358 TYERR("wanted aad");
1359 if ((ge
->f
&AEADF_AADNDEP
) && aad
!= Py_None
&& aad
!= (PyObject
*)ge
->aad
)
1360 VALERR("mismatched aad");
1361 if ((ge
->f
&AEADF_PCHSZ
) &&
1362 (aad
== Py_None ?
0 : GAEADAAD_HLEN(aad
)) != ge
->hsz
)
1363 VALERR("header doesn't match precommitted length");
1364 if ((ge
->f
&AEADF_PCMSZ
) && ge
->mlen
!= ge
->msz
)
1365 VALERR("message doesn't match precommitted length");
1366 if (tszobj
== Py_None
) {
1367 if (ge
->f
&AEADF_PCTSZ
) tsz
= ge
->tsz
;
1368 else tsz
= keysz(0, ge
->e
->ops
->c
->tagsz
);
1370 if ((ge
->f
&AEADF_PCTSZ
) && tsz
!= ge
->tsz
)
1371 VALERR("tag length doesn't match precommitted value");
1372 if (keysz(tsz
, ge
->e
->ops
->c
->tagsz
) != tsz
) VALERR("bad tag length");
1374 csz
= ge
->e
->ops
->c
->bufsz
; c
= xmalloc(csz
); buf_init(&b
, c
, csz
);
1375 tag
= bytestring_pywrap(0, tsz
);
1376 err
= GAEAD_DONE(ge
->e
, aad
== Py_None ?
0 : GAEADAAD_A(aad
), &b
,
1377 PyString_AS_STRING(tag
), tsz
);
1378 assert(!err
); (void)err
;
1379 buf_flip(&b
); rc
= Py_BuildValue("NN", bytestring_pywrapbuf(&b
), tag
);
1385 static const PyMethodDef gaeadenc_pymethods
[] = {
1386 #define METHNAME(name) gaeemeth_##name
1387 NAMETH(aad
, "ENC.aad() -> AAD")
1388 KWMETH(reinit
, "ENC.reinit(NONCE, [hsz], [msz], [tsz])")
1389 METH (encrypt
, "ENC.encrypt(MSG) -> CT")
1390 KWMETH(done
, "ENC.done([tsz], [aad]) -> CT, TAG")
1395 static PyObject
*gaeaddec_pywrap(PyObject
*cobj
, gaead_dec
*d
, unsigned f
,
1396 size_t hsz
, size_t csz
, size_t tsz
)
1399 assert(cobj
); Py_INCREF(cobj
);
1400 gd
= PyObject_NEW(gaeaddec_pyobj
, (PyTypeObject
*)cobj
);
1401 gd
->d
= d
; gd
->f
= f
; gd
->hsz
= hsz
; gd
->csz
= csz
; gd
->tsz
= tsz
;
1402 gd
->aad
= 0; gd
->clen
= 0;
1403 return ((PyObject
*)gd
);
1406 static void gaeaddec_pydealloc(PyObject
*me
)
1408 gaeaddec_pyobj
*gd
= (gaeaddec_pyobj
*)me
;
1410 gaea_sever(&gd
->aad
); GAEAD_DESTROY(GAEADDEC_D(me
));
1411 Py_DECREF(me
->ob_type
); FREEOBJ(me
);
1414 static PyObject
*gaedget_hsz(PyObject
*me
, void *hunoz
)
1416 if (GAEADDEC_F(me
)&AEADF_PCHSZ
) return getulong(GAEADDEC_HSZ(me
));
1420 static PyObject
*gaedget_csz(PyObject
*me
, void *hunoz
)
1422 if (GAEADDEC_F(me
)&AEADF_PCMSZ
) return getulong(GAEADDEC_CSZ(me
));
1426 static PyObject
*gaedget_tsz(PyObject
*me
, void *hunoz
)
1428 if (GAEADDEC_F(me
)&AEADF_PCTSZ
) return getulong(GAEADDEC_TSZ(me
));
1432 static PyObject
*gaedget_clen(PyObject
*me
, void *hunoz
)
1433 { return getulong(GAEADDEC_CLEN(me
)); }
1435 static const PyGetSetDef gaeaddec_pygetset
[] = {
1436 #define GETSETNAME(op, name) gaed##op##_##name
1437 GET (hsz
, "DEC.hsz -> precommitted header length or `None'")
1438 GET (csz
, "DEC.csz -> precommitted ciphertext length or `None'")
1439 GET (tsz
, "DEC.tsz -> precommitted tag length or `None'")
1440 GET (clen
, "DEC.clen -> ciphertext length so far")
1445 static PyObject
*gaedmeth_aad(PyObject
*me
)
1447 gaeaddec_pyobj
*gd
= (gaeaddec_pyobj
*)me
;
1449 if (!(gd
->f
&AEADF_AADNDEP
))
1450 return (gaeadaad_pywrap((PyObject
*)GCAEADDEC_KEY(gd
->ob_type
)->aad
,
1451 GAEAD_AAD(gd
->d
), 0, 0));
1454 gd
->aad
= (gaeadaad_pyobj
*)
1455 gaeadaad_pywrap((PyObject
*)GCAEADENC_KEY(gd
->ob_type
)->aad
,
1456 GAEAD_AAD(gd
->d
), gd
->f
&(AEADF_PCHSZ
| AEADF_NOAAD
),
1459 return ((PyObject
*)gd
->aad
);
1463 static PyObject
*gaedmeth_reinit(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
1465 static const char *const kwlist
[] = { "nonce", "hsz", "csz", "tsz", 0 };
1466 gaeaddec_pyobj
*gd
= (gaeaddec_pyobj
*)me
;
1467 char *n
; Py_ssize_t nsz
;
1468 PyObject
*hszobj
= Py_None
, *cszobj
= Py_None
, *tszobj
= Py_None
;
1469 size_t hsz
= 0, csz
= 0, tsz
= 0;
1472 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#|OOO:enc", KWLIST
,
1473 &n
, &nsz
, &hszobj
, &cszobj
, &tszobj
))
1475 if (check_aead_encdec(gd
->d
->ops
->c
, &f
, nsz
,
1476 hszobj
, &hsz
, cszobj
, &csz
, tszobj
, &tsz
))
1478 if (GAEAD_REINIT(gd
->d
, n
, nsz
, hsz
, csz
, tsz
))
1479 VALERR("bad aead parameter combination");
1480 gaea_sever(&gd
->aad
);
1481 gd
->f
= f
; gd
->hsz
= hsz
; gd
->csz
= csz
; gd
->tsz
= tsz
;
1486 static PyObject
*gaedmeth_decrypt(PyObject
*me
, PyObject
*arg
)
1488 gaeaddec_pyobj
*gd
= (gaeaddec_pyobj
*)me
;
1489 char *c
; Py_ssize_t csz
;
1490 char *m
= 0; size_t msz
; buf b
;
1494 if (!PyArg_ParseTuple(arg
, "s#:decrypt", &c
, &csz
)) goto end
;
1495 if (gd
->f
&AEADF_AADFIRST
) {
1496 if ((gd
->f
&AEADF_PCHSZ
) && (gd
->aad ? gd
->aad
->hlen
: 0) != gd
->hsz
)
1497 VALERR("header doesn't match precommitted length");
1498 gaea_invalidate(gd
->aad
);
1500 if ((gd
->f
&AEADF_PCMSZ
) && csz
> gd
->csz
- gd
->clen
)
1501 VALERR("too large for precommitted message length");
1502 msz
= csz
+ gd
->d
->ops
->c
->bufsz
; m
= xmalloc(msz
); buf_init(&b
, m
, msz
);
1503 err
= GAEAD_DECRYPT(gd
->d
, c
, csz
, &b
); assert(!err
); (void)err
;
1504 buf_flip(&b
); rc
= bytestring_pywrapbuf(&b
); gd
->clen
+= csz
;
1510 static PyObject
*gaedmeth_done(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
1512 static const char *const kwlist
[] = { "tag", "aad", 0 };
1513 gaeaddec_pyobj
*gd
= (gaeaddec_pyobj
*)me
;
1514 PyObject
*aad
= Py_None
;
1515 char *t
; Py_ssize_t tsz
;
1516 char *m
= 0; size_t msz
; buf b
;
1520 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#|O:done", KWLIST
,
1523 if (aad
!= Py_None
&&
1524 !PyObject_TypeCheck(aad
,
1525 (PyTypeObject
*)GCAEADENC_KEY(me
->ob_type
)->aad
))
1526 TYERR("wanted aad");
1527 if ((gd
->f
&AEADF_AADNDEP
) && aad
!= Py_None
&& aad
!= (PyObject
*)gd
->aad
)
1528 VALERR("mismatched aad");
1529 if ((gd
->f
&AEADF_PCHSZ
) &&
1530 (aad
== Py_None ?
0 : GAEADAAD_HLEN(aad
)) != gd
->hsz
)
1531 VALERR("header doesn't match precommitted length");
1532 if ((gd
->f
&AEADF_PCMSZ
) && gd
->clen
!= gd
->csz
)
1533 VALERR("message doesn't match precommitted length");
1534 if ((gd
->f
&AEADF_PCTSZ
) && tsz
!= gd
->tsz
)
1535 VALERR("tag length doesn't match precommitted value");
1536 if (keysz(tsz
, gd
->d
->ops
->c
->tagsz
) != tsz
) VALERR("bad tag length");
1537 msz
= gd
->d
->ops
->c
->bufsz
; m
= xmalloc(msz
); buf_init(&b
, m
, msz
);
1538 err
= GAEAD_DONE(gd
->d
, aad
== Py_None ?
0 : GAEADAAD_A(aad
), &b
, t
, tsz
);
1540 if (!err
) VALERR("decryption failed");
1541 buf_flip(&b
); rc
= bytestring_pywrapbuf(&b
);
1547 static const PyMethodDef gaeaddec_pymethods
[] = {
1548 #define METHNAME(name) gaedmeth_##name
1549 NAMETH(aad
, "DEC.aad() -> AAD")
1550 KWMETH(reinit
, "DEC.reinit(NONCE, [hsz], [csz], [tsz])")
1551 METH (decrypt
, "DEC.decrypt(CT) -> MSG")
1552 KWMETH(done
, "DEC.done(TAG, [aad]) -> MSG | None")
1557 static PyTypeObject gcaead_pytype_skel
= {
1558 PyObject_HEAD_INIT(0) 0, /* Header */
1559 "GCAEAD", /* @tp_name@ */
1560 sizeof(gcaead_pyobj
), /* @tp_basicsize@ */
1561 0, /* @tp_itemsize@ */
1563 0, /* @tp_dealloc@ */
1565 0, /* @tp_getattr@ */
1566 0, /* @tp_setattr@ */
1567 0, /* @tp_compare@ */
1569 0, /* @tp_as_number@ */
1570 0, /* @tp_as_sequence@ */
1571 0, /* @tp_as_mapping@ */
1575 0, /* @tp_getattro@ */
1576 0, /* @tp_setattro@ */
1577 0, /* @tp_as_buffer@ */
1578 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1579 Py_TPFLAGS_BASETYPE
,
1582 "Authenticated encryption (key) metaclass.",
1584 0, /* @tp_traverse@ */
1586 0, /* @tp_richcompare@ */
1587 0, /* @tp_weaklistoffset@ */
1589 0, /* @tp_iternext@ */
1590 0, /* @tp_methods@ */
1591 0, /* @tp_members@ */
1592 PYGETSET(gcaead
), /* @tp_getset@ */
1595 0, /* @tp_descr_get@ */
1596 0, /* @tp_descr_set@ */
1597 0, /* @tp_dictoffset@ */
1599 PyType_GenericAlloc
, /* @tp_alloc@ */
1600 abstract_pynew
, /* @tp_new@ */
1605 static PyTypeObject gaeadkey_pytype_skel
= {
1606 PyObject_HEAD_INIT(0) 0, /* Header */
1607 "GAEKey", /* @tp_name@ */
1608 sizeof(gaeadkey_pyobj
), /* @tp_basicsize@ */
1609 0, /* @tp_itemsize@ */
1611 gaeadkey_pydealloc
, /* @tp_dealloc@ */
1613 0, /* @tp_getattr@ */
1614 0, /* @tp_setattr@ */
1615 0, /* @tp_compare@ */
1617 0, /* @tp_as_number@ */
1618 0, /* @tp_as_sequence@ */
1619 0, /* @tp_as_mapping@ */
1623 0, /* @tp_getattro@ */
1624 0, /* @tp_setattro@ */
1625 0, /* @tp_as_buffer@ */
1626 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1627 Py_TPFLAGS_BASETYPE
,
1630 "Authenticated encryption key.",
1632 0, /* @tp_traverse@ */
1634 0, /* @tp_richcompare@ */
1635 0, /* @tp_weaklistoffset@ */
1637 0, /* @tp_iternext@ */
1638 PYMETHODS(gaeadkey
), /* @tp_methods@ */
1639 0, /* @tp_members@ */
1640 0, /* @tp_getset@ */
1643 0, /* @tp_descr_get@ */
1644 0, /* @tp_descr_set@ */
1645 0, /* @tp_dictoffset@ */
1647 PyType_GenericAlloc
, /* @tp_alloc@ */
1648 abstract_pynew
, /* @tp_new@ */
1653 static PyTypeObject gcaeadaad_pytype_skel
= {
1654 PyObject_HEAD_INIT(0) 0, /* Header */
1655 "GAEAADClass", /* @tp_name@ */
1656 sizeof(gcaeadaad_pyobj
), /* @tp_basicsize@ */
1657 0, /* @tp_itemsize@ */
1659 0, /* @tp_dealloc@ */
1661 0, /* @tp_getattr@ */
1662 0, /* @tp_setattr@ */
1663 0, /* @tp_compare@ */
1665 0, /* @tp_as_number@ */
1666 0, /* @tp_as_sequence@ */
1667 0, /* @tp_as_mapping@ */
1671 0, /* @tp_getattro@ */
1672 0, /* @tp_setattro@ */
1673 0, /* @tp_as_buffer@ */
1674 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1675 Py_TPFLAGS_BASETYPE
,
1678 "Authenticated encryption additional-data hash metaclass.",
1680 0, /* @tp_traverse@ */
1682 0, /* @tp_richcompare@ */
1683 0, /* @tp_weaklistoffset@ */
1685 0, /* @tp_iternext@ */
1686 0, /* @tp_methods@ */
1687 0, /* @tp_members@ */
1688 0, /* @tp_getset@ */
1691 0, /* @tp_descr_get@ */
1692 0, /* @tp_descr_set@ */
1693 0, /* @tp_dictoffset@ */
1695 PyType_GenericAlloc
, /* @tp_alloc@ */
1696 abstract_pynew
, /* @tp_new@ */
1701 static PyTypeObject gaeadaad_pytype_skel
= {
1702 PyObject_HEAD_INIT(0) 0, /* Header */
1703 "GAEAAD", /* @tp_name@ */
1704 sizeof(gaeadaad_pyobj
), /* @tp_basicsize@ */
1705 0, /* @tp_itemsize@ */
1707 gaeadaad_pydealloc
, /* @tp_dealloc@ */
1709 0, /* @tp_getattr@ */
1710 0, /* @tp_setattr@ */
1711 0, /* @tp_compare@ */
1713 0, /* @tp_as_number@ */
1714 0, /* @tp_as_sequence@ */
1715 0, /* @tp_as_mapping@ */
1719 0, /* @tp_getattro@ */
1720 0, /* @tp_setattro@ */
1721 0, /* @tp_as_buffer@ */
1722 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1723 Py_TPFLAGS_BASETYPE
,
1726 "Authenticated encryption AAD hash.",
1728 0, /* @tp_traverse@ */
1730 0, /* @tp_richcompare@ */
1731 0, /* @tp_weaklistoffset@ */
1733 0, /* @tp_iternext@ */
1734 PYMETHODS(gaeadaad
), /* @tp_methods@ */
1735 0, /* @tp_members@ */
1736 PYGETSET(gaeadaad
), /* @tp_getset@ */
1739 0, /* @tp_descr_get@ */
1740 0, /* @tp_descr_set@ */
1741 0, /* @tp_dictoffset@ */
1743 PyType_GenericAlloc
, /* @tp_alloc@ */
1744 abstract_pynew
, /* @tp_new@ */
1749 static PyTypeObject gcaeadenc_pytype_skel
= {
1750 PyObject_HEAD_INIT(0) 0, /* Header */
1751 "GAEEncClass", /* @tp_name@ */
1752 sizeof(gcaeadenc_pyobj
), /* @tp_basicsize@ */
1753 0, /* @tp_itemsize@ */
1755 0, /* @tp_dealloc@ */
1757 0, /* @tp_getattr@ */
1758 0, /* @tp_setattr@ */
1759 0, /* @tp_compare@ */
1761 0, /* @tp_as_number@ */
1762 0, /* @tp_as_sequence@ */
1763 0, /* @tp_as_mapping@ */
1767 0, /* @tp_getattro@ */
1768 0, /* @tp_setattro@ */
1769 0, /* @tp_as_buffer@ */
1770 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1771 Py_TPFLAGS_BASETYPE
,
1774 "Authenticated encryption operation metaclass.",
1776 0, /* @tp_traverse@ */
1778 0, /* @tp_richcompare@ */
1779 0, /* @tp_weaklistoffset@ */
1781 0, /* @tp_iternext@ */
1782 0, /* @tp_methods@ */
1783 0, /* @tp_members@ */
1784 0, /* @tp_getset@ */
1787 0, /* @tp_descr_get@ */
1788 0, /* @tp_descr_set@ */
1789 0, /* @tp_dictoffset@ */
1791 PyType_GenericAlloc
, /* @tp_alloc@ */
1792 abstract_pynew
, /* @tp_new@ */
1797 static PyTypeObject gaeadenc_pytype_skel
= {
1798 PyObject_HEAD_INIT(0) 0, /* Header */
1799 "GAEEnc", /* @tp_name@ */
1800 sizeof(gaeadenc_pyobj
), /* @tp_basicsize@ */
1801 0, /* @tp_itemsize@ */
1803 gaeadenc_pydealloc
, /* @tp_dealloc@ */
1805 0, /* @tp_getattr@ */
1806 0, /* @tp_setattr@ */
1807 0, /* @tp_compare@ */
1809 0, /* @tp_as_number@ */
1810 0, /* @tp_as_sequence@ */
1811 0, /* @tp_as_mapping@ */
1815 0, /* @tp_getattro@ */
1816 0, /* @tp_setattro@ */
1817 0, /* @tp_as_buffer@ */
1818 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1819 Py_TPFLAGS_BASETYPE
,
1822 "Authenticated encryption operation.",
1824 0, /* @tp_traverse@ */
1826 0, /* @tp_richcompare@ */
1827 0, /* @tp_weaklistoffset@ */
1829 0, /* @tp_iternext@ */
1830 PYMETHODS(gaeadenc
), /* @tp_methods@ */
1831 0, /* @tp_members@ */
1832 PYGETSET(gaeadenc
), /* @tp_getset@ */
1835 0, /* @tp_descr_get@ */
1836 0, /* @tp_descr_set@ */
1837 0, /* @tp_dictoffset@ */
1839 PyType_GenericAlloc
, /* @tp_alloc@ */
1840 abstract_pynew
, /* @tp_new@ */
1845 static PyTypeObject gcaeaddec_pytype_skel
= {
1846 PyObject_HEAD_INIT(0) 0, /* Header */
1847 "GAEDecClass", /* @tp_name@ */
1848 sizeof(gcaeaddec_pyobj
), /* @tp_basicsize@ */
1849 0, /* @tp_itemsize@ */
1851 0, /* @tp_dealloc@ */
1853 0, /* @tp_getattr@ */
1854 0, /* @tp_setattr@ */
1855 0, /* @tp_compare@ */
1857 0, /* @tp_as_number@ */
1858 0, /* @tp_as_sequence@ */
1859 0, /* @tp_as_mapping@ */
1863 0, /* @tp_getattro@ */
1864 0, /* @tp_setattro@ */
1865 0, /* @tp_as_buffer@ */
1866 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1867 Py_TPFLAGS_BASETYPE
,
1870 "Authenticated decryption operation metaclass.",
1872 0, /* @tp_traverse@ */
1874 0, /* @tp_richcompare@ */
1875 0, /* @tp_weaklistoffset@ */
1877 0, /* @tp_iternext@ */
1878 0, /* @tp_methods@ */
1879 0, /* @tp_members@ */
1880 0, /* @tp_getset@ */
1883 0, /* @tp_descr_get@ */
1884 0, /* @tp_descr_set@ */
1885 0, /* @tp_dictoffset@ */
1887 PyType_GenericAlloc
, /* @tp_alloc@ */
1888 abstract_pynew
, /* @tp_new@ */
1893 static PyTypeObject gaeaddec_pytype_skel
= {
1894 PyObject_HEAD_INIT(0) 0, /* Header */
1895 "GAEDec", /* @tp_name@ */
1896 sizeof(gaeaddec_pyobj
), /* @tp_basicsize@ */
1897 0, /* @tp_itemsize@ */
1899 gaeaddec_pydealloc
, /* @tp_dealloc@ */
1901 0, /* @tp_getattr@ */
1902 0, /* @tp_setattr@ */
1903 0, /* @tp_compare@ */
1905 0, /* @tp_as_number@ */
1906 0, /* @tp_as_sequence@ */
1907 0, /* @tp_as_mapping@ */
1911 0, /* @tp_getattro@ */
1912 0, /* @tp_setattro@ */
1913 0, /* @tp_as_buffer@ */
1914 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1915 Py_TPFLAGS_BASETYPE
,
1918 "Authenticated decryption operation.",
1920 0, /* @tp_traverse@ */
1922 0, /* @tp_richcompare@ */
1923 0, /* @tp_weaklistoffset@ */
1925 0, /* @tp_iternext@ */
1926 PYMETHODS(gaeaddec
), /* @tp_methods@ */
1927 0, /* @tp_members@ */
1928 PYGETSET(gaeaddec
), /* @tp_getset@ */
1931 0, /* @tp_descr_get@ */
1932 0, /* @tp_descr_set@ */
1933 0, /* @tp_dictoffset@ */
1935 PyType_GenericAlloc
, /* @tp_alloc@ */
1936 abstract_pynew
, /* @tp_new@ */
1941 /*----- Hash functions ----------------------------------------------------*/
1943 PyTypeObject
*gchash_pytype
;
1944 static PyTypeObject
*ghash_pytype
;
1945 PyObject
*sha_pyobj
, *has160_pyobj
;
1947 typedef struct ghash_pyobj
{
1952 #define GHASH_PYCHECK(o) PyObject_TypeCheck((o), ghash_pytype)
1953 #define GHASH_H(o) (((ghash_pyobj *)(o))->h)
1955 CONVFUNC(gchash
, gchash
*, GCHASH_CH
)
1956 CONVFUNC(ghash
, ghash
*, GHASH_H
)
1958 static PyObject
*ghash_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1960 static const char *const kwlist
[] = { 0 };
1961 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, ":new", KWLIST
))
1963 return (ghash_pywrap((PyObject
*)ty
, GH_INIT(GCHASH_CH(ty
))));
1968 static PyObject
*gchash_pywrap(gchash
*ch
)
1970 gchash_pyobj
*g
= newtype(gchash_pytype
, 0, ch
->name
);
1972 g
->ty
.ht_type
.tp_basicsize
= sizeof(ghash_pyobj
);
1973 g
->ty
.ht_type
.tp_base
= ghash_pytype
;
1974 Py_INCREF(ghash_pytype
);
1975 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
1976 Py_TPFLAGS_BASETYPE
|
1977 Py_TPFLAGS_HEAPTYPE
);
1978 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
1979 g
->ty
.ht_type
.tp_free
= 0;
1980 g
->ty
.ht_type
.tp_new
= ghash_pynew
;
1981 typeready(&g
->ty
.ht_type
);
1982 return ((PyObject
*)g
);
1985 PyObject
*ghash_pywrap(PyObject
*cobj
, ghash
*h
)
1988 if (!cobj
) cobj
= gchash_pywrap((/*unconst*/ gchash
*)GH_CLASS(h
));
1989 else Py_INCREF(cobj
);
1990 g
= PyObject_NEW(ghash_pyobj
, (PyTypeObject
*)cobj
);
1992 return ((PyObject
*)g
);
1995 static void ghash_pydealloc(PyObject
*me
)
1997 GH_DESTROY(GHASH_H(me
));
1998 Py_DECREF(me
->ob_type
);
2002 static PyObject
*gchget_name(PyObject
*me
, void *hunoz
)
2003 { return (PyString_FromString(GCHASH_CH(me
)->name
)); }
2005 static PyObject
*gchget_hashsz(PyObject
*me
, void *hunoz
)
2006 { return (PyInt_FromLong(GCHASH_CH(me
)->hashsz
)); }
2008 static PyObject
*gchget_bufsz(PyObject
*me
, void *hunoz
)
2009 { return (PyInt_FromLong(GCHASH_CH(me
)->bufsz
)); }
2011 static PyObject
*ghmeth_copy(PyObject
*me
)
2012 { return (ghash_pywrap((PyObject
*)me
->ob_type
, GH_COPY(GHASH_H(me
)))); }
2014 static PyObject
*ghmeth_hash(PyObject
*me
, PyObject
*arg
)
2018 if (!PyArg_ParseTuple(arg
, "s#:hash", &p
, &sz
)) return (0);
2019 GH_HASH(GHASH_H(me
), p
, sz
);
2023 #define GHMETH_HASHU_(n, W, w) \
2024 static PyObject *ghmeth_hashu##w(PyObject *me, PyObject *arg) \
2027 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \
2028 GH_HASHU##W(GHASH_H(me), x); \
2031 DOUINTCONV(GHMETH_HASHU_
)
2033 #define GHMETH_HASHBUF_(n, W, w) \
2034 static PyObject *ghmeth_hashbuf##w(PyObject *me, PyObject *arg) \
2038 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
2039 if (sz > MASK##n) TYERR("string too long"); \
2040 GH_HASHBUF##W(GHASH_H(me), p, sz); \
2045 DOUINTCONV(GHMETH_HASHBUF_
)
2047 static PyObject
*ghmeth_hashstrz(PyObject
*me
, PyObject
*arg
)
2050 if (!PyArg_ParseTuple(arg
, "s:hashstrz", &p
)) return (0);
2051 GH_HASHSTRZ(GHASH_H(me
), p
);
2055 static PyObject
*ghmeth_done(PyObject
*me
)
2059 g
= GH_COPY(GHASH_H(me
));
2060 rc
= bytestring_pywrap(0, g
->ops
->c
->hashsz
);
2061 GH_DONE(g
, PyString_AS_STRING(rc
));
2066 static const PyGetSetDef gchash_pygetset
[] = {
2067 #define GETSETNAME(op, name) gch##op##_##name
2068 GET (bufsz
, "CH.bufsz -> hash buffer size, or zero")
2069 GET (hashsz
, "CH.hashsz -> hash output size")
2070 GET (name
, "CH.name -> name of this kind of hash")
2075 static const PyMethodDef ghash_pymethods
[] = {
2076 #define METHNAME(name) ghmeth_##name
2077 NAMETH(copy
, "H.copy() -> HH")
2078 METH (hash
, "H.hash(M)")
2079 #define METHU_(n, W, w) METH(hashu##w, "H.hashu" #w "(WORD)")
2082 #define METHBUF_(n, W, w) METH(hashbuf##w, "H.hashbuf" #w "(BYTES)")
2083 DOUINTCONV(METHBUF_
)
2085 METH (hashstrz
, "H.hashstrz(STRING)")
2086 NAMETH(done
, "H.done() -> HASH")
2091 static PyTypeObject gchash_pytype_skel
= {
2092 PyObject_HEAD_INIT(0) 0, /* Header */
2093 "GCHash", /* @tp_name@ */
2094 sizeof(gchash_pyobj
), /* @tp_basicsize@ */
2095 0, /* @tp_itemsize@ */
2097 0, /* @tp_dealloc@ */
2099 0, /* @tp_getattr@ */
2100 0, /* @tp_setattr@ */
2101 0, /* @tp_compare@ */
2103 0, /* @tp_as_number@ */
2104 0, /* @tp_as_sequence@ */
2105 0, /* @tp_as_mapping@ */
2109 0, /* @tp_getattro@ */
2110 0, /* @tp_setattro@ */
2111 0, /* @tp_as_buffer@ */
2112 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2113 Py_TPFLAGS_BASETYPE
,
2116 "Hash function metaclass.",
2118 0, /* @tp_traverse@ */
2120 0, /* @tp_richcompare@ */
2121 0, /* @tp_weaklistoffset@ */
2123 0, /* @tp_iternext@ */
2124 0, /* @tp_methods@ */
2125 0, /* @tp_members@ */
2126 PYGETSET(gchash
), /* @tp_getset@ */
2129 0, /* @tp_descr_get@ */
2130 0, /* @tp_descr_set@ */
2131 0, /* @tp_dictoffset@ */
2133 PyType_GenericAlloc
, /* @tp_alloc@ */
2134 abstract_pynew
, /* @tp_new@ */
2139 static PyTypeObject ghash_pytype_skel
= {
2140 PyObject_HEAD_INIT(0) 0, /* Header */
2141 "GHash", /* @tp_name@ */
2142 sizeof(ghash_pyobj
), /* @tp_basicsize@ */
2143 0, /* @tp_itemsize@ */
2145 ghash_pydealloc
, /* @tp_dealloc@ */
2147 0, /* @tp_getattr@ */
2148 0, /* @tp_setattr@ */
2149 0, /* @tp_compare@ */
2151 0, /* @tp_as_number@ */
2152 0, /* @tp_as_sequence@ */
2153 0, /* @tp_as_mapping@ */
2157 0, /* @tp_getattro@ */
2158 0, /* @tp_setattro@ */
2159 0, /* @tp_as_buffer@ */
2160 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2161 Py_TPFLAGS_BASETYPE
,
2164 "Hash function, abstract base class.",
2166 0, /* @tp_traverse@ */
2168 0, /* @tp_richcompare@ */
2169 0, /* @tp_weaklistoffset@ */
2171 0, /* @tp_iternext@ */
2172 PYMETHODS(ghash
), /* @tp_methods@ */
2173 0, /* @tp_members@ */
2174 0, /* @tp_getset@ */
2177 0, /* @tp_descr_get@ */
2178 0, /* @tp_descr_set@ */
2179 0, /* @tp_dictoffset@ */
2181 PyType_GenericAlloc
, /* @tp_alloc@ */
2182 abstract_pynew
, /* @tp_new@ */
2187 /*----- Message authentication --------------------------------------------*/
2189 static PyTypeObject
*gcmac_pytype
, *gmac_pytype
, *gmhash_pytype
;
2191 typedef struct gcmac_pyobj
{
2192 PyHeapTypeObject ty
;
2196 #define GCMAC_PYCHECK(o) PyObject_TypeCheck((o), gcmac_pytype)
2197 #define GCMAC_CM(o) (((gcmac_pyobj *)(o))->cm)
2198 #define GCMAC_F(o) (((gcmac_pyobj *)(o))->f)
2199 CONVFUNC(gcmac
, gcmac
*, GCMAC_CM
)
2200 static PyObject
*gmac_pywrap(PyObject
*, gmac
*);
2202 typedef struct gmac_pyobj
{
2203 PyHeapTypeObject ty
;
2207 extern PyTypeObject
*gmac_pytype
;
2208 #define GMAC_PYCHECK(o) PyObject_TypeCheck((o), gmac_pytype)
2209 #define GMAC_M(o) (((gmac_pyobj *)(o))->m)
2210 #define GMAC_F(o) (((gmac_pyobj *)(o))->f)
2211 extern PyObject
*gmac_pywrap(PyObject
*, gmac
*);
2212 extern int convgmac(PyObject
*, void *);
2214 static PyObject
*gmac_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
2216 static const char *const kwlist
[] = { "k", 0 };
2220 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", KWLIST
, &k
, &sz
))
2222 if (keysz(sz
, GCMAC_CM(ty
)->keysz
) != sz
) VALERR("bad key length");
2223 return (gmac_pywrap((PyObject
*)ty
,
2224 GM_KEY(GCMAC_CM(ty
), k
, sz
)));
2229 static PyObject
*gmhash_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
2231 static const char *const kwlist
[] = { 0 };
2234 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, ":new", KWLIST
)) return (0);
2235 g
= PyObject_NEW(ghash_pyobj
, ty
);
2236 g
->h
= GM_INIT(GMAC_M(ty
));
2238 return ((PyObject
*)g
);
2241 static PyObject
*gcmac_pywrap(gcmac
*cm
)
2243 gcmac_pyobj
*g
= newtype(gcmac_pytype
, 0, cm
->name
);
2245 g
->ty
.ht_type
.tp_basicsize
= sizeof(gmac_pyobj
);
2246 g
->ty
.ht_type
.tp_base
= gmac_pytype
;
2247 Py_INCREF(gmac_pytype
);
2248 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
2249 Py_TPFLAGS_BASETYPE
|
2250 Py_TPFLAGS_HEAPTYPE
);
2251 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
2252 g
->ty
.ht_type
.tp_free
= 0;
2253 g
->ty
.ht_type
.tp_new
= gmac_pynew
;
2254 typeready(&g
->ty
.ht_type
);
2255 return ((PyObject
*)g
);
2258 static PyObject
*gmac_pywrap(PyObject
*cobj
, gmac
*m
)
2261 if (!cobj
) cobj
= gcmac_pywrap((/*unconst*/ gcmac
*)GM_CLASS(m
));
2262 else Py_INCREF(cobj
);
2263 g
= newtype((PyTypeObject
*)cobj
, 0, 0);
2264 g
->ty
.ht_type
.tp_basicsize
= sizeof(ghash_pyobj
);
2265 g
->ty
.ht_name
= PyString_FromFormat("%s(keyed)", m
->ops
->c
->name
);
2266 g
->ty
.ht_type
.tp_name
= PyString_AS_STRING(g
->ty
.ht_name
);
2267 g
->ty
.ht_type
.tp_base
= gmhash_pytype
;
2268 Py_INCREF(gmac_pytype
);
2269 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
2270 Py_TPFLAGS_BASETYPE
|
2271 Py_TPFLAGS_HEAPTYPE
);
2272 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
2273 g
->ty
.ht_type
.tp_free
= 0;
2274 g
->ty
.ht_type
.tp_new
= gmhash_pynew
;
2275 typeready(&g
->ty
.ht_type
);
2277 return ((PyObject
*)g
);
2280 static void gmac_pydealloc(PyObject
*me
)
2282 GM_DESTROY(GMAC_M(me
));
2283 Py_DECREF(me
->ob_type
);
2284 PyType_Type
.tp_dealloc(me
);
2287 static PyObject
*gcmget_name(PyObject
*me
, void *hunoz
)
2288 { return (PyString_FromString(GCMAC_CM(me
)->name
)); }
2290 static PyObject
*gcmget_keysz(PyObject
*me
, void *hunoz
)
2291 { return (keysz_pywrap(GCMAC_CM(me
)->keysz
)); }
2293 static PyObject
*gcmget_tagsz(PyObject
*me
, void *hunoz
)
2294 { return (PyInt_FromLong(GCMAC_CM(me
)->hashsz
)); }
2296 static const PyGetSetDef gcmac_pygetset
[] = {
2297 #define GETSETNAME(op, name) gcm##op##_##name
2298 GET (keysz
, "CM.keysz -> acceptable key sizes")
2299 GET (tagsz
, "CM.tagsz -> MAC output size")
2300 GET (name
, "CM.name -> name of this kind of MAC")
2305 static PyTypeObject gcmac_pytype_skel
= {
2306 PyObject_HEAD_INIT(0) 0, /* Header */
2307 "GCMAC", /* @tp_name@ */
2308 sizeof(gchash_pyobj
), /* @tp_basicsize@ */
2309 0, /* @tp_itemsize@ */
2311 0, /* @tp_dealloc@ */
2313 0, /* @tp_getattr@ */
2314 0, /* @tp_setattr@ */
2315 0, /* @tp_compare@ */
2317 0, /* @tp_as_number@ */
2318 0, /* @tp_as_sequence@ */
2319 0, /* @tp_as_mapping@ */
2323 0, /* @tp_getattro@ */
2324 0, /* @tp_setattro@ */
2325 0, /* @tp_as_buffer@ */
2326 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2327 Py_TPFLAGS_BASETYPE
,
2330 "Message authentication code metametaclass.",
2332 0, /* @tp_traverse@ */
2334 0, /* @tp_richcompare@ */
2335 0, /* @tp_weaklistoffset@ */
2337 0, /* @tp_iternext@ */
2338 0, /* @tp_methods@ */
2339 0, /* @tp_members@ */
2340 PYGETSET(gcmac
), /* @tp_getset@ */
2343 0, /* @tp_descr_get@ */
2344 0, /* @tp_descr_set@ */
2345 0, /* @tp_dictoffset@ */
2347 PyType_GenericAlloc
, /* @tp_alloc@ */
2348 abstract_pynew
, /* @tp_new@ */
2353 static PyTypeObject gmac_pytype_skel
= {
2354 PyObject_HEAD_INIT(0) 0, /* Header */
2355 "GMAC", /* @tp_name@ */
2356 sizeof(gmac_pyobj
), /* @tp_basicsize@ */
2357 0, /* @tp_itemsize@ */
2359 gmac_pydealloc
, /* @tp_dealloc@ */
2361 0, /* @tp_getattr@ */
2362 0, /* @tp_setattr@ */
2363 0, /* @tp_compare@ */
2365 0, /* @tp_as_number@ */
2366 0, /* @tp_as_sequence@ */
2367 0, /* @tp_as_mapping@ */
2371 0, /* @tp_getattro@ */
2372 0, /* @tp_setattro@ */
2373 0, /* @tp_as_buffer@ */
2374 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2375 Py_TPFLAGS_BASETYPE
,
2378 "Message authentication code metaclass, abstract base class.",
2380 0, /* @tp_traverse@ */
2382 0, /* @tp_richcompare@ */
2383 0, /* @tp_weaklistoffset@ */
2385 0, /* @tp_iternext@ */
2386 0, /* @tp_methods@ */
2387 0, /* @tp_members@ */
2388 0, /* @tp_getset@ */
2391 0, /* @tp_descr_get@ */
2392 0, /* @tp_descr_set@ */
2393 0, /* @tp_dictoffset@ */
2395 PyType_GenericAlloc
, /* @tp_alloc@ */
2396 abstract_pynew
, /* @tp_new@ */
2401 static PyTypeObject gmhash_pytype_skel
= {
2402 PyObject_HEAD_INIT(0) 0, /* Header */
2403 "GMACHash", /* @tp_name@ */
2404 sizeof(ghash_pyobj
), /* @tp_basicsize@ */
2405 0, /* @tp_itemsize@ */
2407 ghash_pydealloc
, /* @tp_dealloc@ */
2409 0, /* @tp_getattr@ */
2410 0, /* @tp_setattr@ */
2411 0, /* @tp_compare@ */
2413 0, /* @tp_as_number@ */
2414 0, /* @tp_as_sequence@ */
2415 0, /* @tp_as_mapping@ */
2419 0, /* @tp_getattro@ */
2420 0, /* @tp_setattro@ */
2421 0, /* @tp_as_buffer@ */
2422 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2423 Py_TPFLAGS_BASETYPE
,
2426 "Message authentication code, abstract base class.",
2428 0, /* @tp_traverse@ */
2430 0, /* @tp_richcompare@ */
2431 0, /* @tp_weaklistoffset@ */
2433 0, /* @tp_iternext@ */
2434 0, /* @tp_methods@ */
2435 0, /* @tp_members@ */
2436 0, /* @tp_getset@ */
2439 0, /* @tp_descr_get@ */
2440 0, /* @tp_descr_set@ */
2441 0, /* @tp_dictoffset@ */
2443 PyType_GenericAlloc
, /* @tp_alloc@ */
2444 abstract_pynew
, /* @tp_new@ */
2449 /*----- Special snowflake for Poly1305 ------------------------------------*/
2451 PyTypeObject
*poly1305cls_pytype
, *poly1305key_pytype
, *poly1305hash_pytype
;
2453 typedef struct poly1305key_pyobj
{
2454 PyHeapTypeObject ty
;
2456 } poly1305key_pyobj
;
2458 typedef struct poly1305hash_pyobj
{
2463 } poly1305hash_pyobj
;
2465 #define P1305_F(o) (((poly1305hash_pyobj *)(o))->f)
2466 #define P1305_CTX(o) (&((poly1305hash_pyobj *)(o))->ctx)
2467 CONVFUNC(poly1305hash
, poly1305_ctx
*, P1305_CTX
)
2469 static PyObject
*poly1305hash_pynew(PyTypeObject
*ty
,
2470 PyObject
*arg
, PyObject
*kw
)
2472 static const char *const kwlist
[] = { "mask", 0 };
2473 poly1305key_pyobj
*pk
= (poly1305key_pyobj
*)ty
;
2474 poly1305hash_pyobj
*ph
;
2478 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|s#:new", KWLIST
, &m
, &sz
))
2480 if (m
&& sz
!= POLY1305_MASKSZ
) VALERR("bad mask length");
2481 ph
= PyObject_NEW(poly1305hash_pyobj
, ty
);
2483 if (m
) ph
->f
|= f_mask
;
2484 poly1305_macinit(&ph
->ctx
, &pk
->k
, m
);
2486 return ((PyObject
*)ph
);
2491 static PyObject
*poly1305key_pynew(PyTypeObject
*ty
,
2492 PyObject
*arg
, PyObject
*kw
)
2494 static const char *const kwlist
[] = { "k", 0 };
2495 poly1305key_pyobj
*pk
;
2499 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", KWLIST
, &k
, &sz
))
2501 if (keysz(sz
, poly1305_keysz
) != sz
) VALERR("bad key length");
2503 pk
= newtype(ty
, 0, 0);
2504 pk
->ty
.ht_name
= PyString_FromString("poly1305(keyed)");
2505 pk
->ty
.ht_type
.tp_basicsize
= sizeof(poly1305hash_pyobj
);
2506 pk
->ty
.ht_type
.tp_name
= PyString_AS_STRING(pk
->ty
.ht_name
);
2507 pk
->ty
.ht_type
.tp_base
= poly1305hash_pytype
;
2508 Py_INCREF(poly1305key_pytype
);
2509 pk
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
2510 Py_TPFLAGS_BASETYPE
|
2511 Py_TPFLAGS_HEAPTYPE
);
2512 pk
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
2513 pk
->ty
.ht_type
.tp_free
= 0;
2514 pk
->ty
.ht_type
.tp_new
= poly1305hash_pynew
;
2515 typeready(&pk
->ty
.ht_type
);
2517 poly1305_keyinit(&pk
->k
, k
, sz
);
2518 return ((PyObject
*)pk
);
2524 static PyObject
*poly1305clsget_name(PyObject
*me
, void *hunoz
)
2525 { return (PyString_FromString("poly1305")); }
2527 static PyObject
*poly1305clsget_keysz(PyObject
*me
, void *hunoz
)
2528 { return (keysz_pywrap(poly1305_keysz
)); }
2530 static PyObject
*poly1305clsget_masksz(PyObject
*me
, void *hunoz
)
2531 { return (PyInt_FromLong(POLY1305_MASKSZ
)); }
2533 static PyObject
*poly1305clsget_tagsz(PyObject
*me
, void *hunoz
)
2534 { return (PyInt_FromLong(POLY1305_TAGSZ
)); }
2536 static PyObject
*polymeth_copy(PyObject
*me
)
2538 poly1305hash_pyobj
*ph
;
2539 ph
= PyObject_NEW(poly1305hash_pyobj
, me
->ob_type
);
2540 poly1305_copy(&ph
->ctx
, P1305_CTX(me
));
2541 Py_INCREF(me
->ob_type
);
2542 return ((PyObject
*)ph
);
2545 static PyObject
*polymeth_hash(PyObject
*me
, PyObject
*arg
)
2549 if (!PyArg_ParseTuple(arg
, "s#:hash", &p
, &sz
)) return (0);
2550 poly1305_hash(P1305_CTX(me
), p
, sz
);
2554 #define POLYMETH_HASHU_(n, W, w) \
2555 static PyObject *polymeth_hashu##w(PyObject *me, PyObject *arg) \
2559 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \
2560 STORE##W(b, x); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \
2563 DOUINTCONV(POLYMETH_HASHU_
)
2565 #define POLYMETH_HASHBUF_(n, W, w) \
2566 static PyObject *polymeth_hashbuf##w(PyObject *me, PyObject *arg) \
2571 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
2572 if (sz > MASK##n) TYERR("string too long"); \
2573 STORE##W(b, sz); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \
2574 poly1305_hash(P1305_CTX(me), p, sz); \
2579 DOUINTCONV(POLYMETH_HASHBUF_
)
2581 static PyObject
*polymeth_hashstrz(PyObject
*me
, PyObject
*arg
)
2584 if (!PyArg_ParseTuple(arg
, "s:hashstrz", &p
)) return (0);
2585 poly1305_hash(P1305_CTX(me
), p
, strlen(p
) + 1);
2589 static PyObject
*polymeth_flush(PyObject
*me
)
2590 { poly1305_flush(P1305_CTX(me
)); RETURN_ME
; }
2592 static PyObject
*polymeth_flushzero(PyObject
*me
)
2593 { poly1305_flushzero(P1305_CTX(me
)); RETURN_ME
; }
2595 static PyObject
*polymeth_concat(PyObject
*me
, PyObject
*arg
)
2597 PyObject
*pre
, *suff
;
2598 if (!PyArg_ParseTuple(arg
, "OO:concat", &pre
, &suff
)) return (0);
2599 if (!PyObject_TypeCheck(pre
, poly1305hash_pytype
) ||
2600 !PyObject_TypeCheck(suff
, poly1305hash_pytype
))
2601 TYERR("wanted a poly1305hash");
2602 if (me
->ob_type
!= pre
->ob_type
|| me
->ob_type
!= suff
->ob_type
)
2603 TYERR("key mismatch");
2604 if (P1305_CTX(pre
)->nbuf
) VALERR("prefix is not block-aligned");
2605 poly1305_concat(P1305_CTX(me
), P1305_CTX(pre
), P1305_CTX(suff
));
2611 static PyObject
*polymeth_done(PyObject
*me
)
2614 if (!(P1305_F(me
) & f_mask
)) VALERR("no mask");
2615 rc
= bytestring_pywrap(0, POLY1305_TAGSZ
);
2616 poly1305_done(P1305_CTX(me
), PyString_AS_STRING(rc
));
2622 static const PyGetSetDef poly1305cls_pygetset
[] = {
2623 #define GETSETNAME(op, name) poly1305cls##op##_##name
2624 GET (keysz
, "PC.keysz -> acceptable key sizes")
2625 GET (masksz
, "PC.masksz -> mask size")
2626 GET (tagsz
, "PC.tagsz -> MAC output size")
2627 GET (name
, "PC.name -> name of this kind of MAC")
2632 static const PyMethodDef poly1305hash_pymethods
[] = {
2633 #define METHNAME(name) polymeth_##name
2634 NAMETH(copy
, "P.copy() -> PP")
2635 METH (hash
, "P.hash(M)")
2636 #define METHU_(n, W, w) METH(hashu##w, "P.hashu" #w "(WORD)")
2639 #define METHBUF_(n, W, w) METH(hashbuf##w, "P.hashbuf" #w "(BYTES)")
2640 DOUINTCONV(METHBUF_
)
2642 METH (hashstrz
, "P.hashstrz(STRING)")
2643 NAMETH(flush
, "P.flush()")
2644 NAMETH(flushzero
, "P.flushzero()")
2645 METH (concat
, "P.concat(PREFIX, SUFFIX)")
2646 NAMETH(done
, "P.done() -> TAG")
2651 static PyTypeObject poly1305cls_pytype_skel
= {
2652 PyObject_HEAD_INIT(0) 0, /* Header */
2653 "Poly1305Class", /* @tp_name@ */
2654 sizeof(PyHeapTypeObject
), /* @tp_basicsize@ */
2655 0, /* @tp_itemsize@ */
2657 0, /* @tp_dealloc@ */
2659 0, /* @tp_getattr@ */
2660 0, /* @tp_setattr@ */
2661 0, /* @tp_compare@ */
2663 0, /* @tp_as_number@ */
2664 0, /* @tp_as_sequence@ */
2665 0, /* @tp_as_mapping@ */
2669 0, /* @tp_getattro@ */
2670 0, /* @tp_setattro@ */
2671 0, /* @tp_as_buffer@ */
2672 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2673 Py_TPFLAGS_BASETYPE
,
2676 "Poly1305 metametaclass. Best not to ask.",
2678 0, /* @tp_traverse@ */
2680 0, /* @tp_richcompare@ */
2681 0, /* @tp_weaklistoffset@ */
2683 0, /* @tp_iternext@ */
2684 0, /* @tp_methods@ */
2685 0, /* @tp_members@ */
2686 PYGETSET(poly1305cls
), /* @tp_getset@ */
2689 0, /* @tp_descr_get@ */
2690 0, /* @tp_descr_set@ */
2691 0, /* @tp_dictoffset@ */
2693 PyType_GenericAlloc
, /* @tp_alloc@ */
2694 abstract_pynew
, /* @tp_new@ */
2699 static PyTypeObject poly1305key_pytype_skel
= {
2700 PyObject_HEAD_INIT(0) 0, /* Header */
2701 "poly1305", /* @tp_name@ */
2702 sizeof(poly1305key_pyobj
), /* @tp_basicsize@ */
2703 0, /* @tp_itemsize@ */
2705 0, /* @tp_dealloc@ */
2707 0, /* @tp_getattr@ */
2708 0, /* @tp_setattr@ */
2709 0, /* @tp_compare@ */
2711 0, /* @tp_as_number@ */
2712 0, /* @tp_as_sequence@ */
2713 0, /* @tp_as_mapping@ */
2717 0, /* @tp_getattro@ */
2718 0, /* @tp_setattro@ */
2719 0, /* @tp_as_buffer@ */
2720 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2721 Py_TPFLAGS_BASETYPE
,
2724 "poly1305(K): Poly1305 key.",
2726 0, /* @tp_traverse@ */
2728 0, /* @tp_richcompare@ */
2729 0, /* @tp_weaklistoffset@ */
2731 0, /* @tp_iternext@ */
2732 0, /* @tp_methods@ */
2733 0, /* @tp_members@ */
2734 0, /* @tp_getset@ */
2737 0, /* @tp_descr_get@ */
2738 0, /* @tp_descr_set@ */
2739 0, /* @tp_dictoffset@ */
2741 PyType_GenericAlloc
, /* @tp_alloc@ */
2742 poly1305key_pynew
, /* @tp_new@ */
2747 static PyTypeObject poly1305hash_pytype_skel
= {
2748 PyObject_HEAD_INIT(0) 0, /* Header */
2749 "Poly1305Hash", /* @tp_name@ */
2750 sizeof(poly1305hash_pyobj
), /* @tp_basicsize@ */
2751 0, /* @tp_itemsize@ */
2753 0, /* @tp_dealloc@ */
2755 0, /* @tp_getattr@ */
2756 0, /* @tp_setattr@ */
2757 0, /* @tp_compare@ */
2759 0, /* @tp_as_number@ */
2760 0, /* @tp_as_sequence@ */
2761 0, /* @tp_as_mapping@ */
2765 0, /* @tp_getattro@ */
2766 0, /* @tp_setattro@ */
2767 0, /* @tp_as_buffer@ */
2768 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2769 Py_TPFLAGS_BASETYPE
,
2772 "Poly1305 MAC context base class.",
2774 0, /* @tp_traverse@ */
2776 0, /* @tp_richcompare@ */
2777 0, /* @tp_weaklistoffset@ */
2779 0, /* @tp_iternext@ */
2780 PYMETHODS(poly1305hash
), /* @tp_methods@ */
2781 0, /* @tp_members@ */
2782 0, /* @tp_getset@ */
2785 0, /* @tp_descr_get@ */
2786 0, /* @tp_descr_set@ */
2787 0, /* @tp_dictoffset@ */
2789 PyType_GenericAlloc
, /* @tp_alloc@ */
2790 abstract_pynew
, /* @tp_new@ */
2795 /*----- Special snowflake for HSalsa and HChaCha --------------------------*/
2797 #define DEF_HDANCE(DANCE, HDANCE, dance, hdance) \
2798 static PyObject *meth_##hdance##_prf(PyObject *me, PyObject *arg) \
2800 dance##_ctx dance; \
2802 Py_ssize_t ksz, nsz; \
2804 if (!PyArg_ParseTuple(arg, "s#s#:" #hdance "_prf", \
2805 &k, &ksz, &n, &nsz)) \
2807 if (ksz != keysz(ksz, dance##_keysz)) VALERR("bad key length"); \
2808 if (nsz != HDANCE##_INSZ) VALERR("bad input length"); \
2809 rc = bytestring_pywrap(0, HSALSA20_OUTSZ); \
2810 dance##_init(&dance, k, ksz, 0); \
2811 hdance##_prf(&dance, n, PyString_AS_STRING(rc)); \
2817 DEF_HDANCE(SALSA20
, HSALSA20
, salsa20
, hsalsa20
)
2818 DEF_HDANCE(SALSA20
, HSALSA20
, salsa20
, hsalsa2012
)
2819 DEF_HDANCE(SALSA20
, HSALSA20
, salsa20
, hsalsa208
)
2821 DEF_HDANCE(CHACHA
, HCHACHA
, chacha
, hchacha20
)
2822 DEF_HDANCE(CHACHA
, HCHACHA
, chacha
, hchacha12
)
2823 DEF_HDANCE(CHACHA
, HCHACHA
, chacha
, hchacha8
)
2825 /*----- Keccak-p[1600, n] -------------------------------------------------*/
2827 static PyTypeObject
*kxvik_pytype
;
2829 typedef struct kxvik_pyobj
{
2835 static PyObject
*kxvik_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
2838 kxvik_pyobj
*rc
= 0;
2839 static const char *const kwlist
[] = { "nround", 0 };
2840 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:new", KWLIST
,
2843 rc
= (kxvik_pyobj
*)ty
->tp_alloc(ty
, 0);
2845 keccak1600_init(&rc
->s
);
2847 return ((PyObject
*)rc
);
2850 static PyObject
*kxvikmeth_copy(PyObject
*me
)
2852 kxvik_pyobj
*k
= (kxvik_pyobj
*)me
, *rc
= 0;
2853 rc
= (kxvik_pyobj
*)k
->ob_type
->tp_alloc(k
->ob_type
, 0);
2854 rc
->s
= k
->s
; rc
->n
= k
->n
;
2855 return ((PyObject
*)rc
);
2858 static PyObject
*kxvikmeth_mix(PyObject
*me
, PyObject
*arg
)
2860 kxvik_pyobj
*k
= (kxvik_pyobj
*)me
;
2865 char *p
; Py_ssize_t n
;
2867 if (!PyArg_ParseTuple(arg
, "s#:mix", &p
, &n
)) goto end
;
2868 if (n
> 200) VALERR("out of range");
2869 q
= (const octet
*)p
;
2871 while (n
> 8) { LOAD64_L_(t
[i
], q
); i
++; q
+= 8; n
-= 8; }
2873 memcpy(buf
, q
, n
); memset(buf
+ n
, 0, 8 - n
);
2874 LOAD64_L_(t
[i
], buf
); i
++;
2876 keccak1600_mix(&k
->s
, t
, i
);
2882 static PyObject
*kxvikmeth_extract(PyObject
*me
, PyObject
*arg
)
2884 kxvik_pyobj
*k
= (kxvik_pyobj
*)me
;
2891 if (!PyArg_ParseTuple(arg
, "O&:extract", convuint
, &n
)) goto end
;
2892 if (n
> 200) VALERR("out of range");
2893 rc
= bytestring_pywrap(0, n
);
2894 q
= (octet
*)PyString_AS_STRING(rc
);
2895 keccak1600_extract(&k
->s
, t
, (n
+ 7)/8);
2897 while (n
> 8) { STORE64_L_(q
, t
[i
]); i
++; q
+= 8; n
-= 8; }
2898 if (n
) { STORE64_L_(buf
, t
[i
]); memcpy(q
, buf
, n
); }
2903 static PyObject
*kxvikmeth_step(PyObject
*me
)
2905 kxvik_pyobj
*k
= (kxvik_pyobj
*)me
;
2906 keccak1600_p(&k
->s
, &k
->s
, k
->n
);
2910 static PyObject
*kxvikget_nround(PyObject
*me
, void *hunoz
)
2912 kxvik_pyobj
*k
= (kxvik_pyobj
*)me
;
2913 return (PyInt_FromLong(k
->n
));
2916 static int kxvikset_nround(PyObject
*me
, PyObject
*val
, void *hunoz
)
2918 kxvik_pyobj
*k
= (kxvik_pyobj
*)me
;
2922 if (!val
) NIERR("__del__");
2923 if (!convuint(val
, &n
)) goto end
;
2930 static const PyGetSetDef kxvik_pygetset
[] = {
2931 #define GETSETNAME(op, name) kxvik##op##_##name
2932 GETSET(nround
, "KECCAK.nround -> number of rounds")
2937 static const PyMethodDef kxvik_pymethods
[] = {
2938 #define METHNAME(func) kxvikmeth_##func
2939 NAMETH(copy
, "KECCAK.copy() -> KECCAK'")
2940 METH (mix
, "KECCAK.mix(DATA)")
2941 METH (extract
, "KECCAK.extract(NOCTETS)")
2942 NAMETH(step
, "KECCAK.step()")
2947 static PyTypeObject kxvik_pytype_skel
= {
2948 PyObject_HEAD_INIT(0) 0, /* Header */
2949 "Keccak1600", /* @tp_name@ */
2950 sizeof(kxvik_pyobj
), /* @tp_basicsize@ */
2951 0, /* @tp_itemsize@ */
2953 0, /* @tp_dealloc@ */
2955 0, /* @tp_getattr@ */
2956 0, /* @tp_setattr@ */
2957 0, /* @tp_compare@ */
2959 0, /* @tp_as_number@ */
2960 0, /* @tp_as_sequence@ */
2961 0, /* @tp_as_mapping@ */
2965 0, /* @tp_getattro@ */
2966 0, /* @tp_setattro@ */
2967 0, /* @tp_as_buffer@ */
2968 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2969 Py_TPFLAGS_BASETYPE
,
2972 "Keccak1600([nround = 24]): Keccak-p[1600, n] state.",
2974 0, /* @tp_traverse@ */
2976 0, /* @tp_richcompare@ */
2977 0, /* @tp_weaklistoffset@ */
2979 0, /* @tp_iternext@ */
2980 PYMETHODS(kxvik
), /* @tp_methods@ */
2981 0, /* @tp_members@ */
2982 PYGETSET(kxvik
), /* @tp_getset@ */
2985 0, /* @tp_descr_get@ */
2986 0, /* @tp_descr_set@ */
2987 0, /* @tp_dictoffset@ */
2989 PyType_GenericAlloc
, /* @tp_alloc@ */
2990 kxvik_pynew
, /* @tp_new@ */
2995 static PyTypeObject
*shake_pytype
, *shake128_pytype
, *shake256_pytype
;
2997 typedef struct shake_pyobj
{
3003 #define SHAKE_H(o) (&((shake_pyobj *)(o))->h)
3004 #define SHAKE_ST(o) (((shake_pyobj *)(o))->st)
3006 static PyObject
*shake_dopynew(void (*initfn
)(shake_ctx
*,
3007 const void *, size_t,
3008 const void *, size_t),
3010 PyObject
*arg
, PyObject
*kw
)
3012 shake_pyobj
*rc
= 0;
3013 char *p
= 0, *f
= 0;
3014 Py_ssize_t psz
= 0, fsz
= 0;
3015 static const char *const kwlist
[] = { "perso", "func", 0 };
3017 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|s#s#:new", KWLIST
,
3018 &p
, &psz
, &f
, &fsz
))
3020 rc
= (shake_pyobj
*)ty
->tp_alloc(ty
, 0);
3021 initfn(&rc
->h
, f
, fsz
, p
, psz
);
3024 return ((PyObject
*)rc
);
3027 static PyObject
*shake128_pynew(PyTypeObject
*ty
,
3028 PyObject
*arg
, PyObject
*kw
)
3029 { return (shake_dopynew(cshake128_init
, ty
, arg
, kw
)); }
3031 static PyObject
*shake256_pynew(PyTypeObject
*ty
,
3032 PyObject
*arg
, PyObject
*kw
)
3033 { return (shake_dopynew(cshake256_init
, ty
, arg
, kw
)); }
3035 static int shake_check(PyObject
*me
, int st
)
3037 if (SHAKE_ST(me
) != st
) VALERR("wrong state");
3043 static PyObject
*shakemeth_hash(PyObject
*me
, PyObject
*arg
)
3047 if (!PyArg_ParseTuple(arg
, "s#:hash", &p
, &sz
)) return (0);
3048 if (shake_check(me
, 0)) return (0);
3049 shake_hash(SHAKE_H(me
), p
, sz
);
3053 #define SHAKEMETH_HASHU_(n, W, w) \
3054 static PyObject *shakemeth_hashu##w(PyObject *me, PyObject *arg) \
3058 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \
3059 if (shake_check(me, 0)) return (0); \
3060 STORE##W(b, x); shake_hash(SHAKE_H(me), b, sizeof(b)); \
3063 DOUINTCONV(SHAKEMETH_HASHU_
)
3065 #define SHAKEMETH_HASHBUF_(n, W, w) \
3066 static PyObject *shakemeth_hashbuf##w(PyObject *me, PyObject *arg) \
3071 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
3072 if (sz > MASK##n) TYERR("string too long"); \
3073 if (shake_check(me, 0)) goto end; \
3074 STORE##W(b, sz); shake_hash(SHAKE_H(me), b, sizeof(b)); \
3075 shake_hash(SHAKE_H(me), p, sz); \
3080 DOUINTCONV(SHAKEMETH_HASHBUF_
)
3082 static PyObject
*shakemeth_hashstrz(PyObject
*me
, PyObject
*arg
)
3085 if (!PyArg_ParseTuple(arg
, "s:hashstrz", &p
)) return (0);
3086 if (shake_check(me
, 0)) return (0);
3087 shake_hash(SHAKE_H(me
), p
, strlen(p
) + 1);
3091 static PyObject
*shakemeth_xof(PyObject
*me
)
3093 if (shake_check(me
, 0)) goto end
;
3094 shake_xof(SHAKE_H(me
));
3101 static PyObject
*shakemeth_done(PyObject
*me
, PyObject
*arg
)
3105 if (!PyArg_ParseTuple(arg
, "O&:done", convszt
, &n
)) goto end
;
3106 if (shake_check(me
, 0)) goto end
;
3107 rc
= bytestring_pywrap(0, n
);
3108 shake_done(SHAKE_H(me
), PyString_AS_STRING(rc
), n
);
3114 static PyObject
*shakemeth_copy(PyObject
*me
)
3116 shake_pyobj
*rc
= 0;
3118 rc
= PyObject_NEW(shake_pyobj
, me
->ob_type
);
3119 rc
->h
= *SHAKE_H(me
);
3120 rc
->st
= SHAKE_ST(me
);
3121 return ((PyObject
*)rc
);
3124 static PyObject
*shakemeth_get(PyObject
*me
, PyObject
*arg
)
3129 if (!PyArg_ParseTuple(arg
, "O&:get", convszt
, &sz
)) goto end
;
3130 if (shake_check(me
, 1)) goto end
;
3131 rc
= bytestring_pywrap(0, sz
);
3132 shake_get(SHAKE_H(me
), PyString_AS_STRING(rc
), sz
);
3137 static PyObject
*shakemeth_mask(PyObject
*me
, PyObject
*arg
)
3140 char *p
; Py_ssize_t sz
;
3142 if (!PyArg_ParseTuple(arg
, "s#:mask", &p
, &sz
)) goto end
;
3143 if (shake_check(me
, 1)) goto end
;
3144 rc
= bytestring_pywrap(0, sz
);
3145 shake_mask(SHAKE_H(me
), p
, PyString_AS_STRING(rc
), sz
);
3150 static PyObject
*shakeget_rate(PyObject
*me
, void *hunoz
)
3151 { return (PyInt_FromLong(SHAKE_H(me
)->h
.r
)); }
3153 static PyObject
*shakeget_buffered(PyObject
*me
, void *hunoz
)
3154 { return (PyInt_FromLong(SHAKE_H(me
)->h
.n
)); }
3156 static PyObject
*shakeget_state(PyObject
*me
, void *hunoz
)
3158 int st
= SHAKE_ST(me
);
3159 return (PyString_FromString(st
== 0 ?
"absorb" :
3160 st
== 1 ?
"squeeze" : "dead"));
3163 static const PyGetSetDef shake_pygetset
[] = {
3164 #define GETSETNAME(op, name) shake##op##_##name
3165 GET (rate
, "S.rate -> rate, in bytes")
3166 GET (buffered
, "S.buffered -> amount currently buffered")
3167 GET (state
, "S.state -> `absorb', `squeeze', `dead'")
3172 static const PyMethodDef shake_pymethods
[] = {
3173 #define METHNAME(func) shakemeth_##func
3174 NAMETH(copy
, "S.copy() -> SS")
3175 METH (hash
, "S.hash(M)")
3176 #define METHU_(n, W, w) METH(hashu##w, "S.hashu" #w "(WORD)")
3179 #define METHBUF_(n, W, w) METH(hashbuf##w, "S.hashbuf" #w "(BYTES)")
3180 DOUINTCONV(METHBUF_
)
3182 METH (hashstrz
, "S.hashstrz(STRING)")
3183 NAMETH(xof
, "S.xof()")
3184 METH (done
, "S.done(LEN) -> H")
3185 METH (get
, "S.get(LEN) -> H")
3186 METH (mask
, "S.mask(M) -> C")
3191 static PyTypeObject shake_pytype_skel
= {
3192 PyObject_HEAD_INIT(0) 0, /* Header */
3193 "Shake", /* @tp_name@ */
3194 sizeof(shake_pyobj
), /* @tp_basicsize@ */
3195 0, /* @tp_itemsize@ */
3197 0, /* @tp_dealloc@ */
3199 0, /* @tp_getattr@ */
3200 0, /* @tp_setattr@ */
3201 0, /* @tp_compare@ */
3203 0, /* @tp_as_number@ */
3204 0, /* @tp_as_sequence@ */
3205 0, /* @tp_as_mapping@ */
3209 0, /* @tp_getattro@ */
3210 0, /* @tp_setattro@ */
3211 0, /* @tp_as_buffer@ */
3212 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
3213 Py_TPFLAGS_BASETYPE
,
3216 "SHAKE/cSHAKE/KMAC base class.",
3218 0, /* @tp_traverse@ */
3220 0, /* @tp_richcompare@ */
3221 0, /* @tp_weaklistoffset@ */
3223 0, /* @tp_iternext@ */
3224 PYMETHODS(shake
), /* @tp_methods@ */
3225 0, /* @tp_members@ */
3226 PYGETSET(shake
), /* @tp_getset@ */
3229 0, /* @tp_descr_get@ */
3230 0, /* @tp_descr_set@ */
3231 0, /* @tp_dictoffset@ */
3233 PyType_GenericAlloc
, /* @tp_alloc@ */
3234 abstract_pynew
, /* @tp_new@ */
3239 static PyTypeObject shake128_pytype_skel
= {
3240 PyObject_HEAD_INIT(0) 0, /* Header */
3241 "Shake128", /* @tp_name@ */
3242 0, /* @tp_basicsize@ */
3243 0, /* @tp_itemsize@ */
3245 0, /* @tp_dealloc@ */
3247 0, /* @tp_getattr@ */
3248 0, /* @tp_setattr@ */
3249 0, /* @tp_compare@ */
3251 0, /* @tp_as_number@ */
3252 0, /* @tp_as_sequence@ */
3253 0, /* @tp_as_mapping@ */
3257 0, /* @tp_getattro@ */
3258 0, /* @tp_setattro@ */
3259 0, /* @tp_as_buffer@ */
3260 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
3261 Py_TPFLAGS_BASETYPE
,
3264 "Shake128([perso = STR], [func = STR]): SHAKE128/cSHAKE128 XOF.",
3266 0, /* @tp_traverse@ */
3268 0, /* @tp_richcompare@ */
3269 0, /* @tp_weaklistoffset@ */
3271 0, /* @tp_iternext@ */
3272 0, /* @tp_methods@ */
3273 0, /* @tp_members@ */
3274 0, /* @tp_getset@ */
3277 0, /* @tp_descr_get@ */
3278 0, /* @tp_descr_set@ */
3279 0, /* @tp_dictoffset@ */
3281 PyType_GenericAlloc
, /* @tp_alloc@ */
3282 shake128_pynew
, /* @tp_new@ */
3287 static PyTypeObject shake256_pytype_skel
= {
3288 PyObject_HEAD_INIT(0) 0, /* Header */
3289 "Shake256", /* @tp_name@ */
3290 0, /* @tp_basicsize@ */
3291 0, /* @tp_itemsize@ */
3293 0, /* @tp_dealloc@ */
3295 0, /* @tp_getattr@ */
3296 0, /* @tp_setattr@ */
3297 0, /* @tp_compare@ */
3299 0, /* @tp_as_number@ */
3300 0, /* @tp_as_sequence@ */
3301 0, /* @tp_as_mapping@ */
3305 0, /* @tp_getattro@ */
3306 0, /* @tp_setattro@ */
3307 0, /* @tp_as_buffer@ */
3308 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
3309 Py_TPFLAGS_BASETYPE
,
3312 "Shake256([perso = STR], [func = STR]): SHAKE256/cSHAKE256 XOF.",
3314 0, /* @tp_traverse@ */
3316 0, /* @tp_richcompare@ */
3317 0, /* @tp_weaklistoffset@ */
3319 0, /* @tp_iternext@ */
3320 0, /* @tp_methods@ */
3321 0, /* @tp_members@ */
3322 0, /* @tp_getset@ */
3325 0, /* @tp_descr_get@ */
3326 0, /* @tp_descr_set@ */
3327 0, /* @tp_dictoffset@ */
3329 PyType_GenericAlloc
, /* @tp_alloc@ */
3330 shake256_pynew
, /* @tp_new@ */
3335 /*----- Pseudorandom permutations -----------------------------------------*/
3337 static PyTypeObject
*gcprp_pytype
, *gprp_pytype
;
3339 typedef struct prpinfo
{
3344 void (*init
)(void *, const void *, size_t);
3345 void (*eblk
)(void *, const void *, void *);
3346 void (*dblk
)(void *, const void *, void *);
3349 #define PRP_DEF(PRE, pre) \
3350 static void pre##_prpinit(void *ctx, const void *k, size_t ksz) \
3351 { pre##_init(ctx, k, ksz); } \
3352 static void pre##_prpeblk(void *ctx, const void *in, void *out) \
3354 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
3355 pre##_eblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
3357 static void pre##_prpdblk(void *ctx, const void *in, void *out) \
3359 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
3360 pre##_dblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
3362 static const prpinfo pre##_prpinfo = { \
3363 #pre, pre##_keysz, sizeof(pre##_ctx), PRE##_BLKSZ, \
3364 pre##_prpinit, pre##_prpeblk, pre##_prpdblk \
3368 static const struct prpinfo
*const gprptab
[] = {
3369 #define PRP_ENTRY(PRE, pre) &pre##_prpinfo,
3374 typedef struct gcprp_pyobj
{
3375 PyHeapTypeObject ty
;
3378 #define GCPRP_PRP(o) (((gcprp_pyobj *)(o))->prp)
3380 typedef struct gprp_pyobj
{
3384 #define GPRP_PRP(o) (((gprp_pyobj *)(o))->prp)
3385 #define GPRP_CTX(o) (((gprp_pyobj *)(o)) + 1)
3387 typedef struct prp
{
3392 static PyObject
*gprp_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
3394 static const char *const kwlist
[] = { "key", 0 };
3397 const prpinfo
*prp
= GCPRP_PRP(ty
);
3400 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", KWLIST
, &k
, &sz
))
3402 if (keysz(sz
, prp
->keysz
) != sz
) VALERR("bad key length");
3403 me
= (PyObject
*)ty
->tp_alloc(ty
, 0);
3405 prp
->init(GPRP_CTX(me
), k
, sz
);
3412 static void gprp_pydealloc(PyObject
*me
)
3413 { Py_DECREF(me
->ob_type
); FREEOBJ(me
); }
3415 static PyObject
*gcprp_pywrap(const prpinfo
*prp
)
3417 gcprp_pyobj
*g
= newtype(gcprp_pytype
, 0, prp
->name
);
3419 g
->ty
.ht_type
.tp_basicsize
= sizeof(gprp_pyobj
) + prp
->ctxsz
;
3420 g
->ty
.ht_type
.tp_base
= gprp_pytype
;
3421 Py_INCREF(gprp_pytype
);
3422 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
3423 Py_TPFLAGS_BASETYPE
|
3424 Py_TPFLAGS_HEAPTYPE
);
3425 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
3426 g
->ty
.ht_type
.tp_free
= 0;
3427 g
->ty
.ht_type
.tp_new
= gprp_pynew
;
3428 typeready(&g
->ty
.ht_type
);
3429 return ((PyObject
*)g
);
3432 static PyObject
*gcpget_name(PyObject
*me
, void *hunoz
)
3433 { return (PyString_FromString(GCPRP_PRP(me
)->name
)); }
3434 static PyObject
*gcpget_keysz(PyObject
*me
, void *hunoz
)
3435 { return (keysz_pywrap(GCPRP_PRP(me
)->keysz
)); }
3436 static PyObject
*gcpget_blksz(PyObject
*me
, void *hunoz
)
3437 { return (PyInt_FromLong(GCPRP_PRP(me
)->blksz
)); }
3439 static PyObject
*gpmeth_encrypt(PyObject
*me
, PyObject
*arg
)
3445 if (!PyArg_ParseTuple(arg
, "s#:encrypt", &p
, &n
)) goto end
;
3446 if (n
!= GPRP_PRP(me
)->blksz
) VALERR("incorrect block length");
3447 rc
= bytestring_pywrap(0, n
);
3448 GPRP_PRP(me
)->eblk(GPRP_CTX(me
), p
, PyString_AS_STRING(rc
));
3453 static PyObject
*gpmeth_decrypt(PyObject
*me
, PyObject
*arg
)
3459 if (!PyArg_ParseTuple(arg
, "s#:decrypt", &p
, &n
)) goto end
;
3460 if (n
!= GPRP_PRP(me
)->blksz
) VALERR("incorrect block length");
3461 rc
= bytestring_pywrap(0, n
);
3462 GPRP_PRP(me
)->dblk(GPRP_CTX(me
), p
, PyString_AS_STRING(rc
));
3467 static const PyGetSetDef gcprp_pygetset
[] = {
3468 #define GETSETNAME(op, name) gcp##op##_##name
3469 GET (keysz
, "CP.keysz -> acceptable key sizes")
3470 GET (blksz
, "CP.blksz -> block size")
3471 GET (name
, "CP.name -> name of this kind of PRP")
3476 static const PyMethodDef gprp_pymethods
[] = {
3477 #define METHNAME(name) gpmeth_##name
3478 METH (encrypt
, "P.encrypt(PT) -> CT")
3479 METH (decrypt
, "P.decrypt(CT) -> PT")
3484 static PyTypeObject gcprp_pytype_skel
= {
3485 PyObject_HEAD_INIT(0) 0, /* Header */
3486 "GCPRP", /* @tp_name@ */
3487 sizeof(gcprp_pyobj
), /* @tp_basicsize@ */
3488 0, /* @tp_itemsize@ */
3490 0, /* @tp_dealloc@ */
3492 0, /* @tp_getattr@ */
3493 0, /* @tp_setattr@ */
3494 0, /* @tp_compare@ */
3496 0, /* @tp_as_number@ */
3497 0, /* @tp_as_sequence@ */
3498 0, /* @tp_as_mapping@ */
3502 0, /* @tp_getattro@ */
3503 0, /* @tp_setattro@ */
3504 0, /* @tp_as_buffer@ */
3505 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
3506 Py_TPFLAGS_BASETYPE
,
3509 "Pseudorandom permutation metaclass.",
3511 0, /* @tp_traverse@ */
3513 0, /* @tp_richcompare@ */
3514 0, /* @tp_weaklistoffset@ */
3516 0, /* @tp_iternext@ */
3517 0, /* @tp_methods@ */
3518 0, /* @tp_members@ */
3519 PYGETSET(gcprp
), /* @tp_getset@ */
3522 0, /* @tp_descr_get@ */
3523 0, /* @tp_descr_set@ */
3524 0, /* @tp_dictoffset@ */
3526 PyType_GenericAlloc
, /* @tp_alloc@ */
3527 abstract_pynew
, /* @tp_new@ */
3532 static PyTypeObject gprp_pytype_skel
= {
3533 PyObject_HEAD_INIT(0) 0, /* Header */
3534 "GPRP", /* @tp_name@ */
3535 sizeof(gprp_pyobj
), /* @tp_basicsize@ */
3536 0, /* @tp_itemsize@ */
3538 gprp_pydealloc
, /* @tp_dealloc@ */
3540 0, /* @tp_getattr@ */
3541 0, /* @tp_setattr@ */
3542 0, /* @tp_compare@ */
3544 0, /* @tp_as_number@ */
3545 0, /* @tp_as_sequence@ */
3546 0, /* @tp_as_mapping@ */
3550 0, /* @tp_getattro@ */
3551 0, /* @tp_setattro@ */
3552 0, /* @tp_as_buffer@ */
3553 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
3554 Py_TPFLAGS_BASETYPE
,
3557 "Pseudorandom permutation, abstract base class.",
3559 0, /* @tp_traverse@ */
3561 0, /* @tp_richcompare@ */
3562 0, /* @tp_weaklistoffset@ */
3564 0, /* @tp_iternext@ */
3565 PYMETHODS(gprp
), /* @tp_methods@ */
3566 0, /* @tp_members@ */
3567 0, /* @tp_getset@ */
3570 0, /* @tp_descr_get@ */
3571 0, /* @tp_descr_set@ */
3572 0, /* @tp_dictoffset@ */
3574 PyType_GenericAlloc
, /* @tp_alloc@ */
3575 abstract_pynew
, /* @tp_new@ */
3580 /*----- Main code ---------------------------------------------------------*/
3582 static const PyMethodDef methods
[] = {
3583 #define METHNAME(func) meth_##func
3584 #define METH_HDANCE(hdance, HDance) METH(hdance##_prf, \
3585 "" #hdance "_prf(K, N) -> H: calculate " HDance " hash of N with K")
3586 METH_HDANCE(hsalsa20
, "HSalsa20")
3587 METH_HDANCE(hsalsa2012
, "HSalsa20/12")
3588 METH_HDANCE(hsalsa208
, "HSalsa20/8")
3589 METH_HDANCE(hchacha20
, "HChaCha20")
3590 METH_HDANCE(hchacha12
, "HChaCha12")
3591 METH_HDANCE(hchacha8
, "HChaCha8")
3597 void algorithms_pyinit(void)
3599 INITTYPE(keysz
, root
);
3600 INITTYPE(keyszany
, keysz
);
3601 INITTYPE(keyszrange
, keysz
);
3602 INITTYPE(keyszset
, keysz
);
3603 INITTYPE(gccipher
, type
);
3604 INITTYPE(gcipher
, root
);
3605 INITTYPE(gcaead
, type
);
3606 INITTYPE(gaeadkey
, root
);
3607 INITTYPE(gcaeadaad
, type
);
3608 INITTYPE(gaeadaad
, root
);
3609 INITTYPE(gcaeadenc
, type
);
3610 INITTYPE(gaeadenc
, root
);
3611 INITTYPE(gcaeaddec
, type
);
3612 INITTYPE(gaeaddec
, root
);
3613 INITTYPE(gchash
, type
);
3614 INITTYPE(ghash
, root
);
3615 INITTYPE(gcmac
, type
);
3616 INITTYPE(gmac
, type
);
3617 INITTYPE(gmhash
, ghash
);
3618 INITTYPE(poly1305cls
, type
);
3619 INITTYPE_META(poly1305key
, type
, poly1305cls
);
3620 INITTYPE(poly1305hash
, root
);
3621 INITTYPE(kxvik
, root
);
3622 INITTYPE(shake
, root
);
3623 INITTYPE(shake128
, shake
);
3624 INITTYPE(shake256
, shake
);
3625 INITTYPE(gcprp
, type
);
3626 INITTYPE(gprp
, root
);
3627 addmethods(methods
);
3630 GEN(gcciphers
, cipher
)
3634 #define gcprp prpinfo
3637 void algorithms_pyinsert(PyObject
*mod
)
3640 INSERT("KeySZ", keysz_pytype
);
3641 INSERT("KeySZAny", keyszany_pytype
);
3642 INSERT("KeySZRange", keyszrange_pytype
);
3643 INSERT("KeySZSet", keyszset_pytype
);
3644 INSERT("GCCipher", gccipher_pytype
);
3645 INSERT("GCipher", gcipher_pytype
);
3646 INSERT("gcciphers", gcciphers());
3647 INSERT("GCAEAD", gcaead_pytype
);
3648 INSERT("GAEKey", gaeadkey_pytype
);
3649 INSERT("GAEAADClass", gcaeadaad_pytype
);
3650 INSERT("GAEAAD", gaeadaad_pytype
);
3651 INSERT("GAEEncClass", gcaeadenc_pytype
);
3652 INSERT("GAEEnc", gaeadenc_pytype
);
3653 INSERT("GAEDecClass", gcaeaddec_pytype
);
3654 INSERT("GAEDec", gaeaddec_pytype
);
3655 INSERT("gcaeads", gcaeads());
3656 INSERT("GCHash", gchash_pytype
);
3657 INSERT("GHash", ghash_pytype
);
3658 INSERT("gchashes", d
= gchashes());
3659 sha_pyobj
= PyDict_GetItemString(d
, "sha"); Py_INCREF(sha_pyobj
);
3660 has160_pyobj
= PyDict_GetItemString(d
, "has160"); Py_INCREF(has160_pyobj
);
3661 INSERT("GCMAC", gcmac_pytype
);
3662 INSERT("GMAC", gmac_pytype
);
3663 INSERT("GMACHash", gmhash_pytype
);
3664 INSERT("gcmacs", gcmacs());
3665 INSERT("Poly1305Class", poly1305cls_pytype
);
3666 INSERT("poly1305", poly1305key_pytype
);
3667 INSERT("Poly1305Hash", poly1305hash_pytype
);
3668 INSERT("Keccak1600", kxvik_pytype
);
3669 INSERT("Shake", shake_pytype
);
3670 INSERT("Shake128", shake128_pytype
);
3671 INSERT("Shake256", shake256_pytype
);
3672 INSERT("GCPRP", gcprp_pytype
);
3673 INSERT("GPRP", gprp_pytype
);
3674 INSERT("gcprps", gcprps());
3677 /*----- That's all, folks -------------------------------------------------*/