Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ tag is placed.
| config | [timing-guard](libraries/config/openspec/changes/archive/2026-11-24-timing-guard) | config: one DIT timing guard for every webcpp library, moved from crypto and hash, Windows on ARM64 covered | own | withdrawn | | 2026-11-24 | |
| crypto | [genesis](libraries/crypto/openspec/changes/genesis) | crypto: authenticated encryption, randomness and secp256k1 over C backends | wrapper | implementing | | 2026-10-11 | [code](https://github.com/webcpporg/crypto) |
| crypto | [backend-concepts](libraries/crypto/openspec/changes/backend-concepts) | crypto: every backend capability an internal concept, the contract of the shipped backends | wrapper | proposed | | 2026-11-22 | |
| crypto | [hpke](libraries/crypto/openspec/changes/hpke) | crypto: HPKE (RFC 9180) composed from crypto's own primitives, readable names, the mode from the arguments | wrapper | proposed | | 2026-11-23 | |
| crypto | [foundation](libraries/crypto/openspec/changes/foundation) | crypto: the cryptographic foundation, every backend in full, one default per family and a browser backend | wrapper | accepted | | 2026-11-14 | |
| crypto | [api-gaps](libraries/crypto/openspec/changes/api-gaps) | crypto: the API gaps of version 0.2 closed, and dudect judged on hosted runners | wrapper | implementing | | 2026-11-20 | [code](https://github.com/webcpporg/crypto) |
| crypto | [data-independent-timing](libraries/crypto/openspec/changes/data-independent-timing) | crypto: the library sets arm64's DIT around every cryptographic operation | wrapper | implementing | | 2026-11-20 | [code](https://github.com/webcpporg/crypto) |
| crypto | [formats](libraries/crypto/openspec/changes/formats) | crypto: one encode and one decode for every key and signature format, the format as a value | wrapper | implementing | | 2026-11-22 | [code](https://github.com/webcpporg/crypto) |
| crypto | [hpke](libraries/crypto/openspec/changes/hpke) | crypto: HPKE (RFC 9180) composed from crypto's own primitives, readable names, the mode from the arguments | wrapper | implementing | | 2026-11-23 | [code](https://github.com/webcpporg/crypto) |
| crypto | [one-verb](libraries/crypto/openspec/changes/one-verb) | crypto: codecs' verbs are crypto's: crypto::encode and crypto::decode removed, the check declared to codecs, PEM on a fixed label and decoded in place | wrapper | implementing | | 2026-12-02 | [code](https://github.com/webcpporg/crypto) |
| crypto | [unified-sign](libraries/crypto/openspec/changes/unified-sign) | crypto: one sign and verify on every curve, the scheme always named, the key pair contract and four forms of each operation | wrapper | implementing | | 2026-11-22 | [code](https://github.com/webcpporg/crypto) |
| crypto | [uniform-types](libraries/crypto/openspec/changes/uniform-types) | crypto: typed signatures, typed X25519 keys, and concepts for key pairs, AEADs and boxes | wrapper | implementing | | 2026-11-22 | [code](https://github.com/webcpporg/crypto) |
Expand Down
4 changes: 2 additions & 2 deletions libraries/crypto/openspec/changes/hpke/.openspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ webcpp:
title: "crypto: HPKE (RFC 9180) composed from crypto's own primitives, readable names, the mode from the arguments"
kind: wrapper
port_of: null
status: proposed
status: implementing
release: null
implementation: null
implementation: https://github.com/webcpporg/crypto
decided: 2026-11-23
88 changes: 88 additions & 0 deletions libraries/crypto/openspec/changes/hpke/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,80 @@ returning to the owner.
agreement and an AEAD, both already measured; a later measurement is a release build's, never a
debug build's.

## Rulings made while building (2026-12-04)

Settled by the builder where the design left a choice open or met a fact it did not foresee; each
keeps the owner's decisions and the design's rulings.

- **R13: the asynchronous forms take one pack**, `Suite::async_begin_sending(executor,
arguments_then_token...)` and the same for `async_begin_receiving`, the single-shot `async_seal`
and `async_open`, member and free: the synchronous arguments then the completion token, the
arguments kept until the call runs as the synchronous call takes them (a span as a span, writable
where the argument is; a key, an encapsulated key and a pre-shared key by value; a key pair,
which cannot be copied, by reference) and handed to the synchronous overloads. Why: the four
argument lists stay the synchronous overloads' alone, with one catch-all diagnostic; sixteen more
declarations, each repeating them, would add nothing a reader needs. `async_derive_from_seed`
and the contexts' `async_seal` and `async_open` keep their fixed signatures.
- **R14: the encapsulated key is default-constructible, to its zero value**, public: a regular
value type, as R5 calls it, which a moved-from sender holds (B14) and an error completion gives.
No recipient accepts it: X25519's agreement refuses it as `weak_public_key`, P-256's
`from_bytes` as `invalid_public_key`; the tests check both.
- **R15: `default_backends` and `backends<KeyBackend, CipherBackend>` are declared in
`hpke/context.hpp`**, which `hpke.hpp` includes, rather than in `hpke.hpp` (B1): the contexts'
AEAD type is resolved from them. `detail::hpke_access` is in `hpke.hpp`, beside the setups it
calls. What a program writes is unchanged.
- **R16: the catch-alls test the four argument lists through declared-only functions**
(`detail::hpke_forms<Suite>`, the four forms' parameter types, never defined), so that a call
that converts (a vector to a span) still reaches its real overload and any other list reaches
the catch-all and its one message (B2). A braced `{}` argument, which no template deduces,
reaches no catch-all: the compiler's own "no matching function" names the candidates.
- **R17: `export_secret<Length>` declares `result<secure::secret<Length>>` for every valid
length and a one-byte secret outside the bounds**, so that its `static_assert` with the bounds is
the build's only error (`secure::secret<0>` would add webcpp::secure's own); the message test
pins it.
- **R18: the counting test runs on the backends that allocate nothing**, libsodium and BearSSL
(X25519 beside BearSSL's AEADs, P-256 wholly on BearSSL), every suite: OpenSSL's AES-GCM,
AES-GCM's native default, allocates its context on the C heap, which the counting test counts
with operator new's, and is its page's (D10).
- **R19: OpenSSL's limits include a plaintext of at least one byte**: `OSSL_HPKE_seal` refuses an
empty one, which RFC 9180 allows; the cross-check seals 1, 17 and 300 bytes, and the empty
message is checked by `hpke_test` (B10).
- **R20: two vector programs**: on the defaults, every target (natively AES-GCM on OpenSSL), and
with BearSSL's AEADs (P-256 wholly on BearSSL, X25519 on `backends<sodium, bearssl>`, MLS's suite
natively without OpenSSL, the spec's first scenario), so that every vector runs on both AES-GCM
backends natively. The key encapsulation's and the key schedule's values are read through the
`detail` functions the setups call (`hpke_dhkem`, `hpke_key_schedule`), not stored in the
contexts (B11); `detail::hpke_access` keeps the seam and the sequence number (R9).
- **R21: the vectors' superproject change lands first, on its own branch**: a library's CI checks
out webcpp's `main`, so `third_party/libs/hpke` and `/third_party//hpke` must be on `main` before
crypto's tests run there. The superproject's pull request `third_party/hpke-vectors` (the
submodule, `third_party/Jamfile`, the pin and refusal tests of `tools/test/third_party_test.py`,
the guidelines' table, the release notes) merges before crypto's; the gitlink of crypto follows
in its own pull request. The repository's licence (B3): an IETF Contribution under the IETF
Trust's Legal Provisions, code components under the Simplified BSD License; the submodule
redistributes nothing.
- **R22: the constant-time paths derive the ephemeral key from a secret seed through the test
seam**: the generator's draws are not marked secret by the checkers, so a path with `generate()`
would trace nothing of the sender's agreement. dudect gains HPKE's key schedule against its
shared secret (`hpke_key_schedule`, P-256, HKDF-SHA256 and AES-128-GCM on BearSSL, auth_psk) with
its A/A control (`hpke_key_schedule_aa`): the composition HPKE adds, the curves and AEADs keeping
their own cases. The lane's path list in `crypto.json` names HPKE's.
- **R23: twelve message tests**, D11's list (the two export lengths in one test), with the
specification's `ciphers::aes128_gcm` given as a suite's cipher; each has its MSVC twin in
`test/msvc`.
- **R24: crypto's changelog keeps ten lines**: its unreleased section was at the limit, so its two
PEM lines became one and HPKE's line took the other place.

- **R25: webcpp::hash writes `max` and `min` in parentheses**: HPKE's headers include
webcpp::hash's HKDF, whose `detail/limits.hpp` wrote `std::numeric_limits<std::size_t>::max()`,
which a bare `<windows.h>` before it turns into MSVC's C2589; crypto's `windows_h_before` test
found it on MSVC 14.3. webcpporg/hash's branch `hash/windows-h-macros` parenthesizes every `max`
and `min` of its headers and adds the test `windows_h`; the superproject's gitlink of hash moves
with the vectors (R21), before crypto's pull request.
- **R26: no fuzz target**: crypto's fuzz lane fuzzes its formats' decoders; HPKE decodes nothing:
its untrusted inputs are an encapsulated key of a fixed size, which the curves' fuzzed
`from_bytes` validates, and AEAD ciphertexts, which the tag rejects.

## Risks / Trade-offs

- **A dependency on a hash change** (R1): HPKE cannot start before `hkdf-pieces` merges; that
Expand All @@ -732,3 +806,17 @@ returning to the owner.
Nothing migrates: HPKE is new. After crypto/uniform-types and hash's `hkdf-pieces` merge:
branches `crypto/hpke` of webcpporg/crypto, webcpporg/proposals and the superproject (the vectors
submodule); local verification on every lane; merged by the controller; one CI at the end.

## As built

Built on 2026-12-04: crypto's branch `crypto/hpke` (one commit), the superproject's
`third_party/hpke-vectors` (webcpporg/webcpp#66, the vectors, R21) and hash's
`hash/windows-h-macros` (webcpporg/hash#20, R25). Local verification, crypto's tests and examples:
clang and gcc-15 natively, wasip2, wasip3 and Emscripten, every target clean (first run 684, 489,
634, 616 and 410 passed, then the final head's rebuilt targets); MSVC 14.3 and 14.5 x64 debug
690 passed each on the first run, whose five failures (C2589 of hash's `max` after a bare
`<windows.h>`, C4459 of the asynchronous test's globals) were fixed (R25), then 273 rebuilt targets
passed each on the final head, no new failure; the constant-time lane on Linux (memcheck,
MemorySanitizer and dudect, HPKE's paths and `hpke_key_schedule` |t| 1.45, its A/A control 1.32)
and natively; the macOS lint and the Linux lint clean. Each test was seen failing first: one label
changed (576 failed exports in the vectors), the pre-shared key's bound at 31 (16 suites).
5 changes: 3 additions & 2 deletions libraries/crypto/openspec/changes/hpke/proposal.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,6 @@ None.
RFC 9180 test vectors as a test-data submodule, its `third_party/Jamfile` entry and
CODING-GUIDELINES.md's third-party table, the pointer).
- **Status:** approved by the owner on 2026-11-23 (`decided`), the open questions answered
(design.md, owner decisions 10 to 14); `proposed` until `reference/plan.md` carries the plan's
code, then `accepted`, which `tools/proposals.py` requires.
(design.md, owner decisions 10 to 14); `implementing` since 2026-12-04, `reference/plan.md`
carrying the plan's code: built on crypto's branch `crypto/hpke`, the vectors on the
superproject's `third_party/hpke-vectors` (design R21).
155 changes: 155 additions & 0 deletions libraries/crypto/openspec/changes/hpke/reference/plan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
---
title: "webcpp::crypto, HPKE (RFC 9180): Implementation Plan"
tags: [webcpp, crypto, hpke, rfc9180, mls, plan]
status: accepted
created: 2026-12-04
---

# webcpp::crypto, HPKE (RFC 9180): Implementation Plan

**Goal:** RFC 9180's HPKE in `webcpp::crypto::public_key_encryption::hpke`, composed from crypto's
X25519 and P-256 agreements, webcpp::hash's HKDF over pieces and crypto's AES-GCM and
ChaCha20-Poly1305, every CFRG vector of those suites passing on every target and OpenSSL's HPKE
agreeing within its limits.

**Architecture:** `../design.md` D1 to D12, R1 to R24, B1 to B15. Four headers, no backend of
its own; the asynchronous forms in `async.hpp`; one new error value.

**Spec:** `../proposal.md`, `../design.md`, `../specs/crypto/spec.md`, `../tasks.md`.

Paths are relative to the library, `libs/crypto/` of the superproject, unless they start with the
superproject's root. `$B2` is `env -u CPATH -u CPLUS_INCLUDE_PATH -u C_INCLUDE_PATH b2
--user-config=.local/user-config.jam`, run from the superproject's root.

## Global Constraints

- English everywhere, no em dash, no trailer in commits; never edit a generated file (run
`python3 doc/tables/generate.py`, then `--check`).
- Every public function that computes on a secret holds `secure::timing_guard` first, is
`noexcept` and returns a `result`; every intermediate secret is a `secure::secret<N>`.
- Nothing allocates; no variable-length input is capped (owner decision 6).

## Task 1: the test data and the error value

Files: the superproject's `.gitmodules`, `third_party/libs/hpke` (cfrg/draft-irtf-cfrg-hpke at
`5f503c5`, shallow), `third_party/Jamfile`, `tools/test/third_party_test.py`,
`CODING-GUIDELINES.md`, `AGENTS.md`, `doc/releases/0.1.0.adoc`; crypto's
`include/webcpp/crypto/error.hpp`, `test/error_test.cpp`, `doc/errors.adoc`.

- [x] Step 1: `/third_party//hpke`, as `/third_party//bips` is:

```jam
path-constant HPKE : libs/hpke ;
.hpke = [ regex.replace $(HPKE) "\\\\" "/" ] ;
.hpke-files = [ glob libs/hpke/test-vectors.json ] ;
alias hpke : : <conditional>@hpke-checked-out : :
<define>"\"WEBCPP_TEST_HPKE=\\\"$(.hpke)\\\"\""
[ webcpp.data $(HPKE) ]
<dependency>$(.hpke-files) ;
explicit hpke ;
```

- [x] Step 2: `error::message_limit_reached = 21`, "the context has sealed or opened as many
messages as its nonce allows"; `error_test` counts 21 values.

## Task 2: the labeled key derivation

Files: `include/webcpp/crypto/public_key_encryption/hpke/key_derivation.hpp`.

- [x] Step 1: the tags and the concept (D2), then RFC 9180's labeled HKDF over pieces (D4):

```cpp
template <class KeyDerivation>
[[nodiscard]] boost::system::result<void> hpke_labeled_expand(
std::span<std::uint8_t const, KeyDerivation::hash_size> pseudorandom_key,
std::span<std::uint8_t const> suite_identifier, std::span<std::uint8_t const> label,
std::span<std::uint8_t const> application_info,
std::span<std::uint8_t> output_key_material) noexcept {
std::array<std::uint8_t, 2> const length_bytes{
static_cast<std::uint8_t>(output_key_material.size() >> 8U),
static_cast<std::uint8_t>(output_key_material.size() & 0xffU)};
auto const expanded = hash::hkdf_expand<typename KeyDerivation::hmac_type>(
output_key_material, pseudorandom_key,
{length_bytes, hpke_version_label, suite_identifier, label, application_info});
if (!expanded) {
secure::erase_secret(output_key_material);
return error::backend_failure;
}
return {};
}
```

## Task 3: the key encapsulations

Files: `include/webcpp/crypto/public_key_encryption/hpke/key_encapsulation.hpp`.

- [x] Step 1: `basic_encapsulated_key<K>`, the concept `key_encapsulation_mechanism`, the two
tags with their sizes, `key_derivation_type`, `family_type`, `keypair_type<B>` and
`public_key_type<B>` (D2, D3, R5, R14).
- [x] Step 2: `detail::hpke_dhkem<K, B>`: `extract_and_expand`, `encapsulate` and `decapsulate`
with an optional sender, the agreements joined in a secret, the context on the stack.
- [x] Step 3: `derive_from_seed`: X25519's `sk` unclamped (R7), P-256's candidate loop, its verdict
`p_curve_scalar_valid`'s, declassified; a seed shorter than 32 bytes refused (R6).

## Task 4: the key schedule, the contexts and the suite

Files: `include/webcpp/crypto/public_key_encryption/hpke/context.hpp`,
`include/webcpp/crypto/public_key_encryption/hpke.hpp`, `include/webcpp/crypto.hpp`.

- [x] Step 1: the cipher tags, `suite_cipher`, `sealing_cipher`, `default_backends` and
`backends<K, C>` (R15), the key schedule (D5):

```cpp
schedule.key_schedule_context[0] = mode;
std::ranges::copy(psk_id_hash->bytes(), schedule.key_schedule_context.begin() + 1);
std::ranges::copy(info_hash->bytes(), schedule.key_schedule_context.begin() + 1 + hash_size);
auto extracted = hpke_labeled_extract<key_derivation>(shared_secret, suite_identifier,
secret_label, pre_shared_key);
```

- [x] Step 2: `detail::hpke_context_core<Suite>` (the AEAD, the base nonce, the exporter secret,
the 64-bit sequence number), `sender_context` and `recipient_context`, move-only, wiped.
- [x] Step 3: `suite<K, D, C, Backend>`: the four `begin_sending`, the four `begin_receiving`, the
four single-shot `seal` and `open` (`requires sealing_cipher<Cipher>`), each name's catch-all
(R16), `derive_from_seed`; `pre_shared_key`; the free forms; `detail::hpke_access`.

## Task 5: the tests

Files: `test/support/hpke_vectors.hpp`, `test/hpke_vectors_default_test.cpp`,
`test/hpke_vectors_bearssl_test.cpp`, `test/hpke_test.cpp`, `test/hpke_allocation_test.cpp`,
`test/hpke_openssl_test.cpp`, `test/Jamfile`.

- [x] Step 1: the 64 CFRG entries through every path, on the defaults and with BearSSL's AEADs
(R20); seen failing first with one label changed (576 failed exports).
- [x] Step 2: the four modes, refusals, minimums, sequence limit, Marmot's two recovery-key
vectors; seen failing first with the pre-shared key's bound at 31 (16 suites).
- [x] Step 3: no allocation and the long inputs against the joined computation (R18).
- [x] Step 4: OpenSSL's HPKE, both directions, every suite and mode (R19).

## Task 6: the asynchronous forms

Files: `include/webcpp/crypto/async.hpp`, `test/hpke_async_test.cpp`.

- [x] Step 1: `detail::hpke_complete`, the members' and the free forms' definitions (R13), the
browser's refusal (R8).

## Task 7: messages, coverage, the constant-time lane

Files: `test/messages/hpke_*`, `test/msvc/hpke_*`, `test/support/hpke_coverage.hpp`,
`test/timing_coverage_sodium_test.cpp`, `test/timing_coverage_bearssl_test.cpp`,
`test/constant_time/paths.hpp`, `test/constant_time/dudect_test.cpp`, `test/user_globals_test.cpp`.

- [x] Step 1: twelve message tests and their MSVC twins (R23).
- [x] Step 2: every HPKE function's guard on libsodium and BearSSL.
- [x] Step 3: HPKE's two paths (R22) and dudect's key schedule with its A/A control.

## Task 8: the page and the delivery

Files: `doc/public_key_encryption.adoc`, `doc/async.adoc`, `doc/overview.adoc`,
`doc/secret_key_encryption.adoc`, `doc/changelog.adoc`, `doc/tables/crypto.json`, `README.md`,
`example/hpke_base.cpp`, `example/hpke_base.expected`, `example/Jamfile`.

- [x] Step 1: the guide's HPKE section with D12's table; the example; `crypto.json` (the
namespace's row, the operation, the use case, the twelve fixes, the lane's path); the generated
files rerun.
- [ ] Step 2: every lane, the pull requests, the merges, the superproject's gitlink.
Loading