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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ tag is placed.
| 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 | [primitives](libraries/crypto/openspec/changes/primitives) | crypto: RFC 8439's raw ChaCha20 and Poly1305 as building blocks: the stream cipher on libsodium, BearSSL and OpenSSL, the single-use key authenticator on libsodium and OpenSSL | wrapper | implementing | | 2026-11-23 | [code](https://github.com/webcpporg/crypto) |
| crypto | [rfc9001-tests](libraries/crypto/openspec/changes/rfc9001-tests) | crypto: RFC 9001 Appendix A's QUIC packet protection vectors as interop tests, on every AES-GCM backend, no QUIC API | wrapper | implementing | | 2026-12-01 | [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
11 changes: 11 additions & 0 deletions libraries/crypto/openspec/changes/primitives/.openspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
schema: spec-driven
created: 2026-11-30
webcpp:
library: crypto
title: "crypto: RFC 8439's raw ChaCha20 and Poly1305 as building blocks: the stream cipher on libsodium, BearSSL and OpenSSL, the single-use key authenticator on libsodium and OpenSSL"
kind: wrapper
port_of: null
status: implementing
release: null
implementation: https://github.com/webcpporg/crypto
decided: 2026-11-23
1,054 changes: 1,054 additions & 0 deletions libraries/crypto/openspec/changes/primitives/design.md

Large diffs are not rendered by default.

117 changes: 117 additions & 0 deletions libraries/crypto/openspec/changes/primitives/proposal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
## Why

webcpp::crypto has ChaCha20 only inside its AEADs: `ciphers::chacha20_poly1305` seals and opens
with Poly1305 around the keystream, and a program cannot reach the keystream itself. Protocols
that fix their own construction need the raw RFC 8439 stream cipher. The first is Nostr's NIP-44
(version 2), the encryption of Nostr's direct messages, which webcpp's Nostr work needs: it
derives a ChaCha20 key and a 12-byte nonce with HKDF, encrypts the padded message with ChaCha20
from block counter 0, and authenticates the nonce and the ciphertext with HMAC-SHA256; the AEAD
cannot produce those bytes. Every piece of NIP-44 but the stream cipher is already in webcpp
(secp256k1's ECDH in crypto, HKDF and HMAC in webcpp::hash, base64 in webcpp::codecs).

On 2026-11-23 the owner approved a small change, "primitives", for ChaCha20 and the typed x-only
key; on 2026-11-24 the x-only key moved into crypto's `formats` change, and this change keeps the
raw ChaCha20 alone. NIP-44 itself stays out of crypto: its vectors only prove the cipher where a
real protocol uses it.

On 2026-12-01 the owner added RFC 8439's raw Poly1305 (owner decision 6), the other half of the
AEAD's construction: a protocol that derives a one-time key per message (the ChaCha20 keystream's
first block, as RFC 8439 section 2.6 does) and authenticates with Poly1305 needs the authenticator
under its own key, which no AEAD exposes. It lives in crypto, not beside HMAC in `hash::mac`,
because its key must never be reused, and in a purpose namespace whose name says so. The owner also
asked for a test that composes RFC 8439's AEAD from the two raw pieces and finds
`chacha20_poly1305`'s bytes.

## What Changes

- **`ciphers::basic_chacha20<Family, Backend>` and `ciphers::chacha20<Backend>`**, RFC 8439
section 2.4's ChaCha20 (a 32-byte key, a 12-byte nonce, a 32-bit block counter), in
`<webcpp/crypto/ciphers/stream_cipher.hpp>` and `<webcpp/crypto/ciphers/chacha20.hpp>`, the
family `families::chacha20`: `from_key(key)`, and
`apply_keystream(nonce, data, initial_counter)`, which XORs the keystream from block
`initial_counter` into `data` in place (encryption and decryption are the same call); move-only,
the key wiped; every function holding webcpp::secure's `timing_guard`.
- **A purpose namespace, `crypto::unauthenticated_encryption`** (owner, 2026-11-30), sibling of
`secret_key_encryption`, in `<webcpp/crypto/unauthenticated_encryption/stream_cipher.hpp>`: the
concept `unauthenticated_encryption::stream_cipher` (not an AEAD: it authenticates nothing) and
the free form `unauthenticated_encryption::apply_keystream(cipher, nonce, data,
initial_counter)`; `crypto::from_key<C>` serves it as it serves the AEADs. The concept reads
the algorithm's own `nonce_size`, `block_size` and `counter_type`, not ChaCha20's (owner,
2026-12-01), so that AES-CTR, a later crypto change, joins it unchanged. Its name warns, its
page says when to use it (a protocol that brings its own authentication) and points to
`secret_key_encryption` as the normal path; `ciphers` keeps the algorithms alone.
- **A counter overflow error**: `error::counter_overflow`, a new value of `crypto::error`, returned
before any backend runs when `data` needs more blocks than the counter has left after
`initial_counter` (computed from the algorithm's block size and counter width; RFC 8439's 32-bit
counter, 256 GiB from counter 0), so that no backend wraps,
carries into the nonce or aborts the program.
- **Three backends**: libsodium (`crypto_stream_chacha20_ietf_xor_ic`, the default on every
target, as ChaCha20-Poly1305's), BearSSL (`br_chacha20_ct_run` or its SSE2 code) and OpenSSL
(`EVP_CIPHER` "ChaCha20", native, wasip2, wasip3); the capability table, its generated refusals
(ctaes and secp256k1 have no ChaCha20), `supports_v`, `default_backend_t` and the alias
`/webcpp/crypto//chacha20-default`.
- **The four forms**, as ChaCha20-Poly1305's: the member and the free synchronous forms, and
`async_from_key` and `async_apply_keystream`, member and free, in `<webcpp/crypto/async.hpp>`,
on a native backend; WebCrypto has no ChaCha20, so every form stops the build on a backend that
completes by itself (the browser backend of part B), with a refusal of its own.
- **Tests**: RFC 8439's vectors (sections 2.3.2, 2.4.2, A.1, A.2), the three backends' agreement on
random keys, nonces, counters and lengths up to the last block of the counter, the counter's
boundary on every backend, NIP-44 version 2's end-to-end vectors (paulmillr/nip44's
`nip44.vectors.json`, a new test-data submodule) composed in the tests from crypto's ChaCha20,
secp256k1 and webcpp::hash's HKDF and HMAC; the timing coverage, the constant-time lane's
paths and dudect; message tests for every refusal; no heap.

