The Diffie-Hellman key exchange algorithm

The Diffie-Hellman key exchange was the first widely used method of safely exchanging keys over an insecure channel. This post discusses about the DH algorithm mathematically, it's security and vulnerabilities.

Anish Koulgi

29 October 2020 8 mins read


Introduction

The Diffie-Hellman key exchange was the first widely used method of safely exchanging keys over an insecure channel. The DH is used for securely calculating the shared keys for safe communication. It is widely used in various security protocols like TLS, SSH etc.

Before understanding the actual algorithm, let's understand the need of such an algorithm.

Need of a key exchange method

Let's say a person A wants to communicate with a person B whom he/she has never met before, over an insecure channel. How would they prevent the intruders on the channel from getting hold of their messages ?

The most obvious and common way is to encrypt the message and then send it to person B. So let's say person A uses a key KK for encrypting the message. But person B does not know the key KK which is used for encryption by A.

If A encrypts the message without telling B the key KK, then the intruder won’t be able to read it, but neither will the B! So the important question is:

How can person A securely exchange information with B if A hasn't had the opportunity to share the key ahead of time?

This is where the Diffie-Hellman key exchange algorithm comes into the picture. The DH key exchange was the first publicly-used method for solving this problem. It was published in 1976 by Whitfield Diffie and Martin Hellman.

How does the DH exchange work ?

The DH exchange algorithm uses modular arithmetic as it's core to calculate the shared key from the public and private keys.

The mathematics may seem a bit intimidating at first sight but it's easy when you understand the concept behind it.


Algorithm

So let's take our previous example of person A and B wanting to securely communicate with eachother.

  1. Initially A and B both agree upon two numbers gg called as the generator and pp. Generally pp is a very large prime number that is atleast 20002000 bits long and gg is a smaller number which is a primitive root of pp.
    A number pp is a primitive root of qq if the powers of pp modulo qq i.e.

    p1modq,p2modq,p3modqp^1\,\,mod\,\,q, p^2\,\,mod\,\, q , p^3\,\,mod\,\,q\,\, upto pq1modqp^{q-1}\,\,mod\,\,q\,

    are unique and in the range 11 to q1q - 1.
    Where 'modmod' is the modulo operation.

    Formally, A number pp is a primitive root of qq if the positive powers p1,p2,p3...pq1p^1,p^2,p^3 ... p^{q-1} generate all nonzero congruence classes modulo pp.

    For example, let q=7q = 7. Then p=2p = 2 is not a primitive root, since,

    21mod7=2 2^1\,\,mod\,\,7 = 2
    22mod7=4 2^2\,\,mod\,\,7 = 4
    23mod7=1 2^3\,\,mod\,\,7 = 1
    24mod7=2 2^4\,\,mod\,\,7 = 2

    As the value 2 repeats for both 21mod72^1\,\,mod\,\,7 and 24mod72^4\,\,mod\,\,7, thus 22 is not a primitive root of 77. Let's take another example where q=7q = 7 and p=3p = 3

    31mod7=3 3^1\,\,mod\,\,7 = 3
    32mod7=23^2\,\,mod\,\,7 = 2
    33mod7=63^3\,\,mod\,\,7 = 6
    34mod7=4 3^4\,\,mod\,\,7 = 4
    35mod7=5 3^5\,\,mod\,\,7 = 5
    36mod7=1 3^6\,\,mod\,\,7 = 1

    As all the modmod values are unique and in range 00 to q1q - 1, 33 is a primitive root of 77. Now that a bit of math is clear let's proceed to the next step.

  2. Once pp and gg and decided, A and B choose their respective private keys(secret numbers). Let's say that person A chooses a number aa and B chooses bb.

    aa and bb are the private keys and are never sent independently across the insecure channel.

  3. Now A and B calculate their public keys which can be sent across the insecure channel.
    A calculates it's public key as
    Pa=gamodpP_a = g^a\,\,mod\,\,p
    And similarly B calculates it's public key as
    Pb=gbmodpP_b = g^b\,\,mod\,\,p
    These keys are sent through the channel i.e. A sends it's public key PaP_a to B and B sends it's public key PbP_b to A.

4) After receiving public keys of eachother, A and B simultaneously calculate the shared key which will be used for communication. A calculates the shared key as from B's public key as

Ska=(Pb)amodp \,\,\,\,\,\,S_{ka} =(P_b)^{a}\,\,mod\,\,p
Ska=(gbmodp)amodp \therefore S_{ka} =(g^b\,\,mod\,\,p)^{a}\,\,mod\,\,p

B calculates the shared key as from A's public key as

Skb=(Pa)bmodp\,\,\,\,\,\,S_{kb} =(P_a)^{b}\,\,mod\,\,p
Skb=(gamodp)bmodp \therefore S_{kb} =(g^a\,\,mod\,\,p)^{b}\,\,mod\,\,p

Hang on a minute. How are these keys one and the same? These keys don't seem to be equal though.
Here's where the magic of modular arithmetic comes in. By basic modular arithmetic, we can say that

(amodp)+b(a+b)modp (a\,\,mod\,\,p) + b \equiv (a + b)\,\,mod\,\,p

We can extend this to multiplication as it is nothing but repititive addition!

(amodp)b(ab)modp (a\,\,mod\,\,p) \cdot b \equiv (a \cdot b)\,\,mod\,\,p

Can we extend this to exponentiation? Yes! Exponentiaion is nothing but repititive multiplication! So,

(amodp)b(ab)modp (a\,\,mod\,\,p) ^ b \equiv (a ^ b)\,\,mod\,\,p

Using this result we can simplify the SkaS_{ka} and SkbS_{kb} values as follows :

