chiark / gitweb /
algorithms.c: Check whether `setiv' and `bdry' are implemented before calling.
[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
38PyObject *keysz_pywrap(const octet *k)
39{
40 switch (k[0]) {
41 case KSZ_ANY: {
42 keysz_pyobj *o = PyObject_New(keysz_pyobj, keyszany_pytype);
43 o->dfl = k[1];
44 return ((PyObject *)o);
45 } break;
46 case KSZ_RANGE: {
47 keyszrange_pyobj *o =
b2687a0a 48 PyObject_New(keyszrange_pyobj, keyszrange_pytype);
d7ab1bab 49 o->dfl = k[1];
50 o->min = k[2];
51 o->max = k[3];
52 o->mod = k[4];
53 if (!o->mod) o->mod = 1;
54 return ((PyObject *)o);
55 } break;
56 case KSZ_SET: {
57 keyszset_pyobj *o =
b2687a0a 58 PyObject_New(keyszset_pyobj, keyszset_pytype);
d7ab1bab 59 int i, n;
60 o->dfl = k[1];
61 for (i = 0; k[i + 1]; i++) ;
62 n = i; o->set = PyTuple_New(n);
63 for (i = 0; i < n; i++)
64 PyTuple_SET_ITEM(o->set, i, PyInt_FromLong(k[i + 1]));
65 return ((PyObject *)o);
66 } break;
67 default:
68 abort();
69 }
70}
71
72static PyObject *keyszany_pynew(PyTypeObject *ty,
73 PyObject *arg, PyObject *kw)
74{
75 char *kwlist[] = { "default", 0 };
76 int dfl;
77 keysz_pyobj *o;
78
79 if (!PyArg_ParseTupleAndKeywords(arg, kw, "i:new", kwlist, &dfl))
80 goto end;
81 if (dfl < 0) VALERR("key size cannot be negative");
82 o = (keysz_pyobj *)ty->tp_alloc(ty, 0);
83 o->dfl = dfl;
84 return ((PyObject *)o);
85end:
86 return (0);
87}
88
89static PyObject *keyszrange_pynew(PyTypeObject *ty,
90 PyObject *arg, PyObject *kw)
91{
92 char *kwlist[] = { "default", "min", "max", "mod", 0 };
93 int dfl, min = 0, max = 0, mod = 1;
94 keyszrange_pyobj *o;
95
96 if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|iii:new", kwlist,
97 &dfl, &min, &max, &mod))
98 goto end;
99 if (dfl < 0 || min < 0 || max < 0)
100 VALERR("key size cannot be negative");
101 if (min > dfl || (max && dfl > max))
102 VALERR("bad key size bounds");
103 if (mod <= 0 || dfl % mod || min % mod || max % mod)
104 VALERR("bad key size modulus");
105 o = (keyszrange_pyobj *)ty->tp_alloc(ty, 0);
106 o->dfl = dfl;
107 o->min = min;
108 o->max = max;
109 o->mod = mod;
110 return ((PyObject *)o);
111end:
112 return (0);
113}
114
115static PyObject *keyszset_pynew(PyTypeObject *ty,
116 PyObject *arg, PyObject *kw)
117{
118 char *kwlist[] = { "default", "set", 0 };
119 int dfl, i, n, xx;
120 PyObject *set = 0;
121 PyObject *x = 0, *l = 0;
122 keyszset_pyobj *o = 0;
123
124 if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|O:new", kwlist,
125 &dfl, &set))
126 goto end;
127 if (!set) set = PyTuple_New(0);
128 else Py_INCREF(set);
129 if (!PySequence_Check(set)) TYERR("want a sequence");
130 n = PySequence_Size(set);
131 l = PyList_New(0);
132 if (PyErr_Occurred()) goto end;
133 if (dfl < 0) VALERR("key size cannot be negative");
134 x = PyInt_FromLong(dfl);
135 PyList_Append(l, x);
136 Py_DECREF(x);
137 x = 0;
138 for (i = 0; i < n; i++) {
139 if ((x = PySequence_GetItem(set, i)) == 0) goto end;
140 xx = PyInt_AsLong(x);
141 if (PyErr_Occurred()) goto end;
142 if (xx == dfl) continue;
143 if (xx < 0) VALERR("key size cannot be negative");
144 PyList_Append(l, x);
145 Py_DECREF(x);
b2687a0a 146 x = 0;
d7ab1bab 147 }
148 Py_DECREF(set);
149 if ((set = PySequence_Tuple(l)) == 0) goto end;
150 o = (keyszset_pyobj *)ty->tp_alloc(ty, 0);
151 o->dfl = dfl;
152 o->set = set;
153 Py_INCREF(set);
154end:
155 Py_XDECREF(set);
156 Py_XDECREF(l);
157 Py_XDECREF(x);
158 return ((PyObject *)o);
159}
160
161static PyObject *kaget_min(PyObject *me, void *hunoz)
162 { return (PyInt_FromLong(0)); }
163#define kaget_max kaget_min
164
165static PyObject *ksget_min(PyObject *me, void *hunoz)
166{
167 PyObject *set = ((keyszset_pyobj *)me)->set;
168 int i, n, y, x = -1;
169 n = PyTuple_Size(set);
170 for (i = 0; i < n; i++) {
171 y = PyInt_AsLong(PyTuple_GetItem(set, i));
172 if (x == -1 || y < x) x = y;
173 }
174 return (PyInt_FromLong(x));
175}
176
177static PyObject *ksget_max(PyObject *me, void *hunoz)
178{
179 PyObject *set = ((keyszset_pyobj *)me)->set;
180 int i, n, y, x = -1;
181 n = PyTuple_Size(set);
182 for (i = 0; i < n; i++) {
183 y = PyInt_AsLong(PyTuple_GetItem(set, i));
184 if (y > x) x = y;
185 }
186 return (PyInt_FromLong(x));
187}
188
189static PyMemberDef keysz_pymembers[] = {
190#define MEMBERSTRUCT keysz_pyobj
191#define default dfl /* ugh! */
192 MEMBER(default, T_INT, READONLY, "KSZ.default -> default key size")
193#undef default
194#undef MEMBERSTRUCT
195 { 0 }
196};
197
198static PyGetSetDef keyszany_pygetset[] = {
199#define GETSETNAME(op, name) ka##op##_##name
200 GET (min, "KSZ.min -> smallest allowed key size")
201 GET (max, "KSZ.min -> largest allowed key size")
202#undef GETSETNAME
203 { 0 }
204};
205
206static PyMemberDef keyszrange_pymembers[] = {
207#define MEMBERSTRUCT keyszrange_pyobj
208 MEMBER(min, T_INT, READONLY, "KSZ.min -> smallest allowed key size")
209 MEMBER(max, T_INT, READONLY, "KSZ.min -> largest allowed key size")
210 MEMBER(mod, T_INT, READONLY,
211 "KSZ.mod -> key size must be a multiple of this")
212#undef MEMBERSTRUCT
213 { 0 }
214};
215
216static PyGetSetDef keyszset_pygetset[] = {
217#define GETSETNAME(op, name) ks##op##_##name
218 GET (min, "KSZ.min -> smallest allowed key size")
219 GET (max, "KSZ.min -> largest allowed key size")
220#undef GETSETNAME
221 { 0 }
222};
223
224static PyMemberDef keyszset_pymembers[] = {
225#define MEMBERSTRUCT keyszset_pyobj
226 MEMBER(set, T_OBJECT, READONLY, "KSZ.set -> allowed key sizes")
227#undef MEMBERSTRUCT
228 { 0 }
229};
230
231static PyTypeObject keysz_pytype_skel = {
6d4db0bf 232 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 233 "KeySZ", /* @tp_name@ */
d7ab1bab 234 sizeof(keysz_pyobj), /* @tp_basicsize@ */
235 0, /* @tp_itemsize@ */
236
3aa33042 237 0, /* @tp_dealloc@ */
d7ab1bab 238 0, /* @tp_print@ */
239 0, /* @tp_getattr@ */
240 0, /* @tp_setattr@ */
241 0, /* @tp_compare@ */
242 0, /* @tp_repr@ */
243 0, /* @tp_as_number@ */
244 0, /* @tp_as_sequence@ */
245 0, /* @tp_as_mapping@ */
246 0, /* @tp_hash@ */
247 0, /* @tp_call@ */
248 0, /* @tp_str@ */
249 0, /* @tp_getattro@ */
250 0, /* @tp_setattro@ */
251 0, /* @tp_as_buffer@ */
252 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
253 Py_TPFLAGS_BASETYPE,
254
255 /* @tp_doc@ */
256"Key size constraints.",
257
258 0, /* @tp_traverse@ */
259 0, /* @tp_clear@ */
260 0, /* @tp_richcompare@ */
261 0, /* @tp_weaklistoffset@ */
262 0, /* @tp_iter@ */
963a6148 263 0, /* @tp_iternext@ */
d7ab1bab 264 0, /* @tp_methods@ */
265 keysz_pymembers, /* @tp_members@ */
266 0, /* @tp_getset@ */
267 0, /* @tp_base@ */
268 0, /* @tp_dict@ */
269 0, /* @tp_descr_get@ */
270 0, /* @tp_descr_set@ */
271 0, /* @tp_dictoffset@ */
272 0, /* @tp_init@ */
273 PyType_GenericAlloc, /* @tp_alloc@ */
274 abstract_pynew, /* @tp_new@ */
3aa33042 275 0, /* @tp_free@ */
d7ab1bab 276 0 /* @tp_is_gc@ */
277};
278
279static PyTypeObject keyszany_pytype_skel = {
6d4db0bf 280 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 281 "KeySZAny", /* @tp_name@ */
d7ab1bab 282 sizeof(keysz_pyobj), /* @tp_basicsize@ */
283 0, /* @tp_itemsize@ */
284
3aa33042 285 0, /* @tp_dealloc@ */
d7ab1bab 286 0, /* @tp_print@ */
287 0, /* @tp_getattr@ */
288 0, /* @tp_setattr@ */
289 0, /* @tp_compare@ */
290 0, /* @tp_repr@ */
291 0, /* @tp_as_number@ */
292 0, /* @tp_as_sequence@ */
293 0, /* @tp_as_mapping@ */
294 0, /* @tp_hash@ */
295 0, /* @tp_call@ */
296 0, /* @tp_str@ */
297 0, /* @tp_getattro@ */
298 0, /* @tp_setattro@ */
299 0, /* @tp_as_buffer@ */
300 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
301 Py_TPFLAGS_BASETYPE,
302
303 /* @tp_doc@ */
304"Key size constraints. This object imposes no constraints on size.",
305
306 0, /* @tp_traverse@ */
307 0, /* @tp_clear@ */
308 0, /* @tp_richcompare@ */
309 0, /* @tp_weaklistoffset@ */
310 0, /* @tp_iter@ */
963a6148 311 0, /* @tp_iternext@ */
d7ab1bab 312 0, /* @tp_methods@ */
313 0, /* @tp_members@ */
314 keyszany_pygetset, /* @tp_getset@ */
315 0, /* @tp_base@ */
316 0, /* @tp_dict@ */
317 0, /* @tp_descr_get@ */
318 0, /* @tp_descr_set@ */
319 0, /* @tp_dictoffset@ */
320 0, /* @tp_init@ */
321 PyType_GenericAlloc, /* @tp_alloc@ */
322 keyszany_pynew, /* @tp_new@ */
3aa33042 323 0, /* @tp_free@ */
d7ab1bab 324 0 /* @tp_is_gc@ */
325};
326
327static PyTypeObject keyszrange_pytype_skel = {
6d4db0bf 328 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 329 "KeySZRange", /* @tp_name@ */
d7ab1bab 330 sizeof(keyszrange_pyobj), /* @tp_basicsize@ */
331 0, /* @tp_itemsize@ */
332
3aa33042 333 0, /* @tp_dealloc@ */
d7ab1bab 334 0, /* @tp_print@ */
335 0, /* @tp_getattr@ */
336 0, /* @tp_setattr@ */
337 0, /* @tp_compare@ */
338 0, /* @tp_repr@ */
339 0, /* @tp_as_number@ */
340 0, /* @tp_as_sequence@ */
341 0, /* @tp_as_mapping@ */
342 0, /* @tp_hash@ */
343 0, /* @tp_call@ */
344 0, /* @tp_str@ */
345 0, /* @tp_getattro@ */
346 0, /* @tp_setattro@ */
347 0, /* @tp_as_buffer@ */
348 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
349 Py_TPFLAGS_BASETYPE,
350
351 /* @tp_doc@ */
352"Key size constraints. This object asserts minimum and maximum (if\n\
353sizes, and requires the key length to be a multiple of some value.",
354
355 0, /* @tp_traverse@ */
356 0, /* @tp_clear@ */
357 0, /* @tp_richcompare@ */
358 0, /* @tp_weaklistoffset@ */
359 0, /* @tp_iter@ */
963a6148 360 0, /* @tp_iternext@ */
d7ab1bab 361 0, /* @tp_methods@ */
362 keyszrange_pymembers, /* @tp_members@ */
363 0, /* @tp_getset@ */
364 0, /* @tp_base@ */
365 0, /* @tp_dict@ */
366 0, /* @tp_descr_get@ */
367 0, /* @tp_descr_set@ */
368 0, /* @tp_dictoffset@ */
369 0, /* @tp_init@ */
370 PyType_GenericAlloc, /* @tp_alloc@ */
371 keyszrange_pynew, /* @tp_new@ */
3aa33042 372 0, /* @tp_free@ */
d7ab1bab 373 0 /* @tp_is_gc@ */
374};
375
376static PyTypeObject keyszset_pytype_skel = {
6d4db0bf 377 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 378 "KeySZSet", /* @tp_name@ */
d7ab1bab 379 sizeof(keyszset_pyobj), /* @tp_basicsize@ */
380 0, /* @tp_itemsize@ */
381
3aa33042 382 0, /* @tp_dealloc@ */
d7ab1bab 383 0, /* @tp_print@ */
384 0, /* @tp_getattr@ */
385 0, /* @tp_setattr@ */
386 0, /* @tp_compare@ */
387 0, /* @tp_repr@ */
388 0, /* @tp_as_number@ */
389 0, /* @tp_as_sequence@ */
390 0, /* @tp_as_mapping@ */
391 0, /* @tp_hash@ */
392 0, /* @tp_call@ */
393 0, /* @tp_str@ */
394 0, /* @tp_getattro@ */
395 0, /* @tp_setattro@ */
396 0, /* @tp_as_buffer@ */
397 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
398 Py_TPFLAGS_BASETYPE,
399
400 /* @tp_doc@ */
401"Key size constraints. This object requires the key to be one of a\n\
402few listed sizes.",
403
404 0, /* @tp_traverse@ */
405 0, /* @tp_clear@ */
406 0, /* @tp_richcompare@ */
407 0, /* @tp_weaklistoffset@ */
408 0, /* @tp_iter@ */
963a6148 409 0, /* @tp_iternext@ */
d7ab1bab 410 0, /* @tp_methods@ */
411 keyszset_pymembers, /* @tp_members@ */
412 keyszset_pygetset, /* @tp_getset@ */
413 0, /* @tp_base@ */
414 0, /* @tp_dict@ */
415 0, /* @tp_descr_get@ */
416 0, /* @tp_descr_set@ */
417 0, /* @tp_dictoffset@ */
418 0, /* @tp_init@ */
419 PyType_GenericAlloc, /* @tp_alloc@ */
420 keyszset_pynew, /* @tp_new@ */
3aa33042 421 0, /* @tp_free@ */
d7ab1bab 422 0 /* @tp_is_gc@ */
423};
424
89157adc
MW
425#define KSZCONVOP(op) \
426 static PyObject *meth__KeySZ_##op(PyObject *me, PyObject *arg) \
427 { \
428 double x, y; \
429 if (!PyArg_ParseTuple(arg, "Od:" #op, &me, &x)) return (0); \
430 y = keysz_##op(x); \
431 return (PyFloat_FromDouble(y)); \
432 }
433KSZCONVOP(fromdl)
434KSZCONVOP(fromschnorr)
435KSZCONVOP(fromif)
436KSZCONVOP(fromec)
437KSZCONVOP(todl)
438KSZCONVOP(toschnorr)
439KSZCONVOP(toif)
440KSZCONVOP(toec)
441#undef KSZCONVOP
442
d7ab1bab 443/*----- Symmetric encryption ----------------------------------------------*/
444
445PyTypeObject *gccipher_pytype, *gcipher_pytype;
446
447CONVFUNC(gccipher, gccipher *, GCCIPHER_CC)
448CONVFUNC(gcipher, gcipher *, GCIPHER_C)
449
450PyObject *gcipher_pywrap(PyObject *cobj, gcipher *c, unsigned f)
451{
452 gcipher_pyobj *g;
453 if (!cobj) cobj = gccipher_pywrap((/*unconst*/ gccipher *)GC_CLASS(c));
454 else Py_INCREF(cobj);
455 g = PyObject_NEW(gcipher_pyobj, (PyTypeObject *)cobj);
456 g->c = c;
457 g->f = f;
b2687a0a 458 return ((PyObject *)g);
d7ab1bab 459}
460
461static PyObject *gcipher_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
462{
463 char *kwlist[] = { "k", 0 };
464 char *k;
6b54260d 465 Py_ssize_t sz;
d7ab1bab 466
467 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz))
468 goto end;
469 if (keysz(sz, GCCIPHER_CC(ty)->keysz) != sz) VALERR("bad key length");
470 return (gcipher_pywrap((PyObject *)ty,
471 GC_INIT(GCCIPHER_CC(ty), k, sz),
472 f_freeme));
473end:
b2687a0a 474 return (0);
d7ab1bab 475}
476
477PyObject *gccipher_pywrap(gccipher *cc)
478{
df9f8366 479 gccipher_pyobj *g = newtype(gccipher_pytype, 0, cc->name);
d7ab1bab 480 g->cc = cc;
24b3d57b
MW
481 g->ty.ht_type.tp_basicsize = sizeof(gcipher_pyobj);
482 g->ty.ht_type.tp_base = gcipher_pytype;
d7ab1bab 483 Py_INCREF(gcipher_pytype);
24b3d57b
MW
484 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
485 Py_TPFLAGS_BASETYPE |
486 Py_TPFLAGS_HEAPTYPE);
487 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
488 g->ty.ht_type.tp_free = 0;
489 g->ty.ht_type.tp_new = gcipher_pynew;
dc075750 490 typeready(&g->ty.ht_type);
d7ab1bab 491 return ((PyObject *)g);
492}
493
494static void gcipher_pydealloc(PyObject *me)
495{
496 if (GCIPHER_F(me) & f_freeme)
497 GC_DESTROY(GCIPHER_C(me));
498 Py_DECREF(me->ob_type);
3aa33042 499 FREEOBJ(me);
d7ab1bab 500}
501
502static PyObject *gccget_name(PyObject *me, void *hunoz)
503 { return (PyString_FromString(GCCIPHER_CC(me)->name)); }
504
505static PyObject *gccget_keysz(PyObject *me, void *hunoz)
506 { return (keysz_pywrap(GCCIPHER_CC(me)->keysz)); }
507
508static PyObject *gccget_blksz(PyObject *me, void *hunoz)
509 { return (PyInt_FromLong(GCCIPHER_CC(me)->blksz)); }
510
511static PyObject *gcmeth_encrypt(PyObject *me, PyObject *arg)
512{
513 char *p;
6b54260d 514 Py_ssize_t sz;
d7ab1bab 515 PyObject *rc = 0;
516
517 if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &sz)) return (0);
518 rc = bytestring_pywrap(0, sz);
519 GC_ENCRYPT(GCIPHER_C(me), p, PyString_AS_STRING(rc), sz);
520 return (rc);
521}
522
523static PyObject *gcmeth_enczero(PyObject *me, PyObject *arg)
524{
525 char *p;
526 int sz;
527 PyObject *rc = 0;
528
529 if (!PyArg_ParseTuple(arg, "i:enczero", &sz)) return (0);
530 rc = bytestring_pywrap(0, sz);
531 p = PyString_AS_STRING(rc);
532 memset(p, 0, sz);
533 GC_ENCRYPT(GCIPHER_C(me), p, p, sz);
534 return (rc);
535}
536
537static PyObject *gcmeth_decrypt(PyObject *me, PyObject *arg)
538{
539 char *p;
6b54260d 540 Py_ssize_t sz;
d7ab1bab 541 PyObject *rc = 0;
542
543 if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &sz)) return (0);
544 rc = bytestring_pywrap(0, sz);
545 GC_DECRYPT(GCIPHER_C(me), p, PyString_AS_STRING(rc), sz);
546 return (rc);
547}
548
549static PyObject *gcmeth_deczero(PyObject *me, PyObject *arg)
550{
551 char *p;
552 int sz;
553 PyObject *rc = 0;
554
555 if (!PyArg_ParseTuple(arg, "i:deczero", &sz)) return (0);
556 rc = bytestring_pywrap(0, sz);
557 p = PyString_AS_STRING(rc);
558 memset(p, 0, sz);
559 GC_DECRYPT(GCIPHER_C(me), p, p, sz);
560 return (rc);
561}
562
563static PyObject *gcmeth_setiv(PyObject *me, PyObject *arg)
564{
565 char *p;
6b54260d 566 Py_ssize_t sz;
d7ab1bab 567
568 if (!PyArg_ParseTuple(arg, "s#:setiv", &p, &sz)) goto end;
30eef666 569 if (!GCIPHER_C(me)->ops->setiv) VALERR("`setiv' not supported");
d7ab1bab 570 if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode");
571 if (sz != GC_CLASS(GCIPHER_C(me))->blksz) VALERR("bad IV length");
572 GC_SETIV(GCIPHER_C(me), p);
573 RETURN_ME;
574end:
575 return (0);
576}
577
578static PyObject *gcmeth_bdry(PyObject *me, PyObject *arg)
579{
580 if (!PyArg_ParseTuple(arg, ":bdry")) goto end;
30eef666 581 if (!GCIPHER_C(me)->ops->bdry) VALERR("`bdry' not supported");
d7ab1bab 582 if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode");
583 GC_BDRY(GCIPHER_C(me));
584 RETURN_ME;
585end:
586 return (0);
587}
588
589static PyGetSetDef gccipher_pygetset[] = {
590#define GETSETNAME(op, name) gcc##op##_##name
591 GET (keysz, "CC.keysz -> acceptable key sizes")
592 GET (blksz, "CC.blksz -> block size, or zero")
593 GET (name, "CC.name -> name of this kind of cipher")
594#undef GETSETNAME
595 { 0 }
596};
597
598static PyMethodDef gcipher_pymethods[] = {
599#define METHNAME(name) gcmeth_##name
600 METH (encrypt, "C.encrypt(PT) -> CT")
601 METH (enczero, "C.enczero(N) -> CT")
602 METH (decrypt, "C.decrypt(CT) -> PT")
603 METH (deczero, "C.deczero(N) -> PT")
604 METH (setiv, "C.setiv(IV)")
605 METH (bdry, "C.bdry()")
606#undef METHNAME
607 { 0 }
608};
609
610static PyTypeObject gccipher_pytype_skel = {
6d4db0bf 611 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 612 "GCCipher", /* @tp_name@ */
d7ab1bab 613 sizeof(gccipher_pyobj), /* @tp_basicsize@ */
614 0, /* @tp_itemsize@ */
615
616 0, /* @tp_dealloc@ */
617 0, /* @tp_print@ */
618 0, /* @tp_getattr@ */
619 0, /* @tp_setattr@ */
620 0, /* @tp_compare@ */
621 0, /* @tp_repr@ */
622 0, /* @tp_as_number@ */
623 0, /* @tp_as_sequence@ */
624 0, /* @tp_as_mapping@ */
625 0, /* @tp_hash@ */
626 0, /* @tp_call@ */
627 0, /* @tp_str@ */
628 0, /* @tp_getattro@ */
629 0, /* @tp_setattro@ */
630 0, /* @tp_as_buffer@ */
631 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
632 Py_TPFLAGS_BASETYPE,
633
634 /* @tp_doc@ */
635"Symmetric cipher metaclass.",
636
637 0, /* @tp_traverse@ */
638 0, /* @tp_clear@ */
639 0, /* @tp_richcompare@ */
640 0, /* @tp_weaklistoffset@ */
641 0, /* @tp_iter@ */
963a6148 642 0, /* @tp_iternext@ */
d7ab1bab 643 0, /* @tp_methods@ */
644 0, /* @tp_members@ */
645 gccipher_pygetset, /* @tp_getset@ */
646 0, /* @tp_base@ */
647 0, /* @tp_dict@ */
648 0, /* @tp_descr_get@ */
649 0, /* @tp_descr_set@ */
650 0, /* @tp_dictoffset@ */
651 0, /* @tp_init@ */
652 PyType_GenericAlloc, /* @tp_alloc@ */
653 abstract_pynew, /* @tp_new@ */
3aa33042 654 0, /* @tp_free@ */
d7ab1bab 655 0 /* @tp_is_gc@ */
656};
657
658static PyTypeObject gcipher_pytype_skel = {
6d4db0bf 659 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 660 "GCipher", /* @tp_name@ */
d7ab1bab 661 sizeof(gcipher_pyobj), /* @tp_basicsize@ */
662 0, /* @tp_itemsize@ */
663
664 gcipher_pydealloc, /* @tp_dealloc@ */
665 0, /* @tp_print@ */
666 0, /* @tp_getattr@ */
667 0, /* @tp_setattr@ */
668 0, /* @tp_compare@ */
669 0, /* @tp_repr@ */
670 0, /* @tp_as_number@ */
671 0, /* @tp_as_sequence@ */
672 0, /* @tp_as_mapping@ */
673 0, /* @tp_hash@ */
674 0, /* @tp_call@ */
675 0, /* @tp_str@ */
676 0, /* @tp_getattro@ */
677 0, /* @tp_setattro@ */
678 0, /* @tp_as_buffer@ */
679 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
680 Py_TPFLAGS_BASETYPE,
681
682 /* @tp_doc@ */
683"Symmetric cipher, abstract base class.",
684
685 0, /* @tp_traverse@ */
686 0, /* @tp_clear@ */
687 0, /* @tp_richcompare@ */
688 0, /* @tp_weaklistoffset@ */
689 0, /* @tp_iter@ */
963a6148 690 0, /* @tp_iternext@ */
d7ab1bab 691 gcipher_pymethods, /* @tp_methods@ */
692 0, /* @tp_members@ */
693 0, /* @tp_getset@ */
694 0, /* @tp_base@ */
695 0, /* @tp_dict@ */
696 0, /* @tp_descr_get@ */
697 0, /* @tp_descr_set@ */
698 0, /* @tp_dictoffset@ */
699 0, /* @tp_init@ */
700 PyType_GenericAlloc, /* @tp_alloc@ */
701 abstract_pynew, /* @tp_new@ */
3aa33042 702 0, /* @tp_free@ */
d7ab1bab 703 0 /* @tp_is_gc@ */
704};
705
706/*----- Hash functions ----------------------------------------------------*/
707
708PyTypeObject *gchash_pytype, *ghash_pytype;
709
710CONVFUNC(gchash, gchash *, GCHASH_CH)
711CONVFUNC(ghash, ghash *, GHASH_H)
712
713static PyObject *ghash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
714{
715 char *kwlist[] = { 0 };
716 if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", kwlist))
717 goto end;
718 return (ghash_pywrap((PyObject *)ty, GH_INIT(GCHASH_CH(ty)), f_freeme));
719end:
b2687a0a 720 return (0);
d7ab1bab 721}
722
723PyObject *gchash_pywrap(gchash *ch)
724{
df9f8366 725 gchash_pyobj *g = newtype(gchash_pytype, 0, ch->name);
d7ab1bab 726 g->ch = ch;
24b3d57b
MW
727 g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj);
728 g->ty.ht_type.tp_base = ghash_pytype;
d7ab1bab 729 Py_INCREF(ghash_pytype);
24b3d57b
MW
730 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
731 Py_TPFLAGS_BASETYPE |
732 Py_TPFLAGS_HEAPTYPE);
733 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
734 g->ty.ht_type.tp_free = 0;
735 g->ty.ht_type.tp_new = ghash_pynew;
dc075750 736 typeready(&g->ty.ht_type);
d7ab1bab 737 return ((PyObject *)g);
738}
739
740PyObject *ghash_pywrap(PyObject *cobj, ghash *h, unsigned f)
741{
742 ghash_pyobj *g;
743 if (!cobj) cobj = gchash_pywrap((/*unconst*/ gchash *)GH_CLASS(h));
744 else Py_INCREF(cobj);
745 g = PyObject_NEW(ghash_pyobj, (PyTypeObject *)cobj);
746 g->h = h;
747 g->f = f;
b2687a0a 748 return ((PyObject *)g);
d7ab1bab 749}
750
751static void ghash_pydealloc(PyObject *me)
752{
753 if (GHASH_F(me) & f_freeme)
754 GH_DESTROY(GHASH_H(me));
755 Py_DECREF(me->ob_type);
3aa33042 756 FREEOBJ(me);
d7ab1bab 757}
758
759static PyObject *gchget_name(PyObject *me, void *hunoz)
760 { return (PyString_FromString(GCHASH_CH(me)->name)); }
761
762static PyObject *gchget_hashsz(PyObject *me, void *hunoz)
763 { return (PyInt_FromLong(GCHASH_CH(me)->hashsz)); }
764
765static PyObject *gchget_bufsz(PyObject *me, void *hunoz)
766 { return (PyInt_FromLong(GCHASH_CH(me)->bufsz)); }
767
768static PyObject *ghmeth_hash(PyObject *me, PyObject *arg)
769{
770 char *p;
6b54260d 771 Py_ssize_t sz;
d7ab1bab 772 if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0);
773 GH_HASH(GHASH_H(me), p, sz);
774 RETURN_ME;
775}
776
46e6ad89 777#define GHMETH_HASHU_(n, W, w) \
778 static PyObject *ghmeth_hashu##w(PyObject *me, PyObject *arg) \
779 { \
780 uint##n x; \
781 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \
782 GH_HASHU##W(GHASH_H(me), x); \
783 RETURN_ME; \
784 end: \
785 return (0); \
786 }
787DOUINTCONV(GHMETH_HASHU_)
788
789#define GHMETH_HASHBUF_(n, W, w) \
790 static PyObject *ghmeth_hashbuf##w(PyObject *me, PyObject *arg) \
791 { \
792 char *p; \
6b54260d 793 Py_ssize_t sz; \
46e6ad89 794 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
795 if (sz > MASK##n) TYERR("string too long"); \
796 GH_HASHBUF##W(GHASH_H(me), p, sz); \
797 RETURN_ME; \
798 end: \
799 return (0); \
800 }
801DOUINTCONV(GHMETH_HASHBUF_)
802
803static PyObject *ghmeth_hashstrz(PyObject *me, PyObject *arg)
804{
805 char *p;
806 if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0);
807 GH_HASHSTRZ(GHASH_H(me), p);
808 RETURN_ME;
809}
810
07bcd768
MW
811static PyObject *ghmeth_done(PyObject *me, PyObject *arg)
812{
813 ghash *g;
814 PyObject *rc;
815 if (!PyArg_ParseTuple(arg, ":done")) return (0);
816 g = GH_COPY(GHASH_H(me));
817 rc = bytestring_pywrap(0, g->ops->c->hashsz);
818 GH_DONE(g, PyString_AS_STRING(rc));
819 GH_DESTROY(g);
820 return (rc);
821}
822
823static PyGetSetDef gchash_pygetset[] = {
824#define GETSETNAME(op, name) gch##op##_##name
825 GET (bufsz, "CH.bufsz -> hash buffer size, or zero")
826 GET (hashsz, "CH.hashsz -> hash output size")
827 GET (name, "CH.name -> name of this kind of hash")
828#undef GETSETNAME
829 { 0 }
830};
831
d7ab1bab 832static PyMethodDef ghash_pymethods[] = {
833#define METHNAME(name) ghmeth_##name
834 METH (hash, "H.hash(M)")
46e6ad89 835#define METHU_(n, W, w) METH(hashu##w, "H.hashu" #w "(WORD)")
836 DOUINTCONV(METHU_)
07bcd768 837#undef METHU_
46e6ad89 838#define METHBUF_(n, W, w) METH(hashbuf##w, "H.hashbuf" #w "(BYTES)")
839 DOUINTCONV(METHBUF_)
07bcd768 840#undef METHBUF_
46e6ad89 841 METH (hashstrz, "H.hashstrz(STRING)")
d7ab1bab 842 METH (done, "H.done() -> HASH")
843#undef METHNAME
844 { 0 }
845};
846
847static PyTypeObject gchash_pytype_skel = {
6d4db0bf 848 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 849 "GCHash", /* @tp_name@ */
d7ab1bab 850 sizeof(gchash_pyobj), /* @tp_basicsize@ */
851 0, /* @tp_itemsize@ */
852
853 0, /* @tp_dealloc@ */
854 0, /* @tp_print@ */
855 0, /* @tp_getattr@ */
856 0, /* @tp_setattr@ */
857 0, /* @tp_compare@ */
858 0, /* @tp_repr@ */
859 0, /* @tp_as_number@ */
860 0, /* @tp_as_sequence@ */
861 0, /* @tp_as_mapping@ */
862 0, /* @tp_hash@ */
863 0, /* @tp_call@ */
864 0, /* @tp_str@ */
865 0, /* @tp_getattro@ */
866 0, /* @tp_setattro@ */
867 0, /* @tp_as_buffer@ */
868 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
869 Py_TPFLAGS_BASETYPE,
870
871 /* @tp_doc@ */
872"Hash function metaclass.",
873
874 0, /* @tp_traverse@ */
875 0, /* @tp_clear@ */
876 0, /* @tp_richcompare@ */
877 0, /* @tp_weaklistoffset@ */
878 0, /* @tp_iter@ */
963a6148 879 0, /* @tp_iternext@ */
d7ab1bab 880 0, /* @tp_methods@ */
881 0, /* @tp_members@ */
882 gchash_pygetset, /* @tp_getset@ */
883 0, /* @tp_base@ */
884 0, /* @tp_dict@ */
885 0, /* @tp_descr_get@ */
886 0, /* @tp_descr_set@ */
887 0, /* @tp_dictoffset@ */
888 0, /* @tp_init@ */
889 PyType_GenericAlloc, /* @tp_alloc@ */
890 abstract_pynew, /* @tp_new@ */
3aa33042 891 0, /* @tp_free@ */
d7ab1bab 892 0 /* @tp_is_gc@ */
893};
894
895static PyTypeObject ghash_pytype_skel = {
6d4db0bf 896 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 897 "GHash", /* @tp_name@ */
d7ab1bab 898 sizeof(ghash_pyobj), /* @tp_basicsize@ */
899 0, /* @tp_itemsize@ */
900
901 ghash_pydealloc, /* @tp_dealloc@ */
902 0, /* @tp_print@ */
903 0, /* @tp_getattr@ */
904 0, /* @tp_setattr@ */
905 0, /* @tp_compare@ */
906 0, /* @tp_repr@ */
907 0, /* @tp_as_number@ */
908 0, /* @tp_as_sequence@ */
909 0, /* @tp_as_mapping@ */
910 0, /* @tp_hash@ */
911 0, /* @tp_call@ */
912 0, /* @tp_str@ */
913 0, /* @tp_getattro@ */
914 0, /* @tp_setattro@ */
915 0, /* @tp_as_buffer@ */
916 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
917 Py_TPFLAGS_BASETYPE,
918
919 /* @tp_doc@ */
920"Hash function, abstract base class.",
921
922 0, /* @tp_traverse@ */
923 0, /* @tp_clear@ */
924 0, /* @tp_richcompare@ */
925 0, /* @tp_weaklistoffset@ */
926 0, /* @tp_iter@ */
963a6148 927 0, /* @tp_iternext@ */
d7ab1bab 928 ghash_pymethods, /* @tp_methods@ */
929 0, /* @tp_members@ */
930 0, /* @tp_getset@ */
931 0, /* @tp_base@ */
932 0, /* @tp_dict@ */
933 0, /* @tp_descr_get@ */
934 0, /* @tp_descr_set@ */
935 0, /* @tp_dictoffset@ */
936 0, /* @tp_init@ */
937 PyType_GenericAlloc, /* @tp_alloc@ */
938 abstract_pynew, /* @tp_new@ */
3aa33042 939 0, /* @tp_free@ */
d7ab1bab 940 0 /* @tp_is_gc@ */
941};
942
943/*----- Message authentication --------------------------------------------*/
944
945PyTypeObject *gcmac_pytype, *gmac_pytype, *gmhash_pytype;
946
947CONVFUNC(gcmac, gcmac *, GCMAC_CM)
948CONVFUNC(gmac, gmac *, GMAC_M)
949CONVFUNC(gmhash, ghash *, GHASH_H)
950
951static PyObject *gmac_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
952{
953 char *kwlist[] = { "k", 0 };
954 char *k;
6b54260d 955 Py_ssize_t sz;
d7ab1bab 956
957 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz))
958 goto end;
959 if (keysz(sz, GCMAC_CM(ty)->keysz) != sz) VALERR("bad key length");
960 return (gmac_pywrap((PyObject *)ty,
961 GM_KEY(GCMAC_CM(ty), k, sz),
962 f_freeme));
963end:
b2687a0a 964 return (0);
d7ab1bab 965}
966
967static PyObject *gmhash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
968{
969 char *kwlist[] = { 0 };
970 ghash_pyobj *g;
971
972 if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", kwlist)) return (0);
973 g = PyObject_NEW(ghash_pyobj, ty);
974 g->h = GM_INIT(GMAC_M(ty));
975 g->f = f_freeme;
976 Py_INCREF(ty);
977 return ((PyObject *)g);
978}
979
980PyObject *gcmac_pywrap(gcmac *cm)
981{
df9f8366 982 gcmac_pyobj *g = newtype(gcmac_pytype, 0, cm->name);
d7ab1bab 983 g->cm = cm;
24b3d57b
MW
984 g->ty.ht_type.tp_basicsize = sizeof(gmac_pyobj);
985 g->ty.ht_type.tp_base = gmac_pytype;
d7ab1bab 986 Py_INCREF(gmac_pytype);
24b3d57b
MW
987 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
988 Py_TPFLAGS_BASETYPE |
989 Py_TPFLAGS_HEAPTYPE);
990 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
991 g->ty.ht_type.tp_free = 0;
992 g->ty.ht_type.tp_new = gmac_pynew;
dc075750 993 typeready(&g->ty.ht_type);
d7ab1bab 994 return ((PyObject *)g);
995}
996
997PyObject *gmac_pywrap(PyObject *cobj, gmac *m, unsigned f)
998{
999 gmac_pyobj *g;
1000 if (!cobj) cobj = gcmac_pywrap((/*unconst*/ gcmac *)GM_CLASS(m));
1001 else Py_INCREF(cobj);
df9f8366 1002 g = newtype((PyTypeObject *)cobj, 0, 0);
828b1388 1003 g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj);
24b3d57b
MW
1004 g->ty.ht_name = PyString_FromFormat("%s(keyed)", m->ops->c->name);
1005 g->ty.ht_type.tp_name = PyString_AS_STRING(g->ty.ht_name);
1006 g->ty.ht_type.tp_base = gmhash_pytype;
d7ab1bab 1007 Py_INCREF(gmac_pytype);
24b3d57b
MW
1008 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
1009 Py_TPFLAGS_BASETYPE |
1010 Py_TPFLAGS_HEAPTYPE);
1011 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
1012 g->ty.ht_type.tp_free = 0;
1013 g->ty.ht_type.tp_new = gmhash_pynew;
dc075750 1014 typeready(&g->ty.ht_type);
d7ab1bab 1015 g->m = m;
1016 g->f = f;
b2687a0a 1017 return ((PyObject *)g);
d7ab1bab 1018}
1019
1020static void gmac_pydealloc(PyObject *me)
1021{
1022 if (GMAC_F(me) & f_freeme)
1023 GM_DESTROY(GMAC_M(me));
1024 Py_DECREF(me->ob_type);
d7ab1bab 1025 PyType_Type.tp_dealloc(me);
1026}
1027
1028static PyObject *gcmget_name(PyObject *me, void *hunoz)
1029 { return (PyString_FromString(GCMAC_CM(me)->name)); }
1030
1031static PyObject *gcmget_keysz(PyObject *me, void *hunoz)
1032 { return (keysz_pywrap(GCMAC_CM(me)->keysz)); }
1033
1034static PyObject *gcmget_tagsz(PyObject *me, void *hunoz)
1035 { return (PyInt_FromLong(GCMAC_CM(me)->hashsz)); }
1036
1037static PyGetSetDef gcmac_pygetset[] = {
1038#define GETSETNAME(op, name) gcm##op##_##name
1039 GET (keysz, "CM.keysz -> acceptable key sizes")
1040 GET (tagsz, "CM.tagsz -> MAC output size")
1041 GET (name, "CM.name -> name of this kind of MAC")
1042#undef GETSETNAME
1043 { 0 }
1044};
1045
1046static PyTypeObject gcmac_pytype_skel = {
6d4db0bf 1047 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 1048 "GCMAC", /* @tp_name@ */
d7ab1bab 1049 sizeof(gchash_pyobj), /* @tp_basicsize@ */
1050 0, /* @tp_itemsize@ */
1051
1052 0, /* @tp_dealloc@ */
1053 0, /* @tp_print@ */
1054 0, /* @tp_getattr@ */
1055 0, /* @tp_setattr@ */
1056 0, /* @tp_compare@ */
1057 0, /* @tp_repr@ */
1058 0, /* @tp_as_number@ */
1059 0, /* @tp_as_sequence@ */
1060 0, /* @tp_as_mapping@ */
1061 0, /* @tp_hash@ */
1062 0, /* @tp_call@ */
1063 0, /* @tp_str@ */
1064 0, /* @tp_getattro@ */
1065 0, /* @tp_setattro@ */
1066 0, /* @tp_as_buffer@ */
1067 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1068 Py_TPFLAGS_BASETYPE,
1069
1070 /* @tp_doc@ */
1071"Message authentication code metametaclass.",
1072
1073 0, /* @tp_traverse@ */
1074 0, /* @tp_clear@ */
1075 0, /* @tp_richcompare@ */
1076 0, /* @tp_weaklistoffset@ */
1077 0, /* @tp_iter@ */
963a6148 1078 0, /* @tp_iternext@ */
d7ab1bab 1079 0, /* @tp_methods@ */
1080 0, /* @tp_members@ */
1081 gcmac_pygetset, /* @tp_getset@ */
1082 0, /* @tp_base@ */
1083 0, /* @tp_dict@ */
1084 0, /* @tp_descr_get@ */
1085 0, /* @tp_descr_set@ */
1086 0, /* @tp_dictoffset@ */
1087 0, /* @tp_init@ */
1088 PyType_GenericAlloc, /* @tp_alloc@ */
1089 abstract_pynew, /* @tp_new@ */
3aa33042 1090 0, /* @tp_free@ */
d7ab1bab 1091 0 /* @tp_is_gc@ */
1092};
1093
1094static PyTypeObject gmac_pytype_skel = {
6d4db0bf 1095 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 1096 "GMAC", /* @tp_name@ */
d7ab1bab 1097 sizeof(gmac_pyobj), /* @tp_basicsize@ */
1098 0, /* @tp_itemsize@ */
1099
1100 gmac_pydealloc, /* @tp_dealloc@ */
1101 0, /* @tp_print@ */
1102 0, /* @tp_getattr@ */
1103 0, /* @tp_setattr@ */
1104 0, /* @tp_compare@ */
1105 0, /* @tp_repr@ */
1106 0, /* @tp_as_number@ */
1107 0, /* @tp_as_sequence@ */
1108 0, /* @tp_as_mapping@ */
1109 0, /* @tp_hash@ */
1110 0, /* @tp_call@ */
1111 0, /* @tp_str@ */
1112 0, /* @tp_getattro@ */
1113 0, /* @tp_setattro@ */
1114 0, /* @tp_as_buffer@ */
1115 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1116 Py_TPFLAGS_BASETYPE,
1117
1118 /* @tp_doc@ */
1119"Message authentication code metaclass, abstract base class.",
1120
1121 0, /* @tp_traverse@ */
1122 0, /* @tp_clear@ */
1123 0, /* @tp_richcompare@ */
1124 0, /* @tp_weaklistoffset@ */
1125 0, /* @tp_iter@ */
963a6148 1126 0, /* @tp_iternext@ */
d7ab1bab 1127 0, /* @tp_methods@ */
1128 0, /* @tp_members@ */
1129 0, /* @tp_getset@ */
1130 0, /* @tp_base@ */
1131 0, /* @tp_dict@ */
1132 0, /* @tp_descr_get@ */
1133 0, /* @tp_descr_set@ */
1134 0, /* @tp_dictoffset@ */
1135 0, /* @tp_init@ */
1136 PyType_GenericAlloc, /* @tp_alloc@ */
1137 abstract_pynew, /* @tp_new@ */
3aa33042 1138 0, /* @tp_free@ */
d7ab1bab 1139 0 /* @tp_is_gc@ */
1140};
1141
1142static PyTypeObject gmhash_pytype_skel = {
6d4db0bf 1143 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 1144 "GMACHash", /* @tp_name@ */
d7ab1bab 1145 sizeof(ghash_pyobj), /* @tp_basicsize@ */
1146 0, /* @tp_itemsize@ */
1147
1148 ghash_pydealloc, /* @tp_dealloc@ */
1149 0, /* @tp_print@ */
1150 0, /* @tp_getattr@ */
1151 0, /* @tp_setattr@ */
1152 0, /* @tp_compare@ */
1153 0, /* @tp_repr@ */
1154 0, /* @tp_as_number@ */
1155 0, /* @tp_as_sequence@ */
1156 0, /* @tp_as_mapping@ */
1157 0, /* @tp_hash@ */
1158 0, /* @tp_call@ */
1159 0, /* @tp_str@ */
1160 0, /* @tp_getattro@ */
1161 0, /* @tp_setattro@ */
1162 0, /* @tp_as_buffer@ */
1163 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1164 Py_TPFLAGS_BASETYPE,
1165
1166 /* @tp_doc@ */
1167"Message authentication code, abstract base class.",
1168
1169 0, /* @tp_traverse@ */
1170 0, /* @tp_clear@ */
1171 0, /* @tp_richcompare@ */
1172 0, /* @tp_weaklistoffset@ */
1173 0, /* @tp_iter@ */
963a6148 1174 0, /* @tp_iternext@ */
d7ab1bab 1175 0, /* @tp_methods@ */
1176 0, /* @tp_members@ */
1177 0, /* @tp_getset@ */
1178 0, /* @tp_base@ */
1179 0, /* @tp_dict@ */
1180 0, /* @tp_descr_get@ */
1181 0, /* @tp_descr_set@ */
1182 0, /* @tp_dictoffset@ */
1183 0, /* @tp_init@ */
1184 PyType_GenericAlloc, /* @tp_alloc@ */
1185 abstract_pynew, /* @tp_new@ */
3aa33042 1186 0, /* @tp_free@ */
d7ab1bab 1187 0 /* @tp_is_gc@ */
1188};
1189
204d480b
MW
1190/*----- Special snowflake for Poly1305 ------------------------------------*/
1191
1192PyTypeObject *poly1305cls_pytype, *poly1305key_pytype, *poly1305hash_pytype;
1193
1194typedef struct poly1305key_pyobj {
1195 PyHeapTypeObject ty;
1196 poly1305_key k;
1197} poly1305key_pyobj;
1198
1199typedef struct poly1305hash_pyobj {
1200 PyObject_HEAD
1201 unsigned f;
1202#define f_mask 1u
1203 poly1305_ctx ctx;
1204} poly1305hash_pyobj;
1205
1206#define P1305_F(o) (((poly1305hash_pyobj *)(o))->f)
1207#define P1305_CTX(o) (&((poly1305hash_pyobj *)(o))->ctx)
1208CONVFUNC(poly1305hash, poly1305_ctx *, P1305_CTX)
1209
1210static PyObject *poly1305hash_pynew(PyTypeObject *ty,
1211 PyObject *arg, PyObject *kw)
1212{
1213 char *kwlist[] = { "mask", 0 };
1214 poly1305key_pyobj *pk = (poly1305key_pyobj *)ty;
1215 poly1305hash_pyobj *ph;
1216 char *m = 0;
6b54260d 1217 Py_ssize_t sz;
204d480b
MW
1218
1219 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|s#:new", kwlist, &m, &sz))
1220 return (0);
1221 if (m && sz != POLY1305_MASKSZ) VALERR("bad mask length");
1222 ph = PyObject_NEW(poly1305hash_pyobj, ty);
1223 ph->f = 0;
1224 if (m) ph->f |= f_mask;
1225 poly1305_macinit(&ph->ctx, &pk->k, m);
1226 Py_INCREF(ty);
1227 return ((PyObject *)ph);
1228end:
1229 return (0);
1230}
1231
1232static PyObject *poly1305key_pynew(PyTypeObject *ty,
1233 PyObject *arg, PyObject *kw)
1234{
1235 char *kwlist[] = { "k", 0 };
1236 poly1305key_pyobj *pk;
1237 char *k;
6b54260d 1238 Py_ssize_t sz;
204d480b
MW
1239
1240 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz))
1241 goto end;
1242 if (keysz(sz, poly1305_keysz) != sz) VALERR("bad key length");
1243
1244 pk = newtype(ty, 0, 0);
1245 pk->ty.ht_name = PyString_FromString("poly1305(keyed)");
1246 pk->ty.ht_type.tp_basicsize = sizeof(poly1305hash_pyobj);
1247 pk->ty.ht_type.tp_name = PyString_AS_STRING(pk->ty.ht_name);
1248 pk->ty.ht_type.tp_base = poly1305hash_pytype;
1249 Py_INCREF(poly1305key_pytype);
1250 pk->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
1251 Py_TPFLAGS_BASETYPE |
1252 Py_TPFLAGS_HEAPTYPE);
1253 pk->ty.ht_type.tp_alloc = PyType_GenericAlloc;
1254 pk->ty.ht_type.tp_free = 0;
1255 pk->ty.ht_type.tp_new = poly1305hash_pynew;
1256 typeready(&pk->ty.ht_type);
1257
1258 poly1305_keyinit(&pk->k, k, sz);
1259 return ((PyObject *)pk);
1260
1261end:
1262 return (0);
1263}
1264
1265static PyObject *poly1305clsget_name(PyObject *me, void *hunoz)
1266 { return (PyString_FromString("poly1305")); }
1267
1268static PyObject *poly1305clsget_keysz(PyObject *me, void *hunoz)
1269 { return (keysz_pywrap(poly1305_keysz)); }
1270
1271static PyObject *poly1305clsget_masksz(PyObject *me, void *hunoz)
1272 { return (PyInt_FromLong(POLY1305_MASKSZ)); }
1273
1274static PyObject *poly1305clsget_tagsz(PyObject *me, void *hunoz)
1275 { return (PyInt_FromLong(POLY1305_TAGSZ)); }
1276
1277static PyObject *polymeth_copy(PyObject *me, PyObject *arg)
1278{
1279 poly1305hash_pyobj *ph;
1280 if (!PyArg_ParseTuple(arg, ":copy")) return (0);
1281 ph = PyObject_NEW(poly1305hash_pyobj, me->ob_type);
1282 poly1305_copy(&ph->ctx, P1305_CTX(me));
1283 Py_INCREF(me->ob_type);
1284 return ((PyObject *)ph);
1285}
1286
1287static PyObject *polymeth_hash(PyObject *me, PyObject *arg)
1288{
1289 char *p;
6b54260d 1290 Py_ssize_t sz;
204d480b
MW
1291 if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0);
1292 poly1305_hash(P1305_CTX(me), p, sz);
1293 RETURN_ME;
1294}
1295
1296#define POLYMETH_HASHU_(n, W, w) \
1297 static PyObject *polymeth_hashu##w(PyObject *me, PyObject *arg) \
1298 { \
1299 uint##n x; \
1300 octet b[SZ_##W]; \
1301 if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \
0c87e818 1302 STORE##W(b, x); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \
204d480b
MW
1303 RETURN_ME; \
1304 end: \
1305 return (0); \
1306 }
1307DOUINTCONV(POLYMETH_HASHU_)
1308
1309#define POLYMETH_HASHBUF_(n, W, w) \
1310 static PyObject *polymeth_hashbuf##w(PyObject *me, PyObject *arg) \
1311 { \
1312 char *p; \
6b54260d 1313 Py_ssize_t sz; \
204d480b
MW
1314 octet b[SZ_##W]; \
1315 if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \
1316 if (sz > MASK##n) TYERR("string too long"); \
0c87e818 1317 STORE##W(b, sz); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \
204d480b
MW
1318 poly1305_hash(P1305_CTX(me), p, sz); \
1319 RETURN_ME; \
1320 end: \
1321 return (0); \
1322 }
1323DOUINTCONV(POLYMETH_HASHBUF_)
1324
1325static PyObject *polymeth_hashstrz(PyObject *me, PyObject *arg)
1326{
1327 char *p;
1328 if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0);
1329 poly1305_hash(P1305_CTX(me), p, strlen(p) + 1);
1330 RETURN_ME;
1331}
1332
1333static PyObject *polymeth_flush(PyObject *me, PyObject *arg)
1334{
1335 if (!PyArg_ParseTuple(arg, ":flush")) return (0);
1336 poly1305_flush(P1305_CTX(me));
1337 RETURN_ME;
1338}
1339
5c17375a
MW
1340static PyObject *polymeth_flushzero(PyObject *me, PyObject *arg)
1341{
1342 if (!PyArg_ParseTuple(arg, ":flushzero")) return (0);
1343 poly1305_flushzero(P1305_CTX(me));
1344 RETURN_ME;
1345}
1346
204d480b
MW
1347static PyObject *polymeth_concat(PyObject *me, PyObject *arg)
1348{
1349 PyObject *pre, *suff;
1350 if (!PyArg_ParseTuple(arg, "OO:concat", &pre, &suff)) return (0);
1351 if (!PyObject_TypeCheck(pre, poly1305hash_pytype) ||
1352 !PyObject_TypeCheck(suff, poly1305hash_pytype))
1353 TYERR("wanted a poly1305hash");
1354 if (me->ob_type != pre->ob_type || me->ob_type != suff->ob_type)
1355 TYERR("key mismatch");
1356 if (P1305_CTX(pre)->nbuf) VALERR("prefix is not block-aligned");
1357 poly1305_concat(P1305_CTX(me), P1305_CTX(pre), P1305_CTX(suff));
1358 RETURN_ME;
1359end:
1360 return (0);
1361}
1362
1363static PyObject *polymeth_done(PyObject *me, PyObject *arg)
1364{
1365 PyObject *rc;
1366 if (!PyArg_ParseTuple(arg, ":done")) return (0);
1367 if (!(P1305_F(me) & f_mask)) VALERR("no mask");
1368 rc = bytestring_pywrap(0, POLY1305_TAGSZ);
1369 poly1305_done(P1305_CTX(me), PyString_AS_STRING(rc));
1370 return (rc);
1371end:
1372 return (0);
1373}
1374
1375static PyGetSetDef poly1305cls_pygetset[] = {
1376#define GETSETNAME(op, name) poly1305cls##op##_##name
1377 GET (keysz, "PC.keysz -> acceptable key sizes")
1378 GET (masksz, "PC.masksz -> mask size")
1379 GET (tagsz, "PC.tagsz -> MAC output size")
1380 GET (name, "PC.name -> name of this kind of MAC")
1381#undef GETSETNAME
1382 { 0 }
1383};
1384
1385static PyMethodDef poly1305hash_pymethods[] = {
1386#define METHNAME(name) polymeth_##name
1387 METH (copy, "P.copy() -> PP")
1388 METH (hash, "P.hash(M)")
1389#define METHU_(n, W, w) METH(hashu##w, "P.hashu" #w "(WORD)")
1390 DOUINTCONV(METHU_)
1391#undef METHU_
1392#define METHBUF_(n, W, w) METH(hashbuf##w, "P.hashbuf" #w "(BYTES)")
1393 DOUINTCONV(METHBUF_)
1394#undef METHBUF_
1395 METH (hashstrz, "P.hashstrz(STRING)")
1396 METH (flush, "P.flush()")
5c17375a 1397 METH (flushzero, "P.flushzero()")
204d480b
MW
1398 METH (concat, "P.concat(PREFIX, SUFFIX)")
1399 METH (done, "P.done() -> TAG")
1400#undef METHNAME
1401 { 0 }
1402};
1403
1404static PyTypeObject poly1305cls_pytype_skel = {
1405 PyObject_HEAD_INIT(0) 0, /* Header */
1406 "Poly1305Class", /* @tp_name@ */
1407 sizeof(PyHeapTypeObject), /* @tp_basicsize@ */
1408 0, /* @tp_itemsize@ */
1409
1410 0, /* @tp_dealloc@ */
1411 0, /* @tp_print@ */
1412 0, /* @tp_getattr@ */
1413 0, /* @tp_setattr@ */
1414 0, /* @tp_compare@ */
1415 0, /* @tp_repr@ */
1416 0, /* @tp_as_number@ */
1417 0, /* @tp_as_sequence@ */
1418 0, /* @tp_as_mapping@ */
1419 0, /* @tp_hash@ */
1420 0, /* @tp_call@ */
1421 0, /* @tp_str@ */
1422 0, /* @tp_getattro@ */
1423 0, /* @tp_setattro@ */
1424 0, /* @tp_as_buffer@ */
1425 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1426 Py_TPFLAGS_BASETYPE,
1427
1428 /* @tp_doc@ */
1429"Poly1305 metametaclass. Best not to ask.",
1430
1431 0, /* @tp_traverse@ */
1432 0, /* @tp_clear@ */
1433 0, /* @tp_richcompare@ */
1434 0, /* @tp_weaklistoffset@ */
1435 0, /* @tp_iter@ */
1436 0, /* @tp_iternext@ */
1437 0, /* @tp_methods@ */
1438 0, /* @tp_members@ */
1439 poly1305cls_pygetset, /* @tp_getset@ */
1440 0, /* @tp_base@ */
1441 0, /* @tp_dict@ */
1442 0, /* @tp_descr_get@ */
1443 0, /* @tp_descr_set@ */
1444 0, /* @tp_dictoffset@ */
1445 0, /* @tp_init@ */
1446 PyType_GenericAlloc, /* @tp_alloc@ */
1447 abstract_pynew, /* @tp_new@ */
1448 0, /* @tp_free@ */
1449 0 /* @tp_is_gc@ */
1450};
1451
1452static PyTypeObject poly1305key_pytype_skel = {
1453 PyObject_HEAD_INIT(0) 0, /* Header */
1454 "poly1305", /* @tp_name@ */
1455 sizeof(poly1305key_pyobj), /* @tp_basicsize@ */
1456 0, /* @tp_itemsize@ */
1457
1458 0, /* @tp_dealloc@ */
1459 0, /* @tp_print@ */
1460 0, /* @tp_getattr@ */
1461 0, /* @tp_setattr@ */
1462 0, /* @tp_compare@ */
1463 0, /* @tp_repr@ */
1464 0, /* @tp_as_number@ */
1465 0, /* @tp_as_sequence@ */
1466 0, /* @tp_as_mapping@ */
1467 0, /* @tp_hash@ */
1468 0, /* @tp_call@ */
1469 0, /* @tp_str@ */
1470 0, /* @tp_getattro@ */
1471 0, /* @tp_setattro@ */
1472 0, /* @tp_as_buffer@ */
1473 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1474 Py_TPFLAGS_BASETYPE,
1475
1476 /* @tp_doc@ */
1477"Poly1305 key.",
1478
1479 0, /* @tp_traverse@ */
1480 0, /* @tp_clear@ */
1481 0, /* @tp_richcompare@ */
1482 0, /* @tp_weaklistoffset@ */
1483 0, /* @tp_iter@ */
1484 0, /* @tp_iternext@ */
1485 0, /* @tp_methods@ */
1486 0, /* @tp_members@ */
1487 0, /* @tp_getset@ */
1488 0, /* @tp_base@ */
1489 0, /* @tp_dict@ */
1490 0, /* @tp_descr_get@ */
1491 0, /* @tp_descr_set@ */
1492 0, /* @tp_dictoffset@ */
1493 0, /* @tp_init@ */
1494 PyType_GenericAlloc, /* @tp_alloc@ */
1495 poly1305key_pynew, /* @tp_new@ */
1496 0, /* @tp_free@ */
1497 0 /* @tp_is_gc@ */
1498};
1499
1500static PyTypeObject poly1305hash_pytype_skel = {
1501 PyObject_HEAD_INIT(0) 0, /* Header */
1502 "Poly1305Hash", /* @tp_name@ */
1503 sizeof(poly1305hash_pyobj), /* @tp_basicsize@ */
1504 0, /* @tp_itemsize@ */
1505
1506 0, /* @tp_dealloc@ */
1507 0, /* @tp_print@ */
1508 0, /* @tp_getattr@ */
1509 0, /* @tp_setattr@ */
1510 0, /* @tp_compare@ */
1511 0, /* @tp_repr@ */
1512 0, /* @tp_as_number@ */
1513 0, /* @tp_as_sequence@ */
1514 0, /* @tp_as_mapping@ */
1515 0, /* @tp_hash@ */
1516 0, /* @tp_call@ */
1517 0, /* @tp_str@ */
1518 0, /* @tp_getattro@ */
1519 0, /* @tp_setattro@ */
1520 0, /* @tp_as_buffer@ */
1521 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1522 Py_TPFLAGS_BASETYPE,
1523
1524 /* @tp_doc@ */
1525"Poly1305 MAC context base class.",
1526
1527 0, /* @tp_traverse@ */
1528 0, /* @tp_clear@ */
1529 0, /* @tp_richcompare@ */
1530 0, /* @tp_weaklistoffset@ */
1531 0, /* @tp_iter@ */
1532 0, /* @tp_iternext@ */
1533 poly1305hash_pymethods, /* @tp_methods@ */
1534 0, /* @tp_members@ */
1535 0, /* @tp_getset@ */
1536 0, /* @tp_base@ */
1537 0, /* @tp_dict@ */
1538 0, /* @tp_descr_get@ */
1539 0, /* @tp_descr_set@ */
1540 0, /* @tp_dictoffset@ */
1541 0, /* @tp_init@ */
1542 PyType_GenericAlloc, /* @tp_alloc@ */
1543 abstract_pynew, /* @tp_new@ */
1544 0, /* @tp_free@ */
1545 0 /* @tp_is_gc@ */
1546};
1547
a75e68c9
MW
1548/*----- Special snowflake for HSalsa and HChaCha --------------------------*/
1549
1550#define DEF_HDANCE(DANCE, HDANCE, dance, hdance) \
1551 static PyObject *meth_##hdance##_prf(PyObject *me, PyObject *arg) \
1552 { \
1553 dance##_ctx dance; \
1554 char *k, *n; \
6b54260d 1555 Py_ssize_t ksz, nsz; \
a75e68c9
MW
1556 PyObject *rc; \
1557 if (!PyArg_ParseTuple(arg, "s#s#:" #hdance "_prf", \
1558 &k, &ksz, &n, &nsz)) \
1559 goto end; \
1560 if (ksz != DANCE##_KEYSZ) VALERR("bad key length"); \
1561 if (nsz != HDANCE##_INSZ) VALERR("bad input length"); \
1562 rc = bytestring_pywrap(0, HSALSA20_OUTSZ); \
1563 dance##_init(&dance, k, ksz, 0); \
1564 hdance##_prf(&dance, n, PyString_AS_STRING(rc)); \
1565 return (rc); \
1566 end: \
1567 return (0); \
1568 }
1569
1570DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa20)
1571DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa2012)
1572DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa208)
1573
1574DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha20)
1575DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha12)
1576DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha8)
204d480b 1577
03ed9abb
MW
1578/*----- Pseudorandom permutations -----------------------------------------*/
1579
1580static PyTypeObject *gcprp_pytype, *gprp_pytype;
1581
1582typedef struct prpinfo {
1583 const char *name;
1584 const octet *keysz;
1585 size_t ctxsz;
1586 size_t blksz;
1587 void (*init)(void *, const void *, size_t);
1588 void (*eblk)(void *, const void *, void *);
1589 void (*dblk)(void *, const void *, void *);
1590} prpinfo;
1591
1592#define PRP_DEF(PRE, pre) \
1593 static void pre##_prpinit(void *ctx, const void *k, size_t ksz) \
1594 { pre##_init(ctx, k, ksz); } \
1595 static void pre##_prpeblk(void *ctx, const void *in, void *out) \
1596 { \
1597 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
1598 pre##_eblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
1599 } \
1600 static void pre##_prpdblk(void *ctx, const void *in, void *out) \
1601 { \
1602 uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \
1603 pre##_dblk(ctx, w, w); BLKC_STORE(PRE, out, w); \
1604 } \
1605 static const prpinfo pre##_prpinfo = { \
1606 #pre, pre##_keysz, sizeof(pre##_ctx), PRE##_BLKSZ, \
1607 pre##_prpinit, pre##_prpeblk, pre##_prpdblk \
1608 };
1609PRPS(PRP_DEF)
1610
1611static const struct prpinfo *const gprptab[] = {
1612#define PRP_ENTRY(PRE, pre) &pre##_prpinfo,
1613 PRPS(PRP_ENTRY)
1614 0
b2687a0a 1615};
03ed9abb
MW
1616
1617typedef struct gcprp_pyobj {
1618 PyHeapTypeObject ty;
1619 const prpinfo *prp;
1620} gcprp_pyobj;
1621#define GCPRP_PRP(o) (((gcprp_pyobj *)(o))->prp)
1622
1623typedef struct gprp_pyobj {
1624 PyObject_HEAD
1625 const prpinfo *prp;
1626} gprp_pyobj;
1627#define GPRP_PRP(o) (((gprp_pyobj *)(o))->prp)
1628#define GPRP_CTX(o) (((gprp_pyobj *)(o)) + 1)
1629
1630typedef struct prp {
1631 const prpinfo *prp;
1632 void *ctx;
1633} prp;
1634
1635static PyObject *gprp_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1636{
1637 char *kwlist[] = { "key", 0 };
1638 char *k;
6b54260d 1639 Py_ssize_t sz;
03ed9abb
MW
1640 const prpinfo *prp = GCPRP_PRP(ty);
1641 PyObject *me;
1642
1643 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz))
1644 goto end;
1645 if (keysz(sz, prp->keysz) != sz) VALERR("bad key length");
1646 me = (PyObject *)ty->tp_alloc(ty, 0);
1647 GPRP_PRP(me) = prp;
1648 prp->init(GPRP_CTX(me), k, sz);
1649 Py_INCREF(me);
1650 return (me);
1651end:
b2687a0a 1652 return (0);
03ed9abb
MW
1653}
1654
1655static void gprp_pydealloc(PyObject *me)
1656 { Py_DECREF(me->ob_type); FREEOBJ(me); }
1657
1658static PyObject *gcprp_pywrap(const prpinfo *prp)
1659{
1660 gcprp_pyobj *g = newtype(gcprp_pytype, 0, prp->name);
1661 g->prp = prp;
24b3d57b
MW
1662 g->ty.ht_type.tp_basicsize = sizeof(gprp_pyobj) + prp->ctxsz;
1663 g->ty.ht_type.tp_base = gprp_pytype;
03ed9abb 1664 Py_INCREF(gprp_pytype);
24b3d57b
MW
1665 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
1666 Py_TPFLAGS_BASETYPE |
1667 Py_TPFLAGS_HEAPTYPE);
1668 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
1669 g->ty.ht_type.tp_free = 0;
1670 g->ty.ht_type.tp_new = gprp_pynew;
dc075750 1671 typeready(&g->ty.ht_type);
03ed9abb
MW
1672 return ((PyObject *)g);
1673}
1674
1675static PyObject *gcpget_name(PyObject *me, void *hunoz)
1676 { return (PyString_FromString(GCPRP_PRP(me)->name)); }
1677static PyObject *gcpget_keysz(PyObject *me, void *hunoz)
1678 { return (keysz_pywrap(GCPRP_PRP(me)->keysz)); }
1679static PyObject *gcpget_blksz(PyObject *me, void *hunoz)
1680 { return (PyInt_FromLong(GCPRP_PRP(me)->blksz)); }
1681
1682static PyObject *gpmeth_encrypt(PyObject *me, PyObject *arg)
1683{
1684 char *p;
6b54260d 1685 Py_ssize_t n;
03ed9abb
MW
1686 PyObject *rc = 0;
1687
1688 if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &n)) goto end;
1689 if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length");
1690 rc = bytestring_pywrap(0, n);
1691 GPRP_PRP(me)->eblk(GPRP_CTX(me), p, PyString_AS_STRING(rc));
1692end:
1693 return (rc);
1694}
1695
1696static PyObject *gpmeth_decrypt(PyObject *me, PyObject *arg)
1697{
1698 char *p;
6b54260d 1699 Py_ssize_t n;
03ed9abb
MW
1700 PyObject *rc = 0;
1701
1702 if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &n)) goto end;
1703 if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length");
1704 rc = bytestring_pywrap(0, n);
1705 GPRP_PRP(me)->dblk(GPRP_CTX(me), p, PyString_AS_STRING(rc));
1706end:
1707 return (rc);
1708}
1709
1710static PyGetSetDef gcprp_pygetset[] = {
1711#define GETSETNAME(op, name) gcp##op##_##name
1712 GET (keysz, "CP.keysz -> acceptable key sizes")
1713 GET (blksz, "CP.blksz -> block size")
1714 GET (name, "CP.name -> name of this kind of PRP")
1715#undef GETSETNAME
1716 { 0 }
1717};
1718
1719static PyMethodDef gprp_pymethods[] = {
1720#define METHNAME(name) gpmeth_##name
1721 METH (encrypt, "P.encrypt(PT) -> CT")
1722 METH (decrypt, "P.decrypt(CT) -> PT")
1723#undef METHNAME
1724 { 0 }
1725};
1726
1727static PyTypeObject gcprp_pytype_skel = {
1728 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 1729 "GCPRP", /* @tp_name@ */
03ed9abb
MW
1730 sizeof(gcprp_pyobj), /* @tp_basicsize@ */
1731 0, /* @tp_itemsize@ */
1732
1733 0, /* @tp_dealloc@ */
1734 0, /* @tp_print@ */
1735 0, /* @tp_getattr@ */
1736 0, /* @tp_setattr@ */
1737 0, /* @tp_compare@ */
1738 0, /* @tp_repr@ */
1739 0, /* @tp_as_number@ */
1740 0, /* @tp_as_sequence@ */
1741 0, /* @tp_as_mapping@ */
1742 0, /* @tp_hash@ */
1743 0, /* @tp_call@ */
1744 0, /* @tp_str@ */
1745 0, /* @tp_getattro@ */
1746 0, /* @tp_setattro@ */
1747 0, /* @tp_as_buffer@ */
1748 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1749 Py_TPFLAGS_BASETYPE,
1750
1751 /* @tp_doc@ */
1752"Pseudorandom permutation metaclass.",
1753
1754 0, /* @tp_traverse@ */
1755 0, /* @tp_clear@ */
1756 0, /* @tp_richcompare@ */
1757 0, /* @tp_weaklistoffset@ */
1758 0, /* @tp_iter@ */
1759 0, /* @tp_iternext@ */
1760 0, /* @tp_methods@ */
1761 0, /* @tp_members@ */
1762 gcprp_pygetset, /* @tp_getset@ */
1763 0, /* @tp_base@ */
1764 0, /* @tp_dict@ */
1765 0, /* @tp_descr_get@ */
1766 0, /* @tp_descr_set@ */
1767 0, /* @tp_dictoffset@ */
1768 0, /* @tp_init@ */
1769 PyType_GenericAlloc, /* @tp_alloc@ */
1770 abstract_pynew, /* @tp_new@ */
1771 0, /* @tp_free@ */
1772 0 /* @tp_is_gc@ */
1773};
1774
1775static PyTypeObject gprp_pytype_skel = {
1776 PyObject_HEAD_INIT(0) 0, /* Header */
c461c9b3 1777 "GPRP", /* @tp_name@ */
03ed9abb
MW
1778 sizeof(gprp_pyobj), /* @tp_basicsize@ */
1779 0, /* @tp_itemsize@ */
1780
1781 gprp_pydealloc, /* @tp_dealloc@ */
1782 0, /* @tp_print@ */
1783 0, /* @tp_getattr@ */
1784 0, /* @tp_setattr@ */
1785 0, /* @tp_compare@ */
1786 0, /* @tp_repr@ */
1787 0, /* @tp_as_number@ */
1788 0, /* @tp_as_sequence@ */
1789 0, /* @tp_as_mapping@ */
1790 0, /* @tp_hash@ */
1791 0, /* @tp_call@ */
1792 0, /* @tp_str@ */
1793 0, /* @tp_getattro@ */
1794 0, /* @tp_setattro@ */
1795 0, /* @tp_as_buffer@ */
1796 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1797 Py_TPFLAGS_BASETYPE,
1798
1799 /* @tp_doc@ */
1800"Pseudorandom permutation, abstract base class.",
1801
1802 0, /* @tp_traverse@ */
1803 0, /* @tp_clear@ */
1804 0, /* @tp_richcompare@ */
1805 0, /* @tp_weaklistoffset@ */
1806 0, /* @tp_iter@ */
1807 0, /* @tp_iternext@ */
1808 gprp_pymethods, /* @tp_methods@ */
1809 0, /* @tp_members@ */
1810 0, /* @tp_getset@ */
1811 0, /* @tp_base@ */
1812 0, /* @tp_dict@ */
1813 0, /* @tp_descr_get@ */
1814 0, /* @tp_descr_set@ */
1815 0, /* @tp_dictoffset@ */
1816 0, /* @tp_init@ */
1817 PyType_GenericAlloc, /* @tp_alloc@ */
1818 abstract_pynew, /* @tp_new@ */
1819 0, /* @tp_free@ */
1820 0 /* @tp_is_gc@ */
1821};
1822
d7ab1bab 1823/*----- Main code ---------------------------------------------------------*/
1824
89157adc
MW
1825static PyMethodDef methods[] = {
1826#define METHNAME(func) meth_##func
1827 METH (_KeySZ_fromdl, "\
1828fromdl(N) -> M: convert integer discrete log field size to work factor")
1829 METH (_KeySZ_fromschnorr, "\
1830fromschnorr(N) -> M: convert Schnorr group order to work factor")
1831 METH (_KeySZ_fromif, "\
1832fromif(N) -> M: convert integer factorization problem size to work factor")
1833 METH (_KeySZ_fromec, "\
1834fromec(N) -> M: convert elliptic curve group order to work factor")
1835 METH (_KeySZ_todl, "\
1836todl(N) -> M: convert work factor to integer discrete log field size")
1837 METH (_KeySZ_toschnorr, "\
1838toschnorr(N) -> M: convert work factor to Schnorr group order")
1839 METH (_KeySZ_toif, "\
1840toif(N) -> M: convert work factor to integer factorization problem size")
1841 METH (_KeySZ_toec, "\
1842toec(N) -> M: convert work factor to elliptic curve group order")
a75e68c9
MW
1843 METH (_KeySZ_toec, "\
1844toec(N) -> M: convert work factor to elliptic curve group order")
1845#define METH_HDANCE(hdance, HDance) METH(hdance##_prf, "\
1846" #hdance "_prf(K, N) -> H: calculate " HDance " hash of N with K")
1847 METH_HDANCE(hsalsa20, "HSalsa20")
1848 METH_HDANCE(hsalsa2012, "HSalsa20/12")
1849 METH_HDANCE(hsalsa208, "HSalsa20/8")
1850 METH_HDANCE(hchacha20, "HChaCha20")
1851 METH_HDANCE(hchacha12, "HChaCha12")
1852 METH_HDANCE(hchacha8, "HChaCha8")
1853#undef METH_DANCE
89157adc
MW
1854#undef METHNAME
1855 { 0 }
1856};
1857
d7ab1bab 1858void algorithms_pyinit(void)
1859{
1860 INITTYPE(keysz, root);
1861 INITTYPE(keyszany, keysz);
1862 INITTYPE(keyszrange, keysz);
1863 INITTYPE(keyszset, keysz);
1864 INITTYPE(gccipher, type);
1865 INITTYPE(gcipher, root);
1866 INITTYPE(gchash, type);
1867 INITTYPE(ghash, root);
1868 INITTYPE(gcmac, type);
1869 INITTYPE(gmac, type);
1870 INITTYPE(gmhash, ghash);
204d480b
MW
1871 INITTYPE(poly1305cls, type);
1872 INITTYPE_META(poly1305key, type, poly1305cls);
1873 INITTYPE(poly1305hash, root);
03ed9abb
MW
1874 INITTYPE(gcprp, type);
1875 INITTYPE(gprp, root);
89157adc 1876 addmethods(methods);
d7ab1bab 1877}
1878
d7ab1bab 1879GEN(gcciphers, cipher)
1880GEN(gchashes, hash)
1881GEN(gcmacs, mac)
03ed9abb
MW
1882#define gcprp prpinfo
1883GEN(gcprps, prp)
d7ab1bab 1884
1885void algorithms_pyinsert(PyObject *mod)
1886{
1887 PyObject *d;
1888 INSERT("KeySZ", keysz_pytype);
1889 INSERT("KeySZAny", keyszany_pytype);
1890 INSERT("KeySZRange", keyszrange_pytype);
1891 INSERT("KeySZSet", keyszset_pytype);
1892 INSERT("GCCipher", gccipher_pytype);
1893 INSERT("GCipher", gcipher_pytype);
1894 INSERT("gcciphers", gcciphers());
1895 INSERT("GCHash", gchash_pytype);
1896 INSERT("GHash", ghash_pytype);
1897 INSERT("gchashes", d = gchashes());
1898 sha_pyobj = PyDict_GetItemString(d, "sha"); Py_INCREF(sha_pyobj);
1899 has160_pyobj = PyDict_GetItemString(d, "has160"); Py_INCREF(has160_pyobj);
1900 INSERT("GCMAC", gcmac_pytype);
1901 INSERT("GMAC", gmac_pytype);
1902 INSERT("GMACHash", gmhash_pytype);
1903 INSERT("gcmacs", gcmacs());
204d480b
MW
1904 INSERT("Poly1305Class", poly1305cls_pytype);
1905 INSERT("poly1305", poly1305key_pytype);
1906 INSERT("Poly1305Hash", poly1305hash_pytype);
03ed9abb
MW
1907 INSERT("GCPRP", gcprp_pytype);
1908 INSERT("GPRP", gprp_pytype);
1909 INSERT("gcprps", gcprps());
d7ab1bab 1910}
1911
1912/*----- That's all, folks -------------------------------------------------*/