From 43a146344e1aef772596828e871b338555928a16 Mon Sep 17 00:00:00 2001 From: zonyitoo Date: Sat, 2 Nov 2024 22:06:45 +0800 Subject: [PATCH] feat: random_iv_or_salt made independent in crate::utils --- Cargo.toml | 6 +++--- src/lib.rs | 1 + src/utils.rs | 25 +++++++++++++++++++++++++ src/v1/cipher.rs | 25 ++----------------------- 4 files changed, 31 insertions(+), 26 deletions(-) create mode 100644 src/utils.rs diff --git a/Cargo.toml b/Cargo.toml index 2646b3a..c295e98 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "shadowsocks-crypto" -version = "0.5.7" +version = "0.5.8" authors = ["luozijun ", "ty "] edition = "2021" license = "MIT" @@ -13,7 +13,7 @@ rust-version = "1.61" [features] default = ["v1", "v1-aead"] -v1 = ["md-5", "rand", "cfg-if"] +v1 = ["md-5", "cfg-if"] v1-stream = ["v1", "chacha20", "aes", "ctr", "camellia"] v1-aead = ["v1", "aes-gcm", "chacha20poly1305", "hkdf", "sha1"] v1-aead-extra = [ @@ -34,7 +34,7 @@ ring = ["ring-compat"] [dependencies] cfg-if = { version = "1.0", optional = true } -rand = { version = "0.8", optional = true } +rand = "0.8" aes-gcm = { version = "0.10", optional = true } aes-gcm-siv = { version = "0.11", optional = true } ccm = { version = "0.5", optional = true } diff --git a/src/lib.rs b/src/lib.rs index a84a232..198ead9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,7 @@ pub mod v1; pub mod v2; pub mod kind; +pub mod utils; pub use self::kind::{CipherCategory, CipherKind}; diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..77a201f --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,25 @@ +//! Common utilities + +/// Generate random bytes into `iv_or_salt` +pub fn random_iv_or_salt(iv_or_salt: &mut [u8]) { + use rand::Rng; + + // Gen IV or Gen Salt by KEY-LEN + if iv_or_salt.is_empty() { + return; + } + + let mut rng = rand::thread_rng(); + loop { + rng.fill(iv_or_salt); + + // https://stackoverflow.com/questions/65367552/checking-a-vecu8-to-see-if-its-all-zero + let (prefix, aligned, suffix) = unsafe { iv_or_salt.align_to::() }; + let is_zeros = + prefix.iter().all(|&x| x == 0) && aligned.iter().all(|&x| x == 0) && suffix.iter().all(|&x| x == 0); + + if !is_zeros { + break; + } + } +} diff --git a/src/v1/cipher.rs b/src/v1/cipher.rs index c9be091..da11758 100644 --- a/src/v1/cipher.rs +++ b/src/v1/cipher.rs @@ -6,29 +6,8 @@ use super::dummy::DummyCipher; #[cfg(feature = "v1-stream")] use super::streamcipher::StreamCipher; -/// Generate random bytes into `iv_or_salt` -pub fn random_iv_or_salt(iv_or_salt: &mut [u8]) { - use rand::Rng; - - // Gen IV or Gen Salt by KEY-LEN - if iv_or_salt.is_empty() { - return; - } - - let mut rng = rand::thread_rng(); - loop { - rng.fill(iv_or_salt); - - // https://stackoverflow.com/questions/65367552/checking-a-vecu8-to-see-if-its-all-zero - let (prefix, aligned, suffix) = unsafe { iv_or_salt.align_to::() }; - let is_zeros = - prefix.iter().all(|&x| x == 0) && aligned.iter().all(|&x| x == 0) && suffix.iter().all(|&x| x == 0); - - if !is_zeros { - break; - } - } -} +#[deprecated(since = "0.5.8", note = "prefer utils::random_iv_or_salt")] +pub use crate::utils::random_iv_or_salt; /// Key derivation of OpenSSL's [EVP_BytesToKey](https://wiki.openssl.org/index.php/Manual:EVP_BytesToKey(3)) pub fn openssl_bytes_to_key(password: &[u8], key: &mut [u8]) {