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"
30 #include "algorithms.h"
32 /*----- Main code ---------------------------------------------------------*/
34 PyTypeObject
*grand_pytype
, *truerand_pytype
;
35 PyTypeObject
*lcrand_pytype
, *fibrand_pytype
;
36 PyTypeObject
*dsarand_pytype
, *bbs_pytype
, *bbspriv_pytype
;
37 PyTypeObject
*sslprf_pytype
, *tlsdx_pytype
, *tlsprf_pytype
;
40 static PyObject
*gccrands_dict
;
42 static PyObject
*grand_dopywrap(PyTypeObject
*ty
, grand
*r
, unsigned f
)
46 g
= (grand_pyobj
*)ty
->tp_alloc(ty
, 0);
49 return ((PyObject
*)g
);
52 PyObject
*grand_pywrap(grand
*r
, unsigned f
)
54 PyTypeObject
*ty
= grand_pytype
;
57 if (strcmp(r
->ops
->name
, "rand") == 0) ty
= truerand_pytype
;
58 else if (strcmp(r
->ops
->name
, "lcrand") == 0) ty
= lcrand_pytype
;
59 else if (strcmp(r
->ops
->name
, "fibrand") == 0) ty
= fibrand_pytype
;
60 else if (strcmp(r
->ops
->name
, "dsarand") == 0) ty
= dsarand_pytype
;
61 else if (strcmp(r
->ops
->name
, "bbs") == 0) ty
= bbs_pytype
;
62 else if (strcmp(r
->ops
->name
, "sslprf") == 0) ty
= sslprf_pytype
;
63 else if (strcmp(r
->ops
->name
, "tlsdx") == 0) ty
= tlsdx_pytype
;
64 else if (strcmp(r
->ops
->name
, "tlsprf") == 0) ty
= tlsprf_pytype
;
65 else if ((ob
= PyDict_GetItemString(gccrands_dict
, r
->ops
->name
)) != 0)
66 ty
= (PyTypeObject
*)ob
;
67 return (grand_dopywrap(ty
, r
, f
));
70 CONVFUNC(grand
, grand
*, GRAND_R
)
72 static int grand_check(PyObject
*me
)
74 if (!GRAND_R(me
)) VALERR("random generator object is no longer valid");
80 static PyObject
*grmeth_byte(PyObject
*me
, PyObject
*arg
)
82 if (!PyArg_ParseTuple(arg
, ":byte")) return (0);
83 if (grand_check(me
)) return (0);
84 return (PyInt_FromLong(grand_byte(GRAND_R(me
))));
87 static PyObject
*grmeth_word(PyObject
*me
, PyObject
*arg
)
89 if (!PyArg_ParseTuple(arg
, ":word")) return (0);
90 if (grand_check(me
)) return (0);
91 return (getulong(grand_word(GRAND_R(me
))));
94 static PyObject
*grmeth_range(PyObject
*me
, PyObject
*arg
)
100 if (!PyArg_ParseTuple(arg
, "O:range", &m
)) return (0);
101 if (grand_check(me
)) return (0);
102 if (PyInt_Check(m
)) {
103 long mm
= PyInt_AS_LONG(m
);
106 if (mm
<= 0xffffffff)
107 return (PyInt_FromLong(grand_range(GRAND_R(me
), mm
)));
109 if ((x
= getmp(m
)) == 0)
113 y
= mprand_range(MP_NEW
, x
, GRAND_R(me
), 0);
115 return (mp_pywrap(y
));
117 VALERR("range must be strictly positive");
123 static PyObject
*grmeth_mp(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
127 static const char *const kwlist
[] = { "bits", "or", 0 };
129 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&|O&:mp", KWLIST
,
130 convszt
, &l
, convmpw
, &o
))
132 if (grand_check(me
)) return (0);
133 if (l
< MPW_BITS
&& (o
>> l
)) VALERR("or mask too large");
134 return (mp_pywrap(mprand(MP_NEW
, l
, GRAND_R(me
), o
)));
139 static PyObject
*grmeth_block(PyObject
*me
, PyObject
*arg
)
144 if (!PyArg_ParseTuple(arg
, "O&:block", convulong
, &n
)) goto end
;
145 if (grand_check(me
)) return (0);
146 rc
= bytestring_pywrap(0, n
);
147 grand_fill(GRAND_R(me
), PyString_AS_STRING(rc
), n
);
152 static int checkop(grand
*r
, unsigned op
, const char *what
)
154 if (r
->ops
->misc(r
, GRAND_CHECK
, op
)) return (0);
155 PyErr_Format(PyExc_TypeError
, "operation %s not supported", what
);
159 static PyObject
*grmeth_seedint(PyObject
*me
, PyObject
*arg
)
162 grand
*r
= GRAND_R(me
);
163 if (!PyArg_ParseTuple(arg
, "i:seedint", &i
) ||
164 grand_check(me
) || checkop(r
, GRAND_SEEDINT
, "seedint"))
166 r
->ops
->misc(r
, GRAND_SEEDINT
, i
);
172 static PyObject
*grmeth_seedword(PyObject
*me
, PyObject
*arg
)
175 grand
*r
= GRAND_R(me
);
176 if (!PyArg_ParseTuple(arg
, "O&:seedword", convu32
, &u
) ||
177 grand_check(me
) || checkop(r
, GRAND_SEEDUINT32
, "seedword"))
179 r
->ops
->misc(r
, GRAND_SEEDUINT32
, u
);
185 static PyObject
*grmeth_seedblock(PyObject
*me
, PyObject
*arg
)
189 grand
*r
= GRAND_R(me
);
190 if (!PyArg_ParseTuple(arg
, "s#:seedblock", &p
, &n
) ||
191 grand_check(me
) || checkop(r
, GRAND_SEEDBLOCK
, "seedblock"))
193 r
->ops
->misc(r
, GRAND_SEEDBLOCK
, p
, (size_t)n
);
199 static PyObject
*grmeth_seedmp(PyObject
*me
, PyObject
*arg
)
203 grand
*r
= GRAND_R(me
);
204 if (!PyArg_ParseTuple(arg
, "O:seedmp", &x
) ||
205 grand_check(me
) || checkop(r
, GRAND_SEEDMP
, "seedmp") ||
206 (xx
= getmp(x
)) == 0)
208 r
->ops
->misc(r
, GRAND_SEEDMP
, xx
);
215 static PyObject
*grmeth_seedrand(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
217 static const char *const kwlist
[] = { "rng", 0 };
218 grand
*r
= GRAND_R(me
);
219 grand
*rr
= &rand_global
;
220 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:seedrand", KWLIST
,
222 grand_check(me
) || checkop(r
, GRAND_SEEDRAND
, "seedrand"))
224 r
->ops
->misc(r
, GRAND_SEEDRAND
, rr
);
230 static PyObject
*grmeth_mask(PyObject
*me
, PyObject
*arg
)
232 grand
*r
= GRAND_R(me
);
237 if (!PyArg_ParseTuple(arg
, "s#:mask", &p
, &sz
)) return (0);
238 if (grand_check(me
)) return (0);
239 rc
= bytestring_pywrap(0, sz
);
240 q
= PyString_AS_STRING(rc
);
242 while (sz
--) *q
++ ^= *p
++;
246 static void grand_pydealloc(PyObject
*me
)
248 grand_pyobj
*g
= (grand_pyobj
*)me
;
249 if ((g
->f
& f_freeme
) && g
->r
) GR_DESTROY(g
->r
);
253 static PyObject
*grget_name(PyObject
*me
, void *hunoz
)
254 { return (grand_check(me
) ?
0 : PyString_FromString(GRAND_R(me
)->ops
->name
)); }
256 static PyObject
*grget_cryptop(PyObject
*me
, void *hunoz
)
257 { return (grand_check(me
) ?
0 : getbool(GRAND_R(me
)->ops
->f
& GRAND_CRYPTO
)); }
259 static PyGetSetDef grand_pygetset
[] = {
260 #define GETSETNAME(op, name) gr##op##_##name
261 GET (name
, "R.name -> name of this kind of generator")
262 GET (cryptop
, "R.cryptop -> flag: cryptographically strong?")
267 static PyMethodDef grand_pymethods
[] = {
268 #define METHNAME(name) grmeth_##name
269 METH (byte
, "R.byte() -> BYTE")
270 METH (word
, "R.word() -> WORD")
271 METH (block
, "R.block(N) -> STRING")
272 KWMETH(mp
, "R.mp(bits, [or = 0]) -> MP")
273 METH (range
, "R.range(MAX) -> INT")
274 METH (mask
, "R.mask(STR) -> STR")
275 METH (seedint
, "R.seedint(I)")
276 METH (seedword
, "R.seedword(I)")
277 METH (seedblock
, "R.seedblock(BYTES)")
278 METH (seedmp
, "R.seedmp(X)")
279 KWMETH(seedrand
, "R.seedrand(RR)")
284 static PyTypeObject grand_pytype_skel
= {
285 PyObject_HEAD_INIT(0) 0, /* Header */
286 "GRand", /* @tp_name@ */
287 sizeof(grand_pyobj
), /* @tp_basicsize@ */
288 0, /* @tp_itemsize@ */
290 grand_pydealloc
, /* @tp_dealloc@ */
292 0, /* @tp_getattr@ */
293 0, /* @tp_setattr@ */
294 0, /* @tp_compare@ */
296 0, /* @tp_as_number@ */
297 0, /* @tp_as_sequence@ */
298 0, /* @tp_as_mapping@ */
302 0, /* @tp_getattro@ */
303 0, /* @tp_setattro@ */
304 0, /* @tp_as_buffer@ */
305 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
309 "Generic random number source.",
311 0, /* @tp_traverse@ */
313 0, /* @tp_richcompare@ */
314 0, /* @tp_weaklistoffset@ */
316 0, /* @tp_iternext@ */
317 grand_pymethods
, /* @tp_methods@ */
318 0, /* @tp_members@ */
319 grand_pygetset
, /* @tp_getset@ */
322 0, /* @tp_descr_get@ */
323 0, /* @tp_descr_set@ */
324 0, /* @tp_dictoffset@ */
326 PyType_GenericAlloc
, /* @tp_alloc@ */
327 abstract_pynew
, /* @tp_new@ */
332 static PyObject
*lcrand_pynew(PyTypeObject
*me
, PyObject
*arg
, PyObject
*kw
)
335 static const char *const kwlist
[] = { "seed", 0 };
336 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:new", KWLIST
, convu32
, &n
))
338 return (grand_dopywrap(lcrand_pytype
, lcrand_create(n
), f_freeme
));
341 static PyTypeObject lcrand_pytype_skel
= {
342 PyObject_HEAD_INIT(0) 0, /* Header */
343 "LCRand", /* @tp_name@ */
344 sizeof(grand_pyobj
), /* @tp_basicsize@ */
345 0, /* @tp_itemsize@ */
347 grand_pydealloc
, /* @tp_dealloc@ */
349 0, /* @tp_getattr@ */
350 0, /* @tp_setattr@ */
351 0, /* @tp_compare@ */
353 0, /* @tp_as_number@ */
354 0, /* @tp_as_sequence@ */
355 0, /* @tp_as_mapping@ */
359 0, /* @tp_getattro@ */
360 0, /* @tp_setattro@ */
361 0, /* @tp_as_buffer@ */
362 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
366 "LCRand([seed = 0]): linear congruential generator.",
368 0, /* @tp_traverse@ */
370 0, /* @tp_richcompare@ */
371 0, /* @tp_weaklistoffset@ */
373 0, /* @tp_iternext@ */
374 0, /* @tp_methods@ */
375 0, /* @tp_members@ */
379 0, /* @tp_descr_get@ */
380 0, /* @tp_descr_set@ */
381 0, /* @tp_dictoffset@ */
383 PyType_GenericAlloc
, /* @tp_alloc@ */
384 lcrand_pynew
, /* @tp_new@ */
389 static PyObject
*fibrand_pynew(PyTypeObject
*me
, PyObject
*arg
, PyObject
*kw
)
392 static const char *const kwlist
[] = { "seed", 0 };
393 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:new", KWLIST
, convu32
, &n
))
395 return (grand_dopywrap(fibrand_pytype
, fibrand_create(n
), f_freeme
));
398 static PyTypeObject fibrand_pytype_skel
= {
399 PyObject_HEAD_INIT(0) 0, /* Header */
400 "FibRand", /* @tp_name@ */
401 sizeof(grand_pyobj
), /* @tp_basicsize@ */
402 0, /* @tp_itemsize@ */
404 grand_pydealloc
, /* @tp_dealloc@ */
406 0, /* @tp_getattr@ */
407 0, /* @tp_setattr@ */
408 0, /* @tp_compare@ */
410 0, /* @tp_as_number@ */
411 0, /* @tp_as_sequence@ */
412 0, /* @tp_as_mapping@ */
416 0, /* @tp_getattro@ */
417 0, /* @tp_setattro@ */
418 0, /* @tp_as_buffer@ */
419 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
423 "FibRand([seed = 0]): Fibonacci generator.",
425 0, /* @tp_traverse@ */
427 0, /* @tp_richcompare@ */
428 0, /* @tp_weaklistoffset@ */
430 0, /* @tp_iternext@ */
431 0, /* @tp_methods@ */
432 0, /* @tp_members@ */
436 0, /* @tp_descr_get@ */
437 0, /* @tp_descr_set@ */
438 0, /* @tp_dictoffset@ */
440 PyType_GenericAlloc
, /* @tp_alloc@ */
441 fibrand_pynew
, /* @tp_new@ */
446 /*----- True random generator ---------------------------------------------*/
448 static PyObject
*trmeth_gate(PyObject
*me
, PyObject
*arg
)
450 grand
*r
= GRAND_R(me
);
451 if (!PyArg_ParseTuple(arg
, ":gate")) return (0);
452 r
->ops
->misc(r
, RAND_GATE
);
456 static PyObject
*trmeth_stretch(PyObject
*me
, PyObject
*arg
)
458 grand
*r
= GRAND_R(me
);
459 if (!PyArg_ParseTuple(arg
, ":stretch")) return (0);
460 r
->ops
->misc(r
, RAND_STRETCH
);
464 static PyObject
*trmeth_add(PyObject
*me
, PyObject
*arg
)
466 grand
*r
= GRAND_R(me
);
467 char *p
; Py_ssize_t n
; unsigned goodbits
;
468 if (!PyArg_ParseTuple(arg
, "s#O&:add", &p
, &n
, convuint
, &goodbits
))
470 r
->ops
->misc(r
, RAND_ADD
, p
, (size_t)n
, goodbits
);
474 static PyObject
*trmeth_key(PyObject
*me
, PyObject
*arg
)
476 grand
*r
= GRAND_R(me
);
477 char *p
; Py_ssize_t n
;
478 if (!PyArg_ParseTuple(arg
, "s#:key", &p
, &n
)) return (0);
479 r
->ops
->misc(r
, RAND_KEY
, p
, (size_t)n
);
483 static PyObject
*trmeth_seed(PyObject
*me
, PyObject
*arg
)
485 grand
*r
= GRAND_R(me
);
487 if (!PyArg_ParseTuple(arg
, "O&:seed", convuint
, &u
)) return (0);
488 if (u
> RAND_IBITS
) VALERR("pointlessly large");
489 r
->ops
->misc(r
, RAND_SEED
, u
);
495 static PyObject
*trmeth_timer(PyObject
*me
, PyObject
*arg
)
497 grand
*r
= GRAND_R(me
);
498 if (!PyArg_ParseTuple(arg
, ":timer")) return (0);
499 r
->ops
->misc(r
, RAND_TIMER
);
503 static PyObject
*truerand_pynew(PyTypeObject
*ty
,
504 PyObject
*arg
, PyObject
*kw
)
506 static const char *const kwlist
[] = { 0 };
509 if (PyArg_ParseTupleAndKeywords(arg
, kw
, ":new", KWLIST
)) goto end
;
511 r
->ops
->misc(r
, RAND_NOISESRC
, &noise_source
);
512 r
->ops
->misc(r
, RAND_SEED
, 160);
513 rc
= grand_dopywrap(ty
, r
, f_freeme
);
518 static PyMethodDef truerand_pymethods
[] = {
519 #define METHNAME(name) trmeth_##name
520 METH (gate
, "R.gate()")
521 METH (stretch
, "R.stretch()")
522 METH (key
, "R.key(BYTES)")
523 METH (seed
, "R.seed(NBITS)")
524 METH (add
, "R.add(BYTES, GOODBITS")
525 METH (timer
, "R.timer()")
530 static PyObject
*trget_goodbits(PyObject
*me
, void *hunoz
)
532 grand
*r
= GRAND_R(me
);
533 return (PyInt_FromLong(r
->ops
->misc(r
, RAND_GOODBITS
)));
536 static PyGetSetDef truerand_pygetset
[] = {
537 #define GETSETNAME(op, name) tr##op##_##name
538 GET (goodbits
, "R.goodbits -> good bits of entropy remaining")
543 static PyTypeObject truerand_pytype_skel
= {
544 PyObject_HEAD_INIT(0) 0, /* Header */
545 "TrueRand", /* @tp_name@ */
546 sizeof(grand_pyobj
), /* @tp_basicsize@ */
547 0, /* @tp_itemsize@ */
549 grand_pydealloc
, /* @tp_dealloc@ */
551 0, /* @tp_getattr@ */
552 0, /* @tp_setattr@ */
553 0, /* @tp_compare@ */
555 0, /* @tp_as_number@ */
556 0, /* @tp_as_sequence@ */
557 0, /* @tp_as_mapping@ */
561 0, /* @tp_getattro@ */
562 0, /* @tp_setattro@ */
563 0, /* @tp_as_buffer@ */
564 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
568 "TrueRand(): true random number source.",
570 0, /* @tp_traverse@ */
572 0, /* @tp_richcompare@ */
573 0, /* @tp_weaklistoffset@ */
575 0, /* @tp_iternext@ */
576 truerand_pymethods
, /* @tp_methods@ */
577 0, /* @tp_members@ */
578 truerand_pygetset
, /* @tp_getset@ */
581 0, /* @tp_descr_get@ */
582 0, /* @tp_descr_set@ */
583 0, /* @tp_dictoffset@ */
585 PyType_GenericAlloc
, /* @tp_alloc@ */
586 truerand_pynew
, /* @tp_new@ */
591 /*----- Generators from symmetric encryption algorithms -------------------*/
593 static PyTypeObject
*gccrand_pytype
, *gcrand_pytype
, *gclatinrand_pytype
;
595 typedef grand
*gcrand_func(const void *, size_t sz
);
596 typedef grand
*gcirand_func(const void *, size_t sz
, uint32
);
597 typedef grand
*gcnrand_func(const void *, size_t sz
, const void *);
598 typedef grand
*gcshakerand_func(const void *, size_t,
599 const void *, size_t,
600 const void *, size_t);
601 typedef grand
*gcshafuncrand_func(const void *, size_t,
602 const void *, size_t);
603 typedef grand
*gckmacrand_func(const void *, size_t, const void *, size_t);
604 typedef struct gccrand_info
{
612 #define RNGF_MASK 255u
622 typedef struct gccrand_pyobj
{
624 const gccrand_info
*info
;
626 #define GCCRAND_INFO(o) (((gccrand_pyobj *)(o))->info)
628 #define GCCRAND_DEF(name, ksz, func, f, nsz) \
629 static const gccrand_info func##_info = \
630 { name, ksz, f, nsz, (gcrand_func *)func };
633 static const gccrand_info
*const gcrandtab
[] = {
634 #define GCCRAND_ENTRY(name, ksz, func, f, nsz) &func##_info,
639 static PyObject
*gcrand_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
641 const gccrand_info
*info
= GCCRAND_INFO(ty
);
642 static const char *const kwlist
[] = { "key", 0 };
646 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", KWLIST
, &k
, &n
))
648 if (keysz(n
, info
->keysz
) != n
) VALERR("bad key length");
649 return (grand_dopywrap(ty
, info
->func(k
, n
), f_freeme
));
654 static PyObject
*gcirand_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
656 const gccrand_info
*info
= GCCRAND_INFO(ty
);
658 static const char *const kwlist
[] = { "key", "i", 0 };
662 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#O&:new", KWLIST
,
663 &k
, &n
, convu32
, &i
))
665 if (keysz(n
, info
->keysz
) != n
) VALERR("bad key length");
666 return (grand_dopywrap(ty
,
667 ((gcirand_func
*)info
->func
)(k
, n
, i
),
673 static PyObject
*gcnrand_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
675 const gccrand_info
*info
= GCCRAND_INFO(ty
);
676 static const char *const kwlist
[] = { "key", "nonce", 0 };
680 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#s#:new", KWLIST
,
683 if (keysz(ksz
, info
->keysz
) != ksz
) VALERR("bad key length");
684 if (nsz
!= info
->noncesz
) VALERR("bad nonce length");
685 return (grand_dopywrap(ty
,
686 ((gcnrand_func
*)info
->func
)(k
, ksz
, n
),
692 static PyObject
*gcshakyrand_pynew(PyTypeObject
*ty
,
693 PyObject
*arg
, PyObject
*kw
)
695 const gccrand_info
*info
= GCCRAND_INFO(ty
);
697 *const kwlist_shake
[] = { "key", "func", "perso", 0 },
698 *const kwlist_func
[] = { "key", "perso", 0 };
699 char *k
, *f
= 0, *p
= 0;
700 Py_ssize_t ksz
, fsz
= 0, psz
= 0;
702 if ((info
->f
&RNGF_MASK
) == RNG_SHAKE
703 ?
!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#|s#s#:new",
704 (/*unconst*/ char **)kwlist_shake
,
705 &k
, &ksz
, &f
, &fsz
, &p
, &psz
)
706 : !PyArg_ParseTupleAndKeywords(arg
, kw
, "s#|s#:new",
707 (/*unconst*/ char **)kwlist_func
,
710 if (keysz(ksz
, info
->keysz
) != ksz
) VALERR("bad key length");
711 return (grand_dopywrap(ty
,
712 (info
->f
&RNGF_MASK
) == RNG_SHAKE
713 ?
((gcshakerand_func
*)info
->func
)(f
, fsz
,
716 : ((gcshafuncrand_func
*)info
->func
)(p
, psz
,
723 static PyObject
*gccrand_pywrap(const gccrand_info
*info
)
725 gccrand_pyobj
*g
= newtype(gccrand_pytype
, 0, info
->name
);
727 g
->ty
.ht_type
.tp_basicsize
= sizeof(grand_pyobj
);
728 switch (info
->f
&RNGF_MASK
) {
729 case RNG_LATIN
: g
->ty
.ht_type
.tp_base
= gclatinrand_pytype
; break;
730 default: g
->ty
.ht_type
.tp_base
= gcrand_pytype
; break;
732 Py_INCREF(g
->ty
.ht_type
.tp_base
);
733 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
734 Py_TPFLAGS_BASETYPE
|
735 Py_TPFLAGS_HEAPTYPE
);
736 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
737 g
->ty
.ht_type
.tp_free
= 0;
738 switch (info
->f
&RNGF_MASK
) {
739 case RNG_LATIN
: g
->ty
.ht_type
.tp_new
= gcnrand_pynew
; break;
740 case RNG_SEAL
: g
->ty
.ht_type
.tp_new
= gcirand_pynew
; break;
741 case RNG_SHAKE
: case RNG_KMAC
:
742 g
->ty
.ht_type
.tp_new
= gcshakyrand_pynew
; break;
743 default: g
->ty
.ht_type
.tp_new
= gcrand_pynew
; break;
745 typeready(&g
->ty
.ht_type
);
746 return ((PyObject
*)g
);
749 static PyObject
*gccrget_name(PyObject
*me
, void *hunoz
)
750 { return (PyString_FromString(GCCRAND_INFO(me
)->name
)); }
751 static PyObject
*gccrget_keysz(PyObject
*me
, void *hunoz
)
752 { return (keysz_pywrap(GCCRAND_INFO(me
)->keysz
)); }
754 static PyObject
*gclrmeth_tell(PyObject
*me
, PyObject
*arg
)
756 grand
*r
= GRAND_R(me
);
760 if (!PyArg_ParseTuple(arg
, ":tell")) return (0);
761 r
->ops
->misc(r
, SALSA20_TELLU64
, &off
);
766 static PyObject
*gclrmeth_seek(PyObject
*me
, PyObject
*arg
)
768 grand
*r
= GRAND_R(me
);
771 if (!PyArg_ParseTuple(arg
, "O&:seek", convk64
, &off
)) return (0);
772 r
->ops
->misc(r
, SALSA20_SEEKU64
, off
);
776 static PyGetSetDef gccrand_pygetset
[] = {
777 #define GETSETNAME(op, name) gccr##op##_##name
778 GET (keysz
, "CR.keysz -> acceptable key sizes")
779 GET (name
, "CR.name -> name of this kind of generator")
784 static PyMethodDef gclatinrand_pymethods
[] = {
785 #define METHNAME(name) gclrmeth_##name
786 METH (tell
, "R.tell() -> OFF")
787 METH (seek
, "R.seek(OFF)")
792 static PyTypeObject gccrand_pytype_skel
= {
793 PyObject_HEAD_INIT(0) 0, /* Header */
794 "GCCRand", /* @tp_name@ */
795 sizeof(gccrand_pyobj
), /* @tp_basicsize@ */
796 0, /* @tp_itemsize@ */
798 0, /* @tp_dealloc@ */
800 0, /* @tp_getattr@ */
801 0, /* @tp_setattr@ */
802 0, /* @tp_compare@ */
804 0, /* @tp_as_number@ */
805 0, /* @tp_as_sequence@ */
806 0, /* @tp_as_mapping@ */
810 0, /* @tp_getattro@ */
811 0, /* @tp_setattro@ */
812 0, /* @tp_as_buffer@ */
813 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
817 "Metaclass for symmetric crypto-based generators.",
819 0, /* @tp_traverse@ */
821 0, /* @tp_richcompare@ */
822 0, /* @tp_weaklistoffset@ */
824 0, /* @tp_iternext@ */
825 0, /* @tp_methods@ */
826 0, /* @tp_members@ */
827 gccrand_pygetset
, /* @tp_getset@ */
830 0, /* @tp_descr_get@ */
831 0, /* @tp_descr_set@ */
832 0, /* @tp_dictoffset@ */
834 PyType_GenericAlloc
, /* @tp_alloc@ */
835 abstract_pynew
, /* @tp_new@ */
840 static PyTypeObject gcrand_pytype_skel
= {
841 PyObject_HEAD_INIT(0) 0, /* Header */
842 "GCRand", /* @tp_name@ */
843 sizeof(grand_pyobj
), /* @tp_basicsize@ */
844 0, /* @tp_itemsize@ */
846 grand_pydealloc
, /* @tp_dealloc@ */
848 0, /* @tp_getattr@ */
849 0, /* @tp_setattr@ */
850 0, /* @tp_compare@ */
852 0, /* @tp_as_number@ */
853 0, /* @tp_as_sequence@ */
854 0, /* @tp_as_mapping@ */
858 0, /* @tp_getattro@ */
859 0, /* @tp_setattro@ */
860 0, /* @tp_as_buffer@ */
861 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
865 "Abstract base class for symmetric crypto-based generators.",
867 0, /* @tp_traverse@ */
869 0, /* @tp_richcompare@ */
870 0, /* @tp_weaklistoffset@ */
872 0, /* @tp_iternext@ */
873 0, /* @tp_methods@ */
874 0, /* @tp_members@ */
878 0, /* @tp_descr_get@ */
879 0, /* @tp_descr_set@ */
880 0, /* @tp_dictoffset@ */
882 PyType_GenericAlloc
, /* @tp_alloc@ */
883 abstract_pynew
, /* @tp_new@ */
888 static PyTypeObject gclatinrand_pytype_skel
= {
889 PyObject_HEAD_INIT(0) 0, /* Header */
890 "GCLatinRand", /* @tp_name@ */
891 sizeof(grand_pyobj
), /* @tp_basicsize@ */
892 0, /* @tp_itemsize@ */
894 grand_pydealloc
, /* @tp_dealloc@ */
896 0, /* @tp_getattr@ */
897 0, /* @tp_setattr@ */
898 0, /* @tp_compare@ */
900 0, /* @tp_as_number@ */
901 0, /* @tp_as_sequence@ */
902 0, /* @tp_as_mapping@ */
906 0, /* @tp_getattro@ */
907 0, /* @tp_setattro@ */
908 0, /* @tp_as_buffer@ */
909 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
913 "Abstract base class for symmetric crypto-based generators.",
915 0, /* @tp_traverse@ */
917 0, /* @tp_richcompare@ */
918 0, /* @tp_weaklistoffset@ */
920 0, /* @tp_iternext@ */
921 gclatinrand_pymethods
, /* @tp_methods@ */
922 0, /* @tp_members@ */
926 0, /* @tp_descr_get@ */
927 0, /* @tp_descr_set@ */
928 0, /* @tp_dictoffset@ */
930 PyType_GenericAlloc
, /* @tp_alloc@ */
931 abstract_pynew
, /* @tp_new@ */
936 /*----- SSL and TLS generators --------------------------------------------*/
938 static PyObject
*sslprf_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
942 const gchash
*hco
= &md5
, *hci
= &sha
;
944 static const char *const kwlist
[] = { "key", "seed", "ohash", "ihash", 0 };
946 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#s#|O&O&:new", KWLIST
,
948 convgchash
, &hco
, convgchash
, &hci
))
950 rc
= grand_dopywrap(ty
, sslprf_rand(hco
, hci
, k
, ksz
, s
, ssz
), f_freeme
);
955 static PyObject
*tlsdx_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
959 const gcmac
*mc
= &sha_hmac
;
961 static const char *const kwlist
[] = { "key", "seed", "mac", 0 };
963 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#s#|O&:new", KWLIST
,
967 rc
= grand_dopywrap(ty
, tlsdx_rand(mc
, k
, ksz
, s
, ssz
), f_freeme
);
972 static PyObject
*tlsprf_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
976 const gcmac
*mcl
= &md5_hmac
, *mcr
= &sha_hmac
;
978 static const char *const kwlist
[] = { "key", "seed", "lmac", "rmac", 0 };
980 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#s#|O&O&:new", KWLIST
,
982 convgcmac
, &mcl
, convgcmac
, &mcr
))
984 rc
= grand_dopywrap(ty
, tlsprf_rand(mcl
, mcr
, k
, ksz
, s
, ssz
), f_freeme
);
989 static PyTypeObject sslprf_pytype_skel
= {
990 PyObject_HEAD_INIT(0) 0, /* Header */
991 "SSLRand", /* @tp_name@ */
992 sizeof(grand_pyobj
), /* @tp_basicsize@ */
993 0, /* @tp_itemsize@ */
995 grand_pydealloc
, /* @tp_dealloc@ */
997 0, /* @tp_getattr@ */
998 0, /* @tp_setattr@ */
999 0, /* @tp_compare@ */
1001 0, /* @tp_as_number@ */
1002 0, /* @tp_as_sequence@ */
1003 0, /* @tp_as_mapping@ */
1007 0, /* @tp_getattro@ */
1008 0, /* @tp_setattro@ */
1009 0, /* @tp_as_buffer@ */
1010 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1011 Py_TPFLAGS_BASETYPE
,
1014 "SSLRand(KEY, SEED, [ohash = md5], [ihash = sha]):\n\
1015 RNG for SSL master secret.",
1017 0, /* @tp_traverse@ */
1019 0, /* @tp_richcompare@ */
1020 0, /* @tp_weaklistoffset@ */
1022 0, /* @tp_iternext@ */
1023 0, /* @tp_methods@ */
1024 0, /* @tp_members@ */
1025 0, /* @tp_getset@ */
1028 0, /* @tp_descr_get@ */
1029 0, /* @tp_descr_set@ */
1030 0, /* @tp_dictoffset@ */
1032 PyType_GenericAlloc
, /* @tp_alloc@ */
1033 sslprf_pynew
, /* @tp_new@ */
1038 static PyTypeObject tlsdx_pytype_skel
= {
1039 PyObject_HEAD_INIT(0) 0, /* Header */
1040 "TLSDataExpansion", /* @tp_name@ */
1041 sizeof(grand_pyobj
), /* @tp_basicsize@ */
1042 0, /* @tp_itemsize@ */
1044 grand_pydealloc
, /* @tp_dealloc@ */
1046 0, /* @tp_getattr@ */
1047 0, /* @tp_setattr@ */
1048 0, /* @tp_compare@ */
1050 0, /* @tp_as_number@ */
1051 0, /* @tp_as_sequence@ */
1052 0, /* @tp_as_mapping@ */
1056 0, /* @tp_getattro@ */
1057 0, /* @tp_setattro@ */
1058 0, /* @tp_as_buffer@ */
1059 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1060 Py_TPFLAGS_BASETYPE
,
1063 "TLSDataExpansion(KEY, SEED, [mac = sha_hmac]):\n\
1064 TLS data expansion function.",
1066 0, /* @tp_traverse@ */
1068 0, /* @tp_richcompare@ */
1069 0, /* @tp_weaklistoffset@ */
1071 0, /* @tp_iternext@ */
1072 0, /* @tp_methods@ */
1073 0, /* @tp_members@ */
1074 0, /* @tp_getset@ */
1077 0, /* @tp_descr_get@ */
1078 0, /* @tp_descr_set@ */
1079 0, /* @tp_dictoffset@ */
1081 PyType_GenericAlloc
, /* @tp_alloc@ */
1082 tlsdx_pynew
, /* @tp_new@ */
1087 static PyTypeObject tlsprf_pytype_skel
= {
1088 PyObject_HEAD_INIT(0) 0, /* Header */
1089 "TLSPRF", /* @tp_name@ */
1090 sizeof(grand_pyobj
), /* @tp_basicsize@ */
1091 0, /* @tp_itemsize@ */
1093 grand_pydealloc
, /* @tp_dealloc@ */
1095 0, /* @tp_getattr@ */
1096 0, /* @tp_setattr@ */
1097 0, /* @tp_compare@ */
1099 0, /* @tp_as_number@ */
1100 0, /* @tp_as_sequence@ */
1101 0, /* @tp_as_mapping@ */
1105 0, /* @tp_getattro@ */
1106 0, /* @tp_setattro@ */
1107 0, /* @tp_as_buffer@ */
1108 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1109 Py_TPFLAGS_BASETYPE
,
1112 "TLSPRF(KEY, SEED, [lmac = md5_hmac], [rmac = sha_hmac]):\n\
1113 TLS pseudorandom function.",
1115 0, /* @tp_traverse@ */
1117 0, /* @tp_richcompare@ */
1118 0, /* @tp_weaklistoffset@ */
1120 0, /* @tp_iternext@ */
1121 0, /* @tp_methods@ */
1122 0, /* @tp_members@ */
1123 0, /* @tp_getset@ */
1126 0, /* @tp_descr_get@ */
1127 0, /* @tp_descr_set@ */
1128 0, /* @tp_dictoffset@ */
1130 PyType_GenericAlloc
, /* @tp_alloc@ */
1131 tlsprf_pynew
, /* @tp_new@ */
1136 /*----- DSA generator -----------------------------------------------------*/
1138 static PyObject
*dsarand_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1143 static const char *const kwlist
[] = { "seed", 0 };
1145 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", KWLIST
, &p
, &sz
))
1147 rc
= grand_dopywrap(ty
, dsarand_create(p
, sz
), f_freeme
);
1152 static PyObject
*drget_seed(PyObject
*me
, void *hunoz
)
1154 grand
*r
= GRAND_R(me
);
1155 int n
= r
->ops
->misc(r
, DSARAND_SEEDSZ
);
1156 PyObject
*rc
= bytestring_pywrap(0, n
);
1157 r
->ops
->misc(r
, DSARAND_GETSEED
, PyString_AS_STRING(rc
));
1161 static PyGetSetDef dsarand_pygetset
[] = {
1162 #define GETSETNAME(op, name) dr##op##_##name
1163 GET (seed
, "R.seed -> current generator seed")
1168 static PyTypeObject dsarand_pytype_skel
= {
1169 PyObject_HEAD_INIT(0) 0, /* Header */
1170 "DSARand", /* @tp_name@ */
1171 sizeof(grand_pyobj
), /* @tp_basicsize@ */
1172 0, /* @tp_itemsize@ */
1174 grand_pydealloc
, /* @tp_dealloc@ */
1176 0, /* @tp_getattr@ */
1177 0, /* @tp_setattr@ */
1178 0, /* @tp_compare@ */
1180 0, /* @tp_as_number@ */
1181 0, /* @tp_as_sequence@ */
1182 0, /* @tp_as_mapping@ */
1186 0, /* @tp_getattro@ */
1187 0, /* @tp_setattro@ */
1188 0, /* @tp_as_buffer@ */
1189 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1190 Py_TPFLAGS_BASETYPE
,
1193 "DSARand(SEED): pseudorandom number generator for DSA parameters.",
1195 0, /* @tp_traverse@ */
1197 0, /* @tp_richcompare@ */
1198 0, /* @tp_weaklistoffset@ */
1200 0, /* @tp_iternext@ */
1201 0, /* @tp_methods@ */
1202 0, /* @tp_members@ */
1203 dsarand_pygetset
, /* @tp_getset@ */
1206 0, /* @tp_descr_get@ */
1207 0, /* @tp_descr_set@ */
1208 0, /* @tp_dictoffset@ */
1210 PyType_GenericAlloc
, /* @tp_alloc@ */
1211 dsarand_pynew
, /* @tp_new@ */
1216 /*----- Blum-Blum-Shub generator ------------------------------------------*/
1218 static PyObject
*bbs_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1220 mp
*n
= 0, *x
= MP_TWO
;
1222 static const char *const kwlist
[] = { "n", "x", 0 };
1224 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&|O&:new", KWLIST
,
1225 convmp
, &n
, convmp
, &x
))
1227 rc
= grand_dopywrap(ty
, bbs_rand(n
, x
), f_freeme
);
1234 static PyObject
*bbsmeth_step(PyObject
*me
, PyObject
*arg
)
1236 grand
*r
= GRAND_R(me
); if (!PyArg_ParseTuple(arg
, ":step")) return (0);
1237 r
->ops
->misc(r
, BBS_STEP
); RETURN_ME
;
1240 static PyObject
*bbsmeth_bits(PyObject
*me
, PyObject
*arg
)
1242 grand
*r
= GRAND_R(me
); unsigned n
; uint32 w
;
1243 if (!PyArg_ParseTuple(arg
, "O&:bits", convuint
, &n
)) goto end
;
1244 if (n
> 32) VALERR("can't get more than 32 bits");
1245 r
->ops
->misc(r
, BBS_BITS
, n
, &w
); return (getulong(w
));
1250 static PyObject
*bbsmeth_wrap(PyObject
*me
, PyObject
*arg
)
1252 grand
*r
= GRAND_R(me
); if (!PyArg_ParseTuple(arg
, ":wrap")) return (0);
1253 r
->ops
->misc(r
, BBS_WRAP
); RETURN_ME
;
1256 static PyObject
*bbsget_n(PyObject
*me
, void *hunoz
)
1258 mp
*x
= MP_NEW
; grand
*r
= GRAND_R(me
);
1259 r
->ops
->misc(r
, BBS_MOD
, &x
); return (mp_pywrap(x
));
1262 static PyObject
*bbsget_x(PyObject
*me
, void *hunoz
)
1264 mp
*x
= MP_NEW
; grand
*r
= GRAND_R(me
);
1265 r
->ops
->misc(r
, BBS_STATE
, &x
); return (mp_pywrap(x
));
1268 static int bbsset_x(PyObject
*me
, PyObject
*val
, void *hunoz
)
1270 mp
*x
= 0; grand
*r
= GRAND_R(me
); int rc
= -1; if (!x
) NIERR("__del__");
1271 if ((x
= getmp(val
)) == 0) goto end
;
1272 r
->ops
->misc(r
, BBS_SET
, x
); rc
= 0;
1273 end
: mp_drop(x
); return (rc
);
1276 static PyObject
*bbsget_stepsz(PyObject
*me
, void *hunoz
)
1278 grand
*r
= GRAND_R(me
);
1279 return (PyInt_FromLong(r
->ops
->misc(r
, BBS_STEPSZ
)));
1282 static PyMethodDef bbs_pymethods
[] = {
1283 #define METHNAME(name) bbsmeth_##name
1284 METH (step
, "R.step(): steps the generator (not useful)")
1285 METH (bits
, "R.bits(N) -> W: returns N bits (<= 32) from the generator")
1286 METH (wrap
, "R.wrap(): flushes unused bits in internal buffer")
1291 static PyGetSetDef bbs_pygetset
[] = {
1292 #define GETSETNAME(op, name) bbs##op##_##name
1293 GET (n
, "R.n -> Blum modulus")
1294 GETSET(x
, "R.x -> current seed value")
1295 GET (stepsz
, "R.stepsz -> number of bits generated per step")
1300 static PyTypeObject bbs_pytype_skel
= {
1301 PyObject_HEAD_INIT(0) 0, /* Header */
1302 "BlumBlumShub", /* @tp_name@ */
1303 sizeof(grand_pyobj
), /* @tp_basicsize@ */
1304 0, /* @tp_itemsize@ */
1306 grand_pydealloc
, /* @tp_dealloc@ */
1308 0, /* @tp_getattr@ */
1309 0, /* @tp_setattr@ */
1310 0, /* @tp_compare@ */
1312 0, /* @tp_as_number@ */
1313 0, /* @tp_as_sequence@ */
1314 0, /* @tp_as_mapping@ */
1318 0, /* @tp_getattro@ */
1319 0, /* @tp_setattro@ */
1320 0, /* @tp_as_buffer@ */
1321 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1322 Py_TPFLAGS_BASETYPE
,
1325 "BlumBlumShub(N, [x = 2]): Blum-Blum-Shub pseudorandom number generator.",
1327 0, /* @tp_traverse@ */
1329 0, /* @tp_richcompare@ */
1330 0, /* @tp_weaklistoffset@ */
1332 0, /* @tp_iternext@ */
1333 bbs_pymethods
, /* @tp_methods@ */
1334 0, /* @tp_members@ */
1335 bbs_pygetset
, /* @tp_getset@ */
1338 0, /* @tp_descr_get@ */
1339 0, /* @tp_descr_set@ */
1340 0, /* @tp_dictoffset@ */
1342 PyType_GenericAlloc
, /* @tp_alloc@ */
1343 bbs_pynew
, /* @tp_new@ */
1348 typedef struct bbspriv_pyobj
{
1353 #define BBSPRIV_BP(o) (&((bbspriv_pyobj *)(o))->bp)
1355 static PyObject
*bbspriv_pynew(PyTypeObject
*ty
,
1356 PyObject
*arg
, PyObject
*kw
)
1358 mp
*p
= 0, *q
= 0, *n
= 0, *x
= MP_TWO
;
1359 bbspriv_pyobj
*rc
= 0;
1360 static const char *const kwlist
[] = { "n", "p", "q", "seed", 0 };
1362 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&O&O&O&:new", KWLIST
,
1363 convmp
, &n
, convmp
, &p
, convmp
, &q
,
1366 if (!n
+ !p
+ !q
> 1) VALERR("must specify at least two of n, p, q");
1367 if (!n
) n
= mp_mul(MP_NEW
, p
, q
);
1368 else if (!p
) mp_div(&p
, 0, n
, q
);
1369 else if (!q
) mp_div(&q
, 0, n
, p
);
1370 rc
= (bbspriv_pyobj
*)ty
->tp_alloc(ty
, 0);
1371 rc
->gr
.r
= bbs_rand(n
, x
);
1372 rc
->gr
.f
= f_freeme
;
1373 rc
->bp
.p
= MP_COPY(p
);
1374 rc
->bp
.q
= MP_COPY(q
);
1375 rc
->bp
.n
= MP_COPY(n
);
1377 mp_drop(p
); mp_drop(q
); mp_drop(n
); mp_drop(x
);
1378 return ((PyObject
*)rc
);
1381 static PyObject
*meth__BBSPriv_generate(PyObject
*me
,
1382 PyObject
*arg
, PyObject
*kw
)
1384 bbs_priv bp
= { 0 };
1387 unsigned nbits
, n
= 0;
1388 grand
*r
= &rand_global
;
1389 static const char *const kwlist
[] =
1390 { "class", "nbits", "event", "rng", "nsteps", "seed", 0 };
1391 bbspriv_pyobj
*rc
= 0;
1393 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "OO&|O&O&O&O&:generate", KWLIST
,
1394 &me
, convuint
, &nbits
, convpgev
, &evt
,
1395 convgrand
, &r
, convuint
, &n
, convmp
, &x
))
1397 if (bbs_gen(&bp
, nbits
, r
, n
, evt
.proc
, evt
.ctx
))
1398 VALERR("prime genration failed");
1399 rc
= PyObject_New(bbspriv_pyobj
, bbspriv_pytype
);
1400 rc
->gr
.r
= bbs_rand(bp
.n
, x
);
1401 rc
->gr
.f
= f_freeme
;
1402 rc
->bp
.p
= MP_COPY(bp
.p
);
1403 rc
->bp
.q
= MP_COPY(bp
.q
);
1404 rc
->bp
.n
= MP_COPY(bp
.n
);
1406 mp_drop(bp
.p
); mp_drop(bp
.q
); mp_drop(bp
.n
); mp_drop(x
);
1407 return ((PyObject
*)rc
);
1410 static void bbspriv_pydealloc(PyObject
*me
)
1412 bbs_priv
*bp
= BBSPRIV_BP(me
);
1416 grand_pydealloc(me
);
1419 static PyObject
*bpmeth_ff(PyObject
*me
, PyObject
*arg
)
1421 mp
*n
= 0; grand
*r
= GRAND_R(me
); bbs_priv
*bp
= BBSPRIV_BP(me
);
1422 if (!PyArg_ParseTuple(arg
, "O&:ff", convmp
, &n
)) return (0);
1423 r
->ops
->misc(r
, BBS_FF
, bp
, n
); RETURN_ME
;
1426 static PyObject
*bpmeth_rew(PyObject
*me
, PyObject
*arg
)
1428 mp
*n
= 0; grand
*r
= GRAND_R(me
); bbs_priv
*bp
= BBSPRIV_BP(me
);
1429 if (!PyArg_ParseTuple(arg
, "O&:rew", convmp
, &n
)) return (0);
1430 r
->ops
->misc(r
, BBS_REW
, bp
, n
); RETURN_ME
;
1433 static PyObject
*bpget_n(PyObject
*me
, void *hunoz
)
1434 { return (mp_pywrap(MP_COPY(BBSPRIV_BP(me
)->n
))); }
1436 static PyObject
*bpget_p(PyObject
*me
, void *hunoz
)
1437 { return (mp_pywrap(MP_COPY(BBSPRIV_BP(me
)->p
))); }
1439 static PyObject
*bpget_q(PyObject
*me
, void *hunoz
)
1440 { return (mp_pywrap(MP_COPY(BBSPRIV_BP(me
)->q
))); }
1442 static PyMethodDef bbspriv_pymethods
[] = {
1443 #define METHNAME(name) bpmeth_##name
1444 METH (ff
, "R.ff(N): fast-forward N places")
1445 METH (rew
, "R.rew(N): rewind N places")
1450 static PyGetSetDef bbspriv_pygetset
[] = {
1451 #define GETSETNAME(op, name) bp##op##_##name
1452 GET (n
, "R.n -> Blum modulus")
1453 GET (p
, "R.p -> one of the factors of the modulus")
1454 GET (q
, "R.q -> one of the factors of the modulus")
1459 static PyTypeObject bbspriv_pytype_skel
= {
1460 PyObject_HEAD_INIT(0) 0, /* Header */
1461 "BBSPriv", /* @tp_name@ */
1462 sizeof(bbspriv_pyobj
), /* @tp_basicsize@ */
1463 0, /* @tp_itemsize@ */
1465 bbspriv_pydealloc
, /* @tp_dealloc@ */
1467 0, /* @tp_getattr@ */
1468 0, /* @tp_setattr@ */
1469 0, /* @tp_compare@ */
1471 0, /* @tp_as_number@ */
1472 0, /* @tp_as_sequence@ */
1473 0, /* @tp_as_mapping@ */
1477 0, /* @tp_getattro@ */
1478 0, /* @tp_setattro@ */
1479 0, /* @tp_as_buffer@ */
1480 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1481 Py_TPFLAGS_BASETYPE
,
1484 "BBSPriv(..., seed = 2]): Blum-Blum-Shub, with private key.\n\
1485 Keywords: n, p, q; must provide at least two",
1487 0, /* @tp_traverse@ */
1489 0, /* @tp_richcompare@ */
1490 0, /* @tp_weaklistoffset@ */
1492 0, /* @tp_iternext@ */
1493 bbspriv_pymethods
, /* @tp_methods@ */
1494 0, /* @tp_members@ */
1495 bbspriv_pygetset
, /* @tp_getset@ */
1498 0, /* @tp_descr_get@ */
1499 0, /* @tp_descr_set@ */
1500 0, /* @tp_dictoffset@ */
1502 PyType_GenericAlloc
, /* @tp_alloc@ */
1503 bbspriv_pynew
, /* @tp_new@ */
1508 /*----- Global stuff ------------------------------------------------------*/
1510 static PyMethodDef methods
[] = {
1511 #define METHNAME(name) meth_##name
1512 KWMETH(_BBSPriv_generate
, "\
1513 generate(NBITS, [event = pgen_nullev, rng = rand, nsteps = 0, seed = 2])")
1518 void rand_pyinit(void)
1520 INITTYPE(grand
, root
);
1521 INITTYPE(truerand
, grand
);
1522 INITTYPE(fibrand
, grand
);
1523 INITTYPE(lcrand
, grand
);
1524 INITTYPE(dsarand
, grand
);
1525 INITTYPE(bbs
, grand
);
1526 INITTYPE(bbspriv
, bbs
);
1527 INITTYPE(sslprf
, grand
);
1528 INITTYPE(tlsdx
, grand
);
1529 INITTYPE(tlsprf
, grand
);
1530 INITTYPE(gccrand
, type
);
1531 INITTYPE(gcrand
, grand
);
1532 INITTYPE(gclatinrand
, gcrand
);
1533 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
1534 rand_seed(RAND_GLOBAL
, 160);
1535 addmethods(methods
);
1538 #define gccrand gccrand_info
1539 GEN(gccrands
, crand
)
1541 void rand_pyinsert(PyObject
*mod
)
1543 INSERT("GRand", grand_pytype
);
1544 INSERT("TrueRand", truerand_pytype
);
1545 INSERT("LCRand", lcrand_pytype
);
1546 INSERT("FibRand", fibrand_pytype
);
1547 INSERT("SSLRand", sslprf_pytype
);
1548 INSERT("TLSDataExpansion", tlsdx_pytype
);
1549 INSERT("TLSPRF", tlsprf_pytype
);
1550 INSERT("DSARand", dsarand_pytype
);
1551 INSERT("BlumBlumShub", bbs_pytype
);
1552 INSERT("BBSPriv", bbspriv_pytype
);
1553 INSERT("GCCRand", gccrand_pytype
);
1554 INSERT("GCRand", gcrand_pytype
);
1555 INSERT("GCLatinRand", gclatinrand_pytype
);
1556 rand_pyobj
= grand_pywrap(&rand_global
, 0); Py_INCREF(rand_pyobj
);
1557 gccrands_dict
= gccrands(); Py_INCREF(gccrands_dict
);
1558 INSERT("gccrands", gccrands_dict
);
1559 INSERT("rand", rand_pyobj
);
1562 /*----- That's all, folks -------------------------------------------------*/