Switch noise from the RingResolver to the DefaultResolver (#1439)

* hmm...

* Switch snow resolver to default

* Fix documentation

* Use the sha2 crate for sha512 hashing

* Use ring on native

* Use different features on different targets

Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
This commit is contained in:
Ashley
2020-02-12 21:44:50 +01:00
committed by GitHub
parent afb1c877a3
commit 8238fd2100
5 changed files with 30 additions and 13 deletions

View File

@ -34,6 +34,7 @@ libp2p-swarm = { version = "0.5.0", path = "swarm" }
libp2p-uds = { version = "0.15.0", path = "transports/uds" }
libp2p-wasm-ext = { version = "0.8.0", path = "transports/wasm-ext" }
libp2p-yamux = { version = "0.15.0", path = "muxers/yamux" }
libp2p-noise = { version = "0.13.0", path = "protocols/noise" }
parking_lot = "0.10.0"
pin-project = "0.4.6"
smallvec = "1.0"
@ -43,7 +44,6 @@ wasm-timer = "0.2.4"
libp2p-deflate = { version = "0.7.0", path = "protocols/deflate" }
libp2p-dns = { version = "0.15.0", path = "transports/dns" }
libp2p-mdns = { version = "0.15.0", path = "misc/mdns" }
libp2p-noise = { version = "0.13.0", path = "protocols/noise" }
libp2p-tcp = { version = "0.15.0", path = "transports/tcp" }
libp2p-websocket = { version = "0.15.0", path = "transports/websocket", optional = true }

View File

@ -15,11 +15,17 @@ libp2p-core = { version = "0.15.0", path = "../../core" }
log = "0.4"
prost = "0.6.1"
rand = "0.7.2"
ring = { version = "0.16.9", features = ["alloc"], default-features = false }
snow = { version = "0.6.1", features = ["ring-resolver"], default-features = false }
sha2 = "0.8.0"
x25519-dalek = "0.5"
zeroize = "1"
[target.'cfg(not(target_os = "unknown"))'.dependencies]
snow = { version = "0.6.1", features = ["ring-resolver"], default-features = false }
[target.'cfg(target_os = "unknown")'.dependencies]
snow = { version = "0.6.1", features = ["default-resolver"], default-features = false }
[dev-dependencies]
env_logger = "0.7.1"
libp2p-tcp = { version = "0.15.0", path = "../../transports/tcp" }

View File

@ -198,12 +198,10 @@ impl<T: AsRef<[u8]>> AsRef<[u8]> for PublicKey<T> {
}
}
/// Custom `snow::CryptoResolver` which delegates to the `RingResolver`
/// Custom `snow::CryptoResolver` which delegates to either the
/// `RingResolver` on native or the `DefaultResolver` on wasm
/// for hash functions and symmetric ciphers, while using x25519-dalek
/// for Curve25519 DH. We do not use the default resolver for any of
/// the choices, because it comes with unwanted additional dependencies,
/// notably rust-crypto, and to avoid being affected by changes to
/// the defaults.
/// for Curve25519 DH.
struct Resolver;
impl snow::resolvers::CryptoResolver for Resolver {
@ -220,12 +218,26 @@ impl snow::resolvers::CryptoResolver for Resolver {
}
fn resolve_hash(&self, choice: &snow::params::HashChoice) -> Option<Box<dyn snow::types::Hash>> {
#[cfg(target_os = "unknown")]
{
snow::resolvers::DefaultResolver.resolve_hash(choice)
}
#[cfg(not(target_os = "unknown"))]
{
snow::resolvers::RingResolver.resolve_hash(choice)
}
}
fn resolve_cipher(&self, choice: &snow::params::CipherChoice) -> Option<Box<dyn snow::types::Cipher>> {
#[cfg(target_os = "unknown")]
{
snow::resolvers::DefaultResolver.resolve_cipher(choice)
}
#[cfg(not(target_os = "unknown"))]
{
snow::resolvers::RingResolver.resolve_cipher(choice)
}
}
}
/// Wrapper around a CSPRNG to implement `snow::Random` trait for.

View File

@ -26,7 +26,7 @@ use lazy_static::lazy_static;
use libp2p_core::UpgradeInfo;
use libp2p_core::{identity, identity::ed25519};
use rand::Rng;
use ring::digest::{SHA512, digest};
use sha2::{Sha512, Digest};
use x25519_dalek::{X25519_BASEPOINT_BYTES, x25519};
use zeroize::Zeroize;
@ -212,7 +212,7 @@ impl SecretKey<X25519> {
// the same to yield a Curve25519 keypair with the same public key.
// let ed25519_sk = ed25519::SecretKey::from(ed);
let mut curve25519_sk: [u8; 32] = [0; 32];
let hash = digest(&SHA512, ed25519_sk.as_ref());
let hash = Sha512::digest(ed25519_sk.as_ref());
curve25519_sk.copy_from_slice(&hash.as_ref()[..32]);
let sk = SecretKey(X25519(curve25519_sk)); // Copy
curve25519_sk.zeroize();

View File

@ -181,7 +181,6 @@ pub use libp2p_mplex as mplex;
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
#[doc(inline)]
pub use libp2p_mdns as mdns;
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
#[doc(inline)]
pub use libp2p_noise as noise;
#[doc(inline)]