X-Git-Url: https://git.novaco.in/?p=novacoin.git;a=blobdiff_plain;f=src%2Fkeystore.cpp;h=f659495a1a8093b0a4c7d245d6d00717749012f0;hp=765144a9b76526588227104501cdb3900def0750;hb=4e87d341f75f13bbd7d108c31c03886fbc4df56f;hpb=a48c671957e37594d8f9e0fd51b24e7a4f44300e diff --git a/src/keystore.cpp b/src/keystore.cpp index 765144a..f659495 100644 --- a/src/keystore.cpp +++ b/src/keystore.cpp @@ -4,6 +4,7 @@ #include "headers.h" #include "db.h" +#include "crypter.h" std::vector CKeyStore::GenerateNewKey() { @@ -17,6 +18,7 @@ std::vector CKeyStore::GenerateNewKey() bool CBasicKeyStore::AddKey(const CKey& key) { + CRITICAL_BLOCK(cs_mapPubKeys) CRITICAL_BLOCK(cs_KeyStore) { mapKeys[key.GetPubKey()] = key.GetPrivKey(); @@ -25,31 +27,46 @@ bool CBasicKeyStore::AddKey(const CKey& key) return true; } -bool CCryptoKeyStore::Unlock(const CMasterKey& vMasterKeyIn) +std::vector CCryptoKeyStore::GenerateNewKey() { - if (!SetCrypted()) - return false; + RandAddSeedPerfmon(); + CKey key; + key.MakeNewKey(); + if (!AddKey(key)) + throw std::runtime_error("CCryptoKeyStore::GenerateNewKey() : AddKey failed"); + return key.GetPubKey(); +} - std::map, std::vector >::const_iterator mi = mapCryptedKeys.begin(); - for (; mi != mapCryptedKeys.end(); ++mi) +bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn) +{ + CRITICAL_BLOCK(cs_vMasterKey) { - const std::vector &vchPubKey = (*mi).first; - const std::vector &vchCryptedSecret = (*mi).second; - CSecret vchSecret; - // decrypt vchCryptedSecret using vMasterKeyIn, into vchSecret - CKey key; - key.SetSecret(vchSecret); - if (key.GetPubKey() == vchPubKey) - break; - return false; + if (!SetCrypted()) + return false; + + std::map, std::vector >::const_iterator mi = mapCryptedKeys.begin(); + for (; mi != mapCryptedKeys.end(); ++mi) + { + const std::vector &vchPubKey = (*mi).first; + const std::vector &vchCryptedSecret = (*mi).second; + CSecret vchSecret; + if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret)) + return false; + CKey key; + key.SetSecret(vchSecret); + if (key.GetPubKey() == vchPubKey) + break; + return false; + } + vMasterKey = vMasterKeyIn; } - vMasterKey = vMasterKeyIn; return true; } bool CCryptoKeyStore::AddKey(const CKey& key) { CRITICAL_BLOCK(cs_KeyStore) + CRITICAL_BLOCK(cs_vMasterKey) { if (!IsCrypted()) return CBasicKeyStore::AddKey(key); @@ -57,12 +74,13 @@ bool CCryptoKeyStore::AddKey(const CKey& key) if (IsLocked()) return false; - CSecret vchSecret = key.GetSecret(); - std::vector vchCryptedSecret; - // encrypt vchSecret using vMasterKey, into vchCryptedSecret + std::vector vchPubKey = key.GetPubKey(); + if (!EncryptSecret(vMasterKey, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret)) + return false; - AddCryptedKey(key.GetPubKey(), vchCryptedSecret); + if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret)) + return false; } return true; } @@ -70,6 +88,7 @@ bool CCryptoKeyStore::AddKey(const CKey& key) bool CCryptoKeyStore::AddCryptedKey(const std::vector &vchPubKey, const std::vector &vchCryptedSecret) { + CRITICAL_BLOCK(cs_mapPubKeys) CRITICAL_BLOCK(cs_KeyStore) { if (!SetCrypted()) @@ -83,38 +102,48 @@ bool CCryptoKeyStore::AddCryptedKey(const std::vector &vchPubKey, bool CCryptoKeyStore::GetPrivKey(const std::vector &vchPubKey, CPrivKey& keyOut) const { - if (!IsCrypted()) - return CBasicKeyStore::GetPrivKey(vchPubKey, keyOut); - - std::map, std::vector >::const_iterator mi = mapCryptedKeys.find(vchPubKey); - if (mi != mapCryptedKeys.end()) + CRITICAL_BLOCK(cs_vMasterKey) { - const std::vector &vchCryptedSecret = (*mi).second; - CSecret vchSecret; - // decrypt vchCryptedSecret using vMasterKey into vchSecret; - CKey key; - key.SetSecret(vchSecret); - keyOut = key.GetPrivKey(); - return true; + if (!IsCrypted()) + return CBasicKeyStore::GetPrivKey(vchPubKey, keyOut); + + std::map, std::vector >::const_iterator mi = mapCryptedKeys.find(vchPubKey); + if (mi != mapCryptedKeys.end()) + { + const std::vector &vchCryptedSecret = (*mi).second; + CSecret vchSecret; + if (!DecryptSecret(vMasterKey, (*mi).second, Hash((*mi).first.begin(), (*mi).first.end()), vchSecret)) + return false; + CKey key; + key.SetSecret(vchSecret); + keyOut = key.GetPrivKey(); + return true; + } } return false; } -bool CCryptoKeyStore::GenerateMasterKey() +bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn) { - if (!mapCryptedKeys.empty()) - return false; - - RandAddSeedPerfmon(); - - vMasterKey.resize(32); - RAND_bytes(&vMasterKey[0], 32); - - if (!IsCrypted()) + CRITICAL_BLOCK(cs_KeyStore) + CRITICAL_BLOCK(cs_vMasterKey) { - // upgrade wallet + if (!mapCryptedKeys.empty() || IsCrypted()) + return false; + fUseCrypto = true; + CKey key; + BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys) + { + if (!key.SetPrivKey(mKey.second)) + return false; + std::vector vchCryptedSecret; + if (!EncryptSecret(vMasterKeyIn, key.GetSecret(), Hash(mKey.first.begin(), mKey.first.end()), vchCryptedSecret)) + return false; + if (!AddCryptedKey(mKey.first, vchCryptedSecret)) + return false; + } + mapKeys.clear(); } - return true; }