304 lines
20 KiB
Markdown
304 lines
20 KiB
Markdown
# Day 1 — Arcanum
|
||
|
||
Status: v0.1.0 candidate, **not audited**. Nothing here is called secure before an external audit.
|
||
|
||
## Why
|
||
|
||
Every Domi product that moves data between devices (Via first, then Canalis, Custos sealing and
|
||
Tabellarius) needs a key exchange that survives a future quantum computer. Traffic recorded today
|
||
can be decrypted later ("harvest now, decrypt later"), so the protection has to be in place before
|
||
such a computer exists. Arcanum is the one small, reviewable crate that provides it, so the
|
||
ecosystem audits one piece of code instead of five.
|
||
|
||
## What
|
||
|
||
| Piece | What it does | Standard |
|
||
|:--|:--|:--|
|
||
| `xwing` | Hybrid KEM: ML-KEM-768 + X25519, X-Wing exactly as drafted | draft-connolly-cfrg-xwing-kem-11, FIPS 203, RFC 7748 |
|
||
| `exchange` | A **mutual** exchange: each peer encapsulates to the other's public key; both get a `Session` bound to both secrets, the full transcript and a caller context | HKDF (RFC 5869) with SHA-256 |
|
||
| `Session::wireguard_psk(epoch)` | The 32-byte WireGuard pre-shared key for one rotation epoch | HKDF-Expand |
|
||
| `seal` / `open` | One-shot sealed box to a recipient's hybrid public key | X-Wing + HKDF + ChaCha20-Poly1305 (RFC 8439) |
|
||
|
||
Not in scope for Day 1: networking, WireGuard itself, signatures (ML-DSA comes later), migrating
|
||
other Domi apps.
|
||
|
||
## How
|
||
|
||
### Primitives (all from vetted crates, none written here)
|
||
|
||
| Primitive | Crate | Version locked | Notes |
|
||
|:--|:--|:--|:--|
|
||
| ML-KEM-768 | `ml-kem` (RustCrypto) | 0.3.4 | FIPS 203 final. README: "never been independently audited". |
|
||
| X25519 | `x25519-dalek` | 3.0.0 | with `curve25519-dalek` 5.0.0 |
|
||
| SHA3-256 | `sha3` (RustCrypto) | 0.12.0 | X-Wing combiner |
|
||
| SHAKE256 | `shake` (RustCrypto) | 0.1.0 | X-Wing key expansion |
|
||
| HKDF-SHA-256 | `hkdf` 0.13.0, `hmac` 0.13.0, `sha2` 0.11.1 | | key schedule |
|
||
| ChaCha20-Poly1305 | `chacha20poly1305` (RustCrypto) | 0.11.0 | sealing |
|
||
| Zeroisation | `zeroize` | 1.9.2 | every secret type is `ZeroizeOnDrop` |
|
||
|
||
Exact versions are pinned by `Cargo.lock`, which is committed. `ml-kem` and `x25519-dalek` are
|
||
pinned with `=` in `Cargo.toml` as well, so an upgrade is a deliberate edit that re-runs the KATs.
|
||
|
||
### The hybrid KEM: X-Wing, draft -11 (decision D-QA-1)
|
||
|
||
Facts checked on 2026-11-25:
|
||
|
||
- The current draft is **draft-connolly-cfrg-xwing-kem-11**, dated 2026-09-23, an individual
|
||
Internet-Draft (intended status Informational), not an RFC and not CFRG-adopted.
|
||
Source: <https://datatracker.ietf.org/doc/draft-connolly-cfrg-xwing-kem/>.
|
||
- Its change log lists only editorial changes since -06 (ASN.1 module, SHAKE output length stated
|
||
in bits, implementation list, PEM header). Source: Appendix G of
|
||
<https://www.ietf.org/archive/id/draft-connolly-cfrg-xwing-kem-11.txt>.
|
||
- Appendix C has three vectors and still carries the note "TODO: replace with test vectors that
|
||
re-use ML-KEM, X25519 values", so **later drafts may change the vectors**. Arcanum pins the vector
|
||
file by sha256 together with the draft number (`tests/vectors/SOURCES.md`); a changed file fails
|
||
the KAT, and moving to a new draft is a deliberate decision.
|
||
- RustCrypto's `x-wing` 0.1.1 (2026-10-01) describes itself as draft 06; its vectors are the same
|
||
three as draft -11 (identical after removing whitespace).
|
||
|
||
**Decision D-QA-1 (recommended by QA, adopted).** Mark's spec asks for a combiner that hashes both
|
||
shared secrets, both ciphertexts and both public keys, "X-Wing-style … use it if it fits". X-Wing's
|
||
combiner hashes `ss_M`, `ss_X`, `ct_X` and `pk_X` and deliberately leaves `ct_M` and `pk_M` to
|
||
ML-KEM's own binding (draft -11 §5.3). So:
|
||
|
||
1. **The KEM is X-Wing, unmodified**, so the draft's vectors pass byte for byte:
|
||
```
|
||
expand(sk) = SHAKE256(sk, 96 bytes) -> d = [0:32], z = [32:64], sk_X = [64:96]
|
||
(pk_M, dk_M) = ML-KEM-768.KeyGen_internal(d, z); pk_X = X25519(sk_X, 9)
|
||
Encaps(pk, eseed) : (ct_M, ss_M) = ML-KEM-768.Encaps_internal(pk_M, eseed[0:32])
|
||
ek_X = eseed[32:64]; ct_X = X25519(ek_X, 9); ss_X = X25519(ek_X, pk_X)
|
||
Combiner = SHA3-256(ss_M || ss_X || ct_X || pk_X || "\.//^\")
|
||
```
|
||
2. **Both public keys and both ciphertexts are bound in Arcanum's own key schedule**, where the
|
||
`domi/arcanum/v1/...` labels live (below). That meets the spec in full, and the spec's own
|
||
"use it if it fits" condition holds.
|
||
|
||
Arcanum composes X-Wing from `ml-kem`, `x25519-dalek`, `sha3` and `shake` rather than calling the
|
||
`x-wing` crate, because mandatory test 3 needs the combiner as a separate function and because
|
||
Arcanum wants typed errors for every malformed input. The composition is about twenty lines of
|
||
glue between vetted primitives. It is pinned by the draft's three vectors **and** by a differential
|
||
test against RustCrypto `x-wing` (a dev-dependency only) over random seeds and random ciphertexts.
|
||
|
||
**Stricter than the draft, on purpose.** Arcanum rejects an X25519 public key or ciphertext part
|
||
that is a low-order point (`was_contributory() == false`, a constant-time check in `x25519-dalek`).
|
||
An honest key or ciphertext is never such a point, so this changes no honest exchange and no
|
||
official vector; it removes the case where the classical half silently contributes a known value
|
||
(QA A-16, A-17). RustCrypto ships the same option as `DecapsulationKeyRejectNonContrib`.
|
||
|
||
**Canonical X25519 public keys only (QA R2-02).** Draft -11 says nothing about the encoding of
|
||
`pk_X` beyond "the 32 byte string representing the u-coordinate" (§4), and its Python reference
|
||
decodes with `% p`, so it would accept aliases. RFC 7748 decoders mask bit 255 and reduce mod p,
|
||
which means `pk_X`, `pk_X` with bit 255 set, and `pk_X + p` (where that fits) are the same key to
|
||
X25519 but different bytes to the combiner and the transcript: a peer given an alias "succeeds" and
|
||
silently derives a PSK the owner never matches. `PublicKey::from_bytes` therefore refuses an
|
||
X25519 half with bit 255 set or a value of at least 2^255 - 19, as `Error::InvalidPublicKey`.
|
||
X25519 outputs are always canonical, so no honest key and no official vector is affected. A
|
||
ciphertext's `ct_X` is not checked this way: it is single-use, and its raw bytes are bound by the
|
||
combiner (QA A-04), so an alias only gives a different secret.
|
||
|
||
**ML-KEM key check.** The draft requires the FIPS 203 §7.2 encapsulation key check.
|
||
`ml-kem`'s `EncapsulationKey::new` performs it; Arcanum maps its failure to
|
||
`Error::InvalidPublicKey`, and the NIST ACVP `encapsulationKeyCheck` vectors check that.
|
||
|
||
**X25519 clamping.** Five bits of the 32-byte X25519 ephemeral scalar (bits 0–2 of the first byte,
|
||
bits 6–7 of the last) are clamped by RFC 7748, so changing only those bits of `eseed[32:64]` does
|
||
not change the exchange. That is the standard's behaviour, not a binding gap; the byte-flip test
|
||
flips bit 4 of each byte.
|
||
|
||
### Is `ml-kem` stable enough?
|
||
|
||
- `ml-kem` 0.3.4 (2026-10-31) is the newest release; the 0.3 line has been out since 2026-04-28
|
||
(0.3.0, 0.3.2, 0.3.3, 0.3.4). Source: <https://crates.io/api/v1/crates/ml-kem>. It depends on
|
||
`rand_core` 0.10 (read from its `Cargo.toml`).
|
||
- It is still 0.x, so a minor bump may break the API. Its README says it implements FIPS 203
|
||
(final) and that it **"has never been independently audited"**. Source:
|
||
<https://github.com/RustCrypto/KEMs/blob/master/ml-kem/README.md>.
|
||
- **Correction to a QA fact (F10):** in 0.3.4, `EncapsulationKey::encapsulate_deterministic` is
|
||
public **whether or not** the `hazmat` feature is on; `hazmat` only un-hides it in the docs
|
||
(`#[cfg_attr(not(feature = "hazmat"), doc(hidden))]`, `src/encapsulation_key.rs`). So `ml-kem`'s
|
||
feature gives no protection; Arcanum's own `hazmat` feature (below) is what gates the
|
||
deterministic paths for Domi code.
|
||
- Judgement: stable enough for X-Wing in a household mesh where WireGuard's classical security
|
||
remains underneath (the PSK can only add security, see THREAT_MODEL.md), **not** stable enough to
|
||
be the only protection of anything. The version is pinned; an upgrade re-runs the KATs.
|
||
|
||
### `x25519-dalek`
|
||
|
||
`x25519-dalek` 3.0.0 (2026-07-06), the newest stable release, on `curve25519-dalek` 5.0.0, with
|
||
`SharedSecret::was_contributory()`. Source: <https://crates.io/api/v1/crates/x25519-dalek> and the
|
||
crate source. Used with `static_secrets` and `zeroize`.
|
||
|
||
### The exchange is mutual (decision on QA A-15)
|
||
|
||
A PSK from a **single** encapsulation authenticates only the receiver: anyone who knows the
|
||
responder's public key can encapsulate to it. Today WireGuard's static X25519 keys authenticate
|
||
the peers, but a quantum attacker can recover those from the public keys, and then a one-way PSK
|
||
is the only thing left — and the attacker could impersonate the initiator and choose it.
|
||
|
||
**Chosen: both directions.** The initiator encapsulates to the responder (`ct_IR`), the responder
|
||
encapsulates back to the initiator (`ct_RI`), and the session is derived from both secrets and the
|
||
whole transcript. Whoever lacks either secret key cannot compute the PSK. This is the design
|
||
Rosenpass uses for the same reason. API:
|
||
|
||
```
|
||
let init = exchange::initiate(&my_sk, &peer_pk, context, rng)?; // send init.ciphertext()
|
||
let (ct_ri, session) = exchange::respond(&my_sk, &peer_pk, &ct_ir, context, rng)?; // send ct_ri
|
||
let session = init.finish(&ct_ri)?;
|
||
```
|
||
|
||
What Day 3 must still do (recorded here and in THREAT_MODEL.md):
|
||
|
||
- **Authenticate the public keys.** Mutual KEM authentication is only as good as each side's copy
|
||
of the other's hybrid public key. Via distributes them signed with Tessera. If Tessera's
|
||
signatures are classical (Ed25519), a quantum attacker who can forge them can substitute keys.
|
||
That is the gap ML-DSA closes later.
|
||
- **Forward secrecy against key theft.** With static key pairs, someone who later steals both
|
||
secret keys and has recorded the exchanges can recompute every PSK. Day 3 should add an
|
||
ephemeral key pair per exchange (Rosenpass does), or rotate the key pairs.
|
||
- **One exchange per epoch**, every 120 s (below).
|
||
- **Replay and rollback protection** (QA A-13). Arcanum is pure and keeps no epoch state, so
|
||
refusing an old epoch or an old exchange is the caller's job. Arcanum does not wrap epochs: the
|
||
caller passes a `u64`, and `epoch_at` is a division (QA A-12 becomes a Day 3 test).
|
||
|
||
### Key schedule (labels `domi/arcanum/v1/...`)
|
||
|
||
```
|
||
exchange: th = SHA-256("domi/arcanum/v1/transcript/exchange"
|
||
|| pk_I (1216) || pk_R (1216) || ct_IR (1120) || ct_RI (1120)
|
||
|| u64be(len ctx) || ctx)
|
||
prk = HKDF-Extract(salt = "domi/arcanum/v1/extract", ikm = ss_IR || ss_RI || th)
|
||
psk(epoch) = HKDF-Expand(prk, "domi/arcanum/v1/wireguard-psk" || u64be(epoch), 32)
|
||
|
||
seal: th = SHA-256("domi/arcanum/v1/transcript/seal" || pk (1216) || ct (1120)
|
||
|| u64be(len aad) || aad)
|
||
prk = HKDF-Extract(salt = "domi/arcanum/v1/extract", ikm = ss || th)
|
||
key = HKDF-Expand(prk, "domi/arcanum/v1/seal/key", 32)
|
||
nonce = HKDF-Expand(prk, "domi/arcanum/v1/seal/nonce", 12)
|
||
```
|
||
|
||
- `pk_*` are whole hybrid public keys (`pk_M || pk_X`) and `ct_*` whole ciphertexts
|
||
(`ct_M || ct_X`), so both public keys and both ciphertexts of each direction are hashed.
|
||
- Roles are bound by position (initiator first), so a reflected exchange gives a different PSK
|
||
(QA A-14).
|
||
- Every transcript field before the context is fixed-width and the context is length-prefixed, so
|
||
the transcript encoding is injective (QA A-08). The HKDF-Expand helper takes exactly one label
|
||
and one fixed-width field array (QA R2-03), so its `info` is injective by its signature, not by
|
||
the discipline of its callers; this did not change any v1 byte (the golden vectors are unchanged). Labels are unique, versioned and prefix-free (QA A-07, a unit
|
||
test). The epoch is a full 64-bit big-endian value (QA A-11).
|
||
- `ctx` is the caller's binding (for Via: both Tessera device ids, the protocol name, the version).
|
||
- A v2 changes the labels and never reuses these. The v1 outputs are frozen by golden vectors
|
||
(`tests/golden_v1.rs`, QA A-10, A-38), so a "harmless" relabel fails loudly instead of making
|
||
every stored Custos blob unopenable.
|
||
|
||
### WireGuard PSK and rotation
|
||
|
||
- Rosenpass, the reference design, rotates every two minutes: `REKEY_AFTER_TIME_RESPONDER = 120 s`,
|
||
`REKEY_AFTER_TIME_INITIATOR = 130 s` (so the roles alternate), and `REJECT_AFTER_TIME = 180 s`.
|
||
Source: <https://github.com/rosenpass/rosenpass/blob/main/rosenpass/src/protocol/constants.rs>,
|
||
read 2026-11-25; the code comment cites WireGuard's own two-minute rekey. The Go port
|
||
(cunicu.li/go-rosenpass) uses the same 120 s / +10 s / 3 min.
|
||
- Arcanum takes no clock. It exports `RECOMMENDED_EPOCH_SECONDS = 120` and a pure helper
|
||
`epoch_at(unix_seconds) = unix_seconds / 120`; the caller passes the time.
|
||
- `Session::wireguard_psk(epoch)` is deterministic: both peers get the same PSK for the same epoch,
|
||
and successive epochs differ. **Forward secrecy between epochs needs a fresh exchange per
|
||
epoch.** Deriving several epochs from one session is a fallback when an exchange fails.
|
||
|
||
### Sealing
|
||
|
||
`sealed = 0x01 || ct (1120 B) || ChaCha20-Poly1305 ciphertext || tag (16 B)`, AEAD associated
|
||
data = the version byte, key and nonce from the schedule above. Each seal uses a fresh
|
||
encapsulation, so a key and nonce are never used twice. Overhead is 1137 bytes. **Anyone who knows
|
||
the recipient's public key can seal**: there is no sender authentication, and ChaCha20-Poly1305 is
|
||
not key-committing, so Custos must not read a successful `open` as proof of who sealed it
|
||
(QA A-27). The output buffer is allocated once at its final size, so no buffer that held
|
||
plaintext is reallocated and freed unwiped (QA A-29). This is a documented construction, not HPKE
|
||
(RFC 9180).
|
||
|
||
### Shape of the API
|
||
|
||
- `#![no_std]` with `alloc`, `#![forbid(unsafe_code)]`, no I/O, no clock, no OS RNG in the normal
|
||
dependency graph (checked by `tests/feature_hygiene.rs`).
|
||
- Randomness is a parameter: `&mut R where R: rand_core::CryptoRng` (rand_core 0.10). **`rng` must
|
||
be the operating system's RNG** (`getrandom`, `OsRng`) or a CSPRNG seeded from it once per
|
||
process, never a fixed, derived or configured seed (QA R2-04). `CryptoRng` is only a marker: a
|
||
CSPRNG with a constant seed satisfies it and makes `generate`, `initiate`, `respond` and `seal`
|
||
fully deterministic, which no type check can prevent. Each of those functions says so.
|
||
- **The raw one-way KEM is not in the release API (QA R2-01).** `PublicKey::encapsulate`,
|
||
`SecretKey::decapsulate` and `SharedSecret::as_bytes` exist only with `hazmat`. A one-way X-Wing
|
||
secret authenticates only the receiver and is bound to no transcript, so a release consumer
|
||
can reach a key only through `exchange` (mutual, transcript-bound) or `seal`. Interop with other
|
||
X-Wing users, if Domi ever needs it, gets its own loudly named feature and the same
|
||
feature-graph check.
|
||
- **The `hazmat` feature** exposes the deterministic entry points (`SecretKey::from_seed`,
|
||
`PublicKey::encapsulate_derand`, `exchange::{initiate,respond}_derand`, `seal::seal_derand`)
|
||
used by the known-answer tests. It is off by default. The tests turn it on through a
|
||
self-dev-dependency, which never reaches a normal build; `tests/feature_hygiene.rs` asserts that
|
||
neither `arcanum/hazmat` nor `ml-kem/hazmat` is in the normal-edge feature graph, and proves the
|
||
check can see the feature when it is on (QA A-34, A-35); `tests/qa_consumer_api.rs` compiles a
|
||
default-features consumer to prove none of these items, nor the raw KEM, can be named.
|
||
- **Rules for consumers (via-core, via, and later Canalis, Custos, Tabellarius):**
|
||
- run the same `cargo tree -e features,normal -i arcanum` / `-i ml-kem` check in CI, so no
|
||
`[dependencies]` entry turns on `arcanum/hazmat`;
|
||
- **no direct `ml-kem` (or `x25519-dalek`, `x-wing`) dependency**: ban them in `deny.toml`
|
||
(`[bans] deny = [...]`, with `arcanum` as the only allowed wrapper). In `ml-kem` 0.3.4,
|
||
`encapsulate_deterministic` is public with or without its `hazmat` feature, so a direct
|
||
dependency reopens chosen-randomness encapsulation;
|
||
- grep for `from_seed(` outside `tests/`, which catches a CSPRNG seeded from a constant (R2-04).
|
||
- Every secret type (`SecretKey`, `SharedSecret`, `Session`, `Initiation`, `WireGuardPsk`) is
|
||
`ZeroizeOnDrop` (`Initiation` got the marker with QA R2-05), is not `Copy` or `Clone`, has no
|
||
`PartialEq`, and prints as `[redacted]` in `Debug` (compile-time asserted in `tests/qa_review.rs`).
|
||
`seal` keeps its output buffer `Zeroizing` until the plaintext in it has been encrypted, so the
|
||
`MessageTooLong` path wipes the copy (R2-05).
|
||
- Every parser returns `Result<_, arcanum::Error>`; errors carry only lengths and variants.
|
||
|
||
## Tests (written first)
|
||
|
||
| # | Mandatory test | File |
|
||
|:--|:--|:--|
|
||
| 1 | KATs: ML-KEM-768 (NIST ACVP), X25519 (RFC 7748), X-Wing (draft -11) + differential vs `x-wing` | `tests/kat_mlkem768.rs`, `tests/kat_x25519.rs`, `tests/kat_xwing.rs` |
|
||
| 2 | Both sides derive the same secret; any changed input byte changes it | `tests/exchange.rs` |
|
||
| 3 | A zeroed component secret changes the output (both halves bound) | `src/binding_tests.rs`, `tests/binding.rs` |
|
||
| 4 | PSK epochs differ and are reproducible | `tests/psk_epochs.rs` |
|
||
| 5 | Malformed input gives typed errors, never a panic; fuzz target | `tests/malformed.rs`, `fuzz/fuzz_targets/untrusted_input.rs` |
|
||
| 6 | `cargo deny`, `cargo audit` clean; no unsafe | `deny.toml`, `tests/no_unsafe.rs`, `#![forbid(unsafe_code)]` |
|
||
|
||
Also: `tests/golden_v1.rs` (frozen v1 outputs), `tests/feature_hygiene.rs` (no hazmat, no OS RNG
|
||
in the release graph), and QA's `tests/qa_review.rs`, `tests/qa_consumer_api.rs` (what a
|
||
default-features consumer can and cannot compile), `src/qa_review_tests.rs` and `zeroize-probe/`.
|
||
Vector provenance is in `tests/vectors/SOURCES.md`. QA's attack plan
|
||
(`F:\Akce\Via\qa\01_arcanum_attacks.md`) is mapped case by case in the PR's self-review.
|
||
|
||
## Risks
|
||
|
||
| Risk | Consequence | Mitigation |
|
||
|:--|:--|:--|
|
||
| A bug in our X-Wing glue | Peers agree on a weak secret, or interoperate with nobody | Official vectors + differential test vs RustCrypto `x-wing` |
|
||
| `ml-kem` or `x25519-dalek` bug, side channel | Weaker PQ or classical half | Hybrid: either half suffices; versions pinned; `cargo audit` |
|
||
| Reusing one session for many epochs | No forward secrecy between epochs | Documented; Day 3 runs a fresh exchange per epoch |
|
||
| Substituted public key (MITM) | Attacker learns the PSK | Mutual exchange; Via must authenticate both hybrid public keys (Tessera); PQ signatures later. See THREAT_MODEL.md |
|
||
| Static key theft later | Recorded exchanges recomputable | Day 3 adds ephemeral keys or rotates key pairs |
|
||
| Deterministic API reaching production | Predictable PSKs | `hazmat` off by default; feature-graph test; consumers repeat it |
|
||
| Secrets left in memory | Key recovery from a memory dump | `ZeroizeOnDrop` everywhere we own the bytes; residuals listed below |
|
||
| Draft changes before RFC | Interop break with other X-Wing users | Draft number and vector sha256 pinned; labels versioned |
|
||
| A relabel or re-encoding | Stored sealed blobs unopenable; peers disagree | Golden v1 vectors |
|
||
|
||
## Gaps (known, not hidden)
|
||
|
||
- **Not audited**, and neither are `ml-kem` nor the `x-wing` reference.
|
||
- **No post-quantum authentication of public keys.** The mutual exchange authenticates whoever
|
||
holds the secret keys matching the public keys each side was given; getting the right public
|
||
keys is the caller's job, and ML-DSA is out of scope.
|
||
- **No forward secrecy against later theft of static secret keys** within Arcanum itself.
|
||
- **Side channels** are inherited from the crates; Arcanum adds no constant-time analysis of its
|
||
own (QA A-32, a dudect run, is not done).
|
||
- **Zeroisation residuals:** HKDF/HMAC and SHA-3 internal states are wiped only as far as those
|
||
crates' `zeroize` features go; values moved by the compiler may leave copies on the stack;
|
||
`PublicKey` copies are not secret. QA's allocator probe (`zeroize-probe/`, A-29) checks the heap
|
||
only: no secret block is freed unwiped; stack copies are out of its reach.
|
||
- **Sealing is not HPKE**, and has no sender authentication.
|
||
- **X25519 RFC 7748 1,000,000-iteration vector** is not run (minutes in a debug build); the
|
||
1- and 1,000-iteration vectors are.
|
||
- **FIPS 203 itself** was not read from the NIST PDF; the ML-KEM facts rest on the ACVP vectors,
|
||
the draft's references and the `ml-kem` source.
|
||
- **Fuzzing** ran on Windows MSVC with libFuzzer for the time recorded in the PR, not continuously.
|