Consolidate keypairs in core. (#972)

* Consolidate keypairs in core.

Introduce the concept of a node's identity keypair in libp2p-core,
instead of only the public key:

  * New module: libp2p_core::identity with submodules for the currently
    supported key types. An identity::Keypair and identity::PublicKey
    support the creation and verification of signatures. The public key
    supports encoding/decoding according to the libp2p specs.

  * The secio protocol is simplified as a result of moving code to libp2p-core.

  * The noise protocol is slightly simplified by consolidating ed25519
    keypairs in libp2p-core and using x25519-dalek for DH. Furthermore,
    Ed25519 to X25519 keypair conversion is now complete and tested.

Generalise over the DH keys in the noise protocol.

Generalise over the DH keys and thus DH parameter in handshake patterns
of the Noise protocol, such that it is easy to support other DH schemes
in the future, e.g. X448.

* Address new review comments.
This commit is contained in:
Roman Borschel
2019-03-11 13:42:53 +01:00
committed by GitHub
parent 26df15641c
commit 2c66f82b11
37 changed files with 1742 additions and 1020 deletions

View File

@ -221,15 +221,16 @@ pub enum IdentifyEvent {
mod tests {
use crate::{Identify, IdentifyEvent};
use futures::prelude::*;
use libp2p_core::identity;
use libp2p_core::{upgrade, upgrade::OutboundUpgradeExt, upgrade::InboundUpgradeExt, Swarm, Transport};
use std::io;
#[test]
fn periodic_id_works() {
let node1_key = libp2p_secio::SecioKeyPair::ed25519_generated().unwrap();
let node1_public_key = node1_key.to_public_key();
let node2_key = libp2p_secio::SecioKeyPair::ed25519_generated().unwrap();
let node2_public_key = node2_key.to_public_key();
let node1_key = identity::Keypair::generate_ed25519();
let node1_public_key = node1_key.public();
let node2_key = identity::Keypair::generate_ed25519();
let node2_public_key = node2_key.public();
let mut swarm1 = {
// TODO: make creating the transport more elegant ; literaly half of the code of the test
@ -253,7 +254,7 @@ mod tests {
let mut swarm2 = {
// TODO: make creating the transport more elegant ; literaly half of the code of the test
// is about creating the transport
let local_peer_id = node2_public_key.clone().into_peer_id();
let local_peer_id = node2_public_key.clone().into();
let transport = libp2p_tcp::TcpConfig::new()
.with_upgrade(libp2p_secio::SecioConfig::new(node2_key))
.and_then(move |out, endpoint| {

View File

@ -67,10 +67,12 @@ impl<T> IdentifySender<T> where T: AsyncWrite {
.map(|addr| addr.into_bytes())
.collect();
let pubkey_bytes = info.public_key.into_protobuf_encoding();
let mut message = structs_proto::Identify::new();
message.set_agentVersion(info.agent_version);
message.set_protocolVersion(info.protocol_version);
message.set_publicKey(info.public_key.into_protobuf_encoding());
message.set_publicKey(pubkey_bytes);
message.set_listenAddrs(listen_addrs);
message.set_observedAddr(observed_addr.to_bytes());
message.set_protocols(RepeatedField::from_vec(info.protocols));
@ -244,9 +246,12 @@ fn parse_proto_msg(msg: BytesMut) -> Result<(IdentifyInfo, Multiaddr), IoError>
addrs
};
let public_key = PublicKey::from_protobuf_encoding(msg.get_publicKey())
.map_err(|e| IoError::new(IoErrorKind::InvalidData, e))?;
let observed_addr = bytes_to_multiaddr(msg.take_observedAddr())?;
let info = IdentifyInfo {
public_key: PublicKey::from_protobuf_encoding(msg.get_publicKey())?,
public_key,
protocol_version: msg.take_protocolVersion(),
agent_version: msg.take_agentVersion(),
listen_addrs,
@ -266,13 +271,15 @@ mod tests {
use tokio::runtime::current_thread::Runtime;
use libp2p_tcp::TcpConfig;
use futures::{Future, Stream};
use libp2p_core::{PublicKey, Transport, upgrade::{apply_outbound, apply_inbound}};
use libp2p_core::{identity, Transport, upgrade::{apply_outbound, apply_inbound}};
use std::{io, sync::mpsc, thread};
#[test]
fn correct_transfer() {
// We open a server and a client, send info from the server to the client, and check that
// they were successfully received.
let send_pubkey = identity::Keypair::generate_ed25519().public();
let recv_pubkey = send_pubkey.clone();
let (tx, rx) = mpsc::channel();
@ -296,7 +303,7 @@ mod tests {
.and_then(|sender| {
sender.send(
IdentifyInfo {
public_key: PublicKey::Ed25519(vec![1, 2, 3, 4, 5, 7]),
public_key: send_pubkey,
protocol_version: "proto_version".to_owned(),
agent_version: "agent_version".to_owned(),
listen_addrs: vec![
@ -322,7 +329,7 @@ mod tests {
})
.and_then(|RemoteInfo { info, observed_addr, .. }| {
assert_eq!(observed_addr, "/ip4/100.101.102.103/tcp/5000".parse().unwrap());
assert_eq!(info.public_key, PublicKey::Ed25519(vec![1, 2, 3, 4, 5, 7]));
assert_eq!(info.public_key, recv_pubkey);
assert_eq!(info.protocol_version, "proto_version");
assert_eq!(info.agent_version, "agent_version");
assert_eq!(info.listen_addrs,