IV generation

This commit is contained in:
femsci 2022-12-18 15:24:04 +01:00
parent 821af01c8f
commit 4ec212dd4b
Signed by: femsci
GPG key ID: 08F7911F0E650C67
3 changed files with 9 additions and 5 deletions

2
Cargo.lock generated
View file

@ -598,7 +598,7 @@ dependencies = [
"password-hash 0.4.2", "password-hash 0.4.2",
"passwords", "passwords",
"pbkdf2", "pbkdf2",
"rand_core", "rand",
"reqwest", "reqwest",
"sha2", "sha2",
"sha3", "sha3",

View file

@ -12,7 +12,7 @@ argon2 = "0.4.1"
bcrypt = "0.13.0" bcrypt = "0.13.0"
pbkdf2 = "0.10" pbkdf2 = "0.10"
password-hash = "0.4.2" password-hash = "0.4.2"
rand_core = { version = "0.6", features = ["std"] } rand = "0.8.5"
passwords = "3.1.12" passwords = "3.1.12"
reqwest = "0.11.13" reqwest = "0.11.13"

View file

@ -8,6 +8,7 @@ use std::{
use aes_gcm::{aead::Aead, Aes256Gcm, KeyInit, Nonce}; use aes_gcm::{aead::Aead, Aes256Gcm, KeyInit, Nonce};
use argon2::PasswordHasher; use argon2::PasswordHasher;
use password_hash::SaltString; use password_hash::SaltString;
use rand::{rngs::StdRng, RngCore, SeedableRng};
use super::{Store, StoreRecord}; use super::{Store, StoreRecord};
@ -217,13 +218,16 @@ fn serialize_header(header: &LocalStoreHeader) -> Vec<u8> {
fn try_create_store(path: &String, passphrase: &Vec<u8>) -> bool { fn try_create_store(path: &String, passphrase: &Vec<u8>) -> bool {
let timestamp: u32 = std::time::UNIX_EPOCH.elapsed().unwrap().as_secs() as u32; let timestamp: u32 = std::time::UNIX_EPOCH.elapsed().unwrap().as_secs() as u32;
let salt = SaltString::generate(&mut rand_core::OsRng); let mut rng = StdRng::from_entropy();
let salt = SaltString::generate(&mut rng);
let (default_hash_type, default_enc_type) = (HashType::Argon2, EncryptionType::AesGcm); let (default_hash_type, default_enc_type) = (HashType::Argon2, EncryptionType::AesGcm);
let key = derive_key(&passphrase, &salt, default_hash_type).unwrap(); let key = derive_key(&passphrase, &salt, default_hash_type).unwrap();
let iv: &[u8; 12] = b"unique nonce"; //TODO: Nonce generation let mut iv: [u8; 12] = [0; 12];
rng.fill_bytes(&mut iv);
let mut salt_buf: [u8; 16] = [0; 16]; let mut salt_buf: [u8; 16] = [0; 16];
salt.b64_decode(&mut salt_buf).unwrap(); salt.b64_decode(&mut salt_buf).unwrap();
@ -262,7 +266,7 @@ fn try_create_store(path: &String, passphrase: &Vec<u8>) -> bool {
let mut buf = serialize_header(&header); let mut buf = serialize_header(&header);
buf.extend_from_slice(&salt_buf); buf.extend_from_slice(&salt_buf);
buf.extend_from_slice(iv); buf.extend_from_slice(&iv);
buf.append([b'\0'].repeat(16 - iv.len()).as_mut()); buf.append([b'\0'].repeat(16 - iv.len()).as_mut());
buf.extend_from_slice(&enc_buf); buf.extend_from_slice(&enc_buf);