|
7 | 7 | import pytest |
8 | 8 |
|
9 | 9 | from cryptography.exceptions import InvalidSignature |
| 10 | +from cryptography.hazmat.primitives import serialization |
10 | 11 | from cryptography.hazmat.primitives.asymmetric.mldsa import ( |
| 12 | + MlDsa44PrivateKey, |
| 13 | + MlDsa44PublicKey, |
11 | 14 | MlDsa65PrivateKey, |
12 | 15 | MlDsa65PublicKey, |
13 | 16 | ) |
14 | 17 |
|
15 | 18 | from .utils import wycheproof_tests |
16 | 19 |
|
17 | 20 |
|
| 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 | + |
18 | 81 | @pytest.mark.supported( |
19 | 82 | only_if=lambda backend: backend.mldsa_supported(), |
20 | 83 | skip_message="Requires a backend with ML-DSA-65 support", |
|
0 commit comments