Skip to content

Commit e5242a8

Browse files
authored
Merge pull request #89 from mjdemilliano/ml-dsa-generate-from-seed
ML-DSA: Support (re-)generating MlDsaPrivate from seed
2 parents ac6eee4 + 1de8b0c commit e5242a8

3 files changed

Lines changed: 31 additions & 1 deletion

File tree

scripts/build_ffi.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,7 @@ def build_ffi(local_wolfssl, features):
10271027
int wc_dilithium_set_level(dilithium_key* key, byte level);
10281028
void wc_dilithium_free(dilithium_key* key);
10291029
int wc_dilithium_make_key(dilithium_key* key, WC_RNG* rng);
1030+
int wc_dilithium_make_key_from_seed(dilithium_key* key, const byte* seed);
10301031
int wc_dilithium_export_private(dilithium_key* key, byte* out, word32* outLen);
10311032
int wc_dilithium_import_private(const byte* priv, word32 privSz, dilithium_key* key);
10321033
int wc_dilithium_export_public(dilithium_key* key, byte* out, word32* outLen);

tests/test_mldsa.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
if _lib.ML_DSA_ENABLED:
2626
import pytest
27-
2827
from wolfcrypt.ciphers import MlDsaPrivate, MlDsaPublic, MlDsaType, ML_DSA_SIGNATURE_SEED_LENGTH
2928
from wolfcrypt.random import Random
3029

wolfcrypt/ciphers.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,6 +2173,36 @@ def make_key(cls, mldsa_type, rng=Random()):
21732173

21742174
return mldsa_priv
21752175

2176+
@classmethod
2177+
def make_key_from_seed(cls, mldsa_type, seed):
2178+
"""
2179+
Deterministically generate the key from a seed.
2180+
2181+
:param mldsa_type: ML-DSA type
2182+
:type mldsa_type: MlDsaType
2183+
:param seed: the (32 byte) seed from which to deterministically create the key
2184+
:type seed: bytes
2185+
"""
2186+
mldsa_priv = cls(mldsa_type)
2187+
try:
2188+
seed_view = memoryview(seed)
2189+
except TypeError as exception:
2190+
raise TypeError(
2191+
"seed must support the buffer protocol, such as `bytes` or `bytearray`"
2192+
) from exception
2193+
if len(seed_view) != ML_DSA_KEYGEN_SEED_LENGTH:
2194+
raise ValueError(
2195+
f"Seed for generating ML-DSA key must be {ML_DSA_KEYGEN_SEED_LENGTH} bytes"
2196+
)
2197+
2198+
ret = _lib.wc_dilithium_make_key_from_seed(mldsa_priv.native_object,
2199+
_ffi.from_buffer(seed_view))
2200+
2201+
if ret < 0: # pragma: no cover
2202+
raise WolfCryptError("wc_dilithium_make_key_from_seed() error (%d)" % ret)
2203+
2204+
return mldsa_priv
2205+
21762206
@property
21772207
def pub_key_size(self):
21782208
"""

0 commit comments

Comments
 (0)