opyebEd25519Lab

X25519 shared secrets and why HKDF is not optional

Key exchange · 7 min read

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.

StepWhat happens
ExtractCombine the shared secret and salt into a PRK
ExpandStretch 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 Ed25519Lab

Frequently 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

Read next

0 bytes of your input sent to a server (advertising and affiliate widgets excluded)