chiark / gitweb /
bytestring.c (bytestring_pyrepeat): Don't divide by zero.
[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 #define PY_SSIZE_T_CLEAN
37
38 #include <Python.h>
39 #include <longintrepr.h>
40 #include <structmember.h>
41
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>
47
48 #include <catacomb/buf.h>
49 #include <catacomb/ct.h>
50
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>
62
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>
75
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>
84
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>
97
98 #include <catacomb/gf.h>
99 #include <catacomb/gfreduce.h>
100 #include <catacomb/gfn.h>
101
102 #include <catacomb/field.h>
103 #include <catacomb/field-guts.h>
104
105 #include <catacomb/ec.h>
106 #include <catacomb/ec-raw.h>
107 #include <catacomb/ectab.h>
108
109 #include <catacomb/group.h>
110 #include <catacomb/group-guts.h>
111
112 #include <catacomb/gdsa.h>
113 #include <catacomb/gkcdsa.h>
114 #include <catacomb/rsa.h>
115
116 #include <catacomb/key.h>
117 #include <catacomb/passphrase.h>
118 #include <catacomb/pixie.h>
119
120 #include <catacomb/share.h>
121 #include <catacomb/gfshare.h>
122
123 /*----- Utility macros ----------------------------------------------------*/
124
125 #define RETURN_OBJ(obj) do { Py_INCREF(obj); return (obj); } while (0)
126 #define RETURN_NONE RETURN_OBJ(Py_None)
127 #define RETURN_NOTIMPL RETURN_OBJ(Py_NotImplemented)
128 #define RETURN_TRUE RETURN_OBJ(Py_True)
129 #define RETURN_FALSE RETURN_OBJ(Py_False)
130 #define RETURN_ME RETURN_OBJ(me)
131
132 #define EXCERR(exc, str) do {                                           \
133   PyErr_SetString(exc, str);                                            \
134   goto end;                                                             \
135 } while (0)
136 #define VALERR(str) EXCERR(PyExc_ValueError, str)
137 #define TYERR(str) EXCERR(PyExc_TypeError, str)
138 #define IXERR(str) EXCERR(PyExc_IndexError, str)
139 #define ZDIVERR(str) EXCERR(PyExc_ZeroDivisionError, str)
140 #define SYSERR(str) EXCERR(PyExc_SystemError, str)
141 #define NIERR(str) EXCERR(PyExc_NotImplementedError, str)
142 #define INDEXERR(idx) do {                                              \
143   PyErr_SetObject(PyExc_KeyError, idx);                                 \
144   goto end;                                                             \
145 } while (0)
146 #define OSERR(name) do {                                                \
147   PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);                  \
148   goto end;                                                             \
149 } while (0)
150 #define PGENERR do { pgenerr(); goto end; } while (0)
151
152 #define CONVFUNC(ty, cty, ext)                                          \
153   int conv##ty(PyObject *o, void *p)                                    \
154   {                                                                     \
155     if (!PyObject_TypeCheck(o, ty##_pytype))                            \
156       TYERR("wanted a " #ty);                                           \
157     *(cty *)p = ext(o);                                                 \
158     return (1);                                                         \
159   end:                                                                  \
160     return (0);                                                         \
161   }
162
163 #if PY_VERSION_HEX < 0x02050000         /* Compatibility hack */
164 #  define ht_name name
165 #  define ht_type type
166 #endif
167
168 #define root_pytype 0
169 #define type_pytype &PyType_Type
170 #define INITTYPE_META(ty, base, meta) do {                              \
171   ty##_pytype_skel.tp_base = base##_pytype;                             \
172   ty##_pytype = inittype(&ty##_pytype_skel, meta##_pytype);             \
173 } while (0)
174 #define INITTYPE(ty, base) INITTYPE_META(ty, base, type)
175
176 #define INSERT(name, ob) do {                                           \
177   PyObject *_o = (PyObject *)(ob);                                      \
178   Py_INCREF(_o);                                                        \
179   PyModule_AddObject(mod, name, _o);                                    \
180 } while (0)
181
182 #define INSEXC(name, var, base, meth)                                   \
183   INSERT(name, var = mkexc(mod, base, name, meth))
184
185 #define METH(func, doc)                                                 \
186   { #func, METHNAME(func), METH_VARARGS, doc },
187 #define KWMETH(func, doc)                                               \
188   { #func, (PyCFunction)METHNAME(func),                                 \
189     METH_VARARGS | METH_KEYWORDS, doc },
190
191 #define GET(func, doc)                                                  \
192   { #func, GETSETNAME(get, func), 0, doc },
193 #define GETSET(func, doc)                                               \
194   { #func, GETSETNAME(get, func), GETSETNAME(set, func), doc },
195
196 #define MEMBER(name, ty, f, doc)                                        \
197   { #name, ty, offsetof(MEMBERSTRUCT, name), f, doc },
198
199 #define MODULES(_)                                                      \
200   _(util)                                                               \
201   _(bytestring) _(buffer)                                               \
202   _(rand) _(algorithms) _(pubkey) _(pgen)                               \
203   _(mp) _(field) _(ec) _(group)                                         \
204   _(passphrase) _(share) _(key)
205 #define DOMODINIT(m) m##_pyinit();
206 #define DOMODINSERT(m) m##_pyinsert(mod);
207 #define INIT_MODULES do { MODULES(DOMODINIT) } while (0)
208 #define INSERT_MODULES do { MODULES(DOMODINSERT) } while (0)
209
210 #define DO(m)                                                           \
211   extern void m##_pyinit(void);                                         \
212   extern void m##_pyinsert(PyObject *);
213 MODULES(DO)
214 #undef DO
215
216 #define FREEOBJ(obj)                                                    \
217   (((PyObject *)(obj))->ob_type->tp_free((PyObject *)(obj)))
218
219 #define GEN(func, base)                                                 \
220   static PyObject *func(void)                                           \
221   {                                                                     \
222     PyObject *d = PyDict_New();                                         \
223     PyObject *o;                                                        \
224     int i;                                                              \
225                                                                         \
226     for (i = 0; g##base##tab[i]; i++) {                                 \
227       o = gc##base##_pywrap((/*unconst*/ gc##base *)g##base##tab[i]);   \
228       PyDict_SetItemString(d,                                           \
229                            (/*unconst*/ char *)g##base##tab[i]->name,   \
230                            o);                                          \
231       Py_DECREF(o);                                                     \
232     }                                                                   \
233     return (d);                                                         \
234   }
235
236 #define KWLIST (/*unconst*/ char **)kwlist
237
238 struct nameval { const char *name; unsigned long value; };
239 extern void setconstants(PyObject *, const struct nameval *);
240
241 extern PyObject *mexp_common(PyObject *, PyObject *, size_t,
242                              PyObject *(*id)(PyObject *),
243                              int (*fill)(void *, PyObject *,
244                                          PyObject *, PyObject *),
245                              PyObject *(*exp)(PyObject *, void *, int),
246                              void (*drop)(void *));
247
248 extern int convulong(PyObject *, void *);
249 #define DECL_CONVU_(n) extern int convu##n(PyObject *, void *);
250 DOUINTSZ(DECL_CONVU_)
251 extern int convmpw(PyObject *, void *);
252 extern int convuint(PyObject *, void *);
253 extern int convk64(PyObject *, void *);
254 extern int convszt(PyObject *, void *);
255 extern int convbool(PyObject *, void *);
256 extern PyObject *abstract_pynew(PyTypeObject *, PyObject *, PyObject *);
257 extern PyObject *getbool(int);
258 extern PyObject *getulong(unsigned long);
259 extern PyObject *getk64(kludge64);
260 extern void *newtype(PyTypeObject *, const PyTypeObject *, const char *);
261
262 extern PyObject *mkexc(PyObject *, PyObject *, const char *, PyMethodDef *);
263 extern void typeready(PyTypeObject *);
264 extern PyTypeObject *inittype(PyTypeObject *, PyTypeObject *);
265 extern void addmethods(const PyMethodDef *);
266 extern PyMethodDef *donemethods(void);
267
268 /*----- Mapping methods ---------------------------------------------------*/
269
270 #define GMAP_METH(func, doc) { #func, gmapmeth_##func, METH_VARARGS, doc },
271 #define GMAP_KWMETH(func, doc)                                          \
272   { #func, (PyCFunction)gmapmeth_##func, METH_VARARGS|METH_KEYWORDS, doc },
273 #define GMAP_METHDECL(func, doc)                                        \
274   extern PyObject *gmapmeth_##func(PyObject *, PyObject *);
275 #define GMAP_KWMETHDECL(func, doc)                                      \
276   extern PyObject *gmapmeth_##func(PyObject *, PyObject *, PyObject *);
277
278 #define GMAP_DOROMETHODS(METH, KWMETH)                                  \
279   METH  (has_key,       "D.has_key(KEY) -> BOOL")                       \
280   METH  (keys,          "D.keys() -> LIST")                             \
281   METH  (values,        "D.values() -> LIST")                           \
282   METH  (items,         "D.items() -> LIST")                            \
283   METH  (iterkeys,      "D.iterkeys() -> ITER")                         \
284   METH  (itervalues,    "D.itervalues() -> ITER")                       \
285   METH  (iteritems,     "D.iteritems() -> ITER")                        \
286   KWMETH(get,           "D.get(KEY, [default = None]) -> VALUE")        \
287
288 #define GMAP_DOMETHODS(METH, KWMETH)                                    \
289   GMAP_DOROMETHODS(METH, KWMETH)                                        \
290   METH  (clear,         "D.clear()")                                    \
291   KWMETH(setdefault,    "D.setdefault(K, [default = None]) -> VALUE")   \
292   KWMETH(pop,           "D.pop(KEY, [default = <error>]) -> VALUE")     \
293   METH  (popitem,       "D.popitem() -> (KEY, VALUE)")                  \
294   METH  (update,        "D.update(MAP)")
295
296 GMAP_DOMETHODS(GMAP_METHDECL, GMAP_KWMETHDECL)
297 #define GMAP_ROMETHODS GMAP_DOROMETHODS(GMAP_METH, GMAP_KWMETH)
298 #define GMAP_METHODS GMAP_DOMETHODS(GMAP_METH, GMAP_KWMETH)
299 extern Py_ssize_t gmap_pysize(PyObject *);
300 extern PySequenceMethods gmap_pysequence;
301 extern PyMethodDef gmap_pymethods[];
302
303 /*----- Bytestrings -------------------------------------------------------*/
304
305 PyTypeObject *bytestring_pyobj;
306 PyObject *bytestring_pywrap(const void *, size_t);
307 PyObject *bytestring_pywrapbuf(buf *);
308
309 /*----- Multiprecision arithmetic -----------------------------------------*/
310
311 typedef struct mp_pyobj {
312   PyObject_HEAD
313   mp *x;
314 } mp_pyobj;
315
316 extern PyTypeObject *mp_pytype;
317 extern PyTypeObject *gf_pytype;
318 #define MP_X(o) (((mp_pyobj *)(o))->x)
319 #define MP_PYCHECK(o) PyObject_TypeCheck((o), mp_pytype)
320 #define GF_PYCHECK(o) PyObject_TypeCheck((o), gf_pytype)
321
322 extern mp *mp_frompylong(PyObject *);
323 extern PyObject *mp_topylong(mp *);
324 extern mp *tomp(PyObject *);
325 extern mp *getmp(PyObject *);
326 extern int convmp(PyObject *, void *);
327 extern mp *getgf(PyObject *);
328 extern int convgf(PyObject *, void *);
329 extern PyObject *mp_pywrap(mp *);
330 extern PyObject *gf_pywrap(mp *);
331 extern mp *mp_frompyobject(PyObject *, int);
332 extern PyObject *mp_topystring(mp *, int,
333                                const char *, const char *, const char *);
334 extern int mp_tolong_checked(mp *, long *, int);
335
336 /*----- Abstract fields ---------------------------------------------------*/
337
338 typedef struct field_pyobj {
339   PyHeapTypeObject ty;
340   field *f;
341 } field_pyobj;
342
343 extern PyTypeObject *fe_pytype;
344 #define FE_PYCHECK(o) PyObject_TypeCheck((o), fe_pytype)
345 #define FE_F(o) (((fe_pyobj *)(o))->f)
346 #define FE_FOBJ(o) ((PyObject *)(o)->ob_type)
347 #define FE_X(o) (((fe_pyobj *)(o))->x)
348 extern PyObject *fe_pywrap(PyObject *, mp *);
349 extern mp *getfe(field *, PyObject *);
350
351 typedef struct fe_pyobj {
352   PyObject_HEAD
353   field *f;
354   mp *x;
355 } fe_pyobj;
356
357 extern PyTypeObject *field_pytype;
358 extern PyTypeObject *primefield_pytype;
359 extern PyTypeObject *niceprimefield_pytype;
360 extern PyTypeObject *binfield_pytype;
361 extern PyTypeObject *binpolyfield_pytype;
362 extern PyTypeObject *binnormfield_pytype;
363 #define FIELD_PYCHECK(o) PyObject_TypeCheck((o), field_pytype)
364 #define FIELD_F(o) (((field_pyobj *)(o))->f)
365 extern PyObject *field_pywrap(field *);
366 extern field *field_copy(field *);
367
368 /*----- Elliptic curves ---------------------------------------------------*/
369
370 typedef struct ecpt_pyobj {
371   PyObject_HEAD
372   ec_curve *c;
373   ec p;
374 } ecpt_pyobj;
375
376 extern PyTypeObject *ecpt_pytype, *ecptcurve_pytype;
377 #define ECPT_PYCHECK(o) PyObject_TypeCheck((o), ecpt_pytype)
378 #define ECPTCURVE_PYCHECK(o) PyObject_TypeCheck((o), ecptcurve_pytype)
379 #define ECPT_C(o) (((ecpt_pyobj *)(o))->c)
380 #define ECPT_COBJ(o) ((PyObject *)(o)->ob_type)
381 #define ECPT_FOBJ(o) ECCURVE_FOBJ(ECPT_COBJ((o)))
382 #define ECPT_P(o) (&((ecpt_pyobj *)(o))->p)
383 extern PyObject *ecpt_pywrap(PyObject *, ec *);
384 extern PyObject *ecpt_pywrapout(void *, ec *);
385 extern int toecpt(ec_curve *, ec *, PyObject *);
386 extern int getecpt(ec_curve *, ec *, PyObject *);
387 extern void getecptout(ec *, PyObject *);
388 extern int convecpt(PyObject *, void *);
389
390 typedef struct eccurve_pyobj {
391   PyHeapTypeObject ty;
392   ec_curve *c;
393   PyObject *fobj;
394 } eccurve_pyobj;
395
396 extern PyTypeObject *eccurve_pytype;
397 extern PyTypeObject *ecprimecurve_pytype;
398 extern PyTypeObject *ecprimeprojcurve_pytype;
399 extern PyTypeObject *ecbincurve_pytype;
400 extern PyTypeObject *ecbinprojcurve_pytype;
401 #define ECCURVE_PYCHECK(o) PyObject_TypeCheck((o), eccurve_pytype)
402 #define ECCURVE_C(o) (((eccurve_pyobj *)(o))->c)
403 #define ECCURVE_FOBJ(o) (((eccurve_pyobj *)(o))->fobj)
404 extern PyObject *eccurve_pywrap(PyObject *, ec_curve *);
405 extern ec_curve *eccurve_copy(ec_curve *);
406
407 typedef struct ecinfo_pyobj {
408   PyObject_HEAD
409   ec_info ei;
410   PyObject *cobj;
411 } ecinfo_pyobj;
412
413 extern PyTypeObject *ecinfo_pytype;
414 #define ECINFO_PYCHECK(o) PyObject_TypeCheck((o), ecinfo_pytype)
415 #define ECINFO_EI(o) (&((ecinfo_pyobj *)(o))->ei)
416 #define ECINFO_COBJ(o) (((ecinfo_pyobj *)(o))->cobj)
417 extern void ecinfo_copy(ec_info *, const ec_info *);
418 extern PyObject *ecinfo_pywrap(ec_info *);
419
420 /*----- Cyclic groups -----------------------------------------------------*/
421
422 typedef struct fginfo_pyobj {
423   PyObject_HEAD
424   gprime_param dp;
425 } fginfo_pyobj;
426
427 PyTypeObject *fginfo_pytype, *dhinfo_pytype, *bindhinfo_pytype;
428 #define FGINFO_DP(fg) (&((fginfo_pyobj *)(fg))->dp)
429 PyObject *fginfo_pywrap(gprime_param *, PyTypeObject *);
430
431 typedef struct ge_pyobj {
432   PyObject_HEAD
433   ge *x;
434   group *g;
435 } ge_pyobj;
436
437 extern PyTypeObject *ge_pytype;
438 #define GE_PYCHECK(o) PyObject_TypeCheck((o), ge_pytype)
439 #define GE_X(o) (((ge_pyobj *)(o))->x)
440 #define GE_G(o) (((ge_pyobj *)(o))->g)
441 #define GE_GOBJ(o) ((PyObject *)(group_pyobj *)(o)->ob_type)
442 extern PyObject *ge_pywrap(PyObject *, ge *);
443
444 typedef struct group_pyobj {
445   PyHeapTypeObject ty;
446   group *g;
447 } group_pyobj;
448
449 extern PyTypeObject *group_pytype;
450 extern PyTypeObject *primegroup_pytype, *bingroup_pytype, *ecgroup_pytype;
451 #define GROUP_G(o) (((group_pyobj *)(o))->g)
452 extern PyObject *group_pywrap(group *);
453 extern group *group_copy(group *);
454
455 /*----- Random number generators ------------------------------------------*/
456
457 #define f_freeme 1u
458
459 typedef struct grand_pyobj {
460   PyObject_HEAD
461   unsigned f;
462   grand *r;
463 } grand_pyobj;
464
465 extern PyTypeObject *grand_pytype, *truerand_pytype;
466 extern PyTypeObject *lcrand_pytype,* fibrand_pytype;
467 extern PyTypeObject *dsarand_pytype, *bbs_pytype;
468 extern PyObject *rand_pyobj;
469 #define GRAND_PYCHECK(o) PyObject_TypeCheck((o), grand_pytype)
470 #define GRAND_F(o) (((grand_pyobj *)(o))->f)
471 #define GRAND_R(o) (((grand_pyobj *)(o))->r)
472 extern PyObject *grand_pywrap(grand *, unsigned);
473 extern int convgrand(PyObject *, void *);
474
475 /*----- Key sizes ---------------------------------------------------------*/
476
477 typedef struct keysz_pyobj {
478   PyObject_HEAD
479   int dfl;
480 } keysz_pyobj;
481
482 typedef struct keyszrange_pyobj {
483   PyObject_HEAD
484   int dfl;
485   int min, max, mod;
486 } keyszrange_pyobj;
487
488 typedef struct keyszset_pyobj {
489   PyObject_HEAD
490   int dfl;
491   PyObject *set;
492 } keyszset_pyobj;
493
494 #define KEYSZ_PYCHECK(o) PyObject_TypeCheck((o), keysz_pytype)
495 extern PyObject *keysz_pywrap(const octet *);
496
497 /*----- Symmetric cryptography --------------------------------------------*/
498
499 typedef struct gccipher_pyobj {
500   PyHeapTypeObject ty;
501   gccipher *cc;
502 } gccipher_pyobj;
503
504 extern PyTypeObject *gccipher_pytype;
505 #define GCCIPHER_PYCHECK(o) PyObject_TypeCheck((o), gccipher_pytype)
506 #define GCCIPHER_CC(o) (((gccipher_pyobj *)(o))->cc)
507 extern PyObject *gccipher_pywrap(gccipher *);
508 extern int convgccipher(PyObject *, void *);
509
510 typedef struct gcipher_pyobj {
511   PyObject_HEAD
512   gcipher *c;
513 } gcipher_pyobj;
514
515 extern PyTypeObject *gcipher_pytype;
516 #define GCIPHER_PYCHECK(o) PyObject_TypeCheck((o), gcipher_pytype)
517 #define GCIPHER_C(o) (((gcipher_pyobj *)(o))->c)
518 extern PyObject *gcipher_pywrap(PyObject *, gcipher *);
519 extern int convgcipher(PyObject *, void *);
520
521 typedef struct gcaead_pyobj {
522   PyHeapTypeObject ty;
523   gcaead *aec;
524   struct gcaeadaad_pyobj *aad;
525   struct gcaeadenc_pyobj *enc;
526   struct gcaeaddec_pyobj *dec;
527 } gcaead_pyobj;
528
529 extern PyTypeObject *gcaead_pytype;
530 #define GCAEAD_PYCHECK(o) PyObject_TypeCheck((o), gcaead_pytype)
531 #define GCAEAD_AEC(o) (((gcaead_pyobj *)(o))->aec)
532 #define GCAEAD_AAD(o) (((gcaead_pyobj *)(o))->aad)
533 #define GCAEAD_ENC(o) (((gcaead_pyobj *)(o))->enc)
534 #define GCAEAD_DEC(o) (((gcaead_pyobj *)(o))->dec)
535 extern PyObject *gcaead_pywrap(gcaead *);
536 extern int convgcaead(PyObject *, void *);
537
538 typedef struct gaeadkey_pyobj {
539   PyObject_HEAD
540   gaead_key *k;
541 } gaeadkey_pyobj;
542
543 extern PyTypeObject *gaeadkey_pytype;
544 #define GAEADKEY_PYCHECK(o) PyObject_TypeCheck((o), gaeadkey_pytype)
545 #define GAEADKEY_K(o) (((gaeadkey_pyobj *)(o))->k)
546 extern PyObject *gaeadkey_pywrap(PyObject *, gaead_key *);
547 extern int convgaeadkey(PyObject *, void *);
548
549 typedef struct gcaeadaad_pyobj {
550   PyHeapTypeObject ty;
551   gcaead_pyobj *key;
552 } gcaeadaad_pyobj;
553 #define GCAEADAAD_KEY(o) (((gcaeadaad_pyobj *)(o))->key)
554 extern PyTypeObject *gcaeadaad_pytype;
555
556 typedef struct gaeadaad_pyobj {
557   PyObject_HEAD
558   gaead_aad *a;
559   unsigned f;
560 #define AEADF_DEAD 32768u
561   size_t hsz, hlen;
562 } gaeadaad_pyobj;
563
564 extern PyTypeObject *gaeadaad_pytype;
565 #define GAEADAAD_PYCHECK(o) PyObject_TypeCheck((o), gaeadaad_pytype)
566 #define GAEADAAD_A(o) (((gaeadaad_pyobj *)(o))->a)
567 #define GAEADAAD_F(o) (((gaeadaad_pyobj *)(o))->f)
568 #define GAEADAAD_HSZ(o) (((gaeadaad_pyobj *)(o))->hsz)
569 #define GAEADAAD_HLEN(o) (((gaeadaad_pyobj *)(o))->hlen)
570 extern PyObject *gaeadaad_pywrap(PyObject *, gaead_aad *, unsigned, size_t);
571 extern int convgaeadaad(PyObject *, void *);
572
573 typedef struct gcaeadenc_pyobj {
574   PyHeapTypeObject ty;
575   gcaead_pyobj *key;
576 } gcaeadenc_pyobj;
577 #define GCAEADENC_KEY(o) (((gcaeadenc_pyobj *)(o))->key)
578 extern PyTypeObject *gcaeadenc_pytype;
579
580 typedef struct gaeadenc_pyobj {
581   PyObject_HEAD
582   gaead_enc *e;
583   gaeadaad_pyobj *aad;
584   unsigned f;
585   size_t hsz, msz, tsz;
586   size_t mlen;
587 } gaeadenc_pyobj;
588
589 extern PyTypeObject *gaeadenc_pytype;
590 #define GAEADENC_PYCHECK(o) PyObject_TypeCheck((o), gaeadenc_pytype)
591 #define GAEADENC_AAD(o) (((gaeadenc_pyobj *)(o))->aad)
592 #define GAEADENC_E(o) (((gaeadenc_pyobj *)(o))->e)
593 #define GAEADENC_F(o) (((gaeadenc_pyobj *)(o))->f)
594 #define GAEADENC_HSZ(o) (((gaeadenc_pyobj *)(o))->hsz)
595 #define GAEADENC_MSZ(o) (((gaeadenc_pyobj *)(o))->msz)
596 #define GAEADENC_TSZ(o) (((gaeadenc_pyobj *)(o))->tsz)
597 #define GAEADENC_MLEN(o) (((gaeadenc_pyobj *)(o))->mlen)
598 extern PyObject *gaeadenc_pywrap(PyObject *, gaead_enc *, unsigned,
599                                  size_t, size_t, size_t);
600 extern int convgaeadenc(PyObject *, void *);
601
602 typedef struct gcaeaddec_pyobj {
603   PyHeapTypeObject ty;
604   gcaead_pyobj *key;
605 } gcaeaddec_pyobj;
606 #define GCAEADDEC_KEY(o) (((gcaeaddec_pyobj *)(o))->key)
607 extern PyTypeObject *gcaeaddec_pytype;
608
609 typedef struct gaeaddec_pyobj {
610   PyObject_HEAD
611   gaead_dec *d;
612   gaeadaad_pyobj *aad;
613   unsigned f;
614   size_t hsz, csz, tsz;
615   size_t clen;
616 } gaeaddec_pyobj;
617
618 extern PyTypeObject *gaeaddec_pytype;
619 #define GAEADDEC_PYCHECK(o) PyObject_TypeCheck((o), gaeaddec_pytype)
620 #define GAEADDEC_AAD(o) (((gaeaddec_pyobj *)(o))->aad)
621 #define GAEADDEC_D(o) (((gaeaddec_pyobj *)(o))->d)
622 #define GAEADDEC_F(o) (((gaeaddec_pyobj *)(o))->f)
623 #define GAEADDEC_HSZ(o) (((gaeaddec_pyobj *)(o))->hsz)
624 #define GAEADDEC_CSZ(o) (((gaeaddec_pyobj *)(o))->csz)
625 #define GAEADDEC_TSZ(o) (((gaeaddec_pyobj *)(o))->tsz)
626 #define GAEADDEC_CLEN(o) (((gaeaddec_pyobj *)(o))->clen)
627 extern PyObject *gaeaddec_pywrap(PyObject *, gaead_dec *, unsigned,
628                                  size_t, size_t, size_t);
629 extern int convgaeaddec(PyObject *, void *);
630
631 typedef struct gchash_pyobj {
632   PyHeapTypeObject ty;
633   gchash *ch;
634 } gchash_pyobj;
635
636 extern PyTypeObject *gchash_pytype;
637 #define GCHASH_PYCHECK(o) PyObject_TypeCheck((o), gchash_pytype)
638 #define GCHASH_CH(o) (((gchash_pyobj *)(o))->ch)
639 extern PyObject *gchash_pywrap(gchash *);
640 extern int convgchash(PyObject *, void *);
641
642 typedef struct ghash_pyobj {
643   PyObject_HEAD
644   ghash *h;
645 } ghash_pyobj;
646
647 extern PyTypeObject *ghash_pytype, *gmhash_pytype;
648 extern PyObject *sha_pyobj, *has160_pyobj;
649 #define GHASH_PYCHECK(o) PyObject_TypeCheck((o), ghash_pytype)
650 #define GHASH_H(o) (((ghash_pyobj *)(o))->h)
651 extern PyObject *ghash_pywrap(PyObject *, ghash *);
652 extern int convghash(PyObject *, void *);
653 extern int convgmhash(PyObject *, void *);
654
655 typedef struct gcmac_pyobj {
656   PyHeapTypeObject ty;
657   gcmac *cm;
658 } gcmac_pyobj;
659
660 extern PyTypeObject *gcmac_pytype;
661 #define GCMAC_PYCHECK(o) PyObject_TypeCheck((o), gcmac_pytype)
662 #define GCMAC_CM(o) (((gcmac_pyobj *)(o))->cm)
663 #define GCMAC_F(o) (((gcmac_pyobj *)(o))->f)
664 extern PyObject *gcmac_pywrap(gcmac *);
665 extern int convgcmac(PyObject *, void *);
666
667 typedef struct gmac_pyobj {
668   PyHeapTypeObject ty;
669   gmac *m;
670 } gmac_pyobj;
671
672 extern PyTypeObject *gmac_pytype;
673 #define GMAC_PYCHECK(o) PyObject_TypeCheck((o), gmac_pytype)
674 #define GMAC_M(o) (((gmac_pyobj *)(o))->m)
675 #define GMAC_F(o) (((gmac_pyobj *)(o))->f)
676 extern PyObject *gmac_pywrap(PyObject *, gmac *);
677 extern int convgmac(PyObject *, void *);
678
679 /*----- Key generation ----------------------------------------------------*/
680
681 typedef struct pfilt_pyobj {
682   PyObject_HEAD
683   pfilt f;
684   int st;
685 } pfilt_pyobj;
686
687 extern PyTypeObject *pfilt_pytype;
688 #define PFILT_PYCHECK(o) PyObject_TypeCheck(o, pfilt_pytype)
689 #define PFILT_F(o) (&((pfilt_pyobj *)(o))->f)
690 #define PFILT_ST(o) (((pfilt_pyobj *)(o))->st)
691
692 typedef struct { pgen_proc *proc; void *ctx; } pgev;
693 #define PGEV_HEAD PyObject_HEAD pgev pg;
694
695 typedef struct pgev_pyobj {
696   PGEV_HEAD
697 } pgev_pyobj;
698
699 extern PyTypeObject *pgev_pytype;
700 #define PGEV_PYCHECK(o) PyObject_TypeCheck(o, pgev_pytype)
701 #define PGEV_PG(o) (&((pgev_pyobj *)(o))->pg)
702
703 extern int convpgev(PyObject *, void *);
704 extern void droppgev(pgev *);
705 extern void pgenerr(void);
706
707 /*----- That's all, folks -------------------------------------------------*/
708
709 #ifdef __cplusplus
710   }
711 #endif
712
713 #endif