def test_deserialize_public_not_bytes(self): ''' serialized key must be bytes ''' with self.assertRaises(ValueError) as ctx: ed25519.verifying_key_from_string(u"not bytes") self.assertIn("must be bytes", str(ctx.exception))
def test_key_serialization(self): """ a serialized+deserialized keypair is the same as the original """ private_key, public_key = ed25519.create_signing_keypair() private_key_str = ed25519.string_from_signing_key(private_key) self.assertIsInstance(private_key_str, native_bytes) private_key2, public_key2 = ed25519.signing_keypair_from_string( private_key_str) # the deserialized signing keys are the same as the original self.assertEqual( ed25519.string_from_signing_key(private_key), ed25519.string_from_signing_key(private_key2), ) self.assertEqual( ed25519.string_from_verifying_key(public_key), ed25519.string_from_verifying_key(public_key2), ) # ditto, but for the verifying keys public_key_str = ed25519.string_from_verifying_key(public_key) self.assertIsInstance(public_key_str, native_bytes) public_key2 = ed25519.verifying_key_from_string(public_key_str) self.assertEqual( ed25519.string_from_verifying_key(public_key), ed25519.string_from_verifying_key(public_key2), )
def test_unsigned_announcement(self): ed25519.verifying_key_from_string(b"pub-v0-wodst6ly4f7i7akt2nxizsmmy2rlmer6apltl56zctn67wfyu5tq") mock_tub = Mock() ic = IntroducerClient( mock_tub, u"pb://", u"fake_nick", "0.0.0", "1.2.3", (0, u"i am a nonce"), "invalid", ) self.assertEqual(0, ic._debug_counts["inbound_announcement"]) ic.got_announcements([ ("message", "v0-aaaaaaa", "v0-wodst6ly4f7i7akt2nxizsmmy2rlmer6apltl56zctn67wfyu5tq") ]) # we should have rejected this announcement due to a bad signature self.assertEqual(0, ic._debug_counts["inbound_announcement"])
def unsign_from_foolscap(ann_t): (msg, sig_vs, claimed_key_vs) = ann_t if not sig_vs or not claimed_key_vs: raise UnknownKeyError("only signed announcements recognized") if not sig_vs.startswith(b"v0-"): raise UnknownKeyError("only v0- signatures recognized") if not claimed_key_vs.startswith(b"v0-"): raise UnknownKeyError("only v0- keys recognized") claimed_key = ed25519.verifying_key_from_string(b"pub-" + claimed_key_vs) sig_bytes = base32.a2b(remove_prefix(sig_vs, b"v0-")) ed25519.verify_signature(claimed_key, sig_bytes, msg) key_vs = claimed_key_vs ann = json.loads(msg.decode("utf-8")) return (ann, key_vs)
def test_decode_ed15519_keypair(self): ''' Created using the old code: from allmydata.util.keyutil import make_keypair, parse_privkey, parse_pubkey test_data = b'test' priv_str, pub_str = make_keypair() priv, _ = parse_privkey(priv_str) pub = parse_pubkey(pub_str) sig = priv.sign(test_data) pub.verify(sig, test_data) This simply checks that keys and signatures generated using the old code are still valid using the new code. ''' priv_str = b'priv-v0-lqcj746bqa4npkb6zpyc6esd74x3bl6mbcjgqend7cvtgmcpawhq' pub_str = b'pub-v0-yzpqin3of3ep363lwzxwpvgai3ps43dao46k2jds5kw5ohhpcwhq' test_data = b'test' sig = ( b'\xde\x0e\xd6\xe2\xf5\x03]8\xfe\xa71\xad\xb4g\x03\x11\x81\x8b\x08\xffz\xf4K\xa0' b'\x86 ier!\xe8\xe5#*\x9d\x8c\x0bI\x02\xd90\x0e7\xbeW\xbf\xa3\xfe\xc1\x1c\xf5+\xe9)' b'\xa3\xde\xc9\xc6s\xc9\x90\xf7x\x08') private_key, derived_public_key = ed25519.signing_keypair_from_string( priv_str) public_key = ed25519.verifying_key_from_string(pub_str) self.assertEqual( ed25519.string_from_verifying_key(public_key), ed25519.string_from_verifying_key(derived_public_key), ) new_sig = ed25519.sign_data(private_key, test_data) self.assertEqual(new_sig, sig) ed25519.verify_signature(public_key, new_sig, test_data) ed25519.verify_signature(derived_public_key, new_sig, test_data) ed25519.verify_signature(public_key, sig, test_data) ed25519.verify_signature(derived_public_key, sig, test_data)