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