Skip to content

Commit acbd662

Browse files
reaperhulkalex
authored andcommitted
restore a subset of the rand module (#708)
* restore a subset of the rand module * flake * remove cleanup, go ahead and assume status will always be 1 * lighten and add power
1 parent 4aa52c3 commit acbd662

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

CHANGELOG.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Changes:
2424
^^^^^^^^
2525

2626

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>`_
2730
- Corrected a use-after-free when reusing an issuer or subject from an ``X509`` object after the underlying object has been mutated.
2831
`#709 <https://github.com/pyca/pyopenssl/pull/709>`_
2932

src/OpenSSL/rand.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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()

tests/test_rand.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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

0 commit comments

Comments
 (0)