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