Master Core  v0.0.9 - 2abfd2849db8ba7a83957c64eb976b406713c123
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
crypter.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2013 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef __CRYPTER_H__
6 #define __CRYPTER_H__
7 
8 #include "allocators.h"
9 #include "serialize.h"
10 #include "keystore.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 
17 /*
18 Private key encryption is done based on a CMasterKey,
19 which holds a salt and random encryption key.
20 
21 CMasterKeys are encrypted using AES-256-CBC using a key
22 derived using derivation method nDerivationMethod
23 (0 == EVP_sha512()) and derivation iterations nDeriveIterations.
24 vchOtherDerivationParameters is provided for alternative algorithms
25 which may require more parameters (such as scrypt).
26 
27 Wallet Private Keys are then encrypted using AES-256-CBC
28 with the double-sha256 of the public key as the IV, and the
29 master key's key as the encryption key (see keystore.[ch]).
30 */
31 
34 {
35 public:
36  std::vector<unsigned char> vchCryptedKey;
37  std::vector<unsigned char> vchSalt;
38  // 0 = EVP_sha512()
39  // 1 = scrypt()
40  unsigned int nDerivationMethod;
41  unsigned int nDeriveIterations;
42  // Use this for more parameters to key derivation,
43  // such as the various parameters to scrypt
44  std::vector<unsigned char> vchOtherDerivationParameters;
45 
47  (
48  READWRITE(vchCryptedKey);
49  READWRITE(vchSalt);
50  READWRITE(nDerivationMethod);
51  READWRITE(nDeriveIterations);
52  READWRITE(vchOtherDerivationParameters);
53  )
54  CMasterKey()
55  {
56  // 25000 rounds is just under 0.1 seconds on a 1.86 GHz Pentium M
57  // ie slightly lower than the lowest hardware we need bother supporting
58  nDeriveIterations = 25000;
59  nDerivationMethod = 0;
60  vchOtherDerivationParameters = std::vector<unsigned char>(0);
61  }
62 };
63 
64 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
65 
67 class CCrypter
68 {
69 private:
70  unsigned char chKey[WALLET_CRYPTO_KEY_SIZE];
71  unsigned char chIV[WALLET_CRYPTO_KEY_SIZE];
72  bool fKeySet;
73 
74 public:
75  bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod);
76  bool Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext);
77  bool Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext);
78  bool SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV);
79 
80  void CleanKey()
81  {
82  OPENSSL_cleanse(chKey, sizeof(chKey));
83  OPENSSL_cleanse(chIV, sizeof(chIV));
84  fKeySet = false;
85  }
86 
88  {
89  fKeySet = false;
90 
91  // 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)
92  // Note that this does nothing about suspend-to-disk (which will put all our key data on disk)
93  // 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.
94  LockedPageManager::Instance().LockRange(&chKey[0], sizeof chKey);
95  LockedPageManager::Instance().LockRange(&chIV[0], sizeof chIV);
96  }
97 
99  {
100  CleanKey();
101 
102  LockedPageManager::Instance().UnlockRange(&chKey[0], sizeof chKey);
103  LockedPageManager::Instance().UnlockRange(&chIV[0], sizeof chIV);
104  }
105 };
106 
107 bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext);
108 bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext);
109 
114 {
115 private:
117 
118  CKeyingMaterial vMasterKey;
119 
120  // if fUseCrypto is true, mapKeys must be empty
121  // if fUseCrypto is false, vMasterKey must be empty
123 
124 protected:
125  bool SetCrypted();
126 
127  // will encrypt previously unencrypted keys
128  bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
129 
130  bool Unlock(const CKeyingMaterial& vMasterKeyIn);
131 
132 public:
133  CCryptoKeyStore() : fUseCrypto(false)
134  {
135  }
136 
137  bool IsCrypted() const
138  {
139  return fUseCrypto;
140  }
141 
142  bool IsLocked() const
143  {
144  if (!IsCrypted())
145  return false;
146  bool result;
147  {
148  LOCK(cs_KeyStore);
149  result = vMasterKey.empty();
150  }
151  return result;
152  }
153 
154  bool Lock();
155 
156  virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
157  bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
158  bool HaveKey(const CKeyID &address) const
159  {
160  {
161  LOCK(cs_KeyStore);
162  if (!IsCrypted())
163  return CBasicKeyStore::HaveKey(address);
164  return mapCryptedKeys.count(address) > 0;
165  }
166  return false;
167  }
168  bool GetKey(const CKeyID &address, CKey& keyOut) const;
169  bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
170  void GetKeys(std::set<CKeyID> &setAddress) const
171  {
172  if (!IsCrypted())
173  {
174  CBasicKeyStore::GetKeys(setAddress);
175  return;
176  }
177  setAddress.clear();
178  CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
179  while (mi != mapCryptedKeys.end())
180  {
181  setAddress.insert((*mi).first);
182  mi++;
183  }
184  }
185 
186  /* Wallet status (encrypted, locked) changed.
187  * Note: Called without locks held.
188  */
189  boost::signals2::signal<void (CCryptoKeyStore* wallet)> NotifyStatusChanged;
190 };
191 
192 #endif
bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector< unsigned char > &chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
Definition: crypter.cpp:15
bool IsCrypted() const
Definition: crypter.h:137
unsigned int nDerivationMethod
Definition: crypter.h:40
CCriticalSection cs_KeyStore
Definition: keystore.h:20
bool HaveKey(const CKeyID &address) const
Definition: crypter.h:158
bool Encrypt(const CKeyingMaterial &vchPlaintext, std::vector< unsigned char > &vchCiphertext)
Definition: crypter.cpp:48
const unsigned int WALLET_CRYPTO_KEY_SIZE
Definition: crypter.h:14
#define READWRITE(obj)
Definition: serialize.h:92
void LockRange(void *p, size_t size)
Definition: allocators.h:46
unsigned char chIV[WALLET_CRYPTO_KEY_SIZE]
Definition: crypter.h:71
bool SetKey(const CKeyingMaterial &chNewKey, const std::vector< unsigned char > &chNewIV)
Definition: crypter.cpp:36
Encryption/decryption context with key information.
Definition: crypter.h:67
void GetKeys(std::set< CKeyID > &setAddress) const
Definition: keystore.h:62
std::vector< unsigned char > vchCryptedKey
Definition: crypter.h:36
bool HaveKey(const CKeyID &address) const
Definition: keystore.h:53
Master key for wallet encryption.
Definition: crypter.h:33
bool SetCrypted()
Definition: crypter.cpp:123
std::vector< unsigned char > vchOtherDerivationParameters
Definition: crypter.h:44
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
Definition: crypter.h:64
void CleanKey()
Definition: crypter.h:80
bool DecryptSecret(const CKeyingMaterial &vMasterKey, const std::vector< unsigned char > &vchCiphertext, const uint256 &nIV, CKeyingMaterial &vchPlaintext)
Definition: crypter.cpp:113
std::map< CKeyID, std::pair< CPubKey, std::vector< unsigned char > > > CryptedKeyMap
Definition: keystore.h:94
bool EncryptKeys(CKeyingMaterial &vMasterKeyIn)
Definition: crypter.cpp:252
bool IsLocked() const
Definition: crypter.h:142
IMPLEMENT_SERIALIZE(READWRITE(vchCryptedKey);READWRITE(vchSalt);READWRITE(nDerivationMethod);READWRITE(nDeriveIterations);READWRITE(vchOtherDerivationParameters);) CMasterKey()
Definition: crypter.h:47
CKeyingMaterial vMasterKey
Definition: crypter.h:118
virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret)
Definition: crypter.cpp:199
Keystore which keeps the private keys encrypted.
Definition: crypter.h:113
bool GetKey(const CKeyID &address, CKey &keyOut) const
Definition: crypter.cpp:211
bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
Definition: crypter.cpp:235
CCrypter()
Definition: crypter.h:87
boost::signals2::signal< void(CCryptoKeyStore *wallet)> NotifyStatusChanged
Definition: crypter.h:189
void GetKeys(std::set< CKeyID > &setAddress) const
Definition: crypter.h:170
#define LOCK(cs)
Definition: sync.h:156
bool fKeySet
Definition: crypter.h:72
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: allocators.h:253
An encapsulated public key.
Definition: key.h:42
bool Unlock(const CKeyingMaterial &vMasterKeyIn)
Definition: crypter.cpp:148
bool Decrypt(const std::vector< unsigned char > &vchCiphertext, CKeyingMaterial &vchPlaintext)
Definition: crypter.cpp:75
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
Definition: crypter.cpp:177
unsigned char chKey[WALLET_CRYPTO_KEY_SIZE]
Definition: crypter.h:70
static LockedPageManager & Instance()
Definition: allocators.h:139
256-bit unsigned integer
Definition: uint256.h:531
const unsigned int WALLET_CRYPTO_SALT_SIZE
Definition: crypter.h:15
CryptedKeyMap mapCryptedKeys
Definition: crypter.h:116
void UnlockRange(void *p, size_t size)
Definition: allocators.h:69
A reference to a CKey: the Hash160 of its serialized public key.
Definition: key.h:26
~CCrypter()
Definition: crypter.h:98
std::vector< unsigned char > vchSalt
Definition: crypter.h:37
bool fUseCrypto
Definition: crypter.h:122
bool EncryptSecret(const CKeyingMaterial &vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256 &nIV, std::vector< unsigned char > &vchCiphertext)
Definition: crypter.cpp:103
An encapsulated private key.
Definition: key.h:179
unsigned int nDeriveIterations
Definition: crypter.h:41
Basic key store, that keeps keys in an address->secret map.
Definition: keystore.h:45