|
| 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