algorithms.c: Delete pointless line break.
[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 PUBLIC_SYMBOLS;
31 #include "algorithms.h"
32 PRIVATE_SYMBOLS;
33
34 /*----- Key sizes ---------------------------------------------------------*/
35
36 PyTypeObject *keysz_pytype;
37 PyTypeObject *keyszany_pytype, *keyszrange_pytype, *keyszset_pytype;
38 PyObject *sha_pyobj, *has160_pyobj;
39
40 #ifndef KSZ_OPMASK
41 # define KSZ_OPMASK 0x1f
42 #endif
43
44 #ifndef KSZ_16BIT
45 # define KSZ_16BIT 0x20
46 #endif
47
48 PyObject *keysz_pywrap(const octet *k)
49 {
50 unsigned op = *k++;
51 #define ARG(i) (op&KSZ_16BIT ? LOAD16(k + 2*(i)) : k[i])
52 switch (op&KSZ_OPMASK) {
53 case KSZ_ANY: {
54 keysz_pyobj *o = PyObject_New(keysz_pyobj, keyszany_pytype);
55 o->dfl = ARG(0);
56 return ((PyObject *)o);
57 } break;
58 case KSZ_RANGE: {
59 keyszrange_pyobj *o =
60 PyObject_New(keyszrange_pyobj, keyszrange_pytype);
61 o->dfl = ARG(0);
62 o->min = ARG(1);
63 o->max = ARG(2);
64 o->mod = ARG(3);
65 if (!o->mod) o->mod = 1;
66 return ((PyObject *)o);
67 } break;
68 case KSZ_SET: {
69 keyszset_pyobj *o =
70 PyObject_New(keyszset_pyobj, keyszset_pytype);
71 int i, n;
72 o->dfl = ARG(0);
73 for (i = 0; ARG(i); i++) ;
74 n = i; o->set = PyTuple_New(n);
75 for (i = 0; i < n; i++)
76 PyTuple_SET_ITEM(o->set, i, PyInt_FromLong(ARG(i)));
77 return ((PyObject *)o);
78 } break;
79 default:
80 abort();
81 }
82 #undef ARG
83 }
84
85 static PyObject *keyszany_pynew(PyTypeObject *ty,
86 PyObject *arg, PyObject *kw)
87 {
88 static const char *const kwlist[] = { "default", 0 };
89 int dfl;
90 keysz_pyobj *o;
91
92 if (!PyArg_ParseTupleAndKeywords(arg, kw, "i:new", KWLIST, &dfl))
93 goto end;
94 if (dfl < 0) VALERR("key size cannot be negative");
95 o = (keysz_pyobj *)ty->tp_alloc(ty, 0);
96 o->dfl = dfl;
97 return ((PyObject *)o);
98 end:
99 return (0);
100 }
101
102 static PyObject *keyszrange_pynew(PyTypeObject *ty,
103 PyObject *arg, PyObject *kw)
104 {
105 static const char *const kwlist[] = { "default", "min", "max", "mod", 0 };
106 int dfl, min = 0, max = 0, mod = 1;
107 keyszrange_pyobj *o;
108
109 if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|iii:new", KWLIST,
110 &dfl, &min, &max, &mod))
111 goto end;
112 if (dfl < 0 || min < 0) VALERR("key size cannot be negative");
113 if (min > dfl || (max && dfl > max)) VALERR("bad key size bounds");
114 if (mod <= 0 || dfl%mod || min%mod || max%mod)
115 VALERR("bad key size modulus");
116 o = (keyszrange_pyobj *)ty->tp_alloc(ty, 0);
117 o->dfl = dfl;
118 o->min = min;
119 o->max = max;
120 o->mod = mod;
121 return ((PyObject *)o);
122 end:
123 return (0);
124 }
125
126 static PyObject *keyszset_pynew(PyTypeObject *ty,
127 PyObject *arg, PyObject *kw)
128 {
129 static const char *const kwlist[] = { "default", "set", 0 };
130 int dfl, i, n, xx;
131 PyObject *set = 0;
132 PyObject *x = 0, *l = 0;
133 keyszset_pyobj *o = 0;
134
135 if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|O:new", KWLIST, &dfl, &set))
136 goto end;
137 if (!set) set = PyTuple_New(0);
138 else Py_INCREF(set);
139 if (!PySequence_Check(set)) TYERR("want a sequence");
140 n = PySequence_Size(set);
141 l = PyList_New(0);
142 if (PyErr_Occurred()) goto end;
143 if (dfl < 0) VALERR("key size cannot be negative");
144 x = PyInt_FromLong(dfl);
145 PyList_Append(l, x);
146 Py_DECREF(x);
147 x = 0;
148 for (i = 0; i < n; i++) {
149 if ((x = PySequence_GetItem(set, i)) == 0) goto end;
150 xx = PyInt_AsLong(x);
151 if (PyErr_Occurred()) goto end;
152 if (xx == dfl) continue;
153 if (xx < 0) VALERR("key size cannot be negative");
154 PyList_Append(l, x);
155 Py_DECREF(x);
156 x = 0;
157 }
158 Py_DECREF(set);
159 if ((set = PySequence_Tuple(l)) == 0) goto end;
160 o = (keyszset_pyobj *)ty->tp_alloc(ty, 0);
161 o->dfl = dfl;
162 o->set = set;
163 Py_INCREF(set);
164 end:
165 Py_XDECREF(set);
166 Py_XDECREF(l);
167 Py_XDECREF(x);
168 return ((PyObject *)o);
169 }
170
171 static PyObject *kaget_min(PyObject *me, void *hunoz)
172 { return (PyInt_FromLong(0)); }
173 #define kaget_max kaget_min
174
175 static PyObject *ksget_min(PyObject *me, void *hunoz)
176 {
177 PyObject *set = ((keyszset_pyobj *)me)->set;
178 int i, n, y, x = -1;
179 n = PyTuple_Size(set);
180 for (i = 0; i < n; i++) {
181 y = PyInt_AsLong(PyTuple_GetItem(set, i));
182 if (x == -1 || y < x) x = y;
183 }
184 return (PyInt_FromLong(x));
185 }
186
187 static PyObject *ksget_max(PyObject *me, void *hunoz)
188 {
189 PyObject *set = ((keyszset_pyobj *)me)->set;
190 int i, n, y, x = -1;
191 n = PyTuple_Size(set);
192 for (i = 0; i < n; i++) {
193 y = PyInt_AsLong(PyTuple_GetItem(set, i));
194 if (y > x) x = y;
195 }
196 return (PyInt_FromLong(x));
197 }
198
199 static PyMemberDef keysz_pymembers[] = {
200 #define MEMBERSTRUCT keysz_pyobj
201 #define default dfl /* ugh! */
202 MEMBER(default, T_INT, READONLY, "KSZ.default -> default key size")
203 #undef default
204 #undef MEMBERSTRUCT
205 { 0 }
206 };
207
208 static PyGetSetDef keyszany_pygetset[] = {
209 #define GETSETNAME(op, name) ka##op##_##name
210 GET (min, "KSZ.min -> smallest allowed key size")
211 GET (max, "KSZ.min -> largest allowed key size")
212 #undef GETSETNAME
213 { 0 }
214 };
215
216 static PyMemberDef keyszrange_pymembers[] = {
217 #define MEMBERSTRUCT keyszrange_pyobj
218 MEMBER(min, T_INT, READONLY, "KSZ.min -> smallest allowed key size")
219 MEMBER(max, T_INT, READONLY, "KSZ.min -> largest allowed key size")
220 MEMBER(mod, T_INT, READONLY,
221 "KSZ.mod -> key size must be a multiple of this")
222 #undef MEMBERSTRUCT
223 { 0 }
224 };
225
226 static PyGetSetDef keyszset_pygetset[] = {
227 #define GETSETNAME(op, name) ks##op##_##name
228 GET (min, "KSZ.min -> smallest allowed key size")
229 GET (max, "KSZ.min -> largest allowed key size")
230 #undef GETSETNAME
231 { 0 }
232 };
233
234 static PyMemberDef keyszset_pymembers[] = {
235 #define MEMBERSTRUCT keyszset_pyobj
236 MEMBER(set, T_OBJECT, READONLY, "KSZ.set -> allowed key sizes")
237 #undef MEMBERSTRUCT
238 { 0 }
239 };
240
241 static PyTypeObject keysz_pytype_skel = {
242 PyObject_HEAD_INIT(0) 0, /* Header */
243 "KeySZ", /* @tp_name@ */
244 sizeof(keysz_pyobj), /* @tp_basicsize@ */
245 0, /* @tp_itemsize@ */
246
247 0, /* @tp_dealloc@ */
248 0, /* @tp_print@ */
249 0, /* @tp_getattr@ */
250 0, /* @tp_setattr@ */
251 0, /* @tp_compare@ */
252 0, /* @tp_repr@ */
253 0, /* @tp_as_number@ */
254 0, /* @tp_as_sequence@ */
255 0, /* @tp_as_mapping@ */
256 0, /* @tp_hash@ */
257 0, /* @tp_call@ */
258 0, /* @tp_str@ */
259 0, /* @tp_getattro@ */
260 0, /* @tp_setattro@ */
261 0, /* @tp_as_buffer@ */
262 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
263 Py_TPFLAGS_BASETYPE,
264
265 /* @tp_doc@ */
266 "Key size constraints. Abstract.",
267
268 0, /* @tp_traverse@ */
269 0, /* @tp_clear@ */
270 0, /* @tp_richcompare@ */
271 0, /* @tp_weaklistoffset@ */
272 0, /* @tp_iter@ */
273 0, /* @tp_iternext@ */
274 0, /* @tp_methods@ */
275 keysz_pymembers, /* @tp_members@ */
276 0, /* @tp_getset@ */
277 0, /* @tp_base@ */
278 0, /* @tp_dict@ */
279 0, /* @tp_descr_get@ */
280 0, /* @tp_descr_set@ */
281 0, /* @tp_dictoffset@ */
282 0, /* @tp_init@ */
283 PyType_GenericAlloc, /* @tp_alloc@ */
284 abstract_pynew, /* @tp_new@ */
285 0, /* @tp_free@ */
286 0 /* @tp_is_gc@ */
287 };
288
289 static PyTypeObject keyszany_pytype_skel = {
290 PyObject_HEAD_INIT(0) 0, /* Header */
291 "KeySZAny", /* @tp_name@ */
292 sizeof(keysz_pyobj), /* @tp_basicsize@ */
293 0, /* @tp_itemsize@ */
294
295 0, /* @tp_dealloc@ */
296 0, /* @tp_print@ */
297 0, /* @tp_getattr@ */
298 0, /* @tp_setattr@ */
299 0, /* @tp_compare@ */
300 0, /* @tp_repr@ */
301 0, /* @tp_as_number@ */
302 0, /* @tp_as_sequence@ */
303 0, /* @tp_as_mapping@ */
304 0, /* @tp_hash@ */
305 0, /* @tp_call@ */
306 0, /* @tp_str@ */
307 0, /* @tp_getattro@ */
308 0, /* @tp_setattro@ */
309 0, /* @tp_as_buffer@ */
310 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
311 Py_TPFLAGS_BASETYPE,
312
313 /* @tp_doc@ */
314 "KeySZAny(DEFAULT)\n\
315 Key size constraints. This object imposes no constraints on size.",
316
317 0, /* @tp_traverse@ */
318 0, /* @tp_clear@ */
319 0, /* @tp_richcompare@ */
320 0, /* @tp_weaklistoffset@ */
321 0, /* @tp_iter@ */
322 0, /* @tp_iternext@ */
323 0, /* @tp_methods@ */
324 0, /* @tp_members@ */
325 keyszany_pygetset, /* @tp_getset@ */
326 0, /* @tp_base@ */
327 0, /* @tp_dict@ */
328 0, /* @tp_descr_get@ */
329 0, /* @tp_descr_set@ */
330 0, /* @tp_dictoffset@ */
331 0, /* @tp_init@ */
332 PyType_GenericAlloc, /* @tp_alloc@ */
333 keyszany_pynew, /* @tp_new@ */
334 0, /* @tp_free@ */
335 0 /* @tp_is_gc@ */
336 };
337
338 static PyTypeObject keyszrange_pytype_skel = {
339 PyObject_HEAD_INIT(0) 0, /* Header */
340 "KeySZRange", /* @tp_name@ */
341 sizeof(keyszrange_pyobj), /* @tp_basicsize@ */
342 0, /* @tp_itemsize@ */
343
344 0, /* @tp_dealloc@ */
345 0, /* @tp_print@ */
346 0, /* @tp_getattr@ */
347 0, /* @tp_setattr@ */
348 0, /* @tp_compare@ */
349 0, /* @tp_repr@ */
350 0, /* @tp_as_number@ */
351 0, /* @tp_as_sequence@ */
352 0, /* @tp_as_mapping@ */
353 0, /* @tp_hash@ */
354 0, /* @tp_call@ */
355 0, /* @tp_str@ */
356 0, /* @tp_getattro@ */
357 0, /* @tp_setattro@ */
358 0, /* @tp_as_buffer@ */
359 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
360 Py_TPFLAGS_BASETYPE,
361
362 /* @tp_doc@ */
363 "KeySZRange(DEFAULT, [min = 0], [max = 0], [mod = 1])\n\
364 Key size constraints. Key size must be between MIN and MAX inclusive,\n\
365 and be a multiple of MOD.",
366
367 0, /* @tp_traverse@ */
368 0, /* @tp_clear@ */
369 0, /* @tp_richcompare@ */
370 0, /* @tp_weaklistoffset@ */
371 0, /* @tp_iter@ */
372 0, /* @tp_iternext@ */
373 0, /* @tp_methods@ */
374 keyszrange_pymembers, /* @tp_members@ */
375 0, /* @tp_getset@ */
376 0, /* @tp_base@ */
377 0, /* @tp_dict@ */
378 0, /* @tp_descr_get@ */
379 0, /* @tp_descr_set@ */
380 0, /* @tp_dictoffset@ */
381 0, /* @tp_init@ */
382 PyType_GenericAlloc, /* @tp_alloc@ */
383 keyszrange_pynew, /* @tp_new@ */
384 0, /* @tp_free@ */
385 0 /* @tp_is_gc@ */
386 };
387
388 static PyTypeObject keyszset_pytype_skel = {
389 PyObject_HEAD_INIT(0) 0, /* Header */
390 "KeySZSet", /* @tp_name@ */
391 sizeof(keyszset_pyobj), /* @tp_basicsize@ */
392 0, /* @tp_itemsize@ */
393
394 0, /* @tp_dealloc@ */
395 0, /* @tp_print@ */
396 0, /* @tp_getattr@ */
397 0, /* @tp_setattr@ */
398 0, /* @tp_compare@ */
399 0, /* @tp_repr@ */
400 0, /* @tp_as_number@ */
401 0, /* @tp_as_sequence@ */
402 0, /* @tp_as_mapping@ */
403 0, /* @tp_hash@ */
404 0, /* @tp_call@ */
405 0, /* @tp_str@ */
406 0, /* @tp_getattro@ */
407 0, /* @tp_setattro@ */
408 0, /* @tp_as_buffer@ */
409 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
410 Py_TPFLAGS_BASETYPE,
411
412 /* @tp_doc@ */
413 "KeySZSet(DEFAULT, SEQ)\n\
414 Key size constraints. Key size must be DEFAULT or one in SEQ.",
415
416 0, /* @tp_traverse@ */
417 0, /* @tp_clear@ */
418 0, /* @tp_richcompare@ */
419 0, /* @tp_weaklistoffset@ */
420 0, /* @tp_iter@ */
421 0, /* @tp_iternext@ */
422 0, /* @tp_methods@ */
423 keyszset_pymembers, /* @tp_members@ */
424 keyszset_pygetset, /* @tp_getset@ */
425 0, /* @tp_base@ */
426 0, /* @tp_dict@ */
427 0, /* @tp_descr_get@ */
428 0, /* @tp_descr_set@ */
429 0, /* @tp_dictoffset@ */
430 0, /* @tp_init@ */
431 PyType_GenericAlloc, /* @tp_alloc@ */
432 keyszset_pynew, /* @tp_new@ */
433 0, /* @tp_free@ */
434 0 /* @tp_is_gc@ */
435 };
436
437 #define KSZCONVOP(op) \
438 static PyObject *meth__KeySZ_##op(PyObject *me, PyObject *arg) \
439 { \
440 double x, y; \
441 if (!PyArg_ParseTuple(arg, "Od:" #op, &me, &x)) return (0); \
442 y = keysz_##op(x); \
443 return (PyFloat_FromDouble(y)); \
444 }
445 KSZCONVOP(fromdl)
446 KSZCONVOP(fromschnorr)
447 KSZCONVOP(fromif)
448 KSZCONVOP(fromec)
449 KSZCONVOP(todl)
450 KSZCONVOP(toschnorr)
451 KSZCONVOP(toif)
452 KSZCONVOP(toec)
453 #undef KSZCONVOP
454
455 /*----- Symmetric encryption ----------------------------------------------*/
456
457 PyTypeObject *gccipher_pytype, *gcipher_pytype;
458
459 CONVFUNC(gccipher, gccipher *, GCCIPHER_CC)
460 CONVFUNC(gcipher, gcipher *, GCIPHER_C)
461
462 PyObject *gcipher_pywrap(PyObject *cobj, gcipher *c)
463 {
464 gcipher_pyobj *g;
465 if (!cobj) cobj = gccipher_pywrap((/*unconst*/ gccipher *)GC_CLASS(c));
466 else Py_INCREF(cobj);
467 g = PyObject_NEW(gcipher_pyobj, (PyTypeObject *)cobj);
468 g->c = c;
469 return ((PyObject *)g);
470 }
471
472 static PyObject *gcipher_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
473 {
474 static const char *const kwlist[] = { "k", 0 };
475 char *k;
476 Py_ssize_t sz;
477
478 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &k, &sz))
479 goto end;
480 if (keysz(sz, GCCIPHER_CC(ty)->keysz) != sz) VALERR("bad key length");
481 return (gcipher_pywrap((PyObject *)ty,
482 GC_INIT(GCCIPHER_CC(ty), k, sz)));
483 end:
484 return (0);
485 }
486
487 PyObject *gccipher_pywrap(gccipher *cc)
488 {
489 gccipher_pyobj *g = newtype(gccipher_pytype, 0, cc->name);
490 g->cc = cc;
491 g->ty.ht_type.tp_basicsize = sizeof(gcipher_pyobj);
492 g->ty.ht_type.tp_base = gcipher_pytype;
493 Py_INCREF(gcipher_pytype);
494 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
495 Py_TPFLAGS_BASETYPE |
496 Py_TPFLAGS_HEAPTYPE);
497 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
498 g->ty.ht_type.tp_free = 0;
499 g->ty.ht_type.tp_new = gcipher_pynew;
500 typeready(&g->ty.ht_type);
501 return ((PyObject *)g);
502 }
503
504 static void gcipher_pydealloc(PyObject *me)
505 {
506 GC_DESTROY(GCIPHER_C(me));
507 Py_DECREF(me->ob_type);
508 FREEOBJ(me);
509 }
510
511 static PyObject *gccget_name(PyObject *me, void *hunoz)
512 { return (PyString_FromString(GCCIPHER_CC(me)->name)); }
513
514 static PyObject *gccget_keysz(PyObject *me, void *hunoz)
515 { return (keysz_pywrap(GCCIPHER_CC(me)->keysz)); }
516
517 static PyObject *gccget_blksz(PyObject *me, void *hunoz)
518 { return (PyInt_FromLong(GCCIPHER_CC(me)->blksz)); }
519
520 static PyObject *gcmeth_encrypt(PyObject *me, PyObject *arg)
521 {
522 char *p;
523 Py_ssize_t sz;
524 PyObject *rc = 0;
525
526 if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &sz)) return (0);
527 rc = bytestring_pywrap(0, sz);
528 GC_ENCRYPT(GCIPHER_C(me), p, PyString_AS_STRING(rc), sz);
529 return (rc);
530 }
531
532 static PyObject *gcmeth_enczero(PyObject *me, PyObject *arg)
533 {
534 char *p;
535 int sz;
536 PyObject *rc = 0;
537
538 if (!PyArg_ParseTuple(arg, "i:enczero", &sz)) return (0);
539 rc = bytestring_pywrap(0, sz);
540 p = PyString_AS_STRING(rc);
541 memset(p, 0, sz);
542 GC_ENCRYPT(GCIPHER_C(me), p, p, sz);
543 return (rc);
544 }
545
546 static PyObject *gcmeth_decrypt(PyObject *me, PyObject *arg)
547 {
548 char *p;
549 Py_ssize_t sz;
550 PyObject *rc = 0;
551
552 if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &sz)) return (0);
553 rc = bytestring_pywrap(0, sz);
554 GC_DECRYPT(GCIPHER_C(me), p, PyString_AS_STRING(rc), sz);
555 return (rc);
556 }
557
558 static PyObject *gcmeth_deczero(PyObject *me, PyObject *arg)
559 {
560 char *p;
561 int sz;
562 PyObject *rc = 0;
563
564 if (!PyArg_ParseTuple(arg, "i:deczero", &sz)) return (0);
565 rc = bytestring_pywrap(0, sz);
566 p = PyString_AS_STRING(rc);
567 memset(p, 0, sz);
568 GC_DECRYPT(GCIPHER_C(me), p, p, sz);
569 return (rc);
570 }
571
572 static PyObject *gcmeth_setiv(PyObject *me, PyObject *arg)
573 {
574 char *p;
575 Py_ssize_t sz;
576
577 if (!PyArg_ParseTuple(arg, "s#:setiv", &p, &sz)) goto end;
578 if (!GCIPHER_C(me)->ops->setiv) VALERR("`setiv' not supported");
579 if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode");
580 if (sz != GC_CLASS(GCIPHER_C(me))->blksz) VALERR("bad IV length");
581 GC_SETIV(GCIPHER_C(me), p);
582 RETURN_ME;
583 end:
584 return (0);
585 }
586
587 static PyObject *gcmeth_bdry(PyObject *me, PyObject *arg)
588 {
589 if (!PyArg_ParseTuple(arg, ":bdry")) goto end;
590 if (!GCIPHER_C(me)->ops->bdry) VALERR("`bdry' not supported");
591 if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode");
592 GC_BDRY(GCIPHER_C(me));
593 RETURN_ME;
594 end:
595 return (0);
596 }
597
598 static PyGetSetDef gccipher_pygetset[] = {
599 #define GETSETNAME(op, name) gcc##op##_##name
600 GET (keysz, "CC.keysz -> acceptable key sizes")
601 GET (blksz, "CC.blksz -> block size, or zero")
602 GET (name, "CC.name -> name of this kind of cipher")
603 #undef GETSETNAME
604 { 0 }
605 };
606
607 static PyMethodDef gcipher_pymethods[] = {
608 #define METHNAME(name) gcmeth_##name
609 METH (encrypt, "C.encrypt(PT) -> CT")
610 METH (enczero, "C.enczero(N) -> CT")
611 METH (decrypt, "C.decrypt(CT) -> PT")
612 METH (deczero, "C.deczero(N) -> PT")
613 METH (setiv, "C.setiv(IV)")
614 METH (bdry, "C.bdry()")
615 #undef METHNAME
616 { 0 }
617 };
618
619 static PyTypeObject gccipher_pytype_skel = {
620 PyObject_HEAD_INIT(0) 0, /* Header */
621 "GCCipher", /* @tp_name@ */
622 sizeof(gccipher_pyobj), /* @tp_basicsize@ */
623 0, /* @tp_itemsize@ */
624
625 0, /* @tp_dealloc@ */
626 0, /* @tp_print@ */
627 0, /* @tp_getattr@ */
628 0, /* @tp_setattr@ */
629 0, /* @tp_compare@ */
630 0, /* @tp_repr@ */
631 0, /* @tp_as_number@ */
632 0, /* @tp_as_sequence@ */
633 0, /* @tp_as_mapping@ */
634 0, /* @tp_hash@ */
635 0, /* @tp_call@ */
636 0, /* @tp_str@ */
637 0, /* @tp_getattro@ */
638 0, /* @tp_setattro@ */
639 0, /* @tp_as_buffer@ */
640 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
641 Py_TPFLAGS_BASETYPE,
642
643 /* @tp_doc@ */
644 "Symmetric cipher metaclass.",
645
646 0, /* @tp_traverse@ */
647 0, /* @tp_clear@ */
648 0, /* @tp_richcompare@ */
649 0, /* @tp_weaklistoffset@ */
650 0, /* @tp_iter@ */
651 0, /* @tp_iternext@ */
652 0, /* @tp_methods@ */
653 0, /* @tp_members@ */
654 gccipher_pygetset, /* @tp_getset@ */
655 0, /* @tp_base@ */
656 0, /* @tp_dict@ */
657 0, /* @tp_descr_get@ */
658 0, /* @tp_descr_set@ */
659 0, /* @tp_dictoffset@ */
660 0, /* @tp_init@ */
661 PyType_GenericAlloc, /* @tp_alloc@ */
662 abstract_pynew, /* @tp_new@ */
663 0, /* @tp_free@ */
664 0 /* @tp_is_gc@ */
665 };
666
667 static PyTypeObject gcipher_pytype_skel = {
668 PyObject_HEAD_INIT(0) 0, /* Header */
669 "GCipher", /* @tp_name@ */
670 sizeof(gcipher_pyobj), /* @tp_basicsize@ */
671 0, /* @tp_itemsize@ */
672
673 gcipher_pydealloc, /* @tp_dealloc@ */
674 0, /* @tp_print@ */
675 0, /* @tp_getattr@ */
676 0, /* @tp_setattr@ */
677 0, /* @tp_compare@ */
678 0, /* @tp_repr@ */
679 0, /* @tp_as_number@ */
680 0, /* @tp_as_sequence@ */
681 0, /* @tp_as_mapping@ */
682 0, /* @tp_hash@ */
683 0, /* @tp_call@ */
684 0, /* @tp_str@ */
685 0, /* @tp_getattro@ */
686 0, /* @tp_setattro@ */
687 0, /* @tp_as_buffer@ */
688 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
689 Py_TPFLAGS_BASETYPE,
690
691 /* @tp_doc@ */
692 "Symmetric cipher, abstract base class.",
693
694 0, /* @tp_traverse@ */
695 0, /* @tp_clear@ */
696 0, /* @tp_richcompare@ */
697 0, /* @tp_weaklistoffset@ */
698 0, /* @tp_iter@ */
699 0, /* @tp_iternext@ */
700 gcipher_pymethods, /* @tp_methods@ */
701 0, /* @tp_members@ */
702 0, /* @tp_getset@ */
703 0, /* @tp_base@ */
704 0, /* @tp_dict@ */
705 0, /* @tp_descr_get@ */
706 0, /* @tp_descr_set@ */
707 0, /* @tp_dictoffset@ */
708 0, /* @tp_init@ */
709 PyType_GenericAlloc, /* @tp_alloc@ */
710 abstract_pynew, /* @tp_new@ */
711 0, /* @tp_free@ */
712 0 /* @tp_is_gc@ */
713 };
714
715 /*----- Authenticated encryption ------------------------------------------*/
716
717 PyTypeObject *gcaead_pytype, *gaeadkey_pytype;
718 PyTypeObject *gcaeadaad_pytype, *gaeadaad_pytype;
719 PyTypeObject *gcaeadenc_pytype, *gaeadenc_pytype;
720 PyTypeObject *gcaeaddec_pytype, *gaeaddec_pytype;
721
722 CONVFUNC(gcaead, gcaead *, GCAEAD_AEC)
723 CONVFUNC(gaeadkey, gaead_key *, GAEADKEY_K)
724
725 PyObject *gaeadkey_pywrap(PyObject *cobj, gaead_key *k)
726 {
727 gaeadkey_pyobj *gk;
728
729 if (!cobj) cobj = gcaead_pywrap((/*unconst*/ gcaead *)GAEAD_CLASS(k));
730 else Py_INCREF(cobj);
731 gk = PyObject_NEW(gaeadkey_pyobj, (PyTypeObject *)cobj);
732 gk->k = k;
733 return ((PyObject *)gk);
734 }
735
736 static PyObject *gaeadkey_pynew(PyTypeObject *ty,
737 PyObject *arg, PyObject *kw)
738 {
739 static const char *const kwlist[] = { "k", 0 };
740 char *k;
741 Py_ssize_t sz;
742
743 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &k, &sz))
744 goto end;
745 if (keysz(sz, GCAEAD_AEC(ty)->keysz) != sz) VALERR("bad key length");
746 return (gaeadkey_pywrap((PyObject *)ty,
747 GAEAD_KEY(GCAEAD_AEC(ty), k, sz)));
748 end:
749 return (0);
750 }
751
752 PyObject *gcaead_pywrap(gcaead *aec)
753 {
754 gcaead_pyobj *gck;
755 gcaeadaad_pyobj *gca;
756 gcaeadenc_pyobj *gce;
757 gcaeaddec_pyobj *gcd;
758
759 #define MKTYPE(obj, thing, newfn, namefmt) do { \
760 (obj) = newtype(gcaead_pytype, 0, 0); \
761 (obj)->ty.ht_name = PyString_FromFormat(namefmt, aec->name); \
762 (obj)->ty.ht_type.tp_name = PyString_AS_STRING((obj)->ty.ht_name); \
763 (obj)->ty.ht_type.tp_basicsize = sizeof(gaead##thing##_pyobj); \
764 (obj)->ty.ht_type.tp_base = gaead##thing##_pytype; \
765 Py_INCREF(gaead##thing##_pytype); \
766 (obj)->ty.ht_type.tp_flags = \
767 (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE); \
768 (obj)->ty.ht_type.tp_alloc = PyType_GenericAlloc; \
769 (obj)->ty.ht_type.tp_free = 0; \
770 (obj)->ty.ht_type.tp_new = newfn; \
771 typeready(&(obj)->ty.ht_type); \
772 } while (0)
773
774 MKTYPE(gck, key, gaeadkey_pynew, "%s(key)");
775 MKTYPE(gca, aad, abstract_pynew, "%s(aad-hash)");
776 MKTYPE(gce, enc, abstract_pynew, "%s(encrypt)");
777 MKTYPE(gcd, dec, abstract_pynew, "%s(decrypt)");
778
779 #undef MKTYPE
780
781 gck->aec = aec; gck->aad = gca; gck->enc = gce; gck->dec = gcd;
782 gca->key = gce->key = gcd->key = gck;
783 return ((PyObject *)gck);
784 }
785
786 static void gaeadkey_pydealloc(PyObject *me)
787 { GAEAD_DESTROY(GAEADKEY_K(me)); Py_DECREF(me->ob_type); FREEOBJ(me); }
788
789 static PyObject *gcaeget_name(PyObject *me, void *hunoz)
790 { return (PyString_FromString(GCAEAD_AEC(me)->name)); }
791
792 static PyObject *gcaeget_keysz(PyObject *me, void *hunoz)
793 { return (keysz_pywrap(GCAEAD_AEC(me)->keysz)); }
794
795 static PyObject *gcaeget_noncesz(PyObject *me, void *hunoz)
796 { return (keysz_pywrap(GCAEAD_AEC(me)->noncesz)); }
797
798 static PyObject *gcaeget_tagsz(PyObject *me, void *hunoz)
799 { return (keysz_pywrap(GCAEAD_AEC(me)->tagsz)); }
800
801 static PyObject *gcaeget_blksz(PyObject *me, void *hunoz)
802 { return (PyInt_FromLong(GCAEAD_AEC(me)->blksz)); }
803
804 static PyObject *gcaeget_bufsz(PyObject *me, void *hunoz)
805 { return (PyInt_FromLong(GCAEAD_AEC(me)->bufsz)); }
806
807 static PyObject *gcaeget_ohd(PyObject *me, void *hunoz)
808 { return (PyInt_FromLong(GCAEAD_AEC(me)->ohd)); }
809
810 static PyObject *gcaeget_flags(PyObject *me, void *hunoz)
811 { return (PyInt_FromLong(GCAEAD_AEC(me)->f)); }
812
813 static PyGetSetDef gcaead_pygetset[] = {
814 #define GETSETNAME(op, name) gcae##op##_##name
815 GET (keysz, "AEC.keysz -> acceptable key sizes")
816 GET (noncesz, "AEC.noncesz -> acceptable nonce sizes")
817 GET (tagsz, "AEC.tagsz -> acceptable tag sizes")
818 GET (blksz, "AEC.blksz -> block size, or zero")
819 GET (bufsz, "AEC.bufsz -> amount of data buffered internally")
820 GET (ohd, "AEC.ohd -> maximum encryption overhead")
821 GET (name, "AEC.name -> name of this kind of AEAD scheme")
822 GET (flags, "AEC.flags -> mask of `AEADF_...' flags")
823 #undef GETSETNAME
824 { 0 }
825 };
826
827 static PyObject *gaekmeth_aad(PyObject *me, PyObject *arg)
828 {
829 const gaead_key *k = GAEADKEY_K(me);
830 PyObject *rc = 0;
831
832 if (!PyArg_ParseTuple(arg, ":aad")) return (0);
833 if (k->ops->c->f&AEADF_AADNDEP)
834 VALERR("aad must be associated with enc/dec op");
835 rc = gaeadaad_pywrap((PyObject *)GCAEAD_AAD(me->ob_type),
836 GAEAD_AAD(k), 0, 0);
837 end:
838 return (rc);
839 }
840
841 static int check_aead_encdec(const gcaead *aec, unsigned *f_out, size_t nsz,
842 PyObject *hszobj, size_t *hsz_out,
843 PyObject *mszobj, size_t *msz_out,
844 PyObject *tszobj, size_t *tsz_out)
845 {
846 unsigned f = 0, miss;
847 int rc = -1;
848
849 if (hszobj != Py_None)
850 { f |= AEADF_PCHSZ; if (!convszt(hszobj, hsz_out)) goto end; }
851 if (mszobj != Py_None)
852 { f |= AEADF_PCMSZ; if (!convszt(mszobj, msz_out)) goto end; }
853 if (tszobj != Py_None)
854 { f |= AEADF_PCTSZ; if (!convszt(tszobj, tsz_out)) goto end; }
855 miss = aec->f&~f;
856 if (miss&AEADF_PCHSZ) VALERR("header length precommitment required");
857 if (miss&AEADF_PCMSZ) VALERR("message length precommitment required");
858 if (miss&AEADF_PCTSZ) VALERR("tag length precommitment required");
859 if (keysz(nsz, aec->noncesz) != nsz) VALERR("bad nonce length");
860 if (tszobj != Py_None && keysz(*tsz_out, aec->tagsz) != *tsz_out)
861 VALERR("bad tag length");
862 *f_out = f | aec->f; rc = 0;
863 end:
864 return (rc);
865 }
866
867 static PyObject *gaekmeth_enc(PyObject *me, PyObject *arg, PyObject *kw)
868 {
869 static const char *const kwlist[] = { "nonce", "hsz", "msz", "tsz", 0 };
870 const gaead_key *k = GAEADKEY_K(me);
871 gaead_enc *e;
872 PyObject *rc = 0;
873 char *n; Py_ssize_t nsz;
874 PyObject *hszobj = Py_None, *mszobj = Py_None, *tszobj = Py_None;
875 size_t hsz = 0, msz = 0, tsz = 0;
876 unsigned f;
877
878 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|OOO:enc", KWLIST,
879 &n, &nsz, &hszobj, &mszobj, &tszobj))
880 goto end;
881 if (check_aead_encdec(k->ops->c, &f, nsz,
882 hszobj, &hsz, mszobj, &msz, tszobj, &tsz))
883 goto end;
884 e = GAEAD_ENC(GAEADKEY_K(me), n, nsz, hsz, msz, tsz);
885 if (!e) VALERR("bad aead parameter combination");
886 rc = gaeadenc_pywrap((PyObject *)GCAEAD_ENC(me->ob_type),
887 e, f, hsz, msz, tsz);
888 end:
889 return (rc);
890 }
891
892 static PyObject *gaekmeth_dec(PyObject *me, PyObject *arg, PyObject *kw)
893 {
894 static const char *const kwlist[] = { "nonce", "hsz", "csz", "tsz", 0 };
895 const gaead_key *k = GAEADKEY_K(me);
896 gaead_dec *d;
897 PyObject *rc = 0;
898 char *n; Py_ssize_t nsz;
899 PyObject *hszobj = Py_None, *cszobj = Py_None, *tszobj = Py_None;
900 size_t hsz = 0, csz = 0, tsz = 0;
901 unsigned f;
902
903 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|OOO:dec", KWLIST,
904 &n, &nsz, &hszobj, &cszobj, &tszobj))
905 goto end;
906 if (check_aead_encdec(k->ops->c, &f, nsz,
907 hszobj, &hsz, cszobj, &csz, tszobj, &tsz))
908 goto end;
909 d = GAEAD_DEC(GAEADKEY_K(me), n, nsz, hsz, csz, tsz);
910 if (!d) VALERR("bad aead parameter combination");
911 rc = gaeaddec_pywrap((PyObject *)GCAEAD_DEC(me->ob_type),
912 d, f, hsz, csz, tsz);
913 end:
914 return (rc);
915 }
916
917 static PyMethodDef gaeadkey_pymethods[] = {
918 #define METHNAME(name) gaekmeth_##name
919 METH (aad, "KEY.aad() -> AAD")
920 KWMETH(enc, "KEY.enc(NONCE, [hsz], [msz], [tsz]) -> ENC")
921 KWMETH(dec, "KEY.dec(NONCE, [hsz], [csz], [tsz]) -> DEC")
922 #undef METHNAME
923 { 0 }
924 };
925
926 PyObject *gaeadaad_pywrap(PyObject *cobj, gaead_aad *a,
927 unsigned f, size_t hsz)
928 {
929 gaeadaad_pyobj *ga;
930
931 assert(cobj); Py_INCREF(cobj);
932 ga = PyObject_NEW(gaeadaad_pyobj, (PyTypeObject *)cobj);
933 ga->a = a; ga->f = f; ga->hsz = hsz; ga->hlen = 0;
934 return ((PyObject *)ga);
935 }
936
937 static void gaeadaad_pydealloc(PyObject *me)
938 {
939 gaeadaad_pyobj *ga = (gaeadaad_pyobj *)me;
940
941 if (ga->a) GAEAD_DESTROY(ga->a);
942 Py_DECREF(me->ob_type); FREEOBJ(me);
943 }
944
945 static int gaea_check(PyObject *me)
946 {
947 gaeadaad_pyobj *ga = (gaeadaad_pyobj *)me;
948 int rc = -1;
949
950 if ((ga->f&AEADF_DEAD) || !ga->a) VALERR("aad object no longer active");
951 rc = 0;
952 end:
953 return (rc);
954 }
955
956 static void gaea_invalidate(gaeadaad_pyobj *ga)
957 { if (ga) ga->f |= AEADF_DEAD; }
958
959 static void gaea_sever(gaeadaad_pyobj **ga_inout)
960 {
961 gaeadaad_pyobj *ga = *ga_inout;
962 if (ga) { ga->f |= AEADF_DEAD; ga->a = 0; Py_DECREF(ga); *ga_inout = 0; }
963 }
964
965 static PyObject *gaeaget_hsz(PyObject *me, void *hunoz)
966 {
967 if (gaea_check(me)) return (0);
968 else if (GAEADAAD_F(me)&AEADF_PCHSZ) return getulong(GAEADAAD_HSZ(me));
969 else RETURN_NONE;
970 }
971
972 static PyObject *gaeaget_hlen(PyObject *me, void *hunoz)
973 { return (gaea_check(me) ? 0 : getulong(GAEADAAD_HLEN(me))); }
974
975 static PyGetSetDef gaeadaad_pygetset[] = {
976 #define GETSETNAME(op, name) gaea##op##_##name
977 GET (hsz, "AAD.hsz -> precommitted header length or `None'")
978 GET (hlen, "AAD.hlen -> header length so far")
979 #undef GETSETNAME
980 { 0 }
981 };
982
983 static PyObject *gaeameth_copy(PyObject *me, PyObject *arg)
984 {
985 PyObject *rc = 0;
986
987 if (!PyArg_ParseTuple(arg, ":copy")) goto end;
988 if (gaea_check(me)) goto end;
989 if (GAEADAAD_F(me)&AEADF_AADNDEP)
990 VALERR("can't duplicate nonce-dependent aad");
991 rc = gaeadaad_pywrap((PyObject *)me->ob_type,
992 GAEAD_DUP(GAEADAAD_A(me)), 0, 0);
993 end:
994 return (rc);
995 }
996
997 static int gaeadaad_hash(PyObject *me, const void *h, size_t hsz)
998 {
999 gaeadaad_pyobj *ga = (gaeadaad_pyobj *)me;
1000 int rc = -1;
1001
1002 if (gaea_check(me)) goto end;
1003 if ((ga->f&AEADF_NOAAD) && hsz)
1004 VALERR("header data not permitted");
1005 if ((ga->f&AEADF_PCHSZ) && hsz > ga->hsz - ga->hlen)
1006 VALERR("too large for precommitted header length");
1007 GAEAD_HASH(ga->a, h, hsz); ga->hlen += hsz;
1008 rc = 0;
1009 end:
1010 return (rc);
1011 }
1012
1013
1014 static PyObject *gaeameth_hash(PyObject *me, PyObject *arg)
1015 {
1016 char *h; Py_ssize_t hsz;
1017
1018 if (!PyArg_ParseTuple(arg, "s#:hash", &h, &hsz)) return (0);
1019 if (gaeadaad_hash(me, h, hsz)) return (0);
1020 RETURN_ME;
1021 }
1022
1023 #define GAEAMETH_HASHU_(n, W, w) \
1024 static PyObject *gaeameth_hashu##w(PyObject *me, PyObject *arg) \
1025 { \
1026 uint##n x; octet b[SZ_##W]; \
1027 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \
1028 STORE##W(b, x); if (gaeadaad_hash(me, b, sizeof(b))) return (0); \
1029 RETURN_ME; \
1030 }
1031 DOUINTCONV(GAEAMETH_HASHU_)
1032
1033 #define GAEAMETH_HASHBUF_(n, W, w) \
1034 static PyObject *gaeameth_hashbuf##w(PyObject *me, PyObject *arg) \
1035 { \
1036 char *p; Py_ssize_t sz; octet b[SZ_##W]; \
1037 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
1038 if (sz > MASK##n) TYERR("string too long"); \
1039 STORE##W(b, sz); if (gaeadaad_hash(me, b, sizeof(b))) goto end; \
1040 if (gaeadaad_hash(me, p, sz)) goto end; \
1041 RETURN_ME; \
1042 end: \
1043 return (0); \
1044 }
1045 DOUINTCONV(GAEAMETH_HASHBUF_)
1046
1047 static PyObject *gaeameth_hashstrz(PyObject *me, PyObject *arg)
1048 {
1049 char *p;
1050 if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0);
1051 if (gaeadaad_hash(me, p, strlen(p) + 1)) return (0);
1052 RETURN_ME;
1053 }
1054
1055 static PyMethodDef gaeadaad_pymethods[] = {
1056 #define METHNAME(name) gaeameth_##name
1057 METH (copy, "AAD.copy() -> AAD'")
1058 METH (hash, "AAD.hash(H)")
1059 #define METHU_(n, W, w) METH(hashu##w, "AAD.hashu" #w "(WORD)")
1060 DOUINTCONV(METHU_)
1061 #undef METHU_
1062 #define METHBUF_(n, W, w) METH(hashbuf##w, "AAD.hashbuf" #w "(BYTES)")
1063 DOUINTCONV(METHBUF_)
1064 #undef METHBUF_
1065 METH (hashstrz, "AAD.hashstrz(STRING)")
1066 #undef METHNAME
1067 { 0 }
1068 };
1069
1070 PyObject *gaeadenc_pywrap(PyObject *cobj, gaead_enc *e, unsigned f,
1071 size_t hsz, size_t msz, size_t tsz)
1072 {
1073 gaeadenc_pyobj *ge;
1074
1075 assert(cobj); Py_INCREF(cobj);
1076 ge = PyObject_NEW(gaeadenc_pyobj, (PyTypeObject *)cobj);
1077 ge->e = e; ge->f = f; ge->hsz = hsz; ge->msz = msz; ge->tsz = tsz;
1078 ge->aad = 0; ge->mlen = 0;
1079 return ((PyObject *)ge);
1080 }
1081
1082 static void gaeadenc_pydealloc(PyObject *me)
1083 {
1084 gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me;
1085
1086 gaea_sever(&ge->aad); GAEAD_DESTROY(ge->e);
1087 Py_DECREF(me->ob_type); FREEOBJ(me);
1088 }
1089
1090 static PyObject *gaeeget_hsz(PyObject *me, void *hunoz)
1091 {
1092 if (GAEADENC_F(me)&AEADF_PCHSZ) return getulong(GAEADENC_HSZ(me));
1093 else RETURN_NONE;
1094 }
1095
1096 static PyObject *gaeeget_msz(PyObject *me, void *hunoz)
1097 {
1098 if (GAEADENC_F(me)&AEADF_PCMSZ) return getulong(GAEADENC_MSZ(me));
1099 else RETURN_NONE;
1100 }
1101
1102 static PyObject *gaeeget_tsz(PyObject *me, void *hunoz)
1103 {
1104 if (GAEADENC_F(me)&AEADF_PCTSZ) return getulong(GAEADENC_TSZ(me));
1105 else RETURN_NONE;
1106 }
1107
1108 static PyObject *gaeeget_mlen(PyObject *me, void *hunoz)
1109 { return getulong(GAEADENC_MLEN(me)); }
1110
1111 static PyGetSetDef gaeadenc_pygetset[] = {
1112 #define GETSETNAME(op, name) gaee##op##_##name
1113 GET (hsz, "ENC.hsz -> precommitted header length or `None'")
1114 GET (msz, "ENC.msz -> precommitted message length or `None'")
1115 GET (tsz, "ENC.tsz -> precommitted tag length or `None'")
1116 GET (mlen, "ENC.mlen -> message length so far")
1117 #undef GETSETNAME
1118 { 0 }
1119 };
1120
1121 static PyObject *gaeemeth_aad(PyObject *me, PyObject *arg)
1122 {
1123 gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me;
1124 PyObject *rc = 0;
1125
1126 if (!PyArg_ParseTuple(arg, ":aad")) return (0);
1127 if (!(ge->f&AEADF_AADNDEP))
1128 rc = gaeadaad_pywrap((PyObject *)GCAEADENC_KEY(ge->ob_type)->aad,
1129 GAEAD_AAD(ge->e), 0, 0);
1130 else {
1131 if ((ge->f&AEADF_AADFIRST) && ge->mlen)
1132 VALERR("too late for aad");
1133 if (!ge->aad)
1134 ge->aad = (gaeadaad_pyobj *)
1135 gaeadaad_pywrap((PyObject *)GCAEADENC_KEY(ge->ob_type)->aad,
1136 GAEAD_AAD(ge->e), ge->f&AEADF_PCHSZ, ge->hsz);
1137 Py_INCREF(ge->aad);
1138 rc = (PyObject *)ge->aad;
1139 }
1140 end:
1141 return (rc);
1142 }
1143
1144 static PyObject *gaeemeth_reinit(PyObject *me, PyObject *arg, PyObject *kw)
1145 {
1146 static const char *const kwlist[] = { "nonce", "hsz", "msz", "tsz", 0 };
1147 gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me;
1148 char *n; Py_ssize_t nsz;
1149 PyObject *hszobj = Py_None, *mszobj = Py_None, *tszobj = Py_None;
1150 size_t hsz = 0, msz = 0, tsz = 0;
1151 unsigned f;
1152
1153 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|OOO:enc", KWLIST,
1154 &n, &nsz, &hszobj, &mszobj, &tszobj))
1155 goto end;
1156 if (check_aead_encdec(ge->e->ops->c, &f, nsz,
1157 hszobj, &hsz, mszobj, &msz, tszobj, &tsz))
1158 goto end;
1159 if (GAEAD_REINIT(ge->e, n, nsz, hsz, msz, tsz))
1160 VALERR("bad aead parameter combination");
1161 gaea_sever(&ge->aad);
1162 ge->f = f; ge->hsz = hsz; ge->msz = msz; ge->tsz = tsz;
1163 end:
1164 return (0);
1165 }
1166
1167 static PyObject *gaeemeth_encrypt(PyObject *me, PyObject *arg)
1168 {
1169 gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me;
1170 char *m; Py_ssize_t msz;
1171 char *c = 0; size_t csz; buf b;
1172 int err;
1173 PyObject *rc = 0;
1174
1175 if (!PyArg_ParseTuple(arg, "s#:encrypt", &m, &msz)) goto end;
1176 if (ge->f&AEADF_AADFIRST) {
1177 if ((ge->f&AEADF_PCHSZ) && (ge->aad ? ge->aad->hlen : 0) != ge->hsz)
1178 VALERR("header doesn't match precommitted length");
1179 gaea_invalidate(ge->aad);
1180 }
1181 if ((ge->f&AEADF_PCMSZ) && msz > ge->msz - ge->mlen)
1182 VALERR("too large for precommitted message length");
1183 csz = msz + ge->e->ops->c->bufsz; c = xmalloc(csz); buf_init(&b, c, csz);
1184 err = GAEAD_ENCRYPT(ge->e, m, msz, &b); assert(!err); (void)err;
1185 buf_flip(&b); rc = bytestring_pywrapbuf(&b); ge->mlen += msz;
1186 end:
1187 xfree(c);
1188 return (rc);
1189 }
1190
1191 static PyObject *gaeemeth_done(PyObject *me, PyObject *arg, PyObject *kw)
1192 {
1193 static const char *const kwlist[] = { "tsz", "aad", 0 };
1194 gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me;
1195 PyObject *aad = Py_None;
1196 char *c = 0; size_t csz; buf b;
1197 PyObject *tszobj = Py_None; PyObject *tag; size_t tsz;
1198 int err;
1199 PyObject *rc = 0;
1200
1201 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|OO:done", KWLIST,
1202 &tszobj, &aad))
1203 goto end;
1204 if (tszobj != Py_None && !convszt(tszobj, &tsz)) goto end;
1205 if (aad != Py_None &&
1206 !PyObject_TypeCheck(aad,
1207 (PyTypeObject *)GCAEADENC_KEY(me->ob_type)->aad))
1208 TYERR("wanted aad");
1209 if ((ge->f&AEADF_AADNDEP) && aad != Py_None && aad != (PyObject *)ge->aad)
1210 VALERR("mismatched aad");
1211 if ((ge->f&AEADF_PCHSZ) &&
1212 (aad == Py_None ? 0 : GAEADAAD_HLEN(aad)) != ge->hsz)
1213 VALERR("header doesn't match precommitted length");
1214 if ((ge->f&AEADF_PCMSZ) && ge->mlen != ge->msz)
1215 VALERR("message doesn't match precommitted length");
1216 if (tszobj == Py_None) {
1217 if (ge->f&AEADF_PCTSZ) tsz = ge->tsz;
1218 else tsz = keysz(0, ge->e->ops->c->tagsz);
1219 } else {
1220 if ((ge->f&AEADF_PCTSZ) && tsz != ge->tsz)
1221 VALERR("tag length doesn't match precommitted value");
1222 if (keysz(tsz, ge->e->ops->c->tagsz) != tsz) VALERR("bad tag length");
1223 }
1224 csz = ge->e->ops->c->bufsz; c = xmalloc(csz); buf_init(&b, c, csz);
1225 tag = bytestring_pywrap(0, tsz);
1226 err = GAEAD_DONE(ge->e, aad == Py_None ? 0 : GAEADAAD_A(aad), &b,
1227 PyString_AS_STRING(tag), tsz);
1228 assert(!err); (void)err;
1229 buf_flip(&b); rc = Py_BuildValue("NN", bytestring_pywrapbuf(&b), tag);
1230 end:
1231 xfree(c);
1232 return (rc);
1233 }
1234
1235 static PyMethodDef gaeadenc_pymethods[] = {
1236 #define METHNAME(name) gaeemeth_##name
1237 METH (aad, "ENC.aad() -> AAD")
1238 KWMETH(reinit, "ENC.reinit(NONCE, [hsz], [msz], [tsz])")
1239 METH (encrypt, "ENC.encrypt(MSG) -> CT")
1240 KWMETH(done, "ENC.done([tsz], [aad]) -> CT, TAG")
1241 #undef METHNAME
1242 { 0 }
1243 };
1244
1245 PyObject *gaeaddec_pywrap(PyObject *cobj, gaead_dec *d, unsigned f,
1246 size_t hsz, size_t csz, size_t tsz)
1247 {
1248 gaeaddec_pyobj *gd;
1249 assert(cobj); Py_INCREF(cobj);
1250 gd = PyObject_NEW(gaeaddec_pyobj, (PyTypeObject *)cobj);
1251 gd->d = d; gd->f = f; gd->hsz = hsz; gd->csz = csz; gd->tsz = tsz;
1252 gd->aad = 0; gd->clen = 0;
1253 return ((PyObject *)gd);
1254 }
1255
1256 static void gaeaddec_pydealloc(PyObject *me)
1257 {
1258 gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me;
1259
1260 gaea_sever(&gd->aad); GAEAD_DESTROY(GAEADDEC_D(me));
1261 Py_DECREF(me->ob_type); FREEOBJ(me);
1262 }
1263
1264 static PyObject *gaedget_hsz(PyObject *me, void *hunoz)
1265 {
1266 if (GAEADDEC_F(me)&AEADF_PCHSZ) return getulong(GAEADDEC_HSZ(me));
1267 else RETURN_NONE;
1268 }
1269
1270 static PyObject *gaedget_csz(PyObject *me, void *hunoz)
1271 {
1272 if (GAEADDEC_F(me)&AEADF_PCMSZ) return getulong(GAEADDEC_CSZ(me));
1273 else RETURN_NONE;
1274 }
1275
1276 static PyObject *gaedget_tsz(PyObject *me, void *hunoz)
1277 {
1278 if (GAEADDEC_F(me)&AEADF_PCTSZ) return getulong(GAEADDEC_TSZ(me));
1279 else RETURN_NONE;
1280 }
1281
1282 static PyObject *gaedget_clen(PyObject *me, void *hunoz)
1283 { return getulong(GAEADDEC_CLEN(me)); }
1284
1285 static PyGetSetDef gaeaddec_pygetset[] = {
1286 #define GETSETNAME(op, name) gaed##op##_##name
1287 GET (hsz, "DEC.hsz -> precommitted header length or `None'")
1288 GET (csz, "DEC.csz -> precommitted ciphertext length or `None'")
1289 GET (tsz, "DEC.tsz -> precommitted tag length or `None'")
1290 GET (clen, "DEC.clen -> ciphertext length so far")
1291 #undef GETSETNAME
1292 { 0 }
1293 };
1294
1295 static PyObject *gaedmeth_aad(PyObject *me, PyObject *arg)
1296 {
1297 gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me;
1298
1299 if (!PyArg_ParseTuple(arg, ":aad")) return (0);
1300 if (!(gd->f&AEADF_AADNDEP))
1301 return (gaeadaad_pywrap((PyObject *)GCAEADDEC_KEY(gd->ob_type)->aad,
1302 GAEAD_AAD(gd->d), 0, 0));
1303 else {
1304 if (!gd->aad)
1305 gd->aad = (gaeadaad_pyobj *)
1306 gaeadaad_pywrap((PyObject *)GCAEADENC_KEY(gd->ob_type)->aad,
1307 GAEAD_AAD(gd->d), gd->f&AEADF_PCHSZ, 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 != 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, PyObject *arg, PyObject *kw)
2671 {
2672 unsigned n = 24;
2673 kxvik_pyobj *rc = 0;
2674 static const char *const kwlist[] = { "nround", 0 };
2675 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", KWLIST,
2676 convuint, &n))
2677 goto end;
2678 rc = (kxvik_pyobj *)ty->tp_alloc(ty, 0);
2679 rc->n = n;
2680 keccak1600_init(&rc->s);
2681 end:
2682 return ((PyObject *)rc);
2683 }
2684
2685 static PyObject *kxvikmeth_copy(PyObject *me, PyObject *arg)
2686 {
2687 kxvik_pyobj *k = (kxvik_pyobj *)me, *rc = 0;
2688 if (!PyArg_ParseTuple(arg, ":copy")) goto end;
2689 rc = (kxvik_pyobj *)k->ob_type->tp_alloc(k->ob_type, 0);
2690 rc->s = k->s; rc->n = k->n;
2691 end:
2692 return ((PyObject *)rc);
2693 }
2694
2695 static PyObject *kxvikmeth_mix(PyObject *me, PyObject *arg)
2696 {
2697 kxvik_pyobj *k = (kxvik_pyobj *)me;
2698 kludge64 t[25];
2699 const octet *q;
2700 octet buf[8];
2701 unsigned i;
2702 char *p; Py_ssize_t n;
2703
2704 if (!PyArg_ParseTuple(arg, "s#:mix", &p, &n)) goto end;
2705 if (n > 200) VALERR("out of range");
2706 q = (const octet *)p;
2707 i = 0;
2708 while (n > 8) { LOAD64_L_(t[i], q); i++; q += 8; n -= 8; }
2709 if (n) {
2710 memcpy(buf, q, n); memset(buf + n, 0, 8 - n);
2711 LOAD64_L_(t[i], buf); i++;
2712 }
2713 keccak1600_mix(&k->s, t, i);
2714 RETURN_ME;
2715 end:
2716 return (0);
2717 }
2718
2719 static PyObject *kxvikmeth_extract(PyObject *me, PyObject *arg)
2720 {
2721 kxvik_pyobj *k = (kxvik_pyobj *)me;
2722 PyObject *rc = 0;
2723 kludge64 t[25];
2724 octet *q, buf[8];
2725 unsigned i;
2726 unsigned n;
2727
2728 if (!PyArg_ParseTuple(arg, "O&:mix", convuint, &n)) goto end;
2729 if (n > 200) VALERR("out of range");
2730 rc = bytestring_pywrap(0, n);
2731 q = (octet *)PyString_AS_STRING(rc);
2732 keccak1600_extract(&k->s, t, (n + 7)/8);
2733 i = 0;
2734 while (n > 8) { STORE64_L_(q, t[i]); i++; q += 8; n -= 8; }
2735 if (n) { STORE64_L_(buf, t[i]); memcpy(q, buf, n); }
2736 end:
2737 return (rc);
2738 }
2739
2740 static PyObject *kxvikmeth_step(PyObject *me, PyObject *arg)
2741 {
2742 kxvik_pyobj *k = (kxvik_pyobj *)me;
2743 if (!PyArg_ParseTuple(arg, ":step")) return (0);
2744 keccak1600_p(&k->s, &k->s, k->n);
2745 RETURN_ME;
2746 }
2747
2748 static PyObject *kxvikget_nround(PyObject *me, void *hunoz)
2749 {
2750 kxvik_pyobj *k = (kxvik_pyobj *)me;
2751 return (PyInt_FromLong(k->n));
2752 }
2753
2754 static int kxvikset_nround(PyObject *me, PyObject *val, void *hunoz)
2755 {
2756 kxvik_pyobj *k = (kxvik_pyobj *)me;
2757 unsigned n;
2758
2759 if (!convuint(val, &n)) return (-1);
2760 k->n = n;
2761 return (0);
2762 }
2763
2764 static PyGetSetDef kxvik_pygetset[] = {
2765 #define GETSETNAME(op, name) kxvik##op##_##name
2766 GETSET(nround, "KECCAK.nround -> number of rounds")
2767 #undef GETSETNAME
2768 { 0 }
2769 };
2770
2771 static PyMethodDef kxvik_pymethods[] = {
2772 #define METHNAME(func) kxvikmeth_##func
2773 METH (copy, "KECCAK.copy() -> KECCAK'")
2774 METH (mix, "KECCAK.mix(DATA)")
2775 METH (extract, "KECCAK.extract(NOCTETS)")
2776 METH (step, "KECCAK.step()")
2777 #undef METHNAME
2778 { 0 }
2779 };
2780
2781 static PyTypeObject kxvik_pytype_skel = {
2782 PyObject_HEAD_INIT(0) 0, /* Header */
2783 "Keccak1600", /* @tp_name@ */
2784 sizeof(kxvik_pyobj), /* @tp_basicsize@ */
2785 0, /* @tp_itemsize@ */
2786
2787 0, /* @tp_dealloc@ */
2788 0, /* @tp_print@ */
2789 0, /* @tp_getattr@ */
2790 0, /* @tp_setattr@ */
2791 0, /* @tp_compare@ */
2792 0, /* @tp_repr@ */
2793 0, /* @tp_as_number@ */
2794 0, /* @tp_as_sequence@ */
2795 0, /* @tp_as_mapping@ */
2796 0, /* @tp_hash@ */
2797 0, /* @tp_call@ */
2798 0, /* @tp_str@ */
2799 0, /* @tp_getattro@ */
2800 0, /* @tp_setattro@ */
2801 0, /* @tp_as_buffer@ */
2802 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
2803 Py_TPFLAGS_BASETYPE,
2804
2805 /* @tp_doc@ */
2806 "Keccak1600([nround = 24]): Keccak-p[1600, n] state.",
2807
2808 0, /* @tp_traverse@ */
2809 0, /* @tp_clear@ */
2810 0, /* @tp_richcompare@ */
2811 0, /* @tp_weaklistoffset@ */
2812 0, /* @tp_iter@ */
2813 0, /* @tp_iternext@ */
2814 kxvik_pymethods, /* @tp_methods@ */
2815 0, /* @tp_members@ */
2816 kxvik_pygetset, /* @tp_getset@ */
2817 0, /* @tp_base@ */
2818 0, /* @tp_dict@ */
2819 0, /* @tp_descr_get@ */
2820 0, /* @tp_descr_set@ */
2821 0, /* @tp_dictoffset@ */
2822 0, /* @tp_init@ */
2823 PyType_GenericAlloc, /* @tp_alloc@ */
2824 kxvik_pynew, /* @tp_new@ */
2825 0, /* @tp_free@ */
2826 0 /* @tp_is_gc@ */
2827 };
2828
2829 static PyTypeObject *shake_pytype, *shake128_pytype, *shake256_pytype;
2830
2831 typedef struct shake_pyobj {
2832 PyObject_HEAD
2833 int st;
2834 shake_ctx h;
2835 } shake_pyobj;
2836
2837 #define SHAKE_H(o) (&((shake_pyobj *)(o))->h)
2838 #define SHAKE_ST(o) (((shake_pyobj *)(o))->st)
2839
2840 static PyObject *shake_dopynew(void (*initfn)(shake_ctx *,
2841 const void *, size_t,
2842 const void *, size_t),
2843 PyTypeObject *ty,
2844 PyObject *arg, PyObject *kw)
2845 {
2846 shake_pyobj *rc = 0;
2847 char *p = 0, *f = 0;
2848 Py_ssize_t psz = 0, fsz = 0;
2849 static const char *const kwlist[] = { "perso", "func", 0 };
2850
2851 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|s#s#:new", KWLIST,
2852 &p, &psz, &f, &fsz))
2853 goto end;
2854 rc = (shake_pyobj *)ty->tp_alloc(ty, 0);
2855 initfn(&rc->h, f, fsz, p, psz);
2856 rc->st = 0;
2857 end:
2858 return ((PyObject *)rc);
2859 }
2860
2861 static PyObject *shake128_pynew(PyTypeObject *ty,
2862 PyObject *arg, PyObject *kw)
2863 { return (shake_dopynew(cshake128_init, ty, arg, kw)); }
2864
2865 static PyObject *shake256_pynew(PyTypeObject *ty,
2866 PyObject *arg, PyObject *kw)
2867 { return (shake_dopynew(cshake256_init, ty, arg, kw)); }
2868
2869 static int shake_check(PyObject *me, int st)
2870 {
2871 if (SHAKE_ST(me) != st) VALERR("wrong state");
2872 return (0);
2873 end:
2874 return (-1);
2875 }
2876
2877 static PyObject *shakemeth_hash(PyObject *me, PyObject *arg)
2878 {
2879 char *p;
2880 Py_ssize_t sz;
2881 if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0);
2882 if (shake_check(me, 0)) return (0);
2883 shake_hash(SHAKE_H(me), p, sz);
2884 RETURN_ME;
2885 }
2886
2887 #define SHAKEMETH_HASHU_(n, W, w) \
2888 static PyObject *shakemeth_hashu##w(PyObject *me, PyObject *arg) \
2889 { \
2890 uint##n x; \
2891 octet b[SZ_##W]; \
2892 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \
2893 if (shake_check(me, 0)) return (0); \
2894 STORE##W(b, x); shake_hash(SHAKE_H(me), b, sizeof(b)); \
2895 RETURN_ME; \
2896 }
2897 DOUINTCONV(SHAKEMETH_HASHU_)
2898
2899 #define SHAKEMETH_HASHBUF_(n, W, w) \
2900 static PyObject *shakemeth_hashbuf##w(PyObject *me, PyObject *arg) \
2901 { \
2902 char *p; \
2903 Py_ssize_t sz; \
2904 octet b[SZ_##W]; \
2905 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
2906 if (sz > MASK##n) TYERR("string too long"); \
2907 if (shake_check(me, 0)) goto end; \
2908 STORE##W(b, sz); shake_hash(SHAKE_H(me), b, sizeof(b)); \
2909 shake_hash(SHAKE_H(me), p, sz); \
2910 RETURN_ME; \
2911 end: \
2912 return (0); \
2913 }
2914 DOUINTCONV(SHAKEMETH_HASHBUF_)
2915
2916 static PyObject *shakemeth_hashstrz(PyObject *me, PyObject *arg)
2917 {
2918 char *p;
2919 if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0);
2920 if (shake_check(me, 0)) return (0);
2921 shake_hash(SHAKE_H(me), p, strlen(p) + 1);
2922 RETURN_ME;
2923 }
2924
2925 static PyObject *shakemeth_xof(PyObject *me, PyObject *arg)
2926 {
2927 if (!PyArg_ParseTuple(arg, ":xof")) goto end;
2928 if (shake_check(me, 0)) goto end;
2929 shake_xof(SHAKE_H(me));
2930 SHAKE_ST(me) = 1;
2931 RETURN_ME;
2932 end:
2933 return (0);
2934 }
2935
2936 static PyObject *shakemeth_done(PyObject *me, PyObject *arg)
2937 {
2938 PyObject *rc = 0;
2939 size_t n;
2940 if (!PyArg_ParseTuple(arg, "O&:done", convszt, &n)) goto end;
2941 if (shake_check(me, 0)) goto end;
2942 rc = bytestring_pywrap(0, n);
2943 shake_done(SHAKE_H(me), PyString_AS_STRING(rc), n);
2944 SHAKE_ST(me) = -1;
2945 end:
2946 return (rc);
2947 }
2948
2949 static PyObject *shakemeth_copy(PyObject *me, PyObject *arg)
2950 {
2951 shake_pyobj *rc = 0;
2952
2953 if (!PyArg_ParseTuple(arg, ":copy")) goto end;
2954 rc = PyObject_NEW(shake_pyobj, me->ob_type);
2955 rc->h = *SHAKE_H(me);
2956 rc->st = SHAKE_ST(me);
2957 end:
2958 return ((PyObject *)me);
2959 }
2960
2961 static PyObject *shakemeth_get(PyObject *me, PyObject *arg)
2962 {
2963 PyObject *rc = 0;
2964 size_t sz;
2965
2966 if (!PyArg_ParseTuple(arg, "O&:get", convszt, &sz)) goto end;
2967 if (shake_check(me, 1)) goto end;
2968 rc = bytestring_pywrap(0, sz);
2969 shake_get(SHAKE_H(me), PyString_AS_STRING(rc), sz);
2970 end:
2971 return (rc);
2972 }
2973
2974 static PyObject *shakemeth_mask(PyObject *me, PyObject *arg)
2975 {
2976 PyObject *rc = 0;
2977 char *p; Py_ssize_t sz;
2978
2979 if (!PyArg_ParseTuple(arg, "s#:mask", &p, &sz)) goto end;
2980 if (shake_check(me, 1)) goto end;
2981 rc = bytestring_pywrap(0, sz);
2982 shake_mask(SHAKE_H(me), p, PyString_AS_STRING(rc), sz);
2983 end:
2984 return (rc);
2985 }
2986
2987 static PyObject *shakeget_rate(PyObject *me, void *hunoz)
2988 { return (PyInt_FromLong(SHAKE_H(me)->h.r)); }
2989
2990 static PyObject *shakeget_buffered(PyObject *me, void *hunoz)
2991 { return (PyInt_FromLong(SHAKE_H(me)->h.n)); }
2992
2993 static PyObject *shakeget_state(PyObject *me, void *hunoz)
2994 {
2995 int st = SHAKE_ST(me);
2996 return (PyString_FromString(st == 0 ? "absorb" :
2997 st == 1 ? "squeeze" : "dead"));
2998 }
2999
3000 static PyGetSetDef shake_pygetset[] = {
3001 #define GETSETNAME(op, name) shake##op##_##name
3002 GET (rate, "S.rate -> rate, in bytes")
3003 GET (buffered, "S.buffered -> amount currently buffered")
3004 GET (state, "S.state -> `absorb', `squeeze', `dead'")
3005 #undef GETSETNAME
3006 { 0 }
3007 };
3008
3009 static PyMethodDef shake_pymethods[] = {
3010 #define METHNAME(func) shakemeth_##func
3011 METH (copy, "S.copy() -> SS")
3012 METH (hash, "S.hash(M)")
3013 #define METHU_(n, W, w) METH(hashu##w, "S.hashu" #w "(WORD)")
3014 DOUINTCONV(METHU_)
3015 #undef METHU_
3016 #define METHBUF_(n, W, w) METH(hashbuf##w, "S.hashbuf" #w "(BYTES)")
3017 DOUINTCONV(METHBUF_)
3018 #undef METHBUF_
3019 METH (hashstrz, "S.hashstrz(STRING)")
3020 METH (xof, "S.xof()")
3021 METH (done, "S.done(LEN) ->H")
3022 METH (get, "S.get(LEN) -> H")
3023 METH (mask, "S.mask(M) -> C")
3024 #undef METHNAME
3025 { 0 }
3026 };
3027
3028 static PyTypeObject shake_pytype_skel = {
3029 PyObject_HEAD_INIT(0) 0, /* Header */
3030 "Shake", /* @tp_name@ */
3031 sizeof(shake_pyobj), /* @tp_basicsize@ */
3032 0, /* @tp_itemsize@ */
3033
3034 0, /* @tp_dealloc@ */
3035 0, /* @tp_print@ */
3036 0, /* @tp_getattr@ */
3037 0, /* @tp_setattr@ */
3038 0, /* @tp_compare@ */
3039 0, /* @tp_repr@ */
3040 0, /* @tp_as_number@ */
3041 0, /* @tp_as_sequence@ */
3042 0, /* @tp_as_mapping@ */
3043 0, /* @tp_hash@ */
3044 0, /* @tp_call@ */
3045 0, /* @tp_str@ */
3046 0, /* @tp_getattro@ */
3047 0, /* @tp_setattro@ */
3048 0, /* @tp_as_buffer@ */
3049 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
3050 Py_TPFLAGS_BASETYPE,
3051
3052 /* @tp_doc@ */
3053 "SHAKE/cSHAKE base class.",
3054
3055 0, /* @tp_traverse@ */
3056 0, /* @tp_clear@ */
3057 0, /* @tp_richcompare@ */
3058 0, /* @tp_weaklistoffset@ */
3059 0, /* @tp_iter@ */
3060 0, /* @tp_iternext@ */
3061 shake_pymethods, /* @tp_methods@ */
3062 0, /* @tp_members@ */
3063 shake_pygetset, /* @tp_getset@ */
3064 0, /* @tp_base@ */
3065 0, /* @tp_dict@ */
3066 0, /* @tp_descr_get@ */
3067 0, /* @tp_descr_set@ */
3068 0, /* @tp_dictoffset@ */
3069 0, /* @tp_init@ */
3070 PyType_GenericAlloc, /* @tp_alloc@ */
3071 abstract_pynew, /* @tp_new@ */
3072 0, /* @tp_free@ */
3073 0 /* @tp_is_gc@ */
3074 };
3075
3076 static PyTypeObject shake128_pytype_skel = {
3077 PyObject_HEAD_INIT(0) 0, /* Header */
3078 "Shake128", /* @tp_name@ */
3079 0, /* @tp_basicsize@ */
3080 0, /* @tp_itemsize@ */
3081
3082 0, /* @tp_dealloc@ */
3083 0, /* @tp_print@ */
3084 0, /* @tp_getattr@ */
3085 0, /* @tp_setattr@ */
3086 0, /* @tp_compare@ */
3087 0, /* @tp_repr@ */
3088 0, /* @tp_as_number@ */
3089 0, /* @tp_as_sequence@ */
3090 0, /* @tp_as_mapping@ */
3091 0, /* @tp_hash@ */
3092 0, /* @tp_call@ */
3093 0, /* @tp_str@ */
3094 0, /* @tp_getattro@ */
3095 0, /* @tp_setattro@ */
3096 0, /* @tp_as_buffer@ */
3097 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
3098 Py_TPFLAGS_BASETYPE,
3099
3100 /* @tp_doc@ */
3101 "Shake128([perso = STR], [func = STR]): SHAKE128/cSHAKE128 XOF.",
3102
3103 0, /* @tp_traverse@ */
3104 0, /* @tp_clear@ */
3105 0, /* @tp_richcompare@ */
3106 0, /* @tp_weaklistoffset@ */
3107 0, /* @tp_iter@ */
3108 0, /* @tp_iternext@ */
3109 0, /* @tp_methods@ */
3110 0, /* @tp_members@ */
3111 0, /* @tp_getset@ */
3112 0, /* @tp_base@ */
3113 0, /* @tp_dict@ */
3114 0, /* @tp_descr_get@ */
3115 0, /* @tp_descr_set@ */
3116 0, /* @tp_dictoffset@ */
3117 0, /* @tp_init@ */
3118 PyType_GenericAlloc, /* @tp_alloc@ */
3119 shake128_pynew, /* @tp_new@ */
3120 0, /* @tp_free@ */
3121 0 /* @tp_is_gc@ */
3122 };
3123
3124 static PyTypeObject shake256_pytype_skel = {
3125 PyObject_HEAD_INIT(0) 0, /* Header */
3126 "Shake256", /* @tp_name@ */
3127 0, /* @tp_basicsize@ */
3128 0, /* @tp_itemsize@ */
3129
3130 0, /* @tp_dealloc@ */
3131 0, /* @tp_print@ */
3132 0, /* @tp_getattr@ */
3133 0, /* @tp_setattr@ */
3134 0, /* @tp_compare@ */
3135 0, /* @tp_repr@ */
3136 0, /* @tp_as_number@ */
3137 0, /* @tp_as_sequence@ */
3138 0, /* @tp_as_mapping@ */
3139 0, /* @tp_hash@ */
3140 0, /* @tp_call@ */
3141 0, /* @tp_str@ */
3142 0, /* @tp_getattro@ */
3143 0, /* @tp_setattro@ */
3144 0, /* @tp_as_buffer@ */
3145 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
3146 Py_TPFLAGS_BASETYPE,
3147
3148 /* @tp_doc@ */
3149 "Shake256([perso = STR], [func = STR]): SHAKE256/cSHAKE256 XOF.",
3150
3151 0, /* @tp_traverse@ */
3152 0, /* @tp_clear@ */
3153 0, /* @tp_richcompare@ */
3154 0, /* @tp_weaklistoffset@ */
3155 0, /* @tp_iter@ */
3156 0, /* @tp_iternext@ */
3157 0, /* @tp_methods@ */
3158 0, /* @tp_members@ */
3159 0, /* @tp_getset@ */
3160 0, /* @tp_base@ */
3161 0, /* @tp_dict@ */
3162 0, /* @tp_descr_get@ */
3163 0, /* @tp_descr_set@ */
3164 0, /* @tp_dictoffset@ */
3165 0, /* @tp_init@ */
3166 PyType_GenericAlloc, /* @tp_alloc@ */
3167 shake256_pynew, /* @tp_new@ */
3168 0, /* @tp_free@ */
3169 0 /* @tp_is_gc@ */
3170 };
3171
3172 /*----- Pseudorandom permutations -----------------------------------------*/
3173
3174 static PyTypeObject *gcprp_pytype, *gprp_pytype;
3175
3176 typedef struct prpinfo {
3177 const char *name;
3178 const octet *keysz;
3179 size_t ctxsz;
3180 size_t blksz;
3181 void (*init)(void *, const void *, size_t);
3182 void (*eblk)(void *, const void *, void *);
3183 void (*dblk)(void *, const void *, void *);
3184 } prpinfo;
3185
3186 #define PRP_DEF(PRE, pre) \
3187 static void pre##_prpinit(void *ctx, const void *k, size_t ksz) \
3188 { pre##_init(ctx, k, ksz); } \
3189 static void pre##_prpeblk(void *ctx, const void *in, void *out) \
3190 { \
3191 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
3192 pre##_eblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
3193 } \
3194 static void pre##_prpdblk(void *ctx, const void *in, void *out) \
3195 { \
3196 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
3197 pre##_dblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
3198 } \
3199 static const prpinfo pre##_prpinfo = { \
3200 #pre, pre##_keysz, sizeof(pre##_ctx), PRE##_BLKSZ, \
3201 pre##_prpinit, pre##_prpeblk, pre##_prpdblk \
3202 };
3203 PRPS(PRP_DEF)
3204
3205 static const struct prpinfo *const gprptab[] = {
3206 #define PRP_ENTRY(PRE, pre) &pre##_prpinfo,
3207 PRPS(PRP_ENTRY)
3208 0
3209 };
3210
3211 typedef struct gcprp_pyobj {
3212 PyHeapTypeObject ty;
3213 const prpinfo *prp;
3214 } gcprp_pyobj;
3215 #define GCPRP_PRP(o) (((gcprp_pyobj *)(o))->prp)
3216
3217 typedef struct gprp_pyobj {
3218 PyObject_HEAD
3219 const prpinfo *prp;
3220 } gprp_pyobj;
3221 #define GPRP_PRP(o) (((gprp_pyobj *)(o))->prp)
3222 #define GPRP_CTX(o) (((gprp_pyobj *)(o)) + 1)
3223
3224 typedef struct prp {
3225 const prpinfo *prp;
3226 void *ctx;
3227 } prp;
3228
3229 static PyObject *gprp_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
3230 {
3231 static const char *const kwlist[] = { "key", 0 };
3232 char *k;
3233 Py_ssize_t sz;
3234 const prpinfo *prp = GCPRP_PRP(ty);
3235 PyObject *me;
3236
3237 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &k, &sz))
3238 goto end;
3239 if (keysz(sz, prp->keysz) != sz) VALERR("bad key length");
3240 me = (PyObject *)ty->tp_alloc(ty, 0);
3241 GPRP_PRP(me) = prp;
3242 prp->init(GPRP_CTX(me), k, sz);
3243 Py_INCREF(me);
3244 return (me);
3245 end:
3246 return (0);
3247 }
3248
3249 static void gprp_pydealloc(PyObject *me)
3250 { Py_DECREF(me->ob_type); FREEOBJ(me); }
3251
3252 static PyObject *gcprp_pywrap(const prpinfo *prp)
3253 {
3254 gcprp_pyobj *g = newtype(gcprp_pytype, 0, prp->name);
3255 g->prp = prp;
3256 g->ty.ht_type.tp_basicsize = sizeof(gprp_pyobj) + prp->ctxsz;
3257 g->ty.ht_type.tp_base = gprp_pytype;
3258 Py_INCREF(gprp_pytype);
3259 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
3260 Py_TPFLAGS_BASETYPE |
3261 Py_TPFLAGS_HEAPTYPE);
3262 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
3263 g->ty.ht_type.tp_free = 0;
3264 g->ty.ht_type.tp_new = gprp_pynew;
3265 typeready(&g->ty.ht_type);
3266 return ((PyObject *)g);
3267 }
3268
3269 static PyObject *gcpget_name(PyObject *me, void *hunoz)
3270 { return (PyString_FromString(GCPRP_PRP(me)->name)); }
3271 static PyObject *gcpget_keysz(PyObject *me, void *hunoz)
3272 { return (keysz_pywrap(GCPRP_PRP(me)->keysz)); }
3273 static PyObject *gcpget_blksz(PyObject *me, void *hunoz)
3274 { return (PyInt_FromLong(GCPRP_PRP(me)->blksz)); }
3275
3276 static PyObject *gpmeth_encrypt(PyObject *me, PyObject *arg)
3277 {
3278 char *p;
3279 Py_ssize_t n;
3280 PyObject *rc = 0;
3281
3282 if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &n)) goto end;
3283 if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length");
3284 rc = bytestring_pywrap(0, n);
3285 GPRP_PRP(me)->eblk(GPRP_CTX(me), p, PyString_AS_STRING(rc));
3286 end:
3287 return (rc);
3288 }
3289
3290 static PyObject *gpmeth_decrypt(PyObject *me, PyObject *arg)
3291 {
3292 char *p;
3293 Py_ssize_t n;
3294 PyObject *rc = 0;
3295
3296 if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &n)) goto end;
3297 if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length");
3298 rc = bytestring_pywrap(0, n);
3299 GPRP_PRP(me)->dblk(GPRP_CTX(me), p, PyString_AS_STRING(rc));
3300 end:
3301 return (rc);
3302 }
3303
3304 static PyGetSetDef gcprp_pygetset[] = {
3305 #define GETSETNAME(op, name) gcp##op##_##name
3306 GET (keysz, "CP.keysz -> acceptable key sizes")
3307 GET (blksz, "CP.blksz -> block size")
3308 GET (name, "CP.name -> name of this kind of PRP")
3309 #undef GETSETNAME
3310 { 0 }
3311 };
3312
3313 static PyMethodDef gprp_pymethods[] = {
3314 #define METHNAME(name) gpmeth_##name
3315 METH (encrypt, "P.encrypt(PT) -> CT")
3316 METH (decrypt, "P.decrypt(CT) -> PT")
3317 #undef METHNAME
3318 { 0 }
3319 };
3320
3321 static PyTypeObject gcprp_pytype_skel = {
3322 PyObject_HEAD_INIT(0) 0, /* Header */
3323 "GCPRP", /* @tp_name@ */
3324 sizeof(gcprp_pyobj), /* @tp_basicsize@ */
3325 0, /* @tp_itemsize@ */
3326
3327 0, /* @tp_dealloc@ */
3328 0, /* @tp_print@ */
3329 0, /* @tp_getattr@ */
3330 0, /* @tp_setattr@ */
3331 0, /* @tp_compare@ */
3332 0, /* @tp_repr@ */
3333 0, /* @tp_as_number@ */
3334 0, /* @tp_as_sequence@ */
3335 0, /* @tp_as_mapping@ */
3336 0, /* @tp_hash@ */
3337 0, /* @tp_call@ */
3338 0, /* @tp_str@ */
3339 0, /* @tp_getattro@ */
3340 0, /* @tp_setattro@ */
3341 0, /* @tp_as_buffer@ */
3342 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
3343 Py_TPFLAGS_BASETYPE,
3344
3345 /* @tp_doc@ */
3346 "Pseudorandom permutation metaclass.",
3347
3348 0, /* @tp_traverse@ */
3349 0, /* @tp_clear@ */
3350 0, /* @tp_richcompare@ */
3351 0, /* @tp_weaklistoffset@ */
3352 0, /* @tp_iter@ */
3353 0, /* @tp_iternext@ */
3354 0, /* @tp_methods@ */
3355 0, /* @tp_members@ */
3356 gcprp_pygetset, /* @tp_getset@ */
3357 0, /* @tp_base@ */
3358 0, /* @tp_dict@ */
3359 0, /* @tp_descr_get@ */
3360 0, /* @tp_descr_set@ */
3361 0, /* @tp_dictoffset@ */
3362 0, /* @tp_init@ */
3363 PyType_GenericAlloc, /* @tp_alloc@ */
3364 abstract_pynew, /* @tp_new@ */
3365 0, /* @tp_free@ */
3366 0 /* @tp_is_gc@ */
3367 };
3368
3369 static PyTypeObject gprp_pytype_skel = {
3370 PyObject_HEAD_INIT(0) 0, /* Header */
3371 "GPRP", /* @tp_name@ */
3372 sizeof(gprp_pyobj), /* @tp_basicsize@ */
3373 0, /* @tp_itemsize@ */
3374
3375 gprp_pydealloc, /* @tp_dealloc@ */
3376 0, /* @tp_print@ */
3377 0, /* @tp_getattr@ */
3378 0, /* @tp_setattr@ */
3379 0, /* @tp_compare@ */
3380 0, /* @tp_repr@ */
3381 0, /* @tp_as_number@ */
3382 0, /* @tp_as_sequence@ */
3383 0, /* @tp_as_mapping@ */
3384 0, /* @tp_hash@ */
3385 0, /* @tp_call@ */
3386 0, /* @tp_str@ */
3387 0, /* @tp_getattro@ */
3388 0, /* @tp_setattro@ */
3389 0, /* @tp_as_buffer@ */
3390 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
3391 Py_TPFLAGS_BASETYPE,
3392
3393 /* @tp_doc@ */
3394 "Pseudorandom permutation, abstract base class.",
3395
3396 0, /* @tp_traverse@ */
3397 0, /* @tp_clear@ */
3398 0, /* @tp_richcompare@ */
3399 0, /* @tp_weaklistoffset@ */
3400 0, /* @tp_iter@ */
3401 0, /* @tp_iternext@ */
3402 gprp_pymethods, /* @tp_methods@ */
3403 0, /* @tp_members@ */
3404 0, /* @tp_getset@ */
3405 0, /* @tp_base@ */
3406 0, /* @tp_dict@ */
3407 0, /* @tp_descr_get@ */
3408 0, /* @tp_descr_set@ */
3409 0, /* @tp_dictoffset@ */
3410 0, /* @tp_init@ */
3411 PyType_GenericAlloc, /* @tp_alloc@ */
3412 abstract_pynew, /* @tp_new@ */
3413 0, /* @tp_free@ */
3414 0 /* @tp_is_gc@ */
3415 };
3416
3417 /*----- Main code ---------------------------------------------------------*/
3418
3419 static PyMethodDef methods[] = {
3420 #define METHNAME(func) meth_##func
3421 METH (_KeySZ_fromdl, "\
3422 fromdl(N) -> M: convert integer discrete log field size to work factor")
3423 METH (_KeySZ_fromschnorr, "\
3424 fromschnorr(N) -> M: convert Schnorr group order to work factor")
3425 METH (_KeySZ_fromif, "\
3426 fromif(N) -> M: convert integer factorization problem size to work factor")
3427 METH (_KeySZ_fromec, "\
3428 fromec(N) -> M: convert elliptic curve group order to work factor")
3429 METH (_KeySZ_todl, "\
3430 todl(N) -> M: convert work factor to integer discrete log field size")
3431 METH (_KeySZ_toschnorr, "\
3432 toschnorr(N) -> M: convert work factor to Schnorr group order")
3433 METH (_KeySZ_toif, "\
3434 toif(N) -> M: convert work factor to integer factorization problem size")
3435 METH (_KeySZ_toec, "\
3436 toec(N) -> M: convert work factor to elliptic curve group order")
3437 METH (_KeySZ_toec, "\
3438 toec(N) -> M: convert work factor to elliptic curve group order")
3439 #define METH_HDANCE(hdance, HDance) METH(hdance##_prf, "\
3440 " #hdance "_prf(K, N) -> H: calculate " HDance " hash of N with K")
3441 METH_HDANCE(hsalsa20, "HSalsa20")
3442 METH_HDANCE(hsalsa2012, "HSalsa20/12")
3443 METH_HDANCE(hsalsa208, "HSalsa20/8")
3444 METH_HDANCE(hchacha20, "HChaCha20")
3445 METH_HDANCE(hchacha12, "HChaCha12")
3446 METH_HDANCE(hchacha8, "HChaCha8")
3447 #undef METH_DANCE
3448 #undef METHNAME
3449 { 0 }
3450 };
3451
3452 void algorithms_pyinit(void)
3453 {
3454 INITTYPE(keysz, root);
3455 INITTYPE(keyszany, keysz);
3456 INITTYPE(keyszrange, keysz);
3457 INITTYPE(keyszset, keysz);
3458 INITTYPE(gccipher, type);
3459 INITTYPE(gcipher, root);
3460 INITTYPE(gcaead, type);
3461 INITTYPE(gaeadkey, root);
3462 INITTYPE(gcaeadaad, type);
3463 INITTYPE(gaeadaad, root);
3464 INITTYPE(gcaeadenc, type);
3465 INITTYPE(gaeadenc, root);
3466 INITTYPE(gcaeaddec, type);
3467 INITTYPE(gaeaddec, root);
3468 INITTYPE(gchash, type);
3469 INITTYPE(ghash, root);
3470 INITTYPE(gcmac, type);
3471 INITTYPE(gmac, type);
3472 INITTYPE(gmhash, ghash);
3473 INITTYPE(poly1305cls, type);
3474 INITTYPE_META(poly1305key, type, poly1305cls);
3475 INITTYPE(poly1305hash, root);
3476 INITTYPE(kxvik, root);
3477 INITTYPE(shake, root);
3478 INITTYPE(shake128, shake);
3479 INITTYPE(shake256, shake);
3480 INITTYPE(gcprp, type);
3481 INITTYPE(gprp, root);
3482 addmethods(methods);
3483 }
3484
3485 GEN(gcciphers, cipher)
3486 GEN(gcaeads, aead)
3487 GEN(gchashes, hash)
3488 GEN(gcmacs, mac)
3489 #define gcprp prpinfo
3490 GEN(gcprps, prp)
3491
3492 void algorithms_pyinsert(PyObject *mod)
3493 {
3494 PyObject *d;
3495 INSERT("KeySZ", keysz_pytype);
3496 INSERT("KeySZAny", keyszany_pytype);
3497 INSERT("KeySZRange", keyszrange_pytype);
3498 INSERT("KeySZSet", keyszset_pytype);
3499 INSERT("GCCipher", gccipher_pytype);
3500 INSERT("GCipher", gcipher_pytype);
3501 INSERT("gcciphers", gcciphers());
3502 INSERT("GCAEAD", gcaead_pytype);
3503 INSERT("GAEKey", gaeadkey_pytype);
3504 INSERT("GAEAADClass", gcaeadaad_pytype);
3505 INSERT("GAEAAD", gaeadaad_pytype);
3506 INSERT("GAEEncClass", gcaeadenc_pytype);
3507 INSERT("GAEEnc", gaeadenc_pytype);
3508 INSERT("GAEDecClass", gcaeaddec_pytype);
3509 INSERT("GAEDec", gaeaddec_pytype);
3510 INSERT("gcaeads", gcaeads());
3511 INSERT("GCHash", gchash_pytype);
3512 INSERT("GHash", ghash_pytype);
3513 INSERT("gchashes", d = gchashes());
3514 sha_pyobj = PyDict_GetItemString(d, "sha"); Py_INCREF(sha_pyobj);
3515 has160_pyobj = PyDict_GetItemString(d, "has160"); Py_INCREF(has160_pyobj);
3516 INSERT("GCMAC", gcmac_pytype);
3517 INSERT("GMAC", gmac_pytype);
3518 INSERT("GMACHash", gmhash_pytype);
3519 INSERT("gcmacs", gcmacs());
3520 INSERT("Poly1305Class", poly1305cls_pytype);
3521 INSERT("poly1305", poly1305key_pytype);
3522 INSERT("Poly1305Hash", poly1305hash_pytype);
3523 INSERT("Keccak1600", kxvik_pytype);
3524 INSERT("Shake", shake_pytype);
3525 INSERT("Shake128", shake128_pytype);
3526 INSERT("Shake256", shake256_pytype);
3527 INSERT("GCPRP", gcprp_pytype);
3528 INSERT("GPRP", gprp_pytype);
3529 INSERT("gcprps", gcprps());
3530 }
3531
3532 /*----- That's all, folks -------------------------------------------------*/