Skip to content

Commit

Permalink
Fuzz test for BIP324 key derivation
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruv committed Nov 24, 2021
1 parent 271b390 commit 5b71c65
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ test_fuzz_fuzz_SOURCES = \
test/fuzz/banman.cpp \
test/fuzz/base_encode_decode.cpp \
test/fuzz/bech32.cpp \
test/fuzz/bip324.cpp \
test/fuzz/block.cpp \
test/fuzz/block_header.cpp \
test/fuzz/blockfilter.cpp \
Expand Down
41 changes: 41 additions & 0 deletions src/test/fuzz/bip324.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2021 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <chainparams.h>
#include <crypto/bip324.h>
#include <key.h>
#include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h>

void initialize_chainparams()
{
SelectParams(CBaseChainParams::REGTEST);
}

FUZZ_TARGET_INIT(bip324, initialize_chainparams)
{
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};

ECDHSecret ecdh_secret;
ecdh_secret.resize(ECDH_SECRET_SIZE);
auto ecdh_secret_bytes = fuzzed_data_provider.ConsumeBytes<uint8_t>(ECDH_SECRET_SIZE);
ecdh_secret_bytes.resize(ECDH_SECRET_SIZE);

memcpy(ecdh_secret.data(), ecdh_secret_bytes.data(), ECDH_SECRET_SIZE);

auto initiator_hdata_len = fuzzed_data_provider.ConsumeIntegralInRange(0, 4096);
auto initiator_hdata = fuzzed_data_provider.ConsumeBytes<uint8_t>(initiator_hdata_len);

auto responder_hdata_len = fuzzed_data_provider.ConsumeIntegralInRange(0, 4096);
auto responder_hdata = fuzzed_data_provider.ConsumeBytes<uint8_t>(responder_hdata_len);

BIP324Keys keys;
assert(derive_bip324_keys(std::move(ecdh_secret), initiator_hdata, responder_hdata, keys));
assert(keys.initiator_F.size() == BIP324_KEY_LEN);
assert(keys.initiator_V.size() == BIP324_KEY_LEN);
assert(keys.responder_F.size() == BIP324_KEY_LEN);
assert(keys.responder_V.size() == BIP324_KEY_LEN);
assert(keys.session_id.size() == BIP324_KEY_LEN);
assert("0000000000000000000000000000000000000000000000000000000000000000" == HexStr(ecdh_secret));
}

0 comments on commit 5b71c65

Please sign in to comment.