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