Raven Core  3.0.0
P2P Digital Currency
crypter.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef RAVEN_WALLET_CRYPTER_H
7 #define RAVEN_WALLET_CRYPTER_H
8 
9 #include "keystore.h"
10 #include "serialize.h"
12 
13 #include <atomic>
14 
15 const unsigned int WALLET_CRYPTO_KEY_SIZE = 32;
16 const unsigned int WALLET_CRYPTO_SALT_SIZE = 8;
17 const unsigned int WALLET_CRYPTO_IV_SIZE = 16;
18 
36 {
37 public:
38  std::vector<unsigned char> vchCryptedKey;
39  std::vector<unsigned char> vchSalt;
42  unsigned int nDerivationMethod;
43  unsigned int nDeriveIterations;
46  std::vector<unsigned char> vchOtherDerivationParameters;
47 
49 
50  template <typename Stream, typename Operation>
51  inline void SerializationOp(Stream& s, Operation ser_action) {
52  READWRITE(vchCryptedKey);
53  READWRITE(vchSalt);
54  READWRITE(nDerivationMethod);
55  READWRITE(nDeriveIterations);
56  READWRITE(vchOtherDerivationParameters);
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;
64  nDerivationMethod = 0;
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  std::vector<unsigned char, secure_allocator<unsigned char>> vchKey;
82  std::vector<unsigned char, secure_allocator<unsigned char>> vchIV;
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(vchKey.data(), vchKey.size());
96  memory_cleanse(vchIV.data(), vchIV.size());
97  fKeySet = false;
98  }
99 
101  {
102  fKeySet = false;
103  vchKey.resize(WALLET_CRYPTO_KEY_SIZE);
104  vchIV.resize(WALLET_CRYPTO_IV_SIZE);
105  }
106 
108  {
109  CleanKey();
110  }
111 };
112 
117 {
118 private:
119 
121 
124  std::atomic<bool> fUseCrypto;
125 
128 
129 protected:
130  bool SetCrypted();
131 
133  bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
134 
135  bool Unlock(const CKeyingMaterial& vMasterKeyIn);
137 
138 public:
139  CCryptoKeyStore() : fUseCrypto(false), fDecryptionThoroughlyChecked(false)
140  {
141  }
142 
143  bool IsCrypted() const
144  {
145  return fUseCrypto;
146  }
147 
148  bool IsLocked() const
149  {
150  if (!IsCrypted())
151  return false;
152  bool result;
153  {
154  LOCK(cs_KeyStore);
155  result = vMasterKey.empty();
156  }
157  return result;
158  }
159 
160  bool Lock();
161 
162  virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
163  bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey) override;
164  bool HaveKey(const CKeyID &address) const override
165  {
166  {
167  LOCK(cs_KeyStore);
168  if (!IsCrypted()) {
169  return CBasicKeyStore::HaveKey(address);
170  }
171  return mapCryptedKeys.count(address) > 0;
172  }
173  return false;
174  }
175  bool GetKey(const CKeyID &address, CKey& keyOut) const override;
176  bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override;
177  std::set<CKeyID> GetKeys() const override
178  {
179  LOCK(cs_KeyStore);
180  if (!IsCrypted()) {
181  return CBasicKeyStore::GetKeys();
182  }
183  std::set<CKeyID> set_address;
184  for (const auto& mi : mapCryptedKeys) {
185  set_address.insert(mi.first);
186  }
187  return set_address;
188  }
189 
194  boost::signals2::signal<void (CCryptoKeyStore* wallet)> NotifyStatusChanged;
195 };
196 
197 #endif // RAVEN_WALLET_CRYPTER_H
unsigned int nDerivationMethod
0 = EVP_sha512() 1 = scrypt()
Definition: crypter.h:42
bool HaveKey(const CKeyID &address) const override
Check whether a key corresponding to a given address is present in the store.
Definition: crypter.h:164
const unsigned int WALLET_CRYPTO_KEY_SIZE
Definition: crypter.h:15
#define READWRITE(obj)
Definition: serialize.h:163
Encryption/decryption context with key information.
Definition: crypter.h:77
bool IsCrypted() const
Definition: crypter.h:143
std::vector< unsigned char > vchCryptedKey
Definition: crypter.h:38
Private key encryption is done based on a CMasterKey, which holds a salt and random encryption key...
Definition: crypter.h:35
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:57
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
Definition: keystore.h:107
std::vector< unsigned char > vchOtherDerivationParameters
Use this for more parameters to key derivation, such as the various parameters to scrypt...
Definition: crypter.h:46
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
Definition: crypter.h:69
void CleanKey()
Definition: crypter.h:93
std::map< CKeyID, std::pair< CPubKey, std::vector< unsigned char > > > CryptedKeyMap
Definition: keystore.h:108
std::set< CKeyID > GetKeys() const override
Definition: crypter.h:177
std::atomic< bool > fUseCrypto
if fUseCrypto is true, mapKeys must be empty if fUseCrypto is false, vMasterKey must be empty ...
Definition: crypter.h:124
CKeyingMaterial vMasterKey
Definition: crypter.h:120
const unsigned int WALLET_CRYPTO_IV_SIZE
Definition: crypter.h:17
Keystore which keeps the private keys encrypted.
Definition: crypter.h:116
CCrypter()
Definition: crypter.h:100
boost::signals2::signal< void(CCryptoKeyStore *wallet)> NotifyStatusChanged
Wallet status (encrypted, locked) changed.
Definition: crypter.h:194
void memory_cleanse(void *ptr, size_t len)
Definition: cleanse.cpp:28
std::vector< unsigned char, secure_allocator< unsigned char > > vchKey
Definition: crypter.h:81
std::vector< unsigned char, secure_allocator< unsigned char > > vchIV
Definition: crypter.h:82
#define LOCK(cs)
Definition: sync.h:176
bool IsLocked() const
Definition: crypter.h:148
bool fKeySet
Definition: crypter.h:83
An encapsulated public key.
Definition: pubkey.h:40
ADD_SERIALIZE_METHODS
Definition: crypter.h:48
CMasterKey()
Definition: crypter.h:59
const unsigned int WALLET_CRYPTO_SALT_SIZE
Definition: crypter.h:16
CryptedKeyMap mapCryptedKeys
Definition: crypter.h:136
void SerializationOp(Stream &s, Operation ser_action)
Definition: crypter.h:51
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:30
~CCrypter()
Definition: crypter.h:107
std::vector< unsigned char > vchSalt
Definition: crypter.h:39
An encapsulated private key.
Definition: key.h:36
unsigned int nDeriveIterations
Definition: crypter.h:43
bool HaveKey(const CKeyID &address) const override
Check whether a key corresponding to a given address is present in the store.
Definition: keystore.h:66
bool fDecryptionThoroughlyChecked
keeps track of whether Unlock has run a thorough check before
Definition: crypter.h:127
std::set< CKeyID > GetKeys() const override
Definition: keystore.h:75
Basic key store, that keeps keys in an address->secret map.
Definition: keystore.h:55