Keep reference to random number generator in EccPrivate.

Add a test for the case where no random number generator is passed
to EccPrivate.
pull/101/head
Robert de Vries 2026-04-15 16:58:02 +02:00
parent 26203d77d2
commit b4ddce17de
2 changed files with 19 additions and 16 deletions

View File

@ -614,12 +614,11 @@ if _lib.ECC_ENABLED:
def test_ecc_make_shared_secret():
rng = Random()
a = EccPrivate.make_key(32, rng=rng)
a = EccPrivate.make_key(32, rng=Random())
a_pub = EccPublic()
a_pub.import_x963(a.export_x963())
b = EccPrivate.make_key(32, rng=rng)
b = EccPrivate.make_key(32, rng=Random())
b_pub = EccPublic()
b_pub.import_x963(b.export_x963())
@ -628,6 +627,13 @@ if _lib.ECC_ENABLED:
== a.shared_secret(b_pub) \
== b.shared_secret(a_pub)
def test_ecc_make_key_no_rng():
key = EccPrivate.make_key(32)
pub_key = EccPublic()
pub_key.import_x963(key.export_x963())
assert key.shared_secret(pub_key)
if _lib.ED25519_ENABLED:
@pytest.fixture
def ed25519_private(vectors):

View File

@ -1229,31 +1229,30 @@ if _lib.ECC_ENABLED:
class EccPrivate(EccPublic):
def __init__(self, key=None, rng=None):
super().__init__(key)
if rng is None:
rng = Random()
self._rng = rng
@classmethod
def make_key(cls, size, rng=None):
"""
Generates a new key pair of desired length **size**.
"""
if rng is None:
rng = Random()
ecc = cls()
ret = _lib.wc_ecc_make_key(rng.native_object, size,
ecc = cls(rng=rng)
ret = _lib.wc_ecc_make_key(ecc._rng.native_object, size,
ecc.native_object)
if ret < 0:
raise WolfCryptError("Key generation error (%d)" % ret)
if _lib.ECC_TIMING_RESISTANCE_ENABLED and (not _lib.FIPS_ENABLED or
_lib.FIPS_VERSION > 2):
ret = _lib.wc_ecc_set_rng(ecc.native_object, rng.native_object)
ret = _lib.wc_ecc_set_rng(ecc.native_object, ecc._rng.native_object)
if ret < 0:
raise WolfCryptError("Error setting ECC RNG (%d)" % ret)
# Retain the RNG so it outlives the ECC key. Even outside the
# timing-resistance path, wolfSSL internals may retain a pointer
# to the RNG; keeping the reference avoids any UAF risk.
ecc._rng = rng
return ecc
def decode_key(self, key):
@ -2450,8 +2449,6 @@ if _lib.ML_DSA_ENABLED:
:return: signature
:rtype: bytes
"""
if rng is None:
rng = Random()
msg_bytestype = t2b(message)
in_size = self.sig_size
signature = _ffi.new(f"byte[{in_size}]")