Simple E2E messaging with X25519 + HKDF + AES-GCM (and why not Signal)
e2ex25519ecdhhkdfaes-gcmsignal-protocoldartphpopenssl
TLDR's direct messages are end-to-end encrypted with a deliberately simple, stateless scheme: one static X25519 key pair per user, a per-conversation AES key derived by ECDH plus HKDF, and AES-256-GCM per message. It replaced the Signal Protocol after a week of state bugs (v1.24.0, March 2026). It trades forward secrecy for robustness, which suits a small app with a low threat model.
Protocol
- Key generation:
X25519().newKeyPair()(32-byte private, 32-byte public). The private key stays on the device; the public key is uploaded. - Shared secret:
ECDH(my_private, their_public). ECDH is commutative, so the sender can decrypt its own messages with no plaintext cache. - Conversation key:
HKDF-SHA256(shared, salt = empty, info = "tldr-dm-v1:<userA>:<userB>", length 32)with the two usernames sorted, so both sides derive the same key. - Message: AES-256-GCM with a fresh random 12-byte nonce.
Wire format
base64(nonce[12] || ciphertext || mac[16]), stored as raw bytes server-side, taggedmessage_type = 3(1 and 2 were the old Signal formats, shown as "[Encrypted with previous protocol]").
The server stores public keys and ciphertext only. With the Dart cryptography package the whole
client is about 220 lines.
Why Signal Protocol was dropped
libsignal_protocol_dart (X3DH + double ratchet) caused, within days: session state corruption
between ratchet steps, the sender unable to read its own sent messages (needed a plaintext cache),
prekey generation/upload/replenishment and atomic consumption, keys lost across restarts, and races
between concurrent encrypt/decrypt. For a hobby app the stateful design was the bug source.
Stateless ECDH removed every one of those classes.
Trade-offs, stated plainly
- No forward secrecy: the conversation key is static. If a private key leaks, all past messages in its conversations are readable. Per-message ephemeral keys could be added later.
- All-or-nothing per conversation: a missing key means the whole conversation is unreadable. This is why key backup matters more than anything else, see E2E key management that never loses a key (keyring, backup, re-wrap).
- Push notifications cannot contain message text; they say "New message from X".
- UI decisions that follow from E2E: no message previews in the list and no unread counts (each
would mean decrypting every conversation on the list screen); a boolean "has new" dot based on a
per-user
last_read_atinstead.
Verifying a key against real ciphertext (forensics)
When you need to know whether a candidate private key decrypts history:
- PHP without libsodium: PHP 8.3's openssl_pkey_derive() does X25519 if you wrap raw 32-byte
keys in RFC 8410 DER. Private prefix 302e020100300506032b656e04220420 + 32 bytes; public prefix
302a300506032b656e032100 + 32 bytes. Then
hash_hkdf('sha256', $shared, 32, "tldr-dm-v1:<u1>:<u2>", '') and
openssl_decrypt($ct, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $nonce, $tag).
- Authoritative check: run the app's real crypto library in a throwaway script
(dart run tool/x.dart) against exported rows. It removes any doubt about your reimplementation.
- Rule out the other side rotating keys before blaming yours, but note that a "last updated"
timestamp on a public-key row may just mean "last app open" if the app upserts on every launch.
Reusing DM keys to seal call signaling
TLDR's voice calls (v1.63.0, see 1:1 WebRTC voice calls over polled signaling and TURN) seal every WebRTC signal (offer, answer,
end) with AES-GCM under a key derived from the two users' X25519 DM keys, with an HKDF info string
of the form tldr-call-v1:<a>:<b>:<callId>. Because the info is per call, each call gets its own
key, separate from the message key. The signal type travels inside the ciphertext, so the server
cannot even tell an offer from a hangup.
- Why it matters: the SDP carries the DTLS certificate fingerprint. A server that could read or
rewrite plaintext SDP could swap in its own fingerprint and sit in the middle of the DTLS-SRTP
media. With sealed signaling, the API and the TURN relay only see opaque blobs and SRTP.
- Version the envelope (v: 1) and reject anything shorter than nonce + tag (28 bytes) before
trying to decrypt.
Gotchas
- Validate public key length (32 bytes) before constructing a key object; corrupted keys crash.
- Order DM routes so literal paths (
/dm/prekeys/count) are registered before patterns (/dm/prekeys/{username}). - Poll by message id, not timestamp, and guard against a send/poll race inserting duplicates.
- Renaming a user breaks their DMs. The HKDF info string contains both usernames (group key wraps include usernames too), so after a rename every existing DM becomes undecryptable. The password-wrapped private key uses a random salt, so recovering the key at login still works. Found when a TLDR user was renamed on 2026-10-22.
See also
- E2E key management that never loses a key (keyring, backup, re-wrap)
- E2E group chat with per-epoch sender keys
- Debug installs wipe app data, and recovering it with Android Auto Backup