General overview Diffie–Hellman key exchange establishes a shared secret between two parties that can be used for secret communication for exchanging data over a public network. An analogy illustrates the concept of public key exchange by using colors instead of very large numbers: The process begins by having the two parties,
Alice and Bob, publicly agree on an arbitrary starting color that does not need to be kept secret. In this example, the color is yellow. Each person also selects a secret color that they keep to themselves – in this case, red and cyan. The crucial part of the process is that Alice and Bob each mix their own secret color together with their mutually shared color, resulting in orange-tan and light-blue mixtures respectively, and then publicly exchange the two mixed colors. Finally, each of them mixes the color they received from the partner with their own private color. The result is a final color mixture (yellow-brown in this case) that is identical to their partner's final color mixture. If a third party listened to the exchange, they would only know the common color (yellow) and the first mixed colors (orange-tan and light-blue), but it would be very hard for them to find out the final secret color (yellow-brown). Bringing the analogy back to a
real-life exchange using large numbers rather than colors, this determination is computationally expensive; it is impossible to compute in a practical amount of time even for modern
supercomputers.
Cryptographic explanation The simplest and the original implementation, of the protocol uses the
multiplicative group of integers modulo p, where
p is
prime, and
g is a
primitive root modulo p. To guard against potential vulnerabilities, it is recommended to use prime numbers of at least 2048 bits in length. This increases the difficulty for an adversary attempting to compute the discrete logarithm and compromise the shared secret. These two values are chosen in this way to ensure that the resulting shared secret can take on any value from 1 to . Here is an example of the protocol, with non-secret values in , and secret values in ''''''. •
Alice and Bob publicly agree to use a modulus '
= and base ' = (which is a primitive root modulo 23). • Alice chooses a secret integer '''''''
= 4, then sends Bob ' = '''''''
mod ' • '
= mod = (in this example both ' and '''''''''' have the same value 4, but this is usually not the case) • Bob chooses a secret integer '''''''
= 3, then sends Alice ' = '''''''
mod ' • '
= ''' mod = • Alice computes '''''
= ''
mod ' • '''''
= mod = ' • Bob computes '''''
= ''
mod ' • '''''
= mod = ' • Alice and Bob now share a secret (the number 18). Both Alice and Bob have arrived at the same values because under mod
p, : {\color{Blue}A}^{\color{Red}\boldsymbol{b}}\bmod {\color{Blue}p} = {\color{Blue}g}^{\color{Red}\boldsymbol{ab}}\bmod {\color{Blue}p} = {\color{Blue}g}^{\color{Red}\boldsymbol{ba}}\bmod {\color{Blue}p} = {\color{Blue}B}^{\color{Red}\boldsymbol{a}}\bmod {\color{Blue}p} More specifically, : ({\color{Blue}g}^{\color{Red}\boldsymbol{a}}\bmod {\color{Blue}p})^{\color{Red}\boldsymbol{b}}\bmod {\color{Blue}p} = ({\color{Blue}g}^{\color{Red}\boldsymbol{b}}\bmod {\color{Blue}p})^{\color{Red}\boldsymbol{a}}\bmod {\color{Blue}p} Only
a and
b are kept secret. All the other values –
p,
g,
ga mod
p, and
gb mod
p – are sent in the clear. The strength of the scheme comes from the fact that
gab mod
p =
gba mod
p take extremely long times to compute by any known classical algorithm just from the knowledge of
p,
g,
ga mod
p, and
gb mod
p. Such a function that is easy to compute but hard to invert is called a
one-way function. Once Alice and Bob compute the shared secret they can use it as an encryption key, known only to them, for sending messages across the same open communications channel. Of course, much larger values of
a,
b, and
p would be needed to make this example secure, since there are only 23 possible results of
n mod 23. However, if
p is a prime of at least 600 digits, then even the fastest modern computers using the fastest known algorithm cannot find
a given only
g,
p and
ga mod
p. Such a problem is called the
discrete logarithm problem. • Alice and Bob agree on a natural number
n and a
generating element
g in the finite
cyclic group G of order
n. (This is usually done long before the rest of the protocol;
g and
n are assumed to be known by all attackers.) The group
G is written multiplicatively. • Alice picks a random
natural number a with 1 a
of G'' to Bob. • Bob picks a random natural number
b with 1 b
of G'' to Alice. • Alice computes the element of G. • Bob computes the element of G. Both Alice and Bob are now in possession of the group element
gab =
gba, which can serve as the shared secret key. The group
G satisfies the requisite condition for
secure communication as long as there is no efficient algorithm for determining
gab given
g,
ga, and
gb. For example, the
elliptic curve Diffie–Hellman protocol is a variant that represents an element of G as a point on an elliptic curve instead of as an integer modulo n. Variants using
hyperelliptic curves have also been proposed. The
supersingular isogeny key exchange is a Diffie–Hellman variant that was designed to be secure against
quantum computers, but it was broken in July 2022. == Ephemeral and/or static keys ==