You are on page 1of 35

Cryptography and Network Security

Spring 2006
http://www.abo.fi/~ipetre/crypto/

Lecture 7: Public-key cryptography and RSA

Ion Petre
Academy of Finland and
Department of IT, Åbo Akademi University

April 6, 2006 1
Some unanswered questions on symmetric
cryptosystems

„ Key management: changing the secret key or establishing one is


nontrivial
‰ Change the keys two users share (should be done reasonably often)
‰ Establish a secret key with somebody you do not know and cannot meet
in person: (e.g., visiting secure websites such as e-shops)
‰ This could be done via a trusted Key Distribution Center (details in a
future lecture)
‰ Can (or should) we really trust the KDC?
‰ “What good would it do after all to develop impenetrable cryptosystems, if
their users were forced to share their keys with a KDC that could be
compromised by either burglary or subpoena?” – Diffie, 1988
„ Digital signatures: one should make sure that a message came
intact from the claimed sender

April 6, 2006 2
A breakthrough idea

„ Rather than having a secret key that the two users must share, each
users has two keys
‰ One key is secret and he is the only one who knows it
‰ The other key is public and anyone who wishes to send him a
message uses that key to encrypt the message
‰ Diffie and Hellman first (publicly) introduced the idea in 1976 – this was
radically different than all previous efforts
‰ NSA claims to have known it sine mid-1960s!
‰ Communications-Electronic Security Group (the British counterpart of
NSA) documented the idea in a classified report in 1970

April 6, 2006 3
A word of warning

„ Public-key cryptography complements rather than replaces


symmetric cryptography
‰ There is nothing in principle to make public-key crypto more secure than
symmetric crypto
‰ Public-key crypto does not make symmetric crypto obsolete: it has its
advantages but also its (major) drawbacks such as speed
‰ Due to its low speed, it is mostly confined to key management and
digital signatures

April 6, 2006 4
The idea of public-key cryptography

„ The concept was proposed in 1976 by Diffie and Hellman although


no practical way to design such a system was suggested
„ Each user has two keys: one encryption key that he makes public
and one decryption key that he keeps secret
‰ Clearly, it should be computationally infeasible to determine the
decryption key given only the encryption key and the cryptographic
algorithm
„ Some algorithms (such as RSA) satisfy also the following useful
characteristic:
‰ Either one of the two keys can be used for encryption – the other one
should then be used to decrypt the message
„ First we will investigate the concept with no reference yet to practical
design of a public-key system

April 6, 2006 5
Essential steps in public-key encryption

„ Each user generates a pair of keys to be used for encryption and


decryption
„ Each user places one of the two keys in a public register and the
other key is kept private
„ If B wants to send a confidential message to A, B encrypts the
message using A’s public key
„ When A receives the message, she decrypts it using her private key
‰ Nobody else can decrypt the message because that can only be done
using A’s private key
‰ Deducing a private key should be infeasible
„ If a user wishes to change his keys – generate another pair of keys
and publish the public one: no interaction with other users is needed

April 6, 2006 6
Bob sends an encrypted message to Alice

April 6, 2006 7
Some notation

„ The public key of user A will be denoted KUA


„ The private key of user A will be denoted KRA
„ Encryption method will be a function E
„ Decryption method will be a function D
„ If B wishes to send a plain message X to A, then he sends the
cryptotext Y=E(KUA,X)
„ The intended receiver A will decrypt the message: D(KRA,Y)=X

April 6, 2006 8
A first attack on the public-key scheme –
authenticity

„ Immediate attack on this scheme:


‰ An attacker may impersonate user B: he sends a message E(KUA,X)
and claims in the message to be B – A has no guarantee this is so
‰ This was guaranteed in classical cryptosystems simply through knowing
the key (only A and B are supposed to know the symmetric key)
„ The authenticity of user B can be established as follows:
‰ B will encrypt the message using his private key: Y=E(KRB,X)
‰ This shows the authenticity of the sender because (supposedly) he is the
only one who knows the private key
‰ The entire encrypted message serves as a digital signature
‰ Note: this may not be the best possible solution: ideally, digital signatures
should be rather small so that one can preserve many of them over a
long period of time
„ Better schemes will be presented a couple of lectures on

