Every encrypted connection between services starts with two machines agreeing a secret key over a network that an adversary may be watching, and a weak or misconfigured key exchange undermines everything built on top of it. Key exchange is the step in TLS, SSH, IPsec and messaging protocols where that shared secret is created. Diffie-Hellman and its elliptic curve form (ECDH) have done this job for decades, and post-quantum key encapsulation (ML-KEM) is now being combined with them. This guide explains how key exchange works, why ephemeral exchange gives forward secrecy, what goes wrong in practice and how hybrid post-quantum groups fit in. It is written for engineers and architects who run machine-to-machine encryption, and it complements the Post-Quantum Migration for NHIs Guide.
Key takeaways
- Key exchange creates the session key; authentication proves who you agreed it with. Diffie-Hellman alone does not stop an adversary in the middle; certificates, host keys or pre-shared keys do.
- Ephemeral exchange gives forward secrecy. With ECDHE a stolen server private key does not decrypt past recorded sessions, which is why RFC 9325 says static RSA and static DH suites should not be negotiated.
- Use named, standard groups. X25519, P-256 and the RFC 7919 FFDHE groups avoid the weak and custom parameters behind past attacks.
- Hybrid post-quantum exchange is now standardised for TLS 1.3. RFC 10024 (August 2026) defines X25519MLKEM768 and two NIST-curve hybrids built on FIPS 203 ML-KEM.
- Machine identities depend on this step. Workload mTLS, SSH automation and API traffic all inherit the strength of the negotiated group, so key exchange belongs in the cryptographic inventory.
What it is
A key exchange (or key establishment) scheme lets two parties derive the same secret value without ever sending it. NIST SP 800-56A Rev. 3 specifies the pair-wise schemes based on discrete logarithms: Diffie-Hellman and MQV, over finite fields (FFC) and elliptic curves (ECC). The derived secret is fed into a key derivation function to produce symmetric keys, which then protect data with an AEAD cipher. See the Authenticated Encryption and Block Cipher Modes Guide for that second stage.
There are three families in common use:
- Finite field Diffie-Hellman (DH, DHE): each side picks a private exponent, publishes g raised to that exponent modulo a large prime p, and raises the other side's value to its own exponent. Security rests on the discrete logarithm problem in that group.
- Elliptic curve Diffie-Hellman (ECDH, ECDHE): the same idea on an elliptic curve group, with much smaller keys for similar strength. X25519 uses 32-byte keys and targets about 128-bit security (RFC 7748 describes it as slightly under that level); X448 uses 56-byte keys and targets about 224 bits.
- Key encapsulation mechanisms (KEMs): one side publishes an encapsulation key, the other generates a random secret and sends it encrypted as a ciphertext. FIPS 203 (Final, 13 August 2024) specifies ML-KEM in three parameter sets, ML-KEM-512, ML-KEM-768 and ML-KEM-1024, whose security rests on the Module Learning with Errors problem and is believed to resist quantum adversaries. NIST SP 800-227 (Final, September 2025) gives recommendations for using KEMs.
Static, ephemeral and key transport compared
| Approach | Example | Forward secrecy | Status in modern TLS | Main risk |
|---|---|---|---|---|
| RSA key transport | TLS_RSA_* suites in TLS 1.2 | No | RFC 9325: should not be negotiated; absent from TLS 1.3 | Server key compromise decrypts all recorded traffic |
| Static DH or ECDH | TLS_DH_*, TLS_ECDH_* suites | No | RFC 9325: should not be negotiated | Long-lived secret reused across sessions |
| Ephemeral finite field DH | TLS_DHE_* in TLS 1.2; ffdhe groups in TLS 1.3 | Yes | RFC 9325: TLS_DHE_* should not be negotiated in TLS 1.2; 2048-bit minimum | Custom or small groups, cost of large moduli |
| Ephemeral elliptic curve DH | ECDHE with X25519 or P-256 | Yes | Preferred in RFC 9325; default in TLS 1.3 | Quantum attack on recorded traffic in future |
| Hybrid ECDHE plus ML-KEM | X25519MLKEM768 (RFC 10024) | Yes | Proposed Standard for TLS 1.3, codepoint 0x11EC | Larger handshakes; middlebox and library support |
How it works
Inside a TLS 1.3 handshake
- ClientHello: the client lists supported groups (for example X25519MLKEM768, X25519, secp256r1) and sends a key share for one or more of them.
- ServerHello: the server picks a group and returns its own share. Both sides compute the shared secret; for a hybrid group they compute both the ML-KEM and ECDH secrets and concatenate them (64 bytes for X25519MLKEM768 under RFC 10024).
- Key schedule: the secret is fed through HKDF to derive handshake and traffic keys. From here everything is encrypted.
- Authentication: the server, and in mTLS the client, signs the handshake transcript with its certificate key. This is what binds the exchanged secret to an identity and defeats an adversary in the middle (MITRE ATT&CK T1557).
SSH follows the same pattern: an ephemeral exchange such as curve25519 produces session keys, and the server's host key signature authenticates it. See the SSH Protocol and Certificate Authority Guide.
Group sizes and wire costs
| Group | Source | Client share | Server share | Notes |
|---|---|---|---|---|
| X25519 | RFC 7748 | 32 bytes | 32 bytes | Fast, simple, constant-time designs available |
| secp256r1 (P-256) | NIST curve | 65 bytes uncompressed | 65 bytes | Widely required in regulated and FIPS settings |
| ffdhe2048 to ffdhe8192 | RFC 7919, codepoints 256 to 260 | 256 to 1,024 bytes | Same | Safe primes with generator 2; estimated about 103 to 192-bit strength |
| X25519MLKEM768 | RFC 10024, 0x11EC (Recommended) | 1,216 bytes | 1,120 bytes | ML-KEM-768 encapsulation key plus X25519 share |
| SecP256r1MLKEM768 | RFC 10024, 0x11EB | 1,249 bytes | 1,153 bytes | Uses P-256 for environments that require NIST curves |
| SecP384r1MLKEM1024 | RFC 10024, 0x11ED | 1,665 bytes | 1,665 bytes | Higher security category, larger handshake |
Hybrid client shares above about 1,200 bytes can push the ClientHello across more than one TCP packet, which has exposed middleboxes and servers that assumed it would fit in one. Test through load balancers, proxies and inspection devices before enabling hybrids broadly. The live Hybrid Post-Quantum TLS Key Exchange Guide covers rollout in detail.
Validating what the peer sends
- Finite field: RFC 7919 requires each side to check that the peer's public value Y satisfies 1 < Y < p minus 1 and abort otherwise, and says clients accepting custom groups must reject moduli under 768 bits and should reject those under 1,024 bits.
- X25519 and X448: RFC 7748 notes that small-order inputs can force an all-zero shared secret; implementations may check for this and abort, and protocol designers must not assume both parties contributed to the secret.
- Side channels: RFC 7748 asks for constant-time implementation, with no memory access or branches that depend on key bits.
Why it matters
Key exchange failures are silent. A connection with a weak group still shows a padlock, still passes health checks and still carries production traffic. Three properties are at stake:
- Forward secrecy: with ephemeral keys, compromise of a long-term private key (a TLS certificate key or SSH host key) lets an attacker impersonate the service from that point, but not decrypt sessions recorded earlier. RSA key transport and static DH lose this property.
- Harvest now, decrypt later: traffic recorded today with classical ECDHE could be decrypted by a future quantum computer. Data with a long confidentiality life (health, financial, government, long-lived credentials sent over the wire) is the reason hybrid ML-KEM groups exist now, before signatures need to change.
- Machine identity trust: service meshes, workload identity systems and API gateways establish thousands of sessions per second between non-human identities. Their key exchange settings are usually inherited from a library or platform default, so one weak default spreads everywhere. See the mTLS Guide.
Choosing and rolling out
- Inventory negotiated groups on every endpoint (public, internal, service mesh, SSH) with a scanner or handshake logs, and record them in the Cryptographic Inventory and CBOM Guide format.
- Remove non-forward-secret options: disable TLS_RSA_*, static DH and static ECDH suites, and TLS_DHE_* in TLS 1.2, in line with RFC 9325.
- Set a classical baseline: X25519 and P-256 for ECDHE; ffdhe2048 or larger only where finite field DH is required.
- Pilot hybrids on a canary ring of clients and services with X25519MLKEM768 first in the preference list and X25519 as fallback. Measure handshake failures and latency.
- Widen in rings (internal service mesh, then partner APIs, then public edge), keeping the classical fallback until telemetry shows negligible failures.
- Roll back by reordering the group preference list to classical first; no certificate or key change is needed, which makes this an unusually safe migration step.
Small organisations mostly inherit this from their CDN, cloud load balancer and operating system updates; the job is to confirm the setting and avoid pinning old libraries. Large organisations need policy (approved groups per tier), central TLS libraries and per-environment exceptions for appliances that cannot yet support hybrids. Regulated environments should check which groups their validated modules support; see the FIPS 140-3 and Cryptographic Module Validation Guide.
Detection and measurement
- Log the negotiated group and protocol version on load balancers, proxies and service mesh sidecars.
- Alert on any session using non-forward-secret key exchange or a downgrade from the configured preference (ATT&CK T1600 Weaken Encryption covers adversaries forcing weaker cryptography).
- Metrics: percentage of sessions with forward secrecy (target 100%); percentage of internal and external sessions using a hybrid PQ group; number of endpoints still offering static RSA or DH suites; handshake failure rate after enabling hybrids; count of appliances on the exception list and their planned upgrade dates.
Framework mapping
| Framework | Reference | Relevance |
|---|---|---|
| NIST SP 800-53 Rev. 5 | SC-12 Cryptographic Key Establishment and Management | Approved key establishment schemes and groups |
| NIST SP 800-53 Rev. 5 | SC-13 Cryptographic Protection | Algorithm choice, including ML-KEM |
| NIST SP 800-53 Rev. 5 | SC-8 Transmission Confidentiality and Integrity | Protected channels between services |
| NIST CSF 2.0 | PR.DS-02 | Data in transit protected |
| NIST standards | SP 800-56A Rev. 3, FIPS 203, SP 800-227 | Scheme specifications and KEM guidance |
| MITRE ATT&CK | T1557, T1600, T1040 | Interception, forced weakening and sniffing that key exchange must withstand |
Common misconceptions
- "Diffie-Hellman stops man-in-the-middle attacks." Unauthenticated DH lets an attacker run two exchanges, one with each side. Only authentication of the transcript prevents it.
- "A bigger certificate key makes the session stronger." Session confidentiality depends on the negotiated group, not the certificate key size. A 4,096-bit RSA certificate with a weak DH group is still weak.
- "Post-quantum means replacing certificates first." Key exchange is the urgent part because recorded traffic can be decrypted later; signature migration can follow. See the ML-DSA and SLH-DSA Post-Quantum Signatures Guide.
- "ML-KEM is just Diffie-Hellman with new maths." It is a KEM: one side encapsulates a secret to the other's key. Protocols that relied on DH properties, such as both parties contributing, need redesign rather than a drop-in swap.
- "Hybrid is a temporary hack." Hybrids keep classical security if a new scheme is later weakened, which is why RFC 10024 marks X25519MLKEM768 as Recommended for TLS 1.3.
- "Custom DH parameters are safer because they are unique." RFC 7919 exists because clients cannot cheaply verify arbitrary groups; standard safe-prime groups are the safer choice.
Related concepts
- Authenticated encryption: what the derived keys are used for. See the Authenticated Encryption and Block Cipher Modes Guide.
- TLS handshake and configuration: see the How TLS Works Guide and the TLS Server Configuration Baseline Guide.
- End-to-end messaging: protocols such as Signal extend DH into ratchets for per-message keys. See the End-to-End Encryption and Messaging Protocols Guide.
- Crypto-agility: group preference lists are the most agile part of TLS. See the Crypto-Agility Guide.
Practitioner checklist
- Inventory negotiated key exchange groups across public, internal, mesh and SSH endpoints.
- Disable RSA key transport, static DH and static ECDH suites everywhere.
- Standardise on X25519 and P-256, with ffdhe2048 or larger only where needed.
- Validate peer public values as RFC 7919 and RFC 7748 describe, using maintained libraries.
- Pilot X25519MLKEM768 with classical fallback and test through every middlebox.
- Log negotiated groups and alert on non-forward-secret or downgraded sessions.
- Prioritise hybrid exchange for traffic carrying long-lived secrets and machine credentials.
- Track appliance exceptions with owners and upgrade dates.
Standards and references
- NIST: SP 800-56A Rev. 3 Pair-Wise Key-Establishment Schemes Using Discrete Logarithm Cryptography
- NIST: FIPS 203 Module-Lattice-Based Key-Encapsulation Mechanism Standard
- NIST: SP 800-227 Recommendations for Key-Encapsulation Mechanisms
- IETF: RFC 10024 Post-Quantum Traditional Hybrid Key Agreement for TLS 1.3
- IRTF: RFC 7748 Elliptic Curves for Security
- IETF: RFC 7919 Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
- IETF: RFC 9325 Recommendations for Secure Use of TLS and DTLS (BCP 195)
- IETF: RFC 8446 The Transport Layer Security Protocol Version 1.3
Related NHI Mgmt Group resources: Post-Quantum Migration for NHIs Guide · Hybrid Post-Quantum TLS Key Exchange Guide · mTLS Guide · Cryptographic Key Management Guide