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