Sealing mail so my own server can't read it
Email arrives in plain text, like all email. Milliseconds later, Keyhaven has sealed it to a key that only my devices have. Here's the envelope, what it protects, and why the alias addresses themselves stay readable.

Email is not encrypted on its way in. The sender’s server hands Cloudflare a readable message, and Cloudflare hands it to my Worker, still readable. I can’t change that part. Nobody running a mailbox can.
What I can control is what gets stored. In Keyhaven, the goal was: the copy that sits in the database can only be opened by my own devices, and the Worker that writes it never has the key to open it again.
A padlock anyone can close
The trick is public-key crypto. When I created my account, my device made an ECDH P-256 key pair:
- the public key is stored on the server in plain sight. The Worker needs it, and it’s fine for anyone to have it
- the private key is encrypted with my vault key, so it only exists in readable form on a device I’ve unlocked
Think of the public key as a padlock I hand out. Anyone can snap it shut. Only I have the key that opens it.
Sealing one message
For every message, the Worker does this:
- makes a brand-new one-time key pair, just for this message
- combines its one-time private key with my public key (ECDH) into a shared secret
- runs that secret through HKDF-SHA256 with a fixed label to get a one-time AES-256-GCM key
- encrypts the message, keeps the one-time public key in the envelope, and forgets the rest
To open it, my device combines my private key with the one-time public key from the envelope. ECDH gives the same shared secret from either side, so it gets the same AES key. It’s the same idea as the “sealed box” in other crypto libraries, built only from what WebCrypto offers.

The envelope is small and boring, which is how I like it:
{
"v": 1,
"alg": "ECDH-P256-HKDF-SHA256-A256GCM",
"epk": "<the one-time public key>",
"iv": "<12 random bytes>",
"ct": "<the sealed message>"
}
v and alg are there so the format can change one day; the code that opens it refuses anything it doesn’t know. And because it’s AES-GCM, a single changed byte anywhere makes opening fail instead of showing something wrong.
Two seals per message
Each mail is actually sealed twice:
- a small summary (sender, recipient, subject, date), so the inbox list can be shown by opening a few small seals instead of downloading every body
- the raw message itself, as bytes, cut into chunks in the database (how that works)
Mail I send from an alias is kept in my Sent folder, sealed exactly the same way. Drafts are encrypted too, with my vault key.
What it protects, and what it doesn’t
It protects everything that’s stored. A copy of the database, someone browsing it, the logs: all they find is envelopes. Even with the Worker’s code and the whole database, the stored mail stays shut, because the private key is there only encrypted under my vault key: opening it means guessing my master password.
It doesn’t protect:
- the moment of arrival. Cloudflare and the Worker see the message in memory for those few milliseconds, as any mail host does
- mail I send. It has to pass Brevo, my sending service, readable. That’s how email works. And since 1.3.0 shows whether each sent mail was delivered, the server keeps a few plain facts next to the sealed copy: Brevo’s message id, the recipients’ addresses, the status and Brevo’s reason. Brevo has all of those anyway; the text of the mail stays sealed
- the sender’s side. Their mail server has its own copy
So this isn’t end-to-end from the sender, PGP-style. It’s “from the moment it arrives, only my devices can read it”. And one more honest point: one key opens all of it. If my mail private key ever leaked, every stored mail could be opened. That’s why it never leaves my devices unencrypted.
Why the addresses stay plain
Before the Worker can seal anything, it has to answer two questions about every message: is this address one of my aliases? and is it turned on? That’s a lookup by address, on every mail, done by code that holds no key at all. And the address has already passed through Cloudflare in plain text as part of delivering the mail.
So alias addresses are stored in plain text. What I attach to an alias is not: which site it’s for, my note, the login it belongs to. Those are encrypted with my vault key, with the address as associated data, so a note can’t be quietly moved to another alias.
The server therefore knows which alias got mail, when, and how big it was. It doesn’t know what was in it.
Encrypt everything the server doesn’t need. Be honest about the little it does.
Showing it safely
Opening happens on my device, and HTML mail can be hostile, so it’s shown in a sandboxed frame with scripts switched off and its own strict rules: no network at all until I press Load images, and then images only. Script tags, base tags and auto-refresh tags are removed first. Links open in a new tab without telling the site where I came from.
That also means tracking pixels don’t fire just because I opened a newsletter. Since 1.3.0 they don’t fire even when I do press Load images: a 1×1 picture, a hidden one, or one whose address looks like an open-tracker is left out either way, and the mail says how many it dropped.