Skip to content

crypto: foundation increment 1, primitives over libsodium, BearSSL, OpenSSL and ctaes - #4

Merged
pinhopro merged 110 commits into
mainfrom
crypto/foundation-1
Nov 19, 2026
Merged

pinhopro merged 110 commits into
mainfrom
crypto/foundation-1

Conversation

@pinhopro

@pinhopro pinhopro commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

What this brings

webcpp::crypto's foundation, increment 1 (version 0.2's first part): primitives over C backends.

  • Backends: libsodium (shared with webcpp::hash, link-time canaries, C suites), BearSSL, OpenSSL, ctaes; one default per family and target, with refusals that name their Jamfile line; supports_v and the capability table per target.
  • Ciphers: AES block cipher, AES-GCM, ChaCha20-Poly1305 (IETF and original), XChaCha20-Poly1305, secret boxes, hybrid::box, sealed_box; failed seals and opens zero their data.
  • Curves: secp256k1, Ed25519 (strict verification, Ed25519ph over webcpp::hash), X25519 (agreement refusing all-zero secrets, kx_keypair), RFC 8410 SPKI and PKCS#8, PEM.
  • secret<N>, equal, random_device, errors 13 to 20.
  • Constant-time lanes: dudect (under arm64's DIT), Valgrind memcheck and MemorySanitizer in a Linux container, each with planted leaks that must be caught.
  • The page: chapters per namespace and use case, generated tables (capabilities, defaults, release-built sizes and throughput, constant-time results), llms.txt.

Verification (local, on the final heads, before this PR)

  • Full lint (tools/lint/lint.sh, clang-format and clang-tidy): clean.
  • Lanes from scratch, -a, test and example: native 309, gcc-15 277, wasip2 268, wasip3 261, emscripten 202; 0 failed, 0 warnings.
  • Constant time: macOS native dudect 3/3 (DIT set; the planted leak caught at |t| 96 to 247), wasip2 2/2 (X25519 printed, not judged), Linux container (memcheck, MSan) 10/10 with planted leaks caught.
  • C suites on four targets, release size tables regenerated, throughput re-measured on release builds on an idle machine (1-minute load 2.2 to 3.4, two runs within 3%).
  • Docs, site and site-test 97/97; the crypto pages checked rendered at 1280 px and 375 px (no sideways scroll).
  • Tools tests 64/64, crypto_jamfiles_test on native, wasip2, wasip3 and emscripten; generate.py --check.
  • Local MSVC lane (real cl.exe under Wine): 14.3 and 14.5 each 538 passed, 0 new failures, 19 known Wine artifacts; crypto's 305 tests and test/msvc 41/41 pass. heavy_test is listed as a provisional Wine artifact (Rosetta aborts cl on it); no Windows build has compiled it yet, so the Windows CI decides.
  • Not run locally: the CI's x86-64 container lanes and Apple Clang.

For the owner

  • X25519 on wasip2 is an open finding. dudect times libsodium's X25519 agreement under wasmtime on Apple silicon at |t| 107 to 260 (threshold 10); natively it passes. The likely cause is Apple's DIT bit, which wasmtime does not set: with DIT set in wasmtime's main thread (an experiment, not the lane) the same case reads |t| 1.5 to 2.1. The wasip2 case prints its result without judging, and the constant-time page, crypto.json and the X25519 chapter state the finding.
  • webcpp does not set DIT for user programs. The dudect harness sets DIT on macOS and Linux arm64 and fails where the CPU has DIT and it is not set. Without DIT, X25519 with an all-zero secret runs about 1 ns faster in 15 us (|t| 6.7 to 14.5). The docs tell users on Apple silicon to set DIT around secret operations. Whether the library should set it is an owner decision for later.
  • AES-GCM's default on MSVC is OpenSSL built no-asm, which is not constant time; stated in the docs.
  • gcm's seal does not zero its output on backend_failure (OpenSSL path), unlike every other family's seal and open; follow-up.
  • The trailing-comma workaround: clang-tidy's readability-trailing-comma and clang-format disagree on one-row vectors; the ChaCha fixtures keep their layout under an exemption (b7cc466).
  • C4459 on MSVC: a program with a namespace-scope name such as seed, message or peer_public_key, built at /W4 /WX, can hit C4459 through a header parameter; the headers were left (no rename makes the class go away); the library's own tests are clean.
  • Parked Minors: x25519/formats.hpp:188's refusal is not reached by a test (its text is pinned through x25519.hpp:165); generate.py accepts a lane_note that is neither a string nor an object.
  • After merge, every CI lane's OpenSSL and libdatachannel configuration cache misses once (build.py changed), so the first run is slower.

Related PRs

🤖 Generated with Claude Code

R added 30 commits November 18, 2026 13:12
…nt's changelog form asks: the lines of related changes are one
…very public header, backends/sodium.hpp included, compiles in the lint's aggregate
…c asks, and clang-tidy's misc-explicit-constructor is silenced for it
doc/tables/crypto.json gains its families (gcm, aes_block, secp256k1):
per family its algorithms, per backend the targets on which it has
them, each claimed only where a test of the family's operations runs,
constant time where it is not the backend's design, and the default per
target with its reason. generate.py checks them and writes from them
include/webcpp/crypto/detail/capabilities.hpp (the family and algorithm
tags, detail::supported per cell and target, each family's refusal
macro naming the backends that have it) and test/capabilities_test.cpp,
which asserts every cell and makes every present one on each target.
supports_v reads the table; ciphers::gcm, the AES block cipher and
secp256k1's key pair name their family_type and name_type.
…e, and each refusal holds on its target

crypto.json's old defaults list moves into families[].default, which
states the defaults the code has now, each row claimed only where a
test that fixes it runs; the defaults page, the backends pages and
llms.txt read it. The refusal macros are written per target, so that
a backend without the family on that target is refused rather than
told to include a header. --check's printing is tested through
checked(), a type keeps std:: as it is, the per-family constant time
no longer restates the backend's, and the changelog's merged lines
keep their facts.
… D32)

