コード例 #1
0
    def test(self):
        seed = randstr(SEEDBYTES)
        signer = ecdsa.SigningKey(seed)
        verifier = signer.get_verifying_key()
        self._help_test_sign_and_check_good_keys(signer, verifier)

        vstr = verifier.serialize()
        self.failUnlessEqual(len(vstr), PUBKEYBYTES)
        verifier2 = ecdsa.VerifyingKey(vstr)
        self._help_test_sign_and_check_good_keys(signer, verifier2)

        signer2 = ecdsa.SigningKey(seed)
        self._help_test_sign_and_check_good_keys(signer2, verifier2)

        verifier3 = signer2.get_verifying_key()
        self._help_test_sign_and_check_good_keys(signer, verifier3)

        # Now test various ways that the keys could be corrupted or ill-matched.

        # Flip one bit of the public key.
        badvstr = flip_one_bit(vstr)
        try:
            badverifier = ecdsa.VerifyingKey(badvstr)
        except ecdsa.Error:
            # Ok, fine, the verifying key was corrupted and Crypto++ detected this fact.
            pass
        else:
            self._help_test_sign_and_check_bad_keys(signer, badverifier)

        # Randomize all bits of the public key.
        badvstr = randstr(len(vstr))
        assert badvstr != vstr, "Internal error -- randstr() produced the same string twice: %s == %s" % (badvstr, vstr)
        try:
            badverifier = ecdsa.VerifyingKey(badvstr)
        except ecdsa.Error:
            # Ok, fine, the key was corrupted and Crypto++ detected this fact.
            pass
        else:
            self._help_test_sign_and_check_bad_keys(signer, badverifier)

        # Flip one bit of the private key.
        badseed = flip_one_bit(seed)
        badsigner = ecdsa.SigningKey(badseed)
        self._help_test_sign_and_check_bad_keys(badsigner, verifier)

        # Randomize all bits of the private key.
        badseed = randstr(len(seed))
        assert badseed != seed, "Internal error -- randstr() produced the same string twice: %s == %s" % (badseed, seed)
        badsigner = ecdsa.SigningKey(badseed)
        self._help_test_sign_and_check_bad_keys(badsigner, verifier)
コード例 #2
0
    def test_from_signer_and_serialize_and_deserialize(self):
        seed = randstr(SEEDBYTES)
        signer = ecdsa.SigningKey(seed)

        verifier = signer.get_verifying_key()
        s1 = verifier.serialize()
        self.failUnlessEqual(len(s1), PUBKEYBYTES)
        ecdsa.VerifyingKey(s1)
        s2 = verifier.serialize()
        self.failUnlessEqual(s1, s2)
コード例 #3
0
    def test_construct_from_same_seed_is_reproducible(self):
        seed = randstr(SEEDBYTES)
        signer1 = ecdsa.SigningKey(seed)
        signer2 = ecdsa.SigningKey(seed)
        self.failUnlessEqual(signer1.get_verifying_key().serialize(), signer2.get_verifying_key().serialize())

        # ... and using different seeds constructs a different private key.
        seed3 = randstr(SEEDBYTES)
        assert seed3 != seed, "Internal error in Python random module's PRNG (or in pycryptopp's hacks to it to facilitate testing) -- got two identical strings from randstr(%s)" % SEEDBYTES
        signer3 = ecdsa.SigningKey(seed3)
        self.failIfEqual(signer1.get_verifying_key().serialize(), signer3.get_verifying_key().serialize())

        # Also try the all-zeroes string just because bugs sometimes are
        # data-dependent on zero or cause bogus zeroes.
        seed4 = '\x00'*SEEDBYTES
        assert seed4 != seed, "Internal error in Python random module's PRNG (or in pycryptopp's hacks to it to facilitate testing) -- got the all-zeroes string from randstr(%s)" % SEEDBYTES
        signer4 = ecdsa.SigningKey(seed4)
        self.failIfEqual(signer4.get_verifying_key().serialize(), signer1.get_verifying_key().serialize())

        signer5 = ecdsa.SigningKey(seed4)
        self.failUnlessEqual(signer5.get_verifying_key().serialize(), signer4.get_verifying_key().serialize())
コード例 #4
0
 def test_compatibility(self):
     # Confirm that the KDF used by the SigningKey constructor doesn't
     # change without suitable backwards-compability
     seed = 'bd616451f65d151ddd63efef42202c13457d1a0a44fb6f642be4bf9567ef19c6'.decode('hex')
     signer = ecdsa.SigningKey(seed)
     v1 = signer.get_verifying_key()
     vs = v1.serialize()
     vs32 = base64.b32encode(vs)
     self.failUnlessEqual(vs32, "AIWKEM44YHQCR3VI7SF7IJI7SSW6YNLMGMWBIXQWXC5522H2KXXHO===")
     v2 = ecdsa.VerifyingKey(vs)
     # print signer.sign("message").encode('hex')
     sig = 'a914953c6e6cecaf97d3d7f142ed1da88014752c3e1cd43b38f3327a73be67075bda7ec4a2ead5bb8a3471271a44ffbbd456b4d3ca470584c05703fdbe7b5bc0'.decode('hex')
     self.failUnless(v1.verify("message", sig))
     self.failUnless(v2.verify("message", sig))
コード例 #5
0
 def ver_init(self, N):
     signer = ecdsa.SigningKey(self.seed)
     self.sig = signer.sign(msg)
     self.verifier = signer.get_verifying_key()
コード例 #6
0
 def sign_init(self, N):
     self.signer = ecdsa.SigningKey(self.seed)
コード例 #7
0
 def gen(self, N):
     for i in xrange(N):
          ecdsa.SigningKey(self.seed)
コード例 #8
0
 def test_sign_and_verify_emptymsg(self):
     seed = randstr(SEEDBYTES)
     signer = ecdsa.SigningKey(seed)
     sig = signer.sign("")
     v = signer.get_verifying_key()
     self.failUnless(v.verify("", sig))
コード例 #9
0
 def test_sign(self):
     seed = randstr(SEEDBYTES)
     signer = ecdsa.SigningKey(seed)
     sig = signer.sign("message")
     self.failUnlessEqual(len(sig), SIGBYTES)
コード例 #10
0
 def test_construct(self):
     seed = randstr(SEEDBYTES)
     ecdsa.SigningKey(seed)
コード例 #11
0
 def test_construct_bad_arg_type(self):
     try:
         ecdsa.SigningKey(1)
     except TypeError, le:
         self.failUnless("must be string" in str(le), le)
コード例 #12
0
 def test_construct_short_seed(self):
     try:
         ecdsa.SigningKey("\x00\x00\x00")
     except ecdsa.Error, le:
         self.failUnless("seed is required to be of length " in str(le), le)
コード例 #13
0
 def test_keys():
     test_privkey = ecdsa.SigningKey("testseedtestseedtestseedtestseed")
     test_pubkey = test_privkey.get_verifying_key()
     return test_pubkey, test_privkey