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 PyObject
*grmeth_byte(PyObject
*me
, PyObject
*arg
)
74 if (!PyArg_ParseTuple(arg
, ":byte")) return (0);
75 return (PyInt_FromLong(grand_byte(GRAND_R(me
))));
78 static PyObject
*grmeth_word(PyObject
*me
, PyObject
*arg
)
80 if (!PyArg_ParseTuple(arg
, ":word")) return (0);
81 return (getulong(grand_word(GRAND_R(me
))));
84 static PyObject
*grmeth_range(PyObject
*me
, PyObject
*arg
)
90 if (!PyArg_ParseTuple(arg
, "O:range", &m
)) return (0);
92 long mm
= PyInt_AS_LONG(m
);
96 return (PyInt_FromLong(grand_range(GRAND_R(me
), mm
)));
98 if ((x
= getmp(m
)) == 0)
102 y
= mprand_range(MP_NEW
, x
, GRAND_R(me
), 0);
104 return (mp_pywrap(y
));
106 TYERR("range must be nonnegative");
112 static PyObject
*grmeth_mp(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
116 char *kwlist
[] = { "bits", "or", 0 };
118 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&|O&:mp", kwlist
,
119 convszt
, &l
, convmpw
, &o
))
121 return (mp_pywrap(mprand(MP_NEW
, l
, GRAND_R(me
), o
)));
126 static PyObject
*grmeth_block(PyObject
*me
, PyObject
*arg
)
131 if (!PyArg_ParseTuple(arg
, "O&:block", convulong
, &n
)) goto end
;
132 rc
= bytestring_pywrap(0, n
);
133 grand_fill(GRAND_R(me
), PyString_AS_STRING(rc
), n
);
138 static int checkop(grand
*r
, unsigned op
, const char *what
)
140 if (r
->ops
->misc(r
, GRAND_CHECK
, op
))
142 PyErr_Format(PyExc_TypeError
, "operation %s not supported", what
);
146 static PyObject
*grmeth_seedint(PyObject
*me
, PyObject
*arg
)
149 grand
*r
= GRAND_R(me
);
150 if (!PyArg_ParseTuple(arg
, "i:seedint", &i
) ||
151 checkop(r
, GRAND_SEEDINT
, "seedint"))
153 r
->ops
->misc(r
, GRAND_SEEDINT
, i
);
159 static PyObject
*grmeth_seedword(PyObject
*me
, PyObject
*arg
)
162 grand
*r
= GRAND_R(me
);
163 if (!PyArg_ParseTuple(arg
, "O&:seedword", convu32
, &u
) ||
164 checkop(r
, GRAND_SEEDUINT32
, "seedword"))
166 r
->ops
->misc(r
, GRAND_SEEDUINT32
, u
);
172 static PyObject
*grmeth_seedblock(PyObject
*me
, PyObject
*arg
)
176 grand
*r
= GRAND_R(me
);
177 if (!PyArg_ParseTuple(arg
, "s#:seedblock", &p
, &n
) ||
178 checkop(r
, GRAND_SEEDBLOCK
, "seedblock"))
180 r
->ops
->misc(r
, GRAND_SEEDBLOCK
, p
, (size_t)n
);
186 static PyObject
*grmeth_seedmp(PyObject
*me
, PyObject
*arg
)
190 grand
*r
= GRAND_R(me
);
191 if (!PyArg_ParseTuple(arg
, "O:seedmp", &x
) ||
192 checkop(r
, GRAND_SEEDMP
, "seedmp") ||
193 (xx
= getmp(x
)) == 0)
195 r
->ops
->misc(r
, GRAND_SEEDMP
, xx
);
202 static PyObject
*grmeth_seedrand(PyObject
*me
, PyObject
*arg
, PyObject
*kw
)
204 char *kwlist
[] = { "rng", 0 };
205 grand
*r
= GRAND_R(me
);
206 grand
*rr
= &rand_global
;
207 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:seedrand", kwlist
,
209 checkop(r
, GRAND_SEEDRAND
, "seedrand"))
211 r
->ops
->misc(r
, GRAND_SEEDRAND
, rr
);
217 static PyObject
*grmeth_mask(PyObject
*me
, PyObject
*arg
)
219 grand
*r
= GRAND_R(me
);
224 if (!PyArg_ParseTuple(arg
, "s#:mask", &p
, &sz
)) return (0);
225 rc
= bytestring_pywrap(0, sz
);
226 q
= PyString_AS_STRING(rc
);
228 while (sz
--) *q
++ ^= *p
++;
232 static void grand_pydealloc(PyObject
*me
)
234 grand_pyobj
*g
= (grand_pyobj
*)me
;
240 static PyObject
*grget_name(PyObject
*me
, void *hunoz
)
241 { return (PyString_FromString(GRAND_R(me
)->ops
->name
)); }
243 static PyObject
*grget_cryptop(PyObject
*me
, void *hunoz
)
244 { return (getbool(GRAND_R(me
)->ops
->f
& GRAND_CRYPTO
)); }
246 static PyGetSetDef grand_pygetset
[] = {
247 #define GETSETNAME(op, name) gr##op##_##name
248 GET (name
, "R.name -> name of this kind of generator")
249 GET (cryptop
, "R.cryptop -> flag: cryptographically strong?")
254 static PyMethodDef grand_pymethods
[] = {
255 #define METHNAME(name) grmeth_##name
256 METH (byte
, "R.byte() -> BYTE")
257 METH (word
, "R.word() -> WORD")
258 METH (block
, "R.block(N) -> STRING")
259 KWMETH(mp
, "R.mp(bits, or = 0) -> MP")
260 METH (range
, "R.range(MAX) -> INT")
261 METH (mask
, "R.mask(STR) -> STR")
262 METH (seedint
, "R.seedint(I)")
263 METH (seedword
, "R.seedword(I)")
264 METH (seedblock
, "R.seedblock(BYTES)")
265 METH (seedmp
, "R.seedmp(X)")
266 KWMETH(seedrand
, "R.seedrand(RR)")
271 static PyTypeObject grand_pytype_skel
= {
272 PyObject_HEAD_INIT(0) 0, /* Header */
273 "GRand", /* @tp_name@ */
274 sizeof(grand_pyobj
), /* @tp_basicsize@ */
275 0, /* @tp_itemsize@ */
277 grand_pydealloc
, /* @tp_dealloc@ */
279 0, /* @tp_getattr@ */
280 0, /* @tp_setattr@ */
281 0, /* @tp_compare@ */
283 0, /* @tp_as_number@ */
284 0, /* @tp_as_sequence@ */
285 0, /* @tp_as_mapping@ */
289 0, /* @tp_getattro@ */
290 0, /* @tp_setattro@ */
291 0, /* @tp_as_buffer@ */
292 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
296 "Generic random number source.",
298 0, /* @tp_traverse@ */
300 0, /* @tp_richcompare@ */
301 0, /* @tp_weaklistoffset@ */
303 0, /* @tp_iternext@ */
304 grand_pymethods
, /* @tp_methods@ */
305 0, /* @tp_members@ */
306 grand_pygetset
, /* @tp_getset@ */
309 0, /* @tp_descr_get@ */
310 0, /* @tp_descr_set@ */
311 0, /* @tp_dictoffset@ */
313 PyType_GenericAlloc
, /* @tp_alloc@ */
314 abstract_pynew
, /* @tp_new@ */
319 static PyObject
*lcrand_pynew(PyTypeObject
*me
, PyObject
*arg
, PyObject
*kw
)
322 char *kwlist
[] = { "seed", 0 };
323 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:new", kwlist
, convu32
, &n
))
325 return (grand_dopywrap(lcrand_pytype
, lcrand_create(n
), f_freeme
));
328 static PyTypeObject lcrand_pytype_skel
= {
329 PyObject_HEAD_INIT(0) 0, /* Header */
330 "LCRand", /* @tp_name@ */
331 sizeof(grand_pyobj
), /* @tp_basicsize@ */
332 0, /* @tp_itemsize@ */
334 grand_pydealloc
, /* @tp_dealloc@ */
336 0, /* @tp_getattr@ */
337 0, /* @tp_setattr@ */
338 0, /* @tp_compare@ */
340 0, /* @tp_as_number@ */
341 0, /* @tp_as_sequence@ */
342 0, /* @tp_as_mapping@ */
346 0, /* @tp_getattro@ */
347 0, /* @tp_setattro@ */
348 0, /* @tp_as_buffer@ */
349 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
353 "Linear congruential generator.",
355 0, /* @tp_traverse@ */
357 0, /* @tp_richcompare@ */
358 0, /* @tp_weaklistoffset@ */
360 0, /* @tp_iternext@ */
361 0, /* @tp_methods@ */
362 0, /* @tp_members@ */
366 0, /* @tp_descr_get@ */
367 0, /* @tp_descr_set@ */
368 0, /* @tp_dictoffset@ */
370 PyType_GenericAlloc
, /* @tp_alloc@ */
371 lcrand_pynew
, /* @tp_new@ */
376 static PyObject
*fibrand_pynew(PyTypeObject
*me
, PyObject
*arg
, PyObject
*kw
)
379 char *kwlist
[] = { "seed", 0 };
380 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&:new", kwlist
, convu32
, &n
))
382 return (grand_dopywrap(fibrand_pytype
, fibrand_create(n
), f_freeme
));
385 static PyTypeObject fibrand_pytype_skel
= {
386 PyObject_HEAD_INIT(0) 0, /* Header */
387 "FibRand", /* @tp_name@ */
388 sizeof(grand_pyobj
), /* @tp_basicsize@ */
389 0, /* @tp_itemsize@ */
391 grand_pydealloc
, /* @tp_dealloc@ */
393 0, /* @tp_getattr@ */
394 0, /* @tp_setattr@ */
395 0, /* @tp_compare@ */
397 0, /* @tp_as_number@ */
398 0, /* @tp_as_sequence@ */
399 0, /* @tp_as_mapping@ */
403 0, /* @tp_getattro@ */
404 0, /* @tp_setattro@ */
405 0, /* @tp_as_buffer@ */
406 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
410 "Fibonacci generator.",
412 0, /* @tp_traverse@ */
414 0, /* @tp_richcompare@ */
415 0, /* @tp_weaklistoffset@ */
417 0, /* @tp_iternext@ */
418 0, /* @tp_methods@ */
419 0, /* @tp_members@ */
423 0, /* @tp_descr_get@ */
424 0, /* @tp_descr_set@ */
425 0, /* @tp_dictoffset@ */
427 PyType_GenericAlloc
, /* @tp_alloc@ */
428 fibrand_pynew
, /* @tp_new@ */
433 /*----- True random generator ---------------------------------------------*/
435 static PyObject
*trmeth_gate(PyObject
*me
, PyObject
*arg
)
437 grand
*r
= GRAND_R(me
);
438 if (!PyArg_ParseTuple(arg
, ":gate")) return (0);
439 r
->ops
->misc(r
, RAND_GATE
);
443 static PyObject
*trmeth_stretch(PyObject
*me
, PyObject
*arg
)
445 grand
*r
= GRAND_R(me
);
446 if (!PyArg_ParseTuple(arg
, ":stretch")) return (0);
447 r
->ops
->misc(r
, RAND_STRETCH
);
451 static PyObject
*trmeth_add(PyObject
*me
, PyObject
*arg
)
453 grand
*r
= GRAND_R(me
);
454 char *p
; int n
; unsigned goodbits
;
455 if (!PyArg_ParseTuple(arg
, "s#O&:add", &p
, &n
, convuint
, &goodbits
))
457 r
->ops
->misc(r
, RAND_ADD
, p
, (size_t)n
, goodbits
);
461 static PyObject
*trmeth_key(PyObject
*me
, PyObject
*arg
)
463 grand
*r
= GRAND_R(me
);
465 if (!PyArg_ParseTuple(arg
, "s#:key", &p
, &n
)) return (0);
466 r
->ops
->misc(r
, RAND_KEY
, p
, (size_t)n
);
470 static PyObject
*trmeth_seed(PyObject
*me
, PyObject
*arg
)
472 grand
*r
= GRAND_R(me
);
474 if (!PyArg_ParseTuple(arg
, "O&:seed", convuint
, &u
)) return (0);
475 if (u
> RAND_IBITS
) VALERR("pointlessly large");
476 r
->ops
->misc(r
, RAND_SEED
, u
);
482 static PyObject
*trmeth_timer(PyObject
*me
, PyObject
*arg
)
484 grand
*r
= GRAND_R(me
);
485 if (!PyArg_ParseTuple(arg
, ":timer")) return (0);
486 r
->ops
->misc(r
, RAND_TIMER
);
490 static PyObject
*truerand_pynew(PyTypeObject
*ty
,
491 PyObject
*arg
, PyObject
*kw
)
493 char *kwlist
[] = { 0 };
496 if (PyArg_ParseTupleAndKeywords(arg
, kw
, ":new", kwlist
)) goto end
;
498 r
->ops
->misc(r
, RAND_NOISESRC
, &noise_source
);
499 r
->ops
->misc(r
, RAND_SEED
, 160);
500 rc
= grand_dopywrap(ty
, r
, f_freeme
);
505 static PyMethodDef truerand_pymethods
[] = {
506 #define METHNAME(name) trmeth_##name
507 METH (gate
, "R.gate()")
508 METH (stretch
, "R.stretch()")
509 METH (key
, "R.key(BYTES)")
510 METH (seed
, "R.seed(NBITS)")
511 METH (add
, "R.add(BYTES, GOODBITS")
512 METH (timer
, "R.timer()")
517 static PyObject
*trget_goodbits(PyObject
*me
, void *hunoz
)
519 grand
*r
= GRAND_R(me
);
520 return (PyInt_FromLong(r
->ops
->misc(r
, RAND_GOODBITS
)));
523 static PyGetSetDef truerand_pygetset
[] = {
524 #define GETSETNAME(op, name) tr##op##_##name
525 GET (goodbits
, "R.goodbits -> good bits of entropy remaining")
530 static PyTypeObject truerand_pytype_skel
= {
531 PyObject_HEAD_INIT(0) 0, /* Header */
532 "TrueRand", /* @tp_name@ */
533 sizeof(grand_pyobj
), /* @tp_basicsize@ */
534 0, /* @tp_itemsize@ */
536 grand_pydealloc
, /* @tp_dealloc@ */
538 0, /* @tp_getattr@ */
539 0, /* @tp_setattr@ */
540 0, /* @tp_compare@ */
542 0, /* @tp_as_number@ */
543 0, /* @tp_as_sequence@ */
544 0, /* @tp_as_mapping@ */
548 0, /* @tp_getattro@ */
549 0, /* @tp_setattro@ */
550 0, /* @tp_as_buffer@ */
551 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
555 "True random number source.",
557 0, /* @tp_traverse@ */
559 0, /* @tp_richcompare@ */
560 0, /* @tp_weaklistoffset@ */
562 0, /* @tp_iternext@ */
563 truerand_pymethods
, /* @tp_methods@ */
564 0, /* @tp_members@ */
565 truerand_pygetset
, /* @tp_getset@ */
568 0, /* @tp_descr_get@ */
569 0, /* @tp_descr_set@ */
570 0, /* @tp_dictoffset@ */
572 PyType_GenericAlloc
, /* @tp_alloc@ */
573 truerand_pynew
, /* @tp_new@ */
578 /*----- Generators from symmetric encryption algorithms -------------------*/
580 static PyTypeObject
*gccrand_pytype
, *gcrand_pytype
;
582 typedef grand
*gcrand_func(const void *, size_t sz
);
583 typedef grand
*gcirand_func(const void *, size_t sz
, uint32
);
584 typedef grand
*gcnrand_func(const void *, size_t sz
, const void *);
585 typedef struct gccrand_info
{
594 #define RNGF_NONCE 2u
596 typedef struct gccrand_pyobj
{
598 const gccrand_info
*info
;
600 #define GCCRAND_INFO(o) (((gccrand_pyobj *)(o))->info)
602 #define GCCRAND_DEF(name, ksz, func, f, nsz) \
603 static const gccrand_info func##_info = \
604 { name, ksz, f, nsz, (gcrand_func *)func };
607 static const gccrand_info
*const gcrandtab
[] = {
608 #define GCCRAND_ENTRY(name, ksz, func, f, nsz) &func##_info,
613 static PyObject
*gcrand_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
615 const gccrand_info
*info
= GCCRAND_INFO(ty
);
616 static char *kwlist
[] = { "key", 0 };
620 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", kwlist
, &k
, &n
))
622 if (keysz(n
, info
->keysz
) != n
) VALERR("bad key length");
623 return (grand_dopywrap(ty
, info
->func(k
, n
), f_freeme
));
628 static PyObject
*gcirand_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
630 const gccrand_info
*info
= GCCRAND_INFO(ty
);
632 static char *kwlist
[] = { "key", "i", 0 };
636 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#O&:new", kwlist
,
637 &k
, &n
, convu32
, &i
))
639 if (keysz(n
, info
->keysz
) != n
) VALERR("bad key length");
640 return (grand_dopywrap(ty
,
641 ((gcirand_func
*)info
->func
)(k
, n
, i
),
647 static PyObject
*gcnrand_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
649 const gccrand_info
*info
= GCCRAND_INFO(ty
);
650 static char *kwlist
[] = { "key", "nonce", 0 };
654 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#s#:new", kwlist
,
657 if (keysz(ksz
, info
->keysz
) != ksz
) VALERR("bad key length");
658 if (nsz
!= info
->noncesz
) VALERR("bad nonce length");
659 return (grand_dopywrap(ty
,
660 ((gcnrand_func
*)info
->func
)(k
, ksz
, n
),
666 static PyObject
*gccrand_pywrap(const gccrand_info
*info
)
668 gccrand_pyobj
*g
= newtype(gccrand_pytype
, 0, info
->name
);
670 g
->ty
.ht_type
.tp_basicsize
= sizeof(grand_pyobj
);
671 g
->ty
.ht_type
.tp_base
= gcrand_pytype
;
672 Py_INCREF(gcrand_pytype
);
673 g
->ty
.ht_type
.tp_flags
= (Py_TPFLAGS_DEFAULT
|
674 Py_TPFLAGS_BASETYPE
|
675 Py_TPFLAGS_HEAPTYPE
);
676 g
->ty
.ht_type
.tp_alloc
= PyType_GenericAlloc
;
677 g
->ty
.ht_type
.tp_free
= 0;
678 if (info
->f
& RNGF_INT
) g
->ty
.ht_type
.tp_new
= gcirand_pynew
;
679 else if (info
->f
& RNGF_NONCE
) g
->ty
.ht_type
.tp_new
= gcnrand_pynew
;
680 else g
->ty
.ht_type
.tp_new
= gcrand_pynew
;
681 typeready(&g
->ty
.ht_type
);
682 return ((PyObject
*)g
);
685 static PyObject
*gccrget_name(PyObject
*me
, void *hunoz
)
686 { return (PyString_FromString(GCCRAND_INFO(me
)->name
)); }
687 static PyObject
*gccrget_keysz(PyObject
*me
, void *hunoz
)
688 { return (keysz_pywrap(GCCRAND_INFO(me
)->keysz
)); }
690 static PyGetSetDef gccrand_pygetset
[] = {
691 #define GETSETNAME(op, name) gccr##op##_##name
692 GET (keysz
, "CR.keysz -> acceptable key sizes")
693 GET (name
, "CR.name -> name of this kind of generator")
698 static PyTypeObject gccrand_pytype_skel
= {
699 PyObject_HEAD_INIT(0) 0, /* Header */
700 "GCCRand", /* @tp_name@ */
701 sizeof(gccrand_pyobj
), /* @tp_basicsize@ */
702 0, /* @tp_itemsize@ */
704 0, /* @tp_dealloc@ */
706 0, /* @tp_getattr@ */
707 0, /* @tp_setattr@ */
708 0, /* @tp_compare@ */
710 0, /* @tp_as_number@ */
711 0, /* @tp_as_sequence@ */
712 0, /* @tp_as_mapping@ */
716 0, /* @tp_getattro@ */
717 0, /* @tp_setattro@ */
718 0, /* @tp_as_buffer@ */
719 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
723 "Metaclass for symmetric crypto-based generators.",
725 0, /* @tp_traverse@ */
727 0, /* @tp_richcompare@ */
728 0, /* @tp_weaklistoffset@ */
730 0, /* @tp_iternext@ */
731 0, /* @tp_methods@ */
732 0, /* @tp_members@ */
733 gccrand_pygetset
, /* @tp_getset@ */
736 0, /* @tp_descr_get@ */
737 0, /* @tp_descr_set@ */
738 0, /* @tp_dictoffset@ */
740 PyType_GenericAlloc
, /* @tp_alloc@ */
741 abstract_pynew
, /* @tp_new@ */
746 static PyTypeObject gcrand_pytype_skel
= {
747 PyObject_HEAD_INIT(0) 0, /* Header */
748 "GCRand", /* @tp_name@ */
749 sizeof(grand_pyobj
), /* @tp_basicsize@ */
750 0, /* @tp_itemsize@ */
752 grand_pydealloc
, /* @tp_dealloc@ */
754 0, /* @tp_getattr@ */
755 0, /* @tp_setattr@ */
756 0, /* @tp_compare@ */
758 0, /* @tp_as_number@ */
759 0, /* @tp_as_sequence@ */
760 0, /* @tp_as_mapping@ */
764 0, /* @tp_getattro@ */
765 0, /* @tp_setattro@ */
766 0, /* @tp_as_buffer@ */
767 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
771 "Abstract base class for symmetric crypto-based generators.",
773 0, /* @tp_traverse@ */
775 0, /* @tp_richcompare@ */
776 0, /* @tp_weaklistoffset@ */
778 0, /* @tp_iternext@ */
779 0, /* @tp_methods@ */
780 0, /* @tp_members@ */
784 0, /* @tp_descr_get@ */
785 0, /* @tp_descr_set@ */
786 0, /* @tp_dictoffset@ */
788 PyType_GenericAlloc
, /* @tp_alloc@ */
789 abstract_pynew
, /* @tp_new@ */
794 /*----- SSL and TLS generators --------------------------------------------*/
796 static PyObject
*sslprf_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
800 const gchash
*hco
= &md5
, *hci
= &sha
;
802 char *kwlist
[] = { "key", "seed", "ohash", "ihash", 0 };
804 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#s#|O&O&:new", kwlist
,
806 convgchash
, &hco
, convgchash
, &hci
))
808 rc
= grand_dopywrap(ty
, sslprf_rand(hco
, hci
, k
, ksz
, s
, ssz
), f_freeme
);
813 static PyObject
*tlsdx_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
817 const gcmac
*mc
= &sha_hmac
;
819 char *kwlist
[] = { "key", "seed", "mac", 0 };
821 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#s#|O&:new", kwlist
,
825 rc
= grand_dopywrap(ty
, tlsdx_rand(mc
, k
, ksz
, s
, ssz
), f_freeme
);
830 static PyObject
*tlsprf_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
834 const gcmac
*mcl
= &md5_hmac
, *mcr
= &sha_hmac
;
836 char *kwlist
[] = { "key", "seed", "lmac", "rmac", 0 };
838 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#s#|O&O&:new", kwlist
,
840 convgcmac
, &mcl
, convgcmac
, &mcr
))
842 rc
= grand_dopywrap(ty
, tlsprf_rand(mcl
, mcr
, k
, ksz
, s
, ssz
), f_freeme
);
847 static PyTypeObject sslprf_pytype_skel
= {
848 PyObject_HEAD_INIT(0) 0, /* Header */
849 "SSLRand", /* @tp_name@ */
850 sizeof(grand_pyobj
), /* @tp_basicsize@ */
851 0, /* @tp_itemsize@ */
853 grand_pydealloc
, /* @tp_dealloc@ */
855 0, /* @tp_getattr@ */
856 0, /* @tp_setattr@ */
857 0, /* @tp_compare@ */
859 0, /* @tp_as_number@ */
860 0, /* @tp_as_sequence@ */
861 0, /* @tp_as_mapping@ */
865 0, /* @tp_getattro@ */
866 0, /* @tp_setattro@ */
867 0, /* @tp_as_buffer@ */
868 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
872 "Random number generator for SSL master secret.",
874 0, /* @tp_traverse@ */
876 0, /* @tp_richcompare@ */
877 0, /* @tp_weaklistoffset@ */
879 0, /* @tp_iternext@ */
880 0, /* @tp_methods@ */
881 0, /* @tp_members@ */
885 0, /* @tp_descr_get@ */
886 0, /* @tp_descr_set@ */
887 0, /* @tp_dictoffset@ */
889 PyType_GenericAlloc
, /* @tp_alloc@ */
890 sslprf_pynew
, /* @tp_new@ */
895 static PyTypeObject tlsdx_pytype_skel
= {
896 PyObject_HEAD_INIT(0) 0, /* Header */
897 "TLSDataExpansion", /* @tp_name@ */
898 sizeof(grand_pyobj
), /* @tp_basicsize@ */
899 0, /* @tp_itemsize@ */
901 grand_pydealloc
, /* @tp_dealloc@ */
903 0, /* @tp_getattr@ */
904 0, /* @tp_setattr@ */
905 0, /* @tp_compare@ */
907 0, /* @tp_as_number@ */
908 0, /* @tp_as_sequence@ */
909 0, /* @tp_as_mapping@ */
913 0, /* @tp_getattro@ */
914 0, /* @tp_setattro@ */
915 0, /* @tp_as_buffer@ */
916 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
920 "TLS data expansion function.",
922 0, /* @tp_traverse@ */
924 0, /* @tp_richcompare@ */
925 0, /* @tp_weaklistoffset@ */
927 0, /* @tp_iternext@ */
928 0, /* @tp_methods@ */
929 0, /* @tp_members@ */
933 0, /* @tp_descr_get@ */
934 0, /* @tp_descr_set@ */
935 0, /* @tp_dictoffset@ */
937 PyType_GenericAlloc
, /* @tp_alloc@ */
938 tlsdx_pynew
, /* @tp_new@ */
943 static PyTypeObject tlsprf_pytype_skel
= {
944 PyObject_HEAD_INIT(0) 0, /* Header */
945 "TLSPRF", /* @tp_name@ */
946 sizeof(grand_pyobj
), /* @tp_basicsize@ */
947 0, /* @tp_itemsize@ */
949 grand_pydealloc
, /* @tp_dealloc@ */
951 0, /* @tp_getattr@ */
952 0, /* @tp_setattr@ */
953 0, /* @tp_compare@ */
955 0, /* @tp_as_number@ */
956 0, /* @tp_as_sequence@ */
957 0, /* @tp_as_mapping@ */
961 0, /* @tp_getattro@ */
962 0, /* @tp_setattro@ */
963 0, /* @tp_as_buffer@ */
964 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
968 "TLS pseudorandom function.",
970 0, /* @tp_traverse@ */
972 0, /* @tp_richcompare@ */
973 0, /* @tp_weaklistoffset@ */
975 0, /* @tp_iternext@ */
976 0, /* @tp_methods@ */
977 0, /* @tp_members@ */
981 0, /* @tp_descr_get@ */
982 0, /* @tp_descr_set@ */
983 0, /* @tp_dictoffset@ */
985 PyType_GenericAlloc
, /* @tp_alloc@ */
986 tlsprf_pynew
, /* @tp_new@ */
991 /*----- DSA generator -----------------------------------------------------*/
993 static PyObject
*dsarand_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
998 char *kwlist
[] = { "seed", 0 };
1000 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "s#:new", kwlist
, &p
, &sz
))
1002 rc
= grand_dopywrap(ty
, dsarand_create(p
, sz
), f_freeme
);
1007 static PyObject
*drget_seed(PyObject
*me
, void *hunoz
)
1009 grand
*r
= GRAND_R(me
);
1010 int n
= r
->ops
->misc(r
, DSARAND_SEEDSZ
);
1011 PyObject
*rc
= bytestring_pywrap(0, n
);
1012 r
->ops
->misc(r
, DSARAND_GETSEED
, PyString_AS_STRING(rc
));
1016 static PyGetSetDef dsarand_pygetset
[] = {
1017 #define GETSETNAME(op, name) dr##op##_##name
1018 GET (seed
, "R.seed -> current generator seed")
1023 static PyTypeObject dsarand_pytype_skel
= {
1024 PyObject_HEAD_INIT(0) 0, /* Header */
1025 "DSARand", /* @tp_name@ */
1026 sizeof(grand_pyobj
), /* @tp_basicsize@ */
1027 0, /* @tp_itemsize@ */
1029 grand_pydealloc
, /* @tp_dealloc@ */
1031 0, /* @tp_getattr@ */
1032 0, /* @tp_setattr@ */
1033 0, /* @tp_compare@ */
1035 0, /* @tp_as_number@ */
1036 0, /* @tp_as_sequence@ */
1037 0, /* @tp_as_mapping@ */
1041 0, /* @tp_getattro@ */
1042 0, /* @tp_setattro@ */
1043 0, /* @tp_as_buffer@ */
1044 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1045 Py_TPFLAGS_BASETYPE
,
1048 "Pseudorandom number generator for constructing DSA parameters.",
1050 0, /* @tp_traverse@ */
1052 0, /* @tp_richcompare@ */
1053 0, /* @tp_weaklistoffset@ */
1055 0, /* @tp_iternext@ */
1056 0, /* @tp_methods@ */
1057 0, /* @tp_members@ */
1058 dsarand_pygetset
, /* @tp_getset@ */
1061 0, /* @tp_descr_get@ */
1062 0, /* @tp_descr_set@ */
1063 0, /* @tp_dictoffset@ */
1065 PyType_GenericAlloc
, /* @tp_alloc@ */
1066 dsarand_pynew
, /* @tp_new@ */
1071 /*----- Blum-Blum-Shub generator ------------------------------------------*/
1073 static PyObject
*bbs_pynew(PyTypeObject
*ty
, PyObject
*arg
, PyObject
*kw
)
1075 mp
*n
= 0, *x
= MP_TWO
;
1077 char *kwlist
[] = { "n", "x", 0 };
1079 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "O&|O&:new", kwlist
,
1080 convmp
, &n
, convmp
, &x
))
1082 rc
= grand_dopywrap(ty
, bbs_rand(n
, x
), f_freeme
);
1089 static PyObject
*bbsmeth_step(PyObject
*me
, PyObject
*arg
)
1091 grand
*r
= GRAND_R(me
); if (!PyArg_ParseTuple(arg
, ":step")) return (0);
1092 r
->ops
->misc(r
, BBS_STEP
); RETURN_ME
;
1095 static PyObject
*bbsmeth_bits(PyObject
*me
, PyObject
*arg
)
1097 grand
*r
= GRAND_R(me
); unsigned n
; uint32 w
;
1098 if (!PyArg_ParseTuple(arg
, "O&:bits", convuint
, &n
)) goto end
;
1099 if (n
> 32) VALERR("can't get more than 32 bits");
1100 r
->ops
->misc(r
, BBS_BITS
, n
, &w
); return (getulong(w
));
1105 static PyObject
*bbsmeth_wrap(PyObject
*me
, PyObject
*arg
)
1107 grand
*r
= GRAND_R(me
); if (!PyArg_ParseTuple(arg
, ":wrap")) return (0);
1108 r
->ops
->misc(r
, BBS_WRAP
); RETURN_ME
;
1111 static PyObject
*bbsget_n(PyObject
*me
, void *hunoz
)
1113 mp
*x
= MP_NEW
; grand
*r
= GRAND_R(me
);
1114 r
->ops
->misc(r
, BBS_MOD
, &x
); return (mp_pywrap(x
));
1117 static PyObject
*bbsget_x(PyObject
*me
, void *hunoz
)
1119 mp
*x
= MP_NEW
; grand
*r
= GRAND_R(me
);
1120 r
->ops
->misc(r
, BBS_STATE
, &x
); return (mp_pywrap(x
));
1123 static int bbsset_x(PyObject
*me
, PyObject
*val
, void *hunoz
)
1125 mp
*x
= 0; grand
*r
= GRAND_R(me
); int rc
= -1; if (!x
) NIERR("__del__");
1126 if ((x
= getmp(val
)) == 0) goto end
; r
->ops
->misc(r
, BBS_SET
, x
); rc
= 0;
1127 end
: mp_drop(x
); return (rc
);
1130 static PyObject
*bbsget_stepsz(PyObject
*me
, void *hunoz
)
1132 grand
*r
= GRAND_R(me
);
1133 return (PyInt_FromLong(r
->ops
->misc(r
, BBS_STEPSZ
)));
1136 static PyMethodDef bbs_pymethods
[] = {
1137 #define METHNAME(name) bbsmeth_##name
1138 METH (step
, "R.step(): steps the generator (not useful)")
1139 METH (bits
, "R.bits(N) -> W: returns N bits (<= 32) from the generator")
1140 METH (wrap
, "R.wrap(): flushes unused bits in internal buffer")
1145 static PyGetSetDef bbs_pygetset
[] = {
1146 #define GETSETNAME(op, name) bbs##op##_##name
1147 GET (n
, "R.n -> Blum modulus")
1148 GETSET(x
, "R.x -> current seed value")
1149 GET (stepsz
, "R.stepsz -> number of bits generated per step")
1154 static PyTypeObject bbs_pytype_skel
= {
1155 PyObject_HEAD_INIT(0) 0, /* Header */
1156 "BlumBlumShub", /* @tp_name@ */
1157 sizeof(grand_pyobj
), /* @tp_basicsize@ */
1158 0, /* @tp_itemsize@ */
1160 grand_pydealloc
, /* @tp_dealloc@ */
1162 0, /* @tp_getattr@ */
1163 0, /* @tp_setattr@ */
1164 0, /* @tp_compare@ */
1166 0, /* @tp_as_number@ */
1167 0, /* @tp_as_sequence@ */
1168 0, /* @tp_as_mapping@ */
1172 0, /* @tp_getattro@ */
1173 0, /* @tp_setattro@ */
1174 0, /* @tp_as_buffer@ */
1175 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1176 Py_TPFLAGS_BASETYPE
,
1179 "Blum-Blum-Shub strong pseudorandom number generator.",
1181 0, /* @tp_traverse@ */
1183 0, /* @tp_richcompare@ */
1184 0, /* @tp_weaklistoffset@ */
1186 0, /* @tp_iternext@ */
1187 bbs_pymethods
, /* @tp_methods@ */
1188 0, /* @tp_members@ */
1189 bbs_pygetset
, /* @tp_getset@ */
1192 0, /* @tp_descr_get@ */
1193 0, /* @tp_descr_set@ */
1194 0, /* @tp_dictoffset@ */
1196 PyType_GenericAlloc
, /* @tp_alloc@ */
1197 bbs_pynew
, /* @tp_new@ */
1202 typedef struct bbspriv_pyobj
{
1207 #define BBSPRIV_BP(o) (&((bbspriv_pyobj *)(o))->bp)
1209 static PyObject
*bbspriv_pynew(PyTypeObject
*ty
,
1210 PyObject
*arg
, PyObject
*kw
)
1212 mp
*p
= 0, *q
= 0, *n
= 0, *x
= MP_TWO
;
1213 bbspriv_pyobj
*rc
= 0;
1214 char *kwlist
[] = { "n", "p", "q", "seed", 0 };
1216 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "|O&O&O&O&:new", kwlist
,
1217 convmp
, &n
, convmp
, &p
, convmp
, &q
,
1220 if (!n
+ !p
+ !q
> 1) VALERR("must specify at least two of n, p, q");
1221 if (!n
) n
= mp_mul(MP_NEW
, p
, q
);
1222 else if (!p
) mp_div(&p
, 0, n
, q
);
1223 else if (!q
) mp_div(&q
, 0, n
, p
);
1224 rc
= (bbspriv_pyobj
*)ty
->tp_alloc(ty
, 0);
1225 rc
->gr
.r
= bbs_rand(n
, x
);
1226 rc
->gr
.f
= f_freeme
;
1227 rc
->bp
.p
= MP_COPY(p
);
1228 rc
->bp
.q
= MP_COPY(q
);
1229 rc
->bp
.n
= MP_COPY(n
);
1231 mp_drop(p
); mp_drop(q
); mp_drop(n
); mp_drop(x
);
1232 return ((PyObject
*)rc
);
1235 static PyObject
*meth__BBSPriv_generate(PyObject
*me
,
1236 PyObject
*arg
, PyObject
*kw
)
1238 bbs_priv bp
= { 0 };
1241 unsigned nbits
, n
= 0;
1242 grand
*r
= &rand_global
;
1243 char *kwlist
[] = { "class", "nbits", "event", "rng", "nsteps", "seed", 0 };
1244 bbspriv_pyobj
*rc
= 0;
1246 if (!PyArg_ParseTupleAndKeywords(arg
, kw
, "OO&|O&O&O&O&:generate", kwlist
,
1247 &me
, convuint
, &nbits
, convpgev
, &evt
,
1248 convgrand
, &r
, convuint
, &n
, convmp
, &x
))
1250 if (bbs_gen(&bp
, nbits
, r
, n
, evt
.proc
, evt
.ctx
))
1251 VALERR("prime genration failed");
1252 rc
= PyObject_New(bbspriv_pyobj
, bbspriv_pytype
);
1253 rc
->gr
.r
= bbs_rand(bp
.n
, x
);
1254 rc
->gr
.f
= f_freeme
;
1255 rc
->bp
.p
= MP_COPY(bp
.p
);
1256 rc
->bp
.q
= MP_COPY(bp
.q
);
1257 rc
->bp
.n
= MP_COPY(bp
.n
);
1259 mp_drop(bp
.p
); mp_drop(bp
.q
); mp_drop(bp
.n
); mp_drop(x
);
1260 return ((PyObject
*)rc
);
1263 static void bbspriv_pydealloc(PyObject
*me
)
1265 bbs_priv
*bp
= BBSPRIV_BP(me
);
1269 grand_pydealloc(me
);
1272 static PyObject
*bpmeth_ff(PyObject
*me
, PyObject
*arg
)
1274 mp
*n
= 0; grand
*r
= GRAND_R(me
); bbs_priv
*bp
= BBSPRIV_BP(me
);
1275 if (!PyArg_ParseTuple(arg
, "O&:ff", convmp
, &n
)) return (0);
1276 r
->ops
->misc(r
, BBS_FF
, bp
, n
); RETURN_ME
;
1279 static PyObject
*bpmeth_rew(PyObject
*me
, PyObject
*arg
)
1281 mp
*n
= 0; grand
*r
= GRAND_R(me
); bbs_priv
*bp
= BBSPRIV_BP(me
);
1282 if (!PyArg_ParseTuple(arg
, "O&:rew", convmp
, &n
)) return (0);
1283 r
->ops
->misc(r
, BBS_REW
, bp
, n
); RETURN_ME
;
1286 static PyObject
*bpget_n(PyObject
*me
, void *hunoz
)
1287 { return (mp_pywrap(MP_COPY(BBSPRIV_BP(me
)->n
))); }
1289 static PyObject
*bpget_p(PyObject
*me
, void *hunoz
)
1290 { return (mp_pywrap(MP_COPY(BBSPRIV_BP(me
)->p
))); }
1292 static PyObject
*bpget_q(PyObject
*me
, void *hunoz
)
1293 { return (mp_pywrap(MP_COPY(BBSPRIV_BP(me
)->q
))); }
1295 static PyMethodDef bbspriv_pymethods
[] = {
1296 #define METHNAME(name) bpmeth_##name
1297 METH (ff
, "R.ff(N): fast-forward N places")
1298 METH (rew
, "R.rew(N): rewind N places")
1303 static PyGetSetDef bbspriv_pygetset
[] = {
1304 #define GETSETNAME(op, name) bp##op##_##name
1305 GET (n
, "R.n -> Blum modulus")
1306 GET (p
, "R.p -> one of the factors of the modulus")
1307 GET (q
, "R.q -> one of the factors of the modulus")
1312 static PyTypeObject bbspriv_pytype_skel
= {
1313 PyObject_HEAD_INIT(0) 0, /* Header */
1314 "BBSPriv", /* @tp_name@ */
1315 sizeof(bbspriv_pyobj
), /* @tp_basicsize@ */
1316 0, /* @tp_itemsize@ */
1318 bbspriv_pydealloc
, /* @tp_dealloc@ */
1320 0, /* @tp_getattr@ */
1321 0, /* @tp_setattr@ */
1322 0, /* @tp_compare@ */
1324 0, /* @tp_as_number@ */
1325 0, /* @tp_as_sequence@ */
1326 0, /* @tp_as_mapping@ */
1330 0, /* @tp_getattro@ */
1331 0, /* @tp_setattro@ */
1332 0, /* @tp_as_buffer@ */
1333 Py_TPFLAGS_DEFAULT
| /* @tp_flags@ */
1334 Py_TPFLAGS_BASETYPE
,
1337 "Blum-Blum-Shub strong pseudorandom generator, with private key.",
1339 0, /* @tp_traverse@ */
1341 0, /* @tp_richcompare@ */
1342 0, /* @tp_weaklistoffset@ */
1344 0, /* @tp_iternext@ */
1345 bbspriv_pymethods
, /* @tp_methods@ */
1346 0, /* @tp_members@ */
1347 bbspriv_pygetset
, /* @tp_getset@ */
1350 0, /* @tp_descr_get@ */
1351 0, /* @tp_descr_set@ */
1352 0, /* @tp_dictoffset@ */
1354 PyType_GenericAlloc
, /* @tp_alloc@ */
1355 bbspriv_pynew
, /* @tp_new@ */
1360 /*----- Global stuff ------------------------------------------------------*/
1362 static PyMethodDef methods
[] = {
1363 #define METHNAME(name) meth_##name
1364 KWMETH(_BBSPriv_generate
, "\
1365 generate(NBITS, [event = pgen_nullev, rng = rand, nsteps = 0, seed = 2])")
1370 void rand_pyinit(void)
1372 INITTYPE(grand
, root
);
1373 INITTYPE(truerand
, grand
);
1374 INITTYPE(fibrand
, grand
);
1375 INITTYPE(lcrand
, grand
);
1376 INITTYPE(dsarand
, grand
);
1377 INITTYPE(bbs
, grand
);
1378 INITTYPE(bbspriv
, bbs
);
1379 INITTYPE(sslprf
, grand
);
1380 INITTYPE(tlsdx
, grand
);
1381 INITTYPE(tlsprf
, grand
);
1382 INITTYPE(gccrand
, type
);
1383 INITTYPE(gcrand
, grand
);
1384 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
1385 rand_seed(RAND_GLOBAL
, 160);
1386 addmethods(methods
);
1389 #define gccrand gccrand_info
1390 GEN(gccrands
, crand
)
1392 void rand_pyinsert(PyObject
*mod
)
1394 INSERT("GRand", grand_pytype
);
1395 INSERT("TrueRand", truerand_pytype
);
1396 INSERT("LCRand", lcrand_pytype
);
1397 INSERT("FibRand", fibrand_pytype
);
1398 INSERT("SSLRand", sslprf_pytype
);
1399 INSERT("TLSDataExpansion", tlsdx_pytype
);
1400 INSERT("TLSPRF", tlsprf_pytype
);
1401 INSERT("DSARand", dsarand_pytype
);
1402 INSERT("BlumBlumShub", bbs_pytype
);
1403 INSERT("BBSPriv", bbspriv_pytype
);
1404 INSERT("GCCRand", gccrand_pytype
);
1405 INSERT("GCRand", gcrand_pytype
);
1406 rand_pyobj
= grand_pywrap(&rand_global
, 0); Py_INCREF(rand_pyobj
);
1407 gccrands_dict
= gccrands(); Py_INCREF(gccrands_dict
);
1408 INSERT("gccrands", gccrands_dict
);
1409 INSERT("rand", rand_pyobj
);
1412 /*----- That's all, folks -------------------------------------------------*/