chiark / gitweb /
Fix a few minor warnings.
[catacomb-python] / rand.c
1 /* -*-c-*-
2  *
3  * Random-number generators
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 /*----- Header files ------------------------------------------------------*/
28
29 #include "catacomb-python.h"
30 #include "algorithms.h"
31
32 /*----- Main code ---------------------------------------------------------*/
33
34 PyTypeObject *grand_pytype, *truerand_pytype;
35 PyTypeObject *lcrand_pytype, *fibrand_pytype;
36 PyTypeObject *dsarand_pytype, *bbs_pytype, *bbspriv_pytype;
37 PyTypeObject *sslprf_pytype, *tlsdx_pytype, *tlsprf_pytype;
38 PyObject *rand_pyobj;
39
40 static PyObject *gccrands_dict;
41
42 static PyObject *grand_dopywrap(PyTypeObject *ty, grand *r, unsigned f)
43 {
44   grand_pyobj *g;
45
46   g = (grand_pyobj *)ty->tp_alloc(ty, 0);
47   g->r = r;
48   g->f = f;
49   return ((PyObject *)g);
50 }
51
52 PyObject *grand_pywrap(grand *r, unsigned f)
53 {
54   PyTypeObject *ty = grand_pytype;
55   PyObject *ob;
56
57   if (strcmp(r->ops->name, "rand") == 0) ty = truerand_pytype;
58   else if (strcmp(r->ops->name, "lcrand") == 0) ty = lcrand_pytype;
59   else if (strcmp(r->ops->name, "fibrand") == 0) ty = fibrand_pytype;
60   else if (strcmp(r->ops->name, "dsarand") == 0) ty = dsarand_pytype;
61   else if (strcmp(r->ops->name, "bbs") == 0) ty = bbs_pytype;
62   else if (strcmp(r->ops->name, "sslprf") == 0) ty = sslprf_pytype;
63   else if (strcmp(r->ops->name, "tlsdx") == 0) ty = tlsdx_pytype;
64   else if (strcmp(r->ops->name, "tlsprf") == 0) ty = tlsprf_pytype;
65   else if ((ob = PyDict_GetItemString(gccrands_dict, r->ops->name)) != 0)
66     ty = (PyTypeObject *)ob;
67   return (grand_dopywrap(ty, r, f));
68 }
69
70 CONVFUNC(grand, grand *, GRAND_R)
71
72 static PyObject *grmeth_byte(PyObject *me, PyObject *arg)
73 {
74   if (!PyArg_ParseTuple(arg, ":byte")) return (0);
75   return (PyInt_FromLong(grand_byte(GRAND_R(me))));
76 }
77
78 static PyObject *grmeth_word(PyObject *me, PyObject *arg)
79 {
80   if (!PyArg_ParseTuple(arg, ":word")) return (0);
81   return (getulong(grand_word(GRAND_R(me))));
82 }
83
84 static PyObject *grmeth_range(PyObject *me, PyObject *arg)
85 {
86   PyObject *m;
87   mp *x = 0;
88   mp *y = 0;
89
90   if (!PyArg_ParseTuple(arg, "O:range", &m)) return (0);
91   if (PyInt_Check(m)) {
92     long mm = PyInt_AS_LONG(m);
93     if (mm < 0)
94       goto negative;
95     if (mm <= 0xffffffff)
96       return (PyInt_FromLong(grand_range(GRAND_R(me), mm)));
97   }
98   if ((x = getmp(m)) == 0)
99     goto end;
100   if (MP_NEGP(x))
101     goto negative;
102   y = mprand_range(MP_NEW, x, GRAND_R(me), 0);
103   MP_DROP(x);
104   return (mp_pywrap(y));
105 negative:
106   TYERR("range must be nonnegative");
107 end:
108   if (x) MP_DROP(x);
109   return (0);
110 }
111
112 static PyObject *grmeth_mp(PyObject *me, PyObject *arg, PyObject *kw)
113 {
114   size_t l;
115   mpw o;
116   char *kwlist[] = { "bits", "or", 0 };
117
118   if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:mp", kwlist,
119                                    convszt, &l, convmpw, &o))
120     goto end;
121   return (mp_pywrap(mprand(MP_NEW, l, GRAND_R(me), o)));
122 end:
123   return (0);
124 }
125
126 static PyObject *grmeth_block(PyObject *me, PyObject *arg)
127 {
128   unsigned long n;
129   PyObject *rc = 0;
130
131   if (!PyArg_ParseTuple(arg, "O&:block", convulong, &n)) goto end;
132   rc = bytestring_pywrap(0, n);
133   grand_fill(GRAND_R(me), PyString_AS_STRING(rc), n);
134 end:
135   return (rc);
136 }
137
138 static int checkop(grand *r, unsigned op, const char *what)
139 {
140   if (r->ops->misc(r, GRAND_CHECK, op))
141     return (0);
142   PyErr_Format(PyExc_TypeError, "operation %s not supported", what);
143   return (-1);
144 }
145
146 static PyObject *grmeth_seedint(PyObject *me, PyObject *arg)
147 {
148   int i;
149   grand *r = GRAND_R(me);
150   if (!PyArg_ParseTuple(arg, "i:seedint", &i) ||
151       checkop(r, GRAND_SEEDINT, "seedint"))
152     goto end;
153   r->ops->misc(r, GRAND_SEEDINT, i);
154   RETURN_ME;
155 end:
156   return (0);
157 }
158
159 static PyObject *grmeth_seedword(PyObject *me, PyObject *arg)
160 {
161   uint32 u;
162   grand *r = GRAND_R(me);
163   if (!PyArg_ParseTuple(arg, "O&:seedword", convu32, &u) ||
164       checkop(r, GRAND_SEEDUINT32, "seedword"))
165     goto end;
166   r->ops->misc(r, GRAND_SEEDUINT32, u);
167   RETURN_ME;
168 end:
169   return (0);
170 }
171
172 static PyObject *grmeth_seedblock(PyObject *me, PyObject *arg)
173 {
174   char *p;
175   int n;
176   grand *r = GRAND_R(me);
177   if (!PyArg_ParseTuple(arg, "s#:seedblock", &p, &n) ||
178       checkop(r, GRAND_SEEDBLOCK, "seedblock"))
179     goto end;
180   r->ops->misc(r, GRAND_SEEDBLOCK, p, (size_t)n);
181   RETURN_ME;
182 end:
183   return (0);
184 }
185
186 static PyObject *grmeth_seedmp(PyObject *me, PyObject *arg)
187 {
188   PyObject *x;
189   mp *xx;
190   grand *r = GRAND_R(me);
191   if (!PyArg_ParseTuple(arg, "O:seedmp", &x) ||
192       checkop(r, GRAND_SEEDMP, "seedmp") ||
193       (xx = getmp(x)) == 0)
194     goto end;
195   r->ops->misc(r, GRAND_SEEDMP, xx);
196   MP_DROP(xx);
197   RETURN_ME;
198 end:
199   return (0);
200 }
201
202 static PyObject *grmeth_seedrand(PyObject *me, PyObject *arg, PyObject *kw)
203 {
204   char *kwlist[] = { "rng", 0 };
205   grand *r = GRAND_R(me);
206   grand *rr = &rand_global;
207   if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:seedrand", kwlist,
208                                    convgrand, &rr) ||
209       checkop(r, GRAND_SEEDRAND, "seedrand"))
210     goto end;
211   r->ops->misc(r, GRAND_SEEDRAND, rr);
212   RETURN_ME;
213 end:
214   return (0);
215 }
216
217 static PyObject *grmeth_mask(PyObject *me, PyObject *arg)
218 {
219   grand *r = GRAND_R(me);
220   char *p, *q;
221   int sz;
222   PyObject *rc;
223
224   if (!PyArg_ParseTuple(arg, "s#:mask", &p, &sz)) return (0);
225   rc = bytestring_pywrap(0, sz);
226   q = PyString_AS_STRING(rc);
227   GR_FILL(r, q, sz);
228   while (sz--) *q++ ^= *p++;
229   return (rc);
230 }
231
232 static void grand_pydealloc(PyObject *me)
233 {
234   grand_pyobj *g = (grand_pyobj *)me;
235   if (g->f & f_freeme)
236     GR_DESTROY(g->r);
237   FREEOBJ(me);
238 }
239
240 static PyObject *grget_name(PyObject *me, void *hunoz)
241   { return (PyString_FromString(GRAND_R(me)->ops->name)); }
242
243 static PyObject *grget_cryptop(PyObject *me, void *hunoz)
244   { return (getbool(GRAND_R(me)->ops->f & GRAND_CRYPTO)); }
245
246 static PyGetSetDef grand_pygetset[] = {
247 #define GETSETNAME(op, name) gr##op##_##name
248   GET   (name,          "R.name -> name of this kind of generator")
249   GET   (cryptop,       "R.cryptop -> flag: cryptographically strong?")
250 #undef GETSETNAME
251   { 0 }
252 };
253
254 static PyMethodDef grand_pymethods[] = {
255 #define METHNAME(name) grmeth_##name
256   METH  (byte,          "R.byte() -> BYTE")
257   METH  (word,          "R.word() -> WORD")
258   METH  (block,         "R.block(N) -> STRING")
259   KWMETH(mp,            "R.mp(bits, or = 0) -> MP")
260   METH  (range,         "R.range(MAX) -> INT")
261   METH  (mask,          "R.mask(STR) -> STR")
262   METH  (seedint,       "R.seedint(I)")
263   METH  (seedword,      "R.seedword(I)")
264   METH  (seedblock,     "R.seedblock(BYTES)")
265   METH  (seedmp,        "R.seedmp(X)")
266   KWMETH(seedrand,      "R.seedrand(RR)")
267 #undef METHNAME
268   { 0 }
269 };
270
271 static PyTypeObject grand_pytype_skel = {
272   PyObject_HEAD_INIT(0) 0,              /* Header */
273   "catacomb.GRand",                     /* @tp_name@ */
274   sizeof(grand_pyobj),                  /* @tp_basicsize@ */
275   0,                                    /* @tp_itemsize@ */
276
277   grand_pydealloc,                      /* @tp_dealloc@ */
278   0,                                    /* @tp_print@ */
279   0,                                    /* @tp_getattr@ */
280   0,                                    /* @tp_setattr@ */
281   0,                                    /* @tp_compare@ */
282   0,                                    /* @tp_repr@ */
283   0,                                    /* @tp_as_number@ */
284   0,                                    /* @tp_as_sequence@ */
285   0,                                    /* @tp_as_mapping@ */
286   0,                                    /* @tp_hash@ */
287   0,                                    /* @tp_call@ */
288   0,                                    /* @tp_str@ */
289   0,                                    /* @tp_getattro@ */
290   0,                                    /* @tp_setattro@ */
291   0,                                    /* @tp_as_buffer@ */
292   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
293     Py_TPFLAGS_BASETYPE,
294
295   /* @tp_doc@ */
296 "Generic random number source.",
297
298   0,                                    /* @tp_traverse@ */
299   0,                                    /* @tp_clear@ */
300   0,                                    /* @tp_richcompare@ */
301   0,                                    /* @tp_weaklistoffset@ */
302   0,                                    /* @tp_iter@ */
303   0,                                    /* @tp_iternext@ */
304   grand_pymethods,                      /* @tp_methods@ */
305   0,                                    /* @tp_members@ */
306   grand_pygetset,                       /* @tp_getset@ */
307   0,                                    /* @tp_base@ */
308   0,                                    /* @tp_dict@ */
309   0,                                    /* @tp_descr_get@ */
310   0,                                    /* @tp_descr_set@ */
311   0,                                    /* @tp_dictoffset@ */
312   0,                                    /* @tp_init@ */
313   PyType_GenericAlloc,                  /* @tp_alloc@ */
314   abstract_pynew,                       /* @tp_new@ */
315   0,                                    /* @tp_free@ */
316   0                                     /* @tp_is_gc@ */
317 };
318
319 static PyObject *lcrand_pynew(PyTypeObject *me, PyObject *arg, PyObject *kw)
320 {
321   uint32 n = 0;
322   char *kwlist[] = { "seed", 0 };
323   if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", kwlist, convu32, &n))
324     return (0);
325   return (grand_dopywrap(lcrand_pytype, lcrand_create(n), f_freeme));
326 }
327
328 static PyTypeObject lcrand_pytype_skel = {
329   PyObject_HEAD_INIT(0) 0,              /* Header */
330   "catacomb.LCRand",                    /* @tp_name@ */
331   sizeof(grand_pyobj),                  /* @tp_basicsize@ */
332   0,                                    /* @tp_itemsize@ */
333
334   grand_pydealloc,                      /* @tp_dealloc@ */
335   0,                                    /* @tp_print@ */
336   0,                                    /* @tp_getattr@ */
337   0,                                    /* @tp_setattr@ */
338   0,                                    /* @tp_compare@ */
339   0,                                    /* @tp_repr@ */
340   0,                                    /* @tp_as_number@ */
341   0,                                    /* @tp_as_sequence@ */
342   0,                                    /* @tp_as_mapping@ */
343   0,                                    /* @tp_hash@ */
344   0,                                    /* @tp_call@ */
345   0,                                    /* @tp_str@ */
346   0,                                    /* @tp_getattro@ */
347   0,                                    /* @tp_setattro@ */
348   0,                                    /* @tp_as_buffer@ */
349   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
350     Py_TPFLAGS_BASETYPE,
351
352   /* @tp_doc@ */
353 "Linear congruential generator.",
354
355   0,                                    /* @tp_traverse@ */
356   0,                                    /* @tp_clear@ */
357   0,                                    /* @tp_richcompare@ */
358   0,                                    /* @tp_weaklistoffset@ */
359   0,                                    /* @tp_iter@ */
360   0,                                    /* @tp_iternext@ */
361   0,                                    /* @tp_methods@ */
362   0,                                    /* @tp_members@ */
363   0,                                    /* @tp_getset@ */
364   0,                                    /* @tp_base@ */
365   0,                                    /* @tp_dict@ */
366   0,                                    /* @tp_descr_get@ */
367   0,                                    /* @tp_descr_set@ */
368   0,                                    /* @tp_dictoffset@ */
369   0,                                    /* @tp_init@ */
370   PyType_GenericAlloc,                  /* @tp_alloc@ */
371   lcrand_pynew,                         /* @tp_new@ */
372   0,                                    /* @tp_free@ */
373   0                                     /* @tp_is_gc@ */
374 };
375
376 static PyObject *fibrand_pynew(PyTypeObject *me, PyObject *arg, PyObject *kw)
377 {
378   uint32 n = 0;
379   char *kwlist[] = { "seed", 0 };
380   if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", kwlist, convu32, &n))
381     return (0);
382   return (grand_dopywrap(fibrand_pytype, fibrand_create(n), f_freeme));
383 }
384
385 static PyTypeObject fibrand_pytype_skel = {
386   PyObject_HEAD_INIT(0) 0,              /* Header */
387   "catacomb.FibRand",                   /* @tp_name@ */
388   sizeof(grand_pyobj),                  /* @tp_basicsize@ */
389   0,                                    /* @tp_itemsize@ */
390
391   grand_pydealloc,                      /* @tp_dealloc@ */
392   0,                                    /* @tp_print@ */
393   0,                                    /* @tp_getattr@ */
394   0,                                    /* @tp_setattr@ */
395   0,                                    /* @tp_compare@ */
396   0,                                    /* @tp_repr@ */
397   0,                                    /* @tp_as_number@ */
398   0,                                    /* @tp_as_sequence@ */
399   0,                                    /* @tp_as_mapping@ */
400   0,                                    /* @tp_hash@ */
401   0,                                    /* @tp_call@ */
402   0,                                    /* @tp_str@ */
403   0,                                    /* @tp_getattro@ */
404   0,                                    /* @tp_setattro@ */
405   0,                                    /* @tp_as_buffer@ */
406   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
407     Py_TPFLAGS_BASETYPE,
408
409   /* @tp_doc@ */
410 "Fibonacci generator.",
411
412   0,                                    /* @tp_traverse@ */
413   0,                                    /* @tp_clear@ */
414   0,                                    /* @tp_richcompare@ */
415   0,                                    /* @tp_weaklistoffset@ */
416   0,                                    /* @tp_iter@ */
417   0,                                    /* @tp_iternext@ */
418   0,                                    /* @tp_methods@ */
419   0,                                    /* @tp_members@ */
420   0,                                    /* @tp_getset@ */
421   0,                                    /* @tp_base@ */
422   0,                                    /* @tp_dict@ */
423   0,                                    /* @tp_descr_get@ */
424   0,                                    /* @tp_descr_set@ */
425   0,                                    /* @tp_dictoffset@ */
426   0,                                    /* @tp_init@ */
427   PyType_GenericAlloc,                  /* @tp_alloc@ */
428   fibrand_pynew,                        /* @tp_new@ */
429   0,                                    /* @tp_free@ */
430   0                                     /* @tp_is_gc@ */
431 };
432
433 /*----- True random generator ---------------------------------------------*/
434
435 static PyObject *trmeth_gate(PyObject *me, PyObject *arg)
436 {
437   grand *r = GRAND_R(me);
438   if (!PyArg_ParseTuple(arg, ":gate")) return (0);
439   r->ops->misc(r, RAND_GATE);
440   RETURN_ME;
441 }
442
443 static PyObject *trmeth_stretch(PyObject *me, PyObject *arg)
444 {
445   grand *r = GRAND_R(me);
446   if (!PyArg_ParseTuple(arg, ":stretch")) return (0);
447   r->ops->misc(r, RAND_STRETCH);
448   RETURN_ME;
449 }
450
451 static PyObject *trmeth_add(PyObject *me, PyObject *arg)
452 {
453   grand *r = GRAND_R(me);
454   char *p; int n; unsigned goodbits;
455   if (!PyArg_ParseTuple(arg, "s#O&:add", &p, &n, convuint, &goodbits))
456     return (0);
457   r->ops->misc(r, RAND_ADD, p, (size_t)n, goodbits);
458   RETURN_ME;
459 }
460
461 static PyObject *trmeth_key(PyObject *me, PyObject *arg)
462 {
463   grand *r = GRAND_R(me);
464   char *p; int n;
465   if (!PyArg_ParseTuple(arg, "s#:key", &p, &n)) return (0);
466   r->ops->misc(r, RAND_KEY, p, (size_t)n);
467   RETURN_ME;
468 }
469
470 static PyObject *trmeth_seed(PyObject *me, PyObject *arg)
471 {
472   grand *r = GRAND_R(me);
473   unsigned u;
474   if (!PyArg_ParseTuple(arg, "O&:seed", convuint, &u)) return (0);
475   if (u > RAND_IBITS) VALERR("pointlessly large");
476   r->ops->misc(r, RAND_SEED, u);
477   RETURN_ME;
478 end:
479   return (0);
480 }
481
482 static PyObject *trmeth_timer(PyObject *me, PyObject *arg)
483 {
484   grand *r = GRAND_R(me);
485   if (!PyArg_ParseTuple(arg, ":timer")) return (0);
486   r->ops->misc(r, RAND_TIMER);
487   RETURN_ME;
488 }
489
490 static PyObject *truerand_pynew(PyTypeObject *ty,
491                                 PyObject *arg, PyObject *kw)
492 {
493   char *kwlist[] = { 0 };
494   grand *r;
495   PyObject *rc = 0;
496   if (PyArg_ParseTupleAndKeywords(arg, kw, ":new", kwlist)) goto end;
497   r = rand_create();
498   r->ops->misc(r, RAND_NOISESRC, &noise_source);
499   r->ops->misc(r, RAND_SEED, 160);
500   rc = grand_dopywrap(ty, r, f_freeme);
501 end:
502   return (rc);
503 }
504
505 static PyMethodDef truerand_pymethods[] = {
506 #define METHNAME(name) trmeth_##name
507   METH  (gate,          "R.gate()")
508   METH  (stretch,       "R.stretch()")
509   METH  (key,           "R.key(BYTES)")
510   METH  (seed,          "R.seed(NBITS)")
511   METH  (add,           "R.add(BYTES, GOODBITS")
512   METH  (timer,         "R.timer()")
513 #undef METHNAME
514   { 0 }
515 };
516
517 static PyObject *trget_goodbits(PyObject *me, void *hunoz)
518 {
519   grand *r = GRAND_R(me);
520   return (PyInt_FromLong(r->ops->misc(r, RAND_GOODBITS)));
521 }
522
523 static PyGetSetDef truerand_pygetset[] = {
524 #define GETSETNAME(op, name) tr##op##_##name
525   GET   (goodbits,      "R.goodbits -> good bits of entropy remaining")
526 #undef GETSETNAME
527   { 0 }
528 };
529
530 static PyTypeObject truerand_pytype_skel = {
531   PyObject_HEAD_INIT(0) 0,              /* Header */
532   "catacomb.TrueRand",                  /* @tp_name@ */
533   sizeof(grand_pyobj),                  /* @tp_basicsize@ */
534   0,                                    /* @tp_itemsize@ */
535
536   grand_pydealloc,                      /* @tp_dealloc@ */
537   0,                                    /* @tp_print@ */
538   0,                                    /* @tp_getattr@ */
539   0,                                    /* @tp_setattr@ */
540   0,                                    /* @tp_compare@ */
541   0,                                    /* @tp_repr@ */
542   0,                                    /* @tp_as_number@ */
543   0,                                    /* @tp_as_sequence@ */
544   0,                                    /* @tp_as_mapping@ */
545   0,                                    /* @tp_hash@ */
546   0,                                    /* @tp_call@ */
547   0,                                    /* @tp_str@ */
548   0,                                    /* @tp_getattro@ */
549   0,                                    /* @tp_setattro@ */
550   0,                                    /* @tp_as_buffer@ */
551   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
552     Py_TPFLAGS_BASETYPE,
553
554   /* @tp_doc@ */
555 "True random number source.",
556
557   0,                                    /* @tp_traverse@ */
558   0,                                    /* @tp_clear@ */
559   0,                                    /* @tp_richcompare@ */
560   0,                                    /* @tp_weaklistoffset@ */
561   0,                                    /* @tp_iter@ */
562   0,                                    /* @tp_iternext@ */
563   truerand_pymethods,                   /* @tp_methods@ */
564   0,                                    /* @tp_members@ */
565   truerand_pygetset,                    /* @tp_getset@ */
566   0,                                    /* @tp_base@ */
567   0,                                    /* @tp_dict@ */
568   0,                                    /* @tp_descr_get@ */
569   0,                                    /* @tp_descr_set@ */
570   0,                                    /* @tp_dictoffset@ */
571   0,                                    /* @tp_init@ */
572   PyType_GenericAlloc,                  /* @tp_alloc@ */
573   truerand_pynew,                       /* @tp_new@ */
574   0,                                    /* @tp_free@ */
575   0                                     /* @tp_is_gc@ */
576 };
577
578 /*----- Generators from symmetric encryption algorithms -------------------*/
579
580 static PyTypeObject *gccrand_pytype, *gcrand_pytype;
581
582 typedef grand *gcrand_func(const void *, size_t sz);
583 typedef grand *gcirand_func(const void *, size_t sz, uint32);
584 typedef struct gccrand_info {
585   const char *name;
586   const octet *keysz;
587   unsigned f;
588   gcrand_func *func;
589 } gccrand_info;
590
591 typedef struct gccrand_pyobj {
592   PyHeapTypeObject ty;
593   const gccrand_info *info;
594 } gccrand_pyobj;
595 #define GCCRAND_INFO(o) (((gccrand_pyobj *)(o))->info)
596
597 #define GCCRAND_DEF(name, ksz, func, f)                                 \
598   static const gccrand_info func##_info =                               \
599     { name, ksz, f, (gcrand_func *)func };
600 RNGS(GCCRAND_DEF)
601
602 static const gccrand_info *const gcrandtab[] = {
603 #define GCCRAND_ENTRY(name, ksz, func, f) &func##_info,
604   RNGS(GCCRAND_ENTRY)
605   0
606 };
607
608 static PyObject *gcrand_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
609 {
610   const gccrand_info *info = GCCRAND_INFO(ty);
611   static char *kwlist[] = { "key", 0 };
612   char *k;
613   int n;
614
615   if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &n))
616     goto end;
617   if (keysz(n, info->keysz) != n) VALERR("bad key length");
618   return (grand_dopywrap(ty, info->func(k, n), f_freeme));
619 end:
620   return (0);
621 }
622
623 static PyObject *gcirand_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
624 {
625   const gccrand_info *info = GCCRAND_INFO(ty);
626   uint32 i = 0;
627   static char *kwlist[] = { "key", "i", 0 };
628   char *k;
629   int n;
630
631   if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#O&:new", kwlist,
632                                    &k, &n, convu32, &i))
633     goto end;
634   if (keysz(n, info->keysz) != n) VALERR("bad key length");
635   return (grand_dopywrap(ty,
636                          ((gcirand_func *)info->func)(k, n, i),
637                          f_freeme));
638 end:
639   return (0);
640 }
641
642 static PyObject *gccrand_pywrap(const gccrand_info *info)
643 {
644   gccrand_pyobj *g = newtype(gccrand_pytype, 0, info->name);
645   g->info = info;
646   g->ty.ht_type.tp_basicsize = sizeof(grand_pyobj);
647   g->ty.ht_type.tp_base = gcrand_pytype;
648   Py_INCREF(gcrand_pytype);
649   g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
650                             Py_TPFLAGS_BASETYPE |
651                             Py_TPFLAGS_HEAPTYPE);
652   g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
653   g->ty.ht_type.tp_free = 0;
654   if (info->f & RNGF_INT)
655     g->ty.ht_type.tp_new = gcirand_pynew;
656   else
657     g->ty.ht_type.tp_new = gcrand_pynew;
658   PyType_Ready(&g->ty.ht_type);
659   return ((PyObject *)g);
660 }
661
662 static PyObject *gccrget_name(PyObject *me, void *hunoz)
663   { return (PyString_FromString(GCCRAND_INFO(me)->name)); }
664 static PyObject *gccrget_keysz(PyObject *me, void *hunoz)
665   { return (keysz_pywrap(GCCRAND_INFO(me)->keysz)); }
666
667 static PyGetSetDef gccrand_pygetset[] = {
668 #define GETSETNAME(op, name) gccr##op##_##name
669   GET   (keysz,                 "CR.keysz -> acceptable key sizes")
670   GET   (name,                  "CR.name -> name of this kind of generator")
671 #undef GETSETNAME
672   { 0 }
673 };
674
675 static PyTypeObject gccrand_pytype_skel = {
676   PyObject_HEAD_INIT(0) 0,              /* Header */
677   "catacomb.GCCRand",                   /* @tp_name@ */
678   sizeof(gccrand_pyobj),                /* @tp_basicsize@ */
679   0,                                    /* @tp_itemsize@ */
680
681   0,                                    /* @tp_dealloc@ */
682   0,                                    /* @tp_print@ */
683   0,                                    /* @tp_getattr@ */
684   0,                                    /* @tp_setattr@ */
685   0,                                    /* @tp_compare@ */
686   0,                                    /* @tp_repr@ */
687   0,                                    /* @tp_as_number@ */
688   0,                                    /* @tp_as_sequence@ */
689   0,                                    /* @tp_as_mapping@ */
690   0,                                    /* @tp_hash@ */
691   0,                                    /* @tp_call@ */
692   0,                                    /* @tp_str@ */
693   0,                                    /* @tp_getattro@ */
694   0,                                    /* @tp_setattro@ */
695   0,                                    /* @tp_as_buffer@ */
696   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
697     Py_TPFLAGS_BASETYPE,
698
699   /* @tp_doc@ */
700 "Metaclass for symmetric crypto-based generators.",
701
702   0,                                    /* @tp_traverse@ */
703   0,                                    /* @tp_clear@ */
704   0,                                    /* @tp_richcompare@ */
705   0,                                    /* @tp_weaklistoffset@ */
706   0,                                    /* @tp_iter@ */
707   0,                                    /* @tp_iternext@ */
708   0,                                    /* @tp_methods@ */
709   0,                                    /* @tp_members@ */
710   gccrand_pygetset,                     /* @tp_getset@ */
711   0,                                    /* @tp_base@ */
712   0,                                    /* @tp_dict@ */
713   0,                                    /* @tp_descr_get@ */
714   0,                                    /* @tp_descr_set@ */
715   0,                                    /* @tp_dictoffset@ */
716   0,                                    /* @tp_init@ */
717   PyType_GenericAlloc,                  /* @tp_alloc@ */
718   abstract_pynew,                       /* @tp_new@ */
719   0,                                    /* @tp_free@ */
720   0                                     /* @tp_is_gc@ */
721 };
722
723 static PyTypeObject gcrand_pytype_skel = {
724   PyObject_HEAD_INIT(0) 0,              /* Header */
725   "catacomb.GCRand",                    /* @tp_name@ */
726   sizeof(grand_pyobj),                  /* @tp_basicsize@ */
727   0,                                    /* @tp_itemsize@ */
728
729   grand_pydealloc,                      /* @tp_dealloc@ */
730   0,                                    /* @tp_print@ */
731   0,                                    /* @tp_getattr@ */
732   0,                                    /* @tp_setattr@ */
733   0,                                    /* @tp_compare@ */
734   0,                                    /* @tp_repr@ */
735   0,                                    /* @tp_as_number@ */
736   0,                                    /* @tp_as_sequence@ */
737   0,                                    /* @tp_as_mapping@ */
738   0,                                    /* @tp_hash@ */
739   0,                                    /* @tp_call@ */
740   0,                                    /* @tp_str@ */
741   0,                                    /* @tp_getattro@ */
742   0,                                    /* @tp_setattro@ */
743   0,                                    /* @tp_as_buffer@ */
744   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
745     Py_TPFLAGS_BASETYPE,
746
747   /* @tp_doc@ */
748 "Abstract base class for symmetric crypto-based generators.",
749
750   0,                                    /* @tp_traverse@ */
751   0,                                    /* @tp_clear@ */
752   0,                                    /* @tp_richcompare@ */
753   0,                                    /* @tp_weaklistoffset@ */
754   0,                                    /* @tp_iter@ */
755   0,                                    /* @tp_iternext@ */
756   0,                                    /* @tp_methods@ */
757   0,                                    /* @tp_members@ */
758   0,                                    /* @tp_getset@ */
759   0,                                    /* @tp_base@ */
760   0,                                    /* @tp_dict@ */
761   0,                                    /* @tp_descr_get@ */
762   0,                                    /* @tp_descr_set@ */
763   0,                                    /* @tp_dictoffset@ */
764   0,                                    /* @tp_init@ */
765   PyType_GenericAlloc,                  /* @tp_alloc@ */
766   abstract_pynew,                       /* @tp_new@ */
767   0,                                    /* @tp_free@ */
768   0                                     /* @tp_is_gc@ */
769 };
770
771 /*----- SSL and TLS generators --------------------------------------------*/
772
773 static PyObject *sslprf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
774 {
775   char *k, *s;
776   int ksz, ssz;
777   const gchash *hco = &md5, *hci = &sha;
778   PyObject *rc = 0;
779   char *kwlist[] = { "key", "seed", "ohash", "ihash", 0 };
780
781   if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#s#|O&O&:new", kwlist,
782                                    &k, &ksz, &s, &ssz,
783                                    convgchash, &hco, convgchash, &hci))
784     goto end;
785   rc = grand_dopywrap(ty, sslprf_rand(hco, hci, k, ksz, s, ssz), f_freeme);
786 end:
787   return (rc);
788 }
789
790 static PyObject *tlsdx_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
791 {
792   char *k, *s;
793   int ksz, ssz;
794   const gcmac *mc = &sha_hmac;
795   PyObject *rc = 0;
796   char *kwlist[] = { "key", "seed", "mac", 0 };
797
798   if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#s#|O&:new", kwlist,
799                                    &k, &ksz, &s, &ssz,
800                                    convgcmac, &mc))
801     goto end;
802   rc = grand_dopywrap(ty, tlsdx_rand(mc, k, ksz, s, ssz), f_freeme);
803 end:
804   return (rc);
805 }
806
807 static PyObject *tlsprf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
808 {
809   char *k, *s;
810   int ksz, ssz;
811   const gcmac *mcl = &md5_hmac, *mcr = &sha_hmac;
812   PyObject *rc = 0;
813   char *kwlist[] = { "key", "seed", "lmac", "rmac", 0 };
814
815   if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#s#|O&O&:new", kwlist,
816                                    &k, &ksz, &s, &ssz,
817                                    convgcmac, &mcl, convgcmac, &mcr))
818     goto end;
819   rc = grand_dopywrap(ty, tlsprf_rand(mcl, mcr, k, ksz, s, ssz), f_freeme);
820 end:
821   return (rc);
822 }
823
824 static PyTypeObject sslprf_pytype_skel = {
825   PyObject_HEAD_INIT(0) 0,              /* Header */
826   "catacomb.SSLRand",                   /* @tp_name@ */
827   sizeof(grand_pyobj),                  /* @tp_basicsize@ */
828   0,                                    /* @tp_itemsize@ */
829
830   grand_pydealloc,                      /* @tp_dealloc@ */
831   0,                                    /* @tp_print@ */
832   0,                                    /* @tp_getattr@ */
833   0,                                    /* @tp_setattr@ */
834   0,                                    /* @tp_compare@ */
835   0,                                    /* @tp_repr@ */
836   0,                                    /* @tp_as_number@ */
837   0,                                    /* @tp_as_sequence@ */
838   0,                                    /* @tp_as_mapping@ */
839   0,                                    /* @tp_hash@ */
840   0,                                    /* @tp_call@ */
841   0,                                    /* @tp_str@ */
842   0,                                    /* @tp_getattro@ */
843   0,                                    /* @tp_setattro@ */
844   0,                                    /* @tp_as_buffer@ */
845   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
846     Py_TPFLAGS_BASETYPE,
847
848   /* @tp_doc@ */
849 "Random number generator for SSL master secret.",
850
851   0,                                    /* @tp_traverse@ */
852   0,                                    /* @tp_clear@ */
853   0,                                    /* @tp_richcompare@ */
854   0,                                    /* @tp_weaklistoffset@ */
855   0,                                    /* @tp_iter@ */
856   0,                                    /* @tp_iternext@ */
857   0,                                    /* @tp_methods@ */
858   0,                                    /* @tp_members@ */
859   0,                                    /* @tp_getset@ */
860   0,                                    /* @tp_base@ */
861   0,                                    /* @tp_dict@ */
862   0,                                    /* @tp_descr_get@ */
863   0,                                    /* @tp_descr_set@ */
864   0,                                    /* @tp_dictoffset@ */
865   0,                                    /* @tp_init@ */
866   PyType_GenericAlloc,                  /* @tp_alloc@ */
867   sslprf_pynew,                         /* @tp_new@ */
868   0,                                    /* @tp_free@ */
869   0                                     /* @tp_is_gc@ */
870 };
871
872 static PyTypeObject tlsdx_pytype_skel = {
873   PyObject_HEAD_INIT(0) 0,              /* Header */
874   "catacomb.TLSDataExpansion",          /* @tp_name@ */
875   sizeof(grand_pyobj),                  /* @tp_basicsize@ */
876   0,                                    /* @tp_itemsize@ */
877
878   grand_pydealloc,                      /* @tp_dealloc@ */
879   0,                                    /* @tp_print@ */
880   0,                                    /* @tp_getattr@ */
881   0,                                    /* @tp_setattr@ */
882   0,                                    /* @tp_compare@ */
883   0,                                    /* @tp_repr@ */
884   0,                                    /* @tp_as_number@ */
885   0,                                    /* @tp_as_sequence@ */
886   0,                                    /* @tp_as_mapping@ */
887   0,                                    /* @tp_hash@ */
888   0,                                    /* @tp_call@ */
889   0,                                    /* @tp_str@ */
890   0,                                    /* @tp_getattro@ */
891   0,                                    /* @tp_setattro@ */
892   0,                                    /* @tp_as_buffer@ */
893   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
894     Py_TPFLAGS_BASETYPE,
895
896   /* @tp_doc@ */
897 "TLS data expansion function.",
898
899   0,                                    /* @tp_traverse@ */
900   0,                                    /* @tp_clear@ */
901   0,                                    /* @tp_richcompare@ */
902   0,                                    /* @tp_weaklistoffset@ */
903   0,                                    /* @tp_iter@ */
904   0,                                    /* @tp_iternext@ */
905   0,                                    /* @tp_methods@ */
906   0,                                    /* @tp_members@ */
907   0,                                    /* @tp_getset@ */
908   0,                                    /* @tp_base@ */
909   0,                                    /* @tp_dict@ */
910   0,                                    /* @tp_descr_get@ */
911   0,                                    /* @tp_descr_set@ */
912   0,                                    /* @tp_dictoffset@ */
913   0,                                    /* @tp_init@ */
914   PyType_GenericAlloc,                  /* @tp_alloc@ */
915   tlsdx_pynew,                          /* @tp_new@ */
916   0,                                    /* @tp_free@ */
917   0                                     /* @tp_is_gc@ */
918 };
919
920 static PyTypeObject tlsprf_pytype_skel = {
921   PyObject_HEAD_INIT(0) 0,              /* Header */
922   "catacomb.TLSPRF",                    /* @tp_name@ */
923   sizeof(grand_pyobj),                  /* @tp_basicsize@ */
924   0,                                    /* @tp_itemsize@ */
925
926   grand_pydealloc,                      /* @tp_dealloc@ */
927   0,                                    /* @tp_print@ */
928   0,                                    /* @tp_getattr@ */
929   0,                                    /* @tp_setattr@ */
930   0,                                    /* @tp_compare@ */
931   0,                                    /* @tp_repr@ */
932   0,                                    /* @tp_as_number@ */
933   0,                                    /* @tp_as_sequence@ */
934   0,                                    /* @tp_as_mapping@ */
935   0,                                    /* @tp_hash@ */
936   0,                                    /* @tp_call@ */
937   0,                                    /* @tp_str@ */
938   0,                                    /* @tp_getattro@ */
939   0,                                    /* @tp_setattro@ */
940   0,                                    /* @tp_as_buffer@ */
941   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
942     Py_TPFLAGS_BASETYPE,
943
944   /* @tp_doc@ */
945 "TLS pseudorandom function.",
946
947   0,                                    /* @tp_traverse@ */
948   0,                                    /* @tp_clear@ */
949   0,                                    /* @tp_richcompare@ */
950   0,                                    /* @tp_weaklistoffset@ */
951   0,                                    /* @tp_iter@ */
952   0,                                    /* @tp_iternext@ */
953   0,                                    /* @tp_methods@ */
954   0,                                    /* @tp_members@ */
955   0,                                    /* @tp_getset@ */
956   0,                                    /* @tp_base@ */
957   0,                                    /* @tp_dict@ */
958   0,                                    /* @tp_descr_get@ */
959   0,                                    /* @tp_descr_set@ */
960   0,                                    /* @tp_dictoffset@ */
961   0,                                    /* @tp_init@ */
962   PyType_GenericAlloc,                  /* @tp_alloc@ */
963   tlsprf_pynew,                         /* @tp_new@ */
964   0,                                    /* @tp_free@ */
965   0                                     /* @tp_is_gc@ */
966 };
967
968 /*----- DSA generator -----------------------------------------------------*/
969
970 static PyObject *dsarand_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
971 {
972   char *p;
973   int sz;
974   PyObject *rc = 0;
975   char *kwlist[] = { "seed", 0 };
976
977   if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &p, &sz))
978     goto end;
979   rc = grand_dopywrap(ty, dsarand_create(p, sz), f_freeme);
980 end:
981   return (rc);
982 }
983
984 static PyObject *drget_seed(PyObject *me, void *hunoz)
985 {
986   grand *r = GRAND_R(me);
987   int n = r->ops->misc(r, DSARAND_SEEDSZ);
988   PyObject *rc = bytestring_pywrap(0, n);
989   r->ops->misc(r, DSARAND_GETSEED, PyString_AS_STRING(rc));
990   return (rc);
991 }
992
993 static PyGetSetDef dsarand_pygetset[] = {
994 #define GETSETNAME(op, name) dr##op##_##name
995   GET   (seed,          "R.seed -> current generator seed")
996 #undef GETSETNAME
997   { 0 }
998 };
999
1000 static PyTypeObject dsarand_pytype_skel = {
1001   PyObject_HEAD_INIT(0) 0,              /* Header */
1002   "catacomb.DSARand",                   /* @tp_name@ */
1003   sizeof(grand_pyobj),                  /* @tp_basicsize@ */
1004   0,                                    /* @tp_itemsize@ */
1005
1006   grand_pydealloc,                      /* @tp_dealloc@ */
1007   0,                                    /* @tp_print@ */
1008   0,                                    /* @tp_getattr@ */
1009   0,                                    /* @tp_setattr@ */
1010   0,                                    /* @tp_compare@ */
1011   0,                                    /* @tp_repr@ */
1012   0,                                    /* @tp_as_number@ */
1013   0,                                    /* @tp_as_sequence@ */
1014   0,                                    /* @tp_as_mapping@ */
1015   0,                                    /* @tp_hash@ */
1016   0,                                    /* @tp_call@ */
1017   0,                                    /* @tp_str@ */
1018   0,                                    /* @tp_getattro@ */
1019   0,                                    /* @tp_setattro@ */
1020   0,                                    /* @tp_as_buffer@ */
1021   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
1022     Py_TPFLAGS_BASETYPE,
1023
1024   /* @tp_doc@ */
1025 "Pseudorandom number generator for constructing DSA parameters.",
1026
1027   0,                                    /* @tp_traverse@ */
1028   0,                                    /* @tp_clear@ */
1029   0,                                    /* @tp_richcompare@ */
1030   0,                                    /* @tp_weaklistoffset@ */
1031   0,                                    /* @tp_iter@ */
1032   0,                                    /* @tp_iternext@ */
1033   0,                                    /* @tp_methods@ */
1034   0,                                    /* @tp_members@ */
1035   dsarand_pygetset,                     /* @tp_getset@ */
1036   0,                                    /* @tp_base@ */
1037   0,                                    /* @tp_dict@ */
1038   0,                                    /* @tp_descr_get@ */
1039   0,                                    /* @tp_descr_set@ */
1040   0,                                    /* @tp_dictoffset@ */
1041   0,                                    /* @tp_init@ */
1042   PyType_GenericAlloc,                  /* @tp_alloc@ */
1043   dsarand_pynew,                        /* @tp_new@ */
1044   0,                                    /* @tp_free@ */
1045   0                                     /* @tp_is_gc@ */
1046 };
1047
1048 /*----- Blum-Blum-Shub generator ------------------------------------------*/
1049
1050 static PyObject *bbs_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1051 {
1052   mp *n = 0, *x = MP_TWO;
1053   PyObject *rc = 0;
1054   char *kwlist[] = { "n", "x", 0 };
1055
1056   if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:new", kwlist,
1057                                    convmp, &n, convmp, &x))
1058     goto end;
1059   rc = grand_dopywrap(ty, bbs_rand(n, x), f_freeme);
1060 end:
1061   mp_drop(n);
1062   mp_drop(x);
1063   return (rc);
1064 }
1065
1066 static PyObject *bbsmeth_step(PyObject *me, PyObject *arg)
1067 {
1068   grand *r = GRAND_R(me); if (!PyArg_ParseTuple(arg, ":step")) return (0);
1069   r->ops->misc(r, BBS_STEP); RETURN_ME;
1070 }
1071
1072 static PyObject *bbsmeth_bits(PyObject *me, PyObject *arg)
1073 {
1074   grand *r = GRAND_R(me); unsigned n; uint32 w;
1075   if (!PyArg_ParseTuple(arg, "O&:bits", convuint, &n)) goto end;
1076   if (n > 32) VALERR("can't get more than 32 bits");
1077   r->ops->misc(r, BBS_BITS, n, &w); return (getulong(w));
1078 end:
1079   return (0);
1080 }
1081
1082 static PyObject *bbsmeth_wrap(PyObject *me, PyObject *arg)
1083 {
1084   grand *r = GRAND_R(me); if (!PyArg_ParseTuple(arg, ":wrap")) return (0);
1085   r->ops->misc(r, BBS_WRAP); RETURN_ME;
1086 }
1087
1088 static PyObject *bbsget_n(PyObject *me, void *hunoz)
1089 {
1090   mp *x = MP_NEW; grand *r = GRAND_R(me);
1091   r->ops->misc(r, BBS_MOD, &x); return (mp_pywrap(x));
1092 }
1093
1094 static PyObject *bbsget_x(PyObject *me, void *hunoz)
1095 {
1096   mp *x = MP_NEW; grand *r = GRAND_R(me);
1097   r->ops->misc(r, BBS_STATE, &x); return (mp_pywrap(x));
1098 }
1099
1100 static int bbsset_x(PyObject *me, PyObject *val, void *hunoz)
1101 {
1102   mp *x = 0; grand *r = GRAND_R(me); int rc = -1; if (!x) NIERR("__del__");
1103   if ((x = getmp(val)) == 0) goto end; r->ops->misc(r, BBS_SET, x); rc = 0;
1104   end: mp_drop(x); return (rc);
1105 }
1106
1107 static PyObject *bbsget_stepsz(PyObject *me, void *hunoz)
1108 {
1109   grand *r = GRAND_R(me);
1110   return (PyInt_FromLong(r->ops->misc(r, BBS_STEPSZ)));
1111 }
1112
1113 static PyMethodDef bbs_pymethods[] = {
1114 #define METHNAME(name) bbsmeth_##name
1115   METH  (step,  "R.step(): steps the generator (not useful)")
1116   METH  (bits,  "R.bits(N) -> W: returns N bits (<= 32) from the generator")
1117   METH  (wrap,  "R.wrap(): flushes unused bits in internal buffer")
1118 #undef METHNAME
1119   { 0 }
1120 };
1121
1122 static PyGetSetDef bbs_pygetset[] = {
1123 #define GETSETNAME(op, name) bbs##op##_##name
1124   GET   (n,             "R.n -> Blum modulus")
1125   GETSET(x,             "R.x -> current seed value")
1126   GET   (stepsz,        "R.stepsz -> number of bits generated per step")
1127 #undef GETSETNAME
1128   { 0 }
1129 };
1130
1131 static PyTypeObject bbs_pytype_skel = {
1132   PyObject_HEAD_INIT(0) 0,              /* Header */
1133   "catacomb.BlumBlumShub",              /* @tp_name@ */
1134   sizeof(grand_pyobj),                  /* @tp_basicsize@ */
1135   0,                                    /* @tp_itemsize@ */
1136
1137   grand_pydealloc,                      /* @tp_dealloc@ */
1138   0,                                    /* @tp_print@ */
1139   0,                                    /* @tp_getattr@ */
1140   0,                                    /* @tp_setattr@ */
1141   0,                                    /* @tp_compare@ */
1142   0,                                    /* @tp_repr@ */
1143   0,                                    /* @tp_as_number@ */
1144   0,                                    /* @tp_as_sequence@ */
1145   0,                                    /* @tp_as_mapping@ */
1146   0,                                    /* @tp_hash@ */
1147   0,                                    /* @tp_call@ */
1148   0,                                    /* @tp_str@ */
1149   0,                                    /* @tp_getattro@ */
1150   0,                                    /* @tp_setattro@ */
1151   0,                                    /* @tp_as_buffer@ */
1152   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
1153     Py_TPFLAGS_BASETYPE,
1154
1155   /* @tp_doc@ */
1156 "Blum-Blum-Shub strong pseudorandom number generator.",
1157
1158   0,                                    /* @tp_traverse@ */
1159   0,                                    /* @tp_clear@ */
1160   0,                                    /* @tp_richcompare@ */
1161   0,                                    /* @tp_weaklistoffset@ */
1162   0,                                    /* @tp_iter@ */
1163   0,                                    /* @tp_iternext@ */
1164   bbs_pymethods,                        /* @tp_methods@ */
1165   0,                                    /* @tp_members@ */
1166   bbs_pygetset,                         /* @tp_getset@ */
1167   0,                                    /* @tp_base@ */
1168   0,                                    /* @tp_dict@ */
1169   0,                                    /* @tp_descr_get@ */
1170   0,                                    /* @tp_descr_set@ */
1171   0,                                    /* @tp_dictoffset@ */
1172   0,                                    /* @tp_init@ */
1173   PyType_GenericAlloc,                  /* @tp_alloc@ */
1174   bbs_pynew,                            /* @tp_new@ */
1175   0,                                    /* @tp_free@ */
1176   0                                     /* @tp_is_gc@ */
1177 };
1178
1179 typedef struct bbspriv_pyobj {
1180   grand_pyobj gr;
1181   bbs_priv bp;
1182 } bbspriv_pyobj;
1183
1184 #define BBSPRIV_BP(o) (&((bbspriv_pyobj *)(o))->bp)
1185
1186 static PyObject *bbspriv_pynew(PyTypeObject *ty,
1187                                PyObject *arg, PyObject *kw)
1188 {
1189   mp *p = 0, *q = 0, *n = 0, *x = MP_TWO;
1190   bbspriv_pyobj *rc = 0;
1191   char *kwlist[] = { "n", "p", "q", "seed", 0 };
1192
1193   if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&O&O&O&:new", kwlist,
1194                                    convmp, &n, convmp, &p, convmp, &q,
1195                                    convmp, &x))
1196     goto end;
1197   if (!n + !p + !q > 1) VALERR("must specify at least two of n, p, q");
1198   if (!n) n = mp_mul(MP_NEW, p, q);
1199   else if (!p) mp_div(&p, 0, n, q);
1200   else if (!q) mp_div(&q, 0, n, p);
1201   rc = (bbspriv_pyobj *)ty->tp_alloc(ty, 0);
1202   rc->gr.r = bbs_rand(n, x);
1203   rc->gr.f = f_freeme;
1204   rc->bp.p = MP_COPY(p);
1205   rc->bp.q = MP_COPY(q);
1206   rc->bp.n = MP_COPY(n);
1207 end:
1208   mp_drop(p); mp_drop(q); mp_drop(n); mp_drop(x);
1209   return ((PyObject *)rc);
1210 }
1211
1212 static PyObject *meth__BBSPriv_generate(PyObject *me,
1213                                         PyObject *arg, PyObject *kw)
1214 {
1215   bbs_priv bp = { 0 };
1216   mp *x = MP_TWO;
1217   pgev evt = { 0 };
1218   unsigned nbits, n = 0;
1219   grand *r = &rand_global;
1220   char *kwlist[] = { "class", "nbits", "event", "rng", "nsteps", "seed", 0 };
1221   bbspriv_pyobj *rc = 0;
1222
1223   if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&|O&O&O&O&:generate", kwlist,
1224                                    &me, convuint, &nbits, convpgev, &evt,
1225                                    convgrand, &r, convuint, &n, convmp, &x))
1226     goto end;
1227   if (bbs_gen(&bp, nbits, r, n, evt.proc, evt.ctx))
1228     VALERR("prime genration failed");
1229   rc = PyObject_New(bbspriv_pyobj, bbspriv_pytype);
1230   rc->gr.r = bbs_rand(bp.n, x);
1231   rc->gr.f = f_freeme;
1232   rc->bp.p = MP_COPY(bp.p);
1233   rc->bp.q = MP_COPY(bp.q);
1234   rc->bp.n = MP_COPY(bp.n);
1235 end:
1236   mp_drop(bp.p); mp_drop(bp.q); mp_drop(bp.n); mp_drop(x);
1237   return ((PyObject *)rc);
1238 }
1239
1240 static void bbspriv_pydealloc(PyObject *me)
1241 {
1242   bbs_priv *bp = BBSPRIV_BP(me);
1243   mp_drop(bp->n);
1244   mp_drop(bp->p);
1245   mp_drop(bp->q);
1246   grand_pydealloc(me);
1247 }
1248
1249 static PyObject *bpmeth_ff(PyObject *me, PyObject *arg)
1250 {
1251   mp *n = 0; grand *r = GRAND_R(me); bbs_priv *bp = BBSPRIV_BP(me);
1252   if (!PyArg_ParseTuple(arg, "O&:ff", convmp, &n)) return (0);
1253   r->ops->misc(r, BBS_FF, bp, n); RETURN_ME;
1254 }
1255
1256 static PyObject *bpmeth_rew(PyObject *me, PyObject *arg)
1257 {
1258   mp *n = 0; grand *r = GRAND_R(me); bbs_priv *bp = BBSPRIV_BP(me);
1259   if (!PyArg_ParseTuple(arg, "O&:rew", convmp, &n)) return (0);
1260   r->ops->misc(r, BBS_REW, bp, n); RETURN_ME;
1261 }
1262
1263 static PyObject *bpget_n(PyObject *me, void *hunoz)
1264   { return (mp_pywrap(MP_COPY(BBSPRIV_BP(me)->n))); }
1265
1266 static PyObject *bpget_p(PyObject *me, void *hunoz)
1267   { return (mp_pywrap(MP_COPY(BBSPRIV_BP(me)->p))); }
1268
1269 static PyObject *bpget_q(PyObject *me, void *hunoz)
1270   { return (mp_pywrap(MP_COPY(BBSPRIV_BP(me)->q))); }
1271
1272 static PyMethodDef bbspriv_pymethods[] = {
1273 #define METHNAME(name) bpmeth_##name
1274   METH  (ff,                    "R.ff(N): fast-forward N places")
1275   METH  (rew,                   "R.rew(N): rewind N places")
1276 #undef METHNAME
1277   { 0 }
1278 };
1279
1280 static PyGetSetDef bbspriv_pygetset[] = {
1281 #define GETSETNAME(op, name) bp##op##_##name
1282   GET   (n,             "R.n -> Blum modulus")
1283   GET   (p,             "R.p -> one of the factors of the modulus")
1284   GET   (q,             "R.q -> one of the factors of the modulus")
1285 #undef GETSETNAME
1286   { 0 }
1287 };
1288
1289 static PyTypeObject bbspriv_pytype_skel = {
1290   PyObject_HEAD_INIT(0) 0,              /* Header */
1291   "catacomb.BBSPriv",                   /* @tp_name@ */
1292   sizeof(bbspriv_pyobj),                /* @tp_basicsize@ */
1293   0,                                    /* @tp_itemsize@ */
1294
1295   bbspriv_pydealloc,                    /* @tp_dealloc@ */
1296   0,                                    /* @tp_print@ */
1297   0,                                    /* @tp_getattr@ */
1298   0,                                    /* @tp_setattr@ */
1299   0,                                    /* @tp_compare@ */
1300   0,                                    /* @tp_repr@ */
1301   0,                                    /* @tp_as_number@ */
1302   0,                                    /* @tp_as_sequence@ */
1303   0,                                    /* @tp_as_mapping@ */
1304   0,                                    /* @tp_hash@ */
1305   0,                                    /* @tp_call@ */
1306   0,                                    /* @tp_str@ */
1307   0,                                    /* @tp_getattro@ */
1308   0,                                    /* @tp_setattro@ */
1309   0,                                    /* @tp_as_buffer@ */
1310   Py_TPFLAGS_DEFAULT |                  /* @tp_flags@ */
1311     Py_TPFLAGS_BASETYPE,
1312
1313   /* @tp_doc@ */
1314 "Blum-Blum-Shub strong pseudorandom generator, with private key.",
1315
1316   0,                                    /* @tp_traverse@ */
1317   0,                                    /* @tp_clear@ */
1318   0,                                    /* @tp_richcompare@ */
1319   0,                                    /* @tp_weaklistoffset@ */
1320   0,                                    /* @tp_iter@ */
1321   0,                                    /* @tp_iternext@ */
1322   bbspriv_pymethods,                    /* @tp_methods@ */
1323   0,                                    /* @tp_members@ */
1324   bbspriv_pygetset,                     /* @tp_getset@ */
1325   0,                                    /* @tp_base@ */
1326   0,                                    /* @tp_dict@ */
1327   0,                                    /* @tp_descr_get@ */
1328   0,                                    /* @tp_descr_set@ */
1329   0,                                    /* @tp_dictoffset@ */
1330   0,                                    /* @tp_init@ */
1331   PyType_GenericAlloc,                  /* @tp_alloc@ */
1332   bbspriv_pynew,                        /* @tp_new@ */
1333   0,                                    /* @tp_free@ */
1334   0                                     /* @tp_is_gc@ */
1335 };
1336
1337 /*----- Global stuff ------------------------------------------------------*/
1338
1339 static PyMethodDef methods[] = {
1340 #define METHNAME(name) meth_##name
1341   KWMETH(_BBSPriv_generate,             "\
1342 generate(NBITS, [event = pgen_nullev, rng = rand, nsteps = 0, seed = 2])")
1343 #undef METHNAME
1344   { 0 }
1345 };
1346
1347 void rand_pyinit(void)
1348 {
1349   INITTYPE(grand, root);
1350   INITTYPE(truerand, grand);
1351   INITTYPE(fibrand, grand);
1352   INITTYPE(lcrand, grand);
1353   INITTYPE(dsarand, grand);
1354   INITTYPE(bbs, grand);
1355   INITTYPE(bbspriv, bbs);
1356   INITTYPE(sslprf, grand);
1357   INITTYPE(tlsdx, grand);
1358   INITTYPE(tlsprf, grand);
1359   INITTYPE(gccrand, type);
1360   INITTYPE(gcrand, grand);
1361   rand_noisesrc(RAND_GLOBAL, &noise_source);
1362   rand_seed(RAND_GLOBAL, 160);
1363   addmethods(methods);
1364 }
1365
1366 #define gccrand gccrand_info
1367 GEN(gccrands, crand)
1368
1369 void rand_pyinsert(PyObject *mod)
1370 {
1371   INSERT("GRand", grand_pytype);
1372   INSERT("TrueRand", truerand_pytype);
1373   INSERT("LCRand", lcrand_pytype);
1374   INSERT("FibRand", fibrand_pytype);
1375   INSERT("SSLRand", sslprf_pytype);
1376   INSERT("TLSDataExpansion", tlsdx_pytype);
1377   INSERT("TLSPRF", tlsprf_pytype);
1378   INSERT("DSARand", dsarand_pytype);
1379   INSERT("BlumBlumShub", bbs_pytype);
1380   INSERT("BBSPriv", bbspriv_pytype);
1381   INSERT("GCCRand", gccrand_pytype);
1382   INSERT("GCRand", gcrand_pytype);
1383   rand_pyobj = grand_pywrap(&rand_global, 0); Py_INCREF(rand_pyobj);
1384   gccrands_dict = gccrands(); Py_INCREF(gccrands_dict);
1385   INSERT("gccrands", gccrands_dict);
1386   INSERT("rand", rand_pyobj);
1387 }
1388
1389 /*----- That's all, folks -------------------------------------------------*/