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