Each family's default backend is chosen by WEBCPP_TARGET_* alone, whatever the
program links, for the reason doc/tables/crypto.json gives (the owner's answers
Q1 to Q4). generate.py writes detail/defaults.hpp from families[].default:
default_backend_of<Family> with one specialisation per family that has a
default, default_backend_t, and default_backend, AES-GCM's. AES-GCM's default
is OpenSSL natively and BearSSL on WebAssembly; secp256k1's is libsecp256k1;
the AES block cipher has none.

config.hpp no longer chooses a default: WEBCPP_CRYPTO_DEFAULT_BACKEND_BEARSSL
and _OPENSSL stop the build with the type to write (Q3). aes256_gcm.hpp includes
its default's header only when it is linked; the family's primary engine,
defined in gcm.hpp and aes.hpp with an empty primary state, is the generated
refusal, which for the default not linked names the Jamfile line that links it
and the type that names another backend. The refusal of an unknown backend now
fires for an unknown type alone, and a one-target block opens with #ifdef.

The default tests assert the fixed defaults with every backend linked, none,
each alone and the plan's retired defines; default_selected and
default_openssl_wasm become message tests of Q3's text, default_bearssl natively
becomes gcm_default_openssl_not_linked, each with its MSVC case; two_defaults,
no_backend_native, MSVC no_backend and default_two_files (its detect_mismatch)
are gone. The examples that link BearSSL alone name it in the type.
… compilers, each compiler of a target once

The check of two defaults on one target keyed on the free-text where (and
before it linked), so one compiler written in two ways passed, and nothing
checked that the rows of a target covered each compiler. A row now lists
compilers by their names in crypto.json's compilers; check_families faults an
unknown name, a compiler of a target in two rows, and one in none. The pages
are unchanged.
…ion, so em++ runs its whole post-link optimisation

secret, canary_read, canary_read_absent and wipe_objects_* were built at
<optimization>speed in the debug variant, whose -g made em++ warn
-Wlimited-postlink-optimizations ten times a lane: DWARF keeps binaryen to its
limited passes. They read what an optimiser leaves, so on Emscripten they now
drop the debug information (<toolset>emscripten:<debug-symbols>off) and
wasm-opt runs in full, the stricter test; elsewhere nothing changes.
…he default's refusal says what a build without b2 links

