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/dstr.h>
43 #include <mLib/macros.h>
44 #include <mLib/quis.h>
45 #include <mLib/unihash.h>
47 #include <catacomb/buf.h>
48 #include <catacomb/ct.h>
50 #include <catacomb/grand.h>
51 #include <catacomb/rand.h>
52 #include <catacomb/noise.h>
53 #include <catacomb/bbs.h>
54 #include <catacomb/mprand.h>
55 #include <catacomb/lcrand.h>
56 #include <catacomb/fibrand.h>
57 #include <catacomb/dsarand.h>
58 #include <catacomb/sslprf.h>
59 #include <catacomb/tlsprf.h>
60 #include <catacomb/blkc.h>
62 #include <catacomb/gcipher.h>
63 #include <catacomb/gaead.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 /*----- Other preliminaries -----------------------------------------------*/
124 #define GOBBLE_SEMI extern int notexist
125 #if defined(__GNUC__) && defined(__ELF__)
126 # define PRIVATE_SYMBOLS _Pragma("GCC visibility push(hidden)") GOBBLE_SEMI
127 # define PUBLIC_SYMBOLS _Pragma("GCC visibility pop") GOBBLE_SEMI
128 # define EXPORT __attribute__((__visibility__("default")))
130 # define PRIVATE_SYMBOLS GOBBLE_SEMI
131 # define PUBLIC_SYMBOLS GOBBLE_SEMI
137 /*----- Utility macros ----------------------------------------------------*/
139 #define RETURN_OBJ(obj) do { Py_INCREF(obj); return (obj); } while (0)
140 #define RETURN_NONE RETURN_OBJ(Py_None)
141 #define RETURN_NOTIMPL RETURN_OBJ(Py_NotImplemented)
142 #define RETURN_TRUE RETURN_OBJ(Py_True)
143 #define RETURN_FALSE RETURN_OBJ(Py_False)
144 #define RETURN_ME RETURN_OBJ(me)
146 #define EXCERR(exc, str) do { \
147 PyErr_SetString(exc, str); \
150 #define VALERR(str) EXCERR(PyExc_ValueError, str)
151 #define OVFERR(str) EXCERR(PyExc_OverflowError, str)
152 #define TYERR(str) EXCERR(PyExc_TypeError, str)
153 #define IXERR(str) EXCERR(PyExc_IndexError, str)
154 #define ZDIVERR(str) EXCERR(PyExc_ZeroDivisionError, str)
155 #define SYSERR(str) EXCERR(PyExc_SystemError, str)
156 #define NIERR(str) EXCERR(PyExc_NotImplementedError, str)
157 #define INDEXERR(idx) do { \
158 PyErr_SetObject(PyExc_KeyError, idx); \
161 #define OSERR(name) do { \
162 PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); \
165 #define PGENERR(exc) do { pgenerr(exc); goto end; } while (0)
167 #define CONVFUNC(ty, cty, ext) \
168 int conv##ty(PyObject *o, void *p) \
170 if (!PyObject_TypeCheck(o, ty##_pytype)) \
171 TYERR("wanted a " #ty); \
172 *(cty *)p = ext(o); \
178 #define root_pytype 0
179 #define type_pytype &PyType_Type
180 #define INITTYPE_META(ty, base, meta) do { \
181 ty##_pytype_skel.tp_base = base##_pytype; \
182 ty##_pytype = inittype(&ty##_pytype_skel, meta##_pytype); \
184 #define INITTYPE(ty, base) INITTYPE_META(ty, base, type)
186 extern PyObject
*home_module
;
187 extern PyObject
*modname
;
189 #define INSERT(name, ob) do { \
190 PyObject *_o = (PyObject *)(ob); \
192 PyModule_AddObject(mod, name, _o); \
195 #define INSEXC(name, var, base, meth) \
196 INSERT(name, var = mkexc(mod, base, name, meth))
198 #define METH(func, doc) \
199 { #func, METHNAME(func), METH_VARARGS, doc },
200 #define KWMETH(func, doc) \
201 { #func, (PyCFunction)METHNAME(func), \
202 METH_VARARGS | METH_KEYWORDS, doc },
204 #define GET(func, doc) \
205 { #func, GETSETNAME(get, func), 0, doc },
206 #define GETSET(func, doc) \
207 { #func, GETSETNAME(get, func), GETSETNAME(set, func), doc },
209 #define MEMBER(name, ty, f, doc) \
210 { #name, ty, offsetof(MEMBERSTRUCT, name), f, doc },
214 _(bytestring) _(buffer) \
215 _(rand) _(algorithms) _(pubkey) _(pgen) \
216 _(mp) _(field) _(ec) _(group) \
217 _(passphrase) _(share) _(key)
218 #define DOMODINIT(m) m##_pyinit();
219 #define DOMODINSERT(m) m##_pyinsert(mod);
220 #define INIT_MODULES do { MODULES(DOMODINIT) } while (0)
221 #define INSERT_MODULES do { MODULES(DOMODINSERT) } while (0)
222 #define DECLARE_MODINIT(m) \
223 extern void m##_pyinit(void); \
224 extern void m##_pyinsert(PyObject *);
226 MODULES(DECLARE_MODINIT
)
228 #define FREEOBJ(obj) \
229 (((PyObject *)(obj))->ob_type->tp_free((PyObject *)(obj)))
231 #define GEN(func, base) \
232 static PyObject *func(void) \
234 PyObject *d = PyDict_New(); \
238 for (i = 0; g##base##tab[i]; i++) { \
239 o = gc##base##_pywrap((/*unconst*/ gc##base *)g##base##tab[i]); \
240 PyDict_SetItemString(d, \
241 (/*unconst*/ char *)g##base##tab[i]->name, \
248 #define KWLIST (/*unconst*/ char **)kwlist
250 struct nameval
{ const char *name
; unsigned f
; unsigned long value
; };
252 extern void setconstants(PyObject
*, const struct nameval
*);
254 extern PyObject
*mexp_common(PyObject
*, PyObject
*, size_t,
255 PyObject
*(*id
)(PyObject
*),
256 int (*fill
)(void *, PyObject
*,
257 PyObject
*, PyObject
*),
258 PyObject
*(*exp
)(PyObject
*, void *, int),
259 void (*drop
)(void *));
261 extern int convulong(PyObject
*, void *);
262 #define DECL_CONVU_(n) extern int convu##n(PyObject *, void *);
263 DOUINTSZ(DECL_CONVU_
)
264 extern int convmpw(PyObject
*, void *);
265 extern int convuint(PyObject
*, void *);
266 extern int convk64(PyObject
*, void *);
267 extern int convszt(PyObject
*, void *);
268 extern int convbool(PyObject
*, void *);
269 extern PyObject
*abstract_pynew(PyTypeObject
*, PyObject
*, PyObject
*);
270 extern PyObject
*getbool(int);
271 extern PyObject
*getulong(unsigned long);
272 extern PyObject
*getk64(kludge64
);
273 extern void *newtype(PyTypeObject
*, const PyTypeObject
*, const char *);
275 struct excinfo
{ PyObject
*ty
, *val
, *tb
; };
276 #define EXCINFO_INIT { 0, 0, 0 }
278 extern PyObject
*mkexc(PyObject
*, PyObject
*, const char *, PyMethodDef
*);
279 #define INIT_EXCINFO(exc) do { \
280 struct excinfo *_exc = (exc); _exc->ty = _exc->val = _exc->tb = 0; \
282 #define RELEASE_EXCINFO(exc) do { \
283 struct excinfo *_exc = (exc); \
284 Py_XDECREF(_exc->ty); _exc->ty = 0; \
285 Py_XDECREF(_exc->val); _exc->val = 0; \
286 Py_XDECREF(_exc->tb); _exc->tb = 0; \
288 #define STASH_EXCINFO(exc) do { \
289 struct excinfo *_exc = (exc); \
290 PyErr_Fetch(&_exc->ty, &_exc->val, &_exc->tb); \
291 PyErr_NormalizeException(&_exc->ty, &_exc->val, &_exc->tb); \
293 #define RESTORE_EXCINFO(exc) do { \
294 struct excinfo *_exc = (exc); \
295 PyErr_Restore(_exc->ty, _exc->val, _exc->tb); \
296 _exc->ty = _exc->val = _exc->tb = 0; \
298 extern void report_lost_exception(struct excinfo
*, const char *, ...);
299 extern void report_lost_exception_v(struct excinfo
*, const char *, va_list);
300 extern void stash_exception(struct excinfo
*, const char *, ...);
301 extern void restore_exception(struct excinfo
*, const char *, ...);
303 extern void typeready(PyTypeObject
*);
304 extern PyTypeObject
*inittype(PyTypeObject
*, PyTypeObject
*);
305 extern void addmethods(const PyMethodDef
*);
306 extern PyMethodDef
*donemethods(void);
308 /*----- Mapping methods ---------------------------------------------------*/
310 #define GMAP_METH(func, doc) { #func, gmapmeth_##func, METH_VARARGS, doc },
311 #define GMAP_KWMETH(func, doc) \
312 { #func, (PyCFunction)gmapmeth_##func, METH_VARARGS|METH_KEYWORDS, doc },
313 #define GMAP_METHDECL(func, doc) \
314 extern PyObject *gmapmeth_##func(PyObject *, PyObject *);
315 #define GMAP_KWMETHDECL(func, doc) \
316 extern PyObject *gmapmeth_##func(PyObject *, PyObject *, PyObject *);
318 #define GMAP_DOROMETHODS(METH, KWMETH) \
319 METH (has_key, "D.has_key(KEY) -> BOOL") \
320 METH (keys, "D.keys() -> LIST") \
321 METH (values, "D.values() -> LIST") \
322 METH (items, "D.items() -> LIST") \
323 METH (iterkeys, "D.iterkeys() -> ITER") \
324 METH (itervalues, "D.itervalues() -> ITER") \
325 METH (iteritems, "D.iteritems() -> ITER") \
326 KWMETH(get, "D.get(KEY, [default = None]) -> VALUE")
328 #define GMAP_DOMETHODS(METH, KWMETH) \
329 GMAP_DOROMETHODS(METH, KWMETH) \
330 METH (clear, "D.clear()") \
331 KWMETH(setdefault, "D.setdefault(K, [default = None]) -> VALUE") \
332 KWMETH(pop, "D.pop(KEY, [default = <error>]) -> VALUE") \
333 METH (popitem, "D.popitem() -> (KEY, VALUE)") \
334 METH (update, "D.update(MAP)")
336 GMAP_DOMETHODS(GMAP_METHDECL
, GMAP_KWMETHDECL
)
337 #define GMAP_ROMETHODS GMAP_DOROMETHODS(GMAP_METH, GMAP_KWMETH)
338 #define GMAP_METHODS GMAP_DOMETHODS(GMAP_METH, GMAP_KWMETH)
339 extern Py_ssize_t
gmap_pysize(PyObject
*);
340 extern PySequenceMethods gmap_pysequence
;
341 extern PyMethodDef gmap_pymethods
[];
343 /*----- Bytestrings -------------------------------------------------------*/
345 PyObject
*bytestring_pywrap(const void *, size_t);
346 PyObject
*bytestring_pywrapbuf(buf
*);
348 /*----- Multiprecision arithmetic -----------------------------------------*/
350 typedef struct mp_pyobj
{
355 extern PyTypeObject
*mp_pytype
;
356 extern PyTypeObject
*gf_pytype
;
357 #define MP_X(o) (((mp_pyobj *)(o))->x)
358 #define MP_PYCHECK(o) PyObject_TypeCheck((o), mp_pytype)
359 #define GF_PYCHECK(o) PyObject_TypeCheck((o), gf_pytype)
361 extern mp
*mp_frompylong(PyObject
*);
362 extern PyObject
*mp_topylong(mp
*);
363 extern mp
*tomp(PyObject
*);
364 extern mp
*getmp(PyObject
*);
365 extern int convmp(PyObject
*, void *);
366 extern mp
*getgf(PyObject
*);
367 extern int convgf(PyObject
*, void *);
368 extern PyObject
*mp_pywrap(mp
*);
369 extern PyObject
*gf_pywrap(mp
*);
370 extern long mphash(mp
*);
371 extern mp
*mp_frompyobject(PyObject
*, int);
372 extern PyObject
*mp_topystring(mp
*, int,
373 const char *, const char *, const char *);
374 extern int mp_tolong_checked(mp
*, long *, int);
376 /*----- Abstract fields ---------------------------------------------------*/
378 typedef struct field_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 typedef struct fe_pyobj
{
400 extern PyTypeObject
*fe_pytype
;
401 #define FE_PYCHECK(o) PyObject_TypeCheck((o), fe_pytype)
402 #define FE_F(o) (((fe_pyobj *)(o))->f)
403 #define FE_FOBJ(o) ((PyObject *)(o)->ob_type)
404 #define FE_X(o) (((fe_pyobj *)(o))->x)
405 extern PyObject
*fe_pywrap(PyObject
*, mp
*);
407 /*----- Elliptic curves ---------------------------------------------------*/
409 typedef struct eccurve_pyobj
{
415 extern PyTypeObject
*eccurve_pytype
;
416 extern PyTypeObject
*ecprimecurve_pytype
;
417 extern PyTypeObject
*ecprimeprojcurve_pytype
;
418 extern PyTypeObject
*ecbincurve_pytype
;
419 extern PyTypeObject
*ecbinprojcurve_pytype
;
420 #define ECCURVE_PYCHECK(o) PyObject_TypeCheck((o), eccurve_pytype)
421 #define ECCURVE_C(o) (((eccurve_pyobj *)(o))->c)
422 #define ECCURVE_FOBJ(o) (((eccurve_pyobj *)(o))->fobj)
423 extern PyObject
*eccurve_pywrap(PyObject
*, ec_curve
*);
424 extern ec_curve
*eccurve_copy(ec_curve
*);
426 typedef struct ecpt_pyobj
{
432 extern PyTypeObject
*ecpt_pytype
, *ecptcurve_pytype
;
433 #define ECPT_PYCHECK(o) PyObject_TypeCheck((o), ecpt_pytype)
434 #define ECPTCURVE_PYCHECK(o) PyObject_TypeCheck((o), ecptcurve_pytype)
435 #define ECPT_C(o) (((ecpt_pyobj *)(o))->c)
436 #define ECPT_COBJ(o) ((PyObject *)(o)->ob_type)
437 #define ECPT_FOBJ(o) ECCURVE_FOBJ(ECPT_COBJ((o)))
438 #define ECPT_P(o) (&((ecpt_pyobj *)(o))->p)
439 extern PyObject
*ecpt_pywrap(PyObject
*, ec
*);
440 extern PyObject
*ecpt_pywrapout(void *, ec
*);
441 extern int toecpt(ec_curve
*, ec
*, PyObject
*);
442 extern int getecpt(ec_curve
*, ec
*, PyObject
*);
443 extern void getecptout(ec
*, PyObject
*);
444 extern int convecpt(PyObject
*, void *);
446 typedef struct ecinfo_pyobj
{
452 extern PyTypeObject
*ecinfo_pytype
;
453 #define ECINFO_PYCHECK(o) PyObject_TypeCheck((o), ecinfo_pytype)
454 #define ECINFO_EI(o) (&((ecinfo_pyobj *)(o))->ei)
455 #define ECINFO_COBJ(o) (((ecinfo_pyobj *)(o))->cobj)
456 extern void ecinfo_copy(ec_info
*, const ec_info
*);
457 extern PyObject
*ecinfo_pywrap(ec_info
*);
459 /*----- Cyclic groups -----------------------------------------------------*/
461 typedef struct ge_pyobj
{
467 extern PyTypeObject
*ge_pytype
;
468 #define GE_PYCHECK(o) PyObject_TypeCheck((o), ge_pytype)
469 #define GE_X(o) (((ge_pyobj *)(o))->x)
470 #define GE_G(o) (((ge_pyobj *)(o))->g)
471 #define GE_GOBJ(o) ((PyObject *)(group_pyobj *)(o)->ob_type)
472 extern PyObject
*ge_pywrap(PyObject
*, ge
*);
474 typedef struct group_pyobj
{
479 extern PyTypeObject
*group_pytype
;
480 #define GROUP_G(o) (((group_pyobj *)(o))->g)
481 extern PyObject
*group_pywrap(group
*);
482 extern group
*group_copy(group
*);
484 /*----- Random number generators ------------------------------------------*/
488 typedef struct grand_pyobj
{
494 extern PyTypeObject
*grand_pytype
;
495 extern PyObject
*rand_pyobj
;
496 #define GRAND_PYCHECK(o) PyObject_TypeCheck((o), grand_pytype)
497 #define GRAND_F(o) (((grand_pyobj *)(o))->f)
498 #define GRAND_R(o) (((grand_pyobj *)(o))->r)
499 extern PyObject
*grand_pywrap(grand
*, unsigned);
500 extern int convgrand(PyObject
*, void *);
502 /*----- Symmetric cryptography --------------------------------------------*/
504 extern PyObject
*keysz_pywrap(const octet
*);
506 extern int convgccipher(PyObject
*, void *);
507 extern PyObject
*gccipher_pywrap(gccipher
*);
509 typedef struct gchash_pyobj
{
514 extern PyTypeObject
*gchash_pytype
;
515 extern PyObject
*sha_pyobj
, *has160_pyobj
;
516 #define GCHASH_PYCHECK(o) PyObject_TypeCheck((o), gchash_pytype)
517 #define GCHASH_CH(o) (((gchash_pyobj *)(o))->ch)
518 extern PyObject
*ghash_pywrap(PyObject
*, ghash
*);
519 extern int convgchash(PyObject
*, void *);
520 extern int convghash(PyObject
*, void *);
522 extern int convgcmac(PyObject
*, void *);
524 /*----- Key generation ----------------------------------------------------*/
526 typedef struct pfilt_pyobj
{
532 extern PyTypeObject
*pfilt_pytype
;
533 #define PFILT_PYCHECK(o) PyObject_TypeCheck(o, pfilt_pytype)
534 #define PFILT_F(o) (&((pfilt_pyobj *)(o))->f)
535 #define PFILT_ST(o) (((pfilt_pyobj *)(o))->st)
537 typedef struct { pgen_proc
*proc
; void *ctx
; } pgev
;
538 #define PGEV_HEAD PyObject_HEAD pgev pg;
540 typedef struct pgev_pyobj
{
544 extern PyTypeObject
*pgev_pytype
;
545 #define PGEV_PYCHECK(o) PyObject_TypeCheck(o, pgev_pytype)
546 #define PGEV_PG(o) (&((pgev_pyobj *)(o))->pg)
548 typedef struct pypgev
{
554 extern int convpgev(PyObject
*, void *);
555 extern void droppgev(pypgev
*);
556 extern void pgenerr(struct excinfo
*exc
);
558 /*----- That's all, folks -------------------------------------------------*/