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" | |
6640e060 | 30 | PUBLIC_SYMBOLS; |
d7ab1bab | 31 | #include "algorithms.h" |
6640e060 | 32 | PRIVATE_SYMBOLS; |
d7ab1bab | 33 | |
34 | /*----- Key sizes ---------------------------------------------------------*/ | |
35 | ||
36 | PyTypeObject *keysz_pytype; | |
37 | PyTypeObject *keyszany_pytype, *keyszrange_pytype, *keyszset_pytype; | |
38 | PyObject *sha_pyobj, *has160_pyobj; | |
39 | ||
cfe23cf8 MW |
40 | #ifndef KSZ_OPMASK |
41 | # define KSZ_OPMASK 0x1f | |
42 | #endif | |
43 | ||
44 | #ifndef KSZ_16BIT | |
45 | # define KSZ_16BIT 0x20 | |
46 | #endif | |
47 | ||
d7ab1bab | 48 | PyObject *keysz_pywrap(const octet *k) |
49 | { | |
cfe23cf8 MW |
50 | unsigned op = *k++; |
51 | #define ARG(i) (op&KSZ_16BIT ? LOAD16(k + 2*(i)) : k[i]) | |
52 | switch (op&KSZ_OPMASK) { | |
d7ab1bab | 53 | case KSZ_ANY: { |
54 | keysz_pyobj *o = PyObject_New(keysz_pyobj, keyszany_pytype); | |
cfe23cf8 | 55 | o->dfl = ARG(0); |
d7ab1bab | 56 | return ((PyObject *)o); |
57 | } break; | |
58 | case KSZ_RANGE: { | |
59 | keyszrange_pyobj *o = | |
b2687a0a | 60 | PyObject_New(keyszrange_pyobj, keyszrange_pytype); |
cfe23cf8 MW |
61 | o->dfl = ARG(0); |
62 | o->min = ARG(1); | |
63 | o->max = ARG(2); | |
64 | o->mod = ARG(3); | |
d7ab1bab | 65 | if (!o->mod) o->mod = 1; |
66 | return ((PyObject *)o); | |
67 | } break; | |
68 | case KSZ_SET: { | |
69 | keyszset_pyobj *o = | |
b2687a0a | 70 | PyObject_New(keyszset_pyobj, keyszset_pytype); |
d7ab1bab | 71 | int i, n; |
cfe23cf8 MW |
72 | o->dfl = ARG(0); |
73 | for (i = 0; ARG(i); i++) ; | |
d7ab1bab | 74 | n = i; o->set = PyTuple_New(n); |
75 | for (i = 0; i < n; i++) | |
cfe23cf8 | 76 | PyTuple_SET_ITEM(o->set, i, PyInt_FromLong(ARG(i))); |
d7ab1bab | 77 | return ((PyObject *)o); |
78 | } break; | |
79 | default: | |
80 | abort(); | |
81 | } | |
cfe23cf8 | 82 | #undef ARG |
d7ab1bab | 83 | } |
84 | ||
85 | static PyObject *keyszany_pynew(PyTypeObject *ty, | |
86 | PyObject *arg, PyObject *kw) | |
87 | { | |
827f89d7 | 88 | static const char *const kwlist[] = { "default", 0 }; |
d7ab1bab | 89 | int dfl; |
90 | keysz_pyobj *o; | |
91 | ||
827f89d7 | 92 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "i:new", KWLIST, &dfl)) |
d7ab1bab | 93 | goto end; |
94 | if (dfl < 0) VALERR("key size cannot be negative"); | |
95 | o = (keysz_pyobj *)ty->tp_alloc(ty, 0); | |
96 | o->dfl = dfl; | |
97 | return ((PyObject *)o); | |
98 | end: | |
99 | return (0); | |
100 | } | |
101 | ||
102 | static PyObject *keyszrange_pynew(PyTypeObject *ty, | |
103 | PyObject *arg, PyObject *kw) | |
104 | { | |
827f89d7 | 105 | static const char *const kwlist[] = { "default", "min", "max", "mod", 0 }; |
d7ab1bab | 106 | int dfl, min = 0, max = 0, mod = 1; |
107 | keyszrange_pyobj *o; | |
108 | ||
827f89d7 | 109 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|iii:new", KWLIST, |
d7ab1bab | 110 | &dfl, &min, &max, &mod)) |
111 | goto end; | |
838ab173 MW |
112 | if (dfl < 0 || min < 0) VALERR("key size cannot be negative"); |
113 | if (min > dfl || (max && dfl > max)) VALERR("bad key size bounds"); | |
114 | if (mod <= 0 || dfl%mod || min%mod || max%mod) | |
d7ab1bab | 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); | |
122 | end: | |
123 | return (0); | |
124 | } | |
125 | ||
126 | static PyObject *keyszset_pynew(PyTypeObject *ty, | |
127 | PyObject *arg, PyObject *kw) | |
128 | { | |
827f89d7 | 129 | static const char *const kwlist[] = { "default", "set", 0 }; |
d7ab1bab | 130 | int dfl, i, n, xx; |
131 | PyObject *set = 0; | |
132 | PyObject *x = 0, *l = 0; | |
133 | keyszset_pyobj *o = 0; | |
134 | ||
827f89d7 | 135 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|O:new", KWLIST, &dfl, &set)) |
d7ab1bab | 136 | goto end; |
137 | if (!set) set = PyTuple_New(0); | |
138 | else Py_INCREF(set); | |
139 | if (!PySequence_Check(set)) TYERR("want a sequence"); | |
4281a7ee MW |
140 | n = PySequence_Size(set); if (n < 0) goto end; |
141 | l = PyList_New(0); if (!l) goto end; | |
d7ab1bab | 142 | if (dfl < 0) VALERR("key size cannot be negative"); |
143 | x = PyInt_FromLong(dfl); | |
144 | PyList_Append(l, x); | |
145 | Py_DECREF(x); | |
146 | x = 0; | |
147 | for (i = 0; i < n; i++) { | |
148 | if ((x = PySequence_GetItem(set, i)) == 0) goto end; | |
149 | xx = PyInt_AsLong(x); | |
150 | if (PyErr_Occurred()) goto end; | |
151 | if (xx == dfl) continue; | |
152 | if (xx < 0) VALERR("key size cannot be negative"); | |
153 | PyList_Append(l, x); | |
154 | Py_DECREF(x); | |
b2687a0a | 155 | x = 0; |
d7ab1bab | 156 | } |
157 | Py_DECREF(set); | |
158 | if ((set = PySequence_Tuple(l)) == 0) goto end; | |
159 | o = (keyszset_pyobj *)ty->tp_alloc(ty, 0); | |
160 | o->dfl = dfl; | |
161 | o->set = set; | |
162 | Py_INCREF(set); | |
163 | end: | |
164 | Py_XDECREF(set); | |
165 | Py_XDECREF(l); | |
166 | Py_XDECREF(x); | |
167 | return ((PyObject *)o); | |
168 | } | |
169 | ||
170 | static PyObject *kaget_min(PyObject *me, void *hunoz) | |
171 | { return (PyInt_FromLong(0)); } | |
172 | #define kaget_max kaget_min | |
173 | ||
174 | static PyObject *ksget_min(PyObject *me, void *hunoz) | |
175 | { | |
176 | PyObject *set = ((keyszset_pyobj *)me)->set; | |
177 | int i, n, y, x = -1; | |
178 | n = PyTuple_Size(set); | |
179 | for (i = 0; i < n; i++) { | |
180 | y = PyInt_AsLong(PyTuple_GetItem(set, i)); | |
181 | if (x == -1 || y < x) x = y; | |
182 | } | |
183 | return (PyInt_FromLong(x)); | |
184 | } | |
185 | ||
186 | static PyObject *ksget_max(PyObject *me, void *hunoz) | |
187 | { | |
188 | PyObject *set = ((keyszset_pyobj *)me)->set; | |
189 | int i, n, y, x = -1; | |
190 | n = PyTuple_Size(set); | |
191 | for (i = 0; i < n; i++) { | |
192 | y = PyInt_AsLong(PyTuple_GetItem(set, i)); | |
193 | if (y > x) x = y; | |
194 | } | |
195 | return (PyInt_FromLong(x)); | |
196 | } | |
197 | ||
198 | static PyMemberDef keysz_pymembers[] = { | |
199 | #define MEMBERSTRUCT keysz_pyobj | |
200 | #define default dfl /* ugh! */ | |
201 | MEMBER(default, T_INT, READONLY, "KSZ.default -> default key size") | |
202 | #undef default | |
203 | #undef MEMBERSTRUCT | |
204 | { 0 } | |
205 | }; | |
206 | ||
207 | static PyGetSetDef keyszany_pygetset[] = { | |
208 | #define GETSETNAME(op, name) ka##op##_##name | |
209 | GET (min, "KSZ.min -> smallest allowed key size") | |
d050e0fa | 210 | GET (max, "KSZ.max -> largest allowed key size") |
d7ab1bab | 211 | #undef GETSETNAME |
212 | { 0 } | |
213 | }; | |
214 | ||
215 | static PyMemberDef keyszrange_pymembers[] = { | |
216 | #define MEMBERSTRUCT keyszrange_pyobj | |
217 | MEMBER(min, T_INT, READONLY, "KSZ.min -> smallest allowed key size") | |
d050e0fa | 218 | MEMBER(max, T_INT, READONLY, "KSZ.max -> largest allowed key size") |
d7ab1bab | 219 | MEMBER(mod, T_INT, READONLY, |
220 | "KSZ.mod -> key size must be a multiple of this") | |
221 | #undef MEMBERSTRUCT | |
222 | { 0 } | |
223 | }; | |
224 | ||
225 | static PyGetSetDef keyszset_pygetset[] = { | |
226 | #define GETSETNAME(op, name) ks##op##_##name | |
227 | GET (min, "KSZ.min -> smallest allowed key size") | |
d050e0fa | 228 | GET (max, "KSZ.max -> largest allowed key size") |
d7ab1bab | 229 | #undef GETSETNAME |
230 | { 0 } | |
231 | }; | |
232 | ||
233 | static PyMemberDef keyszset_pymembers[] = { | |
234 | #define MEMBERSTRUCT keyszset_pyobj | |
235 | MEMBER(set, T_OBJECT, READONLY, "KSZ.set -> allowed key sizes") | |
236 | #undef MEMBERSTRUCT | |
237 | { 0 } | |
238 | }; | |
239 | ||
240 | static PyTypeObject keysz_pytype_skel = { | |
6d4db0bf | 241 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 242 | "KeySZ", /* @tp_name@ */ |
d7ab1bab | 243 | sizeof(keysz_pyobj), /* @tp_basicsize@ */ |
244 | 0, /* @tp_itemsize@ */ | |
245 | ||
3aa33042 | 246 | 0, /* @tp_dealloc@ */ |
d7ab1bab | 247 | 0, /* @tp_print@ */ |
248 | 0, /* @tp_getattr@ */ | |
249 | 0, /* @tp_setattr@ */ | |
250 | 0, /* @tp_compare@ */ | |
251 | 0, /* @tp_repr@ */ | |
252 | 0, /* @tp_as_number@ */ | |
253 | 0, /* @tp_as_sequence@ */ | |
254 | 0, /* @tp_as_mapping@ */ | |
255 | 0, /* @tp_hash@ */ | |
256 | 0, /* @tp_call@ */ | |
257 | 0, /* @tp_str@ */ | |
258 | 0, /* @tp_getattro@ */ | |
259 | 0, /* @tp_setattro@ */ | |
260 | 0, /* @tp_as_buffer@ */ | |
261 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
262 | Py_TPFLAGS_BASETYPE, | |
263 | ||
264 | /* @tp_doc@ */ | |
06cd26e8 | 265 | "Key size constraints. Abstract.", |
d7ab1bab | 266 | |
267 | 0, /* @tp_traverse@ */ | |
268 | 0, /* @tp_clear@ */ | |
269 | 0, /* @tp_richcompare@ */ | |
270 | 0, /* @tp_weaklistoffset@ */ | |
271 | 0, /* @tp_iter@ */ | |
963a6148 | 272 | 0, /* @tp_iternext@ */ |
d7ab1bab | 273 | 0, /* @tp_methods@ */ |
274 | keysz_pymembers, /* @tp_members@ */ | |
275 | 0, /* @tp_getset@ */ | |
276 | 0, /* @tp_base@ */ | |
277 | 0, /* @tp_dict@ */ | |
278 | 0, /* @tp_descr_get@ */ | |
279 | 0, /* @tp_descr_set@ */ | |
280 | 0, /* @tp_dictoffset@ */ | |
281 | 0, /* @tp_init@ */ | |
282 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
283 | abstract_pynew, /* @tp_new@ */ | |
3aa33042 | 284 | 0, /* @tp_free@ */ |
d7ab1bab | 285 | 0 /* @tp_is_gc@ */ |
286 | }; | |
287 | ||
288 | static PyTypeObject keyszany_pytype_skel = { | |
6d4db0bf | 289 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 290 | "KeySZAny", /* @tp_name@ */ |
d7ab1bab | 291 | sizeof(keysz_pyobj), /* @tp_basicsize@ */ |
292 | 0, /* @tp_itemsize@ */ | |
293 | ||
3aa33042 | 294 | 0, /* @tp_dealloc@ */ |
d7ab1bab | 295 | 0, /* @tp_print@ */ |
296 | 0, /* @tp_getattr@ */ | |
297 | 0, /* @tp_setattr@ */ | |
298 | 0, /* @tp_compare@ */ | |
299 | 0, /* @tp_repr@ */ | |
300 | 0, /* @tp_as_number@ */ | |
301 | 0, /* @tp_as_sequence@ */ | |
302 | 0, /* @tp_as_mapping@ */ | |
303 | 0, /* @tp_hash@ */ | |
304 | 0, /* @tp_call@ */ | |
305 | 0, /* @tp_str@ */ | |
306 | 0, /* @tp_getattro@ */ | |
307 | 0, /* @tp_setattro@ */ | |
308 | 0, /* @tp_as_buffer@ */ | |
309 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
310 | Py_TPFLAGS_BASETYPE, | |
311 | ||
312 | /* @tp_doc@ */ | |
06cd26e8 MW |
313 | "KeySZAny(DEFAULT)\n\ |
314 | Key size constraints. This object imposes no constraints on size.", | |
d7ab1bab | 315 | |
316 | 0, /* @tp_traverse@ */ | |
317 | 0, /* @tp_clear@ */ | |
318 | 0, /* @tp_richcompare@ */ | |
319 | 0, /* @tp_weaklistoffset@ */ | |
320 | 0, /* @tp_iter@ */ | |
963a6148 | 321 | 0, /* @tp_iternext@ */ |
d7ab1bab | 322 | 0, /* @tp_methods@ */ |
323 | 0, /* @tp_members@ */ | |
324 | keyszany_pygetset, /* @tp_getset@ */ | |
325 | 0, /* @tp_base@ */ | |
326 | 0, /* @tp_dict@ */ | |
327 | 0, /* @tp_descr_get@ */ | |
328 | 0, /* @tp_descr_set@ */ | |
329 | 0, /* @tp_dictoffset@ */ | |
330 | 0, /* @tp_init@ */ | |
331 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
332 | keyszany_pynew, /* @tp_new@ */ | |
3aa33042 | 333 | 0, /* @tp_free@ */ |
d7ab1bab | 334 | 0 /* @tp_is_gc@ */ |
335 | }; | |
336 | ||
337 | static PyTypeObject keyszrange_pytype_skel = { | |
6d4db0bf | 338 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 339 | "KeySZRange", /* @tp_name@ */ |
d7ab1bab | 340 | sizeof(keyszrange_pyobj), /* @tp_basicsize@ */ |
341 | 0, /* @tp_itemsize@ */ | |
342 | ||
3aa33042 | 343 | 0, /* @tp_dealloc@ */ |
d7ab1bab | 344 | 0, /* @tp_print@ */ |
345 | 0, /* @tp_getattr@ */ | |
346 | 0, /* @tp_setattr@ */ | |
347 | 0, /* @tp_compare@ */ | |
348 | 0, /* @tp_repr@ */ | |
349 | 0, /* @tp_as_number@ */ | |
350 | 0, /* @tp_as_sequence@ */ | |
351 | 0, /* @tp_as_mapping@ */ | |
352 | 0, /* @tp_hash@ */ | |
353 | 0, /* @tp_call@ */ | |
354 | 0, /* @tp_str@ */ | |
355 | 0, /* @tp_getattro@ */ | |
356 | 0, /* @tp_setattro@ */ | |
357 | 0, /* @tp_as_buffer@ */ | |
358 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
359 | Py_TPFLAGS_BASETYPE, | |
360 | ||
361 | /* @tp_doc@ */ | |
06cd26e8 MW |
362 | "KeySZRange(DEFAULT, [min = 0], [max = 0], [mod = 1])\n\ |
363 | Key size constraints. Key size must be between MIN and MAX inclusive,\n\ | |
364 | and be a multiple of MOD.", | |
d7ab1bab | 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 | ||
387 | static 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@ */ | |
06cd26e8 MW |
412 | "KeySZSet(DEFAULT, SEQ)\n\ |
413 | Key size constraints. Key size must be DEFAULT or one in SEQ.", | |
d7ab1bab | 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 | } | |
444 | KSZCONVOP(fromdl) | |
445 | KSZCONVOP(fromschnorr) | |
446 | KSZCONVOP(fromif) | |
447 | KSZCONVOP(fromec) | |
448 | KSZCONVOP(todl) | |
449 | KSZCONVOP(toschnorr) | |
450 | KSZCONVOP(toif) | |
451 | KSZCONVOP(toec) | |
452 | #undef KSZCONVOP | |
453 | ||
d7ab1bab | 454 | /*----- Symmetric encryption ----------------------------------------------*/ |
455 | ||
456 | PyTypeObject *gccipher_pytype, *gcipher_pytype; | |
457 | ||
458 | CONVFUNC(gccipher, gccipher *, GCCIPHER_CC) | |
459 | CONVFUNC(gcipher, gcipher *, GCIPHER_C) | |
460 | ||
c41d0371 | 461 | PyObject *gcipher_pywrap(PyObject *cobj, gcipher *c) |
d7ab1bab | 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; | |
b2687a0a | 468 | return ((PyObject *)g); |
d7ab1bab | 469 | } |
470 | ||
471 | static PyObject *gcipher_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
472 | { | |
827f89d7 | 473 | static const char *const kwlist[] = { "k", 0 }; |
d7ab1bab | 474 | char *k; |
6b54260d | 475 | Py_ssize_t sz; |
d7ab1bab | 476 | |
827f89d7 | 477 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &k, &sz)) |
d7ab1bab | 478 | goto end; |
479 | if (keysz(sz, GCCIPHER_CC(ty)->keysz) != sz) VALERR("bad key length"); | |
480 | return (gcipher_pywrap((PyObject *)ty, | |
c41d0371 | 481 | GC_INIT(GCCIPHER_CC(ty), k, sz))); |
d7ab1bab | 482 | end: |
b2687a0a | 483 | return (0); |
d7ab1bab | 484 | } |
485 | ||
486 | PyObject *gccipher_pywrap(gccipher *cc) | |
487 | { | |
df9f8366 | 488 | gccipher_pyobj *g = newtype(gccipher_pytype, 0, cc->name); |
d7ab1bab | 489 | g->cc = cc; |
24b3d57b MW |
490 | g->ty.ht_type.tp_basicsize = sizeof(gcipher_pyobj); |
491 | g->ty.ht_type.tp_base = gcipher_pytype; | |
d7ab1bab | 492 | Py_INCREF(gcipher_pytype); |
24b3d57b MW |
493 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
494 | Py_TPFLAGS_BASETYPE | | |
495 | Py_TPFLAGS_HEAPTYPE); | |
496 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; | |
497 | g->ty.ht_type.tp_free = 0; | |
498 | g->ty.ht_type.tp_new = gcipher_pynew; | |
dc075750 | 499 | typeready(&g->ty.ht_type); |
d7ab1bab | 500 | return ((PyObject *)g); |
501 | } | |
502 | ||
503 | static void gcipher_pydealloc(PyObject *me) | |
504 | { | |
c41d0371 | 505 | GC_DESTROY(GCIPHER_C(me)); |
d7ab1bab | 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 | ||
bebf03ab MW |
714 | /*----- Authenticated encryption ------------------------------------------*/ |
715 | ||
716 | PyTypeObject *gcaead_pytype, *gaeadkey_pytype; | |
717 | PyTypeObject *gcaeadaad_pytype, *gaeadaad_pytype; | |
718 | PyTypeObject *gcaeadenc_pytype, *gaeadenc_pytype; | |
719 | PyTypeObject *gcaeaddec_pytype, *gaeaddec_pytype; | |
720 | ||
721 | CONVFUNC(gcaead, gcaead *, GCAEAD_AEC) | |
722 | CONVFUNC(gaeadkey, gaead_key *, GAEADKEY_K) | |
723 | ||
724 | PyObject *gaeadkey_pywrap(PyObject *cobj, gaead_key *k) | |
725 | { | |
726 | gaeadkey_pyobj *gk; | |
727 | ||
728 | if (!cobj) cobj = gcaead_pywrap((/*unconst*/ gcaead *)GAEAD_CLASS(k)); | |
729 | else Py_INCREF(cobj); | |
730 | gk = PyObject_NEW(gaeadkey_pyobj, (PyTypeObject *)cobj); | |
731 | gk->k = k; | |
732 | return ((PyObject *)gk); | |
733 | } | |
734 | ||
735 | static PyObject *gaeadkey_pynew(PyTypeObject *ty, | |
736 | PyObject *arg, PyObject *kw) | |
737 | { | |
738 | static const char *const kwlist[] = { "k", 0 }; | |
739 | char *k; | |
740 | Py_ssize_t sz; | |
741 | ||
742 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &k, &sz)) | |
743 | goto end; | |
744 | if (keysz(sz, GCAEAD_AEC(ty)->keysz) != sz) VALERR("bad key length"); | |
745 | return (gaeadkey_pywrap((PyObject *)ty, | |
746 | GAEAD_KEY(GCAEAD_AEC(ty), k, sz))); | |
747 | end: | |
748 | return (0); | |
749 | } | |
750 | ||
751 | PyObject *gcaead_pywrap(gcaead *aec) | |
752 | { | |
753 | gcaead_pyobj *gck; | |
754 | gcaeadaad_pyobj *gca; | |
755 | gcaeadenc_pyobj *gce; | |
756 | gcaeaddec_pyobj *gcd; | |
757 | ||
758 | #define MKTYPE(obj, thing, newfn, namefmt) do { \ | |
759 | (obj) = newtype(gcaead_pytype, 0, 0); \ | |
760 | (obj)->ty.ht_name = PyString_FromFormat(namefmt, aec->name); \ | |
761 | (obj)->ty.ht_type.tp_name = PyString_AS_STRING((obj)->ty.ht_name); \ | |
762 | (obj)->ty.ht_type.tp_basicsize = sizeof(gaead##thing##_pyobj); \ | |
763 | (obj)->ty.ht_type.tp_base = gaead##thing##_pytype; \ | |
764 | Py_INCREF(gaead##thing##_pytype); \ | |
765 | (obj)->ty.ht_type.tp_flags = \ | |
766 | (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE); \ | |
767 | (obj)->ty.ht_type.tp_alloc = PyType_GenericAlloc; \ | |
768 | (obj)->ty.ht_type.tp_free = 0; \ | |
769 | (obj)->ty.ht_type.tp_new = newfn; \ | |
770 | typeready(&(obj)->ty.ht_type); \ | |
771 | } while (0) | |
772 | ||
773 | MKTYPE(gck, key, gaeadkey_pynew, "%s(key)"); | |
774 | MKTYPE(gca, aad, abstract_pynew, "%s(aad-hash)"); | |
775 | MKTYPE(gce, enc, abstract_pynew, "%s(encrypt)"); | |
776 | MKTYPE(gcd, dec, abstract_pynew, "%s(decrypt)"); | |
777 | ||
778 | #undef MKTYPE | |
779 | ||
780 | gck->aec = aec; gck->aad = gca; gck->enc = gce; gck->dec = gcd; | |
781 | gca->key = gce->key = gcd->key = gck; | |
782 | return ((PyObject *)gck); | |
783 | } | |
784 | ||
785 | static void gaeadkey_pydealloc(PyObject *me) | |
786 | { GAEAD_DESTROY(GAEADKEY_K(me)); Py_DECREF(me->ob_type); FREEOBJ(me); } | |
787 | ||
788 | static PyObject *gcaeget_name(PyObject *me, void *hunoz) | |
789 | { return (PyString_FromString(GCAEAD_AEC(me)->name)); } | |
790 | ||
791 | static PyObject *gcaeget_keysz(PyObject *me, void *hunoz) | |
792 | { return (keysz_pywrap(GCAEAD_AEC(me)->keysz)); } | |
793 | ||
794 | static PyObject *gcaeget_noncesz(PyObject *me, void *hunoz) | |
795 | { return (keysz_pywrap(GCAEAD_AEC(me)->noncesz)); } | |
796 | ||
797 | static PyObject *gcaeget_tagsz(PyObject *me, void *hunoz) | |
798 | { return (keysz_pywrap(GCAEAD_AEC(me)->tagsz)); } | |
799 | ||
800 | static PyObject *gcaeget_blksz(PyObject *me, void *hunoz) | |
801 | { return (PyInt_FromLong(GCAEAD_AEC(me)->blksz)); } | |
802 | ||
803 | static PyObject *gcaeget_bufsz(PyObject *me, void *hunoz) | |
804 | { return (PyInt_FromLong(GCAEAD_AEC(me)->bufsz)); } | |
805 | ||
806 | static PyObject *gcaeget_ohd(PyObject *me, void *hunoz) | |
807 | { return (PyInt_FromLong(GCAEAD_AEC(me)->ohd)); } | |
808 | ||
809 | static PyObject *gcaeget_flags(PyObject *me, void *hunoz) | |
810 | { return (PyInt_FromLong(GCAEAD_AEC(me)->f)); } | |
811 | ||
812 | static PyGetSetDef gcaead_pygetset[] = { | |
813 | #define GETSETNAME(op, name) gcae##op##_##name | |
814 | GET (keysz, "AEC.keysz -> acceptable key sizes") | |
815 | GET (noncesz, "AEC.noncesz -> acceptable nonce sizes") | |
816 | GET (tagsz, "AEC.tagsz -> acceptable tag sizes") | |
817 | GET (blksz, "AEC.blksz -> block size, or zero") | |
818 | GET (bufsz, "AEC.bufsz -> amount of data buffered internally") | |
819 | GET (ohd, "AEC.ohd -> maximum encryption overhead") | |
820 | GET (name, "AEC.name -> name of this kind of AEAD scheme") | |
821 | GET (flags, "AEC.flags -> mask of `AEADF_...' flags") | |
822 | #undef GETSETNAME | |
823 | { 0 } | |
824 | }; | |
825 | ||
826 | static PyObject *gaekmeth_aad(PyObject *me, PyObject *arg) | |
827 | { | |
828 | const gaead_key *k = GAEADKEY_K(me); | |
829 | PyObject *rc = 0; | |
830 | ||
831 | if (!PyArg_ParseTuple(arg, ":aad")) return (0); | |
832 | if (k->ops->c->f&AEADF_AADNDEP) | |
833 | VALERR("aad must be associated with enc/dec op"); | |
834 | rc = gaeadaad_pywrap((PyObject *)GCAEAD_AAD(me->ob_type), | |
835 | GAEAD_AAD(k), 0, 0); | |
836 | end: | |
837 | return (rc); | |
838 | } | |
839 | ||
840 | static int check_aead_encdec(const gcaead *aec, unsigned *f_out, size_t nsz, | |
841 | PyObject *hszobj, size_t *hsz_out, | |
842 | PyObject *mszobj, size_t *msz_out, | |
843 | PyObject *tszobj, size_t *tsz_out) | |
844 | { | |
845 | unsigned f = 0, miss; | |
846 | int rc = -1; | |
847 | ||
848 | if (hszobj != Py_None) | |
849 | { f |= AEADF_PCHSZ; if (!convszt(hszobj, hsz_out)) goto end; } | |
850 | if (mszobj != Py_None) | |
851 | { f |= AEADF_PCMSZ; if (!convszt(mszobj, msz_out)) goto end; } | |
852 | if (tszobj != Py_None) | |
853 | { f |= AEADF_PCTSZ; if (!convszt(tszobj, tsz_out)) goto end; } | |
854 | miss = aec->f&~f; | |
855 | if (miss&AEADF_PCHSZ) VALERR("header length precommitment required"); | |
856 | if (miss&AEADF_PCMSZ) VALERR("message length precommitment required"); | |
857 | if (miss&AEADF_PCTSZ) VALERR("tag length precommitment required"); | |
858 | if (keysz(nsz, aec->noncesz) != nsz) VALERR("bad nonce length"); | |
859 | if (tszobj != Py_None && keysz(*tsz_out, aec->tagsz) != *tsz_out) | |
860 | VALERR("bad tag length"); | |
861 | *f_out = f | aec->f; rc = 0; | |
862 | end: | |
863 | return (rc); | |
864 | } | |
865 | ||
866 | static PyObject *gaekmeth_enc(PyObject *me, PyObject *arg, PyObject *kw) | |
867 | { | |
868 | static const char *const kwlist[] = { "nonce", "hsz", "msz", "tsz", 0 }; | |
869 | const gaead_key *k = GAEADKEY_K(me); | |
870 | gaead_enc *e; | |
871 | PyObject *rc = 0; | |
872 | char *n; Py_ssize_t nsz; | |
873 | PyObject *hszobj = Py_None, *mszobj = Py_None, *tszobj = Py_None; | |
874 | size_t hsz = 0, msz = 0, tsz = 0; | |
875 | unsigned f; | |
876 | ||
877 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|OOO:enc", KWLIST, | |
878 | &n, &nsz, &hszobj, &mszobj, &tszobj)) | |
879 | goto end; | |
880 | if (check_aead_encdec(k->ops->c, &f, nsz, | |
881 | hszobj, &hsz, mszobj, &msz, tszobj, &tsz)) | |
882 | goto end; | |
883 | e = GAEAD_ENC(GAEADKEY_K(me), n, nsz, hsz, msz, tsz); | |
884 | if (!e) VALERR("bad aead parameter combination"); | |
885 | rc = gaeadenc_pywrap((PyObject *)GCAEAD_ENC(me->ob_type), | |
886 | e, f, hsz, msz, tsz); | |
887 | end: | |
888 | return (rc); | |
889 | } | |
890 | ||
891 | static PyObject *gaekmeth_dec(PyObject *me, PyObject *arg, PyObject *kw) | |
892 | { | |
893 | static const char *const kwlist[] = { "nonce", "hsz", "csz", "tsz", 0 }; | |
894 | const gaead_key *k = GAEADKEY_K(me); | |
895 | gaead_dec *d; | |
896 | PyObject *rc = 0; | |
897 | char *n; Py_ssize_t nsz; | |
898 | PyObject *hszobj = Py_None, *cszobj = Py_None, *tszobj = Py_None; | |
899 | size_t hsz = 0, csz = 0, tsz = 0; | |
900 | unsigned f; | |
901 | ||
902 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|OOO:dec", KWLIST, | |
903 | &n, &nsz, &hszobj, &cszobj, &tszobj)) | |
904 | goto end; | |
905 | if (check_aead_encdec(k->ops->c, &f, nsz, | |
906 | hszobj, &hsz, cszobj, &csz, tszobj, &tsz)) | |
907 | goto end; | |
908 | d = GAEAD_DEC(GAEADKEY_K(me), n, nsz, hsz, csz, tsz); | |
909 | if (!d) VALERR("bad aead parameter combination"); | |
910 | rc = gaeaddec_pywrap((PyObject *)GCAEAD_DEC(me->ob_type), | |
911 | d, f, hsz, csz, tsz); | |
912 | end: | |
913 | return (rc); | |
914 | } | |
915 | ||
916 | static PyMethodDef gaeadkey_pymethods[] = { | |
917 | #define METHNAME(name) gaekmeth_##name | |
918 | METH (aad, "KEY.aad() -> AAD") | |
919 | KWMETH(enc, "KEY.enc(NONCE, [hsz], [msz], [tsz]) -> ENC") | |
920 | KWMETH(dec, "KEY.dec(NONCE, [hsz], [csz], [tsz]) -> DEC") | |
921 | #undef METHNAME | |
922 | { 0 } | |
923 | }; | |
924 | ||
925 | PyObject *gaeadaad_pywrap(PyObject *cobj, gaead_aad *a, | |
926 | unsigned f, size_t hsz) | |
927 | { | |
928 | gaeadaad_pyobj *ga; | |
929 | ||
930 | assert(cobj); Py_INCREF(cobj); | |
931 | ga = PyObject_NEW(gaeadaad_pyobj, (PyTypeObject *)cobj); | |
932 | ga->a = a; ga->f = f; ga->hsz = hsz; ga->hlen = 0; | |
933 | return ((PyObject *)ga); | |
934 | } | |
935 | ||
936 | static void gaeadaad_pydealloc(PyObject *me) | |
937 | { | |
938 | gaeadaad_pyobj *ga = (gaeadaad_pyobj *)me; | |
939 | ||
940 | if (ga->a) GAEAD_DESTROY(ga->a); | |
941 | Py_DECREF(me->ob_type); FREEOBJ(me); | |
942 | } | |
943 | ||
944 | static int gaea_check(PyObject *me) | |
945 | { | |
946 | gaeadaad_pyobj *ga = (gaeadaad_pyobj *)me; | |
947 | int rc = -1; | |
948 | ||
949 | if ((ga->f&AEADF_DEAD) || !ga->a) VALERR("aad object no longer active"); | |
950 | rc = 0; | |
951 | end: | |
952 | return (rc); | |
953 | } | |
954 | ||
955 | static void gaea_invalidate(gaeadaad_pyobj *ga) | |
956 | { if (ga) ga->f |= AEADF_DEAD; } | |
957 | ||
958 | static void gaea_sever(gaeadaad_pyobj **ga_inout) | |
959 | { | |
960 | gaeadaad_pyobj *ga = *ga_inout; | |
961 | if (ga) { ga->f |= AEADF_DEAD; ga->a = 0; Py_DECREF(ga); *ga_inout = 0; } | |
962 | } | |
963 | ||
964 | static PyObject *gaeaget_hsz(PyObject *me, void *hunoz) | |
965 | { | |
966 | if (gaea_check(me)) return (0); | |
967 | else if (GAEADAAD_F(me)&AEADF_PCHSZ) return getulong(GAEADAAD_HSZ(me)); | |
968 | else RETURN_NONE; | |
969 | } | |
970 | ||
971 | static PyObject *gaeaget_hlen(PyObject *me, void *hunoz) | |
972 | { return (gaea_check(me) ? 0 : getulong(GAEADAAD_HLEN(me))); } | |
973 | ||
974 | static PyGetSetDef gaeadaad_pygetset[] = { | |
975 | #define GETSETNAME(op, name) gaea##op##_##name | |
976 | GET (hsz, "AAD.hsz -> precommitted header length or `None'") | |
977 | GET (hlen, "AAD.hlen -> header length so far") | |
978 | #undef GETSETNAME | |
979 | { 0 } | |
980 | }; | |
981 | ||
982 | static PyObject *gaeameth_copy(PyObject *me, PyObject *arg) | |
983 | { | |
984 | PyObject *rc = 0; | |
985 | ||
986 | if (!PyArg_ParseTuple(arg, ":copy")) goto end; | |
987 | if (gaea_check(me)) goto end; | |
988 | if (GAEADAAD_F(me)&AEADF_AADNDEP) | |
989 | VALERR("can't duplicate nonce-dependent aad"); | |
990 | rc = gaeadaad_pywrap((PyObject *)me->ob_type, | |
991 | GAEAD_DUP(GAEADAAD_A(me)), 0, 0); | |
de7abe02 | 992 | GAEADAAD_HLEN(rc) = GAEADAAD_HLEN(me); |
bebf03ab MW |
993 | end: |
994 | return (rc); | |
995 | } | |
996 | ||
997 | static int gaeadaad_hash(PyObject *me, const void *h, size_t hsz) | |
998 | { | |
999 | gaeadaad_pyobj *ga = (gaeadaad_pyobj *)me; | |
1000 | int rc = -1; | |
1001 | ||
1002 | if (gaea_check(me)) goto end; | |
1003 | if ((ga->f&AEADF_NOAAD) && hsz) | |
1004 | VALERR("header data not permitted"); | |
1005 | if ((ga->f&AEADF_PCHSZ) && hsz > ga->hsz - ga->hlen) | |
1006 | VALERR("too large for precommitted header length"); | |
1007 | GAEAD_HASH(ga->a, h, hsz); ga->hlen += hsz; | |
1008 | rc = 0; | |
1009 | end: | |
1010 | return (rc); | |
1011 | } | |
1012 | ||
1013 | ||
1014 | static PyObject *gaeameth_hash(PyObject *me, PyObject *arg) | |
1015 | { | |
1016 | char *h; Py_ssize_t hsz; | |
1017 | ||
1018 | if (!PyArg_ParseTuple(arg, "s#:hash", &h, &hsz)) return (0); | |
1019 | if (gaeadaad_hash(me, h, hsz)) return (0); | |
1020 | RETURN_ME; | |
1021 | } | |
1022 | ||
1023 | #define GAEAMETH_HASHU_(n, W, w) \ | |
1024 | static PyObject *gaeameth_hashu##w(PyObject *me, PyObject *arg) \ | |
1025 | { \ | |
1026 | uint##n x; octet b[SZ_##W]; \ | |
1027 | if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \ | |
1028 | STORE##W(b, x); if (gaeadaad_hash(me, b, sizeof(b))) return (0); \ | |
1029 | RETURN_ME; \ | |
1030 | } | |
1031 | DOUINTCONV(GAEAMETH_HASHU_) | |
1032 | ||
1033 | #define GAEAMETH_HASHBUF_(n, W, w) \ | |
1034 | static PyObject *gaeameth_hashbuf##w(PyObject *me, PyObject *arg) \ | |
1035 | { \ | |
1036 | char *p; Py_ssize_t sz; octet b[SZ_##W]; \ | |
1037 | if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \ | |
1038 | if (sz > MASK##n) TYERR("string too long"); \ | |
1039 | STORE##W(b, sz); if (gaeadaad_hash(me, b, sizeof(b))) goto end; \ | |
1040 | if (gaeadaad_hash(me, p, sz)) goto end; \ | |
1041 | RETURN_ME; \ | |
1042 | end: \ | |
1043 | return (0); \ | |
1044 | } | |
1045 | DOUINTCONV(GAEAMETH_HASHBUF_) | |
1046 | ||
1047 | static PyObject *gaeameth_hashstrz(PyObject *me, PyObject *arg) | |
1048 | { | |
1049 | char *p; | |
1050 | if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0); | |
1051 | if (gaeadaad_hash(me, p, strlen(p) + 1)) return (0); | |
1052 | RETURN_ME; | |
1053 | } | |
1054 | ||
1055 | static PyMethodDef gaeadaad_pymethods[] = { | |
1056 | #define METHNAME(name) gaeameth_##name | |
1057 | METH (copy, "AAD.copy() -> AAD'") | |
1058 | METH (hash, "AAD.hash(H)") | |
1059 | #define METHU_(n, W, w) METH(hashu##w, "AAD.hashu" #w "(WORD)") | |
1060 | DOUINTCONV(METHU_) | |
1061 | #undef METHU_ | |
1062 | #define METHBUF_(n, W, w) METH(hashbuf##w, "AAD.hashbuf" #w "(BYTES)") | |
1063 | DOUINTCONV(METHBUF_) | |
1064 | #undef METHBUF_ | |
1065 | METH (hashstrz, "AAD.hashstrz(STRING)") | |
1066 | #undef METHNAME | |
1067 | { 0 } | |
1068 | }; | |
1069 | ||
1070 | PyObject *gaeadenc_pywrap(PyObject *cobj, gaead_enc *e, unsigned f, | |
1071 | size_t hsz, size_t msz, size_t tsz) | |
1072 | { | |
1073 | gaeadenc_pyobj *ge; | |
1074 | ||
1075 | assert(cobj); Py_INCREF(cobj); | |
1076 | ge = PyObject_NEW(gaeadenc_pyobj, (PyTypeObject *)cobj); | |
1077 | ge->e = e; ge->f = f; ge->hsz = hsz; ge->msz = msz; ge->tsz = tsz; | |
1078 | ge->aad = 0; ge->mlen = 0; | |
1079 | return ((PyObject *)ge); | |
1080 | } | |
1081 | ||
1082 | static void gaeadenc_pydealloc(PyObject *me) | |
1083 | { | |
1084 | gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me; | |
1085 | ||
1086 | gaea_sever(&ge->aad); GAEAD_DESTROY(ge->e); | |
1087 | Py_DECREF(me->ob_type); FREEOBJ(me); | |
1088 | } | |
1089 | ||
1090 | static PyObject *gaeeget_hsz(PyObject *me, void *hunoz) | |
1091 | { | |
1092 | if (GAEADENC_F(me)&AEADF_PCHSZ) return getulong(GAEADENC_HSZ(me)); | |
1093 | else RETURN_NONE; | |
1094 | } | |
1095 | ||
1096 | static PyObject *gaeeget_msz(PyObject *me, void *hunoz) | |
1097 | { | |
1098 | if (GAEADENC_F(me)&AEADF_PCMSZ) return getulong(GAEADENC_MSZ(me)); | |
1099 | else RETURN_NONE; | |
1100 | } | |
1101 | ||
1102 | static PyObject *gaeeget_tsz(PyObject *me, void *hunoz) | |
1103 | { | |
1104 | if (GAEADENC_F(me)&AEADF_PCTSZ) return getulong(GAEADENC_TSZ(me)); | |
1105 | else RETURN_NONE; | |
1106 | } | |
1107 | ||
1108 | static PyObject *gaeeget_mlen(PyObject *me, void *hunoz) | |
1109 | { return getulong(GAEADENC_MLEN(me)); } | |
1110 | ||
1111 | static PyGetSetDef gaeadenc_pygetset[] = { | |
1112 | #define GETSETNAME(op, name) gaee##op##_##name | |
1113 | GET (hsz, "ENC.hsz -> precommitted header length or `None'") | |
1114 | GET (msz, "ENC.msz -> precommitted message length or `None'") | |
1115 | GET (tsz, "ENC.tsz -> precommitted tag length or `None'") | |
1116 | GET (mlen, "ENC.mlen -> message length so far") | |
1117 | #undef GETSETNAME | |
1118 | { 0 } | |
1119 | }; | |
1120 | ||
1121 | static PyObject *gaeemeth_aad(PyObject *me, PyObject *arg) | |
1122 | { | |
1123 | gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me; | |
1124 | PyObject *rc = 0; | |
1125 | ||
1126 | if (!PyArg_ParseTuple(arg, ":aad")) return (0); | |
1127 | if (!(ge->f&AEADF_AADNDEP)) | |
1128 | rc = gaeadaad_pywrap((PyObject *)GCAEADENC_KEY(ge->ob_type)->aad, | |
1129 | GAEAD_AAD(ge->e), 0, 0); | |
1130 | else { | |
1131 | if ((ge->f&AEADF_AADFIRST) && ge->mlen) | |
1132 | VALERR("too late for aad"); | |
1133 | if (!ge->aad) | |
1134 | ge->aad = (gaeadaad_pyobj *) | |
1135 | gaeadaad_pywrap((PyObject *)GCAEADENC_KEY(ge->ob_type)->aad, | |
f77c6b68 MW |
1136 | GAEAD_AAD(ge->e), ge->f&(AEADF_PCHSZ | AEADF_NOAAD), |
1137 | ge->hsz); | |
bebf03ab MW |
1138 | Py_INCREF(ge->aad); |
1139 | rc = (PyObject *)ge->aad; | |
1140 | } | |
1141 | end: | |
1142 | return (rc); | |
1143 | } | |
1144 | ||
1145 | static PyObject *gaeemeth_reinit(PyObject *me, PyObject *arg, PyObject *kw) | |
1146 | { | |
1147 | static const char *const kwlist[] = { "nonce", "hsz", "msz", "tsz", 0 }; | |
1148 | gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me; | |
1149 | char *n; Py_ssize_t nsz; | |
1150 | PyObject *hszobj = Py_None, *mszobj = Py_None, *tszobj = Py_None; | |
1151 | size_t hsz = 0, msz = 0, tsz = 0; | |
1152 | unsigned f; | |
1153 | ||
1154 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|OOO:enc", KWLIST, | |
1155 | &n, &nsz, &hszobj, &mszobj, &tszobj)) | |
1156 | goto end; | |
1157 | if (check_aead_encdec(ge->e->ops->c, &f, nsz, | |
1158 | hszobj, &hsz, mszobj, &msz, tszobj, &tsz)) | |
1159 | goto end; | |
1160 | if (GAEAD_REINIT(ge->e, n, nsz, hsz, msz, tsz)) | |
1161 | VALERR("bad aead parameter combination"); | |
1162 | gaea_sever(&ge->aad); | |
1163 | ge->f = f; ge->hsz = hsz; ge->msz = msz; ge->tsz = tsz; | |
1164 | end: | |
1165 | return (0); | |
1166 | } | |
1167 | ||
1168 | static PyObject *gaeemeth_encrypt(PyObject *me, PyObject *arg) | |
1169 | { | |
1170 | gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me; | |
1171 | char *m; Py_ssize_t msz; | |
1172 | char *c = 0; size_t csz; buf b; | |
1173 | int err; | |
1174 | PyObject *rc = 0; | |
1175 | ||
1176 | if (!PyArg_ParseTuple(arg, "s#:encrypt", &m, &msz)) goto end; | |
1177 | if (ge->f&AEADF_AADFIRST) { | |
1178 | if ((ge->f&AEADF_PCHSZ) && (ge->aad ? ge->aad->hlen : 0) != ge->hsz) | |
1179 | VALERR("header doesn't match precommitted length"); | |
1180 | gaea_invalidate(ge->aad); | |
1181 | } | |
1182 | if ((ge->f&AEADF_PCMSZ) && msz > ge->msz - ge->mlen) | |
1183 | VALERR("too large for precommitted message length"); | |
1184 | csz = msz + ge->e->ops->c->bufsz; c = xmalloc(csz); buf_init(&b, c, csz); | |
1185 | err = GAEAD_ENCRYPT(ge->e, m, msz, &b); assert(!err); (void)err; | |
1186 | buf_flip(&b); rc = bytestring_pywrapbuf(&b); ge->mlen += msz; | |
1187 | end: | |
1188 | xfree(c); | |
1189 | return (rc); | |
1190 | } | |
1191 | ||
1192 | static PyObject *gaeemeth_done(PyObject *me, PyObject *arg, PyObject *kw) | |
1193 | { | |
1194 | static const char *const kwlist[] = { "tsz", "aad", 0 }; | |
1195 | gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me; | |
1196 | PyObject *aad = Py_None; | |
1197 | char *c = 0; size_t csz; buf b; | |
1198 | PyObject *tszobj = Py_None; PyObject *tag; size_t tsz; | |
1199 | int err; | |
1200 | PyObject *rc = 0; | |
1201 | ||
1202 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|OO:done", KWLIST, | |
1203 | &tszobj, &aad)) | |
1204 | goto end; | |
1205 | if (tszobj != Py_None && !convszt(tszobj, &tsz)) goto end; | |
1206 | if (aad != Py_None && | |
1207 | !PyObject_TypeCheck(aad, | |
1208 | (PyTypeObject *)GCAEADENC_KEY(me->ob_type)->aad)) | |
1209 | TYERR("wanted aad"); | |
1210 | if ((ge->f&AEADF_AADNDEP) && aad != Py_None && aad != (PyObject *)ge->aad) | |
1211 | VALERR("mismatched aad"); | |
1212 | if ((ge->f&AEADF_PCHSZ) && | |
1213 | (aad == Py_None ? 0 : GAEADAAD_HLEN(aad)) != ge->hsz) | |
1214 | VALERR("header doesn't match precommitted length"); | |
1215 | if ((ge->f&AEADF_PCMSZ) && ge->mlen != ge->msz) | |
1216 | VALERR("message doesn't match precommitted length"); | |
1217 | if (tszobj == Py_None) { | |
1218 | if (ge->f&AEADF_PCTSZ) tsz = ge->tsz; | |
1219 | else tsz = keysz(0, ge->e->ops->c->tagsz); | |
1220 | } else { | |
1221 | if ((ge->f&AEADF_PCTSZ) && tsz != ge->tsz) | |
1222 | VALERR("tag length doesn't match precommitted value"); | |
1223 | if (keysz(tsz, ge->e->ops->c->tagsz) != tsz) VALERR("bad tag length"); | |
1224 | } | |
1225 | csz = ge->e->ops->c->bufsz; c = xmalloc(csz); buf_init(&b, c, csz); | |
1226 | tag = bytestring_pywrap(0, tsz); | |
1227 | err = GAEAD_DONE(ge->e, aad == Py_None ? 0 : GAEADAAD_A(aad), &b, | |
1228 | PyString_AS_STRING(tag), tsz); | |
1229 | assert(!err); (void)err; | |
1230 | buf_flip(&b); rc = Py_BuildValue("NN", bytestring_pywrapbuf(&b), tag); | |
1231 | end: | |
1232 | xfree(c); | |
1233 | return (rc); | |
1234 | } | |
1235 | ||
1236 | static PyMethodDef gaeadenc_pymethods[] = { | |
1237 | #define METHNAME(name) gaeemeth_##name | |
1238 | METH (aad, "ENC.aad() -> AAD") | |
1239 | KWMETH(reinit, "ENC.reinit(NONCE, [hsz], [msz], [tsz])") | |
1240 | METH (encrypt, "ENC.encrypt(MSG) -> CT") | |
1241 | KWMETH(done, "ENC.done([tsz], [aad]) -> CT, TAG") | |
1242 | #undef METHNAME | |
1243 | { 0 } | |
1244 | }; | |
1245 | ||
1246 | PyObject *gaeaddec_pywrap(PyObject *cobj, gaead_dec *d, unsigned f, | |
1247 | size_t hsz, size_t csz, size_t tsz) | |
1248 | { | |
1249 | gaeaddec_pyobj *gd; | |
1250 | assert(cobj); Py_INCREF(cobj); | |
1251 | gd = PyObject_NEW(gaeaddec_pyobj, (PyTypeObject *)cobj); | |
1252 | gd->d = d; gd->f = f; gd->hsz = hsz; gd->csz = csz; gd->tsz = tsz; | |
1253 | gd->aad = 0; gd->clen = 0; | |
1254 | return ((PyObject *)gd); | |
1255 | } | |
1256 | ||
1257 | static void gaeaddec_pydealloc(PyObject *me) | |
1258 | { | |
1259 | gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me; | |
1260 | ||
1261 | gaea_sever(&gd->aad); GAEAD_DESTROY(GAEADDEC_D(me)); | |
1262 | Py_DECREF(me->ob_type); FREEOBJ(me); | |
1263 | } | |
1264 | ||
1265 | static PyObject *gaedget_hsz(PyObject *me, void *hunoz) | |
1266 | { | |
1267 | if (GAEADDEC_F(me)&AEADF_PCHSZ) return getulong(GAEADDEC_HSZ(me)); | |
1268 | else RETURN_NONE; | |
1269 | } | |
1270 | ||
1271 | static PyObject *gaedget_csz(PyObject *me, void *hunoz) | |
1272 | { | |
1273 | if (GAEADDEC_F(me)&AEADF_PCMSZ) return getulong(GAEADDEC_CSZ(me)); | |
1274 | else RETURN_NONE; | |
1275 | } | |
1276 | ||
1277 | static PyObject *gaedget_tsz(PyObject *me, void *hunoz) | |
1278 | { | |
1279 | if (GAEADDEC_F(me)&AEADF_PCTSZ) return getulong(GAEADDEC_TSZ(me)); | |
1280 | else RETURN_NONE; | |
1281 | } | |
1282 | ||
1283 | static PyObject *gaedget_clen(PyObject *me, void *hunoz) | |
1284 | { return getulong(GAEADDEC_CLEN(me)); } | |
1285 | ||
1286 | static PyGetSetDef gaeaddec_pygetset[] = { | |
1287 | #define GETSETNAME(op, name) gaed##op##_##name | |
1288 | GET (hsz, "DEC.hsz -> precommitted header length or `None'") | |
1289 | GET (csz, "DEC.csz -> precommitted ciphertext length or `None'") | |
1290 | GET (tsz, "DEC.tsz -> precommitted tag length or `None'") | |
1291 | GET (clen, "DEC.clen -> ciphertext length so far") | |
1292 | #undef GETSETNAME | |
1293 | { 0 } | |
1294 | }; | |
1295 | ||
1296 | static PyObject *gaedmeth_aad(PyObject *me, PyObject *arg) | |
1297 | { | |
1298 | gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me; | |
1299 | ||
1300 | if (!PyArg_ParseTuple(arg, ":aad")) return (0); | |
1301 | if (!(gd->f&AEADF_AADNDEP)) | |
1302 | return (gaeadaad_pywrap((PyObject *)GCAEADDEC_KEY(gd->ob_type)->aad, | |
1303 | GAEAD_AAD(gd->d), 0, 0)); | |
1304 | else { | |
1305 | if (!gd->aad) | |
1306 | gd->aad = (gaeadaad_pyobj *) | |
1307 | gaeadaad_pywrap((PyObject *)GCAEADENC_KEY(gd->ob_type)->aad, | |
f77c6b68 MW |
1308 | GAEAD_AAD(gd->d), gd->f&(AEADF_PCHSZ | AEADF_NOAAD), |
1309 | gd->hsz); | |
bebf03ab MW |
1310 | Py_INCREF(gd->aad); |
1311 | return ((PyObject *)gd->aad); | |
1312 | } | |
1313 | } | |
1314 | ||
1315 | static PyObject *gaedmeth_reinit(PyObject *me, PyObject *arg, PyObject *kw) | |
1316 | { | |
1317 | static const char *const kwlist[] = { "nonce", "hsz", "csz", "tsz", 0 }; | |
1318 | gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me; | |
1319 | char *n; Py_ssize_t nsz; | |
1320 | PyObject *hszobj = Py_None, *cszobj = Py_None, *tszobj = Py_None; | |
1321 | size_t hsz = 0, csz = 0, tsz = 0; | |
1322 | unsigned f; | |
1323 | ||
1324 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|OOO:enc", KWLIST, | |
1325 | &n, &nsz, &hszobj, &cszobj, &tszobj)) | |
1326 | goto end; | |
1327 | if (check_aead_encdec(gd->d->ops->c, &f, nsz, | |
1328 | hszobj, &hsz, cszobj, &csz, tszobj, &tsz)) | |
1329 | goto end; | |
1330 | if (GAEAD_REINIT(gd->d, n, nsz, hsz, csz, tsz)) | |
1331 | VALERR("bad aead parameter combination"); | |
1332 | gaea_sever(&gd->aad); | |
1333 | gd->f = f; gd->hsz = hsz; gd->csz = csz; gd->tsz = tsz; | |
1334 | end: | |
1335 | return (0); | |
1336 | } | |
1337 | ||
1338 | static PyObject *gaedmeth_decrypt(PyObject *me, PyObject *arg) | |
1339 | { | |
1340 | gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me; | |
1341 | char *c; Py_ssize_t csz; | |
1342 | char *m = 0; size_t msz; buf b; | |
1343 | int err; | |
1344 | PyObject *rc = 0; | |
1345 | ||
1346 | if (!PyArg_ParseTuple(arg, "s#:decrypt", &c, &csz)) goto end; | |
1347 | if (gd->f&AEADF_AADFIRST) { | |
1348 | if ((gd->f&AEADF_PCHSZ) && (gd->aad ? gd->aad->hlen : 0) != gd->hsz) | |
1349 | VALERR("header doesn't match precommitted length"); | |
1350 | gaea_invalidate(gd->aad); | |
1351 | } | |
1352 | if ((gd->f&AEADF_PCMSZ) && csz > gd->csz - gd->clen) | |
1353 | VALERR("too large for precommitted message length"); | |
1354 | msz = csz + gd->d->ops->c->bufsz; m = xmalloc(msz); buf_init(&b, m, msz); | |
1355 | err = GAEAD_DECRYPT(gd->d, c, csz, &b); assert(!err); (void)err; | |
1356 | buf_flip(&b); rc = bytestring_pywrapbuf(&b); gd->clen += csz; | |
1357 | end: | |
1358 | xfree(m); | |
1359 | return (rc); | |
1360 | } | |
1361 | ||
1362 | static PyObject *gaedmeth_done(PyObject *me, PyObject *arg, PyObject *kw) | |
1363 | { | |
1364 | static const char *const kwlist[] = { "tag", "aad", 0 }; | |
1365 | gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me; | |
1366 | PyObject *aad = Py_None; | |
1367 | char *t; Py_ssize_t tsz; | |
1368 | char *m = 0; size_t msz; buf b; | |
1369 | int err; | |
1370 | PyObject *rc = 0; | |
1371 | ||
1372 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#|O:done", KWLIST, | |
1373 | &t, &tsz, &aad)) | |
1374 | goto end; | |
1375 | if (aad != Py_None && | |
1376 | !PyObject_TypeCheck(aad, | |
1377 | (PyTypeObject *)GCAEADENC_KEY(me->ob_type)->aad)) | |
1378 | TYERR("wanted aad"); | |
1379 | if ((gd->f&AEADF_AADNDEP) && aad != Py_None && aad != (PyObject *)gd->aad) | |
1380 | VALERR("mismatched aad"); | |
1381 | if ((gd->f&AEADF_PCHSZ) && | |
1382 | (aad == Py_None ? 0 : GAEADAAD_HLEN(aad)) != gd->hsz) | |
1383 | VALERR("header doesn't match precommitted length"); | |
1384 | if ((gd->f&AEADF_PCMSZ) && gd->clen != gd->csz) | |
1385 | VALERR("message doesn't match precommitted length"); | |
1386 | if ((gd->f&AEADF_PCTSZ) && tsz != gd->tsz) | |
1387 | VALERR("tag length doesn't match precommitted value"); | |
1388 | if (keysz(tsz, gd->d->ops->c->tagsz) != tsz) VALERR("bad tag length"); | |
1389 | msz = gd->d->ops->c->bufsz; m = xmalloc(msz); buf_init(&b, m, msz); | |
1390 | err = GAEAD_DONE(gd->d, aad == Py_None ? 0 : GAEADAAD_A(aad), &b, t, tsz); | |
1391 | assert(err >= 0); | |
1392 | if (!err) VALERR("decryption failed"); | |
1393 | buf_flip(&b); rc = bytestring_pywrapbuf(&b); | |
1394 | end: | |
1395 | xfree(m); | |
1396 | return (rc); | |
1397 | } | |
1398 | ||
1399 | static PyMethodDef gaeaddec_pymethods[] = { | |
1400 | #define METHNAME(name) gaedmeth_##name | |
1401 | METH (aad, "DEC.aad() -> AAD") | |
1402 | KWMETH(reinit, "DEC.reinit(NONCE, [hsz], [csz], [tsz])") | |
1403 | METH (decrypt, "DEC.decrypt(CT) -> MSG") | |
1404 | KWMETH(done, "DEC.done(TAG, [aad]) -> MSG | None") | |
1405 | #undef METHNAME | |
1406 | { 0 } | |
1407 | }; | |
1408 | ||
1409 | static PyTypeObject gcaead_pytype_skel = { | |
1410 | PyObject_HEAD_INIT(0) 0, /* Header */ | |
1411 | "GCAEAD", /* @tp_name@ */ | |
1412 | sizeof(gcaead_pyobj), /* @tp_basicsize@ */ | |
1413 | 0, /* @tp_itemsize@ */ | |
1414 | ||
1415 | 0, /* @tp_dealloc@ */ | |
1416 | 0, /* @tp_print@ */ | |
1417 | 0, /* @tp_getattr@ */ | |
1418 | 0, /* @tp_setattr@ */ | |
1419 | 0, /* @tp_compare@ */ | |
1420 | 0, /* @tp_repr@ */ | |
1421 | 0, /* @tp_as_number@ */ | |
1422 | 0, /* @tp_as_sequence@ */ | |
1423 | 0, /* @tp_as_mapping@ */ | |
1424 | 0, /* @tp_hash@ */ | |
1425 | 0, /* @tp_call@ */ | |
1426 | 0, /* @tp_str@ */ | |
1427 | 0, /* @tp_getattro@ */ | |
1428 | 0, /* @tp_setattro@ */ | |
1429 | 0, /* @tp_as_buffer@ */ | |
1430 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1431 | Py_TPFLAGS_BASETYPE, | |
1432 | ||
1433 | /* @tp_doc@ */ | |
1434 | "Authenticated encryption (key) metaclass.", | |
1435 | ||
1436 | 0, /* @tp_traverse@ */ | |
1437 | 0, /* @tp_clear@ */ | |
1438 | 0, /* @tp_richcompare@ */ | |
1439 | 0, /* @tp_weaklistoffset@ */ | |
1440 | 0, /* @tp_iter@ */ | |
1441 | 0, /* @tp_iternext@ */ | |
1442 | 0, /* @tp_methods@ */ | |
1443 | 0, /* @tp_members@ */ | |
1444 | gcaead_pygetset, /* @tp_getset@ */ | |
1445 | 0, /* @tp_base@ */ | |
1446 | 0, /* @tp_dict@ */ | |
1447 | 0, /* @tp_descr_get@ */ | |
1448 | 0, /* @tp_descr_set@ */ | |
1449 | 0, /* @tp_dictoffset@ */ | |
1450 | 0, /* @tp_init@ */ | |
1451 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1452 | abstract_pynew, /* @tp_new@ */ | |
1453 | 0, /* @tp_free@ */ | |
1454 | 0 /* @tp_is_gc@ */ | |
1455 | }; | |
1456 | ||
1457 | static PyTypeObject gaeadkey_pytype_skel = { | |
1458 | PyObject_HEAD_INIT(0) 0, /* Header */ | |
1459 | "GAEKey", /* @tp_name@ */ | |
1460 | sizeof(gaeadkey_pyobj), /* @tp_basicsize@ */ | |
1461 | 0, /* @tp_itemsize@ */ | |
1462 | ||
1463 | gaeadkey_pydealloc, /* @tp_dealloc@ */ | |
1464 | 0, /* @tp_print@ */ | |
1465 | 0, /* @tp_getattr@ */ | |
1466 | 0, /* @tp_setattr@ */ | |
1467 | 0, /* @tp_compare@ */ | |
1468 | 0, /* @tp_repr@ */ | |
1469 | 0, /* @tp_as_number@ */ | |
1470 | 0, /* @tp_as_sequence@ */ | |
1471 | 0, /* @tp_as_mapping@ */ | |
1472 | 0, /* @tp_hash@ */ | |
1473 | 0, /* @tp_call@ */ | |
1474 | 0, /* @tp_str@ */ | |
1475 | 0, /* @tp_getattro@ */ | |
1476 | 0, /* @tp_setattro@ */ | |
1477 | 0, /* @tp_as_buffer@ */ | |
1478 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1479 | Py_TPFLAGS_BASETYPE, | |
1480 | ||
1481 | /* @tp_doc@ */ | |
1482 | "Authenticated encryption key.", | |
1483 | ||
1484 | 0, /* @tp_traverse@ */ | |
1485 | 0, /* @tp_clear@ */ | |
1486 | 0, /* @tp_richcompare@ */ | |
1487 | 0, /* @tp_weaklistoffset@ */ | |
1488 | 0, /* @tp_iter@ */ | |
1489 | 0, /* @tp_iternext@ */ | |
1490 | gaeadkey_pymethods, /* @tp_methods@ */ | |
1491 | 0, /* @tp_members@ */ | |
1492 | 0, /* @tp_getset@ */ | |
1493 | 0, /* @tp_base@ */ | |
1494 | 0, /* @tp_dict@ */ | |
1495 | 0, /* @tp_descr_get@ */ | |
1496 | 0, /* @tp_descr_set@ */ | |
1497 | 0, /* @tp_dictoffset@ */ | |
1498 | 0, /* @tp_init@ */ | |
1499 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1500 | abstract_pynew, /* @tp_new@ */ | |
1501 | 0, /* @tp_free@ */ | |
1502 | 0 /* @tp_is_gc@ */ | |
1503 | }; | |
1504 | ||
1505 | static PyTypeObject gcaeadaad_pytype_skel = { | |
1506 | PyObject_HEAD_INIT(0) 0, /* Header */ | |
1507 | "GAEAADClass", /* @tp_name@ */ | |
1508 | sizeof(gcaeadaad_pyobj), /* @tp_basicsize@ */ | |
1509 | 0, /* @tp_itemsize@ */ | |
1510 | ||
1511 | 0, /* @tp_dealloc@ */ | |
1512 | 0, /* @tp_print@ */ | |
1513 | 0, /* @tp_getattr@ */ | |
1514 | 0, /* @tp_setattr@ */ | |
1515 | 0, /* @tp_compare@ */ | |
1516 | 0, /* @tp_repr@ */ | |
1517 | 0, /* @tp_as_number@ */ | |
1518 | 0, /* @tp_as_sequence@ */ | |
1519 | 0, /* @tp_as_mapping@ */ | |
1520 | 0, /* @tp_hash@ */ | |
1521 | 0, /* @tp_call@ */ | |
1522 | 0, /* @tp_str@ */ | |
1523 | 0, /* @tp_getattro@ */ | |
1524 | 0, /* @tp_setattro@ */ | |
1525 | 0, /* @tp_as_buffer@ */ | |
1526 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1527 | Py_TPFLAGS_BASETYPE, | |
1528 | ||
1529 | /* @tp_doc@ */ | |
1530 | "Authenticated encryption additional-data hash metaclass.", | |
1531 | ||
1532 | 0, /* @tp_traverse@ */ | |
1533 | 0, /* @tp_clear@ */ | |
1534 | 0, /* @tp_richcompare@ */ | |
1535 | 0, /* @tp_weaklistoffset@ */ | |
1536 | 0, /* @tp_iter@ */ | |
1537 | 0, /* @tp_iternext@ */ | |
1538 | 0, /* @tp_methods@ */ | |
1539 | 0, /* @tp_members@ */ | |
1540 | 0, /* @tp_getset@ */ | |
1541 | 0, /* @tp_base@ */ | |
1542 | 0, /* @tp_dict@ */ | |
1543 | 0, /* @tp_descr_get@ */ | |
1544 | 0, /* @tp_descr_set@ */ | |
1545 | 0, /* @tp_dictoffset@ */ | |
1546 | 0, /* @tp_init@ */ | |
1547 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1548 | abstract_pynew, /* @tp_new@ */ | |
1549 | 0, /* @tp_free@ */ | |
1550 | 0 /* @tp_is_gc@ */ | |
1551 | }; | |
1552 | ||
1553 | static PyTypeObject gaeadaad_pytype_skel = { | |
1554 | PyObject_HEAD_INIT(0) 0, /* Header */ | |
1555 | "GAEAAD", /* @tp_name@ */ | |
1556 | sizeof(gaeadaad_pyobj), /* @tp_basicsize@ */ | |
1557 | 0, /* @tp_itemsize@ */ | |
1558 | ||
1559 | gaeadaad_pydealloc, /* @tp_dealloc@ */ | |
1560 | 0, /* @tp_print@ */ | |
1561 | 0, /* @tp_getattr@ */ | |
1562 | 0, /* @tp_setattr@ */ | |
1563 | 0, /* @tp_compare@ */ | |
1564 | 0, /* @tp_repr@ */ | |
1565 | 0, /* @tp_as_number@ */ | |
1566 | 0, /* @tp_as_sequence@ */ | |
1567 | 0, /* @tp_as_mapping@ */ | |
1568 | 0, /* @tp_hash@ */ | |
1569 | 0, /* @tp_call@ */ | |
1570 | 0, /* @tp_str@ */ | |
1571 | 0, /* @tp_getattro@ */ | |
1572 | 0, /* @tp_setattro@ */ | |
1573 | 0, /* @tp_as_buffer@ */ | |
1574 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1575 | Py_TPFLAGS_BASETYPE, | |
1576 | ||
1577 | /* @tp_doc@ */ | |
1578 | "Authenticated encryption AAD hash.", | |
1579 | ||
1580 | 0, /* @tp_traverse@ */ | |
1581 | 0, /* @tp_clear@ */ | |
1582 | 0, /* @tp_richcompare@ */ | |
1583 | 0, /* @tp_weaklistoffset@ */ | |
1584 | 0, /* @tp_iter@ */ | |
1585 | 0, /* @tp_iternext@ */ | |
1586 | gaeadaad_pymethods, /* @tp_methods@ */ | |
1587 | 0, /* @tp_members@ */ | |
1588 | gaeadaad_pygetset, /* @tp_getset@ */ | |
1589 | 0, /* @tp_base@ */ | |
1590 | 0, /* @tp_dict@ */ | |
1591 | 0, /* @tp_descr_get@ */ | |
1592 | 0, /* @tp_descr_set@ */ | |
1593 | 0, /* @tp_dictoffset@ */ | |
1594 | 0, /* @tp_init@ */ | |
1595 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1596 | abstract_pynew, /* @tp_new@ */ | |
1597 | 0, /* @tp_free@ */ | |
1598 | 0 /* @tp_is_gc@ */ | |
1599 | }; | |
1600 | ||
1601 | static PyTypeObject gcaeadenc_pytype_skel = { | |
1602 | PyObject_HEAD_INIT(0) 0, /* Header */ | |
1603 | "GAEEncClass", /* @tp_name@ */ | |
1604 | sizeof(gcaeadenc_pyobj), /* @tp_basicsize@ */ | |
1605 | 0, /* @tp_itemsize@ */ | |
1606 | ||
1607 | 0, /* @tp_dealloc@ */ | |
1608 | 0, /* @tp_print@ */ | |
1609 | 0, /* @tp_getattr@ */ | |
1610 | 0, /* @tp_setattr@ */ | |
1611 | 0, /* @tp_compare@ */ | |
1612 | 0, /* @tp_repr@ */ | |
1613 | 0, /* @tp_as_number@ */ | |
1614 | 0, /* @tp_as_sequence@ */ | |
1615 | 0, /* @tp_as_mapping@ */ | |
1616 | 0, /* @tp_hash@ */ | |
1617 | 0, /* @tp_call@ */ | |
1618 | 0, /* @tp_str@ */ | |
1619 | 0, /* @tp_getattro@ */ | |
1620 | 0, /* @tp_setattro@ */ | |
1621 | 0, /* @tp_as_buffer@ */ | |
1622 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1623 | Py_TPFLAGS_BASETYPE, | |
1624 | ||
1625 | /* @tp_doc@ */ | |
1626 | "Authenticated encryption operation metaclass.", | |
1627 | ||
1628 | 0, /* @tp_traverse@ */ | |
1629 | 0, /* @tp_clear@ */ | |
1630 | 0, /* @tp_richcompare@ */ | |
1631 | 0, /* @tp_weaklistoffset@ */ | |
1632 | 0, /* @tp_iter@ */ | |
1633 | 0, /* @tp_iternext@ */ | |
1634 | 0, /* @tp_methods@ */ | |
1635 | 0, /* @tp_members@ */ | |
1636 | 0, /* @tp_getset@ */ | |
1637 | 0, /* @tp_base@ */ | |
1638 | 0, /* @tp_dict@ */ | |
1639 | 0, /* @tp_descr_get@ */ | |
1640 | 0, /* @tp_descr_set@ */ | |
1641 | 0, /* @tp_dictoffset@ */ | |
1642 | 0, /* @tp_init@ */ | |
1643 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1644 | abstract_pynew, /* @tp_new@ */ | |
1645 | 0, /* @tp_free@ */ | |
1646 | 0 /* @tp_is_gc@ */ | |
1647 | }; | |
1648 | ||
1649 | static PyTypeObject gaeadenc_pytype_skel = { | |
1650 | PyObject_HEAD_INIT(0) 0, /* Header */ | |
1651 | "GAEEnc", /* @tp_name@ */ | |
1652 | sizeof(gaeadenc_pyobj), /* @tp_basicsize@ */ | |
1653 | 0, /* @tp_itemsize@ */ | |
1654 | ||
1655 | gaeadenc_pydealloc, /* @tp_dealloc@ */ | |
1656 | 0, /* @tp_print@ */ | |
1657 | 0, /* @tp_getattr@ */ | |
1658 | 0, /* @tp_setattr@ */ | |
1659 | 0, /* @tp_compare@ */ | |
1660 | 0, /* @tp_repr@ */ | |
1661 | 0, /* @tp_as_number@ */ | |
1662 | 0, /* @tp_as_sequence@ */ | |
1663 | 0, /* @tp_as_mapping@ */ | |
1664 | 0, /* @tp_hash@ */ | |
1665 | 0, /* @tp_call@ */ | |
1666 | 0, /* @tp_str@ */ | |
1667 | 0, /* @tp_getattro@ */ | |
1668 | 0, /* @tp_setattro@ */ | |
1669 | 0, /* @tp_as_buffer@ */ | |
1670 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1671 | Py_TPFLAGS_BASETYPE, | |
1672 | ||
1673 | /* @tp_doc@ */ | |
1674 | "Authenticated encryption operation.", | |
1675 | ||
1676 | 0, /* @tp_traverse@ */ | |
1677 | 0, /* @tp_clear@ */ | |
1678 | 0, /* @tp_richcompare@ */ | |
1679 | 0, /* @tp_weaklistoffset@ */ | |
1680 | 0, /* @tp_iter@ */ | |
1681 | 0, /* @tp_iternext@ */ | |
1682 | gaeadenc_pymethods, /* @tp_methods@ */ | |
1683 | 0, /* @tp_members@ */ | |
1684 | gaeadenc_pygetset, /* @tp_getset@ */ | |
1685 | 0, /* @tp_base@ */ | |
1686 | 0, /* @tp_dict@ */ | |
1687 | 0, /* @tp_descr_get@ */ | |
1688 | 0, /* @tp_descr_set@ */ | |
1689 | 0, /* @tp_dictoffset@ */ | |
1690 | 0, /* @tp_init@ */ | |
1691 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1692 | abstract_pynew, /* @tp_new@ */ | |
1693 | 0, /* @tp_free@ */ | |
1694 | 0 /* @tp_is_gc@ */ | |
1695 | }; | |
1696 | ||
1697 | static PyTypeObject gcaeaddec_pytype_skel = { | |
1698 | PyObject_HEAD_INIT(0) 0, /* Header */ | |
1699 | "GAEDecClass", /* @tp_name@ */ | |
1700 | sizeof(gcaeaddec_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 | "Authenticated decryption operation metaclass.", | |
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 | 0, /* @tp_methods@ */ | |
1731 | 0, /* @tp_members@ */ | |
1732 | 0, /* @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 | abstract_pynew, /* @tp_new@ */ | |
1741 | 0, /* @tp_free@ */ | |
1742 | 0 /* @tp_is_gc@ */ | |
1743 | }; | |
1744 | ||
1745 | static PyTypeObject gaeaddec_pytype_skel = { | |
1746 | PyObject_HEAD_INIT(0) 0, /* Header */ | |
1747 | "GAEDec", /* @tp_name@ */ | |
1748 | sizeof(gaeaddec_pyobj), /* @tp_basicsize@ */ | |
1749 | 0, /* @tp_itemsize@ */ | |
1750 | ||
1751 | gaeaddec_pydealloc, /* @tp_dealloc@ */ | |
1752 | 0, /* @tp_print@ */ | |
1753 | 0, /* @tp_getattr@ */ | |
1754 | 0, /* @tp_setattr@ */ | |
1755 | 0, /* @tp_compare@ */ | |
1756 | 0, /* @tp_repr@ */ | |
1757 | 0, /* @tp_as_number@ */ | |
1758 | 0, /* @tp_as_sequence@ */ | |
1759 | 0, /* @tp_as_mapping@ */ | |
1760 | 0, /* @tp_hash@ */ | |
1761 | 0, /* @tp_call@ */ | |
1762 | 0, /* @tp_str@ */ | |
1763 | 0, /* @tp_getattro@ */ | |
1764 | 0, /* @tp_setattro@ */ | |
1765 | 0, /* @tp_as_buffer@ */ | |
1766 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1767 | Py_TPFLAGS_BASETYPE, | |
1768 | ||
1769 | /* @tp_doc@ */ | |
1770 | "Authenticated decryption operation.", | |
1771 | ||
1772 | 0, /* @tp_traverse@ */ | |
1773 | 0, /* @tp_clear@ */ | |
1774 | 0, /* @tp_richcompare@ */ | |
1775 | 0, /* @tp_weaklistoffset@ */ | |
1776 | 0, /* @tp_iter@ */ | |
1777 | 0, /* @tp_iternext@ */ | |
1778 | gaeaddec_pymethods, /* @tp_methods@ */ | |
1779 | 0, /* @tp_members@ */ | |
1780 | gaeaddec_pygetset, /* @tp_getset@ */ | |
1781 | 0, /* @tp_base@ */ | |
1782 | 0, /* @tp_dict@ */ | |
1783 | 0, /* @tp_descr_get@ */ | |
1784 | 0, /* @tp_descr_set@ */ | |
1785 | 0, /* @tp_dictoffset@ */ | |
1786 | 0, /* @tp_init@ */ | |
1787 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1788 | abstract_pynew, /* @tp_new@ */ | |
1789 | 0, /* @tp_free@ */ | |
1790 | 0 /* @tp_is_gc@ */ | |
1791 | }; | |
1792 | ||
d7ab1bab | 1793 | /*----- Hash functions ----------------------------------------------------*/ |
1794 | ||
1795 | PyTypeObject *gchash_pytype, *ghash_pytype; | |
1796 | ||
1797 | CONVFUNC(gchash, gchash *, GCHASH_CH) | |
1798 | CONVFUNC(ghash, ghash *, GHASH_H) | |
1799 | ||
1800 | static PyObject *ghash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
1801 | { | |
827f89d7 MW |
1802 | static const char *const kwlist[] = { 0 }; |
1803 | if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", KWLIST)) | |
d7ab1bab | 1804 | goto end; |
c41d0371 | 1805 | return (ghash_pywrap((PyObject *)ty, GH_INIT(GCHASH_CH(ty)))); |
d7ab1bab | 1806 | end: |
b2687a0a | 1807 | return (0); |
d7ab1bab | 1808 | } |
1809 | ||
1810 | PyObject *gchash_pywrap(gchash *ch) | |
1811 | { | |
df9f8366 | 1812 | gchash_pyobj *g = newtype(gchash_pytype, 0, ch->name); |
d7ab1bab | 1813 | g->ch = ch; |
24b3d57b MW |
1814 | g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj); |
1815 | g->ty.ht_type.tp_base = ghash_pytype; | |
d7ab1bab | 1816 | Py_INCREF(ghash_pytype); |
24b3d57b MW |
1817 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
1818 | Py_TPFLAGS_BASETYPE | | |
1819 | Py_TPFLAGS_HEAPTYPE); | |
1820 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; | |
1821 | g->ty.ht_type.tp_free = 0; | |
1822 | g->ty.ht_type.tp_new = ghash_pynew; | |
dc075750 | 1823 | typeready(&g->ty.ht_type); |
d7ab1bab | 1824 | return ((PyObject *)g); |
1825 | } | |
1826 | ||
c41d0371 | 1827 | PyObject *ghash_pywrap(PyObject *cobj, ghash *h) |
d7ab1bab | 1828 | { |
1829 | ghash_pyobj *g; | |
1830 | if (!cobj) cobj = gchash_pywrap((/*unconst*/ gchash *)GH_CLASS(h)); | |
1831 | else Py_INCREF(cobj); | |
1832 | g = PyObject_NEW(ghash_pyobj, (PyTypeObject *)cobj); | |
1833 | g->h = h; | |
b2687a0a | 1834 | return ((PyObject *)g); |
d7ab1bab | 1835 | } |
1836 | ||
1837 | static void ghash_pydealloc(PyObject *me) | |
1838 | { | |
c41d0371 | 1839 | GH_DESTROY(GHASH_H(me)); |
d7ab1bab | 1840 | Py_DECREF(me->ob_type); |
3aa33042 | 1841 | FREEOBJ(me); |
d7ab1bab | 1842 | } |
1843 | ||
1844 | static PyObject *gchget_name(PyObject *me, void *hunoz) | |
1845 | { return (PyString_FromString(GCHASH_CH(me)->name)); } | |
1846 | ||
1847 | static PyObject *gchget_hashsz(PyObject *me, void *hunoz) | |
1848 | { return (PyInt_FromLong(GCHASH_CH(me)->hashsz)); } | |
1849 | ||
1850 | static PyObject *gchget_bufsz(PyObject *me, void *hunoz) | |
1851 | { return (PyInt_FromLong(GCHASH_CH(me)->bufsz)); } | |
1852 | ||
cfe80e51 MW |
1853 | static PyObject *ghmeth_copy(PyObject *me, PyObject *arg) |
1854 | { | |
1855 | if (!PyArg_ParseTuple(arg, ":copy")) return (0); | |
1856 | return (ghash_pywrap((PyObject *)me->ob_type, GH_COPY(GHASH_H(me)))); | |
1857 | } | |
1858 | ||
d7ab1bab | 1859 | static PyObject *ghmeth_hash(PyObject *me, PyObject *arg) |
1860 | { | |
1861 | char *p; | |
6b54260d | 1862 | Py_ssize_t sz; |
d7ab1bab | 1863 | if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0); |
1864 | GH_HASH(GHASH_H(me), p, sz); | |
1865 | RETURN_ME; | |
1866 | } | |
1867 | ||
46e6ad89 | 1868 | #define GHMETH_HASHU_(n, W, w) \ |
1869 | static PyObject *ghmeth_hashu##w(PyObject *me, PyObject *arg) \ | |
1870 | { \ | |
1871 | uint##n x; \ | |
0e5c668c | 1872 | if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \ |
46e6ad89 | 1873 | GH_HASHU##W(GHASH_H(me), x); \ |
1874 | RETURN_ME; \ | |
46e6ad89 | 1875 | } |
1876 | DOUINTCONV(GHMETH_HASHU_) | |
1877 | ||
1878 | #define GHMETH_HASHBUF_(n, W, w) \ | |
1879 | static PyObject *ghmeth_hashbuf##w(PyObject *me, PyObject *arg) \ | |
1880 | { \ | |
1881 | char *p; \ | |
6b54260d | 1882 | Py_ssize_t sz; \ |
46e6ad89 | 1883 | if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \ |
1884 | if (sz > MASK##n) TYERR("string too long"); \ | |
1885 | GH_HASHBUF##W(GHASH_H(me), p, sz); \ | |
1886 | RETURN_ME; \ | |
1887 | end: \ | |
1888 | return (0); \ | |
1889 | } | |
1890 | DOUINTCONV(GHMETH_HASHBUF_) | |
1891 | ||
1892 | static PyObject *ghmeth_hashstrz(PyObject *me, PyObject *arg) | |
1893 | { | |
1894 | char *p; | |
1895 | if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0); | |
1896 | GH_HASHSTRZ(GHASH_H(me), p); | |
1897 | RETURN_ME; | |
1898 | } | |
1899 | ||
07bcd768 MW |
1900 | static PyObject *ghmeth_done(PyObject *me, PyObject *arg) |
1901 | { | |
1902 | ghash *g; | |
1903 | PyObject *rc; | |
1904 | if (!PyArg_ParseTuple(arg, ":done")) return (0); | |
1905 | g = GH_COPY(GHASH_H(me)); | |
1906 | rc = bytestring_pywrap(0, g->ops->c->hashsz); | |
1907 | GH_DONE(g, PyString_AS_STRING(rc)); | |
1908 | GH_DESTROY(g); | |
1909 | return (rc); | |
1910 | } | |
1911 | ||
1912 | static PyGetSetDef gchash_pygetset[] = { | |
1913 | #define GETSETNAME(op, name) gch##op##_##name | |
1914 | GET (bufsz, "CH.bufsz -> hash buffer size, or zero") | |
1915 | GET (hashsz, "CH.hashsz -> hash output size") | |
1916 | GET (name, "CH.name -> name of this kind of hash") | |
1917 | #undef GETSETNAME | |
1918 | { 0 } | |
1919 | }; | |
1920 | ||
d7ab1bab | 1921 | static PyMethodDef ghash_pymethods[] = { |
1922 | #define METHNAME(name) ghmeth_##name | |
cfe80e51 | 1923 | METH (copy, "H.copy() -> HH") |
d7ab1bab | 1924 | METH (hash, "H.hash(M)") |
46e6ad89 | 1925 | #define METHU_(n, W, w) METH(hashu##w, "H.hashu" #w "(WORD)") |
1926 | DOUINTCONV(METHU_) | |
07bcd768 | 1927 | #undef METHU_ |
46e6ad89 | 1928 | #define METHBUF_(n, W, w) METH(hashbuf##w, "H.hashbuf" #w "(BYTES)") |
1929 | DOUINTCONV(METHBUF_) | |
07bcd768 | 1930 | #undef METHBUF_ |
46e6ad89 | 1931 | METH (hashstrz, "H.hashstrz(STRING)") |
d7ab1bab | 1932 | METH (done, "H.done() -> HASH") |
1933 | #undef METHNAME | |
1934 | { 0 } | |
1935 | }; | |
1936 | ||
1937 | static PyTypeObject gchash_pytype_skel = { | |
6d4db0bf | 1938 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 1939 | "GCHash", /* @tp_name@ */ |
d7ab1bab | 1940 | sizeof(gchash_pyobj), /* @tp_basicsize@ */ |
1941 | 0, /* @tp_itemsize@ */ | |
1942 | ||
1943 | 0, /* @tp_dealloc@ */ | |
1944 | 0, /* @tp_print@ */ | |
1945 | 0, /* @tp_getattr@ */ | |
1946 | 0, /* @tp_setattr@ */ | |
1947 | 0, /* @tp_compare@ */ | |
1948 | 0, /* @tp_repr@ */ | |
1949 | 0, /* @tp_as_number@ */ | |
1950 | 0, /* @tp_as_sequence@ */ | |
1951 | 0, /* @tp_as_mapping@ */ | |
1952 | 0, /* @tp_hash@ */ | |
1953 | 0, /* @tp_call@ */ | |
1954 | 0, /* @tp_str@ */ | |
1955 | 0, /* @tp_getattro@ */ | |
1956 | 0, /* @tp_setattro@ */ | |
1957 | 0, /* @tp_as_buffer@ */ | |
1958 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1959 | Py_TPFLAGS_BASETYPE, | |
1960 | ||
1961 | /* @tp_doc@ */ | |
1962 | "Hash function metaclass.", | |
1963 | ||
1964 | 0, /* @tp_traverse@ */ | |
1965 | 0, /* @tp_clear@ */ | |
1966 | 0, /* @tp_richcompare@ */ | |
1967 | 0, /* @tp_weaklistoffset@ */ | |
1968 | 0, /* @tp_iter@ */ | |
963a6148 | 1969 | 0, /* @tp_iternext@ */ |
d7ab1bab | 1970 | 0, /* @tp_methods@ */ |
1971 | 0, /* @tp_members@ */ | |
1972 | gchash_pygetset, /* @tp_getset@ */ | |
1973 | 0, /* @tp_base@ */ | |
1974 | 0, /* @tp_dict@ */ | |
1975 | 0, /* @tp_descr_get@ */ | |
1976 | 0, /* @tp_descr_set@ */ | |
1977 | 0, /* @tp_dictoffset@ */ | |
1978 | 0, /* @tp_init@ */ | |
1979 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1980 | abstract_pynew, /* @tp_new@ */ | |
3aa33042 | 1981 | 0, /* @tp_free@ */ |
d7ab1bab | 1982 | 0 /* @tp_is_gc@ */ |
1983 | }; | |
1984 | ||
1985 | static PyTypeObject ghash_pytype_skel = { | |
6d4db0bf | 1986 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 1987 | "GHash", /* @tp_name@ */ |
d7ab1bab | 1988 | sizeof(ghash_pyobj), /* @tp_basicsize@ */ |
1989 | 0, /* @tp_itemsize@ */ | |
1990 | ||
1991 | ghash_pydealloc, /* @tp_dealloc@ */ | |
1992 | 0, /* @tp_print@ */ | |
1993 | 0, /* @tp_getattr@ */ | |
1994 | 0, /* @tp_setattr@ */ | |
1995 | 0, /* @tp_compare@ */ | |
1996 | 0, /* @tp_repr@ */ | |
1997 | 0, /* @tp_as_number@ */ | |
1998 | 0, /* @tp_as_sequence@ */ | |
1999 | 0, /* @tp_as_mapping@ */ | |
2000 | 0, /* @tp_hash@ */ | |
2001 | 0, /* @tp_call@ */ | |
2002 | 0, /* @tp_str@ */ | |
2003 | 0, /* @tp_getattro@ */ | |
2004 | 0, /* @tp_setattro@ */ | |
2005 | 0, /* @tp_as_buffer@ */ | |
2006 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
2007 | Py_TPFLAGS_BASETYPE, | |
2008 | ||
2009 | /* @tp_doc@ */ | |
2010 | "Hash function, abstract base class.", | |
2011 | ||
2012 | 0, /* @tp_traverse@ */ | |
2013 | 0, /* @tp_clear@ */ | |
2014 | 0, /* @tp_richcompare@ */ | |
2015 | 0, /* @tp_weaklistoffset@ */ | |
2016 | 0, /* @tp_iter@ */ | |
963a6148 | 2017 | 0, /* @tp_iternext@ */ |
d7ab1bab | 2018 | ghash_pymethods, /* @tp_methods@ */ |
2019 | 0, /* @tp_members@ */ | |
2020 | 0, /* @tp_getset@ */ | |
2021 | 0, /* @tp_base@ */ | |
2022 | 0, /* @tp_dict@ */ | |
2023 | 0, /* @tp_descr_get@ */ | |
2024 | 0, /* @tp_descr_set@ */ | |
2025 | 0, /* @tp_dictoffset@ */ | |
2026 | 0, /* @tp_init@ */ | |
2027 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
2028 | abstract_pynew, /* @tp_new@ */ | |
3aa33042 | 2029 | 0, /* @tp_free@ */ |
d7ab1bab | 2030 | 0 /* @tp_is_gc@ */ |
2031 | }; | |
2032 | ||
2033 | /*----- Message authentication --------------------------------------------*/ | |
2034 | ||
2035 | PyTypeObject *gcmac_pytype, *gmac_pytype, *gmhash_pytype; | |
2036 | ||
2037 | CONVFUNC(gcmac, gcmac *, GCMAC_CM) | |
2038 | CONVFUNC(gmac, gmac *, GMAC_M) | |
2039 | CONVFUNC(gmhash, ghash *, GHASH_H) | |
2040 | ||
2041 | static PyObject *gmac_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
2042 | { | |
827f89d7 | 2043 | static const char *const kwlist[] = { "k", 0 }; |
d7ab1bab | 2044 | char *k; |
6b54260d | 2045 | Py_ssize_t sz; |
d7ab1bab | 2046 | |
827f89d7 | 2047 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &k, &sz)) |
d7ab1bab | 2048 | goto end; |
2049 | if (keysz(sz, GCMAC_CM(ty)->keysz) != sz) VALERR("bad key length"); | |
2050 | return (gmac_pywrap((PyObject *)ty, | |
c41d0371 | 2051 | GM_KEY(GCMAC_CM(ty), k, sz))); |
d7ab1bab | 2052 | end: |
b2687a0a | 2053 | return (0); |
d7ab1bab | 2054 | } |
2055 | ||
2056 | static PyObject *gmhash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
2057 | { | |
827f89d7 | 2058 | static const char *const kwlist[] = { 0 }; |
d7ab1bab | 2059 | ghash_pyobj *g; |
2060 | ||
827f89d7 | 2061 | if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", KWLIST)) return (0); |
d7ab1bab | 2062 | g = PyObject_NEW(ghash_pyobj, ty); |
2063 | g->h = GM_INIT(GMAC_M(ty)); | |
d7ab1bab | 2064 | Py_INCREF(ty); |
2065 | return ((PyObject *)g); | |
2066 | } | |
2067 | ||
2068 | PyObject *gcmac_pywrap(gcmac *cm) | |
2069 | { | |
df9f8366 | 2070 | gcmac_pyobj *g = newtype(gcmac_pytype, 0, cm->name); |
d7ab1bab | 2071 | g->cm = cm; |
24b3d57b MW |
2072 | g->ty.ht_type.tp_basicsize = sizeof(gmac_pyobj); |
2073 | g->ty.ht_type.tp_base = gmac_pytype; | |
d7ab1bab | 2074 | Py_INCREF(gmac_pytype); |
24b3d57b MW |
2075 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
2076 | Py_TPFLAGS_BASETYPE | | |
2077 | Py_TPFLAGS_HEAPTYPE); | |
2078 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; | |
2079 | g->ty.ht_type.tp_free = 0; | |
2080 | g->ty.ht_type.tp_new = gmac_pynew; | |
dc075750 | 2081 | typeready(&g->ty.ht_type); |
d7ab1bab | 2082 | return ((PyObject *)g); |
2083 | } | |
2084 | ||
c41d0371 | 2085 | PyObject *gmac_pywrap(PyObject *cobj, gmac *m) |
d7ab1bab | 2086 | { |
2087 | gmac_pyobj *g; | |
2088 | if (!cobj) cobj = gcmac_pywrap((/*unconst*/ gcmac *)GM_CLASS(m)); | |
2089 | else Py_INCREF(cobj); | |
df9f8366 | 2090 | g = newtype((PyTypeObject *)cobj, 0, 0); |
828b1388 | 2091 | g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj); |
24b3d57b MW |
2092 | g->ty.ht_name = PyString_FromFormat("%s(keyed)", m->ops->c->name); |
2093 | g->ty.ht_type.tp_name = PyString_AS_STRING(g->ty.ht_name); | |
2094 | g->ty.ht_type.tp_base = gmhash_pytype; | |
d7ab1bab | 2095 | Py_INCREF(gmac_pytype); |
24b3d57b MW |
2096 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
2097 | Py_TPFLAGS_BASETYPE | | |
2098 | Py_TPFLAGS_HEAPTYPE); | |
2099 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; | |
2100 | g->ty.ht_type.tp_free = 0; | |
2101 | g->ty.ht_type.tp_new = gmhash_pynew; | |
dc075750 | 2102 | typeready(&g->ty.ht_type); |
d7ab1bab | 2103 | g->m = m; |
b2687a0a | 2104 | return ((PyObject *)g); |
d7ab1bab | 2105 | } |
2106 | ||
2107 | static void gmac_pydealloc(PyObject *me) | |
2108 | { | |
c41d0371 | 2109 | GM_DESTROY(GMAC_M(me)); |
d7ab1bab | 2110 | Py_DECREF(me->ob_type); |
d7ab1bab | 2111 | PyType_Type.tp_dealloc(me); |
2112 | } | |
2113 | ||
2114 | static PyObject *gcmget_name(PyObject *me, void *hunoz) | |
2115 | { return (PyString_FromString(GCMAC_CM(me)->name)); } | |
2116 | ||
2117 | static PyObject *gcmget_keysz(PyObject *me, void *hunoz) | |
2118 | { return (keysz_pywrap(GCMAC_CM(me)->keysz)); } | |
2119 | ||
2120 | static PyObject *gcmget_tagsz(PyObject *me, void *hunoz) | |
2121 | { return (PyInt_FromLong(GCMAC_CM(me)->hashsz)); } | |
2122 | ||
2123 | static PyGetSetDef gcmac_pygetset[] = { | |
2124 | #define GETSETNAME(op, name) gcm##op##_##name | |
2125 | GET (keysz, "CM.keysz -> acceptable key sizes") | |
2126 | GET (tagsz, "CM.tagsz -> MAC output size") | |
2127 | GET (name, "CM.name -> name of this kind of MAC") | |
2128 | #undef GETSETNAME | |
2129 | { 0 } | |
2130 | }; | |
2131 | ||
2132 | static PyTypeObject gcmac_pytype_skel = { | |
6d4db0bf | 2133 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 2134 | "GCMAC", /* @tp_name@ */ |
d7ab1bab | 2135 | sizeof(gchash_pyobj), /* @tp_basicsize@ */ |
2136 | 0, /* @tp_itemsize@ */ | |
2137 | ||
2138 | 0, /* @tp_dealloc@ */ | |
2139 | 0, /* @tp_print@ */ | |
2140 | 0, /* @tp_getattr@ */ | |
2141 | 0, /* @tp_setattr@ */ | |
2142 | 0, /* @tp_compare@ */ | |
2143 | 0, /* @tp_repr@ */ | |
2144 | 0, /* @tp_as_number@ */ | |
2145 | 0, /* @tp_as_sequence@ */ | |
2146 | 0, /* @tp_as_mapping@ */ | |
2147 | 0, /* @tp_hash@ */ | |
2148 | 0, /* @tp_call@ */ | |
2149 | 0, /* @tp_str@ */ | |
2150 | 0, /* @tp_getattro@ */ | |
2151 | 0, /* @tp_setattro@ */ | |
2152 | 0, /* @tp_as_buffer@ */ | |
2153 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
2154 | Py_TPFLAGS_BASETYPE, | |
2155 | ||
2156 | /* @tp_doc@ */ | |
2157 | "Message authentication code metametaclass.", | |
2158 | ||
2159 | 0, /* @tp_traverse@ */ | |
2160 | 0, /* @tp_clear@ */ | |
2161 | 0, /* @tp_richcompare@ */ | |
2162 | 0, /* @tp_weaklistoffset@ */ | |
2163 | 0, /* @tp_iter@ */ | |
963a6148 | 2164 | 0, /* @tp_iternext@ */ |
d7ab1bab | 2165 | 0, /* @tp_methods@ */ |
2166 | 0, /* @tp_members@ */ | |
2167 | gcmac_pygetset, /* @tp_getset@ */ | |
2168 | 0, /* @tp_base@ */ | |
2169 | 0, /* @tp_dict@ */ | |
2170 | 0, /* @tp_descr_get@ */ | |
2171 | 0, /* @tp_descr_set@ */ | |
2172 | 0, /* @tp_dictoffset@ */ | |
2173 | 0, /* @tp_init@ */ | |
2174 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
2175 | abstract_pynew, /* @tp_new@ */ | |
3aa33042 | 2176 | 0, /* @tp_free@ */ |
d7ab1bab | 2177 | 0 /* @tp_is_gc@ */ |
2178 | }; | |
2179 | ||
2180 | static PyTypeObject gmac_pytype_skel = { | |
6d4db0bf | 2181 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 2182 | "GMAC", /* @tp_name@ */ |
d7ab1bab | 2183 | sizeof(gmac_pyobj), /* @tp_basicsize@ */ |
2184 | 0, /* @tp_itemsize@ */ | |
2185 | ||
2186 | gmac_pydealloc, /* @tp_dealloc@ */ | |
2187 | 0, /* @tp_print@ */ | |
2188 | 0, /* @tp_getattr@ */ | |
2189 | 0, /* @tp_setattr@ */ | |
2190 | 0, /* @tp_compare@ */ | |
2191 | 0, /* @tp_repr@ */ | |
2192 | 0, /* @tp_as_number@ */ | |
2193 | 0, /* @tp_as_sequence@ */ | |
2194 | 0, /* @tp_as_mapping@ */ | |
2195 | 0, /* @tp_hash@ */ | |
2196 | 0, /* @tp_call@ */ | |
2197 | 0, /* @tp_str@ */ | |
2198 | 0, /* @tp_getattro@ */ | |
2199 | 0, /* @tp_setattro@ */ | |
2200 | 0, /* @tp_as_buffer@ */ | |
2201 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
2202 | Py_TPFLAGS_BASETYPE, | |
2203 | ||
2204 | /* @tp_doc@ */ | |
2205 | "Message authentication code metaclass, abstract base class.", | |
2206 | ||
2207 | 0, /* @tp_traverse@ */ | |
2208 | 0, /* @tp_clear@ */ | |
2209 | 0, /* @tp_richcompare@ */ | |
2210 | 0, /* @tp_weaklistoffset@ */ | |
2211 | 0, /* @tp_iter@ */ | |
963a6148 | 2212 | 0, /* @tp_iternext@ */ |
d7ab1bab | 2213 | 0, /* @tp_methods@ */ |
2214 | 0, /* @tp_members@ */ | |
2215 | 0, /* @tp_getset@ */ | |
2216 | 0, /* @tp_base@ */ | |
2217 | 0, /* @tp_dict@ */ | |
2218 | 0, /* @tp_descr_get@ */ | |
2219 | 0, /* @tp_descr_set@ */ | |
2220 | 0, /* @tp_dictoffset@ */ | |
2221 | 0, /* @tp_init@ */ | |
2222 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
2223 | abstract_pynew, /* @tp_new@ */ | |
3aa33042 | 2224 | 0, /* @tp_free@ */ |
d7ab1bab | 2225 | 0 /* @tp_is_gc@ */ |
2226 | }; | |
2227 | ||
2228 | static PyTypeObject gmhash_pytype_skel = { | |
6d4db0bf | 2229 | PyObject_HEAD_INIT(0) 0, /* Header */ |
c461c9b3 | 2230 | "GMACHash", /* @tp_name@ */ |
d7ab1bab | 2231 | sizeof(ghash_pyobj), /* @tp_basicsize@ */ |
2232 | 0, /* @tp_itemsize@ */ | |
2233 | ||
2234 | ghash_pydealloc, /* @tp_dealloc@ */ | |
2235 | 0, /* @tp_print@ */ | |
2236 | 0, /* @tp_getattr@ */ | |
2237 | 0, /* @tp_setattr@ */ | |
2238 | 0, /* @tp_compare@ */ | |
2239 | 0, /* @tp_repr@ */ | |
2240 | 0, /* @tp_as_number@ */ | |
2241 | 0, /* @tp_as_sequence@ */ | |
2242 | 0, /* @tp_as_mapping@ */ | |
2243 | 0, /* @tp_hash@ */ | |
2244 | 0, /* @tp_call@ */ | |
2245 | 0, /* @tp_str@ */ | |
2246 | 0, /* @tp_getattro@ */ | |
2247 | 0, /* @tp_setattro@ */ | |
2248 | 0, /* @tp_as_buffer@ */ | |
2249 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
2250 | Py_TPFLAGS_BASETYPE, | |
2251 | ||
2252 | /* @tp_doc@ */ | |
2253 | "Message authentication code, abstract base class.", | |
2254 | ||
2255 | 0, /* @tp_traverse@ */ | |
2256 | 0, /* @tp_clear@ */ | |
2257 | 0, /* @tp_richcompare@ */ | |
2258 | 0, /* @tp_weaklistoffset@ */ | |
2259 | 0, /* @tp_iter@ */ | |
963a6148 | 2260 | 0, /* @tp_iternext@ */ |
d7ab1bab | 2261 | 0, /* @tp_methods@ */ |
2262 | 0, /* @tp_members@ */ | |
2263 | 0, /* @tp_getset@ */ | |
2264 | 0, /* @tp_base@ */ | |
2265 | 0, /* @tp_dict@ */ | |
2266 | 0, /* @tp_descr_get@ */ | |
2267 | 0, /* @tp_descr_set@ */ | |
2268 | 0, /* @tp_dictoffset@ */ | |
2269 | 0, /* @tp_init@ */ | |
2270 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
2271 | abstract_pynew, /* @tp_new@ */ | |
3aa33042 | 2272 | 0, /* @tp_free@ */ |
d7ab1bab | 2273 | 0 /* @tp_is_gc@ */ |
2274 | }; | |
2275 | ||
204d480b MW |
2276 | /*----- Special snowflake for Poly1305 ------------------------------------*/ |
2277 | ||
2278 | PyTypeObject *poly1305cls_pytype, *poly1305key_pytype, *poly1305hash_pytype; | |
2279 | ||
2280 | typedef struct poly1305key_pyobj { | |
2281 | PyHeapTypeObject ty; | |
2282 | poly1305_key k; | |
2283 | } poly1305key_pyobj; | |
2284 | ||
2285 | typedef struct poly1305hash_pyobj { | |
2286 | PyObject_HEAD | |
2287 | unsigned f; | |
2288 | #define f_mask 1u | |
2289 | poly1305_ctx ctx; | |
2290 | } poly1305hash_pyobj; | |
2291 | ||
2292 | #define P1305_F(o) (((poly1305hash_pyobj *)(o))->f) | |
2293 | #define P1305_CTX(o) (&((poly1305hash_pyobj *)(o))->ctx) | |
2294 | CONVFUNC(poly1305hash, poly1305_ctx *, P1305_CTX) | |
2295 | ||
2296 | static PyObject *poly1305hash_pynew(PyTypeObject *ty, | |
2297 | PyObject *arg, PyObject *kw) | |
2298 | { | |
827f89d7 | 2299 | static const char *const kwlist[] = { "mask", 0 }; |
204d480b MW |
2300 | poly1305key_pyobj *pk = (poly1305key_pyobj *)ty; |
2301 | poly1305hash_pyobj *ph; | |
2302 | char *m = 0; | |
6b54260d | 2303 | Py_ssize_t sz; |
204d480b | 2304 | |
827f89d7 | 2305 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|s#:new", KWLIST, &m, &sz)) |
204d480b MW |
2306 | return (0); |
2307 | if (m && sz != POLY1305_MASKSZ) VALERR("bad mask length"); | |
2308 | ph = PyObject_NEW(poly1305hash_pyobj, ty); | |
2309 | ph->f = 0; | |
2310 | if (m) ph->f |= f_mask; | |
2311 | poly1305_macinit(&ph->ctx, &pk->k, m); | |
2312 | Py_INCREF(ty); | |
2313 | return ((PyObject *)ph); | |
2314 | end: | |
2315 | return (0); | |
2316 | } | |
2317 | ||
2318 | static PyObject *poly1305key_pynew(PyTypeObject *ty, | |
2319 | PyObject *arg, PyObject *kw) | |
2320 | { | |
827f89d7 | 2321 | static const char *const kwlist[] = { "k", 0 }; |
204d480b MW |
2322 | poly1305key_pyobj *pk; |
2323 | char *k; | |
6b54260d | 2324 | Py_ssize_t sz; |
204d480b | 2325 | |
827f89d7 | 2326 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &k, &sz)) |
204d480b MW |
2327 | goto end; |
2328 | if (keysz(sz, poly1305_keysz) != sz) VALERR("bad key length"); | |
2329 | ||
2330 | pk = newtype(ty, 0, 0); | |
2331 | pk->ty.ht_name = PyString_FromString("poly1305(keyed)"); | |
2332 | pk->ty.ht_type.tp_basicsize = sizeof(poly1305hash_pyobj); | |
2333 | pk->ty.ht_type.tp_name = PyString_AS_STRING(pk->ty.ht_name); | |
2334 | pk->ty.ht_type.tp_base = poly1305hash_pytype; | |
2335 | Py_INCREF(poly1305key_pytype); | |
2336 | pk->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | | |
2337 | Py_TPFLAGS_BASETYPE | | |
2338 | Py_TPFLAGS_HEAPTYPE); | |
2339 | pk->ty.ht_type.tp_alloc = PyType_GenericAlloc; | |
2340 | pk->ty.ht_type.tp_free = 0; | |
2341 | pk->ty.ht_type.tp_new = poly1305hash_pynew; | |
2342 | typeready(&pk->ty.ht_type); | |
2343 | ||
2344 | poly1305_keyinit(&pk->k, k, sz); | |
2345 | return ((PyObject *)pk); | |
2346 | ||
2347 | end: | |
2348 | return (0); | |
2349 | } | |
2350 | ||
2351 | static PyObject *poly1305clsget_name(PyObject *me, void *hunoz) | |
2352 | { return (PyString_FromString("poly1305")); } | |
2353 | ||
2354 | static PyObject *poly1305clsget_keysz(PyObject *me, void *hunoz) | |
2355 | { return (keysz_pywrap(poly1305_keysz)); } | |
2356 | ||
2357 | static PyObject *poly1305clsget_masksz(PyObject *me, void *hunoz) | |
2358 | { return (PyInt_FromLong(POLY1305_MASKSZ)); } | |
2359 | ||
2360 | static PyObject *poly1305clsget_tagsz(PyObject *me, void *hunoz) | |
2361 | { return (PyInt_FromLong(POLY1305_TAGSZ)); } | |
2362 | ||
2363 | static PyObject *polymeth_copy(PyObject *me, PyObject *arg) | |
2364 | { | |
2365 | poly1305hash_pyobj *ph; | |
2366 | if (!PyArg_ParseTuple(arg, ":copy")) return (0); | |
2367 | ph = PyObject_NEW(poly1305hash_pyobj, me->ob_type); | |
2368 | poly1305_copy(&ph->ctx, P1305_CTX(me)); | |
2369 | Py_INCREF(me->ob_type); | |
2370 | return ((PyObject *)ph); | |
2371 | } | |
2372 | ||
2373 | static PyObject *polymeth_hash(PyObject *me, PyObject *arg) | |
2374 | { | |
2375 | char *p; | |
6b54260d | 2376 | Py_ssize_t sz; |
204d480b MW |
2377 | if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0); |
2378 | poly1305_hash(P1305_CTX(me), p, sz); | |
2379 | RETURN_ME; | |
2380 | } | |
2381 | ||
2382 | #define POLYMETH_HASHU_(n, W, w) \ | |
2383 | static PyObject *polymeth_hashu##w(PyObject *me, PyObject *arg) \ | |
2384 | { \ | |
2385 | uint##n x; \ | |
2386 | octet b[SZ_##W]; \ | |
0e5c668c | 2387 | if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \ |
0c87e818 | 2388 | STORE##W(b, x); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \ |
204d480b | 2389 | RETURN_ME; \ |
204d480b MW |
2390 | } |
2391 | DOUINTCONV(POLYMETH_HASHU_) | |
2392 | ||
2393 | #define POLYMETH_HASHBUF_(n, W, w) \ | |
2394 | static PyObject *polymeth_hashbuf##w(PyObject *me, PyObject *arg) \ | |
2395 | { \ | |
2396 | char *p; \ | |
6b54260d | 2397 | Py_ssize_t sz; \ |
204d480b MW |
2398 | octet b[SZ_##W]; \ |
2399 | if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \ | |
2400 | if (sz > MASK##n) TYERR("string too long"); \ | |
0c87e818 | 2401 | STORE##W(b, sz); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \ |
204d480b MW |
2402 | poly1305_hash(P1305_CTX(me), p, sz); \ |
2403 | RETURN_ME; \ | |
2404 | end: \ | |
2405 | return (0); \ | |
2406 | } | |
2407 | DOUINTCONV(POLYMETH_HASHBUF_) | |
2408 | ||
2409 | static PyObject *polymeth_hashstrz(PyObject *me, PyObject *arg) | |
2410 | { | |
2411 | char *p; | |
2412 | if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0); | |
2413 | poly1305_hash(P1305_CTX(me), p, strlen(p) + 1); | |
2414 | RETURN_ME; | |
2415 | } | |
2416 | ||
2417 | static PyObject *polymeth_flush(PyObject *me, PyObject *arg) | |
2418 | { | |
2419 | if (!PyArg_ParseTuple(arg, ":flush")) return (0); | |
2420 | poly1305_flush(P1305_CTX(me)); | |
2421 | RETURN_ME; | |
2422 | } | |
2423 | ||
5c17375a MW |
2424 | static PyObject *polymeth_flushzero(PyObject *me, PyObject *arg) |
2425 | { | |
2426 | if (!PyArg_ParseTuple(arg, ":flushzero")) return (0); | |
2427 | poly1305_flushzero(P1305_CTX(me)); | |
2428 | RETURN_ME; | |
2429 | } | |
2430 | ||
204d480b MW |
2431 | static PyObject *polymeth_concat(PyObject *me, PyObject *arg) |
2432 | { | |
2433 | PyObject *pre, *suff; | |
2434 | if (!PyArg_ParseTuple(arg, "OO:concat", &pre, &suff)) return (0); | |
2435 | if (!PyObject_TypeCheck(pre, poly1305hash_pytype) || | |
2436 | !PyObject_TypeCheck(suff, poly1305hash_pytype)) | |
2437 | TYERR("wanted a poly1305hash"); | |
2438 | if (me->ob_type != pre->ob_type || me->ob_type != suff->ob_type) | |
2439 | TYERR("key mismatch"); | |
2440 | if (P1305_CTX(pre)->nbuf) VALERR("prefix is not block-aligned"); | |
2441 | poly1305_concat(P1305_CTX(me), P1305_CTX(pre), P1305_CTX(suff)); | |
2442 | RETURN_ME; | |
2443 | end: | |
2444 | return (0); | |
2445 | } | |
2446 | ||
2447 | static PyObject *polymeth_done(PyObject *me, PyObject *arg) | |
2448 | { | |
2449 | PyObject *rc; | |
2450 | if (!PyArg_ParseTuple(arg, ":done")) return (0); | |
2451 | if (!(P1305_F(me) & f_mask)) VALERR("no mask"); | |
2452 | rc = bytestring_pywrap(0, POLY1305_TAGSZ); | |
2453 | poly1305_done(P1305_CTX(me), PyString_AS_STRING(rc)); | |
2454 | return (rc); | |
2455 | end: | |
2456 | return (0); | |
2457 | } | |
2458 | ||
2459 | static PyGetSetDef poly1305cls_pygetset[] = { | |
2460 | #define GETSETNAME(op, name) poly1305cls##op##_##name | |
2461 | GET (keysz, "PC.keysz -> acceptable key sizes") | |
2462 | GET (masksz, "PC.masksz -> mask size") | |
2463 | GET (tagsz, "PC.tagsz -> MAC output size") | |
2464 | GET (name, "PC.name -> name of this kind of MAC") | |
2465 | #undef GETSETNAME | |
2466 | { 0 } | |
2467 | }; | |
2468 | ||
2469 | static PyMethodDef poly1305hash_pymethods[] = { | |
2470 | #define METHNAME(name) polymeth_##name | |
2471 | METH (copy, "P.copy() -> PP") | |
2472 | METH (hash, "P.hash(M)") | |
2473 | #define METHU_(n, W, w) METH(hashu##w, "P.hashu" #w "(WORD)") | |
2474 | DOUINTCONV(METHU_) | |
2475 | #undef METHU_ | |
2476 | #define METHBUF_(n, W, w) METH(hashbuf##w, "P.hashbuf" #w "(BYTES)") | |
2477 | DOUINTCONV(METHBUF_) | |
2478 | #undef METHBUF_ | |
2479 | METH (hashstrz, "P.hashstrz(STRING)") | |
2480 | METH (flush, "P.flush()") | |
5c17375a | 2481 | METH (flushzero, "P.flushzero()") |
204d480b MW |
2482 | METH (concat, "P.concat(PREFIX, SUFFIX)") |
2483 | METH (done, "P.done() -> TAG") | |
2484 | #undef METHNAME | |
2485 | { 0 } | |
2486 | }; | |
2487 | ||
2488 | static PyTypeObject poly1305cls_pytype_skel = { | |
2489 | PyObject_HEAD_INIT(0) 0, /* Header */ | |
2490 | "Poly1305Class", /* @tp_name@ */ | |
2491 | sizeof(PyHeapTypeObject), /* @tp_basicsize@ */ | |
2492 | 0, /* @tp_itemsize@ */ | |
2493 | ||
2494 | 0, /* @tp_dealloc@ */ | |
2495 | 0, /* @tp_print@ */ | |
2496 | 0, /* @tp_getattr@ */ | |
2497 | 0, /* @tp_setattr@ */ | |
2498 | 0, /* @tp_compare@ */ | |
2499 | 0, /* @tp_repr@ */ | |
2500 | 0, /* @tp_as_number@ */ | |
2501 | 0, /* @tp_as_sequence@ */ | |
2502 | 0, /* @tp_as_mapping@ */ | |
2503 | 0, /* @tp_hash@ */ | |
2504 | 0, /* @tp_call@ */ | |
2505 | 0, /* @tp_str@ */ | |
2506 | 0, /* @tp_getattro@ */ | |
2507 | 0, /* @tp_setattro@ */ | |
2508 | 0, /* @tp_as_buffer@ */ | |
2509 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
2510 | Py_TPFLAGS_BASETYPE, | |
2511 | ||
2512 | /* @tp_doc@ */ | |
2513 | "Poly1305 metametaclass. Best not to ask.", | |
2514 | ||
2515 | 0, /* @tp_traverse@ */ | |
2516 | 0, /* @tp_clear@ */ | |
2517 | 0, /* @tp_richcompare@ */ | |
2518 | 0, /* @tp_weaklistoffset@ */ | |
2519 | 0, /* @tp_iter@ */ | |
2520 | 0, /* @tp_iternext@ */ | |
2521 | 0, /* @tp_methods@ */ | |
2522 | 0, /* @tp_members@ */ | |
2523 | poly1305cls_pygetset, /* @tp_getset@ */ | |
2524 | 0, /* @tp_base@ */ | |
2525 | 0, /* @tp_dict@ */ | |
2526 | 0, /* @tp_descr_get@ */ | |
2527 | 0, /* @tp_descr_set@ */ | |
2528 | 0, /* @tp_dictoffset@ */ | |
2529 | 0, /* @tp_init@ */ | |
2530 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
2531 | abstract_pynew, /* @tp_new@ */ | |
2532 | 0, /* @tp_free@ */ | |
2533 | 0 /* @tp_is_gc@ */ | |
2534 | }; | |
2535 | ||
2536 | static PyTypeObject poly1305key_pytype_skel = { | |
2537 | PyObject_HEAD_INIT(0) 0, /* Header */ | |
2538 | "poly1305", /* @tp_name@ */ | |
2539 | sizeof(poly1305key_pyobj), /* @tp_basicsize@ */ | |
2540 | 0, /* @tp_itemsize@ */ | |
2541 | ||
2542 | 0, /* @tp_dealloc@ */ | |
2543 | 0, /* @tp_print@ */ | |
2544 | 0, /* @tp_getattr@ */ | |
2545 | 0, /* @tp_setattr@ */ | |
2546 | 0, /* @tp_compare@ */ | |
2547 | 0, /* @tp_repr@ */ | |
2548 | 0, /* @tp_as_number@ */ | |
2549 | 0, /* @tp_as_sequence@ */ | |
2550 | 0, /* @tp_as_mapping@ */ | |
2551 | 0, /* @tp_hash@ */ | |
2552 | 0, /* @tp_call@ */ | |
2553 | 0, /* @tp_str@ */ | |
2554 | 0, /* @tp_getattro@ */ | |
2555 | 0, /* @tp_setattro@ */ | |
2556 | 0, /* @tp_as_buffer@ */ | |
2557 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
2558 | Py_TPFLAGS_BASETYPE, | |
2559 | ||
2560 | /* @tp_doc@ */ | |
06cd26e8 | 2561 | "poly1305(K): Poly1305 key.", |
204d480b MW |
2562 | |
2563 | 0, /* @tp_traverse@ */ | |
2564 | 0, /* @tp_clear@ */ | |
2565 | 0, /* @tp_richcompare@ */ | |
2566 | 0, /* @tp_weaklistoffset@ */ | |
2567 | 0, /* @tp_iter@ */ | |
2568 | 0, /* @tp_iternext@ */ | |
2569 | 0, /* @tp_methods@ */ | |
2570 | 0, /* @tp_members@ */ | |
2571 | 0, /* @tp_getset@ */ | |
2572 | 0, /* @tp_base@ */ | |
2573 | 0, /* @tp_dict@ */ | |
2574 | 0, /* @tp_descr_get@ */ | |
2575 | 0, /* @tp_descr_set@ */ | |
2576 | 0, /* @tp_dictoffset@ */ | |
2577 | 0, /* @tp_init@ */ | |
2578 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
2579 | poly1305key_pynew, /* @tp_new@ */ | |
2580 | 0, /* @tp_free@ */ | |
2581 | 0 /* @tp_is_gc@ */ | |
2582 | }; | |
2583 | ||
2584 | static PyTypeObject poly1305hash_pytype_skel = { | |
2585 | PyObject_HEAD_INIT(0) 0, /* Header */ | |
2586 | "Poly1305Hash", /* @tp_name@ */ | |
2587 | sizeof(poly1305hash_pyobj), /* @tp_basicsize@ */ | |
2588 | 0, /* @tp_itemsize@ */ | |
2589 | ||
2590 | 0, /* @tp_dealloc@ */ | |
2591 | 0, /* @tp_print@ */ | |
2592 | 0, /* @tp_getattr@ */ | |
2593 | 0, /* @tp_setattr@ */ | |
2594 | 0, /* @tp_compare@ */ | |
2595 | 0, /* @tp_repr@ */ | |
2596 | 0, /* @tp_as_number@ */ | |
2597 | 0, /* @tp_as_sequence@ */ | |
2598 | 0, /* @tp_as_mapping@ */ | |
2599 | 0, /* @tp_hash@ */ | |
2600 | 0, /* @tp_call@ */ | |
2601 | 0, /* @tp_str@ */ | |
2602 | 0, /* @tp_getattro@ */ | |
2603 | 0, /* @tp_setattro@ */ | |
2604 | 0, /* @tp_as_buffer@ */ | |
2605 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
2606 | Py_TPFLAGS_BASETYPE, | |
2607 | ||
2608 | /* @tp_doc@ */ | |
2609 | "Poly1305 MAC context base class.", | |
2610 | ||
2611 | 0, /* @tp_traverse@ */ | |
2612 | 0, /* @tp_clear@ */ | |
2613 | 0, /* @tp_richcompare@ */ | |
2614 | 0, /* @tp_weaklistoffset@ */ | |
2615 | 0, /* @tp_iter@ */ | |
2616 | 0, /* @tp_iternext@ */ | |
2617 | poly1305hash_pymethods, /* @tp_methods@ */ | |
2618 | 0, /* @tp_members@ */ | |
2619 | 0, /* @tp_getset@ */ | |
2620 | 0, /* @tp_base@ */ | |
2621 | 0, /* @tp_dict@ */ | |
2622 | 0, /* @tp_descr_get@ */ | |
2623 | 0, /* @tp_descr_set@ */ | |
2624 | 0, /* @tp_dictoffset@ */ | |
2625 | 0, /* @tp_init@ */ | |
2626 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
2627 | abstract_pynew, /* @tp_new@ */ | |
2628 | 0, /* @tp_free@ */ | |
2629 | 0 /* @tp_is_gc@ */ | |
2630 | }; | |
2631 | ||
a75e68c9 MW |
2632 | /*----- Special snowflake for HSalsa and HChaCha --------------------------*/ |
2633 | ||
2634 | #define DEF_HDANCE(DANCE, HDANCE, dance, hdance) \ | |
2635 | static PyObject *meth_##hdance##_prf(PyObject *me, PyObject *arg) \ | |
2636 | { \ | |
2637 | dance##_ctx dance; \ | |
2638 | char *k, *n; \ | |
6b54260d | 2639 | Py_ssize_t ksz, nsz; \ |
a75e68c9 MW |
2640 | PyObject *rc; \ |
2641 | if (!PyArg_ParseTuple(arg, "s#s#:" #hdance "_prf", \ | |
2642 | &k, &ksz, &n, &nsz)) \ | |
2643 | goto end; \ | |
0b7c311a | 2644 | if (ksz != keysz(ksz, dance##_keysz)) VALERR("bad key length"); \ |
a75e68c9 MW |
2645 | if (nsz != HDANCE##_INSZ) VALERR("bad input length"); \ |
2646 | rc = bytestring_pywrap(0, HSALSA20_OUTSZ); \ | |
2647 | dance##_init(&dance, k, ksz, 0); \ | |
2648 | hdance##_prf(&dance, n, PyString_AS_STRING(rc)); \ | |
2649 | return (rc); \ | |
2650 | end: \ | |
2651 | return (0); \ | |
2652 | } | |
2653 | ||
2654 | DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa20) | |
2655 | DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa2012) | |
2656 | DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa208) | |
2657 | ||
2658 | DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha20) | |
2659 | DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha12) | |
2660 | DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha8) | |
204d480b | 2661 | |
b35fdbe6 MW |
2662 | /*----- Keccak-p[1600, n] -------------------------------------------------*/ |
2663 | ||
2664 | static PyTypeObject *kxvik_pytype; | |
2665 | ||
2666 | typedef struct kxvik_pyobj { | |
2667 | PyObject_HEAD | |
2668 | keccak1600_state s; | |
2669 | unsigned n; | |
2670 | } kxvik_pyobj; | |
2671 | ||
8d2dd9dc | 2672 | static PyObject *kxvik_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) |
b35fdbe6 MW |
2673 | { |
2674 | unsigned n = 24; | |
2675 | kxvik_pyobj *rc = 0; | |
827f89d7 MW |
2676 | static const char *const kwlist[] = { "nround", 0 }; |
2677 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", KWLIST, | |
b35fdbe6 MW |
2678 | convuint, &n)) |
2679 | goto end; | |
2680 | rc = (kxvik_pyobj *)ty->tp_alloc(ty, 0); | |
2681 | rc->n = n; | |
2682 | keccak1600_init(&rc->s); | |
2683 | end: | |
2684 | return ((PyObject *)rc); | |
2685 | } | |
2686 | ||
cfe80e51 MW |
2687 | static PyObject *kxvikmeth_copy(PyObject *me, PyObject *arg) |
2688 | { | |
2689 | kxvik_pyobj *k = (kxvik_pyobj *)me, *rc = 0; | |
2690 | if (!PyArg_ParseTuple(arg, ":copy")) goto end; | |
2691 | rc = (kxvik_pyobj *)k->ob_type->tp_alloc(k->ob_type, 0); | |
2692 | rc->s = k->s; rc->n = k->n; | |
2693 | end: | |
2694 | return ((PyObject *)rc); | |
2695 | } | |
2696 | ||
b35fdbe6 MW |
2697 | static PyObject *kxvikmeth_mix(PyObject *me, PyObject *arg) |
2698 | { | |
2699 | kxvik_pyobj *k = (kxvik_pyobj *)me; | |
2700 | kludge64 t[25]; | |
2701 | const octet *q; | |
2702 | octet buf[8]; | |
2703 | unsigned i; | |
2704 | char *p; Py_ssize_t n; | |
2705 | ||
2706 | if (!PyArg_ParseTuple(arg, "s#:mix", &p, &n)) goto end; | |
2707 | if (n > 200) VALERR("out of range"); | |
2708 | q = (const octet *)p; | |
2709 | i = 0; | |
2710 | while (n > 8) { LOAD64_L_(t[i], q); i++; q += 8; n -= 8; } | |
2711 | if (n) { | |
2712 | memcpy(buf, q, n); memset(buf + n, 0, 8 - n); | |
2713 | LOAD64_L_(t[i], buf); i++; | |
2714 | } | |
2715 | keccak1600_mix(&k->s, t, i); | |
2716 | RETURN_ME; | |
2717 | end: | |
2718 | return (0); | |
2719 | } | |
2720 | ||
2721 | static PyObject *kxvikmeth_extract(PyObject *me, PyObject *arg) | |
2722 | { | |
2723 | kxvik_pyobj *k = (kxvik_pyobj *)me; | |
2724 | PyObject *rc = 0; | |
2725 | kludge64 t[25]; | |
2726 | octet *q, buf[8]; | |
2727 | unsigned i; | |
2728 | unsigned n; | |
2729 | ||
ae5dbbad | 2730 | if (!PyArg_ParseTuple(arg, "O&:extract", convuint, &n)) goto end; |
b35fdbe6 MW |
2731 | if (n > 200) VALERR("out of range"); |
2732 | rc = bytestring_pywrap(0, n); | |
2733 | q = (octet *)PyString_AS_STRING(rc); | |
2734 | keccak1600_extract(&k->s, t, (n + 7)/8); | |
2735 | i = 0; | |
2736 | while (n > 8) { STORE64_L_(q, t[i]); i++; q += 8; n -= 8; } | |
2737 | if (n) { STORE64_L_(buf, t[i]); memcpy(q, buf, n); } | |
2738 | end: | |
2739 | return (rc); | |
2740 | } | |
2741 | ||
2742 | static PyObject *kxvikmeth_step(PyObject *me, PyObject *arg) | |
2743 | { | |
2744 | kxvik_pyobj *k = (kxvik_pyobj *)me; | |
2745 | if (!PyArg_ParseTuple(arg, ":step")) return (0); | |
2746 | keccak1600_p(&k->s, &k->s, k->n); | |
2747 | RETURN_ME; | |
2748 | } | |
2749 | ||
2750 | static PyObject *kxvikget_nround(PyObject *me, void *hunoz) | |
2751 | { | |
2752 | kxvik_pyobj *k = (kxvik_pyobj *)me; | |
2753 | return (PyInt_FromLong(k->n)); | |
2754 | } | |
2755 | ||
2756 | static int kxvikset_nround(PyObject *me, PyObject *val, void *hunoz) | |
2757 | { | |
2758 | kxvik_pyobj *k = (kxvik_pyobj *)me; | |
2759 | unsigned n; | |
d26c1ca8 | 2760 | int rc = -1; |
b35fdbe6 | 2761 | |
d26c1ca8 MW |
2762 | if (!val) NIERR("__del__"); |
2763 | if (!convuint(val, &n)) goto end; | |
b35fdbe6 | 2764 | k->n = n; |
d26c1ca8 MW |
2765 | rc = 0; |
2766 | end: | |
2767 | return (rc); | |
b35fdbe6 MW |
2768 | } |
2769 | ||
2770 | static PyGetSetDef kxvik_pygetset[] = { | |
2771 | #define GETSETNAME(op, name) kxvik##op##_##name | |
2772 | GETSET(nround, "KECCAK.nround -> number of rounds") | |
2773 | #undef GETSETNAME | |
2774 | { 0 } | |
2775 | }; | |
2776 | ||
2777 | static PyMethodDef kxvik_pymethods[] = { | |
2778 | #define METHNAME(func) kxvikmeth_##func | |
cfe80e51 | 2779 | METH (copy, "KECCAK.copy() -> KECCAK'") |
b35fdbe6 MW |
2780 | METH (mix, "KECCAK.mix(DATA)") |
2781 | METH (extract, "KECCAK.extract(NOCTETS)") | |
2782 | METH (step, "KECCAK.step()") | |
2783 | #undef METHNAME | |
2784 | { 0 } | |
2785 | }; | |
2786 | ||
2787 | static PyTypeObject kxvik_pytype_skel = { | |
2788 | PyObject_HEAD_INIT(0) 0, /* Header */ | |
2789 | "Keccak1600", /* @tp_name@ */ | |
2790 | sizeof(kxvik_pyobj), /* @tp_basicsize@ */ | |
2791 | 0, /* @tp_itemsize@ */ | |
2792 | ||
2793 | 0, /* @tp_dealloc@ */ | |
2794 | 0, /* @tp_print@ */ | |
2795 | 0, /* @tp_getattr@ */ | |
2796 | 0, /* @tp_setattr@ */ | |
2797 | 0, /* @tp_compare@ */ | |
2798 | 0, /* @tp_repr@ */ | |
2799 | 0, /* @tp_as_number@ */ | |
2800 | 0, /* @tp_as_sequence@ */ | |
2801 | 0, /* @tp_as_mapping@ */ | |
2802 | 0, /* @tp_hash@ */ | |
2803 | 0, /* @tp_call@ */ | |
2804 | 0, /* @tp_str@ */ | |
2805 | 0, /* @tp_getattro@ */ | |
2806 | 0, /* @tp_setattro@ */ | |
2807 | 0, /* @tp_as_buffer@ */ | |
2808 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
2809 | Py_TPFLAGS_BASETYPE, | |
2810 | ||
2811 | /* @tp_doc@ */ | |
06cd26e8 | 2812 | "Keccak1600([nround = 24]): Keccak-p[1600, n] state.", |
b35fdbe6 MW |
2813 | |
2814 | 0, /* @tp_traverse@ */ | |
2815 | 0, /* @tp_clear@ */ | |
2816 | 0, /* @tp_richcompare@ */ | |
2817 | 0, /* @tp_weaklistoffset@ */ | |
2818 | 0, /* @tp_iter@ */ | |
2819 | 0, /* @tp_iternext@ */ | |
2820 | kxvik_pymethods, /* @tp_methods@ */ | |
2821 | 0, /* @tp_members@ */ | |
2822 | kxvik_pygetset, /* @tp_getset@ */ | |
2823 | 0, /* @tp_base@ */ | |
2824 | 0, /* @tp_dict@ */ | |
2825 | 0, /* @tp_descr_get@ */ | |
2826 | 0, /* @tp_descr_set@ */ | |
2827 | 0, /* @tp_dictoffset@ */ | |
2828 | 0, /* @tp_init@ */ | |
2829 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
2830 | kxvik_pynew, /* @tp_new@ */ | |
2831 | 0, /* @tp_free@ */ | |
2832 | 0 /* @tp_is_gc@ */ | |
2833 | }; | |
2834 | ||
6bd22b53 MW |
2835 | static PyTypeObject *shake_pytype, *shake128_pytype, *shake256_pytype; |
2836 | ||
2837 | typedef struct shake_pyobj { | |
2838 | PyObject_HEAD | |
2839 | int st; | |
2840 | shake_ctx h; | |
2841 | } shake_pyobj; | |
2842 | ||
2843 | #define SHAKE_H(o) (&((shake_pyobj *)(o))->h) | |
2844 | #define SHAKE_ST(o) (((shake_pyobj *)(o))->st) | |
2845 | ||
2846 | static PyObject *shake_dopynew(void (*initfn)(shake_ctx *, | |
2847 | const void *, size_t, | |
2848 | const void *, size_t), | |
2849 | PyTypeObject *ty, | |
2850 | PyObject *arg, PyObject *kw) | |
2851 | { | |
2852 | shake_pyobj *rc = 0; | |
2853 | char *p = 0, *f = 0; | |
2854 | Py_ssize_t psz = 0, fsz = 0; | |
827f89d7 | 2855 | static const char *const kwlist[] = { "perso", "func", 0 }; |
6bd22b53 | 2856 | |
827f89d7 | 2857 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|s#s#:new", KWLIST, |
6bd22b53 MW |
2858 | &p, &psz, &f, &fsz)) |
2859 | goto end; | |
2860 | rc = (shake_pyobj *)ty->tp_alloc(ty, 0); | |
2861 | initfn(&rc->h, f, fsz, p, psz); | |
2862 | rc->st = 0; | |
2863 | end: | |
2864 | return ((PyObject *)rc); | |
2865 | } | |
2866 | ||
2867 | static PyObject *shake128_pynew(PyTypeObject *ty, | |
2868 | PyObject *arg, PyObject *kw) | |
2869 | { return (shake_dopynew(cshake128_init, ty, arg, kw)); } | |
2870 | ||
2871 | static PyObject *shake256_pynew(PyTypeObject *ty, | |
2872 | PyObject *arg, PyObject *kw) | |
2873 | { return (shake_dopynew(cshake256_init, ty, arg, kw)); } | |
2874 | ||
2875 | static int shake_check(PyObject *me, int st) | |
2876 | { | |
2877 | if (SHAKE_ST(me) != st) VALERR("wrong state"); | |
2878 | return (0); | |
2879 | end: | |
2880 | return (-1); | |
2881 | } | |
2882 | ||
2883 | static PyObject *shakemeth_hash(PyObject *me, PyObject *arg) | |
2884 | { | |
2885 | char *p; | |
2886 | Py_ssize_t sz; | |
2887 | if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0); | |
2888 | if (shake_check(me, 0)) return (0); | |
2889 | shake_hash(SHAKE_H(me), p, sz); | |
2890 | RETURN_ME; | |
2891 | } | |
2892 | ||
2893 | #define SHAKEMETH_HASHU_(n, W, w) \ | |
2894 | static PyObject *shakemeth_hashu##w(PyObject *me, PyObject *arg) \ | |
2895 | { \ | |
2896 | uint##n x; \ | |
2897 | octet b[SZ_##W]; \ | |
0e5c668c MW |
2898 | if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \ |
2899 | if (shake_check(me, 0)) return (0); \ | |
6bd22b53 MW |
2900 | STORE##W(b, x); shake_hash(SHAKE_H(me), b, sizeof(b)); \ |
2901 | RETURN_ME; \ | |
6bd22b53 MW |
2902 | } |
2903 | DOUINTCONV(SHAKEMETH_HASHU_) | |
2904 | ||
2905 | #define SHAKEMETH_HASHBUF_(n, W, w) \ | |
2906 | static PyObject *shakemeth_hashbuf##w(PyObject *me, PyObject *arg) \ | |
2907 | { \ | |
2908 | char *p; \ | |
2909 | Py_ssize_t sz; \ | |
2910 | octet b[SZ_##W]; \ | |
2911 | if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \ | |
2912 | if (sz > MASK##n) TYERR("string too long"); \ | |
2913 | if (shake_check(me, 0)) goto end; \ | |
2914 | STORE##W(b, sz); shake_hash(SHAKE_H(me), b, sizeof(b)); \ | |
2915 | shake_hash(SHAKE_H(me), p, sz); \ | |
2916 | RETURN_ME; \ | |
2917 | end: \ | |
2918 | return (0); \ | |
2919 | } | |
2920 | DOUINTCONV(SHAKEMETH_HASHBUF_) | |
2921 | ||
2922 | static PyObject *shakemeth_hashstrz(PyObject *me, PyObject *arg) | |
2923 | { | |
2924 | char *p; | |
2925 | if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0); | |
2926 | if (shake_check(me, 0)) return (0); | |
2927 | shake_hash(SHAKE_H(me), p, strlen(p) + 1); | |
2928 | RETURN_ME; | |
2929 | } | |
2930 | ||
2931 | static PyObject *shakemeth_xof(PyObject *me, PyObject *arg) | |
2932 | { | |
2933 | if (!PyArg_ParseTuple(arg, ":xof")) goto end; | |
2934 | if (shake_check(me, 0)) goto end; | |
2935 | shake_xof(SHAKE_H(me)); | |
2936 | SHAKE_ST(me) = 1; | |
2937 | RETURN_ME; | |
2938 | end: | |
2939 | return (0); | |
2940 | } | |
2941 | ||
2942 | static PyObject *shakemeth_done(PyObject *me, PyObject *arg) | |
2943 | { | |
2944 | PyObject *rc = 0; | |
2945 | size_t n; | |
2946 | if (!PyArg_ParseTuple(arg, "O&:done", convszt, &n)) goto end; | |
2947 | if (shake_check(me, 0)) goto end; | |
2948 | rc = bytestring_pywrap(0, n); | |
2949 | shake_done(SHAKE_H(me), PyString_AS_STRING(rc), n); | |
2950 | SHAKE_ST(me) = -1; | |
2951 | end: | |
2952 | return (rc); | |
2953 | } | |
2954 | ||
2955 | static PyObject *shakemeth_copy(PyObject *me, PyObject *arg) | |
2956 | { | |
2957 | shake_pyobj *rc = 0; | |
2958 | ||
2959 | if (!PyArg_ParseTuple(arg, ":copy")) goto end; | |
2960 | rc = PyObject_NEW(shake_pyobj, me->ob_type); | |
2961 | rc->h = *SHAKE_H(me); | |
2962 | rc->st = SHAKE_ST(me); | |
2963 | end: | |
9b5c9816 | 2964 | return ((PyObject *)rc); |
6bd22b53 MW |
2965 | } |
2966 | ||
2967 | static PyObject *shakemeth_get(PyObject *me, PyObject *arg) | |
2968 | { | |
2969 | PyObject *rc = 0; | |
2970 | size_t sz; | |
2971 | ||
2972 | if (!PyArg_ParseTuple(arg, "O&:get", convszt, &sz)) goto end; | |
2973 | if (shake_check(me, 1)) goto end; | |
2974 | rc = bytestring_pywrap(0, sz); | |
2975 | shake_get(SHAKE_H(me), PyString_AS_STRING(rc), sz); | |
2976 | end: | |
2977 | return (rc); | |
2978 | } | |
2979 | ||
2980 | static PyObject *shakemeth_mask(PyObject *me, PyObject *arg) | |
2981 | { | |
2982 | PyObject *rc = 0; | |
2983 | char *p; Py_ssize_t sz; | |
2984 | ||
2985 | if (!PyArg_ParseTuple(arg, "s#:mask", &p, &sz)) goto end; | |
2986 | if (shake_check(me, 1)) goto end; | |
2987 | rc = bytestring_pywrap(0, sz); | |
2988 | shake_mask(SHAKE_H(me), p, PyString_AS_STRING(rc), sz); | |
2989 | end: | |
2990 | return (rc); | |
2991 | } | |
2992 | ||
2993 | static PyObject *shakeget_rate(PyObject *me, void *hunoz) | |
2994 | { return (PyInt_FromLong(SHAKE_H(me)->h.r)); } | |
2995 | ||
2996 | static PyObject *shakeget_buffered(PyObject *me, void *hunoz) | |
2997 | { return (PyInt_FromLong(SHAKE_H(me)->h.n)); } | |
2998 | ||
2999 | static PyObject *shakeget_state(PyObject *me, void *hunoz) | |
3000 | { | |
3001 | int st = SHAKE_ST(me); | |
3002 | return (PyString_FromString(st == 0 ? "absorb" : | |
3003 | st == 1 ? "squeeze" : "dead")); | |
3004 | } | |
3005 | ||
3006 | static PyGetSetDef shake_pygetset[] = { | |
3007 | #define GETSETNAME(op, name) shake##op##_##name | |
3008 | GET (rate, "S.rate -> rate, in bytes") | |
3009 | GET (buffered, "S.buffered -> amount currently buffered") | |
3010 | GET (state, "S.state -> `absorb', `squeeze', `dead'") | |
3011 | #undef GETSETNAME | |
3012 | { 0 } | |
3013 | }; | |
3014 | ||
3015 | static PyMethodDef shake_pymethods[] = { | |
3016 | #define METHNAME(func) shakemeth_##func | |
3017 | METH (copy, "S.copy() -> SS") | |
3018 | METH (hash, "S.hash(M)") | |
3019 | #define METHU_(n, W, w) METH(hashu##w, "S.hashu" #w "(WORD)") | |
3020 | DOUINTCONV(METHU_) | |
3021 | #undef METHU_ | |
3022 | #define METHBUF_(n, W, w) METH(hashbuf##w, "S.hashbuf" #w "(BYTES)") | |
3023 | DOUINTCONV(METHBUF_) | |
3024 | #undef METHBUF_ | |
3025 | METH (hashstrz, "S.hashstrz(STRING)") | |
3026 | METH (xof, "S.xof()") | |
3027 | METH (done, "S.done(LEN) ->H") | |
3028 | METH (get, "S.get(LEN) -> H") | |
3029 | METH (mask, "S.mask(M) -> C") | |
3030 | #undef METHNAME | |
3031 | { 0 } | |
3032 | }; | |
3033 | ||
3034 | static PyTypeObject shake_pytype_skel = { | |
3035 | PyObject_HEAD_INIT(0) 0, /* Header */ | |
3036 | "Shake", /* @tp_name@ */ | |
3037 | sizeof(shake_pyobj), /* @tp_basicsize@ */ | |
3038 | 0, /* @tp_itemsize@ */ | |
3039 | ||
3040 | 0, /* @tp_dealloc@ */ | |
3041 | 0, /* @tp_print@ */ | |
3042 | 0, /* @tp_getattr@ */ | |
3043 | 0, /* @tp_setattr@ */ | |
3044 | 0, /* @tp_compare@ */ | |
3045 | 0, /* @tp_repr@ */ | |
3046 | 0, /* @tp_as_number@ */ | |
3047 | 0, /* @tp_as_sequence@ */ | |
3048 | 0, /* @tp_as_mapping@ */ | |
3049 | 0, /* @tp_hash@ */ | |
3050 | 0, /* @tp_call@ */ | |
3051 | 0, /* @tp_str@ */ | |
3052 | 0, /* @tp_getattro@ */ | |
3053 | 0, /* @tp_setattro@ */ | |
3054 | 0, /* @tp_as_buffer@ */ | |
3055 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
3056 | Py_TPFLAGS_BASETYPE, | |
3057 | ||
3058 | /* @tp_doc@ */ | |
3059 | "SHAKE/cSHAKE base class.", | |
3060 | ||
3061 | 0, /* @tp_traverse@ */ | |
3062 | 0, /* @tp_clear@ */ | |
3063 | 0, /* @tp_richcompare@ */ | |
3064 | 0, /* @tp_weaklistoffset@ */ | |
3065 | 0, /* @tp_iter@ */ | |
3066 | 0, /* @tp_iternext@ */ | |
3067 | shake_pymethods, /* @tp_methods@ */ | |
3068 | 0, /* @tp_members@ */ | |
3069 | shake_pygetset, /* @tp_getset@ */ | |
3070 | 0, /* @tp_base@ */ | |
3071 | 0, /* @tp_dict@ */ | |
3072 | 0, /* @tp_descr_get@ */ | |
3073 | 0, /* @tp_descr_set@ */ | |
3074 | 0, /* @tp_dictoffset@ */ | |
3075 | 0, /* @tp_init@ */ | |
3076 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
3077 | abstract_pynew, /* @tp_new@ */ | |
3078 | 0, /* @tp_free@ */ | |
3079 | 0 /* @tp_is_gc@ */ | |
3080 | }; | |
3081 | ||
3082 | static PyTypeObject shake128_pytype_skel = { | |
3083 | PyObject_HEAD_INIT(0) 0, /* Header */ | |
3084 | "Shake128", /* @tp_name@ */ | |
3085 | 0, /* @tp_basicsize@ */ | |
3086 | 0, /* @tp_itemsize@ */ | |
3087 | ||
3088 | 0, /* @tp_dealloc@ */ | |
3089 | 0, /* @tp_print@ */ | |
3090 | 0, /* @tp_getattr@ */ | |
3091 | 0, /* @tp_setattr@ */ | |
3092 | 0, /* @tp_compare@ */ | |
3093 | 0, /* @tp_repr@ */ | |
3094 | 0, /* @tp_as_number@ */ | |
3095 | 0, /* @tp_as_sequence@ */ | |
3096 | 0, /* @tp_as_mapping@ */ | |
3097 | 0, /* @tp_hash@ */ | |
3098 | 0, /* @tp_call@ */ | |
3099 | 0, /* @tp_str@ */ | |
3100 | 0, /* @tp_getattro@ */ | |
3101 | 0, /* @tp_setattro@ */ | |
3102 | 0, /* @tp_as_buffer@ */ | |
3103 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
3104 | Py_TPFLAGS_BASETYPE, | |
3105 | ||
3106 | /* @tp_doc@ */ | |
06cd26e8 | 3107 | "Shake128([perso = STR], [func = STR]): SHAKE128/cSHAKE128 XOF.", |
6bd22b53 MW |
3108 | |
3109 | 0, /* @tp_traverse@ */ | |
3110 | 0, /* @tp_clear@ */ | |
3111 | 0, /* @tp_richcompare@ */ | |
3112 | 0, /* @tp_weaklistoffset@ */ | |
3113 | 0, /* @tp_iter@ */ | |
3114 | 0, /* @tp_iternext@ */ | |
3115 | 0, /* @tp_methods@ */ | |
3116 | 0, /* @tp_members@ */ | |
3117 | 0, /* @tp_getset@ */ | |
3118 | 0, /* @tp_base@ */ | |
3119 | 0, /* @tp_dict@ */ | |
3120 | 0, /* @tp_descr_get@ */ | |
3121 | 0, /* @tp_descr_set@ */ | |
3122 | 0, /* @tp_dictoffset@ */ | |
3123 | 0, /* @tp_init@ */ | |
3124 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
3125 | shake128_pynew, /* @tp_new@ */ | |
3126 | 0, /* @tp_free@ */ | |
3127 | 0 /* @tp_is_gc@ */ | |
3128 | }; | |
3129 | ||
3130 | static PyTypeObject shake256_pytype_skel = { | |
3131 | PyObject_HEAD_INIT(0) 0, /* Header */ | |
3132 | "Shake256", /* @tp_name@ */ | |
3133 | 0, /* @tp_basicsize@ */ | |
3134 | 0, /* @tp_itemsize@ */ | |
3135 | ||
3136 | 0, /* @tp_dealloc@ */ | |
3137 | 0, /* @tp_print@ */ | |
3138 | 0, /* @tp_getattr@ */ | |
3139 | 0, /* @tp_setattr@ */ | |
3140 | 0, /* @tp_compare@ */ | |
3141 | 0, /* @tp_repr@ */ | |
3142 | 0, /* @tp_as_number@ */ | |
3143 | 0, /* @tp_as_sequence@ */ | |
3144 | 0, /* @tp_as_mapping@ */ | |
3145 | 0, /* @tp_hash@ */ | |
3146 | 0, /* @tp_call@ */ | |
3147 | 0, /* @tp_str@ */ | |
3148 | 0, /* @tp_getattro@ */ | |
3149 | 0, /* @tp_setattro@ */ | |
3150 | 0, /* @tp_as_buffer@ */ | |
3151 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
3152 | Py_TPFLAGS_BASETYPE, | |
3153 | ||
3154 | /* @tp_doc@ */ | |
06cd26e8 | 3155 | "Shake256([perso = STR], [func = STR]): SHAKE256/cSHAKE256 XOF.", |
6bd22b53 MW |
3156 | |
3157 | 0, /* @tp_traverse@ */ | |
3158 | 0, /* @tp_clear@ */ | |
3159 | 0, /* @tp_richcompare@ */ | |
3160 | 0, /* @tp_weaklistoffset@ */ | |
3161 | 0, /* @tp_iter@ */ | |
3162 | 0, /* @tp_iternext@ */ | |
3163 | 0, /* @tp_methods@ */ | |
3164 | 0, /* @tp_members@ */ | |
3165 | 0, /* @tp_getset@ */ | |
3166 | 0, /* @tp_base@ */ | |
3167 | 0, /* @tp_dict@ */ | |
3168 | 0, /* @tp_descr_get@ */ | |
3169 | 0, /* @tp_descr_set@ */ | |
3170 | 0, /* @tp_dictoffset@ */ | |
3171 | 0, /* @tp_init@ */ | |
3172 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
3173 | shake256_pynew, /* @tp_new@ */ | |
3174 | 0, /* @tp_free@ */ | |
3175 | 0 /* @tp_is_gc@ */ | |
3176 | }; | |
3177 | ||
03ed9abb MW |
3178 | /*----- Pseudorandom permutations -----------------------------------------*/ |
3179 | ||
3180 | static PyTypeObject *gcprp_pytype, *gprp_pytype; | |
3181 | ||
3182 | typedef struct prpinfo { | |
3183 | const char *name; | |
3184 | const octet *keysz; | |
3185 | size_t ctxsz; | |
3186 | size_t blksz; | |
3187 | void (*init)(void *, const void *, size_t); | |
3188 | void (*eblk)(void *, const void *, void *); | |
3189 | void (*dblk)(void *, const void *, void *); | |
3190 | } prpinfo; | |
3191 | ||
3192 | #define PRP_DEF(PRE, pre) \ | |
3193 | static void pre##_prpinit(void *ctx, const void *k, size_t ksz) \ | |
3194 | { pre##_init(ctx, k, ksz); } \ | |
3195 | static void pre##_prpeblk(void *ctx, const void *in, void *out) \ | |
3196 | { \ | |
3197 | uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \ | |
3198 | pre##_eblk(ctx, w, w); BLKC_STORE(PRE, out, w); \ | |
3199 | } \ | |
3200 | static void pre##_prpdblk(void *ctx, const void *in, void *out) \ | |
3201 | { \ | |
3202 | uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \ | |
3203 | pre##_dblk(ctx, w, w); BLKC_STORE(PRE, out, w); \ | |
3204 | } \ | |
3205 | static const prpinfo pre##_prpinfo = { \ | |
3206 | #pre, pre##_keysz, sizeof(pre##_ctx), PRE##_BLKSZ, \ | |
3207 | pre##_prpinit, pre##_prpeblk, pre##_prpdblk \ | |
3208 | }; | |
3209 | PRPS(PRP_DEF) | |
3210 | ||
3211 | static const struct prpinfo *const gprptab[] = { | |
3212 | #define PRP_ENTRY(PRE, pre) &pre##_prpinfo, | |
3213 | PRPS(PRP_ENTRY) | |
3214 | 0 | |
b2687a0a | 3215 | }; |
03ed9abb MW |
3216 | |
3217 | typedef struct gcprp_pyobj { | |
3218 | PyHeapTypeObject ty; | |
3219 | const prpinfo *prp; | |
3220 | } gcprp_pyobj; | |
3221 | #define GCPRP_PRP(o) (((gcprp_pyobj *)(o))->prp) | |
3222 | ||
3223 | typedef struct gprp_pyobj { | |
3224 | PyObject_HEAD | |
3225 | const prpinfo *prp; | |
3226 | } gprp_pyobj; | |
3227 | #define GPRP_PRP(o) (((gprp_pyobj *)(o))->prp) | |
3228 | #define GPRP_CTX(o) (((gprp_pyobj *)(o)) + 1) | |
3229 | ||
3230 | typedef struct prp { | |
3231 | const prpinfo *prp; | |
3232 | void *ctx; | |
3233 | } prp; | |
3234 | ||
3235 | static PyObject *gprp_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
3236 | { | |
827f89d7 | 3237 | static const char *const kwlist[] = { "key", 0 }; |
03ed9abb | 3238 | char *k; |
6b54260d | 3239 | Py_ssize_t sz; |
03ed9abb MW |
3240 | const prpinfo *prp = GCPRP_PRP(ty); |
3241 | PyObject *me; | |
3242 | ||
827f89d7 | 3243 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &k, &sz)) |
03ed9abb MW |
3244 | goto end; |
3245 | if (keysz(sz, prp->keysz) != sz) VALERR("bad key length"); | |
3246 | me = (PyObject *)ty->tp_alloc(ty, 0); | |
3247 | GPRP_PRP(me) = prp; | |
3248 | prp->init(GPRP_CTX(me), k, sz); | |
3249 | Py_INCREF(me); | |
3250 | return (me); | |
3251 | end: | |
b2687a0a | 3252 | return (0); |
03ed9abb MW |
3253 | } |
3254 | ||
3255 | static void gprp_pydealloc(PyObject *me) | |
3256 | { Py_DECREF(me->ob_type); FREEOBJ(me); } | |
3257 | ||
3258 | static PyObject *gcprp_pywrap(const prpinfo *prp) | |
3259 | { | |
3260 | gcprp_pyobj *g = newtype(gcprp_pytype, 0, prp->name); | |
3261 | g->prp = prp; | |
24b3d57b MW |
3262 | g->ty.ht_type.tp_basicsize = sizeof(gprp_pyobj) + prp->ctxsz; |
3263 | g->ty.ht_type.tp_base = gprp_pytype; | |
03ed9abb | 3264 | Py_INCREF(gprp_pytype); |
24b3d57b MW |
3265 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
3266 | Py_TPFLAGS_BASETYPE | | |
3267 | Py_TPFLAGS_HEAPTYPE); | |
3268 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; | |
3269 | g->ty.ht_type.tp_free = 0; | |
3270 | g->ty.ht_type.tp_new = gprp_pynew; | |
dc075750 | 3271 | typeready(&g->ty.ht_type); |
03ed9abb MW |
3272 | return ((PyObject *)g); |
3273 | } | |
3274 | ||
3275 | static PyObject *gcpget_name(PyObject *me, void *hunoz) | |
3276 | { return (PyString_FromString(GCPRP_PRP(me)->name)); } | |
3277 | static PyObject *gcpget_keysz(PyObject *me, void *hunoz) | |
3278 | { return (keysz_pywrap(GCPRP_PRP(me)->keysz)); } | |
3279 | static PyObject *gcpget_blksz(PyObject *me, void *hunoz) | |
3280 | { return (PyInt_FromLong(GCPRP_PRP(me)->blksz)); } | |
3281 | ||
3282 | static PyObject *gpmeth_encrypt(PyObject *me, PyObject *arg) | |
3283 | { | |
3284 | char *p; | |
6b54260d | 3285 | Py_ssize_t n; |
03ed9abb MW |
3286 | PyObject *rc = 0; |
3287 | ||
3288 | if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &n)) goto end; | |
3289 | if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length"); | |
3290 | rc = bytestring_pywrap(0, n); | |
3291 | GPRP_PRP(me)->eblk(GPRP_CTX(me), p, PyString_AS_STRING(rc)); | |
3292 | end: | |
3293 | return (rc); | |
3294 | } | |
3295 | ||
3296 | static PyObject *gpmeth_decrypt(PyObject *me, PyObject *arg) | |
3297 | { | |
3298 | char *p; | |
6b54260d | 3299 | Py_ssize_t n; |
03ed9abb MW |
3300 | PyObject *rc = 0; |
3301 | ||
3302 | if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &n)) goto end; | |
3303 | if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length"); | |
3304 | rc = bytestring_pywrap(0, n); | |
3305 | GPRP_PRP(me)->dblk(GPRP_CTX(me), p, PyString_AS_STRING(rc)); | |
3306 | end: | |
3307 | return (rc); | |
3308 | } | |
3309 | ||
3310 | static PyGetSetDef gcprp_pygetset[] = { | |
3311 | #define GETSETNAME(op, name) gcp##op##_##name | |
3312 | GET (keysz, "CP.keysz -> acceptable key sizes") | |
3313 | GET (blksz, "CP.blksz -> block size") | |
3314 | GET (name, "CP.name -> name of this kind of PRP") | |
3315 | #undef GETSETNAME | |
3316 | { 0 } | |
3317 | }; | |
3318 | ||
3319 | static PyMethodDef gprp_pymethods[] = { | |
3320 | #define METHNAME(name) gpmeth_##name | |
3321 | METH (encrypt, "P.encrypt(PT) -> CT") | |
3322 | METH (decrypt, "P.decrypt(CT) -> PT") | |
3323 | #undef METHNAME | |
3324 | { 0 } | |
3325 | }; | |
3326 | ||
3327 | static PyTypeObject gcprp_pytype_skel = { | |
3328 | PyObject_HEAD_INIT(0) 0, /* Header */ | |
c461c9b3 | 3329 | "GCPRP", /* @tp_name@ */ |
03ed9abb MW |
3330 | sizeof(gcprp_pyobj), /* @tp_basicsize@ */ |
3331 | 0, /* @tp_itemsize@ */ | |
3332 | ||
3333 | 0, /* @tp_dealloc@ */ | |
3334 | 0, /* @tp_print@ */ | |
3335 | 0, /* @tp_getattr@ */ | |
3336 | 0, /* @tp_setattr@ */ | |
3337 | 0, /* @tp_compare@ */ | |
3338 | 0, /* @tp_repr@ */ | |
3339 | 0, /* @tp_as_number@ */ | |
3340 | 0, /* @tp_as_sequence@ */ | |
3341 | 0, /* @tp_as_mapping@ */ | |
3342 | 0, /* @tp_hash@ */ | |
3343 | 0, /* @tp_call@ */ | |
3344 | 0, /* @tp_str@ */ | |
3345 | 0, /* @tp_getattro@ */ | |
3346 | 0, /* @tp_setattro@ */ | |
3347 | 0, /* @tp_as_buffer@ */ | |
3348 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
3349 | Py_TPFLAGS_BASETYPE, | |
3350 | ||
3351 | /* @tp_doc@ */ | |
3352 | "Pseudorandom permutation metaclass.", | |
3353 | ||
3354 | 0, /* @tp_traverse@ */ | |
3355 | 0, /* @tp_clear@ */ | |
3356 | 0, /* @tp_richcompare@ */ | |
3357 | 0, /* @tp_weaklistoffset@ */ | |
3358 | 0, /* @tp_iter@ */ | |
3359 | 0, /* @tp_iternext@ */ | |
3360 | 0, /* @tp_methods@ */ | |
3361 | 0, /* @tp_members@ */ | |
3362 | gcprp_pygetset, /* @tp_getset@ */ | |
3363 | 0, /* @tp_base@ */ | |
3364 | 0, /* @tp_dict@ */ | |
3365 | 0, /* @tp_descr_get@ */ | |
3366 | 0, /* @tp_descr_set@ */ | |
3367 | 0, /* @tp_dictoffset@ */ | |
3368 | 0, /* @tp_init@ */ | |
3369 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
3370 | abstract_pynew, /* @tp_new@ */ | |
3371 | 0, /* @tp_free@ */ | |
3372 | 0 /* @tp_is_gc@ */ | |
3373 | }; | |
3374 | ||
3375 | static PyTypeObject gprp_pytype_skel = { | |
3376 | PyObject_HEAD_INIT(0) 0, /* Header */ | |
c461c9b3 | 3377 | "GPRP", /* @tp_name@ */ |
03ed9abb MW |
3378 | sizeof(gprp_pyobj), /* @tp_basicsize@ */ |
3379 | 0, /* @tp_itemsize@ */ | |
3380 | ||
3381 | gprp_pydealloc, /* @tp_dealloc@ */ | |
3382 | 0, /* @tp_print@ */ | |
3383 | 0, /* @tp_getattr@ */ | |
3384 | 0, /* @tp_setattr@ */ | |
3385 | 0, /* @tp_compare@ */ | |
3386 | 0, /* @tp_repr@ */ | |
3387 | 0, /* @tp_as_number@ */ | |
3388 | 0, /* @tp_as_sequence@ */ | |
3389 | 0, /* @tp_as_mapping@ */ | |
3390 | 0, /* @tp_hash@ */ | |
3391 | 0, /* @tp_call@ */ | |
3392 | 0, /* @tp_str@ */ | |
3393 | 0, /* @tp_getattro@ */ | |
3394 | 0, /* @tp_setattro@ */ | |
3395 | 0, /* @tp_as_buffer@ */ | |
3396 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
3397 | Py_TPFLAGS_BASETYPE, | |
3398 | ||
3399 | /* @tp_doc@ */ | |
3400 | "Pseudorandom permutation, abstract base class.", | |
3401 | ||
3402 | 0, /* @tp_traverse@ */ | |
3403 | 0, /* @tp_clear@ */ | |
3404 | 0, /* @tp_richcompare@ */ | |
3405 | 0, /* @tp_weaklistoffset@ */ | |
3406 | 0, /* @tp_iter@ */ | |
3407 | 0, /* @tp_iternext@ */ | |
3408 | gprp_pymethods, /* @tp_methods@ */ | |
3409 | 0, /* @tp_members@ */ | |
3410 | 0, /* @tp_getset@ */ | |
3411 | 0, /* @tp_base@ */ | |
3412 | 0, /* @tp_dict@ */ | |
3413 | 0, /* @tp_descr_get@ */ | |
3414 | 0, /* @tp_descr_set@ */ | |
3415 | 0, /* @tp_dictoffset@ */ | |
3416 | 0, /* @tp_init@ */ | |
3417 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
3418 | abstract_pynew, /* @tp_new@ */ | |
3419 | 0, /* @tp_free@ */ | |
3420 | 0 /* @tp_is_gc@ */ | |
3421 | }; | |
3422 | ||
d7ab1bab | 3423 | /*----- Main code ---------------------------------------------------------*/ |
3424 | ||
89157adc MW |
3425 | static PyMethodDef methods[] = { |
3426 | #define METHNAME(func) meth_##func | |
3427 | METH (_KeySZ_fromdl, "\ | |
3428 | fromdl(N) -> M: convert integer discrete log field size to work factor") | |
3429 | METH (_KeySZ_fromschnorr, "\ | |
3430 | fromschnorr(N) -> M: convert Schnorr group order to work factor") | |
3431 | METH (_KeySZ_fromif, "\ | |
3432 | fromif(N) -> M: convert integer factorization problem size to work factor") | |
3433 | METH (_KeySZ_fromec, "\ | |
3434 | fromec(N) -> M: convert elliptic curve group order to work factor") | |
3435 | METH (_KeySZ_todl, "\ | |
3436 | todl(N) -> M: convert work factor to integer discrete log field size") | |
3437 | METH (_KeySZ_toschnorr, "\ | |
3438 | toschnorr(N) -> M: convert work factor to Schnorr group order") | |
3439 | METH (_KeySZ_toif, "\ | |
3440 | toif(N) -> M: convert work factor to integer factorization problem size") | |
3441 | METH (_KeySZ_toec, "\ | |
3442 | toec(N) -> M: convert work factor to elliptic curve group order") | |
a75e68c9 MW |
3443 | METH (_KeySZ_toec, "\ |
3444 | toec(N) -> M: convert work factor to elliptic curve group order") | |
3445 | #define METH_HDANCE(hdance, HDance) METH(hdance##_prf, "\ | |
3446 | " #hdance "_prf(K, N) -> H: calculate " HDance " hash of N with K") | |
3447 | METH_HDANCE(hsalsa20, "HSalsa20") | |
3448 | METH_HDANCE(hsalsa2012, "HSalsa20/12") | |
3449 | METH_HDANCE(hsalsa208, "HSalsa20/8") | |
3450 | METH_HDANCE(hchacha20, "HChaCha20") | |
3451 | METH_HDANCE(hchacha12, "HChaCha12") | |
3452 | METH_HDANCE(hchacha8, "HChaCha8") | |
3453 | #undef METH_DANCE | |
89157adc MW |
3454 | #undef METHNAME |
3455 | { 0 } | |
3456 | }; | |
3457 | ||
d7ab1bab | 3458 | void algorithms_pyinit(void) |
3459 | { | |
3460 | INITTYPE(keysz, root); | |
3461 | INITTYPE(keyszany, keysz); | |
3462 | INITTYPE(keyszrange, keysz); | |
3463 | INITTYPE(keyszset, keysz); | |
3464 | INITTYPE(gccipher, type); | |
3465 | INITTYPE(gcipher, root); | |
bebf03ab MW |
3466 | INITTYPE(gcaead, type); |
3467 | INITTYPE(gaeadkey, root); | |
3468 | INITTYPE(gcaeadaad, type); | |
3469 | INITTYPE(gaeadaad, root); | |
3470 | INITTYPE(gcaeadenc, type); | |
3471 | INITTYPE(gaeadenc, root); | |
3472 | INITTYPE(gcaeaddec, type); | |
3473 | INITTYPE(gaeaddec, root); | |
d7ab1bab | 3474 | INITTYPE(gchash, type); |
3475 | INITTYPE(ghash, root); | |
3476 | INITTYPE(gcmac, type); | |
3477 | INITTYPE(gmac, type); | |
3478 | INITTYPE(gmhash, ghash); | |
204d480b MW |
3479 | INITTYPE(poly1305cls, type); |
3480 | INITTYPE_META(poly1305key, type, poly1305cls); | |
3481 | INITTYPE(poly1305hash, root); | |
b35fdbe6 | 3482 | INITTYPE(kxvik, root); |
6bd22b53 MW |
3483 | INITTYPE(shake, root); |
3484 | INITTYPE(shake128, shake); | |
3485 | INITTYPE(shake256, shake); | |
03ed9abb MW |
3486 | INITTYPE(gcprp, type); |
3487 | INITTYPE(gprp, root); | |
89157adc | 3488 | addmethods(methods); |
d7ab1bab | 3489 | } |
3490 | ||
d7ab1bab | 3491 | GEN(gcciphers, cipher) |
bebf03ab | 3492 | GEN(gcaeads, aead) |
d7ab1bab | 3493 | GEN(gchashes, hash) |
3494 | GEN(gcmacs, mac) | |
03ed9abb MW |
3495 | #define gcprp prpinfo |
3496 | GEN(gcprps, prp) | |
d7ab1bab | 3497 | |
3498 | void algorithms_pyinsert(PyObject *mod) | |
3499 | { | |
3500 | PyObject *d; | |
3501 | INSERT("KeySZ", keysz_pytype); | |
3502 | INSERT("KeySZAny", keyszany_pytype); | |
3503 | INSERT("KeySZRange", keyszrange_pytype); | |
3504 | INSERT("KeySZSet", keyszset_pytype); | |
3505 | INSERT("GCCipher", gccipher_pytype); | |
3506 | INSERT("GCipher", gcipher_pytype); | |
3507 | INSERT("gcciphers", gcciphers()); | |
bebf03ab MW |
3508 | INSERT("GCAEAD", gcaead_pytype); |
3509 | INSERT("GAEKey", gaeadkey_pytype); | |
3510 | INSERT("GAEAADClass", gcaeadaad_pytype); | |
3511 | INSERT("GAEAAD", gaeadaad_pytype); | |
3512 | INSERT("GAEEncClass", gcaeadenc_pytype); | |
3513 | INSERT("GAEEnc", gaeadenc_pytype); | |
3514 | INSERT("GAEDecClass", gcaeaddec_pytype); | |
3515 | INSERT("GAEDec", gaeaddec_pytype); | |
3516 | INSERT("gcaeads", gcaeads()); | |
d7ab1bab | 3517 | INSERT("GCHash", gchash_pytype); |
3518 | INSERT("GHash", ghash_pytype); | |
3519 | INSERT("gchashes", d = gchashes()); | |
3520 | sha_pyobj = PyDict_GetItemString(d, "sha"); Py_INCREF(sha_pyobj); | |
3521 | has160_pyobj = PyDict_GetItemString(d, "has160"); Py_INCREF(has160_pyobj); | |
3522 | INSERT("GCMAC", gcmac_pytype); | |
3523 | INSERT("GMAC", gmac_pytype); | |
3524 | INSERT("GMACHash", gmhash_pytype); | |
3525 | INSERT("gcmacs", gcmacs()); | |
204d480b MW |
3526 | INSERT("Poly1305Class", poly1305cls_pytype); |
3527 | INSERT("poly1305", poly1305key_pytype); | |
3528 | INSERT("Poly1305Hash", poly1305hash_pytype); | |
b35fdbe6 | 3529 | INSERT("Keccak1600", kxvik_pytype); |
6bd22b53 MW |
3530 | INSERT("Shake", shake_pytype); |
3531 | INSERT("Shake128", shake128_pytype); | |
3532 | INSERT("Shake256", shake256_pytype); | |
03ed9abb MW |
3533 | INSERT("GCPRP", gcprp_pytype); |
3534 | INSERT("GPRP", gprp_pytype); | |
3535 | INSERT("gcprps", gcprps()); | |
d7ab1bab | 3536 | } |
3537 | ||
3538 | /*----- That's all, folks -------------------------------------------------*/ |