11 #include <boost/foreach.hpp>
12 #include <openssl/aes.h>
13 #include <openssl/evp.h>
21 if (nDerivationMethod == 0)
22 i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &chSalt[0],
23 (
unsigned char *)&strKeyData[0], strKeyData.size(), nRounds,
chKey,
chIV);
27 OPENSSL_cleanse(chKey,
sizeof(chKey));
55 int nLen = vchPlaintext.size();
56 int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
57 vchCiphertext = std::vector<unsigned char> (nCLen);
63 EVP_CIPHER_CTX_init(&ctx);
64 if (fOk) fOk = EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL,
chKey,
chIV);
65 if (fOk) fOk = EVP_EncryptUpdate(&ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen);
66 if (fOk) fOk = EVP_EncryptFinal_ex(&ctx, (&vchCiphertext[0])+nCLen, &nFLen);
67 EVP_CIPHER_CTX_cleanup(&ctx);
69 if (!fOk)
return false;
71 vchCiphertext.resize(nCLen + nFLen);
81 int nLen = vchCiphertext.size();
82 int nPLen = nLen, nFLen = 0;
90 EVP_CIPHER_CTX_init(&ctx);
91 if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL,
chKey,
chIV);
92 if (fOk) fOk = EVP_DecryptUpdate(&ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen);
93 if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, (&vchPlaintext[0])+nPLen, &nFLen);
94 EVP_CIPHER_CTX_cleanup(&ctx);
96 if (!fOk)
return false;
98 vchPlaintext.resize(nPLen + nFLen);
108 if(!cKeyCrypter.
SetKey(vMasterKey, chIV))
118 if(!cKeyCrypter.
SetKey(vMasterKey, chIV))
158 const CPubKey &vchPubKey = (*mi).second.first;
159 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
163 if (vchSecret.size() != 32)
187 std::vector<unsigned char> vchCryptedSecret;
221 const CPubKey &vchPubKey = (*mi).second.first;
222 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
226 if (vchSecret.size() != 32)
228 keyOut.
Set(vchSecret.begin(), vchSecret.end(), vchPubKey.
IsCompressed());
245 vchPubKeyOut = (*mi).second.first;
260 BOOST_FOREACH(KeyMap::value_type& mKey,
mapKeys)
262 const CKey &key = mKey.second;
265 std::vector<unsigned char> vchCryptedSecret;
bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector< unsigned char > &chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
CCriticalSection cs_KeyStore
const unsigned char * begin() const
bool Encrypt(const CKeyingMaterial &vchPlaintext, std::vector< unsigned char > &vchCiphertext)
const unsigned int WALLET_CRYPTO_KEY_SIZE
unsigned char chIV[WALLET_CRYPTO_KEY_SIZE]
bool SetKey(const CKeyingMaterial &chNewKey, const std::vector< unsigned char > &chNewIV)
Encryption/decryption context with key information.
const unsigned char * end() const
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
bool EncryptKeys(CKeyingMaterial &vMasterKeyIn)
CKeyingMaterial vMasterKey
virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret)
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
bool GetKey(const CKeyID &address, CKey &keyOut) const
bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
boost::signals2::signal< void(CCryptoKeyStore *wallet)> NotifyStatusChanged
bool GetKey(const CKeyID &address, CKey &keyOut) const
virtual bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
CPubKey GetPubKey() const
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
An encapsulated public key.
bool Unlock(const CKeyingMaterial &vMasterKeyIn)
bool Decrypt(const std::vector< unsigned char > &vchCiphertext, CKeyingMaterial &vchPlaintext)
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
void Set(const T pbegin, const T pend, bool fCompressedIn)
unsigned char chKey[WALLET_CRYPTO_KEY_SIZE]
bool IsCompressed() const
const unsigned int WALLET_CRYPTO_SALT_SIZE
CryptedKeyMap mapCryptedKeys
void * memcpy(void *a, const void *b, size_t c)
bool DecryptSecret(const CKeyingMaterial &vMasterKey, const std::vector< unsigned char > &vchCiphertext, const uint256 &nIV, CKeyingMaterial &vchPlaintext)
A reference to a CKey: the Hash160 of its serialized public key.
bool EncryptSecret(const CKeyingMaterial &vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256 &nIV, std::vector< unsigned char > &vchCiphertext)
An encapsulated private key.