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