From c0e05f4d1e211f37d56152fc957ac99d86f28c5d Mon Sep 17 00:00:00 2001 From: R Date: Sat, 5 Dec 2026 08:24:12 -0300 Subject: [PATCH 01/29] crypto: the symmetric and box capability concepts of detail::backend (backend-concepts task 1.1): block_cipher, gcm, aead, authenticated_box, stream_cipher, authenticator, box, sealed_box and kx, each shipped engine checked and a stand-in per requirement --- .../webcpp/crypto/detail/backend/boxes.hpp | 92 ++++ .../webcpp/crypto/detail/backend/common.hpp | 52 ++ .../crypto/detail/backend/symmetric.hpp | 132 +++++ test/Jamfile | 17 + test/backend_concepts_symmetric_test.cpp | 472 ++++++++++++++++++ 5 files changed, 765 insertions(+) create mode 100644 include/webcpp/crypto/detail/backend/boxes.hpp create mode 100644 include/webcpp/crypto/detail/backend/common.hpp create mode 100644 include/webcpp/crypto/detail/backend/symmetric.hpp create mode 100644 test/backend_concepts_symmetric_test.cpp diff --git a/include/webcpp/crypto/detail/backend/boxes.hpp b/include/webcpp/crypto/detail/backend/boxes.hpp new file mode 100644 index 0000000..e6b4795 --- /dev/null +++ b/include/webcpp/crypto/detail/backend/boxes.hpp @@ -0,0 +1,92 @@ +// 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) + +#ifndef WEBCPP_CRYPTO_DETAIL_BACKEND_BOXES_HPP +#define WEBCPP_CRYPTO_DETAIL_BACKEND_BOXES_HPP + +#include + +#include + +#include +#include +#include +#include + +/** + * The public-key boxes and the key exchange (design D4): crypto_box, the sealed box and + * crypto_kx, over X25519 keys of 32 bytes. + */ +namespace webcpp::crypto::detail::backend { + +/** The size of an X25519 key, secret or public. */ +inline constexpr std::size_t x25519_key_size = 32; + +/** + * The nonce and tag sizes of both crypto_box families (XSalsa20-Poly1305 and + * XChaCha20-Poly1305), whose family types are tags without sizes. + */ +inline constexpr std::size_t box_nonce_size = 24; +/** See @ref box_nonce_size. */ +inline constexpr std::size_t box_tag_size = 16; + +/** + * crypto_box of a family: `init(state, own_secret_key, peer_public_key)` precomputes the shared + * key, then `seal` and `open` as a secret box's, with a nonce of `box_nonce_size` bytes. `Family` + * names the family the engine serves; both families share the sizes. + */ +template +concept box = + stateful_engine && + requires(Engine::state& engine_state, + std::span own_secret_key, + std::span peer_public_key, + std::span nonce_bytes, + std::span data_bytes, boost::hash2::digest& tag_output, + std::array const& expected_tag) { + { Engine::init(engine_state, own_secret_key, peer_public_key) } noexcept -> fallible; + { Engine::seal(engine_state, nonce_bytes, data_bytes, tag_output) } noexcept -> fallible; + { Engine::open(engine_state, nonce_bytes, data_bytes, expected_tag) } noexcept -> fallible; + }; + +/** + * The sealed box: static `seal(recipient_public_key, message, output)` with a fresh ephemeral key, + * and `open(recipient_public_key, recipient_secret_key, sealed, output)`; no state. + */ +template +concept sealed_box = + requires(std::span recipient_public_key, + std::span recipient_secret_key, + std::span input_bytes, std::span output_bytes) { + { Engine::seal(recipient_public_key, input_bytes, output_bytes) } noexcept -> fallible; + { + Engine::open(recipient_public_key, recipient_secret_key, input_bytes, output_bytes) + } noexcept -> fallible; + }; + +/** + * crypto_kx: `seed_keypair(seed, secret_key)` derives a key pair from a 32-byte seed, and + * `client` and `server` derive the two session keys from the own key pair and the peer's public + * key; no state. + */ +template +concept kx = requires( + std::span seed_bytes, std::span secret_key_output, + std::span secret_key, std::span public_key, + std::span peer_public_key, std::span receive_key, + std::span transmit_key) { + { Engine::seed_keypair(seed_bytes, secret_key_output) } noexcept -> fallible; + { + Engine::client(secret_key, public_key, peer_public_key, receive_key, transmit_key) + } noexcept -> fallible; + { + Engine::server(secret_key, public_key, peer_public_key, receive_key, transmit_key) + } noexcept -> fallible; +}; + +} // namespace webcpp::crypto::detail::backend + +#endif // WEBCPP_CRYPTO_DETAIL_BACKEND_BOXES_HPP diff --git a/include/webcpp/crypto/detail/backend/common.hpp b/include/webcpp/crypto/detail/backend/common.hpp new file mode 100644 index 0000000..b72f9dc --- /dev/null +++ b/include/webcpp/crypto/detail/backend/common.hpp @@ -0,0 +1,52 @@ +// 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) + +#ifndef WEBCPP_CRYPTO_DETAIL_BACKEND_COMMON_HPP +#define WEBCPP_CRYPTO_DETAIL_BACKEND_COMMON_HPP + +#include + +#include + +/** + * The capability concepts: what crypto's public types require of the engines of the backends + * crypto ships, an internal contract (design D1, D10). Each concept is named after the + * capability, never after a backend, and is a conjunction of named requirements so that a + * compiler names the operation an engine lacks. + * + * What every capability shares (design D2), beyond what a concept checks: + * + * - Timing guard: the public function holds `detail::timing_guard`; an engine function never holds + * one and never calls a public function. + * - Allocation: an engine never allocates through the C++ heap or a program's allocator; a backend + * library's own allocations are the backend's, named in its table row; a failed allocation + * inside the library is `backend_failure` or `errc::not_enough_memory`, never an exception. + * - Thread safety: engine functions may run concurrently on distinct states; a backend's global + * initialisation is idempotent, thread-safe and done by the engine. + * - Wiping: `reset` wipes every secret the state holds and releases any handle; `move` leaves the + * source as `reset` does; a secret written into a caller's span is written only on success. + * - Canary: an engine reads its backend's canary before its first library call. + * - Errors: values of `crypto::error` only, never an exception. + */ +namespace webcpp::crypto::detail::backend { + +/** An operation's result where it can fail and returns nothing. */ +template +concept fallible = std::same_as>; + +/** + * An engine whose objects keep a state: `typename Engine::state`, with `move(target, source)` + * leaving the source as `reset` leaves it, and `reset(state)` wiping it, both `noexcept`. + */ +template +concept stateful_engine = requires(Engine::state& target_state, Engine::state& source_state) { + { Engine::move(target_state, source_state) } noexcept; + { Engine::reset(target_state) } noexcept; +}; + +} // namespace webcpp::crypto::detail::backend + +#endif // WEBCPP_CRYPTO_DETAIL_BACKEND_COMMON_HPP diff --git a/include/webcpp/crypto/detail/backend/symmetric.hpp b/include/webcpp/crypto/detail/backend/symmetric.hpp new file mode 100644 index 0000000..fb9d684 --- /dev/null +++ b/include/webcpp/crypto/detail/backend/symmetric.hpp @@ -0,0 +1,132 @@ +// 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) + +#ifndef WEBCPP_CRYPTO_DETAIL_BACKEND_SYMMETRIC_HPP +#define WEBCPP_CRYPTO_DETAIL_BACKEND_SYMMETRIC_HPP + +#include + +#include + +#include +#include +#include +#include + +/** + * The symmetric capabilities (design D4): the AES block cipher, GCM, the AEADs, the secret boxes, + * the raw stream cipher and the single-use key authenticator. Every operation is `noexcept`; one + * that can fail returns `result`; outputs are written into the caller's spans. + */ +namespace webcpp::crypto::detail::backend { + +/** A keyed state's `init(state, key)`: the key's bytes, of a size the public type has checked. */ +template +concept keyed_init = + requires(Engine::state& engine_state, std::span key_bytes) { + { Engine::init(engine_state, key_bytes) } noexcept -> fallible; + }; + +/** + * The AES block cipher of a key size: `init`, then `encrypt` and `decrypt` of one 16-byte block + * in place. + */ +template +concept block_cipher = stateful_engine && keyed_init && + requires(Engine::state& engine_state, std::span block_bytes) { + { Engine::encrypt(engine_state, block_bytes) } noexcept -> fallible; + { Engine::decrypt(engine_state, block_bytes) } noexcept -> fallible; + }; + +/** + * GCM over a block cipher, one-shot and incremental alike: `start(state, is_sealing, nonce, + * associated_data)`, `run(state, data)` in place, any number of times, then `tag(state, out)` on + * sealing or `check(state, tag)` on opening (the comparison in constant time, its verdict + * `authentication_failed`), and `abandon(state)` for a message left unfinished. + */ +template +concept gcm = + stateful_engine && keyed_init && + requires( + Engine::state& engine_state, bool is_sealing, std::span nonce_bytes, + std::span associated_data_bytes, std::span data_bytes, + boost::hash2::digest<16>& tag_output, std::array const& expected_tag) { + { + Engine::start(engine_state, is_sealing, nonce_bytes, associated_data_bytes) + } noexcept -> fallible; + { Engine::run(engine_state, data_bytes) } noexcept -> fallible; + { Engine::tag(engine_state, tag_output) } noexcept -> fallible; + { Engine::check(engine_state, expected_tag) } noexcept -> fallible; + { Engine::abandon(engine_state) } noexcept; + }; + +/** + * An AEAD of a family: `seal(state, nonce, associated_data, data, tag)` encrypts the data in place + * and writes the tag; `open(state, nonce, associated_data, data, tag)` checks the tag in constant + * time and decrypts in place only when it holds (`authentication_failed` otherwise). + */ +template +concept aead = + stateful_engine && keyed_init && + requires(Engine::state& engine_state, std::span nonce_bytes, + std::span associated_data_bytes, + std::span data_bytes, boost::hash2::digest& tag_output, + std::array const& expected_tag) { + { + Engine::seal(engine_state, nonce_bytes, associated_data_bytes, data_bytes, tag_output) + } noexcept -> fallible; + { + Engine::open(engine_state, nonce_bytes, associated_data_bytes, data_bytes, expected_tag) + } noexcept -> fallible; + }; + +/** + * A secret box of a family: an AEAD without associated data, its nonce the family's + * `nonce_size`. + */ +template +concept authenticated_box = + stateful_engine && keyed_init && + requires(Engine::state& engine_state, + std::span nonce_bytes, + std::span data_bytes, boost::hash2::digest& tag_output, + std::array const& expected_tag) { + { Engine::seal(engine_state, nonce_bytes, data_bytes, tag_output) } noexcept -> fallible; + { Engine::open(engine_state, nonce_bytes, data_bytes, expected_tag) } noexcept -> fallible; + }; + +/** + * A raw stream cipher of a family (primitives' D4): `apply(state, nonce, initial_counter, data)` + * XORs the keystream from block `initial_counter` into the data in place; the nonce has the + * family's `nonce_size`, the counter its `counter_type`, and the public type has checked that the + * data is non-empty and within the counter. + */ +template +concept stream_cipher = + stateful_engine && keyed_init && + requires(Engine::state& engine_state, + std::span nonce_bytes, + Family::counter_type initial_counter, std::span data_bytes) { + { + Engine::apply(engine_state, nonce_bytes, initial_counter, data_bytes) + } noexcept -> fallible; + }; + +/** + * A single-use key authenticator of a family (primitives' D14): one static + * `authenticate(key, message, tag)` and no state, since no object keeps a key; the comparison of + * `verify` is crypto's own, in constant time, so the engine has no `verify`. + */ +template +concept authenticator = requires(std::span key_bytes, + std::span message_bytes, + std::span tag_bytes) { + { Engine::authenticate(key_bytes, message_bytes, tag_bytes) } noexcept -> fallible; +}; + +} // namespace webcpp::crypto::detail::backend + +#endif // WEBCPP_CRYPTO_DETAIL_BACKEND_SYMMETRIC_HPP diff --git a/test/Jamfile b/test/Jamfile index 93fc688..6d9293c 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -32,6 +32,8 @@ webcpp.headers-alone crypto : ../include : crypto.hpp crypto/config.hpp crypto/ciphers/aes.hpp crypto/ciphers/gcm.hpp crypto/random_device.hpp crypto/detail/bytes.hpp crypto/detail/engines.hpp crypto/detail/entropy.hpp crypto/detail/limits.hpp + crypto/detail/backend/common.hpp crypto/detail/backend/symmetric.hpp + crypto/detail/backend/boxes.hpp crypto/detail/windows.hpp crypto/supports.hpp crypto/detail/capabilities.hpp crypto/detail/defaults.hpp crypto/detail/async_access.hpp crypto/ciphers/aead.hpp @@ -1181,3 +1183,18 @@ webcpp.compile doc_sign : ../../../tools/test/fixtures/crypto_jamfiles/sign/sign webcpp.compile doc_sign_small : ../../../tools/test/fixtures/crypto_jamfiles/sign_small/sign.cpp : /third_party//secp256k1 small ; + +# The symmetric and box capability concepts of detail::backend (backend-concepts +# design D4): every engine the table lists for the AES block cipher, GCM, the +# AEADs, the secret boxes, the stream cipher, the authenticator, crypto_box, +# the sealed box and crypto_kx satisfies its concept, on every backend the +# target builds; a stand-in that lacks one operation, or gives it another +# result or no noexcept, satisfies none. +crypto-run backend_concepts_symmetric : backend_concepts_symmetric_test.cpp + : /third_party//bearssl /third_party//ctaes + /third_party//sodium /webcpp/crypto//openssl + : native wasip2 wasip3 ; +crypto-run backend_concepts_symmetric_emscripten : backend_concepts_symmetric_test.cpp + : /third_party//bearssl /third_party//ctaes + /third_party//sodium + : emscripten ; diff --git a/test/backend_concepts_symmetric_test.cpp b/test/backend_concepts_symmetric_test.cpp new file mode 100644 index 0000000..29b4422 --- /dev/null +++ b/test/backend_concepts_symmetric_test.cpp @@ -0,0 +1,472 @@ +// 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) + +// The symmetric and box capability concepts of detail::backend (backend-concepts design D1, D2, +// D4): every engine the table lists for the AES block cipher, GCM, the AEADs, the secret boxes, +// the raw stream cipher, the single-use key authenticator, crypto_box, the sealed box and +// crypto_kx satisfies its concept on every backend this target links; and a stand-in engine that +// lacks one operation, or gives it the wrong result or no noexcept, satisfies none, one stand-in +// per requirement, so that each requirement is shown to be checked. + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef WEBCPP_CRYPTO_HAS_BEARSSL +#include +#endif +#ifdef WEBCPP_CRYPTO_HAS_CTAES +#include +#endif +#ifdef WEBCPP_CRYPTO_HAS_OPENSSL +#include +#endif +#ifdef WEBCPP_CRYPTO_HAS_SODIUM +#include +#endif + +#include +#include + +#include +#include +#include +#include + +namespace backend = webcpp::crypto::detail::backend; +namespace detail = webcpp::crypto::detail; +namespace families = webcpp::crypto::families; +namespace backends = webcpp::crypto::backends; + +namespace { + +using fallible = boost::system::result; + +// The shipped engines, on the backends this target links (the table's cells, crypto.json). + +template +inline constexpr bool all_gcm = (backend::gcm> && ...); + +template +inline constexpr bool all_block = + (backend::block_cipher> && ...); + +#ifdef WEBCPP_CRYPTO_HAS_BEARSSL +static_assert(all_gcm); +static_assert(backend::aead, + families::chacha20_poly1305>); +static_assert(backend::stream_cipher, + families::chacha20>); +#endif + +#ifdef WEBCPP_CRYPTO_HAS_CTAES +static_assert(all_block); +#endif + +#ifdef WEBCPP_CRYPTO_HAS_OPENSSL +static_assert(all_gcm); +static_assert(all_block); +static_assert(backend::aead, + families::chacha20_poly1305>); +static_assert(backend::stream_cipher, + families::chacha20>); +static_assert( + backend::authenticator, + families::poly1305>); +#endif + +#ifdef WEBCPP_CRYPTO_HAS_SODIUM +template +using sodium_aead = detail::aead_engine; +static_assert(backend::aead, families::chacha20_poly1305>); +static_assert(backend::aead, + families::chacha20_poly1305_original>); +static_assert( + backend::aead, families::xchacha20_poly1305>); +static_assert(backend::stream_cipher, + families::chacha20>); +static_assert( + backend::authenticator, + families::poly1305>); +static_assert(backend::authenticated_box< + detail::secretbox_engine, + families::secretbox_xsalsa20poly1305>); +static_assert(backend::authenticated_box< + detail::secretbox_engine, + families::secretbox_xchacha20poly1305>); +static_assert(backend::box, + families::box_xsalsa20poly1305>); +static_assert(backend::box, + families::box_xchacha20poly1305>); +static_assert( + backend::sealed_box>); +static_assert(backend::kx>); +#endif + +// Stand-ins: a complete engine per concept, then one stand-in per requirement that breaks it. + +struct stand_in_state {}; + +using digest = boost::hash2::digest<16>; +using tag_array = std::array; +using bytes = std::span; +using const_bytes = std::span; + +struct keyed_base { + using state = stand_in_state; + + static fallible init(stand_in_state& /*engine_state*/, const_bytes /*input_bytes*/) noexcept { + return {}; + } + + static void move(stand_in_state& /*engine_state*/, state& /*argument*/) noexcept {} + + static void reset(stand_in_state& /*engine_state*/) noexcept {} +}; + +struct block_complete : keyed_base { + static fallible encrypt(stand_in_state& /*engine_state*/, bytes /*data_bytes*/) noexcept { + return {}; + } + + static fallible decrypt(stand_in_state& /*engine_state*/, bytes /*data_bytes*/) noexcept { + return {}; + } +}; + +struct block_without_decrypt : keyed_base { + static fallible encrypt(stand_in_state& /*engine_state*/, bytes /*data_bytes*/) noexcept { + return {}; + } +}; + +struct block_throwing_encrypt : keyed_base { + static fallible encrypt(stand_in_state& /*engine_state*/, bytes /*data_bytes*/) { return {}; } + + static fallible decrypt(stand_in_state& /*engine_state*/, bytes /*data_bytes*/) noexcept { + return {}; + } +}; + +struct block_without_reset { + using state = stand_in_state; + + static fallible init(stand_in_state& /*engine_state*/, const_bytes /*input_bytes*/) noexcept { + return {}; + } + + static void move(stand_in_state& /*engine_state*/, state& /*argument*/) noexcept {} + + static fallible encrypt(stand_in_state& /*engine_state*/, bytes /*data_bytes*/) noexcept { + return {}; + } + + static fallible decrypt(stand_in_state& /*engine_state*/, bytes /*data_bytes*/) noexcept { + return {}; + } +}; + +struct block_bool_init { + using state = stand_in_state; + + static bool init(stand_in_state& /*engine_state*/, const_bytes /*input_bytes*/) noexcept { + return true; + } + + static void move(stand_in_state& /*engine_state*/, state& /*argument*/) noexcept {} + + static void reset(stand_in_state& /*engine_state*/) noexcept {} + + static fallible encrypt(stand_in_state& /*engine_state*/, bytes /*data_bytes*/) noexcept { + return {}; + } + + static fallible decrypt(stand_in_state& /*engine_state*/, bytes /*data_bytes*/) noexcept { + return {}; + } +}; + +static_assert(backend::block_cipher); +static_assert(!backend::block_cipher); +static_assert(!backend::block_cipher); +static_assert(!backend::block_cipher); +static_assert(!backend::block_cipher); + +struct gcm_operations : keyed_base { + static fallible start(stand_in_state& /*engine_state*/, bool /*is_sealing*/, + const_bytes /*input_bytes*/, const_bytes /*input_bytes_2*/) noexcept { + return {}; + } + + static fallible run(stand_in_state& /*engine_state*/, bytes /*data_bytes*/) noexcept { + return {}; + } + + static fallible tag(stand_in_state& /*engine_state*/, digest& /*tag_output*/) noexcept { + return {}; + } +}; + +struct gcm_complete : gcm_operations { + static fallible check(stand_in_state& /*engine_state*/, + tag_array const& /*expected_tag*/) noexcept { + return {}; + } + + static void abandon(stand_in_state& /*engine_state*/) noexcept {} +}; + +struct gcm_without_check : gcm_operations { + static void abandon(stand_in_state& /*engine_state*/) noexcept {} +}; + +struct gcm_without_abandon : gcm_operations { + static fallible check(stand_in_state& /*engine_state*/, + tag_array const& /*expected_tag*/) noexcept { + return {}; + } +}; + +static_assert(backend::gcm); +static_assert(!backend::gcm); +static_assert(!backend::gcm); + +struct aead_complete : keyed_base { + static fallible seal(stand_in_state& /*engine_state*/, const_bytes /*input_bytes*/, + const_bytes /*input_bytes_2*/, bytes /*data_bytes*/, + digest& /*tag_output*/) noexcept { + return {}; + } + + static fallible open(stand_in_state& /*engine_state*/, const_bytes /*input_bytes*/, + const_bytes /*input_bytes_2*/, bytes /*data_bytes*/, + tag_array const& /*expected_tag*/) noexcept { + return {}; + } +}; + +struct aead_without_open : keyed_base { + static fallible seal(stand_in_state& /*engine_state*/, const_bytes /*input_bytes*/, + const_bytes /*input_bytes_2*/, bytes /*data_bytes*/, + digest& /*tag_output*/) noexcept { + return {}; + } +}; + +struct aead_without_associated_data : keyed_base { + static fallible seal(stand_in_state& /*engine_state*/, const_bytes /*input_bytes*/, + bytes /*data_bytes*/, digest& /*tag_output*/) noexcept { + return {}; + } + + static fallible open(stand_in_state& /*engine_state*/, const_bytes /*input_bytes*/, + bytes /*data_bytes*/, tag_array const& /*expected_tag*/) noexcept { + return {}; + } +}; + +static_assert(backend::aead); +static_assert(!backend::aead); +static_assert(!backend::aead); + +struct stream_complete : keyed_base { + static fallible apply(stand_in_state& /*engine_state*/, + std::span /*nonce_bytes*/, + std::uint32_t /*initial_counter*/, bytes /*data_bytes*/) noexcept { + return {}; + } +}; + +struct stream_without_counter : keyed_base { + static fallible apply(stand_in_state& /*engine_state*/, + std::span /*nonce_bytes*/, + bytes /*data_bytes*/) noexcept { + return {}; + } +}; + +struct stream_without_move { + using state = stand_in_state; + + static fallible init(stand_in_state& /*engine_state*/, const_bytes /*input_bytes*/) noexcept { + return {}; + } + + static void reset(stand_in_state& /*engine_state*/) noexcept {} + + static fallible apply(stand_in_state& /*engine_state*/, + std::span /*nonce_bytes*/, + std::uint32_t /*initial_counter*/, bytes /*data_bytes*/) noexcept { + return {}; + } +}; + +static_assert(backend::stream_cipher); +static_assert(!backend::stream_cipher); +static_assert(!backend::stream_cipher); +// A stream cipher is no AEAD, and an AEAD no stream cipher. +static_assert(!backend::aead); +static_assert(!backend::stream_cipher); + +struct authenticator_complete { + static fallible authenticate(std::span /*key_bytes*/, + const_bytes /*input_bytes*/, + std::span /*tag_bytes*/) noexcept { + return {}; + } +}; + +struct authenticator_throwing { + static fallible authenticate(std::span /*key_bytes*/, + const_bytes /*input_bytes*/, + std::span /*tag_bytes*/) { + return {}; + } +}; + +struct authenticator_void { + static void authenticate(std::span /*key_bytes*/, + const_bytes /*input_bytes*/, + std::span /*tag_bytes*/) noexcept {} +}; + +static_assert(backend::authenticator); +static_assert(!backend::authenticator); +static_assert(!backend::authenticator); + +struct box_like_secretbox : keyed_base { + static fallible seal(stand_in_state& /*engine_state*/, + std::span /*nonce_bytes*/, bytes /*data_bytes*/, + digest& /*tag_output*/) noexcept { + return {}; + } + + static fallible open(stand_in_state& /*engine_state*/, + std::span /*nonce_bytes*/, bytes /*data_bytes*/, + tag_array const& /*expected_tag*/) noexcept { + return {}; + } +}; + +struct secretbox_wrong_nonce : keyed_base { + static fallible seal(stand_in_state& /*engine_state*/, + std::span /*nonce_bytes*/, bytes /*data_bytes*/, + digest& /*tag_output*/) noexcept { + return {}; + } + + static fallible open(stand_in_state& /*engine_state*/, + std::span /*nonce_bytes*/, bytes /*data_bytes*/, + tag_array const& /*expected_tag*/) noexcept { + return {}; + } +}; + +static_assert(backend::authenticated_box); +static_assert( + !backend::authenticated_box); + +struct box_complete { + using state = stand_in_state; + + static fallible init(stand_in_state& /*engine_state*/, + std::span /*key_bytes*/, + std::span /*key_bytes_2*/) noexcept { + return {}; + } + + static void move(stand_in_state& /*engine_state*/, state& /*argument*/) noexcept {} + + static void reset(stand_in_state& /*engine_state*/) noexcept {} + + static fallible seal(stand_in_state& /*engine_state*/, + std::span /*nonce_bytes*/, bytes /*data_bytes*/, + digest& /*tag_output*/) noexcept { + return {}; + } + + static fallible open(stand_in_state& /*engine_state*/, + std::span /*nonce_bytes*/, bytes /*data_bytes*/, + tag_array const& /*expected_tag*/) noexcept { + return {}; + } +}; + +// A secret box's init takes one key; a box's, the own secret key and the peer's public key. +static_assert(backend::box); +static_assert(!backend::box); +static_assert(!backend::authenticated_box); + +struct sealed_box_complete { + static fallible seal(std::span /*key_bytes*/, + const_bytes /*input_bytes*/, bytes /*data_bytes*/) noexcept { + return {}; + } + + static fallible open(std::span /*key_bytes*/, + std::span /*key_bytes_2*/, + const_bytes /*input_bytes*/, bytes /*data_bytes*/) noexcept { + return {}; + } +}; + +struct sealed_box_without_open { + static fallible seal(std::span /*key_bytes*/, + const_bytes /*input_bytes*/, bytes /*data_bytes*/) noexcept { + return {}; + } +}; + +static_assert(backend::sealed_box); +static_assert(!backend::sealed_box); + +using key32 = std::span; +using out32 = std::span; + +struct kx_complete { + static fallible seed_keypair(key32 /*key_bytes*/, out32 /*output_bytes*/) noexcept { + return {}; + } + + static fallible client(key32 /*key_bytes*/, key32 /*key_bytes_2*/, key32 /*key_bytes_3*/, + out32 /*output_bytes*/, out32 /*output_bytes_2*/) noexcept { + return {}; + } + + static fallible server(key32 /*key_bytes*/, key32 /*key_bytes_2*/, key32 /*key_bytes_3*/, + out32 /*output_bytes*/, out32 /*output_bytes_2*/) noexcept { + return {}; + } +}; + +struct kx_without_server { + static fallible seed_keypair(key32 /*key_bytes*/, out32 /*output_bytes*/) noexcept { + return {}; + } + + static fallible client(key32 /*key_bytes*/, key32 /*key_bytes_2*/, key32 /*key_bytes_3*/, + out32 /*output_bytes*/, out32 /*output_bytes_2*/) noexcept { + return {}; + } +}; + +static_assert(backend::kx); +static_assert(!backend::kx); + +} // namespace + +int main() { + return 0; +} From 1106124ce3d910bc627b93eabb07d5afffbf27d2 Mon Sep 17 00:00:00 2001 From: R Date: Sat, 5 Dec 2026 09:25:39 -0300 Subject: [PATCH 02/29] backend concepts: a failing stand-in for every requirement, task 1.1's review fixes - test/support/backend_stand_in.hpp: a stand-in engine is a class template over a defect (one operation missing, not noexcept or of another result; one parameter of another size or type); rejects_every_defect and rejects_every_shape assert that a concept takes the complete stand-in and refuses every defect it lists. - The symmetric test uses it for block_cipher, gcm, aead, authenticated_box, stream_cipher and authenticator; the box concepts move to backend_concepts_boxes_test, with box, sealed_box and kx, and their sizes checked against kx_keypair's. - move, reset and abandon must return void; box takes no family; kx's sizes are named constants; block_cipher and gcm document why they take no key size; the stand-ins' sizes are the families'. --- .../webcpp/crypto/detail/backend/boxes.hpp | 37 +- .../webcpp/crypto/detail/backend/common.hpp | 9 +- .../crypto/detail/backend/symmetric.hpp | 17 +- test/Jamfile | 14 +- test/backend_concepts_boxes_test.cpp | 244 +++++++ test/backend_concepts_symmetric_test.cpp | 598 ++++++++---------- test/support/backend_stand_in.hpp | 155 +++++ 7 files changed, 709 insertions(+), 365 deletions(-) create mode 100644 test/backend_concepts_boxes_test.cpp create mode 100644 test/support/backend_stand_in.hpp diff --git a/include/webcpp/crypto/detail/backend/boxes.hpp b/include/webcpp/crypto/detail/backend/boxes.hpp index e6b4795..1cffe7b 100644 --- a/include/webcpp/crypto/detail/backend/boxes.hpp +++ b/include/webcpp/crypto/detail/backend/boxes.hpp @@ -33,12 +33,18 @@ inline constexpr std::size_t box_nonce_size = 24; /** See @ref box_nonce_size. */ inline constexpr std::size_t box_tag_size = 16; +/** crypto_kx's seed, an X25519 secret's size. */ +inline constexpr std::size_t kx_seed_size = x25519_key_size; +/** crypto_kx's session keys' size, each. */ +inline constexpr std::size_t kx_session_key_size = 32; + /** - * crypto_box of a family: `init(state, own_secret_key, peer_public_key)` precomputes the shared - * key, then `seal` and `open` as a secret box's, with a nonce of `box_nonce_size` bytes. `Family` - * names the family the engine serves; both families share the sizes. + * crypto_box: `init(state, own_secret_key, peer_public_key)` precomputes the shared key, then + * `seal` and `open` as a secret box's, with a nonce of `box_nonce_size` bytes. Both families + * (XSalsa20-Poly1305, XChaCha20-Poly1305) share the sizes and their family types are tags without + * them, so the concept takes no family (D4 wrote `box`; STATUS.md ruling 1.1-M1). */ -template +template concept box = stateful_engine && requires(Engine::state& engine_state, @@ -54,7 +60,9 @@ concept box = /** * The sealed box: static `seal(recipient_public_key, message, output)` with a fresh ephemeral key, - * and `open(recipient_public_key, recipient_secret_key, sealed, output)`; no state. + * and `open(recipient_public_key, recipient_secret_key, sealed, output)`; no state. D4's text had + * `open(recipient, sealed, out)`; the concept follows the engine, which takes the recipient's two + * keys (STATUS.md ruling 1.1-M2). */ template concept sealed_box = @@ -68,16 +76,19 @@ concept sealed_box = }; /** - * crypto_kx: `seed_keypair(seed, secret_key)` derives a key pair from a 32-byte seed, and - * `client` and `server` derive the two session keys from the own key pair and the peer's public - * key; no state. + * crypto_kx: `seed_keypair(seed, secret_key)` derives a key pair from a seed, and `client` and + * `server` derive the two session keys from the own key pair and the peer's public key; no state. + * D4's text named them `derive_from_seed`, `client_session_keys` and `server_session_keys`; the + * concept follows the engine's names (STATUS.md ruling 1.1-M2). */ template -concept kx = requires( - std::span seed_bytes, std::span secret_key_output, - std::span secret_key, std::span public_key, - std::span peer_public_key, std::span receive_key, - std::span transmit_key) { +concept kx = requires(std::span seed_bytes, + std::span secret_key_output, + std::span secret_key, + std::span public_key, + std::span peer_public_key, + std::span receive_key, + std::span transmit_key) { { Engine::seed_keypair(seed_bytes, secret_key_output) } noexcept -> fallible; { Engine::client(secret_key, public_key, peer_public_key, receive_key, transmit_key) diff --git a/include/webcpp/crypto/detail/backend/common.hpp b/include/webcpp/crypto/detail/backend/common.hpp index b72f9dc..c6c156c 100644 --- a/include/webcpp/crypto/detail/backend/common.hpp +++ b/include/webcpp/crypto/detail/backend/common.hpp @@ -14,8 +14,9 @@ /** * The capability concepts: what crypto's public types require of the engines of the backends * crypto ships, an internal contract (design D1, D10). Each concept is named after the - * capability, never after a backend, and is a conjunction of named requirements so that a - * compiler names the operation an engine lacks. + * capability, never after a backend, and is a conjunction of the shared requirements + * (`stateful_engine`, `keyed_init`) and one requires-expression that lists the operations, so that + * a compiler names the requirement an engine fails. * * What every capability shares (design D2), beyond what a concept checks: * @@ -43,8 +44,8 @@ concept fallible = std::same_as>; */ template concept stateful_engine = requires(Engine::state& target_state, Engine::state& source_state) { - { Engine::move(target_state, source_state) } noexcept; - { Engine::reset(target_state) } noexcept; + { Engine::move(target_state, source_state) } noexcept -> std::same_as; + { Engine::reset(target_state) } noexcept -> std::same_as; }; } // namespace webcpp::crypto::detail::backend diff --git a/include/webcpp/crypto/detail/backend/symmetric.hpp b/include/webcpp/crypto/detail/backend/symmetric.hpp index fb9d684..2c4894f 100644 --- a/include/webcpp/crypto/detail/backend/symmetric.hpp +++ b/include/webcpp/crypto/detail/backend/symmetric.hpp @@ -12,6 +12,7 @@ #include #include +#include #include #include #include @@ -31,8 +32,12 @@ concept keyed_init = }; /** - * The AES block cipher of a key size: `init`, then `encrypt` and `decrypt` of one 16-byte block - * in place. + * The AES block cipher: `init`, then `encrypt` and `decrypt` of one 16-byte block in place. + * + * The key size is not a parameter of the concept (D4 wrote `block_cipher`): `init` + * takes the key as a dynamic span the public type has sized, so no expression could check it; the + * engine is one class per key size, `block_engine`, and the table's cells assert + * the concept per key size (STATUS.md ruling 1.1-I2). */ template concept block_cipher = stateful_engine && keyed_init && @@ -46,6 +51,8 @@ concept block_cipher = stateful_engine && keyed_init && * associated_data)`, `run(state, data)` in place, any number of times, then `tag(state, out)` on * sealing or `check(state, tag)` on opening (the comparison in constant time, its verdict * `authentication_failed`), and `abandon(state)` for a message left unfinished. + * + * As for @ref block_cipher, the key size is the engine class's, not the concept's (ruling 1.1-I2). */ template concept gcm = @@ -60,13 +67,15 @@ concept gcm = { Engine::run(engine_state, data_bytes) } noexcept -> fallible; { Engine::tag(engine_state, tag_output) } noexcept -> fallible; { Engine::check(engine_state, expected_tag) } noexcept -> fallible; - { Engine::abandon(engine_state) } noexcept; + { Engine::abandon(engine_state) } noexcept -> std::same_as; }; /** * An AEAD of a family: `seal(state, nonce, associated_data, data, tag)` encrypts the data in place * and writes the tag; `open(state, nonce, associated_data, data, tag)` checks the tag in constant - * time and decrypts in place only when it holds (`authentication_failed` otherwise). + * time and decrypts in place only when it holds (`authentication_failed` otherwise). The tag has + * the family's `tag_size`; the nonce is a dynamic span, its size the family's, checked by the + * public type, so it is not part of the contract. */ template concept aead = diff --git a/test/Jamfile b/test/Jamfile index 6d9293c..ca804bb 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -1184,12 +1184,11 @@ webcpp.compile doc_sign_small : ../../../tools/test/fixtures/crypto_jamfiles/sign_small/sign.cpp : /third_party//secp256k1 small ; -# The symmetric and box capability concepts of detail::backend (backend-concepts -# design D4): every engine the table lists for the AES block cipher, GCM, the -# AEADs, the secret boxes, the stream cipher, the authenticator, crypto_box, -# the sealed box and crypto_kx satisfies its concept, on every backend the -# target builds; a stand-in that lacks one operation, or gives it another -# result or no noexcept, satisfies none. +# The capability concepts of detail::backend (backend-concepts design D4), one +# test per area header: every engine the table lists satisfies its concept, on +# every backend the target builds; a stand-in (support/backend_stand_in.hpp) +# satisfies it when complete and fails it with any one operation missing, not +# noexcept or of another result, or any one parameter of another shape. crypto-run backend_concepts_symmetric : backend_concepts_symmetric_test.cpp : /third_party//bearssl /third_party//ctaes /third_party//sodium /webcpp/crypto//openssl @@ -1198,3 +1197,6 @@ crypto-run backend_concepts_symmetric_emscripten : backend_concepts_symmetric_te : /third_party//bearssl /third_party//ctaes /third_party//sodium : emscripten ; +crypto-run backend_concepts_boxes : backend_concepts_boxes_test.cpp + : /third_party//sodium + : native wasip2 wasip3 emscripten ; diff --git a/test/backend_concepts_boxes_test.cpp b/test/backend_concepts_boxes_test.cpp new file mode 100644 index 0000000..093a9cb --- /dev/null +++ b/test/backend_concepts_boxes_test.cpp @@ -0,0 +1,244 @@ +// 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) + +// The box capability concepts of detail::backend (backend-concepts design D1, D2, D4): every +// engine the table lists for crypto_box, the sealed box and crypto_kx satisfies its concept on +// every backend this target links; the concepts' sizes are the public types'; and for each +// concept a stand-in engine (support/backend_stand_in.hpp) satisfies it when complete and fails +// it with any one operation missing, not noexcept or of another result, or any one size parameter +// of another size, so that each requirement is shown to be checked. + +#include "support/backend_stand_in.hpp" + +#include +#include +#include +#include +#include + +#ifdef WEBCPP_CRYPTO_HAS_SODIUM +#include +#endif + +#include + +#include +#include +#include + +namespace backend = webcpp::crypto::detail::backend; +namespace key_exchange = webcpp::crypto::key_exchange; +namespace detail = webcpp::crypto::detail; +namespace families = webcpp::crypto::families; +namespace backends = webcpp::crypto::backends; + +namespace { + +// The shipped engines, on the backends this target links (the table's cells, crypto.json). + +#ifdef WEBCPP_CRYPTO_HAS_SODIUM +static_assert(backend::box>); +static_assert(backend::box>); +static_assert( + backend::sealed_box>); +static_assert(backend::kx>); + +// The concepts' sizes are the public types'. +static_assert(backend::kx_seed_size == key_exchange::kx_keypair::seed_size); +static_assert(backend::x25519_key_size == + key_exchange::kx_keypair::public_key_size); +static_assert(backend::kx_session_key_size == + key_exchange::kx_keypair::session_key_size); +#endif + +// Stand-ins. + +using backend_stand_in::bytes; +using backend_stand_in::const_bytes; +using backend_stand_in::defect; +using backend_stand_in::done; +using backend_stand_in::first_op; +using backend_stand_in::init_op; +using backend_stand_in::move_op; +using backend_stand_in::nothrow; +using backend_stand_in::present; +using backend_stand_in::reset_op; +using backend_stand_in::result_of; +using backend_stand_in::result_void; +using backend_stand_in::shape_of; +using backend_stand_in::sized_input; +using backend_stand_in::sized_output; +using backend_stand_in::state_operations; +using backend_stand_in::state_type; + +inline constexpr std::size_t key_size = backend::x25519_key_size; + +// box: init with the own secret key and the peer's public key, seal and open. + +inline constexpr int seal_op = first_op; +inline constexpr int open_op = first_op + 1; +inline constexpr int box_secret_shape = first_op + 2; +inline constexpr int box_peer_shape = first_op + 3; +inline constexpr int box_seal_nonce_shape = first_op + 4; +inline constexpr int box_open_nonce_shape = first_op + 5; +inline constexpr int box_tag_shape = first_op + 6; +inline constexpr int box_check_shape = first_op + 7; + +template +struct box_stand_in : state_operations { + static result_of init( + state_type& /*engine_state*/, sized_input /*own_key*/, + sized_input /*peer_key*/) noexcept(nothrow) + requires present + { + return done>(); + } + + static result_of seal( + state_type& /*engine_state*/, + sized_input /*nonce_bytes*/, + bytes /*data_bytes*/, + shape_of&, + boost::hash2::digest&> /*tag_bytes*/) noexcept(nothrow) + requires present + { + return done>(); + } + + static result_of open( + state_type& /*engine_state*/, + sized_input /*nonce_bytes*/, + bytes /*data_bytes*/, + shape_of const&, + std::array const&> /*tag_bytes*/) noexcept(nothrow) + requires present + { + return done>(); + } +}; + +template +struct box_satisfied { + static constexpr bool value = backend::box>; +}; + +static_assert(backend_stand_in::rejects_every_defect); +static_assert(backend_stand_in::rejects_every_shape); + +// sealed_box: static seal and open, no state. + +inline constexpr int sealed_seal_op = 0; +inline constexpr int sealed_open_op = 1; +inline constexpr int sealed_seal_public_shape = 2; +inline constexpr int sealed_open_public_shape = 3; +inline constexpr int sealed_open_secret_shape = 4; + +template +struct sealed_box_stand_in { + static result_of seal( + sized_input /*recipient_public_key*/, + const_bytes /*message_bytes*/, + bytes /*sealed_output*/) noexcept(nothrow) + requires present + { + return done>(); + } + + static result_of open( + sized_input /*recipient_public_key*/, + sized_input /*recipient_secret_key*/, + const_bytes /*sealed_bytes*/, + bytes /*message_output*/) noexcept(nothrow) + requires present + { + return done>(); + } +}; + +template +struct sealed_box_satisfied { + static constexpr bool value = backend::sealed_box>; +}; + +static_assert( + backend_stand_in::rejects_every_defect); +static_assert( + backend_stand_in::rejects_every_shape); + +// kx: static seed_keypair, client and server, no state; every key's size. + +inline constexpr int seed_keypair_op = 0; +inline constexpr int client_op = 1; +inline constexpr int server_op = 2; +inline constexpr int seed_shape = 3; +inline constexpr int seed_secret_shape = 4; +/** The first of client's five shapes (secret, public, peer, receive, transmit); server's follow. */ +inline constexpr int client_shape = 5; +inline constexpr int server_shape = 10; + +template +struct kx_stand_in { + static result_of seed_keypair( + sized_input /*seed_bytes*/, + sized_output /*secret_key_output*/) noexcept(nothrow) + requires present + { + return done>(); + } + + static result_of client( + sized_input /*secret_key*/, + sized_input /*public_key*/, + sized_input /*peer_public_key*/, + sized_output /*receive_key*/, + sized_output /*transmit_key*/) noexcept(nothrow) + requires present + { + return done>(); + } + + static result_of server( + sized_input /*secret_key*/, + sized_input /*public_key*/, + sized_input /*peer_public_key*/, + sized_output /*receive_key*/, + sized_output /*transmit_key*/) noexcept(nothrow) + requires present + { + return done>(); + } +}; + +template +struct kx_satisfied { + static constexpr bool value = backend::kx>; +}; + +static_assert( + backend_stand_in::rejects_every_defect); +static_assert(backend_stand_in::rejects_every_shape< + kx_satisfied, seed_shape, seed_secret_shape, client_shape, client_shape + 1, + client_shape + 2, client_shape + 3, client_shape + 4, server_shape, server_shape + 1, + server_shape + 2, server_shape + 3, server_shape + 4>); + +} // namespace + +int main() { + return 0; +} diff --git a/test/backend_concepts_symmetric_test.cpp b/test/backend_concepts_symmetric_test.cpp index 29b4422..ae674d1 100644 --- a/test/backend_concepts_symmetric_test.cpp +++ b/test/backend_concepts_symmetric_test.cpp @@ -4,12 +4,15 @@ // accompanying file LICENSE_1_0.txt or copy at // https://www.boost.org/LICENSE_1_0.txt) -// The symmetric and box capability concepts of detail::backend (backend-concepts design D1, D2, -// D4): every engine the table lists for the AES block cipher, GCM, the AEADs, the secret boxes, -// the raw stream cipher, the single-use key authenticator, crypto_box, the sealed box and -// crypto_kx satisfies its concept on every backend this target links; and a stand-in engine that -// lacks one operation, or gives it the wrong result or no noexcept, satisfies none, one stand-in -// per requirement, so that each requirement is shown to be checked. +// The symmetric capability concepts of detail::backend (backend-concepts design D1, D2, D4): +// every engine the table lists for the AES block cipher, GCM, the AEADs, the secret boxes, the raw +// stream cipher and the single-use key authenticator satisfies its concept on every backend this +// target links; and for each concept a stand-in engine (support/backend_stand_in.hpp) satisfies +// it when complete and fails it with any one operation missing, not noexcept or of another result, +// or any one size or type parameter of another shape, so that each requirement is shown to be +// checked. + +#include "support/backend_stand_in.hpp" #include #include @@ -17,11 +20,7 @@ #include #include #include -#include #include -#include -#include -#include #include #ifdef WEBCPP_CRYPTO_HAS_BEARSSL @@ -38,12 +37,10 @@ #endif #include -#include #include #include #include -#include namespace backend = webcpp::crypto::detail::backend; namespace detail = webcpp::crypto::detail; @@ -52,8 +49,6 @@ namespace backends = webcpp::crypto::backends; namespace { -using fallible = boost::system::result; - // The shipped engines, on the backends this target links (the table's cells, crypto.json). template @@ -106,364 +101,291 @@ static_assert(backend::authenticated_box< static_assert(backend::authenticated_box< detail::secretbox_engine, families::secretbox_xchacha20poly1305>); -static_assert(backend::box, - families::box_xsalsa20poly1305>); -static_assert(backend::box, - families::box_xchacha20poly1305>); -static_assert( - backend::sealed_box>); -static_assert(backend::kx>); #endif -// Stand-ins: a complete engine per concept, then one stand-in per requirement that breaks it. - -struct stand_in_state {}; - -using digest = boost::hash2::digest<16>; -using tag_array = std::array; -using bytes = std::span; -using const_bytes = std::span; - -struct keyed_base { - using state = stand_in_state; - - static fallible init(stand_in_state& /*engine_state*/, const_bytes /*input_bytes*/) noexcept { - return {}; - } - - static void move(stand_in_state& /*engine_state*/, state& /*argument*/) noexcept {} - - static void reset(stand_in_state& /*engine_state*/) noexcept {} -}; - -struct block_complete : keyed_base { - static fallible encrypt(stand_in_state& /*engine_state*/, bytes /*data_bytes*/) noexcept { - return {}; - } - - static fallible decrypt(stand_in_state& /*engine_state*/, bytes /*data_bytes*/) noexcept { - return {}; - } -}; - -struct block_without_decrypt : keyed_base { - static fallible encrypt(stand_in_state& /*engine_state*/, bytes /*data_bytes*/) noexcept { - return {}; - } -}; - -struct block_throwing_encrypt : keyed_base { - static fallible encrypt(stand_in_state& /*engine_state*/, bytes /*data_bytes*/) { return {}; } - - static fallible decrypt(stand_in_state& /*engine_state*/, bytes /*data_bytes*/) noexcept { - return {}; - } -}; - -struct block_without_reset { - using state = stand_in_state; - - static fallible init(stand_in_state& /*engine_state*/, const_bytes /*input_bytes*/) noexcept { - return {}; - } - - static void move(stand_in_state& /*engine_state*/, state& /*argument*/) noexcept {} - - static fallible encrypt(stand_in_state& /*engine_state*/, bytes /*data_bytes*/) noexcept { - return {}; - } - - static fallible decrypt(stand_in_state& /*engine_state*/, bytes /*data_bytes*/) noexcept { - return {}; - } -}; - -struct block_bool_init { - using state = stand_in_state; - - static bool init(stand_in_state& /*engine_state*/, const_bytes /*input_bytes*/) noexcept { - return true; - } - - static void move(stand_in_state& /*engine_state*/, state& /*argument*/) noexcept {} - - static void reset(stand_in_state& /*engine_state*/) noexcept {} - - static fallible encrypt(stand_in_state& /*engine_state*/, bytes /*data_bytes*/) noexcept { - return {}; - } - - static fallible decrypt(stand_in_state& /*engine_state*/, bytes /*data_bytes*/) noexcept { - return {}; - } -}; - -static_assert(backend::block_cipher); -static_assert(!backend::block_cipher); -static_assert(!backend::block_cipher); -static_assert(!backend::block_cipher); -static_assert(!backend::block_cipher); - -struct gcm_operations : keyed_base { - static fallible start(stand_in_state& /*engine_state*/, bool /*is_sealing*/, - const_bytes /*input_bytes*/, const_bytes /*input_bytes_2*/) noexcept { - return {}; - } - - static fallible run(stand_in_state& /*engine_state*/, bytes /*data_bytes*/) noexcept { - return {}; - } - - static fallible tag(stand_in_state& /*engine_state*/, digest& /*tag_output*/) noexcept { - return {}; - } -}; - -struct gcm_complete : gcm_operations { - static fallible check(stand_in_state& /*engine_state*/, - tag_array const& /*expected_tag*/) noexcept { - return {}; - } - - static void abandon(stand_in_state& /*engine_state*/) noexcept {} -}; - -struct gcm_without_check : gcm_operations { - static void abandon(stand_in_state& /*engine_state*/) noexcept {} -}; - -struct gcm_without_abandon : gcm_operations { - static fallible check(stand_in_state& /*engine_state*/, - tag_array const& /*expected_tag*/) noexcept { - return {}; - } -}; - -static_assert(backend::gcm); -static_assert(!backend::gcm); -static_assert(!backend::gcm); - -struct aead_complete : keyed_base { - static fallible seal(stand_in_state& /*engine_state*/, const_bytes /*input_bytes*/, - const_bytes /*input_bytes_2*/, bytes /*data_bytes*/, - digest& /*tag_output*/) noexcept { - return {}; - } - - static fallible open(stand_in_state& /*engine_state*/, const_bytes /*input_bytes*/, - const_bytes /*input_bytes_2*/, bytes /*data_bytes*/, - tag_array const& /*expected_tag*/) noexcept { - return {}; +// Stand-ins, their sizes and types the families' own. + +using backend_stand_in::bytes; +using backend_stand_in::const_bytes; +using backend_stand_in::defect; +using backend_stand_in::done; +using backend_stand_in::first_op; +using backend_stand_in::init_op; +using backend_stand_in::keyed_operations; +using backend_stand_in::move_op; +using backend_stand_in::nothrow; +using backend_stand_in::present; +using backend_stand_in::reset_op; +using backend_stand_in::result_of; +using backend_stand_in::result_void; +using backend_stand_in::shape_of; +using backend_stand_in::sized_input; +using backend_stand_in::sized_output; +using backend_stand_in::state_type; + +/** A tag output of `Size` bytes, or of one byte less under shape `Target`'s defect. */ +template +using tag_output = + shape_of&, boost::hash2::digest&>; + +/** An expected tag of `Size` bytes, or of one byte less under shape `Target`'s defect. */ +template +using expected_tag = shape_of const&, + std::array const&>; + +// block_cipher: encrypt and decrypt, over keyed state operations. + +inline constexpr int encrypt_op = first_op; +inline constexpr int decrypt_op = first_op + 1; + +template +struct block_stand_in : keyed_operations { + static result_of encrypt( + state_type& /*engine_state*/, bytes /*block_bytes*/) noexcept(nothrow) + requires present + { + return done>(); + } + + static result_of decrypt( + state_type& /*engine_state*/, bytes /*block_bytes*/) noexcept(nothrow) + requires present + { + return done>(); } }; -struct aead_without_open : keyed_base { - static fallible seal(stand_in_state& /*engine_state*/, const_bytes /*input_bytes*/, - const_bytes /*input_bytes_2*/, bytes /*data_bytes*/, - digest& /*tag_output*/) noexcept { - return {}; - } +template +struct block_satisfied { + static constexpr bool value = backend::block_cipher>; }; -struct aead_without_associated_data : keyed_base { - static fallible seal(stand_in_state& /*engine_state*/, const_bytes /*input_bytes*/, - bytes /*data_bytes*/, digest& /*tag_output*/) noexcept { - return {}; - } - - static fallible open(stand_in_state& /*engine_state*/, const_bytes /*input_bytes*/, - bytes /*data_bytes*/, tag_array const& /*expected_tag*/) noexcept { - return {}; - } -}; - -static_assert(backend::aead); -static_assert(!backend::aead); -static_assert(!backend::aead); - -struct stream_complete : keyed_base { - static fallible apply(stand_in_state& /*engine_state*/, - std::span /*nonce_bytes*/, - std::uint32_t /*initial_counter*/, bytes /*data_bytes*/) noexcept { - return {}; +static_assert(backend_stand_in::rejects_every_defect); + +// gcm: start, run, tag, check and abandon; the tag's two sizes. + +inline constexpr int start_op = first_op; +inline constexpr int run_op = first_op + 1; +inline constexpr int tag_op = first_op + 2; +inline constexpr int check_op = first_op + 3; +inline constexpr int abandon_op = first_op + 4; +inline constexpr int gcm_tag_shape = first_op + 5; +inline constexpr int gcm_check_shape = first_op + 6; +inline constexpr std::size_t gcm_tag_size = 16; + +template +struct gcm_stand_in : keyed_operations { + static result_of start( + state_type& /*engine_state*/, bool /*is_sealing*/, const_bytes /*nonce_bytes*/, + const_bytes /*associated_data_bytes*/) noexcept(nothrow) + requires present + { + return done>(); + } + + static result_of run( + state_type& /*engine_state*/, bytes /*data_bytes*/) noexcept(nothrow) + requires present + { + return done>(); + } + + static result_of tag( + state_type& /*engine_state*/, + tag_output /*tag_bytes*/) noexcept(nothrow) + requires present + { + return done>(); + } + + static result_of check( + state_type& /*engine_state*/, + expected_tag /*tag_bytes*/) noexcept(nothrow) + requires present + { + return done>(); + } + + static result_of abandon(state_type& /*engine_state*/) noexcept( + nothrow) + requires present + { + return done>(); } }; -struct stream_without_counter : keyed_base { - static fallible apply(stand_in_state& /*engine_state*/, - std::span /*nonce_bytes*/, - bytes /*data_bytes*/) noexcept { - return {}; - } +template +struct gcm_satisfied { + static constexpr bool value = backend::gcm>; }; -struct stream_without_move { - using state = stand_in_state; - - static fallible init(stand_in_state& /*engine_state*/, const_bytes /*input_bytes*/) noexcept { - return {}; - } - - static void reset(stand_in_state& /*engine_state*/) noexcept {} - - static fallible apply(stand_in_state& /*engine_state*/, - std::span /*nonce_bytes*/, - std::uint32_t /*initial_counter*/, bytes /*data_bytes*/) noexcept { - return {}; +static_assert( + backend_stand_in::rejects_every_defect); +static_assert(backend_stand_in::rejects_every_shape); + +// aead: seal and open, the family's tag size. + +using aead_family = families::chacha20_poly1305; +inline constexpr int seal_op = first_op; +inline constexpr int open_op = first_op + 1; +inline constexpr int aead_tag_shape = first_op + 2; +inline constexpr int aead_check_shape = first_op + 3; + +template +struct aead_stand_in : keyed_operations { + static result_of seal( + state_type& /*engine_state*/, const_bytes /*nonce_bytes*/, + const_bytes /*associated_data_bytes*/, bytes /*data_bytes*/, + tag_output /*tag_bytes*/) noexcept(nothrow) + requires present + { + return done>(); + } + + static result_of open( + state_type& /*engine_state*/, const_bytes /*nonce_bytes*/, + const_bytes /*associated_data_bytes*/, bytes /*data_bytes*/, + expected_tag /*tag_bytes*/) noexcept(nothrow) + requires present + { + return done>(); } }; -static_assert(backend::stream_cipher); -static_assert(!backend::stream_cipher); -static_assert(!backend::stream_cipher); -// A stream cipher is no AEAD, and an AEAD no stream cipher. -static_assert(!backend::aead); -static_assert(!backend::stream_cipher); - -struct authenticator_complete { - static fallible authenticate(std::span /*key_bytes*/, - const_bytes /*input_bytes*/, - std::span /*tag_bytes*/) noexcept { - return {}; - } +template +struct aead_satisfied { + static constexpr bool value = backend::aead, aead_family>; }; -struct authenticator_throwing { - static fallible authenticate(std::span /*key_bytes*/, - const_bytes /*input_bytes*/, - std::span /*tag_bytes*/) { - return {}; +static_assert(backend_stand_in::rejects_every_defect); +static_assert( + backend_stand_in::rejects_every_shape); + +// authenticated_box: seal and open without associated data, the family's nonce and tag sizes. + +using secretbox_family = families::secretbox_xsalsa20poly1305; +inline constexpr int secretbox_seal_nonce_shape = first_op + 2; +inline constexpr int secretbox_open_nonce_shape = first_op + 3; +inline constexpr int secretbox_tag_shape = first_op + 4; +inline constexpr int secretbox_check_shape = first_op + 5; + +template +struct secretbox_stand_in : keyed_operations { + static result_of seal( + state_type& /*engine_state*/, + sized_input + /*nonce_bytes*/, + bytes /*data_bytes*/, + tag_output /*tag_bytes*/) noexcept(nothrow) + requires present + { + return done>(); + } + + static result_of open( + state_type& /*engine_state*/, + sized_input + /*nonce_bytes*/, + bytes /*data_bytes*/, + expected_tag + /*tag_bytes*/) noexcept(nothrow) + requires present + { + return done>(); } }; -struct authenticator_void { - static void authenticate(std::span /*key_bytes*/, - const_bytes /*input_bytes*/, - std::span /*tag_bytes*/) noexcept {} +template +struct secretbox_satisfied { + static constexpr bool value = + backend::authenticated_box, secretbox_family>; }; -static_assert(backend::authenticator); -static_assert(!backend::authenticator); -static_assert(!backend::authenticator); - -struct box_like_secretbox : keyed_base { - static fallible seal(stand_in_state& /*engine_state*/, - std::span /*nonce_bytes*/, bytes /*data_bytes*/, - digest& /*tag_output*/) noexcept { - return {}; - } - - static fallible open(stand_in_state& /*engine_state*/, - std::span /*nonce_bytes*/, bytes /*data_bytes*/, - tag_array const& /*expected_tag*/) noexcept { - return {}; +static_assert(backend_stand_in::rejects_every_defect); +static_assert(backend_stand_in::rejects_every_shape); + +// stream_cipher: apply, the family's nonce size and counter type. The counter is taken by value, +// so a wider integer would convert; the wrong shape is a counter the family's type cannot become. + +using stream_family = families::chacha20; +inline constexpr int apply_op = first_op; +inline constexpr int stream_nonce_shape = first_op + 1; +inline constexpr int stream_counter_shape = first_op + 2; + +/** A counter the family's `counter_type` does not convert to. */ +struct unrelated_counter {}; + +template +struct stream_stand_in : keyed_operations { + static result_of apply( + state_type& /*engine_state*/, + sized_input /*nonce_bytes*/, + shape_of + /*initial_counter*/, + bytes /*data_bytes*/) noexcept(nothrow) + requires present + { + return done>(); } }; -struct secretbox_wrong_nonce : keyed_base { - static fallible seal(stand_in_state& /*engine_state*/, - std::span /*nonce_bytes*/, bytes /*data_bytes*/, - digest& /*tag_output*/) noexcept { - return {}; - } - - static fallible open(stand_in_state& /*engine_state*/, - std::span /*nonce_bytes*/, bytes /*data_bytes*/, - tag_array const& /*expected_tag*/) noexcept { - return {}; - } +template +struct stream_satisfied { + static constexpr bool value = backend::stream_cipher, stream_family>; }; -static_assert(backend::authenticated_box); static_assert( - !backend::authenticated_box); - -struct box_complete { - using state = stand_in_state; - - static fallible init(stand_in_state& /*engine_state*/, - std::span /*key_bytes*/, - std::span /*key_bytes_2*/) noexcept { - return {}; - } - - static void move(stand_in_state& /*engine_state*/, state& /*argument*/) noexcept {} - - static void reset(stand_in_state& /*engine_state*/) noexcept {} - - static fallible seal(stand_in_state& /*engine_state*/, - std::span /*nonce_bytes*/, bytes /*data_bytes*/, - digest& /*tag_output*/) noexcept { - return {}; - } - - static fallible open(stand_in_state& /*engine_state*/, - std::span /*nonce_bytes*/, bytes /*data_bytes*/, - tag_array const& /*expected_tag*/) noexcept { - return {}; - } -}; - -// A secret box's init takes one key; a box's, the own secret key and the peer's public key. -static_assert(backend::box); -static_assert(!backend::box); -static_assert(!backend::authenticated_box); - -struct sealed_box_complete { - static fallible seal(std::span /*key_bytes*/, - const_bytes /*input_bytes*/, bytes /*data_bytes*/) noexcept { - return {}; - } - - static fallible open(std::span /*key_bytes*/, - std::span /*key_bytes_2*/, - const_bytes /*input_bytes*/, bytes /*data_bytes*/) noexcept { - return {}; + backend_stand_in::rejects_every_defect); +static_assert(backend_stand_in::rejects_every_shape); + +// authenticator: one static authenticate, no state; the family's key and tag sizes. + +using authenticator_family = families::poly1305; +inline constexpr int authenticate_op = 0; +inline constexpr int authenticator_key_shape = 1; +inline constexpr int authenticator_tag_shape = 2; + +template +struct authenticator_stand_in { + static result_of authenticate( + sized_input + /*key_bytes*/, + const_bytes /*message_bytes*/, + sized_output + /*tag_bytes*/) noexcept(nothrow) + requires present + { + return done>(); } }; -struct sealed_box_without_open { - static fallible seal(std::span /*key_bytes*/, - const_bytes /*input_bytes*/, bytes /*data_bytes*/) noexcept { - return {}; - } -}; - -static_assert(backend::sealed_box); -static_assert(!backend::sealed_box); - -using key32 = std::span; -using out32 = std::span; - -struct kx_complete { - static fallible seed_keypair(key32 /*key_bytes*/, out32 /*output_bytes*/) noexcept { - return {}; - } - - static fallible client(key32 /*key_bytes*/, key32 /*key_bytes_2*/, key32 /*key_bytes_3*/, - out32 /*output_bytes*/, out32 /*output_bytes_2*/) noexcept { - return {}; - } - - static fallible server(key32 /*key_bytes*/, key32 /*key_bytes_2*/, key32 /*key_bytes_3*/, - out32 /*output_bytes*/, out32 /*output_bytes_2*/) noexcept { - return {}; - } +template +struct authenticator_satisfied { + static constexpr bool value = + backend::authenticator, authenticator_family>; }; -struct kx_without_server { - static fallible seed_keypair(key32 /*key_bytes*/, out32 /*output_bytes*/) noexcept { - return {}; - } - - static fallible client(key32 /*key_bytes*/, key32 /*key_bytes_2*/, key32 /*key_bytes_3*/, - out32 /*output_bytes*/, out32 /*output_bytes_2*/) noexcept { - return {}; - } -}; +static_assert(backend_stand_in::rejects_every_defect); +static_assert(backend_stand_in::rejects_every_shape< + authenticator_satisfied, authenticator_key_shape, authenticator_tag_shape>); -static_assert(backend::kx); -static_assert(!backend::kx); +// The concepts do not overlap: a stream cipher is no AEAD, an AEAD no stream cipher, and a secret +// box (no associated data) no AEAD. +static_assert(!backend::aead, aead_family>); +static_assert(!backend::stream_cipher, stream_family>); +static_assert(!backend::aead, aead_family>); } // namespace diff --git a/test/support/backend_stand_in.hpp b/test/support/backend_stand_in.hpp new file mode 100644 index 0000000..c60c06d --- /dev/null +++ b/test/support/backend_stand_in.hpp @@ -0,0 +1,155 @@ +// 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) + +// The defect selector of the capability concept tests (backend-concepts design D4, task 1.1's +// review I1): a stand-in engine is a class template over a `defect`, complete when the defect is +// none, and otherwise with exactly one operation missing, not noexcept or of another result, or +// with exactly one parameter of another shape (a size or a type the concept does not take). +// `rejects_every_defect` then shows that the concept takes the complete engine and refuses every +// defect of every operation and shape it lists, so that each requirement is shown to be checked. +// +// Operations and shapes are numbered by the stand-in, the shared state ones first: `move_op`, +// `reset_op` and `init_op`; a stand-in's own operations from `first_op`. + +#ifndef WEBCPP_TEST_CRYPTO_SUPPORT_BACKEND_STAND_IN_HPP +#define WEBCPP_TEST_CRYPTO_SUPPORT_BACKEND_STAND_IN_HPP + +#include + +#include +#include +#include +#include + +namespace backend_stand_in { + +/** An operation's result where it can fail and returns nothing. */ +using result_void = boost::system::result; +/** A view of bytes to write. */ +using bytes = std::span; +/** A view of bytes to read. */ +using const_bytes = std::span; +/** A view of exactly `Size` bytes to read. */ +template +using fixed_input = std::span; +/** A view of exactly `Size` bytes to write. */ +template +using fixed_output = std::span; + +/** What is wrong with a stand-in, if anything. */ +enum class flaw : std::uint8_t { + none, ///< The stand-in is complete. + missing, ///< The operation is absent. + throwing, ///< The operation is not noexcept. + wrong_result, ///< The operation returns another type. + wrong_shape, ///< The parameter has another size or type. +}; + +/** One defect: the operation or shape it hits, and how. */ +struct defect { + int target = -1; ///< The operation's or shape's number. + flaw kind = flaw::none; ///< What is wrong with it. +}; + +/** The number of a state's `move`. */ +inline constexpr int move_op = 0; +/** The number of a state's `reset`. */ +inline constexpr int reset_op = 1; +/** The number of a keyed state's `init`. */ +inline constexpr int init_op = 2; +/** The first number of a stand-in's own operations. */ +inline constexpr int first_op = 3; + +/** Whether operation `Target` is present under `Defect`. */ +template +inline constexpr bool present = Defect.target != Target || Defect.kind != flaw::missing; + +/** Whether operation `Target` is noexcept under `Defect`. */ +template +inline constexpr bool nothrow = Defect.target != Target || Defect.kind != flaw::throwing; + +/** Another result than `Result`: `bool` for `result`, `result` for any other. */ +template +using other_result = std::conditional_t, bool, result_void>; + +/** Operation `Target`'s result under `Defect`: `Result`, or another one. */ +template +using result_of = std::conditional_t, Result>; + +/** Shape `Target`'s parameter type under `Defect`: `Right`, or `Wrong`. */ +template +using shape_of = + std::conditional_t; + +/** A fixed view of `Size` bytes to read, or of one byte less under shape `Target`'s defect. */ +template +using sized_input = shape_of, fixed_input>; + +/** A fixed view of `Size` bytes to write, or of one byte less under shape `Target`'s defect. */ +template +using sized_output = shape_of, fixed_output>; + +/** The default value of `Result`, the stand-ins' every result. */ +template +constexpr Result done() noexcept { + return Result(); +} + +/** The state of every stand-in. */ +struct state_type {}; + +/** `move` and `reset`, each of which a defect may hit. */ +template +struct state_operations { + using state = state_type; + + static result_of move( + state& /*target_state*/, state& /*source_state*/) noexcept(nothrow) + requires present + { + return done>(); + } + + static result_of reset(state& /*engine_state*/) noexcept( + nothrow) + requires present + { + return done>(); + } +}; + +/** `move`, `reset` and a one-key `init(state, key)`, each of which a defect may hit. */ +template +struct keyed_operations : state_operations { + static result_of init( + state_type& /*engine_state*/, const_bytes /*key_bytes*/) noexcept(nothrow) + requires present + { + return done>(); + } +}; + +/** + * Whether `Satisfied::value` holds for the complete stand-in and fails for each of the + * missing, throwing and wrong-result defects of every operation in `Operations`. + */ +template