April 6, 2006 9
A scheme to authenticate the sender of the message

April 6, 2006 10
Encryption and authenticity

„ Still a drawback: the scheme on the previous slide authenticate but


does not ensure security: anybody can decrypt the message using
B’s public key
„ One can provide both authentication and confidentiality using the
public-key scheme twice:
‰ B encrypts X with his private key: Y=E(KRB,X)
‰ B encrypts Y with A’s public key: Z=E(KUA,Y)
‰ A will decrypt Z (and she is the only one capable of doing it):
Y=D(KRA,Z)
‰ A can now get the plaintext and ensure that it comes from B (he is the
only one who knows his private key): decrypt Y using B’s public key:
X=E(KUB,Y)

April 6, 2006 11
Secrecy and authentication using public-key schemes

April 6, 2006 12
Applications for public-key cryptosystems

1. Encryption/decryption: sender encrypts the message with the


receiver’s public key
2. Digital signature: sender “signs” the message (or a representative
part of the message) using his private key
3. Key exchange: two sides cooperate to exchange a secret key for
later use in a secret-key cryptosystem

April 6, 2006 13
Requirements for public-key cryptosystems

„ Generating a key pair (public key, private key) is computationally


easy
„ Encrypting a message using a known key (his own private or
somebody else’s public) is computationally easy
„ Decrypting a message using a known key (his own private or
somebody else’s public) is computationally easy
„ Knowing the public key, it is computationally infeasible for an
opponent to deduce the private key
„ Knowing the public key and a ciphertext, it is computationally
infeasible for an opponent to deduce the private key
„ Useful extra feature: encryption and decryption can be applied in
any order:
E( KUA, D(KRA,X) ) =D(KRA, E( KUA, X) )

April 6, 2006 14
Designing a public-key cryptosystem

„ Computationally easy usually means polynomial-time algorithm


„ Computationally infeasible more difficult to define
‰ Usually means super-polynomial-time algorithms, e.g., exponential-time algorithms
‰ Classical complexity analysis (worst-case complexity or average-case complexity) are
worthless in cryptography – one should make sure a problem is difficult for virtually all
inputs and not just in the worse or in the average case
„ Public-key cryptosystems usually rely on difficult math functions rather than S-P
networks as classical cryptosystems
‰ One-way function: easy to calculate in one direction, infeasible to calculate in the
other direction (i.e., the inverse is infeasible to compute)
‰ Trap-door function: difficult function that becomes easy if some extra information is
known
‰ Aim: find a trap-door one-way function for encryption – decryption will be the inverse

April 6, 2006 15
RSA

„ One of the first proposals on implementing the concept of public-key


cryptography was that of Rivest, Shamir, Adleman – 1977: RSA
„ The RSA scheme is a block cipher in which the plaintext and the ciphertext
are integers between 0 and n-1 for some fixed n
‰ Typical size for n is 1024 bits (or 309 decimal digits)
‰ To be secure with today’s technology size should between 1024 and 2048 bits
„ Idea of RSA: it is a difficult math problem to factorize (large) integers
‰ Choose p and q odd primes, n=pq
‰ Choose integers d,e such that Med=M mod n, for all M<n
‰ Plaintext: block of k bits, where 2k<n≤2k+1 – can be considered a number M with
M<n
‰ Encryption: C=Me mod n
‰ Decryption: Cd mod n = Mde mod n = M
‰ Public key: KU={e,n}
‰ Private key: KR={d,n}
„ Question: How do we find d,e?
‰ Answer: Number Theory!

April 6, 2006 16
Motto for our introduction to Number Theory

The Devil said to Daniel Webster: "Set me a task I can't carry out, and I'll give you anything
in the world you ask for."

Daniel Webster: "Fair enough. Prove that for n greater than 2, the equation an + bn = cn has
no non-trivial solution in the integers."

They agreed on a three-day period for the labour, and the Devil disappeared.

At the end of three days, the Devil presented himself, haggard, jumpy, biting his lip. Daniel
Webster said to him, "Well, how did you do at my task? Did you prove the theorem?'

"Eh? No . . . no, I haven't proved it."

"Then I can have whatever I ask for? Money? The Presidency?'

