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