Diffie hellman example problem

Suppose two people, Alice and Bob [traditional names], want to use insecure email to agree on a secret "shared key" that they can use to do further encryption for a long message. How is that possible? The so-called Diffie-Hellman method provides a way. This method is one of the ingredients of SSL, the encryption package that is part of the Netscape browser.

These notes are a little more detailed than in class, for clarity, but on the exam you are responsible only for doing exercises like Problem 8.2.

B. The mod function

The main ingredient is the "remainder" or "modulo" or "mod" function, denoted % in Perl. For example, 25%10 is 5 (say "25 mod 10 is 5") and 25%16 is 9 ("25 mod 16 is 9"). For n%10 , the result will always be one of 0,1. 9 .

As you can see, any positive integer modulo 10 is just the last digit in base 10: 1537%10 is 7 , etc. You can think of "modulo 10" for positive integers as meaning "ignore all decimal digits except the last one".

Doing "modular arithmetic" with "modulus" 10 means doing addition, subtraction, and multiplication (including powers) where you only care about the remainder modulo 10. You can use some other modulus m instead of 10, as long as it's the same through the whole problem. It works very smoothly.

The "as often as you want" principle: If you are doing modular arithmetic to find an the answer modulo m, you can take the remainder modulo m as often as you want during the calculations, without changing the answer.

Example 1. To find 1537 x 4248 modulo 10, you could multiply out and take the last digit, but a better way would be to replace 1537 by 7 and 4248 by 8 to start, find 7 x 8 = 56, and then take 56 mod 10 to get 6 as the answer.

A handy standard notation is to write a b (mod m) if a and b have the same remainder modulo m. This is read "a is congruent to b modulo m". In this notation the example just mentioned looks like this: 1537 x 4248 7 x 8 = 56 6 (mod 10).

Example 2. Find 2 8 (mod 11).

One solution. 2 8 = 256; 11 goes into 256 with quotient 23 and remainder 3.

Another solution. Find 2 2 , 2 4 , 2 8 by squaring repeatedly, but take remainders mod 11 each chance you get: 2 2 = 4, 2 4 = 4 2 = 16 5, 2 8 5 2 = 25 3.

Example 3. Find all the powers of 2 up to 2 10 , each modulo 11.

Solution. Keep doubling, taking remainders modulo 11 whenever possible:

2, 4, 8, 16 5, 10, 20 9, 18 7, 14 3, 6, 12 1 (mod 11). So the answer is 2, 4, 8, 5, 10, 9, 7, 3, 6, 1.

Notice that the powers of 2 run through all possible remainders modulo 11, except 0. We say 2 is a "generator" modulo 11. There is a theorem that if you take a prime modulus, then there is always some generator, and in fact 2 often works. If 2 doesn't, maybe 3 will.

C. The Diffie-Hellman method

  1. Alice and Bob, using insecure communication, agree on a huge prime p and a generator g. They don't care if someone listens in.
  2. Alice chooses some large random integer x A &lt p and keeps it secret. Likewise Bob chooses x B &lt p and keeps it secret. These are their "private keys".
  3. Alice computes her "public key" y A g x A (mod p) and sends it to Bob using insecure communication. Bob computes his public key y B g x B and sends it to Alice. Here 0 < y A < p, 0 < y B < p.

But z A = z B , since z A y B x A (g x B ) x A = g (x A x B ) (mod p) and similarly z B (g x A ) x B = g (x A x B ) (mod p). So this value is their shared secret key. They can use it to encrypt and decrypt the rest of their communication by some faster method.

In this calculation, notice that the step y B x A (g x B ) x A involved replacing g x B by its remainder y B , (in the reverse direction) so we were really using the "as often as you want" principle.

. 7 x . 8 ------- . 6 . . . ------- . 6

E. An example

For Diffie-Hellman to be secure, it is desirable to use a prime p with 1024 bits; in base 10 that would be about 308 digits. An example, expressed in hexadecimal, is

p= de9b707d 4c5a4633 c0290c95 ff30a605 aeb7ae86 4ff48370 f13cf01c 49adb9f2 3d19a439 f743ee77 03cf342d 87f43110 5c843c78 ca4df639 931f3458 fae8a94d 1687e99a 76ed99d0 ba87189f 42fd31ad 8262c54a 8cf5914a e6c28c54 0d714a5f 6087a172 fb74f481 4c6f968d 72386ef3 45a05180 c3b3c7dd d5ef6fe7 6b0531c3

z= 56c03667 f3b50335 ad532d0a dcaa2897 a02c0878 099d8e3a ab9d80b2 b5c83e2f 14c78cee 664bce7d 209e0fd8 b73f7f68 22fcdf6f fade5af2 ddbb38ff 3d2270ce bbed172d 7c399f47 ee9f1067 f1b85ccb ec8f43b7 21b4f980 2f3ea51a 8acd1f6f b526ecf4 a45ad62b 0ac17551 727b6a7c 7aadb936 2394b410 611a21a7 711dcde2

To compute with huge integers like these, you need a special "multiple precision" software package, because the built-in arithmetic on computer chips handles only 32 or 64 bits.

F. Solution to the homework problem

Here p=11, g=2, x A = 9, x B = 4. So y A = 2 x A = 2 9 (mod 11).

You can find this most easily by finding 2 2 =4, 2 4 = 4 2 = 16 (mod 11), 2 8 = (2 4 ) 2 5 2 = 25 3 (mod 11), and finally 2 9 = 2 x 2 8 2 x 3 = 6. So y A = 6.

Similary, 2 x B = 2 4 = 16 5 (mod 11), so y B = 5.

The secret shared key z A is the remainder of y B x A = 5 9 (mod 11). So find 5 2 = 25 3 (mod 11), 5 4 = (5 2 ) 2 3 2 = 9 (mod 11), 5 8 = (5 4 ) 2 9 2 = 81 4 (mod 11), 5 9 = 5 x 5 8 5 x 4 = 20 9 (mod 11). As a check, z B is the remainder of y A x B = 6 4 (mod 11). 6 2 = 36 3 (mod 11) so 6 4 = (6 2 ) 2 3 2 = 9 (mod 11), which checks. So z A = z B = 9.
Next: About this document Up: No Title Previous: No Title

Kirby A. Baker
Sun Mar 21 19:36:51 PST 1999