Skip to content

Commit 3d351f2

Browse files
committed
ML-DSA-44: add wycheproof test vectors
Signed-off-by: Alexander Bokovoy <abokovoy@redhat.com>
1 parent 68dbd10 commit 3d351f2

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

tests/wycheproof/test_mldsa.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,77 @@
77
import pytest
88

99
from cryptography.exceptions import InvalidSignature
10+
from cryptography.hazmat.primitives import serialization
1011
from cryptography.hazmat.primitives.asymmetric.mldsa import (
12+
MlDsa44PrivateKey,
13+
MlDsa44PublicKey,
1114
MlDsa65PrivateKey,
1215
MlDsa65PublicKey,
1316
)
1417

1518
from .utils import wycheproof_tests
1619

1720

21+
@pytest.mark.supported(
22+
only_if=lambda backend: backend.mldsa44_supported(),
23+
skip_message="Requires OpenSSL with ML-DSA-44 support",
24+
)
25+
@wycheproof_tests("mldsa_44_sign_seed_test.json")
26+
def test_mldsa44_signature(backend, wycheproof):
27+
if wycheproof.has_flag("Internal"):
28+
alg = getattr(wycheproof.testfiledata, "algorithm", None)
29+
pytest.skip(f"Internal implementation test for {alg}")
30+
31+
assert wycheproof.testgroup["type"] == "MlDsaSign"
32+
33+
seed = binascii.unhexlify(wycheproof.testgroup["privateSeed"])
34+
try:
35+
private_key = MlDsa44PrivateKey.from_seed_bytes(seed)
36+
except ValueError:
37+
assert wycheproof.invalid
38+
assert wycheproof.has_flag("IncorrectPrivateKeyLength")
39+
return
40+
try:
41+
public_key = MlDsa44PublicKey.from_public_bytes(
42+
binascii.unhexlify(wycheproof.testgroup["publicKey"])
43+
)
44+
except ValueError:
45+
assert wycheproof.invalid
46+
assert wycheproof.has_flag("IncorrectPublicKeyLength")
47+
return
48+
49+
pkey_pkcs8 = wycheproof.testgroup.get("privateKeyPkcs8", None)
50+
if pkey_pkcs8 is not None:
51+
serialization.load_der_private_key(
52+
binascii.unhexlify(pkey_pkcs8), None
53+
)
54+
55+
testkey = private_key.public_key()
56+
57+
assert public_key.public_bytes(
58+
serialization.Encoding.Raw, serialization.PublicFormat.Raw
59+
) == testkey.public_bytes(
60+
serialization.Encoding.Raw, serialization.PublicFormat.Raw
61+
)
62+
63+
msg = binascii.unhexlify(wycheproof.testcase["msg"])
64+
expected_sig = binascii.unhexlify(wycheproof.testcase["sig"])
65+
context = wycheproof.testcase.get("ctx", None)
66+
if wycheproof.valid:
67+
if context is not None:
68+
context = binascii.unhexlify(context)
69+
testkey.verify_with_context(expected_sig, msg, context)
70+
else:
71+
public_key.verify(expected_sig, msg)
72+
else:
73+
with pytest.raises(InvalidSignature):
74+
if context is not None:
75+
context = binascii.unhexlify(context)
76+
testkey.verify_with_context(expected_sig, msg, context)
77+
else:
78+
public_key.verify(expected_sig, msg)
79+
80+
1881
@pytest.mark.supported(
1982
only_if=lambda backend: backend.mldsa_supported(),
2083
skip_message="Requires a backend with ML-DSA-65 support",

0 commit comments

Comments
 (0)