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
);
143 if (PyErr_Occurred()) goto end
;
144 if (dfl
< 0) VALERR("key size cannot be negative");
145 x
= PyInt_FromLong(dfl
);
149 for (i
= 0; i
< n
; i
++) {
150 if ((x
= PySequence_GetItem(set
, i
)) == 0) goto end
;
151 xx
= PyInt_AsLong(x
);
152 if (PyErr_Occurred()) goto end
;
153 if (xx
== dfl
) continue;
154 if (xx
< 0) VALERR("key size cannot be negative");
160 if ((set
= PySequence_Tuple(l
)) == 0) goto end
;
161 o
= (keyszset_pyobj
*)ty
->tp_alloc(ty
, 0);
169 return ((PyObject
*)o
);
172 static PyObject
*kaget_min(PyObject
*me
, void *hunoz
)
173 { return (PyInt_FromLong(0)); }
174 #define kaget_max kaget_min
176 static PyObject
*ksget_min(PyObject
*me
, void *hunoz
)
178 PyObject
*set
= ((keyszset_pyobj
*)me
)->set
;
180 n
= PyTuple_Size(set
);
181 for (i
= 0; i
< n
; i
++) {
182 y
= PyInt_AsLong(PyTuple_GetItem(set
, i
));
183 if (x
== -1 || y
< x
) x
= y
;
185 return (PyInt_FromLong(x
));
188 static PyObject
*ksget_max(PyObject
*me
, void *hunoz
)
190 PyObject
*set
= ((keyszset_pyobj
*)me
)->set
;
192 n
= PyTuple_Size(set
);
193 for (i
= 0; i
< n
; i
++) {
194 y
= PyInt_AsLong(PyTuple_GetItem(set
, i
));
197 return (PyInt_FromLong(x
));
200 static PyMemberDef keysz_pymembers
[] = {
201 #define MEMBERSTRUCT keysz_pyobj
202 #define default dfl /* ugh! */
203 MEMBER(default, T_INT
, READONLY
, "KSZ.default -> default key size")
209 static PyGetSetDef keyszany_pygetset
[] = {
210 #define GETSETNAME(op, name) ka##op##_##name
211 GET (min
, "KSZ.min -> smallest allowed key size")
212 GET (max
, "KSZ.min -> largest allowed key size")
217 static PyMemberDef keyszrange_pymembers
[] = {
218 #define MEMBERSTRUCT keyszrange_pyobj
219 MEMBER(min
, T_INT
, READONLY
, "KSZ.min -> smallest allowed key size")
220 MEMBER(max
, T_INT
, READONLY
, "KSZ.min -> largest allowed key size")
221 MEMBER(mod
, T_INT
, READONLY
,
222 "KSZ.mod -> key size must be a multiple of this")
227 static PyGetSetDef keyszset_pygetset
[] = {
228 #define GETSETNAME(op, name) ks##op##_##name
229 GET (min
, "KSZ.min -> smallest allowed key size")
230 GET (max
, "KSZ.min -> largest allowed key size")
235 static PyMemberDef keyszset_pymembers
[] = {
236 #define MEMBERSTRUCT keyszset_pyobj
237 MEMBER(set
, T_OBJECT
, READONLY
, "KSZ.set -> allowed key sizes")
242 static PyTypeObject keysz_pytype_skel
= {
243 PyObject_HEAD_INIT(0) 0, /* Header */
244 "KeySZ", /* @tp_name@ */
245 sizeof(keysz_pyobj
), /* @tp_basicsize@ */
246 0, /* @tp_itemsize@ */
248 0, /* @tp_dealloc@ */
250 0, /* @tp_getattr@ */
251 0, /* @tp_setattr@ */
252 0, /* @tp_compare@ */
254 0, /* @tp_as_number@ */
255 0, /* @tp_as_sequence@ */
256 0, /* @tp_as_mapping@ */
260 0, /* @tp_getattro@ */
261 0, /* @tp_setattro@ */
262 0, /* @tp_as_buffer@ */
263 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
267 "Key size constraints.",
269 0, /* @tp_traverse@ */
271 0, /* @tp_richcompare@ */
272 0, /* @tp_weaklistoffset@ */
274 0, /* @tp_iternext@ */
275 0, /* @tp_methods@ */
276 keysz_pymembers
, /* @tp_members@ */
280 0, /* @tp_descr_get@ */
281 0, /* @tp_descr_set@ */
282 0, /* @tp_dictoffset@ */
284 PyType_GenericAlloc
, /* @tp_alloc@ */
285 abstract_pynew
, /* @tp_new@ */
290 static PyTypeObject keyszany_pytype_skel
= {
291 PyObject_HEAD_INIT(0) 0, /* Header */
292 "KeySZAny", /* @tp_name@ */
293 sizeof(keysz_pyobj
), /* @tp_basicsize@ */
294 0, /* @tp_itemsize@ */
296 0, /* @tp_dealloc@ */
298 0, /* @tp_getattr@ */
299 0, /* @tp_setattr@ */
300 0, /* @tp_compare@ */
302 0, /* @tp_as_number@ */
303 0, /* @tp_as_sequence@ */
304 0, /* @tp_as_mapping@ */
308 0, /* @tp_getattro@ */
309 0, /* @tp_setattro@ */
310 0, /* @tp_as_buffer@ */
311 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
315 "Key size constraints. This object imposes no constraints on size.",
317 0, /* @tp_traverse@ */
319 0, /* @tp_richcompare@ */
320 0, /* @tp_weaklistoffset@ */
322 0, /* @tp_iternext@ */
323 0, /* @tp_methods@ */
324 0, /* @tp_members@ */
325 keyszany_pygetset
, /* @tp_getset@ */
328 0, /* @tp_descr_get@ */
329 0, /* @tp_descr_set@ */
330 0, /* @tp_dictoffset@ */
332 PyType_GenericAlloc
, /* @tp_alloc@ */
333 keyszany_pynew
, /* @tp_new@ */
338 static PyTypeObject keyszrange_pytype_skel
= {
339 PyObject_HEAD_INIT(0) 0, /* Header */
340 "KeySZRange", /* @tp_name@ */
341 sizeof(keyszrange_pyobj
), /* @tp_basicsize@ */
342 0, /* @tp_itemsize@ */
344 0, /* @tp_dealloc@ */
346 0, /* @tp_getattr@ */
347 0, /* @tp_setattr@ */
348 0, /* @tp_compare@ */
350 0, /* @tp_as_number@ */
351 0, /* @tp_as_sequence@ */
352 0, /* @tp_as_mapping@ */
356 0, /* @tp_getattro@ */
357 0, /* @tp_setattro@ */
358 0, /* @tp_as_buffer@ */
359 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
363 "Key size constraints. This object asserts minimum and maximum (if\n\
364 sizes, and requires the key length to be a multiple of some value.",
366 0, /* @tp_traverse@ */
368 0, /* @tp_richcompare@ */
369 0, /* @tp_weaklistoffset@ */
371 0, /* @tp_iternext@ */
372 0, /* @tp_methods@ */
373 keyszrange_pymembers
, /* @tp_members@ */
377 0, /* @tp_descr_get@ */
378 0, /* @tp_descr_set@ */
379 0, /* @tp_dictoffset@ */
381 PyType_GenericAlloc
, /* @tp_alloc@ */
382 keyszrange_pynew
, /* @tp_new@ */
387 static PyTypeObject keyszset_pytype_skel
= {
388 PyObject_HEAD_INIT(0) 0, /* Header */
389 "KeySZSet", /* @tp_name@ */
390 sizeof(keyszset_pyobj
), /* @tp_basicsize@ */
391 0, /* @tp_itemsize@ */
393 0, /* @tp_dealloc@ */
395 0, /* @tp_getattr@ */
396 0, /* @tp_setattr@ */
397 0, /* @tp_compare@ */
399 0, /* @tp_as_number@ */
400 0, /* @tp_as_sequence@ */
401 0, /* @tp_as_mapping@ */
405 0, /* @tp_getattro@ */
406 0, /* @tp_setattro@ */
407 0, /* @tp_as_buffer@ */
408 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
412 "Key size constraints. This object requires the key to be one of a\n\
415 0, /* @tp_traverse@ */
417 0, /* @tp_richcompare@ */
418 0, /* @tp_weaklistoffset@ */
420 0, /* @tp_iternext@ */
421 0, /* @tp_methods@ */
422 keyszset_pymembers
, /* @tp_members@ */
423 keyszset_pygetset
, /* @tp_getset@ */
426 0, /* @tp_descr_get@ */
427 0, /* @tp_descr_set@ */
428 0, /* @tp_dictoffset@ */
430 PyType_GenericAlloc
, /* @tp_alloc@ */
431 keyszset_pynew
, /* @tp_new@ */
436 #define KSZCONVOP(op) \
437 static PyObject *meth__KeySZ_##op(PyObject *me, PyObject *arg) \
440 if (!PyArg_ParseTuple(arg, "Od:" #op, &me, &x)) return (0); \
442 return (PyFloat_FromDouble(y)); \
445 KSZCONVOP(fromschnorr
)
454 /*----- Symmetric encryption ----------------------------------------------*/
456 PyTypeObject
*gccipher_pytype
, *gcipher_pytype
;
458 CONVFUNC(gccipher
, gccipher
*, GCCIPHER_CC
)
459 CONVFUNC(gcipher
, gcipher
*, GCIPHER_C
)
461 PyObject
*gcipher_pywrap(PyObject
*cobj
, gcipher
*c
, unsigned f
)
464 if (!cobj
) cobj
= gccipher_pywrap((/*unconst*/ gccipher
*)GC_CLASS(c
));
465 else Py_INCREF(cobj
);
466 g
= PyObject_NEW(gcipher_pyobj
, (PyTypeObject
*)cobj
);
469 return ((PyObject
*)g
);
472 static PyObject
*gcipher_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
474 char *kwlist
[] = { "k", 0 };
478 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", kwlist
, &k
, &sz
))
480 if (keysz(sz
, GCCIPHER_CC(ty
)->keysz
) != sz
) VALERR("bad key length");
481 return (gcipher_pywrap((PyObject
*)ty
,
482 GC_INIT(GCCIPHER_CC(ty
), k
, sz
),
488 PyObject
*gccipher_pywrap(gccipher
*cc
)
490 gccipher_pyobj
*g
= newtype(gccipher_pytype
, 0, cc
->name
);
492 g
->ty
.ht_type
.tp_basicsize
= sizeof(gcipher_pyobj
);
493 g
->ty
.ht_type
.tp_base
= gcipher_pytype
;
494 Py_INCREF(gcipher_pytype
);
495 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
496 Py_TPFLAGS_BASETYPE
|
497 Py_TPFLAGS_HEAPTYPE
);
498 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
499 g
->ty
.ht_type
.tp_free
= 0;
500 g
->ty
.ht_type
.tp_new
= gcipher_pynew
;
501 typeready(&g
->ty
.ht_type
);
502 return ((PyObject
*)g
);
505 static void gcipher_pydealloc(PyObject
*me
)
507 if (GCIPHER_F(me
) & f_freeme
)
508 GC_DESTROY(GCIPHER_C(me
));
509 Py_DECREF(me
->ob_type
);
513 static PyObject
*gccget_name(PyObject
*me
, void *hunoz
)
514 { return (PyString_FromString(GCCIPHER_CC(me
)->name
)); }
516 static PyObject
*gccget_keysz(PyObject
*me
, void *hunoz
)
517 { return (keysz_pywrap(GCCIPHER_CC(me
)->keysz
)); }
519 static PyObject
*gccget_blksz(PyObject
*me
, void *hunoz
)
520 { return (PyInt_FromLong(GCCIPHER_CC(me
)->blksz
)); }
522 static PyObject
*gcmeth_encrypt(PyObject
*me
, PyObject
*arg
)
528 if (!PyArg_ParseTuple(arg
, "s#:encrypt", &p
, &sz
)) return (0);
529 rc
= bytestring_pywrap(0, sz
);
530 GC_ENCRYPT(GCIPHER_C(me
), p
, PyString_AS_STRING(rc
), sz
);
534 static PyObject
*gcmeth_enczero(PyObject
*me
, PyObject
*arg
)
540 if (!PyArg_ParseTuple(arg
, "i:enczero", &sz
)) return (0);
541 rc
= bytestring_pywrap(0, sz
);
542 p
= PyString_AS_STRING(rc
);
544 GC_ENCRYPT(GCIPHER_C(me
), p
, p
, sz
);
548 static PyObject
*gcmeth_decrypt(PyObject
*me
, PyObject
*arg
)
554 if (!PyArg_ParseTuple(arg
, "s#:decrypt", &p
, &sz
)) return (0);
555 rc
= bytestring_pywrap(0, sz
);
556 GC_DECRYPT(GCIPHER_C(me
), p
, PyString_AS_STRING(rc
), sz
);
560 static PyObject
*gcmeth_deczero(PyObject
*me
, PyObject
*arg
)
566 if (!PyArg_ParseTuple(arg
, "i:deczero", &sz
)) return (0);
567 rc
= bytestring_pywrap(0, sz
);
568 p
= PyString_AS_STRING(rc
);
570 GC_DECRYPT(GCIPHER_C(me
), p
, p
, sz
);
574 static PyObject
*gcmeth_setiv(PyObject
*me
, PyObject
*arg
)
579 if (!PyArg_ParseTuple(arg
, "s#:setiv", &p
, &sz
)) goto end
;
580 if (!GCIPHER_C(me
)->ops
->setiv
) VALERR("`setiv' not supported");
581 if (!GC_CLASS(GCIPHER_C(me
))->blksz
) VALERR("not a block cipher mode");
582 if (sz
!= GC_CLASS(GCIPHER_C(me
))->blksz
) VALERR("bad IV length");
583 GC_SETIV(GCIPHER_C(me
), p
);
589 static PyObject
*gcmeth_bdry(PyObject
*me
, PyObject
*arg
)
591 if (!PyArg_ParseTuple(arg
, ":bdry")) goto end
;
592 if (!GCIPHER_C(me
)->ops
->bdry
) VALERR("`bdry' not supported");
593 if (!GC_CLASS(GCIPHER_C(me
))->blksz
) VALERR("not a block cipher mode");
594 GC_BDRY(GCIPHER_C(me
));
600 static PyGetSetDef gccipher_pygetset
[] = {
601 #define GETSETNAME(op, name) gcc##op##_##name
602 GET (keysz
, "CC.keysz -> acceptable key sizes")
603 GET (blksz
, "CC.blksz -> block size, or zero")
604 GET (name
, "CC.name -> name of this kind of cipher")
609 static PyMethodDef gcipher_pymethods
[] = {
610 #define METHNAME(name) gcmeth_##name
611 METH (encrypt
, "C.encrypt(PT) -> CT")
612 METH (enczero
, "C.enczero(N) -> CT")
613 METH (decrypt
, "C.decrypt(CT) -> PT")
614 METH (deczero
, "C.deczero(N) -> PT")
615 METH (setiv
, "C.setiv(IV)")
616 METH (bdry
, "C.bdry()")
621 static PyTypeObject gccipher_pytype_skel
= {
622 PyObject_HEAD_INIT(0) 0, /* Header */
623 "GCCipher", /* @tp_name@ */
624 sizeof(gccipher_pyobj
), /* @tp_basicsize@ */
625 0, /* @tp_itemsize@ */
627 0, /* @tp_dealloc@ */
629 0, /* @tp_getattr@ */
630 0, /* @tp_setattr@ */
631 0, /* @tp_compare@ */
633 0, /* @tp_as_number@ */
634 0, /* @tp_as_sequence@ */
635 0, /* @tp_as_mapping@ */
639 0, /* @tp_getattro@ */
640 0, /* @tp_setattro@ */
641 0, /* @tp_as_buffer@ */
642 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
646 "Symmetric cipher metaclass.",
648 0, /* @tp_traverse@ */
650 0, /* @tp_richcompare@ */
651 0, /* @tp_weaklistoffset@ */
653 0, /* @tp_iternext@ */
654 0, /* @tp_methods@ */
655 0, /* @tp_members@ */
656 gccipher_pygetset
, /* @tp_getset@ */
659 0, /* @tp_descr_get@ */
660 0, /* @tp_descr_set@ */
661 0, /* @tp_dictoffset@ */
663 PyType_GenericAlloc
, /* @tp_alloc@ */
664 abstract_pynew
, /* @tp_new@ */
669 static PyTypeObject gcipher_pytype_skel
= {
670 PyObject_HEAD_INIT(0) 0, /* Header */
671 "GCipher", /* @tp_name@ */
672 sizeof(gcipher_pyobj
), /* @tp_basicsize@ */
673 0, /* @tp_itemsize@ */
675 gcipher_pydealloc
, /* @tp_dealloc@ */
677 0, /* @tp_getattr@ */
678 0, /* @tp_setattr@ */
679 0, /* @tp_compare@ */
681 0, /* @tp_as_number@ */
682 0, /* @tp_as_sequence@ */
683 0, /* @tp_as_mapping@ */
687 0, /* @tp_getattro@ */
688 0, /* @tp_setattro@ */
689 0, /* @tp_as_buffer@ */
690 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
694 "Symmetric cipher, abstract base class.",
696 0, /* @tp_traverse@ */
698 0, /* @tp_richcompare@ */
699 0, /* @tp_weaklistoffset@ */
701 0, /* @tp_iternext@ */
702 gcipher_pymethods
, /* @tp_methods@ */
703 0, /* @tp_members@ */
707 0, /* @tp_descr_get@ */
708 0, /* @tp_descr_set@ */
709 0, /* @tp_dictoffset@ */
711 PyType_GenericAlloc
, /* @tp_alloc@ */
712 abstract_pynew
, /* @tp_new@ */
717 /*----- Hash functions ----------------------------------------------------*/
719 PyTypeObject
*gchash_pytype
, *ghash_pytype
;
721 CONVFUNC(gchash
, gchash
*, GCHASH_CH
)
722 CONVFUNC(ghash
, ghash
*, GHASH_H
)
724 static PyObject
*ghash_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
726 char *kwlist
[] = { 0 };
727 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, ":new", kwlist
))
729 return (ghash_pywrap((PyObject
*)ty
, GH_INIT(GCHASH_CH(ty
)), f_freeme
));
734 PyObject
*gchash_pywrap(gchash
*ch
)
736 gchash_pyobj
*g
= newtype(gchash_pytype
, 0, ch
->name
);
738 g
->ty
.ht_type
.tp_basicsize
= sizeof(ghash_pyobj
);
739 g
->ty
.ht_type
.tp_base
= ghash_pytype
;
740 Py_INCREF(ghash_pytype
);
741 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
742 Py_TPFLAGS_BASETYPE
|
743 Py_TPFLAGS_HEAPTYPE
);
744 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
745 g
->ty
.ht_type
.tp_free
= 0;
746 g
->ty
.ht_type
.tp_new
= ghash_pynew
;
747 typeready(&g
->ty
.ht_type
);
748 return ((PyObject
*)g
);
751 PyObject
*ghash_pywrap(PyObject
*cobj
, ghash
*h
, unsigned f
)
754 if (!cobj
) cobj
= gchash_pywrap((/*unconst*/ gchash
*)GH_CLASS(h
));
755 else Py_INCREF(cobj
);
756 g
= PyObject_NEW(ghash_pyobj
, (PyTypeObject
*)cobj
);
759 return ((PyObject
*)g
);
762 static void ghash_pydealloc(PyObject
*me
)
764 if (GHASH_F(me
) & f_freeme
)
765 GH_DESTROY(GHASH_H(me
));
766 Py_DECREF(me
->ob_type
);
770 static PyObject
*gchget_name(PyObject
*me
, void *hunoz
)
771 { return (PyString_FromString(GCHASH_CH(me
)->name
)); }
773 static PyObject
*gchget_hashsz(PyObject
*me
, void *hunoz
)
774 { return (PyInt_FromLong(GCHASH_CH(me
)->hashsz
)); }
776 static PyObject
*gchget_bufsz(PyObject
*me
, void *hunoz
)
777 { return (PyInt_FromLong(GCHASH_CH(me
)->bufsz
)); }
779 static PyObject
*ghmeth_hash(PyObject
*me
, PyObject
*arg
)
783 if (!PyArg_ParseTuple(arg
, "s#:hash", &p
, &sz
)) return (0);
784 GH_HASH(GHASH_H(me
), p
, sz
);
788 #define GHMETH_HASHU_(n, W, w) \
789 static PyObject *ghmeth_hashu##w(PyObject *me, PyObject *arg) \
792 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \
793 GH_HASHU##W(GHASH_H(me), x); \
798 DOUINTCONV(GHMETH_HASHU_
)
800 #define GHMETH_HASHBUF_(n, W, w) \
801 static PyObject *ghmeth_hashbuf##w(PyObject *me, PyObject *arg) \
805 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
806 if (sz > MASK##n) TYERR("string too long"); \
807 GH_HASHBUF##W(GHASH_H(me), p, sz); \
812 DOUINTCONV(GHMETH_HASHBUF_
)
814 static PyObject
*ghmeth_hashstrz(PyObject
*me
, PyObject
*arg
)
817 if (!PyArg_ParseTuple(arg
, "s:hashstrz", &p
)) return (0);
818 GH_HASHSTRZ(GHASH_H(me
), p
);
822 static PyObject
*ghmeth_done(PyObject
*me
, PyObject
*arg
)
826 if (!PyArg_ParseTuple(arg
, ":done")) return (0);
827 g
= GH_COPY(GHASH_H(me
));
828 rc
= bytestring_pywrap(0, g
->ops
->c
->hashsz
);
829 GH_DONE(g
, PyString_AS_STRING(rc
));
834 static PyGetSetDef gchash_pygetset
[] = {
835 #define GETSETNAME(op, name) gch##op##_##name
836 GET (bufsz
, "CH.bufsz -> hash buffer size, or zero")
837 GET (hashsz
, "CH.hashsz -> hash output size")
838 GET (name
, "CH.name -> name of this kind of hash")
843 static PyMethodDef ghash_pymethods
[] = {
844 #define METHNAME(name) ghmeth_##name
845 METH (hash
, "H.hash(M)")
846 #define METHU_(n, W, w) METH(hashu##w, "H.hashu" #w "(WORD)")
849 #define METHBUF_(n, W, w) METH(hashbuf##w, "H.hashbuf" #w "(BYTES)")
852 METH (hashstrz
, "H.hashstrz(STRING)")
853 METH (done
, "H.done() -> HASH")
858 static PyTypeObject gchash_pytype_skel
= {
859 PyObject_HEAD_INIT(0) 0, /* Header */
860 "GCHash", /* @tp_name@ */
861 sizeof(gchash_pyobj
), /* @tp_basicsize@ */
862 0, /* @tp_itemsize@ */
864 0, /* @tp_dealloc@ */
866 0, /* @tp_getattr@ */
867 0, /* @tp_setattr@ */
868 0, /* @tp_compare@ */
870 0, /* @tp_as_number@ */
871 0, /* @tp_as_sequence@ */
872 0, /* @tp_as_mapping@ */
876 0, /* @tp_getattro@ */
877 0, /* @tp_setattro@ */
878 0, /* @tp_as_buffer@ */
879 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
883 "Hash function metaclass.",
885 0, /* @tp_traverse@ */
887 0, /* @tp_richcompare@ */
888 0, /* @tp_weaklistoffset@ */
890 0, /* @tp_iternext@ */
891 0, /* @tp_methods@ */
892 0, /* @tp_members@ */
893 gchash_pygetset
, /* @tp_getset@ */
896 0, /* @tp_descr_get@ */
897 0, /* @tp_descr_set@ */
898 0, /* @tp_dictoffset@ */
900 PyType_GenericAlloc
, /* @tp_alloc@ */
901 abstract_pynew
, /* @tp_new@ */
906 static PyTypeObject ghash_pytype_skel
= {
907 PyObject_HEAD_INIT(0) 0, /* Header */
908 "GHash", /* @tp_name@ */
909 sizeof(ghash_pyobj
), /* @tp_basicsize@ */
910 0, /* @tp_itemsize@ */
912 ghash_pydealloc
, /* @tp_dealloc@ */
914 0, /* @tp_getattr@ */
915 0, /* @tp_setattr@ */
916 0, /* @tp_compare@ */
918 0, /* @tp_as_number@ */
919 0, /* @tp_as_sequence@ */
920 0, /* @tp_as_mapping@ */
924 0, /* @tp_getattro@ */
925 0, /* @tp_setattro@ */
926 0, /* @tp_as_buffer@ */
927 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
931 "Hash function, abstract base class.",
933 0, /* @tp_traverse@ */
935 0, /* @tp_richcompare@ */
936 0, /* @tp_weaklistoffset@ */
938 0, /* @tp_iternext@ */
939 ghash_pymethods
, /* @tp_methods@ */
940 0, /* @tp_members@ */
944 0, /* @tp_descr_get@ */
945 0, /* @tp_descr_set@ */
946 0, /* @tp_dictoffset@ */
948 PyType_GenericAlloc
, /* @tp_alloc@ */
949 abstract_pynew
, /* @tp_new@ */
954 /*----- Message authentication --------------------------------------------*/
956 PyTypeObject
*gcmac_pytype
, *gmac_pytype
, *gmhash_pytype
;
958 CONVFUNC(gcmac
, gcmac
*, GCMAC_CM
)
959 CONVFUNC(gmac
, gmac
*, GMAC_M
)
960 CONVFUNC(gmhash
, ghash
*, GHASH_H
)
962 static PyObject
*gmac_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
964 char *kwlist
[] = { "k", 0 };
968 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", kwlist
, &k
, &sz
))
970 if (keysz(sz
, GCMAC_CM(ty
)->keysz
) != sz
) VALERR("bad key length");
971 return (gmac_pywrap((PyObject
*)ty
,
972 GM_KEY(GCMAC_CM(ty
), k
, sz
),
978 static PyObject
*gmhash_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
980 char *kwlist
[] = { 0 };
983 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, ":new", kwlist
)) return (0);
984 g
= PyObject_NEW(ghash_pyobj
, ty
);
985 g
->h
= GM_INIT(GMAC_M(ty
));
988 return ((PyObject
*)g
);
991 PyObject
*gcmac_pywrap(gcmac
*cm
)
993 gcmac_pyobj
*g
= newtype(gcmac_pytype
, 0, cm
->name
);
995 g
->ty
.ht_type
.tp_basicsize
= sizeof(gmac_pyobj
);
996 g
->ty
.ht_type
.tp_base
= gmac_pytype
;
997 Py_INCREF(gmac_pytype
);
998 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
999 Py_TPFLAGS_BASETYPE
|
1000 Py_TPFLAGS_HEAPTYPE
);
1001 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
1002 g
->ty
.ht_type
.tp_free
= 0;
1003 g
->ty
.ht_type
.tp_new
= gmac_pynew
;
1004 typeready(&g
->ty
.ht_type
);
1005 return ((PyObject
*)g
);
1008 PyObject
*gmac_pywrap(PyObject
*cobj
, gmac
*m
, unsigned f
)
1011 if (!cobj
) cobj
= gcmac_pywrap((/*unconst*/ gcmac
*)GM_CLASS(m
));
1012 else Py_INCREF(cobj
);
1013 g
= newtype((PyTypeObject
*)cobj
, 0, 0);
1014 g
->ty
.ht_type
.tp_basicsize
= sizeof(ghash_pyobj
);
1015 g
->ty
.ht_name
= PyString_FromFormat("%s(keyed)", m
->ops
->c
->name
);
1016 g
->ty
.ht_type
.tp_name
= PyString_AS_STRING(g
->ty
.ht_name
);
1017 g
->ty
.ht_type
.tp_base
= gmhash_pytype
;
1018 Py_INCREF(gmac_pytype
);
1019 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
1020 Py_TPFLAGS_BASETYPE
|
1021 Py_TPFLAGS_HEAPTYPE
);
1022 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
1023 g
->ty
.ht_type
.tp_free
= 0;
1024 g
->ty
.ht_type
.tp_new
= gmhash_pynew
;
1025 typeready(&g
->ty
.ht_type
);
1028 return ((PyObject
*)g
);
1031 static void gmac_pydealloc(PyObject
*me
)
1033 if (GMAC_F(me
) & f_freeme
)
1034 GM_DESTROY(GMAC_M(me
));
1035 Py_DECREF(me
->ob_type
);
1036 PyType_Type
.tp_dealloc(me
);
1039 static PyObject
*gcmget_name(PyObject
*me
, void *hunoz
)
1040 { return (PyString_FromString(GCMAC_CM(me
)->name
)); }
1042 static PyObject
*gcmget_keysz(PyObject
*me
, void *hunoz
)
1043 { return (keysz_pywrap(GCMAC_CM(me
)->keysz
)); }
1045 static PyObject
*gcmget_tagsz(PyObject
*me
, void *hunoz
)
1046 { return (PyInt_FromLong(GCMAC_CM(me
)->hashsz
)); }
1048 static PyGetSetDef gcmac_pygetset
[] = {
1049 #define GETSETNAME(op, name) gcm##op##_##name
1050 GET (keysz
, "CM.keysz -> acceptable key sizes")
1051 GET (tagsz
, "CM.tagsz -> MAC output size")
1052 GET (name
, "CM.name -> name of this kind of MAC")
1057 static PyTypeObject gcmac_pytype_skel
= {
1058 PyObject_HEAD_INIT(0) 0, /* Header */
1059 "GCMAC", /* @tp_name@ */
1060 sizeof(gchash_pyobj
), /* @tp_basicsize@ */
1061 0, /* @tp_itemsize@ */
1063 0, /* @tp_dealloc@ */
1065 0, /* @tp_getattr@ */
1066 0, /* @tp_setattr@ */
1067 0, /* @tp_compare@ */
1069 0, /* @tp_as_number@ */
1070 0, /* @tp_as_sequence@ */
1071 0, /* @tp_as_mapping@ */
1075 0, /* @tp_getattro@ */
1076 0, /* @tp_setattro@ */
1077 0, /* @tp_as_buffer@ */
1078 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1079 Py_TPFLAGS_BASETYPE
,
1082 "Message authentication code metametaclass.",
1084 0, /* @tp_traverse@ */
1086 0, /* @tp_richcompare@ */
1087 0, /* @tp_weaklistoffset@ */
1089 0, /* @tp_iternext@ */
1090 0, /* @tp_methods@ */
1091 0, /* @tp_members@ */
1092 gcmac_pygetset
, /* @tp_getset@ */
1095 0, /* @tp_descr_get@ */
1096 0, /* @tp_descr_set@ */
1097 0, /* @tp_dictoffset@ */
1099 PyType_GenericAlloc
, /* @tp_alloc@ */
1100 abstract_pynew
, /* @tp_new@ */
1105 static PyTypeObject gmac_pytype_skel
= {
1106 PyObject_HEAD_INIT(0) 0, /* Header */
1107 "GMAC", /* @tp_name@ */
1108 sizeof(gmac_pyobj
), /* @tp_basicsize@ */
1109 0, /* @tp_itemsize@ */
1111 gmac_pydealloc
, /* @tp_dealloc@ */
1113 0, /* @tp_getattr@ */
1114 0, /* @tp_setattr@ */
1115 0, /* @tp_compare@ */
1117 0, /* @tp_as_number@ */
1118 0, /* @tp_as_sequence@ */
1119 0, /* @tp_as_mapping@ */
1123 0, /* @tp_getattro@ */
1124 0, /* @tp_setattro@ */
1125 0, /* @tp_as_buffer@ */
1126 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1127 Py_TPFLAGS_BASETYPE
,
1130 "Message authentication code metaclass, abstract base class.",
1132 0, /* @tp_traverse@ */
1134 0, /* @tp_richcompare@ */
1135 0, /* @tp_weaklistoffset@ */
1137 0, /* @tp_iternext@ */
1138 0, /* @tp_methods@ */
1139 0, /* @tp_members@ */
1140 0, /* @tp_getset@ */
1143 0, /* @tp_descr_get@ */
1144 0, /* @tp_descr_set@ */
1145 0, /* @tp_dictoffset@ */
1147 PyType_GenericAlloc
, /* @tp_alloc@ */
1148 abstract_pynew
, /* @tp_new@ */
1153 static PyTypeObject gmhash_pytype_skel
= {
1154 PyObject_HEAD_INIT(0) 0, /* Header */
1155 "GMACHash", /* @tp_name@ */
1156 sizeof(ghash_pyobj
), /* @tp_basicsize@ */
1157 0, /* @tp_itemsize@ */
1159 ghash_pydealloc
, /* @tp_dealloc@ */
1161 0, /* @tp_getattr@ */
1162 0, /* @tp_setattr@ */
1163 0, /* @tp_compare@ */
1165 0, /* @tp_as_number@ */
1166 0, /* @tp_as_sequence@ */
1167 0, /* @tp_as_mapping@ */
1171 0, /* @tp_getattro@ */
1172 0, /* @tp_setattro@ */
1173 0, /* @tp_as_buffer@ */
1174 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1175 Py_TPFLAGS_BASETYPE
,
1178 "Message authentication code, abstract base class.",
1180 0, /* @tp_traverse@ */
1182 0, /* @tp_richcompare@ */
1183 0, /* @tp_weaklistoffset@ */
1185 0, /* @tp_iternext@ */
1186 0, /* @tp_methods@ */
1187 0, /* @tp_members@ */
1188 0, /* @tp_getset@ */
1191 0, /* @tp_descr_get@ */
1192 0, /* @tp_descr_set@ */
1193 0, /* @tp_dictoffset@ */
1195 PyType_GenericAlloc
, /* @tp_alloc@ */
1196 abstract_pynew
, /* @tp_new@ */
1201 /*----- Special snowflake for Poly1305 ------------------------------------*/
1203 PyTypeObject
*poly1305cls_pytype
, *poly1305key_pytype
, *poly1305hash_pytype
;
1205 typedef struct poly1305key_pyobj
{
1206 PyHeapTypeObject ty
;
1208 } poly1305key_pyobj
;
1210 typedef struct poly1305hash_pyobj
{
1215 } poly1305hash_pyobj
;
1217 #define P1305_F(o) (((poly1305hash_pyobj *)(o))->f)
1218 #define P1305_CTX(o) (&((poly1305hash_pyobj *)(o))->ctx)
1219 CONVFUNC(poly1305hash
, poly1305_ctx
*, P1305_CTX
)
1221 static PyObject
*poly1305hash_pynew(PyTypeObject
*ty
,
1222 PyObject
*arg
, PyObject
*kw
)
1224 char *kwlist
[] = { "mask", 0 };
1225 poly1305key_pyobj
*pk
= (poly1305key_pyobj
*)ty
;
1226 poly1305hash_pyobj
*ph
;
1230 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|s#:new", kwlist
, &m
, &sz
))
1232 if (m
&& sz
!= POLY1305_MASKSZ
) VALERR("bad mask length");
1233 ph
= PyObject_NEW(poly1305hash_pyobj
, ty
);
1235 if (m
) ph
->f
|= f_mask
;
1236 poly1305_macinit(&ph
->ctx
, &pk
->k
, m
);
1238 return ((PyObject
*)ph
);
1243 static PyObject
*poly1305key_pynew(PyTypeObject
*ty
,
1244 PyObject
*arg
, PyObject
*kw
)
1246 char *kwlist
[] = { "k", 0 };
1247 poly1305key_pyobj
*pk
;
1251 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", kwlist
, &k
, &sz
))
1253 if (keysz(sz
, poly1305_keysz
) != sz
) VALERR("bad key length");
1255 pk
= newtype(ty
, 0, 0);
1256 pk
->ty
.ht_name
= PyString_FromString("poly1305(keyed)");
1257 pk
->ty
.ht_type
.tp_basicsize
= sizeof(poly1305hash_pyobj
);
1258 pk
->ty
.ht_type
.tp_name
= PyString_AS_STRING(pk
->ty
.ht_name
);
1259 pk
->ty
.ht_type
.tp_base
= poly1305hash_pytype
;
1260 Py_INCREF(poly1305key_pytype
);
1261 pk
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
1262 Py_TPFLAGS_BASETYPE
|
1263 Py_TPFLAGS_HEAPTYPE
);
1264 pk
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
1265 pk
->ty
.ht_type
.tp_free
= 0;
1266 pk
->ty
.ht_type
.tp_new
= poly1305hash_pynew
;
1267 typeready(&pk
->ty
.ht_type
);
1269 poly1305_keyinit(&pk
->k
, k
, sz
);
1270 return ((PyObject
*)pk
);
1276 static PyObject
*poly1305clsget_name(PyObject
*me
, void *hunoz
)
1277 { return (PyString_FromString("poly1305")); }
1279 static PyObject
*poly1305clsget_keysz(PyObject
*me
, void *hunoz
)
1280 { return (keysz_pywrap(poly1305_keysz
)); }
1282 static PyObject
*poly1305clsget_masksz(PyObject
*me
, void *hunoz
)
1283 { return (PyInt_FromLong(POLY1305_MASKSZ
)); }
1285 static PyObject
*poly1305clsget_tagsz(PyObject
*me
, void *hunoz
)
1286 { return (PyInt_FromLong(POLY1305_TAGSZ
)); }
1288 static PyObject
*polymeth_copy(PyObject
*me
, PyObject
*arg
)
1290 poly1305hash_pyobj
*ph
;
1291 if (!PyArg_ParseTuple(arg
, ":copy")) return (0);
1292 ph
= PyObject_NEW(poly1305hash_pyobj
, me
->ob_type
);
1293 poly1305_copy(&ph
->ctx
, P1305_CTX(me
));
1294 Py_INCREF(me
->ob_type
);
1295 return ((PyObject
*)ph
);
1298 static PyObject
*polymeth_hash(PyObject
*me
, PyObject
*arg
)
1302 if (!PyArg_ParseTuple(arg
, "s#:hash", &p
, &sz
)) return (0);
1303 poly1305_hash(P1305_CTX(me
), p
, sz
);
1307 #define POLYMETH_HASHU_(n, W, w) \
1308 static PyObject *polymeth_hashu##w(PyObject *me, PyObject *arg) \
1312 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \
1313 STORE##W(b, x); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \
1318 DOUINTCONV(POLYMETH_HASHU_
)
1320 #define POLYMETH_HASHBUF_(n, W, w) \
1321 static PyObject *polymeth_hashbuf##w(PyObject *me, PyObject *arg) \
1326 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
1327 if (sz > MASK##n) TYERR("string too long"); \
1328 STORE##W(b, sz); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \
1329 poly1305_hash(P1305_CTX(me), p, sz); \
1334 DOUINTCONV(POLYMETH_HASHBUF_
)
1336 static PyObject
*polymeth_hashstrz(PyObject
*me
, PyObject
*arg
)
1339 if (!PyArg_ParseTuple(arg
, "s:hashstrz", &p
)) return (0);
1340 poly1305_hash(P1305_CTX(me
), p
, strlen(p
) + 1);
1344 static PyObject
*polymeth_flush(PyObject
*me
, PyObject
*arg
)
1346 if (!PyArg_ParseTuple(arg
, ":flush")) return (0);
1347 poly1305_flush(P1305_CTX(me
));
1351 static PyObject
*polymeth_flushzero(PyObject
*me
, PyObject
*arg
)
1353 if (!PyArg_ParseTuple(arg
, ":flushzero")) return (0);
1354 poly1305_flushzero(P1305_CTX(me
));
1358 static PyObject
*polymeth_concat(PyObject
*me
, PyObject
*arg
)
1360 PyObject
*pre
, *suff
;
1361 if (!PyArg_ParseTuple(arg
, "OO:concat", &pre
, &suff
)) return (0);
1362 if (!PyObject_TypeCheck(pre
, poly1305hash_pytype
) ||
1363 !PyObject_TypeCheck(suff
, poly1305hash_pytype
))
1364 TYERR("wanted a poly1305hash");
1365 if (me
->ob_type
!= pre
->ob_type
|| me
->ob_type
!= suff
->ob_type
)
1366 TYERR("key mismatch");
1367 if (P1305_CTX(pre
)->nbuf
) VALERR("prefix is not block-aligned");
1368 poly1305_concat(P1305_CTX(me
), P1305_CTX(pre
), P1305_CTX(suff
));
1374 static PyObject
*polymeth_done(PyObject
*me
, PyObject
*arg
)
1377 if (!PyArg_ParseTuple(arg
, ":done")) return (0);
1378 if (!(P1305_F(me
) & f_mask
)) VALERR("no mask");
1379 rc
= bytestring_pywrap(0, POLY1305_TAGSZ
);
1380 poly1305_done(P1305_CTX(me
), PyString_AS_STRING(rc
));
1386 static PyGetSetDef poly1305cls_pygetset
[] = {
1387 #define GETSETNAME(op, name) poly1305cls##op##_##name
1388 GET (keysz
, "PC.keysz -> acceptable key sizes")
1389 GET (masksz
, "PC.masksz -> mask size")
1390 GET (tagsz
, "PC.tagsz -> MAC output size")
1391 GET (name
, "PC.name -> name of this kind of MAC")
1396 static PyMethodDef poly1305hash_pymethods
[] = {
1397 #define METHNAME(name) polymeth_##name
1398 METH (copy
, "P.copy() -> PP")
1399 METH (hash
, "P.hash(M)")
1400 #define METHU_(n, W, w) METH(hashu##w, "P.hashu" #w "(WORD)")
1403 #define METHBUF_(n, W, w) METH(hashbuf##w, "P.hashbuf" #w "(BYTES)")
1404 DOUINTCONV(METHBUF_
)
1406 METH (hashstrz
, "P.hashstrz(STRING)")
1407 METH (flush
, "P.flush()")
1408 METH (flushzero
, "P.flushzero()")
1409 METH (concat
, "P.concat(PREFIX, SUFFIX)")
1410 METH (done
, "P.done() -> TAG")
1415 static PyTypeObject poly1305cls_pytype_skel
= {
1416 PyObject_HEAD_INIT(0) 0, /* Header */
1417 "Poly1305Class", /* @tp_name@ */
1418 sizeof(PyHeapTypeObject
), /* @tp_basicsize@ */
1419 0, /* @tp_itemsize@ */
1421 0, /* @tp_dealloc@ */
1423 0, /* @tp_getattr@ */
1424 0, /* @tp_setattr@ */
1425 0, /* @tp_compare@ */
1427 0, /* @tp_as_number@ */
1428 0, /* @tp_as_sequence@ */
1429 0, /* @tp_as_mapping@ */
1433 0, /* @tp_getattro@ */
1434 0, /* @tp_setattro@ */
1435 0, /* @tp_as_buffer@ */
1436 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1437 Py_TPFLAGS_BASETYPE
,
1440 "Poly1305 metametaclass. Best not to ask.",
1442 0, /* @tp_traverse@ */
1444 0, /* @tp_richcompare@ */
1445 0, /* @tp_weaklistoffset@ */
1447 0, /* @tp_iternext@ */
1448 0, /* @tp_methods@ */
1449 0, /* @tp_members@ */
1450 poly1305cls_pygetset
, /* @tp_getset@ */
1453 0, /* @tp_descr_get@ */
1454 0, /* @tp_descr_set@ */
1455 0, /* @tp_dictoffset@ */
1457 PyType_GenericAlloc
, /* @tp_alloc@ */
1458 abstract_pynew
, /* @tp_new@ */
1463 static PyTypeObject poly1305key_pytype_skel
= {
1464 PyObject_HEAD_INIT(0) 0, /* Header */
1465 "poly1305", /* @tp_name@ */
1466 sizeof(poly1305key_pyobj
), /* @tp_basicsize@ */
1467 0, /* @tp_itemsize@ */
1469 0, /* @tp_dealloc@ */
1471 0, /* @tp_getattr@ */
1472 0, /* @tp_setattr@ */
1473 0, /* @tp_compare@ */
1475 0, /* @tp_as_number@ */
1476 0, /* @tp_as_sequence@ */
1477 0, /* @tp_as_mapping@ */
1481 0, /* @tp_getattro@ */
1482 0, /* @tp_setattro@ */
1483 0, /* @tp_as_buffer@ */
1484 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1485 Py_TPFLAGS_BASETYPE
,
1490 0, /* @tp_traverse@ */
1492 0, /* @tp_richcompare@ */
1493 0, /* @tp_weaklistoffset@ */
1495 0, /* @tp_iternext@ */
1496 0, /* @tp_methods@ */
1497 0, /* @tp_members@ */
1498 0, /* @tp_getset@ */
1501 0, /* @tp_descr_get@ */
1502 0, /* @tp_descr_set@ */
1503 0, /* @tp_dictoffset@ */
1505 PyType_GenericAlloc
, /* @tp_alloc@ */
1506 poly1305key_pynew
, /* @tp_new@ */
1511 static PyTypeObject poly1305hash_pytype_skel
= {
1512 PyObject_HEAD_INIT(0) 0, /* Header */
1513 "Poly1305Hash", /* @tp_name@ */
1514 sizeof(poly1305hash_pyobj
), /* @tp_basicsize@ */
1515 0, /* @tp_itemsize@ */
1517 0, /* @tp_dealloc@ */
1519 0, /* @tp_getattr@ */
1520 0, /* @tp_setattr@ */
1521 0, /* @tp_compare@ */
1523 0, /* @tp_as_number@ */
1524 0, /* @tp_as_sequence@ */
1525 0, /* @tp_as_mapping@ */
1529 0, /* @tp_getattro@ */
1530 0, /* @tp_setattro@ */
1531 0, /* @tp_as_buffer@ */
1532 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1533 Py_TPFLAGS_BASETYPE
,
1536 "Poly1305 MAC context base class.",
1538 0, /* @tp_traverse@ */
1540 0, /* @tp_richcompare@ */
1541 0, /* @tp_weaklistoffset@ */
1543 0, /* @tp_iternext@ */
1544 poly1305hash_pymethods
, /* @tp_methods@ */
1545 0, /* @tp_members@ */
1546 0, /* @tp_getset@ */
1549 0, /* @tp_descr_get@ */
1550 0, /* @tp_descr_set@ */
1551 0, /* @tp_dictoffset@ */
1553 PyType_GenericAlloc
, /* @tp_alloc@ */
1554 abstract_pynew
, /* @tp_new@ */
1559 /*----- Special snowflake for HSalsa and HChaCha --------------------------*/
1561 #define DEF_HDANCE(DANCE, HDANCE, dance, hdance) \
1562 static PyObject *meth_##hdance##_prf(PyObject *me, PyObject *arg) \
1564 dance##_ctx dance; \
1566 Py_ssize_t ksz, nsz; \
1568 if (!PyArg_ParseTuple(arg, "s#s#:" #hdance "_prf", \
1569 &k, &ksz, &n, &nsz)) \
1571 if (ksz != DANCE##_KEYSZ) VALERR("bad key length"); \
1572 if (nsz != HDANCE##_INSZ) VALERR("bad input length"); \
1573 rc = bytestring_pywrap(0, HSALSA20_OUTSZ); \
1574 dance##_init(&dance, k, ksz, 0); \
1575 hdance##_prf(&dance, n, PyString_AS_STRING(rc)); \
1581 DEF_HDANCE(SALSA20
, HSALSA20
, salsa20
, hsalsa20
)
1582 DEF_HDANCE(SALSA20
, HSALSA20
, salsa20
, hsalsa2012
)
1583 DEF_HDANCE(SALSA20
, HSALSA20
, salsa20
, hsalsa208
)
1585 DEF_HDANCE(CHACHA
, HCHACHA
, chacha
, hchacha20
)
1586 DEF_HDANCE(CHACHA
, HCHACHA
, chacha
, hchacha12
)
1587 DEF_HDANCE(CHACHA
, HCHACHA
, chacha
, hchacha8
)
1589 /*----- Keccak-p[1600, n] -------------------------------------------------*/
1591 static PyTypeObject
*kxvik_pytype
;
1593 typedef struct kxvik_pyobj
{
1599 static PyObject
*kxvik_pynew(PyTypeObject
*ty
,
1600 PyObject
*arg
, PyObject
*kw
)
1603 kxvik_pyobj
*rc
= 0;
1604 char *kwlist
[] = { "nround", 0 };
1605 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:new", kwlist
,
1608 rc
= (kxvik_pyobj
*)ty
->tp_alloc(ty
, 0);
1610 keccak1600_init(&rc
->s
);
1612 return ((PyObject
*)rc
);
1615 static PyObject
*kxvikmeth_mix(PyObject
*me
, PyObject
*arg
)
1617 kxvik_pyobj
*k
= (kxvik_pyobj
*)me
;
1622 char *p
; Py_ssize_t n
;
1624 if (!PyArg_ParseTuple(arg
, "s#:mix", &p
, &n
)) goto end
;
1625 if (n
> 200) VALERR("out of range");
1626 q
= (const octet
*)p
;
1628 while (n
> 8) { LOAD64_L_(t
[i
], q
); i
++; q
+= 8; n
-= 8; }
1630 memcpy(buf
, q
, n
); memset(buf
+ n
, 0, 8 - n
);
1631 LOAD64_L_(t
[i
], buf
); i
++;
1633 keccak1600_mix(&k
->s
, t
, i
);
1639 static PyObject
*kxvikmeth_extract(PyObject
*me
, PyObject
*arg
)
1641 kxvik_pyobj
*k
= (kxvik_pyobj
*)me
;
1648 if (!PyArg_ParseTuple(arg
, "O&:mix", convuint
, &n
)) goto end
;
1649 if (n
> 200) VALERR("out of range");
1650 rc
= bytestring_pywrap(0, n
);
1651 q
= (octet
*)PyString_AS_STRING(rc
);
1652 keccak1600_extract(&k
->s
, t
, (n
+ 7)/8);
1654 while (n
> 8) { STORE64_L_(q
, t
[i
]); i
++; q
+= 8; n
-= 8; }
1655 if (n
) { STORE64_L_(buf
, t
[i
]); memcpy(q
, buf
, n
); }
1660 static PyObject
*kxvikmeth_step(PyObject
*me
, PyObject
*arg
)
1662 kxvik_pyobj
*k
= (kxvik_pyobj
*)me
;
1663 if (!PyArg_ParseTuple(arg
, ":step")) return (0);
1664 keccak1600_p(&k
->s
, &k
->s
, k
->n
);
1668 static PyObject
*kxvikget_nround(PyObject
*me
, void *hunoz
)
1670 kxvik_pyobj
*k
= (kxvik_pyobj
*)me
;
1671 return (PyInt_FromLong(k
->n
));
1674 static int kxvikset_nround(PyObject
*me
, PyObject
*val
, void *hunoz
)
1676 kxvik_pyobj
*k
= (kxvik_pyobj
*)me
;
1679 if (!convuint(val
, &n
)) return (-1);
1684 static PyGetSetDef kxvik_pygetset
[] = {
1685 #define GETSETNAME(op, name) kxvik##op##_##name
1686 GETSET(nround
, "KECCAK.nround -> number of rounds")
1691 static PyMethodDef kxvik_pymethods
[] = {
1692 #define METHNAME(func) kxvikmeth_##func
1693 METH (mix
, "KECCAK.mix(DATA)")
1694 METH (extract
, "KECCAK.extract(NOCTETS)")
1695 METH (step
, "KECCAK.step()")
1700 static PyTypeObject kxvik_pytype_skel
= {
1701 PyObject_HEAD_INIT(0) 0, /* Header */
1702 "Keccak1600", /* @tp_name@ */
1703 sizeof(kxvik_pyobj
), /* @tp_basicsize@ */
1704 0, /* @tp_itemsize@ */
1706 0, /* @tp_dealloc@ */
1708 0, /* @tp_getattr@ */
1709 0, /* @tp_setattr@ */
1710 0, /* @tp_compare@ */
1712 0, /* @tp_as_number@ */
1713 0, /* @tp_as_sequence@ */
1714 0, /* @tp_as_mapping@ */
1718 0, /* @tp_getattro@ */
1719 0, /* @tp_setattro@ */
1720 0, /* @tp_as_buffer@ */
1721 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1722 Py_TPFLAGS_BASETYPE
,
1725 "Keccak-p[1600, n] state.",
1727 0, /* @tp_traverse@ */
1729 0, /* @tp_richcompare@ */
1730 0, /* @tp_weaklistoffset@ */
1732 0, /* @tp_iternext@ */
1733 kxvik_pymethods
, /* @tp_methods@ */
1734 0, /* @tp_members@ */
1735 kxvik_pygetset
, /* @tp_getset@ */
1738 0, /* @tp_descr_get@ */
1739 0, /* @tp_descr_set@ */
1740 0, /* @tp_dictoffset@ */
1742 PyType_GenericAlloc
, /* @tp_alloc@ */
1743 kxvik_pynew
, /* @tp_new@ */
1748 static PyTypeObject
*shake_pytype
, *shake128_pytype
, *shake256_pytype
;
1750 typedef struct shake_pyobj
{
1756 #define SHAKE_H(o) (&((shake_pyobj *)(o))->h)
1757 #define SHAKE_ST(o) (((shake_pyobj *)(o))->st)
1759 static PyObject
*shake_dopynew(void (*initfn
)(shake_ctx
*,
1760 const void *, size_t,
1761 const void *, size_t),
1763 PyObject
*arg
, PyObject
*kw
)
1765 shake_pyobj
*rc
= 0;
1766 char *p
= 0, *f
= 0;
1767 Py_ssize_t psz
= 0, fsz
= 0;
1768 char *kwlist
[] = { "perso", "func", 0 };
1770 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|s#s#:new", kwlist
,
1771 &p
, &psz
, &f
, &fsz
))
1773 rc
= (shake_pyobj
*)ty
->tp_alloc(ty
, 0);
1774 initfn(&rc
->h
, f
, fsz
, p
, psz
);
1777 return ((PyObject
*)rc
);
1780 static PyObject
*shake128_pynew(PyTypeObject
*ty
,
1781 PyObject
*arg
, PyObject
*kw
)
1782 { return (shake_dopynew(cshake128_init
, ty
, arg
, kw
)); }
1784 static PyObject
*shake256_pynew(PyTypeObject
*ty
,
1785 PyObject
*arg
, PyObject
*kw
)
1786 { return (shake_dopynew(cshake256_init
, ty
, arg
, kw
)); }
1788 static int shake_check(PyObject
*me
, int st
)
1790 if (SHAKE_ST(me
) != st
) VALERR("wrong state");
1796 static PyObject
*shakemeth_hash(PyObject
*me
, PyObject
*arg
)
1800 if (!PyArg_ParseTuple(arg
, "s#:hash", &p
, &sz
)) return (0);
1801 if (shake_check(me
, 0)) return (0);
1802 shake_hash(SHAKE_H(me
), p
, sz
);
1806 #define SHAKEMETH_HASHU_(n, W, w) \
1807 static PyObject *shakemeth_hashu##w(PyObject *me, PyObject *arg) \
1811 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \
1812 if (shake_check(me, 0)) goto end; \
1813 STORE##W(b, x); shake_hash(SHAKE_H(me), b, sizeof(b)); \
1818 DOUINTCONV(SHAKEMETH_HASHU_
)
1820 #define SHAKEMETH_HASHBUF_(n, W, w) \
1821 static PyObject *shakemeth_hashbuf##w(PyObject *me, PyObject *arg) \
1826 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
1827 if (sz > MASK##n) TYERR("string too long"); \
1828 if (shake_check(me, 0)) goto end; \
1829 STORE##W(b, sz); shake_hash(SHAKE_H(me), b, sizeof(b)); \
1830 shake_hash(SHAKE_H(me), p, sz); \
1835 DOUINTCONV(SHAKEMETH_HASHBUF_
)
1837 static PyObject
*shakemeth_hashstrz(PyObject
*me
, PyObject
*arg
)
1840 if (!PyArg_ParseTuple(arg
, "s:hashstrz", &p
)) return (0);
1841 if (shake_check(me
, 0)) return (0);
1842 shake_hash(SHAKE_H(me
), p
, strlen(p
) + 1);
1846 static PyObject
*shakemeth_xof(PyObject
*me
, PyObject
*arg
)
1848 if (!PyArg_ParseTuple(arg
, ":xof")) goto end
;
1849 if (shake_check(me
, 0)) goto end
;
1850 shake_xof(SHAKE_H(me
));
1857 static PyObject
*shakemeth_done(PyObject
*me
, PyObject
*arg
)
1861 if (!PyArg_ParseTuple(arg
, "O&:done", convszt
, &n
)) goto end
;
1862 if (shake_check(me
, 0)) goto end
;
1863 rc
= bytestring_pywrap(0, n
);
1864 shake_done(SHAKE_H(me
), PyString_AS_STRING(rc
), n
);
1870 static PyObject
*shakemeth_copy(PyObject
*me
, PyObject
*arg
)
1872 shake_pyobj
*rc
= 0;
1874 if (!PyArg_ParseTuple(arg
, ":copy")) goto end
;
1875 rc
= PyObject_NEW(shake_pyobj
, me
->ob_type
);
1876 rc
->h
= *SHAKE_H(me
);
1877 rc
->st
= SHAKE_ST(me
);
1879 return ((PyObject
*)me
);
1882 static PyObject
*shakemeth_get(PyObject
*me
, PyObject
*arg
)
1887 if (!PyArg_ParseTuple(arg
, "O&:get", convszt
, &sz
)) goto end
;
1888 if (shake_check(me
, 1)) goto end
;
1889 rc
= bytestring_pywrap(0, sz
);
1890 shake_get(SHAKE_H(me
), PyString_AS_STRING(rc
), sz
);
1895 static PyObject
*shakemeth_mask(PyObject
*me
, PyObject
*arg
)
1898 char *p
; Py_ssize_t sz
;
1900 if (!PyArg_ParseTuple(arg
, "s#:mask", &p
, &sz
)) goto end
;
1901 if (shake_check(me
, 1)) goto end
;
1902 rc
= bytestring_pywrap(0, sz
);
1903 shake_mask(SHAKE_H(me
), p
, PyString_AS_STRING(rc
), sz
);
1908 static PyObject
*shakeget_rate(PyObject
*me
, void *hunoz
)
1909 { return (PyInt_FromLong(SHAKE_H(me
)->h
.r
)); }
1911 static PyObject
*shakeget_buffered(PyObject
*me
, void *hunoz
)
1912 { return (PyInt_FromLong(SHAKE_H(me
)->h
.n
)); }
1914 static PyObject
*shakeget_state(PyObject
*me
, void *hunoz
)
1916 int st
= SHAKE_ST(me
);
1917 return (PyString_FromString(st
== 0 ?
"absorb" :
1918 st
== 1 ?
"squeeze" : "dead"));
1921 static PyGetSetDef shake_pygetset
[] = {
1922 #define GETSETNAME(op, name) shake##op##_##name
1923 GET (rate
, "S.rate -> rate, in bytes")
1924 GET (buffered
, "S.buffered -> amount currently buffered")
1925 GET (state
, "S.state -> `absorb', `squeeze', `dead'")
1930 static PyMethodDef shake_pymethods
[] = {
1931 #define METHNAME(func) shakemeth_##func
1932 METH (copy
, "S.copy() -> SS")
1933 METH (hash
, "S.hash(M)")
1934 #define METHU_(n, W, w) METH(hashu##w, "S.hashu" #w "(WORD)")
1937 #define METHBUF_(n, W, w) METH(hashbuf##w, "S.hashbuf" #w "(BYTES)")
1938 DOUINTCONV(METHBUF_
)
1940 METH (hashstrz
, "S.hashstrz(STRING)")
1941 METH (xof
, "S.xof()")
1942 METH (done
, "S.done(LEN) ->H")
1943 METH (get
, "S.get(LEN) -> H")
1944 METH (mask
, "S.mask(M) -> C")
1949 static PyTypeObject shake_pytype_skel
= {
1950 PyObject_HEAD_INIT(0) 0, /* Header */
1951 "Shake", /* @tp_name@ */
1952 sizeof(shake_pyobj
), /* @tp_basicsize@ */
1953 0, /* @tp_itemsize@ */
1955 0, /* @tp_dealloc@ */
1957 0, /* @tp_getattr@ */
1958 0, /* @tp_setattr@ */
1959 0, /* @tp_compare@ */
1961 0, /* @tp_as_number@ */
1962 0, /* @tp_as_sequence@ */
1963 0, /* @tp_as_mapping@ */
1967 0, /* @tp_getattro@ */
1968 0, /* @tp_setattro@ */
1969 0, /* @tp_as_buffer@ */
1970 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1971 Py_TPFLAGS_BASETYPE
,
1974 "SHAKE/cSHAKE base class.",
1976 0, /* @tp_traverse@ */
1978 0, /* @tp_richcompare@ */
1979 0, /* @tp_weaklistoffset@ */
1981 0, /* @tp_iternext@ */
1982 shake_pymethods
, /* @tp_methods@ */
1983 0, /* @tp_members@ */
1984 shake_pygetset
, /* @tp_getset@ */
1987 0, /* @tp_descr_get@ */
1988 0, /* @tp_descr_set@ */
1989 0, /* @tp_dictoffset@ */
1991 PyType_GenericAlloc
, /* @tp_alloc@ */
1992 abstract_pynew
, /* @tp_new@ */
1997 static PyTypeObject shake128_pytype_skel
= {
1998 PyObject_HEAD_INIT(0) 0, /* Header */
1999 "Shake128", /* @tp_name@ */
2000 0, /* @tp_basicsize@ */
2001 0, /* @tp_itemsize@ */
2003 0, /* @tp_dealloc@ */
2005 0, /* @tp_getattr@ */
2006 0, /* @tp_setattr@ */
2007 0, /* @tp_compare@ */
2009 0, /* @tp_as_number@ */
2010 0, /* @tp_as_sequence@ */
2011 0, /* @tp_as_mapping@ */
2015 0, /* @tp_getattro@ */
2016 0, /* @tp_setattro@ */
2017 0, /* @tp_as_buffer@ */
2018 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2019 Py_TPFLAGS_BASETYPE
,
2022 "SHAKE128/cSHAKE128 XOF.",
2024 0, /* @tp_traverse@ */
2026 0, /* @tp_richcompare@ */
2027 0, /* @tp_weaklistoffset@ */
2029 0, /* @tp_iternext@ */
2030 0, /* @tp_methods@ */
2031 0, /* @tp_members@ */
2032 0, /* @tp_getset@ */
2035 0, /* @tp_descr_get@ */
2036 0, /* @tp_descr_set@ */
2037 0, /* @tp_dictoffset@ */
2039 PyType_GenericAlloc
, /* @tp_alloc@ */
2040 shake128_pynew
, /* @tp_new@ */
2045 static PyTypeObject shake256_pytype_skel
= {
2046 PyObject_HEAD_INIT(0) 0, /* Header */
2047 "Shake256", /* @tp_name@ */
2048 0, /* @tp_basicsize@ */
2049 0, /* @tp_itemsize@ */
2051 0, /* @tp_dealloc@ */
2053 0, /* @tp_getattr@ */
2054 0, /* @tp_setattr@ */
2055 0, /* @tp_compare@ */
2057 0, /* @tp_as_number@ */
2058 0, /* @tp_as_sequence@ */
2059 0, /* @tp_as_mapping@ */
2063 0, /* @tp_getattro@ */
2064 0, /* @tp_setattro@ */
2065 0, /* @tp_as_buffer@ */
2066 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2067 Py_TPFLAGS_BASETYPE
,
2070 "SHAKE256/cSHAKE256 XOF.",
2072 0, /* @tp_traverse@ */
2074 0, /* @tp_richcompare@ */
2075 0, /* @tp_weaklistoffset@ */
2077 0, /* @tp_iternext@ */
2078 0, /* @tp_methods@ */
2079 0, /* @tp_members@ */
2080 0, /* @tp_getset@ */
2083 0, /* @tp_descr_get@ */
2084 0, /* @tp_descr_set@ */
2085 0, /* @tp_dictoffset@ */
2087 PyType_GenericAlloc
, /* @tp_alloc@ */
2088 shake256_pynew
, /* @tp_new@ */
2093 /*----- Pseudorandom permutations -----------------------------------------*/
2095 static PyTypeObject
*gcprp_pytype
, *gprp_pytype
;
2097 typedef struct prpinfo
{
2102 void (*init
)(void *, const void *, size_t);
2103 void (*eblk
)(void *, const void *, void *);
2104 void (*dblk
)(void *, const void *, void *);
2107 #define PRP_DEF(PRE, pre) \
2108 static void pre##_prpinit(void *ctx, const void *k, size_t ksz) \
2109 { pre##_init(ctx, k, ksz); } \
2110 static void pre##_prpeblk(void *ctx, const void *in, void *out) \
2112 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
2113 pre##_eblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
2115 static void pre##_prpdblk(void *ctx, const void *in, void *out) \
2117 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
2118 pre##_dblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
2120 static const prpinfo pre##_prpinfo = { \
2121 #pre, pre##_keysz, sizeof(pre##_ctx), PRE##_BLKSZ, \
2122 pre##_prpinit, pre##_prpeblk, pre##_prpdblk \
2126 static const struct prpinfo
*const gprptab
[] = {
2127 #define PRP_ENTRY(PRE, pre) &pre##_prpinfo,
2132 typedef struct gcprp_pyobj
{
2133 PyHeapTypeObject ty
;
2136 #define GCPRP_PRP(o) (((gcprp_pyobj *)(o))->prp)
2138 typedef struct gprp_pyobj
{
2142 #define GPRP_PRP(o) (((gprp_pyobj *)(o))->prp)
2143 #define GPRP_CTX(o) (((gprp_pyobj *)(o)) + 1)
2145 typedef struct prp
{
2150 static PyObject
*gprp_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
2152 char *kwlist
[] = { "key", 0 };
2155 const prpinfo
*prp
= GCPRP_PRP(ty
);
2158 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", kwlist
, &k
, &sz
))
2160 if (keysz(sz
, prp
->keysz
) != sz
) VALERR("bad key length");
2161 me
= (PyObject
*)ty
->tp_alloc(ty
, 0);
2163 prp
->init(GPRP_CTX(me
), k
, sz
);
2170 static void gprp_pydealloc(PyObject
*me
)
2171 { Py_DECREF(me
->ob_type
); FREEOBJ(me
); }
2173 static PyObject
*gcprp_pywrap(const prpinfo
*prp
)
2175 gcprp_pyobj
*g
= newtype(gcprp_pytype
, 0, prp
->name
);
2177 g
->ty
.ht_type
.tp_basicsize
= sizeof(gprp_pyobj
) + prp
->ctxsz
;
2178 g
->ty
.ht_type
.tp_base
= gprp_pytype
;
2179 Py_INCREF(gprp_pytype
);
2180 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
2181 Py_TPFLAGS_BASETYPE
|
2182 Py_TPFLAGS_HEAPTYPE
);
2183 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
2184 g
->ty
.ht_type
.tp_free
= 0;
2185 g
->ty
.ht_type
.tp_new
= gprp_pynew
;
2186 typeready(&g
->ty
.ht_type
);
2187 return ((PyObject
*)g
);
2190 static PyObject
*gcpget_name(PyObject
*me
, void *hunoz
)
2191 { return (PyString_FromString(GCPRP_PRP(me
)->name
)); }
2192 static PyObject
*gcpget_keysz(PyObject
*me
, void *hunoz
)
2193 { return (keysz_pywrap(GCPRP_PRP(me
)->keysz
)); }
2194 static PyObject
*gcpget_blksz(PyObject
*me
, void *hunoz
)
2195 { return (PyInt_FromLong(GCPRP_PRP(me
)->blksz
)); }
2197 static PyObject
*gpmeth_encrypt(PyObject
*me
, PyObject
*arg
)
2203 if (!PyArg_ParseTuple(arg
, "s#:encrypt", &p
, &n
)) goto end
;
2204 if (n
!= GPRP_PRP(me
)->blksz
) VALERR("incorrect block length");
2205 rc
= bytestring_pywrap(0, n
);
2206 GPRP_PRP(me
)->eblk(GPRP_CTX(me
), p
, PyString_AS_STRING(rc
));
2211 static PyObject
*gpmeth_decrypt(PyObject
*me
, PyObject
*arg
)
2217 if (!PyArg_ParseTuple(arg
, "s#:decrypt", &p
, &n
)) goto end
;
2218 if (n
!= GPRP_PRP(me
)->blksz
) VALERR("incorrect block length");
2219 rc
= bytestring_pywrap(0, n
);
2220 GPRP_PRP(me
)->dblk(GPRP_CTX(me
), p
, PyString_AS_STRING(rc
));
2225 static PyGetSetDef gcprp_pygetset
[] = {
2226 #define GETSETNAME(op, name) gcp##op##_##name
2227 GET (keysz
, "CP.keysz -> acceptable key sizes")
2228 GET (blksz
, "CP.blksz -> block size")
2229 GET (name
, "CP.name -> name of this kind of PRP")
2234 static PyMethodDef gprp_pymethods
[] = {
2235 #define METHNAME(name) gpmeth_##name
2236 METH (encrypt
, "P.encrypt(PT) -> CT")
2237 METH (decrypt
, "P.decrypt(CT) -> PT")
2242 static PyTypeObject gcprp_pytype_skel
= {
2243 PyObject_HEAD_INIT(0) 0, /* Header */
2244 "GCPRP", /* @tp_name@ */
2245 sizeof(gcprp_pyobj
), /* @tp_basicsize@ */
2246 0, /* @tp_itemsize@ */
2248 0, /* @tp_dealloc@ */
2250 0, /* @tp_getattr@ */
2251 0, /* @tp_setattr@ */
2252 0, /* @tp_compare@ */
2254 0, /* @tp_as_number@ */
2255 0, /* @tp_as_sequence@ */
2256 0, /* @tp_as_mapping@ */
2260 0, /* @tp_getattro@ */
2261 0, /* @tp_setattro@ */
2262 0, /* @tp_as_buffer@ */
2263 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2264 Py_TPFLAGS_BASETYPE
,
2267 "Pseudorandom permutation metaclass.",
2269 0, /* @tp_traverse@ */
2271 0, /* @tp_richcompare@ */
2272 0, /* @tp_weaklistoffset@ */
2274 0, /* @tp_iternext@ */
2275 0, /* @tp_methods@ */
2276 0, /* @tp_members@ */
2277 gcprp_pygetset
, /* @tp_getset@ */
2280 0, /* @tp_descr_get@ */
2281 0, /* @tp_descr_set@ */
2282 0, /* @tp_dictoffset@ */
2284 PyType_GenericAlloc
, /* @tp_alloc@ */
2285 abstract_pynew
, /* @tp_new@ */
2290 static PyTypeObject gprp_pytype_skel
= {
2291 PyObject_HEAD_INIT(0) 0, /* Header */
2292 "GPRP", /* @tp_name@ */
2293 sizeof(gprp_pyobj
), /* @tp_basicsize@ */
2294 0, /* @tp_itemsize@ */
2296 gprp_pydealloc
, /* @tp_dealloc@ */
2298 0, /* @tp_getattr@ */
2299 0, /* @tp_setattr@ */
2300 0, /* @tp_compare@ */
2302 0, /* @tp_as_number@ */
2303 0, /* @tp_as_sequence@ */
2304 0, /* @tp_as_mapping@ */
2308 0, /* @tp_getattro@ */
2309 0, /* @tp_setattro@ */
2310 0, /* @tp_as_buffer@ */
2311 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2312 Py_TPFLAGS_BASETYPE
,
2315 "Pseudorandom permutation, abstract base class.",
2317 0, /* @tp_traverse@ */
2319 0, /* @tp_richcompare@ */
2320 0, /* @tp_weaklistoffset@ */
2322 0, /* @tp_iternext@ */
2323 gprp_pymethods
, /* @tp_methods@ */
2324 0, /* @tp_members@ */
2325 0, /* @tp_getset@ */
2328 0, /* @tp_descr_get@ */
2329 0, /* @tp_descr_set@ */
2330 0, /* @tp_dictoffset@ */
2332 PyType_GenericAlloc
, /* @tp_alloc@ */
2333 abstract_pynew
, /* @tp_new@ */
2338 /*----- Main code ---------------------------------------------------------*/
2340 static PyMethodDef methods
[] = {
2341 #define METHNAME(func) meth_##func
2342 METH (_KeySZ_fromdl
, "\
2343 fromdl(N) -> M: convert integer discrete log field size to work factor")
2344 METH (_KeySZ_fromschnorr
, "\
2345 fromschnorr(N) -> M: convert Schnorr group order to work factor")
2346 METH (_KeySZ_fromif
, "\
2347 fromif(N) -> M: convert integer factorization problem size to work factor")
2348 METH (_KeySZ_fromec
, "\
2349 fromec(N) -> M: convert elliptic curve group order to work factor")
2350 METH (_KeySZ_todl
, "\
2351 todl(N) -> M: convert work factor to integer discrete log field size")
2352 METH (_KeySZ_toschnorr
, "\
2353 toschnorr(N) -> M: convert work factor to Schnorr group order")
2354 METH (_KeySZ_toif
, "\
2355 toif(N) -> M: convert work factor to integer factorization problem size")
2356 METH (_KeySZ_toec
, "\
2357 toec(N) -> M: convert work factor to elliptic curve group order")
2358 METH (_KeySZ_toec
, "\
2359 toec(N) -> M: convert work factor to elliptic curve group order")
2360 #define METH_HDANCE(hdance, HDance) METH(hdance##_prf, "\
2361 " #hdance "_prf(K, N) -> H: calculate " HDance " hash of N with K")
2362 METH_HDANCE(hsalsa20
, "HSalsa20")
2363 METH_HDANCE(hsalsa2012
, "HSalsa20/12")
2364 METH_HDANCE(hsalsa208
, "HSalsa20/8")
2365 METH_HDANCE(hchacha20
, "HChaCha20")
2366 METH_HDANCE(hchacha12
, "HChaCha12")
2367 METH_HDANCE(hchacha8
, "HChaCha8")
2373 void algorithms_pyinit(void)
2375 INITTYPE(keysz
, root
);
2376 INITTYPE(keyszany
, keysz
);
2377 INITTYPE(keyszrange
, keysz
);
2378 INITTYPE(keyszset
, keysz
);
2379 INITTYPE(gccipher
, type
);
2380 INITTYPE(gcipher
, root
);
2381 INITTYPE(gchash
, type
);
2382 INITTYPE(ghash
, root
);
2383 INITTYPE(gcmac
, type
);
2384 INITTYPE(gmac
, type
);
2385 INITTYPE(gmhash
, ghash
);
2386 INITTYPE(poly1305cls
, type
);
2387 INITTYPE_META(poly1305key
, type
, poly1305cls
);
2388 INITTYPE(poly1305hash
, root
);
2389 INITTYPE(kxvik
, root
);
2390 INITTYPE(shake
, root
);
2391 INITTYPE(shake128
, shake
);
2392 INITTYPE(shake256
, shake
);
2393 INITTYPE(gcprp
, type
);
2394 INITTYPE(gprp
, root
);
2395 addmethods(methods
);
2398 GEN(gcciphers
, cipher
)
2401 #define gcprp prpinfo
2404 void algorithms_pyinsert(PyObject
*mod
)
2407 INSERT("KeySZ", keysz_pytype
);
2408 INSERT("KeySZAny", keyszany_pytype
);
2409 INSERT("KeySZRange", keyszrange_pytype
);
2410 INSERT("KeySZSet", keyszset_pytype
);
2411 INSERT("GCCipher", gccipher_pytype
);
2412 INSERT("GCipher", gcipher_pytype
);
2413 INSERT("gcciphers", gcciphers());
2414 INSERT("GCHash", gchash_pytype
);
2415 INSERT("GHash", ghash_pytype
);
2416 INSERT("gchashes", d
= gchashes());
2417 sha_pyobj
= PyDict_GetItemString(d
, "sha"); Py_INCREF(sha_pyobj
);
2418 has160_pyobj
= PyDict_GetItemString(d
, "has160"); Py_INCREF(has160_pyobj
);
2419 INSERT("GCMAC", gcmac_pytype
);
2420 INSERT("GMAC", gmac_pytype
);
2421 INSERT("GMACHash", gmhash_pytype
);
2422 INSERT("gcmacs", gcmacs());
2423 INSERT("Poly1305Class", poly1305cls_pytype
);
2424 INSERT("poly1305", poly1305key_pytype
);
2425 INSERT("Poly1305Hash", poly1305hash_pytype
);
2426 INSERT("Keccak1600", kxvik_pytype
);
2427 INSERT("Shake", shake_pytype
);
2428 INSERT("Shake128", shake128_pytype
);
2429 INSERT("Shake256", shake256_pytype
);
2430 INSERT("GCPRP", gcprp_pytype
);
2431 INSERT("GPRP", gprp_pytype
);
2432 INSERT("gcprps", gcprps());
2435 /*----- That's all, folks -------------------------------------------------*/