3 * Symmetric cryptography
5 * (c) 2004 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Python interface to Catacomb.
12 * Catacomb/Python is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * Catacomb/Python is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with Catacomb/Python; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
29 #include "catacomb-python.h"
30 #include "algorithms.h"
32 /*----- Key sizes ---------------------------------------------------------*/
34 PyTypeObject
*keysz_pytype
;
35 PyTypeObject
*keyszany_pytype
, *keyszrange_pytype
, *keyszset_pytype
;
36 PyObject
*sha_pyobj
, *has160_pyobj
;
39 # define KSZ_OPMASK 0x1f
43 # define KSZ_16BIT 0x20
46 PyObject
*keysz_pywrap(const octet
*k
)
49 #define ARG(i) (op&KSZ_16BIT ? LOAD16(k + 2*(i)) : k[i])
50 switch (op
&KSZ_OPMASK
) {
52 keysz_pyobj
*o
= PyObject_New(keysz_pyobj
, keyszany_pytype
);
54 return ((PyObject
*)o
);
58 PyObject_New(keyszrange_pyobj
, keyszrange_pytype
);
63 if (!o
->mod
) o
->mod
= 1;
64 return ((PyObject
*)o
);
68 PyObject_New(keyszset_pyobj
, keyszset_pytype
);
71 for (i
= 0; ARG(i
); i
++) ;
72 n
= i
; o
->set
= PyTuple_New(n
);
73 for (i
= 0; i
< n
; i
++)
74 PyTuple_SET_ITEM(o
->set
, i
, PyInt_FromLong(ARG(i
)));
75 return ((PyObject
*)o
);
83 static PyObject
*keyszany_pynew(PyTypeObject
*ty
,
84 PyObject
*arg
, PyObject
*kw
)
86 static const char *const kwlist
[] = { "default", 0 };
90 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "i:new", KWLIST
, &dfl
))
92 if (dfl
< 0) VALERR("key size cannot be negative");
93 o
= (keysz_pyobj
*)ty
->tp_alloc(ty
, 0);
95 return ((PyObject
*)o
);
100 static PyObject
*keyszrange_pynew(PyTypeObject
*ty
,
101 PyObject
*arg
, PyObject
*kw
)
103 static const char *const kwlist
[] = { "default", "min", "max", "mod", 0 };
104 int dfl
, min
= 0, max
= 0, mod
= 1;
107 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "i|iii:new", KWLIST
,
108 &dfl
, &min
, &max
, &mod
))
110 if (dfl
< 0 || min
< 0) VALERR("key size cannot be negative");
111 if (min
> dfl
|| (max
&& dfl
> max
)) VALERR("bad key size bounds");
112 if (mod
<= 0 || dfl
%mod
|| min
%mod
|| max
%mod
)
113 VALERR("bad key size modulus");
114 o
= (keyszrange_pyobj
*)ty
->tp_alloc(ty
, 0);
119 return ((PyObject
*)o
);
124 static PyObject
*keyszset_pynew(PyTypeObject
*ty
,
125 PyObject
*arg
, PyObject
*kw
)
127 static const char *const kwlist
[] = { "default", "set", 0 };
130 PyObject
*x
= 0, *l
= 0;
131 keyszset_pyobj
*o
= 0;
133 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "i|O:new", KWLIST
, &dfl
, &set
))
135 if (!set
) set
= PyTuple_New(0);
137 if (!PySequence_Check(set
)) TYERR("want a sequence");
138 n
= PySequence_Size(set
);
140 if (PyErr_Occurred()) goto end
;
141 if (dfl
< 0) VALERR("key size cannot be negative");
142 x
= PyInt_FromLong(dfl
);
146 for (i
= 0; i
< n
; i
++) {
147 if ((x
= PySequence_GetItem(set
, i
)) == 0) goto end
;
148 xx
= PyInt_AsLong(x
);
149 if (PyErr_Occurred()) goto end
;
150 if (xx
== dfl
) continue;
151 if (xx
< 0) VALERR("key size cannot be negative");
157 if ((set
= PySequence_Tuple(l
)) == 0) goto end
;
158 o
= (keyszset_pyobj
*)ty
->tp_alloc(ty
, 0);
166 return ((PyObject
*)o
);
169 static PyObject
*kaget_min(PyObject
*me
, void *hunoz
)
170 { return (PyInt_FromLong(0)); }
171 #define kaget_max kaget_min
173 static PyObject
*ksget_min(PyObject
*me
, void *hunoz
)
175 PyObject
*set
= ((keyszset_pyobj
*)me
)->set
;
177 n
= PyTuple_Size(set
);
178 for (i
= 0; i
< n
; i
++) {
179 y
= PyInt_AsLong(PyTuple_GetItem(set
, i
));
180 if (x
== -1 || y
< x
) x
= y
;
182 return (PyInt_FromLong(x
));
185 static PyObject
*ksget_max(PyObject
*me
, void *hunoz
)
187 PyObject
*set
= ((keyszset_pyobj
*)me
)->set
;
189 n
= PyTuple_Size(set
);
190 for (i
= 0; i
< n
; i
++) {
191 y
= PyInt_AsLong(PyTuple_GetItem(set
, i
));
194 return (PyInt_FromLong(x
));
197 static PyMemberDef keysz_pymembers
[] = {
198 #define MEMBERSTRUCT keysz_pyobj
199 #define default dfl /* ugh! */
200 MEMBER(default, T_INT
, READONLY
, "KSZ.default -> default key size")
206 static PyGetSetDef keyszany_pygetset
[] = {
207 #define GETSETNAME(op, name) ka##op##_##name
208 GET (min
, "KSZ.min -> smallest allowed key size")
209 GET (max
, "KSZ.min -> largest allowed key size")
214 static PyMemberDef keyszrange_pymembers
[] = {
215 #define MEMBERSTRUCT keyszrange_pyobj
216 MEMBER(min
, T_INT
, READONLY
, "KSZ.min -> smallest allowed key size")
217 MEMBER(max
, T_INT
, READONLY
, "KSZ.min -> largest allowed key size")
218 MEMBER(mod
, T_INT
, READONLY
,
219 "KSZ.mod -> key size must be a multiple of this")
224 static PyGetSetDef keyszset_pygetset
[] = {
225 #define GETSETNAME(op, name) ks##op##_##name
226 GET (min
, "KSZ.min -> smallest allowed key size")
227 GET (max
, "KSZ.min -> largest allowed key size")
232 static PyMemberDef keyszset_pymembers
[] = {
233 #define MEMBERSTRUCT keyszset_pyobj
234 MEMBER(set
, T_OBJECT
, READONLY
, "KSZ.set -> allowed key sizes")
239 static PyTypeObject keysz_pytype_skel
= {
240 PyObject_HEAD_INIT(0) 0, /* Header */
241 "KeySZ", /* @tp_name@ */
242 sizeof(keysz_pyobj
), /* @tp_basicsize@ */
243 0, /* @tp_itemsize@ */
245 0, /* @tp_dealloc@ */
247 0, /* @tp_getattr@ */
248 0, /* @tp_setattr@ */
249 0, /* @tp_compare@ */
251 0, /* @tp_as_number@ */
252 0, /* @tp_as_sequence@ */
253 0, /* @tp_as_mapping@ */
257 0, /* @tp_getattro@ */
258 0, /* @tp_setattro@ */
259 0, /* @tp_as_buffer@ */
260 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
264 "Key size constraints. Abstract.",
266 0, /* @tp_traverse@ */
268 0, /* @tp_richcompare@ */
269 0, /* @tp_weaklistoffset@ */
271 0, /* @tp_iternext@ */
272 0, /* @tp_methods@ */
273 keysz_pymembers
, /* @tp_members@ */
277 0, /* @tp_descr_get@ */
278 0, /* @tp_descr_set@ */
279 0, /* @tp_dictoffset@ */
281 PyType_GenericAlloc
, /* @tp_alloc@ */
282 abstract_pynew
, /* @tp_new@ */
287 static PyTypeObject keyszany_pytype_skel
= {
288 PyObject_HEAD_INIT(0) 0, /* Header */
289 "KeySZAny", /* @tp_name@ */
290 sizeof(keysz_pyobj
), /* @tp_basicsize@ */
291 0, /* @tp_itemsize@ */
293 0, /* @tp_dealloc@ */
295 0, /* @tp_getattr@ */
296 0, /* @tp_setattr@ */
297 0, /* @tp_compare@ */
299 0, /* @tp_as_number@ */
300 0, /* @tp_as_sequence@ */
301 0, /* @tp_as_mapping@ */
305 0, /* @tp_getattro@ */
306 0, /* @tp_setattro@ */
307 0, /* @tp_as_buffer@ */
308 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
312 "KeySZAny(DEFAULT)\n\
313 Key size constraints. This object imposes no constraints on size.",
315 0, /* @tp_traverse@ */
317 0, /* @tp_richcompare@ */
318 0, /* @tp_weaklistoffset@ */
320 0, /* @tp_iternext@ */
321 0, /* @tp_methods@ */
322 0, /* @tp_members@ */
323 keyszany_pygetset
, /* @tp_getset@ */
326 0, /* @tp_descr_get@ */
327 0, /* @tp_descr_set@ */
328 0, /* @tp_dictoffset@ */
330 PyType_GenericAlloc
, /* @tp_alloc@ */
331 keyszany_pynew
, /* @tp_new@ */
336 static PyTypeObject keyszrange_pytype_skel
= {
337 PyObject_HEAD_INIT(0) 0, /* Header */
338 "KeySZRange", /* @tp_name@ */
339 sizeof(keyszrange_pyobj
), /* @tp_basicsize@ */
340 0, /* @tp_itemsize@ */
342 0, /* @tp_dealloc@ */
344 0, /* @tp_getattr@ */
345 0, /* @tp_setattr@ */
346 0, /* @tp_compare@ */
348 0, /* @tp_as_number@ */
349 0, /* @tp_as_sequence@ */
350 0, /* @tp_as_mapping@ */
354 0, /* @tp_getattro@ */
355 0, /* @tp_setattro@ */
356 0, /* @tp_as_buffer@ */
357 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
361 "KeySZRange(DEFAULT, [min = 0], [max = 0], [mod = 1])\n\
362 Key size constraints. Key size must be between MIN and MAX inclusive,\n\
363 and be a multiple of MOD.",
365 0, /* @tp_traverse@ */
367 0, /* @tp_richcompare@ */
368 0, /* @tp_weaklistoffset@ */
370 0, /* @tp_iternext@ */
371 0, /* @tp_methods@ */
372 keyszrange_pymembers
, /* @tp_members@ */
376 0, /* @tp_descr_get@ */
377 0, /* @tp_descr_set@ */
378 0, /* @tp_dictoffset@ */
380 PyType_GenericAlloc
, /* @tp_alloc@ */
381 keyszrange_pynew
, /* @tp_new@ */
386 static PyTypeObject keyszset_pytype_skel
= {
387 PyObject_HEAD_INIT(0) 0, /* Header */
388 "KeySZSet", /* @tp_name@ */
389 sizeof(keyszset_pyobj
), /* @tp_basicsize@ */
390 0, /* @tp_itemsize@ */
392 0, /* @tp_dealloc@ */
394 0, /* @tp_getattr@ */
395 0, /* @tp_setattr@ */
396 0, /* @tp_compare@ */
398 0, /* @tp_as_number@ */
399 0, /* @tp_as_sequence@ */
400 0, /* @tp_as_mapping@ */
404 0, /* @tp_getattro@ */
405 0, /* @tp_setattro@ */
406 0, /* @tp_as_buffer@ */
407 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
411 "KeySZSet(DEFAULT, SEQ)\n\
412 Key size constraints. Key size must be DEFAULT or one in SEQ.",
414 0, /* @tp_traverse@ */
416 0, /* @tp_richcompare@ */
417 0, /* @tp_weaklistoffset@ */
419 0, /* @tp_iternext@ */
420 0, /* @tp_methods@ */
421 keyszset_pymembers
, /* @tp_members@ */
422 keyszset_pygetset
, /* @tp_getset@ */
425 0, /* @tp_descr_get@ */
426 0, /* @tp_descr_set@ */
427 0, /* @tp_dictoffset@ */
429 PyType_GenericAlloc
, /* @tp_alloc@ */
430 keyszset_pynew
, /* @tp_new@ */
435 #define KSZCONVOP(op) \
436 static PyObject *meth__KeySZ_##op(PyObject *me, PyObject *arg) \
439 if (!PyArg_ParseTuple(arg, "Od:" #op, &me, &x)) return (0); \
441 return (PyFloat_FromDouble(y)); \
444 KSZCONVOP(fromschnorr
)
453 /*----- Symmetric encryption ----------------------------------------------*/
455 PyTypeObject
*gccipher_pytype
, *gcipher_pytype
;
457 CONVFUNC(gccipher
, gccipher
*, GCCIPHER_CC
)
458 CONVFUNC(gcipher
, gcipher
*, GCIPHER_C
)
460 PyObject
*gcipher_pywrap(PyObject
*cobj
, gcipher
*c
)
463 if (!cobj
) cobj
= gccipher_pywrap((/*unconst*/ gccipher
*)GC_CLASS(c
));
464 else Py_INCREF(cobj
);
465 g
= PyObject_NEW(gcipher_pyobj
, (PyTypeObject
*)cobj
);
467 return ((PyObject
*)g
);
470 static PyObject
*gcipher_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
472 static const char *const kwlist
[] = { "k", 0 };
476 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", KWLIST
, &k
, &sz
))
478 if (keysz(sz
, GCCIPHER_CC(ty
)->keysz
) != sz
) VALERR("bad key length");
479 return (gcipher_pywrap((PyObject
*)ty
,
480 GC_INIT(GCCIPHER_CC(ty
), k
, sz
)));
485 PyObject
*gccipher_pywrap(gccipher
*cc
)
487 gccipher_pyobj
*g
= newtype(gccipher_pytype
, 0, cc
->name
);
489 g
->ty
.ht_type
.tp_basicsize
= sizeof(gcipher_pyobj
);
490 g
->ty
.ht_type
.tp_base
= gcipher_pytype
;
491 Py_INCREF(gcipher_pytype
);
492 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
493 Py_TPFLAGS_BASETYPE
|
494 Py_TPFLAGS_HEAPTYPE
);
495 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
496 g
->ty
.ht_type
.tp_free
= 0;
497 g
->ty
.ht_type
.tp_new
= gcipher_pynew
;
498 typeready(&g
->ty
.ht_type
);
499 return ((PyObject
*)g
);
502 static void gcipher_pydealloc(PyObject
*me
)
504 GC_DESTROY(GCIPHER_C(me
));
505 Py_DECREF(me
->ob_type
);
509 static PyObject
*gccget_name(PyObject
*me
, void *hunoz
)
510 { return (PyString_FromString(GCCIPHER_CC(me
)->name
)); }
512 static PyObject
*gccget_keysz(PyObject
*me
, void *hunoz
)
513 { return (keysz_pywrap(GCCIPHER_CC(me
)->keysz
)); }
515 static PyObject
*gccget_blksz(PyObject
*me
, void *hunoz
)
516 { return (PyInt_FromLong(GCCIPHER_CC(me
)->blksz
)); }
518 static PyObject
*gcmeth_encrypt(PyObject
*me
, PyObject
*arg
)
524 if (!PyArg_ParseTuple(arg
, "s#:encrypt", &p
, &sz
)) return (0);
525 rc
= bytestring_pywrap(0, sz
);
526 GC_ENCRYPT(GCIPHER_C(me
), p
, PyString_AS_STRING(rc
), sz
);
530 static PyObject
*gcmeth_enczero(PyObject
*me
, PyObject
*arg
)
536 if (!PyArg_ParseTuple(arg
, "i:enczero", &sz
)) return (0);
537 rc
= bytestring_pywrap(0, sz
);
538 p
= PyString_AS_STRING(rc
);
540 GC_ENCRYPT(GCIPHER_C(me
), p
, p
, sz
);
544 static PyObject
*gcmeth_decrypt(PyObject
*me
, PyObject
*arg
)
550 if (!PyArg_ParseTuple(arg
, "s#:decrypt", &p
, &sz
)) return (0);
551 rc
= bytestring_pywrap(0, sz
);
552 GC_DECRYPT(GCIPHER_C(me
), p
, PyString_AS_STRING(rc
), sz
);
556 static PyObject
*gcmeth_deczero(PyObject
*me
, PyObject
*arg
)
562 if (!PyArg_ParseTuple(arg
, "i:deczero", &sz
)) return (0);
563 rc
= bytestring_pywrap(0, sz
);
564 p
= PyString_AS_STRING(rc
);
566 GC_DECRYPT(GCIPHER_C(me
), p
, p
, sz
);
570 static PyObject
*gcmeth_setiv(PyObject
*me
, PyObject
*arg
)
575 if (!PyArg_ParseTuple(arg
, "s#:setiv", &p
, &sz
)) goto end
;
576 if (!GCIPHER_C(me
)->ops
->setiv
) VALERR("`setiv' not supported");
577 if (!GC_CLASS(GCIPHER_C(me
))->blksz
) VALERR("not a block cipher mode");
578 if (sz
!= GC_CLASS(GCIPHER_C(me
))->blksz
) VALERR("bad IV length");
579 GC_SETIV(GCIPHER_C(me
), p
);
585 static PyObject
*gcmeth_bdry(PyObject
*me
, PyObject
*arg
)
587 if (!PyArg_ParseTuple(arg
, ":bdry")) goto end
;
588 if (!GCIPHER_C(me
)->ops
->bdry
) VALERR("`bdry' not supported");
589 if (!GC_CLASS(GCIPHER_C(me
))->blksz
) VALERR("not a block cipher mode");
590 GC_BDRY(GCIPHER_C(me
));
596 static PyGetSetDef gccipher_pygetset
[] = {
597 #define GETSETNAME(op, name) gcc##op##_##name
598 GET (keysz
, "CC.keysz -> acceptable key sizes")
599 GET (blksz
, "CC.blksz -> block size, or zero")
600 GET (name
, "CC.name -> name of this kind of cipher")
605 static PyMethodDef gcipher_pymethods
[] = {
606 #define METHNAME(name) gcmeth_##name
607 METH (encrypt
, "C.encrypt(PT) -> CT")
608 METH (enczero
, "C.enczero(N) -> CT")
609 METH (decrypt
, "C.decrypt(CT) -> PT")
610 METH (deczero
, "C.deczero(N) -> PT")
611 METH (setiv
, "C.setiv(IV)")
612 METH (bdry
, "C.bdry()")
617 static PyTypeObject gccipher_pytype_skel
= {
618 PyObject_HEAD_INIT(0) 0, /* Header */
619 "GCCipher", /* @tp_name@ */
620 sizeof(gccipher_pyobj
), /* @tp_basicsize@ */
621 0, /* @tp_itemsize@ */
623 0, /* @tp_dealloc@ */
625 0, /* @tp_getattr@ */
626 0, /* @tp_setattr@ */
627 0, /* @tp_compare@ */
629 0, /* @tp_as_number@ */
630 0, /* @tp_as_sequence@ */
631 0, /* @tp_as_mapping@ */
635 0, /* @tp_getattro@ */
636 0, /* @tp_setattro@ */
637 0, /* @tp_as_buffer@ */
638 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
642 "Symmetric cipher metaclass.",
644 0, /* @tp_traverse@ */
646 0, /* @tp_richcompare@ */
647 0, /* @tp_weaklistoffset@ */
649 0, /* @tp_iternext@ */
650 0, /* @tp_methods@ */
651 0, /* @tp_members@ */
652 gccipher_pygetset
, /* @tp_getset@ */
655 0, /* @tp_descr_get@ */
656 0, /* @tp_descr_set@ */
657 0, /* @tp_dictoffset@ */
659 PyType_GenericAlloc
, /* @tp_alloc@ */
660 abstract_pynew
, /* @tp_new@ */
665 static PyTypeObject gcipher_pytype_skel
= {
666 PyObject_HEAD_INIT(0) 0, /* Header */
667 "GCipher", /* @tp_name@ */
668 sizeof(gcipher_pyobj
), /* @tp_basicsize@ */
669 0, /* @tp_itemsize@ */
671 gcipher_pydealloc
, /* @tp_dealloc@ */
673 0, /* @tp_getattr@ */
674 0, /* @tp_setattr@ */
675 0, /* @tp_compare@ */
677 0, /* @tp_as_number@ */
678 0, /* @tp_as_sequence@ */
679 0, /* @tp_as_mapping@ */
683 0, /* @tp_getattro@ */
684 0, /* @tp_setattro@ */
685 0, /* @tp_as_buffer@ */
686 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
690 "Symmetric cipher, abstract base class.",
692 0, /* @tp_traverse@ */
694 0, /* @tp_richcompare@ */
695 0, /* @tp_weaklistoffset@ */
697 0, /* @tp_iternext@ */
698 gcipher_pymethods
, /* @tp_methods@ */
699 0, /* @tp_members@ */
703 0, /* @tp_descr_get@ */
704 0, /* @tp_descr_set@ */
705 0, /* @tp_dictoffset@ */
707 PyType_GenericAlloc
, /* @tp_alloc@ */
708 abstract_pynew
, /* @tp_new@ */
713 /*----- Hash functions ----------------------------------------------------*/
715 PyTypeObject
*gchash_pytype
, *ghash_pytype
;
717 CONVFUNC(gchash
, gchash
*, GCHASH_CH
)
718 CONVFUNC(ghash
, ghash
*, GHASH_H
)
720 static PyObject
*ghash_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
722 static const char *const kwlist
[] = { 0 };
723 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, ":new", KWLIST
))
725 return (ghash_pywrap((PyObject
*)ty
, GH_INIT(GCHASH_CH(ty
))));
730 PyObject
*gchash_pywrap(gchash
*ch
)
732 gchash_pyobj
*g
= newtype(gchash_pytype
, 0, ch
->name
);
734 g
->ty
.ht_type
.tp_basicsize
= sizeof(ghash_pyobj
);
735 g
->ty
.ht_type
.tp_base
= ghash_pytype
;
736 Py_INCREF(ghash_pytype
);
737 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
738 Py_TPFLAGS_BASETYPE
|
739 Py_TPFLAGS_HEAPTYPE
);
740 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
741 g
->ty
.ht_type
.tp_free
= 0;
742 g
->ty
.ht_type
.tp_new
= ghash_pynew
;
743 typeready(&g
->ty
.ht_type
);
744 return ((PyObject
*)g
);
747 PyObject
*ghash_pywrap(PyObject
*cobj
, ghash
*h
)
750 if (!cobj
) cobj
= gchash_pywrap((/*unconst*/ gchash
*)GH_CLASS(h
));
751 else Py_INCREF(cobj
);
752 g
= PyObject_NEW(ghash_pyobj
, (PyTypeObject
*)cobj
);
754 return ((PyObject
*)g
);
757 static void ghash_pydealloc(PyObject
*me
)
759 GH_DESTROY(GHASH_H(me
));
760 Py_DECREF(me
->ob_type
);
764 static PyObject
*gchget_name(PyObject
*me
, void *hunoz
)
765 { return (PyString_FromString(GCHASH_CH(me
)->name
)); }
767 static PyObject
*gchget_hashsz(PyObject
*me
, void *hunoz
)
768 { return (PyInt_FromLong(GCHASH_CH(me
)->hashsz
)); }
770 static PyObject
*gchget_bufsz(PyObject
*me
, void *hunoz
)
771 { return (PyInt_FromLong(GCHASH_CH(me
)->bufsz
)); }
773 static PyObject
*ghmeth_hash(PyObject
*me
, PyObject
*arg
)
777 if (!PyArg_ParseTuple(arg
, "s#:hash", &p
, &sz
)) return (0);
778 GH_HASH(GHASH_H(me
), p
, sz
);
782 #define GHMETH_HASHU_(n, W, w) \
783 static PyObject *ghmeth_hashu##w(PyObject *me, PyObject *arg) \
786 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \
787 GH_HASHU##W(GHASH_H(me), x); \
790 DOUINTCONV(GHMETH_HASHU_
)
792 #define GHMETH_HASHBUF_(n, W, w) \
793 static PyObject *ghmeth_hashbuf##w(PyObject *me, PyObject *arg) \
797 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
798 if (sz > MASK##n) TYERR("string too long"); \
799 GH_HASHBUF##W(GHASH_H(me), p, sz); \
804 DOUINTCONV(GHMETH_HASHBUF_
)
806 static PyObject
*ghmeth_hashstrz(PyObject
*me
, PyObject
*arg
)
809 if (!PyArg_ParseTuple(arg
, "s:hashstrz", &p
)) return (0);
810 GH_HASHSTRZ(GHASH_H(me
), p
);
814 static PyObject
*ghmeth_done(PyObject
*me
, PyObject
*arg
)
818 if (!PyArg_ParseTuple(arg
, ":done")) return (0);
819 g
= GH_COPY(GHASH_H(me
));
820 rc
= bytestring_pywrap(0, g
->ops
->c
->hashsz
);
821 GH_DONE(g
, PyString_AS_STRING(rc
));
826 static PyGetSetDef gchash_pygetset
[] = {
827 #define GETSETNAME(op, name) gch##op##_##name
828 GET (bufsz
, "CH.bufsz -> hash buffer size, or zero")
829 GET (hashsz
, "CH.hashsz -> hash output size")
830 GET (name
, "CH.name -> name of this kind of hash")
835 static PyMethodDef ghash_pymethods
[] = {
836 #define METHNAME(name) ghmeth_##name
837 METH (hash
, "H.hash(M)")
838 #define METHU_(n, W, w) METH(hashu##w, "H.hashu" #w "(WORD)")
841 #define METHBUF_(n, W, w) METH(hashbuf##w, "H.hashbuf" #w "(BYTES)")
844 METH (hashstrz
, "H.hashstrz(STRING)")
845 METH (done
, "H.done() -> HASH")
850 static PyTypeObject gchash_pytype_skel
= {
851 PyObject_HEAD_INIT(0) 0, /* Header */
852 "GCHash", /* @tp_name@ */
853 sizeof(gchash_pyobj
), /* @tp_basicsize@ */
854 0, /* @tp_itemsize@ */
856 0, /* @tp_dealloc@ */
858 0, /* @tp_getattr@ */
859 0, /* @tp_setattr@ */
860 0, /* @tp_compare@ */
862 0, /* @tp_as_number@ */
863 0, /* @tp_as_sequence@ */
864 0, /* @tp_as_mapping@ */
868 0, /* @tp_getattro@ */
869 0, /* @tp_setattro@ */
870 0, /* @tp_as_buffer@ */
871 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
875 "Hash function metaclass.",
877 0, /* @tp_traverse@ */
879 0, /* @tp_richcompare@ */
880 0, /* @tp_weaklistoffset@ */
882 0, /* @tp_iternext@ */
883 0, /* @tp_methods@ */
884 0, /* @tp_members@ */
885 gchash_pygetset
, /* @tp_getset@ */
888 0, /* @tp_descr_get@ */
889 0, /* @tp_descr_set@ */
890 0, /* @tp_dictoffset@ */
892 PyType_GenericAlloc
, /* @tp_alloc@ */
893 abstract_pynew
, /* @tp_new@ */
898 static PyTypeObject ghash_pytype_skel
= {
899 PyObject_HEAD_INIT(0) 0, /* Header */
900 "GHash", /* @tp_name@ */
901 sizeof(ghash_pyobj
), /* @tp_basicsize@ */
902 0, /* @tp_itemsize@ */
904 ghash_pydealloc
, /* @tp_dealloc@ */
906 0, /* @tp_getattr@ */
907 0, /* @tp_setattr@ */
908 0, /* @tp_compare@ */
910 0, /* @tp_as_number@ */
911 0, /* @tp_as_sequence@ */
912 0, /* @tp_as_mapping@ */
916 0, /* @tp_getattro@ */
917 0, /* @tp_setattro@ */
918 0, /* @tp_as_buffer@ */
919 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
923 "Hash function, abstract base class.",
925 0, /* @tp_traverse@ */
927 0, /* @tp_richcompare@ */
928 0, /* @tp_weaklistoffset@ */
930 0, /* @tp_iternext@ */
931 ghash_pymethods
, /* @tp_methods@ */
932 0, /* @tp_members@ */
936 0, /* @tp_descr_get@ */
937 0, /* @tp_descr_set@ */
938 0, /* @tp_dictoffset@ */
940 PyType_GenericAlloc
, /* @tp_alloc@ */
941 abstract_pynew
, /* @tp_new@ */
946 /*----- Message authentication --------------------------------------------*/
948 PyTypeObject
*gcmac_pytype
, *gmac_pytype
, *gmhash_pytype
;
950 CONVFUNC(gcmac
, gcmac
*, GCMAC_CM
)
951 CONVFUNC(gmac
, gmac
*, GMAC_M
)
952 CONVFUNC(gmhash
, ghash
*, GHASH_H
)
954 static PyObject
*gmac_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
956 static const char *const kwlist
[] = { "k", 0 };
960 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", KWLIST
, &k
, &sz
))
962 if (keysz(sz
, GCMAC_CM(ty
)->keysz
) != sz
) VALERR("bad key length");
963 return (gmac_pywrap((PyObject
*)ty
,
964 GM_KEY(GCMAC_CM(ty
), k
, sz
)));
969 static PyObject
*gmhash_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
971 static const char *const kwlist
[] = { 0 };
974 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, ":new", KWLIST
)) return (0);
975 g
= PyObject_NEW(ghash_pyobj
, ty
);
976 g
->h
= GM_INIT(GMAC_M(ty
));
978 return ((PyObject
*)g
);
981 PyObject
*gcmac_pywrap(gcmac
*cm
)
983 gcmac_pyobj
*g
= newtype(gcmac_pytype
, 0, cm
->name
);
985 g
->ty
.ht_type
.tp_basicsize
= sizeof(gmac_pyobj
);
986 g
->ty
.ht_type
.tp_base
= gmac_pytype
;
987 Py_INCREF(gmac_pytype
);
988 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
989 Py_TPFLAGS_BASETYPE
|
990 Py_TPFLAGS_HEAPTYPE
);
991 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
992 g
->ty
.ht_type
.tp_free
= 0;
993 g
->ty
.ht_type
.tp_new
= gmac_pynew
;
994 typeready(&g
->ty
.ht_type
);
995 return ((PyObject
*)g
);
998 PyObject
*gmac_pywrap(PyObject
*cobj
, gmac
*m
)
1001 if (!cobj
) cobj
= gcmac_pywrap((/*unconst*/ gcmac
*)GM_CLASS(m
));
1002 else Py_INCREF(cobj
);
1003 g
= newtype((PyTypeObject
*)cobj
, 0, 0);
1004 g
->ty
.ht_type
.tp_basicsize
= sizeof(ghash_pyobj
);
1005 g
->ty
.ht_name
= PyString_FromFormat("%s(keyed)", m
->ops
->c
->name
);
1006 g
->ty
.ht_type
.tp_name
= PyString_AS_STRING(g
->ty
.ht_name
);
1007 g
->ty
.ht_type
.tp_base
= gmhash_pytype
;
1008 Py_INCREF(gmac_pytype
);
1009 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
1010 Py_TPFLAGS_BASETYPE
|
1011 Py_TPFLAGS_HEAPTYPE
);
1012 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
1013 g
->ty
.ht_type
.tp_free
= 0;
1014 g
->ty
.ht_type
.tp_new
= gmhash_pynew
;
1015 typeready(&g
->ty
.ht_type
);
1017 return ((PyObject
*)g
);
1020 static void gmac_pydealloc(PyObject
*me
)
1022 GM_DESTROY(GMAC_M(me
));
1023 Py_DECREF(me
->ob_type
);
1024 PyType_Type
.tp_dealloc(me
);
1027 static PyObject
*gcmget_name(PyObject
*me
, void *hunoz
)
1028 { return (PyString_FromString(GCMAC_CM(me
)->name
)); }
1030 static PyObject
*gcmget_keysz(PyObject
*me
, void *hunoz
)
1031 { return (keysz_pywrap(GCMAC_CM(me
)->keysz
)); }
1033 static PyObject
*gcmget_tagsz(PyObject
*me
, void *hunoz
)
1034 { return (PyInt_FromLong(GCMAC_CM(me
)->hashsz
)); }
1036 static PyGetSetDef gcmac_pygetset
[] = {
1037 #define GETSETNAME(op, name) gcm##op##_##name
1038 GET (keysz
, "CM.keysz -> acceptable key sizes")
1039 GET (tagsz
, "CM.tagsz -> MAC output size")
1040 GET (name
, "CM.name -> name of this kind of MAC")
1045 static PyTypeObject gcmac_pytype_skel
= {
1046 PyObject_HEAD_INIT(0) 0, /* Header */
1047 "GCMAC", /* @tp_name@ */
1048 sizeof(gchash_pyobj
), /* @tp_basicsize@ */
1049 0, /* @tp_itemsize@ */
1051 0, /* @tp_dealloc@ */
1053 0, /* @tp_getattr@ */
1054 0, /* @tp_setattr@ */
1055 0, /* @tp_compare@ */
1057 0, /* @tp_as_number@ */
1058 0, /* @tp_as_sequence@ */
1059 0, /* @tp_as_mapping@ */
1063 0, /* @tp_getattro@ */
1064 0, /* @tp_setattro@ */
1065 0, /* @tp_as_buffer@ */
1066 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1067 Py_TPFLAGS_BASETYPE
,
1070 "Message authentication code metametaclass.",
1072 0, /* @tp_traverse@ */
1074 0, /* @tp_richcompare@ */
1075 0, /* @tp_weaklistoffset@ */
1077 0, /* @tp_iternext@ */
1078 0, /* @tp_methods@ */
1079 0, /* @tp_members@ */
1080 gcmac_pygetset
, /* @tp_getset@ */
1083 0, /* @tp_descr_get@ */
1084 0, /* @tp_descr_set@ */
1085 0, /* @tp_dictoffset@ */
1087 PyType_GenericAlloc
, /* @tp_alloc@ */
1088 abstract_pynew
, /* @tp_new@ */
1093 static PyTypeObject gmac_pytype_skel
= {
1094 PyObject_HEAD_INIT(0) 0, /* Header */
1095 "GMAC", /* @tp_name@ */
1096 sizeof(gmac_pyobj
), /* @tp_basicsize@ */
1097 0, /* @tp_itemsize@ */
1099 gmac_pydealloc
, /* @tp_dealloc@ */
1101 0, /* @tp_getattr@ */
1102 0, /* @tp_setattr@ */
1103 0, /* @tp_compare@ */
1105 0, /* @tp_as_number@ */
1106 0, /* @tp_as_sequence@ */
1107 0, /* @tp_as_mapping@ */
1111 0, /* @tp_getattro@ */
1112 0, /* @tp_setattro@ */
1113 0, /* @tp_as_buffer@ */
1114 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1115 Py_TPFLAGS_BASETYPE
,
1118 "Message authentication code metaclass, abstract base class.",
1120 0, /* @tp_traverse@ */
1122 0, /* @tp_richcompare@ */
1123 0, /* @tp_weaklistoffset@ */
1125 0, /* @tp_iternext@ */
1126 0, /* @tp_methods@ */
1127 0, /* @tp_members@ */
1128 0, /* @tp_getset@ */
1131 0, /* @tp_descr_get@ */
1132 0, /* @tp_descr_set@ */
1133 0, /* @tp_dictoffset@ */
1135 PyType_GenericAlloc
, /* @tp_alloc@ */
1136 abstract_pynew
, /* @tp_new@ */
1141 static PyTypeObject gmhash_pytype_skel
= {
1142 PyObject_HEAD_INIT(0) 0, /* Header */
1143 "GMACHash", /* @tp_name@ */
1144 sizeof(ghash_pyobj
), /* @tp_basicsize@ */
1145 0, /* @tp_itemsize@ */
1147 ghash_pydealloc
, /* @tp_dealloc@ */
1149 0, /* @tp_getattr@ */
1150 0, /* @tp_setattr@ */
1151 0, /* @tp_compare@ */
1153 0, /* @tp_as_number@ */
1154 0, /* @tp_as_sequence@ */
1155 0, /* @tp_as_mapping@ */
1159 0, /* @tp_getattro@ */
1160 0, /* @tp_setattro@ */
1161 0, /* @tp_as_buffer@ */
1162 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1163 Py_TPFLAGS_BASETYPE
,
1166 "Message authentication code, abstract base class.",
1168 0, /* @tp_traverse@ */
1170 0, /* @tp_richcompare@ */
1171 0, /* @tp_weaklistoffset@ */
1173 0, /* @tp_iternext@ */
1174 0, /* @tp_methods@ */
1175 0, /* @tp_members@ */
1176 0, /* @tp_getset@ */
1179 0, /* @tp_descr_get@ */
1180 0, /* @tp_descr_set@ */
1181 0, /* @tp_dictoffset@ */
1183 PyType_GenericAlloc
, /* @tp_alloc@ */
1184 abstract_pynew
, /* @tp_new@ */
1189 /*----- Special snowflake for Poly1305 ------------------------------------*/
1191 PyTypeObject
*poly1305cls_pytype
, *poly1305key_pytype
, *poly1305hash_pytype
;
1193 typedef struct poly1305key_pyobj
{
1194 PyHeapTypeObject ty
;
1196 } poly1305key_pyobj
;
1198 typedef struct poly1305hash_pyobj
{
1203 } poly1305hash_pyobj
;
1205 #define P1305_F(o) (((poly1305hash_pyobj *)(o))->f)
1206 #define P1305_CTX(o) (&((poly1305hash_pyobj *)(o))->ctx)
1207 CONVFUNC(poly1305hash
, poly1305_ctx
*, P1305_CTX
)
1209 static PyObject
*poly1305hash_pynew(PyTypeObject
*ty
,
1210 PyObject
*arg
, PyObject
*kw
)
1212 static const char *const kwlist
[] = { "mask", 0 };
1213 poly1305key_pyobj
*pk
= (poly1305key_pyobj
*)ty
;
1214 poly1305hash_pyobj
*ph
;
1218 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|s#:new", KWLIST
, &m
, &sz
))
1220 if (m
&& sz
!= POLY1305_MASKSZ
) VALERR("bad mask length");
1221 ph
= PyObject_NEW(poly1305hash_pyobj
, ty
);
1223 if (m
) ph
->f
|= f_mask
;
1224 poly1305_macinit(&ph
->ctx
, &pk
->k
, m
);
1226 return ((PyObject
*)ph
);
1231 static PyObject
*poly1305key_pynew(PyTypeObject
*ty
,
1232 PyObject
*arg
, PyObject
*kw
)
1234 static const char *const kwlist
[] = { "k", 0 };
1235 poly1305key_pyobj
*pk
;
1239 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", KWLIST
, &k
, &sz
))
1241 if (keysz(sz
, poly1305_keysz
) != sz
) VALERR("bad key length");
1243 pk
= newtype(ty
, 0, 0);
1244 pk
->ty
.ht_name
= PyString_FromString("poly1305(keyed)");
1245 pk
->ty
.ht_type
.tp_basicsize
= sizeof(poly1305hash_pyobj
);
1246 pk
->ty
.ht_type
.tp_name
= PyString_AS_STRING(pk
->ty
.ht_name
);
1247 pk
->ty
.ht_type
.tp_base
= poly1305hash_pytype
;
1248 Py_INCREF(poly1305key_pytype
);
1249 pk
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
1250 Py_TPFLAGS_BASETYPE
|
1251 Py_TPFLAGS_HEAPTYPE
);
1252 pk
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
1253 pk
->ty
.ht_type
.tp_free
= 0;
1254 pk
->ty
.ht_type
.tp_new
= poly1305hash_pynew
;
1255 typeready(&pk
->ty
.ht_type
);
1257 poly1305_keyinit(&pk
->k
, k
, sz
);
1258 return ((PyObject
*)pk
);
1264 static PyObject
*poly1305clsget_name(PyObject
*me
, void *hunoz
)
1265 { return (PyString_FromString("poly1305")); }
1267 static PyObject
*poly1305clsget_keysz(PyObject
*me
, void *hunoz
)
1268 { return (keysz_pywrap(poly1305_keysz
)); }
1270 static PyObject
*poly1305clsget_masksz(PyObject
*me
, void *hunoz
)
1271 { return (PyInt_FromLong(POLY1305_MASKSZ
)); }
1273 static PyObject
*poly1305clsget_tagsz(PyObject
*me
, void *hunoz
)
1274 { return (PyInt_FromLong(POLY1305_TAGSZ
)); }
1276 static PyObject
*polymeth_copy(PyObject
*me
, PyObject
*arg
)
1278 poly1305hash_pyobj
*ph
;
1279 if (!PyArg_ParseTuple(arg
, ":copy")) return (0);
1280 ph
= PyObject_NEW(poly1305hash_pyobj
, me
->ob_type
);
1281 poly1305_copy(&ph
->ctx
, P1305_CTX(me
));
1282 Py_INCREF(me
->ob_type
);
1283 return ((PyObject
*)ph
);
1286 static PyObject
*polymeth_hash(PyObject
*me
, PyObject
*arg
)
1290 if (!PyArg_ParseTuple(arg
, "s#:hash", &p
, &sz
)) return (0);
1291 poly1305_hash(P1305_CTX(me
), p
, sz
);
1295 #define POLYMETH_HASHU_(n, W, w) \
1296 static PyObject *polymeth_hashu##w(PyObject *me, PyObject *arg) \
1300 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \
1301 STORE##W(b, x); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \
1304 DOUINTCONV(POLYMETH_HASHU_
)
1306 #define POLYMETH_HASHBUF_(n, W, w) \
1307 static PyObject *polymeth_hashbuf##w(PyObject *me, PyObject *arg) \
1312 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
1313 if (sz > MASK##n) TYERR("string too long"); \
1314 STORE##W(b, sz); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \
1315 poly1305_hash(P1305_CTX(me), p, sz); \
1320 DOUINTCONV(POLYMETH_HASHBUF_
)
1322 static PyObject
*polymeth_hashstrz(PyObject
*me
, PyObject
*arg
)
1325 if (!PyArg_ParseTuple(arg
, "s:hashstrz", &p
)) return (0);
1326 poly1305_hash(P1305_CTX(me
), p
, strlen(p
) + 1);
1330 static PyObject
*polymeth_flush(PyObject
*me
, PyObject
*arg
)
1332 if (!PyArg_ParseTuple(arg
, ":flush")) return (0);
1333 poly1305_flush(P1305_CTX(me
));
1337 static PyObject
*polymeth_flushzero(PyObject
*me
, PyObject
*arg
)
1339 if (!PyArg_ParseTuple(arg
, ":flushzero")) return (0);
1340 poly1305_flushzero(P1305_CTX(me
));
1344 static PyObject
*polymeth_concat(PyObject
*me
, PyObject
*arg
)
1346 PyObject
*pre
, *suff
;
1347 if (!PyArg_ParseTuple(arg
, "OO:concat", &pre
, &suff
)) return (0);
1348 if (!PyObject_TypeCheck(pre
, poly1305hash_pytype
) ||
1349 !PyObject_TypeCheck(suff
, poly1305hash_pytype
))
1350 TYERR("wanted a poly1305hash");
1351 if (me
->ob_type
!= pre
->ob_type
|| me
->ob_type
!= suff
->ob_type
)
1352 TYERR("key mismatch");
1353 if (P1305_CTX(pre
)->nbuf
) VALERR("prefix is not block-aligned");
1354 poly1305_concat(P1305_CTX(me
), P1305_CTX(pre
), P1305_CTX(suff
));
1360 static PyObject
*polymeth_done(PyObject
*me
, PyObject
*arg
)
1363 if (!PyArg_ParseTuple(arg
, ":done")) return (0);
1364 if (!(P1305_F(me
) & f_mask
)) VALERR("no mask");
1365 rc
= bytestring_pywrap(0, POLY1305_TAGSZ
);
1366 poly1305_done(P1305_CTX(me
), PyString_AS_STRING(rc
));
1372 static PyGetSetDef poly1305cls_pygetset
[] = {
1373 #define GETSETNAME(op, name) poly1305cls##op##_##name
1374 GET (keysz
, "PC.keysz -> acceptable key sizes")
1375 GET (masksz
, "PC.masksz -> mask size")
1376 GET (tagsz
, "PC.tagsz -> MAC output size")
1377 GET (name
, "PC.name -> name of this kind of MAC")
1382 static PyMethodDef poly1305hash_pymethods
[] = {
1383 #define METHNAME(name) polymeth_##name
1384 METH (copy
, "P.copy() -> PP")
1385 METH (hash
, "P.hash(M)")
1386 #define METHU_(n, W, w) METH(hashu##w, "P.hashu" #w "(WORD)")
1389 #define METHBUF_(n, W, w) METH(hashbuf##w, "P.hashbuf" #w "(BYTES)")
1390 DOUINTCONV(METHBUF_
)
1392 METH (hashstrz
, "P.hashstrz(STRING)")
1393 METH (flush
, "P.flush()")
1394 METH (flushzero
, "P.flushzero()")
1395 METH (concat
, "P.concat(PREFIX, SUFFIX)")
1396 METH (done
, "P.done() -> TAG")
1401 static PyTypeObject poly1305cls_pytype_skel
= {
1402 PyObject_HEAD_INIT(0) 0, /* Header */
1403 "Poly1305Class", /* @tp_name@ */
1404 sizeof(PyHeapTypeObject
), /* @tp_basicsize@ */
1405 0, /* @tp_itemsize@ */
1407 0, /* @tp_dealloc@ */
1409 0, /* @tp_getattr@ */
1410 0, /* @tp_setattr@ */
1411 0, /* @tp_compare@ */
1413 0, /* @tp_as_number@ */
1414 0, /* @tp_as_sequence@ */
1415 0, /* @tp_as_mapping@ */
1419 0, /* @tp_getattro@ */
1420 0, /* @tp_setattro@ */
1421 0, /* @tp_as_buffer@ */
1422 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1423 Py_TPFLAGS_BASETYPE
,
1426 "Poly1305 metametaclass. Best not to ask.",
1428 0, /* @tp_traverse@ */
1430 0, /* @tp_richcompare@ */
1431 0, /* @tp_weaklistoffset@ */
1433 0, /* @tp_iternext@ */
1434 0, /* @tp_methods@ */
1435 0, /* @tp_members@ */
1436 poly1305cls_pygetset
, /* @tp_getset@ */
1439 0, /* @tp_descr_get@ */
1440 0, /* @tp_descr_set@ */
1441 0, /* @tp_dictoffset@ */
1443 PyType_GenericAlloc
, /* @tp_alloc@ */
1444 abstract_pynew
, /* @tp_new@ */
1449 static PyTypeObject poly1305key_pytype_skel
= {
1450 PyObject_HEAD_INIT(0) 0, /* Header */
1451 "poly1305", /* @tp_name@ */
1452 sizeof(poly1305key_pyobj
), /* @tp_basicsize@ */
1453 0, /* @tp_itemsize@ */
1455 0, /* @tp_dealloc@ */
1457 0, /* @tp_getattr@ */
1458 0, /* @tp_setattr@ */
1459 0, /* @tp_compare@ */
1461 0, /* @tp_as_number@ */
1462 0, /* @tp_as_sequence@ */
1463 0, /* @tp_as_mapping@ */
1467 0, /* @tp_getattro@ */
1468 0, /* @tp_setattro@ */
1469 0, /* @tp_as_buffer@ */
1470 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1471 Py_TPFLAGS_BASETYPE
,
1474 "poly1305(K): Poly1305 key.",
1476 0, /* @tp_traverse@ */
1478 0, /* @tp_richcompare@ */
1479 0, /* @tp_weaklistoffset@ */
1481 0, /* @tp_iternext@ */
1482 0, /* @tp_methods@ */
1483 0, /* @tp_members@ */
1484 0, /* @tp_getset@ */
1487 0, /* @tp_descr_get@ */
1488 0, /* @tp_descr_set@ */
1489 0, /* @tp_dictoffset@ */
1491 PyType_GenericAlloc
, /* @tp_alloc@ */
1492 poly1305key_pynew
, /* @tp_new@ */
1497 static PyTypeObject poly1305hash_pytype_skel
= {
1498 PyObject_HEAD_INIT(0) 0, /* Header */
1499 "Poly1305Hash", /* @tp_name@ */
1500 sizeof(poly1305hash_pyobj
), /* @tp_basicsize@ */
1501 0, /* @tp_itemsize@ */
1503 0, /* @tp_dealloc@ */
1505 0, /* @tp_getattr@ */
1506 0, /* @tp_setattr@ */
1507 0, /* @tp_compare@ */
1509 0, /* @tp_as_number@ */
1510 0, /* @tp_as_sequence@ */
1511 0, /* @tp_as_mapping@ */
1515 0, /* @tp_getattro@ */
1516 0, /* @tp_setattro@ */
1517 0, /* @tp_as_buffer@ */
1518 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1519 Py_TPFLAGS_BASETYPE
,
1522 "Poly1305 MAC context base class.",
1524 0, /* @tp_traverse@ */
1526 0, /* @tp_richcompare@ */
1527 0, /* @tp_weaklistoffset@ */
1529 0, /* @tp_iternext@ */
1530 poly1305hash_pymethods
, /* @tp_methods@ */
1531 0, /* @tp_members@ */
1532 0, /* @tp_getset@ */
1535 0, /* @tp_descr_get@ */
1536 0, /* @tp_descr_set@ */
1537 0, /* @tp_dictoffset@ */
1539 PyType_GenericAlloc
, /* @tp_alloc@ */
1540 abstract_pynew
, /* @tp_new@ */
1545 /*----- Special snowflake for HSalsa and HChaCha --------------------------*/
1547 #define DEF_HDANCE(DANCE, HDANCE, dance, hdance) \
1548 static PyObject *meth_##hdance##_prf(PyObject *me, PyObject *arg) \
1550 dance##_ctx dance; \
1552 Py_ssize_t ksz, nsz; \
1554 if (!PyArg_ParseTuple(arg, "s#s#:" #hdance "_prf", \
1555 &k, &ksz, &n, &nsz)) \
1557 if (ksz != DANCE##_KEYSZ) VALERR("bad key length"); \
1558 if (nsz != HDANCE##_INSZ) VALERR("bad input length"); \
1559 rc = bytestring_pywrap(0, HSALSA20_OUTSZ); \
1560 dance##_init(&dance, k, ksz, 0); \
1561 hdance##_prf(&dance, n, PyString_AS_STRING(rc)); \
1567 DEF_HDANCE(SALSA20
, HSALSA20
, salsa20
, hsalsa20
)
1568 DEF_HDANCE(SALSA20
, HSALSA20
, salsa20
, hsalsa2012
)
1569 DEF_HDANCE(SALSA20
, HSALSA20
, salsa20
, hsalsa208
)
1571 DEF_HDANCE(CHACHA
, HCHACHA
, chacha
, hchacha20
)
1572 DEF_HDANCE(CHACHA
, HCHACHA
, chacha
, hchacha12
)
1573 DEF_HDANCE(CHACHA
, HCHACHA
, chacha
, hchacha8
)
1575 /*----- Keccak-p[1600, n] -------------------------------------------------*/
1577 static PyTypeObject
*kxvik_pytype
;
1579 typedef struct kxvik_pyobj
{
1585 static PyObject
*kxvik_pynew(PyTypeObject
*ty
,
1586 PyObject
*arg
, PyObject
*kw
)
1589 kxvik_pyobj
*rc
= 0;
1590 static const char *const kwlist
[] = { "nround", 0 };
1591 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:new", KWLIST
,
1594 rc
= (kxvik_pyobj
*)ty
->tp_alloc(ty
, 0);
1596 keccak1600_init(&rc
->s
);
1598 return ((PyObject
*)rc
);
1601 static PyObject
*kxvikmeth_mix(PyObject
*me
, PyObject
*arg
)
1603 kxvik_pyobj
*k
= (kxvik_pyobj
*)me
;
1608 char *p
; Py_ssize_t n
;
1610 if (!PyArg_ParseTuple(arg
, "s#:mix", &p
, &n
)) goto end
;
1611 if (n
> 200) VALERR("out of range");
1612 q
= (const octet
*)p
;
1614 while (n
> 8) { LOAD64_L_(t
[i
], q
); i
++; q
+= 8; n
-= 8; }
1616 memcpy(buf
, q
, n
); memset(buf
+ n
, 0, 8 - n
);
1617 LOAD64_L_(t
[i
], buf
); i
++;
1619 keccak1600_mix(&k
->s
, t
, i
);
1625 static PyObject
*kxvikmeth_extract(PyObject
*me
, PyObject
*arg
)
1627 kxvik_pyobj
*k
= (kxvik_pyobj
*)me
;
1634 if (!PyArg_ParseTuple(arg
, "O&:mix", convuint
, &n
)) goto end
;
1635 if (n
> 200) VALERR("out of range");
1636 rc
= bytestring_pywrap(0, n
);
1637 q
= (octet
*)PyString_AS_STRING(rc
);
1638 keccak1600_extract(&k
->s
, t
, (n
+ 7)/8);
1640 while (n
> 8) { STORE64_L_(q
, t
[i
]); i
++; q
+= 8; n
-= 8; }
1641 if (n
) { STORE64_L_(buf
, t
[i
]); memcpy(q
, buf
, n
); }
1646 static PyObject
*kxvikmeth_step(PyObject
*me
, PyObject
*arg
)
1648 kxvik_pyobj
*k
= (kxvik_pyobj
*)me
;
1649 if (!PyArg_ParseTuple(arg
, ":step")) return (0);
1650 keccak1600_p(&k
->s
, &k
->s
, k
->n
);
1654 static PyObject
*kxvikget_nround(PyObject
*me
, void *hunoz
)
1656 kxvik_pyobj
*k
= (kxvik_pyobj
*)me
;
1657 return (PyInt_FromLong(k
->n
));
1660 static int kxvikset_nround(PyObject
*me
, PyObject
*val
, void *hunoz
)
1662 kxvik_pyobj
*k
= (kxvik_pyobj
*)me
;
1665 if (!convuint(val
, &n
)) return (-1);
1670 static PyGetSetDef kxvik_pygetset
[] = {
1671 #define GETSETNAME(op, name) kxvik##op##_##name
1672 GETSET(nround
, "KECCAK.nround -> number of rounds")
1677 static PyMethodDef kxvik_pymethods
[] = {
1678 #define METHNAME(func) kxvikmeth_##func
1679 METH (mix
, "KECCAK.mix(DATA)")
1680 METH (extract
, "KECCAK.extract(NOCTETS)")
1681 METH (step
, "KECCAK.step()")
1686 static PyTypeObject kxvik_pytype_skel
= {
1687 PyObject_HEAD_INIT(0) 0, /* Header */
1688 "Keccak1600", /* @tp_name@ */
1689 sizeof(kxvik_pyobj
), /* @tp_basicsize@ */
1690 0, /* @tp_itemsize@ */
1692 0, /* @tp_dealloc@ */
1694 0, /* @tp_getattr@ */
1695 0, /* @tp_setattr@ */
1696 0, /* @tp_compare@ */
1698 0, /* @tp_as_number@ */
1699 0, /* @tp_as_sequence@ */
1700 0, /* @tp_as_mapping@ */
1704 0, /* @tp_getattro@ */
1705 0, /* @tp_setattro@ */
1706 0, /* @tp_as_buffer@ */
1707 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1708 Py_TPFLAGS_BASETYPE
,
1711 "Keccak1600([nround = 24]): Keccak-p[1600, n] state.",
1713 0, /* @tp_traverse@ */
1715 0, /* @tp_richcompare@ */
1716 0, /* @tp_weaklistoffset@ */
1718 0, /* @tp_iternext@ */
1719 kxvik_pymethods
, /* @tp_methods@ */
1720 0, /* @tp_members@ */
1721 kxvik_pygetset
, /* @tp_getset@ */
1724 0, /* @tp_descr_get@ */
1725 0, /* @tp_descr_set@ */
1726 0, /* @tp_dictoffset@ */
1728 PyType_GenericAlloc
, /* @tp_alloc@ */
1729 kxvik_pynew
, /* @tp_new@ */
1734 static PyTypeObject
*shake_pytype
, *shake128_pytype
, *shake256_pytype
;
1736 typedef struct shake_pyobj
{
1742 #define SHAKE_H(o) (&((shake_pyobj *)(o))->h)
1743 #define SHAKE_ST(o) (((shake_pyobj *)(o))->st)
1745 static PyObject
*shake_dopynew(void (*initfn
)(shake_ctx
*,
1746 const void *, size_t,
1747 const void *, size_t),
1749 PyObject
*arg
, PyObject
*kw
)
1751 shake_pyobj
*rc
= 0;
1752 char *p
= 0, *f
= 0;
1753 Py_ssize_t psz
= 0, fsz
= 0;
1754 static const char *const kwlist
[] = { "perso", "func", 0 };
1756 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|s#s#:new", KWLIST
,
1757 &p
, &psz
, &f
, &fsz
))
1759 rc
= (shake_pyobj
*)ty
->tp_alloc(ty
, 0);
1760 initfn(&rc
->h
, f
, fsz
, p
, psz
);
1763 return ((PyObject
*)rc
);
1766 static PyObject
*shake128_pynew(PyTypeObject
*ty
,
1767 PyObject
*arg
, PyObject
*kw
)
1768 { return (shake_dopynew(cshake128_init
, ty
, arg
, kw
)); }
1770 static PyObject
*shake256_pynew(PyTypeObject
*ty
,
1771 PyObject
*arg
, PyObject
*kw
)
1772 { return (shake_dopynew(cshake256_init
, ty
, arg
, kw
)); }
1774 static int shake_check(PyObject
*me
, int st
)
1776 if (SHAKE_ST(me
) != st
) VALERR("wrong state");
1782 static PyObject
*shakemeth_hash(PyObject
*me
, PyObject
*arg
)
1786 if (!PyArg_ParseTuple(arg
, "s#:hash", &p
, &sz
)) return (0);
1787 if (shake_check(me
, 0)) return (0);
1788 shake_hash(SHAKE_H(me
), p
, sz
);
1792 #define SHAKEMETH_HASHU_(n, W, w) \
1793 static PyObject *shakemeth_hashu##w(PyObject *me, PyObject *arg) \
1797 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \
1798 if (shake_check(me, 0)) return (0); \
1799 STORE##W(b, x); shake_hash(SHAKE_H(me), b, sizeof(b)); \
1802 DOUINTCONV(SHAKEMETH_HASHU_
)
1804 #define SHAKEMETH_HASHBUF_(n, W, w) \
1805 static PyObject *shakemeth_hashbuf##w(PyObject *me, PyObject *arg) \
1810 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
1811 if (sz > MASK##n) TYERR("string too long"); \
1812 if (shake_check(me, 0)) goto end; \
1813 STORE##W(b, sz); shake_hash(SHAKE_H(me), b, sizeof(b)); \
1814 shake_hash(SHAKE_H(me), p, sz); \
1819 DOUINTCONV(SHAKEMETH_HASHBUF_
)
1821 static PyObject
*shakemeth_hashstrz(PyObject
*me
, PyObject
*arg
)
1824 if (!PyArg_ParseTuple(arg
, "s:hashstrz", &p
)) return (0);
1825 if (shake_check(me
, 0)) return (0);
1826 shake_hash(SHAKE_H(me
), p
, strlen(p
) + 1);
1830 static PyObject
*shakemeth_xof(PyObject
*me
, PyObject
*arg
)
1832 if (!PyArg_ParseTuple(arg
, ":xof")) goto end
;
1833 if (shake_check(me
, 0)) goto end
;
1834 shake_xof(SHAKE_H(me
));
1841 static PyObject
*shakemeth_done(PyObject
*me
, PyObject
*arg
)
1845 if (!PyArg_ParseTuple(arg
, "O&:done", convszt
, &n
)) goto end
;
1846 if (shake_check(me
, 0)) goto end
;
1847 rc
= bytestring_pywrap(0, n
);
1848 shake_done(SHAKE_H(me
), PyString_AS_STRING(rc
), n
);
1854 static PyObject
*shakemeth_copy(PyObject
*me
, PyObject
*arg
)
1856 shake_pyobj
*rc
= 0;
1858 if (!PyArg_ParseTuple(arg
, ":copy")) goto end
;
1859 rc
= PyObject_NEW(shake_pyobj
, me
->ob_type
);
1860 rc
->h
= *SHAKE_H(me
);
1861 rc
->st
= SHAKE_ST(me
);
1863 return ((PyObject
*)me
);
1866 static PyObject
*shakemeth_get(PyObject
*me
, PyObject
*arg
)
1871 if (!PyArg_ParseTuple(arg
, "O&:get", convszt
, &sz
)) goto end
;
1872 if (shake_check(me
, 1)) goto end
;
1873 rc
= bytestring_pywrap(0, sz
);
1874 shake_get(SHAKE_H(me
), PyString_AS_STRING(rc
), sz
);
1879 static PyObject
*shakemeth_mask(PyObject
*me
, PyObject
*arg
)
1882 char *p
; Py_ssize_t sz
;
1884 if (!PyArg_ParseTuple(arg
, "s#:mask", &p
, &sz
)) goto end
;
1885 if (shake_check(me
, 1)) goto end
;
1886 rc
= bytestring_pywrap(0, sz
);
1887 shake_mask(SHAKE_H(me
), p
, PyString_AS_STRING(rc
), sz
);
1892 static PyObject
*shakeget_rate(PyObject
*me
, void *hunoz
)
1893 { return (PyInt_FromLong(SHAKE_H(me
)->h
.r
)); }
1895 static PyObject
*shakeget_buffered(PyObject
*me
, void *hunoz
)
1896 { return (PyInt_FromLong(SHAKE_H(me
)->h
.n
)); }
1898 static PyObject
*shakeget_state(PyObject
*me
, void *hunoz
)
1900 int st
= SHAKE_ST(me
);
1901 return (PyString_FromString(st
== 0 ?
"absorb" :
1902 st
== 1 ?
"squeeze" : "dead"));
1905 static PyGetSetDef shake_pygetset
[] = {
1906 #define GETSETNAME(op, name) shake##op##_##name
1907 GET (rate
, "S.rate -> rate, in bytes")
1908 GET (buffered
, "S.buffered -> amount currently buffered")
1909 GET (state
, "S.state -> `absorb', `squeeze', `dead'")
1914 static PyMethodDef shake_pymethods
[] = {
1915 #define METHNAME(func) shakemeth_##func
1916 METH (copy
, "S.copy() -> SS")
1917 METH (hash
, "S.hash(M)")
1918 #define METHU_(n, W, w) METH(hashu##w, "S.hashu" #w "(WORD)")
1921 #define METHBUF_(n, W, w) METH(hashbuf##w, "S.hashbuf" #w "(BYTES)")
1922 DOUINTCONV(METHBUF_
)
1924 METH (hashstrz
, "S.hashstrz(STRING)")
1925 METH (xof
, "S.xof()")
1926 METH (done
, "S.done(LEN) ->H")
1927 METH (get
, "S.get(LEN) -> H")
1928 METH (mask
, "S.mask(M) -> C")
1933 static PyTypeObject shake_pytype_skel
= {
1934 PyObject_HEAD_INIT(0) 0, /* Header */
1935 "Shake", /* @tp_name@ */
1936 sizeof(shake_pyobj
), /* @tp_basicsize@ */
1937 0, /* @tp_itemsize@ */
1939 0, /* @tp_dealloc@ */
1941 0, /* @tp_getattr@ */
1942 0, /* @tp_setattr@ */
1943 0, /* @tp_compare@ */
1945 0, /* @tp_as_number@ */
1946 0, /* @tp_as_sequence@ */
1947 0, /* @tp_as_mapping@ */
1951 0, /* @tp_getattro@ */
1952 0, /* @tp_setattro@ */
1953 0, /* @tp_as_buffer@ */
1954 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1955 Py_TPFLAGS_BASETYPE
,
1958 "SHAKE/cSHAKE base class.",
1960 0, /* @tp_traverse@ */
1962 0, /* @tp_richcompare@ */
1963 0, /* @tp_weaklistoffset@ */
1965 0, /* @tp_iternext@ */
1966 shake_pymethods
, /* @tp_methods@ */
1967 0, /* @tp_members@ */
1968 shake_pygetset
, /* @tp_getset@ */
1971 0, /* @tp_descr_get@ */
1972 0, /* @tp_descr_set@ */
1973 0, /* @tp_dictoffset@ */
1975 PyType_GenericAlloc
, /* @tp_alloc@ */
1976 abstract_pynew
, /* @tp_new@ */
1981 static PyTypeObject shake128_pytype_skel
= {
1982 PyObject_HEAD_INIT(0) 0, /* Header */
1983 "Shake128", /* @tp_name@ */
1984 0, /* @tp_basicsize@ */
1985 0, /* @tp_itemsize@ */
1987 0, /* @tp_dealloc@ */
1989 0, /* @tp_getattr@ */
1990 0, /* @tp_setattr@ */
1991 0, /* @tp_compare@ */
1993 0, /* @tp_as_number@ */
1994 0, /* @tp_as_sequence@ */
1995 0, /* @tp_as_mapping@ */
1999 0, /* @tp_getattro@ */
2000 0, /* @tp_setattro@ */
2001 0, /* @tp_as_buffer@ */
2002 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2003 Py_TPFLAGS_BASETYPE
,
2006 "Shake128([perso = STR], [func = STR]): SHAKE128/cSHAKE128 XOF.",
2008 0, /* @tp_traverse@ */
2010 0, /* @tp_richcompare@ */
2011 0, /* @tp_weaklistoffset@ */
2013 0, /* @tp_iternext@ */
2014 0, /* @tp_methods@ */
2015 0, /* @tp_members@ */
2016 0, /* @tp_getset@ */
2019 0, /* @tp_descr_get@ */
2020 0, /* @tp_descr_set@ */
2021 0, /* @tp_dictoffset@ */
2023 PyType_GenericAlloc
, /* @tp_alloc@ */
2024 shake128_pynew
, /* @tp_new@ */
2029 static PyTypeObject shake256_pytype_skel
= {
2030 PyObject_HEAD_INIT(0) 0, /* Header */
2031 "Shake256", /* @tp_name@ */
2032 0, /* @tp_basicsize@ */
2033 0, /* @tp_itemsize@ */
2035 0, /* @tp_dealloc@ */
2037 0, /* @tp_getattr@ */
2038 0, /* @tp_setattr@ */
2039 0, /* @tp_compare@ */
2041 0, /* @tp_as_number@ */
2042 0, /* @tp_as_sequence@ */
2043 0, /* @tp_as_mapping@ */
2047 0, /* @tp_getattro@ */
2048 0, /* @tp_setattro@ */
2049 0, /* @tp_as_buffer@ */
2050 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2051 Py_TPFLAGS_BASETYPE
,
2054 "Shake256([perso = STR], [func = STR]): SHAKE256/cSHAKE256 XOF.",
2056 0, /* @tp_traverse@ */
2058 0, /* @tp_richcompare@ */
2059 0, /* @tp_weaklistoffset@ */
2061 0, /* @tp_iternext@ */
2062 0, /* @tp_methods@ */
2063 0, /* @tp_members@ */
2064 0, /* @tp_getset@ */
2067 0, /* @tp_descr_get@ */
2068 0, /* @tp_descr_set@ */
2069 0, /* @tp_dictoffset@ */
2071 PyType_GenericAlloc
, /* @tp_alloc@ */
2072 shake256_pynew
, /* @tp_new@ */
2077 /*----- Pseudorandom permutations -----------------------------------------*/
2079 static PyTypeObject
*gcprp_pytype
, *gprp_pytype
;
2081 typedef struct prpinfo
{
2086 void (*init
)(void *, const void *, size_t);
2087 void (*eblk
)(void *, const void *, void *);
2088 void (*dblk
)(void *, const void *, void *);
2091 #define PRP_DEF(PRE, pre) \
2092 static void pre##_prpinit(void *ctx, const void *k, size_t ksz) \
2093 { pre##_init(ctx, k, ksz); } \
2094 static void pre##_prpeblk(void *ctx, const void *in, void *out) \
2096 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
2097 pre##_eblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
2099 static void pre##_prpdblk(void *ctx, const void *in, void *out) \
2101 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
2102 pre##_dblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
2104 static const prpinfo pre##_prpinfo = { \
2105 #pre, pre##_keysz, sizeof(pre##_ctx), PRE##_BLKSZ, \
2106 pre##_prpinit, pre##_prpeblk, pre##_prpdblk \
2110 static const struct prpinfo
*const gprptab
[] = {
2111 #define PRP_ENTRY(PRE, pre) &pre##_prpinfo,
2116 typedef struct gcprp_pyobj
{
2117 PyHeapTypeObject ty
;
2120 #define GCPRP_PRP(o) (((gcprp_pyobj *)(o))->prp)
2122 typedef struct gprp_pyobj
{
2126 #define GPRP_PRP(o) (((gprp_pyobj *)(o))->prp)
2127 #define GPRP_CTX(o) (((gprp_pyobj *)(o)) + 1)
2129 typedef struct prp
{
2134 static PyObject
*gprp_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
2136 static const char *const kwlist
[] = { "key", 0 };
2139 const prpinfo
*prp
= GCPRP_PRP(ty
);
2142 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", KWLIST
, &k
, &sz
))
2144 if (keysz(sz
, prp
->keysz
) != sz
) VALERR("bad key length");
2145 me
= (PyObject
*)ty
->tp_alloc(ty
, 0);
2147 prp
->init(GPRP_CTX(me
), k
, sz
);
2154 static void gprp_pydealloc(PyObject
*me
)
2155 { Py_DECREF(me
->ob_type
); FREEOBJ(me
); }
2157 static PyObject
*gcprp_pywrap(const prpinfo
*prp
)
2159 gcprp_pyobj
*g
= newtype(gcprp_pytype
, 0, prp
->name
);
2161 g
->ty
.ht_type
.tp_basicsize
= sizeof(gprp_pyobj
) + prp
->ctxsz
;
2162 g
->ty
.ht_type
.tp_base
= gprp_pytype
;
2163 Py_INCREF(gprp_pytype
);
2164 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
2165 Py_TPFLAGS_BASETYPE
|
2166 Py_TPFLAGS_HEAPTYPE
);
2167 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
2168 g
->ty
.ht_type
.tp_free
= 0;
2169 g
->ty
.ht_type
.tp_new
= gprp_pynew
;
2170 typeready(&g
->ty
.ht_type
);
2171 return ((PyObject
*)g
);
2174 static PyObject
*gcpget_name(PyObject
*me
, void *hunoz
)
2175 { return (PyString_FromString(GCPRP_PRP(me
)->name
)); }
2176 static PyObject
*gcpget_keysz(PyObject
*me
, void *hunoz
)
2177 { return (keysz_pywrap(GCPRP_PRP(me
)->keysz
)); }
2178 static PyObject
*gcpget_blksz(PyObject
*me
, void *hunoz
)
2179 { return (PyInt_FromLong(GCPRP_PRP(me
)->blksz
)); }
2181 static PyObject
*gpmeth_encrypt(PyObject
*me
, PyObject
*arg
)
2187 if (!PyArg_ParseTuple(arg
, "s#:encrypt", &p
, &n
)) goto end
;
2188 if (n
!= GPRP_PRP(me
)->blksz
) VALERR("incorrect block length");
2189 rc
= bytestring_pywrap(0, n
);
2190 GPRP_PRP(me
)->eblk(GPRP_CTX(me
), p
, PyString_AS_STRING(rc
));
2195 static PyObject
*gpmeth_decrypt(PyObject
*me
, PyObject
*arg
)
2201 if (!PyArg_ParseTuple(arg
, "s#:decrypt", &p
, &n
)) goto end
;
2202 if (n
!= GPRP_PRP(me
)->blksz
) VALERR("incorrect block length");
2203 rc
= bytestring_pywrap(0, n
);
2204 GPRP_PRP(me
)->dblk(GPRP_CTX(me
), p
, PyString_AS_STRING(rc
));
2209 static PyGetSetDef gcprp_pygetset
[] = {
2210 #define GETSETNAME(op, name) gcp##op##_##name
2211 GET (keysz
, "CP.keysz -> acceptable key sizes")
2212 GET (blksz
, "CP.blksz -> block size")
2213 GET (name
, "CP.name -> name of this kind of PRP")
2218 static PyMethodDef gprp_pymethods
[] = {
2219 #define METHNAME(name) gpmeth_##name
2220 METH (encrypt
, "P.encrypt(PT) -> CT")
2221 METH (decrypt
, "P.decrypt(CT) -> PT")
2226 static PyTypeObject gcprp_pytype_skel
= {
2227 PyObject_HEAD_INIT(0) 0, /* Header */
2228 "GCPRP", /* @tp_name@ */
2229 sizeof(gcprp_pyobj
), /* @tp_basicsize@ */
2230 0, /* @tp_itemsize@ */
2232 0, /* @tp_dealloc@ */
2234 0, /* @tp_getattr@ */
2235 0, /* @tp_setattr@ */
2236 0, /* @tp_compare@ */
2238 0, /* @tp_as_number@ */
2239 0, /* @tp_as_sequence@ */
2240 0, /* @tp_as_mapping@ */
2244 0, /* @tp_getattro@ */
2245 0, /* @tp_setattro@ */
2246 0, /* @tp_as_buffer@ */
2247 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2248 Py_TPFLAGS_BASETYPE
,
2251 "Pseudorandom permutation metaclass.",
2253 0, /* @tp_traverse@ */
2255 0, /* @tp_richcompare@ */
2256 0, /* @tp_weaklistoffset@ */
2258 0, /* @tp_iternext@ */
2259 0, /* @tp_methods@ */
2260 0, /* @tp_members@ */
2261 gcprp_pygetset
, /* @tp_getset@ */
2264 0, /* @tp_descr_get@ */
2265 0, /* @tp_descr_set@ */
2266 0, /* @tp_dictoffset@ */
2268 PyType_GenericAlloc
, /* @tp_alloc@ */
2269 abstract_pynew
, /* @tp_new@ */
2274 static PyTypeObject gprp_pytype_skel
= {
2275 PyObject_HEAD_INIT(0) 0, /* Header */
2276 "GPRP", /* @tp_name@ */
2277 sizeof(gprp_pyobj
), /* @tp_basicsize@ */
2278 0, /* @tp_itemsize@ */
2280 gprp_pydealloc
, /* @tp_dealloc@ */
2282 0, /* @tp_getattr@ */
2283 0, /* @tp_setattr@ */
2284 0, /* @tp_compare@ */
2286 0, /* @tp_as_number@ */
2287 0, /* @tp_as_sequence@ */
2288 0, /* @tp_as_mapping@ */
2292 0, /* @tp_getattro@ */
2293 0, /* @tp_setattro@ */
2294 0, /* @tp_as_buffer@ */
2295 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2296 Py_TPFLAGS_BASETYPE
,
2299 "Pseudorandom permutation, abstract base class.",
2301 0, /* @tp_traverse@ */
2303 0, /* @tp_richcompare@ */
2304 0, /* @tp_weaklistoffset@ */
2306 0, /* @tp_iternext@ */
2307 gprp_pymethods
, /* @tp_methods@ */
2308 0, /* @tp_members@ */
2309 0, /* @tp_getset@ */
2312 0, /* @tp_descr_get@ */
2313 0, /* @tp_descr_set@ */
2314 0, /* @tp_dictoffset@ */
2316 PyType_GenericAlloc
, /* @tp_alloc@ */
2317 abstract_pynew
, /* @tp_new@ */
2322 /*----- Main code ---------------------------------------------------------*/
2324 static PyMethodDef methods
[] = {
2325 #define METHNAME(func) meth_##func
2326 METH (_KeySZ_fromdl
, "\
2327 fromdl(N) -> M: convert integer discrete log field size to work factor")
2328 METH (_KeySZ_fromschnorr
, "\
2329 fromschnorr(N) -> M: convert Schnorr group order to work factor")
2330 METH (_KeySZ_fromif
, "\
2331 fromif(N) -> M: convert integer factorization problem size to work factor")
2332 METH (_KeySZ_fromec
, "\
2333 fromec(N) -> M: convert elliptic curve group order to work factor")
2334 METH (_KeySZ_todl
, "\
2335 todl(N) -> M: convert work factor to integer discrete log field size")
2336 METH (_KeySZ_toschnorr
, "\
2337 toschnorr(N) -> M: convert work factor to Schnorr group order")
2338 METH (_KeySZ_toif
, "\
2339 toif(N) -> M: convert work factor to integer factorization problem size")
2340 METH (_KeySZ_toec
, "\
2341 toec(N) -> M: convert work factor to elliptic curve group order")
2342 METH (_KeySZ_toec
, "\
2343 toec(N) -> M: convert work factor to elliptic curve group order")
2344 #define METH_HDANCE(hdance, HDance) METH(hdance##_prf, "\
2345 " #hdance "_prf(K, N) -> H: calculate " HDance " hash of N with K")
2346 METH_HDANCE(hsalsa20
, "HSalsa20")
2347 METH_HDANCE(hsalsa2012
, "HSalsa20/12")
2348 METH_HDANCE(hsalsa208
, "HSalsa20/8")
2349 METH_HDANCE(hchacha20
, "HChaCha20")
2350 METH_HDANCE(hchacha12
, "HChaCha12")
2351 METH_HDANCE(hchacha8
, "HChaCha8")
2357 void algorithms_pyinit(void)
2359 INITTYPE(keysz
, root
);
2360 INITTYPE(keyszany
, keysz
);
2361 INITTYPE(keyszrange
, keysz
);
2362 INITTYPE(keyszset
, keysz
);
2363 INITTYPE(gccipher
, type
);
2364 INITTYPE(gcipher
, root
);
2365 INITTYPE(gchash
, type
);
2366 INITTYPE(ghash
, root
);
2367 INITTYPE(gcmac
, type
);
2368 INITTYPE(gmac
, type
);
2369 INITTYPE(gmhash
, ghash
);
2370 INITTYPE(poly1305cls
, type
);
2371 INITTYPE_META(poly1305key
, type
, poly1305cls
);
2372 INITTYPE(poly1305hash
, root
);
2373 INITTYPE(kxvik
, root
);
2374 INITTYPE(shake
, root
);
2375 INITTYPE(shake128
, shake
);
2376 INITTYPE(shake256
, shake
);
2377 INITTYPE(gcprp
, type
);
2378 INITTYPE(gprp
, root
);
2379 addmethods(methods
);
2382 GEN(gcciphers
, cipher
)
2385 #define gcprp prpinfo
2388 void algorithms_pyinsert(PyObject
*mod
)
2391 INSERT("KeySZ", keysz_pytype
);
2392 INSERT("KeySZAny", keyszany_pytype
);
2393 INSERT("KeySZRange", keyszrange_pytype
);
2394 INSERT("KeySZSet", keyszset_pytype
);
2395 INSERT("GCCipher", gccipher_pytype
);
2396 INSERT("GCipher", gcipher_pytype
);
2397 INSERT("gcciphers", gcciphers());
2398 INSERT("GCHash", gchash_pytype
);
2399 INSERT("GHash", ghash_pytype
);
2400 INSERT("gchashes", d
= gchashes());
2401 sha_pyobj
= PyDict_GetItemString(d
, "sha"); Py_INCREF(sha_pyobj
);
2402 has160_pyobj
= PyDict_GetItemString(d
, "has160"); Py_INCREF(has160_pyobj
);
2403 INSERT("GCMAC", gcmac_pytype
);
2404 INSERT("GMAC", gmac_pytype
);
2405 INSERT("GMACHash", gmhash_pytype
);
2406 INSERT("gcmacs", gcmacs());
2407 INSERT("Poly1305Class", poly1305cls_pytype
);
2408 INSERT("poly1305", poly1305key_pytype
);
2409 INSERT("Poly1305Hash", poly1305hash_pytype
);
2410 INSERT("Keccak1600", kxvik_pytype
);
2411 INSERT("Shake", shake_pytype
);
2412 INSERT("Shake128", shake128_pytype
);
2413 INSERT("Shake256", shake256_pytype
);
2414 INSERT("GCPRP", gcprp_pytype
);
2415 INSERT("GPRP", gprp_pytype
);
2416 INSERT("gcprps", gcprps());
2419 /*----- That's all, folks -------------------------------------------------*/