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