From b142964d3110a0a23c18ffefcbeea5ef6f271a43 Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Mon, 9 Sep 2019 20:04:35 -0400 Subject: [PATCH] Adds support for verifying ed25519 signatures, for secio --- libp2p/crypto/ed25519.py | 29 +++++++++++++++++++++++++++++ libp2p/crypto/serialization.py | 2 ++ setup.py | 1 + 3 files changed, 32 insertions(+) create mode 100644 libp2p/crypto/ed25519.py diff --git a/libp2p/crypto/ed25519.py b/libp2p/crypto/ed25519.py new file mode 100644 index 00000000..713e74b8 --- /dev/null +++ b/libp2p/crypto/ed25519.py @@ -0,0 +1,29 @@ +from Crypto.Hash import SHA256 + +from libp2p.crypto.keys import KeyType, PublicKey +from nacl.public import PublicKey as Ed255129PublicKeyImpl +from nacl.signing import BadSignatureError, VerifyKey + + +class Ed25519PublicKey(PublicKey): + def __init__(self, impl: Ed255129PublicKeyImpl) -> None: + self.impl = impl + + def to_bytes(self) -> bytes: + return bytes(self.impl) + + @classmethod + def from_bytes(cls, key_bytes: bytes) -> "Ed25519PublicKey": + return cls(Ed255129PublicKeyImpl(key_bytes)) + + def get_type(self) -> KeyType: + return KeyType.Ed25519 + + def verify(self, data: bytes, signature: bytes) -> bool: + verify_key = VerifyKey(self.to_bytes()) + h = SHA256.new(data) + try: + verify_key.verify(h, signature) + except BadSignatureError: + return False + return True diff --git a/libp2p/crypto/serialization.py b/libp2p/crypto/serialization.py index dedcf85d..6bc686e3 100644 --- a/libp2p/crypto/serialization.py +++ b/libp2p/crypto/serialization.py @@ -1,3 +1,4 @@ +from libp2p.crypto.ed25519 import Ed25519PublicKey from libp2p.crypto.keys import KeyType, PrivateKey, PublicKey from libp2p.crypto.rsa import RSAPublicKey from libp2p.crypto.secp256k1 import Secp256k1PrivateKey, Secp256k1PublicKey @@ -5,6 +6,7 @@ from libp2p.crypto.secp256k1 import Secp256k1PrivateKey, Secp256k1PublicKey key_type_to_public_key_deserializer = { KeyType.Secp256k1.value: Secp256k1PublicKey.from_bytes, KeyType.RSA.value: RSAPublicKey.from_bytes, + KeyType.Ed25519.value: Ed25519PublicKey.from_bytes, } key_type_to_private_key_deserializer = { diff --git a/setup.py b/setup.py index 6e6ad676..152a5b1a 100644 --- a/setup.py +++ b/setup.py @@ -41,6 +41,7 @@ setuptools.setup( "protobuf==3.9.0", "coincurve>=10.0.0,<11.0.0", "fastecdsa==1.7.4", + "pynacl==1.3.0", ], extras_require=extras_require, packages=setuptools.find_packages(exclude=["tests", "tests.*"]),