summaryrefslogtreecommitdiff
path: root/tests/hazmat/primitives
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2018-12-12 08:08:27 +0800
committerAlex Gaynor <alex.gaynor@gmail.com>2018-12-11 19:08:27 -0500
commit4c5740a6747b78502f432b662024e5bf6a4ae8c4 (patch)
treec245a60d06c205cb9c744ea03aeb1b9172abe45e /tests/hazmat/primitives
parent0143367da8896d4c188df390ba3fad868b770d02 (diff)
downloadcryptography-4c5740a6747b78502f432b662024e5bf6a4ae8c4.tar.gz
Compressed point support (#4629)
* compressed point support * refactor to use oct2point directly * small docs change * remove deprecation for the moment and a bit of review feedback * no backend arg, implicitly import it * missed a spot * double oops * remove superfluous call * use refactored method * use vector file * one last item
Diffstat (limited to 'tests/hazmat/primitives')
-rw-r--r--tests/hazmat/primitives/test_ec.py76
1 files changed, 75 insertions, 1 deletions
diff --git a/tests/hazmat/primitives/test_ec.py b/tests/hazmat/primitives/test_ec.py
index 6d4936619..9a8ddf600 100644
--- a/tests/hazmat/primitives/test_ec.py
+++ b/tests/hazmat/primitives/test_ec.py
@@ -212,7 +212,7 @@ def test_from_encoded_point_invalid_length():
)
-def test_from_encoded_point_unsupported_point_type():
+def test_from_encoded_point_unsupported_point_no_backend():
# set to point type 2.
unsupported_type = binascii.unhexlify(
"02233ea3b0027127084cd2cd336a13aeef69c598d8af61369a36454a17c6c22a"
@@ -1009,6 +1009,80 @@ class TestEllipticCurvePEMPublicKeySerialization(object):
serialization.Encoding.PEM, serialization.PublicFormat.PKCS1
)
+ @pytest.mark.parametrize(
+ "vector",
+ load_vectors_from_file(
+ os.path.join("asymmetric", "EC", "compressed_points.txt"),
+ load_nist_vectors
+ )
+ )
+ def test_from_encoded_point_compressed(self, vector):
+ curve = {
+ b"SECP256R1": ec.SECP256R1(),
+ b"SECP256K1": ec.SECP256K1(),
+ }[vector["curve"]]
+ point = binascii.unhexlify(vector["point"])
+ pn = ec.EllipticCurvePublicKey.from_encoded_point(curve, point)
+ public_num = pn.public_numbers()
+ assert public_num.x == int(vector["x"], 16)
+ assert public_num.y == int(vector["y"], 16)
+
+ def test_from_encoded_point_notoncurve(self):
+ uncompressed_point = binascii.unhexlify(
+ "047399336a9edf2197c2f8eb3d39aed9c34a66e45d918a07dc7684c42c9b37ac"
+ "686699ececc4f5f0d756d3c450708a0694eb0a07a68b805070b40b058d27271f"
+ "6e"
+ )
+ with pytest.raises(ValueError):
+ ec.EllipticCurvePublicKey.from_encoded_point(
+ ec.SECP256R1(), uncompressed_point
+ )
+
+ def test_from_encoded_point_uncompressed(self):
+ uncompressed_point = binascii.unhexlify(
+ "047399336a9edf2197c2f8eb3d39aed9c34a66e45d918a07dc7684c42c9b37ac"
+ "686699ececc4f5f0d756d3c450708a0694eb0a07a68b805070b40b058d27271f"
+ "6d"
+ )
+ pn = ec.EllipticCurvePublicKey.from_encoded_point(
+ ec.SECP256R1(), uncompressed_point
+ )
+ assert pn.public_numbers().x == int(
+ '7399336a9edf2197c2f8eb3d39aed9c34a66e45d918a07dc7684c42c9b37ac68',
+ 16
+ )
+ assert pn.public_numbers().y == int(
+ '6699ececc4f5f0d756d3c450708a0694eb0a07a68b805070b40b058d27271f6d',
+ 16
+ )
+
+ def test_from_encoded_point_invalid_length(self):
+ bad_data = binascii.unhexlify(
+ "047399336a9edf2197c2f8eb3d39aed9c34a66e45d918a07dc7684c42c9b37ac"
+ "686699ececc4f5f0d756d3c450708a0694eb0a07a68b805070b40b058d27271f"
+ "6d"
+ )
+ with pytest.raises(ValueError):
+ ec.EllipticCurvePublicKey.from_encoded_point(
+ ec.SECP384R1(), bad_data
+ )
+
+ def test_from_encoded_point_not_a_curve(self):
+ with pytest.raises(TypeError):
+ ec.EllipticCurvePublicKey.from_encoded_point(
+ "notacurve", b"\x04data"
+ )
+
+ def test_from_encoded_point_unsupported_encoding(self):
+ unsupported_type = binascii.unhexlify(
+ "057399336a9edf2197c2f8eb3d39aed9c34a66e45d918a07dc7684c42c9b37ac6"
+ "8"
+ )
+ with pytest.raises(ValueError):
+ ec.EllipticCurvePublicKey.from_encoded_point(
+ ec.SECP256R1(), unsupported_type
+ )
+
@pytest.mark.requires_backend_interface(interface=EllipticCurveBackend)
class TestECDSAVerification(object):