From 54d57cdd8e57354a274fe443e68ee34ef88cdee4 Mon Sep 17 00:00:00 2001 From: glennsl Date: Mon, 8 May 2023 14:44:01 +0200 Subject: [PATCH 1/5] chore(dependencies): hex -> 0.4.3 --- Cargo.toml | 2 +- src/digest.rs | 4 +--- src/sha3.rs | 7 +++---- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7f0d1b4..8ab0ac4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,4 +23,4 @@ cc = "^1.0.79" libc = { version = "^0.2", default-features = false } rand = "^0.3" base64 = "^0.5" -hex = "^0.2" +hex = "^0.4.3" diff --git a/src/digest.rs b/src/digest.rs index b9e57aa..b57d10e 100644 --- a/src/digest.rs +++ b/src/digest.rs @@ -72,10 +72,8 @@ pub trait Digest { * String in hexadecimal format. */ fn result_str(&mut self) -> String { - use hex::ToHex; - let mut buf: Vec = repeat(0).take((self.output_bits() + 7) / 8).collect(); self.result(&mut buf); - (&buf[..]).to_hex() + hex::encode(&buf[..]) } } diff --git a/src/sha3.rs b/src/sha3.rs index 626e6ee..c6a43b6 100644 --- a/src/sha3.rs +++ b/src/sha3.rs @@ -449,7 +449,6 @@ impl Clone for Sha3 { #[cfg(test)] mod tests { use digest::Digest; - use hex::{self, ToHex}; use sha3::{Sha3, Sha3Mode}; struct Test { @@ -471,8 +470,8 @@ mod tests { let mut out_str = vec![0u8; t.output_str.len() / 2]; sh.result(&mut out_str); - println!("{}", &out_str.to_hex()); - assert!(&out_str.to_hex() == t.output_str); + println!("{}", hex::encode(&out_str)); + assert!(hex::encode(&out_str) == t.output_str); sh.reset(); } @@ -491,7 +490,7 @@ mod tests { sh.result(&mut out_str); - assert!(&out_str.to_hex() == t.output_str); + assert!(hex::encode(&out_str) == t.output_str); sh.reset(); } From 06f27adf47593b6dfe2e3d175be5e5bca27759bc Mon Sep 17 00:00:00 2001 From: glennsl Date: Mon, 8 May 2023 14:55:34 +0200 Subject: [PATCH 2/5] chore(dependencies): base64 -> 0.21 --- Cargo.toml | 2 +- src/pbkdf2.rs | 14 +++++++------- src/scrypt.rs | 16 ++++++++-------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8ab0ac4..c3e5435 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,5 +22,5 @@ cc = "^1.0.79" [dependencies] libc = { version = "^0.2", default-features = false } rand = "^0.3" -base64 = "^0.5" +base64 = "^0.21" hex = "^0.4.3" diff --git a/src/pbkdf2.rs b/src/pbkdf2.rs index 781ea94..9b4d155 100644 --- a/src/pbkdf2.rs +++ b/src/pbkdf2.rs @@ -13,7 +13,7 @@ use cryptoutil::copy_memory; use std::io; use std::iter::repeat; -use base64; +use base64::{self, Engine}; use rand::{OsRng, Rng}; use cryptoutil::{read_u32_be, write_u32_be}; @@ -145,11 +145,11 @@ pub fn pbkdf2_simple(password: &str, c: u32) -> io::Result { let mut result = "$rpbkdf2$0$".to_string(); let mut tmp = [0u8; 4]; write_u32_be(&mut tmp, c); - result.push_str(&base64::encode_config(&tmp, base64::STANDARD)[..]); + result.push_str(&base64::engine::general_purpose::STANDARD.encode(&tmp)); result.push('$'); - result.push_str(&base64::encode_config(&salt, base64::STANDARD)[..]); + result.push_str(&base64::engine::general_purpose::STANDARD.encode(&salt)); result.push('$'); - result.push_str(&base64::encode_config(&dk, base64::STANDARD)[..]); + result.push_str(&base64::engine::general_purpose::STANDARD.encode(&dk)); result.push('$'); Ok(result) @@ -201,7 +201,7 @@ pub fn pbkdf2_check(password: &str, hashed_value: &str) -> Result match base64::decode(pstr) { + Some(pstr) => match base64::engine::general_purpose::STANDARD.decode(pstr) { Ok(pvec) => { if pvec.len() != 4 { return Err(ERR_STR); @@ -215,7 +215,7 @@ pub fn pbkdf2_check(password: &str, hashed_value: &str) -> Result match base64::decode(sstr) { + Some(sstr) => match base64::engine::general_purpose::STANDARD.decode(sstr) { Ok(salt) => salt, Err(_) => return Err(ERR_STR), }, @@ -224,7 +224,7 @@ pub fn pbkdf2_check(password: &str, hashed_value: &str) -> Result match base64::decode(hstr) { + Some(hstr) => match base64::engine::general_purpose::STANDARD.decode(hstr) { Ok(hash) => hash, Err(_) => return Err(ERR_STR), }, diff --git a/src/scrypt.rs b/src/scrypt.rs index af3f1dc..21d04f8 100644 --- a/src/scrypt.rs +++ b/src/scrypt.rs @@ -18,7 +18,7 @@ use std::io; use std::iter::repeat; use std::mem::size_of; -use base64; +use base64::{self, Engine}; use rand::{OsRng, Rng}; use cryptoutil::{read_u32_le, read_u32v_le, write_u32_le}; @@ -293,19 +293,19 @@ pub fn scrypt_simple(password: &str, params: &ScryptParams) -> io::Result Result match base64::decode(pstr) { + Some(pstr) => match base64::engine::general_purpose::STANDARD.decode(pstr) { Ok(x) => x, Err(_) => return Err(ERR_STR), }, @@ -386,7 +386,7 @@ pub fn scrypt_check(password: &str, hashed_value: &str) -> Result match base64::decode(sstr) { + Some(sstr) => match base64::engine::general_purpose::STANDARD.decode(sstr) { Ok(salt) => salt, Err(_) => return Err(ERR_STR), }, @@ -395,7 +395,7 @@ pub fn scrypt_check(password: &str, hashed_value: &str) -> Result match base64::decode(hstr) { + Some(hstr) => match base64::engine::general_purpose::STANDARD.decode(hstr) { Ok(hash) => hash, Err(_) => return Err(ERR_STR), }, From 16e76817a32d53dffccfa01efc6b764755d3bb25 Mon Sep 17 00:00:00 2001 From: glennsl Date: Mon, 8 May 2023 16:06:20 +0200 Subject: [PATCH 3/5] refactor: use getrandom instead of deprecated OsRng::new --- Cargo.toml | 1 + src/pbkdf2.rs | 7 ++----- src/scrypt.rs | 9 +++------ 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c3e5435..79ce144 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,5 +22,6 @@ cc = "^1.0.79" [dependencies] libc = { version = "^0.2", default-features = false } rand = "^0.3" +getrandom = { version = "^0.2", features = ["std"] } base64 = "^0.21" hex = "^0.4.3" diff --git a/src/pbkdf2.rs b/src/pbkdf2.rs index 9b4d155..3635b16 100644 --- a/src/pbkdf2.rs +++ b/src/pbkdf2.rs @@ -14,8 +14,6 @@ use std::io; use std::iter::repeat; use base64::{self, Engine}; -use rand::{OsRng, Rng}; - use cryptoutil::{read_u32_be, write_u32_be}; use hmac::Hmac; use mac::Mac; @@ -130,10 +128,9 @@ pub fn pbkdf2(mac: &mut M, salt: &[u8], c: u32, output: &mut [u8]) { * */ pub fn pbkdf2_simple(password: &str, c: u32) -> io::Result { - let mut rng = OsRng::new()?; - // 128-bit salt - let salt: Vec = rng.gen_iter::().take(16).collect(); + let mut salt = [0u8, 16]; + getrandom::getrandom(&mut salt)?; // 256-bit derived key let mut dk = [0u8; 32]; diff --git a/src/scrypt.rs b/src/scrypt.rs index 21d04f8..a515372 100644 --- a/src/scrypt.rs +++ b/src/scrypt.rs @@ -19,8 +19,6 @@ use std::iter::repeat; use std::mem::size_of; use base64::{self, Engine}; -use rand::{OsRng, Rng}; - use cryptoutil::{read_u32_le, read_u32v_le, write_u32_le}; use hmac::Hmac; use pbkdf2::pbkdf2; @@ -276,15 +274,14 @@ pub fn scrypt(password: &[u8], salt: &[u8], params: &ScryptParams, output: &mut * */ pub fn scrypt_simple(password: &str, params: &ScryptParams) -> io::Result { - let mut rng = OsRng::new()?; - // 128-bit salt - let salt: Vec = rng.gen_iter::().take(16).collect(); + let mut salt = [0u8, 16]; + getrandom::getrandom(&mut salt)?; // 256-bit derived key let mut dk = [0u8; 32]; - scrypt(password.as_bytes(), &*salt, params, &mut dk); + scrypt(password.as_bytes(), &salt, params, &mut dk); let mut result = "$rscrypt$".to_string(); if params.r < 256 && params.p < 256 { From fde4458e792dc6a0cbcff234e1a05f2b9040808d Mon Sep 17 00:00:00 2001 From: glennsl Date: Mon, 8 May 2023 16:10:27 +0200 Subject: [PATCH 4/5] chore(dependencies): rand -> 0.4 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 79ce144..6e9e394 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ cc = "^1.0.79" [dependencies] libc = { version = "^0.2", default-features = false } -rand = "^0.3" +rand = "^0.4" getrandom = { version = "^0.2", features = ["std"] } base64 = "^0.21" hex = "^0.4.3" From 3bdf517479b8e036748edc3ffea7c612fb4a303b Mon Sep 17 00:00:00 2001 From: glennsl Date: Mon, 8 May 2023 16:12:25 +0200 Subject: [PATCH 5/5] chore: fix "unknown escape sequence" warning --- src/aesni_helpers.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aesni_helpers.c b/src/aesni_helpers.c index c889068..8af03ef 100644 --- a/src/aesni_helpers.c +++ b/src/aesni_helpers.c @@ -71,7 +71,7 @@ void rust_crypto_aesni_setup_working_key_128( pxor %%xmm2, %%xmm1; \ movdqu %%xmm1, (%0); \ add $0x10, %0; \ - ret; \ \ + ret; \ 2: \ " : "+r" (round_key)