- **`authenticators::poly1305<Backend>`**, RFC 8439 section 2.5's one-time authenticator (a
32-byte key r || s, a 16-byte tag), in `<webcpp/crypto/authenticators/poly1305.hpp>`: the static
`authenticate(key, message)`, giving the tag, and `verify(key, message, tag)`, comparing in
constant time through `secure::constant_time_equal` and returning `authentication_failed` on a
mismatch; no keyed object and no `from_key`: the key enters every call. No new error value.
- **A purpose namespace, `crypto::single_use_key_authentication`** (owner, 2026-12-01), in
`<webcpp/crypto/single_use_key_authentication/authenticator.hpp>`: the concept
`single_use_key_authenticator` and the free forms `authenticate<A>` and `verify<A>`, and
`async_authenticate` and `async_verify` (static members and free forms) in `async.hpp`. Its page
says the key is used once, points a reusable key to `hash::mac` (HMAC), and says one-time
passwords (HOTP, TOTP) are not what it does.
- **Two backends for Poly1305**: libsodium (`crypto_onetimeauth_poly1305`, the default on every
target) and OpenSSL (`EVP_MAC` "POLY1305", native, wasip2, wasip3); BearSSL (Poly1305 only inside
its AEAD), ctaes, secp256k1 and the browser's backend refused at compile time.
- **Poly1305's tests**: RFC 8439's section 2.5.2 and Appendix A.3 vectors, the two backends'
agreement, forged tags, the timing coverage, the constant-time lane and dudect, no heap; and the
composition matrix: RFC 8439 section 2.8's AEAD built from the raw ChaCha20 and Poly1305, equal byte
for byte to `chacha20_poly1305`, across the 18 combinations of the three pieces' backends, the
AEAD itself unchanged.

Not changed: ChaCha20-Poly1305 and its forms, every existing name, every existing error value.

## Capabilities

### New Capabilities

None.

### Modified Capabilities

- `crypto`: RFC 8439's raw ChaCha20 stream cipher, its concept, its counter overflow error and
its three backends; RFC 8439's raw Poly1305, its purpose namespace, its concept and its two
backends.

## Impact

- **Kind:** a wrapper (`"port-of": null`), as the genesis.
- **Breaking:** no; new names and one new error value (22, after hpke's 21). Order: after crypto's
`formats` (on which the NIP-44 test reads its base64 through webcpp::codecs), codecs' Q1 and
crypto's `hpke`; before crypto's `backend-concepts`, whose catalogue gains the stream cipher's
and the authenticator's capabilities, and part B, whose browser backend refuses ChaCha20 and
Poly1305 through this change's refusals.
It needs nothing from `hpke` and `hpke` nothing from it (design, "The queue").
- **Implementation:** webcpporg/crypto (`ciphers/stream_cipher.hpp`, `ciphers/chacha20.hpp`,
`unauthenticated_encryption/stream_cipher.hpp`, `authenticators/poly1305.hpp`,
`single_use_key_authentication/authenticator.hpp`, `config.hpp`,
`detail/engines.hpp`, `detail/asynchronous.hpp`, `async.hpp`, `error.hpp`, the three backends'
headers, tests, message tests and their MSVC twins, the constant-time lane's paths and dudect,
the sizes programs, the benches, an example, `doc/`, `doc/tables/crypto.json` and its generated
files), the superproject (the NIP-44 vectors as a test-data submodule, its `third_party/Jamfile`
entry, CODING-GUIDELINES.md's third-party row, the crypto fixtures, the pointer).
- **Status:** the owner approved the scope on 2026-11-23 and narrowed it on 2026-11-24
(`decided`), answered its namespace question on 2026-11-30, and added Poly1305 and the composition test on
2026-12-01 (about 24 hours in all); `proposed` until the owner reviews this design; `reference/plan.md` holds the plan.
Loading