Alex Gaynor | c574e75 | 2018-07-17 09:20:13 -0400 | [diff] [blame] | 1 | # 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 | |
| 5 | from __future__ import absolute_import, division, print_function |
| 6 | |
| 7 | import binascii |
| 8 | |
| 9 | import pytest |
| 10 | |
| 11 | from cryptography.exceptions import InvalidSignature, UnsupportedAlgorithm |
| 12 | from cryptography.hazmat.backends.interfaces import EllipticCurveBackend |
| 13 | from cryptography.hazmat.primitives import hashes, serialization |
| 14 | from 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 Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 23 | "SHA3-224": hashes.SHA3_224(), |
| 24 | "SHA3-256": hashes.SHA3_256(), |
| 25 | "SHA3-384": hashes.SHA3_384(), |
| 26 | "SHA3-512": hashes.SHA3_512(), |
Alex Gaynor | c574e75 | 2018-07-17 09:20:13 -0400 | [diff] [blame] | 27 | } |
| 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 Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 41 | "ecdsa_secp224r1_sha3_224_test.json", |
| 42 | "ecdsa_secp224r1_sha3_256_test.json", |
| 43 | "ecdsa_secp224r1_sha3_512_test.json", |
Alex Gaynor | c574e75 | 2018-07-17 09:20:13 -0400 | [diff] [blame] | 44 | "ecdsa_secp256k1_sha256_test.json", |
| 45 | "ecdsa_secp256k1_sha512_test.json", |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 46 | "ecdsa_secp256k1_sha3_256_test.json", |
| 47 | "ecdsa_secp256k1_sha3_512_test.json", |
Alex Gaynor | c574e75 | 2018-07-17 09:20:13 -0400 | [diff] [blame] | 48 | "ecdsa_secp256r1_sha256_test.json", |
| 49 | "ecdsa_secp256r1_sha512_test.json", |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 50 | "ecdsa_secp256r1_sha3_256_test.json", |
| 51 | "ecdsa_secp256r1_sha3_512_test.json", |
Alex Gaynor | c574e75 | 2018-07-17 09:20:13 -0400 | [diff] [blame] | 52 | "ecdsa_secp384r1_sha384_test.json", |
| 53 | "ecdsa_secp384r1_sha512_test.json", |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 54 | "ecdsa_secp384r1_sha3_384_test.json", |
| 55 | "ecdsa_secp384r1_sha3_512_test.json", |
Alex Gaynor | c574e75 | 2018-07-17 09:20:13 -0400 | [diff] [blame] | 56 | "ecdsa_secp521r1_sha512_test.json", |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 57 | "ecdsa_secp521r1_sha3_512_test.json", |
Alex Gaynor | c574e75 | 2018-07-17 09:20:13 -0400 | [diff] [blame] | 58 | ) |
| 59 | def 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 Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 65 | # 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 Gaynor | c574e75 | 2018-07-17 09:20:13 -0400 | [diff] [blame] | 68 | pytest.skip( |
| 69 | "unable to load key (curve {})".format( |
| 70 | wycheproof.testgroup["key"]["curve"] |
| 71 | ) |
| 72 | ) |
| 73 | digest = _DIGESTS[wycheproof.testgroup["sha"]] |
| 74 | |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 75 | 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 Gaynor | c574e75 | 2018-07-17 09:20:13 -0400 | [diff] [blame] | 80 | ): |
| 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 | ) |