Bitcoin Core 31.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
crypter.cpp
Go to the documentation of this file.
1// Copyright (c) 2022-present 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
6#include <test/fuzz/fuzz.h>
7#include <test/fuzz/util.h>
9#include <wallet/crypter.h>
10
11namespace wallet {
12namespace {
13
14void initialize_crypter()
15{
16 static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
17}
18
19FUZZ_TARGET(crypter, .init = initialize_crypter)
20{
22 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
23 bool good_data{true};
24
25 CCrypter crypt;
26 // These values are regularly updated within `CallOneOf`
27 std::vector<unsigned char> cipher_text_ed;
28 CKeyingMaterial plain_text_ed;
29 const std::vector<unsigned char> random_key = ConsumeFixedLengthByteVector(fuzzed_data_provider, WALLET_CRYPTO_KEY_SIZE);
30
31 if (fuzzed_data_provider.ConsumeBool()) {
32 const std::string random_string = fuzzed_data_provider.ConsumeRandomLengthString(100);
33 SecureString secure_string(random_string.begin(), random_string.end());
34
35 const unsigned int derivation_method = fuzzed_data_provider.ConsumeBool() ? 0 : fuzzed_data_provider.ConsumeIntegral<unsigned int>();
36
37 // Limiting the value of rounds since it is otherwise uselessly expensive and causes a timeout when fuzzing.
38 crypt.SetKeyFromPassphrase(/*key_data=*/secure_string,
39 /*salt=*/ConsumeFixedLengthByteVector(fuzzed_data_provider, WALLET_CRYPTO_SALT_SIZE),
40 /*rounds=*/fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, CMasterKey::DEFAULT_DERIVE_ITERATIONS),
41 /*derivation_method=*/derivation_method);
42 }
43
44 CKey random_ckey;
45 random_ckey.Set(random_key.begin(), random_key.end(), /*fCompressedIn=*/fuzzed_data_provider.ConsumeBool());
46 if (!random_ckey.IsValid()) return;
47 CPubKey pubkey{random_ckey.GetPubKey()};
48
49 LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 100)
50 {
52 fuzzed_data_provider,
53 [&] {
54 const std::vector<unsigned char> random_vector = ConsumeFixedLengthByteVector(fuzzed_data_provider, WALLET_CRYPTO_KEY_SIZE);
55 plain_text_ed = CKeyingMaterial(random_vector.begin(), random_vector.end());
56 },
57 [&] {
58 cipher_text_ed = ConsumeRandomLengthByteVector(fuzzed_data_provider, 64);
59 },
60 [&] {
61 (void)crypt.Encrypt(plain_text_ed, cipher_text_ed);
62 },
63 [&] {
64 (void)crypt.Decrypt(cipher_text_ed, plain_text_ed);
65 },
66 [&] {
67 const CKeyingMaterial master_key(random_key.begin(), random_key.end());;
68 (void)EncryptSecret(master_key, plain_text_ed, pubkey.GetHash(), cipher_text_ed);
69 },
70 [&] {
71 std::optional<CPubKey> random_pub_key{ConsumeDeserializable<CPubKey>(fuzzed_data_provider)};
72 if (!random_pub_key) {
73 good_data = false;
74 return;
75 }
76 pubkey = *random_pub_key;
77 },
78 [&] {
79 const CKeyingMaterial master_key(random_key.begin(), random_key.end());
80 CKey key;
81 (void)DecryptKey(master_key, cipher_text_ed, pubkey, key);
82 });
83 }
84}
85} // namespace
86} // namespace wallet
bool IsValid() const
Check whether this private key is valid.
Definition key.h:124
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition key.cpp:183
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition key.h:104
uint256 GetHash() const
Get the 256-bit hash of this public key.
Definition pubkey.h:166
Encryption/decryption context with key information.
Definition crypter.h:72
static constexpr unsigned int DEFAULT_DERIVE_ITERATIONS
Default/minimum number of key derivation rounds.
Definition crypter.h:48
#define FUZZ_TARGET(...)
Definition fuzz.h:35
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition fuzz.h:22
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
Definition crypter.h:63
const unsigned int WALLET_CRYPTO_KEY_SIZE
Definition crypter.h:14
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:53
std::unique_ptr< T > MakeNoLogFileContext(const ChainType chain_type=ChainType::REGTEST, TestOpts opts={})
Make a test setup that has disk access to the debug.log file disabled.
std::vector< B > ConsumeFixedLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const size_t length) noexcept
Returns a byte vector of specified size regardless of the number of remaining bytes available from th...
Definition util.h:252
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition util.h:35
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition util.h:57
std::optional< T > ConsumeDeserializable(FuzzedDataProvider &fuzzed_data_provider, const P &params, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition util.h:100
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
Definition random.cpp:19
@ ZEROS
Seed with a compile time constant of zeros.
Definition random.h:19
std::string random_string(uint32_t length)