pock: Use floor division on integers.
[catacomb-python] / catacomb-python.h
1 /* -*-c-*-
2 *
3 * Definitions for Catacomb bindings
4 *
5 * (c) 2004 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the Python interface to Catacomb.
11 *
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.
16 *
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.
21 *
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.
25 */
26
27 #ifndef CATACOMB_PYTHON_H
28 #define CATACOMB_PYTHON_H
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 /*----- Header files ------------------------------------------------------*/
35
36 #include "pyke/pyke-mLib.h"
37
38 PUBLIC_SYMBOLS;
39 #include <longintrepr.h>
40
41 #include <mLib/dstr.h>
42 #include <mLib/macros.h>
43 #include <mLib/quis.h>
44 #include <mLib/unihash.h>
45
46 #include <catacomb/buf.h>
47 #include <catacomb/ct.h>
48
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>
60
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>
73
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>
82
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>
95
96 #include <catacomb/gf.h>
97 #include <catacomb/gfreduce.h>
98 #include <catacomb/gfn.h>
99
100 #include <catacomb/field.h>
101 #include <catacomb/field-guts.h>
102
103 #include <catacomb/ec.h>
104 #include <catacomb/ec-raw.h>
105 #include <catacomb/ectab.h>
106
107 #include <catacomb/group.h>
108 #include <catacomb/group-guts.h>
109
110 #include <catacomb/gdsa.h>
111 #include <catacomb/gkcdsa.h>
112 #include <catacomb/rsa.h>
113
114 #include <catacomb/key.h>
115 #include <catacomb/passphrase.h>
116 #include <catacomb/pixie.h>
117
118 #include <catacomb/share.h>
119 #include <catacomb/gfshare.h>
120 PRIVATE_SYMBOLS;
121
122 /*----- Miscellaneous preliminaries ---------------------------------------*/
123
124 /* Submodules. */
125 #define MODULES(_) \
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)
132
133 /* Exceptions. */
134 #define PGENERR(err) do { pgenerr(err); goto end; } while (0)
135
136 /* Conversions. */
137 extern int convmpw(PyObject *, void *);
138
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));
147
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 *));
155
156 /*----- Bytestrings -------------------------------------------------------*/
157
158 PyObject *bytestring_pywrap(const void *, size_t);
159 PyObject *bytestring_pywrapbuf(buf *);
160
161 /*----- Multiprecision arithmetic -----------------------------------------*/
162
163 typedef struct mp_pyobj {
164 PyObject_HEAD
165 mp *x;
166 } mp_pyobj;
167
168 extern PyTypeObject *mp_pytype;
169 extern PyTypeObject *gf_pytype;
170 #define MP_X(o) (((mp_pyobj *)(o))->x)
171 #define MP_PYCHECK(o) PyObject_TypeCheck((o), mp_pytype)
172 #define GF_PYCHECK(o) PyObject_TypeCheck((o), gf_pytype)
173
174 extern mp *mp_frompylong(PyObject *);
175 extern PyObject *mp_topylong(mp *);
176 extern mp *tomp(PyObject *);
177 extern mp *getmp(PyObject *);
178 extern int convmp(PyObject *, void *);
179 extern mp *getgf(PyObject *);
180 extern int convgf(PyObject *, void *);
181 extern PyObject *mp_pywrap(mp *);
182 extern PyObject *gf_pywrap(mp *);
183 extern Py_hash_t mphash(mp *);
184 extern mp *mp_frompyobject(PyObject *, int);
185 extern PyObject *mp_topystring(mp *, int,
186 const char *, const char *, const char *);
187 extern int mp_tolong_checked(mp *, long *, int);
188
189 /*----- Abstract fields ---------------------------------------------------*/
190
191 typedef struct field_pyobj {
192 PyHeapTypeObject ty;
193 field *f;
194 } field_pyobj;
195
196 extern PyTypeObject *field_pytype;
197 extern PyTypeObject *primefield_pytype;
198 extern PyTypeObject *niceprimefield_pytype;
199 extern PyTypeObject *binfield_pytype;
200 extern PyTypeObject *binpolyfield_pytype;
201 extern PyTypeObject *binnormfield_pytype;
202 #define FIELD_PYCHECK(o) PyObject_TypeCheck((o), field_pytype)
203 #define FIELD_F(o) (((field_pyobj *)(o))->f)
204 extern PyObject *field_pywrap(field *);
205 extern field *field_copy(field *);
206
207 typedef struct fe_pyobj {
208 PyObject_HEAD
209 field *f;
210 mp *x;
211 } fe_pyobj;
212
213 extern PyTypeObject *fe_pytype;
214 #define FE_PYCHECK(o) PyObject_TypeCheck((o), fe_pytype)
215 #define FE_F(o) (((fe_pyobj *)(o))->f)
216 #define FE_FOBJ(o) ((PyObject *)Py_TYPE(o))
217 #define FE_X(o) (((fe_pyobj *)(o))->x)
218 extern PyObject *fe_pywrap(PyObject *, mp *);
219
220 /*----- Elliptic curves ---------------------------------------------------*/
221
222 typedef struct eccurve_pyobj {
223 PyHeapTypeObject ty;
224 ec_curve *c;
225 PyObject *fobj;
226 } eccurve_pyobj;
227
228 extern PyTypeObject *eccurve_pytype;
229 extern PyTypeObject *ecprimecurve_pytype;
230 extern PyTypeObject *ecprimeprojcurve_pytype;
231 extern PyTypeObject *ecbincurve_pytype;
232 extern PyTypeObject *ecbinprojcurve_pytype;
233 #define ECCURVE_PYCHECK(o) PyObject_TypeCheck((o), eccurve_pytype)
234 #define ECCURVE_C(o) (((eccurve_pyobj *)(o))->c)
235 #define ECCURVE_FOBJ(o) (((eccurve_pyobj *)(o))->fobj)
236 extern PyObject *eccurve_pywrap(PyObject *, ec_curve *);
237 extern ec_curve *eccurve_copy(ec_curve *);
238
239 typedef struct ecpt_pyobj {
240 PyObject_HEAD
241 ec_curve *c;
242 ec p;
243 } ecpt_pyobj;
244
245 extern PyTypeObject *ecpt_pytype, *ecptcurve_pytype;
246 #define ECPT_PYCHECK(o) PyObject_TypeCheck((o), ecpt_pytype)
247 #define ECPTCURVE_PYCHECK(o) PyObject_TypeCheck((o), ecptcurve_pytype)
248 #define ECPT_C(o) (((ecpt_pyobj *)(o))->c)
249 #define ECPT_COBJ(o) ((PyObject *)Py_TYPE(o))
250 #define ECPT_FOBJ(o) ECCURVE_FOBJ(ECPT_COBJ((o)))
251 #define ECPT_P(o) (&((ecpt_pyobj *)(o))->p)
252 extern PyObject *ecpt_pywrap(PyObject *, ec *);
253 extern PyObject *ecpt_pywrapout(void *, ec *);
254 extern int toecpt(ec_curve *, ec *, PyObject *);
255 extern int getecpt(ec_curve *, ec *, PyObject *);
256 extern void getecptout(ec *, PyObject *);
257 extern int convecpt(PyObject *, void *);
258
259 typedef struct ecinfo_pyobj {
260 PyObject_HEAD
261 ec_info ei;
262 PyObject *cobj;
263 } ecinfo_pyobj;
264
265 extern PyTypeObject *ecinfo_pytype;
266 #define ECINFO_PYCHECK(o) PyObject_TypeCheck((o), ecinfo_pytype)
267 #define ECINFO_EI(o) (&((ecinfo_pyobj *)(o))->ei)
268 #define ECINFO_COBJ(o) (((ecinfo_pyobj *)(o))->cobj)
269 extern void ecinfo_copy(ec_info *, const ec_info *);
270 extern PyObject *ecinfo_pywrap(ec_info *);
271
272 /*----- Cyclic groups -----------------------------------------------------*/
273
274 typedef struct ge_pyobj {
275 PyObject_HEAD
276 ge *x;
277 group *g;
278 } ge_pyobj;
279
280 extern PyTypeObject *ge_pytype;
281 #define GE_PYCHECK(o) PyObject_TypeCheck((o), ge_pytype)
282 #define GE_X(o) (((ge_pyobj *)(o))->x)
283 #define GE_G(o) (((ge_pyobj *)(o))->g)
284 #define GE_GOBJ(o) (PyObject *)(Py_TYPE(o))
285 extern PyObject *ge_pywrap(PyObject *, ge *);
286
287 typedef struct group_pyobj {
288 PyHeapTypeObject ty;
289 group *g;
290 } group_pyobj;
291
292 extern PyTypeObject *group_pytype;
293 #define GROUP_G(o) (((group_pyobj *)(o))->g)
294 extern PyObject *group_pywrap(group *);
295 extern group *group_copy(group *);
296
297 /*----- Random number generators ------------------------------------------*/
298
299 #define f_freeme 1u
300
301 typedef struct grand_pyobj {
302 PyObject_HEAD
303 unsigned f;
304 grand *r;
305 } grand_pyobj;
306
307 extern PyTypeObject *grand_pytype;
308 extern PyObject *rand_pyobj;
309 #define GRAND_PYCHECK(o) PyObject_TypeCheck((o), grand_pytype)
310 #define GRAND_F(o) (((grand_pyobj *)(o))->f)
311 #define GRAND_R(o) (((grand_pyobj *)(o))->r)
312 extern PyObject *grand_pywrap(grand *, unsigned);
313 extern int convgrand(PyObject *, void *);
314
315 /*----- Symmetric cryptography --------------------------------------------*/
316
317 extern PyObject *keysz_pywrap(const octet *);
318
319 extern int convgccipher(PyObject *, void *);
320 extern PyObject *gccipher_pywrap(gccipher *);
321
322 typedef struct gchash_pyobj {
323 PyHeapTypeObject ty;
324 gchash *ch;
325 } gchash_pyobj;
326
327 extern PyTypeObject *gchash_pytype;
328 extern PyObject *sha_pyobj, *has160_pyobj;
329 #define GCHASH_PYCHECK(o) PyObject_TypeCheck((o), gchash_pytype)
330 #define GCHASH_CH(o) (((gchash_pyobj *)(o))->ch)
331 extern PyObject *ghash_pywrap(PyObject *, ghash *);
332 extern int convgchash(PyObject *, void *);
333 extern int convghash(PyObject *, void *);
334
335 extern int convgcmac(PyObject *, void *);
336
337 /*----- Key generation ----------------------------------------------------*/
338
339 typedef struct pfilt_pyobj {
340 PyObject_HEAD
341 pfilt f;
342 int st;
343 } pfilt_pyobj;
344
345 extern PyTypeObject *pfilt_pytype;
346 #define PFILT_PYCHECK(o) PyObject_TypeCheck(o, pfilt_pytype)
347 #define PFILT_F(o) (&((pfilt_pyobj *)(o))->f)
348 #define PFILT_ST(o) (((pfilt_pyobj *)(o))->st)
349
350 typedef struct { pgen_proc *proc; void *ctx; } pgev;
351 #define PGEV_HEAD PyObject_HEAD pgev pg;
352
353 typedef struct pgev_pyobj {
354 PGEV_HEAD
355 } pgev_pyobj;
356
357 extern PyTypeObject *pgev_pytype;
358 #define PGEV_PYCHECK(o) PyObject_TypeCheck(o, pgev_pytype)
359 #define PGEV_PG(o) (&((pgev_pyobj *)(o))->pg)
360
361 typedef struct pypgev {
362 pgev ev;
363 PyObject *obj;
364 struct excinfo *exc;
365 } pypgev;
366
367 extern int convpgev(PyObject *, void *);
368 extern void droppgev(pypgev *);
369 extern void pgenerr(struct excinfo *exc);
370
371 /*----- That's all, folks -------------------------------------------------*/
372
373 #ifdef __cplusplus
374 }
375 #endif
376
377 #endif