Bitcoin Core 28.99.0
P2P Digital Currency
crypter.cpp
Go to the documentation of this file.
1// Copyright (c) 2022 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
14const TestingSetup* g_setup;
15void initialize_crypter()
16{
17 static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
18 g_setup = testing_setup.get();
19}
20
21FUZZ_TARGET(crypter, .init = initialize_crypter)
22{
23 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
24 bool good_data{true};
25
26 CCrypter crypt;
27 // These values are regularly updated within `CallOneOf`
28 std::vector<unsigned char> cipher_text_ed;
29 CKeyingMaterial plain_text_ed;
30 const std::vector<unsigned char> random_key = ConsumeFixedLengthByteVector(fuzzed_data_provider, WALLET_CRYPTO_KEY_SIZE);
31
32 if (fuzzed_data_provider.ConsumeBool()) {
33 const std::string random_string = fuzzed_data_provider.ConsumeRandomLengthString(100);
34 SecureString secure_string(random_string.begin(), random_string.end());
35
36 const unsigned int derivation_method = fuzzed_data_provider.ConsumeBool() ? 0 : fuzzed_data_provider.ConsumeIntegral<unsigned int>();
37
38 // Limiting the value of rounds since it is otherwise uselessly expensive and causes a timeout when fuzzing.
39 crypt.SetKeyFromPassphrase(/*key_data=*/secure_string,
40 /*salt=*/ConsumeFixedLengthByteVector(fuzzed_data_provider, WALLET_CRYPTO_SALT_SIZE),
41 /*rounds=*/fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 25000),
42 /*derivation_method=*/derivation_method);
43 }
44
45 LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 100)
46 {
48 fuzzed_data_provider,
49 [&] {
50 const std::vector<unsigned char> random_vector = ConsumeFixedLengthByteVector(fuzzed_data_provider, WALLET_CRYPTO_KEY_SIZE);
51 plain_text_ed = CKeyingMaterial(random_vector.begin(), random_vector.end());
52 },
53 [&] {
54 cipher_text_ed = ConsumeRandomLengthByteVector(fuzzed_data_provider, 64);
55 },
56 [&] {
57 (void)crypt.Encrypt(plain_text_ed, cipher_text_ed);
58 },
59 [&] {
60 (void)crypt.Decrypt(cipher_text_ed, plain_text_ed);
61 },
62 [&] {
63 const CKeyingMaterial master_key(random_key.begin(), random_key.end());
64 const uint256 iv = ConsumeUInt256(fuzzed_data_provider);
65 (void)EncryptSecret(master_key, plain_text_ed, iv, cipher_text_ed);
66 },
67 [&] {
68 const CKeyingMaterial master_key(random_key.begin(), random_key.end());
69 const uint256 iv = ConsumeUInt256(fuzzed_data_provider);
70 (void)DecryptSecret(master_key, cipher_text_ed, iv, plain_text_ed);
71 },
72 [&] {
73 std::optional<CPubKey> random_pub_key = ConsumeDeserializable<CPubKey>(fuzzed_data_provider);
74 if (!random_pub_key) {
75 good_data = false;
76 return;
77 }
78 const CPubKey pub_key = *random_pub_key;
79 const CKeyingMaterial master_key(random_key.begin(), random_key.end());
80 const std::vector<unsigned char> crypted_secret = ConsumeRandomLengthByteVector(fuzzed_data_provider, 64);
81 CKey key;
82 (void)DecryptKey(master_key, crypted_secret, pub_key, key);
83 });
84 }
85}
86} // namespace
87} // namespace wallet
An encapsulated private key.
Definition: key.h:35
An encapsulated public key.
Definition: pubkey.h:34
256-bit opaque blob.
Definition: uint256.h:190
#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: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
FUZZ_TARGET(coin_grinder)
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
Testing setup that configures a complete environment.
Definition: setup_common.h:120
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:256
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:171
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