diff --git a/CODING-GUIDELINES.md b/CODING-GUIDELINES.md index 43264225..9df9d776 100644 --- a/CODING-GUIDELINES.md +++ b/CODING-GUIDELINES.md @@ -6921,8 +6921,9 @@ cryptography over widely used C libraries that webcpp builds from their sources: AES-GCM and the AES block cipher, a `random_device` on the system's generator, a constant-time `equal`, and secp256k1's BIP340 Schnorr and ECDSA signatures, ECDH, strict DER and the x-only tweak. Version 0.1 is the -proposal's increments 1 and 2, as the owner set them on 2026-11-14; what is -decided and not built is in chapter 17. +proposal's increments 1 and 2, as the owner set them on 2026-11-14; OpenSSL's +memory option is built after it (its block below, unreleased); what is decided +and not built is in chapter 17. **Scope.** @@ -7097,9 +7098,9 @@ decided and not built is in chapter 17. **Errors and exceptions.** - **Every operation that can fail returns `boost::system::result` or - `result`; the category `webcpp.crypto` declares the values 1 to 12, - fixed and never reused, and 13 and 14 are reserved for OpenSSL's memory - option.** The owner's rule that every library works with exceptions on and + `result`; the category `webcpp.crypto` declares the values 1 to 14, + fixed and never reused; 13 and 14, reserved in 0.1, are OpenSSL's memory + option's.** The owner's rule that every library works with exceptions on and off; a value once given is never given again. Source: `include/webcpp/crypto/error.hpp` (libs/crypto); a ruling of the plan's revision of 2026-11-14 (proposals, `reference/plan.md`). @@ -7208,16 +7209,109 @@ decided and not built is in chapter 17. the user gives; no seal, open, `encrypt`, `fill`, signature, `ecdh` or verification allocates on any backend; OpenSSL's `make` allocates its context through OpenSSL's own allocator, which its page states and - `allocation_openssl` counts.** The owner's allocators decision, with - OpenSSL's departure accepted on 2026-10-11 until its memory option. + `allocation_openssl` counts, unless the program takes OpenSSL's memory + option (below), which sends every allocation of OpenSSL's to the program's + resource, natively.** The owner's allocators decision, with OpenSSL's + departure accepted on 2026-10-11; the option serves a program that wants + OpenSSL's allocations in its own resource. Source: `libraries/crypto/openspec/changes/genesis/design.md` D17 - (proposals); `test/support/counting.hpp` (libs/crypto). + (proposals); `test/support/counting.hpp`, `test/openssl_memory/Jamfile` + (libs/crypto). - **Key schedules inside webcpp's objects are wiped on destruction and on move by stores through a `volatile` pointer followed by `std::atomic_signal_fence(std::memory_order_seq_cst)`.** The optimiser cannot drop these stores, where it can drop a `memset`. Source: `include/webcpp/crypto/detail/wipe.hpp` (libs/crypto). +**OpenSSL's memory option (built after 0.1.0; plan Tasks 15, 16 and 33).** + +- **`WEBCPP_CRYPTO_OPENSSL_MEMORY_RESOURCE` sends every allocation of + OpenSSL in the process to a `std::pmr::memory_resource`; the program gives + it with `/webcpp/crypto//openssl` in its project's requirements, + and `config.hpp` refuses it with an `#error` on every WebAssembly target, + Emscripten, wasip2 and wasip3, and without OpenSSL linked.** The owner's + decision of 2026-10-11 made `CRYPTO_set_mem_functions` an option of the + config, so that a program that wants OpenSSL's allocations in its own + allocator can say so; the library beside the define keeps every target that + gets it linked with OpenSSL. The owner's decision of 2026-11-17 made it + native only: on WebAssembly a program's default backend is BearSSL, which + allocates nothing, and the option's tests are native designs. + Source: `include/webcpp/crypto/config.hpp`, `doc/memory.adoc` + (libs/crypto); `libraries/crypto/openspec/changes/genesis/reference/spec.md` + section 3.7 and `design.md` D16 (proposals). +- **Under the option the program defines + `webcpp::crypto::openssl_memory_resource()` once, a `noexcept` function that + returns a `std::pmr::memory_resource&`, and calls + `backends::openssl::install_memory_functions()` as the first statement of + `main`; webcpp never installs from a static initialiser.** OpenSSL's hook + carries no user data, so any other form would keep the resource in a global + variable of webcpp's, which the rulebook forbids; static initialisation + order is unspecified and cannot return an error. + Source: `include/webcpp/crypto/backends/openssl.hpp`, + `example/openssl_memory.cpp` (libs/crypto). +- **The program's resource is never destroyed, is thread-safe wherever + OpenSSL may run on two threads, and is reached without an exception; the + example makes it with `new` and never deletes it.** OpenSSL frees through it + after `main`. + Source: `example/openssl_memory.cpp` (libs/crypto). +- **`install_memory_functions()` only reads OpenSSL's state until its guards + pass: webcpp's adapter already in place is a success that changes nothing, + functions other than OpenSSL's three defaults are + `memory_functions_refused`, then the resource is constructed, then the + adapter is installed, and OpenSSL's refusal is `memory_functions_refused`; + every OpenSSL `make` under the option checks that the adapter is in place + and otherwise returns `memory_functions_not_installed`.** Replacing another + party's functions would make OpenSSL free that party's blocks through + webcpp's adapter; a forgotten install gives an error, not an allocation + outside the resource. The defaults are told by their three addresses alone, + so a libcrypto DLL, which webcpp never builds, is refused (chapter 19). + Source: `include/webcpp/crypto/backends/openssl.hpp` (libs/crypto). +- **The adapter is three `extern "C"`, `inline`, `noexcept` functions of + external linkage, each block headed by its size and aligned to the larger + of `alignof(std::max_align_t)` and `__STDCPP_DEFAULT_NEW_ALIGNMENT__`; with + exceptions each call into the resource sits in a `try` whose `catch (...)` + returns null.** OpenSSL calls them as C functions and frees without a size; + no exception may cross OpenSSL. + Source: `include/webcpp/crypto/backends/openssl.hpp` (libs/crypto). +- **On MSVC `backends/openssl.hpp` carries + `#pragma detect_mismatch("webcpp_crypto_openssl_memory", ...)`, `"resource"` + with the option and `"openssl"` without it.** Two files of one program + compiled one with the option and one without then fail with LNK2038. + Source: `include/webcpp/crypto/backends/openssl.hpp`, + `test/msvc/memory_two_files` (libs/crypto). +- **Two allocations stay outside the resource, the C library's `FILE` while + OpenSSL reads its configuration file and OpenSSL's secure heap; the + resource must serve OpenSSL's initialisation: one that fails there leaves + OpenSSL unusable for the rest of the process on an OpenSSL built without + threads, and on one built with threads (MSVC's only build) the call never + returns; on Windows a thread that used OpenSSL calls + `OPENSSL_thread_stop()` before it ends, or its blocks stay in the resource; + the program's code that uses the backend lives in one module; the page + states each.** No hook of OpenSSL's reaches the first two; OpenSSL keeps a + failed initialisation, and with threads reports the failure through an + error queue that needs the initialisation running, which it re-enters; the + static libcrypto on Windows is never told that a thread ends; a second + module has its own copy of the adapter, whose address the guard does not + recognise. + Source: `doc/memory.adoc`, `example/openssl_memory.cpp`, + `test/openssl_memory/fail_init_test.cpp` (libs/crypto). +- **The option's programs are native, in `test/openssl_memory`, each declared + with exceptions and without by the rule `memory-run`: `adapter`; + `everything`, `late`, `foreign`, `fresh_install`, `forgotten`, `replaced`, + `second_install`, `fail_second`, `fail_init`, `fail_init_threads` (the + same program on an OpenSSL built with threads) and `handshake`, which count + every call of the C library's allocator through an interposer per system; + and `neighbours`, beside Asio's TLS and libdatachannel; `header_alone` and + the `windows_h` twins compile. The fixtures `memory_option`, + `memory_missing_definition_install`, `memory_missing_definition_make`, + `memory_two_files` and `openssl_memory_on_emscripten` build the user's + Jamfiles, and `test/messages` and `test/msvc` check each `#error`.** The + counter proves the whole process, Beast's TLS and libdatachannel's DTLS + included; a forgotten definition fails the link, and the project's form the + page shows is built and run. + Source: `test/openssl_memory/Jamfile` (libs/crypto); + `tools/test/fixtures/crypto_jamfiles/` (webcpp). + **Build.** - **BearSSL, libsecp256k1 and ctaes are pinned submodules under @@ -8382,10 +8476,9 @@ What webcpp does not have yet, and the chapters that mention it: webcpp::asio (chapter 15's trystero entry). Authenticated encryption and randomness are webcpp::crypto's, and the relays' WebSockets Beast's. Trystero can then retain its protocol over those shared facilities. -- **crypto after 0.1** (chapter 17): OpenSSL's memory option, constant time - checked, sizes and throughput measured, the browser lane, the CI's nightly - level, NIST CAVP's vectors, ChaCha20-Poly1305, Botan, and trystero's code on - the library (its sealers, its random and its signer). +- **crypto after 0.1** (chapter 17): constant time checked, sizes and + throughput measured, the browser lane, the CI's nightly level, NIST CAVP's + vectors, ChaCha20-Poly1305 and Botan. - **The libraries web developers use most,** brought to C++: a JavaScript interpreter (QuickJS), libp2p, HTTP fetch. - **The site, increments 2 to 8.** Increment 1 is done: the site's @@ -8446,66 +8539,12 @@ each with its decisions as decided. While a proposal is open, its own change that builds it moves its decisions into chapter 14 or 15. Their conflicts with chapters 1 to 13 are in chapter 19. -### crypto, after 0.1 (accepted 2026-10-11; increments 1 and 2 built in 0.1.0, chapter 15) +### crypto, after 0.1 (accepted 2026-10-11; increments 1 and 2 built in 0.1.0 and the memory option built after 0.1, chapter 15) The plan (`reference/plan.md`, revised on 2026-11-14) built version 0.1 from -its increments 1 and 2; the owner deferred the rest, kept as written and -re-read before it runs. What 0.1 built is in chapter 15; what follows is -decided and not built. - -**OpenSSL's memory option (plan Tasks 15, 16 and 33).** - -- **`WEBCPP_CRYPTO_OPENSSL_MEMORY_RESOURCE` sends every allocation of - OpenSSL in the process to a `std::pmr::memory_resource`; the program gives - it with `/webcpp/crypto//openssl` in its project's requirements, and - `config.hpp` refuses it on Emscripten and without OpenSSL linked.** The - owner's decision of 2026-10-11 made `CRYPTO_set_mem_functions` an option of - the config, so that a program that wants OpenSSL's allocations in its own - allocator can say so; the library beside the define keeps every target that - gets it linked with OpenSSL. - Source: `libraries/crypto/openspec/changes/genesis/reference/spec.md` - section 3.7 (proposals). -- **Under the option the program defines - `webcpp::crypto::openssl_memory_resource()` once, a `noexcept` function that - returns a `std::pmr::memory_resource&`, and calls - `backends::openssl::install_memory_functions()` as the first statement of - `main`; webcpp never installs from a static initialiser.** OpenSSL's hook - carries no user data, so any other form would keep the resource in a global - variable of webcpp's, which the rulebook forbids; static initialisation - order is unspecified and cannot return an error. - Source: `libraries/crypto/openspec/changes/genesis/design.md` D16 - (proposals). -- **The program's resource is never destroyed, is thread-safe wherever - OpenSSL may run on two threads, and is reached without an exception.** - OpenSSL frees through it after `main`. - Source: `libraries/crypto/openspec/changes/genesis/design.md` D16 - (proposals). -- **`install_memory_functions()` only reads OpenSSL's state until its guards - pass: webcpp's adapter already in place is a success that changes nothing, - another party's functions are `memory_functions_refused`, then the resource - is constructed, then the adapter is installed, and OpenSSL's refusal is - `memory_functions_refused`; every OpenSSL `make` under the option checks - that the adapter is in place and otherwise returns - `memory_functions_not_installed`.** Replacing another party's functions - would make OpenSSL free that party's blocks through webcpp's adapter; a - forgotten install gives an error, not an allocation outside the resource. - The two values are 13 and 14, reserved in 0.1. - Source: `libraries/crypto/openspec/changes/genesis/reference/spec.md` - sections 2.9 and 3.7 (proposals). -- **The adapter is three `extern "C"`, `inline`, `noexcept` functions of - external linkage, each block headed by its size and aligned to the larger - of `alignof(std::max_align_t)` and `__STDCPP_DEFAULT_NEW_ALIGNMENT__`; with - exceptions each call into the resource sits in a `try` whose `catch (...)` - returns null.** OpenSSL calls them as C functions and frees without a size; - no exception may cross OpenSSL. - Source: `libraries/crypto/openspec/changes/genesis/reference/spec.md` - section 3.7 (proposals). -- **On MSVC `backends/openssl.hpp` carries - `#pragma detect_mismatch("webcpp_crypto_openssl_memory", ...)`, `"resource"` - with the option and `"openssl"` without it.** Two files of one program - compiled one with the option and one without then fail with LNK2038. - Source: `libraries/crypto/openspec/changes/genesis/reference/spec.md` - section 3.7 (proposals). +its increments 1 and 2, and OpenSSL's memory option after it; the owner +deferred the rest, kept as written and re-read before it runs. What is built +is in chapter 15; what follows is decided and not built. **Constant time (plan Task 20).** @@ -8601,18 +8640,6 @@ decided and not built. Source: `libraries/crypto/openspec/changes/genesis/reference/spec.md` section 3.6, and `design.md` D9 and D17 (proposals). -**trystero's migration (increment 3).** - -- **trystero moves onto the library: `ciphers::aes256_gcm` with its 16-byte - IVs replaces its native and browser sealers, `random_device::fill` its - `random`, `curves::secp256k1::keypair` its signer, and its direct use of - OpenSSL goes.** 0.1 built only its build's half: trystero links - `/third_party//secp256k1`, its code unchanged. Errors are values, so a - system without entropy fails the operation that needed the bytes with - `entropy_unavailable` where trystero aborts today. - Source: `libraries/crypto/openspec/changes/genesis/design.md` Migration - Plan (proposals). - **Open questions for the owner.** - Gate O2: the nightly level, built by Task 25b. @@ -9727,6 +9754,14 @@ says what the tree does. unsupported policy's inline namespace, since they honoured every setting; the tree puts them inside it, since natively the policy decides a data channel's `max_packet_life_time` of 0 (webrtc `871a4ef`). +- **crypto.** The genesis specification's section 3.7 and its plan's Task + 15 give OpenSSL's memory option a Windows branch that tells a libcrypto + DLL's default functions by `GetModuleHandleExW` and `GetProcAddress`, and + Task 16 measures the MSVC shared link. The tree compares the three + addresses alone: webcpp builds OpenSSL static only, so the branch could + never run in a test, and a libcrypto DLL (a program built without b2) is + refused with `memory_functions_refused`, which the page says. Serving such + a program takes the plan's branch back, never measured. - **asio.** The 2026-11-03 amendment reconciles default turn limit 100, supported targets, source-built TLS, private configuration/allocation fixes and guide order. Proposal source snapshots must name the final committed diff --git a/doc/releases/0.1.0.adoc b/doc/releases/0.1.0.adoc index 008e4dda..219ef1d6 100644 --- a/doc/releases/0.1.0.adoc +++ b/doc/releases/0.1.0.adoc @@ -67,7 +67,7 @@ None. == Tools and build * Fixed emscripten release programs linking unoptimized; they now link at `-O3`. -* Fixed WASI release programs carrying wasi-sdk's libc++ debug information, most of their size; they now link with `--strip-debug`. +* Fixed WASI release programs carrying wasi-sdk's `libc++` debug information, most of their size; they now link with `--strip-debug`. * Fixed clean CMake consumer audits rejecting system libraries and phony dependency rules. * Changed CI to consume verified prepared environments without rebuilding images in PR jobs. * Added verified archive caches and isolated container availability from unrelated CI lanes. diff --git a/libs/crypto b/libs/crypto index ae937e57..cc2b8010 160000 --- a/libs/crypto +++ b/libs/crypto @@ -1 +1 @@ -Subproject commit ae937e5743ce78e97ac2e9983838eca69fbc3f15 +Subproject commit cc2b80105ddad2849eb85a94817d92043cc5243f diff --git a/tools/test/fixtures/crypto_jamfiles/memory_missing_definition_install/Jamfile b/tools/test/fixtures/crypto_jamfiles/memory_missing_definition_install/Jamfile new file mode 100644 index 00000000..1695cf50 --- /dev/null +++ b/tools/test/fixtures/crypto_jamfiles/memory_missing_definition_install/Jamfile @@ -0,0 +1,15 @@ +# 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 memory option given as spec section 7.3 says, in the requirements of the +# program's project with OpenSSL beside it, and openssl_memory_resource() not +# defined. +# tag::jamfile[] +project app + : requirements WEBCPP_CRYPTO_OPENSSL_MEMORY_RESOURCE /webcpp/crypto//openssl ; + +exe app : app.cpp /webcpp/crypto//crypto ; +# end::jamfile[] diff --git a/tools/test/fixtures/crypto_jamfiles/memory_missing_definition_install/app.cpp b/tools/test/fixtures/crypto_jamfiles/memory_missing_definition_install/app.cpp new file mode 100644 index 00000000..b263040d --- /dev/null +++ b/tools/test/fixtures/crypto_jamfiles/memory_missing_definition_install/app.cpp @@ -0,0 +1,14 @@ +// 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) + +// Sets the memory option and installs OpenSSL's memory functions, but defines no +// webcpp::crypto::openssl_memory_resource(): the install odr-uses the adapter, which calls it, +// so the link fails naming the function (spec section 3.7). +#include + +int main() { + return webcpp::crypto::backends::openssl::install_memory_functions() ? 0 : 1; +} diff --git a/tools/test/fixtures/crypto_jamfiles/memory_missing_definition_install/memory_missing_definition_install.outcome b/tools/test/fixtures/crypto_jamfiles/memory_missing_definition_install/memory_missing_definition_install.outcome new file mode 100644 index 00000000..efb4e054 --- /dev/null +++ b/tools/test/fixtures/crypto_jamfiles/memory_missing_definition_install/memory_missing_definition_install.outcome @@ -0,0 +1,18 @@ +# 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 lines of each linker, measured on 2026-11-17: ld64 (Apple clang 21), GNU ld 2.44 (gcc +# 15.3, Debian 13). MSVC's link.exe is test/msvc/memory_missing_definition_*'s case. +[all] +targets: native +build: fails +# tag::messages[] +[native-darwin] +contains: Undefined symbols for architecture +contains: "webcpp::crypto::openssl_memory_resource()", referenced from: +[native-linux] +contains: undefined reference to `webcpp::crypto::openssl_memory_resource()' +# end::messages[] diff --git a/tools/test/fixtures/crypto_jamfiles/memory_missing_definition_make/Jamfile b/tools/test/fixtures/crypto_jamfiles/memory_missing_definition_make/Jamfile new file mode 100644 index 00000000..1695cf50 --- /dev/null +++ b/tools/test/fixtures/crypto_jamfiles/memory_missing_definition_make/Jamfile @@ -0,0 +1,15 @@ +# 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 memory option given as spec section 7.3 says, in the requirements of the +# program's project with OpenSSL beside it, and openssl_memory_resource() not +# defined. +# tag::jamfile[] +project app + : requirements WEBCPP_CRYPTO_OPENSSL_MEMORY_RESOURCE /webcpp/crypto//openssl ; + +exe app : app.cpp /webcpp/crypto//crypto ; +# end::jamfile[] diff --git a/tools/test/fixtures/crypto_jamfiles/memory_missing_definition_make/app.cpp b/tools/test/fixtures/crypto_jamfiles/memory_missing_definition_make/app.cpp new file mode 100644 index 00000000..ab78b653 --- /dev/null +++ b/tools/test/fixtures/crypto_jamfiles/memory_missing_definition_make/app.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) + +// Sets the memory option and makes AES-256-GCM on OpenSSL, without the install, and defines no +// webcpp::crypto::openssl_memory_resource(): make's guard compares OpenSSL's functions with the +// adapter's address, which odr-uses the adapter, so the link fails naming the function (spec +// section 3.7). +#include +#include +#include + +#include +#include + +int main() { + std::array const key{}; + auto const aead = webcpp::crypto::ciphers::gcm< + webcpp::crypto::ciphers::aes256>::make(key); + return aead ? 0 : 1; +} diff --git a/tools/test/fixtures/crypto_jamfiles/memory_missing_definition_make/memory_missing_definition_make.outcome b/tools/test/fixtures/crypto_jamfiles/memory_missing_definition_make/memory_missing_definition_make.outcome new file mode 100644 index 00000000..efb4e054 --- /dev/null +++ b/tools/test/fixtures/crypto_jamfiles/memory_missing_definition_make/memory_missing_definition_make.outcome @@ -0,0 +1,18 @@ +# 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 lines of each linker, measured on 2026-11-17: ld64 (Apple clang 21), GNU ld 2.44 (gcc +# 15.3, Debian 13). MSVC's link.exe is test/msvc/memory_missing_definition_*'s case. +[all] +targets: native +build: fails +# tag::messages[] +[native-darwin] +contains: Undefined symbols for architecture +contains: "webcpp::crypto::openssl_memory_resource()", referenced from: +[native-linux] +contains: undefined reference to `webcpp::crypto::openssl_memory_resource()' +# end::messages[] diff --git a/tools/test/fixtures/crypto_jamfiles/memory_option/Jamfile b/tools/test/fixtures/crypto_jamfiles/memory_option/Jamfile new file mode 100644 index 00000000..a84b041d --- /dev/null +++ b/tools/test/fixtures/crypto_jamfiles/memory_option/Jamfile @@ -0,0 +1,15 @@ +# 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 memory option as spec section 7.3 gives it: the define and OpenSSL in the +# requirements of the program's project, so that every target of the program +# gets both. +# tag::jamfile[] +project app + : requirements WEBCPP_CRYPTO_OPENSSL_MEMORY_RESOURCE /webcpp/crypto//openssl ; + +exe app : app.cpp /webcpp/crypto//crypto ; +# end::jamfile[] diff --git a/tools/test/fixtures/crypto_jamfiles/memory_option/app.cpp b/tools/test/fixtures/crypto_jamfiles/memory_option/app.cpp new file mode 100644 index 00000000..c97c87ed --- /dev/null +++ b/tools/test/fixtures/crypto_jamfiles/memory_option/app.cpp @@ -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) + +// Sends every allocation of OpenSSL's to the program's own resource, then seals and opens one +// block with AES-256-GCM on the default backend, OpenSSL natively when linked, and prints the +// backend that ran. The zero key and IV keep the output stable: a real program draws its key +// from random_device and never reuses an IV under it. +#include +#include + +#include +#include +#include +#include +#include + +// Declared by crypto under WEBCPP_CRYPTO_OPENSSL_MEMORY_RESOURCE, defined once here. Never +// destroyed: OpenSSL frees through it after main returns. +std::pmr::memory_resource& webcpp::crypto::openssl_memory_resource() noexcept { + // Never deleted, on purpose; a new that fails this early ends the program, which has no + // resource for OpenSSL. + // NOLINTNEXTLINE(cppcoreguidelines-owning-memory,bugprone-unhandled-exception-at-new) + static auto* const resource = new std::pmr::synchronized_pool_resource; + return *resource; +} + +int main() { + auto const installed = webcpp::crypto::backends::openssl::install_memory_functions(); + if (!installed) { + std::string const message = installed.error().message(); + std::printf("install: %s\n", message.c_str()); + return 1; + } + std::array const key{}; + std::array const iv{}; + std::array data{}; + auto aead = webcpp::crypto::ciphers::aes256_gcm::make(key); + if (!aead) { + std::string const message = aead.error().message(); + std::printf("make: %s\n", message.c_str()); + return 1; + } + auto const tag = aead->seal(iv, {}, data); + bool const ok = tag && aead->open(iv, {}, data, *tag) && data == std::array{}; + auto const name = webcpp::crypto::ciphers::aes256_gcm::backend_type::name; + std::printf("aes256_gcm %s backend=%.*s\n", ok ? "ok" : "FAILED", static_cast(name.size()), + name.data()); + return ok ? 0 : 1; +} diff --git a/tools/test/fixtures/crypto_jamfiles/memory_option/memory_option.outcome b/tools/test/fixtures/crypto_jamfiles/memory_option/memory_option.outcome new file mode 100644 index 00000000..3d2bc54c --- /dev/null +++ b/tools/test/fixtures/crypto_jamfiles/memory_option/memory_option.outcome @@ -0,0 +1,13 @@ +# 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) + +[all] +targets: native +build: succeeds +run: app +# tag::output[] +aes256_gcm ok backend=openssl +# end::output[] diff --git a/tools/test/fixtures/crypto_jamfiles/memory_two_files/Jamfile b/tools/test/fixtures/crypto_jamfiles/memory_two_files/Jamfile new file mode 100644 index 00000000..e6872eec --- /dev/null +++ b/tools/test/fixtures/crypto_jamfiles/memory_two_files/Jamfile @@ -0,0 +1,14 @@ +# 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) + +# Two files of one program, one compiled with the memory option and one without, +# as a project that gives the define to one target alone builds them. +# tag::jamfile[] +obj with : with.cpp /webcpp/crypto//crypto /webcpp/crypto//openssl + : WEBCPP_CRYPTO_OPENSSL_MEMORY_RESOURCE ; + +exe app : main.cpp with /webcpp/crypto//crypto /webcpp/crypto//openssl ; +# end::jamfile[] diff --git a/tools/test/fixtures/crypto_jamfiles/memory_two_files/main.cpp b/tools/test/fixtures/crypto_jamfiles/memory_two_files/main.cpp new file mode 100644 index 00000000..5596f026 --- /dev/null +++ b/tools/test/fixtures/crypto_jamfiles/memory_two_files/main.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) + +// The file of memory_two_files compiled without the memory option: it makes AES-256-GCM on +// OpenSSL, whose make differs from with.cpp's, and calls with.cpp's. +#include +#include +#include + +#include +#include + +bool make_with_the_option(); + +int main() { + std::array const key{}; + auto const aead = webcpp::crypto::ciphers::gcm< + webcpp::crypto::ciphers::aes256>::make(key); + return aead && make_with_the_option() ? 0 : 1; +} diff --git a/tools/test/fixtures/crypto_jamfiles/memory_two_files/memory_two_files.outcome b/tools/test/fixtures/crypto_jamfiles/memory_two_files/memory_two_files.outcome new file mode 100644 index 00000000..85037855 --- /dev/null +++ b/tools/test/fixtures/crypto_jamfiles/memory_two_files/memory_two_files.outcome @@ -0,0 +1,12 @@ +# 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) + +# A record: two files compiled one with and one without the memory option link here, GNU ld +# and ld64 keeping one of the two make functions. Only MSVC's linker stops them, with LNK2038 +# (backends/openssl.hpp's #pragma detect_mismatch), libs/crypto/test/msvc/memory_two_files. +[all] +targets: native +build: succeeds diff --git a/tools/test/fixtures/crypto_jamfiles/memory_two_files/with.cpp b/tools/test/fixtures/crypto_jamfiles/memory_two_files/with.cpp new file mode 100644 index 00000000..da1df856 --- /dev/null +++ b/tools/test/fixtures/crypto_jamfiles/memory_two_files/with.cpp @@ -0,0 +1,26 @@ +// 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 file of memory_two_files compiled with the memory option: it defines the program's +// resource, and makes AES-256-GCM on OpenSSL under the option. +#include +#include +#include + +#include +#include +#include + +std::pmr::memory_resource& webcpp::crypto::openssl_memory_resource() noexcept { + return *std::pmr::new_delete_resource(); +} + +bool make_with_the_option() { + std::array const key{}; + return webcpp::crypto::ciphers::gcm< + webcpp::crypto::ciphers::aes256>::make(key) + .has_value(); +} diff --git a/tools/test/fixtures/crypto_jamfiles/openssl_memory_on_emscripten/Jamfile b/tools/test/fixtures/crypto_jamfiles/openssl_memory_on_emscripten/Jamfile new file mode 100644 index 00000000..0c6ec52b --- /dev/null +++ b/tools/test/fixtures/crypto_jamfiles/openssl_memory_on_emscripten/Jamfile @@ -0,0 +1,9 @@ +# 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) + +# tag::jamfile[] +exe app : app.cpp /webcpp/crypto//crypto : WEBCPP_CRYPTO_OPENSSL_MEMORY_RESOURCE ; +# end::jamfile[] diff --git a/tools/test/fixtures/crypto_jamfiles/openssl_memory_on_emscripten/app.cpp b/tools/test/fixtures/crypto_jamfiles/openssl_memory_on_emscripten/app.cpp new file mode 100644 index 00000000..6c80fb35 --- /dev/null +++ b/tools/test/fixtures/crypto_jamfiles/openssl_memory_on_emscripten/app.cpp @@ -0,0 +1,9 @@ +// 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) + +#include + +int main() {} diff --git a/tools/test/fixtures/crypto_jamfiles/openssl_memory_on_emscripten/openssl_memory_on_emscripten.outcome b/tools/test/fixtures/crypto_jamfiles/openssl_memory_on_emscripten/openssl_memory_on_emscripten.outcome new file mode 100644 index 00000000..ac6e5fc3 --- /dev/null +++ b/tools/test/fixtures/crypto_jamfiles/openssl_memory_on_emscripten/openssl_memory_on_emscripten.outcome @@ -0,0 +1,14 @@ +# 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 compiler's line of the #error starts with the header's path on the machine, so the +# message is a contains: line. +[all] +targets: emscripten +build: fails +# tag::messages[] +contains: webcpp::crypto: WEBCPP_CRYPTO_OPENSSL_MEMORY_RESOURCE is an option of native programs, and this program is built for WebAssembly. Remove that define from the program's Jamfile. +# end::messages[]