Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
eecca14
crypto: backend-concepts as built from the primitives head: the devic…
Dec 5, 2026
4d5fc07
crypto: backend-concepts STATUS: task 1.1's commits and verification
Dec 5, 2026
03caf01
crypto: backend-concepts: task 1.1's review rulings (no KeySize, box …
Dec 5, 2026
6b58971
crypto: backend-concepts: task 1.2's rulings and verification; task 1…
Dec 5, 2026
be48603
crypto/backend-concepts: task 2.1 and its review fixes as built (ruli…
Dec 5, 2026
0ddd845
crypto/backend-concepts: task 1.2's review fixes (rulings I1-I3, M1-M…
Dec 5, 2026
fd60bd4
crypto/backend-concepts: task 1.2's second review fixes (N1-N5) and t…
Dec 5, 2026
a3646e1
crypto/backend-concepts: task 2.2 as built (secp256k1 behind engines:…
Dec 5, 2026
91cfe2a
Merge remote-tracking branch 'origin/main' into crypto/backend-concepts
Dec 5, 2026
fc1aa03
crypto: backend-concepts task 2.3 as built, task 2.2's review minors,…
Dec 5, 2026
ba1b71c
crypto: backend-concepts task 3.1 built: capability cells, the marker…
Dec 5, 2026
5fbbe61
crypto: backend-concepts task 2.3's review I2: task 3.2 names the opa…
Dec 5, 2026
d0ad2fb
crypto: backend-concepts task 3.1's review folded: program-facing cap…
Dec 5, 2026
306fab0
crypto backend-concepts: task 3.2 done, its rulings in STATUS
Dec 5, 2026
bda8f04
crypto backend-concepts: the 3.2 review's fixes, their rulings in STATUS
Dec 5, 2026
bcce9fe
crypto backend-concepts: the 3.2 re-review's async message test in ST…
Dec 5, 2026
a9eb1ba
crypto backend-concepts: task 4.1 done, its rulings in STATUS
Dec 5, 2026
f94d46d
crypto backend-concepts: the 4.1 review's fixes, their rulings in STATUS
Dec 5, 2026
fb942ad
crypto backend-concepts: the 4.1 re-review's minors, 4.1 reviewed clean
Dec 5, 2026
071bd7f
crypto backend-concepts: the spec describes what was built, the final…
Dec 5, 2026
1b1cccf
crypto backend-concepts: the final re-review's spec wording and rulings
Dec 5, 2026
d17c495
crypto backend-concepts: task 4.2's final lanes in STATUS
Dec 5, 2026
99674f2
crypto backend-concepts: task 4.2 done, the pull requests' CI green
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
155 changes: 145 additions & 10 deletions libraries/crypto/openspec/changes/backend-concepts/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,50 @@ the engines it describes (typed keys and signatures, X25519's key pair, the boxe
link. `capabilities_test` checks that a present cell has a specialization; nothing checks the
specialization's operations.

## As built from (2026-12-05)

The implementation starts from crypto `c638dbf` (the primitives change, branch `crypto/primitives`,
not yet merged) and the superproject `34ea790`; formats, codecs, HPKE, api-gaps and async part A are
merged. What that changes in the context above, ruled while building (`reference/STATUS.md`):

- **Two engines more**: the primitives change added `stream_engine<families::chacha20, B>` (`init`,
`apply(state, nonce, initial_counter, data)`, `move`, `reset`) on libsodium, BearSSL and OpenSSL,
and `authenticator_engine<families::poly1305, B>` (one static `authenticate(key, message, tag)`,
no state) on libsodium and OpenSSL. D4 gains `stream_cipher<E, Family>` and
`authenticator<E, Family>`.
- **GCM is on BearSSL and OpenSSL only**, and the AES block cipher on ctaes and OpenSSL (BearSSL's
`block_engine` is a refusal of its own); the incremental open is today's `start`, `run` and
`check`, so `gcm_incremental_open` is not a concept of its own.
- **base64 left crypto**: the codecs library (webcpp::codecs) encodes and decodes base64, and the
PEM engine is gone; the encoding area of D4 has no concept, and R4's `base64_engine` is not built.
- **The box families are tags without sizes**: both crypto_box families have a 24-byte nonce and a
16-byte tag, which the `box` concept names as constants; `box` therefore takes no family (D4's
`box<E, Family>` is `box<E>`).
- **The key size is the engine class's, not the concept's**: `block_engine<B, KeySize>` and
`gcm_engine<B, KeySize>` are one class per key size and `init` takes a dynamic span the public
type has sized, so D4's `block_cipher<E, KeySize>` and `gcm<E, KeySize>` are `block_cipher<E>`
and `gcm<E>`, asserted once per key size.
- **kx and the sealed box keep the engines' names**: D4's `derive_from_seed`,
`client_session_keys`, `server_session_keys` and the three-argument sealed-box `open` are the
engines' `seed_keypair`, `client`, `server` and `open(recipient_public, recipient_secret, sealed,
out)`.
- **The key engines take one set of names in task 2.1**: `import_secret`, `generate`,
`public_key` (fallible), `export_secret` (fallible) and `move` on every curve; X25519's engine,
stateless today, becomes state-based so that an opaque X25519 key has a state. P-256's
`p256_point` has no `compress` (the public type's, on bytes it has).
- **The formats engines** are `ed25519_formats_engine` and `x25519_formats_engine` on libsodium;
P-256's and secp256k1's formats call the curve's engine or library directly. Task 2.1 dissolves
the two into `detail::rfc8410`, a fixed prefix and the key, with no backend: PKCS#8 reads the
secret through the key engine's `export_secret`.
- **The key concepts take their review's shape (task 1.2's review)**: P-256 signs and verifies
in a digest form or a message form (WebCrypto's); the asynchronous concepts mirror
`async.hpp`'s calls exactly (const executor, const lvalues, the token forwarded) and are
`async_import_secret` and `async_import_key`, with no `async_export`; `ed25519ph` takes the key
engine as a second parameter.
- **The sized families move to `detail/families.hpp`**, so that a backend header, which includes
no public header, can assert D5's concepts after each specialization.
- **HPKE** composes the curves' and webcpp::hash's public types and has no engine of its own.

## Intent (owner, 2026-11-22)

Put the library's capabilities into concepts; a backend implements only the concepts it has. The
Expand Down Expand Up @@ -114,6 +158,12 @@ it, and is named after the capability, never after a backend: `backend::aead<Eng
`backend::p256_ecdsa_signing<Engine>`. Each family header includes its area's header, and no
concept header includes a backend.

> **As built (`reference/STATUS.md`)**: no `encoding.hpp` (base64 moved to webcpp::codecs and the
> PEM engine is gone, ruling "No `base64` concept"); `common.hpp` holds the shared contract and the
> requirements every capability reuses, and `keys.hpp` the four key concepts that take no curve
> (`key_import`, `key_generate`, `key_public`, `key_export`, ruling 1.2-M3), which each curve's
> header names (`p256_import = key_import<Engine>`).

### D2. The contract every capability shares

What a concept checks (C++ requirements):
Expand Down Expand Up @@ -151,6 +201,13 @@ and in `capabilities_test`, the coverage tests and the lanes):

### D3. The opaque key

> **Amended on 2026-11-30 by device-backends**: "a backend whose keys never leave it implements
> neither `*_import` nor `*_export`" is now said by two public traits generated from the table:
> `crypto::exports_secret_keys_v` (no `*_export`, browser-backend's D16) and
> `crypto::imports_secret_keys_v` (no `*_import`), whose refusals speak before the capability
> assertions of D9; a backend may also keep `*_import` and refuse one key at run time (a PKCS#11 token,
> `not_supported`). The text below is as approved.

A key capability is split by what it does to the key:

| Capability | Meaning | Needs the secret in memory |
Expand Down Expand Up @@ -180,29 +237,51 @@ shape is proven before part B exists; it has a synchronous form and an asynchron
It is a test tool, not a shipped backend: outside the table, `supports_v` and the two-way check
(R6). Part B's browser backend is the first real consumer of both shapes.

> **As built (task 2.3, `reference/STATUS.md`)**: `test/backends/opaque.hpp`, backends `opaque`
> and `opaque_async` of `webcpp::test::opaque_backend`, whose table holds BearSSL's P-256 and
> libsodium's X25519 states; `generate()` on Ed25519, X25519 and P-256 calls the engine's
> `generate` when it has one. The asynchronous engine reaches a key pair's work through
> `detail::async_access` (`generated`, `agreed`, `signed_digest`).

### D4. The catalogue

| Area | Concept (`detail::backend::`) | Operations | Engines checked |
|---|---|---|---|
| Symmetric | `block_cipher<E, KeySize>` | `init`, `encrypt`, `decrypt`, `move`, `reset` | `block_engine` on OpenSSL, ctaes |
| | `gcm<E, KeySize>` | `init`, `start`, `run`, `tag`, `check`, `move`, `abandon`, `reset` | `gcm_engine` on BearSSL, OpenSSL, ctaes |
| | `gcm<E, KeySize>` | `init`, `start`, `run`, `tag`, `check`, `move`, `abandon`, `reset` | `gcm_engine` on BearSSL, OpenSSL |
| | `gcm_incremental_open<E, KeySize>` | the incremental open's `start`, `authenticate`, `decrypt`, `verify`, `finish` | as GCM where offered |
| | `aead<E, Family>` | `init`, `seal(iv, ad, data, tag)`, `open(iv, ad, data, tag)`, `move`, `reset` | `aead_engine` for each family on BearSSL, OpenSSL, libsodium |
| | `authenticated_box<E, Family>` | `init`, `seal(iv, data, tag)`, `open(iv, data, tag)`, `move`, `reset` | secretbox on libsodium |
| | `stream_cipher<E, Family>` | `init`, `apply(nonce, initial_counter, data)`, `move`, `reset`; the nonce the family's `nonce_size`, the counter its `counter_type` | `stream_engine<families::chacha20, B>` on libsodium, BearSSL, OpenSSL |
| | `authenticator<E, Family>` | one static `authenticate(key, message, tag)`, no state; `verify` is crypto's own constant-time comparison | `authenticator_engine<families::poly1305, B>` on libsodium, OpenSSL |
| Public-key encryption | `box<E, Family>` | `init(own, peer)`, `seal`, `open`, `move`, `reset` | `box_engine` on libsodium |
| | `sealed_box<E>` | `seal(recipient, data, out)`, `open(recipient, sealed, out)` | `sealed_box_engine` on libsodium |
| Key exchange | `kx<E>` | `derive_from_seed`, `client_session_keys`, `server_session_keys` over X25519 states | `kx_engine` on libsodium |
| Ed25519 | `ed25519_import`, `ed25519_generate`, `ed25519_public`, `ed25519_signing`, `ed25519_verifying`, `ed25519_export`, `ed25519_to_x25519` | per D3 | `ed25519_engine` on libsodium |
| | `ed25519ph<E>` | `begin`, `update`, `sign`, `verify` | `ed25519ph_engine` on libsodium |
| X25519 | `x25519_import`, `x25519_generate`, `x25519_public`, `x25519_agreement`, `x25519_export` | per D3; `agree` refuses an all-zero result | `x25519_engine` on libsodium |
| P-256 | `p256_import`, `p256_generate`, `p256_public`, `p256_point` (validate, compress, decompress), `p256_ecdsa_signing`, `p256_ecdsa_verifying`, `p256_ecdh`, `p256_export`, `ecdsa_der` | per D3 | `p_curve_engine<families::p256, B>` on BearSSL (and OpenSSL where the table has it) |
| P-256 | `p256_import`, `p256_generate`, `p256_public`, `p256_point` (validate, decompress), `p256_ecdsa_signing` and `p256_ecdsa_verifying` (over a digest), `p256_ecdsa_message_signing` and `p256_ecdsa_message_verifying` (over a message, hashed inside), `p256_ecdh`, `p256_export`, `ecdsa_der` | per D3 | `p_curve_engine<families::p256, B>` on BearSSL (and OpenSSL where the table has it) |
| secp256k1 | `secp256k1_import`, `secp256k1_generate`, `secp256k1_public`, `secp256k1_export`, `secp256k1_ecdsa_signing`, `secp256k1_ecdsa_verifying`, `secp256k1_schnorr_signing`, `secp256k1_schnorr_verifying`, `secp256k1_recoverable` (sign and recover), `secp256k1_ecdh`, `secp256k1_tweak`, `secp256k1_ellswift`, `secp256k1_musig`, `secp256k1_silent_payments` | per D3 for the keys; one per protocol for the rest | `secp256k1_engine<Capability, backends::secp256k1>` (D8) |
| Encoding | `base64<E>` | constant-time `encode` and `decode`, standard with padding and URL-safe without, into fixed spans | `pem_engine` on libsodium (renamed `base64_engine`) |
| Entropy | `entropy_source<E>` | `fill(span)` returning `result<void>` (`entropy_unavailable`) | the platform sources of `detail/entropy.hpp` |
| Asynchronous | `async_import<E, A>` (behind `async_from_secret` and `async_from_key`), `async_generate<E, K>`, `async_seal<E, A>`, `async_open<E, A>`, `async_sign<E, K, S>`, `async_verify<E, S, P>`, `async_agree<E, K>`, `async_export<E, K>` | the shapes `async.hpp` documents: executor first, token last, completion signatures as the public functions' | `async_engine<backends::browser>` (part B), the stand-in |
| Asynchronous | `async_import_secret<E, K>` (behind `async_from_secret`), `async_import_key<E, C>` (behind `async_from_key`), `async_generate<E, K>`, `async_seal<E, A>`, `async_open<E, A>`, `async_sign<E, K, S>`, `async_verify<E, S, P>`, `async_agree<E, K>`, each over the executor and token types | the shapes `async.hpp` documents: executor first, token last, completion signatures as the public functions' | `async_engine<backends::browser>` (part B), the stand-in |

> **As built (task 1.2 and its review, `reference/STATUS.md`)**; the rows above are corrected in
> place, and this note keeps what the approved text said: `ed25519ph<E, KeyEngine>` takes
> the key engine whose state `sign` reads; P-256's signing and verifying come in two forms, the
> digest one above (`sign(state, digest, signature)`, BearSSL's) and a message one,
> `p256_ecdsa_message_signing` (`sign_message(state, message, signature)`) and
> `p256_ecdsa_message_verifying` (`verify_message(point, message, signature)`), for a backend that
> hashes inside as WebCrypto does (browser-backend's D3 signs the message); a backend has either or
> both. The asynchronous row is `async_import_secret<E, K>` (behind `async_from_secret`) and
> `async_import_key<E, C>` (behind `async_from_key`), `async_generate`, `async_seal`, `async_open`,
> `async_sign`, `async_verify`, `async_agree`, each over the executor and token types, and no
> `async_export` (browser-backend's owner decision 6: a backend that completes by itself keeps its
> keys). There is no `base64` (base64 left crypto), and `p256_point` has no `compress`. The approved
> rows read `p256_point` (validate, compress, decompress), an Encoding row `base64<E>` on the PEM
> engine, and an Asynchronous row of `async_import<E, A>` behind both imports and `async_export<E, K>`.

The formats change's per-curve formats engines dissolve into these: a format codec needs a key's
`*_public`, `*_import`, `*_export`, `p256_point`, `ecdsa_der` and `base64`, and holds no engine of
`*_public`, `*_import`, `*_export`, `p256_point` and `ecdsa_der` (base64 is webcpp::codecs'), and holds no engine of
its own.

### D5. Every engine is checked
Expand All @@ -222,13 +301,36 @@ list nor in the table (R6). A missing operation is a diagnostic naming the conce
operation (each concept is a conjunction of named sub-requirements, so compilers print which one
failed).

> **As built (task 3.1, `reference/STATUS.md`)**: the marker is a variable template beside each
> specialisation, `detail::specialized_engine_v<Engine>`, not a member, since reading a member
> would instantiate a refusing primary; a family row lists its capabilities (`"capabilities"`,
> each with its kind, concept and engines) and a backend's cell those its engines have;
> `capabilities_test` folds over `detail::shipped_backends` and checks each capability's engines,
> counted only where marked, against its cell, and the secret-key traits against the export and
> import engines.

### D6. Asynchronous and opaque together

> **Amended on 2026-11-30 by browser-backend** (owner decision of 2026-11-23): a backend that
> completes by itself may also implement the synchronous concepts by waiting for its own
> completion; `backends::browser` does, its synchronous engine functions suspending the
> WebAssembly stack (and refusing with `error::cannot_suspend` where it cannot suspend). The
> public synchronous forms refuse only where the engine lacks the synchronous capability. Whether a
> backend lets secret key bytes leave it is the trait `detail::exports_secret_keys_v<Backend>`
> (browser-backend's D16, owner decision of 2026-11-30), checked against the `*_export` concepts.
> The paragraph below is the text as approved, corrected in place by task 1.2's review (the note
> after it keeps the approved sentence).

A backend that completes by itself (`asynchronous_backend_v`) implements the `async_*` concepts and
none of the synchronous ones; the public synchronous forms keep refusing with the name of their
`async_` form (crypto/sign-unified's D9). Export of an extractable opaque key is `async_export`,
the operation the formats change left to part B; a non-extractable key has none, and the private
formats are refused by type.
`async_` form (crypto/sign-unified's D9). Such a backend keeps every key non-extractable: there is
no asynchronous export, and the private formats are refused by type.

> **As built (task 1.2's review)**: there is no `async_export` (the paragraph above is corrected in
> place; the approved text said: "Export of an extractable opaque key is `async_export`, the
> operation the formats change left to part B; a non-extractable key has none"). Browser-backend's owner decision 6
> keeps every key of a backend that completes by itself non-extractable, so no caller exists; the
> concept comes back with the first backend that exports asynchronously.

### D7. One source of truth, and the table (a ruling, R1)

Expand Down Expand Up @@ -260,6 +362,11 @@ when its engine satisfies the concept. The table stays, as the header-free decla
program asks `supports_capability_v<capabilities::p256_export, B>` before it calls `secret()`.
Both stay header-free and both are checked against the concepts.

> **As built (task 3.1)**: the capability tags are `crypto::capabilities::<name>`, and a capability
> is of a kind, `use` among them, which `supports_v` counts; P-256's digest and message forms each
> count for signing and verifying. `exports_secret_keys_v` and `imports_secret_keys_v` are public
> in `<webcpp/crypto/supports.hpp>` beside `supports_capability_v`, generated into `detail`.

### D8. secp256k1 behind an engine

The curve's headers call libsecp256k1 directly today. Their calls move, unchanged, behind
Expand All @@ -269,6 +376,14 @@ MuSig2, silent payments), in `backends/secp256k1.hpp`, so that the curve uses th
curve uses and is split as they are (owner decision 3). The static context, the self test, the canary and the no-allocation verification stay as they
are; the memory option's workspace for MuSig2 and silent payments is part of those concepts.

> **As built (task 2.2, `reference/STATUS.md`)**: one specialization per tag of
> `detail::backend::secp256k1_capabilities`, the key pair's four capabilities one `key` engine, with
> two engines more than the list above, `point` (parsing, serializing and comparing points and
> x-only keys) and ECDSA's signature forms in the `ecdsa` engine (`secp256k1_ecdsa_forms`). The
> curve's headers reach them through `secp256k1_engine_of<Capability>`, fixed to
> `backends::secp256k1`. The key pair keeps allocating the context's storage from its memory
> resource; the engine creates the context in it (`context_size`, `start`).

### D9. The public API's constraints

Each public type checks, in the member that needs it, the capability it uses: `basic_keypair<B>::from_secret`
Expand All @@ -284,6 +399,16 @@ uniform-types concepts (`crypto::keypair`, `crypto::aead`, ...) are the public a
the backend concepts are what their members require of a backend, and the names never collide
(`crypto::aead` versus `detail::backend::aead`).

> **As built (task 3.2, `reference/STATUS.md`)**: a message says "this backend", not the
> backend's name (a C++20 `static_assert` message is a literal; the instantiation notes name the
> backend), and names the capability by its public tag: "webcpp::crypto: this backend has P-256
> but not crypto::capabilities::p256_ecdsa_signing: ...". Before any capability, the secret-key
> traits speak, each by its own message and alone (D3's and D6's amendments): `from_secret`,
> `async_from_secret`, `secret()`, the private formats and `to_x25519()` on a backend that keeps
> its keys name `crypto::imports_secret_keys_v` or `exports_secret_keys_v`, whatever its engine
> has. `can_sign` and `can_verify` agree with `sign` and `verify` on P-256's two forms (final
> review M4).

### D10. No public backend API (owner decision 5)

The concepts, the engines and the states stay in `detail`; no `crypto::backend` namespace, no
Expand All @@ -307,8 +432,8 @@ The public factories are named by their input (uniform-types' owner decision 9:
- **R2 (owner decision 3): secp256k1 moves behind engines, split into capabilities** (D8).
- **R3 (owner decision 4): `generate()` uses the engine's `*_generate` when the backend has one**,
mandatory for opaque keys; else `random_device` and `*_import`.
- **R4: the formats engines dissolve** (D4) into key and encoding capabilities; the PEM family's
engine becomes `base64_engine` under the family `families::pem`, which keeps its table row.
- **R4: the formats engines dissolve** (D4) into key capabilities; the PEM family's engine is gone
with base64, which left crypto for webcpp::codecs, so no `base64_engine` is built.
- **R5: the concepts' headers live in `detail/backend/`**, not `detail/capabilities/`, which would
sit beside the generated `detail/capabilities.hpp` and read as its part.
- **R6 (coordinator): the test-only opaque backend lives outside the table and the two-way check**;
Expand Down Expand Up @@ -377,6 +502,16 @@ struct opaque_p256_engine {
}; // no import, no export: from_secret(bytes) and secret() refuse, naming p256_import and p256_export
```

> **As built (`reference/STATUS.md`)**: the block above is the approved sketch. `supports_v`
> counts the use operations alone (R8, ruling 3.1 "a capability's kind decides what `supports_v`
> counts"): BearSSL's P-256 row is its signing, verifying and agreement, never `p256_import`, and
> the generated `capabilities_test` checks each capability's cell against its concept both ways.
> An opaque engine's public key is the fallible `public_key(state, point)` (the concept
> `key_public`), not `void public_point`; and `from_secret(bytes)` and
> `secret()` on it refuse by the secret-key traits, `crypto::imports_secret_keys_v` and
> `exports_secret_keys_v`, before `p256_import` and `p256_export` (`test/backends/opaque.hpp`,
> message test `opaque_keys_kept`).

## Risks / Trade-offs

- **Concepts too narrow for the browser backend**: mitigated by the opaque stand-in now, synchronous
Expand Down
Loading