Skip to content

Commit 4c1c578

Browse files
authored
Merge pull request #92 from roberthdevries/add-support-for-random-with-nonce
Add support for nonce in random number generation.
2 parents 65e133c + 4564459 commit 4c1c578

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

scripts/build_ffi.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,8 @@ def build_ffi(local_wolfssl, features):
543543
typedef struct { ...; } OS_Seed;
544544
545545
int wc_InitRng(WC_RNG*);
546+
int wc_InitRngNonce(WC_RNG*, byte*, word32);
547+
int wc_InitRngNonce_ex(WC_RNG*, byte*, word32, void*, int);
546548
int wc_RNG_GenerateBlock(WC_RNG*, byte*, word32);
547549
int wc_RNG_GenerateByte(WC_RNG*, byte*);
548550
int wc_FreeRng(WC_RNG*);

tests/test_random.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,14 @@ def test_bytes(rng):
3737
assert len(rng.bytes(1)) == 1
3838
assert len(rng.bytes(8)) == 8
3939
assert len(rng.bytes(128)) == 128
40+
41+
@pytest.fixture
42+
def rng_nonce():
43+
return Random(b"abcdefghijklmnopqrstuv")
44+
45+
def test_nonce_byte(rng_nonce):
46+
assert len(rng_nonce.byte()) == 1
47+
48+
@pytest.mark.parametrize("length", (1, 8, 128))
49+
def test_nonce_bytes(rng_nonce, length):
50+
assert len(rng_nonce.bytes(length)) == length

wolfcrypt/random.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ class Random(object):
3131
A Cryptographically Secure Pseudo Random Number Generator - CSPRNG
3232
"""
3333

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

37-
ret = _lib.wc_InitRng(self.native_object)
37+
if nonce == _ffi.NULL:
38+
nonce_size = 0
39+
else:
40+
nonce_size = len(nonce)
41+
ret = _lib.wc_InitRngNonce_ex(self.native_object, nonce, nonce_size, _ffi.NULL, device_id)
3842
if ret < 0: # pragma: no cover
3943
self.native_object = None
4044
raise WolfCryptError("RNG init error (%d)" % ret)

0 commit comments

Comments
 (0)