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