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 #include "pyke/pyke-mLib.h"
39 #include <longintrepr.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>
47 #include <catacomb/ct.h>
49 #include <catacomb/grand.h>
50 #include <catacomb/rand.h>
51 #include <catacomb/noise.h>
52 #include <catacomb/bbs.h>
53 #include <catacomb/mprand.h>
54 #include <catacomb/lcrand.h>
55 #include <catacomb/fibrand.h>
56 #include <catacomb/dsarand.h>
57 #include <catacomb/sslprf.h>
58 #include <catacomb/tlsprf.h>
59 #include <catacomb/blkc.h>
61 #include <catacomb/gcipher.h>
62 #include <catacomb/gaead.h>
63 #include <catacomb/ghash.h>
64 #include <catacomb/gmac.h>
65 #include <catacomb/md5.h>
66 #include <catacomb/md5-hmac.h>
67 #include <catacomb/poly1305.h>
68 #include <catacomb/sha.h>
69 #include <catacomb/sha-mgf.h>
70 #include <catacomb/sha-hmac.h>
71 #include <catacomb/keccak1600.h>
72 #include <catacomb/sha3.h>
74 #include <catacomb/mp.h>
75 #include <catacomb/mpint.h>
76 #include <catacomb/mpmul.h>
77 #include <catacomb/mpcrt.h>
78 #include <catacomb/mpmont.h>
79 #include <catacomb/mpbarrett.h>
80 #include <catacomb/mpreduce.h>
81 #include <catacomb/mp-fibonacci.h>
83 #include <catacomb/pgen.h>
84 #include <catacomb/pfilt.h>
85 #include <catacomb/strongprime.h>
86 #include <catacomb/limlee.h>
87 #include <catacomb/dh.h>
88 #include <catacomb/ptab.h>
89 #include <catacomb/bintab.h>
90 #include <catacomb/dsa.h>
91 #include <catacomb/x25519.h>
92 #include <catacomb/x448.h>
93 #include <catacomb/ed25519.h>
94 #include <catacomb/ed448.h>
96 #include <catacomb/gf.h>
97 #include <catacomb/gfreduce.h>
98 #include <catacomb/gfn.h>
100 #include <catacomb/field.h>
101 #include <catacomb/field-guts.h>
103 #include <catacomb/ec.h>
104 #include <catacomb/ec-raw.h>
105 #include <catacomb/ectab.h>
107 #include <catacomb/group.h>
108 #include <catacomb/group-guts.h>
110 #include <catacomb/gdsa.h>
111 #include <catacomb/gkcdsa.h>
112 #include <catacomb/rsa.h>
114 #include <catacomb/key.h>
115 #include <catacomb/passphrase.h>
116 #include <catacomb/pixie.h>
118 #include <catacomb/share.h>
119 #include <catacomb/gfshare.h>
122 /*----- Miscellaneous preliminaries ---------------------------------------*/
126 _(pyke_core) _(pyke_gmap) \
127 _(bytestring) _(buffer) \
128 _(rand) _(algorithms) _(pubkey) _(pgen) \
129 _(mp) _(field) _(ec) _(group) \
130 _(passphrase) _(share) _(key)
131 MODULES(DECLARE_MODINIT
)
134 #define PGENERR(err) do { pgenerr(err); goto end; } while (0)
137 extern int convmpw(PyObject
*, void *);
139 /* Building tables of things. */
140 extern PyObject
*make_algtab(const void *tab
, size_t esz
,
141 const char *(*namefn
)(const void *),
142 PyObject
*(*valfn
)(const void *));
143 extern PyObject
*make_grouptab(const void *tab
, size_t esz
,
144 const char *(*namefn
)(const void *),
145 int (*ixfn
)(const void *),
146 PyObject
*(*valfn
)(int));
148 /* Common handling for simultaneous exponentiation. */
149 extern PyObject
*mexp_common(PyObject
*, PyObject
*, size_t,
150 PyObject
*(*id
)(PyObject
*),
151 int (*fill
)(void *, PyObject
*,
152 PyObject
*, PyObject
*),
153 PyObject
*(*exp
)(PyObject
*, void *, int),
154 void (*drop
)(void *));
156 /*----- Bytestrings -------------------------------------------------------*/
158 PyObject
*bytestring_pywrap(const void *, size_t);
159 PyObject
*bytestring_pywrapbuf(buf
*);
161 /*----- Buffers -----------------------------------------------------------*/
163 typedef struct buf_pyobj
{
170 extern PyTypeObject
*rbuf_pytype
, *wbuf_pytype
;
171 #define RBUF_PYCHECK(o) PyObject_TypeCheck((o), rbuf_pytype)
172 #define WBUF_PYCHECK(o) PyObject_TypeCheck((o), wbuf_pytype)
173 #define BUF_B(o) (&((buf_pyobj *)(o))->b)
174 #define BUF_SUB(o) (((buf_pyobj *)(o))->sub)
175 #define BUF_LK(o) (((buf_pyobj *)(o))->lk)
177 extern PyObject
*buferr
;
178 #define BUFERR(str) do { PyErr_SetString(buferr, str); goto end; } while (0)
180 extern int ensurebuf(PyObject
*, size_t);
182 /*----- Multiprecision arithmetic -----------------------------------------*/
184 typedef struct mp_pyobj
{
189 extern PyTypeObject
*mp_pytype
;
190 extern PyTypeObject
*gf_pytype
;
191 #define MP_X(o) (((mp_pyobj *)(o))->x)
192 #define MP_PYCHECK(o) PyObject_TypeCheck((o), mp_pytype)
193 #define GF_PYCHECK(o) PyObject_TypeCheck((o), gf_pytype)
195 extern mp
*mp_frompylong(PyObject
*);
196 extern PyObject
*mp_topylong(mp
*);
197 extern mp
*tomp(PyObject
*);
198 extern mp
*implicitmp(PyObject
*);
199 extern mp
*getmp(PyObject
*);
200 extern int convmp(PyObject
*, void *);
201 extern mp
*implicitgf(PyObject
*);
202 extern mp
*getgf(PyObject
*);
203 extern int convgf(PyObject
*, void *);
204 extern PyObject
*mp_pywrap(mp
*);
205 extern PyObject
*gf_pywrap(mp
*);
206 extern Py_hash_t
mphash(mp
*);
207 extern mp
*mp_frompyobject(PyObject
*, int);
208 extern PyObject
*mp_topystring(mp
*, int,
209 const char *, const char *, const char *);
210 extern int mp_tolong_checked(mp
*, long *, int);
212 /*----- Abstract fields ---------------------------------------------------*/
214 typedef struct field_pyobj
{
219 extern PyTypeObject
*field_pytype
;
220 extern PyTypeObject
*primefield_pytype
;
221 extern PyTypeObject
*niceprimefield_pytype
;
222 extern PyTypeObject
*binfield_pytype
;
223 extern PyTypeObject
*binpolyfield_pytype
;
224 extern PyTypeObject
*binnormfield_pytype
;
225 #define FIELD_PYCHECK(o) PyObject_TypeCheck((o), field_pytype)
226 #define FIELD_F(o) (((field_pyobj *)(o))->f)
227 extern PyObject
*field_pywrap(field
*);
228 extern field
*field_copy(field
*);
230 typedef struct fe_pyobj
{
236 extern PyTypeObject
*fe_pytype
;
237 #define FE_PYCHECK(o) PyObject_TypeCheck((o), fe_pytype)
238 #define FE_F(o) (((fe_pyobj *)(o))->f)
239 #define FE_FOBJ(o) ((PyObject *)Py_TYPE(o))
240 #define FE_X(o) (((fe_pyobj *)(o))->x)
241 extern PyObject
*fe_pywrap(PyObject
*, mp
*);
243 /*----- Elliptic curves ---------------------------------------------------*/
245 typedef struct eccurve_pyobj
{
251 extern PyTypeObject
*eccurve_pytype
;
252 extern PyTypeObject
*ecprimecurve_pytype
;
253 extern PyTypeObject
*ecprimeprojcurve_pytype
;
254 extern PyTypeObject
*ecbincurve_pytype
;
255 extern PyTypeObject
*ecbinprojcurve_pytype
;
256 #define ECCURVE_PYCHECK(o) PyObject_TypeCheck((o), eccurve_pytype)
257 #define ECCURVE_C(o) (((eccurve_pyobj *)(o))->c)
258 #define ECCURVE_FOBJ(o) (((eccurve_pyobj *)(o))->fobj)
259 extern PyObject
*eccurve_pywrap(PyObject
*, ec_curve
*);
260 extern ec_curve
*eccurve_copy(ec_curve
*);
262 typedef struct ecpt_pyobj
{
268 extern PyTypeObject
*ecpt_pytype
, *ecptcurve_pytype
;
269 #define ECPT_PYCHECK(o) PyObject_TypeCheck((o), ecpt_pytype)
270 #define ECPTCURVE_PYCHECK(o) PyObject_TypeCheck((o), ecptcurve_pytype)
271 #define ECPT_C(o) (((ecpt_pyobj *)(o))->c)
272 #define ECPT_COBJ(o) ((PyObject *)Py_TYPE(o))
273 #define ECPT_FOBJ(o) ECCURVE_FOBJ(ECPT_COBJ((o)))
274 #define ECPT_P(o) (&((ecpt_pyobj *)(o))->p)
275 extern PyObject
*ecpt_pywrap(PyObject
*, ec
*);
276 extern PyObject
*ecpt_pywrapout(void *, ec
*);
277 extern int toecpt(ec_curve
*, ec
*, PyObject
*);
278 extern int getecpt(ec_curve
*, ec
*, PyObject
*);
279 extern void getecptout(ec
*, PyObject
*);
280 extern int convecpt(PyObject
*, void *);
282 typedef struct ecinfo_pyobj
{
288 extern PyTypeObject
*ecinfo_pytype
;
289 #define ECINFO_PYCHECK(o) PyObject_TypeCheck((o), ecinfo_pytype)
290 #define ECINFO_EI(o) (&((ecinfo_pyobj *)(o))->ei)
291 #define ECINFO_COBJ(o) (((ecinfo_pyobj *)(o))->cobj)
292 extern void ecinfo_copy(ec_info
*, const ec_info
*);
293 extern PyObject
*ecinfo_pywrap(ec_info
*);
295 /*----- Cyclic groups -----------------------------------------------------*/
297 typedef struct ge_pyobj
{
303 extern PyTypeObject
*ge_pytype
;
304 #define GE_PYCHECK(o) PyObject_TypeCheck((o), ge_pytype)
305 #define GE_X(o) (((ge_pyobj *)(o))->x)
306 #define GE_G(o) (((ge_pyobj *)(o))->g)
307 #define GE_GOBJ(o) (PyObject *)(Py_TYPE(o))
308 extern PyObject
*ge_pywrap(PyObject
*, ge
*);
310 typedef struct group_pyobj
{
315 extern PyTypeObject
*group_pytype
;
316 #define GROUP_G(o) (((group_pyobj *)(o))->g)
317 extern PyObject
*group_pywrap(group
*);
318 extern group
*group_copy(group
*);
320 /*----- Random number generators ------------------------------------------*/
324 typedef struct grand_pyobj
{
330 extern PyTypeObject
*grand_pytype
;
331 extern PyObject
*rand_pyobj
;
332 #define GRAND_PYCHECK(o) PyObject_TypeCheck((o), grand_pytype)
333 #define GRAND_F(o) (((grand_pyobj *)(o))->f)
334 #define GRAND_R(o) (((grand_pyobj *)(o))->r)
335 extern PyObject
*grand_pywrap(grand
*, unsigned);
336 extern int convgrand(PyObject
*, void *);
338 /*----- Symmetric cryptography --------------------------------------------*/
340 extern PyObject
*keysz_pywrap(const octet
*);
342 extern int convgccipher(PyObject
*, void *);
343 extern PyObject
*gccipher_pywrap(gccipher
*);
345 typedef struct gchash_pyobj
{
350 extern PyTypeObject
*gchash_pytype
;
351 extern PyObject
*sha_pyobj
, *has160_pyobj
;
352 #define GCHASH_PYCHECK(o) PyObject_TypeCheck((o), gchash_pytype)
353 #define GCHASH_CH(o) (((gchash_pyobj *)(o))->ch)
354 extern PyObject
*ghash_pywrap(PyObject
*, ghash
*);
355 extern int convgchash(PyObject
*, void *);
356 extern int convghash(PyObject
*, void *);
358 extern int convgcmac(PyObject
*, void *);
360 /*----- Key generation ----------------------------------------------------*/
362 typedef struct pfilt_pyobj
{
368 extern PyTypeObject
*pfilt_pytype
;
369 #define PFILT_PYCHECK(o) PyObject_TypeCheck(o, pfilt_pytype)
370 #define PFILT_F(o) (&((pfilt_pyobj *)(o))->f)
371 #define PFILT_ST(o) (((pfilt_pyobj *)(o))->st)
373 typedef struct { pgen_proc
*proc
; void *ctx
; } pgev
;
374 #define PGEV_HEAD PyObject_HEAD pgev pg;
376 typedef struct pgev_pyobj
{
380 extern PyTypeObject
*pgev_pytype
;
381 #define PGEV_PYCHECK(o) PyObject_TypeCheck(o, pgev_pytype)
382 #define PGEV_PG(o) (&((pgev_pyobj *)(o))->pg)
384 typedef struct pypgev
{
390 extern int convpgev(PyObject
*, void *);
391 extern void droppgev(pypgev
*);
392 extern void pgenerr(struct excinfo
*exc
);
394 /*----- That's all, folks -------------------------------------------------*/