Bitcoin Core 32.99.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
15{
16 static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
17}
18
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;
30
34
36
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,
41 /*derivation_method=*/derivation_method);
42 }
43
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
52 [&] {
55 },
56 [&] {
58 },
59 [&] {
61 },
62 [&] {
64 },
65 [&] {
66 const CKeyingMaterial master_key(random_key.begin(), random_key.end());
67 (void)EncryptSecret(master_key, plain_text_ed, pubkey.GetHash(), cipher_text_ed);
68 },
69 [&] {
71 if (!random_pub_key) {
72 good_data = false;
73 return;
74 }
75 pubkey = *random_pub_key;
76 },
77 [&] {
78 const CKeyingMaterial master_key(random_key.begin(), random_key.end());
79 CKey key;
80 (void)DecryptKey(master_key, cipher_text_ed, pubkey, key);
81 });
82 }
83}
84} // namespace
85} // namespace wallet
An encapsulated private key.
Definition key.h:46
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition key.h:114
An encapsulated public key.
Definition pubkey.h:43
std::string ConsumeRandomLengthString(size_t max_length)
T ConsumeIntegralInRange(T min, T max)
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
Definition basic.cpp:11
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
Definition crypter.h:63
bool DecryptKey(const CKeyingMaterial &master_key, const std::span< const unsigned char > crypted_secret, const CPubKey &pub_key, CKey &key)
Definition crypter.cpp:132
bool EncryptSecret(const CKeyingMaterial &vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256 &nIV, std::vector< unsigned char > &vchCiphertext)
Definition crypter.cpp:111
constexpr unsigned int WALLET_CRYPTO_KEY_SIZE
Definition crypter.h:14
constexpr unsigned int WALLET_CRYPTO_SALT_SIZE
Definition crypter.h:15
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition secure.h:53
SeedRandomStateForTest(SeedRand::ZEROS)
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:280
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition util.h:37
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition util.h:63
@ ZEROS
Seed with a compile time constant of zeros.
std::string random_string(uint32_t length)
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82
FuzzedDataProvider & fuzzed_data_provider
Definition fees.cpp:46