Bitcoin Core 28.99.0
P2P Digital Currency
crypter.h
Go to the documentation of this file.
1// Copyright (c) 2009-2021 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>
11
12
13namespace wallet {
14const unsigned int WALLET_CRYPTO_KEY_SIZE = 32;
15const unsigned int WALLET_CRYPTO_SALT_SIZE = 8;
16const unsigned int WALLET_CRYPTO_IV_SIZE = 16;
17
35{
36public:
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 READWRITE(obj.vchCryptedKey, obj.vchSalt, obj.nDerivationMethod, obj.nDeriveIterations, obj.vchOtherDerivationParameters);
50 }
51
53 {
54 // 25000 rounds is just under 0.1 seconds on a 1.86 GHz Pentium M
55 // ie slightly lower than the lowest hardware we need bother supporting
56 nDeriveIterations = 25000;
58 vchOtherDerivationParameters = std::vector<unsigned char>(0);
59 }
60};
61
62typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
63
64namespace wallet_crypto_tests
65{
66 class TestCrypter;
67}
68
71{
72friend class wallet_crypto_tests::TestCrypter; // for test access to chKey/chIV
73private:
74 std::vector<unsigned char, secure_allocator<unsigned char>> vchKey;
75 std::vector<unsigned char, secure_allocator<unsigned char>> vchIV;
76 bool fKeySet;
77
78 int BytesToKeySHA512AES(std::span<const unsigned char> salt, const SecureString& key_data, int count, unsigned char* key, unsigned char* iv) const;
79
80public:
81 bool SetKeyFromPassphrase(const SecureString& key_data, std::span<const unsigned char> salt, const unsigned int rounds, const unsigned int derivation_method);
82 bool Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext) const;
83 bool Decrypt(std::span<const unsigned char> ciphertext, CKeyingMaterial& plaintext) const;
84 bool SetKey(const CKeyingMaterial& new_key, std::span<const unsigned char> new_iv);
85
86 void CleanKey()
87 {
88 memory_cleanse(vchKey.data(), vchKey.size());
89 memory_cleanse(vchIV.data(), vchIV.size());
90 fKeySet = false;
91 }
92
94 {
95 fKeySet = false;
98 }
99
101 {
102 CleanKey();
103 }
104};
105
106bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext);
107bool DecryptSecret(const CKeyingMaterial& master_key, std::span<const unsigned char> ciphertext, const uint256& iv, CKeyingMaterial& plaintext);
108bool DecryptKey(const CKeyingMaterial& master_key, std::span<const unsigned char> crypted_secret, const CPubKey& pub_key, CKey& key);
109} // namespace wallet
110
111#endif // BITCOIN_WALLET_CRYPTER_H
An encapsulated private key.
Definition: key.h:35
An encapsulated public key.
Definition: pubkey.h:34
256-bit opaque blob.
Definition: uint256.h:190
Encryption/decryption context with key information.
Definition: crypter.h:71
friend class wallet_crypto_tests::TestCrypter
Definition: crypter.h:72
bool Decrypt(std::span< const unsigned char > ciphertext, CKeyingMaterial &plaintext) const
Definition: crypter.cpp:94
int BytesToKeySHA512AES(std::span< const unsigned char > salt, const SecureString &key_data, int count, unsigned char *key, unsigned char *iv) const
Definition: crypter.cpp:15
std::vector< unsigned char, secure_allocator< unsigned char > > vchKey
Definition: crypter.h:74
bool SetKeyFromPassphrase(const SecureString &key_data, std::span< const unsigned char > salt, const unsigned int rounds, const unsigned int derivation_method)
Definition: crypter.cpp:41
bool SetKey(const CKeyingMaterial &new_key, std::span< const unsigned char > new_iv)
Definition: crypter.cpp:63
bool Encrypt(const CKeyingMaterial &vchPlaintext, std::vector< unsigned char > &vchCiphertext) const
Definition: crypter.cpp:76
std::vector< unsigned char, secure_allocator< unsigned char > > vchIV
Definition: crypter.h:75
void CleanKey()
Definition: crypter.h:86
Private key encryption is done based on a CMasterKey, which holds a salt and random encryption key.
Definition: crypter.h:35
std::vector< unsigned char > vchSalt
Definition: crypter.h:38
unsigned int nDerivationMethod
0 = EVP_sha512() 1 = scrypt()
Definition: crypter.h:41
std::vector< unsigned char > vchCryptedKey
Definition: crypter.h:37
unsigned int nDeriveIterations
Definition: crypter.h:42
std::vector< unsigned char > vchOtherDerivationParameters
Use this for more parameters to key derivation, such as the various parameters to scrypt.
Definition: crypter.h:45
SERIALIZE_METHODS(CMasterKey, obj)
Definition: crypter.h:47
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 > > CKeyingMaterial
Definition: crypter.h:62
const unsigned int WALLET_CRYPTO_KEY_SIZE
Definition: crypter.h:14
bool DecryptSecret(const CKeyingMaterial &master_key, const std::span< const unsigned char > ciphertext, const uint256 &iv, CKeyingMaterial &plaintext)
Definition: crypter.cpp:121
const unsigned int WALLET_CRYPTO_IV_SIZE
Definition: crypter.h:16
bool DecryptKey(const CKeyingMaterial &master_key, const std::span< const unsigned char > crypted_secret, const CPubKey &pub_key, CKey &key)
Definition: crypter.cpp:132
const unsigned int WALLET_CRYPTO_SALT_SIZE
Definition: crypter.h:15
bool EncryptSecret(const CKeyingMaterial &vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256 &nIV, std::vector< unsigned char > &vchCiphertext)
Definition: crypter.cpp:111
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:58
#define READWRITE(...)
Definition: serialize.h:156
static int count