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