diff options
author | Katharina Fey <kookie@spacekookie.de> | 2017-12-15 13:47:16 +0100 |
---|---|---|
committer | Katharina Fey <kookie@spacekookie.de> | 2017-12-15 13:47:16 +0100 |
commit | a5711348aeabb089f7da2c5951ba95562d0128a4 (patch) | |
tree | 7687a095236e9d1fde7cfa04178c2d2f46de72f1 /src | |
parent | 9368787f0649a1ecb8f05da6610386c40c9c3e53 (diff) |
Using key utils. Doing further testing
Diffstat (limited to '')
-rw-r--r-- | src/crypto/engine.rs | 19 | ||||
-rw-r--r-- | src/crypto/mod.rs | 6 | ||||
-rw-r--r-- | src/vault/mod.rs | 16 |
3 files changed, 24 insertions, 17 deletions
diff --git a/src/crypto/engine.rs b/src/crypto/engine.rs index e083a94..33ed62b 100644 --- a/src/crypto/engine.rs +++ b/src/crypto/engine.rs @@ -2,11 +2,13 @@ //! //! -use super::DEFAULT_KEYLENGTH; +// use super::DEFAULT_KEYLENGTH; use super::encoding; use super::random; use super::hash; +use super::key::KEY_LENGTH; + use aesni::{Aes128, BlockCipher}; use generic_array::GenericArray; use std::str::from_utf8_unchecked; @@ -25,7 +27,8 @@ impl CryptoEngine { pub fn new(password: &str, _: &str) -> CryptoEngine { /* Generate a random key */ - let secret_key = random::bytes(DEFAULT_KEYLENGTH); + let secret_key = random::bytes(KEY_LENGTH); + println!("RAW KEY key: {}", encoding::encode_base64(&CryptoEngine::vec_to_str(&secret_key))); /* Encrypt secret_key with password */ let k = hash::blake2_16(password, ""); @@ -84,7 +87,7 @@ impl CryptoEngine { let mut encrypted: Vec<u8> = Vec::new(); let mut start: usize = 0; - let mut stop: usize = DEFAULT_KEYLENGTH; + let mut stop: usize = KEY_LENGTH; loop { let slice = to_encrypt[start..stop].as_bytes(); @@ -98,7 +101,7 @@ impl CryptoEngine { } start = stop; - stop += DEFAULT_KEYLENGTH; + stop += KEY_LENGTH; if to_encrypt.len() < stop { break; } @@ -117,7 +120,7 @@ impl CryptoEngine { let sliced = CryptoEngine::str_to_vec(&data); let mut start: usize = 0; - let mut stop: usize = DEFAULT_KEYLENGTH; + let mut stop: usize = KEY_LENGTH; loop { let slice = &sliced[start..stop]; @@ -128,7 +131,7 @@ impl CryptoEngine { decryted.push_str(&CryptoEngine::vec_to_str(&block)); start = stop; - stop += DEFAULT_KEYLENGTH; + stop += KEY_LENGTH; if sliced.len() < stop { break; } @@ -157,14 +160,14 @@ impl CryptoEngine { /// data padding soon. But it works for now, I guess fn pad_data(&self, data: &str) -> String { - if data.len() % DEFAULT_KEYLENGTH == 0 { + if data.len() % KEY_LENGTH == 0 { return String::from(data); } return format!( "{: <width$}", data, - width = data.len() + (data.len() % DEFAULT_KEYLENGTH) + width = data.len() + (data.len() % KEY_LENGTH) ); } } diff --git a/src/crypto/mod.rs b/src/crypto/mod.rs index 423d33a..9e7fc13 100644 --- a/src/crypto/mod.rs +++ b/src/crypto/mod.rs @@ -3,9 +3,11 @@ //! A crypto engine is attached to a vault and provides easy to use //! and high-level encryption and decryption functions. -pub mod hash; +pub mod encoding; pub mod random; pub mod engine; -pub mod encoding; +pub mod hash; +pub mod key; +#[deprecated] const DEFAULT_KEYLENGTH: usize = 16; diff --git a/src/vault/mod.rs b/src/vault/mod.rs index 70e0bb0..b7dd62d 100644 --- a/src/vault/mod.rs +++ b/src/vault/mod.rs @@ -19,6 +19,7 @@ use std::fs::File; use std::fs; use crypto::engine::CryptoEngine; +use crypto::key; use record::{Record, Payload}; use serde_json; @@ -79,17 +80,16 @@ impl Vault { pathbuf.push(format!("{}.vault", name)); /* Load the secret key */ - let mut key = String::new(); + // let mut key = String::new(); + let k: String; { pathbuf.push("primary.key"); let key_path = pathbuf.as_os_str(); - let mut key_file = File::open(key_path).unwrap(); - key_file.read_to_string(&mut key).expect( - "Failed to load primary key file!", - ); - }; + k = key::load_key(key_path); + } - let crypto = CryptoEngine::load_existing(&key, password); + println!("Existing key: {}", k); + let crypto = CryptoEngine::load_existing(&k, password); /* Load all existing records */ pathbuf.pop(); @@ -201,6 +201,8 @@ impl Vault { None => return ErrorType::FailedToInitialise, }; + println!("Primary key: {}", key); + /* Write encrypted key to disk */ { self.path.push("primary.key"); |