algorithms.c: Add missing `copy' methods on hash and Keccak objects.
[catacomb-python] / algorithms.c
1 /* -*-c-*-
2 *
3 * Symmetric cryptography
4 *
5 * (c) 2004 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the Python interface to Catacomb.
11 *
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.
16 *
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.
21 *
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.
25 */
26
27 /*----- Header files ------------------------------------------------------*/
28
29 #include "catacomb-python.h"
30 #include "algorithms.h"
31
32 /*----- Key sizes ---------------------------------------------------------*/
33
34 PyTypeObject *keysz_pytype;
35 PyTypeObject *keyszany_pytype, *keyszrange_pytype, *keyszset_pytype;
36 PyObject *sha_pyobj, *has160_pyobj;
37
38 #ifndef KSZ_OPMASK
39 # define KSZ_OPMASK 0x1f
40 #endif
41
42 #ifndef KSZ_16BIT
43 # define KSZ_16BIT 0x20
44 #endif
45
46 PyObject *keysz_pywrap(const octet *k)
47 {
48 unsigned op = *k++;
49 #define ARG(i) (op&KSZ_16BIT ? LOAD16(k + 2*(i)) : k[i])
50 switch (op&KSZ_OPMASK) {
51 case KSZ_ANY: {
52 keysz_pyobj *o = PyObject_New(keysz_pyobj, keyszany_pytype);
53 o->dfl = ARG(0);
54 return ((PyObject *)o);
55 } break;
56 case KSZ_RANGE: {
57 keyszrange_pyobj *o =
58 PyObject_New(keyszrange_pyobj, keyszrange_pytype);
59 o->dfl = ARG(0);
60 o->min = ARG(1);
61 o->max = ARG(2);
62 o->mod = ARG(3);
63 if (!o->mod) o->mod = 1;
64 return ((PyObject *)o);
65 } break;
66 case KSZ_SET: {
67 keyszset_pyobj *o =
68 PyObject_New(keyszset_pyobj, keyszset_pytype);
69 int i, n;
70 o->dfl = ARG(0);
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);
76 } break;
77 default:
78 abort();
79 }
80 #undef ARG
81 }
82
83 static PyObject *keyszany_pynew(PyTypeObject *ty,
84 PyObject *arg, PyObject *kw)
85 {
86 static const char *const kwlist[] = { "default", 0 };
87 int dfl;
88 keysz_pyobj *o;
89
90 if (!PyArg_ParseTupleAndKeywords(arg, kw, "i:new", KWLIST, &dfl))
91 goto end;
92 if (dfl < 0) VALERR("key size cannot be negative");
93 o = (keysz_pyobj *)ty->tp_alloc(ty, 0);
94 o->dfl = dfl;
95 return ((PyObject *)o);
96 end:
97 return (0);
98 }
99
100 static PyObject *keyszrange_pynew(PyTypeObject *ty,
101 PyObject *arg, PyObject *kw)
102 {
103 static const char *const kwlist[] = { "default", "min", "max", "mod", 0 };
104 int dfl, min = 0, max = 0, mod = 1;
105 keyszrange_pyobj *o;
106
107 if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|iii:new", KWLIST,
108 &dfl, &min, &max, &mod))
109 goto end;
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);
115 o->dfl = dfl;
116 o->min = min;
117 o->max = max;
118 o->mod = mod;
119 return ((PyObject *)o);
120 end:
121 return (0);
122 }
123
124 static PyObject *keyszset_pynew(PyTypeObject *ty,
125 PyObject *arg, PyObject *kw)
126 {
127 static const char *const kwlist[] = { "default", "set", 0 };
128 int dfl, i, n, xx;
129 PyObject *set = 0;
130 PyObject *x = 0, *l = 0;
131 keyszset_pyobj *o = 0;
132
133 if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|O:new", KWLIST, &dfl, &set))
134 goto end;
135 if (!set) set = PyTuple_New(0);
136 else Py_INCREF(set);
137 if (!PySequence_Check(set)) TYERR("want a sequence");
138 n = PySequence_Size(set);
139 l = PyList_New(0);
140 if (PyErr_Occurred()) goto end;
141 if (dfl < 0) VALERR("key size cannot be negative");
142 x = PyInt_FromLong(dfl);
143 PyList_Append(l, x);
144 Py_DECREF(x);
145 x = 0;
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");
152 PyList_Append(l, x);
153 Py_DECREF(x);
154 x = 0;
155 }
156 Py_DECREF(set);
157 if ((set = PySequence_Tuple(l)) == 0) goto end;
158 o = (keyszset_pyobj *)ty->tp_alloc(ty, 0);
159 o->dfl = dfl;
160 o->set = set;
161 Py_INCREF(set);
162 end:
163 Py_XDECREF(set);
164 Py_XDECREF(l);
165 Py_XDECREF(x);
166 return ((PyObject *)o);
167 }
168
169 static PyObject *kaget_min(PyObject *me, void *hunoz)
170 { return (PyInt_FromLong(0)); }
171 #define kaget_max kaget_min
172
173 static PyObject *ksget_min(PyObject *me, void *hunoz)
174 {
175 PyObject *set = ((keyszset_pyobj *)me)->set;
176 int i, n, y, x = -1;
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;
181 }
182 return (PyInt_FromLong(x));
183 }
184
185 static PyObject *ksget_max(PyObject *me, void *hunoz)
186 {
187 PyObject *set = ((keyszset_pyobj *)me)->set;
188 int i, n, y, x = -1;
189 n = PyTuple_Size(set);
190 for (i = 0; i < n; i++) {
191 y = PyInt_AsLong(PyTuple_GetItem(set, i));
192 if (y > x) x = y;
193 }
194 return (PyInt_FromLong(x));
195 }
196
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")
201 #undef default
202 #undef MEMBERSTRUCT
203 { 0 }
204 };
205
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")
210 #undef GETSETNAME
211 { 0 }
212 };
213
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")
220 #undef MEMBERSTRUCT
221 { 0 }
222 };
223
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")
228 #undef GETSETNAME
229 { 0 }
230 };
231
232 static PyMemberDef keyszset_pymembers[] = {
233 #define MEMBERSTRUCT keyszset_pyobj
234 MEMBER(set, T_OBJECT, READONLY, "KSZ.set -> allowed key sizes")
235 #undef MEMBERSTRUCT
236 { 0 }
237 };
238
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@ */
244
245 0, /* @tp_dealloc@ */
246 0, /* @tp_print@ */
247 0, /* @tp_getattr@ */
248 0, /* @tp_setattr@ */
249 0, /* @tp_compare@ */
250 0, /* @tp_repr@ */
251 0, /* @tp_as_number@ */
252 0, /* @tp_as_sequence@ */
253 0, /* @tp_as_mapping@ */
254 0, /* @tp_hash@ */
255 0, /* @tp_call@ */
256 0, /* @tp_str@ */
257 0, /* @tp_getattro@ */
258 0, /* @tp_setattro@ */
259 0, /* @tp_as_buffer@ */
260 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
261 Py_TPFLAGS_BASETYPE,
262
263 /* @tp_doc@ */
264 "Key size constraints. Abstract.",
265
266 0, /* @tp_traverse@ */
267 0, /* @tp_clear@ */
268 0, /* @tp_richcompare@ */
269 0, /* @tp_weaklistoffset@ */
270 0, /* @tp_iter@ */
271 0, /* @tp_iternext@ */
272 0, /* @tp_methods@ */
273 keysz_pymembers, /* @tp_members@ */
274 0, /* @tp_getset@ */
275 0, /* @tp_base@ */
276 0, /* @tp_dict@ */
277 0, /* @tp_descr_get@ */
278 0, /* @tp_descr_set@ */
279 0, /* @tp_dictoffset@ */
280 0, /* @tp_init@ */
281 PyType_GenericAlloc, /* @tp_alloc@ */
282 abstract_pynew, /* @tp_new@ */
283 0, /* @tp_free@ */
284 0 /* @tp_is_gc@ */
285 };
286
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@ */
292
293 0, /* @tp_dealloc@ */
294 0, /* @tp_print@ */
295 0, /* @tp_getattr@ */
296 0, /* @tp_setattr@ */
297 0, /* @tp_compare@ */
298 0, /* @tp_repr@ */
299 0, /* @tp_as_number@ */
300 0, /* @tp_as_sequence@ */
301 0, /* @tp_as_mapping@ */
302 0, /* @tp_hash@ */
303 0, /* @tp_call@ */
304 0, /* @tp_str@ */
305 0, /* @tp_getattro@ */
306 0, /* @tp_setattro@ */
307 0, /* @tp_as_buffer@ */
308 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
309 Py_TPFLAGS_BASETYPE,
310
311 /* @tp_doc@ */
312 "KeySZAny(DEFAULT)\n\
313 Key size constraints. This object imposes no constraints on size.",
314
315 0, /* @tp_traverse@ */
316 0, /* @tp_clear@ */
317 0, /* @tp_richcompare@ */
318 0, /* @tp_weaklistoffset@ */
319 0, /* @tp_iter@ */
320 0, /* @tp_iternext@ */
321 0, /* @tp_methods@ */
322 0, /* @tp_members@ */
323 keyszany_pygetset, /* @tp_getset@ */
324 0, /* @tp_base@ */
325 0, /* @tp_dict@ */
326 0, /* @tp_descr_get@ */
327 0, /* @tp_descr_set@ */
328 0, /* @tp_dictoffset@ */
329 0, /* @tp_init@ */
330 PyType_GenericAlloc, /* @tp_alloc@ */
331 keyszany_pynew, /* @tp_new@ */
332 0, /* @tp_free@ */
333 0 /* @tp_is_gc@ */
334 };
335
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@ */
341
342 0, /* @tp_dealloc@ */
343 0, /* @tp_print@ */
344 0, /* @tp_getattr@ */
345 0, /* @tp_setattr@ */
346 0, /* @tp_compare@ */
347 0, /* @tp_repr@ */
348 0, /* @tp_as_number@ */
349 0, /* @tp_as_sequence@ */
350 0, /* @tp_as_mapping@ */
351 0, /* @tp_hash@ */
352 0, /* @tp_call@ */
353 0, /* @tp_str@ */
354 0, /* @tp_getattro@ */
355 0, /* @tp_setattro@ */
356 0, /* @tp_as_buffer@ */
357 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
358 Py_TPFLAGS_BASETYPE,
359
360 /* @tp_doc@ */
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.",
364
365 0, /* @tp_traverse@ */
366 0, /* @tp_clear@ */
367 0, /* @tp_richcompare@ */
368 0, /* @tp_weaklistoffset@ */
369 0, /* @tp_iter@ */
370 0, /* @tp_iternext@ */
371 0, /* @tp_methods@ */
372 keyszrange_pymembers, /* @tp_members@ */
373 0, /* @tp_getset@ */
374 0, /* @tp_base@ */
375 0, /* @tp_dict@ */
376 0, /* @tp_descr_get@ */
377 0, /* @tp_descr_set@ */
378 0, /* @tp_dictoffset@ */
379 0, /* @tp_init@ */
380 PyType_GenericAlloc, /* @tp_alloc@ */
381 keyszrange_pynew, /* @tp_new@ */
382 0, /* @tp_free@ */
383 0 /* @tp_is_gc@ */
384 };
385
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@ */
391
392 0, /* @tp_dealloc@ */
393 0, /* @tp_print@ */
394 0, /* @tp_getattr@ */
395 0, /* @tp_setattr@ */
396 0, /* @tp_compare@ */
397 0, /* @tp_repr@ */
398 0, /* @tp_as_number@ */
399 0, /* @tp_as_sequence@ */
400 0, /* @tp_as_mapping@ */
401 0, /* @tp_hash@ */
402 0, /* @tp_call@ */
403 0, /* @tp_str@ */
404 0, /* @tp_getattro@ */
405 0, /* @tp_setattro@ */
406 0, /* @tp_as_buffer@ */
407 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
408 Py_TPFLAGS_BASETYPE,
409
410 /* @tp_doc@ */
411 "KeySZSet(DEFAULT, SEQ)\n\
412 Key size constraints. Key size must be DEFAULT or one in SEQ.",
413
414 0, /* @tp_traverse@ */
415 0, /* @tp_clear@ */
416 0, /* @tp_richcompare@ */
417 0, /* @tp_weaklistoffset@ */
418 0, /* @tp_iter@ */
419 0, /* @tp_iternext@ */
420 0, /* @tp_methods@ */
421 keyszset_pymembers, /* @tp_members@ */
422 keyszset_pygetset, /* @tp_getset@ */
423 0, /* @tp_base@ */
424 0, /* @tp_dict@ */
425 0, /* @tp_descr_get@ */
426 0, /* @tp_descr_set@ */
427 0, /* @tp_dictoffset@ */
428 0, /* @tp_init@ */
429 PyType_GenericAlloc, /* @tp_alloc@ */
430 keyszset_pynew, /* @tp_new@ */
431 0, /* @tp_free@ */
432 0 /* @tp_is_gc@ */
433 };
434
435 #define KSZCONVOP(op) \
436 static PyObject *meth__KeySZ_##op(PyObject *me, PyObject *arg) \
437 { \
438 double x, y; \
439 if (!PyArg_ParseTuple(arg, "Od:" #op, &me, &x)) return (0); \
440 y = keysz_##op(x); \
441 return (PyFloat_FromDouble(y)); \
442 }
443 KSZCONVOP(fromdl)
444 KSZCONVOP(fromschnorr)
445 KSZCONVOP(fromif)
446 KSZCONVOP(fromec)
447 KSZCONVOP(todl)
448 KSZCONVOP(toschnorr)
449 KSZCONVOP(toif)
450 KSZCONVOP(toec)
451 #undef KSZCONVOP
452
453 /*----- Symmetric encryption ----------------------------------------------*/
454
455 PyTypeObject *gccipher_pytype, *gcipher_pytype;
456
457 CONVFUNC(gccipher, gccipher *, GCCIPHER_CC)
458 CONVFUNC(gcipher, gcipher *, GCIPHER_C)
459
460 PyObject *gcipher_pywrap(PyObject *cobj, gcipher *c)
461 {
462 gcipher_pyobj *g;
463 if (!cobj) cobj = gccipher_pywrap((/*unconst*/ gccipher *)GC_CLASS(c));
464 else Py_INCREF(cobj);
465 g = PyObject_NEW(gcipher_pyobj, (PyTypeObject *)cobj);
466 g->c = c;
467 return ((PyObject *)g);
468 }
469
470 static PyObject *gcipher_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
471 {
472 static const char *const kwlist[] = { "k", 0 };
473 char *k;
474 Py_ssize_t sz;
475
476 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &k, &sz))
477 goto end;
478 if (keysz(sz, GCCIPHER_CC(ty)->keysz) != sz) VALERR("bad key length");
479 return (gcipher_pywrap((PyObject *)ty,
480 GC_INIT(GCCIPHER_CC(ty), k, sz)));
481 end:
482 return (0);
483 }
484
485 PyObject *gccipher_pywrap(gccipher *cc)
486 {
487 gccipher_pyobj *g = newtype(gccipher_pytype, 0, cc->name);
488 g->cc = cc;
489 g->ty.ht_type.tp_basicsize = sizeof(gcipher_pyobj);
490 g->ty.ht_type.tp_base = gcipher_pytype;
491 Py_INCREF(gcipher_pytype);
492 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
493 Py_TPFLAGS_BASETYPE |
494 Py_TPFLAGS_HEAPTYPE);
495 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
496 g->ty.ht_type.tp_free = 0;
497 g->ty.ht_type.tp_new = gcipher_pynew;
498 typeready(&g->ty.ht_type);
499 return ((PyObject *)g);
500 }
501
502 static void gcipher_pydealloc(PyObject *me)
503 {
504 GC_DESTROY(GCIPHER_C(me));
505 Py_DECREF(me->ob_type);
506 FREEOBJ(me);
507 }
508
509 static PyObject *gccget_name(PyObject *me, void *hunoz)
510 { return (PyString_FromString(GCCIPHER_CC(me)->name)); }
511
512 static PyObject *gccget_keysz(PyObject *me, void *hunoz)
513 { return (keysz_pywrap(GCCIPHER_CC(me)->keysz)); }
514
515 static PyObject *gccget_blksz(PyObject *me, void *hunoz)
516 { return (PyInt_FromLong(GCCIPHER_CC(me)->blksz)); }
517
518 static PyObject *gcmeth_encrypt(PyObject *me, PyObject *arg)
519 {
520 char *p;
521 Py_ssize_t sz;
522 PyObject *rc = 0;
523
524 if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &sz)) return (0);
525 rc = bytestring_pywrap(0, sz);
526 GC_ENCRYPT(GCIPHER_C(me), p, PyString_AS_STRING(rc), sz);
527 return (rc);
528 }
529
530 static PyObject *gcmeth_enczero(PyObject *me, PyObject *arg)
531 {
532 char *p;
533 int sz;
534 PyObject *rc = 0;
535
536 if (!PyArg_ParseTuple(arg, "i:enczero", &sz)) return (0);
537 rc = bytestring_pywrap(0, sz);
538 p = PyString_AS_STRING(rc);
539 memset(p, 0, sz);
540 GC_ENCRYPT(GCIPHER_C(me), p, p, sz);
541 return (rc);
542 }
543
544 static PyObject *gcmeth_decrypt(PyObject *me, PyObject *arg)
545 {
546 char *p;
547 Py_ssize_t sz;
548 PyObject *rc = 0;
549
550 if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &sz)) return (0);
551 rc = bytestring_pywrap(0, sz);
552 GC_DECRYPT(GCIPHER_C(me), p, PyString_AS_STRING(rc), sz);
553 return (rc);
554 }
555
556 static PyObject *gcmeth_deczero(PyObject *me, PyObject *arg)
557 {
558 char *p;
559 int sz;
560 PyObject *rc = 0;
561
562 if (!PyArg_ParseTuple(arg, "i:deczero", &sz)) return (0);
563 rc = bytestring_pywrap(0, sz);
564 p = PyString_AS_STRING(rc);
565 memset(p, 0, sz);
566 GC_DECRYPT(GCIPHER_C(me), p, p, sz);
567 return (rc);
568 }
569
570 static PyObject *gcmeth_setiv(PyObject *me, PyObject *arg)
571 {
572 char *p;
573 Py_ssize_t sz;
574
575 if (!PyArg_ParseTuple(arg, "s#:setiv", &p, &sz)) goto end;
576 if (!GCIPHER_C(me)->ops->setiv) VALERR("`setiv' not supported");
577 if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode");
578 if (sz != GC_CLASS(GCIPHER_C(me))->blksz) VALERR("bad IV length");
579 GC_SETIV(GCIPHER_C(me), p);
580 RETURN_ME;
581 end:
582 return (0);
583 }
584
585 static PyObject *gcmeth_bdry(PyObject *me, PyObject *arg)
586 {
587 if (!PyArg_ParseTuple(arg, ":bdry")) goto end;
588 if (!GCIPHER_C(me)->ops->bdry) VALERR("`bdry' not supported");
589 if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode");
590 GC_BDRY(GCIPHER_C(me));
591 RETURN_ME;
592 end:
593 return (0);
594 }
595
596 static PyGetSetDef gccipher_pygetset[] = {
597 #define GETSETNAME(op, name) gcc##op##_##name
598 GET (keysz, "CC.keysz -> acceptable key sizes")
599 GET (blksz, "CC.blksz -> block size, or zero")
600 GET (name, "CC.name -> name of this kind of cipher")
601 #undef GETSETNAME
602 { 0 }
603 };
604
605 static PyMethodDef gcipher_pymethods[] = {
606 #define METHNAME(name) gcmeth_##name
607 METH (encrypt, "C.encrypt(PT) -> CT")
608 METH (enczero, "C.enczero(N) -> CT")
609 METH (decrypt, "C.decrypt(CT) -> PT")
610 METH (deczero, "C.deczero(N) -> PT")
611 METH (setiv, "C.setiv(IV)")
612 METH (bdry, "C.bdry()")
613 #undef METHNAME
614 { 0 }
615 };
616
617 static PyTypeObject gccipher_pytype_skel = {
618 PyObject_HEAD_INIT(0) 0, /* Header */
619 "GCCipher", /* @tp_name@ */
620 sizeof(gccipher_pyobj), /* @tp_basicsize@ */
621 0, /* @tp_itemsize@ */
622
623 0, /* @tp_dealloc@ */
624 0, /* @tp_print@ */
625 0, /* @tp_getattr@ */
626 0, /* @tp_setattr@ */
627 0, /* @tp_compare@ */
628 0, /* @tp_repr@ */
629 0, /* @tp_as_number@ */
630 0, /* @tp_as_sequence@ */
631 0, /* @tp_as_mapping@ */
632 0, /* @tp_hash@ */
633 0, /* @tp_call@ */
634 0, /* @tp_str@ */
635 0, /* @tp_getattro@ */
636 0, /* @tp_setattro@ */
637 0, /* @tp_as_buffer@ */
638 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
639 Py_TPFLAGS_BASETYPE,
640
641 /* @tp_doc@ */
642 "Symmetric cipher metaclass.",
643
644 0, /* @tp_traverse@ */
645 0, /* @tp_clear@ */
646 0, /* @tp_richcompare@ */
647 0, /* @tp_weaklistoffset@ */
648 0, /* @tp_iter@ */
649 0, /* @tp_iternext@ */
650 0, /* @tp_methods@ */
651 0, /* @tp_members@ */
652 gccipher_pygetset, /* @tp_getset@ */
653 0, /* @tp_base@ */
654 0, /* @tp_dict@ */
655 0, /* @tp_descr_get@ */
656 0, /* @tp_descr_set@ */
657 0, /* @tp_dictoffset@ */
658 0, /* @tp_init@ */
659 PyType_GenericAlloc, /* @tp_alloc@ */
660 abstract_pynew, /* @tp_new@ */
661 0, /* @tp_free@ */
662 0 /* @tp_is_gc@ */
663 };
664
665 static PyTypeObject gcipher_pytype_skel = {
666 PyObject_HEAD_INIT(0) 0, /* Header */
667 "GCipher", /* @tp_name@ */
668 sizeof(gcipher_pyobj), /* @tp_basicsize@ */
669 0, /* @tp_itemsize@ */
670
671 gcipher_pydealloc, /* @tp_dealloc@ */
672 0, /* @tp_print@ */
673 0, /* @tp_getattr@ */
674 0, /* @tp_setattr@ */
675 0, /* @tp_compare@ */
676 0, /* @tp_repr@ */
677 0, /* @tp_as_number@ */
678 0, /* @tp_as_sequence@ */
679 0, /* @tp_as_mapping@ */
680 0, /* @tp_hash@ */
681 0, /* @tp_call@ */
682 0, /* @tp_str@ */
683 0, /* @tp_getattro@ */
684 0, /* @tp_setattro@ */
685 0, /* @tp_as_buffer@ */
686 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
687 Py_TPFLAGS_BASETYPE,
688
689 /* @tp_doc@ */
690 "Symmetric cipher, abstract base class.",
691
692 0, /* @tp_traverse@ */
693 0, /* @tp_clear@ */
694 0, /* @tp_richcompare@ */
695 0, /* @tp_weaklistoffset@ */
696 0, /* @tp_iter@ */
697 0, /* @tp_iternext@ */
698 gcipher_pymethods, /* @tp_methods@ */
699 0, /* @tp_members@ */
700 0, /* @tp_getset@ */
701 0, /* @tp_base@ */
702 0, /* @tp_dict@ */
703 0, /* @tp_descr_get@ */
704 0, /* @tp_descr_set@ */
705 0, /* @tp_dictoffset@ */
706 0, /* @tp_init@ */
707 PyType_GenericAlloc, /* @tp_alloc@ */
708 abstract_pynew, /* @tp_new@ */
709 0, /* @tp_free@ */
710 0 /* @tp_is_gc@ */
711 };
712
713 /*----- Hash functions ----------------------------------------------------*/
714
715 PyTypeObject *gchash_pytype, *ghash_pytype;
716
717 CONVFUNC(gchash, gchash *, GCHASH_CH)
718 CONVFUNC(ghash, ghash *, GHASH_H)
719
720 static PyObject *ghash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
721 {
722 static const char *const kwlist[] = { 0 };
723 if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", KWLIST))
724 goto end;
725 return (ghash_pywrap((PyObject *)ty, GH_INIT(GCHASH_CH(ty))));
726 end:
727 return (0);
728 }
729
730 PyObject *gchash_pywrap(gchash *ch)
731 {
732 gchash_pyobj *g = newtype(gchash_pytype, 0, ch->name);
733 g->ch = ch;
734 g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj);
735 g->ty.ht_type.tp_base = ghash_pytype;
736 Py_INCREF(ghash_pytype);
737 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
738 Py_TPFLAGS_BASETYPE |
739 Py_TPFLAGS_HEAPTYPE);
740 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
741 g->ty.ht_type.tp_free = 0;
742 g->ty.ht_type.tp_new = ghash_pynew;
743 typeready(&g->ty.ht_type);
744 return ((PyObject *)g);
745 }
746
747 PyObject *ghash_pywrap(PyObject *cobj, ghash *h)
748 {
749 ghash_pyobj *g;
750 if (!cobj) cobj = gchash_pywrap((/*unconst*/ gchash *)GH_CLASS(h));
751 else Py_INCREF(cobj);
752 g = PyObject_NEW(ghash_pyobj, (PyTypeObject *)cobj);
753 g->h = h;
754 return ((PyObject *)g);
755 }
756
757 static void ghash_pydealloc(PyObject *me)
758 {
759 GH_DESTROY(GHASH_H(me));
760 Py_DECREF(me->ob_type);
761 FREEOBJ(me);
762 }
763
764 static PyObject *gchget_name(PyObject *me, void *hunoz)
765 { return (PyString_FromString(GCHASH_CH(me)->name)); }
766
767 static PyObject *gchget_hashsz(PyObject *me, void *hunoz)
768 { return (PyInt_FromLong(GCHASH_CH(me)->hashsz)); }
769
770 static PyObject *gchget_bufsz(PyObject *me, void *hunoz)
771 { return (PyInt_FromLong(GCHASH_CH(me)->bufsz)); }
772
773 static PyObject *ghmeth_copy(PyObject *me, PyObject *arg)
774 {
775 if (!PyArg_ParseTuple(arg, ":copy")) return (0);
776 return (ghash_pywrap((PyObject *)me->ob_type, GH_COPY(GHASH_H(me))));
777 }
778
779 static PyObject *ghmeth_hash(PyObject *me, PyObject *arg)
780 {
781 char *p;
782 Py_ssize_t sz;
783 if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0);
784 GH_HASH(GHASH_H(me), p, sz);
785 RETURN_ME;
786 }
787
788 #define GHMETH_HASHU_(n, W, w) \
789 static PyObject *ghmeth_hashu##w(PyObject *me, PyObject *arg) \
790 { \
791 uint##n x; \
792 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \
793 GH_HASHU##W(GHASH_H(me), x); \
794 RETURN_ME; \
795 }
796 DOUINTCONV(GHMETH_HASHU_)
797
798 #define GHMETH_HASHBUF_(n, W, w) \
799 static PyObject *ghmeth_hashbuf##w(PyObject *me, PyObject *arg) \
800 { \
801 char *p; \
802 Py_ssize_t sz; \
803 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
804 if (sz > MASK##n) TYERR("string too long"); \
805 GH_HASHBUF##W(GHASH_H(me), p, sz); \
806 RETURN_ME; \
807 end: \
808 return (0); \
809 }
810 DOUINTCONV(GHMETH_HASHBUF_)
811
812 static PyObject *ghmeth_hashstrz(PyObject *me, PyObject *arg)
813 {
814 char *p;
815 if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0);
816 GH_HASHSTRZ(GHASH_H(me), p);
817 RETURN_ME;
818 }
819
820 static PyObject *ghmeth_done(PyObject *me, PyObject *arg)
821 {
822 ghash *g;
823 PyObject *rc;
824 if (!PyArg_ParseTuple(arg, ":done")) return (0);
825 g = GH_COPY(GHASH_H(me));
826 rc = bytestring_pywrap(0, g->ops->c->hashsz);
827 GH_DONE(g, PyString_AS_STRING(rc));
828 GH_DESTROY(g);
829 return (rc);
830 }
831
832 static PyGetSetDef gchash_pygetset[] = {
833 #define GETSETNAME(op, name) gch##op##_##name
834 GET (bufsz, "CH.bufsz -> hash buffer size, or zero")
835 GET (hashsz, "CH.hashsz -> hash output size")
836 GET (name, "CH.name -> name of this kind of hash")
837 #undef GETSETNAME
838 { 0 }
839 };
840
841 static PyMethodDef ghash_pymethods[] = {
842 #define METHNAME(name) ghmeth_##name
843 METH (copy, "H.copy() -> HH")
844 METH (hash, "H.hash(M)")
845 #define METHU_(n, W, w) METH(hashu##w, "H.hashu" #w "(WORD)")
846 DOUINTCONV(METHU_)
847 #undef METHU_
848 #define METHBUF_(n, W, w) METH(hashbuf##w, "H.hashbuf" #w "(BYTES)")
849 DOUINTCONV(METHBUF_)
850 #undef METHBUF_
851 METH (hashstrz, "H.hashstrz(STRING)")
852 METH (done, "H.done() -> HASH")
853 #undef METHNAME
854 { 0 }
855 };
856
857 static PyTypeObject gchash_pytype_skel = {
858 PyObject_HEAD_INIT(0) 0, /* Header */
859 "GCHash", /* @tp_name@ */
860 sizeof(gchash_pyobj), /* @tp_basicsize@ */
861 0, /* @tp_itemsize@ */
862
863 0, /* @tp_dealloc@ */
864 0, /* @tp_print@ */
865 0, /* @tp_getattr@ */
866 0, /* @tp_setattr@ */
867 0, /* @tp_compare@ */
868 0, /* @tp_repr@ */
869 0, /* @tp_as_number@ */
870 0, /* @tp_as_sequence@ */
871 0, /* @tp_as_mapping@ */
872 0, /* @tp_hash@ */
873 0, /* @tp_call@ */
874 0, /* @tp_str@ */
875 0, /* @tp_getattro@ */
876 0, /* @tp_setattro@ */
877 0, /* @tp_as_buffer@ */
878 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
879 Py_TPFLAGS_BASETYPE,
880
881 /* @tp_doc@ */
882 "Hash function metaclass.",
883
884 0, /* @tp_traverse@ */
885 0, /* @tp_clear@ */
886 0, /* @tp_richcompare@ */
887 0, /* @tp_weaklistoffset@ */
888 0, /* @tp_iter@ */
889 0, /* @tp_iternext@ */
890 0, /* @tp_methods@ */
891 0, /* @tp_members@ */
892 gchash_pygetset, /* @tp_getset@ */
893 0, /* @tp_base@ */
894 0, /* @tp_dict@ */
895 0, /* @tp_descr_get@ */
896 0, /* @tp_descr_set@ */
897 0, /* @tp_dictoffset@ */
898 0, /* @tp_init@ */
899 PyType_GenericAlloc, /* @tp_alloc@ */
900 abstract_pynew, /* @tp_new@ */
901 0, /* @tp_free@ */
902 0 /* @tp_is_gc@ */
903 };
904
905 static PyTypeObject ghash_pytype_skel = {
906 PyObject_HEAD_INIT(0) 0, /* Header */
907 "GHash", /* @tp_name@ */
908 sizeof(ghash_pyobj), /* @tp_basicsize@ */
909 0, /* @tp_itemsize@ */
910
911 ghash_pydealloc, /* @tp_dealloc@ */
912 0, /* @tp_print@ */
913 0, /* @tp_getattr@ */
914 0, /* @tp_setattr@ */
915 0, /* @tp_compare@ */
916 0, /* @tp_repr@ */
917 0, /* @tp_as_number@ */
918 0, /* @tp_as_sequence@ */
919 0, /* @tp_as_mapping@ */
920 0, /* @tp_hash@ */
921 0, /* @tp_call@ */
922 0, /* @tp_str@ */
923 0, /* @tp_getattro@ */
924 0, /* @tp_setattro@ */
925 0, /* @tp_as_buffer@ */
926 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
927 Py_TPFLAGS_BASETYPE,
928
929 /* @tp_doc@ */
930 "Hash function, abstract base class.",
931
932 0, /* @tp_traverse@ */
933 0, /* @tp_clear@ */
934 0, /* @tp_richcompare@ */
935 0, /* @tp_weaklistoffset@ */
936 0, /* @tp_iter@ */
937 0, /* @tp_iternext@ */
938 ghash_pymethods, /* @tp_methods@ */
939 0, /* @tp_members@ */
940 0, /* @tp_getset@ */
941 0, /* @tp_base@ */
942 0, /* @tp_dict@ */
943 0, /* @tp_descr_get@ */
944 0, /* @tp_descr_set@ */
945 0, /* @tp_dictoffset@ */
946 0, /* @tp_init@ */
947 PyType_GenericAlloc, /* @tp_alloc@ */
948 abstract_pynew, /* @tp_new@ */
949 0, /* @tp_free@ */
950 0 /* @tp_is_gc@ */
951 };
952
953 /*----- Message authentication --------------------------------------------*/
954
955 PyTypeObject *gcmac_pytype, *gmac_pytype, *gmhash_pytype;
956
957 CONVFUNC(gcmac, gcmac *, GCMAC_CM)
958 CONVFUNC(gmac, gmac *, GMAC_M)
959 CONVFUNC(gmhash, ghash *, GHASH_H)
960
961 static PyObject *gmac_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
962 {
963 static const char *const kwlist[] = { "k", 0 };
964 char *k;
965 Py_ssize_t sz;
966
967 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &k, &sz))
968 goto end;
969 if (keysz(sz, GCMAC_CM(ty)->keysz) != sz) VALERR("bad key length");
970 return (gmac_pywrap((PyObject *)ty,
971 GM_KEY(GCMAC_CM(ty), k, sz)));
972 end:
973 return (0);
974 }
975
976 static PyObject *gmhash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
977 {
978 static const char *const kwlist[] = { 0 };
979 ghash_pyobj *g;
980
981 if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", KWLIST)) return (0);
982 g = PyObject_NEW(ghash_pyobj, ty);
983 g->h = GM_INIT(GMAC_M(ty));
984 Py_INCREF(ty);
985 return ((PyObject *)g);
986 }
987
988 PyObject *gcmac_pywrap(gcmac *cm)
989 {
990 gcmac_pyobj *g = newtype(gcmac_pytype, 0, cm->name);
991 g->cm = cm;
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);
1003 }
1004
1005 PyObject *gmac_pywrap(PyObject *cobj, gmac *m)
1006 {
1007 gmac_pyobj *g;
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);
1023 g->m = m;
1024 return ((PyObject *)g);
1025 }
1026
1027 static void gmac_pydealloc(PyObject *me)
1028 {
1029 GM_DESTROY(GMAC_M(me));
1030 Py_DECREF(me->ob_type);
1031 PyType_Type.tp_dealloc(me);
1032 }
1033
1034 static PyObject *gcmget_name(PyObject *me, void *hunoz)
1035 { return (PyString_FromString(GCMAC_CM(me)->name)); }
1036
1037 static PyObject *gcmget_keysz(PyObject *me, void *hunoz)
1038 { return (keysz_pywrap(GCMAC_CM(me)->keysz)); }
1039
1040 static PyObject *gcmget_tagsz(PyObject *me, void *hunoz)
1041 { return (PyInt_FromLong(GCMAC_CM(me)->hashsz)); }
1042
1043 static PyGetSetDef gcmac_pygetset[] = {
1044 #define GETSETNAME(op, name) gcm##op##_##name
1045 GET (keysz, "CM.keysz -> acceptable key sizes")
1046 GET (tagsz, "CM.tagsz -> MAC output size")
1047 GET (name, "CM.name -> name of this kind of MAC")
1048 #undef GETSETNAME
1049 { 0 }
1050 };
1051
1052 static PyTypeObject gcmac_pytype_skel = {
1053 PyObject_HEAD_INIT(0) 0, /* Header */
1054 "GCMAC", /* @tp_name@ */
1055 sizeof(gchash_pyobj), /* @tp_basicsize@ */
1056 0, /* @tp_itemsize@ */
1057
1058 0, /* @tp_dealloc@ */
1059 0, /* @tp_print@ */
1060 0, /* @tp_getattr@ */
1061 0, /* @tp_setattr@ */
1062 0, /* @tp_compare@ */
1063 0, /* @tp_repr@ */
1064 0, /* @tp_as_number@ */
1065 0, /* @tp_as_sequence@ */
1066 0, /* @tp_as_mapping@ */
1067 0, /* @tp_hash@ */
1068 0, /* @tp_call@ */
1069 0, /* @tp_str@ */
1070 0, /* @tp_getattro@ */
1071 0, /* @tp_setattro@ */
1072 0, /* @tp_as_buffer@ */
1073 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1074 Py_TPFLAGS_BASETYPE,
1075
1076 /* @tp_doc@ */
1077 "Message authentication code metametaclass.",
1078
1079 0, /* @tp_traverse@ */
1080 0, /* @tp_clear@ */
1081 0, /* @tp_richcompare@ */
1082 0, /* @tp_weaklistoffset@ */
1083 0, /* @tp_iter@ */
1084 0, /* @tp_iternext@ */
1085 0, /* @tp_methods@ */
1086 0, /* @tp_members@ */
1087 gcmac_pygetset, /* @tp_getset@ */
1088 0, /* @tp_base@ */
1089 0, /* @tp_dict@ */
1090 0, /* @tp_descr_get@ */
1091 0, /* @tp_descr_set@ */
1092 0, /* @tp_dictoffset@ */
1093 0, /* @tp_init@ */
1094 PyType_GenericAlloc, /* @tp_alloc@ */
1095 abstract_pynew, /* @tp_new@ */
1096 0, /* @tp_free@ */
1097 0 /* @tp_is_gc@ */
1098 };
1099
1100 static PyTypeObject gmac_pytype_skel = {
1101 PyObject_HEAD_INIT(0) 0, /* Header */
1102 "GMAC", /* @tp_name@ */
1103 sizeof(gmac_pyobj), /* @tp_basicsize@ */
1104 0, /* @tp_itemsize@ */
1105
1106 gmac_pydealloc, /* @tp_dealloc@ */
1107 0, /* @tp_print@ */
1108 0, /* @tp_getattr@ */
1109 0, /* @tp_setattr@ */
1110 0, /* @tp_compare@ */
1111 0, /* @tp_repr@ */
1112 0, /* @tp_as_number@ */
1113 0, /* @tp_as_sequence@ */
1114 0, /* @tp_as_mapping@ */
1115 0, /* @tp_hash@ */
1116 0, /* @tp_call@ */
1117 0, /* @tp_str@ */
1118 0, /* @tp_getattro@ */
1119 0, /* @tp_setattro@ */
1120 0, /* @tp_as_buffer@ */
1121 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1122 Py_TPFLAGS_BASETYPE,
1123
1124 /* @tp_doc@ */
1125 "Message authentication code metaclass, abstract base class.",
1126
1127 0, /* @tp_traverse@ */
1128 0, /* @tp_clear@ */
1129 0, /* @tp_richcompare@ */
1130 0, /* @tp_weaklistoffset@ */
1131 0, /* @tp_iter@ */
1132 0, /* @tp_iternext@ */
1133 0, /* @tp_methods@ */
1134 0, /* @tp_members@ */
1135 0, /* @tp_getset@ */
1136 0, /* @tp_base@ */
1137 0, /* @tp_dict@ */
1138 0, /* @tp_descr_get@ */
1139 0, /* @tp_descr_set@ */
1140 0, /* @tp_dictoffset@ */
1141 0, /* @tp_init@ */
1142 PyType_GenericAlloc, /* @tp_alloc@ */
1143 abstract_pynew, /* @tp_new@ */
1144 0, /* @tp_free@ */
1145 0 /* @tp_is_gc@ */
1146 };
1147
1148 static PyTypeObject gmhash_pytype_skel = {
1149 PyObject_HEAD_INIT(0) 0, /* Header */
1150 "GMACHash", /* @tp_name@ */
1151 sizeof(ghash_pyobj), /* @tp_basicsize@ */
1152 0, /* @tp_itemsize@ */
1153
1154 ghash_pydealloc, /* @tp_dealloc@ */
1155 0, /* @tp_print@ */
1156 0, /* @tp_getattr@ */
1157 0, /* @tp_setattr@ */
1158 0, /* @tp_compare@ */
1159 0, /* @tp_repr@ */
1160 0, /* @tp_as_number@ */
1161 0, /* @tp_as_sequence@ */
1162 0, /* @tp_as_mapping@ */
1163 0, /* @tp_hash@ */
1164 0, /* @tp_call@ */
1165 0, /* @tp_str@ */
1166 0, /* @tp_getattro@ */
1167 0, /* @tp_setattro@ */
1168 0, /* @tp_as_buffer@ */
1169 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1170 Py_TPFLAGS_BASETYPE,
1171
1172 /* @tp_doc@ */
1173 "Message authentication code, abstract base class.",
1174
1175 0, /* @tp_traverse@ */
1176 0, /* @tp_clear@ */
1177 0, /* @tp_richcompare@ */
1178 0, /* @tp_weaklistoffset@ */
1179 0, /* @tp_iter@ */
1180 0, /* @tp_iternext@ */
1181 0, /* @tp_methods@ */
1182 0, /* @tp_members@ */
1183 0, /* @tp_getset@ */
1184 0, /* @tp_base@ */
1185 0, /* @tp_dict@ */
1186 0, /* @tp_descr_get@ */
1187 0, /* @tp_descr_set@ */
1188 0, /* @tp_dictoffset@ */
1189 0, /* @tp_init@ */
1190 PyType_GenericAlloc, /* @tp_alloc@ */
1191 abstract_pynew, /* @tp_new@ */
1192 0, /* @tp_free@ */
1193 0 /* @tp_is_gc@ */
1194 };
1195
1196 /*----- Special snowflake for Poly1305 ------------------------------------*/
1197
1198 PyTypeObject *poly1305cls_pytype, *poly1305key_pytype, *poly1305hash_pytype;
1199
1200 typedef struct poly1305key_pyobj {
1201 PyHeapTypeObject ty;
1202 poly1305_key k;
1203 } poly1305key_pyobj;
1204
1205 typedef struct poly1305hash_pyobj {
1206 PyObject_HEAD
1207 unsigned f;
1208 #define f_mask 1u
1209 poly1305_ctx ctx;
1210 } poly1305hash_pyobj;
1211
1212 #define P1305_F(o) (((poly1305hash_pyobj *)(o))->f)
1213 #define P1305_CTX(o) (&((poly1305hash_pyobj *)(o))->ctx)
1214 CONVFUNC(poly1305hash, poly1305_ctx *, P1305_CTX)
1215
1216 static PyObject *poly1305hash_pynew(PyTypeObject *ty,
1217 PyObject *arg, PyObject *kw)
1218 {
1219 static const char *const kwlist[] = { "mask", 0 };
1220 poly1305key_pyobj *pk = (poly1305key_pyobj *)ty;
1221 poly1305hash_pyobj *ph;
1222 char *m = 0;
1223 Py_ssize_t sz;
1224
1225 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|s#:new", KWLIST, &m, &sz))
1226 return (0);
1227 if (m && sz != POLY1305_MASKSZ) VALERR("bad mask length");
1228 ph = PyObject_NEW(poly1305hash_pyobj, ty);
1229 ph->f = 0;
1230 if (m) ph->f |= f_mask;
1231 poly1305_macinit(&ph->ctx, &pk->k, m);
1232 Py_INCREF(ty);
1233 return ((PyObject *)ph);
1234 end:
1235 return (0);
1236 }
1237
1238 static PyObject *poly1305key_pynew(PyTypeObject *ty,
1239 PyObject *arg, PyObject *kw)
1240 {
1241 static const char *const kwlist[] = { "k", 0 };
1242 poly1305key_pyobj *pk;
1243 char *k;
1244 Py_ssize_t sz;
1245
1246 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &k, &sz))
1247 goto end;
1248 if (keysz(sz, poly1305_keysz) != sz) VALERR("bad key length");
1249
1250 pk = newtype(ty, 0, 0);
1251 pk->ty.ht_name = PyString_FromString("poly1305(keyed)");
1252 pk->ty.ht_type.tp_basicsize = sizeof(poly1305hash_pyobj);
1253 pk->ty.ht_type.tp_name = PyString_AS_STRING(pk->ty.ht_name);
1254 pk->ty.ht_type.tp_base = poly1305hash_pytype;
1255 Py_INCREF(poly1305key_pytype);
1256 pk->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
1257 Py_TPFLAGS_BASETYPE |
1258 Py_TPFLAGS_HEAPTYPE);
1259 pk->ty.ht_type.tp_alloc = PyType_GenericAlloc;
1260 pk->ty.ht_type.tp_free = 0;
1261 pk->ty.ht_type.tp_new = poly1305hash_pynew;
1262 typeready(&pk->ty.ht_type);
1263
1264 poly1305_keyinit(&pk->k, k, sz);
1265 return ((PyObject *)pk);
1266
1267 end:
1268 return (0);
1269 }
1270
1271 static PyObject *poly1305clsget_name(PyObject *me, void *hunoz)
1272 { return (PyString_FromString("poly1305")); }
1273
1274 static PyObject *poly1305clsget_keysz(PyObject *me, void *hunoz)
1275 { return (keysz_pywrap(poly1305_keysz)); }
1276
1277 static PyObject *poly1305clsget_masksz(PyObject *me, void *hunoz)
1278 { return (PyInt_FromLong(POLY1305_MASKSZ)); }
1279
1280 static PyObject *poly1305clsget_tagsz(PyObject *me, void *hunoz)
1281 { return (PyInt_FromLong(POLY1305_TAGSZ)); }
1282
1283 static PyObject *polymeth_copy(PyObject *me, PyObject *arg)
1284 {
1285 poly1305hash_pyobj *ph;
1286 if (!PyArg_ParseTuple(arg, ":copy")) return (0);
1287 ph = PyObject_NEW(poly1305hash_pyobj, me->ob_type);
1288 poly1305_copy(&ph->ctx, P1305_CTX(me));
1289 Py_INCREF(me->ob_type);
1290 return ((PyObject *)ph);
1291 }
1292
1293 static PyObject *polymeth_hash(PyObject *me, PyObject *arg)
1294 {
1295 char *p;
1296 Py_ssize_t sz;
1297 if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0);
1298 poly1305_hash(P1305_CTX(me), p, sz);
1299 RETURN_ME;
1300 }
1301
1302 #define POLYMETH_HASHU_(n, W, w) \
1303 static PyObject *polymeth_hashu##w(PyObject *me, PyObject *arg) \
1304 { \
1305 uint##n x; \
1306 octet b[SZ_##W]; \
1307 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \
1308 STORE##W(b, x); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \
1309 RETURN_ME; \
1310 }
1311 DOUINTCONV(POLYMETH_HASHU_)
1312
1313 #define POLYMETH_HASHBUF_(n, W, w) \
1314 static PyObject *polymeth_hashbuf##w(PyObject *me, PyObject *arg) \
1315 { \
1316 char *p; \
1317 Py_ssize_t sz; \
1318 octet b[SZ_##W]; \
1319 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
1320 if (sz > MASK##n) TYERR("string too long"); \
1321 STORE##W(b, sz); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \
1322 poly1305_hash(P1305_CTX(me), p, sz); \
1323 RETURN_ME; \
1324 end: \
1325 return (0); \
1326 }
1327 DOUINTCONV(POLYMETH_HASHBUF_)
1328
1329 static PyObject *polymeth_hashstrz(PyObject *me, PyObject *arg)
1330 {
1331 char *p;
1332 if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0);
1333 poly1305_hash(P1305_CTX(me), p, strlen(p) + 1);
1334 RETURN_ME;
1335 }
1336
1337 static PyObject *polymeth_flush(PyObject *me, PyObject *arg)
1338 {
1339 if (!PyArg_ParseTuple(arg, ":flush")) return (0);
1340 poly1305_flush(P1305_CTX(me));
1341 RETURN_ME;
1342 }
1343
1344 static PyObject *polymeth_flushzero(PyObject *me, PyObject *arg)
1345 {
1346 if (!PyArg_ParseTuple(arg, ":flushzero")) return (0);
1347 poly1305_flushzero(P1305_CTX(me));
1348 RETURN_ME;
1349 }
1350
1351 static PyObject *polymeth_concat(PyObject *me, PyObject *arg)
1352 {
1353 PyObject *pre, *suff;
1354 if (!PyArg_ParseTuple(arg, "OO:concat", &pre, &suff)) return (0);
1355 if (!PyObject_TypeCheck(pre, poly1305hash_pytype) ||
1356 !PyObject_TypeCheck(suff, poly1305hash_pytype))
1357 TYERR("wanted a poly1305hash");
1358 if (me->ob_type != pre->ob_type || me->ob_type != suff->ob_type)
1359 TYERR("key mismatch");
1360 if (P1305_CTX(pre)->nbuf) VALERR("prefix is not block-aligned");
1361 poly1305_concat(P1305_CTX(me), P1305_CTX(pre), P1305_CTX(suff));
1362 RETURN_ME;
1363 end:
1364 return (0);
1365 }
1366
1367 static PyObject *polymeth_done(PyObject *me, PyObject *arg)
1368 {
1369 PyObject *rc;
1370 if (!PyArg_ParseTuple(arg, ":done")) return (0);
1371 if (!(P1305_F(me) & f_mask)) VALERR("no mask");
1372 rc = bytestring_pywrap(0, POLY1305_TAGSZ);
1373 poly1305_done(P1305_CTX(me), PyString_AS_STRING(rc));
1374 return (rc);
1375 end:
1376 return (0);
1377 }
1378
1379 static PyGetSetDef poly1305cls_pygetset[] = {
1380 #define GETSETNAME(op, name) poly1305cls##op##_##name
1381 GET (keysz, "PC.keysz -> acceptable key sizes")
1382 GET (masksz, "PC.masksz -> mask size")
1383 GET (tagsz, "PC.tagsz -> MAC output size")
1384 GET (name, "PC.name -> name of this kind of MAC")
1385 #undef GETSETNAME
1386 { 0 }
1387 };
1388
1389 static PyMethodDef poly1305hash_pymethods[] = {
1390 #define METHNAME(name) polymeth_##name
1391 METH (copy, "P.copy() -> PP")
1392 METH (hash, "P.hash(M)")
1393 #define METHU_(n, W, w) METH(hashu##w, "P.hashu" #w "(WORD)")
1394 DOUINTCONV(METHU_)
1395 #undef METHU_
1396 #define METHBUF_(n, W, w) METH(hashbuf##w, "P.hashbuf" #w "(BYTES)")
1397 DOUINTCONV(METHBUF_)
1398 #undef METHBUF_
1399 METH (hashstrz, "P.hashstrz(STRING)")
1400 METH (flush, "P.flush()")
1401 METH (flushzero, "P.flushzero()")
1402 METH (concat, "P.concat(PREFIX, SUFFIX)")
1403 METH (done, "P.done() -> TAG")
1404 #undef METHNAME
1405 { 0 }
1406 };
1407
1408 static PyTypeObject poly1305cls_pytype_skel = {
1409 PyObject_HEAD_INIT(0) 0, /* Header */
1410 "Poly1305Class", /* @tp_name@ */
1411 sizeof(PyHeapTypeObject), /* @tp_basicsize@ */
1412 0, /* @tp_itemsize@ */
1413
1414 0, /* @tp_dealloc@ */
1415 0, /* @tp_print@ */
1416 0, /* @tp_getattr@ */
1417 0, /* @tp_setattr@ */
1418 0, /* @tp_compare@ */
1419 0, /* @tp_repr@ */
1420 0, /* @tp_as_number@ */
1421 0, /* @tp_as_sequence@ */
1422 0, /* @tp_as_mapping@ */
1423 0, /* @tp_hash@ */
1424 0, /* @tp_call@ */
1425 0, /* @tp_str@ */
1426 0, /* @tp_getattro@ */
1427 0, /* @tp_setattro@ */
1428 0, /* @tp_as_buffer@ */
1429 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1430 Py_TPFLAGS_BASETYPE,
1431
1432 /* @tp_doc@ */
1433 "Poly1305 metametaclass. Best not to ask.",
1434
1435 0, /* @tp_traverse@ */
1436 0, /* @tp_clear@ */
1437 0, /* @tp_richcompare@ */
1438 0, /* @tp_weaklistoffset@ */
1439 0, /* @tp_iter@ */
1440 0, /* @tp_iternext@ */
1441 0, /* @tp_methods@ */
1442 0, /* @tp_members@ */
1443 poly1305cls_pygetset, /* @tp_getset@ */
1444 0, /* @tp_base@ */
1445 0, /* @tp_dict@ */
1446 0, /* @tp_descr_get@ */
1447 0, /* @tp_descr_set@ */
1448 0, /* @tp_dictoffset@ */
1449 0, /* @tp_init@ */
1450 PyType_GenericAlloc, /* @tp_alloc@ */
1451 abstract_pynew, /* @tp_new@ */
1452 0, /* @tp_free@ */
1453 0 /* @tp_is_gc@ */
1454 };
1455
1456 static PyTypeObject poly1305key_pytype_skel = {
1457 PyObject_HEAD_INIT(0) 0, /* Header */
1458 "poly1305", /* @tp_name@ */
1459 sizeof(poly1305key_pyobj), /* @tp_basicsize@ */
1460 0, /* @tp_itemsize@ */
1461
1462 0, /* @tp_dealloc@ */
1463 0, /* @tp_print@ */
1464 0, /* @tp_getattr@ */
1465 0, /* @tp_setattr@ */
1466 0, /* @tp_compare@ */
1467 0, /* @tp_repr@ */
1468 0, /* @tp_as_number@ */
1469 0, /* @tp_as_sequence@ */
1470 0, /* @tp_as_mapping@ */
1471 0, /* @tp_hash@ */
1472 0, /* @tp_call@ */
1473 0, /* @tp_str@ */
1474 0, /* @tp_getattro@ */
1475 0, /* @tp_setattro@ */
1476 0, /* @tp_as_buffer@ */
1477 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1478 Py_TPFLAGS_BASETYPE,
1479
1480 /* @tp_doc@ */
1481 "poly1305(K): Poly1305 key.",
1482
1483 0, /* @tp_traverse@ */
1484 0, /* @tp_clear@ */
1485 0, /* @tp_richcompare@ */
1486 0, /* @tp_weaklistoffset@ */
1487 0, /* @tp_iter@ */
1488 0, /* @tp_iternext@ */
1489 0, /* @tp_methods@ */
1490 0, /* @tp_members@ */
1491 0, /* @tp_getset@ */
1492 0, /* @tp_base@ */
1493 0, /* @tp_dict@ */
1494 0, /* @tp_descr_get@ */
1495 0, /* @tp_descr_set@ */
1496 0, /* @tp_dictoffset@ */
1497 0, /* @tp_init@ */
1498 PyType_GenericAlloc, /* @tp_alloc@ */
1499 poly1305key_pynew, /* @tp_new@ */
1500 0, /* @tp_free@ */
1501 0 /* @tp_is_gc@ */
1502 };
1503
1504 static PyTypeObject poly1305hash_pytype_skel = {
1505 PyObject_HEAD_INIT(0) 0, /* Header */
1506 "Poly1305Hash", /* @tp_name@ */
1507 sizeof(poly1305hash_pyobj), /* @tp_basicsize@ */
1508 0, /* @tp_itemsize@ */
1509
1510 0, /* @tp_dealloc@ */
1511 0, /* @tp_print@ */
1512 0, /* @tp_getattr@ */
1513 0, /* @tp_setattr@ */
1514 0, /* @tp_compare@ */
1515 0, /* @tp_repr@ */
1516 0, /* @tp_as_number@ */
1517 0, /* @tp_as_sequence@ */
1518 0, /* @tp_as_mapping@ */
1519 0, /* @tp_hash@ */
1520 0, /* @tp_call@ */
1521 0, /* @tp_str@ */
1522 0, /* @tp_getattro@ */
1523 0, /* @tp_setattro@ */
1524 0, /* @tp_as_buffer@ */
1525 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1526 Py_TPFLAGS_BASETYPE,
1527
1528 /* @tp_doc@ */
1529 "Poly1305 MAC context base class.",
1530
1531 0, /* @tp_traverse@ */
1532 0, /* @tp_clear@ */
1533 0, /* @tp_richcompare@ */
1534 0, /* @tp_weaklistoffset@ */
1535 0, /* @tp_iter@ */
1536 0, /* @tp_iternext@ */
1537 poly1305hash_pymethods, /* @tp_methods@ */
1538 0, /* @tp_members@ */
1539 0, /* @tp_getset@ */
1540 0, /* @tp_base@ */
1541 0, /* @tp_dict@ */
1542 0, /* @tp_descr_get@ */
1543 0, /* @tp_descr_set@ */
1544 0, /* @tp_dictoffset@ */
1545 0, /* @tp_init@ */
1546 PyType_GenericAlloc, /* @tp_alloc@ */
1547 abstract_pynew, /* @tp_new@ */
1548 0, /* @tp_free@ */
1549 0 /* @tp_is_gc@ */
1550 };
1551
1552 /*----- Special snowflake for HSalsa and HChaCha --------------------------*/
1553
1554 #define DEF_HDANCE(DANCE, HDANCE, dance, hdance) \
1555 static PyObject *meth_##hdance##_prf(PyObject *me, PyObject *arg) \
1556 { \
1557 dance##_ctx dance; \
1558 char *k, *n; \
1559 Py_ssize_t ksz, nsz; \
1560 PyObject *rc; \
1561 if (!PyArg_ParseTuple(arg, "s#s#:" #hdance "_prf", \
1562 &k, &ksz, &n, &nsz)) \
1563 goto end; \
1564 if (ksz != DANCE##_KEYSZ) VALERR("bad key length"); \
1565 if (nsz != HDANCE##_INSZ) VALERR("bad input length"); \
1566 rc = bytestring_pywrap(0, HSALSA20_OUTSZ); \
1567 dance##_init(&dance, k, ksz, 0); \
1568 hdance##_prf(&dance, n, PyString_AS_STRING(rc)); \
1569 return (rc); \
1570 end: \
1571 return (0); \
1572 }
1573
1574 DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa20)
1575 DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa2012)
1576 DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa208)
1577
1578 DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha20)
1579 DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha12)
1580 DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha8)
1581
1582 /*----- Keccak-p[1600, n] -------------------------------------------------*/
1583
1584 static PyTypeObject *kxvik_pytype;
1585
1586 typedef struct kxvik_pyobj {
1587 PyObject_HEAD
1588 keccak1600_state s;
1589 unsigned n;
1590 } kxvik_pyobj;
1591
1592 static PyObject *kxvik_pynew(PyTypeObject *ty,
1593 PyObject *arg, PyObject *kw)
1594 {
1595 unsigned n = 24;
1596 kxvik_pyobj *rc = 0;
1597 static const char *const kwlist[] = { "nround", 0 };
1598 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", KWLIST,
1599 convuint, &n))
1600 goto end;
1601 rc = (kxvik_pyobj *)ty->tp_alloc(ty, 0);
1602 rc->n = n;
1603 keccak1600_init(&rc->s);
1604 end:
1605 return ((PyObject *)rc);
1606 }
1607
1608 static PyObject *kxvikmeth_copy(PyObject *me, PyObject *arg)
1609 {
1610 kxvik_pyobj *k = (kxvik_pyobj *)me, *rc = 0;
1611 if (!PyArg_ParseTuple(arg, ":copy")) goto end;
1612 rc = (kxvik_pyobj *)k->ob_type->tp_alloc(k->ob_type, 0);
1613 rc->s = k->s; rc->n = k->n;
1614 end:
1615 return ((PyObject *)rc);
1616 }
1617
1618 static PyObject *kxvikmeth_mix(PyObject *me, PyObject *arg)
1619 {
1620 kxvik_pyobj *k = (kxvik_pyobj *)me;
1621 kludge64 t[25];
1622 const octet *q;
1623 octet buf[8];
1624 unsigned i;
1625 char *p; Py_ssize_t n;
1626
1627 if (!PyArg_ParseTuple(arg, "s#:mix", &p, &n)) goto end;
1628 if (n > 200) VALERR("out of range");
1629 q = (const octet *)p;
1630 i = 0;
1631 while (n > 8) { LOAD64_L_(t[i], q); i++; q += 8; n -= 8; }
1632 if (n) {
1633 memcpy(buf, q, n); memset(buf + n, 0, 8 - n);
1634 LOAD64_L_(t[i], buf); i++;
1635 }
1636 keccak1600_mix(&k->s, t, i);
1637 RETURN_ME;
1638 end:
1639 return (0);
1640 }
1641
1642 static PyObject *kxvikmeth_extract(PyObject *me, PyObject *arg)
1643 {
1644 kxvik_pyobj *k = (kxvik_pyobj *)me;
1645 PyObject *rc = 0;
1646 kludge64 t[25];
1647 octet *q, buf[8];
1648 unsigned i;
1649 unsigned n;
1650
1651 if (!PyArg_ParseTuple(arg, "O&:mix", convuint, &n)) goto end;
1652 if (n > 200) VALERR("out of range");
1653 rc = bytestring_pywrap(0, n);
1654 q = (octet *)PyString_AS_STRING(rc);
1655 keccak1600_extract(&k->s, t, (n + 7)/8);
1656 i = 0;
1657 while (n > 8) { STORE64_L_(q, t[i]); i++; q += 8; n -= 8; }
1658 if (n) { STORE64_L_(buf, t[i]); memcpy(q, buf, n); }
1659 end:
1660 return (rc);
1661 }
1662
1663 static PyObject *kxvikmeth_step(PyObject *me, PyObject *arg)
1664 {
1665 kxvik_pyobj *k = (kxvik_pyobj *)me;
1666 if (!PyArg_ParseTuple(arg, ":step")) return (0);
1667 keccak1600_p(&k->s, &k->s, k->n);
1668 RETURN_ME;
1669 }
1670
1671 static PyObject *kxvikget_nround(PyObject *me, void *hunoz)
1672 {
1673 kxvik_pyobj *k = (kxvik_pyobj *)me;
1674 return (PyInt_FromLong(k->n));
1675 }
1676
1677 static int kxvikset_nround(PyObject *me, PyObject *val, void *hunoz)
1678 {
1679 kxvik_pyobj *k = (kxvik_pyobj *)me;
1680 unsigned n;
1681
1682 if (!convuint(val, &n)) return (-1);
1683 k->n = n;
1684 return (0);
1685 }
1686
1687 static PyGetSetDef kxvik_pygetset[] = {
1688 #define GETSETNAME(op, name) kxvik##op##_##name
1689 GETSET(nround, "KECCAK.nround -> number of rounds")
1690 #undef GETSETNAME
1691 { 0 }
1692 };
1693
1694 static PyMethodDef kxvik_pymethods[] = {
1695 #define METHNAME(func) kxvikmeth_##func
1696 METH (copy, "KECCAK.copy() -> KECCAK'")
1697 METH (mix, "KECCAK.mix(DATA)")
1698 METH (extract, "KECCAK.extract(NOCTETS)")
1699 METH (step, "KECCAK.step()")
1700 #undef METHNAME
1701 { 0 }
1702 };
1703
1704 static PyTypeObject kxvik_pytype_skel = {
1705 PyObject_HEAD_INIT(0) 0, /* Header */
1706 "Keccak1600", /* @tp_name@ */
1707 sizeof(kxvik_pyobj), /* @tp_basicsize@ */
1708 0, /* @tp_itemsize@ */
1709
1710 0, /* @tp_dealloc@ */
1711 0, /* @tp_print@ */
1712 0, /* @tp_getattr@ */
1713 0, /* @tp_setattr@ */
1714 0, /* @tp_compare@ */
1715 0, /* @tp_repr@ */
1716 0, /* @tp_as_number@ */
1717 0, /* @tp_as_sequence@ */
1718 0, /* @tp_as_mapping@ */
1719 0, /* @tp_hash@ */
1720 0, /* @tp_call@ */
1721 0, /* @tp_str@ */
1722 0, /* @tp_getattro@ */
1723 0, /* @tp_setattro@ */
1724 0, /* @tp_as_buffer@ */
1725 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1726 Py_TPFLAGS_BASETYPE,
1727
1728 /* @tp_doc@ */
1729 "Keccak1600([nround = 24]): Keccak-p[1600, n] state.",
1730
1731 0, /* @tp_traverse@ */
1732 0, /* @tp_clear@ */
1733 0, /* @tp_richcompare@ */
1734 0, /* @tp_weaklistoffset@ */
1735 0, /* @tp_iter@ */
1736 0, /* @tp_iternext@ */
1737 kxvik_pymethods, /* @tp_methods@ */
1738 0, /* @tp_members@ */
1739 kxvik_pygetset, /* @tp_getset@ */
1740 0, /* @tp_base@ */
1741 0, /* @tp_dict@ */
1742 0, /* @tp_descr_get@ */
1743 0, /* @tp_descr_set@ */
1744 0, /* @tp_dictoffset@ */
1745 0, /* @tp_init@ */
1746 PyType_GenericAlloc, /* @tp_alloc@ */
1747 kxvik_pynew, /* @tp_new@ */
1748 0, /* @tp_free@ */
1749 0 /* @tp_is_gc@ */
1750 };
1751
1752 static PyTypeObject *shake_pytype, *shake128_pytype, *shake256_pytype;
1753
1754 typedef struct shake_pyobj {
1755 PyObject_HEAD
1756 int st;
1757 shake_ctx h;
1758 } shake_pyobj;
1759
1760 #define SHAKE_H(o) (&((shake_pyobj *)(o))->h)
1761 #define SHAKE_ST(o) (((shake_pyobj *)(o))->st)
1762
1763 static PyObject *shake_dopynew(void (*initfn)(shake_ctx *,
1764 const void *, size_t,
1765 const void *, size_t),
1766 PyTypeObject *ty,
1767 PyObject *arg, PyObject *kw)
1768 {
1769 shake_pyobj *rc = 0;
1770 char *p = 0, *f = 0;
1771 Py_ssize_t psz = 0, fsz = 0;
1772 static const char *const kwlist[] = { "perso", "func", 0 };
1773
1774 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|s#s#:new", KWLIST,
1775 &p, &psz, &f, &fsz))
1776 goto end;
1777 rc = (shake_pyobj *)ty->tp_alloc(ty, 0);
1778 initfn(&rc->h, f, fsz, p, psz);
1779 rc->st = 0;
1780 end:
1781 return ((PyObject *)rc);
1782 }
1783
1784 static PyObject *shake128_pynew(PyTypeObject *ty,
1785 PyObject *arg, PyObject *kw)
1786 { return (shake_dopynew(cshake128_init, ty, arg, kw)); }
1787
1788 static PyObject *shake256_pynew(PyTypeObject *ty,
1789 PyObject *arg, PyObject *kw)
1790 { return (shake_dopynew(cshake256_init, ty, arg, kw)); }
1791
1792 static int shake_check(PyObject *me, int st)
1793 {
1794 if (SHAKE_ST(me) != st) VALERR("wrong state");
1795 return (0);
1796 end:
1797 return (-1);
1798 }
1799
1800 static PyObject *shakemeth_hash(PyObject *me, PyObject *arg)
1801 {
1802 char *p;
1803 Py_ssize_t sz;
1804 if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0);
1805 if (shake_check(me, 0)) return (0);
1806 shake_hash(SHAKE_H(me), p, sz);
1807 RETURN_ME;
1808 }
1809
1810 #define SHAKEMETH_HASHU_(n, W, w) \
1811 static PyObject *shakemeth_hashu##w(PyObject *me, PyObject *arg) \
1812 { \
1813 uint##n x; \
1814 octet b[SZ_##W]; \
1815 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \
1816 if (shake_check(me, 0)) return (0); \
1817 STORE##W(b, x); shake_hash(SHAKE_H(me), b, sizeof(b)); \
1818 RETURN_ME; \
1819 }
1820 DOUINTCONV(SHAKEMETH_HASHU_)
1821
1822 #define SHAKEMETH_HASHBUF_(n, W, w) \
1823 static PyObject *shakemeth_hashbuf##w(PyObject *me, PyObject *arg) \
1824 { \
1825 char *p; \
1826 Py_ssize_t sz; \
1827 octet b[SZ_##W]; \
1828 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
1829 if (sz > MASK##n) TYERR("string too long"); \
1830 if (shake_check(me, 0)) goto end; \
1831 STORE##W(b, sz); shake_hash(SHAKE_H(me), b, sizeof(b)); \
1832 shake_hash(SHAKE_H(me), p, sz); \
1833 RETURN_ME; \
1834 end: \
1835 return (0); \
1836 }
1837 DOUINTCONV(SHAKEMETH_HASHBUF_)
1838
1839 static PyObject *shakemeth_hashstrz(PyObject *me, PyObject *arg)
1840 {
1841 char *p;
1842 if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0);
1843 if (shake_check(me, 0)) return (0);
1844 shake_hash(SHAKE_H(me), p, strlen(p) + 1);
1845 RETURN_ME;
1846 }
1847
1848 static PyObject *shakemeth_xof(PyObject *me, PyObject *arg)
1849 {
1850 if (!PyArg_ParseTuple(arg, ":xof")) goto end;
1851 if (shake_check(me, 0)) goto end;
1852 shake_xof(SHAKE_H(me));
1853 SHAKE_ST(me) = 1;
1854 RETURN_ME;
1855 end:
1856 return (0);
1857 }
1858
1859 static PyObject *shakemeth_done(PyObject *me, PyObject *arg)
1860 {
1861 PyObject *rc = 0;
1862 size_t n;
1863 if (!PyArg_ParseTuple(arg, "O&:done", convszt, &n)) goto end;
1864 if (shake_check(me, 0)) goto end;
1865 rc = bytestring_pywrap(0, n);
1866 shake_done(SHAKE_H(me), PyString_AS_STRING(rc), n);
1867 SHAKE_ST(me) = -1;
1868 end:
1869 return (rc);
1870 }
1871
1872 static PyObject *shakemeth_copy(PyObject *me, PyObject *arg)
1873 {
1874 shake_pyobj *rc = 0;
1875
1876 if (!PyArg_ParseTuple(arg, ":copy")) goto end;
1877 rc = PyObject_NEW(shake_pyobj, me->ob_type);
1878 rc->h = *SHAKE_H(me);
1879 rc->st = SHAKE_ST(me);
1880 end:
1881 return ((PyObject *)me);
1882 }
1883
1884 static PyObject *shakemeth_get(PyObject *me, PyObject *arg)
1885 {
1886 PyObject *rc = 0;
1887 size_t sz;
1888
1889 if (!PyArg_ParseTuple(arg, "O&:get", convszt, &sz)) goto end;
1890 if (shake_check(me, 1)) goto end;
1891 rc = bytestring_pywrap(0, sz);
1892 shake_get(SHAKE_H(me), PyString_AS_STRING(rc), sz);
1893 end:
1894 return (rc);
1895 }
1896
1897 static PyObject *shakemeth_mask(PyObject *me, PyObject *arg)
1898 {
1899 PyObject *rc = 0;
1900 char *p; Py_ssize_t sz;
1901
1902 if (!PyArg_ParseTuple(arg, "s#:mask", &p, &sz)) goto end;
1903 if (shake_check(me, 1)) goto end;
1904 rc = bytestring_pywrap(0, sz);
1905 shake_mask(SHAKE_H(me), p, PyString_AS_STRING(rc), sz);
1906 end:
1907 return (rc);
1908 }
1909
1910 static PyObject *shakeget_rate(PyObject *me, void *hunoz)
1911 { return (PyInt_FromLong(SHAKE_H(me)->h.r)); }
1912
1913 static PyObject *shakeget_buffered(PyObject *me, void *hunoz)
1914 { return (PyInt_FromLong(SHAKE_H(me)->h.n)); }
1915
1916 static PyObject *shakeget_state(PyObject *me, void *hunoz)
1917 {
1918 int st = SHAKE_ST(me);
1919 return (PyString_FromString(st == 0 ? "absorb" :
1920 st == 1 ? "squeeze" : "dead"));
1921 }
1922
1923 static PyGetSetDef shake_pygetset[] = {
1924 #define GETSETNAME(op, name) shake##op##_##name
1925 GET (rate, "S.rate -> rate, in bytes")
1926 GET (buffered, "S.buffered -> amount currently buffered")
1927 GET (state, "S.state -> `absorb', `squeeze', `dead'")
1928 #undef GETSETNAME
1929 { 0 }
1930 };
1931
1932 static PyMethodDef shake_pymethods[] = {
1933 #define METHNAME(func) shakemeth_##func
1934 METH (copy, "S.copy() -> SS")
1935 METH (hash, "S.hash(M)")
1936 #define METHU_(n, W, w) METH(hashu##w, "S.hashu" #w "(WORD)")
1937 DOUINTCONV(METHU_)
1938 #undef METHU_
1939 #define METHBUF_(n, W, w) METH(hashbuf##w, "S.hashbuf" #w "(BYTES)")
1940 DOUINTCONV(METHBUF_)
1941 #undef METHBUF_
1942 METH (hashstrz, "S.hashstrz(STRING)")
1943 METH (xof, "S.xof()")
1944 METH (done, "S.done(LEN) ->H")
1945 METH (get, "S.get(LEN) -> H")
1946 METH (mask, "S.mask(M) -> C")
1947 #undef METHNAME
1948 { 0 }
1949 };
1950
1951 static PyTypeObject shake_pytype_skel = {
1952 PyObject_HEAD_INIT(0) 0, /* Header */
1953 "Shake", /* @tp_name@ */
1954 sizeof(shake_pyobj), /* @tp_basicsize@ */
1955 0, /* @tp_itemsize@ */
1956
1957 0, /* @tp_dealloc@ */
1958 0, /* @tp_print@ */
1959 0, /* @tp_getattr@ */
1960 0, /* @tp_setattr@ */
1961 0, /* @tp_compare@ */
1962 0, /* @tp_repr@ */
1963 0, /* @tp_as_number@ */
1964 0, /* @tp_as_sequence@ */
1965 0, /* @tp_as_mapping@ */
1966 0, /* @tp_hash@ */
1967 0, /* @tp_call@ */
1968 0, /* @tp_str@ */
1969 0, /* @tp_getattro@ */
1970 0, /* @tp_setattro@ */
1971 0, /* @tp_as_buffer@ */
1972 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1973 Py_TPFLAGS_BASETYPE,
1974
1975 /* @tp_doc@ */
1976 "SHAKE/cSHAKE base class.",
1977
1978 0, /* @tp_traverse@ */
1979 0, /* @tp_clear@ */
1980 0, /* @tp_richcompare@ */
1981 0, /* @tp_weaklistoffset@ */
1982 0, /* @tp_iter@ */
1983 0, /* @tp_iternext@ */
1984 shake_pymethods, /* @tp_methods@ */
1985 0, /* @tp_members@ */
1986 shake_pygetset, /* @tp_getset@ */
1987 0, /* @tp_base@ */
1988 0, /* @tp_dict@ */
1989 0, /* @tp_descr_get@ */
1990 0, /* @tp_descr_set@ */
1991 0, /* @tp_dictoffset@ */
1992 0, /* @tp_init@ */
1993 PyType_GenericAlloc, /* @tp_alloc@ */
1994 abstract_pynew, /* @tp_new@ */
1995 0, /* @tp_free@ */
1996 0 /* @tp_is_gc@ */
1997 };
1998
1999 static PyTypeObject shake128_pytype_skel = {
2000 PyObject_HEAD_INIT(0) 0, /* Header */
2001 "Shake128", /* @tp_name@ */
2002 0, /* @tp_basicsize@ */
2003 0, /* @tp_itemsize@ */
2004
2005 0, /* @tp_dealloc@ */
2006 0, /* @tp_print@ */
2007 0, /* @tp_getattr@ */
2008 0, /* @tp_setattr@ */
2009 0, /* @tp_compare@ */
2010 0, /* @tp_repr@ */
2011 0, /* @tp_as_number@ */
2012 0, /* @tp_as_sequence@ */
2013 0, /* @tp_as_mapping@ */
2014 0, /* @tp_hash@ */
2015 0, /* @tp_call@ */
2016 0, /* @tp_str@ */
2017 0, /* @tp_getattro@ */
2018 0, /* @tp_setattro@ */
2019 0, /* @tp_as_buffer@ */
2020 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2021 Py_TPFLAGS_BASETYPE,
2022
2023 /* @tp_doc@ */
2024 "Shake128([perso = STR], [func = STR]): SHAKE128/cSHAKE128 XOF.",
2025
2026 0, /* @tp_traverse@ */
2027 0, /* @tp_clear@ */
2028 0, /* @tp_richcompare@ */
2029 0, /* @tp_weaklistoffset@ */
2030 0, /* @tp_iter@ */
2031 0, /* @tp_iternext@ */
2032 0, /* @tp_methods@ */
2033 0, /* @tp_members@ */
2034 0, /* @tp_getset@ */
2035 0, /* @tp_base@ */
2036 0, /* @tp_dict@ */
2037 0, /* @tp_descr_get@ */
2038 0, /* @tp_descr_set@ */
2039 0, /* @tp_dictoffset@ */
2040 0, /* @tp_init@ */
2041 PyType_GenericAlloc, /* @tp_alloc@ */
2042 shake128_pynew, /* @tp_new@ */
2043 0, /* @tp_free@ */
2044 0 /* @tp_is_gc@ */
2045 };
2046
2047 static PyTypeObject shake256_pytype_skel = {
2048 PyObject_HEAD_INIT(0) 0, /* Header */
2049 "Shake256", /* @tp_name@ */
2050 0, /* @tp_basicsize@ */
2051 0, /* @tp_itemsize@ */
2052
2053 0, /* @tp_dealloc@ */
2054 0, /* @tp_print@ */
2055 0, /* @tp_getattr@ */
2056 0, /* @tp_setattr@ */
2057 0, /* @tp_compare@ */
2058 0, /* @tp_repr@ */
2059 0, /* @tp_as_number@ */
2060 0, /* @tp_as_sequence@ */
2061 0, /* @tp_as_mapping@ */
2062 0, /* @tp_hash@ */
2063 0, /* @tp_call@ */
2064 0, /* @tp_str@ */
2065 0, /* @tp_getattro@ */
2066 0, /* @tp_setattro@ */
2067 0, /* @tp_as_buffer@ */
2068 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2069 Py_TPFLAGS_BASETYPE,
2070
2071 /* @tp_doc@ */
2072 "Shake256([perso = STR], [func = STR]): SHAKE256/cSHAKE256 XOF.",
2073
2074 0, /* @tp_traverse@ */
2075 0, /* @tp_clear@ */
2076 0, /* @tp_richcompare@ */
2077 0, /* @tp_weaklistoffset@ */
2078 0, /* @tp_iter@ */
2079 0, /* @tp_iternext@ */
2080 0, /* @tp_methods@ */
2081 0, /* @tp_members@ */
2082 0, /* @tp_getset@ */
2083 0, /* @tp_base@ */
2084 0, /* @tp_dict@ */
2085 0, /* @tp_descr_get@ */
2086 0, /* @tp_descr_set@ */
2087 0, /* @tp_dictoffset@ */
2088 0, /* @tp_init@ */
2089 PyType_GenericAlloc, /* @tp_alloc@ */
2090 shake256_pynew, /* @tp_new@ */
2091 0, /* @tp_free@ */
2092 0 /* @tp_is_gc@ */
2093 };
2094
2095 /*----- Pseudorandom permutations -----------------------------------------*/
2096
2097 static PyTypeObject *gcprp_pytype, *gprp_pytype;
2098
2099 typedef struct prpinfo {
2100 const char *name;
2101 const octet *keysz;
2102 size_t ctxsz;
2103 size_t blksz;
2104 void (*init)(void *, const void *, size_t);
2105 void (*eblk)(void *, const void *, void *);
2106 void (*dblk)(void *, const void *, void *);
2107 } prpinfo;
2108
2109 #define PRP_DEF(PRE, pre) \
2110 static void pre##_prpinit(void *ctx, const void *k, size_t ksz) \
2111 { pre##_init(ctx, k, ksz); } \
2112 static void pre##_prpeblk(void *ctx, const void *in, void *out) \
2113 { \
2114 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
2115 pre##_eblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
2116 } \
2117 static void pre##_prpdblk(void *ctx, const void *in, void *out) \
2118 { \
2119 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
2120 pre##_dblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
2121 } \
2122 static const prpinfo pre##_prpinfo = { \
2123 #pre, pre##_keysz, sizeof(pre##_ctx), PRE##_BLKSZ, \
2124 pre##_prpinit, pre##_prpeblk, pre##_prpdblk \
2125 };
2126 PRPS(PRP_DEF)
2127
2128 static const struct prpinfo *const gprptab[] = {
2129 #define PRP_ENTRY(PRE, pre) &pre##_prpinfo,
2130 PRPS(PRP_ENTRY)
2131 0
2132 };
2133
2134 typedef struct gcprp_pyobj {
2135 PyHeapTypeObject ty;
2136 const prpinfo *prp;
2137 } gcprp_pyobj;
2138 #define GCPRP_PRP(o) (((gcprp_pyobj *)(o))->prp)
2139
2140 typedef struct gprp_pyobj {
2141 PyObject_HEAD
2142 const prpinfo *prp;
2143 } gprp_pyobj;
2144 #define GPRP_PRP(o) (((gprp_pyobj *)(o))->prp)
2145 #define GPRP_CTX(o) (((gprp_pyobj *)(o)) + 1)
2146
2147 typedef struct prp {
2148 const prpinfo *prp;
2149 void *ctx;
2150 } prp;
2151
2152 static PyObject *gprp_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
2153 {
2154 static const char *const kwlist[] = { "key", 0 };
2155 char *k;
2156 Py_ssize_t sz;
2157 const prpinfo *prp = GCPRP_PRP(ty);
2158 PyObject *me;
2159
2160 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &k, &sz))
2161 goto end;
2162 if (keysz(sz, prp->keysz) != sz) VALERR("bad key length");
2163 me = (PyObject *)ty->tp_alloc(ty, 0);
2164 GPRP_PRP(me) = prp;
2165 prp->init(GPRP_CTX(me), k, sz);
2166 Py_INCREF(me);
2167 return (me);
2168 end:
2169 return (0);
2170 }
2171
2172 static void gprp_pydealloc(PyObject *me)
2173 { Py_DECREF(me->ob_type); FREEOBJ(me); }
2174
2175 static PyObject *gcprp_pywrap(const prpinfo *prp)
2176 {
2177 gcprp_pyobj *g = newtype(gcprp_pytype, 0, prp->name);
2178 g->prp = prp;
2179 g->ty.ht_type.tp_basicsize = sizeof(gprp_pyobj) + prp->ctxsz;
2180 g->ty.ht_type.tp_base = gprp_pytype;
2181 Py_INCREF(gprp_pytype);
2182 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
2183 Py_TPFLAGS_BASETYPE |
2184 Py_TPFLAGS_HEAPTYPE);
2185 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
2186 g->ty.ht_type.tp_free = 0;
2187 g->ty.ht_type.tp_new = gprp_pynew;
2188 typeready(&g->ty.ht_type);
2189 return ((PyObject *)g);
2190 }
2191
2192 static PyObject *gcpget_name(PyObject *me, void *hunoz)
2193 { return (PyString_FromString(GCPRP_PRP(me)->name)); }
2194 static PyObject *gcpget_keysz(PyObject *me, void *hunoz)
2195 { return (keysz_pywrap(GCPRP_PRP(me)->keysz)); }
2196 static PyObject *gcpget_blksz(PyObject *me, void *hunoz)
2197 { return (PyInt_FromLong(GCPRP_PRP(me)->blksz)); }
2198
2199 static PyObject *gpmeth_encrypt(PyObject *me, PyObject *arg)
2200 {
2201 char *p;
2202 Py_ssize_t n;
2203 PyObject *rc = 0;
2204
2205 if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &n)) goto end;
2206 if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length");
2207 rc = bytestring_pywrap(0, n);
2208 GPRP_PRP(me)->eblk(GPRP_CTX(me), p, PyString_AS_STRING(rc));
2209 end:
2210 return (rc);
2211 }
2212
2213 static PyObject *gpmeth_decrypt(PyObject *me, PyObject *arg)
2214 {
2215 char *p;
2216 Py_ssize_t n;
2217 PyObject *rc = 0;
2218
2219 if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &n)) goto end;
2220 if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length");
2221 rc = bytestring_pywrap(0, n);
2222 GPRP_PRP(me)->dblk(GPRP_CTX(me), p, PyString_AS_STRING(rc));
2223 end:
2224 return (rc);
2225 }
2226
2227 static PyGetSetDef gcprp_pygetset[] = {
2228 #define GETSETNAME(op, name) gcp##op##_##name
2229 GET (keysz, "CP.keysz -> acceptable key sizes")
2230 GET (blksz, "CP.blksz -> block size")
2231 GET (name, "CP.name -> name of this kind of PRP")
2232 #undef GETSETNAME
2233 { 0 }
2234 };
2235
2236 static PyMethodDef gprp_pymethods[] = {
2237 #define METHNAME(name) gpmeth_##name
2238 METH (encrypt, "P.encrypt(PT) -> CT")
2239 METH (decrypt, "P.decrypt(CT) -> PT")
2240 #undef METHNAME
2241 { 0 }
2242 };
2243
2244 static PyTypeObject gcprp_pytype_skel = {
2245 PyObject_HEAD_INIT(0) 0, /* Header */
2246 "GCPRP", /* @tp_name@ */
2247 sizeof(gcprp_pyobj), /* @tp_basicsize@ */
2248 0, /* @tp_itemsize@ */
2249
2250 0, /* @tp_dealloc@ */
2251 0, /* @tp_print@ */
2252 0, /* @tp_getattr@ */
2253 0, /* @tp_setattr@ */
2254 0, /* @tp_compare@ */
2255 0, /* @tp_repr@ */
2256 0, /* @tp_as_number@ */
2257 0, /* @tp_as_sequence@ */
2258 0, /* @tp_as_mapping@ */
2259 0, /* @tp_hash@ */
2260 0, /* @tp_call@ */
2261 0, /* @tp_str@ */
2262 0, /* @tp_getattro@ */
2263 0, /* @tp_setattro@ */
2264 0, /* @tp_as_buffer@ */
2265 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2266 Py_TPFLAGS_BASETYPE,
2267
2268 /* @tp_doc@ */
2269 "Pseudorandom permutation metaclass.",
2270
2271 0, /* @tp_traverse@ */
2272 0, /* @tp_clear@ */
2273 0, /* @tp_richcompare@ */
2274 0, /* @tp_weaklistoffset@ */
2275 0, /* @tp_iter@ */
2276 0, /* @tp_iternext@ */
2277 0, /* @tp_methods@ */
2278 0, /* @tp_members@ */
2279 gcprp_pygetset, /* @tp_getset@ */
2280 0, /* @tp_base@ */
2281 0, /* @tp_dict@ */
2282 0, /* @tp_descr_get@ */
2283 0, /* @tp_descr_set@ */
2284 0, /* @tp_dictoffset@ */
2285 0, /* @tp_init@ */
2286 PyType_GenericAlloc, /* @tp_alloc@ */
2287 abstract_pynew, /* @tp_new@ */
2288 0, /* @tp_free@ */
2289 0 /* @tp_is_gc@ */
2290 };
2291
2292 static PyTypeObject gprp_pytype_skel = {
2293 PyObject_HEAD_INIT(0) 0, /* Header */
2294 "GPRP", /* @tp_name@ */
2295 sizeof(gprp_pyobj), /* @tp_basicsize@ */
2296 0, /* @tp_itemsize@ */
2297
2298 gprp_pydealloc, /* @tp_dealloc@ */
2299 0, /* @tp_print@ */
2300 0, /* @tp_getattr@ */
2301 0, /* @tp_setattr@ */
2302 0, /* @tp_compare@ */
2303 0, /* @tp_repr@ */
2304 0, /* @tp_as_number@ */
2305 0, /* @tp_as_sequence@ */
2306 0, /* @tp_as_mapping@ */
2307 0, /* @tp_hash@ */
2308 0, /* @tp_call@ */
2309 0, /* @tp_str@ */
2310 0, /* @tp_getattro@ */
2311 0, /* @tp_setattro@ */
2312 0, /* @tp_as_buffer@ */
2313 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2314 Py_TPFLAGS_BASETYPE,
2315
2316 /* @tp_doc@ */
2317 "Pseudorandom permutation, abstract base class.",
2318
2319 0, /* @tp_traverse@ */
2320 0, /* @tp_clear@ */
2321 0, /* @tp_richcompare@ */
2322 0, /* @tp_weaklistoffset@ */
2323 0, /* @tp_iter@ */
2324 0, /* @tp_iternext@ */
2325 gprp_pymethods, /* @tp_methods@ */
2326 0, /* @tp_members@ */
2327 0, /* @tp_getset@ */
2328 0, /* @tp_base@ */
2329 0, /* @tp_dict@ */
2330 0, /* @tp_descr_get@ */
2331 0, /* @tp_descr_set@ */
2332 0, /* @tp_dictoffset@ */
2333 0, /* @tp_init@ */
2334 PyType_GenericAlloc, /* @tp_alloc@ */
2335 abstract_pynew, /* @tp_new@ */
2336 0, /* @tp_free@ */
2337 0 /* @tp_is_gc@ */
2338 };
2339
2340 /*----- Main code ---------------------------------------------------------*/
2341
2342 static PyMethodDef methods[] = {
2343 #define METHNAME(func) meth_##func
2344 METH (_KeySZ_fromdl, "\
2345 fromdl(N) -> M: convert integer discrete log field size to work factor")
2346 METH (_KeySZ_fromschnorr, "\
2347 fromschnorr(N) -> M: convert Schnorr group order to work factor")
2348 METH (_KeySZ_fromif, "\
2349 fromif(N) -> M: convert integer factorization problem size to work factor")
2350 METH (_KeySZ_fromec, "\
2351 fromec(N) -> M: convert elliptic curve group order to work factor")
2352 METH (_KeySZ_todl, "\
2353 todl(N) -> M: convert work factor to integer discrete log field size")
2354 METH (_KeySZ_toschnorr, "\
2355 toschnorr(N) -> M: convert work factor to Schnorr group order")
2356 METH (_KeySZ_toif, "\
2357 toif(N) -> M: convert work factor to integer factorization problem size")
2358 METH (_KeySZ_toec, "\
2359 toec(N) -> M: convert work factor to elliptic curve group order")
2360 METH (_KeySZ_toec, "\
2361 toec(N) -> M: convert work factor to elliptic curve group order")
2362 #define METH_HDANCE(hdance, HDance) METH(hdance##_prf, "\
2363 " #hdance "_prf(K, N) -> H: calculate " HDance " hash of N with K")
2364 METH_HDANCE(hsalsa20, "HSalsa20")
2365 METH_HDANCE(hsalsa2012, "HSalsa20/12")
2366 METH_HDANCE(hsalsa208, "HSalsa20/8")
2367 METH_HDANCE(hchacha20, "HChaCha20")
2368 METH_HDANCE(hchacha12, "HChaCha12")
2369 METH_HDANCE(hchacha8, "HChaCha8")
2370 #undef METH_DANCE
2371 #undef METHNAME
2372 { 0 }
2373 };
2374
2375 void algorithms_pyinit(void)
2376 {
2377 INITTYPE(keysz, root);
2378 INITTYPE(keyszany, keysz);
2379 INITTYPE(keyszrange, keysz);
2380 INITTYPE(keyszset, keysz);
2381 INITTYPE(gccipher, type);
2382 INITTYPE(gcipher, root);
2383 INITTYPE(gchash, type);
2384 INITTYPE(ghash, root);
2385 INITTYPE(gcmac, type);
2386 INITTYPE(gmac, type);
2387 INITTYPE(gmhash, ghash);
2388 INITTYPE(poly1305cls, type);
2389 INITTYPE_META(poly1305key, type, poly1305cls);
2390 INITTYPE(poly1305hash, root);
2391 INITTYPE(kxvik, root);
2392 INITTYPE(shake, root);
2393 INITTYPE(shake128, shake);
2394 INITTYPE(shake256, shake);
2395 INITTYPE(gcprp, type);
2396 INITTYPE(gprp, root);
2397 addmethods(methods);
2398 }
2399
2400 GEN(gcciphers, cipher)
2401 GEN(gchashes, hash)
2402 GEN(gcmacs, mac)
2403 #define gcprp prpinfo
2404 GEN(gcprps, prp)
2405
2406 void algorithms_pyinsert(PyObject *mod)
2407 {
2408 PyObject *d;
2409 INSERT("KeySZ", keysz_pytype);
2410 INSERT("KeySZAny", keyszany_pytype);
2411 INSERT("KeySZRange", keyszrange_pytype);
2412 INSERT("KeySZSet", keyszset_pytype);
2413 INSERT("GCCipher", gccipher_pytype);
2414 INSERT("GCipher", gcipher_pytype);
2415 INSERT("gcciphers", gcciphers());
2416 INSERT("GCHash", gchash_pytype);
2417 INSERT("GHash", ghash_pytype);
2418 INSERT("gchashes", d = gchashes());
2419 sha_pyobj = PyDict_GetItemString(d, "sha"); Py_INCREF(sha_pyobj);
2420 has160_pyobj = PyDict_GetItemString(d, "has160"); Py_INCREF(has160_pyobj);
2421 INSERT("GCMAC", gcmac_pytype);
2422 INSERT("GMAC", gmac_pytype);
2423 INSERT("GMACHash", gmhash_pytype);
2424 INSERT("gcmacs", gcmacs());
2425 INSERT("Poly1305Class", poly1305cls_pytype);
2426 INSERT("poly1305", poly1305key_pytype);
2427 INSERT("Poly1305Hash", poly1305hash_pytype);
2428 INSERT("Keccak1600", kxvik_pytype);
2429 INSERT("Shake", shake_pytype);
2430 INSERT("Shake128", shake128_pytype);
2431 INSERT("Shake256", shake256_pytype);
2432 INSERT("GCPRP", gcprp_pytype);
2433 INSERT("GPRP", gprp_pytype);
2434 INSERT("gcprps", gcprps());
2435 }
2436
2437 /*----- That's all, folks -------------------------------------------------*/