Skip to content

Commit 5b71c65

Browse files
committed
Fuzz test for BIP324 key derivation
1 parent 271b390 commit 5b71c65

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ test_fuzz_fuzz_SOURCES = \
220220
test/fuzz/banman.cpp \
221221
test/fuzz/base_encode_decode.cpp \
222222
test/fuzz/bech32.cpp \
223+
test/fuzz/bip324.cpp \
223224
test/fuzz/block.cpp \
224225
test/fuzz/block_header.cpp \
225226
test/fuzz/blockfilter.cpp \

src/test/fuzz/bip324.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) 2021 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 <chainparams.h>
6+
#include <crypto/bip324.h>
7+
#include <key.h>
8+
#include <test/fuzz/FuzzedDataProvider.h>
9+
#include <test/fuzz/fuzz.h>
10+
11+
void initialize_chainparams()
12+
{
13+
SelectParams(CBaseChainParams::REGTEST);
14+
}
15+
16+
FUZZ_TARGET_INIT(bip324, initialize_chainparams)
17+
{
18+
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
19+
20+
ECDHSecret ecdh_secret;
21+
ecdh_secret.resize(ECDH_SECRET_SIZE);
22+
auto ecdh_secret_bytes = fuzzed_data_provider.ConsumeBytes<uint8_t>(ECDH_SECRET_SIZE);
23+
ecdh_secret_bytes.resize(ECDH_SECRET_SIZE);
24+
25+
memcpy(ecdh_secret.data(), ecdh_secret_bytes.data(), ECDH_SECRET_SIZE);
26+
27+
auto initiator_hdata_len = fuzzed_data_provider.ConsumeIntegralInRange(0, 4096);
28+
auto initiator_hdata = fuzzed_data_provider.ConsumeBytes<uint8_t>(initiator_hdata_len);
29+
30+
auto responder_hdata_len = fuzzed_data_provider.ConsumeIntegralInRange(0, 4096);
31+
auto responder_hdata = fuzzed_data_provider.ConsumeBytes<uint8_t>(responder_hdata_len);
32+
33+
BIP324Keys keys;
34+
assert(derive_bip324_keys(std::move(ecdh_secret), initiator_hdata, responder_hdata, keys));
35+
assert(keys.initiator_F.size() == BIP324_KEY_LEN);
36+
assert(keys.initiator_V.size() == BIP324_KEY_LEN);
37+
assert(keys.responder_F.size() == BIP324_KEY_LEN);
38+
assert(keys.responder_V.size() == BIP324_KEY_LEN);
39+
assert(keys.session_id.size() == BIP324_KEY_LEN);
40+
assert("0000000000000000000000000000000000000000000000000000000000000000" == HexStr(ecdh_secret));
41+
}

0 commit comments

Comments
 (0)