def test_lengths(self): default = NIST192p priv = SigningKey.generate() pub = priv.get_verifying_key() self.failUnlessEqual(len(pub.to_string()), default.verifying_key_length) sig = priv.sign("data") self.failUnlessEqual(len(sig), default.signature_length) if BENCH: print for curve in (NIST192p, NIST224p, NIST256p, NIST384p, NIST521p): start = time.time() priv = SigningKey.generate(curve=curve) pub1 = priv.get_verifying_key() keygen_time = time.time() - start pub2 = VerifyingKey.from_string(pub1.to_string(), curve) self.failUnlessEqual(pub1.to_string(), pub2.to_string()) self.failUnlessEqual(len(pub1.to_string()), curve.verifying_key_length) start = time.time() sig = priv.sign("data") sign_time = time.time() - start self.failUnlessEqual(len(sig), curve.signature_length) if BENCH: start = time.time() pub1.verify(sig, "data") verify_time = time.time() - start print "%s: siglen=%d, keygen=%0.3fs, sign=%0.3f, verify=%0.3f" \ % (curve.name, curve.signature_length, keygen_time, sign_time, verify_time)
def test_lengths(self): default = NIST192p priv = SigningKey.generate() pub = priv.get_verifying_key() self.assertEqual(len(pub.to_string()), default.verifying_key_length) sig = priv.sign(b("data")) self.assertEqual(len(sig), default.signature_length) if BENCH: print_() for curve in (NIST192p, NIST224p, NIST256p, NIST384p, NIST521p): start = time.time() priv = SigningKey.generate(curve=curve) pub1 = priv.get_verifying_key() keygen_time = time.time() - start pub2 = VerifyingKey.from_string(pub1.to_string(), curve) self.assertEqual(pub1.to_string(), pub2.to_string()) self.assertEqual(len(pub1.to_string()), curve.verifying_key_length) start = time.time() sig = priv.sign(b("data")) sign_time = time.time() - start self.assertEqual(len(sig), curve.signature_length) if BENCH: start = time.time() pub1.verify(sig, b("data")) verify_time = time.time() - start print_("%s: siglen=%d, keygen=%0.3fs, sign=%0.3f, verify=%0.3f" \ % (curve.name, curve.signature_length, keygen_time, sign_time, verify_time))
def test_hashfunc(self): sk = SigningKey.generate(curve=NIST256p, hashfunc=sha256) data = "security level is 128 bits" sig = sk.sign(data) vk = VerifyingKey.from_string(sk.get_verifying_key().to_string(), curve=NIST256p, hashfunc=sha256) self.failUnless(vk.verify(sig, data)) sk2 = SigningKey.generate(curve=NIST256p) sig2 = sk2.sign(data, hashfunc=sha256) vk2 = VerifyingKey.from_string(sk2.get_verifying_key().to_string(), curve=NIST256p, hashfunc=sha256) self.failUnless(vk2.verify(sig2, data)) vk3 = VerifyingKey.from_string(sk.get_verifying_key().to_string(), curve=NIST256p) self.failUnless(vk3.verify(sig, data, hashfunc=sha256))
def test_hashfunc(self): sk = SigningKey.generate(curve=NIST256p, hashfunc=sha256) data = b("security level is 128 bits") sig = sk.sign(data) vk = VerifyingKey.from_string(sk.get_verifying_key().to_string(), curve=NIST256p, hashfunc=sha256) self.assertTrue(vk.verify(sig, data)) sk2 = SigningKey.generate(curve=NIST256p) sig2 = sk2.sign(data, hashfunc=sha256) vk2 = VerifyingKey.from_string(sk2.get_verifying_key().to_string(), curve=NIST256p, hashfunc=sha256) self.assertTrue(vk2.verify(sig2, data)) vk3 = VerifyingKey.from_string(sk.get_verifying_key().to_string(), curve=NIST256p) self.assertTrue(vk3.verify(sig, data, hashfunc=sha256))
def test_pubkey_strings(self): priv1 = SigningKey.generate() pub1 = priv1.get_verifying_key() s1 = pub1.to_string() self.assertEqual(type(s1), binary_type) self.assertEqual(len(s1), NIST192p.verifying_key_length) pub2 = VerifyingKey.from_string(s1) self.assertTruePubkeysEqual(pub1, pub2) priv1 = SigningKey.generate(curve=NIST256p) pub1 = priv1.get_verifying_key() s1 = pub1.to_string() self.assertEqual(type(s1), binary_type) self.assertEqual(len(s1), NIST256p.verifying_key_length) pub2 = VerifyingKey.from_string(s1, curve=NIST256p) self.assertTruePubkeysEqual(pub1, pub2) pub1_der = pub1.to_der() self.assertEqual(type(pub1_der), binary_type) pub2 = VerifyingKey.from_der(pub1_der) self.assertTruePubkeysEqual(pub1, pub2) self.assertRaises(der.UnexpectedDER, VerifyingKey.from_der, pub1_der + b("junk")) badpub = VerifyingKey.from_der(pub1_der) class FakeGenerator: def order(self): return 123456789 badcurve = Curve("unknown", None, FakeGenerator(), (1, 2, 3, 4, 5, 6), None) badpub.curve = badcurve badder = badpub.to_der() self.assertRaises(UnknownCurveError, VerifyingKey.from_der, badder) pem = pub1.to_pem() self.assertEqual(type(pem), binary_type) self.assertTrue(pem.startswith(b("-----BEGIN PUBLIC KEY-----")), pem) self.assertTrue(pem.strip().endswith(b("-----END PUBLIC KEY-----")), pem) pub2 = VerifyingKey.from_pem(pem) self.assertTruePubkeysEqual(pub1, pub2)
def test_basic(self): priv = SigningKey.generate() pub = priv.get_verifying_key() data = b("blahblah") sig = priv.sign(data) self.assertTrue(pub.verify(sig, data)) self.assertRaises(BadSignatureError, pub.verify, sig, data + b("bad")) pub2 = VerifyingKey.from_string(pub.to_string()) self.assertTrue(pub2.verify(sig, data))
def test_basic(self): priv = SigningKey.generate() pub = priv.get_verifying_key() data = "blahblah" sig = priv.sign(data) self.failUnless(pub.verify(sig, data)) self.failUnlessRaises(BadSignatureError, pub.verify, sig, data + "bad") pub2 = VerifyingKey.from_string(pub.to_string()) self.failUnless(pub2.verify(sig, data))
def test_basic(self): priv = SigningKey.generate() pub = priv.get_verifying_key() data = "blahblah" sig = priv.sign(data) self.failUnless(pub.verify(sig, data)) self.failUnlessRaises(BadSignatureError, pub.verify, sig, data+"bad") pub2 = VerifyingKey.from_string(pub.to_string()) self.failUnless(pub2.verify(sig, data))
def test_pubkey_strings(self): priv1 = SigningKey.generate() pub1 = priv1.get_verifying_key() s1 = pub1.to_string() self.failUnlessEqual(type(s1), str) self.failUnlessEqual(len(s1), NIST192p.verifying_key_length) pub2 = VerifyingKey.from_string(s1) self.failUnlessPubkeysEqual(pub1, pub2) priv1 = SigningKey.generate(curve=NIST256p) pub1 = priv1.get_verifying_key() s1 = pub1.to_string() self.failUnlessEqual(type(s1), str) self.failUnlessEqual(len(s1), NIST256p.verifying_key_length) pub2 = VerifyingKey.from_string(s1, curve=NIST256p) self.failUnlessPubkeysEqual(pub1, pub2) pub1_der = pub1.to_der() self.failUnlessEqual(type(pub1_der), str) pub2 = VerifyingKey.from_der(pub1_der) self.failUnlessPubkeysEqual(pub1, pub2) self.failUnlessRaises(der.UnexpectedDER, VerifyingKey.from_der, pub1_der + "junk") badpub = VerifyingKey.from_der(pub1_der) class FakeGenerator: def order(self): return 123456789 badcurve = Curve("unknown", None, FakeGenerator(), (1, 2, 3, 4, 5, 6)) badpub.curve = badcurve badder = badpub.to_der() self.failUnlessRaises(UnknownCurveError, VerifyingKey.from_der, badder) pem = pub1.to_pem() self.failUnlessEqual(type(pem), str) self.failUnless(pem.startswith("-----BEGIN PUBLIC KEY-----"), pem) self.failUnless(pem.strip().endswith("-----END PUBLIC KEY-----"), pem) pub2 = VerifyingKey.from_pem(pem) self.failUnlessPubkeysEqual(pub1, pub2)