@@ -102,7 +102,7 @@ bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingM
102
102
}
103
103
104
104
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)
106
106
{
107
107
CCrypter cKeyCrypter;
108
108
std::vector<unsigned char > chIV (WALLET_CRYPTO_KEY_SIZE);
@@ -112,7 +112,7 @@ bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vch
112
112
return cKeyCrypter.Encrypt (*((const CKeyingMaterial*)&vchPlaintext), vchCiphertext);
113
113
}
114
114
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)
116
116
{
117
117
CCrypter cKeyCrypter;
118
118
std::vector<unsigned char > chIV (WALLET_CRYPTO_KEY_SIZE);
@@ -122,6 +122,19 @@ bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned
122
122
return cKeyCrypter.Decrypt (vchCiphertext, *((CKeyingMaterial*)&vchPlaintext));
123
123
}
124
124
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
+
125
138
bool CCryptoKeyStore::SetCrypted ()
126
139
{
127
140
LOCK (cs_KeyStore);
@@ -161,20 +174,8 @@ bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
161
174
{
162
175
const CPubKey &vchPubKey = (*mi).second .first ;
163
176
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
- }
175
177
CKey key;
176
- key.Set (vchSecret.begin (), vchSecret.end (), vchPubKey.IsCompressed ());
177
- if (key.GetPubKey () != vchPubKey)
178
+ if (!DecryptKey (vMasterKeyIn, vchCryptedSecret, vchPubKey, key))
178
179
{
179
180
keyFail = true ;
180
181
break ;
@@ -243,13 +244,7 @@ bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
243
244
{
244
245
const CPubKey &vchPubKey = (*mi).second .first ;
245
246
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);
253
248
}
254
249
}
255
250
return false ;
0 commit comments