diff options
Diffstat (limited to 'src/cryptography/hazmat/primitives/asymmetric/utils.py')
-rw-r--r-- | src/cryptography/hazmat/primitives/asymmetric/utils.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/cryptography/hazmat/primitives/asymmetric/utils.py b/src/cryptography/hazmat/primitives/asymmetric/utils.py index bad9ab73..b62eadf0 100644 --- a/src/cryptography/hazmat/primitives/asymmetric/utils.py +++ b/src/cryptography/hazmat/primitives/asymmetric/utils.py @@ -13,6 +13,7 @@ from pyasn1.type import namedtype, univ import six from cryptography import utils +from cryptography.hazmat.primitives.asymmetric.ec import EllipticCurve class _DSSSigValue(univ.Sequence): @@ -71,3 +72,36 @@ 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: + return b'\x00' + else: + # Get the ceiling of curve.key_size / 8 + 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': + return None, None + elif data.startswith(b'\x04'): + # Get the ceiling of curve.key_size / 8 + 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') |