Skip to content

Commit

Permalink
update: remove rnd from security-iv-printable-prefix feature (#844)
Browse files Browse the repository at this point in the history
Co-authored-by: ty <[email protected]>
  • Loading branch information
chuxi and zonyitoo authored May 22, 2022
1 parent 809b7d8 commit c27a3de
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions crates/shadowsocks/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,11 @@ impl Context {
{
const SECURITY_PRINTABLE_PREFIX_LEN: usize = 6;
if nonce.len() >= SECURITY_PRINTABLE_PREFIX_LEN {
use rand::Rng;
// Printable characters use base64 letters instead
static ASCII_PRINTABLE_CHARS: &[u8] = br##"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"##;

// Printable characters follows definition of isprint in C/C++
static ASCII_PRINTABLE_CHARS: &[u8] = br##"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ "##;

let mut rng = rand::thread_rng();
for b in nonce.iter_mut().take(SECURITY_PRINTABLE_PREFIX_LEN) {
*b = ASCII_PRINTABLE_CHARS[rng.gen_range::<usize, _>(0..ASCII_PRINTABLE_CHARS.len())];
*b = ASCII_PRINTABLE_CHARS[(*b as usize) % ASCII_PRINTABLE_CHARS.len()];
}
}
}
Expand Down Expand Up @@ -166,3 +163,20 @@ impl Context {
self.replay_policy
}
}

#[cfg(test)]
mod tests {
use crate::config::ServerType;
use crate::context::Context;
use byte_string::ByteStr;
use shadowsocks_crypto::CipherKind;

#[test]
fn generate_nonce() {
let mut salt = vec![0u8; 64];
let context = Context::new(ServerType::Server);
context.generate_nonce(CipherKind::AES_128_GCM, &mut salt, false);
println!("generate nonce printable ascii: {:?}", ByteStr::new(&salt));
}

}

0 comments on commit c27a3de

Please sign in to comment.