CR CR
RSA and Public Key Cryptography
Chester Rebeiro IIT Madras
STINSON : chapter 5, 6
RSA and Public Key Cryptography Chester Rebeiro IIT Madras CR CR - - PowerPoint PPT Presentation
RSA and Public Key Cryptography Chester Rebeiro IIT Madras CR CR STINSON : chapter 5, 6 Ciphers Symmetric Algorithms EncrypAon and DecrypAon use the same key i.e. K E = K D Examples: Block Ciphers : DES, AES, PRESENT, etc.
STINSON : chapter 5, 6
2
Alice Bob Plaintext “APack at Dawn!!” untrusted communicaAon link
The Key K is a secret
E D KE KD “APack at Dawn!!” encrypAon decrypAon #%AR3Xf34^$ (ciphertext)
3
Encryp<on Key KE not same as decryp<on key KD KE known as Bob’s public key; KD is Bob’s private key
Advantage : No need of secure key exchange between Alice and Bob
Asymmetric key algorithms based on trapdoor one-way func<ons
Press to lock (can be easily done) Once locked it is difficult to unlock without a key
4
Locked (difficult to unlock) Easily Unlocked trapdoor
5
6
– Integer FactorizaAon (in NP, maybe NP-complete)
– It is easy to compute N – However given N it is difficult to factorize into P and Q
– Discrete Log Problem (in NP)
7
– Only Alice, who owns the private key could have signed
8
9
Alice and Bob agree upon a prime p and a generator g. This is public information choose a secret a compute A = ga mod p choose a secret b compute B = gb mod p B A Compute K = Ba mod p Compute K = Ab mod p Ab mod p = (ga)b mod p = (gb)a mod p = Ba mod p
Diffie-Hellman Key Exchange
10
11
12
Bob first creates a pair of keys (one public the other private)
) , , ( ' ) , ( ' )) ( mod( Compute . 4 1 )) ( , gcd( and )) ( 1 ( random a Choose . 3 ) 1 )( 1 ( ) ( and Compute . 2 ) ( , primes large two Generate . 1
1
a q p is key private s Bob b n is key public s Bob n b a n b n b b q p n q p n q p q p φ φ φ φ
−
= = < < − − = × = ≠
Given the private key it is easy to compute the public key Given the public key it is difficult to derive the private key
13
Encryption
n b K
Z x where n x y x e ∈ = = mod ) (
Decryption
a K
14
395413 13 1
15
Encryption
n b K
Z x where n x y x e ∈ = = mod ) (
Decryption
a K
n t n t ab a b a
+
) ( 1 ) ( φ φ
n
From Fermat’s theorem
16
n
ab ab ab
ab
p t p t q q p t n t ab
+ +
) ( ) ( ) ( 1 ) ( ) ( 1 ) ( ϕ ϕ φ φ φ φ
17
c mod
i ei z 4 1 12* x = x 3 x2 2 1 x4 * x = x5 1 1 X10 * x = x11 1 x22 * x = x23
– Typically can perform arithmeAc on 8/16/32/64 bit numbers
(gmp library)
18
base : 2b, where b = 64/32/16/8 bits 1024 bits
19
= (2, 76, 176, 22, 234)256 = (80, 239, 242, 132)256 i ai bi cin ai+bi+cin(mod 256) Carry? cout 234 132 110 (110 < 234)? 1 1 22 242 1 9 (9 < 22)? 1 2 176 239 1 160 (160 ≤ 176)? 1 3 76 80 1 157 (157 ≤ 76)? 4 2 2 (2 ≤ 2)?
“ComputaAonal Number Theory”, Abhijit Das, CRC Press
20
21
= (2, 76, 176, 22, 234)256 = (80, 239, 242, 132)256 i ai bi
Cin Borrow?
Cout ai-bi-cin(mod 256) 234 132 (234 < 132)? 102 1 22 242 (22 < 242)? 1
2 176 239 1 (176 < 239)? 1
3 76 80 1 (76 < 80)? 1
4 2 1 (2 < 0)? 1
22
23
24
= (18, 214, 135)256 = (4, 143, 244, 234)256
25
( )
l l h l l h h h l h l h l l m l h l h l l h h m h h l l m h l l h m h h l m h l m h
b a b a b a b a b b a a b a B b b a a b a b a B b a b a B b a b a B b a b a b B b b a B a a n m Let n b a + − − = − − + − − + + + = + + + = × + = + = = − ) )( ( using ) )( ( ) ( ) ( ) ( 2 / . words ary B with integers sion multipreci two be , Let
2 2
Karatsuba multiplication converts n bit multiplications into 3 multiplications of n/2 bits The penalty is an increased number of additions
26 B = 256; a = 123456789 = (7, 91, 205, 21)256 b = 987654321 = (58, 222, 104, 177)256
n=4; m=2 ah = (7, 91); al = (205, 21) a = (7, 91)2562 + (205, 21) bh = (58, 222); bl = (104, 177) b = (58, 222)2562 + (104, 177)
ahbh = (1, 176, 254, 234)256 albl = (83, 222, 83, 133)256 ah - bh = -(197, 186)256 al - bl = -(45, 211)256 (ah - bh) (al - bl) = (35, 100, 170, 78)256 ahbl + albh = ahbh+ albl - (ah - bh) (al - bl) = (50, 42, 168, 33)256 1 176 254 234 50 42 168 33 83 222 83 133 1 177 49 20 251 255 83 133
27
28
c = a x b mod m No specific benefits this way
29
30
Montgomery's trick 1) t = a⋅ b 2) u= (t +((t mod R)⋅ m'mod R)⋅ m) / R 3) if (u ≥ m) return u − m; else return u.
31
Input: c, y Output: yc mod N exp(c,y){ R0 = 1 * R mod N R1 = y * R mod N for i=n-1 to 0 do if ci = 0 then R1 = R0 * R1 R0 = R0 * R0 else R0 = R0 * R1 R1 = R1 * R1 return (R0 * R-1) } Convert to Montgomery domain. Multiplications in Montgomery domain.
domain. Return to Original domain
32
33
34
35
32 Bit ARM Cortex 16 Bit TI Micro-controller
36
primes
37
38
hPps://en.wikipedia.org/wiki/Largest_known_prime_number
– For example, if n is approx 21024, then need to check around 2507 numbers
– Randomized algorithms
39
40
41
If n is prime, then is true for any ‘a’. Therefore the algorithm would always return FALSE. If n is composite is false but may be true for some choices of a. In this case, the algorithm may return TRUE sometime and FALSE other times. For example: n = 221 (13*17) and a = 38 then 38220 mod 221 ≡ 1. (FALSE returned) We need to increase our confidence with more values of a
1 ≡ −
1 ≡ −
42
43
Some composites act as primes. Irrespective of the ‘a’ chosen, the test passes. for example Carmichael numbers are composite numbers which satisfy Fermat’s little theorem irrespective of the value of a.
n an mod 1
1 ≡ −
44
b2 ≡1modn b2 −1≡ modn (b+1)(b−1) ≡ 0modn either(b+1) ≡ 0modn or(b−1) ≡ 0modn
n−1 2 = b
45
46
r
d d
2
r
d d
2
– The roots of x2 = 1 mod n is either +1 or -1 – In the sequence, if ad is 1, then all elements in the sequence will be 1 – If ad is not 1, then there should be some element in the sequence which is -1, in order to have the final element as 1
47
r
d d
2
1 (Fermat ‘s and we assume n is prime)
48
' composite is ' Otherwise . 5 ' prime is ' , 1 mod b c calculate , 1 , , 1 For . 4 ' prime is ' , 1 mod Compute . 3 nonzero a random at Select . 2 2 1 that such integer
an Find . 1
i
2
n return T n return c If n r i T n return b If n a b T Z a T d n d T
d n s
− = ≡ − = ± = = ∈ = −
49
If an element is not a quadratic residue, then it is a quadratic non-residue quadratic non-residues in Z13 are {2, 5, 6, 7, 8, 11} a cannot be 0
50
51
1 mod mod mod . . , when
1 2 ) 1 ( 2 2 1 2
≡ ≡ ≡ = ≡ ∈ ∃
− − −
p x p x a p x a t s Z x QR a is a
p p p p
▹
p
2 1 −
A result from Euler
p a a p
p
mod | when
2 1
≡
−
52
p p p p p p p
2 1 2 1 2 1 2 2 1 1 2 1 2
− − − − − −
53
p
2 1 −
6 6 2 1 13
−
7 2 1 15
−
7 2 1 15
−
Euler’s Witness Euler’s Liar Congruence always holds when n is an odd prime Congruence may
when n is not prime
54
} ) mod ( mod ) ( compute 1 1 that such integer random a choose ){ (
2 1
COMPOSITE return else PRIME possibly return n y x if n a y compute COMPOSITE return x if n a x n- a a n ASSEN SOLOVAYSTR
n
≡ = = ⎟ ⎠ ⎞ ⎜ ⎝ ⎛ = ≤ ≤
−
error probability is at most ½ How to compute Legendre’s symbol
55
4 3 2 1
e 4 e 3 e 2 e 1
4 3 2 1
4 3 2 1 e e e e
Then,
T
56
⎪ ⎪ ⎩ ⎪ ⎪ ⎨ ⎧ ⎟ ⎠ ⎞ ⎜ ⎝ ⎛ ≡ ≡ ⎟ ⎠ ⎞ ⎜ ⎝ ⎛ − = ⎟ ⎠ ⎞ ⎜ ⎝ ⎛ ⎟ ⎠ ⎞ ⎜ ⎝ ⎛ ⎟ ⎠ ⎞ ⎜ ⎝ ⎛ = ⎟ ⎠ ⎞ ⎜ ⎝ ⎛ = ⎟ ⎠ ⎞ ⎜ ⎝ ⎛ ⎟ ⎠ ⎞ ⎜ ⎝ ⎛ = ⎟ ⎠ ⎞ ⎜ ⎝ ⎛ ⎩ ⎨ ⎧ ± ≡ − ± ≡ = ⎟ ⎠ ⎞ ⎜ ⎝ ⎛ ⎟ ⎠ ⎞ ⎜ ⎝ ⎛ = ⎟ ⎠ ⎞ ⎜ ⎝ ⎛ ≡
a n a n if a n n a
is a if n t n n a t a even is a if n b n a n ab n if n if n n b n a then n b a If
k k
4 mod 3 , . 5 P 2 , 2 , . 4 P . 3 P 8 mod 3 1 8 mod 1 1 2 . 2 P mod . 1 P
57
From the theorem P5, P1, then P2 P5, P1, P5, P1, P3, P2 P5, P1 and 1 is a QR mod 13
58
59
Fundamental theorem of arithmeAc Any integer number (greater than 1) is either prime or a product of prime powers
60 k
e k e e e
2 1
3 2 1
prime generation algorithm
Prime factors of n cannot be greater than
⎣ ⎦
n
n = n / p : remove this factor from n
Running Time of algorithm order of π(n1/2)
61
q p n × =
choose arandom integer a(1< a < n). If gcd(a,n) ≠1,then a is a prime factor. However, this is most likely not the case.
1
Supposeweselectsome Land compute d=gcd(aL-1,n) if 1< d < n then we have factored n d | n and d |(aL −1) d has to be the prime p or the prime q
2
If gcd(aL −1,n) = n This is possible only when p | n and p | aL −1 (or q | n and q | aL −1) and aL −1> n
3
How to choose L? No easy way, trial and error!! Factorials have a lot of
way. So, take L as a factorial of some number r.
why aL-1? since d is prime and d |(aL −1) aL ≡1modd ϕ(d)| L => (d −1)k=L Thus we need to find L which is some factor of (d −1).
62
done! are we ;
factor prime the is 3 repeat and increment , 1
next value with 1 from again start , 1 gcd compute 3 done. are we n,
factor prime a is gcd then this , 1 gcd if 2 2 1 n else d S r d if else a S n d if , n)
d . S (a, n) > . S a . S
r!
= = ← ←
Pollard p-1 factorization for n. r = 2,3, 4, …..
When r = d-1 then L = r! = (d-1)! = d-1(d-2)! = (d-1)k (d-1) | L à we will get the gcd(ak(d-1), n) = n or its prime factor.
63
4 3 2 1
4 3 2 1
4 4 3 3 2 2 1 1
where
j i
j i j i
64
4 3 2 1
j i −
65
n n
Example :
2 +
− )
1 i i i n
66
41 ) 82123 , 63222 gcd( ) , gcd(
10 3
= = − N x x
A factor of N
Drawback… Large number of GCD
computations in this case Can we reduce the number
This column is just for understanding. In reality we will not know this Given xi mod N, we compute gcds of every pair until we find a gcd greater than 1
67
l t t
+
(and a shape like the Greek letter rho)
16 11 40 2 5 26 21 32 1
+
l j j
68
)) ( ( ) (
1 2 1 − −
= = = ∈ =
i i i i i n
y f f x y x f x Z y x random a choose
16 11 40 2 5 26 21 32 1
claim : The first time xi = yi mod p occurs when i ≤ t + l
d return N y x d If
i i
, ) , gcd( > − =
loop This means that we get a collision before x completing an entire circle
69
xi and yi meet at the same point in the cycle Therefore, yi must have traversed (some) cycles more
(−t modl)
70
71
2 2 ≡
2 2
72
2 2
2 2
7 ) 7 , 91 gcd( 13 ) 13 , 91 gcd( = =
So… we can use x and y to factorize N.
2 2 ≡
But how do we find such pairs?
73
2 2
32 and 200 are not perfect squares. However (32x200 = 6400) = 802 is a perfect square
2 2
Thus, it is possible to combine non-squares to form a prefect square
the examples are borrowed from Mark Stamp (http://cs.sjsu.edu/faculty/stamp/)
74
Recall, Fundamental theorem of arithmeAc Any integer number (greater than 1) is either prime or a product of prime powers
k
e k e e e
2 1
3 2 1
Thus, a number is a perfect square if it prime factors have even powers.
3 2 1
Thus, 32 = 2550 not a perfect square 200 = 2352 not a perfect square (32x200) = 2550 x 2352 = 2852 = (2451)2 is a prefect square
(A number is said to be b-smooth, if its factors are in this set)
– Compute – Test if y factors completely in the set B. – If NO, then discard. ELSE save (y, r) (these are called B-smooth numbers)
75
N r y mod
2
=
76
All numbers are 6-smooth except 60 and 75. Leave these and consider all others
2 3 5 7 11 13
1 1 1 20 2 1 63 2 1
1 1
1 1 1 80 4 1
77
2 3 5 7 11 13
1 1 1 20 2 1 63 2 1
1 1
1 1 1 80 4 1
78
Find rows where exponents sum is even
sum 2 2 2 2 2 2
1829 mod 901 1459 1829 mod ) 13 7 5 3 2 1 ( ) 85 61 43 42 (
2 2 2 2
≡ × × × × × − ≡ × × ×
79
1829 mod 901 1459 1829 mod ) 13 7 5 3 2 1 ( ) 85 61 43 42 (
2 2 2 2
≡ × × × × × − ≡ × × × 31 59 1829 31 ) 558 , 1829 gcd( 558 | 1829 59 ) 2360 , 1829 gcd( 2360 | 1829 ) 901 1459 )( 901 1459 ( | 1829 × = = = = = − + Thus ▹ ▹
– Fastest for less than 100 digits
– Fastest technique known so far for greater than 100 digits – Open source code (google GGNFS)
– Best so far is 768 bit factorizaAon – Current challenges 896 bits (reward $75,000), 1024 bit ($100,000)
80
https://en.wikipedia.org/wiki/RSA_Factoring_Challenge
81
82
) 1 ) ( ( 1 ) ( ) ( 1 ) ( ) 1 )( 1 ( ) ( /
2
= + + − − + + − = + + − = − − = = = n p n n p p n p n n q p pq q p n p n q pq n φ φ φ
Solve to get p (a factor of n)
There are two trivial and two non-trivial soluAons for The trivial soluAons are +1 and -1
83
2 ≡
⎩ ⎨ ⎧ ≡ ≡ 〈=〉 ≡ q y p y n y mod 1 mod 1 mod 1
2 2 2
By CRT, these congruences are equivalent
⎩ ⎨ ⎧ − ≡ ≡ p y p y mod 1 mod 1 ⎩ ⎨ ⎧ − ≡ ≡ q y q y mod 1 mod 1 q y p y mod 1 mod 1 − ≡ + ≡ q y p y mod 1 mod 1 + ≡ − ≡
To get the non-trivial solutions solve using CRT
84
q y p y mod 1 mod 1 − ≡ + ≡ q y p y mod 1 mod 1 + ≡ − ≡
(31⋅31−1 mod13−13⋅13−1 mod31)mod403 (31⋅8−13⋅12)mod403≡ 92 403− 92 = 311
403 mod 1 311 92 :
2 2
≡ ≡ Note
2 ≡
The non-trivial solutions are 92 and 311 What happens when we solve
q y p y mod 1 mod 1 + ≡ + ≡
85
1 ≡ −
ab
2 1 −
2 2
However we need to have a non- trivial result
86
" " 4 step ; 2 / ) even is ( . 7 ; " d is n
factor a " , 1 . 6 ) , 1 gcd( compute . 5 mod put . 4 message any choose . 3 2 1 Represent . 2 1 compute given . 1 failure return else goto t t t if exit return d if n y d n x y x ab t ab a
t
= ≠ − ← = − = −
) 1 )( 1 ( | mod ) 1 ( , mod 1
2 1 2 1 1
− + ≡ − ≡ =
−
y y n n y thus n x y
ab
1 ) ( ) ( mod 1 − = ≡ ab n k n ab φ φ we assume we know the private key a This will only work if y ≠±1 mod n. If y = ±1 mod n. then goto step 7
Probability of success of the attack is at-least 1/2
87
) ( 31 ) 403 , 310 gcd( 311 403 mod 2 403 mod 270 2 540 : 2 1 403 mod 2 403 mod 540 2 1080 : 1 2 1080 1
270 540
n
factor a x y t loop x y t loop x ab t
t t
= ≡ = ≡ = = ≡ = ≡ = = = = − = 1 403 mod 9 403 mod 135 2 270 : 3 1 403 mod 9 403 mod 270 2 540 : 2 1 403 mod 9 403 mod 540 2 1080 : 1 9 1080 1
135 270 540
≡ = ≡ = = ≡ = ≡ = = ≡ = ≡ = = = = − =
t t t
x y t loop x y t loop x y t loop x ab t can’t divide 135 further. failure
88
89
Alice m3mod N1 m m3mod N2 m3mod N2
Insecure channel c1 c2 c3
90
Alice m3mod N1 m m3mod N2 m3mod N2
Insecure channel c1 c2 c3
3 3 3 2 3 2 1 3 1
mod mod mod N m c N m c N m c ≡ ≡ ≡
– i.e. The message can be decrypted
91
) mod( mod mod mod
3 2 1 3 3 3 3 2 3 2 1 3 1
N N N m X N m c N m c N m c ⋅ ⋅ ≡ 〈=〉 ⎪ ⎩ ⎪ ⎨ ⎧ ≡ ≡ ≡
By CRT It is tempAng to have small private and public keys, so that encrypAon or decrypAon may be carried out efficiently. However you would do this at the cost of security!!
92
3
4 n
a <
93
be must therefore , even is ) 1 )( 1 ( 1 1 1 gcd Thus, 1 )) ( gcd( and key public the is message the ; ciphertext the is mod b q p )) )(q- (b, (p- n b, φ b x y n x y
b
− − = = ≡
is since 1 b n x n x n y n y Jacobi consider
b
⎟ ⎠ ⎞ ⎜ ⎝ ⎛ = ⎟ ⎠ ⎞ ⎜ ⎝ ⎛ = ⎟ ⎠ ⎞ ⎜ ⎝ ⎛ ± = ⎟ ⎠ ⎞ ⎜ ⎝ ⎛ thus, RSA encrypAon leaks the value of the Jacobi symbol ⎟ ⎠ ⎞ ⎜ ⎝ ⎛ n x
94
first half second half
95
x = 3mod13 HALF(1) = 0 2x ≡ 6mod13 HALF(2) = 0 4x ≡12mod13 HALF(4) =1 8x ≡11mod13 HALF(8) =1 16x ≡ 9mod13 HALF(16) =1 HALF(m) = if 0 ≤ mxmodn < n 2 1 if n 2 ≤ mxmodn < n −1 ⎧ ⎨ ⎪ ⎪ ⎩ ⎪ ⎪
Consider this function example [0-6.5) [6.5,13) [0,13) [0,3.25) [0,1.625) [1.625,3.25) 1 3
96
HALF(n,b, y) = if 0 ≤ x < n 2 1 if n 2 ≤ x < n −1 ⎧ ⎨ ⎪ ⎪ ⎩ ⎪ ⎪
n x y n x y n x y n x y n x y
b b b b b b b b b
mod ) 16 ( 16 mod ) 8 ( 8 mod ) 4 ( 4 mod ) 2 ( 2 mod ≡ ⋅ ≡ ⋅ ≡ ⋅ ≡ ⋅ ≡
2 , ) ( n x y HALF ∈ = = ▹
2 , 4 1 ) 2 ( n n x y HALF
b
∈ = = ▹
4 , ) 2 ( n x y HALF
b
∈ = = ▹
8 , ) 2 (
2
n x y HALF
b
∈ = = ▹
4 , 8 ) 2 (
2
n n x y HALF
b
∈ = = ▹
97
1 1 1 1 1 1 1
Thus, if we have an efficient function HALF, we can recover the plaintext message. hi n=1457, b=779, y=722
98
Bob sends his public key Alice encrypts with Bob’s public key Bob decrypts with his private key
99
Bob sends his public key Alice encrypts with Mallory’s public key Bob decrypts with his private key Man in the middle Intercepts messages Mallory sends her public key Mallory decrypts with her private key and re- encrypts with Bob’s public key
100
Bob sends his public key A l i c e e n c r y p t s w i t h B
’ s p u b l i c k e y Bob decrypts with his private key
101
Ron was Wrong, Whit is right, 2012
102
STINSON : chapter 6
103
G in elements all generates 1}
i : { en element th primitive a is If .
has it if a as termed is 1 = such that integer smallest the is
The G, Let .
group a be Let
i m
≤ ≤ = ∈ ⋅ α α α α α α α n element primitive m n ) (G, } 1 , 2 , 4 , 8 , 3 , 6 , 12 , 11 , 9 , 5 , 10 , 7 { 7 , 7 Let 12
group a forms ) , ( } 12 , , 3 , 2 , 1 {
* 13 * 13 *
13
= ∈ ⋅ = Z Z Z Consider
and generates all elements in Z. Thus, 7 is a primitive element
104
} 1 : { set the Define
with group the in element primitive a be ) , ( − ≤ ≤ = ∈ ⋅ n i n G Let group a be G Let
i
α α α
β β β α
α
logarithm discrete the as log Denote let ), 1 ( integer unique any For = = − ≤ ≤ a n a a
a
Given α and a, it is easy to compute β Given α and β it is computationally difficult to determine what a was
105
p
Z ∈ α
a mod
Private key : Public keys :
p , ,β α
a
Encryption
p x y p y where y y x e Z k ret random a choose
k k k p
mod , mod ) , ( ) ( ) (sec
2 1 2 1
β α ⋅ = = = ←
Decryption
ka ka ka k a k
− − −
1 1 1 1 2
106
2396 x (435765)-1 mod p
107
a mod
Given α and β it is computationally difficult to determine what a was
4 3 2
(until you reach β)
4 3 2
108
a mod
p m where Rewrite = + = r mq a as a
r q m r mq
−
We neither know q nor r, so we need to try out several values for q and r until we find a collision
109
5 4 3 2
6 1
−
4 4 6 3 3 6 2 2 6 1 1 6 6
− − − − −
collision Thus, m=6, q=4, r=1, a= mq+r = 25 List 1 List 2
110
Create List 1 Create List 2 Find collision
111
O(m) O(mlog m) O(m) O(mlog m) O(log m) O(mlogm) ~ O(m) = O(p1/2)
112
a mod
113
} 1 : { set the Define
with group the in element primitive a be ) , ( − ≤ ≤ = ∈ ⋅ n i n G Let group a be G Let
i
α α α
ab b a
Computational DH (CDH)
c b a
Decision DH (DDH)
114
Alice and Bob agree upon a prime p and a generator g. This is public information choose a secret a compute A = ga mod p choose a secret b compute B = gb mod p B A Compute K = Ba mod p Compute K = Ab mod p Ab mod p = (ga)b mod p = (gb)a mod p = Ba mod p