3 * Random-number generators
5 * (c) 2004 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Python interface to Catacomb.
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.
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.
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.
27 /*----- Header files ------------------------------------------------------*/
29 #include "catacomb-python.h"
31 #include "algorithms.h"
34 /*----- Main code ---------------------------------------------------------*/
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;
43 static PyObject *gccrands_dict;
45 static PyObject *grand_dopywrap(PyTypeObject *ty, grand *r, unsigned f)
49 g = (grand_pyobj *)ty->tp_alloc(ty, 0);
52 return ((PyObject *)g);
55 PyObject *grand_pywrap(grand *r, unsigned f)
57 PyTypeObject *ty = grand_pytype;
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 = PyMapping_GetItemString
69 (gccrands_dict, (/*unconst*/ char *)r->ops->name)) != 0)
70 ty = (PyTypeObject *)ob;
71 return (grand_dopywrap(ty, r, f));
74 CONVFUNC(grand, grand *, GRAND_R)
76 static int grand_check(PyObject *me)
78 if (!GRAND_R(me)) VALERR("random generator object is no longer valid");
84 static PyObject *grmeth_byte(PyObject *me)
86 if (grand_check(me)) return (0);
87 return (PyInt_FromLong(grand_byte(GRAND_R(me))));
90 static PyObject *grmeth_word(PyObject *me)
92 if (grand_check(me)) return (0);
93 return (getulong(grand_word(GRAND_R(me))));
96 static PyObject *grmeth_range(PyObject *me, PyObject *arg)
102 if (!PyArg_ParseTuple(arg, "O:range", &m)) return (0);
103 if (grand_check(me)) return (0);
104 if (PyInt_Check(m)) {
105 long mm = PyInt_AS_LONG(m);
108 if (mm <= 0xffffffff)
109 return (PyInt_FromLong(grand_range(GRAND_R(me), mm)));
111 if ((x = getmp(m)) == 0)
115 y = mprand_range(MP_NEW, x, GRAND_R(me), 0);
117 return (mp_pywrap(y));
119 VALERR("range must be strictly positive");
125 static PyObject *grmeth_mp(PyObject *me, PyObject *arg, PyObject *kw)
129 static const char *const kwlist[] = { "bits", "or", 0 };
131 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:mp", KWLIST,
132 convszt, &l, convmpw, &o))
134 if (grand_check(me)) return (0);
135 if (l < MPW_BITS && (o >> l)) VALERR("or mask too large");
136 return (mp_pywrap(mprand(MP_NEW, l, GRAND_R(me), o)));
141 static PyObject *grmeth_block(PyObject *me, PyObject *arg)
146 if (!PyArg_ParseTuple(arg, "O&:block", convulong, &n)) goto end;
147 if (grand_check(me)) return (0);
148 rc = bytestring_pywrap(0, n);
149 grand_fill(GRAND_R(me), PyString_AS_STRING(rc), n);
154 static int checkop(grand *r, unsigned op, const char *what)
156 if (r->ops->misc(r, GRAND_CHECK, op)) return (0);
157 PyErr_Format(PyExc_TypeError, "operation %s not supported", what);
161 static PyObject *grmeth_seedint(PyObject *me, PyObject *arg)
164 grand *r = GRAND_R(me);
165 if (!PyArg_ParseTuple(arg, "i:seedint", &i) ||
166 grand_check(me) || checkop(r, GRAND_SEEDINT, "seedint"))
168 r->ops->misc(r, GRAND_SEEDINT, i);
174 static PyObject *grmeth_seedword(PyObject *me, PyObject *arg)
177 grand *r = GRAND_R(me);
178 if (!PyArg_ParseTuple(arg, "O&:seedword", convu32, &u) ||
179 grand_check(me) || checkop(r, GRAND_SEEDUINT32, "seedword"))
181 r->ops->misc(r, GRAND_SEEDUINT32, u);
187 static PyObject *grmeth_seedblock(PyObject *me, PyObject *arg)
191 grand *r = GRAND_R(me);
192 if (!PyArg_ParseTuple(arg, "s#:seedblock", &p, &n) ||
193 grand_check(me) || checkop(r, GRAND_SEEDBLOCK, "seedblock"))
195 r->ops->misc(r, GRAND_SEEDBLOCK, p, (size_t)n);
201 static PyObject *grmeth_seedmp(PyObject *me, PyObject *arg)
205 grand *r = GRAND_R(me);
206 if (!PyArg_ParseTuple(arg, "O:seedmp", &x) ||
207 grand_check(me) || checkop(r, GRAND_SEEDMP, "seedmp") ||
208 (xx = getmp(x)) == 0)
210 r->ops->misc(r, GRAND_SEEDMP, xx);
217 static PyObject *grmeth_seedrand(PyObject *me, PyObject *arg, PyObject *kw)
219 static const char *const kwlist[] = { "rng", 0 };
220 grand *r = GRAND_R(me);
221 grand *rr = &rand_global;
222 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:seedrand", KWLIST,
224 grand_check(me) || checkop(r, GRAND_SEEDRAND, "seedrand"))
226 r->ops->misc(r, GRAND_SEEDRAND, rr);
232 static PyObject *grmeth_mask(PyObject *me, PyObject *arg)
234 grand *r = GRAND_R(me);
239 if (!PyArg_ParseTuple(arg, "s#:mask", &p, &sz)) return (0);
240 if (grand_check(me)) return (0);
241 rc = bytestring_pywrap(0, sz);
242 q = PyString_AS_STRING(rc);
244 while (sz--) *q++ ^= *p++;
248 static void grand_pydealloc(PyObject *me)
250 grand_pyobj *g = (grand_pyobj *)me;
251 if ((g->f & f_freeme) && g->r) GR_DESTROY(g->r);
255 static PyObject *grget_name(PyObject *me, void *hunoz)
256 { return (grand_check(me) ? 0 : PyString_FromString(GRAND_R(me)->ops->name)); }
258 static PyObject *grget_cryptop(PyObject *me, void *hunoz)
259 { return (grand_check(me) ? 0 : getbool(GRAND_R(me)->ops->f & GRAND_CRYPTO)); }
261 static const PyGetSetDef grand_pygetset[] = {
262 #define GETSETNAME(op, name) gr##op##_##name
263 GET (name, "R.name -> name of this kind of generator")
264 GET (cryptop, "R.cryptop -> flag: cryptographically strong?")
269 static const PyMethodDef grand_pymethods[] = {
270 #define METHNAME(name) grmeth_##name
271 NAMETH(byte, "R.byte() -> BYTE")
272 NAMETH(word, "R.word() -> WORD")
273 METH (block, "R.block(N) -> STRING")
274 KWMETH(mp, "R.mp(bits, [or = 0]) -> MP")
275 METH (range, "R.range(MAX) -> INT")
276 METH (mask, "R.mask(STR) -> STR")
277 METH (seedint, "R.seedint(I)")
278 METH (seedword, "R.seedword(I)")
279 METH (seedblock, "R.seedblock(BYTES)")
280 METH (seedmp, "R.seedmp(X)")
281 KWMETH(seedrand, "R.seedrand(RR)")
286 static const PyTypeObject grand_pytype_skel = {
287 PyVarObject_HEAD_INIT(0, 0) /* Header */
288 "GRand", /* @tp_name@ */
289 sizeof(grand_pyobj), /* @tp_basicsize@ */
290 0, /* @tp_itemsize@ */
292 grand_pydealloc, /* @tp_dealloc@ */
294 0, /* @tp_getattr@ */
295 0, /* @tp_setattr@ */
296 0, /* @tp_compare@ */
298 0, /* @tp_as_number@ */
299 0, /* @tp_as_sequence@ */
300 0, /* @tp_as_mapping@ */
304 0, /* @tp_getattro@ */
305 0, /* @tp_setattro@ */
306 0, /* @tp_as_buffer@ */
307 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
311 "Generic random number source.",
313 0, /* @tp_traverse@ */
315 0, /* @tp_richcompare@ */
316 0, /* @tp_weaklistoffset@ */
318 0, /* @tp_iternext@ */
319 PYMETHODS(grand), /* @tp_methods@ */
320 0, /* @tp_members@ */
321 PYGETSET(grand), /* @tp_getset@ */
324 0, /* @tp_descr_get@ */
325 0, /* @tp_descr_set@ */
326 0, /* @tp_dictoffset@ */
328 PyType_GenericAlloc, /* @tp_alloc@ */
329 abstract_pynew, /* @tp_new@ */
334 static PyObject *lcrand_pynew(PyTypeObject *me, PyObject *arg, PyObject *kw)
337 static const char *const kwlist[] = { "seed", 0 };
338 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", KWLIST, convu32, &n))
340 return (grand_dopywrap(lcrand_pytype, lcrand_create(n), f_freeme));
343 static const PyTypeObject lcrand_pytype_skel = {
344 PyVarObject_HEAD_INIT(0, 0) /* Header */
345 "LCRand", /* @tp_name@ */
346 sizeof(grand_pyobj), /* @tp_basicsize@ */
347 0, /* @tp_itemsize@ */
349 grand_pydealloc, /* @tp_dealloc@ */
351 0, /* @tp_getattr@ */
352 0, /* @tp_setattr@ */
353 0, /* @tp_compare@ */
355 0, /* @tp_as_number@ */
356 0, /* @tp_as_sequence@ */
357 0, /* @tp_as_mapping@ */
361 0, /* @tp_getattro@ */
362 0, /* @tp_setattro@ */
363 0, /* @tp_as_buffer@ */
364 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
368 "LCRand([seed = 0]): linear congruential generator.",
370 0, /* @tp_traverse@ */
372 0, /* @tp_richcompare@ */
373 0, /* @tp_weaklistoffset@ */
375 0, /* @tp_iternext@ */
376 0, /* @tp_methods@ */
377 0, /* @tp_members@ */
381 0, /* @tp_descr_get@ */
382 0, /* @tp_descr_set@ */
383 0, /* @tp_dictoffset@ */
385 PyType_GenericAlloc, /* @tp_alloc@ */
386 lcrand_pynew, /* @tp_new@ */
391 static PyObject *fibrand_pynew(PyTypeObject *me, PyObject *arg, PyObject *kw)
394 static const char *const kwlist[] = { "seed", 0 };
395 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", KWLIST, convu32, &n))
397 return (grand_dopywrap(fibrand_pytype, fibrand_create(n), f_freeme));
400 static const PyTypeObject fibrand_pytype_skel = {
401 PyVarObject_HEAD_INIT(0, 0) /* Header */
402 "FibRand", /* @tp_name@ */
403 sizeof(grand_pyobj), /* @tp_basicsize@ */
404 0, /* @tp_itemsize@ */
406 grand_pydealloc, /* @tp_dealloc@ */
408 0, /* @tp_getattr@ */
409 0, /* @tp_setattr@ */
410 0, /* @tp_compare@ */
412 0, /* @tp_as_number@ */
413 0, /* @tp_as_sequence@ */
414 0, /* @tp_as_mapping@ */
418 0, /* @tp_getattro@ */
419 0, /* @tp_setattro@ */
420 0, /* @tp_as_buffer@ */
421 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
425 "FibRand([seed = 0]): Fibonacci generator.",
427 0, /* @tp_traverse@ */
429 0, /* @tp_richcompare@ */
430 0, /* @tp_weaklistoffset@ */
432 0, /* @tp_iternext@ */
433 0, /* @tp_methods@ */
434 0, /* @tp_members@ */
438 0, /* @tp_descr_get@ */
439 0, /* @tp_descr_set@ */
440 0, /* @tp_dictoffset@ */
442 PyType_GenericAlloc, /* @tp_alloc@ */
443 fibrand_pynew, /* @tp_new@ */
448 /*----- True random generator ---------------------------------------------*/
450 static PyObject *trmeth_gate(PyObject *me)
451 { grand *r = GRAND_R(me); r->ops->misc(GRAND_R(me), RAND_GATE); RETURN_ME; }
453 static PyObject *trmeth_stretch(PyObject *me)
454 { grand *r = GRAND_R(me); r->ops->misc(r, RAND_STRETCH); RETURN_ME; }
456 static PyObject *trmeth_add(PyObject *me, PyObject *arg)
458 grand *r = GRAND_R(me);
459 char *p; Py_ssize_t n; unsigned goodbits;
460 if (!PyArg_ParseTuple(arg, "s#O&:add", &p, &n, convuint, &goodbits))
462 r->ops->misc(r, RAND_ADD, p, (size_t)n, goodbits);
466 static PyObject *trmeth_key(PyObject *me, PyObject *arg)
468 grand *r = GRAND_R(me);
469 char *p; Py_ssize_t n;
470 if (!PyArg_ParseTuple(arg, "s#:key", &p, &n)) return (0);
471 r->ops->misc(r, RAND_KEY, p, (size_t)n);
475 static PyObject *trmeth_seed(PyObject *me, PyObject *arg)
477 grand *r = GRAND_R(me);
479 if (!PyArg_ParseTuple(arg, "O&:seed", convuint, &u)) return (0);
480 if (u > RAND_IBITS) VALERR("pointlessly large");
481 r->ops->misc(r, RAND_SEED, u);
487 static PyObject *trmeth_timer(PyObject *me)
488 { grand *r = GRAND_R(me); r->ops->misc(r, RAND_TIMER); RETURN_ME; }
490 static PyObject *truerand_pynew(PyTypeObject *ty,
491 PyObject *arg, PyObject *kw)
493 static const char *const kwlist[] = { 0 };
496 if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", KWLIST)) goto end;
498 r->ops->misc(r, RAND_NOISESRC, &noise_source);
499 r->ops->misc(r, RAND_SEED, 160);
500 rc = grand_dopywrap(ty, r, f_freeme);
505 static const PyMethodDef truerand_pymethods[] = {
506 #define METHNAME(name) trmeth_##name
507 NAMETH(gate, "R.gate()")
508 NAMETH(stretch, "R.stretch()")
509 METH (key, "R.key(BYTES)")
510 METH (seed, "R.seed(NBITS)")
511 METH (add, "R.add(BYTES, GOODBITS")
512 NAMETH(timer, "R.timer()")
517 static PyObject *trget_goodbits(PyObject *me, void *hunoz)
519 grand *r = GRAND_R(me);
520 return (PyInt_FromLong(r->ops->misc(r, RAND_GOODBITS)));
523 static const PyGetSetDef truerand_pygetset[] = {
524 #define GETSETNAME(op, name) tr##op##_##name
525 GET (goodbits, "R.goodbits -> good bits of entropy remaining")
530 static const PyTypeObject truerand_pytype_skel = {
531 PyVarObject_HEAD_INIT(0, 0) /* Header */
532 "TrueRand", /* @tp_name@ */
533 sizeof(grand_pyobj), /* @tp_basicsize@ */
534 0, /* @tp_itemsize@ */
536 grand_pydealloc, /* @tp_dealloc@ */
538 0, /* @tp_getattr@ */
539 0, /* @tp_setattr@ */
540 0, /* @tp_compare@ */
542 0, /* @tp_as_number@ */
543 0, /* @tp_as_sequence@ */
544 0, /* @tp_as_mapping@ */
548 0, /* @tp_getattro@ */
549 0, /* @tp_setattro@ */
550 0, /* @tp_as_buffer@ */
551 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
555 "TrueRand(): true random number source.",
557 0, /* @tp_traverse@ */
559 0, /* @tp_richcompare@ */
560 0, /* @tp_weaklistoffset@ */
562 0, /* @tp_iternext@ */
563 PYMETHODS(truerand), /* @tp_methods@ */
564 0, /* @tp_members@ */
565 PYGETSET(truerand), /* @tp_getset@ */
568 0, /* @tp_descr_get@ */
569 0, /* @tp_descr_set@ */
570 0, /* @tp_dictoffset@ */
572 PyType_GenericAlloc, /* @tp_alloc@ */
573 truerand_pynew, /* @tp_new@ */
578 /*----- Generators from symmetric encryption algorithms -------------------*/
580 static PyTypeObject *gccrand_pytype, *gcrand_pytype, *gclatinrand_pytype;
582 typedef grand *gcrand_func(const void *, size_t sz);
583 typedef grand *gcirand_func(const void *, size_t sz, uint32);
584 typedef grand *gcnrand_func(const void *, size_t sz, const void *);
585 typedef grand *gcshakerand_func(const void *, size_t,
586 const void *, size_t,
587 const void *, size_t);
588 typedef grand *gcshafuncrand_func(const void *, size_t,
589 const void *, size_t);
590 typedef grand *gckmacrand_func(const void *, size_t, const void *, size_t);
591 typedef struct gccrand_info {
599 #define RNGF_MASK 255u
609 typedef struct gccrand_pyobj {
611 const gccrand_info *info;
613 #define GCCRAND_INFO(o) (((gccrand_pyobj *)(o))->info)
615 #define GCCRAND_DEF(name, ksz, func, f, nsz) \
616 static const gccrand_info func##_info = \
617 { name, ksz, f, nsz, (gcrand_func *)func };
620 static const gccrand_info *const gcrandtab[] = {
621 #define GCCRAND_ENTRY(name, ksz, func, f, nsz) &func##_info,
626 static PyObject *gcrand_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
628 const gccrand_info *info = GCCRAND_INFO(ty);
629 static const char *const kwlist[] = { "key", 0 };
633 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &k, &n))
635 if (keysz(n, info->keysz) != n) VALERR("bad key length");
636 return (grand_dopywrap(ty, info->func(k, n), f_freeme));
641 static PyObject *gcirand_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
643 const gccrand_info *info = GCCRAND_INFO(ty);
645 static const char *const kwlist[] = { "key", "i", 0 };
649 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#O&:new", KWLIST,
650 &k, &n, convu32, &i))
652 if (keysz(n, info->keysz) != n) VALERR("bad key length");
653 return (grand_dopywrap(ty,
654 ((gcirand_func *)info->func)(k, n, i),
660 static PyObject *gcnrand_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
662 const gccrand_info *info = GCCRAND_INFO(ty);
663 static const char *const kwlist[] = { "key", "nonce", 0 };
667 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#s#:new", KWLIST,
670 if (keysz(ksz, info->keysz) != ksz) VALERR("bad key length");
671 if (nsz != info->noncesz) VALERR("bad nonce length");
672 return (grand_dopywrap(ty,
673 ((gcnrand_func *)info->func)(k, ksz, n),
679 static PyObject *gcshakyrand_pynew(PyTypeObject *ty,
680 PyObject *arg, PyObject *kw)
682 const gccrand_info *info = GCCRAND_INFO(ty);
684 *const kwlist_shake[] = { "key", "func", "perso", 0 },
685 *const kwlist_func[] = { "key", "perso", 0 };
686 char *k, *f = 0, *p = 0;
687 Py_ssize_t ksz, fsz = 0, psz = 0;
689 if ((info->f&RNGF_MASK) == RNG_SHAKE
690 ? !PyArg_ParseTupleAndKeywords(arg, kw, "s#|s#s#:new",
691 (/*unconst*/ char **)kwlist_shake,
692 &k, &ksz, &f, &fsz, &p, &psz)
693 : !PyArg_ParseTupleAndKeywords(arg, kw, "s#|s#:new",
694 (/*unconst*/ char **)kwlist_func,
697 if (keysz(ksz, info->keysz) != ksz) VALERR("bad key length");
698 return (grand_dopywrap(ty,
699 (info->f&RNGF_MASK) == RNG_SHAKE
700 ? ((gcshakerand_func *)info->func)(f, fsz,
703 : ((gcshafuncrand_func *)info->func)(p, psz,
710 static PyObject *gccrand_pywrap(const gccrand_info *info)
712 gccrand_pyobj *g = newtype(gccrand_pytype, 0, info->name);
714 g->ty.ht_type.tp_basicsize = sizeof(grand_pyobj);
715 switch (info->f&RNGF_MASK) {
716 case RNG_LATIN: g->ty.ht_type.tp_base = gclatinrand_pytype; break;
717 default: g->ty.ht_type.tp_base = gcrand_pytype; break;
719 Py_INCREF(g->ty.ht_type.tp_base);
720 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
721 Py_TPFLAGS_BASETYPE |
722 Py_TPFLAGS_HEAPTYPE);
723 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
724 g->ty.ht_type.tp_free = 0;
725 switch (info->f&RNGF_MASK) {
726 case RNG_LATIN: g->ty.ht_type.tp_new = gcnrand_pynew; break;
727 case RNG_SEAL: g->ty.ht_type.tp_new = gcirand_pynew; break;
728 case RNG_SHAKE: case RNG_KMAC:
729 g->ty.ht_type.tp_new = gcshakyrand_pynew; break;
730 default: g->ty.ht_type.tp_new = gcrand_pynew; break;
732 typeready(&g->ty.ht_type);
733 return ((PyObject *)g);
736 static PyObject *gccrget_name(PyObject *me, void *hunoz)
737 { return (PyString_FromString(GCCRAND_INFO(me)->name)); }
738 static PyObject *gccrget_keysz(PyObject *me, void *hunoz)
739 { return (keysz_pywrap(GCCRAND_INFO(me)->keysz)); }
741 static PyObject *gclrmeth_tell(PyObject *me)
743 grand *r = GRAND_R(me);
747 r->ops->misc(r, SALSA20_TELLU64, &off);
752 static PyObject *gclrmeth_seek(PyObject *me, PyObject *arg)
754 grand *r = GRAND_R(me);
757 if (!PyArg_ParseTuple(arg, "O&:seek", convk64, &off)) return (0);
758 r->ops->misc(r, SALSA20_SEEKU64, off);
762 static const PyGetSetDef gccrand_pygetset[] = {
763 #define GETSETNAME(op, name) gccr##op##_##name
764 GET (keysz, "CR.keysz -> acceptable key sizes")
765 GET (name, "CR.name -> name of this kind of generator")
770 static const PyMethodDef gclatinrand_pymethods[] = {
771 #define METHNAME(name) gclrmeth_##name
772 NAMETH(tell, "R.tell() -> OFF")
773 METH (seek, "R.seek(OFF)")
778 static const PyTypeObject gccrand_pytype_skel = {
779 PyVarObject_HEAD_INIT(0, 0) /* Header */
780 "GCCRand", /* @tp_name@ */
781 sizeof(gccrand_pyobj), /* @tp_basicsize@ */
782 0, /* @tp_itemsize@ */
784 0, /* @tp_dealloc@ */
786 0, /* @tp_getattr@ */
787 0, /* @tp_setattr@ */
788 0, /* @tp_compare@ */
790 0, /* @tp_as_number@ */
791 0, /* @tp_as_sequence@ */
792 0, /* @tp_as_mapping@ */
796 0, /* @tp_getattro@ */
797 0, /* @tp_setattro@ */
798 0, /* @tp_as_buffer@ */
799 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
803 "Metaclass for symmetric crypto-based generators.",
805 0, /* @tp_traverse@ */
807 0, /* @tp_richcompare@ */
808 0, /* @tp_weaklistoffset@ */
810 0, /* @tp_iternext@ */
811 0, /* @tp_methods@ */
812 0, /* @tp_members@ */
813 PYGETSET(gccrand), /* @tp_getset@ */
816 0, /* @tp_descr_get@ */
817 0, /* @tp_descr_set@ */
818 0, /* @tp_dictoffset@ */
820 PyType_GenericAlloc, /* @tp_alloc@ */
821 abstract_pynew, /* @tp_new@ */
826 static const PyTypeObject gcrand_pytype_skel = {
827 PyVarObject_HEAD_INIT(0, 0) /* Header */
828 "GCRand", /* @tp_name@ */
829 sizeof(grand_pyobj), /* @tp_basicsize@ */
830 0, /* @tp_itemsize@ */
832 grand_pydealloc, /* @tp_dealloc@ */
834 0, /* @tp_getattr@ */
835 0, /* @tp_setattr@ */
836 0, /* @tp_compare@ */
838 0, /* @tp_as_number@ */
839 0, /* @tp_as_sequence@ */
840 0, /* @tp_as_mapping@ */
844 0, /* @tp_getattro@ */
845 0, /* @tp_setattro@ */
846 0, /* @tp_as_buffer@ */
847 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
851 "Abstract base class for symmetric crypto-based generators.",
853 0, /* @tp_traverse@ */
855 0, /* @tp_richcompare@ */
856 0, /* @tp_weaklistoffset@ */
858 0, /* @tp_iternext@ */
859 0, /* @tp_methods@ */
860 0, /* @tp_members@ */
864 0, /* @tp_descr_get@ */
865 0, /* @tp_descr_set@ */
866 0, /* @tp_dictoffset@ */
868 PyType_GenericAlloc, /* @tp_alloc@ */
869 abstract_pynew, /* @tp_new@ */
874 static const PyTypeObject gclatinrand_pytype_skel = {
875 PyVarObject_HEAD_INIT(0, 0) /* Header */
876 "GCLatinRand", /* @tp_name@ */
877 sizeof(grand_pyobj), /* @tp_basicsize@ */
878 0, /* @tp_itemsize@ */
880 grand_pydealloc, /* @tp_dealloc@ */
882 0, /* @tp_getattr@ */
883 0, /* @tp_setattr@ */
884 0, /* @tp_compare@ */
886 0, /* @tp_as_number@ */
887 0, /* @tp_as_sequence@ */
888 0, /* @tp_as_mapping@ */
892 0, /* @tp_getattro@ */
893 0, /* @tp_setattro@ */
894 0, /* @tp_as_buffer@ */
895 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
899 "Abstract base class for symmetric crypto-based generators.",
901 0, /* @tp_traverse@ */
903 0, /* @tp_richcompare@ */
904 0, /* @tp_weaklistoffset@ */
906 0, /* @tp_iternext@ */
907 PYMETHODS(gclatinrand), /* @tp_methods@ */
908 0, /* @tp_members@ */
912 0, /* @tp_descr_get@ */
913 0, /* @tp_descr_set@ */
914 0, /* @tp_dictoffset@ */
916 PyType_GenericAlloc, /* @tp_alloc@ */
917 abstract_pynew, /* @tp_new@ */
922 /*----- SSL and TLS generators --------------------------------------------*/
924 static PyObject *sslprf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
928 const gchash *hco = &md5, *hci = &sha;
930 static const char *const kwlist[] = { "key", "seed", "ohash", "ihash", 0 };
932 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#s#|O&O&:new", KWLIST,
934 convgchash, &hco, convgchash, &hci))
936 rc = grand_dopywrap(ty, sslprf_rand(hco, hci, k, ksz, s, ssz), f_freeme);
941 static PyObject *tlsdx_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
945 const gcmac *mc = &sha_hmac;
947 static const char *const kwlist[] = { "key", "seed", "mac", 0 };
949 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#s#|O&:new", KWLIST,
953 rc = grand_dopywrap(ty, tlsdx_rand(mc, k, ksz, s, ssz), f_freeme);
958 static PyObject *tlsprf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
962 const gcmac *mcl = &md5_hmac, *mcr = &sha_hmac;
964 static const char *const kwlist[] = { "key", "seed", "lmac", "rmac", 0 };
966 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#s#|O&O&:new", KWLIST,
968 convgcmac, &mcl, convgcmac, &mcr))
970 rc = grand_dopywrap(ty, tlsprf_rand(mcl, mcr, k, ksz, s, ssz), f_freeme);
975 static const PyTypeObject sslprf_pytype_skel = {
976 PyVarObject_HEAD_INIT(0, 0) /* Header */
977 "SSLRand", /* @tp_name@ */
978 sizeof(grand_pyobj), /* @tp_basicsize@ */
979 0, /* @tp_itemsize@ */
981 grand_pydealloc, /* @tp_dealloc@ */
983 0, /* @tp_getattr@ */
984 0, /* @tp_setattr@ */
985 0, /* @tp_compare@ */
987 0, /* @tp_as_number@ */
988 0, /* @tp_as_sequence@ */
989 0, /* @tp_as_mapping@ */
993 0, /* @tp_getattro@ */
994 0, /* @tp_setattro@ */
995 0, /* @tp_as_buffer@ */
996 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1000 "SSLRand(KEY, SEED, [ohash = md5], [ihash = sha]):\n"
1001 " RNG for SSL master secret.",
1003 0, /* @tp_traverse@ */
1005 0, /* @tp_richcompare@ */
1006 0, /* @tp_weaklistoffset@ */
1008 0, /* @tp_iternext@ */
1009 0, /* @tp_methods@ */
1010 0, /* @tp_members@ */
1011 0, /* @tp_getset@ */
1014 0, /* @tp_descr_get@ */
1015 0, /* @tp_descr_set@ */
1016 0, /* @tp_dictoffset@ */
1018 PyType_GenericAlloc, /* @tp_alloc@ */
1019 sslprf_pynew, /* @tp_new@ */
1024 static const PyTypeObject tlsdx_pytype_skel = {
1025 PyVarObject_HEAD_INIT(0, 0) /* Header */
1026 "TLSDataExpansion", /* @tp_name@ */
1027 sizeof(grand_pyobj), /* @tp_basicsize@ */
1028 0, /* @tp_itemsize@ */
1030 grand_pydealloc, /* @tp_dealloc@ */
1032 0, /* @tp_getattr@ */
1033 0, /* @tp_setattr@ */
1034 0, /* @tp_compare@ */
1036 0, /* @tp_as_number@ */
1037 0, /* @tp_as_sequence@ */
1038 0, /* @tp_as_mapping@ */
1042 0, /* @tp_getattro@ */
1043 0, /* @tp_setattro@ */
1044 0, /* @tp_as_buffer@ */
1045 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1046 Py_TPFLAGS_BASETYPE,
1049 "TLSDataExpansion(KEY, SEED, [mac = sha_hmac]):\n"
1050 " TLS data expansion function.",
1052 0, /* @tp_traverse@ */
1054 0, /* @tp_richcompare@ */
1055 0, /* @tp_weaklistoffset@ */
1057 0, /* @tp_iternext@ */
1058 0, /* @tp_methods@ */
1059 0, /* @tp_members@ */
1060 0, /* @tp_getset@ */
1063 0, /* @tp_descr_get@ */
1064 0, /* @tp_descr_set@ */
1065 0, /* @tp_dictoffset@ */
1067 PyType_GenericAlloc, /* @tp_alloc@ */
1068 tlsdx_pynew, /* @tp_new@ */
1073 static const PyTypeObject tlsprf_pytype_skel = {
1074 PyVarObject_HEAD_INIT(0, 0) /* Header */
1075 "TLSPRF", /* @tp_name@ */
1076 sizeof(grand_pyobj), /* @tp_basicsize@ */
1077 0, /* @tp_itemsize@ */
1079 grand_pydealloc, /* @tp_dealloc@ */
1081 0, /* @tp_getattr@ */
1082 0, /* @tp_setattr@ */
1083 0, /* @tp_compare@ */
1085 0, /* @tp_as_number@ */
1086 0, /* @tp_as_sequence@ */
1087 0, /* @tp_as_mapping@ */
1091 0, /* @tp_getattro@ */
1092 0, /* @tp_setattro@ */
1093 0, /* @tp_as_buffer@ */
1094 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1095 Py_TPFLAGS_BASETYPE,
1098 "TLSPRF(KEY, SEED, [lmac = md5_hmac], [rmac = sha_hmac]):\n"
1099 " TLS pseudorandom function.",
1101 0, /* @tp_traverse@ */
1103 0, /* @tp_richcompare@ */
1104 0, /* @tp_weaklistoffset@ */
1106 0, /* @tp_iternext@ */
1107 0, /* @tp_methods@ */
1108 0, /* @tp_members@ */
1109 0, /* @tp_getset@ */
1112 0, /* @tp_descr_get@ */
1113 0, /* @tp_descr_set@ */
1114 0, /* @tp_dictoffset@ */
1116 PyType_GenericAlloc, /* @tp_alloc@ */
1117 tlsprf_pynew, /* @tp_new@ */
1122 /*----- DSA generator -----------------------------------------------------*/
1124 static PyObject *dsarand_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1129 static const char *const kwlist[] = { "seed", 0 };
1131 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", KWLIST, &p, &sz))
1133 rc = grand_dopywrap(ty, dsarand_create(p, sz), f_freeme);
1138 static PyObject *drget_seed(PyObject *me, void *hunoz)
1140 grand *r = GRAND_R(me);
1141 int n = r->ops->misc(r, DSARAND_SEEDSZ);
1142 PyObject *rc = bytestring_pywrap(0, n);
1143 r->ops->misc(r, DSARAND_GETSEED, PyString_AS_STRING(rc));
1147 static const PyGetSetDef dsarand_pygetset[] = {
1148 #define GETSETNAME(op, name) dr##op##_##name
1149 GET (seed, "R.seed -> current generator seed")
1154 static const PyTypeObject dsarand_pytype_skel = {
1155 PyVarObject_HEAD_INIT(0, 0) /* Header */
1156 "DSARand", /* @tp_name@ */
1157 sizeof(grand_pyobj), /* @tp_basicsize@ */
1158 0, /* @tp_itemsize@ */
1160 grand_pydealloc, /* @tp_dealloc@ */
1162 0, /* @tp_getattr@ */
1163 0, /* @tp_setattr@ */
1164 0, /* @tp_compare@ */
1166 0, /* @tp_as_number@ */
1167 0, /* @tp_as_sequence@ */
1168 0, /* @tp_as_mapping@ */
1172 0, /* @tp_getattro@ */
1173 0, /* @tp_setattro@ */
1174 0, /* @tp_as_buffer@ */
1175 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1176 Py_TPFLAGS_BASETYPE,
1179 "DSARand(SEED): pseudorandom number generator for DSA parameters.",
1181 0, /* @tp_traverse@ */
1183 0, /* @tp_richcompare@ */
1184 0, /* @tp_weaklistoffset@ */
1186 0, /* @tp_iternext@ */
1187 0, /* @tp_methods@ */
1188 0, /* @tp_members@ */
1189 PYGETSET(dsarand), /* @tp_getset@ */
1192 0, /* @tp_descr_get@ */
1193 0, /* @tp_descr_set@ */
1194 0, /* @tp_dictoffset@ */
1196 PyType_GenericAlloc, /* @tp_alloc@ */
1197 dsarand_pynew, /* @tp_new@ */
1202 /*----- Blum-Blum-Shub generator ------------------------------------------*/
1204 static PyObject *bbs_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1206 mp *n = 0, *x = MP_TWO;
1208 static const char *const kwlist[] = { "n", "x", 0 };
1210 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:new", KWLIST,
1211 convmp, &n, convmp, &x))
1213 rc = grand_dopywrap(ty, bbs_rand(n, x), f_freeme);
1220 static PyObject *bbsmeth_step(PyObject *me)
1221 { grand *r = GRAND_R(me); r->ops->misc(r, BBS_STEP); RETURN_ME; }
1223 static PyObject *bbsmeth_bits(PyObject *me, PyObject *arg)
1225 grand *r = GRAND_R(me); unsigned n; uint32 w;
1226 if (!PyArg_ParseTuple(arg, "O&:bits", convuint, &n)) goto end;
1227 if (n > 32) VALERR("can't get more than 32 bits");
1228 r->ops->misc(r, BBS_BITS, n, &w); return (getulong(w));
1233 static PyObject *bbsmeth_wrap(PyObject *me)
1234 { grand *r = GRAND_R(me); r->ops->misc(r, BBS_WRAP); RETURN_ME; }
1236 static PyObject *bbsget_n(PyObject *me, void *hunoz)
1238 mp *x = MP_NEW; grand *r = GRAND_R(me);
1239 r->ops->misc(r, BBS_MOD, &x); return (mp_pywrap(x));
1242 static PyObject *bbsget_x(PyObject *me, void *hunoz)
1244 mp *x = MP_NEW; grand *r = GRAND_R(me);
1245 r->ops->misc(r, BBS_STATE, &x); return (mp_pywrap(x));
1248 static int bbsset_x(PyObject *me, PyObject *val, void *hunoz)
1250 mp *x = 0; grand *r = GRAND_R(me); int rc = -1; if (!val) NIERR("__del__");
1251 if ((x = getmp(val)) == 0) goto end;
1252 r->ops->misc(r, BBS_SET, x); rc = 0;
1253 end: mp_drop(x); return (rc);
1256 static PyObject *bbsget_stepsz(PyObject *me, void *hunoz)
1258 grand *r = GRAND_R(me);
1259 return (PyInt_FromLong(r->ops->misc(r, BBS_STEPSZ)));
1262 static const PyMethodDef bbs_pymethods[] = {
1263 #define METHNAME(name) bbsmeth_##name
1264 NAMETH(step, "R.step(): steps the generator (not useful)")
1265 METH (bits, "R.bits(N) -> W: returns N bits (<= 32) from the generator")
1266 NAMETH(wrap, "R.wrap(): flushes unused bits in internal buffer")
1271 static const PyGetSetDef bbs_pygetset[] = {
1272 #define GETSETNAME(op, name) bbs##op##_##name
1273 GET (n, "R.n -> Blum modulus")
1274 GETSET(x, "R.x -> current seed value")
1275 GET (stepsz, "R.stepsz -> number of bits generated per step")
1280 static const PyTypeObject bbs_pytype_skel = {
1281 PyVarObject_HEAD_INIT(0, 0) /* Header */
1282 "BlumBlumShub", /* @tp_name@ */
1283 sizeof(grand_pyobj), /* @tp_basicsize@ */
1284 0, /* @tp_itemsize@ */
1286 grand_pydealloc, /* @tp_dealloc@ */
1288 0, /* @tp_getattr@ */
1289 0, /* @tp_setattr@ */
1290 0, /* @tp_compare@ */
1292 0, /* @tp_as_number@ */
1293 0, /* @tp_as_sequence@ */
1294 0, /* @tp_as_mapping@ */
1298 0, /* @tp_getattro@ */
1299 0, /* @tp_setattro@ */
1300 0, /* @tp_as_buffer@ */
1301 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1302 Py_TPFLAGS_BASETYPE,
1305 "BlumBlumShub(N, [x = 2]): Blum-Blum-Shub pseudorandom number generator.",
1307 0, /* @tp_traverse@ */
1309 0, /* @tp_richcompare@ */
1310 0, /* @tp_weaklistoffset@ */
1312 0, /* @tp_iternext@ */
1313 PYMETHODS(bbs), /* @tp_methods@ */
1314 0, /* @tp_members@ */
1315 PYGETSET(bbs), /* @tp_getset@ */
1318 0, /* @tp_descr_get@ */
1319 0, /* @tp_descr_set@ */
1320 0, /* @tp_dictoffset@ */
1322 PyType_GenericAlloc, /* @tp_alloc@ */
1323 bbs_pynew, /* @tp_new@ */
1328 typedef struct bbspriv_pyobj {
1333 #define BBSPRIV_BP(o) (&((bbspriv_pyobj *)(o))->bp)
1335 static PyObject *bbspriv_pynew(PyTypeObject *ty,
1336 PyObject *arg, PyObject *kw)
1338 mp *p = 0, *q = 0, *n = 0, *x = MP_TWO;
1339 bbspriv_pyobj *rc = 0;
1340 static const char *const kwlist[] = { "n", "p", "q", "seed", 0 };
1342 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&O&O&O&:new", KWLIST,
1343 convmp, &n, convmp, &p, convmp, &q,
1346 if (!n + !p + !q > 1) VALERR("must specify at least two of n, p, q");
1347 if (!n) n = mp_mul(MP_NEW, p, q);
1348 else if (!p) mp_div(&p, 0, n, q);
1349 else if (!q) mp_div(&q, 0, n, p);
1350 rc = (bbspriv_pyobj *)ty->tp_alloc(ty, 0);
1351 rc->gr.r = bbs_rand(n, x);
1352 rc->gr.f = f_freeme;
1353 rc->bp.p = MP_COPY(p);
1354 rc->bp.q = MP_COPY(q);
1355 rc->bp.n = MP_COPY(n);
1357 mp_drop(p); mp_drop(q); mp_drop(n); mp_drop(x);
1358 return ((PyObject *)rc);
1361 static PyObject *bpmeth_generate(PyObject *me, PyObject *arg, PyObject *kw)
1363 bbs_priv bp = { 0 };
1365 struct excinfo exc = EXCINFO_INIT;
1366 pypgev evt = { { 0 } };
1367 unsigned nbits, n = 0;
1368 grand *r = &rand_global;
1369 static const char *const kwlist[] =
1370 { "nbits", "event", "rng", "nsteps", "seed", 0 };
1371 bbspriv_pyobj *rc = 0;
1374 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&O&O&O&:generate", KWLIST,
1375 convuint, &nbits, convpgev, &evt,
1376 convgrand, &r, convuint, &n, convmp, &x))
1378 if (bbs_gen(&bp, nbits, r, n, evt.ev.proc, evt.ev.ctx))
1380 rc = PyObject_New(bbspriv_pyobj, bbspriv_pytype);
1381 rc->gr.r = bbs_rand(bp.n, x);
1382 rc->gr.f = f_freeme;
1383 rc->bp.p = MP_COPY(bp.p);
1384 rc->bp.q = MP_COPY(bp.q);
1385 rc->bp.n = MP_COPY(bp.n);
1387 mp_drop(bp.p); mp_drop(bp.q); mp_drop(bp.n); mp_drop(x);
1388 return ((PyObject *)rc);
1391 static void bbspriv_pydealloc(PyObject *me)
1393 bbs_priv *bp = BBSPRIV_BP(me);
1397 grand_pydealloc(me);
1400 static PyObject *bpmeth_ff(PyObject *me, PyObject *arg)
1402 mp *n = 0; grand *r = GRAND_R(me); bbs_priv *bp = BBSPRIV_BP(me);
1403 if (!PyArg_ParseTuple(arg, "O&:ff", convmp, &n)) return (0);
1404 r->ops->misc(r, BBS_FF, bp, n); RETURN_ME;
1407 static PyObject *bpmeth_rew(PyObject *me, PyObject *arg)
1409 mp *n = 0; grand *r = GRAND_R(me); bbs_priv *bp = BBSPRIV_BP(me);
1410 if (!PyArg_ParseTuple(arg, "O&:rew", convmp, &n)) return (0);
1411 r->ops->misc(r, BBS_REW, bp, n); RETURN_ME;
1414 static PyObject *bpget_n(PyObject *me, void *hunoz)
1415 { return (mp_pywrap(MP_COPY(BBSPRIV_BP(me)->n))); }
1417 static PyObject *bpget_p(PyObject *me, void *hunoz)
1418 { return (mp_pywrap(MP_COPY(BBSPRIV_BP(me)->p))); }
1420 static PyObject *bpget_q(PyObject *me, void *hunoz)
1421 { return (mp_pywrap(MP_COPY(BBSPRIV_BP(me)->q))); }
1423 static const PyMethodDef bbspriv_pymethods[] = {
1424 #define METHNAME(name) bpmeth_##name
1425 METH (ff, "R.ff(N): fast-forward N places")
1426 METH (rew, "R.rew(N): rewind N places")
1427 KWSMTH(generate, "generate(NBITS, [event = pgen_nullev], "
1428 "[rng = rand], [nsteps = 0], [seed = 2]) -> R")
1433 static const PyGetSetDef bbspriv_pygetset[] = {
1434 #define GETSETNAME(op, name) bp##op##_##name
1435 GET (n, "R.n -> Blum modulus")
1436 GET (p, "R.p -> one of the factors of the modulus")
1437 GET (q, "R.q -> one of the factors of the modulus")
1442 static const PyTypeObject bbspriv_pytype_skel = {
1443 PyVarObject_HEAD_INIT(0, 0) /* Header */
1444 "BBSPriv", /* @tp_name@ */
1445 sizeof(bbspriv_pyobj), /* @tp_basicsize@ */
1446 0, /* @tp_itemsize@ */
1448 bbspriv_pydealloc, /* @tp_dealloc@ */
1450 0, /* @tp_getattr@ */
1451 0, /* @tp_setattr@ */
1452 0, /* @tp_compare@ */
1454 0, /* @tp_as_number@ */
1455 0, /* @tp_as_sequence@ */
1456 0, /* @tp_as_mapping@ */
1460 0, /* @tp_getattro@ */
1461 0, /* @tp_setattro@ */
1462 0, /* @tp_as_buffer@ */
1463 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1464 Py_TPFLAGS_BASETYPE,
1467 "BBSPriv(..., [seed = 2]): Blum-Blum-Shub, with private key.\n"
1468 " Keywords: n, p, q; must provide at least two",
1470 0, /* @tp_traverse@ */
1472 0, /* @tp_richcompare@ */
1473 0, /* @tp_weaklistoffset@ */
1475 0, /* @tp_iternext@ */
1476 PYMETHODS(bbspriv), /* @tp_methods@ */
1477 0, /* @tp_members@ */
1478 PYGETSET(bbspriv), /* @tp_getset@ */
1481 0, /* @tp_descr_get@ */
1482 0, /* @tp_descr_set@ */
1483 0, /* @tp_dictoffset@ */
1485 PyType_GenericAlloc, /* @tp_alloc@ */
1486 bbspriv_pynew, /* @tp_new@ */
1491 /*----- Global stuff ------------------------------------------------------*/
1493 static const struct nameval consts[] = {
1498 void rand_pyinit(void)
1500 INITTYPE(grand, root);
1501 INITTYPE(truerand, grand);
1502 INITTYPE(fibrand, grand);
1503 INITTYPE(lcrand, grand);
1504 INITTYPE(dsarand, grand);
1505 INITTYPE(bbs, grand);
1506 INITTYPE(bbspriv, bbs);
1507 INITTYPE(sslprf, grand);
1508 INITTYPE(tlsdx, grand);
1509 INITTYPE(tlsprf, grand);
1510 INITTYPE(gccrand, type);
1511 INITTYPE(gcrand, grand);
1512 INITTYPE(gclatinrand, gcrand);
1513 rand_noisesrc(RAND_GLOBAL, &noise_source);
1514 rand_seed(RAND_GLOBAL, 160);
1517 static const char *crand_namefn(const void *p)
1518 { const gccrand_info *const *cls = p; return (*cls ? (*cls)->name : 0); }
1519 static PyObject *crand_valfn(const void *p)
1520 { const gccrand_info *const *cls = p; return (gccrand_pywrap(*cls)); }
1522 void rand_pyinsert(PyObject *mod)
1524 INSERT("GRand", grand_pytype);
1525 INSERT("TrueRand", truerand_pytype);
1526 INSERT("LCRand", lcrand_pytype);
1527 INSERT("FibRand", fibrand_pytype);
1528 INSERT("SSLRand", sslprf_pytype);
1529 INSERT("TLSDataExpansion", tlsdx_pytype);
1530 INSERT("TLSPRF", tlsprf_pytype);
1531 INSERT("DSARand", dsarand_pytype);
1532 INSERT("BlumBlumShub", bbs_pytype);
1533 INSERT("BBSPriv", bbspriv_pytype);
1534 INSERT("GCCRand", gccrand_pytype);
1535 INSERT("GCRand", gcrand_pytype);
1536 INSERT("GCLatinRand", gclatinrand_pytype);
1537 rand_pyobj = grand_pywrap(&rand_global, 0); Py_INCREF(rand_pyobj);
1538 gccrands_dict = make_algtab(gcrandtab, sizeof(gccrand_info *),
1539 crand_namefn, crand_valfn);
1540 INSERT("gccrands", gccrands_dict); Py_INCREF(gccrands_dict);
1541 INSERT("rand", rand_pyobj);
1542 setconstants(mod, consts);
1545 /*----- That's all, folks -------------------------------------------------*/