CSS411 Cryptography

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.

21 min read

Key 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 compute

Think 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 propertyDetail
Scheme typeA public-key distribution scheme
What it cannot doExchange an arbitrary message
What it can doEstablish a common key, known only to the two participants
Key dependenceThe 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 nn is an integer rr in the range [1,n−1][1, n-1]. For xx in the range [0,n−2][0, n-2], the values of rx mod nr^x \bmod n are all different. The slide task returns −1-1 if nn is not prime.

Slide example: input n=7n = 7. The smallest primitive root is 33.

xx012345
3x mod 73^x \bmod 7132645

The six values 1,3,2,6,4,51, 3, 2, 6, 4, 5 are all different. They cover every nonzero residue mod 7, thus 33 is a primitive root.

The range of xx goes to n−2n-2, not to n−1n-1. The reason: rn−1 mod n=1r^{n-1} \bmod n = 1 again (Fermat), so x=n−1x = n-1 repeats the value for x=0x = 0.

The DH protocolSearch on YouTube

SetupSearch on YouTube

All users agree on global parameters:

  • A large prime integer (or polynomial) qq.
  • A number aa that is a primitive root mod qq.

Each user (for example, A) makes a key:

  • A chooses a secret key (a number): xA<qx_A \lt q.
  • A computes the public key: yA=axA mod qy_A = a^{x_A} \bmod q.
  • A makes the key yAy_A public.
ItemKnown byPublic?
qqeveryoneyes
aaeveryoneyes
xAx_AAlice onlyno
yA=axA mod qy_A = a^{x_A} \bmod qeveryoneyes

Shared session keySearch on YouTube

The shared session key for users A and B is KABK_{AB}:

KAB=axA⋅xB mod q=yAxB mod q (B computes)=yBxA mod q (A computes)K_{AB} = a^{x_A \cdot x_B} \bmod q = y_A^{x_B} \bmod q \ (\text{B computes}) = y_B^{x_A} \bmod q \ (\text{A computes})

Each principal has the public key of the other, its own secret, and aa and qq.

The two routes give the same value:

yBxA=(axB)xA=axAxB=(axA)xB=yAxB(modq)y_B^{x_A} = (a^{x_B})^{x_A} = a^{x_A x_B} = (a^{x_A})^{x_B} = y_A^{x_B} \pmod q

An eavesdropper sees only qq, aa, yAy_A and yBy_B. To get KABK_{AB}, the eavesdropper must find xAx_A or xBx_B from y=ax mod qy = a^{x} \bmod q. This is the discrete logarithm problem, which is hard for a large qq.

Protocol figureSearch on YouTube

The slide figure shows the protocol for Alice and Bob in parallel:

StepAliceBob
1Shares a prime qq and an integer α<q\alpha \lt q, a primitive root of qqShares the same qq and α\alpha
2Makes a private key XA<qX_A \lt qMakes a private key XB<qX_B \lt q
3Calculates a public key YA=αXA mod qY_A = \alpha^{X_A} \bmod qCalculates a public key YB=αXB mod qY_B = \alpha^{X_B} \bmod q
4Receives Bob's public key YBY_B in plaintextReceives Alice's public key YAY_A in plaintext
5Calculates the shared secret key K=(YB)XA mod qK = (Y_B)^{X_A} \bmod qCalculates the shared secret key K=(YA)XB mod qK = (Y_A)^{X_B} \bmod q
Public: q and αα is a primitive root of qAliceBobPrivate XA​ < qPrivate XB​ < qYA​ = αXA​ mod qYB​ = αXB​ mod qYA​ in clearYB​ in clearK = (YB​)XA​ mod qK = (YA​)XB​ mod qBoth values equal αXA·XB​ mod q.An eavesdropper sees q, α, YA​, YB​: a discrete logarithm problem.
Diffie-Hellman: each side keeps a private key, sends only its public key, and computes the same shared key K.

Setup order: q, a, x, y, K. First the prime qq, then the primitive root aa, then the private xx, then the public yy, then the shared KK. The public key is aa to your xx. The shared key is the other side's yy to your xx.

Session key useSearch on YouTube

KABK_{AB} 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.

StepComputeValue
Agree on parametersprime q=353q = 353, primitive root a=3a = 3
Alice selects a random secretxAx_A9797
Bob selects a random secretxBx_B233233
Alice computes her public keyyA=397 mod 353y_A = 3^{97} \bmod 3534040
Bob computes his public keyyB=3233 mod 353y_B = 3^{233} \bmod 353248248
Alice computes the shared keyKAB=yBxA mod 353=24897 mod 353K_{AB} = y_B^{x_A} \bmod 353 = 248^{97} \bmod 353160160
Bob computes the shared keyKAB=yAxB mod 353=40233 mod 353K_{AB} = y_A^{x_B} \bmod 353 = 40^{233} \bmod 353160160

Both routes give KAB=160K_{AB} = 160.

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.

kk01234567
32k mod 3533^{2^k} \bmod 3533981207136140185337
2482k mod 353248^{2^k} \bmod 3532488217289213185337
402k mod 35340^{2^k} \bmod 353401884417129518722131

The exponents in binary: 97=64+32+197 = 64 + 32 + 1 and 233=128+64+32+8+1233 = 128 + 64 + 32 + 8 + 1.

