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
= PyDict_GetItemString(gccrands_dict
, r
->ops
->name
)) != 0)
69 ty
= (PyTypeObject
*)ob
;
70 return (grand_dopywrap(ty
, r
, f
));
73 CONVFUNC(grand
, grand
*, GRAND_R
)
75 static int grand_check(PyObject
*me
)
77 if (!GRAND_R(me
)) VALERR("random generator object is no longer valid");
83 static PyObject
*grmeth_byte(PyObject
*me
, PyObject
*arg
)
85 if (!PyArg_ParseTuple(arg
, ":byte")) return (0);
86 if (grand_check(me
)) return (0);
87 return (PyInt_FromLong(grand_byte(GRAND_R(me
))));
90 static PyObject
*grmeth_word(PyObject
*me
, PyObject
*arg
)
92 if (!PyArg_ParseTuple(arg
, ":word")) return (0);
93 if (grand_check(me
)) return (0);
94 return (getulong(grand_word(GRAND_R(me
))));
97 static PyObject
*grmeth_range(PyObject
*me
, PyObject
*arg
)
103 if (!PyArg_ParseTuple(arg
, "O:range", &m
)) return (0);
104 if (grand_check(me
)) return (0);
105 if (PyInt_Check(m
)) {
106 long mm
= PyInt_AS_LONG(m
);
109 if (mm
<= 0xffffffff)
110 return (PyInt_FromLong(grand_range(GRAND_R(me
), mm
)));
112 if ((x
= getmp(m
)) == 0)
116 y
= mprand_range(MP_NEW
, x
, GRAND_R(me
), 0);
118 return (mp_pywrap(y
));
120 VALERR("range must be strictly positive");
126 static PyObject
*grmeth_mp(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
130 static const char *const kwlist
[] = { "bits", "or", 0 };
132 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&|O&:mp", KWLIST
,
133 convszt
, &l
, convmpw
, &o
))
135 if (grand_check(me
)) return (0);
136 if (l
< MPW_BITS
&& (o
>> l
)) VALERR("or mask too large");
137 return (mp_pywrap(mprand(MP_NEW
, l
, GRAND_R(me
), o
)));
142 static PyObject
*grmeth_block(PyObject
*me
, PyObject
*arg
)
147 if (!PyArg_ParseTuple(arg
, "O&:block", convulong
, &n
)) goto end
;
148 if (grand_check(me
)) return (0);
149 rc
= bytestring_pywrap(0, n
);
150 grand_fill(GRAND_R(me
), PyString_AS_STRING(rc
), n
);
155 static int checkop(grand
*r
, unsigned op
, const char *what
)
157 if (r
->ops
->misc(r
, GRAND_CHECK
, op
)) return (0);
158 PyErr_Format(PyExc_TypeError
, "operation %s not supported", what
);
162 static PyObject
*grmeth_seedint(PyObject
*me
, PyObject
*arg
)
165 grand
*r
= GRAND_R(me
);
166 if (!PyArg_ParseTuple(arg
, "i:seedint", &i
) ||
167 grand_check(me
) || checkop(r
, GRAND_SEEDINT
, "seedint"))
169 r
->ops
->misc(r
, GRAND_SEEDINT
, i
);
175 static PyObject
*grmeth_seedword(PyObject
*me
, PyObject
*arg
)
178 grand
*r
= GRAND_R(me
);
179 if (!PyArg_ParseTuple(arg
, "O&:seedword", convu32
, &u
) ||
180 grand_check(me
) || checkop(r
, GRAND_SEEDUINT32
, "seedword"))
182 r
->ops
->misc(r
, GRAND_SEEDUINT32
, u
);
188 static PyObject
*grmeth_seedblock(PyObject
*me
, PyObject
*arg
)
192 grand
*r
= GRAND_R(me
);
193 if (!PyArg_ParseTuple(arg
, "s#:seedblock", &p
, &n
) ||
194 grand_check(me
) || checkop(r
, GRAND_SEEDBLOCK
, "seedblock"))
196 r
->ops
->misc(r
, GRAND_SEEDBLOCK
, p
, (size_t)n
);
202 static PyObject
*grmeth_seedmp(PyObject
*me
, PyObject
*arg
)
206 grand
*r
= GRAND_R(me
);
207 if (!PyArg_ParseTuple(arg
, "O:seedmp", &x
) ||
208 grand_check(me
) || checkop(r
, GRAND_SEEDMP
, "seedmp") ||
209 (xx
= getmp(x
)) == 0)
211 r
->ops
->misc(r
, GRAND_SEEDMP
, xx
);
218 static PyObject
*grmeth_seedrand(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
220 static const char *const kwlist
[] = { "rng", 0 };
221 grand
*r
= GRAND_R(me
);
222 grand
*rr
= &rand_global
;
223 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:seedrand", KWLIST
,
225 grand_check(me
) || checkop(r
, GRAND_SEEDRAND
, "seedrand"))
227 r
->ops
->misc(r
, GRAND_SEEDRAND
, rr
);
233 static PyObject
*grmeth_mask(PyObject
*me
, PyObject
*arg
)
235 grand
*r
= GRAND_R(me
);
240 if (!PyArg_ParseTuple(arg
, "s#:mask", &p
, &sz
)) return (0);
241 if (grand_check(me
)) return (0);
242 rc
= bytestring_pywrap(0, sz
);
243 q
= PyString_AS_STRING(rc
);
245 while (sz
--) *q
++ ^= *p
++;
249 static void grand_pydealloc(PyObject
*me
)
251 grand_pyobj
*g
= (grand_pyobj
*)me
;
252 if ((g
->f
& f_freeme
) && g
->r
) GR_DESTROY(g
->r
);
256 static PyObject
*grget_name(PyObject
*me
, void *hunoz
)
257 { return (grand_check(me
) ?
0 : PyString_FromString(GRAND_R(me
)->ops
->name
)); }
259 static PyObject
*grget_cryptop(PyObject
*me
, void *hunoz
)
260 { return (grand_check(me
) ?
0 : getbool(GRAND_R(me
)->ops
->f
& GRAND_CRYPTO
)); }
262 static PyGetSetDef grand_pygetset
[] = {
263 #define GETSETNAME(op, name) gr##op##_##name
264 GET (name
, "R.name -> name of this kind of generator")
265 GET (cryptop
, "R.cryptop -> flag: cryptographically strong?")
270 static PyMethodDef grand_pymethods
[] = {
271 #define METHNAME(name) grmeth_##name
272 METH (byte
, "R.byte() -> BYTE")
273 METH (word
, "R.word() -> WORD")
274 METH (block
, "R.block(N) -> STRING")
275 KWMETH(mp
, "R.mp(bits, [or = 0]) -> MP")
276 METH (range
, "R.range(MAX) -> INT")
277 METH (mask
, "R.mask(STR) -> STR")
278 METH (seedint
, "R.seedint(I)")
279 METH (seedword
, "R.seedword(I)")
280 METH (seedblock
, "R.seedblock(BYTES)")
281 METH (seedmp
, "R.seedmp(X)")
282 KWMETH(seedrand
, "R.seedrand(RR)")
287 static PyTypeObject grand_pytype_skel
= {
288 PyObject_HEAD_INIT(0) 0, /* Header */
289 "GRand", /* @tp_name@ */
290 sizeof(grand_pyobj
), /* @tp_basicsize@ */
291 0, /* @tp_itemsize@ */
293 grand_pydealloc
, /* @tp_dealloc@ */
295 0, /* @tp_getattr@ */
296 0, /* @tp_setattr@ */
297 0, /* @tp_compare@ */
299 0, /* @tp_as_number@ */
300 0, /* @tp_as_sequence@ */
301 0, /* @tp_as_mapping@ */
305 0, /* @tp_getattro@ */
306 0, /* @tp_setattro@ */
307 0, /* @tp_as_buffer@ */
308 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
312 "Generic random number source.",
314 0, /* @tp_traverse@ */
316 0, /* @tp_richcompare@ */
317 0, /* @tp_weaklistoffset@ */
319 0, /* @tp_iternext@ */
320 grand_pymethods
, /* @tp_methods@ */
321 0, /* @tp_members@ */
322 grand_pygetset
, /* @tp_getset@ */
325 0, /* @tp_descr_get@ */
326 0, /* @tp_descr_set@ */
327 0, /* @tp_dictoffset@ */
329 PyType_GenericAlloc
, /* @tp_alloc@ */
330 abstract_pynew
, /* @tp_new@ */
335 static PyObject
*lcrand_pynew(PyTypeObject
*me
, PyObject
*arg
, PyObject
*kw
)
338 static const char *const kwlist
[] = { "seed", 0 };
339 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:new", KWLIST
, convu32
, &n
))
341 return (grand_dopywrap(lcrand_pytype
, lcrand_create(n
), f_freeme
));
344 static PyTypeObject lcrand_pytype_skel
= {
345 PyObject_HEAD_INIT(0) 0, /* Header */
346 "LCRand", /* @tp_name@ */
347 sizeof(grand_pyobj
), /* @tp_basicsize@ */
348 0, /* @tp_itemsize@ */
350 grand_pydealloc
, /* @tp_dealloc@ */
352 0, /* @tp_getattr@ */
353 0, /* @tp_setattr@ */
354 0, /* @tp_compare@ */
356 0, /* @tp_as_number@ */
357 0, /* @tp_as_sequence@ */
358 0, /* @tp_as_mapping@ */
362 0, /* @tp_getattro@ */
363 0, /* @tp_setattro@ */
364 0, /* @tp_as_buffer@ */
365 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
369 "LCRand([seed = 0]): linear congruential generator.",
371 0, /* @tp_traverse@ */
373 0, /* @tp_richcompare@ */
374 0, /* @tp_weaklistoffset@ */
376 0, /* @tp_iternext@ */
377 0, /* @tp_methods@ */
378 0, /* @tp_members@ */
382 0, /* @tp_descr_get@ */
383 0, /* @tp_descr_set@ */
384 0, /* @tp_dictoffset@ */
386 PyType_GenericAlloc
, /* @tp_alloc@ */
387 lcrand_pynew
, /* @tp_new@ */
392 static PyObject
*fibrand_pynew(PyTypeObject
*me
, PyObject
*arg
, PyObject
*kw
)
395 static const char *const kwlist
[] = { "seed", 0 };
396 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:new", KWLIST
, convu32
, &n
))
398 return (grand_dopywrap(fibrand_pytype
, fibrand_create(n
), f_freeme
));
401 static PyTypeObject fibrand_pytype_skel
= {
402 PyObject_HEAD_INIT(0) 0, /* Header */
403 "FibRand", /* @tp_name@ */
404 sizeof(grand_pyobj
), /* @tp_basicsize@ */
405 0, /* @tp_itemsize@ */
407 grand_pydealloc
, /* @tp_dealloc@ */
409 0, /* @tp_getattr@ */
410 0, /* @tp_setattr@ */
411 0, /* @tp_compare@ */
413 0, /* @tp_as_number@ */
414 0, /* @tp_as_sequence@ */
415 0, /* @tp_as_mapping@ */
419 0, /* @tp_getattro@ */
420 0, /* @tp_setattro@ */
421 0, /* @tp_as_buffer@ */
422 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
426 "FibRand([seed = 0]): Fibonacci generator.",
428 0, /* @tp_traverse@ */
430 0, /* @tp_richcompare@ */
431 0, /* @tp_weaklistoffset@ */
433 0, /* @tp_iternext@ */
434 0, /* @tp_methods@ */
435 0, /* @tp_members@ */
439 0, /* @tp_descr_get@ */
440 0, /* @tp_descr_set@ */
441 0, /* @tp_dictoffset@ */
443 PyType_GenericAlloc
, /* @tp_alloc@ */
444 fibrand_pynew
, /* @tp_new@ */
449 /*----- True random generator ---------------------------------------------*/
451 static PyObject
*trmeth_gate(PyObject
*me
, PyObject
*arg
)
453 grand
*r
= GRAND_R(me
);
454 if (!PyArg_ParseTuple(arg
, ":gate")) return (0);
455 r
->ops
->misc(r
, RAND_GATE
);
459 static PyObject
*trmeth_stretch(PyObject
*me
, PyObject
*arg
)
461 grand
*r
= GRAND_R(me
);
462 if (!PyArg_ParseTuple(arg
, ":stretch")) return (0);
463 r
->ops
->misc(r
, RAND_STRETCH
);
467 static PyObject
*trmeth_add(PyObject
*me
, PyObject
*arg
)
469 grand
*r
= GRAND_R(me
);
470 char *p
; Py_ssize_t n
; unsigned goodbits
;
471 if (!PyArg_ParseTuple(arg
, "s#O&:add", &p
, &n
, convuint
, &goodbits
))
473 r
->ops
->misc(r
, RAND_ADD
, p
, (size_t)n
, goodbits
);
477 static PyObject
*trmeth_key(PyObject
*me
, PyObject
*arg
)
479 grand
*r
= GRAND_R(me
);
480 char *p
; Py_ssize_t n
;
481 if (!PyArg_ParseTuple(arg
, "s#:key", &p
, &n
)) return (0);
482 r
->ops
->misc(r
, RAND_KEY
, p
, (size_t)n
);
486 static PyObject
*trmeth_seed(PyObject
*me
, PyObject
*arg
)
488 grand
*r
= GRAND_R(me
);
490 if (!PyArg_ParseTuple(arg
, "O&:seed", convuint
, &u
)) return (0);
491 if (u
> RAND_IBITS
) VALERR("pointlessly large");
492 r
->ops
->misc(r
, RAND_SEED
, u
);
498 static PyObject
*trmeth_timer(PyObject
*me
, PyObject
*arg
)
500 grand
*r
= GRAND_R(me
);
501 if (!PyArg_ParseTuple(arg
, ":timer")) return (0);
502 r
->ops
->misc(r
, RAND_TIMER
);
506 static PyObject
*truerand_pynew(PyTypeObject
*ty
,
507 PyObject
*arg
, PyObject
*kw
)
509 static const char *const kwlist
[] = { 0 };
512 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, ":new", KWLIST
)) goto end
;
514 r
->ops
->misc(r
, RAND_NOISESRC
, &noise_source
);
515 r
->ops
->misc(r
, RAND_SEED
, 160);
516 rc
= grand_dopywrap(ty
, r
, f_freeme
);
521 static PyMethodDef truerand_pymethods
[] = {
522 #define METHNAME(name) trmeth_##name
523 METH (gate
, "R.gate()")
524 METH (stretch
, "R.stretch()")
525 METH (key
, "R.key(BYTES)")
526 METH (seed
, "R.seed(NBITS)")
527 METH (add
, "R.add(BYTES, GOODBITS")
528 METH (timer
, "R.timer()")
533 static PyObject
*trget_goodbits(PyObject
*me
, void *hunoz
)
535 grand
*r
= GRAND_R(me
);
536 return (PyInt_FromLong(r
->ops
->misc(r
, RAND_GOODBITS
)));
539 static PyGetSetDef truerand_pygetset
[] = {
540 #define GETSETNAME(op, name) tr##op##_##name
541 GET (goodbits
, "R.goodbits -> good bits of entropy remaining")
546 static PyTypeObject truerand_pytype_skel
= {
547 PyObject_HEAD_INIT(0) 0, /* Header */
548 "TrueRand", /* @tp_name@ */
549 sizeof(grand_pyobj
), /* @tp_basicsize@ */
550 0, /* @tp_itemsize@ */
552 grand_pydealloc
, /* @tp_dealloc@ */
554 0, /* @tp_getattr@ */
555 0, /* @tp_setattr@ */
556 0, /* @tp_compare@ */
558 0, /* @tp_as_number@ */
559 0, /* @tp_as_sequence@ */
560 0, /* @tp_as_mapping@ */
564 0, /* @tp_getattro@ */
565 0, /* @tp_setattro@ */
566 0, /* @tp_as_buffer@ */
567 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
571 "TrueRand(): true random number source.",
573 0, /* @tp_traverse@ */
575 0, /* @tp_richcompare@ */
576 0, /* @tp_weaklistoffset@ */
578 0, /* @tp_iternext@ */
579 truerand_pymethods
, /* @tp_methods@ */
580 0, /* @tp_members@ */
581 truerand_pygetset
, /* @tp_getset@ */
584 0, /* @tp_descr_get@ */
585 0, /* @tp_descr_set@ */
586 0, /* @tp_dictoffset@ */
588 PyType_GenericAlloc
, /* @tp_alloc@ */
589 truerand_pynew
, /* @tp_new@ */
594 /*----- Generators from symmetric encryption algorithms -------------------*/
596 static PyTypeObject
*gccrand_pytype
, *gcrand_pytype
, *gclatinrand_pytype
;
598 typedef grand
*gcrand_func(const void *, size_t sz
);
599 typedef grand
*gcirand_func(const void *, size_t sz
, uint32
);
600 typedef grand
*gcnrand_func(const void *, size_t sz
, const void *);
601 typedef grand
*gcshakerand_func(const void *, size_t,
602 const void *, size_t,
603 const void *, size_t);
604 typedef grand
*gcshafuncrand_func(const void *, size_t,
605 const void *, size_t);
606 typedef grand
*gckmacrand_func(const void *, size_t, const void *, size_t);
607 typedef struct gccrand_info
{
615 #define RNGF_MASK 255u
625 typedef struct gccrand_pyobj
{
627 const gccrand_info
*info
;
629 #define GCCRAND_INFO(o) (((gccrand_pyobj *)(o))->info)
631 #define GCCRAND_DEF(name, ksz, func, f, nsz) \
632 static const gccrand_info func##_info = \
633 { name, ksz, f, nsz, (gcrand_func *)func };
636 static const gccrand_info
*const gcrandtab
[] = {
637 #define GCCRAND_ENTRY(name, ksz, func, f, nsz) &func##_info,
642 static PyObject
*gcrand_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
644 const gccrand_info
*info
= GCCRAND_INFO(ty
);
645 static const char *const kwlist
[] = { "key", 0 };
649 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", KWLIST
, &k
, &n
))
651 if (keysz(n
, info
->keysz
) != n
) VALERR("bad key length");
652 return (grand_dopywrap(ty
, info
->func(k
, n
), f_freeme
));
657 static PyObject
*gcirand_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
659 const gccrand_info
*info
= GCCRAND_INFO(ty
);
661 static const char *const kwlist
[] = { "key", "i", 0 };
665 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#O&:new", KWLIST
,
666 &k
, &n
, convu32
, &i
))
668 if (keysz(n
, info
->keysz
) != n
) VALERR("bad key length");
669 return (grand_dopywrap(ty
,
670 ((gcirand_func
*)info
->func
)(k
, n
, i
),
676 static PyObject
*gcnrand_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
678 const gccrand_info
*info
= GCCRAND_INFO(ty
);
679 static const char *const kwlist
[] = { "key", "nonce", 0 };
683 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#s#:new", KWLIST
,
686 if (keysz(ksz
, info
->keysz
) != ksz
) VALERR("bad key length");
687 if (nsz
!= info
->noncesz
) VALERR("bad nonce length");
688 return (grand_dopywrap(ty
,
689 ((gcnrand_func
*)info
->func
)(k
, ksz
, n
),
695 static PyObject
*gcshakyrand_pynew(PyTypeObject
*ty
,
696 PyObject
*arg
, PyObject
*kw
)
698 const gccrand_info
*info
= GCCRAND_INFO(ty
);
700 *const kwlist_shake
[] = { "key", "func", "perso", 0 },
701 *const kwlist_func
[] = { "key", "perso", 0 };
702 char *k
, *f
= 0, *p
= 0;
703 Py_ssize_t ksz
, fsz
= 0, psz
= 0;
705 if ((info
->f
&RNGF_MASK
) == RNG_SHAKE
706 ?
!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#|s#s#:new",
707 (/*unconst*/ char **)kwlist_shake
,
708 &k
, &ksz
, &f
, &fsz
, &p
, &psz
)
709 : !PyArg_ParseTupleAndKeywords(arg
, kw
, "s#|s#:new",
710 (/*unconst*/ char **)kwlist_func
,
713 if (keysz(ksz
, info
->keysz
) != ksz
) VALERR("bad key length");
714 return (grand_dopywrap(ty
,
715 (info
->f
&RNGF_MASK
) == RNG_SHAKE
716 ?
((gcshakerand_func
*)info
->func
)(f
, fsz
,
719 : ((gcshafuncrand_func
*)info
->func
)(p
, psz
,
726 static PyObject
*gccrand_pywrap(const gccrand_info
*info
)
728 gccrand_pyobj
*g
= newtype(gccrand_pytype
, 0, info
->name
);
730 g
->ty
.ht_type
.tp_basicsize
= sizeof(grand_pyobj
);
731 switch (info
->f
&RNGF_MASK
) {
732 case RNG_LATIN
: g
->ty
.ht_type
.tp_base
= gclatinrand_pytype
; break;
733 default: g
->ty
.ht_type
.tp_base
= gcrand_pytype
; break;
735 Py_INCREF(g
->ty
.ht_type
.tp_base
);
736 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
737 Py_TPFLAGS_BASETYPE
|
738 Py_TPFLAGS_HEAPTYPE
);
739 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
740 g
->ty
.ht_type
.tp_free
= 0;
741 switch (info
->f
&RNGF_MASK
) {
742 case RNG_LATIN
: g
->ty
.ht_type
.tp_new
= gcnrand_pynew
; break;
743 case RNG_SEAL
: g
->ty
.ht_type
.tp_new
= gcirand_pynew
; break;
744 case RNG_SHAKE
: case RNG_KMAC
:
745 g
->ty
.ht_type
.tp_new
= gcshakyrand_pynew
; break;
746 default: g
->ty
.ht_type
.tp_new
= gcrand_pynew
; break;
748 typeready(&g
->ty
.ht_type
);
749 return ((PyObject
*)g
);
752 static PyObject
*gccrget_name(PyObject
*me
, void *hunoz
)
753 { return (PyString_FromString(GCCRAND_INFO(me
)->name
)); }
754 static PyObject
*gccrget_keysz(PyObject
*me
, void *hunoz
)
755 { return (keysz_pywrap(GCCRAND_INFO(me
)->keysz
)); }
757 static PyObject
*gclrmeth_tell(PyObject
*me
, PyObject
*arg
)
759 grand
*r
= GRAND_R(me
);
763 if (!PyArg_ParseTuple(arg
, ":tell")) return (0);
764 r
->ops
->misc(r
, SALSA20_TELLU64
, &off
);
769 static PyObject
*gclrmeth_seek(PyObject
*me
, PyObject
*arg
)
771 grand
*r
= GRAND_R(me
);
774 if (!PyArg_ParseTuple(arg
, "O&:seek", convk64
, &off
)) return (0);
775 r
->ops
->misc(r
, SALSA20_SEEKU64
, off
);
779 static PyGetSetDef gccrand_pygetset
[] = {
780 #define GETSETNAME(op, name) gccr##op##_##name
781 GET (keysz
, "CR.keysz -> acceptable key sizes")
782 GET (name
, "CR.name -> name of this kind of generator")
787 static PyMethodDef gclatinrand_pymethods
[] = {
788 #define METHNAME(name) gclrmeth_##name
789 METH (tell
, "R.tell() -> OFF")
790 METH (seek
, "R.seek(OFF)")
795 static PyTypeObject gccrand_pytype_skel
= {
796 PyObject_HEAD_INIT(0) 0, /* Header */
797 "GCCRand", /* @tp_name@ */
798 sizeof(gccrand_pyobj
), /* @tp_basicsize@ */
799 0, /* @tp_itemsize@ */
801 0, /* @tp_dealloc@ */
803 0, /* @tp_getattr@ */
804 0, /* @tp_setattr@ */
805 0, /* @tp_compare@ */
807 0, /* @tp_as_number@ */
808 0, /* @tp_as_sequence@ */
809 0, /* @tp_as_mapping@ */
813 0, /* @tp_getattro@ */
814 0, /* @tp_setattro@ */
815 0, /* @tp_as_buffer@ */
816 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
820 "Metaclass for symmetric crypto-based generators.",
822 0, /* @tp_traverse@ */
824 0, /* @tp_richcompare@ */
825 0, /* @tp_weaklistoffset@ */
827 0, /* @tp_iternext@ */
828 0, /* @tp_methods@ */
829 0, /* @tp_members@ */
830 gccrand_pygetset
, /* @tp_getset@ */
833 0, /* @tp_descr_get@ */
834 0, /* @tp_descr_set@ */
835 0, /* @tp_dictoffset@ */
837 PyType_GenericAlloc
, /* @tp_alloc@ */
838 abstract_pynew
, /* @tp_new@ */
843 static PyTypeObject gcrand_pytype_skel
= {
844 PyObject_HEAD_INIT(0) 0, /* Header */
845 "GCRand", /* @tp_name@ */
846 sizeof(grand_pyobj
), /* @tp_basicsize@ */
847 0, /* @tp_itemsize@ */
849 grand_pydealloc
, /* @tp_dealloc@ */
851 0, /* @tp_getattr@ */
852 0, /* @tp_setattr@ */
853 0, /* @tp_compare@ */
855 0, /* @tp_as_number@ */
856 0, /* @tp_as_sequence@ */
857 0, /* @tp_as_mapping@ */
861 0, /* @tp_getattro@ */
862 0, /* @tp_setattro@ */
863 0, /* @tp_as_buffer@ */
864 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
868 "Abstract base class for symmetric crypto-based generators.",
870 0, /* @tp_traverse@ */
872 0, /* @tp_richcompare@ */
873 0, /* @tp_weaklistoffset@ */
875 0, /* @tp_iternext@ */
876 0, /* @tp_methods@ */
877 0, /* @tp_members@ */
881 0, /* @tp_descr_get@ */
882 0, /* @tp_descr_set@ */
883 0, /* @tp_dictoffset@ */
885 PyType_GenericAlloc
, /* @tp_alloc@ */
886 abstract_pynew
, /* @tp_new@ */
891 static PyTypeObject gclatinrand_pytype_skel
= {
892 PyObject_HEAD_INIT(0) 0, /* Header */
893 "GCLatinRand", /* @tp_name@ */
894 sizeof(grand_pyobj
), /* @tp_basicsize@ */
895 0, /* @tp_itemsize@ */
897 grand_pydealloc
, /* @tp_dealloc@ */
899 0, /* @tp_getattr@ */
900 0, /* @tp_setattr@ */
901 0, /* @tp_compare@ */
903 0, /* @tp_as_number@ */
904 0, /* @tp_as_sequence@ */
905 0, /* @tp_as_mapping@ */
909 0, /* @tp_getattro@ */
910 0, /* @tp_setattro@ */
911 0, /* @tp_as_buffer@ */
912 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
916 "Abstract base class for symmetric crypto-based generators.",
918 0, /* @tp_traverse@ */
920 0, /* @tp_richcompare@ */
921 0, /* @tp_weaklistoffset@ */
923 0, /* @tp_iternext@ */
924 gclatinrand_pymethods
, /* @tp_methods@ */
925 0, /* @tp_members@ */
929 0, /* @tp_descr_get@ */
930 0, /* @tp_descr_set@ */
931 0, /* @tp_dictoffset@ */
933 PyType_GenericAlloc
, /* @tp_alloc@ */
934 abstract_pynew
, /* @tp_new@ */
939 /*----- SSL and TLS generators --------------------------------------------*/
941 static PyObject
*sslprf_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
945 const gchash
*hco
= &md5
, *hci
= &sha
;
947 static const char *const kwlist
[] = { "key", "seed", "ohash", "ihash", 0 };
949 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#s#|O&O&:new", KWLIST
,
951 convgchash
, &hco
, convgchash
, &hci
))
953 rc
= grand_dopywrap(ty
, sslprf_rand(hco
, hci
, k
, ksz
, s
, ssz
), f_freeme
);
958 static PyObject
*tlsdx_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
962 const gcmac
*mc
= &sha_hmac
;
964 static const char *const kwlist
[] = { "key", "seed", "mac", 0 };
966 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#s#|O&:new", KWLIST
,
970 rc
= grand_dopywrap(ty
, tlsdx_rand(mc
, k
, ksz
, s
, ssz
), f_freeme
);
975 static PyObject
*tlsprf_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
979 const gcmac
*mcl
= &md5_hmac
, *mcr
= &sha_hmac
;
981 static const char *const kwlist
[] = { "key", "seed", "lmac", "rmac", 0 };
983 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#s#|O&O&:new", KWLIST
,
985 convgcmac
, &mcl
, convgcmac
, &mcr
))
987 rc
= grand_dopywrap(ty
, tlsprf_rand(mcl
, mcr
, k
, ksz
, s
, ssz
), f_freeme
);
992 static PyTypeObject sslprf_pytype_skel
= {
993 PyObject_HEAD_INIT(0) 0, /* Header */
994 "SSLRand", /* @tp_name@ */
995 sizeof(grand_pyobj
), /* @tp_basicsize@ */
996 0, /* @tp_itemsize@ */
998 grand_pydealloc
, /* @tp_dealloc@ */
1000 0, /* @tp_getattr@ */
1001 0, /* @tp_setattr@ */
1002 0, /* @tp_compare@ */
1004 0, /* @tp_as_number@ */
1005 0, /* @tp_as_sequence@ */
1006 0, /* @tp_as_mapping@ */
1010 0, /* @tp_getattro@ */
1011 0, /* @tp_setattro@ */
1012 0, /* @tp_as_buffer@ */
1013 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1014 Py_TPFLAGS_BASETYPE
,
1017 "SSLRand(KEY, SEED, [ohash = md5], [ihash = sha]):\n"
1018 " RNG for SSL master secret.",
1020 0, /* @tp_traverse@ */
1022 0, /* @tp_richcompare@ */
1023 0, /* @tp_weaklistoffset@ */
1025 0, /* @tp_iternext@ */
1026 0, /* @tp_methods@ */
1027 0, /* @tp_members@ */
1028 0, /* @tp_getset@ */
1031 0, /* @tp_descr_get@ */
1032 0, /* @tp_descr_set@ */
1033 0, /* @tp_dictoffset@ */
1035 PyType_GenericAlloc
, /* @tp_alloc@ */
1036 sslprf_pynew
, /* @tp_new@ */
1041 static PyTypeObject tlsdx_pytype_skel
= {
1042 PyObject_HEAD_INIT(0) 0, /* Header */
1043 "TLSDataExpansion", /* @tp_name@ */
1044 sizeof(grand_pyobj
), /* @tp_basicsize@ */
1045 0, /* @tp_itemsize@ */
1047 grand_pydealloc
, /* @tp_dealloc@ */
1049 0, /* @tp_getattr@ */
1050 0, /* @tp_setattr@ */
1051 0, /* @tp_compare@ */
1053 0, /* @tp_as_number@ */
1054 0, /* @tp_as_sequence@ */
1055 0, /* @tp_as_mapping@ */
1059 0, /* @tp_getattro@ */
1060 0, /* @tp_setattro@ */
1061 0, /* @tp_as_buffer@ */
1062 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1063 Py_TPFLAGS_BASETYPE
,
1066 "TLSDataExpansion(KEY, SEED, [mac = sha_hmac]):\n"
1067 " TLS data expansion function.",
1069 0, /* @tp_traverse@ */
1071 0, /* @tp_richcompare@ */
1072 0, /* @tp_weaklistoffset@ */
1074 0, /* @tp_iternext@ */
1075 0, /* @tp_methods@ */
1076 0, /* @tp_members@ */
1077 0, /* @tp_getset@ */
1080 0, /* @tp_descr_get@ */
1081 0, /* @tp_descr_set@ */
1082 0, /* @tp_dictoffset@ */
1084 PyType_GenericAlloc
, /* @tp_alloc@ */
1085 tlsdx_pynew
, /* @tp_new@ */
1090 static PyTypeObject tlsprf_pytype_skel
= {
1091 PyObject_HEAD_INIT(0) 0, /* Header */
1092 "TLSPRF", /* @tp_name@ */
1093 sizeof(grand_pyobj
), /* @tp_basicsize@ */
1094 0, /* @tp_itemsize@ */
1096 grand_pydealloc
, /* @tp_dealloc@ */
1098 0, /* @tp_getattr@ */
1099 0, /* @tp_setattr@ */
1100 0, /* @tp_compare@ */
1102 0, /* @tp_as_number@ */
1103 0, /* @tp_as_sequence@ */
1104 0, /* @tp_as_mapping@ */
1108 0, /* @tp_getattro@ */
1109 0, /* @tp_setattro@ */
1110 0, /* @tp_as_buffer@ */
1111 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1112 Py_TPFLAGS_BASETYPE
,
1115 "TLSPRF(KEY, SEED, [lmac = md5_hmac], [rmac = sha_hmac]):\n"
1116 " TLS pseudorandom function.",
1118 0, /* @tp_traverse@ */
1120 0, /* @tp_richcompare@ */
1121 0, /* @tp_weaklistoffset@ */
1123 0, /* @tp_iternext@ */
1124 0, /* @tp_methods@ */
1125 0, /* @tp_members@ */
1126 0, /* @tp_getset@ */
1129 0, /* @tp_descr_get@ */
1130 0, /* @tp_descr_set@ */
1131 0, /* @tp_dictoffset@ */
1133 PyType_GenericAlloc
, /* @tp_alloc@ */
1134 tlsprf_pynew
, /* @tp_new@ */
1139 /*----- DSA generator -----------------------------------------------------*/
1141 static PyObject
*dsarand_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1146 static const char *const kwlist
[] = { "seed", 0 };
1148 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", KWLIST
, &p
, &sz
))
1150 rc
= grand_dopywrap(ty
, dsarand_create(p
, sz
), f_freeme
);
1155 static PyObject
*drget_seed(PyObject
*me
, void *hunoz
)
1157 grand
*r
= GRAND_R(me
);
1158 int n
= r
->ops
->misc(r
, DSARAND_SEEDSZ
);
1159 PyObject
*rc
= bytestring_pywrap(0, n
);
1160 r
->ops
->misc(r
, DSARAND_GETSEED
, PyString_AS_STRING(rc
));
1164 static PyGetSetDef dsarand_pygetset
[] = {
1165 #define GETSETNAME(op, name) dr##op##_##name
1166 GET (seed
, "R.seed -> current generator seed")
1171 static PyTypeObject dsarand_pytype_skel
= {
1172 PyObject_HEAD_INIT(0) 0, /* Header */
1173 "DSARand", /* @tp_name@ */
1174 sizeof(grand_pyobj
), /* @tp_basicsize@ */
1175 0, /* @tp_itemsize@ */
1177 grand_pydealloc
, /* @tp_dealloc@ */
1179 0, /* @tp_getattr@ */
1180 0, /* @tp_setattr@ */
1181 0, /* @tp_compare@ */
1183 0, /* @tp_as_number@ */
1184 0, /* @tp_as_sequence@ */
1185 0, /* @tp_as_mapping@ */
1189 0, /* @tp_getattro@ */
1190 0, /* @tp_setattro@ */
1191 0, /* @tp_as_buffer@ */
1192 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1193 Py_TPFLAGS_BASETYPE
,
1196 "DSARand(SEED): pseudorandom number generator for DSA parameters.",
1198 0, /* @tp_traverse@ */
1200 0, /* @tp_richcompare@ */
1201 0, /* @tp_weaklistoffset@ */
1203 0, /* @tp_iternext@ */
1204 0, /* @tp_methods@ */
1205 0, /* @tp_members@ */
1206 dsarand_pygetset
, /* @tp_getset@ */
1209 0, /* @tp_descr_get@ */
1210 0, /* @tp_descr_set@ */
1211 0, /* @tp_dictoffset@ */
1213 PyType_GenericAlloc
, /* @tp_alloc@ */
1214 dsarand_pynew
, /* @tp_new@ */
1219 /*----- Blum-Blum-Shub generator ------------------------------------------*/
1221 static PyObject
*bbs_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1223 mp
*n
= 0, *x
= MP_TWO
;
1225 static const char *const kwlist
[] = { "n", "x", 0 };
1227 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&|O&:new", KWLIST
,
1228 convmp
, &n
, convmp
, &x
))
1230 rc
= grand_dopywrap(ty
, bbs_rand(n
, x
), f_freeme
);
1237 static PyObject
*bbsmeth_step(PyObject
*me
, PyObject
*arg
)
1239 grand
*r
= GRAND_R(me
); if (!PyArg_ParseTuple(arg
, ":step")) return (0);
1240 r
->ops
->misc(r
, BBS_STEP
); RETURN_ME
;
1243 static PyObject
*bbsmeth_bits(PyObject
*me
, PyObject
*arg
)
1245 grand
*r
= GRAND_R(me
); unsigned n
; uint32 w
;
1246 if (!PyArg_ParseTuple(arg
, "O&:bits", convuint
, &n
)) goto end
;
1247 if (n
> 32) VALERR("can't get more than 32 bits");
1248 r
->ops
->misc(r
, BBS_BITS
, n
, &w
); return (getulong(w
));
1253 static PyObject
*bbsmeth_wrap(PyObject
*me
, PyObject
*arg
)
1255 grand
*r
= GRAND_R(me
); if (!PyArg_ParseTuple(arg
, ":wrap")) return (0);
1256 r
->ops
->misc(r
, BBS_WRAP
); RETURN_ME
;
1259 static PyObject
*bbsget_n(PyObject
*me
, void *hunoz
)
1261 mp
*x
= MP_NEW
; grand
*r
= GRAND_R(me
);
1262 r
->ops
->misc(r
, BBS_MOD
, &x
); return (mp_pywrap(x
));
1265 static PyObject
*bbsget_x(PyObject
*me
, void *hunoz
)
1267 mp
*x
= MP_NEW
; grand
*r
= GRAND_R(me
);
1268 r
->ops
->misc(r
, BBS_STATE
, &x
); return (mp_pywrap(x
));
1271 static int bbsset_x(PyObject
*me
, PyObject
*val
, void *hunoz
)
1273 mp
*x
= 0; grand
*r
= GRAND_R(me
); int rc
= -1; if (!val
) NIERR("__del__");
1274 if ((x
= getmp(val
)) == 0) goto end
;
1275 r
->ops
->misc(r
, BBS_SET
, x
); rc
= 0;
1276 end
: mp_drop(x
); return (rc
);
1279 static PyObject
*bbsget_stepsz(PyObject
*me
, void *hunoz
)
1281 grand
*r
= GRAND_R(me
);
1282 return (PyInt_FromLong(r
->ops
->misc(r
, BBS_STEPSZ
)));
1285 static PyMethodDef bbs_pymethods
[] = {
1286 #define METHNAME(name) bbsmeth_##name
1287 METH (step
, "R.step(): steps the generator (not useful)")
1288 METH (bits
, "R.bits(N) -> W: returns N bits (<= 32) from the generator")
1289 METH (wrap
, "R.wrap(): flushes unused bits in internal buffer")
1294 static PyGetSetDef bbs_pygetset
[] = {
1295 #define GETSETNAME(op, name) bbs##op##_##name
1296 GET (n
, "R.n -> Blum modulus")
1297 GETSET(x
, "R.x -> current seed value")
1298 GET (stepsz
, "R.stepsz -> number of bits generated per step")
1303 static PyTypeObject bbs_pytype_skel
= {
1304 PyObject_HEAD_INIT(0) 0, /* Header */
1305 "BlumBlumShub", /* @tp_name@ */
1306 sizeof(grand_pyobj
), /* @tp_basicsize@ */
1307 0, /* @tp_itemsize@ */
1309 grand_pydealloc
, /* @tp_dealloc@ */
1311 0, /* @tp_getattr@ */
1312 0, /* @tp_setattr@ */
1313 0, /* @tp_compare@ */
1315 0, /* @tp_as_number@ */
1316 0, /* @tp_as_sequence@ */
1317 0, /* @tp_as_mapping@ */
1321 0, /* @tp_getattro@ */
1322 0, /* @tp_setattro@ */
1323 0, /* @tp_as_buffer@ */
1324 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1325 Py_TPFLAGS_BASETYPE
,
1328 "BlumBlumShub(N, [x = 2]): Blum-Blum-Shub pseudorandom number generator.",
1330 0, /* @tp_traverse@ */
1332 0, /* @tp_richcompare@ */
1333 0, /* @tp_weaklistoffset@ */
1335 0, /* @tp_iternext@ */
1336 bbs_pymethods
, /* @tp_methods@ */
1337 0, /* @tp_members@ */
1338 bbs_pygetset
, /* @tp_getset@ */
1341 0, /* @tp_descr_get@ */
1342 0, /* @tp_descr_set@ */
1343 0, /* @tp_dictoffset@ */
1345 PyType_GenericAlloc
, /* @tp_alloc@ */
1346 bbs_pynew
, /* @tp_new@ */
1351 typedef struct bbspriv_pyobj
{
1356 #define BBSPRIV_BP(o) (&((bbspriv_pyobj *)(o))->bp)
1358 static PyObject
*bbspriv_pynew(PyTypeObject
*ty
,
1359 PyObject
*arg
, PyObject
*kw
)
1361 mp
*p
= 0, *q
= 0, *n
= 0, *x
= MP_TWO
;
1362 bbspriv_pyobj
*rc
= 0;
1363 static const char *const kwlist
[] = { "n", "p", "q", "seed", 0 };
1365 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&O&O&O&:new", KWLIST
,
1366 convmp
, &n
, convmp
, &p
, convmp
, &q
,
1369 if (!n
+ !p
+ !q
> 1) VALERR("must specify at least two of n, p, q");
1370 if (!n
) n
= mp_mul(MP_NEW
, p
, q
);
1371 else if (!p
) mp_div(&p
, 0, n
, q
);
1372 else if (!q
) mp_div(&q
, 0, n
, p
);
1373 rc
= (bbspriv_pyobj
*)ty
->tp_alloc(ty
, 0);
1374 rc
->gr
.r
= bbs_rand(n
, x
);
1375 rc
->gr
.f
= f_freeme
;
1376 rc
->bp
.p
= MP_COPY(p
);
1377 rc
->bp
.q
= MP_COPY(q
);
1378 rc
->bp
.n
= MP_COPY(n
);
1380 mp_drop(p
); mp_drop(q
); mp_drop(n
); mp_drop(x
);
1381 return ((PyObject
*)rc
);
1384 static PyObject
*meth__BBSPriv_generate(PyObject
*me
,
1385 PyObject
*arg
, PyObject
*kw
)
1387 bbs_priv bp
= { 0 };
1389 struct excinfo exc
= EXCINFO_INIT
;
1390 pypgev evt
= { { 0 } };
1391 unsigned nbits
, n
= 0;
1392 grand
*r
= &rand_global
;
1393 static const char *const kwlist
[] =
1394 { "class", "nbits", "event", "rng", "nsteps", "seed", 0 };
1395 bbspriv_pyobj
*rc
= 0;
1398 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "OO&|O&O&O&O&:generate", KWLIST
,
1399 &me
, convuint
, &nbits
, convpgev
, &evt
,
1400 convgrand
, &r
, convuint
, &n
, convmp
, &x
))
1402 if (bbs_gen(&bp
, nbits
, r
, n
, evt
.ev
.proc
, evt
.ev
.ctx
))
1404 rc
= PyObject_New(bbspriv_pyobj
, bbspriv_pytype
);
1405 rc
->gr
.r
= bbs_rand(bp
.n
, x
);
1406 rc
->gr
.f
= f_freeme
;
1407 rc
->bp
.p
= MP_COPY(bp
.p
);
1408 rc
->bp
.q
= MP_COPY(bp
.q
);
1409 rc
->bp
.n
= MP_COPY(bp
.n
);
1411 mp_drop(bp
.p
); mp_drop(bp
.q
); mp_drop(bp
.n
); mp_drop(x
);
1412 return ((PyObject
*)rc
);
1415 static void bbspriv_pydealloc(PyObject
*me
)
1417 bbs_priv
*bp
= BBSPRIV_BP(me
);
1421 grand_pydealloc(me
);
1424 static PyObject
*bpmeth_ff(PyObject
*me
, PyObject
*arg
)
1426 mp
*n
= 0; grand
*r
= GRAND_R(me
); bbs_priv
*bp
= BBSPRIV_BP(me
);
1427 if (!PyArg_ParseTuple(arg
, "O&:ff", convmp
, &n
)) return (0);
1428 r
->ops
->misc(r
, BBS_FF
, bp
, n
); RETURN_ME
;
1431 static PyObject
*bpmeth_rew(PyObject
*me
, PyObject
*arg
)
1433 mp
*n
= 0; grand
*r
= GRAND_R(me
); bbs_priv
*bp
= BBSPRIV_BP(me
);
1434 if (!PyArg_ParseTuple(arg
, "O&:rew", convmp
, &n
)) return (0);
1435 r
->ops
->misc(r
, BBS_REW
, bp
, n
); RETURN_ME
;
1438 static PyObject
*bpget_n(PyObject
*me
, void *hunoz
)
1439 { return (mp_pywrap(MP_COPY(BBSPRIV_BP(me
)->n
))); }
1441 static PyObject
*bpget_p(PyObject
*me
, void *hunoz
)
1442 { return (mp_pywrap(MP_COPY(BBSPRIV_BP(me
)->p
))); }
1444 static PyObject
*bpget_q(PyObject
*me
, void *hunoz
)
1445 { return (mp_pywrap(MP_COPY(BBSPRIV_BP(me
)->q
))); }
1447 static PyMethodDef bbspriv_pymethods
[] = {
1448 #define METHNAME(name) bpmeth_##name
1449 METH (ff
, "R.ff(N): fast-forward N places")
1450 METH (rew
, "R.rew(N): rewind N places")
1455 static PyGetSetDef bbspriv_pygetset
[] = {
1456 #define GETSETNAME(op, name) bp##op##_##name
1457 GET (n
, "R.n -> Blum modulus")
1458 GET (p
, "R.p -> one of the factors of the modulus")
1459 GET (q
, "R.q -> one of the factors of the modulus")
1464 static PyTypeObject bbspriv_pytype_skel
= {
1465 PyObject_HEAD_INIT(0) 0, /* Header */
1466 "BBSPriv", /* @tp_name@ */
1467 sizeof(bbspriv_pyobj
), /* @tp_basicsize@ */
1468 0, /* @tp_itemsize@ */
1470 bbspriv_pydealloc
, /* @tp_dealloc@ */
1472 0, /* @tp_getattr@ */
1473 0, /* @tp_setattr@ */
1474 0, /* @tp_compare@ */
1476 0, /* @tp_as_number@ */
1477 0, /* @tp_as_sequence@ */
1478 0, /* @tp_as_mapping@ */
1482 0, /* @tp_getattro@ */
1483 0, /* @tp_setattro@ */
1484 0, /* @tp_as_buffer@ */
1485 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1486 Py_TPFLAGS_BASETYPE
,
1489 "BBSPriv(..., [seed = 2]): Blum-Blum-Shub, with private key.\n"
1490 " Keywords: n, p, q; must provide at least two",
1492 0, /* @tp_traverse@ */
1494 0, /* @tp_richcompare@ */
1495 0, /* @tp_weaklistoffset@ */
1497 0, /* @tp_iternext@ */
1498 bbspriv_pymethods
, /* @tp_methods@ */
1499 0, /* @tp_members@ */
1500 bbspriv_pygetset
, /* @tp_getset@ */
1503 0, /* @tp_descr_get@ */
1504 0, /* @tp_descr_set@ */
1505 0, /* @tp_dictoffset@ */
1507 PyType_GenericAlloc
, /* @tp_alloc@ */
1508 bbspriv_pynew
, /* @tp_new@ */
1513 /*----- Global stuff ------------------------------------------------------*/
1515 static PyMethodDef methods
[] = {
1516 #define METHNAME(name) meth_##name
1517 KWMETH(_BBSPriv_generate
, "generate(NBITS, [event = pgen_nullev], "
1518 "[rng = rand], [nsteps = 0], [seed = 2]) -> R")
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 -------------------------------------------------*/