Пример #1
0
def run(curve, tag):
    sk, pk = generate_keypair(curve)
    msg = "My message to sign"

    # Signature
    start = time.time()
    sig = sign(curve, sk, msg)
    sign_time = time.time() - start

    # For signature verification there is no meaning of using infective
    # computations in scalar multiplications.
    if curve.infective:
        pk.curve = wcurve.secp256r1_curve()

    # Verification
    start = time.time()
    # /!\ in a real implementation the public key would most likely come
    # from an untrusted remote party so it would then be required to check
    # the validity of the public key before calling this function. That is
    # instantiating the right curve, calling JacobianPoint.from_affine()
    # or JacobianPoint.uncompress(), and calling JacobianPoint.is_valid().
    valid = verify(pk, sig, msg)
    verify_time = time.time() - start

    print('%-25s: sign=%0.3fs  verify=%0.3fs  valid=%s' % \
              (tag, sign_time, verify_time, valid))
Пример #2
0
 def setUp(self):
     self.cwd = os.path.dirname(os.path.abspath(__file__))
     self.curve_name = 'prime256v1'
     self.curve = wcurve.secp256r1_curve()
     self.curve_infective = wcurve.secp256r1_curve_infective()
     self.bin = 'ec_ref'
     self.bin_path = os.path.join(self.cwd, self.bin)
Пример #3
0
def run(curve, tag):
    sk, pk = generate_keypair(curve)
    msg = "My message to sign"

    # Signature
    start = time.time()
    sig = sign(curve, sk, msg)
    sign_time = time.time() - start

    # For signature verification there is no meaning of using infective
    # computations in scalar multiplications.
    if curve.infective:
        pk.curve = wcurve.secp256r1_curve()

    # Verification
    start = time.time()
    # /!\ in a real implementation the public key would most likely come
    # from an untrusted remote party so it would then be required to check
    # the validity of the public key before calling this function. That is
    # instantiating the right curve, calling JacobianPoint.from_affine()
    # or JacobianPoint.uncompress(), and calling JacobianPoint.is_valid().
    valid = verify(pk, sig, msg)
    verify_time = time.time() - start

    print('%-25s: sign=%0.3fs  verify=%0.3fs  valid=%s' % \
              (tag, sign_time, verify_time, valid))
Пример #4
0
 def testEq(self):
     self.assertEqual(self.curve.base_point, self.curve.base_point)
     self.assertEqual(self.curve.point_at_infinity, self.curve.point_at_infinity)
     self.assertEqual(self.curve.point_at_infinity, -self.curve.point_at_infinity)
     self.assertFalse(self.curve.base_point == self.curve.point_at_infinity)
     self.assertTrue(self.curve.base_point != self.curve.point_at_infinity)
     curve2 = wcurve.secp256r1_curve()
     self.assertEqual(self.curve, self.curve)
     self.assertEqual(self.curve, curve2)
     self.assertEqual(self.curve.base_point, curve2.base_point)
     curve2.n = 42
     self.assertNotEqual(self.curve, curve2)
     self.assertNotEqual(self.curve.base_point, curve2.base_point)
Пример #5
0
    return secret_key * curve.base_point

def compute_sharedkey(secret_key, pub_key):
    if not pub_key.is_valid():
        return
    return secret_key * pub_key

def run(curve, tag):
    sk1 = generate_secretkey(curve)
    sk2 = generate_secretkey(curve)

    pk1 = compute_pubkey(curve, sk1)
    start = time.time()
    pk2 = compute_pubkey(curve, sk2)
    pub_time = time.time() - start

    sh1 = compute_sharedkey(sk1, pk2)
    assert sh1 is not None
    start = time.time()
    sh2 = compute_sharedkey(sk2, pk1)
    shared_time = time.time() - start
    assert sh2 is not None

    print('%-25s: pub_key=%0.3fs  shared_key=%0.3fs  equals=%s' % \
              (tag, pub_time, shared_time, sh1 == sh2))

if __name__ == '__main__':
    run(wcurve.secp256r1_curve(), 'secp256r1')
    run(wcurve.secp256r1_curve_infective(),
        'secp256r1_curve_infective')