RSA and Public Key Cryptography
CR
Cryptography
Chester Rebeiro IIT Madras
STINSON : chapter 5, 6
RSA and Public Key Cryptography Cryptography Chester Rebeiro IIT - - PowerPoint PPT Presentation
RSA and Public Key Cryptography Cryptography Chester Rebeiro IIT Madras CR STINSON : chapter 5, 6 Ciphers Symmetric Algorithms Encryption and Decryption use the same key i.e. K E = K D Examples: Block Ciphers : DES, AES,
STINSON : chapter 5, 6
2
Alice Bob Plaintext untrusted communication link E D KE KD “Attack at Dawn!!” encryption decryption #%AR3Xf34^$ (ciphertext)
Plaintext “Attack at Dawn!!”
The Key K is a secret
3
Encryption Key KE not same as decryption 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 functions
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
– Factorization of two primes
– It is easy to compute N – However given N it is difficult to factorize into P and Q
– However given N it is difficult to factorize into P and Q
– Discrete Log Problem
7
– Only Alice, who owns the private key could have signed
8
Alice and Bob agree upon a prime p and a generator g. This is public information
Diffie-Hellman Key Exchange
9
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
10
11
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
n b a n b n b b q p n q p n q p q p φ φ φ φ
−
= = < < − − = × = ≠
12
) , , ( ' ) , ( ' )) ( mod( Compute . 4
1
a q p is key private s Bob b n is key public s Bob n b a φ
−
=
Given the private key it is easy the public key Given the public key it is difficult to derive the private key
Encryption Decryption
13 n b K
Z x where n x y x e ∈ = = mod ) (
a K
14
395413 13 1
Encryption
n b K
Z x where n x y x e ∈ = = mod ) (
Decryption
a K
n
15
n t n t ab a b a
+
) ( 1 ) ( φ φ
From Fermat’s theorem
n
ab
16
ab ab
ab
p t p t q q p t n t ab
+ +
) ( ) ( ) ( 1 ) ( ) ( 1 ) ( ϕ ϕ φ φ φ φ
c mod
17
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 arithmetic on 8/16/32/64 bit numbers
18
base : 2b, where b = 64/32/16/8 bits 1024 bits
= (2, 76, 176, 22, 234)256 = (80, 239, 242, 132)256 i ai bi cin ai+bi+cin(mod 256) Carry? cout
19
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)?
“Computational Number Theory”, Abhijit Das, CRC Press
= (2, 76, 176, 22, 234)256 = (80, 239, 242, 132)256 i ai bi
Cin Borrow?
Cout ai-bi-cin(mod 256)
20
i ai bi Cout ai-bi-cin(mod 256) 234 132 (234 < 132)? 102 1 22 242 (22 < 242)? 1 36 2 176 239 1 (176 < 239)? 1 192 3 76 80 1 (76 < 80)? 1 251 4 2 1 (2 < 0)? 1
= (18, 214, 135)256 = (4, 143, 244, 234)256
21
m m l m h l m h
b B b b a B a a n m Let n b a + + + = × + = + = = − 2 / . words ary B with integers sion multipreci two be , Let
2
22
( )
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
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 + − − = − − + − − + + + = + + + = × ) )( ( using ) )( ( ) ( ) ( ) (
2 2
Karatsuba multiplication converts n bit multiplications into 3 multiplications of n/2 bits The penalty is an increased number of additions
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)
23
ah = (7, 91); al = (205, 21) a = (7, 91)2562 + (205, 21) bh = (58, 222); bl = (104, 177) b = (58, 222)2562 + (104, 177)
ab
24
25
26
primes
27
28
https://en.wikipedia.org/wiki/Largest_known_prime_number
– For example, if n is approx 21024, then need to check around 2507 numbers
– Randomized algorithms
29
30
n
−
If n is prime, then is true for any ‘a’ If n is composite
1 ≡ −
1 ≡ −
31
1
n
−
If n is composite is false but may be true for some values of a. For example: n = 221 and a = 38 38220 mod 221 ≡ 1. We need to increase our confidence with more values of a
1 ≡ −
32
Some composites act as primes. Irrespective of the ‘a’ chosen, the test passes. for example Carmichael numbers are composite numbers which
n an mod 1
1 ≡ −
33
for example Carmichael numbers are composite numbers which satisfy Fermat’s little theorem irrespective of the value of a.
n a mod 1
2 ≡
34
n a
n a either n a a n a mod ) 1 ( mod ) 1 ( mod ) 1 )( 1 ( mod 1
2
≡ − ≡ + ≡ − + ≡ −
35
r
d d
2
r
d d
2
36
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
37
d d d d d
s
2 2 2 2
3 2 1
1 (Fermat ‘s)
' prime is ' , 1 mod Compute . 3 nonzero a random at Select . 2 2 1 that such integer
an Find . 1 n return b If n a b T Z a T d n d T
d n s
± = = ∈ = − Input n
38
' composite is ' Otherwise . 5 ' prime is ' , 1 mod b c calculate , 1 , , 1 For . 4 ' prime is ' , 1
i
2
n return T n return c If n r i T n return b If − = ≡ − = ± = L
39
If an element is not a quadratic resiidue, then it is a quadratic non-residue quadratic non-residues in Z13 are {2, 5, 6, 7, 8, 11}
40
p
2 1 −
A result from Euler
41
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 a a p
p
mod | when
2 1
≡
−
p a squaring prime
an is p if even is p note p a consider p x a t s exists Z x such no QNR a is a
p p p
1 mod : ) , 1 ( mod : mod . . , when
2 1 2 1 2
≡ − ≡ ∈
− − −
42
p a Thus p a p a Thus p a so
p p p p
mod 1 QR a not is a since , mod 1 mod 1 , mod 1 ,
2 1 2 1 2 1 2 2 1
− ≡ ≠ ± ≡ ≡
− − − −
p
2 1 −
6 2 1 13
−
Congurence always hold n is an odd prime
43
6
7 2 1 15
−
7 2 1 15
−
Euler’s Witness Euler’s Liar s holds when prime Congurence may
when n is an odd prime
) ( compute 1 1 that such integer random a choose ){ ( COMPOSITE return x if n a x n- a a n ASSEN SOLOVAYSTR = = ≤ ≤
How to compute
44
} ) mod ( mod ) (
2 1
COMPOSITE return else PRIME possibly return n y x if n a y compute COMPOSITE return x if
n
≡ = =
−
error probability is at most ½ after k invocations of this algorithm, Legendre’s symbol
ion factorizat prime with integer positive
an is Suppose n
45
... p p p p n ion factorizat prime with integer positive
an is Suppose
4 3 2 1
e 4 e 3 e 2 e 1
× × × = n L × × × × =
4 3 2 1
4 3 2 1 e e e e
p a p a p a p a n a
Then,
T
= ± ≡ − ± ≡ = = ≡ b a ab n if n if n n b n a then n b a If . 3 P 8 mod 3 1 8 mod 1 1 2 . 2 P mod . 1 P
46
≡ ≡ − = = = =
a n a n if a n n a
is a if n t n n a t a even is a if n n n
k k
4 mod 3 , . 5 P 2 , 2 , . 4 P . 3 P
From the theorem P5, P1, then P2 P5, P1, P5, P1, P3, P2
47
P5, P1, P5, P1, P3, P2 P5, P1 and 1 is a QR mod 13
48
49
Fundamental theorem of arithmetic Any integer number (greater than 1) is either prime or a product of prime powers
k
e k e e e
3 2 1
3 2 1
50
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 π(2n/2)
q p n × =
. 1 gcd as case the not likely most is this However, factor. prime a is then , 1 ) , gcd( If ). 1 ( integer random a choose = ≠ < < (a,n) a n a n a a
1
). 1 ( compute to use We . 1 such that L an get we Suppose −
L
a L L |
magically
2
How to find the magic L?
4
51
1 | , ) ' ( mod 1 ) 1 ( | 1 ). 1 ( compute to use We
) 1 (
− ≡ ≡ = − = − −
− L k p L L
a p Thus Theorem Little s Fermat by p a a L k p L p a L >
anything. conclude Cannot also. 1 | then n, n) 1,
if n.
factor a found have we then , ) , 1 gcd( if Thus . be also may and either is ) , 1 gcd( , 1 | and | , Since ) , 1 gcd( compute Now
L
− = ≠ − − − −
L L L L L
a q n n a n p n a a p n p n a
3
No easy way, trial and error!! Factorials have a lot of divisors. So that is a nice way. So, take L as a factorial of some number r.
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 a S n d if , n)
d . S (a, n) > . S a . S
r!
= ← ←
Pollard p-1 factorization for n.
52
done! are we ;
factor prime the is 3 repeat and increment , 1
next value with 1 from again start , n else d S r d if else a S n d if = =
r = 2,3, 4, H.. Will the algorithm terminate?
4 3 2 1
1 1
53
4 3 2 1
4 4 3 3 2 2
where
j i
j i j i
4 3 2 1
54
j i −
n n
Example :
2 +
55
Example :
2 +
− )
1 i i i n
DrawbackH Large number of GCD
This column is just for understanding. In reality we will not know this
56
41 ) 82123 , 63222 gcd( ) , gcd(
10 3
= = − N x x
A factor of N
Large number of GCD
55. Can we reduce the number
Given xi mod N, we compute gcds of every pair until we find a gcd greater than 1
40 2 5 26 21 32 1
57
l t t
+
(and a shape like the Greek letter rho)
16 11 40
+
l j j
∈ =
n
Z y x random a choose
5 26 21 32
58
)) ( ( ) (
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 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
i i
xi and yi meet at the same point in the cycle Therefore, yi must have traversed (some) cycles more
59
i i i i
2
60
N y x mod
2 2 ≡
) )( ( | ) ( |
2 2
y x y x N y x N + − = − >
61
2 2
2 2
62
7 ) 7 , 91 gcd( 13 ) 13 , 91 gcd( = =
SoH we can use x and y to factorize N.
N y x mod
2 2 ≡
But how do we find such pairs?
32 and 200 are not perfect squares. However (32x200 = 6400) = 802 is a perfect square
2 2
63
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/)
Recall, Fundamental theorem of arithmetic Any integer number (greater than 1) is either prime or a product of prime powers
k
e k e e e
3 2 1
3 2 1
Thus, a number is a perfect square if it prime factors have even powers.
64
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
N r y mod
2
=
– Compute – Test if y factors completely in the set B. – If NO, then discard. ELSE save (y, r) (these are called B-smooth numbers)
65
N r y mod =
66
All numbers are B-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
67
2 3 5 7 11 13
1 1 1 20 2 1 63 2 1
1 1
1 1 1 80 4 1
68
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
≡ × × × × × − ≡ × × ×
1829 mod 901 1459 1829 mod ) 13 7 5 3 2 1 ( ) 85 61 43 42 (
2 2 2 2
≡ × × × × × − ≡ × × × 59 ) 2360 , 1829 gcd( 2360 | 1829 ) 901 1459 )( 901 1459 ( | 1829 = = − + >
69
31 59 1829 31 ) 558 , 1829 gcd( 558 | 1829 59 ) 2360 , 1829 gcd( 2360 | 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 factorization – Current challenges 896 bits (reward $75,000), 1024 bit ($100,000)
70
https://en.wikipedia.org/wiki/RSA_Factoring_Challenge
71
) 1 )( 1 ( ) ( / + + − = − − = = = q p n p n q pq n φ
72
) 1 ) ( ( 1 ) ( ) ( 1 ) (
2
= + + − − + + − = + + − = n p n n p p n p n n q p pq φ φ
Solve to get p (a factor of n)
There are two trivial and two non-trivial solutions for The trivial solutions are +1 and -1
n y mod 1
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 mod 1
73
≡ q y 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
q y p y mod 1 mod 1 − ≡ + ≡ q y p y mod 1 mod 1 + ≡ − ≡
n y mod 1
2 ≡
74
311 91 403 92 403 mod ) 12 13 8 31 ( 403 mod ) 31 mod 13 13 13 mod 31 31 (
1 1
= − ≡ ⋅ − ⋅ ⋅ − ⋅
− −
403 mod 1 311 92 :
2 2
≡ ≡ Note
The non-trivial solutions are 92 and 311 What happens when we solve
q y p y mod 1 mod 1 + ≡ + ≡
75
1 ≡ −
ab
2 1 −
2 2
However we need to have a non- trivial result
mod put . 4 message any choose . 3 2 1 Represent . 2 1 compute given . 1 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
76
" " 4 step ; 2 / ) even is ( . 7 ; " d is n
factor a " , 1 . 6 ) , 1 gcd( compute . 5 mod put . 4 failure return else goto t t t if exit return d if n y d n x y
t
= ≠ − ← =
) 1 )( 1 ( | − + y y n
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
311 403 mod 2 403 mod 270 540 : 2 1 403 mod 2 403 mod 540 2 1080 : 1 2 1080 1
270 540
x y t loop x y t loop x ab t
t t
≡ = ≡ = = ≡ = ≡ = = = = − =
77
) ( 31 ) 403 , 310 gcd( 311 403 mod 2 403 mod 270 2 : 2 n
factor a x y t loop = ≡ = ≡ = = 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
78
Alice m3mod N1 m m3mod N2 m3mod N2 c1 c2 c3
79
Insecure channel
Alice m3mod N1 m m3mod N2 m3mod N2 c1 c2 c3
3 3 3 2 3 2 1 3 1
mod mod mod N m c N m c N m c ≡ ≡ ≡
80
Insecure channel
) 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
– i.e. The message can be decrypted
81
It is tempting to have small private and public keys, so that encryption or decryption may be carried out efficiently. However you would do this at the cost of security!!
3
4 n
a <
82
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
− − = = ≡
83
is since 1 b n x n x n y n y Jacobi consider
b
= = ± = thus, RSA encryption leaks the value of the Jacobi symbol n x
first half second half
84
) ( 13 mod 3 = = x HALF x − < ≤ < ≤ = 1 2 1 2 ) ( n x n if n x if x HALF
Consider this function example [0-6.5) [6.5,13) [0,13) [0,3.25) [0,1.625) 1
85
1 ) 16 ( 13 mod 9 16 1 ) 8 ( 13 mod 11 8 1 ) 4 ( 13 mod 12 4 ) 2 ( 13 mod 6 2 ) ( 13 mod 3 = ≡ = ≡ = ≡ = ≡ = = x HALF x x HALF x x HALF x x HALF x x HALF x
[0,1.625) [1.625,3.25) 1 3
− < ≤ < ≤ = 1 2 1 2 ) , , ( n x n if n x if y b n HALF
n x y n x y n x y
b b b b b
mod ) 4 ( 4 mod ) 2 ( 2 mod ≡ ⋅ ≡ ⋅ ≡
) ( n x y HALF ∈ = =
86
n x y n x y n x y
b b b b b b
mod ) 16 ( 16 mod ) 8 ( 8 mod ) 4 ( 4 ≡ ⋅ ≡ ⋅ ≡ ⋅
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
∈ = = >
1 1 1 1
hi n=1457, b=779, y=722
87
1 1 1 1
Thus, if we have an efficient function HALF, we can recover the plaintext message.
88
Bob decrypts with his private key
Man in the middle Intercepts messages
89
Bob decrypts with his private key Mallory decrypts with her private key and re- encrypts with Bob’s public key
90
Bob decrypts with his private key
91
Ron was Wrong, Whit is right, 2012
92
STINSON : chapter 6
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
m
∈ ⋅ α α α α α n element primitive m n ) (G,
93
G in elements all generates 1}
i : {
i
≤ ≤ = α α } 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 L
<7> has order 12 and generates all elements in Z. Thus, 7 is a primitive element
} 1 : { set the Define
with group the in element primitive a be ) , ( − ≤ ≤ = ∈ ⋅ n i n G Let group a be G Let
i
α α α
94
β β β α
α
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
p
Z ∈ α
p
a mod
α β ≡
Private key : Public keys :
p , ,β α a
95
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
96
a mod
Given α and β it is computationally difficult to determine what a was
...... , , ,
4 3 2
α α α α
(until you reach β)
97
...... , , , α α α α
(until you reach β)
...... , , ,
4 3 2
α α α α
a mod
p m where Rewrite = + = r mq a as a
98
p p
r q m r mq
mod mod α α β α α β ≡ ≡
−
We neither know q nor r, so we need to try out several values for q and r until we find a collision
6 1
−
6
−
collision
99
5 4 3 2
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
Create List 1
100
Create List 1 Create List 2 Find collision
O(m) O(mlog m)
101
O(m) O(mlog m) O(log m) O(mlogm) ~ O(m) = O(p1/2)
a mod
102
} 1 : { set the Define
with group the in element primitive a be ) , ( − ≤ ≤ = ∈ ⋅ n i n G Let group a be G Let
i
α α α
103
ab b a
Computational DH (CDH)
c b a
Decision DH (DDH)
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
104
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