From e01f77bc49f8977843ebbc9a4e43292ff972d3d2 Mon Sep 17 00:00:00 2001 From: "pinkforest(she/her)" <36498018+pinkforest@users.noreply.github.com> Date: Tue, 30 Aug 2022 17:20:41 +1000 Subject: [PATCH] transports/noise: Migrate away from deprecated `sodiumoxide` for tests (#2817) --- transports/noise/Cargo.toml | 3 ++- transports/noise/src/protocol/x25519.rs | 31 +++++++++++-------------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/transports/noise/Cargo.toml b/transports/noise/Cargo.toml index 708b3eb2..351b8d82 100644 --- a/transports/noise/Cargo.toml +++ b/transports/noise/Cargo.toml @@ -33,7 +33,8 @@ async-io = "1.2.0" env_logger = "0.9.0" libp2p-tcp = { path = "../../transports/tcp" } quickcheck = "0.9.0" -sodiumoxide = "0.2.5" +libsodium-sys-stable = { version = "1.19.22", features = ["fetch-latest"] } +ed25519-compact = "1.0.11" [build-dependencies] prost-build = "0.11" diff --git a/transports/noise/src/protocol/x25519.rs b/transports/noise/src/protocol/x25519.rs index bc22dcc7..297122c3 100644 --- a/transports/noise/src/protocol/x25519.rs +++ b/transports/noise/src/protocol/x25519.rs @@ -278,10 +278,13 @@ impl snow::types::Dh for Keypair { #[cfg(test)] mod tests { use super::*; + // Use the ed25519_compact for testing + use ed25519_compact; use libp2p_core::identity::ed25519; + // Use the libsodium-sys-stable crypto_sign imports for testing + use libsodium_sys::crypto_sign_ed25519_pk_to_curve25519; + use libsodium_sys::crypto_sign_ed25519_sk_to_curve25519; use quickcheck::*; - use sodiumoxide::crypto::sign; - use std::os::raw::c_int; use x25519_dalek::StaticSecret; // ed25519 to x25519 keypair conversion must yield the same results as @@ -292,9 +295,11 @@ mod tests { let ed25519 = ed25519::Keypair::generate(); let x25519 = Keypair::from(SecretKey::from_ed25519(&ed25519.secret())); - let sodium_sec = ed25519_sk_to_curve25519(&sign::SecretKey(ed25519.encode())); - let sodium_pub = - ed25519_pk_to_curve25519(&sign::PublicKey(ed25519.public().encode().clone())); + let sodium_sec = + ed25519_sk_to_curve25519(&ed25519_compact::SecretKey::new(ed25519.encode())); + let sodium_pub = ed25519_pk_to_curve25519(&ed25519_compact::PublicKey::new( + ed25519.public().encode().clone(), + )); let our_pub = x25519.public.0; // libsodium does the [clamping] of the scalar upon key construction, @@ -327,18 +332,10 @@ mod tests { quickcheck(prop as fn() -> _); } - // Bindings to libsodium's ed25519 to curve25519 key conversions, to check that - // they agree with the conversions performed in this module. - - extern "C" { - pub fn crypto_sign_ed25519_pk_to_curve25519(c: *mut u8, e: *const u8) -> c_int; - pub fn crypto_sign_ed25519_sk_to_curve25519(c: *mut u8, e: *const u8) -> c_int; - } - - pub fn ed25519_pk_to_curve25519(k: &sign::PublicKey) -> Option<[u8; 32]> { + pub fn ed25519_pk_to_curve25519(k: &ed25519_compact::PublicKey) -> Option<[u8; 32]> { let mut out = [0u8; 32]; unsafe { - if crypto_sign_ed25519_pk_to_curve25519(out.as_mut_ptr(), (&k.0).as_ptr()) == 0 { + if crypto_sign_ed25519_pk_to_curve25519(out.as_mut_ptr(), k.as_ptr()) == 0 { Some(out) } else { None @@ -346,10 +343,10 @@ mod tests { } } - pub fn ed25519_sk_to_curve25519(k: &sign::SecretKey) -> Option<[u8; 32]> { + pub fn ed25519_sk_to_curve25519(k: &ed25519_compact::SecretKey) -> Option<[u8; 32]> { let mut out = [0u8; 32]; unsafe { - if crypto_sign_ed25519_sk_to_curve25519(out.as_mut_ptr(), (&k.0).as_ptr()) == 0 { + if crypto_sign_ed25519_sk_to_curve25519(out.as_mut_ptr(), k.as_ptr()) == 0 { Some(out) } else { None