t/t-algorithms.py: Add a simple test for `Keccak1600.copy'.
[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); if (n < 0) goto end;
139 l = PyList_New(0); if (!l) goto end;
140 if (dfl < 0) VALERR("key size cannot be negative");
141 x = PyInt_FromLong(dfl);
142 PyList_Append(l, x);
143 Py_DECREF(x);
144 x = 0;
145 for (i = 0; i < n; i++) {
146 if ((x = PySequence_GetItem(set, i)) == 0) goto end;
147 xx = PyInt_AsLong(x);
148 if (PyErr_Occurred()) goto end;
149 if (xx == dfl) continue;
150 if (xx < 0) VALERR("key size cannot be negative");
151 PyList_Append(l, x);
152 Py_DECREF(x);
153 x = 0;
154 }
155 Py_DECREF(set);
156 if ((set = PySequence_Tuple(l)) == 0) goto end;
157 o = (keyszset_pyobj *)ty->tp_alloc(ty, 0);
158 o->dfl = dfl;
159 o->set = set;
160 Py_INCREF(set);
161 end:
162 Py_XDECREF(set);
163 Py_XDECREF(l);
164 Py_XDECREF(x);
165 return ((PyObject *)o);
166 }
167
168 static PyObject *kaget_min(PyObject *me, void *hunoz)
169 { return (PyInt_FromLong(0)); }
170 #define kaget_max kaget_min
171
172 static PyObject *ksget_min(PyObject *me, void *hunoz)
173 {
174 PyObject *set = ((keyszset_pyobj *)me)->set;
175 int i, n, y, x = -1;
176 n = PyTuple_Size(set);
177 for (i = 0; i < n; i++) {
178 y = PyInt_AsLong(PyTuple_GetItem(set, i));
179 if (x == -1 || y < x) x = y;
180 }
181 return (PyInt_FromLong(x));
182 }
183
184 static PyObject *ksget_max(PyObject *me, void *hunoz)
185 {
186 PyObject *set = ((keyszset_pyobj *)me)->set;
187 int i, n, y, x = -1;
188 n = PyTuple_Size(set);
189 for (i = 0; i < n; i++) {
190 y = PyInt_AsLong(PyTuple_GetItem(set, i));
191 if (y > x) x = y;
192 }
193 return (PyInt_FromLong(x));
194 }
195
196 static PyMemberDef keysz_pymembers[] = {
197 #define MEMBERSTRUCT keysz_pyobj
198 #define default dfl /* ugh! */
199 MEMBER(default, T_INT, READONLY, "KSZ.default -> default key size")
200 #undef default
201 #undef MEMBERSTRUCT
202 { 0 }
203 };
204
205 static PyGetSetDef keyszany_pygetset[] = {
206 #define GETSETNAME(op, name) ka##op##_##name
207 GET (min, "KSZ.min -> smallest allowed key size")
208 GET (max, "KSZ.max -> largest allowed key size")
209 #undef GETSETNAME
210 { 0 }
211 };
212
213 static PyMemberDef keyszrange_pymembers[] = {
214 #define MEMBERSTRUCT keyszrange_pyobj
215 MEMBER(min, T_INT, READONLY, "KSZ.min -> smallest allowed key size")
216 MEMBER(max, T_INT, READONLY, "KSZ.max -> largest allowed key size")
217 MEMBER(mod, T_INT, READONLY,
218 "KSZ.mod -> key size must be a multiple of this")
219 #undef MEMBERSTRUCT
220 { 0 }
221 };
222
223 static PyGetSetDef keyszset_pygetset[] = {
224 #define GETSETNAME(op, name) ks##op##_##name
225 GET (min, "KSZ.min -> smallest allowed key size")
226 GET (max, "KSZ.max -> largest allowed key size")
227 #undef GETSETNAME
228 { 0 }
229 };
230
231 static PyMemberDef keyszset_pymembers[] = {
232 #define MEMBERSTRUCT keyszset_pyobj
233 MEMBER(set, T_OBJECT, READONLY, "KSZ.set -> allowed key sizes")
234 #undef MEMBERSTRUCT
235 { 0 }
236 };
237
238 static PyTypeObject keysz_pytype_skel = {
239 PyObject_HEAD_INIT(0) 0, /* Header */
240 "KeySZ", /* @tp_name@ */
241 sizeof(keysz_pyobj), /* @tp_basicsize@ */
242 0, /* @tp_itemsize@ */
243
244 0, /* @tp_dealloc@ */
245 0, /* @tp_print@ */
246 0, /* @tp_getattr@ */
247 0, /* @tp_setattr@ */
248 0, /* @tp_compare@ */
249 0, /* @tp_repr@ */
250 0, /* @tp_as_number@ */
251 0, /* @tp_as_sequence@ */
252 0, /* @tp_as_mapping@ */
253 0, /* @tp_hash@ */
254 0, /* @tp_call@ */
255 0, /* @tp_str@ */
256 0, /* @tp_getattro@ */
257 0, /* @tp_setattro@ */
258 0, /* @tp_as_buffer@ */
259 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
260 Py_TPFLAGS_BASETYPE,
261
262 /* @tp_doc@ */
263 "Key size constraints. Abstract.",
264
265 0, /* @tp_traverse@ */
266 0, /* @tp_clear@ */
267 0, /* @tp_richcompare@ */
268 0, /* @tp_weaklistoffset@ */
269 0, /* @tp_iter@ */
270 0, /* @tp_iternext@ */
271 0, /* @tp_methods@ */
272 keysz_pymembers, /* @tp_members@ */
273 0, /* @tp_getset@ */
274 0, /* @tp_base@ */
275 0, /* @tp_dict@ */
276 0, /* @tp_descr_get@ */
277 0, /* @tp_descr_set@ */
278 0, /* @tp_dictoffset@ */
279 0, /* @tp_init@ */
280 PyType_GenericAlloc, /* @tp_alloc@ */
281 abstract_pynew, /* @tp_new@ */
282 0, /* @tp_free@ */
283 0 /* @tp_is_gc@ */
284 };
285
286 static PyTypeObject keyszany_pytype_skel = {
287 PyObject_HEAD_INIT(0) 0, /* Header */
288 "KeySZAny", /* @tp_name@ */
289 sizeof(keysz_pyobj), /* @tp_basicsize@ */
290 0, /* @tp_itemsize@ */
291
292 0, /* @tp_dealloc@ */
293 0, /* @tp_print@ */
294 0, /* @tp_getattr@ */
295 0, /* @tp_setattr@ */
296 0, /* @tp_compare@ */
297 0, /* @tp_repr@ */
298 0, /* @tp_as_number@ */
299 0, /* @tp_as_sequence@ */
300 0, /* @tp_as_mapping@ */
301 0, /* @tp_hash@ */
302 0, /* @tp_call@ */
303 0, /* @tp_str@ */
304 0, /* @tp_getattro@ */
305 0, /* @tp_setattro@ */
306 0, /* @tp_as_buffer@ */
307 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
308 Py_TPFLAGS_BASETYPE,
309
310 /* @tp_doc@ */
311 "KeySZAny(DEFAULT)\n\
312 Key size constraints. This object imposes no constraints on size.",
313
314 0, /* @tp_traverse@ */
315 0, /* @tp_clear@ */
316 0, /* @tp_richcompare@ */
317 0, /* @tp_weaklistoffset@ */
318 0, /* @tp_iter@ */
319 0, /* @tp_iternext@ */
320 0, /* @tp_methods@ */
321 0, /* @tp_members@ */
322 keyszany_pygetset, /* @tp_getset@ */
323 0, /* @tp_base@ */
324 0, /* @tp_dict@ */
325 0, /* @tp_descr_get@ */
326 0, /* @tp_descr_set@ */
327 0, /* @tp_dictoffset@ */
328 0, /* @tp_init@ */
329 PyType_GenericAlloc, /* @tp_alloc@ */
330 keyszany_pynew, /* @tp_new@ */
331 0, /* @tp_free@ */
332 0 /* @tp_is_gc@ */
333 };
334
335 static PyTypeObject keyszrange_pytype_skel = {
336 PyObject_HEAD_INIT(0) 0, /* Header */
337 "KeySZRange", /* @tp_name@ */
338 sizeof(keyszrange_pyobj), /* @tp_basicsize@ */
339 0, /* @tp_itemsize@ */
340
341 0, /* @tp_dealloc@ */
342 0, /* @tp_print@ */
343 0, /* @tp_getattr@ */
344 0, /* @tp_setattr@ */
345 0, /* @tp_compare@ */
346 0, /* @tp_repr@ */
347 0, /* @tp_as_number@ */
348 0, /* @tp_as_sequence@ */
349 0, /* @tp_as_mapping@ */
350 0, /* @tp_hash@ */
351 0, /* @tp_call@ */
352 0, /* @tp_str@ */
353 0, /* @tp_getattro@ */
354 0, /* @tp_setattro@ */
355 0, /* @tp_as_buffer@ */
356 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
357 Py_TPFLAGS_BASETYPE,
358
359 /* @tp_doc@ */
360 "KeySZRange(DEFAULT, [min = 0], [max = 0], [mod = 1])\n\
361 Key size constraints. Key size must be between MIN and MAX inclusive,\n\
362 and be a multiple of MOD.",
363
364 0, /* @tp_traverse@ */
365 0, /* @tp_clear@ */
366 0, /* @tp_richcompare@ */
367 0, /* @tp_weaklistoffset@ */
368 0, /* @tp_iter@ */
369 0, /* @tp_iternext@ */
370 0, /* @tp_methods@ */
371 keyszrange_pymembers, /* @tp_members@ */
372 0, /* @tp_getset@ */
373 0, /* @tp_base@ */
374 0, /* @tp_dict@ */
375 0, /* @tp_descr_get@ */
376 0, /* @tp_descr_set@ */
377 0, /* @tp_dictoffset@ */
378 0, /* @tp_init@ */
379 PyType_GenericAlloc, /* @tp_alloc@ */
380 keyszrange_pynew, /* @tp_new@ */
381 0, /* @tp_free@ */
382 0 /* @tp_is_gc@ */
383 };
384
385 static PyTypeObject keyszset_pytype_skel = {
386 PyObject_HEAD_INIT(0) 0, /* Header */
387 "KeySZSet", /* @tp_name@ */
388 sizeof(keyszset_pyobj), /* @tp_basicsize@ */
389 0, /* @tp_itemsize@ */
390
391 0, /* @tp_dealloc@ */
392 0, /* @tp_print@ */
393 0, /* @tp_getattr@ */
394 0, /* @tp_setattr@ */
395 0, /* @tp_compare@ */
396 0, /* @tp_repr@ */
397 0, /* @tp_as_number@ */
398 0, /* @tp_as_sequence@ */
399 0, /* @tp_as_mapping@ */
400 0, /* @tp_hash@ */
401 0, /* @tp_call@ */
402 0, /* @tp_str@ */
403 0, /* @tp_getattro@ */
404 0, /* @tp_setattro@ */
405 0, /* @tp_as_buffer@ */
406 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
407 Py_TPFLAGS_BASETYPE,
408
409 /* @tp_doc@ */
410 "KeySZSet(DEFAULT, SEQ)\n\
411 Key size constraints. Key size must be DEFAULT or one in SEQ.",
412
413 0, /* @tp_traverse@ */
414 0, /* @tp_clear@ */
415 0, /* @tp_richcompare@ */
416 0, /* @tp_weaklistoffset@ */
417 0, /* @tp_iter@ */
418 0, /* @tp_iternext@ */
419 0, /* @tp_methods@ */
420 keyszset_pymembers, /* @tp_members@ */
421 keyszset_pygetset, /* @tp_getset@ */
422 0, /* @tp_base@ */
423 0, /* @tp_dict@ */
424 0, /* @tp_descr_get@ */
425 0, /* @tp_descr_set@ */
426 0, /* @tp_dictoffset@ */
427 0, /* @tp_init@ */
428 PyType_GenericAlloc, /* @tp_alloc@ */
429 keyszset_pynew, /* @tp_new@ */
430 0, /* @tp_free@ */
431 0 /* @tp_is_gc@ */
432 };
433
434 #define KSZCONVOP(op) \
435 static PyObject *meth__KeySZ_##op(PyObject *me, PyObject *arg) \
436 { \
437 double x, y; \
438 if (!PyArg_ParseTuple(arg, "Od:" #op, &me, &x)) return (0); \
439 y = keysz_##op(x); \
440 return (PyFloat_FromDouble(y)); \
441 }
442 KSZCONVOP(fromdl)
443 KSZCONVOP(fromschnorr)
444 KSZCONVOP(fromif)
445 KSZCONVOP(fromec)
446 KSZCONVOP(todl)
447 KSZCONVOP(toschnorr)
448 KSZCONVOP(toif)
449 KSZCONVOP(toec)
450 #undef KSZCONVOP
451
452 /*----- Symmetric encryption ----------------------------------------------*/
453
454 PyTypeObject *gccipher_pytype, *gcipher_pytype;
455
456 CONVFUNC(gccipher, gccipher *, GCCIPHER_CC)
457 CONVFUNC(gcipher, gcipher *, GCIPHER_C)
458
459 PyObject *gcipher_pywrap(PyObject *cobj, gcipher *c)
460 {
461 gcipher_pyobj *g;
462 if (!cobj) cobj = gccipher_pywrap((/*unconst*/ gccipher *)GC_CLASS(c));
463 else Py_INCREF(cobj);
464 g = PyObject_NEW(gcipher_pyobj, (PyTypeObject *)cobj);
465 g->c = c;
466 return ((PyObject *)g);
467 }
468
469 static PyObject *gcipher_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
470 {
471 static const char *const kwlist[] = { "k", 0 };
472 char *k;
473 Py_ssize_t sz;
474
475 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &k, &sz))
476 goto end;
477 if (keysz(sz, GCCIPHER_CC(ty)->keysz) != sz) VALERR("bad key length");
478 return (gcipher_pywrap((PyObject *)ty,
479 GC_INIT(GCCIPHER_CC(ty), k, sz)));
480 end:
481 return (0);
482 }
483
484 PyObject *gccipher_pywrap(gccipher *cc)
485 {
486 gccipher_pyobj *g = newtype(gccipher_pytype, 0, cc->name);
487 g->cc = cc;
488 g->ty.ht_type.tp_basicsize = sizeof(gcipher_pyobj);
489 g->ty.ht_type.tp_base = gcipher_pytype;
490 Py_INCREF(gcipher_pytype);
491 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
492 Py_TPFLAGS_BASETYPE |
493 Py_TPFLAGS_HEAPTYPE);
494 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
495 g->ty.ht_type.tp_free = 0;
496 g->ty.ht_type.tp_new = gcipher_pynew;
497 typeready(&g->ty.ht_type);
498 return ((PyObject *)g);
499 }
500
501 static void gcipher_pydealloc(PyObject *me)
502 {
503 GC_DESTROY(GCIPHER_C(me));
504 Py_DECREF(me->ob_type);
505 FREEOBJ(me);
506 }
507
508 static PyObject *gccget_name(PyObject *me, void *hunoz)
509 { return (PyString_FromString(GCCIPHER_CC(me)->name)); }
510
511 static PyObject *gccget_keysz(PyObject *me, void *hunoz)
512 { return (keysz_pywrap(GCCIPHER_CC(me)->keysz)); }
513
514 static PyObject *gccget_blksz(PyObject *me, void *hunoz)
515 { return (PyInt_FromLong(GCCIPHER_CC(me)->blksz)); }
516
517 static PyObject *gcmeth_encrypt(PyObject *me, PyObject *arg)
518 {
519 char *p;
520 Py_ssize_t sz;
521 PyObject *rc = 0;
522
523 if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &sz)) return (0);
524 rc = bytestring_pywrap(0, sz);
525 GC_ENCRYPT(GCIPHER_C(me), p, PyString_AS_STRING(rc), sz);
526 return (rc);
527 }
528
529 static PyObject *gcmeth_enczero(PyObject *me, PyObject *arg)
530 {
531 char *p;
532 int sz;
533 PyObject *rc = 0;
534
535 if (!PyArg_ParseTuple(arg, "i:enczero", &sz)) return (0);
536 rc = bytestring_pywrap(0, sz);
537 p = PyString_AS_STRING(rc);
538 memset(p, 0, sz);
539 GC_ENCRYPT(GCIPHER_C(me), p, p, sz);
540 return (rc);
541 }
542
543 static PyObject *gcmeth_decrypt(PyObject *me, PyObject *arg)
544 {
545 char *p;
546 Py_ssize_t sz;
547 PyObject *rc = 0;
548
549 if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &sz)) return (0);
550 rc = bytestring_pywrap(0, sz);
551 GC_DECRYPT(GCIPHER_C(me), p, PyString_AS_STRING(rc), sz);
552 return (rc);
553 }
554
555 static PyObject *gcmeth_deczero(PyObject *me, PyObject *arg)
556 {
557 char *p;
558 int sz;
559 PyObject *rc = 0;
560
561 if (!PyArg_ParseTuple(arg, "i:deczero", &sz)) return (0);
562 rc = bytestring_pywrap(0, sz);
563 p = PyString_AS_STRING(rc);
564 memset(p, 0, sz);
565 GC_DECRYPT(GCIPHER_C(me), p, p, sz);
566 return (rc);
567 }
568
569 static PyObject *gcmeth_setiv(PyObject *me, PyObject *arg)
570 {
571 char *p;
572 Py_ssize_t sz;
573
574 if (!PyArg_ParseTuple(arg, "s#:setiv", &p, &sz)) goto end;
575 if (!GCIPHER_C(me)->ops->setiv) VALERR("`setiv' not supported");
576 if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode");
577 if (sz != GC_CLASS(GCIPHER_C(me))->blksz) VALERR("bad IV length");
578 GC_SETIV(GCIPHER_C(me), p);
579 RETURN_ME;
580 end:
581 return (0);
582 }
583
584 static PyObject *gcmeth_bdry(PyObject *me, PyObject *arg)
585 {
586 if (!PyArg_ParseTuple(arg, ":bdry")) goto end;
587 if (!GCIPHER_C(me)->ops->bdry) VALERR("`bdry' not supported");
588 if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode");
589 GC_BDRY(GCIPHER_C(me));
590 RETURN_ME;
591 end:
592 return (0);
593 }
594
595 static PyGetSetDef gccipher_pygetset[] = {
596 #define GETSETNAME(op, name) gcc##op##_##name
597 GET (keysz, "CC.keysz -> acceptable key sizes")
598 GET (blksz, "CC.blksz -> block size, or zero")
599 GET (name, "CC.name -> name of this kind of cipher")
600 #undef GETSETNAME
601 { 0 }
602 };
603
604 static PyMethodDef gcipher_pymethods[] = {
605 #define METHNAME(name) gcmeth_##name
606 METH (encrypt, "C.encrypt(PT) -> CT")
607 METH (enczero, "C.enczero(N) -> CT")
608 METH (decrypt, "C.decrypt(CT) -> PT")
609 METH (deczero, "C.deczero(N) -> PT")
610 METH (setiv, "C.setiv(IV)")
611 METH (bdry, "C.bdry()")
612 #undef METHNAME
613 { 0 }
614 };
615
616 static PyTypeObject gccipher_pytype_skel = {
617 PyObject_HEAD_INIT(0) 0, /* Header */
618 "GCCipher", /* @tp_name@ */
619 sizeof(gccipher_pyobj), /* @tp_basicsize@ */
620 0, /* @tp_itemsize@ */
621
622 0, /* @tp_dealloc@ */
623 0, /* @tp_print@ */
624 0, /* @tp_getattr@ */
625 0, /* @tp_setattr@ */
626 0, /* @tp_compare@ */
627 0, /* @tp_repr@ */
628 0, /* @tp_as_number@ */
629 0, /* @tp_as_sequence@ */
630 0, /* @tp_as_mapping@ */
631 0, /* @tp_hash@ */
632 0, /* @tp_call@ */
633 0, /* @tp_str@ */
634 0, /* @tp_getattro@ */
635 0, /* @tp_setattro@ */
636 0, /* @tp_as_buffer@ */
637 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
638 Py_TPFLAGS_BASETYPE,
639
640 /* @tp_doc@ */
641 "Symmetric cipher metaclass.",
642
643 0, /* @tp_traverse@ */
644 0, /* @tp_clear@ */
645 0, /* @tp_richcompare@ */
646 0, /* @tp_weaklistoffset@ */
647 0, /* @tp_iter@ */
648 0, /* @tp_iternext@ */
649 0, /* @tp_methods@ */
650 0, /* @tp_members@ */
651 gccipher_pygetset, /* @tp_getset@ */
652 0, /* @tp_base@ */
653 0, /* @tp_dict@ */
654 0, /* @tp_descr_get@ */
655 0, /* @tp_descr_set@ */
656 0, /* @tp_dictoffset@ */
657 0, /* @tp_init@ */
658 PyType_GenericAlloc, /* @tp_alloc@ */
659 abstract_pynew, /* @tp_new@ */
660 0, /* @tp_free@ */
661 0 /* @tp_is_gc@ */
662 };
663
664 static PyTypeObject gcipher_pytype_skel = {
665 PyObject_HEAD_INIT(0) 0, /* Header */
666 "GCipher", /* @tp_name@ */
667 sizeof(gcipher_pyobj), /* @tp_basicsize@ */
668 0, /* @tp_itemsize@ */
669
670 gcipher_pydealloc, /* @tp_dealloc@ */
671 0, /* @tp_print@ */
672 0, /* @tp_getattr@ */
673 0, /* @tp_setattr@ */
674 0, /* @tp_compare@ */
675 0, /* @tp_repr@ */
676 0, /* @tp_as_number@ */
677 0, /* @tp_as_sequence@ */
678 0, /* @tp_as_mapping@ */
679 0, /* @tp_hash@ */
680 0, /* @tp_call@ */
681 0, /* @tp_str@ */
682 0, /* @tp_getattro@ */
683 0, /* @tp_setattro@ */
684 0, /* @tp_as_buffer@ */
685 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
686 Py_TPFLAGS_BASETYPE,
687
688 /* @tp_doc@ */
689 "Symmetric cipher, abstract base class.",
690
691 0, /* @tp_traverse@ */
692 0, /* @tp_clear@ */
693 0, /* @tp_richcompare@ */
694 0, /* @tp_weaklistoffset@ */
695 0, /* @tp_iter@ */
696 0, /* @tp_iternext@ */
697 gcipher_pymethods, /* @tp_methods@ */
698 0, /* @tp_members@ */
699 0, /* @tp_getset@ */
700 0, /* @tp_base@ */
701 0, /* @tp_dict@ */
702 0, /* @tp_descr_get@ */
703 0, /* @tp_descr_set@ */
704 0, /* @tp_dictoffset@ */
705 0, /* @tp_init@ */
706 PyType_GenericAlloc, /* @tp_alloc@ */
707 abstract_pynew, /* @tp_new@ */
708 0, /* @tp_free@ */
709 0 /* @tp_is_gc@ */
710 };
711
712 /*----- Authenticated encryption ------------------------------------------*/
713
714 PyTypeObject *gcaead_pytype, *gaeadkey_pytype;
715 PyTypeObject *gcaeadaad_pytype, *gaeadaad_pytype;
716 PyTypeObject *gcaeadenc_pytype, *gaeadenc_pytype;
717 PyTypeObject *gcaeaddec_pytype, *gaeaddec_pytype;
718
719 CONVFUNC(gcaead, gcaead *, GCAEAD_AEC)
720 CONVFUNC(gaeadkey, gaead_key *, GAEADKEY_K)
721
722 PyObject *gaeadkey_pywrap(PyObject *cobj, gaead_key *k)
723 {
724 gaeadkey_pyobj *gk;
725
726 if (!cobj) cobj = gcaead_pywrap((/*unconst*/ gcaead *)GAEAD_CLASS(k));
727 else Py_INCREF(cobj);
728 gk = PyObject_NEW(gaeadkey_pyobj, (PyTypeObject *)cobj);
729 gk->k = k;
730 return ((PyObject *)gk);
731 }
732
733 static PyObject *gaeadkey_pynew(PyTypeObject *ty,
734 PyObject *arg, PyObject *kw)
735 {
736 static const char *const kwlist[] = { "k", 0 };
737 char *k;
738 Py_ssize_t sz;
739
740 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &k, &sz))
741 goto end;
742 if (keysz(sz, GCAEAD_AEC(ty)->keysz) != sz) VALERR("bad key length");
743 return (gaeadkey_pywrap((PyObject *)ty,
744 GAEAD_KEY(GCAEAD_AEC(ty), k, sz)));
745 end:
746 return (0);
747 }
748
749 PyObject *gcaead_pywrap(gcaead *aec)
750 {
751 gcaead_pyobj *gck;
752 gcaeadaad_pyobj *gca;
753 gcaeadenc_pyobj *gce;
754 gcaeaddec_pyobj *gcd;
755
756 #define MKTYPE(obj, thing, newfn, namefmt) do { \
757 (obj) = newtype(gcaead_pytype, 0, 0); \
758 (obj)->ty.ht_name = PyString_FromFormat(namefmt, aec->name); \
759 (obj)->ty.ht_type.tp_name = PyString_AS_STRING((obj)->ty.ht_name); \
760 (obj)->ty.ht_type.tp_basicsize = sizeof(gaead##thing##_pyobj); \
761 (obj)->ty.ht_type.tp_base = gaead##thing##_pytype; \
762 Py_INCREF(gaead##thing##_pytype); \
763 (obj)->ty.ht_type.tp_flags = \
764 (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE); \
765 (obj)->ty.ht_type.tp_alloc = PyType_GenericAlloc; \
766 (obj)->ty.ht_type.tp_free = 0; \
767 (obj)->ty.ht_type.tp_new = newfn; \
768 typeready(&(obj)->ty.ht_type); \
769 } while (0)
770
771 MKTYPE(gck, key, gaeadkey_pynew, "%s(key)");
772 MKTYPE(gca, aad, abstract_pynew, "%s(aad-hash)");
773 MKTYPE(gce, enc, abstract_pynew, "%s(encrypt)");
774 MKTYPE(gcd, dec, abstract_pynew, "%s(decrypt)");
775
776 #undef MKTYPE
777
778 gck->aec = aec; gck->aad = gca; gck->enc = gce; gck->dec = gcd;
779 gca->key = gce->key = gcd->key = gck;
780 return ((PyObject *)gck);
781 }
782
783 static void gaeadkey_pydealloc(PyObject *me)
784 { GAEAD_DESTROY(GAEADKEY_K(me)); Py_DECREF(me->ob_type); FREEOBJ(me); }
785
786 static PyObject *gcaeget_name(PyObject *me, void *hunoz)
787 { return (PyString_FromString(GCAEAD_AEC(me)->name)); }
788
789 static PyObject *gcaeget_keysz(PyObject *me, void *hunoz)
790 { return (keysz_pywrap(GCAEAD_AEC(me)->keysz)); }
791
792 static PyObject *gcaeget_noncesz(PyObject *me, void *hunoz)
793 { return (keysz_pywrap(GCAEAD_AEC(me)->noncesz)); }
794
795 static PyObject *gcaeget_tagsz(PyObject *me, void *hunoz)
796 { return (keysz_pywrap(GCAEAD_AEC(me)->tagsz)); }
797
798 static PyObject *gcaeget_blksz(PyObject *me, void *hunoz)
799 { return (PyInt_FromLong(GCAEAD_AEC(me)->blksz)); }
800
801 static PyObject *gcaeget_bufsz(PyObject *me, void *hunoz)
802 { return (PyInt_FromLong(GCAEAD_AEC(me)->bufsz)); }
803
804 static PyObject *gcaeget_ohd(PyObject *me, void *hunoz)
805 { return (PyInt_FromLong(GCAEAD_AEC(me)->ohd)); }
806
807 static PyObject *gcaeget_flags(PyObject *me, void *hunoz)
808 { return (PyInt_FromLong(GCAEAD_AEC(me)->f)); }
809
810 static PyGetSetDef gcaead_pygetset[] = {
811 #define GETSETNAME(op, name) gcae##op##_##name
812 GET (keysz, "AEC.keysz -> acceptable key sizes")
813 GET (noncesz, "AEC.noncesz -> acceptable nonce sizes")
814 GET (tagsz, "AEC.tagsz -> acceptable tag sizes")
815 GET (blksz, "AEC.blksz -> block size, or zero")
816 GET (bufsz, "AEC.bufsz -> amount of data buffered internally")
817 GET (ohd, "AEC.ohd -> maximum encryption overhead")
818 GET (name, "AEC.name -> name of this kind of AEAD scheme")
819 GET (flags, "AEC.flags -> mask of `AEADF_...' flags")
820 #undef GETSETNAME
821 { 0 }
822 };
823
824 static PyObject *gaekmeth_aad(PyObject *me, PyObject *arg)
825 {
826 const gaead_key *k = GAEADKEY_K(me);
827 PyObject *rc = 0;
828
829 if (!PyArg_ParseTuple(arg, ":aad")) return (0);
830 if (k->ops->c->f&AEADF_AADNDEP)
831 VALERR("aad must be associated with enc/dec op");
832 rc = gaeadaad_pywrap((PyObject *)GCAEAD_AAD(me->ob_type),
833 GAEAD_AAD(k), 0, 0);
834 end:
835 return (rc);
836 }
837
838 static int check_aead_encdec(const gcaead *aec, unsigned *f_out, size_t nsz,
839 PyObject *hszobj, size_t *hsz_out,
840 PyObject *mszobj, size_t *msz_out,
841 PyObject *tszobj, size_t *tsz_out)
842 {
843 unsigned f = 0, miss;
844 int rc = -1;
845
846 if (hszobj != Py_None)
847 { f |= AEADF_PCHSZ; if (!convszt(hszobj, hsz_out)) goto end; }
848 if (mszobj != Py_None)
849 { f |= AEADF_PCMSZ; if (!convszt(mszobj, msz_out)) goto end; }
850 if (tszobj != Py_None)
851 { f |= AEADF_PCTSZ; if (!convszt(tszobj, tsz_out)) goto end; }
852 miss = aec->f&~f;
853 if (miss&AEADF_PCHSZ) VALERR("header length precommitment required");
854 if (miss&AEADF_PCMSZ) VALERR("message length precommitment required");
855 if (miss&AEADF_PCTSZ) VALERR("tag length precommitment required");
856 if (keysz(nsz, aec->noncesz) != nsz) VALERR("bad nonce length");
857 if (tszobj != Py_None && keysz(*tsz_out, aec->tagsz) != *tsz_out)
858 VALERR("bad tag length");
859 *f_out = f | aec->f; rc = 0;
860 end:
861 return (rc);
862 }
863
864 static PyObject *gaekmeth_enc(PyObject *me, PyObject *arg, PyObject *kw)
865 {
866 static const char *const kwlist[] = { "nonce", "hsz", "msz", "tsz", 0 };
867 const gaead_key *k = GAEADKEY_K(me);
868 gaead_enc *e;
869 PyObject *rc = 0;
870 char *n; Py_ssize_t nsz;
871 PyObject *hszobj = Py_None, *mszobj = Py_None, *tszobj = Py_None;
872 size_t hsz = 0, msz = 0, tsz = 0;
873 unsigned f;
874
875 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|OOO:enc", KWLIST,
876 &n, &nsz, &hszobj, &mszobj, &tszobj))
877 goto end;
878 if (check_aead_encdec(k->ops->c, &f, nsz,
879 hszobj, &hsz, mszobj, &msz, tszobj, &tsz))
880 goto end;
881 e = GAEAD_ENC(GAEADKEY_K(me), n, nsz, hsz, msz, tsz);
882 if (!e) VALERR("bad aead parameter combination");
883 rc = gaeadenc_pywrap((PyObject *)GCAEAD_ENC(me->ob_type),
884 e, f, hsz, msz, tsz);
885 end:
886 return (rc);
887 }
888
889 static PyObject *gaekmeth_dec(PyObject *me, PyObject *arg, PyObject *kw)
890 {
891 static const char *const kwlist[] = { "nonce", "hsz", "csz", "tsz", 0 };
892 const gaead_key *k = GAEADKEY_K(me);
893 gaead_dec *d;
894 PyObject *rc = 0;
895 char *n; Py_ssize_t nsz;
896 PyObject *hszobj = Py_None, *cszobj = Py_None, *tszobj = Py_None;
897 size_t hsz = 0, csz = 0, tsz = 0;
898 unsigned f;
899
900 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|OOO:dec", KWLIST,
901 &n, &nsz, &hszobj, &cszobj, &tszobj))
902 goto end;
903 if (check_aead_encdec(k->ops->c, &f, nsz,
904 hszobj, &hsz, cszobj, &csz, tszobj, &tsz))
905 goto end;
906 d = GAEAD_DEC(GAEADKEY_K(me), n, nsz, hsz, csz, tsz);
907 if (!d) VALERR("bad aead parameter combination");
908 rc = gaeaddec_pywrap((PyObject *)GCAEAD_DEC(me->ob_type),
909 d, f, hsz, csz, tsz);
910 end:
911 return (rc);
912 }
913
914 static PyMethodDef gaeadkey_pymethods[] = {
915 #define METHNAME(name) gaekmeth_##name
916 METH (aad, "KEY.aad() -> AAD")
917 KWMETH(enc, "KEY.enc(NONCE, [hsz], [msz], [tsz]) -> ENC")
918 KWMETH(dec, "KEY.dec(NONCE, [hsz], [csz], [tsz]) -> DEC")
919 #undef METHNAME
920 { 0 }
921 };
922
923 PyObject *gaeadaad_pywrap(PyObject *cobj, gaead_aad *a,
924 unsigned f, size_t hsz)
925 {
926 gaeadaad_pyobj *ga;
927
928 assert(cobj); Py_INCREF(cobj);
929 ga = PyObject_NEW(gaeadaad_pyobj, (PyTypeObject *)cobj);
930 ga->a = a; ga->f = f; ga->hsz = hsz; ga->hlen = 0;
931 return ((PyObject *)ga);
932 }
933
934 static void gaeadaad_pydealloc(PyObject *me)
935 {
936 gaeadaad_pyobj *ga = (gaeadaad_pyobj *)me;
937
938 if (ga->a) GAEAD_DESTROY(ga->a);
939 Py_DECREF(me->ob_type); FREEOBJ(me);
940 }
941
942 static int gaea_check(PyObject *me)
943 {
944 gaeadaad_pyobj *ga = (gaeadaad_pyobj *)me;
945 int rc = -1;
946
947 if ((ga->f&AEADF_DEAD) || !ga->a) VALERR("aad object no longer active");
948 rc = 0;
949 end:
950 return (rc);
951 }
952
953 static void gaea_invalidate(gaeadaad_pyobj *ga)
954 { if (ga) ga->f |= AEADF_DEAD; }
955
956 static void gaea_sever(gaeadaad_pyobj **ga_inout)
957 {
958 gaeadaad_pyobj *ga = *ga_inout;
959 if (ga) { ga->f |= AEADF_DEAD; ga->a = 0; Py_DECREF(ga); *ga_inout = 0; }
960 }
961
962 static PyObject *gaeaget_hsz(PyObject *me, void *hunoz)
963 {
964 if (gaea_check(me)) return (0);
965 else if (GAEADAAD_F(me)&AEADF_PCHSZ) return getulong(GAEADAAD_HSZ(me));
966 else RETURN_NONE;
967 }
968
969 static PyObject *gaeaget_hlen(PyObject *me, void *hunoz)
970 { return (gaea_check(me) ? 0 : getulong(GAEADAAD_HLEN(me))); }
971
972 static PyGetSetDef gaeadaad_pygetset[] = {
973 #define GETSETNAME(op, name) gaea##op##_##name
974 GET (hsz, "AAD.hsz -> precommitted header length or `None'")
975 GET (hlen, "AAD.hlen -> header length so far")
976 #undef GETSETNAME
977 { 0 }
978 };
979
980 static PyObject *gaeameth_copy(PyObject *me, PyObject *arg)
981 {
982 PyObject *rc = 0;
983
984 if (!PyArg_ParseTuple(arg, ":copy")) goto end;
985 if (gaea_check(me)) goto end;
986 if (GAEADAAD_F(me)&AEADF_AADNDEP)
987 VALERR("can't duplicate nonce-dependent aad");
988 rc = gaeadaad_pywrap((PyObject *)me->ob_type,
989 GAEAD_DUP(GAEADAAD_A(me)), 0, 0);
990 GAEADAAD_HLEN(rc) = GAEADAAD_HLEN(me);
991 end:
992 return (rc);
993 }
994
995 static int gaeadaad_hash(PyObject *me, const void *h, size_t hsz)
996 {
997 gaeadaad_pyobj *ga = (gaeadaad_pyobj *)me;
998 int rc = -1;
999
1000 if (gaea_check(me)) goto end;
1001 if ((ga->f&AEADF_NOAAD) && hsz)
1002 VALERR("header data not permitted");
1003 if ((ga->f&AEADF_PCHSZ) && hsz > ga->hsz - ga->hlen)
1004 VALERR("too large for precommitted header length");
1005 GAEAD_HASH(ga->a, h, hsz); ga->hlen += hsz;
1006 rc = 0;
1007 end:
1008 return (rc);
1009 }
1010
1011
1012 static PyObject *gaeameth_hash(PyObject *me, PyObject *arg)
1013 {
1014 char *h; Py_ssize_t hsz;
1015
1016 if (!PyArg_ParseTuple(arg, "s#:hash", &h, &hsz)) return (0);
1017 if (gaeadaad_hash(me, h, hsz)) return (0);
1018 RETURN_ME;
1019 }
1020
1021 #define GAEAMETH_HASHU_(n, W, w) \
1022 static PyObject *gaeameth_hashu##w(PyObject *me, PyObject *arg) \
1023 { \
1024 uint##n x; octet b[SZ_##W]; \
1025 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \
1026 STORE##W(b, x); if (gaeadaad_hash(me, b, sizeof(b))) return (0); \
1027 RETURN_ME; \
1028 }
1029 DOUINTCONV(GAEAMETH_HASHU_)
1030
1031 #define GAEAMETH_HASHBUF_(n, W, w) \
1032 static PyObject *gaeameth_hashbuf##w(PyObject *me, PyObject *arg) \
1033 { \
1034 char *p; Py_ssize_t sz; octet b[SZ_##W]; \
1035 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
1036 if (sz > MASK##n) TYERR("string too long"); \
1037 STORE##W(b, sz); if (gaeadaad_hash(me, b, sizeof(b))) goto end; \
1038 if (gaeadaad_hash(me, p, sz)) goto end; \
1039 RETURN_ME; \
1040 end: \
1041 return (0); \
1042 }
1043 DOUINTCONV(GAEAMETH_HASHBUF_)
1044
1045 static PyObject *gaeameth_hashstrz(PyObject *me, PyObject *arg)
1046 {
1047 char *p;
1048 if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0);
1049 if (gaeadaad_hash(me, p, strlen(p) + 1)) return (0);
1050 RETURN_ME;
1051 }
1052
1053 static PyMethodDef gaeadaad_pymethods[] = {
1054 #define METHNAME(name) gaeameth_##name
1055 METH (copy, "AAD.copy() -> AAD'")
1056 METH (hash, "AAD.hash(H)")
1057 #define METHU_(n, W, w) METH(hashu##w, "AAD.hashu" #w "(WORD)")
1058 DOUINTCONV(METHU_)
1059 #undef METHU_
1060 #define METHBUF_(n, W, w) METH(hashbuf##w, "AAD.hashbuf" #w "(BYTES)")
1061 DOUINTCONV(METHBUF_)
1062 #undef METHBUF_
1063 METH (hashstrz, "AAD.hashstrz(STRING)")
1064 #undef METHNAME
1065 { 0 }
1066 };
1067
1068 PyObject *gaeadenc_pywrap(PyObject *cobj, gaead_enc *e, unsigned f,
1069 size_t hsz, size_t msz, size_t tsz)
1070 {
1071 gaeadenc_pyobj *ge;
1072
1073 assert(cobj); Py_INCREF(cobj);
1074 ge = PyObject_NEW(gaeadenc_pyobj, (PyTypeObject *)cobj);
1075 ge->e = e; ge->f = f; ge->hsz = hsz; ge->msz = msz; ge->tsz = tsz;
1076 ge->aad = 0; ge->mlen = 0;
1077 return ((PyObject *)ge);
1078 }
1079
1080 static void gaeadenc_pydealloc(PyObject *me)
1081 {
1082 gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me;
1083
1084 gaea_sever(&ge->aad); GAEAD_DESTROY(ge->e);
1085 Py_DECREF(me->ob_type); FREEOBJ(me);
1086 }
1087
1088 static PyObject *gaeeget_hsz(PyObject *me, void *hunoz)
1089 {
1090 if (GAEADENC_F(me)&AEADF_PCHSZ) return getulong(GAEADENC_HSZ(me));
1091 else RETURN_NONE;
1092 }
1093
1094 static PyObject *gaeeget_msz(PyObject *me, void *hunoz)
1095 {
1096 if (GAEADENC_F(me)&AEADF_PCMSZ) return getulong(GAEADENC_MSZ(me));
1097 else RETURN_NONE;
1098 }
1099
1100 static PyObject *gaeeget_tsz(PyObject *me, void *hunoz)
1101 {
1102 if (GAEADENC_F(me)&AEADF_PCTSZ) return getulong(GAEADENC_TSZ(me));
1103 else RETURN_NONE;
1104 }
1105
1106 static PyObject *gaeeget_mlen(PyObject *me, void *hunoz)
1107 { return getulong(GAEADENC_MLEN(me)); }
1108
1109 static PyGetSetDef gaeadenc_pygetset[] = {
1110 #define GETSETNAME(op, name) gaee##op##_##name
1111 GET (hsz, "ENC.hsz -> precommitted header length or `None'")
1112 GET (msz, "ENC.msz -> precommitted message length or `None'")
1113 GET (tsz, "ENC.tsz -> precommitted tag length or `None'")
1114 GET (mlen, "ENC.mlen -> message length so far")
1115 #undef GETSETNAME
1116 { 0 }
1117 };
1118
1119 static PyObject *gaeemeth_aad(PyObject *me, PyObject *arg)
1120 {
1121 gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me;
1122 PyObject *rc = 0;
1123
1124 if (!PyArg_ParseTuple(arg, ":aad")) return (0);
1125 if (!(ge->f&AEADF_AADNDEP))
1126 rc = gaeadaad_pywrap((PyObject *)GCAEADENC_KEY(ge->ob_type)->aad,
1127 GAEAD_AAD(ge->e), 0, 0);
1128 else {
1129 if ((ge->f&AEADF_AADFIRST) && ge->mlen)
1130 VALERR("too late for aad");
1131 if (!ge->aad)
1132 ge->aad = (gaeadaad_pyobj *)
1133 gaeadaad_pywrap((PyObject *)GCAEADENC_KEY(ge->ob_type)->aad,
1134 GAEAD_AAD(ge->e), ge->f&(AEADF_PCHSZ | AEADF_NOAAD),
1135 ge->hsz);
1136 Py_INCREF(ge->aad);
1137 rc = (PyObject *)ge->aad;
1138 }
1139 end:
1140 return (rc);
1141 }
1142
1143 static PyObject *gaeemeth_reinit(PyObject *me, PyObject *arg, PyObject *kw)
1144 {
1145 static const char *const kwlist[] = { "nonce", "hsz", "msz", "tsz", 0 };
1146 gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me;
1147 char *n; Py_ssize_t nsz;
1148 PyObject *hszobj = Py_None, *mszobj = Py_None, *tszobj = Py_None;
1149 size_t hsz = 0, msz = 0, tsz = 0;
1150 unsigned f;
1151
1152 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|OOO:enc", KWLIST,
1153 &n, &nsz, &hszobj, &mszobj, &tszobj))
1154 goto end;
1155 if (check_aead_encdec(ge->e->ops->c, &f, nsz,
1156 hszobj, &hsz, mszobj, &msz, tszobj, &tsz))
1157 goto end;
1158 if (GAEAD_REINIT(ge->e, n, nsz, hsz, msz, tsz))
1159 VALERR("bad aead parameter combination");
1160 gaea_sever(&ge->aad);
1161 ge->f = f; ge->hsz = hsz; ge->msz = msz; ge->tsz = tsz;
1162 end:
1163 return (0);
1164 }
1165
1166 static PyObject *gaeemeth_encrypt(PyObject *me, PyObject *arg)
1167 {
1168 gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me;
1169 char *m; Py_ssize_t msz;
1170 char *c = 0; size_t csz; buf b;
1171 int err;
1172 PyObject *rc = 0;
1173
1174 if (!PyArg_ParseTuple(arg, "s#:encrypt", &m, &msz)) goto end;
1175 if (ge->f&AEADF_AADFIRST) {
1176 if ((ge->f&AEADF_PCHSZ) && (ge->aad ? ge->aad->hlen : 0) != ge->hsz)
1177 VALERR("header doesn't match precommitted length");
1178 gaea_invalidate(ge->aad);
1179 }
1180 if ((ge->f&AEADF_PCMSZ) && msz > ge->msz - ge->mlen)
1181 VALERR("too large for precommitted message length");
1182 csz = msz + ge->e->ops->c->bufsz; c = xmalloc(csz); buf_init(&b, c, csz);
1183 err = GAEAD_ENCRYPT(ge->e, m, msz, &b); assert(!err); (void)err;
1184 buf_flip(&b); rc = bytestring_pywrapbuf(&b); ge->mlen += msz;
1185 end:
1186 xfree(c);
1187 return (rc);
1188 }
1189
1190 static PyObject *gaeemeth_done(PyObject *me, PyObject *arg, PyObject *kw)
1191 {
1192 static const char *const kwlist[] = { "tsz", "aad", 0 };
1193 gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me;
1194 PyObject *aad = Py_None;
1195 char *c = 0; size_t csz; buf b;
1196 PyObject *tszobj = Py_None; PyObject *tag; size_t tsz;
1197 int err;
1198 PyObject *rc = 0;
1199
1200 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|OO:done", KWLIST,
1201 &tszobj, &aad))
1202 goto end;
1203 if (tszobj != Py_None && !convszt(tszobj, &tsz)) goto end;
1204 if (aad != Py_None &&
1205 !PyObject_TypeCheck(aad,
1206 (PyTypeObject *)GCAEADENC_KEY(me->ob_type)->aad))
1207 TYERR("wanted aad");
1208 if ((ge->f&AEADF_AADNDEP) && aad != Py_None && aad != (PyObject *)ge->aad)
1209 VALERR("mismatched aad");
1210 if ((ge->f&AEADF_PCHSZ) &&
1211 (aad == Py_None ? 0 : GAEADAAD_HLEN(aad)) != ge->hsz)
1212 VALERR("header doesn't match precommitted length");
1213 if ((ge->f&AEADF_PCMSZ) && ge->mlen != ge->msz)
1214 VALERR("message doesn't match precommitted length");
1215 if (tszobj == Py_None) {
1216 if (ge->f&AEADF_PCTSZ) tsz = ge->tsz;
1217 else tsz = keysz(0, ge->e->ops->c->tagsz);
1218 } else {
1219 if ((ge->f&AEADF_PCTSZ) && tsz != ge->tsz)
1220 VALERR("tag length doesn't match precommitted value");
1221 if (keysz(tsz, ge->e->ops->c->tagsz) != tsz) VALERR("bad tag length");
1222 }
1223 csz = ge->e->ops->c->bufsz; c = xmalloc(csz); buf_init(&b, c, csz);
1224 tag = bytestring_pywrap(0, tsz);
1225 err = GAEAD_DONE(ge->e, aad == Py_None ? 0 : GAEADAAD_A(aad), &b,
1226 PyString_AS_STRING(tag), tsz);
1227 assert(!err); (void)err;
1228 buf_flip(&b); rc = Py_BuildValue("NN", bytestring_pywrapbuf(&b), tag);
1229 end:
1230 xfree(c);
1231 return (rc);
1232 }
1233
1234 static PyMethodDef gaeadenc_pymethods[] = {
1235 #define METHNAME(name) gaeemeth_##name
1236 METH (aad, "ENC.aad() -> AAD")
1237 KWMETH(reinit, "ENC.reinit(NONCE, [hsz], [msz], [tsz])")
1238 METH (encrypt, "ENC.encrypt(MSG) -> CT")
1239 KWMETH(done, "ENC.done([tsz], [aad]) -> CT, TAG")
1240 #undef METHNAME
1241 { 0 }
1242 };
1243
1244 PyObject *gaeaddec_pywrap(PyObject *cobj, gaead_dec *d, unsigned f,
1245 size_t hsz, size_t csz, size_t tsz)
1246 {
1247 gaeaddec_pyobj *gd;
1248 assert(cobj); Py_INCREF(cobj);
1249 gd = PyObject_NEW(gaeaddec_pyobj, (PyTypeObject *)cobj);
1250 gd->d = d; gd->f = f; gd->hsz = hsz; gd->csz = csz; gd->tsz = tsz;
1251 gd->aad = 0; gd->clen = 0;
1252 return ((PyObject *)gd);
1253 }
1254
1255 static void gaeaddec_pydealloc(PyObject *me)
1256 {
1257 gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me;
1258
1259 gaea_sever(&gd->aad); GAEAD_DESTROY(GAEADDEC_D(me));
1260 Py_DECREF(me->ob_type); FREEOBJ(me);
1261 }
1262
1263 static PyObject *gaedget_hsz(PyObject *me, void *hunoz)
1264 {
1265 if (GAEADDEC_F(me)&AEADF_PCHSZ) return getulong(GAEADDEC_HSZ(me));
1266 else RETURN_NONE;
1267 }
1268
1269 static PyObject *gaedget_csz(PyObject *me, void *hunoz)
1270 {
1271 if (GAEADDEC_F(me)&AEADF_PCMSZ) return getulong(GAEADDEC_CSZ(me));
1272 else RETURN_NONE;
1273 }
1274
1275 static PyObject *gaedget_tsz(PyObject *me, void *hunoz)
1276 {
1277 if (GAEADDEC_F(me)&AEADF_PCTSZ) return getulong(GAEADDEC_TSZ(me));
1278 else RETURN_NONE;
1279 }
1280
1281 static PyObject *gaedget_clen(PyObject *me, void *hunoz)
1282 { return getulong(GAEADDEC_CLEN(me)); }
1283
1284 static PyGetSetDef gaeaddec_pygetset[] = {
1285 #define GETSETNAME(op, name) gaed##op##_##name
1286 GET (hsz, "DEC.hsz -> precommitted header length or `None'")
1287 GET (csz, "DEC.csz -> precommitted ciphertext length or `None'")
1288 GET (tsz, "DEC.tsz -> precommitted tag length or `None'")
1289 GET (clen, "DEC.clen -> ciphertext length so far")
1290 #undef GETSETNAME
1291 { 0 }
1292 };
1293
1294 static PyObject *gaedmeth_aad(PyObject *me, PyObject *arg)
1295 {
1296 gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me;
1297
1298 if (!PyArg_ParseTuple(arg, ":aad")) return (0);
1299 if (!(gd->f&AEADF_AADNDEP))
1300 return (gaeadaad_pywrap((PyObject *)GCAEADDEC_KEY(gd->ob_type)->aad,
1301 GAEAD_AAD(gd->d), 0, 0));
1302 else {
1303 if (!gd->aad)
1304 gd->aad = (gaeadaad_pyobj *)
1305 gaeadaad_pywrap((PyObject *)GCAEADENC_KEY(gd->ob_type)->aad,
1306 GAEAD_AAD(gd->d), gd->f&(AEADF_PCHSZ | AEADF_NOAAD),
1307 gd->hsz);
1308 Py_INCREF(gd->aad);
1309 return ((PyObject *)gd->aad);
1310 }
1311 }
1312
1313 static PyObject *gaedmeth_reinit(PyObject *me, PyObject *arg, PyObject *kw)
1314 {
1315 static const char *const kwlist[] = { "nonce", "hsz", "csz", "tsz", 0 };
1316 gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me;
1317 char *n; Py_ssize_t nsz;
1318 PyObject *hszobj = Py_None, *cszobj = Py_None, *tszobj = Py_None;
1319 size_t hsz = 0, csz = 0, tsz = 0;
1320 unsigned f;
1321
1322 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|OOO:enc", KWLIST,
1323 &n, &nsz, &hszobj, &cszobj, &tszobj))
1324 goto end;
1325 if (check_aead_encdec(gd->d->ops->c, &f, nsz,
1326 hszobj, &hsz, cszobj, &csz, tszobj, &tsz))
1327 goto end;
1328 if (GAEAD_REINIT(gd->d, n, nsz, hsz, csz, tsz))
1329 VALERR("bad aead parameter combination");
1330 gaea_sever(&gd->aad);
1331 gd->f = f; gd->hsz = hsz; gd->csz = csz; gd->tsz = tsz;
1332 end:
1333 return (0);
1334 }
1335
1336 static PyObject *gaedmeth_decrypt(PyObject *me, PyObject *arg)
1337 {
1338 gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me;
1339 char *c; Py_ssize_t csz;
1340 char *m = 0; size_t msz; buf b;
1341 int err;
1342 PyObject *rc = 0;
1343
1344 if (!PyArg_ParseTuple(arg, "s#:decrypt", &c, &csz)) goto end;
1345 if (gd->f&AEADF_AADFIRST) {
1346 if ((gd->f&AEADF_PCHSZ) && (gd->aad ? gd->aad->hlen : 0) != gd->hsz)
1347 VALERR("header doesn't match precommitted length");
1348 gaea_invalidate(gd->aad);
1349 }
1350 if ((gd->f&AEADF_PCMSZ) && csz > gd->csz - gd->clen)
1351 VALERR("too large for precommitted message length");
1352 msz = csz + gd->d->ops->c->bufsz; m = xmalloc(msz); buf_init(&b, m, msz);
1353 err = GAEAD_DECRYPT(gd->d, c, csz, &b); assert(!err); (void)err;
1354 buf_flip(&b); rc = bytestring_pywrapbuf(&b); gd->clen += csz;
1355 end:
1356 xfree(m);
1357 return (rc);
1358 }
1359
1360 static PyObject *gaedmeth_done(PyObject *me, PyObject *arg, PyObject *kw)
1361 {
1362 static const char *const kwlist[] = { "tag", "aad", 0 };
1363 gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me;
1364 PyObject *aad = Py_None;
1365 char *t; Py_ssize_t tsz;
1366 char *m = 0; size_t msz; buf b;
1367 int err;
1368 PyObject *rc = 0;
1369
1370 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|O:done", KWLIST,
1371 &t, &tsz, &aad))
1372 goto end;
1373 if (aad != Py_None &&
1374 !PyObject_TypeCheck(aad,
1375 (PyTypeObject *)GCAEADENC_KEY(me->ob_type)->aad))
1376 TYERR("wanted aad");
1377 if ((gd->f&AEADF_AADNDEP) && aad != Py_None && aad != (PyObject *)gd->aad)
1378 VALERR("mismatched aad");
1379 if ((gd->f&AEADF_PCHSZ) &&
1380 (aad == Py_None ? 0 : GAEADAAD_HLEN(aad)) != gd->hsz)
1381 VALERR("header doesn't match precommitted length");
1382 if ((gd->f&AEADF_PCMSZ) && gd->clen != gd->csz)
1383 VALERR("message doesn't match precommitted length");
1384 if ((gd->f&AEADF_PCTSZ) && tsz != gd->tsz)
1385 VALERR("tag length doesn't match precommitted value");
1386 if (keysz(tsz, gd->d->ops->c->tagsz) != tsz) VALERR("bad tag length");
1387 msz = gd->d->ops->c->bufsz; m = xmalloc(msz); buf_init(&b, m, msz);
1388 err = GAEAD_DONE(gd->d, aad == Py_None ? 0 : GAEADAAD_A(aad), &b, t, tsz);
1389 assert(err >= 0);
1390 if (!err) VALERR("decryption failed");
1391 buf_flip(&b); rc = bytestring_pywrapbuf(&b);
1392 end:
1393 xfree(m);
1394 return (rc);
1395 }
1396
1397 static PyMethodDef gaeaddec_pymethods[] = {
1398 #define METHNAME(name) gaedmeth_##name
1399 METH (aad, "DEC.aad() -> AAD")
1400 KWMETH(reinit, "DEC.reinit(NONCE, [hsz], [csz], [tsz])")
1401 METH (decrypt, "DEC.decrypt(CT) -> MSG")
1402 KWMETH(done, "DEC.done(TAG, [aad]) -> MSG | None")
1403 #undef METHNAME
1404 { 0 }
1405 };
1406
1407 static PyTypeObject gcaead_pytype_skel = {
1408 PyObject_HEAD_INIT(0) 0, /* Header */
1409 "GCAEAD", /* @tp_name@ */
1410 sizeof(gcaead_pyobj), /* @tp_basicsize@ */
1411 0, /* @tp_itemsize@ */
1412
1413 0, /* @tp_dealloc@ */
1414 0, /* @tp_print@ */
1415 0, /* @tp_getattr@ */
1416 0, /* @tp_setattr@ */
1417 0, /* @tp_compare@ */
1418 0, /* @tp_repr@ */
1419 0, /* @tp_as_number@ */
1420 0, /* @tp_as_sequence@ */
1421 0, /* @tp_as_mapping@ */
1422 0, /* @tp_hash@ */
1423 0, /* @tp_call@ */
1424 0, /* @tp_str@ */
1425 0, /* @tp_getattro@ */
1426 0, /* @tp_setattro@ */
1427 0, /* @tp_as_buffer@ */
1428 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1429 Py_TPFLAGS_BASETYPE,
1430
1431 /* @tp_doc@ */
1432 "Authenticated encryption (key) metaclass.",
1433
1434 0, /* @tp_traverse@ */
1435 0, /* @tp_clear@ */
1436 0, /* @tp_richcompare@ */
1437 0, /* @tp_weaklistoffset@ */
1438 0, /* @tp_iter@ */
1439 0, /* @tp_iternext@ */
1440 0, /* @tp_methods@ */
1441 0, /* @tp_members@ */
1442 gcaead_pygetset, /* @tp_getset@ */
1443 0, /* @tp_base@ */
1444 0, /* @tp_dict@ */
1445 0, /* @tp_descr_get@ */
1446 0, /* @tp_descr_set@ */
1447 0, /* @tp_dictoffset@ */
1448 0, /* @tp_init@ */
1449 PyType_GenericAlloc, /* @tp_alloc@ */
1450 abstract_pynew, /* @tp_new@ */
1451 0, /* @tp_free@ */
1452 0 /* @tp_is_gc@ */
1453 };
1454
1455 static PyTypeObject gaeadkey_pytype_skel = {
1456 PyObject_HEAD_INIT(0) 0, /* Header */
1457 "GAEKey", /* @tp_name@ */
1458 sizeof(gaeadkey_pyobj), /* @tp_basicsize@ */
1459 0, /* @tp_itemsize@ */
1460
1461 gaeadkey_pydealloc, /* @tp_dealloc@ */
1462 0, /* @tp_print@ */
1463 0, /* @tp_getattr@ */
1464 0, /* @tp_setattr@ */
1465 0, /* @tp_compare@ */
1466 0, /* @tp_repr@ */
1467 0, /* @tp_as_number@ */
1468 0, /* @tp_as_sequence@ */
1469 0, /* @tp_as_mapping@ */
1470 0, /* @tp_hash@ */
1471 0, /* @tp_call@ */
1472 0, /* @tp_str@ */
1473 0, /* @tp_getattro@ */
1474 0, /* @tp_setattro@ */
1475 0, /* @tp_as_buffer@ */
1476 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1477 Py_TPFLAGS_BASETYPE,
1478
1479 /* @tp_doc@ */
1480 "Authenticated encryption key.",
1481
1482 0, /* @tp_traverse@ */
1483 0, /* @tp_clear@ */
1484 0, /* @tp_richcompare@ */
1485 0, /* @tp_weaklistoffset@ */
1486 0, /* @tp_iter@ */
1487 0, /* @tp_iternext@ */
1488 gaeadkey_pymethods, /* @tp_methods@ */
1489 0, /* @tp_members@ */
1490 0, /* @tp_getset@ */
1491 0, /* @tp_base@ */
1492 0, /* @tp_dict@ */
1493 0, /* @tp_descr_get@ */
1494 0, /* @tp_descr_set@ */
1495 0, /* @tp_dictoffset@ */
1496 0, /* @tp_init@ */
1497 PyType_GenericAlloc, /* @tp_alloc@ */
1498 abstract_pynew, /* @tp_new@ */
1499 0, /* @tp_free@ */
1500 0 /* @tp_is_gc@ */
1501 };
1502
1503 static PyTypeObject gcaeadaad_pytype_skel = {
1504 PyObject_HEAD_INIT(0) 0, /* Header */
1505 "GAEAADClass", /* @tp_name@ */
1506 sizeof(gcaeadaad_pyobj), /* @tp_basicsize@ */
1507 0, /* @tp_itemsize@ */
1508
1509 0, /* @tp_dealloc@ */
1510 0, /* @tp_print@ */
1511 0, /* @tp_getattr@ */
1512 0, /* @tp_setattr@ */
1513 0, /* @tp_compare@ */
1514 0, /* @tp_repr@ */
1515 0, /* @tp_as_number@ */
1516 0, /* @tp_as_sequence@ */
1517 0, /* @tp_as_mapping@ */
1518 0, /* @tp_hash@ */
1519 0, /* @tp_call@ */
1520 0, /* @tp_str@ */
1521 0, /* @tp_getattro@ */
1522 0, /* @tp_setattro@ */
1523 0, /* @tp_as_buffer@ */
1524 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1525 Py_TPFLAGS_BASETYPE,
1526
1527 /* @tp_doc@ */
1528 "Authenticated encryption additional-data hash metaclass.",
1529
1530 0, /* @tp_traverse@ */
1531 0, /* @tp_clear@ */
1532 0, /* @tp_richcompare@ */
1533 0, /* @tp_weaklistoffset@ */
1534 0, /* @tp_iter@ */
1535 0, /* @tp_iternext@ */
1536 0, /* @tp_methods@ */
1537 0, /* @tp_members@ */
1538 0, /* @tp_getset@ */
1539 0, /* @tp_base@ */
1540 0, /* @tp_dict@ */
1541 0, /* @tp_descr_get@ */
1542 0, /* @tp_descr_set@ */
1543 0, /* @tp_dictoffset@ */
1544 0, /* @tp_init@ */
1545 PyType_GenericAlloc, /* @tp_alloc@ */
1546 abstract_pynew, /* @tp_new@ */
1547 0, /* @tp_free@ */
1548 0 /* @tp_is_gc@ */
1549 };
1550
1551 static PyTypeObject gaeadaad_pytype_skel = {
1552 PyObject_HEAD_INIT(0) 0, /* Header */
1553 "GAEAAD", /* @tp_name@ */
1554 sizeof(gaeadaad_pyobj), /* @tp_basicsize@ */
1555 0, /* @tp_itemsize@ */
1556
1557 gaeadaad_pydealloc, /* @tp_dealloc@ */
1558 0, /* @tp_print@ */
1559 0, /* @tp_getattr@ */
1560 0, /* @tp_setattr@ */
1561 0, /* @tp_compare@ */
1562 0, /* @tp_repr@ */
1563 0, /* @tp_as_number@ */
1564 0, /* @tp_as_sequence@ */
1565 0, /* @tp_as_mapping@ */
1566 0, /* @tp_hash@ */
1567 0, /* @tp_call@ */
1568 0, /* @tp_str@ */
1569 0, /* @tp_getattro@ */
1570 0, /* @tp_setattro@ */
1571 0, /* @tp_as_buffer@ */
1572 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1573 Py_TPFLAGS_BASETYPE,
1574
1575 /* @tp_doc@ */
1576 "Authenticated encryption AAD hash.",
1577
1578 0, /* @tp_traverse@ */
1579 0, /* @tp_clear@ */
1580 0, /* @tp_richcompare@ */
1581 0, /* @tp_weaklistoffset@ */
1582 0, /* @tp_iter@ */
1583 0, /* @tp_iternext@ */
1584 gaeadaad_pymethods, /* @tp_methods@ */
1585 0, /* @tp_members@ */
1586 gaeadaad_pygetset, /* @tp_getset@ */
1587 0, /* @tp_base@ */
1588 0, /* @tp_dict@ */
1589 0, /* @tp_descr_get@ */
1590 0, /* @tp_descr_set@ */
1591 0, /* @tp_dictoffset@ */
1592 0, /* @tp_init@ */
1593 PyType_GenericAlloc, /* @tp_alloc@ */
1594 abstract_pynew, /* @tp_new@ */
1595 0, /* @tp_free@ */
1596 0 /* @tp_is_gc@ */
1597 };
1598
1599 static PyTypeObject gcaeadenc_pytype_skel = {
1600 PyObject_HEAD_INIT(0) 0, /* Header */
1601 "GAEEncClass", /* @tp_name@ */
1602 sizeof(gcaeadenc_pyobj), /* @tp_basicsize@ */
1603 0, /* @tp_itemsize@ */
1604
1605 0, /* @tp_dealloc@ */
1606 0, /* @tp_print@ */
1607 0, /* @tp_getattr@ */
1608 0, /* @tp_setattr@ */
1609 0, /* @tp_compare@ */
1610 0, /* @tp_repr@ */
1611 0, /* @tp_as_number@ */
1612 0, /* @tp_as_sequence@ */
1613 0, /* @tp_as_mapping@ */
1614 0, /* @tp_hash@ */
1615 0, /* @tp_call@ */
1616 0, /* @tp_str@ */
1617 0, /* @tp_getattro@ */
1618 0, /* @tp_setattro@ */
1619 0, /* @tp_as_buffer@ */
1620 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1621 Py_TPFLAGS_BASETYPE,
1622
1623 /* @tp_doc@ */
1624 "Authenticated encryption operation metaclass.",
1625
1626 0, /* @tp_traverse@ */
1627 0, /* @tp_clear@ */
1628 0, /* @tp_richcompare@ */
1629 0, /* @tp_weaklistoffset@ */
1630 0, /* @tp_iter@ */
1631 0, /* @tp_iternext@ */
1632 0, /* @tp_methods@ */
1633 0, /* @tp_members@ */
1634 0, /* @tp_getset@ */
1635 0, /* @tp_base@ */
1636 0, /* @tp_dict@ */
1637 0, /* @tp_descr_get@ */
1638 0, /* @tp_descr_set@ */
1639 0, /* @tp_dictoffset@ */
1640 0, /* @tp_init@ */
1641 PyType_GenericAlloc, /* @tp_alloc@ */
1642 abstract_pynew, /* @tp_new@ */
1643 0, /* @tp_free@ */
1644 0 /* @tp_is_gc@ */
1645 };
1646
1647 static PyTypeObject gaeadenc_pytype_skel = {
1648 PyObject_HEAD_INIT(0) 0, /* Header */
1649 "GAEEnc", /* @tp_name@ */
1650 sizeof(gaeadenc_pyobj), /* @tp_basicsize@ */
1651 0, /* @tp_itemsize@ */
1652
1653 gaeadenc_pydealloc, /* @tp_dealloc@ */
1654 0, /* @tp_print@ */
1655 0, /* @tp_getattr@ */
1656 0, /* @tp_setattr@ */
1657 0, /* @tp_compare@ */
1658 0, /* @tp_repr@ */
1659 0, /* @tp_as_number@ */
1660 0, /* @tp_as_sequence@ */
1661 0, /* @tp_as_mapping@ */
1662 0, /* @tp_hash@ */
1663 0, /* @tp_call@ */
1664 0, /* @tp_str@ */
1665 0, /* @tp_getattro@ */
1666 0, /* @tp_setattro@ */
1667 0, /* @tp_as_buffer@ */
1668 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1669 Py_TPFLAGS_BASETYPE,
1670
1671 /* @tp_doc@ */
1672 "Authenticated encryption operation.",
1673
1674 0, /* @tp_traverse@ */
1675 0, /* @tp_clear@ */
1676 0, /* @tp_richcompare@ */
1677 0, /* @tp_weaklistoffset@ */
1678 0, /* @tp_iter@ */
1679 0, /* @tp_iternext@ */
1680 gaeadenc_pymethods, /* @tp_methods@ */
1681 0, /* @tp_members@ */
1682 gaeadenc_pygetset, /* @tp_getset@ */
1683 0, /* @tp_base@ */
1684 0, /* @tp_dict@ */
1685 0, /* @tp_descr_get@ */
1686 0, /* @tp_descr_set@ */
1687 0, /* @tp_dictoffset@ */
1688 0, /* @tp_init@ */
1689 PyType_GenericAlloc, /* @tp_alloc@ */
1690 abstract_pynew, /* @tp_new@ */
1691 0, /* @tp_free@ */
1692 0 /* @tp_is_gc@ */
1693 };
1694
1695 static PyTypeObject gcaeaddec_pytype_skel = {
1696 PyObject_HEAD_INIT(0) 0, /* Header */
1697 "GAEDecClass", /* @tp_name@ */
1698 sizeof(gcaeaddec_pyobj), /* @tp_basicsize@ */
1699 0, /* @tp_itemsize@ */
1700
1701 0, /* @tp_dealloc@ */
1702 0, /* @tp_print@ */
1703 0, /* @tp_getattr@ */
1704 0, /* @tp_setattr@ */
1705 0, /* @tp_compare@ */
1706 0, /* @tp_repr@ */
1707 0, /* @tp_as_number@ */
1708 0, /* @tp_as_sequence@ */
1709 0, /* @tp_as_mapping@ */
1710 0, /* @tp_hash@ */
1711 0, /* @tp_call@ */
1712 0, /* @tp_str@ */
1713 0, /* @tp_getattro@ */
1714 0, /* @tp_setattro@ */
1715 0, /* @tp_as_buffer@ */
1716 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1717 Py_TPFLAGS_BASETYPE,
1718
1719 /* @tp_doc@ */
1720 "Authenticated decryption operation metaclass.",
1721
1722 0, /* @tp_traverse@ */
1723 0, /* @tp_clear@ */
1724 0, /* @tp_richcompare@ */
1725 0, /* @tp_weaklistoffset@ */
1726 0, /* @tp_iter@ */
1727 0, /* @tp_iternext@ */
1728 0, /* @tp_methods@ */
1729 0, /* @tp_members@ */
1730 0, /* @tp_getset@ */
1731 0, /* @tp_base@ */
1732 0, /* @tp_dict@ */
1733 0, /* @tp_descr_get@ */
1734 0, /* @tp_descr_set@ */
1735 0, /* @tp_dictoffset@ */
1736 0, /* @tp_init@ */
1737 PyType_GenericAlloc, /* @tp_alloc@ */
1738 abstract_pynew, /* @tp_new@ */
1739 0, /* @tp_free@ */
1740 0 /* @tp_is_gc@ */
1741 };
1742
1743 static PyTypeObject gaeaddec_pytype_skel = {
1744 PyObject_HEAD_INIT(0) 0, /* Header */
1745 "GAEDec", /* @tp_name@ */
1746 sizeof(gaeaddec_pyobj), /* @tp_basicsize@ */
1747 0, /* @tp_itemsize@ */
1748
1749 gaeaddec_pydealloc, /* @tp_dealloc@ */
1750 0, /* @tp_print@ */
1751 0, /* @tp_getattr@ */
1752 0, /* @tp_setattr@ */
1753 0, /* @tp_compare@ */
1754 0, /* @tp_repr@ */
1755 0, /* @tp_as_number@ */
1756 0, /* @tp_as_sequence@ */
1757 0, /* @tp_as_mapping@ */
1758 0, /* @tp_hash@ */
1759 0, /* @tp_call@ */
1760 0, /* @tp_str@ */
1761 0, /* @tp_getattro@ */
1762 0, /* @tp_setattro@ */
1763 0, /* @tp_as_buffer@ */
1764 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1765 Py_TPFLAGS_BASETYPE,
1766
1767 /* @tp_doc@ */
1768 "Authenticated decryption operation.",
1769
1770 0, /* @tp_traverse@ */
1771 0, /* @tp_clear@ */
1772 0, /* @tp_richcompare@ */
1773 0, /* @tp_weaklistoffset@ */
1774 0, /* @tp_iter@ */
1775 0, /* @tp_iternext@ */
1776 gaeaddec_pymethods, /* @tp_methods@ */
1777 0, /* @tp_members@ */
1778 gaeaddec_pygetset, /* @tp_getset@ */
1779 0, /* @tp_base@ */
1780 0, /* @tp_dict@ */
1781 0, /* @tp_descr_get@ */
1782 0, /* @tp_descr_set@ */
1783 0, /* @tp_dictoffset@ */
1784 0, /* @tp_init@ */
1785 PyType_GenericAlloc, /* @tp_alloc@ */
1786 abstract_pynew, /* @tp_new@ */
1787 0, /* @tp_free@ */
1788 0 /* @tp_is_gc@ */
1789 };
1790
1791 /*----- Hash functions ----------------------------------------------------*/
1792
1793 PyTypeObject *gchash_pytype, *ghash_pytype;
1794
1795 CONVFUNC(gchash, gchash *, GCHASH_CH)
1796 CONVFUNC(ghash, ghash *, GHASH_H)
1797
1798 static PyObject *ghash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1799 {
1800 static const char *const kwlist[] = { 0 };
1801 if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", KWLIST))
1802 goto end;
1803 return (ghash_pywrap((PyObject *)ty, GH_INIT(GCHASH_CH(ty))));
1804 end:
1805 return (0);
1806 }
1807
1808 PyObject *gchash_pywrap(gchash *ch)
1809 {
1810 gchash_pyobj *g = newtype(gchash_pytype, 0, ch->name);
1811 g->ch = ch;
1812 g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj);
1813 g->ty.ht_type.tp_base = ghash_pytype;
1814 Py_INCREF(ghash_pytype);
1815 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
1816 Py_TPFLAGS_BASETYPE |
1817 Py_TPFLAGS_HEAPTYPE);
1818 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
1819 g->ty.ht_type.tp_free = 0;
1820 g->ty.ht_type.tp_new = ghash_pynew;
1821 typeready(&g->ty.ht_type);
1822 return ((PyObject *)g);
1823 }
1824
1825 PyObject *ghash_pywrap(PyObject *cobj, ghash *h)
1826 {
1827 ghash_pyobj *g;
1828 if (!cobj) cobj = gchash_pywrap((/*unconst*/ gchash *)GH_CLASS(h));
1829 else Py_INCREF(cobj);
1830 g = PyObject_NEW(ghash_pyobj, (PyTypeObject *)cobj);
1831 g->h = h;
1832 return ((PyObject *)g);
1833 }
1834
1835 static void ghash_pydealloc(PyObject *me)
1836 {
1837 GH_DESTROY(GHASH_H(me));
1838 Py_DECREF(me->ob_type);
1839 FREEOBJ(me);
1840 }
1841
1842 static PyObject *gchget_name(PyObject *me, void *hunoz)
1843 { return (PyString_FromString(GCHASH_CH(me)->name)); }
1844
1845 static PyObject *gchget_hashsz(PyObject *me, void *hunoz)
1846 { return (PyInt_FromLong(GCHASH_CH(me)->hashsz)); }
1847
1848 static PyObject *gchget_bufsz(PyObject *me, void *hunoz)
1849 { return (PyInt_FromLong(GCHASH_CH(me)->bufsz)); }
1850
1851 static PyObject *ghmeth_copy(PyObject *me, PyObject *arg)
1852 {
1853 if (!PyArg_ParseTuple(arg, ":copy")) return (0);
1854 return (ghash_pywrap((PyObject *)me->ob_type, GH_COPY(GHASH_H(me))));
1855 }
1856
1857 static PyObject *ghmeth_hash(PyObject *me, PyObject *arg)
1858 {
1859 char *p;
1860 Py_ssize_t sz;
1861 if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0);
1862 GH_HASH(GHASH_H(me), p, sz);
1863 RETURN_ME;
1864 }
1865
1866 #define GHMETH_HASHU_(n, W, w) \
1867 static PyObject *ghmeth_hashu##w(PyObject *me, PyObject *arg) \
1868 { \
1869 uint##n x; \
1870 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \
1871 GH_HASHU##W(GHASH_H(me), x); \
1872 RETURN_ME; \
1873 }
1874 DOUINTCONV(GHMETH_HASHU_)
1875
1876 #define GHMETH_HASHBUF_(n, W, w) \
1877 static PyObject *ghmeth_hashbuf##w(PyObject *me, PyObject *arg) \
1878 { \
1879 char *p; \
1880 Py_ssize_t sz; \
1881 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
1882 if (sz > MASK##n) TYERR("string too long"); \
1883 GH_HASHBUF##W(GHASH_H(me), p, sz); \
1884 RETURN_ME; \
1885 end: \
1886 return (0); \
1887 }
1888 DOUINTCONV(GHMETH_HASHBUF_)
1889
1890 static PyObject *ghmeth_hashstrz(PyObject *me, PyObject *arg)
1891 {
1892 char *p;
1893 if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0);
1894 GH_HASHSTRZ(GHASH_H(me), p);
1895 RETURN_ME;
1896 }
1897
1898 static PyObject *ghmeth_done(PyObject *me, PyObject *arg)
1899 {
1900 ghash *g;
1901 PyObject *rc;
1902 if (!PyArg_ParseTuple(arg, ":done")) return (0);
1903 g = GH_COPY(GHASH_H(me));
1904 rc = bytestring_pywrap(0, g->ops->c->hashsz);
1905 GH_DONE(g, PyString_AS_STRING(rc));
1906 GH_DESTROY(g);
1907 return (rc);
1908 }
1909
1910 static PyGetSetDef gchash_pygetset[] = {
1911 #define GETSETNAME(op, name) gch##op##_##name
1912 GET (bufsz, "CH.bufsz -> hash buffer size, or zero")
1913 GET (hashsz, "CH.hashsz -> hash output size")
1914 GET (name, "CH.name -> name of this kind of hash")
1915 #undef GETSETNAME
1916 { 0 }
1917 };
1918
1919 static PyMethodDef ghash_pymethods[] = {
1920 #define METHNAME(name) ghmeth_##name
1921 METH (copy, "H.copy() -> HH")
1922 METH (hash, "H.hash(M)")
1923 #define METHU_(n, W, w) METH(hashu##w, "H.hashu" #w "(WORD)")
1924 DOUINTCONV(METHU_)
1925 #undef METHU_
1926 #define METHBUF_(n, W, w) METH(hashbuf##w, "H.hashbuf" #w "(BYTES)")
1927 DOUINTCONV(METHBUF_)
1928 #undef METHBUF_
1929 METH (hashstrz, "H.hashstrz(STRING)")
1930 METH (done, "H.done() -> HASH")
1931 #undef METHNAME
1932 { 0 }
1933 };
1934
1935 static PyTypeObject gchash_pytype_skel = {
1936 PyObject_HEAD_INIT(0) 0, /* Header */
1937 "GCHash", /* @tp_name@ */
1938 sizeof(gchash_pyobj), /* @tp_basicsize@ */
1939 0, /* @tp_itemsize@ */
1940
1941 0, /* @tp_dealloc@ */
1942 0, /* @tp_print@ */
1943 0, /* @tp_getattr@ */
1944 0, /* @tp_setattr@ */
1945 0, /* @tp_compare@ */
1946 0, /* @tp_repr@ */
1947 0, /* @tp_as_number@ */
1948 0, /* @tp_as_sequence@ */
1949 0, /* @tp_as_mapping@ */
1950 0, /* @tp_hash@ */
1951 0, /* @tp_call@ */
1952 0, /* @tp_str@ */
1953 0, /* @tp_getattro@ */
1954 0, /* @tp_setattro@ */
1955 0, /* @tp_as_buffer@ */
1956 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1957 Py_TPFLAGS_BASETYPE,
1958
1959 /* @tp_doc@ */
1960 "Hash function metaclass.",
1961
1962 0, /* @tp_traverse@ */
1963 0, /* @tp_clear@ */
1964 0, /* @tp_richcompare@ */
1965 0, /* @tp_weaklistoffset@ */
1966 0, /* @tp_iter@ */
1967 0, /* @tp_iternext@ */
1968 0, /* @tp_methods@ */
1969 0, /* @tp_members@ */
1970 gchash_pygetset, /* @tp_getset@ */
1971 0, /* @tp_base@ */
1972 0, /* @tp_dict@ */
1973 0, /* @tp_descr_get@ */
1974 0, /* @tp_descr_set@ */
1975 0, /* @tp_dictoffset@ */
1976 0, /* @tp_init@ */
1977 PyType_GenericAlloc, /* @tp_alloc@ */
1978 abstract_pynew, /* @tp_new@ */
1979 0, /* @tp_free@ */
1980 0 /* @tp_is_gc@ */
1981 };
1982
1983 static PyTypeObject ghash_pytype_skel = {
1984 PyObject_HEAD_INIT(0) 0, /* Header */
1985 "GHash", /* @tp_name@ */
1986 sizeof(ghash_pyobj), /* @tp_basicsize@ */
1987 0, /* @tp_itemsize@ */
1988
1989 ghash_pydealloc, /* @tp_dealloc@ */
1990 0, /* @tp_print@ */
1991 0, /* @tp_getattr@ */
1992 0, /* @tp_setattr@ */
1993 0, /* @tp_compare@ */
1994 0, /* @tp_repr@ */
1995 0, /* @tp_as_number@ */
1996 0, /* @tp_as_sequence@ */
1997 0, /* @tp_as_mapping@ */
1998 0, /* @tp_hash@ */
1999 0, /* @tp_call@ */
2000 0, /* @tp_str@ */
2001 0, /* @tp_getattro@ */
2002 0, /* @tp_setattro@ */
2003 0, /* @tp_as_buffer@ */
2004 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2005 Py_TPFLAGS_BASETYPE,
2006
2007 /* @tp_doc@ */
2008 "Hash function, abstract base class.",
2009
2010 0, /* @tp_traverse@ */
2011 0, /* @tp_clear@ */
2012 0, /* @tp_richcompare@ */
2013 0, /* @tp_weaklistoffset@ */
2014 0, /* @tp_iter@ */
2015 0, /* @tp_iternext@ */
2016 ghash_pymethods, /* @tp_methods@ */
2017 0, /* @tp_members@ */
2018 0, /* @tp_getset@ */
2019 0, /* @tp_base@ */
2020 0, /* @tp_dict@ */
2021 0, /* @tp_descr_get@ */
2022 0, /* @tp_descr_set@ */
2023 0, /* @tp_dictoffset@ */
2024 0, /* @tp_init@ */
2025 PyType_GenericAlloc, /* @tp_alloc@ */
2026 abstract_pynew, /* @tp_new@ */
2027 0, /* @tp_free@ */
2028 0 /* @tp_is_gc@ */
2029 };
2030
2031 /*----- Message authentication --------------------------------------------*/
2032
2033 PyTypeObject *gcmac_pytype, *gmac_pytype, *gmhash_pytype;
2034
2035 CONVFUNC(gcmac, gcmac *, GCMAC_CM)
2036 CONVFUNC(gmac, gmac *, GMAC_M)
2037 CONVFUNC(gmhash, ghash *, GHASH_H)
2038
2039 static PyObject *gmac_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
2040 {
2041 static const char *const kwlist[] = { "k", 0 };
2042 char *k;
2043 Py_ssize_t sz;
2044
2045 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &k, &sz))
2046 goto end;
2047 if (keysz(sz, GCMAC_CM(ty)->keysz) != sz) VALERR("bad key length");
2048 return (gmac_pywrap((PyObject *)ty,
2049 GM_KEY(GCMAC_CM(ty), k, sz)));
2050 end:
2051 return (0);
2052 }
2053
2054 static PyObject *gmhash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
2055 {
2056 static const char *const kwlist[] = { 0 };
2057 ghash_pyobj *g;
2058
2059 if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", KWLIST)) return (0);
2060 g = PyObject_NEW(ghash_pyobj, ty);
2061 g->h = GM_INIT(GMAC_M(ty));
2062 Py_INCREF(ty);
2063 return ((PyObject *)g);
2064 }
2065
2066 PyObject *gcmac_pywrap(gcmac *cm)
2067 {
2068 gcmac_pyobj *g = newtype(gcmac_pytype, 0, cm->name);
2069 g->cm = cm;
2070 g->ty.ht_type.tp_basicsize = sizeof(gmac_pyobj);
2071 g->ty.ht_type.tp_base = gmac_pytype;
2072 Py_INCREF(gmac_pytype);
2073 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
2074 Py_TPFLAGS_BASETYPE |
2075 Py_TPFLAGS_HEAPTYPE);
2076 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
2077 g->ty.ht_type.tp_free = 0;
2078 g->ty.ht_type.tp_new = gmac_pynew;
2079 typeready(&g->ty.ht_type);
2080 return ((PyObject *)g);
2081 }
2082
2083 PyObject *gmac_pywrap(PyObject *cobj, gmac *m)
2084 {
2085 gmac_pyobj *g;
2086 if (!cobj) cobj = gcmac_pywrap((/*unconst*/ gcmac *)GM_CLASS(m));
2087 else Py_INCREF(cobj);
2088 g = newtype((PyTypeObject *)cobj, 0, 0);
2089 g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj);
2090 g->ty.ht_name = PyString_FromFormat("%s(keyed)", m->ops->c->name);
2091 g->ty.ht_type.tp_name = PyString_AS_STRING(g->ty.ht_name);
2092 g->ty.ht_type.tp_base = gmhash_pytype;
2093 Py_INCREF(gmac_pytype);
2094 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
2095 Py_TPFLAGS_BASETYPE |
2096 Py_TPFLAGS_HEAPTYPE);
2097 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
2098 g->ty.ht_type.tp_free = 0;
2099 g->ty.ht_type.tp_new = gmhash_pynew;
2100 typeready(&g->ty.ht_type);
2101 g->m = m;
2102 return ((PyObject *)g);
2103 }
2104
2105 static void gmac_pydealloc(PyObject *me)
2106 {
2107 GM_DESTROY(GMAC_M(me));
2108 Py_DECREF(me->ob_type);
2109 PyType_Type.tp_dealloc(me);
2110 }
2111
2112 static PyObject *gcmget_name(PyObject *me, void *hunoz)
2113 { return (PyString_FromString(GCMAC_CM(me)->name)); }
2114
2115 static PyObject *gcmget_keysz(PyObject *me, void *hunoz)
2116 { return (keysz_pywrap(GCMAC_CM(me)->keysz)); }
2117
2118 static PyObject *gcmget_tagsz(PyObject *me, void *hunoz)
2119 { return (PyInt_FromLong(GCMAC_CM(me)->hashsz)); }
2120
2121 static PyGetSetDef gcmac_pygetset[] = {
2122 #define GETSETNAME(op, name) gcm##op##_##name
2123 GET (keysz, "CM.keysz -> acceptable key sizes")
2124 GET (tagsz, "CM.tagsz -> MAC output size")
2125 GET (name, "CM.name -> name of this kind of MAC")
2126 #undef GETSETNAME
2127 { 0 }
2128 };
2129
2130 static PyTypeObject gcmac_pytype_skel = {
2131 PyObject_HEAD_INIT(0) 0, /* Header */
2132 "GCMAC", /* @tp_name@ */
2133 sizeof(gchash_pyobj), /* @tp_basicsize@ */
2134 0, /* @tp_itemsize@ */
2135
2136 0, /* @tp_dealloc@ */
2137 0, /* @tp_print@ */
2138 0, /* @tp_getattr@ */
2139 0, /* @tp_setattr@ */
2140 0, /* @tp_compare@ */
2141 0, /* @tp_repr@ */
2142 0, /* @tp_as_number@ */
2143 0, /* @tp_as_sequence@ */
2144 0, /* @tp_as_mapping@ */
2145 0, /* @tp_hash@ */
2146 0, /* @tp_call@ */
2147 0, /* @tp_str@ */
2148 0, /* @tp_getattro@ */
2149 0, /* @tp_setattro@ */
2150 0, /* @tp_as_buffer@ */
2151 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2152 Py_TPFLAGS_BASETYPE,
2153
2154 /* @tp_doc@ */
2155 "Message authentication code metametaclass.",
2156
2157 0, /* @tp_traverse@ */
2158 0, /* @tp_clear@ */
2159 0, /* @tp_richcompare@ */
2160 0, /* @tp_weaklistoffset@ */
2161 0, /* @tp_iter@ */
2162 0, /* @tp_iternext@ */
2163 0, /* @tp_methods@ */
2164 0, /* @tp_members@ */
2165 gcmac_pygetset, /* @tp_getset@ */
2166 0, /* @tp_base@ */
2167 0, /* @tp_dict@ */
2168 0, /* @tp_descr_get@ */
2169 0, /* @tp_descr_set@ */
2170 0, /* @tp_dictoffset@ */
2171 0, /* @tp_init@ */
2172 PyType_GenericAlloc, /* @tp_alloc@ */
2173 abstract_pynew, /* @tp_new@ */
2174 0, /* @tp_free@ */
2175 0 /* @tp_is_gc@ */
2176 };
2177
2178 static PyTypeObject gmac_pytype_skel = {
2179 PyObject_HEAD_INIT(0) 0, /* Header */
2180 "GMAC", /* @tp_name@ */
2181 sizeof(gmac_pyobj), /* @tp_basicsize@ */
2182 0, /* @tp_itemsize@ */
2183
2184 gmac_pydealloc, /* @tp_dealloc@ */
2185 0, /* @tp_print@ */
2186 0, /* @tp_getattr@ */
2187 0, /* @tp_setattr@ */
2188 0, /* @tp_compare@ */
2189 0, /* @tp_repr@ */
2190 0, /* @tp_as_number@ */
2191 0, /* @tp_as_sequence@ */
2192 0, /* @tp_as_mapping@ */
2193 0, /* @tp_hash@ */
2194 0, /* @tp_call@ */
2195 0, /* @tp_str@ */
2196 0, /* @tp_getattro@ */
2197 0, /* @tp_setattro@ */
2198 0, /* @tp_as_buffer@ */
2199 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2200 Py_TPFLAGS_BASETYPE,
2201
2202 /* @tp_doc@ */
2203 "Message authentication code metaclass, abstract base class.",
2204
2205 0, /* @tp_traverse@ */
2206 0, /* @tp_clear@ */
2207 0, /* @tp_richcompare@ */
2208 0, /* @tp_weaklistoffset@ */
2209 0, /* @tp_iter@ */
2210 0, /* @tp_iternext@ */
2211 0, /* @tp_methods@ */
2212 0, /* @tp_members@ */
2213 0, /* @tp_getset@ */
2214 0, /* @tp_base@ */
2215 0, /* @tp_dict@ */
2216 0, /* @tp_descr_get@ */
2217 0, /* @tp_descr_set@ */
2218 0, /* @tp_dictoffset@ */
2219 0, /* @tp_init@ */
2220 PyType_GenericAlloc, /* @tp_alloc@ */
2221 abstract_pynew, /* @tp_new@ */
2222 0, /* @tp_free@ */
2223 0 /* @tp_is_gc@ */
2224 };
2225
2226 static PyTypeObject gmhash_pytype_skel = {
2227 PyObject_HEAD_INIT(0) 0, /* Header */
2228 "GMACHash", /* @tp_name@ */
2229 sizeof(ghash_pyobj), /* @tp_basicsize@ */
2230 0, /* @tp_itemsize@ */
2231
2232 ghash_pydealloc, /* @tp_dealloc@ */
2233 0, /* @tp_print@ */
2234 0, /* @tp_getattr@ */
2235 0, /* @tp_setattr@ */
2236 0, /* @tp_compare@ */
2237 0, /* @tp_repr@ */
2238 0, /* @tp_as_number@ */
2239 0, /* @tp_as_sequence@ */
2240 0, /* @tp_as_mapping@ */
2241 0, /* @tp_hash@ */
2242 0, /* @tp_call@ */
2243 0, /* @tp_str@ */
2244 0, /* @tp_getattro@ */
2245 0, /* @tp_setattro@ */
2246 0, /* @tp_as_buffer@ */
2247 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2248 Py_TPFLAGS_BASETYPE,
2249
2250 /* @tp_doc@ */
2251 "Message authentication code, abstract base class.",
2252
2253 0, /* @tp_traverse@ */
2254 0, /* @tp_clear@ */
2255 0, /* @tp_richcompare@ */
2256 0, /* @tp_weaklistoffset@ */
2257 0, /* @tp_iter@ */
2258 0, /* @tp_iternext@ */
2259 0, /* @tp_methods@ */
2260 0, /* @tp_members@ */
2261 0, /* @tp_getset@ */
2262 0, /* @tp_base@ */
2263 0, /* @tp_dict@ */
2264 0, /* @tp_descr_get@ */
2265 0, /* @tp_descr_set@ */
2266 0, /* @tp_dictoffset@ */
2267 0, /* @tp_init@ */
2268 PyType_GenericAlloc, /* @tp_alloc@ */
2269 abstract_pynew, /* @tp_new@ */
2270 0, /* @tp_free@ */
2271 0 /* @tp_is_gc@ */
2272 };
2273
2274 /*----- Special snowflake for Poly1305 ------------------------------------*/
2275
2276 PyTypeObject *poly1305cls_pytype, *poly1305key_pytype, *poly1305hash_pytype;
2277
2278 typedef struct poly1305key_pyobj {
2279 PyHeapTypeObject ty;
2280 poly1305_key k;
2281 } poly1305key_pyobj;
2282
2283 typedef struct poly1305hash_pyobj {
2284 PyObject_HEAD
2285 unsigned f;
2286 #define f_mask 1u
2287 poly1305_ctx ctx;
2288 } poly1305hash_pyobj;
2289
2290 #define P1305_F(o) (((poly1305hash_pyobj *)(o))->f)
2291 #define P1305_CTX(o) (&((poly1305hash_pyobj *)(o))->ctx)
2292 CONVFUNC(poly1305hash, poly1305_ctx *, P1305_CTX)
2293
2294 static PyObject *poly1305hash_pynew(PyTypeObject *ty,
2295 PyObject *arg, PyObject *kw)
2296 {
2297 static const char *const kwlist[] = { "mask", 0 };
2298 poly1305key_pyobj *pk = (poly1305key_pyobj *)ty;
2299 poly1305hash_pyobj *ph;
2300 char *m = 0;
2301 Py_ssize_t sz;
2302
2303 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|s#:new", KWLIST, &m, &sz))
2304 return (0);
2305 if (m && sz != POLY1305_MASKSZ) VALERR("bad mask length");
2306 ph = PyObject_NEW(poly1305hash_pyobj, ty);
2307 ph->f = 0;
2308 if (m) ph->f |= f_mask;
2309 poly1305_macinit(&ph->ctx, &pk->k, m);
2310 Py_INCREF(ty);
2311 return ((PyObject *)ph);
2312 end:
2313 return (0);
2314 }
2315
2316 static PyObject *poly1305key_pynew(PyTypeObject *ty,
2317 PyObject *arg, PyObject *kw)
2318 {
2319 static const char *const kwlist[] = { "k", 0 };
2320 poly1305key_pyobj *pk;
2321 char *k;
2322 Py_ssize_t sz;
2323
2324 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &k, &sz))
2325 goto end;
2326 if (keysz(sz, poly1305_keysz) != sz) VALERR("bad key length");
2327
2328 pk = newtype(ty, 0, 0);
2329 pk->ty.ht_name = PyString_FromString("poly1305(keyed)");
2330 pk->ty.ht_type.tp_basicsize = sizeof(poly1305hash_pyobj);
2331 pk->ty.ht_type.tp_name = PyString_AS_STRING(pk->ty.ht_name);
2332 pk->ty.ht_type.tp_base = poly1305hash_pytype;
2333 Py_INCREF(poly1305key_pytype);
2334 pk->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
2335 Py_TPFLAGS_BASETYPE |
2336 Py_TPFLAGS_HEAPTYPE);
2337 pk->ty.ht_type.tp_alloc = PyType_GenericAlloc;
2338 pk->ty.ht_type.tp_free = 0;
2339 pk->ty.ht_type.tp_new = poly1305hash_pynew;
2340 typeready(&pk->ty.ht_type);
2341
2342 poly1305_keyinit(&pk->k, k, sz);
2343 return ((PyObject *)pk);
2344
2345 end:
2346 return (0);
2347 }
2348
2349 static PyObject *poly1305clsget_name(PyObject *me, void *hunoz)
2350 { return (PyString_FromString("poly1305")); }
2351
2352 static PyObject *poly1305clsget_keysz(PyObject *me, void *hunoz)
2353 { return (keysz_pywrap(poly1305_keysz)); }
2354
2355 static PyObject *poly1305clsget_masksz(PyObject *me, void *hunoz)
2356 { return (PyInt_FromLong(POLY1305_MASKSZ)); }
2357
2358 static PyObject *poly1305clsget_tagsz(PyObject *me, void *hunoz)
2359 { return (PyInt_FromLong(POLY1305_TAGSZ)); }
2360
2361 static PyObject *polymeth_copy(PyObject *me, PyObject *arg)
2362 {
2363 poly1305hash_pyobj *ph;
2364 if (!PyArg_ParseTuple(arg, ":copy")) return (0);
2365 ph = PyObject_NEW(poly1305hash_pyobj, me->ob_type);
2366 poly1305_copy(&ph->ctx, P1305_CTX(me));
2367 Py_INCREF(me->ob_type);
2368 return ((PyObject *)ph);
2369 }
2370
2371 static PyObject *polymeth_hash(PyObject *me, PyObject *arg)
2372 {
2373 char *p;
2374 Py_ssize_t sz;
2375 if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0);
2376 poly1305_hash(P1305_CTX(me), p, sz);
2377 RETURN_ME;
2378 }
2379
2380 #define POLYMETH_HASHU_(n, W, w) \
2381 static PyObject *polymeth_hashu##w(PyObject *me, PyObject *arg) \
2382 { \
2383 uint##n x; \
2384 octet b[SZ_##W]; \
2385 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \
2386 STORE##W(b, x); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \
2387 RETURN_ME; \
2388 }
2389 DOUINTCONV(POLYMETH_HASHU_)
2390
2391 #define POLYMETH_HASHBUF_(n, W, w) \
2392 static PyObject *polymeth_hashbuf##w(PyObject *me, PyObject *arg) \
2393 { \
2394 char *p; \
2395 Py_ssize_t sz; \
2396 octet b[SZ_##W]; \
2397 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
2398 if (sz > MASK##n) TYERR("string too long"); \
2399 STORE##W(b, sz); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \
2400 poly1305_hash(P1305_CTX(me), p, sz); \
2401 RETURN_ME; \
2402 end: \
2403 return (0); \
2404 }
2405 DOUINTCONV(POLYMETH_HASHBUF_)
2406
2407 static PyObject *polymeth_hashstrz(PyObject *me, PyObject *arg)
2408 {
2409 char *p;
2410 if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0);
2411 poly1305_hash(P1305_CTX(me), p, strlen(p) + 1);
2412 RETURN_ME;
2413 }
2414
2415 static PyObject *polymeth_flush(PyObject *me, PyObject *arg)
2416 {
2417 if (!PyArg_ParseTuple(arg, ":flush")) return (0);
2418 poly1305_flush(P1305_CTX(me));
2419 RETURN_ME;
2420 }
2421
2422 static PyObject *polymeth_flushzero(PyObject *me, PyObject *arg)
2423 {
2424 if (!PyArg_ParseTuple(arg, ":flushzero")) return (0);
2425 poly1305_flushzero(P1305_CTX(me));
2426 RETURN_ME;
2427 }
2428
2429 static PyObject *polymeth_concat(PyObject *me, PyObject *arg)
2430 {
2431 PyObject *pre, *suff;
2432 if (!PyArg_ParseTuple(arg, "OO:concat", &pre, &suff)) return (0);
2433 if (!PyObject_TypeCheck(pre, poly1305hash_pytype) ||
2434 !PyObject_TypeCheck(suff, poly1305hash_pytype))
2435 TYERR("wanted a poly1305hash");
2436 if (me->ob_type != pre->ob_type || me->ob_type != suff->ob_type)
2437 TYERR("key mismatch");
2438 if (P1305_CTX(pre)->nbuf) VALERR("prefix is not block-aligned");
2439 poly1305_concat(P1305_CTX(me), P1305_CTX(pre), P1305_CTX(suff));
2440 RETURN_ME;
2441 end:
2442 return (0);
2443 }
2444
2445 static PyObject *polymeth_done(PyObject *me, PyObject *arg)
2446 {
2447 PyObject *rc;
2448 if (!PyArg_ParseTuple(arg, ":done")) return (0);
2449 if (!(P1305_F(me) & f_mask)) VALERR("no mask");
2450 rc = bytestring_pywrap(0, POLY1305_TAGSZ);
2451 poly1305_done(P1305_CTX(me), PyString_AS_STRING(rc));
2452 return (rc);
2453 end:
2454 return (0);
2455 }
2456
2457 static PyGetSetDef poly1305cls_pygetset[] = {
2458 #define GETSETNAME(op, name) poly1305cls##op##_##name
2459 GET (keysz, "PC.keysz -> acceptable key sizes")
2460 GET (masksz, "PC.masksz -> mask size")
2461 GET (tagsz, "PC.tagsz -> MAC output size")
2462 GET (name, "PC.name -> name of this kind of MAC")
2463 #undef GETSETNAME
2464 { 0 }
2465 };
2466
2467 static PyMethodDef poly1305hash_pymethods[] = {
2468 #define METHNAME(name) polymeth_##name
2469 METH (copy, "P.copy() -> PP")
2470 METH (hash, "P.hash(M)")
2471 #define METHU_(n, W, w) METH(hashu##w, "P.hashu" #w "(WORD)")
2472 DOUINTCONV(METHU_)
2473 #undef METHU_
2474 #define METHBUF_(n, W, w) METH(hashbuf##w, "P.hashbuf" #w "(BYTES)")
2475 DOUINTCONV(METHBUF_)
2476 #undef METHBUF_
2477 METH (hashstrz, "P.hashstrz(STRING)")
2478 METH (flush, "P.flush()")
2479 METH (flushzero, "P.flushzero()")
2480 METH (concat, "P.concat(PREFIX, SUFFIX)")
2481 METH (done, "P.done() -> TAG")
2482 #undef METHNAME
2483 { 0 }
2484 };
2485
2486 static PyTypeObject poly1305cls_pytype_skel = {
2487 PyObject_HEAD_INIT(0) 0, /* Header */
2488 "Poly1305Class", /* @tp_name@ */
2489 sizeof(PyHeapTypeObject), /* @tp_basicsize@ */
2490 0, /* @tp_itemsize@ */
2491
2492 0, /* @tp_dealloc@ */
2493 0, /* @tp_print@ */
2494 0, /* @tp_getattr@ */
2495 0, /* @tp_setattr@ */
2496 0, /* @tp_compare@ */
2497 0, /* @tp_repr@ */
2498 0, /* @tp_as_number@ */
2499 0, /* @tp_as_sequence@ */
2500 0, /* @tp_as_mapping@ */
2501 0, /* @tp_hash@ */
2502 0, /* @tp_call@ */
2503 0, /* @tp_str@ */
2504 0, /* @tp_getattro@ */
2505 0, /* @tp_setattro@ */
2506 0, /* @tp_as_buffer@ */
2507 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2508 Py_TPFLAGS_BASETYPE,
2509
2510 /* @tp_doc@ */
2511 "Poly1305 metametaclass. Best not to ask.",
2512
2513 0, /* @tp_traverse@ */
2514 0, /* @tp_clear@ */
2515 0, /* @tp_richcompare@ */
2516 0, /* @tp_weaklistoffset@ */
2517 0, /* @tp_iter@ */
2518 0, /* @tp_iternext@ */
2519 0, /* @tp_methods@ */
2520 0, /* @tp_members@ */
2521 poly1305cls_pygetset, /* @tp_getset@ */
2522 0, /* @tp_base@ */
2523 0, /* @tp_dict@ */
2524 0, /* @tp_descr_get@ */
2525 0, /* @tp_descr_set@ */
2526 0, /* @tp_dictoffset@ */
2527 0, /* @tp_init@ */
2528 PyType_GenericAlloc, /* @tp_alloc@ */
2529 abstract_pynew, /* @tp_new@ */
2530 0, /* @tp_free@ */
2531 0 /* @tp_is_gc@ */
2532 };
2533
2534 static PyTypeObject poly1305key_pytype_skel = {
2535 PyObject_HEAD_INIT(0) 0, /* Header */
2536 "poly1305", /* @tp_name@ */
2537 sizeof(poly1305key_pyobj), /* @tp_basicsize@ */
2538 0, /* @tp_itemsize@ */
2539
2540 0, /* @tp_dealloc@ */
2541 0, /* @tp_print@ */
2542 0, /* @tp_getattr@ */
2543 0, /* @tp_setattr@ */
2544 0, /* @tp_compare@ */
2545 0, /* @tp_repr@ */
2546 0, /* @tp_as_number@ */
2547 0, /* @tp_as_sequence@ */
2548 0, /* @tp_as_mapping@ */
2549 0, /* @tp_hash@ */
2550 0, /* @tp_call@ */
2551 0, /* @tp_str@ */
2552 0, /* @tp_getattro@ */
2553 0, /* @tp_setattro@ */
2554 0, /* @tp_as_buffer@ */
2555 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2556 Py_TPFLAGS_BASETYPE,
2557
2558 /* @tp_doc@ */
2559 "poly1305(K): Poly1305 key.",
2560
2561 0, /* @tp_traverse@ */
2562 0, /* @tp_clear@ */
2563 0, /* @tp_richcompare@ */
2564 0, /* @tp_weaklistoffset@ */
2565 0, /* @tp_iter@ */
2566 0, /* @tp_iternext@ */
2567 0, /* @tp_methods@ */
2568 0, /* @tp_members@ */
2569 0, /* @tp_getset@ */
2570 0, /* @tp_base@ */
2571 0, /* @tp_dict@ */
2572 0, /* @tp_descr_get@ */
2573 0, /* @tp_descr_set@ */
2574 0, /* @tp_dictoffset@ */
2575 0, /* @tp_init@ */
2576 PyType_GenericAlloc, /* @tp_alloc@ */
2577 poly1305key_pynew, /* @tp_new@ */
2578 0, /* @tp_free@ */
2579 0 /* @tp_is_gc@ */
2580 };
2581
2582 static PyTypeObject poly1305hash_pytype_skel = {
2583 PyObject_HEAD_INIT(0) 0, /* Header */
2584 "Poly1305Hash", /* @tp_name@ */
2585 sizeof(poly1305hash_pyobj), /* @tp_basicsize@ */
2586 0, /* @tp_itemsize@ */
2587
2588 0, /* @tp_dealloc@ */
2589 0, /* @tp_print@ */
2590 0, /* @tp_getattr@ */
2591 0, /* @tp_setattr@ */
2592 0, /* @tp_compare@ */
2593 0, /* @tp_repr@ */
2594 0, /* @tp_as_number@ */
2595 0, /* @tp_as_sequence@ */
2596 0, /* @tp_as_mapping@ */
2597 0, /* @tp_hash@ */
2598 0, /* @tp_call@ */
2599 0, /* @tp_str@ */
2600 0, /* @tp_getattro@ */
2601 0, /* @tp_setattro@ */
2602 0, /* @tp_as_buffer@ */
2603 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2604 Py_TPFLAGS_BASETYPE,
2605
2606 /* @tp_doc@ */
2607 "Poly1305 MAC context base class.",
2608
2609 0, /* @tp_traverse@ */
2610 0, /* @tp_clear@ */
2611 0, /* @tp_richcompare@ */
2612 0, /* @tp_weaklistoffset@ */
2613 0, /* @tp_iter@ */
2614 0, /* @tp_iternext@ */
2615 poly1305hash_pymethods, /* @tp_methods@ */
2616 0, /* @tp_members@ */
2617 0, /* @tp_getset@ */
2618 0, /* @tp_base@ */
2619 0, /* @tp_dict@ */
2620 0, /* @tp_descr_get@ */
2621 0, /* @tp_descr_set@ */
2622 0, /* @tp_dictoffset@ */
2623 0, /* @tp_init@ */
2624 PyType_GenericAlloc, /* @tp_alloc@ */
2625 abstract_pynew, /* @tp_new@ */
2626 0, /* @tp_free@ */
2627 0 /* @tp_is_gc@ */
2628 };
2629
2630 /*----- Special snowflake for HSalsa and HChaCha --------------------------*/
2631
2632 #define DEF_HDANCE(DANCE, HDANCE, dance, hdance) \
2633 static PyObject *meth_##hdance##_prf(PyObject *me, PyObject *arg) \
2634 { \
2635 dance##_ctx dance; \
2636 char *k, *n; \
2637 Py_ssize_t ksz, nsz; \
2638 PyObject *rc; \
2639 if (!PyArg_ParseTuple(arg, "s#s#:" #hdance "_prf", \
2640 &k, &ksz, &n, &nsz)) \
2641 goto end; \
2642 if (ksz != keysz(ksz, dance##_keysz)) VALERR("bad key length"); \
2643 if (nsz != HDANCE##_INSZ) VALERR("bad input length"); \
2644 rc = bytestring_pywrap(0, HSALSA20_OUTSZ); \
2645 dance##_init(&dance, k, ksz, 0); \
2646 hdance##_prf(&dance, n, PyString_AS_STRING(rc)); \
2647 return (rc); \
2648 end: \
2649 return (0); \
2650 }
2651
2652 DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa20)
2653 DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa2012)
2654 DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa208)
2655
2656 DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha20)
2657 DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha12)
2658 DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha8)
2659
2660 /*----- Keccak-p[1600, n] -------------------------------------------------*/
2661
2662 static PyTypeObject *kxvik_pytype;
2663
2664 typedef struct kxvik_pyobj {
2665 PyObject_HEAD
2666 keccak1600_state s;
2667 unsigned n;
2668 } kxvik_pyobj;
2669
2670 static PyObject *kxvik_pynew(PyTypeObject *ty,
2671 PyObject *arg, PyObject *kw)
2672 {
2673 unsigned n = 24;
2674 kxvik_pyobj *rc = 0;
2675 static const char *const kwlist[] = { "nround", 0 };
2676 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", KWLIST,
2677 convuint, &n))
2678 goto end;
2679 rc = (kxvik_pyobj *)ty->tp_alloc(ty, 0);
2680 rc->n = n;
2681 keccak1600_init(&rc->s);
2682 end:
2683 return ((PyObject *)rc);
2684 }
2685
2686 static PyObject *kxvikmeth_copy(PyObject *me, PyObject *arg)
2687 {
2688 kxvik_pyobj *k = (kxvik_pyobj *)me, *rc = 0;
2689 if (!PyArg_ParseTuple(arg, ":copy")) goto end;
2690 rc = (kxvik_pyobj *)k->ob_type->tp_alloc(k->ob_type, 0);
2691 rc->s = k->s; rc->n = k->n;
2692 end:
2693 return ((PyObject *)rc);
2694 }
2695
2696 static PyObject *kxvikmeth_mix(PyObject *me, PyObject *arg)
2697 {
2698 kxvik_pyobj *k = (kxvik_pyobj *)me;
2699 kludge64 t[25];
2700 const octet *q;
2701 octet buf[8];
2702 unsigned i;
2703 char *p; Py_ssize_t n;
2704
2705 if (!PyArg_ParseTuple(arg, "s#:mix", &p, &n)) goto end;
2706 if (n > 200) VALERR("out of range");
2707 q = (const octet *)p;
2708 i = 0;
2709 while (n > 8) { LOAD64_L_(t[i], q); i++; q += 8; n -= 8; }
2710 if (n) {
2711 memcpy(buf, q, n); memset(buf + n, 0, 8 - n);
2712 LOAD64_L_(t[i], buf); i++;
2713 }
2714 keccak1600_mix(&k->s, t, i);
2715 RETURN_ME;
2716 end:
2717 return (0);
2718 }
2719
2720 static PyObject *kxvikmeth_extract(PyObject *me, PyObject *arg)
2721 {
2722 kxvik_pyobj *k = (kxvik_pyobj *)me;
2723 PyObject *rc = 0;
2724 kludge64 t[25];
2725 octet *q, buf[8];
2726 unsigned i;
2727 unsigned n;
2728
2729 if (!PyArg_ParseTuple(arg, "O&:extract", convuint, &n)) goto end;
2730 if (n > 200) VALERR("out of range");
2731 rc = bytestring_pywrap(0, n);
2732 q = (octet *)PyString_AS_STRING(rc);
2733 keccak1600_extract(&k->s, t, (n + 7)/8);
2734 i = 0;
2735 while (n > 8) { STORE64_L_(q, t[i]); i++; q += 8; n -= 8; }
2736 if (n) { STORE64_L_(buf, t[i]); memcpy(q, buf, n); }
2737 end:
2738 return (rc);
2739 }
2740
2741 static PyObject *kxvikmeth_step(PyObject *me, PyObject *arg)
2742 {
2743 kxvik_pyobj *k = (kxvik_pyobj *)me;
2744 if (!PyArg_ParseTuple(arg, ":step")) return (0);
2745 keccak1600_p(&k->s, &k->s, k->n);
2746 RETURN_ME;
2747 }
2748
2749 static PyObject *kxvikget_nround(PyObject *me, void *hunoz)
2750 {
2751 kxvik_pyobj *k = (kxvik_pyobj *)me;
2752 return (PyInt_FromLong(k->n));
2753 }
2754
2755 static int kxvikset_nround(PyObject *me, PyObject *val, void *hunoz)
2756 {
2757 kxvik_pyobj *k = (kxvik_pyobj *)me;
2758 unsigned n;
2759 int rc = -1;
2760
2761 if (!val) NIERR("__del__");
2762 if (!convuint(val, &n)) goto end;
2763 k->n = n;
2764 rc = 0;
2765 end:
2766 return (rc);
2767 }
2768
2769 static PyGetSetDef kxvik_pygetset[] = {
2770 #define GETSETNAME(op, name) kxvik##op##_##name
2771 GETSET(nround, "KECCAK.nround -> number of rounds")
2772 #undef GETSETNAME
2773 { 0 }
2774 };
2775
2776 static PyMethodDef kxvik_pymethods[] = {
2777 #define METHNAME(func) kxvikmeth_##func
2778 METH (copy, "KECCAK.copy() -> KECCAK'")
2779 METH (mix, "KECCAK.mix(DATA)")
2780 METH (extract, "KECCAK.extract(NOCTETS)")
2781 METH (step, "KECCAK.step()")
2782 #undef METHNAME
2783 { 0 }
2784 };
2785
2786 static PyTypeObject kxvik_pytype_skel = {
2787 PyObject_HEAD_INIT(0) 0, /* Header */
2788 "Keccak1600", /* @tp_name@ */
2789 sizeof(kxvik_pyobj), /* @tp_basicsize@ */
2790 0, /* @tp_itemsize@ */
2791
2792 0, /* @tp_dealloc@ */
2793 0, /* @tp_print@ */
2794 0, /* @tp_getattr@ */
2795 0, /* @tp_setattr@ */
2796 0, /* @tp_compare@ */
2797 0, /* @tp_repr@ */
2798 0, /* @tp_as_number@ */
2799 0, /* @tp_as_sequence@ */
2800 0, /* @tp_as_mapping@ */
2801 0, /* @tp_hash@ */
2802 0, /* @tp_call@ */
2803 0, /* @tp_str@ */
2804 0, /* @tp_getattro@ */
2805 0, /* @tp_setattro@ */
2806 0, /* @tp_as_buffer@ */
2807 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2808 Py_TPFLAGS_BASETYPE,
2809
2810 /* @tp_doc@ */
2811 "Keccak1600([nround = 24]): Keccak-p[1600, n] state.",
2812
2813 0, /* @tp_traverse@ */
2814 0, /* @tp_clear@ */
2815 0, /* @tp_richcompare@ */
2816 0, /* @tp_weaklistoffset@ */
2817 0, /* @tp_iter@ */
2818 0, /* @tp_iternext@ */
2819 kxvik_pymethods, /* @tp_methods@ */
2820 0, /* @tp_members@ */
2821 kxvik_pygetset, /* @tp_getset@ */
2822 0, /* @tp_base@ */
2823 0, /* @tp_dict@ */
2824 0, /* @tp_descr_get@ */
2825 0, /* @tp_descr_set@ */
2826 0, /* @tp_dictoffset@ */
2827 0, /* @tp_init@ */
2828 PyType_GenericAlloc, /* @tp_alloc@ */
2829 kxvik_pynew, /* @tp_new@ */
2830 0, /* @tp_free@ */
2831 0 /* @tp_is_gc@ */
2832 };
2833
2834 static PyTypeObject *shake_pytype, *shake128_pytype, *shake256_pytype;
2835
2836 typedef struct shake_pyobj {
2837 PyObject_HEAD
2838 int st;
2839 shake_ctx h;
2840 } shake_pyobj;
2841
2842 #define SHAKE_H(o) (&((shake_pyobj *)(o))->h)
2843 #define SHAKE_ST(o) (((shake_pyobj *)(o))->st)
2844
2845 static PyObject *shake_dopynew(void (*initfn)(shake_ctx *,
2846 const void *, size_t,
2847 const void *, size_t),
2848 PyTypeObject *ty,
2849 PyObject *arg, PyObject *kw)
2850 {
2851 shake_pyobj *rc = 0;
2852 char *p = 0, *f = 0;
2853 Py_ssize_t psz = 0, fsz = 0;
2854 static const char *const kwlist[] = { "perso", "func", 0 };
2855
2856 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|s#s#:new", KWLIST,
2857 &p, &psz, &f, &fsz))
2858 goto end;
2859 rc = (shake_pyobj *)ty->tp_alloc(ty, 0);
2860 initfn(&rc->h, f, fsz, p, psz);
2861 rc->st = 0;
2862 end:
2863 return ((PyObject *)rc);
2864 }
2865
2866 static PyObject *shake128_pynew(PyTypeObject *ty,
2867 PyObject *arg, PyObject *kw)
2868 { return (shake_dopynew(cshake128_init, ty, arg, kw)); }
2869
2870 static PyObject *shake256_pynew(PyTypeObject *ty,
2871 PyObject *arg, PyObject *kw)
2872 { return (shake_dopynew(cshake256_init, ty, arg, kw)); }
2873
2874 static int shake_check(PyObject *me, int st)
2875 {
2876 if (SHAKE_ST(me) != st) VALERR("wrong state");
2877 return (0);
2878 end:
2879 return (-1);
2880 }
2881
2882 static PyObject *shakemeth_hash(PyObject *me, PyObject *arg)
2883 {
2884 char *p;
2885 Py_ssize_t sz;
2886 if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0);
2887 if (shake_check(me, 0)) return (0);
2888 shake_hash(SHAKE_H(me), p, sz);
2889 RETURN_ME;
2890 }
2891
2892 #define SHAKEMETH_HASHU_(n, W, w) \
2893 static PyObject *shakemeth_hashu##w(PyObject *me, PyObject *arg) \
2894 { \
2895 uint##n x; \
2896 octet b[SZ_##W]; \
2897 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \
2898 if (shake_check(me, 0)) return (0); \
2899 STORE##W(b, x); shake_hash(SHAKE_H(me), b, sizeof(b)); \
2900 RETURN_ME; \
2901 }
2902 DOUINTCONV(SHAKEMETH_HASHU_)
2903
2904 #define SHAKEMETH_HASHBUF_(n, W, w) \
2905 static PyObject *shakemeth_hashbuf##w(PyObject *me, PyObject *arg) \
2906 { \
2907 char *p; \
2908 Py_ssize_t sz; \
2909 octet b[SZ_##W]; \
2910 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
2911 if (sz > MASK##n) TYERR("string too long"); \
2912 if (shake_check(me, 0)) goto end; \
2913 STORE##W(b, sz); shake_hash(SHAKE_H(me), b, sizeof(b)); \
2914 shake_hash(SHAKE_H(me), p, sz); \
2915 RETURN_ME; \
2916 end: \
2917 return (0); \
2918 }
2919 DOUINTCONV(SHAKEMETH_HASHBUF_)
2920
2921 static PyObject *shakemeth_hashstrz(PyObject *me, PyObject *arg)
2922 {
2923 char *p;
2924 if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0);
2925 if (shake_check(me, 0)) return (0);
2926 shake_hash(SHAKE_H(me), p, strlen(p) + 1);
2927 RETURN_ME;
2928 }
2929
2930 static PyObject *shakemeth_xof(PyObject *me, PyObject *arg)
2931 {
2932 if (!PyArg_ParseTuple(arg, ":xof")) goto end;
2933 if (shake_check(me, 0)) goto end;
2934 shake_xof(SHAKE_H(me));
2935 SHAKE_ST(me) = 1;
2936 RETURN_ME;
2937 end:
2938 return (0);
2939 }
2940
2941 static PyObject *shakemeth_done(PyObject *me, PyObject *arg)
2942 {
2943 PyObject *rc = 0;
2944 size_t n;
2945 if (!PyArg_ParseTuple(arg, "O&:done", convszt, &n)) goto end;
2946 if (shake_check(me, 0)) goto end;
2947 rc = bytestring_pywrap(0, n);
2948 shake_done(SHAKE_H(me), PyString_AS_STRING(rc), n);
2949 SHAKE_ST(me) = -1;
2950 end:
2951 return (rc);
2952 }
2953
2954 static PyObject *shakemeth_copy(PyObject *me, PyObject *arg)
2955 {
2956 shake_pyobj *rc = 0;
2957
2958 if (!PyArg_ParseTuple(arg, ":copy")) goto end;
2959 rc = PyObject_NEW(shake_pyobj, me->ob_type);
2960 rc->h = *SHAKE_H(me);
2961 rc->st = SHAKE_ST(me);
2962 end:
2963 return ((PyObject *)rc);
2964 }
2965
2966 static PyObject *shakemeth_get(PyObject *me, PyObject *arg)
2967 {
2968 PyObject *rc = 0;
2969 size_t sz;
2970
2971 if (!PyArg_ParseTuple(arg, "O&:get", convszt, &sz)) goto end;
2972 if (shake_check(me, 1)) goto end;
2973 rc = bytestring_pywrap(0, sz);
2974 shake_get(SHAKE_H(me), PyString_AS_STRING(rc), sz);
2975 end:
2976 return (rc);
2977 }
2978
2979 static PyObject *shakemeth_mask(PyObject *me, PyObject *arg)
2980 {
2981 PyObject *rc = 0;
2982 char *p; Py_ssize_t sz;
2983
2984 if (!PyArg_ParseTuple(arg, "s#:mask", &p, &sz)) goto end;
2985 if (shake_check(me, 1)) goto end;
2986 rc = bytestring_pywrap(0, sz);
2987 shake_mask(SHAKE_H(me), p, PyString_AS_STRING(rc), sz);
2988 end:
2989 return (rc);
2990 }
2991
2992 static PyObject *shakeget_rate(PyObject *me, void *hunoz)
2993 { return (PyInt_FromLong(SHAKE_H(me)->h.r)); }
2994
2995 static PyObject *shakeget_buffered(PyObject *me, void *hunoz)
2996 { return (PyInt_FromLong(SHAKE_H(me)->h.n)); }
2997
2998 static PyObject *shakeget_state(PyObject *me, void *hunoz)
2999 {
3000 int st = SHAKE_ST(me);
3001 return (PyString_FromString(st == 0 ? "absorb" :
3002 st == 1 ? "squeeze" : "dead"));
3003 }
3004
3005 static PyGetSetDef shake_pygetset[] = {
3006 #define GETSETNAME(op, name) shake##op##_##name
3007 GET (rate, "S.rate -> rate, in bytes")
3008 GET (buffered, "S.buffered -> amount currently buffered")
3009 GET (state, "S.state -> `absorb', `squeeze', `dead'")
3010 #undef GETSETNAME
3011 { 0 }
3012 };
3013
3014 static PyMethodDef shake_pymethods[] = {
3015 #define METHNAME(func) shakemeth_##func
3016 METH (copy, "S.copy() -> SS")
3017 METH (hash, "S.hash(M)")
3018 #define METHU_(n, W, w) METH(hashu##w, "S.hashu" #w "(WORD)")
3019 DOUINTCONV(METHU_)
3020 #undef METHU_
3021 #define METHBUF_(n, W, w) METH(hashbuf##w, "S.hashbuf" #w "(BYTES)")
3022 DOUINTCONV(METHBUF_)
3023 #undef METHBUF_
3024 METH (hashstrz, "S.hashstrz(STRING)")
3025 METH (xof, "S.xof()")
3026 METH (done, "S.done(LEN) ->H")
3027 METH (get, "S.get(LEN) -> H")
3028 METH (mask, "S.mask(M) -> C")
3029 #undef METHNAME
3030 { 0 }
3031 };
3032
3033 static PyTypeObject shake_pytype_skel = {
3034 PyObject_HEAD_INIT(0) 0, /* Header */
3035 "Shake", /* @tp_name@ */
3036 sizeof(shake_pyobj), /* @tp_basicsize@ */
3037 0, /* @tp_itemsize@ */
3038
3039 0, /* @tp_dealloc@ */
3040 0, /* @tp_print@ */
3041 0, /* @tp_getattr@ */
3042 0, /* @tp_setattr@ */
3043 0, /* @tp_compare@ */
3044 0, /* @tp_repr@ */
3045 0, /* @tp_as_number@ */
3046 0, /* @tp_as_sequence@ */
3047 0, /* @tp_as_mapping@ */
3048 0, /* @tp_hash@ */
3049 0, /* @tp_call@ */
3050 0, /* @tp_str@ */
3051 0, /* @tp_getattro@ */
3052 0, /* @tp_setattro@ */
3053 0, /* @tp_as_buffer@ */
3054 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
3055 Py_TPFLAGS_BASETYPE,
3056
3057 /* @tp_doc@ */
3058 "SHAKE/cSHAKE base class.",
3059
3060 0, /* @tp_traverse@ */
3061 0, /* @tp_clear@ */
3062 0, /* @tp_richcompare@ */
3063 0, /* @tp_weaklistoffset@ */
3064 0, /* @tp_iter@ */
3065 0, /* @tp_iternext@ */
3066 shake_pymethods, /* @tp_methods@ */
3067 0, /* @tp_members@ */
3068 shake_pygetset, /* @tp_getset@ */
3069 0, /* @tp_base@ */
3070 0, /* @tp_dict@ */
3071 0, /* @tp_descr_get@ */
3072 0, /* @tp_descr_set@ */
3073 0, /* @tp_dictoffset@ */
3074 0, /* @tp_init@ */
3075 PyType_GenericAlloc, /* @tp_alloc@ */
3076 abstract_pynew, /* @tp_new@ */
3077 0, /* @tp_free@ */
3078 0 /* @tp_is_gc@ */
3079 };
3080
3081 static PyTypeObject shake128_pytype_skel = {
3082 PyObject_HEAD_INIT(0) 0, /* Header */
3083 "Shake128", /* @tp_name@ */
3084 0, /* @tp_basicsize@ */
3085 0, /* @tp_itemsize@ */
3086
3087 0, /* @tp_dealloc@ */
3088 0, /* @tp_print@ */
3089 0, /* @tp_getattr@ */
3090 0, /* @tp_setattr@ */
3091 0, /* @tp_compare@ */
3092 0, /* @tp_repr@ */
3093 0, /* @tp_as_number@ */
3094 0, /* @tp_as_sequence@ */
3095 0, /* @tp_as_mapping@ */
3096 0, /* @tp_hash@ */
3097 0, /* @tp_call@ */
3098 0, /* @tp_str@ */
3099 0, /* @tp_getattro@ */
3100 0, /* @tp_setattro@ */
3101 0, /* @tp_as_buffer@ */
3102 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
3103 Py_TPFLAGS_BASETYPE,
3104
3105 /* @tp_doc@ */
3106 "Shake128([perso = STR], [func = STR]): SHAKE128/cSHAKE128 XOF.",
3107
3108 0, /* @tp_traverse@ */
3109 0, /* @tp_clear@ */
3110 0, /* @tp_richcompare@ */
3111 0, /* @tp_weaklistoffset@ */
3112 0, /* @tp_iter@ */
3113 0, /* @tp_iternext@ */
3114 0, /* @tp_methods@ */
3115 0, /* @tp_members@ */
3116 0, /* @tp_getset@ */
3117 0, /* @tp_base@ */
3118 0, /* @tp_dict@ */
3119 0, /* @tp_descr_get@ */
3120 0, /* @tp_descr_set@ */
3121 0, /* @tp_dictoffset@ */
3122 0, /* @tp_init@ */
3123 PyType_GenericAlloc, /* @tp_alloc@ */
3124 shake128_pynew, /* @tp_new@ */
3125 0, /* @tp_free@ */
3126 0 /* @tp_is_gc@ */
3127 };
3128
3129 static PyTypeObject shake256_pytype_skel = {
3130 PyObject_HEAD_INIT(0) 0, /* Header */
3131 "Shake256", /* @tp_name@ */
3132 0, /* @tp_basicsize@ */
3133 0, /* @tp_itemsize@ */
3134
3135 0, /* @tp_dealloc@ */
3136 0, /* @tp_print@ */
3137 0, /* @tp_getattr@ */
3138 0, /* @tp_setattr@ */
3139 0, /* @tp_compare@ */
3140 0, /* @tp_repr@ */
3141 0, /* @tp_as_number@ */
3142 0, /* @tp_as_sequence@ */
3143 0, /* @tp_as_mapping@ */
3144 0, /* @tp_hash@ */
3145 0, /* @tp_call@ */
3146 0, /* @tp_str@ */
3147 0, /* @tp_getattro@ */
3148 0, /* @tp_setattro@ */
3149 0, /* @tp_as_buffer@ */
3150 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
3151 Py_TPFLAGS_BASETYPE,
3152
3153 /* @tp_doc@ */
3154 "Shake256([perso = STR], [func = STR]): SHAKE256/cSHAKE256 XOF.",
3155
3156 0, /* @tp_traverse@ */
3157 0, /* @tp_clear@ */
3158 0, /* @tp_richcompare@ */
3159 0, /* @tp_weaklistoffset@ */
3160 0, /* @tp_iter@ */
3161 0, /* @tp_iternext@ */
3162 0, /* @tp_methods@ */
3163 0, /* @tp_members@ */
3164 0, /* @tp_getset@ */
3165 0, /* @tp_base@ */
3166 0, /* @tp_dict@ */
3167 0, /* @tp_descr_get@ */
3168 0, /* @tp_descr_set@ */
3169 0, /* @tp_dictoffset@ */
3170 0, /* @tp_init@ */
3171 PyType_GenericAlloc, /* @tp_alloc@ */
3172 shake256_pynew, /* @tp_new@ */
3173 0, /* @tp_free@ */
3174 0 /* @tp_is_gc@ */
3175 };
3176
3177 /*----- Pseudorandom permutations -----------------------------------------*/
3178
3179 static PyTypeObject *gcprp_pytype, *gprp_pytype;
3180
3181 typedef struct prpinfo {
3182 const char *name;
3183 const octet *keysz;
3184 size_t ctxsz;
3185 size_t blksz;
3186 void (*init)(void *, const void *, size_t);
3187 void (*eblk)(void *, const void *, void *);
3188 void (*dblk)(void *, const void *, void *);
3189 } prpinfo;
3190
3191 #define PRP_DEF(PRE, pre) \
3192 static void pre##_prpinit(void *ctx, const void *k, size_t ksz) \
3193 { pre##_init(ctx, k, ksz); } \
3194 static void pre##_prpeblk(void *ctx, const void *in, void *out) \
3195 { \
3196 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
3197 pre##_eblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
3198 } \
3199 static void pre##_prpdblk(void *ctx, const void *in, void *out) \
3200 { \
3201 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
3202 pre##_dblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
3203 } \
3204 static const prpinfo pre##_prpinfo = { \
3205 #pre, pre##_keysz, sizeof(pre##_ctx), PRE##_BLKSZ, \
3206 pre##_prpinit, pre##_prpeblk, pre##_prpdblk \
3207 };
3208 PRPS(PRP_DEF)
3209
3210 static const struct prpinfo *const gprptab[] = {
3211 #define PRP_ENTRY(PRE, pre) &pre##_prpinfo,
3212 PRPS(PRP_ENTRY)
3213 0
3214 };
3215
3216 typedef struct gcprp_pyobj {
3217 PyHeapTypeObject ty;
3218 const prpinfo *prp;
3219 } gcprp_pyobj;
3220 #define GCPRP_PRP(o) (((gcprp_pyobj *)(o))->prp)
3221
3222 typedef struct gprp_pyobj {
3223 PyObject_HEAD
3224 const prpinfo *prp;
3225 } gprp_pyobj;
3226 #define GPRP_PRP(o) (((gprp_pyobj *)(o))->prp)
3227 #define GPRP_CTX(o) (((gprp_pyobj *)(o)) + 1)
3228
3229 typedef struct prp {
3230 const prpinfo *prp;
3231 void *ctx;
3232 } prp;
3233
3234 static PyObject *gprp_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
3235 {
3236 static const char *const kwlist[] = { "key", 0 };
3237 char *k;
3238 Py_ssize_t sz;
3239 const prpinfo *prp = GCPRP_PRP(ty);
3240 PyObject *me;
3241
3242 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &k, &sz))
3243 goto end;
3244 if (keysz(sz, prp->keysz) != sz) VALERR("bad key length");
3245 me = (PyObject *)ty->tp_alloc(ty, 0);
3246 GPRP_PRP(me) = prp;
3247 prp->init(GPRP_CTX(me), k, sz);
3248 Py_INCREF(me);
3249 return (me);
3250 end:
3251 return (0);
3252 }
3253
3254 static void gprp_pydealloc(PyObject *me)
3255 { Py_DECREF(me->ob_type); FREEOBJ(me); }
3256
3257 static PyObject *gcprp_pywrap(const prpinfo *prp)
3258 {
3259 gcprp_pyobj *g = newtype(gcprp_pytype, 0, prp->name);
3260 g->prp = prp;
3261 g->ty.ht_type.tp_basicsize = sizeof(gprp_pyobj) + prp->ctxsz;
3262 g->ty.ht_type.tp_base = gprp_pytype;
3263 Py_INCREF(gprp_pytype);
3264 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
3265 Py_TPFLAGS_BASETYPE |
3266 Py_TPFLAGS_HEAPTYPE);
3267 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
3268 g->ty.ht_type.tp_free = 0;
3269 g->ty.ht_type.tp_new = gprp_pynew;
3270 typeready(&g->ty.ht_type);
3271 return ((PyObject *)g);
3272 }
3273
3274 static PyObject *gcpget_name(PyObject *me, void *hunoz)
3275 { return (PyString_FromString(GCPRP_PRP(me)->name)); }
3276 static PyObject *gcpget_keysz(PyObject *me, void *hunoz)
3277 { return (keysz_pywrap(GCPRP_PRP(me)->keysz)); }
3278 static PyObject *gcpget_blksz(PyObject *me, void *hunoz)
3279 { return (PyInt_FromLong(GCPRP_PRP(me)->blksz)); }
3280
3281 static PyObject *gpmeth_encrypt(PyObject *me, PyObject *arg)
3282 {
3283 char *p;
3284 Py_ssize_t n;
3285 PyObject *rc = 0;
3286
3287 if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &n)) goto end;
3288 if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length");
3289 rc = bytestring_pywrap(0, n);
3290 GPRP_PRP(me)->eblk(GPRP_CTX(me), p, PyString_AS_STRING(rc));
3291 end:
3292 return (rc);
3293 }
3294
3295 static PyObject *gpmeth_decrypt(PyObject *me, PyObject *arg)
3296 {
3297 char *p;
3298 Py_ssize_t n;
3299 PyObject *rc = 0;
3300
3301 if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &n)) goto end;
3302 if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length");
3303 rc = bytestring_pywrap(0, n);
3304 GPRP_PRP(me)->dblk(GPRP_CTX(me), p, PyString_AS_STRING(rc));
3305 end:
3306 return (rc);
3307 }
3308
3309 static PyGetSetDef gcprp_pygetset[] = {
3310 #define GETSETNAME(op, name) gcp##op##_##name
3311 GET (keysz, "CP.keysz -> acceptable key sizes")
3312 GET (blksz, "CP.blksz -> block size")
3313 GET (name, "CP.name -> name of this kind of PRP")
3314 #undef GETSETNAME
3315 { 0 }
3316 };
3317
3318 static PyMethodDef gprp_pymethods[] = {
3319 #define METHNAME(name) gpmeth_##name
3320 METH (encrypt, "P.encrypt(PT) -> CT")
3321 METH (decrypt, "P.decrypt(CT) -> PT")
3322 #undef METHNAME
3323 { 0 }
3324 };
3325
3326 static PyTypeObject gcprp_pytype_skel = {
3327 PyObject_HEAD_INIT(0) 0, /* Header */
3328 "GCPRP", /* @tp_name@ */
3329 sizeof(gcprp_pyobj), /* @tp_basicsize@ */
3330 0, /* @tp_itemsize@ */
3331
3332 0, /* @tp_dealloc@ */
3333 0, /* @tp_print@ */
3334 0, /* @tp_getattr@ */
3335 0, /* @tp_setattr@ */
3336 0, /* @tp_compare@ */
3337 0, /* @tp_repr@ */
3338 0, /* @tp_as_number@ */
3339 0, /* @tp_as_sequence@ */
3340 0, /* @tp_as_mapping@ */
3341 0, /* @tp_hash@ */
3342 0, /* @tp_call@ */
3343 0, /* @tp_str@ */
3344 0, /* @tp_getattro@ */
3345 0, /* @tp_setattro@ */
3346 0, /* @tp_as_buffer@ */
3347 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
3348 Py_TPFLAGS_BASETYPE,
3349
3350 /* @tp_doc@ */
3351 "Pseudorandom permutation metaclass.",
3352
3353 0, /* @tp_traverse@ */
3354 0, /* @tp_clear@ */
3355 0, /* @tp_richcompare@ */
3356 0, /* @tp_weaklistoffset@ */
3357 0, /* @tp_iter@ */
3358 0, /* @tp_iternext@ */
3359 0, /* @tp_methods@ */
3360 0, /* @tp_members@ */
3361 gcprp_pygetset, /* @tp_getset@ */
3362 0, /* @tp_base@ */
3363 0, /* @tp_dict@ */
3364 0, /* @tp_descr_get@ */
3365 0, /* @tp_descr_set@ */
3366 0, /* @tp_dictoffset@ */
3367 0, /* @tp_init@ */
3368 PyType_GenericAlloc, /* @tp_alloc@ */
3369 abstract_pynew, /* @tp_new@ */
3370 0, /* @tp_free@ */
3371 0 /* @tp_is_gc@ */
3372 };
3373
3374 static PyTypeObject gprp_pytype_skel = {
3375 PyObject_HEAD_INIT(0) 0, /* Header */
3376 "GPRP", /* @tp_name@ */
3377 sizeof(gprp_pyobj), /* @tp_basicsize@ */
3378 0, /* @tp_itemsize@ */
3379
3380 gprp_pydealloc, /* @tp_dealloc@ */
3381 0, /* @tp_print@ */
3382 0, /* @tp_getattr@ */
3383 0, /* @tp_setattr@ */
3384 0, /* @tp_compare@ */
3385 0, /* @tp_repr@ */
3386 0, /* @tp_as_number@ */
3387 0, /* @tp_as_sequence@ */
3388 0, /* @tp_as_mapping@ */
3389 0, /* @tp_hash@ */
3390 0, /* @tp_call@ */
3391 0, /* @tp_str@ */
3392 0, /* @tp_getattro@ */
3393 0, /* @tp_setattro@ */
3394 0, /* @tp_as_buffer@ */
3395 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
3396 Py_TPFLAGS_BASETYPE,
3397
3398 /* @tp_doc@ */
3399 "Pseudorandom permutation, abstract base class.",
3400
3401 0, /* @tp_traverse@ */
3402 0, /* @tp_clear@ */
3403 0, /* @tp_richcompare@ */
3404 0, /* @tp_weaklistoffset@ */
3405 0, /* @tp_iter@ */
3406 0, /* @tp_iternext@ */
3407 gprp_pymethods, /* @tp_methods@ */
3408 0, /* @tp_members@ */
3409 0, /* @tp_getset@ */
3410 0, /* @tp_base@ */
3411 0, /* @tp_dict@ */
3412 0, /* @tp_descr_get@ */
3413 0, /* @tp_descr_set@ */
3414 0, /* @tp_dictoffset@ */
3415 0, /* @tp_init@ */
3416 PyType_GenericAlloc, /* @tp_alloc@ */
3417 abstract_pynew, /* @tp_new@ */
3418 0, /* @tp_free@ */
3419 0 /* @tp_is_gc@ */
3420 };
3421
3422 /*----- Main code ---------------------------------------------------------*/
3423
3424 static PyMethodDef methods[] = {
3425 #define METHNAME(func) meth_##func
3426 METH (_KeySZ_fromdl, "\
3427 fromdl(N) -> M: convert integer discrete log field size to work factor")
3428 METH (_KeySZ_fromschnorr, "\
3429 fromschnorr(N) -> M: convert Schnorr group order to work factor")
3430 METH (_KeySZ_fromif, "\
3431 fromif(N) -> M: convert integer factorization problem size to work factor")
3432 METH (_KeySZ_fromec, "\
3433 fromec(N) -> M: convert elliptic curve group order to work factor")
3434 METH (_KeySZ_todl, "\
3435 todl(N) -> M: convert work factor to integer discrete log field size")
3436 METH (_KeySZ_toschnorr, "\
3437 toschnorr(N) -> M: convert work factor to Schnorr group order")
3438 METH (_KeySZ_toif, "\
3439 toif(N) -> M: convert work factor to integer factorization problem size")
3440 METH (_KeySZ_toec, "\
3441 toec(N) -> M: convert work factor to elliptic curve group order")
3442 METH (_KeySZ_toec, "\
3443 toec(N) -> M: convert work factor to elliptic curve group order")
3444 #define METH_HDANCE(hdance, HDance) METH(hdance##_prf, "\
3445 " #hdance "_prf(K, N) -> H: calculate " HDance " hash of N with K")
3446 METH_HDANCE(hsalsa20, "HSalsa20")
3447 METH_HDANCE(hsalsa2012, "HSalsa20/12")
3448 METH_HDANCE(hsalsa208, "HSalsa20/8")
3449 METH_HDANCE(hchacha20, "HChaCha20")
3450 METH_HDANCE(hchacha12, "HChaCha12")
3451 METH_HDANCE(hchacha8, "HChaCha8")
3452 #undef METH_DANCE
3453 #undef METHNAME
3454 { 0 }
3455 };
3456
3457 void algorithms_pyinit(void)
3458 {
3459 INITTYPE(keysz, root);
3460 INITTYPE(keyszany, keysz);
3461 INITTYPE(keyszrange, keysz);
3462 INITTYPE(keyszset, keysz);
3463 INITTYPE(gccipher, type);
3464 INITTYPE(gcipher, root);
3465 INITTYPE(gcaead, type);
3466 INITTYPE(gaeadkey, root);
3467 INITTYPE(gcaeadaad, type);
3468 INITTYPE(gaeadaad, root);
3469 INITTYPE(gcaeadenc, type);
3470 INITTYPE(gaeadenc, root);
3471 INITTYPE(gcaeaddec, type);
3472 INITTYPE(gaeaddec, root);
3473 INITTYPE(gchash, type);
3474 INITTYPE(ghash, root);
3475 INITTYPE(gcmac, type);
3476 INITTYPE(gmac, type);
3477 INITTYPE(gmhash, ghash);
3478 INITTYPE(poly1305cls, type);
3479 INITTYPE_META(poly1305key, type, poly1305cls);
3480 INITTYPE(poly1305hash, root);
3481 INITTYPE(kxvik, root);
3482 INITTYPE(shake, root);
3483 INITTYPE(shake128, shake);
3484 INITTYPE(shake256, shake);
3485 INITTYPE(gcprp, type);
3486 INITTYPE(gprp, root);
3487 addmethods(methods);
3488 }
3489
3490 GEN(gcciphers, cipher)
3491 GEN(gcaeads, aead)
3492 GEN(gchashes, hash)
3493 GEN(gcmacs, mac)
3494 #define gcprp prpinfo
3495 GEN(gcprps, prp)
3496
3497 void algorithms_pyinsert(PyObject *mod)
3498 {
3499 PyObject *d;
3500 INSERT("KeySZ", keysz_pytype);
3501 INSERT("KeySZAny", keyszany_pytype);
3502 INSERT("KeySZRange", keyszrange_pytype);
3503 INSERT("KeySZSet", keyszset_pytype);
3504 INSERT("GCCipher", gccipher_pytype);
3505 INSERT("GCipher", gcipher_pytype);
3506 INSERT("gcciphers", gcciphers());
3507 INSERT("GCAEAD", gcaead_pytype);
3508 INSERT("GAEKey", gaeadkey_pytype);
3509 INSERT("GAEAADClass", gcaeadaad_pytype);
3510 INSERT("GAEAAD", gaeadaad_pytype);
3511 INSERT("GAEEncClass", gcaeadenc_pytype);
3512 INSERT("GAEEnc", gaeadenc_pytype);
3513 INSERT("GAEDecClass", gcaeaddec_pytype);
3514 INSERT("GAEDec", gaeaddec_pytype);
3515 INSERT("gcaeads", gcaeads());
3516 INSERT("GCHash", gchash_pytype);
3517 INSERT("GHash", ghash_pytype);
3518 INSERT("gchashes", d = gchashes());
3519 sha_pyobj = PyDict_GetItemString(d, "sha"); Py_INCREF(sha_pyobj);
3520 has160_pyobj = PyDict_GetItemString(d, "has160"); Py_INCREF(has160_pyobj);
3521 INSERT("GCMAC", gcmac_pytype);
3522 INSERT("GMAC", gmac_pytype);
3523 INSERT("GMACHash", gmhash_pytype);
3524 INSERT("gcmacs", gcmacs());
3525 INSERT("Poly1305Class", poly1305cls_pytype);
3526 INSERT("poly1305", poly1305key_pytype);
3527 INSERT("Poly1305Hash", poly1305hash_pytype);
3528 INSERT("Keccak1600", kxvik_pytype);
3529 INSERT("Shake", shake_pytype);
3530 INSERT("Shake128", shake128_pytype);
3531 INSERT("Shake256", shake256_pytype);
3532 INSERT("GCPRP", gcprp_pytype);
3533 INSERT("GPRP", gprp_pytype);
3534 INSERT("gcprps", gcprps());
3535 }
3536
3537 /*----- That's all, folks -------------------------------------------------*/