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);
106 if (mm <= 0) goto notpos;
107 else if (mm <= 0xffffffff)
108 return (PyInt_FromLong(grand_range(GRAND_R(me), mm)));
110 if ((x = getmp(m)) == 0) goto end;
111 if (!MP_POSP(x)) goto notpos;
112 y = mprand_range(MP_NEW, x, GRAND_R(me), 0);
114 return (mp_pywrap(y));
116 VALERR("range must be strictly positive");
122 static PyObject *grmeth_mp(PyObject *me, PyObject *arg, PyObject *kw)
126 static const char *const kwlist[] = { "bits", "or", 0 };
128 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:mp", KWLIST,
129 convszt, &l, convmpw, &o))
131 if (grand_check(me)) return (0);
132 if (l < MPW_BITS && (o >> l)) VALERR("or mask too large");
133 return (mp_pywrap(mprand(MP_NEW, l, GRAND_R(me), o)));
138 static PyObject *grmeth_block(PyObject *me, PyObject *arg)
143 if (!PyArg_ParseTuple(arg, "O&:block", convulong, &n)) goto end;
144 if (grand_check(me)) return (0);
145 rc = bytestring_pywrap(0, n);
146 grand_fill(GRAND_R(me), BIN_PTR(rc), n);
151 static int checkop(grand *r, unsigned op, const char *what)
153 if (r->ops->misc(r, GRAND_CHECK, op)) return (0);
154 PyErr_Format(PyExc_TypeError, "operation %s not supported", what);
158 static PyObject *grmeth_seedint(PyObject *me, PyObject *arg)
161 grand *r = GRAND_R(me);
162 if (!PyArg_ParseTuple(arg, "i:seedint", &i) ||
163 grand_check(me) || checkop(r, GRAND_SEEDINT, "seedint"))
165 r->ops->misc(r, GRAND_SEEDINT, i);
171 static PyObject *grmeth_seedword(PyObject *me, PyObject *arg)
174 grand *r = GRAND_R(me);
175 if (!PyArg_ParseTuple(arg, "O&:seedword", convu32, &u) ||
176 grand_check(me) || checkop(r, GRAND_SEEDUINT32, "seedword"))
178 r->ops->misc(r, GRAND_SEEDUINT32, u);
184 static PyObject *grmeth_seedblock(PyObject *me, PyObject *arg)
187 grand *r = GRAND_R(me);
188 if (!PyArg_ParseTuple(arg, "O&:seedblock", convbin, &in) ||
189 grand_check(me) || checkop(r, GRAND_SEEDBLOCK, "seedblock"))
191 r->ops->misc(r, GRAND_SEEDBLOCK, in.p, (size_t)in.sz);
197 static PyObject *grmeth_seedmp(PyObject *me, PyObject *arg)
201 grand *r = GRAND_R(me);
202 if (!PyArg_ParseTuple(arg, "O:seedmp", &x) ||
203 grand_check(me) || checkop(r, GRAND_SEEDMP, "seedmp") ||
204 (xx = getmp(x)) == 0)
206 r->ops->misc(r, GRAND_SEEDMP, xx);
213 static PyObject *grmeth_seedrand(PyObject *me, PyObject *arg, PyObject *kw)
215 static const char *const kwlist[] = { "rng", 0 };
216 grand *r = GRAND_R(me);
217 grand *rr = &rand_global;
218 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:seedrand", KWLIST,
220 grand_check(me) || checkop(r, GRAND_SEEDRAND, "seedrand"))
222 r->ops->misc(r, GRAND_SEEDRAND, rr);
228 static PyObject *grmeth_mask(PyObject *me, PyObject *arg)
230 grand *r = GRAND_R(me);
232 const octet *p; size_t n;
236 if (!PyArg_ParseTuple(arg, "O&:mask", convbin, &in)) return (0);
237 if (grand_check(me)) return (0);
238 rc = bytestring_pywrap(0, in.sz);
239 q = (octet *)BIN_PTR(rc);
240 GR_FILL(r, q, in.sz);
241 p = in.p; n = in.sz; while (n--) *q++ ^= *p++;
245 static void grand_pydealloc(PyObject *me)
247 grand_pyobj *g = (grand_pyobj *)me;
248 if ((g->f & f_freeme) && g->r) GR_DESTROY(g->r);
252 static PyObject *grget_name(PyObject *me, void *hunoz)
253 { return (grand_check(me) ? 0 : TEXT_FROMSTR(GRAND_R(me)->ops->name)); }
255 static PyObject *grget_cryptop(PyObject *me, void *hunoz)
256 { return (grand_check(me) ? 0 : getbool(GRAND_R(me)->ops->f & GRAND_CRYPTO)); }
258 static const PyGetSetDef grand_pygetset[] = {
259 #define GETSETNAME(op, name) gr##op##_##name
260 GET (name, "R.name -> name of this kind of generator")
261 GET (cryptop, "R.cryptop -> flag: cryptographically strong?")
266 static const PyMethodDef grand_pymethods[] = {
267 #define METHNAME(name) grmeth_##name
268 NAMETH(byte, "R.byte() -> BYTE")
269 NAMETH(word, "R.word() -> WORD")
270 METH (block, "R.block(N) -> STRING")
271 KWMETH(mp, "R.mp(bits, [or = 0]) -> MP")
272 METH (range, "R.range(MAX) -> INT")
273 METH (mask, "R.mask(STR) -> STR")
274 METH (seedint, "R.seedint(I)")
275 METH (seedword, "R.seedword(I)")
276 METH (seedblock, "R.seedblock(BYTES)")
277 METH (seedmp, "R.seedmp(X)")
278 KWMETH(seedrand, "R.seedrand(RR)")
283 static const PyTypeObject grand_pytype_skel = {
284 PyVarObject_HEAD_INIT(0, 0) /* Header */
285 "GRand", /* @tp_name@ */
286 sizeof(grand_pyobj), /* @tp_basicsize@ */
287 0, /* @tp_itemsize@ */
289 grand_pydealloc, /* @tp_dealloc@ */
291 0, /* @tp_getattr@ */
292 0, /* @tp_setattr@ */
293 0, /* @tp_compare@ */
295 0, /* @tp_as_number@ */
296 0, /* @tp_as_sequence@ */
297 0, /* @tp_as_mapping@ */
301 0, /* @tp_getattro@ */
302 0, /* @tp_setattro@ */
303 0, /* @tp_as_buffer@ */
304 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
308 "Generic random number source.",
310 0, /* @tp_traverse@ */
312 0, /* @tp_richcompare@ */
313 0, /* @tp_weaklistoffset@ */
315 0, /* @tp_iternext@ */
316 PYMETHODS(grand), /* @tp_methods@ */
317 0, /* @tp_members@ */
318 PYGETSET(grand), /* @tp_getset@ */
321 0, /* @tp_descr_get@ */
322 0, /* @tp_descr_set@ */
323 0, /* @tp_dictoffset@ */
325 PyType_GenericAlloc, /* @tp_alloc@ */
326 abstract_pynew, /* @tp_new@ */
331 static PyObject *lcrand_pynew(PyTypeObject *me, PyObject *arg, PyObject *kw)
334 static const char *const kwlist[] = { "seed", 0 };
335 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", KWLIST, convu32, &n))
337 return (grand_dopywrap(lcrand_pytype, lcrand_create(n), f_freeme));
340 static const PyTypeObject lcrand_pytype_skel = {
341 PyVarObject_HEAD_INIT(0, 0) /* Header */
342 "LCRand", /* @tp_name@ */
343 sizeof(grand_pyobj), /* @tp_basicsize@ */
344 0, /* @tp_itemsize@ */
346 grand_pydealloc, /* @tp_dealloc@ */
348 0, /* @tp_getattr@ */
349 0, /* @tp_setattr@ */
350 0, /* @tp_compare@ */
352 0, /* @tp_as_number@ */
353 0, /* @tp_as_sequence@ */
354 0, /* @tp_as_mapping@ */
358 0, /* @tp_getattro@ */
359 0, /* @tp_setattro@ */
360 0, /* @tp_as_buffer@ */
361 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
365 "LCRand([seed = 0]): linear congruential generator.",
367 0, /* @tp_traverse@ */
369 0, /* @tp_richcompare@ */
370 0, /* @tp_weaklistoffset@ */
372 0, /* @tp_iternext@ */
373 0, /* @tp_methods@ */
374 0, /* @tp_members@ */
378 0, /* @tp_descr_get@ */
379 0, /* @tp_descr_set@ */
380 0, /* @tp_dictoffset@ */
382 PyType_GenericAlloc, /* @tp_alloc@ */
383 lcrand_pynew, /* @tp_new@ */
388 static PyObject *fibrand_pynew(PyTypeObject *me, PyObject *arg, PyObject *kw)
391 static const char *const kwlist[] = { "seed", 0 };
392 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", KWLIST, convu32, &n))
394 return (grand_dopywrap(fibrand_pytype, fibrand_create(n), f_freeme));
397 static const PyTypeObject fibrand_pytype_skel = {
398 PyVarObject_HEAD_INIT(0, 0) /* Header */
399 "FibRand", /* @tp_name@ */
400 sizeof(grand_pyobj), /* @tp_basicsize@ */
401 0, /* @tp_itemsize@ */
403 grand_pydealloc, /* @tp_dealloc@ */
405 0, /* @tp_getattr@ */
406 0, /* @tp_setattr@ */
407 0, /* @tp_compare@ */
409 0, /* @tp_as_number@ */
410 0, /* @tp_as_sequence@ */
411 0, /* @tp_as_mapping@ */
415 0, /* @tp_getattro@ */
416 0, /* @tp_setattro@ */
417 0, /* @tp_as_buffer@ */
418 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
422 "FibRand([seed = 0]): Fibonacci generator.",
424 0, /* @tp_traverse@ */
426 0, /* @tp_richcompare@ */
427 0, /* @tp_weaklistoffset@ */
429 0, /* @tp_iternext@ */
430 0, /* @tp_methods@ */
431 0, /* @tp_members@ */
435 0, /* @tp_descr_get@ */
436 0, /* @tp_descr_set@ */
437 0, /* @tp_dictoffset@ */
439 PyType_GenericAlloc, /* @tp_alloc@ */
440 fibrand_pynew, /* @tp_new@ */
445 /*----- True random generator ---------------------------------------------*/
447 static PyObject *trmeth_gate(PyObject *me)
448 { grand *r = GRAND_R(me); r->ops->misc(GRAND_R(me), RAND_GATE); RETURN_ME; }
450 static PyObject *trmeth_stretch(PyObject *me)
451 { grand *r = GRAND_R(me); r->ops->misc(r, RAND_STRETCH); RETURN_ME; }
453 static PyObject *trmeth_add(PyObject *me, PyObject *arg)
455 grand *r = GRAND_R(me);
456 struct bin in; unsigned goodbits;
457 if (!PyArg_ParseTuple(arg, "O&O&:add", convbin, &in, convuint, &goodbits))
459 r->ops->misc(r, RAND_ADD, in.p, (size_t)in.sz, goodbits);
463 static PyObject *trmeth_key(PyObject *me, PyObject *arg)
465 grand *r = GRAND_R(me);
467 if (!PyArg_ParseTuple(arg, "O&:key", convbin, &k)) return (0);
468 r->ops->misc(r, RAND_KEY, k.p, (size_t)k.sz);
472 static PyObject *trmeth_seed(PyObject *me, PyObject *arg)
474 grand *r = GRAND_R(me);
476 if (!PyArg_ParseTuple(arg, "O&:seed", convuint, &u)) return (0);
477 if (u > RAND_IBITS) VALERR("pointlessly large");
478 r->ops->misc(r, RAND_SEED, u);
484 static PyObject *trmeth_timer(PyObject *me)
485 { grand *r = GRAND_R(me); r->ops->misc(r, RAND_TIMER); RETURN_ME; }
487 static PyObject *truerand_pynew(PyTypeObject *ty,
488 PyObject *arg, PyObject *kw)
490 static const char *const kwlist[] = { 0 };
493 if (!PyArg_ParseTupleAndKeywords(arg, kw, ":new", KWLIST)) goto end;
495 r->ops->misc(r, RAND_NOISESRC, &noise_source);
496 r->ops->misc(r, RAND_SEED, 160);
497 rc = grand_dopywrap(ty, r, f_freeme);
502 static const PyMethodDef truerand_pymethods[] = {
503 #define METHNAME(name) trmeth_##name
504 NAMETH(gate, "R.gate()")
505 NAMETH(stretch, "R.stretch()")
506 METH (key, "R.key(BYTES)")
507 METH (seed, "R.seed(NBITS)")
508 METH (add, "R.add(BYTES, GOODBITS")
509 NAMETH(timer, "R.timer()")
514 static PyObject *trget_goodbits(PyObject *me, void *hunoz)
516 grand *r = GRAND_R(me);
517 return (PyInt_FromLong(r->ops->misc(r, RAND_GOODBITS)));
520 static const PyGetSetDef truerand_pygetset[] = {
521 #define GETSETNAME(op, name) tr##op##_##name
522 GET (goodbits, "R.goodbits -> good bits of entropy remaining")
527 static const PyTypeObject truerand_pytype_skel = {
528 PyVarObject_HEAD_INIT(0, 0) /* Header */
529 "TrueRand", /* @tp_name@ */
530 sizeof(grand_pyobj), /* @tp_basicsize@ */
531 0, /* @tp_itemsize@ */
533 grand_pydealloc, /* @tp_dealloc@ */
535 0, /* @tp_getattr@ */
536 0, /* @tp_setattr@ */
537 0, /* @tp_compare@ */
539 0, /* @tp_as_number@ */
540 0, /* @tp_as_sequence@ */
541 0, /* @tp_as_mapping@ */
545 0, /* @tp_getattro@ */
546 0, /* @tp_setattro@ */
547 0, /* @tp_as_buffer@ */
548 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
552 "TrueRand(): true random number source.",
554 0, /* @tp_traverse@ */
556 0, /* @tp_richcompare@ */
557 0, /* @tp_weaklistoffset@ */
559 0, /* @tp_iternext@ */
560 PYMETHODS(truerand), /* @tp_methods@ */
561 0, /* @tp_members@ */
562 PYGETSET(truerand), /* @tp_getset@ */
565 0, /* @tp_descr_get@ */
566 0, /* @tp_descr_set@ */
567 0, /* @tp_dictoffset@ */
569 PyType_GenericAlloc, /* @tp_alloc@ */
570 truerand_pynew, /* @tp_new@ */
575 /*----- Generators from symmetric encryption algorithms -------------------*/
577 static PyTypeObject *gccrand_pytype, *gcrand_pytype, *gclatinrand_pytype;
579 typedef grand *gcrand_func(const void *, size_t sz);
580 typedef grand *gcirand_func(const void *, size_t sz, uint32);
581 typedef grand *gcnrand_func(const void *, size_t sz, const void *);
582 typedef grand *gcshakerand_func(const void *, size_t,
583 const void *, size_t,
584 const void *, size_t);
585 typedef grand *gcshafuncrand_func(const void *, size_t,
586 const void *, size_t);
587 typedef grand *gckmacrand_func(const void *, size_t, const void *, size_t);
588 typedef struct gccrand_info {
596 #define RNGF_MASK 255u
606 typedef struct gccrand_pyobj {
608 const gccrand_info *info;
610 #define GCCRAND_INFO(o) (((gccrand_pyobj *)(o))->info)
612 #define GCCRAND_DEF(name, ksz, func, f, nsz) \
613 static const gccrand_info func##_info = \
614 { name, ksz, f, nsz, (gcrand_func *)func };
617 static const gccrand_info *const gcrandtab[] = {
618 #define GCCRAND_ENTRY(name, ksz, func, f, nsz) &func##_info,
623 static PyObject *gcrand_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
625 const gccrand_info *info = GCCRAND_INFO(ty);
626 static const char *const kwlist[] = { "key", 0 };
629 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", KWLIST, convbin, &k))
631 if (keysz(k.sz, info->keysz) != k.sz) VALERR("bad key length");
632 return (grand_dopywrap(ty, info->func(k.p, k.sz), f_freeme));
637 static PyObject *gcirand_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
639 const gccrand_info *info = GCCRAND_INFO(ty);
641 static const char *const kwlist[] = { "key", "i", 0 };
644 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&:new", KWLIST,
645 convbin, &k, convu32, &i))
647 if (keysz(k.sz, info->keysz) != k.sz) VALERR("bad key length");
648 return (grand_dopywrap(ty,
649 ((gcirand_func *)info->func)(k.p, k.sz, i),
655 static PyObject *gcnrand_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
657 const gccrand_info *info = GCCRAND_INFO(ty);
658 static const char *const kwlist[] = { "key", "nonce", 0 };
661 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&:new", KWLIST,
662 convbin, &k, convbin, &n))
664 if (keysz(k.sz, info->keysz) != k.sz) VALERR("bad key length");
665 if (n.sz != info->noncesz) VALERR("bad nonce length");
666 return (grand_dopywrap(ty,
667 ((gcnrand_func *)info->func)(k.p, k.sz, n.p),
673 static PyObject *gcshakyrand_pynew(PyTypeObject *ty,
674 PyObject *arg, PyObject *kw)
676 const gccrand_info *info = GCCRAND_INFO(ty);
678 *const kwlist_shake[] = { "key", "func", "perso", 0 },
679 *const kwlist_func[] = { "key", "perso", 0 };
680 struct bin k, f = { 0, 0 }, p = { 0, 0 };
682 if ((info->f&RNGF_MASK) == RNG_SHAKE
683 ? !PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&O&:new",
684 (/*unconst*/ char **)kwlist_shake,
686 convbin, &f, convbin, &p)
687 : !PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:new",
688 (/*unconst*/ char **)kwlist_func,
689 convbin, &k, convbin, &p))
691 if (keysz(k.sz, info->keysz) != k.sz) VALERR("bad key length");
692 return (grand_dopywrap(ty,
693 (info->f&RNGF_MASK) == RNG_SHAKE
694 ? ((gcshakerand_func *)info->func)(f.p, f.sz,
697 : ((gcshafuncrand_func *)info->func)(p.p, p.sz,
704 static PyObject *gccrand_pywrap(const gccrand_info *info)
706 gccrand_pyobj *g = newtype(gccrand_pytype, 0, info->name);
708 g->ty.ht_type.tp_basicsize = sizeof(grand_pyobj);
709 switch (info->f&RNGF_MASK) {
710 case RNG_LATIN: g->ty.ht_type.tp_base = gclatinrand_pytype; break;
711 default: g->ty.ht_type.tp_base = gcrand_pytype; break;
713 Py_INCREF(g->ty.ht_type.tp_base);
714 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
715 Py_TPFLAGS_BASETYPE |
716 Py_TPFLAGS_HEAPTYPE);
717 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
718 g->ty.ht_type.tp_free = 0;
719 switch (info->f&RNGF_MASK) {
720 case RNG_LATIN: g->ty.ht_type.tp_new = gcnrand_pynew; break;
721 case RNG_SEAL: g->ty.ht_type.tp_new = gcirand_pynew; break;
722 case RNG_SHAKE: case RNG_KMAC:
723 g->ty.ht_type.tp_new = gcshakyrand_pynew; break;
724 default: g->ty.ht_type.tp_new = gcrand_pynew; break;
726 typeready(&g->ty.ht_type);
727 return ((PyObject *)g);
730 static PyObject *gccrget_name(PyObject *me, void *hunoz)
731 { return (TEXT_FROMSTR(GCCRAND_INFO(me)->name)); }
732 static PyObject *gccrget_keysz(PyObject *me, void *hunoz)
733 { return (keysz_pywrap(GCCRAND_INFO(me)->keysz)); }
735 static PyObject *gclrmeth_tell(PyObject *me)
737 grand *r = GRAND_R(me);
741 r->ops->misc(r, SALSA20_TELLU64, &off);
746 static PyObject *gclrmeth_seek(PyObject *me, PyObject *arg)
748 grand *r = GRAND_R(me);
751 if (!PyArg_ParseTuple(arg, "O&:seek", convk64, &off)) return (0);
752 r->ops->misc(r, SALSA20_SEEKU64, off);
756 static const PyGetSetDef gccrand_pygetset[] = {
757 #define GETSETNAME(op, name) gccr##op##_##name
758 GET (keysz, "CR.keysz -> acceptable key sizes")
759 GET (name, "CR.name -> name of this kind of generator")
764 static const PyMethodDef gclatinrand_pymethods[] = {
765 #define METHNAME(name) gclrmeth_##name
766 NAMETH(tell, "R.tell() -> OFF")
767 METH (seek, "R.seek(OFF)")
772 static const PyTypeObject gccrand_pytype_skel = {
773 PyVarObject_HEAD_INIT(0, 0) /* Header */
774 "GCCRand", /* @tp_name@ */
775 sizeof(gccrand_pyobj), /* @tp_basicsize@ */
776 0, /* @tp_itemsize@ */
778 0, /* @tp_dealloc@ */
780 0, /* @tp_getattr@ */
781 0, /* @tp_setattr@ */
782 0, /* @tp_compare@ */
784 0, /* @tp_as_number@ */
785 0, /* @tp_as_sequence@ */
786 0, /* @tp_as_mapping@ */
790 0, /* @tp_getattro@ */
791 0, /* @tp_setattro@ */
792 0, /* @tp_as_buffer@ */
793 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
797 "Metaclass for symmetric crypto-based generators.",
799 0, /* @tp_traverse@ */
801 0, /* @tp_richcompare@ */
802 0, /* @tp_weaklistoffset@ */
804 0, /* @tp_iternext@ */
805 0, /* @tp_methods@ */
806 0, /* @tp_members@ */
807 PYGETSET(gccrand), /* @tp_getset@ */
810 0, /* @tp_descr_get@ */
811 0, /* @tp_descr_set@ */
812 0, /* @tp_dictoffset@ */
814 PyType_GenericAlloc, /* @tp_alloc@ */
815 abstract_pynew, /* @tp_new@ */
820 static const PyTypeObject gcrand_pytype_skel = {
821 PyVarObject_HEAD_INIT(0, 0) /* Header */
822 "GCRand", /* @tp_name@ */
823 sizeof(grand_pyobj), /* @tp_basicsize@ */
824 0, /* @tp_itemsize@ */
826 grand_pydealloc, /* @tp_dealloc@ */
828 0, /* @tp_getattr@ */
829 0, /* @tp_setattr@ */
830 0, /* @tp_compare@ */
832 0, /* @tp_as_number@ */
833 0, /* @tp_as_sequence@ */
834 0, /* @tp_as_mapping@ */
838 0, /* @tp_getattro@ */
839 0, /* @tp_setattro@ */
840 0, /* @tp_as_buffer@ */
841 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
845 "Abstract base class for symmetric crypto-based generators.",
847 0, /* @tp_traverse@ */
849 0, /* @tp_richcompare@ */
850 0, /* @tp_weaklistoffset@ */
852 0, /* @tp_iternext@ */
853 0, /* @tp_methods@ */
854 0, /* @tp_members@ */
858 0, /* @tp_descr_get@ */
859 0, /* @tp_descr_set@ */
860 0, /* @tp_dictoffset@ */
862 PyType_GenericAlloc, /* @tp_alloc@ */
863 abstract_pynew, /* @tp_new@ */
868 static const PyTypeObject gclatinrand_pytype_skel = {
869 PyVarObject_HEAD_INIT(0, 0) /* Header */
870 "GCLatinRand", /* @tp_name@ */
871 sizeof(grand_pyobj), /* @tp_basicsize@ */
872 0, /* @tp_itemsize@ */
874 grand_pydealloc, /* @tp_dealloc@ */
876 0, /* @tp_getattr@ */
877 0, /* @tp_setattr@ */
878 0, /* @tp_compare@ */
880 0, /* @tp_as_number@ */
881 0, /* @tp_as_sequence@ */
882 0, /* @tp_as_mapping@ */
886 0, /* @tp_getattro@ */
887 0, /* @tp_setattro@ */
888 0, /* @tp_as_buffer@ */
889 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
893 "Abstract base class for symmetric crypto-based generators.",
895 0, /* @tp_traverse@ */
897 0, /* @tp_richcompare@ */
898 0, /* @tp_weaklistoffset@ */
900 0, /* @tp_iternext@ */
901 PYMETHODS(gclatinrand), /* @tp_methods@ */
902 0, /* @tp_members@ */
906 0, /* @tp_descr_get@ */
907 0, /* @tp_descr_set@ */
908 0, /* @tp_dictoffset@ */
910 PyType_GenericAlloc, /* @tp_alloc@ */
911 abstract_pynew, /* @tp_new@ */
916 /*----- SSL and TLS generators --------------------------------------------*/
918 static PyObject *sslprf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
921 const gchash *hco = &md5, *hci = &sha;
923 static const char *const kwlist[] = { "key", "seed", "ohash", "ihash", 0 };
925 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&|O&O&:new", KWLIST,
926 convbin, &k, convbin, &s,
927 convgchash, &hco, convgchash, &hci))
929 rc = grand_dopywrap(ty, sslprf_rand(hco, hci, k.p, k.sz, s.p, s.sz),
935 static PyObject *tlsdx_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
938 const gcmac *mc = &sha_hmac;
940 static const char *const kwlist[] = { "key", "seed", "mac", 0 };
942 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&|O&:new", KWLIST,
943 convbin, &k, convbin, &s,
946 rc = grand_dopywrap(ty, tlsdx_rand(mc, k.p, k.sz, s.p, s.sz), f_freeme);
951 static PyObject *tlsprf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
954 const gcmac *mcl = &md5_hmac, *mcr = &sha_hmac;
956 static const char *const kwlist[] = { "key", "seed", "lmac", "rmac", 0 };
958 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&O&|O&O&:new", KWLIST,
959 convbin, &k, convbin, &s,
960 convgcmac, &mcl, convgcmac, &mcr))
962 rc = grand_dopywrap(ty, tlsprf_rand(mcl, mcr, k.p, k.sz, s.p, s.sz),
968 static const PyTypeObject sslprf_pytype_skel = {
969 PyVarObject_HEAD_INIT(0, 0) /* Header */
970 "SSLRand", /* @tp_name@ */
971 sizeof(grand_pyobj), /* @tp_basicsize@ */
972 0, /* @tp_itemsize@ */
974 grand_pydealloc, /* @tp_dealloc@ */
976 0, /* @tp_getattr@ */
977 0, /* @tp_setattr@ */
978 0, /* @tp_compare@ */
980 0, /* @tp_as_number@ */
981 0, /* @tp_as_sequence@ */
982 0, /* @tp_as_mapping@ */
986 0, /* @tp_getattro@ */
987 0, /* @tp_setattro@ */
988 0, /* @tp_as_buffer@ */
989 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
993 "SSLRand(KEY, SEED, [ohash = md5], [ihash = sha]):\n"
994 " RNG for SSL master secret.",
996 0, /* @tp_traverse@ */
998 0, /* @tp_richcompare@ */
999 0, /* @tp_weaklistoffset@ */
1001 0, /* @tp_iternext@ */
1002 0, /* @tp_methods@ */
1003 0, /* @tp_members@ */
1004 0, /* @tp_getset@ */
1007 0, /* @tp_descr_get@ */
1008 0, /* @tp_descr_set@ */
1009 0, /* @tp_dictoffset@ */
1011 PyType_GenericAlloc, /* @tp_alloc@ */
1012 sslprf_pynew, /* @tp_new@ */
1017 static const PyTypeObject tlsdx_pytype_skel = {
1018 PyVarObject_HEAD_INIT(0, 0) /* Header */
1019 "TLSDataExpansion", /* @tp_name@ */
1020 sizeof(grand_pyobj), /* @tp_basicsize@ */
1021 0, /* @tp_itemsize@ */
1023 grand_pydealloc, /* @tp_dealloc@ */
1025 0, /* @tp_getattr@ */
1026 0, /* @tp_setattr@ */
1027 0, /* @tp_compare@ */
1029 0, /* @tp_as_number@ */
1030 0, /* @tp_as_sequence@ */
1031 0, /* @tp_as_mapping@ */
1035 0, /* @tp_getattro@ */
1036 0, /* @tp_setattro@ */
1037 0, /* @tp_as_buffer@ */
1038 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1039 Py_TPFLAGS_BASETYPE,
1042 "TLSDataExpansion(KEY, SEED, [mac = sha_hmac]):\n"
1043 " TLS data expansion function.",
1045 0, /* @tp_traverse@ */
1047 0, /* @tp_richcompare@ */
1048 0, /* @tp_weaklistoffset@ */
1050 0, /* @tp_iternext@ */
1051 0, /* @tp_methods@ */
1052 0, /* @tp_members@ */
1053 0, /* @tp_getset@ */
1056 0, /* @tp_descr_get@ */
1057 0, /* @tp_descr_set@ */
1058 0, /* @tp_dictoffset@ */
1060 PyType_GenericAlloc, /* @tp_alloc@ */
1061 tlsdx_pynew, /* @tp_new@ */
1066 static const PyTypeObject tlsprf_pytype_skel = {
1067 PyVarObject_HEAD_INIT(0, 0) /* Header */
1068 "TLSPRF", /* @tp_name@ */
1069 sizeof(grand_pyobj), /* @tp_basicsize@ */
1070 0, /* @tp_itemsize@ */
1072 grand_pydealloc, /* @tp_dealloc@ */
1074 0, /* @tp_getattr@ */
1075 0, /* @tp_setattr@ */
1076 0, /* @tp_compare@ */
1078 0, /* @tp_as_number@ */
1079 0, /* @tp_as_sequence@ */
1080 0, /* @tp_as_mapping@ */
1084 0, /* @tp_getattro@ */
1085 0, /* @tp_setattro@ */
1086 0, /* @tp_as_buffer@ */
1087 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1088 Py_TPFLAGS_BASETYPE,
1091 "TLSPRF(KEY, SEED, [lmac = md5_hmac], [rmac = sha_hmac]):\n"
1092 " TLS pseudorandom function.",
1094 0, /* @tp_traverse@ */
1096 0, /* @tp_richcompare@ */
1097 0, /* @tp_weaklistoffset@ */
1099 0, /* @tp_iternext@ */
1100 0, /* @tp_methods@ */
1101 0, /* @tp_members@ */
1102 0, /* @tp_getset@ */
1105 0, /* @tp_descr_get@ */
1106 0, /* @tp_descr_set@ */
1107 0, /* @tp_dictoffset@ */
1109 PyType_GenericAlloc, /* @tp_alloc@ */
1110 tlsprf_pynew, /* @tp_new@ */
1115 /*----- DSA generator -----------------------------------------------------*/
1117 static PyObject *dsarand_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1121 static const char *const kwlist[] = { "seed", 0 };
1123 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&:new", KWLIST, convbin, &in))
1125 rc = grand_dopywrap(ty, dsarand_create(in.p, in.sz), f_freeme);
1130 static PyObject *drget_seed(PyObject *me, void *hunoz)
1132 grand *r = GRAND_R(me);
1133 int n = r->ops->misc(r, DSARAND_SEEDSZ);
1134 PyObject *rc = bytestring_pywrap(0, n);
1135 r->ops->misc(r, DSARAND_GETSEED, BIN_PTR(rc));
1139 static const PyGetSetDef dsarand_pygetset[] = {
1140 #define GETSETNAME(op, name) dr##op##_##name
1141 GET (seed, "R.seed -> current generator seed")
1146 static const PyTypeObject dsarand_pytype_skel = {
1147 PyVarObject_HEAD_INIT(0, 0) /* Header */
1148 "DSARand", /* @tp_name@ */
1149 sizeof(grand_pyobj), /* @tp_basicsize@ */
1150 0, /* @tp_itemsize@ */
1152 grand_pydealloc, /* @tp_dealloc@ */
1154 0, /* @tp_getattr@ */
1155 0, /* @tp_setattr@ */
1156 0, /* @tp_compare@ */
1158 0, /* @tp_as_number@ */
1159 0, /* @tp_as_sequence@ */
1160 0, /* @tp_as_mapping@ */
1164 0, /* @tp_getattro@ */
1165 0, /* @tp_setattro@ */
1166 0, /* @tp_as_buffer@ */
1167 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1168 Py_TPFLAGS_BASETYPE,
1171 "DSARand(SEED): pseudorandom number generator for DSA parameters.",
1173 0, /* @tp_traverse@ */
1175 0, /* @tp_richcompare@ */
1176 0, /* @tp_weaklistoffset@ */
1178 0, /* @tp_iternext@ */
1179 0, /* @tp_methods@ */
1180 0, /* @tp_members@ */
1181 PYGETSET(dsarand), /* @tp_getset@ */
1184 0, /* @tp_descr_get@ */
1185 0, /* @tp_descr_set@ */
1186 0, /* @tp_dictoffset@ */
1188 PyType_GenericAlloc, /* @tp_alloc@ */
1189 dsarand_pynew, /* @tp_new@ */
1194 /*----- Blum-Blum-Shub generator ------------------------------------------*/
1196 static PyObject *bbs_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1198 mp *n = 0, *x = MP_TWO;
1200 static const char *const kwlist[] = { "n", "x", 0 };
1202 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:new", KWLIST,
1203 convmp, &n, convmp, &x))
1205 rc = grand_dopywrap(ty, bbs_rand(n, x), f_freeme);
1212 static PyObject *bbsmeth_step(PyObject *me)
1213 { grand *r = GRAND_R(me); r->ops->misc(r, BBS_STEP); RETURN_ME; }
1215 static PyObject *bbsmeth_bits(PyObject *me, PyObject *arg)
1217 grand *r = GRAND_R(me); unsigned n; uint32 w;
1218 if (!PyArg_ParseTuple(arg, "O&:bits", convuint, &n)) goto end;
1219 if (n > 32) VALERR("can't get more than 32 bits");
1220 r->ops->misc(r, BBS_BITS, n, &w); return (getulong(w));
1225 static PyObject *bbsmeth_wrap(PyObject *me)
1226 { grand *r = GRAND_R(me); r->ops->misc(r, BBS_WRAP); RETURN_ME; }
1228 static PyObject *bbsget_n(PyObject *me, void *hunoz)
1230 mp *x = MP_NEW; grand *r = GRAND_R(me);
1231 r->ops->misc(r, BBS_MOD, &x); return (mp_pywrap(x));
1234 static PyObject *bbsget_x(PyObject *me, void *hunoz)
1236 mp *x = MP_NEW; grand *r = GRAND_R(me);
1237 r->ops->misc(r, BBS_STATE, &x); return (mp_pywrap(x));
1240 static int bbsset_x(PyObject *me, PyObject *val, void *hunoz)
1242 mp *x = 0; grand *r = GRAND_R(me); int rc = -1; if (!val) NIERR("__del__");
1243 if ((x = getmp(val)) == 0) goto end;
1244 r->ops->misc(r, BBS_SET, x); rc = 0;
1245 end: mp_drop(x); return (rc);
1248 static PyObject *bbsget_stepsz(PyObject *me, void *hunoz)
1250 grand *r = GRAND_R(me);
1251 return (PyInt_FromLong(r->ops->misc(r, BBS_STEPSZ)));
1254 static const PyMethodDef bbs_pymethods[] = {
1255 #define METHNAME(name) bbsmeth_##name
1256 NAMETH(step, "R.step(): steps the generator (not useful)")
1257 METH (bits, "R.bits(N) -> W: returns N bits (<= 32) from the generator")
1258 NAMETH(wrap, "R.wrap(): flushes unused bits in internal buffer")
1263 static const PyGetSetDef bbs_pygetset[] = {
1264 #define GETSETNAME(op, name) bbs##op##_##name
1265 GET (n, "R.n -> Blum modulus")
1266 GETSET(x, "R.x -> current seed value")
1267 GET (stepsz, "R.stepsz -> number of bits generated per step")
1272 static const PyTypeObject bbs_pytype_skel = {
1273 PyVarObject_HEAD_INIT(0, 0) /* Header */
1274 "BlumBlumShub", /* @tp_name@ */
1275 sizeof(grand_pyobj), /* @tp_basicsize@ */
1276 0, /* @tp_itemsize@ */
1278 grand_pydealloc, /* @tp_dealloc@ */
1280 0, /* @tp_getattr@ */
1281 0, /* @tp_setattr@ */
1282 0, /* @tp_compare@ */
1284 0, /* @tp_as_number@ */
1285 0, /* @tp_as_sequence@ */
1286 0, /* @tp_as_mapping@ */
1290 0, /* @tp_getattro@ */
1291 0, /* @tp_setattro@ */
1292 0, /* @tp_as_buffer@ */
1293 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1294 Py_TPFLAGS_BASETYPE,
1297 "BlumBlumShub(N, [x = 2]): Blum-Blum-Shub pseudorandom number generator.",
1299 0, /* @tp_traverse@ */
1301 0, /* @tp_richcompare@ */
1302 0, /* @tp_weaklistoffset@ */
1304 0, /* @tp_iternext@ */
1305 PYMETHODS(bbs), /* @tp_methods@ */
1306 0, /* @tp_members@ */
1307 PYGETSET(bbs), /* @tp_getset@ */
1310 0, /* @tp_descr_get@ */
1311 0, /* @tp_descr_set@ */
1312 0, /* @tp_dictoffset@ */
1314 PyType_GenericAlloc, /* @tp_alloc@ */
1315 bbs_pynew, /* @tp_new@ */
1320 typedef struct bbspriv_pyobj {
1325 #define BBSPRIV_BP(o) (&((bbspriv_pyobj *)(o))->bp)
1327 static PyObject *bbspriv_pynew(PyTypeObject *ty,
1328 PyObject *arg, PyObject *kw)
1330 mp *p = 0, *q = 0, *n = 0, *x = MP_TWO;
1331 bbspriv_pyobj *rc = 0;
1332 static const char *const kwlist[] = { "n", "p", "q", "seed", 0 };
1334 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&O&O&O&:new", KWLIST,
1335 convmp, &n, convmp, &p, convmp, &q,
1338 if (!n + !p + !q > 1) VALERR("must specify at least two of n, p, q");
1339 if (!n) n = mp_mul(MP_NEW, p, q);
1340 else if (!p) mp_div(&p, 0, n, q);
1341 else if (!q) mp_div(&q, 0, n, p);
1342 rc = (bbspriv_pyobj *)ty->tp_alloc(ty, 0);
1343 rc->gr.r = bbs_rand(n, x);
1344 rc->gr.f = f_freeme;
1345 rc->bp.p = MP_COPY(p);
1346 rc->bp.q = MP_COPY(q);
1347 rc->bp.n = MP_COPY(n);
1349 mp_drop(p); mp_drop(q); mp_drop(n); mp_drop(x);
1350 return ((PyObject *)rc);
1353 static PyObject *bpmeth_generate(PyObject *me, PyObject *arg, PyObject *kw)
1355 bbs_priv bp = { 0 };
1357 struct excinfo exc = EXCINFO_INIT;
1358 pypgev evt = { { 0 } };
1359 unsigned nbits, n = 0;
1360 grand *r = &rand_global;
1361 static const char *const kwlist[] =
1362 { "nbits", "event", "rng", "nsteps", "seed", 0 };
1363 bbspriv_pyobj *rc = 0;
1366 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&O&O&O&:generate", KWLIST,
1367 convuint, &nbits, convpgev, &evt,
1368 convgrand, &r, convuint, &n, convmp, &x))
1370 if (bbs_gen(&bp, nbits, r, n, evt.ev.proc, evt.ev.ctx))
1372 rc = PyObject_New(bbspriv_pyobj, bbspriv_pytype);
1373 rc->gr.r = bbs_rand(bp.n, x);
1374 rc->gr.f = f_freeme;
1375 rc->bp.p = MP_COPY(bp.p);
1376 rc->bp.q = MP_COPY(bp.q);
1377 rc->bp.n = MP_COPY(bp.n);
1379 mp_drop(bp.p); mp_drop(bp.q); mp_drop(bp.n); mp_drop(x);
1380 return ((PyObject *)rc);
1383 static void bbspriv_pydealloc(PyObject *me)
1385 bbs_priv *bp = BBSPRIV_BP(me);
1389 grand_pydealloc(me);
1392 static PyObject *bpmeth_ff(PyObject *me, PyObject *arg)
1394 mp *n = 0; grand *r = GRAND_R(me); bbs_priv *bp = BBSPRIV_BP(me);
1395 if (!PyArg_ParseTuple(arg, "O&:ff", convmp, &n)) return (0);
1396 r->ops->misc(r, BBS_FF, bp, n); RETURN_ME;
1399 static PyObject *bpmeth_rew(PyObject *me, PyObject *arg)
1401 mp *n = 0; grand *r = GRAND_R(me); bbs_priv *bp = BBSPRIV_BP(me);
1402 if (!PyArg_ParseTuple(arg, "O&:rew", convmp, &n)) return (0);
1403 r->ops->misc(r, BBS_REW, bp, n); RETURN_ME;
1406 static PyObject *bpget_n(PyObject *me, void *hunoz)
1407 { return (mp_pywrap(MP_COPY(BBSPRIV_BP(me)->n))); }
1409 static PyObject *bpget_p(PyObject *me, void *hunoz)
1410 { return (mp_pywrap(MP_COPY(BBSPRIV_BP(me)->p))); }
1412 static PyObject *bpget_q(PyObject *me, void *hunoz)
1413 { return (mp_pywrap(MP_COPY(BBSPRIV_BP(me)->q))); }
1415 static const PyMethodDef bbspriv_pymethods[] = {
1416 #define METHNAME(name) bpmeth_##name
1417 METH (ff, "R.ff(N): fast-forward N places")
1418 METH (rew, "R.rew(N): rewind N places")
1419 KWSMTH(generate, "generate(NBITS, [event = pgen_nullev], "
1420 "[rng = rand], [nsteps = 0], [seed = 2]) -> R")
1425 static const PyGetSetDef bbspriv_pygetset[] = {
1426 #define GETSETNAME(op, name) bp##op##_##name
1427 GET (n, "R.n -> Blum modulus")
1428 GET (p, "R.p -> one of the factors of the modulus")
1429 GET (q, "R.q -> one of the factors of the modulus")
1434 static const PyTypeObject bbspriv_pytype_skel = {
1435 PyVarObject_HEAD_INIT(0, 0) /* Header */
1436 "BBSPriv", /* @tp_name@ */
1437 sizeof(bbspriv_pyobj), /* @tp_basicsize@ */
1438 0, /* @tp_itemsize@ */
1440 bbspriv_pydealloc, /* @tp_dealloc@ */
1442 0, /* @tp_getattr@ */
1443 0, /* @tp_setattr@ */
1444 0, /* @tp_compare@ */
1446 0, /* @tp_as_number@ */
1447 0, /* @tp_as_sequence@ */
1448 0, /* @tp_as_mapping@ */
1452 0, /* @tp_getattro@ */
1453 0, /* @tp_setattro@ */
1454 0, /* @tp_as_buffer@ */
1455 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1456 Py_TPFLAGS_BASETYPE,
1459 "BBSPriv(..., [seed = 2]): Blum-Blum-Shub, with private key.\n"
1460 " Keywords: n, p, q; must provide at least two",
1462 0, /* @tp_traverse@ */
1464 0, /* @tp_richcompare@ */
1465 0, /* @tp_weaklistoffset@ */
1467 0, /* @tp_iternext@ */
1468 PYMETHODS(bbspriv), /* @tp_methods@ */
1469 0, /* @tp_members@ */
1470 PYGETSET(bbspriv), /* @tp_getset@ */
1473 0, /* @tp_descr_get@ */
1474 0, /* @tp_descr_set@ */
1475 0, /* @tp_dictoffset@ */
1477 PyType_GenericAlloc, /* @tp_alloc@ */
1478 bbspriv_pynew, /* @tp_new@ */
1483 /*----- Global stuff ------------------------------------------------------*/
1485 static const struct nameval consts[] = {
1490 void rand_pyinit(void)
1492 INITTYPE(grand, root);
1493 INITTYPE(truerand, grand);
1494 INITTYPE(fibrand, grand);
1495 INITTYPE(lcrand, grand);
1496 INITTYPE(dsarand, grand);
1497 INITTYPE(bbs, grand);
1498 INITTYPE(bbspriv, bbs);
1499 INITTYPE(sslprf, grand);
1500 INITTYPE(tlsdx, grand);
1501 INITTYPE(tlsprf, grand);
1502 INITTYPE(gccrand, type);
1503 INITTYPE(gcrand, grand);
1504 INITTYPE(gclatinrand, gcrand);
1505 rand_noisesrc(RAND_GLOBAL, &noise_source);
1506 rand_seed(RAND_GLOBAL, 160);
1509 static const char *crand_namefn(const void *p)
1510 { const gccrand_info *const *cls = p; return (*cls ? (*cls)->name : 0); }
1511 static PyObject *crand_valfn(const void *p)
1512 { const gccrand_info *const *cls = p; return (gccrand_pywrap(*cls)); }
1514 void rand_pyinsert(PyObject *mod)
1516 INSERT("GRand", grand_pytype);
1517 INSERT("TrueRand", truerand_pytype);
1518 INSERT("LCRand", lcrand_pytype);
1519 INSERT("FibRand", fibrand_pytype);
1520 INSERT("SSLRand", sslprf_pytype);
1521 INSERT("TLSDataExpansion", tlsdx_pytype);
1522 INSERT("TLSPRF", tlsprf_pytype);
1523 INSERT("DSARand", dsarand_pytype);
1524 INSERT("BlumBlumShub", bbs_pytype);
1525 INSERT("BBSPriv", bbspriv_pytype);
1526 INSERT("GCCRand", gccrand_pytype);
1527 INSERT("GCRand", gcrand_pytype);
1528 INSERT("GCLatinRand", gclatinrand_pytype);
1529 rand_pyobj = grand_pywrap(&rand_global, 0); Py_INCREF(rand_pyobj);
1530 gccrands_dict = make_algtab(gcrandtab, sizeof(gccrand_info *),
1531 crand_namefn, crand_valfn);
1532 INSERT("gccrands", gccrands_dict); Py_INCREF(gccrands_dict);
1533 INSERT("rand", rand_pyobj);
1534 setconstants(mod, consts);
1537 /*----- That's all, folks -------------------------------------------------*/