From 76388a3ef4007c8ae1bb6e609538715e8fab2207 Mon Sep 17 00:00:00 2001 From: Niko Date: Mon, 12 Jun 2023 23:22:46 +0300 Subject: [PATCH] allow pazzle_length of zero, sore its value in wallet --- ng-wallet/src/lib.rs | 51 +++++++++++++++++++++++++----------------- ng-wallet/src/types.rs | 9 +++++++- 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/ng-wallet/src/lib.rs b/ng-wallet/src/lib.rs index 3a6ea13..6ff7ffa 100644 --- a/ng-wallet/src/lib.rs +++ b/ng-wallet/src/lib.rs @@ -324,7 +324,11 @@ pub async fn create_wallet_v0( let creating_pazzle = Instant::now(); // pazzle_length can only be 9, 12, or 15 - if (params.pazzle_length != 9 && params.pazzle_length != 12 && params.pazzle_length != 15) { + if (params.pazzle_length != 9 + && params.pazzle_length != 12 + && params.pazzle_length != 15 + && params.pazzle_length != 0) + { return Err(NgWalletError::InvalidPazzleLength); } @@ -439,8 +443,22 @@ pub async fn create_wallet_v0( sites: vec![site], }; + let mut master_key = [0u8; 32]; + getrandom::getrandom(&mut master_key).map_err(|e| NgWalletError::InternalError)?; + let mut salt_pazzle = [0u8; 16]; - getrandom::getrandom(&mut salt_pazzle).map_err(|e| NgWalletError::InternalError)?; + let mut enc_master_key_pazzle = [0u8; 48]; + if params.pazzle_length > 0 { + getrandom::getrandom(&mut salt_pazzle).map_err(|e| NgWalletError::InternalError)?; + + let pazzle_key = derive_key_from_pass( + [pazzle.clone(), params.pin.to_vec()].concat(), + salt_pazzle, + wallet_id, + ); + + enc_master_key_pazzle = enc_master_key(master_key, pazzle_key, 0, wallet_id)?; + } let mut salt_mnemonic = [0u8; 16]; getrandom::getrandom(&mut salt_mnemonic).map_err(|e| NgWalletError::InternalError)?; @@ -448,23 +466,12 @@ pub async fn create_wallet_v0( //println!("salt_pazzle {:?}", salt_pazzle); //println!("salt_mnemonic {:?}", salt_mnemonic); - let pazzle_key = derive_key_from_pass( - [pazzle.clone(), params.pin.to_vec()].concat(), - salt_pazzle, - wallet_id, - ); - let mnemonic_key = derive_key_from_pass( [transmute_to_bytes(&mnemonic), ¶ms.pin].concat(), salt_mnemonic, wallet_id, ); - let mut master_key = [0u8; 32]; - getrandom::getrandom(&mut master_key).map_err(|e| NgWalletError::InternalError)?; - - let enc_master_key_pazzle = enc_master_key(master_key, pazzle_key, 0, wallet_id)?; - let enc_master_key_mnemonic = enc_master_key(master_key, mnemonic_key, 0, wallet_id)?; let timestamp = now_timestamp(); @@ -481,6 +488,7 @@ pub async fn create_wallet_v0( let wallet_content = WalletContentV0 { security_img: cursor.into_inner(), security_txt: new_string, + pazzle_length: params.pazzle_length, salt_pazzle, salt_mnemonic, enc_master_key_pazzle, @@ -630,16 +638,17 @@ mod tests { "opening of wallet with mnemonic took: {} ms", opening_mnemonic.elapsed().as_millis() ); - let opening_pazzle = Instant::now(); - let w = open_wallet_with_pazzle(Wallet::V0(v0.clone()), res.pazzle, pin) - .expect("open with pazzle"); + if v0.content.pazzle_length > 0 { + let opening_pazzle = Instant::now(); + let w = open_wallet_with_pazzle(Wallet::V0(v0.clone()), res.pazzle, pin) + .expect("open with pazzle"); + log!( + "opening of wallet with pazzle took: {} ms", + opening_pazzle.elapsed().as_millis() + ); + } //println!("encrypted part {:?}", w); - - log!( - "opening of wallet with pazzle took: {} ms", - opening_pazzle.elapsed().as_millis() - ); } } } diff --git a/ng-wallet/src/types.rs b/ng-wallet/src/types.rs index 15ff87b..023d83a 100644 --- a/ng-wallet/src/types.rs +++ b/ng-wallet/src/types.rs @@ -113,6 +113,9 @@ pub struct WalletContentV0 { pub security_txt: String, + /// can be 9, 12 or 15 (or 0, in this case salt_pazzle and enc_master_key_pazzle are filled with zeros and should not be used) + pub pazzle_length: u8, + pub salt_pazzle: [u8; 16], pub salt_mnemonic: [u8; 16], @@ -175,6 +178,11 @@ impl Wallet { Wallet::V0(v0) => v0.sig, } } + pub fn pazzle_length(&self) -> u8 { + match self { + Wallet::V0(v0) => v0.content.pazzle_length, + } + } } /// Add Wallet Version 0 @@ -216,7 +224,6 @@ impl AddWallet { pub struct CreateWalletV0 { #[serde(with = "serde_bytes")] pub security_img: Vec, - pub security_txt: String, pub pin: [u8; 4], pub pazzle_length: u8,