Lorenzo Wynberg
← Writing

Holding no keys: the encryption behind Kuentas

How Kuentas encrypts a business's books on the phones that keep them: keys per compartment, a signed roster of who's allowed in, rotation when someone leaves, and a server that relays everything and can read none of it.

Lorenzo Wynberg · November 20, 2026 · 8 min read

CoreOrdersTeamSignature: still valid after the server withholds parts

A small business's books say a lot about it: what it earns, who owes it, what it owes, what it pays its people. Most apps protect that with "it's encrypted in our database". That stops a stolen disk. It doesn't stop whoever runs the service, a breached server, or someone on the inside.

For Kuentas I wanted something stronger: the server holds no key that could read the books, and nothing about that rests on our word. The customer version of this story is on the Kuentas blog, How we encrypt your books. This is the engineering version: the keys, who holds them, and what happens when the team changes.

The rule everything follows

The server stores and relays ciphertext. It never decrypts, and it holds no key that could. That's not only a policy: lint rules keep the decrypting functions out of the server code, so a change that tried to open a record there fails the check.

Everything below is how the phones manage keys among themselves so that rule can hold.

The keys, from the person down

A person

12 words

BIP-39, 128 bits → Argon2id (64 MiB, 3 passes)

Passkey

PRF output → HKDF-SHA-256

Account key

32 random bytes, one per person

Identity

Ed25519 + X25519: who you are in every book

A book

Roster

signed chain of who's in, with which role, on which devices

Core keygeneration n

every book record: sales, stock, loans, savings, receipts

Orders keygeneration n

a dispatcher's copy of each order, and its status

Team keygeneration n

the team's own records, like invites

Every record

sealed under its compartment's current key; the header names key and generation

A device

Device keys

Ed25519 + X25519, non-extractable in the browser

Device certificate

signed by the identity it belongs to

App lock

PIN (Argon2id) or passkey → lock key → a data key per book

Local database

SQLite, ChaCha20-Poly1305 on every page

No key in this picture ever reaches the server. A grant carries a compartment key sealed to one identity and signed by the owner who gave it.

A person has one account key: 32 random bytes. It's what lets you come back on a new phone, so it's wrapped two ways:

  • by your 12 words (BIP-39, 128 bits), stretched with Argon2id at 64 MiB and 3 passes;
  • by each passkey you register, from the passkey's PRF output through HKDF-SHA-256.

The account key wraps your identity, an Ed25519 signing key and an X25519 key for receiving sealed things. Your identity is who you are in every book you belong to.

