13 #include <boost/foreach.hpp> 14 #include <openssl/aes.h> 15 #include <openssl/evp.h> 23 if (nDerivationMethod == 0)
24 i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &chSalt[0],
25 (
unsigned char *)&strKeyData[0], strKeyData.size(), nRounds,
chKey,
chIV);
57 int nLen = vchPlaintext.size();
58 int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
59 vchCiphertext = std::vector<unsigned char> (nCLen);
61 EVP_CIPHER_CTX *
ctx = EVP_CIPHER_CTX_new();
63 if (!
ctx)
return false;
67 EVP_CIPHER_CTX_init(
ctx);
68 if (fOk) fOk = EVP_EncryptInit_ex(
ctx, EVP_aes_256_cbc(), NULL,
chKey,
chIV) != 0;
69 if (fOk) fOk = EVP_EncryptUpdate(
ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen) != 0;
70 if (fOk) fOk = EVP_EncryptFinal_ex(
ctx, (&vchCiphertext[0]) + nCLen, &nFLen) != 0;
71 EVP_CIPHER_CTX_cleanup(
ctx);
73 EVP_CIPHER_CTX_free(
ctx);
75 if (!fOk)
return false;
77 vchCiphertext.resize(nCLen + nFLen);
87 int nLen = vchCiphertext.size();
88 int nPLen = nLen, nFLen = 0;
92 EVP_CIPHER_CTX *
ctx = EVP_CIPHER_CTX_new();
94 if (!
ctx)
return false;
98 EVP_CIPHER_CTX_init(
ctx);
99 if (fOk) fOk = EVP_DecryptInit_ex(
ctx, EVP_aes_256_cbc(), NULL,
chKey,
chIV) != 0;
100 if (fOk) fOk = EVP_DecryptUpdate(
ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen) != 0;
101 if (fOk) fOk = EVP_DecryptFinal_ex(
ctx, (&vchPlaintext[0]) + nPLen, &nFLen) != 0;
102 EVP_CIPHER_CTX_cleanup(
ctx);
104 EVP_CIPHER_CTX_free(
ctx);
106 if (!fOk)
return false;
108 vchPlaintext.resize(nPLen + nFLen);
118 if(!cKeyCrypter.
SetKey(vMasterKey, chIV))
129 int nLen = sPlaintext.size();
130 int nCLen = nLen + AES_BLOCK_SIZE;
134 if(sKey.size() != 32 || sIV.size() != AES_BLOCK_SIZE) {
135 LogPrintf(
"crypter EncryptAES256 - Invalid key or block size: Key: %d sIV:%d\n", sKey.size(), sIV.size());
140 sCiphertext.resize(nCLen);
143 EVP_CIPHER_CTX *
ctx = EVP_CIPHER_CTX_new();
145 if (!
ctx)
return false;
149 EVP_CIPHER_CTX_init(
ctx);
150 if (fOk) fOk = EVP_EncryptInit_ex(
ctx, EVP_aes_256_cbc(), NULL, (
const unsigned char*) &sKey[0], (
const unsigned char*) &sIV[0]);
151 if (fOk) fOk = EVP_EncryptUpdate(
ctx, (
unsigned char*) &sCiphertext[0], &nCLen, (
const unsigned char*) &sPlaintext[0], nLen);
152 if (fOk) fOk = EVP_EncryptFinal_ex(
ctx, (
unsigned char*) (&sCiphertext[0])+nCLen, &nFLen);
153 EVP_CIPHER_CTX_cleanup(
ctx);
155 EVP_CIPHER_CTX_free(
ctx);
157 if (!fOk)
return false;
159 sCiphertext.resize(nCLen + nFLen);
169 if(!cKeyCrypter.
SetKey(vMasterKey, chIV))
177 int nLen = sCiphertext.size();
178 int nPLen = nLen, nFLen = 0;
181 if(sKey.size() != 32 || sIV.size() != AES_BLOCK_SIZE) {
182 LogPrintf(
"crypter DecryptAES256 - Invalid key or block size\n");
186 sPlaintext.resize(nPLen);
188 EVP_CIPHER_CTX *
ctx = EVP_CIPHER_CTX_new();
190 if (!
ctx)
return false;
194 EVP_CIPHER_CTX_init(
ctx);
195 if (fOk) fOk = EVP_DecryptInit_ex(
ctx, EVP_aes_256_cbc(), NULL, (
const unsigned char*) &sKey[0], (
const unsigned char*) &sIV[0]);
196 if (fOk) fOk = EVP_DecryptUpdate(
ctx, (
unsigned char *) &sPlaintext[0], &nPLen, (
const unsigned char *) &sCiphertext[0], nLen);
197 if (fOk) fOk = EVP_DecryptFinal_ex(
ctx, (
unsigned char *) (&sPlaintext[0])+nPLen, &nFLen);
198 EVP_CIPHER_CTX_cleanup(
ctx);
200 EVP_CIPHER_CTX_free(
ctx);
202 if (!fOk)
return false;
204 sPlaintext.resize(nPLen + nFLen);
215 if (vchSecret.size() != 32)
219 return key.VerifyPubKey(vchPubKey);
255 bool keyPass =
false;
256 bool keyFail =
false;
260 const CPubKey &vchPubKey = (*mi).second.first;
261 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
263 if (!
DecryptKey(vMasterKeyIn, vchCryptedSecret, vchPubKey,
key))
272 if (keyPass && keyFail)
274 LogPrintf(
"The wallet is probably corrupted: Some keys decrypt but not all.\n");
283 bool chainPass =
false;
312 std::vector<unsigned char> vchCryptedSecret;
346 const CPubKey &vchPubKey = (*mi).second.first;
347 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
364 vchPubKeyOut = (*mi).second.first;
381 BOOST_FOREACH(KeyMap::value_type& mKey,
mapKeys)
386 std::vector<unsigned char> vchCryptedSecret;
413 std::vector<unsigned char> vchCryptedSeed;
421 SecureVector vchSecureCryptedSeed(vchCryptedSeed.begin(), vchCryptedSeed.end());
430 std::vector<unsigned char> vchCryptedMnemonic;
431 std::vector<unsigned char> vchCryptedMnemonicPassphrase;
435 if (!vchMnemonicPassphrase.empty() && !
EncryptSecret(vMasterKeyIn, vchMnemonicPassphrase,
hdChain.
GetID(), vchCryptedMnemonicPassphrase))
438 SecureVector vchSecureCryptedMnemonic(vchCryptedMnemonic.begin(), vchCryptedMnemonic.end());
439 SecureVector vchSecureCryptedMnemonicPassphrase(vchCryptedMnemonicPassphrase.begin(), vchCryptedMnemonicPassphrase.end());
463 std::vector<unsigned char> vchCryptedSeed(vchSecureCryptedSeed.begin(), vchSecureCryptedSeed.end());
468 if (!hdChainRet.
SetSeed(vchSecureSeed,
false))
483 std::vector<unsigned char> vchCryptedMnemonic(vchSecureCryptedMnemonic.begin(), vchSecureCryptedMnemonic.end());
484 std::vector<unsigned char> vchCryptedMnemonicPassphrase(vchSecureCryptedMnemonicPassphrase.begin(), vchSecureCryptedMnemonicPassphrase.end());
491 if (!hdChainRet.
SetMnemonic(vchSecureMnemonic, vchSecureMnemonicPassphrase,
false))
496 hdChainRet.
Debug(__func__);
const unsigned int WALLET_CRYPTO_KEY_SIZE
bool DecryptHDChain(CHDChain &hdChainRet) const
bool EncryptAES256(const SecureString &sKey, const SecureString &sPlaintext, const std::string &sIV, std::string &sCiphertext)
bool Encrypt(const CKeyingMaterial &vchPlaintext, std::vector< unsigned char > &vchCiphertext)
bool fOnlyMixingAllowed
if fOnlyMixingAllowed is true, only mixing should be allowed in unlocked wallet
uint256 GetHash() const
Get the 256-bit hash of this public key.
bool GetMnemonic(SecureVector &vchMnemonicRet, SecureVector &vchMnemonicPassphraseRet) const
unsigned char chKey[WALLET_CRYPTO_KEY_SIZE]
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
Add a key to the store.
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
SecureVector GetSeed() const
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
CryptedKeyMap mapCryptedKeys
bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector< unsigned char > &chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
static bool DecryptKey(const CKeyingMaterial &vMasterKey, const std::vector< unsigned char > &vchCryptedSecret, const CPubKey &vchPubKey, CKey &key)
bool GetKey(const CKeyID &address, CKey &keyOut) const
bool DecryptAES256(const SecureString &sKey, const std::string &sCiphertext, const std::string &sIV, SecureString &sPlaintext)
bool SetHDChain(const CHDChain &chain)
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
bool GetHDChain(CHDChain &hdChainRet) const
unsigned char chIV[WALLET_CRYPTO_KEY_SIZE]
bool GetKey(const CKeyID &address, CKey &keyOut) const
void memory_cleanse(void *ptr, size_t len)
bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
CCriticalSection cs_KeyStore
static secp256k1_context * ctx
boost::signals2::signal< void(CCryptoKeyStore *wallet)> NotifyStatusChanged
std::vector< unsigned char, secure_allocator< unsigned char > > SecureVector
bool IsLocked(bool fForMixing=false) const
bool SetKey(const CKeyingMaterial &chNewKey, const std::vector< unsigned char > &chNewIV)
static bool DecryptSecret(const CKeyingMaterial &vMasterKey, const std::vector< unsigned char > &vchCiphertext, const uint256 &nIV, CKeyingMaterial &vchPlaintext)
bool Decrypt(const std::vector< unsigned char > &vchCiphertext, CKeyingMaterial &vchPlaintext)
void SetCrypted(bool fCryptedIn)
const unsigned int WALLET_CRYPTO_SALT_SIZE
bool IsCompressed() const
Check whether this is a compressed public key.
void * memcpy(void *a, const void *b, size_t c)
bool Unlock(const CKeyingMaterial &vMasterKeyIn, bool fForMixingOnly=false)
bool Lock(bool fAllowMixing=false)
bool EncryptHDChain(const CKeyingMaterial &vMasterKeyIn)
virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret)
CKeyingMaterial vMasterKey
bool fDecryptionThoroughlyChecked
keeps track of whether Unlock has run a thorough check before
bool SetMnemonic(const SecureVector &vchMnemonic, const SecureVector &vchMnemonicPassphrase, bool fUpdateID)
bool SetSeed(const SecureVector &vchSeedIn, bool fUpdateID)
bool SetCryptedHDChain(const CHDChain &chain)
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
Add a key to the store.
bool EncryptKeys(CKeyingMaterial &vMasterKeyIn)
will encrypt previously unencrypted keys
static bool EncryptSecret(const CKeyingMaterial &vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256 &nIV, std::vector< unsigned char > &vchCiphertext)
void Debug(std::string strName) const