PRCYCoin  2.0.0.7rc1
P2P Digital Currency
crypter.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2014 The Bitcoin 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_CRYPTER_H
6 #define BITCOIN_CRYPTER_H
7 
8 #include "allocators.h"
9 #include "keystore.h"
10 #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 const unsigned int WALLET_CRYPTO_IV_SIZE = 16;
17 
35 {
36 public:
37  std::vector<unsigned char> vchCryptedKey;
38  std::vector<unsigned char> vchSalt;
41  unsigned int nDerivationMethod;
42  unsigned int nDeriveIterations;
45  std::vector<unsigned char> vchOtherDerivationParameters;
46 
48 
49  template <typename Stream, typename Operation>
50  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
51  {
57  }
58 
60  {
61  // 25000 rounds is just under 0.1 seconds on a 1.86 GHz Pentium M
62  // ie slightly lower than the lowest hardware we need bother supporting
63  nDeriveIterations = 25000;
65  vchOtherDerivationParameters = std::vector<unsigned char>(0);
66  }
67 };
68 
69 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
70 
71 namespace wallet_crypto
72 {
73  class TestCrypter;
74 }
75 
77 class CCrypter
78 {
79  friend class wallet_crypto::TestCrypter; // for test access to chKey/chIV
80 private:
81  unsigned char chKey[WALLET_CRYPTO_KEY_SIZE];
82  unsigned char chIV[WALLET_CRYPTO_IV_SIZE];
83  bool fKeySet;
84 
85  int BytesToKeySHA512AES(const std::vector<unsigned char>& chSalt, const SecureString& strKeyData, int count, unsigned char *key,unsigned char *iv) const;
86 
87 public:
88  bool SetKeyFromPassphrase(const SecureString& strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod);
89  bool Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char>& vchCiphertext) const;
90  bool Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext) const;
91  bool SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV);
92 
93  void CleanKey()
94  {
95  memory_cleanse(chKey, sizeof(chKey));
96  memory_cleanse(chIV, sizeof(chIV));
97  fKeySet = false;
98  }
99 
101  {
102  fKeySet = false;
103 
104  // 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)
105  // Note that this does nothing about suspend-to-disk (which will put all our key data on disk)
106  // 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.
109  }
110 
112  {
113  CleanKey();
114 
117  }
118 };
119 
120 bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial& vchPlaintext, const uint256& nIV, std::vector<unsigned char>& vchCiphertext);
121 bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext);
122 
123 
128 {
129 private:
133 
134 protected:
135  // TODO: In the future, move this variable to the wallet class directly following upstream's structure.
137 
138  bool SetCrypted();
139 
141  bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
142  bool EncryptHDChain(const CKeyingMaterial& vMasterKeyIn);
143  bool DecryptHDChain(CHDChain& hdChainRet) const;
144  bool SetHDChain(const CHDChain& chain);
145  bool SetCryptedHDChain(const CHDChain& chain);
146  bool GetHDChain(CHDChain& hdChainRet) const;
147 
150 
151 public:
153 
154  bool IsCrypted() const
155  {
156  return fUseCrypto;
157  }
158 
159  bool IsLocked() const
160  {
161  if (!IsCrypted())
162  return false;
163  bool result;
164  {
165  LOCK(cs_KeyStore);
166  result = vMasterKey.empty();
167  }
168  return result;
169  }
170 
171  virtual bool AddCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret);
172  bool AddKeyPubKey(const CKey& key, const CPubKey& pubkey);
173  bool HaveKey(const CKeyID& address) const
174  {
175  {
176  LOCK(cs_KeyStore);
177  if (!IsCrypted())
178  return CBasicKeyStore::HaveKey(address);
179  return mapCryptedKeys.count(address) > 0;
180  }
181  return false;
182  }
183  bool GetKey(const CKeyID& address, CKey& keyOut) const;
184  bool GetPubKey(const CKeyID& address, CPubKey& vchPubKeyOut) const;
185  void GetKeys(std::set<CKeyID>& setAddress) const
186  {
187  if (!IsCrypted()) {
188  CBasicKeyStore::GetKeys(setAddress);
189  return;
190  }
191  setAddress.clear();
192  CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
193  while (mi != mapCryptedKeys.end()) {
194  setAddress.insert((*mi).first);
195  mi++;
196  }
197  }
198 
203  boost::signals2::signal<void(CCryptoKeyStore* wallet)> NotifyStatusChanged;
204 };
205 
206 #endif // BITCOIN_CRYPTER_H
CBasicKeyStore::GetKeys
void GetKeys(std::set< CKeyID > &setAddress) const
Definition: keystore.h:77
CMasterKey::vchSalt
std::vector< unsigned char > vchSalt
Definition: crypter.h:38
CCrypter::~CCrypter
~CCrypter()
Definition: crypter.h:111
WALLET_CRYPTO_KEY_SIZE
const unsigned int WALLET_CRYPTO_KEY_SIZE
Definition: crypter.h:14
CCryptoKeyStore::EncryptHDChain
bool EncryptHDChain(const CKeyingMaterial &vMasterKeyIn)
Definition: crypter.cpp:234
CCryptoKeyStore::SetCrypted
bool SetCrypted()
Definition: crypter.cpp:127
LockedPageManagerBase::UnlockRange
void UnlockRange(void *p, size_t size)
Definition: allocators.h:69
CMasterKey::vchOtherDerivationParameters
std::vector< unsigned char > vchOtherDerivationParameters
Use this for more parameters to key derivation, such as the various parameters to scrypt.
Definition: crypter.h:45
CCrypter::TestCrypter
friend class wallet_crypto::TestCrypter
Definition: crypter.h:79
CCryptoKeyStore::cryptedHDChain
CHDChain cryptedHDChain
Definition: crypter.h:149
CCryptoKeyStore
Keystore which keeps the private keys encrypted.
Definition: crypter.h:127
CCrypter::fKeySet
bool fKeySet
Definition: crypter.h:83
CCrypter::Encrypt
bool Encrypt(const CKeyingMaterial &vchPlaintext, std::vector< unsigned char > &vchCiphertext) const
Definition: crypter.cpp:74
CCryptoKeyStore::SetHDChain
bool SetHDChain(const CHDChain &chain)
Definition: crypter.cpp:334
CCryptoKeyStore::GetKeys
void GetKeys(std::set< CKeyID > &setAddress) const
Definition: crypter.h:185
CCryptoKeyStore::GetKey
bool GetKey(const CKeyID &address, CKey &keyOut) const
Definition: crypter.cpp:172
CKeyingMaterial
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
Definition: crypter.h:69
CMasterKey::ADD_SERIALIZE_METHODS
ADD_SERIALIZE_METHODS
Definition: crypter.h:47
DecryptSecret
bool DecryptSecret(const CKeyingMaterial &vMasterKey, const std::vector< unsigned char > &vchCiphertext, const uint256 &nIV, CKeyingMaterial &vchPlaintext)
Definition: crypter.cpp:117
CMasterKey::vchCryptedKey
std::vector< unsigned char > vchCryptedKey
Definition: crypter.h:37
CBasicKeyStore::HaveKey
bool HaveKey(const CKeyID &address) const
Check whether a key corresponding to a given address is present in the store.
Definition: keystore.h:68
CCrypter::Decrypt
bool Decrypt(const std::vector< unsigned char > &vchCiphertext, CKeyingMaterial &vchPlaintext) const
Definition: crypter.cpp:91
CMasterKey
Private key encryption is done based on a CMasterKey, which holds a salt and random encryption key.
Definition: crypter.h:34
CKeyID
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:29
memory_cleanse
void memory_cleanse(void *ptr, size_t len)
Definition: cleanse.cpp:27
CKeyStore::cs_KeyStore
RecursiveMutex cs_KeyStore
Definition: keystore.h:24
CCryptoKeyStore::AddKeyPubKey
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
Add a key to the store.
Definition: crypter.cpp:138
WALLET_CRYPTO_IV_SIZE
const unsigned int WALLET_CRYPTO_IV_SIZE
Definition: crypter.h:16
CCryptoKeyStore::CCryptoKeyStore
CCryptoKeyStore()
Definition: crypter.h:152
CCrypter::chIV
unsigned char chIV[WALLET_CRYPTO_IV_SIZE]
Definition: crypter.h:82
CCryptoKeyStore::NotifyStatusChanged
boost::signals2::signal< void(CCryptoKeyStore *wallet)> NotifyStatusChanged
Wallet status (encrypted, locked) changed.
Definition: crypter.h:203
CCrypter::BytesToKeySHA512AES
int BytesToKeySHA512AES(const std::vector< unsigned char > &chSalt, const SecureString &strKeyData, int count, unsigned char *key, unsigned char *iv) const
Definition: crypter.cpp:16
CCryptoKeyStore::SetCryptedHDChain
bool SetCryptedHDChain(const CHDChain &chain)
Definition: crypter.cpp:346
CCryptoKeyStore::fUseCrypto
bool fUseCrypto
if fUseCrypto is true, mapKeys must be empty if fUseCrypto is false, vMasterKey must be empty
Definition: crypter.h:132
CCrypter::CleanKey
void CleanKey()
Definition: crypter.h:93
SecureString
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: allocators.h:262
allocators.h
CCrypter::chKey
unsigned char chKey[WALLET_CRYPTO_KEY_SIZE]
Definition: crypter.h:81
keystore.h
TestCrypter
Definition: crypto_tests.cpp:85
uint256
256-bit unsigned big integer.
Definition: uint256.h:38
CryptedKeyMap
std::map< CKeyID, std::pair< CPubKey, std::vector< unsigned char > > > CryptedKeyMap
Definition: keystore.h:112
CMasterKey::SerializationOp
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: crypter.h:50
CCrypter::CCrypter
CCrypter()
Definition: crypter.h:100
CCryptoKeyStore::AddCryptedKey
virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret)
Definition: crypter.cpp:160
CPubKey
An encapsulated public key.
Definition: pubkey.h:37
READWRITE
#define READWRITE(obj)
Definition: serialize.h:164
CCrypter::SetKey
bool SetKey(const CKeyingMaterial &chNewKey, const std::vector< unsigned char > &chNewIV)
Definition: crypter.cpp:62
EncryptSecret
bool EncryptSecret(const CKeyingMaterial &vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256 &nIV, std::vector< unsigned char > &vchCiphertext)
Definition: crypter.cpp:107
CKey
An encapsulated private key.
Definition: key.h:39
CCrypter
Encryption/decryption context with key information.
Definition: crypter.h:77
CHDChain
Definition: hdchain.h:11
LOCK
#define LOCK(cs)
Definition: sync.h:182
CCryptoKeyStore::DecryptHDChain
bool DecryptHDChain(CHDChain &hdChainRet) const
Definition: crypter.cpp:287
key
CKey key
Definition: bip38tooldialog.cpp:173
CCryptoKeyStore::GetHDChain
bool GetHDChain(CHDChain &hdChainRet) const
Definition: crypter.cpp:358
CCryptoKeyStore::mapCryptedKeys
CryptedKeyMap mapCryptedKeys
Definition: crypter.h:148
CCrypter::SetKeyFromPassphrase
bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector< unsigned char > &chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
Definition: crypter.cpp:43
CMasterKey::CMasterKey
CMasterKey()
Definition: crypter.h:59
CMasterKey::nDerivationMethod
unsigned int nDerivationMethod
0 = EVP_sha512() 1 = scrypt()
Definition: crypter.h:41
LockedPageManager::Instance
static LockedPageManager & Instance()
Definition: allocators.h:139
serialize.h
CCryptoKeyStore::GetPubKey
bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
Definition: crypter.cpp:195
CCryptoKeyStore::IsCrypted
bool IsCrypted() const
Definition: crypter.h:154
CBasicKeyStore
Basic key store, that keeps keys in an address->secret map.
Definition: keystore.h:57
WALLET_CRYPTO_SALT_SIZE
const unsigned int WALLET_CRYPTO_SALT_SIZE
Definition: crypter.h:15
wallet_crypto
Definition: crypter.h:71
CCryptoKeyStore::vMasterKey
CKeyingMaterial vMasterKey
Definition: crypter.h:136
CMasterKey::nDeriveIterations
unsigned int nDeriveIterations
Definition: crypter.h:42
CCryptoKeyStore::IsLocked
bool IsLocked() const
Definition: crypter.h:159
CCryptoKeyStore::EncryptKeys
bool EncryptKeys(CKeyingMaterial &vMasterKeyIn)
will encrypt previously unencrypted keys
Definition: crypter.cpp:211
CCryptoKeyStore::HaveKey
bool HaveKey(const CKeyID &address) const
Check whether a key corresponding to a given address is present in the store.
Definition: crypter.h:173
LockedPageManagerBase::LockRange
void LockRange(void *p, size_t size)
Definition: allocators.h:47