From 91ec4cdbd5e07c677e2b9d8f4b86e3a141a10770 Mon Sep 17 00:00:00 2001 From: "Y. T. Chung" Date: Mon, 9 Oct 2017 01:24:41 +0800 Subject: [PATCH] Allow building without libsodium --- Cargo.toml | 9 ++++++++- appveyor.yml | 1 - src/crypto/aead.rs | 15 +++++++++++++-- src/crypto/cipher.rs | 20 ++++++++++++++++++++ src/crypto/mod.rs | 1 + src/crypto/stream.rs | 3 +++ src/lib.rs | 3 ++- 7 files changed, 47 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ff725b4d..6ae5cb22 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/appveyor.yml b/appveyor.yml index f8e7cc2d..0cc400a9 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,6 +1,5 @@ environment: SSL_CERT_FILE: "C:\\OpenSSL\\cacert.pem" - SODIUM_BUILD_STATIC: yes matrix: - TARGET: x86_64-pc-windows-msvc diff --git a/src/crypto/aead.rs b/src/crypto/aead.rs index 67ffd4ad..6406f493 100644 --- a/src/crypto/aead.rs +++ b/src/crypto/aead.rs @@ -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; + } +} diff --git a/src/crypto/cipher.rs b/src/crypto/cipher.rs index 06165880..c976d969 100644 --- a/src/crypto/cipher.rs +++ b/src/crypto/cipher.rs @@ -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), diff --git a/src/crypto/mod.rs b/src/crypto/mod.rs index 6e4c109b..e876bba2 100644 --- a/src/crypto/mod.rs +++ b/src/crypto/mod.rs @@ -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 diff --git a/src/crypto/stream.rs b/src/crypto/stream.rs index 0c16ae00..272b1df5 100644 --- a/src/crypto/stream.rs +++ b/src/crypto/stream.rs @@ -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 | diff --git a/src/lib.rs b/src/lib.rs index d5dbe320..43043eca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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;