@@ -2267,7 +2267,7 @@ def sign(self, message, rng=Random(), ctx=None):
22672267 :type message: bytes or str
22682268 :param rng: random number generator for sign
22692269 :type rng: Random
2270- :param ctx: context (optional)
2270+ :param ctx: context (optional, maximum 255 bytes )
22712271 :type ctx: None for no context, str or bytes otherwise
22722272 :return: signature
22732273 :rtype: bytes
@@ -2280,16 +2280,20 @@ def sign(self, message, rng=Random(), ctx=None):
22802280
22812281 if ctx is not None :
22822282 ctx_bytestype = t2b (ctx )
2283+ if len (ctx_bytestype ) > 255 :
2284+ raise ValueError (f"context length { len (ctx_bytestype )} too large: must be 255 bytes or less" )
22832285 ret = _lib .wc_dilithium_sign_ctx_msg (
22842286 _ffi .from_buffer (ctx_bytestype ),
2285- len (ctx_bytestype ),
2287+ len (ctx_bytestype ), # length must be < 256 bytes
22862288 _ffi .from_buffer (msg_bytestype ),
22872289 len (msg_bytestype ),
22882290 signature ,
22892291 out_size ,
22902292 self .native_object ,
22912293 rng .native_object ,
22922294 )
2295+ if ret < 0 : # pragma: no cover
2296+ raise WolfCryptError ("wc_dilithium_sign_ctx_msg() error (%d)" % ret )
22932297 else :
22942298 ret = _lib .wc_dilithium_sign_msg (
22952299 _ffi .from_buffer (msg_bytestype ),
@@ -2299,10 +2303,9 @@ def sign(self, message, rng=Random(), ctx=None):
22992303 self .native_object ,
23002304 rng .native_object ,
23012305 )
2302-
2303- if ret < 0 : # pragma: no cover
2304- raise WolfCryptError ("wc_dilithium_sign_msg() error (%d)" % ret )
2305-
2306+ if ret < 0 : # pragma: no cover
2307+ raise WolfCryptError ("wc_dilithium_sign_msg() error (%d)" % ret )
2308+
23062309 if in_size != out_size [0 ]:
23072310 raise WolfCryptError (
23082311 "in_size=%d and out_size=%d don't match" % (in_size , out_size [0 ])
0 commit comments