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