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

Upgrade dependencies #8

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ cc = "^1.0.79"

[dependencies]
libc = { version = "^0.2", default-features = false }
rand = "^0.3"
base64 = "^0.5"
hex = "^0.2"
rand = "^0.4"
getrandom = { version = "^0.2", features = ["std"] }
base64 = "^0.21"
hex = "^0.4.3"
2 changes: 1 addition & 1 deletion src/aesni_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions src/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@ pub trait Digest {
* String in hexadecimal format.
*/
fn result_str(&mut self) -> String {
use hex::ToHex;

let mut buf: Vec<u8> = repeat(0).take((self.output_bits() + 7) / 8).collect();
self.result(&mut buf);
(&buf[..]).to_hex()
hex::encode(&buf[..])
}
}
21 changes: 9 additions & 12 deletions src/pbkdf2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ use cryptoutil::copy_memory;
use std::io;
use std::iter::repeat;

use base64;
use rand::{OsRng, Rng};

use base64::{self, Engine};
use cryptoutil::{read_u32_be, write_u32_be};
use hmac::Hmac;
use mac::Mac;
Expand Down Expand Up @@ -130,10 +128,9 @@ pub fn pbkdf2<M: Mac>(mac: &mut M, salt: &[u8], c: u32, output: &mut [u8]) {
*
*/
pub fn pbkdf2_simple(password: &str, c: u32) -> io::Result<String> {
let mut rng = OsRng::new()?;

// 128-bit salt
let salt: Vec<u8> = rng.gen_iter::<u8>().take(16).collect();
let mut salt = [0u8, 16];
getrandom::getrandom(&mut salt)?;

// 256-bit derived key
let mut dk = [0u8; 32];
Expand All @@ -145,11 +142,11 @@ pub fn pbkdf2_simple(password: &str, c: u32) -> io::Result<String> {
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)
Expand Down Expand Up @@ -201,7 +198,7 @@ pub fn pbkdf2_check(password: &str, hashed_value: &str) -> Result<bool, &'static

// Parse the iteration count
let c = match iter.next() {
Some(pstr) => match base64::decode(pstr) {
Some(pstr) => match base64::engine::general_purpose::STANDARD.decode(pstr) {
Ok(pvec) => {
if pvec.len() != 4 {
return Err(ERR_STR);
Expand All @@ -215,7 +212,7 @@ pub fn pbkdf2_check(password: &str, hashed_value: &str) -> Result<bool, &'static

// Salt
let salt = match iter.next() {
Some(sstr) => match base64::decode(sstr) {
Some(sstr) => match base64::engine::general_purpose::STANDARD.decode(sstr) {
Ok(salt) => salt,
Err(_) => return Err(ERR_STR),
},
Expand All @@ -224,7 +221,7 @@ pub fn pbkdf2_check(password: &str, hashed_value: &str) -> Result<bool, &'static

// Hashed value
let hash = match iter.next() {
Some(hstr) => match base64::decode(hstr) {
Some(hstr) => match base64::engine::general_purpose::STANDARD.decode(hstr) {
Ok(hash) => hash,
Err(_) => return Err(ERR_STR),
},
Expand Down
25 changes: 11 additions & 14 deletions src/scrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ use std::io;
use std::iter::repeat;
use std::mem::size_of;

use base64;
use rand::{OsRng, Rng};

use base64::{self, Engine};
use cryptoutil::{read_u32_le, read_u32v_le, write_u32_le};
use hmac::Hmac;
use pbkdf2::pbkdf2;
Expand Down Expand Up @@ -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<String> {
let mut rng = OsRng::new()?;

// 128-bit salt
let salt: Vec<u8> = rng.gen_iter::<u8>().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 {
Expand All @@ -293,19 +290,19 @@ pub fn scrypt_simple(password: &str, params: &ScryptParams) -> io::Result<String
tmp[0] = params.log_n;
tmp[1] = params.r as u8;
tmp[2] = params.p as u8;
result.push_str(&*base64::encode_config(&tmp, base64::STANDARD));
result.push_str(&*base64::engine::general_purpose::STANDARD.encode(&tmp));
} else {
result.push_str("1$");
let mut tmp = [0u8; 9];
tmp[0] = params.log_n;
write_u32_le(&mut tmp[1..5], params.r);
write_u32_le(&mut tmp[5..9], params.p);
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)
Expand Down Expand Up @@ -353,7 +350,7 @@ pub fn scrypt_check(password: &str, hashed_value: &str) -> Result<bool, &'static
// Parse the parameters - the size of them depends on the if we are using the compact or
// expanded format
let pvec = match iter.next() {
Some(pstr) => match base64::decode(pstr) {
Some(pstr) => match base64::engine::general_purpose::STANDARD.decode(pstr) {
Ok(x) => x,
Err(_) => return Err(ERR_STR),
},
Expand Down Expand Up @@ -386,7 +383,7 @@ pub fn scrypt_check(password: &str, hashed_value: &str) -> Result<bool, &'static

// Salt
let salt = match iter.next() {
Some(sstr) => match base64::decode(sstr) {
Some(sstr) => match base64::engine::general_purpose::STANDARD.decode(sstr) {
Ok(salt) => salt,
Err(_) => return Err(ERR_STR),
},
Expand All @@ -395,7 +392,7 @@ pub fn scrypt_check(password: &str, hashed_value: &str) -> Result<bool, &'static

// Hashed value
let hash = match iter.next() {
Some(hstr) => match base64::decode(hstr) {
Some(hstr) => match base64::engine::general_purpose::STANDARD.decode(hstr) {
Ok(hash) => hash,
Err(_) => return Err(ERR_STR),
},
Expand Down
7 changes: 3 additions & 4 deletions src/sha3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down