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 ------------------------------------------------------*/
37 #include <longintrepr.h>
38 #include <structmember.h>
40 #include <mLib/darray.h>
41 #include <mLib/dstr.h>
42 #include <mLib/macros.h>
43 #include <mLib/quis.h>
44 #include <mLib/unihash.h>
46 #include <catacomb/buf.h>
48 #include <catacomb/grand.h>
49 #include <catacomb/rand.h>
50 #include <catacomb/noise.h>
51 #include <catacomb/bbs.h>
52 #include <catacomb/mprand.h>
53 #include <catacomb/lcrand.h>
54 #include <catacomb/fibrand.h>
55 #include <catacomb/dsarand.h>
56 #include <catacomb/sslprf.h>
57 #include <catacomb/tlsprf.h>
58 #include <catacomb/blkc.h>
60 #include <catacomb/gcipher.h>
61 #include <catacomb/ghash.h>
62 #include <catacomb/gmac.h>
63 #include <catacomb/md5.h>
64 #include <catacomb/md5-hmac.h>
65 #include <catacomb/poly1305.h>
66 #include <catacomb/sha.h>
67 #include <catacomb/sha-mgf.h>
68 #include <catacomb/sha-hmac.h>
70 #include <catacomb/mp.h>
71 #include <catacomb/mpint.h>
72 #include <catacomb/mpmul.h>
73 #include <catacomb/mpcrt.h>
74 #include <catacomb/mpmont.h>
75 #include <catacomb/mpbarrett.h>
76 #include <catacomb/mpreduce.h>
77 #include <catacomb/mp-fibonacci.h>
79 #include <catacomb/pgen.h>
80 #include <catacomb/pfilt.h>
81 #include <catacomb/strongprime.h>
82 #include <catacomb/limlee.h>
83 #include <catacomb/dh.h>
84 #include <catacomb/ptab.h>
85 #include <catacomb/bintab.h>
86 #include <catacomb/dsa.h>
88 #include <catacomb/gf.h>
89 #include <catacomb/gfreduce.h>
90 #include <catacomb/gfn.h>
92 #include <catacomb/field.h>
93 #include <catacomb/field-guts.h>
95 #include <catacomb/ec.h>
96 #include <catacomb/ec-raw.h>
97 #include <catacomb/ectab.h>
99 #include <catacomb/group.h>
100 #include <catacomb/group-guts.h>
102 #include <catacomb/gdsa.h>
103 #include <catacomb/gkcdsa.h>
104 #include <catacomb/rsa.h>
106 #include <catacomb/key.h>
107 #include <catacomb/passphrase.h>
108 #include <catacomb/pixie.h>
110 #include <catacomb/share.h>
111 #include <catacomb/gfshare.h>
113 /*----- Utility macros ----------------------------------------------------*/
115 #define RETURN_OBJ(obj) do { Py_INCREF(obj); return (obj); } while (0)
116 #define RETURN_NONE RETURN_OBJ(Py_None)
117 #define RETURN_NOTIMPL RETURN_OBJ(Py_NotImplemented)
118 #define RETURN_TRUE RETURN_OBJ(Py_True)
119 #define RETURN_FALSE RETURN_OBJ(Py_False)
120 #define RETURN_ME RETURN_OBJ(me)
122 #define EXCERR(exc, str) do { \
123 PyErr_SetString(exc, str); \
126 #define VALERR(str) EXCERR(PyExc_ValueError, str)
127 #define TYERR(str) EXCERR(PyExc_TypeError, str)
128 #define ZDIVERR(str) EXCERR(PyExc_ZeroDivisionError, str)
129 #define SYSERR(str) EXCERR(PyExc_SystemError, str)
130 #define NIERR(str) EXCERR(PyExc_NotImplementedError, str)
131 #define INDEXERR(idx) do { \
132 PyErr_SetObject(PyExc_KeyError, idx); \
135 #define OSERR(name) do { \
136 PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); \
139 #define PGENERR do { pgenerr(); goto end; } while (0)
141 #define CONVFUNC(ty, cty, ext) \
142 int conv##ty(PyObject *o, void *p) \
144 if (!PyObject_TypeCheck(o, ty##_pytype)) \
145 TYERR("wanted a " #ty); \
146 *(cty *)p = ext(o); \
152 #if PY_VERSION_HEX < 0x02050000 /* Compatibility hack */
153 # define ht_name name
154 # define ht_type type
157 #define root_pytype 0
158 #define type_pytype &PyType_Type
159 #define INITTYPE_META(ty, base, meta) do { \
160 ty##_pytype_skel.tp_base = base##_pytype; \
161 ty##_pytype = inittype(&ty##_pytype_skel, meta##_pytype); \
163 #define INITTYPE(ty, base) INITTYPE_META(ty, base, type)
165 #define INSERT(name, ob) do { \
166 PyObject *_o = (PyObject *)(ob); \
168 PyModule_AddObject(mod, name, _o); \
171 #define INSEXC(name, var, base, meth) \
172 INSERT(name, var = mkexc(mod, base, name, meth))
174 #define METH(func, doc) \
175 { #func, METHNAME(func), METH_VARARGS, doc },
176 #define KWMETH(func, doc) \
177 { #func, (PyCFunction)METHNAME(func), \
178 METH_VARARGS | METH_KEYWORDS, doc },
180 #define GET(func, doc) \
181 { #func, GETSETNAME(get, func), 0, doc },
182 #define GETSET(func, doc) \
183 { #func, GETSETNAME(get, func), GETSETNAME(set, func), doc },
185 #define MEMBER(name, ty, f, doc) \
186 { #name, ty, offsetof(MEMBERSTRUCT, name), f, doc },
190 _(bytestring) _(buffer) \
191 _(rand) _(algorithms) _(pubkey) _(pgen) \
192 _(mp) _(field) _(ec) _(group) \
193 _(passphrase) _(share) _(key)
194 #define DOMODINIT(m) m##_pyinit();
195 #define DOMODINSERT(m) m##_pyinsert(mod);
196 #define INIT_MODULES do { MODULES(DOMODINIT) } while (0)
197 #define INSERT_MODULES do { MODULES(DOMODINSERT) } while (0)
200 extern void m##_pyinit(void); \
201 extern void m##_pyinsert(PyObject *);
205 #define FREEOBJ(obj) \
206 (((PyObject *)(obj))->ob_type->tp_free((PyObject *)(obj)))
208 #define GEN(func, base) \
209 static PyObject *func(void) \
211 PyObject *d = PyDict_New(); \
215 for (i = 0; g##base##tab[i]; i++) { \
216 o = gc##base##_pywrap((/*unconst*/ gc##base *)g##base##tab[i]); \
217 PyDict_SetItemString(d, \
218 (/*unconst*/ char *)g##base##tab[i]->name, \
225 struct nameval
{ const char *name
; unsigned long value
; };
226 extern void setconstants(PyObject
*, const struct nameval
*);
228 extern PyObject
*mexp_common(PyObject
*, PyObject
*, size_t,
229 PyObject
*(*id
)(PyObject
*),
230 int (*fill
)(void *, PyObject
*,
231 PyObject
*, PyObject
*),
232 PyObject
*(*exp
)(PyObject
*, void *, int),
233 void (*drop
)(void *));
235 extern int convulong(PyObject
*, void *);
236 #define DECL_CONVU_(n) extern int convu##n(PyObject *, void *);
237 DOUINTSZ(DECL_CONVU_
)
238 extern int convmpw(PyObject
*, void *);
239 extern int convuint(PyObject
*, void *);
240 extern int convk64(PyObject
*, void *);
241 extern int convszt(PyObject
*, void *);
242 extern int convbool(PyObject
*, void *);
243 extern PyObject
*abstract_pynew(PyTypeObject
*, PyObject
*, PyObject
*);
244 extern PyObject
*getbool(int);
245 extern PyObject
*getulong(unsigned long);
246 extern PyObject
*getk64(kludge64
);
247 extern void *newtype(PyTypeObject
*, const PyTypeObject
*, const char *);
249 extern PyObject
*mkexc(PyObject
*, PyObject
*, const char *, PyMethodDef
*);
250 extern void typeready(PyTypeObject
*);
251 extern PyTypeObject
*inittype(PyTypeObject
*, PyTypeObject
*);
252 extern void addmethods(const PyMethodDef
*);
253 extern PyMethodDef
*donemethods(void);
255 /*----- Mapping methods ---------------------------------------------------*/
257 #define GMAP_METH(func, doc) { #func, gmapmeth_##func, METH_VARARGS, doc },
258 #define GMAP_KWMETH(func, doc) \
259 { #func, (PyCFunction)gmapmeth_##func, METH_VARARGS|METH_KEYWORDS, doc },
260 #define GMAP_METHDECL(func, doc) \
261 extern PyObject *gmapmeth_##func(PyObject *, PyObject *);
262 #define GMAP_KWMETHDECL(func, doc) \
263 extern PyObject *gmapmeth_##func(PyObject *, PyObject *, PyObject *);
265 #define GMAP_DOROMETHODS(METH, KWMETH) \
266 METH (has_key, "D.has_key(KEY) -> BOOL") \
267 METH (keys, "D.keys() -> LIST") \
268 METH (values, "D.values() -> LIST") \
269 METH (items, "D.items() -> LIST") \
270 METH (iterkeys, "D.iterkeys() -> ITER") \
271 METH (itervalues, "D.itervalues() -> ITER") \
272 METH (iteritems, "D.iteritems() -> ITER") \
273 KWMETH(get, "D.get(KEY, [default = None]) -> VALUE") \
275 #define GMAP_DOMETHODS(METH, KWMETH) \
276 GMAP_DOROMETHODS(METH, KWMETH) \
277 METH (clear, "D.clear()") \
278 KWMETH(setdefault, "D.setdefault(K, [default = None]) -> VALUE") \
279 KWMETH(pop, "D.pop(KEY, [default = <error>]) -> VALUE") \
280 METH (popitem, "D.popitem() -> (KEY, VALUE)") \
281 METH (update, "D.update(MAP)")
283 GMAP_DOMETHODS(GMAP_METHDECL
, GMAP_KWMETHDECL
)
284 #define GMAP_ROMETHODS GMAP_DOROMETHODS(GMAP_METH, GMAP_KWMETH)
285 #define GMAP_METHODS GMAP_DOMETHODS(GMAP_METH, GMAP_KWMETH)
286 extern Py_ssize_t
gmap_pysize(PyObject
*);
287 extern PySequenceMethods gmap_pysequence
;
288 extern PyMethodDef gmap_pymethods
[];
290 /*----- Bytestrings -------------------------------------------------------*/
292 PyTypeObject
*bytestring_pyobj
;
293 PyObject
*bytestring_pywrap(const void *, size_t);
294 PyObject
*bytestring_pywrapbuf(buf
*);
296 /*----- Multiprecision arithmetic -----------------------------------------*/
298 typedef struct mp_pyobj
{
303 extern PyTypeObject
*mp_pytype
;
304 extern PyTypeObject
*gf_pytype
;
305 #define MP_X(o) (((mp_pyobj *)(o))->x)
306 #define MP_PYCHECK(o) PyObject_TypeCheck((o), mp_pytype)
307 #define GF_PYCHECK(o) PyObject_TypeCheck((o), gf_pytype)
309 extern mp
*mp_frompylong(PyObject
*);
310 extern PyObject
*mp_topylong(mp
*);
311 extern mp
*tomp(PyObject
*);
312 extern mp
*getmp(PyObject
*);
313 extern int convmp(PyObject
*, void *);
314 extern mp
*getgf(PyObject
*);
315 extern int convgf(PyObject
*, void *);
316 extern PyObject
*mp_pywrap(mp
*);
317 extern PyObject
*gf_pywrap(mp
*);
318 extern mp
*mp_frompyobject(PyObject
*, int);
319 extern PyObject
*mp_topystring(mp
*, int,
320 const char *, const char *, const char *);
321 extern int mp_tolong_checked(mp
*, long *);
323 /*----- Abstract fields ---------------------------------------------------*/
325 typedef struct field_pyobj
{
330 extern PyTypeObject
*fe_pytype
;
331 #define FE_PYCHECK(o) PyObject_TypeCheck((o), fe_pytype)
332 #define FE_F(o) (((fe_pyobj *)(o))->f)
333 #define FE_FOBJ(o) ((PyObject *)(o)->ob_type)
334 #define FE_X(o) (((fe_pyobj *)(o))->x)
335 extern PyObject
*fe_pywrap(PyObject
*, mp
*);
336 extern mp
*getfe(field
*, PyObject
*);
338 typedef struct fe_pyobj
{
344 extern PyTypeObject
*field_pytype
;
345 extern PyTypeObject
*primefield_pytype
;
346 extern PyTypeObject
*niceprimefield_pytype
;
347 extern PyTypeObject
*binfield_pytype
;
348 extern PyTypeObject
*binpolyfield_pytype
;
349 extern PyTypeObject
*binnormfield_pytype
;
350 #define FIELD_PYCHECK(o) PyObject_TypeCheck((o), field_pytype)
351 #define FIELD_F(o) (((field_pyobj *)(o))->f)
352 extern PyObject
*field_pywrap(field
*);
353 extern field
*field_copy(field
*);
355 /*----- Elliptic curves ---------------------------------------------------*/
357 typedef struct ecpt_pyobj
{
363 extern PyTypeObject
*ecpt_pytype
, *ecptcurve_pytype
;
364 #define ECPT_PYCHECK(o) PyObject_TypeCheck((o), ecpt_pytype)
365 #define ECPTCURVE_PYCHECK(o) PyObject_TypeCheck((o), ecptcurve_pytype)
366 #define ECPT_C(o) (((ecpt_pyobj *)(o))->c)
367 #define ECPT_COBJ(o) ((PyObject *)(o)->ob_type)
368 #define ECPT_FOBJ(o) ECCURVE_FOBJ(ECPT_COBJ((o)))
369 #define ECPT_P(o) (&((ecpt_pyobj *)(o))->p)
370 extern PyObject
*ecpt_pywrap(PyObject
*, ec
*);
371 extern PyObject
*ecpt_pywrapout(void *, ec
*);
372 extern int toecpt(ec_curve
*, ec
*, PyObject
*);
373 extern int getecpt(ec_curve
*, ec
*, PyObject
*);
374 extern void getecptout(ec
*, PyObject
*);
375 extern int convecpt(PyObject
*, void *);
377 typedef struct eccurve_pyobj
{
383 extern PyTypeObject
*eccurve_pytype
;
384 extern PyTypeObject
*ecprimecurve_pytype
;
385 extern PyTypeObject
*ecprimeprojcurve_pytype
;
386 extern PyTypeObject
*ecbincurve_pytype
;
387 extern PyTypeObject
*ecbinprojcurve_pytype
;
388 #define ECCURVE_PYCHECK(o) PyObject_TypeCheck((o), eccurve_pytype)
389 #define ECCURVE_C(o) (((eccurve_pyobj *)(o))->c)
390 #define ECCURVE_FOBJ(o) (((eccurve_pyobj *)(o))->fobj)
391 extern PyObject
*eccurve_pywrap(PyObject
*, ec_curve
*);
392 extern ec_curve
*eccurve_copy(ec_curve
*);
394 typedef struct ecinfo_pyobj
{
400 extern PyTypeObject
*ecinfo_pytype
;
401 #define ECINFO_PYCHECK(o) PyObject_TypeCheck((o), ecinfo_pytype)
402 #define ECINFO_EI(o) (&((ecinfo_pyobj *)(o))->ei)
403 #define ECINFO_COBJ(o) (((ecinfo_pyobj *)(o))->cobj)
404 extern void ecinfo_copy(ec_info
*, const ec_info
*);
405 extern PyObject
*ecinfo_pywrap(ec_info
*);
407 /*----- Cyclic groups -----------------------------------------------------*/
409 typedef struct fginfo_pyobj
{
414 PyTypeObject
*fginfo_pytype
, *dhinfo_pytype
, *bindhinfo_pytype
;
415 #define FGINFO_DP(fg) (&((fginfo_pyobj *)(fg))->dp)
416 PyObject
*fginfo_pywrap(gprime_param
*, PyTypeObject
*);
418 typedef struct ge_pyobj
{
424 extern PyTypeObject
*ge_pytype
;
425 #define GE_PYCHECK(o) PyObject_TypeCheck((o), ge_pytype)
426 #define GE_X(o) (((ge_pyobj *)(o))->x)
427 #define GE_G(o) (((ge_pyobj *)(o))->g)
428 #define GE_GOBJ(o) ((PyObject *)(group_pyobj *)(o)->ob_type)
429 extern PyObject
*ge_pywrap(PyObject
*, ge
*);
431 typedef struct group_pyobj
{
436 extern PyTypeObject
*group_pytype
;
437 extern PyTypeObject
*primegroup_pytype
, *bingroup_pytype
, *ecgroup_pytype
;
438 #define GROUP_G(o) (((group_pyobj *)(o))->g)
439 extern PyObject
*group_pywrap(group
*);
440 extern group
*group_copy(group
*);
442 /*----- Random number generators ------------------------------------------*/
446 typedef struct grand_pyobj
{
452 extern PyTypeObject
*grand_pytype
, *truerand_pytype
;
453 extern PyTypeObject
*lcrand_pytype
,* fibrand_pytype
;
454 extern PyTypeObject
*dsarand_pytype
, *bbs_pytype
;
455 extern PyObject
*rand_pyobj
;
456 #define GRAND_PYCHECK(o) PyObject_TypeCheck((o), grand_pytype)
457 #define GRAND_F(o) (((grand_pyobj *)(o))->f)
458 #define GRAND_R(o) (((grand_pyobj *)(o))->r)
459 extern PyObject
*grand_pywrap(grand
*, unsigned);
460 extern int convgrand(PyObject
*, void *);
462 /*----- Key sizes ---------------------------------------------------------*/
464 typedef struct keysz_pyobj
{
469 typedef struct keyszrange_pyobj
{
475 typedef struct keyszset_pyobj
{
481 #define KEYSZ_PYCHECK(o) PyObject_TypeCheck((o), keysz_pytype)
482 extern PyObject
*keysz_pywrap(const octet
*);
484 /*----- Symmetric cryptography --------------------------------------------*/
486 typedef struct gccipher_pyobj
{
491 extern PyTypeObject
*gccipher_pytype
;
492 #define GCCIPHER_PYCHECK(o) PyObject_TypeCheck((o), gccipher_pytype)
493 #define GCCIPHER_CC(o) (((gccipher_pyobj *)(o))->cc)
494 #define GCCIPHER_F(o) (((gccipher_pyobj *)(o))->f)
495 extern PyObject
*gccipher_pywrap(gccipher
*);
496 extern int convgccipher(PyObject
*, void *);
497 extern int convgcipher(PyObject
*, void *);
499 typedef struct gcipher_pyobj
{
505 extern PyTypeObject
*gcipher_pytype
;
506 #define GCIPHER_PYCHECK(o) PyObject_TypeCheck((o), gcipher_pytype)
507 #define GCIPHER_C(o) (((gcipher_pyobj *)(o))->c)
508 #define GCIPHER_F(o) (((gcipher_pyobj *)(o))->f)
509 extern PyObject
*gcipher_pywrap(PyObject
*, gcipher
*, unsigned);
510 extern int convgcipher(PyObject
*, void *);
512 typedef struct gchash_pyobj
{
517 extern PyTypeObject
*gchash_pytype
;
518 #define GCHASH_PYCHECK(o) PyObject_TypeCheck((o), gchash_pytype)
519 #define GCHASH_CH(o) (((gchash_pyobj *)(o))->ch)
520 #define GCHASH_F(o) (((gchash_pyobj *)(o))->f)
521 extern PyObject
*gchash_pywrap(gchash
*);
522 extern int convgchash(PyObject
*, void *);
524 typedef struct ghash_pyobj
{
530 extern PyTypeObject
*ghash_pytype
, *gmhash_pytype
;
531 extern PyObject
*sha_pyobj
, *has160_pyobj
;
532 #define GHASH_PYCHECK(o) PyObject_TypeCheck((o), ghash_pytype)
533 #define GHASH_H(o) (((ghash_pyobj *)(o))->h)
534 #define GHASH_F(o) (((ghash_pyobj *)(o))->f)
535 extern PyObject
*ghash_pywrap(PyObject
*, ghash
*, unsigned);
536 extern int convghash(PyObject
*, void *);
537 extern int convgmhash(PyObject
*, void *);
539 typedef struct gcmac_pyobj
{
544 extern PyTypeObject
*gcmac_pytype
;
545 #define GCMAC_PYCHECK(o) PyObject_TypeCheck((o), gcmac_pytype)
546 #define GCMAC_CM(o) (((gcmac_pyobj *)(o))->cm)
547 #define GCMAC_F(o) (((gcmac_pyobj *)(o))->f)
548 extern PyObject
*gcmac_pywrap(gcmac
*);
549 extern int convgcmac(PyObject
*, void *);
551 typedef struct gmac_pyobj
{
557 extern PyTypeObject
*gmac_pytype
;
558 #define GMAC_PYCHECK(o) PyObject_TypeCheck((o), gmac_pytype)
559 #define GMAC_M(o) (((gmac_pyobj *)(o))->m)
560 #define GMAC_F(o) (((gmac_pyobj *)(o))->f)
561 extern PyObject
*gmac_pywrap(PyObject
*, gmac
*, unsigned);
562 extern int convgmac(PyObject
*, void *);
564 /*----- Key generation ----------------------------------------------------*/
566 typedef struct pfilt_pyobj
{
572 extern PyTypeObject
*pfilt_pytype
;
573 #define PFILT_PYCHECK(o) PyObject_TypeCheck(o, pfilt_pytype)
574 #define PFILT_F(o) (&((pfilt_pyobj *)(o))->f)
575 #define PFILT_ST(o) (((pfilt_pyobj *)(o))->st)
577 typedef struct { pgen_proc
*proc
; void *ctx
; } pgev
;
578 #define PGEV_HEAD PyObject_HEAD pgev pg;
580 typedef struct pgev_pyobj
{
584 extern PyTypeObject
*pgev_pytype
;
585 #define PGEV_PYCHECK(o) PyObject_TypeCheck(o, pgev_pytype)
586 #define PGEV_PG(o) (&((pgev_pyobj *)(o))->pg)
588 extern int convpgev(PyObject
*, void *);
589 extern void droppgev(pgev
*);
590 extern void pgenerr(void);
592 /*----- That's all, folks -------------------------------------------------*/