COMP424
Computer Security

______________________________________

Prof. Wiegley
jeffw@csun.edu

Rivest, Shamir & Adelman (RSA)
Implementation

1


“Relatively prime”_______________________________________

2


“Modulo”_______________________________________________

3


“Modulo algebra (multiplication)”_______________________

4


“Modulo algebra (addition)”_____________________________

5


“Modulo algebra (inverse)”______________________________

6


“RSA”__________________________________________________

In order to encrypt and decrypt a message, RSA relies on three values:

n
e

d

The remaining slides present how n,e and d are computed.

7


Step 1: obtaining n______________________________________

  1. Pick a prime number, p.
  2. Pick a different prime number, q

Any prime numbers will work though in practice, to be secure, p and q should not be twin primes or small.

n is simply the product of the two primes p and q.

n = pq

8


Step 2: obtaining e______________________________________

  1. Pick any value e such that e is relatively prime to (p - 1)(q - 1)

But there is a method for quickly testing if a chosen value is relatively prime to (p - 1)(q - 1).

9


Euclidean Algorithm (used to sift possible values of e).___

  1. Choose a possible value for e.
  2. Test if e is relatively prime to (p - 1)(q - 1) using the Euclidean algorithm.

    Start by setting up a series of equation of the form:

    Ei : αi - βi ⋅δi = γi

    where βi = αδii. For each equation αi = δi-1 and δi = γi-1.

    This is esentially subtracting the largest possible multiple of δ from α. δ and the remainder, γ, must share a common factor in order for α and δ to share a common factor. Continue the equations until γi = 0.

10


Euclidean Algorithm example____________________________

Begin with α0 = (p - 1)(q - 1) and δ0 = e.

E0 :   (p - 1)(q - 1) -    α  ⋅  e  =   β
     (73 - 1)(61 - 1) -    α  ⋅  35 =   β
             72⋅60  -    α  ⋅  35 =   β
              4320  -  123  ⋅  35 =   15
E1 :            35  -    2  ⋅  15 =   5
E2 :            15  -    3  ⋅  5  =   0

Since the second last result is 5 then e and (p- 1)(q - 1) share the factor 5. So e is not relatively prime to (p - 1)(q - 1).

11


Increment e by 2 and repeat test.________________________

  1. Since the first choice for e was unacceptable, increment e by 2 and test again. Repeat this procedure until e is relatively prime to (p - 1)(q - 1).

12


  1. Test if e is proven to be relatively prime to (p - 1)(q - 1).
    E0 :   (p - 1)(q- 1)  -    α  ⋅ e   =   β
      (73 - 1)(61- 1)  -    α  ⋅ 37  =   β
              72⋅60  -    α  ⋅ 37  =   β
               4320  -  116  ⋅ 37  =   28
E1 :             37  -    1  ⋅ 28  =   9
E2 :             28  -    3  ⋅ 9   =   1
E3 :              9  -    9  ⋅ 1   =   0

    Since the second last result is 1 then the smallest common factor of e and (p - 1)(q - 1) is 1 and therefore e is relatively prime to (p - 1)(q - 1).

13


Step 3: Calculating d____________________________________

14


Negative d?_____________________________________________

15


Final results_____________________________________________

16


Encryption______________________________________________

So now we have a public key of {e,n} and a private key of {d,n} we can decrypt a message P by:

      e
C = P  mod n.

Similarly an encrypted message, C, can be decrypted to yield the original message, P, by:

P = Cd mod n.

This works because if P = Cd and C = Pe then

P = P ed = P e⋅d = P 1 = P.

17


ModPow________________________________________________

But Pe is going to be rediculously large. So large that modern calculators cannot carry out this computation.1

A method is needed to constrain the calculation to small numbers and we will use modulo arithmetic to provide this ability.

First, start with P1 mod n. We can see from the axioms that

  2    1         1
P  = (P  mod n)(P  mod n) mod n.

In general:

P a⋅b = (P a mod n)(P b mod n) mod n.

So let’s work with powers of 2 to aid the calculation of Pe.

18


