def test_register_errors(self): """Unit test of native Python EC registration errors""" G, n = get_ec_curve_params(b'nistp256') p, a, b, Gx, Gy = G.curve.p, G.curve.a, G.curve.b, G.x, G.y with self.subTest('Bad prime'): with self.assertRaises(ValueError): register_prime_curve(b'bad', p+1, a, b, Gx, Gy, n) with self.subTest('Bad a, b pair'): with self.assertRaises(ValueError): register_prime_curve(b'bad', p, a+1, b, Gx, Gy, n) with self.subTest('Bad generator point'): with self.assertRaises(ValueError): register_prime_curve(b'bad', p, a, b, Gx+1, Gy, n) with self.subTest('Bad order'): with self.assertRaises(ValueError): register_prime_curve(b'bad', p, a, b, Gx, Gy, n+1) with self.subTest('Weak prime'): with self.assertRaises(ValueError): register_prime_curve(b'bad', 263, 2, 3, 200, 39, 270)
def test_lookup_by_params(self): """Test errors in EC curve lookup by params""" G, n = get_ec_curve_params(b'nistp256') with self.subTest('Bad curve'): with self.assertRaises(ValueError): lookup_ec_curve_by_params(G.curve.p+1, G.curve.a, G.curve.b, G.encode(), n) with self.subTest('Unknown curve'): with self.assertRaises(ValueError): lookup_ec_curve_by_params(263, 2, 3, encode_ec_point(2, 200, 39), 270)
def test_math(self): """Unit test native Python EC point math""" G, n = get_ec_curve_params(b'nistp256') G2, _ = get_ec_curve_params(b'nistp521') Inf = PrimePoint.construct(G.curve, None, None) with self.subTest('Add to infinity'): self.assertEqual(G + Inf, G) with self.subTest('Negate'): negG = -G self.assertEqual(-negG, G) with self.subTest('Negate infinity'): self.assertEqual(-Inf, Inf) with self.subTest('Multiply returning infinity'): self.assertEqual(n * G, Inf) with self.subTest('Add from different curves'): with self.assertRaises(ValueError): _ = G + G2
def test_encode(self): """Unit test native Python EC point encoding""" G, _ = get_ec_curve_params(b'nistp256') with self.subTest('Encode infinity'): self.assertEqual(encode_ec_point(None, None, None), b'\x00') with self.subTest('Decode infinity'): point = PrimePoint.decode(G.curve, b'\x00') self.assertEqual((point.curve, point.x, point.y), (None, None, None)) with self.subTest('Encode and decode'): self.assertEqual(PrimePoint.decode(G.curve, G.encode()), G) with self.subTest('Bad point type'): with self.assertRaises(ValueError): decode_ec_point(0, b'\x05') with self.subTest('Bad point length'): with self.assertRaises(ValueError): decode_ec_point(G.curve.keylen, G.encode()[:-1])
def test_get_params(self): """Test errors getting EC curve params""" with self.subTest('Get params'): with self.assertRaises(ValueError): get_ec_curve_params(b'xxx')