| 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 | PyObject *keysz_pywrap(const octet *k) |
| 39 | { |
| 40 | switch (k[0]) { |
| 41 | case KSZ_ANY: { |
| 42 | keysz_pyobj *o = PyObject_New(keysz_pyobj, keyszany_pytype); |
| 43 | o->dfl = k[1]; |
| 44 | return ((PyObject *)o); |
| 45 | } break; |
| 46 | case KSZ_RANGE: { |
| 47 | keyszrange_pyobj *o = |
| 48 | PyObject_New(keyszrange_pyobj, keyszrange_pytype); |
| 49 | o->dfl = k[1]; |
| 50 | o->min = k[2]; |
| 51 | o->max = k[3]; |
| 52 | o->mod = k[4]; |
| 53 | if (!o->mod) o->mod = 1; |
| 54 | return ((PyObject *)o); |
| 55 | } break; |
| 56 | case KSZ_SET: { |
| 57 | keyszset_pyobj *o = |
| 58 | PyObject_New(keyszset_pyobj, keyszset_pytype); |
| 59 | int i, n; |
| 60 | o->dfl = k[1]; |
| 61 | for (i = 0; k[i + 1]; i++) ; |
| 62 | n = i; o->set = PyTuple_New(n); |
| 63 | for (i = 0; i < n; i++) |
| 64 | PyTuple_SET_ITEM(o->set, i, PyInt_FromLong(k[i + 1])); |
| 65 | return ((PyObject *)o); |
| 66 | } break; |
| 67 | default: |
| 68 | abort(); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | static PyObject *keyszany_pynew(PyTypeObject *ty, |
| 73 | PyObject *arg, PyObject *kw) |
| 74 | { |
| 75 | char *kwlist[] = { "default", 0 }; |
| 76 | int dfl; |
| 77 | keysz_pyobj *o; |
| 78 | |
| 79 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "i:new", kwlist, &dfl)) |
| 80 | goto end; |
| 81 | if (dfl < 0) VALERR("key size cannot be negative"); |
| 82 | o = (keysz_pyobj *)ty->tp_alloc(ty, 0); |
| 83 | o->dfl = dfl; |
| 84 | return ((PyObject *)o); |
| 85 | end: |
| 86 | return (0); |
| 87 | } |
| 88 | |
| 89 | static PyObject *keyszrange_pynew(PyTypeObject *ty, |
| 90 | PyObject *arg, PyObject *kw) |
| 91 | { |
| 92 | char *kwlist[] = { "default", "min", "max", "mod", 0 }; |
| 93 | int dfl, min = 0, max = 0, mod = 1; |
| 94 | keyszrange_pyobj *o; |
| 95 | |
| 96 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|iii:new", kwlist, |
| 97 | &dfl, &min, &max, &mod)) |
| 98 | goto end; |
| 99 | if (dfl < 0 || min < 0 || max < 0) |
| 100 | VALERR("key size cannot be negative"); |
| 101 | if (min > dfl || (max && dfl > max)) |
| 102 | VALERR("bad key size bounds"); |
| 103 | if (mod <= 0 || dfl % mod || min % mod || max % mod) |
| 104 | VALERR("bad key size modulus"); |
| 105 | o = (keyszrange_pyobj *)ty->tp_alloc(ty, 0); |
| 106 | o->dfl = dfl; |
| 107 | o->min = min; |
| 108 | o->max = max; |
| 109 | o->mod = mod; |
| 110 | return ((PyObject *)o); |
| 111 | end: |
| 112 | return (0); |
| 113 | } |
| 114 | |
| 115 | static PyObject *keyszset_pynew(PyTypeObject *ty, |
| 116 | PyObject *arg, PyObject *kw) |
| 117 | { |
| 118 | char *kwlist[] = { "default", "set", 0 }; |
| 119 | int dfl, i, n, xx; |
| 120 | PyObject *set = 0; |
| 121 | PyObject *x = 0, *l = 0; |
| 122 | keyszset_pyobj *o = 0; |
| 123 | |
| 124 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|O:new", kwlist, |
| 125 | &dfl, &set)) |
| 126 | goto end; |
| 127 | if (!set) set = PyTuple_New(0); |
| 128 | else Py_INCREF(set); |
| 129 | if (!PySequence_Check(set)) TYERR("want a sequence"); |
| 130 | n = PySequence_Size(set); |
| 131 | l = PyList_New(0); |
| 132 | if (PyErr_Occurred()) goto end; |
| 133 | if (dfl < 0) VALERR("key size cannot be negative"); |
| 134 | x = PyInt_FromLong(dfl); |
| 135 | PyList_Append(l, x); |
| 136 | Py_DECREF(x); |
| 137 | x = 0; |
| 138 | for (i = 0; i < n; i++) { |
| 139 | if ((x = PySequence_GetItem(set, i)) == 0) goto end; |
| 140 | xx = PyInt_AsLong(x); |
| 141 | if (PyErr_Occurred()) goto end; |
| 142 | if (xx == dfl) continue; |
| 143 | if (xx < 0) VALERR("key size cannot be negative"); |
| 144 | PyList_Append(l, x); |
| 145 | Py_DECREF(x); |
| 146 | x = 0; |
| 147 | } |
| 148 | Py_DECREF(set); |
| 149 | if ((set = PySequence_Tuple(l)) == 0) goto end; |
| 150 | o = (keyszset_pyobj *)ty->tp_alloc(ty, 0); |
| 151 | o->dfl = dfl; |
| 152 | o->set = set; |
| 153 | Py_INCREF(set); |
| 154 | end: |
| 155 | Py_XDECREF(set); |
| 156 | Py_XDECREF(l); |
| 157 | Py_XDECREF(x); |
| 158 | return ((PyObject *)o); |
| 159 | } |
| 160 | |
| 161 | static PyObject *kaget_min(PyObject *me, void *hunoz) |
| 162 | { return (PyInt_FromLong(0)); } |
| 163 | #define kaget_max kaget_min |
| 164 | |
| 165 | static PyObject *ksget_min(PyObject *me, void *hunoz) |
| 166 | { |
| 167 | PyObject *set = ((keyszset_pyobj *)me)->set; |
| 168 | int i, n, y, x = -1; |
| 169 | n = PyTuple_Size(set); |
| 170 | for (i = 0; i < n; i++) { |
| 171 | y = PyInt_AsLong(PyTuple_GetItem(set, i)); |
| 172 | if (x == -1 || y < x) x = y; |
| 173 | } |
| 174 | return (PyInt_FromLong(x)); |
| 175 | } |
| 176 | |
| 177 | static PyObject *ksget_max(PyObject *me, void *hunoz) |
| 178 | { |
| 179 | PyObject *set = ((keyszset_pyobj *)me)->set; |
| 180 | int i, n, y, x = -1; |
| 181 | n = PyTuple_Size(set); |
| 182 | for (i = 0; i < n; i++) { |
| 183 | y = PyInt_AsLong(PyTuple_GetItem(set, i)); |
| 184 | if (y > x) x = y; |
| 185 | } |
| 186 | return (PyInt_FromLong(x)); |
| 187 | } |
| 188 | |
| 189 | static PyMemberDef keysz_pymembers[] = { |
| 190 | #define MEMBERSTRUCT keysz_pyobj |
| 191 | #define default dfl /* ugh! */ |
| 192 | MEMBER(default, T_INT, READONLY, "KSZ.default -> default key size") |
| 193 | #undef default |
| 194 | #undef MEMBERSTRUCT |
| 195 | { 0 } |
| 196 | }; |
| 197 | |
| 198 | static PyGetSetDef keyszany_pygetset[] = { |
| 199 | #define GETSETNAME(op, name) ka##op##_##name |
| 200 | GET (min, "KSZ.min -> smallest allowed key size") |
| 201 | GET (max, "KSZ.min -> largest allowed key size") |
| 202 | #undef GETSETNAME |
| 203 | { 0 } |
| 204 | }; |
| 205 | |
| 206 | static PyMemberDef keyszrange_pymembers[] = { |
| 207 | #define MEMBERSTRUCT keyszrange_pyobj |
| 208 | MEMBER(min, T_INT, READONLY, "KSZ.min -> smallest allowed key size") |
| 209 | MEMBER(max, T_INT, READONLY, "KSZ.min -> largest allowed key size") |
| 210 | MEMBER(mod, T_INT, READONLY, |
| 211 | "KSZ.mod -> key size must be a multiple of this") |
| 212 | #undef MEMBERSTRUCT |
| 213 | { 0 } |
| 214 | }; |
| 215 | |
| 216 | static PyGetSetDef keyszset_pygetset[] = { |
| 217 | #define GETSETNAME(op, name) ks##op##_##name |
| 218 | GET (min, "KSZ.min -> smallest allowed key size") |
| 219 | GET (max, "KSZ.min -> largest allowed key size") |
| 220 | #undef GETSETNAME |
| 221 | { 0 } |
| 222 | }; |
| 223 | |
| 224 | static PyMemberDef keyszset_pymembers[] = { |
| 225 | #define MEMBERSTRUCT keyszset_pyobj |
| 226 | MEMBER(set, T_OBJECT, READONLY, "KSZ.set -> allowed key sizes") |
| 227 | #undef MEMBERSTRUCT |
| 228 | { 0 } |
| 229 | }; |
| 230 | |
| 231 | static PyTypeObject keysz_pytype_skel = { |
| 232 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 233 | "KeySZ", /* @tp_name@ */ |
| 234 | sizeof(keysz_pyobj), /* @tp_basicsize@ */ |
| 235 | 0, /* @tp_itemsize@ */ |
| 236 | |
| 237 | 0, /* @tp_dealloc@ */ |
| 238 | 0, /* @tp_print@ */ |
| 239 | 0, /* @tp_getattr@ */ |
| 240 | 0, /* @tp_setattr@ */ |
| 241 | 0, /* @tp_compare@ */ |
| 242 | 0, /* @tp_repr@ */ |
| 243 | 0, /* @tp_as_number@ */ |
| 244 | 0, /* @tp_as_sequence@ */ |
| 245 | 0, /* @tp_as_mapping@ */ |
| 246 | 0, /* @tp_hash@ */ |
| 247 | 0, /* @tp_call@ */ |
| 248 | 0, /* @tp_str@ */ |
| 249 | 0, /* @tp_getattro@ */ |
| 250 | 0, /* @tp_setattro@ */ |
| 251 | 0, /* @tp_as_buffer@ */ |
| 252 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 253 | Py_TPFLAGS_BASETYPE, |
| 254 | |
| 255 | /* @tp_doc@ */ |
| 256 | "Key size constraints.", |
| 257 | |
| 258 | 0, /* @tp_traverse@ */ |
| 259 | 0, /* @tp_clear@ */ |
| 260 | 0, /* @tp_richcompare@ */ |
| 261 | 0, /* @tp_weaklistoffset@ */ |
| 262 | 0, /* @tp_iter@ */ |
| 263 | 0, /* @tp_iternext@ */ |
| 264 | 0, /* @tp_methods@ */ |
| 265 | keysz_pymembers, /* @tp_members@ */ |
| 266 | 0, /* @tp_getset@ */ |
| 267 | 0, /* @tp_base@ */ |
| 268 | 0, /* @tp_dict@ */ |
| 269 | 0, /* @tp_descr_get@ */ |
| 270 | 0, /* @tp_descr_set@ */ |
| 271 | 0, /* @tp_dictoffset@ */ |
| 272 | 0, /* @tp_init@ */ |
| 273 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 274 | abstract_pynew, /* @tp_new@ */ |
| 275 | 0, /* @tp_free@ */ |
| 276 | 0 /* @tp_is_gc@ */ |
| 277 | }; |
| 278 | |
| 279 | static PyTypeObject keyszany_pytype_skel = { |
| 280 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 281 | "KeySZAny", /* @tp_name@ */ |
| 282 | sizeof(keysz_pyobj), /* @tp_basicsize@ */ |
| 283 | 0, /* @tp_itemsize@ */ |
| 284 | |
| 285 | 0, /* @tp_dealloc@ */ |
| 286 | 0, /* @tp_print@ */ |
| 287 | 0, /* @tp_getattr@ */ |
| 288 | 0, /* @tp_setattr@ */ |
| 289 | 0, /* @tp_compare@ */ |
| 290 | 0, /* @tp_repr@ */ |
| 291 | 0, /* @tp_as_number@ */ |
| 292 | 0, /* @tp_as_sequence@ */ |
| 293 | 0, /* @tp_as_mapping@ */ |
| 294 | 0, /* @tp_hash@ */ |
| 295 | 0, /* @tp_call@ */ |
| 296 | 0, /* @tp_str@ */ |
| 297 | 0, /* @tp_getattro@ */ |
| 298 | 0, /* @tp_setattro@ */ |
| 299 | 0, /* @tp_as_buffer@ */ |
| 300 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 301 | Py_TPFLAGS_BASETYPE, |
| 302 | |
| 303 | /* @tp_doc@ */ |
| 304 | "Key size constraints. This object imposes no constraints on size.", |
| 305 | |
| 306 | 0, /* @tp_traverse@ */ |
| 307 | 0, /* @tp_clear@ */ |
| 308 | 0, /* @tp_richcompare@ */ |
| 309 | 0, /* @tp_weaklistoffset@ */ |
| 310 | 0, /* @tp_iter@ */ |
| 311 | 0, /* @tp_iternext@ */ |
| 312 | 0, /* @tp_methods@ */ |
| 313 | 0, /* @tp_members@ */ |
| 314 | keyszany_pygetset, /* @tp_getset@ */ |
| 315 | 0, /* @tp_base@ */ |
| 316 | 0, /* @tp_dict@ */ |
| 317 | 0, /* @tp_descr_get@ */ |
| 318 | 0, /* @tp_descr_set@ */ |
| 319 | 0, /* @tp_dictoffset@ */ |
| 320 | 0, /* @tp_init@ */ |
| 321 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 322 | keyszany_pynew, /* @tp_new@ */ |
| 323 | 0, /* @tp_free@ */ |
| 324 | 0 /* @tp_is_gc@ */ |
| 325 | }; |
| 326 | |
| 327 | static PyTypeObject keyszrange_pytype_skel = { |
| 328 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 329 | "KeySZRange", /* @tp_name@ */ |
| 330 | sizeof(keyszrange_pyobj), /* @tp_basicsize@ */ |
| 331 | 0, /* @tp_itemsize@ */ |
| 332 | |
| 333 | 0, /* @tp_dealloc@ */ |
| 334 | 0, /* @tp_print@ */ |
| 335 | 0, /* @tp_getattr@ */ |
| 336 | 0, /* @tp_setattr@ */ |
| 337 | 0, /* @tp_compare@ */ |
| 338 | 0, /* @tp_repr@ */ |
| 339 | 0, /* @tp_as_number@ */ |
| 340 | 0, /* @tp_as_sequence@ */ |
| 341 | 0, /* @tp_as_mapping@ */ |
| 342 | 0, /* @tp_hash@ */ |
| 343 | 0, /* @tp_call@ */ |
| 344 | 0, /* @tp_str@ */ |
| 345 | 0, /* @tp_getattro@ */ |
| 346 | 0, /* @tp_setattro@ */ |
| 347 | 0, /* @tp_as_buffer@ */ |
| 348 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 349 | Py_TPFLAGS_BASETYPE, |
| 350 | |
| 351 | /* @tp_doc@ */ |
| 352 | "Key size constraints. This object asserts minimum and maximum (if\n\ |
| 353 | sizes, and requires the key length to be a multiple of some value.", |
| 354 | |
| 355 | 0, /* @tp_traverse@ */ |
| 356 | 0, /* @tp_clear@ */ |
| 357 | 0, /* @tp_richcompare@ */ |
| 358 | 0, /* @tp_weaklistoffset@ */ |
| 359 | 0, /* @tp_iter@ */ |
| 360 | 0, /* @tp_iternext@ */ |
| 361 | 0, /* @tp_methods@ */ |
| 362 | keyszrange_pymembers, /* @tp_members@ */ |
| 363 | 0, /* @tp_getset@ */ |
| 364 | 0, /* @tp_base@ */ |
| 365 | 0, /* @tp_dict@ */ |
| 366 | 0, /* @tp_descr_get@ */ |
| 367 | 0, /* @tp_descr_set@ */ |
| 368 | 0, /* @tp_dictoffset@ */ |
| 369 | 0, /* @tp_init@ */ |
| 370 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 371 | keyszrange_pynew, /* @tp_new@ */ |
| 372 | 0, /* @tp_free@ */ |
| 373 | 0 /* @tp_is_gc@ */ |
| 374 | }; |
| 375 | |
| 376 | static PyTypeObject keyszset_pytype_skel = { |
| 377 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 378 | "KeySZSet", /* @tp_name@ */ |
| 379 | sizeof(keyszset_pyobj), /* @tp_basicsize@ */ |
| 380 | 0, /* @tp_itemsize@ */ |
| 381 | |
| 382 | 0, /* @tp_dealloc@ */ |
| 383 | 0, /* @tp_print@ */ |
| 384 | 0, /* @tp_getattr@ */ |
| 385 | 0, /* @tp_setattr@ */ |
| 386 | 0, /* @tp_compare@ */ |
| 387 | 0, /* @tp_repr@ */ |
| 388 | 0, /* @tp_as_number@ */ |
| 389 | 0, /* @tp_as_sequence@ */ |
| 390 | 0, /* @tp_as_mapping@ */ |
| 391 | 0, /* @tp_hash@ */ |
| 392 | 0, /* @tp_call@ */ |
| 393 | 0, /* @tp_str@ */ |
| 394 | 0, /* @tp_getattro@ */ |
| 395 | 0, /* @tp_setattro@ */ |
| 396 | 0, /* @tp_as_buffer@ */ |
| 397 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 398 | Py_TPFLAGS_BASETYPE, |
| 399 | |
| 400 | /* @tp_doc@ */ |
| 401 | "Key size constraints. This object requires the key to be one of a\n\ |
| 402 | few listed sizes.", |
| 403 | |
| 404 | 0, /* @tp_traverse@ */ |
| 405 | 0, /* @tp_clear@ */ |
| 406 | 0, /* @tp_richcompare@ */ |
| 407 | 0, /* @tp_weaklistoffset@ */ |
| 408 | 0, /* @tp_iter@ */ |
| 409 | 0, /* @tp_iternext@ */ |
| 410 | 0, /* @tp_methods@ */ |
| 411 | keyszset_pymembers, /* @tp_members@ */ |
| 412 | keyszset_pygetset, /* @tp_getset@ */ |
| 413 | 0, /* @tp_base@ */ |
| 414 | 0, /* @tp_dict@ */ |
| 415 | 0, /* @tp_descr_get@ */ |
| 416 | 0, /* @tp_descr_set@ */ |
| 417 | 0, /* @tp_dictoffset@ */ |
| 418 | 0, /* @tp_init@ */ |
| 419 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 420 | keyszset_pynew, /* @tp_new@ */ |
| 421 | 0, /* @tp_free@ */ |
| 422 | 0 /* @tp_is_gc@ */ |
| 423 | }; |
| 424 | |
| 425 | #define KSZCONVOP(op) \ |
| 426 | static PyObject *meth__KeySZ_##op(PyObject *me, PyObject *arg) \ |
| 427 | { \ |
| 428 | double x, y; \ |
| 429 | if (!PyArg_ParseTuple(arg, "Od:" #op, &me, &x)) return (0); \ |
| 430 | y = keysz_##op(x); \ |
| 431 | return (PyFloat_FromDouble(y)); \ |
| 432 | } |
| 433 | KSZCONVOP(fromdl) |
| 434 | KSZCONVOP(fromschnorr) |
| 435 | KSZCONVOP(fromif) |
| 436 | KSZCONVOP(fromec) |
| 437 | KSZCONVOP(todl) |
| 438 | KSZCONVOP(toschnorr) |
| 439 | KSZCONVOP(toif) |
| 440 | KSZCONVOP(toec) |
| 441 | #undef KSZCONVOP |
| 442 | |
| 443 | /*----- Symmetric encryption ----------------------------------------------*/ |
| 444 | |
| 445 | PyTypeObject *gccipher_pytype, *gcipher_pytype; |
| 446 | |
| 447 | CONVFUNC(gccipher, gccipher *, GCCIPHER_CC) |
| 448 | CONVFUNC(gcipher, gcipher *, GCIPHER_C) |
| 449 | |
| 450 | PyObject *gcipher_pywrap(PyObject *cobj, gcipher *c, unsigned f) |
| 451 | { |
| 452 | gcipher_pyobj *g; |
| 453 | if (!cobj) cobj = gccipher_pywrap((/*unconst*/ gccipher *)GC_CLASS(c)); |
| 454 | else Py_INCREF(cobj); |
| 455 | g = PyObject_NEW(gcipher_pyobj, (PyTypeObject *)cobj); |
| 456 | g->c = c; |
| 457 | g->f = f; |
| 458 | return ((PyObject *)g); |
| 459 | } |
| 460 | |
| 461 | static PyObject *gcipher_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) |
| 462 | { |
| 463 | char *kwlist[] = { "k", 0 }; |
| 464 | char *k; |
| 465 | Py_ssize_t sz; |
| 466 | |
| 467 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz)) |
| 468 | goto end; |
| 469 | if (keysz(sz, GCCIPHER_CC(ty)->keysz) != sz) VALERR("bad key length"); |
| 470 | return (gcipher_pywrap((PyObject *)ty, |
| 471 | GC_INIT(GCCIPHER_CC(ty), k, sz), |
| 472 | f_freeme)); |
| 473 | end: |
| 474 | return (0); |
| 475 | } |
| 476 | |
| 477 | PyObject *gccipher_pywrap(gccipher *cc) |
| 478 | { |
| 479 | gccipher_pyobj *g = newtype(gccipher_pytype, 0, cc->name); |
| 480 | g->cc = cc; |
| 481 | g->ty.ht_type.tp_basicsize = sizeof(gcipher_pyobj); |
| 482 | g->ty.ht_type.tp_base = gcipher_pytype; |
| 483 | Py_INCREF(gcipher_pytype); |
| 484 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
| 485 | Py_TPFLAGS_BASETYPE | |
| 486 | Py_TPFLAGS_HEAPTYPE); |
| 487 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; |
| 488 | g->ty.ht_type.tp_free = 0; |
| 489 | g->ty.ht_type.tp_new = gcipher_pynew; |
| 490 | typeready(&g->ty.ht_type); |
| 491 | return ((PyObject *)g); |
| 492 | } |
| 493 | |
| 494 | static void gcipher_pydealloc(PyObject *me) |
| 495 | { |
| 496 | if (GCIPHER_F(me) & f_freeme) |
| 497 | GC_DESTROY(GCIPHER_C(me)); |
| 498 | Py_DECREF(me->ob_type); |
| 499 | FREEOBJ(me); |
| 500 | } |
| 501 | |
| 502 | static PyObject *gccget_name(PyObject *me, void *hunoz) |
| 503 | { return (PyString_FromString(GCCIPHER_CC(me)->name)); } |
| 504 | |
| 505 | static PyObject *gccget_keysz(PyObject *me, void *hunoz) |
| 506 | { return (keysz_pywrap(GCCIPHER_CC(me)->keysz)); } |
| 507 | |
| 508 | static PyObject *gccget_blksz(PyObject *me, void *hunoz) |
| 509 | { return (PyInt_FromLong(GCCIPHER_CC(me)->blksz)); } |
| 510 | |
| 511 | static PyObject *gcmeth_encrypt(PyObject *me, PyObject *arg) |
| 512 | { |
| 513 | char *p; |
| 514 | Py_ssize_t sz; |
| 515 | PyObject *rc = 0; |
| 516 | |
| 517 | if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &sz)) return (0); |
| 518 | rc = bytestring_pywrap(0, sz); |
| 519 | GC_ENCRYPT(GCIPHER_C(me), p, PyString_AS_STRING(rc), sz); |
| 520 | return (rc); |
| 521 | } |
| 522 | |
| 523 | static PyObject *gcmeth_enczero(PyObject *me, PyObject *arg) |
| 524 | { |
| 525 | char *p; |
| 526 | int sz; |
| 527 | PyObject *rc = 0; |
| 528 | |
| 529 | if (!PyArg_ParseTuple(arg, "i:enczero", &sz)) return (0); |
| 530 | rc = bytestring_pywrap(0, sz); |
| 531 | p = PyString_AS_STRING(rc); |
| 532 | memset(p, 0, sz); |
| 533 | GC_ENCRYPT(GCIPHER_C(me), p, p, sz); |
| 534 | return (rc); |
| 535 | } |
| 536 | |
| 537 | static PyObject *gcmeth_decrypt(PyObject *me, PyObject *arg) |
| 538 | { |
| 539 | char *p; |
| 540 | Py_ssize_t sz; |
| 541 | PyObject *rc = 0; |
| 542 | |
| 543 | if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &sz)) return (0); |
| 544 | rc = bytestring_pywrap(0, sz); |
| 545 | GC_DECRYPT(GCIPHER_C(me), p, PyString_AS_STRING(rc), sz); |
| 546 | return (rc); |
| 547 | } |
| 548 | |
| 549 | static PyObject *gcmeth_deczero(PyObject *me, PyObject *arg) |
| 550 | { |
| 551 | char *p; |
| 552 | int sz; |
| 553 | PyObject *rc = 0; |
| 554 | |
| 555 | if (!PyArg_ParseTuple(arg, "i:deczero", &sz)) return (0); |
| 556 | rc = bytestring_pywrap(0, sz); |
| 557 | p = PyString_AS_STRING(rc); |
| 558 | memset(p, 0, sz); |
| 559 | GC_DECRYPT(GCIPHER_C(me), p, p, sz); |
| 560 | return (rc); |
| 561 | } |
| 562 | |
| 563 | static PyObject *gcmeth_setiv(PyObject *me, PyObject *arg) |
| 564 | { |
| 565 | char *p; |
| 566 | Py_ssize_t sz; |
| 567 | |
| 568 | if (!PyArg_ParseTuple(arg, "s#:setiv", &p, &sz)) goto end; |
| 569 | if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode"); |
| 570 | if (sz != GC_CLASS(GCIPHER_C(me))->blksz) VALERR("bad IV length"); |
| 571 | GC_SETIV(GCIPHER_C(me), p); |
| 572 | RETURN_ME; |
| 573 | end: |
| 574 | return (0); |
| 575 | } |
| 576 | |
| 577 | static PyObject *gcmeth_bdry(PyObject *me, PyObject *arg) |
| 578 | { |
| 579 | if (!PyArg_ParseTuple(arg, ":bdry")) goto end; |
| 580 | if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode"); |
| 581 | GC_BDRY(GCIPHER_C(me)); |
| 582 | RETURN_ME; |
| 583 | end: |
| 584 | return (0); |
| 585 | } |
| 586 | |
| 587 | static PyGetSetDef gccipher_pygetset[] = { |
| 588 | #define GETSETNAME(op, name) gcc##op##_##name |
| 589 | GET (keysz, "CC.keysz -> acceptable key sizes") |
| 590 | GET (blksz, "CC.blksz -> block size, or zero") |
| 591 | GET (name, "CC.name -> name of this kind of cipher") |
| 592 | #undef GETSETNAME |
| 593 | { 0 } |
| 594 | }; |
| 595 | |
| 596 | static PyMethodDef gcipher_pymethods[] = { |
| 597 | #define METHNAME(name) gcmeth_##name |
| 598 | METH (encrypt, "C.encrypt(PT) -> CT") |
| 599 | METH (enczero, "C.enczero(N) -> CT") |
| 600 | METH (decrypt, "C.decrypt(CT) -> PT") |
| 601 | METH (deczero, "C.deczero(N) -> PT") |
| 602 | METH (setiv, "C.setiv(IV)") |
| 603 | METH (bdry, "C.bdry()") |
| 604 | #undef METHNAME |
| 605 | { 0 } |
| 606 | }; |
| 607 | |
| 608 | static PyTypeObject gccipher_pytype_skel = { |
| 609 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 610 | "GCCipher", /* @tp_name@ */ |
| 611 | sizeof(gccipher_pyobj), /* @tp_basicsize@ */ |
| 612 | 0, /* @tp_itemsize@ */ |
| 613 | |
| 614 | 0, /* @tp_dealloc@ */ |
| 615 | 0, /* @tp_print@ */ |
| 616 | 0, /* @tp_getattr@ */ |
| 617 | 0, /* @tp_setattr@ */ |
| 618 | 0, /* @tp_compare@ */ |
| 619 | 0, /* @tp_repr@ */ |
| 620 | 0, /* @tp_as_number@ */ |
| 621 | 0, /* @tp_as_sequence@ */ |
| 622 | 0, /* @tp_as_mapping@ */ |
| 623 | 0, /* @tp_hash@ */ |
| 624 | 0, /* @tp_call@ */ |
| 625 | 0, /* @tp_str@ */ |
| 626 | 0, /* @tp_getattro@ */ |
| 627 | 0, /* @tp_setattro@ */ |
| 628 | 0, /* @tp_as_buffer@ */ |
| 629 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 630 | Py_TPFLAGS_BASETYPE, |
| 631 | |
| 632 | /* @tp_doc@ */ |
| 633 | "Symmetric cipher metaclass.", |
| 634 | |
| 635 | 0, /* @tp_traverse@ */ |
| 636 | 0, /* @tp_clear@ */ |
| 637 | 0, /* @tp_richcompare@ */ |
| 638 | 0, /* @tp_weaklistoffset@ */ |
| 639 | 0, /* @tp_iter@ */ |
| 640 | 0, /* @tp_iternext@ */ |
| 641 | 0, /* @tp_methods@ */ |
| 642 | 0, /* @tp_members@ */ |
| 643 | gccipher_pygetset, /* @tp_getset@ */ |
| 644 | 0, /* @tp_base@ */ |
| 645 | 0, /* @tp_dict@ */ |
| 646 | 0, /* @tp_descr_get@ */ |
| 647 | 0, /* @tp_descr_set@ */ |
| 648 | 0, /* @tp_dictoffset@ */ |
| 649 | 0, /* @tp_init@ */ |
| 650 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 651 | abstract_pynew, /* @tp_new@ */ |
| 652 | 0, /* @tp_free@ */ |
| 653 | 0 /* @tp_is_gc@ */ |
| 654 | }; |
| 655 | |
| 656 | static PyTypeObject gcipher_pytype_skel = { |
| 657 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 658 | "GCipher", /* @tp_name@ */ |
| 659 | sizeof(gcipher_pyobj), /* @tp_basicsize@ */ |
| 660 | 0, /* @tp_itemsize@ */ |
| 661 | |
| 662 | gcipher_pydealloc, /* @tp_dealloc@ */ |
| 663 | 0, /* @tp_print@ */ |
| 664 | 0, /* @tp_getattr@ */ |
| 665 | 0, /* @tp_setattr@ */ |
| 666 | 0, /* @tp_compare@ */ |
| 667 | 0, /* @tp_repr@ */ |
| 668 | 0, /* @tp_as_number@ */ |
| 669 | 0, /* @tp_as_sequence@ */ |
| 670 | 0, /* @tp_as_mapping@ */ |
| 671 | 0, /* @tp_hash@ */ |
| 672 | 0, /* @tp_call@ */ |
| 673 | 0, /* @tp_str@ */ |
| 674 | 0, /* @tp_getattro@ */ |
| 675 | 0, /* @tp_setattro@ */ |
| 676 | 0, /* @tp_as_buffer@ */ |
| 677 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 678 | Py_TPFLAGS_BASETYPE, |
| 679 | |
| 680 | /* @tp_doc@ */ |
| 681 | "Symmetric cipher, abstract base class.", |
| 682 | |
| 683 | 0, /* @tp_traverse@ */ |
| 684 | 0, /* @tp_clear@ */ |
| 685 | 0, /* @tp_richcompare@ */ |
| 686 | 0, /* @tp_weaklistoffset@ */ |
| 687 | 0, /* @tp_iter@ */ |
| 688 | 0, /* @tp_iternext@ */ |
| 689 | gcipher_pymethods, /* @tp_methods@ */ |
| 690 | 0, /* @tp_members@ */ |
| 691 | 0, /* @tp_getset@ */ |
| 692 | 0, /* @tp_base@ */ |
| 693 | 0, /* @tp_dict@ */ |
| 694 | 0, /* @tp_descr_get@ */ |
| 695 | 0, /* @tp_descr_set@ */ |
| 696 | 0, /* @tp_dictoffset@ */ |
| 697 | 0, /* @tp_init@ */ |
| 698 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 699 | abstract_pynew, /* @tp_new@ */ |
| 700 | 0, /* @tp_free@ */ |
| 701 | 0 /* @tp_is_gc@ */ |
| 702 | }; |
| 703 | |
| 704 | /*----- Hash functions ----------------------------------------------------*/ |
| 705 | |
| 706 | PyTypeObject *gchash_pytype, *ghash_pytype; |
| 707 | |
| 708 | CONVFUNC(gchash, gchash *, GCHASH_CH) |
| 709 | CONVFUNC(ghash, ghash *, GHASH_H) |
| 710 | |
| 711 | static PyObject *ghash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) |
| 712 | { |
| 713 | char *kwlist[] = { 0 }; |
| 714 | if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", kwlist)) |
| 715 | goto end; |
| 716 | return (ghash_pywrap((PyObject *)ty, GH_INIT(GCHASH_CH(ty)), f_freeme)); |
| 717 | end: |
| 718 | return (0); |
| 719 | } |
| 720 | |
| 721 | PyObject *gchash_pywrap(gchash *ch) |
| 722 | { |
| 723 | gchash_pyobj *g = newtype(gchash_pytype, 0, ch->name); |
| 724 | g->ch = ch; |
| 725 | g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj); |
| 726 | g->ty.ht_type.tp_base = ghash_pytype; |
| 727 | Py_INCREF(ghash_pytype); |
| 728 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
| 729 | Py_TPFLAGS_BASETYPE | |
| 730 | Py_TPFLAGS_HEAPTYPE); |
| 731 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; |
| 732 | g->ty.ht_type.tp_free = 0; |
| 733 | g->ty.ht_type.tp_new = ghash_pynew; |
| 734 | typeready(&g->ty.ht_type); |
| 735 | return ((PyObject *)g); |
| 736 | } |
| 737 | |
| 738 | PyObject *ghash_pywrap(PyObject *cobj, ghash *h, unsigned f) |
| 739 | { |
| 740 | ghash_pyobj *g; |
| 741 | if (!cobj) cobj = gchash_pywrap((/*unconst*/ gchash *)GH_CLASS(h)); |
| 742 | else Py_INCREF(cobj); |
| 743 | g = PyObject_NEW(ghash_pyobj, (PyTypeObject *)cobj); |
| 744 | g->h = h; |
| 745 | g->f = f; |
| 746 | return ((PyObject *)g); |
| 747 | } |
| 748 | |
| 749 | static void ghash_pydealloc(PyObject *me) |
| 750 | { |
| 751 | if (GHASH_F(me) & f_freeme) |
| 752 | GH_DESTROY(GHASH_H(me)); |
| 753 | Py_DECREF(me->ob_type); |
| 754 | FREEOBJ(me); |
| 755 | } |
| 756 | |
| 757 | static PyObject *gchget_name(PyObject *me, void *hunoz) |
| 758 | { return (PyString_FromString(GCHASH_CH(me)->name)); } |
| 759 | |
| 760 | static PyObject *gchget_hashsz(PyObject *me, void *hunoz) |
| 761 | { return (PyInt_FromLong(GCHASH_CH(me)->hashsz)); } |
| 762 | |
| 763 | static PyObject *gchget_bufsz(PyObject *me, void *hunoz) |
| 764 | { return (PyInt_FromLong(GCHASH_CH(me)->bufsz)); } |
| 765 | |
| 766 | static PyObject *ghmeth_hash(PyObject *me, PyObject *arg) |
| 767 | { |
| 768 | char *p; |
| 769 | Py_ssize_t sz; |
| 770 | if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0); |
| 771 | GH_HASH(GHASH_H(me), p, sz); |
| 772 | RETURN_ME; |
| 773 | } |
| 774 | |
| 775 | #define GHMETH_HASHU_(n, W, w) \ |
| 776 | static PyObject *ghmeth_hashu##w(PyObject *me, PyObject *arg) \ |
| 777 | { \ |
| 778 | uint##n x; \ |
| 779 | if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \ |
| 780 | GH_HASHU##W(GHASH_H(me), x); \ |
| 781 | RETURN_ME; \ |
| 782 | end: \ |
| 783 | return (0); \ |
| 784 | } |
| 785 | DOUINTCONV(GHMETH_HASHU_) |
| 786 | |
| 787 | #define GHMETH_HASHBUF_(n, W, w) \ |
| 788 | static PyObject *ghmeth_hashbuf##w(PyObject *me, PyObject *arg) \ |
| 789 | { \ |
| 790 | char *p; \ |
| 791 | Py_ssize_t sz; \ |
| 792 | if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \ |
| 793 | if (sz > MASK##n) TYERR("string too long"); \ |
| 794 | GH_HASHBUF##W(GHASH_H(me), p, sz); \ |
| 795 | RETURN_ME; \ |
| 796 | end: \ |
| 797 | return (0); \ |
| 798 | } |
| 799 | DOUINTCONV(GHMETH_HASHBUF_) |
| 800 | |
| 801 | static PyObject *ghmeth_hashstrz(PyObject *me, PyObject *arg) |
| 802 | { |
| 803 | char *p; |
| 804 | if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0); |
| 805 | GH_HASHSTRZ(GHASH_H(me), p); |
| 806 | RETURN_ME; |
| 807 | } |
| 808 | |
| 809 | static PyObject *ghmeth_done(PyObject *me, PyObject *arg) |
| 810 | { |
| 811 | ghash *g; |
| 812 | PyObject *rc; |
| 813 | if (!PyArg_ParseTuple(arg, ":done")) return (0); |
| 814 | g = GH_COPY(GHASH_H(me)); |
| 815 | rc = bytestring_pywrap(0, g->ops->c->hashsz); |
| 816 | GH_DONE(g, PyString_AS_STRING(rc)); |
| 817 | GH_DESTROY(g); |
| 818 | return (rc); |
| 819 | } |
| 820 | |
| 821 | static PyGetSetDef gchash_pygetset[] = { |
| 822 | #define GETSETNAME(op, name) gch##op##_##name |
| 823 | GET (bufsz, "CH.bufsz -> hash buffer size, or zero") |
| 824 | GET (hashsz, "CH.hashsz -> hash output size") |
| 825 | GET (name, "CH.name -> name of this kind of hash") |
| 826 | #undef GETSETNAME |
| 827 | { 0 } |
| 828 | }; |
| 829 | |
| 830 | static PyMethodDef ghash_pymethods[] = { |
| 831 | #define METHNAME(name) ghmeth_##name |
| 832 | METH (hash, "H.hash(M)") |
| 833 | #define METHU_(n, W, w) METH(hashu##w, "H.hashu" #w "(WORD)") |
| 834 | DOUINTCONV(METHU_) |
| 835 | #undef METHU_ |
| 836 | #define METHBUF_(n, W, w) METH(hashbuf##w, "H.hashbuf" #w "(BYTES)") |
| 837 | DOUINTCONV(METHBUF_) |
| 838 | #undef METHBUF_ |
| 839 | METH (hashstrz, "H.hashstrz(STRING)") |
| 840 | METH (done, "H.done() -> HASH") |
| 841 | #undef METHNAME |
| 842 | { 0 } |
| 843 | }; |
| 844 | |
| 845 | static PyTypeObject gchash_pytype_skel = { |
| 846 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 847 | "GCHash", /* @tp_name@ */ |
| 848 | sizeof(gchash_pyobj), /* @tp_basicsize@ */ |
| 849 | 0, /* @tp_itemsize@ */ |
| 850 | |
| 851 | 0, /* @tp_dealloc@ */ |
| 852 | 0, /* @tp_print@ */ |
| 853 | 0, /* @tp_getattr@ */ |
| 854 | 0, /* @tp_setattr@ */ |
| 855 | 0, /* @tp_compare@ */ |
| 856 | 0, /* @tp_repr@ */ |
| 857 | 0, /* @tp_as_number@ */ |
| 858 | 0, /* @tp_as_sequence@ */ |
| 859 | 0, /* @tp_as_mapping@ */ |
| 860 | 0, /* @tp_hash@ */ |
| 861 | 0, /* @tp_call@ */ |
| 862 | 0, /* @tp_str@ */ |
| 863 | 0, /* @tp_getattro@ */ |
| 864 | 0, /* @tp_setattro@ */ |
| 865 | 0, /* @tp_as_buffer@ */ |
| 866 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 867 | Py_TPFLAGS_BASETYPE, |
| 868 | |
| 869 | /* @tp_doc@ */ |
| 870 | "Hash function metaclass.", |
| 871 | |
| 872 | 0, /* @tp_traverse@ */ |
| 873 | 0, /* @tp_clear@ */ |
| 874 | 0, /* @tp_richcompare@ */ |
| 875 | 0, /* @tp_weaklistoffset@ */ |
| 876 | 0, /* @tp_iter@ */ |
| 877 | 0, /* @tp_iternext@ */ |
| 878 | 0, /* @tp_methods@ */ |
| 879 | 0, /* @tp_members@ */ |
| 880 | gchash_pygetset, /* @tp_getset@ */ |
| 881 | 0, /* @tp_base@ */ |
| 882 | 0, /* @tp_dict@ */ |
| 883 | 0, /* @tp_descr_get@ */ |
| 884 | 0, /* @tp_descr_set@ */ |
| 885 | 0, /* @tp_dictoffset@ */ |
| 886 | 0, /* @tp_init@ */ |
| 887 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 888 | abstract_pynew, /* @tp_new@ */ |
| 889 | 0, /* @tp_free@ */ |
| 890 | 0 /* @tp_is_gc@ */ |
| 891 | }; |
| 892 | |
| 893 | static PyTypeObject ghash_pytype_skel = { |
| 894 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 895 | "GHash", /* @tp_name@ */ |
| 896 | sizeof(ghash_pyobj), /* @tp_basicsize@ */ |
| 897 | 0, /* @tp_itemsize@ */ |
| 898 | |
| 899 | ghash_pydealloc, /* @tp_dealloc@ */ |
| 900 | 0, /* @tp_print@ */ |
| 901 | 0, /* @tp_getattr@ */ |
| 902 | 0, /* @tp_setattr@ */ |
| 903 | 0, /* @tp_compare@ */ |
| 904 | 0, /* @tp_repr@ */ |
| 905 | 0, /* @tp_as_number@ */ |
| 906 | 0, /* @tp_as_sequence@ */ |
| 907 | 0, /* @tp_as_mapping@ */ |
| 908 | 0, /* @tp_hash@ */ |
| 909 | 0, /* @tp_call@ */ |
| 910 | 0, /* @tp_str@ */ |
| 911 | 0, /* @tp_getattro@ */ |
| 912 | 0, /* @tp_setattro@ */ |
| 913 | 0, /* @tp_as_buffer@ */ |
| 914 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 915 | Py_TPFLAGS_BASETYPE, |
| 916 | |
| 917 | /* @tp_doc@ */ |
| 918 | "Hash function, abstract base class.", |
| 919 | |
| 920 | 0, /* @tp_traverse@ */ |
| 921 | 0, /* @tp_clear@ */ |
| 922 | 0, /* @tp_richcompare@ */ |
| 923 | 0, /* @tp_weaklistoffset@ */ |
| 924 | 0, /* @tp_iter@ */ |
| 925 | 0, /* @tp_iternext@ */ |
| 926 | ghash_pymethods, /* @tp_methods@ */ |
| 927 | 0, /* @tp_members@ */ |
| 928 | 0, /* @tp_getset@ */ |
| 929 | 0, /* @tp_base@ */ |
| 930 | 0, /* @tp_dict@ */ |
| 931 | 0, /* @tp_descr_get@ */ |
| 932 | 0, /* @tp_descr_set@ */ |
| 933 | 0, /* @tp_dictoffset@ */ |
| 934 | 0, /* @tp_init@ */ |
| 935 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 936 | abstract_pynew, /* @tp_new@ */ |
| 937 | 0, /* @tp_free@ */ |
| 938 | 0 /* @tp_is_gc@ */ |
| 939 | }; |
| 940 | |
| 941 | /*----- Message authentication --------------------------------------------*/ |
| 942 | |
| 943 | PyTypeObject *gcmac_pytype, *gmac_pytype, *gmhash_pytype; |
| 944 | |
| 945 | CONVFUNC(gcmac, gcmac *, GCMAC_CM) |
| 946 | CONVFUNC(gmac, gmac *, GMAC_M) |
| 947 | CONVFUNC(gmhash, ghash *, GHASH_H) |
| 948 | |
| 949 | static PyObject *gmac_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) |
| 950 | { |
| 951 | char *kwlist[] = { "k", 0 }; |
| 952 | char *k; |
| 953 | Py_ssize_t sz; |
| 954 | |
| 955 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz)) |
| 956 | goto end; |
| 957 | if (keysz(sz, GCMAC_CM(ty)->keysz) != sz) VALERR("bad key length"); |
| 958 | return (gmac_pywrap((PyObject *)ty, |
| 959 | GM_KEY(GCMAC_CM(ty), k, sz), |
| 960 | f_freeme)); |
| 961 | end: |
| 962 | return (0); |
| 963 | } |
| 964 | |
| 965 | static PyObject *gmhash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) |
| 966 | { |
| 967 | char *kwlist[] = { 0 }; |
| 968 | ghash_pyobj *g; |
| 969 | |
| 970 | if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", kwlist)) return (0); |
| 971 | g = PyObject_NEW(ghash_pyobj, ty); |
| 972 | g->h = GM_INIT(GMAC_M(ty)); |
| 973 | g->f = f_freeme; |
| 974 | Py_INCREF(ty); |
| 975 | return ((PyObject *)g); |
| 976 | } |
| 977 | |
| 978 | PyObject *gcmac_pywrap(gcmac *cm) |
| 979 | { |
| 980 | gcmac_pyobj *g = newtype(gcmac_pytype, 0, cm->name); |
| 981 | g->cm = cm; |
| 982 | g->ty.ht_type.tp_basicsize = sizeof(gmac_pyobj); |
| 983 | g->ty.ht_type.tp_base = gmac_pytype; |
| 984 | Py_INCREF(gmac_pytype); |
| 985 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
| 986 | Py_TPFLAGS_BASETYPE | |
| 987 | Py_TPFLAGS_HEAPTYPE); |
| 988 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; |
| 989 | g->ty.ht_type.tp_free = 0; |
| 990 | g->ty.ht_type.tp_new = gmac_pynew; |
| 991 | typeready(&g->ty.ht_type); |
| 992 | return ((PyObject *)g); |
| 993 | } |
| 994 | |
| 995 | PyObject *gmac_pywrap(PyObject *cobj, gmac *m, unsigned f) |
| 996 | { |
| 997 | gmac_pyobj *g; |
| 998 | if (!cobj) cobj = gcmac_pywrap((/*unconst*/ gcmac *)GM_CLASS(m)); |
| 999 | else Py_INCREF(cobj); |
| 1000 | g = newtype((PyTypeObject *)cobj, 0, 0); |
| 1001 | g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj); |
| 1002 | g->ty.ht_name = PyString_FromFormat("%s(keyed)", m->ops->c->name); |
| 1003 | g->ty.ht_type.tp_name = PyString_AS_STRING(g->ty.ht_name); |
| 1004 | g->ty.ht_type.tp_base = gmhash_pytype; |
| 1005 | Py_INCREF(gmac_pytype); |
| 1006 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
| 1007 | Py_TPFLAGS_BASETYPE | |
| 1008 | Py_TPFLAGS_HEAPTYPE); |
| 1009 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; |
| 1010 | g->ty.ht_type.tp_free = 0; |
| 1011 | g->ty.ht_type.tp_new = gmhash_pynew; |
| 1012 | typeready(&g->ty.ht_type); |
| 1013 | g->m = m; |
| 1014 | g->f = f; |
| 1015 | return ((PyObject *)g); |
| 1016 | } |
| 1017 | |
| 1018 | static void gmac_pydealloc(PyObject *me) |
| 1019 | { |
| 1020 | if (GMAC_F(me) & f_freeme) |
| 1021 | GM_DESTROY(GMAC_M(me)); |
| 1022 | Py_DECREF(me->ob_type); |
| 1023 | PyType_Type.tp_dealloc(me); |
| 1024 | } |
| 1025 | |
| 1026 | static PyObject *gcmget_name(PyObject *me, void *hunoz) |
| 1027 | { return (PyString_FromString(GCMAC_CM(me)->name)); } |
| 1028 | |
| 1029 | static PyObject *gcmget_keysz(PyObject *me, void *hunoz) |
| 1030 | { return (keysz_pywrap(GCMAC_CM(me)->keysz)); } |
| 1031 | |
| 1032 | static PyObject *gcmget_tagsz(PyObject *me, void *hunoz) |
| 1033 | { return (PyInt_FromLong(GCMAC_CM(me)->hashsz)); } |
| 1034 | |
| 1035 | static PyGetSetDef gcmac_pygetset[] = { |
| 1036 | #define GETSETNAME(op, name) gcm##op##_##name |
| 1037 | GET (keysz, "CM.keysz -> acceptable key sizes") |
| 1038 | GET (tagsz, "CM.tagsz -> MAC output size") |
| 1039 | GET (name, "CM.name -> name of this kind of MAC") |
| 1040 | #undef GETSETNAME |
| 1041 | { 0 } |
| 1042 | }; |
| 1043 | |
| 1044 | static PyTypeObject gcmac_pytype_skel = { |
| 1045 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1046 | "GCMAC", /* @tp_name@ */ |
| 1047 | sizeof(gchash_pyobj), /* @tp_basicsize@ */ |
| 1048 | 0, /* @tp_itemsize@ */ |
| 1049 | |
| 1050 | 0, /* @tp_dealloc@ */ |
| 1051 | 0, /* @tp_print@ */ |
| 1052 | 0, /* @tp_getattr@ */ |
| 1053 | 0, /* @tp_setattr@ */ |
| 1054 | 0, /* @tp_compare@ */ |
| 1055 | 0, /* @tp_repr@ */ |
| 1056 | 0, /* @tp_as_number@ */ |
| 1057 | 0, /* @tp_as_sequence@ */ |
| 1058 | 0, /* @tp_as_mapping@ */ |
| 1059 | 0, /* @tp_hash@ */ |
| 1060 | 0, /* @tp_call@ */ |
| 1061 | 0, /* @tp_str@ */ |
| 1062 | 0, /* @tp_getattro@ */ |
| 1063 | 0, /* @tp_setattro@ */ |
| 1064 | 0, /* @tp_as_buffer@ */ |
| 1065 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 1066 | Py_TPFLAGS_BASETYPE, |
| 1067 | |
| 1068 | /* @tp_doc@ */ |
| 1069 | "Message authentication code metametaclass.", |
| 1070 | |
| 1071 | 0, /* @tp_traverse@ */ |
| 1072 | 0, /* @tp_clear@ */ |
| 1073 | 0, /* @tp_richcompare@ */ |
| 1074 | 0, /* @tp_weaklistoffset@ */ |
| 1075 | 0, /* @tp_iter@ */ |
| 1076 | 0, /* @tp_iternext@ */ |
| 1077 | 0, /* @tp_methods@ */ |
| 1078 | 0, /* @tp_members@ */ |
| 1079 | gcmac_pygetset, /* @tp_getset@ */ |
| 1080 | 0, /* @tp_base@ */ |
| 1081 | 0, /* @tp_dict@ */ |
| 1082 | 0, /* @tp_descr_get@ */ |
| 1083 | 0, /* @tp_descr_set@ */ |
| 1084 | 0, /* @tp_dictoffset@ */ |
| 1085 | 0, /* @tp_init@ */ |
| 1086 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 1087 | abstract_pynew, /* @tp_new@ */ |
| 1088 | 0, /* @tp_free@ */ |
| 1089 | 0 /* @tp_is_gc@ */ |
| 1090 | }; |
| 1091 | |
| 1092 | static PyTypeObject gmac_pytype_skel = { |
| 1093 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1094 | "GMAC", /* @tp_name@ */ |
| 1095 | sizeof(gmac_pyobj), /* @tp_basicsize@ */ |
| 1096 | 0, /* @tp_itemsize@ */ |
| 1097 | |
| 1098 | gmac_pydealloc, /* @tp_dealloc@ */ |
| 1099 | 0, /* @tp_print@ */ |
| 1100 | 0, /* @tp_getattr@ */ |
| 1101 | 0, /* @tp_setattr@ */ |
| 1102 | 0, /* @tp_compare@ */ |
| 1103 | 0, /* @tp_repr@ */ |
| 1104 | 0, /* @tp_as_number@ */ |
| 1105 | 0, /* @tp_as_sequence@ */ |
| 1106 | 0, /* @tp_as_mapping@ */ |
| 1107 | 0, /* @tp_hash@ */ |
| 1108 | 0, /* @tp_call@ */ |
| 1109 | 0, /* @tp_str@ */ |
| 1110 | 0, /* @tp_getattro@ */ |
| 1111 | 0, /* @tp_setattro@ */ |
| 1112 | 0, /* @tp_as_buffer@ */ |
| 1113 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 1114 | Py_TPFLAGS_BASETYPE, |
| 1115 | |
| 1116 | /* @tp_doc@ */ |
| 1117 | "Message authentication code metaclass, abstract base class.", |
| 1118 | |
| 1119 | 0, /* @tp_traverse@ */ |
| 1120 | 0, /* @tp_clear@ */ |
| 1121 | 0, /* @tp_richcompare@ */ |
| 1122 | 0, /* @tp_weaklistoffset@ */ |
| 1123 | 0, /* @tp_iter@ */ |
| 1124 | 0, /* @tp_iternext@ */ |
| 1125 | 0, /* @tp_methods@ */ |
| 1126 | 0, /* @tp_members@ */ |
| 1127 | 0, /* @tp_getset@ */ |
| 1128 | 0, /* @tp_base@ */ |
| 1129 | 0, /* @tp_dict@ */ |
| 1130 | 0, /* @tp_descr_get@ */ |
| 1131 | 0, /* @tp_descr_set@ */ |
| 1132 | 0, /* @tp_dictoffset@ */ |
| 1133 | 0, /* @tp_init@ */ |
| 1134 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 1135 | abstract_pynew, /* @tp_new@ */ |
| 1136 | 0, /* @tp_free@ */ |
| 1137 | 0 /* @tp_is_gc@ */ |
| 1138 | }; |
| 1139 | |
| 1140 | static PyTypeObject gmhash_pytype_skel = { |
| 1141 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1142 | "GMACHash", /* @tp_name@ */ |
| 1143 | sizeof(ghash_pyobj), /* @tp_basicsize@ */ |
| 1144 | 0, /* @tp_itemsize@ */ |
| 1145 | |
| 1146 | ghash_pydealloc, /* @tp_dealloc@ */ |
| 1147 | 0, /* @tp_print@ */ |
| 1148 | 0, /* @tp_getattr@ */ |
| 1149 | 0, /* @tp_setattr@ */ |
| 1150 | 0, /* @tp_compare@ */ |
| 1151 | 0, /* @tp_repr@ */ |
| 1152 | 0, /* @tp_as_number@ */ |
| 1153 | 0, /* @tp_as_sequence@ */ |
| 1154 | 0, /* @tp_as_mapping@ */ |
| 1155 | 0, /* @tp_hash@ */ |
| 1156 | 0, /* @tp_call@ */ |
| 1157 | 0, /* @tp_str@ */ |
| 1158 | 0, /* @tp_getattro@ */ |
| 1159 | 0, /* @tp_setattro@ */ |
| 1160 | 0, /* @tp_as_buffer@ */ |
| 1161 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 1162 | Py_TPFLAGS_BASETYPE, |
| 1163 | |
| 1164 | /* @tp_doc@ */ |
| 1165 | "Message authentication code, abstract base class.", |
| 1166 | |
| 1167 | 0, /* @tp_traverse@ */ |
| 1168 | 0, /* @tp_clear@ */ |
| 1169 | 0, /* @tp_richcompare@ */ |
| 1170 | 0, /* @tp_weaklistoffset@ */ |
| 1171 | 0, /* @tp_iter@ */ |
| 1172 | 0, /* @tp_iternext@ */ |
| 1173 | 0, /* @tp_methods@ */ |
| 1174 | 0, /* @tp_members@ */ |
| 1175 | 0, /* @tp_getset@ */ |
| 1176 | 0, /* @tp_base@ */ |
| 1177 | 0, /* @tp_dict@ */ |
| 1178 | 0, /* @tp_descr_get@ */ |
| 1179 | 0, /* @tp_descr_set@ */ |
| 1180 | 0, /* @tp_dictoffset@ */ |
| 1181 | 0, /* @tp_init@ */ |
| 1182 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 1183 | abstract_pynew, /* @tp_new@ */ |
| 1184 | 0, /* @tp_free@ */ |
| 1185 | 0 /* @tp_is_gc@ */ |
| 1186 | }; |
| 1187 | |
| 1188 | /*----- Special snowflake for Poly1305 ------------------------------------*/ |
| 1189 | |
| 1190 | PyTypeObject *poly1305cls_pytype, *poly1305key_pytype, *poly1305hash_pytype; |
| 1191 | |
| 1192 | typedef struct poly1305key_pyobj { |
| 1193 | PyHeapTypeObject ty; |
| 1194 | poly1305_key k; |
| 1195 | } poly1305key_pyobj; |
| 1196 | |
| 1197 | typedef struct poly1305hash_pyobj { |
| 1198 | PyObject_HEAD |
| 1199 | unsigned f; |
| 1200 | #define f_mask 1u |
| 1201 | poly1305_ctx ctx; |
| 1202 | } poly1305hash_pyobj; |
| 1203 | |
| 1204 | #define P1305_F(o) (((poly1305hash_pyobj *)(o))->f) |
| 1205 | #define P1305_CTX(o) (&((poly1305hash_pyobj *)(o))->ctx) |
| 1206 | CONVFUNC(poly1305hash, poly1305_ctx *, P1305_CTX) |
| 1207 | |
| 1208 | static PyObject *poly1305hash_pynew(PyTypeObject *ty, |
| 1209 | PyObject *arg, PyObject *kw) |
| 1210 | { |
| 1211 | char *kwlist[] = { "mask", 0 }; |
| 1212 | poly1305key_pyobj *pk = (poly1305key_pyobj *)ty; |
| 1213 | poly1305hash_pyobj *ph; |
| 1214 | char *m = 0; |
| 1215 | Py_ssize_t sz; |
| 1216 | |
| 1217 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "|s#:new", kwlist, &m, &sz)) |
| 1218 | return (0); |
| 1219 | if (m && sz != POLY1305_MASKSZ) VALERR("bad mask length"); |
| 1220 | ph = PyObject_NEW(poly1305hash_pyobj, ty); |
| 1221 | ph->f = 0; |
| 1222 | if (m) ph->f |= f_mask; |
| 1223 | poly1305_macinit(&ph->ctx, &pk->k, m); |
| 1224 | Py_INCREF(ty); |
| 1225 | return ((PyObject *)ph); |
| 1226 | end: |
| 1227 | return (0); |
| 1228 | } |
| 1229 | |
| 1230 | static PyObject *poly1305key_pynew(PyTypeObject *ty, |
| 1231 | PyObject *arg, PyObject *kw) |
| 1232 | { |
| 1233 | char *kwlist[] = { "k", 0 }; |
| 1234 | poly1305key_pyobj *pk; |
| 1235 | char *k; |
| 1236 | Py_ssize_t sz; |
| 1237 | |
| 1238 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz)) |
| 1239 | goto end; |
| 1240 | if (keysz(sz, poly1305_keysz) != sz) VALERR("bad key length"); |
| 1241 | |
| 1242 | pk = newtype(ty, 0, 0); |
| 1243 | pk->ty.ht_name = PyString_FromString("poly1305(keyed)"); |
| 1244 | pk->ty.ht_type.tp_basicsize = sizeof(poly1305hash_pyobj); |
| 1245 | pk->ty.ht_type.tp_name = PyString_AS_STRING(pk->ty.ht_name); |
| 1246 | pk->ty.ht_type.tp_base = poly1305hash_pytype; |
| 1247 | Py_INCREF(poly1305key_pytype); |
| 1248 | pk->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
| 1249 | Py_TPFLAGS_BASETYPE | |
| 1250 | Py_TPFLAGS_HEAPTYPE); |
| 1251 | pk->ty.ht_type.tp_alloc = PyType_GenericAlloc; |
| 1252 | pk->ty.ht_type.tp_free = 0; |
| 1253 | pk->ty.ht_type.tp_new = poly1305hash_pynew; |
| 1254 | typeready(&pk->ty.ht_type); |
| 1255 | |
| 1256 | poly1305_keyinit(&pk->k, k, sz); |
| 1257 | return ((PyObject *)pk); |
| 1258 | |
| 1259 | end: |
| 1260 | return (0); |
| 1261 | } |
| 1262 | |
| 1263 | static PyObject *poly1305clsget_name(PyObject *me, void *hunoz) |
| 1264 | { return (PyString_FromString("poly1305")); } |
| 1265 | |
| 1266 | static PyObject *poly1305clsget_keysz(PyObject *me, void *hunoz) |
| 1267 | { return (keysz_pywrap(poly1305_keysz)); } |
| 1268 | |
| 1269 | static PyObject *poly1305clsget_masksz(PyObject *me, void *hunoz) |
| 1270 | { return (PyInt_FromLong(POLY1305_MASKSZ)); } |
| 1271 | |
| 1272 | static PyObject *poly1305clsget_tagsz(PyObject *me, void *hunoz) |
| 1273 | { return (PyInt_FromLong(POLY1305_TAGSZ)); } |
| 1274 | |
| 1275 | static PyObject *polymeth_copy(PyObject *me, PyObject *arg) |
| 1276 | { |
| 1277 | poly1305hash_pyobj *ph; |
| 1278 | if (!PyArg_ParseTuple(arg, ":copy")) return (0); |
| 1279 | ph = PyObject_NEW(poly1305hash_pyobj, me->ob_type); |
| 1280 | poly1305_copy(&ph->ctx, P1305_CTX(me)); |
| 1281 | Py_INCREF(me->ob_type); |
| 1282 | return ((PyObject *)ph); |
| 1283 | } |
| 1284 | |
| 1285 | static PyObject *polymeth_hash(PyObject *me, PyObject *arg) |
| 1286 | { |
| 1287 | char *p; |
| 1288 | Py_ssize_t sz; |
| 1289 | if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0); |
| 1290 | poly1305_hash(P1305_CTX(me), p, sz); |
| 1291 | RETURN_ME; |
| 1292 | } |
| 1293 | |
| 1294 | #define POLYMETH_HASHU_(n, W, w) \ |
| 1295 | static PyObject *polymeth_hashu##w(PyObject *me, PyObject *arg) \ |
| 1296 | { \ |
| 1297 | uint##n x; \ |
| 1298 | octet b[SZ_##W]; \ |
| 1299 | if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \ |
| 1300 | STORE##W(b, x); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \ |
| 1301 | RETURN_ME; \ |
| 1302 | end: \ |
| 1303 | return (0); \ |
| 1304 | } |
| 1305 | DOUINTCONV(POLYMETH_HASHU_) |
| 1306 | |
| 1307 | #define POLYMETH_HASHBUF_(n, W, w) \ |
| 1308 | static PyObject *polymeth_hashbuf##w(PyObject *me, PyObject *arg) \ |
| 1309 | { \ |
| 1310 | char *p; \ |
| 1311 | Py_ssize_t sz; \ |
| 1312 | octet b[SZ_##W]; \ |
| 1313 | if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \ |
| 1314 | if (sz > MASK##n) TYERR("string too long"); \ |
| 1315 | STORE##W(b, sz); poly1305_hash(P1305_CTX(me), b, sizeof(b)); \ |
| 1316 | poly1305_hash(P1305_CTX(me), p, sz); \ |
| 1317 | RETURN_ME; \ |
| 1318 | end: \ |
| 1319 | return (0); \ |
| 1320 | } |
| 1321 | DOUINTCONV(POLYMETH_HASHBUF_) |
| 1322 | |
| 1323 | static PyObject *polymeth_hashstrz(PyObject *me, PyObject *arg) |
| 1324 | { |
| 1325 | char *p; |
| 1326 | if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0); |
| 1327 | poly1305_hash(P1305_CTX(me), p, strlen(p) + 1); |
| 1328 | RETURN_ME; |
| 1329 | } |
| 1330 | |
| 1331 | static PyObject *polymeth_flush(PyObject *me, PyObject *arg) |
| 1332 | { |
| 1333 | if (!PyArg_ParseTuple(arg, ":flush")) return (0); |
| 1334 | poly1305_flush(P1305_CTX(me)); |
| 1335 | RETURN_ME; |
| 1336 | } |
| 1337 | |
| 1338 | static PyObject *polymeth_flushzero(PyObject *me, PyObject *arg) |
| 1339 | { |
| 1340 | if (!PyArg_ParseTuple(arg, ":flushzero")) return (0); |
| 1341 | poly1305_flushzero(P1305_CTX(me)); |
| 1342 | RETURN_ME; |
| 1343 | } |
| 1344 | |
| 1345 | static PyObject *polymeth_concat(PyObject *me, PyObject *arg) |
| 1346 | { |
| 1347 | PyObject *pre, *suff; |
| 1348 | if (!PyArg_ParseTuple(arg, "OO:concat", &pre, &suff)) return (0); |
| 1349 | if (!PyObject_TypeCheck(pre, poly1305hash_pytype) || |
| 1350 | !PyObject_TypeCheck(suff, poly1305hash_pytype)) |
| 1351 | TYERR("wanted a poly1305hash"); |
| 1352 | if (me->ob_type != pre->ob_type || me->ob_type != suff->ob_type) |
| 1353 | TYERR("key mismatch"); |
| 1354 | if (P1305_CTX(pre)->nbuf) VALERR("prefix is not block-aligned"); |
| 1355 | poly1305_concat(P1305_CTX(me), P1305_CTX(pre), P1305_CTX(suff)); |
| 1356 | RETURN_ME; |
| 1357 | end: |
| 1358 | return (0); |
| 1359 | } |
| 1360 | |
| 1361 | static PyObject *polymeth_done(PyObject *me, PyObject *arg) |
| 1362 | { |
| 1363 | PyObject *rc; |
| 1364 | if (!PyArg_ParseTuple(arg, ":done")) return (0); |
| 1365 | if (!(P1305_F(me) & f_mask)) VALERR("no mask"); |
| 1366 | rc = bytestring_pywrap(0, POLY1305_TAGSZ); |
| 1367 | poly1305_done(P1305_CTX(me), PyString_AS_STRING(rc)); |
| 1368 | return (rc); |
| 1369 | end: |
| 1370 | return (0); |
| 1371 | } |
| 1372 | |
| 1373 | static PyGetSetDef poly1305cls_pygetset[] = { |
| 1374 | #define GETSETNAME(op, name) poly1305cls##op##_##name |
| 1375 | GET (keysz, "PC.keysz -> acceptable key sizes") |
| 1376 | GET (masksz, "PC.masksz -> mask size") |
| 1377 | GET (tagsz, "PC.tagsz -> MAC output size") |
| 1378 | GET (name, "PC.name -> name of this kind of MAC") |
| 1379 | #undef GETSETNAME |
| 1380 | { 0 } |
| 1381 | }; |
| 1382 | |
| 1383 | static PyMethodDef poly1305hash_pymethods[] = { |
| 1384 | #define METHNAME(name) polymeth_##name |
| 1385 | METH (copy, "P.copy() -> PP") |
| 1386 | METH (hash, "P.hash(M)") |
| 1387 | #define METHU_(n, W, w) METH(hashu##w, "P.hashu" #w "(WORD)") |
| 1388 | DOUINTCONV(METHU_) |
| 1389 | #undef METHU_ |
| 1390 | #define METHBUF_(n, W, w) METH(hashbuf##w, "P.hashbuf" #w "(BYTES)") |
| 1391 | DOUINTCONV(METHBUF_) |
| 1392 | #undef METHBUF_ |
| 1393 | METH (hashstrz, "P.hashstrz(STRING)") |
| 1394 | METH (flush, "P.flush()") |
| 1395 | METH (flushzero, "P.flushzero()") |
| 1396 | METH (concat, "P.concat(PREFIX, SUFFIX)") |
| 1397 | METH (done, "P.done() -> TAG") |
| 1398 | #undef METHNAME |
| 1399 | { 0 } |
| 1400 | }; |
| 1401 | |
| 1402 | static PyTypeObject poly1305cls_pytype_skel = { |
| 1403 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1404 | "Poly1305Class", /* @tp_name@ */ |
| 1405 | sizeof(PyHeapTypeObject), /* @tp_basicsize@ */ |
| 1406 | 0, /* @tp_itemsize@ */ |
| 1407 | |
| 1408 | 0, /* @tp_dealloc@ */ |
| 1409 | 0, /* @tp_print@ */ |
| 1410 | 0, /* @tp_getattr@ */ |
| 1411 | 0, /* @tp_setattr@ */ |
| 1412 | 0, /* @tp_compare@ */ |
| 1413 | 0, /* @tp_repr@ */ |
| 1414 | 0, /* @tp_as_number@ */ |
| 1415 | 0, /* @tp_as_sequence@ */ |
| 1416 | 0, /* @tp_as_mapping@ */ |
| 1417 | 0, /* @tp_hash@ */ |
| 1418 | 0, /* @tp_call@ */ |
| 1419 | 0, /* @tp_str@ */ |
| 1420 | 0, /* @tp_getattro@ */ |
| 1421 | 0, /* @tp_setattro@ */ |
| 1422 | 0, /* @tp_as_buffer@ */ |
| 1423 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 1424 | Py_TPFLAGS_BASETYPE, |
| 1425 | |
| 1426 | /* @tp_doc@ */ |
| 1427 | "Poly1305 metametaclass. Best not to ask.", |
| 1428 | |
| 1429 | 0, /* @tp_traverse@ */ |
| 1430 | 0, /* @tp_clear@ */ |
| 1431 | 0, /* @tp_richcompare@ */ |
| 1432 | 0, /* @tp_weaklistoffset@ */ |
| 1433 | 0, /* @tp_iter@ */ |
| 1434 | 0, /* @tp_iternext@ */ |
| 1435 | 0, /* @tp_methods@ */ |
| 1436 | 0, /* @tp_members@ */ |
| 1437 | poly1305cls_pygetset, /* @tp_getset@ */ |
| 1438 | 0, /* @tp_base@ */ |
| 1439 | 0, /* @tp_dict@ */ |
| 1440 | 0, /* @tp_descr_get@ */ |
| 1441 | 0, /* @tp_descr_set@ */ |
| 1442 | 0, /* @tp_dictoffset@ */ |
| 1443 | 0, /* @tp_init@ */ |
| 1444 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 1445 | abstract_pynew, /* @tp_new@ */ |
| 1446 | 0, /* @tp_free@ */ |
| 1447 | 0 /* @tp_is_gc@ */ |
| 1448 | }; |
| 1449 | |
| 1450 | static PyTypeObject poly1305key_pytype_skel = { |
| 1451 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1452 | "poly1305", /* @tp_name@ */ |
| 1453 | sizeof(poly1305key_pyobj), /* @tp_basicsize@ */ |
| 1454 | 0, /* @tp_itemsize@ */ |
| 1455 | |
| 1456 | 0, /* @tp_dealloc@ */ |
| 1457 | 0, /* @tp_print@ */ |
| 1458 | 0, /* @tp_getattr@ */ |
| 1459 | 0, /* @tp_setattr@ */ |
| 1460 | 0, /* @tp_compare@ */ |
| 1461 | 0, /* @tp_repr@ */ |
| 1462 | 0, /* @tp_as_number@ */ |
| 1463 | 0, /* @tp_as_sequence@ */ |
| 1464 | 0, /* @tp_as_mapping@ */ |
| 1465 | 0, /* @tp_hash@ */ |
| 1466 | 0, /* @tp_call@ */ |
| 1467 | 0, /* @tp_str@ */ |
| 1468 | 0, /* @tp_getattro@ */ |
| 1469 | 0, /* @tp_setattro@ */ |
| 1470 | 0, /* @tp_as_buffer@ */ |
| 1471 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 1472 | Py_TPFLAGS_BASETYPE, |
| 1473 | |
| 1474 | /* @tp_doc@ */ |
| 1475 | "Poly1305 key.", |
| 1476 | |
| 1477 | 0, /* @tp_traverse@ */ |
| 1478 | 0, /* @tp_clear@ */ |
| 1479 | 0, /* @tp_richcompare@ */ |
| 1480 | 0, /* @tp_weaklistoffset@ */ |
| 1481 | 0, /* @tp_iter@ */ |
| 1482 | 0, /* @tp_iternext@ */ |
| 1483 | 0, /* @tp_methods@ */ |
| 1484 | 0, /* @tp_members@ */ |
| 1485 | 0, /* @tp_getset@ */ |
| 1486 | 0, /* @tp_base@ */ |
| 1487 | 0, /* @tp_dict@ */ |
| 1488 | 0, /* @tp_descr_get@ */ |
| 1489 | 0, /* @tp_descr_set@ */ |
| 1490 | 0, /* @tp_dictoffset@ */ |
| 1491 | 0, /* @tp_init@ */ |
| 1492 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 1493 | poly1305key_pynew, /* @tp_new@ */ |
| 1494 | 0, /* @tp_free@ */ |
| 1495 | 0 /* @tp_is_gc@ */ |
| 1496 | }; |
| 1497 | |
| 1498 | static PyTypeObject poly1305hash_pytype_skel = { |
| 1499 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1500 | "Poly1305Hash", /* @tp_name@ */ |
| 1501 | sizeof(poly1305hash_pyobj), /* @tp_basicsize@ */ |
| 1502 | 0, /* @tp_itemsize@ */ |
| 1503 | |
| 1504 | 0, /* @tp_dealloc@ */ |
| 1505 | 0, /* @tp_print@ */ |
| 1506 | 0, /* @tp_getattr@ */ |
| 1507 | 0, /* @tp_setattr@ */ |
| 1508 | 0, /* @tp_compare@ */ |
| 1509 | 0, /* @tp_repr@ */ |
| 1510 | 0, /* @tp_as_number@ */ |
| 1511 | 0, /* @tp_as_sequence@ */ |
| 1512 | 0, /* @tp_as_mapping@ */ |
| 1513 | 0, /* @tp_hash@ */ |
| 1514 | 0, /* @tp_call@ */ |
| 1515 | 0, /* @tp_str@ */ |
| 1516 | 0, /* @tp_getattro@ */ |
| 1517 | 0, /* @tp_setattro@ */ |
| 1518 | 0, /* @tp_as_buffer@ */ |
| 1519 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 1520 | Py_TPFLAGS_BASETYPE, |
| 1521 | |
| 1522 | /* @tp_doc@ */ |
| 1523 | "Poly1305 MAC context base class.", |
| 1524 | |
| 1525 | 0, /* @tp_traverse@ */ |
| 1526 | 0, /* @tp_clear@ */ |
| 1527 | 0, /* @tp_richcompare@ */ |
| 1528 | 0, /* @tp_weaklistoffset@ */ |
| 1529 | 0, /* @tp_iter@ */ |
| 1530 | 0, /* @tp_iternext@ */ |
| 1531 | poly1305hash_pymethods, /* @tp_methods@ */ |
| 1532 | 0, /* @tp_members@ */ |
| 1533 | 0, /* @tp_getset@ */ |
| 1534 | 0, /* @tp_base@ */ |
| 1535 | 0, /* @tp_dict@ */ |
| 1536 | 0, /* @tp_descr_get@ */ |
| 1537 | 0, /* @tp_descr_set@ */ |
| 1538 | 0, /* @tp_dictoffset@ */ |
| 1539 | 0, /* @tp_init@ */ |
| 1540 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 1541 | abstract_pynew, /* @tp_new@ */ |
| 1542 | 0, /* @tp_free@ */ |
| 1543 | 0 /* @tp_is_gc@ */ |
| 1544 | }; |
| 1545 | |
| 1546 | /*----- Special snowflake for HSalsa and HChaCha --------------------------*/ |
| 1547 | |
| 1548 | #define DEF_HDANCE(DANCE, HDANCE, dance, hdance) \ |
| 1549 | static PyObject *meth_##hdance##_prf(PyObject *me, PyObject *arg) \ |
| 1550 | { \ |
| 1551 | dance##_ctx dance; \ |
| 1552 | char *k, *n; \ |
| 1553 | Py_ssize_t ksz, nsz; \ |
| 1554 | PyObject *rc; \ |
| 1555 | if (!PyArg_ParseTuple(arg, "s#s#:" #hdance "_prf", \ |
| 1556 | &k, &ksz, &n, &nsz)) \ |
| 1557 | goto end; \ |
| 1558 | if (ksz != DANCE##_KEYSZ) VALERR("bad key length"); \ |
| 1559 | if (nsz != HDANCE##_INSZ) VALERR("bad input length"); \ |
| 1560 | rc = bytestring_pywrap(0, HSALSA20_OUTSZ); \ |
| 1561 | dance##_init(&dance, k, ksz, 0); \ |
| 1562 | hdance##_prf(&dance, n, PyString_AS_STRING(rc)); \ |
| 1563 | return (rc); \ |
| 1564 | end: \ |
| 1565 | return (0); \ |
| 1566 | } |
| 1567 | |
| 1568 | DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa20) |
| 1569 | DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa2012) |
| 1570 | DEF_HDANCE(SALSA20, HSALSA20, salsa20, hsalsa208) |
| 1571 | |
| 1572 | DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha20) |
| 1573 | DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha12) |
| 1574 | DEF_HDANCE(CHACHA, HCHACHA, chacha, hchacha8) |
| 1575 | |
| 1576 | /*----- Pseudorandom permutations -----------------------------------------*/ |
| 1577 | |
| 1578 | static PyTypeObject *gcprp_pytype, *gprp_pytype; |
| 1579 | |
| 1580 | typedef struct prpinfo { |
| 1581 | const char *name; |
| 1582 | const octet *keysz; |
| 1583 | size_t ctxsz; |
| 1584 | size_t blksz; |
| 1585 | void (*init)(void *, const void *, size_t); |
| 1586 | void (*eblk)(void *, const void *, void *); |
| 1587 | void (*dblk)(void *, const void *, void *); |
| 1588 | } prpinfo; |
| 1589 | |
| 1590 | #define PRP_DEF(PRE, pre) \ |
| 1591 | static void pre##_prpinit(void *ctx, const void *k, size_t ksz) \ |
| 1592 | { pre##_init(ctx, k, ksz); } \ |
| 1593 | static void pre##_prpeblk(void *ctx, const void *in, void *out) \ |
| 1594 | { \ |
| 1595 | uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \ |
| 1596 | pre##_eblk(ctx, w, w); BLKC_STORE(PRE, out, w); \ |
| 1597 | } \ |
| 1598 | static void pre##_prpdblk(void *ctx, const void *in, void *out) \ |
| 1599 | { \ |
| 1600 | uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \ |
| 1601 | pre##_dblk(ctx, w, w); BLKC_STORE(PRE, out, w); \ |
| 1602 | } \ |
| 1603 | static const prpinfo pre##_prpinfo = { \ |
| 1604 | #pre, pre##_keysz, sizeof(pre##_ctx), PRE##_BLKSZ, \ |
| 1605 | pre##_prpinit, pre##_prpeblk, pre##_prpdblk \ |
| 1606 | }; |
| 1607 | PRPS(PRP_DEF) |
| 1608 | |
| 1609 | static const struct prpinfo *const gprptab[] = { |
| 1610 | #define PRP_ENTRY(PRE, pre) &pre##_prpinfo, |
| 1611 | PRPS(PRP_ENTRY) |
| 1612 | 0 |
| 1613 | }; |
| 1614 | |
| 1615 | typedef struct gcprp_pyobj { |
| 1616 | PyHeapTypeObject ty; |
| 1617 | const prpinfo *prp; |
| 1618 | } gcprp_pyobj; |
| 1619 | #define GCPRP_PRP(o) (((gcprp_pyobj *)(o))->prp) |
| 1620 | |
| 1621 | typedef struct gprp_pyobj { |
| 1622 | PyObject_HEAD |
| 1623 | const prpinfo *prp; |
| 1624 | } gprp_pyobj; |
| 1625 | #define GPRP_PRP(o) (((gprp_pyobj *)(o))->prp) |
| 1626 | #define GPRP_CTX(o) (((gprp_pyobj *)(o)) + 1) |
| 1627 | |
| 1628 | typedef struct prp { |
| 1629 | const prpinfo *prp; |
| 1630 | void *ctx; |
| 1631 | } prp; |
| 1632 | |
| 1633 | static PyObject *gprp_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) |
| 1634 | { |
| 1635 | char *kwlist[] = { "key", 0 }; |
| 1636 | char *k; |
| 1637 | Py_ssize_t sz; |
| 1638 | const prpinfo *prp = GCPRP_PRP(ty); |
| 1639 | PyObject *me; |
| 1640 | |
| 1641 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz)) |
| 1642 | goto end; |
| 1643 | if (keysz(sz, prp->keysz) != sz) VALERR("bad key length"); |
| 1644 | me = (PyObject *)ty->tp_alloc(ty, 0); |
| 1645 | GPRP_PRP(me) = prp; |
| 1646 | prp->init(GPRP_CTX(me), k, sz); |
| 1647 | Py_INCREF(me); |
| 1648 | return (me); |
| 1649 | end: |
| 1650 | return (0); |
| 1651 | } |
| 1652 | |
| 1653 | static void gprp_pydealloc(PyObject *me) |
| 1654 | { Py_DECREF(me->ob_type); FREEOBJ(me); } |
| 1655 | |
| 1656 | static PyObject *gcprp_pywrap(const prpinfo *prp) |
| 1657 | { |
| 1658 | gcprp_pyobj *g = newtype(gcprp_pytype, 0, prp->name); |
| 1659 | g->prp = prp; |
| 1660 | g->ty.ht_type.tp_basicsize = sizeof(gprp_pyobj) + prp->ctxsz; |
| 1661 | g->ty.ht_type.tp_base = gprp_pytype; |
| 1662 | Py_INCREF(gprp_pytype); |
| 1663 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
| 1664 | Py_TPFLAGS_BASETYPE | |
| 1665 | Py_TPFLAGS_HEAPTYPE); |
| 1666 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; |
| 1667 | g->ty.ht_type.tp_free = 0; |
| 1668 | g->ty.ht_type.tp_new = gprp_pynew; |
| 1669 | typeready(&g->ty.ht_type); |
| 1670 | return ((PyObject *)g); |
| 1671 | } |
| 1672 | |
| 1673 | static PyObject *gcpget_name(PyObject *me, void *hunoz) |
| 1674 | { return (PyString_FromString(GCPRP_PRP(me)->name)); } |
| 1675 | static PyObject *gcpget_keysz(PyObject *me, void *hunoz) |
| 1676 | { return (keysz_pywrap(GCPRP_PRP(me)->keysz)); } |
| 1677 | static PyObject *gcpget_blksz(PyObject *me, void *hunoz) |
| 1678 | { return (PyInt_FromLong(GCPRP_PRP(me)->blksz)); } |
| 1679 | |
| 1680 | static PyObject *gpmeth_encrypt(PyObject *me, PyObject *arg) |
| 1681 | { |
| 1682 | char *p; |
| 1683 | Py_ssize_t n; |
| 1684 | PyObject *rc = 0; |
| 1685 | |
| 1686 | if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &n)) goto end; |
| 1687 | if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length"); |
| 1688 | rc = bytestring_pywrap(0, n); |
| 1689 | GPRP_PRP(me)->eblk(GPRP_CTX(me), p, PyString_AS_STRING(rc)); |
| 1690 | end: |
| 1691 | return (rc); |
| 1692 | } |
| 1693 | |
| 1694 | static PyObject *gpmeth_decrypt(PyObject *me, PyObject *arg) |
| 1695 | { |
| 1696 | char *p; |
| 1697 | Py_ssize_t n; |
| 1698 | PyObject *rc = 0; |
| 1699 | |
| 1700 | if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &n)) goto end; |
| 1701 | if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length"); |
| 1702 | rc = bytestring_pywrap(0, n); |
| 1703 | GPRP_PRP(me)->dblk(GPRP_CTX(me), p, PyString_AS_STRING(rc)); |
| 1704 | end: |
| 1705 | return (rc); |
| 1706 | } |
| 1707 | |
| 1708 | static PyGetSetDef gcprp_pygetset[] = { |
| 1709 | #define GETSETNAME(op, name) gcp##op##_##name |
| 1710 | GET (keysz, "CP.keysz -> acceptable key sizes") |
| 1711 | GET (blksz, "CP.blksz -> block size") |
| 1712 | GET (name, "CP.name -> name of this kind of PRP") |
| 1713 | #undef GETSETNAME |
| 1714 | { 0 } |
| 1715 | }; |
| 1716 | |
| 1717 | static PyMethodDef gprp_pymethods[] = { |
| 1718 | #define METHNAME(name) gpmeth_##name |
| 1719 | METH (encrypt, "P.encrypt(PT) -> CT") |
| 1720 | METH (decrypt, "P.decrypt(CT) -> PT") |
| 1721 | #undef METHNAME |
| 1722 | { 0 } |
| 1723 | }; |
| 1724 | |
| 1725 | static PyTypeObject gcprp_pytype_skel = { |
| 1726 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1727 | "GCPRP", /* @tp_name@ */ |
| 1728 | sizeof(gcprp_pyobj), /* @tp_basicsize@ */ |
| 1729 | 0, /* @tp_itemsize@ */ |
| 1730 | |
| 1731 | 0, /* @tp_dealloc@ */ |
| 1732 | 0, /* @tp_print@ */ |
| 1733 | 0, /* @tp_getattr@ */ |
| 1734 | 0, /* @tp_setattr@ */ |
| 1735 | 0, /* @tp_compare@ */ |
| 1736 | 0, /* @tp_repr@ */ |
| 1737 | 0, /* @tp_as_number@ */ |
| 1738 | 0, /* @tp_as_sequence@ */ |
| 1739 | 0, /* @tp_as_mapping@ */ |
| 1740 | 0, /* @tp_hash@ */ |
| 1741 | 0, /* @tp_call@ */ |
| 1742 | 0, /* @tp_str@ */ |
| 1743 | 0, /* @tp_getattro@ */ |
| 1744 | 0, /* @tp_setattro@ */ |
| 1745 | 0, /* @tp_as_buffer@ */ |
| 1746 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 1747 | Py_TPFLAGS_BASETYPE, |
| 1748 | |
| 1749 | /* @tp_doc@ */ |
| 1750 | "Pseudorandom permutation metaclass.", |
| 1751 | |
| 1752 | 0, /* @tp_traverse@ */ |
| 1753 | 0, /* @tp_clear@ */ |
| 1754 | 0, /* @tp_richcompare@ */ |
| 1755 | 0, /* @tp_weaklistoffset@ */ |
| 1756 | 0, /* @tp_iter@ */ |
| 1757 | 0, /* @tp_iternext@ */ |
| 1758 | 0, /* @tp_methods@ */ |
| 1759 | 0, /* @tp_members@ */ |
| 1760 | gcprp_pygetset, /* @tp_getset@ */ |
| 1761 | 0, /* @tp_base@ */ |
| 1762 | 0, /* @tp_dict@ */ |
| 1763 | 0, /* @tp_descr_get@ */ |
| 1764 | 0, /* @tp_descr_set@ */ |
| 1765 | 0, /* @tp_dictoffset@ */ |
| 1766 | 0, /* @tp_init@ */ |
| 1767 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 1768 | abstract_pynew, /* @tp_new@ */ |
| 1769 | 0, /* @tp_free@ */ |
| 1770 | 0 /* @tp_is_gc@ */ |
| 1771 | }; |
| 1772 | |
| 1773 | static PyTypeObject gprp_pytype_skel = { |
| 1774 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1775 | "GPRP", /* @tp_name@ */ |
| 1776 | sizeof(gprp_pyobj), /* @tp_basicsize@ */ |
| 1777 | 0, /* @tp_itemsize@ */ |
| 1778 | |
| 1779 | gprp_pydealloc, /* @tp_dealloc@ */ |
| 1780 | 0, /* @tp_print@ */ |
| 1781 | 0, /* @tp_getattr@ */ |
| 1782 | 0, /* @tp_setattr@ */ |
| 1783 | 0, /* @tp_compare@ */ |
| 1784 | 0, /* @tp_repr@ */ |
| 1785 | 0, /* @tp_as_number@ */ |
| 1786 | 0, /* @tp_as_sequence@ */ |
| 1787 | 0, /* @tp_as_mapping@ */ |
| 1788 | 0, /* @tp_hash@ */ |
| 1789 | 0, /* @tp_call@ */ |
| 1790 | 0, /* @tp_str@ */ |
| 1791 | 0, /* @tp_getattro@ */ |
| 1792 | 0, /* @tp_setattro@ */ |
| 1793 | 0, /* @tp_as_buffer@ */ |
| 1794 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 1795 | Py_TPFLAGS_BASETYPE, |
| 1796 | |
| 1797 | /* @tp_doc@ */ |
| 1798 | "Pseudorandom permutation, abstract base class.", |
| 1799 | |
| 1800 | 0, /* @tp_traverse@ */ |
| 1801 | 0, /* @tp_clear@ */ |
| 1802 | 0, /* @tp_richcompare@ */ |
| 1803 | 0, /* @tp_weaklistoffset@ */ |
| 1804 | 0, /* @tp_iter@ */ |
| 1805 | 0, /* @tp_iternext@ */ |
| 1806 | gprp_pymethods, /* @tp_methods@ */ |
| 1807 | 0, /* @tp_members@ */ |
| 1808 | 0, /* @tp_getset@ */ |
| 1809 | 0, /* @tp_base@ */ |
| 1810 | 0, /* @tp_dict@ */ |
| 1811 | 0, /* @tp_descr_get@ */ |
| 1812 | 0, /* @tp_descr_set@ */ |
| 1813 | 0, /* @tp_dictoffset@ */ |
| 1814 | 0, /* @tp_init@ */ |
| 1815 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 1816 | abstract_pynew, /* @tp_new@ */ |
| 1817 | 0, /* @tp_free@ */ |
| 1818 | 0 /* @tp_is_gc@ */ |
| 1819 | }; |
| 1820 | |
| 1821 | /*----- Main code ---------------------------------------------------------*/ |
| 1822 | |
| 1823 | static PyMethodDef methods[] = { |
| 1824 | #define METHNAME(func) meth_##func |
| 1825 | METH (_KeySZ_fromdl, "\ |
| 1826 | fromdl(N) -> M: convert integer discrete log field size to work factor") |
| 1827 | METH (_KeySZ_fromschnorr, "\ |
| 1828 | fromschnorr(N) -> M: convert Schnorr group order to work factor") |
| 1829 | METH (_KeySZ_fromif, "\ |
| 1830 | fromif(N) -> M: convert integer factorization problem size to work factor") |
| 1831 | METH (_KeySZ_fromec, "\ |
| 1832 | fromec(N) -> M: convert elliptic curve group order to work factor") |
| 1833 | METH (_KeySZ_todl, "\ |
| 1834 | todl(N) -> M: convert work factor to integer discrete log field size") |
| 1835 | METH (_KeySZ_toschnorr, "\ |
| 1836 | toschnorr(N) -> M: convert work factor to Schnorr group order") |
| 1837 | METH (_KeySZ_toif, "\ |
| 1838 | toif(N) -> M: convert work factor to integer factorization problem size") |
| 1839 | METH (_KeySZ_toec, "\ |
| 1840 | toec(N) -> M: convert work factor to elliptic curve group order") |
| 1841 | METH (_KeySZ_toec, "\ |
| 1842 | toec(N) -> M: convert work factor to elliptic curve group order") |
| 1843 | #define METH_HDANCE(hdance, HDance) METH(hdance##_prf, "\ |
| 1844 | " #hdance "_prf(K, N) -> H: calculate " HDance " hash of N with K") |
| 1845 | METH_HDANCE(hsalsa20, "HSalsa20") |
| 1846 | METH_HDANCE(hsalsa2012, "HSalsa20/12") |
| 1847 | METH_HDANCE(hsalsa208, "HSalsa20/8") |
| 1848 | METH_HDANCE(hchacha20, "HChaCha20") |
| 1849 | METH_HDANCE(hchacha12, "HChaCha12") |
| 1850 | METH_HDANCE(hchacha8, "HChaCha8") |
| 1851 | #undef METH_DANCE |
| 1852 | #undef METHNAME |
| 1853 | { 0 } |
| 1854 | }; |
| 1855 | |
| 1856 | void algorithms_pyinit(void) |
| 1857 | { |
| 1858 | INITTYPE(keysz, root); |
| 1859 | INITTYPE(keyszany, keysz); |
| 1860 | INITTYPE(keyszrange, keysz); |
| 1861 | INITTYPE(keyszset, keysz); |
| 1862 | INITTYPE(gccipher, type); |
| 1863 | INITTYPE(gcipher, root); |
| 1864 | INITTYPE(gchash, type); |
| 1865 | INITTYPE(ghash, root); |
| 1866 | INITTYPE(gcmac, type); |
| 1867 | INITTYPE(gmac, type); |
| 1868 | INITTYPE(gmhash, ghash); |
| 1869 | INITTYPE(poly1305cls, type); |
| 1870 | INITTYPE_META(poly1305key, type, poly1305cls); |
| 1871 | INITTYPE(poly1305hash, root); |
| 1872 | INITTYPE(gcprp, type); |
| 1873 | INITTYPE(gprp, root); |
| 1874 | addmethods(methods); |
| 1875 | } |
| 1876 | |
| 1877 | GEN(gcciphers, cipher) |
| 1878 | GEN(gchashes, hash) |
| 1879 | GEN(gcmacs, mac) |
| 1880 | #define gcprp prpinfo |
| 1881 | GEN(gcprps, prp) |
| 1882 | |
| 1883 | void algorithms_pyinsert(PyObject *mod) |
| 1884 | { |
| 1885 | PyObject *d; |
| 1886 | INSERT("KeySZ", keysz_pytype); |
| 1887 | INSERT("KeySZAny", keyszany_pytype); |
| 1888 | INSERT("KeySZRange", keyszrange_pytype); |
| 1889 | INSERT("KeySZSet", keyszset_pytype); |
| 1890 | INSERT("GCCipher", gccipher_pytype); |
| 1891 | INSERT("GCipher", gcipher_pytype); |
| 1892 | INSERT("gcciphers", gcciphers()); |
| 1893 | INSERT("GCHash", gchash_pytype); |
| 1894 | INSERT("GHash", ghash_pytype); |
| 1895 | INSERT("gchashes", d = gchashes()); |
| 1896 | sha_pyobj = PyDict_GetItemString(d, "sha"); Py_INCREF(sha_pyobj); |
| 1897 | has160_pyobj = PyDict_GetItemString(d, "has160"); Py_INCREF(has160_pyobj); |
| 1898 | INSERT("GCMAC", gcmac_pytype); |
| 1899 | INSERT("GMAC", gmac_pytype); |
| 1900 | INSERT("GMACHash", gmhash_pytype); |
| 1901 | INSERT("gcmacs", gcmacs()); |
| 1902 | INSERT("Poly1305Class", poly1305cls_pytype); |
| 1903 | INSERT("poly1305", poly1305key_pytype); |
| 1904 | INSERT("Poly1305Hash", poly1305hash_pytype); |
| 1905 | INSERT("GCPRP", gcprp_pytype); |
| 1906 | INSERT("GPRP", gprp_pytype); |
| 1907 | INSERT("gcprps", gcprps()); |
| 1908 | } |
| 1909 | |
| 1910 | /*----- That's all, folks -------------------------------------------------*/ |