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