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