Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 1 | """ |
| 2 | PRNG management routines, thin wrappers. |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 3 | """ |
| 4 | |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 5 | import os |
| 6 | import warnings |
| 7 | |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 8 | from functools import partial |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 9 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 10 | from six import integer_types as _integer_types |
| 11 | |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 12 | from OpenSSL._util import ( |
| 13 | ffi as _ffi, |
| 14 | lib as _lib, |
Jean-Paul Calderone | cbb68cc | 2015-04-11 12:41:30 -0400 | [diff] [blame] | 15 | exception_from_error_queue as _exception_from_error_queue, |
| 16 | path_string as _path_string) |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 17 | |
| 18 | |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 19 | class Error(Exception): |
Jean-Paul Calderone | 511cde0 | 2013-12-29 10:31:13 -0500 | [diff] [blame] | 20 | """ |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 21 | An error occurred in an :mod:`OpenSSL.rand` API. |
| 22 | |
| 23 | If the current RAND method supports any errors, this is raised when needed. |
| 24 | The default method does not raise this when the entropy pool is depleted. |
| 25 | |
| 26 | Whenever this exception is raised directly, it has a list of error messages |
| 27 | from the OpenSSL error queue, where each item is a tuple *(lib, function, |
| 28 | reason)*. Here *lib*, *function* and *reason* are all strings, describing |
| 29 | where and what the problem is. |
| 30 | |
| 31 | See :manpage:`err(3)` for more information. |
Jean-Paul Calderone | 511cde0 | 2013-12-29 10:31:13 -0500 | [diff] [blame] | 32 | """ |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 33 | |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 34 | _raise_current_error = partial(_exception_from_error_queue, Error) |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 35 | |
| 36 | _unspecified = object() |
| 37 | |
Jean-Paul Calderone | e80a25a | 2014-01-09 14:33:42 -0500 | [diff] [blame] | 38 | _builtin_bytes = bytes |
| 39 | |
Alex Gaynor | ca87ff6 | 2015-09-04 23:31:03 -0400 | [diff] [blame] | 40 | |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 41 | def bytes(num_bytes): |
| 42 | """ |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 43 | Get some random bytes from the PRNG as a string. |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 44 | |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 45 | This is a wrapper for the C function ``RAND_bytes``. |
| 46 | |
| 47 | :param num_bytes: The number of bytes to fetch. |
| 48 | |
| 49 | :return: A string of random bytes. |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 50 | """ |
Jean-Paul Calderone | 53a5e13 | 2014-01-11 08:31:19 -0500 | [diff] [blame] | 51 | if not isinstance(num_bytes, _integer_types): |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 52 | raise TypeError("num_bytes must be an integer") |
| 53 | |
| 54 | if num_bytes < 0: |
| 55 | raise ValueError("num_bytes must not be negative") |
| 56 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 57 | result_buffer = _ffi.new("char[]", num_bytes) |
| 58 | result_code = _lib.RAND_bytes(result_buffer, num_bytes) |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 59 | if result_code == -1: |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 60 | # TODO: No tests for this code path. Triggering a RAND_bytes failure |
| 61 | # might involve supplying a custom ENGINE? That's hard. |
| 62 | _raise_current_error() |
| 63 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 64 | return _ffi.buffer(result_buffer)[:] |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 65 | |
| 66 | |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 67 | def add(buffer, entropy): |
| 68 | """ |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 69 | Mix bytes from *string* into the PRNG state. |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 70 | |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 71 | The *entropy* argument is (the lower bound of) an estimate of how much |
| 72 | randomness is contained in *string*, measured in bytes. |
| 73 | |
| 74 | For more information, see e.g. :rfc:`1750`. |
| 75 | |
| 76 | :param buffer: Buffer with random data. |
| 77 | :param entropy: The entropy (in bytes) measurement of the buffer. |
| 78 | |
| 79 | :return: :obj:`None` |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 80 | """ |
Jean-Paul Calderone | e80a25a | 2014-01-09 14:33:42 -0500 | [diff] [blame] | 81 | if not isinstance(buffer, _builtin_bytes): |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 82 | raise TypeError("buffer must be a byte string") |
| 83 | |
| 84 | if not isinstance(entropy, int): |
| 85 | raise TypeError("entropy must be an integer") |
| 86 | |
| 87 | # TODO Nothing tests this call actually being made, or made properly. |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 88 | _lib.RAND_add(buffer, len(buffer), entropy) |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 89 | |
| 90 | |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 91 | def seed(buffer): |
| 92 | """ |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 93 | Equivalent to calling :func:`add` with *entropy* as the length of *buffer*. |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 94 | |
| 95 | :param buffer: Buffer with random data |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 96 | |
| 97 | :return: :obj:`None` |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 98 | """ |
Jean-Paul Calderone | e80a25a | 2014-01-09 14:33:42 -0500 | [diff] [blame] | 99 | if not isinstance(buffer, _builtin_bytes): |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 100 | raise TypeError("buffer must be a byte string") |
| 101 | |
| 102 | # TODO Nothing tests this call actually being made, or made properly. |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 103 | _lib.RAND_seed(buffer, len(buffer)) |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 104 | |
| 105 | |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 106 | def status(): |
| 107 | """ |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 108 | Check whether the PRNG has been seeded with enough data. |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 109 | |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 110 | :return: :obj:`True` if the PRNG is seeded enough, :obj:`False` otherwise. |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 111 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 112 | return _lib.RAND_status() |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 113 | |
| 114 | |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 115 | def egd(path, bytes=_unspecified): |
| 116 | """ |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 117 | Query the system random source and seed the PRNG. |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 118 | |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 119 | Does *not* actually query the EGD. |
| 120 | |
Hynek Schlawack | 0cc6154 | 2016-01-19 14:09:32 +0100 | [diff] [blame] | 121 | .. deprecated:: 16.0.0 |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 122 | EGD was only necessary for some commercial UNIX systems that all |
| 123 | reached their ends of life more than a decade ago. See |
| 124 | `pyca/cryptography#1636 |
| 125 | <https://github.com/pyca/cryptography/pull/1636>`_. |
| 126 | |
| 127 | :param path: Ignored. |
| 128 | :param bytes: (optional) The number of bytes to read, default is 255. |
| 129 | |
| 130 | :returns: ``len(bytes)`` or 255 if not specified. |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 131 | """ |
Hynek Schlawack | 0cc6154 | 2016-01-19 14:09:32 +0100 | [diff] [blame] | 132 | warnings.warn("OpenSSL.rand.egd() is deprecated as of 16.0.0.", |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 133 | DeprecationWarning) |
| 134 | |
Jean-Paul Calderone | a8f7a94 | 2014-01-11 08:45:37 -0500 | [diff] [blame] | 135 | if not isinstance(path, _builtin_bytes): |
| 136 | raise TypeError("path must be a byte string") |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 137 | |
| 138 | if bytes is _unspecified: |
| 139 | bytes = 255 |
| 140 | elif not isinstance(bytes, int): |
| 141 | raise TypeError("bytes must be an integer") |
| 142 | |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 143 | seed(os.urandom(bytes)) |
| 144 | return bytes |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 145 | |
| 146 | |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 147 | def cleanup(): |
| 148 | """ |
| 149 | Erase the memory used by the PRNG. |
| 150 | |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 151 | This is a wrapper for the C function ``RAND_cleanup``. |
| 152 | |
| 153 | :return: :obj:`None` |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 154 | """ |
| 155 | # TODO Nothing tests this call actually being made, or made properly. |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 156 | _lib.RAND_cleanup() |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 157 | |
| 158 | |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 159 | def load_file(filename, maxbytes=_unspecified): |
| 160 | """ |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 161 | Read *maxbytes* of data from *filename* and seed the PRNG with it. |
| 162 | |
| 163 | Read the whole file if *maxbytes* is not specified or negative. |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 164 | |
Jean-Paul Calderone | 4e0c43f | 2015-04-13 10:15:17 -0400 | [diff] [blame] | 165 | :param filename: The file to read data from (``bytes`` or ``unicode``). |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 166 | :param maxbytes: (optional) The number of bytes to read. Default is to |
| 167 | read the entire file. |
Jean-Paul Calderone | 4e0c43f | 2015-04-13 10:15:17 -0400 | [diff] [blame] | 168 | |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 169 | :return: The number of bytes read |
| 170 | """ |
Jean-Paul Calderone | cbb68cc | 2015-04-11 12:41:30 -0400 | [diff] [blame] | 171 | filename = _path_string(filename) |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 172 | |
| 173 | if maxbytes is _unspecified: |
| 174 | maxbytes = -1 |
| 175 | elif not isinstance(maxbytes, int): |
| 176 | raise TypeError("maxbytes must be an integer") |
| 177 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 178 | return _lib.RAND_load_file(filename, maxbytes) |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 179 | |
| 180 | |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 181 | def write_file(filename): |
| 182 | """ |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 183 | Write a number of random bytes (currently 1024) to the file *path*. This |
| 184 | file can then be used with :func:`load_file` to seed the PRNG again. |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 185 | |
Jean-Paul Calderone | 4e0c43f | 2015-04-13 10:15:17 -0400 | [diff] [blame] | 186 | :param filename: The file to write data to (``bytes`` or ``unicode``). |
| 187 | |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 188 | :return: The number of bytes written. |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 189 | """ |
Jean-Paul Calderone | cbb68cc | 2015-04-11 12:41:30 -0400 | [diff] [blame] | 190 | filename = _path_string(filename) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 191 | return _lib.RAND_write_file(filename) |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 192 | |
| 193 | |
| 194 | # TODO There are no tests for screen at all |
| 195 | def screen(): |
| 196 | """ |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 197 | Add the current contents of the screen to the PRNG state. |
| 198 | |
| 199 | Availability: Windows. |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 200 | |
| 201 | :return: None |
| 202 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 203 | _lib.RAND_screen() |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 204 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 205 | if getattr(_lib, 'RAND_screen', None) is None: |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 206 | del screen |
| 207 | |
| 208 | |
| 209 | # TODO There are no tests for the RAND strings being loaded, whatever that |
| 210 | # means. |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 211 | _lib.ERR_load_RAND_strings() |