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