Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2022-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.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>11namespace wallet {12namespace {14void initialize_crypter()15{16static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();17}19FUZZ_TARGET(crypter, .init = initialize_crypter)20{21SeedRandomStateForTest(SeedRand::ZEROS);22FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());23bool good_data{true};25CCrypter crypt;26// These values are regularly updated within `CallOneOf`27std::vector<unsigned char> cipher_text_ed;28CKeyingMaterial plain_text_ed;29const std::vector<unsigned char> random_key = ConsumeFixedLengthByteVector(fuzzed_data_provider, WALLET_CRYPTO_KEY_SIZE);31if (fuzzed_data_provider.ConsumeBool()) {32const std::string random_string = fuzzed_data_provider.ConsumeRandomLengthString(100);33SecureString secure_string(random_string.begin(), random_string.end());35const unsigned int derivation_method = fuzzed_data_provider.ConsumeBool() ? 0 : fuzzed_data_provider.ConsumeIntegral<unsigned int>();37// Limiting the value of rounds since it is otherwise uselessly expensive and causes a timeout when fuzzing.38(void)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}44CKey random_ckey;45random_ckey.Set(random_key.begin(), random_key.end(), /*fCompressedIn=*/fuzzed_data_provider.ConsumeBool());46if (!random_ckey.IsValid()) return;47CPubKey pubkey{random_ckey.GetPubKey()};49LIMITED_WHILE (good_data && fuzzed_data_provider.ConsumeBool(), 100) {50CallOneOf(51fuzzed_data_provider,52[&] {53const std::vector<unsigned char> random_vector = ConsumeFixedLengthByteVector(fuzzed_data_provider, WALLET_CRYPTO_KEY_SIZE);54plain_text_ed = CKeyingMaterial(random_vector.begin(), random_vector.end());55},56[&] {57cipher_text_ed = ConsumeRandomLengthByteVector(fuzzed_data_provider, 64);58},59[&] {60(void)crypt.Encrypt(plain_text_ed, cipher_text_ed);61},62[&] {63(void)crypt.Decrypt(cipher_text_ed, plain_text_ed);64},65[&] {66const CKeyingMaterial master_key(random_key.begin(), random_key.end());67(void)EncryptSecret(master_key, plain_text_ed, pubkey.GetHash(), cipher_text_ed);68},69[&] {70std::optional<CPubKey> random_pub_key{ConsumeDeserializable<CPubKey>(fuzzed_data_provider)};71if (!random_pub_key) {72good_data = false;73return;74}75pubkey = *random_pub_key;76},77[&] {78const CKeyingMaterial master_key(random_key.begin(), random_key.end());79CKey key;80(void)DecryptKey(master_key, cipher_text_ed, pubkey, key);81});82}83}84} // namespace85} // namespace wallet
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.