Skip to content

Commit b8fdfbe

Browse files
committed
Public_key: fix check for x and y size
the x and y needs to be on curve, so they need to be smaller than the curve's prime, not the base point order See Section 3.2.2.1 of SEC 1 v2
1 parent 8aac4a4 commit b8fdfbe

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/ecdsa/ecdsa.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def __init__(self, generator, point, verify=True):
112112
"""
113113
Low level ECDSA public key object.
114114
115-
:param generator: the Point that generates the group
115+
:param generator: the Point that generates the group (the base point)
116116
:param point: the Point that defines the public key
117117
:param bool verify: if True check if point is valid point on curve
118118
@@ -124,8 +124,9 @@ def __init__(self, generator, point, verify=True):
124124
self.generator = generator
125125
self.point = point
126126
n = generator.order()
127-
if point.x() < 0 or n <= point.x() or point.y() < 0 or n <= point.y():
128-
raise InvalidPointError("Generator point has x or y out of range.")
127+
p = self.curve.p()
128+
if not (0 <= point.x() < p) or not (0 <= point.y() < p):
129+
raise InvalidPointError("The public point has x or y out of range.")
129130
if verify and not self.curve.contains_point(point.x(), point.y()):
130131
raise InvalidPointError("Point does not lie on the curve")
131132
if not n:
@@ -266,7 +267,8 @@ def point_is_valid(generator, x, y):
266267

267268
n = generator.order()
268269
curve = generator.curve()
269-
if x < 0 or n <= x or y < 0 or n <= y:
270+
p = curve.p()
271+
if not (0 <= x < p) or not (0 <= y < p):
270272
return False
271273
if not curve.contains_point(x, y):
272274
return False

src/ecdsa/test_pyecdsa.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ def test_decoding_with_point_at_infinity(self):
621621
VerifyingKey.from_string(b('\x00'))
622622

623623
def test_not_lying_on_curve(self):
624-
enc = number_to_string(NIST192p.order, NIST192p.order+1)
624+
enc = number_to_string(NIST192p.curve.p(), NIST192p.curve.p()+1)
625625

626626
with self.assertRaises(MalformedPointError):
627627
VerifyingKey.from_string(b('\x02') + enc)

0 commit comments

Comments
 (0)