Review of Task 5', fix round 1. The use cases and five examples named BearSSL in
the type, which natively steers a server on ARM to BearSSL's portable AES, the
case OpenSSL's native default exists to avoid. They name aes256_gcm again and
link its default on each target, OpenSSL natively and BearSSL on WebAssembly,
through a rule of example/Jamfile that the guide shows; the guide states the cost
of naming BearSSL natively where it shows that form.

The never-called member functions of the primary engines return
backend_failure, so a path the refusal missed fails closed. The needs message
ends with what a build without b2 defines and links, from each backend's
without_b2 in crypto.json. The other backend it names is the first of
crypto.json's backends that has the family on the target when the program is
built, never one decided at run time, in the family's shown name, which
check_families requires where there is another backend. The fixes of
no_backend_wasm and openssl_only_on_wasi match their message.
…target, generated with the defaults

Review of Task 5', fix round 2. The guide taught a rule of the examples'
Jamfile that called the internal webcpp-target module and could not be copied.
generate.py now writes a block of build.jam, from families[].default as
detail/defaults.hpp is, that gives each family with a default the alias
/webcpp/crypto//<family>-default: its usage requirements link the default's
library on each target, OpenSSL natively and BearSSL on WebAssembly for
gcm-default. --check keeps the two from drifting. The examples link
gcm-default; jamfile.adoc shows the Jamfile of the new fixture gcm_default,
which builds and runs on all four targets, and the use cases name the line.
… clang-tidy's modernize-use-nodiscard asks
… 3, agree's own all-zero check), and its results [[nodiscard]]

The conversion's main-subgroup check is now the only check that refuses two of the keys the test passes; a stand-in engine that does not refuse an all-zero shared secret shows that agree refuses it itself. The point (0, -1) is of order 2. The use case names curves::ed25519::keypair::x25519_secret in full.
…hout threads

config.hpp defines WEBCPP_CRYPTO_HAS_THREADS by design D23's rule (natively BOOST_HAS_THREADS, on Emscripten -pthread, never on WASI, never with WEBCPP_CRYPTO_NO_THREADS). detail/heavy.hpp holds the refusal every heavy operation's async_ variant starts with, and detail::run_heavy, which posts the synchronous call to the program's executor and completes on the handler's associated executor. Increment 1 declares no heavy operation (design D32).
…25519 joins the curves table, its results are nodiscard, its wipes are tested, and llms.txt reads ph's sign and verify in their class
…'s row, the generator refuses keys it does not know, and the changelog's lines fit the lint
… alone, and libsodium's engine passes an empty span's data as it is

libsodium's detached decryptions check the tag alone when the output is null,
and its ChaCha20 streams return at once on no byte, so the engine's static
placeholder byte goes. The AEAD tests include what they use, and the
ChaCha20-Poly1305 default's message gains its MSVC case.
…against RFC 8439, Wycheproof and libsodium

BearSSL runs SSE2's ChaCha20 and ctmulq's Poly1305 where it has them and
its constant-time portable code otherwise, the seam pinning the latter, and
its tag is compared by equal; OpenSSL's EVP ChaCha20-Poly1305 runs GCM's
sequence of AEAD controls on its own context. Both pass aead_checks.hpp,
Wycheproof's 325 cases and agree with libsodium pairwise; ctaes's refusal
and the default's now name the three backends. The additional data may
touch the data but not overlap it, since BearSSL reads it after it
encrypts.
…ha20-Poly1305 AEADs merged into libsodium's line
…sio for its async_ variants, and its test says why it runs the io_context
R added 27 commits November 18, 2026 19:37
…uration it peeks is gone or respelt

The lane reads third_party/Jamfile's private .sodium-common, .sodium-posix and .sodium-linux
and removes <define>HAVE_INLINE_ASM=1 by its spelling: an empty read, or one without that
define, now stops the build at the Jamfile with what to update, rather than build libsodium
with no configuration or with its inline assembly back in.
…he data

