Bitcoin Core  0.19.99
P2P Digital Currency
crypter.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2019 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 
13 const unsigned int WALLET_CRYPTO_KEY_SIZE = 32;
14 const unsigned int WALLET_CRYPTO_SALT_SIZE = 8;
15 const unsigned int WALLET_CRYPTO_IV_SIZE = 16;
16 
34 {
35 public:
36  std::vector<unsigned char> vchCryptedKey;
37  std::vector<unsigned char> vchSalt;
40  unsigned int nDerivationMethod;
41  unsigned int nDeriveIterations;
44  std::vector<unsigned char> vchOtherDerivationParameters;
45 
47 
48  template <typename Stream, typename Operation>
49  inline void SerializationOp(Stream& s, Operation ser_action) {
50  READWRITE(vchCryptedKey);
51  READWRITE(vchSalt);
52  READWRITE(nDerivationMethod);
53  READWRITE(nDeriveIterations);
54  READWRITE(vchOtherDerivationParameters);
55  }
56 
58  {
59  // 25000 rounds is just under 0.1 seconds on a 1.86 GHz Pentium M
60  // ie slightly lower than the lowest hardware we need bother supporting
61  nDeriveIterations = 25000;
62  nDerivationMethod = 0;
63  vchOtherDerivationParameters = std::vector<unsigned char>(0);
64  }
65 };
66 
67 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
68 
70 {
71  class TestCrypter;
72 }
73 
75 class CCrypter
76 {
77 friend class wallet_crypto_tests::TestCrypter; // for test access to chKey/chIV
78 private:
79  std::vector<unsigned char, secure_allocator<unsigned char>> vchKey;
80  std::vector<unsigned char, secure_allocator<unsigned char>> vchIV;
81  bool fKeySet;
82 
83  int BytesToKeySHA512AES(const std::vector<unsigned char>& chSalt, const SecureString& strKeyData, int count, unsigned char *key,unsigned char *iv) const;
84 
85 public:
86  bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod);
87  bool Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext) const;
88  bool Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext) const;
89  bool SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV);
90 
91  void CleanKey()
92  {
93  memory_cleanse(vchKey.data(), vchKey.size());
94  memory_cleanse(vchIV.data(), vchIV.size());
95  fKeySet = false;
96  }
97 
99  {
100  fKeySet = false;
101  vchKey.resize(WALLET_CRYPTO_KEY_SIZE);
102  vchIV.resize(WALLET_CRYPTO_IV_SIZE);
103  }
104 
106  {
107  CleanKey();
108  }
109 };
110 
111 bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext);
112 bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext);
113 bool DecryptKey(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCryptedSecret, const CPubKey& vchPubKey, CKey& key);
114 
115 #endif // BITCOIN_WALLET_CRYPTER_H
unsigned int nDerivationMethod
0 = EVP_sha512() 1 = scrypt()
Definition: crypter.h:40
const unsigned int WALLET_CRYPTO_KEY_SIZE
Definition: crypter.h:13
Encryption/decryption context with key information.
Definition: crypter.h:75
std::vector< unsigned char > vchCryptedKey
Definition: crypter.h:36
Private key encryption is done based on a CMasterKey, which holds a salt and random encryption key...
Definition: crypter.h:33
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:60
std::vector< unsigned char > vchOtherDerivationParameters
Use this for more parameters to key derivation, such as the various parameters to scrypt...
Definition: crypter.h:44
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
Definition: crypter.h:67
void CleanKey()
Definition: crypter.h:91
bool DecryptSecret(const CKeyingMaterial &vMasterKey, const std::vector< unsigned char > &vchCiphertext, const uint256 &nIV, CKeyingMaterial &vchPlaintext)
Definition: crypter.cpp:117
const unsigned int WALLET_CRYPTO_IV_SIZE
Definition: crypter.h:15
CCrypter()
Definition: crypter.h:98
void memory_cleanse(void *ptr, size_t len)
Secure overwrite a buffer (possibly containing secret data) with zero-bytes.
Definition: cleanse.cpp:14
std::vector< unsigned char, secure_allocator< unsigned char > > vchKey
Definition: crypter.h:79
std::vector< unsigned char, secure_allocator< unsigned char > > vchIV
Definition: crypter.h:80
bool fKeySet
Definition: crypter.h:81
An encapsulated public key.
Definition: pubkey.h:30
ADD_SERIALIZE_METHODS
Definition: crypter.h:46
CMasterKey()
Definition: crypter.h:57
256-bit opaque blob.
Definition: uint256.h:120
const unsigned int WALLET_CRYPTO_SALT_SIZE
Definition: crypter.h:14
void SerializationOp(Stream &s, Operation ser_action)
Definition: crypter.h:49
~CCrypter()
Definition: crypter.h:105
static int count
Definition: tests.c:45
std::vector< unsigned char > vchSalt
Definition: crypter.h:37
bool EncryptSecret(const CKeyingMaterial &vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256 &nIV, std::vector< unsigned char > &vchCiphertext)
Definition: crypter.cpp:107
An encapsulated private key.
Definition: key.h:27
bool DecryptKey(const CKeyingMaterial &vMasterKey, const std::vector< unsigned char > &vchCryptedSecret, const CPubKey &vchPubKey, CKey &key)
Definition: crypter.cpp:127
unsigned int nDeriveIterations
Definition: crypter.h:41
#define READWRITE(...)
Definition: serialize.h:186