Skip to content

Commit d15d95c

Browse files
committed
Merge bitcoin/bitcoin#30575: fuzz: fix timeout in crypter target
bfd3c29 fuzz: fix timeout in crypter target (brunoerg) Pull request description: Fixes #30503 - Move SetKeyFromPassphrase to out of LIMITED_WHILE - Remove `SetKey` calls since it is already called internally by other functions. - Reduce number of iterations (100 is enough, no need for 10,000). ACKs for top commit: maflcko: review ACK bfd3c29 📆 dergoegge: utACK bfd3c29 Tree-SHA512: 275ab7d07a20bfd07279a23613678993c10c166f40cdc900213b9f4d5afb107462d5f88518a0f4ce2a52f3b7950ff2c01cf74292042f16996909fcb96f827d3e
2 parents 1afa3c8 + bfd3c29 commit d15d95c

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

src/wallet/test/fuzz/crypter.cpp

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,23 @@ FUZZ_TARGET(crypter, .init = initialize_crypter)
2929
CKeyingMaterial plain_text_ed;
3030
const std::vector<unsigned char> random_key = ConsumeFixedLengthByteVector(fuzzed_data_provider, WALLET_CRYPTO_KEY_SIZE);
3131

32-
LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 10000)
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 nRounds since it is otherwise uselessly expensive and causes a timeout when fuzzing.
39+
crypt.SetKeyFromPassphrase(/*strKeyData=*/secure_string,
40+
/*chSalt=*/ConsumeFixedLengthByteVector(fuzzed_data_provider, WALLET_CRYPTO_SALT_SIZE),
41+
/*nRounds=*/fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 25000),
42+
/*nDerivationMethod=*/derivation_method);
43+
}
44+
45+
LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 100)
3346
{
3447
CallOneOf(
3548
fuzzed_data_provider,
36-
[&] {
37-
const std::string random_string = fuzzed_data_provider.ConsumeRandomLengthString(100);
38-
SecureString secure_string(random_string.begin(), random_string.end());
39-
40-
const unsigned int derivation_method = fuzzed_data_provider.ConsumeBool() ? 0 : fuzzed_data_provider.ConsumeIntegral<unsigned int>();
41-
42-
// Limiting the value of nRounds since it is otherwise uselessly expensive and causes a timeout when fuzzing.
43-
crypt.SetKeyFromPassphrase(/*strKeyData=*/secure_string,
44-
/*chSalt=*/ConsumeFixedLengthByteVector(fuzzed_data_provider, WALLET_CRYPTO_SALT_SIZE),
45-
/*nRounds=*/fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 25000),
46-
/*nDerivationMethod=*/derivation_method);
47-
},
48-
[&] {
49-
const std::vector<unsigned char> random_vector = ConsumeFixedLengthByteVector(fuzzed_data_provider, WALLET_CRYPTO_KEY_SIZE);
50-
const CKeyingMaterial new_key(random_vector.begin(), random_vector.end());
51-
const std::vector<unsigned char>& new_IV = ConsumeFixedLengthByteVector(fuzzed_data_provider, WALLET_CRYPTO_IV_SIZE);
52-
crypt.SetKey(new_key, new_IV);
53-
},
5449
[&] {
5550
const std::vector<unsigned char> random_vector = ConsumeFixedLengthByteVector(fuzzed_data_provider, WALLET_CRYPTO_KEY_SIZE);
5651
plain_text_ed = CKeyingMaterial(random_vector.begin(), random_vector.end());
@@ -67,12 +62,12 @@ FUZZ_TARGET(crypter, .init = initialize_crypter)
6762
[&] {
6863
const CKeyingMaterial master_key(random_key.begin(), random_key.end());
6964
const uint256 iv = ConsumeUInt256(fuzzed_data_provider);
70-
EncryptSecret(master_key, plain_text_ed, iv, cipher_text_ed);
65+
(void)EncryptSecret(master_key, plain_text_ed, iv, cipher_text_ed);
7166
},
7267
[&] {
7368
const CKeyingMaterial master_key(random_key.begin(), random_key.end());
7469
const uint256 iv = ConsumeUInt256(fuzzed_data_provider);
75-
DecryptSecret(master_key, cipher_text_ed, iv, plain_text_ed);
70+
(void)DecryptSecret(master_key, cipher_text_ed, iv, plain_text_ed);
7671
},
7772
[&] {
7873
std::optional<CPubKey> random_pub_key = ConsumeDeserializable<CPubKey>(fuzzed_data_provider);
@@ -84,7 +79,7 @@ FUZZ_TARGET(crypter, .init = initialize_crypter)
8479
const CKeyingMaterial master_key(random_key.begin(), random_key.end());
8580
const std::vector<unsigned char> crypted_secret = ConsumeRandomLengthByteVector(fuzzed_data_provider, 64);
8681
CKey key;
87-
DecryptKey(master_key, crypted_secret, pub_key, key);
82+
(void)DecryptKey(master_key, crypted_secret, pub_key, key);
8883
});
8984
}
9085
}

0 commit comments

Comments
 (0)