Skip to content

Commit 43a1463

Browse files
committed
feat: random_iv_or_salt made independent in crate::utils
1 parent ae4c20b commit 43a1463

File tree

4 files changed

+31
-26
lines changed

4 files changed

+31
-26
lines changed

Diff for: Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "shadowsocks-crypto"
3-
version = "0.5.7"
3+
version = "0.5.8"
44
authors = ["luozijun <[email protected]>", "ty <[email protected]>"]
55
edition = "2021"
66
license = "MIT"
@@ -13,7 +13,7 @@ rust-version = "1.61"
1313

1414
[features]
1515
default = ["v1", "v1-aead"]
16-
v1 = ["md-5", "rand", "cfg-if"]
16+
v1 = ["md-5", "cfg-if"]
1717
v1-stream = ["v1", "chacha20", "aes", "ctr", "camellia"]
1818
v1-aead = ["v1", "aes-gcm", "chacha20poly1305", "hkdf", "sha1"]
1919
v1-aead-extra = [
@@ -34,7 +34,7 @@ ring = ["ring-compat"]
3434

3535
[dependencies]
3636
cfg-if = { version = "1.0", optional = true }
37-
rand = { version = "0.8", optional = true }
37+
rand = "0.8"
3838
aes-gcm = { version = "0.10", optional = true }
3939
aes-gcm-siv = { version = "0.11", optional = true }
4040
ccm = { version = "0.5", optional = true }

Diff for: src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub mod v1;
1111
pub mod v2;
1212

1313
pub mod kind;
14+
pub mod utils;
1415

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

Diff for: src/utils.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//! Common utilities
2+
3+
/// Generate random bytes into `iv_or_salt`
4+
pub fn random_iv_or_salt(iv_or_salt: &mut [u8]) {
5+
use rand::Rng;
6+
7+
// Gen IV or Gen Salt by KEY-LEN
8+
if iv_or_salt.is_empty() {
9+
return;
10+
}
11+
12+
let mut rng = rand::thread_rng();
13+
loop {
14+
rng.fill(iv_or_salt);
15+
16+
// https://stackoverflow.com/questions/65367552/checking-a-vecu8-to-see-if-its-all-zero
17+
let (prefix, aligned, suffix) = unsafe { iv_or_salt.align_to::<u128>() };
18+
let is_zeros =
19+
prefix.iter().all(|&x| x == 0) && aligned.iter().all(|&x| x == 0) && suffix.iter().all(|&x| x == 0);
20+
21+
if !is_zeros {
22+
break;
23+
}
24+
}
25+
}

Diff for: src/v1/cipher.rs

+2-23
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,8 @@ use super::dummy::DummyCipher;
66
#[cfg(feature = "v1-stream")]
77
use super::streamcipher::StreamCipher;
88

9-
/// Generate random bytes into `iv_or_salt`
10-
pub fn random_iv_or_salt(iv_or_salt: &mut [u8]) {
11-
use rand::Rng;
12-
13-
// Gen IV or Gen Salt by KEY-LEN
14-
if iv_or_salt.is_empty() {
15-
return;
16-
}
17-
18-
let mut rng = rand::thread_rng();
19-
loop {
20-
rng.fill(iv_or_salt);
21-
22-
// https://stackoverflow.com/questions/65367552/checking-a-vecu8-to-see-if-its-all-zero
23-
let (prefix, aligned, suffix) = unsafe { iv_or_salt.align_to::<u128>() };
24-
let is_zeros =
25-
prefix.iter().all(|&x| x == 0) && aligned.iter().all(|&x| x == 0) && suffix.iter().all(|&x| x == 0);
26-
27-
if !is_zeros {
28-
break;
29-
}
30-
}
31-
}
9+
#[deprecated(since = "0.5.8", note = "prefer utils::random_iv_or_salt")]
10+
pub use crate::utils::random_iv_or_salt;
3211

3312
/// Key derivation of OpenSSL's [EVP_BytesToKey](https://wiki.openssl.org/index.php/Manual:EVP_BytesToKey(3))
3413
pub fn openssl_bytes_to_key(password: &[u8], key: &mut [u8]) {

0 commit comments

Comments
 (0)