From 1989900302cbc76f94f614e382f9ee97161364d7 Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Thu, 26 Nov 2026 11:08:08 -0500 Subject: [PATCH 1/9] Count server channels from accept. --- src/sessions/session_server.cpp | 10 ++++------ test/sessions/session_server.cpp | 34 +++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/sessions/session_server.cpp b/src/sessions/session_server.cpp index 1204dc427..1c1a56df0 100644 --- a/src/sessions/session_server.cpp +++ b/src/sessions/session_server.cpp @@ -253,6 +253,9 @@ void session_server::handle_accepted(const code& ec, const socket::ptr& socket, // Creates channel_xxxx cast as channel::ptr. const auto channel = create_channel(socket); + BC_ASSERT(!is_zero(add1(channel_count_))); + channel_count_ = ceilinged_add(channel_count_, one); + LOGS("Accepted " << (secure ? "private " : "clear ") << name_ << " connection [" << channel->endpoint() << "] on binding [" << acceptor->local() << "]."); @@ -306,9 +309,6 @@ void session_server::handle_channel_start(const code& LOG_ONLY(ec), BC_ASSERT(stranded()); LOGS("Inbound " << name_ << " channel start [" << channel->endpoint() << "] " << ec.message()); - - BC_ASSERT(!is_zero(add1(channel_count_))); - channel_count_ = ceilinged_add(channel_count_, one); } void session_server::handle_channel_stop(const code& LOG_ONLY(ec), @@ -318,9 +318,7 @@ void session_server::handle_channel_stop(const code& LOG_ONLY(ec), LOGS("Inbound " << name_ << " channel stop [" << channel->endpoint() << "] " << ec.message()); - // Start/stop race with handle_channel_start can leave channel_count_ zero - // here before first increment. - ////BC_ASSERT(!is_zero(channel_count_)); + BC_ASSERT(!is_zero(channel_count_)); channel_count_ = floored_subtract(channel_count_, one); } diff --git a/test/sessions/session_server.cpp b/test/sessions/session_server.cpp index a3d511c98..cd3791307 100644 --- a/test/sessions/session_server.cpp +++ b/test/sessions/session_server.cpp @@ -299,7 +299,7 @@ class mock_server if (protocoled_.exchange(true)) return; - // Queued behind the channel start handler, which counts the channel. + // Queued behind the channel start handler. io::post(channel->strand(), [this]() NOEXCEPT { io::post(strand(), [this]() NOEXCEPT @@ -356,6 +356,18 @@ class mock_server_disabled } }; +class mock_server_pending + : public mock_server +{ +public: + using mock_server::mock_server; + + void do_attach_handshake(const channel::ptr&, + const result_handler&) NOEXCEPT override + { + } +}; + template static code start_server(net& instance, const std::shared_ptr& session) { @@ -419,6 +431,26 @@ BC_AUTO_TEST_CASE(session_server__accept__oversubscribed__second_client_dropped) instance.close(); } +BC_AUTO_TEST_CASE(session_server__accept__pending_handshake__second_client_dropped) +{ + const logger log{}; + settings set(test::network_settings()); + net instance(set, log); + const auto session = std::make_shared(instance, 1, one_connection); + BC_REQUIRE_EQUAL(start_server(instance, session), error::success); + + io::context io{}; + io::socket first{ io }; + REQUIRE_SUCCESS(first, connect, test::loopback(session->port())); + + io::socket second{ io }; + REQUIRE_SUCCESS(second, connect, test::loopback(session->port())); + BC_REQUIRE(dropped(second)); + + stop(instance, session); + instance.close(); +} + BC_AUTO_TEST_CASE(session_server__accept__disabled__client_dropped) { const logger log{}; From f26930996cb318fc219b8d82df66e5855475a7db Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Thu, 26 Nov 2026 11:57:20 -0500 Subject: [PATCH 2/9] Add sv2 Noise fixed-key vector tests. --- builds/gnu/Makefile.am | 1 + .../libbitcoin-network-test.vcxproj | 2 + .../libbitcoin-network-test.vcxproj.filters | 25 +- test/noise/vectors.cpp | 230 ++++++++++++++++++ test/noise/vectors.hpp | 102 ++++++++ 5 files changed, 352 insertions(+), 8 deletions(-) create mode 100644 test/noise/vectors.cpp create mode 100644 test/noise/vectors.hpp diff --git a/builds/gnu/Makefile.am b/builds/gnu/Makefile.am index e66ddeac5..a5263cd46 100644 --- a/builds/gnu/Makefile.am +++ b/builds/gnu/Makefile.am @@ -884,6 +884,7 @@ test_libbitcoin_network_test_SOURCES = \ ${srcdir}/../../test/net/hosts.cpp \ ${srcdir}/../../test/net/proxy.cpp \ ${srcdir}/../../test/net/socket.cpp \ + ${srcdir}/../../test/noise/vectors.cpp \ ${srcdir}/../../test/p2ps/cipher.cpp \ ${srcdir}/../../test/p2ps/stream.cpp \ ${srcdir}/../../test/protocols/protocol.cpp \ diff --git a/builds/msvc/vs2026/libbitcoin-network-test/libbitcoin-network-test.vcxproj b/builds/msvc/vs2026/libbitcoin-network-test/libbitcoin-network-test.vcxproj index c97fcbe2b..4b26d8b71 100644 --- a/builds/msvc/vs2026/libbitcoin-network-test/libbitcoin-network-test.vcxproj +++ b/builds/msvc/vs2026/libbitcoin-network-test/libbitcoin-network-test.vcxproj @@ -272,6 +272,7 @@ $(IntDir)test_net_socket.obj + $(IntDir)test_p2ps_cipher.obj @@ -340,6 +341,7 @@ + diff --git a/builds/msvc/vs2026/libbitcoin-network-test/libbitcoin-network-test.vcxproj.filters b/builds/msvc/vs2026/libbitcoin-network-test/libbitcoin-network-test.vcxproj.filters index 5def735d6..01402fb00 100644 --- a/builds/msvc/vs2026/libbitcoin-network-test/libbitcoin-network-test.vcxproj.filters +++ b/builds/msvc/vs2026/libbitcoin-network-test/libbitcoin-network-test.vcxproj.filters @@ -64,30 +64,33 @@ {D28FC18A-8B81-4247-0000-000000000003} - + {D28FC18A-8B81-4247-0000-000000000004} - + {D28FC18A-8B81-4247-0000-000000000005} - + {D28FC18A-8B81-4247-0000-000000000006} - + {D28FC18A-8B81-4247-0000-000000000007} - + {D28FC18A-8B81-4247-0000-000000000008} - + {D28FC18A-8B81-4247-0000-000000000009} - + {D28FC18A-8B81-4247-0000-000000000010} - + {D28FC18A-8B81-4247-0000-0000000000A1} + + {D28FC18A-8B81-4247-0000-0000000000B1} + @@ -528,6 +531,9 @@ src\net + + src\noise + src\p2ps @@ -680,6 +686,9 @@ src\functional + + src\noise + src diff --git a/test/noise/vectors.cpp b/test/noise/vectors.cpp new file mode 100644 index 000000000..312492743 --- /dev/null +++ b/test/noise/vectors.cpp @@ -0,0 +1,230 @@ +/** + * Copyright (c) 2011-2026 libbitcoin developers + * + * This file is part of libbitcoin. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#include "../test.hpp" +#include "vectors.hpp" + +BC_AUTO_TEST_SUITE(noise_vectors_tests) + +using namespace bc::system; + +constexpr size_t chunk_size = 65519; + +template +static data_array part(const data_slice& data, size_t offset) +{ + data_array out{}; + std::copy_n(std::next(data.begin(), offset), Size, out.begin()); + return out; +} + +static hash_digest mix_hash(const hash_digest& hash, const data_slice& data) +{ + return sha256_hash(splice(hash, data)); +} + +static hash_digest ecdh(const ec_secret& secret, const ec_ellswift& initiator, + const ec_ellswift& responder, bool responding) +{ + hash_digest out{}; + ellswift::exchange(out, secret, initiator, responder, responding); + return out; +} + +static ec_xonly xonly(const ec_ellswift& key) +{ + ec_compressed point{}; + ellswift::decode(point, key); + return part(point, one); +} + +static void seal(chacha20_poly1305& cipher, uint64_t& nonce, + const data_chunk& plain, data_chunk& out) +{ + data_chunk sealed(plain.size() + chacha20_poly1305::expansion); + cipher.encrypt(plain, {}, 0, nonce++, sealed); + out.insert(out.end(), sealed.begin(), sealed.end()); +} + +static hash_digest frame_digest(chacha20_poly1305& cipher, uint64_t& nonce, + const sv2_noise::frame& frame) +{ + const auto extension = to_little_endian(frame.extension); + const auto length = to_little_endian(frame.size); + const data_chunk header + { + extension[0], extension[1], frame.type, length[0], length[1], length[2] + }; + + data_chunk payload(frame.size); + std::iota(payload.begin(), payload.end(), uint8_t{}); + + data_chunk out{}; + seal(cipher, nonce, header, out); + for (size_t start = 0; start < payload.size(); start += chunk_size) + { + const auto end = std::min(payload.size(), start + chunk_size); + seal(cipher, nonce, { std::next(payload.begin(), start), + std::next(payload.begin(), end) }, out); + } + + return sha256_hash(out); +} + +static std::vector digests(const hash_digest& key, + const std::vector& frames) +{ + uint64_t nonce{}; + chacha20_poly1305 cipher{ key }; + std::vector out{}; + for (const auto& frame: frames) + out.push_back(frame_digest(cipher, nonce, frame)); + + return out; +} + +static std::vector expected( + const std::vector& frames) +{ + std::vector out{}; + for (const auto& frame: frames) + out.push_back(frame.digest); + + return out; +} + +BC_AUTO_TEST_CASE(noise_vectors__sha256__protocol_name__initialized) +{ + const std::string name{ sv2_noise::protocol_name }; + const auto ck = sha256_hash(to_chunk(name)); + const auto h = sha256_hash(ck); + BC_REQUIRE_EQUAL(ck, sv2_noise::initialized_ck); + BC_REQUIRE_EQUAL(h, sv2_noise::initialized_h); +} + +BC_AUTO_TEST_CASE(noise_vectors__mix_hash__message1__message1_h) +{ + const auto h = mix_hash(mix_hash(sv2_noise::initialized_h, sv2_noise::initiator_ellswift), {}); + BC_REQUIRE_EQUAL(sv2_noise::message1, sv2_noise::initiator_ellswift); + BC_REQUIRE_EQUAL(h, sv2_noise::message1_h); + BC_REQUIRE_EQUAL(sv2_noise::message1_ck, sv2_noise::initialized_ck); +} + +BC_AUTO_TEST_CASE(noise_vectors__ellswift_decode__keys__public_keys) +{ + ec_compressed initiator{}; + ec_compressed responder{}; + ec_compressed server{}; + const auto initiator_public = secret_to_public(initiator, sv2_noise::initiator_secret); + const auto responder_public = secret_to_public(responder, sv2_noise::responder_secret); + const auto server_public = secret_to_public(server, sv2_noise::static_secret); + BC_REQUIRE(initiator_public); + BC_REQUIRE(responder_public); + BC_REQUIRE(server_public); + BC_REQUIRE_EQUAL(xonly(sv2_noise::initiator_ellswift), part(initiator, one)); + BC_REQUIRE_EQUAL(xonly(sv2_noise::responder_ellswift), part(responder, one)); + BC_REQUIRE_EQUAL(xonly(sv2_noise::static_ellswift), part(server, one)); +} + +BC_AUTO_TEST_CASE(noise_vectors__exchange__ephemeral__ee_ck) +{ + const auto initiator = ecdh(sv2_noise::initiator_secret, sv2_noise::initiator_ellswift, sv2_noise::responder_ellswift, false); + const auto responder = ecdh(sv2_noise::responder_secret, sv2_noise::initiator_ellswift, sv2_noise::responder_ellswift, true); + const auto keys = hkdf::key<64>(initiator, sv2_noise::message1_ck, {}); + const auto h = mix_hash(sv2_noise::message1_h, sv2_noise::responder_ellswift); + BC_REQUIRE_EQUAL(initiator, responder); + BC_REQUIRE_EQUAL(part(keys, zero), sv2_noise::ee_ck); + BC_REQUIRE_EQUAL(h, sv2_noise::ee_h); +} + +BC_AUTO_TEST_CASE(noise_vectors__exchange__static__es_ck) +{ + const auto initiator = ecdh(sv2_noise::initiator_secret, sv2_noise::initiator_ellswift, sv2_noise::static_ellswift, false); + const auto responder = ecdh(sv2_noise::static_secret, sv2_noise::initiator_ellswift, sv2_noise::static_ellswift, true); + const auto keys = hkdf::key<64>(initiator, sv2_noise::ee_ck, {}); + const auto h = mix_hash(sv2_noise::ee_h, part<80>(sv2_noise::message2, 64)); + BC_REQUIRE_EQUAL(initiator, responder); + BC_REQUIRE_EQUAL(part(keys, zero), sv2_noise::es_ck); + BC_REQUIRE_EQUAL(h, sv2_noise::es_h); +} + +BC_AUTO_TEST_CASE(noise_vectors__decrypt__message2__static_key_and_certificate) +{ + const auto ee = ecdh(sv2_noise::initiator_secret, sv2_noise::initiator_ellswift, sv2_noise::responder_ellswift, false); + const auto es = ecdh(sv2_noise::initiator_secret, sv2_noise::initiator_ellswift, sv2_noise::static_ellswift, false); + chacha20_poly1305 first{ part(hkdf::key<64>(ee, sv2_noise::message1_ck, {}), hash_size) }; + chacha20_poly1305 second{ part(hkdf::key<64>(es, sv2_noise::ee_ck, {}), hash_size) }; + ec_ellswift server{}; + data_array<74> certificate{}; + const auto opened_server = first.decrypt(server, sv2_noise::ee_h, 0, 0, part<80>(sv2_noise::message2, 64)); + const auto opened_certificate = second.decrypt(certificate, sv2_noise::es_h, 0, 0, part<90>(sv2_noise::message2, 144)); + const auto h = mix_hash(sv2_noise::es_h, part<90>(sv2_noise::message2, 144)); + BC_REQUIRE_EQUAL(part<64>(sv2_noise::message2, zero), sv2_noise::responder_ellswift); + BC_REQUIRE(opened_server); + BC_REQUIRE_EQUAL(server, sv2_noise::static_ellswift); + BC_REQUIRE(opened_certificate); + BC_REQUIRE_EQUAL(certificate, sv2_noise::signature_noise_message); + BC_REQUIRE_EQUAL(h, sv2_noise::message2_h); +} + +BC_AUTO_TEST_CASE(noise_vectors__schnorr_sign__certificate__signature_noise_message) +{ + const auto header = splice(to_little_endian(sv2_noise::version), to_little_endian(sv2_noise::valid_from), to_little_endian(sv2_noise::not_valid_after)); + const auto hash = sha256_hash(splice(header, xonly(sv2_noise::static_ellswift))); + ec_signature signature{}; + const auto created = schnorr::sign(signature, sv2_noise::authority_secret, hash, sv2_noise::certificate_aux); + const auto verified = schnorr::verify_signature(sv2_noise::authority_xonly, hash, signature); + BC_REQUIRE_EQUAL(header, part<10>(sv2_noise::signature_noise_message, zero)); + BC_REQUIRE(created); + BC_REQUIRE(verified); + BC_REQUIRE_EQUAL(signature, part<64>(sv2_noise::signature_noise_message, 10)); +} + +BC_AUTO_TEST_CASE(noise_vectors__hkdf__split__transport_keys) +{ + const auto keys = hkdf::key<64>({}, sv2_noise::message2_ck, {}); + BC_REQUIRE_EQUAL(sv2_noise::message2_ck, sv2_noise::es_ck); + BC_REQUIRE_EQUAL(part(keys, zero), sv2_noise::initiator_key); + BC_REQUIRE_EQUAL(part(keys, hash_size), sv2_noise::responder_key); +} + +BC_AUTO_TEST_CASE(noise_vectors__encrypt__initiator_frames__digests) +{ + const auto actual = digests(sv2_noise::initiator_key, sv2_noise::initiator_frames); + BC_REQUIRE_EQUAL(actual, expected(sv2_noise::initiator_frames)); +} + +BC_AUTO_TEST_CASE(noise_vectors__encrypt__responder_frames__digests) +{ + const auto actual = digests(sv2_noise::responder_key, sv2_noise::responder_frames); + BC_REQUIRE_EQUAL(actual, expected(sv2_noise::responder_frames)); +} + +BC_AUTO_TEST_CASE(noise_vectors__base58_check__authority_xonly__authority_key) +{ + const auto key = encode_base58(insert_checksum<38>({ base16_array("0100"), sv2_noise::authority_xonly })); + BC_REQUIRE_EQUAL(key, sv2_noise::authority_key); +} + +BC_AUTO_TEST_CASE(noise_vectors__base58_check__specification_key__specification_encoding) +{ + const auto key = encode_base58(insert_checksum<38>({ base16_array("0100"), base16_array("76637000979c1c11af0c300bcd8c7fe48610fce9b9c11e3daee35ae0b08a7455") })); + BC_REQUIRE_EQUAL(key, "9bXiEd8boQVhq7WddEcERUL5tyyJVFYdU8th3HfbNXK3Yw6GRXh"); +} + +BC_AUTO_TEST_SUITE_END() diff --git a/test/noise/vectors.hpp b/test/noise/vectors.hpp new file mode 100644 index 000000000..2df68974a --- /dev/null +++ b/test/noise/vectors.hpp @@ -0,0 +1,102 @@ +/** + * Copyright (c) 2011-2026 libbitcoin developers + * + * This file is part of libbitcoin. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#ifndef LIBBITCOIN_NETWORK_TEST_NOISE_VECTORS_HPP +#define LIBBITCOIN_NETWORK_TEST_NOISE_VECTORS_HPP + +#include "../test.hpp" + +// sv2 Noise NX (sv2-spec 4), from a reference over the bip324 and bip340 +// reference code, reproduced byte for byte by SRI noise_sv2 (main 67e7bd7). + +namespace sv2_noise { + +constexpr auto protocol_name = "Noise_NX_Secp256k1+EllSwift_ChaChaPoly_SHA256"; +constexpr auto authority_key = "9br3pDEyhsQoXmojoXVRsNGBa8xNw1F35npYzXM5yWDfZaxjaEo"; + +constexpr auto authority_secret = system::base16_array("8db036afbf4962033bf40ab365acbff06ff97557748ec0bfcc73bdaa9f93c10f"); +constexpr auto authority_xonly = system::base16_array("a006b0296b471e3234b428d9382de575654ae0c5f6b41056bd2332b07327b048"); +constexpr auto static_secret = system::base16_array("b6695a1ef24a25ac673a41b04290a363338c059d173d6ed198d21428261127e6"); +constexpr auto static_ellswift = system::base16_array("b064e778df21f39c7c3f1ce01cfa2e7bba72d3f9af40e8bab6a753c7c3e719d2caaf80c8b2c2be72fef69a0282db0e77c62384b3eaf70d46953c6769c5f92044"); +constexpr auto initiator_secret = system::base16_array("f33e3ec708f381ba59b55185e0e5c5efebebf5250b9ea94118d8ae2f6543fb08"); +constexpr auto initiator_ellswift = system::base16_array("5b19ef8a73dbd8cdefe0b3698b1ecde96697b2b2de6fc50bd5c3265e414bc615837f247b9ecd4d4c5530964706aa43bb73b2ddfc022c5729a522f5d312ee8436"); +constexpr auto responder_secret = system::base16_array("ab3a3da4bff749c2d32a8f90ad816c92774a9a9010cd809cc9d155747049b45e"); +constexpr auto responder_ellswift = system::base16_array("56a621c640bbb918afe365c689a974f5dbbd63268279f02b7cc8c6e7518e282d6ee596534a476cc385978c4a9f5a0a19eff5fe45cc4a0f24e67f691b5e0d07e2"); +constexpr auto certificate_aux = system::base16_array("55ecfd017c7b5ddc8cb19c6af2891e9f56206be5e704e49e05acc9bd0407279f"); +constexpr uint16_t version = 0; +constexpr uint32_t valid_from = 1700000000; +constexpr uint32_t not_valid_after = 1900000000; + +// Handshake hash (h) and chaining key (ck) after each step. +constexpr auto initialized_h = system::base16_array("922fa32e4f487c0d59caa3bed7899ce3d98db7e13dbd3b7cf2d23dd433dc6104"); +constexpr auto initialized_ck = system::base16_array("2eb47881208e9eee1f669f67c66ee70ea9ea88090d503fe830dc4bc83e29bf10"); +constexpr auto message1_h = system::base16_array("0ea0bb64b88d73ef60a48565f31874a325ad2c8b6690e6c1cf7cf5f2e9fe4b58"); +constexpr auto message1_ck = system::base16_array("2eb47881208e9eee1f669f67c66ee70ea9ea88090d503fe830dc4bc83e29bf10"); +constexpr auto ee_h = system::base16_array("08a52fca186fdd8a2d731e525c5fe167ff957f918cb1fe5d71be5da4fc282ba5"); +constexpr auto ee_ck = system::base16_array("da941453451c92a7b10de9f41f14f595d0736c429443776e7f5e1000678f9cdc"); +constexpr auto es_h = system::base16_array("918fb389415e90a92a119a02c55c1cfd92cf8a7a61d577d12a5dbbec2a0742c5"); +constexpr auto es_ck = system::base16_array("2f31a1c5fd1c1847fff458a91e48c3a6533486803527e6ff4fd3c5fa3619e9af"); +constexpr auto message2_h = system::base16_array("d530ad16caf7f362591e521c1706e7bf6cb5106b6b8eaac20af78a667cebc0e0"); +constexpr auto message2_ck = system::base16_array("2f31a1c5fd1c1847fff458a91e48c3a6533486803527e6ff4fd3c5fa3619e9af"); + +constexpr auto signature_noise_message = system::base16_array("000000f1536500b33f71f5c52e0c1de2080074aa5bba625860617bdd23bb549936165f70f8ae6011417e81750af780796072ceff32a40d3c20a641408de3de9963c8d9f9c9206a108817"); +constexpr auto message1 = system::base16_array("5b19ef8a73dbd8cdefe0b3698b1ecde96697b2b2de6fc50bd5c3265e414bc615837f247b9ecd4d4c5530964706aa43bb73b2ddfc022c5729a522f5d312ee8436"); +constexpr auto message2 = system::base16_array("56a621c640bbb918afe365c689a974f5dbbd63268279f02b7cc8c6e7518e282d6ee596534a476cc385978c4a9f5a0a19eff5fe45cc4a0f24e67f691b5e0d07e2bb880058852e92ad144116e557202a3f00049f71d64ed75ad5671387eaec057d71102a540a6b3067db06879349e3425c47e4abd35a787296ba02a456756750ae562a04e58e8a632088f563fdb33bcaf34f4ab700e421b5a33caa5e1d8ada6182abfa48db676fea706988927b2d0d402da7228831c204deeccead0faf8ae549464183691fe233b5642b92b3dc30b69189d280a28a3c52d78e8942a11680972c00dc4ca642d8fe9de21853"); +// Transport keys, initiator to responder and responder to initiator. +constexpr auto initiator_key = system::base16_array("be02c290834bd33ed36d8db82dc3c76d62de47d2248724584c7ab285bfe9a922"); +constexpr auto responder_key = system::base16_array("956d735e56fee69cb7ea5030c5b9073da0cd6f2b683c7275e3ff1482168bac15"); + +// Byte i of a payload is i mod 256. Each direction is a sequence of frames +// with nonces counting from zero, the digest the sha256 of the frame. +struct frame +{ + uint16_t extension; + uint8_t type; + uint32_t size; + system::hash_digest digest; +}; + +const std::vector initiator_frames +{ + { 0x0000, 0x1f, 0, system::base16_array("19c5fcc3a15cae3991af0fd0bb571dd5184c49e476fbba68772f1809ddb01bee") }, + { 0x8000, 0x20, 1, system::base16_array("f914330f6d90ce48bf3f067739ccd52ad11d2b2383981cf05539de025bb879c0") }, + { 0x0000, 0x21, 6, system::base16_array("9394b4e16f83e85b37857a3a472165f6ab52c26b2f451999ba8a978d98a43d96") }, + { 0x8000, 0x22, 65518, system::base16_array("0d3fa09370bd9788b67c54f65a1f539b583fbf443c80aaa65b3d8a715c2b1f1b") }, + { 0x0000, 0x23, 65519, system::base16_array("9763ed79afaa015328646811ec8d90d947866022523bea6026f1f0246c19c7b7") }, + { 0x8000, 0x24, 65520, system::base16_array("2a98a0095338779aa26b77300dd2c99a43c617fdac21817c11e7127fd5cdada4") }, + { 0x0000, 0x25, 131038, system::base16_array("09839606c59e6550574c4a4ce90d776d2f6f7776bbfd86b39cbe4a52292d60a5") }, + { 0x8000, 0x26, 131039, system::base16_array("ae4155e87974a5d12e23558721b4603498f8ade5298d1ab31085cfa5447d3428") }, + { 0x0000, 0x27, 200000, system::base16_array("c8309a14b1d86dfcf732aa7760c65fa5fab916c491afead20b5d6ee8061259d5") }, +}; + +const std::vector responder_frames +{ + { 0x0000, 0x1f, 0, system::base16_array("e04768ca15b045bce6f5fd6e35fe3e3c8a87cbf634a658ca77463bb90a19f216") }, + { 0x8000, 0x20, 1, system::base16_array("0a548afc0545932e94437fa8b2b6eb65cfc33d8eaa51b533b42fe81a26fe8ed2") }, + { 0x0000, 0x21, 6, system::base16_array("0d5e7a7483ad5c3708c8990ee1a48c23348f6f2f3250f631f57f700d5bf1c52e") }, + { 0x8000, 0x22, 65518, system::base16_array("eb36eeac27d3d4c80bd8a53fee181b1ce8432f1d755d690c0b86a047c608273d") }, + { 0x0000, 0x23, 65519, system::base16_array("4e32c2d37a1f62c881bcea7bace906756fca166c05794dbd5a289380fe312f47") }, + { 0x8000, 0x24, 65520, system::base16_array("f094d87c1636910b569c9f95db8a41f2d8afa4301b8fb0f860ba0b23cbc97d6f") }, + { 0x0000, 0x25, 131038, system::base16_array("d7329d81b0bc66c4c25d244ad92672183d32df928582418ad575379250c52e08") }, + { 0x8000, 0x26, 131039, system::base16_array("694a8b27b0e88ecb9cdc95f20f6b1d349964b7cadce9a4c13d6965833f4f66f8") }, + { 0x0000, 0x27, 200000, system::base16_array("5faf43101d69d392eba2c6f764bbaaa9522190d58e694ac53d4bb3d2e55fc891") }, +}; + +} // namespace sv2_noise + +#endif From a286d01c62a1a8034cef77aedf71667ef9c714e3 Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Thu, 26 Nov 2026 12:05:41 -0500 Subject: [PATCH 3/9] Add sv2 Noise NX handshake and transport cipher. --- builds/gnu/Makefile.am | 9 + .../libbitcoin-network-test.vcxproj | 3 + .../libbitcoin-network-test.vcxproj.filters | 3 + .../libbitcoin-network.vcxproj | 5 + .../libbitcoin-network.vcxproj.filters | 75 +++-- include/bitcoin/network.hpp | 2 + include/bitcoin/network/noise/cipher.hpp | 158 ++++++++++ include/bitcoin/network/noise/noise.hpp | 24 ++ src/noise/cipher.cpp | 298 ++++++++++++++++++ test/noise/cipher.cpp | 281 +++++++++++++++++ 10 files changed, 828 insertions(+), 30 deletions(-) create mode 100644 include/bitcoin/network/noise/cipher.hpp create mode 100644 include/bitcoin/network/noise/noise.hpp create mode 100644 src/noise/cipher.cpp create mode 100644 test/noise/cipher.cpp diff --git a/builds/gnu/Makefile.am b/builds/gnu/Makefile.am index a5263cd46..28cd82d3b 100644 --- a/builds/gnu/Makefile.am +++ b/builds/gnu/Makefile.am @@ -187,6 +187,7 @@ src_libbitcoin_network_la_SOURCES = \ ${srcdir}/../../src/net/socket_tcp.cpp \ ${srcdir}/../../src/net/socket_ws.cpp \ ${srcdir}/../../src/net/socket_zmtp.cpp \ + ${srcdir}/../../src/noise/cipher.cpp \ ${srcdir}/../../src/p2ps/cipher.cpp \ ${srcdir}/../../src/p2ps/stream.cpp \ ${srcdir}/../../src/protocols/protocol.cpp \ @@ -622,6 +623,13 @@ include_bitcoin_network_net_HEADERS = \ ${srcdir}/../../include/bitcoin/network/net/proxy.hpp \ ${srcdir}/../../include/bitcoin/network/net/socket.hpp +include_bitcoin_network_noisedir = \ + ${includedir}/bitcoin/network/noise + +include_bitcoin_network_noise_HEADERS = \ + ${srcdir}/../../include/bitcoin/network/noise/cipher.hpp \ + ${srcdir}/../../include/bitcoin/network/noise/noise.hpp + include_bitcoin_network_p2psdir = \ ${includedir}/bitcoin/network/p2ps @@ -884,6 +892,7 @@ test_libbitcoin_network_test_SOURCES = \ ${srcdir}/../../test/net/hosts.cpp \ ${srcdir}/../../test/net/proxy.cpp \ ${srcdir}/../../test/net/socket.cpp \ + ${srcdir}/../../test/noise/cipher.cpp \ ${srcdir}/../../test/noise/vectors.cpp \ ${srcdir}/../../test/p2ps/cipher.cpp \ ${srcdir}/../../test/p2ps/stream.cpp \ diff --git a/builds/msvc/vs2026/libbitcoin-network-test/libbitcoin-network-test.vcxproj b/builds/msvc/vs2026/libbitcoin-network-test/libbitcoin-network-test.vcxproj index 4b26d8b71..f2a390cd9 100644 --- a/builds/msvc/vs2026/libbitcoin-network-test/libbitcoin-network-test.vcxproj +++ b/builds/msvc/vs2026/libbitcoin-network-test/libbitcoin-network-test.vcxproj @@ -272,6 +272,9 @@ $(IntDir)test_net_socket.obj + + $(IntDir)test_noise_cipher.obj + $(IntDir)test_p2ps_cipher.obj diff --git a/builds/msvc/vs2026/libbitcoin-network-test/libbitcoin-network-test.vcxproj.filters b/builds/msvc/vs2026/libbitcoin-network-test/libbitcoin-network-test.vcxproj.filters index 01402fb00..710fc0bc2 100644 --- a/builds/msvc/vs2026/libbitcoin-network-test/libbitcoin-network-test.vcxproj.filters +++ b/builds/msvc/vs2026/libbitcoin-network-test/libbitcoin-network-test.vcxproj.filters @@ -531,6 +531,9 @@ src\net + + src\noise + src\noise diff --git a/builds/msvc/vs2026/libbitcoin-network/libbitcoin-network.vcxproj b/builds/msvc/vs2026/libbitcoin-network/libbitcoin-network.vcxproj index 7b0c2e207..439d2be9b 100644 --- a/builds/msvc/vs2026/libbitcoin-network/libbitcoin-network.vcxproj +++ b/builds/msvc/vs2026/libbitcoin-network/libbitcoin-network.vcxproj @@ -271,6 +271,9 @@ + + $(IntDir)src_noise_cipher.obj + $(IntDir)src_p2ps_cipher.obj @@ -519,6 +522,8 @@ + + diff --git a/builds/msvc/vs2026/libbitcoin-network/libbitcoin-network.vcxproj.filters b/builds/msvc/vs2026/libbitcoin-network/libbitcoin-network.vcxproj.filters index ce77f006a..7e8775a0f 100644 --- a/builds/msvc/vs2026/libbitcoin-network/libbitcoin-network.vcxproj.filters +++ b/builds/msvc/vs2026/libbitcoin-network/libbitcoin-network.vcxproj.filters @@ -109,96 +109,102 @@ {564EB540-D6B6-425C-0000-000000000003} - + {564EB540-D6B6-425C-0000-000000000004} - + {564EB540-D6B6-425C-0000-000000000005} - + {564EB540-D6B6-425C-0000-000000000006} - + {564EB540-D6B6-425C-0000-000000000007} - + {564EB540-D6B6-425C-0000-000000000008} - + {564EB540-D6B6-425C-0000-000000000009} - + {564EB540-D6B6-425C-0000-000000000010} - + {564EB540-D6B6-425C-0000-000000000011} - + {564EB540-D6B6-425C-0000-0000000000A2} - + {564EB540-D6B6-425C-0000-0000000000B2} - + {564EB540-D6B6-425C-0000-0000000000C2} - + {564EB540-D6B6-425C-0000-0000000000D2} - + {564EB540-D6B6-425C-0000-0000000000E2} - + {564EB540-D6B6-425C-0000-0000000000F2} - + {564EB540-D6B6-425C-0000-000000000003} - + {564EB540-D6B6-425C-0000-000000000004} - + {564EB540-D6B6-425C-0000-000000000005} - + {564EB540-D6B6-425C-0000-000000000006} - + {564EB540-D6B6-425C-0000-000000000007} - + {564EB540-D6B6-425C-0000-000000000008} - + {564EB540-D6B6-425C-0000-000000000009} - + {564EB540-D6B6-425C-0000-000000000010} - + {564EB540-D6B6-425C-0000-000000000011} - + {564EB540-D6B6-425C-0000-000000000012} - + {564EB540-D6B6-425C-0000-0000000000A3} - + {564EB540-D6B6-425C-0000-0000000000B3} - + {564EB540-D6B6-425C-0000-0000000000C3} - + {564EB540-D6B6-425C-0000-0000000000D3} - + {564EB540-D6B6-425C-0000-0000000000E3} - + {564EB540-D6B6-425C-0000-0000000000F3} + + {564EB540-D6B6-425C-0000-000000000004} + + + {564EB540-D6B6-425C-0000-000000000005} + @@ -615,6 +621,9 @@ src\net + + src\noise + src\p2ps @@ -1313,6 +1322,12 @@ include\bitcoin\network\net + + include\bitcoin\network\noise + + + include\bitcoin\network\noise + include\bitcoin\network\p2ps diff --git a/include/bitcoin/network.hpp b/include/bitcoin/network.hpp index 6ccc3aaa1..f6fb94240 100644 --- a/include/bitcoin/network.hpp +++ b/include/bitcoin/network.hpp @@ -204,6 +204,8 @@ #include #include #include +#include +#include #include #include #include diff --git a/include/bitcoin/network/noise/cipher.hpp b/include/bitcoin/network/noise/cipher.hpp new file mode 100644 index 000000000..f2a313357 --- /dev/null +++ b/include/bitcoin/network/noise/cipher.hpp @@ -0,0 +1,158 @@ +/** + * Copyright (c) 2011-2026 libbitcoin developers + * + * This file is part of libbitcoin. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#ifndef LIBBITCOIN_NETWORK_NOISE_CIPHER_HPP +#define LIBBITCOIN_NETWORK_NOISE_CIPHER_HPP + +#include +#include +#include + +namespace libbitcoin { +namespace network { +namespace noise { + +/// Stratum v2 Noise NX (Noise_NX_Secp256k1+EllSwift_ChaChaPoly_SHA256) +/// handshake and transport state of one connection. The initiator sends act1, +/// the responder answers with act2 (its static key and certificate), and both +/// then derive directional transport ciphers. Transport frames are sv2 frames +/// (heading then payload), the heading and each payload chunk sealed as +/// separate blocks. Not thread safe. +class BCT_API cipher final +{ +public: + DELETE_COPY_MOVE(cipher); + + /// Constants. + static constexpr size_t key_size = system::ec_ellswift_size; + static constexpr size_t tag_size = system::chacha20_poly1305::expansion; + static constexpr size_t certificate_size = 74; + static constexpr size_t act1_size = key_size; + static constexpr size_t act2_size = key_size + key_size + tag_size + + certificate_size + tag_size; + static constexpr size_t heading_size = 6; + static constexpr size_t sealed_heading_size = heading_size + tag_size; + static constexpr size_t chunk_size = 65535 - tag_size; + + typedef system::ec_ellswift key; + typedef system::data_array certificate; + typedef system::data_array act1; + typedef system::data_array act2; + typedef std::span span; + + /// Sealed size of a payload of the given plaintext size. + static constexpr size_t sealed_size(size_t payload) NOEXCEPT + { + const auto remainder = payload % chunk_size; + return (payload / chunk_size) * (chunk_size + tag_size) + + (is_zero(remainder) ? zero : remainder + tag_size); + } + + /// Initiator: generate an ephemeral keypair. + cipher() NOEXCEPT; + + /// Initiator: given ephemeral keypair (deterministic, for test vectors). + cipher(const system::ec_secret& secret, const key& public_key) NOEXCEPT; + + /// Responder: static keypair and certificate, generates ephemeral keypair. + cipher(const system::ec_secret& static_secret, const key& static_key, + const certificate& proof) NOEXCEPT; + + /// Responder: given ephemeral keypair (deterministic, for test vectors). + cipher(const system::ec_secret& secret, const key& public_key, + const system::ec_secret& static_secret, const key& static_key, + const certificate& proof) NOEXCEPT; + + /// Own ephemeral public key (ElligatorSwift encoded). + const key& public_key() const NOEXCEPT; + + /// Handshake hash (final after respond or complete). + const system::hash_digest& handshake_hash() const NOEXCEPT; + + /// Initiator: produce act1. + void initiate(act1& out) NOEXCEPT; + + /// Responder: consume act1 and produce act2, deriving transport ciphers. + bool respond(act2& out, const act1& in) NOEXCEPT; + + /// Initiator: consume act2, obtaining the responder static key and its + /// (unverified) certificate, and deriving transport ciphers. + bool complete(key& static_key, certificate& proof, + const act2& in) NOEXCEPT; + + /// Seal a frame (heading then payload) into out, where + /// out = sealed_heading_size + sealed_size(frame size - heading_size). + /// False if the send nonce is exhausted. + bool encrypt(const std::span& out, const span& frame) NOEXCEPT; + + /// Open a sealed heading (in = sealed_heading_size) into plain. + /// False if the tag does not authenticate or nonce is exhausted. + bool decrypt_heading(const std::span& plain, + const span& in) NOEXCEPT; + + /// Open a sealed payload (in = sealed_size(plain size)) into plain. + /// False if a tag does not authenticate or nonce is exhausted. + bool decrypt_payload(const std::span& plain, + const span& in) NOEXCEPT; + +private: + // ChaCha20-Poly1305 with a counter nonce, the last value reserved. + class state + { + public: + state(const system::hash_digest& secret) NOEXCEPT; + bool encrypt(const std::span& out, const span& aad, + const span& plain) NOEXCEPT; + bool decrypt(const std::span& plain, const span& aad, + const span& in) NOEXCEPT; + + private: + system::chacha20_poly1305 aead_; + uint64_t nonce_{}; + }; + + void mix_hash(const span& data) NOEXCEPT; + void mix_key(const system::hash_digest& material) NOEXCEPT; + bool encrypt_and_hash(const std::span& out, + const span& plain) NOEXCEPT; + bool decrypt_and_hash(const std::span& plain, + const span& in) NOEXCEPT; + void split(bool initiator) NOEXCEPT; + + // Keys. + system::ec_secret secret_; + key public_; + system::ec_secret static_secret_{}; + key static_{}; + certificate proof_{}; + + // Symmetric state. + system::hash_digest chaining_; + system::hash_digest hash_; + std::optional handshake_{}; + + // Transport. + std::optional send_{}; + std::optional receive_{}; +}; + +} // namespace noise +} // namespace network +} // namespace libbitcoin + +#endif diff --git a/include/bitcoin/network/noise/noise.hpp b/include/bitcoin/network/noise/noise.hpp new file mode 100644 index 000000000..0c859c305 --- /dev/null +++ b/include/bitcoin/network/noise/noise.hpp @@ -0,0 +1,24 @@ +/** + * Copyright (c) 2011-2026 libbitcoin developers + * + * This file is part of libbitcoin. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#ifndef LIBBITCOIN_NETWORK_NOISE_NOISE_HPP +#define LIBBITCOIN_NETWORK_NOISE_NOISE_HPP + +#include + +#endif diff --git a/src/noise/cipher.cpp b/src/noise/cipher.cpp new file mode 100644 index 000000000..bd4fec31c --- /dev/null +++ b/src/noise/cipher.cpp @@ -0,0 +1,298 @@ +/** + * Copyright (c) 2011-2026 libbitcoin developers + * + * This file is part of libbitcoin. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#include + +#include +#include + +namespace libbitcoin { +namespace network { +namespace noise { + +using namespace system; + +constexpr char protocol_name[] = "Noise_NX_Secp256k1+EllSwift_ChaChaPoly_SHA256"; + +static void generate(ec_secret& secret, cipher::key& public_key) NOEXCEPT +{ + // A secret outside the group order is astronomically improbable. + hash_digest tweak{}; + do + { + maybe_random::fill(secret); + maybe_random::fill(tweak); + } + while (!ellswift::create(public_key, secret, tweak)); +} + +// Construction. +// ---------------------------------------------------------------------------- + +cipher::cipher() NOEXCEPT + : cipher(ec_secret{}, key{}) +{ + generate(secret_, public_); +} + +cipher::cipher(const ec_secret& secret, const key& public_key) NOEXCEPT + : secret_(secret), + public_(public_key), + chaining_(sha256_hash(to_chunk(protocol_name))), + hash_(sha256_hash(chaining_)) +{ +} + +cipher::cipher(const ec_secret& static_secret, const key& static_key, + const certificate& proof) NOEXCEPT + : cipher(ec_secret{}, key{}, static_secret, static_key, proof) +{ + generate(secret_, public_); +} + +cipher::cipher(const ec_secret& secret, const key& public_key, + const ec_secret& static_secret, const key& static_key, + const certificate& proof) NOEXCEPT + : secret_(secret), + public_(public_key), + static_secret_(static_secret), + static_(static_key), + proof_(proof), + chaining_(sha256_hash(to_chunk(protocol_name))), + hash_(sha256_hash(chaining_)) +{ +} + +const cipher::key& cipher::public_key() const NOEXCEPT +{ + return public_; +} + +const hash_digest& cipher::handshake_hash() const NOEXCEPT +{ + return hash_; +} + +// Handshake. +// ---------------------------------------------------------------------------- + +// -> e +void cipher::initiate(act1& out) NOEXCEPT +{ + out = public_; + mix_hash(public_); + mix_hash({}); +} + +// <- e, ee, s, es, SIGNATURE_NOISE_MESSAGE +bool cipher::respond(act2& out, const act1& in) NOEXCEPT +{ + mix_hash(in); + mix_hash({}); + + const std::span message{ out }; + std::copy(public_.begin(), public_.end(), out.begin()); + mix_hash(public_); + + hash_digest ecdh{}; + if (!ellswift::exchange(ecdh, secret_, in, public_, true)) + return false; + + mix_key(ecdh); + if (!encrypt_and_hash(message.subspan(key_size, key_size + tag_size), + static_)) + return false; + + if (!ellswift::exchange(ecdh, static_secret_, in, static_, true)) + return false; + + mix_key(ecdh); + ecdh = {}; + if (!encrypt_and_hash(message.subspan(key_size + key_size + tag_size), + proof_)) + return false; + + split(false); + return true; +} + +bool cipher::complete(key& static_key, certificate& proof, + const act2& in) NOEXCEPT +{ + const span message{ in }; + const auto theirs = slice(in); + mix_hash(theirs); + + hash_digest ecdh{}; + if (!ellswift::exchange(ecdh, secret_, public_, theirs, false)) + return false; + + mix_key(ecdh); + if (!decrypt_and_hash(static_key, message.subspan(key_size, + key_size + tag_size))) + return false; + + if (!ellswift::exchange(ecdh, secret_, public_, static_key, false)) + return false; + + mix_key(ecdh); + ecdh = {}; + if (!decrypt_and_hash(proof, message.subspan(key_size + key_size + + tag_size))) + return false; + + split(true); + return true; +} + +// Transport. +// ---------------------------------------------------------------------------- + +bool cipher::encrypt(const std::span& out, const span& frame) NOEXCEPT +{ + BC_ASSERT(frame.size() >= heading_size); + const auto payload = frame.subspan(heading_size); + BC_ASSERT(out.size() == sealed_heading_size + sealed_size(payload.size())); + + if (!send_->encrypt(out.first(sealed_heading_size), {}, + frame.first(heading_size))) + return false; + + auto sealed = out.subspan(sealed_heading_size); + for (size_t start{}; start < payload.size(); start += chunk_size) + { + const auto size = std::min(chunk_size, payload.size() - start); + if (!send_->encrypt(sealed.first(size + tag_size), {}, + payload.subspan(start, size))) + return false; + + sealed = sealed.subspan(size + tag_size); + } + + return true; +} + +bool cipher::decrypt_heading(const std::span& plain, + const span& in) NOEXCEPT +{ + BC_ASSERT(plain.size() == heading_size); + BC_ASSERT(in.size() == sealed_heading_size); + return receive_->decrypt(plain, {}, in); +} + +bool cipher::decrypt_payload(const std::span& plain, + const span& in) NOEXCEPT +{ + BC_ASSERT(in.size() == sealed_size(plain.size())); + + auto sealed = in; + for (size_t start{}; start < plain.size(); start += chunk_size) + { + const auto size = std::min(chunk_size, plain.size() - start); + if (!receive_->decrypt(plain.subspan(start, size), {}, + sealed.first(size + tag_size))) + return false; + + sealed = sealed.subspan(size + tag_size); + } + + return true; +} + +// Symmetric state (private). +// ---------------------------------------------------------------------------- + +void cipher::mix_hash(const span& data) NOEXCEPT +{ + hash_ = sha256_hash2(hash_, { data.begin(), data.end() }); +} + +void cipher::mix_key(const hash_digest& material) NOEXCEPT +{ + const auto keys = hkdf::key(material, chaining_, + {}); + chaining_ = slice(keys); + handshake_.emplace(slice(keys)); +} + +bool cipher::encrypt_and_hash(const std::span& out, + const span& plain) NOEXCEPT +{ + if (!handshake_->encrypt(out, hash_, plain)) + return false; + + mix_hash(out); + return true; +} + +bool cipher::decrypt_and_hash(const std::span& plain, + const span& in) NOEXCEPT +{ + if (!handshake_->decrypt(plain, hash_, in)) + return false; + + mix_hash(in); + return true; +} + +// The first key is the initiator's sending key. +void cipher::split(bool initiator) NOEXCEPT +{ + const auto keys = hkdf::key({}, chaining_, {}); + const auto first = slice(keys); + const auto second = slice(keys); + send_.emplace(initiator ? first : second); + receive_.emplace(initiator ? second : first); + + // The secrets are no longer required. + secret_ = {}; + static_secret_ = {}; + handshake_.reset(); +} + +// Cipher state (private). +// ---------------------------------------------------------------------------- + +cipher::state::state(const hash_digest& secret) NOEXCEPT + : aead_(secret) +{ +} + +bool cipher::state::encrypt(const std::span& out, const span& aad, + const span& plain) NOEXCEPT +{ + if (nonce_ == max_uint64) + return false; + + aead_.encrypt(plain, aad, 0, nonce_++, out); + return true; +} + +bool cipher::state::decrypt(const std::span& plain, const span& aad, + const span& in) NOEXCEPT +{ + if (nonce_ == max_uint64 || !aead_.decrypt(plain, aad, 0, nonce_, in)) + return false; + + ++nonce_; + return true; +} + +} // namespace noise +} // namespace network +} // namespace libbitcoin diff --git a/test/noise/cipher.cpp b/test/noise/cipher.cpp new file mode 100644 index 000000000..da09295c6 --- /dev/null +++ b/test/noise/cipher.cpp @@ -0,0 +1,281 @@ +/** + * Copyright (c) 2011-2026 libbitcoin developers + * + * This file is part of libbitcoin. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#include "../test.hpp" +#include "vectors.hpp" + +BC_AUTO_TEST_SUITE(noise_cipher_tests) + +using namespace network::noise; +using namespace system; + +typedef std::vector frames; +typedef data_array heading; + +static bool handshake(cipher& initiator, cipher& responder) +{ + cipher::act1 request{}; + cipher::act2 response{}; + cipher::key static_key{}; + cipher::certificate proof{}; + initiator.initiate(request); + return responder.respond(response, request) && + initiator.complete(static_key, proof, response); +} + +static bool complete_tampered(size_t index) +{ + cipher initiator{ sv2_noise::initiator_secret, + sv2_noise::initiator_ellswift }; + + cipher::act1 request{}; + cipher::key static_key{}; + cipher::certificate proof{}; + auto response = sv2_noise::message2; + response.at(index) ^= 1u; + initiator.initiate(request); + return initiator.complete(static_key, proof, response); +} + +// Byte i of a payload is i mod 256. +static data_chunk plain_frame(const sv2_noise::frame& frame) +{ + const auto extension = to_little_endian(frame.extension); + const auto length = to_little_endian(frame.size); + data_chunk out + { + extension[0], extension[1], frame.type, length[0], length[1], length[2] + }; + + data_chunk payload(frame.size); + std::iota(payload.begin(), payload.end(), uint8_t{}); + out.insert(out.end(), payload.begin(), payload.end()); + return out; +} + +static data_chunk seal(cipher& sender, const data_chunk& frame) +{ + const auto payload = frame.size() - cipher::heading_size; + data_chunk out(cipher::sealed_heading_size + cipher::sealed_size(payload)); + return sender.encrypt(out, frame) ? out : data_chunk{}; +} + +static data_chunk open(cipher& receiver, const data_chunk& sealed) +{ + const cipher::span in{ sealed }; + heading head{}; + if (!receiver.decrypt_heading(head, in.first(cipher::sealed_heading_size))) + return {}; + + const auto size = from_little_array(slice<3, 6>(head)); + data_chunk out(cipher::heading_size + size); + std::copy(head.begin(), head.end(), out.begin()); + const std::span payload{ out }; + return receiver.decrypt_payload(payload.subspan(cipher::heading_size), + in.subspan(cipher::sealed_heading_size)) ? out : data_chunk{}; +} + +static std::vector digests(cipher& sender, const frames& values) +{ + std::vector out{}; + for (const auto& frame: values) + out.push_back(sha256_hash(seal(sender, plain_frame(frame)))); + + return out; +} + +static std::vector expected(const frames& values) +{ + std::vector out{}; + for (const auto& frame: values) + out.push_back(frame.digest); + + return out; +} + +static std::vector opened(cipher& sender, cipher& receiver, + const frames& values) +{ + std::vector out{}; + for (const auto& frame: values) + out.push_back(sha256_hash(open(receiver, + seal(sender, plain_frame(frame))))); + + return out; +} + +static std::vector plains(const frames& values) +{ + std::vector out{}; + for (const auto& frame: values) + out.push_back(sha256_hash(plain_frame(frame))); + + return out; +} + +BC_AUTO_TEST_CASE(noise_cipher__sealed_size__chunk_boundaries__expected) +{ + BC_REQUIRE_EQUAL(cipher::sealed_size(0), 0u); + BC_REQUIRE_EQUAL(cipher::sealed_size(1), 17u); + BC_REQUIRE_EQUAL(cipher::sealed_size(65519), 65535u); + BC_REQUIRE_EQUAL(cipher::sealed_size(65520), 65552u); + BC_REQUIRE_EQUAL(cipher::sealed_size(131038), 131070u); + BC_REQUIRE_EQUAL(cipher::sealed_size(131039), 131087u); +} + +BC_AUTO_TEST_CASE(noise_cipher__initiate__vector_keys__message1) +{ + cipher initiator{ sv2_noise::initiator_secret, sv2_noise::initiator_ellswift }; + cipher::act1 out{}; + initiator.initiate(out); + BC_REQUIRE_EQUAL(out, sv2_noise::message1); + BC_REQUIRE_EQUAL(initiator.public_key(), sv2_noise::initiator_ellswift); + BC_REQUIRE_EQUAL(initiator.handshake_hash(), sv2_noise::message1_h); +} + +BC_AUTO_TEST_CASE(noise_cipher__respond__message1__message2) +{ + cipher responder{ sv2_noise::responder_secret, sv2_noise::responder_ellswift, sv2_noise::static_secret, sv2_noise::static_ellswift, sv2_noise::signature_noise_message }; + cipher::act2 out{}; + const auto result = responder.respond(out, sv2_noise::message1); + BC_REQUIRE(result); + BC_REQUIRE_EQUAL(out, sv2_noise::message2); + BC_REQUIRE_EQUAL(responder.handshake_hash(), sv2_noise::message2_h); +} + +BC_AUTO_TEST_CASE(noise_cipher__complete__message2__static_key_and_certificate) +{ + cipher initiator{ sv2_noise::initiator_secret, sv2_noise::initiator_ellswift }; + cipher::act1 request{}; + cipher::key static_key{}; + cipher::certificate proof{}; + initiator.initiate(request); + const auto result = initiator.complete(static_key, proof, sv2_noise::message2); + BC_REQUIRE(result); + BC_REQUIRE_EQUAL(static_key, sv2_noise::static_ellswift); + BC_REQUIRE_EQUAL(proof, sv2_noise::signature_noise_message); + BC_REQUIRE_EQUAL(initiator.handshake_hash(), sv2_noise::message2_h); +} + +BC_AUTO_TEST_CASE(noise_cipher__complete__tampered_ephemeral__false) +{ + BC_REQUIRE(!complete_tampered(0)); +} + +BC_AUTO_TEST_CASE(noise_cipher__complete__tampered_static__false) +{ + BC_REQUIRE(!complete_tampered(64)); +} + +BC_AUTO_TEST_CASE(noise_cipher__complete__tampered_certificate__false) +{ + BC_REQUIRE(!complete_tampered(144)); +} + +BC_AUTO_TEST_CASE(noise_cipher__complete__tampered_tag__false) +{ + BC_REQUIRE(!complete_tampered(233)); +} + +BC_AUTO_TEST_CASE(noise_cipher__encrypt__initiator_frames__digests) +{ + cipher initiator{ sv2_noise::initiator_secret, sv2_noise::initiator_ellswift }; + cipher responder{ sv2_noise::responder_secret, sv2_noise::responder_ellswift, sv2_noise::static_secret, sv2_noise::static_ellswift, sv2_noise::signature_noise_message }; + const auto result = handshake(initiator, responder); + const auto actual = digests(initiator, sv2_noise::initiator_frames); + BC_REQUIRE(result); + BC_REQUIRE_EQUAL(actual, expected(sv2_noise::initiator_frames)); +} + +BC_AUTO_TEST_CASE(noise_cipher__encrypt__responder_frames__digests) +{ + cipher initiator{ sv2_noise::initiator_secret, sv2_noise::initiator_ellswift }; + cipher responder{ sv2_noise::responder_secret, sv2_noise::responder_ellswift, sv2_noise::static_secret, sv2_noise::static_ellswift, sv2_noise::signature_noise_message }; + const auto result = handshake(initiator, responder); + const auto actual = digests(responder, sv2_noise::responder_frames); + BC_REQUIRE(result); + BC_REQUIRE_EQUAL(actual, expected(sv2_noise::responder_frames)); +} + +BC_AUTO_TEST_CASE(noise_cipher__decrypt__initiator_frames__plain_frames) +{ + cipher initiator{ sv2_noise::initiator_secret, sv2_noise::initiator_ellswift }; + cipher responder{ sv2_noise::responder_secret, sv2_noise::responder_ellswift, sv2_noise::static_secret, sv2_noise::static_ellswift, sv2_noise::signature_noise_message }; + const auto result = handshake(initiator, responder); + const auto actual = opened(initiator, responder, sv2_noise::initiator_frames); + BC_REQUIRE(result); + BC_REQUIRE_EQUAL(actual, plains(sv2_noise::initiator_frames)); +} + +BC_AUTO_TEST_CASE(noise_cipher__decrypt__responder_frames__plain_frames) +{ + cipher initiator{ sv2_noise::initiator_secret, sv2_noise::initiator_ellswift }; + cipher responder{ sv2_noise::responder_secret, sv2_noise::responder_ellswift, sv2_noise::static_secret, sv2_noise::static_ellswift, sv2_noise::signature_noise_message }; + const auto result = handshake(initiator, responder); + const auto actual = opened(responder, initiator, sv2_noise::responder_frames); + BC_REQUIRE(result); + BC_REQUIRE_EQUAL(actual, plains(sv2_noise::responder_frames)); +} + +BC_AUTO_TEST_CASE(noise_cipher__decrypt_heading__tampered_then_intact__false_then_true) +{ + cipher initiator{ sv2_noise::initiator_secret, sv2_noise::initiator_ellswift }; + cipher responder{ sv2_noise::responder_secret, sv2_noise::responder_ellswift, sv2_noise::static_secret, sv2_noise::static_ellswift, sv2_noise::signature_noise_message }; + const auto result = handshake(initiator, responder); + const auto plain = plain_frame(sv2_noise::initiator_frames.front()); + const auto sealed = seal(initiator, plain); + auto tampered = sealed; + tampered.front() ^= 1u; + heading head{}; + const auto rejected = responder.decrypt_heading(head, tampered); + const auto accepted = responder.decrypt_heading(head, sealed); + BC_REQUIRE(result); + BC_REQUIRE(!rejected); + BC_REQUIRE(accepted); + BC_REQUIRE_EQUAL(to_chunk(head), plain); +} + +BC_AUTO_TEST_CASE(noise_cipher__complete__generated_keys__static_key_and_matching_hash) +{ + cipher initiator{}; + cipher responder{ sv2_noise::static_secret, sv2_noise::static_ellswift, sv2_noise::signature_noise_message }; + cipher::act1 request{}; + cipher::act2 response{}; + cipher::key static_key{}; + cipher::certificate proof{}; + initiator.initiate(request); + const auto responded = responder.respond(response, request); + const auto completed = initiator.complete(static_key, proof, response); + BC_REQUIRE(responded); + BC_REQUIRE(completed); + BC_REQUIRE_EQUAL(static_key, sv2_noise::static_ellswift); + BC_REQUIRE_EQUAL(proof, sv2_noise::signature_noise_message); + BC_REQUIRE_EQUAL(initiator.handshake_hash(), responder.handshake_hash()); +} + +BC_AUTO_TEST_CASE(noise_cipher__decrypt__generated_keys__plain_frames) +{ + cipher initiator{}; + cipher responder{ sv2_noise::static_secret, sv2_noise::static_ellswift, sv2_noise::signature_noise_message }; + const auto result = handshake(initiator, responder); + const auto actual = opened(initiator, responder, sv2_noise::initiator_frames); + BC_REQUIRE(result); + BC_REQUIRE_EQUAL(actual, plains(sv2_noise::initiator_frames)); +} + +BC_AUTO_TEST_SUITE_END() From 87f016a89a91663aefe08d74c52c144c8dace278 Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Thu, 26 Nov 2026 21:44:49 -0500 Subject: [PATCH 4/9] Add sv2 Noise authority certificate and responder context. --- builds/gnu/Makefile.am | 6 + .../libbitcoin-network-test.vcxproj | 10 +- .../libbitcoin-network-test.vcxproj.filters | 6 + .../libbitcoin-network.vcxproj | 12 +- .../libbitcoin-network.vcxproj.filters | 12 ++ include/bitcoin/network.hpp | 2 + include/bitcoin/network/noise/certificate.hpp | 87 +++++++++ include/bitcoin/network/noise/context.hpp | 81 ++++++++ include/bitcoin/network/noise/noise.hpp | 2 + src/noise/certificate.cpp | 144 ++++++++++++++ src/noise/context.cpp | 101 ++++++++++ test/noise/certificate.cpp | 181 ++++++++++++++++++ test/noise/context.cpp | 104 ++++++++++ 13 files changed, 746 insertions(+), 2 deletions(-) create mode 100644 include/bitcoin/network/noise/certificate.hpp create mode 100644 include/bitcoin/network/noise/context.hpp create mode 100644 src/noise/certificate.cpp create mode 100644 src/noise/context.cpp create mode 100644 test/noise/certificate.cpp create mode 100644 test/noise/context.cpp diff --git a/builds/gnu/Makefile.am b/builds/gnu/Makefile.am index 28cd82d3b..3ad07594f 100644 --- a/builds/gnu/Makefile.am +++ b/builds/gnu/Makefile.am @@ -187,7 +187,9 @@ src_libbitcoin_network_la_SOURCES = \ ${srcdir}/../../src/net/socket_tcp.cpp \ ${srcdir}/../../src/net/socket_ws.cpp \ ${srcdir}/../../src/net/socket_zmtp.cpp \ + ${srcdir}/../../src/noise/certificate.cpp \ ${srcdir}/../../src/noise/cipher.cpp \ + ${srcdir}/../../src/noise/context.cpp \ ${srcdir}/../../src/p2ps/cipher.cpp \ ${srcdir}/../../src/p2ps/stream.cpp \ ${srcdir}/../../src/protocols/protocol.cpp \ @@ -627,7 +629,9 @@ include_bitcoin_network_noisedir = \ ${includedir}/bitcoin/network/noise include_bitcoin_network_noise_HEADERS = \ + ${srcdir}/../../include/bitcoin/network/noise/certificate.hpp \ ${srcdir}/../../include/bitcoin/network/noise/cipher.hpp \ + ${srcdir}/../../include/bitcoin/network/noise/context.hpp \ ${srcdir}/../../include/bitcoin/network/noise/noise.hpp include_bitcoin_network_p2psdir = \ @@ -892,7 +896,9 @@ test_libbitcoin_network_test_SOURCES = \ ${srcdir}/../../test/net/hosts.cpp \ ${srcdir}/../../test/net/proxy.cpp \ ${srcdir}/../../test/net/socket.cpp \ + ${srcdir}/../../test/noise/certificate.cpp \ ${srcdir}/../../test/noise/cipher.cpp \ + ${srcdir}/../../test/noise/context.cpp \ ${srcdir}/../../test/noise/vectors.cpp \ ${srcdir}/../../test/p2ps/cipher.cpp \ ${srcdir}/../../test/p2ps/stream.cpp \ diff --git a/builds/msvc/vs2026/libbitcoin-network-test/libbitcoin-network-test.vcxproj b/builds/msvc/vs2026/libbitcoin-network-test/libbitcoin-network-test.vcxproj index f2a390cd9..9562b2666 100644 --- a/builds/msvc/vs2026/libbitcoin-network-test/libbitcoin-network-test.vcxproj +++ b/builds/msvc/vs2026/libbitcoin-network-test/libbitcoin-network-test.vcxproj @@ -272,9 +272,15 @@ $(IntDir)test_net_socket.obj + + $(IntDir)test_noise_certificate.obj + $(IntDir)test_noise_cipher.obj + + $(IntDir)test_noise_context.obj + $(IntDir)test_p2ps_cipher.obj @@ -322,7 +328,9 @@ $(IntDir)test_tls_socket.obj - + + $(IntDir)test_tor_certificate.obj + diff --git a/builds/msvc/vs2026/libbitcoin-network-test/libbitcoin-network-test.vcxproj.filters b/builds/msvc/vs2026/libbitcoin-network-test/libbitcoin-network-test.vcxproj.filters index 710fc0bc2..6b8603257 100644 --- a/builds/msvc/vs2026/libbitcoin-network-test/libbitcoin-network-test.vcxproj.filters +++ b/builds/msvc/vs2026/libbitcoin-network-test/libbitcoin-network-test.vcxproj.filters @@ -531,9 +531,15 @@ src\net + + src\noise + src\noise + + src\noise + src\noise diff --git a/builds/msvc/vs2026/libbitcoin-network/libbitcoin-network.vcxproj b/builds/msvc/vs2026/libbitcoin-network/libbitcoin-network.vcxproj index 439d2be9b..f8cc1ed64 100644 --- a/builds/msvc/vs2026/libbitcoin-network/libbitcoin-network.vcxproj +++ b/builds/msvc/vs2026/libbitcoin-network/libbitcoin-network.vcxproj @@ -271,9 +271,15 @@ + + $(IntDir)src_noise_certificate.obj + $(IntDir)src_noise_cipher.obj + + $(IntDir)src_noise_context.obj + $(IntDir)src_p2ps_cipher.obj @@ -319,7 +325,9 @@ $(IntDir)src_tls_stream.obj - + + $(IntDir)src_tor_certificate.obj + @@ -522,7 +530,9 @@ + + diff --git a/builds/msvc/vs2026/libbitcoin-network/libbitcoin-network.vcxproj.filters b/builds/msvc/vs2026/libbitcoin-network/libbitcoin-network.vcxproj.filters index 7e8775a0f..e86460b23 100644 --- a/builds/msvc/vs2026/libbitcoin-network/libbitcoin-network.vcxproj.filters +++ b/builds/msvc/vs2026/libbitcoin-network/libbitcoin-network.vcxproj.filters @@ -621,9 +621,15 @@ src\net + + src\noise + src\noise + + src\noise + src\p2ps @@ -1322,9 +1328,15 @@ include\bitcoin\network\net + + include\bitcoin\network\noise + include\bitcoin\network\noise + + include\bitcoin\network\noise + include\bitcoin\network\noise diff --git a/include/bitcoin/network.hpp b/include/bitcoin/network.hpp index f6fb94240..4a967dfe6 100644 --- a/include/bitcoin/network.hpp +++ b/include/bitcoin/network.hpp @@ -204,7 +204,9 @@ #include #include #include +#include #include +#include #include #include #include diff --git a/include/bitcoin/network/noise/certificate.hpp b/include/bitcoin/network/noise/certificate.hpp new file mode 100644 index 000000000..4fe486388 --- /dev/null +++ b/include/bitcoin/network/noise/certificate.hpp @@ -0,0 +1,87 @@ +/** + * Copyright (c) 2011-2026 libbitcoin developers + * + * This file is part of libbitcoin. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#ifndef LIBBITCOIN_NETWORK_NOISE_CERTIFICATE_HPP +#define LIBBITCOIN_NETWORK_NOISE_CERTIFICATE_HPP + +#include +#include + +namespace libbitcoin { +namespace network { +namespace noise { + +/// Stratum v2 authority certificate (SIGNATURE_NOISE_MESSAGE): version, +/// validity window (unix seconds) and the authority's bip340 signature of the +/// server static x-only key. The authority key is published to miners as +/// base58check of its version bytes and x-only key. +class BCT_API certificate final +{ +public: + /// Constants. + static constexpr uint16_t version = 0; + static constexpr size_t header_size = sizeof(uint16_t) + + sizeof(uint32_t) + sizeof(uint32_t); + static constexpr size_t authority_size = sizeof(uint16_t) + + system::ec_xonly_size; + + typedef cipher::certificate data; + + /// The x-only key of a secret (false if the secret is invalid). + static bool to_xonly(system::ec_xonly& out, + const system::ec_secret& secret) NOEXCEPT; + + /// The x-only key of an ElligatorSwift encoded key. + static bool to_xonly(system::ec_xonly& out, + const cipher::key& key) NOEXCEPT; + + /// The x-only key of a compressed key. + static system::ec_xonly to_xonly(const system::ec_compressed& key) NOEXCEPT; + + /// Sign the server key over the window, with random auxiliary data. + static bool sign(data& out, const system::ec_secret& authority, + const system::ec_xonly& server, uint32_t valid_from, + uint32_t not_valid_after) NOEXCEPT; + + /// Sign the server key over the window (deterministic, for test vectors). + static bool sign(data& out, const system::ec_secret& authority, + const system::ec_xonly& server, uint32_t valid_from, + uint32_t not_valid_after, const system::hash_digest& auxiliary) NOEXCEPT; + + /// Verify version, window (inclusive of now) and signature. + static bool verify(const data& in, const system::ec_xonly& server, + const system::ec_xonly& authority, uint32_t now) NOEXCEPT; + + /// The window of a certificate. + static uint32_t valid_from(const data& in) NOEXCEPT; + static uint32_t not_valid_after(const data& in) NOEXCEPT; + + /// Encode an authority key for publication. + static std::string encode_authority( + const system::ec_xonly& authority) NOEXCEPT; + + /// Decode a published authority key (false if malformed). + static bool decode_authority(system::ec_xonly& out, + const std::string& text) NOEXCEPT; +}; + +} // namespace noise +} // namespace network +} // namespace libbitcoin + +#endif diff --git a/include/bitcoin/network/noise/context.hpp b/include/bitcoin/network/noise/context.hpp new file mode 100644 index 000000000..50437e448 --- /dev/null +++ b/include/bitcoin/network/noise/context.hpp @@ -0,0 +1,81 @@ +/** + * Copyright (c) 2011-2026 libbitcoin developers + * + * This file is part of libbitcoin. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#ifndef LIBBITCOIN_NETWORK_NOISE_CONTEXT_HPP +#define LIBBITCOIN_NETWORK_NOISE_CONTEXT_HPP + +#include +#include +#include +#include + +namespace libbitcoin { +namespace network { +namespace noise { + +/// Shared responder configuration of Noise sockets: the static keypair and +/// either a pre-issued certificate or the authority secret, with which a +/// certificate is issued for each handshake. Owner must outlive all sockets +/// created with reference to it. Thread safe. +class BCT_API context final +{ +public: + DELETE_COPY_MOVE(context); + + /// Present the pre-issued certificate. + context(const system::ec_secret& static_secret, + const certificate::data& issued) NOEXCEPT; + + /// Issue certificates valid for validity seconds either side of issue. + context(const system::ec_secret& static_secret, + const system::ec_secret& authority, uint32_t validity) NOEXCEPT; + + /// False if a secret is invalid. + bool valid() const NOEXCEPT; + + /// The static keypair (the public key ElligatorSwift encoded). + const system::ec_secret& static_secret() const NOEXCEPT; + const cipher::key& static_key() const NOEXCEPT; + + /// The authority key (issuing only, otherwise empty). + const std::optional& authority() const NOEXCEPT; + + /// The certificate to present at now (false if issuing fails). + bool issue(certificate::data& out, uint32_t now) const NOEXCEPT; + +private: + context(const system::ec_secret& static_secret, + const certificate::data& issued, + const system::ec_secret& authority, uint32_t validity, + bool issuing) NOEXCEPT; + + const system::ec_secret static_secret_; + const certificate::data certificate_; + const system::ec_secret authority_secret_; + const uint32_t validity_; + cipher::key static_key_{}; + system::ec_xonly server_{}; + std::optional authority_{}; + bool valid_{}; +}; + +} // namespace noise +} // namespace network +} // namespace libbitcoin + +#endif diff --git a/include/bitcoin/network/noise/noise.hpp b/include/bitcoin/network/noise/noise.hpp index 0c859c305..32113f88d 100644 --- a/include/bitcoin/network/noise/noise.hpp +++ b/include/bitcoin/network/noise/noise.hpp @@ -19,6 +19,8 @@ #ifndef LIBBITCOIN_NETWORK_NOISE_NOISE_HPP #define LIBBITCOIN_NETWORK_NOISE_NOISE_HPP +#include #include +#include #endif diff --git a/src/noise/certificate.cpp b/src/noise/certificate.cpp new file mode 100644 index 000000000..8a1a5e985 --- /dev/null +++ b/src/noise/certificate.cpp @@ -0,0 +1,144 @@ +/** + * Copyright (c) 2011-2026 libbitcoin developers + * + * This file is part of libbitcoin. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#include + +#include + +namespace libbitcoin { +namespace network { +namespace noise { + +using namespace system; + +constexpr data_array authority_prefix{ 0x01, 0x00 }; +constexpr size_t checked_size = certificate::authority_size + checksum_default_size; + +static data_array header(uint32_t valid_from, + uint32_t not_valid_after) NOEXCEPT +{ + return splice(to_little_endian(certificate::version), + to_little_endian(valid_from), to_little_endian(not_valid_after)); +} + +static hash_digest signature_hash(const ec_xonly& server, uint32_t valid_from, + uint32_t not_valid_after) NOEXCEPT +{ + return sha256_hash2(header(valid_from, not_valid_after), server); +} + +bool certificate::to_xonly(ec_xonly& out, const ec_secret& secret) NOEXCEPT +{ + ec_compressed point{}; + if (!secret_to_public(point, secret)) + return false; + + out = to_xonly(point); + return true; +} + +bool certificate::to_xonly(ec_xonly& out, const cipher::key& key) NOEXCEPT +{ + ec_compressed point{}; + if (!ellswift::decode(point, key)) + return false; + + out = to_xonly(point); + return true; +} + +ec_xonly certificate::to_xonly(const ec_compressed& key) NOEXCEPT +{ + return slice(key); +} + +bool certificate::sign(data& out, const ec_secret& authority, + const ec_xonly& server, uint32_t valid_from, + uint32_t not_valid_after) NOEXCEPT +{ + hash_digest auxiliary{}; + maybe_random::fill(auxiliary); + return sign(out, authority, server, valid_from, not_valid_after, + auxiliary); +} + +bool certificate::sign(data& out, const ec_secret& authority, + const ec_xonly& server, uint32_t valid_from, uint32_t not_valid_after, + const hash_digest& auxiliary) NOEXCEPT +{ + ec_signature signature{}; + const auto hash = signature_hash(server, valid_from, not_valid_after); + if (!schnorr::sign(signature, authority, hash, auxiliary)) + return false; + + out = splice(header(valid_from, not_valid_after), signature); + return true; +} + +bool certificate::verify(const data& in, const ec_xonly& server, + const ec_xonly& authority, uint32_t now) NOEXCEPT +{ + const auto from = valid_from(in); + const auto after = not_valid_after(in); + if (from_little_array(slice(in)) != + version || now < from || now > after) + return false; + + const auto hash = signature_hash(server, from, after); + const auto signature = slice(in); + return schnorr::verify_signature(authority, hash, signature); +} + +uint32_t certificate::valid_from(const data& in) NOEXCEPT +{ + constexpr auto start = sizeof(uint16_t); + return from_little_array( + slice(in)); +} + +uint32_t certificate::not_valid_after(const data& in) NOEXCEPT +{ + constexpr auto start = sizeof(uint16_t) + sizeof(uint32_t); + return from_little_array( + slice(in)); +} + +std::string certificate::encode_authority(const ec_xonly& authority) NOEXCEPT +{ + return encode_base58(insert_checksum( + { + authority_prefix, + authority + })); +} + +bool certificate::decode_authority(ec_xonly& out, + const std::string& text) NOEXCEPT +{ + data_array checked{}; + if (!decode_base58(checked, text) || !verify_checksum(checked) || + slice(checked) != authority_prefix) + return false; + + out = slice(checked); + return true; +} + +} // namespace noise +} // namespace network +} // namespace libbitcoin diff --git a/src/noise/context.cpp b/src/noise/context.cpp new file mode 100644 index 000000000..e825e7926 --- /dev/null +++ b/src/noise/context.cpp @@ -0,0 +1,101 @@ +/** + * Copyright (c) 2011-2026 libbitcoin developers + * + * This file is part of libbitcoin. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#include + +#include + +namespace libbitcoin { +namespace network { +namespace noise { + +using namespace system; + +context::context(const ec_secret& static_secret, + const certificate::data& issued) NOEXCEPT + : context(static_secret, issued, {}, zero, false) +{ +} + +context::context(const ec_secret& static_secret, const ec_secret& authority, + uint32_t validity) NOEXCEPT + : context(static_secret, {}, authority, validity, true) +{ +} + +context::context(const ec_secret& static_secret, + const certificate::data& issued, const ec_secret& authority, + uint32_t validity, bool issuing) NOEXCEPT + : static_secret_(static_secret), + certificate_(issued), + authority_secret_(authority), + validity_(validity) +{ + hash_digest auxiliary{}; + maybe_random::fill(auxiliary); + if (!certificate::to_xonly(server_, static_secret_) || + !ellswift::create(static_key_, static_secret_, auxiliary)) + return; + + if (issuing) + { + ec_xonly key{}; + if (!certificate::to_xonly(key, authority_secret_)) + return; + + authority_.emplace(key); + } + + valid_ = true; +} + +bool context::valid() const NOEXCEPT +{ + return valid_; +} + +const ec_secret& context::static_secret() const NOEXCEPT +{ + return static_secret_; +} + +const cipher::key& context::static_key() const NOEXCEPT +{ + return static_key_; +} + +const std::optional& context::authority() const NOEXCEPT +{ + return authority_; +} + +bool context::issue(certificate::data& out, uint32_t now) const NOEXCEPT +{ + if (!authority_.has_value()) + { + out = certificate_; + return true; + } + + return certificate::sign(out, authority_secret_, server_, + floored_subtract(now, validity_), ceilinged_add(now, validity_)); +} + +} // namespace noise +} // namespace network +} // namespace libbitcoin diff --git a/test/noise/certificate.cpp b/test/noise/certificate.cpp new file mode 100644 index 000000000..76f709010 --- /dev/null +++ b/test/noise/certificate.cpp @@ -0,0 +1,181 @@ +/** + * Copyright (c) 2011-2026 libbitcoin developers + * + * This file is part of libbitcoin. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#include "../test.hpp" +#include "vectors.hpp" + +BC_AUTO_TEST_SUITE(noise_certificate_tests) + +using namespace network::noise; +using namespace system; + +static ec_xonly server_xonly() +{ + ec_xonly out{}; + certificate::to_xonly(out, sv2_noise::static_ellswift); + return out; +} + +static bool verify_at(uint32_t now) +{ + return certificate::verify(sv2_noise::signature_noise_message, + server_xonly(), sv2_noise::authority_xonly, now); +} + +static bool verify_tampered(size_t index) +{ + auto tampered = sv2_noise::signature_noise_message; + tampered.at(index) ^= 1u; + return certificate::verify(tampered, server_xonly(), + sv2_noise::authority_xonly, sv2_noise::valid_from); +} + +static std::string encode_prefixed(uint8_t first, uint8_t second) +{ + const data_array<2> prefix{ first, second }; + return encode_base58(insert_checksum<38>( + { + prefix, + sv2_noise::authority_xonly + })); +} + +BC_AUTO_TEST_CASE(noise_certificate__to_xonly__authority_secret__authority_xonly) +{ + ec_xonly out{}; + const auto result = certificate::to_xonly(out, sv2_noise::authority_secret); + BC_REQUIRE(result); + BC_REQUIRE_EQUAL(out, sv2_noise::authority_xonly); +} + +BC_AUTO_TEST_CASE(noise_certificate__to_xonly__invalid_secret__false) +{ + ec_xonly out{}; + BC_REQUIRE(!certificate::to_xonly(out, ec_secret{})); +} + +BC_AUTO_TEST_CASE(noise_certificate__to_xonly__static_ellswift__static_secret_xonly) +{ + ec_xonly expected{}; + ec_xonly out{}; + const auto from_secret = certificate::to_xonly(expected, sv2_noise::static_secret); + const auto from_key = certificate::to_xonly(out, sv2_noise::static_ellswift); + BC_REQUIRE(from_secret); + BC_REQUIRE(from_key); + BC_REQUIRE_EQUAL(out, expected); +} + +BC_AUTO_TEST_CASE(noise_certificate__sign__vector_inputs__signature_noise_message) +{ + certificate::data out{}; + const auto result = certificate::sign(out, sv2_noise::authority_secret, server_xonly(), sv2_noise::valid_from, sv2_noise::not_valid_after, sv2_noise::certificate_aux); + BC_REQUIRE(result); + BC_REQUIRE_EQUAL(out, sv2_noise::signature_noise_message); +} + +BC_AUTO_TEST_CASE(noise_certificate__sign__random_auxiliary__verifies) +{ + certificate::data out{}; + const auto issued = certificate::sign(out, sv2_noise::authority_secret, server_xonly(), sv2_noise::valid_from, sv2_noise::not_valid_after); + const auto verified = certificate::verify(out, server_xonly(), sv2_noise::authority_xonly, sv2_noise::valid_from); + BC_REQUIRE(issued); + BC_REQUIRE(verified); +} + +BC_AUTO_TEST_CASE(noise_certificate__sign__invalid_authority__false) +{ + certificate::data out{}; + BC_REQUIRE(!certificate::sign(out, ec_secret{}, server_xonly(), sv2_noise::valid_from, sv2_noise::not_valid_after)); +} + +BC_AUTO_TEST_CASE(noise_certificate__window__vector__vector_window) +{ + BC_REQUIRE_EQUAL(certificate::valid_from(sv2_noise::signature_noise_message), sv2_noise::valid_from); + BC_REQUIRE_EQUAL(certificate::not_valid_after(sv2_noise::signature_noise_message), sv2_noise::not_valid_after); +} + +BC_AUTO_TEST_CASE(noise_certificate__verify__window_bounds__true) +{ + BC_REQUIRE(verify_at(sv2_noise::valid_from)); + BC_REQUIRE(verify_at(sv2_noise::not_valid_after)); +} + +BC_AUTO_TEST_CASE(noise_certificate__verify__outside_window__false) +{ + BC_REQUIRE(!verify_at(sv2_noise::valid_from - 1u)); + BC_REQUIRE(!verify_at(sv2_noise::not_valid_after + 1u)); +} + +BC_AUTO_TEST_CASE(noise_certificate__verify__other_authority__false) +{ + BC_REQUIRE(!certificate::verify(sv2_noise::signature_noise_message, server_xonly(), server_xonly(), sv2_noise::valid_from)); +} + +BC_AUTO_TEST_CASE(noise_certificate__verify__other_server__false) +{ + BC_REQUIRE(!certificate::verify(sv2_noise::signature_noise_message, sv2_noise::authority_xonly, sv2_noise::authority_xonly, sv2_noise::valid_from)); +} + +BC_AUTO_TEST_CASE(noise_certificate__verify__tampered_version__false) +{ + BC_REQUIRE(!verify_tampered(0)); +} + +BC_AUTO_TEST_CASE(noise_certificate__verify__tampered_signature__false) +{ + BC_REQUIRE(!verify_tampered(10)); +} + +BC_AUTO_TEST_CASE(noise_certificate__encode_authority__vector__vector_key) +{ + BC_REQUIRE_EQUAL(certificate::encode_authority(sv2_noise::authority_xonly), sv2_noise::authority_key); +} + +BC_AUTO_TEST_CASE(noise_certificate__encode_authority__specification_key__specification_encoding) +{ + BC_REQUIRE_EQUAL(certificate::encode_authority(base16_array("76637000979c1c11af0c300bcd8c7fe48610fce9b9c11e3daee35ae0b08a7455")), "9bXiEd8boQVhq7WddEcERUL5tyyJVFYdU8th3HfbNXK3Yw6GRXh"); +} + +BC_AUTO_TEST_CASE(noise_certificate__decode_authority__vector_key__authority_xonly) +{ + ec_xonly out{}; + const auto result = certificate::decode_authority(out, sv2_noise::authority_key); + BC_REQUIRE(result); + BC_REQUIRE_EQUAL(out, sv2_noise::authority_xonly); +} + +BC_AUTO_TEST_CASE(noise_certificate__decode_authority__other_prefix__false) +{ + ec_xonly out{}; + const auto encoded = encode_prefixed(0x02, 0x00); + BC_REQUIRE(!certificate::decode_authority(out, encoded)); +} + +BC_AUTO_TEST_CASE(noise_certificate__decode_authority__bad_checksum__false) +{ + ec_xonly out{}; + BC_REQUIRE(!certificate::decode_authority(out, "9br3pDEyhsQoXmojoXVRsNGBa8xNw1F35npYzXM5yWDfZaxjaEp")); +} + +BC_AUTO_TEST_CASE(noise_certificate__decode_authority__malformed__false) +{ + ec_xonly out{}; + BC_REQUIRE(!certificate::decode_authority(out, "0OIl")); +} + +BC_AUTO_TEST_SUITE_END() diff --git a/test/noise/context.cpp b/test/noise/context.cpp new file mode 100644 index 000000000..541ed986e --- /dev/null +++ b/test/noise/context.cpp @@ -0,0 +1,104 @@ +/** + * Copyright (c) 2011-2026 libbitcoin developers + * + * This file is part of libbitcoin. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#include "../test.hpp" +#include "vectors.hpp" + +BC_AUTO_TEST_SUITE(noise_context_tests) + +using namespace network::noise; +using namespace system; + +constexpr uint32_t validity = 3600; +constexpr uint32_t now = 1800000000; + +static ec_xonly to_xonly(const cipher::key& key) +{ + ec_xonly out{}; + certificate::to_xonly(out, key); + return out; +} + +static ec_xonly to_xonly(const ec_secret& secret) +{ + ec_xonly out{}; + certificate::to_xonly(out, secret); + return out; +} + +BC_AUTO_TEST_CASE(noise_context__construct__invalid_static_secret__invalid) +{ + const context instance{ ec_secret{}, sv2_noise::signature_noise_message }; + BC_REQUIRE(!instance.valid()); +} + +BC_AUTO_TEST_CASE(noise_context__construct__invalid_authority_secret__invalid) +{ + const context instance{ sv2_noise::static_secret, ec_secret{}, validity }; + BC_REQUIRE(!instance.valid()); +} + +BC_AUTO_TEST_CASE(noise_context__construct__pre_issued__static_keypair) +{ + const context instance{ sv2_noise::static_secret, sv2_noise::signature_noise_message }; + BC_REQUIRE(instance.valid()); + BC_REQUIRE(!instance.authority().has_value()); + BC_REQUIRE_EQUAL(instance.static_secret(), sv2_noise::static_secret); + BC_REQUIRE_EQUAL(to_xonly(instance.static_key()), to_xonly(sv2_noise::static_secret)); +} + +BC_AUTO_TEST_CASE(noise_context__issue__pre_issued__pre_issued_certificate) +{ + const context instance{ sv2_noise::static_secret, sv2_noise::signature_noise_message }; + certificate::data out{}; + const auto result = instance.issue(out, now); + BC_REQUIRE(result); + BC_REQUIRE_EQUAL(out, sv2_noise::signature_noise_message); +} + +BC_AUTO_TEST_CASE(noise_context__construct__issuing__authority_key) +{ + const context instance{ sv2_noise::static_secret, sv2_noise::authority_secret, validity }; + BC_REQUIRE(instance.valid()); + BC_REQUIRE(instance.authority().has_value()); + BC_REQUIRE_EQUAL(instance.authority().value(), sv2_noise::authority_xonly); +} + +BC_AUTO_TEST_CASE(noise_context__issue__issuing__verifiable_window_around_now) +{ + const context instance{ sv2_noise::static_secret, sv2_noise::authority_secret, validity }; + certificate::data out{}; + const auto result = instance.issue(out, now); + const auto verified = certificate::verify(out, to_xonly(instance.static_key()), sv2_noise::authority_xonly, now); + BC_REQUIRE(result); + BC_REQUIRE(verified); + BC_REQUIRE_EQUAL(certificate::valid_from(out), now - validity); + BC_REQUIRE_EQUAL(certificate::not_valid_after(out), now + validity); +} + +BC_AUTO_TEST_CASE(noise_context__issue__issuing_near_zero__floored_window) +{ + const context instance{ sv2_noise::static_secret, sv2_noise::authority_secret, validity }; + certificate::data out{}; + const auto result = instance.issue(out, 42); + BC_REQUIRE(result); + BC_REQUIRE_EQUAL(certificate::valid_from(out), 0u); + BC_REQUIRE_EQUAL(certificate::not_valid_after(out), 42u + validity); +} + +BC_AUTO_TEST_SUITE_END() From 1eaf26b27e44a1fbfe541f9ea07f4ee3cbb4f3a9 Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Thu, 26 Nov 2026 21:53:51 -0500 Subject: [PATCH 5/9] Reject a reversed sv2 certificate window. --- include/bitcoin/network/noise/certificate.hpp | 4 +++- src/noise/certificate.cpp | 3 +++ test/noise/certificate.cpp | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/include/bitcoin/network/noise/certificate.hpp b/include/bitcoin/network/noise/certificate.hpp index 4fe486388..8823320b9 100644 --- a/include/bitcoin/network/noise/certificate.hpp +++ b/include/bitcoin/network/noise/certificate.hpp @@ -54,11 +54,13 @@ class BCT_API certificate final static system::ec_xonly to_xonly(const system::ec_compressed& key) NOEXCEPT; /// Sign the server key over the window, with random auxiliary data. + /// False if the window is reversed or the authority secret is invalid. static bool sign(data& out, const system::ec_secret& authority, const system::ec_xonly& server, uint32_t valid_from, uint32_t not_valid_after) NOEXCEPT; - /// Sign the server key over the window (deterministic, for test vectors). + /// Sign the server key over the window with the auxiliary data. + /// False if the window is reversed or the authority secret is invalid. static bool sign(data& out, const system::ec_secret& authority, const system::ec_xonly& server, uint32_t valid_from, uint32_t not_valid_after, const system::hash_digest& auxiliary) NOEXCEPT; diff --git a/src/noise/certificate.cpp b/src/noise/certificate.cpp index 8a1a5e985..d2f540250 100644 --- a/src/noise/certificate.cpp +++ b/src/noise/certificate.cpp @@ -81,6 +81,9 @@ bool certificate::sign(data& out, const ec_secret& authority, const ec_xonly& server, uint32_t valid_from, uint32_t not_valid_after, const hash_digest& auxiliary) NOEXCEPT { + if (not_valid_after < valid_from) + return false; + ec_signature signature{}; const auto hash = signature_hash(server, valid_from, not_valid_after); if (!schnorr::sign(signature, authority, hash, auxiliary)) diff --git a/test/noise/certificate.cpp b/test/noise/certificate.cpp index 76f709010..01e85c620 100644 --- a/test/noise/certificate.cpp +++ b/test/noise/certificate.cpp @@ -88,6 +88,21 @@ BC_AUTO_TEST_CASE(noise_certificate__sign__vector_inputs__signature_noise_messag BC_REQUIRE_EQUAL(out, sv2_noise::signature_noise_message); } +BC_AUTO_TEST_CASE(noise_certificate__sign__zero_auxiliary__expected) +{ + const auto expected = base16_array("000000f1536500b33f7143d817e25999d8c2d63c7722304f611b02535ea53ec0fcf8f3bc14b426ebf5990f4d9200a5967b40ab859c7e6fe7e6203b432f0bd9df522125781f878e9bc45e"); + certificate::data out{}; + const auto result = certificate::sign(out, sv2_noise::authority_secret, server_xonly(), sv2_noise::valid_from, sv2_noise::not_valid_after, hash_digest{}); + BC_REQUIRE(result); + BC_REQUIRE_EQUAL(out, expected); +} + +BC_AUTO_TEST_CASE(noise_certificate__sign__reversed_window__false) +{ + certificate::data out{}; + BC_REQUIRE(!certificate::sign(out, sv2_noise::authority_secret, server_xonly(), sv2_noise::not_valid_after, sv2_noise::valid_from, hash_digest{})); +} + BC_AUTO_TEST_CASE(noise_certificate__sign__random_auxiliary__verifies) { certificate::data out{}; From ddfbca065e48900d1c961dc6577882d751992f72 Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Thu, 26 Nov 2026 23:00:06 -0500 Subject: [PATCH 6/9] Add advertised urls to http server settings. --- include/bitcoin/network/settings.hpp | 6 +++ src/settings.cpp | 38 ++++++++++++++++++ test/settings.cpp | 60 ++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) diff --git a/include/bitcoin/network/settings.hpp b/include/bitcoin/network/settings.hpp index 391977b8f..03c535b11 100644 --- a/include/bitcoin/network/settings.hpp +++ b/include/bitcoin/network/settings.hpp @@ -244,9 +244,15 @@ struct BCT_API settings /// Initialize the tls::context and normalize hosts/origins. code initialize_context() NOEXCEPT override; + /// Set the default port of bindings and advertisements without one. + void default_ports() NOEXCEPT override; + /// Sent via responses if configured. std::string server{ BC_HTTP_SERVER_NAME }; + /// Public http/https urls of the service (ws/wss on the same port). + system::config::urls advertise{}; + /// Validated against hosts/origins if configured. config::endpoints hosts{}; config::endpoints origins{}; diff --git a/src/settings.cpp b/src/settings.cpp index ac5c3ecba..f303a6a37 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -384,11 +384,49 @@ settings::transport settings::zmtp_server::secure_context() const NOEXCEPT // http_server // ---------------------------------------------------------------------------- +// An advertisement without scheme or port is both http and https, and one +// without port takes the default port of its scheme. +void settings::http_server::default_ports() NOEXCEPT +{ + tls_server::default_ports(); + + system::config::urls urls{}; + for (const auto& url: advertise) + { + const auto& scheme = url.scheme(); + const auto& host = url.host(); + if (!is_zero(url.port())) + { + urls.push_back(url); + } + else if (scheme.empty()) + { + urls.emplace_back("http", host, bind_port); + urls.emplace_back("https", host, safe_port); + } + else + { + urls.emplace_back(scheme, host, + scheme == "https" ? safe_port : bind_port); + } + } + + advertise = std::move(urls); +} + code settings::http_server::initialize_context() NOEXCEPT { if (const auto ec = tls_server::initialize_context()) return ec; + const auto unserved = [](const system::config::url& url) NOEXCEPT + { + return url.scheme() != "http" && url.scheme() != "https"; + }; + + if (std::ranges::any_of(advertise, unserved)) + return error::invalid_configuration; + host_names_ = config::to_host_names(hosts); origin_names_ = config::to_host_names(origins); return error::success; diff --git a/test/settings.cpp b/test/settings.cpp index 3f061aa4f..fde474529 100644 --- a/test/settings.cpp +++ b/test/settings.cpp @@ -512,6 +512,65 @@ BC_AUTO_TEST_CASE(settings__http_server_initialize_context__portless_hosts__any_ BC_REQUIRE(!config::contains_origin(instance.origin_names(), "http://localhost:8780", 80)); } +BC_AUTO_TEST_CASE(settings__http_server_default_ports__schemeless_portless_advertise__http_and_https) +{ + settings::http_server instance{ "test" }; + instance.bind_port = 8880; + instance.safe_port = 8843; + instance.advertise = { system::config::url{ "example.com" } }; + instance.default_ports(); + BC_REQUIRE_EQUAL(instance.advertise.size(), 2u); + BC_REQUIRE_EQUAL(instance.advertise.at(0).to_uri(), "http://example.com:8880"); + BC_REQUIRE_EQUAL(instance.advertise.at(1).to_uri(), "https://example.com:8843"); +} + +BC_AUTO_TEST_CASE(settings__http_server_default_ports__portless_advertise__scheme_default_port) +{ + settings::http_server instance{ "test" }; + instance.bind_port = 8880; + instance.safe_port = 8843; + instance.advertise = { system::config::url{ "http://1.2.3.4" }, system::config::url{ "https://example.com" } }; + instance.default_ports(); + BC_REQUIRE_EQUAL(instance.advertise.size(), 2u); + BC_REQUIRE_EQUAL(instance.advertise.at(0).to_uri(), "http://1.2.3.4:8880"); + BC_REQUIRE_EQUAL(instance.advertise.at(1).to_uri(), "https://example.com:8843"); +} + +BC_AUTO_TEST_CASE(settings__http_server_default_ports__ported_advertise__unchanged) +{ + settings::http_server instance{ "test" }; + instance.bind_port = 8880; + instance.safe_port = 8843; + instance.advertise = { system::config::url{ "https://example.com:443" } }; + instance.default_ports(); + BC_REQUIRE_EQUAL(instance.advertise.size(), 1u); + BC_REQUIRE_EQUAL(instance.advertise.at(0).to_uri(), "https://example.com:443"); +} + +BC_AUTO_TEST_CASE(settings__http_server_initialize_context__defaulted_advertise__success) +{ + settings::http_server instance{ "test" }; + instance.advertise = { system::config::url{ "example.com" }, system::config::url{ "https://example.com:443" } }; + instance.default_ports(); + BC_REQUIRE_EQUAL(instance.initialize_context(), error::success); +} + +BC_AUTO_TEST_CASE(settings__http_server_initialize_context__schemeless_ported_advertise__invalid_configuration) +{ + settings::http_server instance{ "test" }; + instance.advertise = { system::config::url{ "example.com:8880" } }; + instance.default_ports(); + BC_REQUIRE_EQUAL(instance.initialize_context(), error::invalid_configuration); +} + +BC_AUTO_TEST_CASE(settings__http_server_initialize_context__tcp_advertise__invalid_configuration) +{ + settings::http_server instance{ "test" }; + instance.advertise = { system::config::url{ "tcp://example.com" } }; + instance.default_ports(); + BC_REQUIRE_EQUAL(instance.initialize_context(), error::invalid_configuration); +} + BC_AUTO_TEST_CASE(settings__tls_server__defaults__expected) { constexpr auto name = "test"; @@ -573,6 +632,7 @@ BC_AUTO_TEST_CASE(settings__http_server__defaults__expected) BC_REQUIRE(instance.compression); BC_REQUIRE(!instance.authorize()); BC_REQUIRE(instance.credentials.empty()); + BC_REQUIRE(instance.advertise.empty()); constexpr auto digest = system::base16_array("d9fa291efa0c924851f3ea14e73b1847506ab451fb834b7101e5a1a88f94f500"); BC_REQUIRE_EQUAL(config::credential{ ":" }.digest(), digest); From 00909f0d4c93bac5ebe0e52ed6329c557a66526d Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Fri, 27 Nov 2026 03:05:14 -0500 Subject: [PATCH 7/9] Decode an sv2 authority key payload. --- include/bitcoin/network/noise/certificate.hpp | 4 +++ src/noise/certificate.cpp | 14 ++++++++++ test/noise/certificate.cpp | 28 +++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/include/bitcoin/network/noise/certificate.hpp b/include/bitcoin/network/noise/certificate.hpp index 8823320b9..68cd816eb 100644 --- a/include/bitcoin/network/noise/certificate.hpp +++ b/include/bitcoin/network/noise/certificate.hpp @@ -80,6 +80,10 @@ class BCT_API certificate final /// Decode a published authority key (false if malformed). static bool decode_authority(system::ec_xonly& out, const std::string& text) NOEXCEPT; + + /// The key of a published authority key payload (false if malformed). + static bool to_authority(system::ec_xonly& out, + const system::data_slice& payload) NOEXCEPT; }; } // namespace noise diff --git a/src/noise/certificate.cpp b/src/noise/certificate.cpp index d2f540250..03102d30b 100644 --- a/src/noise/certificate.cpp +++ b/src/noise/certificate.cpp @@ -142,6 +142,20 @@ bool certificate::decode_authority(ec_xonly& out, return true; } +bool certificate::to_authority(ec_xonly& out, + const data_slice& payload) NOEXCEPT +{ + if (payload.size() != authority_size) + return false; + + const auto key = payload.to_array(); + if (slice(key) != authority_prefix) + return false; + + out = slice(key); + return true; +} + } // namespace noise } // namespace network } // namespace libbitcoin diff --git a/test/noise/certificate.cpp b/test/noise/certificate.cpp index 01e85c620..3c22344f8 100644 --- a/test/noise/certificate.cpp +++ b/test/noise/certificate.cpp @@ -55,6 +55,14 @@ static std::string encode_prefixed(uint8_t first, uint8_t second) })); } +static data_chunk authority_payload(uint8_t first, size_t size) +{ + auto payload = to_chunk(splice(data_array<2>{ first, 0x00 }, + sv2_noise::authority_xonly)); + payload.resize(size); + return payload; +} + BC_AUTO_TEST_CASE(noise_certificate__to_xonly__authority_secret__authority_xonly) { ec_xonly out{}; @@ -193,4 +201,24 @@ BC_AUTO_TEST_CASE(noise_certificate__decode_authority__malformed__false) BC_REQUIRE(!certificate::decode_authority(out, "0OIl")); } +BC_AUTO_TEST_CASE(noise_certificate__to_authority__vector_payload__authority_xonly) +{ + ec_xonly out{}; + const auto result = certificate::to_authority(out, authority_payload(0x01, certificate::authority_size)); + BC_REQUIRE(result); + BC_REQUIRE_EQUAL(out, sv2_noise::authority_xonly); +} + +BC_AUTO_TEST_CASE(noise_certificate__to_authority__other_prefix__false) +{ + ec_xonly out{}; + BC_REQUIRE(!certificate::to_authority(out, authority_payload(0x02, certificate::authority_size))); +} + +BC_AUTO_TEST_CASE(noise_certificate__to_authority__short_payload__false) +{ + ec_xonly out{}; + BC_REQUIRE(!certificate::to_authority(out, authority_payload(0x01, sub1(certificate::authority_size)))); +} + BC_AUTO_TEST_SUITE_END() From 71c698ff457440527ffa900b67a3d78b32f6eefa Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Thu, 26 Nov 2026 23:55:23 -0500 Subject: [PATCH 8/9] Add sv2 Noise NX handshake and transport cipher. --- src/noise/cipher.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/noise/cipher.cpp b/src/noise/cipher.cpp index bd4fec31c..aa4f598aa 100644 --- a/src/noise/cipher.cpp +++ b/src/noise/cipher.cpp @@ -165,6 +165,7 @@ bool cipher::complete(key& static_key, certificate& proof, bool cipher::encrypt(const std::span& out, const span& frame) NOEXCEPT { + BC_ASSERT(send_.has_value()); BC_ASSERT(frame.size() >= heading_size); const auto payload = frame.subspan(heading_size); BC_ASSERT(out.size() == sealed_heading_size + sealed_size(payload.size())); @@ -190,6 +191,7 @@ bool cipher::encrypt(const std::span& out, const span& frame) NOEXCEPT bool cipher::decrypt_heading(const std::span& plain, const span& in) NOEXCEPT { + BC_ASSERT(receive_.has_value()); BC_ASSERT(plain.size() == heading_size); BC_ASSERT(in.size() == sealed_heading_size); return receive_->decrypt(plain, {}, in); @@ -198,6 +200,7 @@ bool cipher::decrypt_heading(const std::span& plain, bool cipher::decrypt_payload(const std::span& plain, const span& in) NOEXCEPT { + BC_ASSERT(receive_.has_value()); BC_ASSERT(in.size() == sealed_size(plain.size())); auto sealed = in; From e8e6a24d399657cbb1e327ce252ca2215ca62a5e Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Thu, 26 Nov 2026 23:55:24 -0500 Subject: [PATCH 9/9] Add sv2 Noise fixed-key vector tests. --- test/noise/vectors.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/test/noise/vectors.hpp b/test/noise/vectors.hpp index 2df68974a..54d625331 100644 --- a/test/noise/vectors.hpp +++ b/test/noise/vectors.hpp @@ -57,6 +57,7 @@ constexpr auto message2_ck = system::base16_array("2f31a1c5fd1c1847fff458a91e48c constexpr auto signature_noise_message = system::base16_array("000000f1536500b33f71f5c52e0c1de2080074aa5bba625860617bdd23bb549936165f70f8ae6011417e81750af780796072ceff32a40d3c20a641408de3de9963c8d9f9c9206a108817"); constexpr auto message1 = system::base16_array("5b19ef8a73dbd8cdefe0b3698b1ecde96697b2b2de6fc50bd5c3265e414bc615837f247b9ecd4d4c5530964706aa43bb73b2ddfc022c5729a522f5d312ee8436"); constexpr auto message2 = system::base16_array("56a621c640bbb918afe365c689a974f5dbbd63268279f02b7cc8c6e7518e282d6ee596534a476cc385978c4a9f5a0a19eff5fe45cc4a0f24e67f691b5e0d07e2bb880058852e92ad144116e557202a3f00049f71d64ed75ad5671387eaec057d71102a540a6b3067db06879349e3425c47e4abd35a787296ba02a456756750ae562a04e58e8a632088f563fdb33bcaf34f4ab700e421b5a33caa5e1d8ada6182abfa48db676fea706988927b2d0d402da7228831c204deeccead0faf8ae549464183691fe233b5642b92b3dc30b69189d280a28a3c52d78e8942a11680972c00dc4ca642d8fe9de21853"); + // Transport keys, initiator to responder and responder to initiator. constexpr auto initiator_key = system::base16_array("be02c290834bd33ed36d8db82dc3c76d62de47d2248724584c7ab285bfe9a922"); constexpr auto responder_key = system::base16_array("956d735e56fee69cb7ea5030c5b9073da0cd6f2b683c7275e3ff1482168bac15");