Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1 | /* |
| 2 | * rand.c |
| 3 | * |
| 4 | * Copyright (C) AB Strakt 2001, All rights reserved |
| 5 | * |
| 6 | * PRNG management routines, thin wrappers. |
| 7 | * See the file RATIONALE for a short explanation of why this module was written. |
| 8 | * |
| 9 | * Reviewed 2001-07-23 |
| 10 | */ |
| 11 | #include <Python.h> |
| 12 | |
| 13 | /* |
| 14 | * In order to get the RAND_screen definition from the rand.h |
| 15 | * WIN32 or WINDOWS needs to be defined, otherwise we get a |
| 16 | * warning. |
| 17 | */ |
Jean-Paul Calderone | 7ef4ee2 | 2008-12-29 16:45:27 -0500 | [diff] [blame] | 18 | #ifdef MS_WINDOWS |
| 19 | # ifndef WIN32 |
| 20 | # define WIN32 |
| 21 | # endif |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 22 | #endif |
| 23 | #include <openssl/rand.h> |
| 24 | |
| 25 | static char rand_doc[] = "\n\ |
| 26 | PRNG management routines, thin wrappers.\n\ |
| 27 | See the file RATIONALE for a short explanation of why this module was written.\n\ |
| 28 | "; |
| 29 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 30 | static char rand_add_doc[] = "\n\ |
| 31 | Add data with a given entropy to the PRNG\n\ |
| 32 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 33 | @param buffer: Buffer with random data\n\ |
| 34 | @param entropy: The entropy (in bytes) measurement of the buffer\n\ |
| 35 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 36 | "; |
| 37 | |
| 38 | static PyObject * |
| 39 | rand_add(PyObject *spam, PyObject *args) |
| 40 | { |
| 41 | char *buf; |
| 42 | int size; |
| 43 | double entropy; |
| 44 | |
| 45 | if (!PyArg_ParseTuple(args, "s#d:add", &buf, &size, &entropy)) |
| 46 | return NULL; |
| 47 | |
| 48 | RAND_add(buf, size, entropy); |
| 49 | |
| 50 | Py_INCREF(Py_None); |
| 51 | return Py_None; |
| 52 | } |
| 53 | |
| 54 | static char rand_seed_doc[] = "\n\ |
| 55 | Alias for rand_add, with entropy equal to length\n\ |
| 56 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 57 | @param buffer: Buffer with random data\n\ |
| 58 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 59 | "; |
| 60 | |
| 61 | static PyObject * |
| 62 | rand_seed(PyObject *spam, PyObject *args) |
| 63 | { |
| 64 | char *buf; |
| 65 | int size; |
| 66 | |
| 67 | if (!PyArg_ParseTuple(args, "s#:seed", &buf, &size)) |
| 68 | return NULL; |
| 69 | |
| 70 | RAND_seed(buf, size); |
| 71 | |
| 72 | Py_INCREF(Py_None); |
| 73 | return Py_None; |
| 74 | } |
| 75 | |
| 76 | static char rand_status_doc[] = "\n\ |
| 77 | Retrieve the status of the PRNG\n\ |
| 78 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 79 | @return: True if the PRNG is seeded enough, false otherwise\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 80 | "; |
| 81 | |
| 82 | static PyObject * |
| 83 | rand_status(PyObject *spam, PyObject *args) |
| 84 | { |
| 85 | if (!PyArg_ParseTuple(args, ":status")) |
| 86 | return NULL; |
| 87 | |
| 88 | return PyInt_FromLong((long)RAND_status()); |
| 89 | } |
| 90 | |
| 91 | #ifdef MS_WINDOWS |
| 92 | static char rand_screen_doc[] = "\n\ |
| 93 | Add the current contents of the screen to the PRNG state. Availability:\n\ |
| 94 | Windows.\n\ |
| 95 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 96 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 97 | "; |
| 98 | |
| 99 | static PyObject * |
| 100 | rand_screen(PyObject *spam, PyObject *args) |
| 101 | { |
| 102 | if (!PyArg_ParseTuple(args, ":screen")) |
| 103 | return NULL; |
| 104 | |
| 105 | RAND_screen(); |
| 106 | Py_INCREF(Py_None); |
| 107 | return Py_None; |
| 108 | } |
| 109 | #endif |
| 110 | |
| 111 | static char rand_egd_doc[] = "\n\ |
| 112 | Query an entropy gathering daemon (EGD) for random data and add it to the\n\ |
| 113 | PRNG. I haven't found any problems when the socket is missing, the function\n\ |
| 114 | just returns 0.\n\ |
| 115 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 116 | @param path: The path to the EGD socket\n\ |
| 117 | @param bytes: (optional) The number of bytes to read, default is 255\n\ |
| 118 | @returns: The number of bytes read (NB: a value of 0 isn't necessarily an\n\ |
| 119 | error, check rand.status())\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 120 | "; |
| 121 | |
| 122 | static PyObject * |
| 123 | rand_egd(PyObject *spam, PyObject *args) |
| 124 | { |
| 125 | char *path; |
| 126 | int bytes = 255; |
| 127 | |
| 128 | if (!PyArg_ParseTuple(args, "s|i:egd", &path, &bytes)) |
| 129 | return NULL; |
| 130 | |
| 131 | return PyInt_FromLong((long)RAND_egd_bytes(path, bytes)); |
| 132 | } |
| 133 | |
| 134 | static char rand_cleanup_doc[] = "\n\ |
| 135 | Erase the memory used by the PRNG.\n\ |
| 136 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 137 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 138 | "; |
| 139 | |
| 140 | static PyObject * |
| 141 | rand_cleanup(PyObject *spam, PyObject *args) |
| 142 | { |
| 143 | if (!PyArg_ParseTuple(args, ":cleanup")) |
| 144 | return NULL; |
| 145 | |
| 146 | RAND_cleanup(); |
| 147 | |
| 148 | Py_INCREF(Py_None); |
| 149 | return Py_None; |
| 150 | } |
| 151 | |
| 152 | static char rand_load_file_doc[] = "\n\ |
| 153 | Seed the PRNG with data from a file\n\ |
| 154 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 155 | @param filename: The file to read data from\n\ |
| 156 | @param maxbytes: (optional) The number of bytes to read, default is\n\ |
| 157 | to read the entire file\n\ |
| 158 | @return: The number of bytes read\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 159 | "; |
| 160 | |
| 161 | static PyObject * |
| 162 | rand_load_file(PyObject *spam, PyObject *args) |
| 163 | { |
| 164 | char *filename; |
| 165 | int maxbytes = -1; |
| 166 | |
| 167 | if (!PyArg_ParseTuple(args, "s|i:load_file", &filename, &maxbytes)) |
| 168 | return NULL; |
| 169 | |
| 170 | return PyInt_FromLong((long)RAND_load_file(filename, maxbytes)); |
| 171 | } |
| 172 | |
| 173 | static char rand_write_file_doc[] = "\n\ |
| 174 | Save PRNG state to a file\n\ |
| 175 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 176 | @param filename: The file to write data to\n\ |
| 177 | @return: The number of bytes written\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 178 | "; |
| 179 | |
| 180 | static PyObject * |
| 181 | rand_write_file(PyObject *spam, PyObject *args) |
| 182 | { |
| 183 | char *filename; |
| 184 | |
| 185 | if (!PyArg_ParseTuple(args, "s:write_file", &filename)) |
| 186 | return NULL; |
| 187 | |
| 188 | return PyInt_FromLong((long)RAND_write_file(filename)); |
| 189 | } |
| 190 | |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame^] | 191 | static char rand_bytes_doc[] = "\n\ |
| 192 | Get some randomm bytes as a string.\n\ |
| 193 | \n\ |
| 194 | @param num_bytes: The number of bytes to fetch\n\ |
| 195 | @return: A string of random bytes\n\ |
| 196 | "; |
| 197 | |
| 198 | static PyObject * |
| 199 | rand_bytes(PyObject *spam, PyObject *args, PyObject *keywds) |
| 200 | { |
| 201 | int num_bytes; |
| 202 | static char *kwlist[] = {"num_bytes", NULL}; |
| 203 | char *buf; |
| 204 | PyObject *obj; |
| 205 | |
| 206 | if (!PyArg_ParseTupleAndKeywords(args, keywds, "i:bytes", kwlist, &num_bytes)) |
| 207 | return NULL; |
| 208 | if(num_bytes < 0) { |
| 209 | PyErr_SetString(PyExc_ValueError, "num_bytes must not be negative"); |
| 210 | return NULL; |
| 211 | } |
| 212 | buf = malloc(num_bytes); |
| 213 | if (buf == NULL) /* out of memory */ |
| 214 | return NULL; |
| 215 | RAND_bytes(buf, num_bytes); |
| 216 | obj = PyString_FromStringAndSize(buf, num_bytes); |
| 217 | free(buf); |
| 218 | return obj; |
| 219 | } |
| 220 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 221 | |
| 222 | /* Methods in the OpenSSL.rand module */ |
| 223 | static PyMethodDef rand_methods[] = { |
| 224 | { "add", (PyCFunction)rand_add, METH_VARARGS, rand_add_doc }, |
| 225 | { "seed", (PyCFunction)rand_seed, METH_VARARGS, rand_seed_doc }, |
| 226 | { "status", (PyCFunction)rand_status, METH_VARARGS, rand_status_doc }, |
| 227 | #ifdef MS_WINDOWS |
| 228 | { "screen", (PyCFunction)rand_screen, METH_VARARGS, rand_screen_doc }, |
| 229 | #endif |
| 230 | { "egd", (PyCFunction)rand_egd, METH_VARARGS, rand_egd_doc }, |
| 231 | { "cleanup", (PyCFunction)rand_cleanup, METH_VARARGS, rand_cleanup_doc }, |
| 232 | { "load_file", (PyCFunction)rand_load_file, METH_VARARGS, rand_load_file_doc }, |
| 233 | { "write_file",(PyCFunction)rand_write_file, METH_VARARGS, rand_write_file_doc }, |
Rick Dean | 433dc64 | 2009-07-07 13:11:55 -0500 | [diff] [blame^] | 234 | { "bytes", (PyCFunction)rand_bytes, METH_VARARGS|METH_KEYWORDS, rand_bytes_doc }, |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 235 | { NULL, NULL } |
| 236 | }; |
| 237 | |
| 238 | |
| 239 | /* |
| 240 | * Initialize the rand sub module |
| 241 | * |
| 242 | * Arguments: None |
| 243 | * Returns: None |
| 244 | */ |
| 245 | void |
| 246 | initrand(void) |
| 247 | { |
| 248 | PyObject *module; |
| 249 | |
| 250 | ERR_load_RAND_strings(); |
| 251 | |
| 252 | if ((module = Py_InitModule3("rand", rand_methods, rand_doc)) == NULL) |
| 253 | return; |
| 254 | } |
| 255 | |