256 lines
13 KiB
Markdown
256 lines
13 KiB
Markdown
# Tessera and the event envelope — design note (v1)
|
||
|
||
*A tessera hospitalis was a token broken between two families so they could recognise each other.*
|
||
|
||
Status: written before the code on day 1. Every format here is versioned by its magic; a change bumps it.
|
||
|
||
## 1. What it is for
|
||
|
||
One Domi identity per person, held in Custos. Every Domi app gets its **own subkey**, certified by that
|
||
identity, so no app ever creates long-term identity on its own. Apps then sign their events with the subkey;
|
||
anyone who knows the person's root public key can check that an event really came from one of their apps.
|
||
|
||
| Piece | Lives in | Holds secrets? |
|
||
|:--|:--|:--|
|
||
| Root key | the Custos vault, never leaves it (CustosPublic `custos-vault`, `docs/IDENTITY.md` there) | yes |
|
||
| Root derivation, cert format, cert verification, match code, HLC, merge | `crates/tessera` | no, except in-memory `RootKey`/`Subkey` |
|
||
| Event envelope | `crates/envelope` | no |
|
||
| Asking Custos for a cert on Windows | `crates/custos-local` | no |
|
||
|
||
`tessera` and `envelope` are **pure**: no I/O, no network, no clock. Time is always a parameter.
|
||
|
||
## 2. Encoding primitives
|
||
|
||
All multi-byte integers are big-endian. `str8` = one length byte, then that many bytes. Text fields are
|
||
restricted ASCII so there is exactly one encoding of every value:
|
||
|
||
- **id** (`app_id`, `purpose`, envelope `app`): 1–32 bytes of `[a-z0-9-]`, not starting or ending with `-`.
|
||
|
||
Base64 in JSON and QR codes is **base64url without padding**.
|
||
|
||
## 3. The root key
|
||
|
||
```
|
||
entropy 32 bytes from the OS CSPRNG (Custos makes it; tessera only takes it as a parameter)
|
||
mnemonic BIP39, English, 24 words = bip39::Mnemonic::from_entropy(entropy)
|
||
seed BIP39 seed of the mnemonic with the empty passphrase (PBKDF2-HMAC-SHA512, 2048 rounds, 64 bytes)
|
||
root Ed25519 secret key = HKDF-SHA256(salt = "tessera root v1", ikm = seed, info = "ed25519")[0..32]
|
||
```
|
||
|
||
- The same mnemonic gives the same root public key, every time (test vector in §9).
|
||
- **Order (decided):** entropy → mnemonic shown **once** → root imported into the vault → entropy, seed and
|
||
mnemonic dropped (zeroized). A key that was created non-extractable first could never be backed up, so the
|
||
code makes the other order impossible: Custos' `Enrollment` is a typestate (`begin → show → confirm →
|
||
import`), and `import` exists only on the type that `show` returns.
|
||
- `tessera::RootKey::from_mnemonic` exists for recovery (v1.1) and tests. It has no method that returns the
|
||
secret; it can only sign certs and report its public key.
|
||
|
||
## 4. Subkey certificate (`TSC1`)
|
||
|
||
```
|
||
"TSC1" 4
|
||
root 32 the root public key that signs this cert
|
||
subkey 32 the subkey's Ed25519 public key
|
||
app_id str8 id; "device" is reserved for device subkeys
|
||
purpose str8 id, e.g. "sync"; for device subkeys "device"
|
||
not_before 8 u64 Unix seconds
|
||
not_after 8 u64 Unix seconds, > not_before
|
||
sig 64 Ed25519(root, "tessera cert v1\0" ‖ every byte above)
|
||
```
|
||
|
||
`tbs` ("to be signed") is every byte before `sig`. The domain prefix means the root can only ever sign
|
||
something that parses as a cert: Custos' vault refuses a `tbs` that does not start with `TSC1` and name its
|
||
own root.
|
||
|
||
`verify(cert, root, now)` checks, in this order, and returns the first failure:
|
||
|
||
| Check | Error |
|
||
|:--|:--|
|
||
| `cert.root == root` | `WrongRoot` |
|
||
| signature valid (`verify_strict`) | `BadSignature` (tampered) |
|
||
| `now >= not_before` | `NotYetValid` |
|
||
| `now < not_after` | `Expired` |
|
||
|
||
Decoding is strict: wrong magic, a wrong length, an invalid id, `not_after <= not_before` or trailing bytes
|
||
are errors.
|
||
|
||
**Validity Custos issues:** `not_before = now − 5 min` (clock skew between devices), `not_after = now + 365 days`.
|
||
Renewal and revocation are v1.1.
|
||
|
||
## 5. Match code
|
||
|
||
Six digits the app and Custos both show, so the person approves the key the app actually holds:
|
||
|
||
```
|
||
match = u32_be(SHA-256("tessera match v1" ‖ subkey_pub)[0..4]) mod 1 000 000, zero-padded to 6 digits
|
||
```
|
||
|
||
The code is not secret. Test vector in §9.
|
||
|
||
## 6. Hybrid logical clock
|
||
|
||
`Hlc { wall: u64 ms, logical: u32 }`, ordered by `wall` then `logical`; encoded as 12 bytes.
|
||
|
||
- `Clock::tick(now)`: `wall' = max(last.wall, now)`; `logical' = last.logical + 1` if `wall' == last.wall`,
|
||
else 0.
|
||
- `Clock::observe(remote, now)`: refuses `remote.wall > now + max_drift` (`TooFarAhead`); then
|
||
`wall' = max(last.wall, remote.wall, now)` and `logical'` per the HLC paper (the larger logical of the
|
||
clocks that hold `wall'`, plus one).
|
||
- A logical counter that would overflow is an error, never a wrap.
|
||
|
||
## 7. Merge
|
||
|
||
`merge(log, batch, verify) -> (log', rejected)`:
|
||
|
||
- Each event has an **order key** `(hlc, author, hash)`, where `hash = SHA-256(encoded event)`.
|
||
- Every event in `batch` is verified with the caller's `verify`; a failure goes to `rejected` with its
|
||
reason and is never inserted (the app logs it).
|
||
- `log'` = the union of `log` and the verified events, de-duplicated by `hash`, sorted by order key.
|
||
- The log's bytes are `u32 length ‖ encoded event` for each event in order. Merging the same batches in any
|
||
order, any number of times, gives byte-identical logs (property test).
|
||
|
||
The events already in `log` are not verified again: they were verified when they arrived.
|
||
|
||
## 8. The event envelope (`TEV1`)
|
||
|
||
```
|
||
"TEV1" 4
|
||
then exactly these fields, in this order, each as tag (1) ‖ length (u32) ‖ value:
|
||
1 app id
|
||
2 schema u32 (4 bytes): the version of the payload's schema
|
||
3 hlc 12 bytes
|
||
4 author 32 bytes: the subkey public key
|
||
5 cert a TSC1 cert (§4)
|
||
6 payload 0–256 KiB: MLS ciphertext from day 2; opaque here
|
||
7 sig 64 bytes: Ed25519(author, "tessera event v1\0" ‖ every byte before field 7)
|
||
```
|
||
|
||
Decoding is strict and total: it returns a whole `Envelope` or a typed error with a reason (`BadMagic`,
|
||
`UnknownField`, `FieldOutOfOrder`, `BadLength`, `Invalid`, `TrailingBytes`, `TooLarge`, …). An unknown tag is
|
||
`UnknownField`, a known tag in the wrong place is `FieldOutOfOrder`. Encoding is canonical: decoding and
|
||
encoding again gives the same bytes. A decoded envelope is not yet trusted.
|
||
|
||
`Envelope::verify(roots, now, max_drift)` checks:
|
||
|
||
| Check | Error |
|
||
|:--|:--|
|
||
| `author == cert.subkey` | `AuthorNotCert` |
|
||
| `cert.app_id == app` (app boundaries) | `WrongApp` |
|
||
| `cert.root` is one of the trusted `roots` (the group's members) | `UnknownRoot` |
|
||
| the cert is valid at the event's time (`hlc.wall / 1000`), §4 | `Cert(…)` |
|
||
| `hlc.wall <= now + max_drift` | `FromTheFuture` |
|
||
| the signature is valid (`verify_strict`) | `BadSignature` |
|
||
|
||
The cert is checked at the event's own time, so a log stays verifiable after a cert expires and rebuilding
|
||
from it is deterministic. The cost: whoever steals a subkey can backdate events into its validity. Revocation
|
||
(v1.1) closes this; until then validity is one year.
|
||
|
||
## 9. Test vectors
|
||
|
||
| Input | Output |
|
||
|:--|:--|
|
||
| entropy `00` × 32 | mnemonic `abandon` × 23 + `art` |
|
||
| that mnemonic | root public key in `crates/tessera/tests/mandatory.rs` (`ROOT_PUB_ZERO`), also pinned in CustosPublic |
|
||
| subkey public key `07` × 32 | match code in `crates/tessera/tests/mandatory.rs` |
|
||
|
||
## 10. Asking Custos for a cert (`custos-local`, Windows)
|
||
|
||
Custos' local API is the named pipe `\\.\pipe\custos-domi` (Custos `docs/02_app.md`; only the current
|
||
Windows user can open it; Custos identifies the caller by its executable). Day 1 adds one operation:
|
||
|
||
```
|
||
→ {"op":"subkey","app":"Fiscus Domi","app_id":"fiscus","purpose":"sync","subkey":"<base64url 32 bytes>"}
|
||
← {"ok":true,"cert":"<base64url TSC1>","match":"123456"}
|
||
← {"ok":false,"error":"denied"|"locked"|"no_identity"|"bad_request","message":"…"}
|
||
```
|
||
|
||
1. The app makes its subkey itself (`Subkey::from_seed` with 32 bytes from the OS CSPRNG) and keeps the seed
|
||
in Custos (`set domi://<app_id>/tessera-subkey`), never in a file.
|
||
2. It shows the match code of its subkey and sends the request.
|
||
3. Custos shows the app (program path and signer), the app id, the purpose and the match code. One click
|
||
approves. Custos signs the cert with the root, records it (Domi identity → apps), and answers.
|
||
|
||
No answer ever contains private key material: only the cert (public keys and a signature).
|
||
|
||
## 11. Not in v1
|
||
|
||
Revocation, renewal, recovery UI (v1.1), MLS and the relay (day 2), WASM (v1.1).
|
||
|
||
## 12. v1.1: revocation and the backup secret (docs/V1_1.md)
|
||
|
||
### Revocation (`TRV1`)
|
||
```
|
||
"TRV1" 4
|
||
root 32 the root that withdraws the subkey
|
||
subkey 32 the revoked subkey
|
||
revoked_at 8 u64 Unix seconds
|
||
part 2 u16, this part's index
|
||
parts 2 u16, how many parts the statement has (1..256)
|
||
count 4 u32, hashes in this part (0..7000)
|
||
hashes count×32 SHA-256 of each event by the subkey that stays valid: sorted, unique
|
||
sig 64 Ed25519(root, "tessera revoke v1\0" ‖ every byte above)
|
||
```
|
||
- The root signs it through `RevokeSigner::sign_revocation_tbs`, in its own domain. A signer refuses a `tbs`
|
||
that does not start `TRV1 ‖ its own root`, and the cert signer refuses a `TRV1`, so neither statement can
|
||
pass for the other.
|
||
- Decoding is strict: wrong magic, a part outside its count, unsorted or repeated hashes, a short field or
|
||
trailing bytes are errors.
|
||
- **`RevocationSet`** applies a group's statements:
|
||
- A statement counts once all its parts are in (parts add up).
|
||
- An event by a revoked subkey stays valid only if every complete statement for that subkey lists it, so a
|
||
later statement never widens what is kept.
|
||
- The answer does not depend on the order statements arrive in.
|
||
- Statements are accepted only from a root of the group.
|
||
|
||
### The backup secret
|
||
```
|
||
backup secret = HKDF-SHA256(salt "tessera backup v1", ikm = root secret (the Ed25519 seed, §3), info = app id)
|
||
seal key = HKDF-Expand(backup secret, "seal") XChaCha20-Poly1305: nonce (24) ‖ ciphertext
|
||
mailbox seed = HKDF-Expand(backup secret, "mailbox") the backup mailbox's Tabellarius access key
|
||
```
|
||
- Custos derives it (the root never leaves Custos) and hands an app only its own.
|
||
- `BackupSecret` has no method that returns the secret: it seals, opens, and gives the mailbox seed. That seed
|
||
is a relay credential of the kind invite tokens carry, the one exception in `tests/surface.rs`.
|
||
|
||
**Test vector** (the zero-entropy mnemonic of §9, app `fiscus`):
|
||
- mailbox seed: `IygCOrh-ibO6-KY5AXQ9Prl2SyPWQIyZZ-anffcbMCU`
|
||
- `seal(nonce = 24 zero bytes, aad = "", plain = "")`: `AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAci_JRbtsx5prR83KT9Y7LA`
|
||
|
||
### Custos' part (`custos-local`, three more pipe operations)
|
||
|
||
The person starts a revocation **in Custos** (Domi identity → a key → Revoke). Custos cannot call an app, so
|
||
each app asks, on every sync, whether there is anything to do:
|
||
|
||
```
|
||
→ {"op":"revocations","app":"Fiscus Domi","app_id":"fiscus"}
|
||
← {"ok":true,"revocations":[{"subkey":"<base64url>","revoked_at":1793000000,"statements":["<base64url TRV1>",…]|null}]}
|
||
|
||
→ {"op":"revoke","app":"Fiscus Domi","app_id":"fiscus","subkey":"<base64url>","revoked_at":1793000000,
|
||
"accepted":"<base64url: the kept events' SHA-256, 32 bytes each, concatenated>"}
|
||
← {"ok":true,"statements":["<base64url TRV1>",…]}
|
||
|
||
→ {"op":"backup_key","app":"Fiscus Domi","app_id":"fiscus"}
|
||
← {"ok":true,"secret":"<base64url, 32 bytes>"}
|
||
```
|
||
|
||
- **All three need the app's namespace grant** (`domi://<app_id>/…`, the one its subkey seed lives in), so an
|
||
app reaches only its own revocations and its own backup secret.
|
||
- **`revocations`** lists the revocations the person started for this app's subkeys and for device keys.
|
||
`statements` is `null` until a device of this app sent its list.
|
||
- **`revoke`** signs only a revocation the person started, at the `revoked_at` Custos chose (otherwise
|
||
`not_found`), and only once: a second call gets the statements already signed. The app sends the hashes of
|
||
every event by that subkey in all its groups (`Client::events_by_all`).
|
||
- **An app that does not answer:** Custos shows the revocation as waiting. A second, explicit click signs it
|
||
with an empty list, which throws away every event that key wrote (docs/V1_1.md decision 2). A device key
|
||
(`app_id` `device`) is signed at once with an empty list: in v1.1a no app log holds events by one.
|
||
- With the statements in hand the app calls `Client::apply_revocation`: in every group the key was in, it
|
||
announces them, removes the key's leaves and takes their mailbox keys off the relay. It skips a group that
|
||
already has them, so calling it on every sync is safe.
|
||
- **`backup_key`** gives the app its backup secret (above). The app keeps it as `BackupSecret` and calls
|
||
`Client::backup` after each sync and `Client::restore` on a new device.
|
||
- **Limits:** one pipe message is at most 1 MiB, so one `revoke` carries up to 24,000 kept events.
|
||
|
||
Custos' vault signs `TRV1` only for a key the person marked revoked, at that time (CustosPublic
|
||
docs/IDENTITY.md), so an app with the namespace grant can choose which events stay, not which key is
|
||
revoked.
|