Paul Kehrer | acbd662 | 2017-11-20 22:25:18 +0800 | [diff] [blame^] | 1 | """ |
| 2 | PRNG management routines, thin wrappers. |
| 3 | """ |
| 4 | |
| 5 | from OpenSSL._util import lib as _lib |
| 6 | |
| 7 | |
| 8 | def add(buffer, entropy): |
| 9 | """ |
| 10 | Mix bytes from *string* into the PRNG state. |
| 11 | |
| 12 | The *entropy* argument is (the lower bound of) an estimate of how much |
| 13 | randomness is contained in *string*, measured in bytes. |
| 14 | |
| 15 | For more information, see e.g. :rfc:`1750`. |
| 16 | |
| 17 | This function is only relevant if you are forking Python processes and |
| 18 | need to reseed the CSPRNG after fork. |
| 19 | |
| 20 | :param buffer: Buffer with random data. |
| 21 | :param entropy: The entropy (in bytes) measurement of the buffer. |
| 22 | |
| 23 | :return: :obj:`None` |
| 24 | """ |
| 25 | if not isinstance(buffer, bytes): |
| 26 | raise TypeError("buffer must be a byte string") |
| 27 | |
| 28 | if not isinstance(entropy, int): |
| 29 | raise TypeError("entropy must be an integer") |
| 30 | |
| 31 | _lib.RAND_add(buffer, len(buffer), entropy) |
| 32 | |
| 33 | |
| 34 | def status(): |
| 35 | """ |
| 36 | Check whether the PRNG has been seeded with enough data. |
| 37 | |
| 38 | :return: 1 if the PRNG is seeded enough, 0 otherwise. |
| 39 | """ |
| 40 | return _lib.RAND_status() |