diff --git a/doc/generated/backends-aes_block.adoc b/doc/generated/backends-aes_block.adoc index 653c894..866a636 100644 --- a/doc/generated/backends-aes_block.adoc +++ b/doc/generated/backends-aes_block.adoc @@ -21,12 +21,12 @@ The backends of `+ciphers::aes128+`, `+aes192+`, `+aes256+`, from the test progr |OpenSSL |`+/webcpp/crypto//openssl+` |`++` -|native, wasip2, wasip3 (tested by `+aes_openssl+`, `+agreement_aes+`); not on emscripten: `+/webcpp/crypto//openssl+` stops the build there +|native, wasip2, wasip3 (tested by `+aes_openssl+`, `+agreement_aes+`, `+quic_rfc9001_openssl+`); not on emscripten: `+/webcpp/crypto//openssl+` stops the build there |ctaes |`+/third_party//ctaes+` |`++` -|native, wasip2, wasip3, emscripten (tested by `+aes_ctaes+`, `+agreement_aes+`) +|native, wasip2, wasip3, emscripten (tested by `+aes_ctaes+`, `+agreement_aes+`, `+quic_rfc9001_bearssl+`) |=== `+aes128<>+`, `+aes192<>+` and `+aes256<>+` name the default backend; `+gcm>+` names AES-GCM's. The default per target, whatever the program links, as <> gives it with its reasons and tests: diff --git a/doc/generated/backends-gcm.adoc b/doc/generated/backends-gcm.adoc index 1db54a1..a4be9c3 100644 --- a/doc/generated/backends-gcm.adoc +++ b/doc/generated/backends-gcm.adoc @@ -16,12 +16,12 @@ The backends of `+ciphers::gcm+`, `+aes128_gcm+`, `+aes256_gcm+`, from the test |BearSSL |`+/third_party//bearssl+` |`++` -|native, wasip2, wasip3, emscripten (tested by `+gcm_bearssl+`, `+gcm_wycheproof_bearssl+`, `+agreement_gcm+`, `+agreement_gcm_portable+`) +|native, wasip2, wasip3, emscripten (tested by `+gcm_bearssl+`, `+gcm_wycheproof_bearssl+`, `+agreement_gcm+`, `+agreement_gcm_portable+`, `+quic_rfc9001_bearssl+`) |OpenSSL |`+/webcpp/crypto//openssl+` |`++` -|native, wasip2, wasip3 (tested by `+gcm_openssl+`, `+gcm_wycheproof_openssl+`, `+agreement_gcm+`, `+agreement_gcm_portable+`); not on emscripten: `+/webcpp/crypto//openssl+` stops the build there +|native, wasip2, wasip3 (tested by `+gcm_openssl+`, `+gcm_wycheproof_openssl+`, `+agreement_gcm+`, `+agreement_gcm_portable+`, `+quic_rfc9001_openssl+`); not on emscripten: `+/webcpp/crypto//openssl+` stops the build there |ctaes |`+/third_party//ctaes+` diff --git a/doc/generated/matrix.adoc b/doc/generated/matrix.adoc index 84570ba..847de06 100644 --- a/doc/generated/matrix.adoc +++ b/doc/generated/matrix.adoc @@ -59,22 +59,22 @@ |`+ciphers::aes128+`, `+aes192+`, `+aes256+` |OpenSSL |native, wasip2, wasip3; not on emscripten: `+/webcpp/crypto//openssl+` stops the build there -|`+aes_openssl+`, `+agreement_aes+` +|`+aes_openssl+`, `+agreement_aes+`, `+quic_rfc9001_openssl+` |`+ciphers::aes128+`, `+aes192+`, `+aes256+` |ctaes |native, wasip2, wasip3, emscripten -|`+aes_ctaes+`, `+agreement_aes+` +|`+aes_ctaes+`, `+agreement_aes+`, `+quic_rfc9001_bearssl+` |`+ciphers::gcm+`, `+aes128_gcm+`, `+aes256_gcm+` |BearSSL |native, wasip2, wasip3, emscripten -|`+gcm_bearssl+`, `+gcm_wycheproof_bearssl+`, `+agreement_gcm+`, `+agreement_gcm_portable+` +|`+gcm_bearssl+`, `+gcm_wycheproof_bearssl+`, `+agreement_gcm+`, `+agreement_gcm_portable+`, `+quic_rfc9001_bearssl+` |`+ciphers::gcm+`, `+aes128_gcm+`, `+aes256_gcm+` |OpenSSL |native, wasip2, wasip3; not on emscripten: `+/webcpp/crypto//openssl+` stops the build there -|`+gcm_openssl+`, `+gcm_wycheproof_openssl+`, `+agreement_gcm+`, `+agreement_gcm_portable+` +|`+gcm_openssl+`, `+gcm_wycheproof_openssl+`, `+agreement_gcm+`, `+agreement_gcm_portable+`, `+quic_rfc9001_openssl+` |`+ciphers::gcm+`, `+aes128_gcm+`, `+aes256_gcm+` |ctaes diff --git a/doc/tables/crypto.json b/doc/tables/crypto.json index 9d9ba46..64597f8 100644 --- a/doc/tables/crypto.json +++ b/doc/tables/crypto.json @@ -324,8 +324,8 @@ "section": "crypto-block", "default": "`aes128<>`, `aes192<>` and `aes256<>` name the default backend; `gcm>` names AES-GCM's.", "backends": { - "openssl": {"tests": ["aes_openssl", "agreement_aes"]}, - "ctaes": {"tests": ["aes_ctaes", "agreement_aes"]}, + "openssl": {"tests": ["aes_openssl", "agreement_aes", "quic_rfc9001_openssl"]}, + "ctaes": {"tests": ["aes_ctaes", "agreement_aes", "quic_rfc9001_bearssl"]}, "bearssl": {"refused": "aes_on_bearssl"} } }, @@ -343,9 +343,9 @@ "default": "`aes128_gcm` and `aes256_gcm` name the default backend.", "backends": { "bearssl": {"tests": ["gcm_bearssl", "gcm_wycheproof_bearssl", "agreement_gcm", - "agreement_gcm_portable"]}, + "agreement_gcm_portable", "quic_rfc9001_bearssl"]}, "openssl": {"tests": ["gcm_openssl", "gcm_wycheproof_openssl", "agreement_gcm", - "agreement_gcm_portable"]}, + "agreement_gcm_portable", "quic_rfc9001_openssl"]}, "ctaes": {"refused": "gcm_on_ctaes"} } }, diff --git a/test/Jamfile b/test/Jamfile index 94237ae..eebc86f 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -923,6 +923,19 @@ crypto-run agreement_chacha_portable_emscripten : agreement_chacha_portable_test WEBCPP_CRYPTO_DETAIL_BEARSSL_PORTABLE : emscripten ; +# RFC 9001 Appendix A.1 to A.4, QUIC version 1's packet protection +# (support/quic_rfc9001.hpp; the change rfc9001-tests): the Initial keys +# through webcpp::hash's HKDF, both Initial packets protected and unprotected, +# a tampered one refused, the Retry integrity tag. On OpenSSL for AES-128-GCM +# and the header protection's block; on BearSSL, which has no block cipher, +# with ctaes for it; and on the defaults, aes128_gcm and aes128<>. +crypto-run quic_rfc9001_openssl : quic_rfc9001_openssl_test.cpp + : /webcpp/crypto//openssl : native wasip2 wasip3 ; +crypto-run quic_rfc9001_bearssl : quic_rfc9001_bearssl_test.cpp + : /third_party//bearssl /third_party//ctaes ; +crypto-run quic_rfc9001_default : quic_rfc9001_default_test.cpp + : /webcpp/crypto//gcm-default /webcpp/crypto//aes_block-default ; + # before and after every public header (spec sections 2.2 and 8.6), # with every backend linked: on MSVC (the CI's msvc lanes) the headers beside a # bare ; elsewhere the headers alone. diff --git a/test/quic_rfc9001_bearssl_test.cpp b/test/quic_rfc9001_bearssl_test.cpp new file mode 100644 index 0000000..08a2cbe --- /dev/null +++ b/test/quic_rfc9001_bearssl_test.cpp @@ -0,0 +1,28 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// RFC 9001 Appendix A.1 to A.4 (QUIC version 1 packet protection) on BearSSL: AES-128-GCM on +// backends::bearssl, which has no block cipher, and the header protection's AES-128 block on +// backends::ctaes, the pair the defaults make on WebAssembly (support/quic_rfc9001.hpp). + +#include +#include +#include +#include +#include + +#include "support/quic_rfc9001.hpp" + +#include + +int main() { + namespace ciphers = webcpp::crypto::ciphers; + using webcpp::crypto::backends::bearssl; + using webcpp::crypto::backends::ctaes; + webcpp::test::quic_rfc9001::check_appendix_a>, + ciphers::aes128>(); + return webcpp::test::exit_status(boost::report_errors()); +} diff --git a/test/quic_rfc9001_default_test.cpp b/test/quic_rfc9001_default_test.cpp new file mode 100644 index 0000000..3c0d6e8 --- /dev/null +++ b/test/quic_rfc9001_default_test.cpp @@ -0,0 +1,23 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// RFC 9001 Appendix A.1 to A.4 (QUIC version 1 packet protection) on the defaults a program +// writes, aes128_gcm and aes128<>: natively OpenSSL for both, on wasip2, wasip3 and Emscripten +// BearSSL and ctaes (support/quic_rfc9001.hpp). + +#include +#include +#include + +#include "support/quic_rfc9001.hpp" + +#include + +int main() { + namespace ciphers = webcpp::crypto::ciphers; + webcpp::test::quic_rfc9001::check_appendix_a>(); + return webcpp::test::exit_status(boost::report_errors()); +} diff --git a/test/quic_rfc9001_openssl_test.cpp b/test/quic_rfc9001_openssl_test.cpp new file mode 100644 index 0000000..09acb3b --- /dev/null +++ b/test/quic_rfc9001_openssl_test.cpp @@ -0,0 +1,25 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// RFC 9001 Appendix A.1 to A.4 (QUIC version 1 packet protection) on OpenSSL: AES-128-GCM and +// the header protection's AES-128 block both on backends::openssl (support/quic_rfc9001.hpp). + +#include +#include +#include +#include + +#include "support/quic_rfc9001.hpp" + +#include + +int main() { + namespace ciphers = webcpp::crypto::ciphers; + using webcpp::crypto::backends::openssl; + webcpp::test::quic_rfc9001::check_appendix_a>, + ciphers::aes128>(); + return webcpp::test::exit_status(boost::report_errors()); +} diff --git a/test/support/quic_rfc9001.hpp b/test/support/quic_rfc9001.hpp new file mode 100644 index 0000000..91c467b --- /dev/null +++ b/test/support/quic_rfc9001.hpp @@ -0,0 +1,403 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// QUIC version 1's packet protection (RFC 9001 sections 5.1 to 5.4 and 5.8) written with crypto's +// public API, and the checks of Appendix A.1 to A.4 over it (support/quic_rfc9001_vectors.hpp): +// the change rfc9001-tests. Each check is a template over the AEAD, a `gcm>`, and the +// header protection's block cipher, an `aes128`, so that each backend's program +// (quic_rfc9001__test.cpp) runs them all. Nothing here is crypto's API: TLS 1.3's +// HKDF-Expand-Label and QUIC's header parsing belong to the test (ruling R1). + +#ifndef WEBCPP_TEST_CRYPTO_SUPPORT_QUIC_RFC9001_HPP +#define WEBCPP_TEST_CRYPTO_SUPPORT_QUIC_RFC9001_HPP + +#include +#include +#include + +#include "../require.hpp" +#include "hex.hpp" +#include "quic_rfc9001_vectors.hpp" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace webcpp::test::quic_rfc9001 { + +/** A text's bytes, viewed. */ +inline std::span bytes_of(std::string_view text) { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast): a text's bytes, viewed + return {reinterpret_cast(text.data()), text.size()}; +} + +/** RFC 8446 section 7.1: the prefix of every HkdfLabel's label. */ +inline constexpr std::string_view tls13_prefix = "tls13 "; +/** Section 5.2: the client's Initial secret's label. */ +inline constexpr std::string_view client_in = "client in"; +/** Section 5.2: the server's Initial secret's label. */ +inline constexpr std::string_view server_in = "server in"; +/** Section 5.1: the AEAD key's label. */ +inline constexpr std::string_view quic_key = "quic key"; +/** Section 5.1: the IV's label. */ +inline constexpr std::string_view quic_iv = "quic iv"; +/** Section 5.1: the header protection key's label. */ +inline constexpr std::string_view quic_hp = "quic hp"; + +/** + * RFC 8446 section 7.1's HkdfLabel with an empty context, as five pieces that HKDF-Expand reads + * one after another: the output's length (2 bytes), the label's length (1 byte), "tls13 ", the + * label, the context's length (0). + */ +class hkdf_label { +public: + /** + * @param output_length The output's length, at most 255 bytes here. + * @param label The label, without "tls13 ". + */ + hkdf_label(std::size_t output_length, std::string_view label) + : output_length_{ + static_cast(output_length >> 8U), + static_cast(output_length & 0xffU), + }, + label_length_{static_cast(tls13_prefix.size() + label.size())}, + label_(bytes_of(label)) {} + + /** The pieces, pointing into this object and the label. */ + [[nodiscard]] std::array, 5> pieces() const { + return {output_length_, label_length_, bytes_of(tls13_prefix), label_, context_length_}; + } + + /** The pieces joined: the HkdfLabel's bytes. */ + [[nodiscard]] std::vector bytes() const { + std::vector joined; + for (std::span const piece : pieces()) { + joined.insert(joined.end(), piece.begin(), piece.end()); + } + return joined; + } + +private: + std::array output_length_; + std::array label_length_; + std::span label_; + std::array context_length_{}; +}; + +/** + * HKDF-Expand-Label(secret, label, "", N) over SHA-256 (RFC 8446 section 7.1), through + * webcpp::hash's HKDF-Expand over pieces, on hash's default backend (ruling R4). + */ +template +std::array expand_label(std::span secret, + std::string_view label) { + hkdf_label const info(N, label); + auto const info_pieces = info.pieces(); + std::array output{}; + auto const expanded = + webcpp::hash::hkdf_expand(output, secret, info_pieces); + require(BOOST_TEST(expanded.has_value())); + return output; +} + +/** The keys of one direction of a connection, as section 5.1 derives them from its secret. */ +struct packet_keys { + /** AES-128-GCM's key. */ + std::array key; + /** The nonce's base. */ + std::array iv; + /** The header protection's AES-128 key. */ + std::array hp; +}; + +/** Section 5.1: "quic key", "quic iv" and "quic hp" of `secret`. */ +inline packet_keys derive_packet_keys(std::span secret) { + return { + .key = expand_label<16>(secret, quic_key), + .iv = expand_label<12>(secret, quic_iv), + .hp = expand_label<16>(secret, quic_hp), + }; +} + +/** Section 5.3: the nonce, the IV with the packet number XORed into its last 8 bytes. */ +inline std::array packet_nonce(std::array const& iv, + std::uint64_t packet_number) { + std::array nonce = iv; + for (std::size_t i = 0; i < 8; ++i) { + nonce[11 - i] ^= static_cast(packet_number >> (8U * i)); + } + return nonce; +} + +/** Section 5.4.3: the first 5 bytes of AES-ECB(hp, sample), on the block cipher `Block`. */ +template +std::array header_protection_mask(std::span hp, + std::span sample) { + auto cipher = Block::from_key(hp); + require(BOOST_TEST(cipher.has_value())); + std::array block{}; + std::copy_n(sample.begin(), block.size(), block.begin()); + require(BOOST_TEST(cipher->encrypt(block).has_value())); + std::array mask{}; + std::copy_n(block.begin(), mask.size(), mask.begin()); + return mask; +} + +/** RFC 9000 section 16: the length of the variable-length integer at `bytes`, and its value. */ +inline std::size_t read_varint(std::span bytes, std::uint64_t& value) { + std::size_t const length = std::size_t{1} << (static_cast(bytes[0]) >> 6U); + value = bytes[0] & 0x3fU; + for (std::size_t i = 1; i < length; ++i) { + value = (value << 8U) | bytes[i]; + } + return length; +} + +/** + * RFC 9000 section 17.2.2: the offset of an Initial packet's Packet Number field, past the first + * byte, the version, both connection IDs, the token and the length. + */ +inline std::size_t initial_packet_number_offset(std::span packet) { + std::size_t offset = 1 + 4; + offset += 1 + packet[offset]; // the Destination Connection ID + offset += 1 + packet[offset]; // the Source Connection ID + std::uint64_t token_length = 0; + offset += read_varint(packet.subspan(offset), token_length); + offset += static_cast(token_length); + std::uint64_t length = 0; + offset += read_varint(packet.subspan(offset), length); + return offset; +} + +/** Section 5.4.1: the bits of the first byte header protection masks, by the header's form. */ +inline unsigned first_byte_mask(std::uint8_t first_byte) { + return (first_byte & 0x80U) != 0 ? 0x0fU : 0x1fU; +} + +/** + * Sections 5.3 and 5.4: protects an Initial packet, `header` (unprotected, ending with its packet + * number) followed by `payload`: seals the payload with the header as associated data, then + * masks the first byte and the packet number with the sample at 4 bytes past its start. + */ +template +std::vector protect(packet_keys const& keys, std::span header, + std::uint64_t packet_number, + std::span payload) { + std::vector packet(header.begin(), header.end()); + packet.insert(packet.end(), payload.begin(), payload.end()); + auto aead = Aead::from_key(keys.key); + require(BOOST_TEST(aead.has_value())); + auto const nonce = packet_nonce(keys.iv, packet_number); + auto const tag = + aead->seal(nonce, header, std::span(packet).subspan(header.size(), payload.size())); + require(BOOST_TEST(tag.has_value())); + packet.insert(packet.end(), tag->begin(), tag->end()); + + std::size_t const packet_number_length = (header[0] & 0x03U) + 1U; + std::size_t const packet_number_offset = header.size() - packet_number_length; + auto const mask = header_protection_mask( + keys.hp, std::span(packet).subspan(packet_number_offset + 4, 16)); + packet[0] ^= static_cast(mask[0] & first_byte_mask(packet[0])); + for (std::size_t i = 0; i < packet_number_length; ++i) { + packet[packet_number_offset + i] ^= mask[1 + i]; + } + return packet; +} + +/** What `unprotect` gives back. */ +struct unprotected_packet { + /** The header, its first byte and packet number unmasked. */ + std::vector header; + /** The payload, opened in place; zeroed when the open failed. */ + std::vector payload; + /** The packet number as the packet carries it (RFC 9001's examples need no reconstruction). */ + std::uint64_t packet_number = 0; + /** The open's result. */ + boost::system::result opened; +}; + +/** + * Sections 5.3 and 5.4, as a receiver: removes an Initial packet's header protection, the packet + * number's length read from the unmasked first byte, then opens the payload. + */ +template +unprotected_packet unprotect(packet_keys const& keys, std::span packet) { + std::size_t const packet_number_offset = initial_packet_number_offset(packet); + auto const mask = + header_protection_mask(keys.hp, packet.subspan(packet_number_offset + 4, 16)); + auto const first_byte = static_cast(static_cast(packet[0]) ^ + (mask[0] & first_byte_mask(packet[0]))); + std::size_t const packet_number_length = (first_byte & 0x03U) + 1U; + std::size_t const header_size = packet_number_offset + packet_number_length; + + unprotected_packet result{ + .header = {packet.begin(), packet.begin() + header_size}, + .payload = {packet.begin() + header_size, packet.end() - 16}, + .opened = {}, + }; + result.header[0] = first_byte; + for (std::size_t i = 0; i < packet_number_length; ++i) { + result.header[packet_number_offset + i] ^= mask[1 + i]; + result.packet_number = + (result.packet_number << 8U) | result.header[packet_number_offset + i]; + } + auto aead = Aead::from_key(keys.key); + require(BOOST_TEST(aead.has_value())); + auto const nonce = packet_nonce(keys.iv, result.packet_number); + result.opened = aead->open(nonce, result.header, result.payload, packet.last(16)); + return result; +} + +/** A.2's payload: the CRYPTO frame, then PADDING frames (zero bytes) to 1,162 bytes. */ +inline std::vector client_payload() { + std::vector payload = from_hex(client_crypto_frame); + payload.resize(client_payload_size, 0); + return payload; +} + +/** A.1: the HkdfLabels, the initial secret and both directions' secrets, keys, IVs and hp. */ +inline void check_keys() { + BOOST_TEST_EQ(to_hex(hkdf_label(32, client_in).bytes()), to_hex(label_client_in)); + BOOST_TEST_EQ(to_hex(hkdf_label(32, server_in).bytes()), to_hex(label_server_in)); + BOOST_TEST_EQ(to_hex(hkdf_label(16, quic_key).bytes()), to_hex(label_quic_key)); + BOOST_TEST_EQ(to_hex(hkdf_label(12, quic_iv).bytes()), to_hex(label_quic_iv)); + BOOST_TEST_EQ(to_hex(hkdf_label(16, quic_hp).bytes()), to_hex(label_quic_hp)); + + auto const extracted = webcpp::hash::hkdf_extract( + initial_salt, client_destination_connection_id); + if (!BOOST_TEST(extracted.has_value())) { + return; + } + BOOST_TEST_EQ(to_hex(*extracted), to_hex(initial_secret)); + + for (auto const& [label, expected] : { + std::pair{client_in, &client_keys}, + std::pair{server_in, &server_keys}, + }) { + auto const secret = expand_label<32>(initial_secret, label); + BOOST_TEST_EQ(to_hex(secret), to_hex(expected->secret)); + auto const keys = derive_packet_keys(secret); + BOOST_TEST_EQ(to_hex(keys.key), to_hex(expected->key)); + BOOST_TEST_EQ(to_hex(keys.iv), to_hex(expected->iv)); + BOOST_TEST_EQ(to_hex(keys.hp), to_hex(expected->hp)); + } +} + +/** A.1's keys of one direction, as `protect` and `unprotect` take them. */ +inline packet_keys keys_of(initial_keys const& keys) { + return {.key = keys.key, .iv = keys.iv, .hp = keys.hp}; +} + +/** A.2: the client's Initial, its sample and mask, protected and unprotected. */ +template +void check_client_initial() { + auto const keys = keys_of(client_keys); + BOOST_TEST_EQ(to_hex(header_protection_mask(keys.hp, client_sample)), + to_hex(client_mask)); + auto const header = from_hex(client_header); + auto const payload = client_payload(); + auto const packet = protect(keys, header, client_packet_number, payload); + BOOST_TEST_EQ(to_hex(packet), std::string(client_packet)); + + auto const expected_packet = from_hex(client_packet); + auto const opened = unprotect(keys, expected_packet); + BOOST_TEST(opened.opened.has_value()); + BOOST_TEST_EQ(opened.packet_number, client_packet_number); + BOOST_TEST_EQ(to_hex(opened.header), std::string(client_header)); + BOOST_TEST_EQ(to_hex(opened.payload), to_hex(payload)); +} + +/** A.3: the server's Initial, its sample and mask, protected and unprotected. */ +template +void check_server_initial() { + auto const keys = keys_of(server_keys); + BOOST_TEST_EQ(to_hex(header_protection_mask(keys.hp, server_sample)), + to_hex(server_mask)); + auto const header = from_hex(server_header); + auto const payload = from_hex(server_payload); + auto const packet = protect(keys, header, server_packet_number, payload); + BOOST_TEST_EQ(to_hex(packet), std::string(server_packet)); + + auto const expected_packet = from_hex(server_packet); + auto const opened = unprotect(keys, expected_packet); + BOOST_TEST(opened.opened.has_value()); + BOOST_TEST_EQ(opened.packet_number, server_packet_number); + BOOST_TEST_EQ(to_hex(opened.header), std::string(server_header)); + BOOST_TEST_EQ(to_hex(opened.payload), std::string(server_payload)); +} + +/** A.2's packet with one bit of its protected payload flipped: refused, the payload zeroed. */ +template +void check_tampered_initial() { + auto packet = from_hex(client_packet); + packet[100] ^= 0x01U; + auto const opened = unprotect(keys_of(client_keys), packet); + BOOST_TEST(opened.opened.error() == webcpp::crypto::error::authentication_failed); + BOOST_TEST(std::ranges::all_of(opened.payload, [](std::uint8_t b) { return b == 0; })); +} + +/** + * Section 5.8's Retry pseudo-packet: the original Destination Connection ID's length and bytes, + * then the Retry packet without its integrity tag. + */ +inline std::vector retry_pseudo_packet( + std::span original_destination_connection_id) { + auto const retry = from_hex(retry_packet); + std::vector pseudo_packet{ + static_cast(original_destination_connection_id.size()), + }; + pseudo_packet.insert(pseudo_packet.end(), original_destination_connection_id.begin(), + original_destination_connection_id.end()); + pseudo_packet.insert(pseudo_packet.end(), retry.begin(), retry.end() - 16); + return pseudo_packet; +} + +/** A.4: the Retry integrity tag sealed, opened, and refused for another connection ID. */ +template +void check_retry() { + auto aead = Aead::from_key(retry_key); + if (!BOOST_TEST(aead.has_value())) { + return; + } + auto const retry = from_hex(retry_packet); + auto const expected_tag = std::span(retry).last(16); + auto const pseudo_packet = retry_pseudo_packet(client_destination_connection_id); + auto const tag = aead->seal(retry_nonce, pseudo_packet, std::span{}); + if (BOOST_TEST(tag.has_value())) { + BOOST_TEST_EQ(to_hex(*tag), to_hex(expected_tag)); + } + BOOST_TEST(aead->open(retry_nonce, pseudo_packet, std::span{}, expected_tag) + .has_value()); + + auto other_connection_id = client_destination_connection_id; + other_connection_id[0] ^= 0x01U; + BOOST_TEST(aead->open(retry_nonce, retry_pseudo_packet(other_connection_id), + std::span{}, expected_tag) + .error() == webcpp::crypto::error::authentication_failed); +} + +/** Every check of A.1 to A.4 on the AEAD `Aead` and the header protection's block `Block`. */ +template +void check_appendix_a() { + check_keys(); + check_client_initial(); + check_server_initial(); + check_tampered_initial(); + check_retry(); +} + +} // namespace webcpp::test::quic_rfc9001 + +#endif // WEBCPP_TEST_CRYPTO_SUPPORT_QUIC_RFC9001_HPP diff --git a/test/support/quic_rfc9001_vectors.hpp b/test/support/quic_rfc9001_vectors.hpp new file mode 100644 index 0000000..a52248c --- /dev/null +++ b/test/support/quic_rfc9001_vectors.hpp @@ -0,0 +1,178 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// RFC 9001 (Using TLS to Secure QUIC) Appendix A's sample packet protection, copied from the RFC +// Editor's text (https://www.rfc-editor.org/rfc/rfc9001.txt): section 5.2's initial salt, section +// 5.8's Retry key and nonce, and Appendix A.1 to A.4, each value named by its section. A.5 +// (ChaCha20-Poly1305) is not here: its header protection needs raw ChaCha20, which crypto does not +// expose (the change rfc9001-tests, ruling R7). + +#ifndef WEBCPP_TEST_CRYPTO_SUPPORT_QUIC_RFC9001_VECTORS_HPP +#define WEBCPP_TEST_CRYPTO_SUPPORT_QUIC_RFC9001_VECTORS_HPP + +#include "hex.hpp" + +#include +#include +#include +#include + +namespace webcpp::test::quic_rfc9001 { + +/** Section 5.2: the salt of QUIC version 1's initial secret. */ +inline constexpr auto initial_salt = hex<20>("38762cf7f55934b34d179ae6a4c80cadccbb7f0a"); + +/** Appendix A: the client's chosen Destination Connection ID. */ +inline constexpr auto client_destination_connection_id = hex<8>("8394c8f03e515708"); + +/** A.1: the HkdfLabel of "client in", 32 bytes, an empty context. */ +inline constexpr auto label_client_in = hex<19>("00200f746c73313320636c69656e7420696e00"); +/** A.1: the HkdfLabel of "server in", 32 bytes, an empty context. */ +inline constexpr auto label_server_in = hex<19>("00200f746c7331332073657276657220696e00"); +/** A.1: the HkdfLabel of "quic key", 16 bytes, an empty context. */ +inline constexpr auto label_quic_key = hex<18>("00100e746c7331332071756963206b657900"); +/** A.1: the HkdfLabel of "quic iv", 12 bytes, an empty context. */ +inline constexpr auto label_quic_iv = hex<17>("000c0d746c733133207175696320697600"); +/** A.1: the HkdfLabel of "quic hp", 16 bytes, an empty context. */ +inline constexpr auto label_quic_hp = hex<17>("00100d746c733133207175696320687000"); + +/** A.1: HKDF-Extract(initial_salt, client_destination_connection_id). */ +inline constexpr auto initial_secret = + hex<32>("7db5df06e7a69e432496adedb00851923595221596ae2ae9fb8115c1e9ed0a44"); + +/** The keys of one direction of A.1. */ +struct initial_keys { + /** HKDF-Expand-Label(initial_secret, "client in" or "server in", "", 32). */ + std::array secret; + /** HKDF-Expand-Label(secret, "quic key", "", 16), AES-128-GCM's key. */ + std::array key; + /** HKDF-Expand-Label(secret, "quic iv", "", 12), the nonce's base. */ + std::array iv; + /** HKDF-Expand-Label(secret, "quic hp", "", 16), the header protection's AES-128 key. */ + std::array hp; +}; + +/** A.1: the client's Initial keys. */ +inline constexpr initial_keys client_keys{ + .secret = hex<32>("c00cf151ca5be075ed0ebfb5c80323c42d6b7db67881289af4008f1f6c357aea"), + .key = hex<16>("1f369613dd76d5467730efcbe3b1a22d"), + .iv = hex<12>("fa044b2f42a3fd3b46fb255c"), + .hp = hex<16>("9f50449e04a0e810283a1e9933adedd2"), +}; + +/** A.1: the server's Initial keys. */ +inline constexpr initial_keys server_keys{ + .secret = hex<32>("3c199828fd139efd216c155ad844cc81fb82fa8d7446fa7d78be803acdda951b"), + .key = hex<16>("cf3a5331653c364c88f0f379b6067e37"), + .iv = hex<12>("0ac1493ca1905853b0bba03e"), + .hp = hex<16>("c206b8d9b9f0f37644430b490eeaa314"), +}; + +/** A.2: the client's payload, its CRYPTO frame and PADDING frames, 1,162 bytes. */ +inline constexpr std::size_t client_payload_size = 1162; +/** A.2: the client's packet number. */ +inline constexpr std::uint64_t client_packet_number = 2; +/** A.2: the header protection's sample, the first 16 bytes of the protected payload. */ +inline constexpr auto client_sample = hex<16>("d1b1c98dd7689fb8ec11d242b123dc9b"); +/** A.2: the first 5 bytes of AES-ECB(hp, sample). */ +inline constexpr auto client_mask = hex<5>("437b9aec36"); + +/** A.3: the server's packet number. */ +inline constexpr std::uint64_t server_packet_number = 1; +/** A.3: the header protection's sample, from the third protected byte. */ +inline constexpr auto server_sample = hex<16>("2cd0991cd25b0aac406a5816b6394100"); +/** A.3: the first 5 bytes of AES-ECB(hp, sample). */ +inline constexpr auto server_mask = hex<5>("2ec0d8356a"); + +/** Section 5.8: the Retry integrity tag's AES-128-GCM key. */ +inline constexpr auto retry_key = hex<16>("be0c690b9f66575a1d766b54e368c84e"); +/** Section 5.8: the Retry integrity tag's nonce. */ +inline constexpr auto retry_nonce = hex<12>("461599d35d632bf2239825bb"); + +/** + * A.2: the client's CRYPTO frame, which PADDING frames (zero bytes) follow up to + * `client_payload_size`. + */ +inline constexpr std::string_view client_crypto_frame = + "060040f1010000ed0303ebf8fa56f12939b9584a3896472ec40bb863cfd3e868" + "04fe3a47f06a2b69484c00000413011302010000c000000010000e00000b6578" + "616d706c652e636f6dff01000100000a00080006001d00170018001000070005" + "04616c706e000500050100000000003300260024001d00209370b2c9caa47fba" + "baf4559fedba753de171fa71f50f1ce15d43e994ec74d748002b000302030400" + "0d0010000e0403050306030203080408050806002d00020101001c0002400100" + "3900320408ffffffffffffffff05048000ffff07048000ffff08011001048000" + "75300901100f088394c8f03e51570806048000ffff"; + +/** A.2: the client's unprotected header, packet number 2 on 4 bytes. */ +inline constexpr std::string_view client_header = "c300000001088394c8f03e5157080000449e00000002"; + +/** A.2: the client's protected packet, 1,200 bytes. */ +inline constexpr std::string_view client_packet = + "c000000001088394c8f03e5157080000449e7b9aec34d1b1c98dd7689fb8ec11" + "d242b123dc9bd8bab936b47d92ec356c0bab7df5976d27cd449f63300099f399" + "1c260ec4c60d17b31f8429157bb35a1282a643a8d2262cad67500cadb8e7378c" + "8eb7539ec4d4905fed1bee1fc8aafba17c750e2c7ace01e6005f80fcb7df6212" + "30c83711b39343fa028cea7f7fb5ff89eac2308249a02252155e2347b63d58c5" + "457afd84d05dfffdb20392844ae812154682e9cf012f9021a6f0be17ddd0c208" + "4dce25ff9b06cde535d0f920a2db1bf362c23e596d11a4f5a6cf3948838a3aec" + "4e15daf8500a6ef69ec4e3feb6b1d98e610ac8b7ec3faf6ad760b7bad1db4ba3" + "485e8a94dc250ae3fdb41ed15fb6a8e5eba0fc3dd60bc8e30c5c4287e53805db" + "059ae0648db2f64264ed5e39be2e20d82df566da8dd5998ccabdae053060ae6c" + "7b4378e846d29f37ed7b4ea9ec5d82e7961b7f25a9323851f681d582363aa5f8" + "9937f5a67258bf63ad6f1a0b1d96dbd4faddfcefc5266ba6611722395c906556" + "be52afe3f565636ad1b17d508b73d8743eeb524be22b3dcbc2c7468d54119c74" + "68449a13d8e3b95811a198f3491de3e7fe942b330407abf82a4ed7c1b311663a" + "c69890f4157015853d91e923037c227a33cdd5ec281ca3f79c44546b9d90ca00" + "f064c99e3dd97911d39fe9c5d0b23a229a234cb36186c4819e8b9c5927726632" + "291d6a418211cc2962e20fe47feb3edf330f2c603a9d48c0fcb5699dbfe58964" + "25c5bac4aee82e57a85aaf4e2513e4f05796b07ba2ee47d80506f8d2c25e50fd" + "14de71e6c418559302f939b0e1abd576f279c4b2e0feb85c1f28ff18f58891ff" + "ef132eef2fa09346aee33c28eb130ff28f5b766953334113211996d20011a198" + "e3fc433f9f2541010ae17c1bf202580f6047472fb36857fe843b19f5984009dd" + "c324044e847a4f4a0ab34f719595de37252d6235365e9b84392b061085349d73" + "203a4a13e96f5432ec0fd4a1ee65accdd5e3904df54c1da510b0ff20dcc0c77f" + "cb2c0e0eb605cb0504db87632cf3d8b4dae6e705769d1de354270123cb11450e" + "fc60ac47683d7b8d0f811365565fd98c4c8eb936bcab8d069fc33bd801b03ade" + "a2e1fbc5aa463d08ca19896d2bf59a071b851e6c239052172f296bfb5e724047" + "90a2181014f3b94a4e97d117b438130368cc39dbb2d198065ae3986547926cd2" + "162f40a29f0c3c8745c0f50fba3852e566d44575c29d39a03f0cda721984b6f4" + "40591f355e12d439ff150aab7613499dbd49adabc8676eef023b15b65bfc5ca0" + "6948109f23f350db82123535eb8a7433bdabcb909271a6ecbcb58b936a88cd4e" + "8f2e6ff5800175f113253d8fa9ca8885c2f552e657dc603f252e1a8e308f76f0" + "be79e2fb8f5d5fbbe2e30ecadd220723c8c0aea8078cdfcb3868263ff8f09400" + "54da48781893a7e49ad5aff4af300cd804a6b6279ab3ff3afb64491c85194aab" + "760d58a606654f9f4400e8b38591356fbf6425aca26dc85244259ff2b19c41b9" + "f96f3ca9ec1dde434da7d2d392b905ddf3d1f9af93d1af5950bd493f5aa731b4" + "056df31bd267b6b90a079831aaf579be0a39013137aac6d404f518cfd4684064" + "7e78bfe706ca4cf5e9c5453e9f7cfd2b8b4c8d169a44e55c88d4a9a7f9474241" + "e221af44860018ab0856972e194cd934"; + +/** A.3: the server's payload, an ACK frame and a CRYPTO frame. */ +inline constexpr std::string_view server_payload = + "02000000000600405a020000560303eefce7f7b37ba1d1632e96677825ddf739" + "88cfc79825df566dc5430b9a045a1200130100002e00330024001d00209d3c94" + "0d89690b84d08a60993c144eca684d1081287c834d5311bcf32bb9da1a002b00" + "020304"; + +/** A.3: the server's unprotected header, packet number 1 on 2 bytes. */ +inline constexpr std::string_view server_header = "c1000000010008f067a5502a4262b50040750001"; + +/** A.3: the server's protected packet. */ +inline constexpr std::string_view server_packet = + "cf000000010008f067a5502a4262b5004075c0d95a482cd0991cd25b0aac406a" + "5816b6394100f37a1c69797554780bb38cc5a99f5ede4cf73c3ec2493a1839b3" + "dbcba3f6ea46c5b7684df3548e7ddeb9c3bf9c73cc3f3bded74b562bfb19fb84" + "022f8ef4cdd93795d77d06edbb7aaf2f58891850abbdca3d20398c276456cbc4" + "2158407dd074ee"; + +/** A.4: the Retry packet, its integrity tag the last 16 bytes. */ +inline constexpr std::string_view retry_packet = + "ff000000010008f067a5502a4262b5746f6b656e04a265ba2eff4d829058fb3f" + "0f2496ba"; + +} // namespace webcpp::test::quic_rfc9001 + +#endif // WEBCPP_TEST_CRYPTO_SUPPORT_QUIC_RFC9001_VECTORS_HPP