from __future__ import print_function import sys import hypothesis.strategies as st from hypothesis import given, settings, note, example
try: import unittest2 as unittest except ImportError: import unittest import pytest from .ecdsa import (
Private_key,
Public_key,
Signature,
generator_192,
digest_integer,
ellipticcurve,
point_is_valid,
generator_224,
generator_256,
generator_384,
generator_521,
generator_secp256k1,
curve_192,
InvalidPointError,
curve_112r2,
generator_112r2,
int_to_string,
) from .ellipticcurve import Point
HYP_SETTINGS = {} # old hypothesis doesn't have the "deadline" setting if sys.version_info > (2, 7): # pragma: no branch # SEC521p is slow, allow long execution for it
HYP_SETTINGS["deadline"] = 5000
class TestP192FromX9_62(unittest.TestCase): """Check test vectors from X9.62"""
def test_signature(self):
r, s = self.sig.r, self.sig.s assert r == 3342403536405981729393488334694600415596881826869351677613 assert s == 5735822328888155254683894997897571951568553642892029982342
def test_inequality_public_key_not_implemented(self):
gen = generator_192
x = 0xC58D61F88D905293BCD4CD0080BCB1B7F811F2FFA41979F6
y = 0x8804DC7A7C4C7F8B5D437F5156F3312CA7D6DE8A0E11867F
point = ellipticcurve.Point(gen.curve(), x, y)
pub_key = Public_key(gen, point)
self.assertNotEqual(pub_key, None)
def test_public_key_with_generator_without_order(self):
gen = ellipticcurve.PointJacobi(
generator_192.curve(), generator_192.x(), generator_192.y(), 1
)
x = 0xC58D61F88D905293BCD4CD0080BCB1B7F811F2FFA41979F6
y = 0x8804DC7A7C4C7F8B5D437F5156F3312CA7D6DE8A0E11867F
point = ellipticcurve.Point(gen.curve(), x, y)
with self.assertRaises(InvalidPointError) as e:
Public_key(gen, point)
self.assertIn("Generator point must have order", str(e.exception))
def test_public_point_on_curve_not_scalar_multiple_of_base_point(self):
x = 2
y = 0xBE6AA4938EF7CFE6FE29595B6B00 # we need a curve with cofactor != 1
point = ellipticcurve.PointJacobi(curve_112r2, x, y, 1)
self.assertTrue(curve_112r2.contains_point(x, y))
with self.assertRaises(InvalidPointError) as e:
Public_key(generator_112r2, point)
self.assertIn("Generator point order", str(e.exception))
def test_point_is_valid_with_not_scalar_multiple_of_base_point(self):
x = 2
y = 0xBE6AA4938EF7CFE6FE29595B6B00
# the tests to verify the extensiveness of tests in ecdsa.ecdsa # if PointJacobi gets modified to calculate the x and y mod p the tests # below will need to use a fake/mock object def test_invalid_point_x_negative(self):
pt = ellipticcurve.PointJacobi(curve_192, -1, 0, 1)
with self.assertRaises(InvalidPointError) as e:
Public_key(generator_192, pt)
self.assertIn("The public point has x or y", str(e.exception))
with self.assertRaises(InvalidPointError) as e:
Public_key(generator_192, pt)
self.assertIn("The public point has x or y", str(e.exception))
class TestPublicKeyVerifies(unittest.TestCase): # test all the different ways that a signature can be publicly invalid
@classmethod def setUpClass(cls):
gen = generator_192
x = 0xC58D61F88D905293BCD4CD0080BCB1B7F811F2FFA41979F6
y = 0x8804DC7A7C4C7F8B5D437F5156F3312CA7D6DE8A0E11867F
point = ellipticcurve.Point(gen.curve(), x, y)
cls.pub_key = Public_key(gen, point)
def test_sig_with_r_zero(self):
sig = Signature(0, 1)
self.assertFalse(self.pub_key.verifies(1, sig))
def test_sig_with_r_order(self):
sig = Signature(generator_192.order(), 1)
self.assertFalse(self.pub_key.verifies(1, sig))
def test_sig_with_s_zero(self):
sig = Signature(1, 0)
self.assertFalse(self.pub_key.verifies(1, sig))
def test_sig_with_s_order(self):
sig = Signature(1, generator_192.order())
self.assertFalse(self.pub_key.verifies(1, sig))
class TestPrivateKey(unittest.TestCase):
@classmethod def setUpClass(cls):
gen = generator_192
x = 0xC58D61F88D905293BCD4CD0080BCB1B7F811F2FFA41979F6
y = 0x8804DC7A7C4C7F8B5D437F5156F3312CA7D6DE8A0E11867F
point = ellipticcurve.Point(gen.curve(), x, y)
cls.pub_key = Public_key(gen, point)
@pytest.mark.parametrize("generator,x,y,expected", P192_POINTS) def test_point_validity(generator, x, y, expected): """
`generator` defines the curve; is `(x, y)` a point on
this curve? `expected` isTrueif the right answer is Yes. """ assert point_is_valid(generator, x, y) == expected
@pytest.mark.parametrize("gen,msg,qx,qy,r,s,expected", CURVE_192_KATS) def test_signature_validity(gen, msg, qx, qy, r, s, expected): """
`msg` = message, `qx` and `qy` represent the base point on
elliptic curve of `gen`, `r` and `s` are the signature, and
`expected` isTrue iff the signature is expected to be valid."""
pubk = Public_key(gen, ellipticcurve.Point(gen.curve(), qx, qy)) with pytest.warns(DeprecationWarning) as warns:
msg_dgst = digest_integer(msg) assert len(warns) == 3 assert"unused"in warns[0].message.args[0] assert"unused"in warns[1].message.args[0] assert"unused"in warns[2].message.args[0] assert expected == pubk.verifies(msg_dgst, Signature(r, s))
@pytest.mark.parametrize( "gen,msg,qx,qy,r,s,expected", [x for x in CURVE_192_KATS if x[6]]
) def test_pk_recovery(gen, msg, r, s, qx, qy, expected): del expected
sign = Signature(r, s) with pytest.warns(DeprecationWarning) as warns:
msg_dgst = digest_integer(msg) assert len(warns) == 3 assert"unused"in warns[0].message.args[0] assert"unused"in warns[1].message.args[0] assert"unused"in warns[2].message.args[0]
pks = sign.recover_public_keys(msg_dgst, gen)
assert pks
# Test if the signature is valid for all found public keys for pk in pks:
q = pk.point
test_signature_validity(gen, msg, q.x(), q.y(), r, s, True)
# Test if the original public key is in the set of found keys
original_q = ellipticcurve.Point(gen.curve(), qx, qy)
points = [pk.point for pk in pks] assert original_q in points
@settings(**SIG_VER_SETTINGS)
@example((generator_224, 4, 1, 1))
@given(st_random_gen_key_msg_nonce()) def test_sig_verify(args): """
Check if signing and verification works for arbitrary messages and
that signatures for other messages are rejected. """
generator, sec_mult, msg, nonce = args
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.