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