Branch data Line data Source code
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 : :
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 : 1 : void initialize_crypter()
15 : : {
16 [ + - + - : 1 : static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
+ - ]
17 : 1 : }
18 : :
19 [ + - ]: 1223 : FUZZ_TARGET(crypter, .init = initialize_crypter)
20 : : {
21 : 765 : SeedRandomStateForTest(SeedRand::ZEROS);
22 : 765 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
23 : 765 : bool good_data{true};
24 : :
25 : 765 : CCrypter crypt;
26 : : // These values are regularly updated within `CallOneOf`
27 : 765 : std::vector<unsigned char> cipher_text_ed;
28 : 765 : CKeyingMaterial plain_text_ed;
29 : 765 : const std::vector<unsigned char> random_key = ConsumeFixedLengthByteVector(fuzzed_data_provider, WALLET_CRYPTO_KEY_SIZE);
30 : :
31 [ + + ]: 765 : if (fuzzed_data_provider.ConsumeBool()) {
32 [ + - ]: 553 : const std::string random_string = fuzzed_data_provider.ConsumeRandomLengthString(100);
33 [ - + + - ]: 553 : SecureString secure_string(random_string.begin(), random_string.end());
34 : :
35 [ + + ]: 553 : 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 : 553 : crypt.SetKeyFromPassphrase(/*key_data=*/secure_string,
39 [ - + + - ]: 1106 : /*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 : 553 : }
43 : :
44 : 765 : CKey random_ckey;
45 [ + - ]: 765 : random_ckey.Set(random_key.begin(), random_key.end(), /*fCompressedIn=*/fuzzed_data_provider.ConsumeBool());
46 [ + + ]: 765 : if (!random_ckey.IsValid()) return;
47 [ + - ]: 762 : CPubKey pubkey{random_ckey.GetPubKey()};
48 : :
49 [ + + + + : 72464 : LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 100)
+ + ]
50 : : {
51 [ + - ]: 35500 : CallOneOf(
52 : : fuzzed_data_provider,
53 : 1319 : [&] {
54 : 1319 : const std::vector<unsigned char> random_vector = ConsumeFixedLengthByteVector(fuzzed_data_provider, WALLET_CRYPTO_KEY_SIZE);
55 [ + - ]: 1319 : plain_text_ed = CKeyingMaterial(random_vector.begin(), random_vector.end());
56 : 1319 : },
57 : 1737 : [&] {
58 : 1737 : cipher_text_ed = ConsumeRandomLengthByteVector(fuzzed_data_provider, 64);
59 : 1737 : },
60 : 1633 : [&] {
61 : 1633 : (void)crypt.Encrypt(plain_text_ed, cipher_text_ed);
62 : 1633 : },
63 : 4022 : [&] {
64 [ - + ]: 4022 : (void)crypt.Decrypt(cipher_text_ed, plain_text_ed);
65 : 4022 : },
66 : 4043 : [&] {
67 : 4043 : const CKeyingMaterial master_key(random_key.begin(), random_key.end());;
68 [ + - + - ]: 4043 : (void)EncryptSecret(master_key, plain_text_ed, pubkey.GetHash(), cipher_text_ed);
69 : 4043 : },
70 : 1237 : [&] {
71 : 1237 : std::optional<CPubKey> random_pub_key{ConsumeDeserializable<CPubKey>(fuzzed_data_provider)};
72 [ + + ]: 1237 : if (!random_pub_key) {
73 : 60 : good_data = false;
74 : 60 : return;
75 : : }
76 : 1177 : pubkey = *random_pub_key;
77 : : },
78 : 21509 : [&] {
79 : 21509 : const CKeyingMaterial master_key(random_key.begin(), random_key.end());
80 : 21509 : CKey key;
81 [ - + + - ]: 21509 : (void)DecryptKey(master_key, cipher_text_ed, pubkey, key);
82 : 21509 : });
83 : : }
84 : 765 : }
85 : : } // namespace
86 : : } // namespace wallet
|