Skip to content

Commit 93b7544

Browse files
committed
Merge pull request #5319
35f7227 Clean up wallet encryption code. (Daniel Kraft)
2 parents 31dedb4 + 35f7227 commit 93b7544

File tree

2 files changed

+17
-25
lines changed

2 files changed

+17
-25
lines changed

src/crypter.cpp

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingM
102102
}
103103

104104

105-
bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext)
105+
static bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext)
106106
{
107107
CCrypter cKeyCrypter;
108108
std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
@@ -112,7 +112,7 @@ bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vch
112112
return cKeyCrypter.Encrypt(*((const CKeyingMaterial*)&vchPlaintext), vchCiphertext);
113113
}
114114

115-
bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext)
115+
static bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext)
116116
{
117117
CCrypter cKeyCrypter;
118118
std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
@@ -122,6 +122,19 @@ bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned
122122
return cKeyCrypter.Decrypt(vchCiphertext, *((CKeyingMaterial*)&vchPlaintext));
123123
}
124124

125+
static bool DecryptKey(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCryptedSecret, const CPubKey& vchPubKey, CKey& key)
126+
{
127+
CKeyingMaterial vchSecret;
128+
if(!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
129+
return false;
130+
131+
if (vchSecret.size() != 32)
132+
return false;
133+
134+
key.Set(vchSecret.begin(), vchSecret.end(), vchPubKey.IsCompressed());
135+
return key.VerifyPubKey(vchPubKey);
136+
}
137+
125138
bool CCryptoKeyStore::SetCrypted()
126139
{
127140
LOCK(cs_KeyStore);
@@ -161,20 +174,8 @@ bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
161174
{
162175
const CPubKey &vchPubKey = (*mi).second.first;
163176
const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
164-
CKeyingMaterial vchSecret;
165-
if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
166-
{
167-
keyFail = true;
168-
break;
169-
}
170-
if (vchSecret.size() != 32)
171-
{
172-
keyFail = true;
173-
break;
174-
}
175177
CKey key;
176-
key.Set(vchSecret.begin(), vchSecret.end(), vchPubKey.IsCompressed());
177-
if (key.GetPubKey() != vchPubKey)
178+
if (!DecryptKey(vMasterKeyIn, vchCryptedSecret, vchPubKey, key))
178179
{
179180
keyFail = true;
180181
break;
@@ -243,13 +244,7 @@ bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
243244
{
244245
const CPubKey &vchPubKey = (*mi).second.first;
245246
const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
246-
CKeyingMaterial vchSecret;
247-
if (!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
248-
return false;
249-
if (vchSecret.size() != 32)
250-
return false;
251-
keyOut.Set(vchSecret.begin(), vchSecret.end(), vchPubKey.IsCompressed());
252-
return true;
247+
return DecryptKey(vMasterKey, vchCryptedSecret, vchPubKey, keyOut);
253248
}
254249
}
255250
return false;

src/crypter.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,6 @@ class CCrypter
107107
}
108108
};
109109

110-
bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext);
111-
bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext);
112-
113110
/** Keystore which keeps the private keys encrypted.
114111
* It derives from the basic key store, which is used if no encryption is active.
115112
*/

0 commit comments

Comments
 (0)