From 3aeaa2875faac01ae87a08551f4f28a64ddce79d Mon Sep 17 00:00:00 2001 From: "Y. T. Chung" Date: Sun, 13 Dec 2015 21:29:26 +0800 Subject: [PATCH] use ChaCha20 and Salsa20 from rust-crypto, get rid of libsodium-sys --- Cargo.toml | 9 ++------- src/crypto/cipher.rs | 21 ++++++++------------- src/crypto/mod.rs | 2 -- src/lib.rs | 3 --- src/relay/tcprelay/server.rs | 15 ++++++++++++++- 5 files changed, 24 insertions(+), 26 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0adf79f8..7499aa35 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,11 +36,10 @@ cipher-idea-cfb = [] cipher-rc2-cfb = [] cipher-rc4 = [] cipher-seed-cfb = [] -cipher-chacha20 = ["enable-sodium"] -cipher-salsa20 = ["enable-sodium"] +cipher-chacha20 = [] +cipher-salsa20 = [] enable-udp = [] -enable-sodium = ["libsodium-sys"] [[bin]] @@ -74,10 +73,6 @@ env_logger = "^0.3.2" rust-crypto = "^0.2.34" ip = "1.0.0" -[dependencies.libsodium-sys] -git = "https://github.com/zonyitoo/libsodium-sys.git" -optional = true - [dependencies.lru-cache] git = "https://github.com/zonyitoo/lru-cache.git" ver = "7688eae5a4ce8e6f9beb075e11d1abb3d2b2f635" diff --git a/src/crypto/cipher.rs b/src/crypto/cipher.rs index 50e6ff71..841dd3c9 100644 --- a/src/crypto/cipher.rs +++ b/src/crypto/cipher.rs @@ -28,10 +28,9 @@ use std::convert::From; use crypto::openssl; use crypto::table; -#[cfg(feature = "enable-sodium")] -use crypto::sodium; use crypto::CryptoMode; use crypto::rc4_md5; +use crypto::crypto::CryptoCipher; use crypto::digest::{self, DigestType}; @@ -208,8 +207,6 @@ pub enum CipherType { impl CipherType { pub fn block_size(&self) -> usize { - use libsodium_ffi::{crypto_stream_chacha20_NONCEBYTES, crypto_stream_salsa20_NONCEBYTES}; - match *self { CipherType::Table => 0, @@ -250,14 +247,12 @@ impl CipherType { #[cfg(feature = "cipher-rc4")] CipherType::Rc4Md5 => 16, #[cfg(feature = "cipher-seed-cfb")] CipherType::SeedCfb => 16, - #[cfg(feature = "cipher-chacha20")] CipherType::ChaCha20 => crypto_stream_chacha20_NONCEBYTES as usize, - #[cfg(feature = "cipher-salsa20")] CipherType::Salsa20 => crypto_stream_salsa20_NONCEBYTES as usize, + #[cfg(feature = "cipher-chacha20")] CipherType::ChaCha20 => 8, + #[cfg(feature = "cipher-salsa20")] CipherType::Salsa20 => 8, } } pub fn key_size(&self) -> usize { - use libsodium_ffi::{crypto_stream_chacha20_KEYBYTES, crypto_stream_salsa20_KEYBYTES}; - match *self { CipherType::Table => 0, @@ -298,8 +293,8 @@ impl CipherType { #[cfg(feature = "cipher-rc4")] CipherType::Rc4Md5 => 16, #[cfg(feature = "cipher-seed-cfb")] CipherType::SeedCfb => 16, - #[cfg(feature = "cipher-chacha20")] CipherType::ChaCha20 => crypto_stream_chacha20_KEYBYTES as usize, - #[cfg(feature = "cipher-salsa20")] CipherType::Salsa20 => crypto_stream_salsa20_KEYBYTES as usize, + #[cfg(feature = "cipher-chacha20")] CipherType::ChaCha20 => 32, + #[cfg(feature = "cipher-salsa20")] CipherType::Salsa20 => 32, } } @@ -574,9 +569,9 @@ macro_rules! define_ciphers { define_ciphers! { TableCipher => table::TableCipher, - SodiumCipher => sodium::SodiumCipher, Rc4Md5Cipher => rc4_md5::Rc4Md5Cipher, OpenSSLCipher => openssl::OpenSSLCipher, + CryptoCipher => CryptoCipher, } /// Generate a specific Cipher with key and initialize vector @@ -586,10 +581,10 @@ pub fn with_type(t: CipherType, key: &[u8], iv: &[u8], mode: CryptoMode) -> Ciph #[cfg(feature = "cipher-chacha20")] CipherType::ChaCha20 => - CipherVariant::new(sodium::SodiumCipher::new(t, key, iv)), + CipherVariant::new(CryptoCipher::new(t, key, iv)), #[cfg(feature = "cipher-salsa20")] CipherType::Salsa20 => - CipherVariant::new(sodium::SodiumCipher::new(t, key, iv)), + CipherVariant::new(CryptoCipher::new(t, key, iv)), #[cfg(feature = "cipher-rc4")] CipherType::Rc4Md5 => diff --git a/src/crypto/mod.rs b/src/crypto/mod.rs index 86986a6a..b3f1c8f4 100644 --- a/src/crypto/mod.rs +++ b/src/crypto/mod.rs @@ -29,8 +29,6 @@ pub mod openssl; pub mod digest; pub mod table; pub mod rc4_md5; -#[cfg(feature = "enable-sodium")] -pub mod sodium; pub mod crypto; #[derive(Clone, Copy)] diff --git a/src/lib.rs b/src/lib.rs index eb1d916c..6d6fdd26 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,15 +23,12 @@ #![crate_name = "shadowsocks"] #![feature(box_syntax, libc, test, slice_patterns, lookup_host, convert)] -#![feature(ip_addr)] extern crate rustc_serialize as serialize; #[macro_use] extern crate log; extern crate lru_cache; -extern crate libsodium_sys as libsodium_ffi; - extern crate byteorder; extern crate rand; diff --git a/src/relay/tcprelay/server.rs b/src/relay/tcprelay/server.rs index d49fedfd..f1dc9d9d 100644 --- a/src/relay/tcprelay/server.rs +++ b/src/relay/tcprelay/server.rs @@ -23,6 +23,7 @@ use std::sync::Arc; use std::io::{self, Read, Write, BufReader, ErrorKind}; +use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6}; use coio::Builder; use coio::net::{TcpListener, TcpStream, Shutdown}; @@ -166,7 +167,19 @@ impl TcpRelayServer { let processing = || { let mut last_err: Option> = None; for addr in addrs.into_iter() { - match TcpStream::connect(&(addr.ip(), *port)) { + let addr = match addr { + SocketAddr::V4(addr) => { + SocketAddr::V4(SocketAddrV4::new(addr.ip().clone(), *port)) + }, + SocketAddr::V6(addr) => { + SocketAddr::V6(SocketAddrV6::new(addr.ip().clone(), + *port, + addr.flowinfo(), + addr.scope_id())) + } + }; + + match TcpStream::connect(addr) { Ok(stream) => return Ok(stream), Err(err) => { error!("Unable to connect {:?}: {}", addr, err);