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