TExcelTAD

X25519 and Forward Secrecy

Suppose an attacker records all the encrypted traffic to your web server for a year, and then steals the server's private key. With old-style TLS (RSA key exchange), every recorded session can now be decrypted: the session keys were sent encrypted to that key. With forward secrecy, the stolen key is useless for the past, because each session's key came from a fresh Diffie–Hellman exchange whose secrets were deleted when the session ended. Today that exchange is almost always X25519.

X25519

X25519 (RFC 7748) is Diffie–Hellman on Daniel Bernstein's Curve25519, a Montgomery curve over the prime 2255−192^{255} - 19. A private key is 32 random bytes; the public key is a 32-byte xx-coordinate; the shared secret is 32 bytes. It was designed so that the obvious implementation is the safe one:

  • every 32-byte string is a valid public key, so there is no "is this point on the curve?" check to forget (the invalid-curve attacks that hit some ECDH implementations don't apply);
  • the Montgomery ladder computes the scalar multiplication in constant time with a fixed sequence of operations;
  • private keys are "clamped" (a few bits set or cleared) so small-subgroup tricks don't reveal key bits.

It is the most used key exchange on the Internet: the default in TLS 1.3 implementations (and the classical half of the hybrid post-quantum groups browsers now prefer), SSH (curve25519-sha256, and inside OpenSSH's hybrid post-quantum defaults), WireGuard, Signal and age. The sandbox's TLS sessions show it as Server Temp Key: X25519, 253 bits.

On the RFC test keys

The example uses the published key pairs of RFC 7748 section 6.1 (lab use only) and checks the shared secret against the RFC. The raw shared secret is never used directly as a key: it goes through a KDF (here HKDF with a context label) to produce the session key.

python · try it yourself
from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
from cryptography.hazmat.primitives import hashes
# RFC 7748 section 6.1 test keys (published: lab use only)
alice = X25519PrivateKey.from_private_bytes(bytes.fromhex("77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a"))
bob = X25519PrivateKey.from_private_bytes(bytes.fromhex("5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb"))
A = alice.public_key().public_bytes_raw(); B = bob.public_key().public_bytes_raw()
print("Alice public:", A.hex()[:32] + "...", len(A), "bytes")
s1 = alice.exchange(bob.public_key()); s2 = bob.exchange(alice.public_key())
print("shared secret:", s1.hex())
print("both sides equal:", s1 == s2)
print("matches RFC 7748:", s1.hex() == "4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742")
key = HKDF(algorithm=hashes.SHA256(), length=32, salt=None, info=b"lab chat v1").derive(s1)
print("session key (HKDF):", key.hex()[:32] + "...")
Alice public: 8520f0098930a754748b7ddcb43ef75a... 32 bytes
shared secret: 4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742
both sides equal: True
matches RFC 7748: True
session key (HKDF): d4d4f35fd3b72013d16a9cf7ec7b4a79...

In real use both sides call X25519PrivateKey.generate() for every session, exchange public keys inside an authenticated handshake, derive keys, and delete the private keys.

Forward secrecy

RSA key exchange (TLS 1.2 TLS_RSA_*) session 1 session 2 server key stolen recorded sessions 1 and 2 decrypted: their keys were encrypted to the stolen key ephemeral (EC)DHE, e.g. X25519 (TLS 1.3) session 1 session 2 server key stolen recorded sessions stay secret: their DH secrets were deleted; the key only signs

A protocol has forward secrecy (sometimes "perfect forward secrecy") when compromising long-term keys does not reveal past session keys. The recipe:

  1. Each session uses fresh, ephemeral DH key pairs (the "E" in ECDHE).
  2. The long-term key is used only to authenticate (sign the handshake), never to encrypt session keys.
  3. Ephemeral secrets and session keys are erased when no longer needed.

The Diffie–Hellman lab (finite-field DH with toy numbers instead of a curve, the same idea) shows the recipe at work. Treat each setting of a and b as one session: with p=997p = 997, g=5g = 5, the session with a=11a = 11, b=29b = 29 agrees on the key 532, and a later one with a=23a = 23, b=8b = 8 on 993. Nothing long-term enters either calculation, so once both sides delete a and b, a recording of AA and BB can only be attacked by Eve's discrete logarithm, which is instant for this toy prime and hopeless for X25519. The Mallory switch shows the other half of the recipe: the long-term key is still needed to sign the key shares.

Alicesecret a = 11Bobsecret b = 29Eve (listens)sees the wire onlypublic: p = 997, g = 5 (order of g mod p = 332, not a generator)A = 5^11 mod 997 = 48828125 mod 997 = 50B = 5^29 mod 997= 200A = 50B = 200receives B = 200receives A = 50s = 200^11 mod 997 = 532s = 50^29 mod 997 = 532shared secret: both have s = g^(ab) mod p = 532Eve saw p, g, A = 50, B = 200. Trying x = 1, 2, 3 … she finds g^x ≡ A at x = 11(11 tries). With a 2048-bit p that search is out of reach.A passive listener is beaten; an active one is not (turn Mallory on).

TLS 1.3 has forward secrecy for every full handshake. TLS 1.2 had it only with ECDHE_* or DHE_* suites; TLS_RSA_* suites encrypt the premaster secret to the certificate's RSA key, so anyone who later gets that key, through a breach, a court order or a vulnerability such as Heartbleed (2014), can decrypt recorded traffic.

Where forward secrecy is weaker than it looks

  • Session resumption: TLS session tickets are encrypted with a ticket key on the server. If that key is long-lived and stolen, resumed sessions (and in TLS 1.2 the original ones) can be decrypted. Rotate ticket keys every few hours, or use resumption with a fresh (EC)DHE (TLS 1.3 psk_dhe_ke).
  • 0-RTT early data in TLS 1.3 is encrypted under a key derived from the resumption secret only, so it lacks forward secrecy for that first flight and can be replayed.
  • Messaging: Signal's double ratchet goes further, giving forward secrecy per message and "post-compromise security" (recovery after a key leak).

Quantum computers and "harvest now, decrypt later"

X25519 is an elliptic-curve DH, so a large quantum computer would break it. Traffic recorded today could be decrypted then; forward secrecy doesn't help against that, because the attacker breaks each recorded ephemeral exchange directly. That is why browsers and servers have started deploying the hybrid X25519MLKEM768 key exchange (Post-Quantum Migration).

How missing forward secrecy is detected

  • TLS scans of your own servers: any accepted TLS_RSA_WITH_* suite; Server Temp Key missing in openssl s_client -brief output for TLS 1.2; testssl.sh's "forward secrecy" section.
  • Configuration: long-lived session-ticket keys (nginx ssl_session_ticket_key files that never change), shared across a fleet for months.
  • Protocol design reviews: custom protocols where a client encrypts a session key to the server's long-term public key.
  • Key exchange inventory: ssh -Q kex, IPsec IKE proposals (DH groups 14, 19, 20, 31 = Curve25519 are fine; groups 1, 2, 5 are not).

How it is fixed

  • Prefer TLS 1.3; in TLS 1.2 allow only ECDHE suites with AEAD.
  • Rotate or disable session-ticket keys; let the TLS library manage them.
  • In custom protocols: ephemeral X25519 for every session, authenticated with signatures or known static keys, keys derived with HKDF and erased after use (or use Noise, a framework that does this correctly, as WireGuard does).

Pitfalls

  • Reusing an "ephemeral" key for many sessions to save CPU: forward secrecy then only covers the period until that key is replaced.
  • Using the raw X25519 output as an AES key: always run it through a KDF with the transcript or context.
  • Confusing X25519 (key exchange) with Ed25519 (signatures).

Practice

1. A server recorded in 2023 used TLS_RSA_WITH_AES_128_GCM_SHA256. Its private key leaked in 2026. Which traffic is exposed?

All recorded sessions that used RSA key exchange with that certificate key, back to when the key was first used: the premaster secrets were encrypted to it. Sessions that used ECDHE suites stay protected (unless session-ticket keys also leaked).

2. Why does X25519 not need a "point is on the curve" check while ECDH on P-256 does?

X25519 uses only the xx-coordinate with a Montgomery ladder, and every 32-byte value corresponds to a point on the curve or its twist, both of which have large prime-order subgroups; clamping handles the small cofactors. P-256 implementations that accept arbitrary (x,y)(x, y) must validate them, or invalid-curve attacks leak key bits.

3. Change the HKDF info label in the example. What happens to the session key, and why is that useful?

It changes completely. Different labels give independent keys from the same shared secret, so one exchange can safely produce separate keys for each direction or purpose.

Test yourself

What does forward secrecy guarantee?

  1. Messages arrive in order
  2. Stealing a long-term key later does not reveal past session keys
  3. The server is authenticated
  4. Quantum resistance

Which TLS 1.2 cipher suite lacks forward secrecy?

  1. TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
  2. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
  3. TLS_RSA_WITH_AES_128_GCM_SHA256
  4. TLS_DHE_RSA_WITH_AES_256_GCM_SHA384

How long are X25519 public keys and shared secrets?

  1. 16 bytes
  2. 32 bytes
  3. 64 bytes
  4. 256 bytes

Why is the raw X25519 shared secret passed through HKDF?

  1. To make it shorter
  2. To derive uniformly random, context-bound keys from it
  3. Because X25519 output is public
  4. To sign it

In the lab, two sessions use fresh secrets (a = 11, b = 29, then a = 23, b = 8) and agree on 532 and then 993. What gives this forward secrecy?

  1. The prime 997 is secret
  2. Each session's secrets are ephemeral and deleted, and no long-term key takes part in computing the session key
  3. Bob reuses b
  4. Eve cannot see A and B