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