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