algorithms.c: Add basic support for Keccak[1600, n].
[catacomb-python] / rand.c
1 /* -*-c-*-
2 *
3 * Random-number generators
4 *
5 * (c) 2004 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the Python interface to Catacomb.
11 *
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.
16 *
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.
21 *
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.
25 */
26
27 /*----- Header files ------------------------------------------------------*/
28
29 #include "catacomb-python.h"
30 #include "algorithms.h"
31
32 /*----- Main code ---------------------------------------------------------*/
33
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;
38 PyObject *rand_pyobj;
39
40 static PyObject *gccrands_dict;
41
42 static PyObject *grand_dopywrap(PyTypeObject *ty, grand *r, unsigned f)
43 {
44 grand_pyobj *g;
45
46 g = (grand_pyobj *)ty->tp_alloc(ty, 0);
47 g->r = r;
48 g->f = f;
49 return ((PyObject *)g);
50 }
51
52 PyObject *grand_pywrap(grand *r, unsigned f)
53 {
54 PyTypeObject *ty = grand_pytype;
55 PyObject *ob;
56
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));
68 }
69
70 CONVFUNC(grand, grand *, GRAND_R)
71
72 static PyObject *grmeth_byte(PyObject *me, PyObject *arg)
73 {
74 if (!PyArg_ParseTuple(arg, ":byte")) return (0);
75 return (PyInt_FromLong(grand_byte(GRAND_R(me))));
76 }
77
78 static PyObject *grmeth_word(PyObject *me, PyObject *arg)
79 {
80 if (!PyArg_ParseTuple(arg, ":word")) return (0);
81 return (getulong(grand_word(GRAND_R(me))));
82 }
83
84 static PyObject *grmeth_range(PyObject *me, PyObject *arg)
85 {
86 PyObject *m;
87 mp *x = 0;
88 mp *y = 0;
89
90 if (!PyArg_ParseTuple(arg, "O:range", &m)) return (0);
91 if (PyInt_Check(m)) {
92 long mm = PyInt_AS_LONG(m);
93 if (mm <= 0)
94 goto notpos;
95 if (mm <= 0xffffffff)
96 return (PyInt_FromLong(grand_range(GRAND_R(me), mm)));
97 }
98 if ((x = getmp(m)) == 0)
99 goto end;
100 if (!MP_POSP(x))
101 goto notpos;
102 y = mprand_range(MP_NEW, x, GRAND_R(me), 0);
103 MP_DROP(x);
104 return (mp_pywrap(y));
105 notpos:
106 VALERR("range must be strictly positive");
107 end:
108 if (x) MP_DROP(x);
109 return (0);
110 }
111
112 static PyObject *grmeth_mp(PyObject *me, PyObject *arg, PyObject *kw)
113 {
114 size_t l;
115 mpw o = 0;
116 char *kwlist[] = { "bits", "or", 0 };
117
118 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:mp", kwlist,
119 convszt, &l, convmpw, &o))
120 goto end;
121 if (l < MPW_BITS && (o >> l)) VALERR("or mask too large");
122 return (mp_pywrap(mprand(MP_NEW, l, GRAND_R(me), o)));
123 end:
124 return (0);
125 }
126
127 static PyObject *grmeth_block(PyObject *me, PyObject *arg)
128 {
129 unsigned long n;
130 PyObject *rc = 0;
131
132 if (!PyArg_ParseTuple(arg, "O&:block", convulong, &n)) goto end;
133 rc = bytestring_pywrap(0, n);
134 grand_fill(GRAND_R(me), PyString_AS_STRING(rc), n);
135 end:
136 return (rc);
137 }
138
139 static int checkop(grand *r, unsigned op, const char *what)
140 {
141 if (r->ops->misc(r, GRAND_CHECK, op))
142 return (0);
143 PyErr_Format(PyExc_TypeError, "operation %s not supported", what);
144 return (-1);
145 }
146
147 static PyObject *grmeth_seedint(PyObject *me, PyObject *arg)
148 {
149 int i;
150 grand *r = GRAND_R(me);
151 if (!PyArg_ParseTuple(arg, "i:seedint", &i) ||
152 checkop(r, GRAND_SEEDINT, "seedint"))
153 goto end;
154 r->ops->misc(r, GRAND_SEEDINT, i);
155 RETURN_ME;
156 end:
157 return (0);
158 }
159
160 static PyObject *grmeth_seedword(PyObject *me, PyObject *arg)
161 {
162 uint32 u;
163 grand *r = GRAND_R(me);
164 if (!PyArg_ParseTuple(arg, "O&:seedword", convu32, &u) ||
165 checkop(r, GRAND_SEEDUINT32, "seedword"))
166 goto end;
167 r->ops->misc(r, GRAND_SEEDUINT32, u);
168 RETURN_ME;
169 end:
170 return (0);
171 }
172
173 static PyObject *grmeth_seedblock(PyObject *me, PyObject *arg)
174 {
175 char *p;
176 Py_ssize_t n;
177 grand *r = GRAND_R(me);
178 if (!PyArg_ParseTuple(arg, "s#:seedblock", &p, &n) ||
179 checkop(r, GRAND_SEEDBLOCK, "seedblock"))
180 goto end;
181 r->ops->misc(r, GRAND_SEEDBLOCK, p, (size_t)n);
182 RETURN_ME;
183 end:
184 return (0);
185 }
186
187 static PyObject *grmeth_seedmp(PyObject *me, PyObject *arg)
188 {
189 PyObject *x;
190 mp *xx;
191 grand *r = GRAND_R(me);
192 if (!PyArg_ParseTuple(arg, "O:seedmp", &x) ||
193 checkop(r, GRAND_SEEDMP, "seedmp") ||
194 (xx = getmp(x)) == 0)
195 goto end;
196 r->ops->misc(r, GRAND_SEEDMP, xx);
197 MP_DROP(xx);
198 RETURN_ME;
199 end:
200 return (0);
201 }
202
203 static PyObject *grmeth_seedrand(PyObject *me, PyObject *arg, PyObject *kw)
204 {
205 char *kwlist[] = { "rng", 0 };
206 grand *r = GRAND_R(me);
207 grand *rr = &rand_global;
208 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:seedrand", kwlist,
209 convgrand, &rr) ||
210 checkop(r, GRAND_SEEDRAND, "seedrand"))
211 goto end;
212 r->ops->misc(r, GRAND_SEEDRAND, rr);
213 RETURN_ME;
214 end:
215 return (0);
216 }
217
218 static PyObject *grmeth_mask(PyObject *me, PyObject *arg)
219 {
220 grand *r = GRAND_R(me);
221 char *p, *q;
222 Py_ssize_t sz;
223 PyObject *rc;
224
225 if (!PyArg_ParseTuple(arg, "s#:mask", &p, &sz)) return (0);
226 rc = bytestring_pywrap(0, sz);
227 q = PyString_AS_STRING(rc);
228 GR_FILL(r, q, sz);
229 while (sz--) *q++ ^= *p++;
230 return (rc);
231 }
232
233 static void grand_pydealloc(PyObject *me)
234 {
235 grand_pyobj *g = (grand_pyobj *)me;
236 if (g->f & f_freeme)
237 GR_DESTROY(g->r);
238 FREEOBJ(me);
239 }
240
241 static PyObject *grget_name(PyObject *me, void *hunoz)
242 { return (PyString_FromString(GRAND_R(me)->ops->name)); }
243
244 static PyObject *grget_cryptop(PyObject *me, void *hunoz)
245 { return (getbool(GRAND_R(me)->ops->f & GRAND_CRYPTO)); }
246
247 static PyGetSetDef grand_pygetset[] = {
248 #define GETSETNAME(op, name) gr##op##_##name
249 GET (name, "R.name -> name of this kind of generator")
250 GET (cryptop, "R.cryptop -> flag: cryptographically strong?")
251 #undef GETSETNAME
252 { 0 }
253 };
254
255 static PyMethodDef grand_pymethods[] = {
256 #define METHNAME(name) grmeth_##name
257 METH (byte, "R.byte() -> BYTE")
258 METH (word, "R.word() -> WORD")
259 METH (block, "R.block(N) -> STRING")
260 KWMETH(mp, "R.mp(bits, [or = 0]) -> MP")
261 METH (range, "R.range(MAX) -> INT")
262 METH (mask, "R.mask(STR) -> STR")
263 METH (seedint, "R.seedint(I)")
264 METH (seedword, "R.seedword(I)")
265 METH (seedblock, "R.seedblock(BYTES)")
266 METH (seedmp, "R.seedmp(X)")
267 KWMETH(seedrand, "R.seedrand(RR)")
268 #undef METHNAME
269 { 0 }
270 };
271
272 static PyTypeObject grand_pytype_skel = {
273 PyObject_HEAD_INIT(0) 0, /* Header */
274 "GRand", /* @tp_name@ */
275 sizeof(grand_pyobj), /* @tp_basicsize@ */
276 0, /* @tp_itemsize@ */
277
278 grand_pydealloc, /* @tp_dealloc@ */
279 0, /* @tp_print@ */
280 0, /* @tp_getattr@ */
281 0, /* @tp_setattr@ */
282 0, /* @tp_compare@ */
283 0, /* @tp_repr@ */
284 0, /* @tp_as_number@ */
285 0, /* @tp_as_sequence@ */
286 0, /* @tp_as_mapping@ */
287 0, /* @tp_hash@ */
288 0, /* @tp_call@ */
289 0, /* @tp_str@ */
290 0, /* @tp_getattro@ */
291 0, /* @tp_setattro@ */
292 0, /* @tp_as_buffer@ */
293 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
294 Py_TPFLAGS_BASETYPE,
295
296 /* @tp_doc@ */
297 "Generic random number source.",
298
299 0, /* @tp_traverse@ */
300 0, /* @tp_clear@ */
301 0, /* @tp_richcompare@ */
302 0, /* @tp_weaklistoffset@ */
303 0, /* @tp_iter@ */
304 0, /* @tp_iternext@ */
305 grand_pymethods, /* @tp_methods@ */
306 0, /* @tp_members@ */
307 grand_pygetset, /* @tp_getset@ */
308 0, /* @tp_base@ */
309 0, /* @tp_dict@ */
310 0, /* @tp_descr_get@ */
311 0, /* @tp_descr_set@ */
312 0, /* @tp_dictoffset@ */
313 0, /* @tp_init@ */
314 PyType_GenericAlloc, /* @tp_alloc@ */
315 abstract_pynew, /* @tp_new@ */
316 0, /* @tp_free@ */
317 0 /* @tp_is_gc@ */
318 };
319
320 static PyObject *lcrand_pynew(PyTypeObject *me, PyObject *arg, PyObject *kw)
321 {
322 uint32 n = 0;
323 char *kwlist[] = { "seed", 0 };
324 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", kwlist, convu32, &n))
325 return (0);
326 return (grand_dopywrap(lcrand_pytype, lcrand_create(n), f_freeme));
327 }
328
329 static PyTypeObject lcrand_pytype_skel = {
330 PyObject_HEAD_INIT(0) 0, /* Header */
331 "LCRand", /* @tp_name@ */
332 sizeof(grand_pyobj), /* @tp_basicsize@ */
333 0, /* @tp_itemsize@ */
334
335 grand_pydealloc, /* @tp_dealloc@ */
336 0, /* @tp_print@ */
337 0, /* @tp_getattr@ */
338 0, /* @tp_setattr@ */
339 0, /* @tp_compare@ */
340 0, /* @tp_repr@ */
341 0, /* @tp_as_number@ */
342 0, /* @tp_as_sequence@ */
343 0, /* @tp_as_mapping@ */
344 0, /* @tp_hash@ */
345 0, /* @tp_call@ */
346 0, /* @tp_str@ */
347 0, /* @tp_getattro@ */
348 0, /* @tp_setattro@ */
349 0, /* @tp_as_buffer@ */
350 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
351 Py_TPFLAGS_BASETYPE,
352
353 /* @tp_doc@ */
354 "Linear congruential generator.",
355
356 0, /* @tp_traverse@ */
357 0, /* @tp_clear@ */
358 0, /* @tp_richcompare@ */
359 0, /* @tp_weaklistoffset@ */
360 0, /* @tp_iter@ */
361 0, /* @tp_iternext@ */
362 0, /* @tp_methods@ */
363 0, /* @tp_members@ */
364 0, /* @tp_getset@ */
365 0, /* @tp_base@ */
366 0, /* @tp_dict@ */
367 0, /* @tp_descr_get@ */
368 0, /* @tp_descr_set@ */
369 0, /* @tp_dictoffset@ */
370 0, /* @tp_init@ */
371 PyType_GenericAlloc, /* @tp_alloc@ */
372 lcrand_pynew, /* @tp_new@ */
373 0, /* @tp_free@ */
374 0 /* @tp_is_gc@ */
375 };
376
377 static PyObject *fibrand_pynew(PyTypeObject *me, PyObject *arg, PyObject *kw)
378 {
379 uint32 n = 0;
380 char *kwlist[] = { "seed", 0 };
381 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&:new", kwlist, convu32, &n))
382 return (0);
383 return (grand_dopywrap(fibrand_pytype, fibrand_create(n), f_freeme));
384 }
385
386 static PyTypeObject fibrand_pytype_skel = {
387 PyObject_HEAD_INIT(0) 0, /* Header */
388 "FibRand", /* @tp_name@ */
389 sizeof(grand_pyobj), /* @tp_basicsize@ */
390 0, /* @tp_itemsize@ */
391
392 grand_pydealloc, /* @tp_dealloc@ */
393 0, /* @tp_print@ */
394 0, /* @tp_getattr@ */
395 0, /* @tp_setattr@ */
396 0, /* @tp_compare@ */
397 0, /* @tp_repr@ */
398 0, /* @tp_as_number@ */
399 0, /* @tp_as_sequence@ */
400 0, /* @tp_as_mapping@ */
401 0, /* @tp_hash@ */
402 0, /* @tp_call@ */
403 0, /* @tp_str@ */
404 0, /* @tp_getattro@ */
405 0, /* @tp_setattro@ */
406 0, /* @tp_as_buffer@ */
407 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
408 Py_TPFLAGS_BASETYPE,
409
410 /* @tp_doc@ */
411 "Fibonacci generator.",
412
413 0, /* @tp_traverse@ */
414 0, /* @tp_clear@ */
415 0, /* @tp_richcompare@ */
416 0, /* @tp_weaklistoffset@ */
417 0, /* @tp_iter@ */
418 0, /* @tp_iternext@ */
419 0, /* @tp_methods@ */
420 0, /* @tp_members@ */
421 0, /* @tp_getset@ */
422 0, /* @tp_base@ */
423 0, /* @tp_dict@ */
424 0, /* @tp_descr_get@ */
425 0, /* @tp_descr_set@ */
426 0, /* @tp_dictoffset@ */
427 0, /* @tp_init@ */
428 PyType_GenericAlloc, /* @tp_alloc@ */
429 fibrand_pynew, /* @tp_new@ */
430 0, /* @tp_free@ */
431 0 /* @tp_is_gc@ */
432 };
433
434 /*----- True random generator ---------------------------------------------*/
435
436 static PyObject *trmeth_gate(PyObject *me, PyObject *arg)
437 {
438 grand *r = GRAND_R(me);
439 if (!PyArg_ParseTuple(arg, ":gate")) return (0);
440 r->ops->misc(r, RAND_GATE);
441 RETURN_ME;
442 }
443
444 static PyObject *trmeth_stretch(PyObject *me, PyObject *arg)
445 {
446 grand *r = GRAND_R(me);
447 if (!PyArg_ParseTuple(arg, ":stretch")) return (0);
448 r->ops->misc(r, RAND_STRETCH);
449 RETURN_ME;
450 }
451
452 static PyObject *trmeth_add(PyObject *me, PyObject *arg)
453 {
454 grand *r = GRAND_R(me);
455 char *p; Py_ssize_t n; unsigned goodbits;
456 if (!PyArg_ParseTuple(arg, "s#O&:add", &p, &n, convuint, &goodbits))
457 return (0);
458 r->ops->misc(r, RAND_ADD, p, (size_t)n, goodbits);
459 RETURN_ME;
460 }
461
462 static PyObject *trmeth_key(PyObject *me, PyObject *arg)
463 {
464 grand *r = GRAND_R(me);
465 char *p; Py_ssize_t n;
466 if (!PyArg_ParseTuple(arg, "s#:key", &p, &n)) return (0);
467 r->ops->misc(r, RAND_KEY, p, (size_t)n);
468 RETURN_ME;
469 }
470
471 static PyObject *trmeth_seed(PyObject *me, PyObject *arg)
472 {
473 grand *r = GRAND_R(me);
474 unsigned u;
475 if (!PyArg_ParseTuple(arg, "O&:seed", convuint, &u)) return (0);
476 if (u > RAND_IBITS) VALERR("pointlessly large");
477 r->ops->misc(r, RAND_SEED, u);
478 RETURN_ME;
479 end:
480 return (0);
481 }
482
483 static PyObject *trmeth_timer(PyObject *me, PyObject *arg)
484 {
485 grand *r = GRAND_R(me);
486 if (!PyArg_ParseTuple(arg, ":timer")) return (0);
487 r->ops->misc(r, RAND_TIMER);
488 RETURN_ME;
489 }
490
491 static PyObject *truerand_pynew(PyTypeObject *ty,
492 PyObject *arg, PyObject *kw)
493 {
494 char *kwlist[] = { 0 };
495 grand *r;
496 PyObject *rc = 0;
497 if (PyArg_ParseTupleAndKeywords(arg, kw, ":new", kwlist)) goto end;
498 r = rand_create();
499 r->ops->misc(r, RAND_NOISESRC, &noise_source);
500 r->ops->misc(r, RAND_SEED, 160);
501 rc = grand_dopywrap(ty, r, f_freeme);
502 end:
503 return (rc);
504 }
505
506 static PyMethodDef truerand_pymethods[] = {
507 #define METHNAME(name) trmeth_##name
508 METH (gate, "R.gate()")
509 METH (stretch, "R.stretch()")
510 METH (key, "R.key(BYTES)")
511 METH (seed, "R.seed(NBITS)")
512 METH (add, "R.add(BYTES, GOODBITS")
513 METH (timer, "R.timer()")
514 #undef METHNAME
515 { 0 }
516 };
517
518 static PyObject *trget_goodbits(PyObject *me, void *hunoz)
519 {
520 grand *r = GRAND_R(me);
521 return (PyInt_FromLong(r->ops->misc(r, RAND_GOODBITS)));
522 }
523
524 static PyGetSetDef truerand_pygetset[] = {
525 #define GETSETNAME(op, name) tr##op##_##name
526 GET (goodbits, "R.goodbits -> good bits of entropy remaining")
527 #undef GETSETNAME
528 { 0 }
529 };
530
531 static PyTypeObject truerand_pytype_skel = {
532 PyObject_HEAD_INIT(0) 0, /* Header */
533 "TrueRand", /* @tp_name@ */
534 sizeof(grand_pyobj), /* @tp_basicsize@ */
535 0, /* @tp_itemsize@ */
536
537 grand_pydealloc, /* @tp_dealloc@ */
538 0, /* @tp_print@ */
539 0, /* @tp_getattr@ */
540 0, /* @tp_setattr@ */
541 0, /* @tp_compare@ */
542 0, /* @tp_repr@ */
543 0, /* @tp_as_number@ */
544 0, /* @tp_as_sequence@ */
545 0, /* @tp_as_mapping@ */
546 0, /* @tp_hash@ */
547 0, /* @tp_call@ */
548 0, /* @tp_str@ */
549 0, /* @tp_getattro@ */
550 0, /* @tp_setattro@ */
551 0, /* @tp_as_buffer@ */
552 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
553 Py_TPFLAGS_BASETYPE,
554
555 /* @tp_doc@ */
556 "True random number source.",
557
558 0, /* @tp_traverse@ */
559 0, /* @tp_clear@ */
560 0, /* @tp_richcompare@ */
561 0, /* @tp_weaklistoffset@ */
562 0, /* @tp_iter@ */
563 0, /* @tp_iternext@ */
564 truerand_pymethods, /* @tp_methods@ */
565 0, /* @tp_members@ */
566 truerand_pygetset, /* @tp_getset@ */
567 0, /* @tp_base@ */
568 0, /* @tp_dict@ */
569 0, /* @tp_descr_get@ */
570 0, /* @tp_descr_set@ */
571 0, /* @tp_dictoffset@ */
572 0, /* @tp_init@ */
573 PyType_GenericAlloc, /* @tp_alloc@ */
574 truerand_pynew, /* @tp_new@ */
575 0, /* @tp_free@ */
576 0 /* @tp_is_gc@ */
577 };
578
579 /*----- Generators from symmetric encryption algorithms -------------------*/
580
581 static PyTypeObject *gccrand_pytype, *gcrand_pytype, *gclatinrand_pytype;
582
583 typedef grand *gcrand_func(const void *, size_t sz);
584 typedef grand *gcirand_func(const void *, size_t sz, uint32);
585 typedef grand *gcnrand_func(const void *, size_t sz, const void *);
586 typedef struct gccrand_info {
587 const char *name;
588 const octet *keysz;
589 unsigned f;
590 size_t noncesz;
591 gcrand_func *func;
592 } gccrand_info;
593
594 #define RNGF_MASK 255u
595
596 enum {
597 RNG_PLAIN = 0,
598 RNG_SEAL,
599 RNG_LATIN,
600 RNG_SHAKE,
601 RNG_KMAC
602 };
603
604 typedef struct gccrand_pyobj {
605 PyHeapTypeObject ty;
606 const gccrand_info *info;
607 } gccrand_pyobj;
608 #define GCCRAND_INFO(o) (((gccrand_pyobj *)(o))->info)
609
610 #define GCCRAND_DEF(name, ksz, func, f, nsz) \
611 static const gccrand_info func##_info = \
612 { name, ksz, f, nsz, (gcrand_func *)func };
613 RNGS(GCCRAND_DEF)
614
615 static const gccrand_info *const gcrandtab[] = {
616 #define GCCRAND_ENTRY(name, ksz, func, f, nsz) &func##_info,
617 RNGS(GCCRAND_ENTRY)
618 0
619 };
620
621 static PyObject *gcrand_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
622 {
623 const gccrand_info *info = GCCRAND_INFO(ty);
624 static char *kwlist[] = { "key", 0 };
625 char *k;
626 Py_ssize_t n;
627
628 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &k, &n))
629 goto end;
630 if (keysz(n, info->keysz) != n) VALERR("bad key length");
631 return (grand_dopywrap(ty, info->func(k, n), f_freeme));
632 end:
633 return (0);
634 }
635
636 static PyObject *gcirand_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
637 {
638 const gccrand_info *info = GCCRAND_INFO(ty);
639 uint32 i = 0;
640 static char *kwlist[] = { "key", "i", 0 };
641 char *k;
642 Py_ssize_t n;
643
644 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#O&:new", kwlist,
645 &k, &n, convu32, &i))
646 goto end;
647 if (keysz(n, info->keysz) != n) VALERR("bad key length");
648 return (grand_dopywrap(ty,
649 ((gcirand_func *)info->func)(k, n, i),
650 f_freeme));
651 end:
652 return (0);
653 }
654
655 static PyObject *gcnrand_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
656 {
657 const gccrand_info *info = GCCRAND_INFO(ty);
658 static char *kwlist[] = { "key", "nonce", 0 };
659 char *k, *n;
660 Py_ssize_t ksz, nsz;
661
662 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#s#:new", kwlist,
663 &k, &ksz, &n, &nsz))
664 goto end;
665 if (keysz(ksz, info->keysz) != ksz) VALERR("bad key length");
666 if (nsz != info->noncesz) VALERR("bad nonce length");
667 return (grand_dopywrap(ty,
668 ((gcnrand_func *)info->func)(k, ksz, n),
669 f_freeme));
670 end:
671 return (0);
672 }
673
674 static PyObject *gccrand_pywrap(const gccrand_info *info)
675 {
676 gccrand_pyobj *g = newtype(gccrand_pytype, 0, info->name);
677 g->info = info;
678 g->ty.ht_type.tp_basicsize = sizeof(grand_pyobj);
679 switch (info->f&RNGF_MASK) {
680 case RNG_LATIN: g->ty.ht_type.tp_base = gclatinrand_pytype; break;
681 default: g->ty.ht_type.tp_base = gcrand_pytype; break;
682 }
683 Py_INCREF(g->ty.ht_type.tp_base);
684 g->ty.ht_type.tp_flags = (Py_TPFLAGS_DEFAULT |
685 Py_TPFLAGS_BASETYPE |
686 Py_TPFLAGS_HEAPTYPE);
687 g->ty.ht_type.tp_alloc = PyType_GenericAlloc;
688 g->ty.ht_type.tp_free = 0;
689 switch (info->f&RNGF_MASK) {
690 case RNG_LATIN: g->ty.ht_type.tp_new = gcnrand_pynew; break;
691 case RNG_SEAL: g->ty.ht_type.tp_new = gcirand_pynew; break;
692 default: g->ty.ht_type.tp_new = gcrand_pynew; break;
693 }
694 typeready(&g->ty.ht_type);
695 return ((PyObject *)g);
696 }
697
698 static PyObject *gccrget_name(PyObject *me, void *hunoz)
699 { return (PyString_FromString(GCCRAND_INFO(me)->name)); }
700 static PyObject *gccrget_keysz(PyObject *me, void *hunoz)
701 { return (keysz_pywrap(GCCRAND_INFO(me)->keysz)); }
702
703 static PyObject *gclrmeth_tell(PyObject *me, PyObject *arg)
704 {
705 grand *r = GRAND_R(me);
706 PyObject *rc = 0;
707 kludge64 off;
708
709 if (!PyArg_ParseTuple(arg, ":tell")) return (0);
710 r->ops->misc(r, SALSA20_TELLU64, &off);
711 rc = getk64(off);
712 return (rc);
713 }
714
715 static PyObject *gclrmeth_seek(PyObject *me, PyObject *arg)
716 {
717 grand *r = GRAND_R(me);
718 kludge64 off;
719
720 if (!PyArg_ParseTuple(arg, "O&:seek", convk64, &off)) return (0);
721 r->ops->misc(r, SALSA20_SEEKU64, off);
722 RETURN_ME;
723 }
724
725 static PyGetSetDef gccrand_pygetset[] = {
726 #define GETSETNAME(op, name) gccr##op##_##name
727 GET (keysz, "CR.keysz -> acceptable key sizes")
728 GET (name, "CR.name -> name of this kind of generator")
729 #undef GETSETNAME
730 { 0 }
731 };
732
733 static PyMethodDef gclatinrand_pymethods[] = {
734 #define METHNAME(name) gclrmeth_##name
735 METH (tell, "R.tell() -> OFF")
736 METH (seek, "R.seek(OFF)")
737 #undef METHNAME
738 { 0 }
739 };
740
741 static PyTypeObject gccrand_pytype_skel = {
742 PyObject_HEAD_INIT(0) 0, /* Header */
743 "GCCRand", /* @tp_name@ */
744 sizeof(gccrand_pyobj), /* @tp_basicsize@ */
745 0, /* @tp_itemsize@ */
746
747 0, /* @tp_dealloc@ */
748 0, /* @tp_print@ */
749 0, /* @tp_getattr@ */
750 0, /* @tp_setattr@ */
751 0, /* @tp_compare@ */
752 0, /* @tp_repr@ */
753 0, /* @tp_as_number@ */
754 0, /* @tp_as_sequence@ */
755 0, /* @tp_as_mapping@ */
756 0, /* @tp_hash@ */
757 0, /* @tp_call@ */
758 0, /* @tp_str@ */
759 0, /* @tp_getattro@ */
760 0, /* @tp_setattro@ */
761 0, /* @tp_as_buffer@ */
762 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
763 Py_TPFLAGS_BASETYPE,
764
765 /* @tp_doc@ */
766 "Metaclass for symmetric crypto-based generators.",
767
768 0, /* @tp_traverse@ */
769 0, /* @tp_clear@ */
770 0, /* @tp_richcompare@ */
771 0, /* @tp_weaklistoffset@ */
772 0, /* @tp_iter@ */
773 0, /* @tp_iternext@ */
774 0, /* @tp_methods@ */
775 0, /* @tp_members@ */
776 gccrand_pygetset, /* @tp_getset@ */
777 0, /* @tp_base@ */
778 0, /* @tp_dict@ */
779 0, /* @tp_descr_get@ */
780 0, /* @tp_descr_set@ */
781 0, /* @tp_dictoffset@ */
782 0, /* @tp_init@ */
783 PyType_GenericAlloc, /* @tp_alloc@ */
784 abstract_pynew, /* @tp_new@ */
785 0, /* @tp_free@ */
786 0 /* @tp_is_gc@ */
787 };
788
789 static PyTypeObject gcrand_pytype_skel = {
790 PyObject_HEAD_INIT(0) 0, /* Header */
791 "GCRand", /* @tp_name@ */
792 sizeof(grand_pyobj), /* @tp_basicsize@ */
793 0, /* @tp_itemsize@ */
794
795 grand_pydealloc, /* @tp_dealloc@ */
796 0, /* @tp_print@ */
797 0, /* @tp_getattr@ */
798 0, /* @tp_setattr@ */
799 0, /* @tp_compare@ */
800 0, /* @tp_repr@ */
801 0, /* @tp_as_number@ */
802 0, /* @tp_as_sequence@ */
803 0, /* @tp_as_mapping@ */
804 0, /* @tp_hash@ */
805 0, /* @tp_call@ */
806 0, /* @tp_str@ */
807 0, /* @tp_getattro@ */
808 0, /* @tp_setattro@ */
809 0, /* @tp_as_buffer@ */
810 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
811 Py_TPFLAGS_BASETYPE,
812
813 /* @tp_doc@ */
814 "Abstract base class for symmetric crypto-based generators.",
815
816 0, /* @tp_traverse@ */
817 0, /* @tp_clear@ */
818 0, /* @tp_richcompare@ */
819 0, /* @tp_weaklistoffset@ */
820 0, /* @tp_iter@ */
821 0, /* @tp_iternext@ */
822 0, /* @tp_methods@ */
823 0, /* @tp_members@ */
824 0, /* @tp_getset@ */
825 0, /* @tp_base@ */
826 0, /* @tp_dict@ */
827 0, /* @tp_descr_get@ */
828 0, /* @tp_descr_set@ */
829 0, /* @tp_dictoffset@ */
830 0, /* @tp_init@ */
831 PyType_GenericAlloc, /* @tp_alloc@ */
832 abstract_pynew, /* @tp_new@ */
833 0, /* @tp_free@ */
834 0 /* @tp_is_gc@ */
835 };
836
837 static PyTypeObject gclatinrand_pytype_skel = {
838 PyObject_HEAD_INIT(0) 0, /* Header */
839 "GCLatinRand", /* @tp_name@ */
840 sizeof(grand_pyobj), /* @tp_basicsize@ */
841 0, /* @tp_itemsize@ */
842
843 grand_pydealloc, /* @tp_dealloc@ */
844 0, /* @tp_print@ */
845 0, /* @tp_getattr@ */
846 0, /* @tp_setattr@ */
847 0, /* @tp_compare@ */
848 0, /* @tp_repr@ */
849 0, /* @tp_as_number@ */
850 0, /* @tp_as_sequence@ */
851 0, /* @tp_as_mapping@ */
852 0, /* @tp_hash@ */
853 0, /* @tp_call@ */
854 0, /* @tp_str@ */
855 0, /* @tp_getattro@ */
856 0, /* @tp_setattro@ */
857 0, /* @tp_as_buffer@ */
858 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
859 Py_TPFLAGS_BASETYPE,
860
861 /* @tp_doc@ */
862 "Abstract base class for symmetric crypto-based generators.",
863
864 0, /* @tp_traverse@ */
865 0, /* @tp_clear@ */
866 0, /* @tp_richcompare@ */
867 0, /* @tp_weaklistoffset@ */
868 0, /* @tp_iter@ */
869 0, /* @tp_iternext@ */
870 gclatinrand_pymethods, /* @tp_methods@ */
871 0, /* @tp_members@ */
872 0, /* @tp_getset@ */
873 0, /* @tp_base@ */
874 0, /* @tp_dict@ */
875 0, /* @tp_descr_get@ */
876 0, /* @tp_descr_set@ */
877 0, /* @tp_dictoffset@ */
878 0, /* @tp_init@ */
879 PyType_GenericAlloc, /* @tp_alloc@ */
880 abstract_pynew, /* @tp_new@ */
881 0, /* @tp_free@ */
882 0 /* @tp_is_gc@ */
883 };
884
885 /*----- SSL and TLS generators --------------------------------------------*/
886
887 static PyObject *sslprf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
888 {
889 char *k, *s;
890 int ksz, ssz;
891 const gchash *hco = &md5, *hci = &sha;
892 PyObject *rc = 0;
893 char *kwlist[] = { "key", "seed", "ohash", "ihash", 0 };
894
895 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#s#|O&O&:new", kwlist,
896 &k, &ksz, &s, &ssz,
897 convgchash, &hco, convgchash, &hci))
898 goto end;
899 rc = grand_dopywrap(ty, sslprf_rand(hco, hci, k, ksz, s, ssz), f_freeme);
900 end:
901 return (rc);
902 }
903
904 static PyObject *tlsdx_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
905 {
906 char *k, *s;
907 int ksz, ssz;
908 const gcmac *mc = &sha_hmac;
909 PyObject *rc = 0;
910 char *kwlist[] = { "key", "seed", "mac", 0 };
911
912 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#s#|O&:new", kwlist,
913 &k, &ksz, &s, &ssz,
914 convgcmac, &mc))
915 goto end;
916 rc = grand_dopywrap(ty, tlsdx_rand(mc, k, ksz, s, ssz), f_freeme);
917 end:
918 return (rc);
919 }
920
921 static PyObject *tlsprf_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
922 {
923 char *k, *s;
924 int ksz, ssz;
925 const gcmac *mcl = &md5_hmac, *mcr = &sha_hmac;
926 PyObject *rc = 0;
927 char *kwlist[] = { "key", "seed", "lmac", "rmac", 0 };
928
929 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#s#|O&O&:new", kwlist,
930 &k, &ksz, &s, &ssz,
931 convgcmac, &mcl, convgcmac, &mcr))
932 goto end;
933 rc = grand_dopywrap(ty, tlsprf_rand(mcl, mcr, k, ksz, s, ssz), f_freeme);
934 end:
935 return (rc);
936 }
937
938 static PyTypeObject sslprf_pytype_skel = {
939 PyObject_HEAD_INIT(0) 0, /* Header */
940 "SSLRand", /* @tp_name@ */
941 sizeof(grand_pyobj), /* @tp_basicsize@ */
942 0, /* @tp_itemsize@ */
943
944 grand_pydealloc, /* @tp_dealloc@ */
945 0, /* @tp_print@ */
946 0, /* @tp_getattr@ */
947 0, /* @tp_setattr@ */
948 0, /* @tp_compare@ */
949 0, /* @tp_repr@ */
950 0, /* @tp_as_number@ */
951 0, /* @tp_as_sequence@ */
952 0, /* @tp_as_mapping@ */
953 0, /* @tp_hash@ */
954 0, /* @tp_call@ */
955 0, /* @tp_str@ */
956 0, /* @tp_getattro@ */
957 0, /* @tp_setattro@ */
958 0, /* @tp_as_buffer@ */
959 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
960 Py_TPFLAGS_BASETYPE,
961
962 /* @tp_doc@ */
963 "Random number generator for SSL master secret.",
964
965 0, /* @tp_traverse@ */
966 0, /* @tp_clear@ */
967 0, /* @tp_richcompare@ */
968 0, /* @tp_weaklistoffset@ */
969 0, /* @tp_iter@ */
970 0, /* @tp_iternext@ */
971 0, /* @tp_methods@ */
972 0, /* @tp_members@ */
973 0, /* @tp_getset@ */
974 0, /* @tp_base@ */
975 0, /* @tp_dict@ */
976 0, /* @tp_descr_get@ */
977 0, /* @tp_descr_set@ */
978 0, /* @tp_dictoffset@ */
979 0, /* @tp_init@ */
980 PyType_GenericAlloc, /* @tp_alloc@ */
981 sslprf_pynew, /* @tp_new@ */
982 0, /* @tp_free@ */
983 0 /* @tp_is_gc@ */
984 };
985
986 static PyTypeObject tlsdx_pytype_skel = {
987 PyObject_HEAD_INIT(0) 0, /* Header */
988 "TLSDataExpansion", /* @tp_name@ */
989 sizeof(grand_pyobj), /* @tp_basicsize@ */
990 0, /* @tp_itemsize@ */
991
992 grand_pydealloc, /* @tp_dealloc@ */
993 0, /* @tp_print@ */
994 0, /* @tp_getattr@ */
995 0, /* @tp_setattr@ */
996 0, /* @tp_compare@ */
997 0, /* @tp_repr@ */
998 0, /* @tp_as_number@ */
999 0, /* @tp_as_sequence@ */
1000 0, /* @tp_as_mapping@ */
1001 0, /* @tp_hash@ */
1002 0, /* @tp_call@ */
1003 0, /* @tp_str@ */
1004 0, /* @tp_getattro@ */
1005 0, /* @tp_setattro@ */
1006 0, /* @tp_as_buffer@ */
1007 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1008 Py_TPFLAGS_BASETYPE,
1009
1010 /* @tp_doc@ */
1011 "TLS data expansion function.",
1012
1013 0, /* @tp_traverse@ */
1014 0, /* @tp_clear@ */
1015 0, /* @tp_richcompare@ */
1016 0, /* @tp_weaklistoffset@ */
1017 0, /* @tp_iter@ */
1018 0, /* @tp_iternext@ */
1019 0, /* @tp_methods@ */
1020 0, /* @tp_members@ */
1021 0, /* @tp_getset@ */
1022 0, /* @tp_base@ */
1023 0, /* @tp_dict@ */
1024 0, /* @tp_descr_get@ */
1025 0, /* @tp_descr_set@ */
1026 0, /* @tp_dictoffset@ */
1027 0, /* @tp_init@ */
1028 PyType_GenericAlloc, /* @tp_alloc@ */
1029 tlsdx_pynew, /* @tp_new@ */
1030 0, /* @tp_free@ */
1031 0 /* @tp_is_gc@ */
1032 };
1033
1034 static PyTypeObject tlsprf_pytype_skel = {
1035 PyObject_HEAD_INIT(0) 0, /* Header */
1036 "TLSPRF", /* @tp_name@ */
1037 sizeof(grand_pyobj), /* @tp_basicsize@ */
1038 0, /* @tp_itemsize@ */
1039
1040 grand_pydealloc, /* @tp_dealloc@ */
1041 0, /* @tp_print@ */
1042 0, /* @tp_getattr@ */
1043 0, /* @tp_setattr@ */
1044 0, /* @tp_compare@ */
1045 0, /* @tp_repr@ */
1046 0, /* @tp_as_number@ */
1047 0, /* @tp_as_sequence@ */
1048 0, /* @tp_as_mapping@ */
1049 0, /* @tp_hash@ */
1050 0, /* @tp_call@ */
1051 0, /* @tp_str@ */
1052 0, /* @tp_getattro@ */
1053 0, /* @tp_setattro@ */
1054 0, /* @tp_as_buffer@ */
1055 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1056 Py_TPFLAGS_BASETYPE,
1057
1058 /* @tp_doc@ */
1059 "TLS pseudorandom function.",
1060
1061 0, /* @tp_traverse@ */
1062 0, /* @tp_clear@ */
1063 0, /* @tp_richcompare@ */
1064 0, /* @tp_weaklistoffset@ */
1065 0, /* @tp_iter@ */
1066 0, /* @tp_iternext@ */
1067 0, /* @tp_methods@ */
1068 0, /* @tp_members@ */
1069 0, /* @tp_getset@ */
1070 0, /* @tp_base@ */
1071 0, /* @tp_dict@ */
1072 0, /* @tp_descr_get@ */
1073 0, /* @tp_descr_set@ */
1074 0, /* @tp_dictoffset@ */
1075 0, /* @tp_init@ */
1076 PyType_GenericAlloc, /* @tp_alloc@ */
1077 tlsprf_pynew, /* @tp_new@ */
1078 0, /* @tp_free@ */
1079 0 /* @tp_is_gc@ */
1080 };
1081
1082 /*----- DSA generator -----------------------------------------------------*/
1083
1084 static PyObject *dsarand_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1085 {
1086 char *p;
1087 int sz;
1088 PyObject *rc = 0;
1089 char *kwlist[] = { "seed", 0 };
1090
1091 if (!PyArg_ParseTupleAndKeywords(arg, kw, "s#:new", kwlist, &p, &sz))
1092 goto end;
1093 rc = grand_dopywrap(ty, dsarand_create(p, sz), f_freeme);
1094 end:
1095 return (rc);
1096 }
1097
1098 static PyObject *drget_seed(PyObject *me, void *hunoz)
1099 {
1100 grand *r = GRAND_R(me);
1101 int n = r->ops->misc(r, DSARAND_SEEDSZ);
1102 PyObject *rc = bytestring_pywrap(0, n);
1103 r->ops->misc(r, DSARAND_GETSEED, PyString_AS_STRING(rc));
1104 return (rc);
1105 }
1106
1107 static PyGetSetDef dsarand_pygetset[] = {
1108 #define GETSETNAME(op, name) dr##op##_##name
1109 GET (seed, "R.seed -> current generator seed")
1110 #undef GETSETNAME
1111 { 0 }
1112 };
1113
1114 static PyTypeObject dsarand_pytype_skel = {
1115 PyObject_HEAD_INIT(0) 0, /* Header */
1116 "DSARand", /* @tp_name@ */
1117 sizeof(grand_pyobj), /* @tp_basicsize@ */
1118 0, /* @tp_itemsize@ */
1119
1120 grand_pydealloc, /* @tp_dealloc@ */
1121 0, /* @tp_print@ */
1122 0, /* @tp_getattr@ */
1123 0, /* @tp_setattr@ */
1124 0, /* @tp_compare@ */
1125 0, /* @tp_repr@ */
1126 0, /* @tp_as_number@ */
1127 0, /* @tp_as_sequence@ */
1128 0, /* @tp_as_mapping@ */
1129 0, /* @tp_hash@ */
1130 0, /* @tp_call@ */
1131 0, /* @tp_str@ */
1132 0, /* @tp_getattro@ */
1133 0, /* @tp_setattro@ */
1134 0, /* @tp_as_buffer@ */
1135 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1136 Py_TPFLAGS_BASETYPE,
1137
1138 /* @tp_doc@ */
1139 "Pseudorandom number generator for constructing DSA parameters.",
1140
1141 0, /* @tp_traverse@ */
1142 0, /* @tp_clear@ */
1143 0, /* @tp_richcompare@ */
1144 0, /* @tp_weaklistoffset@ */
1145 0, /* @tp_iter@ */
1146 0, /* @tp_iternext@ */
1147 0, /* @tp_methods@ */
1148 0, /* @tp_members@ */
1149 dsarand_pygetset, /* @tp_getset@ */
1150 0, /* @tp_base@ */
1151 0, /* @tp_dict@ */
1152 0, /* @tp_descr_get@ */
1153 0, /* @tp_descr_set@ */
1154 0, /* @tp_dictoffset@ */
1155 0, /* @tp_init@ */
1156 PyType_GenericAlloc, /* @tp_alloc@ */
1157 dsarand_pynew, /* @tp_new@ */
1158 0, /* @tp_free@ */
1159 0 /* @tp_is_gc@ */
1160 };
1161
1162 /*----- Blum-Blum-Shub generator ------------------------------------------*/
1163
1164 static PyObject *bbs_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
1165 {
1166 mp *n = 0, *x = MP_TWO;
1167 PyObject *rc = 0;
1168 char *kwlist[] = { "n", "x", 0 };
1169
1170 if (!PyArg_ParseTupleAndKeywords(arg, kw, "O&|O&:new", kwlist,
1171 convmp, &n, convmp, &x))
1172 goto end;
1173 rc = grand_dopywrap(ty, bbs_rand(n, x), f_freeme);
1174 end:
1175 mp_drop(n);
1176 mp_drop(x);
1177 return (rc);
1178 }
1179
1180 static PyObject *bbsmeth_step(PyObject *me, PyObject *arg)
1181 {
1182 grand *r = GRAND_R(me); if (!PyArg_ParseTuple(arg, ":step")) return (0);
1183 r->ops->misc(r, BBS_STEP); RETURN_ME;
1184 }
1185
1186 static PyObject *bbsmeth_bits(PyObject *me, PyObject *arg)
1187 {
1188 grand *r = GRAND_R(me); unsigned n; uint32 w;
1189 if (!PyArg_ParseTuple(arg, "O&:bits", convuint, &n)) goto end;
1190 if (n > 32) VALERR("can't get more than 32 bits");
1191 r->ops->misc(r, BBS_BITS, n, &w); return (getulong(w));
1192 end:
1193 return (0);
1194 }
1195
1196 static PyObject *bbsmeth_wrap(PyObject *me, PyObject *arg)
1197 {
1198 grand *r = GRAND_R(me); if (!PyArg_ParseTuple(arg, ":wrap")) return (0);
1199 r->ops->misc(r, BBS_WRAP); RETURN_ME;
1200 }
1201
1202 static PyObject *bbsget_n(PyObject *me, void *hunoz)
1203 {
1204 mp *x = MP_NEW; grand *r = GRAND_R(me);
1205 r->ops->misc(r, BBS_MOD, &x); return (mp_pywrap(x));
1206 }
1207
1208 static PyObject *bbsget_x(PyObject *me, void *hunoz)
1209 {
1210 mp *x = MP_NEW; grand *r = GRAND_R(me);
1211 r->ops->misc(r, BBS_STATE, &x); return (mp_pywrap(x));
1212 }
1213
1214 static int bbsset_x(PyObject *me, PyObject *val, void *hunoz)
1215 {
1216 mp *x = 0; grand *r = GRAND_R(me); int rc = -1; if (!x) NIERR("__del__");
1217 if ((x = getmp(val)) == 0) goto end; r->ops->misc(r, BBS_SET, x); rc = 0;
1218 end: mp_drop(x); return (rc);
1219 }
1220
1221 static PyObject *bbsget_stepsz(PyObject *me, void *hunoz)
1222 {
1223 grand *r = GRAND_R(me);
1224 return (PyInt_FromLong(r->ops->misc(r, BBS_STEPSZ)));
1225 }
1226
1227 static PyMethodDef bbs_pymethods[] = {
1228 #define METHNAME(name) bbsmeth_##name
1229 METH (step, "R.step(): steps the generator (not useful)")
1230 METH (bits, "R.bits(N) -> W: returns N bits (<= 32) from the generator")
1231 METH (wrap, "R.wrap(): flushes unused bits in internal buffer")
1232 #undef METHNAME
1233 { 0 }
1234 };
1235
1236 static PyGetSetDef bbs_pygetset[] = {
1237 #define GETSETNAME(op, name) bbs##op##_##name
1238 GET (n, "R.n -> Blum modulus")
1239 GETSET(x, "R.x -> current seed value")
1240 GET (stepsz, "R.stepsz -> number of bits generated per step")
1241 #undef GETSETNAME
1242 { 0 }
1243 };
1244
1245 static PyTypeObject bbs_pytype_skel = {
1246 PyObject_HEAD_INIT(0) 0, /* Header */
1247 "BlumBlumShub", /* @tp_name@ */
1248 sizeof(grand_pyobj), /* @tp_basicsize@ */
1249 0, /* @tp_itemsize@ */
1250
1251 grand_pydealloc, /* @tp_dealloc@ */
1252 0, /* @tp_print@ */
1253 0, /* @tp_getattr@ */
1254 0, /* @tp_setattr@ */
1255 0, /* @tp_compare@ */
1256 0, /* @tp_repr@ */
1257 0, /* @tp_as_number@ */
1258 0, /* @tp_as_sequence@ */
1259 0, /* @tp_as_mapping@ */
1260 0, /* @tp_hash@ */
1261 0, /* @tp_call@ */
1262 0, /* @tp_str@ */
1263 0, /* @tp_getattro@ */
1264 0, /* @tp_setattro@ */
1265 0, /* @tp_as_buffer@ */
1266 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1267 Py_TPFLAGS_BASETYPE,
1268
1269 /* @tp_doc@ */
1270 "Blum-Blum-Shub strong pseudorandom number generator.",
1271
1272 0, /* @tp_traverse@ */
1273 0, /* @tp_clear@ */
1274 0, /* @tp_richcompare@ */
1275 0, /* @tp_weaklistoffset@ */
1276 0, /* @tp_iter@ */
1277 0, /* @tp_iternext@ */
1278 bbs_pymethods, /* @tp_methods@ */
1279 0, /* @tp_members@ */
1280 bbs_pygetset, /* @tp_getset@ */
1281 0, /* @tp_base@ */
1282 0, /* @tp_dict@ */
1283 0, /* @tp_descr_get@ */
1284 0, /* @tp_descr_set@ */
1285 0, /* @tp_dictoffset@ */
1286 0, /* @tp_init@ */
1287 PyType_GenericAlloc, /* @tp_alloc@ */
1288 bbs_pynew, /* @tp_new@ */
1289 0, /* @tp_free@ */
1290 0 /* @tp_is_gc@ */
1291 };
1292
1293 typedef struct bbspriv_pyobj {
1294 grand_pyobj gr;
1295 bbs_priv bp;
1296 } bbspriv_pyobj;
1297
1298 #define BBSPRIV_BP(o) (&((bbspriv_pyobj *)(o))->bp)
1299
1300 static PyObject *bbspriv_pynew(PyTypeObject *ty,
1301 PyObject *arg, PyObject *kw)
1302 {
1303 mp *p = 0, *q = 0, *n = 0, *x = MP_TWO;
1304 bbspriv_pyobj *rc = 0;
1305 char *kwlist[] = { "n", "p", "q", "seed", 0 };
1306
1307 if (!PyArg_ParseTupleAndKeywords(arg, kw, "|O&O&O&O&:new", kwlist,
1308 convmp, &n, convmp, &p, convmp, &q,
1309 convmp, &x))
1310 goto end;
1311 if (!n + !p + !q > 1) VALERR("must specify at least two of n, p, q");
1312 if (!n) n = mp_mul(MP_NEW, p, q);
1313 else if (!p) mp_div(&p, 0, n, q);
1314 else if (!q) mp_div(&q, 0, n, p);
1315 rc = (bbspriv_pyobj *)ty->tp_alloc(ty, 0);
1316 rc->gr.r = bbs_rand(n, x);
1317 rc->gr.f = f_freeme;
1318 rc->bp.p = MP_COPY(p);
1319 rc->bp.q = MP_COPY(q);
1320 rc->bp.n = MP_COPY(n);
1321 end:
1322 mp_drop(p); mp_drop(q); mp_drop(n); mp_drop(x);
1323 return ((PyObject *)rc);
1324 }
1325
1326 static PyObject *meth__BBSPriv_generate(PyObject *me,
1327 PyObject *arg, PyObject *kw)
1328 {
1329 bbs_priv bp = { 0 };
1330 mp *x = MP_TWO;
1331 pgev evt = { 0 };
1332 unsigned nbits, n = 0;
1333 grand *r = &rand_global;
1334 char *kwlist[] = { "class", "nbits", "event", "rng", "nsteps", "seed", 0 };
1335 bbspriv_pyobj *rc = 0;
1336
1337 if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO&|O&O&O&O&:generate", kwlist,
1338 &me, convuint, &nbits, convpgev, &evt,
1339 convgrand, &r, convuint, &n, convmp, &x))
1340 goto end;
1341 if (bbs_gen(&bp, nbits, r, n, evt.proc, evt.ctx))
1342 VALERR("prime genration failed");
1343 rc = PyObject_New(bbspriv_pyobj, bbspriv_pytype);
1344 rc->gr.r = bbs_rand(bp.n, x);
1345 rc->gr.f = f_freeme;
1346 rc->bp.p = MP_COPY(bp.p);
1347 rc->bp.q = MP_COPY(bp.q);
1348 rc->bp.n = MP_COPY(bp.n);
1349 end:
1350 mp_drop(bp.p); mp_drop(bp.q); mp_drop(bp.n); mp_drop(x);
1351 return ((PyObject *)rc);
1352 }
1353
1354 static void bbspriv_pydealloc(PyObject *me)
1355 {
1356 bbs_priv *bp = BBSPRIV_BP(me);
1357 mp_drop(bp->n);
1358 mp_drop(bp->p);
1359 mp_drop(bp->q);
1360 grand_pydealloc(me);
1361 }
1362
1363 static PyObject *bpmeth_ff(PyObject *me, PyObject *arg)
1364 {
1365 mp *n = 0; grand *r = GRAND_R(me); bbs_priv *bp = BBSPRIV_BP(me);
1366 if (!PyArg_ParseTuple(arg, "O&:ff", convmp, &n)) return (0);
1367 r->ops->misc(r, BBS_FF, bp, n); RETURN_ME;
1368 }
1369
1370 static PyObject *bpmeth_rew(PyObject *me, PyObject *arg)
1371 {
1372 mp *n = 0; grand *r = GRAND_R(me); bbs_priv *bp = BBSPRIV_BP(me);
1373 if (!PyArg_ParseTuple(arg, "O&:rew", convmp, &n)) return (0);
1374 r->ops->misc(r, BBS_REW, bp, n); RETURN_ME;
1375 }
1376
1377 static PyObject *bpget_n(PyObject *me, void *hunoz)
1378 { return (mp_pywrap(MP_COPY(BBSPRIV_BP(me)->n))); }
1379
1380 static PyObject *bpget_p(PyObject *me, void *hunoz)
1381 { return (mp_pywrap(MP_COPY(BBSPRIV_BP(me)->p))); }
1382
1383 static PyObject *bpget_q(PyObject *me, void *hunoz)
1384 { return (mp_pywrap(MP_COPY(BBSPRIV_BP(me)->q))); }
1385
1386 static PyMethodDef bbspriv_pymethods[] = {
1387 #define METHNAME(name) bpmeth_##name
1388 METH (ff, "R.ff(N): fast-forward N places")
1389 METH (rew, "R.rew(N): rewind N places")
1390 #undef METHNAME
1391 { 0 }
1392 };
1393
1394 static PyGetSetDef bbspriv_pygetset[] = {
1395 #define GETSETNAME(op, name) bp##op##_##name
1396 GET (n, "R.n -> Blum modulus")
1397 GET (p, "R.p -> one of the factors of the modulus")
1398 GET (q, "R.q -> one of the factors of the modulus")
1399 #undef GETSETNAME
1400 { 0 }
1401 };
1402
1403 static PyTypeObject bbspriv_pytype_skel = {
1404 PyObject_HEAD_INIT(0) 0, /* Header */
1405 "BBSPriv", /* @tp_name@ */
1406 sizeof(bbspriv_pyobj), /* @tp_basicsize@ */
1407 0, /* @tp_itemsize@ */
1408
1409 bbspriv_pydealloc, /* @tp_dealloc@ */
1410 0, /* @tp_print@ */
1411 0, /* @tp_getattr@ */
1412 0, /* @tp_setattr@ */
1413 0, /* @tp_compare@ */
1414 0, /* @tp_repr@ */
1415 0, /* @tp_as_number@ */
1416 0, /* @tp_as_sequence@ */
1417 0, /* @tp_as_mapping@ */
1418 0, /* @tp_hash@ */
1419 0, /* @tp_call@ */
1420 0, /* @tp_str@ */
1421 0, /* @tp_getattro@ */
1422 0, /* @tp_setattro@ */
1423 0, /* @tp_as_buffer@ */
1424 Py_TPFLAGS_DEFAULT | /* @tp_flags@ */
1425 Py_TPFLAGS_BASETYPE,
1426
1427 /* @tp_doc@ */
1428 "Blum-Blum-Shub strong pseudorandom generator, with private key.",
1429
1430 0, /* @tp_traverse@ */
1431 0, /* @tp_clear@ */
1432 0, /* @tp_richcompare@ */
1433 0, /* @tp_weaklistoffset@ */
1434 0, /* @tp_iter@ */
1435 0, /* @tp_iternext@ */
1436 bbspriv_pymethods, /* @tp_methods@ */
1437 0, /* @tp_members@ */
1438 bbspriv_pygetset, /* @tp_getset@ */
1439 0, /* @tp_base@ */
1440 0, /* @tp_dict@ */
1441 0, /* @tp_descr_get@ */
1442 0, /* @tp_descr_set@ */
1443 0, /* @tp_dictoffset@ */
1444 0, /* @tp_init@ */
1445 PyType_GenericAlloc, /* @tp_alloc@ */
1446 bbspriv_pynew, /* @tp_new@ */
1447 0, /* @tp_free@ */
1448 0 /* @tp_is_gc@ */
1449 };
1450
1451 /*----- Global stuff ------------------------------------------------------*/
1452
1453 static PyMethodDef methods[] = {
1454 #define METHNAME(name) meth_##name
1455 KWMETH(_BBSPriv_generate, "\
1456 generate(NBITS, [event = pgen_nullev, rng = rand, nsteps = 0, seed = 2])")
1457 #undef METHNAME
1458 { 0 }
1459 };
1460
1461 void rand_pyinit(void)
1462 {
1463 INITTYPE(grand, root);
1464 INITTYPE(truerand, grand);
1465 INITTYPE(fibrand, grand);
1466 INITTYPE(lcrand, grand);
1467 INITTYPE(dsarand, grand);
1468 INITTYPE(bbs, grand);
1469 INITTYPE(bbspriv, bbs);
1470 INITTYPE(sslprf, grand);
1471 INITTYPE(tlsdx, grand);
1472 INITTYPE(tlsprf, grand);
1473 INITTYPE(gccrand, type);
1474 INITTYPE(gcrand, grand);
1475 INITTYPE(gclatinrand, gcrand);
1476 rand_noisesrc(RAND_GLOBAL, &noise_source);
1477 rand_seed(RAND_GLOBAL, 160);
1478 addmethods(methods);
1479 }
1480
1481 #define gccrand gccrand_info
1482 GEN(gccrands, crand)
1483
1484 void rand_pyinsert(PyObject *mod)
1485 {
1486 INSERT("GRand", grand_pytype);
1487 INSERT("TrueRand", truerand_pytype);
1488 INSERT("LCRand", lcrand_pytype);
1489 INSERT("FibRand", fibrand_pytype);
1490 INSERT("SSLRand", sslprf_pytype);
1491 INSERT("TLSDataExpansion", tlsdx_pytype);
1492 INSERT("TLSPRF", tlsprf_pytype);
1493 INSERT("DSARand", dsarand_pytype);
1494 INSERT("BlumBlumShub", bbs_pytype);
1495 INSERT("BBSPriv", bbspriv_pytype);
1496 INSERT("GCCRand", gccrand_pytype);
1497 INSERT("GCRand", gcrand_pytype);
1498 INSERT("GCLatinRand", gclatinrand_pytype);
1499 rand_pyobj = grand_pywrap(&rand_global, 0); Py_INCREF(rand_pyobj);
1500 gccrands_dict = gccrands(); Py_INCREF(gccrands_dict);
1501 INSERT("gccrands", gccrands_dict);
1502 INSERT("rand", rand_pyobj);
1503 }
1504
1505 /*----- That's all, folks -------------------------------------------------*/