Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions scripts/build_ffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,8 @@ def build_ffi(local_wolfssl, features):
typedef struct { ...; } OS_Seed;

int wc_InitRng(WC_RNG*);
int wc_InitRngNonce(WC_RNG*, byte*, word32);
int wc_InitRngNonce_ex(WC_RNG*, byte*, word32, void*, int);
int wc_RNG_GenerateBlock(WC_RNG*, byte*, word32);
int wc_RNG_GenerateByte(WC_RNG*, byte*);
int wc_FreeRng(WC_RNG*);
Expand Down
11 changes: 11 additions & 0 deletions tests/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,14 @@ def test_bytes(rng):
assert len(rng.bytes(1)) == 1
assert len(rng.bytes(8)) == 8
assert len(rng.bytes(128)) == 128

@pytest.fixture
def rng_nonce():
return Random(b"abcdefghijklmnopqrstuv")

def test_nonce_byte(rng_nonce):
assert len(rng_nonce.byte()) == 1

@pytest.mark.parametrize("length", (1, 8, 128))
def test_nonce_bytes(rng_nonce, length):
assert len(rng_nonce.bytes(length)) == length
8 changes: 6 additions & 2 deletions wolfcrypt/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ class Random(object):
A Cryptographically Secure Pseudo Random Number Generator - CSPRNG
"""

def __init__(self):
def __init__(self, nonce=_ffi.NULL, device_id=_lib.INVALID_DEVID):
self.native_object = _ffi.new("WC_RNG *")

ret = _lib.wc_InitRng(self.native_object)
if nonce == _ffi.NULL:
nonce_size = 0
else:
nonce_size = len(nonce)
ret = _lib.wc_InitRngNonce_ex(self.native_object, nonce, nonce_size, _ffi.NULL, device_id)
if ret < 0: # pragma: no cover
self.native_object = None
raise WolfCryptError("RNG init error (%d)" % ret)
Expand Down
Loading