23 if(!count || !key || !iv)
29 di.
Write((
const unsigned char*)strKeyData.c_str(), strKeyData.size());
31 di.
Write(&chSalt[0], chSalt.size());
34 for(
int i = 0; i != count - 1; i++)
49 if (nDerivationMethod == 0)
69 memcpy(
vchIV.data(), chNewIV.data(), chNewIV.size());
82 vchCiphertext.resize(vchPlaintext.size() + AES_BLOCKSIZE);
85 size_t nLen = enc.
Encrypt(&vchPlaintext[0], vchPlaintext.size(), &vchCiphertext[0]);
86 if(nLen < vchPlaintext.size())
88 vchCiphertext.resize(nLen);
99 int nLen = vchCiphertext.size();
101 vchPlaintext.resize(nLen);
104 nLen = dec.
Decrypt(&vchCiphertext[0], vchCiphertext.size(), &vchPlaintext[0]);
107 vchPlaintext.resize(nLen);
117 if(!cKeyCrypter.
SetKey(vMasterKey, chIV))
127 if(!cKeyCrypter.
SetKey(vMasterKey, chIV))
132 static bool DecryptKey(
const CKeyingMaterial& vMasterKey,
const std::vector<unsigned char>& vchCryptedSecret,
const CPubKey& vchPubKey,
CKey& key)
135 if(!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.
GetHash(), vchSecret))
138 if (vchSecret.size() != 32)
150 if (!mapKeys.empty())
166 NotifyStatusChanged(
this);
177 bool keyPass =
false;
178 bool keyFail =
false;
179 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
180 for (; mi != mapCryptedKeys.end(); ++mi)
182 const CPubKey &vchPubKey = (*mi).second.first;
183 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
185 if (!DecryptKey(vMasterKeyIn, vchCryptedSecret, vchPubKey, key))
191 if (fDecryptionThoroughlyChecked)
194 if (keyPass && keyFail)
196 LogPrintf(
"The wallet is probably corrupted: Some keys decrypt but not all.\n");
199 if (keyFail || !keyPass)
201 vMasterKey = vMasterKeyIn;
202 fDecryptionThoroughlyChecked =
true;
204 NotifyStatusChanged(
this);
218 std::vector<unsigned char> vchCryptedSecret;
220 if (!EncryptSecret(vMasterKey, vchSecret, pubkey.
GetHash(), vchCryptedSecret))
223 if (!AddCryptedKey(pubkey, vchCryptedSecret))
237 mapCryptedKeys[vchPubKey.
GetID()] = make_pair(vchPubKey, vchCryptedSecret);
249 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
250 if (mi != mapCryptedKeys.end())
252 const CPubKey &vchPubKey = (*mi).second.first;
253 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
254 return DecryptKey(vMasterKey, vchCryptedSecret, vchPubKey, keyOut);
267 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
268 if (mi != mapCryptedKeys.end())
270 vchPubKeyOut = (*mi).second.first;
282 if (!mapCryptedKeys.empty() || IsCrypted())
286 for (KeyMap::value_type& mKey : mapKeys)
288 const CKey &key = mKey.second;
291 std::vector<unsigned char> vchCryptedSecret;
292 if (!EncryptSecret(vMasterKeyIn, vchSecret, vchPubKey.
GetHash(), vchCryptedSecret))
294 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))
bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector< unsigned char > &chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
const unsigned char * begin() const
const unsigned int WALLET_CRYPTO_KEY_SIZE
bool VerifyPubKey(const CPubKey &vchPubKey) const
Verify thoroughly whether a private key and a public key match.
bool SetKey(const CKeyingMaterial &chNewKey, const std::vector< unsigned char > &chNewIV)
Encryption/decryption context with key information.
int Encrypt(const unsigned char *data, int size, unsigned char *out) const
bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override
const unsigned char * end() const
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
bool Decrypt(const std::vector< unsigned char > &vchCiphertext, CKeyingMaterial &vchPlaintext) const
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
bool EncryptKeys(CKeyingMaterial &vMasterKeyIn)
will encrypt previously unencrypted keys
assert(len-trim+(2 *lenIndices)<=WIDTH)
const unsigned int WALLET_CRYPTO_IV_SIZE
virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret)
bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override
void Finalize(unsigned char hash[OUTPUT_SIZE])
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) override
Add a key to the store.
void memory_cleanse(void *ptr, size_t len)
static const size_t OUTPUT_SIZE
std::vector< unsigned char, secure_allocator< unsigned char > > vchKey
std::vector< unsigned char, secure_allocator< unsigned char > > vchIV
CPubKey GetPubKey() const
Compute the public key from a private key.
An encapsulated public key.
bool Unlock(const CKeyingMaterial &vMasterKeyIn)
int Decrypt(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.
uint256 GetHash() const
Get the 256-bit hash of this public key.
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.
int BytesToKeySHA512AES(const std::vector< unsigned char > &chSalt, const SecureString &strKeyData, int count, unsigned char *key, unsigned char *iv) const
bool IsCompressed() const
Check whether this is a compressed public key.
const unsigned int WALLET_CRYPTO_SALT_SIZE
bool Encrypt(const CKeyingMaterial &vchPlaintext, std::vector< unsigned char > &vchCiphertext) const
void * memcpy(void *a, const void *b, size_t c)
CSHA512 & Write(const unsigned char *data, size_t len)
A reference to a CKey: the Hash160 of its serialized public key.
struct evm_uint160be address(struct evm_env *env)
An encapsulated private key.
A hasher class for SHA-512.
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)