diff --git a/Cargo.toml b/Cargo.toml index 82c7729c..3e261501 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/protocols/noise/Cargo.toml b/protocols/noise/Cargo.toml index 0f2e00a8..18fbb005 100644 --- a/protocols/noise/Cargo.toml +++ b/protocols/noise/Cargo.toml @@ -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" } diff --git a/protocols/noise/src/protocol.rs b/protocols/noise/src/protocol.rs index 60124f86..1fd9fa03 100644 --- a/protocols/noise/src/protocol.rs +++ b/protocols/noise/src/protocol.rs @@ -198,12 +198,10 @@ impl> AsRef<[u8]> for PublicKey { } } -/// 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,11 +218,25 @@ impl snow::resolvers::CryptoResolver for Resolver { } fn resolve_hash(&self, choice: &snow::params::HashChoice) -> Option> { - snow::resolvers::RingResolver.resolve_hash(choice) + #[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> { - snow::resolvers::RingResolver.resolve_cipher(choice) + #[cfg(target_os = "unknown")] + { + snow::resolvers::DefaultResolver.resolve_cipher(choice) + } + #[cfg(not(target_os = "unknown"))] + { + snow::resolvers::RingResolver.resolve_cipher(choice) + } } } diff --git a/protocols/noise/src/protocol/x25519.rs b/protocols/noise/src/protocol/x25519.rs index de414a36..8a19971d 100644 --- a/protocols/noise/src/protocol/x25519.rs +++ b/protocols/noise/src/protocol/x25519.rs @@ -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 { // 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(); diff --git a/src/lib.rs b/src/lib.rs index 94bf747c..da831671 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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)]