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