A book has a roster (who's in, with which role, on which devices) and three compartments, each with its own key. Every record is sealed with XChaCha20-Poly1305 directly under its compartment's current key. There's no per-record key to manage: the record's 38-byte header names the compartment, the key id and the key's generation, and that header is bound in as additional authenticated data, so it can't be altered without breaking the seal.

A device has its own Ed25519 and X25519 keys, created as WebCrypto keys the browser won't let anyone read out, and a certificate signed by the identity it belongs to. On the device, the app lock (a PIN through Argon2id, or a passkey) derives a data key per book, and the local database itself is SQLite encrypted with ChaCha20-Poly1305, page by page.

Three compartments, four roles

Not everyone on a team should see everything. A delivery driver needs the orders, not the books. So a book's records are split across three compartments:

  • Core: every book record. Sales, expenses, stock, loans, savings, receipts.
  • Orders: a copy of each order as a dispatcher reads it, written beside its sale by whoever records it, and its dispatched status.
  • Team: the team's own records, like invites.

A role is simply which compartment keys a person holds:

CoreNo key

Sealed. This phone holds no key for it, and the server withholds it.

OrdersKey held

A copy of each order as a dispatcher reads it, and whether it went out

TeamNo key

Sealed. This phone holds no key for it, and the server withholds it.

Reads the orders and marks them dispatched. Nothing else in the books opens on this phone.

A key reaches a person as a grant: the compartment key, sealed to their identity's X25519 key with crypto_box_seal, and signed by the owner who granted it. A grant also names the book it's for, so it can't be replayed into another one.

Withholding without breaking the signature

Here's the part I like most. When a phone commits changes, it signs them, and that signature has to verify on every other phone. But a dispatcher's phone shouldn't even receive the Core records it can't decrypt.

If the signature covered the records, the server couldn't remove any without breaking it. So it doesn't. A commit carries up to three parts, one per compartment, and the signature covers each part's compartment and digest, not its contents.

As the owner's phone signed it

Core: sealed records…
Orders: sealed records…
Team: sealed records…

As a dispatcher's phone receives it

Core: digest only32 bytes
Orders: sealed records…
Team: digest only32 bytes

Signed: "kuentas/commit/v3" ‖ book ‖ log ‖ device ‖ seq ‖ roster ‖ prev hash ‖ each part's compartment and digest

The signature never covered the contents, only their digests. Swap a part for its digest and the signature still checks out, so the server can withhold what a role can't read without being able to forge anything. A phone refuses a withheld part its own role should have received.

That lets the server do the one useful thing it can do without any keys: hand each phone only the parts its role can read, replacing the rest with their digests. The signature still verifies. And it cuts the other way too: a phone checks that anything withheld from it is something its role isn't allowed, so a server can't quietly starve a helper of the books.

The roster: who's allowed, provably

If the server can't decide who's on the team, who does? The owners' devices, through a signed chain.

  1. #0pinned

    Book created

    Owner · Marta

  2. #1signed ✓

    Adds Luis as helper

    signed by Marta's phone

    prev: hash(#0)

  3. #2signed ✓

    Adds Ana as dispatcher

    signed by Marta's phone

    prev: hash(#1)

  4. #3signed ✓

    Revokes Luis's old phone

    signed by Marta's laptop

    prev: hash(#2)

A phone trusts a roster only from a link hash it already holds, the book's pin, and checks every link after it: signed by an active device of an owner, chained to the one before, and never leaving the book without an owner.

The roster is a chain of signed links, nine kinds in all: the book's genesis, adding or removing a member, changing a role, adding or revoking a device, adding or rotating a compartment key, and replacing a member's identity. Each link is signed with Ed25519 under the context kuentas/roster/v2, and names the hash of the link before it.

Two rules keep it honest:

  • A phone trusts a roster only from a link hash it already holds, the book's pin, which it got when it joined. A server can't hand it a different history from the start.
  • Every link must be signed by an active device of an owner, and the chain can never leave a book without an owner.

When someone leaves: rotation

Removing someone from the roster stops future grants. But they still have the keys they were given. So when someone is removed, loses a role, or has a device revoked, every compartment they held becomes pending rotation.

Core

key generation

3 · pending

Orders

key generation

5 · pending

Team

key generation

2

Ana's outbox

Ana marks an order dispatched; it waits

An owner's phone adds a roster link removing Luis. He held keys for Core and Orders, so both compartments are now pending rotation.

While a compartment is pending, nobody writes to it: anything sealed under the old key could still be read by the person who left. Saves don't fail; they wait in the outbox. An owner's device then makes the next generation of each key, records it in the roster with a commitment, and grants it to every device still allowed to hold it. The waiting saves go out under the new key.

The person who left keeps what they could already read. That's an honest limit of any system like this: you can't un-show someone a record. What rotation guarantees is that nothing written after they left opens for them.

Joining a phone, and getting back in

Pairing a new phone works without the server ever seeing a key. The new phone shows a 10-character code (Crockford base32, 50 bits) derived from a secret only it holds. Run through Argon2id, that code gives a public commitment and a proof key. A phone you already trust checks the commitment and sends back a proof carrying what the new phone needs, tagged and signed by your identity. The invite itself carries no pin, on purpose: it travels by email, through the server, so a pin inside it would only be the server's word. A joining phone takes its pin from an owner's signed pairing proof alone.

Recovering an account with your 12 words or a passkey works on a new phone, but there's a deliberate 48-hour wait before it completes, with a "No fui yo" (it wasn't me) link to cancel it. Someone who steals your words still has to get past two days of you being told.

Formats you can pin down

Every byte format (commit, grant, roster link, snapshot, enrollment) starts with a version byte, and every signature and derived key is bound to a context string: there are 39 of them, from kuentas/commit/v3 to kuentas/grant/v2. A signature made for one purpose can't be passed off as another.

The formats are checked against committed test vectors, and older versions were deleted outright rather than kept around, because none had ever shipped. The only formats in the code are the ones in use.

Assume I turn evil

The right threat model for a service that holds someone's books is that the service itself is the attacker. That means me: the person who runs the server, writes the code and holds the deploy keys. So I tried to break it from the server's side. Pick an attack and see what stops it.

server@kuentas · hostile mode
$ choose an attack

The last one isn't stopped, and I'd rather say so than bury it. A web app runs whatever code its origin serves, so a server willing to ship malicious JavaScript could read what the app reads. Every protection above assumes the app itself is honest. The real fix is native apps with code you can verify, and that's not shipped yet.

What the server can see

The server sees

  • Which compartment, key id and generation each record is under
  • Which device sent each commit, when, and how big it was
  • The roster: members, roles and devices
  • Your account, team names and the book's name and settings

The server never sees

  • Any key: account, identity, compartment or device
  • Any amount, name, product, customer or note in your books
  • Your 12 words, PIN or passkey secret
  • Receipt photos and PDFs
Metadata is the honest limit: the server can tell when and how much each phone syncs, and who's on the team.

The honest limit is metadata. The server can see when and how much each phone syncs, which compartment and key generation each record is under, and who's on the team with which role. It can't see what's in the books.

What it adds up to

The design comes down to three moves:

  1. Keys live on the people and devices that need them, wrapped by things only those people have.
  2. Authority comes from signatures, not from the server: the roster, the grants, and every commit are signed and checked on every phone.
  3. The server keeps the jobs that need no keys: storing, ordering, and withholding by role. Those it can do. It can't read, forge, or decide who's allowed.

The sync engine is built on the same idea. If you want to see how phones agree on what happened without the server deciding, read A sync engine that never merges.