Commit | Line | Data |
---|---|---|
d7ab1bab | 1 | /* -*-c-*- |
2 | * | |
d7ab1bab | 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 | ||
c6e89d48 MW |
36 | static PyTypeObject *keysz_pytype; |
37 | static PyTypeObject *keyszany_pytype, *keyszrange_pytype, *keyszset_pytype; | |
38 | ||
39 | typedef struct keysz_pyobj { | |
40 | PyObject_HEAD | |
41 | int dfl; | |
42 | } keysz_pyobj; | |
43 | ||
44 | typedef struct keyszrange_pyobj { | |
45 | PyObject_HEAD | |
46 | int dfl; | |
47 | int min, max, mod; | |
48 | } keyszrange_pyobj; | |
49 | ||
50 | typedef struct keyszset_pyobj { | |
51 | PyObject_HEAD | |
52 | int dfl; | |
53 | PyObject *set; | |
54 | } keyszset_pyobj; | |
55 | ||
56 | #define KEYSZ_PYCHECK(o) PyObject_TypeCheck((o), keysz_pytype) | |
d7ab1bab | 57 | |
cfe23cf8 MW |
58 | #ifndef KSZ_OPMASK |
59 | # define KSZ_OPMASK 0x1f | |
60 | #endif | |
61 | ||
62 | #ifndef KSZ_16BIT | |
63 | # define KSZ_16BIT 0x20 | |
64 | #endif | |
65 | ||
d7ab1bab | 66 | PyObject *keysz_pywrap(const octet *k) |
67 | { | |
cfe23cf8 MW |
68 | unsigned op = *k++; |
69 | #define ARG(i) (op&KSZ_16BIT ? LOAD16(k + 2*(i)) : k[i]) | |
70 | switch (op&KSZ_OPMASK) { | |
d7ab1bab | 71 | case KSZ_ANY: { |
72 | keysz_pyobj *o = PyObject_New(keysz_pyobj, keyszany_pytype); | |
cfe23cf8 | 73 | o->dfl = ARG(0); |
d7ab1bab | 74 | return ((PyObject *)o); |
75 | } break; | |
76 | case KSZ_RANGE: { | |
77 | keyszrange_pyobj *o = | |
b2687a0a | 78 | PyObject_New(keyszrange_pyobj, keyszrange_pytype); |
cfe23cf8 MW |
79 | o->dfl = ARG(0); |
80 | o->min = ARG(1); | |
81 | o->max = ARG(2); | |
82 | o->mod = ARG(3); | |
d7ab1bab | 83 | if (!o->mod) o->mod = 1; |
84 | return ((PyObject *)o); | |
85 | } break; | |
86 | case KSZ_SET: { | |
87 | keyszset_pyobj *o = | |
b2687a0a | 88 | PyObject_New(keyszset_pyobj, keyszset_pytype); |
fd46a88d | 89 | PyObject *l; |
d7ab1bab | 90 | int i, n; |
cfe23cf8 MW |
91 | o->dfl = ARG(0); |
92 | for (i = 0; ARG(i); i++) ; | |
fd46a88d MW |
93 | n = i; l = PyList_New(n); |
94 | for (i = 0; i < n; i++) PyList_SET_ITEM(l, i, PyInt_FromLong(ARG(i))); | |
95 | o->set = PyFrozenSet_New(l); Py_DECREF(l); | |
d7ab1bab | 96 | return ((PyObject *)o); |
97 | } break; | |
98 | default: | |
99 | abort(); | |
100 | } | |
cfe23cf8 | 101 | #undef ARG |
d7ab1bab | 102 | } |
103 | ||
104 | static PyObject *keyszany_pynew(PyTypeObject *ty, | |
105 | PyObject *arg, PyObject *kw) | |
106 | { | |
827f89d7 | 107 | static const char *const kwlist[] = { "default", 0 }; |
d7ab1bab | 108 | int dfl; |
109 | keysz_pyobj *o; | |
110 | ||
827f89d7 | 111 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "i:new", KWLIST, &dfl)) |
d7ab1bab | 112 | goto end; |
113 | if (dfl < 0) VALERR("key size cannot be negative"); | |
114 | o = (keysz_pyobj *)ty->tp_alloc(ty, 0); | |
115 | o->dfl = dfl; | |
116 | return ((PyObject *)o); | |
117 | end: | |
118 | return (0); | |
119 | } | |
120 | ||
121 | static PyObject *keyszrange_pynew(PyTypeObject *ty, | |
122 | PyObject *arg, PyObject *kw) | |
123 | { | |
827f89d7 | 124 | static const char *const kwlist[] = { "default", "min", "max", "mod", 0 }; |
a9410db7 MW |
125 | int dfl, min = 0, max, mod = 1; |
126 | PyObject *maxobj = Py_None; | |
d7ab1bab | 127 | keyszrange_pyobj *o; |
128 | ||
a9410db7 MW |
129 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|iOi:new", KWLIST, |
130 | &dfl, &min, &maxobj, &mod)) | |
d7ab1bab | 131 | goto end; |
a9410db7 MW |
132 | if (maxobj == Py_None) |
133 | max = 0; | |
134 | else { | |
135 | max = PyInt_AsLong(maxobj); | |
136 | if (max == -1 && PyErr_Occurred()) goto end; | |
137 | } | |
138 | if (dfl < 0 || min < 0 || max < 0) VALERR("key size cannot be negative"); | |
838ab173 MW |
139 | if (min > dfl || (max && dfl > max)) VALERR("bad key size bounds"); |
140 | if (mod <= 0 || dfl%mod || min%mod || max%mod) | |
d7ab1bab | 141 | VALERR("bad key size modulus"); |
142 | o = (keyszrange_pyobj *)ty->tp_alloc(ty, 0); | |
143 | o->dfl = dfl; | |
144 | o->min = min; | |
145 | o->max = max; | |
146 | o->mod = mod; | |
147 | return ((PyObject *)o); | |
148 | end: | |
149 | return (0); | |
150 | } | |
151 | ||
152 | static PyObject *keyszset_pynew(PyTypeObject *ty, | |
153 | PyObject *arg, PyObject *kw) | |
154 | { | |
827f89d7 | 155 | static const char *const kwlist[] = { "default", "set", 0 }; |
fd46a88d | 156 | int dfl, xx; |
d7ab1bab | 157 | PyObject *set = 0; |
fd46a88d | 158 | PyObject *x = 0, *l = 0, *i = 0; |
d7ab1bab | 159 | keyszset_pyobj *o = 0; |
160 | ||
827f89d7 | 161 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|O:new", KWLIST, &dfl, &set)) |
d7ab1bab | 162 | goto end; |
fd46a88d MW |
163 | if (set) i = PyObject_GetIter(set); |
164 | else { set = PyTuple_New(0); i = PyObject_GetIter(set); Py_DECREF(set); } | |
165 | if (!i) goto end; | |
4281a7ee | 166 | l = PyList_New(0); if (!l) goto end; |
d7ab1bab | 167 | if (dfl < 0) VALERR("key size cannot be negative"); |
fd46a88d MW |
168 | x = PyInt_FromLong(dfl); PyList_Append(l, x); Py_DECREF(x); x = 0; |
169 | for (;;) { | |
170 | x = PyIter_Next(i); if (!x) break; | |
171 | xx = PyInt_AsLong(x); if (xx == -1 && PyErr_Occurred()) goto end; | |
d7ab1bab | 172 | if (xx < 0) VALERR("key size cannot be negative"); |
fd46a88d | 173 | PyList_Append(l, x); Py_DECREF(x); x = 0; |
d7ab1bab | 174 | } |
fd46a88d | 175 | if ((set = PyFrozenSet_New(l)) == 0) goto end; |
d7ab1bab | 176 | o = (keyszset_pyobj *)ty->tp_alloc(ty, 0); |
177 | o->dfl = dfl; | |
178 | o->set = set; | |
d7ab1bab | 179 | end: |
d7ab1bab | 180 | Py_XDECREF(l); |
181 | Py_XDECREF(x); | |
182 | return ((PyObject *)o); | |
183 | } | |
184 | ||
185 | static PyObject *kaget_min(PyObject *me, void *hunoz) | |
186 | { return (PyInt_FromLong(0)); } | |
a9410db7 MW |
187 | static PyObject *kaget_max(PyObject *me, void *hunoz) |
188 | { RETURN_NONE; } | |
189 | ||
190 | static PyObject *krget_max(PyObject *me, void *hunoz) | |
191 | { | |
192 | int max = ((keyszrange_pyobj *)me)->max; | |
193 | if (max) return (PyInt_FromLong(max)); | |
194 | else RETURN_NONE; | |
195 | } | |
d7ab1bab | 196 | |
197 | static PyObject *ksget_min(PyObject *me, void *hunoz) | |
198 | { | |
fd46a88d MW |
199 | PyObject *i = PyObject_GetIter(((keyszset_pyobj *)me)->set); |
200 | PyObject *v = 0; | |
201 | int y, x = -1; | |
202 | for (;;) { | |
203 | v = PyIter_Next(i); if (!v) break; | |
204 | y = PyInt_AsLong(v); assert(y >= 0); | |
d7ab1bab | 205 | if (x == -1 || y < x) x = y; |
206 | } | |
fd46a88d MW |
207 | Py_DECREF(i); Py_XDECREF(v); |
208 | if (PyErr_Occurred()) return (0); | |
d7ab1bab | 209 | return (PyInt_FromLong(x)); |
210 | } | |
211 | ||
212 | static PyObject *ksget_max(PyObject *me, void *hunoz) | |
213 | { | |
fd46a88d MW |
214 | PyObject *i = PyObject_GetIter(((keyszset_pyobj *)me)->set); |
215 | PyObject *v = 0; | |
216 | int y, x = -1; | |
217 | for (;;) { | |
218 | v = PyIter_Next(i); if (!v) break; | |
219 | y = PyInt_AsLong(v); assert(y >= 0); | |
d7ab1bab | 220 | if (y > x) x = y; |
221 | } | |
fd46a88d MW |
222 | Py_DECREF(i); Py_XDECREF(v); |
223 | if (PyErr_Occurred()) return (0); | |
d7ab1bab | 224 | return (PyInt_FromLong(x)); |
225 | } | |
226 | ||
c90f712e | 227 | static const PyMemberDef keysz_pymembers[] = { |
d7ab1bab | 228 | #define MEMBERSTRUCT keysz_pyobj |
8dfb09a7 | 229 | MEMRNM(default, T_INT, dfl, READONLY, "KSZ.default -> default key size") |
d7ab1bab | 230 | #undef MEMBERSTRUCT |
231 | { 0 } | |
232 | }; | |
233 | ||
f281293c | 234 | #define KSZCONVOP(op) \ |
a157093f | 235 | static PyObject *kszmeth_##op(PyObject *me, PyObject *arg) \ |
f281293c MW |
236 | { \ |
237 | double x, y; \ | |
a157093f | 238 | if (!PyArg_ParseTuple(arg, "d:" #op, &x)) return (0); \ |
f281293c MW |
239 | y = keysz_##op(x); \ |
240 | return (PyFloat_FromDouble(y)); \ | |
241 | } | |
242 | KSZCONVOP(fromdl) | |
243 | KSZCONVOP(fromschnorr) | |
244 | KSZCONVOP(fromif) | |
245 | KSZCONVOP(fromec) | |
246 | KSZCONVOP(todl) | |
247 | KSZCONVOP(toschnorr) | |
248 | KSZCONVOP(toif) | |
249 | KSZCONVOP(toec) | |
250 | #undef KSZCONVOP | |
251 | ||
a157093f MW |
252 | static const PyMethodDef keysz_pymethods[] = { |
253 | #define METHNAME(name) kszmeth_##name | |
254 | SMTH (fromdl, "fromdl(N) -> M: " | |
255 | "convert integer discrete log field size to work factor") | |
256 | SMTH (fromschnorr, "fromschnorr(N) -> M: " | |
257 | "convert Schnorr group order to work factor") | |
258 | SMTH (fromif, "fromif(N) -> M: " | |
259 | "convert integer factorization problem size to work factor") | |
260 | SMTH (fromec, "fromec(N) -> M: " | |
261 | "convert elliptic curve group order to work factor") | |
262 | SMTH (todl, "todl(N) -> M: " | |
263 | "convert work factor to integer discrete log field size") | |
264 | SMTH (toschnorr, "toschnorr(N) -> M: " | |
265 | "convert work factor to Schnorr group order") | |
266 | SMTH (toif, "toif(N) -> M: " | |
267 | "convert work factor to integer factorization problem size") | |
268 | SMTH (toec, "toec(N) -> M: " | |
269 | "convert work factor to elliptic curve group order") | |
270 | SMTH (toec, "toec(N) -> M: " | |
271 | "convert work factor to elliptic curve group order") | |
272 | #undef METHNAME | |
273 | { 0 } | |
274 | }; | |
275 | ||
c90f712e | 276 | static const PyGetSetDef keyszany_pygetset[] = { |
d7ab1bab | 277 | #define GETSETNAME(op, name) ka##op##_##name |
d96c882e MW |
278 | GET (min, "KSZ.min -> smallest allowed key size") |
279 | GET (max, "KSZ.max -> largest allowed key size") | |
d7ab1bab | 280 | #undef GETSETNAME |
281 | { 0 } | |
282 | }; | |
283 | ||
c90f712e | 284 | static const PyMemberDef keyszrange_pymembers[] = { |
d7ab1bab | 285 | #define MEMBERSTRUCT keyszrange_pyobj |
d96c882e | 286 | MEMBER(min, T_INT, READONLY, "KSZ.min -> smallest allowed key size") |
d96c882e MW |
287 | MEMBER(mod, T_INT, READONLY, |
288 | "KSZ.mod -> key size must be a multiple of this") | |
d7ab1bab | 289 | #undef MEMBERSTRUCT |
290 | { 0 } | |
291 | }; | |
292 | ||
a9410db7 MW |
293 | static const PyGetSetDef keyszrange_pygetset[] = { |
294 | #define GETSETNAME(op, name) kr##op##_##name | |
295 | GET (max, "KSZ.max -> largest allowed key size") | |
296 | #undef GETSETNAME | |
297 | { 0 } | |
298 | }; | |
299 | ||
c90f712e | 300 | static const PyGetSetDef keyszset_pygetset[] = { |
d7ab1bab | 301 | #define GETSETNAME(op, name) ks##op##_##name |
d96c882e MW |
302 | GET (min, "KSZ.min -> smallest allowed key size") |
303 | GET (max, "KSZ.max -> largest allowed key size") | |
d7ab1bab | 304 | #undef GETSETNAME |
305 | { 0 } | |
306 | }; | |
307 | ||
c90f712e | 308 | static const PyMemberDef keyszset_pymembers[] = { |
d7ab1bab | 309 | #define MEMBERSTRUCT keyszset_pyobj |
d96c882e | 310 | MEMBER(set, T_OBJECT, READONLY, "KSZ.set -> allowed key sizes") |
d7ab1bab | 311 | #undef MEMBERSTRUCT |
312 | { 0 } | |
313 | }; | |
314 | ||
c263b05c | 315 | static const PyTypeObject keysz_pytype_skel = { |
591bf41b | 316 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
c461c9b3 | 317 | "KeySZ", /* @tp_name@ */ |
d7ab1bab | 318 | sizeof(keysz_pyobj), /* @tp_basicsize@ */ |
319 | 0, /* @tp_itemsize@ */ | |
320 | ||
3aa33042 | 321 | 0, /* @tp_dealloc@ */ |
d7ab1bab | 322 | 0, /* @tp_print@ */ |
323 | 0, /* @tp_getattr@ */ | |
324 | 0, /* @tp_setattr@ */ | |
325 | 0, /* @tp_compare@ */ | |
326 | 0, /* @tp_repr@ */ | |
327 | 0, /* @tp_as_number@ */ | |
328 | 0, /* @tp_as_sequence@ */ | |
329 | 0, /* @tp_as_mapping@ */ | |
330 | 0, /* @tp_hash@ */ | |
331 | 0, /* @tp_call@ */ | |
332 | 0, /* @tp_str@ */ | |
333 | 0, /* @tp_getattro@ */ | |
334 | 0, /* @tp_setattro@ */ | |
335 | 0, /* @tp_as_buffer@ */ | |
336 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
337 | Py_TPFLAGS_BASETYPE, | |
338 | ||
339 | /* @tp_doc@ */ | |
d96c882e | 340 | "Key size constraints. Abstract.", |
d7ab1bab | 341 | |
342 | 0, /* @tp_traverse@ */ | |
343 | 0, /* @tp_clear@ */ | |
344 | 0, /* @tp_richcompare@ */ | |
345 | 0, /* @tp_weaklistoffset@ */ | |
346 | 0, /* @tp_iter@ */ | |
963a6148 | 347 | 0, /* @tp_iternext@ */ |
a157093f | 348 | PYMETHODS(keysz), /* @tp_methods@ */ |
c90f712e | 349 | PYMEMBERS(keysz), /* @tp_members@ */ |
d7ab1bab | 350 | 0, /* @tp_getset@ */ |
351 | 0, /* @tp_base@ */ | |
352 | 0, /* @tp_dict@ */ | |
353 | 0, /* @tp_descr_get@ */ | |
354 | 0, /* @tp_descr_set@ */ | |
355 | 0, /* @tp_dictoffset@ */ | |
356 | 0, /* @tp_init@ */ | |
357 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
358 | abstract_pynew, /* @tp_new@ */ | |
3aa33042 | 359 | 0, /* @tp_free@ */ |
d7ab1bab | 360 | 0 /* @tp_is_gc@ */ |
361 | }; | |
362 | ||
c263b05c | 363 | static const PyTypeObject keyszany_pytype_skel = { |
591bf41b | 364 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
c461c9b3 | 365 | "KeySZAny", /* @tp_name@ */ |
d7ab1bab | 366 | sizeof(keysz_pyobj), /* @tp_basicsize@ */ |
367 | 0, /* @tp_itemsize@ */ | |
368 | ||
3aa33042 | 369 | 0, /* @tp_dealloc@ */ |
d7ab1bab | 370 | 0, /* @tp_print@ */ |
371 | 0, /* @tp_getattr@ */ | |
372 | 0, /* @tp_setattr@ */ | |
373 | 0, /* @tp_compare@ */ | |
374 | 0, /* @tp_repr@ */ | |
375 | 0, /* @tp_as_number@ */ | |
376 | 0, /* @tp_as_sequence@ */ | |
377 | 0, /* @tp_as_mapping@ */ | |
378 | 0, /* @tp_hash@ */ | |
379 | 0, /* @tp_call@ */ | |
380 | 0, /* @tp_str@ */ | |
381 | 0, /* @tp_getattro@ */ | |
382 | 0, /* @tp_setattro@ */ | |
383 | 0, /* @tp_as_buffer@ */ | |
384 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
385 | Py_TPFLAGS_BASETYPE, | |
386 | ||
387 | /* @tp_doc@ */ | |
d96c882e MW |
388 | "KeySZAny(DEFAULT)\n" |
389 | " Key size constraints. This object imposes no constraints on size.", | |
d7ab1bab | 390 | |
391 | 0, /* @tp_traverse@ */ | |
392 | 0, /* @tp_clear@ */ | |
393 | 0, /* @tp_richcompare@ */ | |
394 | 0, /* @tp_weaklistoffset@ */ | |
395 | 0, /* @tp_iter@ */ | |
963a6148 | 396 | 0, /* @tp_iternext@ */ |
d7ab1bab | 397 | 0, /* @tp_methods@ */ |
398 | 0, /* @tp_members@ */ | |
c90f712e | 399 | PYGETSET(keyszany), /* @tp_getset@ */ |
d7ab1bab | 400 | 0, /* @tp_base@ */ |
401 | 0, /* @tp_dict@ */ | |
402 | 0, /* @tp_descr_get@ */ | |
403 | 0, /* @tp_descr_set@ */ | |
404 | 0, /* @tp_dictoffset@ */ | |
405 | 0, /* @tp_init@ */ | |
406 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
407 | keyszany_pynew, /* @tp_new@ */ | |
3aa33042 | 408 | 0, /* @tp_free@ */ |
d7ab1bab | 409 | 0 /* @tp_is_gc@ */ |
410 | }; | |
411 | ||
c263b05c | 412 | static const PyTypeObject keyszrange_pytype_skel = { |
591bf41b | 413 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
c461c9b3 | 414 | "KeySZRange", /* @tp_name@ */ |
d7ab1bab | 415 | sizeof(keyszrange_pyobj), /* @tp_basicsize@ */ |
416 | 0, /* @tp_itemsize@ */ | |
417 | ||
3aa33042 | 418 | 0, /* @tp_dealloc@ */ |
d7ab1bab | 419 | 0, /* @tp_print@ */ |
420 | 0, /* @tp_getattr@ */ | |
421 | 0, /* @tp_setattr@ */ | |
422 | 0, /* @tp_compare@ */ | |
423 | 0, /* @tp_repr@ */ | |
424 | 0, /* @tp_as_number@ */ | |
425 | 0, /* @tp_as_sequence@ */ | |
426 | 0, /* @tp_as_mapping@ */ | |
427 | 0, /* @tp_hash@ */ | |
428 | 0, /* @tp_call@ */ | |
429 | 0, /* @tp_str@ */ | |
430 | 0, /* @tp_getattro@ */ | |
431 | 0, /* @tp_setattro@ */ | |
432 | 0, /* @tp_as_buffer@ */ | |
433 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
434 | Py_TPFLAGS_BASETYPE, | |
435 | ||
436 | /* @tp_doc@ */ | |
d96c882e MW |
437 | "KeySZRange(DEFAULT, [min = 0], [max = 0], [mod = 1])\n" |
438 | " Key size constraints: size must be between MIN and MAX inclusive, and\n" | |
439 | " be a multiple of MOD.", | |
d7ab1bab | 440 | |
441 | 0, /* @tp_traverse@ */ | |
442 | 0, /* @tp_clear@ */ | |
443 | 0, /* @tp_richcompare@ */ | |
444 | 0, /* @tp_weaklistoffset@ */ | |
445 | 0, /* @tp_iter@ */ | |
963a6148 | 446 | 0, /* @tp_iternext@ */ |
d7ab1bab | 447 | 0, /* @tp_methods@ */ |
c90f712e | 448 | PYMEMBERS(keyszrange), /* @tp_members@ */ |
a9410db7 | 449 | PYGETSET(keyszrange), /* @tp_getset@ */ |
d7ab1bab | 450 | 0, /* @tp_base@ */ |
451 | 0, /* @tp_dict@ */ | |
452 | 0, /* @tp_descr_get@ */ | |
453 | 0, /* @tp_descr_set@ */ | |
454 | 0, /* @tp_dictoffset@ */ | |
455 | 0, /* @tp_init@ */ | |
456 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
457 | keyszrange_pynew, /* @tp_new@ */ | |
3aa33042 | 458 | 0, /* @tp_free@ */ |
d7ab1bab | 459 | 0 /* @tp_is_gc@ */ |
460 | }; | |
461 | ||
c263b05c | 462 | static const PyTypeObject keyszset_pytype_skel = { |
591bf41b | 463 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
c461c9b3 | 464 | "KeySZSet", /* @tp_name@ */ |
d7ab1bab | 465 | sizeof(keyszset_pyobj), /* @tp_basicsize@ */ |
466 | 0, /* @tp_itemsize@ */ | |
467 | ||
3aa33042 | 468 | 0, /* @tp_dealloc@ */ |
d7ab1bab | 469 | 0, /* @tp_print@ */ |
470 | 0, /* @tp_getattr@ */ | |
471 | 0, /* @tp_setattr@ */ | |
472 | 0, /* @tp_compare@ */ | |
473 | 0, /* @tp_repr@ */ | |
474 | 0, /* @tp_as_number@ */ | |
475 | 0, /* @tp_as_sequence@ */ | |
476 | 0, /* @tp_as_mapping@ */ | |
477 | 0, /* @tp_hash@ */ | |
478 | 0, /* @tp_call@ */ | |
479 | 0, /* @tp_str@ */ | |
480 | 0, /* @tp_getattro@ */ | |
481 | 0, /* @tp_setattro@ */ | |
482 | 0, /* @tp_as_buffer@ */ | |
483 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
484 | Py_TPFLAGS_BASETYPE, | |
485 | ||
486 | /* @tp_doc@ */ | |
fd46a88d MW |
487 | "KeySZSet(DEFAULT, ITER)\n" |
488 | " Key size constraints: size must be DEFAULT or an element of ITER.", | |
d7ab1bab | 489 | |
490 | 0, /* @tp_traverse@ */ | |
491 | 0, /* @tp_clear@ */ | |
492 | 0, /* @tp_richcompare@ */ | |
493 | 0, /* @tp_weaklistoffset@ */ | |
494 | 0, /* @tp_iter@ */ | |
963a6148 | 495 | 0, /* @tp_iternext@ */ |
d7ab1bab | 496 | 0, /* @tp_methods@ */ |
c90f712e MW |
497 | PYMEMBERS(keyszset), /* @tp_members@ */ |
498 | PYGETSET(keyszset), /* @tp_getset@ */ | |
d7ab1bab | 499 | 0, /* @tp_base@ */ |
500 | 0, /* @tp_dict@ */ | |
501 | 0, /* @tp_descr_get@ */ | |
502 | 0, /* @tp_descr_set@ */ | |
503 | 0, /* @tp_dictoffset@ */ | |
504 | 0, /* @tp_init@ */ | |
505 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
506 | keyszset_pynew, /* @tp_new@ */ | |
3aa33042 | 507 | 0, /* @tp_free@ */ |
d7ab1bab | 508 | 0 /* @tp_is_gc@ */ |
509 | }; | |
510 | ||
511 | /*----- Symmetric encryption ----------------------------------------------*/ | |
512 | ||
c6e89d48 MW |
513 | static PyTypeObject *gccipher_pytype, *gcipher_pytype; |
514 | ||
515 | typedef struct gccipher_pyobj { | |
516 | PyHeapTypeObject ty; | |
517 | gccipher *cc; | |
518 | } gccipher_pyobj; | |
519 | ||
520 | #define GCCIPHER_PYCHECK(o) PyObject_TypeCheck((o), gccipher_pytype) | |
521 | #define GCCIPHER_CC(o) (((gccipher_pyobj *)(o))->cc) | |
522 | ||
523 | typedef struct gcipher_pyobj { | |
524 | PyObject_HEAD | |
525 | gcipher *c; | |
526 | } gcipher_pyobj; | |
527 | ||
528 | #define GCIPHER_PYCHECK(o) PyObject_TypeCheck((o), gcipher_pytype) | |
529 | #define GCIPHER_C(o) (((gcipher_pyobj *)(o))->c) | |
d7ab1bab | 530 | |
531 | CONVFUNC(gccipher, gccipher *, GCCIPHER_CC) | |
d7ab1bab | 532 | |
c6e89d48 | 533 | static PyObject *gcipher_pywrap(PyObject *cobj, gcipher *c) |
d7ab1bab | 534 | { |
535 | gcipher_pyobj *g; | |
536 | if (!cobj) cobj = gccipher_pywrap((/*unconst*/ gccipher *)GC_CLASS(c)); | |
537 | else Py_INCREF(cobj); | |
538 | g = PyObject_NEW(gcipher_pyobj, (PyTypeObject *)cobj); | |
539 | g->c = c; | |
b2687a0a | 540 | return ((PyObject *)g); |
d7ab1bab | 541 | } |
542 | ||
543 | static PyObject *gcipher_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
544 | { | |
827f89d7 | 545 | static const char *const kwlist[] = { "k", 0 }; |
21684137 MW |
546 | struct bin k = BIN_INIT; |
547 | PyObject *rc = 0; | |
d7ab1bab | 548 | |
67c75893 | 549 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", KWLIST, convbin, &k)) |
d7ab1bab | 550 | goto end; |
67c75893 | 551 | if (keysz(k.sz, GCCIPHER_CC(ty)->keysz) != k.sz) VALERR("bad key length"); |
21684137 | 552 | rc = gcipher_pywrap((PyObject *)ty, GC_INIT(GCCIPHER_CC(ty), k.p, k.sz)); |
d7ab1bab | 553 | end: |
21684137 MW |
554 | freebin(&k); |
555 | return (rc); | |
d7ab1bab | 556 | } |
557 | ||
558 | PyObject *gccipher_pywrap(gccipher *cc) | |
559 | { | |
df9f8366 | 560 | gccipher_pyobj *g = newtype(gccipher_pytype, 0, cc->name); |
d7ab1bab | 561 | g->cc = cc; |
24b3d57b MW |
562 | g->ty.ht_type.tp_basicsize = sizeof(gcipher_pyobj); |
563 | g->ty.ht_type.tp_base = gcipher_pytype; | |
d7ab1bab | 564 | Py_INCREF(gcipher_pytype); |
24b3d57b MW |
565 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
566 | Py_TPFLAGS_BASETYPE | | |
567 | Py_TPFLAGS_HEAPTYPE); | |
568 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; | |
569 | g->ty.ht_type.tp_free = 0; | |
570 | g->ty.ht_type.tp_new = gcipher_pynew; | |
dc075750 | 571 | typeready(&g->ty.ht_type); |
d7ab1bab | 572 | return ((PyObject *)g); |
573 | } | |
574 | ||
575 | static void gcipher_pydealloc(PyObject *me) | |
576 | { | |
c41d0371 | 577 | GC_DESTROY(GCIPHER_C(me)); |
2a448f86 | 578 | Py_DECREF(Py_TYPE(me)); |
3aa33042 | 579 | FREEOBJ(me); |
d7ab1bab | 580 | } |
581 | ||
582 | static PyObject *gccget_name(PyObject *me, void *hunoz) | |
2135a6d3 | 583 | { return (TEXT_FROMSTR(GCCIPHER_CC(me)->name)); } |
d7ab1bab | 584 | |
585 | static PyObject *gccget_keysz(PyObject *me, void *hunoz) | |
586 | { return (keysz_pywrap(GCCIPHER_CC(me)->keysz)); } | |
587 | ||
588 | static PyObject *gccget_blksz(PyObject *me, void *hunoz) | |
589 | { return (PyInt_FromLong(GCCIPHER_CC(me)->blksz)); } | |
590 | ||
591 | static PyObject *gcmeth_encrypt(PyObject *me, PyObject *arg) | |
592 | { | |
21684137 | 593 | struct bin m = BIN_INIT; |
d7ab1bab | 594 | PyObject *rc = 0; |
595 | ||
21684137 | 596 | if (!PyArg_ParseTuple(arg, "O&:encrypt", convbin, &m)) goto end; |
67c75893 | 597 | rc = bytestring_pywrap(0, m.sz); |
2135a6d3 | 598 | GC_ENCRYPT(GCIPHER_C(me), m.p, BIN_PTR(rc), m.sz); |
21684137 MW |
599 | end: |
600 | freebin(&m); | |
d7ab1bab | 601 | return (rc); |
602 | } | |
603 | ||
604 | static PyObject *gcmeth_enczero(PyObject *me, PyObject *arg) | |
605 | { | |
606 | char *p; | |
607 | int sz; | |
608 | PyObject *rc = 0; | |
609 | ||
610 | if (!PyArg_ParseTuple(arg, "i:enczero", &sz)) return (0); | |
611 | rc = bytestring_pywrap(0, sz); | |
2135a6d3 | 612 | p = BIN_PTR(rc); |
d7ab1bab | 613 | memset(p, 0, sz); |
614 | GC_ENCRYPT(GCIPHER_C(me), p, p, sz); | |
615 | return (rc); | |
616 | } | |
617 | ||
618 | static PyObject *gcmeth_decrypt(PyObject *me, PyObject *arg) | |
619 | { | |
21684137 | 620 | struct bin c = BIN_INIT; |
d7ab1bab | 621 | PyObject *rc = 0; |
622 | ||
21684137 | 623 | if (!PyArg_ParseTuple(arg, "O&:decrypt", convbin, &c)) goto end; |
67c75893 | 624 | rc = bytestring_pywrap(0, c.sz); |
2135a6d3 | 625 | GC_DECRYPT(GCIPHER_C(me), c.p, BIN_PTR(rc), c.sz); |
21684137 MW |
626 | end: |
627 | freebin(&c); | |
d7ab1bab | 628 | return (rc); |
629 | } | |
630 | ||
631 | static PyObject *gcmeth_deczero(PyObject *me, PyObject *arg) | |
632 | { | |
633 | char *p; | |
634 | int sz; | |
635 | PyObject *rc = 0; | |
636 | ||
637 | if (!PyArg_ParseTuple(arg, "i:deczero", &sz)) return (0); | |
638 | rc = bytestring_pywrap(0, sz); | |
2135a6d3 | 639 | p = BIN_PTR(rc); |
d7ab1bab | 640 | memset(p, 0, sz); |
641 | GC_DECRYPT(GCIPHER_C(me), p, p, sz); | |
642 | return (rc); | |
643 | } | |
644 | ||
645 | static PyObject *gcmeth_setiv(PyObject *me, PyObject *arg) | |
646 | { | |
21684137 MW |
647 | struct bin iv = BIN_INIT; |
648 | PyObject *rc = 0; | |
d7ab1bab | 649 | |
67c75893 | 650 | if (!PyArg_ParseTuple(arg, "O&:setiv", convbin, &iv)) goto end; |
30eef666 | 651 | if (!GCIPHER_C(me)->ops->setiv) VALERR("`setiv' not supported"); |
d7ab1bab | 652 | if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode"); |
67c75893 MW |
653 | if (iv.sz != GC_CLASS(GCIPHER_C(me))->blksz) VALERR("bad IV length"); |
654 | GC_SETIV(GCIPHER_C(me), iv.p); | |
21684137 | 655 | rc = me; Py_INCREF(rc); |
d7ab1bab | 656 | end: |
21684137 MW |
657 | freebin(&iv); |
658 | return (rc); | |
d7ab1bab | 659 | } |
660 | ||
91e56f06 | 661 | static PyObject *gcmeth_bdry(PyObject *me) |
d7ab1bab | 662 | { |
30eef666 | 663 | if (!GCIPHER_C(me)->ops->bdry) VALERR("`bdry' not supported"); |
d7ab1bab | 664 | if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode"); |
665 | GC_BDRY(GCIPHER_C(me)); | |
666 | RETURN_ME; | |
667 | end: | |
668 | return (0); | |
669 | } | |
670 | ||
c90f712e | 671 | static const PyGetSetDef gccipher_pygetset[] = { |
d7ab1bab | 672 | #define GETSETNAME(op, name) gcc##op##_##name |
d96c882e MW |
673 | GET (keysz, "CC.keysz -> acceptable key sizes") |
674 | GET (blksz, "CC.blksz -> block size, or zero") | |
675 | GET (name, "CC.name -> name of this kind of cipher") | |
d7ab1bab | 676 | #undef GETSETNAME |
677 | { 0 } | |
678 | }; | |
679 | ||
c90f712e | 680 | static const PyMethodDef gcipher_pymethods[] = { |
d7ab1bab | 681 | #define METHNAME(name) gcmeth_##name |
d96c882e MW |
682 | METH (encrypt, "C.encrypt(PT) -> CT") |
683 | METH (enczero, "C.enczero(N) -> CT") | |
684 | METH (decrypt, "C.decrypt(CT) -> PT") | |
685 | METH (deczero, "C.deczero(N) -> PT") | |
686 | METH (setiv, "C.setiv(IV)") | |
91e56f06 | 687 | NAMETH(bdry, "C.bdry()") |
d7ab1bab | 688 | #undef METHNAME |
689 | { 0 } | |
690 | }; | |
691 | ||
c263b05c | 692 | static const PyTypeObject gccipher_pytype_skel = { |
591bf41b | 693 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
c461c9b3 | 694 | "GCCipher", /* @tp_name@ */ |
d7ab1bab | 695 | sizeof(gccipher_pyobj), /* @tp_basicsize@ */ |
696 | 0, /* @tp_itemsize@ */ | |
697 | ||
698 | 0, /* @tp_dealloc@ */ | |
699 | 0, /* @tp_print@ */ | |
700 | 0, /* @tp_getattr@ */ | |
701 | 0, /* @tp_setattr@ */ | |
702 | 0, /* @tp_compare@ */ | |
703 | 0, /* @tp_repr@ */ | |
704 | 0, /* @tp_as_number@ */ | |
705 | 0, /* @tp_as_sequence@ */ | |
706 | 0, /* @tp_as_mapping@ */ | |
707 | 0, /* @tp_hash@ */ | |
708 | 0, /* @tp_call@ */ | |
709 | 0, /* @tp_str@ */ | |
710 | 0, /* @tp_getattro@ */ | |
711 | 0, /* @tp_setattro@ */ | |
712 | 0, /* @tp_as_buffer@ */ | |
713 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
714 | Py_TPFLAGS_BASETYPE, | |
715 | ||
716 | /* @tp_doc@ */ | |
d96c882e | 717 | "Symmetric cipher metaclass.", |
d7ab1bab | 718 | |
719 | 0, /* @tp_traverse@ */ | |
720 | 0, /* @tp_clear@ */ | |
721 | 0, /* @tp_richcompare@ */ | |
722 | 0, /* @tp_weaklistoffset@ */ | |
723 | 0, /* @tp_iter@ */ | |
963a6148 | 724 | 0, /* @tp_iternext@ */ |
d7ab1bab | 725 | 0, /* @tp_methods@ */ |
726 | 0, /* @tp_members@ */ | |
c90f712e | 727 | PYGETSET(gccipher), /* @tp_getset@ */ |
d7ab1bab | 728 | 0, /* @tp_base@ */ |
729 | 0, /* @tp_dict@ */ | |
730 | 0, /* @tp_descr_get@ */ | |
731 | 0, /* @tp_descr_set@ */ | |
732 | 0, /* @tp_dictoffset@ */ | |
733 | 0, /* @tp_init@ */ | |
734 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
735 | abstract_pynew, /* @tp_new@ */ | |
3aa33042 | 736 | 0, /* @tp_free@ */ |
d7ab1bab | 737 | 0 /* @tp_is_gc@ */ |
738 | }; | |
739 | ||
c263b05c | 740 | static const PyTypeObject gcipher_pytype_skel = { |
591bf41b | 741 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
c461c9b3 | 742 | "GCipher", /* @tp_name@ */ |
d7ab1bab | 743 | sizeof(gcipher_pyobj), /* @tp_basicsize@ */ |
744 | 0, /* @tp_itemsize@ */ | |
745 | ||
746 | gcipher_pydealloc, /* @tp_dealloc@ */ | |
747 | 0, /* @tp_print@ */ | |
748 | 0, /* @tp_getattr@ */ | |
749 | 0, /* @tp_setattr@ */ | |
750 | 0, /* @tp_compare@ */ | |
751 | 0, /* @tp_repr@ */ | |
752 | 0, /* @tp_as_number@ */ | |
753 | 0, /* @tp_as_sequence@ */ | |
754 | 0, /* @tp_as_mapping@ */ | |
755 | 0, /* @tp_hash@ */ | |
756 | 0, /* @tp_call@ */ | |
757 | 0, /* @tp_str@ */ | |
758 | 0, /* @tp_getattro@ */ | |
759 | 0, /* @tp_setattro@ */ | |
760 | 0, /* @tp_as_buffer@ */ | |
761 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
762 | Py_TPFLAGS_BASETYPE, | |
763 | ||
764 | /* @tp_doc@ */ | |
d96c882e | 765 | "Symmetric cipher, abstract base class.", |
d7ab1bab | 766 | |
767 | 0, /* @tp_traverse@ */ | |
768 | 0, /* @tp_clear@ */ | |
769 | 0, /* @tp_richcompare@ */ | |
770 | 0, /* @tp_weaklistoffset@ */ | |
771 | 0, /* @tp_iter@ */ | |
963a6148 | 772 | 0, /* @tp_iternext@ */ |
c90f712e | 773 | PYMETHODS(gcipher), /* @tp_methods@ */ |
d7ab1bab | 774 | 0, /* @tp_members@ */ |
775 | 0, /* @tp_getset@ */ | |
776 | 0, /* @tp_base@ */ | |
777 | 0, /* @tp_dict@ */ | |
778 | 0, /* @tp_descr_get@ */ | |
779 | 0, /* @tp_descr_set@ */ | |
780 | 0, /* @tp_dictoffset@ */ | |
781 | 0, /* @tp_init@ */ | |
782 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
783 | abstract_pynew, /* @tp_new@ */ | |
3aa33042 | 784 | 0, /* @tp_free@ */ |
d7ab1bab | 785 | 0 /* @tp_is_gc@ */ |
786 | }; | |
787 | ||
bebf03ab MW |
788 | /*----- Authenticated encryption ------------------------------------------*/ |
789 | ||
c6e89d48 MW |
790 | static PyTypeObject *gcaead_pytype, *gaeadkey_pytype; |
791 | static PyTypeObject *gcaeadaad_pytype, *gaeadaad_pytype; | |
792 | static PyTypeObject *gcaeadenc_pytype, *gaeadenc_pytype; | |
793 | static PyTypeObject *gcaeaddec_pytype, *gaeaddec_pytype; | |
794 | ||
795 | typedef struct gcaead_pyobj { | |
796 | PyHeapTypeObject ty; | |
797 | gcaead *aec; | |
798 | struct gcaeadaad_pyobj *aad; | |
799 | struct gcaeadenc_pyobj *enc; | |
800 | struct gcaeaddec_pyobj *dec; | |
801 | } gcaead_pyobj; | |
802 | ||
803 | #define GCAEAD_PYCHECK(o) PyObject_TypeCheck((o), gcaead_pytype) | |
804 | #define GCAEAD_AEC(o) (((gcaead_pyobj *)(o))->aec) | |
805 | #define GCAEAD_AAD(o) (((gcaead_pyobj *)(o))->aad) | |
806 | #define GCAEAD_ENC(o) (((gcaead_pyobj *)(o))->enc) | |
807 | #define GCAEAD_DEC(o) (((gcaead_pyobj *)(o))->dec) | |
808 | static PyObject *gcaead_pywrap(gcaead *); | |
809 | ||
810 | typedef struct gaeadkey_pyobj { | |
811 | PyObject_HEAD | |
812 | gaead_key *k; | |
813 | } gaeadkey_pyobj; | |
814 | ||
815 | #define GAEADKEY_PYCHECK(o) PyObject_TypeCheck((o), gaeadkey_pytype) | |
816 | #define GAEADKEY_K(o) (((gaeadkey_pyobj *)(o))->k) | |
817 | ||
818 | typedef struct gcaeadaad_pyobj { | |
819 | PyHeapTypeObject ty; | |
820 | gcaead_pyobj *key; | |
821 | } gcaeadaad_pyobj; | |
822 | ||
823 | #define GCAEADAAD_KEY(o) (((gcaeadaad_pyobj *)(o))->key) | |
824 | static PyObject *gaeadaad_pywrap(PyObject *, gaead_aad *, unsigned, size_t); | |
825 | ||
826 | typedef struct gaeadaad_pyobj { | |
827 | PyObject_HEAD | |
828 | gaead_aad *a; | |
829 | unsigned f; | |
830 | #define AEADF_DEAD 32768u | |
831 | size_t hsz, hlen; | |
832 | } gaeadaad_pyobj; | |
833 | ||
834 | #define GAEADAAD_PYCHECK(o) PyObject_TypeCheck((o), gaeadaad_pytype) | |
835 | #define GAEADAAD_A(o) (((gaeadaad_pyobj *)(o))->a) | |
836 | #define GAEADAAD_F(o) (((gaeadaad_pyobj *)(o))->f) | |
837 | #define GAEADAAD_HSZ(o) (((gaeadaad_pyobj *)(o))->hsz) | |
838 | #define GAEADAAD_HLEN(o) (((gaeadaad_pyobj *)(o))->hlen) | |
839 | ||
840 | typedef struct gcaeadenc_pyobj { | |
841 | PyHeapTypeObject ty; | |
842 | gcaead_pyobj *key; | |
843 | } gcaeadenc_pyobj; | |
844 | ||
845 | #define GCAEADENC_KEY(o) (((gcaeadenc_pyobj *)(o))->key) | |
846 | static PyObject *gaeadenc_pywrap(PyObject *, gaead_enc *, unsigned, | |
847 | size_t, size_t, size_t); | |
848 | ||
849 | typedef struct gaeadenc_pyobj { | |
850 | PyObject_HEAD | |
851 | gaead_enc *e; | |
852 | gaeadaad_pyobj *aad; | |
853 | unsigned f; | |
854 | size_t hsz, msz, tsz; | |
855 | size_t mlen; | |
856 | } gaeadenc_pyobj; | |
857 | ||
858 | #define GAEADENC_PYCHECK(o) PyObject_TypeCheck((o), gaeadenc_pytype) | |
859 | #define GAEADENC_AAD(o) (((gaeadenc_pyobj *)(o))->aad) | |
860 | #define GAEADENC_E(o) (((gaeadenc_pyobj *)(o))->e) | |
861 | #define GAEADENC_F(o) (((gaeadenc_pyobj *)(o))->f) | |
862 | #define GAEADENC_HSZ(o) (((gaeadenc_pyobj *)(o))->hsz) | |
863 | #define GAEADENC_MSZ(o) (((gaeadenc_pyobj *)(o))->msz) | |
864 | #define GAEADENC_TSZ(o) (((gaeadenc_pyobj *)(o))->tsz) | |
865 | #define GAEADENC_MLEN(o) (((gaeadenc_pyobj *)(o))->mlen) | |
866 | ||
867 | typedef struct gcaeaddec_pyobj { | |
868 | PyHeapTypeObject ty; | |
869 | gcaead_pyobj *key; | |
870 | } gcaeaddec_pyobj; | |
871 | ||
872 | #define GCAEADDEC_KEY(o) (((gcaeaddec_pyobj *)(o))->key) | |
873 | static PyObject *gaeaddec_pywrap(PyObject *, gaead_dec *, unsigned, | |
874 | size_t, size_t, size_t); | |
875 | ||
876 | typedef struct gaeaddec_pyobj { | |
877 | PyObject_HEAD | |
878 | gaead_dec *d; | |
879 | gaeadaad_pyobj *aad; | |
880 | unsigned f; | |
881 | size_t hsz, csz, tsz; | |
882 | size_t clen; | |
883 | } gaeaddec_pyobj; | |
884 | ||
885 | #define GAEADDEC_PYCHECK(o) PyObject_TypeCheck((o), gaeaddec_pytype) | |
886 | #define GAEADDEC_AAD(o) (((gaeaddec_pyobj *)(o))->aad) | |
887 | #define GAEADDEC_D(o) (((gaeaddec_pyobj *)(o))->d) | |
888 | #define GAEADDEC_F(o) (((gaeaddec_pyobj *)(o))->f) | |
889 | #define GAEADDEC_HSZ(o) (((gaeaddec_pyobj *)(o))->hsz) | |
890 | #define GAEADDEC_CSZ(o) (((gaeaddec_pyobj *)(o))->csz) | |
891 | #define GAEADDEC_TSZ(o) (((gaeaddec_pyobj *)(o))->tsz) | |
892 | #define GAEADDEC_CLEN(o) (((gaeaddec_pyobj *)(o))->clen) | |
893 | ||
894 | static PyObject *gaeadkey_pywrap(PyObject *cobj, gaead_key *k) | |
bebf03ab MW |
895 | { |
896 | gaeadkey_pyobj *gk; | |
897 | ||
898 | if (!cobj) cobj = gcaead_pywrap((/*unconst*/ gcaead *)GAEAD_CLASS(k)); | |
899 | else Py_INCREF(cobj); | |
900 | gk = PyObject_NEW(gaeadkey_pyobj, (PyTypeObject *)cobj); | |
901 | gk->k = k; | |
902 | return ((PyObject *)gk); | |
903 | } | |
904 | ||
905 | static PyObject *gaeadkey_pynew(PyTypeObject *ty, | |
906 | PyObject *arg, PyObject *kw) | |
907 | { | |
908 | static const char *const kwlist[] = { "k", 0 }; | |
21684137 MW |
909 | struct bin k = BIN_INIT; |
910 | PyObject *rc = 0; | |
bebf03ab | 911 | |
67c75893 | 912 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", KWLIST, convbin, &k)) |
bebf03ab | 913 | goto end; |
67c75893 | 914 | if (keysz(k.sz, GCAEAD_AEC(ty)->keysz) != k.sz) VALERR("bad key length"); |
21684137 MW |
915 | rc = gaeadkey_pywrap((PyObject *)ty, |
916 | GAEAD_KEY(GCAEAD_AEC(ty), k.p, k.sz)); | |
bebf03ab | 917 | end: |
21684137 MW |
918 | freebin(&k); |
919 | return (rc); | |
bebf03ab MW |
920 | } |
921 | ||
c6e89d48 | 922 | static PyObject *gcaead_pywrap(gcaead *aec) |
bebf03ab MW |
923 | { |
924 | gcaead_pyobj *gck; | |
925 | gcaeadaad_pyobj *gca; | |
926 | gcaeadenc_pyobj *gce; | |
927 | gcaeaddec_pyobj *gcd; | |
928 | ||
929 | #define MKTYPE(obj, thing, newfn, namefmt) do { \ | |
930 | (obj) = newtype(gcaead_pytype, 0, 0); \ | |
2135a6d3 MW |
931 | (obj)->ty.ht_name = TEXT_FORMAT(namefmt, aec->name); \ |
932 | (obj)->ty.ht_type.tp_name = TEXT_PTR((obj)->ty.ht_name); \ | |
bebf03ab MW |
933 | (obj)->ty.ht_type.tp_basicsize = sizeof(gaead##thing##_pyobj); \ |
934 | (obj)->ty.ht_type.tp_base = gaead##thing##_pytype; \ | |
935 | Py_INCREF(gaead##thing##_pytype); \ | |
936 | (obj)->ty.ht_type.tp_flags = \ | |
937 | (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE); \ | |
938 | (obj)->ty.ht_type.tp_alloc = PyType_GenericAlloc; \ | |
939 | (obj)->ty.ht_type.tp_free = 0; \ | |
940 | (obj)->ty.ht_type.tp_new = newfn; \ | |
941 | typeready(&(obj)->ty.ht_type); \ | |
942 | } while (0) | |
943 | ||
944 | MKTYPE(gck, key, gaeadkey_pynew, "%s(key)"); | |
945 | MKTYPE(gca, aad, abstract_pynew, "%s(aad-hash)"); | |
946 | MKTYPE(gce, enc, abstract_pynew, "%s(encrypt)"); | |
947 | MKTYPE(gcd, dec, abstract_pynew, "%s(decrypt)"); | |
948 | ||
949 | #undef MKTYPE | |
950 | ||
951 | gck->aec = aec; gck->aad = gca; gck->enc = gce; gck->dec = gcd; | |
952 | gca->key = gce->key = gcd->key = gck; | |
953 | return ((PyObject *)gck); | |
954 | } | |
955 | ||
956 | static void gaeadkey_pydealloc(PyObject *me) | |
2a448f86 | 957 | { GAEAD_DESTROY(GAEADKEY_K(me)); Py_DECREF(Py_TYPE(me)); FREEOBJ(me); } |
bebf03ab MW |
958 | |
959 | static PyObject *gcaeget_name(PyObject *me, void *hunoz) | |
2135a6d3 | 960 | { return (TEXT_FROMSTR(GCAEAD_AEC(me)->name)); } |
bebf03ab MW |
961 | |
962 | static PyObject *gcaeget_keysz(PyObject *me, void *hunoz) | |
963 | { return (keysz_pywrap(GCAEAD_AEC(me)->keysz)); } | |
964 | ||
965 | static PyObject *gcaeget_noncesz(PyObject *me, void *hunoz) | |
966 | { return (keysz_pywrap(GCAEAD_AEC(me)->noncesz)); } | |
967 | ||
968 | static PyObject *gcaeget_tagsz(PyObject *me, void *hunoz) | |
969 | { return (keysz_pywrap(GCAEAD_AEC(me)->tagsz)); } | |
970 | ||
971 | static PyObject *gcaeget_blksz(PyObject *me, void *hunoz) | |
972 | { return (PyInt_FromLong(GCAEAD_AEC(me)->blksz)); } | |
973 | ||
974 | static PyObject *gcaeget_bufsz(PyObject *me, void *hunoz) | |
975 | { return (PyInt_FromLong(GCAEAD_AEC(me)->bufsz)); } | |
976 | ||
977 | static PyObject *gcaeget_ohd(PyObject *me, void *hunoz) | |
978 | { return (PyInt_FromLong(GCAEAD_AEC(me)->ohd)); } | |
979 | ||
980 | static PyObject *gcaeget_flags(PyObject *me, void *hunoz) | |
981 | { return (PyInt_FromLong(GCAEAD_AEC(me)->f)); } | |
982 | ||
c90f712e | 983 | static const PyGetSetDef gcaead_pygetset[] = { |
bebf03ab | 984 | #define GETSETNAME(op, name) gcae##op##_##name |
d96c882e MW |
985 | GET (keysz, "AEC.keysz -> acceptable key sizes") |
986 | GET (noncesz, "AEC.noncesz -> acceptable nonce sizes") | |
987 | GET (tagsz, "AEC.tagsz -> acceptable tag sizes") | |
988 | GET (blksz, "AEC.blksz -> block size, or zero") | |
989 | GET (bufsz, "AEC.bufsz -> amount of data buffered internally") | |
990 | GET (ohd, "AEC.ohd -> maximum encryption overhead") | |
991 | GET (name, "AEC.name -> name of this kind of AEAD scheme") | |
992 | GET (flags, "AEC.flags -> mask of `AEADF_...' flags") | |
bebf03ab MW |
993 | #undef GETSETNAME |
994 | { 0 } | |
995 | }; | |
996 | ||
91e56f06 | 997 | static PyObject *gaekmeth_aad(PyObject *me) |
bebf03ab MW |
998 | { |
999 | const gaead_key *k = GAEADKEY_K(me); | |
1000 | PyObject *rc = 0; | |
1001 | ||
bebf03ab MW |
1002 | if (k->ops->c->f&AEADF_AADNDEP) |
1003 | VALERR("aad must be associated with enc/dec op"); | |
2a448f86 | 1004 | rc = gaeadaad_pywrap((PyObject *)GCAEAD_AAD(Py_TYPE(me)), |
bebf03ab MW |
1005 | GAEAD_AAD(k), 0, 0); |
1006 | end: | |
1007 | return (rc); | |
1008 | } | |
1009 | ||
1010 | static int check_aead_encdec(const gcaead *aec, unsigned *f_out, size_t nsz, | |
1011 | PyObject *hszobj, size_t *hsz_out, | |
1012 | PyObject *mszobj, size_t *msz_out, | |
1013 | PyObject *tszobj, size_t *tsz_out) | |
1014 | { | |
1015 | unsigned f = 0, miss; | |
1016 | int rc = -1; | |
1017 | ||
1018 | if (hszobj != Py_None) | |
1019 | { f |= AEADF_PCHSZ; if (!convszt(hszobj, hsz_out)) goto end; } | |
1020 | if (mszobj != Py_None) | |
1021 | { f |= AEADF_PCMSZ; if (!convszt(mszobj, msz_out)) goto end; } | |
1022 | if (tszobj != Py_None) | |
1023 | { f |= AEADF_PCTSZ; if (!convszt(tszobj, tsz_out)) goto end; } | |
1024 | miss = aec->f&~f; | |
1025 | if (miss&AEADF_PCHSZ) VALERR("header length precommitment required"); | |
1026 | if (miss&AEADF_PCMSZ) VALERR("message length precommitment required"); | |
1027 | if (miss&AEADF_PCTSZ) VALERR("tag length precommitment required"); | |
1028 | if (keysz(nsz, aec->noncesz) != nsz) VALERR("bad nonce length"); | |
1029 | if (tszobj != Py_None && keysz(*tsz_out, aec->tagsz) != *tsz_out) | |
1030 | VALERR("bad tag length"); | |
1031 | *f_out = f | aec->f; rc = 0; | |
1032 | end: | |
1033 | return (rc); | |
1034 | } | |
1035 | ||
1036 | static PyObject *gaekmeth_enc(PyObject *me, PyObject *arg, PyObject *kw) | |
1037 | { | |
1038 | static const char *const kwlist[] = { "nonce", "hsz", "msz", "tsz", 0 }; | |
1039 | const gaead_key *k = GAEADKEY_K(me); | |
1040 | gaead_enc *e; | |
1041 | PyObject *rc = 0; | |
21684137 | 1042 | struct bin n = BIN_INIT; |
bebf03ab MW |
1043 | PyObject *hszobj = Py_None, *mszobj = Py_None, *tszobj = Py_None; |
1044 | size_t hsz = 0, msz = 0, tsz = 0; | |
1045 | unsigned f; | |
1046 | ||
67c75893 MW |
1047 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|OOO:enc", KWLIST, |
1048 | convbin, &n, &hszobj, &mszobj, &tszobj)) | |
bebf03ab | 1049 | goto end; |
67c75893 | 1050 | if (check_aead_encdec(k->ops->c, &f, n.sz, |
bebf03ab MW |
1051 | hszobj, &hsz, mszobj, &msz, tszobj, &tsz)) |
1052 | goto end; | |
67c75893 | 1053 | e = GAEAD_ENC(GAEADKEY_K(me), n.p, n.sz, hsz, msz, tsz); |
bebf03ab | 1054 | if (!e) VALERR("bad aead parameter combination"); |
2a448f86 | 1055 | rc = gaeadenc_pywrap((PyObject *)GCAEAD_ENC(Py_TYPE(me)), |
bebf03ab MW |
1056 | e, f, hsz, msz, tsz); |
1057 | end: | |
21684137 | 1058 | freebin(&n); |
bebf03ab MW |
1059 | return (rc); |
1060 | } | |
1061 | ||
1062 | static PyObject *gaekmeth_dec(PyObject *me, PyObject *arg, PyObject *kw) | |
1063 | { | |
1064 | static const char *const kwlist[] = { "nonce", "hsz", "csz", "tsz", 0 }; | |
1065 | const gaead_key *k = GAEADKEY_K(me); | |
1066 | gaead_dec *d; | |
1067 | PyObject *rc = 0; | |
21684137 | 1068 | struct bin n = BIN_INIT; |
bebf03ab MW |
1069 | PyObject *hszobj = Py_None, *cszobj = Py_None, *tszobj = Py_None; |
1070 | size_t hsz = 0, csz = 0, tsz = 0; | |
1071 | unsigned f; | |
1072 | ||
67c75893 MW |
1073 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|OOO:dec", KWLIST, |
1074 | convbin, &n, &hszobj, &cszobj, &tszobj)) | |
bebf03ab | 1075 | goto end; |
67c75893 | 1076 | if (check_aead_encdec(k->ops->c, &f, n.sz, |
bebf03ab MW |
1077 | hszobj, &hsz, cszobj, &csz, tszobj, &tsz)) |
1078 | goto end; | |
67c75893 | 1079 | d = GAEAD_DEC(GAEADKEY_K(me), n.p, n.sz, hsz, csz, tsz); |
bebf03ab | 1080 | if (!d) VALERR("bad aead parameter combination"); |
2a448f86 | 1081 | rc = gaeaddec_pywrap((PyObject *)GCAEAD_DEC(Py_TYPE(me)), |
bebf03ab MW |
1082 | d, f, hsz, csz, tsz); |
1083 | end: | |
21684137 | 1084 | freebin(&n); |
bebf03ab MW |
1085 | return (rc); |
1086 | } | |
1087 | ||
c90f712e | 1088 | static const PyMethodDef gaeadkey_pymethods[] = { |
bebf03ab | 1089 | #define METHNAME(name) gaekmeth_##name |
91e56f06 | 1090 | NAMETH(aad, "KEY.aad() -> AAD") |
d96c882e MW |
1091 | KWMETH(enc, "KEY.enc(NONCE, [hsz], [msz], [tsz]) -> ENC") |
1092 | KWMETH(dec, "KEY.dec(NONCE, [hsz], [csz], [tsz]) -> DEC") | |
bebf03ab MW |
1093 | #undef METHNAME |
1094 | { 0 } | |
1095 | }; | |
1096 | ||
c6e89d48 MW |
1097 | static PyObject *gaeadaad_pywrap(PyObject *cobj, gaead_aad *a, |
1098 | unsigned f, size_t hsz) | |
bebf03ab MW |
1099 | { |
1100 | gaeadaad_pyobj *ga; | |
1101 | ||
1102 | assert(cobj); Py_INCREF(cobj); | |
1103 | ga = PyObject_NEW(gaeadaad_pyobj, (PyTypeObject *)cobj); | |
1104 | ga->a = a; ga->f = f; ga->hsz = hsz; ga->hlen = 0; | |
1105 | return ((PyObject *)ga); | |
1106 | } | |
1107 | ||
1108 | static void gaeadaad_pydealloc(PyObject *me) | |
1109 | { | |
1110 | gaeadaad_pyobj *ga = (gaeadaad_pyobj *)me; | |
1111 | ||
1112 | if (ga->a) GAEAD_DESTROY(ga->a); | |
2a448f86 | 1113 | Py_DECREF(Py_TYPE(me)); FREEOBJ(me); |
bebf03ab MW |
1114 | } |
1115 | ||
1116 | static int gaea_check(PyObject *me) | |
1117 | { | |
1118 | gaeadaad_pyobj *ga = (gaeadaad_pyobj *)me; | |
1119 | int rc = -1; | |
1120 | ||
1121 | if ((ga->f&AEADF_DEAD) || !ga->a) VALERR("aad object no longer active"); | |
1122 | rc = 0; | |
1123 | end: | |
1124 | return (rc); | |
1125 | } | |
1126 | ||
1127 | static void gaea_invalidate(gaeadaad_pyobj *ga) | |
1128 | { if (ga) ga->f |= AEADF_DEAD; } | |
1129 | ||
1130 | static void gaea_sever(gaeadaad_pyobj **ga_inout) | |
1131 | { | |
1132 | gaeadaad_pyobj *ga = *ga_inout; | |
1133 | if (ga) { ga->f |= AEADF_DEAD; ga->a = 0; Py_DECREF(ga); *ga_inout = 0; } | |
1134 | } | |
1135 | ||
1136 | static PyObject *gaeaget_hsz(PyObject *me, void *hunoz) | |
1137 | { | |
1138 | if (gaea_check(me)) return (0); | |
1139 | else if (GAEADAAD_F(me)&AEADF_PCHSZ) return getulong(GAEADAAD_HSZ(me)); | |
1140 | else RETURN_NONE; | |
1141 | } | |
1142 | ||
1143 | static PyObject *gaeaget_hlen(PyObject *me, void *hunoz) | |
1144 | { return (gaea_check(me) ? 0 : getulong(GAEADAAD_HLEN(me))); } | |
1145 | ||
c90f712e | 1146 | static const PyGetSetDef gaeadaad_pygetset[] = { |
bebf03ab | 1147 | #define GETSETNAME(op, name) gaea##op##_##name |
d96c882e MW |
1148 | GET (hsz, "AAD.hsz -> precommitted header length or `None'") |
1149 | GET (hlen, "AAD.hlen -> header length so far") | |
bebf03ab MW |
1150 | #undef GETSETNAME |
1151 | { 0 } | |
1152 | }; | |
1153 | ||
91e56f06 | 1154 | static PyObject *gaeameth_copy(PyObject *me) |
bebf03ab MW |
1155 | { |
1156 | PyObject *rc = 0; | |
1157 | ||
bebf03ab MW |
1158 | if (gaea_check(me)) goto end; |
1159 | if (GAEADAAD_F(me)&AEADF_AADNDEP) | |
1160 | VALERR("can't duplicate nonce-dependent aad"); | |
2a448f86 | 1161 | rc = gaeadaad_pywrap((PyObject *)Py_TYPE(me), |
bebf03ab | 1162 | GAEAD_DUP(GAEADAAD_A(me)), 0, 0); |
de7abe02 | 1163 | GAEADAAD_HLEN(rc) = GAEADAAD_HLEN(me); |
bebf03ab MW |
1164 | end: |
1165 | return (rc); | |
1166 | } | |
1167 | ||
1168 | static int gaeadaad_hash(PyObject *me, const void *h, size_t hsz) | |
1169 | { | |
1170 | gaeadaad_pyobj *ga = (gaeadaad_pyobj *)me; | |
1171 | int rc = -1; | |
1172 | ||
1173 | if (gaea_check(me)) goto end; | |
1174 | if ((ga->f&AEADF_NOAAD) && hsz) | |
1175 | VALERR("header data not permitted"); | |
1176 | if ((ga->f&AEADF_PCHSZ) && hsz > ga->hsz - ga->hlen) | |
1177 | VALERR("too large for precommitted header length"); | |
1178 | GAEAD_HASH(ga->a, h, hsz); ga->hlen += hsz; | |
1179 | rc = 0; | |
1180 | end: | |
1181 | return (rc); | |
1182 | } | |
1183 | ||
bebf03ab MW |
1184 | static PyObject *gaeameth_hash(PyObject *me, PyObject *arg) |
1185 | { | |
21684137 MW |
1186 | struct bin h = BIN_INIT; |
1187 | PyObject *rc = 0; | |
bebf03ab | 1188 | |
21684137 MW |
1189 | if (!PyArg_ParseTuple(arg, "O&:hash", convbin, &h)) goto end; |
1190 | if (gaeadaad_hash(me, h.p, h.sz)) goto end; | |
1191 | rc = me; Py_INCREF(rc); | |
1192 | end: | |
1193 | freebin(&h); | |
1194 | return (rc); | |
bebf03ab MW |
1195 | } |
1196 | ||
1197 | #define GAEAMETH_HASHU_(n, W, w) \ | |
1198 | static PyObject *gaeameth_hashu##w(PyObject *me, PyObject *arg) \ | |
1199 | { \ | |
1200 | uint##n x; octet b[SZ_##W]; \ | |
21684137 | 1201 | \ |
bebf03ab MW |
1202 | if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \ |
1203 | STORE##W(b, x); if (gaeadaad_hash(me, b, sizeof(b))) return (0); \ | |
1204 | RETURN_ME; \ | |
1205 | } | |
1206 | DOUINTCONV(GAEAMETH_HASHU_) | |
1207 | ||
1208 | #define GAEAMETH_HASHBUF_(n, W, w) \ | |
1209 | static PyObject *gaeameth_hashbuf##w(PyObject *me, PyObject *arg) \ | |
1210 | { \ | |
21684137 MW |
1211 | struct bin in = BIN_INIT; octet b[SZ_##W]; \ |
1212 | PyObject *rc = 0; \ | |
1213 | \ | |
67c75893 | 1214 | if (!PyArg_ParseTuple(arg, "O&:hashbuf" #w, convbin, &in)) goto end; \ |
e01ab3c9 | 1215 | if (in.sz > MASK##n) VALERR("too large"); \ |
67c75893 MW |
1216 | STORE##W(b, in.sz); if (gaeadaad_hash(me, b, sizeof(b))) goto end; \ |
1217 | if (gaeadaad_hash(me, in.p, in.sz)) goto end; \ | |
21684137 | 1218 | rc = me; Py_INCREF(rc); \ |
bebf03ab | 1219 | end: \ |
21684137 MW |
1220 | freebin(&in); \ |
1221 | return (rc); \ | |
bebf03ab MW |
1222 | } |
1223 | DOUINTCONV(GAEAMETH_HASHBUF_) | |
1224 | ||
1225 | static PyObject *gaeameth_hashstrz(PyObject *me, PyObject *arg) | |
1226 | { | |
1227 | char *p; | |
1228 | if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0); | |
1229 | if (gaeadaad_hash(me, p, strlen(p) + 1)) return (0); | |
1230 | RETURN_ME; | |
1231 | } | |
1232 | ||
c90f712e | 1233 | static const PyMethodDef gaeadaad_pymethods[] = { |
bebf03ab | 1234 | #define METHNAME(name) gaeameth_##name |
91e56f06 | 1235 | NAMETH(copy, "AAD.copy() -> AAD'") |
d96c882e | 1236 | METH (hash, "AAD.hash(H)") |
bebf03ab MW |
1237 | #define METHU_(n, W, w) METH(hashu##w, "AAD.hashu" #w "(WORD)") |
1238 | DOUINTCONV(METHU_) | |
1239 | #undef METHU_ | |
1240 | #define METHBUF_(n, W, w) METH(hashbuf##w, "AAD.hashbuf" #w "(BYTES)") | |
1241 | DOUINTCONV(METHBUF_) | |
1242 | #undef METHBUF_ | |
d96c882e | 1243 | METH (hashstrz, "AAD.hashstrz(STRING)") |
bebf03ab MW |
1244 | #undef METHNAME |
1245 | { 0 } | |
1246 | }; | |
1247 | ||
c6e89d48 MW |
1248 | static PyObject *gaeadenc_pywrap(PyObject *cobj, gaead_enc *e, unsigned f, |
1249 | size_t hsz, size_t msz, size_t tsz) | |
bebf03ab MW |
1250 | { |
1251 | gaeadenc_pyobj *ge; | |
1252 | ||
1253 | assert(cobj); Py_INCREF(cobj); | |
1254 | ge = PyObject_NEW(gaeadenc_pyobj, (PyTypeObject *)cobj); | |
1255 | ge->e = e; ge->f = f; ge->hsz = hsz; ge->msz = msz; ge->tsz = tsz; | |
1256 | ge->aad = 0; ge->mlen = 0; | |
1257 | return ((PyObject *)ge); | |
1258 | } | |
1259 | ||
1260 | static void gaeadenc_pydealloc(PyObject *me) | |
1261 | { | |
1262 | gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me; | |
1263 | ||
1264 | gaea_sever(&ge->aad); GAEAD_DESTROY(ge->e); | |
2a448f86 | 1265 | Py_DECREF(Py_TYPE(me)); FREEOBJ(me); |
bebf03ab MW |
1266 | } |
1267 | ||
1268 | static PyObject *gaeeget_hsz(PyObject *me, void *hunoz) | |
1269 | { | |
1270 | if (GAEADENC_F(me)&AEADF_PCHSZ) return getulong(GAEADENC_HSZ(me)); | |
1271 | else RETURN_NONE; | |
1272 | } | |
1273 | ||
1274 | static PyObject *gaeeget_msz(PyObject *me, void *hunoz) | |
1275 | { | |
1276 | if (GAEADENC_F(me)&AEADF_PCMSZ) return getulong(GAEADENC_MSZ(me)); | |
1277 | else RETURN_NONE; | |
1278 | } | |
1279 | ||
1280 | static PyObject *gaeeget_tsz(PyObject *me, void *hunoz) | |
1281 | { | |
1282 | if (GAEADENC_F(me)&AEADF_PCTSZ) return getulong(GAEADENC_TSZ(me)); | |
1283 | else RETURN_NONE; | |
1284 | } | |
1285 | ||
1286 | static PyObject *gaeeget_mlen(PyObject *me, void *hunoz) | |
1287 | { return getulong(GAEADENC_MLEN(me)); } | |
1288 | ||
c90f712e | 1289 | static const PyGetSetDef gaeadenc_pygetset[] = { |
bebf03ab | 1290 | #define GETSETNAME(op, name) gaee##op##_##name |
d96c882e MW |
1291 | GET (hsz, "ENC.hsz -> precommitted header length or `None'") |
1292 | GET (msz, "ENC.msz -> precommitted message length or `None'") | |
1293 | GET (tsz, "ENC.tsz -> precommitted tag length or `None'") | |
1294 | GET (mlen, "ENC.mlen -> message length so far") | |
bebf03ab MW |
1295 | #undef GETSETNAME |
1296 | { 0 } | |
1297 | }; | |
1298 | ||
91e56f06 | 1299 | static PyObject *gaeemeth_aad(PyObject *me) |
bebf03ab MW |
1300 | { |
1301 | gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me; | |
1302 | PyObject *rc = 0; | |
1303 | ||
bebf03ab | 1304 | if (!(ge->f&AEADF_AADNDEP)) |
2a448f86 | 1305 | rc = gaeadaad_pywrap((PyObject *)GCAEADENC_KEY(Py_TYPE(ge))->aad, |
bebf03ab MW |
1306 | GAEAD_AAD(ge->e), 0, 0); |
1307 | else { | |
1308 | if ((ge->f&AEADF_AADFIRST) && ge->mlen) | |
1309 | VALERR("too late for aad"); | |
1310 | if (!ge->aad) | |
1311 | ge->aad = (gaeadaad_pyobj *) | |
2a448f86 | 1312 | gaeadaad_pywrap((PyObject *)GCAEADENC_KEY(Py_TYPE(ge))->aad, |
f77c6b68 MW |
1313 | GAEAD_AAD(ge->e), ge->f&(AEADF_PCHSZ | AEADF_NOAAD), |
1314 | ge->hsz); | |
bebf03ab MW |
1315 | Py_INCREF(ge->aad); |
1316 | rc = (PyObject *)ge->aad; | |
1317 | } | |
1318 | end: | |
1319 | return (rc); | |
1320 | } | |
1321 | ||
1322 | static PyObject *gaeemeth_reinit(PyObject *me, PyObject *arg, PyObject *kw) | |
1323 | { | |
1324 | static const char *const kwlist[] = { "nonce", "hsz", "msz", "tsz", 0 }; | |
1325 | gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me; | |
21684137 | 1326 | struct bin n = BIN_INIT; |
bebf03ab MW |
1327 | PyObject *hszobj = Py_None, *mszobj = Py_None, *tszobj = Py_None; |
1328 | size_t hsz = 0, msz = 0, tsz = 0; | |
21684137 | 1329 | PyObject *rc = 0; |
bebf03ab MW |
1330 | unsigned f; |
1331 | ||
67c75893 MW |
1332 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|OOO:enc", KWLIST, |
1333 | convbin, &n, &hszobj, &mszobj, &tszobj)) | |
bebf03ab | 1334 | goto end; |
67c75893 | 1335 | if (check_aead_encdec(ge->e->ops->c, &f, n.sz, |
bebf03ab MW |
1336 | hszobj, &hsz, mszobj, &msz, tszobj, &tsz)) |
1337 | goto end; | |
67c75893 | 1338 | if (GAEAD_REINIT(ge->e, n.p, n.sz, hsz, msz, tsz)) |
bebf03ab MW |
1339 | VALERR("bad aead parameter combination"); |
1340 | gaea_sever(&ge->aad); | |
1341 | ge->f = f; ge->hsz = hsz; ge->msz = msz; ge->tsz = tsz; | |
21684137 | 1342 | rc = me; Py_INCREF(rc); |
bebf03ab | 1343 | end: |
21684137 MW |
1344 | freebin(&n); |
1345 | return (rc); | |
bebf03ab MW |
1346 | } |
1347 | ||
1348 | static PyObject *gaeemeth_encrypt(PyObject *me, PyObject *arg) | |
1349 | { | |
1350 | gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me; | |
21684137 | 1351 | struct bin m = BIN_INIT; |
bebf03ab MW |
1352 | char *c = 0; size_t csz; buf b; |
1353 | int err; | |
1354 | PyObject *rc = 0; | |
1355 | ||
67c75893 | 1356 | if (!PyArg_ParseTuple(arg, "O&:encrypt", convbin, &m)) goto end; |
bebf03ab MW |
1357 | if (ge->f&AEADF_AADFIRST) { |
1358 | if ((ge->f&AEADF_PCHSZ) && (ge->aad ? ge->aad->hlen : 0) != ge->hsz) | |
1359 | VALERR("header doesn't match precommitted length"); | |
1360 | gaea_invalidate(ge->aad); | |
1361 | } | |
67c75893 | 1362 | if ((ge->f&AEADF_PCMSZ) && m.sz > ge->msz - ge->mlen) |
bebf03ab | 1363 | VALERR("too large for precommitted message length"); |
67c75893 MW |
1364 | csz = m.sz + ge->e->ops->c->bufsz; c = xmalloc(csz); buf_init(&b, c, csz); |
1365 | err = GAEAD_ENCRYPT(ge->e, m.p, m.sz, &b); assert(!err); (void)err; | |
1366 | buf_flip(&b); rc = bytestring_pywrapbuf(&b); ge->mlen += m.sz; | |
bebf03ab | 1367 | end: |
21684137 | 1368 | freebin(&m); xfree(c); |
bebf03ab MW |
1369 | return (rc); |
1370 | } | |
1371 | ||
1372 | static PyObject *gaeemeth_done(PyObject *me, PyObject *arg, PyObject *kw) | |
1373 | { | |
1374 | static const char *const kwlist[] = { "tsz", "aad", 0 }; | |
1375 | gaeadenc_pyobj *ge = (gaeadenc_pyobj *)me; | |
1376 | PyObject *aad = Py_None; | |
1377 | char *c = 0; size_t csz; buf b; | |
1378 | PyObject *tszobj = Py_None; PyObject *tag; size_t tsz; | |
1379 | int err; | |
1380 | PyObject *rc = 0; | |
1381 | ||
1382 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|OO:done", KWLIST, | |
1383 | &tszobj, &aad)) | |
1384 | goto end; | |
1385 | if (tszobj != Py_None && !convszt(tszobj, &tsz)) goto end; | |
1386 | if (aad != Py_None && | |
1387 | !PyObject_TypeCheck(aad, | |
2a448f86 | 1388 | (PyTypeObject *)GCAEADENC_KEY(Py_TYPE(me))->aad)) |
bebf03ab MW |
1389 | TYERR("wanted aad"); |
1390 | if ((ge->f&AEADF_AADNDEP) && aad != Py_None && aad != (PyObject *)ge->aad) | |
1391 | VALERR("mismatched aad"); | |
1392 | if ((ge->f&AEADF_PCHSZ) && | |
1393 | (aad == Py_None ? 0 : GAEADAAD_HLEN(aad)) != ge->hsz) | |
1394 | VALERR("header doesn't match precommitted length"); | |
1395 | if ((ge->f&AEADF_PCMSZ) && ge->mlen != ge->msz) | |
1396 | VALERR("message doesn't match precommitted length"); | |
1397 | if (tszobj == Py_None) { | |
1398 | if (ge->f&AEADF_PCTSZ) tsz = ge->tsz; | |
1399 | else tsz = keysz(0, ge->e->ops->c->tagsz); | |
1400 | } else { | |
1401 | if ((ge->f&AEADF_PCTSZ) && tsz != ge->tsz) | |
1402 | VALERR("tag length doesn't match precommitted value"); | |
1403 | if (keysz(tsz, ge->e->ops->c->tagsz) != tsz) VALERR("bad tag length"); | |
1404 | } | |
1405 | csz = ge->e->ops->c->bufsz; c = xmalloc(csz); buf_init(&b, c, csz); | |
1406 | tag = bytestring_pywrap(0, tsz); | |
1407 | err = GAEAD_DONE(ge->e, aad == Py_None ? 0 : GAEADAAD_A(aad), &b, | |
2135a6d3 | 1408 | BIN_PTR(tag), tsz); |
bebf03ab MW |
1409 | assert(!err); (void)err; |
1410 | buf_flip(&b); rc = Py_BuildValue("NN", bytestring_pywrapbuf(&b), tag); | |
1411 | end: | |
1412 | xfree(c); | |
1413 | return (rc); | |
1414 | } | |
1415 | ||
c90f712e | 1416 | static const PyMethodDef gaeadenc_pymethods[] = { |
bebf03ab | 1417 | #define METHNAME(name) gaeemeth_##name |
91e56f06 | 1418 | NAMETH(aad, "ENC.aad() -> AAD") |
d96c882e MW |
1419 | KWMETH(reinit, "ENC.reinit(NONCE, [hsz], [msz], [tsz])") |
1420 | METH (encrypt, "ENC.encrypt(MSG) -> CT") | |
1421 | KWMETH(done, "ENC.done([tsz], [aad]) -> CT, TAG") | |
bebf03ab MW |
1422 | #undef METHNAME |
1423 | { 0 } | |
1424 | }; | |
1425 | ||
c6e89d48 MW |
1426 | static PyObject *gaeaddec_pywrap(PyObject *cobj, gaead_dec *d, unsigned f, |
1427 | size_t hsz, size_t csz, size_t tsz) | |
bebf03ab MW |
1428 | { |
1429 | gaeaddec_pyobj *gd; | |
1430 | assert(cobj); Py_INCREF(cobj); | |
1431 | gd = PyObject_NEW(gaeaddec_pyobj, (PyTypeObject *)cobj); | |
1432 | gd->d = d; gd->f = f; gd->hsz = hsz; gd->csz = csz; gd->tsz = tsz; | |
1433 | gd->aad = 0; gd->clen = 0; | |
1434 | return ((PyObject *)gd); | |
1435 | } | |
1436 | ||
1437 | static void gaeaddec_pydealloc(PyObject *me) | |
1438 | { | |
1439 | gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me; | |
1440 | ||
1441 | gaea_sever(&gd->aad); GAEAD_DESTROY(GAEADDEC_D(me)); | |
2a448f86 | 1442 | Py_DECREF(Py_TYPE(me)); FREEOBJ(me); |
bebf03ab MW |
1443 | } |
1444 | ||
1445 | static PyObject *gaedget_hsz(PyObject *me, void *hunoz) | |
1446 | { | |
1447 | if (GAEADDEC_F(me)&AEADF_PCHSZ) return getulong(GAEADDEC_HSZ(me)); | |
1448 | else RETURN_NONE; | |
1449 | } | |
1450 | ||
1451 | static PyObject *gaedget_csz(PyObject *me, void *hunoz) | |
1452 | { | |
1453 | if (GAEADDEC_F(me)&AEADF_PCMSZ) return getulong(GAEADDEC_CSZ(me)); | |
1454 | else RETURN_NONE; | |
1455 | } | |
1456 | ||
1457 | static PyObject *gaedget_tsz(PyObject *me, void *hunoz) | |
1458 | { | |
1459 | if (GAEADDEC_F(me)&AEADF_PCTSZ) return getulong(GAEADDEC_TSZ(me)); | |
1460 | else RETURN_NONE; | |
1461 | } | |
1462 | ||
1463 | static PyObject *gaedget_clen(PyObject *me, void *hunoz) | |
1464 | { return getulong(GAEADDEC_CLEN(me)); } | |
1465 | ||
c90f712e | 1466 | static const PyGetSetDef gaeaddec_pygetset[] = { |
bebf03ab | 1467 | #define GETSETNAME(op, name) gaed##op##_##name |
d96c882e MW |
1468 | GET (hsz, "DEC.hsz -> precommitted header length or `None'") |
1469 | GET (csz, "DEC.csz -> precommitted ciphertext length or `None'") | |
1470 | GET (tsz, "DEC.tsz -> precommitted tag length or `None'") | |
1471 | GET (clen, "DEC.clen -> ciphertext length so far") | |
bebf03ab MW |
1472 | #undef GETSETNAME |
1473 | { 0 } | |
1474 | }; | |
1475 | ||
91e56f06 | 1476 | static PyObject *gaedmeth_aad(PyObject *me) |
bebf03ab MW |
1477 | { |
1478 | gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me; | |
1479 | ||
bebf03ab | 1480 | if (!(gd->f&AEADF_AADNDEP)) |
2a448f86 | 1481 | return (gaeadaad_pywrap((PyObject *)GCAEADDEC_KEY(Py_TYPE(gd))->aad, |
bebf03ab MW |
1482 | GAEAD_AAD(gd->d), 0, 0)); |
1483 | else { | |
1484 | if (!gd->aad) | |
1485 | gd->aad = (gaeadaad_pyobj *) | |
2a448f86 | 1486 | gaeadaad_pywrap((PyObject *)GCAEADENC_KEY(Py_TYPE(gd))->aad, |
f77c6b68 MW |
1487 | GAEAD_AAD(gd->d), gd->f&(AEADF_PCHSZ | AEADF_NOAAD), |
1488 | gd->hsz); | |
bebf03ab MW |
1489 | Py_INCREF(gd->aad); |
1490 | return ((PyObject *)gd->aad); | |
1491 | } | |
1492 | } | |
1493 | ||
1494 | static PyObject *gaedmeth_reinit(PyObject *me, PyObject *arg, PyObject *kw) | |
1495 | { | |
1496 | static const char *const kwlist[] = { "nonce", "hsz", "csz", "tsz", 0 }; | |
1497 | gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me; | |
21684137 | 1498 | struct bin n = BIN_INIT; |
bebf03ab MW |
1499 | PyObject *hszobj = Py_None, *cszobj = Py_None, *tszobj = Py_None; |
1500 | size_t hsz = 0, csz = 0, tsz = 0; | |
21684137 | 1501 | PyObject *rc = 0; |
bebf03ab MW |
1502 | unsigned f; |
1503 | ||
67c75893 MW |
1504 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|OOO:enc", KWLIST, |
1505 | convbin, &n, &hszobj, &cszobj, &tszobj)) | |
bebf03ab | 1506 | goto end; |
67c75893 | 1507 | if (check_aead_encdec(gd->d->ops->c, &f, n.sz, |
bebf03ab MW |
1508 | hszobj, &hsz, cszobj, &csz, tszobj, &tsz)) |
1509 | goto end; | |
67c75893 | 1510 | if (GAEAD_REINIT(gd->d, n.p, n.sz, hsz, csz, tsz)) |
bebf03ab MW |
1511 | VALERR("bad aead parameter combination"); |
1512 | gaea_sever(&gd->aad); | |
1513 | gd->f = f; gd->hsz = hsz; gd->csz = csz; gd->tsz = tsz; | |
21684137 | 1514 | rc = me; Py_INCREF(rc); |
bebf03ab | 1515 | end: |
21684137 | 1516 | freebin(&n); |
bebf03ab MW |
1517 | return (0); |
1518 | } | |
1519 | ||
1520 | static PyObject *gaedmeth_decrypt(PyObject *me, PyObject *arg) | |
1521 | { | |
1522 | gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me; | |
21684137 | 1523 | struct bin c = BIN_INIT; |
bebf03ab MW |
1524 | char *m = 0; size_t msz; buf b; |
1525 | int err; | |
1526 | PyObject *rc = 0; | |
1527 | ||
67c75893 | 1528 | if (!PyArg_ParseTuple(arg, "O&:decrypt", convbin, &c)) goto end; |
bebf03ab MW |
1529 | if (gd->f&AEADF_AADFIRST) { |
1530 | if ((gd->f&AEADF_PCHSZ) && (gd->aad ? gd->aad->hlen : 0) != gd->hsz) | |
1531 | VALERR("header doesn't match precommitted length"); | |
1532 | gaea_invalidate(gd->aad); | |
1533 | } | |
67c75893 | 1534 | if ((gd->f&AEADF_PCMSZ) && c.sz > gd->csz - gd->clen) |
bebf03ab | 1535 | VALERR("too large for precommitted message length"); |
67c75893 MW |
1536 | msz = c.sz + gd->d->ops->c->bufsz; m = xmalloc(msz); buf_init(&b, m, msz); |
1537 | err = GAEAD_DECRYPT(gd->d, c.p, c.sz, &b); assert(!err); (void)err; | |
1538 | buf_flip(&b); rc = bytestring_pywrapbuf(&b); gd->clen += c.sz; | |
bebf03ab | 1539 | end: |
21684137 | 1540 | freebin(&c); xfree(m); |
bebf03ab MW |
1541 | return (rc); |
1542 | } | |
1543 | ||
1544 | static PyObject *gaedmeth_done(PyObject *me, PyObject *arg, PyObject *kw) | |
1545 | { | |
1546 | static const char *const kwlist[] = { "tag", "aad", 0 }; | |
1547 | gaeaddec_pyobj *gd = (gaeaddec_pyobj *)me; | |
1548 | PyObject *aad = Py_None; | |
21684137 | 1549 | struct bin t = BIN_INIT; |
bebf03ab MW |
1550 | char *m = 0; size_t msz; buf b; |
1551 | int err; | |
1552 | PyObject *rc = 0; | |
1553 | ||
67c75893 MW |
1554 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O:done", KWLIST, |
1555 | convbin, &t, &aad)) | |
bebf03ab MW |
1556 | goto end; |
1557 | if (aad != Py_None && | |
1558 | !PyObject_TypeCheck(aad, | |
2a448f86 | 1559 | (PyTypeObject *)GCAEADENC_KEY(Py_TYPE(me))->aad)) |
bebf03ab MW |
1560 | TYERR("wanted aad"); |
1561 | if ((gd->f&AEADF_AADNDEP) && aad != Py_None && aad != (PyObject *)gd->aad) | |
1562 | VALERR("mismatched aad"); | |
1563 | if ((gd->f&AEADF_PCHSZ) && | |
1564 | (aad == Py_None ? 0 : GAEADAAD_HLEN(aad)) != gd->hsz) | |
1565 | VALERR("header doesn't match precommitted length"); | |
1566 | if ((gd->f&AEADF_PCMSZ) && gd->clen != gd->csz) | |
1567 | VALERR("message doesn't match precommitted length"); | |
67c75893 | 1568 | if ((gd->f&AEADF_PCTSZ) && t.sz != gd->tsz) |
bebf03ab | 1569 | VALERR("tag length doesn't match precommitted value"); |
67c75893 | 1570 | if (keysz(t.sz, gd->d->ops->c->tagsz) != t.sz) VALERR("bad tag length"); |
bebf03ab | 1571 | msz = gd->d->ops->c->bufsz; m = xmalloc(msz); buf_init(&b, m, msz); |
67c75893 MW |
1572 | err = GAEAD_DONE(gd->d, aad == Py_None ? 0 : GAEADAAD_A(aad), |
1573 | &b, t.p, t.sz); | |
bebf03ab MW |
1574 | assert(err >= 0); |
1575 | if (!err) VALERR("decryption failed"); | |
1576 | buf_flip(&b); rc = bytestring_pywrapbuf(&b); | |
1577 | end: | |
21684137 | 1578 | freebin(&t); xfree(m); |
bebf03ab MW |
1579 | return (rc); |
1580 | } | |
1581 | ||
c90f712e | 1582 | static const PyMethodDef gaeaddec_pymethods[] = { |
bebf03ab | 1583 | #define METHNAME(name) gaedmeth_##name |
91e56f06 | 1584 | NAMETH(aad, "DEC.aad() -> AAD") |
d96c882e MW |
1585 | KWMETH(reinit, "DEC.reinit(NONCE, [hsz], [csz], [tsz])") |
1586 | METH (decrypt, "DEC.decrypt(CT) -> MSG") | |
1587 | KWMETH(done, "DEC.done(TAG, [aad]) -> MSG | None") | |
bebf03ab MW |
1588 | #undef METHNAME |
1589 | { 0 } | |
1590 | }; | |
1591 | ||
c263b05c | 1592 | static const PyTypeObject gcaead_pytype_skel = { |
591bf41b | 1593 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
bebf03ab MW |
1594 | "GCAEAD", /* @tp_name@ */ |
1595 | sizeof(gcaead_pyobj), /* @tp_basicsize@ */ | |
1596 | 0, /* @tp_itemsize@ */ | |
1597 | ||
1598 | 0, /* @tp_dealloc@ */ | |
1599 | 0, /* @tp_print@ */ | |
1600 | 0, /* @tp_getattr@ */ | |
1601 | 0, /* @tp_setattr@ */ | |
1602 | 0, /* @tp_compare@ */ | |
1603 | 0, /* @tp_repr@ */ | |
1604 | 0, /* @tp_as_number@ */ | |
1605 | 0, /* @tp_as_sequence@ */ | |
1606 | 0, /* @tp_as_mapping@ */ | |
1607 | 0, /* @tp_hash@ */ | |
1608 | 0, /* @tp_call@ */ | |
1609 | 0, /* @tp_str@ */ | |
1610 | 0, /* @tp_getattro@ */ | |
1611 | 0, /* @tp_setattro@ */ | |
1612 | 0, /* @tp_as_buffer@ */ | |
1613 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1614 | Py_TPFLAGS_BASETYPE, | |
1615 | ||
1616 | /* @tp_doc@ */ | |
d96c882e | 1617 | "Authenticated encryption (key) metaclass.", |
bebf03ab MW |
1618 | |
1619 | 0, /* @tp_traverse@ */ | |
1620 | 0, /* @tp_clear@ */ | |
1621 | 0, /* @tp_richcompare@ */ | |
1622 | 0, /* @tp_weaklistoffset@ */ | |
1623 | 0, /* @tp_iter@ */ | |
1624 | 0, /* @tp_iternext@ */ | |
1625 | 0, /* @tp_methods@ */ | |
1626 | 0, /* @tp_members@ */ | |
c90f712e | 1627 | PYGETSET(gcaead), /* @tp_getset@ */ |
bebf03ab MW |
1628 | 0, /* @tp_base@ */ |
1629 | 0, /* @tp_dict@ */ | |
1630 | 0, /* @tp_descr_get@ */ | |
1631 | 0, /* @tp_descr_set@ */ | |
1632 | 0, /* @tp_dictoffset@ */ | |
1633 | 0, /* @tp_init@ */ | |
1634 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1635 | abstract_pynew, /* @tp_new@ */ | |
1636 | 0, /* @tp_free@ */ | |
1637 | 0 /* @tp_is_gc@ */ | |
1638 | }; | |
1639 | ||
c263b05c | 1640 | static const PyTypeObject gaeadkey_pytype_skel = { |
591bf41b | 1641 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
bebf03ab MW |
1642 | "GAEKey", /* @tp_name@ */ |
1643 | sizeof(gaeadkey_pyobj), /* @tp_basicsize@ */ | |
1644 | 0, /* @tp_itemsize@ */ | |
1645 | ||
1646 | gaeadkey_pydealloc, /* @tp_dealloc@ */ | |
1647 | 0, /* @tp_print@ */ | |
1648 | 0, /* @tp_getattr@ */ | |
1649 | 0, /* @tp_setattr@ */ | |
1650 | 0, /* @tp_compare@ */ | |
1651 | 0, /* @tp_repr@ */ | |
1652 | 0, /* @tp_as_number@ */ | |
1653 | 0, /* @tp_as_sequence@ */ | |
1654 | 0, /* @tp_as_mapping@ */ | |
1655 | 0, /* @tp_hash@ */ | |
1656 | 0, /* @tp_call@ */ | |
1657 | 0, /* @tp_str@ */ | |
1658 | 0, /* @tp_getattro@ */ | |
1659 | 0, /* @tp_setattro@ */ | |
1660 | 0, /* @tp_as_buffer@ */ | |
1661 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1662 | Py_TPFLAGS_BASETYPE, | |
1663 | ||
1664 | /* @tp_doc@ */ | |
d96c882e | 1665 | "Authenticated encryption key.", |
bebf03ab MW |
1666 | |
1667 | 0, /* @tp_traverse@ */ | |
1668 | 0, /* @tp_clear@ */ | |
1669 | 0, /* @tp_richcompare@ */ | |
1670 | 0, /* @tp_weaklistoffset@ */ | |
1671 | 0, /* @tp_iter@ */ | |
1672 | 0, /* @tp_iternext@ */ | |
c90f712e | 1673 | PYMETHODS(gaeadkey), /* @tp_methods@ */ |
bebf03ab MW |
1674 | 0, /* @tp_members@ */ |
1675 | 0, /* @tp_getset@ */ | |
1676 | 0, /* @tp_base@ */ | |
1677 | 0, /* @tp_dict@ */ | |
1678 | 0, /* @tp_descr_get@ */ | |
1679 | 0, /* @tp_descr_set@ */ | |
1680 | 0, /* @tp_dictoffset@ */ | |
1681 | 0, /* @tp_init@ */ | |
1682 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1683 | abstract_pynew, /* @tp_new@ */ | |
1684 | 0, /* @tp_free@ */ | |
1685 | 0 /* @tp_is_gc@ */ | |
1686 | }; | |
1687 | ||
c263b05c | 1688 | static const PyTypeObject gcaeadaad_pytype_skel = { |
591bf41b | 1689 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
bebf03ab MW |
1690 | "GAEAADClass", /* @tp_name@ */ |
1691 | sizeof(gcaeadaad_pyobj), /* @tp_basicsize@ */ | |
1692 | 0, /* @tp_itemsize@ */ | |
1693 | ||
1694 | 0, /* @tp_dealloc@ */ | |
1695 | 0, /* @tp_print@ */ | |
1696 | 0, /* @tp_getattr@ */ | |
1697 | 0, /* @tp_setattr@ */ | |
1698 | 0, /* @tp_compare@ */ | |
1699 | 0, /* @tp_repr@ */ | |
1700 | 0, /* @tp_as_number@ */ | |
1701 | 0, /* @tp_as_sequence@ */ | |
1702 | 0, /* @tp_as_mapping@ */ | |
1703 | 0, /* @tp_hash@ */ | |
1704 | 0, /* @tp_call@ */ | |
1705 | 0, /* @tp_str@ */ | |
1706 | 0, /* @tp_getattro@ */ | |
1707 | 0, /* @tp_setattro@ */ | |
1708 | 0, /* @tp_as_buffer@ */ | |
1709 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1710 | Py_TPFLAGS_BASETYPE, | |
1711 | ||
1712 | /* @tp_doc@ */ | |
d96c882e | 1713 | "Authenticated encryption additional-data hash metaclass.", |
bebf03ab MW |
1714 | |
1715 | 0, /* @tp_traverse@ */ | |
1716 | 0, /* @tp_clear@ */ | |
1717 | 0, /* @tp_richcompare@ */ | |
1718 | 0, /* @tp_weaklistoffset@ */ | |
1719 | 0, /* @tp_iter@ */ | |
1720 | 0, /* @tp_iternext@ */ | |
1721 | 0, /* @tp_methods@ */ | |
1722 | 0, /* @tp_members@ */ | |
1723 | 0, /* @tp_getset@ */ | |
1724 | 0, /* @tp_base@ */ | |
1725 | 0, /* @tp_dict@ */ | |
1726 | 0, /* @tp_descr_get@ */ | |
1727 | 0, /* @tp_descr_set@ */ | |
1728 | 0, /* @tp_dictoffset@ */ | |
1729 | 0, /* @tp_init@ */ | |
1730 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1731 | abstract_pynew, /* @tp_new@ */ | |
1732 | 0, /* @tp_free@ */ | |
1733 | 0 /* @tp_is_gc@ */ | |
1734 | }; | |
1735 | ||
c263b05c | 1736 | static const PyTypeObject gaeadaad_pytype_skel = { |
591bf41b | 1737 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
bebf03ab MW |
1738 | "GAEAAD", /* @tp_name@ */ |
1739 | sizeof(gaeadaad_pyobj), /* @tp_basicsize@ */ | |
1740 | 0, /* @tp_itemsize@ */ | |
1741 | ||
1742 | gaeadaad_pydealloc, /* @tp_dealloc@ */ | |
1743 | 0, /* @tp_print@ */ | |
1744 | 0, /* @tp_getattr@ */ | |
1745 | 0, /* @tp_setattr@ */ | |
1746 | 0, /* @tp_compare@ */ | |
1747 | 0, /* @tp_repr@ */ | |
1748 | 0, /* @tp_as_number@ */ | |
1749 | 0, /* @tp_as_sequence@ */ | |
1750 | 0, /* @tp_as_mapping@ */ | |
1751 | 0, /* @tp_hash@ */ | |
1752 | 0, /* @tp_call@ */ | |
1753 | 0, /* @tp_str@ */ | |
1754 | 0, /* @tp_getattro@ */ | |
1755 | 0, /* @tp_setattro@ */ | |
1756 | 0, /* @tp_as_buffer@ */ | |
1757 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1758 | Py_TPFLAGS_BASETYPE, | |
1759 | ||
1760 | /* @tp_doc@ */ | |
d96c882e | 1761 | "Authenticated encryption AAD hash.", |
bebf03ab MW |
1762 | |
1763 | 0, /* @tp_traverse@ */ | |
1764 | 0, /* @tp_clear@ */ | |
1765 | 0, /* @tp_richcompare@ */ | |
1766 | 0, /* @tp_weaklistoffset@ */ | |
1767 | 0, /* @tp_iter@ */ | |
1768 | 0, /* @tp_iternext@ */ | |
c90f712e | 1769 | PYMETHODS(gaeadaad), /* @tp_methods@ */ |
bebf03ab | 1770 | 0, /* @tp_members@ */ |
c90f712e | 1771 | PYGETSET(gaeadaad), /* @tp_getset@ */ |
bebf03ab MW |
1772 | 0, /* @tp_base@ */ |
1773 | 0, /* @tp_dict@ */ | |
1774 | 0, /* @tp_descr_get@ */ | |
1775 | 0, /* @tp_descr_set@ */ | |
1776 | 0, /* @tp_dictoffset@ */ | |
1777 | 0, /* @tp_init@ */ | |
1778 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1779 | abstract_pynew, /* @tp_new@ */ | |
1780 | 0, /* @tp_free@ */ | |
1781 | 0 /* @tp_is_gc@ */ | |
1782 | }; | |
1783 | ||
c263b05c | 1784 | static const PyTypeObject gcaeadenc_pytype_skel = { |
591bf41b | 1785 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
bebf03ab MW |
1786 | "GAEEncClass", /* @tp_name@ */ |
1787 | sizeof(gcaeadenc_pyobj), /* @tp_basicsize@ */ | |
1788 | 0, /* @tp_itemsize@ */ | |
1789 | ||
1790 | 0, /* @tp_dealloc@ */ | |
1791 | 0, /* @tp_print@ */ | |
1792 | 0, /* @tp_getattr@ */ | |
1793 | 0, /* @tp_setattr@ */ | |
1794 | 0, /* @tp_compare@ */ | |
1795 | 0, /* @tp_repr@ */ | |
1796 | 0, /* @tp_as_number@ */ | |
1797 | 0, /* @tp_as_sequence@ */ | |
1798 | 0, /* @tp_as_mapping@ */ | |
1799 | 0, /* @tp_hash@ */ | |
1800 | 0, /* @tp_call@ */ | |
1801 | 0, /* @tp_str@ */ | |
1802 | 0, /* @tp_getattro@ */ | |
1803 | 0, /* @tp_setattro@ */ | |
1804 | 0, /* @tp_as_buffer@ */ | |
1805 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1806 | Py_TPFLAGS_BASETYPE, | |
1807 | ||
1808 | /* @tp_doc@ */ | |
d96c882e | 1809 | "Authenticated encryption operation metaclass.", |
bebf03ab MW |
1810 | |
1811 | 0, /* @tp_traverse@ */ | |
1812 | 0, /* @tp_clear@ */ | |
1813 | 0, /* @tp_richcompare@ */ | |
1814 | 0, /* @tp_weaklistoffset@ */ | |
1815 | 0, /* @tp_iter@ */ | |
1816 | 0, /* @tp_iternext@ */ | |
1817 | 0, /* @tp_methods@ */ | |
1818 | 0, /* @tp_members@ */ | |
1819 | 0, /* @tp_getset@ */ | |
1820 | 0, /* @tp_base@ */ | |
1821 | 0, /* @tp_dict@ */ | |
1822 | 0, /* @tp_descr_get@ */ | |
1823 | 0, /* @tp_descr_set@ */ | |
1824 | 0, /* @tp_dictoffset@ */ | |
1825 | 0, /* @tp_init@ */ | |
1826 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1827 | abstract_pynew, /* @tp_new@ */ | |
1828 | 0, /* @tp_free@ */ | |
1829 | 0 /* @tp_is_gc@ */ | |
1830 | }; | |
1831 | ||
c263b05c | 1832 | static const PyTypeObject gaeadenc_pytype_skel = { |
591bf41b | 1833 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
bebf03ab MW |
1834 | "GAEEnc", /* @tp_name@ */ |
1835 | sizeof(gaeadenc_pyobj), /* @tp_basicsize@ */ | |
1836 | 0, /* @tp_itemsize@ */ | |
1837 | ||
1838 | gaeadenc_pydealloc, /* @tp_dealloc@ */ | |
1839 | 0, /* @tp_print@ */ | |
1840 | 0, /* @tp_getattr@ */ | |
1841 | 0, /* @tp_setattr@ */ | |
1842 | 0, /* @tp_compare@ */ | |
1843 | 0, /* @tp_repr@ */ | |
1844 | 0, /* @tp_as_number@ */ | |
1845 | 0, /* @tp_as_sequence@ */ | |
1846 | 0, /* @tp_as_mapping@ */ | |
1847 | 0, /* @tp_hash@ */ | |
1848 | 0, /* @tp_call@ */ | |
1849 | 0, /* @tp_str@ */ | |
1850 | 0, /* @tp_getattro@ */ | |
1851 | 0, /* @tp_setattro@ */ | |
1852 | 0, /* @tp_as_buffer@ */ | |
1853 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1854 | Py_TPFLAGS_BASETYPE, | |
1855 | ||
1856 | /* @tp_doc@ */ | |
d96c882e | 1857 | "Authenticated encryption operation.", |
bebf03ab MW |
1858 | |
1859 | 0, /* @tp_traverse@ */ | |
1860 | 0, /* @tp_clear@ */ | |
1861 | 0, /* @tp_richcompare@ */ | |
1862 | 0, /* @tp_weaklistoffset@ */ | |
1863 | 0, /* @tp_iter@ */ | |
1864 | 0, /* @tp_iternext@ */ | |
c90f712e | 1865 | PYMETHODS(gaeadenc), /* @tp_methods@ */ |
bebf03ab | 1866 | 0, /* @tp_members@ */ |
c90f712e | 1867 | PYGETSET(gaeadenc), /* @tp_getset@ */ |
bebf03ab MW |
1868 | 0, /* @tp_base@ */ |
1869 | 0, /* @tp_dict@ */ | |
1870 | 0, /* @tp_descr_get@ */ | |
1871 | 0, /* @tp_descr_set@ */ | |
1872 | 0, /* @tp_dictoffset@ */ | |
1873 | 0, /* @tp_init@ */ | |
1874 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1875 | abstract_pynew, /* @tp_new@ */ | |
1876 | 0, /* @tp_free@ */ | |
1877 | 0 /* @tp_is_gc@ */ | |
1878 | }; | |
1879 | ||
c263b05c | 1880 | static const PyTypeObject gcaeaddec_pytype_skel = { |
591bf41b | 1881 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
bebf03ab MW |
1882 | "GAEDecClass", /* @tp_name@ */ |
1883 | sizeof(gcaeaddec_pyobj), /* @tp_basicsize@ */ | |
1884 | 0, /* @tp_itemsize@ */ | |
1885 | ||
1886 | 0, /* @tp_dealloc@ */ | |
1887 | 0, /* @tp_print@ */ | |
1888 | 0, /* @tp_getattr@ */ | |
1889 | 0, /* @tp_setattr@ */ | |
1890 | 0, /* @tp_compare@ */ | |
1891 | 0, /* @tp_repr@ */ | |
1892 | 0, /* @tp_as_number@ */ | |
1893 | 0, /* @tp_as_sequence@ */ | |
1894 | 0, /* @tp_as_mapping@ */ | |
1895 | 0, /* @tp_hash@ */ | |
1896 | 0, /* @tp_call@ */ | |
1897 | 0, /* @tp_str@ */ | |
1898 | 0, /* @tp_getattro@ */ | |
1899 | 0, /* @tp_setattro@ */ | |
1900 | 0, /* @tp_as_buffer@ */ | |
1901 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1902 | Py_TPFLAGS_BASETYPE, | |
1903 | ||
1904 | /* @tp_doc@ */ | |
d96c882e | 1905 | "Authenticated decryption operation metaclass.", |
bebf03ab MW |
1906 | |
1907 | 0, /* @tp_traverse@ */ | |
1908 | 0, /* @tp_clear@ */ | |
1909 | 0, /* @tp_richcompare@ */ | |
1910 | 0, /* @tp_weaklistoffset@ */ | |
1911 | 0, /* @tp_iter@ */ | |
1912 | 0, /* @tp_iternext@ */ | |
1913 | 0, /* @tp_methods@ */ | |
1914 | 0, /* @tp_members@ */ | |
1915 | 0, /* @tp_getset@ */ | |
1916 | 0, /* @tp_base@ */ | |
1917 | 0, /* @tp_dict@ */ | |
1918 | 0, /* @tp_descr_get@ */ | |
1919 | 0, /* @tp_descr_set@ */ | |
1920 | 0, /* @tp_dictoffset@ */ | |
1921 | 0, /* @tp_init@ */ | |
1922 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1923 | abstract_pynew, /* @tp_new@ */ | |
1924 | 0, /* @tp_free@ */ | |
1925 | 0 /* @tp_is_gc@ */ | |
1926 | }; | |
1927 | ||
c263b05c | 1928 | static const PyTypeObject gaeaddec_pytype_skel = { |
591bf41b | 1929 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
bebf03ab MW |
1930 | "GAEDec", /* @tp_name@ */ |
1931 | sizeof(gaeaddec_pyobj), /* @tp_basicsize@ */ | |
1932 | 0, /* @tp_itemsize@ */ | |
1933 | ||
1934 | gaeaddec_pydealloc, /* @tp_dealloc@ */ | |
1935 | 0, /* @tp_print@ */ | |
1936 | 0, /* @tp_getattr@ */ | |
1937 | 0, /* @tp_setattr@ */ | |
1938 | 0, /* @tp_compare@ */ | |
1939 | 0, /* @tp_repr@ */ | |
1940 | 0, /* @tp_as_number@ */ | |
1941 | 0, /* @tp_as_sequence@ */ | |
1942 | 0, /* @tp_as_mapping@ */ | |
1943 | 0, /* @tp_hash@ */ | |
1944 | 0, /* @tp_call@ */ | |
1945 | 0, /* @tp_str@ */ | |
1946 | 0, /* @tp_getattro@ */ | |
1947 | 0, /* @tp_setattro@ */ | |
1948 | 0, /* @tp_as_buffer@ */ | |
1949 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
1950 | Py_TPFLAGS_BASETYPE, | |
1951 | ||
1952 | /* @tp_doc@ */ | |
d96c882e | 1953 | "Authenticated decryption operation.", |
bebf03ab MW |
1954 | |
1955 | 0, /* @tp_traverse@ */ | |
1956 | 0, /* @tp_clear@ */ | |
1957 | 0, /* @tp_richcompare@ */ | |
1958 | 0, /* @tp_weaklistoffset@ */ | |
1959 | 0, /* @tp_iter@ */ | |
1960 | 0, /* @tp_iternext@ */ | |
c90f712e | 1961 | PYMETHODS(gaeaddec), /* @tp_methods@ */ |
bebf03ab | 1962 | 0, /* @tp_members@ */ |
c90f712e | 1963 | PYGETSET(gaeaddec), /* @tp_getset@ */ |
bebf03ab MW |
1964 | 0, /* @tp_base@ */ |
1965 | 0, /* @tp_dict@ */ | |
1966 | 0, /* @tp_descr_get@ */ | |
1967 | 0, /* @tp_descr_set@ */ | |
1968 | 0, /* @tp_dictoffset@ */ | |
1969 | 0, /* @tp_init@ */ | |
1970 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
1971 | abstract_pynew, /* @tp_new@ */ | |
1972 | 0, /* @tp_free@ */ | |
1973 | 0 /* @tp_is_gc@ */ | |
1974 | }; | |
1975 | ||
d7ab1bab | 1976 | /*----- Hash functions ----------------------------------------------------*/ |
1977 | ||
c6e89d48 MW |
1978 | PyTypeObject *gchash_pytype; |
1979 | static PyTypeObject *ghash_pytype; | |
1980 | PyObject *sha_pyobj, *has160_pyobj; | |
1981 | ||
1982 | typedef struct ghash_pyobj { | |
1983 | PyObject_HEAD | |
1984 | ghash *h; | |
1985 | } ghash_pyobj; | |
1986 | ||
1987 | #define GHASH_PYCHECK(o) PyObject_TypeCheck((o), ghash_pytype) | |
1988 | #define GHASH_H(o) (((ghash_pyobj *)(o))->h) | |
d7ab1bab | 1989 | |
1990 | CONVFUNC(gchash, gchash *, GCHASH_CH) | |
1991 | CONVFUNC(ghash, ghash *, GHASH_H) | |
1992 | ||
1993 | static PyObject *ghash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
1994 | { | |
827f89d7 MW |
1995 | static const char *const kwlist[] = { 0 }; |
1996 | if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", KWLIST)) | |
d7ab1bab | 1997 | goto end; |
c41d0371 | 1998 | return (ghash_pywrap((PyObject *)ty, GH_INIT(GCHASH_CH(ty)))); |
d7ab1bab | 1999 | end: |
b2687a0a | 2000 | return (0); |
d7ab1bab | 2001 | } |
2002 | ||
c6e89d48 | 2003 | static PyObject *gchash_pywrap(gchash *ch) |
d7ab1bab | 2004 | { |
df9f8366 | 2005 | gchash_pyobj *g = newtype(gchash_pytype, 0, ch->name); |
d7ab1bab | 2006 | g->ch = ch; |
24b3d57b MW |
2007 | g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj); |
2008 | g->ty.ht_type.tp_base = ghash_pytype; | |
d7ab1bab | 2009 | Py_INCREF(ghash_pytype); |
24b3d57b MW |
2010 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
2011 | Py_TPFLAGS_BASETYPE | | |
2012 | Py_TPFLAGS_HEAPTYPE); | |
2013 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; | |
2014 | g->ty.ht_type.tp_free = 0; | |
2015 | g->ty.ht_type.tp_new = ghash_pynew; | |
dc075750 | 2016 | typeready(&g->ty.ht_type); |
d7ab1bab | 2017 | return ((PyObject *)g); |
2018 | } | |
2019 | ||
c41d0371 | 2020 | PyObject *ghash_pywrap(PyObject *cobj, ghash *h) |
d7ab1bab | 2021 | { |
2022 | ghash_pyobj *g; | |
2023 | if (!cobj) cobj = gchash_pywrap((/*unconst*/ gchash *)GH_CLASS(h)); | |
2024 | else Py_INCREF(cobj); | |
2025 | g = PyObject_NEW(ghash_pyobj, (PyTypeObject *)cobj); | |
2026 | g->h = h; | |
b2687a0a | 2027 | return ((PyObject *)g); |
d7ab1bab | 2028 | } |
2029 | ||
2030 | static void ghash_pydealloc(PyObject *me) | |
2031 | { | |
c41d0371 | 2032 | GH_DESTROY(GHASH_H(me)); |
2a448f86 | 2033 | Py_DECREF(Py_TYPE(me)); |
3aa33042 | 2034 | FREEOBJ(me); |
d7ab1bab | 2035 | } |
2036 | ||
2037 | static PyObject *gchget_name(PyObject *me, void *hunoz) | |
2135a6d3 | 2038 | { return (TEXT_FROMSTR(GCHASH_CH(me)->name)); } |
d7ab1bab | 2039 | |
2040 | static PyObject *gchget_hashsz(PyObject *me, void *hunoz) | |
2041 | { return (PyInt_FromLong(GCHASH_CH(me)->hashsz)); } | |
2042 | ||
2043 | static PyObject *gchget_bufsz(PyObject *me, void *hunoz) | |
2044 | { return (PyInt_FromLong(GCHASH_CH(me)->bufsz)); } | |
2045 | ||
91e56f06 | 2046 | static PyObject *ghmeth_copy(PyObject *me) |
2a448f86 | 2047 | { return (ghash_pywrap((PyObject *)Py_TYPE(me), GH_COPY(GHASH_H(me)))); } |
cfe80e51 | 2048 | |
d7ab1bab | 2049 | static PyObject *ghmeth_hash(PyObject *me, PyObject *arg) |
2050 | { | |
21684137 MW |
2051 | struct bin m = BIN_INIT; |
2052 | PyObject *rc = 0; | |
2053 | ||
2054 | if (!PyArg_ParseTuple(arg, "O&:hash", convbin, &m)) goto end; | |
67c75893 | 2055 | GH_HASH(GHASH_H(me), m.p, m.sz); |
21684137 MW |
2056 | rc = me; Py_INCREF(rc); |
2057 | end: | |
2058 | freebin(&m); | |
2059 | return (rc); | |
d7ab1bab | 2060 | } |
2061 | ||
46e6ad89 | 2062 | #define GHMETH_HASHU_(n, W, w) \ |
2063 | static PyObject *ghmeth_hashu##w(PyObject *me, PyObject *arg) \ | |
2064 | { \ | |
2065 | uint##n x; \ | |
21684137 | 2066 | \ |
0e5c668c | 2067 | if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \ |
46e6ad89 | 2068 | GH_HASHU##W(GHASH_H(me), x); \ |
2069 | RETURN_ME; \ | |
46e6ad89 | 2070 | } |
2071 | DOUINTCONV(GHMETH_HASHU_) | |
2072 | ||
2073 | #define GHMETH_HASHBUF_(n, W, w) \ | |
2074 | static PyObject *ghmeth_hashbuf##w(PyObject *me, PyObject *arg) \ | |
2075 | { \ | |
21684137 MW |
2076 | struct bin in = BIN_INIT; \ |
2077 | PyObject *rc = 0; \ | |
2078 | \ | |
67c75893 | 2079 | if (!PyArg_ParseTuple(arg, "O&:hashbuf" #w, convbin, &in)) goto end; \ |
e01ab3c9 | 2080 | if (in.sz > MASK##n) VALERR("too large"); \ |
67c75893 | 2081 | GH_HASHBUF##W(GHASH_H(me), in.p, in.sz); \ |
21684137 | 2082 | rc = me; Py_INCREF(rc); \ |
46e6ad89 | 2083 | end: \ |
21684137 | 2084 | freebin(&in); \ |
46e6ad89 | 2085 | return (0); \ |
2086 | } | |
2087 | DOUINTCONV(GHMETH_HASHBUF_) | |
2088 | ||
2089 | static PyObject *ghmeth_hashstrz(PyObject *me, PyObject *arg) | |
2090 | { | |
2091 | char *p; | |
2092 | if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0); | |
2093 | GH_HASHSTRZ(GHASH_H(me), p); | |
2094 | RETURN_ME; | |
2095 | } | |
2096 | ||
91e56f06 | 2097 | static PyObject *ghmeth_done(PyObject *me) |
07bcd768 MW |
2098 | { |
2099 | ghash *g; | |
2100 | PyObject *rc; | |
07bcd768 MW |
2101 | g = GH_COPY(GHASH_H(me)); |
2102 | rc = bytestring_pywrap(0, g->ops->c->hashsz); | |
2135a6d3 | 2103 | GH_DONE(g, BIN_PTR(rc)); |
07bcd768 MW |
2104 | GH_DESTROY(g); |
2105 | return (rc); | |
2106 | } | |
2107 | ||
c90f712e | 2108 | static const PyGetSetDef gchash_pygetset[] = { |
07bcd768 | 2109 | #define GETSETNAME(op, name) gch##op##_##name |
d96c882e MW |
2110 | GET (bufsz, "CH.bufsz -> hash buffer size, or zero") |
2111 | GET (hashsz, "CH.hashsz -> hash output size") | |
2112 | GET (name, "CH.name -> name of this kind of hash") | |
07bcd768 MW |
2113 | #undef GETSETNAME |
2114 | { 0 } | |
2115 | }; | |
2116 | ||
c90f712e | 2117 | static const PyMethodDef ghash_pymethods[] = { |
d7ab1bab | 2118 | #define METHNAME(name) ghmeth_##name |
91e56f06 | 2119 | NAMETH(copy, "H.copy() -> HH") |
d96c882e | 2120 | METH (hash, "H.hash(M)") |
46e6ad89 | 2121 | #define METHU_(n, W, w) METH(hashu##w, "H.hashu" #w "(WORD)") |
2122 | DOUINTCONV(METHU_) | |
07bcd768 | 2123 | #undef METHU_ |
46e6ad89 | 2124 | #define METHBUF_(n, W, w) METH(hashbuf##w, "H.hashbuf" #w "(BYTES)") |
2125 | DOUINTCONV(METHBUF_) | |
07bcd768 | 2126 | #undef METHBUF_ |
d96c882e | 2127 | METH (hashstrz, "H.hashstrz(STRING)") |
91e56f06 | 2128 | NAMETH(done, "H.done() -> HASH") |
d7ab1bab | 2129 | #undef METHNAME |
2130 | { 0 } | |
2131 | }; | |
2132 | ||
c263b05c | 2133 | static const PyTypeObject gchash_pytype_skel = { |
591bf41b | 2134 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
c461c9b3 | 2135 | "GCHash", /* @tp_name@ */ |
d7ab1bab | 2136 | sizeof(gchash_pyobj), /* @tp_basicsize@ */ |
2137 | 0, /* @tp_itemsize@ */ | |
2138 | ||
2139 | 0, /* @tp_dealloc@ */ | |
2140 | 0, /* @tp_print@ */ | |
2141 | 0, /* @tp_getattr@ */ | |
2142 | 0, /* @tp_setattr@ */ | |
2143 | 0, /* @tp_compare@ */ | |
2144 | 0, /* @tp_repr@ */ | |
2145 | 0, /* @tp_as_number@ */ | |
2146 | 0, /* @tp_as_sequence@ */ | |
2147 | 0, /* @tp_as_mapping@ */ | |
2148 | 0, /* @tp_hash@ */ | |
2149 | 0, /* @tp_call@ */ | |
2150 | 0, /* @tp_str@ */ | |
2151 | 0, /* @tp_getattro@ */ | |
2152 | 0, /* @tp_setattro@ */ | |
2153 | 0, /* @tp_as_buffer@ */ | |
2154 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
2155 | Py_TPFLAGS_BASETYPE, | |
2156 | ||
2157 | /* @tp_doc@ */ | |
d96c882e | 2158 | "Hash function metaclass.", |
d7ab1bab | 2159 | |
2160 | 0, /* @tp_traverse@ */ | |
2161 | 0, /* @tp_clear@ */ | |
2162 | 0, /* @tp_richcompare@ */ | |
2163 | 0, /* @tp_weaklistoffset@ */ | |
2164 | 0, /* @tp_iter@ */ | |
963a6148 | 2165 | 0, /* @tp_iternext@ */ |
d7ab1bab | 2166 | 0, /* @tp_methods@ */ |
2167 | 0, /* @tp_members@ */ | |
c90f712e | 2168 | PYGETSET(gchash), /* @tp_getset@ */ |
d7ab1bab | 2169 | 0, /* @tp_base@ */ |
2170 | 0, /* @tp_dict@ */ | |
2171 | 0, /* @tp_descr_get@ */ | |
2172 | 0, /* @tp_descr_set@ */ | |
2173 | 0, /* @tp_dictoffset@ */ | |
2174 | 0, /* @tp_init@ */ | |
2175 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
2176 | abstract_pynew, /* @tp_new@ */ | |
3aa33042 | 2177 | 0, /* @tp_free@ */ |
d7ab1bab | 2178 | 0 /* @tp_is_gc@ */ |
2179 | }; | |
2180 | ||
c263b05c | 2181 | static const PyTypeObject ghash_pytype_skel = { |
591bf41b | 2182 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
c461c9b3 | 2183 | "GHash", /* @tp_name@ */ |
d7ab1bab | 2184 | sizeof(ghash_pyobj), /* @tp_basicsize@ */ |
2185 | 0, /* @tp_itemsize@ */ | |
2186 | ||
2187 | ghash_pydealloc, /* @tp_dealloc@ */ | |
2188 | 0, /* @tp_print@ */ | |
2189 | 0, /* @tp_getattr@ */ | |
2190 | 0, /* @tp_setattr@ */ | |
2191 | 0, /* @tp_compare@ */ | |
2192 | 0, /* @tp_repr@ */ | |
2193 | 0, /* @tp_as_number@ */ | |
2194 | 0, /* @tp_as_sequence@ */ | |
2195 | 0, /* @tp_as_mapping@ */ | |
2196 | 0, /* @tp_hash@ */ | |
2197 | 0, /* @tp_call@ */ | |
2198 | 0, /* @tp_str@ */ | |
2199 | 0, /* @tp_getattro@ */ | |
2200 | 0, /* @tp_setattro@ */ | |
2201 | 0, /* @tp_as_buffer@ */ | |
2202 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
2203 | Py_TPFLAGS_BASETYPE, | |
2204 | ||
2205 | /* @tp_doc@ */ | |
d96c882e | 2206 | "Hash function, abstract base class.", |
d7ab1bab | 2207 | |
2208 | 0, /* @tp_traverse@ */ | |
2209 | 0, /* @tp_clear@ */ | |
2210 | 0, /* @tp_richcompare@ */ | |
2211 | 0, /* @tp_weaklistoffset@ */ | |
2212 | 0, /* @tp_iter@ */ | |
963a6148 | 2213 | 0, /* @tp_iternext@ */ |
c90f712e | 2214 | PYMETHODS(ghash), /* @tp_methods@ */ |
d7ab1bab | 2215 | 0, /* @tp_members@ */ |
2216 | 0, /* @tp_getset@ */ | |
2217 | 0, /* @tp_base@ */ | |
2218 | 0, /* @tp_dict@ */ | |
2219 | 0, /* @tp_descr_get@ */ | |
2220 | 0, /* @tp_descr_set@ */ | |
2221 | 0, /* @tp_dictoffset@ */ | |
2222 | 0, /* @tp_init@ */ | |
2223 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
2224 | abstract_pynew, /* @tp_new@ */ | |
3aa33042 | 2225 | 0, /* @tp_free@ */ |
d7ab1bab | 2226 | 0 /* @tp_is_gc@ */ |
2227 | }; | |
2228 | ||
2229 | /*----- Message authentication --------------------------------------------*/ | |
2230 | ||
c6e89d48 | 2231 | static PyTypeObject *gcmac_pytype, *gmac_pytype, *gmhash_pytype; |
d7ab1bab | 2232 | |
c6e89d48 MW |
2233 | typedef struct gcmac_pyobj { |
2234 | PyHeapTypeObject ty; | |
2235 | gcmac *cm; | |
2236 | } gcmac_pyobj; | |
2237 | ||
2238 | #define GCMAC_PYCHECK(o) PyObject_TypeCheck((o), gcmac_pytype) | |
2239 | #define GCMAC_CM(o) (((gcmac_pyobj *)(o))->cm) | |
2240 | #define GCMAC_F(o) (((gcmac_pyobj *)(o))->f) | |
d7ab1bab | 2241 | CONVFUNC(gcmac, gcmac *, GCMAC_CM) |
c6e89d48 MW |
2242 | static PyObject *gmac_pywrap(PyObject *, gmac *); |
2243 | ||
2244 | typedef struct gmac_pyobj { | |
2245 | PyHeapTypeObject ty; | |
2246 | gmac *m; | |
2247 | } gmac_pyobj; | |
2248 | ||
2249 | extern PyTypeObject *gmac_pytype; | |
2250 | #define GMAC_PYCHECK(o) PyObject_TypeCheck((o), gmac_pytype) | |
2251 | #define GMAC_M(o) (((gmac_pyobj *)(o))->m) | |
2252 | #define GMAC_F(o) (((gmac_pyobj *)(o))->f) | |
2253 | extern PyObject *gmac_pywrap(PyObject *, gmac *); | |
2254 | extern int convgmac(PyObject *, void *); | |
d7ab1bab | 2255 | |
2256 | static PyObject *gmac_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
2257 | { | |
827f89d7 | 2258 | static const char *const kwlist[] = { "k", 0 }; |
21684137 MW |
2259 | struct bin k = BIN_INIT; |
2260 | PyObject *rc = 0; | |
d7ab1bab | 2261 | |
67c75893 | 2262 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", KWLIST, convbin, &k)) |
d7ab1bab | 2263 | goto end; |
67c75893 | 2264 | if (keysz(k.sz, GCMAC_CM(ty)->keysz) != k.sz) VALERR("bad key length"); |
21684137 | 2265 | rc = gmac_pywrap((PyObject *)ty, GM_KEY(GCMAC_CM(ty), k.p, k.sz)); |
d7ab1bab | 2266 | end: |
21684137 | 2267 | freebin(&k); |
b2687a0a | 2268 | return (0); |
d7ab1bab | 2269 | } |
2270 | ||
2271 | static PyObject *gmhash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
2272 | { | |
827f89d7 | 2273 | static const char *const kwlist[] = { 0 }; |
d7ab1bab | 2274 | ghash_pyobj *g; |
2275 | ||
827f89d7 | 2276 | if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", KWLIST)) return (0); |
d7ab1bab | 2277 | g = PyObject_NEW(ghash_pyobj, ty); |
2278 | g->h = GM_INIT(GMAC_M(ty)); | |
d7ab1bab | 2279 | Py_INCREF(ty); |
2280 | return ((PyObject *)g); | |
2281 | } | |
2282 | ||
c6e89d48 | 2283 | static PyObject *gcmac_pywrap(gcmac *cm) |
d7ab1bab | 2284 | { |
df9f8366 | 2285 | gcmac_pyobj *g = newtype(gcmac_pytype, 0, cm->name); |
d7ab1bab | 2286 | g->cm = cm; |
24b3d57b MW |
2287 | g->ty.ht_type.tp_basicsize = sizeof(gmac_pyobj); |
2288 | g->ty.ht_type.tp_base = gmac_pytype; | |
d7ab1bab | 2289 | Py_INCREF(gmac_pytype); |
24b3d57b MW |
2290 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
2291 | Py_TPFLAGS_BASETYPE | | |
2292 | Py_TPFLAGS_HEAPTYPE); | |
2293 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; | |
2294 | g->ty.ht_type.tp_free = 0; | |
2295 | g->ty.ht_type.tp_new = gmac_pynew; | |
dc075750 | 2296 | typeready(&g->ty.ht_type); |
d7ab1bab | 2297 | return ((PyObject *)g); |
2298 | } | |
2299 | ||
c6e89d48 | 2300 | static PyObject *gmac_pywrap(PyObject *cobj, gmac *m) |
d7ab1bab | 2301 | { |
2302 | gmac_pyobj *g; | |
2303 | if (!cobj) cobj = gcmac_pywrap((/*unconst*/ gcmac *)GM_CLASS(m)); | |
2304 | else Py_INCREF(cobj); | |
df9f8366 | 2305 | g = newtype((PyTypeObject *)cobj, 0, 0); |
828b1388 | 2306 | g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj); |
2135a6d3 MW |
2307 | g->ty.ht_name = TEXT_FORMAT("%s(keyed)", m->ops->c->name); |
2308 | g->ty.ht_type.tp_name = TEXT_PTR(g->ty.ht_name); | |
24b3d57b | 2309 | g->ty.ht_type.tp_base = gmhash_pytype; |
d7ab1bab | 2310 | Py_INCREF(gmac_pytype); |
24b3d57b MW |
2311 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
2312 | Py_TPFLAGS_BASETYPE | | |
2313 | Py_TPFLAGS_HEAPTYPE); | |
2314 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; | |
2315 | g->ty.ht_type.tp_free = 0; | |
2316 | g->ty.ht_type.tp_new = gmhash_pynew; | |
dc075750 | 2317 | typeready(&g->ty.ht_type); |
d7ab1bab | 2318 | g->m = m; |
b2687a0a | 2319 | return ((PyObject *)g); |
d7ab1bab | 2320 | } |
2321 | ||
2322 | static void gmac_pydealloc(PyObject *me) | |
2323 | { | |
c41d0371 | 2324 | GM_DESTROY(GMAC_M(me)); |
2a448f86 | 2325 | Py_DECREF(Py_TYPE(me)); |
d7ab1bab | 2326 | PyType_Type.tp_dealloc(me); |
2327 | } | |
2328 | ||
2329 | static PyObject *gcmget_name(PyObject *me, void *hunoz) | |
2135a6d3 | 2330 | { return (TEXT_FROMSTR(GCMAC_CM(me)->name)); } |
d7ab1bab | 2331 | |
2332 | static PyObject *gcmget_keysz(PyObject *me, void *hunoz) | |
2333 | { return (keysz_pywrap(GCMAC_CM(me)->keysz)); } | |
2334 | ||
2335 | static PyObject *gcmget_tagsz(PyObject *me, void *hunoz) | |
2336 | { return (PyInt_FromLong(GCMAC_CM(me)->hashsz)); } | |
2337 | ||
c90f712e | 2338 | static const PyGetSetDef gcmac_pygetset[] = { |
d7ab1bab | 2339 | #define GETSETNAME(op, name) gcm##op##_##name |
d96c882e MW |
2340 | GET (keysz, "CM.keysz -> acceptable key sizes") |
2341 | GET (tagsz, "CM.tagsz -> MAC output size") | |
2342 | GET (name, "CM.name -> name of this kind of MAC") | |
d7ab1bab | 2343 | #undef GETSETNAME |
2344 | { 0 } | |
2345 | }; | |
2346 | ||
71574cba MW |
2347 | static PyObject *gmget_name(PyObject *me, void *hunoz) |
2348 | { return (TEXT_FROMSTR(GMAC_M(me)->ops->c->name)); } | |
2349 | ||
2350 | static PyObject *gmget_hashsz(PyObject *me, void *hunoz) | |
2351 | { return (PyInt_FromLong(GMAC_M(me)->ops->c->hashsz)); } | |
2352 | #define gmget_tagsz gmget_hashsz | |
2353 | ||
2354 | static const PyGetSetDef gmac_pygetset[] = { | |
2355 | #define GETSETNAME(op, name) gm##op##_##name | |
2356 | GET (hashsz, "M.hashsz -> MAC output size") | |
2357 | GET (tagsz, "M.tagsz -> MAC output size") | |
2358 | GET (name, "M.name -> name of this kind of MAC") | |
2359 | #undef GETSETNAME | |
2360 | { 0 } | |
2361 | }; | |
2362 | ||
c263b05c | 2363 | static const PyTypeObject gcmac_pytype_skel = { |
591bf41b | 2364 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
c461c9b3 | 2365 | "GCMAC", /* @tp_name@ */ |
d7ab1bab | 2366 | sizeof(gchash_pyobj), /* @tp_basicsize@ */ |
2367 | 0, /* @tp_itemsize@ */ | |
2368 | ||
2369 | 0, /* @tp_dealloc@ */ | |
2370 | 0, /* @tp_print@ */ | |
2371 | 0, /* @tp_getattr@ */ | |
2372 | 0, /* @tp_setattr@ */ | |
2373 | 0, /* @tp_compare@ */ | |
2374 | 0, /* @tp_repr@ */ | |
2375 | 0, /* @tp_as_number@ */ | |
2376 | 0, /* @tp_as_sequence@ */ | |
2377 | 0, /* @tp_as_mapping@ */ | |
2378 | 0, /* @tp_hash@ */ | |
2379 | 0, /* @tp_call@ */ | |
2380 | 0, /* @tp_str@ */ | |
2381 | 0, /* @tp_getattro@ */ | |
2382 | 0, /* @tp_setattro@ */ | |
2383 | 0, /* @tp_as_buffer@ */ | |
2384 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
2385 | Py_TPFLAGS_BASETYPE, | |
2386 | ||
2387 | /* @tp_doc@ */ | |
d96c882e | 2388 | "Message authentication code metametaclass.", |
d7ab1bab | 2389 | |
2390 | 0, /* @tp_traverse@ */ | |
2391 | 0, /* @tp_clear@ */ | |
2392 | 0, /* @tp_richcompare@ */ | |
2393 | 0, /* @tp_weaklistoffset@ */ | |
2394 | 0, /* @tp_iter@ */ | |
963a6148 | 2395 | 0, /* @tp_iternext@ */ |
d7ab1bab | 2396 | 0, /* @tp_methods@ */ |
2397 | 0, /* @tp_members@ */ | |
c90f712e | 2398 | PYGETSET(gcmac), /* @tp_getset@ */ |
d7ab1bab | 2399 | 0, /* @tp_base@ */ |
2400 | 0, /* @tp_dict@ */ | |
2401 | 0, /* @tp_descr_get@ */ | |
2402 | 0, /* @tp_descr_set@ */ | |
2403 | 0, /* @tp_dictoffset@ */ | |
2404 | 0, /* @tp_init@ */ | |
2405 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
2406 | abstract_pynew, /* @tp_new@ */ | |
3aa33042 | 2407 | 0, /* @tp_free@ */ |
d7ab1bab | 2408 | 0 /* @tp_is_gc@ */ |
2409 | }; | |
2410 | ||
c263b05c | 2411 | static const PyTypeObject gmac_pytype_skel = { |
591bf41b | 2412 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
c461c9b3 | 2413 | "GMAC", /* @tp_name@ */ |
d7ab1bab | 2414 | sizeof(gmac_pyobj), /* @tp_basicsize@ */ |
2415 | 0, /* @tp_itemsize@ */ | |
2416 | ||
2417 | gmac_pydealloc, /* @tp_dealloc@ */ | |
2418 | 0, /* @tp_print@ */ | |
2419 | 0, /* @tp_getattr@ */ | |
2420 | 0, /* @tp_setattr@ */ | |
2421 | 0, /* @tp_compare@ */ | |
2422 | 0, /* @tp_repr@ */ | |
2423 | 0, /* @tp_as_number@ */ | |
2424 | 0, /* @tp_as_sequence@ */ | |
2425 | 0, /* @tp_as_mapping@ */ | |
2426 | 0, /* @tp_hash@ */ | |
2427 | 0, /* @tp_call@ */ | |
2428 | 0, /* @tp_str@ */ | |
2429 | 0, /* @tp_getattro@ */ | |
2430 | 0, /* @tp_setattro@ */ | |
2431 | 0, /* @tp_as_buffer@ */ | |
2432 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
2433 | Py_TPFLAGS_BASETYPE, | |
2434 | ||
2435 | /* @tp_doc@ */ | |
d96c882e | 2436 | "Message authentication code metaclass, abstract base class.", |
d7ab1bab | 2437 | |
2438 | 0, /* @tp_traverse@ */ | |
2439 | 0, /* @tp_clear@ */ | |
2440 | 0, /* @tp_richcompare@ */ | |
2441 | 0, /* @tp_weaklistoffset@ */ | |
2442 | 0, /* @tp_iter@ */ | |
963a6148 | 2443 | 0, /* @tp_iternext@ */ |
d7ab1bab | 2444 | 0, /* @tp_methods@ */ |
2445 | 0, /* @tp_members@ */ | |
71574cba | 2446 | PYGETSET(gmac), /* @tp_getset@ */ |
d7ab1bab | 2447 | 0, /* @tp_base@ */ |
2448 | 0, /* @tp_dict@ */ | |
2449 | 0, /* @tp_descr_get@ */ | |
2450 | 0, /* @tp_descr_set@ */ | |
2451 | 0, /* @tp_dictoffset@ */ | |
2452 | 0, /* @tp_init@ */ | |
2453 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
2454 | abstract_pynew, /* @tp_new@ */ | |
3aa33042 | 2455 | 0, /* @tp_free@ */ |
d7ab1bab | 2456 | 0 /* @tp_is_gc@ */ |
2457 | }; | |
2458 | ||
c263b05c | 2459 | static const PyTypeObject gmhash_pytype_skel = { |
591bf41b | 2460 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
c461c9b3 | 2461 | "GMACHash", /* @tp_name@ */ |
d7ab1bab | 2462 | sizeof(ghash_pyobj), /* @tp_basicsize@ */ |
2463 | 0, /* @tp_itemsize@ */ | |
2464 | ||
2465 | ghash_pydealloc, /* @tp_dealloc@ */ | |
2466 | 0, /* @tp_print@ */ | |
2467 | 0, /* @tp_getattr@ */ | |
2468 | 0, /* @tp_setattr@ */ | |
2469 | 0, /* @tp_compare@ */ | |
2470 | 0, /* @tp_repr@ */ | |
2471 | 0, /* @tp_as_number@ */ | |
2472 | 0, /* @tp_as_sequence@ */ | |
2473 | 0, /* @tp_as_mapping@ */ | |
2474 | 0, /* @tp_hash@ */ | |
2475 | 0, /* @tp_call@ */ | |
2476 | 0, /* @tp_str@ */ | |
2477 | 0, /* @tp_getattro@ */ | |
2478 | 0, /* @tp_setattro@ */ | |
2479 | 0, /* @tp_as_buffer@ */ | |
2480 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
2481 | Py_TPFLAGS_BASETYPE, | |
2482 | ||
2483 | /* @tp_doc@ */ | |
d96c882e | 2484 | "Message authentication code, abstract base class.", |
d7ab1bab | 2485 | |
2486 | 0, /* @tp_traverse@ */ | |
2487 | 0, /* @tp_clear@ */ | |
2488 | 0, /* @tp_richcompare@ */ | |
2489 | 0, /* @tp_weaklistoffset@ */ | |
2490 | 0, /* @tp_iter@ */ | |
963a6148 | 2491 | 0, /* @tp_iternext@ */ |
d7ab1bab | 2492 | 0, /* @tp_methods@ */ |
2493 | 0, /* @tp_members@ */ | |
2494 | 0, /* @tp_getset@ */ | |
2495 | 0, /* @tp_base@ */ | |
2496 | 0, /* @tp_dict@ */ | |
2497 | 0, /* @tp_descr_get@ */ | |
2498 | 0, /* @tp_descr_set@ */ | |
2499 | 0, /* @tp_dictoffset@ */ | |
2500 | 0, /* @tp_init@ */ | |
2501 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
2502 | abstract_pynew, /* @tp_new@ */ | |
3aa33042 | 2503 | 0, /* @tp_free@ */ |
d7ab1bab | 2504 | 0 /* @tp_is_gc@ */ |
2505 | }; | |
2506 | ||
204d480b MW |
2507 | /*----- Special snowflake for Poly1305 ------------------------------------*/ |
2508 | ||
2509 | PyTypeObject *poly1305cls_pytype, *poly1305key_pytype, *poly1305hash_pytype; | |
2510 | ||
2511 | typedef struct poly1305key_pyobj { | |
2512 | PyHeapTypeObject ty; | |
2513 | poly1305_key k; | |
2514 | } poly1305key_pyobj; | |
2515 | ||
2516 | typedef struct poly1305hash_pyobj { | |
2517 | PyObject_HEAD | |
2518 | unsigned f; | |
2519 | #define f_mask 1u | |
2520 | poly1305_ctx ctx; | |
2521 | } poly1305hash_pyobj; | |
2522 | ||
2523 | #define P1305_F(o) (((poly1305hash_pyobj *)(o))->f) | |
2524 | #define P1305_CTX(o) (&((poly1305hash_pyobj *)(o))->ctx) | |
2525 | CONVFUNC(poly1305hash, poly1305_ctx *, P1305_CTX) | |
2526 | ||
2527 | static PyObject *poly1305hash_pynew(PyTypeObject *ty, | |
2528 | PyObject *arg, PyObject *kw) | |
2529 | { | |
827f89d7 | 2530 | static const char *const kwlist[] = { "mask", 0 }; |
204d480b MW |
2531 | poly1305key_pyobj *pk = (poly1305key_pyobj *)ty; |
2532 | poly1305hash_pyobj *ph; | |
21684137 MW |
2533 | PyObject *rc = 0; |
2534 | struct bin m = BIN_INIT; | |
204d480b | 2535 | |
67c75893 | 2536 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", KWLIST, convbin, &m)) |
21684137 | 2537 | goto end; |
67c75893 | 2538 | if (m.p && m.sz != POLY1305_MASKSZ) VALERR("bad mask length"); |
204d480b MW |
2539 | ph = PyObject_NEW(poly1305hash_pyobj, ty); |
2540 | ph->f = 0; | |
67c75893 MW |
2541 | if (m.p) ph->f |= f_mask; |
2542 | poly1305_macinit(&ph->ctx, &pk->k, m.p); | |
204d480b | 2543 | Py_INCREF(ty); |
21684137 | 2544 | rc = (PyObject *)ph; |
204d480b | 2545 | end: |
21684137 MW |
2546 | freebin(&m); |
2547 | return (rc); | |
204d480b MW |
2548 | } |
2549 | ||
2550 | static PyObject *poly1305key_pynew(PyTypeObject *ty, | |
2551 | PyObject *arg, PyObject *kw) | |
2552 | { | |
827f89d7 | 2553 | static const char *const kwlist[] = { "k", 0 }; |
204d480b | 2554 | poly1305key_pyobj *pk; |
21684137 MW |
2555 | struct bin k = BIN_INIT; |
2556 | PyObject *rc = 0; | |
204d480b | 2557 | |
67c75893 | 2558 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", KWLIST, convbin, &k)) |
204d480b | 2559 | goto end; |
67c75893 | 2560 | if (keysz(k.sz, poly1305_keysz) != k.sz) VALERR("bad key length"); |
204d480b MW |
2561 | |
2562 | pk = newtype(ty, 0, 0); | |
2135a6d3 | 2563 | pk->ty.ht_name = TEXT_FROMSTR("poly1305(keyed)"); |
204d480b | 2564 | pk->ty.ht_type.tp_basicsize = sizeof(poly1305hash_pyobj); |
2135a6d3 | 2565 | pk->ty.ht_type.tp_name = TEXT_PTR(pk->ty.ht_name); |
204d480b MW |
2566 | pk->ty.ht_type.tp_base = poly1305hash_pytype; |
2567 | Py_INCREF(poly1305key_pytype); | |
2568 | pk->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | | |
2569 | Py_TPFLAGS_BASETYPE | | |
2570 | Py_TPFLAGS_HEAPTYPE); | |
2571 | pk->ty.ht_type.tp_alloc = PyType_GenericAlloc; | |
2572 | pk->ty.ht_type.tp_free = 0; | |
2573 | pk->ty.ht_type.tp_new = poly1305hash_pynew; | |
2574 | typeready(&pk->ty.ht_type); | |
2575 | ||
67c75893 | 2576 | poly1305_keyinit(&pk->k, k.p, k.sz); |
21684137 | 2577 | rc = (PyObject *)pk; |
204d480b | 2578 | end: |
21684137 MW |
2579 | freebin(&k); |
2580 | return (rc); | |
204d480b MW |
2581 | } |
2582 | ||
2583 | static PyObject *poly1305clsget_name(PyObject *me, void *hunoz) | |
2135a6d3 | 2584 | { return (TEXT_FROMSTR("poly1305")); } |
204d480b MW |
2585 | |
2586 | static PyObject *poly1305clsget_keysz(PyObject *me, void *hunoz) | |
2587 | { return (keysz_pywrap(poly1305_keysz)); } | |
2588 | ||
2589 | static PyObject *poly1305clsget_masksz(PyObject *me, void *hunoz) | |
2590 | { return (PyInt_FromLong(POLY1305_MASKSZ)); } | |
2591 | ||
2592 | static PyObject *poly1305clsget_tagsz(PyObject *me, void *hunoz) | |
2593 | { return (PyInt_FromLong(POLY1305_TAGSZ)); } | |
2594 | ||
91e56f06 | 2595 | static PyObject *polymeth_copy(PyObject *me) |
204d480b MW |
2596 | { |
2597 | poly1305hash_pyobj *ph; | |
2a448f86 | 2598 | ph = PyObject_NEW(poly1305hash_pyobj, Py_TYPE(me)); |
204d480b | 2599 | poly1305_copy(&ph->ctx, P1305_CTX(me)); |
2a448f86 | 2600 | Py_INCREF(Py_TYPE(me)); |
204d480b MW |
2601 | return ((PyObject *)ph); |
2602 | } | |
2603 | ||
2604 | static PyObject *polymeth_hash(PyObject *me, PyObject *arg) | |
2605 | { | |
21684137 MW |
2606 | struct bin m = BIN_INIT; |
2607 | PyObject *rc = 0; | |
2608 | ||
2609 | if (!PyArg_ParseTuple(arg, "O&:hash", convbin, &m)) goto end; | |
67c75893 | 2610 | poly1305_hash(P1305_CTX(me), m.p, m.sz); |
21684137 MW |
2611 | rc = me; Py_INCREF(rc); |
2612 | end: | |
2613 | freebin(&m); | |
2614 | return (rc); | |
204d480b MW |
2615 | } |
2616 | ||
2617 | #define POLYMETH_HASHU_(n, W, w) \ | |
2618 | static PyObject *polymeth_hashu##w(PyObject *me, PyObject *arg) \ | |
2619 | { \ | |
2620 | uint##n x; \ | |
2621 | octet b[SZ_##W]; \ | |
21684137 | 2622 | \ |
0e5c668c | 2623 | if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \ |
0c87e818 | 2624 | STORE##W(b, x); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \ |
204d480b | 2625 | RETURN_ME; \ |
204d480b MW |
2626 | } |
2627 | DOUINTCONV(POLYMETH_HASHU_) | |
2628 | ||
2629 | #define POLYMETH_HASHBUF_(n, W, w) \ | |
2630 | static PyObject *polymeth_hashbuf##w(PyObject *me, PyObject *arg) \ | |
2631 | { \ | |
21684137 | 2632 | struct bin in = BIN_INIT; \ |
204d480b | 2633 | octet b[SZ_##W]; \ |
21684137 MW |
2634 | PyObject *rc = 0; \ |
2635 | \ | |
67c75893 | 2636 | if (!PyArg_ParseTuple(arg, "O&:hashbuf" #w, convbin, &in)) goto end; \ |
e01ab3c9 | 2637 | if (in.sz > MASK##n) VALERR("too large"); \ |
67c75893 MW |
2638 | STORE##W(b, in.sz); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \ |
2639 | poly1305_hash(P1305_CTX(me), in.p, in.sz); \ | |
21684137 | 2640 | rc = me; Py_INCREF(rc); \ |
204d480b | 2641 | end: \ |
21684137 MW |
2642 | freebin(&in); \ |
2643 | return (rc); \ | |
204d480b MW |
2644 | } |
2645 | DOUINTCONV(POLYMETH_HASHBUF_) | |
2646 | ||
2647 | static PyObject *polymeth_hashstrz(PyObject *me, PyObject *arg) | |
2648 | { | |
2649 | char *p; | |
2650 | if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0); | |
2651 | poly1305_hash(P1305_CTX(me), p, strlen(p) + 1); | |
2652 | RETURN_ME; | |
2653 | } | |
2654 | ||
91e56f06 MW |
2655 | static PyObject *polymeth_flush(PyObject *me) |
2656 | { poly1305_flush(P1305_CTX(me)); RETURN_ME; } | |
204d480b | 2657 | |
91e56f06 MW |
2658 | static PyObject *polymeth_flushzero(PyObject *me) |
2659 | { poly1305_flushzero(P1305_CTX(me)); RETURN_ME; } | |
5c17375a | 2660 | |
204d480b MW |
2661 | static PyObject *polymeth_concat(PyObject *me, PyObject *arg) |
2662 | { | |
2663 | PyObject *pre, *suff; | |
2664 | if (!PyArg_ParseTuple(arg, "OO:concat", &pre, &suff)) return (0); | |
2665 | if (!PyObject_TypeCheck(pre, poly1305hash_pytype) || | |
2666 | !PyObject_TypeCheck(suff, poly1305hash_pytype)) | |
2667 | TYERR("wanted a poly1305hash"); | |
2a448f86 | 2668 | if (Py_TYPE(me) != Py_TYPE(pre) || Py_TYPE(me) != Py_TYPE(suff)) |
204d480b MW |
2669 | TYERR("key mismatch"); |
2670 | if (P1305_CTX(pre)->nbuf) VALERR("prefix is not block-aligned"); | |
2671 | poly1305_concat(P1305_CTX(me), P1305_CTX(pre), P1305_CTX(suff)); | |
2672 | RETURN_ME; | |
2673 | end: | |
2674 | return (0); | |
2675 | } | |
2676 | ||
91e56f06 | 2677 | static PyObject *polymeth_done(PyObject *me) |
204d480b MW |
2678 | { |
2679 | PyObject *rc; | |
204d480b MW |
2680 | if (!(P1305_F(me) & f_mask)) VALERR("no mask"); |
2681 | rc = bytestring_pywrap(0, POLY1305_TAGSZ); | |
2135a6d3 | 2682 | poly1305_done(P1305_CTX(me), BIN_PTR(rc)); |
204d480b MW |
2683 | return (rc); |
2684 | end: | |
2685 | return (0); | |
2686 | } | |
2687 | ||
c90f712e | 2688 | static const PyGetSetDef poly1305cls_pygetset[] = { |
204d480b | 2689 | #define GETSETNAME(op, name) poly1305cls##op##_##name |
d96c882e MW |
2690 | GET (keysz, "PC.keysz -> acceptable key sizes") |
2691 | GET (masksz, "PC.masksz -> mask size") | |
2692 | GET (tagsz, "PC.tagsz -> MAC output size") | |
2693 | GET (name, "PC.name -> name of this kind of MAC") | |
204d480b MW |
2694 | #undef GETSETNAME |
2695 | { 0 } | |
2696 | }; | |
2697 | ||
71574cba MW |
2698 | static PyObject *poly1305get_name(PyObject *me, void *hunoz) |
2699 | { RETURN_OBJ(((PyHeapTypeObject *)poly1305key_pytype)->ht_name); } | |
2700 | ||
2701 | static PyObject *poly1305get_tagsz(PyObject *me, void *hunoz) | |
2702 | { return (PyInt_FromLong(16)); } | |
2703 | ||
2704 | static const PyGetSetDef poly1305_pygetset[] = { | |
2705 | #define GETSETNAME(op, name) poly1305##op##_##name | |
2706 | GET (tagsz, "PK.tagsz -> MAC output size") | |
2707 | GET (name, "PK.name -> name of this kind of MAC") | |
2708 | #undef GETSETNAME | |
2709 | { 0 } | |
2710 | }; | |
2711 | ||
c90f712e | 2712 | static const PyMethodDef poly1305hash_pymethods[] = { |
204d480b | 2713 | #define METHNAME(name) polymeth_##name |
91e56f06 | 2714 | NAMETH(copy, "P.copy() -> PP") |
d96c882e | 2715 | METH (hash, "P.hash(M)") |
204d480b MW |
2716 | #define METHU_(n, W, w) METH(hashu##w, "P.hashu" #w "(WORD)") |
2717 | DOUINTCONV(METHU_) | |
2718 | #undef METHU_ | |
2719 | #define METHBUF_(n, W, w) METH(hashbuf##w, "P.hashbuf" #w "(BYTES)") | |
2720 | DOUINTCONV(METHBUF_) | |
2721 | #undef METHBUF_ | |
d96c882e | 2722 | METH (hashstrz, "P.hashstrz(STRING)") |
91e56f06 MW |
2723 | NAMETH(flush, "P.flush()") |
2724 | NAMETH(flushzero, "P.flushzero()") | |
d96c882e | 2725 | METH (concat, "P.concat(PREFIX, SUFFIX)") |
91e56f06 | 2726 | NAMETH(done, "P.done() -> TAG") |
204d480b MW |
2727 | #undef METHNAME |
2728 | { 0 } | |
2729 | }; | |
2730 | ||
c263b05c | 2731 | static const PyTypeObject poly1305cls_pytype_skel = { |
591bf41b | 2732 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
2edb633e | 2733 | "_Poly1305Class", /* @tp_name@ */ |
204d480b MW |
2734 | sizeof(PyHeapTypeObject), /* @tp_basicsize@ */ |
2735 | 0, /* @tp_itemsize@ */ | |
2736 | ||
2737 | 0, /* @tp_dealloc@ */ | |
2738 | 0, /* @tp_print@ */ | |
2739 | 0, /* @tp_getattr@ */ | |
2740 | 0, /* @tp_setattr@ */ | |
2741 | 0, /* @tp_compare@ */ | |
2742 | 0, /* @tp_repr@ */ | |
2743 | 0, /* @tp_as_number@ */ | |
2744 | 0, /* @tp_as_sequence@ */ | |
2745 | 0, /* @tp_as_mapping@ */ | |
2746 | 0, /* @tp_hash@ */ | |
2747 | 0, /* @tp_call@ */ | |
2748 | 0, /* @tp_str@ */ | |
2749 | 0, /* @tp_getattro@ */ | |
2750 | 0, /* @tp_setattro@ */ | |
2751 | 0, /* @tp_as_buffer@ */ | |
2752 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
2753 | Py_TPFLAGS_BASETYPE, | |
2754 | ||
2755 | /* @tp_doc@ */ | |
d96c882e | 2756 | "Poly1305 metametaclass. Best not to ask.", |
204d480b MW |
2757 | |
2758 | 0, /* @tp_traverse@ */ | |
2759 | 0, /* @tp_clear@ */ | |
2760 | 0, /* @tp_richcompare@ */ | |
2761 | 0, /* @tp_weaklistoffset@ */ | |
2762 | 0, /* @tp_iter@ */ | |
2763 | 0, /* @tp_iternext@ */ | |
2764 | 0, /* @tp_methods@ */ | |
2765 | 0, /* @tp_members@ */ | |
c90f712e | 2766 | PYGETSET(poly1305cls), /* @tp_getset@ */ |
204d480b MW |
2767 | 0, /* @tp_base@ */ |
2768 | 0, /* @tp_dict@ */ | |
2769 | 0, /* @tp_descr_get@ */ | |
2770 | 0, /* @tp_descr_set@ */ | |
2771 | 0, /* @tp_dictoffset@ */ | |
2772 | 0, /* @tp_init@ */ | |
2773 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
2774 | abstract_pynew, /* @tp_new@ */ | |
2775 | 0, /* @tp_free@ */ | |
2776 | 0 /* @tp_is_gc@ */ | |
2777 | }; | |
2778 | ||
c263b05c | 2779 | static const PyTypeObject poly1305key_pytype_skel = { |
591bf41b | 2780 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
204d480b MW |
2781 | "poly1305", /* @tp_name@ */ |
2782 | sizeof(poly1305key_pyobj), /* @tp_basicsize@ */ | |
2783 | 0, /* @tp_itemsize@ */ | |
2784 | ||
2785 | 0, /* @tp_dealloc@ */ | |
2786 | 0, /* @tp_print@ */ | |
2787 | 0, /* @tp_getattr@ */ | |
2788 | 0, /* @tp_setattr@ */ | |
2789 | 0, /* @tp_compare@ */ | |
2790 | 0, /* @tp_repr@ */ | |
2791 | 0, /* @tp_as_number@ */ | |
2792 | 0, /* @tp_as_sequence@ */ | |
2793 | 0, /* @tp_as_mapping@ */ | |
2794 | 0, /* @tp_hash@ */ | |
2795 | 0, /* @tp_call@ */ | |
2796 | 0, /* @tp_str@ */ | |
2797 | 0, /* @tp_getattro@ */ | |
2798 | 0, /* @tp_setattro@ */ | |
2799 | 0, /* @tp_as_buffer@ */ | |
2800 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
2801 | Py_TPFLAGS_BASETYPE, | |
2802 | ||
2803 | /* @tp_doc@ */ | |
d96c882e | 2804 | "poly1305(K): Poly1305 key.", |
204d480b MW |
2805 | |
2806 | 0, /* @tp_traverse@ */ | |
2807 | 0, /* @tp_clear@ */ | |
2808 | 0, /* @tp_richcompare@ */ | |
2809 | 0, /* @tp_weaklistoffset@ */ | |
2810 | 0, /* @tp_iter@ */ | |
2811 | 0, /* @tp_iternext@ */ | |
2812 | 0, /* @tp_methods@ */ | |
2813 | 0, /* @tp_members@ */ | |
71574cba | 2814 | PYGETSET(poly1305), /* @tp_getset@ */ |
204d480b MW |
2815 | 0, /* @tp_base@ */ |
2816 | 0, /* @tp_dict@ */ | |
2817 | 0, /* @tp_descr_get@ */ | |
2818 | 0, /* @tp_descr_set@ */ | |
2819 | 0, /* @tp_dictoffset@ */ | |
2820 | 0, /* @tp_init@ */ | |
2821 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
2822 | poly1305key_pynew, /* @tp_new@ */ | |
2823 | 0, /* @tp_free@ */ | |
2824 | 0 /* @tp_is_gc@ */ | |
2825 | }; | |
2826 | ||
c263b05c | 2827 | static const PyTypeObject poly1305hash_pytype_skel = { |
591bf41b | 2828 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
204d480b MW |
2829 | "Poly1305Hash", /* @tp_name@ */ |
2830 | sizeof(poly1305hash_pyobj), /* @tp_basicsize@ */ | |
2831 | 0, /* @tp_itemsize@ */ | |
2832 | ||
2833 | 0, /* @tp_dealloc@ */ | |
2834 | 0, /* @tp_print@ */ | |
2835 | 0, /* @tp_getattr@ */ | |
2836 | 0, /* @tp_setattr@ */ | |
2837 | 0, /* @tp_compare@ */ | |
2838 | 0, /* @tp_repr@ */ | |
2839 | 0, /* @tp_as_number@ */ | |
2840 | 0, /* @tp_as_sequence@ */ | |
2841 | 0, /* @tp_as_mapping@ */ | |
2842 | 0, /* @tp_hash@ */ | |
2843 | 0, /* @tp_call@ */ | |
2844 | 0, /* @tp_str@ */ | |
2845 | 0, /* @tp_getattro@ */ | |
2846 | 0, /* @tp_setattro@ */ | |
2847 | 0, /* @tp_as_buffer@ */ | |
2848 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
2849 | Py_TPFLAGS_BASETYPE, | |
2850 | ||
2851 | /* @tp_doc@ */ | |
d96c882e | 2852 | "Poly1305 MAC context base class.", |
204d480b MW |
2853 | |
2854 | 0, /* @tp_traverse@ */ | |
2855 | 0, /* @tp_clear@ */ | |
2856 | 0, /* @tp_richcompare@ */ | |
2857 | 0, /* @tp_weaklistoffset@ */ | |
2858 | 0, /* @tp_iter@ */ | |
2859 | 0, /* @tp_iternext@ */ | |
c90f712e | 2860 | PYMETHODS(poly1305hash), /* @tp_methods@ */ |
204d480b MW |
2861 | 0, /* @tp_members@ */ |
2862 | 0, /* @tp_getset@ */ | |
2863 | 0, /* @tp_base@ */ | |
2864 | 0, /* @tp_dict@ */ | |
2865 | 0, /* @tp_descr_get@ */ | |
2866 | 0, /* @tp_descr_set@ */ | |
2867 | 0, /* @tp_dictoffset@ */ | |
2868 | 0, /* @tp_init@ */ | |
2869 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
2870 | abstract_pynew, /* @tp_new@ */ | |
2871 | 0, /* @tp_free@ */ | |
2872 | 0 /* @tp_is_gc@ */ | |
2873 | }; | |
2874 | ||
a75e68c9 MW |
2875 | /*----- Special snowflake for HSalsa and HChaCha --------------------------*/ |
2876 | ||
2877 | #define DEF_HDANCE(DANCE, HDANCE, dance, hdance) \ | |
2878 | static PyObject *meth_##hdance##_prf(PyObject *me, PyObject *arg) \ | |
2879 | { \ | |
2880 | dance##_ctx dance; \ | |
21684137 MW |
2881 | struct bin k = BIN_INIT, n = BIN_INIT; \ |
2882 | PyObject *rc = 0; \ | |
2883 | \ | |
67c75893 MW |
2884 | if (!PyArg_ParseTuple(arg, "O&O&:" #hdance "_prf", \ |
2885 | convbin, &k, convbin, &n)) \ | |
a75e68c9 | 2886 | goto end; \ |
67c75893 MW |
2887 | if (k.sz != keysz(k.sz, dance##_keysz)) VALERR("bad key length"); \ |
2888 | if (n.sz != HDANCE##_INSZ) VALERR("bad input length"); \ | |
a75e68c9 | 2889 | rc = bytestring_pywrap(0, HSALSA20_OUTSZ); \ |
67c75893 | 2890 | dance##_init(&dance, k.p, k.sz, 0); \ |
2135a6d3 | 2891 | hdance##_prf(&dance, n.p, BIN_PTR(rc)); \ |
a75e68c9 | 2892 | end: \ |
21684137 MW |
2893 | freebin(&k); freebin(&n); \ |
2894 | return (rc); \ | |
a75e68c9 MW |
2895 | } |
2896 | ||
2897 | DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa20) | |
2898 | DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa2012) | |
2899 | DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa208) | |
2900 | ||
2901 | DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha20) | |
2902 | DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha12) | |
2903 | DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha8) | |
204d480b | 2904 | |
b35fdbe6 MW |
2905 | /*----- Keccak-p[1600, n] -------------------------------------------------*/ |
2906 | ||
2907 | static PyTypeObject *kxvik_pytype; | |
2908 | ||
2909 | typedef struct kxvik_pyobj { | |
2910 | PyObject_HEAD | |
2911 | keccak1600_state s; | |
2912 | unsigned n; | |
2913 | } kxvik_pyobj; | |
2914 | ||
8d2dd9dc | 2915 | static PyObject *kxvik_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) |
b35fdbe6 MW |
2916 | { |
2917 | unsigned n = 24; | |
2918 | kxvik_pyobj *rc = 0; | |
827f89d7 MW |
2919 | static const char *const kwlist[] = { "nround", 0 }; |
2920 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", KWLIST, | |
b35fdbe6 MW |
2921 | convuint, &n)) |
2922 | goto end; | |
2923 | rc = (kxvik_pyobj *)ty->tp_alloc(ty, 0); | |
2924 | rc->n = n; | |
2925 | keccak1600_init(&rc->s); | |
2926 | end: | |
2927 | return ((PyObject *)rc); | |
2928 | } | |
2929 | ||
91e56f06 | 2930 | static PyObject *kxvikmeth_copy(PyObject *me) |
cfe80e51 MW |
2931 | { |
2932 | kxvik_pyobj *k = (kxvik_pyobj *)me, *rc = 0; | |
2a448f86 | 2933 | rc = (kxvik_pyobj *)Py_TYPE(k)->tp_alloc(Py_TYPE(k), 0); |
cfe80e51 | 2934 | rc->s = k->s; rc->n = k->n; |
cfe80e51 MW |
2935 | return ((PyObject *)rc); |
2936 | } | |
2937 | ||
b35fdbe6 MW |
2938 | static PyObject *kxvikmeth_mix(PyObject *me, PyObject *arg) |
2939 | { | |
2940 | kxvik_pyobj *k = (kxvik_pyobj *)me; | |
2941 | kludge64 t[25]; | |
2942 | const octet *q; | |
2943 | octet buf[8]; | |
2944 | unsigned i; | |
21684137 MW |
2945 | struct bin in = BIN_INIT; |
2946 | PyObject *rc = 0; | |
67c75893 | 2947 | size_t n; |
b35fdbe6 | 2948 | |
67c75893 MW |
2949 | if (!PyArg_ParseTuple(arg, "O&:mix", convbin, &in)) goto end; |
2950 | if (in.sz > 200) VALERR("out of range"); | |
2951 | q = in.p; n = in.sz; | |
b35fdbe6 MW |
2952 | i = 0; |
2953 | while (n > 8) { LOAD64_L_(t[i], q); i++; q += 8; n -= 8; } | |
2954 | if (n) { | |
2955 | memcpy(buf, q, n); memset(buf + n, 0, 8 - n); | |
2956 | LOAD64_L_(t[i], buf); i++; | |
2957 | } | |
2958 | keccak1600_mix(&k->s, t, i); | |
21684137 | 2959 | rc = me; Py_INCREF(rc); |
b35fdbe6 | 2960 | end: |
21684137 MW |
2961 | freebin(&in); |
2962 | return (rc); | |
b35fdbe6 MW |
2963 | } |
2964 | ||
df0bd0f9 MW |
2965 | static PyObject *kxvikmeth_set(PyObject *me, PyObject *arg) |
2966 | { | |
2967 | kxvik_pyobj *k = (kxvik_pyobj *)me; | |
2968 | kludge64 t[25]; | |
2969 | const octet *q; | |
2970 | unsigned i; | |
21684137 MW |
2971 | struct bin in = BIN_INIT; |
2972 | PyObject *rc = 0; | |
df0bd0f9 MW |
2973 | size_t n; |
2974 | ||
2975 | if (!PyArg_ParseTuple(arg, "O&:set", convbin, &in)) goto end; | |
2976 | if (in.sz > 200) VALERR("out of range"); | |
2977 | q = in.p; n = in.sz; | |
2978 | i = 0; | |
2979 | while (n >= 8) { LOAD64_L_(t[i], q); i++; q += 8; n -= 8; } | |
2980 | if (n) VALERR("not 64-bit aligned"); | |
2981 | keccak1600_set(&k->s, t, i); | |
21684137 | 2982 | rc = me; Py_INCREF(rc); |
df0bd0f9 | 2983 | end: |
21684137 MW |
2984 | freebin(&in); |
2985 | return (rc); | |
df0bd0f9 MW |
2986 | } |
2987 | ||
b35fdbe6 MW |
2988 | static PyObject *kxvikmeth_extract(PyObject *me, PyObject *arg) |
2989 | { | |
2990 | kxvik_pyobj *k = (kxvik_pyobj *)me; | |
2991 | PyObject *rc = 0; | |
2992 | kludge64 t[25]; | |
2993 | octet *q, buf[8]; | |
2994 | unsigned i; | |
2995 | unsigned n; | |
2996 | ||
ae5dbbad | 2997 | if (!PyArg_ParseTuple(arg, "O&:extract", convuint, &n)) goto end; |
b35fdbe6 MW |
2998 | if (n > 200) VALERR("out of range"); |
2999 | rc = bytestring_pywrap(0, n); | |
2135a6d3 | 3000 | q = (octet *)BIN_PTR(rc); |
b35fdbe6 MW |
3001 | keccak1600_extract(&k->s, t, (n + 7)/8); |
3002 | i = 0; | |
3003 | while (n > 8) { STORE64_L_(q, t[i]); i++; q += 8; n -= 8; } | |
3004 | if (n) { STORE64_L_(buf, t[i]); memcpy(q, buf, n); } | |
3005 | end: | |
3006 | return (rc); | |
3007 | } | |
3008 | ||
91e56f06 | 3009 | static PyObject *kxvikmeth_step(PyObject *me) |
b35fdbe6 MW |
3010 | { |
3011 | kxvik_pyobj *k = (kxvik_pyobj *)me; | |
b35fdbe6 MW |
3012 | keccak1600_p(&k->s, &k->s, k->n); |
3013 | RETURN_ME; | |
3014 | } | |
3015 | ||
8e923c68 MW |
3016 | static const PyMemberDef kxvik_pymembers[] = { |
3017 | #define MEMBERSTRUCT kxvik_pyobj | |
3018 | MEMRNM(nround, T_UINT, n, 0, "KECCAC.nround -> number of rounds") | |
3019 | #undef MEMBERSTRUCT | |
b35fdbe6 MW |
3020 | { 0 } |
3021 | }; | |
3022 | ||
c90f712e | 3023 | static const PyMethodDef kxvik_pymethods[] = { |
b35fdbe6 | 3024 | #define METHNAME(func) kxvikmeth_##func |
91e56f06 | 3025 | NAMETH(copy, "KECCAK.copy() -> KECCAK'") |
d96c882e | 3026 | METH (mix, "KECCAK.mix(DATA)") |
df0bd0f9 | 3027 | METH (set, "KECCAK.set(DATA)") |
d96c882e | 3028 | METH (extract, "KECCAK.extract(NOCTETS)") |
91e56f06 | 3029 | NAMETH(step, "KECCAK.step()") |
b35fdbe6 MW |
3030 | #undef METHNAME |
3031 | { 0 } | |
3032 | }; | |
3033 | ||
c263b05c | 3034 | static const PyTypeObject kxvik_pytype_skel = { |
591bf41b | 3035 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
b35fdbe6 MW |
3036 | "Keccak1600", /* @tp_name@ */ |
3037 | sizeof(kxvik_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@ */ | |
d96c882e | 3059 | "Keccak1600([nround = 24]): Keccak-p[1600, n] state.", |
b35fdbe6 MW |
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@ */ | |
c90f712e | 3067 | PYMETHODS(kxvik), /* @tp_methods@ */ |
8e923c68 MW |
3068 | PYMEMBERS(kxvik), /* @tp_members@ */ |
3069 | 0, /* @tp_getset@ */ | |
b35fdbe6 MW |
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 | kxvik_pynew, /* @tp_new@ */ | |
3078 | 0, /* @tp_free@ */ | |
3079 | 0 /* @tp_is_gc@ */ | |
3080 | }; | |
3081 | ||
6bd22b53 MW |
3082 | static PyTypeObject *shake_pytype, *shake128_pytype, *shake256_pytype; |
3083 | ||
3084 | typedef struct shake_pyobj { | |
3085 | PyObject_HEAD | |
3086 | int st; | |
3087 | shake_ctx h; | |
3088 | } shake_pyobj; | |
3089 | ||
3090 | #define SHAKE_H(o) (&((shake_pyobj *)(o))->h) | |
3091 | #define SHAKE_ST(o) (((shake_pyobj *)(o))->st) | |
3092 | ||
3093 | static PyObject *shake_dopynew(void (*initfn)(shake_ctx *, | |
3094 | const void *, size_t, | |
3095 | const void *, size_t), | |
3096 | PyTypeObject *ty, | |
3097 | PyObject *arg, PyObject *kw) | |
3098 | { | |
3099 | shake_pyobj *rc = 0; | |
04b75d71 | 3100 | PyObject *pobj = Py_None, *fobj = Py_None; |
21684137 | 3101 | struct bin p = BIN_INIT, f = BIN_INIT; |
827f89d7 | 3102 | static const char *const kwlist[] = { "perso", "func", 0 }; |
6bd22b53 | 3103 | |
04b75d71 MW |
3104 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|OO:new", KWLIST, &pobj, &fobj)) |
3105 | goto end; | |
3106 | if (pobj != Py_None && !convbin(pobj, &p)) goto end; | |
3107 | if (fobj != Py_None && !convbin(fobj, &f)) goto end; | |
6bd22b53 | 3108 | rc = (shake_pyobj *)ty->tp_alloc(ty, 0); |
67c75893 | 3109 | initfn(&rc->h, f.p, f.sz, p.p, p.sz); |
6bd22b53 MW |
3110 | rc->st = 0; |
3111 | end: | |
21684137 | 3112 | freebin(&p); freebin(&f); |
6bd22b53 MW |
3113 | return ((PyObject *)rc); |
3114 | } | |
3115 | ||
3116 | static PyObject *shake128_pynew(PyTypeObject *ty, | |
3117 | PyObject *arg, PyObject *kw) | |
3118 | { return (shake_dopynew(cshake128_init, ty, arg, kw)); } | |
3119 | ||
3120 | static PyObject *shake256_pynew(PyTypeObject *ty, | |
3121 | PyObject *arg, PyObject *kw) | |
3122 | { return (shake_dopynew(cshake256_init, ty, arg, kw)); } | |
3123 | ||
3124 | static int shake_check(PyObject *me, int st) | |
3125 | { | |
3126 | if (SHAKE_ST(me) != st) VALERR("wrong state"); | |
3127 | return (0); | |
3128 | end: | |
3129 | return (-1); | |
3130 | } | |
3131 | ||
3132 | static PyObject *shakemeth_hash(PyObject *me, PyObject *arg) | |
3133 | { | |
21684137 MW |
3134 | struct bin m = BIN_INIT; |
3135 | PyObject *rc = 0; | |
3136 | ||
3137 | if (!PyArg_ParseTuple(arg, "O&:hash", convbin, &m)) goto end; | |
3138 | if (shake_check(me, 0)) goto end; | |
67c75893 | 3139 | shake_hash(SHAKE_H(me), m.p, m.sz); |
21684137 MW |
3140 | rc = me; Py_INCREF(rc); |
3141 | end: | |
3142 | freebin(&m); | |
3143 | return (rc); | |
6bd22b53 MW |
3144 | } |
3145 | ||
3146 | #define SHAKEMETH_HASHU_(n, W, w) \ | |
3147 | static PyObject *shakemeth_hashu##w(PyObject *me, PyObject *arg) \ | |
3148 | { \ | |
3149 | uint##n x; \ | |
3150 | octet b[SZ_##W]; \ | |
21684137 | 3151 | \ |
0e5c668c MW |
3152 | if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) return (0); \ |
3153 | if (shake_check(me, 0)) return (0); \ | |
6bd22b53 MW |
3154 | STORE##W(b, x); shake_hash(SHAKE_H(me), b, sizeof(b)); \ |
3155 | RETURN_ME; \ | |
6bd22b53 MW |
3156 | } |
3157 | DOUINTCONV(SHAKEMETH_HASHU_) | |
3158 | ||
3159 | #define SHAKEMETH_HASHBUF_(n, W, w) \ | |
3160 | static PyObject *shakemeth_hashbuf##w(PyObject *me, PyObject *arg) \ | |
3161 | { \ | |
21684137 MW |
3162 | struct bin in = BIN_INIT; \ |
3163 | PyObject *rc = 0; \ | |
6bd22b53 | 3164 | octet b[SZ_##W]; \ |
21684137 | 3165 | \ |
67c75893 | 3166 | if (!PyArg_ParseTuple(arg, "O&:hashbuf" #w, convbin, &in)) goto end; \ |
e01ab3c9 | 3167 | if (in.sz > MASK##n) VALERR("too large"); \ |
6bd22b53 | 3168 | if (shake_check(me, 0)) goto end; \ |
67c75893 MW |
3169 | STORE##W(b, in.sz); shake_hash(SHAKE_H(me), b, sizeof(b)); \ |
3170 | shake_hash(SHAKE_H(me), in.p, in.sz); \ | |
21684137 | 3171 | rc = me; Py_INCREF(rc); \ |
6bd22b53 | 3172 | end: \ |
21684137 MW |
3173 | freebin(&in); \ |
3174 | return (rc); \ | |
6bd22b53 MW |
3175 | } |
3176 | DOUINTCONV(SHAKEMETH_HASHBUF_) | |
3177 | ||
3178 | static PyObject *shakemeth_hashstrz(PyObject *me, PyObject *arg) | |
3179 | { | |
3180 | char *p; | |
21684137 | 3181 | |
6bd22b53 MW |
3182 | if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0); |
3183 | if (shake_check(me, 0)) return (0); | |
3184 | shake_hash(SHAKE_H(me), p, strlen(p) + 1); | |
3185 | RETURN_ME; | |
3186 | } | |
3187 | ||
91e56f06 | 3188 | static PyObject *shakemeth_xof(PyObject *me) |
6bd22b53 | 3189 | { |
6bd22b53 MW |
3190 | if (shake_check(me, 0)) goto end; |
3191 | shake_xof(SHAKE_H(me)); | |
3192 | SHAKE_ST(me) = 1; | |
3193 | RETURN_ME; | |
3194 | end: | |
3195 | return (0); | |
3196 | } | |
3197 | ||
aaf8c7aa | 3198 | static PyObject *shakemeth_done(PyObject *me, PyObject *arg, PyObject *kw) |
6bd22b53 MW |
3199 | { |
3200 | PyObject *rc = 0; | |
aaf8c7aa MW |
3201 | size_t n = 100 - SHAKE_H(me)->h.r/2; |
3202 | static const char *const kwlist[] = { "hsz", 0 }; | |
21684137 | 3203 | |
aaf8c7aa MW |
3204 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:done", KWLIST, convszt, &n)) |
3205 | goto end; | |
6bd22b53 MW |
3206 | if (shake_check(me, 0)) goto end; |
3207 | rc = bytestring_pywrap(0, n); | |
2135a6d3 | 3208 | shake_done(SHAKE_H(me), BIN_PTR(rc), n); |
6bd22b53 MW |
3209 | SHAKE_ST(me) = -1; |
3210 | end: | |
3211 | return (rc); | |
3212 | } | |
3213 | ||
91e56f06 | 3214 | static PyObject *shakemeth_copy(PyObject *me) |
6bd22b53 MW |
3215 | { |
3216 | shake_pyobj *rc = 0; | |
3217 | ||
2a448f86 | 3218 | rc = PyObject_NEW(shake_pyobj, Py_TYPE(me)); |
6bd22b53 MW |
3219 | rc->h = *SHAKE_H(me); |
3220 | rc->st = SHAKE_ST(me); | |
9b5c9816 | 3221 | return ((PyObject *)rc); |
6bd22b53 MW |
3222 | } |
3223 | ||
3224 | static PyObject *shakemeth_get(PyObject *me, PyObject *arg) | |
3225 | { | |
3226 | PyObject *rc = 0; | |
3227 | size_t sz; | |
3228 | ||
3229 | if (!PyArg_ParseTuple(arg, "O&:get", convszt, &sz)) goto end; | |
3230 | if (shake_check(me, 1)) goto end; | |
3231 | rc = bytestring_pywrap(0, sz); | |
2135a6d3 | 3232 | shake_get(SHAKE_H(me), BIN_PTR(rc), sz); |
6bd22b53 MW |
3233 | end: |
3234 | return (rc); | |
3235 | } | |
3236 | ||
3237 | static PyObject *shakemeth_mask(PyObject *me, PyObject *arg) | |
3238 | { | |
3239 | PyObject *rc = 0; | |
21684137 | 3240 | struct bin in = BIN_INIT; |
6bd22b53 | 3241 | |
67c75893 | 3242 | if (!PyArg_ParseTuple(arg, "O&:mask", convbin, &in)) goto end; |
6bd22b53 | 3243 | if (shake_check(me, 1)) goto end; |
67c75893 | 3244 | rc = bytestring_pywrap(0, in.sz); |
2135a6d3 | 3245 | shake_mask(SHAKE_H(me), in.p, BIN_PTR(rc), in.sz); |
6bd22b53 | 3246 | end: |
21684137 | 3247 | freebin(&in); |
6bd22b53 MW |
3248 | return (rc); |
3249 | } | |
3250 | ||
6bd22b53 MW |
3251 | static PyObject *shakeget_state(PyObject *me, void *hunoz) |
3252 | { | |
3253 | int st = SHAKE_ST(me); | |
2135a6d3 MW |
3254 | return (TEXT_FROMSTR(st == 0 ? "absorb" : |
3255 | st == 1 ? "squeeze" : "dead")); | |
6bd22b53 MW |
3256 | } |
3257 | ||
8e923c68 MW |
3258 | static const PyMemberDef shake_pymembers[] = { |
3259 | #define MEMBERSTRUCT shake_pyobj | |
3260 | MEMRNM(rate, T_UINT, h.h.r, READONLY, "S.rate -> rate, in bytes") | |
3261 | MEMRNM(buffered, T_UINT, h.h.n, READONLY, | |
3262 | "S.buffered -> amount currently buffered") | |
3263 | #undef MEMBERSTRUCT | |
3264 | { 0 } | |
3265 | }; | |
3266 | ||
c90f712e | 3267 | static const PyGetSetDef shake_pygetset[] = { |
6bd22b53 | 3268 | #define GETSETNAME(op, name) shake##op##_##name |
d96c882e | 3269 | GET (state, "S.state -> `absorb', `squeeze', `dead'") |
6bd22b53 MW |
3270 | #undef GETSETNAME |
3271 | { 0 } | |
3272 | }; | |
3273 | ||
c90f712e | 3274 | static const PyMethodDef shake_pymethods[] = { |
6bd22b53 | 3275 | #define METHNAME(func) shakemeth_##func |
91e56f06 | 3276 | NAMETH(copy, "S.copy() -> SS") |
d96c882e | 3277 | METH (hash, "S.hash(M)") |
6bd22b53 MW |
3278 | #define METHU_(n, W, w) METH(hashu##w, "S.hashu" #w "(WORD)") |
3279 | DOUINTCONV(METHU_) | |
3280 | #undef METHU_ | |
3281 | #define METHBUF_(n, W, w) METH(hashbuf##w, "S.hashbuf" #w "(BYTES)") | |
3282 | DOUINTCONV(METHBUF_) | |
3283 | #undef METHBUF_ | |
d96c882e | 3284 | METH (hashstrz, "S.hashstrz(STRING)") |
91e56f06 | 3285 | NAMETH(xof, "S.xof()") |
aaf8c7aa | 3286 | KWMETH(done, "S.done([hsz = CAP]) -> H") |
d96c882e MW |
3287 | METH (get, "S.get(LEN) -> H") |
3288 | METH (mask, "S.mask(M) -> C") | |
6bd22b53 MW |
3289 | #undef METHNAME |
3290 | { 0 } | |
3291 | }; | |
3292 | ||
c263b05c | 3293 | static const PyTypeObject shake_pytype_skel = { |
591bf41b | 3294 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
6bd22b53 MW |
3295 | "Shake", /* @tp_name@ */ |
3296 | sizeof(shake_pyobj), /* @tp_basicsize@ */ | |
3297 | 0, /* @tp_itemsize@ */ | |
3298 | ||
3299 | 0, /* @tp_dealloc@ */ | |
3300 | 0, /* @tp_print@ */ | |
3301 | 0, /* @tp_getattr@ */ | |
3302 | 0, /* @tp_setattr@ */ | |
3303 | 0, /* @tp_compare@ */ | |
3304 | 0, /* @tp_repr@ */ | |
3305 | 0, /* @tp_as_number@ */ | |
3306 | 0, /* @tp_as_sequence@ */ | |
3307 | 0, /* @tp_as_mapping@ */ | |
3308 | 0, /* @tp_hash@ */ | |
3309 | 0, /* @tp_call@ */ | |
3310 | 0, /* @tp_str@ */ | |
3311 | 0, /* @tp_getattro@ */ | |
3312 | 0, /* @tp_setattro@ */ | |
3313 | 0, /* @tp_as_buffer@ */ | |
3314 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
3315 | Py_TPFLAGS_BASETYPE, | |
3316 | ||
3317 | /* @tp_doc@ */ | |
d96c882e | 3318 | "SHAKE/cSHAKE/KMAC base class.", |
6bd22b53 MW |
3319 | |
3320 | 0, /* @tp_traverse@ */ | |
3321 | 0, /* @tp_clear@ */ | |
3322 | 0, /* @tp_richcompare@ */ | |
3323 | 0, /* @tp_weaklistoffset@ */ | |
3324 | 0, /* @tp_iter@ */ | |
3325 | 0, /* @tp_iternext@ */ | |
c90f712e | 3326 | PYMETHODS(shake), /* @tp_methods@ */ |
8e923c68 | 3327 | PYMEMBERS(shake), /* @tp_members@ */ |
c90f712e | 3328 | PYGETSET(shake), /* @tp_getset@ */ |
6bd22b53 MW |
3329 | 0, /* @tp_base@ */ |
3330 | 0, /* @tp_dict@ */ | |
3331 | 0, /* @tp_descr_get@ */ | |
3332 | 0, /* @tp_descr_set@ */ | |
3333 | 0, /* @tp_dictoffset@ */ | |
3334 | 0, /* @tp_init@ */ | |
3335 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
3336 | abstract_pynew, /* @tp_new@ */ | |
3337 | 0, /* @tp_free@ */ | |
3338 | 0 /* @tp_is_gc@ */ | |
3339 | }; | |
3340 | ||
c263b05c | 3341 | static const PyTypeObject shake128_pytype_skel = { |
591bf41b | 3342 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
6bd22b53 MW |
3343 | "Shake128", /* @tp_name@ */ |
3344 | 0, /* @tp_basicsize@ */ | |
3345 | 0, /* @tp_itemsize@ */ | |
3346 | ||
3347 | 0, /* @tp_dealloc@ */ | |
3348 | 0, /* @tp_print@ */ | |
3349 | 0, /* @tp_getattr@ */ | |
3350 | 0, /* @tp_setattr@ */ | |
3351 | 0, /* @tp_compare@ */ | |
3352 | 0, /* @tp_repr@ */ | |
3353 | 0, /* @tp_as_number@ */ | |
3354 | 0, /* @tp_as_sequence@ */ | |
3355 | 0, /* @tp_as_mapping@ */ | |
3356 | 0, /* @tp_hash@ */ | |
3357 | 0, /* @tp_call@ */ | |
3358 | 0, /* @tp_str@ */ | |
3359 | 0, /* @tp_getattro@ */ | |
3360 | 0, /* @tp_setattro@ */ | |
3361 | 0, /* @tp_as_buffer@ */ | |
3362 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
3363 | Py_TPFLAGS_BASETYPE, | |
3364 | ||
3365 | /* @tp_doc@ */ | |
d96c882e | 3366 | "Shake128([perso = STR], [func = STR]): SHAKE128/cSHAKE128 XOF.", |
6bd22b53 MW |
3367 | |
3368 | 0, /* @tp_traverse@ */ | |
3369 | 0, /* @tp_clear@ */ | |
3370 | 0, /* @tp_richcompare@ */ | |
3371 | 0, /* @tp_weaklistoffset@ */ | |
3372 | 0, /* @tp_iter@ */ | |
3373 | 0, /* @tp_iternext@ */ | |
3374 | 0, /* @tp_methods@ */ | |
3375 | 0, /* @tp_members@ */ | |
3376 | 0, /* @tp_getset@ */ | |
3377 | 0, /* @tp_base@ */ | |
3378 | 0, /* @tp_dict@ */ | |
3379 | 0, /* @tp_descr_get@ */ | |
3380 | 0, /* @tp_descr_set@ */ | |
3381 | 0, /* @tp_dictoffset@ */ | |
3382 | 0, /* @tp_init@ */ | |
3383 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
3384 | shake128_pynew, /* @tp_new@ */ | |
3385 | 0, /* @tp_free@ */ | |
3386 | 0 /* @tp_is_gc@ */ | |
3387 | }; | |
3388 | ||
c263b05c | 3389 | static const PyTypeObject shake256_pytype_skel = { |
591bf41b | 3390 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
6bd22b53 MW |
3391 | "Shake256", /* @tp_name@ */ |
3392 | 0, /* @tp_basicsize@ */ | |
3393 | 0, /* @tp_itemsize@ */ | |
3394 | ||
3395 | 0, /* @tp_dealloc@ */ | |
3396 | 0, /* @tp_print@ */ | |
3397 | 0, /* @tp_getattr@ */ | |
3398 | 0, /* @tp_setattr@ */ | |
3399 | 0, /* @tp_compare@ */ | |
3400 | 0, /* @tp_repr@ */ | |
3401 | 0, /* @tp_as_number@ */ | |
3402 | 0, /* @tp_as_sequence@ */ | |
3403 | 0, /* @tp_as_mapping@ */ | |
3404 | 0, /* @tp_hash@ */ | |
3405 | 0, /* @tp_call@ */ | |
3406 | 0, /* @tp_str@ */ | |
3407 | 0, /* @tp_getattro@ */ | |
3408 | 0, /* @tp_setattro@ */ | |
3409 | 0, /* @tp_as_buffer@ */ | |
3410 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
3411 | Py_TPFLAGS_BASETYPE, | |
3412 | ||
3413 | /* @tp_doc@ */ | |
d96c882e | 3414 | "Shake256([perso = STR], [func = STR]): SHAKE256/cSHAKE256 XOF.", |
6bd22b53 MW |
3415 | |
3416 | 0, /* @tp_traverse@ */ | |
3417 | 0, /* @tp_clear@ */ | |
3418 | 0, /* @tp_richcompare@ */ | |
3419 | 0, /* @tp_weaklistoffset@ */ | |
3420 | 0, /* @tp_iter@ */ | |
3421 | 0, /* @tp_iternext@ */ | |
3422 | 0, /* @tp_methods@ */ | |
3423 | 0, /* @tp_members@ */ | |
3424 | 0, /* @tp_getset@ */ | |
3425 | 0, /* @tp_base@ */ | |
3426 | 0, /* @tp_dict@ */ | |
3427 | 0, /* @tp_descr_get@ */ | |
3428 | 0, /* @tp_descr_set@ */ | |
3429 | 0, /* @tp_dictoffset@ */ | |
3430 | 0, /* @tp_init@ */ | |
3431 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
3432 | shake256_pynew, /* @tp_new@ */ | |
3433 | 0, /* @tp_free@ */ | |
3434 | 0 /* @tp_is_gc@ */ | |
3435 | }; | |
3436 | ||
668b5f54 MW |
3437 | static PyTypeObject *kmac_pytype, *kmac128_pytype, *kmac256_pytype; |
3438 | ||
3439 | static PyObject *kmac_dopynew(void (*initfn)(shake_ctx *, | |
3440 | const void *, size_t, | |
3441 | const void *, size_t), | |
3442 | PyTypeObject *ty, | |
3443 | PyObject *arg, PyObject *kw) | |
3444 | { | |
3445 | shake_pyobj *rc = 0; | |
3446 | PyObject *pobj = Py_None; | |
21684137 | 3447 | struct bin k = BIN_INIT, p = BIN_INIT; |
668b5f54 MW |
3448 | static const char *const kwlist[] = { "key", "perso", 0 }; |
3449 | ||
3450 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O:new", KWLIST, | |
3451 | convbin, &k, &pobj)) | |
3452 | goto end; | |
3453 | if (pobj != Py_None && !convbin(pobj, &p)) goto end; | |
3454 | rc = (shake_pyobj *)ty->tp_alloc(ty, 0); | |
3455 | initfn(&rc->h, p.p, p.sz, k.p, k.sz); | |
3456 | rc->st = 0; | |
3457 | end: | |
21684137 | 3458 | freebin(&k); freebin(&p); |
668b5f54 MW |
3459 | return ((PyObject *)rc); |
3460 | } | |
3461 | ||
3462 | static PyObject *kmac128_pynew(PyTypeObject *ty, | |
3463 | PyObject *arg, PyObject *kw) | |
3464 | { return (kmac_dopynew(kmac128_init, ty, arg, kw)); } | |
3465 | ||
3466 | static PyObject *kmac256_pynew(PyTypeObject *ty, | |
3467 | PyObject *arg, PyObject *kw) | |
3468 | { return (kmac_dopynew(kmac256_init, ty, arg, kw)); } | |
3469 | ||
3470 | static PyObject *kmacmeth_xof(PyObject *me) | |
3471 | { | |
3472 | if (shake_check(me, 0)) goto end; | |
3473 | kmac_xof(SHAKE_H(me)); | |
3474 | SHAKE_ST(me) = 1; | |
3475 | RETURN_ME; | |
3476 | end: | |
3477 | return (0); | |
3478 | } | |
3479 | ||
3480 | static PyObject *kmacmeth_done(PyObject *me, PyObject *arg, PyObject *kw) | |
3481 | { | |
3482 | PyObject *rc = 0; | |
3483 | size_t n = 100 - SHAKE_H(me)->h.r/2; | |
3484 | static const char *const kwlist[] = { "hsz", 0 }; | |
3485 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:done", KWLIST, convszt, &n)) | |
3486 | goto end; | |
3487 | if (shake_check(me, 0)) goto end; | |
3488 | rc = bytestring_pywrap(0, n); | |
3489 | kmac_done(SHAKE_H(me), BIN_PTR(rc), n); | |
3490 | SHAKE_ST(me) = -1; | |
3491 | end: | |
3492 | return (rc); | |
3493 | } | |
3494 | ||
3495 | static const PyMethodDef kmac_pymethods[] = { | |
3496 | #define METHNAME(func) kmacmeth_##func | |
3497 | NAMETH(xof, "K.xof()") | |
3498 | KWMETH(done, "K.done([hsz = CAP/2]) -> T") | |
3499 | #undef METHNAME | |
3500 | { 0 } | |
3501 | }; | |
3502 | ||
3503 | static const PyTypeObject kmac_pytype_skel = { | |
3504 | PyVarObject_HEAD_INIT(0, 0) /* Header */ | |
3505 | "KMAC", /* @tp_name@ */ | |
3506 | sizeof(shake_pyobj), /* @tp_basicsize@ */ | |
3507 | 0, /* @tp_itemsize@ */ | |
3508 | ||
3509 | 0, /* @tp_dealloc@ */ | |
3510 | 0, /* @tp_print@ */ | |
3511 | 0, /* @tp_getattr@ */ | |
3512 | 0, /* @tp_setattr@ */ | |
3513 | 0, /* @tp_compare@ */ | |
3514 | 0, /* @tp_repr@ */ | |
3515 | 0, /* @tp_as_number@ */ | |
3516 | 0, /* @tp_as_sequence@ */ | |
3517 | 0, /* @tp_as_mapping@ */ | |
3518 | 0, /* @tp_hash@ */ | |
3519 | 0, /* @tp_call@ */ | |
3520 | 0, /* @tp_str@ */ | |
3521 | 0, /* @tp_getattro@ */ | |
3522 | 0, /* @tp_setattro@ */ | |
3523 | 0, /* @tp_as_buffer@ */ | |
3524 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
3525 | Py_TPFLAGS_BASETYPE, | |
3526 | ||
3527 | /* @tp_doc@ */ | |
3528 | "KMAC base class.", | |
3529 | ||
3530 | 0, /* @tp_traverse@ */ | |
3531 | 0, /* @tp_clear@ */ | |
3532 | 0, /* @tp_richcompare@ */ | |
3533 | 0, /* @tp_weaklistoffset@ */ | |
3534 | 0, /* @tp_iter@ */ | |
3535 | 0, /* @tp_iternext@ */ | |
3536 | PYMETHODS(kmac), /* @tp_methods@ */ | |
3537 | 0, /* @tp_members@ */ | |
3538 | 0, /* @tp_getset@ */ | |
3539 | 0, /* @tp_base@ */ | |
3540 | 0, /* @tp_dict@ */ | |
3541 | 0, /* @tp_descr_get@ */ | |
3542 | 0, /* @tp_descr_set@ */ | |
3543 | 0, /* @tp_dictoffset@ */ | |
3544 | 0, /* @tp_init@ */ | |
3545 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
3546 | abstract_pynew, /* @tp_new@ */ | |
3547 | 0, /* @tp_free@ */ | |
3548 | 0 /* @tp_is_gc@ */ | |
3549 | }; | |
3550 | ||
3551 | static const PyTypeObject kmac128_pytype_skel = { | |
3552 | PyVarObject_HEAD_INIT(0, 0) /* Header */ | |
3553 | "KMAC128", /* @tp_name@ */ | |
3554 | 0, /* @tp_basicsize@ */ | |
3555 | 0, /* @tp_itemsize@ */ | |
3556 | ||
3557 | 0, /* @tp_dealloc@ */ | |
3558 | 0, /* @tp_print@ */ | |
3559 | 0, /* @tp_getattr@ */ | |
3560 | 0, /* @tp_setattr@ */ | |
3561 | 0, /* @tp_compare@ */ | |
3562 | 0, /* @tp_repr@ */ | |
3563 | 0, /* @tp_as_number@ */ | |
3564 | 0, /* @tp_as_sequence@ */ | |
3565 | 0, /* @tp_as_mapping@ */ | |
3566 | 0, /* @tp_hash@ */ | |
3567 | 0, /* @tp_call@ */ | |
3568 | 0, /* @tp_str@ */ | |
3569 | 0, /* @tp_getattro@ */ | |
3570 | 0, /* @tp_setattro@ */ | |
3571 | 0, /* @tp_as_buffer@ */ | |
3572 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
3573 | Py_TPFLAGS_BASETYPE, | |
3574 | ||
3575 | /* @tp_doc@ */ | |
3576 | "KMAC128(KEY, [perso = STR]): KMAC XOMAC.", | |
3577 | ||
3578 | 0, /* @tp_traverse@ */ | |
3579 | 0, /* @tp_clear@ */ | |
3580 | 0, /* @tp_richcompare@ */ | |
3581 | 0, /* @tp_weaklistoffset@ */ | |
3582 | 0, /* @tp_iter@ */ | |
3583 | 0, /* @tp_iternext@ */ | |
3584 | 0, /* @tp_methods@ */ | |
3585 | 0, /* @tp_members@ */ | |
3586 | 0, /* @tp_getset@ */ | |
3587 | 0, /* @tp_base@ */ | |
3588 | 0, /* @tp_dict@ */ | |
3589 | 0, /* @tp_descr_get@ */ | |
3590 | 0, /* @tp_descr_set@ */ | |
3591 | 0, /* @tp_dictoffset@ */ | |
3592 | 0, /* @tp_init@ */ | |
3593 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
3594 | kmac128_pynew, /* @tp_new@ */ | |
3595 | 0, /* @tp_free@ */ | |
3596 | 0 /* @tp_is_gc@ */ | |
3597 | }; | |
3598 | ||
3599 | static const PyTypeObject kmac256_pytype_skel = { | |
3600 | PyVarObject_HEAD_INIT(0, 0) /* Header */ | |
3601 | "KMAC256", /* @tp_name@ */ | |
3602 | 0, /* @tp_basicsize@ */ | |
3603 | 0, /* @tp_itemsize@ */ | |
3604 | ||
3605 | 0, /* @tp_dealloc@ */ | |
3606 | 0, /* @tp_print@ */ | |
3607 | 0, /* @tp_getattr@ */ | |
3608 | 0, /* @tp_setattr@ */ | |
3609 | 0, /* @tp_compare@ */ | |
3610 | 0, /* @tp_repr@ */ | |
3611 | 0, /* @tp_as_number@ */ | |
3612 | 0, /* @tp_as_sequence@ */ | |
3613 | 0, /* @tp_as_mapping@ */ | |
3614 | 0, /* @tp_hash@ */ | |
3615 | 0, /* @tp_call@ */ | |
3616 | 0, /* @tp_str@ */ | |
3617 | 0, /* @tp_getattro@ */ | |
3618 | 0, /* @tp_setattro@ */ | |
3619 | 0, /* @tp_as_buffer@ */ | |
3620 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
3621 | Py_TPFLAGS_BASETYPE, | |
3622 | ||
3623 | /* @tp_doc@ */ | |
3624 | "KMAC256(KEY, [perso = STR]): KMAC XOMAC.", | |
3625 | ||
3626 | 0, /* @tp_traverse@ */ | |
3627 | 0, /* @tp_clear@ */ | |
3628 | 0, /* @tp_richcompare@ */ | |
3629 | 0, /* @tp_weaklistoffset@ */ | |
3630 | 0, /* @tp_iter@ */ | |
3631 | 0, /* @tp_iternext@ */ | |
3632 | 0, /* @tp_methods@ */ | |
3633 | 0, /* @tp_members@ */ | |
3634 | 0, /* @tp_getset@ */ | |
3635 | 0, /* @tp_base@ */ | |
3636 | 0, /* @tp_dict@ */ | |
3637 | 0, /* @tp_descr_get@ */ | |
3638 | 0, /* @tp_descr_set@ */ | |
3639 | 0, /* @tp_dictoffset@ */ | |
3640 | 0, /* @tp_init@ */ | |
3641 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
3642 | kmac256_pynew, /* @tp_new@ */ | |
3643 | 0, /* @tp_free@ */ | |
3644 | 0 /* @tp_is_gc@ */ | |
3645 | }; | |
3646 | ||
a29f5426 MW |
3647 | static PyTypeObject *strobe_pytype; |
3648 | ||
3649 | typedef struct strobe_pyobj { | |
3650 | PyObject_HEAD | |
3651 | strobe_ctx ctx; | |
3652 | } strobe_pyobj; | |
3653 | #define STROBE_CTX(o) (&((strobe_pyobj *)(o))->ctx) | |
3654 | ||
3655 | static int convstrbf(PyObject *x, void *p) | |
3656 | { | |
3657 | unsigned *ff = p, f; | |
3658 | const char *q; | |
3659 | size_t sz; | |
3660 | int rc = 0; | |
3661 | ||
3662 | if (TEXT_CHECK(x)) { | |
3663 | TEXT_PTRLEN(x, q, sz); f = 0; | |
3664 | while (sz--) switch (*q++) { | |
3665 | case 'I': f |= STRBF_I; break; | |
3666 | case 'A': f |= STRBF_A; break; | |
3667 | case 'C': f |= STRBF_C; break; | |
3668 | case 'T': f |= STRBF_T; break; | |
3669 | case 'M': f |= STRBF_M; break; | |
3670 | default: if (!ISSPACE(q[-1])) VALERR("unknown flag character"); | |
3671 | } | |
3672 | *ff = f; rc = 1; | |
3673 | } else if (convuint(x, ff)) | |
3674 | rc = 1; | |
3675 | else { | |
3676 | PyErr_Clear(); | |
3677 | TYERR("expected string or int"); | |
3678 | } | |
3679 | end: | |
3680 | return (rc); | |
3681 | } | |
3682 | ||
3683 | ||
3684 | static PyObject *strobe_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
3685 | { | |
3686 | strobe_pyobj *rc = 0; | |
3687 | unsigned lambda = 128; | |
3688 | static const char *const kwlist[] = { "l", 0 }; | |
3689 | ||
3690 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", KWLIST, | |
3691 | convuint, &lambda)) | |
3692 | goto end; | |
3693 | if (lambda%32 || lambda > 704) VALERR("invalid security parameter"); | |
3694 | rc = (strobe_pyobj *)ty->tp_alloc(ty, 0); | |
3695 | strobe_init(&rc->ctx, lambda); | |
3696 | end: | |
3697 | return ((PyObject *)rc); | |
3698 | } | |
3699 | ||
3700 | static PyObject *strobemeth_copy(PyObject *me) | |
3701 | { | |
3702 | strobe_pyobj *strobe = (strobe_pyobj *)me, *rc; | |
3703 | ||
3704 | rc = PyObject_NEW(strobe_pyobj, me->ob_type); | |
3705 | rc->ctx = strobe->ctx; | |
3706 | return ((PyObject *)rc); | |
3707 | } | |
3708 | ||
3709 | static int strobe_checkinactive(PyObject *me) | |
3710 | { | |
3711 | int rc = -1; | |
3712 | ||
3713 | if (STROBE_CTX(me)->f&STRBF_ACTIVE) VALERR("operation already active"); | |
3714 | rc = 0; | |
3715 | end: | |
3716 | return (rc); | |
3717 | } | |
3718 | ||
3719 | static int strobe_checkactive(PyObject *me) | |
3720 | { | |
3721 | int rc = -1; | |
3722 | ||
3723 | if (!(STROBE_CTX(me)->f&STRBF_ACTIVE)) VALERR("operation not active"); | |
3724 | rc = 0; | |
3725 | end: | |
3726 | return (rc); | |
3727 | } | |
3728 | ||
3729 | static PyObject *strobemeth_begin(PyObject *me, PyObject *arg) | |
3730 | { | |
3731 | unsigned f; | |
3732 | ||
3733 | if (!PyArg_ParseTuple(arg, "O&:begin", convstrbf, &f)) goto end; | |
3734 | if (strobe_checkinactive(me)) goto end; | |
3735 | if (f&~STRBF_VALIDMASK) VALERR("bad flags"); | |
3736 | strobe_begin(STROBE_CTX(me), f); | |
3737 | RETURN_ME; | |
3738 | end: | |
3739 | return (0); | |
3740 | } | |
3741 | ||
3742 | static PyObject *strobe_pyprocess(PyObject *me, | |
3743 | const void *p0, size_t sz0, | |
3744 | const void *p1, size_t sz1) | |
3745 | { | |
3746 | char *q; | |
3747 | PyObject *rc = 0; | |
3748 | ||
3749 | if (strobe_checkactive(me)) goto end; | |
3750 | if (!(STROBE_CTX(me)->f&STRBF_WANTIN) && p0) | |
3751 | VALERR("no input expected for current operation"); | |
3752 | if (!(STROBE_CTX(me)->f&STRBF_WANTOUT)) q = 0; | |
3753 | else { rc = bytestring_pywrap(0, sz0 + sz1); q = BIN_PTR(rc); } | |
3754 | strobe_process(STROBE_CTX(me), p0, q, sz0); | |
3755 | if (sz1) strobe_process(STROBE_CTX(me), p1, q ? q + sz0 : 0, sz1); | |
3756 | if (!q) RETURN_ME; | |
3757 | end: | |
3758 | return (rc); | |
3759 | } | |
3760 | ||
3761 | static PyObject *strobemeth_process(PyObject *me, PyObject *arg) | |
3762 | { | |
3763 | PyObject *inobj; | |
21684137 | 3764 | struct bin in = BIN_INIT; |
a29f5426 MW |
3765 | PyObject *rc = 0; |
3766 | ||
3767 | in.p = 0; | |
3768 | if (!PyArg_ParseTuple(arg, "O:process", &inobj)) goto end; | |
3769 | if (!convszt(inobj, &in.sz) && (PyErr_Clear(), !convbin(inobj, &in))) { | |
3770 | PyErr_Clear(); | |
3771 | TYERR("expected integer, string, unicode, or single-segment buffer"); | |
3772 | } | |
3773 | rc = strobe_pyprocess(me, in.p, in.sz, 0, 0); | |
3774 | end: | |
21684137 | 3775 | freebin(&in); |
a29f5426 MW |
3776 | return (rc); |
3777 | } | |
3778 | ||
3779 | static PyObject *strobemeth_done(PyObject *me) | |
3780 | { | |
3781 | unsigned f; | |
3782 | int rc; | |
3783 | ||
3784 | if (strobe_checkactive(me)) return (0); | |
3785 | f = STROBE_CTX(me)->f; rc = strobe_done(STROBE_CTX(me)); | |
3786 | if (f&STRBF_VRFOUT) return (getbool(!rc)); | |
3787 | else RETURN_ME; | |
3788 | } | |
3789 | ||
3790 | #define STROBEMETH_PROCESSU_(n, W, w) \ | |
3791 | static PyObject *strobemeth_processu##w(PyObject *me, PyObject *arg) \ | |
3792 | { \ | |
3793 | uint##n x; \ | |
3794 | octet b[SZ_##W]; \ | |
21684137 | 3795 | \ |
a29f5426 MW |
3796 | if (!PyArg_ParseTuple(arg, "O&:processu" #w, convu##n, &x)) return (0); \ |
3797 | STORE##W(b, x); return (strobe_pyprocess(me, b, sizeof(b), 0, 0)); \ | |
3798 | } | |
3799 | DOUINTCONV(STROBEMETH_PROCESSU_) | |
3800 | ||
3801 | #define STROBEMETH_PROCESSBUF_(n, W, w) \ | |
3802 | static PyObject *strobemeth_processbuf##w(PyObject *me, PyObject *arg) \ | |
3803 | { \ | |
21684137 | 3804 | struct bin in = BIN_INIT; \ |
a29f5426 | 3805 | octet b[SZ_##W]; \ |
21684137 MW |
3806 | PyObject *rc = 0; \ |
3807 | \ | |
a29f5426 MW |
3808 | if (!PyArg_ParseTuple(arg, "O&:processbuf" #w, convbin, &in)) goto end; \ |
3809 | if (in.sz > MASK##n) VALERR("too large"); \ | |
3810 | STORE##W(b, in.sz); \ | |
21684137 | 3811 | rc = strobe_pyprocess(me, b, sizeof(b), in.p, in.sz); \ |
a29f5426 | 3812 | end: \ |
21684137 MW |
3813 | freebin(&in); \ |
3814 | return (rc); \ | |
a29f5426 MW |
3815 | } |
3816 | DOUINTCONV(STROBEMETH_PROCESSBUF_) | |
3817 | ||
3818 | static PyObject *strobemeth_processstrz(PyObject *me, PyObject *arg) | |
3819 | { | |
3820 | char *p; | |
3821 | if (!PyArg_ParseTuple(arg, "s:processstrz", &p)) return (0); | |
3822 | return (strobe_pyprocess(me, p, strlen(p) + 1, 0, 0)); | |
3823 | } | |
3824 | ||
3825 | #define STROBEMETH_INONLY_(meth, arg) \ | |
3826 | static PyObject *strobemeth_##meth(PyObject *me, \ | |
3827 | PyObject *arg, PyObject *kw) \ | |
3828 | { \ | |
21684137 MW |
3829 | struct bin in = BIN_INIT; unsigned f = 0; \ |
3830 | PyObject *rc = 0; \ | |
a29f5426 | 3831 | static const char *const kwlist[] = { #arg, "f", 0 }; \ |
21684137 | 3832 | \ |
a29f5426 MW |
3833 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:" #meth, KWLIST, \ |
3834 | convbin, &in, convstrbf, &f)) \ | |
3835 | goto end; \ | |
3836 | if (f&~STRBF_M) VALERR("bad flags"); \ | |
3837 | if (strobe_checkinactive(me)) goto end; \ | |
3838 | strobe_##meth(STROBE_CTX(me), f, in.p, in.sz); \ | |
21684137 | 3839 | rc = me; Py_INCREF(rc); \ |
a29f5426 | 3840 | end: \ |
21684137 MW |
3841 | freebin(&in); \ |
3842 | return (rc); \ | |
a29f5426 MW |
3843 | } |
3844 | ||
3845 | #define STROBEMETH_OUTONLY_(meth) \ | |
3846 | static PyObject *strobemeth_##meth(PyObject *me, \ | |
3847 | PyObject *arg, PyObject *kw) \ | |
3848 | { \ | |
3849 | size_t sz; unsigned f = 0; PyObject *rc; \ | |
3850 | static const char *const kwlist[] = { "sz", "f", 0 }; \ | |
21684137 | 3851 | \ |
a29f5426 MW |
3852 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:" #meth, KWLIST, \ |
3853 | convszt, &sz, convstrbf, &f)) \ | |
3854 | goto end; \ | |
3855 | if (f&~STRBF_M) VALERR("bad flags"); \ | |
3856 | if (strobe_checkinactive(me)) goto end; \ | |
3857 | rc = bytestring_pywrap(0, sz); \ | |
3858 | strobe_##meth(STROBE_CTX(me), f, BIN_PTR(rc), sz); \ | |
a29f5426 | 3859 | end: \ |
21684137 | 3860 | return (rc); \ |
a29f5426 MW |
3861 | } |
3862 | ||
3863 | #define STROBEMETH_INOUT_(meth, arg) \ | |
3864 | static PyObject *strobemeth_##meth(PyObject *me, \ | |
3865 | PyObject *arg, PyObject *kw) \ | |
3866 | { \ | |
21684137 | 3867 | struct bin in = BIN_INIT; unsigned f = 0; PyObject *rc; \ |
a29f5426 | 3868 | static const char *const kwlist[] = { #arg, "f", 0 }; \ |
21684137 | 3869 | \ |
a29f5426 MW |
3870 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:" #meth, KWLIST, \ |
3871 | convbin, &in, convstrbf, &f)) \ | |
3872 | goto end; \ | |
3873 | if (f&~STRBF_M) VALERR("bad flags"); \ | |
3874 | if (strobe_checkinactive(me)) goto end; \ | |
3875 | rc = bytestring_pywrap(0, in.sz); \ | |
3876 | strobe_##meth(STROBE_CTX(me), f, in.p, BIN_PTR(rc), in.sz); \ | |
a29f5426 | 3877 | end: \ |
21684137 MW |
3878 | freebin(&in); \ |
3879 | return (rc); \ | |
a29f5426 MW |
3880 | } |
3881 | ||
3882 | STROBEMETH_INONLY_(key, key) | |
3883 | STROBEMETH_INONLY_(ad, msg) | |
3884 | STROBEMETH_OUTONLY_(prf) | |
3885 | STROBEMETH_INONLY_(clrout, msg) | |
3886 | STROBEMETH_INONLY_(clrin, msg) | |
3887 | STROBEMETH_INOUT_(encout, msg) | |
3888 | STROBEMETH_INOUT_(encin, ct) | |
3889 | STROBEMETH_OUTONLY_(macout) | |
3890 | ||
3891 | static PyObject *strobemeth_macin(PyObject *me, PyObject *arg, PyObject *kw) | |
3892 | { | |
21684137 MW |
3893 | struct bin in = BIN_INIT; unsigned f = 0; |
3894 | PyObject *rc = 0; | |
a29f5426 | 3895 | static const char *const kwlist[] = { "tag", "f", 0 }; |
21684137 | 3896 | |
a29f5426 MW |
3897 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:macin", KWLIST, |
3898 | convbin, &in, convstrbf, &f)) | |
3899 | goto end; | |
3900 | if (f&~STRBF_M) VALERR("bad flags"); | |
3901 | if (strobe_checkinactive(me)) goto end; | |
21684137 | 3902 | rc = getbool(!strobe_macin(STROBE_CTX(me), f, in.p, in.sz)); |
a29f5426 | 3903 | end: |
21684137 MW |
3904 | freebin(&in); |
3905 | return (rc); | |
a29f5426 MW |
3906 | } |
3907 | ||
3908 | static PyObject *strobemeth_ratchet(PyObject *me, | |
3909 | PyObject *arg, PyObject *kw) | |
3910 | { | |
3911 | size_t sz; unsigned f = 0; | |
3912 | static const char *const kwlist[] = { "sz", "f", 0 }; | |
21684137 | 3913 | |
a29f5426 MW |
3914 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:ratchet", KWLIST, |
3915 | convszt, &sz, convstrbf, &f)) | |
3916 | goto end; | |
3917 | if (f&~STRBF_M) VALERR("bad flags"); | |
3918 | if (strobe_checkinactive(me)) goto end; | |
3919 | strobe_ratchet(STROBE_CTX(me), f, sz); RETURN_ME; | |
3920 | end: | |
3921 | return (0); | |
3922 | } | |
3923 | ||
3924 | static PyObject *strobeget_l(PyObject *me, void *hunoz) | |
3925 | { return (PyInt_FromLong(4*(200 - STROBE_CTX(me)->r))); } | |
3926 | ||
3927 | static PyObject *strobeget_role(PyObject *me, void *hunoz) | |
3928 | { return (PyInt_FromLong(STROBE_CTX(me)->f&STROBE_ROLEMASK)); } | |
3929 | ||
3930 | static PyObject *strobeget_activep(PyObject *me, void *hunoz) | |
3931 | { return (getbool(STROBE_CTX(me)->f&STRBF_ACTIVE)); } | |
3932 | ||
3933 | static const PyGetSetDef strobe_pygetset[] = { | |
3934 | #define GETSETNAME(op, name) strobe##op##_##name | |
3935 | GET (l, "STROBE.l -> security parameter") | |
3936 | GET (role, "STROBE.role -> STRBRL_...") | |
3937 | GET (activep, "STROBE.activep -> operation active?") | |
3938 | #undef GETSETNAME | |
3939 | { 0 } | |
3940 | }; | |
3941 | ||
3942 | static const PyMethodDef strobe_pymethods[] = { | |
3943 | #define METHNAME(func) strobemeth_##func | |
3944 | NAMETH(copy, "S.copy() -> SS") | |
3945 | METH (begin, "S.begin(MSGTY)") | |
3946 | METH (process, "S.process(MSG | SZ) [-> OUT]") | |
3947 | #define METHU_(n, W, w) \ | |
3948 | METH(processu##w, "S.processu" #w "(WORD) [-> OUT]") | |
3949 | DOUINTCONV(METHU_) | |
3950 | #undef METHU_ | |
3951 | #define METHBUF_(n, W, w) \ | |
3952 | METH(processbuf##w, "S.processbuf" #w "(BYTES) [-> OUT]") | |
3953 | DOUINTCONV(METHBUF_) | |
3954 | #undef METHBUF_ | |
3955 | METH (processstrz, "S.processstrz(STRING) [-> OUT]") | |
3956 | NAMETH(done, "S.done() [-> OK?]") | |
3957 | KWMETH(key, "S.key(KEY, [f = 0])") | |
3958 | KWMETH(ad, "S.ad(MSG, [f = 0])") | |
3959 | KWMETH(prf, "S.prf(SZ, [f = 0]) -> BYTES") | |
3960 | KWMETH(clrout, "S.clrout(MSG, [f = 0])") | |
3961 | KWMETH(clrin, "S.clrin(MSG, [f = 0])") | |
3962 | KWMETH(encout, "S.encout(MSG, [f = 0]) -> CT") | |
3963 | KWMETH(encin, "S.encin(CT, [f = 0]) -> MSG") | |
3964 | KWMETH(macout, "S.macout(SZ, [f = 0]) -> TAG") | |
3965 | KWMETH(macin, "S.macin(TAG, [f = 0]) -> OK?") | |
3966 | KWMETH(ratchet, "S.ratchet(SZ, [f = 0])") | |
3967 | #undef METHNAME | |
3968 | { 0 } | |
3969 | }; | |
3970 | ||
3971 | static const PyTypeObject strobe_pytype_skel = { | |
3972 | PyVarObject_HEAD_INIT(0, 0) /* Header */ | |
3973 | "Strobe", /* @tp_name@ */ | |
3974 | sizeof(strobe_pyobj), /* @tp_basicsize@ */ | |
3975 | 0, /* @tp_itemsize@ */ | |
3976 | ||
3977 | 0, /* @tp_dealloc@ */ | |
3978 | 0, /* @tp_print@ */ | |
3979 | 0, /* @tp_getattr@ */ | |
3980 | 0, /* @tp_setattr@ */ | |
3981 | 0, /* @tp_compare@ */ | |
3982 | 0, /* @tp_repr@ */ | |
3983 | 0, /* @tp_as_number@ */ | |
3984 | 0, /* @tp_as_sequence@ */ | |
3985 | 0, /* @tp_as_mapping@ */ | |
3986 | 0, /* @tp_hash@ */ | |
3987 | 0, /* @tp_call@ */ | |
3988 | 0, /* @tp_str@ */ | |
3989 | 0, /* @tp_getattro@ */ | |
3990 | 0, /* @tp_setattro@ */ | |
3991 | 0, /* @tp_as_buffer@ */ | |
3992 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
3993 | Py_TPFLAGS_BASETYPE, | |
3994 | ||
3995 | /* @tp_doc@ */ | |
3996 | "STROBE symmetric-crypto framework.", | |
3997 | ||
3998 | 0, /* @tp_traverse@ */ | |
3999 | 0, /* @tp_clear@ */ | |
4000 | 0, /* @tp_richcompare@ */ | |
4001 | 0, /* @tp_weaklistoffset@ */ | |
4002 | 0, /* @tp_iter@ */ | |
4003 | 0, /* @tp_iternext@ */ | |
4004 | PYMETHODS(strobe), /* @tp_methods@ */ | |
4005 | 0, /* @tp_members@ */ | |
4006 | PYGETSET(strobe), /* @tp_getset@ */ | |
4007 | 0, /* @tp_base@ */ | |
4008 | 0, /* @tp_dict@ */ | |
4009 | 0, /* @tp_descr_get@ */ | |
4010 | 0, /* @tp_descr_set@ */ | |
4011 | 0, /* @tp_dictoffset@ */ | |
4012 | 0, /* @tp_init@ */ | |
4013 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
4014 | strobe_pynew, /* @tp_new@ */ | |
4015 | 0, /* @tp_free@ */ | |
4016 | 0 /* @tp_is_gc@ */ | |
4017 | }; | |
4018 | ||
03ed9abb MW |
4019 | /*----- Pseudorandom permutations -----------------------------------------*/ |
4020 | ||
4021 | static PyTypeObject *gcprp_pytype, *gprp_pytype; | |
4022 | ||
4023 | typedef struct prpinfo { | |
4024 | const char *name; | |
4025 | const octet *keysz; | |
4026 | size_t ctxsz; | |
4027 | size_t blksz; | |
4028 | void (*init)(void *, const void *, size_t); | |
4029 | void (*eblk)(void *, const void *, void *); | |
4030 | void (*dblk)(void *, const void *, void *); | |
4031 | } prpinfo; | |
4032 | ||
4033 | #define PRP_DEF(PRE, pre) \ | |
4034 | static void pre##_prpinit(void *ctx, const void *k, size_t ksz) \ | |
4035 | { pre##_init(ctx, k, ksz); } \ | |
4036 | static void pre##_prpeblk(void *ctx, const void *in, void *out) \ | |
4037 | { \ | |
4038 | uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \ | |
4039 | pre##_eblk(ctx, w, w); BLKC_STORE(PRE, out, w); \ | |
4040 | } \ | |
4041 | static void pre##_prpdblk(void *ctx, const void *in, void *out) \ | |
4042 | { \ | |
4043 | uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \ | |
4044 | pre##_dblk(ctx, w, w); BLKC_STORE(PRE, out, w); \ | |
4045 | } \ | |
4046 | static const prpinfo pre##_prpinfo = { \ | |
4047 | #pre, pre##_keysz, sizeof(pre##_ctx), PRE##_BLKSZ, \ | |
4048 | pre##_prpinit, pre##_prpeblk, pre##_prpdblk \ | |
4049 | }; | |
4050 | PRPS(PRP_DEF) | |
4051 | ||
4052 | static const struct prpinfo *const gprptab[] = { | |
4053 | #define PRP_ENTRY(PRE, pre) &pre##_prpinfo, | |
4054 | PRPS(PRP_ENTRY) | |
4055 | 0 | |
b2687a0a | 4056 | }; |
03ed9abb MW |
4057 | |
4058 | typedef struct gcprp_pyobj { | |
4059 | PyHeapTypeObject ty; | |
4060 | const prpinfo *prp; | |
4061 | } gcprp_pyobj; | |
4062 | #define GCPRP_PRP(o) (((gcprp_pyobj *)(o))->prp) | |
4063 | ||
4064 | typedef struct gprp_pyobj { | |
4065 | PyObject_HEAD | |
4066 | const prpinfo *prp; | |
4067 | } gprp_pyobj; | |
4068 | #define GPRP_PRP(o) (((gprp_pyobj *)(o))->prp) | |
4069 | #define GPRP_CTX(o) (((gprp_pyobj *)(o)) + 1) | |
4070 | ||
4071 | typedef struct prp { | |
4072 | const prpinfo *prp; | |
4073 | void *ctx; | |
4074 | } prp; | |
4075 | ||
4076 | static PyObject *gprp_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) | |
4077 | { | |
827f89d7 | 4078 | static const char *const kwlist[] = { "key", 0 }; |
21684137 | 4079 | struct bin k = BIN_INIT; |
03ed9abb | 4080 | const prpinfo *prp = GCPRP_PRP(ty); |
0e5b95a8 | 4081 | PyObject *rc = 0; |
03ed9abb | 4082 | |
67c75893 | 4083 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", KWLIST, convbin, &k)) |
03ed9abb | 4084 | goto end; |
67c75893 | 4085 | if (keysz(k.sz, prp->keysz) != k.sz) VALERR("bad key length"); |
0e5b95a8 | 4086 | rc = (PyObject *)ty->tp_alloc(ty, 0); |
03ed9abb | 4087 | GPRP_PRP(me) = prp; |
67c75893 | 4088 | prp->init(GPRP_CTX(me), k.p, k.sz); |
03ed9abb | 4089 | end: |
21684137 | 4090 | freebin(&k); |
0e5b95a8 | 4091 | return (rc); |
03ed9abb MW |
4092 | } |
4093 | ||
4094 | static void gprp_pydealloc(PyObject *me) | |
2a448f86 | 4095 | { Py_DECREF(Py_TYPE(me)); FREEOBJ(me); } |
03ed9abb MW |
4096 | |
4097 | static PyObject *gcprp_pywrap(const prpinfo *prp) | |
4098 | { | |
4099 | gcprp_pyobj *g = newtype(gcprp_pytype, 0, prp->name); | |
21684137 | 4100 | |
03ed9abb | 4101 | g->prp = prp; |
24b3d57b MW |
4102 | g->ty.ht_type.tp_basicsize = sizeof(gprp_pyobj) + prp->ctxsz; |
4103 | g->ty.ht_type.tp_base = gprp_pytype; | |
03ed9abb | 4104 | Py_INCREF(gprp_pytype); |
24b3d57b MW |
4105 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
4106 | Py_TPFLAGS_BASETYPE | | |
4107 | Py_TPFLAGS_HEAPTYPE); | |
4108 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; | |
4109 | g->ty.ht_type.tp_free = 0; | |
4110 | g->ty.ht_type.tp_new = gprp_pynew; | |
dc075750 | 4111 | typeready(&g->ty.ht_type); |
03ed9abb MW |
4112 | return ((PyObject *)g); |
4113 | } | |
4114 | ||
4115 | static PyObject *gcpget_name(PyObject *me, void *hunoz) | |
2135a6d3 | 4116 | { return (TEXT_FROMSTR(GCPRP_PRP(me)->name)); } |
03ed9abb MW |
4117 | static PyObject *gcpget_keysz(PyObject *me, void *hunoz) |
4118 | { return (keysz_pywrap(GCPRP_PRP(me)->keysz)); } | |
4119 | static PyObject *gcpget_blksz(PyObject *me, void *hunoz) | |
4120 | { return (PyInt_FromLong(GCPRP_PRP(me)->blksz)); } | |
4121 | ||
4122 | static PyObject *gpmeth_encrypt(PyObject *me, PyObject *arg) | |
4123 | { | |
21684137 | 4124 | struct bin m = BIN_INIT; |
03ed9abb MW |
4125 | PyObject *rc = 0; |
4126 | ||
67c75893 MW |
4127 | if (!PyArg_ParseTuple(arg, "O&:encrypt", convbin, &m)) goto end; |
4128 | if (m.sz != GPRP_PRP(me)->blksz) VALERR("incorrect block length"); | |
4129 | rc = bytestring_pywrap(0, m.sz); | |
2135a6d3 | 4130 | GPRP_PRP(me)->eblk(GPRP_CTX(me), m.p, BIN_PTR(rc)); |
03ed9abb | 4131 | end: |
21684137 | 4132 | freebin(&m); |
03ed9abb MW |
4133 | return (rc); |
4134 | } | |
4135 | ||
4136 | static PyObject *gpmeth_decrypt(PyObject *me, PyObject *arg) | |
4137 | { | |
21684137 | 4138 | struct bin c = BIN_INIT; |
03ed9abb MW |
4139 | PyObject *rc = 0; |
4140 | ||
67c75893 MW |
4141 | if (!PyArg_ParseTuple(arg, "O&:decrypt", convbin, &c)) goto end; |
4142 | if (c.sz != GPRP_PRP(me)->blksz) VALERR("incorrect block length"); | |
4143 | rc = bytestring_pywrap(0, c.sz); | |
2135a6d3 | 4144 | GPRP_PRP(me)->dblk(GPRP_CTX(me), c.p, BIN_PTR(rc)); |
03ed9abb | 4145 | end: |
21684137 | 4146 | freebin(&c); |
03ed9abb MW |
4147 | return (rc); |
4148 | } | |
4149 | ||
c90f712e | 4150 | static const PyGetSetDef gcprp_pygetset[] = { |
03ed9abb | 4151 | #define GETSETNAME(op, name) gcp##op##_##name |
d96c882e MW |
4152 | GET (keysz, "CP.keysz -> acceptable key sizes") |
4153 | GET (blksz, "CP.blksz -> block size") | |
4154 | GET (name, "CP.name -> name of this kind of PRP") | |
03ed9abb MW |
4155 | #undef GETSETNAME |
4156 | { 0 } | |
4157 | }; | |
4158 | ||
c90f712e | 4159 | static const PyMethodDef gprp_pymethods[] = { |
03ed9abb | 4160 | #define METHNAME(name) gpmeth_##name |
d96c882e MW |
4161 | METH (encrypt, "P.encrypt(PT) -> CT") |
4162 | METH (decrypt, "P.decrypt(CT) -> PT") | |
03ed9abb MW |
4163 | #undef METHNAME |
4164 | { 0 } | |
4165 | }; | |
4166 | ||
c263b05c | 4167 | static const PyTypeObject gcprp_pytype_skel = { |
591bf41b | 4168 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
c461c9b3 | 4169 | "GCPRP", /* @tp_name@ */ |
03ed9abb MW |
4170 | sizeof(gcprp_pyobj), /* @tp_basicsize@ */ |
4171 | 0, /* @tp_itemsize@ */ | |
4172 | ||
4173 | 0, /* @tp_dealloc@ */ | |
4174 | 0, /* @tp_print@ */ | |
4175 | 0, /* @tp_getattr@ */ | |
4176 | 0, /* @tp_setattr@ */ | |
4177 | 0, /* @tp_compare@ */ | |
4178 | 0, /* @tp_repr@ */ | |
4179 | 0, /* @tp_as_number@ */ | |
4180 | 0, /* @tp_as_sequence@ */ | |
4181 | 0, /* @tp_as_mapping@ */ | |
4182 | 0, /* @tp_hash@ */ | |
4183 | 0, /* @tp_call@ */ | |
4184 | 0, /* @tp_str@ */ | |
4185 | 0, /* @tp_getattro@ */ | |
4186 | 0, /* @tp_setattro@ */ | |
4187 | 0, /* @tp_as_buffer@ */ | |
4188 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
4189 | Py_TPFLAGS_BASETYPE, | |
4190 | ||
4191 | /* @tp_doc@ */ | |
d96c882e | 4192 | "Pseudorandom permutation metaclass.", |
03ed9abb MW |
4193 | |
4194 | 0, /* @tp_traverse@ */ | |
4195 | 0, /* @tp_clear@ */ | |
4196 | 0, /* @tp_richcompare@ */ | |
4197 | 0, /* @tp_weaklistoffset@ */ | |
4198 | 0, /* @tp_iter@ */ | |
4199 | 0, /* @tp_iternext@ */ | |
4200 | 0, /* @tp_methods@ */ | |
4201 | 0, /* @tp_members@ */ | |
c90f712e | 4202 | PYGETSET(gcprp), /* @tp_getset@ */ |
03ed9abb MW |
4203 | 0, /* @tp_base@ */ |
4204 | 0, /* @tp_dict@ */ | |
4205 | 0, /* @tp_descr_get@ */ | |
4206 | 0, /* @tp_descr_set@ */ | |
4207 | 0, /* @tp_dictoffset@ */ | |
4208 | 0, /* @tp_init@ */ | |
4209 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
4210 | abstract_pynew, /* @tp_new@ */ | |
4211 | 0, /* @tp_free@ */ | |
4212 | 0 /* @tp_is_gc@ */ | |
4213 | }; | |
4214 | ||
c263b05c | 4215 | static const PyTypeObject gprp_pytype_skel = { |
591bf41b | 4216 | PyVarObject_HEAD_INIT(0, 0) /* Header */ |
c461c9b3 | 4217 | "GPRP", /* @tp_name@ */ |
03ed9abb MW |
4218 | sizeof(gprp_pyobj), /* @tp_basicsize@ */ |
4219 | 0, /* @tp_itemsize@ */ | |
4220 | ||
4221 | gprp_pydealloc, /* @tp_dealloc@ */ | |
4222 | 0, /* @tp_print@ */ | |
4223 | 0, /* @tp_getattr@ */ | |
4224 | 0, /* @tp_setattr@ */ | |
4225 | 0, /* @tp_compare@ */ | |
4226 | 0, /* @tp_repr@ */ | |
4227 | 0, /* @tp_as_number@ */ | |
4228 | 0, /* @tp_as_sequence@ */ | |
4229 | 0, /* @tp_as_mapping@ */ | |
4230 | 0, /* @tp_hash@ */ | |
4231 | 0, /* @tp_call@ */ | |
4232 | 0, /* @tp_str@ */ | |
4233 | 0, /* @tp_getattro@ */ | |
4234 | 0, /* @tp_setattro@ */ | |
4235 | 0, /* @tp_as_buffer@ */ | |
4236 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ | |
4237 | Py_TPFLAGS_BASETYPE, | |
4238 | ||
4239 | /* @tp_doc@ */ | |
d96c882e | 4240 | "Pseudorandom permutation, abstract base class.", |
03ed9abb MW |
4241 | |
4242 | 0, /* @tp_traverse@ */ | |
4243 | 0, /* @tp_clear@ */ | |
4244 | 0, /* @tp_richcompare@ */ | |
4245 | 0, /* @tp_weaklistoffset@ */ | |
4246 | 0, /* @tp_iter@ */ | |
4247 | 0, /* @tp_iternext@ */ | |
c90f712e | 4248 | PYMETHODS(gprp), /* @tp_methods@ */ |
03ed9abb MW |
4249 | 0, /* @tp_members@ */ |
4250 | 0, /* @tp_getset@ */ | |
4251 | 0, /* @tp_base@ */ | |
4252 | 0, /* @tp_dict@ */ | |
4253 | 0, /* @tp_descr_get@ */ | |
4254 | 0, /* @tp_descr_set@ */ | |
4255 | 0, /* @tp_dictoffset@ */ | |
4256 | 0, /* @tp_init@ */ | |
4257 | PyType_GenericAlloc, /* @tp_alloc@ */ | |
4258 | abstract_pynew, /* @tp_new@ */ | |
4259 | 0, /* @tp_free@ */ | |
4260 | 0 /* @tp_is_gc@ */ | |
4261 | }; | |
4262 | ||
d7ab1bab | 4263 | /*----- Main code ---------------------------------------------------------*/ |
4264 | ||
810542b0 MW |
4265 | static const struct nameval consts[] = { |
4266 | CONST(AEADF_PCHSZ), CONST(AEADF_PCMSZ), CONST(AEADF_PCTSZ), | |
4267 | CONST(AEADF_AADNDEP), CONST(AEADF_AADFIRST), CONST(AEADF_NOAAD), | |
a29f5426 MW |
4268 | CONST(STRBF_I), CONST(STRBF_A), CONST(STRBF_C), CONST(STRBF_T), |
4269 | CONST(STRBF_M), CONST(STROBE_KEY), CONST(STROBE_AD), CONST(STROBE_PRF), | |
4270 | CONST(STROBE_CLROUT), CONST(STROBE_CLRIN), CONST(STROBE_ENCOUT), | |
4271 | CONST(STROBE_ENCIN), CONST(STRBRL_UNDCD), CONST(STRBRL_INIT), | |
4272 | CONST(STRBRL_RESP), | |
810542b0 MW |
4273 | { 0 } |
4274 | }; | |
4275 | ||
c90f712e | 4276 | static const PyMethodDef methods[] = { |
89157adc | 4277 | #define METHNAME(func) meth_##func |
d96c882e MW |
4278 | #define METH_HDANCE(hdance, HDance) METH(hdance##_prf, \ |
4279 | "" #hdance "_prf(K, N) -> H: calculate " HDance " hash of N with K") | |
a75e68c9 MW |
4280 | METH_HDANCE(hsalsa20, "HSalsa20") |
4281 | METH_HDANCE(hsalsa2012, "HSalsa20/12") | |
4282 | METH_HDANCE(hsalsa208, "HSalsa20/8") | |
4283 | METH_HDANCE(hchacha20, "HChaCha20") | |
4284 | METH_HDANCE(hchacha12, "HChaCha12") | |
4285 | METH_HDANCE(hchacha8, "HChaCha8") | |
4286 | #undef METH_DANCE | |
89157adc MW |
4287 | #undef METHNAME |
4288 | { 0 } | |
4289 | }; | |
4290 | ||
d7ab1bab | 4291 | void algorithms_pyinit(void) |
4292 | { | |
4293 | INITTYPE(keysz, root); | |
4294 | INITTYPE(keyszany, keysz); | |
4295 | INITTYPE(keyszrange, keysz); | |
4296 | INITTYPE(keyszset, keysz); | |
4297 | INITTYPE(gccipher, type); | |
4298 | INITTYPE(gcipher, root); | |
bebf03ab MW |
4299 | INITTYPE(gcaead, type); |
4300 | INITTYPE(gaeadkey, root); | |
4301 | INITTYPE(gcaeadaad, type); | |
4302 | INITTYPE(gaeadaad, root); | |
4303 | INITTYPE(gcaeadenc, type); | |
4304 | INITTYPE(gaeadenc, root); | |
4305 | INITTYPE(gcaeaddec, type); | |
4306 | INITTYPE(gaeaddec, root); | |
d7ab1bab | 4307 | INITTYPE(gchash, type); |
4308 | INITTYPE(ghash, root); | |
4309 | INITTYPE(gcmac, type); | |
4310 | INITTYPE(gmac, type); | |
4311 | INITTYPE(gmhash, ghash); | |
204d480b MW |
4312 | INITTYPE(poly1305cls, type); |
4313 | INITTYPE_META(poly1305key, type, poly1305cls); | |
4314 | INITTYPE(poly1305hash, root); | |
b35fdbe6 | 4315 | INITTYPE(kxvik, root); |
6bd22b53 MW |
4316 | INITTYPE(shake, root); |
4317 | INITTYPE(shake128, shake); | |
4318 | INITTYPE(shake256, shake); | |
668b5f54 MW |
4319 | INITTYPE(kmac, shake); |
4320 | INITTYPE(kmac128, kmac); | |
4321 | INITTYPE(kmac256, kmac); | |
a29f5426 | 4322 | INITTYPE(strobe, root); |
03ed9abb MW |
4323 | INITTYPE(gcprp, type); |
4324 | INITTYPE(gprp, root); | |
89157adc | 4325 | addmethods(methods); |
d7ab1bab | 4326 | } |
4327 | ||
03ed9abb | 4328 | #define gcprp prpinfo |
a539ffb8 MW |
4329 | #define CLASS_TABLES(_) _(cipher) _(aead) _(hash) _(mac) _(prp) |
4330 | #define TABLE_FNS(pre) \ | |
4331 | static const char *pre##_namefn(const void *p) \ | |
4332 | { const gc##pre *const *cls = p; return (*cls ? (*cls)->name : 0); } \ | |
4333 | static PyObject *pre##_valfn(const void *p) \ | |
4334 | { gc##pre *const*cls = p; return (gc##pre##_pywrap(*cls)); } | |
4335 | CLASS_TABLES(TABLE_FNS) | |
d7ab1bab | 4336 | |
4337 | void algorithms_pyinsert(PyObject *mod) | |
4338 | { | |
4339 | PyObject *d; | |
4340 | INSERT("KeySZ", keysz_pytype); | |
4341 | INSERT("KeySZAny", keyszany_pytype); | |
4342 | INSERT("KeySZRange", keyszrange_pytype); | |
4343 | INSERT("KeySZSet", keyszset_pytype); | |
4344 | INSERT("GCCipher", gccipher_pytype); | |
4345 | INSERT("GCipher", gcipher_pytype); | |
a539ffb8 MW |
4346 | INSERT("gcciphers", make_algtab(gciphertab, sizeof(gccipher *), |
4347 | cipher_namefn, cipher_valfn)); | |
bebf03ab MW |
4348 | INSERT("GCAEAD", gcaead_pytype); |
4349 | INSERT("GAEKey", gaeadkey_pytype); | |
4350 | INSERT("GAEAADClass", gcaeadaad_pytype); | |
4351 | INSERT("GAEAAD", gaeadaad_pytype); | |
4352 | INSERT("GAEEncClass", gcaeadenc_pytype); | |
4353 | INSERT("GAEEnc", gaeadenc_pytype); | |
4354 | INSERT("GAEDecClass", gcaeaddec_pytype); | |
4355 | INSERT("GAEDec", gaeaddec_pytype); | |
a539ffb8 MW |
4356 | INSERT("gcaeads", make_algtab(gaeadtab, sizeof(gcaead *), |
4357 | aead_namefn, aead_valfn)); | |
d7ab1bab | 4358 | INSERT("GCHash", gchash_pytype); |
4359 | INSERT("GHash", ghash_pytype); | |
a539ffb8 MW |
4360 | d = make_algtab(ghashtab, sizeof(gchash *), hash_namefn, hash_valfn); |
4361 | INSERT("gchashes", d); | |
4362 | sha_pyobj = PyMapping_GetItemString(d, "sha"); Py_INCREF(sha_pyobj); | |
4363 | has160_pyobj = PyMapping_GetItemString(d, "has160"); Py_INCREF(has160_pyobj); | |
d7ab1bab | 4364 | INSERT("GCMAC", gcmac_pytype); |
4365 | INSERT("GMAC", gmac_pytype); | |
4366 | INSERT("GMACHash", gmhash_pytype); | |
a539ffb8 MW |
4367 | INSERT("gcmacs", make_algtab(gmactab, sizeof(gcmac *), |
4368 | mac_namefn, mac_valfn)); | |
2edb633e | 4369 | INSERT("_Poly1305Class", poly1305cls_pytype); |
204d480b MW |
4370 | INSERT("poly1305", poly1305key_pytype); |
4371 | INSERT("Poly1305Hash", poly1305hash_pytype); | |
b35fdbe6 | 4372 | INSERT("Keccak1600", kxvik_pytype); |
6bd22b53 MW |
4373 | INSERT("Shake", shake_pytype); |
4374 | INSERT("Shake128", shake128_pytype); | |
4375 | INSERT("Shake256", shake256_pytype); | |
668b5f54 MW |
4376 | INSERT("KMAC", kmac_pytype); |
4377 | INSERT("KMAC128", kmac128_pytype); | |
4378 | INSERT("KMAC256", kmac256_pytype); | |
a29f5426 | 4379 | INSERT("Strobe", strobe_pytype); |
03ed9abb MW |
4380 | INSERT("GCPRP", gcprp_pytype); |
4381 | INSERT("GPRP", gprp_pytype); | |
a539ffb8 MW |
4382 | INSERT("gcprps", make_algtab(gprptab, sizeof(gcprp *), |
4383 | prp_namefn, prp_valfn)); | |
810542b0 | 4384 | setconstants(mod, consts); |
d7ab1bab | 4385 | } |
4386 | ||
4387 | /*----- That's all, folks -------------------------------------------------*/ |