generate.py takes the namespaces and the backends from crypto.json, writes each comparison table's
speed from doc/performance.json and its measured constant time from the family's entries, refuses a
fact about the world without a source naming its row, refuses a use case a program can follow
without an example, lists every backend's facts and design, libsodium's with its sources, and its
own suite, gives every API page its default per target, and gives each llms.txt entry its default
and its use cases, and llms.txt the measured constant time. The ciphers table gains the ChaCha20
AEADs and the secret boxes; the rules, ChaCha's nonce and the shared secret that is not a key.

The prose: the ciphers chapter's one rule of choice, ChaCha20-Poly1305's sourced constant time,
limits, example and pitfalls; the chapter Choosing a backend, with the defaults, the stated heavy
operations and the threads of a native build; X25519's open finding on wasip2 and webcpp::hash's
HKDF; the hybrid chapter's choice and example; the file-sharing and peer-identity teaching
sections, after age and libp2p; the attribution of libsodium's documentation, whose licence ships
as LICENSE-LIBSODIUM-DOC.txt. Three examples: xchacha_seal, file_share and box_messages.
…on lists alone, so that one respelt stops the build too
…, backend and target, and the ciphers table fits the page

crypto.json records, per family and backend, the targets and checkers of the constant-time lane
(and a note where it runs part of a path), and the root's own paths; the comparison tables, the
constant-time page's per-target columns and llms.txt are written from that one source, "not
measured yet" where the lane runs nothing, and a lane entry or a finding the family does not
claim is a fault. llms.txt gives a family without a default "none", and the AES block cipher's
note on `aes256<>`. The ciphers table is cut into parts (AES, ChaCha20-Poly1305, secret boxes).
The speed cells: each operation apart, three significant figures, the load said, an unknown
backend a fault, and doc/performance.json refused unless built at variant=release. The nonce
misuse and design cells are cited too; the file-sharing use case is the root's.
…the teaching sections on the specifications alone, file_share's refusals named

A message cut into chunks is safe only with each chunk's number and a last-chunk flag under its
tag, citing STREAM and libsodium's secretstream, neither of which this version offers;
chacha.adoc and everyday.adoc said less, and aead.adoc's records now cite STREAM. The age and
libp2p sections name their specifications only, libp2p's identity as one of its key types.
file_share includes <span> and says which refusal is which.
…with its binding, and the constant-time note names X25519

The rulebook's library file now covers libsodium, the generated files and the open X25519 finding. everyday.adoc no longer says never to cut a file into boxes and then how to; the wasip2 cell's note names the family it covers, from the data and the generator, its test first.
…ndition, so that declared-lanes prints no b2 warning that tools/ci/matrix.py cannot read
…t checker alone, and a finding's note names its family only where the checker lists several
…en the backend fails, as their open does, shown on a stand-in engine that writes before it fails
…x keys wiped on destruction and on move, as the other backends' are
…oly1305 too: make, seal, good and failed opens and the destructor in the resource, on main and a second thread (spec 8.5)
…d PEM's default not linked, a backend's header not included, no backend of that name, Ed25519's one policy and secret<0>, prefixed as the others; MSVC cases for kx, box and these
…th a trailing comma, a row per designated member (readability-trailing-comma)
…message to a named string before printing it (the lint's fluent chains)
…information on Emscripten, whose DWARF made em++ warn that it limited its post-link optimisation
…ane sizes' release builds on the four targets
…| split X25519's wasip2 finding over the curves table's cells
…and it is not set; X25519's native finding without DIT documented
…f DIT on Apple silicon, the tables give the native finding to dudect alone, a failed read of the CPU's features fails, dudect_planted
…the constant-time Jamfile, X25519's note and page; dudect_planted builds and runs only the planted X25519 case
… after a bare <windows.h>, the tests' reserved.hpp reaches VirtualAlloc through detail/windows.hpp, and four tests rename the anonymous-namespace names that hid header parameters (C4459)
… nacl_message, which hid sealed_box's parameter (C4459 on MSVC)
…ute load 2.7 before, 3.4 after), confirmed by a second run within 3%, and the comparison tables regenerated from it
@pinhopro
pinhopro merged commit dae9e84 into main Nov 19, 2026
1 of 3 checks passed
@pinhopro
pinhopro deleted the crypto/foundation-1 branch November 19, 2026 20:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant