Skip to content

Commit

Permalink
30DaysOfTests: iv, salt and key length
Browse files Browse the repository at this point in the history
  • Loading branch information
conradkleinespel committed Aug 15, 2017
1 parent c2b5345 commit 4f60fa0
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions src/password/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ const SCRYPT_PARAM_LOG2_N: u8 = 12;
const SCRYPT_PARAM_R: u32 = 8;
const SCRYPT_PARAM_P: u32 = 1;

/// Returns the current default scrypt parameters
fn get_default_scrypt_params() -> scrypt::ScryptParams {
scrypt::ScryptParams::new(SCRYPT_PARAM_LOG2_N, SCRYPT_PARAM_R, SCRYPT_PARAM_P)
}

/// The version of this lib
const VERSION: u32 = 2;

Expand Down Expand Up @@ -141,7 +146,6 @@ fn digest(
Ok(digest)
}


/// The format of the encrypted JSON content in the password file v1.
#[derive(Serialize, Deserialize, Clone)]
pub struct Schema {
Expand Down Expand Up @@ -198,12 +202,13 @@ pub struct PasswordStore {
/// - encrypted blob: variable length
impl PasswordStore {
pub fn new(master_password: SafeString) -> IoResult<PasswordStore> {
// TODO: move this elsewhere so "get_default_scrypt_params"
let salt = generate_random_salt()?;

let scrypt_params =
scrypt::ScryptParams::new(SCRYPT_PARAM_LOG2_N, SCRYPT_PARAM_R, SCRYPT_PARAM_P);

let key = generate_encryption_key(scrypt_params, master_password.deref(), salt);
let key = generate_encryption_key(
get_default_scrypt_params(),
master_password.deref(),
salt,
);

Ok(PasswordStore {
key: key,
Expand Down Expand Up @@ -496,3 +501,31 @@ impl PasswordStore {
self.key = generate_encryption_key(scrypt_params, master_password, self.salt);
}
}

#[cfg(test)]
mod test {
use super::get_default_scrypt_params;
use password::v2::{generate_encryption_key, generate_random_iv, generate_random_salt};

#[test]
fn test_generate_random_iv_has_right_length() {
assert_eq!(generate_random_iv().unwrap().len(), 16);
}

#[test]
fn test_generate_random_salt_has_right_length() {
assert_eq!(generate_random_salt().unwrap().len(), 32);
}

#[test]
fn test_generate_encryption_key_returns_256_bits_key() {
assert_eq!(
generate_encryption_key(
get_default_scrypt_params(),
"hello world",
generate_random_salt().unwrap()
).len(),
32
);
}
}

0 comments on commit 4f60fa0

Please sign in to comment.