Diffie-Hellman
Slide deck 11: primitive roots, the DH setup and shared key, the worked q=353 example, and the man-in-the-middle attack.
Secret Key Exchange (Diffie-Hellman) - ComputerphileComputerphile · Diffie-Hellman key exchange explained, 9 min
Key Exchange Problems - ComputerphileComputerphile · man-in-the-middle attack on Diffie-Hellman, 9 min
Diffie-Hellman Key ExchangeArt of the Problem · Diffie-Hellman with the colour-mixing analogy, 5 min
Diffie-hellman key exchange | Journey into cryptography | Computer Science | Khan AcademyKhan Academy Labs · short Diffie-Hellman animation, 2 minKey exchange conceptSearch on YouTube
The ideaSearch on YouTube
The concept is astounding. Two parties carry on a public conversation and still end up with a secret that only the two of them know. The shared secret comes from private secrets and publicly shared information.
Alice knows: a, q, x_A, y_A, y_B
Bob knows: a, q, x_B, y_B, y_A
Everyone knows: a, q, y_A, y_B
Result: one equal value K_AB that only Alice and Bob can computeThink of mixed paint. Alice and Bob each keep a private colour. They exchange mixed colours in public. Each adds its own private colour to the received mix. Both get the same final mixture, but the public never sees the private colours.
Key distribution schemeSearch on YouTube
| DH property | Detail |
|---|---|
| Scheme type | A public-key distribution scheme |
| What it cannot do | Exchange an arbitrary message |
| What it can do | Establish a common key, known only to the two participants |
| Key dependence | The value of the key depends on the participants (and their private and public key information) |
Primitive rootsSearch on YouTube
A primitive root of a prime number is an integer in the range . For in the range , the values of are all different. The slide task returns if is not prime.
Slide example: input . The smallest primitive root is .
| 0 | 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|---|
| 1 | 3 | 2 | 6 | 4 | 5 |
The six values are all different. They cover every nonzero residue mod 7, thus is a primitive root.
The range of goes to , not to . The reason: again (Fermat), so repeats the value for .
The DH protocolSearch on YouTube
SetupSearch on YouTube
All users agree on global parameters:
- A large prime integer (or polynomial) .
- A number that is a primitive root mod .
Each user (for example, A) makes a key:
- A chooses a secret key (a number): .
- A computes the public key: .
- A makes the key public.
| Item | Known by | Public? |
|---|---|---|
| everyone | yes | |
| everyone | yes | |
| Alice only | no | |
| everyone | yes |
Shared session keySearch on YouTube
The shared session key for users A and B is :
Each principal has the public key of the other, its own secret, and and .
The two routes give the same value:
An eavesdropper sees only , , and . To get , the eavesdropper must find or from . This is the discrete logarithm problem, which is hard for a large .
Protocol figureSearch on YouTube
The slide figure shows the protocol for Alice and Bob in parallel:
| Step | Alice | Bob |
|---|---|---|
| 1 | Shares a prime and an integer , a primitive root of | Shares the same and |
| 2 | Makes a private key | Makes a private key |
| 3 | Calculates a public key | Calculates a public key |
| 4 | Receives Bob's public key in plaintext | Receives Alice's public key in plaintext |
| 5 | Calculates the shared secret key | Calculates the shared secret key |
Setup order: q, a, x, y, K. First the prime , then the primitive root , then the private , then the public , then the shared . The public key is to your . The shared key is the other side's to your .
Session key useSearch on YouTube
is the session key (or pre-key) in a private-key encryption scheme between Alice and Bob. If Alice and Bob communicate again later, they get the same key as before. This is true unless they choose new public keys. They can use nonces and the pre-key to make the session key different.
Worked exampleSearch on YouTube
Parameters and keysSearch on YouTube
Alice and Bob want to swap keys.
| Step | Compute | Value |
|---|---|---|
| Agree on parameters | prime , primitive root | |
| Alice selects a random secret | ||
| Bob selects a random secret | ||
| Alice computes her public key | ||
| Bob computes his public key | ||
| Alice computes the shared key | ||
| Bob computes the shared key |
Both routes give .
Square-and-multiply checkSearch on YouTube
The slide gives only the results. These tables show how to get them. Each row squares the row above, mod 353.
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | |
|---|---|---|---|---|---|---|---|---|
| 3 | 9 | 81 | 207 | 136 | 140 | 185 | 337 | |
| 248 | 82 | 17 | 289 | 213 | 185 | 337 | ||
| 40 | 188 | 44 | 171 | 295 | 187 | 22 | 131 |
The exponents in binary: and .
| Value | Product of the powers | Steps mod 353 | Result |
|---|---|---|---|
| ; | 40 | ||
| 248 | |||
| ; | 160 | ||
| 160 |
Quick check: and . Also and . Both sides get .
Man-in-the-middle attackSearch on YouTube
Key exchange protocolsSearch on YouTube
There are two ways to use DH keys:
- Users can create random private/public DH keys each time they communicate.
- Users can create a known private/public DH key and publish it in a directory. Other users consult the directory and use the keys to communicate securely.
Both methods are vulnerable to a Man-in-the-Middle Attack. Authentication of the keys is necessary.
Attack stepsSearch on YouTube
The slide gives the attack in 7 steps:
- Darth prepares and creates two private/public keys.
- Alice transmits her public key to Bob.
- Darth intercepts this and transmits his first public key to Bob. Darth also calculates a shared key with Alice.
- Bob receives the public key and calculates the shared key (with Darth instead of Alice).
- Bob transmits his public key to Alice.
- Darth intercepts this and transmits his second public key to Alice. Darth calculates a shared key with Bob.
- Alice receives the key and calculates the shared key (with Darth instead of Bob).
Darth can then intercept, decrypt, re-encrypt and forward all messages between Alice and Bob.
Attack notationSearch on YouTube
The slide uses this notation. Darth has two secrets, and .
| Item | Formula | Sent or used by |
|---|---|---|
| Alice's public key | Alice to Darth | |
| Darth's first public key | Darth to Bob (as if from Alice) | |
| Bob's public key | Bob to Darth | |
| Darth's second public key | Darth to Alice (as if from Bob) | |
| Bob's key (shared with Darth) | Bob and Darth | |
| Alice's key (shared with Darth) | Alice and Darth |
Darth computes the same two keys from his own secrets: and .
Bob Darth Alice
<------------ y_A ----------|
<-------- y'_A ------------|
|--------- y_B ----------->|
|----------- y'_B --------->
K_DAB = y'_A^xB mod q K_ADB = y'_B^xA mod qDarth has a private, unauthenticated channel with each of Alice and Bob.
DH by itself does not check identity. In steps 3 and 6, Darth replaces the public keys, and nobody can detect it. Each side thinks that it shares a key with the other side. In fact, each side shares a key with Darth.
DefencesSearch on YouTube
The man-in-the-middle attack is also known as the "Bucket Brigade" attack. The defences from the slide are:
- A reliable way to associate a public key with its principal is necessary.
- Public key infrastructure (PKI) is one way.
- The PGP web of trust is another way.
- In some circumstances, scheduling/timing can prevent MITM.
Exam phrase: "authentication of the keys is needed". DH solves key distribution, but not key authentication. PKI and the PGP web of trust bind a public key to its owner.
Quick self-testSearch on YouTube
What is a primitive root of a prime modulo ?
An integer in such that the values for in are all different. The task returns when is not prime.
Show that 3 is a primitive root modulo 7.
give . The six values are all different, thus is a primitive root.
Which DH values are global, and which are private?
Global: the large prime and the primitive root . Per user: a secret (private) and the public key (public).
Write the shared key expression and explain why both sides get the same value.
. Both expressions are equal to .
Do the slide example with , , , .
. . .
What can DH do, and what can it not do?
DH can establish a common key known only to the two participants. It cannot exchange an arbitrary message.
Name the two key exchange protocols in the slides and their common weakness.
New random DH keys for each communication, or one known DH key published in a directory. Both are vulnerable to a man-in-the-middle attack, because neither authenticates the keys.
List the 7 MITM attack steps in order.
1: Darth creates two key pairs. 2: Alice sends her public key to Bob. 3: Darth intercepts it, sends his first public key to Bob, and calculates a key with Alice. 4: Bob calculates a key with Darth. 5: Bob sends his public key to Alice. 6: Darth intercepts it, sends his second public key to Alice, and calculates a key with Bob. 7: Alice calculates a key with Darth.
Which keys do Bob and Alice compute in the MITM attack?
Bob computes , where . Alice computes , where . Darth knows both keys.
What is the other name of the MITM attack, and what are the defences?
The "Bucket Brigade" attack. Defences: a reliable way to associate a public key with its principal, such as PKI or the PGP web of trust. In some circumstances, scheduling/timing can prevent MITM.
- DH lets two parties get a common secret from a public conversation. Public: the prime , the primitive root , and , . Private: .
- A primitive root of a prime gives different values for in . Example: , .
- . Example: , , , give , , .
- Without key authentication, Darth can do a man-in-the-middle ("Bucket Brigade") attack. PKI and the PGP web of trust are defences.