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