Fabcoin Core  0.16.2
P2P Digital Currency
crypter.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2017 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 FABCOIN_WALLET_CRYPTER_H
6 #define FABCOIN_WALLET_CRYPTER_H
7 
8 #include <keystore.h>
9 #include <serialize.h>
11 
12 const unsigned int WALLET_CRYPTO_KEY_SIZE = 32;
13 const unsigned int WALLET_CRYPTO_SALT_SIZE = 8;
14 const unsigned int WALLET_CRYPTO_IV_SIZE = 16;
15 
33 {
34 public:
35  std::vector<unsigned char> vchCryptedKey;
36  std::vector<unsigned char> vchSalt;
39  unsigned int nDerivationMethod;
40  unsigned int nDeriveIterations;
43  std::vector<unsigned char> vchOtherDerivationParameters;
44 
46 
47  template <typename Stream, typename Operation>
48  inline void SerializationOp(Stream& s, Operation ser_action) {
49  READWRITE(vchCryptedKey);
50  READWRITE(vchSalt);
51  READWRITE(nDerivationMethod);
52  READWRITE(nDeriveIterations);
53  READWRITE(vchOtherDerivationParameters);
54  }
55 
57  {
58  // 25000 rounds is just under 0.1 seconds on a 1.86 GHz Pentium M
59  // ie slightly lower than the lowest hardware we need bother supporting
60  nDeriveIterations = 25000;
61  nDerivationMethod = 0;
62  vchOtherDerivationParameters = std::vector<unsigned char>(0);
63  }
64 };
65 
66 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
67 
68 namespace wallet_crypto
69 {
70  class TestCrypter;
71 }
72 
74 class CCrypter
75 {
76 friend class wallet_crypto::TestCrypter; // for test access to chKey/chIV
77 private:
78  std::vector<unsigned char, secure_allocator<unsigned char>> vchKey;
79  std::vector<unsigned char, secure_allocator<unsigned char>> vchIV;
80  bool fKeySet;
81 
82  int BytesToKeySHA512AES(const std::vector<unsigned char>& chSalt, const SecureString& strKeyData, int count, unsigned char *key,unsigned char *iv) const;
83 
84 public:
85  bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod);
86  bool Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext) const;
87  bool Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext) const;
88  bool SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV);
89 
90  void CleanKey()
91  {
92  memory_cleanse(vchKey.data(), vchKey.size());
93  memory_cleanse(vchIV.data(), vchIV.size());
94  fKeySet = false;
95  }
96 
98  {
99  fKeySet = false;
100  vchKey.resize(WALLET_CRYPTO_KEY_SIZE);
101  vchIV.resize(WALLET_CRYPTO_IV_SIZE);
102  }
103 
105  {
106  CleanKey();
107  }
108 };
109 
114 {
115 private:
117 
119 
123 
126 
127 protected:
128  bool SetCrypted();
129 
131  bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
132 
133  bool Unlock(const CKeyingMaterial& vMasterKeyIn);
134 
135 public:
136  CCryptoKeyStore() : fUseCrypto(false), fDecryptionThoroughlyChecked(false)
137  {
138  }
139 
140  bool IsCrypted() const
141  {
142  return fUseCrypto;
143  }
144 
145  bool IsLocked() const
146  {
147  if (!IsCrypted())
148  return false;
149  bool result;
150  {
151  LOCK(cs_KeyStore);
152  result = vMasterKey.empty();
153  }
154  return result;
155  }
156 
157  bool Lock();
158 
159  virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
160  bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey) override;
161  bool HaveKey(const CKeyID &address) const override
162  {
163  {
164  LOCK(cs_KeyStore);
165  if (!IsCrypted())
166  return CBasicKeyStore::HaveKey(address);
167  return mapCryptedKeys.count(address) > 0;
168  }
169  return false;
170  }
171  bool GetKey(const CKeyID &address, CKey& keyOut) const override;
172  bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override;
173  void GetKeys(std::set<CKeyID> &setAddress) const override
174  {
175  if (!IsCrypted())
176  {
177  CBasicKeyStore::GetKeys(setAddress);
178  return;
179  }
180  setAddress.clear();
181  CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
182  while (mi != mapCryptedKeys.end())
183  {
184  setAddress.insert((*mi).first);
185  mi++;
186  }
187  }
188 
193  boost::signals2::signal<void (CCryptoKeyStore* wallet)> NotifyStatusChanged;
194 };
195 
196 #endif // FABCOIN_WALLET_CRYPTER_H
bool IsCrypted() const
Definition: crypter.h:140
unsigned int nDerivationMethod
0 = EVP_sha512() 1 = scrypt()
Definition: crypter.h:39
bool HaveKey(const CKeyID &address) const override
Check whether a key corresponding to a given address is present in the store.
Definition: crypter.h:161
const unsigned int WALLET_CRYPTO_KEY_SIZE
Definition: crypter.h:12
#define READWRITE(obj)
Definition: serialize.h:179
void GetKeys(std::set< CKeyID > &setAddress) const override
Definition: keystore.h:74
Encryption/decryption context with key information.
Definition: crypter.h:74
std::vector< unsigned char > vchCryptedKey
Definition: crypter.h:35
Private key encryption is done based on a CMasterKey, which holds a salt and random encryption key...
Definition: crypter.h:32
size_t count
Definition: ExecStats.cpp:37
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:56
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
Definition: keystore.h:110
std::vector< unsigned char > vchOtherDerivationParameters
Use this for more parameters to key derivation, such as the various parameters to scrypt...
Definition: crypter.h:43
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
Definition: crypter.h:66
void CleanKey()
Definition: crypter.h:90
std::map< CKeyID, std::pair< CPubKey, std::vector< unsigned char > > > CryptedKeyMap
Definition: keystore.h:111
bool IsLocked() const
Definition: crypter.h:145
CKeyingMaterial vMasterKey
Definition: crypter.h:118
const unsigned int WALLET_CRYPTO_IV_SIZE
Definition: crypter.h:14
Keystore which keeps the private keys encrypted.
Definition: crypter.h:113
void GetKeys(std::set< CKeyID > &setAddress) const override
Definition: crypter.h:173
CCrypter()
Definition: crypter.h:97
boost::signals2::signal< void(CCryptoKeyStore *wallet)> NotifyStatusChanged
Wallet status (encrypted, locked) changed.
Definition: crypter.h:193
void memory_cleanse(void *ptr, size_t len)
Definition: cleanse.cpp:10
std::vector< unsigned char, secure_allocator< unsigned char > > vchKey
Definition: crypter.h:78
std::vector< unsigned char, secure_allocator< unsigned char > > vchIV
Definition: crypter.h:79
#define LOCK(cs)
Definition: sync.h:175
bool fKeySet
Definition: crypter.h:80
An encapsulated public key.
Definition: pubkey.h:39
ADD_SERIALIZE_METHODS
Definition: crypter.h:45
CMasterKey()
Definition: crypter.h:56
const unsigned int WALLET_CRYPTO_SALT_SIZE
Definition: crypter.h:13
CryptedKeyMap mapCryptedKeys
Definition: crypter.h:116
void SerializationOp(Stream &s, Operation ser_action)
Definition: crypter.h:48
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:29
~CCrypter()
Definition: crypter.h:104
std::vector< unsigned char > vchSalt
Definition: crypter.h:36
bool fUseCrypto
if fUseCrypto is true, mapKeys must be empty if fUseCrypto is false, vMasterKey must be empty ...
Definition: crypter.h:122
struct evm_uint160be address(struct evm_env *env)
Definition: capi.c:13
An encapsulated private key.
Definition: key.h:35
unsigned int nDeriveIterations
Definition: crypter.h:40
bool HaveKey(const CKeyID &address) const override
Check whether a key corresponding to a given address is present in the store.
Definition: keystore.h:65
bool fDecryptionThoroughlyChecked
keeps track of whether Unlock has run a thorough check before
Definition: crypter.h:125
Basic key store, that keeps keys in an address->secret map.
Definition: keystore.h:54