Key Exchange and Diffie-Hellman
Key Exchange
Symmetric encryption needs both sides to hold the same key. In the old days that key was handed over physically or read out by voice. Today a server has to establish one across the internet, where anything sent can be intercepted.The solution is key exchange: an algorithm that allows two computers to agree on a key without sending one.
Read that twice, because it is the part that sounds impossible. The key is never transmitted. Both computers finish holding the same number, and no message containing that number ever crosses the network.
This is done using one-way functions. A one-way function is a function that is easy to compute but hard to invert. Mixing paint is the everyday example: it is easy to mix two colours together, and very hard to unmix them, meaning to work out which colours went in from the colour that came out.
Two people, one secret colour each
Alice and Bob want to establish a shared secret key for encryption and decryption. Eve is listening to everything they send. She is called Eve because she eavesdrops.Here is the whole method in four steps. Both of them do all four.
• Step 1. Each starts with a secret colour and shows it to nobody.
• Step 2. Each mixes the public colour into their own secret colour.
• Step 3. Each sends that mixture to the other, out in the open, where Eve sees it.
• Step 4. Each mixes their own secret colour into the mixture that just arrived.
| In the paint story | In Diffie-Hellman | Who sees it |
|---|---|---|
| The public colour both mix in | The base B and the modulus M | Everyone, agreed in the open |
| Alice's secret colour | Alice's secret exponent X | Alice only, never sent |
| Bob's secret colour | Bob's secret exponent Y | Bob only, never sent |
| Mixing two colours together | Raising to a power, then reducing modulo M | Not a value, an operation |
| The mixture each one sends | The number each one computes at step 2 | Everyone, Eve records both |
| The colour both end up with | The shared key | Alice and Bob only, never sent |
Congruent modulo n
Diffie-Hellman key exchange uses modular exponentiation as its one-way function. First, a piece of notation for it.Two positive integers x and y are congruent modulo n when they leave the same remainder on division by n. In Python that test is
x % n == y % n.For example 7 and 2 are congruent modulo 5, since both of them leave a remainder of 2 when divided by 5. This is written 7 = 2 mod 5, with an equals sign that means congruent rather than equal.
When we perform operations here we reduce modulo n, so that every result is a remainder somewhere from 0 to n minus 1.
Why modular exponentiation is one-way
Modular exponentiation means raising a base to a power and then reducing the result modulo n. It is a one-way function because it is easy to raise a power but hard to invert.Going forwards is one line of Python:
print(pow(3, 5, 31)) # prints 26
Going backwards is the problem. Finding the exponent x for which 3 raised to the power x is congruent to 26 modulo 31 is called the discrete logarithm problem, and no efficient algorithm for it is known. With a modulus this small you can just try every exponent in turn until one matches. In real use the base is often small (2 is a common choice), and the modulus is a large prime hundreds of digits long. At that size, trying every exponent is not something any computer will finish, and the cleverer methods that are known are still far too slow.
The Diffie-Hellman key exchange
The process is the painting example with arithmetic in place of colours. Alice and Bob agree publicly on two numbers, a base B and a modulus M. Eve sees both of them.• Step 1. Alice picks a secret exponent X. Bob picks a secret exponent Y. Neither one is ever sent.
• Step 2. Alice computes B to the power X, modulo M. Bob computes B to the power Y, modulo M. In Python each is a single call,
pow(B, X, M).• Step 3. Alice sends her result to Bob, and Bob sends his result to Alice, in the open.
• Step 4. Each raises the number that arrived to their own secret exponent, modulo M. For Alice that is
pow(received, X, M).Alice has now raised B to the power Y, and then raised all of that to the power X. Bob has raised B to the power X, and then raised all of that to the power Y. Both are B raised to the power X times Y, modulo M, so both land on the same number. That number is the shared key.
The program below runs the exchange between a phone and a server, with base 6 and modulus 79. Every calculation in it raises a number to a power, then reduces modulo 79, the same arithmetic as
pow(B, X, M). Then it plays the eavesdropper: it tests the exponents 1, 2, 3, and upward until one gives the number the phone sent. With a modulus this small, that search ends almost at once. Run it, then change it and run it again:• Change
server_secret to another whole number from 1 to 100.• Change
phone_secret to 71.base = 6
modulus = 79
phone_secret = 37
server_secret = 55
phone_sends = (base ** phone_secret) % modulus
server_sends = (base ** server_secret) % modulus
print("The phone sends", phone_sends)
print("The server sends", server_sends)
phone_key = (server_sends ** phone_secret) % modulus
server_key = (phone_sends ** server_secret) % modulus
print("Phone's key:", phone_key)
print("Server's key:", server_key)
# The eavesdropper tries every exponent until one gives what the phone sent
guess = 1
while (base ** guess) % modulus != phone_sends:
guess = guess + 1
print("The eavesdropper finds an exponent that gives what the phone sent:", guess)
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.
Two shortcomings
One shortcoming of the Diffie-Hellman key exchange is the communication overhead it takes to establish a shared key. Alice and Bob have to send the results of their calculations back and forth before a single word of the actual message can be encrypted.Another is that the exchange only sets up one key, between two people. If Alice needs to message different people, she needs to exchange a different key with each of them. A key that several people hold protects nothing between them, since each of them could decrypt what the others send. If she is a bank, she needs thousands of distinct keys, one per customer, and thousands of exchanges to set them all up before any of them can carry a message.