Skip to content

Commit 946dc62

Browse files
authored
Merge pull request #236 from tomato42/ecdsa-coverage
test coverage for ecdsa module
2 parents 06662f7 + e939c45 commit 946dc62

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

src/ecdsa/test_ecdsa.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
generator_384,
2222
generator_521,
2323
generator_secp256k1,
24+
curve_192,
25+
InvalidPointError,
26+
curve_112r2,
27+
generator_112r2,
28+
int_to_string,
2429
)
2530

2631

@@ -96,6 +101,20 @@ def test_inequality_public_key(self):
96101
pub_key2 = Public_key(gen, point2)
97102
self.assertNotEqual(pub_key1, pub_key2)
98103

104+
def test_inequality_different_curves(self):
105+
gen = generator_192
106+
x1 = 0xC58D61F88D905293BCD4CD0080BCB1B7F811F2FFA41979F6
107+
y1 = 0x8804DC7A7C4C7F8B5D437F5156F3312CA7D6DE8A0E11867F
108+
point1 = ellipticcurve.Point(gen.curve(), x1, y1)
109+
110+
x2 = 0x722BA0FB6B8FC8898A4C6AB49E66
111+
y2 = 0x2B7344BB57A7ABC8CA0F1A398C7D
112+
point2 = ellipticcurve.Point(generator_112r2.curve(), x2, y2)
113+
114+
pub_key1 = Public_key(gen, point1)
115+
pub_key2 = Public_key(generator_112r2, point2)
116+
self.assertNotEqual(pub_key1, pub_key2)
117+
99118
def test_inequality_public_key_not_implemented(self):
100119
gen = generator_192
101120
x = 0xC58D61F88D905293BCD4CD0080BCB1B7F811F2FFA41979F6
@@ -104,6 +123,106 @@ def test_inequality_public_key_not_implemented(self):
104123
pub_key = Public_key(gen, point)
105124
self.assertNotEqual(pub_key, None)
106125

126+
def test_public_key_with_generator_without_order(self):
127+
gen = ellipticcurve.PointJacobi(
128+
generator_192.curve(), generator_192.x(), generator_192.y(), 1
129+
)
130+
131+
x = 0xC58D61F88D905293BCD4CD0080BCB1B7F811F2FFA41979F6
132+
y = 0x8804DC7A7C4C7F8B5D437F5156F3312CA7D6DE8A0E11867F
133+
point = ellipticcurve.Point(gen.curve(), x, y)
134+
135+
with self.assertRaises(InvalidPointError) as e:
136+
Public_key(gen, point)
137+
138+
self.assertIn("Generator point must have order", str(e.exception))
139+
140+
def test_public_point_on_curve_not_scalar_multiple_of_base_point(self):
141+
x = 2
142+
y = 0xBE6AA4938EF7CFE6FE29595B6B00
143+
# we need a curve with cofactor != 1
144+
point = ellipticcurve.PointJacobi(curve_112r2, x, y, 1)
145+
146+
self.assertTrue(curve_112r2.contains_point(x, y))
147+
148+
with self.assertRaises(InvalidPointError) as e:
149+
Public_key(generator_112r2, point)
150+
151+
self.assertIn("Generator point order", str(e.exception))
152+
153+
def test_point_is_valid_with_not_scalar_multiple_of_base_point(self):
154+
x = 2
155+
y = 0xBE6AA4938EF7CFE6FE29595B6B00
156+
157+
self.assertFalse(point_is_valid(generator_112r2, x, y))
158+
159+
# the tests to verify the extensiveness of tests in ecdsa.ecdsa
160+
# if PointJacobi gets modified to calculate the x and y mod p the tests
161+
# below will need to use a fake/mock object
162+
def test_invalid_point_x_negative(self):
163+
pt = ellipticcurve.PointJacobi(curve_192, -1, 0, 1)
164+
165+
with self.assertRaises(InvalidPointError) as e:
166+
Public_key(generator_192, pt)
167+
168+
self.assertIn("The public point has x or y", str(e.exception))
169+
170+
def test_invalid_point_x_equal_p(self):
171+
pt = ellipticcurve.PointJacobi(curve_192, curve_192.p(), 0, 1)
172+
173+
with self.assertRaises(InvalidPointError) as e:
174+
Public_key(generator_192, pt)
175+
176+
self.assertIn("The public point has x or y", str(e.exception))
177+
178+
def test_invalid_point_y_negative(self):
179+
pt = ellipticcurve.PointJacobi(curve_192, 0, -1, 1)
180+
181+
with self.assertRaises(InvalidPointError) as e:
182+
Public_key(generator_192, pt)
183+
184+
self.assertIn("The public point has x or y", str(e.exception))
185+
186+
def test_invalid_point_y_equal_p(self):
187+
pt = ellipticcurve.PointJacobi(curve_192, 0, curve_192.p(), 1)
188+
189+
with self.assertRaises(InvalidPointError) as e:
190+
Public_key(generator_192, pt)
191+
192+
self.assertIn("The public point has x or y", str(e.exception))
193+
194+
195+
class TestPublicKeyVerifies(unittest.TestCase):
196+
# test all the different ways that a signature can be publicly invalid
197+
@classmethod
198+
def setUpClass(cls):
199+
gen = generator_192
200+
x = 0xC58D61F88D905293BCD4CD0080BCB1B7F811F2FFA41979F6
201+
y = 0x8804DC7A7C4C7F8B5D437F5156F3312CA7D6DE8A0E11867F
202+
point = ellipticcurve.Point(gen.curve(), x, y)
203+
204+
cls.pub_key = Public_key(gen, point)
205+
206+
def test_sig_with_r_zero(self):
207+
sig = Signature(0, 1)
208+
209+
self.assertFalse(self.pub_key.verifies(1, sig))
210+
211+
def test_sig_with_r_order(self):
212+
sig = Signature(generator_192.order(), 1)
213+
214+
self.assertFalse(self.pub_key.verifies(1, sig))
215+
216+
def test_sig_with_s_zero(self):
217+
sig = Signature(1, 0)
218+
219+
self.assertFalse(self.pub_key.verifies(1, sig))
220+
221+
def test_sig_with_s_order(self):
222+
sig = Signature(1, generator_192.order())
223+
224+
self.assertFalse(self.pub_key.verifies(1, sig))
225+
107226

108227
class TestPrivateKey(unittest.TestCase):
109228
@classmethod
@@ -536,3 +655,7 @@ def test_sig_verify(args):
536655
assert pubkey.verifies(msg, signature)
537656

538657
assert not pubkey.verifies(msg - 1, signature)
658+
659+
660+
def test_int_to_string_with_zero():
661+
assert int_to_string(0) == b"\x00"

0 commit comments

Comments
 (0)