📚 Encyclopedia

Security & Cryptography · updated 2026-11-02

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

  1. Key generation: X25519().newKeyPair() (32-byte private, 32-byte public). The private key stays on the device; the public key is uploaded.
  2. Shared secret: ECDH(my_private, their_public). ECDH is commutative, so the sender can decrypt its own messages with no plaintext cache.
  3. 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.
  4. 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, tagged message_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

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

See also

Linked from

Sources: tldr memory (feature-e2e-dm, feature-dm-keyring, incident-dm-key-wipe-recovery, completed-work, feedback_dm_simplicity), tldr memory (feature-voice-calls), post #1861