blob: 802bb9f00b3ef7ca8ad40b0b53feb1d95359e683 [file] [log] [blame]
Alex Gaynorc574e752018-07-17 09:20:13 -04001# This file is dual licensed under the terms of the Apache License, Version
2# 2.0, and the BSD License. See the LICENSE file in the root of this repository
3# for complete details.
4
5from __future__ import absolute_import, division, print_function
6
7import binascii
8
9import pytest
10
11from cryptography.exceptions import InvalidSignature, UnsupportedAlgorithm
12from cryptography.hazmat.backends.interfaces import EllipticCurveBackend
13from cryptography.hazmat.primitives import hashes, serialization
14from cryptography.hazmat.primitives.asymmetric import ec
15
16
17_DIGESTS = {
18 "SHA-1": hashes.SHA1(),
19 "SHA-224": hashes.SHA224(),
20 "SHA-256": hashes.SHA256(),
21 "SHA-384": hashes.SHA384(),
22 "SHA-512": hashes.SHA512(),
Lucia Lic6ba99d2021-11-08 22:06:11 +080023 "SHA3-224": hashes.SHA3_224(),
24 "SHA3-256": hashes.SHA3_256(),
25 "SHA3-384": hashes.SHA3_384(),
26 "SHA3-512": hashes.SHA3_512(),
Alex Gaynorc574e752018-07-17 09:20:13 -040027}
28
29
30@pytest.mark.requires_backend_interface(interface=EllipticCurveBackend)
31@pytest.mark.wycheproof_tests(
32 "ecdsa_test.json",
33 "ecdsa_brainpoolP224r1_sha224_test.json",
34 "ecdsa_brainpoolP256r1_sha256_test.json",
35 "ecdsa_brainpoolP320r1_sha384_test.json",
36 "ecdsa_brainpoolP384r1_sha384_test.json",
37 "ecdsa_brainpoolP512r1_sha512_test.json",
38 "ecdsa_secp224r1_sha224_test.json",
39 "ecdsa_secp224r1_sha256_test.json",
40 "ecdsa_secp224r1_sha512_test.json",
Lucia Lic6ba99d2021-11-08 22:06:11 +080041 "ecdsa_secp224r1_sha3_224_test.json",
42 "ecdsa_secp224r1_sha3_256_test.json",
43 "ecdsa_secp224r1_sha3_512_test.json",
Alex Gaynorc574e752018-07-17 09:20:13 -040044 "ecdsa_secp256k1_sha256_test.json",
45 "ecdsa_secp256k1_sha512_test.json",
Lucia Lic6ba99d2021-11-08 22:06:11 +080046 "ecdsa_secp256k1_sha3_256_test.json",
47 "ecdsa_secp256k1_sha3_512_test.json",
Alex Gaynorc574e752018-07-17 09:20:13 -040048 "ecdsa_secp256r1_sha256_test.json",
49 "ecdsa_secp256r1_sha512_test.json",
Lucia Lic6ba99d2021-11-08 22:06:11 +080050 "ecdsa_secp256r1_sha3_256_test.json",
51 "ecdsa_secp256r1_sha3_512_test.json",
Alex Gaynorc574e752018-07-17 09:20:13 -040052 "ecdsa_secp384r1_sha384_test.json",
53 "ecdsa_secp384r1_sha512_test.json",
Lucia Lic6ba99d2021-11-08 22:06:11 +080054 "ecdsa_secp384r1_sha3_384_test.json",
55 "ecdsa_secp384r1_sha3_512_test.json",
Alex Gaynorc574e752018-07-17 09:20:13 -040056 "ecdsa_secp521r1_sha512_test.json",
Lucia Lic6ba99d2021-11-08 22:06:11 +080057 "ecdsa_secp521r1_sha3_512_test.json",
Alex Gaynorc574e752018-07-17 09:20:13 -040058)
59def test_ecdsa_signature(backend, wycheproof):
60 try:
61 key = serialization.load_der_public_key(
62 binascii.unhexlify(wycheproof.testgroup["keyDer"]), backend
63 )
64 except (UnsupportedAlgorithm, ValueError):
Lucia Lic6ba99d2021-11-08 22:06:11 +080065 # In some OpenSSL 1.0.2s, some keys fail to load with ValueError,
66 # instead of Unsupported Algorithm. We can remove handling for that
67 # exception when we drop support.
Alex Gaynorc574e752018-07-17 09:20:13 -040068 pytest.skip(
69 "unable to load key (curve {})".format(
70 wycheproof.testgroup["key"]["curve"]
71 )
72 )
73 digest = _DIGESTS[wycheproof.testgroup["sha"]]
74
Lucia Lic6ba99d2021-11-08 22:06:11 +080075 if not backend.hash_supported(digest):
76 pytest.skip("Hash {} not supported".format(digest))
77
78 if wycheproof.valid or (
79 wycheproof.acceptable and not wycheproof.has_flag("MissingZero")
Alex Gaynorc574e752018-07-17 09:20:13 -040080 ):
81 key.verify(
82 binascii.unhexlify(wycheproof.testcase["sig"]),
83 binascii.unhexlify(wycheproof.testcase["msg"]),
84 ec.ECDSA(digest),
85 )
86 else:
87 with pytest.raises(InvalidSignature):
88 key.verify(
89 binascii.unhexlify(wycheproof.testcase["sig"]),
90 binascii.unhexlify(wycheproof.testcase["msg"]),
91 ec.ECDSA(digest),
92 )