Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
c0e05f4
crypto: the symmetric and box capability concepts of detail::backend …
Dec 5, 2026
1106124
backend concepts: a failing stand-in for every requirement, task 1.1'…
Dec 5, 2026
834ed23
backend concepts: the key, entropy and asynchronous concepts (task 1.2)
Dec 5, 2026
4f35e9d
backend concepts: every engine checked in its backend header (task 2.1)
Dec 5, 2026
f9123d3
crypto: keep clang-tidy quiet on the max macro's parentheses in count…
Dec 5, 2026
669165c
crypto: system_entropy's Doc Comment names no entropy API
Dec 5, 2026
96ae0bd
backend concepts: task 2.1's review fixes
Dec 5, 2026
c083588
backend concepts: task 1.2's review fixes
Dec 5, 2026
8823fbb
backend concepts: task 2.1's re-review minors
Dec 5, 2026
98ab701
backend concepts: x25519_mock's new knob initialised everywhere, its …
Dec 5, 2026
b84dd2f
backend concepts: task 1.2's second review fixes
Dec 5, 2026
2eba99c
backend concepts: secp256k1 behind engines (task 2.2)
Dec 5, 2026
9e896b2
backend concepts: the test-only opaque backend, generate() through a …
Dec 5, 2026
d2482e5
backend concepts: task 2.2's review minors
Dec 5, 2026
96c2f82
Merge remote-tracking branch 'origin/main' into crypto/backend-concepts
Dec 5, 2026
7a28173
backend concepts: llms.txt regenerated after task 2.3's generate() co…
Dec 5, 2026
369a04c
backend concepts: the table's capability cells checked against the en…
Dec 5, 2026
ff217b7
backend concepts: task 2.3's review folded (I1, M1-M6)
Dec 5, 2026
0aa2f31
backend concepts: task 3.1's review folded (I1, M1-M5, M7)
Dec 5, 2026
bd8243f
backend concepts: the 2.3 and 3.1 re-reviews' minors folded
Dec 5, 2026
7cec9aa
backend concepts: every public member asserts the capability it uses
Dec 5, 2026
db9fa30
backend concepts: the 3.2 review's fixes folded
Dec 5, 2026
ca53123
backend concepts: the 3.2 re-review's minors folded
Dec 5, 2026
4cf9cb1
backend concepts: the guide's chapter on backends (task 4.1)
Dec 5, 2026
87889e1
backend concepts: the 4.1 review's fixes folded
Dec 5, 2026
2fdc397
backend concepts: the 4.1 re-review's minors folded
Dec 5, 2026
bd7041a
backend concepts: the final review's code minors folded
Dec 5, 2026
b56e53a
backend concepts: the final re-review's minors folded
Dec 5, 2026
fa3ed86
backend concepts: p256_message_form converts the message's size to a …
Dec 5, 2026
4ad34c8
backend concepts: the capability catalogue's family column is a heade…
Dec 6, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 108 additions & 7 deletions doc/backends.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,114 @@ that target, and refused where a message test stops it. The data and the
tables are regenerated, and checked by the documentation's build, at every
change of a test or a pin.

A program asks the same table in {cpp}: `supports_v<Algorithm, Backend>`, in
`<webcpp/crypto/supports.hpp>`, is `true` exactly where the table says that
the backend has the algorithm on the target being compiled, and needs neither
the backend's header nor its library, so that generic code can choose a
backend at compile time. Each algorithm's type names its family and its
algorithm for it (`family_type` and `name_type`, tags of `families` and
`tags`); the test that asserts every cell is generated from the same data.
A program asks the same table in {cpp}, in `<webcpp/crypto/supports.hpp>`,
which needs neither the backend's header nor its library, so that generic code
can choose a backend at compile time. `supports_v<Algorithm, Backend>` is
`true` exactly where the table says that the backend has the algorithm's use
operations on the target being compiled: sign and verify, agree, seal and
open, or the algorithm's one operation. The operations on keys, making,
importing and exporting them, are answered by `supports_capability_v`
(<<crypto-backends-capabilities>>). Each algorithm's type
names its family and its algorithm for it (`family_type` and `name_type`, tags
of `families` and `tags`); the test that asserts every cell is generated from
the same data.

