From 6ba1e66d32efca247679a3b8a946e5dc8dde255c Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Wed, 3 Aug 2016 15:24:09 +0200 Subject: [PATCH] Import wallet fix (#1820) * fixed importing presale wallet with encseed longer than 96 bytes * fixed incorrect pkcs unpadding in decrypting presale wallet --- ethstore/src/bin/ethstore.rs | 2 +- ethstore/src/crypto.rs | 10 ++++++---- ethstore/src/presale.rs | 13 +++++++------ 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/ethstore/src/bin/ethstore.rs b/ethstore/src/bin/ethstore.rs index 5683a8116..748480069 100644 --- a/ethstore/src/bin/ethstore.rs +++ b/ethstore/src/bin/ethstore.rs @@ -138,7 +138,7 @@ fn execute(command: I) -> Result where I: IntoIterator Result<(), SymmetricCipherError> { + pub fn decrypt_cbc(k: &[u8], iv: &[u8], encrypted: &[u8], dest: &mut [u8]) -> Result { let mut encryptor = CbcDecryptor::new(AesSafe128Decryptor::new(k), PkcsPadding, iv.to_vec()); - try!(encryptor.decrypt(&mut RefReadBuffer::new(encrypted), &mut RefWriteBuffer::new(dest), true)); - Ok(()) + let len = dest.len(); + let mut buffer = RefWriteBuffer::new(dest); + try!(encryptor.decrypt(&mut RefReadBuffer::new(encrypted), &mut buffer, true)); + Ok(len - buffer.remaining()) } } diff --git a/ethstore/src/presale.rs b/ethstore/src/presale.rs index 09c86abea..2904db6ef 100644 --- a/ethstore/src/presale.rs +++ b/ethstore/src/presale.rs @@ -10,7 +10,7 @@ use {crypto, Error}; pub struct PresaleWallet { iv: [u8; 16], - ciphertext: [u8; 80], + ciphertext: Vec, address: Address, } @@ -19,8 +19,8 @@ impl From for PresaleWallet { let mut iv = [0u8; 16]; iv.copy_from_slice(&wallet.encseed[..16]); - let mut ciphertext = [0u8; 80]; - ciphertext.copy_from_slice(&wallet.encseed[16..]); + let mut ciphertext = vec![]; + ciphertext.extend_from_slice(&wallet.encseed[16..]); PresaleWallet { iv: iv, @@ -42,10 +42,11 @@ impl PresaleWallet { let mut derived_key = vec![0u8; 16]; pbkdf2(&mut h_mac, password.as_bytes(), 2000, &mut derived_key); - let mut key = [0u8; 64]; - try!(crypto::aes::decrypt_cbc(&derived_key, &self.iv, &self.ciphertext, &mut key).map_err(|_| Error::InvalidPassword)); + let mut key = vec![0; self.ciphertext.len()]; + let len = try!(crypto::aes::decrypt_cbc(&derived_key, &self.iv, &self.ciphertext, &mut key).map_err(|_| Error::InvalidPassword)); + let unpadded = &key[..len]; - let secret = Secret::from(key.keccak256()); + let secret = Secret::from(unpadded.keccak256()); if let Ok(kp) = KeyPair::from_secret(secret) { if kp.address() == self.address { return Ok(kp)