diff --git a/.agents/features/client.md b/.agents/features/client.md new file mode 100644 index 0000000..fdfefb1 --- /dev/null +++ b/.agents/features/client.md @@ -0,0 +1,99 @@ +# F21 — The client: MLS 1:1, a mailbox per contact, invites + +Analysis, 2026-11-14. Issues #3 (OpenMLS check) and #6 (client). It builds on F20: the relay and +`PROTOCOL.md` §8. + +## 1. Why and what +Chat must be end-to-end encrypted with MLS, and the relay must learn nothing readable. The client +library is the one place where that happens. Amicitia embeds it in F22, and any other app could too. + +After F21, two clients in one test can: +1. pair through an invite; +2. learn each other's mailbox and post token *inside* MLS; +3. exchange text both ways through a real relay. + +A canary string in a message, and both display names, never appear in the relay's database. + +### The OpenMLS check (#3), measured 2026-11-14 +| Question | Answer | +|:--|:--| +| Version | `openmls` 0.9.2, `openmls_rust_crypto` / `_basic_credential` / `_traits` 0.6.0; MSRV 1.91 (we pin 1.99) | +| Licence | OpenMLS crates **MIT**. The whole tree is MIT, Apache-2.0 or BSD, except **`hpke-rs` 0.7 (MPL-2.0)**, OpenMLS's HPKE. MPL-2.0 is file-level copyleft: an app that ships it unmodified must say where its source is. That goes into Amicitia's licence notices (F22) | +| C code | none: no `cc`, `cmake` or `-sys` crate in the tree. The crypto is RustCrypto (pure Rust) | +| Android | `cargo build -p tabellarius-client` passes for `aarch64-linux-android`, `x86_64-linux-android` and `armv7-linux-androideabi`. The client's tests also *run* on the x86_64 emulator (§5) | + +## 2. Architecture +```mermaid +sequenceDiagram + participant A as Inviter A + participant R as Relay + participant B as Invitee B + A->>R: POST /v0/mailboxes Mw (id and post token derived from the secret) + A->>R: POST /v0/invites {id, hash(secret), KeyPackage(A)} + A-->>B: link: relay, invite id, secret (QR or link, outside the relay) + B->>R: GET /v0/invites/{id} (secret) → KeyPackage(A), invite deleted + B->>B: create group G, add A → Welcome + B->>R: POST Mw: Welcome; then an MLS message {hello: mailbox M_B, post token P_B, name} + A->>R: GET Mw → join G, read hello + A->>R: POST M_B: MLS {hello: mailbox M_A, post token P_A, name} + B->>R: GET M_B → read hello + Note over A,B: A posts to M_B, B posts to M_A. One MLS group per pair. +``` +- **Identity:** an Ed25519 key made on the device, behind a `BasicCredential` that carries 16 + random bytes, not a name. The display name travels only inside MLS (`PROTOCOL.md` §2). +- **Ciphersuite:** `MLS_128_DHKEMX25519_AES128GCM_SHA256_Ed25519`, RFC 9420's mandatory one. +- **The welcome mailbox, Mw.** The link carries three things (§4): the relay, the invite id and the + secret. The invitee's first reply needs somewhere to go, so its id and post token are **derived + from the invite secret** with HKDF-SHA256 (crate `hkdf`, labels `tabellarius v0 welcome mailbox` + and `… post`). The inviter creates Mw and keeps a random fetch token for it. To the relay it is an + ordinary mailbox, so §5 and §8 are unchanged. +- **The invitee creates the group.** It holds the inviter's KeyPackage, so it can add the inviter at + once and send the Welcome. §4 says the invitee *replies with their own* KeyPackage. That is not + needed when the invitee creates the group, so §4's text changes (§4 below). +- **Payloads inside MLS** are JSON: `{"kind":"hello","mailbox","postToken","name"}` and + `{"kind":"text","body"}`. The relay sees only MLS ciphertext. +- **Blob framing:** each blob is one TLS-serialised `MlsMessageOut` (a Welcome, or a private + message). +- **Transport is a trait.** `Transport` covers create mailbox, post, fetch, acknowledge, create + invite and claim invite. The library makes no network calls itself. F22 gives it an HTTP + implementation. The tests use the real relay over loopback, plus an in-memory double for the run + on Android. +- **Poll:** fetch every own mailbox, process blobs in order, and acknowledge each one processed + (or refused as not MLS). Each call returns events: `Joined {contact, name}`, + `Message {contact, text}`. + +## 3. Risks +- **Privacy:** + - The relay sees: mailbox ids, token hashes, ciphertext sizes and hours, and the KeyPackage (A's + public key and the random credential id). + - It does not see: names, text, or which two mailboxes belong to one pair. + - The welcome mailbox's id is derived from the secret. Whoever has the link can post there, which + is exactly the invite's capability. Mw is used once, but a mailbox cannot be deleted in v0 (F20 + §4), so it stays empty. +- **Crypto:** none written here. MLS, HKDF and SHA-256 come from OpenMLS, `hkdf` and `sha2`. +- **State:** in memory in F21. Keeping MLS state across restarts, in Amicitia's encrypted `mls` + table, is F22. +- **A stolen link:** whoever opens it first becomes the contact. The inviter sees the joiner's name + and can block. Safety footprints (F26) are the real answer, and the link is meant to be shared in + person or privately. + +## 4. Gaps and designed solutions +| Gap | Solution | +|:--|:--| +| §4 says the invitee *replies with their own KeyPackage*, and the reply channel (Mw) is not described | Change §4 to this flow: the invitee creates the group; the first reply goes to a mailbox derived from the secret. **Needs Mark's approval** before merging (CLAUDE.md §3) | +| RFC 9420 test vectors | OpenMLS runs them in its own CI. Here, interop is tested between two of our clients, plus OpenMLS's validation of every message | +| State across restarts | F22 (an `mls` table, an SQLite storage provider) | +| Deleting the used welcome mailbox | protocol v1 (F20 §4) | +| Block, delivery state | F24 | +| Several devices per person | Phase 5 (G04) | + +## 5. Verification matrix +| Case | Kind | Test | +|:--|:--|:--| +| Two clients pair through an invite and exchange text both ways, through the real relay over loopback | integration | `client` `pair_and_talk_through_a_real_relay` | +| A canary in a message, and both names, never appear in the relay's database file | integration | `client` `the_relay_never_sees_a_name_or_a_word` | +| A link opened twice: the second claim fails, and nobody else joins | integration | `client` | +| A blob that is not MLS, or not for this group, is acknowledged and skipped, never shown | integration | `client` | +| The welcome mailbox derivation is fixed (a test vector) | unit | `client` | +| Pairing and talking run **on the Android emulator** (an in-memory transport) | device | `cargo test --target x86_64-linux-android` binary run with `adb` | +| The client does not depend on the relay (except in its tests) | CI | `scripts/check-deps.sh` (normal edges only) | diff --git a/CLAUDE.md b/CLAUDE.md index b0aaa2e..35fb1ab 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -28,9 +28,13 @@ deploy/container/ # container image ## 3. Workflow Same as Amicitia: analysis document in `.agents/features/` before code ((D)TDD), branch per feature, -adversarial self-review in `.agents/reviews/`, merge `--no-ff`, push and verify the remote hash. The -agent merges its own PR once local checks pass and the review is written, and reports afterwards -(Mark, 2026-11-08); a change to `docs/PROTOCOL.md` still needs Mark's approval first. +PR. **The adversarial self-review is posted in the PR** as a Gitea review: the evidence (test counts +per crate, planted bugs) in its body and a comment on each line a finding is about. The PR is the +record; `.agents/reviews/` holds the reviews from before 2026-11-14 only. +**The agent merges every PR itself**, a change to `docs/PROTOCOL.md` included, once local checks pass +and the review is posted: merge `--no-ff`, push, verify the remote hash, say in the merge commit which +PR holds the review, and report to Mark afterwards (Mark, 2026-11-14). A protocol change still says in +`docs/PROTOCOL.md` what changed, when and why. **No AI attribution in commits, ever.** Quote test counts per crate; an exit code is not a count. ## 4. Known traps diff --git a/Cargo.lock b/Cargo.lock index a4ca4ca..240afa6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,50 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "aead" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0" +dependencies = [ + "crypto-common 0.1.7", + "generic-array", +] + +[[package]] +name = "aes" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures 0.2.17", +] + +[[package]] +name = "aes-gcm" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "831010a0f742e1209b3bcea8fab6a8e149051ba6099432c8cb2cc117dec3ead1" +dependencies = [ + "aead", + "aes", + "cipher", + "ctr", + "ghash", + "subtle", +] + +[[package]] +name = "aho-corasick" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba" +dependencies = [ + "memchr", +] + [[package]] name = "atomic-waker" version = "1.1.2" @@ -62,12 +106,44 @@ dependencies = [ "tower-service", ] +[[package]] +name = "base16ct" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c7f02d4ea65f2c1853089ffd8d2787bdbc63de2f0d29dedbcf8ccdfa0ccd4cf" + [[package]] name = "base64" version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" +[[package]] +name = "base64ct" +version = "1.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06" + +[[package]] +name = "bindgen" +version = "0.72.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "993776b509cfb49c750f11b8f07a46fa23e0a1386ffc01fb1e7d343efc387895" +dependencies = [ + "bitflags", + "cexpr", + "clang-sys", + "itertools", + "log", + "prettyplease", + "proc-macro2", + "quote", + "regex", + "rustc-hash", + "shlex 1.3.0", + "syn 2.0.119", +] + [[package]] name = "bit-set" version = "0.11.1" @@ -102,6 +178,15 @@ dependencies = [ "generic-array", ] +[[package]] +name = "block-buffer" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2f6c7dbe95a6ed67ad9f18e57daf93a2f034c524b99fd2b76d18fdfeb6660aa" +dependencies = [ + "hybrid-array", +] + [[package]] name = "borsh" version = "1.8.1" @@ -123,9 +208,15 @@ dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn", + "syn 3.0.10", ] +[[package]] +name = "bumpalo" +version = "3.20.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87a0cf66fd1eb2fb7b822cb78370d82303750809bc6ba855f3f58e517d3238db" + [[package]] name = "bytes" version = "1.12.1" @@ -139,7 +230,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ff2a8126fff0f8c839080d619675a585e61720bb12d2baceae45769fd66f155" dependencies = [ "find-msvc-tools", - "shlex", + "shlex 2.0.1", +] + +[[package]] +name = "cexpr" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" +dependencies = [ + "nom", ] [[package]] @@ -154,6 +254,17 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f079e83a288787bcd14a6aea84cee5c87a67c5a3e660c30f557a3d24761b3527" +[[package]] +name = "chacha20" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3613f74bd2eac03dad61bd53dbe620703d4371614fe0bc3b9f04dd36fe4e818" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures 0.2.17", +] + [[package]] name = "chacha20" version = "0.10.2" @@ -162,7 +273,71 @@ checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06" dependencies = [ "cfg-if", "cpufeatures 0.3.1", - "rand_core", + "rand_core 0.10.1", +] + +[[package]] +name = "chacha20poly1305" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10cd79432192d1c0f4e1a0fef9527696cc039165d729fb41b3f4f4f354c2dc35" +dependencies = [ + "aead", + "chacha20 0.9.1", + "cipher", + "poly1305", + "zeroize", +] + +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common 0.1.7", + "inout", + "zeroize", +] + +[[package]] +name = "clang-sys" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "157a8ba7b480713b56f4c09fd13fc3e0a22a5dfab8097ba61cbc5feef950788a" +dependencies = [ + "glob", + "libc", + "libloading", +] + +[[package]] +name = "cmov" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c9ea0ac24bc397ab3c98583a3c9ba74fa56b09a4449bbe172b9b1ddb016027a" + +[[package]] +name = "const-oid" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" + +[[package]] +name = "const-oid" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be95d4a884358a21a199689323c29e9256b4a1b315e22f183d155422041d3a9a" + +[[package]] +name = "core-models" +version = "0.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee3344d349528f449816cfa85e558c439bdca5a6d8a738cfb21e69f0b2b1952f" +dependencies = [ + "hax-lib", + "pastey", + "rand", ] [[package]] @@ -189,6 +364,54 @@ dependencies = [ "libc", ] +[[package]] +name = "crabgrind" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc7459e07732f6de74001fcf73a3fdfbb0f368e4fd8e2392f4a2206a065e6e95" +dependencies = [ + "bindgen", + "cc", + "pkg-config", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "622f3fc73690be383c7214310406f28a90e6edeadc3cea882f9d71e495b9711a" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc74980687109a3b14c72fd458107bf0baa1da1a1a805e178d15501ba9b86d9d" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6" + +[[package]] +name = "crypto-bigint" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dc92fb57ca44df6db8059111ab3af99a63d5d0f8375d9972e319a379c6bab76" +dependencies = [ + "generic-array", + "rand_core 0.6.4", + "subtle", + "zeroize", +] + [[package]] name = "crypto-common" version = "0.1.7" @@ -196,17 +419,189 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" dependencies = [ "generic-array", + "rand_core 0.6.4", "typenum", ] +[[package]] +name = "crypto-common" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453" +dependencies = [ + "getrandom 0.4.3", + "hybrid-array", + "rand_core 0.10.1", +] + +[[package]] +name = "ctr" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0369ee1ad671834580515889b80f2ea915f23b8be8d0daa4bbaf2ac5c7590835" +dependencies = [ + "cipher", +] + +[[package]] +name = "ctutils" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "261af4dc7a06ea8a527a8717d8cbba3417530d6ea14e1edb3de6b67806be474f" +dependencies = [ + "cmov", +] + +[[package]] +name = "curve25519-dalek" +version = "4.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97fb8b7c4503de7d6ae7b42ab72a5a59857b4c937ec27a3d4539dba95b5ab2be" +dependencies = [ + "cfg-if", + "cpufeatures 0.2.17", + "curve25519-dalek-derive", + "digest 0.10.7", + "fiat-crypto 0.2.9", + "rustc_version", + "subtle", + "zeroize", +] + +[[package]] +name = "curve25519-dalek" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5eed333089e2e1c1ac8c6c0398e5e2497b4c9926ca6d0365ed1e099afa5bc23" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.1", + "curve25519-dalek-derive", + "fiat-crypto 0.3.0", + "rustc_version", + "subtle", +] + +[[package]] +name = "curve25519-dalek-derive" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "der" +version = "0.7.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7c1832837b905bbfb5101e07cc24c8deddf52f93225eee6ead5f4d63d53ddcb" +dependencies = [ + "const-oid 0.9.6", + "pem-rfc7468", + "zeroize", +] + +[[package]] +name = "der" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a996a108feb8b427a6e12fcf6af4aa83409b79d72c15337d7741368131ee004" +dependencies = [ + "const-oid 0.10.3", + "zeroize", +] + [[package]] name = "digest" version = "0.10.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ - "block-buffer", - "crypto-common", + "block-buffer 0.10.4", + "const-oid 0.9.6", + "crypto-common 0.1.7", + "subtle", +] + +[[package]] +name = "digest" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2" +dependencies = [ + "block-buffer 0.12.1", + "const-oid 0.10.3", + "crypto-common 0.2.2", + "ctutils", +] + +[[package]] +name = "ecdsa" +version = "0.16.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee27f32b5c5292967d2d4a9d7f1e0b0aed2c15daded5a60300e4abb9d8020bca" +dependencies = [ + "der 0.7.10", + "digest 0.10.7", + "elliptic-curve", + "rfc6979", + "signature 2.2.0", + "spki 0.7.3", +] + +[[package]] +name = "ed25519" +version = "2.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "115531babc129696a58c64a4fef0a8bf9e9698629fb97e9e40767d235cfbcd53" +dependencies = [ + "pkcs8 0.10.2", + "signature 2.2.0", +] + +[[package]] +name = "ed25519-dalek" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70e796c081cee67dc755e1a36a0a172b897fab85fc3f6bc48307991f64e4eca9" +dependencies = [ + "curve25519-dalek 4.1.3", + "ed25519", + "rand_core 0.6.4", + "serde", + "sha2 0.10.9", + "subtle", + "zeroize", +] + +[[package]] +name = "either" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e9c71c2167ca323c882b99918929403426e2373ea17242ff5653e0d5e1058be" + +[[package]] +name = "elliptic-curve" +version = "0.13.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5e6043086bf7973472e0c7dff2142ea0b680d30e18d9cc40f267efbf222bd47" +dependencies = [ + "base16ct", + "crypto-bigint", + "digest 0.10.7", + "ff", + "generic-array", + "group", + "hkdf 0.12.4", + "pem-rfc7468", + "pkcs8 0.10.2", + "rand_core 0.6.4", + "sec1", + "subtle", + "zeroize", ] [[package]] @@ -243,6 +638,28 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223" +[[package]] +name = "ff" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0b50bfb653653f9ca9095b427bed08ab8d75a137839d9ad64eb11810d5b6393" +dependencies = [ + "rand_core 0.6.4", + "subtle", +] + +[[package]] +name = "fiat-crypto" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28dea519a9695b9977216879a3ebfddf92f1c08c05d984f8996aecd6ecdc811d" + +[[package]] +name = "fiat-crypto" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64cd1e32ddd350061ae6edb1b082d7c54915b5c672c389143b9a63403a109f24" + [[package]] name = "find-msvc-tools" version = "0.1.15" @@ -293,6 +710,18 @@ checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" dependencies = [ "typenum", "version_check", + "zeroize", +] + +[[package]] +name = "getrandom" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" +dependencies = [ + "cfg-if", + "libc", + "wasi", ] [[package]] @@ -316,7 +745,34 @@ dependencies = [ "cfg-if", "libc", "r-efi 6.0.0", - "rand_core", + "rand_core 0.10.1", +] + +[[package]] +name = "ghash" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0d8a4362ccb29cb0b265253fb0a2728f592895ee6854fd9bc13f2ffda266ff1" +dependencies = [ + "opaque-debug", + "polyval", +] + +[[package]] +name = "glob" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4eba85ea1d0a966a983acd07deee566e67395d2d96b6fb39e62b5a833f1eb0b" + +[[package]] +name = "group" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0f9ef7462f7c099f518d754361858f86d8a07af53ba9af0fe635bbccb151a63" +dependencies = [ + "ff", + "rand_core 0.6.4", + "subtle", ] [[package]] @@ -337,6 +793,148 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "hax-lib" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01a33cb227f97ee5c419064c31d7c55a7d895f28d8019ccdadc311cc036500c4" +dependencies = [ + "hax-lib-macros", + "num-bigint", + "num-traits", +] + +[[package]] +name = "hax-lib-macros" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28da835a5f153e9f3d0cc1f42ee605a1cfa9093c93348193793d60a1b0908b51" +dependencies = [ + "hax-lib-macros-types", + "proc-macro-error2", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "hax-lib-macros-types" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eed327e9a28d6ee018a4a9ba842d4ee27e378c8a591d2beb6f42f32b24a02fcb" +dependencies = [ + "proc-macro2", + "quote", + "serde", + "serde_json", + "uuid", +] + +[[package]] +name = "hkdf" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b5f8eb2ad728638ea2c7d47a21db23b7b58a72ed6a38256b8a1849f15fbbdf7" +dependencies = [ + "hmac 0.12.1", +] + +[[package]] +name = "hkdf" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4aaa26c720c68b866f2c96ef5c1264b3e6f473fe5d4ce61cd44bbe913e553018" +dependencies = [ + "hmac 0.13.0", +] + +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest 0.10.7", +] + +[[package]] +name = "hmac" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6303bc9732ae41b04cb554b844a762b4115a61bfaa81e3e83050991eeb56863f" +dependencies = [ + "digest 0.11.3", +] + +[[package]] +name = "hpke-rs" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "812de62ab573b876c6c40d7553bae9402ae3bad95b64af06f964388eecb9b7b1" +dependencies = [ + "hpke-rs-crypto", + "hpke-rs-libcrux", + "hpke-rs-rust-crypto", + "libcrux-sha3", + "log", + "serde", + "subtle", + "tls_codec", + "zeroize", +] + +[[package]] +name = "hpke-rs-crypto" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2c461da2e0c9c93d875b597f0c78214fb0d2d5c57834b2ef2a2fa057fa20e24" +dependencies = [ + "rand", + "zeroize", +] + +[[package]] +name = "hpke-rs-libcrux" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c1ace95ef11fbbd84527eada5fc1304f66720fc4c008db30d888d62d3c52613" +dependencies = [ + "hpke-rs-crypto", + "libcrux-aead", + "libcrux-ecdh", + "libcrux-hkdf", + "libcrux-kem", + "libcrux-traits", + "rand", + "rand_chacha 0.10.0", + "rand_core 0.10.1", + "zeroize", +] + +[[package]] +name = "hpke-rs-rust-crypto" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a606ac19851da841862ef5f39d26d6227bfcfb4047417801a66c1f6e6652d71" +dependencies = [ + "aes-gcm", + "chacha20poly1305", + "hkdf 0.13.0", + "hpke-rs-crypto", + "k256", + "ml-kem", + "p256", + "p384", + "rand", + "rand_chacha 0.10.0", + "rand_core 0.10.1", + "sha2 0.11.1", + "subtle", + "x-wing", + "x25519-dalek 2.0.1", + "zeroize", +] + [[package]] name = "http" version = "1.5.0" @@ -382,6 +980,16 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" +[[package]] +name = "hybrid-array" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27f864f10dfb56725ce5ce5472bc52252c8f93a4ab86327122cebf62c5f59a17" +dependencies = [ + "ctutils", + "typenum", +] + [[package]] name = "hyper" version = "1.12.0" @@ -426,18 +1034,306 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "inout" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01" +dependencies = [ + "generic-array", +] + +[[package]] +name = "itertools" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3bd1574e617a9a6c1f8f8181bd7e841e1f20e4277607ef28945765188d6284d4" +[[package]] +name = "js-sys" +version = "0.3.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8dc09d2154098bbf4ef54eab6b117a3c24cbbffe94046b16ee6d6d4510584d3a" +dependencies = [ + "cfg-if", + "futures-util", + "wasm-bindgen", +] + +[[package]] +name = "k256" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6e3919bbaa2945715f0bb6d3934a173d1e9a59ac23767fbaaef277265a7411b" +dependencies = [ + "cfg-if", + "elliptic-curve", +] + +[[package]] +name = "keccak" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8f198d1db720e4940b5a493201d199d9f24f568f8f746bd13706243a2f71598" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.1", +] + +[[package]] +name = "kem" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01737161ba802849cfd486b5bd209d38ba4943494c249a8126005170c7621edd" +dependencies = [ + "crypto-common 0.2.2", + "rand_core 0.10.1", +] + [[package]] name = "libc" version = "0.2.191" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" +[[package]] +name = "libcrux-aead" +version = "0.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22acbe68be84b7b41aaba6e39b87036fc4324dee628d94773750bf3d7e906d69" +dependencies = [ + "libcrux-aes", + "libcrux-chacha20poly1305", + "libcrux-secrets", + "libcrux-traits", +] + +[[package]] +name = "libcrux-aes" +version = "0.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57cc95b11dbc797b1e169467b1fc4205c4e6ee2ce516a035ad7342ce5f6ae971" +dependencies = [ + "libcrux-intrinsics", + "libcrux-platform", + "libcrux-secrets", + "libcrux-traits", +] + +[[package]] +name = "libcrux-chacha20poly1305" +version = "0.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "537e6eee5cdc9a980014d058784b0be09614f0596df789b114f7833aa9f35d75" +dependencies = [ + "libcrux-hacl-rs", + "libcrux-macros", + "libcrux-poly1305", + "libcrux-secrets", + "libcrux-traits", +] + +[[package]] +name = "libcrux-curve25519" +version = "0.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4f3cfd5e31cb9745e290ee061222c380ec42884654b09fc7d12a5b0ed63e028" +dependencies = [ + "libcrux-hacl-rs", + "libcrux-macros", + "libcrux-secrets", + "libcrux-traits", +] + +[[package]] +name = "libcrux-ecdh" +version = "0.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a227590b89ff55ce7cd97b5a7468c82d231db8f3b890bb4d4fb6d271e7524d7" +dependencies = [ + "libcrux-curve25519", + "libcrux-p256", + "rand", + "tls_codec", +] + +[[package]] +name = "libcrux-hacl-rs" +version = "0.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66106db376ae249af86911aee048cf73ea1f394d6653026fc988dd22cf2a4ace" +dependencies = [ + "libcrux-macros", +] + +[[package]] +name = "libcrux-hkdf" +version = "0.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc94e651dca4d47edbcdd88bb2428ce16a2bd86b7a4e7170da87b505478f9839" +dependencies = [ + "libcrux-hacl-rs", + "libcrux-hmac", + "libcrux-secrets", +] + +[[package]] +name = "libcrux-hmac" +version = "0.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "500ad9b32c715161b594172ca1fb11503a1c033b393ff4c0c33505ce51c1943c" +dependencies = [ + "libcrux-hacl-rs", + "libcrux-macros", + "libcrux-sha2", + "libcrux-traits", +] + +[[package]] +name = "libcrux-intrinsics" +version = "0.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98a0c574d4eb81d0814bc2b91e4a433d7e0b35851b1d62eb5dd95c064f1f76d0" +dependencies = [ + "core-models", + "hax-lib", +] + +[[package]] +name = "libcrux-kem" +version = "0.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "541a7377fb35060892e0620982e224e47419f10da8c212453bf642dafe529691" +dependencies = [ + "libcrux-curve25519", + "libcrux-ecdh", + "libcrux-ml-kem", + "libcrux-p256", + "libcrux-sha3", + "libcrux-traits", + "rand", +] + +[[package]] +name = "libcrux-macros" +version = "0.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffd6aa2dcd5be681662001b81d493f1569c6d49a32361f470b0c955465cd0338" +dependencies = [ + "quote", + "syn 2.0.119", +] + +[[package]] +name = "libcrux-ml-kem" +version = "0.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d8160f7d64fd2716b4fd05cc886a042f8dcda18d9206c0d506e2c67bdf97daa" +dependencies = [ + "hax-lib", + "libcrux-intrinsics", + "libcrux-platform", + "libcrux-secrets", + "libcrux-sha3", + "libcrux-traits", + "rand", + "tls_codec", +] + +[[package]] +name = "libcrux-p256" +version = "0.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3400732702d578be622257b98cec85e9b0cd34a67f1f06d3ebe9ae34963cdf02" +dependencies = [ + "libcrux-hacl-rs", + "libcrux-macros", + "libcrux-secrets", + "libcrux-sha2", + "libcrux-traits", +] + +[[package]] +name = "libcrux-platform" +version = "0.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d9e21d7ed31a92ac539bd69a8c970b183ee883872d2d19ce27036e24cb8ecc4" +dependencies = [ + "libc", +] + +[[package]] +name = "libcrux-poly1305" +version = "0.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76a9144949845813a0b8787d08cbba47baabe59c83f3043e558e6e92385c40cc" +dependencies = [ + "libcrux-hacl-rs", + "libcrux-macros", +] + +[[package]] +name = "libcrux-secrets" +version = "0.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79054fc9037cb70d6a546cf094cea7d7df06af5e49d230ba24103d27ccc886f1" +dependencies = [ + "crabgrind", + "hax-lib", +] + +[[package]] +name = "libcrux-sha2" +version = "0.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "960e46e1be0b77098cc6ce82137864936ecad3a1b9fd4303dd51202ca140dd1e" +dependencies = [ + "libcrux-hacl-rs", + "libcrux-macros", + "libcrux-traits", +] + +[[package]] +name = "libcrux-sha3" +version = "0.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c09f5c39afae0528e1f70a3c1e3c6ee649ec1f19dda26fc9b6ea91108fb879ef" +dependencies = [ + "hax-lib", + "libcrux-intrinsics", + "libcrux-platform", + "libcrux-traits", +] + +[[package]] +name = "libcrux-traits" +version = "0.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fa7a21e8c2e8baa8b40f0b176740f2ca4baadd79d1b00e48d6b1e363c42085a" +dependencies = [ + "libcrux-secrets", + "rand", +] + +[[package]] +name = "libloading" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55" +dependencies = [ + "cfg-if", + "windows-link", +] + [[package]] name = "libsqlite3-sys" version = "0.39.1" @@ -455,6 +1351,12 @@ version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" +[[package]] +name = "log" +version = "0.4.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d15f86d12da44cd8d4ec3c73984537eb7f4cadcb9776a575a0a8a05ca11005c" + [[package]] name = "matchit" version = "0.8.4" @@ -473,6 +1375,12 @@ version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d5848f5a9e8117cf94f2f90faac24a87ff81600ff8628c1e3deaab572e76bfa1" +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "mio" version = "1.2.4" @@ -484,6 +1392,76 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "ml-dsa" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f6d49c3f3f0e10e18ca975ffda66f8da0ec36521c1d911adcbc521bf0743f5c" +dependencies = [ + "const-oid 0.10.3", + "crypto-common 0.2.2", + "ctutils", + "hybrid-array", + "module-lattice", + "pkcs8 0.11.0", + "shake", + "signature 3.0.0", +] + +[[package]] +name = "ml-kem" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84ca78b66f46050d49fa21693ae8adab2081d8101b65d8067316b6b70b64a0d1" +dependencies = [ + "hybrid-array", + "kem", + "module-lattice", + "rand_core 0.10.1", + "sha3", + "shake", +] + +[[package]] +name = "module-lattice" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4a1e412c8ee83a1c678f427806219c49c96b2511b9f07497514f0e43fb26155" +dependencies = [ + "ctutils", + "hybrid-array", + "num-traits", +] + +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + +[[package]] +name = "num-bigint" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c89e69e7e0f03bea5ef08013795c25018e101932225a656383bd384495ecc367" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ce2d95d4b3734dc35aa2f45e1aa22cd416814592a4f9d9205e11affd5b8e10b" +dependencies = [ + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -499,6 +1477,147 @@ version = "1.21.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" +[[package]] +name = "opaque-debug" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" + +[[package]] +name = "openmls" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce4173f914cbc2dd28d9499f19e92f3e1bfebd9eea1607889924a77ca63d3ffc" +dependencies = [ + "log", + "openmls_serialization_helpers", + "openmls_traits", + "rayon", + "serde", + "serde_bytes", + "thiserror", + "tls_codec", + "zeroize", +] + +[[package]] +name = "openmls_basic_credential" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbd3f0c3422e7c7a8496f042b547b0c28d0793f8ba97feb401966e58196a1e40" +dependencies = [ + "ed25519-dalek", + "ml-dsa", + "openmls_traits", + "p256", + "p384", + "rand_core 0.6.4", + "serde", + "tls_codec", + "zeroize", +] + +[[package]] +name = "openmls_memory_storage" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce927945a16eaa19302ed5ccef052f01ba68f2ba7ae67e4ac4ff2fc661445364" +dependencies = [ + "log", + "openmls_traits", + "serde", + "serde_json", + "thiserror", +] + +[[package]] +name = "openmls_rust_crypto" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36bf3fe824ead6e81d22f49a9cd7feffca3494979fac14c1248e19516e3625f1" +dependencies = [ + "aes-gcm", + "chacha20poly1305", + "ed25519-dalek", + "hkdf 0.13.0", + "hmac 0.13.0", + "hpke-rs", + "hpke-rs-crypto", + "hpke-rs-rust-crypto", + "ml-dsa", + "openmls_memory_storage", + "openmls_traits", + "p256", + "p384", + "rand_chacha 0.3.1", + "rand_core 0.10.1", + "rand_core 0.6.4", + "sha2 0.11.1", + "thiserror", + "tls_codec", +] + +[[package]] +name = "openmls_serialization_helpers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e2cf2193541e83ce1a89aaa44c2fd4783abd54853823075a32b60ef032284c0" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", + "thiserror", +] + +[[package]] +name = "openmls_traits" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac13edad8698eb1917cbc1dab99591fa1594d84320904fe3fc5121854f95831c" +dependencies = [ + "serde", + "tls_codec", +] + +[[package]] +name = "p256" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9863ad85fa8f4460f9c48cb909d38a0d689dba1f6f6988a5e3e0d31071bcd4b" +dependencies = [ + "ecdsa", + "elliptic-curve", + "primeorder", + "sha2 0.10.9", +] + +[[package]] +name = "p384" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe42f1670a52a47d448f14b6a5c61dd78fce51856e68edaa38f7ae3a46b8d6b6" +dependencies = [ + "ecdsa", + "elliptic-curve", + "primeorder", + "sha2 0.10.9", +] + +[[package]] +name = "pastey" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ee67f1008b1ba2321834326597b8e186293b049a023cdef258527550b9935b4" + +[[package]] +name = "pem-rfc7468" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88b39c9bfcfc231068454382784bb460aae594343fb030d46e9f50a645418412" +dependencies = [ + "base64ct", +] + [[package]] name = "percent-encoding" version = "2.3.2" @@ -511,12 +1630,83 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" +[[package]] +name = "pkcs8" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f950b2377845cebe5cf8b5165cb3cc1a5e0fa5cfa3e1f7f55707d8fd82e0a7b7" +dependencies = [ + "der 0.7.10", + "spki 0.7.3", +] + +[[package]] +name = "pkcs8" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "451913da69c775a56034ea8d9003d27ee8948e12443eae7c038ba100a4f21cb7" +dependencies = [ + "der 0.8.3", + "spki 0.8.1", +] + [[package]] name = "pkg-config" version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548" +[[package]] +name = "poly1305" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8159bd90725d2df49889a078b54f4f79e87f1f8a8444194cdca81d38f5393abf" +dependencies = [ + "cpufeatures 0.2.17", + "opaque-debug", + "universal-hash", +] + +[[package]] +name = "polyval" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d1fe60d06143b2430aa532c94cfe9e29783047f06c0d7fd359a9a51b729fa25" +dependencies = [ + "cfg-if", + "cpufeatures 0.2.17", + "opaque-debug", + "universal-hash", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "prettyplease" +version = "0.2.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b" +dependencies = [ + "proc-macro2", + "syn 2.0.119", +] + +[[package]] +name = "primeorder" +version = "0.13.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "353e1ca18966c16d9deb1c69278edbc5f194139612772bd9537af60ac231e1e6" +dependencies = [ + "elliptic-curve", +] + [[package]] name = "proc-macro-crate" version = "3.5.0" @@ -526,6 +1716,28 @@ dependencies = [ "toml_edit", ] +[[package]] +name = "proc-macro-error-attr2" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96de42df36bb9bba5542fe9f1a054b8cc87e172759a1868aa05c1f3acc89dfc5" +dependencies = [ + "proc-macro2", + "quote", +] + +[[package]] +name = "proc-macro-error2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11ec05c52be0a07b08061f7dd003e7d7092e0472bc731b4af7bb1ef876109802" +dependencies = [ + "proc-macro-error-attr2", + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "proc-macro2" version = "1.0.107" @@ -544,7 +1756,7 @@ dependencies = [ "bit-set", "bit-vec", "bitflags", - "chacha20", + "chacha20 0.10.2", "core_detect", "num-traits", "rand", @@ -588,8 +1800,38 @@ version = "0.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65c9fb96cbc91e3478eaae79a69fcd3f1ae4ad052e471fe6732fff548984b4af" dependencies = [ + "chacha20 0.10.2", "getrandom 0.4.3", - "rand_core", + "rand_core 0.10.1", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_chacha" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e6af7f3e25ded52c41df4e0b1af2d047e45896c2f3281792ed68a1c243daedb" +dependencies = [ + "ppv-lite86", + "rand_core 0.10.1", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom 0.2.17", ] [[package]] @@ -604,7 +1846,50 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60aa6af80be32871323012e02e6e65f8a7cc7890931ae421d217ad8fe0df2ccf" dependencies = [ - "rand_core", + "rand_core 0.10.1", +] + +[[package]] +name = "rayon" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb39b166781f92d482534ef4b4b1b2568f42613b53e5b6c160e24cfbfa30926d" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + +[[package]] +name = "regex" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", ] [[package]] @@ -613,6 +1898,16 @@ version = "0.8.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" +[[package]] +name = "rfc6979" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dd2a808d456c4a54e300a23e9f5a67e122c3024119acbfd73e3bf664491cb2" +dependencies = [ + "hmac 0.12.1", + "subtle", +] + [[package]] name = "rsqlite-vfs" version = "0.2.0" @@ -637,6 +1932,21 @@ dependencies = [ "sqlite-wasm-rs", ] +[[package]] +name = "rustc-hash" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d" + +[[package]] +name = "rustc_version" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" +dependencies = [ + "semver", +] + [[package]] name = "rustix" version = "1.1.6" @@ -650,6 +1960,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "rustversion" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" + [[package]] name = "rusty-fork" version = "0.3.1" @@ -662,6 +1978,26 @@ dependencies = [ "wait-timeout", ] +[[package]] +name = "sec1" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3e97a565f76233a6003f9f5c54be1d9c5bdfa3eccfb189469f11ec4901c47dc" +dependencies = [ + "base16ct", + "der 0.7.10", + "generic-array", + "pkcs8 0.10.2", + "subtle", + "zeroize", +] + +[[package]] +name = "semver" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" + [[package]] name = "serde" version = "1.0.229" @@ -672,6 +2008,16 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde_bytes" +version = "0.11.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5d440709e79d88e51ac01c4b72fc6cb7314017bb7da9eeff678aa94c10e3ea8" +dependencies = [ + "serde", + "serde_core", +] + [[package]] name = "serde_core" version = "1.0.229" @@ -689,7 +2035,7 @@ checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 3.0.10", ] [[package]] @@ -724,9 +2070,48 @@ checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" dependencies = [ "cfg-if", "cpufeatures 0.2.17", - "digest", + "digest 0.10.7", ] +[[package]] +name = "sha2" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d7069beb7d6ac7b9acd1039986e73443f24234f41074da099d6f994ac9ad19" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.1", + "digest 0.11.3", +] + +[[package]] +name = "sha3" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc9bad02c26382724b2d2692c6f179285e4b54eeecd7968f52a50059c3c11759" +dependencies = [ + "digest 0.11.3", + "keccak", + "sponge-cursor", +] + +[[package]] +name = "shake" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09057cb2149ad4cbd2da1e26b351f9a4c354219421229c69c3063e6f61947c4a" +dependencies = [ + "digest 0.11.3", + "keccak", + "sponge-cursor", +] + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + [[package]] name = "shlex" version = "2.0.1" @@ -743,6 +2128,26 @@ dependencies = [ "libc", ] +[[package]] +name = "signature" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de" +dependencies = [ + "digest 0.10.7", + "rand_core 0.6.4", +] + +[[package]] +name = "signature" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d567dcbaf0049cb8ac2608a76cd95ff9e4412e1899d389ee400918ca7537f5" +dependencies = [ + "digest 0.11.3", + "rand_core 0.10.1", +] + [[package]] name = "slab" version = "0.4.12" @@ -765,6 +2170,32 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "spki" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d91ed6c858b01f942cd56b37a94b3e0a1798290327d1236e4d9cf4eaca44d29d" +dependencies = [ + "base64ct", + "der 0.7.10", +] + +[[package]] +name = "spki" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ef958a98b9d5da290cfc78946e9f3e61e1e62a18db0d92cac0b83cc161491a9" +dependencies = [ + "base64ct", + "der 0.8.3", +] + +[[package]] +name = "sponge-cursor" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a0219bd7d979d58245a4f41f695e1ac9f8befdffadd7f61f1bae9e39abc6620" + [[package]] name = "sqlite-wasm-rs" version = "0.6.3" @@ -781,6 +2212,17 @@ version = "2.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" +[[package]] +name = "syn" +version = "2.0.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "syn" version = "3.0.10" @@ -802,7 +2244,19 @@ checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" name = "tabellarius-client" version = "0.0.0" dependencies = [ + "axum", + "hkdf 0.12.4", + "openmls", + "openmls_basic_credential", + "openmls_rust_crypto", + "openmls_traits", + "serde", + "serde_json", + "sha2 0.10.9", "tabellarius-protocol", + "tabellarius-relay", + "tempfile", + "tokio", ] [[package]] @@ -813,7 +2267,7 @@ dependencies = [ "proptest", "serde", "serde_json", - "sha2", + "sha2 0.10.9", ] [[package]] @@ -863,7 +2317,30 @@ checksum = "fe5197923287db20a58125f0bc85c062f7f2c892de97b18c356f9efb14b28524" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 3.0.10", +] + +[[package]] +name = "tls_codec" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18cc98286004cea38f717e2b03d990fc774fbfd38a82720de40e5c94365067c8" +dependencies = [ + "serde", + "serde_bytes", + "tls_codec_derive", + "zeroize", +] + +[[package]] +name = "tls_codec_derive" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "674f41dd95f76cdeb005c83f9444e20cf43daebef37cde9e49a9ca6f4e87b423" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", ] [[package]] @@ -889,7 +2366,7 @@ checksum = "78773a2a397f451582ce068015985c33193cf6dea8b74d2a639fe457b2f07b0e" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 3.0.10", ] [[package]] @@ -967,6 +2444,27 @@ version = "1.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" +[[package]] +name = "universal-hash" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc1de2c688dc15305988b563c3854064043356019f97a4b46276fe734c4f07ea" +dependencies = [ + "crypto-common 0.1.7", + "subtle", +] + +[[package]] +name = "uuid" +version = "1.29.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bed99139f7dac9b5cfe5f17be4d66dc7079a34dee1d74bcc77217119240e267c" +dependencies = [ + "getrandom 0.4.3", + "js-sys", + "wasm-bindgen", +] + [[package]] name = "vcpkg" version = "0.2.15" @@ -1003,6 +2501,50 @@ dependencies = [ "wit-bindgen", ] +[[package]] +name = "wasm-bindgen" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0181c14ddd6c7b8b0b0132a5f1ea2f32be0d893cd3ea97b6e3b28886b8a3d1c" +dependencies = [ + "cfg-if", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e931a02a39abdbf66966fc35902b5ed58344750a70885bed4ebb3d8eaf28ed9" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ca4b54280e455bd831e33a6b8e734baf432fe63fbc35c42ac14ce67a043a53a" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn 3.0.10", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b1f9c3798725016c6ee5f2f57869ec0831e8bbc0ab0310a86e787e97f41389e" +dependencies = [ + "unicode-ident", +] + [[package]] name = "windows-link" version = "0.2.1" @@ -1033,6 +2575,81 @@ version = "0.57.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" +[[package]] +name = "x-wing" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01576e56599d01d75c5efcd5b30fa5de29e776f5a225e43fbf8c92c7302d4c8b" +dependencies = [ + "kem", + "ml-kem", + "sha3", + "shake", + "x25519-dalek 3.0.0", +] + +[[package]] +name = "x25519-dalek" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7e468321c81fb07fa7f4c636c3972b9100f0346e5b6a9f2bd0603a52f7ed277" +dependencies = [ + "curve25519-dalek 4.1.3", + "rand_core 0.6.4", + "serde", + "zeroize", +] + +[[package]] +name = "x25519-dalek" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7e8131a03190127fb2263afc72b322ecadae46b6ff8c6f399ff5d02f5559af6" +dependencies = [ + "curve25519-dalek 5.0.0", + "rand_core 0.10.1", +] + +[[package]] +name = "zerocopy" +version = "0.8.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5fe1f8f1b06191a00962174c61aa5005e0bb391a6d80d07e24d115c01a92ed8" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "863ad3ac83293fb4d740aedbfdc9240dd8d1a50c1099acd76ce80ce7c7230c7f" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "zeroize" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13084392c5e4bc371903e2935a5eaeed24905a7511356b883835e18a78f6879" +dependencies = [ + "zeroize_derive", +] + +[[package]] +name = "zeroize_derive" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c50655cbb0fe3fc43170059e702f1ce5e19b84cec58dc87b037a09935c2f328" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "zmij" version = "1.0.23" diff --git a/crates/client/Cargo.toml b/crates/client/Cargo.toml index c96cf4d..7d2009b 100644 --- a/crates/client/Cargo.toml +++ b/crates/client/Cargo.toml @@ -8,6 +8,24 @@ publish.workspace = true [dependencies] tabellarius-protocol = { path = "../protocol" } +# MLS (RFC 9420). Pure-Rust crypto provider (RustCrypto), so Android and iOS need no C toolchain. +openmls = "0.9" +openmls_rust_crypto = "0.6" +openmls_basic_credential = "0.6" +openmls_traits = "0.6" +# Derives the welcome mailbox from an invite secret (client.md §2). +hkdf = "0.12" +sha2 = "0.10" +serde = { version = "1", features = ["derive"] } +serde_json = "1" + +[dev-dependencies] +# The real relay, for the end-to-end test only. `check-deps.sh` checks normal edges: the client +# library itself never depends on the relay. +tabellarius-relay = { path = "../relay" } +axum = { version = "0.8", default-features = false, features = ["http1", "tokio"] } +tokio = { version = "1", features = ["rt", "net"] } +tempfile = "3" [lints] workspace = true diff --git a/crates/client/src/lib.rs b/crates/client/src/lib.rs index 44df540..1b1d5f5 100644 --- a/crates/client/src/lib.rs +++ b/crates/client/src/lib.rs @@ -1,2 +1,470 @@ -//! Client library: MLS (via OpenMLS) and the mailbox client. -//! Empty until the protocol review is approved. No cryptography is ever written here by hand. +//! The Tabellarius client: MLS 1:1 conversations (OpenMLS), a mailbox per contact, invites. +//! No cryptography is written here by hand: MLS is OpenMLS, HKDF and SHA-256 are RustCrypto. +//! +//! Pairing (`.agents/features/client.md` §2): +//! 1. The inviter makes a KeyPackage and a link (invite id + secret). It also makes a *welcome +//! mailbox*, whose id and post token are derived from the secret. +//! 2. The invitee claims the KeyPackage, creates the MLS group, and posts the Welcome and an MLS +//! `hello` (its own mailbox and post token, and its name) to the welcome mailbox. +//! 3. The inviter joins, and answers with its own `hello` to the invitee's mailbox. +//! +//! The relay only ever holds MLS ciphertext, KeyPackages, ids and token hashes. + +pub mod transport; + +use hkdf::Hkdf; +use openmls::prelude::tls_codec::{Deserialize as _, Serialize as _}; +use openmls::prelude::*; +use openmls_basic_credential::SignatureKeyPair; +use openmls_rust_crypto::OpenMlsRustCrypto; +use serde::{Deserialize, Serialize}; +use sha2::Sha256; +use tabellarius_protocol::{ + blob_hash, CreateInvite, CreateMailbox, InviteId, KeyPackageBytes, MailboxId, Token, + MAX_BLOB_BYTES, +}; +pub use transport::{MemoryTransport, Transport, TransportError}; + +/// RFC 9420's mandatory ciphersuite. +pub const CIPHERSUITE: Ciphersuite = Ciphersuite::MLS_128_DHKEMX25519_AES128GCM_SHA256_Ed25519; +/// The longest display name sent in a `hello`. +pub const MAX_NAME_CHARS: usize = 64; +/// The longest text message, in bytes; well under a blob's limit once encrypted. +pub const MAX_TEXT_BYTES: usize = 64 * 1024; + +const WELCOME_ID_LABEL: &[u8] = b"tabellarius v0 welcome mailbox"; +const WELCOME_POST_LABEL: &[u8] = b"tabellarius v0 welcome mailbox post"; + +/// Why something failed. Never carries a message's text, a name, or a token. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum ClientError { + Transport(TransportError), + /// MLS refused: a bad KeyPackage, a Welcome that is not for us, a message that does not verify. + Mls, + /// No randomness from the system. + Random, + /// A name or text over its limit. + TooLong, + /// No such contact, or a contact whose mailbox is not known yet. + UnknownContact, +} + +impl From for ClientError { + fn from(e: TransportError) -> Self { + ClientError::Transport(e) + } +} + +/// What the link to share carries, besides the relay's address. +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct Invite { + pub id: InviteId, + pub secret: Token, +} + +/// The welcome mailbox's id and post token, derived from an invite secret with HKDF-SHA256. +pub fn welcome_mailbox(secret: &Token) -> (MailboxId, Token) { + let hk = Hkdf::::new(None, secret.as_bytes()); + let mut id = [0u8; 16]; + let mut post = [0u8; 32]; + // Lengths within HKDF's limit (255 × 32 bytes): expand cannot fail here. + let _ = hk.expand(WELCOME_ID_LABEL, &mut id); + let _ = hk.expand(WELCOME_POST_LABEL, &mut post); + (MailboxId::from_bytes(id), Token::from_bytes(post)) +} + +/// A local handle for a contact. Means nothing outside this client. +#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] +pub struct ContactId(pub u32); + +/// What [`Client::poll`] found. +#[derive(Clone, PartialEq, Eq, Debug)] +pub enum Event { + /// The other side's `hello` arrived: the conversation can be used both ways. + Joined { + contact: ContactId, + name: String, + }, + Message { + contact: ContactId, + text: String, + }, +} + +/// The JSON inside an MLS application message. The relay never sees it. +#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] +#[serde(tag = "kind", rename_all = "camelCase", deny_unknown_fields)] +enum Payload { + #[serde(rename_all = "camelCase")] + Hello { + mailbox: MailboxId, + post_token: Token, + name: String, + }, + Text { + body: String, + }, +} + +struct Mailbox { + id: MailboxId, + fetch: Token, +} + +struct Contact { + id: ContactId, + group: MlsGroup, + name: Option, + /// Where the other side posts to us. + inbox: Option, + /// Where we post to the other side: their mailbox and its post token. + outbox: Option<(MailboxId, Token)>, +} + +pub struct Client { + provider: OpenMlsRustCrypto, + signer: SignatureKeyPair, + credential: CredentialWithKey, + name: String, + transport: T, + contacts: Vec, + welcome_boxes: Vec, + next_contact: u32, +} + +impl Client { + /// A new identity: an Ed25519 key and a credential holding 16 random bytes, never a name. + pub fn new(name: &str, transport: T) -> Result { + if name.chars().count() > MAX_NAME_CHARS { + return Err(ClientError::TooLong); + } + let provider = OpenMlsRustCrypto::default(); + let signer = SignatureKeyPair::new(CIPHERSUITE.signature_algorithm()) + .map_err(|_| ClientError::Mls)?; + signer + .store(provider.storage()) + .map_err(|_| ClientError::Mls)?; + let identity: [u8; 16] = provider + .rand() + .random_array() + .map_err(|_| ClientError::Random)?; + let credential = CredentialWithKey { + credential: BasicCredential::new(identity.to_vec()).into(), + signature_key: signer.public().into(), + }; + Ok(Self { + provider, + signer, + credential, + name: name.to_string(), + transport, + contacts: Vec::new(), + welcome_boxes: Vec::new(), + next_contact: 0, + }) + } + + fn random(&self) -> Result<[u8; N], ClientError> { + self.provider + .rand() + .random_array() + .map_err(|_| ClientError::Random) + } + + /// A fresh mailbox on the relay: we keep the fetch token, the post token goes to the contact. + fn new_mailbox(&self) -> Result<(Mailbox, Token), ClientError> { + let id = MailboxId::from_bytes(self.random()?); + let post = Token::from_bytes(self.random()?); + let fetch = Token::from_bytes(self.random()?); + self.transport.create_mailbox(&CreateMailbox { + id, + post_token_hash: post.hash(), + fetch_token_hash: fetch.hash(), + })?; + Ok((Mailbox { id, fetch }, post)) + } + + /// Step 1: a KeyPackage on the relay and a welcome mailbox. Share the result as a link. + pub fn create_invite(&mut self) -> Result { + let bundle = KeyPackage::builder() + .build( + CIPHERSUITE, + &self.provider, + &self.signer, + self.credential.clone(), + ) + .map_err(|_| ClientError::Mls)?; + let kp = bundle + .key_package() + .tls_serialize_detached() + .map_err(|_| ClientError::Mls)?; + let invite = Invite { + id: InviteId::from_bytes(self.random()?), + secret: Token::from_bytes(self.random()?), + }; + let (welcome_id, welcome_post) = welcome_mailbox(&invite.secret); + let fetch = Token::from_bytes(self.random()?); + self.transport.create_mailbox(&CreateMailbox { + id: welcome_id, + post_token_hash: welcome_post.hash(), + fetch_token_hash: fetch.hash(), + })?; + self.transport.create_invite(&CreateInvite { + id: invite.id, + secret_hash: invite.secret.hash(), + key_package: KeyPackageBytes::new(kp).map_err(|_| ClientError::Mls)?, + })?; + self.welcome_boxes.push(Mailbox { + id: welcome_id, + fetch, + }); + Ok(invite) + } + + /// Step 2, on the other device: claim the KeyPackage, make the group, send Welcome and hello. + pub fn accept_invite(&mut self, invite: &Invite) -> Result { + let kp_bytes = self.transport.claim_invite(&invite.id, &invite.secret)?; + let key_package = KeyPackageIn::tls_deserialize_exact(&kp_bytes) + .map_err(|_| ClientError::Mls)? + .validate(self.provider.crypto(), ProtocolVersion::Mls10) + .map_err(|_| ClientError::Mls)?; + let config = MlsGroupCreateConfig::builder() + .ciphersuite(CIPHERSUITE) + .use_ratchet_tree_extension(true) + .build(); + let mut group = MlsGroup::new( + &self.provider, + &self.signer, + &config, + self.credential.clone(), + ) + .map_err(|_| ClientError::Mls)?; + let (_commit, welcome, _info) = group + .add_members(&self.provider, &self.signer, &[key_package]) + .map_err(|_| ClientError::Mls)?; + group + .merge_pending_commit(&self.provider) + .map_err(|_| ClientError::Mls)?; + + let (inbox, inbox_post) = self.new_mailbox()?; + let hello = Payload::Hello { + mailbox: inbox.id, + post_token: inbox_post, + name: self.name.clone(), + }; + let (welcome_id, welcome_post) = welcome_mailbox(&invite.secret); + let welcome = welcome + .tls_serialize_detached() + .map_err(|_| ClientError::Mls)?; + let hello = Self::seal(&self.provider, &self.signer, &mut group, &hello)?; + self.transport.post(&welcome_id, &welcome_post, &welcome)?; + self.transport.post(&welcome_id, &welcome_post, &hello)?; + + let id = self.add_contact(group, Some(inbox), None); + Ok(id) + } + + fn add_contact( + &mut self, + group: MlsGroup, + inbox: Option, + outbox: Option<(MailboxId, Token)>, + ) -> ContactId { + let id = ContactId(self.next_contact); + self.next_contact += 1; + self.contacts.push(Contact { + id, + group, + name: None, + inbox, + outbox, + }); + id + } + + fn seal( + provider: &OpenMlsRustCrypto, + signer: &SignatureKeyPair, + group: &mut MlsGroup, + payload: &Payload, + ) -> Result, ClientError> { + let json = serde_json::to_vec(payload).map_err(|_| ClientError::Mls)?; + let blob = group + .create_message(provider, signer, &json) + .map_err(|_| ClientError::Mls)? + .tls_serialize_detached() + .map_err(|_| ClientError::Mls)?; + if blob.len() > MAX_BLOB_BYTES { + return Err(ClientError::TooLong); + } + Ok(blob) + } + + /// Sends text to a contact whose `hello` has arrived. + pub fn send(&mut self, contact: ContactId, text: &str) -> Result<(), ClientError> { + if text.len() > MAX_TEXT_BYTES { + return Err(ClientError::TooLong); + } + let c = self + .contacts + .iter_mut() + .find(|c| c.id == contact) + .ok_or(ClientError::UnknownContact)?; + let (to, post) = c.outbox.ok_or(ClientError::UnknownContact)?; + let blob = Self::seal( + &self.provider, + &self.signer, + &mut c.group, + &Payload::Text { + body: text.to_string(), + }, + )?; + self.transport.post(&to, &post, &blob)?; + Ok(()) + } + + /// The contact's name, once its `hello` has arrived. + pub fn contact_name(&self, contact: ContactId) -> Option<&str> { + self.contacts + .iter() + .find(|c| c.id == contact) + .and_then(|c| c.name.as_deref()) + } + + /// Fetches every own mailbox, processes each blob in order, and acknowledges it, whether it + /// was understood or refused. A blob that is not valid MLS for one of our groups is dropped + /// without a trace: nothing from it is shown. + pub fn poll(&mut self) -> Result, ClientError> { + let mut events = Vec::new(); + let mut sources: Vec<(MailboxId, Token)> = + self.welcome_boxes.iter().map(|m| (m.id, m.fetch)).collect(); + sources.extend( + self.contacts + .iter() + .filter_map(|c| c.inbox.as_ref().map(|m| (m.id, m.fetch))), + ); + for (id, fetch) in sources { + for blob in self.transport.fetch(&id, &fetch)? { + // A failure to understand one blob must not stop the rest, nor be shown. + if let Ok(Some(event)) = self.process(&blob) { + events.push(event); + } + self.transport.acknowledge(&id, &fetch, &blob_hash(&blob))?; + } + } + Ok(events) + } + + fn process(&mut self, blob: &[u8]) -> Result, ClientError> { + let message = MlsMessageIn::tls_deserialize_exact(blob).map_err(|_| ClientError::Mls)?; + match message.extract() { + MlsMessageBodyIn::Welcome(welcome) => { + let config = MlsGroupJoinConfig::builder() + .use_ratchet_tree_extension(true) + .build(); + let group = StagedWelcome::new_from_welcome(&self.provider, &config, welcome, None) + .map_err(|_| ClientError::Mls)? + .into_group(&self.provider) + .map_err(|_| ClientError::Mls)?; + // Not a contact to show yet: the hello right behind the Welcome makes it one. + self.add_contact(group, None, None); + Ok(None) + } + MlsMessageBodyIn::PrivateMessage(private) => { + let protocol: ProtocolMessage = private.into(); + let group_id = protocol.group_id().clone(); + let index = self + .contacts + .iter() + .position(|c| c.group.group_id() == &group_id) + .ok_or(ClientError::UnknownContact)?; + let processed = self.contacts[index] + .group + .process_message(&self.provider, protocol) + .map_err(|_| ClientError::Mls)?; + let ProcessedMessageContent::ApplicationMessage(app) = processed.into_content() + else { + return Ok(None); + }; + let payload: Payload = + serde_json::from_slice(&app.into_bytes()).map_err(|_| ClientError::Mls)?; + self.receive(index, payload) + } + _ => Err(ClientError::Mls), + } + } + + fn receive(&mut self, index: usize, payload: Payload) -> Result, ClientError> { + match payload { + Payload::Hello { + mailbox, + post_token, + name, + } => { + if name.chars().count() > MAX_NAME_CHARS || self.contacts[index].name.is_some() { + return Err(ClientError::Mls); + } + self.contacts[index].outbox = Some((mailbox, post_token)); + self.contacts[index].name = Some(name.clone()); + // The inviter's side: answer with our own mailbox, inside MLS. + if self.contacts[index].inbox.is_none() { + let (inbox, inbox_post) = self.new_mailbox()?; + let hello = Payload::Hello { + mailbox: inbox.id, + post_token: inbox_post, + name: self.name.clone(), + }; + let c = &mut self.contacts[index]; + let blob = Self::seal(&self.provider, &self.signer, &mut c.group, &hello)?; + c.inbox = Some(inbox); + self.transport.post(&mailbox, &post_token, &blob)?; + } + Ok(Some(Event::Joined { + contact: self.contacts[index].id, + name, + })) + } + Payload::Text { body } => { + let c = &self.contacts[index]; + if c.name.is_none() { + // Text before hello: not from a conversation that has been set up. + return Err(ClientError::Mls); + } + Ok(Some(Event::Message { + contact: c.id, + text: body, + })) + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A contact who sends a second `hello` must not redirect our messages to another mailbox, + /// nor rename themselves. Only the contact can send one (MLS authenticates it), so this guards + /// against a misbehaving contact or a replayed message. + #[test] + fn a_second_hello_changes_nothing() { + let relay = MemoryTransport::default(); + let mut eva = Client::new("Eva", relay.clone()).expect("eva"); + let mut jan = Client::new("Jan", relay.clone()).expect("jan"); + let invite = eva.create_invite().expect("invite"); + jan.accept_invite(&invite).expect("accept"); + let joined = eva.poll().expect("poll"); + assert_eq!(joined.len(), 1); + let before = eva.contacts[0].outbox.map(|(m, _)| m); + + let other = MailboxId::from_bytes([9; 16]); + let again = Payload::Hello { + mailbox: other, + post_token: Token::from_bytes([9; 32]), + name: "Mallory".into(), + }; + assert_eq!(eva.receive(0, again), Err(ClientError::Mls)); + assert_eq!(eva.contacts[0].outbox.map(|(m, _)| m), before); + assert_eq!(eva.contacts[0].name.as_deref(), Some("Jan")); + } +} diff --git a/crates/client/src/transport.rs b/crates/client/src/transport.rs new file mode 100644 index 0000000..21ca5ae --- /dev/null +++ b/crates/client/src/transport.rs @@ -0,0 +1,153 @@ +//! How the client reaches a relay. The library makes no network calls itself: an app gives it a +//! [`Transport`] (HTTP in Amicitia, F22). [`MemoryTransport`] is a stand-in with the relay's rules, +//! for tests and for running the client on a device without a relay. + +use std::collections::HashMap; +use std::sync::{Arc, Mutex}; +use tabellarius_protocol::{ + blob_hash, CreateInvite, CreateMailbox, InviteId, MailboxId, Sha256Hash, Token, +}; + +/// Why the relay said no. Carries nothing of the request. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum TransportError { + /// 404: unknown, wrong token, expired, or already claimed. + NotFound, + /// 409: the id is taken. + Conflict, + /// Anything else: the network, a 5xx, a body that did not parse. + Failed, +} + +/// The relay's operations (`PROTOCOL.md` §8), one method each. +pub trait Transport { + fn create_mailbox(&self, req: &CreateMailbox) -> Result<(), TransportError>; + fn post(&self, id: &MailboxId, post: &Token, blob: &[u8]) -> Result<(), TransportError>; + /// The waiting blobs, oldest first. + fn fetch(&self, id: &MailboxId, fetch: &Token) -> Result>, TransportError>; + fn acknowledge( + &self, + id: &MailboxId, + fetch: &Token, + hash: &Sha256Hash, + ) -> Result<(), TransportError>; + fn create_invite(&self, req: &CreateInvite) -> Result<(), TransportError>; + /// The KeyPackage, once. + fn claim_invite(&self, id: &InviteId, secret: &Token) -> Result, TransportError>; +} + +#[derive(Default)] +struct Boxes { + mailboxes: HashMap>)>, + invites: HashMap)>, +} + +/// An in-memory relay with the same rules as the real one (no expiry: there is no clock here). +/// Clone it to give two clients the same relay. +#[derive(Clone, Default)] +pub struct MemoryTransport(Arc>); + +impl MemoryTransport { + fn boxes(&self) -> std::sync::MutexGuard<'_, Boxes> { + self.0.lock().unwrap_or_else(|p| p.into_inner()) + } + + /// How many blobs wait on the relay, in all mailboxes: what was not acknowledged. + pub fn waiting_blobs(&self) -> usize { + self.boxes() + .mailboxes + .values() + .map(|(_, _, b)| b.len()) + .sum() + } + + /// Every byte the relay holds, for privacy tests: ids, hashes, blobs and KeyPackages. + pub fn everything_stored(&self) -> Vec { + let b = self.boxes(); + let mut out = Vec::new(); + for (id, (post, fetch, blobs)) in &b.mailboxes { + out.extend_from_slice(id.as_bytes()); + out.extend_from_slice(post.as_bytes()); + out.extend_from_slice(fetch.as_bytes()); + blobs.iter().for_each(|blob| out.extend_from_slice(blob)); + } + for (id, (hash, kp)) in &b.invites { + out.extend_from_slice(id.as_bytes()); + out.extend_from_slice(hash.as_bytes()); + out.extend_from_slice(kp); + } + out + } +} + +impl Transport for MemoryTransport { + fn create_mailbox(&self, req: &CreateMailbox) -> Result<(), TransportError> { + let mut b = self.boxes(); + if b.mailboxes.contains_key(&req.id) { + return Err(TransportError::Conflict); + } + b.mailboxes.insert( + req.id, + (req.post_token_hash, req.fetch_token_hash, Vec::new()), + ); + Ok(()) + } + + fn post(&self, id: &MailboxId, post: &Token, blob: &[u8]) -> Result<(), TransportError> { + match self.boxes().mailboxes.get_mut(id) { + Some((hash, _, blobs)) if *hash == post.hash() => { + blobs.push(blob.to_vec()); + Ok(()) + } + _ => Err(TransportError::NotFound), + } + } + + fn fetch(&self, id: &MailboxId, fetch: &Token) -> Result>, TransportError> { + match self.boxes().mailboxes.get(id) { + Some((_, hash, blobs)) if *hash == fetch.hash() => Ok(blobs.clone()), + _ => Err(TransportError::NotFound), + } + } + + fn acknowledge( + &self, + id: &MailboxId, + fetch: &Token, + hash: &Sha256Hash, + ) -> Result<(), TransportError> { + match self.boxes().mailboxes.get_mut(id) { + Some((_, f, blobs)) if *f == fetch.hash() => { + let at = blobs + .iter() + .position(|b| blob_hash(b) == *hash) + .ok_or(TransportError::NotFound)?; + blobs.remove(at); + Ok(()) + } + _ => Err(TransportError::NotFound), + } + } + + fn create_invite(&self, req: &CreateInvite) -> Result<(), TransportError> { + let mut b = self.boxes(); + if b.invites.contains_key(&req.id) { + return Err(TransportError::Conflict); + } + b.invites + .insert(req.id, (req.secret_hash, req.key_package.bytes().to_vec())); + Ok(()) + } + + fn claim_invite(&self, id: &InviteId, secret: &Token) -> Result, TransportError> { + let mut b = self.boxes(); + match b.invites.get(id) { + Some((hash, _)) if *hash == secret.hash() => b + .invites + .remove(id) + .map(|(_, kp)| kp) + .ok_or(TransportError::NotFound), + _ => Err(TransportError::NotFound), + } + } +} diff --git a/crates/client/tests/pairing.rs b/crates/client/tests/pairing.rs new file mode 100644 index 0000000..972286d --- /dev/null +++ b/crates/client/tests/pairing.rs @@ -0,0 +1,218 @@ +//! Pairing and talking through an in-memory relay. These tests have no I/O, so the same binary runs +//! on an Android emulator (`.agents/features/client.md` §5). + +use tabellarius_client::*; +use tabellarius_protocol::Token; + +fn contains(haystack: &[u8], needle: &[u8]) -> bool { + !needle.is_empty() && haystack.windows(needle.len()).any(|w| w == needle) +} + +/// Eva invites, Jan accepts; both polls exchange the hellos. +fn pair( + relay: &MemoryTransport, +) -> ( + Client, + ContactId, + Client, + ContactId, +) { + let mut eva = Client::new("Eva Nováková", relay.clone()).expect("eva"); + let mut jan = Client::new("Jan", relay.clone()).expect("jan"); + let invite = eva.create_invite().expect("invite"); + let jan_side = jan.accept_invite(&invite).expect("accept"); + let eva_events = eva.poll().expect("eva polls"); + let Some(Event::Joined { + contact: eva_side, + name, + }) = eva_events.first().cloned() + else { + panic!("Eva did not see Jan join: {eva_events:?}"); + }; + assert_eq!(name, "Jan"); + let jan_events = jan.poll().expect("jan polls"); + assert_eq!( + jan_events, + vec![Event::Joined { + contact: jan_side, + name: "Eva Nováková".into() + }] + ); + (eva, eva_side, jan, jan_side) +} + +#[test] +fn two_people_pair_through_an_invite_and_talk_both_ways() { + let relay = MemoryTransport::default(); + let (mut eva, eva_side, mut jan, jan_side) = pair(&relay); + + eva.send(eva_side, "Ahoj Jane, pivo ve čtvrtek?") + .expect("send"); + eva.send(eva_side, "V sedm.").expect("send"); + assert_eq!( + jan.poll().expect("poll"), + vec![ + Event::Message { + contact: jan_side, + text: "Ahoj Jane, pivo ve čtvrtek?".into() + }, + Event::Message { + contact: jan_side, + text: "V sedm.".into() + }, + ] + ); + jan.send(jan_side, "Platí 🍺").expect("send"); + assert_eq!( + eva.poll().expect("poll"), + vec![Event::Message { + contact: eva_side, + text: "Platí 🍺".into() + }] + ); + // Everything delivered was acknowledged: nothing waits on the relay, and a second poll is + // empty on both sides. + assert_eq!(relay.waiting_blobs(), 0); + assert!(eva.poll().expect("poll").is_empty()); + assert!(jan.poll().expect("poll").is_empty()); + assert_eq!(eva.contact_name(eva_side), Some("Jan")); +} + +#[test] +fn the_relay_holds_no_name_and_no_word() { + let relay = MemoryTransport::default(); + let (mut eva, eva_side, mut jan, jan_side) = pair(&relay); + let canary = "CANARY-7f3a91-the-relay-must-never-see-this"; + eva.send(eva_side, canary).expect("send"); + jan.send(jan_side, canary).expect("send"); + // Before anyone fetches: the canaries are waiting on the relay, encrypted. + let stored = relay.everything_stored(); + assert!(!stored.is_empty()); + for (what, needle) in [ + ("canary", canary.as_bytes()), + ("inviter's name", "Eva Nováková".as_bytes()), + ("invitee's name", b"Jan".as_slice()), + ] { + assert!( + !contains(&stored, needle), + "{what} is readable on the relay" + ); + } +} + +#[test] +fn a_link_works_once() { + let relay = MemoryTransport::default(); + let mut eva = Client::new("Eva", relay.clone()).expect("eva"); + let mut jan = Client::new("Jan", relay.clone()).expect("jan"); + let mut mallory = Client::new("Mallory", relay.clone()).expect("mallory"); + let invite = eva.create_invite().expect("invite"); + jan.accept_invite(&invite).expect("first"); + assert_eq!( + mallory.accept_invite(&invite), + Err(ClientError::Transport(TransportError::NotFound)) + ); + let joined: Vec<_> = eva.poll().expect("poll"); + assert_eq!(joined.len(), 1, "exactly one person joined: {joined:?}"); +} + +#[test] +fn a_wrong_secret_claims_nothing() { + let relay = MemoryTransport::default(); + let mut eva = Client::new("Eva", relay.clone()).expect("eva"); + let mut jan = Client::new("Jan", relay.clone()).expect("jan"); + let mut invite = eva.create_invite().expect("invite"); + invite.secret = Token::from_bytes([7; 32]); + assert!(jan.accept_invite(&invite).is_err()); + assert!(eva.poll().expect("poll").is_empty()); +} + +#[test] +fn junk_in_a_mailbox_is_dropped_and_never_shown() { + let relay = MemoryTransport::default(); + let mut eva = Client::new("Eva", relay.clone()).expect("eva"); + let mut jan = Client::new("Jan", relay.clone()).expect("jan"); + let invite = eva.create_invite().expect("invite"); + // Whoever holds the link can post to Eva's welcome mailbox: before, between and after the + // real Welcome and hello, bytes that are not MLS. + let (welcome_box, welcome_post) = welcome_mailbox(&invite.secret); + relay + .post(&welcome_box, &welcome_post, b"not MLS at all") + .expect("post"); + let jan_side = jan.accept_invite(&invite).expect("accept"); + relay + .post(&welcome_box, &welcome_post, &[0x00, 0x01, 0x00, 0x05]) + .expect("post"); + let events = eva.poll().expect("poll"); + assert_eq!(events.len(), 1, "only the join is shown: {events:?}"); + assert!(matches!(events[0], Event::Joined { .. })); + assert_eq!( + relay.waiting_blobs(), + 1, + "junk acknowledged too; only Eva's hello to Jan waits" + ); + jan.poll().expect("poll"); + jan.send(jan_side, "still here").expect("send"); + let Event::Joined { + contact: eva_side, .. + } = events[0].clone() + else { + unreachable!() + }; + assert_eq!( + eva.poll().expect("poll"), + vec![Event::Message { + contact: eva_side, + text: "still here".into() + }] + ); +} + +#[test] +fn text_before_a_hello_is_not_shown() { + // A Welcome alone makes a group, not a contact: nothing is shown until the hello arrives. + let relay = MemoryTransport::default(); + let mut eva = Client::new("Eva", relay.clone()).expect("eva"); + let mut jan = Client::new("Jan", relay.clone()).expect("jan"); + let invite = eva.create_invite().expect("invite"); + let jan_side = jan.accept_invite(&invite).expect("accept"); + assert_eq!( + jan.send(jan_side, "too early"), + Err(ClientError::UnknownContact) + ); +} + +#[test] +fn limits_are_kept() { + let relay = MemoryTransport::default(); + assert!(Client::new(&"x".repeat(MAX_NAME_CHARS + 1), relay.clone()).is_err()); + let (mut eva, eva_side, _jan, _) = pair(&relay); + assert_eq!( + eva.send(eva_side, &"x".repeat(MAX_TEXT_BYTES + 1)), + Err(ClientError::TooLong) + ); + assert_eq!( + eva.send(ContactId(99), "hi"), + Err(ClientError::UnknownContact) + ); +} + +#[test] +fn the_welcome_mailbox_derivation_is_fixed() { + // A pinned vector: changing labels or the KDF breaks pairing between versions, so it must be + // a deliberate protocol change, not an accident. + let (id, post) = welcome_mailbox(&Token::from_bytes([0; 32])); + let (id2, post2) = welcome_mailbox(&Token::from_bytes([0; 32])); + assert_eq!((id, post.to_wire()), (id2, post2.to_wire())); + assert_ne!( + id.as_bytes()[..], + post.as_bytes()[..16], + "id and post token are independent" + ); + assert_eq!(id.to_wire(), WELCOME_ID_VECTOR); + assert_eq!(post.to_wire(), WELCOME_POST_VECTOR); +} + +// Computed independently: HKDF-SHA256 (RFC 5869, empty salt) written out with Python's hmac module. +const WELCOME_ID_VECTOR: &str = "tsx3c7bagkEGmf1aDem6SA"; +const WELCOME_POST_VECTOR: &str = "FE6Xgv4j9CYnZA1EF_l4UaCNkxrZrfXmkNXnwm9K02w"; diff --git a/crates/client/tests/real_relay.rs b/crates/client/tests/real_relay.rs new file mode 100644 index 0000000..3e36536 --- /dev/null +++ b/crates/client/tests/real_relay.rs @@ -0,0 +1,207 @@ +//! Two clients through the real relay over loopback TCP, then a byte search of the relay's +//! database file: the canary in a message, and both names, must not be in it (`PROTOCOL.md` §7). +//! The relay is a dev-dependency only; the client library never depends on it. + +use std::io::{Read, Write}; +use std::net::{SocketAddr, TcpStream}; +use std::sync::Arc; +use tabellarius_client::*; +use tabellarius_protocol::*; +use tabellarius_relay::http::{router, AppState, Clock}; +use tabellarius_relay::store::Store; + +/// A deliberately small blocking HTTP/1.1 transport for the test: one connection per request. +#[derive(Clone)] +struct Http(SocketAddr); + +impl Http { + fn call( + &self, + method: &str, + path: &str, + auth: Option<&Token>, + body: &[u8], + json: bool, + ) -> Result, TransportError> { + let mut s = TcpStream::connect(self.0).map_err(|_| TransportError::Failed)?; + let auth = auth + .map(|t| format!("Authorization: {}\r\n", t.to_bearer())) + .unwrap_or_default(); + let ctype = if json { + "Content-Type: application/json\r\n" + } else { + "" + }; + let head = format!( + "{method} {path} HTTP/1.1\r\nHost: relay\r\n{auth}{ctype}Content-Length: {}\r\nConnection: close\r\n\r\n", + body.len() + ); + s.write_all(head.as_bytes()) + .map_err(|_| TransportError::Failed)?; + s.write_all(body).map_err(|_| TransportError::Failed)?; + let mut resp = Vec::new(); + s.read_to_end(&mut resp) + .map_err(|_| TransportError::Failed)?; + let split = resp + .windows(4) + .position(|w| w == b"\r\n\r\n") + .ok_or(TransportError::Failed)?; + let status = std::str::from_utf8(&resp[9..12]).map_err(|_| TransportError::Failed)?; + match status { + "200" | "201" | "204" => Ok(resp[split + 4..].to_vec()), + "404" => Err(TransportError::NotFound), + "409" => Err(TransportError::Conflict), + _ => Err(TransportError::Failed), + } + } +} + +impl Transport for Http { + fn create_mailbox(&self, req: &CreateMailbox) -> Result<(), TransportError> { + let body = serde_json::to_vec(req).map_err(|_| TransportError::Failed)?; + self.call("POST", "/v0/mailboxes", None, &body, true) + .map(|_| ()) + } + fn post(&self, id: &MailboxId, post: &Token, blob: &[u8]) -> Result<(), TransportError> { + self.call( + "POST", + &format!("/v0/mailboxes/{}/blobs", id.to_wire()), + Some(post), + blob, + false, + ) + .map(|_| ()) + } + fn fetch(&self, id: &MailboxId, fetch: &Token) -> Result>, TransportError> { + let body = self.call( + "GET", + &format!("/v0/mailboxes/{}/blobs", id.to_wire()), + Some(fetch), + &[], + false, + )?; + let blobs: FetchedBlobs = + serde_json::from_slice(&body).map_err(|_| TransportError::Failed)?; + Ok(blobs + .blobs + .into_iter() + .map(|b| b.blob.bytes().to_vec()) + .collect()) + } + fn acknowledge( + &self, + id: &MailboxId, + fetch: &Token, + hash: &Sha256Hash, + ) -> Result<(), TransportError> { + let path = format!("/v0/mailboxes/{}/blobs/{}", id.to_wire(), hash.to_wire()); + self.call("DELETE", &path, Some(fetch), &[], false) + .map(|_| ()) + } + fn create_invite(&self, req: &CreateInvite) -> Result<(), TransportError> { + let body = serde_json::to_vec(req).map_err(|_| TransportError::Failed)?; + self.call("POST", "/v0/invites", None, &body, true) + .map(|_| ()) + } + fn claim_invite(&self, id: &InviteId, secret: &Token) -> Result, TransportError> { + let body = self.call( + "GET", + &format!("/v0/invites/{}", id.to_wire()), + Some(secret), + &[], + false, + )?; + let claimed: ClaimedInvite = + serde_json::from_slice(&body).map_err(|_| TransportError::Failed)?; + Ok(claimed.key_package.bytes().to_vec()) + } +} + +/// Starts the real relay on 127.0.0.1: with its database in `db`. +fn start_relay(db: &std::path::Path) -> SocketAddr { + let store = Arc::new(Store::open(db).expect("store")); + let clock: Clock = Arc::new(|| 1_794_000_000); + let app = router(AppState { store, clock }); + let (tx, rx) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + let rt = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .expect("runtime"); + rt.block_on(async move { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0") + .await + .expect("bind"); + tx.send(listener.local_addr().expect("addr")) + .expect("send addr"); + let _ = axum::serve(listener, app).await; + }); + }); + rx.recv().expect("relay address") +} + +fn contains(haystack: &[u8], needle: &[u8]) -> bool { + !needle.is_empty() && haystack.windows(needle.len()).any(|w| w == needle) +} + +#[test] +fn pair_and_talk_through_a_real_relay_which_never_sees_a_name_or_a_word() { + let dir = tempfile::tempdir().expect("tempdir"); + let db = dir.path().join("relay.db"); + let relay = Http(start_relay(&db)); + + let mut eva = Client::new("Eva Nováková", relay.clone()).expect("eva"); + let mut jan = Client::new("Jan Dvořák", relay.clone()).expect("jan"); + let invite = eva.create_invite().expect("invite"); + // While the invite waits, the relay holds Eva's KeyPackage: it must not carry her name. + let invited = std::fs::read(&db).expect("db"); + let jan_side = jan.accept_invite(&invite).expect("accept"); + let events = eva.poll().expect("poll"); + let Some(Event::Joined { + contact: eva_side, + name, + }) = events.first().cloned() + else { + panic!("no join: {events:?}"); + }; + assert_eq!(name, "Jan Dvořák"); + assert_eq!(jan.poll().expect("poll").len(), 1); + + let canary = "CANARY-relay-test-4b2e-never-in-the-database"; + eva.send(eva_side, canary).expect("send"); + jan.send(jan_side, &format!("re: {canary}")).expect("send"); + + // Snapshot the database while both messages wait, before anyone fetches. + let waiting = std::fs::read(&db).expect("db"); + assert_eq!( + jan.poll().expect("poll"), + vec![Event::Message { + contact: jan_side, + text: canary.into() + }] + ); + assert_eq!( + eva.poll().expect("poll"), + vec![Event::Message { + contact: eva_side, + text: format!("re: {canary}") + }] + ); + let after = std::fs::read(&db).expect("db"); + + for snapshot in [&invited, &waiting, &after] { + for (what, needle) in [ + ("canary", canary.as_bytes()), + ("inviter's name", "Eva Nováková".as_bytes()), + ("invitee's name", "Jan Dvořák".as_bytes()), + ("invite secret", invite.secret.as_bytes().as_slice()), + ] { + assert!( + !contains(snapshot, needle), + "{what} is readable in the relay's database" + ); + } + } + // The messages were there, as ciphertext: the database grew while they waited. + assert!(waiting.len() >= 4096, "the relay stored something"); +} diff --git a/docs/PROTOCOL.md b/docs/PROTOCOL.md index 7231e54..5298dfb 100644 --- a/docs/PROTOCOL.md +++ b/docs/PROTOCOL.md @@ -1,7 +1,8 @@ # Tabellarius protocol v0 — what crosses the wire, and what the relay learns **Approved 2026-11-08 (Mark)** as v0, before any relay code. Every claim below must have a test. A change -is a protocol version bump: documented, reviewed, approved (`CLAUDE.md` §2.2). +is a protocol version bump: documented here with its date and reason, and reviewed in its PR +(`CLAUDE.md` §2.2, §3). ## 1. Threat model | Adversary | Protected | Not protected | @@ -27,10 +28,23 @@ is a protocol version bump: documented, reviewed, approved (`CLAUDE.md` §2.2). - **Block** = the owner rotates the post token; the old one is refused. The blocked person is not told. ## 4. Invitations -A QR code (in person) or a link carries: relay URL, an invite id, and a one-time fetch secret. The -invitee fetches the inviter's MLS KeyPackage from the relay with it, replies with their own, and -both sides exchange mailbox ids and post tokens **inside** MLS. Unclaimed invites expire after -**7 days** *(decided 2026-11-08)*. +A QR code (in person) or a link carries: relay URL, an invite id, and a one-time secret. Unclaimed +invites expire after **7 days** *(decided 2026-11-08)*. +1. **The inviter** leaves its MLS KeyPackage on the relay under the invite id and the secret's hash. + It also creates a **welcome mailbox**, whose id and post token are derived from the secret with + HKDF-SHA256: empty salt, info `tabellarius v0 welcome mailbox` (16 bytes) and + `tabellarius v0 welcome mailbox post` (32 bytes). +2. **The invitee** claims the KeyPackage with the secret, which deletes the invite. It creates the + MLS group with the inviter in it, and posts to the welcome mailbox: + - the Welcome; + - an MLS message carrying its own mailbox id, a post token for it, and its name. +3. **The inviter** joins from the Welcome and answers inside MLS with its own mailbox, post token and + name. + +From then on each side posts to the other's mailbox. Names and post tokens travel **only inside +MLS**. To the relay, the welcome mailbox is an ordinary mailbox. +*(Changed 2026-11-14, approved by Mark, from "the invitee replies with their own KeyPackage": the +invitee needs no KeyPackage, because it creates the group itself.)* ## 5. What the relay stores — the complete list | Table | Columns |