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, and agent devices they add, hold keys.
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.
- The host of an agent device you add. It holds keys for what you gave it, by design.
- 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 | No, unless an owner lets the server’s AI read pages when asked | 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 | No, through the server. An agent device reads what you gave it, on a host you choose. | 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), versioned
Device key pairs (one per device; the private half never leaves it)
└─ receive the ARK when another of your devices approves them
ARK
└─ 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 readable
Document keys = HKDF(container key at epoch e, container ‖ e ‖ document): derived, never stored
Attachment 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 devices never get the root key. They get only the container keys of what they were given, wrapped to their own device key.
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] ‖ ciphertext
HPKE’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 |
| Container key to an agent device | 4 | container, epoch, device, 0 |
| A server-made result to an account (opt-in meeting notes) | 5 | job, user, their key version |
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 device. 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 last
The 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 server still has everything it pushed. Replaced entries are kept 14 days, so a bad snapshot can be healed. Version history survives: it’s in the Loro document.
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 device removed, 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 Agent devices
An agent device is a headless Drusite (drusite agent run) that serves MCP for encrypted
workspaces from a host you choose. You make an enrollment code in Settings → Agents, choosing
the workspaces it may read, whether it may also read your private and shared pages, and whether
it may edit. It enrolls with the code (one use, 15 minutes), you approve it with the six-digit
code, and your device wraps only those workspaces’ container keys to its device key (purpose 4).
It never gets the root key. Agents reach it with tokens from your Drusite server, which the agent
device checks with the server. Removing it revokes its sign-in and rotates every key it 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 devices, 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, search by meaning and Ask run on the device. An owner can let the server’s AI read pages when asked: the device then sends only what that request needs. The server keeps nothing it read. Meeting notes made that way come back sealed to the person (purpose 5).
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, agent devices, and conversion.