X25519 shared secrets and why HKDF is not optional
In one line: How X25519 produces a 32-byte shared secret, how to check both sides agree, and how HKDF-SHA256 derives purpose-specific keys.
X25519 lets two parties compute the same secret independently. They exchange only public keys; the secret never travels. What it produces, however, should not be used as an encryption key directly.
1. The calculation is one line
shared = X25519(my_private_key, peer_public_key)
Internally it runs a Montgomery ladder for 255 iterations; from the outside it takes 32 bytes and returns 32 bytes. Panel ④ shows exactly that.
2. Symmetry
Alice: X25519(a, B) = K Bob : X25519(b, A) = K
Alice's private key with Bob's public key equals Bob's private key with Alice's public key. If that breaks, key agreement does not work, so it is the first thing to check. Generating a Bob pair in panel ④ compares both directions automatically.
3. Why not use the output directly
X25519 output is not uniformly random: it is a point-derived value with possible bit bias, and reusing one shared secret across purposes means one compromise reaches all of them. Standards therefore require a KDF.
| Step | What happens |
|---|---|
| Extract | Combine the shared secret and salt into a PRK |
| Expand | Stretch the PRK with info into a key of the required length |
4. HKDF-SHA256
PRK = HMAC-SHA256(salt, shared_secret) OKM = HMAC-SHA256(PRK, info ‖ 0x01) ‖ … (as many blocks as needed)
Salt is optional but helps domain separation. Info states what the key is for: info = "app:session:v1" and info = "app:backup:v1" produce unrelated keys from the same secret, so leaking one does not expose the other.
5. Practical checklist
- Did you confirm the peer public key really belongs to the peer (signature or certificate)?
- Did the shared secret go straight into a KDF instead of storage?
- Does each purpose use different info?
- Does the derived length match the algorithm (32 bytes for AES-256)?
- Are key pairs fresh per session rather than reused for months?
6. Trying it in Ed25519Lab
Generate an Alice pair and a Bob pair in panel ④ and both the shared secret and the symmetry result appear. Change salt or info and the derived key changes: one character in info is enough to produce a completely different key from the same secret.
Making a key yourself shows the format differences immediately.
Open Ed25519LabFrequently asked questions
Should the shared secret be new each time?
Use ephemeral key pairs per session. That is what TLS 1.3 does, and it limits how much past traffic a future key compromise can expose.
What belongs in salt and info?
Salt can be a public random value or a protocol-defined constant; info is a string naming the purpose. Neither needs to be secret.
References
- RFC 7748 — Elliptic Curves for Security (X25519)
- RFC 5869 — HMAC-based Extract-and-Expand Key Derivation Function (HKDF)
- NIST SP 800-56A — Recommendation for Pair-Wise Key-Establishment Schemes
Read next
- Ed25519 and X25519: one curve, two jobsWhat each of the two Curve25519 algorithms does, and why a key made from the same seed is not interchangeable between them.
- One key, five notations: raw, hex, Base64url, JWK, PEMHow a single 32-byte key becomes hex, Base64url, JWK, PKCS#8 PEM and SPKI PEM, and which form belongs where.
- Ed25519 signing and verification, step by stepFollow the RFC 8032 signing procedure to see why the signature is deterministic and why verification fails on a single changed bit.