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