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