3 * Symmetric cryptography
5 * (c) 2004 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Python interface to Catacomb.
12 * Catacomb/Python is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * Catacomb/Python is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with Catacomb/Python; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
29 #include "catacomb-python.h"
30 #include "algorithms.h"
32 /*----- Key sizes ---------------------------------------------------------*/
34 PyTypeObject
*keysz_pytype
;
35 PyTypeObject
*keyszany_pytype
, *keyszrange_pytype
, *keyszset_pytype
;
36 PyObject
*sha_pyobj
, *has160_pyobj
;
39 # define KSZ_OPMASK 0x1f
43 # define KSZ_16BIT 0x20
46 PyObject
*keysz_pywrap(const octet
*k
)
49 #define ARG(i) (op&KSZ_16BIT ? LOAD16(k + 2*(i)) : k[i])
50 switch (op
&KSZ_OPMASK
) {
52 keysz_pyobj
*o
= PyObject_New(keysz_pyobj
, keyszany_pytype
);
54 return ((PyObject
*)o
);
58 PyObject_New(keyszrange_pyobj
, keyszrange_pytype
);
63 if (!o
->mod
) o
->mod
= 1;
64 return ((PyObject
*)o
);
68 PyObject_New(keyszset_pyobj
, keyszset_pytype
);
71 for (i
= 0; ARG(i
); i
++) ;
72 n
= i
; o
->set
= PyTuple_New(n
);
73 for (i
= 0; i
< n
; i
++)
74 PyTuple_SET_ITEM(o
->set
, i
, PyInt_FromLong(ARG(i
)));
75 return ((PyObject
*)o
);
83 static PyObject
*keyszany_pynew(PyTypeObject
*ty
,
84 PyObject
*arg
, PyObject
*kw
)
86 static const char *const kwlist
[] = { "default", 0 };
90 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "i:new", KWLIST
, &dfl
))
92 if (dfl
< 0) VALERR("key size cannot be negative");
93 o
= (keysz_pyobj
*)ty
->tp_alloc(ty
, 0);
95 return ((PyObject
*)o
);
100 static PyObject
*keyszrange_pynew(PyTypeObject
*ty
,
101 PyObject
*arg
, PyObject
*kw
)
103 static const char *const kwlist
[] = { "default", "min", "max", "mod", 0 };
104 int dfl
, min
= 0, max
= 0, mod
= 1;
107 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "i|iii:new", KWLIST
,
108 &dfl
, &min
, &max
, &mod
))
110 if (dfl
< 0 || min
< 0) VALERR("key size cannot be negative");
111 if (min
> dfl
|| (max
&& dfl
> max
)) VALERR("bad key size bounds");
112 if (mod
<= 0 || dfl
%mod
|| min
%mod
|| max
%mod
)
113 VALERR("bad key size modulus");
114 o
= (keyszrange_pyobj
*)ty
->tp_alloc(ty
, 0);
119 return ((PyObject
*)o
);
124 static PyObject
*keyszset_pynew(PyTypeObject
*ty
,
125 PyObject
*arg
, PyObject
*kw
)
127 static const char *const kwlist
[] = { "default", "set", 0 };
130 PyObject
*x
= 0, *l
= 0;
131 keyszset_pyobj
*o
= 0;
133 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "i|O:new", KWLIST
, &dfl
, &set
))
135 if (!set
) set
= PyTuple_New(0);
137 if (!PySequence_Check(set
)) TYERR("want a sequence");
138 n
= PySequence_Size(set
);
140 if (PyErr_Occurred()) goto end
;
141 if (dfl
< 0) VALERR("key size cannot be negative");
142 x
= PyInt_FromLong(dfl
);
146 for (i
= 0; i
< n
; i
++) {
147 if ((x
= PySequence_GetItem(set
, i
)) == 0) goto end
;
148 xx
= PyInt_AsLong(x
);
149 if (PyErr_Occurred()) goto end
;
150 if (xx
== dfl
) continue;
151 if (xx
< 0) VALERR("key size cannot be negative");
157 if ((set
= PySequence_Tuple(l
)) == 0) goto end
;
158 o
= (keyszset_pyobj
*)ty
->tp_alloc(ty
, 0);
166 return ((PyObject
*)o
);
169 static PyObject
*kaget_min(PyObject
*me
, void *hunoz
)
170 { return (PyInt_FromLong(0)); }
171 #define kaget_max kaget_min
173 static PyObject
*ksget_min(PyObject
*me
, void *hunoz
)
175 PyObject
*set
= ((keyszset_pyobj
*)me
)->set
;
177 n
= PyTuple_Size(set
);
178 for (i
= 0; i
< n
; i
++) {
179 y
= PyInt_AsLong(PyTuple_GetItem(set
, i
));
180 if (x
== -1 || y
< x
) x
= y
;
182 return (PyInt_FromLong(x
));
185 static PyObject
*ksget_max(PyObject
*me
, void *hunoz
)
187 PyObject
*set
= ((keyszset_pyobj
*)me
)->set
;
189 n
= PyTuple_Size(set
);
190 for (i
= 0; i
< n
; i
++) {
191 y
= PyInt_AsLong(PyTuple_GetItem(set
, i
));
194 return (PyInt_FromLong(x
));
197 static PyMemberDef keysz_pymembers
[] = {
198 #define MEMBERSTRUCT keysz_pyobj
199 #define default dfl /* ugh! */
200 MEMBER(default, T_INT
, READONLY
, "KSZ.default -> default key size")
206 static PyGetSetDef keyszany_pygetset
[] = {
207 #define GETSETNAME(op, name) ka##op##_##name
208 GET (min
, "KSZ.min -> smallest allowed key size")
209 GET (max
, "KSZ.min -> largest allowed key size")
214 static PyMemberDef keyszrange_pymembers
[] = {
215 #define MEMBERSTRUCT keyszrange_pyobj
216 MEMBER(min
, T_INT
, READONLY
, "KSZ.min -> smallest allowed key size")
217 MEMBER(max
, T_INT
, READONLY
, "KSZ.min -> largest allowed key size")
218 MEMBER(mod
, T_INT
, READONLY
,
219 "KSZ.mod -> key size must be a multiple of this")
224 static PyGetSetDef keyszset_pygetset
[] = {
225 #define GETSETNAME(op, name) ks##op##_##name
226 GET (min
, "KSZ.min -> smallest allowed key size")
227 GET (max
, "KSZ.min -> largest allowed key size")
232 static PyMemberDef keyszset_pymembers
[] = {
233 #define MEMBERSTRUCT keyszset_pyobj
234 MEMBER(set
, T_OBJECT
, READONLY
, "KSZ.set -> allowed key sizes")
239 static PyTypeObject keysz_pytype_skel
= {
240 PyObject_HEAD_INIT(0) 0, /* Header */
241 "KeySZ", /* @tp_name@ */
242 sizeof(keysz_pyobj
), /* @tp_basicsize@ */
243 0, /* @tp_itemsize@ */
245 0, /* @tp_dealloc@ */
247 0, /* @tp_getattr@ */
248 0, /* @tp_setattr@ */
249 0, /* @tp_compare@ */
251 0, /* @tp_as_number@ */
252 0, /* @tp_as_sequence@ */
253 0, /* @tp_as_mapping@ */
257 0, /* @tp_getattro@ */
258 0, /* @tp_setattro@ */
259 0, /* @tp_as_buffer@ */
260 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
264 "Key size constraints. Abstract.",
266 0, /* @tp_traverse@ */
268 0, /* @tp_richcompare@ */
269 0, /* @tp_weaklistoffset@ */
271 0, /* @tp_iternext@ */
272 0, /* @tp_methods@ */
273 keysz_pymembers
, /* @tp_members@ */
277 0, /* @tp_descr_get@ */
278 0, /* @tp_descr_set@ */
279 0, /* @tp_dictoffset@ */
281 PyType_GenericAlloc
, /* @tp_alloc@ */
282 abstract_pynew
, /* @tp_new@ */
287 static PyTypeObject keyszany_pytype_skel
= {
288 PyObject_HEAD_INIT(0) 0, /* Header */
289 "KeySZAny", /* @tp_name@ */
290 sizeof(keysz_pyobj
), /* @tp_basicsize@ */
291 0, /* @tp_itemsize@ */
293 0, /* @tp_dealloc@ */
295 0, /* @tp_getattr@ */
296 0, /* @tp_setattr@ */
297 0, /* @tp_compare@ */
299 0, /* @tp_as_number@ */
300 0, /* @tp_as_sequence@ */
301 0, /* @tp_as_mapping@ */
305 0, /* @tp_getattro@ */
306 0, /* @tp_setattro@ */
307 0, /* @tp_as_buffer@ */
308 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
312 "KeySZAny(DEFAULT)\n\
313 Key size constraints. This object imposes no constraints on size.",
315 0, /* @tp_traverse@ */
317 0, /* @tp_richcompare@ */
318 0, /* @tp_weaklistoffset@ */
320 0, /* @tp_iternext@ */
321 0, /* @tp_methods@ */
322 0, /* @tp_members@ */
323 keyszany_pygetset
, /* @tp_getset@ */
326 0, /* @tp_descr_get@ */
327 0, /* @tp_descr_set@ */
328 0, /* @tp_dictoffset@ */
330 PyType_GenericAlloc
, /* @tp_alloc@ */
331 keyszany_pynew
, /* @tp_new@ */
336 static PyTypeObject keyszrange_pytype_skel
= {
337 PyObject_HEAD_INIT(0) 0, /* Header */
338 "KeySZRange", /* @tp_name@ */
339 sizeof(keyszrange_pyobj
), /* @tp_basicsize@ */
340 0, /* @tp_itemsize@ */
342 0, /* @tp_dealloc@ */
344 0, /* @tp_getattr@ */
345 0, /* @tp_setattr@ */
346 0, /* @tp_compare@ */
348 0, /* @tp_as_number@ */
349 0, /* @tp_as_sequence@ */
350 0, /* @tp_as_mapping@ */
354 0, /* @tp_getattro@ */
355 0, /* @tp_setattro@ */
356 0, /* @tp_as_buffer@ */
357 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
361 "KeySZRange(DEFAULT, [min = 0], [max = 0], [mod = 1])\n\
362 Key size constraints. Key size must be between MIN and MAX inclusive,\n\
363 and be a multiple of MOD.",
365 0, /* @tp_traverse@ */
367 0, /* @tp_richcompare@ */
368 0, /* @tp_weaklistoffset@ */
370 0, /* @tp_iternext@ */
371 0, /* @tp_methods@ */
372 keyszrange_pymembers
, /* @tp_members@ */
376 0, /* @tp_descr_get@ */
377 0, /* @tp_descr_set@ */
378 0, /* @tp_dictoffset@ */
380 PyType_GenericAlloc
, /* @tp_alloc@ */
381 keyszrange_pynew
, /* @tp_new@ */
386 static PyTypeObject keyszset_pytype_skel
= {
387 PyObject_HEAD_INIT(0) 0, /* Header */
388 "KeySZSet", /* @tp_name@ */
389 sizeof(keyszset_pyobj
), /* @tp_basicsize@ */
390 0, /* @tp_itemsize@ */
392 0, /* @tp_dealloc@ */
394 0, /* @tp_getattr@ */
395 0, /* @tp_setattr@ */
396 0, /* @tp_compare@ */
398 0, /* @tp_as_number@ */
399 0, /* @tp_as_sequence@ */
400 0, /* @tp_as_mapping@ */
404 0, /* @tp_getattro@ */
405 0, /* @tp_setattro@ */
406 0, /* @tp_as_buffer@ */
407 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
411 "KeySZSet(DEFAULT, SEQ)\n\
412 Key size constraints. Key size must be DEFAULT or one in SEQ.",
414 0, /* @tp_traverse@ */
416 0, /* @tp_richcompare@ */
417 0, /* @tp_weaklistoffset@ */
419 0, /* @tp_iternext@ */
420 0, /* @tp_methods@ */
421 keyszset_pymembers
, /* @tp_members@ */
422 keyszset_pygetset
, /* @tp_getset@ */
425 0, /* @tp_descr_get@ */
426 0, /* @tp_descr_set@ */
427 0, /* @tp_dictoffset@ */
429 PyType_GenericAlloc
, /* @tp_alloc@ */
430 keyszset_pynew
, /* @tp_new@ */
435 #define KSZCONVOP(op) \
436 static PyObject *meth__KeySZ_##op(PyObject *me, PyObject *arg) \
439 if (!PyArg_ParseTuple(arg, "Od:" #op, &me, &x)) return (0); \
441 return (PyFloat_FromDouble(y)); \
444 KSZCONVOP(fromschnorr
)
453 /*----- Symmetric encryption ----------------------------------------------*/
455 PyTypeObject
*gccipher_pytype
, *gcipher_pytype
;
457 CONVFUNC(gccipher
, gccipher
*, GCCIPHER_CC
)
458 CONVFUNC(gcipher
, gcipher
*, GCIPHER_C
)
460 PyObject
*gcipher_pywrap(PyObject
*cobj
, gcipher
*c
, 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 static const char *const 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 static const char *const 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)) return (0); \
792 GH_HASHU##W(GHASH_H(me), x); \
795 DOUINTCONV(GHMETH_HASHU_
)
797 #define GHMETH_HASHBUF_(n, W, w) \
798 static PyObject *ghmeth_hashbuf##w(PyObject *me, PyObject *arg) \
802 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
803 if (sz > MASK##n) TYERR("string too long"); \
804 GH_HASHBUF##W(GHASH_H(me), p, sz); \
809 DOUINTCONV(GHMETH_HASHBUF_
)
811 static PyObject
*ghmeth_hashstrz(PyObject
*me
, PyObject
*arg
)
814 if (!PyArg_ParseTuple(arg
, "s:hashstrz", &p
)) return (0);
815 GH_HASHSTRZ(GHASH_H(me
), p
);
819 static PyObject
*ghmeth_done(PyObject
*me
, PyObject
*arg
)
823 if (!PyArg_ParseTuple(arg
, ":done")) return (0);
824 g
= GH_COPY(GHASH_H(me
));
825 rc
= bytestring_pywrap(0, g
->ops
->c
->hashsz
);
826 GH_DONE(g
, PyString_AS_STRING(rc
));
831 static PyGetSetDef gchash_pygetset
[] = {
832 #define GETSETNAME(op, name) gch##op##_##name
833 GET (bufsz
, "CH.bufsz -> hash buffer size, or zero")
834 GET (hashsz
, "CH.hashsz -> hash output size")
835 GET (name
, "CH.name -> name of this kind of hash")
840 static PyMethodDef ghash_pymethods
[] = {
841 #define METHNAME(name) ghmeth_##name
842 METH (hash
, "H.hash(M)")
843 #define METHU_(n, W, w) METH(hashu##w, "H.hashu" #w "(WORD)")
846 #define METHBUF_(n, W, w) METH(hashbuf##w, "H.hashbuf" #w "(BYTES)")
849 METH (hashstrz
, "H.hashstrz(STRING)")
850 METH (done
, "H.done() -> HASH")
855 static PyTypeObject gchash_pytype_skel
= {
856 PyObject_HEAD_INIT(0) 0, /* Header */
857 "GCHash", /* @tp_name@ */
858 sizeof(gchash_pyobj
), /* @tp_basicsize@ */
859 0, /* @tp_itemsize@ */
861 0, /* @tp_dealloc@ */
863 0, /* @tp_getattr@ */
864 0, /* @tp_setattr@ */
865 0, /* @tp_compare@ */
867 0, /* @tp_as_number@ */
868 0, /* @tp_as_sequence@ */
869 0, /* @tp_as_mapping@ */
873 0, /* @tp_getattro@ */
874 0, /* @tp_setattro@ */
875 0, /* @tp_as_buffer@ */
876 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
880 "Hash function metaclass.",
882 0, /* @tp_traverse@ */
884 0, /* @tp_richcompare@ */
885 0, /* @tp_weaklistoffset@ */
887 0, /* @tp_iternext@ */
888 0, /* @tp_methods@ */
889 0, /* @tp_members@ */
890 gchash_pygetset
, /* @tp_getset@ */
893 0, /* @tp_descr_get@ */
894 0, /* @tp_descr_set@ */
895 0, /* @tp_dictoffset@ */
897 PyType_GenericAlloc
, /* @tp_alloc@ */
898 abstract_pynew
, /* @tp_new@ */
903 static PyTypeObject ghash_pytype_skel
= {
904 PyObject_HEAD_INIT(0) 0, /* Header */
905 "GHash", /* @tp_name@ */
906 sizeof(ghash_pyobj
), /* @tp_basicsize@ */
907 0, /* @tp_itemsize@ */
909 ghash_pydealloc
, /* @tp_dealloc@ */
911 0, /* @tp_getattr@ */
912 0, /* @tp_setattr@ */
913 0, /* @tp_compare@ */
915 0, /* @tp_as_number@ */
916 0, /* @tp_as_sequence@ */
917 0, /* @tp_as_mapping@ */
921 0, /* @tp_getattro@ */
922 0, /* @tp_setattro@ */
923 0, /* @tp_as_buffer@ */
924 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
928 "Hash function, abstract base class.",
930 0, /* @tp_traverse@ */
932 0, /* @tp_richcompare@ */
933 0, /* @tp_weaklistoffset@ */
935 0, /* @tp_iternext@ */
936 ghash_pymethods
, /* @tp_methods@ */
937 0, /* @tp_members@ */
941 0, /* @tp_descr_get@ */
942 0, /* @tp_descr_set@ */
943 0, /* @tp_dictoffset@ */
945 PyType_GenericAlloc
, /* @tp_alloc@ */
946 abstract_pynew
, /* @tp_new@ */
951 /*----- Message authentication --------------------------------------------*/
953 PyTypeObject
*gcmac_pytype
, *gmac_pytype
, *gmhash_pytype
;
955 CONVFUNC(gcmac
, gcmac
*, GCMAC_CM
)
956 CONVFUNC(gmac
, gmac
*, GMAC_M
)
957 CONVFUNC(gmhash
, ghash
*, GHASH_H
)
959 static PyObject
*gmac_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
961 static const char *const kwlist
[] = { "k", 0 };
965 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", KWLIST
, &k
, &sz
))
967 if (keysz(sz
, GCMAC_CM(ty
)->keysz
) != sz
) VALERR("bad key length");
968 return (gmac_pywrap((PyObject
*)ty
,
969 GM_KEY(GCMAC_CM(ty
), k
, sz
),
975 static PyObject
*gmhash_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
977 static const char *const kwlist
[] = { 0 };
980 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, ":new", KWLIST
)) return (0);
981 g
= PyObject_NEW(ghash_pyobj
, ty
);
982 g
->h
= GM_INIT(GMAC_M(ty
));
985 return ((PyObject
*)g
);
988 PyObject
*gcmac_pywrap(gcmac
*cm
)
990 gcmac_pyobj
*g
= newtype(gcmac_pytype
, 0, cm
->name
);
992 g
->ty
.ht_type
.tp_basicsize
= sizeof(gmac_pyobj
);
993 g
->ty
.ht_type
.tp_base
= gmac_pytype
;
994 Py_INCREF(gmac_pytype
);
995 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
996 Py_TPFLAGS_BASETYPE
|
997 Py_TPFLAGS_HEAPTYPE
);
998 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
999 g
->ty
.ht_type
.tp_free
= 0;
1000 g
->ty
.ht_type
.tp_new
= gmac_pynew
;
1001 typeready(&g
->ty
.ht_type
);
1002 return ((PyObject
*)g
);
1005 PyObject
*gmac_pywrap(PyObject
*cobj
, gmac
*m
, unsigned f
)
1008 if (!cobj
) cobj
= gcmac_pywrap((/*unconst*/ gcmac
*)GM_CLASS(m
));
1009 else Py_INCREF(cobj
);
1010 g
= newtype((PyTypeObject
*)cobj
, 0, 0);
1011 g
->ty
.ht_type
.tp_basicsize
= sizeof(ghash_pyobj
);
1012 g
->ty
.ht_name
= PyString_FromFormat("%s(keyed)", m
->ops
->c
->name
);
1013 g
->ty
.ht_type
.tp_name
= PyString_AS_STRING(g
->ty
.ht_name
);
1014 g
->ty
.ht_type
.tp_base
= gmhash_pytype
;
1015 Py_INCREF(gmac_pytype
);
1016 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
1017 Py_TPFLAGS_BASETYPE
|
1018 Py_TPFLAGS_HEAPTYPE
);
1019 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
1020 g
->ty
.ht_type
.tp_free
= 0;
1021 g
->ty
.ht_type
.tp_new
= gmhash_pynew
;
1022 typeready(&g
->ty
.ht_type
);
1025 return ((PyObject
*)g
);
1028 static void gmac_pydealloc(PyObject
*me
)
1030 if (GMAC_F(me
) & f_freeme
)
1031 GM_DESTROY(GMAC_M(me
));
1032 Py_DECREF(me
->ob_type
);
1033 PyType_Type
.tp_dealloc(me
);
1036 static PyObject
*gcmget_name(PyObject
*me
, void *hunoz
)
1037 { return (PyString_FromString(GCMAC_CM(me
)->name
)); }
1039 static PyObject
*gcmget_keysz(PyObject
*me
, void *hunoz
)
1040 { return (keysz_pywrap(GCMAC_CM(me
)->keysz
)); }
1042 static PyObject
*gcmget_tagsz(PyObject
*me
, void *hunoz
)
1043 { return (PyInt_FromLong(GCMAC_CM(me
)->hashsz
)); }
1045 static PyGetSetDef gcmac_pygetset
[] = {
1046 #define GETSETNAME(op, name) gcm##op##_##name
1047 GET (keysz
, "CM.keysz -> acceptable key sizes")
1048 GET (tagsz
, "CM.tagsz -> MAC output size")
1049 GET (name
, "CM.name -> name of this kind of MAC")
1054 static PyTypeObject gcmac_pytype_skel
= {
1055 PyObject_HEAD_INIT(0) 0, /* Header */
1056 "GCMAC", /* @tp_name@ */
1057 sizeof(gchash_pyobj
), /* @tp_basicsize@ */
1058 0, /* @tp_itemsize@ */
1060 0, /* @tp_dealloc@ */
1062 0, /* @tp_getattr@ */
1063 0, /* @tp_setattr@ */
1064 0, /* @tp_compare@ */
1066 0, /* @tp_as_number@ */
1067 0, /* @tp_as_sequence@ */
1068 0, /* @tp_as_mapping@ */
1072 0, /* @tp_getattro@ */
1073 0, /* @tp_setattro@ */
1074 0, /* @tp_as_buffer@ */
1075 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1076 Py_TPFLAGS_BASETYPE
,
1079 "Message authentication code metametaclass.",
1081 0, /* @tp_traverse@ */
1083 0, /* @tp_richcompare@ */
1084 0, /* @tp_weaklistoffset@ */
1086 0, /* @tp_iternext@ */
1087 0, /* @tp_methods@ */
1088 0, /* @tp_members@ */
1089 gcmac_pygetset
, /* @tp_getset@ */
1092 0, /* @tp_descr_get@ */
1093 0, /* @tp_descr_set@ */
1094 0, /* @tp_dictoffset@ */
1096 PyType_GenericAlloc
, /* @tp_alloc@ */
1097 abstract_pynew
, /* @tp_new@ */
1102 static PyTypeObject gmac_pytype_skel
= {
1103 PyObject_HEAD_INIT(0) 0, /* Header */
1104 "GMAC", /* @tp_name@ */
1105 sizeof(gmac_pyobj
), /* @tp_basicsize@ */
1106 0, /* @tp_itemsize@ */
1108 gmac_pydealloc
, /* @tp_dealloc@ */
1110 0, /* @tp_getattr@ */
1111 0, /* @tp_setattr@ */
1112 0, /* @tp_compare@ */
1114 0, /* @tp_as_number@ */
1115 0, /* @tp_as_sequence@ */
1116 0, /* @tp_as_mapping@ */
1120 0, /* @tp_getattro@ */
1121 0, /* @tp_setattro@ */
1122 0, /* @tp_as_buffer@ */
1123 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1124 Py_TPFLAGS_BASETYPE
,
1127 "Message authentication code metaclass, abstract base class.",
1129 0, /* @tp_traverse@ */
1131 0, /* @tp_richcompare@ */
1132 0, /* @tp_weaklistoffset@ */
1134 0, /* @tp_iternext@ */
1135 0, /* @tp_methods@ */
1136 0, /* @tp_members@ */
1137 0, /* @tp_getset@ */
1140 0, /* @tp_descr_get@ */
1141 0, /* @tp_descr_set@ */
1142 0, /* @tp_dictoffset@ */
1144 PyType_GenericAlloc
, /* @tp_alloc@ */
1145 abstract_pynew
, /* @tp_new@ */
1150 static PyTypeObject gmhash_pytype_skel
= {
1151 PyObject_HEAD_INIT(0) 0, /* Header */
1152 "GMACHash", /* @tp_name@ */
1153 sizeof(ghash_pyobj
), /* @tp_basicsize@ */
1154 0, /* @tp_itemsize@ */
1156 ghash_pydealloc
, /* @tp_dealloc@ */
1158 0, /* @tp_getattr@ */
1159 0, /* @tp_setattr@ */
1160 0, /* @tp_compare@ */
1162 0, /* @tp_as_number@ */
1163 0, /* @tp_as_sequence@ */
1164 0, /* @tp_as_mapping@ */
1168 0, /* @tp_getattro@ */
1169 0, /* @tp_setattro@ */
1170 0, /* @tp_as_buffer@ */
1171 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1172 Py_TPFLAGS_BASETYPE
,
1175 "Message authentication code, abstract base class.",
1177 0, /* @tp_traverse@ */
1179 0, /* @tp_richcompare@ */
1180 0, /* @tp_weaklistoffset@ */
1182 0, /* @tp_iternext@ */
1183 0, /* @tp_methods@ */
1184 0, /* @tp_members@ */
1185 0, /* @tp_getset@ */
1188 0, /* @tp_descr_get@ */
1189 0, /* @tp_descr_set@ */
1190 0, /* @tp_dictoffset@ */
1192 PyType_GenericAlloc
, /* @tp_alloc@ */
1193 abstract_pynew
, /* @tp_new@ */
1198 /*----- Special snowflake for Poly1305 ------------------------------------*/
1200 PyTypeObject
*poly1305cls_pytype
, *poly1305key_pytype
, *poly1305hash_pytype
;
1202 typedef struct poly1305key_pyobj
{
1203 PyHeapTypeObject ty
;
1205 } poly1305key_pyobj
;
1207 typedef struct poly1305hash_pyobj
{
1212 } poly1305hash_pyobj
;
1214 #define P1305_F(o) (((poly1305hash_pyobj *)(o))->f)
1215 #define P1305_CTX(o) (&((poly1305hash_pyobj *)(o))->ctx)
1216 CONVFUNC(poly1305hash
, poly1305_ctx
*, P1305_CTX
)
1218 static PyObject
*poly1305hash_pynew(PyTypeObject
*ty
,
1219 PyObject
*arg
, PyObject
*kw
)
1221 static const char *const kwlist
[] = { "mask", 0 };
1222 poly1305key_pyobj
*pk
= (poly1305key_pyobj
*)ty
;
1223 poly1305hash_pyobj
*ph
;
1227 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|s#:new", KWLIST
, &m
, &sz
))
1229 if (m
&& sz
!= POLY1305_MASKSZ
) VALERR("bad mask length");
1230 ph
= PyObject_NEW(poly1305hash_pyobj
, ty
);
1232 if (m
) ph
->f
|= f_mask
;
1233 poly1305_macinit(&ph
->ctx
, &pk
->k
, m
);
1235 return ((PyObject
*)ph
);
1240 static PyObject
*poly1305key_pynew(PyTypeObject
*ty
,
1241 PyObject
*arg
, PyObject
*kw
)
1243 static const char *const kwlist
[] = { "k", 0 };
1244 poly1305key_pyobj
*pk
;
1248 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", KWLIST
, &k
, &sz
))
1250 if (keysz(sz
, poly1305_keysz
) != sz
) VALERR("bad key length");
1252 pk
= newtype(ty
, 0, 0);
1253 pk
->ty
.ht_name
= PyString_FromString("poly1305(keyed)");
1254 pk
->ty
.ht_type
.tp_basicsize
= sizeof(poly1305hash_pyobj
);
1255 pk
->ty
.ht_type
.tp_name
= PyString_AS_STRING(pk
->ty
.ht_name
);
1256 pk
->ty
.ht_type
.tp_base
= poly1305hash_pytype
;
1257 Py_INCREF(poly1305key_pytype
);
1258 pk
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
1259 Py_TPFLAGS_BASETYPE
|
1260 Py_TPFLAGS_HEAPTYPE
);
1261 pk
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
1262 pk
->ty
.ht_type
.tp_free
= 0;
1263 pk
->ty
.ht_type
.tp_new
= poly1305hash_pynew
;
1264 typeready(&pk
->ty
.ht_type
);
1266 poly1305_keyinit(&pk
->k
, k
, sz
);
1267 return ((PyObject
*)pk
);
1273 static PyObject
*poly1305clsget_name(PyObject
*me
, void *hunoz
)
1274 { return (PyString_FromString("poly1305")); }
1276 static PyObject
*poly1305clsget_keysz(PyObject
*me
, void *hunoz
)
1277 { return (keysz_pywrap(poly1305_keysz
)); }
1279 static PyObject
*poly1305clsget_masksz(PyObject
*me
, void *hunoz
)
1280 { return (PyInt_FromLong(POLY1305_MASKSZ
)); }
1282 static PyObject
*poly1305clsget_tagsz(PyObject
*me
, void *hunoz
)
1283 { return (PyInt_FromLong(POLY1305_TAGSZ
)); }
1285 static PyObject
*polymeth_copy(PyObject
*me
, PyObject
*arg
)
1287 poly1305hash_pyobj
*ph
;
1288 if (!PyArg_ParseTuple(arg
, ":copy")) return (0);
1289 ph
= PyObject_NEW(poly1305hash_pyobj
, me
->ob_type
);
1290 poly1305_copy(&ph
->ctx
, P1305_CTX(me
));
1291 Py_INCREF(me
->ob_type
);
1292 return ((PyObject
*)ph
);
1295 static PyObject
*polymeth_hash(PyObject
*me
, PyObject
*arg
)
1299 if (!PyArg_ParseTuple(arg
, "s#:hash", &p
, &sz
)) return (0);
1300 poly1305_hash(P1305_CTX(me
), p
, sz
);
1304 #define POLYMETH_HASHU_(n, W, w) \
1305 static PyObject *polymeth_hashu##w(PyObject *me, PyObject *arg) \
1309 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \
1310 STORE##W(b, x); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \
1313 DOUINTCONV(POLYMETH_HASHU_
)
1315 #define POLYMETH_HASHBUF_(n, W, w) \
1316 static PyObject *polymeth_hashbuf##w(PyObject *me, PyObject *arg) \
1321 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
1322 if (sz > MASK##n) TYERR("string too long"); \
1323 STORE##W(b, sz); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \
1324 poly1305_hash(P1305_CTX(me), p, sz); \
1329 DOUINTCONV(POLYMETH_HASHBUF_
)
1331 static PyObject
*polymeth_hashstrz(PyObject
*me
, PyObject
*arg
)
1334 if (!PyArg_ParseTuple(arg
, "s:hashstrz", &p
)) return (0);
1335 poly1305_hash(P1305_CTX(me
), p
, strlen(p
) + 1);
1339 static PyObject
*polymeth_flush(PyObject
*me
, PyObject
*arg
)
1341 if (!PyArg_ParseTuple(arg
, ":flush")) return (0);
1342 poly1305_flush(P1305_CTX(me
));
1346 static PyObject
*polymeth_flushzero(PyObject
*me
, PyObject
*arg
)
1348 if (!PyArg_ParseTuple(arg
, ":flushzero")) return (0);
1349 poly1305_flushzero(P1305_CTX(me
));
1353 static PyObject
*polymeth_concat(PyObject
*me
, PyObject
*arg
)
1355 PyObject
*pre
, *suff
;
1356 if (!PyArg_ParseTuple(arg
, "OO:concat", &pre
, &suff
)) return (0);
1357 if (!PyObject_TypeCheck(pre
, poly1305hash_pytype
) ||
1358 !PyObject_TypeCheck(suff
, poly1305hash_pytype
))
1359 TYERR("wanted a poly1305hash");
1360 if (me
->ob_type
!= pre
->ob_type
|| me
->ob_type
!= suff
->ob_type
)
1361 TYERR("key mismatch");
1362 if (P1305_CTX(pre
)->nbuf
) VALERR("prefix is not block-aligned");
1363 poly1305_concat(P1305_CTX(me
), P1305_CTX(pre
), P1305_CTX(suff
));
1369 static PyObject
*polymeth_done(PyObject
*me
, PyObject
*arg
)
1372 if (!PyArg_ParseTuple(arg
, ":done")) return (0);
1373 if (!(P1305_F(me
) & f_mask
)) VALERR("no mask");
1374 rc
= bytestring_pywrap(0, POLY1305_TAGSZ
);
1375 poly1305_done(P1305_CTX(me
), PyString_AS_STRING(rc
));
1381 static PyGetSetDef poly1305cls_pygetset
[] = {
1382 #define GETSETNAME(op, name) poly1305cls##op##_##name
1383 GET (keysz
, "PC.keysz -> acceptable key sizes")
1384 GET (masksz
, "PC.masksz -> mask size")
1385 GET (tagsz
, "PC.tagsz -> MAC output size")
1386 GET (name
, "PC.name -> name of this kind of MAC")
1391 static PyMethodDef poly1305hash_pymethods
[] = {
1392 #define METHNAME(name) polymeth_##name
1393 METH (copy
, "P.copy() -> PP")
1394 METH (hash
, "P.hash(M)")
1395 #define METHU_(n, W, w) METH(hashu##w, "P.hashu" #w "(WORD)")
1398 #define METHBUF_(n, W, w) METH(hashbuf##w, "P.hashbuf" #w "(BYTES)")
1399 DOUINTCONV(METHBUF_
)
1401 METH (hashstrz
, "P.hashstrz(STRING)")
1402 METH (flush
, "P.flush()")
1403 METH (flushzero
, "P.flushzero()")
1404 METH (concat
, "P.concat(PREFIX, SUFFIX)")
1405 METH (done
, "P.done() -> TAG")
1410 static PyTypeObject poly1305cls_pytype_skel
= {
1411 PyObject_HEAD_INIT(0) 0, /* Header */
1412 "Poly1305Class", /* @tp_name@ */
1413 sizeof(PyHeapTypeObject
), /* @tp_basicsize@ */
1414 0, /* @tp_itemsize@ */
1416 0, /* @tp_dealloc@ */
1418 0, /* @tp_getattr@ */
1419 0, /* @tp_setattr@ */
1420 0, /* @tp_compare@ */
1422 0, /* @tp_as_number@ */
1423 0, /* @tp_as_sequence@ */
1424 0, /* @tp_as_mapping@ */
1428 0, /* @tp_getattro@ */
1429 0, /* @tp_setattro@ */
1430 0, /* @tp_as_buffer@ */
1431 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1432 Py_TPFLAGS_BASETYPE
,
1435 "Poly1305 metametaclass. Best not to ask.",
1437 0, /* @tp_traverse@ */
1439 0, /* @tp_richcompare@ */
1440 0, /* @tp_weaklistoffset@ */
1442 0, /* @tp_iternext@ */
1443 0, /* @tp_methods@ */
1444 0, /* @tp_members@ */
1445 poly1305cls_pygetset
, /* @tp_getset@ */
1448 0, /* @tp_descr_get@ */
1449 0, /* @tp_descr_set@ */
1450 0, /* @tp_dictoffset@ */
1452 PyType_GenericAlloc
, /* @tp_alloc@ */
1453 abstract_pynew
, /* @tp_new@ */
1458 static PyTypeObject poly1305key_pytype_skel
= {
1459 PyObject_HEAD_INIT(0) 0, /* Header */
1460 "poly1305", /* @tp_name@ */
1461 sizeof(poly1305key_pyobj
), /* @tp_basicsize@ */
1462 0, /* @tp_itemsize@ */
1464 0, /* @tp_dealloc@ */
1466 0, /* @tp_getattr@ */
1467 0, /* @tp_setattr@ */
1468 0, /* @tp_compare@ */
1470 0, /* @tp_as_number@ */
1471 0, /* @tp_as_sequence@ */
1472 0, /* @tp_as_mapping@ */
1476 0, /* @tp_getattro@ */
1477 0, /* @tp_setattro@ */
1478 0, /* @tp_as_buffer@ */
1479 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1480 Py_TPFLAGS_BASETYPE
,
1483 "poly1305(K): Poly1305 key.",
1485 0, /* @tp_traverse@ */
1487 0, /* @tp_richcompare@ */
1488 0, /* @tp_weaklistoffset@ */
1490 0, /* @tp_iternext@ */
1491 0, /* @tp_methods@ */
1492 0, /* @tp_members@ */
1493 0, /* @tp_getset@ */
1496 0, /* @tp_descr_get@ */
1497 0, /* @tp_descr_set@ */
1498 0, /* @tp_dictoffset@ */
1500 PyType_GenericAlloc
, /* @tp_alloc@ */
1501 poly1305key_pynew
, /* @tp_new@ */
1506 static PyTypeObject poly1305hash_pytype_skel
= {
1507 PyObject_HEAD_INIT(0) 0, /* Header */
1508 "Poly1305Hash", /* @tp_name@ */
1509 sizeof(poly1305hash_pyobj
), /* @tp_basicsize@ */
1510 0, /* @tp_itemsize@ */
1512 0, /* @tp_dealloc@ */
1514 0, /* @tp_getattr@ */
1515 0, /* @tp_setattr@ */
1516 0, /* @tp_compare@ */
1518 0, /* @tp_as_number@ */
1519 0, /* @tp_as_sequence@ */
1520 0, /* @tp_as_mapping@ */
1524 0, /* @tp_getattro@ */
1525 0, /* @tp_setattro@ */
1526 0, /* @tp_as_buffer@ */
1527 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1528 Py_TPFLAGS_BASETYPE
,
1531 "Poly1305 MAC context base class.",
1533 0, /* @tp_traverse@ */
1535 0, /* @tp_richcompare@ */
1536 0, /* @tp_weaklistoffset@ */
1538 0, /* @tp_iternext@ */
1539 poly1305hash_pymethods
, /* @tp_methods@ */
1540 0, /* @tp_members@ */
1541 0, /* @tp_getset@ */
1544 0, /* @tp_descr_get@ */
1545 0, /* @tp_descr_set@ */
1546 0, /* @tp_dictoffset@ */
1548 PyType_GenericAlloc
, /* @tp_alloc@ */
1549 abstract_pynew
, /* @tp_new@ */
1554 /*----- Special snowflake for HSalsa and HChaCha --------------------------*/
1556 #define DEF_HDANCE(DANCE, HDANCE, dance, hdance) \
1557 static PyObject *meth_##hdance##_prf(PyObject *me, PyObject *arg) \
1559 dance##_ctx dance; \
1561 Py_ssize_t ksz, nsz; \
1563 if (!PyArg_ParseTuple(arg, "s#s#:" #hdance "_prf", \
1564 &k, &ksz, &n, &nsz)) \
1566 if (ksz != DANCE##_KEYSZ) VALERR("bad key length"); \
1567 if (nsz != HDANCE##_INSZ) VALERR("bad input length"); \
1568 rc = bytestring_pywrap(0, HSALSA20_OUTSZ); \
1569 dance##_init(&dance, k, ksz, 0); \
1570 hdance##_prf(&dance, n, PyString_AS_STRING(rc)); \
1576 DEF_HDANCE(SALSA20
, HSALSA20
, salsa20
, hsalsa20
)
1577 DEF_HDANCE(SALSA20
, HSALSA20
, salsa20
, hsalsa2012
)
1578 DEF_HDANCE(SALSA20
, HSALSA20
, salsa20
, hsalsa208
)
1580 DEF_HDANCE(CHACHA
, HCHACHA
, chacha
, hchacha20
)
1581 DEF_HDANCE(CHACHA
, HCHACHA
, chacha
, hchacha12
)
1582 DEF_HDANCE(CHACHA
, HCHACHA
, chacha
, hchacha8
)
1584 /*----- Keccak-p[1600, n] -------------------------------------------------*/
1586 static PyTypeObject
*kxvik_pytype
;
1588 typedef struct kxvik_pyobj
{
1594 static PyObject
*kxvik_pynew(PyTypeObject
*ty
,
1595 PyObject
*arg
, PyObject
*kw
)
1598 kxvik_pyobj
*rc
= 0;
1599 static const char *const kwlist
[] = { "nround", 0 };
1600 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:new", KWLIST
,
1603 rc
= (kxvik_pyobj
*)ty
->tp_alloc(ty
, 0);
1605 keccak1600_init(&rc
->s
);
1607 return ((PyObject
*)rc
);
1610 static PyObject
*kxvikmeth_mix(PyObject
*me
, PyObject
*arg
)
1612 kxvik_pyobj
*k
= (kxvik_pyobj
*)me
;
1617 char *p
; Py_ssize_t n
;
1619 if (!PyArg_ParseTuple(arg
, "s#:mix", &p
, &n
)) goto end
;
1620 if (n
> 200) VALERR("out of range");
1621 q
= (const octet
*)p
;
1623 while (n
> 8) { LOAD64_L_(t
[i
], q
); i
++; q
+= 8; n
-= 8; }
1625 memcpy(buf
, q
, n
); memset(buf
+ n
, 0, 8 - n
);
1626 LOAD64_L_(t
[i
], buf
); i
++;
1628 keccak1600_mix(&k
->s
, t
, i
);
1634 static PyObject
*kxvikmeth_extract(PyObject
*me
, PyObject
*arg
)
1636 kxvik_pyobj
*k
= (kxvik_pyobj
*)me
;
1643 if (!PyArg_ParseTuple(arg
, "O&:mix", convuint
, &n
)) goto end
;
1644 if (n
> 200) VALERR("out of range");
1645 rc
= bytestring_pywrap(0, n
);
1646 q
= (octet
*)PyString_AS_STRING(rc
);
1647 keccak1600_extract(&k
->s
, t
, (n
+ 7)/8);
1649 while (n
> 8) { STORE64_L_(q
, t
[i
]); i
++; q
+= 8; n
-= 8; }
1650 if (n
) { STORE64_L_(buf
, t
[i
]); memcpy(q
, buf
, n
); }
1655 static PyObject
*kxvikmeth_step(PyObject
*me
, PyObject
*arg
)
1657 kxvik_pyobj
*k
= (kxvik_pyobj
*)me
;
1658 if (!PyArg_ParseTuple(arg
, ":step")) return (0);
1659 keccak1600_p(&k
->s
, &k
->s
, k
->n
);
1663 static PyObject
*kxvikget_nround(PyObject
*me
, void *hunoz
)
1665 kxvik_pyobj
*k
= (kxvik_pyobj
*)me
;
1666 return (PyInt_FromLong(k
->n
));
1669 static int kxvikset_nround(PyObject
*me
, PyObject
*val
, void *hunoz
)
1671 kxvik_pyobj
*k
= (kxvik_pyobj
*)me
;
1674 if (!convuint(val
, &n
)) return (-1);
1679 static PyGetSetDef kxvik_pygetset
[] = {
1680 #define GETSETNAME(op, name) kxvik##op##_##name
1681 GETSET(nround
, "KECCAK.nround -> number of rounds")
1686 static PyMethodDef kxvik_pymethods
[] = {
1687 #define METHNAME(func) kxvikmeth_##func
1688 METH (mix
, "KECCAK.mix(DATA)")
1689 METH (extract
, "KECCAK.extract(NOCTETS)")
1690 METH (step
, "KECCAK.step()")
1695 static PyTypeObject kxvik_pytype_skel
= {
1696 PyObject_HEAD_INIT(0) 0, /* Header */
1697 "Keccak1600", /* @tp_name@ */
1698 sizeof(kxvik_pyobj
), /* @tp_basicsize@ */
1699 0, /* @tp_itemsize@ */
1701 0, /* @tp_dealloc@ */
1703 0, /* @tp_getattr@ */
1704 0, /* @tp_setattr@ */
1705 0, /* @tp_compare@ */
1707 0, /* @tp_as_number@ */
1708 0, /* @tp_as_sequence@ */
1709 0, /* @tp_as_mapping@ */
1713 0, /* @tp_getattro@ */
1714 0, /* @tp_setattro@ */
1715 0, /* @tp_as_buffer@ */
1716 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1717 Py_TPFLAGS_BASETYPE
,
1720 "Keccak1600([nround = 24]): Keccak-p[1600, n] state.",
1722 0, /* @tp_traverse@ */
1724 0, /* @tp_richcompare@ */
1725 0, /* @tp_weaklistoffset@ */
1727 0, /* @tp_iternext@ */
1728 kxvik_pymethods
, /* @tp_methods@ */
1729 0, /* @tp_members@ */
1730 kxvik_pygetset
, /* @tp_getset@ */
1733 0, /* @tp_descr_get@ */
1734 0, /* @tp_descr_set@ */
1735 0, /* @tp_dictoffset@ */
1737 PyType_GenericAlloc
, /* @tp_alloc@ */
1738 kxvik_pynew
, /* @tp_new@ */
1743 static PyTypeObject
*shake_pytype
, *shake128_pytype
, *shake256_pytype
;
1745 typedef struct shake_pyobj
{
1751 #define SHAKE_H(o) (&((shake_pyobj *)(o))->h)
1752 #define SHAKE_ST(o) (((shake_pyobj *)(o))->st)
1754 static PyObject
*shake_dopynew(void (*initfn
)(shake_ctx
*,
1755 const void *, size_t,
1756 const void *, size_t),
1758 PyObject
*arg
, PyObject
*kw
)
1760 shake_pyobj
*rc
= 0;
1761 char *p
= 0, *f
= 0;
1762 Py_ssize_t psz
= 0, fsz
= 0;
1763 static const char *const kwlist
[] = { "perso", "func", 0 };
1765 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|s#s#:new", KWLIST
,
1766 &p
, &psz
, &f
, &fsz
))
1768 rc
= (shake_pyobj
*)ty
->tp_alloc(ty
, 0);
1769 initfn(&rc
->h
, f
, fsz
, p
, psz
);
1772 return ((PyObject
*)rc
);
1775 static PyObject
*shake128_pynew(PyTypeObject
*ty
,
1776 PyObject
*arg
, PyObject
*kw
)
1777 { return (shake_dopynew(cshake128_init
, ty
, arg
, kw
)); }
1779 static PyObject
*shake256_pynew(PyTypeObject
*ty
,
1780 PyObject
*arg
, PyObject
*kw
)
1781 { return (shake_dopynew(cshake256_init
, ty
, arg
, kw
)); }
1783 static int shake_check(PyObject
*me
, int st
)
1785 if (SHAKE_ST(me
) != st
) VALERR("wrong state");
1791 static PyObject
*shakemeth_hash(PyObject
*me
, PyObject
*arg
)
1795 if (!PyArg_ParseTuple(arg
, "s#:hash", &p
, &sz
)) return (0);
1796 if (shake_check(me
, 0)) return (0);
1797 shake_hash(SHAKE_H(me
), p
, sz
);
1801 #define SHAKEMETH_HASHU_(n, W, w) \
1802 static PyObject *shakemeth_hashu##w(PyObject *me, PyObject *arg) \
1806 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \
1807 if (shake_check(me, 0)) return (0); \
1808 STORE##W(b, x); shake_hash(SHAKE_H(me), b, sizeof(b)); \
1811 DOUINTCONV(SHAKEMETH_HASHU_
)
1813 #define SHAKEMETH_HASHBUF_(n, W, w) \
1814 static PyObject *shakemeth_hashbuf##w(PyObject *me, PyObject *arg) \
1819 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
1820 if (sz > MASK##n) TYERR("string too long"); \
1821 if (shake_check(me, 0)) goto end; \
1822 STORE##W(b, sz); shake_hash(SHAKE_H(me), b, sizeof(b)); \
1823 shake_hash(SHAKE_H(me), p, sz); \
1828 DOUINTCONV(SHAKEMETH_HASHBUF_
)
1830 static PyObject
*shakemeth_hashstrz(PyObject
*me
, PyObject
*arg
)
1833 if (!PyArg_ParseTuple(arg
, "s:hashstrz", &p
)) return (0);
1834 if (shake_check(me
, 0)) return (0);
1835 shake_hash(SHAKE_H(me
), p
, strlen(p
) + 1);
1839 static PyObject
*shakemeth_xof(PyObject
*me
, PyObject
*arg
)
1841 if (!PyArg_ParseTuple(arg
, ":xof")) goto end
;
1842 if (shake_check(me
, 0)) goto end
;
1843 shake_xof(SHAKE_H(me
));
1850 static PyObject
*shakemeth_done(PyObject
*me
, PyObject
*arg
)
1854 if (!PyArg_ParseTuple(arg
, "O&:done", convszt
, &n
)) goto end
;
1855 if (shake_check(me
, 0)) goto end
;
1856 rc
= bytestring_pywrap(0, n
);
1857 shake_done(SHAKE_H(me
), PyString_AS_STRING(rc
), n
);
1863 static PyObject
*shakemeth_copy(PyObject
*me
, PyObject
*arg
)
1865 shake_pyobj
*rc
= 0;
1867 if (!PyArg_ParseTuple(arg
, ":copy")) goto end
;
1868 rc
= PyObject_NEW(shake_pyobj
, me
->ob_type
);
1869 rc
->h
= *SHAKE_H(me
);
1870 rc
->st
= SHAKE_ST(me
);
1872 return ((PyObject
*)me
);
1875 static PyObject
*shakemeth_get(PyObject
*me
, PyObject
*arg
)
1880 if (!PyArg_ParseTuple(arg
, "O&:get", convszt
, &sz
)) goto end
;
1881 if (shake_check(me
, 1)) goto end
;
1882 rc
= bytestring_pywrap(0, sz
);
1883 shake_get(SHAKE_H(me
), PyString_AS_STRING(rc
), sz
);
1888 static PyObject
*shakemeth_mask(PyObject
*me
, PyObject
*arg
)
1891 char *p
; Py_ssize_t sz
;
1893 if (!PyArg_ParseTuple(arg
, "s#:mask", &p
, &sz
)) goto end
;
1894 if (shake_check(me
, 1)) goto end
;
1895 rc
= bytestring_pywrap(0, sz
);
1896 shake_mask(SHAKE_H(me
), p
, PyString_AS_STRING(rc
), sz
);
1901 static PyObject
*shakeget_rate(PyObject
*me
, void *hunoz
)
1902 { return (PyInt_FromLong(SHAKE_H(me
)->h
.r
)); }
1904 static PyObject
*shakeget_buffered(PyObject
*me
, void *hunoz
)
1905 { return (PyInt_FromLong(SHAKE_H(me
)->h
.n
)); }
1907 static PyObject
*shakeget_state(PyObject
*me
, void *hunoz
)
1909 int st
= SHAKE_ST(me
);
1910 return (PyString_FromString(st
== 0 ?
"absorb" :
1911 st
== 1 ?
"squeeze" : "dead"));
1914 static PyGetSetDef shake_pygetset
[] = {
1915 #define GETSETNAME(op, name) shake##op##_##name
1916 GET (rate
, "S.rate -> rate, in bytes")
1917 GET (buffered
, "S.buffered -> amount currently buffered")
1918 GET (state
, "S.state -> `absorb', `squeeze', `dead'")
1923 static PyMethodDef shake_pymethods
[] = {
1924 #define METHNAME(func) shakemeth_##func
1925 METH (copy
, "S.copy() -> SS")
1926 METH (hash
, "S.hash(M)")
1927 #define METHU_(n, W, w) METH(hashu##w, "S.hashu" #w "(WORD)")
1930 #define METHBUF_(n, W, w) METH(hashbuf##w, "S.hashbuf" #w "(BYTES)")
1931 DOUINTCONV(METHBUF_
)
1933 METH (hashstrz
, "S.hashstrz(STRING)")
1934 METH (xof
, "S.xof()")
1935 METH (done
, "S.done(LEN) ->H")
1936 METH (get
, "S.get(LEN) -> H")
1937 METH (mask
, "S.mask(M) -> C")
1942 static PyTypeObject shake_pytype_skel
= {
1943 PyObject_HEAD_INIT(0) 0, /* Header */
1944 "Shake", /* @tp_name@ */
1945 sizeof(shake_pyobj
), /* @tp_basicsize@ */
1946 0, /* @tp_itemsize@ */
1948 0, /* @tp_dealloc@ */
1950 0, /* @tp_getattr@ */
1951 0, /* @tp_setattr@ */
1952 0, /* @tp_compare@ */
1954 0, /* @tp_as_number@ */
1955 0, /* @tp_as_sequence@ */
1956 0, /* @tp_as_mapping@ */
1960 0, /* @tp_getattro@ */
1961 0, /* @tp_setattro@ */
1962 0, /* @tp_as_buffer@ */
1963 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1964 Py_TPFLAGS_BASETYPE
,
1967 "SHAKE/cSHAKE base class.",
1969 0, /* @tp_traverse@ */
1971 0, /* @tp_richcompare@ */
1972 0, /* @tp_weaklistoffset@ */
1974 0, /* @tp_iternext@ */
1975 shake_pymethods
, /* @tp_methods@ */
1976 0, /* @tp_members@ */
1977 shake_pygetset
, /* @tp_getset@ */
1980 0, /* @tp_descr_get@ */
1981 0, /* @tp_descr_set@ */
1982 0, /* @tp_dictoffset@ */
1984 PyType_GenericAlloc
, /* @tp_alloc@ */
1985 abstract_pynew
, /* @tp_new@ */
1990 static PyTypeObject shake128_pytype_skel
= {
1991 PyObject_HEAD_INIT(0) 0, /* Header */
1992 "Shake128", /* @tp_name@ */
1993 0, /* @tp_basicsize@ */
1994 0, /* @tp_itemsize@ */
1996 0, /* @tp_dealloc@ */
1998 0, /* @tp_getattr@ */
1999 0, /* @tp_setattr@ */
2000 0, /* @tp_compare@ */
2002 0, /* @tp_as_number@ */
2003 0, /* @tp_as_sequence@ */
2004 0, /* @tp_as_mapping@ */
2008 0, /* @tp_getattro@ */
2009 0, /* @tp_setattro@ */
2010 0, /* @tp_as_buffer@ */
2011 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2012 Py_TPFLAGS_BASETYPE
,
2015 "Shake128([perso = STR], [func = STR]): SHAKE128/cSHAKE128 XOF.",
2017 0, /* @tp_traverse@ */
2019 0, /* @tp_richcompare@ */
2020 0, /* @tp_weaklistoffset@ */
2022 0, /* @tp_iternext@ */
2023 0, /* @tp_methods@ */
2024 0, /* @tp_members@ */
2025 0, /* @tp_getset@ */
2028 0, /* @tp_descr_get@ */
2029 0, /* @tp_descr_set@ */
2030 0, /* @tp_dictoffset@ */
2032 PyType_GenericAlloc
, /* @tp_alloc@ */
2033 shake128_pynew
, /* @tp_new@ */
2038 static PyTypeObject shake256_pytype_skel
= {
2039 PyObject_HEAD_INIT(0) 0, /* Header */
2040 "Shake256", /* @tp_name@ */
2041 0, /* @tp_basicsize@ */
2042 0, /* @tp_itemsize@ */
2044 0, /* @tp_dealloc@ */
2046 0, /* @tp_getattr@ */
2047 0, /* @tp_setattr@ */
2048 0, /* @tp_compare@ */
2050 0, /* @tp_as_number@ */
2051 0, /* @tp_as_sequence@ */
2052 0, /* @tp_as_mapping@ */
2056 0, /* @tp_getattro@ */
2057 0, /* @tp_setattro@ */
2058 0, /* @tp_as_buffer@ */
2059 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2060 Py_TPFLAGS_BASETYPE
,
2063 "Shake256([perso = STR], [func = STR]): SHAKE256/cSHAKE256 XOF.",
2065 0, /* @tp_traverse@ */
2067 0, /* @tp_richcompare@ */
2068 0, /* @tp_weaklistoffset@ */
2070 0, /* @tp_iternext@ */
2071 0, /* @tp_methods@ */
2072 0, /* @tp_members@ */
2073 0, /* @tp_getset@ */
2076 0, /* @tp_descr_get@ */
2077 0, /* @tp_descr_set@ */
2078 0, /* @tp_dictoffset@ */
2080 PyType_GenericAlloc
, /* @tp_alloc@ */
2081 shake256_pynew
, /* @tp_new@ */
2086 /*----- Pseudorandom permutations -----------------------------------------*/
2088 static PyTypeObject
*gcprp_pytype
, *gprp_pytype
;
2090 typedef struct prpinfo
{
2095 void (*init
)(void *, const void *, size_t);
2096 void (*eblk
)(void *, const void *, void *);
2097 void (*dblk
)(void *, const void *, void *);
2100 #define PRP_DEF(PRE, pre) \
2101 static void pre##_prpinit(void *ctx, const void *k, size_t ksz) \
2102 { pre##_init(ctx, k, ksz); } \
2103 static void pre##_prpeblk(void *ctx, const void *in, void *out) \
2105 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
2106 pre##_eblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
2108 static void pre##_prpdblk(void *ctx, const void *in, void *out) \
2110 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
2111 pre##_dblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
2113 static const prpinfo pre##_prpinfo = { \
2114 #pre, pre##_keysz, sizeof(pre##_ctx), PRE##_BLKSZ, \
2115 pre##_prpinit, pre##_prpeblk, pre##_prpdblk \
2119 static const struct prpinfo
*const gprptab
[] = {
2120 #define PRP_ENTRY(PRE, pre) &pre##_prpinfo,
2125 typedef struct gcprp_pyobj
{
2126 PyHeapTypeObject ty
;
2129 #define GCPRP_PRP(o) (((gcprp_pyobj *)(o))->prp)
2131 typedef struct gprp_pyobj
{
2135 #define GPRP_PRP(o) (((gprp_pyobj *)(o))->prp)
2136 #define GPRP_CTX(o) (((gprp_pyobj *)(o)) + 1)
2138 typedef struct prp
{
2143 static PyObject
*gprp_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
2145 static const char *const kwlist
[] = { "key", 0 };
2148 const prpinfo
*prp
= GCPRP_PRP(ty
);
2151 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", KWLIST
, &k
, &sz
))
2153 if (keysz(sz
, prp
->keysz
) != sz
) VALERR("bad key length");
2154 me
= (PyObject
*)ty
->tp_alloc(ty
, 0);
2156 prp
->init(GPRP_CTX(me
), k
, sz
);
2163 static void gprp_pydealloc(PyObject
*me
)
2164 { Py_DECREF(me
->ob_type
); FREEOBJ(me
); }
2166 static PyObject
*gcprp_pywrap(const prpinfo
*prp
)
2168 gcprp_pyobj
*g
= newtype(gcprp_pytype
, 0, prp
->name
);
2170 g
->ty
.ht_type
.tp_basicsize
= sizeof(gprp_pyobj
) + prp
->ctxsz
;
2171 g
->ty
.ht_type
.tp_base
= gprp_pytype
;
2172 Py_INCREF(gprp_pytype
);
2173 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
2174 Py_TPFLAGS_BASETYPE
|
2175 Py_TPFLAGS_HEAPTYPE
);
2176 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
2177 g
->ty
.ht_type
.tp_free
= 0;
2178 g
->ty
.ht_type
.tp_new
= gprp_pynew
;
2179 typeready(&g
->ty
.ht_type
);
2180 return ((PyObject
*)g
);
2183 static PyObject
*gcpget_name(PyObject
*me
, void *hunoz
)
2184 { return (PyString_FromString(GCPRP_PRP(me
)->name
)); }
2185 static PyObject
*gcpget_keysz(PyObject
*me
, void *hunoz
)
2186 { return (keysz_pywrap(GCPRP_PRP(me
)->keysz
)); }
2187 static PyObject
*gcpget_blksz(PyObject
*me
, void *hunoz
)
2188 { return (PyInt_FromLong(GCPRP_PRP(me
)->blksz
)); }
2190 static PyObject
*gpmeth_encrypt(PyObject
*me
, PyObject
*arg
)
2196 if (!PyArg_ParseTuple(arg
, "s#:encrypt", &p
, &n
)) goto end
;
2197 if (n
!= GPRP_PRP(me
)->blksz
) VALERR("incorrect block length");
2198 rc
= bytestring_pywrap(0, n
);
2199 GPRP_PRP(me
)->eblk(GPRP_CTX(me
), p
, PyString_AS_STRING(rc
));
2204 static PyObject
*gpmeth_decrypt(PyObject
*me
, PyObject
*arg
)
2210 if (!PyArg_ParseTuple(arg
, "s#:decrypt", &p
, &n
)) goto end
;
2211 if (n
!= GPRP_PRP(me
)->blksz
) VALERR("incorrect block length");
2212 rc
= bytestring_pywrap(0, n
);
2213 GPRP_PRP(me
)->dblk(GPRP_CTX(me
), p
, PyString_AS_STRING(rc
));
2218 static PyGetSetDef gcprp_pygetset
[] = {
2219 #define GETSETNAME(op, name) gcp##op##_##name
2220 GET (keysz
, "CP.keysz -> acceptable key sizes")
2221 GET (blksz
, "CP.blksz -> block size")
2222 GET (name
, "CP.name -> name of this kind of PRP")
2227 static PyMethodDef gprp_pymethods
[] = {
2228 #define METHNAME(name) gpmeth_##name
2229 METH (encrypt
, "P.encrypt(PT) -> CT")
2230 METH (decrypt
, "P.decrypt(CT) -> PT")
2235 static PyTypeObject gcprp_pytype_skel
= {
2236 PyObject_HEAD_INIT(0) 0, /* Header */
2237 "GCPRP", /* @tp_name@ */
2238 sizeof(gcprp_pyobj
), /* @tp_basicsize@ */
2239 0, /* @tp_itemsize@ */
2241 0, /* @tp_dealloc@ */
2243 0, /* @tp_getattr@ */
2244 0, /* @tp_setattr@ */
2245 0, /* @tp_compare@ */
2247 0, /* @tp_as_number@ */
2248 0, /* @tp_as_sequence@ */
2249 0, /* @tp_as_mapping@ */
2253 0, /* @tp_getattro@ */
2254 0, /* @tp_setattro@ */
2255 0, /* @tp_as_buffer@ */
2256 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2257 Py_TPFLAGS_BASETYPE
,
2260 "Pseudorandom permutation metaclass.",
2262 0, /* @tp_traverse@ */
2264 0, /* @tp_richcompare@ */
2265 0, /* @tp_weaklistoffset@ */
2267 0, /* @tp_iternext@ */
2268 0, /* @tp_methods@ */
2269 0, /* @tp_members@ */
2270 gcprp_pygetset
, /* @tp_getset@ */
2273 0, /* @tp_descr_get@ */
2274 0, /* @tp_descr_set@ */
2275 0, /* @tp_dictoffset@ */
2277 PyType_GenericAlloc
, /* @tp_alloc@ */
2278 abstract_pynew
, /* @tp_new@ */
2283 static PyTypeObject gprp_pytype_skel
= {
2284 PyObject_HEAD_INIT(0) 0, /* Header */
2285 "GPRP", /* @tp_name@ */
2286 sizeof(gprp_pyobj
), /* @tp_basicsize@ */
2287 0, /* @tp_itemsize@ */
2289 gprp_pydealloc
, /* @tp_dealloc@ */
2291 0, /* @tp_getattr@ */
2292 0, /* @tp_setattr@ */
2293 0, /* @tp_compare@ */
2295 0, /* @tp_as_number@ */
2296 0, /* @tp_as_sequence@ */
2297 0, /* @tp_as_mapping@ */
2301 0, /* @tp_getattro@ */
2302 0, /* @tp_setattro@ */
2303 0, /* @tp_as_buffer@ */
2304 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
2305 Py_TPFLAGS_BASETYPE
,
2308 "Pseudorandom permutation, abstract base class.",
2310 0, /* @tp_traverse@ */
2312 0, /* @tp_richcompare@ */
2313 0, /* @tp_weaklistoffset@ */
2315 0, /* @tp_iternext@ */
2316 gprp_pymethods
, /* @tp_methods@ */
2317 0, /* @tp_members@ */
2318 0, /* @tp_getset@ */
2321 0, /* @tp_descr_get@ */
2322 0, /* @tp_descr_set@ */
2323 0, /* @tp_dictoffset@ */
2325 PyType_GenericAlloc
, /* @tp_alloc@ */
2326 abstract_pynew
, /* @tp_new@ */
2331 /*----- Main code ---------------------------------------------------------*/
2333 static PyMethodDef methods
[] = {
2334 #define METHNAME(func) meth_##func
2335 METH (_KeySZ_fromdl
, "\
2336 fromdl(N) -> M: convert integer discrete log field size to work factor")
2337 METH (_KeySZ_fromschnorr
, "\
2338 fromschnorr(N) -> M: convert Schnorr group order to work factor")
2339 METH (_KeySZ_fromif
, "\
2340 fromif(N) -> M: convert integer factorization problem size to work factor")
2341 METH (_KeySZ_fromec
, "\
2342 fromec(N) -> M: convert elliptic curve group order to work factor")
2343 METH (_KeySZ_todl
, "\
2344 todl(N) -> M: convert work factor to integer discrete log field size")
2345 METH (_KeySZ_toschnorr
, "\
2346 toschnorr(N) -> M: convert work factor to Schnorr group order")
2347 METH (_KeySZ_toif
, "\
2348 toif(N) -> M: convert work factor to integer factorization problem size")
2349 METH (_KeySZ_toec
, "\
2350 toec(N) -> M: convert work factor to elliptic curve group order")
2351 METH (_KeySZ_toec
, "\
2352 toec(N) -> M: convert work factor to elliptic curve group order")
2353 #define METH_HDANCE(hdance, HDance) METH(hdance##_prf, "\
2354 " #hdance "_prf(K, N) -> H: calculate " HDance " hash of N with K")
2355 METH_HDANCE(hsalsa20
, "HSalsa20")
2356 METH_HDANCE(hsalsa2012
, "HSalsa20/12")
2357 METH_HDANCE(hsalsa208
, "HSalsa20/8")
2358 METH_HDANCE(hchacha20
, "HChaCha20")
2359 METH_HDANCE(hchacha12
, "HChaCha12")
2360 METH_HDANCE(hchacha8
, "HChaCha8")
2366 void algorithms_pyinit(void)
2368 INITTYPE(keysz
, root
);
2369 INITTYPE(keyszany
, keysz
);
2370 INITTYPE(keyszrange
, keysz
);
2371 INITTYPE(keyszset
, keysz
);
2372 INITTYPE(gccipher
, type
);
2373 INITTYPE(gcipher
, root
);
2374 INITTYPE(gchash
, type
);
2375 INITTYPE(ghash
, root
);
2376 INITTYPE(gcmac
, type
);
2377 INITTYPE(gmac
, type
);
2378 INITTYPE(gmhash
, ghash
);
2379 INITTYPE(poly1305cls
, type
);
2380 INITTYPE_META(poly1305key
, type
, poly1305cls
);
2381 INITTYPE(poly1305hash
, root
);
2382 INITTYPE(kxvik
, root
);
2383 INITTYPE(shake
, root
);
2384 INITTYPE(shake128
, shake
);
2385 INITTYPE(shake256
, shake
);
2386 INITTYPE(gcprp
, type
);
2387 INITTYPE(gprp
, root
);
2388 addmethods(methods
);
2391 GEN(gcciphers
, cipher
)
2394 #define gcprp prpinfo
2397 void algorithms_pyinsert(PyObject
*mod
)
2400 INSERT("KeySZ", keysz_pytype
);
2401 INSERT("KeySZAny", keyszany_pytype
);
2402 INSERT("KeySZRange", keyszrange_pytype
);
2403 INSERT("KeySZSet", keyszset_pytype
);
2404 INSERT("GCCipher", gccipher_pytype
);
2405 INSERT("GCipher", gcipher_pytype
);
2406 INSERT("gcciphers", gcciphers());
2407 INSERT("GCHash", gchash_pytype
);
2408 INSERT("GHash", ghash_pytype
);
2409 INSERT("gchashes", d
= gchashes());
2410 sha_pyobj
= PyDict_GetItemString(d
, "sha"); Py_INCREF(sha_pyobj
);
2411 has160_pyobj
= PyDict_GetItemString(d
, "has160"); Py_INCREF(has160_pyobj
);
2412 INSERT("GCMAC", gcmac_pytype
);
2413 INSERT("GMAC", gmac_pytype
);
2414 INSERT("GMACHash", gmhash_pytype
);
2415 INSERT("gcmacs", gcmacs());
2416 INSERT("Poly1305Class", poly1305cls_pytype
);
2417 INSERT("poly1305", poly1305key_pytype
);
2418 INSERT("Poly1305Hash", poly1305hash_pytype
);
2419 INSERT("Keccak1600", kxvik_pytype
);
2420 INSERT("Shake", shake_pytype
);
2421 INSERT("Shake128", shake128_pytype
);
2422 INSERT("Shake256", shake256_pytype
);
2423 INSERT("GCPRP", gcprp_pytype
);
2424 INSERT("GPRP", gprp_pytype
);
2425 INSERT("gcprps", gcprps());
2428 /*----- That's all, folks -------------------------------------------------*/