The Diffie-Hellman Key Exchange
29 June 2021
The Diffie-Hellman Key Exchange is one of the first widely used algorithms to exchange keys publicly. That raises two questions: What is a key exchange? and Why would you do that publicly?.
Let's start with the first one. Some information, e.g. private messages or corporate secrets, are not meant to be accessible by everyone. To ensure that only certain people (or machines) can read this kind of information, one can use various cryptographic systems. These systems allow two or more parties to share information if and only if everyone has a key to encode and/or decode the data. Of course it wouldn't be practical for one person to encode a message with a key which is completely independent from the receiver's key. Therefore, some information needs to be known to all parties involved. We call the procedure of sharing this specific information a key exchange.
Why would you do that publicly? First of all, there are key exchanges which work only privately. The problem here is that you need to find a secure way to exchange the keys. If a person, who's not supposed to get certain data, were to get access to the secretly exchanged key, they could read all of the encoded infomation. Having said that, if the keys are exchanged publicly, there is no harm in giving the keys to people who are not involved.
Now, we have a cryptographic system which exchanges keys publicly, meaning that anyone has access to the public keys. How can we ensure that secret information can only be decoded by certain people? The Diffie-Hellman Key exchange provides one possible solution to this problem.
The Algorithm
Two people, Alice and Bob, want to exchange cryptographic keys. They agree on a prime number and a common public key , both publicly known. Alice creates a secret key and Bob a secret key . Each key is exclusively known to its creator. Alice can now calculate a public key :
Simultaneously, Bob calculates his public key :
Both of these keys, and , are common knowledge. With it, Alice and Bob can calculate their common secret key :
This common secret key can then be used in other cryptographic systems, e.g. to encode secret messages or information.
How Secure is the Algorithm?
If the secret keys and are kept secret, nobody can easily calculate or . A thrid party would need to know the discret logarithm of to base , which means they would need to know a with (equally for ). If such an could be found, the secret key would not longer be secret:
The Diffie-Hellman Algorithm is built on the problem of finding such an . Logarithms in e.g. the natural numbers () or the real numbers () are easy to calucate. Every calculator could find the solution to by simply typing in . The difference to the Diffie-Hellman Algorithm is that we are working with modulo . To date, there is no fast algorithm which can calculate a discrete logarithm modulo a prime number . That leaves a third party only with testing different values for to find . For small prime numbers it can be done in a short amount of time and the algorithm wouldn't be secure. Therefore, security experts suggest long prime numbers with at least 2048 bits. The testing of different values for to solve our logarithm problem would take too long and would be too expensive for attackers to perform.
Examples
To make the alorithm a little easier to understand, we will have a look at the following two examples.
The Diffie Hellman Key Exchange with Colors
In the grapic below you can see how the keys interact with each other and how they are calculated. Certainly, mixing different colors can not provide any information about the security but you can follow the way the algorithm works.
Table of Known and Unknown Variables
In following examlpe shows an overview over a key exchange between Alice and Bob and a third party (Charlie) who's not part of the exchange. Alice and Bob agree on the prime number , the common public key and after some calculation receive the common secret key .
What Alice knows:
- Prime number
- Common public key
- Alice' secret key
- Bob's secret key is unknown
- Alice' public key
- Bob's public key
- Common seceret key
What Bob knows:
- Prime number
- Common public key
- Alice' secret key is unknown
- Bob's secret key
- Alice' public key
- Bob's public key
- Common seceret key
What Charlie knows:
- Prime number
- Common public key
- Alice' secret key is unknown
- Bob's secret key is unknown
- Alice' public key
- Bob's public key
- Common seceret key is unknown
The Diffie-Hellman Key exchange is a powerful tool in cryptographic systems. It forms the base of various encryptions like the ElGamal encryption and can even be modified to be used with ellipitic curves (ECDH).
To be mathematically correct: needs to be a primitive root modulo , but for this article it is enough to assume that and is a natural number ().
Note that the prime number is far to small to guarantee any kind of security. It is only used for demonstration purposes.