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 | |
Alex Gaynor | 8a1de8d | 2017-07-06 22:40:07 -0400 | [diff] [blame] | 5 | import warnings |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 6 | from functools import partial |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 7 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 8 | from six import integer_types as _integer_types |
| 9 | |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 10 | from OpenSSL._util import ( |
| 11 | ffi as _ffi, |
| 12 | lib as _lib, |
Jean-Paul Calderone | cbb68cc | 2015-04-11 12:41:30 -0400 | [diff] [blame] | 13 | exception_from_error_queue as _exception_from_error_queue, |
| 14 | path_string as _path_string) |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 15 | |
| 16 | |
Alex Gaynor | 8a1de8d | 2017-07-06 22:40:07 -0400 | [diff] [blame] | 17 | warnings.warn( |
| 18 | "OpenSSL.rand is deprecated - you should use os.urandom instead", |
| 19 | DeprecationWarning, stacklevel=3 |
| 20 | ) |
| 21 | |
| 22 | |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 23 | class Error(Exception): |
Jean-Paul Calderone | 511cde0 | 2013-12-29 10:31:13 -0500 | [diff] [blame] | 24 | """ |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 25 | An error occurred in an :mod:`OpenSSL.rand` API. |
| 26 | |
| 27 | If the current RAND method supports any errors, this is raised when needed. |
| 28 | The default method does not raise this when the entropy pool is depleted. |
| 29 | |
| 30 | Whenever this exception is raised directly, it has a list of error messages |
| 31 | from the OpenSSL error queue, where each item is a tuple *(lib, function, |
| 32 | reason)*. Here *lib*, *function* and *reason* are all strings, describing |
| 33 | where and what the problem is. |
| 34 | |
| 35 | See :manpage:`err(3)` for more information. |
Jean-Paul Calderone | 511cde0 | 2013-12-29 10:31:13 -0500 | [diff] [blame] | 36 | """ |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 37 | |
Alex Chan | c607706 | 2016-11-18 13:53:39 +0000 | [diff] [blame] | 38 | |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 39 | _raise_current_error = partial(_exception_from_error_queue, Error) |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 40 | |
| 41 | _unspecified = object() |
| 42 | |
Jean-Paul Calderone | e80a25a | 2014-01-09 14:33:42 -0500 | [diff] [blame] | 43 | _builtin_bytes = bytes |
| 44 | |
Alex Gaynor | ca87ff6 | 2015-09-04 23:31:03 -0400 | [diff] [blame] | 45 | |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 46 | def bytes(num_bytes): |
| 47 | """ |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 48 | Get some random bytes from the PRNG as a string. |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 49 | |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 50 | This is a wrapper for the C function ``RAND_bytes``. |
| 51 | |
| 52 | :param num_bytes: The number of bytes to fetch. |
| 53 | |
| 54 | :return: A string of random bytes. |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 55 | """ |
Jean-Paul Calderone | 53a5e13 | 2014-01-11 08:31:19 -0500 | [diff] [blame] | 56 | if not isinstance(num_bytes, _integer_types): |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 57 | raise TypeError("num_bytes must be an integer") |
| 58 | |
| 59 | if num_bytes < 0: |
| 60 | raise ValueError("num_bytes must not be negative") |
| 61 | |
Paul Kehrer | 9f9113a | 2016-09-20 20:10:25 -0500 | [diff] [blame] | 62 | result_buffer = _ffi.new("unsigned char[]", num_bytes) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 63 | result_code = _lib.RAND_bytes(result_buffer, num_bytes) |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 64 | if result_code == -1: |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 65 | # TODO: No tests for this code path. Triggering a RAND_bytes failure |
| 66 | # might involve supplying a custom ENGINE? That's hard. |
| 67 | _raise_current_error() |
| 68 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 69 | return _ffi.buffer(result_buffer)[:] |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 70 | |
| 71 | |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 72 | def add(buffer, entropy): |
| 73 | """ |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 74 | Mix bytes from *string* into the PRNG state. |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 75 | |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 76 | The *entropy* argument is (the lower bound of) an estimate of how much |
| 77 | randomness is contained in *string*, measured in bytes. |
| 78 | |
| 79 | For more information, see e.g. :rfc:`1750`. |
| 80 | |
| 81 | :param buffer: Buffer with random data. |
| 82 | :param entropy: The entropy (in bytes) measurement of the buffer. |
| 83 | |
| 84 | :return: :obj:`None` |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 85 | """ |
Jean-Paul Calderone | e80a25a | 2014-01-09 14:33:42 -0500 | [diff] [blame] | 86 | if not isinstance(buffer, _builtin_bytes): |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 87 | raise TypeError("buffer must be a byte string") |
| 88 | |
| 89 | if not isinstance(entropy, int): |
| 90 | raise TypeError("entropy must be an integer") |
| 91 | |
| 92 | # TODO Nothing tests this call actually being made, or made properly. |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 93 | _lib.RAND_add(buffer, len(buffer), entropy) |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 94 | |
| 95 | |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 96 | def seed(buffer): |
| 97 | """ |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 98 | 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] | 99 | |
| 100 | :param buffer: Buffer with random data |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 101 | |
| 102 | :return: :obj:`None` |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 103 | """ |
Jean-Paul Calderone | e80a25a | 2014-01-09 14:33:42 -0500 | [diff] [blame] | 104 | if not isinstance(buffer, _builtin_bytes): |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 105 | raise TypeError("buffer must be a byte string") |
| 106 | |
| 107 | # TODO Nothing tests this call actually being made, or made properly. |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 108 | _lib.RAND_seed(buffer, len(buffer)) |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 109 | |
| 110 | |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 111 | def status(): |
| 112 | """ |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 113 | Check whether the PRNG has been seeded with enough data. |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 114 | |
Alex Chan | 6b69c55 | 2016-10-24 16:34:41 +0100 | [diff] [blame] | 115 | :return: 1 if the PRNG is seeded enough, 0 otherwise. |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 116 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 117 | return _lib.RAND_status() |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 118 | |
| 119 | |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 120 | def cleanup(): |
| 121 | """ |
| 122 | Erase the memory used by the PRNG. |
| 123 | |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 124 | This is a wrapper for the C function ``RAND_cleanup``. |
| 125 | |
| 126 | :return: :obj:`None` |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 127 | """ |
| 128 | # TODO Nothing tests this call actually being made, or made properly. |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 129 | _lib.RAND_cleanup() |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 130 | |
| 131 | |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 132 | def load_file(filename, maxbytes=_unspecified): |
| 133 | """ |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 134 | Read *maxbytes* of data from *filename* and seed the PRNG with it. |
| 135 | |
| 136 | Read the whole file if *maxbytes* is not specified or negative. |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 137 | |
Jean-Paul Calderone | 4e0c43f | 2015-04-13 10:15:17 -0400 | [diff] [blame] | 138 | :param filename: The file to read data from (``bytes`` or ``unicode``). |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 139 | :param maxbytes: (optional) The number of bytes to read. Default is to |
| 140 | read the entire file. |
Jean-Paul Calderone | 4e0c43f | 2015-04-13 10:15:17 -0400 | [diff] [blame] | 141 | |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 142 | :return: The number of bytes read |
| 143 | """ |
Jean-Paul Calderone | cbb68cc | 2015-04-11 12:41:30 -0400 | [diff] [blame] | 144 | filename = _path_string(filename) |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 145 | |
| 146 | if maxbytes is _unspecified: |
| 147 | maxbytes = -1 |
| 148 | elif not isinstance(maxbytes, int): |
| 149 | raise TypeError("maxbytes must be an integer") |
| 150 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 151 | return _lib.RAND_load_file(filename, maxbytes) |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 152 | |
| 153 | |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 154 | def write_file(filename): |
| 155 | """ |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 156 | Write a number of random bytes (currently 1024) to the file *path*. This |
| 157 | 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] | 158 | |
Jean-Paul Calderone | 4e0c43f | 2015-04-13 10:15:17 -0400 | [diff] [blame] | 159 | :param filename: The file to write data to (``bytes`` or ``unicode``). |
| 160 | |
Hynek Schlawack | 80d005f | 2015-10-20 18:34:13 +0200 | [diff] [blame] | 161 | :return: The number of bytes written. |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 162 | """ |
Jean-Paul Calderone | cbb68cc | 2015-04-11 12:41:30 -0400 | [diff] [blame] | 163 | filename = _path_string(filename) |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 164 | return _lib.RAND_write_file(filename) |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 165 | |
| 166 | |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 167 | # TODO There are no tests for the RAND strings being loaded, whatever that |
| 168 | # means. |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 169 | _lib.ERR_load_RAND_strings() |