"What? Oh, that—of course. But listen! If we could just prove the following two lemmas—"
—The Mathematical Magpie, Clifton Fadiman

April 6, 2006 17
Notions of number theory

„ Fermat’s little theorem: if p is prime and a is positive integer not divisible


by p, then ap-1 ≡ 1 mod p
„ Corollary: For any positive integer a and prime p, ap ≡ a mod p
„ Comments:
‰ This is a first step in our quest to find Med=M mod n – not quite enough though
‰ Fermat’s little theorem provides a necessary condition for an integer p to be
prime – the condition is not sufficient
„ We will turn this theorem into a (probabilistic) test for primality
„ Example:
‰ p=5, a=3, 35=243=3 mod 5
‰ p=5, a=10, 105=100000=10 mod 5 = 0 mod 5
„ Fermat’s theorem, as useful as will turn out to be does not provide us with
integers d,e we are looking for – Euler’s theorem (a refinement of
Fermat’s) does

April 6, 2006 18
Euler’s totient function
„ Euler’s function associates to any positive integer n a number φ(n): the
number of positive integers smaller than n and relatively prime to n
‰ Example:
„ φ(37)=36
„ φ(p)=p-1, for any prime p
„ φ(35)=24: {1,2,3,4,6,8,9,11,12,13,16,17,18,19,22,23,24,26,27,29,31,32,33,34}
‰ Easy to see that for any two primes p,q, φ(pq)=(p-1)(q-1)
„ All numbers smaller than pq are relatively primes with pq except for multiples of
p (q-1 of them) and multiples of q (p-1 of them)
„ Euler’s theorem: for any relatively prime integers a,n we have aφ(n)≡1
mod n
„ Corollary: For any integers a,n we have aφ(n)+1≡ a mod n
„ Corollary: Let p,q be two odd primes and n=pq. Then:
‰ φ(n)=(p-1)(q-1)
‰ For any integer m with 0<m<n, m(p-1)(q-1)+1≡m mod n
‰ For any integers k,m with 0<m<n, mk(p-1)(q-1)+1≡m mod n

April 6, 2006 19
Back to RSA

„ Euler’s theorem provides us the numbers d,e such that Med=M mod n
‰ We have to choose d,e such that ed=kφ(n)+1, or equivalently, d≡e-1 mod φ(n)
‰ Extended Euclid’s algorithm!
„ The RSA scheme
‰ Key generation
„ Choose two odd primes p,q – keep private
„ Compute n=pq – make public
„ Choose e, 1<e<φ(n) with gcd(φ(n),e)=1 – make public
„ Compute d≡e-1 mod φ(n) – keep private
„ Private key is {d,n}
„ Public key is {e,n}
‰ Encryption
„ Plaintext: block of k bits, where 2k<n≤2k+1 – can be considered a number M with M<n
„ Ciphertext: C=Me mod n
‰ Decryption:
„ Ciphertext: C
„ Plaintext: Cd mod n = Mde mod n = M

April 6, 2006 20
Example
„ Key generation RSA scheme
‰ Select primes p=17, q=11
–Key generation
‰ Compute n=pq=187
•Choose primes p,q
‰ Compute φ(n)=(p-1)(q-1)=160
•Compute n=pq
‰ Select e=7 •Choose e, 1<e<φ(n) with gcd(φ(n),e)=1
‰ Compute d: d=23 (use the extended •Compute d≡e-1 mod φ(n)
Euclid’s algorithm)
•Private key is {d,n}
‰ KU={7,187}
•Public key is {e,n}
‰ KR={23,187}
–Encryption
„ Encrypt M=88: 887 mod 187 •C=Me mod n
‰ 887 mod 187 = [ (884 mod 187)(882 mod
187) (88 mod 187) ] = 11 –Decryption:
•Cd mod n = Mde mod n = M
„ Decrypt C=11: 1123 mod 187
‰ M=1123 mod 187= [ (1116 mod 187)(114
mod 187) (112 mod 187)(11 mod 187)]
‰ 112 mod 187 =121
‰ 114 mod 187= 1212 mod 187=55
‰ 118 mod 187=552 mod 187= 33
‰ 1116 mod 187=332 mod 187=154
‰ M=154 x 55 x 121 x 11 mod 187 = 88

