diff options
Diffstat (limited to 'tests/hazmat/primitives/test_ec.py')
-rw-r--r-- | tests/hazmat/primitives/test_ec.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/tests/hazmat/primitives/test_ec.py b/tests/hazmat/primitives/test_ec.py index 65461f70..c53a0cb6 100644 --- a/tests/hazmat/primitives/test_ec.py +++ b/tests/hazmat/primitives/test_ec.py @@ -20,6 +20,7 @@ import os import pytest from cryptography import exceptions, utils +from cryptography.hazmat.backends.interfaces import EllipticCurveBackend from cryptography.hazmat.primitives import hashes, interfaces from cryptography.hazmat.primitives.asymmetric import ec @@ -70,6 +71,15 @@ class DummySignatureAlgorithm(object): pass +@utils.register_interface(EllipticCurveBackend) +class DeprecatedDummyECBackend(object): + def elliptic_curve_private_key_from_numbers(self, numbers): + return b"private_key" + + def elliptic_curve_public_key_from_numbers(self, numbers): + return b"public_key" + + @pytest.mark.elliptic def test_skip_curve_unsupported(backend): with pytest.raises(pytest.skip.Exception): @@ -129,6 +139,42 @@ def test_ec_numbers(): @pytest.mark.elliptic +class TestECWithNumbers(object): + @pytest.mark.parametrize( + ("vector", "hash_type"), + list(itertools.product( + load_vectors_from_file( + os.path.join( + "asymmetric", "ECDSA", "FIPS_186-3", "KeyPair.rsp"), + load_fips_ecdsa_key_pair_vectors + ), + _HASH_TYPES.values() + )) + ) + def test_with_numbers(self, backend, vector, hash_type): + curve_type = ec._CURVE_TYPES[vector['curve']] + + _skip_ecdsa_vector(backend, curve_type, hash_type) + + key = ec.EllipticCurvePrivateNumbers( + vector['d'], + ec.EllipticCurvePublicNumbers( + vector['x'], + vector['y'], + curve_type() + ) + ).private_key(backend) + assert key + + if isinstance(key, interfaces.EllipticCurvePrivateKeyWithNumbers): + priv_num = key.private_numbers() + assert priv_num.private_value == vector['d'] + assert priv_num.public_numbers.x == vector['x'] + assert priv_num.public_numbers.y == vector['y'] + assert curve_type().name == priv_num.public_numbers.curve.name + + +@pytest.mark.elliptic class TestECDSAVectors(object): @pytest.mark.parametrize( ("vector", "hash_type"), @@ -282,3 +328,14 @@ class TestECDSAVectors(object): verifier.verify() else: verifier.verify() + + def test_deprecated_public_private_key_load(self): + b = DeprecatedDummyECBackend() + pub_numbers = ec.EllipticCurvePublicNumbers( + 2, + 3, + ec.SECT283K1() + ) + numbers = ec.EllipticCurvePrivateNumbers(1, pub_numbers) + assert numbers.private_key(b) == b"private_key" + assert pub_numbers.public_key(b) == b"public_key" |