mirror of
https://github.com/shadowsocks/shadowsocks-rust.git
synced 2026-11-06 16:14:11 +00:00
Allow building without libsodium
This commit is contained in:
+8
-1
@@ -26,6 +26,10 @@ path = "src/bin/ssurl.rs"
|
||||
[profile.release]
|
||||
lto = true
|
||||
|
||||
[features]
|
||||
default = ["sodium"]
|
||||
sodium = ["libsodium-ffi"]
|
||||
|
||||
[dependencies]
|
||||
log = "0.3"
|
||||
byteorder = "1.1"
|
||||
@@ -54,7 +58,10 @@ subprocess = "0.1"
|
||||
serde_urlencoded = "0.5"
|
||||
url = "1.5"
|
||||
byte_string = "1.0"
|
||||
libsodium-ffi = "0.1"
|
||||
libsodium-ffi = { version = "0.1", optional = true }
|
||||
|
||||
[target.'cfg(unix)'.dependencies]
|
||||
tokio-signal = "0.1"
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
tokio-signal = "0.1"
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
environment:
|
||||
SSL_CERT_FILE: "C:\\OpenSSL\\cacert.pem"
|
||||
SODIUM_BUILD_STATIC: yes
|
||||
|
||||
matrix:
|
||||
- TARGET: x86_64-pc-windows-msvc
|
||||
|
||||
+13
-2
@@ -10,8 +10,6 @@ use ring::hmac::SigningKey;
|
||||
|
||||
use bytes::{Bytes, BytesMut};
|
||||
|
||||
use libsodium_ffi::sodium_increment;
|
||||
|
||||
/// Encryptor API for AEAD ciphers
|
||||
pub trait AeadEncryptor {
|
||||
/// Encrypt `input` to `output` with `tag`. `output.len()` should equals to `input.len()`.
|
||||
@@ -103,8 +101,21 @@ pub fn make_skey(t: CipherType, key: &[u8], salt: &[u8]) -> Bytes {
|
||||
/// Increase nonce by 1
|
||||
///
|
||||
/// AEAD ciphers requires to increase nonce after encrypt/decrypt every chunk
|
||||
#[cfg(feature = "sodium")]
|
||||
pub fn increase_nonce(nonce: &mut [u8]) {
|
||||
use libsodium_ffi::sodium_increment;
|
||||
|
||||
unsafe {
|
||||
sodium_increment(nonce.as_mut_ptr(), nonce.len());
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "sodium"))]
|
||||
pub fn increase_nonce(nonce: &mut [u8]) {
|
||||
let mut prev: u16 = 0;
|
||||
for i in nonce {
|
||||
prev += *i as u16;
|
||||
*i = prev as u8;
|
||||
prev >>= 8;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,9 +82,13 @@ const CIPHER_RC4_MD5: &'static str = "rc4-md5";
|
||||
|
||||
const CIPHER_TABLE: &'static str = "table";
|
||||
|
||||
#[cfg(feature = "sodium")]
|
||||
const CIPHER_CHACHA20: &'static str = "chacha20";
|
||||
#[cfg(feature = "sodium")]
|
||||
const CIPHER_SALSA20: &'static str = "salsa20";
|
||||
#[cfg(feature = "sodium")]
|
||||
const CIPHER_XSALSA20: &'static str = "xsalsa20";
|
||||
#[cfg(feature = "sodium")]
|
||||
const CIPHER_CHACHA20_IETF: &'static str = "chacha20-ietf";
|
||||
|
||||
const CIPHER_DUMMY: &'static str = "dummy";
|
||||
@@ -112,9 +116,13 @@ pub enum CipherType {
|
||||
Rc4,
|
||||
Rc4Md5,
|
||||
|
||||
#[cfg(feature = "sodium")]
|
||||
ChaCha20,
|
||||
#[cfg(feature = "sodium")]
|
||||
Salsa20,
|
||||
#[cfg(feature = "sodium")]
|
||||
XSalsa20,
|
||||
#[cfg(feature = "sodium")]
|
||||
ChaCha20Ietf,
|
||||
|
||||
Aes128Gcm,
|
||||
@@ -150,6 +158,7 @@ impl CipherType {
|
||||
CipherType::Rc4 |
|
||||
CipherType::Rc4Md5 => symm::Cipher::rc4().key_len(),
|
||||
|
||||
#[cfg(feature = "sodium")]
|
||||
CipherType::ChaCha20 |
|
||||
CipherType::Salsa20 |
|
||||
CipherType::XSalsa20 |
|
||||
@@ -243,9 +252,12 @@ impl CipherType {
|
||||
}
|
||||
CipherType::Rc4Md5 => 16,
|
||||
|
||||
#[cfg(feature = "sodium")]
|
||||
CipherType::ChaCha20 |
|
||||
CipherType::Salsa20 => 8,
|
||||
#[cfg(feature = "sodium")]
|
||||
CipherType::XSalsa20 => 24,
|
||||
#[cfg(feature = "sodium")]
|
||||
CipherType::ChaCha20Ietf => 12,
|
||||
|
||||
CipherType::Aes128Gcm => AES_128_GCM.nonce_len(),
|
||||
@@ -330,9 +342,13 @@ impl FromStr for CipherType {
|
||||
CIPHER_RC4 => Ok(CipherType::Rc4),
|
||||
CIPHER_RC4_MD5 => Ok(CipherType::Rc4Md5),
|
||||
|
||||
#[cfg(feature = "sodium")]
|
||||
CIPHER_CHACHA20 => Ok(CipherType::ChaCha20),
|
||||
#[cfg(feature = "sodium")]
|
||||
CIPHER_SALSA20 => Ok(CipherType::Salsa20),
|
||||
#[cfg(feature = "sodium")]
|
||||
CIPHER_XSALSA20 => Ok(CipherType::XSalsa20),
|
||||
#[cfg(feature = "sodium")]
|
||||
CIPHER_CHACHA20_IETF => Ok(CipherType::ChaCha20Ietf),
|
||||
|
||||
CIPHER_AES_128_GCM => Ok(CipherType::Aes128Gcm),
|
||||
@@ -363,9 +379,13 @@ impl Display for CipherType {
|
||||
CipherType::Rc4 => write!(f, "{}", CIPHER_RC4),
|
||||
CipherType::Rc4Md5 => write!(f, "{}", CIPHER_RC4_MD5),
|
||||
|
||||
#[cfg(feature = "sodium")]
|
||||
CipherType::ChaCha20 => write!(f, "{}", CIPHER_CHACHA20),
|
||||
#[cfg(feature = "sodium")]
|
||||
CipherType::Salsa20 => write!(f, "{}", CIPHER_SALSA20),
|
||||
#[cfg(feature = "sodium")]
|
||||
CipherType::XSalsa20 => write!(f, "{}", CIPHER_XSALSA20),
|
||||
#[cfg(feature = "sodium")]
|
||||
CipherType::ChaCha20Ietf => write!(f, "{}", CIPHER_CHACHA20_IETF),
|
||||
|
||||
CipherType::Aes128Gcm => write!(f, "{}", CIPHER_AES_128_GCM),
|
||||
|
||||
@@ -18,6 +18,7 @@ pub mod ring;
|
||||
pub mod dummy;
|
||||
pub mod aead;
|
||||
pub mod stream;
|
||||
#[cfg(feature = "sodium")]
|
||||
pub mod sodium;
|
||||
|
||||
/// Crypto mode, encrypt or decrypt
|
||||
|
||||
@@ -5,6 +5,7 @@ use crypto::cipher::{CipherCategory, CipherResult, CipherType};
|
||||
use crypto::dummy;
|
||||
use crypto::openssl;
|
||||
use crypto::rc4_md5;
|
||||
#[cfg(feature = "sodium")]
|
||||
use crypto::sodium;
|
||||
use crypto::table;
|
||||
|
||||
@@ -84,6 +85,7 @@ define_stream_ciphers! {
|
||||
pub DummyCipher => dummy::DummyCipher,
|
||||
pub Rc4Md5Cipher => rc4_md5::Rc4Md5Cipher,
|
||||
pub OpenSSLCipher => openssl::OpenSSLCipher,
|
||||
#[cfg(feature = "sodium")]
|
||||
pub SodiumCipher => sodium::SodiumCipher,
|
||||
}
|
||||
|
||||
@@ -95,6 +97,7 @@ pub fn new_stream(t: CipherType, key: &[u8], iv: &[u8], mode: CryptoMode) -> Str
|
||||
CipherType::Table => StreamCipherVariant::new(table::TableCipher::new(key, mode)),
|
||||
CipherType::Dummy => StreamCipherVariant::new(dummy::DummyCipher),
|
||||
|
||||
#[cfg(feature = "sodium")]
|
||||
CipherType::ChaCha20 |
|
||||
CipherType::Salsa20 |
|
||||
CipherType::XSalsa20 |
|
||||
|
||||
+2
-1
@@ -83,6 +83,7 @@ extern crate md_5 as md5;
|
||||
extern crate digest;
|
||||
extern crate ring;
|
||||
extern crate openssl;
|
||||
#[cfg(feature = "sodium")]
|
||||
extern crate libsodium_ffi;
|
||||
|
||||
#[macro_use]
|
||||
@@ -99,7 +100,7 @@ extern crate lazy_static;
|
||||
extern crate scoped_tls;
|
||||
|
||||
extern crate subprocess;
|
||||
#[cfg(unix)]
|
||||
#[cfg(any(unix, windows))]
|
||||
extern crate tokio_signal;
|
||||
|
||||
extern crate byte_string;
|
||||
|
||||
Reference in New Issue
Block a user