aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorAlex Gaynor <alex.gaynor@gmail.com>2015-10-27 22:17:13 -0400
committerAlex Gaynor <alex.gaynor@gmail.com>2015-10-27 22:17:13 -0400
commitd3f4ecdbefe20f4f9cb8b74701c5757557e476e1 (patch)
tree7bd4e100c3536859936c6453208f956719f1d7c5 /tests
parentcab8db3e9535a3900602931cb84d3f273f685d47 (diff)
parent197db3367ceecdbaf0adc5eaa9339b934b10acfe (diff)
downloadcryptography-d3f4ecdbefe20f4f9cb8b74701c5757557e476e1.tar.gz
cryptography-d3f4ecdbefe20f4f9cb8b74701c5757557e476e1.tar.bz2
cryptography-d3f4ecdbefe20f4f9cb8b74701c5757557e476e1.zip
Merge pull request #2447 from reaperhulk/encode-decode-point
add support for encoding/decoding elliptic curve points
Diffstat (limited to 'tests')
-rw-r--r--tests/hazmat/primitives/test_ec.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/tests/hazmat/primitives/test_ec.py b/tests/hazmat/primitives/test_ec.py
index 4c4d5b90..5baaa3cd 100644
--- a/tests/hazmat/primitives/test_ec.py
+++ b/tests/hazmat/primitives/test_ec.py
@@ -4,6 +4,7 @@
from __future__ import absolute_import, division, print_function
+import binascii
import itertools
import os
@@ -148,6 +149,72 @@ def test_ec_numbers():
)
+def test_encode_point():
+ # secp256r1 point
+ x = int(
+ '233ea3b0027127084cd2cd336a13aeef69c598d8af61369a36454a17c6c22aec',
+ 16
+ )
+ y = int(
+ '3ea2c10a84153862be4ec82940f0543f9ba866af9751a6ee79d38460b35f442e',
+ 16
+ )
+ pn = ec.EllipticCurvePublicNumbers(x, y, ec.SECP256R1())
+ data = pn.encode_point()
+ assert data == binascii.unhexlify(
+ "04233ea3b0027127084cd2cd336a13aeef69c598d8af61369a36454a17c6c22ae"
+ "c3ea2c10a84153862be4ec82940f0543f9ba866af9751a6ee79d38460b35f442e"
+ )
+
+
+def test_from_encoded_point():
+ # secp256r1 point
+ data = binascii.unhexlify(
+ "04233ea3b0027127084cd2cd336a13aeef69c598d8af61369a36454a17c6c22ae"
+ "c3ea2c10a84153862be4ec82940f0543f9ba866af9751a6ee79d38460b35f442e"
+ )
+ pn = ec.EllipticCurvePublicNumbers.from_encoded_point(
+ ec.SECP256R1(), data
+ )
+ assert pn.x == int(
+ '233ea3b0027127084cd2cd336a13aeef69c598d8af61369a36454a17c6c22aec',
+ 16
+ )
+ assert pn.y == int(
+ '3ea2c10a84153862be4ec82940f0543f9ba866af9751a6ee79d38460b35f442e',
+ 16
+ )
+
+
+def test_from_encoded_point_invalid_length():
+ bad_data = binascii.unhexlify(
+ "04233ea3b0027127084cd2cd336a13aeef69c598d8af61369a36454a17c6c22ae"
+ "c3ea2c10a84153862be4ec82940f0543f9ba866af9751a6ee79d38460"
+ )
+ with pytest.raises(ValueError):
+ ec.EllipticCurvePublicNumbers.from_encoded_point(
+ ec.SECP384R1(), bad_data
+ )
+
+
+def test_from_encoded_point_unsupported_point_type():
+ # set to point type 2.
+ unsupported_type = binascii.unhexlify(
+ "02233ea3b0027127084cd2cd336a13aeef69c598d8af61369a36454a17c6c22a"
+ )
+ with pytest.raises(ValueError):
+ ec.EllipticCurvePublicNumbers.from_encoded_point(
+ ec.SECP256R1(), unsupported_type
+ )
+
+
+def test_from_encoded_point_not_a_curve():
+ with pytest.raises(TypeError):
+ ec.EllipticCurvePublicNumbers.from_encoded_point(
+ "notacurve", b"\x04data"
+ )
+
+
@pytest.mark.requires_backend_interface(interface=EllipticCurveBackend)
class TestECWithNumbers(object):
@pytest.mark.parametrize(