PIVX Core  5.6.99
P2P Digital Currency
crypter.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2014 The Bitcoin developers
2 // Copyright (c) 2017-2021 The PIVX 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 PIVX_CRYPTER_H
7 #define PIVX_CRYPTER_H
8 
9 #include "keystore.h"
10 #include "serialize.h"
11 #include "streams.h"
13 
14 class uint256;
15 
16 #include <atomic>
17 
18 const unsigned int WALLET_CRYPTO_KEY_SIZE = 32;
19 const unsigned int WALLET_CRYPTO_SALT_SIZE = 8;
20 const unsigned int WALLET_CRYPTO_IV_SIZE = 16;
21 
39 {
40 public:
41  std::vector<unsigned char> vchCryptedKey;
42  std::vector<unsigned char> vchSalt;
45  unsigned int nDerivationMethod;
46  unsigned int nDeriveIterations;
49  std::vector<unsigned char> vchOtherDerivationParameters;
50 
51  SERIALIZE_METHODS(CMasterKey, obj) { READWRITE(obj.vchCryptedKey, obj.vchSalt, obj.nDerivationMethod, obj.nDeriveIterations, obj.vchOtherDerivationParameters); }
52 
54  {
55  // 25000 rounds is just under 0.1 seconds on a 1.86 GHz Pentium M
56  // ie slightly lower than the lowest hardware we need bother supporting
57  nDeriveIterations = 25000;
59  vchOtherDerivationParameters = std::vector<unsigned char>(0);
60  }
61 };
62 
63 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
64 
65 namespace wallet_crypto
66 {
67  class TestCrypter;
68 }
69 
70 class CSecureDataStream : public CBaseDataStream<CKeyingMaterial>
71 {
72 public:
73  explicit CSecureDataStream(int nTypeIn, int nVersionIn) : CBaseDataStream(nTypeIn, nVersionIn) { }
74 
75  CSecureDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn) :
76  CBaseDataStream(pbegin, pend, nTypeIn, nVersionIn) { }
77 
78  CSecureDataStream(const vector_type& vchIn, int nTypeIn, int nVersionIn) :
79  CBaseDataStream(vchIn, nTypeIn, nVersionIn) { }
80 };
81 
83 class CCrypter
84 {
85 friend class wallet_crypto::TestCrypter; // for test access to chKey/chIV
86 private:
87  std::vector<unsigned char, secure_allocator<unsigned char>> vchKey;
88  std::vector<unsigned char, secure_allocator<unsigned char>> vchIV;
89  bool fKeySet;
90 
91  int BytesToKeySHA512AES(const std::vector<unsigned char>& chSalt, const SecureString& strKeyData, int count, unsigned char *key,unsigned char *iv) const;
92 
93 public:
94  bool SetKeyFromPassphrase(const SecureString& strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod);
95  bool Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char>& vchCiphertext) const;
96  bool Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext) const;
97  bool SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV);
98 
99  void CleanKey()
100  {
101  memory_cleanse(vchKey.data(), vchKey.size());
102  memory_cleanse(vchIV.data(), vchIV.size());
103  fKeySet = false;
104  }
105 
107  {
108  fKeySet = false;
111  }
112 
114  {
115  CleanKey();
116  }
117 };
118 
119 bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial& vchPlaintext, const uint256& nIV, std::vector<unsigned char>& vchCiphertext);
120 bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext);
121 
122 
127 {
128 private:
131  std::atomic<bool> fUseCrypto;
132 
133 protected:
134  // TODO: In the future, move this variable to the wallet class directly following upstream's structure.
136  // Sapling
138 
139  bool SetCrypted();
140 
142  bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
143 
145 
146  static bool DecryptKey(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCryptedSecret, const CPubKey& vchPubKey, CKey& key);
147 
148  // Unlock Sapling keys
149  bool UnlockSaplingKeys(const CKeyingMaterial& vMasterKeyIn, bool fDecryptionThoroughlyChecked);
150 
151 public:
153 
154  bool IsCrypted() const { return fUseCrypto; }
155  bool IsLocked() const;
156 
157  virtual bool AddCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret);
158  bool AddKeyPubKey(const CKey& key, const CPubKey& pubkey) override;
159  bool HaveKey(const CKeyID& address) const override;
160 
161  bool GetKey(const CKeyID& address, CKey& keyOut) const override;
162  bool GetPubKey(const CKeyID& address, CPubKey& vchPubKeyOut) const override;
163  std::set<CKeyID> GetKeys() const override;
164 
167  const std::vector<unsigned char>& vchCryptedSecret);
168  bool HaveSaplingSpendingKey(const libzcash::SaplingExtendedFullViewingKey& extfvk) const override;
170 
175  boost::signals2::signal<void(CCryptoKeyStore* wallet)> NotifyStatusChanged;
176 };
177 
178 #endif // PIVX_CRYPTER_H
false
Definition: bls_dkg.cpp:151
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:76
vector_type::const_iterator const_iterator
Definition: streams.h:92
Basic key store, that keeps keys in an address->secret map.
Definition: keystore.h:99
Encryption/decryption context with key information.
Definition: crypter.h:84
bool Encrypt(const CKeyingMaterial &vchPlaintext, std::vector< unsigned char > &vchCiphertext) const
Definition: crypter.cpp:71
bool SetKey(const CKeyingMaterial &chNewKey, const std::vector< unsigned char > &chNewIV)
Definition: crypter.cpp:59
friend class wallet_crypto::TestCrypter
Definition: crypter.h:85
bool fKeySet
Definition: crypter.h:89
CCrypter()
Definition: crypter.h:106
bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector< unsigned char > &chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
Definition: crypter.cpp:40
bool Decrypt(const std::vector< unsigned char > &vchCiphertext, CKeyingMaterial &vchPlaintext) const
Definition: crypter.cpp:88
int BytesToKeySHA512AES(const std::vector< unsigned char > &chSalt, const SecureString &strKeyData, int count, unsigned char *key, unsigned char *iv) const
Definition: crypter.cpp:14
void CleanKey()
Definition: crypter.h:99
std::vector< unsigned char, secure_allocator< unsigned char > > vchIV
Definition: crypter.h:88
std::vector< unsigned char, secure_allocator< unsigned char > > vchKey
Definition: crypter.h:87
~CCrypter()
Definition: crypter.h:113
Keystore which keeps the private keys encrypted.
Definition: crypter.h:127
bool GetKey(const CKeyID &address, CKey &keyOut) const override
Definition: crypter.cpp:199
std::atomic< bool > fUseCrypto
if fUseCrypto is true, mapKeys and mapSaplingSpendingKeys must be empty if fUseCrypto is false,...
Definition: crypter.h:131
static bool DecryptKey(const CKeyingMaterial &vMasterKey, const std::vector< unsigned char > &vchCryptedSecret, const CPubKey &vchPubKey, CKey &key)
Definition: crypter.cpp:124
CryptedSaplingSpendingKeyMap mapCryptedSaplingSpendingKeys
Definition: crypter.h:137
bool HaveSaplingSpendingKey(const libzcash::SaplingExtendedFullViewingKey &extfvk) const override
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) override
Add a key to the store.
Definition: crypter.cpp:156
bool IsLocked() const
Definition: crypter.cpp:148
bool HaveKey(const CKeyID &address) const override
Check whether a key corresponding to a given address is present in the store.
Definition: crypter.cpp:179
virtual bool AddCryptedSaplingSpendingKey(const libzcash::SaplingExtendedFullViewingKey &extfvk, const std::vector< unsigned char > &vchCryptedSecret)
Sapling.
bool UnlockSaplingKeys(const CKeyingMaterial &vMasterKeyIn, bool fDecryptionThoroughlyChecked)
bool EncryptKeys(CKeyingMaterial &vMasterKeyIn)
will encrypt previously unencrypted keys
Definition: crypter.cpp:244
virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret)
Definition: crypter.cpp:188
bool GetSaplingSpendingKey(const libzcash::SaplingExtendedFullViewingKey &extfvk, libzcash::SaplingExtendedSpendingKey &skOut) const override
CKeyingMaterial vMasterKey
Definition: crypter.h:135
bool IsCrypted() const
Definition: crypter.h:154
CryptedKeyMap mapCryptedKeys
Definition: crypter.h:144
std::set< CKeyID > GetKeys() const override
Definition: crypter.cpp:231
boost::signals2::signal< void(CCryptoKeyStore *wallet)> NotifyStatusChanged
Wallet status (encrypted, locked) changed.
Definition: crypter.h:175
bool SetCrypted()
Definition: crypter.cpp:137
bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override
Definition: crypter.cpp:215
An encapsulated private key.
Definition: key.h:30
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:21
Private key encryption is done based on a CMasterKey, which holds a salt and random encryption key.
Definition: crypter.h:39
std::vector< unsigned char > vchSalt
Definition: crypter.h:42
SERIALIZE_METHODS(CMasterKey, obj)
Definition: crypter.h:51
CMasterKey()
Definition: crypter.h:53
unsigned int nDerivationMethod
0 = EVP_sha512() 1 = scrypt()
Definition: crypter.h:45
std::vector< unsigned char > vchCryptedKey
Definition: crypter.h:41
std::vector< unsigned char > vchOtherDerivationParameters
Use this for more parameters to key derivation, such as the various parameters to scrypt.
Definition: crypter.h:49
unsigned int nDeriveIterations
Definition: crypter.h:46
An encapsulated public key.
Definition: pubkey.h:44
CSecureDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn)
Definition: crypter.h:75
CSecureDataStream(int nTypeIn, int nVersionIn)
Definition: crypter.h:73
CSecureDataStream(const vector_type &vchIn, int nTypeIn, int nVersionIn)
Definition: crypter.h:78
256-bit opaque blob.
Definition: uint256.h:138
void memory_cleanse(void *ptr, size_t len)
Definition: cleanse.cpp:27
bool EncryptSecret(const CKeyingMaterial &vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256 &nIV, std::vector< unsigned char > &vchCiphertext)
Definition: crypter.cpp:104
const unsigned int WALLET_CRYPTO_IV_SIZE
Definition: crypter.h:20
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
Definition: crypter.h:63
const unsigned int WALLET_CRYPTO_SALT_SIZE
Definition: crypter.h:19
const unsigned int WALLET_CRYPTO_KEY_SIZE
Definition: crypter.h:18
bool DecryptSecret(const CKeyingMaterial &vMasterKey, const std::vector< unsigned char > &vchCiphertext, const uint256 &nIV, CKeyingMaterial &vchPlaintext)
Definition: crypter.cpp:114
std::map< libzcash::SaplingExtendedFullViewingKey, std::vector< unsigned char > > CryptedSaplingSpendingKeyMap
Sapling.
Definition: keystore.h:157
std::map< CKeyID, std::pair< CPubKey, std::vector< unsigned char > > > CryptedKeyMap
Definition: keystore.h:155
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:61
#define READWRITE(...)
Definition: serialize.h:183