From 45372764f2a9543bda4ffc701903d07d01574c8f Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 1 Oct 2014 17:26:39 +0100 Subject: [PATCH] Repotting of AES stuff. --- alethzero/Main.ui | 6 ---- alethzero/MainWin.cpp | 57 ++++++++---------------------------- libdevcrypto/Common.cpp | 6 ++++ libdevcrypto/Common.h | 3 ++ libdevcrypto/CryptoHeaders.h | 5 ++++ libdevcrypto/SHA3.cpp | 25 ++++++++++++++++ libdevcrypto/SHA3.h | 2 ++ libevm/VM.h | 2 +- 8 files changed, 54 insertions(+), 52 deletions(-) diff --git a/alethzero/Main.ui b/alethzero/Main.ui index 3567d3ffb..8319e11ef 100644 --- a/alethzero/Main.ui +++ b/alethzero/Main.ui @@ -194,11 +194,6 @@ - - - Type Here - - @@ -208,7 +203,6 @@ - diff --git a/alethzero/MainWin.cpp b/alethzero/MainWin.cpp index 8f8f09f3c..114712f24 100644 --- a/alethzero/MainWin.cpp +++ b/alethzero/MainWin.cpp @@ -28,11 +28,6 @@ #include #include #include -#include -#include -#include -#include -#include #include #include #include @@ -586,40 +581,11 @@ void Main::on_importKeyFile_triggered() { js::mValue val; json_spirit::read_string(asString(contents(s.toStdString())), val); - js::mObject obj = val.get_obj(); - KeyPair k; - + auto obj = val.get_obj(); if (obj["encseed"].type() == js::str_type) { - QString pw = QInputDialog::getText(this, "Enter Password", "Enter the wallet's passphrase", QLineEdit::Password); - - string encseedstr = obj["encseed"].get_str(); - bytes encseed = fromHex(encseedstr); - bytes pwbytes = asBytes(pw.toStdString()); - - byte targetBuffer[64]; - byte saltBuffer[64]; - CryptoPP::PKCS5_PBKDF2_HMAC().DeriveKey(targetBuffer, 64, 0, pwbytes.data(), pwbytes.size(), saltBuffer, 0, 2000); - - try - { - CryptoPP::AES::Decryption aesDecryption(targetBuffer, 64); - byte iv[CryptoPP::AES::BLOCKSIZE]; - CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption(aesDecryption, iv); - std::string decrypted; - CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink(decrypted)); - stfDecryptor.Put(encseed.data(), encseed.size()); - stfDecryptor.MessageEnd(); - encseed = asBytes(decrypted); - } - catch (exception const& e) - { - cerr << e.what() << endl; - return; - } - - auto sec = sha3(encseed); - k = KeyPair(sec); + auto encseed = fromHex(obj["encseed"].get_str()); + KeyPair k = KeyPair::fromEncryptedSeed(&encseed, QInputDialog::getText(this, "Enter Password", "Enter the wallet's passphrase", QLineEdit::Password).toStdString()); if (obj["ethaddr"].type() == js::str_type) { Address a(obj["ethaddr"].get_str()); @@ -627,17 +593,18 @@ void Main::on_importKeyFile_triggered() if (a != b && QMessageBox::warning(this, "Key File Invalid", "Could not import the secret key: it doesn't agree with the given address.\nWould you like to attempt to import anyway?", QMessageBox::Yes | QMessageBox::No) == QMessageBox::No) return; } + + if (std::find(m_myKeys.begin(), m_myKeys.end(), k) == m_myKeys.end()) + { + m_myKeys.append(k); + m_keysChanged = true; + update(); + } + else + QMessageBox::warning(this, "Already Have Key", "Could not import the secret key: we already own this account."); } else throw 0; - if (std::find(m_myKeys.begin(), m_myKeys.end(), k) == m_myKeys.end()) - { - m_myKeys.append(k); - m_keysChanged = true; - update(); - } - else - QMessageBox::warning(this, "Already Have Key", "Could not import the secret key: we already own this account."); } catch (...) { diff --git a/libdevcrypto/Common.cpp b/libdevcrypto/Common.cpp index ddc7b0b4b..dd4d6961c 100644 --- a/libdevcrypto/Common.cpp +++ b/libdevcrypto/Common.cpp @@ -102,3 +102,9 @@ KeyPair::KeyPair(h256 _sec): cout << "ADR: " << m_address << endl; #endif } + +KeyPair KeyPair::fromEncryptedSeed(bytesConstRef _seed, std::string const& _password) +{ + return KeyPair(sha3(aesDecrypt(_seed, _password))); +} + diff --git a/libdevcrypto/Common.h b/libdevcrypto/Common.h index 2694d7f8d..d55bab51d 100644 --- a/libdevcrypto/Common.h +++ b/libdevcrypto/Common.h @@ -63,6 +63,9 @@ public: /// Create a new, randomly generated object. static KeyPair create(); + /// Create from an encrypted seed. + static KeyPair fromEncryptedSeed(bytesConstRef _seed, std::string const& _password); + /// Retrieve the secret key. Secret const& secret() const { return m_secret; } /// Retrieve the secret key. diff --git a/libdevcrypto/CryptoHeaders.h b/libdevcrypto/CryptoHeaders.h index 4ff63f1d7..0361091e8 100644 --- a/libdevcrypto/CryptoHeaders.h +++ b/libdevcrypto/CryptoHeaders.h @@ -28,9 +28,14 @@ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wconversion" #pragma GCC diagnostic ignored "-Wunused-parameter" +#pragma GCC diagnostic ignored "-Wunused-variable" #include #include #include +#include +#include +#include +#include #include #pragma warning(pop) #pragma GCC diagnostic pop diff --git a/libdevcrypto/SHA3.cpp b/libdevcrypto/SHA3.cpp index cdd1002e6..e1da1261e 100644 --- a/libdevcrypto/SHA3.cpp +++ b/libdevcrypto/SHA3.cpp @@ -72,5 +72,30 @@ h256 sha3(bytesConstRef _input) return ret; } +bytes aesDecrypt(bytesConstRef _cipher, std::string const& _password, unsigned _rounds, bytesConstRef _salt) +{ + bytes pw = asBytes(_password); + bytes target(CryptoPP::AES::DEFAULT_KEYLENGTH); + + CryptoPP::PKCS5_PBKDF2_HMAC().DeriveKey(target.data(), target.size(), 0, pw.data(), pw.size(), _salt.data(), _salt.size(), _rounds); + + try + { + CryptoPP::AES::Decryption aesDecryption(target.data(), target.size()); + bytes iv(CryptoPP::AES::BLOCKSIZE); + CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption(aesDecryption, iv.data()); + std::string decrypted; + CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink(decrypted)); + stfDecryptor.Put(_cipher.data(), _cipher.size()); + stfDecryptor.MessageEnd(); + return asBytes(decrypted); + } + catch (exception const& e) + { + cerr << e.what() << endl; + return bytes(); + } +} + } } diff --git a/libdevcrypto/SHA3.h b/libdevcrypto/SHA3.h index 1b37846b3..f3837fcc9 100644 --- a/libdevcrypto/SHA3.h +++ b/libdevcrypto/SHA3.h @@ -60,5 +60,7 @@ inline h256 sha3(std::string const& _input) { return sha3(bytesConstRef(_input)) extern h256 EmptySHA3; +bytes aesDecrypt(bytesConstRef _cipher, std::string const& _password, unsigned _rounds = 2000, bytesConstRef _salt = bytesConstRef()); + } } diff --git a/libevm/VM.h b/libevm/VM.h index 3cac55b1f..f28f5fdf6 100644 --- a/libevm/VM.h +++ b/libevm/VM.h @@ -585,7 +585,7 @@ template dev::bytesConstRef dev::eth::VM::go(Ext& _ext, OnOpFunc con m_stack.push_back(m_curPC); break; case Instruction::MSIZE: - m_stack.push_back(m_temp.size()); + m_stack.push_back(m_temp.size() / 32); break; case Instruction::GAS: m_stack.push_back(m_gas);