Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: remove rnd from security-iv-printable-prefix feature #844

Merged
merged 3 commits into from
May 22, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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::NONE, &mut salt, false);
zonyitoo marked this conversation as resolved.
Show resolved Hide resolved
println!("generate nonce printable ascii: {:?}", ByteStr::new(&salt));
}

}