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