| 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 | #include "algorithms.h" |
| 31 | |
| 32 | /*----- Key sizes ---------------------------------------------------------*/ |
| 33 | |
| 34 | PyTypeObject *keysz_pytype; |
| 35 | PyTypeObject *keyszany_pytype, *keyszrange_pytype, *keyszset_pytype; |
| 36 | PyObject *sha_pyobj, *has160_pyobj; |
| 37 | |
| 38 | #ifndef KSZ_OPMASK |
| 39 | # define KSZ_OPMASK 0x1f |
| 40 | #endif |
| 41 | |
| 42 | #ifndef KSZ_16BIT |
| 43 | # define KSZ_16BIT 0x20 |
| 44 | #endif |
| 45 | |
| 46 | PyObject *keysz_pywrap(const octet *k) |
| 47 | { |
| 48 | unsigned op = *k++; |
| 49 | #define ARG(i) (op&KSZ_16BIT ? LOAD16(k + 2*(i)) : k[i]) |
| 50 | switch (op&KSZ_OPMASK) { |
| 51 | case KSZ_ANY: { |
| 52 | keysz_pyobj *o = PyObject_New(keysz_pyobj, keyszany_pytype); |
| 53 | o->dfl = ARG(0); |
| 54 | return ((PyObject *)o); |
| 55 | } break; |
| 56 | case KSZ_RANGE: { |
| 57 | keyszrange_pyobj *o = |
| 58 | PyObject_New(keyszrange_pyobj, keyszrange_pytype); |
| 59 | o->dfl = ARG(0); |
| 60 | o->min = ARG(1); |
| 61 | o->max = ARG(2); |
| 62 | o->mod = ARG(3); |
| 63 | if (!o->mod) o->mod = 1; |
| 64 | return ((PyObject *)o); |
| 65 | } break; |
| 66 | case KSZ_SET: { |
| 67 | keyszset_pyobj *o = |
| 68 | PyObject_New(keyszset_pyobj, keyszset_pytype); |
| 69 | int i, n; |
| 70 | o->dfl = ARG(0); |
| 71 | for (i = 0; ARG(i); i++) ; |
| 72 | n = i; o->set = PyTuple_New(n); |
| 73 | for (i = 0; i < n; i++) |
| 74 | PyTuple_SET_ITEM(o->set, i, PyInt_FromLong(ARG(i))); |
| 75 | return ((PyObject *)o); |
| 76 | } break; |
| 77 | default: |
| 78 | abort(); |
| 79 | } |
| 80 | #undef ARG |
| 81 | } |
| 82 | |
| 83 | static PyObject *keyszany_pynew(PyTypeObject *ty, |
| 84 | PyObject *arg, PyObject *kw) |
| 85 | { |
| 86 | char *kwlist[] = { "default", 0 }; |
| 87 | int dfl; |
| 88 | keysz_pyobj *o; |
| 89 | |
| 90 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "i:new", kwlist, &dfl)) |
| 91 | goto end; |
| 92 | if (dfl < 0) VALERR("key size cannot be negative"); |
| 93 | o = (keysz_pyobj *)ty->tp_alloc(ty, 0); |
| 94 | o->dfl = dfl; |
| 95 | return ((PyObject *)o); |
| 96 | end: |
| 97 | return (0); |
| 98 | } |
| 99 | |
| 100 | static PyObject *keyszrange_pynew(PyTypeObject *ty, |
| 101 | PyObject *arg, PyObject *kw) |
| 102 | { |
| 103 | char *kwlist[] = { "default", "min", "max", "mod", 0 }; |
| 104 | int dfl, min = 0, max = 0, mod = 1; |
| 105 | keyszrange_pyobj *o; |
| 106 | |
| 107 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|iii:new", kwlist, |
| 108 | &dfl, &min, &max, &mod)) |
| 109 | goto end; |
| 110 | if (dfl < 0 || min < 0 || max < 0) |
| 111 | VALERR("key size cannot be negative"); |
| 112 | if (min > dfl || (max && dfl > max)) |
| 113 | VALERR("bad key size bounds"); |
| 114 | if (mod <= 0 || dfl % mod || min % mod || max % mod) |
| 115 | VALERR("bad key size modulus"); |
| 116 | o = (keyszrange_pyobj *)ty->tp_alloc(ty, 0); |
| 117 | o->dfl = dfl; |
| 118 | o->min = min; |
| 119 | o->max = max; |
| 120 | o->mod = mod; |
| 121 | return ((PyObject *)o); |
| 122 | end: |
| 123 | return (0); |
| 124 | } |
| 125 | |
| 126 | static PyObject *keyszset_pynew(PyTypeObject *ty, |
| 127 | PyObject *arg, PyObject *kw) |
| 128 | { |
| 129 | char *kwlist[] = { "default", "set", 0 }; |
| 130 | int dfl, i, n, xx; |
| 131 | PyObject *set = 0; |
| 132 | PyObject *x = 0, *l = 0; |
| 133 | keyszset_pyobj *o = 0; |
| 134 | |
| 135 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|O:new", kwlist, |
| 136 | &dfl, &set)) |
| 137 | goto end; |
| 138 | if (!set) set = PyTuple_New(0); |
| 139 | else Py_INCREF(set); |
| 140 | if (!PySequence_Check(set)) TYERR("want a sequence"); |
| 141 | n = PySequence_Size(set); |
| 142 | l = PyList_New(0); |
| 143 | if (PyErr_Occurred()) goto end; |
| 144 | if (dfl < 0) VALERR("key size cannot be negative"); |
| 145 | x = PyInt_FromLong(dfl); |
| 146 | PyList_Append(l, x); |
| 147 | Py_DECREF(x); |
| 148 | x = 0; |
| 149 | for (i = 0; i < n; i++) { |
| 150 | if ((x = PySequence_GetItem(set, i)) == 0) goto end; |
| 151 | xx = PyInt_AsLong(x); |
| 152 | if (PyErr_Occurred()) goto end; |
| 153 | if (xx == dfl) continue; |
| 154 | if (xx < 0) VALERR("key size cannot be negative"); |
| 155 | PyList_Append(l, x); |
| 156 | Py_DECREF(x); |
| 157 | x = 0; |
| 158 | } |
| 159 | Py_DECREF(set); |
| 160 | if ((set = PySequence_Tuple(l)) == 0) goto end; |
| 161 | o = (keyszset_pyobj *)ty->tp_alloc(ty, 0); |
| 162 | o->dfl = dfl; |
| 163 | o->set = set; |
| 164 | Py_INCREF(set); |
| 165 | end: |
| 166 | Py_XDECREF(set); |
| 167 | Py_XDECREF(l); |
| 168 | Py_XDECREF(x); |
| 169 | return ((PyObject *)o); |
| 170 | } |
| 171 | |
| 172 | static PyObject *kaget_min(PyObject *me, void *hunoz) |
| 173 | { return (PyInt_FromLong(0)); } |
| 174 | #define kaget_max kaget_min |
| 175 | |
| 176 | static PyObject *ksget_min(PyObject *me, void *hunoz) |
| 177 | { |
| 178 | PyObject *set = ((keyszset_pyobj *)me)->set; |
| 179 | int i, n, y, x = -1; |
| 180 | n = PyTuple_Size(set); |
| 181 | for (i = 0; i < n; i++) { |
| 182 | y = PyInt_AsLong(PyTuple_GetItem(set, i)); |
| 183 | if (x == -1 || y < x) x = y; |
| 184 | } |
| 185 | return (PyInt_FromLong(x)); |
| 186 | } |
| 187 | |
| 188 | static PyObject *ksget_max(PyObject *me, void *hunoz) |
| 189 | { |
| 190 | PyObject *set = ((keyszset_pyobj *)me)->set; |
| 191 | int i, n, y, x = -1; |
| 192 | n = PyTuple_Size(set); |
| 193 | for (i = 0; i < n; i++) { |
| 194 | y = PyInt_AsLong(PyTuple_GetItem(set, i)); |
| 195 | if (y > x) x = y; |
| 196 | } |
| 197 | return (PyInt_FromLong(x)); |
| 198 | } |
| 199 | |
| 200 | static PyMemberDef keysz_pymembers[] = { |
| 201 | #define MEMBERSTRUCT keysz_pyobj |
| 202 | #define default dfl /* ugh! */ |
| 203 | MEMBER(default, T_INT, READONLY, "KSZ.default -> default key size") |
| 204 | #undef default |
| 205 | #undef MEMBERSTRUCT |
| 206 | { 0 } |
| 207 | }; |
| 208 | |
| 209 | static PyGetSetDef keyszany_pygetset[] = { |
| 210 | #define GETSETNAME(op, name) ka##op##_##name |
| 211 | GET (min, "KSZ.min -> smallest allowed key size") |
| 212 | GET (max, "KSZ.min -> largest allowed key size") |
| 213 | #undef GETSETNAME |
| 214 | { 0 } |
| 215 | }; |
| 216 | |
| 217 | static PyMemberDef keyszrange_pymembers[] = { |
| 218 | #define MEMBERSTRUCT keyszrange_pyobj |
| 219 | MEMBER(min, T_INT, READONLY, "KSZ.min -> smallest allowed key size") |
| 220 | MEMBER(max, T_INT, READONLY, "KSZ.min -> largest allowed key size") |
| 221 | MEMBER(mod, T_INT, READONLY, |
| 222 | "KSZ.mod -> key size must be a multiple of this") |
| 223 | #undef MEMBERSTRUCT |
| 224 | { 0 } |
| 225 | }; |
| 226 | |
| 227 | static PyGetSetDef keyszset_pygetset[] = { |
| 228 | #define GETSETNAME(op, name) ks##op##_##name |
| 229 | GET (min, "KSZ.min -> smallest allowed key size") |
| 230 | GET (max, "KSZ.min -> largest allowed key size") |
| 231 | #undef GETSETNAME |
| 232 | { 0 } |
| 233 | }; |
| 234 | |
| 235 | static PyMemberDef keyszset_pymembers[] = { |
| 236 | #define MEMBERSTRUCT keyszset_pyobj |
| 237 | MEMBER(set, T_OBJECT, READONLY, "KSZ.set -> allowed key sizes") |
| 238 | #undef MEMBERSTRUCT |
| 239 | { 0 } |
| 240 | }; |
| 241 | |
| 242 | static PyTypeObject keysz_pytype_skel = { |
| 243 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 244 | "KeySZ", /* @tp_name@ */ |
| 245 | sizeof(keysz_pyobj), /* @tp_basicsize@ */ |
| 246 | 0, /* @tp_itemsize@ */ |
| 247 | |
| 248 | 0, /* @tp_dealloc@ */ |
| 249 | 0, /* @tp_print@ */ |
| 250 | 0, /* @tp_getattr@ */ |
| 251 | 0, /* @tp_setattr@ */ |
| 252 | 0, /* @tp_compare@ */ |
| 253 | 0, /* @tp_repr@ */ |
| 254 | 0, /* @tp_as_number@ */ |
| 255 | 0, /* @tp_as_sequence@ */ |
| 256 | 0, /* @tp_as_mapping@ */ |
| 257 | 0, /* @tp_hash@ */ |
| 258 | 0, /* @tp_call@ */ |
| 259 | 0, /* @tp_str@ */ |
| 260 | 0, /* @tp_getattro@ */ |
| 261 | 0, /* @tp_setattro@ */ |
| 262 | 0, /* @tp_as_buffer@ */ |
| 263 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 264 | Py_TPFLAGS_BASETYPE, |
| 265 | |
| 266 | /* @tp_doc@ */ |
| 267 | "Key size constraints.", |
| 268 | |
| 269 | 0, /* @tp_traverse@ */ |
| 270 | 0, /* @tp_clear@ */ |
| 271 | 0, /* @tp_richcompare@ */ |
| 272 | 0, /* @tp_weaklistoffset@ */ |
| 273 | 0, /* @tp_iter@ */ |
| 274 | 0, /* @tp_iternext@ */ |
| 275 | 0, /* @tp_methods@ */ |
| 276 | keysz_pymembers, /* @tp_members@ */ |
| 277 | 0, /* @tp_getset@ */ |
| 278 | 0, /* @tp_base@ */ |
| 279 | 0, /* @tp_dict@ */ |
| 280 | 0, /* @tp_descr_get@ */ |
| 281 | 0, /* @tp_descr_set@ */ |
| 282 | 0, /* @tp_dictoffset@ */ |
| 283 | 0, /* @tp_init@ */ |
| 284 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 285 | abstract_pynew, /* @tp_new@ */ |
| 286 | 0, /* @tp_free@ */ |
| 287 | 0 /* @tp_is_gc@ */ |
| 288 | }; |
| 289 | |
| 290 | static PyTypeObject keyszany_pytype_skel = { |
| 291 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 292 | "KeySZAny", /* @tp_name@ */ |
| 293 | sizeof(keysz_pyobj), /* @tp_basicsize@ */ |
| 294 | 0, /* @tp_itemsize@ */ |
| 295 | |
| 296 | 0, /* @tp_dealloc@ */ |
| 297 | 0, /* @tp_print@ */ |
| 298 | 0, /* @tp_getattr@ */ |
| 299 | 0, /* @tp_setattr@ */ |
| 300 | 0, /* @tp_compare@ */ |
| 301 | 0, /* @tp_repr@ */ |
| 302 | 0, /* @tp_as_number@ */ |
| 303 | 0, /* @tp_as_sequence@ */ |
| 304 | 0, /* @tp_as_mapping@ */ |
| 305 | 0, /* @tp_hash@ */ |
| 306 | 0, /* @tp_call@ */ |
| 307 | 0, /* @tp_str@ */ |
| 308 | 0, /* @tp_getattro@ */ |
| 309 | 0, /* @tp_setattro@ */ |
| 310 | 0, /* @tp_as_buffer@ */ |
| 311 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 312 | Py_TPFLAGS_BASETYPE, |
| 313 | |
| 314 | /* @tp_doc@ */ |
| 315 | "Key size constraints. This object imposes no constraints on size.", |
| 316 | |
| 317 | 0, /* @tp_traverse@ */ |
| 318 | 0, /* @tp_clear@ */ |
| 319 | 0, /* @tp_richcompare@ */ |
| 320 | 0, /* @tp_weaklistoffset@ */ |
| 321 | 0, /* @tp_iter@ */ |
| 322 | 0, /* @tp_iternext@ */ |
| 323 | 0, /* @tp_methods@ */ |
| 324 | 0, /* @tp_members@ */ |
| 325 | keyszany_pygetset, /* @tp_getset@ */ |
| 326 | 0, /* @tp_base@ */ |
| 327 | 0, /* @tp_dict@ */ |
| 328 | 0, /* @tp_descr_get@ */ |
| 329 | 0, /* @tp_descr_set@ */ |
| 330 | 0, /* @tp_dictoffset@ */ |
| 331 | 0, /* @tp_init@ */ |
| 332 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 333 | keyszany_pynew, /* @tp_new@ */ |
| 334 | 0, /* @tp_free@ */ |
| 335 | 0 /* @tp_is_gc@ */ |
| 336 | }; |
| 337 | |
| 338 | static PyTypeObject keyszrange_pytype_skel = { |
| 339 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 340 | "KeySZRange", /* @tp_name@ */ |
| 341 | sizeof(keyszrange_pyobj), /* @tp_basicsize@ */ |
| 342 | 0, /* @tp_itemsize@ */ |
| 343 | |
| 344 | 0, /* @tp_dealloc@ */ |
| 345 | 0, /* @tp_print@ */ |
| 346 | 0, /* @tp_getattr@ */ |
| 347 | 0, /* @tp_setattr@ */ |
| 348 | 0, /* @tp_compare@ */ |
| 349 | 0, /* @tp_repr@ */ |
| 350 | 0, /* @tp_as_number@ */ |
| 351 | 0, /* @tp_as_sequence@ */ |
| 352 | 0, /* @tp_as_mapping@ */ |
| 353 | 0, /* @tp_hash@ */ |
| 354 | 0, /* @tp_call@ */ |
| 355 | 0, /* @tp_str@ */ |
| 356 | 0, /* @tp_getattro@ */ |
| 357 | 0, /* @tp_setattro@ */ |
| 358 | 0, /* @tp_as_buffer@ */ |
| 359 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 360 | Py_TPFLAGS_BASETYPE, |
| 361 | |
| 362 | /* @tp_doc@ */ |
| 363 | "Key size constraints. This object asserts minimum and maximum (if\n\ |
| 364 | sizes, and requires the key length to be a multiple of some value.", |
| 365 | |
| 366 | 0, /* @tp_traverse@ */ |
| 367 | 0, /* @tp_clear@ */ |
| 368 | 0, /* @tp_richcompare@ */ |
| 369 | 0, /* @tp_weaklistoffset@ */ |
| 370 | 0, /* @tp_iter@ */ |
| 371 | 0, /* @tp_iternext@ */ |
| 372 | 0, /* @tp_methods@ */ |
| 373 | keyszrange_pymembers, /* @tp_members@ */ |
| 374 | 0, /* @tp_getset@ */ |
| 375 | 0, /* @tp_base@ */ |
| 376 | 0, /* @tp_dict@ */ |
| 377 | 0, /* @tp_descr_get@ */ |
| 378 | 0, /* @tp_descr_set@ */ |
| 379 | 0, /* @tp_dictoffset@ */ |
| 380 | 0, /* @tp_init@ */ |
| 381 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 382 | keyszrange_pynew, /* @tp_new@ */ |
| 383 | 0, /* @tp_free@ */ |
| 384 | 0 /* @tp_is_gc@ */ |
| 385 | }; |
| 386 | |
| 387 | static PyTypeObject keyszset_pytype_skel = { |
| 388 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 389 | "KeySZSet", /* @tp_name@ */ |
| 390 | sizeof(keyszset_pyobj), /* @tp_basicsize@ */ |
| 391 | 0, /* @tp_itemsize@ */ |
| 392 | |
| 393 | 0, /* @tp_dealloc@ */ |
| 394 | 0, /* @tp_print@ */ |
| 395 | 0, /* @tp_getattr@ */ |
| 396 | 0, /* @tp_setattr@ */ |
| 397 | 0, /* @tp_compare@ */ |
| 398 | 0, /* @tp_repr@ */ |
| 399 | 0, /* @tp_as_number@ */ |
| 400 | 0, /* @tp_as_sequence@ */ |
| 401 | 0, /* @tp_as_mapping@ */ |
| 402 | 0, /* @tp_hash@ */ |
| 403 | 0, /* @tp_call@ */ |
| 404 | 0, /* @tp_str@ */ |
| 405 | 0, /* @tp_getattro@ */ |
| 406 | 0, /* @tp_setattro@ */ |
| 407 | 0, /* @tp_as_buffer@ */ |
| 408 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 409 | Py_TPFLAGS_BASETYPE, |
| 410 | |
| 411 | /* @tp_doc@ */ |
| 412 | "Key size constraints. This object requires the key to be one of a\n\ |
| 413 | few listed sizes.", |
| 414 | |
| 415 | 0, /* @tp_traverse@ */ |
| 416 | 0, /* @tp_clear@ */ |
| 417 | 0, /* @tp_richcompare@ */ |
| 418 | 0, /* @tp_weaklistoffset@ */ |
| 419 | 0, /* @tp_iter@ */ |
| 420 | 0, /* @tp_iternext@ */ |
| 421 | 0, /* @tp_methods@ */ |
| 422 | keyszset_pymembers, /* @tp_members@ */ |
| 423 | keyszset_pygetset, /* @tp_getset@ */ |
| 424 | 0, /* @tp_base@ */ |
| 425 | 0, /* @tp_dict@ */ |
| 426 | 0, /* @tp_descr_get@ */ |
| 427 | 0, /* @tp_descr_set@ */ |
| 428 | 0, /* @tp_dictoffset@ */ |
| 429 | 0, /* @tp_init@ */ |
| 430 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 431 | keyszset_pynew, /* @tp_new@ */ |
| 432 | 0, /* @tp_free@ */ |
| 433 | 0 /* @tp_is_gc@ */ |
| 434 | }; |
| 435 | |
| 436 | #define KSZCONVOP(op) \ |
| 437 | static PyObject *meth__KeySZ_##op(PyObject *me, PyObject *arg) \ |
| 438 | { \ |
| 439 | double x, y; \ |
| 440 | if (!PyArg_ParseTuple(arg, "Od:" #op, &me, &x)) return (0); \ |
| 441 | y = keysz_##op(x); \ |
| 442 | return (PyFloat_FromDouble(y)); \ |
| 443 | } |
| 444 | KSZCONVOP(fromdl) |
| 445 | KSZCONVOP(fromschnorr) |
| 446 | KSZCONVOP(fromif) |
| 447 | KSZCONVOP(fromec) |
| 448 | KSZCONVOP(todl) |
| 449 | KSZCONVOP(toschnorr) |
| 450 | KSZCONVOP(toif) |
| 451 | KSZCONVOP(toec) |
| 452 | #undef KSZCONVOP |
| 453 | |
| 454 | /*----- Symmetric encryption ----------------------------------------------*/ |
| 455 | |
| 456 | PyTypeObject *gccipher_pytype, *gcipher_pytype; |
| 457 | |
| 458 | CONVFUNC(gccipher, gccipher *, GCCIPHER_CC) |
| 459 | CONVFUNC(gcipher, gcipher *, GCIPHER_C) |
| 460 | |
| 461 | PyObject *gcipher_pywrap(PyObject *cobj, gcipher *c, unsigned f) |
| 462 | { |
| 463 | gcipher_pyobj *g; |
| 464 | if (!cobj) cobj = gccipher_pywrap((/*unconst*/ gccipher *)GC_CLASS(c)); |
| 465 | else Py_INCREF(cobj); |
| 466 | g = PyObject_NEW(gcipher_pyobj, (PyTypeObject *)cobj); |
| 467 | g->c = c; |
| 468 | g->f = f; |
| 469 | return ((PyObject *)g); |
| 470 | } |
| 471 | |
| 472 | static PyObject *gcipher_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) |
| 473 | { |
| 474 | char *kwlist[] = { "k", 0 }; |
| 475 | char *k; |
| 476 | Py_ssize_t sz; |
| 477 | |
| 478 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz)) |
| 479 | goto end; |
| 480 | if (keysz(sz, GCCIPHER_CC(ty)->keysz) != sz) VALERR("bad key length"); |
| 481 | return (gcipher_pywrap((PyObject *)ty, |
| 482 | GC_INIT(GCCIPHER_CC(ty), k, sz), |
| 483 | f_freeme)); |
| 484 | end: |
| 485 | return (0); |
| 486 | } |
| 487 | |
| 488 | PyObject *gccipher_pywrap(gccipher *cc) |
| 489 | { |
| 490 | gccipher_pyobj *g = newtype(gccipher_pytype, 0, cc->name); |
| 491 | g->cc = cc; |
| 492 | g->ty.ht_type.tp_basicsize = sizeof(gcipher_pyobj); |
| 493 | g->ty.ht_type.tp_base = gcipher_pytype; |
| 494 | Py_INCREF(gcipher_pytype); |
| 495 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
| 496 | Py_TPFLAGS_BASETYPE | |
| 497 | Py_TPFLAGS_HEAPTYPE); |
| 498 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; |
| 499 | g->ty.ht_type.tp_free = 0; |
| 500 | g->ty.ht_type.tp_new = gcipher_pynew; |
| 501 | typeready(&g->ty.ht_type); |
| 502 | return ((PyObject *)g); |
| 503 | } |
| 504 | |
| 505 | static void gcipher_pydealloc(PyObject *me) |
| 506 | { |
| 507 | if (GCIPHER_F(me) & f_freeme) |
| 508 | GC_DESTROY(GCIPHER_C(me)); |
| 509 | Py_DECREF(me->ob_type); |
| 510 | FREEOBJ(me); |
| 511 | } |
| 512 | |
| 513 | static PyObject *gccget_name(PyObject *me, void *hunoz) |
| 514 | { return (PyString_FromString(GCCIPHER_CC(me)->name)); } |
| 515 | |
| 516 | static PyObject *gccget_keysz(PyObject *me, void *hunoz) |
| 517 | { return (keysz_pywrap(GCCIPHER_CC(me)->keysz)); } |
| 518 | |
| 519 | static PyObject *gccget_blksz(PyObject *me, void *hunoz) |
| 520 | { return (PyInt_FromLong(GCCIPHER_CC(me)->blksz)); } |
| 521 | |
| 522 | static PyObject *gcmeth_encrypt(PyObject *me, PyObject *arg) |
| 523 | { |
| 524 | char *p; |
| 525 | Py_ssize_t sz; |
| 526 | PyObject *rc = 0; |
| 527 | |
| 528 | if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &sz)) return (0); |
| 529 | rc = bytestring_pywrap(0, sz); |
| 530 | GC_ENCRYPT(GCIPHER_C(me), p, PyString_AS_STRING(rc), sz); |
| 531 | return (rc); |
| 532 | } |
| 533 | |
| 534 | static PyObject *gcmeth_enczero(PyObject *me, PyObject *arg) |
| 535 | { |
| 536 | char *p; |
| 537 | int sz; |
| 538 | PyObject *rc = 0; |
| 539 | |
| 540 | if (!PyArg_ParseTuple(arg, "i:enczero", &sz)) return (0); |
| 541 | rc = bytestring_pywrap(0, sz); |
| 542 | p = PyString_AS_STRING(rc); |
| 543 | memset(p, 0, sz); |
| 544 | GC_ENCRYPT(GCIPHER_C(me), p, p, sz); |
| 545 | return (rc); |
| 546 | } |
| 547 | |
| 548 | static PyObject *gcmeth_decrypt(PyObject *me, PyObject *arg) |
| 549 | { |
| 550 | char *p; |
| 551 | Py_ssize_t sz; |
| 552 | PyObject *rc = 0; |
| 553 | |
| 554 | if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &sz)) return (0); |
| 555 | rc = bytestring_pywrap(0, sz); |
| 556 | GC_DECRYPT(GCIPHER_C(me), p, PyString_AS_STRING(rc), sz); |
| 557 | return (rc); |
| 558 | } |
| 559 | |
| 560 | static PyObject *gcmeth_deczero(PyObject *me, PyObject *arg) |
| 561 | { |
| 562 | char *p; |
| 563 | int sz; |
| 564 | PyObject *rc = 0; |
| 565 | |
| 566 | if (!PyArg_ParseTuple(arg, "i:deczero", &sz)) return (0); |
| 567 | rc = bytestring_pywrap(0, sz); |
| 568 | p = PyString_AS_STRING(rc); |
| 569 | memset(p, 0, sz); |
| 570 | GC_DECRYPT(GCIPHER_C(me), p, p, sz); |
| 571 | return (rc); |
| 572 | } |
| 573 | |
| 574 | static PyObject *gcmeth_setiv(PyObject *me, PyObject *arg) |
| 575 | { |
| 576 | char *p; |
| 577 | Py_ssize_t sz; |
| 578 | |
| 579 | if (!PyArg_ParseTuple(arg, "s#:setiv", &p, &sz)) goto end; |
| 580 | if (!GCIPHER_C(me)->ops->setiv) VALERR("`setiv' not supported"); |
| 581 | if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode"); |
| 582 | if (sz != GC_CLASS(GCIPHER_C(me))->blksz) VALERR("bad IV length"); |
| 583 | GC_SETIV(GCIPHER_C(me), p); |
| 584 | RETURN_ME; |
| 585 | end: |
| 586 | return (0); |
| 587 | } |
| 588 | |
| 589 | static PyObject *gcmeth_bdry(PyObject *me, PyObject *arg) |
| 590 | { |
| 591 | if (!PyArg_ParseTuple(arg, ":bdry")) goto end; |
| 592 | if (!GCIPHER_C(me)->ops->bdry) VALERR("`bdry' not supported"); |
| 593 | if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode"); |
| 594 | GC_BDRY(GCIPHER_C(me)); |
| 595 | RETURN_ME; |
| 596 | end: |
| 597 | return (0); |
| 598 | } |
| 599 | |
| 600 | static PyGetSetDef gccipher_pygetset[] = { |
| 601 | #define GETSETNAME(op, name) gcc##op##_##name |
| 602 | GET (keysz, "CC.keysz -> acceptable key sizes") |
| 603 | GET (blksz, "CC.blksz -> block size, or zero") |
| 604 | GET (name, "CC.name -> name of this kind of cipher") |
| 605 | #undef GETSETNAME |
| 606 | { 0 } |
| 607 | }; |
| 608 | |
| 609 | static PyMethodDef gcipher_pymethods[] = { |
| 610 | #define METHNAME(name) gcmeth_##name |
| 611 | METH (encrypt, "C.encrypt(PT) -> CT") |
| 612 | METH (enczero, "C.enczero(N) -> CT") |
| 613 | METH (decrypt, "C.decrypt(CT) -> PT") |
| 614 | METH (deczero, "C.deczero(N) -> PT") |
| 615 | METH (setiv, "C.setiv(IV)") |
| 616 | METH (bdry, "C.bdry()") |
| 617 | #undef METHNAME |
| 618 | { 0 } |
| 619 | }; |
| 620 | |
| 621 | static PyTypeObject gccipher_pytype_skel = { |
| 622 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 623 | "GCCipher", /* @tp_name@ */ |
| 624 | sizeof(gccipher_pyobj), /* @tp_basicsize@ */ |
| 625 | 0, /* @tp_itemsize@ */ |
| 626 | |
| 627 | 0, /* @tp_dealloc@ */ |
| 628 | 0, /* @tp_print@ */ |
| 629 | 0, /* @tp_getattr@ */ |
| 630 | 0, /* @tp_setattr@ */ |
| 631 | 0, /* @tp_compare@ */ |
| 632 | 0, /* @tp_repr@ */ |
| 633 | 0, /* @tp_as_number@ */ |
| 634 | 0, /* @tp_as_sequence@ */ |
| 635 | 0, /* @tp_as_mapping@ */ |
| 636 | 0, /* @tp_hash@ */ |
| 637 | 0, /* @tp_call@ */ |
| 638 | 0, /* @tp_str@ */ |
| 639 | 0, /* @tp_getattro@ */ |
| 640 | 0, /* @tp_setattro@ */ |
| 641 | 0, /* @tp_as_buffer@ */ |
| 642 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 643 | Py_TPFLAGS_BASETYPE, |
| 644 | |
| 645 | /* @tp_doc@ */ |
| 646 | "Symmetric cipher metaclass.", |
| 647 | |
| 648 | 0, /* @tp_traverse@ */ |
| 649 | 0, /* @tp_clear@ */ |
| 650 | 0, /* @tp_richcompare@ */ |
| 651 | 0, /* @tp_weaklistoffset@ */ |
| 652 | 0, /* @tp_iter@ */ |
| 653 | 0, /* @tp_iternext@ */ |
| 654 | 0, /* @tp_methods@ */ |
| 655 | 0, /* @tp_members@ */ |
| 656 | gccipher_pygetset, /* @tp_getset@ */ |
| 657 | 0, /* @tp_base@ */ |
| 658 | 0, /* @tp_dict@ */ |
| 659 | 0, /* @tp_descr_get@ */ |
| 660 | 0, /* @tp_descr_set@ */ |
| 661 | 0, /* @tp_dictoffset@ */ |
| 662 | 0, /* @tp_init@ */ |
| 663 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 664 | abstract_pynew, /* @tp_new@ */ |
| 665 | 0, /* @tp_free@ */ |
| 666 | 0 /* @tp_is_gc@ */ |
| 667 | }; |
| 668 | |
| 669 | static PyTypeObject gcipher_pytype_skel = { |
| 670 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 671 | "GCipher", /* @tp_name@ */ |
| 672 | sizeof(gcipher_pyobj), /* @tp_basicsize@ */ |
| 673 | 0, /* @tp_itemsize@ */ |
| 674 | |
| 675 | gcipher_pydealloc, /* @tp_dealloc@ */ |
| 676 | 0, /* @tp_print@ */ |
| 677 | 0, /* @tp_getattr@ */ |
| 678 | 0, /* @tp_setattr@ */ |
| 679 | 0, /* @tp_compare@ */ |
| 680 | 0, /* @tp_repr@ */ |
| 681 | 0, /* @tp_as_number@ */ |
| 682 | 0, /* @tp_as_sequence@ */ |
| 683 | 0, /* @tp_as_mapping@ */ |
| 684 | 0, /* @tp_hash@ */ |
| 685 | 0, /* @tp_call@ */ |
| 686 | 0, /* @tp_str@ */ |
| 687 | 0, /* @tp_getattro@ */ |
| 688 | 0, /* @tp_setattro@ */ |
| 689 | 0, /* @tp_as_buffer@ */ |
| 690 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 691 | Py_TPFLAGS_BASETYPE, |
| 692 | |
| 693 | /* @tp_doc@ */ |
| 694 | "Symmetric cipher, abstract base class.", |
| 695 | |
| 696 | 0, /* @tp_traverse@ */ |
| 697 | 0, /* @tp_clear@ */ |
| 698 | 0, /* @tp_richcompare@ */ |
| 699 | 0, /* @tp_weaklistoffset@ */ |
| 700 | 0, /* @tp_iter@ */ |
| 701 | 0, /* @tp_iternext@ */ |
| 702 | gcipher_pymethods, /* @tp_methods@ */ |
| 703 | 0, /* @tp_members@ */ |
| 704 | 0, /* @tp_getset@ */ |
| 705 | 0, /* @tp_base@ */ |
| 706 | 0, /* @tp_dict@ */ |
| 707 | 0, /* @tp_descr_get@ */ |
| 708 | 0, /* @tp_descr_set@ */ |
| 709 | 0, /* @tp_dictoffset@ */ |
| 710 | 0, /* @tp_init@ */ |
| 711 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 712 | abstract_pynew, /* @tp_new@ */ |
| 713 | 0, /* @tp_free@ */ |
| 714 | 0 /* @tp_is_gc@ */ |
| 715 | }; |
| 716 | |
| 717 | /*----- Hash functions ----------------------------------------------------*/ |
| 718 | |
| 719 | PyTypeObject *gchash_pytype, *ghash_pytype; |
| 720 | |
| 721 | CONVFUNC(gchash, gchash *, GCHASH_CH) |
| 722 | CONVFUNC(ghash, ghash *, GHASH_H) |
| 723 | |
| 724 | static PyObject *ghash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) |
| 725 | { |
| 726 | char *kwlist[] = { 0 }; |
| 727 | if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", kwlist)) |
| 728 | goto end; |
| 729 | return (ghash_pywrap((PyObject *)ty, GH_INIT(GCHASH_CH(ty)), f_freeme)); |
| 730 | end: |
| 731 | return (0); |
| 732 | } |
| 733 | |
| 734 | PyObject *gchash_pywrap(gchash *ch) |
| 735 | { |
| 736 | gchash_pyobj *g = newtype(gchash_pytype, 0, ch->name); |
| 737 | g->ch = ch; |
| 738 | g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj); |
| 739 | g->ty.ht_type.tp_base = ghash_pytype; |
| 740 | Py_INCREF(ghash_pytype); |
| 741 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
| 742 | Py_TPFLAGS_BASETYPE | |
| 743 | Py_TPFLAGS_HEAPTYPE); |
| 744 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; |
| 745 | g->ty.ht_type.tp_free = 0; |
| 746 | g->ty.ht_type.tp_new = ghash_pynew; |
| 747 | typeready(&g->ty.ht_type); |
| 748 | return ((PyObject *)g); |
| 749 | } |
| 750 | |
| 751 | PyObject *ghash_pywrap(PyObject *cobj, ghash *h, unsigned f) |
| 752 | { |
| 753 | ghash_pyobj *g; |
| 754 | if (!cobj) cobj = gchash_pywrap((/*unconst*/ gchash *)GH_CLASS(h)); |
| 755 | else Py_INCREF(cobj); |
| 756 | g = PyObject_NEW(ghash_pyobj, (PyTypeObject *)cobj); |
| 757 | g->h = h; |
| 758 | g->f = f; |
| 759 | return ((PyObject *)g); |
| 760 | } |
| 761 | |
| 762 | static void ghash_pydealloc(PyObject *me) |
| 763 | { |
| 764 | if (GHASH_F(me) & f_freeme) |
| 765 | GH_DESTROY(GHASH_H(me)); |
| 766 | Py_DECREF(me->ob_type); |
| 767 | FREEOBJ(me); |
| 768 | } |
| 769 | |
| 770 | static PyObject *gchget_name(PyObject *me, void *hunoz) |
| 771 | { return (PyString_FromString(GCHASH_CH(me)->name)); } |
| 772 | |
| 773 | static PyObject *gchget_hashsz(PyObject *me, void *hunoz) |
| 774 | { return (PyInt_FromLong(GCHASH_CH(me)->hashsz)); } |
| 775 | |
| 776 | static PyObject *gchget_bufsz(PyObject *me, void *hunoz) |
| 777 | { return (PyInt_FromLong(GCHASH_CH(me)->bufsz)); } |
| 778 | |
| 779 | static PyObject *ghmeth_hash(PyObject *me, PyObject *arg) |
| 780 | { |
| 781 | char *p; |
| 782 | Py_ssize_t sz; |
| 783 | if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0); |
| 784 | GH_HASH(GHASH_H(me), p, sz); |
| 785 | RETURN_ME; |
| 786 | } |
| 787 | |
| 788 | #define GHMETH_HASHU_(n, W, w) \ |
| 789 | static PyObject *ghmeth_hashu##w(PyObject *me, PyObject *arg) \ |
| 790 | { \ |
| 791 | uint##n x; \ |
| 792 | if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \ |
| 793 | GH_HASHU##W(GHASH_H(me), x); \ |
| 794 | RETURN_ME; \ |
| 795 | end: \ |
| 796 | return (0); \ |
| 797 | } |
| 798 | DOUINTCONV(GHMETH_HASHU_) |
| 799 | |
| 800 | #define GHMETH_HASHBUF_(n, W, w) \ |
| 801 | static PyObject *ghmeth_hashbuf##w(PyObject *me, PyObject *arg) \ |
| 802 | { \ |
| 803 | char *p; \ |
| 804 | Py_ssize_t sz; \ |
| 805 | if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \ |
| 806 | if (sz > MASK##n) TYERR("string too long"); \ |
| 807 | GH_HASHBUF##W(GHASH_H(me), p, sz); \ |
| 808 | RETURN_ME; \ |
| 809 | end: \ |
| 810 | return (0); \ |
| 811 | } |
| 812 | DOUINTCONV(GHMETH_HASHBUF_) |
| 813 | |
| 814 | static PyObject *ghmeth_hashstrz(PyObject *me, PyObject *arg) |
| 815 | { |
| 816 | char *p; |
| 817 | if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0); |
| 818 | GH_HASHSTRZ(GHASH_H(me), p); |
| 819 | RETURN_ME; |
| 820 | } |
| 821 | |
| 822 | static PyObject *ghmeth_done(PyObject *me, PyObject *arg) |
| 823 | { |
| 824 | ghash *g; |
| 825 | PyObject *rc; |
| 826 | if (!PyArg_ParseTuple(arg, ":done")) return (0); |
| 827 | g = GH_COPY(GHASH_H(me)); |
| 828 | rc = bytestring_pywrap(0, g->ops->c->hashsz); |
| 829 | GH_DONE(g, PyString_AS_STRING(rc)); |
| 830 | GH_DESTROY(g); |
| 831 | return (rc); |
| 832 | } |
| 833 | |
| 834 | static PyGetSetDef gchash_pygetset[] = { |
| 835 | #define GETSETNAME(op, name) gch##op##_##name |
| 836 | GET (bufsz, "CH.bufsz -> hash buffer size, or zero") |
| 837 | GET (hashsz, "CH.hashsz -> hash output size") |
| 838 | GET (name, "CH.name -> name of this kind of hash") |
| 839 | #undef GETSETNAME |
| 840 | { 0 } |
| 841 | }; |
| 842 | |
| 843 | static PyMethodDef ghash_pymethods[] = { |
| 844 | #define METHNAME(name) ghmeth_##name |
| 845 | METH (hash, "H.hash(M)") |
| 846 | #define METHU_(n, W, w) METH(hashu##w, "H.hashu" #w "(WORD)") |
| 847 | DOUINTCONV(METHU_) |
| 848 | #undef METHU_ |
| 849 | #define METHBUF_(n, W, w) METH(hashbuf##w, "H.hashbuf" #w "(BYTES)") |
| 850 | DOUINTCONV(METHBUF_) |
| 851 | #undef METHBUF_ |
| 852 | METH (hashstrz, "H.hashstrz(STRING)") |
| 853 | METH (done, "H.done() -> HASH") |
| 854 | #undef METHNAME |
| 855 | { 0 } |
| 856 | }; |
| 857 | |
| 858 | static PyTypeObject gchash_pytype_skel = { |
| 859 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 860 | "GCHash", /* @tp_name@ */ |
| 861 | sizeof(gchash_pyobj), /* @tp_basicsize@ */ |
| 862 | 0, /* @tp_itemsize@ */ |
| 863 | |
| 864 | 0, /* @tp_dealloc@ */ |
| 865 | 0, /* @tp_print@ */ |
| 866 | 0, /* @tp_getattr@ */ |
| 867 | 0, /* @tp_setattr@ */ |
| 868 | 0, /* @tp_compare@ */ |
| 869 | 0, /* @tp_repr@ */ |
| 870 | 0, /* @tp_as_number@ */ |
| 871 | 0, /* @tp_as_sequence@ */ |
| 872 | 0, /* @tp_as_mapping@ */ |
| 873 | 0, /* @tp_hash@ */ |
| 874 | 0, /* @tp_call@ */ |
| 875 | 0, /* @tp_str@ */ |
| 876 | 0, /* @tp_getattro@ */ |
| 877 | 0, /* @tp_setattro@ */ |
| 878 | 0, /* @tp_as_buffer@ */ |
| 879 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 880 | Py_TPFLAGS_BASETYPE, |
| 881 | |
| 882 | /* @tp_doc@ */ |
| 883 | "Hash function metaclass.", |
| 884 | |
| 885 | 0, /* @tp_traverse@ */ |
| 886 | 0, /* @tp_clear@ */ |
| 887 | 0, /* @tp_richcompare@ */ |
| 888 | 0, /* @tp_weaklistoffset@ */ |
| 889 | 0, /* @tp_iter@ */ |
| 890 | 0, /* @tp_iternext@ */ |
| 891 | 0, /* @tp_methods@ */ |
| 892 | 0, /* @tp_members@ */ |
| 893 | gchash_pygetset, /* @tp_getset@ */ |
| 894 | 0, /* @tp_base@ */ |
| 895 | 0, /* @tp_dict@ */ |
| 896 | 0, /* @tp_descr_get@ */ |
| 897 | 0, /* @tp_descr_set@ */ |
| 898 | 0, /* @tp_dictoffset@ */ |
| 899 | 0, /* @tp_init@ */ |
| 900 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 901 | abstract_pynew, /* @tp_new@ */ |
| 902 | 0, /* @tp_free@ */ |
| 903 | 0 /* @tp_is_gc@ */ |
| 904 | }; |
| 905 | |
| 906 | static PyTypeObject ghash_pytype_skel = { |
| 907 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 908 | "GHash", /* @tp_name@ */ |
| 909 | sizeof(ghash_pyobj), /* @tp_basicsize@ */ |
| 910 | 0, /* @tp_itemsize@ */ |
| 911 | |
| 912 | ghash_pydealloc, /* @tp_dealloc@ */ |
| 913 | 0, /* @tp_print@ */ |
| 914 | 0, /* @tp_getattr@ */ |
| 915 | 0, /* @tp_setattr@ */ |
| 916 | 0, /* @tp_compare@ */ |
| 917 | 0, /* @tp_repr@ */ |
| 918 | 0, /* @tp_as_number@ */ |
| 919 | 0, /* @tp_as_sequence@ */ |
| 920 | 0, /* @tp_as_mapping@ */ |
| 921 | 0, /* @tp_hash@ */ |
| 922 | 0, /* @tp_call@ */ |
| 923 | 0, /* @tp_str@ */ |
| 924 | 0, /* @tp_getattro@ */ |
| 925 | 0, /* @tp_setattro@ */ |
| 926 | 0, /* @tp_as_buffer@ */ |
| 927 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 928 | Py_TPFLAGS_BASETYPE, |
| 929 | |
| 930 | /* @tp_doc@ */ |
| 931 | "Hash function, abstract base class.", |
| 932 | |
| 933 | 0, /* @tp_traverse@ */ |
| 934 | 0, /* @tp_clear@ */ |
| 935 | 0, /* @tp_richcompare@ */ |
| 936 | 0, /* @tp_weaklistoffset@ */ |
| 937 | 0, /* @tp_iter@ */ |
| 938 | 0, /* @tp_iternext@ */ |
| 939 | ghash_pymethods, /* @tp_methods@ */ |
| 940 | 0, /* @tp_members@ */ |
| 941 | 0, /* @tp_getset@ */ |
| 942 | 0, /* @tp_base@ */ |
| 943 | 0, /* @tp_dict@ */ |
| 944 | 0, /* @tp_descr_get@ */ |
| 945 | 0, /* @tp_descr_set@ */ |
| 946 | 0, /* @tp_dictoffset@ */ |
| 947 | 0, /* @tp_init@ */ |
| 948 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 949 | abstract_pynew, /* @tp_new@ */ |
| 950 | 0, /* @tp_free@ */ |
| 951 | 0 /* @tp_is_gc@ */ |
| 952 | }; |
| 953 | |
| 954 | /*----- Message authentication --------------------------------------------*/ |
| 955 | |
| 956 | PyTypeObject *gcmac_pytype, *gmac_pytype, *gmhash_pytype; |
| 957 | |
| 958 | CONVFUNC(gcmac, gcmac *, GCMAC_CM) |
| 959 | CONVFUNC(gmac, gmac *, GMAC_M) |
| 960 | CONVFUNC(gmhash, ghash *, GHASH_H) |
| 961 | |
| 962 | static PyObject *gmac_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) |
| 963 | { |
| 964 | char *kwlist[] = { "k", 0 }; |
| 965 | char *k; |
| 966 | Py_ssize_t sz; |
| 967 | |
| 968 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz)) |
| 969 | goto end; |
| 970 | if (keysz(sz, GCMAC_CM(ty)->keysz) != sz) VALERR("bad key length"); |
| 971 | return (gmac_pywrap((PyObject *)ty, |
| 972 | GM_KEY(GCMAC_CM(ty), k, sz), |
| 973 | f_freeme)); |
| 974 | end: |
| 975 | return (0); |
| 976 | } |
| 977 | |
| 978 | static PyObject *gmhash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) |
| 979 | { |
| 980 | char *kwlist[] = { 0 }; |
| 981 | ghash_pyobj *g; |
| 982 | |
| 983 | if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", kwlist)) return (0); |
| 984 | g = PyObject_NEW(ghash_pyobj, ty); |
| 985 | g->h = GM_INIT(GMAC_M(ty)); |
| 986 | g->f = f_freeme; |
| 987 | Py_INCREF(ty); |
| 988 | return ((PyObject *)g); |
| 989 | } |
| 990 | |
| 991 | PyObject *gcmac_pywrap(gcmac *cm) |
| 992 | { |
| 993 | gcmac_pyobj *g = newtype(gcmac_pytype, 0, cm->name); |
| 994 | g->cm = cm; |
| 995 | g->ty.ht_type.tp_basicsize = sizeof(gmac_pyobj); |
| 996 | g->ty.ht_type.tp_base = gmac_pytype; |
| 997 | Py_INCREF(gmac_pytype); |
| 998 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
| 999 | Py_TPFLAGS_BASETYPE | |
| 1000 | Py_TPFLAGS_HEAPTYPE); |
| 1001 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; |
| 1002 | g->ty.ht_type.tp_free = 0; |
| 1003 | g->ty.ht_type.tp_new = gmac_pynew; |
| 1004 | typeready(&g->ty.ht_type); |
| 1005 | return ((PyObject *)g); |
| 1006 | } |
| 1007 | |
| 1008 | PyObject *gmac_pywrap(PyObject *cobj, gmac *m, unsigned f) |
| 1009 | { |
| 1010 | gmac_pyobj *g; |
| 1011 | if (!cobj) cobj = gcmac_pywrap((/*unconst*/ gcmac *)GM_CLASS(m)); |
| 1012 | else Py_INCREF(cobj); |
| 1013 | g = newtype((PyTypeObject *)cobj, 0, 0); |
| 1014 | g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj); |
| 1015 | g->ty.ht_name = PyString_FromFormat("%s(keyed)", m->ops->c->name); |
| 1016 | g->ty.ht_type.tp_name = PyString_AS_STRING(g->ty.ht_name); |
| 1017 | g->ty.ht_type.tp_base = gmhash_pytype; |
| 1018 | Py_INCREF(gmac_pytype); |
| 1019 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
| 1020 | Py_TPFLAGS_BASETYPE | |
| 1021 | Py_TPFLAGS_HEAPTYPE); |
| 1022 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; |
| 1023 | g->ty.ht_type.tp_free = 0; |
| 1024 | g->ty.ht_type.tp_new = gmhash_pynew; |
| 1025 | typeready(&g->ty.ht_type); |
| 1026 | g->m = m; |
| 1027 | g->f = f; |
| 1028 | return ((PyObject *)g); |
| 1029 | } |
| 1030 | |
| 1031 | static void gmac_pydealloc(PyObject *me) |
| 1032 | { |
| 1033 | if (GMAC_F(me) & f_freeme) |
| 1034 | GM_DESTROY(GMAC_M(me)); |
| 1035 | Py_DECREF(me->ob_type); |
| 1036 | PyType_Type.tp_dealloc(me); |
| 1037 | } |
| 1038 | |
| 1039 | static PyObject *gcmget_name(PyObject *me, void *hunoz) |
| 1040 | { return (PyString_FromString(GCMAC_CM(me)->name)); } |
| 1041 | |
| 1042 | static PyObject *gcmget_keysz(PyObject *me, void *hunoz) |
| 1043 | { return (keysz_pywrap(GCMAC_CM(me)->keysz)); } |
| 1044 | |
| 1045 | static PyObject *gcmget_tagsz(PyObject *me, void *hunoz) |
| 1046 | { return (PyInt_FromLong(GCMAC_CM(me)->hashsz)); } |
| 1047 | |
| 1048 | static PyGetSetDef gcmac_pygetset[] = { |
| 1049 | #define GETSETNAME(op, name) gcm##op##_##name |
| 1050 | GET (keysz, "CM.keysz -> acceptable key sizes") |
| 1051 | GET (tagsz, "CM.tagsz -> MAC output size") |
| 1052 | GET (name, "CM.name -> name of this kind of MAC") |
| 1053 | #undef GETSETNAME |
| 1054 | { 0 } |
| 1055 | }; |
| 1056 | |
| 1057 | static PyTypeObject gcmac_pytype_skel = { |
| 1058 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1059 | "GCMAC", /* @tp_name@ */ |
| 1060 | sizeof(gchash_pyobj), /* @tp_basicsize@ */ |
| 1061 | 0, /* @tp_itemsize@ */ |
| 1062 | |
| 1063 | 0, /* @tp_dealloc@ */ |
| 1064 | 0, /* @tp_print@ */ |
| 1065 | 0, /* @tp_getattr@ */ |
| 1066 | 0, /* @tp_setattr@ */ |
| 1067 | 0, /* @tp_compare@ */ |
| 1068 | 0, /* @tp_repr@ */ |
| 1069 | 0, /* @tp_as_number@ */ |
| 1070 | 0, /* @tp_as_sequence@ */ |
| 1071 | 0, /* @tp_as_mapping@ */ |
| 1072 | 0, /* @tp_hash@ */ |
| 1073 | 0, /* @tp_call@ */ |
| 1074 | 0, /* @tp_str@ */ |
| 1075 | 0, /* @tp_getattro@ */ |
| 1076 | 0, /* @tp_setattro@ */ |
| 1077 | 0, /* @tp_as_buffer@ */ |
| 1078 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 1079 | Py_TPFLAGS_BASETYPE, |
| 1080 | |
| 1081 | /* @tp_doc@ */ |
| 1082 | "Message authentication code metametaclass.", |
| 1083 | |
| 1084 | 0, /* @tp_traverse@ */ |
| 1085 | 0, /* @tp_clear@ */ |
| 1086 | 0, /* @tp_richcompare@ */ |
| 1087 | 0, /* @tp_weaklistoffset@ */ |
| 1088 | 0, /* @tp_iter@ */ |
| 1089 | 0, /* @tp_iternext@ */ |
| 1090 | 0, /* @tp_methods@ */ |
| 1091 | 0, /* @tp_members@ */ |
| 1092 | gcmac_pygetset, /* @tp_getset@ */ |
| 1093 | 0, /* @tp_base@ */ |
| 1094 | 0, /* @tp_dict@ */ |
| 1095 | 0, /* @tp_descr_get@ */ |
| 1096 | 0, /* @tp_descr_set@ */ |
| 1097 | 0, /* @tp_dictoffset@ */ |
| 1098 | 0, /* @tp_init@ */ |
| 1099 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 1100 | abstract_pynew, /* @tp_new@ */ |
| 1101 | 0, /* @tp_free@ */ |
| 1102 | 0 /* @tp_is_gc@ */ |
| 1103 | }; |
| 1104 | |
| 1105 | static PyTypeObject gmac_pytype_skel = { |
| 1106 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1107 | "GMAC", /* @tp_name@ */ |
| 1108 | sizeof(gmac_pyobj), /* @tp_basicsize@ */ |
| 1109 | 0, /* @tp_itemsize@ */ |
| 1110 | |
| 1111 | gmac_pydealloc, /* @tp_dealloc@ */ |
| 1112 | 0, /* @tp_print@ */ |
| 1113 | 0, /* @tp_getattr@ */ |
| 1114 | 0, /* @tp_setattr@ */ |
| 1115 | 0, /* @tp_compare@ */ |
| 1116 | 0, /* @tp_repr@ */ |
| 1117 | 0, /* @tp_as_number@ */ |
| 1118 | 0, /* @tp_as_sequence@ */ |
| 1119 | 0, /* @tp_as_mapping@ */ |
| 1120 | 0, /* @tp_hash@ */ |
| 1121 | 0, /* @tp_call@ */ |
| 1122 | 0, /* @tp_str@ */ |
| 1123 | 0, /* @tp_getattro@ */ |
| 1124 | 0, /* @tp_setattro@ */ |
| 1125 | 0, /* @tp_as_buffer@ */ |
| 1126 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 1127 | Py_TPFLAGS_BASETYPE, |
| 1128 | |
| 1129 | /* @tp_doc@ */ |
| 1130 | "Message authentication code metaclass, abstract base class.", |
| 1131 | |
| 1132 | 0, /* @tp_traverse@ */ |
| 1133 | 0, /* @tp_clear@ */ |
| 1134 | 0, /* @tp_richcompare@ */ |
| 1135 | 0, /* @tp_weaklistoffset@ */ |
| 1136 | 0, /* @tp_iter@ */ |
| 1137 | 0, /* @tp_iternext@ */ |
| 1138 | 0, /* @tp_methods@ */ |
| 1139 | 0, /* @tp_members@ */ |
| 1140 | 0, /* @tp_getset@ */ |
| 1141 | 0, /* @tp_base@ */ |
| 1142 | 0, /* @tp_dict@ */ |
| 1143 | 0, /* @tp_descr_get@ */ |
| 1144 | 0, /* @tp_descr_set@ */ |
| 1145 | 0, /* @tp_dictoffset@ */ |
| 1146 | 0, /* @tp_init@ */ |
| 1147 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 1148 | abstract_pynew, /* @tp_new@ */ |
| 1149 | 0, /* @tp_free@ */ |
| 1150 | 0 /* @tp_is_gc@ */ |
| 1151 | }; |
| 1152 | |
| 1153 | static PyTypeObject gmhash_pytype_skel = { |
| 1154 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1155 | "GMACHash", /* @tp_name@ */ |
| 1156 | sizeof(ghash_pyobj), /* @tp_basicsize@ */ |
| 1157 | 0, /* @tp_itemsize@ */ |
| 1158 | |
| 1159 | ghash_pydealloc, /* @tp_dealloc@ */ |
| 1160 | 0, /* @tp_print@ */ |
| 1161 | 0, /* @tp_getattr@ */ |
| 1162 | 0, /* @tp_setattr@ */ |
| 1163 | 0, /* @tp_compare@ */ |
| 1164 | 0, /* @tp_repr@ */ |
| 1165 | 0, /* @tp_as_number@ */ |
| 1166 | 0, /* @tp_as_sequence@ */ |
| 1167 | 0, /* @tp_as_mapping@ */ |
| 1168 | 0, /* @tp_hash@ */ |
| 1169 | 0, /* @tp_call@ */ |
| 1170 | 0, /* @tp_str@ */ |
| 1171 | 0, /* @tp_getattro@ */ |
| 1172 | 0, /* @tp_setattro@ */ |
| 1173 | 0, /* @tp_as_buffer@ */ |
| 1174 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 1175 | Py_TPFLAGS_BASETYPE, |
| 1176 | |
| 1177 | /* @tp_doc@ */ |
| 1178 | "Message authentication code, abstract base class.", |
| 1179 | |
| 1180 | 0, /* @tp_traverse@ */ |
| 1181 | 0, /* @tp_clear@ */ |
| 1182 | 0, /* @tp_richcompare@ */ |
| 1183 | 0, /* @tp_weaklistoffset@ */ |
| 1184 | 0, /* @tp_iter@ */ |
| 1185 | 0, /* @tp_iternext@ */ |
| 1186 | 0, /* @tp_methods@ */ |
| 1187 | 0, /* @tp_members@ */ |
| 1188 | 0, /* @tp_getset@ */ |
| 1189 | 0, /* @tp_base@ */ |
| 1190 | 0, /* @tp_dict@ */ |
| 1191 | 0, /* @tp_descr_get@ */ |
| 1192 | 0, /* @tp_descr_set@ */ |
| 1193 | 0, /* @tp_dictoffset@ */ |
| 1194 | 0, /* @tp_init@ */ |
| 1195 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 1196 | abstract_pynew, /* @tp_new@ */ |
| 1197 | 0, /* @tp_free@ */ |
| 1198 | 0 /* @tp_is_gc@ */ |
| 1199 | }; |
| 1200 | |
| 1201 | /*----- Special snowflake for Poly1305 ------------------------------------*/ |
| 1202 | |
| 1203 | PyTypeObject *poly1305cls_pytype, *poly1305key_pytype, *poly1305hash_pytype; |
| 1204 | |
| 1205 | typedef struct poly1305key_pyobj { |
| 1206 | PyHeapTypeObject ty; |
| 1207 | poly1305_key k; |
| 1208 | } poly1305key_pyobj; |
| 1209 | |
| 1210 | typedef struct poly1305hash_pyobj { |
| 1211 | PyObject_HEAD |
| 1212 | unsigned f; |
| 1213 | #define f_mask 1u |
| 1214 | poly1305_ctx ctx; |
| 1215 | } poly1305hash_pyobj; |
| 1216 | |
| 1217 | #define P1305_F(o) (((poly1305hash_pyobj *)(o))->f) |
| 1218 | #define P1305_CTX(o) (&((poly1305hash_pyobj *)(o))->ctx) |
| 1219 | CONVFUNC(poly1305hash, poly1305_ctx *, P1305_CTX) |
| 1220 | |
| 1221 | static PyObject *poly1305hash_pynew(PyTypeObject *ty, |
| 1222 | PyObject *arg, PyObject *kw) |
| 1223 | { |
| 1224 | char *kwlist[] = { "mask", 0 }; |
| 1225 | poly1305key_pyobj *pk = (poly1305key_pyobj *)ty; |
| 1226 | poly1305hash_pyobj *ph; |
| 1227 | char *m = 0; |
| 1228 | Py_ssize_t sz; |
| 1229 | |
| 1230 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|s#:new", kwlist, &m, &sz)) |
| 1231 | return (0); |
| 1232 | if (m && sz != POLY1305_MASKSZ) VALERR("bad mask length"); |
| 1233 | ph = PyObject_NEW(poly1305hash_pyobj, ty); |
| 1234 | ph->f = 0; |
| 1235 | if (m) ph->f |= f_mask; |
| 1236 | poly1305_macinit(&ph->ctx, &pk->k, m); |
| 1237 | Py_INCREF(ty); |
| 1238 | return ((PyObject *)ph); |
| 1239 | end: |
| 1240 | return (0); |
| 1241 | } |
| 1242 | |
| 1243 | static PyObject *poly1305key_pynew(PyTypeObject *ty, |
| 1244 | PyObject *arg, PyObject *kw) |
| 1245 | { |
| 1246 | char *kwlist[] = { "k", 0 }; |
| 1247 | poly1305key_pyobj *pk; |
| 1248 | char *k; |
| 1249 | Py_ssize_t sz; |
| 1250 | |
| 1251 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz)) |
| 1252 | goto end; |
| 1253 | if (keysz(sz, poly1305_keysz) != sz) VALERR("bad key length"); |
| 1254 | |
| 1255 | pk = newtype(ty, 0, 0); |
| 1256 | pk->ty.ht_name = PyString_FromString("poly1305(keyed)"); |
| 1257 | pk->ty.ht_type.tp_basicsize = sizeof(poly1305hash_pyobj); |
| 1258 | pk->ty.ht_type.tp_name = PyString_AS_STRING(pk->ty.ht_name); |
| 1259 | pk->ty.ht_type.tp_base = poly1305hash_pytype; |
| 1260 | Py_INCREF(poly1305key_pytype); |
| 1261 | pk->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
| 1262 | Py_TPFLAGS_BASETYPE | |
| 1263 | Py_TPFLAGS_HEAPTYPE); |
| 1264 | pk->ty.ht_type.tp_alloc = PyType_GenericAlloc; |
| 1265 | pk->ty.ht_type.tp_free = 0; |
| 1266 | pk->ty.ht_type.tp_new = poly1305hash_pynew; |
| 1267 | typeready(&pk->ty.ht_type); |
| 1268 | |
| 1269 | poly1305_keyinit(&pk->k, k, sz); |
| 1270 | return ((PyObject *)pk); |
| 1271 | |
| 1272 | end: |
| 1273 | return (0); |
| 1274 | } |
| 1275 | |
| 1276 | static PyObject *poly1305clsget_name(PyObject *me, void *hunoz) |
| 1277 | { return (PyString_FromString("poly1305")); } |
| 1278 | |
| 1279 | static PyObject *poly1305clsget_keysz(PyObject *me, void *hunoz) |
| 1280 | { return (keysz_pywrap(poly1305_keysz)); } |
| 1281 | |
| 1282 | static PyObject *poly1305clsget_masksz(PyObject *me, void *hunoz) |
| 1283 | { return (PyInt_FromLong(POLY1305_MASKSZ)); } |
| 1284 | |
| 1285 | static PyObject *poly1305clsget_tagsz(PyObject *me, void *hunoz) |
| 1286 | { return (PyInt_FromLong(POLY1305_TAGSZ)); } |
| 1287 | |
| 1288 | static PyObject *polymeth_copy(PyObject *me, PyObject *arg) |
| 1289 | { |
| 1290 | poly1305hash_pyobj *ph; |
| 1291 | if (!PyArg_ParseTuple(arg, ":copy")) return (0); |
| 1292 | ph = PyObject_NEW(poly1305hash_pyobj, me->ob_type); |
| 1293 | poly1305_copy(&ph->ctx, P1305_CTX(me)); |
| 1294 | Py_INCREF(me->ob_type); |
| 1295 | return ((PyObject *)ph); |
| 1296 | } |
| 1297 | |
| 1298 | static PyObject *polymeth_hash(PyObject *me, PyObject *arg) |
| 1299 | { |
| 1300 | char *p; |
| 1301 | Py_ssize_t sz; |
| 1302 | if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0); |
| 1303 | poly1305_hash(P1305_CTX(me), p, sz); |
| 1304 | RETURN_ME; |
| 1305 | } |
| 1306 | |
| 1307 | #define POLYMETH_HASHU_(n, W, w) \ |
| 1308 | static PyObject *polymeth_hashu##w(PyObject *me, PyObject *arg) \ |
| 1309 | { \ |
| 1310 | uint##n x; \ |
| 1311 | octet b[SZ_##W]; \ |
| 1312 | if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \ |
| 1313 | STORE##W(b, x); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \ |
| 1314 | RETURN_ME; \ |
| 1315 | end: \ |
| 1316 | return (0); \ |
| 1317 | } |
| 1318 | DOUINTCONV(POLYMETH_HASHU_) |
| 1319 | |
| 1320 | #define POLYMETH_HASHBUF_(n, W, w) \ |
| 1321 | static PyObject *polymeth_hashbuf##w(PyObject *me, PyObject *arg) \ |
| 1322 | { \ |
| 1323 | char *p; \ |
| 1324 | Py_ssize_t sz; \ |
| 1325 | octet b[SZ_##W]; \ |
| 1326 | if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \ |
| 1327 | if (sz > MASK##n) TYERR("string too long"); \ |
| 1328 | STORE##W(b, sz); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \ |
| 1329 | poly1305_hash(P1305_CTX(me), p, sz); \ |
| 1330 | RETURN_ME; \ |
| 1331 | end: \ |
| 1332 | return (0); \ |
| 1333 | } |
| 1334 | DOUINTCONV(POLYMETH_HASHBUF_) |
| 1335 | |
| 1336 | static PyObject *polymeth_hashstrz(PyObject *me, PyObject *arg) |
| 1337 | { |
| 1338 | char *p; |
| 1339 | if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0); |
| 1340 | poly1305_hash(P1305_CTX(me), p, strlen(p) + 1); |
| 1341 | RETURN_ME; |
| 1342 | } |
| 1343 | |
| 1344 | static PyObject *polymeth_flush(PyObject *me, PyObject *arg) |
| 1345 | { |
| 1346 | if (!PyArg_ParseTuple(arg, ":flush")) return (0); |
| 1347 | poly1305_flush(P1305_CTX(me)); |
| 1348 | RETURN_ME; |
| 1349 | } |
| 1350 | |
| 1351 | static PyObject *polymeth_flushzero(PyObject *me, PyObject *arg) |
| 1352 | { |
| 1353 | if (!PyArg_ParseTuple(arg, ":flushzero")) return (0); |
| 1354 | poly1305_flushzero(P1305_CTX(me)); |
| 1355 | RETURN_ME; |
| 1356 | } |
| 1357 | |
| 1358 | static PyObject *polymeth_concat(PyObject *me, PyObject *arg) |
| 1359 | { |
| 1360 | PyObject *pre, *suff; |
| 1361 | if (!PyArg_ParseTuple(arg, "OO:concat", &pre, &suff)) return (0); |
| 1362 | if (!PyObject_TypeCheck(pre, poly1305hash_pytype) || |
| 1363 | !PyObject_TypeCheck(suff, poly1305hash_pytype)) |
| 1364 | TYERR("wanted a poly1305hash"); |
| 1365 | if (me->ob_type != pre->ob_type || me->ob_type != suff->ob_type) |
| 1366 | TYERR("key mismatch"); |
| 1367 | if (P1305_CTX(pre)->nbuf) VALERR("prefix is not block-aligned"); |
| 1368 | poly1305_concat(P1305_CTX(me), P1305_CTX(pre), P1305_CTX(suff)); |
| 1369 | RETURN_ME; |
| 1370 | end: |
| 1371 | return (0); |
| 1372 | } |
| 1373 | |
| 1374 | static PyObject *polymeth_done(PyObject *me, PyObject *arg) |
| 1375 | { |
| 1376 | PyObject *rc; |
| 1377 | if (!PyArg_ParseTuple(arg, ":done")) return (0); |
| 1378 | if (!(P1305_F(me) & f_mask)) VALERR("no mask"); |
| 1379 | rc = bytestring_pywrap(0, POLY1305_TAGSZ); |
| 1380 | poly1305_done(P1305_CTX(me), PyString_AS_STRING(rc)); |
| 1381 | return (rc); |
| 1382 | end: |
| 1383 | return (0); |
| 1384 | } |
| 1385 | |
| 1386 | static PyGetSetDef poly1305cls_pygetset[] = { |
| 1387 | #define GETSETNAME(op, name) poly1305cls##op##_##name |
| 1388 | GET (keysz, "PC.keysz -> acceptable key sizes") |
| 1389 | GET (masksz, "PC.masksz -> mask size") |
| 1390 | GET (tagsz, "PC.tagsz -> MAC output size") |
| 1391 | GET (name, "PC.name -> name of this kind of MAC") |
| 1392 | #undef GETSETNAME |
| 1393 | { 0 } |
| 1394 | }; |
| 1395 | |
| 1396 | static PyMethodDef poly1305hash_pymethods[] = { |
| 1397 | #define METHNAME(name) polymeth_##name |
| 1398 | METH (copy, "P.copy() -> PP") |
| 1399 | METH (hash, "P.hash(M)") |
| 1400 | #define METHU_(n, W, w) METH(hashu##w, "P.hashu" #w "(WORD)") |
| 1401 | DOUINTCONV(METHU_) |
| 1402 | #undef METHU_ |
| 1403 | #define METHBUF_(n, W, w) METH(hashbuf##w, "P.hashbuf" #w "(BYTES)") |
| 1404 | DOUINTCONV(METHBUF_) |
| 1405 | #undef METHBUF_ |
| 1406 | METH (hashstrz, "P.hashstrz(STRING)") |
| 1407 | METH (flush, "P.flush()") |
| 1408 | METH (flushzero, "P.flushzero()") |
| 1409 | METH (concat, "P.concat(PREFIX, SUFFIX)") |
| 1410 | METH (done, "P.done() -> TAG") |
| 1411 | #undef METHNAME |
| 1412 | { 0 } |
| 1413 | }; |
| 1414 | |
| 1415 | static PyTypeObject poly1305cls_pytype_skel = { |
| 1416 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1417 | "Poly1305Class", /* @tp_name@ */ |
| 1418 | sizeof(PyHeapTypeObject), /* @tp_basicsize@ */ |
| 1419 | 0, /* @tp_itemsize@ */ |
| 1420 | |
| 1421 | 0, /* @tp_dealloc@ */ |
| 1422 | 0, /* @tp_print@ */ |
| 1423 | 0, /* @tp_getattr@ */ |
| 1424 | 0, /* @tp_setattr@ */ |
| 1425 | 0, /* @tp_compare@ */ |
| 1426 | 0, /* @tp_repr@ */ |
| 1427 | 0, /* @tp_as_number@ */ |
| 1428 | 0, /* @tp_as_sequence@ */ |
| 1429 | 0, /* @tp_as_mapping@ */ |
| 1430 | 0, /* @tp_hash@ */ |
| 1431 | 0, /* @tp_call@ */ |
| 1432 | 0, /* @tp_str@ */ |
| 1433 | 0, /* @tp_getattro@ */ |
| 1434 | 0, /* @tp_setattro@ */ |
| 1435 | 0, /* @tp_as_buffer@ */ |
| 1436 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 1437 | Py_TPFLAGS_BASETYPE, |
| 1438 | |
| 1439 | /* @tp_doc@ */ |
| 1440 | "Poly1305 metametaclass. Best not to ask.", |
| 1441 | |
| 1442 | 0, /* @tp_traverse@ */ |
| 1443 | 0, /* @tp_clear@ */ |
| 1444 | 0, /* @tp_richcompare@ */ |
| 1445 | 0, /* @tp_weaklistoffset@ */ |
| 1446 | 0, /* @tp_iter@ */ |
| 1447 | 0, /* @tp_iternext@ */ |
| 1448 | 0, /* @tp_methods@ */ |
| 1449 | 0, /* @tp_members@ */ |
| 1450 | poly1305cls_pygetset, /* @tp_getset@ */ |
| 1451 | 0, /* @tp_base@ */ |
| 1452 | 0, /* @tp_dict@ */ |
| 1453 | 0, /* @tp_descr_get@ */ |
| 1454 | 0, /* @tp_descr_set@ */ |
| 1455 | 0, /* @tp_dictoffset@ */ |
| 1456 | 0, /* @tp_init@ */ |
| 1457 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 1458 | abstract_pynew, /* @tp_new@ */ |
| 1459 | 0, /* @tp_free@ */ |
| 1460 | 0 /* @tp_is_gc@ */ |
| 1461 | }; |
| 1462 | |
| 1463 | static PyTypeObject poly1305key_pytype_skel = { |
| 1464 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1465 | "poly1305", /* @tp_name@ */ |
| 1466 | sizeof(poly1305key_pyobj), /* @tp_basicsize@ */ |
| 1467 | 0, /* @tp_itemsize@ */ |
| 1468 | |
| 1469 | 0, /* @tp_dealloc@ */ |
| 1470 | 0, /* @tp_print@ */ |
| 1471 | 0, /* @tp_getattr@ */ |
| 1472 | 0, /* @tp_setattr@ */ |
| 1473 | 0, /* @tp_compare@ */ |
| 1474 | 0, /* @tp_repr@ */ |
| 1475 | 0, /* @tp_as_number@ */ |
| 1476 | 0, /* @tp_as_sequence@ */ |
| 1477 | 0, /* @tp_as_mapping@ */ |
| 1478 | 0, /* @tp_hash@ */ |
| 1479 | 0, /* @tp_call@ */ |
| 1480 | 0, /* @tp_str@ */ |
| 1481 | 0, /* @tp_getattro@ */ |
| 1482 | 0, /* @tp_setattro@ */ |
| 1483 | 0, /* @tp_as_buffer@ */ |
| 1484 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 1485 | Py_TPFLAGS_BASETYPE, |
| 1486 | |
| 1487 | /* @tp_doc@ */ |
| 1488 | "Poly1305 key.", |
| 1489 | |
| 1490 | 0, /* @tp_traverse@ */ |
| 1491 | 0, /* @tp_clear@ */ |
| 1492 | 0, /* @tp_richcompare@ */ |
| 1493 | 0, /* @tp_weaklistoffset@ */ |
| 1494 | 0, /* @tp_iter@ */ |
| 1495 | 0, /* @tp_iternext@ */ |
| 1496 | 0, /* @tp_methods@ */ |
| 1497 | 0, /* @tp_members@ */ |
| 1498 | 0, /* @tp_getset@ */ |
| 1499 | 0, /* @tp_base@ */ |
| 1500 | 0, /* @tp_dict@ */ |
| 1501 | 0, /* @tp_descr_get@ */ |
| 1502 | 0, /* @tp_descr_set@ */ |
| 1503 | 0, /* @tp_dictoffset@ */ |
| 1504 | 0, /* @tp_init@ */ |
| 1505 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 1506 | poly1305key_pynew, /* @tp_new@ */ |
| 1507 | 0, /* @tp_free@ */ |
| 1508 | 0 /* @tp_is_gc@ */ |
| 1509 | }; |
| 1510 | |
| 1511 | static PyTypeObject poly1305hash_pytype_skel = { |
| 1512 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1513 | "Poly1305Hash", /* @tp_name@ */ |
| 1514 | sizeof(poly1305hash_pyobj), /* @tp_basicsize@ */ |
| 1515 | 0, /* @tp_itemsize@ */ |
| 1516 | |
| 1517 | 0, /* @tp_dealloc@ */ |
| 1518 | 0, /* @tp_print@ */ |
| 1519 | 0, /* @tp_getattr@ */ |
| 1520 | 0, /* @tp_setattr@ */ |
| 1521 | 0, /* @tp_compare@ */ |
| 1522 | 0, /* @tp_repr@ */ |
| 1523 | 0, /* @tp_as_number@ */ |
| 1524 | 0, /* @tp_as_sequence@ */ |
| 1525 | 0, /* @tp_as_mapping@ */ |
| 1526 | 0, /* @tp_hash@ */ |
| 1527 | 0, /* @tp_call@ */ |
| 1528 | 0, /* @tp_str@ */ |
| 1529 | 0, /* @tp_getattro@ */ |
| 1530 | 0, /* @tp_setattro@ */ |
| 1531 | 0, /* @tp_as_buffer@ */ |
| 1532 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 1533 | Py_TPFLAGS_BASETYPE, |
| 1534 | |
| 1535 | /* @tp_doc@ */ |
| 1536 | "Poly1305 MAC context base class.", |
| 1537 | |
| 1538 | 0, /* @tp_traverse@ */ |
| 1539 | 0, /* @tp_clear@ */ |
| 1540 | 0, /* @tp_richcompare@ */ |
| 1541 | 0, /* @tp_weaklistoffset@ */ |
| 1542 | 0, /* @tp_iter@ */ |
| 1543 | 0, /* @tp_iternext@ */ |
| 1544 | poly1305hash_pymethods, /* @tp_methods@ */ |
| 1545 | 0, /* @tp_members@ */ |
| 1546 | 0, /* @tp_getset@ */ |
| 1547 | 0, /* @tp_base@ */ |
| 1548 | 0, /* @tp_dict@ */ |
| 1549 | 0, /* @tp_descr_get@ */ |
| 1550 | 0, /* @tp_descr_set@ */ |
| 1551 | 0, /* @tp_dictoffset@ */ |
| 1552 | 0, /* @tp_init@ */ |
| 1553 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 1554 | abstract_pynew, /* @tp_new@ */ |
| 1555 | 0, /* @tp_free@ */ |
| 1556 | 0 /* @tp_is_gc@ */ |
| 1557 | }; |
| 1558 | |
| 1559 | /*----- Special snowflake for HSalsa and HChaCha --------------------------*/ |
| 1560 | |
| 1561 | #define DEF_HDANCE(DANCE, HDANCE, dance, hdance) \ |
| 1562 | static PyObject *meth_##hdance##_prf(PyObject *me, PyObject *arg) \ |
| 1563 | { \ |
| 1564 | dance##_ctx dance; \ |
| 1565 | char *k, *n; \ |
| 1566 | Py_ssize_t ksz, nsz; \ |
| 1567 | PyObject *rc; \ |
| 1568 | if (!PyArg_ParseTuple(arg, "s#s#:" #hdance "_prf", \ |
| 1569 | &k, &ksz, &n, &nsz)) \ |
| 1570 | goto end; \ |
| 1571 | if (ksz != DANCE##_KEYSZ) VALERR("bad key length"); \ |
| 1572 | if (nsz != HDANCE##_INSZ) VALERR("bad input length"); \ |
| 1573 | rc = bytestring_pywrap(0, HSALSA20_OUTSZ); \ |
| 1574 | dance##_init(&dance, k, ksz, 0); \ |
| 1575 | hdance##_prf(&dance, n, PyString_AS_STRING(rc)); \ |
| 1576 | return (rc); \ |
| 1577 | end: \ |
| 1578 | return (0); \ |
| 1579 | } |
| 1580 | |
| 1581 | DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa20) |
| 1582 | DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa2012) |
| 1583 | DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa208) |
| 1584 | |
| 1585 | DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha20) |
| 1586 | DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha12) |
| 1587 | DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha8) |
| 1588 | |
| 1589 | /*----- Keccak-p[1600, n] -------------------------------------------------*/ |
| 1590 | |
| 1591 | static PyTypeObject *kxvik_pytype; |
| 1592 | |
| 1593 | typedef struct kxvik_pyobj { |
| 1594 | PyObject_HEAD |
| 1595 | keccak1600_state s; |
| 1596 | unsigned n; |
| 1597 | } kxvik_pyobj; |
| 1598 | |
| 1599 | static PyObject *kxvik_pynew(PyTypeObject *ty, |
| 1600 | PyObject *arg, PyObject *kw) |
| 1601 | { |
| 1602 | unsigned n = 24; |
| 1603 | kxvik_pyobj *rc = 0; |
| 1604 | char *kwlist[] = { "nround", 0 }; |
| 1605 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", kwlist, |
| 1606 | convuint, &n)) |
| 1607 | goto end; |
| 1608 | rc = (kxvik_pyobj *)ty->tp_alloc(ty, 0); |
| 1609 | rc->n = n; |
| 1610 | keccak1600_init(&rc->s); |
| 1611 | end: |
| 1612 | return ((PyObject *)rc); |
| 1613 | } |
| 1614 | |
| 1615 | static PyObject *kxvikmeth_mix(PyObject *me, PyObject *arg) |
| 1616 | { |
| 1617 | kxvik_pyobj *k = (kxvik_pyobj *)me; |
| 1618 | kludge64 t[25]; |
| 1619 | const octet *q; |
| 1620 | octet buf[8]; |
| 1621 | unsigned i; |
| 1622 | char *p; Py_ssize_t n; |
| 1623 | |
| 1624 | if (!PyArg_ParseTuple(arg, "s#:mix", &p, &n)) goto end; |
| 1625 | if (n > 200) VALERR("out of range"); |
| 1626 | q = (const octet *)p; |
| 1627 | i = 0; |
| 1628 | while (n > 8) { LOAD64_L_(t[i], q); i++; q += 8; n -= 8; } |
| 1629 | if (n) { |
| 1630 | memcpy(buf, q, n); memset(buf + n, 0, 8 - n); |
| 1631 | LOAD64_L_(t[i], buf); i++; |
| 1632 | } |
| 1633 | keccak1600_mix(&k->s, t, i); |
| 1634 | RETURN_ME; |
| 1635 | end: |
| 1636 | return (0); |
| 1637 | } |
| 1638 | |
| 1639 | static PyObject *kxvikmeth_extract(PyObject *me, PyObject *arg) |
| 1640 | { |
| 1641 | kxvik_pyobj *k = (kxvik_pyobj *)me; |
| 1642 | PyObject *rc = 0; |
| 1643 | kludge64 t[25]; |
| 1644 | octet *q, buf[8]; |
| 1645 | unsigned i; |
| 1646 | unsigned n; |
| 1647 | |
| 1648 | if (!PyArg_ParseTuple(arg, "O&:mix", convuint, &n)) goto end; |
| 1649 | if (n > 200) VALERR("out of range"); |
| 1650 | rc = bytestring_pywrap(0, n); |
| 1651 | q = (octet *)PyString_AS_STRING(rc); |
| 1652 | keccak1600_extract(&k->s, t, (n + 7)/8); |
| 1653 | i = 0; |
| 1654 | while (n > 8) { STORE64_L_(q, t[i]); i++; q += 8; n -= 8; } |
| 1655 | if (n) { STORE64_L_(buf, t[i]); memcpy(q, buf, n); } |
| 1656 | end: |
| 1657 | return (rc); |
| 1658 | } |
| 1659 | |
| 1660 | static PyObject *kxvikmeth_step(PyObject *me, PyObject *arg) |
| 1661 | { |
| 1662 | kxvik_pyobj *k = (kxvik_pyobj *)me; |
| 1663 | if (!PyArg_ParseTuple(arg, ":step")) return (0); |
| 1664 | keccak1600_p(&k->s, &k->s, k->n); |
| 1665 | RETURN_ME; |
| 1666 | } |
| 1667 | |
| 1668 | static PyObject *kxvikget_nround(PyObject *me, void *hunoz) |
| 1669 | { |
| 1670 | kxvik_pyobj *k = (kxvik_pyobj *)me; |
| 1671 | return (PyInt_FromLong(k->n)); |
| 1672 | } |
| 1673 | |
| 1674 | static int kxvikset_nround(PyObject *me, PyObject *val, void *hunoz) |
| 1675 | { |
| 1676 | kxvik_pyobj *k = (kxvik_pyobj *)me; |
| 1677 | unsigned n; |
| 1678 | |
| 1679 | if (!convuint(val, &n)) return (-1); |
| 1680 | k->n = n; |
| 1681 | return (0); |
| 1682 | } |
| 1683 | |
| 1684 | static PyGetSetDef kxvik_pygetset[] = { |
| 1685 | #define GETSETNAME(op, name) kxvik##op##_##name |
| 1686 | GETSET(nround, "KECCAK.nround -> number of rounds") |
| 1687 | #undef GETSETNAME |
| 1688 | { 0 } |
| 1689 | }; |
| 1690 | |
| 1691 | static PyMethodDef kxvik_pymethods[] = { |
| 1692 | #define METHNAME(func) kxvikmeth_##func |
| 1693 | METH (mix, "KECCAK.mix(DATA)") |
| 1694 | METH (extract, "KECCAK.extract(NOCTETS)") |
| 1695 | METH (step, "KECCAK.step()") |
| 1696 | #undef METHNAME |
| 1697 | { 0 } |
| 1698 | }; |
| 1699 | |
| 1700 | static PyTypeObject kxvik_pytype_skel = { |
| 1701 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1702 | "Keccak1600", /* @tp_name@ */ |
| 1703 | sizeof(kxvik_pyobj), /* @tp_basicsize@ */ |
| 1704 | 0, /* @tp_itemsize@ */ |
| 1705 | |
| 1706 | 0, /* @tp_dealloc@ */ |
| 1707 | 0, /* @tp_print@ */ |
| 1708 | 0, /* @tp_getattr@ */ |
| 1709 | 0, /* @tp_setattr@ */ |
| 1710 | 0, /* @tp_compare@ */ |
| 1711 | 0, /* @tp_repr@ */ |
| 1712 | 0, /* @tp_as_number@ */ |
| 1713 | 0, /* @tp_as_sequence@ */ |
| 1714 | 0, /* @tp_as_mapping@ */ |
| 1715 | 0, /* @tp_hash@ */ |
| 1716 | 0, /* @tp_call@ */ |
| 1717 | 0, /* @tp_str@ */ |
| 1718 | 0, /* @tp_getattro@ */ |
| 1719 | 0, /* @tp_setattro@ */ |
| 1720 | 0, /* @tp_as_buffer@ */ |
| 1721 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 1722 | Py_TPFLAGS_BASETYPE, |
| 1723 | |
| 1724 | /* @tp_doc@ */ |
| 1725 | "Keccak-p[1600, n] state.", |
| 1726 | |
| 1727 | 0, /* @tp_traverse@ */ |
| 1728 | 0, /* @tp_clear@ */ |
| 1729 | 0, /* @tp_richcompare@ */ |
| 1730 | 0, /* @tp_weaklistoffset@ */ |
| 1731 | 0, /* @tp_iter@ */ |
| 1732 | 0, /* @tp_iternext@ */ |
| 1733 | kxvik_pymethods, /* @tp_methods@ */ |
| 1734 | 0, /* @tp_members@ */ |
| 1735 | kxvik_pygetset, /* @tp_getset@ */ |
| 1736 | 0, /* @tp_base@ */ |
| 1737 | 0, /* @tp_dict@ */ |
| 1738 | 0, /* @tp_descr_get@ */ |
| 1739 | 0, /* @tp_descr_set@ */ |
| 1740 | 0, /* @tp_dictoffset@ */ |
| 1741 | 0, /* @tp_init@ */ |
| 1742 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 1743 | kxvik_pynew, /* @tp_new@ */ |
| 1744 | 0, /* @tp_free@ */ |
| 1745 | 0 /* @tp_is_gc@ */ |
| 1746 | }; |
| 1747 | |
| 1748 | static PyTypeObject *shake_pytype, *shake128_pytype, *shake256_pytype; |
| 1749 | |
| 1750 | typedef struct shake_pyobj { |
| 1751 | PyObject_HEAD |
| 1752 | int st; |
| 1753 | shake_ctx h; |
| 1754 | } shake_pyobj; |
| 1755 | |
| 1756 | #define SHAKE_H(o) (&((shake_pyobj *)(o))->h) |
| 1757 | #define SHAKE_ST(o) (((shake_pyobj *)(o))->st) |
| 1758 | |
| 1759 | static PyObject *shake_dopynew(void (*initfn)(shake_ctx *, |
| 1760 | const void *, size_t, |
| 1761 | const void *, size_t), |
| 1762 | PyTypeObject *ty, |
| 1763 | PyObject *arg, PyObject *kw) |
| 1764 | { |
| 1765 | shake_pyobj *rc = 0; |
| 1766 | char *p = 0, *f = 0; |
| 1767 | Py_ssize_t psz = 0, fsz = 0; |
| 1768 | char *kwlist[] = { "perso", "func", 0 }; |
| 1769 | |
| 1770 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|s#s#:new", kwlist, |
| 1771 | &p, &psz, &f, &fsz)) |
| 1772 | goto end; |
| 1773 | rc = (shake_pyobj *)ty->tp_alloc(ty, 0); |
| 1774 | initfn(&rc->h, f, fsz, p, psz); |
| 1775 | rc->st = 0; |
| 1776 | end: |
| 1777 | return ((PyObject *)rc); |
| 1778 | } |
| 1779 | |
| 1780 | static PyObject *shake128_pynew(PyTypeObject *ty, |
| 1781 | PyObject *arg, PyObject *kw) |
| 1782 | { return (shake_dopynew(cshake128_init, ty, arg, kw)); } |
| 1783 | |
| 1784 | static PyObject *shake256_pynew(PyTypeObject *ty, |
| 1785 | PyObject *arg, PyObject *kw) |
| 1786 | { return (shake_dopynew(cshake256_init, ty, arg, kw)); } |
| 1787 | |
| 1788 | static int shake_check(PyObject *me, int st) |
| 1789 | { |
| 1790 | if (SHAKE_ST(me) != st) VALERR("wrong state"); |
| 1791 | return (0); |
| 1792 | end: |
| 1793 | return (-1); |
| 1794 | } |
| 1795 | |
| 1796 | static PyObject *shakemeth_hash(PyObject *me, PyObject *arg) |
| 1797 | { |
| 1798 | char *p; |
| 1799 | Py_ssize_t sz; |
| 1800 | if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0); |
| 1801 | if (shake_check(me, 0)) return (0); |
| 1802 | shake_hash(SHAKE_H(me), p, sz); |
| 1803 | RETURN_ME; |
| 1804 | } |
| 1805 | |
| 1806 | #define SHAKEMETH_HASHU_(n, W, w) \ |
| 1807 | static PyObject *shakemeth_hashu##w(PyObject *me, PyObject *arg) \ |
| 1808 | { \ |
| 1809 | uint##n x; \ |
| 1810 | octet b[SZ_##W]; \ |
| 1811 | if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \ |
| 1812 | if (shake_check(me, 0)) goto end; \ |
| 1813 | STORE##W(b, x); shake_hash(SHAKE_H(me), b, sizeof(b)); \ |
| 1814 | RETURN_ME; \ |
| 1815 | end: \ |
| 1816 | return (0); \ |
| 1817 | } |
| 1818 | DOUINTCONV(SHAKEMETH_HASHU_) |
| 1819 | |
| 1820 | #define SHAKEMETH_HASHBUF_(n, W, w) \ |
| 1821 | static PyObject *shakemeth_hashbuf##w(PyObject *me, PyObject *arg) \ |
| 1822 | { \ |
| 1823 | char *p; \ |
| 1824 | Py_ssize_t sz; \ |
| 1825 | octet b[SZ_##W]; \ |
| 1826 | if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \ |
| 1827 | if (sz > MASK##n) TYERR("string too long"); \ |
| 1828 | if (shake_check(me, 0)) goto end; \ |
| 1829 | STORE##W(b, sz); shake_hash(SHAKE_H(me), b, sizeof(b)); \ |
| 1830 | shake_hash(SHAKE_H(me), p, sz); \ |
| 1831 | RETURN_ME; \ |
| 1832 | end: \ |
| 1833 | return (0); \ |
| 1834 | } |
| 1835 | DOUINTCONV(SHAKEMETH_HASHBUF_) |
| 1836 | |
| 1837 | static PyObject *shakemeth_hashstrz(PyObject *me, PyObject *arg) |
| 1838 | { |
| 1839 | char *p; |
| 1840 | if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0); |
| 1841 | if (shake_check(me, 0)) return (0); |
| 1842 | shake_hash(SHAKE_H(me), p, strlen(p) + 1); |
| 1843 | RETURN_ME; |
| 1844 | } |
| 1845 | |
| 1846 | static PyObject *shakemeth_xof(PyObject *me, PyObject *arg) |
| 1847 | { |
| 1848 | if (!PyArg_ParseTuple(arg, ":xof")) goto end; |
| 1849 | if (shake_check(me, 0)) goto end; |
| 1850 | shake_xof(SHAKE_H(me)); |
| 1851 | SHAKE_ST(me) = 1; |
| 1852 | RETURN_ME; |
| 1853 | end: |
| 1854 | return (0); |
| 1855 | } |
| 1856 | |
| 1857 | static PyObject *shakemeth_done(PyObject *me, PyObject *arg) |
| 1858 | { |
| 1859 | PyObject *rc = 0; |
| 1860 | size_t n; |
| 1861 | if (!PyArg_ParseTuple(arg, "O&:done", convszt, &n)) goto end; |
| 1862 | if (shake_check(me, 0)) goto end; |
| 1863 | rc = bytestring_pywrap(0, n); |
| 1864 | shake_done(SHAKE_H(me), PyString_AS_STRING(rc), n); |
| 1865 | SHAKE_ST(me) = -1; |
| 1866 | end: |
| 1867 | return (rc); |
| 1868 | } |
| 1869 | |
| 1870 | static PyObject *shakemeth_copy(PyObject *me, PyObject *arg) |
| 1871 | { |
| 1872 | shake_pyobj *rc = 0; |
| 1873 | |
| 1874 | if (!PyArg_ParseTuple(arg, ":copy")) goto end; |
| 1875 | rc = PyObject_NEW(shake_pyobj, me->ob_type); |
| 1876 | rc->h = *SHAKE_H(me); |
| 1877 | rc->st = SHAKE_ST(me); |
| 1878 | end: |
| 1879 | return ((PyObject *)me); |
| 1880 | } |
| 1881 | |
| 1882 | static PyObject *shakemeth_get(PyObject *me, PyObject *arg) |
| 1883 | { |
| 1884 | PyObject *rc = 0; |
| 1885 | size_t sz; |
| 1886 | |
| 1887 | if (!PyArg_ParseTuple(arg, "O&:get", convszt, &sz)) goto end; |
| 1888 | if (shake_check(me, 1)) goto end; |
| 1889 | rc = bytestring_pywrap(0, sz); |
| 1890 | shake_get(SHAKE_H(me), PyString_AS_STRING(rc), sz); |
| 1891 | end: |
| 1892 | return (rc); |
| 1893 | } |
| 1894 | |
| 1895 | static PyObject *shakemeth_mask(PyObject *me, PyObject *arg) |
| 1896 | { |
| 1897 | PyObject *rc = 0; |
| 1898 | char *p; Py_ssize_t sz; |
| 1899 | |
| 1900 | if (!PyArg_ParseTuple(arg, "s#:mask", &p, &sz)) goto end; |
| 1901 | if (shake_check(me, 1)) goto end; |
| 1902 | rc = bytestring_pywrap(0, sz); |
| 1903 | shake_mask(SHAKE_H(me), p, PyString_AS_STRING(rc), sz); |
| 1904 | end: |
| 1905 | return (rc); |
| 1906 | } |
| 1907 | |
| 1908 | static PyObject *shakeget_rate(PyObject *me, void *hunoz) |
| 1909 | { return (PyInt_FromLong(SHAKE_H(me)->h.r)); } |
| 1910 | |
| 1911 | static PyObject *shakeget_buffered(PyObject *me, void *hunoz) |
| 1912 | { return (PyInt_FromLong(SHAKE_H(me)->h.n)); } |
| 1913 | |
| 1914 | static PyObject *shakeget_state(PyObject *me, void *hunoz) |
| 1915 | { |
| 1916 | int st = SHAKE_ST(me); |
| 1917 | return (PyString_FromString(st == 0 ? "absorb" : |
| 1918 | st == 1 ? "squeeze" : "dead")); |
| 1919 | } |
| 1920 | |
| 1921 | static PyGetSetDef shake_pygetset[] = { |
| 1922 | #define GETSETNAME(op, name) shake##op##_##name |
| 1923 | GET (rate, "S.rate -> rate, in bytes") |
| 1924 | GET (buffered, "S.buffered -> amount currently buffered") |
| 1925 | GET (state, "S.state -> `absorb', `squeeze', `dead'") |
| 1926 | #undef GETSETNAME |
| 1927 | { 0 } |
| 1928 | }; |
| 1929 | |
| 1930 | static PyMethodDef shake_pymethods[] = { |
| 1931 | #define METHNAME(func) shakemeth_##func |
| 1932 | METH (copy, "S.copy() -> SS") |
| 1933 | METH (hash, "S.hash(M)") |
| 1934 | #define METHU_(n, W, w) METH(hashu##w, "S.hashu" #w "(WORD)") |
| 1935 | DOUINTCONV(METHU_) |
| 1936 | #undef METHU_ |
| 1937 | #define METHBUF_(n, W, w) METH(hashbuf##w, "S.hashbuf" #w "(BYTES)") |
| 1938 | DOUINTCONV(METHBUF_) |
| 1939 | #undef METHBUF_ |
| 1940 | METH (hashstrz, "S.hashstrz(STRING)") |
| 1941 | METH (xof, "S.xof()") |
| 1942 | METH (done, "S.done(LEN) ->H") |
| 1943 | METH (get, "S.get(LEN) -> H") |
| 1944 | METH (mask, "S.mask(M) -> C") |
| 1945 | #undef METHNAME |
| 1946 | { 0 } |
| 1947 | }; |
| 1948 | |
| 1949 | static PyTypeObject shake_pytype_skel = { |
| 1950 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1951 | "Shake", /* @tp_name@ */ |
| 1952 | sizeof(shake_pyobj), /* @tp_basicsize@ */ |
| 1953 | 0, /* @tp_itemsize@ */ |
| 1954 | |
| 1955 | 0, /* @tp_dealloc@ */ |
| 1956 | 0, /* @tp_print@ */ |
| 1957 | 0, /* @tp_getattr@ */ |
| 1958 | 0, /* @tp_setattr@ */ |
| 1959 | 0, /* @tp_compare@ */ |
| 1960 | 0, /* @tp_repr@ */ |
| 1961 | 0, /* @tp_as_number@ */ |
| 1962 | 0, /* @tp_as_sequence@ */ |
| 1963 | 0, /* @tp_as_mapping@ */ |
| 1964 | 0, /* @tp_hash@ */ |
| 1965 | 0, /* @tp_call@ */ |
| 1966 | 0, /* @tp_str@ */ |
| 1967 | 0, /* @tp_getattro@ */ |
| 1968 | 0, /* @tp_setattro@ */ |
| 1969 | 0, /* @tp_as_buffer@ */ |
| 1970 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 1971 | Py_TPFLAGS_BASETYPE, |
| 1972 | |
| 1973 | /* @tp_doc@ */ |
| 1974 | "SHAKE/cSHAKE base class.", |
| 1975 | |
| 1976 | 0, /* @tp_traverse@ */ |
| 1977 | 0, /* @tp_clear@ */ |
| 1978 | 0, /* @tp_richcompare@ */ |
| 1979 | 0, /* @tp_weaklistoffset@ */ |
| 1980 | 0, /* @tp_iter@ */ |
| 1981 | 0, /* @tp_iternext@ */ |
| 1982 | shake_pymethods, /* @tp_methods@ */ |
| 1983 | 0, /* @tp_members@ */ |
| 1984 | shake_pygetset, /* @tp_getset@ */ |
| 1985 | 0, /* @tp_base@ */ |
| 1986 | 0, /* @tp_dict@ */ |
| 1987 | 0, /* @tp_descr_get@ */ |
| 1988 | 0, /* @tp_descr_set@ */ |
| 1989 | 0, /* @tp_dictoffset@ */ |
| 1990 | 0, /* @tp_init@ */ |
| 1991 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 1992 | abstract_pynew, /* @tp_new@ */ |
| 1993 | 0, /* @tp_free@ */ |
| 1994 | 0 /* @tp_is_gc@ */ |
| 1995 | }; |
| 1996 | |
| 1997 | static PyTypeObject shake128_pytype_skel = { |
| 1998 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1999 | "Shake128", /* @tp_name@ */ |
| 2000 | 0, /* @tp_basicsize@ */ |
| 2001 | 0, /* @tp_itemsize@ */ |
| 2002 | |
| 2003 | 0, /* @tp_dealloc@ */ |
| 2004 | 0, /* @tp_print@ */ |
| 2005 | 0, /* @tp_getattr@ */ |
| 2006 | 0, /* @tp_setattr@ */ |
| 2007 | 0, /* @tp_compare@ */ |
| 2008 | 0, /* @tp_repr@ */ |
| 2009 | 0, /* @tp_as_number@ */ |
| 2010 | 0, /* @tp_as_sequence@ */ |
| 2011 | 0, /* @tp_as_mapping@ */ |
| 2012 | 0, /* @tp_hash@ */ |
| 2013 | 0, /* @tp_call@ */ |
| 2014 | 0, /* @tp_str@ */ |
| 2015 | 0, /* @tp_getattro@ */ |
| 2016 | 0, /* @tp_setattro@ */ |
| 2017 | 0, /* @tp_as_buffer@ */ |
| 2018 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 2019 | Py_TPFLAGS_BASETYPE, |
| 2020 | |
| 2021 | /* @tp_doc@ */ |
| 2022 | "SHAKE128/cSHAKE128 XOF.", |
| 2023 | |
| 2024 | 0, /* @tp_traverse@ */ |
| 2025 | 0, /* @tp_clear@ */ |
| 2026 | 0, /* @tp_richcompare@ */ |
| 2027 | 0, /* @tp_weaklistoffset@ */ |
| 2028 | 0, /* @tp_iter@ */ |
| 2029 | 0, /* @tp_iternext@ */ |
| 2030 | 0, /* @tp_methods@ */ |
| 2031 | 0, /* @tp_members@ */ |
| 2032 | 0, /* @tp_getset@ */ |
| 2033 | 0, /* @tp_base@ */ |
| 2034 | 0, /* @tp_dict@ */ |
| 2035 | 0, /* @tp_descr_get@ */ |
| 2036 | 0, /* @tp_descr_set@ */ |
| 2037 | 0, /* @tp_dictoffset@ */ |
| 2038 | 0, /* @tp_init@ */ |
| 2039 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 2040 | shake128_pynew, /* @tp_new@ */ |
| 2041 | 0, /* @tp_free@ */ |
| 2042 | 0 /* @tp_is_gc@ */ |
| 2043 | }; |
| 2044 | |
| 2045 | static PyTypeObject shake256_pytype_skel = { |
| 2046 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 2047 | "Shake256", /* @tp_name@ */ |
| 2048 | 0, /* @tp_basicsize@ */ |
| 2049 | 0, /* @tp_itemsize@ */ |
| 2050 | |
| 2051 | 0, /* @tp_dealloc@ */ |
| 2052 | 0, /* @tp_print@ */ |
| 2053 | 0, /* @tp_getattr@ */ |
| 2054 | 0, /* @tp_setattr@ */ |
| 2055 | 0, /* @tp_compare@ */ |
| 2056 | 0, /* @tp_repr@ */ |
| 2057 | 0, /* @tp_as_number@ */ |
| 2058 | 0, /* @tp_as_sequence@ */ |
| 2059 | 0, /* @tp_as_mapping@ */ |
| 2060 | 0, /* @tp_hash@ */ |
| 2061 | 0, /* @tp_call@ */ |
| 2062 | 0, /* @tp_str@ */ |
| 2063 | 0, /* @tp_getattro@ */ |
| 2064 | 0, /* @tp_setattro@ */ |
| 2065 | 0, /* @tp_as_buffer@ */ |
| 2066 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 2067 | Py_TPFLAGS_BASETYPE, |
| 2068 | |
| 2069 | /* @tp_doc@ */ |
| 2070 | "SHAKE256/cSHAKE256 XOF.", |
| 2071 | |
| 2072 | 0, /* @tp_traverse@ */ |
| 2073 | 0, /* @tp_clear@ */ |
| 2074 | 0, /* @tp_richcompare@ */ |
| 2075 | 0, /* @tp_weaklistoffset@ */ |
| 2076 | 0, /* @tp_iter@ */ |
| 2077 | 0, /* @tp_iternext@ */ |
| 2078 | 0, /* @tp_methods@ */ |
| 2079 | 0, /* @tp_members@ */ |
| 2080 | 0, /* @tp_getset@ */ |
| 2081 | 0, /* @tp_base@ */ |
| 2082 | 0, /* @tp_dict@ */ |
| 2083 | 0, /* @tp_descr_get@ */ |
| 2084 | 0, /* @tp_descr_set@ */ |
| 2085 | 0, /* @tp_dictoffset@ */ |
| 2086 | 0, /* @tp_init@ */ |
| 2087 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 2088 | shake256_pynew, /* @tp_new@ */ |
| 2089 | 0, /* @tp_free@ */ |
| 2090 | 0 /* @tp_is_gc@ */ |
| 2091 | }; |
| 2092 | |
| 2093 | /*----- Pseudorandom permutations -----------------------------------------*/ |
| 2094 | |
| 2095 | static PyTypeObject *gcprp_pytype, *gprp_pytype; |
| 2096 | |
| 2097 | typedef struct prpinfo { |
| 2098 | const char *name; |
| 2099 | const octet *keysz; |
| 2100 | size_t ctxsz; |
| 2101 | size_t blksz; |
| 2102 | void (*init)(void *, const void *, size_t); |
| 2103 | void (*eblk)(void *, const void *, void *); |
| 2104 | void (*dblk)(void *, const void *, void *); |
| 2105 | } prpinfo; |
| 2106 | |
| 2107 | #define PRP_DEF(PRE, pre) \ |
| 2108 | static void pre##_prpinit(void *ctx, const void *k, size_t ksz) \ |
| 2109 | { pre##_init(ctx, k, ksz); } \ |
| 2110 | static void pre##_prpeblk(void *ctx, const void *in, void *out) \ |
| 2111 | { \ |
| 2112 | uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \ |
| 2113 | pre##_eblk(ctx, w, w); BLKC_STORE(PRE, out, w); \ |
| 2114 | } \ |
| 2115 | static void pre##_prpdblk(void *ctx, const void *in, void *out) \ |
| 2116 | { \ |
| 2117 | uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \ |
| 2118 | pre##_dblk(ctx, w, w); BLKC_STORE(PRE, out, w); \ |
| 2119 | } \ |
| 2120 | static const prpinfo pre##_prpinfo = { \ |
| 2121 | #pre, pre##_keysz, sizeof(pre##_ctx), PRE##_BLKSZ, \ |
| 2122 | pre##_prpinit, pre##_prpeblk, pre##_prpdblk \ |
| 2123 | }; |
| 2124 | PRPS(PRP_DEF) |
| 2125 | |
| 2126 | static const struct prpinfo *const gprptab[] = { |
| 2127 | #define PRP_ENTRY(PRE, pre) &pre##_prpinfo, |
| 2128 | PRPS(PRP_ENTRY) |
| 2129 | 0 |
| 2130 | }; |
| 2131 | |
| 2132 | typedef struct gcprp_pyobj { |
| 2133 | PyHeapTypeObject ty; |
| 2134 | const prpinfo *prp; |
| 2135 | } gcprp_pyobj; |
| 2136 | #define GCPRP_PRP(o) (((gcprp_pyobj *)(o))->prp) |
| 2137 | |
| 2138 | typedef struct gprp_pyobj { |
| 2139 | PyObject_HEAD |
| 2140 | const prpinfo *prp; |
| 2141 | } gprp_pyobj; |
| 2142 | #define GPRP_PRP(o) (((gprp_pyobj *)(o))->prp) |
| 2143 | #define GPRP_CTX(o) (((gprp_pyobj *)(o)) + 1) |
| 2144 | |
| 2145 | typedef struct prp { |
| 2146 | const prpinfo *prp; |
| 2147 | void *ctx; |
| 2148 | } prp; |
| 2149 | |
| 2150 | static PyObject *gprp_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) |
| 2151 | { |
| 2152 | char *kwlist[] = { "key", 0 }; |
| 2153 | char *k; |
| 2154 | Py_ssize_t sz; |
| 2155 | const prpinfo *prp = GCPRP_PRP(ty); |
| 2156 | PyObject *me; |
| 2157 | |
| 2158 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz)) |
| 2159 | goto end; |
| 2160 | if (keysz(sz, prp->keysz) != sz) VALERR("bad key length"); |
| 2161 | me = (PyObject *)ty->tp_alloc(ty, 0); |
| 2162 | GPRP_PRP(me) = prp; |
| 2163 | prp->init(GPRP_CTX(me), k, sz); |
| 2164 | Py_INCREF(me); |
| 2165 | return (me); |
| 2166 | end: |
| 2167 | return (0); |
| 2168 | } |
| 2169 | |
| 2170 | static void gprp_pydealloc(PyObject *me) |
| 2171 | { Py_DECREF(me->ob_type); FREEOBJ(me); } |
| 2172 | |
| 2173 | static PyObject *gcprp_pywrap(const prpinfo *prp) |
| 2174 | { |
| 2175 | gcprp_pyobj *g = newtype(gcprp_pytype, 0, prp->name); |
| 2176 | g->prp = prp; |
| 2177 | g->ty.ht_type.tp_basicsize = sizeof(gprp_pyobj) + prp->ctxsz; |
| 2178 | g->ty.ht_type.tp_base = gprp_pytype; |
| 2179 | Py_INCREF(gprp_pytype); |
| 2180 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
| 2181 | Py_TPFLAGS_BASETYPE | |
| 2182 | Py_TPFLAGS_HEAPTYPE); |
| 2183 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; |
| 2184 | g->ty.ht_type.tp_free = 0; |
| 2185 | g->ty.ht_type.tp_new = gprp_pynew; |
| 2186 | typeready(&g->ty.ht_type); |
| 2187 | return ((PyObject *)g); |
| 2188 | } |
| 2189 | |
| 2190 | static PyObject *gcpget_name(PyObject *me, void *hunoz) |
| 2191 | { return (PyString_FromString(GCPRP_PRP(me)->name)); } |
| 2192 | static PyObject *gcpget_keysz(PyObject *me, void *hunoz) |
| 2193 | { return (keysz_pywrap(GCPRP_PRP(me)->keysz)); } |
| 2194 | static PyObject *gcpget_blksz(PyObject *me, void *hunoz) |
| 2195 | { return (PyInt_FromLong(GCPRP_PRP(me)->blksz)); } |
| 2196 | |
| 2197 | static PyObject *gpmeth_encrypt(PyObject *me, PyObject *arg) |
| 2198 | { |
| 2199 | char *p; |
| 2200 | Py_ssize_t n; |
| 2201 | PyObject *rc = 0; |
| 2202 | |
| 2203 | if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &n)) goto end; |
| 2204 | if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length"); |
| 2205 | rc = bytestring_pywrap(0, n); |
| 2206 | GPRP_PRP(me)->eblk(GPRP_CTX(me), p, PyString_AS_STRING(rc)); |
| 2207 | end: |
| 2208 | return (rc); |
| 2209 | } |
| 2210 | |
| 2211 | static PyObject *gpmeth_decrypt(PyObject *me, PyObject *arg) |
| 2212 | { |
| 2213 | char *p; |
| 2214 | Py_ssize_t n; |
| 2215 | PyObject *rc = 0; |
| 2216 | |
| 2217 | if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &n)) goto end; |
| 2218 | if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length"); |
| 2219 | rc = bytestring_pywrap(0, n); |
| 2220 | GPRP_PRP(me)->dblk(GPRP_CTX(me), p, PyString_AS_STRING(rc)); |
| 2221 | end: |
| 2222 | return (rc); |
| 2223 | } |
| 2224 | |
| 2225 | static PyGetSetDef gcprp_pygetset[] = { |
| 2226 | #define GETSETNAME(op, name) gcp##op##_##name |
| 2227 | GET (keysz, "CP.keysz -> acceptable key sizes") |
| 2228 | GET (blksz, "CP.blksz -> block size") |
| 2229 | GET (name, "CP.name -> name of this kind of PRP") |
| 2230 | #undef GETSETNAME |
| 2231 | { 0 } |
| 2232 | }; |
| 2233 | |
| 2234 | static PyMethodDef gprp_pymethods[] = { |
| 2235 | #define METHNAME(name) gpmeth_##name |
| 2236 | METH (encrypt, "P.encrypt(PT) -> CT") |
| 2237 | METH (decrypt, "P.decrypt(CT) -> PT") |
| 2238 | #undef METHNAME |
| 2239 | { 0 } |
| 2240 | }; |
| 2241 | |
| 2242 | static PyTypeObject gcprp_pytype_skel = { |
| 2243 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 2244 | "GCPRP", /* @tp_name@ */ |
| 2245 | sizeof(gcprp_pyobj), /* @tp_basicsize@ */ |
| 2246 | 0, /* @tp_itemsize@ */ |
| 2247 | |
| 2248 | 0, /* @tp_dealloc@ */ |
| 2249 | 0, /* @tp_print@ */ |
| 2250 | 0, /* @tp_getattr@ */ |
| 2251 | 0, /* @tp_setattr@ */ |
| 2252 | 0, /* @tp_compare@ */ |
| 2253 | 0, /* @tp_repr@ */ |
| 2254 | 0, /* @tp_as_number@ */ |
| 2255 | 0, /* @tp_as_sequence@ */ |
| 2256 | 0, /* @tp_as_mapping@ */ |
| 2257 | 0, /* @tp_hash@ */ |
| 2258 | 0, /* @tp_call@ */ |
| 2259 | 0, /* @tp_str@ */ |
| 2260 | 0, /* @tp_getattro@ */ |
| 2261 | 0, /* @tp_setattro@ */ |
| 2262 | 0, /* @tp_as_buffer@ */ |
| 2263 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 2264 | Py_TPFLAGS_BASETYPE, |
| 2265 | |
| 2266 | /* @tp_doc@ */ |
| 2267 | "Pseudorandom permutation metaclass.", |
| 2268 | |
| 2269 | 0, /* @tp_traverse@ */ |
| 2270 | 0, /* @tp_clear@ */ |
| 2271 | 0, /* @tp_richcompare@ */ |
| 2272 | 0, /* @tp_weaklistoffset@ */ |
| 2273 | 0, /* @tp_iter@ */ |
| 2274 | 0, /* @tp_iternext@ */ |
| 2275 | 0, /* @tp_methods@ */ |
| 2276 | 0, /* @tp_members@ */ |
| 2277 | gcprp_pygetset, /* @tp_getset@ */ |
| 2278 | 0, /* @tp_base@ */ |
| 2279 | 0, /* @tp_dict@ */ |
| 2280 | 0, /* @tp_descr_get@ */ |
| 2281 | 0, /* @tp_descr_set@ */ |
| 2282 | 0, /* @tp_dictoffset@ */ |
| 2283 | 0, /* @tp_init@ */ |
| 2284 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 2285 | abstract_pynew, /* @tp_new@ */ |
| 2286 | 0, /* @tp_free@ */ |
| 2287 | 0 /* @tp_is_gc@ */ |
| 2288 | }; |
| 2289 | |
| 2290 | static PyTypeObject gprp_pytype_skel = { |
| 2291 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 2292 | "GPRP", /* @tp_name@ */ |
| 2293 | sizeof(gprp_pyobj), /* @tp_basicsize@ */ |
| 2294 | 0, /* @tp_itemsize@ */ |
| 2295 | |
| 2296 | gprp_pydealloc, /* @tp_dealloc@ */ |
| 2297 | 0, /* @tp_print@ */ |
| 2298 | 0, /* @tp_getattr@ */ |
| 2299 | 0, /* @tp_setattr@ */ |
| 2300 | 0, /* @tp_compare@ */ |
| 2301 | 0, /* @tp_repr@ */ |
| 2302 | 0, /* @tp_as_number@ */ |
| 2303 | 0, /* @tp_as_sequence@ */ |
| 2304 | 0, /* @tp_as_mapping@ */ |
| 2305 | 0, /* @tp_hash@ */ |
| 2306 | 0, /* @tp_call@ */ |
| 2307 | 0, /* @tp_str@ */ |
| 2308 | 0, /* @tp_getattro@ */ |
| 2309 | 0, /* @tp_setattro@ */ |
| 2310 | 0, /* @tp_as_buffer@ */ |
| 2311 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 2312 | Py_TPFLAGS_BASETYPE, |
| 2313 | |
| 2314 | /* @tp_doc@ */ |
| 2315 | "Pseudorandom permutation, abstract base class.", |
| 2316 | |
| 2317 | 0, /* @tp_traverse@ */ |
| 2318 | 0, /* @tp_clear@ */ |
| 2319 | 0, /* @tp_richcompare@ */ |
| 2320 | 0, /* @tp_weaklistoffset@ */ |
| 2321 | 0, /* @tp_iter@ */ |
| 2322 | 0, /* @tp_iternext@ */ |
| 2323 | gprp_pymethods, /* @tp_methods@ */ |
| 2324 | 0, /* @tp_members@ */ |
| 2325 | 0, /* @tp_getset@ */ |
| 2326 | 0, /* @tp_base@ */ |
| 2327 | 0, /* @tp_dict@ */ |
| 2328 | 0, /* @tp_descr_get@ */ |
| 2329 | 0, /* @tp_descr_set@ */ |
| 2330 | 0, /* @tp_dictoffset@ */ |
| 2331 | 0, /* @tp_init@ */ |
| 2332 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 2333 | abstract_pynew, /* @tp_new@ */ |
| 2334 | 0, /* @tp_free@ */ |
| 2335 | 0 /* @tp_is_gc@ */ |
| 2336 | }; |
| 2337 | |
| 2338 | /*----- Main code ---------------------------------------------------------*/ |
| 2339 | |
| 2340 | static PyMethodDef methods[] = { |
| 2341 | #define METHNAME(func) meth_##func |
| 2342 | METH (_KeySZ_fromdl, "\ |
| 2343 | fromdl(N) -> M: convert integer discrete log field size to work factor") |
| 2344 | METH (_KeySZ_fromschnorr, "\ |
| 2345 | fromschnorr(N) -> M: convert Schnorr group order to work factor") |
| 2346 | METH (_KeySZ_fromif, "\ |
| 2347 | fromif(N) -> M: convert integer factorization problem size to work factor") |
| 2348 | METH (_KeySZ_fromec, "\ |
| 2349 | fromec(N) -> M: convert elliptic curve group order to work factor") |
| 2350 | METH (_KeySZ_todl, "\ |
| 2351 | todl(N) -> M: convert work factor to integer discrete log field size") |
| 2352 | METH (_KeySZ_toschnorr, "\ |
| 2353 | toschnorr(N) -> M: convert work factor to Schnorr group order") |
| 2354 | METH (_KeySZ_toif, "\ |
| 2355 | toif(N) -> M: convert work factor to integer factorization problem size") |
| 2356 | METH (_KeySZ_toec, "\ |
| 2357 | toec(N) -> M: convert work factor to elliptic curve group order") |
| 2358 | METH (_KeySZ_toec, "\ |
| 2359 | toec(N) -> M: convert work factor to elliptic curve group order") |
| 2360 | #define METH_HDANCE(hdance, HDance) METH(hdance##_prf, "\ |
| 2361 | " #hdance "_prf(K, N) -> H: calculate " HDance " hash of N with K") |
| 2362 | METH_HDANCE(hsalsa20, "HSalsa20") |
| 2363 | METH_HDANCE(hsalsa2012, "HSalsa20/12") |
| 2364 | METH_HDANCE(hsalsa208, "HSalsa20/8") |
| 2365 | METH_HDANCE(hchacha20, "HChaCha20") |
| 2366 | METH_HDANCE(hchacha12, "HChaCha12") |
| 2367 | METH_HDANCE(hchacha8, "HChaCha8") |
| 2368 | #undef METH_DANCE |
| 2369 | #undef METHNAME |
| 2370 | { 0 } |
| 2371 | }; |
| 2372 | |
| 2373 | void algorithms_pyinit(void) |
| 2374 | { |
| 2375 | INITTYPE(keysz, root); |
| 2376 | INITTYPE(keyszany, keysz); |
| 2377 | INITTYPE(keyszrange, keysz); |
| 2378 | INITTYPE(keyszset, keysz); |
| 2379 | INITTYPE(gccipher, type); |
| 2380 | INITTYPE(gcipher, root); |
| 2381 | INITTYPE(gchash, type); |
| 2382 | INITTYPE(ghash, root); |
| 2383 | INITTYPE(gcmac, type); |
| 2384 | INITTYPE(gmac, type); |
| 2385 | INITTYPE(gmhash, ghash); |
| 2386 | INITTYPE(poly1305cls, type); |
| 2387 | INITTYPE_META(poly1305key, type, poly1305cls); |
| 2388 | INITTYPE(poly1305hash, root); |
| 2389 | INITTYPE(kxvik, root); |
| 2390 | INITTYPE(shake, root); |
| 2391 | INITTYPE(shake128, shake); |
| 2392 | INITTYPE(shake256, shake); |
| 2393 | INITTYPE(gcprp, type); |
| 2394 | INITTYPE(gprp, root); |
| 2395 | addmethods(methods); |
| 2396 | } |
| 2397 | |
| 2398 | GEN(gcciphers, cipher) |
| 2399 | GEN(gchashes, hash) |
| 2400 | GEN(gcmacs, mac) |
| 2401 | #define gcprp prpinfo |
| 2402 | GEN(gcprps, prp) |
| 2403 | |
| 2404 | void algorithms_pyinsert(PyObject *mod) |
| 2405 | { |
| 2406 | PyObject *d; |
| 2407 | INSERT("KeySZ", keysz_pytype); |
| 2408 | INSERT("KeySZAny", keyszany_pytype); |
| 2409 | INSERT("KeySZRange", keyszrange_pytype); |
| 2410 | INSERT("KeySZSet", keyszset_pytype); |
| 2411 | INSERT("GCCipher", gccipher_pytype); |
| 2412 | INSERT("GCipher", gcipher_pytype); |
| 2413 | INSERT("gcciphers", gcciphers()); |
| 2414 | INSERT("GCHash", gchash_pytype); |
| 2415 | INSERT("GHash", ghash_pytype); |
| 2416 | INSERT("gchashes", d = gchashes()); |
| 2417 | sha_pyobj = PyDict_GetItemString(d, "sha"); Py_INCREF(sha_pyobj); |
| 2418 | has160_pyobj = PyDict_GetItemString(d, "has160"); Py_INCREF(has160_pyobj); |
| 2419 | INSERT("GCMAC", gcmac_pytype); |
| 2420 | INSERT("GMAC", gmac_pytype); |
| 2421 | INSERT("GMACHash", gmhash_pytype); |
| 2422 | INSERT("gcmacs", gcmacs()); |
| 2423 | INSERT("Poly1305Class", poly1305cls_pytype); |
| 2424 | INSERT("poly1305", poly1305key_pytype); |
| 2425 | INSERT("Poly1305Hash", poly1305hash_pytype); |
| 2426 | INSERT("Keccak1600", kxvik_pytype); |
| 2427 | INSERT("Shake", shake_pytype); |
| 2428 | INSERT("Shake128", shake128_pytype); |
| 2429 | INSERT("Shake256", shake256_pytype); |
| 2430 | INSERT("GCPRP", gcprp_pytype); |
| 2431 | INSERT("GPRP", gprp_pytype); |
| 2432 | INSERT("gcprps", gcprps()); |
| 2433 | } |
| 2434 | |
| 2435 | /*----- That's all, folks -------------------------------------------------*/ |