mirror of
https://github.com/shadowsocks/shadowsocks-rust.git
synced 2026-02-09 01:59:16 +08:00
use ChaCha20 and Salsa20 from rust-crypto, get rid of libsodium-sys
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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 =>
|
||||
|
||||
@@ -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)]
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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<io::Result<TcpStream>> = 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);
|
||||
|
||||
Reference in New Issue
Block a user