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