3 * Definitions for Catacomb bindings
5 * (c) 2004 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Python interface to Catacomb.
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.
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.
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.
27 #ifndef CATACOMB_PYTHON_H
28 #define CATACOMB_PYTHON_H
34 /*----- Header files ------------------------------------------------------*/
36 #define PY_SSIZE_T_CLEAN
39 #include <longintrepr.h>
40 #include <structmember.h>
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>
48 #include <catacomb/buf.h>
49 #include <catacomb/ct.h>
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>
63 #include <catacomb/gcipher.h>
64 #include <catacomb/ghash.h>
65 #include <catacomb/gmac.h>
66 #include <catacomb/md5.h>
67 #include <catacomb/md5-hmac.h>
68 #include <catacomb/poly1305.h>
69 #include <catacomb/sha.h>
70 #include <catacomb/sha-mgf.h>
71 #include <catacomb/sha-hmac.h>
72 #include <catacomb/keccak1600.h>
73 #include <catacomb/sha3.h>
75 #include <catacomb/mp.h>
76 #include <catacomb/mpint.h>
77 #include <catacomb/mpmul.h>
78 #include <catacomb/mpcrt.h>
79 #include <catacomb/mpmont.h>
80 #include <catacomb/mpbarrett.h>
81 #include <catacomb/mpreduce.h>
82 #include <catacomb/mp-fibonacci.h>
84 #include <catacomb/pgen.h>
85 #include <catacomb/pfilt.h>
86 #include <catacomb/strongprime.h>
87 #include <catacomb/limlee.h>
88 #include <catacomb/dh.h>
89 #include <catacomb/ptab.h>
90 #include <catacomb/bintab.h>
91 #include <catacomb/dsa.h>
92 #include <catacomb/x25519.h>
93 #include <catacomb/x448.h>
94 #include <catacomb/ed25519.h>
95 #include <catacomb/ed448.h>
97 #include <catacomb/gf.h>
98 #include <catacomb/gfreduce.h>
99 #include <catacomb/gfn.h>
101 #include <catacomb/field.h>
102 #include <catacomb/field-guts.h>
104 #include <catacomb/ec.h>
105 #include <catacomb/ec-raw.h>
106 #include <catacomb/ectab.h>
108 #include <catacomb/group.h>
109 #include <catacomb/group-guts.h>
111 #include <catacomb/gdsa.h>
112 #include <catacomb/gkcdsa.h>
113 #include <catacomb/rsa.h>
115 #include <catacomb/key.h>
116 #include <catacomb/passphrase.h>
117 #include <catacomb/pixie.h>
119 #include <catacomb/share.h>
120 #include <catacomb/gfshare.h>
122 /*----- Utility macros ----------------------------------------------------*/
124 #define RETURN_OBJ(obj) do { Py_INCREF(obj); return (obj); } while (0)
125 #define RETURN_NONE RETURN_OBJ(Py_None)
126 #define RETURN_NOTIMPL RETURN_OBJ(Py_NotImplemented)
127 #define RETURN_TRUE RETURN_OBJ(Py_True)
128 #define RETURN_FALSE RETURN_OBJ(Py_False)
129 #define RETURN_ME RETURN_OBJ(me)
131 #define EXCERR(exc, str) do { \
132 PyErr_SetString(exc, str); \
135 #define VALERR(str) EXCERR(PyExc_ValueError, str)
136 #define TYERR(str) EXCERR(PyExc_TypeError, str)
137 #define ZDIVERR(str) EXCERR(PyExc_ZeroDivisionError, str)
138 #define SYSERR(str) EXCERR(PyExc_SystemError, str)
139 #define NIERR(str) EXCERR(PyExc_NotImplementedError, str)
140 #define INDEXERR(idx) do { \
141 PyErr_SetObject(PyExc_KeyError, idx); \
144 #define OSERR(name) do { \
145 PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); \
148 #define PGENERR(exc) do { pgenerr(exc); goto end; } while (0)
150 #define CONVFUNC(ty, cty, ext) \
151 int conv##ty(PyObject *o, void *p) \
153 if (!PyObject_TypeCheck(o, ty##_pytype)) \
154 TYERR("wanted a " #ty); \
155 *(cty *)p = ext(o); \
161 #if PY_VERSION_HEX < 0x02050000 /* Compatibility hack */
162 # define ht_name name
163 # define ht_type type
166 #define root_pytype 0
167 #define type_pytype &PyType_Type
168 #define INITTYPE_META(ty, base, meta) do { \
169 ty##_pytype_skel.tp_base = base##_pytype; \
170 ty##_pytype = inittype(&ty##_pytype_skel, meta##_pytype); \
172 #define INITTYPE(ty, base) INITTYPE_META(ty, base, type)
174 extern PyObject *home_module;
176 #define INSERT(name, ob) do { \
177 PyObject *_o = (PyObject *)(ob); \
179 PyModule_AddObject(mod, name, _o); \
182 #define INSEXC(name, var, base, meth) \
183 INSERT(name, var = mkexc(mod, base, name, meth))
185 #define METH(func, doc) \
186 { #func, METHNAME(func), METH_VARARGS, doc },
187 #define KWMETH(func, doc) \
188 { #func, (PyCFunction)METHNAME(func), \
189 METH_VARARGS | METH_KEYWORDS, doc },
191 #define GET(func, doc) \
192 { #func, GETSETNAME(get, func), 0, doc },
193 #define GETSET(func, doc) \
194 { #func, GETSETNAME(get, func), GETSETNAME(set, func), doc },
196 #define MEMBER(name, ty, f, doc) \
197 { #name, ty, offsetof(MEMBERSTRUCT, name), f, doc },
201 _(bytestring) _(buffer) \
202 _(rand) _(algorithms) _(pubkey) _(pgen) \
203 _(mp) _(field) _(ec) _(group) \
204 _(passphrase) _(share) _(key)
205 #define DOMODINIT(m) m##_pyinit();
206 #define DOMODINSERT(m) m##_pyinsert(mod);
207 #define INIT_MODULES do { MODULES(DOMODINIT) } while (0)
208 #define INSERT_MODULES do { MODULES(DOMODINSERT) } while (0)
211 extern void m##_pyinit(void); \
212 extern void m##_pyinsert(PyObject *);
216 #define FREEOBJ(obj) \
217 (((PyObject *)(obj))->ob_type->tp_free((PyObject *)(obj)))
219 #define GEN(func, base) \
220 static PyObject *func(void) \
222 PyObject *d = PyDict_New(); \
226 for (i = 0; g##base##tab[i]; i++) { \
227 o = gc##base##_pywrap((/*unconst*/ gc##base *)g##base##tab[i]); \
228 PyDict_SetItemString(d, \
229 (/*unconst*/ char *)g##base##tab[i]->name, \
236 struct nameval { const char *name; unsigned f; unsigned long value; };
238 extern void setconstants(PyObject *, const struct nameval *);
240 extern PyObject *mexp_common(PyObject *, PyObject *, size_t,
241 PyObject *(*id)(PyObject *),
242 int (*fill)(void *, PyObject *,
243 PyObject *, PyObject *),
244 PyObject *(*exp)(PyObject *, void *, int),
245 void (*drop)(void *));
247 extern int convulong(PyObject *, void *);
248 #define DECL_CONVU_(n) extern int convu##n(PyObject *, void *);
249 DOUINTSZ(DECL_CONVU_)
250 extern int convmpw(PyObject *, void *);
251 extern int convuint(PyObject *, void *);
252 extern int convk64(PyObject *, void *);
253 extern int convszt(PyObject *, void *);
254 extern int convbool(PyObject *, void *);
255 extern PyObject *abstract_pynew(PyTypeObject *, PyObject *, PyObject *);
256 extern PyObject *getbool(int);
257 extern PyObject *getulong(unsigned long);
258 extern PyObject *getk64(kludge64);
259 extern void *newtype(PyTypeObject *, const PyTypeObject *, const char *);
261 struct excinfo { PyObject *ty, *val, *tb; };
262 #define EXCINFO_INIT { 0, 0, 0 }
264 extern PyObject *mkexc(PyObject *, PyObject *, const char *, PyMethodDef *);
265 #define INIT_EXCINFO(exc) do { \
266 struct excinfo *_exc = (exc); _exc->ty = _exc->val = _exc->tb = 0; \
268 #define RELEASE_EXCINFO(exc) do { \
269 struct excinfo *_exc = (exc); \
270 Py_XDECREF(_exc->ty); _exc->ty = 0; \
271 Py_XDECREF(_exc->val); _exc->val = 0; \
272 Py_XDECREF(_exc->tb); _exc->tb = 0; \
274 #define STASH_EXCINFO(exc) do { \
275 struct excinfo *_exc = (exc); \
276 PyErr_Fetch(&_exc->ty, &_exc->val, &_exc->tb); \
277 PyErr_NormalizeException(&_exc->ty, &_exc->val, &_exc->tb); \
279 #define RESTORE_EXCINFO(exc) do { \
280 struct excinfo *_exc = (exc); \
281 PyErr_Restore(_exc->ty, _exc->val, _exc->tb); \
282 _exc->ty = _exc->val = _exc->tb = 0; \
284 extern void report_lost_exception(struct excinfo *, const char *, ...);
285 extern void report_lost_exception_v(struct excinfo *, const char *, va_list);
286 extern void stash_exception(struct excinfo *, const char *, ...);
287 extern void restore_exception(struct excinfo *, const char *, ...);
289 extern void typeready(PyTypeObject *);
290 extern PyTypeObject *inittype(PyTypeObject *, PyTypeObject *);
291 extern void addmethods(const PyMethodDef *);
292 extern PyMethodDef *donemethods(void);
294 /*----- Mapping methods ---------------------------------------------------*/
296 #define GMAP_METH(func, doc) { #func, gmapmeth_##func, METH_VARARGS, doc },
297 #define GMAP_KWMETH(func, doc) \
298 { #func, (PyCFunction)gmapmeth_##func, METH_VARARGS|METH_KEYWORDS, doc },
299 #define GMAP_METHDECL(func, doc) \
300 extern PyObject *gmapmeth_##func(PyObject *, PyObject *);
301 #define GMAP_KWMETHDECL(func, doc) \
302 extern PyObject *gmapmeth_##func(PyObject *, PyObject *, PyObject *);
304 #define GMAP_DOROMETHODS(METH, KWMETH) \
305 METH (has_key, "D.has_key(KEY) -> BOOL") \
306 METH (keys, "D.keys() -> LIST") \
307 METH (values, "D.values() -> LIST") \
308 METH (items, "D.items() -> LIST") \
309 METH (iterkeys, "D.iterkeys() -> ITER") \
310 METH (itervalues, "D.itervalues() -> ITER") \
311 METH (iteritems, "D.iteritems() -> ITER") \
312 KWMETH(get, "D.get(KEY, [default = None]) -> VALUE") \
314 #define GMAP_DOMETHODS(METH, KWMETH) \
315 GMAP_DOROMETHODS(METH, KWMETH) \
316 METH (clear, "D.clear()") \
317 KWMETH(setdefault, "D.setdefault(K, [default = None]) -> VALUE") \
318 KWMETH(pop, "D.pop(KEY, [default = <error>]) -> VALUE") \
319 METH (popitem, "D.popitem() -> (KEY, VALUE)") \
320 METH (update, "D.update(MAP)")
322 GMAP_DOMETHODS(GMAP_METHDECL, GMAP_KWMETHDECL)
323 #define GMAP_ROMETHODS GMAP_DOROMETHODS(GMAP_METH, GMAP_KWMETH)
324 #define GMAP_METHODS GMAP_DOMETHODS(GMAP_METH, GMAP_KWMETH)
325 extern Py_ssize_t gmap_pysize(PyObject *);
326 extern PySequenceMethods gmap_pysequence;
327 extern PyMethodDef gmap_pymethods[];
329 /*----- Bytestrings -------------------------------------------------------*/
331 PyTypeObject *bytestring_pyobj;
332 PyObject *bytestring_pywrap(const void *, size_t);
333 PyObject *bytestring_pywrapbuf(buf *);
335 /*----- Multiprecision arithmetic -----------------------------------------*/
337 typedef struct mp_pyobj {
342 extern PyTypeObject *mp_pytype;
343 extern PyTypeObject *gf_pytype;
344 #define MP_X(o) (((mp_pyobj *)(o))->x)
345 #define MP_PYCHECK(o) PyObject_TypeCheck((o), mp_pytype)
346 #define GF_PYCHECK(o) PyObject_TypeCheck((o), gf_pytype)
348 extern mp *mp_frompylong(PyObject *);
349 extern PyObject *mp_topylong(mp *);
350 extern mp *tomp(PyObject *);
351 extern mp *getmp(PyObject *);
352 extern int convmp(PyObject *, void *);
353 extern mp *getgf(PyObject *);
354 extern int convgf(PyObject *, void *);
355 extern PyObject *mp_pywrap(mp *);
356 extern PyObject *gf_pywrap(mp *);
357 extern long mphash(mp *);
358 extern mp *mp_frompyobject(PyObject *, int);
359 extern PyObject *mp_topystring(mp *, int,
360 const char *, const char *, const char *);
361 extern int mp_tolong_checked(mp *, long *, int);
363 /*----- Abstract fields ---------------------------------------------------*/
365 typedef struct field_pyobj {
370 extern PyTypeObject *fe_pytype;
371 #define FE_PYCHECK(o) PyObject_TypeCheck((o), fe_pytype)
372 #define FE_F(o) (((fe_pyobj *)(o))->f)
373 #define FE_FOBJ(o) ((PyObject *)(o)->ob_type)
374 #define FE_X(o) (((fe_pyobj *)(o))->x)
375 extern PyObject *fe_pywrap(PyObject *, mp *);
377 typedef struct fe_pyobj {
383 extern PyTypeObject *field_pytype;
384 extern PyTypeObject *primefield_pytype;
385 extern PyTypeObject *niceprimefield_pytype;
386 extern PyTypeObject *binfield_pytype;
387 extern PyTypeObject *binpolyfield_pytype;
388 extern PyTypeObject *binnormfield_pytype;
389 #define FIELD_PYCHECK(o) PyObject_TypeCheck((o), field_pytype)
390 #define FIELD_F(o) (((field_pyobj *)(o))->f)
391 extern PyObject *field_pywrap(field *);
392 extern field *field_copy(field *);
394 /*----- Elliptic curves ---------------------------------------------------*/
396 typedef struct ecpt_pyobj {
402 extern PyTypeObject *ecpt_pytype, *ecptcurve_pytype;
403 #define ECPT_PYCHECK(o) PyObject_TypeCheck((o), ecpt_pytype)
404 #define ECPTCURVE_PYCHECK(o) PyObject_TypeCheck((o), ecptcurve_pytype)
405 #define ECPT_C(o) (((ecpt_pyobj *)(o))->c)
406 #define ECPT_COBJ(o) ((PyObject *)(o)->ob_type)
407 #define ECPT_FOBJ(o) ECCURVE_FOBJ(ECPT_COBJ((o)))
408 #define ECPT_P(o) (&((ecpt_pyobj *)(o))->p)
409 extern PyObject *ecpt_pywrap(PyObject *, ec *);
410 extern PyObject *ecpt_pywrapout(void *, ec *);
411 extern int toecpt(ec_curve *, ec *, PyObject *);
412 extern int getecpt(ec_curve *, ec *, PyObject *);
413 extern void getecptout(ec *, PyObject *);
414 extern int convecpt(PyObject *, void *);
416 typedef struct eccurve_pyobj {
422 extern PyTypeObject *eccurve_pytype;
423 extern PyTypeObject *ecprimecurve_pytype;
424 extern PyTypeObject *ecprimeprojcurve_pytype;
425 extern PyTypeObject *ecbincurve_pytype;
426 extern PyTypeObject *ecbinprojcurve_pytype;
427 #define ECCURVE_PYCHECK(o) PyObject_TypeCheck((o), eccurve_pytype)
428 #define ECCURVE_C(o) (((eccurve_pyobj *)(o))->c)
429 #define ECCURVE_FOBJ(o) (((eccurve_pyobj *)(o))->fobj)
430 extern PyObject *eccurve_pywrap(PyObject *, ec_curve *);
431 extern ec_curve *eccurve_copy(ec_curve *);
433 typedef struct ecinfo_pyobj {
439 extern PyTypeObject *ecinfo_pytype;
440 #define ECINFO_PYCHECK(o) PyObject_TypeCheck((o), ecinfo_pytype)
441 #define ECINFO_EI(o) (&((ecinfo_pyobj *)(o))->ei)
442 #define ECINFO_COBJ(o) (((ecinfo_pyobj *)(o))->cobj)
443 extern void ecinfo_copy(ec_info *, const ec_info *);
444 extern PyObject *ecinfo_pywrap(ec_info *);
446 /*----- Cyclic groups -----------------------------------------------------*/
448 typedef struct fginfo_pyobj {
453 PyTypeObject *fginfo_pytype, *dhinfo_pytype, *bindhinfo_pytype;
454 #define FGINFO_DP(fg) (&((fginfo_pyobj *)(fg))->dp)
455 PyObject *fginfo_pywrap(gprime_param *, PyTypeObject *);
457 typedef struct ge_pyobj {
463 extern PyTypeObject *ge_pytype;
464 #define GE_PYCHECK(o) PyObject_TypeCheck((o), ge_pytype)
465 #define GE_X(o) (((ge_pyobj *)(o))->x)
466 #define GE_G(o) (((ge_pyobj *)(o))->g)
467 #define GE_GOBJ(o) ((PyObject *)(group_pyobj *)(o)->ob_type)
468 extern PyObject *ge_pywrap(PyObject *, ge *);
470 typedef struct group_pyobj {
475 extern PyTypeObject *group_pytype;
476 extern PyTypeObject *primegroup_pytype, *bingroup_pytype, *ecgroup_pytype;
477 #define GROUP_G(o) (((group_pyobj *)(o))->g)
478 extern PyObject *group_pywrap(group *);
479 extern group *group_copy(group *);
481 /*----- Random number generators ------------------------------------------*/
485 typedef struct grand_pyobj {
491 extern PyTypeObject *grand_pytype, *truerand_pytype;
492 extern PyTypeObject *lcrand_pytype,* fibrand_pytype;
493 extern PyTypeObject *dsarand_pytype, *bbs_pytype;
494 extern PyObject *rand_pyobj;
495 #define GRAND_PYCHECK(o) PyObject_TypeCheck((o), grand_pytype)
496 #define GRAND_F(o) (((grand_pyobj *)(o))->f)
497 #define GRAND_R(o) (((grand_pyobj *)(o))->r)
498 extern PyObject *grand_pywrap(grand *, unsigned);
499 extern int convgrand(PyObject *, void *);
501 /*----- Key sizes ---------------------------------------------------------*/
503 typedef struct keysz_pyobj {
508 typedef struct keyszrange_pyobj {
514 typedef struct keyszset_pyobj {
520 #define KEYSZ_PYCHECK(o) PyObject_TypeCheck((o), keysz_pytype)
521 extern PyObject *keysz_pywrap(const octet *);
523 /*----- Symmetric cryptography --------------------------------------------*/
525 typedef struct gccipher_pyobj {
530 extern PyTypeObject *gccipher_pytype;
531 #define GCCIPHER_PYCHECK(o) PyObject_TypeCheck((o), gccipher_pytype)
532 #define GCCIPHER_CC(o) (((gccipher_pyobj *)(o))->cc)
533 #define GCCIPHER_F(o) (((gccipher_pyobj *)(o))->f)
534 extern PyObject *gccipher_pywrap(gccipher *);
535 extern int convgccipher(PyObject *, void *);
536 extern int convgcipher(PyObject *, void *);
538 typedef struct gcipher_pyobj {
544 extern PyTypeObject *gcipher_pytype;
545 #define GCIPHER_PYCHECK(o) PyObject_TypeCheck((o), gcipher_pytype)
546 #define GCIPHER_C(o) (((gcipher_pyobj *)(o))->c)
547 #define GCIPHER_F(o) (((gcipher_pyobj *)(o))->f)
548 extern PyObject *gcipher_pywrap(PyObject *, gcipher *, unsigned);
549 extern int convgcipher(PyObject *, void *);
551 typedef struct gchash_pyobj {
556 extern PyTypeObject *gchash_pytype;
557 #define GCHASH_PYCHECK(o) PyObject_TypeCheck((o), gchash_pytype)
558 #define GCHASH_CH(o) (((gchash_pyobj *)(o))->ch)
559 #define GCHASH_F(o) (((gchash_pyobj *)(o))->f)
560 extern PyObject *gchash_pywrap(gchash *);
561 extern int convgchash(PyObject *, void *);
563 typedef struct ghash_pyobj {
569 extern PyTypeObject *ghash_pytype, *gmhash_pytype;
570 extern PyObject *sha_pyobj, *has160_pyobj;
571 #define GHASH_PYCHECK(o) PyObject_TypeCheck((o), ghash_pytype)
572 #define GHASH_H(o) (((ghash_pyobj *)(o))->h)
573 #define GHASH_F(o) (((ghash_pyobj *)(o))->f)
574 extern PyObject *ghash_pywrap(PyObject *, ghash *, unsigned);
575 extern int convghash(PyObject *, void *);
576 extern int convgmhash(PyObject *, void *);
578 typedef struct gcmac_pyobj {
583 extern PyTypeObject *gcmac_pytype;
584 #define GCMAC_PYCHECK(o) PyObject_TypeCheck((o), gcmac_pytype)
585 #define GCMAC_CM(o) (((gcmac_pyobj *)(o))->cm)
586 #define GCMAC_F(o) (((gcmac_pyobj *)(o))->f)
587 extern PyObject *gcmac_pywrap(gcmac *);
588 extern int convgcmac(PyObject *, void *);
590 typedef struct gmac_pyobj {
596 extern PyTypeObject *gmac_pytype;
597 #define GMAC_PYCHECK(o) PyObject_TypeCheck((o), gmac_pytype)
598 #define GMAC_M(o) (((gmac_pyobj *)(o))->m)
599 #define GMAC_F(o) (((gmac_pyobj *)(o))->f)
600 extern PyObject *gmac_pywrap(PyObject *, gmac *, unsigned);
601 extern int convgmac(PyObject *, void *);
603 /*----- Key generation ----------------------------------------------------*/
605 typedef struct pfilt_pyobj {
611 extern PyTypeObject *pfilt_pytype;
612 #define PFILT_PYCHECK(o) PyObject_TypeCheck(o, pfilt_pytype)
613 #define PFILT_F(o) (&((pfilt_pyobj *)(o))->f)
614 #define PFILT_ST(o) (((pfilt_pyobj *)(o))->st)
616 typedef struct { pgen_proc *proc; void *ctx; } pgev;
617 #define PGEV_HEAD PyObject_HEAD pgev pg;
619 typedef struct pgev_pyobj {
623 extern PyTypeObject *pgev_pytype;
624 #define PGEV_PYCHECK(o) PyObject_TypeCheck(o, pgev_pytype)
625 #define PGEV_PG(o) (&((pgev_pyobj *)(o))->pg)
627 typedef struct pypgev {
633 extern int convpgev(PyObject *, void *);
634 extern void droppgev(pypgev *);
635 extern void pgenerr(struct excinfo *exc);
637 /*----- That's all, folks -------------------------------------------------*/