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