Skip to content

How the encryption works

This page is the technical reference for the security model. The whole cryptographic core lives in a single browser-and-Node file, e2e.js, using WebCrypto and a vendored WebAssembly build of Argon2id (hash-wasm).

The password never leaves the browser.

  1. Before login, the browser calls POST /auth/prelogin with the username and receives a 16-byte salt and the KDF parameters. For unknown usernames, the server returns a deterministic fake salt (HMAC(server secret, username)) so the endpoint doesn’t reveal whether an account exists.
  2. The browser checks that the KDF parameters meet its floor (argon2id, at least 19 MiB memory and 2 passes; default is 64 MiB, 3 passes, parallelism 1) and derives a 32-byte master key with Argon2id.
  3. HKDF-SHA-256 splits master into two independent 256-bit keys:
    • authKey (info = "hytte/auth/v1"): sent to the server as the login secret. The server verifies it against a stored bcrypt hash.
    • wrapKey (info = "hytte/wrap/v1"): AES-256-GCM key that never leaves the browser.

When an account is created, the browser generates two key pairs:

  • X25519 for key agreement (wrapping and unwrapping channel keys).
  • Ed25519 for signing and verifying messages.

The private keys (PKCS#8) are JSON-encoded and sealed with wrapKey using AES-256-GCM (random 12-byte IV, AAD "hytte/bundle/v1|" + lowercase(username)). Only the wrapped bundle and the raw public keys are uploaded to the server.

After login (and after 2FA, if enabled), the browser unwraps the bundle and re-imports the private keys as non-extractable CryptoKey objects:

  • On a normal device, they are kept in IndexedDB until logout so reloads, push decryption and the on-device search index work without re-entering the password.
  • With Public computer ticked at login, they are handed off to the app once and kept in memory only; reloading asks for the password again.

A recovery key is 32 random bytes shown as 52 base32 characters in groups of four. HKDF-SHA-256 (info = "hytte/recovery/v1") derives an AES-256-GCM key that wraps a second copy of the private-key bundle, stored alongside the password-wrapped one.

When you register a passkey that supports the WebAuthn PRF extension, the authenticator’s 32-byte PRF output goes through HKDF-SHA-256 (info = "hytte/prf/v1") to wrap another copy of the bundle. That lets passwordless passkey sign-in unlock your keys on a new device without typing the password.

Every conversation (public channel, private channel, DM or group) has a random 256-bit AES-GCM key per epoch, starting at epoch 1.

When a member wraps a channel key for another member (or for themselves):

shared = X25519(sender_priv, recipient_pub)
info = "hytte/chkey/v1|" + channel_id + "|" + epoch + "|" + sender_key_id + "|" + recipient_key_id
kek = HKDF-SHA-256(shared, info)
wrap = AES-256-GCM(kek, raw_channel_key, aad = info)

Static-static X25519 authenticates the sender, and the AAD binds the wrapped key to the exact channel, epoch and both identity key IDs.

  • Joining: when someone joins a channel, is invited, or resets their account with a new identity key, the server records a key request. Any online member’s browser wraps all epochs of that conversation for the newcomer’s public key and uploads them.
  • Leaving or removal: the server marks the channel for rotation. The next member online generates a fresh 256-bit key for epoch + 1, wraps it for the remaining members, and commits it with a compare-and-swap on the epoch. The server rejects new messages encrypted under a stale epoch.

Each message (and each edit) is encrypted with the conversation’s current channel key and signed with the author’s Ed25519 key.

  • Plaintext: a JSON object containing the message text, optional encrypted attachment descriptors (att) and optional link preview data.
  • AAD: "hytte/msg/v1|" + channel_id + "|" + epoch + "|" + parent_id + "|" + sender_key_id (parent_id is 0 for top-level messages). This prevents the server from replaying a ciphertext into another channel, thread or author.
  • Ciphertext: AES-256-GCM with a random 12-byte IV.
  • Signature: Ed25519 over AAD || 0x00 || IV || ciphertext.
  • Wire format: E2: followed by base64 of:
{"v": 2, "e": 1, "k": 42, "iv": "...", "ct": "...", "sig": "..."}

The server rejects any message that doesn’t start with E2: or doesn’t use the channel’s current epoch. When displaying a message, the browser decrypts it and verifies sig against the Ed25519 public key for identity k. If verification fails, the UI flags the message.

Each channel key derives an HMAC key via HKDF-SHA-256 (info = "hytte/react/v1"). For a reaction on msg_id:

  • Grouping tag: first 16 bytes of HMAC-SHA-256(react_key, "hytte/react/v1|" + msg_id + "|" + emoji), base64-encoded.
  • Encrypted emoji: AES-256-GCM(channel_key, emoji, aad = "hytte/react/v1|" + msg_id).

The server groups reactions by (msg_id, tag) and enforces one reaction per user per tag without learning which emoji it is.

Each uploaded file is encrypted in the browser with a fresh random 256-bit AES-GCM key (aad = "hytte/file/v1"). The server receives only opaque bytes (application/octet-stream) and assigns a random hex filename. The download URL, decryption key, original filename, MIME type and size travel inside the encrypted message envelope.

Link previews are fetched by the sender’s browser through the server and included in the encrypted message envelope, with thumbnails encrypted and uploaded like files.

Two features store data encrypted so only your own identity can read it:

  • Notification keywords (stored on the server) and the on-device search index (stored in IndexedDB).
  • The key is derived via X25519(own_priv, own_pub) → HKDF-SHA-256(shared, "hytte/self/v1|" + purpose + "|" + key_id) → AES-256-GCM.

Because this key is tied to your identity key pair, losing both your password and your recovery key makes self-sealed data unreadable.

Each user’s half of a safety number is 30 digits:

  1. Build input = "hytte/safety/v1|" + lowercase(username) + "|" || pub_x25519 || pub_ed25519.
  2. Iterate h = SHA-512(h || input) 5,200 times (starting from empty h).
  3. Take the first 30 bytes in six 5-byte chunks, interpret each chunk as an unsigned big-endian integer modulo 100000, and zero-pad to 5 digits.

The two users’ 30-digit halves are sorted alphabetically by username and joined into 12 groups of 5 digits, so both people see the exact same 60 digits.