Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

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 #include <atomic>
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) {
56  }
57 
59  {
60  // 25000 rounds is just under 0.1 seconds on a 1.86 GHz Pentium M
61  // ie slightly lower than the lowest hardware we need bother supporting
62  nDeriveIterations = 25000;
64  vchOtherDerivationParameters = std::vector<unsigned char>(0);
65  }
66 };
67 
68 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
69 
70 namespace wallet_crypto
71 {
72  class TestCrypter;
73 }
74 
76 class CCrypter
77 {
78 friend class wallet_crypto::TestCrypter; // for test access to chKey/chIV
79 private:
80  std::vector<unsigned char, secure_allocator<unsigned char>> vchKey;
81  std::vector<unsigned char, secure_allocator<unsigned char>> vchIV;
82  bool fKeySet;
83 
84  int BytesToKeySHA512AES(const std::vector<unsigned char>& chSalt, const SecureString& strKeyData, int count, unsigned char *key,unsigned char *iv) const;
85 
86 public:
87  bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod);
88  bool Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext) const;
89  bool Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext) const;
90  bool SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV);
91 
92  void CleanKey()
93  {
94  memory_cleanse(vchKey.data(), vchKey.size());
95  memory_cleanse(vchIV.data(), vchIV.size());
96  fKeySet = false;
97  }
98 
100  {
101  fKeySet = false;
104  }
105 
107  {
108  CleanKey();
109  }
110 };
111 
112 bool EncryptAES256(const SecureString& sKey, const SecureString& sPlaintext, const std::string& sIV, std::string& sCiphertext);
113 bool DecryptAES256(const SecureString& sKey, const std::string& sCiphertext, const std::string& sIV, SecureString& sPlaintext);
114 
115 
120 {
121 private:
123 
125 
128  std::atomic<bool> fUseCrypto;
129 
132 
135 
136 protected:
137  bool SetCrypted();
138 
140  bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
141 
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 
147  bool Unlock(const CKeyingMaterial& vMasterKeyIn, bool fForMixingOnly = false);
149 
150 public:
152  {
153  }
154 
155  bool IsCrypted() const { return fUseCrypto; }
156  bool IsLocked(bool fForMixing = false) const;
157  bool Lock(bool fForMixing = false);
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  bool GetKey(const CKeyID &address, CKey& keyOut) const override;
163  bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override;
164  std::set<CKeyID> GetKeys() const override;
165 
166  virtual bool GetHDChain(CHDChain& hdChainRet) const override;
167 
172  boost::signals2::signal<void (CCryptoKeyStore* wallet)> NotifyStatusChanged;
173 };
174 
175 #endif // BITCOIN_WALLET_CRYPTER_H
bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector< unsigned char > &chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
Definition: crypter.cpp:42
bool SetHDChain(const CHDChain &chain)
Definition: crypter.cpp:515
unsigned int nDerivationMethod
0 = EVP_sha512() 1 = scrypt()
Definition: crypter.h:41
bool Lock(bool fForMixing=false)
Definition: crypter.cpp:230
bool HaveKey(const CKeyID &address) const override
Check whether a key corresponding to a given address is present in the store.
Definition: crypter.cpp:334
const unsigned int WALLET_CRYPTO_KEY_SIZE
Definition: crypter.h:14
#define READWRITE(obj)
Definition: serialize.h:165
bool Encrypt(const CKeyingMaterial &vchPlaintext, std::vector< unsigned char > &vchCiphertext) const
Definition: crypter.cpp:74
bool SetKey(const CKeyingMaterial &chNewKey, const std::vector< unsigned char > &chNewIV)
Definition: crypter.cpp:62
Encryption/decryption context with key information.
Definition: crypter.h:76
bool IsCrypted() const
Definition: crypter.h:155
std::vector< unsigned char > vchCryptedKey
Definition: crypter.h:37
Private key encryption is done based on a CMasterKey, which holds a salt and random encryption key...
Definition: crypter.h:34
bool SetCrypted()
Definition: crypter.cpp:190
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:85
std::vector< unsigned char > vchOtherDerivationParameters
Use this for more parameters to key derivation, such as the various parameters to scrypt...
Definition: crypter.h:45
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
Definition: crypter.h:68
void CleanKey()
Definition: crypter.h:92
std::map< CKeyID, std::pair< CPubKey, std::vector< unsigned char > > > CryptedKeyMap
Definition: keystore.h:86
bool EncryptKeys(CKeyingMaterial &vMasterKeyIn)
will encrypt previously unencrypted keys
Definition: crypter.cpp:389
int BytesToKeySHA512AES(const std::vector< unsigned char > &chSalt, const SecureString &strKeyData, int count, unsigned char *key, unsigned char *iv) const
Definition: crypter.cpp:16
std::atomic< bool > fUseCrypto
if fUseCrypto is true, mapKeys must be empty if fUseCrypto is false, vMasterKey must be empty ...
Definition: crypter.h:128
CKeyingMaterial vMasterKey
Definition: crypter.h:124
bool DecryptAES256(const SecureString &sKey, const std::string &sCiphertext, const std::string &sIV, SecureString &sPlaintext)
Definition: crypter.cpp:155
bool Decrypt(const std::vector< unsigned char > &vchCiphertext, CKeyingMaterial &vchPlaintext) const
Definition: crypter.cpp:92
const unsigned int WALLET_CRYPTO_IV_SIZE
Definition: crypter.h:16
bool Unlock(const CKeyingMaterial &vMasterKeyIn, bool fForMixingOnly=false)
Definition: crypter.cpp:245
virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret)
Definition: crypter.cpp:323
Keystore which keeps the private keys encrypted.
Definition: crypter.h:119
bool fOnlyMixingAllowed
if fOnlyMixingAllowed is true, only mixing should be allowed in unlocked wallet
Definition: crypter.h:134
bool SetCryptedHDChain(const CHDChain &chain)
Definition: crypter.cpp:527
bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override
Definition: crypter.cpp:360
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) override
Add a key to the store.
Definition: crypter.cpp:299
CCrypter()
Definition: crypter.h:99
false
Definition: bls_dkg.cpp:168
boost::signals2::signal< void(CCryptoKeyStore *wallet)> NotifyStatusChanged
Wallet status (encrypted, locked) changed.
Definition: crypter.h:172
void memory_cleanse(void *ptr, size_t len)
Definition: cleanse.cpp:31
std::vector< unsigned char, secure_allocator< unsigned char > > vchKey
Definition: crypter.h:80
std::vector< unsigned char, secure_allocator< unsigned char > > vchIV
Definition: crypter.h:81
bool fKeySet
Definition: crypter.h:82
An encapsulated public key.
Definition: pubkey.h:30
virtual bool GetHDChain(CHDChain &hdChainRet) const override
Definition: crypter.cpp:539
bool EncryptHDChain(const CKeyingMaterial &vMasterKeyIn)
Definition: crypter.cpp:411
bool GetKey(const CKeyID &address, CKey &keyOut) const override
Definition: crypter.cpp:343
ADD_SERIALIZE_METHODS
Definition: crypter.h:47
CMasterKey()
Definition: crypter.h:58
friend class wallet_crypto::TestCrypter
Definition: crypter.h:78
const unsigned int WALLET_CRYPTO_SALT_SIZE
Definition: crypter.h:15
CryptedKeyMap mapCryptedKeys
Definition: crypter.h:148
void SerializationOp(Stream &s, Operation ser_action)
Definition: crypter.h:50
bool IsLocked(bool fForMixing=false) const
Definition: crypter.cpp:209
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:20
~CCrypter()
Definition: crypter.h:106
static int count
Definition: tests.c:45
std::vector< unsigned char > vchSalt
Definition: crypter.h:38
An encapsulated private key.
Definition: key.h:27
unsigned int nDeriveIterations
Definition: crypter.h:42
std::set< CKeyID > GetKeys() const override
Definition: crypter.cpp:376
bool fDecryptionThoroughlyChecked
keeps track of whether Unlock has run a thorough check before
Definition: crypter.h:131
Basic key store, that keeps keys in an address->secret map.
Definition: keystore.h:56
CHDChain cryptedHDChain
Definition: crypter.h:122
bool EncryptAES256(const SecureString &sKey, const SecureString &sPlaintext, const std::string &sIV, std::string &sCiphertext)
Definition: crypter.cpp:123
bool DecryptHDChain(CHDChain &hdChainRet) const
Definition: crypter.cpp:464
Released under the MIT license