Bitcoin ABC  0.22.13
P2P Digital Currency
crypter.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2016 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 
9 #include <serialize.h>
11 
12 const unsigned int WALLET_CRYPTO_KEY_SIZE = 32;
13 const unsigned int WALLET_CRYPTO_SALT_SIZE = 8;
14 const unsigned int WALLET_CRYPTO_IV_SIZE = 16;
15 
31 class CMasterKey {
32 public:
33  std::vector<uint8_t> vchCryptedKey;
34  std::vector<uint8_t> vchSalt;
37  unsigned int nDerivationMethod;
38  unsigned int nDeriveIterations;
41  std::vector<uint8_t> vchOtherDerivationParameters;
42 
44 
45  template <typename Stream, typename Operation>
46  inline void SerializationOp(Stream &s, Operation ser_action) {
47  READWRITE(vchCryptedKey);
48  READWRITE(vchSalt);
49  READWRITE(nDerivationMethod);
50  READWRITE(nDeriveIterations);
51  READWRITE(vchOtherDerivationParameters);
52  }
53 
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;
58  nDerivationMethod = 0;
59  vchOtherDerivationParameters = std::vector<uint8_t>(0);
60  }
61 };
62 
63 typedef std::vector<uint8_t, secure_allocator<uint8_t>> CKeyingMaterial;
64 
66 class TestCrypter;
67 }
68 
70 class CCrypter {
71  // for test access to chKey/chIV
72  friend class wallet_crypto_tests::TestCrypter;
73 
74 private:
75  std::vector<uint8_t, secure_allocator<uint8_t>> vchKey;
76  std::vector<uint8_t, secure_allocator<uint8_t>> vchIV;
77  bool fKeySet;
78 
79  int BytesToKeySHA512AES(const std::vector<uint8_t> &chSalt,
80  const SecureString &strKeyData, int count,
81  uint8_t *key, uint8_t *iv) const;
82 
83 public:
84  bool SetKeyFromPassphrase(const SecureString &strKeyData,
85  const std::vector<uint8_t> &chSalt,
86  const unsigned int nRounds,
87  const unsigned int nDerivationMethod);
88  bool Encrypt(const CKeyingMaterial &vchPlaintext,
89  std::vector<uint8_t> &vchCiphertext) const;
90  bool Decrypt(const std::vector<uint8_t> &vchCiphertext,
91  CKeyingMaterial &vchPlaintext) const;
92  bool SetKey(const CKeyingMaterial &chNewKey,
93  const std::vector<uint8_t> &chNewIV);
94 
95  void CleanKey() {
96  memory_cleanse(vchKey.data(), vchKey.size());
97  memory_cleanse(vchIV.data(), vchIV.size());
98  fKeySet = false;
99  }
100 
102  fKeySet = false;
103  vchKey.resize(WALLET_CRYPTO_KEY_SIZE);
104  vchIV.resize(WALLET_CRYPTO_IV_SIZE);
105  }
106 
107  ~CCrypter() { CleanKey(); }
108 };
109 
110 bool EncryptSecret(const CKeyingMaterial &vMasterKey,
111  const CKeyingMaterial &vchPlaintext, const uint256 &nIV,
112  std::vector<uint8_t> &vchCiphertext);
113 bool DecryptSecret(const CKeyingMaterial &vMasterKey,
114  const std::vector<uint8_t> &vchCiphertext,
115  const uint256 &nIV, CKeyingMaterial &vchPlaintext);
116 bool DecryptKey(const CKeyingMaterial &vMasterKey,
117  const std::vector<uint8_t> &vchCryptedSecret,
118  const CPubKey &vchPubKey, CKey &key);
119 
120 #endif // BITCOIN_WALLET_CRYPTER_H
unsigned int nDerivationMethod
0 = EVP_sha512() 1 = scrypt()
Definition: crypter.h:37
const unsigned int WALLET_CRYPTO_KEY_SIZE
Definition: crypter.h:12
Encryption/decryption context with key information.
Definition: crypter.h:70
std::vector< uint8_t, secure_allocator< uint8_t > > CKeyingMaterial
Definition: crypter.h:63
Private key encryption is done based on a CMasterKey, which holds a salt and random encryption key...
Definition: crypter.h:31
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:56
void CleanKey()
Definition: crypter.h:95
const unsigned int WALLET_CRYPTO_IV_SIZE
Definition: crypter.h:14
CCrypter()
Definition: crypter.h:101
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< uint8_t > vchOtherDerivationParameters
Use this for more parameters to key derivation, such as the various parameters to scrypt...
Definition: crypter.h:41
bool fKeySet
Definition: crypter.h:77
An encapsulated public key.
Definition: pubkey.h:31
ADD_SERIALIZE_METHODS
Definition: crypter.h:43
CMasterKey()
Definition: crypter.h:54
std::vector< uint8_t > vchCryptedKey
Definition: crypter.h:33
bool DecryptSecret(const CKeyingMaterial &vMasterKey, const std::vector< uint8_t > &vchCiphertext, const uint256 &nIV, CKeyingMaterial &vchPlaintext)
Definition: crypter.cpp:134
256-bit opaque blob.
Definition: uint256.h:120
const unsigned int WALLET_CRYPTO_SALT_SIZE
Definition: crypter.h:13
void SerializationOp(Stream &s, Operation ser_action)
Definition: crypter.h:46
bool EncryptSecret(const CKeyingMaterial &vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256 &nIV, std::vector< uint8_t > &vchCiphertext)
Definition: crypter.cpp:121
std::vector< uint8_t > vchSalt
Definition: crypter.h:34
~CCrypter()
Definition: crypter.h:107
static int count
Definition: tests.c:35
std::vector< uint8_t, secure_allocator< uint8_t > > vchKey
Definition: crypter.h:75
An encapsulated secp256k1 private key.
Definition: key.h:25
unsigned int nDeriveIterations
Definition: crypter.h:38
#define READWRITE(...)
Definition: serialize.h:191
bool DecryptKey(const CKeyingMaterial &vMasterKey, const std::vector< uint8_t > &vchCryptedSecret, const CPubKey &vchPubKey, CKey &key)
Definition: crypter.cpp:147
std::vector< uint8_t, secure_allocator< uint8_t > > vchIV
Definition: crypter.h:76