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