[#crypto-backends-capabilities]
=== What a backend has, operation by operation

A family's cell for a backend is a list of capabilities: making a key from
secret bytes (import), drawing one (generate), its public key (public), its
secret bytes handed out (export), the operations `supports_v` counts (use), and
the operations on public values the family's types need (auxiliary), such as
a P-256 point's check. `supports_capability_v<Capability, Backend>` answers
each, from the same table and with no header either, the capability named by
its tag of `crypto::capabilities`: a program asks
`supports_capability_v<capabilities::p256_export, Backend>` before it calls
`secret()`. A cell may be partial: `supports_v` is `true` for a backend that
signs and verifies but makes no key from secret bytes, and that backend's
`from_secret` stops the build with a message that names the capability it
lacks (<<crypto-jamfile-messages>>). Each capability, by its tag of
`crypto::capabilities`, with the backends whose cell has it:

include::generated/capabilities.adoc[]

P-256's ECDSA comes in two forms. The digest form
(`capabilities::p256_ecdsa_signing`, `p256_ecdsa_verifying`) signs and
verifies a SHA-256 digest, BearSSL's form; the message form
(`p256_ecdsa_message_signing`, `p256_ecdsa_message_verifying`) takes the
message and hashes it inside the backend, the form WebCrypto's ECDSA has; no
backend crypto ships has the message form yet. A backend has either or both.
`sign` and `verify` of a message work on either: crypto gives the message to
the backend's message form when it has one, and otherwise hashes it and uses
the digest form; the signature is made low S either way, and the verification
policy is checked before the backend sees the signature. A digest given as
`crypto::signing_schemes::prehashed` needs the digest form, so `supports_v` is
`true` when either form is present, and
`supports_capability_v<capabilities::p256_ecdsa_signing, Backend>` tells
whether a prehashed digest can be signed.

[#crypto-backends-opaque]
==== Keys that never leave their backend

A backend may keep its keys: the secret is a handle inside the backend, as a
hardware token's or the browser's non-extractable key is, and its bytes never
leave it or enter it. `exports_secret_keys_v<Backend>` says whether a secret
key's bytes leave the backend, and `imports_secret_keys_v<Backend>` whether
they enter it; both are `true` for every backend crypto ships:

include::generated/secret-key-traits.adoc[]

On a backend whose keys stay inside, a program makes its key pairs with
`generate` (or `async_generate`), which draws the key in the backend, and
keeps the key pair object itself, moved where it is needed. What would carry
the secret's bytes stops the build with a message that names the trait, before
any capability:

* `from_secret` and `async_from_secret`, the bytes entering;
* `secret()`, the bytes leaving;
* a key pair's private formats, `formats::raw`, `formats::pkcs8` and
`formats::pem(formats::pkcs8)`, both ways, the bytes leaving or entering;
* Ed25519's `to_x25519()` of a key pair, whose X25519 secret is computed from
the Ed25519 secret's bytes.

Everything else works as on any backend: `public_key()`, signing, verifying,
agreeing, the public key's formats. A program written for both kinds asks the
two traits, or `supports_capability_v` of the import and export capabilities,
and takes the other branch at compile time.

[#crypto-backends-contract]
==== What every backend gives

Every backend crypto ships keeps the same promises, which the capability
tests, the coverage tests and the lanes check, so that a program sees the same
behaviour whatever backend it names:

* Each public function sets the data-independent timing of the CPU where it
has it, around the backend's whole work (<<crypto-backends-dit>>).
* A backend allocates little, and what it allocates is named in its row of
<<crypto-backends-facts>>. BearSSL, ctaes and libsodium allocate nothing.
OpenSSL allocates a cipher's context at `from_key`, and Poly1305's MAC
context at each call, through its own allocator, or through
`openssl_memory_resource()` when a native program sets
`WEBCPP_CRYPTO_OPENSSL_MEMORY_RESOURCE`; its failure is an error value.
libsecp256k1 allocates a key pair's signing context in the
`std::pmr::memory_resource` the program gives `from_secret` or `decode`, the
default resource (the {cpp} heap unless the program replaced it) when none is
named; its failure is the resource's to report.
* Distinct objects may be used on distinct threads at once; one object is
shared only under the program's own synchronization. A backend's one-time
initialization, libsodium's or libsecp256k1's, is done by crypto, once,
safely.
* A key pair's or a cipher's secret is wiped when the object is destroyed or
moved from, and an output that holds a secret is written only on success.
* Errors are error codes: `crypto::error::backend_failure` for a failure the
backend does not explain, and `errc::not_supported` for a capability decided
at run time that the machine lacks.

How a backend is written is crypto's own: the interfaces between crypto and
its backends are internal, change with crypto's backends, and are documented
for crypto's maintainers alone. A program names a backend crypto ships; it
does not add one.

[#crypto-backends-matrix]
=== Operations, backends and targets
Expand Down
2 changes: 1 addition & 1 deletion doc/changelog.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ of every version are on webcpp's index page.
* Added crypto, C-backed primitives on webcpp::secure's guard (DIT on arm64): errors 1 to 22, `+[[nodiscard]]+` results, typed signatures, key pair concepts, factories, WEBCPP_CRYPTO_HAS_THREADS, Asio's asynchronous functions.
* Added random_device, the AES block ciphers, ciphers::gcm, per-target defaults, backends::bearssl (AES-GCM, ChaCha20-Poly1305, curves::p256 keys, low-S ECDSA, agree), backends::openssl and backends::ctaes.
* Added curves::secp256k1 (ECDSA, recoverable ECDSA, BIP 340 Schnorr, agree, tweaks, ElligatorSwift, MuSig2, silent payments), and curves::ed25519 and curves::x25519 on libsodium, Ed25519's verification canonical.
* Added public_key_encryption::hpke, RFC 9180's HPKE on X25519 or P-256, HKDF, and AES-GCM or ChaCha20-Poly1305.
* Added public_key_encryption::hpke (RFC 9180's HPKE on X25519 or P-256, HKDF, AES-GCM or ChaCha20-Poly1305), `supports_capability_v`, the secret-key traits and P-256's message-form ECDSA.
* Added backends::sodium, ChaCha20-Poly1305 AEADs, secretbox, boxes, sealed boxes, kx_keypair; raw ciphers::chacha20 (unauthenticated_encryption, also BearSSL and OpenSSL) and authenticators::poly1305 (single_use_key_authentication, also OpenSSL); the page: chapters, generated tables, Jamfiles, messages, llms.txt, measurements.
// end::unreleased[]
11 changes: 7 additions & 4 deletions doc/choosing_backend.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,13 @@ A program that wants another backend than the default names it as the type's
last template argument and links its library, as
`ciphers::chacha20_poly1305<backends::bearssl>` with `/third_party//bearssl`,
or `ciphers::gcm<ciphers::aes256<backends::bearssl>>` on MSVC. The backend
must have the family on the target being compiled, which
`supports_v<Algorithm, Backend>` tells at compile time; a backend that lacks
it, or that the program does not link, stops the build with a message that
says what to do (<<crypto-jamfile-messages>>). Every backend of a family gives
must have the family's operations on the target being compiled:
`supports_v<Algorithm, Backend>` tells at compile time whether it has the use
operations, and `supports_capability_v<Capability, Backend>` whether it has
each capability beside them, such as importing or exporting a key
(<<crypto-backends-capabilities>>); a backend that lacks one, or that the
program does not link, stops the build with a message that says what to do
(<<crypto-jamfile-messages>>). Every backend of a family gives
the same bytes: the agreement tests seal with one and open with another. What
differs is what each costs and what it promises of its timing: the
<<crypto-backends-facts,library behind each>>, its
Expand Down
11 changes: 8 additions & 3 deletions doc/formats.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,10 @@ tests compare every byte with OpenSSL's natively and on WASI
(`formats_25519_webcrypto_oracle`), 200 keys per curve each. A key pair's
`formats::raw` is its seed (Ed25519) or its secret key (X25519), `secret()` and
`from_secret` under the format's name, and an Ed25519 signature's is its 64
bytes. A public key, which names no backend, is encoded and decoded on its
family's default, libsodium on every target (`/webcpp/crypto//ed25519-default`
and `/webcpp/crypto//x25519-default` link it);
bytes. A public key, which names no backend, is encoded and decoded with none:
RFC 8410 fixes the bytes around the key, so a program that only reads and
writes public keys links no backend (`rfc8410_no_backend`), and a key pair's
PKCS#8 reads its secret through its own backend;
`codecs::decode<x25519::public_key>(formats::spki, bytes)` gives the typed key
a key pair's `agree` takes.

Expand Down Expand Up @@ -461,8 +462,12 @@ door. A program that receives version 2 converts it with OpenSSL's
`openssl pkey` first; a later version accepts it on decoding
(<<crypto-key-formats-jwk>>).

include::generated/backends-ed25519_public_formats.adoc[]

include::generated/backends-ed25519_formats.adoc[]

include::generated/backends-x25519_public_formats.adoc[]

include::generated/backends-x25519_formats.adoc[]

*Example.* RFC 8410's own Ed25519 key pair read from PEM with `codecs::decode`,
Expand Down
2 changes: 1 addition & 1 deletion doc/generated/backend-facts.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ The library behind each, and what it costs a program:
|OpenSSL 3.5.9's libcrypto, built from source and static
|Apache-2.0
|`+<openssl/*.h>+`
|`+from_key+` allocates the cipher's context through OpenSSL's own allocator, outside the program's resource, unless a native program sets `+WEBCPP_CRYPTO_OPENSSL_MEMORY_RESOURCE+`, which sends every allocation of OpenSSL's in the process to `+openssl_memory_resource()+`; a message allocates nothing after `+from_key+`
|`+from_key+` allocates the cipher's context, and each of Poly1305's calls makes and frees its MAC context, through OpenSSL's own allocator, outside the program's resource, unless a native program sets `+WEBCPP_CRYPTO_OPENSSL_MEMORY_RESOURCE+`, which sends every allocation of OpenSSL's in the process to `+openssl_memory_resource()+`; a cipher's message allocates nothing after `+from_key+`

|ctaes
|ctaes as Bitcoin Core v31.1 ships it (`+src/crypto/ctaes+`), built from source
Expand Down
2 changes: 1 addition & 1 deletion doc/generated/backends-ed25519_formats.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// Generated by doc/tables/generate.py from doc/tables/crypto.json, the test Jamfiles,
// the message tests and the Jamfile fixtures: run it again, never edit this file.

The backends of `+codecs::encode+`, `+codecs::decode+`, `+curves::ed25519::public_key::encode+`, `+curves::ed25519::public_key::decode+`, `+curves::ed25519::keypair::encode+`, `+curves::ed25519::keypair::decode+`, `+curves::ed25519::signature::encode+`, `+curves::ed25519::signature::decode+`, from the test programs that run it:
The backends of `+codecs::encode+`, `+codecs::decode+`, `+curves::ed25519::keypair::encode+`, `+curves::ed25519::keypair::decode+`, `+curves::ed25519::signature::encode+`, `+curves::ed25519::signature::decode+`, from the test programs that run it:

[cols="1,2,2,4",options="header"]
|===
Expand Down
20 changes: 20 additions & 0 deletions doc/generated/backends-ed25519_public_formats.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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)

// Generated by doc/tables/generate.py from doc/tables/crypto.json, the test Jamfiles,
// the message tests and the Jamfile fixtures: run it again, never edit this file.

The backends of `+codecs::encode+`, `+codecs::decode+`, `+curves::ed25519::public_key::encode+`, `+curves::ed25519::public_key::decode+`, from the test programs that run it:

[cols="1,2,2,4",options="header"]
|===
|Backend |Jamfile |Header |Targets

|no backend
|`+/webcpp/crypto//crypto+`
|`+<webcpp/crypto/curves/ed25519/formats.hpp>+`
|native, wasip2, wasip3, emscripten (tested by `+rfc8410_no_backend+`, `+formats_ed25519_sodium+`, `+formats_25519_openssl_oracle+`, `+formats_25519_webcrypto_oracle+`)
|===
2 changes: 1 addition & 1 deletion doc/generated/backends-x25519_formats.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// Generated by doc/tables/generate.py from doc/tables/crypto.json, the test Jamfiles,
// the message tests and the Jamfile fixtures: run it again, never edit this file.

The backends of `+codecs::encode+`, `+codecs::decode+`, `+curves::x25519::public_key::encode+`, `+curves::x25519::public_key::decode+`, `+curves::x25519::keypair::encode+`, `+curves::x25519::keypair::decode+`, from the test programs that run it:
The backends of `+codecs::encode+`, `+codecs::decode+`, `+curves::x25519::keypair::encode+`, `+curves::x25519::keypair::decode+`, from the test programs that run it:

[cols="1,2,2,4",options="header"]
|===
Expand Down
20 changes: 20 additions & 0 deletions doc/generated/backends-x25519_public_formats.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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)

// Generated by doc/tables/generate.py from doc/tables/crypto.json, the test Jamfiles,
// the message tests and the Jamfile fixtures: run it again, never edit this file.

The backends of `+codecs::encode+`, `+codecs::decode+`, `+curves::x25519::public_key::encode+`, `+curves::x25519::public_key::decode+`, from the test programs that run it:

[cols="1,2,2,4",options="header"]
|===
|Backend |Jamfile |Header |Targets

|no backend
|`+/webcpp/crypto//crypto+`
|`+<webcpp/crypto/curves/x25519/formats.hpp>+`
|native, wasip2, wasip3, emscripten (tested by `+rfc8410_no_backend+`, `+formats_x25519_sodium+`, `+formats_25519_openssl_oracle+`, `+formats_25519_webcrypto_oracle+`)
|===
Loading
Loading