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