23 if(!count || !key || !iv)
29 di.
Write((
const unsigned char*)strKeyData.c_str(), strKeyData.size());
30 di.
Write(chSalt.data(), chSalt.size());
33 for(
int i = 0; i != count - 1; i++)
48 if (nDerivationMethod == 0)
68 memcpy(
vchIV.data(), chNewIV.data(), chNewIV.size());
81 vchCiphertext.resize(vchPlaintext.size() + AES_BLOCKSIZE);
84 size_t nLen = enc.
Encrypt(&vchPlaintext[0], vchPlaintext.size(), vchCiphertext.data());
85 if(nLen < vchPlaintext.size())
87 vchCiphertext.resize(nLen);
98 int nLen = vchCiphertext.size();
100 vchPlaintext.resize(nLen);
103 nLen = dec.
Decrypt(vchCiphertext.data(), vchCiphertext.size(), &vchPlaintext[0]);
106 vchPlaintext.resize(nLen);
116 if(!cKeyCrypter.
SetKey(vMasterKey, chIV))
126 if(!cKeyCrypter.
SetKey(vMasterKey, chIV))
131 static bool DecryptKey(
const CKeyingMaterial& vMasterKey,
const std::vector<unsigned char>& vchCryptedSecret,
const CPubKey& vchPubKey,
CKey& key)
134 if(!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.
GetHash(), vchSecret))
137 if (vchSecret.size() != 32)
149 if (!mapKeys.empty())
161 return vMasterKey.empty();
185 bool keyPass =
false;
186 bool keyFail =
false;
187 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
188 for (; mi != mapCryptedKeys.end(); ++mi)
190 const CPubKey &vchPubKey = (*mi).second.first;
191 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
193 if (!DecryptKey(vMasterKeyIn, vchCryptedSecret, vchPubKey, key))
202 if (keyPass && keyFail)
204 LogPrintf(
"The wallet is probably corrupted: Some keys decrypt but not all.\n");
207 if (keyFail || !keyPass)
209 vMasterKey = vMasterKeyIn;
227 std::vector<unsigned char> vchCryptedSecret;
229 if (!EncryptSecret(vMasterKey, vchSecret, pubkey.
GetHash(), vchCryptedSecret)) {
247 mapCryptedKeys[vchPubKey.
GetID()] = make_pair(vchPubKey, vchCryptedSecret);
258 return mapCryptedKeys.count(address) > 0;
268 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
269 if (mi != mapCryptedKeys.end())
271 const CPubKey &vchPubKey = (*mi).second.first;
272 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
273 return DecryptKey(vMasterKey, vchCryptedSecret, vchPubKey, keyOut);
284 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
285 if (mi != mapCryptedKeys.end())
287 vchPubKeyOut = (*mi).second.first;
300 std::set<CKeyID> set_address;
301 for (
const auto& mi : mapCryptedKeys) {
302 set_address.insert(mi.first);
310 if (!mapCryptedKeys.empty() ||
IsCrypted())
314 for (
const KeyMap::value_type& mKey : mapKeys)
316 const CKey &key = mKey.second;
319 std::vector<unsigned char> vchCryptedSecret;
320 if (!EncryptSecret(vMasterKeyIn, vchSecret, vchPubKey.
GetHash(), vchCryptedSecret))
bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector< unsigned char > &chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
CSHA3512 & Write(const unsigned char *data, size_t len)
bool HaveKey(const CKeyID &address) const override
Check whether a key corresponding to a given address is present in the store.
const unsigned int WALLET_CRYPTO_KEY_SIZE
bool Encrypt(const CKeyingMaterial &vchPlaintext, std::vector< unsigned char > &vchCiphertext) const
static const size_t OUTPUT_SIZE
bool SetKey(const CKeyingMaterial &chNewKey, const std::vector< unsigned char > &chNewIV)
Encryption/decryption context with key information.
bool VerifyPubKey(const CPubKey &vchPubKey) const
Verify thoroughly whether a private key and a public key match.
CPubKey GetPubKey() const
Compute the public key from a private key.
bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override
CCriticalSection cs_KeyStore
void ImplicitlyLearnRelatedKeyScripts(const CPubKey &pubkey) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
std::set< CKeyID > GetKeys() const override
uint256 GetHash() const
Get the 256-bit hash of this public key.
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
bool EncryptKeys(CKeyingMaterial &vMasterKeyIn)
will encrypt previously unencrypted keys
int BytesToKeySHA512AES(const std::vector< unsigned char > &chSalt, const SecureString &strKeyData, int count, unsigned char *key, unsigned char *iv) const
int Decrypt(const unsigned char *data, int size, unsigned char *out) const
std::atomic< bool > fUseCrypto
if fUseCrypto is true, mapKeys must be empty if fUseCrypto is false, vMasterKey must be empty ...
bool Decrypt(const std::vector< unsigned char > &vchCiphertext, CKeyingMaterial &vchPlaintext) const
const unsigned int WALLET_CRYPTO_IV_SIZE
virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret)
const unsigned char * begin() const
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) override
Add a key to the store.
boost::signals2::signal< void(CCryptoKeyStore *wallet)> NotifyStatusChanged
Wallet status (encrypted, locked) changed.
void memory_cleanse(void *ptr, size_t len)
void Finalize(unsigned char hash[OUTPUT_SIZE])
std::vector< unsigned char, secure_allocator< unsigned char > > vchKey
std::vector< unsigned char, secure_allocator< unsigned char > > vchIV
An encapsulated public key.
bool Unlock(const CKeyingMaterial &vMasterKeyIn)
int Encrypt(const unsigned char *data, int size, unsigned char *out) const
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) override
Add a key to the store.
A hasher class for SHA3-512.
bool GetKey(const CKeyID &address, CKey &keyOut) const override
bool GetKey(const CKeyID &address, CKey &keyOut) const override
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
const unsigned int WALLET_CRYPTO_SALT_SIZE
void * memcpy(void *a, const void *b, size_t c)
const unsigned char * end() const
A reference to a CKey: the Hash360 of its serialized public key.
An encapsulated private key.
std::set< CKeyID > GetKeys() const override
bool HaveKey(const CKeyID &address) const override
Check whether a key corresponding to a given address is present in the store.
bool fDecryptionThoroughlyChecked
keeps track of whether Unlock has run a thorough check before
bool IsCompressed() const
Check whether this is a compressed public key.