ValueProduct of the powersSteps mod 353Result
yA=397y_A = 3^{97}185×140×3185 \times 140 \times 3185×140→131185 \times 140 \to 131; 131×3→40131 \times 3 \to 4040
yB=3233y_B = 3^{233}337×185×140×207×3337 \times 185 \times 140 \times 207 \times 3→217→22→318→248\to 217 \to 22 \to 318 \to 248248
K=24897K = 248^{97}337×185×248337 \times 185 \times 248337×185→217337 \times 185 \to 217; 217×248→160217 \times 248 \to 160160
K=40233K = 40^{233}131×22×187×171×40131 \times 22 \times 187 \times 171 \times 40→58→256→4→160\to 58 \to 256 \to 4 \to 160160

Quick check: 397 mod 353=403^{97} \bmod 353 = 40 and 40233 mod 353=16040^{233} \bmod 353 = 160. Also 3233 mod 353=2483^{233} \bmod 353 = 248 and 24897 mod 353=160248^{97} \bmod 353 = 160. Both sides get KAB=160K_{AB} = 160.

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:

  1. Darth prepares and creates two private/public keys.
  2. Alice transmits her public key to Bob.
  3. Darth intercepts this and transmits his first public key to Bob. Darth also calculates a shared key with Alice.
  4. Bob receives the public key and calculates the shared key (with Darth instead of Alice).
  5. Bob transmits his public key to Alice.
  6. Darth intercepts this and transmits his second public key to Alice. Darth calculates a shared key with Bob.
  7. 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, xDAx_{DA} and xDBx_{DB}.

ItemFormulaSent or used by
Alice's public keyyA=axA mod qy_A = a^{x_A} \bmod qAlice to Darth
Darth's first public keyyA′=axDA mod qy'_A = a^{x_{DA}} \bmod qDarth to Bob (as if from Alice)
Bob's public keyyB=axB mod qy_B = a^{x_B} \bmod qBob to Darth
Darth's second public keyyB′=axDB mod qy'_B = a^{x_{DB}} \bmod qDarth to Alice (as if from Bob)
Bob's key (shared with Darth)KDAB=yA′ xB mod qK_{DAB} = y'^{\,x_B}_A \bmod qBob and Darth
Alice's key (shared with Darth)KADB=yB′ xA mod qK_{ADB} = y'^{\,x_A}_B \bmod qAlice and Darth

Darth computes the same two keys from his own secrets: KDAB=yBxDA mod qK_{DAB} = y_B^{x_{DA}} \bmod q and KADB=yAxDB mod qK_{ADB} = y_A^{x_{DB}} \bmod q.

Bob                       Darth                       Alice
                            <------------ y_A ----------|
 <-------- y'_A ------------|
 |--------- y_B ----------->|
                            |----------- y'_B --------->
K_DAB = y'_A^xB mod q                   K_ADB = y'_B^xA mod q

Darth 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 nn modulo nn?

An integer rr in [1,n−1][1, n-1] such that the values rx mod nr^x \bmod n for xx in [0,n−2][0, n-2] are all different. The task returns −1-1 when nn is not prime.

Show that 3 is a primitive root modulo 7.

30,31,…,35 mod 73^0, 3^1, \dots, 3^5 \bmod 7 give 1,3,2,6,4,51, 3, 2, 6, 4, 5. The six values are all different, thus 33 is a primitive root.

Which DH values are global, and which are private?

Global: the large prime qq and the primitive root aa. Per user: a secret xA<qx_A \lt q (private) and the public key yA=axA mod qy_A = a^{x_A} \bmod q (public).

Write the shared key expression and explain why both sides get the same value.

KAB=yBxA mod q=yAxB mod q=axAxB mod qK_{AB} = y_B^{x_A} \bmod q = y_A^{x_B} \bmod q = a^{x_A x_B} \bmod q. Both expressions are equal to axAxB mod qa^{x_A x_B} \bmod q.

Do the slide example with q=353q = 353, a=3a = 3, xA=97x_A = 97, xB=233x_B = 233.

yA=397 mod 353=40y_A = 3^{97} \bmod 353 = 40. yB=3233 mod 353=248y_B = 3^{233} \bmod 353 = 248. KAB=24897 mod 353=40233 mod 353=160K_{AB} = 248^{97} \bmod 353 = 40^{233} \bmod 353 = 160.

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 KDAB=yA′ xB mod qK_{DAB} = y'^{\,x_B}_A \bmod q, where yA′=axDA mod qy'_A = a^{x_{DA}} \bmod q. Alice computes KADB=yB′ xA mod qK_{ADB} = y'^{\,x_A}_B \bmod q, where yB′=axDB mod qy'_B = a^{x_{DB}} \bmod q. 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 qq, the primitive root aa, and yAy_A, yBy_B. Private: xA,xB<qx_A, x_B \lt q.
  • A primitive root rr of a prime nn gives different values rx mod nr^x \bmod n for xx in [0,n−2][0, n-2]. Example: n=7n = 7, r=3r = 3.
  • KAB=yBxA mod q=yAxB mod q=axAxB mod qK_{AB} = y_B^{x_A} \bmod q = y_A^{x_B} \bmod q = a^{x_A x_B} \bmod q. Example: q=353q = 353, a=3a = 3, xA=97x_A = 97, xB=233x_B = 233 give yA=40y_A = 40, yB=248y_B = 248, KAB=160K_{AB} = 160.
  • Without key authentication, Darth can do a man-in-the-middle ("Bucket Brigade") attack. PKI and the PGP web of trust are defences.