Permalink
Browse files

30DaysOfTests: iv, salt and key length

  • Loading branch information...
conradkdotcom committed Aug 15, 2017
1 parent c2b5345 commit 4f60fa0425e06d5ac7e01d9dfa5d1b38790d5b19
Showing with 39 additions and 6 deletions.
  1. +39 −6 src/password/v2.rs
View
@@ -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;
@@ -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 {
@@ -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,
@@ -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.