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