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
, *truerand_pytype
;
37 PyTypeObject
*lcrand_pytype
, *fibrand_pytype
;
38 PyTypeObject
*dsarand_pytype
, *bbs_pytype
, *bbspriv_pytype
;
39 PyTypeObject
*sslprf_pytype
, *tlsdx_pytype
, *tlsprf_pytype
;
42 static PyObject
*gccrands_dict
;
44 static PyObject
*grand_dopywrap(PyTypeObject
*ty
, grand
*r
, unsigned f
)
48 g
= (grand_pyobj
*)ty
->tp_alloc(ty
, 0);
51 return ((PyObject
*)g
);
54 PyObject
*grand_pywrap(grand
*r
, unsigned f
)
56 PyTypeObject
*ty
= grand_pytype
;
59 if (STRCMP(r
->ops
->name
, ==, "rand")) ty
= truerand_pytype
;
60 else if (STRCMP(r
->ops
->name
, ==, "lcrand")) ty
= lcrand_pytype
;
61 else if (STRCMP(r
->ops
->name
, ==, "fibrand")) ty
= fibrand_pytype
;
62 else if (STRCMP(r
->ops
->name
, ==, "dsarand")) ty
= dsarand_pytype
;
63 else if (STRCMP(r
->ops
->name
, ==, "bbs")) ty
= bbs_pytype
;
64 else if (STRCMP(r
->ops
->name
, ==, "sslprf")) ty
= sslprf_pytype
;
65 else if (STRCMP(r
->ops
->name
, ==, "tlsdx")) ty
= tlsdx_pytype
;
66 else if (STRCMP(r
->ops
->name
, ==, "tlsprf")) ty
= tlsprf_pytype
;
67 else if ((ob
= PyDict_GetItemString(gccrands_dict
, r
->ops
->name
)) != 0)
68 ty
= (PyTypeObject
*)ob
;
69 return (grand_dopywrap(ty
, r
, f
));
72 CONVFUNC(grand
, grand
*, GRAND_R
)
74 static int grand_check(PyObject
*me
)
76 if (!GRAND_R(me
)) VALERR("random generator object is no longer valid");
82 static PyObject
*grmeth_byte(PyObject
*me
, PyObject
*arg
)
84 if (!PyArg_ParseTuple(arg
, ":byte")) return (0);
85 if (grand_check(me
)) return (0);
86 return (PyInt_FromLong(grand_byte(GRAND_R(me
))));
89 static PyObject
*grmeth_word(PyObject
*me
, PyObject
*arg
)
91 if (!PyArg_ParseTuple(arg
, ":word")) return (0);
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 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 PyMethodDef grand_pymethods
[] = {
270 #define METHNAME(name) grmeth_##name
271 METH (byte
, "R.byte() -> BYTE")
272 METH (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 PyTypeObject grand_pytype_skel
= {
287 PyObject_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 grand_pymethods
, /* @tp_methods@ */
320 0, /* @tp_members@ */
321 grand_pygetset
, /* @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 PyTypeObject lcrand_pytype_skel
= {
344 PyObject_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 PyTypeObject fibrand_pytype_skel
= {
401 PyObject_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
, PyObject
*arg
)
452 grand
*r
= GRAND_R(me
);
453 if (!PyArg_ParseTuple(arg
, ":gate")) return (0);
454 r
->ops
->misc(r
, RAND_GATE
);
458 static PyObject
*trmeth_stretch(PyObject
*me
, PyObject
*arg
)
460 grand
*r
= GRAND_R(me
);
461 if (!PyArg_ParseTuple(arg
, ":stretch")) return (0);
462 r
->ops
->misc(r
, RAND_STRETCH
);
466 static PyObject
*trmeth_add(PyObject
*me
, PyObject
*arg
)
468 grand
*r
= GRAND_R(me
);
469 char *p
; Py_ssize_t n
; unsigned goodbits
;
470 if (!PyArg_ParseTuple(arg
, "s#O&:add", &p
, &n
, convuint
, &goodbits
))
472 r
->ops
->misc(r
, RAND_ADD
, p
, (size_t)n
, goodbits
);
476 static PyObject
*trmeth_key(PyObject
*me
, PyObject
*arg
)
478 grand
*r
= GRAND_R(me
);
479 char *p
; Py_ssize_t n
;
480 if (!PyArg_ParseTuple(arg
, "s#:key", &p
, &n
)) return (0);
481 r
->ops
->misc(r
, RAND_KEY
, p
, (size_t)n
);
485 static PyObject
*trmeth_seed(PyObject
*me
, PyObject
*arg
)
487 grand
*r
= GRAND_R(me
);
489 if (!PyArg_ParseTuple(arg
, "O&:seed", convuint
, &u
)) return (0);
490 if (u
> RAND_IBITS
) VALERR("pointlessly large");
491 r
->ops
->misc(r
, RAND_SEED
, u
);
497 static PyObject
*trmeth_timer(PyObject
*me
, PyObject
*arg
)
499 grand
*r
= GRAND_R(me
);
500 if (!PyArg_ParseTuple(arg
, ":timer")) return (0);
501 r
->ops
->misc(r
, RAND_TIMER
);
505 static PyObject
*truerand_pynew(PyTypeObject
*ty
,
506 PyObject
*arg
, PyObject
*kw
)
508 static const char *const kwlist
[] = { 0 };
511 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, ":new", KWLIST
)) goto end
;
513 r
->ops
->misc(r
, RAND_NOISESRC
, &noise_source
);
514 r
->ops
->misc(r
, RAND_SEED
, 160);
515 rc
= grand_dopywrap(ty
, r
, f_freeme
);
520 static PyMethodDef truerand_pymethods
[] = {
521 #define METHNAME(name) trmeth_##name
522 METH (gate
, "R.gate()")
523 METH (stretch
, "R.stretch()")
524 METH (key
, "R.key(BYTES)")
525 METH (seed
, "R.seed(NBITS)")
526 METH (add
, "R.add(BYTES, GOODBITS")
527 METH (timer
, "R.timer()")
532 static PyObject
*trget_goodbits(PyObject
*me
, void *hunoz
)
534 grand
*r
= GRAND_R(me
);
535 return (PyInt_FromLong(r
->ops
->misc(r
, RAND_GOODBITS
)));
538 static PyGetSetDef truerand_pygetset
[] = {
539 #define GETSETNAME(op, name) tr##op##_##name
540 GET (goodbits
, "R.goodbits -> good bits of entropy remaining")
545 static PyTypeObject truerand_pytype_skel
= {
546 PyObject_HEAD_INIT(0) 0, /* Header */
547 "TrueRand", /* @tp_name@ */
548 sizeof(grand_pyobj
), /* @tp_basicsize@ */
549 0, /* @tp_itemsize@ */
551 grand_pydealloc
, /* @tp_dealloc@ */
553 0, /* @tp_getattr@ */
554 0, /* @tp_setattr@ */
555 0, /* @tp_compare@ */
557 0, /* @tp_as_number@ */
558 0, /* @tp_as_sequence@ */
559 0, /* @tp_as_mapping@ */
563 0, /* @tp_getattro@ */
564 0, /* @tp_setattro@ */
565 0, /* @tp_as_buffer@ */
566 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
570 "TrueRand(): true random number source.",
572 0, /* @tp_traverse@ */
574 0, /* @tp_richcompare@ */
575 0, /* @tp_weaklistoffset@ */
577 0, /* @tp_iternext@ */
578 truerand_pymethods
, /* @tp_methods@ */
579 0, /* @tp_members@ */
580 truerand_pygetset
, /* @tp_getset@ */
583 0, /* @tp_descr_get@ */
584 0, /* @tp_descr_set@ */
585 0, /* @tp_dictoffset@ */
587 PyType_GenericAlloc
, /* @tp_alloc@ */
588 truerand_pynew
, /* @tp_new@ */
593 /*----- Generators from symmetric encryption algorithms -------------------*/
595 static PyTypeObject
*gccrand_pytype
, *gcrand_pytype
, *gclatinrand_pytype
;
597 typedef grand
*gcrand_func(const void *, size_t sz
);
598 typedef grand
*gcirand_func(const void *, size_t sz
, uint32
);
599 typedef grand
*gcnrand_func(const void *, size_t sz
, const void *);
600 typedef grand
*gcshakerand_func(const void *, size_t,
601 const void *, size_t,
602 const void *, size_t);
603 typedef grand
*gcshafuncrand_func(const void *, size_t,
604 const void *, size_t);
605 typedef grand
*gckmacrand_func(const void *, size_t, const void *, size_t);
606 typedef struct gccrand_info
{
614 #define RNGF_MASK 255u
624 typedef struct gccrand_pyobj
{
626 const gccrand_info
*info
;
628 #define GCCRAND_INFO(o) (((gccrand_pyobj *)(o))->info)
630 #define GCCRAND_DEF(name, ksz, func, f, nsz) \
631 static const gccrand_info func##_info = \
632 { name, ksz, f, nsz, (gcrand_func *)func };
635 static const gccrand_info
*const gcrandtab
[] = {
636 #define GCCRAND_ENTRY(name, ksz, func, f, nsz) &func##_info,
641 static PyObject
*gcrand_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
643 const gccrand_info
*info
= GCCRAND_INFO(ty
);
644 static const char *const kwlist
[] = { "key", 0 };
648 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", KWLIST
, &k
, &n
))
650 if (keysz(n
, info
->keysz
) != n
) VALERR("bad key length");
651 return (grand_dopywrap(ty
, info
->func(k
, n
), f_freeme
));
656 static PyObject
*gcirand_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
658 const gccrand_info
*info
= GCCRAND_INFO(ty
);
660 static const char *const kwlist
[] = { "key", "i", 0 };
664 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#O&:new", KWLIST
,
665 &k
, &n
, convu32
, &i
))
667 if (keysz(n
, info
->keysz
) != n
) VALERR("bad key length");
668 return (grand_dopywrap(ty
,
669 ((gcirand_func
*)info
->func
)(k
, n
, i
),
675 static PyObject
*gcnrand_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
677 const gccrand_info
*info
= GCCRAND_INFO(ty
);
678 static const char *const kwlist
[] = { "key", "nonce", 0 };
682 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#s#:new", KWLIST
,
685 if (keysz(ksz
, info
->keysz
) != ksz
) VALERR("bad key length");
686 if (nsz
!= info
->noncesz
) VALERR("bad nonce length");
687 return (grand_dopywrap(ty
,
688 ((gcnrand_func
*)info
->func
)(k
, ksz
, n
),
694 static PyObject
*gcshakyrand_pynew(PyTypeObject
*ty
,
695 PyObject
*arg
, PyObject
*kw
)
697 const gccrand_info
*info
= GCCRAND_INFO(ty
);
699 *const kwlist_shake
[] = { "key", "func", "perso", 0 },
700 *const kwlist_func
[] = { "key", "perso", 0 };
701 char *k
, *f
= 0, *p
= 0;
702 Py_ssize_t ksz
, fsz
= 0, psz
= 0;
704 if ((info
->f
&RNGF_MASK
) == RNG_SHAKE
705 ?
!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#|s#s#:new",
706 (/*unconst*/ char **)kwlist_shake
,
707 &k
, &ksz
, &f
, &fsz
, &p
, &psz
)
708 : !PyArg_ParseTupleAndKeywords(arg
, kw
, "s#|s#:new",
709 (/*unconst*/ char **)kwlist_func
,
712 if (keysz(ksz
, info
->keysz
) != ksz
) VALERR("bad key length");
713 return (grand_dopywrap(ty
,
714 (info
->f
&RNGF_MASK
) == RNG_SHAKE
715 ?
((gcshakerand_func
*)info
->func
)(f
, fsz
,
718 : ((gcshafuncrand_func
*)info
->func
)(p
, psz
,
725 static PyObject
*gccrand_pywrap(const gccrand_info
*info
)
727 gccrand_pyobj
*g
= newtype(gccrand_pytype
, 0, info
->name
);
729 g
->ty
.ht_type
.tp_basicsize
= sizeof(grand_pyobj
);
730 switch (info
->f
&RNGF_MASK
) {
731 case RNG_LATIN
: g
->ty
.ht_type
.tp_base
= gclatinrand_pytype
; break;
732 default: g
->ty
.ht_type
.tp_base
= gcrand_pytype
; break;
734 Py_INCREF(g
->ty
.ht_type
.tp_base
);
735 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
736 Py_TPFLAGS_BASETYPE
|
737 Py_TPFLAGS_HEAPTYPE
);
738 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
739 g
->ty
.ht_type
.tp_free
= 0;
740 switch (info
->f
&RNGF_MASK
) {
741 case RNG_LATIN
: g
->ty
.ht_type
.tp_new
= gcnrand_pynew
; break;
742 case RNG_SEAL
: g
->ty
.ht_type
.tp_new
= gcirand_pynew
; break;
743 case RNG_SHAKE
: case RNG_KMAC
:
744 g
->ty
.ht_type
.tp_new
= gcshakyrand_pynew
; break;
745 default: g
->ty
.ht_type
.tp_new
= gcrand_pynew
; break;
747 typeready(&g
->ty
.ht_type
);
748 return ((PyObject
*)g
);
751 static PyObject
*gccrget_name(PyObject
*me
, void *hunoz
)
752 { return (PyString_FromString(GCCRAND_INFO(me
)->name
)); }
753 static PyObject
*gccrget_keysz(PyObject
*me
, void *hunoz
)
754 { return (keysz_pywrap(GCCRAND_INFO(me
)->keysz
)); }
756 static PyObject
*gclrmeth_tell(PyObject
*me
, PyObject
*arg
)
758 grand
*r
= GRAND_R(me
);
762 if (!PyArg_ParseTuple(arg
, ":tell")) return (0);
763 r
->ops
->misc(r
, SALSA20_TELLU64
, &off
);
768 static PyObject
*gclrmeth_seek(PyObject
*me
, PyObject
*arg
)
770 grand
*r
= GRAND_R(me
);
773 if (!PyArg_ParseTuple(arg
, "O&:seek", convk64
, &off
)) return (0);
774 r
->ops
->misc(r
, SALSA20_SEEKU64
, off
);
778 static PyGetSetDef gccrand_pygetset
[] = {
779 #define GETSETNAME(op, name) gccr##op##_##name
780 GET (keysz
, "CR.keysz -> acceptable key sizes")
781 GET (name
, "CR.name -> name of this kind of generator")
786 static PyMethodDef gclatinrand_pymethods
[] = {
787 #define METHNAME(name) gclrmeth_##name
788 METH (tell
, "R.tell() -> OFF")
789 METH (seek
, "R.seek(OFF)")
794 static PyTypeObject gccrand_pytype_skel
= {
795 PyObject_HEAD_INIT(0) 0, /* Header */
796 "GCCRand", /* @tp_name@ */
797 sizeof(gccrand_pyobj
), /* @tp_basicsize@ */
798 0, /* @tp_itemsize@ */
800 0, /* @tp_dealloc@ */
802 0, /* @tp_getattr@ */
803 0, /* @tp_setattr@ */
804 0, /* @tp_compare@ */
806 0, /* @tp_as_number@ */
807 0, /* @tp_as_sequence@ */
808 0, /* @tp_as_mapping@ */
812 0, /* @tp_getattro@ */
813 0, /* @tp_setattro@ */
814 0, /* @tp_as_buffer@ */
815 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
819 "Metaclass for symmetric crypto-based generators.",
821 0, /* @tp_traverse@ */
823 0, /* @tp_richcompare@ */
824 0, /* @tp_weaklistoffset@ */
826 0, /* @tp_iternext@ */
827 0, /* @tp_methods@ */
828 0, /* @tp_members@ */
829 gccrand_pygetset
, /* @tp_getset@ */
832 0, /* @tp_descr_get@ */
833 0, /* @tp_descr_set@ */
834 0, /* @tp_dictoffset@ */
836 PyType_GenericAlloc
, /* @tp_alloc@ */
837 abstract_pynew
, /* @tp_new@ */
842 static PyTypeObject gcrand_pytype_skel
= {
843 PyObject_HEAD_INIT(0) 0, /* Header */
844 "GCRand", /* @tp_name@ */
845 sizeof(grand_pyobj
), /* @tp_basicsize@ */
846 0, /* @tp_itemsize@ */
848 grand_pydealloc
, /* @tp_dealloc@ */
850 0, /* @tp_getattr@ */
851 0, /* @tp_setattr@ */
852 0, /* @tp_compare@ */
854 0, /* @tp_as_number@ */
855 0, /* @tp_as_sequence@ */
856 0, /* @tp_as_mapping@ */
860 0, /* @tp_getattro@ */
861 0, /* @tp_setattro@ */
862 0, /* @tp_as_buffer@ */
863 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
867 "Abstract base class for symmetric crypto-based generators.",
869 0, /* @tp_traverse@ */
871 0, /* @tp_richcompare@ */
872 0, /* @tp_weaklistoffset@ */
874 0, /* @tp_iternext@ */
875 0, /* @tp_methods@ */
876 0, /* @tp_members@ */
880 0, /* @tp_descr_get@ */
881 0, /* @tp_descr_set@ */
882 0, /* @tp_dictoffset@ */
884 PyType_GenericAlloc
, /* @tp_alloc@ */
885 abstract_pynew
, /* @tp_new@ */
890 static PyTypeObject gclatinrand_pytype_skel
= {
891 PyObject_HEAD_INIT(0) 0, /* Header */
892 "GCLatinRand", /* @tp_name@ */
893 sizeof(grand_pyobj
), /* @tp_basicsize@ */
894 0, /* @tp_itemsize@ */
896 grand_pydealloc
, /* @tp_dealloc@ */
898 0, /* @tp_getattr@ */
899 0, /* @tp_setattr@ */
900 0, /* @tp_compare@ */
902 0, /* @tp_as_number@ */
903 0, /* @tp_as_sequence@ */
904 0, /* @tp_as_mapping@ */
908 0, /* @tp_getattro@ */
909 0, /* @tp_setattro@ */
910 0, /* @tp_as_buffer@ */
911 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
915 "Abstract base class for symmetric crypto-based generators.",
917 0, /* @tp_traverse@ */
919 0, /* @tp_richcompare@ */
920 0, /* @tp_weaklistoffset@ */
922 0, /* @tp_iternext@ */
923 gclatinrand_pymethods
, /* @tp_methods@ */
924 0, /* @tp_members@ */
928 0, /* @tp_descr_get@ */
929 0, /* @tp_descr_set@ */
930 0, /* @tp_dictoffset@ */
932 PyType_GenericAlloc
, /* @tp_alloc@ */
933 abstract_pynew
, /* @tp_new@ */
938 /*----- SSL and TLS generators --------------------------------------------*/
940 static PyObject
*sslprf_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
944 const gchash
*hco
= &md5
, *hci
= &sha
;
946 static const char *const kwlist
[] = { "key", "seed", "ohash", "ihash", 0 };
948 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#s#|O&O&:new", KWLIST
,
950 convgchash
, &hco
, convgchash
, &hci
))
952 rc
= grand_dopywrap(ty
, sslprf_rand(hco
, hci
, k
, ksz
, s
, ssz
), f_freeme
);
957 static PyObject
*tlsdx_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
961 const gcmac
*mc
= &sha_hmac
;
963 static const char *const kwlist
[] = { "key", "seed", "mac", 0 };
965 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#s#|O&:new", KWLIST
,
969 rc
= grand_dopywrap(ty
, tlsdx_rand(mc
, k
, ksz
, s
, ssz
), f_freeme
);
974 static PyObject
*tlsprf_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
978 const gcmac
*mcl
= &md5_hmac
, *mcr
= &sha_hmac
;
980 static const char *const kwlist
[] = { "key", "seed", "lmac", "rmac", 0 };
982 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#s#|O&O&:new", KWLIST
,
984 convgcmac
, &mcl
, convgcmac
, &mcr
))
986 rc
= grand_dopywrap(ty
, tlsprf_rand(mcl
, mcr
, k
, ksz
, s
, ssz
), f_freeme
);
991 static PyTypeObject sslprf_pytype_skel
= {
992 PyObject_HEAD_INIT(0) 0, /* Header */
993 "SSLRand", /* @tp_name@ */
994 sizeof(grand_pyobj
), /* @tp_basicsize@ */
995 0, /* @tp_itemsize@ */
997 grand_pydealloc
, /* @tp_dealloc@ */
999 0, /* @tp_getattr@ */
1000 0, /* @tp_setattr@ */
1001 0, /* @tp_compare@ */
1003 0, /* @tp_as_number@ */
1004 0, /* @tp_as_sequence@ */
1005 0, /* @tp_as_mapping@ */
1009 0, /* @tp_getattro@ */
1010 0, /* @tp_setattro@ */
1011 0, /* @tp_as_buffer@ */
1012 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1013 Py_TPFLAGS_BASETYPE
,
1016 "SSLRand(KEY, SEED, [ohash = md5], [ihash = sha]):\n\
1017 RNG for SSL master secret.",
1019 0, /* @tp_traverse@ */
1021 0, /* @tp_richcompare@ */
1022 0, /* @tp_weaklistoffset@ */
1024 0, /* @tp_iternext@ */
1025 0, /* @tp_methods@ */
1026 0, /* @tp_members@ */
1027 0, /* @tp_getset@ */
1030 0, /* @tp_descr_get@ */
1031 0, /* @tp_descr_set@ */
1032 0, /* @tp_dictoffset@ */
1034 PyType_GenericAlloc
, /* @tp_alloc@ */
1035 sslprf_pynew
, /* @tp_new@ */
1040 static PyTypeObject tlsdx_pytype_skel
= {
1041 PyObject_HEAD_INIT(0) 0, /* Header */
1042 "TLSDataExpansion", /* @tp_name@ */
1043 sizeof(grand_pyobj
), /* @tp_basicsize@ */
1044 0, /* @tp_itemsize@ */
1046 grand_pydealloc
, /* @tp_dealloc@ */
1048 0, /* @tp_getattr@ */
1049 0, /* @tp_setattr@ */
1050 0, /* @tp_compare@ */
1052 0, /* @tp_as_number@ */
1053 0, /* @tp_as_sequence@ */
1054 0, /* @tp_as_mapping@ */
1058 0, /* @tp_getattro@ */
1059 0, /* @tp_setattro@ */
1060 0, /* @tp_as_buffer@ */
1061 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1062 Py_TPFLAGS_BASETYPE
,
1065 "TLSDataExpansion(KEY, SEED, [mac = sha_hmac]):\n\
1066 TLS data expansion function.",
1068 0, /* @tp_traverse@ */
1070 0, /* @tp_richcompare@ */
1071 0, /* @tp_weaklistoffset@ */
1073 0, /* @tp_iternext@ */
1074 0, /* @tp_methods@ */
1075 0, /* @tp_members@ */
1076 0, /* @tp_getset@ */
1079 0, /* @tp_descr_get@ */
1080 0, /* @tp_descr_set@ */
1081 0, /* @tp_dictoffset@ */
1083 PyType_GenericAlloc
, /* @tp_alloc@ */
1084 tlsdx_pynew
, /* @tp_new@ */
1089 static PyTypeObject tlsprf_pytype_skel
= {
1090 PyObject_HEAD_INIT(0) 0, /* Header */
1091 "TLSPRF", /* @tp_name@ */
1092 sizeof(grand_pyobj
), /* @tp_basicsize@ */
1093 0, /* @tp_itemsize@ */
1095 grand_pydealloc
, /* @tp_dealloc@ */
1097 0, /* @tp_getattr@ */
1098 0, /* @tp_setattr@ */
1099 0, /* @tp_compare@ */
1101 0, /* @tp_as_number@ */
1102 0, /* @tp_as_sequence@ */
1103 0, /* @tp_as_mapping@ */
1107 0, /* @tp_getattro@ */
1108 0, /* @tp_setattro@ */
1109 0, /* @tp_as_buffer@ */
1110 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1111 Py_TPFLAGS_BASETYPE
,
1114 "TLSPRF(KEY, SEED, [lmac = md5_hmac], [rmac = sha_hmac]):\n\
1115 TLS pseudorandom function.",
1117 0, /* @tp_traverse@ */
1119 0, /* @tp_richcompare@ */
1120 0, /* @tp_weaklistoffset@ */
1122 0, /* @tp_iternext@ */
1123 0, /* @tp_methods@ */
1124 0, /* @tp_members@ */
1125 0, /* @tp_getset@ */
1128 0, /* @tp_descr_get@ */
1129 0, /* @tp_descr_set@ */
1130 0, /* @tp_dictoffset@ */
1132 PyType_GenericAlloc
, /* @tp_alloc@ */
1133 tlsprf_pynew
, /* @tp_new@ */
1138 /*----- DSA generator -----------------------------------------------------*/
1140 static PyObject
*dsarand_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1145 static const char *const kwlist
[] = { "seed", 0 };
1147 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", KWLIST
, &p
, &sz
))
1149 rc
= grand_dopywrap(ty
, dsarand_create(p
, sz
), f_freeme
);
1154 static PyObject
*drget_seed(PyObject
*me
, void *hunoz
)
1156 grand
*r
= GRAND_R(me
);
1157 int n
= r
->ops
->misc(r
, DSARAND_SEEDSZ
);
1158 PyObject
*rc
= bytestring_pywrap(0, n
);
1159 r
->ops
->misc(r
, DSARAND_GETSEED
, PyString_AS_STRING(rc
));
1163 static PyGetSetDef dsarand_pygetset
[] = {
1164 #define GETSETNAME(op, name) dr##op##_##name
1165 GET (seed
, "R.seed -> current generator seed")
1170 static PyTypeObject dsarand_pytype_skel
= {
1171 PyObject_HEAD_INIT(0) 0, /* Header */
1172 "DSARand", /* @tp_name@ */
1173 sizeof(grand_pyobj
), /* @tp_basicsize@ */
1174 0, /* @tp_itemsize@ */
1176 grand_pydealloc
, /* @tp_dealloc@ */
1178 0, /* @tp_getattr@ */
1179 0, /* @tp_setattr@ */
1180 0, /* @tp_compare@ */
1182 0, /* @tp_as_number@ */
1183 0, /* @tp_as_sequence@ */
1184 0, /* @tp_as_mapping@ */
1188 0, /* @tp_getattro@ */
1189 0, /* @tp_setattro@ */
1190 0, /* @tp_as_buffer@ */
1191 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1192 Py_TPFLAGS_BASETYPE
,
1195 "DSARand(SEED): pseudorandom number generator for DSA parameters.",
1197 0, /* @tp_traverse@ */
1199 0, /* @tp_richcompare@ */
1200 0, /* @tp_weaklistoffset@ */
1202 0, /* @tp_iternext@ */
1203 0, /* @tp_methods@ */
1204 0, /* @tp_members@ */
1205 dsarand_pygetset
, /* @tp_getset@ */
1208 0, /* @tp_descr_get@ */
1209 0, /* @tp_descr_set@ */
1210 0, /* @tp_dictoffset@ */
1212 PyType_GenericAlloc
, /* @tp_alloc@ */
1213 dsarand_pynew
, /* @tp_new@ */
1218 /*----- Blum-Blum-Shub generator ------------------------------------------*/
1220 static PyObject
*bbs_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1222 mp
*n
= 0, *x
= MP_TWO
;
1224 static const char *const kwlist
[] = { "n", "x", 0 };
1226 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&|O&:new", KWLIST
,
1227 convmp
, &n
, convmp
, &x
))
1229 rc
= grand_dopywrap(ty
, bbs_rand(n
, x
), f_freeme
);
1236 static PyObject
*bbsmeth_step(PyObject
*me
, PyObject
*arg
)
1238 grand
*r
= GRAND_R(me
); if (!PyArg_ParseTuple(arg
, ":step")) return (0);
1239 r
->ops
->misc(r
, BBS_STEP
); RETURN_ME
;
1242 static PyObject
*bbsmeth_bits(PyObject
*me
, PyObject
*arg
)
1244 grand
*r
= GRAND_R(me
); unsigned n
; uint32 w
;
1245 if (!PyArg_ParseTuple(arg
, "O&:bits", convuint
, &n
)) goto end
;
1246 if (n
> 32) VALERR("can't get more than 32 bits");
1247 r
->ops
->misc(r
, BBS_BITS
, n
, &w
); return (getulong(w
));
1252 static PyObject
*bbsmeth_wrap(PyObject
*me
, PyObject
*arg
)
1254 grand
*r
= GRAND_R(me
); if (!PyArg_ParseTuple(arg
, ":wrap")) return (0);
1255 r
->ops
->misc(r
, BBS_WRAP
); RETURN_ME
;
1258 static PyObject
*bbsget_n(PyObject
*me
, void *hunoz
)
1260 mp
*x
= MP_NEW
; grand
*r
= GRAND_R(me
);
1261 r
->ops
->misc(r
, BBS_MOD
, &x
); return (mp_pywrap(x
));
1264 static PyObject
*bbsget_x(PyObject
*me
, void *hunoz
)
1266 mp
*x
= MP_NEW
; grand
*r
= GRAND_R(me
);
1267 r
->ops
->misc(r
, BBS_STATE
, &x
); return (mp_pywrap(x
));
1270 static int bbsset_x(PyObject
*me
, PyObject
*val
, void *hunoz
)
1272 mp
*x
= 0; grand
*r
= GRAND_R(me
); int rc
= -1; if (!val
) NIERR("__del__");
1273 if ((x
= getmp(val
)) == 0) goto end
;
1274 r
->ops
->misc(r
, BBS_SET
, x
); rc
= 0;
1275 end
: mp_drop(x
); return (rc
);
1278 static PyObject
*bbsget_stepsz(PyObject
*me
, void *hunoz
)
1280 grand
*r
= GRAND_R(me
);
1281 return (PyInt_FromLong(r
->ops
->misc(r
, BBS_STEPSZ
)));
1284 static PyMethodDef bbs_pymethods
[] = {
1285 #define METHNAME(name) bbsmeth_##name
1286 METH (step
, "R.step(): steps the generator (not useful)")
1287 METH (bits
, "R.bits(N) -> W: returns N bits (<= 32) from the generator")
1288 METH (wrap
, "R.wrap(): flushes unused bits in internal buffer")
1293 static PyGetSetDef bbs_pygetset
[] = {
1294 #define GETSETNAME(op, name) bbs##op##_##name
1295 GET (n
, "R.n -> Blum modulus")
1296 GETSET(x
, "R.x -> current seed value")
1297 GET (stepsz
, "R.stepsz -> number of bits generated per step")
1302 static PyTypeObject bbs_pytype_skel
= {
1303 PyObject_HEAD_INIT(0) 0, /* Header */
1304 "BlumBlumShub", /* @tp_name@ */
1305 sizeof(grand_pyobj
), /* @tp_basicsize@ */
1306 0, /* @tp_itemsize@ */
1308 grand_pydealloc
, /* @tp_dealloc@ */
1310 0, /* @tp_getattr@ */
1311 0, /* @tp_setattr@ */
1312 0, /* @tp_compare@ */
1314 0, /* @tp_as_number@ */
1315 0, /* @tp_as_sequence@ */
1316 0, /* @tp_as_mapping@ */
1320 0, /* @tp_getattro@ */
1321 0, /* @tp_setattro@ */
1322 0, /* @tp_as_buffer@ */
1323 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1324 Py_TPFLAGS_BASETYPE
,
1327 "BlumBlumShub(N, [x = 2]): Blum-Blum-Shub pseudorandom number generator.",
1329 0, /* @tp_traverse@ */
1331 0, /* @tp_richcompare@ */
1332 0, /* @tp_weaklistoffset@ */
1334 0, /* @tp_iternext@ */
1335 bbs_pymethods
, /* @tp_methods@ */
1336 0, /* @tp_members@ */
1337 bbs_pygetset
, /* @tp_getset@ */
1340 0, /* @tp_descr_get@ */
1341 0, /* @tp_descr_set@ */
1342 0, /* @tp_dictoffset@ */
1344 PyType_GenericAlloc
, /* @tp_alloc@ */
1345 bbs_pynew
, /* @tp_new@ */
1350 typedef struct bbspriv_pyobj
{
1355 #define BBSPRIV_BP(o) (&((bbspriv_pyobj *)(o))->bp)
1357 static PyObject
*bbspriv_pynew(PyTypeObject
*ty
,
1358 PyObject
*arg
, PyObject
*kw
)
1360 mp
*p
= 0, *q
= 0, *n
= 0, *x
= MP_TWO
;
1361 bbspriv_pyobj
*rc
= 0;
1362 static const char *const kwlist
[] = { "n", "p", "q", "seed", 0 };
1364 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&O&O&O&:new", KWLIST
,
1365 convmp
, &n
, convmp
, &p
, convmp
, &q
,
1368 if (!n
+ !p
+ !q
> 1) VALERR("must specify at least two of n, p, q");
1369 if (!n
) n
= mp_mul(MP_NEW
, p
, q
);
1370 else if (!p
) mp_div(&p
, 0, n
, q
);
1371 else if (!q
) mp_div(&q
, 0, n
, p
);
1372 rc
= (bbspriv_pyobj
*)ty
->tp_alloc(ty
, 0);
1373 rc
->gr
.r
= bbs_rand(n
, x
);
1374 rc
->gr
.f
= f_freeme
;
1375 rc
->bp
.p
= MP_COPY(p
);
1376 rc
->bp
.q
= MP_COPY(q
);
1377 rc
->bp
.n
= MP_COPY(n
);
1379 mp_drop(p
); mp_drop(q
); mp_drop(n
); mp_drop(x
);
1380 return ((PyObject
*)rc
);
1383 static PyObject
*meth__BBSPriv_generate(PyObject
*me
,
1384 PyObject
*arg
, PyObject
*kw
)
1386 bbs_priv bp
= { 0 };
1388 struct excinfo exc
= EXCINFO_INIT
;
1389 pypgev evt
= { { 0 } };
1390 unsigned nbits
, n
= 0;
1391 grand
*r
= &rand_global
;
1392 static const char *const kwlist
[] =
1393 { "class", "nbits", "event", "rng", "nsteps", "seed", 0 };
1394 bbspriv_pyobj
*rc
= 0;
1397 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "OO&|O&O&O&O&:generate", KWLIST
,
1398 &me
, convuint
, &nbits
, convpgev
, &evt
,
1399 convgrand
, &r
, convuint
, &n
, convmp
, &x
))
1401 if (bbs_gen(&bp
, nbits
, r
, n
, evt
.ev
.proc
, evt
.ev
.ctx
))
1403 rc
= PyObject_New(bbspriv_pyobj
, bbspriv_pytype
);
1404 rc
->gr
.r
= bbs_rand(bp
.n
, x
);
1405 rc
->gr
.f
= f_freeme
;
1406 rc
->bp
.p
= MP_COPY(bp
.p
);
1407 rc
->bp
.q
= MP_COPY(bp
.q
);
1408 rc
->bp
.n
= MP_COPY(bp
.n
);
1410 mp_drop(bp
.p
); mp_drop(bp
.q
); mp_drop(bp
.n
); mp_drop(x
);
1411 return ((PyObject
*)rc
);
1414 static void bbspriv_pydealloc(PyObject
*me
)
1416 bbs_priv
*bp
= BBSPRIV_BP(me
);
1420 grand_pydealloc(me
);
1423 static PyObject
*bpmeth_ff(PyObject
*me
, PyObject
*arg
)
1425 mp
*n
= 0; grand
*r
= GRAND_R(me
); bbs_priv
*bp
= BBSPRIV_BP(me
);
1426 if (!PyArg_ParseTuple(arg
, "O&:ff", convmp
, &n
)) return (0);
1427 r
->ops
->misc(r
, BBS_FF
, bp
, n
); RETURN_ME
;
1430 static PyObject
*bpmeth_rew(PyObject
*me
, PyObject
*arg
)
1432 mp
*n
= 0; grand
*r
= GRAND_R(me
); bbs_priv
*bp
= BBSPRIV_BP(me
);
1433 if (!PyArg_ParseTuple(arg
, "O&:rew", convmp
, &n
)) return (0);
1434 r
->ops
->misc(r
, BBS_REW
, bp
, n
); RETURN_ME
;
1437 static PyObject
*bpget_n(PyObject
*me
, void *hunoz
)
1438 { return (mp_pywrap(MP_COPY(BBSPRIV_BP(me
)->n
))); }
1440 static PyObject
*bpget_p(PyObject
*me
, void *hunoz
)
1441 { return (mp_pywrap(MP_COPY(BBSPRIV_BP(me
)->p
))); }
1443 static PyObject
*bpget_q(PyObject
*me
, void *hunoz
)
1444 { return (mp_pywrap(MP_COPY(BBSPRIV_BP(me
)->q
))); }
1446 static PyMethodDef bbspriv_pymethods
[] = {
1447 #define METHNAME(name) bpmeth_##name
1448 METH (ff
, "R.ff(N): fast-forward N places")
1449 METH (rew
, "R.rew(N): rewind N places")
1454 static PyGetSetDef bbspriv_pygetset
[] = {
1455 #define GETSETNAME(op, name) bp##op##_##name
1456 GET (n
, "R.n -> Blum modulus")
1457 GET (p
, "R.p -> one of the factors of the modulus")
1458 GET (q
, "R.q -> one of the factors of the modulus")
1463 static PyTypeObject bbspriv_pytype_skel
= {
1464 PyObject_HEAD_INIT(0) 0, /* Header */
1465 "BBSPriv", /* @tp_name@ */
1466 sizeof(bbspriv_pyobj
), /* @tp_basicsize@ */
1467 0, /* @tp_itemsize@ */
1469 bbspriv_pydealloc
, /* @tp_dealloc@ */
1471 0, /* @tp_getattr@ */
1472 0, /* @tp_setattr@ */
1473 0, /* @tp_compare@ */
1475 0, /* @tp_as_number@ */
1476 0, /* @tp_as_sequence@ */
1477 0, /* @tp_as_mapping@ */
1481 0, /* @tp_getattro@ */
1482 0, /* @tp_setattro@ */
1483 0, /* @tp_as_buffer@ */
1484 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1485 Py_TPFLAGS_BASETYPE
,
1488 "BBSPriv(..., [seed = 2]): Blum-Blum-Shub, with private key.\n\
1489 Keywords: n, p, q; must provide at least two",
1491 0, /* @tp_traverse@ */
1493 0, /* @tp_richcompare@ */
1494 0, /* @tp_weaklistoffset@ */
1496 0, /* @tp_iternext@ */
1497 bbspriv_pymethods
, /* @tp_methods@ */
1498 0, /* @tp_members@ */
1499 bbspriv_pygetset
, /* @tp_getset@ */
1502 0, /* @tp_descr_get@ */
1503 0, /* @tp_descr_set@ */
1504 0, /* @tp_dictoffset@ */
1506 PyType_GenericAlloc
, /* @tp_alloc@ */
1507 bbspriv_pynew
, /* @tp_new@ */
1512 /*----- Global stuff ------------------------------------------------------*/
1514 static PyMethodDef methods
[] = {
1515 #define METHNAME(name) meth_##name
1516 KWMETH(_BBSPriv_generate
, "\
1517 generate(NBITS, [event = pgen_nullev], [rng = rand],\n\
1518 [nsteps = 0], [seed = 2]) -> R")
1523 void rand_pyinit(void)
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
);
1543 #define gccrand gccrand_info
1544 GEN(gccrands
, crand
)
1546 void rand_pyinsert(PyObject
*mod
)
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
);
1567 /*----- That's all, folks -------------------------------------------------*/