Diffie-Hellman

Diffie-Hellman key agreement allows two parties to establish shared secret key material over a public channel without transmitting the shared secret itself. The parties can then pass the result through a key derivation function (KDF) to produce keys for symmetric encryption.

Key Agreement and Key Transport

In key agreement, both parties contribute information and independently derive the same shared secret. Neither party selects the final secret or transmits it across the network. Diffie-Hellman is a key-agreement algorithm.

In key transport, one party generates the shared secret and securely sends it to the other party. Only the sender selects the secret; the recipient recovers the value rather than contributing to its creation.

Finite-Field Diffie-Hellman

Alice and Bob agree on a public prime number pp and generator gg. Alice chooses a private value aa, and Bob independently chooses a private value bb.

There are published sets of pp and gg that are cryptographically secure. For example, RFC 7919 specifies standardized finite-field DH groups called ffdhe2048, ffdhe3072, ffdhe4096. Generally, we use one of these existing sets instead of creating our own.

Computing gamodpg^a \bmod p is known as modular exponentiation, and is the basis of this algorithm. It can be performed efficiently even when the numbers are large.

The following sequence shows the complete exchange:

sequenceDiagram
    participant Alice
    participant Bob

    Alice->>Alice: Choose private value a
    Alice->>Alice: Calculate A = g^a mod p
    Bob->>Bob: Choose private value b
    Bob->>Bob: Calculate B = g^b mod p
    Alice->>Bob: Public value A
    Bob->>Alice: Public value B
    Alice->>Alice: Calculate B^a mod p
    Bob->>Bob: Calculate A^b mod p

The pair (a,A)(a, A) is Alice’s DH key pair: aa is the private key, and A=gamodpA = g^a \bmod p is the corresponding public key. Protocol descriptions may also call AA a DH public value or DH key share. Similarly, Bob’s key pair is (b,B)(b, B).

Both calculations produce the same shared secret:

(gamodp)bmodp=(gbmodp)amodp=gabmodp(g^a \bmod p)^b \bmod p = (g^b \bmod p)^a \bmod p = g^{ab} \bmod p

The private values aa and bb never cross the network.

Discrete Logarithm Problem

Recovering aa from gamodpg^a \bmod p is an instance of the discrete logarithm problem (DLP):

gamodpag^a \bmod p \longrightarrow a

For appropriately chosen cryptographic groups and parameters, no efficient classical algorithm is known for solving the discrete logarithm problem.

Computational Diffie-Hellman Problem

An attacker does not need to recover either private key if they can calculate the shared secret directly. Given the two public values, the Computational Diffie-Hellman (CDH) problem is to calculate gabmodpg^{ab} \bmod p:

gamodp, gbmodpgabmodpg^a \bmod p,\ g^b \bmod p \longrightarrow g^{ab} \bmod p

Solving the DLP would also solve the CDH problem because recovering either private key makes it possible to calculate the shared secret.

Diffie-Hellman relies directly on the CDH problem being computationally infeasible.

Selecting pp and gg

The modulus pp is chosen as a large prime. In the simple full-group form of DH, the possible outputs range from 11 through p1p - 1, so a larger pp provides a larger search space and makes attacks on the discrete logarithm problem more difficult.

After choosing pp, the generator gg must be selected to generate a sufficiently large subgroup. In the simple full-group construction, gg can be a primitive root modulo pp, meaning that gxmodpg^x \bmod p can produce every value from 11 through p1p - 1 as xx varies. Practical DH parameter sets may instead deliberately use a large prime-order subgroup. A poor choice of gg generates only a small subgroup and reduces the effective search space regardless of how large pp is.

For example with p=19p = 19, different choices of gg produce the following sets as the exponent xx varies:

GeneratorPossible values of gxmod19g^x \bmod 19Number of values
g=1g = 1{1}\{1\}11
g=4g = 4{1,4,5,6,7,9,11,16,17}\{1, 4, 5, 6, 7, 9, 11, 16, 17\}99
g=3g = 3{1,2,,18}\{1, 2, \ldots, 18\}1818

The choice g=1g = 1 is unusable because 1xmodp=11^x \bmod p = 1 for every exponent, so every shared secret would be known in advence (which could be used in brute force attacks). The choice g=4g = 4 generates a subgroup containing only half of the available nonzero values. By contrast, g=3g = 3 is a primitive root modulo 1919: its powers generate every nonzero residue from 11 through 1818.

Man-in-the-Middle Attacks

Diffie-Hellman does not authenticate either party and is therefore vulnerable to a man-in-the-middle (MITM) attack. The attacker replaces both public values and establishes a separate shared secret with each party, allowing them to decrypt messages received through one connection and re-encrypt them for the other.

Preventing this attack requires authentication that binds each DH public value to the expected identity. A digital signature is one way for a protocol to provide that binding.

Static and Ephemeral Keys

A static DH key is a private and public key pair reused across multiple DH exchanges. The same key might be reused when connecting to many peers, or it might be maintained specifically for repeated communication with one peer. The defining property is reuse.

An ephemeral DH key is generated for one key exchange and is not reused in later exchanges. Once the DH shared secret has been calculated, the ephemeral private key is no longer needed and can be discarded immediately, potentially before the network connection closes.

The DH shared secret is passed through the KDF to produce the keys used by the protocol. The DH shared secret can be discarded after those keys have been derived. The derived traffic keys remain available while they are needed and are discarded when their connection or key epoch ends.

Forward Secrecy

Using fresh ephemeral keys and deleting their private components enables forward secrecy. It ensures that compromising a long-term private key does not allow an attacker to recover session keys from previously completed sessions.

Forward secrecy therefore limits the historical data exposed when a long-term key is compromised.

In the context of static DH, the reused secret key aa is the long-term key. If an attacker records Bob’s public value B=gbmodpB = g^b \bmod p and later obtains aa, they can calculate Bamodp=gabmodpB^a \bmod p = g^{ab} \bmod p and recover the shared secret. Therefore, static DH does not provide forward secrecy.

Elliptic-Curve Diffie-Hellman

Elliptic-Curve Diffie-Hellman (ECDH) performs the same key-agreement role as finite-field Diffie-Hellman but uses a different mathematical group. It replaces modular exponentiation with elliptic-curve operations that have the same important property: deriving a public key and shared secret is efficient, but reversing the operation to recover a private key is computationally infeasible.

The exchange retains the same overall shape. Each party generates a private key, derives and exchanges a corresponding public key, and combines its private key with the other party’s public key to obtain the same shared secret.

ECDH provides comparable security with much smaller parameters and public keys than finite-field DH:

Key agreementTypical parameter or key sizeApproximate security
Finite-field DH2048 bits112 bits
Finite-field DH3072 bits128 bits
ECDH with a 256-bit curve256 bits128 bits

The smaller values reduce network bandwidth and storage and can improve performance, depending on the implementation and platform.

Similar to finite-field Diffie-Hellman, ECDH parameters are selected from standardized, reviewed sets rather than created by application developers.

ECDH performed with ephemeral keys is called Ephemeral Elliptic-Curve Diffie-Hellman (ECDHE).