|
| 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 UnsupportedAlgorithm |
| 12 | +from cryptography.hazmat.backends.interfaces import EllipticCurveBackend |
| 13 | +from cryptography.hazmat.primitives import serialization |
| 14 | +from cryptography.hazmat.primitives.asymmetric import ec |
| 15 | + |
| 16 | +from ..hazmat.primitives.test_ec import _skip_exchange_algorithm_unsupported |
| 17 | + |
| 18 | + |
| 19 | +_CURVES = { |
| 20 | + "secp224r1": ec.SECP224R1(), |
| 21 | + "secp256r1": ec.SECP256R1(), |
| 22 | + "secp384r1": ec.SECP384R1(), |
| 23 | + "secp521r1": ec.SECP521R1(), |
| 24 | + "secp256k1": ec.SECP256K1(), |
| 25 | + "brainpoolP224r1": None, |
| 26 | + "brainpoolP256r1": ec.BrainpoolP256R1(), |
| 27 | + "brainpoolP320r1": None, |
| 28 | + "brainpoolP384r1": ec.BrainpoolP384R1(), |
| 29 | + "brainpoolP512r1": ec.BrainpoolP512R1(), |
| 30 | + "brainpoolP224t1": None, |
| 31 | + "brainpoolP256t1": None, |
| 32 | + "brainpoolP320t1": None, |
| 33 | + "brainpoolP384t1": None, |
| 34 | + "brainpoolP512t1": None, |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.requires_backend_interface(interface=EllipticCurveBackend) |
| 39 | +@pytest.mark.wycheproof_tests( |
| 40 | + "ecdh_test.json", |
| 41 | + "ecdh_brainpoolP224r1_test.json", |
| 42 | + "ecdh_brainpoolP256r1_test.json", |
| 43 | + "ecdh_brainpoolP320r1_test.json", |
| 44 | + "ecdh_brainpoolP384r1_test.json", |
| 45 | + "ecdh_brainpoolP512r1_test.json", |
| 46 | + "ecdh_secp224r1_test.json", |
| 47 | + "ecdh_secp256k1_test.json", |
| 48 | + "ecdh_secp256r1_test.json", |
| 49 | + "ecdh_secp384r1_test.json", |
| 50 | + "ecdh_secp521r1_test.json", |
| 51 | +) |
| 52 | +def test_ecdh(backend, wycheproof): |
| 53 | + curve = _CURVES[wycheproof.testcase["curve"]] |
| 54 | + if curve is None: |
| 55 | + pytest.skip( |
| 56 | + "Unsupported curve ({})".format(wycheproof.testcase["curve"]) |
| 57 | + ) |
| 58 | + _skip_exchange_algorithm_unsupported(backend, ec.ECDH(), curve) |
| 59 | + |
| 60 | + private_key = ec.derive_private_key( |
| 61 | + int(wycheproof.testcase["private"], 16), curve, backend |
| 62 | + ) |
| 63 | + |
| 64 | + try: |
| 65 | + public_key = serialization.load_der_public_key( |
| 66 | + binascii.unhexlify(wycheproof.testcase["public"]), backend |
| 67 | + ) |
| 68 | + except NotImplementedError: |
| 69 | + assert wycheproof.has_flag("UnnamedCurve") |
| 70 | + return |
| 71 | + except ValueError: |
| 72 | + assert wycheproof.invalid or wycheproof.acceptable |
| 73 | + return |
| 74 | + except UnsupportedAlgorithm: |
| 75 | + return |
| 76 | + |
| 77 | + if wycheproof.valid or wycheproof.acceptable: |
| 78 | + computed_shared = private_key.exchange(ec.ECDH(), public_key) |
| 79 | + expected_shared = binascii.unhexlify(wycheproof.testcase["shared"]) |
| 80 | + assert computed_shared == expected_shared |
| 81 | + else: |
| 82 | + with pytest.raises(ValueError): |
| 83 | + private_key.exchange(ec.ECDH(), public_key) |
0 commit comments