15 Mar 2016

What is Diffie-Hellman?

This article assumes knowledge of basic group theory, and basic asymptotic complexity (big O notation).

For efficiency reasons, most encryption used on the internet and elsewhere is done using symmetric-key algorithms. A symmetric key algorithm uses one key for both encryption and decryption. This requires the sender and receiver to share knowledge of a secret key. For two parties who have never communicated securely before, this is a non-trivial problem. How does one establish a shared secret using insecure channels? This is where a key exchange algorithm like Diffie-Hellman comes in.

Introduction

A key exchange algorithm is pretty self-explanatory. Given an insecure communication channel and no prior communication, two parties should be able to agree on some kind of shared secret using this algorithm, without an eavesdropper learning anything about this secret. For the rest of this article, we will assume the eavesdropper (attacker) is passive, and cannot actively interfere with the communication. Diffie-Hellman is unauthenticated on its own and does not prevent such active attacks.

The Diffie-Hellman key exchange algorithm is based on the “difficulty” of a certain mathematical problem, namely the computation of the discrete logarithm. No one has actually proven that the discrete logarithm can’t be solved (in general) by an efficient classical algorithm. However, mathematicians and computer scientists alike have tried for many years to come with such an algorithm, and failed. Most therefore believe it doesn’t exist. Cryptographers aren’t the betting type, but mathematics is a topic they will bet on. The security of Diffie-Hellman is therefore based on the assumption that the discrete logarithm admits no efficient general solution.

Definition 1 (Discrete logarithm).

Let be any finite cyclic group of order . Let be a generator of , and let . The discrete logarithm of to the base is the unique integer , , such that . We say .

Given , , and , the discrete logarithm problem is a matter of calculating . For some groups, efficient algorithms are known. But others are considered intractable, and these are the ones exploited for cryptographic purposes.

Diffie-Hellman key exchange

The original specification [1] used the multiplicative group of integers modulo , where is a prime number. This group is typically denoted by . Since is prime, the order of this group is . This seems like a complex way to define a group, but it’s simply the set of integers from 1 to inclusive, and integer multiplication modulo as the group operation. Note that uniquely defines . We choose the base to be a primitive root modulo (i.e. a generator of ).

Other groups are used as well, see for example elliptic curve Diffie-Hellman. But unless otherwise specified, Diffie-Hellman generally refers to the multiplicative group of integers modulo . Given this group, the Diffie-Hellman key exchange algorithm is as follows. For convenience, private values are given in red, while public values (sent freely over the wire) are in blue.

Modular exponention is computationally inexpensive, so it is easy for Alice to calculate . Similarly, she can efficiently calculate . Comparatively, an eavesdropper does not know or , and therefore cannot easily find the secret . While the security of Diffie-Hellman has never been proven equivalent to the discrete logarithm problem, computing the discrete log remains the best known attack. Therefore, barring any flaws in the implementation, an attacker’s best bet is to calculate from , or from .

Finding the discrete logarithm

Previously it was mentioned that no efficient algorithms to compute the discrete log exist. What exactly is meant by efficient?

A simple algorithm to find the discrete log is a simple brute force search. Given a base and a , try different values of until . The running time of this algorithm is , or linear in the size of the order of . This is clear by noting that we will potentially need to try every , to find . Recall that the order of is . If we increase the size of by one bit, we’ve effectively doubled the running time of this algorithm. Therefore, it is exponential in the number of digits of . It is evident that increased sizes of very quickly make it infeasible to compute the discrete log, using this algorithm.

Generally, an “efficient” algorithm refers to a polynomial-time algorithm. In this case, we are interested in running times in terms of the number of bits of . If such a polynomial-time algorithm existed, Diffie-Hellman would be broken. The group order would have to be enormous to prevent an attacker from computing the discrete log, which would make the algorithm highly impractical. So while thankfully, no polynomial-time algorithms are known, there are several sub-exponential algorithms that are significantly faster than the naive algorithm above.

The most efficient algorithm known for discrete log computation is the Number Field Sieve (NFS). You may recognize the name, a variant (same name) is used to factor large integers. The specific details of this algorithm are complex, and outside the scope of this article. Many parameters are involved, which can shift computational burdens between the four different steps. When properly tuned parameters are used, the running time of the algorithm is , where refers to the prime number characterizing the group. Most of the algorithm (the first three steps, including the most expensive two) only depends on the group, and not the specific discrete log being calculated.

Another notable algorithm is the Pohlig-Hellman algorithm, designed especially for groups whose order is a smooth integer. A smooth integer is an integer which factors completely into small prime numbers . The running time of this algorithm is . Clearly, this becomes very fast if the number is indeed smooth, but reduces to if has one large prime factor.

Security

As previously stated, the security of Diffie-Hellman has never been proved equivalent to the discrete logarithm problem. What does this mean? The eavesdropper has access to and , and is looking to find (called the Diffie-Hellman problem). This is different from the discrete logarithm problem. It is trivial to show that solving the discrete logarithm gives you a solution to the Diffie-Hellman problem, but the reverse has proven more difficult.

Second, whether or not the discrete logarithm can be computed in polynomial-time on a classical computer is still an open problem. Whether or not this is possible on a quantum computer has already been solved. It is, thanks to Shor’s algorithm.

In any case, the Diffie-Hellman protocol has been around for 40 years, seen widespread use, and still has not been broken. It is widely believed to be secure if the and are chosen correctly. To avoid use of the Pohlig-Hellman algorithm, the prime characterizing must be a safe prime. In other words, must not be a smooth integer, i.e. contain a large prime factor. This is typically done by setting , where is another prime, called a Sophie-Germain prime. This renders Pohlig-Hellman inefficient and leaves NFS as the best attack.

Despite this belief of security, Diffie-Hellman saw a high-profile attack in 2015, named Logjam [2]. The attack exploited a flaw in TLS to downgrade a Diffie-Hellman connection to export-grade strength of 512 bits. The attack affected a limited number of servers – only those that still supported export-grade crypto. This amounted to approximately 4-8% of browser-trusted sites. The authors also successfully compromised connections to misconfigured servers, using unsafe primes. While this attack was dangerous in its own right, the methods used revealed a previously glossed-over fact of the discrete logarithm over the multiplicative group of integers modulo .

NFS supports a large amount of precomputation, if the group is known. The authors found that the vast majority of the internet used only one or two groups. Thus the cost of an attack could be amortized to a huge degree across targets. This is the technique used in Logjam, to break the two 512-bit groups used by 93% of servers supporting export-grade crypto. However, the authors also extrapolated to 1024-bit groups, and estimated that breaking such a group would be within the reach of an adversary with state-level resources. Such a break would affect a huge number of connections across many protocols, including HTTPS, IKE, and SSH. In particular, comparisons were drawn to documents leaked by Edward Snowden which showed the NSA being able to passively decrypt huge numbers of VPN connections.

In short, Diffie-Hellman as a protocol is most likely secure, when used with primes larger or equal to 2048 bits. That said, elliptic curve Diffie-Hellman uses smaller parameters and isn’t vulnerable to the same precomputational woes as regular DH. For these reasons, Diffie-Hellman has fallen largely out of favor, and the elliptic curve variant is recommended instead.

References

[1] Diffie, Whitfield, and Martin E. Hellman. “New directions in cryptography.” Information Theory, IEEE Transactions on 22.6 (1976): 644-654.

[2] Adrian, David, et al. “Imperfect forward secrecy: How Diffie-Hellman fails in practice.” Proceedings of the 22nd ACM SIGSAC Conference on Computer and Communications Security. ACM, 2015.