diff --git a/ChangeLog.rst b/ChangeLog.rst index 61eb4a2..6894a65 100644 --- a/ChangeLog.rst +++ b/ChangeLog.rst @@ -28,7 +28,8 @@ wolfCrypt-py Release 5.9.2 (Jul 1, 2026) * Address many small issues found by Fenrir * Add reseed support to random number generator * The RsaPublic key parameter is now mandatory as it is always needed by an internal function call. -* Add typing annotations +* The `native_object` attribute of `Random` is now read-only. +* Add typing annotations. wolfCrypt-py Release 5.8.4 (Jan 7, 2026) diff --git a/tests/test_mldsa.py b/tests/test_mldsa.py index 7a24ab4..154e457 100644 --- a/tests/test_mldsa.py +++ b/tests/test_mldsa.py @@ -226,6 +226,14 @@ if _lib.ML_DSA_ENABLED: signature_from_same_seed = mldsa_priv.sign_with_seed(message, signature_seed, ctx=context) assert signature == signature_from_same_seed + @pytest.mark.parametrize("seed", [0, "seed"]) + def test_sign_with_seed_bad_type(mldsa_type, rng, seed: int | str): + mldsa_priv = MlDsaPrivate.make_key(mldsa_type, rng) + message = b"This is a test message for ML-DSA signature" + context = b"Some context for the signature" + with pytest.raises(TypeError): + mldsa_priv.sign_with_seed(message, seed, ctx=context) + def test_make_key_from_seed(mldsa_type): seed = bytes(MlDsaPrivate.ML_DSA_KEYGEN_SEED_LENGTH) assert MlDsaPrivate.make_key_from_seed(mldsa_type, seed) @@ -237,3 +245,8 @@ if _lib.ML_DSA_ENABLED: seed = bytes(seed_length) with pytest.raises(ValueError): MlDsaPrivate.make_key_from_seed(mldsa_type, seed) + + @pytest.mark.parametrize("seed", [0, "seed"]) + def test_make_key_from_seed_bad_type(mldsa_type, seed: int | str): + with pytest.raises(TypeError): + MlDsaPrivate.make_key_from_seed(mldsa_type, seed) diff --git a/tests/test_mlkem.py b/tests/test_mlkem.py index 515b2f6..a37c241 100644 --- a/tests/test_mlkem.py +++ b/tests/test_mlkem.py @@ -617,6 +617,21 @@ if _lib.ML_KEM_ENABLED: ss_recv = mlkem_priv.decapsulate(ct) assert ss_send == ss_recv + @pytest.mark.parametrize("mlkem_type", mlkem_types) + @pytest.mark.parametrize("rand", [0, "rand"]) + def test_make_key_with_random_bad_random_type(mlkem_type, rand: int | str): + with pytest.raises(TypeError): + MlKemPrivate.make_key_with_random(mlkem_type, rand) + + @pytest.mark.parametrize("mlkem_type", mlkem_types) + @pytest.mark.parametrize("rand", [0, "rand"]) + def test_encapsulate_with_random_bad_random_type(mlkem_type, rand: int | str): + mlkem_pub = MlKemPublic(mlkem_type) + assert type(mlkem_pub) is MlKemPublic + + with pytest.raises(TypeError): + mlkem_pub.encapsulate_with_random(rand) + @pytest.mark.parametrize("mlkem_type", mlkem_types) def test_size_properties(mlkem_type): refvals = { diff --git a/wolfcrypt/ciphers.py b/wolfcrypt/ciphers.py index 61a9b21..2b5fa9d 100644 --- a/wolfcrypt/ciphers.py +++ b/wolfcrypt/ciphers.py @@ -174,7 +174,7 @@ class _Cipher(ABC): @property @abstractmethod - def key_size(self) -> int: ... + def key_size(self) -> int | None: ... @property @abstractmethod @@ -2148,6 +2148,14 @@ if _lib.ML_KEM_ENABLED: :rtype: MlKemPrivate """ mlkem_priv = cls(mlkem_type) + + try: + memoryview(rand) + except TypeError as exception: + raise TypeError("rand must support the buffer protocol, such as `bytes` or `bytearray`") from exception + + rand = bytes(rand) + ret = _lib.wc_KyberKey_MakeKeyWithRandom(mlkem_priv.native_object, rand, len(rand)) if ret < 0: # pragma: no cover diff --git a/wolfcrypt/hashes.py b/wolfcrypt/hashes.py index 737bcc2..f1386a0 100644 --- a/wolfcrypt/hashes.py +++ b/wolfcrypt/hashes.py @@ -69,7 +69,7 @@ class _Hash(ABC): @property @abstractmethod - def digest_size(self) -> int: ... + def digest_size(self) -> int | None: ... @classmethod @abstractmethod