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