SWE3002- INFORMATION
AND SYSTEMS SECURITY
Module 3 : Public key Cryptosystems
• Number theory fundamentals
• Principles of pubic key crypto systems
• RSA algorithm
• Diffie-Hellman key exchange
Introduction to Number Theory
• Motivation
• Easy do – difficult undo – many crypto problems are of this nature
Easy Difficult
Fast exponentiation Discrete logarithm
Multiplying two large Factoring a huge product
numbers
The basics – and a few minor details
• Modulo arithmetic
• Addition and additive inverse are easy
• Multiplicative inverse doesn’t always exist
• Properties of primes
• A prime is divisible only by itself and one
• Determining primality is not all that easy
• Multiword arithmetic
• Additional method – Chinese remainder theorem
• Finding inverses in finite fields
• Modified Euclid’s algorithm applies here also
Useful results of number theory
• Public key crypto
• RSA algorithm
• Elliptic curve cryptography
• Diffie-Hellman algorithm
• Generates a shared secret key
• Chinese remainder theorem
• Sometimes results in easier multiword arithmetic algorithms
• Generation and testing of large primes
• Useful in all the above
Useful theorems involving ax mod n
• Fermat’s
• ap-1 = 1 mod p, p doesn’t divide a
• Euler’s phi function
• (n) = number of numbers <n and relatively prime to n
• Easily found if factorization is known
• Euler’s theorem
• a (n) = 1 mod n – reduces to Fermat’s for n prime
• Miller-Rabin test
• Based on inverse of Fermat’s theorem
n is not prime if an-1 K1 mod n
• Fast exponentiation
• Convert x to binary – for example x8 is x squared three times
Prime Numbers
• prime numbers only have divisors of 1 and self
• they cannot be written as a product of other numbers
• note: 1 is prime, but is generally not of interest
• eg. 2,3,5,7 are prime, 4,6,8,9,10 are not
• prime numbers are central to number theory
• list of prime number less than 200 is:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79
83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163
167 173 179 181 191 193 197 199
The prime factorization theorem
• A prime is a number divisible only by itself and one
• Any number can be factored uniquely into a product of
primes to some power
• Example 1100 = 2252111
• Relatively prime means (a,b)=1
• (a,b) means gcd(a,b)
• (a,b) is found using Euclid’s algorithm
Prime Factorisation
• to factor a number n is to write it as a product of other
numbers: n=a x b x c
• note that factoring a number is relatively hard compared to
multiplying the factors together to generate the number
• the prime factorisation of a number n is when its written as a
product of primes
• eg. 91=7x13 ; 3600=2 x3 x5
4 2 2
Euclidean Algorithm
• is a simple procedure for determining the GCD of two positive integers.
• Simple definition: Two integers are relatively prime if their only common positive integer
factor is 1.
Let us consider a = q1b + r1
0 ≤ r1 < b
EX : gcd (710,310)
gcd (a,b) => a =qb+r
710 = 2 x 310 + 90
310 = 3 x 90 + 40
90 = 2 x 40 + 10
40 = 4 x 10 +0
gcd(a, b) = (rn , 0) = rn
Relatively Prime Numbers & GCD
• two numbers a, b are relatively prime if have no common
divisors apart from 1
• eg. 8 & 15 are relatively prime since factors of 8 are 1,2,4,8 and of 15
are 1,3,5,15 and 1 is the only common factor
• conversely can determine the greatest common divisor by
comparing their prime factorizations and using least powers
• eg. 300=21x31x52 18=21x32 hence
• GCD(18,300)=21x31x50=6
Euler Totient Function ø(n)
• when doing arithmetic modulo n
• complete set of residues is: 0..n-1
• reduced set of residues is those numbers (residues) which
are relatively prime to n
• eg for n=10,
• complete set of residues is {0,1,2,3,4,5,6,7,8,9}
• reduced set of residues is {1,3,7,9}
• number of elements in reduced set of residues is called the
Euler Totient Function ø(n)
Euler Totient Function ø(n)
• to compute ø(n) need to count number of residues to be
excluded
• in general need prime factorization, but
• for p (p prime) ø(p) = p-1
• for p.q (p,q prime) ø(pq) =(p-1)x(q-1)
• eg.
ø(37) = 36
ø(21) = (3–1)x(7–1) = 2x6 = 12
Fermat's Theorem
• ap-1 = 1 (mod p)
• where p is prime and gcd(a,p)=1
• also known as Fermat’s Little Theorem
• also ap = p (mod p)
• useful in public key and primality testing
• Also, it is an affirmative, but not negative, test for
primality
Euler's Theorem
• a generalisation of Fermat's Theorem
• aø(n) = 1 (mod n)
• for any a,n where gcd(a,n)=1
• eg.
a=3;n=10; ø(10)=4;
hence 34 = 81 = 1 mod 10
a=2;n=11; ø(11)=10;
hence 210 = 1024 = 1 mod 11
Chinese Remainder Theorem
• used to speed up modulo computations
• if working modulo a product of numbers
• eg. mod M = m1m2..mk
• Chinese Remainder theorem lets us work in each
moduli mi separately
• since computational cost is proportional to size,
this is faster than working in the full modulus M
Chinese Remainder Theorem
• can implement CRT in several ways
• to compute A(mod M)
• first compute all ai = A mod mi separately
• determine constants ci below, where Mi = M/mi
• then combine results to get answer using:
Public Key Cryptography and RSA
Private-Key Cryptography
traditional private/secret/single key cryptography uses one key
shared by both sender and receiver
if this key is disclosed communications are compromised
also is symmetric, parties are equal
hence does not protect sender from receiver forging a message & claiming
is sent by sender
Public-Key Cryptography
• probably most significant advance in the 3000 year history of cryptography
• uses two keys – a public & a private key
• asymmetric since parties are not equal
• uses clever application of number theoretic concepts to function
• complements rather than replaces private key crypto
Why Public-Key Cryptography?
• developed to address two key issues:
• key distribution – how to have secure
communications in general without having to trust a
KDC with your key
• digital signatures – how to verify a message comes
intact from the claimed sender
• public invention due to Whitfield Diffie & Martin
Hellman at Stanford Uni in 1976
• known earlier in classified community
Public-Key Cryptography
• public-key/two-key/asymmetric cryptography
involves the use of two keys:
• a public-key, which may be known by anybody, and can
be used to encrypt messages, and verify signatures
• a related private-key, known only to the recipient, used
to decrypt messages, and sign (create) signatures
• infeasible to determine private key from public
• is asymmetric because
• those who encrypt messages or verify signatures cannot
decrypt messages or create signatures
Public-Key Cryptography
Symmetric vs Public-Key
Public-Key Cryptosystems
Public-Key Applications
• can classify uses into 3 categories:
• encryption/decryption (provide secrecy)
• digital signatures (provide authentication)
• key exchange (of session keys)
• some algorithms are suitable for all uses, others
are specific to one
Public-Key Requirements
• Public-Key algorithms rely on two keys where:
• it is computationally infeasible to find decryption key knowing only
algorithm & encryption key
• it is computationally easy to en/decrypt messages when the relevant
(en/decrypt) key is known
• either of the two related keys can be used for encryption, with the other
used for decryption (for some algorithms)
• these are formidable requirements which only a few algorithms
have satisfied
Public-Key Requirements
• need a trapdoor one-way function
• one-way function has
• Y = f(X) easy
• X = f–1(Y) infeasible
• a trap-door one-way function has
• Y = fk(X) easy, if k and X are known
• X = fk–1(Y) easy, if k and Y are known
• X = fk–1(Y) infeasible, if Y known but k not known
• a practical public-key scheme depends on a
suitable trap-door one-way function
Security of Public Key Schemes
like private key schemes brute force exhaustive
search attack is always theoretically possible
but keys used are too large (>512bits)
security relies on a large enough difference in
difficulty between easy (en/decrypt) and hard
(cryptanalyse) problems
more generally the hard problem is known, but is
made hard enough to be impractical to break
requires the use of very large numbers
hence is slow compared to private key schemes
RSA
by Rivest, Shamir & Adleman of MIT in 1977
best known & widely used public-key scheme
based on exponentiation in a finite (Galois) field
over integers modulo a prime
exponentiation takes O((log n)3) operations (easy)
uses large integers (eg. 1024 bits)
security due to cost of factoring large numbers
factorization takes O(e log n log log n) operations (hard)
RSA En/decryption
• to encrypt a message M the sender:
• obtains public key of recipient PU={e,n}
• computes: C = Me mod n, where 0≤M<n
• to decrypt the ciphertext C the owner:
• uses their private key PR={d,n}
• computes: M = Cd mod n
• note that the message M must be smaller than the modulus n
(block if needed)
RSA Key Setup
• each user generates a public/private key pair by:
• selecting two large primes at random: p, q
• computing their system modulus n=p.q
• note ø(n)=(p-1)(q-1)
• selecting at random the encryption key e
• where 1<e<ø(n), gcd(e,ø(n))=1
• solve following equation to find decryption key d
• e.d=1 mod ø(n) and 0≤d≤n
• publish their public encryption key: PU={e,n}
• keep secret private decryption key: PR={d,n}
RSA Example - Key Setup
1. Select primes: p=17 & q=11
2. Calculate n = pq =17 x 11=187
3. Calculate ø(n)=(p–1)(q-1)=16x10=160
4. Select e: gcd(e,160)=1; choose e=7
5. Determine d: de=1 mod 160 and d < 160
Value is d=23 since 23x7=161= 10x160+1
6. Publish public key PU={7,187}
7. Keep secret private key PR={23,187}
RSA Example - En/Decryption
sample RSA encryption/decryption is:
given message M = 88 (nb. 88<187)
encryption:
C = 887 mod 187 = 11
decryption:
M = 1123 mod 187 = 88
Exponentiation
• can use the Square and Multiply Algorithm
• a fast, efficient algorithm for exponentiation
• concept is based on repeatedly squaring base
• and multiplying in the ones that are needed to compute the
result
• look at binary representation of exponent
• only takes O(log2 n) multiples for number n
• eg. 75 = 74.71 = 3.7 = 10 mod 11
• eg. 3129 = 3128.31 = 5.3 = 4 mod 11
Efficient Encryption
• encryption uses exponentiation to power e
• hence if e small, this will be faster
• often choose e=65537 (216-1)
• also see choices of e=3 or e=17
• but if e too small (eg e=3) can attack
• using Chinese remainder theorem & 3 messages with different modulii
• if e fixed must ensure gcd(e,ø(n))=1
• ie reject any p or q not relatively prime to e
Efficient Decryption
• decryption uses exponentiation to power d
• this is likely large, insecure if not
• can use the Chinese Remainder Theorem (CRT) to compute
mod p & q separately. then combine to get desired answer
• approx 4 times faster than doing directly
• only owner of private key who knows values of p & q can use
this technique
RSA Key Generation
• users of RSA must:
• determine two primes at random p, q
• select either e or d and compute the other
• primes p,q must not be easily derived from modulus n=p.q
• means must be sufficiently large
• typically guess and use probabilistic test
• exponents e, d are inverses, so use Inverse algorithm to
compute the other
RSA Security
• possible approaches to attacking RSA are:
• brute force key search - infeasible given size of numbers
• mathematical attacks - based on difficulty of computing ø(n),
by factoring modulus n
• timing attacks - on running of decryption
• chosen ciphertext attacks - given properties of RSA
Factoring Problem
• mathematical approach takes 3 forms:
• factor n=p.q, hence compute ø(n) and then d
• determine ø(n) directly and compute d
• find d directly
• currently believe all equivalent to factoring
• have seen slow improvements over the years
• as of May-05 best is 200 decimal digits (663) bit with LS
• biggest improvement comes from improved algorithm
• currently assume 1024-2048 bit RSA is secure
• ensure p, q of similar size and matching other constraints
Progress in Factoring
Progress
in
Factoring
Timing Attacks
• developed by Paul Kocher in mid-1990’s
• exploit timing variations in operations
• eg. multiplying by small vs large number
• or IF's varying which instructions executed
• infer operand size based on time taken
• RSA exploits time taken in exponentiation
• countermeasures
• use constant exponentiation time
• add random delays
• blind values used in calculations
Chosen Ciphertext Attacks
RSA is vulnerable to a Chosen Ciphertext Attack
(CCA) attackers chooses ciphertexts & gets
decrypted plaintext back
choose ciphertext to exploit properties of RSA to
provide info to help cryptanalysis
can counter with random pad of plaintext or use
Optimal Asymmetric Encryption Padding (OASP)
Primitive Roots
• from Euler’s theorem have aø(n)mod n=1
• consider am=1 (mod n), GCD(a,n)=1
• must exist for m = ø(n) but may be smaller
• once powers reach m, cycle will repeat
• if smallest is m = ø(n) then a is called a primitive root
• if p is prime, then successive powers of a "generate" the group
mod p
• these are useful but relatively hard to find
Discrete Logarithms
• the inverse problem to exponentiation is to find the discrete
logarithm of a number modulo p
• that is to find x such that y = gx (mod p)
• this is written as x = logg y (mod p)
• if g is a primitive root then it always exists, otherwise it may not,
eg.
x = log3 4 mod 13 has no answer
x = log2 3 mod 13 = 4 by trying successive powers
• whilst exponentiation is relatively easy, finding discrete
logarithms is generally a hard problem
Diffie-Hellman Key Exchange
first
public-key type scheme proposed
by Diffie & Hellman in 1976 along with the
exposition of public key concepts
note: now know that Williamson (UK CESG)
secretly proposed the concept in 1970
is a practical method for public exchange
of a secret key
used in a number of commercial products
Diffie-Hellman Key Exchange
a public-key distribution scheme
cannot be used to exchange an arbitrary message
rather it can establish a common key
known only to the two participants
value of key depends on the participants (and
their private and public key information)
based on exponentiation in a finite (Galois) field
(modulo a prime or a polynomial) - easy
security relies on the difficulty of computing
discrete logarithms (similar to factoring) – hard
Diffie-Hellman Setup
all users agree on global parameters:
large prime integer or polynomial q
a being a primitive root mod q
each user (eg. A) generates their key
chooses a secret key (number): xA < q
xA
compute their public key: yA = a mod q
each user makes public that key yA
Diffie-Hellman Key Exchange
shared session key for users A & B is KAB:
[Link]
KAB = a mod q
xB
= yA mod q (which B can compute)
xA
= yB mod q (which A can compute)
KAB is used as session key in private-key
encryption scheme between Alice and Bob
if Alice and Bob subsequently communicate,
they will have the same key as before, unless
they choose new public-keys
attacker needs an x, must solve discrete log
Diffie-Hellman Example
users Alice & Bob who wish to swap keys:
agree on prime q=353 and a=3
select random secret keys:
A chooses xA=97, B chooses xB=233
compute respective public keys:
97
yA=3 mod 353 = 40 (Alice)
233
yB=3 mod 353 = 248 (Bob)
compute shared session key as:
x 97
KAB= yB A mod 353 = 248 = 160 (Alice)
x 233
KAB= yA B mod 353 = 40 = 160 (Bob)
Key Exchange Protocols
users could create random private/public
D-H keys each time they communicate
users could create a known private/public
D-H key and publish in a directory, then
consulted and used to securely
communicate with them
both of these are vulnerable to a Man-in-
the-Middle Attack
authentication of the keys is needed
Man-in-the-Middle Attack
1. Darth prepares by creating 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, forward all
messages between Alice & Bob
Reference
• William Stallings, Cryptography & Network Security- Principles
and Practices, 6th Edition by Pearson Publishers, 2014.