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/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>
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>
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>
98 #include <catacomb/gf.h>
99 #include <catacomb/gfreduce.h>
100 #include <catacomb/gfn.h>
102 #include <catacomb/field.h>
103 #include <catacomb/field-guts.h>
105 #include <catacomb/ec.h>
106 #include <catacomb/ec-raw.h>
107 #include <catacomb/ectab.h>
109 #include <catacomb/group.h>
110 #include <catacomb/group-guts.h>
112 #include <catacomb/gdsa.h>
113 #include <catacomb/gkcdsa.h>
114 #include <catacomb/rsa.h>
116 #include <catacomb/key.h>
117 #include <catacomb/passphrase.h>
118 #include <catacomb/pixie.h>
120 #include <catacomb/share.h>
121 #include <catacomb/gfshare.h>
123 /*----- Other preliminaries -----------------------------------------------*/
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")))
131 # define PRIVATE_SYMBOLS GOBBLE_SEMI
132 # define PUBLIC_SYMBOLS GOBBLE_SEMI
138 /*----- Utility macros ----------------------------------------------------*/
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)
147 #define EXCERR(exc, str) do { \
148 PyErr_SetString(exc, str); \
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); \
162 #define OSERR(name) do { \
163 PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); \
166 #define PGENERR(exc) do { pgenerr(exc); goto end; } while (0)
168 #define CONVFUNC(ty, cty, ext) \
169 int conv##ty(PyObject *o, void *p) \
171 if (!PyObject_TypeCheck(o, ty##_pytype)) \
172 TYERR("wanted a " #ty); \
173 *(cty *)p = ext(o); \
179 #define root_pytype 0
180 #define type_pytype &PyType_Type
181 #define INITTYPE_META(ty, base, meta) do { \
182 ty##_pytype_skel.tp_base = base##_pytype; \
183 ty##_pytype = inittype(&ty##_pytype_skel, meta##_pytype); \
185 #define INITTYPE(ty, base) INITTYPE_META(ty, base, type)
187 extern PyObject
*home_module
;
188 extern PyObject
*modname
;
190 #define INSERT(name, ob) do { \
191 PyObject *_o = (PyObject *)(ob); \
193 PyModule_AddObject(mod, name, _o); \
196 #define INSEXC(name, var, base, meth) \
197 INSERT(name, var = mkexc(mod, base, name, meth))
199 #define METH(func, doc) \
200 { #func, METHNAME(func), METH_VARARGS, doc },
201 #define KWMETH(func, doc) \
202 { #func, (PyCFunction)METHNAME(func), \
203 METH_VARARGS | METH_KEYWORDS, doc },
205 #define GET(func, doc) \
206 { #func, GETSETNAME(get, func), 0, doc },
207 #define GETSET(func, doc) \
208 { #func, GETSETNAME(get, func), GETSETNAME(set, func), doc },
210 #define MEMBER(name, ty, f, doc) \
211 { #name, ty, offsetof(MEMBERSTRUCT, name), f, doc },
215 _(bytestring) _(buffer) \
216 _(rand) _(algorithms) _(pubkey) _(pgen) \
217 _(mp) _(field) _(ec) _(group) \
218 _(passphrase) _(share) _(key)
219 #define DOMODINIT(m) m##_pyinit();
220 #define DOMODINSERT(m) m##_pyinsert(mod);
221 #define INIT_MODULES do { MODULES(DOMODINIT) } while (0)
222 #define INSERT_MODULES do { MODULES(DOMODINSERT) } while (0)
225 extern void m##_pyinit(void); \
226 extern void m##_pyinsert(PyObject *);
230 #define FREEOBJ(obj) \
231 (((PyObject *)(obj))->ob_type->tp_free((PyObject *)(obj)))
233 #define GEN(func, base) \
234 static PyObject *func(void) \
236 PyObject *d = PyDict_New(); \
240 for (i = 0; g##base##tab[i]; i++) { \
241 o = gc##base##_pywrap((/*unconst*/ gc##base *)g##base##tab[i]); \
242 PyDict_SetItemString(d, \
243 (/*unconst*/ char *)g##base##tab[i]->name, \
250 #define KWLIST (/*unconst*/ char **)kwlist
252 struct nameval
{ const char *name
; unsigned f
; unsigned long value
; };
254 extern void setconstants(PyObject
*, const struct nameval
*);
256 extern PyObject
*mexp_common(PyObject
*, PyObject
*, size_t,
257 PyObject
*(*id
)(PyObject
*),
258 int (*fill
)(void *, PyObject
*,
259 PyObject
*, PyObject
*),
260 PyObject
*(*exp
)(PyObject
*, void *, int),
261 void (*drop
)(void *));
263 extern int convulong(PyObject
*, void *);
264 #define DECL_CONVU_(n) extern int convu##n(PyObject *, void *);
265 DOUINTSZ(DECL_CONVU_
)
266 extern int convmpw(PyObject
*, void *);
267 extern int convuint(PyObject
*, void *);
268 extern int convk64(PyObject
*, void *);
269 extern int convszt(PyObject
*, void *);
270 extern int convbool(PyObject
*, void *);
271 extern PyObject
*abstract_pynew(PyTypeObject
*, PyObject
*, PyObject
*);
272 extern PyObject
*getbool(int);
273 extern PyObject
*getulong(unsigned long);
274 extern PyObject
*getk64(kludge64
);
275 extern void *newtype(PyTypeObject
*, const PyTypeObject
*, const char *);
277 struct excinfo
{ PyObject
*ty
, *val
, *tb
; };
278 #define EXCINFO_INIT { 0, 0, 0 }
280 extern PyObject
*mkexc(PyObject
*, PyObject
*, const char *, PyMethodDef
*);
281 #define INIT_EXCINFO(exc) do { \
282 struct excinfo *_exc = (exc); _exc->ty = _exc->val = _exc->tb = 0; \
284 #define RELEASE_EXCINFO(exc) do { \
285 struct excinfo *_exc = (exc); \
286 Py_XDECREF(_exc->ty); _exc->ty = 0; \
287 Py_XDECREF(_exc->val); _exc->val = 0; \
288 Py_XDECREF(_exc->tb); _exc->tb = 0; \
290 #define STASH_EXCINFO(exc) do { \
291 struct excinfo *_exc = (exc); \
292 PyErr_Fetch(&_exc->ty, &_exc->val, &_exc->tb); \
293 PyErr_NormalizeException(&_exc->ty, &_exc->val, &_exc->tb); \
295 #define RESTORE_EXCINFO(exc) do { \
296 struct excinfo *_exc = (exc); \
297 PyErr_Restore(_exc->ty, _exc->val, _exc->tb); \
298 _exc->ty = _exc->val = _exc->tb = 0; \
300 extern void report_lost_exception(struct excinfo
*, const char *, ...);
301 extern void report_lost_exception_v(struct excinfo
*, const char *, va_list);
302 extern void stash_exception(struct excinfo
*, const char *, ...);
303 extern void restore_exception(struct excinfo
*, const char *, ...);
305 extern void typeready(PyTypeObject
*);
306 extern PyTypeObject
*inittype(PyTypeObject
*, PyTypeObject
*);
307 extern void addmethods(const PyMethodDef
*);
308 extern PyMethodDef
*donemethods(void);
310 /*----- Mapping methods ---------------------------------------------------*/
312 #define GMAP_METH(func, doc) { #func, gmapmeth_##func, METH_VARARGS, doc },
313 #define GMAP_KWMETH(func, doc) \
314 { #func, (PyCFunction)gmapmeth_##func, METH_VARARGS|METH_KEYWORDS, doc },
315 #define GMAP_METHDECL(func, doc) \
316 extern PyObject *gmapmeth_##func(PyObject *, PyObject *);
317 #define GMAP_KWMETHDECL(func, doc) \
318 extern PyObject *gmapmeth_##func(PyObject *, PyObject *, PyObject *);
320 #define GMAP_DOROMETHODS(METH, KWMETH) \
321 METH (has_key, "D.has_key(KEY) -> BOOL") \
322 METH (keys, "D.keys() -> LIST") \
323 METH (values, "D.values() -> LIST") \
324 METH (items, "D.items() -> LIST") \
325 METH (iterkeys, "D.iterkeys() -> ITER") \
326 METH (itervalues, "D.itervalues() -> ITER") \
327 METH (iteritems, "D.iteritems() -> ITER") \
328 KWMETH(get, "D.get(KEY, [default = None]) -> VALUE") \
330 #define GMAP_DOMETHODS(METH, KWMETH) \
331 GMAP_DOROMETHODS(METH, KWMETH) \
332 METH (clear, "D.clear()") \
333 KWMETH(setdefault, "D.setdefault(K, [default = None]) -> VALUE") \
334 KWMETH(pop, "D.pop(KEY, [default = <error>]) -> VALUE") \
335 METH (popitem, "D.popitem() -> (KEY, VALUE)") \
336 METH (update, "D.update(MAP)")
338 GMAP_DOMETHODS(GMAP_METHDECL
, GMAP_KWMETHDECL
)
339 #define GMAP_ROMETHODS GMAP_DOROMETHODS(GMAP_METH, GMAP_KWMETH)
340 #define GMAP_METHODS GMAP_DOMETHODS(GMAP_METH, GMAP_KWMETH)
341 extern Py_ssize_t
gmap_pysize(PyObject
*);
342 extern PySequenceMethods gmap_pysequence
;
343 extern PyMethodDef gmap_pymethods
[];
345 /*----- Bytestrings -------------------------------------------------------*/
347 PyObject
*bytestring_pywrap(const void *, size_t);
348 PyObject
*bytestring_pywrapbuf(buf
*);
350 /*----- Multiprecision arithmetic -----------------------------------------*/
352 typedef struct mp_pyobj
{
357 extern PyTypeObject
*mp_pytype
;
358 extern PyTypeObject
*gf_pytype
;
359 #define MP_X(o) (((mp_pyobj *)(o))->x)
360 #define MP_PYCHECK(o) PyObject_TypeCheck((o), mp_pytype)
361 #define GF_PYCHECK(o) PyObject_TypeCheck((o), gf_pytype)
363 extern mp
*mp_frompylong(PyObject
*);
364 extern PyObject
*mp_topylong(mp
*);
365 extern mp
*tomp(PyObject
*);
366 extern mp
*getmp(PyObject
*);
367 extern int convmp(PyObject
*, void *);
368 extern mp
*getgf(PyObject
*);
369 extern int convgf(PyObject
*, void *);
370 extern PyObject
*mp_pywrap(mp
*);
371 extern PyObject
*gf_pywrap(mp
*);
372 extern long mphash(mp
*);
373 extern mp
*mp_frompyobject(PyObject
*, int);
374 extern PyObject
*mp_topystring(mp
*, int,
375 const char *, const char *, const char *);
376 extern int mp_tolong_checked(mp
*, long *, int);
378 /*----- Abstract fields ---------------------------------------------------*/
380 typedef struct field_pyobj
{
385 extern PyTypeObject
*field_pytype
;
386 extern PyTypeObject
*primefield_pytype
;
387 extern PyTypeObject
*niceprimefield_pytype
;
388 extern PyTypeObject
*binfield_pytype
;
389 extern PyTypeObject
*binpolyfield_pytype
;
390 extern PyTypeObject
*binnormfield_pytype
;
391 #define FIELD_PYCHECK(o) PyObject_TypeCheck((o), field_pytype)
392 #define FIELD_F(o) (((field_pyobj *)(o))->f)
393 extern PyObject
*field_pywrap(field
*);
394 extern field
*field_copy(field
*);
396 typedef struct fe_pyobj
{
402 extern PyTypeObject
*fe_pytype
;
403 #define FE_PYCHECK(o) PyObject_TypeCheck((o), fe_pytype)
404 #define FE_F(o) (((fe_pyobj *)(o))->f)
405 #define FE_FOBJ(o) ((PyObject *)(o)->ob_type)
406 #define FE_X(o) (((fe_pyobj *)(o))->x)
407 extern PyObject
*fe_pywrap(PyObject
*, mp
*);
409 /*----- Elliptic curves ---------------------------------------------------*/
411 typedef struct eccurve_pyobj
{
417 extern PyTypeObject
*eccurve_pytype
;
418 extern PyTypeObject
*ecprimecurve_pytype
;
419 extern PyTypeObject
*ecprimeprojcurve_pytype
;
420 extern PyTypeObject
*ecbincurve_pytype
;
421 extern PyTypeObject
*ecbinprojcurve_pytype
;
422 #define ECCURVE_PYCHECK(o) PyObject_TypeCheck((o), eccurve_pytype)
423 #define ECCURVE_C(o) (((eccurve_pyobj *)(o))->c)
424 #define ECCURVE_FOBJ(o) (((eccurve_pyobj *)(o))->fobj)
425 extern PyObject
*eccurve_pywrap(PyObject
*, ec_curve
*);
426 extern ec_curve
*eccurve_copy(ec_curve
*);
428 typedef struct ecpt_pyobj
{
434 extern PyTypeObject
*ecpt_pytype
, *ecptcurve_pytype
;
435 #define ECPT_PYCHECK(o) PyObject_TypeCheck((o), ecpt_pytype)
436 #define ECPTCURVE_PYCHECK(o) PyObject_TypeCheck((o), ecptcurve_pytype)
437 #define ECPT_C(o) (((ecpt_pyobj *)(o))->c)
438 #define ECPT_COBJ(o) ((PyObject *)(o)->ob_type)
439 #define ECPT_FOBJ(o) ECCURVE_FOBJ(ECPT_COBJ((o)))
440 #define ECPT_P(o) (&((ecpt_pyobj *)(o))->p)
441 extern PyObject
*ecpt_pywrap(PyObject
*, ec
*);
442 extern PyObject
*ecpt_pywrapout(void *, ec
*);
443 extern int toecpt(ec_curve
*, ec
*, PyObject
*);
444 extern int getecpt(ec_curve
*, ec
*, PyObject
*);
445 extern void getecptout(ec
*, PyObject
*);
446 extern int convecpt(PyObject
*, void *);
448 typedef struct ecinfo_pyobj
{
454 extern PyTypeObject
*ecinfo_pytype
;
455 #define ECINFO_PYCHECK(o) PyObject_TypeCheck((o), ecinfo_pytype)
456 #define ECINFO_EI(o) (&((ecinfo_pyobj *)(o))->ei)
457 #define ECINFO_COBJ(o) (((ecinfo_pyobj *)(o))->cobj)
458 extern void ecinfo_copy(ec_info
*, const ec_info
*);
459 extern PyObject
*ecinfo_pywrap(ec_info
*);
461 /*----- Cyclic groups -----------------------------------------------------*/
463 typedef struct ge_pyobj
{
469 extern PyTypeObject
*ge_pytype
;
470 #define GE_PYCHECK(o) PyObject_TypeCheck((o), ge_pytype)
471 #define GE_X(o) (((ge_pyobj *)(o))->x)
472 #define GE_G(o) (((ge_pyobj *)(o))->g)
473 #define GE_GOBJ(o) ((PyObject *)(group_pyobj *)(o)->ob_type)
474 extern PyObject
*ge_pywrap(PyObject
*, ge
*);
476 typedef struct group_pyobj
{
481 extern PyTypeObject
*group_pytype
;
482 #define GROUP_G(o) (((group_pyobj *)(o))->g)
483 extern PyObject
*group_pywrap(group
*);
484 extern group
*group_copy(group
*);
486 /*----- Random number generators ------------------------------------------*/
490 typedef struct grand_pyobj
{
496 extern PyTypeObject
*grand_pytype
;
497 extern PyObject
*rand_pyobj
;
498 #define GRAND_PYCHECK(o) PyObject_TypeCheck((o), grand_pytype)
499 #define GRAND_F(o) (((grand_pyobj *)(o))->f)
500 #define GRAND_R(o) (((grand_pyobj *)(o))->r)
501 extern PyObject
*grand_pywrap(grand
*, unsigned);
502 extern int convgrand(PyObject
*, void *);
504 /*----- Symmetric cryptography --------------------------------------------*/
506 extern PyObject
*keysz_pywrap(const octet
*);
508 extern int convgccipher(PyObject
*, void *);
509 extern PyObject
*gccipher_pywrap(gccipher
*);
511 typedef struct gchash_pyobj
{
516 extern PyTypeObject
*gchash_pytype
;
517 extern PyObject
*sha_pyobj
, *has160_pyobj
;
518 #define GCHASH_PYCHECK(o) PyObject_TypeCheck((o), gchash_pytype)
519 #define GCHASH_CH(o) (((gchash_pyobj *)(o))->ch)
520 extern PyObject
*ghash_pywrap(PyObject
*, ghash
*);
521 extern int convgchash(PyObject
*, void *);
522 extern int convghash(PyObject
*, void *);
524 extern int convgcmac(PyObject
*, void *);
526 /*----- Key generation ----------------------------------------------------*/
528 typedef struct pfilt_pyobj
{
534 extern PyTypeObject
*pfilt_pytype
;
535 #define PFILT_PYCHECK(o) PyObject_TypeCheck(o, pfilt_pytype)
536 #define PFILT_F(o) (&((pfilt_pyobj *)(o))->f)
537 #define PFILT_ST(o) (((pfilt_pyobj *)(o))->st)
539 typedef struct { pgen_proc
*proc
; void *ctx
; } pgev
;
540 #define PGEV_HEAD PyObject_HEAD pgev pg;
542 typedef struct pgev_pyobj
{
546 extern PyTypeObject
*pgev_pytype
;
547 #define PGEV_PYCHECK(o) PyObject_TypeCheck(o, pgev_pytype)
548 #define PGEV_PG(o) (&((pgev_pyobj *)(o))->pg)
550 typedef struct pypgev
{
556 extern int convpgev(PyObject
*, void *);
557 extern void droppgev(pypgev
*);
558 extern void pgenerr(struct excinfo
*exc
);
560 /*----- That's all, folks -------------------------------------------------*/