| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Definitions for Catacomb bindings |
| 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 | #ifndef CATACOMB_PYTHON_H |
| 28 | #define CATACOMB_PYTHON_H |
| 29 | |
| 30 | #ifdef __cplusplus |
| 31 | extern "C" { |
| 32 | #endif |
| 33 | |
| 34 | /*----- Header files ------------------------------------------------------*/ |
| 35 | |
| 36 | #define PY_SSIZE_T_CLEAN |
| 37 | |
| 38 | #include <Python.h> |
| 39 | #include <longintrepr.h> |
| 40 | #include <structmember.h> |
| 41 | |
| 42 | #include <mLib/darray.h> |
| 43 | #include <mLib/dstr.h> |
| 44 | #include <mLib/macros.h> |
| 45 | #include <mLib/quis.h> |
| 46 | #include <mLib/unihash.h> |
| 47 | |
| 48 | #include <catacomb/buf.h> |
| 49 | #include <catacomb/ct.h> |
| 50 | |
| 51 | #include <catacomb/grand.h> |
| 52 | #include <catacomb/rand.h> |
| 53 | #include <catacomb/noise.h> |
| 54 | #include <catacomb/bbs.h> |
| 55 | #include <catacomb/mprand.h> |
| 56 | #include <catacomb/lcrand.h> |
| 57 | #include <catacomb/fibrand.h> |
| 58 | #include <catacomb/dsarand.h> |
| 59 | #include <catacomb/sslprf.h> |
| 60 | #include <catacomb/tlsprf.h> |
| 61 | #include <catacomb/blkc.h> |
| 62 | |
| 63 | #include <catacomb/gcipher.h> |
| 64 | #include <catacomb/gaead.h> |
| 65 | #include <catacomb/ghash.h> |
| 66 | #include <catacomb/gmac.h> |
| 67 | #include <catacomb/md5.h> |
| 68 | #include <catacomb/md5-hmac.h> |
| 69 | #include <catacomb/poly1305.h> |
| 70 | #include <catacomb/sha.h> |
| 71 | #include <catacomb/sha-mgf.h> |
| 72 | #include <catacomb/sha-hmac.h> |
| 73 | #include <catacomb/keccak1600.h> |
| 74 | #include <catacomb/sha3.h> |
| 75 | |
| 76 | #include <catacomb/mp.h> |
| 77 | #include <catacomb/mpint.h> |
| 78 | #include <catacomb/mpmul.h> |
| 79 | #include <catacomb/mpcrt.h> |
| 80 | #include <catacomb/mpmont.h> |
| 81 | #include <catacomb/mpbarrett.h> |
| 82 | #include <catacomb/mpreduce.h> |
| 83 | #include <catacomb/mp-fibonacci.h> |
| 84 | |
| 85 | #include <catacomb/pgen.h> |
| 86 | #include <catacomb/pfilt.h> |
| 87 | #include <catacomb/strongprime.h> |
| 88 | #include <catacomb/limlee.h> |
| 89 | #include <catacomb/dh.h> |
| 90 | #include <catacomb/ptab.h> |
| 91 | #include <catacomb/bintab.h> |
| 92 | #include <catacomb/dsa.h> |
| 93 | #include <catacomb/x25519.h> |
| 94 | #include <catacomb/x448.h> |
| 95 | #include <catacomb/ed25519.h> |
| 96 | #include <catacomb/ed448.h> |
| 97 | |
| 98 | #include <catacomb/gf.h> |
| 99 | #include <catacomb/gfreduce.h> |
| 100 | #include <catacomb/gfn.h> |
| 101 | |
| 102 | #include <catacomb/field.h> |
| 103 | #include <catacomb/field-guts.h> |
| 104 | |
| 105 | #include <catacomb/ec.h> |
| 106 | #include <catacomb/ec-raw.h> |
| 107 | #include <catacomb/ectab.h> |
| 108 | |
| 109 | #include <catacomb/group.h> |
| 110 | #include <catacomb/group-guts.h> |
| 111 | |
| 112 | #include <catacomb/gdsa.h> |
| 113 | #include <catacomb/gkcdsa.h> |
| 114 | #include <catacomb/rsa.h> |
| 115 | |
| 116 | #include <catacomb/key.h> |
| 117 | #include <catacomb/passphrase.h> |
| 118 | #include <catacomb/pixie.h> |
| 119 | |
| 120 | #include <catacomb/share.h> |
| 121 | #include <catacomb/gfshare.h> |
| 122 | |
| 123 | /*----- Other preliminaries -----------------------------------------------*/ |
| 124 | |
| 125 | #define GOBBLE_SEMI extern int notexist |
| 126 | #if defined(__GNUC__) && defined(__ELF__) |
| 127 | # define PRIVATE_SYMBOLS _Pragma("GCC visibility push(hidden)") GOBBLE_SEMI |
| 128 | # define PUBLIC_SYMBOLS _Pragma("GCC visibility pop") GOBBLE_SEMI |
| 129 | # define EXPORT __attribute__((__visibility__("default"))) |
| 130 | #else |
| 131 | # define PRIVATE_SYMBOLS GOBBLE_SEMI |
| 132 | # define PUBLIC_SYMBOLS GOBBLE_SEMI |
| 133 | # define EXPORT |
| 134 | #endif |
| 135 | |
| 136 | PRIVATE_SYMBOLS; |
| 137 | |
| 138 | /*----- Utility macros ----------------------------------------------------*/ |
| 139 | |
| 140 | #define RETURN_OBJ(obj) do { Py_INCREF(obj); return (obj); } while (0) |
| 141 | #define RETURN_NONE RETURN_OBJ(Py_None) |
| 142 | #define RETURN_NOTIMPL RETURN_OBJ(Py_NotImplemented) |
| 143 | #define RETURN_TRUE RETURN_OBJ(Py_True) |
| 144 | #define RETURN_FALSE RETURN_OBJ(Py_False) |
| 145 | #define RETURN_ME RETURN_OBJ(me) |
| 146 | |
| 147 | #define EXCERR(exc, str) do { \ |
| 148 | PyErr_SetString(exc, str); \ |
| 149 | goto end; \ |
| 150 | } while (0) |
| 151 | #define VALERR(str) EXCERR(PyExc_ValueError, str) |
| 152 | #define OVFERR(str) EXCERR(PyExc_OverflowError, str) |
| 153 | #define TYERR(str) EXCERR(PyExc_TypeError, str) |
| 154 | #define IXERR(str) EXCERR(PyExc_IndexError, str) |
| 155 | #define ZDIVERR(str) EXCERR(PyExc_ZeroDivisionError, str) |
| 156 | #define SYSERR(str) EXCERR(PyExc_SystemError, str) |
| 157 | #define NIERR(str) EXCERR(PyExc_NotImplementedError, str) |
| 158 | #define INDEXERR(idx) do { \ |
| 159 | PyErr_SetObject(PyExc_KeyError, idx); \ |
| 160 | goto end; \ |
| 161 | } while (0) |
| 162 | #define OSERR(name) do { \ |
| 163 | PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); \ |
| 164 | goto end; \ |
| 165 | } while (0) |
| 166 | #define PGENERR(exc) do { pgenerr(exc); goto end; } while (0) |
| 167 | |
| 168 | #define CONVFUNC(ty, cty, ext) \ |
| 169 | int conv##ty(PyObject *o, void *p) \ |
| 170 | { \ |
| 171 | if (!PyObject_TypeCheck(o, ty##_pytype)) \ |
| 172 | TYERR("wanted a " #ty); \ |
| 173 | *(cty *)p = ext(o); \ |
| 174 | return (1); \ |
| 175 | end: \ |
| 176 | return (0); \ |
| 177 | } |
| 178 | |
| 179 | #if PY_VERSION_HEX < 0x02050000 /* Compatibility hack */ |
| 180 | # define ht_name name |
| 181 | # define ht_type type |
| 182 | #endif |
| 183 | |
| 184 | #define root_pytype 0 |
| 185 | #define type_pytype &PyType_Type |
| 186 | #define INITTYPE_META(ty, base, meta) do { \ |
| 187 | ty##_pytype_skel.tp_base = base##_pytype; \ |
| 188 | ty##_pytype = inittype(&ty##_pytype_skel, meta##_pytype); \ |
| 189 | } while (0) |
| 190 | #define INITTYPE(ty, base) INITTYPE_META(ty, base, type) |
| 191 | |
| 192 | extern PyObject *home_module; |
| 193 | |
| 194 | #define INSERT(name, ob) do { \ |
| 195 | PyObject *_o = (PyObject *)(ob); \ |
| 196 | Py_INCREF(_o); \ |
| 197 | PyModule_AddObject(mod, name, _o); \ |
| 198 | } while (0) |
| 199 | |
| 200 | #define INSEXC(name, var, base, meth) \ |
| 201 | INSERT(name, var = mkexc(mod, base, name, meth)) |
| 202 | |
| 203 | #define METH(func, doc) \ |
| 204 | { #func, METHNAME(func), METH_VARARGS, doc }, |
| 205 | #define KWMETH(func, doc) \ |
| 206 | { #func, (PyCFunction)METHNAME(func), \ |
| 207 | METH_VARARGS | METH_KEYWORDS, doc }, |
| 208 | |
| 209 | #define GET(func, doc) \ |
| 210 | { #func, GETSETNAME(get, func), 0, doc }, |
| 211 | #define GETSET(func, doc) \ |
| 212 | { #func, GETSETNAME(get, func), GETSETNAME(set, func), doc }, |
| 213 | |
| 214 | #define MEMBER(name, ty, f, doc) \ |
| 215 | { #name, ty, offsetof(MEMBERSTRUCT, name), f, doc }, |
| 216 | |
| 217 | #define MODULES(_) \ |
| 218 | _(util) \ |
| 219 | _(bytestring) _(buffer) \ |
| 220 | _(rand) _(algorithms) _(pubkey) _(pgen) \ |
| 221 | _(mp) _(field) _(ec) _(group) \ |
| 222 | _(passphrase) _(share) _(key) |
| 223 | #define DOMODINIT(m) m##_pyinit(); |
| 224 | #define DOMODINSERT(m) m##_pyinsert(mod); |
| 225 | #define INIT_MODULES do { MODULES(DOMODINIT) } while (0) |
| 226 | #define INSERT_MODULES do { MODULES(DOMODINSERT) } while (0) |
| 227 | |
| 228 | #define DO(m) \ |
| 229 | extern void m##_pyinit(void); \ |
| 230 | extern void m##_pyinsert(PyObject *); |
| 231 | MODULES(DO) |
| 232 | #undef DO |
| 233 | |
| 234 | #define FREEOBJ(obj) \ |
| 235 | (((PyObject *)(obj))->ob_type->tp_free((PyObject *)(obj))) |
| 236 | |
| 237 | #define GEN(func, base) \ |
| 238 | static PyObject *func(void) \ |
| 239 | { \ |
| 240 | PyObject *d = PyDict_New(); \ |
| 241 | PyObject *o; \ |
| 242 | int i; \ |
| 243 | \ |
| 244 | for (i = 0; g##base##tab[i]; i++) { \ |
| 245 | o = gc##base##_pywrap((/*unconst*/ gc##base *)g##base##tab[i]); \ |
| 246 | PyDict_SetItemString(d, \ |
| 247 | (/*unconst*/ char *)g##base##tab[i]->name, \ |
| 248 | o); \ |
| 249 | Py_DECREF(o); \ |
| 250 | } \ |
| 251 | return (d); \ |
| 252 | } |
| 253 | |
| 254 | #define KWLIST (/*unconst*/ char **)kwlist |
| 255 | |
| 256 | struct nameval { const char *name; unsigned f; unsigned long value; }; |
| 257 | #define CF_SIGNED 1u |
| 258 | extern void setconstants(PyObject *, const struct nameval *); |
| 259 | |
| 260 | extern PyObject *mexp_common(PyObject *, PyObject *, size_t, |
| 261 | PyObject *(*id)(PyObject *), |
| 262 | int (*fill)(void *, PyObject *, |
| 263 | PyObject *, PyObject *), |
| 264 | PyObject *(*exp)(PyObject *, void *, int), |
| 265 | void (*drop)(void *)); |
| 266 | |
| 267 | extern int convulong(PyObject *, void *); |
| 268 | #define DECL_CONVU_(n) extern int convu##n(PyObject *, void *); |
| 269 | DOUINTSZ(DECL_CONVU_) |
| 270 | extern int convmpw(PyObject *, void *); |
| 271 | extern int convuint(PyObject *, void *); |
| 272 | extern int convk64(PyObject *, void *); |
| 273 | extern int convszt(PyObject *, void *); |
| 274 | extern int convbool(PyObject *, void *); |
| 275 | extern PyObject *abstract_pynew(PyTypeObject *, PyObject *, PyObject *); |
| 276 | extern PyObject *getbool(int); |
| 277 | extern PyObject *getulong(unsigned long); |
| 278 | extern PyObject *getk64(kludge64); |
| 279 | extern void *newtype(PyTypeObject *, const PyTypeObject *, const char *); |
| 280 | |
| 281 | struct excinfo { PyObject *ty, *val, *tb; }; |
| 282 | #define EXCINFO_INIT { 0, 0, 0 } |
| 283 | |
| 284 | extern PyObject *mkexc(PyObject *, PyObject *, const char *, PyMethodDef *); |
| 285 | #define INIT_EXCINFO(exc) do { \ |
| 286 | struct excinfo *_exc = (exc); _exc->ty = _exc->val = _exc->tb = 0; \ |
| 287 | } while (0) |
| 288 | #define RELEASE_EXCINFO(exc) do { \ |
| 289 | struct excinfo *_exc = (exc); \ |
| 290 | Py_XDECREF(_exc->ty); _exc->ty = 0; \ |
| 291 | Py_XDECREF(_exc->val); _exc->val = 0; \ |
| 292 | Py_XDECREF(_exc->tb); _exc->tb = 0; \ |
| 293 | } while (0) |
| 294 | #define STASH_EXCINFO(exc) do { \ |
| 295 | struct excinfo *_exc = (exc); \ |
| 296 | PyErr_Fetch(&_exc->ty, &_exc->val, &_exc->tb); \ |
| 297 | PyErr_NormalizeException(&_exc->ty, &_exc->val, &_exc->tb); \ |
| 298 | } while (0) |
| 299 | #define RESTORE_EXCINFO(exc) do { \ |
| 300 | struct excinfo *_exc = (exc); \ |
| 301 | PyErr_Restore(_exc->ty, _exc->val, _exc->tb); \ |
| 302 | _exc->ty = _exc->val = _exc->tb = 0; \ |
| 303 | } while (0) |
| 304 | extern void report_lost_exception(struct excinfo *, const char *, ...); |
| 305 | extern void report_lost_exception_v(struct excinfo *, const char *, va_list); |
| 306 | extern void stash_exception(struct excinfo *, const char *, ...); |
| 307 | extern void restore_exception(struct excinfo *, const char *, ...); |
| 308 | |
| 309 | extern void typeready(PyTypeObject *); |
| 310 | extern PyTypeObject *inittype(PyTypeObject *, PyTypeObject *); |
| 311 | extern void addmethods(const PyMethodDef *); |
| 312 | extern PyMethodDef *donemethods(void); |
| 313 | |
| 314 | /*----- Mapping methods ---------------------------------------------------*/ |
| 315 | |
| 316 | #define GMAP_METH(func, doc) { #func, gmapmeth_##func, METH_VARARGS, doc }, |
| 317 | #define GMAP_KWMETH(func, doc) \ |
| 318 | { #func, (PyCFunction)gmapmeth_##func, METH_VARARGS|METH_KEYWORDS, doc }, |
| 319 | #define GMAP_METHDECL(func, doc) \ |
| 320 | extern PyObject *gmapmeth_##func(PyObject *, PyObject *); |
| 321 | #define GMAP_KWMETHDECL(func, doc) \ |
| 322 | extern PyObject *gmapmeth_##func(PyObject *, PyObject *, PyObject *); |
| 323 | |
| 324 | #define GMAP_DOROMETHODS(METH, KWMETH) \ |
| 325 | METH (has_key, "D.has_key(KEY) -> BOOL") \ |
| 326 | METH (keys, "D.keys() -> LIST") \ |
| 327 | METH (values, "D.values() -> LIST") \ |
| 328 | METH (items, "D.items() -> LIST") \ |
| 329 | METH (iterkeys, "D.iterkeys() -> ITER") \ |
| 330 | METH (itervalues, "D.itervalues() -> ITER") \ |
| 331 | METH (iteritems, "D.iteritems() -> ITER") \ |
| 332 | KWMETH(get, "D.get(KEY, [default = None]) -> VALUE") \ |
| 333 | |
| 334 | #define GMAP_DOMETHODS(METH, KWMETH) \ |
| 335 | GMAP_DOROMETHODS(METH, KWMETH) \ |
| 336 | METH (clear, "D.clear()") \ |
| 337 | KWMETH(setdefault, "D.setdefault(K, [default = None]) -> VALUE") \ |
| 338 | KWMETH(pop, "D.pop(KEY, [default = <error>]) -> VALUE") \ |
| 339 | METH (popitem, "D.popitem() -> (KEY, VALUE)") \ |
| 340 | METH (update, "D.update(MAP)") |
| 341 | |
| 342 | GMAP_DOMETHODS(GMAP_METHDECL, GMAP_KWMETHDECL) |
| 343 | #define GMAP_ROMETHODS GMAP_DOROMETHODS(GMAP_METH, GMAP_KWMETH) |
| 344 | #define GMAP_METHODS GMAP_DOMETHODS(GMAP_METH, GMAP_KWMETH) |
| 345 | extern Py_ssize_t gmap_pysize(PyObject *); |
| 346 | extern PySequenceMethods gmap_pysequence; |
| 347 | extern PyMethodDef gmap_pymethods[]; |
| 348 | |
| 349 | /*----- Bytestrings -------------------------------------------------------*/ |
| 350 | |
| 351 | PyTypeObject *bytestring_pyobj; |
| 352 | PyObject *bytestring_pywrap(const void *, size_t); |
| 353 | PyObject *bytestring_pywrapbuf(buf *); |
| 354 | |
| 355 | /*----- Multiprecision arithmetic -----------------------------------------*/ |
| 356 | |
| 357 | typedef struct mp_pyobj { |
| 358 | PyObject_HEAD |
| 359 | mp *x; |
| 360 | } mp_pyobj; |
| 361 | |
| 362 | extern PyTypeObject *mp_pytype; |
| 363 | extern PyTypeObject *gf_pytype; |
| 364 | #define MP_X(o) (((mp_pyobj *)(o))->x) |
| 365 | #define MP_PYCHECK(o) PyObject_TypeCheck((o), mp_pytype) |
| 366 | #define GF_PYCHECK(o) PyObject_TypeCheck((o), gf_pytype) |
| 367 | |
| 368 | extern mp *mp_frompylong(PyObject *); |
| 369 | extern PyObject *mp_topylong(mp *); |
| 370 | extern mp *tomp(PyObject *); |
| 371 | extern mp *getmp(PyObject *); |
| 372 | extern int convmp(PyObject *, void *); |
| 373 | extern mp *getgf(PyObject *); |
| 374 | extern int convgf(PyObject *, void *); |
| 375 | extern PyObject *mp_pywrap(mp *); |
| 376 | extern PyObject *gf_pywrap(mp *); |
| 377 | extern long mphash(mp *); |
| 378 | extern mp *mp_frompyobject(PyObject *, int); |
| 379 | extern PyObject *mp_topystring(mp *, int, |
| 380 | const char *, const char *, const char *); |
| 381 | extern int mp_tolong_checked(mp *, long *, int); |
| 382 | |
| 383 | /*----- Abstract fields ---------------------------------------------------*/ |
| 384 | |
| 385 | typedef struct field_pyobj { |
| 386 | PyHeapTypeObject ty; |
| 387 | field *f; |
| 388 | } field_pyobj; |
| 389 | |
| 390 | extern PyTypeObject *fe_pytype; |
| 391 | #define FE_PYCHECK(o) PyObject_TypeCheck((o), fe_pytype) |
| 392 | #define FE_F(o) (((fe_pyobj *)(o))->f) |
| 393 | #define FE_FOBJ(o) ((PyObject *)(o)->ob_type) |
| 394 | #define FE_X(o) (((fe_pyobj *)(o))->x) |
| 395 | extern PyObject *fe_pywrap(PyObject *, mp *); |
| 396 | |
| 397 | typedef struct fe_pyobj { |
| 398 | PyObject_HEAD |
| 399 | field *f; |
| 400 | mp *x; |
| 401 | } fe_pyobj; |
| 402 | |
| 403 | extern PyTypeObject *field_pytype; |
| 404 | extern PyTypeObject *primefield_pytype; |
| 405 | extern PyTypeObject *niceprimefield_pytype; |
| 406 | extern PyTypeObject *binfield_pytype; |
| 407 | extern PyTypeObject *binpolyfield_pytype; |
| 408 | extern PyTypeObject *binnormfield_pytype; |
| 409 | #define FIELD_PYCHECK(o) PyObject_TypeCheck((o), field_pytype) |
| 410 | #define FIELD_F(o) (((field_pyobj *)(o))->f) |
| 411 | extern PyObject *field_pywrap(field *); |
| 412 | extern field *field_copy(field *); |
| 413 | |
| 414 | /*----- Elliptic curves ---------------------------------------------------*/ |
| 415 | |
| 416 | typedef struct ecpt_pyobj { |
| 417 | PyObject_HEAD |
| 418 | ec_curve *c; |
| 419 | ec p; |
| 420 | } ecpt_pyobj; |
| 421 | |
| 422 | extern PyTypeObject *ecpt_pytype, *ecptcurve_pytype; |
| 423 | #define ECPT_PYCHECK(o) PyObject_TypeCheck((o), ecpt_pytype) |
| 424 | #define ECPTCURVE_PYCHECK(o) PyObject_TypeCheck((o), ecptcurve_pytype) |
| 425 | #define ECPT_C(o) (((ecpt_pyobj *)(o))->c) |
| 426 | #define ECPT_COBJ(o) ((PyObject *)(o)->ob_type) |
| 427 | #define ECPT_FOBJ(o) ECCURVE_FOBJ(ECPT_COBJ((o))) |
| 428 | #define ECPT_P(o) (&((ecpt_pyobj *)(o))->p) |
| 429 | extern PyObject *ecpt_pywrap(PyObject *, ec *); |
| 430 | extern PyObject *ecpt_pywrapout(void *, ec *); |
| 431 | extern int toecpt(ec_curve *, ec *, PyObject *); |
| 432 | extern int getecpt(ec_curve *, ec *, PyObject *); |
| 433 | extern void getecptout(ec *, PyObject *); |
| 434 | extern int convecpt(PyObject *, void *); |
| 435 | |
| 436 | typedef struct eccurve_pyobj { |
| 437 | PyHeapTypeObject ty; |
| 438 | ec_curve *c; |
| 439 | PyObject *fobj; |
| 440 | } eccurve_pyobj; |
| 441 | |
| 442 | extern PyTypeObject *eccurve_pytype; |
| 443 | extern PyTypeObject *ecprimecurve_pytype; |
| 444 | extern PyTypeObject *ecprimeprojcurve_pytype; |
| 445 | extern PyTypeObject *ecbincurve_pytype; |
| 446 | extern PyTypeObject *ecbinprojcurve_pytype; |
| 447 | #define ECCURVE_PYCHECK(o) PyObject_TypeCheck((o), eccurve_pytype) |
| 448 | #define ECCURVE_C(o) (((eccurve_pyobj *)(o))->c) |
| 449 | #define ECCURVE_FOBJ(o) (((eccurve_pyobj *)(o))->fobj) |
| 450 | extern PyObject *eccurve_pywrap(PyObject *, ec_curve *); |
| 451 | extern ec_curve *eccurve_copy(ec_curve *); |
| 452 | |
| 453 | typedef struct ecinfo_pyobj { |
| 454 | PyObject_HEAD |
| 455 | ec_info ei; |
| 456 | PyObject *cobj; |
| 457 | } ecinfo_pyobj; |
| 458 | |
| 459 | extern PyTypeObject *ecinfo_pytype; |
| 460 | #define ECINFO_PYCHECK(o) PyObject_TypeCheck((o), ecinfo_pytype) |
| 461 | #define ECINFO_EI(o) (&((ecinfo_pyobj *)(o))->ei) |
| 462 | #define ECINFO_COBJ(o) (((ecinfo_pyobj *)(o))->cobj) |
| 463 | extern void ecinfo_copy(ec_info *, const ec_info *); |
| 464 | extern PyObject *ecinfo_pywrap(ec_info *); |
| 465 | |
| 466 | /*----- Cyclic groups -----------------------------------------------------*/ |
| 467 | |
| 468 | typedef struct fginfo_pyobj { |
| 469 | PyObject_HEAD |
| 470 | gprime_param dp; |
| 471 | } fginfo_pyobj; |
| 472 | |
| 473 | PyTypeObject *fginfo_pytype, *dhinfo_pytype, *bindhinfo_pytype; |
| 474 | #define FGINFO_DP(fg) (&((fginfo_pyobj *)(fg))->dp) |
| 475 | PyObject *fginfo_pywrap(gprime_param *, PyTypeObject *); |
| 476 | |
| 477 | typedef struct ge_pyobj { |
| 478 | PyObject_HEAD |
| 479 | ge *x; |
| 480 | group *g; |
| 481 | } ge_pyobj; |
| 482 | |
| 483 | extern PyTypeObject *ge_pytype; |
| 484 | #define GE_PYCHECK(o) PyObject_TypeCheck((o), ge_pytype) |
| 485 | #define GE_X(o) (((ge_pyobj *)(o))->x) |
| 486 | #define GE_G(o) (((ge_pyobj *)(o))->g) |
| 487 | #define GE_GOBJ(o) ((PyObject *)(group_pyobj *)(o)->ob_type) |
| 488 | extern PyObject *ge_pywrap(PyObject *, ge *); |
| 489 | |
| 490 | typedef struct group_pyobj { |
| 491 | PyHeapTypeObject ty; |
| 492 | group *g; |
| 493 | } group_pyobj; |
| 494 | |
| 495 | extern PyTypeObject *group_pytype; |
| 496 | extern PyTypeObject *primegroup_pytype, *bingroup_pytype, *ecgroup_pytype; |
| 497 | #define GROUP_G(o) (((group_pyobj *)(o))->g) |
| 498 | extern PyObject *group_pywrap(group *); |
| 499 | extern group *group_copy(group *); |
| 500 | |
| 501 | /*----- Random number generators ------------------------------------------*/ |
| 502 | |
| 503 | #define f_freeme 1u |
| 504 | |
| 505 | typedef struct grand_pyobj { |
| 506 | PyObject_HEAD |
| 507 | unsigned f; |
| 508 | grand *r; |
| 509 | } grand_pyobj; |
| 510 | |
| 511 | extern PyTypeObject *grand_pytype, *truerand_pytype; |
| 512 | extern PyTypeObject *lcrand_pytype,* fibrand_pytype; |
| 513 | extern PyTypeObject *dsarand_pytype, *bbs_pytype; |
| 514 | extern PyObject *rand_pyobj; |
| 515 | #define GRAND_PYCHECK(o) PyObject_TypeCheck((o), grand_pytype) |
| 516 | #define GRAND_F(o) (((grand_pyobj *)(o))->f) |
| 517 | #define GRAND_R(o) (((grand_pyobj *)(o))->r) |
| 518 | extern PyObject *grand_pywrap(grand *, unsigned); |
| 519 | extern int convgrand(PyObject *, void *); |
| 520 | |
| 521 | /*----- Key sizes ---------------------------------------------------------*/ |
| 522 | |
| 523 | typedef struct keysz_pyobj { |
| 524 | PyObject_HEAD |
| 525 | int dfl; |
| 526 | } keysz_pyobj; |
| 527 | |
| 528 | typedef struct keyszrange_pyobj { |
| 529 | PyObject_HEAD |
| 530 | int dfl; |
| 531 | int min, max, mod; |
| 532 | } keyszrange_pyobj; |
| 533 | |
| 534 | typedef struct keyszset_pyobj { |
| 535 | PyObject_HEAD |
| 536 | int dfl; |
| 537 | PyObject *set; |
| 538 | } keyszset_pyobj; |
| 539 | |
| 540 | #define KEYSZ_PYCHECK(o) PyObject_TypeCheck((o), keysz_pytype) |
| 541 | extern PyObject *keysz_pywrap(const octet *); |
| 542 | |
| 543 | /*----- Symmetric cryptography --------------------------------------------*/ |
| 544 | |
| 545 | typedef struct gccipher_pyobj { |
| 546 | PyHeapTypeObject ty; |
| 547 | gccipher *cc; |
| 548 | } gccipher_pyobj; |
| 549 | |
| 550 | extern PyTypeObject *gccipher_pytype; |
| 551 | #define GCCIPHER_PYCHECK(o) PyObject_TypeCheck((o), gccipher_pytype) |
| 552 | #define GCCIPHER_CC(o) (((gccipher_pyobj *)(o))->cc) |
| 553 | extern PyObject *gccipher_pywrap(gccipher *); |
| 554 | extern int convgccipher(PyObject *, void *); |
| 555 | |
| 556 | typedef struct gcipher_pyobj { |
| 557 | PyObject_HEAD |
| 558 | gcipher *c; |
| 559 | } gcipher_pyobj; |
| 560 | |
| 561 | extern PyTypeObject *gcipher_pytype; |
| 562 | #define GCIPHER_PYCHECK(o) PyObject_TypeCheck((o), gcipher_pytype) |
| 563 | #define GCIPHER_C(o) (((gcipher_pyobj *)(o))->c) |
| 564 | extern PyObject *gcipher_pywrap(PyObject *, gcipher *); |
| 565 | extern int convgcipher(PyObject *, void *); |
| 566 | |
| 567 | typedef struct gcaead_pyobj { |
| 568 | PyHeapTypeObject ty; |
| 569 | gcaead *aec; |
| 570 | struct gcaeadaad_pyobj *aad; |
| 571 | struct gcaeadenc_pyobj *enc; |
| 572 | struct gcaeaddec_pyobj *dec; |
| 573 | } gcaead_pyobj; |
| 574 | |
| 575 | extern PyTypeObject *gcaead_pytype; |
| 576 | #define GCAEAD_PYCHECK(o) PyObject_TypeCheck((o), gcaead_pytype) |
| 577 | #define GCAEAD_AEC(o) (((gcaead_pyobj *)(o))->aec) |
| 578 | #define GCAEAD_AAD(o) (((gcaead_pyobj *)(o))->aad) |
| 579 | #define GCAEAD_ENC(o) (((gcaead_pyobj *)(o))->enc) |
| 580 | #define GCAEAD_DEC(o) (((gcaead_pyobj *)(o))->dec) |
| 581 | extern PyObject *gcaead_pywrap(gcaead *); |
| 582 | extern int convgcaead(PyObject *, void *); |
| 583 | |
| 584 | typedef struct gaeadkey_pyobj { |
| 585 | PyObject_HEAD |
| 586 | gaead_key *k; |
| 587 | } gaeadkey_pyobj; |
| 588 | |
| 589 | extern PyTypeObject *gaeadkey_pytype; |
| 590 | #define GAEADKEY_PYCHECK(o) PyObject_TypeCheck((o), gaeadkey_pytype) |
| 591 | #define GAEADKEY_K(o) (((gaeadkey_pyobj *)(o))->k) |
| 592 | extern PyObject *gaeadkey_pywrap(PyObject *, gaead_key *); |
| 593 | extern int convgaeadkey(PyObject *, void *); |
| 594 | |
| 595 | typedef struct gcaeadaad_pyobj { |
| 596 | PyHeapTypeObject ty; |
| 597 | gcaead_pyobj *key; |
| 598 | } gcaeadaad_pyobj; |
| 599 | #define GCAEADAAD_KEY(o) (((gcaeadaad_pyobj *)(o))->key) |
| 600 | extern PyTypeObject *gcaeadaad_pytype; |
| 601 | |
| 602 | typedef struct gaeadaad_pyobj { |
| 603 | PyObject_HEAD |
| 604 | gaead_aad *a; |
| 605 | unsigned f; |
| 606 | #define AEADF_DEAD 32768u |
| 607 | size_t hsz, hlen; |
| 608 | } gaeadaad_pyobj; |
| 609 | |
| 610 | extern PyTypeObject *gaeadaad_pytype; |
| 611 | #define GAEADAAD_PYCHECK(o) PyObject_TypeCheck((o), gaeadaad_pytype) |
| 612 | #define GAEADAAD_A(o) (((gaeadaad_pyobj *)(o))->a) |
| 613 | #define GAEADAAD_F(o) (((gaeadaad_pyobj *)(o))->f) |
| 614 | #define GAEADAAD_HSZ(o) (((gaeadaad_pyobj *)(o))->hsz) |
| 615 | #define GAEADAAD_HLEN(o) (((gaeadaad_pyobj *)(o))->hlen) |
| 616 | extern PyObject *gaeadaad_pywrap(PyObject *, gaead_aad *, unsigned, size_t); |
| 617 | extern int convgaeadaad(PyObject *, void *); |
| 618 | |
| 619 | typedef struct gcaeadenc_pyobj { |
| 620 | PyHeapTypeObject ty; |
| 621 | gcaead_pyobj *key; |
| 622 | } gcaeadenc_pyobj; |
| 623 | #define GCAEADENC_KEY(o) (((gcaeadenc_pyobj *)(o))->key) |
| 624 | extern PyTypeObject *gcaeadenc_pytype; |
| 625 | |
| 626 | typedef struct gaeadenc_pyobj { |
| 627 | PyObject_HEAD |
| 628 | gaead_enc *e; |
| 629 | gaeadaad_pyobj *aad; |
| 630 | unsigned f; |
| 631 | size_t hsz, msz, tsz; |
| 632 | size_t mlen; |
| 633 | } gaeadenc_pyobj; |
| 634 | |
| 635 | extern PyTypeObject *gaeadenc_pytype; |
| 636 | #define GAEADENC_PYCHECK(o) PyObject_TypeCheck((o), gaeadenc_pytype) |
| 637 | #define GAEADENC_AAD(o) (((gaeadenc_pyobj *)(o))->aad) |
| 638 | #define GAEADENC_E(o) (((gaeadenc_pyobj *)(o))->e) |
| 639 | #define GAEADENC_F(o) (((gaeadenc_pyobj *)(o))->f) |
| 640 | #define GAEADENC_HSZ(o) (((gaeadenc_pyobj *)(o))->hsz) |
| 641 | #define GAEADENC_MSZ(o) (((gaeadenc_pyobj *)(o))->msz) |
| 642 | #define GAEADENC_TSZ(o) (((gaeadenc_pyobj *)(o))->tsz) |
| 643 | #define GAEADENC_MLEN(o) (((gaeadenc_pyobj *)(o))->mlen) |
| 644 | extern PyObject *gaeadenc_pywrap(PyObject *, gaead_enc *, unsigned, |
| 645 | size_t, size_t, size_t); |
| 646 | extern int convgaeadenc(PyObject *, void *); |
| 647 | |
| 648 | typedef struct gcaeaddec_pyobj { |
| 649 | PyHeapTypeObject ty; |
| 650 | gcaead_pyobj *key; |
| 651 | } gcaeaddec_pyobj; |
| 652 | #define GCAEADDEC_KEY(o) (((gcaeaddec_pyobj *)(o))->key) |
| 653 | extern PyTypeObject *gcaeaddec_pytype; |
| 654 | |
| 655 | typedef struct gaeaddec_pyobj { |
| 656 | PyObject_HEAD |
| 657 | gaead_dec *d; |
| 658 | gaeadaad_pyobj *aad; |
| 659 | unsigned f; |
| 660 | size_t hsz, csz, tsz; |
| 661 | size_t clen; |
| 662 | } gaeaddec_pyobj; |
| 663 | |
| 664 | extern PyTypeObject *gaeaddec_pytype; |
| 665 | #define GAEADDEC_PYCHECK(o) PyObject_TypeCheck((o), gaeaddec_pytype) |
| 666 | #define GAEADDEC_AAD(o) (((gaeaddec_pyobj *)(o))->aad) |
| 667 | #define GAEADDEC_D(o) (((gaeaddec_pyobj *)(o))->d) |
| 668 | #define GAEADDEC_F(o) (((gaeaddec_pyobj *)(o))->f) |
| 669 | #define GAEADDEC_HSZ(o) (((gaeaddec_pyobj *)(o))->hsz) |
| 670 | #define GAEADDEC_CSZ(o) (((gaeaddec_pyobj *)(o))->csz) |
| 671 | #define GAEADDEC_TSZ(o) (((gaeaddec_pyobj *)(o))->tsz) |
| 672 | #define GAEADDEC_CLEN(o) (((gaeaddec_pyobj *)(o))->clen) |
| 673 | extern PyObject *gaeaddec_pywrap(PyObject *, gaead_dec *, unsigned, |
| 674 | size_t, size_t, size_t); |
| 675 | extern int convgaeaddec(PyObject *, void *); |
| 676 | |
| 677 | typedef struct gchash_pyobj { |
| 678 | PyHeapTypeObject ty; |
| 679 | gchash *ch; |
| 680 | } gchash_pyobj; |
| 681 | |
| 682 | extern PyTypeObject *gchash_pytype; |
| 683 | #define GCHASH_PYCHECK(o) PyObject_TypeCheck((o), gchash_pytype) |
| 684 | #define GCHASH_CH(o) (((gchash_pyobj *)(o))->ch) |
| 685 | extern PyObject *gchash_pywrap(gchash *); |
| 686 | extern int convgchash(PyObject *, void *); |
| 687 | |
| 688 | typedef struct ghash_pyobj { |
| 689 | PyObject_HEAD |
| 690 | ghash *h; |
| 691 | } ghash_pyobj; |
| 692 | |
| 693 | extern PyTypeObject *ghash_pytype, *gmhash_pytype; |
| 694 | extern PyObject *sha_pyobj, *has160_pyobj; |
| 695 | #define GHASH_PYCHECK(o) PyObject_TypeCheck((o), ghash_pytype) |
| 696 | #define GHASH_H(o) (((ghash_pyobj *)(o))->h) |
| 697 | extern PyObject *ghash_pywrap(PyObject *, ghash *); |
| 698 | extern int convghash(PyObject *, void *); |
| 699 | extern int convgmhash(PyObject *, void *); |
| 700 | |
| 701 | typedef struct gcmac_pyobj { |
| 702 | PyHeapTypeObject ty; |
| 703 | gcmac *cm; |
| 704 | } gcmac_pyobj; |
| 705 | |
| 706 | extern PyTypeObject *gcmac_pytype; |
| 707 | #define GCMAC_PYCHECK(o) PyObject_TypeCheck((o), gcmac_pytype) |
| 708 | #define GCMAC_CM(o) (((gcmac_pyobj *)(o))->cm) |
| 709 | #define GCMAC_F(o) (((gcmac_pyobj *)(o))->f) |
| 710 | extern PyObject *gcmac_pywrap(gcmac *); |
| 711 | extern int convgcmac(PyObject *, void *); |
| 712 | |
| 713 | typedef struct gmac_pyobj { |
| 714 | PyHeapTypeObject ty; |
| 715 | gmac *m; |
| 716 | } gmac_pyobj; |
| 717 | |
| 718 | extern PyTypeObject *gmac_pytype; |
| 719 | #define GMAC_PYCHECK(o) PyObject_TypeCheck((o), gmac_pytype) |
| 720 | #define GMAC_M(o) (((gmac_pyobj *)(o))->m) |
| 721 | #define GMAC_F(o) (((gmac_pyobj *)(o))->f) |
| 722 | extern PyObject *gmac_pywrap(PyObject *, gmac *); |
| 723 | extern int convgmac(PyObject *, void *); |
| 724 | |
| 725 | /*----- Key generation ----------------------------------------------------*/ |
| 726 | |
| 727 | typedef struct pfilt_pyobj { |
| 728 | PyObject_HEAD |
| 729 | pfilt f; |
| 730 | int st; |
| 731 | } pfilt_pyobj; |
| 732 | |
| 733 | extern PyTypeObject *pfilt_pytype; |
| 734 | #define PFILT_PYCHECK(o) PyObject_TypeCheck(o, pfilt_pytype) |
| 735 | #define PFILT_F(o) (&((pfilt_pyobj *)(o))->f) |
| 736 | #define PFILT_ST(o) (((pfilt_pyobj *)(o))->st) |
| 737 | |
| 738 | typedef struct { pgen_proc *proc; void *ctx; } pgev; |
| 739 | #define PGEV_HEAD PyObject_HEAD pgev pg; |
| 740 | |
| 741 | typedef struct pgev_pyobj { |
| 742 | PGEV_HEAD |
| 743 | } pgev_pyobj; |
| 744 | |
| 745 | extern PyTypeObject *pgev_pytype; |
| 746 | #define PGEV_PYCHECK(o) PyObject_TypeCheck(o, pgev_pytype) |
| 747 | #define PGEV_PG(o) (&((pgev_pyobj *)(o))->pg) |
| 748 | |
| 749 | typedef struct pypgev { |
| 750 | pgev ev; |
| 751 | PyObject *obj; |
| 752 | struct excinfo *exc; |
| 753 | } pypgev; |
| 754 | |
| 755 | extern int convpgev(PyObject *, void *); |
| 756 | extern void droppgev(pypgev *); |
| 757 | extern void pgenerr(struct excinfo *exc); |
| 758 | |
| 759 | /*----- That's all, folks -------------------------------------------------*/ |
| 760 | |
| 761 | #ifdef __cplusplus |
| 762 | } |
| 763 | #endif |
| 764 | |
| 765 | #endif |