April 6, 2006 21
Computational aspects – RSA implementation
Fast modular exponentiation

„ Take each step in turn and discuss RSA scheme


how can it be implemented –Key generation
efficiently •Choose primes p,q
•Compute n=pq
„ For encryption and decryption we •Choose e, 1<e<φ(n) with gcd(φ(n),e)=1
must be able to do quick modular •Compute d≡e-1 mod φ(n)
exponentiations – two ideas are •Private key is {d,n}
useful: •Public key is {e,n}
–Encryption
‰ (ab mod n) = [(a mod n)(b mod n)]
•C=Me mod n
‰ To compute x16 mod n we do not –Decryption:
have to do 15 multiplication but only •Cd mod n = Mde mod n = M
4: compute x2 mod n, x4 mod n, x8
mod n, x16 mod n
‰ Apply this to compute quickly any
exponent, not just powers of 2

April 6, 2006 22
Fast modular exponentiation
RSA scheme
„ Square-and-multiply algorithm –Key generation
‰ Input: n,x,b (b is in base 2 (bk-1,…,b1,b0), b≠0 •Choose primes p,q
‰ Output: xb mod n •Compute n=pq
1. z=1 •Choose e, 1<e<φ(n) with gcd(φ(n),e)=1
2. for i=k-1 downto 0 •Compute d≡e-1 mod φ(n)
3. z=z2 mod n
•Private key is {d,n}
4. if bi=1 then z=zx mod n
•Public key is {e,n}
„ Complexity O(r3), where r=[log2n]
–Encryption
„ Example: encrypt 9726 with
•C=Me mod n
KU={3533,11413}: 97263533mod 11413
‰ 3533=(1,1,0,1,1,1,0,0,1,1,0,1) –Decryption:
‰ Ciphertext: 5761 •Cd mod n = Mde mod n = M

i bi z i bi z
11 1 9726 5 0 77832=6298
10 1 97262x9726=2659 4 0 62982=4629
9 0 26592=5634 3 1 46292x9726=10185
8 1 56342x9726=9167 2 1 101852x9726=105
7 1 91672x9726=4958 1 0 1052=11025
6 1 49582x9726=7783 0 1 110252x9726=5761

April 6, 2006 23
Computational aspects – RSA implementation
Key generation

„ The highlighted part in the algorithm is


easy to implement RSA scheme
‰ Generate a series of random numbers –Key generation
and test each against φ(n) for relative •Choose primes p,q
primality •Compute n=pq
•Choose e, 1<e<φ(n) with gcd(φ(n),e)=1
‰ Testing whether or not two integers •Compute d≡e-1 mod φ(n)
are relatively prime and finding a •Private key is {d,n}
modular inverse can be done with the •Public key is {e,n}
extended Euclid’s algorithm –Encryption
•C=Me mod n
‰ Very few tests are needed before a
–Decryption:
usable e is found: the probability that
•Cd mod n = Mde mod n = M
two random numbers are relatively
prime is 0.6

April 6, 2006 24
Computational aspects – RSA implementation
Key generation

„ No practical techniques to yield large


prime numbers RSA scheme
„ Procedure: generate random odd –Key generation
numbers and test whether that integer is •Choose primes p,q
prime •Compute n=pq
„ Testing whether or not an integer n is a •Choose e, 1<e<φ(n) with gcd(φ(n),e)=1
•Compute d≡e-1 mod φ(n)
prime is a difficult problem (“primality is
•Private key is {d,n}
difficult”)
•Public key is {e,n}
‰ There has been a long standing question
–Encryption
in math whether or not primality can be
•C=Me mod n
tested in polynomial deterministic time
‰ Answer (2002): YES! –Decryption:
•Cd mod n = Mde mod n = M
‰ Manindra Agrawal, Neeraj Kayal and Nitin
Saxena, “PRIMES is in P”, Ann. of Math.
(2), 160:2 (2004) 781--793.
‰ Drawback: high complexity – O(log12n
f(log log n)), where f is a polynomial

April 6, 2006 25
Miller-Rabin primality test

„ Faster methods of testing primality exist – they are all probabilistic


‰ Such an algorithm can give two answers to the question “Is n prime?”
„ No, it is not
„ n is probably prime
„ The probability can be made arbitrarily large
‰ Other algorithms may give precise answer but with low probability they
may take a long time to finish
„ Most popular primality test: Miller- Rabin, based on Fermat’s little
theorem

April 6, 2006 26
Miller-Rabin primality test

„ Fermat’s little theorem: if p is prime and a is positive integer not divisible by p, then ap-1 ≡
1 mod p
„ Idea of the Miller-Rabin test:
‰ We need to test if the odd integer n is prime
‰ n-1 is even, i.e., of the form n-1=2kq, with k>0, q odd: k and q easy to find
‰ Choose an integer a such that 1<a<n-1
j k-1 k
‰ Compute modulo n the values a2 q, 0≤j≤q: aq, a2q,…, a2 q, a2 q
‰ By Fermat’s theorem, if n is prime, then the last value in the sequence is 1 –
the sequence may have some other 1s, consider the first 1 in the sequence
„ Case 1: the first number in the sequence is 1
j j-1
„ Case 2: some number a2 q in the sequence is 1 – in this case a2 q = n-1 mod n
j j-1 j-1 j-1 j-1
‰ 0 = (a2 q -1) mod n = (a2 q – 1) (a2 q + 1) mod n, i.e., n divides (a2 q – 1) or (a2 q + 1)
j-1 j-1
‰ Since we took the first 1 in the sequence, it follows that n divdes (a2 q + 1): a2 q = n-1 mod n
‰ The test: if either the first element in the sequence is 1, or some other element
is n-1, then n could be prime. Otherwise n is certainly not prime

April 6, 2006 27
Miller-Rabin primality test

•Question: for how many integers a does the test fail?


TEST(n) •Failure: n is not prime but the algorithm return
1. n-1=2kq: compute k and q “probably prime”
•Answer: for at most (n-1)/4 integers a with 1≤a≤n-1
2. Select a random integer a, •Thus, the probability of failure is at most ¼
1<a<n-1 •Practical implementation:
3. If aqmod n=1 then return •Repeatedly invoke TEST(n) using random
“probably prime” choices for a
•If TEST(n) return at least ones “not a prime”,
4. For j=0 to k-1 do
j then n is not a prime
5. If a2 q mod n = n-1, then return •If t executions of TEST(n) return “probably
“probably prime” prime”, then the probability that n is indeed a
6. Return “not a prime” prime is larger than 1-4-t
•t=10 gives probability larger than 0.999999

April 6, 2006 28
Computational aspects – RSA implementation
Key generation

„ To choose primes p,q we generate random RSA scheme


numbers p,q on the desired scale of magnitude
and test the primality with Miller-Rabin –Key generation
•Choose primes p,q
„ Question: How many trials should we expect to •Compute n=pq
do before we find a prime?
•Choose e, 1<e<φ(n) with
„ Distribution of primes gcd(φ(n),e)=1
‰ Prime number theorem: for any integer x, the •Compute d≡e-1 mod φ(n)
primes near x are spaced on average one every •Private key is {d,n}
log(x) integers
•Public key is {e,n}
‰ On average we have to test log(x) integers before
we find a prime – reject immediately even integers –Encryption
and integers ending in 5 •C=Me mod n
‰ Correct rate: we need to test in average 0.4 log(x) –Decryption:
integers before we find a prime of the order of x •Cd mod n = Mde mod n = M
‰ Example: if we look for a prime on the order of
magnitude 2200 we need to do in average 55 trials,
order of magnitude 21024: in average 284 trials

April 6, 2006 29
Attacking RSA
„ Brute force attacks: try all possible private keys
‰ As in the other cases defend using large keys:
nowadays integers between 1024 and 2048 bits
„ Mathematical attacks
‰ Factor n into its two primes p,q: this is a hard problem RSA scheme
for large n –Key generation
„ Challenges by RSA Labs to factorize large integers
„ Smallest unsolved challenge: 704 bits •Choose primes p,q
•Compute n=pq
‰ Determine φ(n) directly without first determining p,q:
this math problem is equivalent to factoring •Choose e with gcd(φ(n),e)=1
‰ Determine d directly, without first determining φ(n): this •Compute d≡e-1 mod φ(n)
is believed to be at least as difficult as factoring •Private key is {d,n}
„ Suggestions for design •Public key is {e,n}
‰ The larger the keys, the better but also the slower the –Encryption
algorithm •C=Me mod n
‰ Choosing p,q badly may weaken the algorithm –Decryption:
„ p,q should differ in length by only a few bits: for a 1024-bit •Cd mod n = Mde mod n = M
key, p,q should be on the order of magnitude 1075 to 10100
„ p-1 and q-1 should both contain a large prime factor
„ gcd(p-1,q-1) should be small
„ d should be larger than n1/4

April 6, 2006 30
Attacks on RSA
„ Timing attacks: determine a private key by keeping track
of how long a computer takes to decipher a message Square-and-multiply algorithm
(ciphertext-only attack) – this is essentially an attack on the – Input: n,x,b
fast exponentiation algorithm but can be adapted for any (b is in base 2 (bk-1 ,…,b1,b0)
other algorithm – Output: xb mod n
‰ Whenever a bit is 1 the algorithm has more computations to 1. z=1
do and takes more time 2. for i=k-1 downto 0
3. z=z2 mod n
„ Countermeasures: 4. if bi=1 then z=zx mod n
‰ Ensure that all exponentiations take the same time before
returning a result: degrade performance of the algorithm
‰ Add some random delay: if there is not enough noise the
attack succeeds
‰ Blinding: multiply the ciphertext by a random number before
performing exponentiation – in this way the attacker does not
know the input to the exponentiation algorithm. (implemented
in the commercial products from RSA Data Security Inc.)
„ Decryption M=Cd mod n is modified as follows:
„ Generate a secret random number r between 0 and n-1
„ Compute C’=C(re) mod n where e is the public exponent
„ Compute M’=(C’d) mod n with the ordinary exponentiation
„ Compute M=M’r-1 mod n
„ Reported performance penalty: 2 to 10%

April 6, 2006 31
Pseudo-random number generators

„ Essential in RSA (and elsewhere) to be able to generate pseudo-


random numbers
„ A sequence of numbers is random if they have uniform distribution and
are independent (no value can be deduced from the others)
„ We generally use algorithmic techniques to generate such numbers –
they will not be independent and thus not random
‰ The whole point is to make them “look” random, i.e., make them pass many
test of randomness
„ Three tests to be used in evaluating a pseudo-random number
generator
‰ The function should be full-period generating function: generate all
numbers in its range before repeating
‰ The generated sequence should appear random: pass many statistical test
‰ The function should implement efficiently with 32-bit arithmetic

April 6, 2006 32
Pseudo-random number generators

„ The most widely used technique is the linear congruential method (Lehmer
1951)
Xn+1=(aXn+c) mod m
„ One should be very careful in choosing constants a, c, m: a=c=1 is bad
choice!
„ Value of m should be as large as possible: usually close to 231, very often
chosen to be the prime number 231-1; in this case one can take c=0
„ There are very few good choices for a: for m= 231-1 only a handful of choices
are advisable – very often used is a=75=16807
Xn+1=16807 Xn mod (231-1)
„ Using this in cryptography needs extra care:
‰ If the attacker finds one single value, then he will be able to compute all
subsequent values
‰ Idea: restart the sequence often, using the clock as seed (initial value)

April 6, 2006 33
Cryptographically generated pseudo-random numbers

„ Idea: use cryptographic primitives to generate


pseudo-random numbers
„ One possibility: Use a counter and encrypt each
value for the counter, e.g., with DES – the
cryptotext will be the key
‰ Stronger version: instead of a counter use a
PRNG (pseudo-random number generator)
‰ Technique can be made stronger using a more
sophisticated scheme and 3DES, see ANSI
X9.17 PRNG

April 6, 2006 34
Another speed-up in RSA implementation

„ Operations modulo big integers become more time-consuming as


the integers grows bigger
„ Efficient implementation: use Chinese Remainder Theorem (CRT)
‰ In its simplest formulation, CRT essentially says that if n=pq, then
instead of addition/difference/multiplication modulo n one can perform
the same modulo p and modulo q and then compute the result mod n
„ Big advantage because the modules are much smaller

April 6, 2006 35

You might also like