Skip to content

Commit 1684984

Browse files
committed
Resolve comments by Copilot
1 parent 9a10f58 commit 1684984

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

tests/test_mldsa.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ def test_sign_verify(mldsa_type, rng):
135135
wrong_message = b"This is a wrong message for ML-DSA signature"
136136
assert not mldsa_pub.verify(signature, wrong_message)
137137

138-
# Verify with ctx for signature generated without
138+
# Verify a signature generated without a context but where a context
139+
# is provided during verify
139140
ctx = b"This is a test context for ML-DSA signature"
140141
wrong_ctx = b"This is a wrong context for ML-DSA signature"
141142
assert not mldsa_pub.verify(signature, message, ctx=wrong_ctx)
@@ -150,5 +151,8 @@ def test_sign_verify(mldsa_type, rng):
150151
# Verify the signature by MlDsaPublic
151152
assert mldsa_pub.verify(signature, message, ctx=ctx)
152153

153-
# Verify with wrong ctx
154+
# Verify but do not provide a context
155+
assert not mldsa_pub.verify(signature, message, ctx=None)
156+
157+
# Verify with wrong context
154158
assert not mldsa_pub.verify(signature, message, ctx=wrong_ctx)

wolfcrypt/ciphers.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)