@@ -2031,6 +2031,9 @@ def decapsulate(self, ct):
20312031
20322032
20332033if _lib .ML_DSA_ENABLED :
2034+ ML_DSA_SIGNATURE_SEED_LENGTH = 32
2035+ """The length of a signature generation seed."""
2036+
20342037 class MlDsaType (IntEnum ):
20352038 """
20362039 `MlDsaType` specifies supported ML-DSA types.
@@ -2152,9 +2155,7 @@ def verify(self, signature, message):
21522155 return res [0 ] == 1
21532156
21542157 class MlDsaPrivate (_MlDsaBase ):
2155- _SIGNATURE_SEED_LENGTH = 32
2156- """The length of a signature generation seed."""
2157-
2158+
21582159 @classmethod
21592160 def make_key (cls , mldsa_type , rng = Random ()):
21602161 """
@@ -2289,7 +2290,7 @@ def sign_with_seed(self, message, seed, ctx=None):
22892290 :type message: bytes or str
22902291 :param seed: 32-byte seed for deterministic signature generation.
22912292 :type seed: bytes
2292- :param ctx: context (optional)
2293+ :param ctx: context (optional, maximum 255 bytes )
22932294 :type ctx: None for no context, str or bytes otherwise
22942295 :return: signature
22952296 :rtype: bytes
@@ -2300,20 +2301,33 @@ def sign_with_seed(self, message, seed, ctx=None):
23002301 out_size = _ffi .new ("word32 *" )
23012302 out_size [0 ] = in_size
23022303
2303- assert isinstance (seed , bytes ) and len (seed ) == MlDsaPrivate ._SIGNATURE_SEED_LENGTH , \
2304- f"Seed for generating a signature must be { MlDsaPrivate ._SIGNATURE_SEED_LENGTH } bytes."
2304+ try :
2305+ seed_view = memoryview (seed )
2306+ except TypeError as exception :
2307+ raise TypeError (
2308+ "seed must support the buffer protocol, such as `bytes` or `bytearray`"
2309+ ) from exception
2310+ if len (seed_view ) != ML_DSA_SIGNATURE_SEED_LENGTH :
2311+ raise ValueError (
2312+ f"Seed for generating a signature must be { ML_DSA_SIGNATURE_SEED_LENGTH } "
2313+ "bytes."
2314+ )
23052315
23062316 if ctx is not None :
23072317 ctx_bytestype = t2b (ctx )
2318+ if len (ctx_bytestype ) > 255 :
2319+ raise ValueError (
2320+ f"context length { len (ctx_bytestype )} too large: must be 255 or less"
2321+ )
23082322 ret = _lib .wc_dilithium_sign_ctx_msg_with_seed (
23092323 _ffi .from_buffer (ctx_bytestype ),
2310- len (ctx_bytestype ),
2324+ len (ctx_bytestype ), # length must be < 256 bytes
23112325 _ffi .from_buffer (msg_bytestype ),
23122326 len (msg_bytestype ),
23132327 signature ,
23142328 out_size ,
23152329 self .native_object ,
2316- _ffi .from_buffer (seed ),
2330+ _ffi .from_buffer (seed_view ),
23172331 )
23182332 if ret < 0 : # pragma: no cover
23192333 raise WolfCryptError ("wc_dilithium_sign_ctx_msg_with_seed() error (%d)" % ret )
@@ -2324,7 +2338,7 @@ def sign_with_seed(self, message, seed, ctx=None):
23242338 signature ,
23252339 out_size ,
23262340 self .native_object ,
2327- _ffi .from_buffer (seed ),
2341+ _ffi .from_buffer (seed_view ),
23282342 )
23292343 if ret < 0 : # pragma: no cover
23302344 raise WolfCryptError ("wc_dilithium_sign_msg_with_seed() error (%d)" % ret )
0 commit comments