Asymmetric Encryption
Sam Goto
This is an open study of the RSA encryption scheme (source).
Problem
Bob
wants to send a secret message m
to Alice
without having to rely on a shared secret (symmetric keys)
Background
- In 1970, James Ellis, a british engineer and mathematician was working on an idea for "non-secret encryption": lock and unlock are inverse operations.
- Alice could buy a
lock
, open it, keep thekey
, and send thelock
to Bob. - Bob uses the
lock
to lock abox
with the hiddenmessage
inside and send thebox
back to Alice. - With its
key
, Alice could open thebox
to get themessage
. - Ellis never arrived at a mathematic formulation, but he had an intuitive sense of how it should work: an
encryption
key and adecryption
key. - The decryption key performs the inverse, or the undo operation, that was applied to the encryption key.
- Alice could buy a
- In 1973, Clifford Cocks (cryptographer) looked into a special kind of reversable function a trap door function.
- A known
public
functionf(x) = y
that was easy to compute but hard to invert: giveny
it is hard to computex
, unless you haveprivate
information available. - So, if you wanted to send you a message, you'd use
f(m) = y
and send you thepublic
resulty
, which nobody but you (who have a special piece ofprivate
information), would be able to compute the inverse off
to inverty
into them
. - For this he turned into modular exponentiation.
- He knew that computing discrete logarithms was hard for computers: given
a
ank
such thatb ^ k = a
, it is really hard to computeb
. - So, if Alice provided
e
andn
publicly (the lock), Bob could send a message to Alicem
by calculatingc = m ^ e % n
and sendc
to Alice (and no one would be able to invertc
intom
, because it would be involving computing a discrete logarithm)
This observation was also cleverly used on the diffie helmann key exchange protocol here.
- This would construct a confidential lock, but not a correct one: given
c
,e
andn
it is not possible to invert back tom
, including for Alice - So, he tried to build a trap door in the lock such that (a) only Alice could use it to invert
c
back intom
and (b)m
was inverted correctly. - Something that makes reversing this easier for Alice but hard for everybody else.
- So, he proposed: what if unlocking could be performed by taking
c
and raising it to some other private exponent,d
, to undo the operation and arrive atm
? - That is, can we construct
e
,d
andn
such that for everym
:- If
c = m ^ e % n
is the message Alice gets from Bob - And Alice computed
c ^ d % n
=(m ^ e % n) ^ d % n
=m ^ e ^ d % n
=m ^ (e * d) % n
=m
. - Then Bob's
m
= Alice'sm
andc
,e
andn
could be madepublic
.
- If
- So, he needed to find a way to make Alice construct a
e
andd
such that it lead (correctly) tom
while making it hard for anyone else to findd
(confidential). - So, he needed a second one way function for generating
d
, and for this he looked back at Euclid, Euler and Fermat's little theorem.
- A known
- In ~300BC, Euclid devised the fundamental theorem of arithmetic: every number can be represented uniquely as a product of prime numbers
- He also knew that computing the prime factorization was known to be a hard problem
- So, imagine Alice picked two big prime numbers, say,
p
andq
and multiplied then to constructn = p * q
. - That is, given
n
, it was known to be hard to computep
andq
, unless you have eitherp
orq
. - So, that was a trapdoor: something that is easy to do if you have Alice's
private
information (p
orq
) but hard otherwise. - So, Cocks started to look for function that depended on knowing the factorization of
n
. - For that he looked back to Euler.
- In the ~1700s, Euler was investigating the distribution of prime numbers.
- One important function he defined as the
phi(b)
function.
Not super important to understand it, but it measures the breakability of a number.
phi(n)
outputs how many integers less than or equal to n that do not share. A common factor with n. e.g.phi(8)
=4
(1, 3, 5 and 7)- What's interesting about
phi(n)
, thatphi(n)
is hard to calculate except in one case: prime numbers. - If
n
is prime,phi(n) = n - 1
:phi(n)
is easy to compute for primes. - It was also known that
phi(n)
was multiplicative: i.e.phi(a * b)
=phi(a) * phi(b)
- If
n
is the product of two primesa
andb
thenphi(n) = phi(a * b) = phi(a) * p(b) = (a - 1) * (b - 1)
is easy to compute - And that was the trapdoor that Cocks was looking for: if you know the prime factorization
a
andb
ofn
, thanphi(n)
is easy to compute as(a - 1) * (b - 1)
, but hard to compute otherwise - But, how could he connect this trapdoor to the previously one he devised? For that, he turned back into Fermat and Euler:
- One important function he defined as the
- Fermat-Euler's theorem states that for every
m
,m ^ phi(n) % n = 1
(ifm
andn
are coprime positive integers)- First, we can always exponentiate
1
byk
and we'll always getk
, i.e.1 ^ k = 1
:- Therefore
(m ^ phi(n)) ^ k % n = 1 ^ k
- i.e.
m ^ (k * phi(n)) % n = 1
- Therefore
- Second, we can always multiply both sides by
m
:1 * m = m
:- Therefore
m * (m ^ (k * phi(n)) % n) = m * 1
- Therefore
- i.e.
m ^ (k * phi(n) + 1) % n = m
- First, we can always exponentiate
- That allowed Cocks to put things back together
- From before:
- Bob can encrypt
m
withc = m ^ e % n
, givene
andn
from Alice, and know that it is going to be hard to invertm
. - Alice knows that
- She could take
c
and applyc ^ d % n
to arrive atm
which is the same asm
.- She knows that
m ^ (k * phi(n) + 1) % n = m
(Euler's theorem) - So, Alice can construct
e
,d
andn
such thatm ^ (e * d) % n
=m
- She knows that
- Bob can encrypt
- Alice can pick
e
,d
andn
such that(e * d) = k * phi(n) + 1
- i.e.
d = (k * phi(n) + 1) / e
- i.e.
- Corretness
- If Alice chooses
e
andn
such thatd
as above, that would makem ^ (e * d) % n = m
intom ^ (k * phi(n) + 1) % n = m
which we know to be true (Euler)
- If Alice chooses
- Confidentiality
- Alice would know that the value that she picked for
d
, givene
andn
, is only computable if you can solvephi(n)
, which is only known if you can factorizen
- That is known to be hard, unless you know what
p
andq
(which only Alice knows!)
- Alice would know that the value that she picked for
- From before:
Try it yourself!
Use the demo below to send messages from Bob to Alice!
- Bob wants to send a message
m
= to Alice - Alice, picks two large prime number,
p
= andq
= - Alice computes
n
=p * q
=p * q
=b
- Alices computes
phi(n)
, which is easy for her since knows the factorization ofn
but hard for everybody else - Alice computes
phi(n)
=phi(p * q)
=phi(p) * phi(q)
=(p - 1) * (q - 1)
=(p - 1) * (q - 1)
=phi
- Alice picks some small public exponent
e
= , an odd number that does not share a factor withphi(n)
- Alice computes
d
=(2 * phi(n) + 1 ) / e
=d
- Alice shares
n
= n ande
= e, her public key (the open lock) - Bob computes
c
=m ^ e % n
=m ^ e % n
= c and - Bob sends
c
= c to Alice - Alice computes
r
=c ^ d % n
=c ^ d % n
=r
- Alice knows that
r
=r
=m
=m