3 * Symmetric cryptography
5 * (c) 2004 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Python interface to Catacomb.
12 * Catacomb/Python is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * Catacomb/Python is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with Catacomb/Python; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
29 #include "catacomb-python.h"
30 #include "algorithms.h"
32 /*----- Key sizes ---------------------------------------------------------*/
34 PyTypeObject
*keysz_pytype
;
35 PyTypeObject
*keyszany_pytype
, *keyszrange_pytype
, *keyszset_pytype
;
36 PyObject
*sha_pyobj
, *has160_pyobj
;
39 # define KSZ_OPMASK 0x1f
43 # define KSZ_16BIT 0x20
46 PyObject
*keysz_pywrap(const octet
*k
)
49 #define ARG(i) (op&KSZ_16BIT ? LOAD16(k + 2*(i)) : k[i])
50 switch (op
&KSZ_OPMASK
) {
52 keysz_pyobj
*o
= PyObject_New(keysz_pyobj
, keyszany_pytype
);
54 return ((PyObject
*)o
);
58 PyObject_New(keyszrange_pyobj
, keyszrange_pytype
);
63 if (!o
->mod
) o
->mod
= 1;
64 return ((PyObject
*)o
);
68 PyObject_New(keyszset_pyobj
, keyszset_pytype
);
71 for (i
= 0; ARG(i
); i
++) ;
72 n
= i
; o
->set
= PyTuple_New(n
);
73 for (i
= 0; i
< n
; i
++)
74 PyTuple_SET_ITEM(o
->set
, i
, PyInt_FromLong(ARG(i
)));
75 return ((PyObject
*)o
);
83 static PyObject
*keyszany_pynew(PyTypeObject
*ty
,
84 PyObject
*arg
, PyObject
*kw
)
86 char *kwlist
[] = { "default", 0 };
90 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "i:new", kwlist
, &dfl
))
92 if (dfl
< 0) VALERR("key size cannot be negative");
93 o
= (keysz_pyobj
*)ty
->tp_alloc(ty
, 0);
95 return ((PyObject
*)o
);
100 static PyObject
*keyszrange_pynew(PyTypeObject
*ty
,
101 PyObject
*arg
, PyObject
*kw
)
103 char *kwlist
[] = { "default", "min", "max", "mod", 0 };
104 int dfl
, min
= 0, max
= 0, mod
= 1;
107 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "i|iii:new", kwlist
,
108 &dfl
, &min
, &max
, &mod
))
110 if (dfl
< 0 || min
< 0 || max
< 0)
111 VALERR("key size cannot be negative");
112 if (min
> dfl
|| (max
&& dfl
> max
))
113 VALERR("bad key size bounds");
114 if (mod
<= 0 || dfl
% mod
|| min
% mod
|| max
% mod
)
115 VALERR("bad key size modulus");
116 o
= (keyszrange_pyobj
*)ty
->tp_alloc(ty
, 0);
121 return ((PyObject
*)o
);
126 static PyObject
*keyszset_pynew(PyTypeObject
*ty
,
127 PyObject
*arg
, PyObject
*kw
)
129 char *kwlist
[] = { "default", "set", 0 };
132 PyObject
*x
= 0, *l
= 0;
133 keyszset_pyobj
*o
= 0;
135 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "i|O:new", kwlist
,
138 if (!set
) set
= PyTuple_New(0);
140 if (!PySequence_Check(set
)) TYERR("want a sequence");
141 n
= PySequence_Size(set
); if (n
< 0) goto end
;
142 l
= PyList_New(0); if (!l
) goto end
;
143 if (dfl
< 0) VALERR("key size cannot be negative");
144 x
= PyInt_FromLong(dfl
);
148 for (i
= 0; i
< n
; i
++) {
149 if ((x
= PySequence_GetItem(set
, i
)) == 0) goto end
;
150 xx
= PyInt_AsLong(x
);
151 if (PyErr_Occurred()) goto end
;
152 if (xx
== dfl
) continue;
153 if (xx
< 0) VALERR("key size cannot be negative");
159 if ((set
= PySequence_Tuple(l
)) == 0) goto end
;
160 o
= (keyszset_pyobj
*)ty
->tp_alloc(ty
, 0);
168 return ((PyObject
*)o
);
171 static PyObject
*kaget_min(PyObject
*me
, void *hunoz
)
172 { return (PyInt_FromLong(0)); }
173 #define kaget_max kaget_min
175 static PyObject
*ksget_min(PyObject
*me
, void *hunoz
)
177 PyObject
*set
= ((keyszset_pyobj
*)me
)->set
;
179 n
= PyTuple_Size(set
);
180 for (i
= 0; i
< n
; i
++) {
181 y
= PyInt_AsLong(PyTuple_GetItem(set
, i
));
182 if (x
== -1 || y
< x
) x
= y
;
184 return (PyInt_FromLong(x
));
187 static PyObject
*ksget_max(PyObject
*me
, void *hunoz
)
189 PyObject
*set
= ((keyszset_pyobj
*)me
)->set
;
191 n
= PyTuple_Size(set
);
192 for (i
= 0; i
< n
; i
++) {
193 y
= PyInt_AsLong(PyTuple_GetItem(set
, i
));
196 return (PyInt_FromLong(x
));
199 static PyMemberDef keysz_pymembers
[] = {
200 #define MEMBERSTRUCT keysz_pyobj
201 #define default dfl /* ugh! */
202 MEMBER(default, T_INT
, READONLY
, "KSZ.default -> default key size")
208 static PyGetSetDef keyszany_pygetset
[] = {
209 #define GETSETNAME(op, name) ka##op##_##name
210 GET (min
, "KSZ.min -> smallest allowed key size")
211 GET (max
, "KSZ.max -> largest allowed key size")
216 static PyMemberDef keyszrange_pymembers
[] = {
217 #define MEMBERSTRUCT keyszrange_pyobj
218 MEMBER(min
, T_INT
, READONLY
, "KSZ.min -> smallest allowed key size")
219 MEMBER(max
, T_INT
, READONLY
, "KSZ.max -> largest allowed key size")
220 MEMBER(mod
, T_INT
, READONLY
,
221 "KSZ.mod -> key size must be a multiple of this")
226 static PyGetSetDef keyszset_pygetset
[] = {
227 #define GETSETNAME(op, name) ks##op##_##name
228 GET (min
, "KSZ.min -> smallest allowed key size")
229 GET (max
, "KSZ.max -> largest allowed key size")
234 static PyMemberDef keyszset_pymembers
[] = {
235 #define MEMBERSTRUCT keyszset_pyobj
236 MEMBER(set
, T_OBJECT
, READONLY
, "KSZ.set -> allowed key sizes")
241 static PyTypeObject keysz_pytype_skel
= {
242 PyObject_HEAD_INIT(0) 0, /* Header */
243 "KeySZ", /* @tp_name@ */
244 sizeof(keysz_pyobj
), /* @tp_basicsize@ */
245 0, /* @tp_itemsize@ */
247 0, /* @tp_dealloc@ */
249 0, /* @tp_getattr@ */
250 0, /* @tp_setattr@ */
251 0, /* @tp_compare@ */
253 0, /* @tp_as_number@ */
254 0, /* @tp_as_sequence@ */
255 0, /* @tp_as_mapping@ */
259 0, /* @tp_getattro@ */
260 0, /* @tp_setattro@ */
261 0, /* @tp_as_buffer@ */
262 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
266 "Key size constraints.",
268 0, /* @tp_traverse@ */
270 0, /* @tp_richcompare@ */
271 0, /* @tp_weaklistoffset@ */
273 0, /* @tp_iternext@ */
274 0, /* @tp_methods@ */
275 keysz_pymembers
, /* @tp_members@ */
279 0, /* @tp_descr_get@ */
280 0, /* @tp_descr_set@ */
281 0, /* @tp_dictoffset@ */
283 PyType_GenericAlloc
, /* @tp_alloc@ */
284 abstract_pynew
, /* @tp_new@ */
289 static PyTypeObject keyszany_pytype_skel
= {
290 PyObject_HEAD_INIT(0) 0, /* Header */
291 "KeySZAny", /* @tp_name@ */
292 sizeof(keysz_pyobj
), /* @tp_basicsize@ */
293 0, /* @tp_itemsize@ */
295 0, /* @tp_dealloc@ */
297 0, /* @tp_getattr@ */
298 0, /* @tp_setattr@ */
299 0, /* @tp_compare@ */
301 0, /* @tp_as_number@ */
302 0, /* @tp_as_sequence@ */
303 0, /* @tp_as_mapping@ */
307 0, /* @tp_getattro@ */
308 0, /* @tp_setattro@ */
309 0, /* @tp_as_buffer@ */
310 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
314 "Key size constraints. This object imposes no constraints on size.",
316 0, /* @tp_traverse@ */
318 0, /* @tp_richcompare@ */
319 0, /* @tp_weaklistoffset@ */
321 0, /* @tp_iternext@ */
322 0, /* @tp_methods@ */
323 0, /* @tp_members@ */
324 keyszany_pygetset
, /* @tp_getset@ */
327 0, /* @tp_descr_get@ */
328 0, /* @tp_descr_set@ */
329 0, /* @tp_dictoffset@ */
331 PyType_GenericAlloc
, /* @tp_alloc@ */
332 keyszany_pynew
, /* @tp_new@ */
337 static PyTypeObject keyszrange_pytype_skel
= {
338 PyObject_HEAD_INIT(0) 0, /* Header */
339 "KeySZRange", /* @tp_name@ */
340 sizeof(keyszrange_pyobj
), /* @tp_basicsize@ */
341 0, /* @tp_itemsize@ */
343 0, /* @tp_dealloc@ */
345 0, /* @tp_getattr@ */
346 0, /* @tp_setattr@ */
347 0, /* @tp_compare@ */
349 0, /* @tp_as_number@ */
350 0, /* @tp_as_sequence@ */
351 0, /* @tp_as_mapping@ */
355 0, /* @tp_getattro@ */
356 0, /* @tp_setattro@ */
357 0, /* @tp_as_buffer@ */
358 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
362 "Key size constraints. This object asserts minimum and maximum (if\n\
363 sizes, and requires the key length to be a multiple of some value.",
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 "Key size constraints. This object requires the key to be one of a\n\
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
, unsigned f
)
463 if (!cobj
) cobj
= gccipher_pywrap((/*unconst*/ gccipher
*)GC_CLASS(c
));
464 else Py_INCREF(cobj
);
465 g
= PyObject_NEW(gcipher_pyobj
, (PyTypeObject
*)cobj
);
468 return ((PyObject
*)g
);
471 static PyObject
*gcipher_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
473 char *kwlist
[] = { "k", 0 };
477 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", kwlist
, &k
, &sz
))
479 if (keysz(sz
, GCCIPHER_CC(ty
)->keysz
) != sz
) VALERR("bad key length");
480 return (gcipher_pywrap((PyObject
*)ty
,
481 GC_INIT(GCCIPHER_CC(ty
), k
, sz
),
487 PyObject
*gccipher_pywrap(gccipher
*cc
)
489 gccipher_pyobj
*g
= newtype(gccipher_pytype
, 0, cc
->name
);
491 g
->ty
.ht_type
.tp_basicsize
= sizeof(gcipher_pyobj
);
492 g
->ty
.ht_type
.tp_base
= gcipher_pytype
;
493 Py_INCREF(gcipher_pytype
);
494 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
495 Py_TPFLAGS_BASETYPE
|
496 Py_TPFLAGS_HEAPTYPE
);
497 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
498 g
->ty
.ht_type
.tp_free
= 0;
499 g
->ty
.ht_type
.tp_new
= gcipher_pynew
;
500 typeready(&g
->ty
.ht_type
);
501 return ((PyObject
*)g
);
504 static void gcipher_pydealloc(PyObject
*me
)
506 if (GCIPHER_F(me
) & f_freeme
)
507 GC_DESTROY(GCIPHER_C(me
));
508 Py_DECREF(me
->ob_type
);
512 static PyObject
*gccget_name(PyObject
*me
, void *hunoz
)
513 { return (PyString_FromString(GCCIPHER_CC(me
)->name
)); }
515 static PyObject
*gccget_keysz(PyObject
*me
, void *hunoz
)
516 { return (keysz_pywrap(GCCIPHER_CC(me
)->keysz
)); }
518 static PyObject
*gccget_blksz(PyObject
*me
, void *hunoz
)
519 { return (PyInt_FromLong(GCCIPHER_CC(me
)->blksz
)); }
521 static PyObject
*gcmeth_encrypt(PyObject
*me
, PyObject
*arg
)
527 if (!PyArg_ParseTuple(arg
, "s#:encrypt", &p
, &sz
)) return (0);
528 rc
= bytestring_pywrap(0, sz
);
529 GC_ENCRYPT(GCIPHER_C(me
), p
, PyString_AS_STRING(rc
), sz
);
533 static PyObject
*gcmeth_enczero(PyObject
*me
, PyObject
*arg
)
539 if (!PyArg_ParseTuple(arg
, "i:enczero", &sz
)) return (0);
540 rc
= bytestring_pywrap(0, sz
);
541 p
= PyString_AS_STRING(rc
);
543 GC_ENCRYPT(GCIPHER_C(me
), p
, p
, sz
);
547 static PyObject
*gcmeth_decrypt(PyObject
*me
, PyObject
*arg
)
553 if (!PyArg_ParseTuple(arg
, "s#:decrypt", &p
, &sz
)) return (0);
554 rc
= bytestring_pywrap(0, sz
);
555 GC_DECRYPT(GCIPHER_C(me
), p
, PyString_AS_STRING(rc
), sz
);
559 static PyObject
*gcmeth_deczero(PyObject
*me
, PyObject
*arg
)
565 if (!PyArg_ParseTuple(arg
, "i:deczero", &sz
)) return (0);
566 rc
= bytestring_pywrap(0, sz
);
567 p
= PyString_AS_STRING(rc
);
569 GC_DECRYPT(GCIPHER_C(me
), p
, p
, sz
);
573 static PyObject
*gcmeth_setiv(PyObject
*me
, PyObject
*arg
)
578 if (!PyArg_ParseTuple(arg
, "s#:setiv", &p
, &sz
)) goto end
;
579 if (!GCIPHER_C(me
)->ops
->setiv
) VALERR("`setiv' not supported");
580 if (!GC_CLASS(GCIPHER_C(me
))->blksz
) VALERR("not a block cipher mode");
581 if (sz
!= GC_CLASS(GCIPHER_C(me
))->blksz
) VALERR("bad IV length");
582 GC_SETIV(GCIPHER_C(me
), p
);
588 static PyObject
*gcmeth_bdry(PyObject
*me
, PyObject
*arg
)
590 if (!PyArg_ParseTuple(arg
, ":bdry")) goto end
;
591 if (!GCIPHER_C(me
)->ops
->bdry
) VALERR("`bdry' not supported");
592 if (!GC_CLASS(GCIPHER_C(me
))->blksz
) VALERR("not a block cipher mode");
593 GC_BDRY(GCIPHER_C(me
));
599 static PyGetSetDef gccipher_pygetset
[] = {
600 #define GETSETNAME(op, name) gcc##op##_##name
601 GET (keysz
, "CC.keysz -> acceptable key sizes")
602 GET (blksz
, "CC.blksz -> block size, or zero")
603 GET (name
, "CC.name -> name of this kind of cipher")
608 static PyMethodDef gcipher_pymethods
[] = {
609 #define METHNAME(name) gcmeth_##name
610 METH (encrypt
, "C.encrypt(PT) -> CT")
611 METH (enczero
, "C.enczero(N) -> CT")
612 METH (decrypt
, "C.decrypt(CT) -> PT")
613 METH (deczero
, "C.deczero(N) -> PT")
614 METH (setiv
, "C.setiv(IV)")
615 METH (bdry
, "C.bdry()")
620 static PyTypeObject gccipher_pytype_skel
= {
621 PyObject_HEAD_INIT(0) 0, /* Header */
622 "GCCipher", /* @tp_name@ */
623 sizeof(gccipher_pyobj
), /* @tp_basicsize@ */
624 0, /* @tp_itemsize@ */
626 0, /* @tp_dealloc@ */
628 0, /* @tp_getattr@ */
629 0, /* @tp_setattr@ */
630 0, /* @tp_compare@ */
632 0, /* @tp_as_number@ */
633 0, /* @tp_as_sequence@ */
634 0, /* @tp_as_mapping@ */
638 0, /* @tp_getattro@ */
639 0, /* @tp_setattro@ */
640 0, /* @tp_as_buffer@ */
641 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
645 "Symmetric cipher metaclass.",
647 0, /* @tp_traverse@ */
649 0, /* @tp_richcompare@ */
650 0, /* @tp_weaklistoffset@ */
652 0, /* @tp_iternext@ */
653 0, /* @tp_methods@ */
654 0, /* @tp_members@ */
655 gccipher_pygetset
, /* @tp_getset@ */
658 0, /* @tp_descr_get@ */
659 0, /* @tp_descr_set@ */
660 0, /* @tp_dictoffset@ */
662 PyType_GenericAlloc
, /* @tp_alloc@ */
663 abstract_pynew
, /* @tp_new@ */
668 static PyTypeObject gcipher_pytype_skel
= {
669 PyObject_HEAD_INIT(0) 0, /* Header */
670 "GCipher", /* @tp_name@ */
671 sizeof(gcipher_pyobj
), /* @tp_basicsize@ */
672 0, /* @tp_itemsize@ */
674 gcipher_pydealloc
, /* @tp_dealloc@ */
676 0, /* @tp_getattr@ */
677 0, /* @tp_setattr@ */
678 0, /* @tp_compare@ */
680 0, /* @tp_as_number@ */
681 0, /* @tp_as_sequence@ */
682 0, /* @tp_as_mapping@ */
686 0, /* @tp_getattro@ */
687 0, /* @tp_setattro@ */
688 0, /* @tp_as_buffer@ */
689 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
693 "Symmetric cipher, abstract base class.",
695 0, /* @tp_traverse@ */
697 0, /* @tp_richcompare@ */
698 0, /* @tp_weaklistoffset@ */
700 0, /* @tp_iternext@ */
701 gcipher_pymethods
, /* @tp_methods@ */
702 0, /* @tp_members@ */
706 0, /* @tp_descr_get@ */
707 0, /* @tp_descr_set@ */
708 0, /* @tp_dictoffset@ */
710 PyType_GenericAlloc
, /* @tp_alloc@ */
711 abstract_pynew
, /* @tp_new@ */
716 /*----- Hash functions ----------------------------------------------------*/
718 PyTypeObject
*gchash_pytype
, *ghash_pytype
;
720 CONVFUNC(gchash
, gchash
*, GCHASH_CH
)
721 CONVFUNC(ghash
, ghash
*, GHASH_H
)
723 static PyObject
*ghash_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
725 char *kwlist
[] = { 0 };
726 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, ":new", kwlist
))
728 return (ghash_pywrap((PyObject
*)ty
, GH_INIT(GCHASH_CH(ty
)), f_freeme
));
733 PyObject
*gchash_pywrap(gchash
*ch
)
735 gchash_pyobj
*g
= newtype(gchash_pytype
, 0, ch
->name
);
737 g
->ty
.ht_type
.tp_basicsize
= sizeof(ghash_pyobj
);
738 g
->ty
.ht_type
.tp_base
= ghash_pytype
;
739 Py_INCREF(ghash_pytype
);
740 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
741 Py_TPFLAGS_BASETYPE
|
742 Py_TPFLAGS_HEAPTYPE
);
743 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
744 g
->ty
.ht_type
.tp_free
= 0;
745 g
->ty
.ht_type
.tp_new
= ghash_pynew
;
746 typeready(&g
->ty
.ht_type
);
747 return ((PyObject
*)g
);
750 PyObject
*ghash_pywrap(PyObject
*cobj
, ghash
*h
, unsigned f
)
753 if (!cobj
) cobj
= gchash_pywrap((/*unconst*/ gchash
*)GH_CLASS(h
));
754 else Py_INCREF(cobj
);
755 g
= PyObject_NEW(ghash_pyobj
, (PyTypeObject
*)cobj
);
758 return ((PyObject
*)g
);
761 static void ghash_pydealloc(PyObject
*me
)
763 if (GHASH_F(me
) & f_freeme
)
764 GH_DESTROY(GHASH_H(me
));
765 Py_DECREF(me
->ob_type
);
769 static PyObject
*gchget_name(PyObject
*me
, void *hunoz
)
770 { return (PyString_FromString(GCHASH_CH(me
)->name
)); }
772 static PyObject
*gchget_hashsz(PyObject
*me
, void *hunoz
)
773 { return (PyInt_FromLong(GCHASH_CH(me
)->hashsz
)); }
775 static PyObject
*gchget_bufsz(PyObject
*me
, void *hunoz
)
776 { return (PyInt_FromLong(GCHASH_CH(me
)->bufsz
)); }
778 static PyObject
*ghmeth_hash(PyObject
*me
, PyObject
*arg
)
782 if (!PyArg_ParseTuple(arg
, "s#:hash", &p
, &sz
)) return (0);
783 GH_HASH(GHASH_H(me
), p
, sz
);
787 #define GHMETH_HASHU_(n, W, w) \
788 static PyObject *ghmeth_hashu##w(PyObject *me, PyObject *arg) \
791 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \
792 GH_HASHU##W(GHASH_H(me), x); \
797 DOUINTCONV(GHMETH_HASHU_
)
799 #define GHMETH_HASHBUF_(n, W, w) \
800 static PyObject *ghmeth_hashbuf##w(PyObject *me, PyObject *arg) \
804 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
805 if (sz > MASK##n) TYERR("string too long"); \
806 GH_HASHBUF##W(GHASH_H(me), p, sz); \
811 DOUINTCONV(GHMETH_HASHBUF_
)
813 static PyObject
*ghmeth_hashstrz(PyObject
*me
, PyObject
*arg
)
816 if (!PyArg_ParseTuple(arg
, "s:hashstrz", &p
)) return (0);
817 GH_HASHSTRZ(GHASH_H(me
), p
);
821 static PyObject
*ghmeth_done(PyObject
*me
, PyObject
*arg
)
825 if (!PyArg_ParseTuple(arg
, ":done")) return (0);
826 g
= GH_COPY(GHASH_H(me
));
827 rc
= bytestring_pywrap(0, g
->ops
->c
->hashsz
);
828 GH_DONE(g
, PyString_AS_STRING(rc
));
833 static PyGetSetDef gchash_pygetset
[] = {
834 #define GETSETNAME(op, name) gch##op##_##name
835 GET (bufsz
, "CH.bufsz -> hash buffer size, or zero")
836 GET (hashsz
, "CH.hashsz -> hash output size")
837 GET (name
, "CH.name -> name of this kind of hash")
842 static PyMethodDef ghash_pymethods
[] = {
843 #define METHNAME(name) ghmeth_##name
844 METH (hash
, "H.hash(M)")
845 #define METHU_(n, W, w) METH(hashu##w, "H.hashu" #w "(WORD)")
848 #define METHBUF_(n, W, w) METH(hashbuf##w, "H.hashbuf" #w "(BYTES)")
851 METH (hashstrz
, "H.hashstrz(STRING)")
852 METH (done
, "H.done() -> HASH")
857 static PyTypeObject gchash_pytype_skel
= {
858 PyObject_HEAD_INIT(0) 0, /* Header */
859 "GCHash", /* @tp_name@ */
860 sizeof(gchash_pyobj
), /* @tp_basicsize@ */
861 0, /* @tp_itemsize@ */
863 0, /* @tp_dealloc@ */
865 0, /* @tp_getattr@ */
866 0, /* @tp_setattr@ */
867 0, /* @tp_compare@ */
869 0, /* @tp_as_number@ */
870 0, /* @tp_as_sequence@ */
871 0, /* @tp_as_mapping@ */
875 0, /* @tp_getattro@ */
876 0, /* @tp_setattro@ */
877 0, /* @tp_as_buffer@ */
878 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
882 "Hash function metaclass.",
884 0, /* @tp_traverse@ */
886 0, /* @tp_richcompare@ */
887 0, /* @tp_weaklistoffset@ */
889 0, /* @tp_iternext@ */
890 0, /* @tp_methods@ */
891 0, /* @tp_members@ */
892 gchash_pygetset
, /* @tp_getset@ */
895 0, /* @tp_descr_get@ */
896 0, /* @tp_descr_set@ */
897 0, /* @tp_dictoffset@ */
899 PyType_GenericAlloc
, /* @tp_alloc@ */
900 abstract_pynew
, /* @tp_new@ */
905 static PyTypeObject ghash_pytype_skel
= {
906 PyObject_HEAD_INIT(0) 0, /* Header */
907 "GHash", /* @tp_name@ */
908 sizeof(ghash_pyobj
), /* @tp_basicsize@ */
909 0, /* @tp_itemsize@ */
911 ghash_pydealloc
, /* @tp_dealloc@ */
913 0, /* @tp_getattr@ */
914 0, /* @tp_setattr@ */
915 0, /* @tp_compare@ */
917 0, /* @tp_as_number@ */
918 0, /* @tp_as_sequence@ */
919 0, /* @tp_as_mapping@ */
923 0, /* @tp_getattro@ */
924 0, /* @tp_setattro@ */
925 0, /* @tp_as_buffer@ */
926 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
930 "Hash function, abstract base class.",
932 0, /* @tp_traverse@ */
934 0, /* @tp_richcompare@ */
935 0, /* @tp_weaklistoffset@ */
937 0, /* @tp_iternext@ */
938 ghash_pymethods
, /* @tp_methods@ */
939 0, /* @tp_members@ */
943 0, /* @tp_descr_get@ */
944 0, /* @tp_descr_set@ */
945 0, /* @tp_dictoffset@ */
947 PyType_GenericAlloc
, /* @tp_alloc@ */
948 abstract_pynew
, /* @tp_new@ */
953 /*----- Message authentication --------------------------------------------*/
955 PyTypeObject
*gcmac_pytype
, *gmac_pytype
, *gmhash_pytype
;
957 CONVFUNC(gcmac
, gcmac
*, GCMAC_CM
)
958 CONVFUNC(gmac
, gmac
*, GMAC_M
)
959 CONVFUNC(gmhash
, ghash
*, GHASH_H
)
961 static PyObject
*gmac_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
963 char *kwlist
[] = { "k", 0 };
967 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", kwlist
, &k
, &sz
))
969 if (keysz(sz
, GCMAC_CM(ty
)->keysz
) != sz
) VALERR("bad key length");
970 return (gmac_pywrap((PyObject
*)ty
,
971 GM_KEY(GCMAC_CM(ty
), k
, sz
),
977 static PyObject
*gmhash_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
979 char *kwlist
[] = { 0 };
982 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, ":new", kwlist
)) return (0);
983 g
= PyObject_NEW(ghash_pyobj
, ty
);
984 g
->h
= GM_INIT(GMAC_M(ty
));
987 return ((PyObject
*)g
);
990 PyObject
*gcmac_pywrap(gcmac
*cm
)
992 gcmac_pyobj
*g
= newtype(gcmac_pytype
, 0, cm
->name
);
994 g
->ty
.ht_type
.tp_basicsize
= sizeof(gmac_pyobj
);
995 g
->ty
.ht_type
.tp_base
= gmac_pytype
;
996 Py_INCREF(gmac_pytype
);
997 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
998 Py_TPFLAGS_BASETYPE
|
999 Py_TPFLAGS_HEAPTYPE
);
1000 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
1001 g
->ty
.ht_type
.tp_free
= 0;
1002 g
->ty
.ht_type
.tp_new
= gmac_pynew
;
1003 typeready(&g
->ty
.ht_type
);
1004 return ((PyObject
*)g
);
1007 PyObject
*gmac_pywrap(PyObject
*cobj
, gmac
*m
, unsigned f
)
1010 if (!cobj
) cobj
= gcmac_pywrap((/*unconst*/ gcmac
*)GM_CLASS(m
));
1011 else Py_INCREF(cobj
);
1012 g
= newtype((PyTypeObject
*)cobj
, 0, 0);
1013 g
->ty
.ht_type
.tp_basicsize
= sizeof(ghash_pyobj
);
1014 g
->ty
.ht_name
= PyString_FromFormat("%s(keyed)", m
->ops
->c
->name
);
1015 g
->ty
.ht_type
.tp_name
= PyString_AS_STRING(g
->ty
.ht_name
);
1016 g
->ty
.ht_type
.tp_base
= gmhash_pytype
;
1017 Py_INCREF(gmac_pytype
);
1018 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
1019 Py_TPFLAGS_BASETYPE
|
1020 Py_TPFLAGS_HEAPTYPE
);
1021 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
1022 g
->ty
.ht_type
.tp_free
= 0;
1023 g
->ty
.ht_type
.tp_new
= gmhash_pynew
;
1024 typeready(&g
->ty
.ht_type
);
1027 return ((PyObject
*)g
);
1030 static void gmac_pydealloc(PyObject
*me
)
1032 if (GMAC_F(me
) & f_freeme
)
1033 GM_DESTROY(GMAC_M(me
));
1034 Py_DECREF(me
->ob_type
);
1035 PyType_Type
.tp_dealloc(me
);
1038 static PyObject
*gcmget_name(PyObject
*me
, void *hunoz
)
1039 { return (PyString_FromString(GCMAC_CM(me
)->name
)); }
1041 static PyObject
*gcmget_keysz(PyObject
*me
, void *hunoz
)
1042 { return (keysz_pywrap(GCMAC_CM(me
)->keysz
)); }
1044 static PyObject
*gcmget_tagsz(PyObject
*me
, void *hunoz
)
1045 { return (PyInt_FromLong(GCMAC_CM(me
)->hashsz
)); }
1047 static PyGetSetDef gcmac_pygetset
[] = {
1048 #define GETSETNAME(op, name) gcm##op##_##name
1049 GET (keysz
, "CM.keysz -> acceptable key sizes")
1050 GET (tagsz
, "CM.tagsz -> MAC output size")
1051 GET (name
, "CM.name -> name of this kind of MAC")
1056 static PyTypeObject gcmac_pytype_skel
= {
1057 PyObject_HEAD_INIT(0) 0, /* Header */
1058 "GCMAC", /* @tp_name@ */
1059 sizeof(gchash_pyobj
), /* @tp_basicsize@ */
1060 0, /* @tp_itemsize@ */
1062 0, /* @tp_dealloc@ */
1064 0, /* @tp_getattr@ */
1065 0, /* @tp_setattr@ */
1066 0, /* @tp_compare@ */
1068 0, /* @tp_as_number@ */
1069 0, /* @tp_as_sequence@ */
1070 0, /* @tp_as_mapping@ */
1074 0, /* @tp_getattro@ */
1075 0, /* @tp_setattro@ */
1076 0, /* @tp_as_buffer@ */
1077 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1078 Py_TPFLAGS_BASETYPE
,
1081 "Message authentication code metametaclass.",
1083 0, /* @tp_traverse@ */
1085 0, /* @tp_richcompare@ */
1086 0, /* @tp_weaklistoffset@ */
1088 0, /* @tp_iternext@ */
1089 0, /* @tp_methods@ */
1090 0, /* @tp_members@ */
1091 gcmac_pygetset
, /* @tp_getset@ */
1094 0, /* @tp_descr_get@ */
1095 0, /* @tp_descr_set@ */
1096 0, /* @tp_dictoffset@ */
1098 PyType_GenericAlloc
, /* @tp_alloc@ */
1099 abstract_pynew
, /* @tp_new@ */
1104 static PyTypeObject gmac_pytype_skel
= {
1105 PyObject_HEAD_INIT(0) 0, /* Header */
1106 "GMAC", /* @tp_name@ */
1107 sizeof(gmac_pyobj
), /* @tp_basicsize@ */
1108 0, /* @tp_itemsize@ */
1110 gmac_pydealloc
, /* @tp_dealloc@ */
1112 0, /* @tp_getattr@ */
1113 0, /* @tp_setattr@ */
1114 0, /* @tp_compare@ */
1116 0, /* @tp_as_number@ */
1117 0, /* @tp_as_sequence@ */
1118 0, /* @tp_as_mapping@ */
1122 0, /* @tp_getattro@ */
1123 0, /* @tp_setattro@ */
1124 0, /* @tp_as_buffer@ */
1125 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1126 Py_TPFLAGS_BASETYPE
,
1129 "Message authentication code metaclass, abstract base class.",
1131 0, /* @tp_traverse@ */
1133 0, /* @tp_richcompare@ */
1134 0, /* @tp_weaklistoffset@ */
1136 0, /* @tp_iternext@ */
1137 0, /* @tp_methods@ */
1138 0, /* @tp_members@ */
1139 0, /* @tp_getset@ */
1142 0, /* @tp_descr_get@ */
1143 0, /* @tp_descr_set@ */
1144 0, /* @tp_dictoffset@ */
1146 PyType_GenericAlloc
, /* @tp_alloc@ */
1147 abstract_pynew
, /* @tp_new@ */
1152 static PyTypeObject gmhash_pytype_skel
= {
1153 PyObject_HEAD_INIT(0) 0, /* Header */
1154 "GMACHash", /* @tp_name@ */
1155 sizeof(ghash_pyobj
), /* @tp_basicsize@ */
1156 0, /* @tp_itemsize@ */
1158 ghash_pydealloc
, /* @tp_dealloc@ */
1160 0, /* @tp_getattr@ */
1161 0, /* @tp_setattr@ */
1162 0, /* @tp_compare@ */
1164 0, /* @tp_as_number@ */
1165 0, /* @tp_as_sequence@ */
1166 0, /* @tp_as_mapping@ */
1170 0, /* @tp_getattro@ */
1171 0, /* @tp_setattro@ */
1172 0, /* @tp_as_buffer@ */
1173 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1174 Py_TPFLAGS_BASETYPE
,
1177 "Message authentication code, abstract base class.",
1179 0, /* @tp_traverse@ */
1181 0, /* @tp_richcompare@ */
1182 0, /* @tp_weaklistoffset@ */
1184 0, /* @tp_iternext@ */
1185 0, /* @tp_methods@ */
1186 0, /* @tp_members@ */
1187 0, /* @tp_getset@ */
1190 0, /* @tp_descr_get@ */
1191 0, /* @tp_descr_set@ */
1192 0, /* @tp_dictoffset@ */
1194 PyType_GenericAlloc
, /* @tp_alloc@ */
1195 abstract_pynew
, /* @tp_new@ */
1200 /*----- Special snowflake for Poly1305 ------------------------------------*/
1202 PyTypeObject
*poly1305cls_pytype
, *poly1305key_pytype
, *poly1305hash_pytype
;
1204 typedef struct poly1305key_pyobj
{
1205 PyHeapTypeObject ty
;
1207 } poly1305key_pyobj
;
1209 typedef struct poly1305hash_pyobj
{
1214 } poly1305hash_pyobj
;
1216 #define P1305_F(o) (((poly1305hash_pyobj *)(o))->f)
1217 #define P1305_CTX(o) (&((poly1305hash_pyobj *)(o))->ctx)
1218 CONVFUNC(poly1305hash
, poly1305_ctx
*, P1305_CTX
)
1220 static PyObject
*poly1305hash_pynew(PyTypeObject
*ty
,
1221 PyObject
*arg
, PyObject
*kw
)
1223 char *kwlist
[] = { "mask", 0 };
1224 poly1305key_pyobj
*pk
= (poly1305key_pyobj
*)ty
;
1225 poly1305hash_pyobj
*ph
;
1229 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|s#:new", kwlist
, &m
, &sz
))
1231 if (m
&& sz
!= POLY1305_MASKSZ
) VALERR("bad mask length");
1232 ph
= PyObject_NEW(poly1305hash_pyobj
, ty
);
1234 if (m
) ph
->f
|= f_mask
;
1235 poly1305_macinit(&ph
->ctx
, &pk
->k
, m
);
1237 return ((PyObject
*)ph
);
1242 static PyObject
*poly1305key_pynew(PyTypeObject
*ty
,
1243 PyObject
*arg
, PyObject
*kw
)
1245 char *kwlist
[] = { "k", 0 };
1246 poly1305key_pyobj
*pk
;
1250 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", kwlist
, &k
, &sz
))
1252 if (keysz(sz
, poly1305_keysz
) != sz
) VALERR("bad key length");
1254 pk
= newtype(ty
, 0, 0);
1255 pk
->ty
.ht_name
= PyString_FromString("poly1305(keyed)");
1256 pk
->ty
.ht_type
.tp_basicsize
= sizeof(poly1305hash_pyobj
);
1257 pk
->ty
.ht_type
.tp_name
= PyString_AS_STRING(pk
->ty
.ht_name
);
1258 pk
->ty
.ht_type
.tp_base
= poly1305hash_pytype
;
1259 Py_INCREF(poly1305key_pytype
);
1260 pk
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
1261 Py_TPFLAGS_BASETYPE
|
1262 Py_TPFLAGS_HEAPTYPE
);
1263 pk
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
1264 pk
->ty
.ht_type
.tp_free
= 0;
1265 pk
->ty
.ht_type
.tp_new
= poly1305hash_pynew
;
1266 typeready(&pk
->ty
.ht_type
);
1268 poly1305_keyinit(&pk
->k
, k
, sz
);
1269 return ((PyObject
*)pk
);
1275 static PyObject
*poly1305clsget_name(PyObject
*me
, void *hunoz
)
1276 { return (PyString_FromString("poly1305")); }
1278 static PyObject
*poly1305clsget_keysz(PyObject
*me
, void *hunoz
)
1279 { return (keysz_pywrap(poly1305_keysz
)); }
1281 static PyObject
*poly1305clsget_masksz(PyObject
*me
, void *hunoz
)
1282 { return (PyInt_FromLong(POLY1305_MASKSZ
)); }
1284 static PyObject
*poly1305clsget_tagsz(PyObject
*me
, void *hunoz
)
1285 { return (PyInt_FromLong(POLY1305_TAGSZ
)); }
1287 static PyObject
*polymeth_copy(PyObject
*me
, PyObject
*arg
)
1289 poly1305hash_pyobj
*ph
;
1290 if (!PyArg_ParseTuple(arg
, ":copy")) return (0);
1291 ph
= PyObject_NEW(poly1305hash_pyobj
, me
->ob_type
);
1292 poly1305_copy(&ph
->ctx
, P1305_CTX(me
));
1293 Py_INCREF(me
->ob_type
);
1294 return ((PyObject
*)ph
);
1297 static PyObject
*polymeth_hash(PyObject
*me
, PyObject
*arg
)
1301 if (!PyArg_ParseTuple(arg
, "s#:hash", &p
, &sz
)) return (0);
1302 poly1305_hash(P1305_CTX(me
), p
, sz
);
1306 #define POLYMETH_HASHU_(n, W, w) \
1307 static PyObject *polymeth_hashu##w(PyObject *me, PyObject *arg) \
1311 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \
1312 STORE##W(b, x); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \
1317 DOUINTCONV(POLYMETH_HASHU_
)
1319 #define POLYMETH_HASHBUF_(n, W, w) \
1320 static PyObject *polymeth_hashbuf##w(PyObject *me, PyObject *arg) \
1325 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
1326 if (sz > MASK##n) TYERR("string too long"); \
1327 STORE##W(b, sz); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \
1328 poly1305_hash(P1305_CTX(me), p, sz); \
1333 DOUINTCONV(POLYMETH_HASHBUF_
)
1335 static PyObject
*polymeth_hashstrz(PyObject
*me
, PyObject
*arg
)
1338 if (!PyArg_ParseTuple(arg
, "s:hashstrz", &p
)) return (0);
1339 poly1305_hash(P1305_CTX(me
), p
, strlen(p
) + 1);
1343 static PyObject
*polymeth_flush(PyObject
*me
, PyObject
*arg
)
1345 if (!PyArg_ParseTuple(arg
, ":flush")) return (0);
1346 poly1305_flush(P1305_CTX(me
));
1350 static PyObject
*polymeth_flushzero(PyObject
*me
, PyObject
*arg
)
1352 if (!PyArg_ParseTuple(arg
, ":flushzero")) return (0);
1353 poly1305_flushzero(P1305_CTX(me
));
1357 static PyObject
*polymeth_concat(PyObject
*me
, PyObject
*arg
)
1359 PyObject
*pre
, *suff
;
1360 if (!PyArg_ParseTuple(arg
, "OO:concat", &pre
, &suff
)) return (0);
1361 if (!PyObject_TypeCheck(pre
, poly1305hash_pytype
) ||
1362 !PyObject_TypeCheck(suff
, poly1305hash_pytype
))
1363 TYERR("wanted a poly1305hash");
1364 if (me
->ob_type
!= pre
->ob_type
|| me
->ob_type
!= suff
->ob_type
)
1365 TYERR("key mismatch");
1366 if (P1305_CTX(pre
)->nbuf
) VALERR("prefix is not block-aligned");
1367 poly1305_concat(P1305_CTX(me
), P1305_CTX(pre
), P1305_CTX(suff
));
1373 static PyObject
*polymeth_done(PyObject
*me
, PyObject
*arg
)
1376 if (!PyArg_ParseTuple(arg
, ":done")) return (0);
1377 if (!(P1305_F(me
) & f_mask
)) VALERR("no mask");
1378 rc
= bytestring_pywrap(0, POLY1305_TAGSZ
);
1379 poly1305_done(P1305_CTX(me
), PyString_AS_STRING(rc
));
1385 static PyGetSetDef poly1305cls_pygetset
[] = {
1386 #define GETSETNAME(op, name) poly1305cls##op##_##name
1387 GET (keysz
, "PC.keysz -> acceptable key sizes")
1388 GET (masksz
, "PC.masksz -> mask size")
1389 GET (tagsz
, "PC.tagsz -> MAC output size")
1390 GET (name
, "PC.name -> name of this kind of MAC")
1395 static PyMethodDef poly1305hash_pymethods
[] = {
1396 #define METHNAME(name) polymeth_##name
1397 METH (copy
, "P.copy() -> PP")
1398 METH (hash
, "P.hash(M)")
1399 #define METHU_(n, W, w) METH(hashu##w, "P.hashu" #w "(WORD)")
1402 #define METHBUF_(n, W, w) METH(hashbuf##w, "P.hashbuf" #w "(BYTES)")
1403 DOUINTCONV(METHBUF_
)
1405 METH (hashstrz
, "P.hashstrz(STRING)")
1406 METH (flush
, "P.flush()")
1407 METH (flushzero
, "P.flushzero()")
1408 METH (concat
, "P.concat(PREFIX, SUFFIX)")
1409 METH (done
, "P.done() -> TAG")
1414 static PyTypeObject poly1305cls_pytype_skel
= {
1415 PyObject_HEAD_INIT(0) 0, /* Header */
1416 "Poly1305Class", /* @tp_name@ */
1417 sizeof(PyHeapTypeObject
), /* @tp_basicsize@ */
1418 0, /* @tp_itemsize@ */
1420 0, /* @tp_dealloc@ */
1422 0, /* @tp_getattr@ */
1423 0, /* @tp_setattr@ */
1424 0, /* @tp_compare@ */
1426 0, /* @tp_as_number@ */
1427 0, /* @tp_as_sequence@ */
1428 0, /* @tp_as_mapping@ */
1432 0, /* @tp_getattro@ */
1433 0, /* @tp_setattro@ */
1434 0, /* @tp_as_buffer@ */
1435 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1436 Py_TPFLAGS_BASETYPE
,
1439 "Poly1305 metametaclass. Best not to ask.",
1441 0, /* @tp_traverse@ */
1443 0, /* @tp_richcompare@ */
1444 0, /* @tp_weaklistoffset@ */
1446 0, /* @tp_iternext@ */
1447 0, /* @tp_methods@ */
1448 0, /* @tp_members@ */
1449 poly1305cls_pygetset
, /* @tp_getset@ */
1452 0, /* @tp_descr_get@ */
1453 0, /* @tp_descr_set@ */
1454 0, /* @tp_dictoffset@ */
1456 PyType_GenericAlloc
, /* @tp_alloc@ */
1457 abstract_pynew
, /* @tp_new@ */
1462 static PyTypeObject poly1305key_pytype_skel
= {
1463 PyObject_HEAD_INIT(0) 0, /* Header */
1464 "poly1305", /* @tp_name@ */
1465 sizeof(poly1305key_pyobj
), /* @tp_basicsize@ */
1466 0, /* @tp_itemsize@ */
1468 0, /* @tp_dealloc@ */
1470 0, /* @tp_getattr@ */
1471 0, /* @tp_setattr@ */
1472 0, /* @tp_compare@ */
1474 0, /* @tp_as_number@ */
1475 0, /* @tp_as_sequence@ */
1476 0, /* @tp_as_mapping@ */
1480 0, /* @tp_getattro@ */
1481 0, /* @tp_setattro@ */
1482 0, /* @tp_as_buffer@ */
1483 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1484 Py_TPFLAGS_BASETYPE
,
1489 0, /* @tp_traverse@ */
1491 0, /* @tp_richcompare@ */
1492 0, /* @tp_weaklistoffset@ */
1494 0, /* @tp_iternext@ */
1495 0, /* @tp_methods@ */
1496 0, /* @tp_members@ */
1497 0, /* @tp_getset@ */
1500 0, /* @tp_descr_get@ */
1501 0, /* @tp_descr_set@ */
1502 0, /* @tp_dictoffset@ */
1504 PyType_GenericAlloc
, /* @tp_alloc@ */
1505 poly1305key_pynew
, /* @tp_new@ */
1510 static PyTypeObject poly1305hash_pytype_skel
= {
1511 PyObject_HEAD_INIT(0) 0, /* Header */
1512 "Poly1305Hash", /* @tp_name@ */
1513 sizeof(poly1305hash_pyobj
), /* @tp_basicsize@ */
1514 0, /* @tp_itemsize@ */
1516 0, /* @tp_dealloc@ */
1518 0, /* @tp_getattr@ */
1519 0, /* @tp_setattr@ */
1520 0, /* @tp_compare@ */
1522 0, /* @tp_as_number@ */
1523 0, /* @tp_as_sequence@ */
1524 0, /* @tp_as_mapping@ */
1528 0, /* @tp_getattro@ */
1529 0, /* @tp_setattro@ */
1530 0, /* @tp_as_buffer@ */
1531 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1532 Py_TPFLAGS_BASETYPE
,
1535 "Poly1305 MAC context base class.",
1537 0, /* @tp_traverse@ */
1539 0, /* @tp_richcompare@ */
1540 0, /* @tp_weaklistoffset@ */
1542 0, /* @tp_iternext@ */
1543 poly1305hash_pymethods
, /* @tp_methods@ */
1544 0, /* @tp_members@ */
1545 0, /* @tp_getset@ */
1548 0, /* @tp_descr_get@ */
1549 0, /* @tp_descr_set@ */
1550 0, /* @tp_dictoffset@ */
1552 PyType_GenericAlloc
, /* @tp_alloc@ */
1553 abstract_pynew
, /* @tp_new@ */
1558 /*----- Special snowflake for HSalsa and HChaCha --------------------------*/
1560 #define DEF_HDANCE(DANCE, HDANCE, dance, hdance) \
1561 static PyObject *meth_##hdance##_prf(PyObject *me, PyObject *arg) \
1563 dance##_ctx dance; \
1565 Py_ssize_t ksz, nsz; \
1567 if (!PyArg_ParseTuple(arg, "s#s#:" #hdance "_prf", \
1568 &k, &ksz, &n, &nsz)) \
1570 if (ksz != keysz(ksz, dance##_keysz)) VALERR("bad key length"); \
1571 if (nsz != HDANCE##_INSZ) VALERR("bad input length"); \
1572 rc = bytestring_pywrap(0, HSALSA20_OUTSZ); \
1573 dance##_init(&dance, k, ksz, 0); \
1574 hdance##_prf(&dance, n, PyString_AS_STRING(rc)); \
1580 DEF_HDANCE(SALSA20
, HSALSA20
, salsa20
, hsalsa20
)
1581 DEF_HDANCE(SALSA20
, HSALSA20
, salsa20
, hsalsa2012
)
1582 DEF_HDANCE(SALSA20
, HSALSA20
, salsa20
, hsalsa208
)
1584 DEF_HDANCE(CHACHA
, HCHACHA
, chacha
, hchacha20
)
1585 DEF_HDANCE(CHACHA
, HCHACHA
, chacha
, hchacha12
)
1586 DEF_HDANCE(CHACHA
, HCHACHA
, chacha
, hchacha8
)
1588 /*----- Keccak-p[1600, n] -------------------------------------------------*/
1590 static PyTypeObject
*kxvik_pytype
;
1592 typedef struct kxvik_pyobj
{
1598 static PyObject
*kxvik_pynew(PyTypeObject
*ty
,
1599 PyObject
*arg
, PyObject
*kw
)
1602 kxvik_pyobj
*rc
= 0;
1603 char *kwlist
[] = { "nround", 0 };
1604 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:new", kwlist
,
1607 rc
= (kxvik_pyobj
*)ty
->tp_alloc(ty
, 0);
1609 keccak1600_init(&rc
->s
);
1611 return ((PyObject
*)rc
);
1614 static PyObject
*kxvikmeth_mix(PyObject
*me
, PyObject
*arg
)
1616 kxvik_pyobj
*k
= (kxvik_pyobj
*)me
;
1621 char *p
; Py_ssize_t n
;
1623 if (!PyArg_ParseTuple(arg
, "s#:mix", &p
, &n
)) goto end
;
1624 if (n
> 200) VALERR("out of range");
1625 q
= (const octet
*)p
;
1627 while (n
> 8) { LOAD64_L_(t
[i
], q
); i
++; q
+= 8; n
-= 8; }
1629 memcpy(buf
, q
, n
); memset(buf
+ n
, 0, 8 - n
);
1630 LOAD64_L_(t
[i
], buf
); i
++;
1632 keccak1600_mix(&k
->s
, t
, i
);
1638 static PyObject
*kxvikmeth_extract(PyObject
*me
, PyObject
*arg
)
1640 kxvik_pyobj
*k
= (kxvik_pyobj
*)me
;
1647 if (!PyArg_ParseTuple(arg
, "O&:extract", convuint
, &n
)) goto end
;
1648 if (n
> 200) VALERR("out of range");
1649 rc
= bytestring_pywrap(0, n
);
1650 q
= (octet
*)PyString_AS_STRING(rc
);
1651 keccak1600_extract(&k
->s
, t
, (n
+ 7)/8);
1653 while (n
> 8) { STORE64_L_(q
, t
[i
]); i
++; q
+= 8; n
-= 8; }
1654 if (n
) { STORE64_L_(buf
, t
[i
]); memcpy(q
, buf
, n
); }
1659 static PyObject
*kxvikmeth_step(PyObject
*me
, PyObject
*arg
)
1661 kxvik_pyobj
*k
= (kxvik_pyobj
*)me
;
1662 if (!PyArg_ParseTuple(arg
, ":step")) return (0);
1663 keccak1600_p(&k
->s
, &k
->s
, k
->n
);
1667 static PyObject
*kxvikget_nround(PyObject
*me
, void *hunoz
)
1669 kxvik_pyobj
*k
= (kxvik_pyobj
*)me
;
1670 return (PyInt_FromLong(k
->n
));
1673 static int kxvikset_nround(PyObject
*me
, PyObject
*val
, void *hunoz
)
1675 kxvik_pyobj
*k
= (kxvik_pyobj
*)me
;
1679 if (!val
) NIERR("__del__");
1680 if (!convuint(val
, &n
)) goto end
;
1687 static PyGetSetDef kxvik_pygetset
[] = {
1688 #define GETSETNAME(op, name) kxvik##op##_##name
1689 GETSET(nround
, "KECCAK.nround -> number of rounds")
1694 static PyMethodDef kxvik_pymethods
[] = {
1695 #define METHNAME(func) kxvikmeth_##func
1696 METH (mix
, "KECCAK.mix(DATA)")
1697 METH (extract
, "KECCAK.extract(NOCTETS)")
1698 METH (step
, "KECCAK.step()")
1703 static PyTypeObject kxvik_pytype_skel
= {
1704 PyObject_HEAD_INIT(0) 0, /* Header */
1705 "Keccak1600", /* @tp_name@ */
1706 sizeof(kxvik_pyobj
), /* @tp_basicsize@ */
1707 0, /* @tp_itemsize@ */
1709 0, /* @tp_dealloc@ */
1711 0, /* @tp_getattr@ */
1712 0, /* @tp_setattr@ */
1713 0, /* @tp_compare@ */
1715 0, /* @tp_as_number@ */
1716 0, /* @tp_as_sequence@ */
1717 0, /* @tp_as_mapping@ */
1721 0, /* @tp_getattro@ */
1722 0, /* @tp_setattro@ */
1723 0, /* @tp_as_buffer@ */
1724 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1725 Py_TPFLAGS_BASETYPE
,
1728 "Keccak-p[1600, n] state.",
1730 0, /* @tp_traverse@ */
1732 0, /* @tp_richcompare@ */
1733 0, /* @tp_weaklistoffset@ */
1735 0, /* @tp_iternext@ */
1736 kxvik_pymethods
, /* @tp_methods@ */
1737 0, /* @tp_members@ */
1738 kxvik_pygetset
, /* @tp_getset@ */
1741 0, /* @tp_descr_get@ */
1742 0, /* @tp_descr_set@ */
1743 0, /* @tp_dictoffset@ */
1745 PyType_GenericAlloc
, /* @tp_alloc@ */
1746 kxvik_pynew
, /* @tp_new@ */
1751 static PyTypeObject
*shake_pytype
, *shake128_pytype
, *shake256_pytype
;
1753 typedef struct shake_pyobj
{
1759 #define SHAKE_H(o) (&((shake_pyobj *)(o))->h)
1760 #define SHAKE_ST(o) (((shake_pyobj *)(o))->st)
1762 static PyObject
*shake_dopynew(void (*initfn
)(shake_ctx
*,
1763 const void *, size_t,
1764 const void *, size_t),
1766 PyObject
*arg
, PyObject
*kw
)
1768 shake_pyobj
*rc
= 0;
1769 char *p
= 0, *f
= 0;
1770 Py_ssize_t psz
= 0, fsz
= 0;
1771 char *kwlist
[] = { "perso", "func", 0 };
1773 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|s#s#:new", kwlist
,
1774 &p
, &psz
, &f
, &fsz
))
1776 rc
= (shake_pyobj
*)ty
->tp_alloc(ty
, 0);
1777 initfn(&rc
->h
, f
, fsz
, p
, psz
);
1780 return ((PyObject
*)rc
);
1783 static PyObject
*shake128_pynew(PyTypeObject
*ty
,
1784 PyObject
*arg
, PyObject
*kw
)
1785 { return (shake_dopynew(cshake128_init
, ty
, arg
, kw
)); }
1787 static PyObject
*shake256_pynew(PyTypeObject
*ty
,
1788 PyObject
*arg
, PyObject
*kw
)
1789 { return (shake_dopynew(cshake256_init
, ty
, arg
, kw
)); }
1791 static int shake_check(PyObject
*me
, int st
)
1793 if (SHAKE_ST(me
) != st
) VALERR("wrong state");
1799 static PyObject
*shakemeth_hash(PyObject
*me
, PyObject
*arg
)
1803 if (!PyArg_ParseTuple(arg
, "s#:hash", &p
, &sz
)) return (0);
1804 if (shake_check(me
, 0)) return (0);
1805 shake_hash(SHAKE_H(me
), p
, sz
);
1809 #define SHAKEMETH_HASHU_(n, W, w) \
1810 static PyObject *shakemeth_hashu##w(PyObject *me, PyObject *arg) \
1814 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \
1815 if (shake_check(me, 0)) goto end; \
1816 STORE##W(b, x); shake_hash(SHAKE_H(me), b, sizeof(b)); \
1821 DOUINTCONV(SHAKEMETH_HASHU_
)
1823 #define SHAKEMETH_HASHBUF_(n, W, w) \
1824 static PyObject *shakemeth_hashbuf##w(PyObject *me, PyObject *arg) \
1829 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
1830 if (sz > MASK##n) TYERR("string too long"); \
1831 if (shake_check(me, 0)) goto end; \
1832 STORE##W(b, sz); shake_hash(SHAKE_H(me), b, sizeof(b)); \
1833 shake_hash(SHAKE_H(me), p, sz); \
1838 DOUINTCONV(SHAKEMETH_HASHBUF_
)
1840 static PyObject
*shakemeth_hashstrz(PyObject
*me
, PyObject
*arg
)
1843 if (!PyArg_ParseTuple(arg
, "s:hashstrz", &p
)) return (0);
1844 if (shake_check(me
, 0)) return (0);
1845 shake_hash(SHAKE_H(me
), p
, strlen(p
) + 1);
1849 static PyObject
*shakemeth_xof(PyObject
*me
, PyObject
*arg
)
1851 if (!PyArg_ParseTuple(arg
, ":xof")) goto end
;
1852 if (shake_check(me
, 0)) goto end
;
1853 shake_xof(SHAKE_H(me
));
1860 static PyObject
*shakemeth_done(PyObject
*me
, PyObject
*arg
)
1864 if (!PyArg_ParseTuple(arg
, "O&:done", convszt
, &n
)) goto end
;
1865 if (shake_check(me
, 0)) goto end
;
1866 rc
= bytestring_pywrap(0, n
);
1867 shake_done(SHAKE_H(me
), PyString_AS_STRING(rc
), n
);
1873 static PyObject
*shakemeth_copy(PyObject
*me
, PyObject
*arg
)
1875 shake_pyobj
*rc
= 0;
1877 if (!PyArg_ParseTuple(arg
, ":copy")) goto end
;
1878 rc
= PyObject_NEW(shake_pyobj
, me
->ob_type
);
1879 rc
->h
= *SHAKE_H(me
);
1880 rc
->st
= SHAKE_ST(me
);
1882 return ((PyObject
*)rc
);
1885 static PyObject
*shakemeth_get(PyObject
*me
, PyObject
*arg
)
1890 if (!PyArg_ParseTuple(arg
, "O&:get", convszt
, &sz
)) goto end
;
1891 if (shake_check(me
, 1)) goto end
;
1892 rc
= bytestring_pywrap(0, sz
);
1893 shake_get(SHAKE_H(me
), PyString_AS_STRING(rc
), sz
);
1898 static PyObject
*shakemeth_mask(PyObject
*me
, PyObject
*arg
)
1901 char *p
; Py_ssize_t sz
;
1903 if (!PyArg_ParseTuple(arg
, "s#:mask", &p
, &sz
)) goto end
;
1904 if (shake_check(me
, 1)) goto end
;
1905 rc
= bytestring_pywrap(0, sz
);
1906 shake_mask(SHAKE_H(me
), p
, PyString_AS_STRING(rc
), sz
);
1911 static PyObject
*shakeget_rate(PyObject
*me
, void *hunoz
)
1912 { return (PyInt_FromLong(SHAKE_H(me
)->h
.r
)); }
1914 static PyObject
*shakeget_buffered(PyObject
*me
, void *hunoz
)
1915 { return (PyInt_FromLong(SHAKE_H(me
)->h
.n
)); }
1917 static PyObject
*shakeget_state(PyObject
*me
, void *hunoz
)
1919 int st
= SHAKE_ST(me
);
1920 return (PyString_FromString(st
== 0 ?
"absorb" :
1921 st
== 1 ?
"squeeze" : "dead"));
1924 static PyGetSetDef shake_pygetset
[] = {
1925 #define GETSETNAME(op, name) shake##op##_##name
1926 GET (rate
, "S.rate -> rate, in bytes")
1927 GET (buffered
, "S.buffered -> amount currently buffered")
1928 GET (state
, "S.state -> `absorb', `squeeze', `dead'")
1933 static PyMethodDef shake_pymethods
[] = {
1934 #define METHNAME(func) shakemeth_##func
1935 METH (copy
, "S.copy() -> SS")
1936 METH (hash
, "S.hash(M)")
1937 #define METHU_(n, W, w) METH(hashu##w, "S.hashu" #w "(WORD)")
1940 #define METHBUF_(n, W, w) METH(hashbuf##w, "S.hashbuf" #w "(BYTES)")
1941 DOUINTCONV(METHBUF_
)
1943 METH (hashstrz
, "S.hashstrz(STRING)")
1944 METH (xof
, "S.xof()")
1945 METH (done
, "S.done(LEN) ->H")
1946 METH (get
, "S.get(LEN) -> H")
1947 METH (mask
, "S.mask(M) -> C")
1952 static PyTypeObject shake_pytype_skel
= {
1953 PyObject_HEAD_INIT(0) 0, /* Header */
1954 "Shake", /* @tp_name@ */
1955 sizeof(shake_pyobj
), /* @tp_basicsize@ */
1956 0, /* @tp_itemsize@ */
1958 0, /* @tp_dealloc@ */
1960 0, /* @tp_getattr@ */
1961 0, /* @tp_setattr@ */
1962 0, /* @tp_compare@ */
1964 0, /* @tp_as_number@ */
1965 0, /* @tp_as_sequence@ */
1966 0, /* @tp_as_mapping@ */
1970 0, /* @tp_getattro@ */
1971 0, /* @tp_setattro@ */
1972 0, /* @tp_as_buffer@ */
1973 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1974 Py_TPFLAGS_BASETYPE
,
1977 "SHAKE/cSHAKE base class.",
1979 0, /* @tp_traverse@ */
1981 0, /* @tp_richcompare@ */
1982 0, /* @tp_weaklistoffset@ */
1984 0, /* @tp_iternext@ */
1985 shake_pymethods
, /* @tp_methods@ */
1986 0, /* @tp_members@ */
1987 shake_pygetset
, /* @tp_getset@ */
1990 0, /* @tp_descr_get@ */
1991 0, /* @tp_descr_set@ */
1992 0, /* @tp_dictoffset@ */
1994 PyType_GenericAlloc
, /* @tp_alloc@ */
1995 abstract_pynew
, /* @tp_new@ */
2000 static PyTypeObject shake128_pytype_skel
= {
2001 PyObject_HEAD_INIT(0) 0, /* Header */
2002 "Shake128", /* @tp_name@ */
2003 0, /* @tp_basicsize@ */
2004 0, /* @tp_itemsize@ */
2006 0, /* @tp_dealloc@ */
2008 0, /* @tp_getattr@ */
2009 0, /* @tp_setattr@ */
2010 0, /* @tp_compare@ */
2012 0, /* @tp_as_number@ */
2013 0, /* @tp_as_sequence@ */
2014 0, /* @tp_as_mapping@ */
2018 0, /* @tp_getattro@ */
2019 0, /* @tp_setattro@ */
2020 0, /* @tp_as_buffer@ */
2021 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2022 Py_TPFLAGS_BASETYPE
,
2025 "SHAKE128/cSHAKE128 XOF.",
2027 0, /* @tp_traverse@ */
2029 0, /* @tp_richcompare@ */
2030 0, /* @tp_weaklistoffset@ */
2032 0, /* @tp_iternext@ */
2033 0, /* @tp_methods@ */
2034 0, /* @tp_members@ */
2035 0, /* @tp_getset@ */
2038 0, /* @tp_descr_get@ */
2039 0, /* @tp_descr_set@ */
2040 0, /* @tp_dictoffset@ */
2042 PyType_GenericAlloc
, /* @tp_alloc@ */
2043 shake128_pynew
, /* @tp_new@ */
2048 static PyTypeObject shake256_pytype_skel
= {
2049 PyObject_HEAD_INIT(0) 0, /* Header */
2050 "Shake256", /* @tp_name@ */
2051 0, /* @tp_basicsize@ */
2052 0, /* @tp_itemsize@ */
2054 0, /* @tp_dealloc@ */
2056 0, /* @tp_getattr@ */
2057 0, /* @tp_setattr@ */
2058 0, /* @tp_compare@ */
2060 0, /* @tp_as_number@ */
2061 0, /* @tp_as_sequence@ */
2062 0, /* @tp_as_mapping@ */
2066 0, /* @tp_getattro@ */
2067 0, /* @tp_setattro@ */
2068 0, /* @tp_as_buffer@ */
2069 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2070 Py_TPFLAGS_BASETYPE
,
2073 "SHAKE256/cSHAKE256 XOF.",
2075 0, /* @tp_traverse@ */
2077 0, /* @tp_richcompare@ */
2078 0, /* @tp_weaklistoffset@ */
2080 0, /* @tp_iternext@ */
2081 0, /* @tp_methods@ */
2082 0, /* @tp_members@ */
2083 0, /* @tp_getset@ */
2086 0, /* @tp_descr_get@ */
2087 0, /* @tp_descr_set@ */
2088 0, /* @tp_dictoffset@ */
2090 PyType_GenericAlloc
, /* @tp_alloc@ */
2091 shake256_pynew
, /* @tp_new@ */
2096 /*----- Pseudorandom permutations -----------------------------------------*/
2098 static PyTypeObject
*gcprp_pytype
, *gprp_pytype
;
2100 typedef struct prpinfo
{
2105 void (*init
)(void *, const void *, size_t);
2106 void (*eblk
)(void *, const void *, void *);
2107 void (*dblk
)(void *, const void *, void *);
2110 #define PRP_DEF(PRE, pre) \
2111 static void pre##_prpinit(void *ctx, const void *k, size_t ksz) \
2112 { pre##_init(ctx, k, ksz); } \
2113 static void pre##_prpeblk(void *ctx, const void *in, void *out) \
2115 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
2116 pre##_eblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
2118 static void pre##_prpdblk(void *ctx, const void *in, void *out) \
2120 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
2121 pre##_dblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
2123 static const prpinfo pre##_prpinfo = { \
2124 #pre, pre##_keysz, sizeof(pre##_ctx), PRE##_BLKSZ, \
2125 pre##_prpinit, pre##_prpeblk, pre##_prpdblk \
2129 static const struct prpinfo
*const gprptab
[] = {
2130 #define PRP_ENTRY(PRE, pre) &pre##_prpinfo,
2135 typedef struct gcprp_pyobj
{
2136 PyHeapTypeObject ty
;
2139 #define GCPRP_PRP(o) (((gcprp_pyobj *)(o))->prp)
2141 typedef struct gprp_pyobj
{
2145 #define GPRP_PRP(o) (((gprp_pyobj *)(o))->prp)
2146 #define GPRP_CTX(o) (((gprp_pyobj *)(o)) + 1)
2148 typedef struct prp
{
2153 static PyObject
*gprp_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
2155 char *kwlist
[] = { "key", 0 };
2158 const prpinfo
*prp
= GCPRP_PRP(ty
);
2161 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", kwlist
, &k
, &sz
))
2163 if (keysz(sz
, prp
->keysz
) != sz
) VALERR("bad key length");
2164 me
= (PyObject
*)ty
->tp_alloc(ty
, 0);
2166 prp
->init(GPRP_CTX(me
), k
, sz
);
2173 static void gprp_pydealloc(PyObject
*me
)
2174 { Py_DECREF(me
->ob_type
); FREEOBJ(me
); }
2176 static PyObject
*gcprp_pywrap(const prpinfo
*prp
)
2178 gcprp_pyobj
*g
= newtype(gcprp_pytype
, 0, prp
->name
);
2180 g
->ty
.ht_type
.tp_basicsize
= sizeof(gprp_pyobj
) + prp
->ctxsz
;
2181 g
->ty
.ht_type
.tp_base
= gprp_pytype
;
2182 Py_INCREF(gprp_pytype
);
2183 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
2184 Py_TPFLAGS_BASETYPE
|
2185 Py_TPFLAGS_HEAPTYPE
);
2186 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
2187 g
->ty
.ht_type
.tp_free
= 0;
2188 g
->ty
.ht_type
.tp_new
= gprp_pynew
;
2189 typeready(&g
->ty
.ht_type
);
2190 return ((PyObject
*)g
);
2193 static PyObject
*gcpget_name(PyObject
*me
, void *hunoz
)
2194 { return (PyString_FromString(GCPRP_PRP(me
)->name
)); }
2195 static PyObject
*gcpget_keysz(PyObject
*me
, void *hunoz
)
2196 { return (keysz_pywrap(GCPRP_PRP(me
)->keysz
)); }
2197 static PyObject
*gcpget_blksz(PyObject
*me
, void *hunoz
)
2198 { return (PyInt_FromLong(GCPRP_PRP(me
)->blksz
)); }
2200 static PyObject
*gpmeth_encrypt(PyObject
*me
, PyObject
*arg
)
2206 if (!PyArg_ParseTuple(arg
, "s#:encrypt", &p
, &n
)) goto end
;
2207 if (n
!= GPRP_PRP(me
)->blksz
) VALERR("incorrect block length");
2208 rc
= bytestring_pywrap(0, n
);
2209 GPRP_PRP(me
)->eblk(GPRP_CTX(me
), p
, PyString_AS_STRING(rc
));
2214 static PyObject
*gpmeth_decrypt(PyObject
*me
, PyObject
*arg
)
2220 if (!PyArg_ParseTuple(arg
, "s#:decrypt", &p
, &n
)) goto end
;
2221 if (n
!= GPRP_PRP(me
)->blksz
) VALERR("incorrect block length");
2222 rc
= bytestring_pywrap(0, n
);
2223 GPRP_PRP(me
)->dblk(GPRP_CTX(me
), p
, PyString_AS_STRING(rc
));
2228 static PyGetSetDef gcprp_pygetset
[] = {
2229 #define GETSETNAME(op, name) gcp##op##_##name
2230 GET (keysz
, "CP.keysz -> acceptable key sizes")
2231 GET (blksz
, "CP.blksz -> block size")
2232 GET (name
, "CP.name -> name of this kind of PRP")
2237 static PyMethodDef gprp_pymethods
[] = {
2238 #define METHNAME(name) gpmeth_##name
2239 METH (encrypt
, "P.encrypt(PT) -> CT")
2240 METH (decrypt
, "P.decrypt(CT) -> PT")
2245 static PyTypeObject gcprp_pytype_skel
= {
2246 PyObject_HEAD_INIT(0) 0, /* Header */
2247 "GCPRP", /* @tp_name@ */
2248 sizeof(gcprp_pyobj
), /* @tp_basicsize@ */
2249 0, /* @tp_itemsize@ */
2251 0, /* @tp_dealloc@ */
2253 0, /* @tp_getattr@ */
2254 0, /* @tp_setattr@ */
2255 0, /* @tp_compare@ */
2257 0, /* @tp_as_number@ */
2258 0, /* @tp_as_sequence@ */
2259 0, /* @tp_as_mapping@ */
2263 0, /* @tp_getattro@ */
2264 0, /* @tp_setattro@ */
2265 0, /* @tp_as_buffer@ */
2266 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2267 Py_TPFLAGS_BASETYPE
,
2270 "Pseudorandom permutation metaclass.",
2272 0, /* @tp_traverse@ */
2274 0, /* @tp_richcompare@ */
2275 0, /* @tp_weaklistoffset@ */
2277 0, /* @tp_iternext@ */
2278 0, /* @tp_methods@ */
2279 0, /* @tp_members@ */
2280 gcprp_pygetset
, /* @tp_getset@ */
2283 0, /* @tp_descr_get@ */
2284 0, /* @tp_descr_set@ */
2285 0, /* @tp_dictoffset@ */
2287 PyType_GenericAlloc
, /* @tp_alloc@ */
2288 abstract_pynew
, /* @tp_new@ */
2293 static PyTypeObject gprp_pytype_skel
= {
2294 PyObject_HEAD_INIT(0) 0, /* Header */
2295 "GPRP", /* @tp_name@ */
2296 sizeof(gprp_pyobj
), /* @tp_basicsize@ */
2297 0, /* @tp_itemsize@ */
2299 gprp_pydealloc
, /* @tp_dealloc@ */
2301 0, /* @tp_getattr@ */
2302 0, /* @tp_setattr@ */
2303 0, /* @tp_compare@ */
2305 0, /* @tp_as_number@ */
2306 0, /* @tp_as_sequence@ */
2307 0, /* @tp_as_mapping@ */
2311 0, /* @tp_getattro@ */
2312 0, /* @tp_setattro@ */
2313 0, /* @tp_as_buffer@ */
2314 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2315 Py_TPFLAGS_BASETYPE
,
2318 "Pseudorandom permutation, abstract base class.",
2320 0, /* @tp_traverse@ */
2322 0, /* @tp_richcompare@ */
2323 0, /* @tp_weaklistoffset@ */
2325 0, /* @tp_iternext@ */
2326 gprp_pymethods
, /* @tp_methods@ */
2327 0, /* @tp_members@ */
2328 0, /* @tp_getset@ */
2331 0, /* @tp_descr_get@ */
2332 0, /* @tp_descr_set@ */
2333 0, /* @tp_dictoffset@ */
2335 PyType_GenericAlloc
, /* @tp_alloc@ */
2336 abstract_pynew
, /* @tp_new@ */
2341 /*----- Main code ---------------------------------------------------------*/
2343 static PyMethodDef methods
[] = {
2344 #define METHNAME(func) meth_##func
2345 METH (_KeySZ_fromdl
, "\
2346 fromdl(N) -> M: convert integer discrete log field size to work factor")
2347 METH (_KeySZ_fromschnorr
, "\
2348 fromschnorr(N) -> M: convert Schnorr group order to work factor")
2349 METH (_KeySZ_fromif
, "\
2350 fromif(N) -> M: convert integer factorization problem size to work factor")
2351 METH (_KeySZ_fromec
, "\
2352 fromec(N) -> M: convert elliptic curve group order to work factor")
2353 METH (_KeySZ_todl
, "\
2354 todl(N) -> M: convert work factor to integer discrete log field size")
2355 METH (_KeySZ_toschnorr
, "\
2356 toschnorr(N) -> M: convert work factor to Schnorr group order")
2357 METH (_KeySZ_toif
, "\
2358 toif(N) -> M: convert work factor to integer factorization problem size")
2359 METH (_KeySZ_toec
, "\
2360 toec(N) -> M: convert work factor to elliptic curve group order")
2361 METH (_KeySZ_toec
, "\
2362 toec(N) -> M: convert work factor to elliptic curve group order")
2363 #define METH_HDANCE(hdance, HDance) METH(hdance##_prf, "\
2364 " #hdance "_prf(K, N) -> H: calculate " HDance " hash of N with K")
2365 METH_HDANCE(hsalsa20
, "HSalsa20")
2366 METH_HDANCE(hsalsa2012
, "HSalsa20/12")
2367 METH_HDANCE(hsalsa208
, "HSalsa20/8")
2368 METH_HDANCE(hchacha20
, "HChaCha20")
2369 METH_HDANCE(hchacha12
, "HChaCha12")
2370 METH_HDANCE(hchacha8
, "HChaCha8")
2376 void algorithms_pyinit(void)
2378 INITTYPE(keysz
, root
);
2379 INITTYPE(keyszany
, keysz
);
2380 INITTYPE(keyszrange
, keysz
);
2381 INITTYPE(keyszset
, keysz
);
2382 INITTYPE(gccipher
, type
);
2383 INITTYPE(gcipher
, root
);
2384 INITTYPE(gchash
, type
);
2385 INITTYPE(ghash
, root
);
2386 INITTYPE(gcmac
, type
);
2387 INITTYPE(gmac
, type
);
2388 INITTYPE(gmhash
, ghash
);
2389 INITTYPE(poly1305cls
, type
);
2390 INITTYPE_META(poly1305key
, type
, poly1305cls
);
2391 INITTYPE(poly1305hash
, root
);
2392 INITTYPE(kxvik
, root
);
2393 INITTYPE(shake
, root
);
2394 INITTYPE(shake128
, shake
);
2395 INITTYPE(shake256
, shake
);
2396 INITTYPE(gcprp
, type
);
2397 INITTYPE(gprp
, root
);
2398 addmethods(methods
);
2401 GEN(gcciphers
, cipher
)
2404 #define gcprp prpinfo
2407 void algorithms_pyinsert(PyObject
*mod
)
2410 INSERT("KeySZ", keysz_pytype
);
2411 INSERT("KeySZAny", keyszany_pytype
);
2412 INSERT("KeySZRange", keyszrange_pytype
);
2413 INSERT("KeySZSet", keyszset_pytype
);
2414 INSERT("GCCipher", gccipher_pytype
);
2415 INSERT("GCipher", gcipher_pytype
);
2416 INSERT("gcciphers", gcciphers());
2417 INSERT("GCHash", gchash_pytype
);
2418 INSERT("GHash", ghash_pytype
);
2419 INSERT("gchashes", d
= gchashes());
2420 sha_pyobj
= PyDict_GetItemString(d
, "sha"); Py_INCREF(sha_pyobj
);
2421 has160_pyobj
= PyDict_GetItemString(d
, "has160"); Py_INCREF(has160_pyobj
);
2422 INSERT("GCMAC", gcmac_pytype
);
2423 INSERT("GMAC", gmac_pytype
);
2424 INSERT("GMACHash", gmhash_pytype
);
2425 INSERT("gcmacs", gcmacs());
2426 INSERT("Poly1305Class", poly1305cls_pytype
);
2427 INSERT("poly1305", poly1305key_pytype
);
2428 INSERT("Poly1305Hash", poly1305hash_pytype
);
2429 INSERT("Keccak1600", kxvik_pytype
);
2430 INSERT("Shake", shake_pytype
);
2431 INSERT("Shake128", shake128_pytype
);
2432 INSERT("Shake256", shake256_pytype
);
2433 INSERT("GCPRP", gcprp_pytype
);
2434 INSERT("GPRP", gprp_pytype
);
2435 INSERT("gcprps", gcprps());
2438 /*----- That's all, folks -------------------------------------------------*/