12 #include <boost/foreach.hpp>
13 #include <openssl/aes.h>
14 #include <openssl/evp.h>
22 if (nDerivationMethod == 0)
23 i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &chSalt[0],
24 (
unsigned char *)&strKeyData[0], strKeyData.size(), nRounds,
chKey,
chIV);
28 OPENSSL_cleanse(chKey,
sizeof(chKey));
56 int nLen = vchPlaintext.size();
57 int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
58 vchCiphertext = std::vector<unsigned char> (nCLen);
64 EVP_CIPHER_CTX_init(&ctx);
65 if (fOk) fOk = EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL,
chKey,
chIV);
66 if (fOk) fOk = EVP_EncryptUpdate(&ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen);
67 if (fOk) fOk = EVP_EncryptFinal_ex(&ctx, (&vchCiphertext[0])+nCLen, &nFLen);
68 EVP_CIPHER_CTX_cleanup(&ctx);
70 if (!fOk)
return false;
72 vchCiphertext.resize(nCLen + nFLen);
82 int nLen = vchCiphertext.size();
83 int nPLen = nLen, nFLen = 0;
91 EVP_CIPHER_CTX_init(&ctx);
92 if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL,
chKey,
chIV);
93 if (fOk) fOk = EVP_DecryptUpdate(&ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen);
94 if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, (&vchPlaintext[0])+nPLen, &nFLen);
95 EVP_CIPHER_CTX_cleanup(&ctx);
97 if (!fOk)
return false;
99 vchPlaintext.resize(nPLen + nFLen);
109 if(!cKeyCrypter.
SetKey(vMasterKey, chIV))
119 if(!cKeyCrypter.
SetKey(vMasterKey, chIV))
159 const CPubKey &vchPubKey = (*mi).second.first;
160 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
164 if (vchSecret.size() != 32)
188 std::vector<unsigned char> vchCryptedSecret;
222 const CPubKey &vchPubKey = (*mi).second.first;
223 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
227 if (vchSecret.size() != 32)
229 keyOut.
Set(vchSecret.begin(), vchSecret.end(), vchPubKey.
IsCompressed());
246 vchPubKeyOut = (*mi).second.first;
261 BOOST_FOREACH(KeyMap::value_type& mKey,
mapKeys)
263 const CKey &key = mKey.second;
266 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.