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
;
38 PyObject
*keysz_pywrap(const octet
*k
)
42 keysz_pyobj
*o
= PyObject_New(keysz_pyobj
, keyszany_pytype
);
44 return ((PyObject
*)o
);
48 PyObject_New(keyszrange_pyobj
, keyszrange_pytype
);
53 if (!o
->mod
) o
->mod
= 1;
54 return ((PyObject
*)o
);
58 PyObject_New(keyszset_pyobj
, keyszset_pytype
);
61 for (i
= 0; k
[i
+ 1]; i
++) ;
62 n
= i
; o
->set
= PyTuple_New(n
);
63 for (i
= 0; i
< n
; i
++)
64 PyTuple_SET_ITEM(o
->set
, i
, PyInt_FromLong(k
[i
+ 1]));
65 return ((PyObject
*)o
);
72 static PyObject
*keyszany_pynew(PyTypeObject
*ty
,
73 PyObject
*arg
, PyObject
*kw
)
75 char *kwlist
[] = { "default", 0 };
79 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "i:new", kwlist
, &dfl
))
81 if (dfl
< 0) VALERR("key size cannot be negative");
82 o
= (keysz_pyobj
*)ty
->tp_alloc(ty
, 0);
84 return ((PyObject
*)o
);
89 static PyObject
*keyszrange_pynew(PyTypeObject
*ty
,
90 PyObject
*arg
, PyObject
*kw
)
92 char *kwlist
[] = { "default", "min", "max", "mod", 0 };
93 int dfl
, min
= 0, max
= 0, mod
= 1;
96 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "i|iii:new", kwlist
,
97 &dfl
, &min
, &max
, &mod
))
99 if (dfl
< 0 || min
< 0 || max
< 0)
100 VALERR("key size cannot be negative");
101 if (min
> dfl
|| (max
&& dfl
> max
))
102 VALERR("bad key size bounds");
103 if (mod
<= 0 || dfl
% mod
|| min
% mod
|| max
% mod
)
104 VALERR("bad key size modulus");
105 o
= (keyszrange_pyobj
*)ty
->tp_alloc(ty
, 0);
110 return ((PyObject
*)o
);
115 static PyObject
*keyszset_pynew(PyTypeObject
*ty
,
116 PyObject
*arg
, PyObject
*kw
)
118 char *kwlist
[] = { "default", "set", 0 };
121 PyObject
*x
= 0, *l
= 0;
122 keyszset_pyobj
*o
= 0;
124 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "i|O:new", kwlist
,
127 if (!set
) set
= PyTuple_New(0);
129 if (!PySequence_Check(set
)) TYERR("want a sequence");
130 n
= PySequence_Size(set
);
132 if (PyErr_Occurred()) goto end
;
133 if (dfl
< 0) VALERR("key size cannot be negative");
134 x
= PyInt_FromLong(dfl
);
138 for (i
= 0; i
< n
; i
++) {
139 if ((x
= PySequence_GetItem(set
, i
)) == 0) goto end
;
140 xx
= PyInt_AsLong(x
);
141 if (PyErr_Occurred()) goto end
;
142 if (xx
== dfl
) continue;
143 if (xx
< 0) VALERR("key size cannot be negative");
149 if ((set
= PySequence_Tuple(l
)) == 0) goto end
;
150 o
= (keyszset_pyobj
*)ty
->tp_alloc(ty
, 0);
158 return ((PyObject
*)o
);
161 static PyObject
*kaget_min(PyObject
*me
, void *hunoz
)
162 { return (PyInt_FromLong(0)); }
163 #define kaget_max kaget_min
165 static PyObject
*ksget_min(PyObject
*me
, void *hunoz
)
167 PyObject
*set
= ((keyszset_pyobj
*)me
)->set
;
169 n
= PyTuple_Size(set
);
170 for (i
= 0; i
< n
; i
++) {
171 y
= PyInt_AsLong(PyTuple_GetItem(set
, i
));
172 if (x
== -1 || y
< x
) x
= y
;
174 return (PyInt_FromLong(x
));
177 static PyObject
*ksget_max(PyObject
*me
, void *hunoz
)
179 PyObject
*set
= ((keyszset_pyobj
*)me
)->set
;
181 n
= PyTuple_Size(set
);
182 for (i
= 0; i
< n
; i
++) {
183 y
= PyInt_AsLong(PyTuple_GetItem(set
, i
));
186 return (PyInt_FromLong(x
));
189 static PyMemberDef keysz_pymembers
[] = {
190 #define MEMBERSTRUCT keysz_pyobj
191 #define default dfl /* ugh! */
192 MEMBER(default, T_INT
, READONLY
, "KSZ.default -> default key size")
198 static PyGetSetDef keyszany_pygetset
[] = {
199 #define GETSETNAME(op, name) ka##op##_##name
200 GET (min
, "KSZ.min -> smallest allowed key size")
201 GET (max
, "KSZ.min -> largest allowed key size")
206 static PyMemberDef keyszrange_pymembers
[] = {
207 #define MEMBERSTRUCT keyszrange_pyobj
208 MEMBER(min
, T_INT
, READONLY
, "KSZ.min -> smallest allowed key size")
209 MEMBER(max
, T_INT
, READONLY
, "KSZ.min -> largest allowed key size")
210 MEMBER(mod
, T_INT
, READONLY
,
211 "KSZ.mod -> key size must be a multiple of this")
216 static PyGetSetDef keyszset_pygetset
[] = {
217 #define GETSETNAME(op, name) ks##op##_##name
218 GET (min
, "KSZ.min -> smallest allowed key size")
219 GET (max
, "KSZ.min -> largest allowed key size")
224 static PyMemberDef keyszset_pymembers
[] = {
225 #define MEMBERSTRUCT keyszset_pyobj
226 MEMBER(set
, T_OBJECT
, READONLY
, "KSZ.set -> allowed key sizes")
231 static PyTypeObject keysz_pytype_skel
= {
232 PyObject_HEAD_INIT(0) 0, /* Header */
233 "KeySZ", /* @tp_name@ */
234 sizeof(keysz_pyobj
), /* @tp_basicsize@ */
235 0, /* @tp_itemsize@ */
237 0, /* @tp_dealloc@ */
239 0, /* @tp_getattr@ */
240 0, /* @tp_setattr@ */
241 0, /* @tp_compare@ */
243 0, /* @tp_as_number@ */
244 0, /* @tp_as_sequence@ */
245 0, /* @tp_as_mapping@ */
249 0, /* @tp_getattro@ */
250 0, /* @tp_setattro@ */
251 0, /* @tp_as_buffer@ */
252 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
256 "Key size constraints.",
258 0, /* @tp_traverse@ */
260 0, /* @tp_richcompare@ */
261 0, /* @tp_weaklistoffset@ */
263 0, /* @tp_iternext@ */
264 0, /* @tp_methods@ */
265 keysz_pymembers
, /* @tp_members@ */
269 0, /* @tp_descr_get@ */
270 0, /* @tp_descr_set@ */
271 0, /* @tp_dictoffset@ */
273 PyType_GenericAlloc
, /* @tp_alloc@ */
274 abstract_pynew
, /* @tp_new@ */
279 static PyTypeObject keyszany_pytype_skel
= {
280 PyObject_HEAD_INIT(0) 0, /* Header */
281 "KeySZAny", /* @tp_name@ */
282 sizeof(keysz_pyobj
), /* @tp_basicsize@ */
283 0, /* @tp_itemsize@ */
285 0, /* @tp_dealloc@ */
287 0, /* @tp_getattr@ */
288 0, /* @tp_setattr@ */
289 0, /* @tp_compare@ */
291 0, /* @tp_as_number@ */
292 0, /* @tp_as_sequence@ */
293 0, /* @tp_as_mapping@ */
297 0, /* @tp_getattro@ */
298 0, /* @tp_setattro@ */
299 0, /* @tp_as_buffer@ */
300 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
304 "Key size constraints. This object imposes no constraints on size.",
306 0, /* @tp_traverse@ */
308 0, /* @tp_richcompare@ */
309 0, /* @tp_weaklistoffset@ */
311 0, /* @tp_iternext@ */
312 0, /* @tp_methods@ */
313 0, /* @tp_members@ */
314 keyszany_pygetset
, /* @tp_getset@ */
317 0, /* @tp_descr_get@ */
318 0, /* @tp_descr_set@ */
319 0, /* @tp_dictoffset@ */
321 PyType_GenericAlloc
, /* @tp_alloc@ */
322 keyszany_pynew
, /* @tp_new@ */
327 static PyTypeObject keyszrange_pytype_skel
= {
328 PyObject_HEAD_INIT(0) 0, /* Header */
329 "KeySZRange", /* @tp_name@ */
330 sizeof(keyszrange_pyobj
), /* @tp_basicsize@ */
331 0, /* @tp_itemsize@ */
333 0, /* @tp_dealloc@ */
335 0, /* @tp_getattr@ */
336 0, /* @tp_setattr@ */
337 0, /* @tp_compare@ */
339 0, /* @tp_as_number@ */
340 0, /* @tp_as_sequence@ */
341 0, /* @tp_as_mapping@ */
345 0, /* @tp_getattro@ */
346 0, /* @tp_setattro@ */
347 0, /* @tp_as_buffer@ */
348 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
352 "Key size constraints. This object asserts minimum and maximum (if\n\
353 sizes, and requires the key length to be a multiple of some value.",
355 0, /* @tp_traverse@ */
357 0, /* @tp_richcompare@ */
358 0, /* @tp_weaklistoffset@ */
360 0, /* @tp_iternext@ */
361 0, /* @tp_methods@ */
362 keyszrange_pymembers
, /* @tp_members@ */
366 0, /* @tp_descr_get@ */
367 0, /* @tp_descr_set@ */
368 0, /* @tp_dictoffset@ */
370 PyType_GenericAlloc
, /* @tp_alloc@ */
371 keyszrange_pynew
, /* @tp_new@ */
376 static PyTypeObject keyszset_pytype_skel
= {
377 PyObject_HEAD_INIT(0) 0, /* Header */
378 "KeySZSet", /* @tp_name@ */
379 sizeof(keyszset_pyobj
), /* @tp_basicsize@ */
380 0, /* @tp_itemsize@ */
382 0, /* @tp_dealloc@ */
384 0, /* @tp_getattr@ */
385 0, /* @tp_setattr@ */
386 0, /* @tp_compare@ */
388 0, /* @tp_as_number@ */
389 0, /* @tp_as_sequence@ */
390 0, /* @tp_as_mapping@ */
394 0, /* @tp_getattro@ */
395 0, /* @tp_setattro@ */
396 0, /* @tp_as_buffer@ */
397 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
401 "Key size constraints. This object requires the key to be one of a\n\
404 0, /* @tp_traverse@ */
406 0, /* @tp_richcompare@ */
407 0, /* @tp_weaklistoffset@ */
409 0, /* @tp_iternext@ */
410 0, /* @tp_methods@ */
411 keyszset_pymembers
, /* @tp_members@ */
412 keyszset_pygetset
, /* @tp_getset@ */
415 0, /* @tp_descr_get@ */
416 0, /* @tp_descr_set@ */
417 0, /* @tp_dictoffset@ */
419 PyType_GenericAlloc
, /* @tp_alloc@ */
420 keyszset_pynew
, /* @tp_new@ */
425 #define KSZCONVOP(op) \
426 static PyObject *meth__KeySZ_##op(PyObject *me, PyObject *arg) \
429 if (!PyArg_ParseTuple(arg, "Od:" #op, &me, &x)) return (0); \
431 return (PyFloat_FromDouble(y)); \
434 KSZCONVOP(fromschnorr
)
443 /*----- Symmetric encryption ----------------------------------------------*/
445 PyTypeObject
*gccipher_pytype
, *gcipher_pytype
;
447 CONVFUNC(gccipher
, gccipher
*, GCCIPHER_CC
)
448 CONVFUNC(gcipher
, gcipher
*, GCIPHER_C
)
450 PyObject
*gcipher_pywrap(PyObject
*cobj
, gcipher
*c
, unsigned f
)
453 if (!cobj
) cobj
= gccipher_pywrap((/*unconst*/ gccipher
*)GC_CLASS(c
));
454 else Py_INCREF(cobj
);
455 g
= PyObject_NEW(gcipher_pyobj
, (PyTypeObject
*)cobj
);
458 return ((PyObject
*)g
);
461 static PyObject
*gcipher_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
463 char *kwlist
[] = { "k", 0 };
467 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", kwlist
, &k
, &sz
))
469 if (keysz(sz
, GCCIPHER_CC(ty
)->keysz
) != sz
) VALERR("bad key length");
470 return (gcipher_pywrap((PyObject
*)ty
,
471 GC_INIT(GCCIPHER_CC(ty
), k
, sz
),
477 PyObject
*gccipher_pywrap(gccipher
*cc
)
479 gccipher_pyobj
*g
= newtype(gccipher_pytype
, 0, cc
->name
);
481 g
->ty
.ht_type
.tp_basicsize
= sizeof(gcipher_pyobj
);
482 g
->ty
.ht_type
.tp_base
= gcipher_pytype
;
483 Py_INCREF(gcipher_pytype
);
484 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
485 Py_TPFLAGS_BASETYPE
|
486 Py_TPFLAGS_HEAPTYPE
);
487 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
488 g
->ty
.ht_type
.tp_free
= 0;
489 g
->ty
.ht_type
.tp_new
= gcipher_pynew
;
490 typeready(&g
->ty
.ht_type
);
491 return ((PyObject
*)g
);
494 static void gcipher_pydealloc(PyObject
*me
)
496 if (GCIPHER_F(me
) & f_freeme
)
497 GC_DESTROY(GCIPHER_C(me
));
498 Py_DECREF(me
->ob_type
);
502 static PyObject
*gccget_name(PyObject
*me
, void *hunoz
)
503 { return (PyString_FromString(GCCIPHER_CC(me
)->name
)); }
505 static PyObject
*gccget_keysz(PyObject
*me
, void *hunoz
)
506 { return (keysz_pywrap(GCCIPHER_CC(me
)->keysz
)); }
508 static PyObject
*gccget_blksz(PyObject
*me
, void *hunoz
)
509 { return (PyInt_FromLong(GCCIPHER_CC(me
)->blksz
)); }
511 static PyObject
*gcmeth_encrypt(PyObject
*me
, PyObject
*arg
)
517 if (!PyArg_ParseTuple(arg
, "s#:encrypt", &p
, &sz
)) return (0);
518 rc
= bytestring_pywrap(0, sz
);
519 GC_ENCRYPT(GCIPHER_C(me
), p
, PyString_AS_STRING(rc
), sz
);
523 static PyObject
*gcmeth_enczero(PyObject
*me
, PyObject
*arg
)
529 if (!PyArg_ParseTuple(arg
, "i:enczero", &sz
)) return (0);
530 rc
= bytestring_pywrap(0, sz
);
531 p
= PyString_AS_STRING(rc
);
533 GC_ENCRYPT(GCIPHER_C(me
), p
, p
, sz
);
537 static PyObject
*gcmeth_decrypt(PyObject
*me
, PyObject
*arg
)
543 if (!PyArg_ParseTuple(arg
, "s#:decrypt", &p
, &sz
)) return (0);
544 rc
= bytestring_pywrap(0, sz
);
545 GC_DECRYPT(GCIPHER_C(me
), p
, PyString_AS_STRING(rc
), sz
);
549 static PyObject
*gcmeth_deczero(PyObject
*me
, PyObject
*arg
)
555 if (!PyArg_ParseTuple(arg
, "i:deczero", &sz
)) return (0);
556 rc
= bytestring_pywrap(0, sz
);
557 p
= PyString_AS_STRING(rc
);
559 GC_DECRYPT(GCIPHER_C(me
), p
, p
, sz
);
563 static PyObject
*gcmeth_setiv(PyObject
*me
, PyObject
*arg
)
568 if (!PyArg_ParseTuple(arg
, "s#:setiv", &p
, &sz
)) goto end
;
569 if (!GCIPHER_C(me
)->ops
->setiv
) VALERR("`setiv' not supported");
570 if (!GC_CLASS(GCIPHER_C(me
))->blksz
) VALERR("not a block cipher mode");
571 if (sz
!= GC_CLASS(GCIPHER_C(me
))->blksz
) VALERR("bad IV length");
572 GC_SETIV(GCIPHER_C(me
), p
);
578 static PyObject
*gcmeth_bdry(PyObject
*me
, PyObject
*arg
)
580 if (!PyArg_ParseTuple(arg
, ":bdry")) goto end
;
581 if (!GCIPHER_C(me
)->ops
->bdry
) VALERR("`bdry' not supported");
582 if (!GC_CLASS(GCIPHER_C(me
))->blksz
) VALERR("not a block cipher mode");
583 GC_BDRY(GCIPHER_C(me
));
589 static PyGetSetDef gccipher_pygetset
[] = {
590 #define GETSETNAME(op, name) gcc##op##_##name
591 GET (keysz
, "CC.keysz -> acceptable key sizes")
592 GET (blksz
, "CC.blksz -> block size, or zero")
593 GET (name
, "CC.name -> name of this kind of cipher")
598 static PyMethodDef gcipher_pymethods
[] = {
599 #define METHNAME(name) gcmeth_##name
600 METH (encrypt
, "C.encrypt(PT) -> CT")
601 METH (enczero
, "C.enczero(N) -> CT")
602 METH (decrypt
, "C.decrypt(CT) -> PT")
603 METH (deczero
, "C.deczero(N) -> PT")
604 METH (setiv
, "C.setiv(IV)")
605 METH (bdry
, "C.bdry()")
610 static PyTypeObject gccipher_pytype_skel
= {
611 PyObject_HEAD_INIT(0) 0, /* Header */
612 "GCCipher", /* @tp_name@ */
613 sizeof(gccipher_pyobj
), /* @tp_basicsize@ */
614 0, /* @tp_itemsize@ */
616 0, /* @tp_dealloc@ */
618 0, /* @tp_getattr@ */
619 0, /* @tp_setattr@ */
620 0, /* @tp_compare@ */
622 0, /* @tp_as_number@ */
623 0, /* @tp_as_sequence@ */
624 0, /* @tp_as_mapping@ */
628 0, /* @tp_getattro@ */
629 0, /* @tp_setattro@ */
630 0, /* @tp_as_buffer@ */
631 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
635 "Symmetric cipher metaclass.",
637 0, /* @tp_traverse@ */
639 0, /* @tp_richcompare@ */
640 0, /* @tp_weaklistoffset@ */
642 0, /* @tp_iternext@ */
643 0, /* @tp_methods@ */
644 0, /* @tp_members@ */
645 gccipher_pygetset
, /* @tp_getset@ */
648 0, /* @tp_descr_get@ */
649 0, /* @tp_descr_set@ */
650 0, /* @tp_dictoffset@ */
652 PyType_GenericAlloc
, /* @tp_alloc@ */
653 abstract_pynew
, /* @tp_new@ */
658 static PyTypeObject gcipher_pytype_skel
= {
659 PyObject_HEAD_INIT(0) 0, /* Header */
660 "GCipher", /* @tp_name@ */
661 sizeof(gcipher_pyobj
), /* @tp_basicsize@ */
662 0, /* @tp_itemsize@ */
664 gcipher_pydealloc
, /* @tp_dealloc@ */
666 0, /* @tp_getattr@ */
667 0, /* @tp_setattr@ */
668 0, /* @tp_compare@ */
670 0, /* @tp_as_number@ */
671 0, /* @tp_as_sequence@ */
672 0, /* @tp_as_mapping@ */
676 0, /* @tp_getattro@ */
677 0, /* @tp_setattro@ */
678 0, /* @tp_as_buffer@ */
679 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
683 "Symmetric cipher, abstract base class.",
685 0, /* @tp_traverse@ */
687 0, /* @tp_richcompare@ */
688 0, /* @tp_weaklistoffset@ */
690 0, /* @tp_iternext@ */
691 gcipher_pymethods
, /* @tp_methods@ */
692 0, /* @tp_members@ */
696 0, /* @tp_descr_get@ */
697 0, /* @tp_descr_set@ */
698 0, /* @tp_dictoffset@ */
700 PyType_GenericAlloc
, /* @tp_alloc@ */
701 abstract_pynew
, /* @tp_new@ */
706 /*----- Hash functions ----------------------------------------------------*/
708 PyTypeObject
*gchash_pytype
, *ghash_pytype
;
710 CONVFUNC(gchash
, gchash
*, GCHASH_CH
)
711 CONVFUNC(ghash
, ghash
*, GHASH_H
)
713 static PyObject
*ghash_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
715 char *kwlist
[] = { 0 };
716 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, ":new", kwlist
))
718 return (ghash_pywrap((PyObject
*)ty
, GH_INIT(GCHASH_CH(ty
)), f_freeme
));
723 PyObject
*gchash_pywrap(gchash
*ch
)
725 gchash_pyobj
*g
= newtype(gchash_pytype
, 0, ch
->name
);
727 g
->ty
.ht_type
.tp_basicsize
= sizeof(ghash_pyobj
);
728 g
->ty
.ht_type
.tp_base
= ghash_pytype
;
729 Py_INCREF(ghash_pytype
);
730 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
731 Py_TPFLAGS_BASETYPE
|
732 Py_TPFLAGS_HEAPTYPE
);
733 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
734 g
->ty
.ht_type
.tp_free
= 0;
735 g
->ty
.ht_type
.tp_new
= ghash_pynew
;
736 typeready(&g
->ty
.ht_type
);
737 return ((PyObject
*)g
);
740 PyObject
*ghash_pywrap(PyObject
*cobj
, ghash
*h
, unsigned f
)
743 if (!cobj
) cobj
= gchash_pywrap((/*unconst*/ gchash
*)GH_CLASS(h
));
744 else Py_INCREF(cobj
);
745 g
= PyObject_NEW(ghash_pyobj
, (PyTypeObject
*)cobj
);
748 return ((PyObject
*)g
);
751 static void ghash_pydealloc(PyObject
*me
)
753 if (GHASH_F(me
) & f_freeme
)
754 GH_DESTROY(GHASH_H(me
));
755 Py_DECREF(me
->ob_type
);
759 static PyObject
*gchget_name(PyObject
*me
, void *hunoz
)
760 { return (PyString_FromString(GCHASH_CH(me
)->name
)); }
762 static PyObject
*gchget_hashsz(PyObject
*me
, void *hunoz
)
763 { return (PyInt_FromLong(GCHASH_CH(me
)->hashsz
)); }
765 static PyObject
*gchget_bufsz(PyObject
*me
, void *hunoz
)
766 { return (PyInt_FromLong(GCHASH_CH(me
)->bufsz
)); }
768 static PyObject
*ghmeth_hash(PyObject
*me
, PyObject
*arg
)
772 if (!PyArg_ParseTuple(arg
, "s#:hash", &p
, &sz
)) return (0);
773 GH_HASH(GHASH_H(me
), p
, sz
);
777 #define GHMETH_HASHU_(n, W, w) \
778 static PyObject *ghmeth_hashu##w(PyObject *me, PyObject *arg) \
781 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \
782 GH_HASHU##W(GHASH_H(me), x); \
787 DOUINTCONV(GHMETH_HASHU_
)
789 #define GHMETH_HASHBUF_(n, W, w) \
790 static PyObject *ghmeth_hashbuf##w(PyObject *me, PyObject *arg) \
794 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
795 if (sz > MASK##n) TYERR("string too long"); \
796 GH_HASHBUF##W(GHASH_H(me), p, sz); \
801 DOUINTCONV(GHMETH_HASHBUF_
)
803 static PyObject
*ghmeth_hashstrz(PyObject
*me
, PyObject
*arg
)
806 if (!PyArg_ParseTuple(arg
, "s:hashstrz", &p
)) return (0);
807 GH_HASHSTRZ(GHASH_H(me
), p
);
811 static PyObject
*ghmeth_done(PyObject
*me
, PyObject
*arg
)
815 if (!PyArg_ParseTuple(arg
, ":done")) return (0);
816 g
= GH_COPY(GHASH_H(me
));
817 rc
= bytestring_pywrap(0, g
->ops
->c
->hashsz
);
818 GH_DONE(g
, PyString_AS_STRING(rc
));
823 static PyGetSetDef gchash_pygetset
[] = {
824 #define GETSETNAME(op, name) gch##op##_##name
825 GET (bufsz
, "CH.bufsz -> hash buffer size, or zero")
826 GET (hashsz
, "CH.hashsz -> hash output size")
827 GET (name
, "CH.name -> name of this kind of hash")
832 static PyMethodDef ghash_pymethods
[] = {
833 #define METHNAME(name) ghmeth_##name
834 METH (hash
, "H.hash(M)")
835 #define METHU_(n, W, w) METH(hashu##w, "H.hashu" #w "(WORD)")
838 #define METHBUF_(n, W, w) METH(hashbuf##w, "H.hashbuf" #w "(BYTES)")
841 METH (hashstrz
, "H.hashstrz(STRING)")
842 METH (done
, "H.done() -> HASH")
847 static PyTypeObject gchash_pytype_skel
= {
848 PyObject_HEAD_INIT(0) 0, /* Header */
849 "GCHash", /* @tp_name@ */
850 sizeof(gchash_pyobj
), /* @tp_basicsize@ */
851 0, /* @tp_itemsize@ */
853 0, /* @tp_dealloc@ */
855 0, /* @tp_getattr@ */
856 0, /* @tp_setattr@ */
857 0, /* @tp_compare@ */
859 0, /* @tp_as_number@ */
860 0, /* @tp_as_sequence@ */
861 0, /* @tp_as_mapping@ */
865 0, /* @tp_getattro@ */
866 0, /* @tp_setattro@ */
867 0, /* @tp_as_buffer@ */
868 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
872 "Hash function metaclass.",
874 0, /* @tp_traverse@ */
876 0, /* @tp_richcompare@ */
877 0, /* @tp_weaklistoffset@ */
879 0, /* @tp_iternext@ */
880 0, /* @tp_methods@ */
881 0, /* @tp_members@ */
882 gchash_pygetset
, /* @tp_getset@ */
885 0, /* @tp_descr_get@ */
886 0, /* @tp_descr_set@ */
887 0, /* @tp_dictoffset@ */
889 PyType_GenericAlloc
, /* @tp_alloc@ */
890 abstract_pynew
, /* @tp_new@ */
895 static PyTypeObject ghash_pytype_skel
= {
896 PyObject_HEAD_INIT(0) 0, /* Header */
897 "GHash", /* @tp_name@ */
898 sizeof(ghash_pyobj
), /* @tp_basicsize@ */
899 0, /* @tp_itemsize@ */
901 ghash_pydealloc
, /* @tp_dealloc@ */
903 0, /* @tp_getattr@ */
904 0, /* @tp_setattr@ */
905 0, /* @tp_compare@ */
907 0, /* @tp_as_number@ */
908 0, /* @tp_as_sequence@ */
909 0, /* @tp_as_mapping@ */
913 0, /* @tp_getattro@ */
914 0, /* @tp_setattro@ */
915 0, /* @tp_as_buffer@ */
916 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
920 "Hash function, abstract base class.",
922 0, /* @tp_traverse@ */
924 0, /* @tp_richcompare@ */
925 0, /* @tp_weaklistoffset@ */
927 0, /* @tp_iternext@ */
928 ghash_pymethods
, /* @tp_methods@ */
929 0, /* @tp_members@ */
933 0, /* @tp_descr_get@ */
934 0, /* @tp_descr_set@ */
935 0, /* @tp_dictoffset@ */
937 PyType_GenericAlloc
, /* @tp_alloc@ */
938 abstract_pynew
, /* @tp_new@ */
943 /*----- Message authentication --------------------------------------------*/
945 PyTypeObject
*gcmac_pytype
, *gmac_pytype
, *gmhash_pytype
;
947 CONVFUNC(gcmac
, gcmac
*, GCMAC_CM
)
948 CONVFUNC(gmac
, gmac
*, GMAC_M
)
949 CONVFUNC(gmhash
, ghash
*, GHASH_H
)
951 static PyObject
*gmac_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
953 char *kwlist
[] = { "k", 0 };
957 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", kwlist
, &k
, &sz
))
959 if (keysz(sz
, GCMAC_CM(ty
)->keysz
) != sz
) VALERR("bad key length");
960 return (gmac_pywrap((PyObject
*)ty
,
961 GM_KEY(GCMAC_CM(ty
), k
, sz
),
967 static PyObject
*gmhash_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
969 char *kwlist
[] = { 0 };
972 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, ":new", kwlist
)) return (0);
973 g
= PyObject_NEW(ghash_pyobj
, ty
);
974 g
->h
= GM_INIT(GMAC_M(ty
));
977 return ((PyObject
*)g
);
980 PyObject
*gcmac_pywrap(gcmac
*cm
)
982 gcmac_pyobj
*g
= newtype(gcmac_pytype
, 0, cm
->name
);
984 g
->ty
.ht_type
.tp_basicsize
= sizeof(gmac_pyobj
);
985 g
->ty
.ht_type
.tp_base
= gmac_pytype
;
986 Py_INCREF(gmac_pytype
);
987 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
988 Py_TPFLAGS_BASETYPE
|
989 Py_TPFLAGS_HEAPTYPE
);
990 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
991 g
->ty
.ht_type
.tp_free
= 0;
992 g
->ty
.ht_type
.tp_new
= gmac_pynew
;
993 typeready(&g
->ty
.ht_type
);
994 return ((PyObject
*)g
);
997 PyObject
*gmac_pywrap(PyObject
*cobj
, gmac
*m
, unsigned f
)
1000 if (!cobj
) cobj
= gcmac_pywrap((/*unconst*/ gcmac
*)GM_CLASS(m
));
1001 else Py_INCREF(cobj
);
1002 g
= newtype((PyTypeObject
*)cobj
, 0, 0);
1003 g
->ty
.ht_type
.tp_basicsize
= sizeof(ghash_pyobj
);
1004 g
->ty
.ht_name
= PyString_FromFormat("%s(keyed)", m
->ops
->c
->name
);
1005 g
->ty
.ht_type
.tp_name
= PyString_AS_STRING(g
->ty
.ht_name
);
1006 g
->ty
.ht_type
.tp_base
= gmhash_pytype
;
1007 Py_INCREF(gmac_pytype
);
1008 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
1009 Py_TPFLAGS_BASETYPE
|
1010 Py_TPFLAGS_HEAPTYPE
);
1011 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
1012 g
->ty
.ht_type
.tp_free
= 0;
1013 g
->ty
.ht_type
.tp_new
= gmhash_pynew
;
1014 typeready(&g
->ty
.ht_type
);
1017 return ((PyObject
*)g
);
1020 static void gmac_pydealloc(PyObject
*me
)
1022 if (GMAC_F(me
) & f_freeme
)
1023 GM_DESTROY(GMAC_M(me
));
1024 Py_DECREF(me
->ob_type
);
1025 PyType_Type
.tp_dealloc(me
);
1028 static PyObject
*gcmget_name(PyObject
*me
, void *hunoz
)
1029 { return (PyString_FromString(GCMAC_CM(me
)->name
)); }
1031 static PyObject
*gcmget_keysz(PyObject
*me
, void *hunoz
)
1032 { return (keysz_pywrap(GCMAC_CM(me
)->keysz
)); }
1034 static PyObject
*gcmget_tagsz(PyObject
*me
, void *hunoz
)
1035 { return (PyInt_FromLong(GCMAC_CM(me
)->hashsz
)); }
1037 static PyGetSetDef gcmac_pygetset
[] = {
1038 #define GETSETNAME(op, name) gcm##op##_##name
1039 GET (keysz
, "CM.keysz -> acceptable key sizes")
1040 GET (tagsz
, "CM.tagsz -> MAC output size")
1041 GET (name
, "CM.name -> name of this kind of MAC")
1046 static PyTypeObject gcmac_pytype_skel
= {
1047 PyObject_HEAD_INIT(0) 0, /* Header */
1048 "GCMAC", /* @tp_name@ */
1049 sizeof(gchash_pyobj
), /* @tp_basicsize@ */
1050 0, /* @tp_itemsize@ */
1052 0, /* @tp_dealloc@ */
1054 0, /* @tp_getattr@ */
1055 0, /* @tp_setattr@ */
1056 0, /* @tp_compare@ */
1058 0, /* @tp_as_number@ */
1059 0, /* @tp_as_sequence@ */
1060 0, /* @tp_as_mapping@ */
1064 0, /* @tp_getattro@ */
1065 0, /* @tp_setattro@ */
1066 0, /* @tp_as_buffer@ */
1067 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1068 Py_TPFLAGS_BASETYPE
,
1071 "Message authentication code metametaclass.",
1073 0, /* @tp_traverse@ */
1075 0, /* @tp_richcompare@ */
1076 0, /* @tp_weaklistoffset@ */
1078 0, /* @tp_iternext@ */
1079 0, /* @tp_methods@ */
1080 0, /* @tp_members@ */
1081 gcmac_pygetset
, /* @tp_getset@ */
1084 0, /* @tp_descr_get@ */
1085 0, /* @tp_descr_set@ */
1086 0, /* @tp_dictoffset@ */
1088 PyType_GenericAlloc
, /* @tp_alloc@ */
1089 abstract_pynew
, /* @tp_new@ */
1094 static PyTypeObject gmac_pytype_skel
= {
1095 PyObject_HEAD_INIT(0) 0, /* Header */
1096 "GMAC", /* @tp_name@ */
1097 sizeof(gmac_pyobj
), /* @tp_basicsize@ */
1098 0, /* @tp_itemsize@ */
1100 gmac_pydealloc
, /* @tp_dealloc@ */
1102 0, /* @tp_getattr@ */
1103 0, /* @tp_setattr@ */
1104 0, /* @tp_compare@ */
1106 0, /* @tp_as_number@ */
1107 0, /* @tp_as_sequence@ */
1108 0, /* @tp_as_mapping@ */
1112 0, /* @tp_getattro@ */
1113 0, /* @tp_setattro@ */
1114 0, /* @tp_as_buffer@ */
1115 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1116 Py_TPFLAGS_BASETYPE
,
1119 "Message authentication code metaclass, abstract base class.",
1121 0, /* @tp_traverse@ */
1123 0, /* @tp_richcompare@ */
1124 0, /* @tp_weaklistoffset@ */
1126 0, /* @tp_iternext@ */
1127 0, /* @tp_methods@ */
1128 0, /* @tp_members@ */
1129 0, /* @tp_getset@ */
1132 0, /* @tp_descr_get@ */
1133 0, /* @tp_descr_set@ */
1134 0, /* @tp_dictoffset@ */
1136 PyType_GenericAlloc
, /* @tp_alloc@ */
1137 abstract_pynew
, /* @tp_new@ */
1142 static PyTypeObject gmhash_pytype_skel
= {
1143 PyObject_HEAD_INIT(0) 0, /* Header */
1144 "GMACHash", /* @tp_name@ */
1145 sizeof(ghash_pyobj
), /* @tp_basicsize@ */
1146 0, /* @tp_itemsize@ */
1148 ghash_pydealloc
, /* @tp_dealloc@ */
1150 0, /* @tp_getattr@ */
1151 0, /* @tp_setattr@ */
1152 0, /* @tp_compare@ */
1154 0, /* @tp_as_number@ */
1155 0, /* @tp_as_sequence@ */
1156 0, /* @tp_as_mapping@ */
1160 0, /* @tp_getattro@ */
1161 0, /* @tp_setattro@ */
1162 0, /* @tp_as_buffer@ */
1163 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1164 Py_TPFLAGS_BASETYPE
,
1167 "Message authentication code, abstract base class.",
1169 0, /* @tp_traverse@ */
1171 0, /* @tp_richcompare@ */
1172 0, /* @tp_weaklistoffset@ */
1174 0, /* @tp_iternext@ */
1175 0, /* @tp_methods@ */
1176 0, /* @tp_members@ */
1177 0, /* @tp_getset@ */
1180 0, /* @tp_descr_get@ */
1181 0, /* @tp_descr_set@ */
1182 0, /* @tp_dictoffset@ */
1184 PyType_GenericAlloc
, /* @tp_alloc@ */
1185 abstract_pynew
, /* @tp_new@ */
1190 /*----- Special snowflake for Poly1305 ------------------------------------*/
1192 PyTypeObject
*poly1305cls_pytype
, *poly1305key_pytype
, *poly1305hash_pytype
;
1194 typedef struct poly1305key_pyobj
{
1195 PyHeapTypeObject ty
;
1197 } poly1305key_pyobj
;
1199 typedef struct poly1305hash_pyobj
{
1204 } poly1305hash_pyobj
;
1206 #define P1305_F(o) (((poly1305hash_pyobj *)(o))->f)
1207 #define P1305_CTX(o) (&((poly1305hash_pyobj *)(o))->ctx)
1208 CONVFUNC(poly1305hash
, poly1305_ctx
*, P1305_CTX
)
1210 static PyObject
*poly1305hash_pynew(PyTypeObject
*ty
,
1211 PyObject
*arg
, PyObject
*kw
)
1213 char *kwlist
[] = { "mask", 0 };
1214 poly1305key_pyobj
*pk
= (poly1305key_pyobj
*)ty
;
1215 poly1305hash_pyobj
*ph
;
1219 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|s#:new", kwlist
, &m
, &sz
))
1221 if (m
&& sz
!= POLY1305_MASKSZ
) VALERR("bad mask length");
1222 ph
= PyObject_NEW(poly1305hash_pyobj
, ty
);
1224 if (m
) ph
->f
|= f_mask
;
1225 poly1305_macinit(&ph
->ctx
, &pk
->k
, m
);
1227 return ((PyObject
*)ph
);
1232 static PyObject
*poly1305key_pynew(PyTypeObject
*ty
,
1233 PyObject
*arg
, PyObject
*kw
)
1235 char *kwlist
[] = { "k", 0 };
1236 poly1305key_pyobj
*pk
;
1240 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", kwlist
, &k
, &sz
))
1242 if (keysz(sz
, poly1305_keysz
) != sz
) VALERR("bad key length");
1244 pk
= newtype(ty
, 0, 0);
1245 pk
->ty
.ht_name
= PyString_FromString("poly1305(keyed)");
1246 pk
->ty
.ht_type
.tp_basicsize
= sizeof(poly1305hash_pyobj
);
1247 pk
->ty
.ht_type
.tp_name
= PyString_AS_STRING(pk
->ty
.ht_name
);
1248 pk
->ty
.ht_type
.tp_base
= poly1305hash_pytype
;
1249 Py_INCREF(poly1305key_pytype
);
1250 pk
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
1251 Py_TPFLAGS_BASETYPE
|
1252 Py_TPFLAGS_HEAPTYPE
);
1253 pk
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
1254 pk
->ty
.ht_type
.tp_free
= 0;
1255 pk
->ty
.ht_type
.tp_new
= poly1305hash_pynew
;
1256 typeready(&pk
->ty
.ht_type
);
1258 poly1305_keyinit(&pk
->k
, k
, sz
);
1259 return ((PyObject
*)pk
);
1265 static PyObject
*poly1305clsget_name(PyObject
*me
, void *hunoz
)
1266 { return (PyString_FromString("poly1305")); }
1268 static PyObject
*poly1305clsget_keysz(PyObject
*me
, void *hunoz
)
1269 { return (keysz_pywrap(poly1305_keysz
)); }
1271 static PyObject
*poly1305clsget_masksz(PyObject
*me
, void *hunoz
)
1272 { return (PyInt_FromLong(POLY1305_MASKSZ
)); }
1274 static PyObject
*poly1305clsget_tagsz(PyObject
*me
, void *hunoz
)
1275 { return (PyInt_FromLong(POLY1305_TAGSZ
)); }
1277 static PyObject
*polymeth_copy(PyObject
*me
, PyObject
*arg
)
1279 poly1305hash_pyobj
*ph
;
1280 if (!PyArg_ParseTuple(arg
, ":copy")) return (0);
1281 ph
= PyObject_NEW(poly1305hash_pyobj
, me
->ob_type
);
1282 poly1305_copy(&ph
->ctx
, P1305_CTX(me
));
1283 Py_INCREF(me
->ob_type
);
1284 return ((PyObject
*)ph
);
1287 static PyObject
*polymeth_hash(PyObject
*me
, PyObject
*arg
)
1291 if (!PyArg_ParseTuple(arg
, "s#:hash", &p
, &sz
)) return (0);
1292 poly1305_hash(P1305_CTX(me
), p
, sz
);
1296 #define POLYMETH_HASHU_(n, W, w) \
1297 static PyObject *polymeth_hashu##w(PyObject *me, PyObject *arg) \
1301 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \
1302 STORE##W(b, x); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \
1307 DOUINTCONV(POLYMETH_HASHU_
)
1309 #define POLYMETH_HASHBUF_(n, W, w) \
1310 static PyObject *polymeth_hashbuf##w(PyObject *me, PyObject *arg) \
1315 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
1316 if (sz > MASK##n) TYERR("string too long"); \
1317 STORE##W(b, sz); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \
1318 poly1305_hash(P1305_CTX(me), p, sz); \
1323 DOUINTCONV(POLYMETH_HASHBUF_
)
1325 static PyObject
*polymeth_hashstrz(PyObject
*me
, PyObject
*arg
)
1328 if (!PyArg_ParseTuple(arg
, "s:hashstrz", &p
)) return (0);
1329 poly1305_hash(P1305_CTX(me
), p
, strlen(p
) + 1);
1333 static PyObject
*polymeth_flush(PyObject
*me
, PyObject
*arg
)
1335 if (!PyArg_ParseTuple(arg
, ":flush")) return (0);
1336 poly1305_flush(P1305_CTX(me
));
1340 static PyObject
*polymeth_flushzero(PyObject
*me
, PyObject
*arg
)
1342 if (!PyArg_ParseTuple(arg
, ":flushzero")) return (0);
1343 poly1305_flushzero(P1305_CTX(me
));
1347 static PyObject
*polymeth_concat(PyObject
*me
, PyObject
*arg
)
1349 PyObject
*pre
, *suff
;
1350 if (!PyArg_ParseTuple(arg
, "OO:concat", &pre
, &suff
)) return (0);
1351 if (!PyObject_TypeCheck(pre
, poly1305hash_pytype
) ||
1352 !PyObject_TypeCheck(suff
, poly1305hash_pytype
))
1353 TYERR("wanted a poly1305hash");
1354 if (me
->ob_type
!= pre
->ob_type
|| me
->ob_type
!= suff
->ob_type
)
1355 TYERR("key mismatch");
1356 if (P1305_CTX(pre
)->nbuf
) VALERR("prefix is not block-aligned");
1357 poly1305_concat(P1305_CTX(me
), P1305_CTX(pre
), P1305_CTX(suff
));
1363 static PyObject
*polymeth_done(PyObject
*me
, PyObject
*arg
)
1366 if (!PyArg_ParseTuple(arg
, ":done")) return (0);
1367 if (!(P1305_F(me
) & f_mask
)) VALERR("no mask");
1368 rc
= bytestring_pywrap(0, POLY1305_TAGSZ
);
1369 poly1305_done(P1305_CTX(me
), PyString_AS_STRING(rc
));
1375 static PyGetSetDef poly1305cls_pygetset
[] = {
1376 #define GETSETNAME(op, name) poly1305cls##op##_##name
1377 GET (keysz
, "PC.keysz -> acceptable key sizes")
1378 GET (masksz
, "PC.masksz -> mask size")
1379 GET (tagsz
, "PC.tagsz -> MAC output size")
1380 GET (name
, "PC.name -> name of this kind of MAC")
1385 static PyMethodDef poly1305hash_pymethods
[] = {
1386 #define METHNAME(name) polymeth_##name
1387 METH (copy
, "P.copy() -> PP")
1388 METH (hash
, "P.hash(M)")
1389 #define METHU_(n, W, w) METH(hashu##w, "P.hashu" #w "(WORD)")
1392 #define METHBUF_(n, W, w) METH(hashbuf##w, "P.hashbuf" #w "(BYTES)")
1393 DOUINTCONV(METHBUF_
)
1395 METH (hashstrz
, "P.hashstrz(STRING)")
1396 METH (flush
, "P.flush()")
1397 METH (flushzero
, "P.flushzero()")
1398 METH (concat
, "P.concat(PREFIX, SUFFIX)")
1399 METH (done
, "P.done() -> TAG")
1404 static PyTypeObject poly1305cls_pytype_skel
= {
1405 PyObject_HEAD_INIT(0) 0, /* Header */
1406 "Poly1305Class", /* @tp_name@ */
1407 sizeof(PyHeapTypeObject
), /* @tp_basicsize@ */
1408 0, /* @tp_itemsize@ */
1410 0, /* @tp_dealloc@ */
1412 0, /* @tp_getattr@ */
1413 0, /* @tp_setattr@ */
1414 0, /* @tp_compare@ */
1416 0, /* @tp_as_number@ */
1417 0, /* @tp_as_sequence@ */
1418 0, /* @tp_as_mapping@ */
1422 0, /* @tp_getattro@ */
1423 0, /* @tp_setattro@ */
1424 0, /* @tp_as_buffer@ */
1425 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1426 Py_TPFLAGS_BASETYPE
,
1429 "Poly1305 metametaclass. Best not to ask.",
1431 0, /* @tp_traverse@ */
1433 0, /* @tp_richcompare@ */
1434 0, /* @tp_weaklistoffset@ */
1436 0, /* @tp_iternext@ */
1437 0, /* @tp_methods@ */
1438 0, /* @tp_members@ */
1439 poly1305cls_pygetset
, /* @tp_getset@ */
1442 0, /* @tp_descr_get@ */
1443 0, /* @tp_descr_set@ */
1444 0, /* @tp_dictoffset@ */
1446 PyType_GenericAlloc
, /* @tp_alloc@ */
1447 abstract_pynew
, /* @tp_new@ */
1452 static PyTypeObject poly1305key_pytype_skel
= {
1453 PyObject_HEAD_INIT(0) 0, /* Header */
1454 "poly1305", /* @tp_name@ */
1455 sizeof(poly1305key_pyobj
), /* @tp_basicsize@ */
1456 0, /* @tp_itemsize@ */
1458 0, /* @tp_dealloc@ */
1460 0, /* @tp_getattr@ */
1461 0, /* @tp_setattr@ */
1462 0, /* @tp_compare@ */
1464 0, /* @tp_as_number@ */
1465 0, /* @tp_as_sequence@ */
1466 0, /* @tp_as_mapping@ */
1470 0, /* @tp_getattro@ */
1471 0, /* @tp_setattro@ */
1472 0, /* @tp_as_buffer@ */
1473 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1474 Py_TPFLAGS_BASETYPE
,
1479 0, /* @tp_traverse@ */
1481 0, /* @tp_richcompare@ */
1482 0, /* @tp_weaklistoffset@ */
1484 0, /* @tp_iternext@ */
1485 0, /* @tp_methods@ */
1486 0, /* @tp_members@ */
1487 0, /* @tp_getset@ */
1490 0, /* @tp_descr_get@ */
1491 0, /* @tp_descr_set@ */
1492 0, /* @tp_dictoffset@ */
1494 PyType_GenericAlloc
, /* @tp_alloc@ */
1495 poly1305key_pynew
, /* @tp_new@ */
1500 static PyTypeObject poly1305hash_pytype_skel
= {
1501 PyObject_HEAD_INIT(0) 0, /* Header */
1502 "Poly1305Hash", /* @tp_name@ */
1503 sizeof(poly1305hash_pyobj
), /* @tp_basicsize@ */
1504 0, /* @tp_itemsize@ */
1506 0, /* @tp_dealloc@ */
1508 0, /* @tp_getattr@ */
1509 0, /* @tp_setattr@ */
1510 0, /* @tp_compare@ */
1512 0, /* @tp_as_number@ */
1513 0, /* @tp_as_sequence@ */
1514 0, /* @tp_as_mapping@ */
1518 0, /* @tp_getattro@ */
1519 0, /* @tp_setattro@ */
1520 0, /* @tp_as_buffer@ */
1521 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1522 Py_TPFLAGS_BASETYPE
,
1525 "Poly1305 MAC context base class.",
1527 0, /* @tp_traverse@ */
1529 0, /* @tp_richcompare@ */
1530 0, /* @tp_weaklistoffset@ */
1532 0, /* @tp_iternext@ */
1533 poly1305hash_pymethods
, /* @tp_methods@ */
1534 0, /* @tp_members@ */
1535 0, /* @tp_getset@ */
1538 0, /* @tp_descr_get@ */
1539 0, /* @tp_descr_set@ */
1540 0, /* @tp_dictoffset@ */
1542 PyType_GenericAlloc
, /* @tp_alloc@ */
1543 abstract_pynew
, /* @tp_new@ */
1548 /*----- Special snowflake for HSalsa and HChaCha --------------------------*/
1550 #define DEF_HDANCE(DANCE, HDANCE, dance, hdance) \
1551 static PyObject *meth_##hdance##_prf(PyObject *me, PyObject *arg) \
1553 dance##_ctx dance; \
1555 Py_ssize_t ksz, nsz; \
1557 if (!PyArg_ParseTuple(arg, "s#s#:" #hdance "_prf", \
1558 &k, &ksz, &n, &nsz)) \
1560 if (ksz != DANCE##_KEYSZ) VALERR("bad key length"); \
1561 if (nsz != HDANCE##_INSZ) VALERR("bad input length"); \
1562 rc = bytestring_pywrap(0, HSALSA20_OUTSZ); \
1563 dance##_init(&dance, k, ksz, 0); \
1564 hdance##_prf(&dance, n, PyString_AS_STRING(rc)); \
1570 DEF_HDANCE(SALSA20
, HSALSA20
, salsa20
, hsalsa20
)
1571 DEF_HDANCE(SALSA20
, HSALSA20
, salsa20
, hsalsa2012
)
1572 DEF_HDANCE(SALSA20
, HSALSA20
, salsa20
, hsalsa208
)
1574 DEF_HDANCE(CHACHA
, HCHACHA
, chacha
, hchacha20
)
1575 DEF_HDANCE(CHACHA
, HCHACHA
, chacha
, hchacha12
)
1576 DEF_HDANCE(CHACHA
, HCHACHA
, chacha
, hchacha8
)
1578 /*----- Pseudorandom permutations -----------------------------------------*/
1580 static PyTypeObject
*gcprp_pytype
, *gprp_pytype
;
1582 typedef struct prpinfo
{
1587 void (*init
)(void *, const void *, size_t);
1588 void (*eblk
)(void *, const void *, void *);
1589 void (*dblk
)(void *, const void *, void *);
1592 #define PRP_DEF(PRE, pre) \
1593 static void pre##_prpinit(void *ctx, const void *k, size_t ksz) \
1594 { pre##_init(ctx, k, ksz); } \
1595 static void pre##_prpeblk(void *ctx, const void *in, void *out) \
1597 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
1598 pre##_eblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
1600 static void pre##_prpdblk(void *ctx, const void *in, void *out) \
1602 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
1603 pre##_dblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
1605 static const prpinfo pre##_prpinfo = { \
1606 #pre, pre##_keysz, sizeof(pre##_ctx), PRE##_BLKSZ, \
1607 pre##_prpinit, pre##_prpeblk, pre##_prpdblk \
1611 static const struct prpinfo
*const gprptab
[] = {
1612 #define PRP_ENTRY(PRE, pre) &pre##_prpinfo,
1617 typedef struct gcprp_pyobj
{
1618 PyHeapTypeObject ty
;
1621 #define GCPRP_PRP(o) (((gcprp_pyobj *)(o))->prp)
1623 typedef struct gprp_pyobj
{
1627 #define GPRP_PRP(o) (((gprp_pyobj *)(o))->prp)
1628 #define GPRP_CTX(o) (((gprp_pyobj *)(o)) + 1)
1630 typedef struct prp
{
1635 static PyObject
*gprp_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1637 char *kwlist
[] = { "key", 0 };
1640 const prpinfo
*prp
= GCPRP_PRP(ty
);
1643 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", kwlist
, &k
, &sz
))
1645 if (keysz(sz
, prp
->keysz
) != sz
) VALERR("bad key length");
1646 me
= (PyObject
*)ty
->tp_alloc(ty
, 0);
1648 prp
->init(GPRP_CTX(me
), k
, sz
);
1655 static void gprp_pydealloc(PyObject
*me
)
1656 { Py_DECREF(me
->ob_type
); FREEOBJ(me
); }
1658 static PyObject
*gcprp_pywrap(const prpinfo
*prp
)
1660 gcprp_pyobj
*g
= newtype(gcprp_pytype
, 0, prp
->name
);
1662 g
->ty
.ht_type
.tp_basicsize
= sizeof(gprp_pyobj
) + prp
->ctxsz
;
1663 g
->ty
.ht_type
.tp_base
= gprp_pytype
;
1664 Py_INCREF(gprp_pytype
);
1665 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
1666 Py_TPFLAGS_BASETYPE
|
1667 Py_TPFLAGS_HEAPTYPE
);
1668 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
1669 g
->ty
.ht_type
.tp_free
= 0;
1670 g
->ty
.ht_type
.tp_new
= gprp_pynew
;
1671 typeready(&g
->ty
.ht_type
);
1672 return ((PyObject
*)g
);
1675 static PyObject
*gcpget_name(PyObject
*me
, void *hunoz
)
1676 { return (PyString_FromString(GCPRP_PRP(me
)->name
)); }
1677 static PyObject
*gcpget_keysz(PyObject
*me
, void *hunoz
)
1678 { return (keysz_pywrap(GCPRP_PRP(me
)->keysz
)); }
1679 static PyObject
*gcpget_blksz(PyObject
*me
, void *hunoz
)
1680 { return (PyInt_FromLong(GCPRP_PRP(me
)->blksz
)); }
1682 static PyObject
*gpmeth_encrypt(PyObject
*me
, PyObject
*arg
)
1688 if (!PyArg_ParseTuple(arg
, "s#:encrypt", &p
, &n
)) goto end
;
1689 if (n
!= GPRP_PRP(me
)->blksz
) VALERR("incorrect block length");
1690 rc
= bytestring_pywrap(0, n
);
1691 GPRP_PRP(me
)->eblk(GPRP_CTX(me
), p
, PyString_AS_STRING(rc
));
1696 static PyObject
*gpmeth_decrypt(PyObject
*me
, PyObject
*arg
)
1702 if (!PyArg_ParseTuple(arg
, "s#:decrypt", &p
, &n
)) goto end
;
1703 if (n
!= GPRP_PRP(me
)->blksz
) VALERR("incorrect block length");
1704 rc
= bytestring_pywrap(0, n
);
1705 GPRP_PRP(me
)->dblk(GPRP_CTX(me
), p
, PyString_AS_STRING(rc
));
1710 static PyGetSetDef gcprp_pygetset
[] = {
1711 #define GETSETNAME(op, name) gcp##op##_##name
1712 GET (keysz
, "CP.keysz -> acceptable key sizes")
1713 GET (blksz
, "CP.blksz -> block size")
1714 GET (name
, "CP.name -> name of this kind of PRP")
1719 static PyMethodDef gprp_pymethods
[] = {
1720 #define METHNAME(name) gpmeth_##name
1721 METH (encrypt
, "P.encrypt(PT) -> CT")
1722 METH (decrypt
, "P.decrypt(CT) -> PT")
1727 static PyTypeObject gcprp_pytype_skel
= {
1728 PyObject_HEAD_INIT(0) 0, /* Header */
1729 "GCPRP", /* @tp_name@ */
1730 sizeof(gcprp_pyobj
), /* @tp_basicsize@ */
1731 0, /* @tp_itemsize@ */
1733 0, /* @tp_dealloc@ */
1735 0, /* @tp_getattr@ */
1736 0, /* @tp_setattr@ */
1737 0, /* @tp_compare@ */
1739 0, /* @tp_as_number@ */
1740 0, /* @tp_as_sequence@ */
1741 0, /* @tp_as_mapping@ */
1745 0, /* @tp_getattro@ */
1746 0, /* @tp_setattro@ */
1747 0, /* @tp_as_buffer@ */
1748 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1749 Py_TPFLAGS_BASETYPE
,
1752 "Pseudorandom permutation metaclass.",
1754 0, /* @tp_traverse@ */
1756 0, /* @tp_richcompare@ */
1757 0, /* @tp_weaklistoffset@ */
1759 0, /* @tp_iternext@ */
1760 0, /* @tp_methods@ */
1761 0, /* @tp_members@ */
1762 gcprp_pygetset
, /* @tp_getset@ */
1765 0, /* @tp_descr_get@ */
1766 0, /* @tp_descr_set@ */
1767 0, /* @tp_dictoffset@ */
1769 PyType_GenericAlloc
, /* @tp_alloc@ */
1770 abstract_pynew
, /* @tp_new@ */
1775 static PyTypeObject gprp_pytype_skel
= {
1776 PyObject_HEAD_INIT(0) 0, /* Header */
1777 "GPRP", /* @tp_name@ */
1778 sizeof(gprp_pyobj
), /* @tp_basicsize@ */
1779 0, /* @tp_itemsize@ */
1781 gprp_pydealloc
, /* @tp_dealloc@ */
1783 0, /* @tp_getattr@ */
1784 0, /* @tp_setattr@ */
1785 0, /* @tp_compare@ */
1787 0, /* @tp_as_number@ */
1788 0, /* @tp_as_sequence@ */
1789 0, /* @tp_as_mapping@ */
1793 0, /* @tp_getattro@ */
1794 0, /* @tp_setattro@ */
1795 0, /* @tp_as_buffer@ */
1796 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1797 Py_TPFLAGS_BASETYPE
,
1800 "Pseudorandom permutation, abstract base class.",
1802 0, /* @tp_traverse@ */
1804 0, /* @tp_richcompare@ */
1805 0, /* @tp_weaklistoffset@ */
1807 0, /* @tp_iternext@ */
1808 gprp_pymethods
, /* @tp_methods@ */
1809 0, /* @tp_members@ */
1810 0, /* @tp_getset@ */
1813 0, /* @tp_descr_get@ */
1814 0, /* @tp_descr_set@ */
1815 0, /* @tp_dictoffset@ */
1817 PyType_GenericAlloc
, /* @tp_alloc@ */
1818 abstract_pynew
, /* @tp_new@ */
1823 /*----- Main code ---------------------------------------------------------*/
1825 static PyMethodDef methods
[] = {
1826 #define METHNAME(func) meth_##func
1827 METH (_KeySZ_fromdl
, "\
1828 fromdl(N) -> M: convert integer discrete log field size to work factor")
1829 METH (_KeySZ_fromschnorr
, "\
1830 fromschnorr(N) -> M: convert Schnorr group order to work factor")
1831 METH (_KeySZ_fromif
, "\
1832 fromif(N) -> M: convert integer factorization problem size to work factor")
1833 METH (_KeySZ_fromec
, "\
1834 fromec(N) -> M: convert elliptic curve group order to work factor")
1835 METH (_KeySZ_todl
, "\
1836 todl(N) -> M: convert work factor to integer discrete log field size")
1837 METH (_KeySZ_toschnorr
, "\
1838 toschnorr(N) -> M: convert work factor to Schnorr group order")
1839 METH (_KeySZ_toif
, "\
1840 toif(N) -> M: convert work factor to integer factorization problem size")
1841 METH (_KeySZ_toec
, "\
1842 toec(N) -> M: convert work factor to elliptic curve group order")
1843 METH (_KeySZ_toec
, "\
1844 toec(N) -> M: convert work factor to elliptic curve group order")
1845 #define METH_HDANCE(hdance, HDance) METH(hdance##_prf, "\
1846 " #hdance "_prf(K, N) -> H: calculate " HDance " hash of N with K")
1847 METH_HDANCE(hsalsa20
, "HSalsa20")
1848 METH_HDANCE(hsalsa2012
, "HSalsa20/12")
1849 METH_HDANCE(hsalsa208
, "HSalsa20/8")
1850 METH_HDANCE(hchacha20
, "HChaCha20")
1851 METH_HDANCE(hchacha12
, "HChaCha12")
1852 METH_HDANCE(hchacha8
, "HChaCha8")
1858 void algorithms_pyinit(void)
1860 INITTYPE(keysz
, root
);
1861 INITTYPE(keyszany
, keysz
);
1862 INITTYPE(keyszrange
, keysz
);
1863 INITTYPE(keyszset
, keysz
);
1864 INITTYPE(gccipher
, type
);
1865 INITTYPE(gcipher
, root
);
1866 INITTYPE(gchash
, type
);
1867 INITTYPE(ghash
, root
);
1868 INITTYPE(gcmac
, type
);
1869 INITTYPE(gmac
, type
);
1870 INITTYPE(gmhash
, ghash
);
1871 INITTYPE(poly1305cls
, type
);
1872 INITTYPE_META(poly1305key
, type
, poly1305cls
);
1873 INITTYPE(poly1305hash
, root
);
1874 INITTYPE(gcprp
, type
);
1875 INITTYPE(gprp
, root
);
1876 addmethods(methods
);
1879 GEN(gcciphers
, cipher
)
1882 #define gcprp prpinfo
1885 void algorithms_pyinsert(PyObject
*mod
)
1888 INSERT("KeySZ", keysz_pytype
);
1889 INSERT("KeySZAny", keyszany_pytype
);
1890 INSERT("KeySZRange", keyszrange_pytype
);
1891 INSERT("KeySZSet", keyszset_pytype
);
1892 INSERT("GCCipher", gccipher_pytype
);
1893 INSERT("GCipher", gcipher_pytype
);
1894 INSERT("gcciphers", gcciphers());
1895 INSERT("GCHash", gchash_pytype
);
1896 INSERT("GHash", ghash_pytype
);
1897 INSERT("gchashes", d
= gchashes());
1898 sha_pyobj
= PyDict_GetItemString(d
, "sha"); Py_INCREF(sha_pyobj
);
1899 has160_pyobj
= PyDict_GetItemString(d
, "has160"); Py_INCREF(has160_pyobj
);
1900 INSERT("GCMAC", gcmac_pytype
);
1901 INSERT("GMAC", gmac_pytype
);
1902 INSERT("GMACHash", gmhash_pytype
);
1903 INSERT("gcmacs", gcmacs());
1904 INSERT("Poly1305Class", poly1305cls_pytype
);
1905 INSERT("poly1305", poly1305key_pytype
);
1906 INSERT("Poly1305Hash", poly1305hash_pytype
);
1907 INSERT("GCPRP", gcprp_pytype
);
1908 INSERT("GPRP", gprp_pytype
);
1909 INSERT("gcprps", gcprps());
1912 /*----- That's all, folks -------------------------------------------------*/