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