aboutsummaryrefslogtreecommitdiffstats
path: root/src/cryptography/hazmat/primitives/asymmetric
diff options
context:
space:
mode:
Diffstat (limited to 'src/cryptography/hazmat/primitives/asymmetric')
-rw-r--r--src/cryptography/hazmat/primitives/asymmetric/ec.py28
-rw-r--r--src/cryptography/hazmat/primitives/asymmetric/utils.py34
2 files changed, 28 insertions, 34 deletions
diff --git a/src/cryptography/hazmat/primitives/asymmetric/ec.py b/src/cryptography/hazmat/primitives/asymmetric/ec.py
index c6f83667..f25ea6de 100644
--- a/src/cryptography/hazmat/primitives/asymmetric/ec.py
+++ b/src/cryptography/hazmat/primitives/asymmetric/ec.py
@@ -259,6 +259,34 @@ class EllipticCurvePublicNumbers(object):
def public_key(self, backend):
return backend.load_elliptic_curve_public_numbers(self)
+ def encode_point(self):
+ # key_size is in bits. Convert to bytes and round up
+ byte_length = (self.curve.key_size + 7) // 8
+ return (
+ b'\x04' + utils.int_to_bytes(self.x, byte_length) +
+ utils.int_to_bytes(self.y, byte_length)
+ )
+
+ @classmethod
+ def from_encoded_point(cls, curve, data):
+ if not isinstance(curve, EllipticCurve):
+ raise TypeError("curve must be an EllipticCurve instance")
+
+ if data == b'\x00':
+ raise ValueError("null points are not supported")
+ elif data.startswith(b'\x04'):
+ # key_size is in bits. Convert to bytes and round up
+ byte_length = (curve.key_size + 7) // 8
+ if len(data) == 2 * byte_length + 1:
+ x = utils.int_from_bytes(data[1:byte_length + 1], 'big')
+ y = utils.int_from_bytes(data[byte_length + 1:], 'big')
+ else:
+ raise ValueError('Invalid elliptic curve point data length')
+ else:
+ raise ValueError('Unsupported elliptic curve point type')
+
+ return cls(x, y, curve)
+
curve = utils.read_only_property("_curve")
x = utils.read_only_property("_x")
y = utils.read_only_property("_y")
diff --git a/src/cryptography/hazmat/primitives/asymmetric/utils.py b/src/cryptography/hazmat/primitives/asymmetric/utils.py
index 57dea41a..bad9ab73 100644
--- a/src/cryptography/hazmat/primitives/asymmetric/utils.py
+++ b/src/cryptography/hazmat/primitives/asymmetric/utils.py
@@ -13,7 +13,6 @@ from pyasn1.type import namedtype, univ
import six
from cryptography import utils
-from cryptography.hazmat.primitives.asymmetric.ec import EllipticCurve
class _DSSSigValue(univ.Sequence):
@@ -72,36 +71,3 @@ def encode_dss_signature(r, s):
sig.setComponentByName('r', r)
sig.setComponentByName('s', s)
return encoder.encode(sig)
-
-
-def encode_ec_point(curve, x, y):
- if not isinstance(curve, EllipticCurve):
- raise TypeError("curve must be an EllipticCurve instance")
-
- if x is None:
- raise ValueError("null points are not supported")
- else:
- # key_size is in bits. Convert to bytes and round up
- byte_length = (curve.key_size + 7) // 8
- return (
- b'\x04' + utils.int_to_bytes(x, byte_length) +
- utils.int_to_bytes(y, byte_length)
- )
-
-
-def decode_ec_point(curve, data):
- if not isinstance(curve, EllipticCurve):
- raise TypeError("curve must be an EllipticCurve instance")
-
- if data == b'\x00':
- raise ValueError("null points are not supported")
- elif data.startswith(b'\x04'):
- # key_size is in bits. Convert to bytes and round up
- byte_length = (curve.key_size + 7) // 8
- if len(data) == 2 * byte_length + 1:
- return (utils.int_from_bytes(data[1:byte_length + 1], 'big'),
- utils.int_from_bytes(data[byte_length + 1:], 'big'))
- else:
- raise ValueError('Invalid elliptic curve point data length')
- else:
- raise ValueError('Unsupported elliptic curve point type')