| 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 | int 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 | int 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 | int 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 | int sz; |
| 578 | |
| 579 | if (!PyArg_ParseTuple(arg, "s#:setiv", &p, &sz)) goto end; |
| 580 | if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode"); |
| 581 | if (sz != GC_CLASS(GCIPHER_C(me))->blksz) VALERR("bad IV length"); |
| 582 | GC_SETIV(GCIPHER_C(me), p); |
| 583 | RETURN_ME; |
| 584 | end: |
| 585 | return (0); |
| 586 | } |
| 587 | |
| 588 | static PyObject *gcmeth_bdry(PyObject *me, PyObject *arg) |
| 589 | { |
| 590 | if (!PyArg_ParseTuple(arg, ":bdry")) goto end; |
| 591 | if (!GC_CLASS(GCIPHER_C(me))->blksz) VALERR("not a block cipher mode"); |
| 592 | GC_BDRY(GCIPHER_C(me)); |
| 593 | RETURN_ME; |
| 594 | end: |
| 595 | return (0); |
| 596 | } |
| 597 | |
| 598 | static PyGetSetDef gccipher_pygetset[] = { |
| 599 | #define GETSETNAME(op, name) gcc##op##_##name |
| 600 | GET (keysz, "CC.keysz -> acceptable key sizes") |
| 601 | GET (blksz, "CC.blksz -> block size, or zero") |
| 602 | GET (name, "CC.name -> name of this kind of cipher") |
| 603 | #undef GETSETNAME |
| 604 | { 0 } |
| 605 | }; |
| 606 | |
| 607 | static PyMethodDef gcipher_pymethods[] = { |
| 608 | #define METHNAME(name) gcmeth_##name |
| 609 | METH (encrypt, "C.encrypt(PT) -> CT") |
| 610 | METH (enczero, "C.enczero(N) -> CT") |
| 611 | METH (decrypt, "C.decrypt(CT) -> PT") |
| 612 | METH (deczero, "C.deczero(N) -> PT") |
| 613 | METH (setiv, "C.setiv(IV)") |
| 614 | METH (bdry, "C.bdry()") |
| 615 | #undef METHNAME |
| 616 | { 0 } |
| 617 | }; |
| 618 | |
| 619 | static PyTypeObject gccipher_pytype_skel = { |
| 620 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 621 | "GCCipher", /* @tp_name@ */ |
| 622 | sizeof(gccipher_pyobj), /* @tp_basicsize@ */ |
| 623 | 0, /* @tp_itemsize@ */ |
| 624 | |
| 625 | 0, /* @tp_dealloc@ */ |
| 626 | 0, /* @tp_print@ */ |
| 627 | 0, /* @tp_getattr@ */ |
| 628 | 0, /* @tp_setattr@ */ |
| 629 | 0, /* @tp_compare@ */ |
| 630 | 0, /* @tp_repr@ */ |
| 631 | 0, /* @tp_as_number@ */ |
| 632 | 0, /* @tp_as_sequence@ */ |
| 633 | 0, /* @tp_as_mapping@ */ |
| 634 | 0, /* @tp_hash@ */ |
| 635 | 0, /* @tp_call@ */ |
| 636 | 0, /* @tp_str@ */ |
| 637 | 0, /* @tp_getattro@ */ |
| 638 | 0, /* @tp_setattro@ */ |
| 639 | 0, /* @tp_as_buffer@ */ |
| 640 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 641 | Py_TPFLAGS_BASETYPE, |
| 642 | |
| 643 | /* @tp_doc@ */ |
| 644 | "Symmetric cipher metaclass.", |
| 645 | |
| 646 | 0, /* @tp_traverse@ */ |
| 647 | 0, /* @tp_clear@ */ |
| 648 | 0, /* @tp_richcompare@ */ |
| 649 | 0, /* @tp_weaklistoffset@ */ |
| 650 | 0, /* @tp_iter@ */ |
| 651 | 0, /* @tp_iternext@ */ |
| 652 | 0, /* @tp_methods@ */ |
| 653 | 0, /* @tp_members@ */ |
| 654 | gccipher_pygetset, /* @tp_getset@ */ |
| 655 | 0, /* @tp_base@ */ |
| 656 | 0, /* @tp_dict@ */ |
| 657 | 0, /* @tp_descr_get@ */ |
| 658 | 0, /* @tp_descr_set@ */ |
| 659 | 0, /* @tp_dictoffset@ */ |
| 660 | 0, /* @tp_init@ */ |
| 661 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 662 | abstract_pynew, /* @tp_new@ */ |
| 663 | 0, /* @tp_free@ */ |
| 664 | 0 /* @tp_is_gc@ */ |
| 665 | }; |
| 666 | |
| 667 | static PyTypeObject gcipher_pytype_skel = { |
| 668 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 669 | "GCipher", /* @tp_name@ */ |
| 670 | sizeof(gcipher_pyobj), /* @tp_basicsize@ */ |
| 671 | 0, /* @tp_itemsize@ */ |
| 672 | |
| 673 | gcipher_pydealloc, /* @tp_dealloc@ */ |
| 674 | 0, /* @tp_print@ */ |
| 675 | 0, /* @tp_getattr@ */ |
| 676 | 0, /* @tp_setattr@ */ |
| 677 | 0, /* @tp_compare@ */ |
| 678 | 0, /* @tp_repr@ */ |
| 679 | 0, /* @tp_as_number@ */ |
| 680 | 0, /* @tp_as_sequence@ */ |
| 681 | 0, /* @tp_as_mapping@ */ |
| 682 | 0, /* @tp_hash@ */ |
| 683 | 0, /* @tp_call@ */ |
| 684 | 0, /* @tp_str@ */ |
| 685 | 0, /* @tp_getattro@ */ |
| 686 | 0, /* @tp_setattro@ */ |
| 687 | 0, /* @tp_as_buffer@ */ |
| 688 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 689 | Py_TPFLAGS_BASETYPE, |
| 690 | |
| 691 | /* @tp_doc@ */ |
| 692 | "Symmetric cipher, abstract base class.", |
| 693 | |
| 694 | 0, /* @tp_traverse@ */ |
| 695 | 0, /* @tp_clear@ */ |
| 696 | 0, /* @tp_richcompare@ */ |
| 697 | 0, /* @tp_weaklistoffset@ */ |
| 698 | 0, /* @tp_iter@ */ |
| 699 | 0, /* @tp_iternext@ */ |
| 700 | gcipher_pymethods, /* @tp_methods@ */ |
| 701 | 0, /* @tp_members@ */ |
| 702 | 0, /* @tp_getset@ */ |
| 703 | 0, /* @tp_base@ */ |
| 704 | 0, /* @tp_dict@ */ |
| 705 | 0, /* @tp_descr_get@ */ |
| 706 | 0, /* @tp_descr_set@ */ |
| 707 | 0, /* @tp_dictoffset@ */ |
| 708 | 0, /* @tp_init@ */ |
| 709 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 710 | abstract_pynew, /* @tp_new@ */ |
| 711 | 0, /* @tp_free@ */ |
| 712 | 0 /* @tp_is_gc@ */ |
| 713 | }; |
| 714 | |
| 715 | /*----- Hash functions ----------------------------------------------------*/ |
| 716 | |
| 717 | PyTypeObject *gchash_pytype, *ghash_pytype; |
| 718 | |
| 719 | CONVFUNC(gchash, gchash *, GCHASH_CH) |
| 720 | CONVFUNC(ghash, ghash *, GHASH_H) |
| 721 | |
| 722 | static PyObject *ghash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) |
| 723 | { |
| 724 | char *kwlist[] = { 0 }; |
| 725 | if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", kwlist)) |
| 726 | goto end; |
| 727 | return (ghash_pywrap((PyObject *)ty, GH_INIT(GCHASH_CH(ty)), f_freeme)); |
| 728 | end: |
| 729 | return (0); |
| 730 | } |
| 731 | |
| 732 | PyObject *gchash_pywrap(gchash *ch) |
| 733 | { |
| 734 | gchash_pyobj *g = newtype(gchash_pytype, 0, ch->name); |
| 735 | g->ch = ch; |
| 736 | g->ty.ht_type.tp_basicsize = sizeof(ghash_pyobj); |
| 737 | g->ty.ht_type.tp_base = ghash_pytype; |
| 738 | Py_INCREF(ghash_pytype); |
| 739 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
| 740 | Py_TPFLAGS_BASETYPE | |
| 741 | Py_TPFLAGS_HEAPTYPE); |
| 742 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; |
| 743 | g->ty.ht_type.tp_free = 0; |
| 744 | g->ty.ht_type.tp_new = ghash_pynew; |
| 745 | typeready(&g->ty.ht_type); |
| 746 | return ((PyObject *)g); |
| 747 | } |
| 748 | |
| 749 | PyObject *ghash_pywrap(PyObject *cobj, ghash *h, unsigned f) |
| 750 | { |
| 751 | ghash_pyobj *g; |
| 752 | if (!cobj) cobj = gchash_pywrap((/*unconst*/ gchash *)GH_CLASS(h)); |
| 753 | else Py_INCREF(cobj); |
| 754 | g = PyObject_NEW(ghash_pyobj, (PyTypeObject *)cobj); |
| 755 | g->h = h; |
| 756 | g->f = f; |
| 757 | return ((PyObject *)g); |
| 758 | } |
| 759 | |
| 760 | static void ghash_pydealloc(PyObject *me) |
| 761 | { |
| 762 | if (GHASH_F(me) & f_freeme) |
| 763 | GH_DESTROY(GHASH_H(me)); |
| 764 | Py_DECREF(me->ob_type); |
| 765 | FREEOBJ(me); |
| 766 | } |
| 767 | |
| 768 | static PyObject *gchget_name(PyObject *me, void *hunoz) |
| 769 | { return (PyString_FromString(GCHASH_CH(me)->name)); } |
| 770 | |
| 771 | static PyObject *gchget_hashsz(PyObject *me, void *hunoz) |
| 772 | { return (PyInt_FromLong(GCHASH_CH(me)->hashsz)); } |
| 773 | |
| 774 | static PyObject *gchget_bufsz(PyObject *me, void *hunoz) |
| 775 | { return (PyInt_FromLong(GCHASH_CH(me)->bufsz)); } |
| 776 | |
| 777 | static PyObject *ghmeth_hash(PyObject *me, PyObject *arg) |
| 778 | { |
| 779 | char *p; |
| 780 | int sz; |
| 781 | if (!PyArg_ParseTuple(arg, "s#:hash", &p, &sz)) return (0); |
| 782 | GH_HASH(GHASH_H(me), p, sz); |
| 783 | RETURN_ME; |
| 784 | } |
| 785 | |
| 786 | static PyObject *ghmeth_done(PyObject *me, PyObject *arg) |
| 787 | { |
| 788 | ghash *g; |
| 789 | PyObject *rc; |
| 790 | if (!PyArg_ParseTuple(arg, ":done")) return (0); |
| 791 | g = GH_COPY(GHASH_H(me)); |
| 792 | rc = bytestring_pywrap(0, g->ops->c->hashsz); |
| 793 | GH_DONE(g, PyString_AS_STRING(rc)); |
| 794 | GH_DESTROY(g); |
| 795 | return (rc); |
| 796 | } |
| 797 | |
| 798 | static PyGetSetDef gchash_pygetset[] = { |
| 799 | #define GETSETNAME(op, name) gch##op##_##name |
| 800 | GET (bufsz, "CH.bufsz -> hash buffer size, or zero") |
| 801 | GET (hashsz, "CH.blksz -> hash output size") |
| 802 | GET (name, "CH.name -> name of this kind of hash") |
| 803 | #undef GETSETNAME |
| 804 | { 0 } |
| 805 | }; |
| 806 | |
| 807 | #define GHMETH_HASHU_(n, W, w) \ |
| 808 | static PyObject *ghmeth_hashu##w(PyObject *me, PyObject *arg) \ |
| 809 | { \ |
| 810 | uint##n x; \ |
| 811 | if (!PyArg_ParseTuple(arg, "O&:hashu" #w, convu##n, &x)) goto end; \ |
| 812 | GH_HASHU##W(GHASH_H(me), x); \ |
| 813 | RETURN_ME; \ |
| 814 | end: \ |
| 815 | return (0); \ |
| 816 | } |
| 817 | DOUINTCONV(GHMETH_HASHU_) |
| 818 | |
| 819 | #define GHMETH_HASHBUF_(n, W, w) \ |
| 820 | static PyObject *ghmeth_hashbuf##w(PyObject *me, PyObject *arg) \ |
| 821 | { \ |
| 822 | char *p; \ |
| 823 | int sz; \ |
| 824 | if (!PyArg_ParseTuple(arg, "s#:hashbuf" #w, &p, &sz)) goto end; \ |
| 825 | if (sz > MASK##n) TYERR("string too long"); \ |
| 826 | GH_HASHBUF##W(GHASH_H(me), p, sz); \ |
| 827 | RETURN_ME; \ |
| 828 | end: \ |
| 829 | return (0); \ |
| 830 | } |
| 831 | DOUINTCONV(GHMETH_HASHBUF_) |
| 832 | |
| 833 | static PyObject *ghmeth_hashstrz(PyObject *me, PyObject *arg) |
| 834 | { |
| 835 | char *p; |
| 836 | if (!PyArg_ParseTuple(arg, "s:hashstrz", &p)) return (0); |
| 837 | GH_HASHSTRZ(GHASH_H(me), p); |
| 838 | RETURN_ME; |
| 839 | } |
| 840 | |
| 841 | static PyMethodDef ghash_pymethods[] = { |
| 842 | #define METHNAME(name) ghmeth_##name |
| 843 | METH (hash, "H.hash(M)") |
| 844 | #define METHU_(n, W, w) METH(hashu##w, "H.hashu" #w "(WORD)") |
| 845 | DOUINTCONV(METHU_) |
| 846 | #define METHBUF_(n, W, w) METH(hashbuf##w, "H.hashbuf" #w "(BYTES)") |
| 847 | DOUINTCONV(METHBUF_) |
| 848 | METH (hashstrz, "H.hashstrz(STRING)") |
| 849 | METH (done, "H.done() -> HASH") |
| 850 | #undef METHNAME |
| 851 | { 0 } |
| 852 | }; |
| 853 | |
| 854 | static PyTypeObject gchash_pytype_skel = { |
| 855 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 856 | "GCHash", /* @tp_name@ */ |
| 857 | sizeof(gchash_pyobj), /* @tp_basicsize@ */ |
| 858 | 0, /* @tp_itemsize@ */ |
| 859 | |
| 860 | 0, /* @tp_dealloc@ */ |
| 861 | 0, /* @tp_print@ */ |
| 862 | 0, /* @tp_getattr@ */ |
| 863 | 0, /* @tp_setattr@ */ |
| 864 | 0, /* @tp_compare@ */ |
| 865 | 0, /* @tp_repr@ */ |
| 866 | 0, /* @tp_as_number@ */ |
| 867 | 0, /* @tp_as_sequence@ */ |
| 868 | 0, /* @tp_as_mapping@ */ |
| 869 | 0, /* @tp_hash@ */ |
| 870 | 0, /* @tp_call@ */ |
| 871 | 0, /* @tp_str@ */ |
| 872 | 0, /* @tp_getattro@ */ |
| 873 | 0, /* @tp_setattro@ */ |
| 874 | 0, /* @tp_as_buffer@ */ |
| 875 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 876 | Py_TPFLAGS_BASETYPE, |
| 877 | |
| 878 | /* @tp_doc@ */ |
| 879 | "Hash function metaclass.", |
| 880 | |
| 881 | 0, /* @tp_traverse@ */ |
| 882 | 0, /* @tp_clear@ */ |
| 883 | 0, /* @tp_richcompare@ */ |
| 884 | 0, /* @tp_weaklistoffset@ */ |
| 885 | 0, /* @tp_iter@ */ |
| 886 | 0, /* @tp_iternext@ */ |
| 887 | 0, /* @tp_methods@ */ |
| 888 | 0, /* @tp_members@ */ |
| 889 | gchash_pygetset, /* @tp_getset@ */ |
| 890 | 0, /* @tp_base@ */ |
| 891 | 0, /* @tp_dict@ */ |
| 892 | 0, /* @tp_descr_get@ */ |
| 893 | 0, /* @tp_descr_set@ */ |
| 894 | 0, /* @tp_dictoffset@ */ |
| 895 | 0, /* @tp_init@ */ |
| 896 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 897 | abstract_pynew, /* @tp_new@ */ |
| 898 | 0, /* @tp_free@ */ |
| 899 | 0 /* @tp_is_gc@ */ |
| 900 | }; |
| 901 | |
| 902 | static PyTypeObject ghash_pytype_skel = { |
| 903 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 904 | "GHash", /* @tp_name@ */ |
| 905 | sizeof(ghash_pyobj), /* @tp_basicsize@ */ |
| 906 | 0, /* @tp_itemsize@ */ |
| 907 | |
| 908 | ghash_pydealloc, /* @tp_dealloc@ */ |
| 909 | 0, /* @tp_print@ */ |
| 910 | 0, /* @tp_getattr@ */ |
| 911 | 0, /* @tp_setattr@ */ |
| 912 | 0, /* @tp_compare@ */ |
| 913 | 0, /* @tp_repr@ */ |
| 914 | 0, /* @tp_as_number@ */ |
| 915 | 0, /* @tp_as_sequence@ */ |
| 916 | 0, /* @tp_as_mapping@ */ |
| 917 | 0, /* @tp_hash@ */ |
| 918 | 0, /* @tp_call@ */ |
| 919 | 0, /* @tp_str@ */ |
| 920 | 0, /* @tp_getattro@ */ |
| 921 | 0, /* @tp_setattro@ */ |
| 922 | 0, /* @tp_as_buffer@ */ |
| 923 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 924 | Py_TPFLAGS_BASETYPE, |
| 925 | |
| 926 | /* @tp_doc@ */ |
| 927 | "Hash function, abstract base class.", |
| 928 | |
| 929 | 0, /* @tp_traverse@ */ |
| 930 | 0, /* @tp_clear@ */ |
| 931 | 0, /* @tp_richcompare@ */ |
| 932 | 0, /* @tp_weaklistoffset@ */ |
| 933 | 0, /* @tp_iter@ */ |
| 934 | 0, /* @tp_iternext@ */ |
| 935 | ghash_pymethods, /* @tp_methods@ */ |
| 936 | 0, /* @tp_members@ */ |
| 937 | 0, /* @tp_getset@ */ |
| 938 | 0, /* @tp_base@ */ |
| 939 | 0, /* @tp_dict@ */ |
| 940 | 0, /* @tp_descr_get@ */ |
| 941 | 0, /* @tp_descr_set@ */ |
| 942 | 0, /* @tp_dictoffset@ */ |
| 943 | 0, /* @tp_init@ */ |
| 944 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 945 | abstract_pynew, /* @tp_new@ */ |
| 946 | 0, /* @tp_free@ */ |
| 947 | 0 /* @tp_is_gc@ */ |
| 948 | }; |
| 949 | |
| 950 | /*----- Message authentication --------------------------------------------*/ |
| 951 | |
| 952 | PyTypeObject *gcmac_pytype, *gmac_pytype, *gmhash_pytype; |
| 953 | |
| 954 | CONVFUNC(gcmac, gcmac *, GCMAC_CM) |
| 955 | CONVFUNC(gmac, gmac *, GMAC_M) |
| 956 | CONVFUNC(gmhash, ghash *, GHASH_H) |
| 957 | |
| 958 | static PyObject *gmac_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) |
| 959 | { |
| 960 | char *kwlist[] = { "k", 0 }; |
| 961 | char *k; |
| 962 | int sz; |
| 963 | |
| 964 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz)) |
| 965 | goto end; |
| 966 | if (keysz(sz, GCMAC_CM(ty)->keysz) != sz) VALERR("bad key length"); |
| 967 | return (gmac_pywrap((PyObject *)ty, |
| 968 | GM_KEY(GCMAC_CM(ty), k, sz), |
| 969 | f_freeme)); |
| 970 | end: |
| 971 | return (0); |
| 972 | } |
| 973 | |
| 974 | static PyObject *gmhash_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) |
| 975 | { |
| 976 | char *kwlist[] = { 0 }; |
| 977 | ghash_pyobj *g; |
| 978 | |
| 979 | if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", kwlist)) return (0); |
| 980 | g = PyObject_NEW(ghash_pyobj, ty); |
| 981 | g->h = GM_INIT(GMAC_M(ty)); |
| 982 | g->f = f_freeme; |
| 983 | Py_INCREF(ty); |
| 984 | return ((PyObject *)g); |
| 985 | } |
| 986 | |
| 987 | PyObject *gcmac_pywrap(gcmac *cm) |
| 988 | { |
| 989 | gcmac_pyobj *g = newtype(gcmac_pytype, 0, cm->name); |
| 990 | g->cm = cm; |
| 991 | g->ty.ht_type.tp_basicsize = sizeof(gmac_pyobj); |
| 992 | g->ty.ht_type.tp_base = gmac_pytype; |
| 993 | Py_INCREF(gmac_pytype); |
| 994 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
| 995 | Py_TPFLAGS_BASETYPE | |
| 996 | Py_TPFLAGS_HEAPTYPE); |
| 997 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; |
| 998 | g->ty.ht_type.tp_free = 0; |
| 999 | g->ty.ht_type.tp_new = gmac_pynew; |
| 1000 | typeready(&g->ty.ht_type); |
| 1001 | return ((PyObject *)g); |
| 1002 | } |
| 1003 | |
| 1004 | PyObject *gmac_pywrap(PyObject *cobj, gmac *m, unsigned f) |
| 1005 | { |
| 1006 | gmac_pyobj *g; |
| 1007 | if (!cobj) cobj = gcmac_pywrap((/*unconst*/ gcmac *)GM_CLASS(m)); |
| 1008 | else Py_INCREF(cobj); |
| 1009 | g = newtype((PyTypeObject *)cobj, 0, 0); |
| 1010 | g->ty.ht_name = PyString_FromFormat("%s(keyed)", m->ops->c->name); |
| 1011 | g->ty.ht_type.tp_name = PyString_AS_STRING(g->ty.ht_name); |
| 1012 | g->ty.ht_type.tp_base = gmhash_pytype; |
| 1013 | Py_INCREF(gmac_pytype); |
| 1014 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
| 1015 | Py_TPFLAGS_BASETYPE | |
| 1016 | Py_TPFLAGS_HEAPTYPE); |
| 1017 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; |
| 1018 | g->ty.ht_type.tp_free = 0; |
| 1019 | g->ty.ht_type.tp_new = gmhash_pynew; |
| 1020 | typeready(&g->ty.ht_type); |
| 1021 | g->m = m; |
| 1022 | g->f = f; |
| 1023 | return ((PyObject *)g); |
| 1024 | } |
| 1025 | |
| 1026 | static void gmac_pydealloc(PyObject *me) |
| 1027 | { |
| 1028 | if (GMAC_F(me) & f_freeme) |
| 1029 | GM_DESTROY(GMAC_M(me)); |
| 1030 | Py_DECREF(me->ob_type); |
| 1031 | PyType_Type.tp_dealloc(me); |
| 1032 | } |
| 1033 | |
| 1034 | static PyObject *gcmget_name(PyObject *me, void *hunoz) |
| 1035 | { return (PyString_FromString(GCMAC_CM(me)->name)); } |
| 1036 | |
| 1037 | static PyObject *gcmget_keysz(PyObject *me, void *hunoz) |
| 1038 | { return (keysz_pywrap(GCMAC_CM(me)->keysz)); } |
| 1039 | |
| 1040 | static PyObject *gcmget_tagsz(PyObject *me, void *hunoz) |
| 1041 | { return (PyInt_FromLong(GCMAC_CM(me)->hashsz)); } |
| 1042 | |
| 1043 | static PyGetSetDef gcmac_pygetset[] = { |
| 1044 | #define GETSETNAME(op, name) gcm##op##_##name |
| 1045 | GET (keysz, "CM.keysz -> acceptable key sizes") |
| 1046 | GET (tagsz, "CM.tagsz -> MAC output size") |
| 1047 | GET (name, "CM.name -> name of this kind of MAC") |
| 1048 | #undef GETSETNAME |
| 1049 | { 0 } |
| 1050 | }; |
| 1051 | |
| 1052 | static PyTypeObject gcmac_pytype_skel = { |
| 1053 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1054 | "GCMAC", /* @tp_name@ */ |
| 1055 | sizeof(gchash_pyobj), /* @tp_basicsize@ */ |
| 1056 | 0, /* @tp_itemsize@ */ |
| 1057 | |
| 1058 | 0, /* @tp_dealloc@ */ |
| 1059 | 0, /* @tp_print@ */ |
| 1060 | 0, /* @tp_getattr@ */ |
| 1061 | 0, /* @tp_setattr@ */ |
| 1062 | 0, /* @tp_compare@ */ |
| 1063 | 0, /* @tp_repr@ */ |
| 1064 | 0, /* @tp_as_number@ */ |
| 1065 | 0, /* @tp_as_sequence@ */ |
| 1066 | 0, /* @tp_as_mapping@ */ |
| 1067 | 0, /* @tp_hash@ */ |
| 1068 | 0, /* @tp_call@ */ |
| 1069 | 0, /* @tp_str@ */ |
| 1070 | 0, /* @tp_getattro@ */ |
| 1071 | 0, /* @tp_setattro@ */ |
| 1072 | 0, /* @tp_as_buffer@ */ |
| 1073 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 1074 | Py_TPFLAGS_BASETYPE, |
| 1075 | |
| 1076 | /* @tp_doc@ */ |
| 1077 | "Message authentication code metametaclass.", |
| 1078 | |
| 1079 | 0, /* @tp_traverse@ */ |
| 1080 | 0, /* @tp_clear@ */ |
| 1081 | 0, /* @tp_richcompare@ */ |
| 1082 | 0, /* @tp_weaklistoffset@ */ |
| 1083 | 0, /* @tp_iter@ */ |
| 1084 | 0, /* @tp_iternext@ */ |
| 1085 | 0, /* @tp_methods@ */ |
| 1086 | 0, /* @tp_members@ */ |
| 1087 | gcmac_pygetset, /* @tp_getset@ */ |
| 1088 | 0, /* @tp_base@ */ |
| 1089 | 0, /* @tp_dict@ */ |
| 1090 | 0, /* @tp_descr_get@ */ |
| 1091 | 0, /* @tp_descr_set@ */ |
| 1092 | 0, /* @tp_dictoffset@ */ |
| 1093 | 0, /* @tp_init@ */ |
| 1094 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 1095 | abstract_pynew, /* @tp_new@ */ |
| 1096 | 0, /* @tp_free@ */ |
| 1097 | 0 /* @tp_is_gc@ */ |
| 1098 | }; |
| 1099 | |
| 1100 | static PyTypeObject gmac_pytype_skel = { |
| 1101 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1102 | "GMAC", /* @tp_name@ */ |
| 1103 | sizeof(gmac_pyobj), /* @tp_basicsize@ */ |
| 1104 | 0, /* @tp_itemsize@ */ |
| 1105 | |
| 1106 | gmac_pydealloc, /* @tp_dealloc@ */ |
| 1107 | 0, /* @tp_print@ */ |
| 1108 | 0, /* @tp_getattr@ */ |
| 1109 | 0, /* @tp_setattr@ */ |
| 1110 | 0, /* @tp_compare@ */ |
| 1111 | 0, /* @tp_repr@ */ |
| 1112 | 0, /* @tp_as_number@ */ |
| 1113 | 0, /* @tp_as_sequence@ */ |
| 1114 | 0, /* @tp_as_mapping@ */ |
| 1115 | 0, /* @tp_hash@ */ |
| 1116 | 0, /* @tp_call@ */ |
| 1117 | 0, /* @tp_str@ */ |
| 1118 | 0, /* @tp_getattro@ */ |
| 1119 | 0, /* @tp_setattro@ */ |
| 1120 | 0, /* @tp_as_buffer@ */ |
| 1121 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 1122 | Py_TPFLAGS_BASETYPE, |
| 1123 | |
| 1124 | /* @tp_doc@ */ |
| 1125 | "Message authentication code metaclass, abstract base class.", |
| 1126 | |
| 1127 | 0, /* @tp_traverse@ */ |
| 1128 | 0, /* @tp_clear@ */ |
| 1129 | 0, /* @tp_richcompare@ */ |
| 1130 | 0, /* @tp_weaklistoffset@ */ |
| 1131 | 0, /* @tp_iter@ */ |
| 1132 | 0, /* @tp_iternext@ */ |
| 1133 | 0, /* @tp_methods@ */ |
| 1134 | 0, /* @tp_members@ */ |
| 1135 | 0, /* @tp_getset@ */ |
| 1136 | 0, /* @tp_base@ */ |
| 1137 | 0, /* @tp_dict@ */ |
| 1138 | 0, /* @tp_descr_get@ */ |
| 1139 | 0, /* @tp_descr_set@ */ |
| 1140 | 0, /* @tp_dictoffset@ */ |
| 1141 | 0, /* @tp_init@ */ |
| 1142 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 1143 | abstract_pynew, /* @tp_new@ */ |
| 1144 | 0, /* @tp_free@ */ |
| 1145 | 0 /* @tp_is_gc@ */ |
| 1146 | }; |
| 1147 | |
| 1148 | static PyTypeObject gmhash_pytype_skel = { |
| 1149 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1150 | "GMACHash", /* @tp_name@ */ |
| 1151 | sizeof(ghash_pyobj), /* @tp_basicsize@ */ |
| 1152 | 0, /* @tp_itemsize@ */ |
| 1153 | |
| 1154 | ghash_pydealloc, /* @tp_dealloc@ */ |
| 1155 | 0, /* @tp_print@ */ |
| 1156 | 0, /* @tp_getattr@ */ |
| 1157 | 0, /* @tp_setattr@ */ |
| 1158 | 0, /* @tp_compare@ */ |
| 1159 | 0, /* @tp_repr@ */ |
| 1160 | 0, /* @tp_as_number@ */ |
| 1161 | 0, /* @tp_as_sequence@ */ |
| 1162 | 0, /* @tp_as_mapping@ */ |
| 1163 | 0, /* @tp_hash@ */ |
| 1164 | 0, /* @tp_call@ */ |
| 1165 | 0, /* @tp_str@ */ |
| 1166 | 0, /* @tp_getattro@ */ |
| 1167 | 0, /* @tp_setattro@ */ |
| 1168 | 0, /* @tp_as_buffer@ */ |
| 1169 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 1170 | Py_TPFLAGS_BASETYPE, |
| 1171 | |
| 1172 | /* @tp_doc@ */ |
| 1173 | "Message authentication code, abstract base class.", |
| 1174 | |
| 1175 | 0, /* @tp_traverse@ */ |
| 1176 | 0, /* @tp_clear@ */ |
| 1177 | 0, /* @tp_richcompare@ */ |
| 1178 | 0, /* @tp_weaklistoffset@ */ |
| 1179 | 0, /* @tp_iter@ */ |
| 1180 | 0, /* @tp_iternext@ */ |
| 1181 | 0, /* @tp_methods@ */ |
| 1182 | 0, /* @tp_members@ */ |
| 1183 | 0, /* @tp_getset@ */ |
| 1184 | 0, /* @tp_base@ */ |
| 1185 | 0, /* @tp_dict@ */ |
| 1186 | 0, /* @tp_descr_get@ */ |
| 1187 | 0, /* @tp_descr_set@ */ |
| 1188 | 0, /* @tp_dictoffset@ */ |
| 1189 | 0, /* @tp_init@ */ |
| 1190 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 1191 | abstract_pynew, /* @tp_new@ */ |
| 1192 | 0, /* @tp_free@ */ |
| 1193 | 0 /* @tp_is_gc@ */ |
| 1194 | }; |
| 1195 | |
| 1196 | /*----- Pseudorandom permutations -----------------------------------------*/ |
| 1197 | |
| 1198 | static PyTypeObject *gcprp_pytype, *gprp_pytype; |
| 1199 | |
| 1200 | typedef struct prpinfo { |
| 1201 | const char *name; |
| 1202 | const octet *keysz; |
| 1203 | size_t ctxsz; |
| 1204 | size_t blksz; |
| 1205 | void (*init)(void *, const void *, size_t); |
| 1206 | void (*eblk)(void *, const void *, void *); |
| 1207 | void (*dblk)(void *, const void *, void *); |
| 1208 | } prpinfo; |
| 1209 | |
| 1210 | #define PRP_DEF(PRE, pre) \ |
| 1211 | static void pre##_prpinit(void *ctx, const void *k, size_t ksz) \ |
| 1212 | { pre##_init(ctx, k, ksz); } \ |
| 1213 | static void pre##_prpeblk(void *ctx, const void *in, void *out) \ |
| 1214 | { \ |
| 1215 | uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \ |
| 1216 | pre##_eblk(ctx, w, w); BLKC_STORE(PRE, out, w); \ |
| 1217 | } \ |
| 1218 | static void pre##_prpdblk(void *ctx, const void *in, void *out) \ |
| 1219 | { \ |
| 1220 | uint32 w[PRE##_BLKSZ/4]; BLKC_LOAD(PRE, w, in); \ |
| 1221 | pre##_dblk(ctx, w, w); BLKC_STORE(PRE, out, w); \ |
| 1222 | } \ |
| 1223 | static const prpinfo pre##_prpinfo = { \ |
| 1224 | #pre, pre##_keysz, sizeof(pre##_ctx), PRE##_BLKSZ, \ |
| 1225 | pre##_prpinit, pre##_prpeblk, pre##_prpdblk \ |
| 1226 | }; |
| 1227 | PRPS(PRP_DEF) |
| 1228 | |
| 1229 | static const struct prpinfo *const gprptab[] = { |
| 1230 | #define PRP_ENTRY(PRE, pre) &pre##_prpinfo, |
| 1231 | PRPS(PRP_ENTRY) |
| 1232 | 0 |
| 1233 | }; |
| 1234 | |
| 1235 | typedef struct gcprp_pyobj { |
| 1236 | PyHeapTypeObject ty; |
| 1237 | const prpinfo *prp; |
| 1238 | } gcprp_pyobj; |
| 1239 | #define GCPRP_PRP(o) (((gcprp_pyobj *)(o))->prp) |
| 1240 | |
| 1241 | typedef struct gprp_pyobj { |
| 1242 | PyObject_HEAD |
| 1243 | const prpinfo *prp; |
| 1244 | } gprp_pyobj; |
| 1245 | #define GPRP_PRP(o) (((gprp_pyobj *)(o))->prp) |
| 1246 | #define GPRP_CTX(o) (((gprp_pyobj *)(o)) + 1) |
| 1247 | |
| 1248 | typedef struct prp { |
| 1249 | const prpinfo *prp; |
| 1250 | void *ctx; |
| 1251 | } prp; |
| 1252 | |
| 1253 | static PyObject *gprp_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) |
| 1254 | { |
| 1255 | char *kwlist[] = { "key", 0 }; |
| 1256 | char *k; |
| 1257 | int sz; |
| 1258 | const prpinfo *prp = GCPRP_PRP(ty); |
| 1259 | PyObject *me; |
| 1260 | |
| 1261 | if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &sz)) |
| 1262 | goto end; |
| 1263 | if (keysz(sz, prp->keysz) != sz) VALERR("bad key length"); |
| 1264 | me = (PyObject *)ty->tp_alloc(ty, 0); |
| 1265 | GPRP_PRP(me) = prp; |
| 1266 | prp->init(GPRP_CTX(me), k, sz); |
| 1267 | Py_INCREF(me); |
| 1268 | return (me); |
| 1269 | end: |
| 1270 | return (0); |
| 1271 | } |
| 1272 | |
| 1273 | static void gprp_pydealloc(PyObject *me) |
| 1274 | { Py_DECREF(me->ob_type); FREEOBJ(me); } |
| 1275 | |
| 1276 | static PyObject *gcprp_pywrap(const prpinfo *prp) |
| 1277 | { |
| 1278 | gcprp_pyobj *g = newtype(gcprp_pytype, 0, prp->name); |
| 1279 | g->prp = prp; |
| 1280 | g->ty.ht_type.tp_basicsize = sizeof(gprp_pyobj) + prp->ctxsz; |
| 1281 | g->ty.ht_type.tp_base = gprp_pytype; |
| 1282 | Py_INCREF(gprp_pytype); |
| 1283 | g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT | |
| 1284 | Py_TPFLAGS_BASETYPE | |
| 1285 | Py_TPFLAGS_HEAPTYPE); |
| 1286 | g->ty.ht_type.tp_alloc = PyType_GenericAlloc; |
| 1287 | g->ty.ht_type.tp_free = 0; |
| 1288 | g->ty.ht_type.tp_new = gprp_pynew; |
| 1289 | typeready(&g->ty.ht_type); |
| 1290 | return ((PyObject *)g); |
| 1291 | } |
| 1292 | |
| 1293 | static PyObject *gcpget_name(PyObject *me, void *hunoz) |
| 1294 | { return (PyString_FromString(GCPRP_PRP(me)->name)); } |
| 1295 | static PyObject *gcpget_keysz(PyObject *me, void *hunoz) |
| 1296 | { return (keysz_pywrap(GCPRP_PRP(me)->keysz)); } |
| 1297 | static PyObject *gcpget_blksz(PyObject *me, void *hunoz) |
| 1298 | { return (PyInt_FromLong(GCPRP_PRP(me)->blksz)); } |
| 1299 | |
| 1300 | static PyObject *gpmeth_encrypt(PyObject *me, PyObject *arg) |
| 1301 | { |
| 1302 | char *p; |
| 1303 | int n; |
| 1304 | PyObject *rc = 0; |
| 1305 | |
| 1306 | if (!PyArg_ParseTuple(arg, "s#:encrypt", &p, &n)) goto end; |
| 1307 | if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length"); |
| 1308 | rc = bytestring_pywrap(0, n); |
| 1309 | GPRP_PRP(me)->eblk(GPRP_CTX(me), p, PyString_AS_STRING(rc)); |
| 1310 | end: |
| 1311 | return (rc); |
| 1312 | } |
| 1313 | |
| 1314 | static PyObject *gpmeth_decrypt(PyObject *me, PyObject *arg) |
| 1315 | { |
| 1316 | char *p; |
| 1317 | int n; |
| 1318 | PyObject *rc = 0; |
| 1319 | |
| 1320 | if (!PyArg_ParseTuple(arg, "s#:decrypt", &p, &n)) goto end; |
| 1321 | if (n != GPRP_PRP(me)->blksz) VALERR("incorrect block length"); |
| 1322 | rc = bytestring_pywrap(0, n); |
| 1323 | GPRP_PRP(me)->dblk(GPRP_CTX(me), p, PyString_AS_STRING(rc)); |
| 1324 | end: |
| 1325 | return (rc); |
| 1326 | } |
| 1327 | |
| 1328 | static PyGetSetDef gcprp_pygetset[] = { |
| 1329 | #define GETSETNAME(op, name) gcp##op##_##name |
| 1330 | GET (keysz, "CP.keysz -> acceptable key sizes") |
| 1331 | GET (blksz, "CP.blksz -> block size") |
| 1332 | GET (name, "CP.name -> name of this kind of PRP") |
| 1333 | #undef GETSETNAME |
| 1334 | { 0 } |
| 1335 | }; |
| 1336 | |
| 1337 | static PyMethodDef gprp_pymethods[] = { |
| 1338 | #define METHNAME(name) gpmeth_##name |
| 1339 | METH (encrypt, "P.encrypt(PT) -> CT") |
| 1340 | METH (decrypt, "P.decrypt(CT) -> PT") |
| 1341 | #undef METHNAME |
| 1342 | { 0 } |
| 1343 | }; |
| 1344 | |
| 1345 | static PyTypeObject gcprp_pytype_skel = { |
| 1346 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1347 | "GCPRP", /* @tp_name@ */ |
| 1348 | sizeof(gcprp_pyobj), /* @tp_basicsize@ */ |
| 1349 | 0, /* @tp_itemsize@ */ |
| 1350 | |
| 1351 | 0, /* @tp_dealloc@ */ |
| 1352 | 0, /* @tp_print@ */ |
| 1353 | 0, /* @tp_getattr@ */ |
| 1354 | 0, /* @tp_setattr@ */ |
| 1355 | 0, /* @tp_compare@ */ |
| 1356 | 0, /* @tp_repr@ */ |
| 1357 | 0, /* @tp_as_number@ */ |
| 1358 | 0, /* @tp_as_sequence@ */ |
| 1359 | 0, /* @tp_as_mapping@ */ |
| 1360 | 0, /* @tp_hash@ */ |
| 1361 | 0, /* @tp_call@ */ |
| 1362 | 0, /* @tp_str@ */ |
| 1363 | 0, /* @tp_getattro@ */ |
| 1364 | 0, /* @tp_setattro@ */ |
| 1365 | 0, /* @tp_as_buffer@ */ |
| 1366 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 1367 | Py_TPFLAGS_BASETYPE, |
| 1368 | |
| 1369 | /* @tp_doc@ */ |
| 1370 | "Pseudorandom permutation metaclass.", |
| 1371 | |
| 1372 | 0, /* @tp_traverse@ */ |
| 1373 | 0, /* @tp_clear@ */ |
| 1374 | 0, /* @tp_richcompare@ */ |
| 1375 | 0, /* @tp_weaklistoffset@ */ |
| 1376 | 0, /* @tp_iter@ */ |
| 1377 | 0, /* @tp_iternext@ */ |
| 1378 | 0, /* @tp_methods@ */ |
| 1379 | 0, /* @tp_members@ */ |
| 1380 | gcprp_pygetset, /* @tp_getset@ */ |
| 1381 | 0, /* @tp_base@ */ |
| 1382 | 0, /* @tp_dict@ */ |
| 1383 | 0, /* @tp_descr_get@ */ |
| 1384 | 0, /* @tp_descr_set@ */ |
| 1385 | 0, /* @tp_dictoffset@ */ |
| 1386 | 0, /* @tp_init@ */ |
| 1387 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 1388 | abstract_pynew, /* @tp_new@ */ |
| 1389 | 0, /* @tp_free@ */ |
| 1390 | 0 /* @tp_is_gc@ */ |
| 1391 | }; |
| 1392 | |
| 1393 | static PyTypeObject gprp_pytype_skel = { |
| 1394 | PyObject_HEAD_INIT(0) 0, /* Header */ |
| 1395 | "GPRP", /* @tp_name@ */ |
| 1396 | sizeof(gprp_pyobj), /* @tp_basicsize@ */ |
| 1397 | 0, /* @tp_itemsize@ */ |
| 1398 | |
| 1399 | gprp_pydealloc, /* @tp_dealloc@ */ |
| 1400 | 0, /* @tp_print@ */ |
| 1401 | 0, /* @tp_getattr@ */ |
| 1402 | 0, /* @tp_setattr@ */ |
| 1403 | 0, /* @tp_compare@ */ |
| 1404 | 0, /* @tp_repr@ */ |
| 1405 | 0, /* @tp_as_number@ */ |
| 1406 | 0, /* @tp_as_sequence@ */ |
| 1407 | 0, /* @tp_as_mapping@ */ |
| 1408 | 0, /* @tp_hash@ */ |
| 1409 | 0, /* @tp_call@ */ |
| 1410 | 0, /* @tp_str@ */ |
| 1411 | 0, /* @tp_getattro@ */ |
| 1412 | 0, /* @tp_setattro@ */ |
| 1413 | 0, /* @tp_as_buffer@ */ |
| 1414 | Py_TPFLAGS_DEFAULT | /* @tp_flags@ */ |
| 1415 | Py_TPFLAGS_BASETYPE, |
| 1416 | |
| 1417 | /* @tp_doc@ */ |
| 1418 | "Pseudorandom permutation, abstract base class.", |
| 1419 | |
| 1420 | 0, /* @tp_traverse@ */ |
| 1421 | 0, /* @tp_clear@ */ |
| 1422 | 0, /* @tp_richcompare@ */ |
| 1423 | 0, /* @tp_weaklistoffset@ */ |
| 1424 | 0, /* @tp_iter@ */ |
| 1425 | 0, /* @tp_iternext@ */ |
| 1426 | gprp_pymethods, /* @tp_methods@ */ |
| 1427 | 0, /* @tp_members@ */ |
| 1428 | 0, /* @tp_getset@ */ |
| 1429 | 0, /* @tp_base@ */ |
| 1430 | 0, /* @tp_dict@ */ |
| 1431 | 0, /* @tp_descr_get@ */ |
| 1432 | 0, /* @tp_descr_set@ */ |
| 1433 | 0, /* @tp_dictoffset@ */ |
| 1434 | 0, /* @tp_init@ */ |
| 1435 | PyType_GenericAlloc, /* @tp_alloc@ */ |
| 1436 | abstract_pynew, /* @tp_new@ */ |
| 1437 | 0, /* @tp_free@ */ |
| 1438 | 0 /* @tp_is_gc@ */ |
| 1439 | }; |
| 1440 | |
| 1441 | /*----- Main code ---------------------------------------------------------*/ |
| 1442 | |
| 1443 | static PyMethodDef methods[] = { |
| 1444 | #define METHNAME(func) meth_##func |
| 1445 | METH (_KeySZ_fromdl, "\ |
| 1446 | fromdl(N) -> M: convert integer discrete log field size to work factor") |
| 1447 | METH (_KeySZ_fromschnorr, "\ |
| 1448 | fromschnorr(N) -> M: convert Schnorr group order to work factor") |
| 1449 | METH (_KeySZ_fromif, "\ |
| 1450 | fromif(N) -> M: convert integer factorization problem size to work factor") |
| 1451 | METH (_KeySZ_fromec, "\ |
| 1452 | fromec(N) -> M: convert elliptic curve group order to work factor") |
| 1453 | METH (_KeySZ_todl, "\ |
| 1454 | todl(N) -> M: convert work factor to integer discrete log field size") |
| 1455 | METH (_KeySZ_toschnorr, "\ |
| 1456 | toschnorr(N) -> M: convert work factor to Schnorr group order") |
| 1457 | METH (_KeySZ_toif, "\ |
| 1458 | toif(N) -> M: convert work factor to integer factorization problem size") |
| 1459 | METH (_KeySZ_toec, "\ |
| 1460 | toec(N) -> M: convert work factor to elliptic curve group order") |
| 1461 | #undef METHNAME |
| 1462 | { 0 } |
| 1463 | }; |
| 1464 | |
| 1465 | void algorithms_pyinit(void) |
| 1466 | { |
| 1467 | INITTYPE(keysz, root); |
| 1468 | INITTYPE(keyszany, keysz); |
| 1469 | INITTYPE(keyszrange, keysz); |
| 1470 | INITTYPE(keyszset, keysz); |
| 1471 | INITTYPE(gccipher, type); |
| 1472 | INITTYPE(gcipher, root); |
| 1473 | INITTYPE(gchash, type); |
| 1474 | INITTYPE(ghash, root); |
| 1475 | INITTYPE(gcmac, type); |
| 1476 | INITTYPE(gmac, type); |
| 1477 | INITTYPE(gmhash, ghash); |
| 1478 | INITTYPE(gcprp, type); |
| 1479 | INITTYPE(gprp, root); |
| 1480 | addmethods(methods); |
| 1481 | } |
| 1482 | |
| 1483 | GEN(gcciphers, cipher) |
| 1484 | GEN(gchashes, hash) |
| 1485 | GEN(gcmacs, mac) |
| 1486 | #define gcprp prpinfo |
| 1487 | GEN(gcprps, prp) |
| 1488 | |
| 1489 | void algorithms_pyinsert(PyObject *mod) |
| 1490 | { |
| 1491 | PyObject *d; |
| 1492 | INSERT("KeySZ", keysz_pytype); |
| 1493 | INSERT("KeySZAny", keyszany_pytype); |
| 1494 | INSERT("KeySZRange", keyszrange_pytype); |
| 1495 | INSERT("KeySZSet", keyszset_pytype); |
| 1496 | INSERT("GCCipher", gccipher_pytype); |
| 1497 | INSERT("GCipher", gcipher_pytype); |
| 1498 | INSERT("gcciphers", gcciphers()); |
| 1499 | INSERT("GCHash", gchash_pytype); |
| 1500 | INSERT("GHash", ghash_pytype); |
| 1501 | INSERT("gchashes", d = gchashes()); |
| 1502 | sha_pyobj = PyDict_GetItemString(d, "sha"); Py_INCREF(sha_pyobj); |
| 1503 | has160_pyobj = PyDict_GetItemString(d, "has160"); Py_INCREF(has160_pyobj); |
| 1504 | INSERT("GCMAC", gcmac_pytype); |
| 1505 | INSERT("GMAC", gmac_pytype); |
| 1506 | INSERT("GMACHash", gmhash_pytype); |
| 1507 | INSERT("gcmacs", gcmacs()); |
| 1508 | INSERT("GCPRP", gcprp_pytype); |
| 1509 | INSERT("GPRP", gprp_pytype); |
| 1510 | INSERT("gcprps", gcprps()); |
| 1511 | } |
| 1512 | |
| 1513 | /*----- That's all, folks -------------------------------------------------*/ |