Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 1 | """ |
| 2 | PRNG management routines, thin wrappers. |
| 3 | |
| 4 | See the file RATIONALE for a short explanation of why this module was written. |
| 5 | """ |
| 6 | |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 7 | from functools import partial |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 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, |
| 12 | exception_from_error_queue as _exception_from_error_queue) |
| 13 | |
| 14 | |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 15 | class Error(Exception): |
Jean-Paul Calderone | 511cde0 | 2013-12-29 10:31:13 -0500 | [diff] [blame] | 16 | """ |
| 17 | An error occurred in an `OpenSSL.rand` API. |
| 18 | """ |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 19 | |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 20 | _raise_current_error = partial(_exception_from_error_queue, Error) |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 21 | |
| 22 | _unspecified = object() |
| 23 | |
Jean-Paul Calderone | e80a25a | 2014-01-09 14:33:42 -0500 | [diff] [blame] | 24 | _builtin_bytes = bytes |
| 25 | |
Jean-Paul Calderone | 53a5e13 | 2014-01-11 08:31:19 -0500 | [diff] [blame] | 26 | try: |
| 27 | _integer_types = (int, long) |
| 28 | except NameError: |
| 29 | _integer_types = (int,) |
| 30 | |
| 31 | |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 32 | def bytes(num_bytes): |
| 33 | """ |
| 34 | Get some random bytes as a string. |
| 35 | |
| 36 | :param num_bytes: The number of bytes to fetch |
| 37 | :return: A string of random bytes |
| 38 | """ |
Jean-Paul Calderone | 53a5e13 | 2014-01-11 08:31:19 -0500 | [diff] [blame] | 39 | if not isinstance(num_bytes, _integer_types): |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 40 | raise TypeError("num_bytes must be an integer") |
| 41 | |
| 42 | if num_bytes < 0: |
| 43 | raise ValueError("num_bytes must not be negative") |
| 44 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 45 | result_buffer = _ffi.new("char[]", num_bytes) |
| 46 | result_code = _lib.RAND_bytes(result_buffer, num_bytes) |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 47 | if result_code == -1: |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 48 | # TODO: No tests for this code path. Triggering a RAND_bytes failure |
| 49 | # might involve supplying a custom ENGINE? That's hard. |
| 50 | _raise_current_error() |
| 51 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 52 | return _ffi.buffer(result_buffer)[:] |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 53 | |
| 54 | |
| 55 | |
| 56 | def add(buffer, entropy): |
| 57 | """ |
| 58 | Add data with a given entropy to the PRNG |
| 59 | |
| 60 | :param buffer: Buffer with random data |
| 61 | :param entropy: The entropy (in bytes) measurement of the buffer |
| 62 | :return: None |
| 63 | """ |
Jean-Paul Calderone | e80a25a | 2014-01-09 14:33:42 -0500 | [diff] [blame] | 64 | if not isinstance(buffer, _builtin_bytes): |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 65 | raise TypeError("buffer must be a byte string") |
| 66 | |
| 67 | if not isinstance(entropy, int): |
| 68 | raise TypeError("entropy must be an integer") |
| 69 | |
| 70 | # TODO Nothing tests this call actually being made, or made properly. |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 71 | _lib.RAND_add(buffer, len(buffer), entropy) |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 72 | |
| 73 | |
| 74 | |
| 75 | def seed(buffer): |
| 76 | """ |
| 77 | Alias for rand_add, with entropy equal to length |
| 78 | |
| 79 | :param buffer: Buffer with random data |
| 80 | :return: None |
| 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 | # 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_seed(buffer, len(buffer)) |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 87 | |
| 88 | |
| 89 | |
| 90 | def status(): |
| 91 | """ |
| 92 | Retrieve the status of the PRNG |
| 93 | |
| 94 | :return: True if the PRNG is seeded enough, false otherwise |
| 95 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 96 | return _lib.RAND_status() |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 97 | |
| 98 | |
| 99 | |
| 100 | def egd(path, bytes=_unspecified): |
| 101 | """ |
| 102 | Query an entropy gathering daemon (EGD) for random data and add it to the |
| 103 | PRNG. I haven't found any problems when the socket is missing, the function |
| 104 | just returns 0. |
| 105 | |
| 106 | :param path: The path to the EGD socket |
| 107 | :param bytes: (optional) The number of bytes to read, default is 255 |
| 108 | :returns: The number of bytes read (NB: a value of 0 isn't necessarily an |
| 109 | error, check rand.status()) |
| 110 | """ |
Jean-Paul Calderone | a8f7a94 | 2014-01-11 08:45:37 -0500 | [diff] [blame] | 111 | if not isinstance(path, _builtin_bytes): |
| 112 | raise TypeError("path must be a byte string") |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 113 | |
| 114 | if bytes is _unspecified: |
| 115 | bytes = 255 |
| 116 | elif not isinstance(bytes, int): |
| 117 | raise TypeError("bytes must be an integer") |
| 118 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 119 | return _lib.RAND_egd_bytes(path, bytes) |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 120 | |
| 121 | |
| 122 | |
| 123 | def cleanup(): |
| 124 | """ |
| 125 | Erase the memory used by the PRNG. |
| 126 | |
| 127 | :return: None |
| 128 | """ |
| 129 | # TODO Nothing tests this call actually being made, or made properly. |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 130 | _lib.RAND_cleanup() |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 131 | |
| 132 | |
| 133 | |
| 134 | def load_file(filename, maxbytes=_unspecified): |
| 135 | """ |
| 136 | Seed the PRNG with data from a file |
| 137 | |
| 138 | :param filename: The file to read data from |
| 139 | :param maxbytes: (optional) The number of bytes to read, default is |
| 140 | to read the entire file |
| 141 | :return: The number of bytes read |
| 142 | """ |
Jean-Paul Calderone | 7c55d45 | 2014-01-11 09:46:35 -0500 | [diff] [blame] | 143 | if not isinstance(filename, _builtin_bytes): |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 144 | raise TypeError("filename must be a string") |
| 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 | |
| 154 | |
| 155 | def write_file(filename): |
| 156 | """ |
| 157 | Save PRNG state to a file |
| 158 | |
| 159 | :param filename: The file to write data to |
| 160 | :return: The number of bytes written |
| 161 | """ |
Jean-Paul Calderone | 7c55d45 | 2014-01-11 09:46:35 -0500 | [diff] [blame] | 162 | if not isinstance(filename, _builtin_bytes): |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 163 | raise TypeError("filename must be a string") |
| 164 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 165 | return _lib.RAND_write_file(filename) |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 166 | |
| 167 | |
| 168 | # TODO There are no tests for screen at all |
| 169 | def screen(): |
| 170 | """ |
| 171 | Add the current contents of the screen to the PRNG state. Availability: |
| 172 | Windows. |
| 173 | |
| 174 | :return: None |
| 175 | """ |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 176 | _lib.RAND_screen() |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 177 | |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 178 | if getattr(_lib, 'RAND_screen', None) is None: |
Jean-Paul Calderone | 8210b92 | 2013-02-09 09:03:18 -0800 | [diff] [blame] | 179 | del screen |
| 180 | |
| 181 | |
| 182 | # TODO There are no tests for the RAND strings being loaded, whatever that |
| 183 | # means. |
Jean-Paul Calderone | 6037d07 | 2013-12-28 18:04:00 -0500 | [diff] [blame] | 184 | _lib.ERR_load_RAND_strings() |