Skip to content

Commit

Permalink
feat: random_iv_or_salt made independent in crate::utils
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Nov 2, 2024
1 parent ae4c20b commit 43a1463
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shadowsocks-crypto"
version = "0.5.7"
version = "0.5.8"
authors = ["luozijun <[email protected]>", "ty <[email protected]>"]
edition = "2021"
license = "MIT"
Expand All @@ -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 = [
Expand All @@ -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 }
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod v1;
pub mod v2;

pub mod kind;
pub mod utils;

pub use self::kind::{CipherCategory, CipherKind};

Expand Down
25 changes: 25 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -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::<u128>() };
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;
}
}
}
25 changes: 2 additions & 23 deletions src/v1/cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u128>() };
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]) {
Expand Down

0 comments on commit 43a1463

Please sign in to comment.