def test_ed25519_key(): os.system("rm -rf ./tests/ed25519*") os.system("ssh-keygen -t ed25519 -N '' -b 123 -f ./tests/ed25519") keypair = privkey_parser("tests/ed25519") assert keypair assert keypair.pk assert keypair.sk assert keypair.skpk assert hex2bytes(keypair.skpk) == ed25519.publickey(hex2bytes(keypair.sk))[1] assert hex2bytes(keypair.pk) == ed25519.publickey(hex2bytes(keypair.sk))[0] msg = "hello world".encode() keypair = privkey_parser("tests/ed25519") sig = ed25519_sign(keypair, msg) assert ed25519.sign(msg, ed25519.publickey(hex2bytes(keypair.sk))[1])[:-len(msg)] == sig assert ed25519.open(sig+msg, hex2bytes(keypair.pk)) os.system("rm -rf ./tests/ed25519*") mypass = "******" os.system("ssh-keygen -t ed25519 -N '%s' -a '10' -b 123 -f ./tests/ed25519" % mypass) keypair = privkey_parser("tests/ed25519", mypass) assert keypair assert keypair.pk assert keypair.sk assert keypair.skpk assert hex2bytes(keypair.skpk) == ed25519.publickey(hex2bytes(keypair.sk))[1] assert hex2bytes(keypair.pk) == ed25519.publickey(hex2bytes(keypair.sk))[0] msg = "hello world".encode() keypair = privkey_parser("tests/ed25519", mypass) sig = ed25519_sign(keypair, msg) assert ed25519.sign(msg, ed25519.publickey(hex2bytes(keypair.sk))[1])[:-len(msg)] == sig assert ed25519.open(sig+msg, hex2bytes(keypair.pk))
def test_raw(self): sk_s = b"\x00" * 32 # usually urandom(32) vk_s, skvk_s = raw.publickey(sk_s) self.assertEqual(len(vk_s), 32) exp_vks = unhexlify("3b6a27bcceb6a42d62a3a8d02a6f0d73" "653215771de243a63ac048a18b59da29") self.assertEqual(vk_s, exp_vks) self.assertEqual(skvk_s[:32], sk_s) self.assertEqual(skvk_s[32:], vk_s) msg = b"hello world" msg_and_sig = raw.sign(msg, skvk_s) sig = msg_and_sig[:-len(msg)] self.assertEqual(len(sig), 64) exp_sig = unhexlify("b0b47780f096ae60bfff8d8e7b19c36b" "321ae6e69cca972f2ff987ef30f20d29" "774b53bae404485c4391ddf1b3f37aaa" "8a9747f984eb0884e8aa533386e73305") self.assertEqual(sig, exp_sig) ret = raw.open(sig+msg, vk_s) # don't raise exception self.assertEqual(ret, msg) self.assertRaises(raw.BadSignatureError, raw.open, sig+msg+b".. NOT!", vk_s) self.assertRaises(raw.BadSignatureError, raw.open, sig+flip_bit(msg), vk_s) self.assertRaises(raw.BadSignatureError, raw.open, sig+msg, flip_bit(vk_s)) self.assertRaises(raw.BadSignatureError, raw.open, sig+msg, flip_bit(vk_s, in_byte=2)) self.assertRaises(raw.BadSignatureError, raw.open, flip_bit(sig)+msg, vk_s) self.assertRaises(raw.BadSignatureError, raw.open, flip_bit(sig, in_byte=33)+msg, vk_s)
def test_raw(self): sk_s = b"\x00" * 32 # usually urandom(32) vk_s, skvk_s = raw.publickey(sk_s) self.assertEqual(len(vk_s), 32) exp_vks = unhexlify("3b6a27bcceb6a42d62a3a8d02a6f0d73" "653215771de243a63ac048a18b59da29") self.assertEqual(vk_s, exp_vks) self.assertEqual(skvk_s[:32], sk_s) self.assertEqual(skvk_s[32:], vk_s) msg = b"hello world" msg_and_sig = raw.sign(msg, skvk_s) sig = msg_and_sig[:-len(msg)] self.assertEqual(len(sig), 64) exp_sig = unhexlify("b0b47780f096ae60bfff8d8e7b19c36b" "321ae6e69cca972f2ff987ef30f20d29" "774b53bae404485c4391ddf1b3f37aaa" "8a9747f984eb0884e8aa533386e73305") self.assertEqual(sig, exp_sig) ret = raw.open(sig + msg, vk_s) # don't raise exception self.assertEqual(ret, msg) self.assertRaises(raw.BadSignatureError, raw.open, sig + msg + b".. NOT!", vk_s) self.assertRaises(raw.BadSignatureError, raw.open, sig + flip_bit(msg), vk_s) self.assertRaises(raw.BadSignatureError, raw.open, sig + msg, flip_bit(vk_s)) self.assertRaises(raw.BadSignatureError, raw.open, sig + msg, flip_bit(vk_s, in_byte=2)) self.assertRaises(raw.BadSignatureError, raw.open, flip_bit(sig) + msg, vk_s) self.assertRaises(raw.BadSignatureError, raw.open, flip_bit(sig, in_byte=33) + msg, vk_s)
def sign(self, msg, prefix="", encoding=None): assert isinstance(msg, bytes) if not isinstance(prefix, bytes): prefix = prefix.encode("ascii") sig_and_msg = _ed25519.sign(msg, self.sk_s) # the response is R+S+msg sig_R = sig_and_msg[0:32] sig_S = sig_and_msg[32:64] msg_out = sig_and_msg[64:] sig_out = sig_R + sig_S assert msg_out == msg if encoding: return to_ascii(sig_out, prefix, encoding) return prefix + sig_out
def ed25519_sign(kp: KeyPair, msg: Union[str, bytes]) -> bytes: assert kp.type == "ssh-ed25519" if isinstance(msg, str): msg = msg.encode() return ed25519.sign(msg, hex2bytes(kp.skpk))[:-len(msg)]