End-to-end encryption in Drusite
This is the design as built: who it defends against, what your server can and can’t see, every
key and format, and what it doesn’t promise. The code is in crates/drusite-e2ee (formats, no
I/O), crates/drusite-core (devices) and crates/drusite-server (the server’s checks). The plan
it was built from, with the reasons for each choice, is docs/phase11.md.
To report a vulnerability, see SECURITY.md. For reviewers:
review.md.
Status
- Format 1, suite 1. This is the format 1.0 ships with. From 1.0 a format changes only through its format and suite bytes, and every 1.x app reads every format any 1.x app wrote.
- No independent review yet. The design and
drusite-e2eeare waiting for one. Until then, treat this as a careful design that nobody outside the project has checked. - Dependencies. The newer cryptography crates (
hpke,x-wing,ml-kemand the crates they need) arecargo vetexemptions, not audits.
1. Encrypted or standard, per workspace
Each workspace is one or the other, and its owners can convert it either way.
- Standard: the server can read the workspace, so its search, Ask, agents over
/mcp, meeting jobs and analytics work there. It’s encrypted at rest with the server’s own keys. - End-to-end encrypted: the server stores ciphertext it can’t open. Only the members’ devices hold keys. An agent token a member makes carries a key too, which the server can use only while that token is presented (§6.9).
2. Threat model
Defended against:
- The server’s operator, and anyone who gets what the server has: the database, its backups, the S3 bucket, the key source, the server’s memory and logs.
- Someone removed from a workspace or a page. They can’t read anything written after their removal, even with a copy of the database.
- A member forging edits. Every entry is signed by the device that wrote it, and each device is certified by its person’s account key, so history’s “who changed this” holds even against a member who has the workspace’s key.
- The network: TLS, and DPoP-bound tokens.
Not defended against:
- A compromised, unlocked device. It holds the keys and the notes.
- Whoever holds an agent token you make for an encrypted workspace, and whoever controls the running server while an agent uses it: either can read that workspace (§6.9).
- A server that changes the code it serves to the browser. The web app comes from the server, and an operator who changes it can take keys from that browser. The desktop and phone apps are signed builds from GitHub releases and run no code from the server.
- A server that lies about someone’s public key, unless you compare safety codes (§6.4).
- Availability. A server can refuse, delay or withhold updates. Devices notice some of this (§8) but can’t prevent it.
3. What the operator can and can’t see
| Encrypted workspace | Standard workspace | |
|---|---|---|
| Page text, titles, icons, properties, database columns, views and values | No | Yes |
| The page tree, the sidebar’s order, the trash | No | Yes |
| The workspace’s name and icon | No | Yes |
| Canvas content, journal entries, meeting audio, transcripts and summaries | No | Yes |
| Attachment contents and file names | No | Yes |
| Version history’s content, and named versions’ names | No | Yes |
| Ask questions and answers, search queries | Only what each request needs, while the server’s AI reads pages when asked (on unless an owner turns it off) | Yes |
| Who is a member and their role; guests; who has which private or shared tree | Yes: the server enforces access | Yes |
| How many documents there are, and which tree each page belongs to | Yes, as opaque IDs | Yes |
| Each document’s and file’s size, to within padding (§5.7) | Yes | Yes |
| When each device pushes to which document, and from which address | Yes | Yes |
| Loro peer IDs (random 64-bit numbers), and which device each belongs to | Yes | Yes |
| Page views, if an owner turns Analytics on (off by default when encrypted) | By document ID only | Yes |
| Published pages | Yes: publishing uploads a readable copy | Yes |
| What an agent reads | Only while an agent presents a token a member made for it (§6.9), in that request’s memory. | Yes, through /mcp |
So an operator learns a workspace’s shape: who works with whom, how many pages there are, how big they are and when they change. Not what they say.
4. Keys
Recovery key (24 words; only you hold it) └─ opens → Account root key (ARK), versionedDevice key pairs (one per device; the private half never leaves it) └─ receive the ARK when another of your devices approves themARK └─ seals → the account's identity keys: • signing key pair (Ed25519): certifies devices, signs key changes • encryption key pair (X-Wing: X25519 + ML-KEM-768): receives container keys • the account container seed: your own sealed document (Ask chats)Container keys (32 bytes, one per epoch), one per: • encrypted workspace (its workspace document and its pages) • private or shared tree (private pages, pages shared with some people, guests' pages) └─ wrapped to each member's account encryption key (HPKE) └─ each epoch seals the epoch before it, so history stays readableDocument keys = HKDF(container key at epoch e, container ‖ e ‖ document): derived, never storedAttachment keys: random per file, kept in the block that shows the file- The root key is the one thing a device needs. A new device gets it once, from another of your devices or from the recovery key, and unwraps everything else from the server.
- The recovery key is a key pair. The 24 words are the private half. The public half is stored with the account, so a device can wrap a new root key to it without knowing the words.
- Container keys are wrapped per person, not per device. Adding a device touches no workspace.
- Document keys are derived. Rotating a container’s key changes every page’s key at once.
- Agent tokens never get the root key. They get only the current keys of the workspaces they were made for, wrapped to their own key (§6.9).
Where keys live: inside each device’s SQLCipher store, whose own key is in the platform’s key store (Keychain, Credential Manager, Secret Service, Keystore), or for the browser wrapped by a non-extractable WebCrypto key in its OPFS store. Keys in memory are zeroed when dropped. Signing in with any method (password, passkey, OIDC, magic link, an operator’s recovery link) never gives keys: they come only from an approved device or the recovery key.
5. Formats
Every format starts with a format byte (1) and a suite byte (1). Suite 1 is
XChaCha20-Poly1305, Ed25519 and HKDF-SHA256, with HPKE (RFC 9180, base mode) using X-Wing,
HKDF-SHA256 and ChaCha20-Poly1305 for wraps. IDs are 16-byte UUIDs. Integers are big-endian.
Every key derivation and signature uses a label of its own (drusite … v1), so no key or
signature made for one purpose is valid for another.
5.1 Sealed entry (entry.rs)
One Loro update or snapshot of an encrypted document, as the server stores it.
format u8 ‖ suite u8 ‖ kind u8 (update | snapshot) ‖ workspace [16] ‖ document [16] ‖ document kind u8 ‖ container [16] ‖ epoch u32 ‖ device [16] ‖ through_seq u64 ‖ peers: u16 count, u64 each ‖ sealed Loro bytes: u32 length, nonce [24] ‖ ciphertext ‖ tag [16] ‖ sealed version vector (snapshots only): u32 length, nonce ‖ ciphertext ‖ tag ‖ signature [64]- The header (everything before the sealed parts) is in clear, so the server checks it
without a key. It’s the associated data of both sealed parts (labels
drusite entry data v1anddrusite entry version v1), so an entry moved to another document, container, epoch, device or peer set doesn’t open. - Sealed with the document key, under a random 192-bit nonce. The Loro bytes are padded first.
- The signature is the device’s Ed25519 signature over every byte before it, framed as
drusite signature v1\0 ‖ purpose (1: entry) ‖ bytes.
5.2 Wraps (wrap.rs)
A key, or a small message, sealed to someone’s X-Wing public key with HPKE:
format u8 ‖ suite u8 ‖ encapsulated key [1120] ‖ ciphertextHPKE’s info is drusite wrap v1\0 ‖ purpose u8 ‖ context, so a wrap can’t be replayed for
another purpose or place:
| Purpose | Value | Context |
|---|---|---|
| Root key to a device (approval) | 1 | user, root key version, device |
| Root key to the recovery key | 2 | user, root key version |
| Container key to a member | 3 | container, epoch, user, their key version |
| (retired in 1.2.1; never reused) | 4 | |
| A server-made result to an account (opt-in meeting notes) | 5 | job, user, their key version |
| Container key to an agent token (§6.9) | 6 | container, epoch, token, 0 |
The epoch chain: each epoch’s key seals the previous epoch’s key (associated data drusite epoch chain v1, the container and the epoch), so whoever holds the current key can read the
whole history and nobody needs a wrap per epoch.
5.3 Identity (identity.rs)
- Account keys sealed by the root key: the signing seed, the encryption seed and the
container seed, sealed with XChaCha20-Poly1305 under the root key (associated data
drusite account keys v1, the user and version). - Device certificate: the account’s signature over the user, the account key version that signs it, the device ID, the device’s signing and encryption public keys, when it was made, and whether it’s an agent token’s (§6.9). Devices check every entry’s signer against a certificate from that person’s current or a previous key version.
- Key change: a new key version, signed by the previous signing key, so the people you share with accept it without a warning.
5.4 Safety codes
30 digits in six groups of five, from 5,200 rounds of SHA-512 over drusite safety code v1\0,
the signing key and the user’s ID, as Signal makes its half of a safety number. Devices pin each
person’s signing key the first time they see it. A later key must be signed by the previous one.
One that isn’t shows “safety code changed”, and nothing is shared with it until someone accepts
it.
5.5 Approving a new device (sas.rs)
Numeric comparison with a commitment, the pattern of Bluetooth pairing and ZRTP:
- The new device makes its keys and a random nonce, and sends its public keys with a
commitment, a hash (
drusite approval commitment v1\0) of its nonce and keys. - The approving device sends a random nonce of its own.
- The new device reveals its nonce, and the approving device checks it against the commitment.
- Both show a six-digit code from HKDF-SHA256 (
drusite approval code v1) over both keys and both nonces. The person compares the codes and approves. The approving device then wraps the root key to the new device (purpose 1) and certifies it.
The new device is bound to its nonce before it sees the approver’s, so a server in the middle gets one guess at a six-digit code, one in a million, rather than a search. A request lasts 10 minutes.
5.6 The recovery key (recovery.rs)
32 random bytes written as 24 words from the BIP-39 English list (the last word carries a
checksum). They’re the seed (HKDF, drusite recovery key v1) of an X-Wing key pair. The server
keeps the public half and the root key wrapped to it (purpose 2). A passphrase was ruled out: a
passphrase-wrapped key on the server could be guessed offline by its operator.
5.7 Padding (pad.rs)
Everything sealed is padded to at least 512 bytes, then with Padmé, which reveals O(log log L) bits of a length L at a cost of at most 12%.
5.8 Attachments (file.rs)
STREAM over XChaCha20-Poly1305 in 64 KiB chunks, each authenticated and the last marked as last, so a file can’t be cut short, reordered or spliced. Each file has its own random key, kept in the block that shows it, inside the sealed page.
format u8 ‖ suite u8 ‖ nonce prefix [19] ‖ chunk 0: length and padded length, u64 each (+ tag) ‖ chunks 1…n: the padded content, 64 KiB each (+ tag), the last marked lastThe file’s ID is the BLAKE3 hash of the sealed bytes, so the server can still check a file is stored under its own name. Chunk 0 comes first so audio and video can seek.
6. Protocols
6.1 Sync (protocol 2)
Encrypted documents sync over drusite.sync.v2. Devices push sealed entries; the server stores
them as they are. Before storing one, the server checks, without keys:
- the header’s workspace, document and container are where the push says;
- the epoch is the container’s current one;
- the device is a certified, signed-in device of a member who may edit there;
- the signature verifies;
- the declared Loro peers belong to that device (a peer ID can’t be claimed by two devices).
Devices check the same things and also open the entry: one that doesn’t open, or whose signer isn’t certified, is quarantined, never imported.
Keys travel in sync too: the container keys a connection needs (wrapped to the member), and the certificates of the devices whose entries it gets.
6.2 Compaction and history
The server can’t merge ciphertext, so devices compact: a device writes a sealed snapshot of a
document and the server replaces the log it covers. A device checks afterwards that the snapshot
covers what it knows the server held up to the snapshot’s sequence number: entries the server
sent it, and its own pushes the server stored, through through_seq. Changes logged after a
snapshot aren’t in it and needn’t be; 1.2.0 counted them, and reported good snapshots as bad.
Replaced entries are kept 14 days, so a bad snapshot can be healed. Version history survives: it’s
in the Loro document.
The server takes a reported snapshot away only if every entry it replaced is still there (from
the previous snapshot, or from the first entry). Otherwise the snapshot is the only copy of the
document’s start, and it’s kept. A document whose log doesn’t start at 1 and has no snapshot
covering the rest (as 1.2.0 could leave one) is healed by the next device that holds it: when
a device that may edit it subscribes, the server asks it for a snapshot (compact_wanted), lifts
any bad_compactor mark, and the device’s snapshot of everything it has becomes the base again.
A snapshot declares no peers, and is trusted as written by a member who may edit. A member with the key could hide forged changes in one. This is listed for the independent review.
6.3 Sharing, removal and rotation
The server can’t make a wrap, so devices do the key work. When a device that may share connects,
the server sends it KeyWork: people waiting for a container’s key, or a key due to rotate. It
answers with ShareKeys (wraps of the current key to the people waiting) or Rotate (a new
epoch, sealing the previous one, wrapped to exactly the people with access). The server checks
both: wraps only to people with access, and a new epoch to exactly them.
A key is due to rotate when someone loses access: removed from the workspace (every container in it rotates), taken off a page (that page’s tree), a device removed, an agent token revoked or rotated, or an account deleted. The first member’s device online does it.
Moving a page between the workspace and a private or shared tree is done by a device: it re-seals the page under the destination’s key, and the server deletes the old log, which people who are losing access could read.
6.4 Knowing whose key it is
See §5.4. Comparing safety codes is optional. Without it, a server that lies about a public key when someone is invited could receive the wrap meant for them.
6.5 One copy of the notes
An encrypted workspace exists sealed on the server and whole on its members’ devices, and nowhere
else. Agents don’t get a copy: they reach it through the server’s /mcp with a token that
carries its key (§6.9), or through the desktop app’s local MCP, which serves the app’s own data.
(1.2.0 also had headless processes with their own synced copy; 1.2.1 removed them, signed out any
that were enrolled, and replaced the keys they held.)
6.6 Converting a workspace
An owner turns encryption on or off in Workspace settings. The server can’t seal or open anything, so the members’ devices convert it: each converts the documents it has whole and may edit, sending a sealed (or readable) snapshot that the server switches one document at a time. Files are converted with their pages. Editing never stops. Turning it off sends readable snapshots back, and the server’s own at-rest encryption takes over.
6.7 Deleting an account
Deleting an account deletes its keys, the wraps made to it, its sign-ins, its agent tokens, and the workspaces only it was in. It’s refused while it’s the last owner of a workspace others are in. The workspaces it leaves rotate their keys, as when someone is removed, and so do pages shared with it as a guest.
6.8 The server’s AI, by choice
In an encrypted workspace, the device finds the blocks for search by meaning and Ask. The server’s
AI reads pages when asked, so Ask works as in any workspace: the device sends only what that
request needs, and the server keeps nothing it read. An owner can turn that off for the workspace
(Settings → AI); then only a model on the device answers. A server with [e2ee] server_ai = false
never reads any. Meeting notes made that way come back sealed to the person (purpose 5).
6.9 Agent tokens that carry their key
An agent can use an encrypted workspace through the server’s own /mcp, as it uses a standard
one, with a token a member’s device made. There’s still one copy of the notes, the sealed one on
the server: no mirror, and no readable copy, index, embedding or cache kept
anywhere.
Making it (Settings → Agents → Create token, on a device that holds the keys):
- The device draws 32 random bytes, the token’s secret. The token is
dra_and the secret in base64url, shown once to the person and never sent to the server at this point. - From the secret it derives two key pairs with HKDF-SHA256, each under its own label
(
drusite agent token signing v1,drusite agent token encryption v1): an Ed25519 signing key and an X-Wing encryption key (drusite-e2ee’stoken.rs). - It certifies the token’s public keys with the account’s signing key, as a device’s (§5.3,
with
agentset), naming the token’s ID as the device. - It wraps each chosen workspace’s current key to the token’s encryption key (purpose 6).
- It sends the token’s ID, the BLAKE3 hash of its text (how the server looks tokens up, as for every access token), the certificate and the wraps. The server checks the certificate against the account’s current key, that the wraps are of each workspace’s current epoch, and that the person holds it.
On each request the server looks the token up by its hash, as for any token, then derives the token’s private keys from the token presented, opens its newest wrap of the workspace’s key, walks the epoch chain back as far as the documents need, and opens only the entries the tool call reads, checking each one’s signature against its device’s certificate as a device does. Edits are sealed under the current epoch and signed with the token’s own key, and the server’s checks of a push (§6.1) hold for them as for any device. Search opens and scans the pages in the request; no index or embedding is made. The keys are wiped when the request ends, and the decrypted bytes are dropped with it. If a key doesn’t unwrap, the call fails: nothing falls back to anything readable.
- What a dump gives: the wraps, the public keys, the certificate and the token’s hash. The hash is BLAKE3 of the token’s text; the keys are HKDF of its raw secret under other labels, so neither gives the other, and nothing on the server opens a wrap without the token.
- What it reaches: the workspaces chosen, and in them only what every member can (the workspace’s own key, never a private or shared tree’s). Another workspace holds no wrap to it, so naming it in the token’s access gets nothing.
- What it may do (read only or edit, journal, moving pages to the trash, publishing) is set when it’s made and changed in Settings → Agents; the server reads it afresh on every call, and never allows more than the person’s role.
- Key changes: the device that rotates a workspace’s key (§6.3) also wraps the new key to every live token whose certificate it can check against its person’s pinned key. One left out (by an older app) gets it later from a device that may share the key. Revoking a token, or its running out, deletes its wraps at once and makes the key due to rotate, without it.
- Visibility: members see each token that can read their workspace, and whose it is. Every
change it makes is in What agents changed, with its summary sealed under the workspace’s key
(
drusite agent activity key v1); the server can’t undo one, so a member’s device does. - Publishing a page uploads a readable copy of that page (§3), as publishing from a device does.
- Logs: the MCP library logs requests and results in full at debug level, so the server holds
it at
warnwhatever the log level.
The honest limit: this trusts the running server for the length of each request. Whoever controls the server while an agent uses it could capture the token as it arrives and read that workspace, now and later; and whoever holds the token can read it, through the server. A database or backup taken alone opens nothing. Keep a token to the workspaces an agent needs, and revoke or rotate one you no longer trust.
Rotating it (Settings → Agents → Rotate, POST /api/v1/agent-tokens/<id>/rotate) gives the
same entry (its name, permissions, workspaces and expiry) a new secret. The device does what
making a token does: a new secret, new key pairs derived from it, a new certificate, and wraps of
the current key of each encrypted workspace the old token held a key of. In one transaction the
server checks those as for a new token, stores them under a new token ID, revokes the old token,
deletes its access token and wraps, and marks those workspaces’ keys due to rotate, so the old
secret can’t open anything written after. The old certificate stays, so the old token’s signed
edits still verify.
7. What the browser can and can’t promise
The browser is a device like any other, with keys in its OPFS store that are wiped on sign-out. Against a curious operator or a stolen database or backup, it’s as safe as the apps. Against an operator who serves modified JavaScript, it isn’t, and Settings → Security says so the first time an encrypted workspace opens in a browser.
8. Limits
- Withheld updates. A server can hide one device’s edits from another. A document whose updates never arrive shows that some changes haven’t arrived, and devices check after a snapshot that the server still has what they pushed.
- Removed people keep what they had. Rotation protects what’s written after the removal. It can’t reach into a copy they already have.
- Viewers hold the history. A viewer’s device has the whole document, history included.
- Metadata (§3) is visible to the server, by design: it enforces access.
- Losing every device and the recovery key loses your private pages and the encrypted workspaces only you are in, for good. Shared ones can be given back by their other members.
9. Test vectors and tests
- Golden vectors:
crates/drusite-e2ee/vectors/v1.txt, made bycrates/drusite-e2ee/tests/vectors.rsfrom a deterministic random source (drusite-e2ee test vectors v1), for every format above: wraps, the epoch chain, account keys, device certificates, key changes, safety codes, approval codes, recovery words, sealed entries, sealed files and padding. CI checks them natively on Linux, macOS and Windows and as wasm, so every platform agrees byte for byte. Runcargo test -p drusite-e2ee. - Properties (
tests/props.rs): seal then open round-trips; any changed byte, associated data, epoch or signer fails; padding round-trips; derived keys differ per document and epoch. - Fuzz targets (
crates/drusite-e2ee/fuzz): every parser (sealed entry, file, identity, recovery words, wraps). They need nightly Rust andcargo-fuzz, and haven’t been run for long yet. - End to end:
crates/drusite-server/tests/e2ee.rsruns real devices against a real server and PostgreSQL: setup, approval, sync, rotation, private pages and guests, and conversion; a snapshot whose replaced log is gone, which is kept, and a document that lost its base, healed by a device (a_snapshot_whose_log_is_gone_…); and an agent token on an encrypted workspace (an_agent_token_uses_…), through reading, search, edits, the trash, permissions, a key rotation, rotating the token and revocation, which checks that a dump of the database, the object store and logs at every level hold none of it.