Bitcoin
crypter.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2018 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 <serialize.h>
10 #include <script/signingprovider.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 
71 {
72  class TestCrypter;
73 }
74 
76 class CCrypter
77 {
78 friend class wallet_crypto_tests::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 EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext);
113 bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext);
114 bool DecryptKey(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCryptedSecret, const CPubKey& vchPubKey, CKey& key);
115 
116 #endif // BITCOIN_WALLET_CRYPTER_H
std::vector< unsigned char > vchSalt
Definition: crypter.h:38
std::vector< unsigned char, secure_allocator< unsigned char > > vchKey
Definition: crypter.h:80
ADD_SERIALIZE_METHODS
Definition: crypter.h:47
bool Encrypt(const CKeyingMaterial &vchPlaintext, std::vector< unsigned char > &vchCiphertext) const
Definition: crypter.cpp:74
std::vector< unsigned char > vchOtherDerivationParameters
Definition: crypter.h:45
const unsigned int WALLET_CRYPTO_KEY_SIZE
Definition: crypter.h:14
Definition: crypter.h:70
~CCrypter()
Definition: crypter.h:106
CMasterKey()
Definition: crypter.h:58
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:60
void CleanKey()
Definition: crypter.h:92
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
Definition: crypter.h:68
bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector< unsigned char > &chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
Definition: crypter.cpp:42
bool DecryptSecret(const CKeyingMaterial &vMasterKey, const std::vector< unsigned char > &vchCiphertext, const uint256 &nIV, CKeyingMaterial &vchPlaintext)
Definition: crypter.cpp:120
bool fKeySet
Definition: crypter.h:82
const unsigned int WALLET_CRYPTO_IV_SIZE
Definition: crypter.h:16
Definition: crypter.h:34
std::vector< unsigned char, secure_allocator< unsigned char > > vchIV
Definition: crypter.h:81
int BytesToKeySHA512AES(const std::vector< unsigned char > &chSalt, const SecureString &strKeyData, int count, unsigned char *key, unsigned char *iv) const
Definition: crypter.cpp:16
CCrypter()
Definition: crypter.h:99
void memory_cleanse(void *ptr, size_t len)
Definition: cleanse.cpp:14
Definition: crypter.h:76
unsigned int nDeriveIterations
Definition: crypter.h:42
bool SetKey(const CKeyingMaterial &chNewKey, const std::vector< unsigned char > &chNewIV)
Definition: crypter.cpp:62
std::vector< unsigned char > vchCryptedKey
Definition: crypter.h:37
friend class wallet_crypto_tests::TestCrypter
Definition: crypter.h:78
Definition: uint256.h:121
const unsigned int WALLET_CRYPTO_SALT_SIZE
Definition: crypter.h:15
unsigned int nDerivationMethod
Definition: crypter.h:41
static int count
Definition: tests.c:45
Definition: pubkey.h:30
bool Decrypt(const std::vector< unsigned char > &vchCiphertext, CKeyingMaterial &vchPlaintext) const
Definition: crypter.cpp:92
bool EncryptSecret(const CKeyingMaterial &vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256 &nIV, std::vector< unsigned char > &vchCiphertext)
Definition: crypter.cpp:110
void SerializationOp(Stream &s, Operation ser_action)
Definition: crypter.h:50
bool DecryptKey(const CKeyingMaterial &vMasterKey, const std::vector< unsigned char > &vchCryptedSecret, const CPubKey &vchPubKey, CKey &key)
Definition: crypter.cpp:130
#define READWRITE(...)
Definition: serialize.h:184
Definition: key.h:27
key
Definition: extract_strings_qt.py:80