Ska=(gbmodp)amodp=(((gb)a)modp)modp=gabmodp S_{ka} =(g^b\,\,mod\,\,p)^{a}\,\,mod\,\,p = (((g^b)^a)\,\,mod\,\,p)\,\,mod\,\,p = g^{ab}\,\,mod\,\,p

and

Skb=(gamodp)bmodp=(((ga)b)modp)modp=gabmodp S_{kb} =(g^a\,\,mod\,\,p)^{b}\,\,mod\,\,p = (((g^a)^b)\,\,mod\,\,p)\,\,mod\,\,p = g^{ab}\,\,mod\,\,p
Ska=Skb=gabmodp \therefore \,\,S_{ka} = S_{kb} = g^{ab}\,\,mod\,\,p

And there we go!! Without ever sharing their private keys, both A and B were able to generate a shared key which is completely unknown to the intruders on the channel!

Example

What's better than learning an algorithm through an example? Let's continue our previous example of person A and person B. Feel free to work through the example on your own to get a better grasp on the algorithm.

I'll be using smaller values of aa, bb and pp just for the sake of easy calculation. Practically, aa, bb and pp have very large values to prevent brute force calculation of aa and bb.

  1. Let's assume A and B choose p=7p = 7 and the primitive root g=3g = 3 (we proved that 33 is a primitive root of 77 above).

  2. Let's also assume A chooses a=5a = 5 and B chooses b=6b = 6.

  3. Now A calculates it's public key as
    Pa=gamodp=35mod7=5P_a = g^a\,\,mod\,\,p =3^5\,\,mod\,\,7 = 5 so Pa=5P_a = 5
    B calculates it's public key as
    Pb=gbmodp=36mod7=1P_b = g^b\,\,mod\,\,p =3^6\,\,mod\,\,7 = 1 so Pa=1P_a = 1
    A sends PaP_a to B and B sends PbP_b to A via the channel.

  4. Now A and B calculate the shared key simultaneously as follows :

Ska=(Pb)amodp=(1)5mod7=1mod7=1 \,\,\,\,\,\,S_{ka} =(P_b)^{a}\,\,mod\,\,p = (1)^5\,\,mod\,\,7 = 1\,\,mod\,\,7 = 1
Skb=(Pa)bmodp=(5)6mod7=15625mod7=1 \,\,\,\,\,\,S_{kb} =(P_a)^{b}\,\,mod\,\,p = (5)^6\,\,mod\,\,7 = 15625\,\,mod\,\,7 = 1
Ska=Skb=1 \therefore \,\,S_{ka} = S_{kb} = 1

And voilà! A and B generated the same shared key without ever sharing their private keys. This is the Diffie-Hellman key exchange algorithm.
With the generated shared key, A can encrypt the message and send it to B. Only B can decrypt the message as only B knows the shared key apart from A.

Practical considerations

For Diffie-Hellman to be a practical key exchange algorithm, we need an efficient way to find primitive roots and perform fast exponentiation.
You can read more about finding primitive roots here.
We can use binary exponentiation to calculate the value of abmodpa^b\,\,mod\,\,p for very high values of a,bandpa,b \,\,\,and\,\,\, p in O(log2b)\,\,O(\log_{2}{b}) time.

Security of DH method

Why is the DH algorithm secure? Suppose a third party intruder, C, intercepts pp, gg, PaP_a, and PbP_b. If C can compute aa and bb, he/she easily compute SkS_k just as A and B did. Then C must compute aa satisfying
Pa=gamodpP_a = g^a\,\,mod\,\,p.
This computation is known as taking a discrete logarithm of PaP_a and there are no fast algorithms for doing it. When p is large, say 100s of digits, calculating a discrete logarithm is effectively impossible.

The Logjam attack

  • The most powerful known mechanism for finding the solution of the discrete logarithm problem is the number field sieve algorithm.

  • In 2015, a research team calculated the most commonly used 512 bit DH exchange numbers in TLS.

  • The attack affects any server that supports DHE_EXPORT ciphers, and affects all modern web browsers. 8.4% of the Top 1 Million domains were initially vulnerable.

  • They also suggested that the NSA may already have such capabilities. They quoted that :

“A close reading of published NSA leaks shows that the agency’s attacks on VPNs are consistent with having achieved such a break.”

Is it safe then?

Yes. Despite the Logjam attack the DH method is quite safe when the pp value is atleast 20002000 bits long and is correctly and effectively implemented.

The Logjam attack is not about the weakness of the DH method but it is about the weak implementation of the algorithm.

This article explains the best practices to implement the Diffie-Hellman key exchange method very well.

Applications of DH key exchange

  • The main purpose of the Diffie-Hellman key exchange is to securely generate shared secrets. These secrets can then be used with symmetric-key algorithms to transmit information in a protected manner. Symmetric algorithms are used to encrypt the data as they are more efficient than public key algorithms.

  • The Diffie-Hellman key exchange is frequently implemented in security protocols such as TLS, IPsec, SSH, PGP, and many others.

  • This is used in SSH mainly to establish a connection between the remote server and the client. The communication is often carried out in sessions so that even if an intruder brute forces the private keys, the client and server generate new keys for new sessions.

Conclusion

The Diffie-Hellman exchange is one of the most widely used and safest algorithms for key sharing / generating keys.
While it may be vulerable to smaller pp values, but it is almost impossible to break it for higher values of pp.
And thus it will continue to be a major part of security protocols used for communication over insecure channels.


Thank you for reading this post. Feel free to comment below if you liked this post or have any suggestions or improvements.

Facebook LogoGithub LogoLinkedin Logo

© 2020 Anish Koulgi. All Rights Reserved