diff --git a/src/ecdsa/ecdsa.py b/src/ecdsa/ecdsa.py index f7109659..4c5f16fb 100644 --- a/src/ecdsa/ecdsa.py +++ b/src/ecdsa/ecdsa.py @@ -148,6 +148,8 @@ def __init__(self, generator, point, verify=True): self.point = point n = generator.order() p = self.curve.p() + if point == ellipticcurve.INFINITY: + raise InvalidPointError("The public point is the point at infinity.") if not (0 <= point.x() < p) or not (0 <= point.y() < p): raise InvalidPointError( "The public point has x or y out of range." diff --git a/src/ecdsa/keys.py b/src/ecdsa/keys.py index f74252c7..2b74d118 100644 --- a/src/ecdsa/keys.py +++ b/src/ecdsa/keys.py @@ -162,6 +162,8 @@ def from_public_point( self = cls(_error__please_use_generate=True) if isinstance(curve.curve, CurveEdTw): raise ValueError("Method incompatible with Edwards curves") + if point == ellipticcurve.INFINITY: + raise MalformedPointError("Cannot use the point at infinity as a public key") if not isinstance(point, ellipticcurve.PointJacobi): point = ellipticcurve.PointJacobi.from_affine(point) self.curve = curve diff --git a/src/ecdsa/test_pyecdsa.py b/src/ecdsa/test_pyecdsa.py index 799e9b74..38784541 100644 --- a/src/ecdsa/test_pyecdsa.py +++ b/src/ecdsa/test_pyecdsa.py @@ -65,7 +65,7 @@ curve_brainpoolp384r1, curve_brainpoolp512r1, ) -from .ellipticcurve import Point +from .ellipticcurve import Point, INFINITY from . import der from . import rfc6979 from . import ecdsa @@ -957,6 +957,12 @@ def test_decoding_with_point_at_infinity(self): with self.assertRaises(MalformedPointError): VerifyingKey.from_string(b"\x00") + def test_from_public_point_with_infinity_raises_malformed(self): + # passing INFINITY to from_public_point must raise MalformedPointError, + # not TypeError (GitHub issue #341) + with self.assertRaises(MalformedPointError): + VerifyingKey.from_public_point(INFINITY) + def test_not_lying_on_curve(self): enc = number_to_string(NIST192p.curve.p(), NIST192p.curve.p() + 1)