Browse Source

Repotting of AES stuff.

cl-refactor
Gav Wood 10 years ago
parent
commit
45372764f2
  1. 6
      alethzero/Main.ui
  2. 57
      alethzero/MainWin.cpp
  3. 6
      libdevcrypto/Common.cpp
  4. 3
      libdevcrypto/Common.h
  5. 5
      libdevcrypto/CryptoHeaders.h
  6. 25
      libdevcrypto/SHA3.cpp
  7. 2
      libdevcrypto/SHA3.h
  8. 2
      libevm/VM.h

6
alethzero/Main.ui

@ -194,11 +194,6 @@
<addaction name="dumpTraceStorage"/>
<addaction name="dumpTracePretty"/>
</widget>
<widget class="QMenu" name="menuType_Here">
<property name="title">
<string>Type Here</string>
</property>
</widget>
<addaction name="debugCurrent"/>
<addaction name="menu_Dump_Trace"/>
<addaction name="separator"/>
@ -208,7 +203,6 @@
<addaction name="debugStepBack"/>
<addaction name="debugStepBackInto"/>
<addaction name="debugStepBackOut"/>
<addaction name="menuType_Here"/>
</widget>
<addaction name="menu_File"/>
<addaction name="menu_View"/>

57
alethzero/MainWin.cpp

@ -28,11 +28,6 @@
#include <QtGui/QClipboard>
#include <QtCore/QtCore>
#include <boost/algorithm/string.hpp>
#include <cryptopp/aes.h>
#include <cryptopp/pwdbased.h>
#include <cryptopp/modes.h>
#include <cryptopp/sha.h>
#include <cryptopp/filters.h>
#include <test/JsonSpiritHeaders.h>
#include <libserpent/funcs.h>
#include <libserpent/util.h>
@ -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<CryptoPP::SHA256>().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 (...)
{

6
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)));
}

3
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.

5
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 <sha.h>
#include <sha3.h>
#include <ripemd.h>
#include <aes.h>
#include <pwdbased.h>
#include <modes.h>
#include <filters.h>
#include <secp256k1/secp256k1.h>
#pragma warning(pop)
#pragma GCC diagnostic pop

25
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<CryptoPP::SHA256>().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();
}
}
}
}

2
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());
}
}

2
libevm/VM.h

@ -585,7 +585,7 @@ template <class Ext> 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);

Loading…
Cancel
Save