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