Commit | Line | Data |
---|---|---|
d7ab1bab | 1 | /* -*-c-*- |
d7ab1bab | 2 | * |
3 | * Definitions for Catacomb bindings | |
4 | * | |
5 | * (c) 2004 Straylight/Edgeware | |
6 | */ | |
7 | ||
b2687a0a | 8 | /*----- Licensing notice --------------------------------------------------* |
d7ab1bab | 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. | |
b2687a0a | 16 | * |
d7ab1bab | 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. | |
b2687a0a | 21 | * |
d7ab1bab | 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 | ||
6b54260d MW |
36 | #define PY_SSIZE_T_CLEAN |
37 | ||
d7ab1bab | 38 | #include <Python.h> |
39 | #include <longintrepr.h> | |
40 | #include <structmember.h> | |
41 | ||
d7ab1bab | 42 | #include <mLib/dstr.h> |
43 | #include <mLib/macros.h> | |
46e6ad89 | 44 | #include <mLib/quis.h> |
6d481bc6 | 45 | #include <mLib/unihash.h> |
d7ab1bab | 46 | |
47 | #include <catacomb/buf.h> | |
bfb450cc | 48 | #include <catacomb/ct.h> |
d7ab1bab | 49 | |
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> | |
03ed9abb | 60 | #include <catacomb/blkc.h> |
d7ab1bab | 61 | |
62 | #include <catacomb/gcipher.h> | |
bebf03ab | 63 | #include <catacomb/gaead.h> |
d7ab1bab | 64 | #include <catacomb/ghash.h> |
65 | #include <catacomb/gmac.h> | |
66 | #include <catacomb/md5.h> | |
67 | #include <catacomb/md5-hmac.h> | |
204d480b | 68 | #include <catacomb/poly1305.h> |
d7ab1bab | 69 | #include <catacomb/sha.h> |
70 | #include <catacomb/sha-mgf.h> | |
71 | #include <catacomb/sha-hmac.h> | |
b35fdbe6 | 72 | #include <catacomb/keccak1600.h> |
6bd22b53 | 73 | #include <catacomb/sha3.h> |
d7ab1bab | 74 | |
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> | |
6313f40e | 82 | #include <catacomb/mp-fibonacci.h> |
d7ab1bab | 83 | |
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> | |
848ba392 | 92 | #include <catacomb/x25519.h> |
eb8aa4ec | 93 | #include <catacomb/x448.h> |
dafb2da4 | 94 | #include <catacomb/ed25519.h> |
eee202c3 | 95 | #include <catacomb/ed448.h> |
d7ab1bab | 96 | |
97 | #include <catacomb/gf.h> | |
98 | #include <catacomb/gfreduce.h> | |
99 | #include <catacomb/gfn.h> | |
100 | ||
101 | #include <catacomb/field.h> | |
102 | #include <catacomb/field-guts.h> | |
103 | ||
104 | #include <catacomb/ec.h> | |
105 | #include <catacomb/ec-raw.h> | |
106 | #include <catacomb/ectab.h> | |
107 | ||
108 | #include <catacomb/group.h> | |
109 | #include <catacomb/group-guts.h> | |
110 | ||
111 | #include <catacomb/gdsa.h> | |
112 | #include <catacomb/gkcdsa.h> | |
113 | #include <catacomb/rsa.h> | |
114 | ||
115 | #include <catacomb/key.h> | |
116 | #include <catacomb/passphrase.h> | |
117 | #include <catacomb/pixie.h> | |
118 | ||
46e6ad89 | 119 | #include <catacomb/share.h> |
120 | #include <catacomb/gfshare.h> | |
121 | ||
6640e060 MW |
122 | /*----- Other preliminaries -----------------------------------------------*/ |
123 | ||
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"))) | |
129 | #else | |
130 | # define PRIVATE_SYMBOLS GOBBLE_SEMI | |
131 | # define PUBLIC_SYMBOLS GOBBLE_SEMI | |
132 | # define EXPORT | |
133 | #endif | |
134 | ||
135 | PRIVATE_SYMBOLS; | |
136 | ||
d7ab1bab | 137 | /*----- Utility macros ----------------------------------------------------*/ |
138 | ||
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) | |
145 | ||
146 | #define EXCERR(exc, str) do { \ | |
147 | PyErr_SetString(exc, str); \ | |
148 | goto end; \ | |
149 | } while (0) | |
150 | #define VALERR(str) EXCERR(PyExc_ValueError, str) | |
e9e067d4 | 151 | #define OVFERR(str) EXCERR(PyExc_OverflowError, str) |
d7ab1bab | 152 | #define TYERR(str) EXCERR(PyExc_TypeError, str) |
bb5c23a4 | 153 | #define IXERR(str) EXCERR(PyExc_IndexError, str) |
d7ab1bab | 154 | #define ZDIVERR(str) EXCERR(PyExc_ZeroDivisionError, str) |
d7ab1bab | 155 | #define SYSERR(str) EXCERR(PyExc_SystemError, str) |
11cb3d97 | 156 | #define NIERR(str) EXCERR(PyExc_NotImplementedError, str) |
46e6ad89 | 157 | #define INDEXERR(idx) do { \ |
158 | PyErr_SetObject(PyExc_KeyError, idx); \ | |
159 | goto end; \ | |
160 | } while (0) | |
d7ab1bab | 161 | #define OSERR(name) do { \ |
162 | PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); \ | |
163 | goto end; \ | |
164 | } while (0) | |
930d78e3 | 165 | #define PGENERR(exc) do { pgenerr(exc); goto end; } while (0) |
d7ab1bab | 166 | |
167 | #define CONVFUNC(ty, cty, ext) \ | |
168 | int conv##ty(PyObject *o, void *p) \ | |
169 | { \ | |
170 | if (!PyObject_TypeCheck(o, ty##_pytype)) \ | |
171 | TYERR("wanted a " #ty); \ | |
172 | *(cty *)p = ext(o); \ | |
173 | return (1); \ | |
174 | end: \ | |
175 | return (0); \ | |
176 | } | |
177 | ||
178 | #define root_pytype 0 | |
179 | #define type_pytype &PyType_Type | |
76ca8edd | 180 | #define INITTYPE_META(ty, base, meta) do { \ |
d7ab1bab | 181 | ty##_pytype_skel.tp_base = base##_pytype; \ |
76ca8edd | 182 | ty##_pytype = inittype(&ty##_pytype_skel, meta##_pytype); \ |
d7ab1bab | 183 | } while (0) |
76ca8edd | 184 | #define INITTYPE(ty, base) INITTYPE_META(ty, base, type) |
d7ab1bab | 185 | |
930d78e3 | 186 | extern PyObject *home_module; |
c5236b64 | 187 | extern PyObject *modname; |
930d78e3 MW |
188 | |
189 | #define INSERT(name, ob) do { \ | |
d7ab1bab | 190 | PyObject *_o = (PyObject *)(ob); \ |
b2687a0a | 191 | Py_INCREF(_o); \ |
d7ab1bab | 192 | PyModule_AddObject(mod, name, _o); \ |
193 | } while (0) | |
194 | ||
46e6ad89 | 195 | #define INSEXC(name, var, base, meth) \ |
196 | INSERT(name, var = mkexc(mod, base, name, meth)) | |
197 | ||
d7ab1bab | 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 }, | |
203 | ||
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 }, | |
208 | ||
209 | #define MEMBER(name, ty, f, doc) \ | |
210 | { #name, ty, offsetof(MEMBERSTRUCT, name), f, doc }, | |
211 | ||
46e6ad89 | 212 | #define MODULES(_) \ |
0b1b92bd | 213 | _(util) \ |
46e6ad89 | 214 | _(bytestring) _(buffer) \ |
215 | _(rand) _(algorithms) _(pubkey) _(pgen) \ | |
216 | _(mp) _(field) _(ec) _(group) \ | |
0b1b92bd | 217 | _(passphrase) _(share) _(key) |
d7ab1bab | 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 | ||
223 | #define DO(m) \ | |
224 | extern void m##_pyinit(void); \ | |
225 | extern void m##_pyinsert(PyObject *); | |
226 | MODULES(DO) | |
227 | #undef DO | |
228 | ||
3aa33042 | 229 | #define FREEOBJ(obj) \ |
230 | (((PyObject *)(obj))->ob_type->tp_free((PyObject *)(obj))) | |
231 | ||
11cb3d97 MW |
232 | #define GEN(func, base) \ |
233 | static PyObject *func(void) \ | |
234 | { \ | |
235 | PyObject *d = PyDict_New(); \ | |
236 | PyObject *o; \ | |
237 | int i; \ | |
238 | \ | |
239 | for (i = 0; g##base##tab[i]; i++) { \ | |
240 | o = gc##base##_pywrap((/*unconst*/ gc##base *)g##base##tab[i]); \ | |
241 | PyDict_SetItemString(d, \ | |
242 | (/*unconst*/ char *)g##base##tab[i]->name, \ | |
243 | o); \ | |
244 | Py_DECREF(o); \ | |
245 | } \ | |
246 | return (d); \ | |
247 | } | |
248 | ||
827f89d7 MW |
249 | #define KWLIST (/*unconst*/ char **)kwlist |
250 | ||
9044246b MW |
251 | struct nameval { const char *name; unsigned f; unsigned long value; }; |
252 | #define CF_SIGNED 1u | |
11cb3d97 MW |
253 | extern void setconstants(PyObject *, const struct nameval *); |
254 | ||
255 | extern PyObject *mexp_common(PyObject *, PyObject *, size_t, | |
256 | PyObject *(*id)(PyObject *), | |
257 | int (*fill)(void *, PyObject *, | |
258 | PyObject *, PyObject *), | |
259 | PyObject *(*exp)(PyObject *, void *, int), | |
260 | void (*drop)(void *)); | |
261 | ||
262 | extern int convulong(PyObject *, void *); | |
263 | #define DECL_CONVU_(n) extern int convu##n(PyObject *, void *); | |
264 | DOUINTSZ(DECL_CONVU_) | |
265 | extern int convmpw(PyObject *, void *); | |
266 | extern int convuint(PyObject *, void *); | |
9f9ea9ea | 267 | extern int convk64(PyObject *, void *); |
11cb3d97 MW |
268 | extern int convszt(PyObject *, void *); |
269 | extern int convbool(PyObject *, void *); | |
270 | extern PyObject *abstract_pynew(PyTypeObject *, PyObject *, PyObject *); | |
271 | extern PyObject *getbool(int); | |
fbca05a1 | 272 | extern PyObject *getulong(unsigned long); |
9f9ea9ea | 273 | extern PyObject *getk64(kludge64); |
11cb3d97 MW |
274 | extern void *newtype(PyTypeObject *, const PyTypeObject *, const char *); |
275 | ||
930d78e3 MW |
276 | struct excinfo { PyObject *ty, *val, *tb; }; |
277 | #define EXCINFO_INIT { 0, 0, 0 } | |
278 | ||
ef5675cb | 279 | extern PyObject *mkexc(PyObject *, PyObject *, const char *, PyMethodDef *); |
930d78e3 MW |
280 | #define INIT_EXCINFO(exc) do { \ |
281 | struct excinfo *_exc = (exc); _exc->ty = _exc->val = _exc->tb = 0; \ | |
282 | } while (0) | |
283 | #define RELEASE_EXCINFO(exc) do { \ | |
284 | struct excinfo *_exc = (exc); \ | |
285 | Py_XDECREF(_exc->ty); _exc->ty = 0; \ | |
286 | Py_XDECREF(_exc->val); _exc->val = 0; \ | |
287 | Py_XDECREF(_exc->tb); _exc->tb = 0; \ | |
288 | } while (0) | |
289 | #define STASH_EXCINFO(exc) do { \ | |
290 | struct excinfo *_exc = (exc); \ | |
291 | PyErr_Fetch(&_exc->ty, &_exc->val, &_exc->tb); \ | |
292 | PyErr_NormalizeException(&_exc->ty, &_exc->val, &_exc->tb); \ | |
293 | } while (0) | |
294 | #define RESTORE_EXCINFO(exc) do { \ | |
295 | struct excinfo *_exc = (exc); \ | |
296 | PyErr_Restore(_exc->ty, _exc->val, _exc->tb); \ | |
297 | _exc->ty = _exc->val = _exc->tb = 0; \ | |
298 | } while (0) | |
299 | extern void report_lost_exception(struct excinfo *, const char *, ...); | |
300 | extern void report_lost_exception_v(struct excinfo *, const char *, va_list); | |
301 | extern void stash_exception(struct excinfo *, const char *, ...); | |
302 | extern void restore_exception(struct excinfo *, const char *, ...); | |
303 | ||
dc075750 | 304 | extern void typeready(PyTypeObject *); |
76ca8edd | 305 | extern PyTypeObject *inittype(PyTypeObject *, PyTypeObject *); |
11cb3d97 MW |
306 | extern void addmethods(const PyMethodDef *); |
307 | extern PyMethodDef *donemethods(void); | |
308 | ||
309 | /*----- Mapping methods ---------------------------------------------------*/ | |
310 | ||
311 | #define GMAP_METH(func, doc) { #func, gmapmeth_##func, METH_VARARGS, doc }, | |
312 | #define GMAP_KWMETH(func, doc) \ | |
313 | { #func, (PyCFunction)gmapmeth_##func, METH_VARARGS|METH_KEYWORDS, doc }, | |
314 | #define GMAP_METHDECL(func, doc) \ | |
315 | extern PyObject *gmapmeth_##func(PyObject *, PyObject *); | |
316 | #define GMAP_KWMETHDECL(func, doc) \ | |
317 | extern PyObject *gmapmeth_##func(PyObject *, PyObject *, PyObject *); | |
318 | ||
7ca950d1 | 319 | #define GMAP_DOROMETHODS(METH, KWMETH) \ |
11cb3d97 MW |
320 | METH (has_key, "D.has_key(KEY) -> BOOL") \ |
321 | METH (keys, "D.keys() -> LIST") \ | |
322 | METH (values, "D.values() -> LIST") \ | |
323 | METH (items, "D.items() -> LIST") \ | |
324 | METH (iterkeys, "D.iterkeys() -> ITER") \ | |
325 | METH (itervalues, "D.itervalues() -> ITER") \ | |
b2687a0a | 326 | METH (iteritems, "D.iteritems() -> ITER") \ |
11cb3d97 | 327 | KWMETH(get, "D.get(KEY, [default = None]) -> VALUE") \ |
7ca950d1 MW |
328 | |
329 | #define GMAP_DOMETHODS(METH, KWMETH) \ | |
330 | GMAP_DOROMETHODS(METH, KWMETH) \ | |
331 | METH (clear, "D.clear()") \ | |
11cb3d97 MW |
332 | KWMETH(setdefault, "D.setdefault(K, [default = None]) -> VALUE") \ |
333 | KWMETH(pop, "D.pop(KEY, [default = <error>]) -> VALUE") \ | |
334 | METH (popitem, "D.popitem() -> (KEY, VALUE)") \ | |
335 | METH (update, "D.update(MAP)") | |
336 | ||
337 | GMAP_DOMETHODS(GMAP_METHDECL, GMAP_KWMETHDECL) | |
7ca950d1 | 338 | #define GMAP_ROMETHODS GMAP_DOROMETHODS(GMAP_METH, GMAP_KWMETH) |
11cb3d97 | 339 | #define GMAP_METHODS GMAP_DOMETHODS(GMAP_METH, GMAP_KWMETH) |
3d8f5f7c | 340 | extern Py_ssize_t gmap_pysize(PyObject *); |
11cb3d97 MW |
341 | extern PySequenceMethods gmap_pysequence; |
342 | extern PyMethodDef gmap_pymethods[]; | |
343 | ||
d7ab1bab | 344 | /*----- Bytestrings -------------------------------------------------------*/ |
345 | ||
d7ab1bab | 346 | PyObject *bytestring_pywrap(const void *, size_t); |
347 | PyObject *bytestring_pywrapbuf(buf *); | |
348 | ||
349 | /*----- Multiprecision arithmetic -----------------------------------------*/ | |
350 | ||
351 | typedef struct mp_pyobj { | |
352 | PyObject_HEAD | |
353 | mp *x; | |
354 | } mp_pyobj; | |
355 | ||
356 | extern PyTypeObject *mp_pytype; | |
357 | extern PyTypeObject *gf_pytype; | |
358 | #define MP_X(o) (((mp_pyobj *)(o))->x) | |
359 | #define MP_PYCHECK(o) PyObject_TypeCheck((o), mp_pytype) | |
360 | #define GF_PYCHECK(o) PyObject_TypeCheck((o), gf_pytype) | |
361 | ||
f368b46e MW |
362 | extern mp *mp_frompylong(PyObject *); |
363 | extern PyObject *mp_topylong(mp *); | |
d7ab1bab | 364 | extern mp *tomp(PyObject *); |
365 | extern mp *getmp(PyObject *); | |
366 | extern int convmp(PyObject *, void *); | |
367 | extern mp *getgf(PyObject *); | |
368 | extern int convgf(PyObject *, void *); | |
369 | extern PyObject *mp_pywrap(mp *); | |
370 | extern PyObject *gf_pywrap(mp *); | |
be6df1c3 | 371 | extern long mphash(mp *); |
d7ab1bab | 372 | extern mp *mp_frompyobject(PyObject *, int); |
373 | extern PyObject *mp_topystring(mp *, int, | |
374 | const char *, const char *, const char *); | |
bc243788 | 375 | extern int mp_tolong_checked(mp *, long *, int); |
d7ab1bab | 376 | |
377 | /*----- Abstract fields ---------------------------------------------------*/ | |
378 | ||
379 | typedef struct field_pyobj { | |
df9f8366 | 380 | PyHeapTypeObject ty; |
d7ab1bab | 381 | field *f; |
382 | } field_pyobj; | |
383 | ||
d7ab1bab | 384 | extern PyTypeObject *field_pytype; |
385 | extern PyTypeObject *primefield_pytype; | |
386 | extern PyTypeObject *niceprimefield_pytype; | |
387 | extern PyTypeObject *binfield_pytype; | |
388 | extern PyTypeObject *binpolyfield_pytype; | |
389 | extern PyTypeObject *binnormfield_pytype; | |
390 | #define FIELD_PYCHECK(o) PyObject_TypeCheck((o), field_pytype) | |
391 | #define FIELD_F(o) (((field_pyobj *)(o))->f) | |
392 | extern PyObject *field_pywrap(field *); | |
393 | extern field *field_copy(field *); | |
394 | ||
c6e89d48 MW |
395 | typedef struct fe_pyobj { |
396 | PyObject_HEAD | |
397 | field *f; | |
398 | mp *x; | |
399 | } fe_pyobj; | |
400 | ||
401 | extern PyTypeObject *fe_pytype; | |
402 | #define FE_PYCHECK(o) PyObject_TypeCheck((o), fe_pytype) | |
403 | #define FE_F(o) (((fe_pyobj *)(o))->f) | |
404 | #define FE_FOBJ(o) ((PyObject *)(o)->ob_type) | |
405 | #define FE_X(o) (((fe_pyobj *)(o))->x) | |
406 | extern PyObject *fe_pywrap(PyObject *, mp *); | |
407 | ||
d7ab1bab | 408 | /*----- Elliptic curves ---------------------------------------------------*/ |
409 | ||
c6e89d48 MW |
410 | typedef struct eccurve_pyobj { |
411 | PyHeapTypeObject ty; | |
412 | ec_curve *c; | |
413 | PyObject *fobj; | |
414 | } eccurve_pyobj; | |
415 | ||
416 | extern PyTypeObject *eccurve_pytype; | |
417 | extern PyTypeObject *ecprimecurve_pytype; | |
418 | extern PyTypeObject *ecprimeprojcurve_pytype; | |
419 | extern PyTypeObject *ecbincurve_pytype; | |
420 | extern PyTypeObject *ecbinprojcurve_pytype; | |
421 | #define ECCURVE_PYCHECK(o) PyObject_TypeCheck((o), eccurve_pytype) | |
422 | #define ECCURVE_C(o) (((eccurve_pyobj *)(o))->c) | |
423 | #define ECCURVE_FOBJ(o) (((eccurve_pyobj *)(o))->fobj) | |
424 | extern PyObject *eccurve_pywrap(PyObject *, ec_curve *); | |
425 | extern ec_curve *eccurve_copy(ec_curve *); | |
426 | ||
d7ab1bab | 427 | typedef struct ecpt_pyobj { |
428 | PyObject_HEAD | |
429 | ec_curve *c; | |
430 | ec p; | |
431 | } ecpt_pyobj; | |
432 | ||
433 | extern PyTypeObject *ecpt_pytype, *ecptcurve_pytype; | |
434 | #define ECPT_PYCHECK(o) PyObject_TypeCheck((o), ecpt_pytype) | |
435 | #define ECPTCURVE_PYCHECK(o) PyObject_TypeCheck((o), ecptcurve_pytype) | |
436 | #define ECPT_C(o) (((ecpt_pyobj *)(o))->c) | |
437 | #define ECPT_COBJ(o) ((PyObject *)(o)->ob_type) | |
438 | #define ECPT_FOBJ(o) ECCURVE_FOBJ(ECPT_COBJ((o))) | |
439 | #define ECPT_P(o) (&((ecpt_pyobj *)(o))->p) | |
440 | extern PyObject *ecpt_pywrap(PyObject *, ec *); | |
441 | extern PyObject *ecpt_pywrapout(void *, ec *); | |
442 | extern int toecpt(ec_curve *, ec *, PyObject *); | |
443 | extern int getecpt(ec_curve *, ec *, PyObject *); | |
444 | extern void getecptout(ec *, PyObject *); | |
46e6ad89 | 445 | extern int convecpt(PyObject *, void *); |
d7ab1bab | 446 | |
d7ab1bab | 447 | typedef struct ecinfo_pyobj { |
448 | PyObject_HEAD | |
449 | ec_info ei; | |
450 | PyObject *cobj; | |
451 | } ecinfo_pyobj; | |
b2687a0a | 452 | |
d7ab1bab | 453 | extern PyTypeObject *ecinfo_pytype; |
454 | #define ECINFO_PYCHECK(o) PyObject_TypeCheck((o), ecinfo_pytype) | |
455 | #define ECINFO_EI(o) (&((ecinfo_pyobj *)(o))->ei) | |
456 | #define ECINFO_COBJ(o) (((ecinfo_pyobj *)(o))->cobj) | |
457 | extern void ecinfo_copy(ec_info *, const ec_info *); | |
458 | extern PyObject *ecinfo_pywrap(ec_info *); | |
459 | ||
460 | /*----- Cyclic groups -----------------------------------------------------*/ | |
461 | ||
d7ab1bab | 462 | typedef struct ge_pyobj { |
463 | PyObject_HEAD | |
464 | ge *x; | |
465 | group *g; | |
466 | } ge_pyobj; | |
467 | ||
468 | extern PyTypeObject *ge_pytype; | |
469 | #define GE_PYCHECK(o) PyObject_TypeCheck((o), ge_pytype) | |
470 | #define GE_X(o) (((ge_pyobj *)(o))->x) | |
471 | #define GE_G(o) (((ge_pyobj *)(o))->g) | |
472 | #define GE_GOBJ(o) ((PyObject *)(group_pyobj *)(o)->ob_type) | |
473 | extern PyObject *ge_pywrap(PyObject *, ge *); | |
474 | ||
475 | typedef struct group_pyobj { | |
df9f8366 | 476 | PyHeapTypeObject ty; |
d7ab1bab | 477 | group *g; |
478 | } group_pyobj; | |
479 | ||
480 | extern PyTypeObject *group_pytype; | |
d7ab1bab | 481 | #define GROUP_G(o) (((group_pyobj *)(o))->g) |
482 | extern PyObject *group_pywrap(group *); | |
483 | extern group *group_copy(group *); | |
484 | ||
485 | /*----- Random number generators ------------------------------------------*/ | |
486 | ||
487 | #define f_freeme 1u | |
488 | ||
489 | typedef struct grand_pyobj { | |
490 | PyObject_HEAD | |
491 | unsigned f; | |
492 | grand *r; | |
493 | } grand_pyobj; | |
494 | ||
c6e89d48 | 495 | extern PyTypeObject *grand_pytype; |
d7ab1bab | 496 | extern PyObject *rand_pyobj; |
497 | #define GRAND_PYCHECK(o) PyObject_TypeCheck((o), grand_pytype) | |
498 | #define GRAND_F(o) (((grand_pyobj *)(o))->f) | |
499 | #define GRAND_R(o) (((grand_pyobj *)(o))->r) | |
500 | extern PyObject *grand_pywrap(grand *, unsigned); | |
501 | extern int convgrand(PyObject *, void *); | |
502 | ||
d7ab1bab | 503 | /*----- Symmetric cryptography --------------------------------------------*/ |
504 | ||
c6e89d48 | 505 | extern PyObject *keysz_pywrap(const octet *); |
d7ab1bab | 506 | |
d7ab1bab | 507 | extern int convgccipher(PyObject *, void *); |
c6e89d48 | 508 | extern PyObject *gccipher_pywrap(gccipher *); |
bebf03ab | 509 | |
d7ab1bab | 510 | typedef struct gchash_pyobj { |
df9f8366 | 511 | PyHeapTypeObject ty; |
d7ab1bab | 512 | gchash *ch; |
513 | } gchash_pyobj; | |
514 | ||
515 | extern PyTypeObject *gchash_pytype; | |
c6e89d48 | 516 | extern PyObject *sha_pyobj, *has160_pyobj; |
d7ab1bab | 517 | #define GCHASH_PYCHECK(o) PyObject_TypeCheck((o), gchash_pytype) |
518 | #define GCHASH_CH(o) (((gchash_pyobj *)(o))->ch) | |
c41d0371 | 519 | extern PyObject *ghash_pywrap(PyObject *, ghash *); |
c6e89d48 | 520 | extern int convgchash(PyObject *, void *); |
d7ab1bab | 521 | extern int convghash(PyObject *, void *); |
d7ab1bab | 522 | |
d7ab1bab | 523 | extern int convgcmac(PyObject *, void *); |
524 | ||
d7ab1bab | 525 | /*----- Key generation ----------------------------------------------------*/ |
b2687a0a | 526 | |
d7ab1bab | 527 | typedef struct pfilt_pyobj { |
528 | PyObject_HEAD | |
529 | pfilt f; | |
530 | int st; | |
531 | } pfilt_pyobj; | |
532 | ||
533 | extern PyTypeObject *pfilt_pytype; | |
534 | #define PFILT_PYCHECK(o) PyObject_TypeCheck(o, pfilt_pytype) | |
535 | #define PFILT_F(o) (&((pfilt_pyobj *)(o))->f) | |
536 | #define PFILT_ST(o) (((pfilt_pyobj *)(o))->st) | |
537 | ||
538 | typedef struct { pgen_proc *proc; void *ctx; } pgev; | |
539 | #define PGEV_HEAD PyObject_HEAD pgev pg; | |
540 | ||
541 | typedef struct pgev_pyobj { | |
542 | PGEV_HEAD | |
543 | } pgev_pyobj; | |
544 | ||
545 | extern PyTypeObject *pgev_pytype; | |
546 | #define PGEV_PYCHECK(o) PyObject_TypeCheck(o, pgev_pytype) | |
547 | #define PGEV_PG(o) (&((pgev_pyobj *)(o))->pg) | |
548 | ||
930d78e3 MW |
549 | typedef struct pypgev { |
550 | pgev ev; | |
551 | PyObject *obj; | |
552 | struct excinfo *exc; | |
553 | } pypgev; | |
554 | ||
d7ab1bab | 555 | extern int convpgev(PyObject *, void *); |
930d78e3 MW |
556 | extern void droppgev(pypgev *); |
557 | extern void pgenerr(struct excinfo *exc); | |
d7ab1bab | 558 | |
d7ab1bab | 559 | /*----- That's all, folks -------------------------------------------------*/ |
560 | ||
561 | #ifdef __cplusplus | |
562 | } | |
563 | #endif | |
564 | ||
565 | #endif |