Branch data Line data Source code
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 : :
5 : : #include <test/fuzz/FuzzedDataProvider.h>
6 : : #include <test/fuzz/fuzz.h>
7 : : #include <test/fuzz/util.h>
8 : : #include <test/util/setup_common.h>
9 : : #include <wallet/crypter.h>
10 : :
11 : : namespace wallet {
12 : : namespace {
13 : :
14 : : const TestingSetup* g_setup;
15 : 0 : void initialize_crypter()
16 : : {
17 [ # # # # ]: 0 : static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
18 : 0 : g_setup = testing_setup.get();
19 [ # # ]: 0 : }
20 : :
21 [ # # ]: 0 : FUZZ_TARGET(crypter, .init = initialize_crypter)
22 : : {
23 : 0 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
24 : 0 : bool good_data{true};
25 : :
26 : 0 : CCrypter crypt;
27 : : // These values are regularly updated within `CallOneOf`
28 : 0 : std::vector<unsigned char> cipher_text_ed;
29 : 0 : CKeyingMaterial plain_text_ed;
30 : 0 : const std::vector<unsigned char> random_key = ConsumeFixedLengthByteVector(fuzzed_data_provider, WALLET_CRYPTO_KEY_SIZE);
31 : :
32 [ # # ]: 0 : if (fuzzed_data_provider.ConsumeBool()) {
33 [ # # ]: 0 : const std::string random_string = fuzzed_data_provider.ConsumeRandomLengthString(100);
34 [ # # ]: 0 : SecureString secure_string(random_string.begin(), random_string.end());
35 : :
36 [ # # ]: 0 : const unsigned int derivation_method = fuzzed_data_provider.ConsumeBool() ? 0 : fuzzed_data_provider.ConsumeIntegral<unsigned int>();
37 : :
38 : : // Limiting the value of nRounds since it is otherwise uselessly expensive and causes a timeout when fuzzing.
39 [ # # ]: 0 : crypt.SetKeyFromPassphrase(/*strKeyData=*/secure_string,
40 : 0 : /*chSalt=*/ConsumeFixedLengthByteVector(fuzzed_data_provider, WALLET_CRYPTO_SALT_SIZE),
41 : : /*nRounds=*/fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 25000),
42 : : /*nDerivationMethod=*/derivation_method);
43 : 0 : }
44 : :
45 [ # # # # : 0 : LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 100)
# # ]
46 : : {
47 [ # # ]: 0 : CallOneOf(
48 : : fuzzed_data_provider,
49 : 0 : [&] {
50 : 0 : const std::vector<unsigned char> random_vector = ConsumeFixedLengthByteVector(fuzzed_data_provider, WALLET_CRYPTO_KEY_SIZE);
51 [ # # ]: 0 : plain_text_ed = CKeyingMaterial(random_vector.begin(), random_vector.end());
52 : 0 : },
53 : 0 : [&] {
54 : 0 : cipher_text_ed = ConsumeRandomLengthByteVector(fuzzed_data_provider, 64);
55 : 0 : },
56 : 0 : [&] {
57 : 0 : (void)crypt.Encrypt(plain_text_ed, cipher_text_ed);
58 : 0 : },
59 : 0 : [&] {
60 : 0 : (void)crypt.Decrypt(cipher_text_ed, plain_text_ed);
61 : 0 : },
62 : 0 : [&] {
63 : 0 : const CKeyingMaterial master_key(random_key.begin(), random_key.end());
64 : 0 : const uint256 iv = ConsumeUInt256(fuzzed_data_provider);
65 [ # # ]: 0 : (void)EncryptSecret(master_key, plain_text_ed, iv, cipher_text_ed);
66 : 0 : },
67 : 0 : [&] {
68 : 0 : const CKeyingMaterial master_key(random_key.begin(), random_key.end());
69 : 0 : const uint256 iv = ConsumeUInt256(fuzzed_data_provider);
70 [ # # ]: 0 : (void)DecryptSecret(master_key, cipher_text_ed, iv, plain_text_ed);
71 : 0 : },
72 : 0 : [&] {
73 : 0 : std::optional<CPubKey> random_pub_key = ConsumeDeserializable<CPubKey>(fuzzed_data_provider);
74 [ # # ]: 0 : if (!random_pub_key) {
75 : 0 : good_data = false;
76 : 0 : return;
77 : : }
78 : 0 : const CPubKey pub_key = *random_pub_key;
79 : 0 : const CKeyingMaterial master_key(random_key.begin(), random_key.end());
80 : 0 : const std::vector<unsigned char> crypted_secret = ConsumeRandomLengthByteVector(fuzzed_data_provider, 64);
81 : 0 : CKey key;
82 [ # # ]: 0 : (void)DecryptKey(master_key, crypted_secret, pub_key, key);
83 : 0 : });
84 : : }
85 : 0 : }
86 : : } // namespace
87 : : } // namespace wallet
|