Powers of 2______________________________________________

Let’s take an example using the key computed earlier:

P = 101,e = 75,n = 391.

First, notice that

Pe = 10175 = 10164 ⋅1018 ⋅1012 ⋅1011,
Where the power (75) has been broken down into powers of 2.

We could have broken it down in many ways but powers of two will decrease our work the most.

So first calculate all the values of 101 raised to a power of 2.

19


Calculating exponents of powers of 2_____________________

The first power of 2 is easy:

   1
101 mod n = 101.
Now, for all other powers of two we can use the axiom as a trick:
101x mod n = (101 x2 mod n)(101x2 mod n) mod n.
Since we are only interest in powers of 2, x
2 will simply be the value computed in the previous iteration.

20


The computation________________________________________

Remembering that n = 391, we have:

1011 mod 391  =   101
1012 mod 391  =   (101)(101) mod 391 =   35
1014 mod 391  =   (35)(35) mod 391    =   52
1018 mod 391  =   (52)(52) mod 391    =   358
10116 mod 391 =   (358)(358) mod 391 =   307
10132 mod 391 =   (307)(307) mod 391 =   18
10164 mod 391 =   (18)(18) mod 391    =   324.

Now, these values can be used to quickly compute 101y where y 127.

21


The computation________________________________________

   75         64    8     2    1
101    =  (101  ⋅101 ⋅101 ⋅101 ) mod 391
       =  (324⋅358⋅35 ⋅101) mod 391

So life is a bit simpler but we still have a long string of factors that could produce a number larger than our calculator/computer can deal with.

To reduce the number of factors we can make use

  a⋅b     a         b
P   =  (P  mod  n)(P  mod n) mod n.

22


Combining factors_______________________________________

This will enable us to combine factors two (or more) at a time.

(324⋅358) mod 391 =   (324 mod 391⋅358 mod 391) mod 391

                  =   (324 ⋅358) mod 391
                  =   115992 mod 391
                  =   256.
Similarly:
(35⋅101) mod 391 =   (35 mod 391⋅101 mod 391) mod 391
                 =   (35 ⋅101) mod 391

                 =   3535 mod 391
                 =   16.

23


Combining factors_______________________________________

So

10175  =  (10164 ⋅1018 ⋅1012 ⋅1011) mod 391
       =  (324⋅358⋅35 ⋅101) mod 391

       =  (256⋅16) mod 391
       =  4096 mod 391
       =  186

The cipher text message, after encryption, is therefore 186.

The proof that decryption yields the original message is left as an exercise for the reader.

24


Proving the correctness of RSA__________________________

Lagrange’s theorem states:

φ(n)
b   = 1 mod n.

Where φ(n) = the number of integers less than n that are relatively prime to n.

For RSA, let n = pq. (you can guess where RSA started their thinking now.)

Then, how many relatively prime integers are there in φ(pq)?

25


Combining factors_______________________________________

Take pq, We know that p and q are prime so we can determine φ(pq)

So,

φ(pq) =   pq- p- q- 1
      =   (p - 1)(q- 1).

Therefore,

 φ(n)   (p-1)(q-1)
b   = b         = 1 mod n.
Keep that in mind, you’ll need it in a second.

26


RSA proof______________________________________________

e⋅d =   1 mod (p- 1)(q- 1)
We’re working in modulo so we could have gone “around” the ring some arbitrary number of times. Let l be the number of wraps. Then
e⋅d  =  1 +l(p- 1)(q- 1)
  ed       e⋅d
Pe⋅d  =  P 1+l(p-1)(q-1)
P    =  P
     =  P 1⋅Pl(p-1)(q-1)
     =  P 1⋅(P(p-1)(q-1))l
     =  P 1⋅(1 mod n)l [By lagrange′s substitution!]
     =  P 1⋅(1)l
          1
     =  P 1⋅1
     =  P
     =  P

27


Conclusion______________________________________________

By using Lagrange’s theorem we have proven that

(Pe)d = P,
and thus RSA works as advertised so long as our constraints about selecting p, q and e are met.

28