Dash Core  0.12.2.1
P2P Digital Currency
crypter.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2015 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_WALLET_CRYPTER_H
6 #define BITCOIN_WALLET_CRYPTER_H
7 
8 #include "keystore.h"
9 #include "serialize.h"
11 
12 class uint256;
13 
14 const unsigned int WALLET_CRYPTO_KEY_SIZE = 32;
15 const unsigned int WALLET_CRYPTO_SALT_SIZE = 8;
16 
34 {
35 public:
36  std::vector<unsigned char> vchCryptedKey;
37  std::vector<unsigned char> vchSalt;
40  unsigned int nDerivationMethod;
41  unsigned int nDeriveIterations;
44  std::vector<unsigned char> vchOtherDerivationParameters;
45 
47 
48  template <typename Stream, typename Operation>
49  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
55  }
56 
58  {
59  // 25000 rounds is just under 0.1 seconds on a 1.86 GHz Pentium M
60  // ie slightly lower than the lowest hardware we need bother supporting
61  nDeriveIterations = 25000;
63  vchOtherDerivationParameters = std::vector<unsigned char>(0);
64  }
65 };
66 
67 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
68 
70 class CCrypter
71 {
72 private:
73  unsigned char chKey[WALLET_CRYPTO_KEY_SIZE];
74  unsigned char chIV[WALLET_CRYPTO_KEY_SIZE];
75  bool fKeySet;
76 
77 public:
78  bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod);
79  bool Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext);
80  bool Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext);
81  bool SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV);
82 
83  void CleanKey()
84  {
85  memory_cleanse(chKey, sizeof(chKey));
86  memory_cleanse(chIV, sizeof(chIV));
87  fKeySet = false;
88  }
89 
91  {
92  fKeySet = false;
93 
94  // Try to keep the key data out of swap (and be a bit over-careful to keep the IV that we don't even use out of swap)
95  // Note that this does nothing about suspend-to-disk (which will put all our key data on disk)
96  // Note as well that at no point in this program is any attempt made to prevent stealing of keys by reading the memory of the running process.
99  }
100 
102  {
103  CleanKey();
104 
107  }
108 };
109 
110 bool EncryptAES256(const SecureString& sKey, const SecureString& sPlaintext, const std::string& sIV, std::string& sCiphertext);
111 bool DecryptAES256(const SecureString& sKey, const std::string& sCiphertext, const std::string& sIV, SecureString& sPlaintext);
112 
113 
118 {
119 private:
122 
124 
128 
131 
134 
135 protected:
136  bool SetCrypted();
137 
139  bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
140 
141  bool EncryptHDChain(const CKeyingMaterial& vMasterKeyIn);
142  bool DecryptHDChain(CHDChain& hdChainRet) const;
143  bool SetHDChain(const CHDChain& chain);
144  bool SetCryptedHDChain(const CHDChain& chain);
145 
146  bool Unlock(const CKeyingMaterial& vMasterKeyIn, bool fForMixingOnly = false);
147 
148 public:
150  {
151  }
152 
153  bool IsCrypted() const
154  {
155  return fUseCrypto;
156  }
157 
158  // This function should be used in a different combinations to determine
159  // if CCryptoKeyStore is fully locked so that no operations requiring access
160  // to private keys are possible:
161  // IsLocked(true)
162  // or if CCryptoKeyStore's private keys are available for mixing only:
163  // !IsLocked(true) && IsLocked()
164  // or if they are available for everything:
165  // !IsLocked()
166  bool IsLocked(bool fForMixing = false) const
167  {
168  if (!IsCrypted())
169  return false;
170  bool result;
171  {
172  LOCK(cs_KeyStore);
173  result = vMasterKey.empty();
174  }
175  // fForMixing fOnlyMixingAllowed return
176  // ---------------------------------------
177  // true true result
178  // true false result
179  // false true true
180  // false false result
181 
182  if(!fForMixing && fOnlyMixingAllowed) return true;
183 
184  return result;
185  }
186 
187  bool Lock(bool fAllowMixing = false);
188 
189  virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
190  bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
191  bool HaveKey(const CKeyID &address) const
192  {
193  {
194  LOCK(cs_KeyStore);
195  if (!IsCrypted())
196  return CBasicKeyStore::HaveKey(address);
197  return mapCryptedKeys.count(address) > 0;
198  }
199  return false;
200  }
201  bool GetKey(const CKeyID &address, CKey& keyOut) const;
202  bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
203  void GetKeys(std::set<CKeyID> &setAddress) const
204  {
205  if (!IsCrypted())
206  {
207  CBasicKeyStore::GetKeys(setAddress);
208  return;
209  }
210  setAddress.clear();
211  CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
212  while (mi != mapCryptedKeys.end())
213  {
214  setAddress.insert((*mi).first);
215  mi++;
216  }
217  }
218 
219  bool GetHDChain(CHDChain& hdChainRet) const;
220 
225  boost::signals2::signal<void (CCryptoKeyStore* wallet)> NotifyStatusChanged;
226 };
227 
228 #endif // BITCOIN_WALLET_CRYPTER_H
std::vector< unsigned char > vchSalt
Definition: crypter.h:37
bool IsCrypted() const
Definition: crypter.h:153
ADD_SERIALIZE_METHODS
Definition: crypter.h:46
bool HaveKey(const CKeyID &address) const
Check whether a key corresponding to a given address is present in the store.
Definition: crypter.h:191
std::vector< unsigned char > vchOtherDerivationParameters
Definition: crypter.h:44
const unsigned int WALLET_CRYPTO_KEY_SIZE
Definition: crypter.h:14
#define READWRITE(obj)
Definition: serialize.h:175
bool DecryptHDChain(CHDChain &hdChainRet) const
Definition: crypter.cpp:450
~CCrypter()
Definition: crypter.h:101
CMasterKey()
Definition: crypter.h:57
bool Encrypt(const CKeyingMaterial &vchPlaintext, std::vector< unsigned char > &vchCiphertext)
Definition: crypter.cpp:50
bool fOnlyMixingAllowed
if fOnlyMixingAllowed is true, only mixing should be allowed in unlocked wallet
Definition: crypter.h:133
unsigned char chKey[WALLET_CRYPTO_KEY_SIZE]
Definition: crypter.h:73
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
Add a key to the store.
Definition: crypter.cpp:302
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:61
bool HaveKey(const CKeyID &address) const
Check whether a key corresponding to a given address is present in the store.
Definition: keystore.h:69
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
Definition: keystore.h:116
Definition: pubkey.h:27
void CleanKey()
Definition: crypter.h:83
CryptedKeyMap mapCryptedKeys
Definition: crypter.h:120
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
Definition: crypter.h:67
bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector< unsigned char > &chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
Definition: crypter.cpp:17
std::map< CKeyID, std::pair< CPubKey, std::vector< unsigned char > > > CryptedKeyMap
Definition: keystore.h:117
bool DecryptAES256(const SecureString &sKey, const std::string &sCiphertext, const std::string &sIV, SecureString &sPlaintext)
Definition: crypter.cpp:174
bool fKeySet
Definition: crypter.h:75
bool GetKey(const CKeyID &address, CKey &keyOut) const
Definition: crypter.cpp:336
bool fUseCrypto
Definition: crypter.h:127
bool SetHDChain(const CHDChain &chain)
Definition: crypter.cpp:501
bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
Definition: crypter.cpp:354
bool GetHDChain(CHDChain &hdChainRet) const
Definition: crypter.cpp:525
unsigned char chIV[WALLET_CRYPTO_KEY_SIZE]
Definition: crypter.h:74
CCrypter()
Definition: crypter.h:90
void memory_cleanse(void *ptr, size_t len)
Definition: cleanse.cpp:10
CCriticalSection cs_KeyStore
Definition: keystore.h:23
#define LOCK(cs)
Definition: sync.h:168
CHDChain cryptedHDChain
Definition: crypter.h:121
void LockRange(void *p, size_t size)
Definition: pagelocker.h:44
boost::signals2::signal< void(CCryptoKeyStore *wallet)> NotifyStatusChanged
Definition: crypter.h:225
unsigned int nDeriveIterations
Definition: crypter.h:41
bool IsLocked(bool fForMixing=false) const
Definition: crypter.h:166
void UnlockRange(void *p, size_t size)
Definition: pagelocker.h:66
bool SetKey(const CKeyingMaterial &chNewKey, const std::vector< unsigned char > &chNewIV)
Definition: crypter.cpp:38
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: crypter.h:49
std::vector< unsigned char > vchCryptedKey
Definition: crypter.h:36
bool Decrypt(const std::vector< unsigned char > &vchCiphertext, CKeyingMaterial &vchPlaintext)
Definition: crypter.cpp:81
void GetKeys(std::set< CKeyID > &setAddress) const
Definition: crypter.h:203
void GetKeys(std::set< CKeyID > &setAddress) const
Definition: keystore.h:78
const unsigned int WALLET_CRYPTO_SALT_SIZE
Definition: crypter.h:15
unsigned int nDerivationMethod
Definition: crypter.h:40
bool Unlock(const CKeyingMaterial &vMasterKeyIn, bool fForMixingOnly=false)
Definition: crypter.cpp:248
bool Lock(bool fAllowMixing=false)
Definition: crypter.cpp:233
bool EncryptHDChain(const CKeyingMaterial &vMasterKeyIn)
Definition: crypter.cpp:397
bool SetCrypted()
Definition: crypter.cpp:222
virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret)
Definition: crypter.cpp:324
Definition: pubkey.h:37
CKeyingMaterial vMasterKey
Definition: crypter.h:123
bool fDecryptionThoroughlyChecked
keeps track of whether Unlock has run a thorough check before
Definition: crypter.h:130
static LockedPageManager & Instance()
Definition: pagelocker.h:136
bool SetCryptedHDChain(const CHDChain &chain)
Definition: crypter.cpp:513
Definition: key.h:35
bool EncryptAES256(const SecureString &sKey, const SecureString &sPlaintext, const std::string &sIV, std::string &sCiphertext)
Definition: crypter.cpp:125
bool EncryptKeys(CKeyingMaterial &vMasterKeyIn)
will encrypt previously unencrypted keys
Definition: crypter.cpp:373
result
Definition: rpcuser.py:37