raw
experimental-genesis    1 //  /****************************\
experimental-genesis 2 // * EXPERIMENTAL BRANCH. *
experimental-genesis 3 // * FOR LABORATORY USE ONLY. *
experimental-genesis 4 // ********************************
experimental-genesis 5 // ************
experimental-genesis 6 // **************
experimental-genesis 7 // ****************
experimental-genesis 8 // **** **** ****
experimental-genesis 9 // *** *** ***
experimental-genesis 10 // *** *** ***
experimental-genesis 11 // *** * * **
experimental-genesis 12 // ******** ********
experimental-genesis 13 // ******* ******
experimental-genesis 14 // *** **
experimental-genesis 15 // * ******* **
experimental-genesis 16 // ** * * * * *
experimental-genesis 17 // ** * * ***
experimental-genesis 18 // **** * * * * ****
experimental-genesis 19 // **** *** * * ** ***
experimental-genesis 20 // **** ********* ******
experimental-genesis 21 // ******* ***** *******
experimental-genesis 22 // ********* ****** **
experimental-genesis 23 // ** ****** ******
experimental-genesis 24 // ** ******* **
experimental-genesis 25 // ** ******* ***
experimental-genesis 26 // **** ******** ************
experimental-genesis 27 // ************ ************
experimental-genesis 28 // ******** *******
experimental-genesis 29 // ****** ****
experimental-genesis 30 // *** ***
experimental-genesis 31 // ********************************
experimental-genesis 32 // Copyright (c) 2009-2010 Satoshi Nakamoto
experimental-genesis 33 // Copyright (c) 2011 The Bitcoin developers
experimental-genesis 34 // Distributed under the MIT/X11 software license, see the accompanying
experimental-genesis 35 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
experimental-genesis 36
experimental-genesis 37 #include "headers.h"
experimental-genesis 38 #include "db.h"
experimental-genesis 39 #include "crypter.h"
experimental-genesis 40
experimental-genesis 41 std::vector<unsigned char> CKeyStore::GenerateNewKey()
experimental-genesis 42 {
experimental-genesis 43 RandAddSeedPerfmon();
experimental-genesis 44 CKey key;
experimental-genesis 45 key.MakeNewKey();
experimental-genesis 46 if (!AddKey(key))
experimental-genesis 47 throw std::runtime_error("CKeyStore::GenerateNewKey() : AddKey failed");
experimental-genesis 48 return key.GetPubKey();
experimental-genesis 49 }
experimental-genesis 50
experimental-genesis 51 bool CKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char> &vchPubKeyOut) const
experimental-genesis 52 {
experimental-genesis 53 CKey key;
experimental-genesis 54 if (!GetKey(address, key))
experimental-genesis 55 return false;
experimental-genesis 56 vchPubKeyOut = key.GetPubKey();
experimental-genesis 57 return true;
experimental-genesis 58 }
experimental-genesis 59
experimental-genesis 60 bool CBasicKeyStore::AddKey(const CKey& key)
experimental-genesis 61 {
experimental-genesis 62 CRITICAL_BLOCK(cs_KeyStore)
experimental-genesis 63 mapKeys[key.GetAddress()] = key.GetSecret();
experimental-genesis 64 return true;
experimental-genesis 65 }
experimental-genesis 66
experimental-genesis 67 bool CCryptoKeyStore::SetCrypted()
experimental-genesis 68 {
experimental-genesis 69 CRITICAL_BLOCK(cs_KeyStore)
experimental-genesis 70 {
experimental-genesis 71 if (fUseCrypto)
experimental-genesis 72 return true;
experimental-genesis 73 if (!mapKeys.empty())
experimental-genesis 74 return false;
experimental-genesis 75 fUseCrypto = true;
experimental-genesis 76 }
experimental-genesis 77 return true;
experimental-genesis 78 }
experimental-genesis 79
experimental-genesis 80 std::vector<unsigned char> CCryptoKeyStore::GenerateNewKey()
experimental-genesis 81 {
experimental-genesis 82 RandAddSeedPerfmon();
experimental-genesis 83 CKey key;
experimental-genesis 84 key.MakeNewKey();
experimental-genesis 85 if (!AddKey(key))
experimental-genesis 86 throw std::runtime_error("CCryptoKeyStore::GenerateNewKey() : AddKey failed");
experimental-genesis 87 return key.GetPubKey();
experimental-genesis 88 }
experimental-genesis 89
experimental-genesis 90 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
experimental-genesis 91 {
experimental-genesis 92 CRITICAL_BLOCK(cs_KeyStore)
experimental-genesis 93 {
experimental-genesis 94 if (!SetCrypted())
experimental-genesis 95 return false;
experimental-genesis 96
experimental-genesis 97 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
experimental-genesis 98 for (; mi != mapCryptedKeys.end(); ++mi)
experimental-genesis 99 {
experimental-genesis 100 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
experimental-genesis 101 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
experimental-genesis 102 CSecret vchSecret;
experimental-genesis 103 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
experimental-genesis 104 return false;
experimental-genesis 105 CKey key;
experimental-genesis 106 key.SetSecret(vchSecret);
experimental-genesis 107 if (key.GetPubKey() == vchPubKey)
experimental-genesis 108 break;
experimental-genesis 109 return false;
experimental-genesis 110 }
experimental-genesis 111 vMasterKey = vMasterKeyIn;
experimental-genesis 112 }
experimental-genesis 113 return true;
experimental-genesis 114 }
experimental-genesis 115
experimental-genesis 116 bool CCryptoKeyStore::AddKey(const CKey& key)
experimental-genesis 117 {
experimental-genesis 118 CRITICAL_BLOCK(cs_KeyStore)
experimental-genesis 119 {
experimental-genesis 120 if (!IsCrypted())
experimental-genesis 121 return CBasicKeyStore::AddKey(key);
experimental-genesis 122
experimental-genesis 123 if (IsLocked())
experimental-genesis 124 return false;
experimental-genesis 125
experimental-genesis 126 std::vector<unsigned char> vchCryptedSecret;
experimental-genesis 127 std::vector<unsigned char> vchPubKey = key.GetPubKey();
experimental-genesis 128 if (!EncryptSecret(vMasterKey, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
experimental-genesis 129 return false;
experimental-genesis 130
experimental-genesis 131 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
experimental-genesis 132 return false;
experimental-genesis 133 }
experimental-genesis 134 return true;
experimental-genesis 135 }
experimental-genesis 136
experimental-genesis 137
experimental-genesis 138 bool CCryptoKeyStore::AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
experimental-genesis 139 {
experimental-genesis 140 CRITICAL_BLOCK(cs_KeyStore)
experimental-genesis 141 {
experimental-genesis 142 if (!SetCrypted())
experimental-genesis 143 return false;
experimental-genesis 144
experimental-genesis 145 mapCryptedKeys[CBitcoinAddress(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret);
experimental-genesis 146 }
experimental-genesis 147 return true;
experimental-genesis 148 }
experimental-genesis 149
experimental-genesis 150 bool CCryptoKeyStore::GetKey(const CBitcoinAddress &address, CKey& keyOut) const
experimental-genesis 151 {
experimental-genesis 152 CRITICAL_BLOCK(cs_KeyStore)
experimental-genesis 153 {
experimental-genesis 154 if (!IsCrypted())
experimental-genesis 155 return CBasicKeyStore::GetKey(address, keyOut);
experimental-genesis 156
experimental-genesis 157 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
experimental-genesis 158 if (mi != mapCryptedKeys.end())
experimental-genesis 159 {
experimental-genesis 160 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
experimental-genesis 161 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
experimental-genesis 162 CSecret vchSecret;
experimental-genesis 163 if (!DecryptSecret(vMasterKey, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
experimental-genesis 164 return false;
experimental-genesis 165 keyOut.SetSecret(vchSecret);
experimental-genesis 166 return true;
experimental-genesis 167 }
experimental-genesis 168 }
experimental-genesis 169 return false;
experimental-genesis 170 }
experimental-genesis 171
experimental-genesis 172 bool CCryptoKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const
experimental-genesis 173 {
experimental-genesis 174 CRITICAL_BLOCK(cs_KeyStore)
experimental-genesis 175 {
experimental-genesis 176 if (!IsCrypted())
experimental-genesis 177 return CKeyStore::GetPubKey(address, vchPubKeyOut);
experimental-genesis 178
experimental-genesis 179 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
experimental-genesis 180 if (mi != mapCryptedKeys.end())
experimental-genesis 181 {
experimental-genesis 182 vchPubKeyOut = (*mi).second.first;
experimental-genesis 183 return true;
experimental-genesis 184 }
experimental-genesis 185 }
experimental-genesis 186 return false;
experimental-genesis 187 }
experimental-genesis 188
experimental-genesis 189 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
experimental-genesis 190 {
experimental-genesis 191 CRITICAL_BLOCK(cs_KeyStore)
experimental-genesis 192 {
experimental-genesis 193 if (!mapCryptedKeys.empty() || IsCrypted())
experimental-genesis 194 return false;
experimental-genesis 195
experimental-genesis 196 fUseCrypto = true;
experimental-genesis 197 CKey key;
experimental-genesis 198 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
experimental-genesis 199 {
experimental-genesis 200 if (!key.SetSecret(mKey.second))
experimental-genesis 201 return false;
experimental-genesis 202 const std::vector<unsigned char> vchPubKey = key.GetPubKey();
experimental-genesis 203 std::vector<unsigned char> vchCryptedSecret;
experimental-genesis 204 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
experimental-genesis 205 return false;
experimental-genesis 206 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))
experimental-genesis 207 return false;
experimental-genesis 208 }
experimental-genesis 209 mapKeys.clear();
experimental-genesis 210 }
experimental-genesis 211 return true;
experimental-genesis 212 }