File tree Expand file tree Collapse file tree 3 files changed +81
-0
lines changed Expand file tree Collapse file tree 3 files changed +81
-0
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,9 @@ Changes:
24
24
^^^^^^^^
25
25
26
26
27
+ - Re-added a subset of the ``OpenSSL.rand `` module.
28
+ This subset allows conscientious users to reseed the OpenSSL CSPRNG after fork.
29
+ `#708 <https://github.com/pyca/pyopenssl/pull/708 >`_
27
30
- Corrected a use-after-free when reusing an issuer or subject from an ``X509 `` object after the underlying object has been mutated.
28
31
`#709 <https://github.com/pyca/pyopenssl/pull/709 >`_
29
32
Original file line number Diff line number Diff line change
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 ()
Original file line number Diff line number Diff line change
1
+ # Copyright (c) Frederick Dean
2
+ # See LICENSE for details.
3
+
4
+ """
5
+ Unit tests for `OpenSSL.rand`.
6
+ """
7
+
8
+ import pytest
9
+
10
+ from OpenSSL import rand
11
+
12
+
13
+ class TestRand (object ):
14
+
15
+ @pytest .mark .parametrize ('args' , [
16
+ (b"foo" , None ),
17
+ (None , 3 ),
18
+ ])
19
+ def test_add_wrong_args (self , args ):
20
+ """
21
+ `OpenSSL.rand.add` raises `TypeError` if called with arguments not of
22
+ type `str` and `int`.
23
+ """
24
+ with pytest .raises (TypeError ):
25
+ rand .add (* args )
26
+
27
+ def test_add (self ):
28
+ """
29
+ `OpenSSL.rand.add` adds entropy to the PRNG.
30
+ """
31
+ rand .add (b'hamburger' , 3 )
32
+
33
+ def test_status (self ):
34
+ """
35
+ `OpenSSL.rand.status` returns `1` if the PRNG has sufficient entropy,
36
+ `0` otherwise.
37
+ """
38
+ assert rand .status () == 1
You can’t perform that action at this time.
0 commit comments