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