RSA cryptographyยถ
this paradigm of public / private keys is at the heart of modern cryptography
and is used all over the place, from SSH to TLS, and many more
in this notebook, we will focus on the RSA algorithm, knowing that it is just one of many
that is widely used for secure data transmission
(RSA name comes from the initials of its inventors:
Rivest, Shamir, and Adleman, who introduced it in 1977)
Weโve used RSA already
in the installation notebook, we have already used RSA keys to set up SSH access to github
the invariant in all these algorithms is that they rely on the mathematical properties of finite groups, so what weโre going to learn about RSA has some features in common with other public-key algorithms, such as e.g. elliptic curves cryptography (ECC)
finite groupsยถ
a finite group is a set of elements with a binary operation that satisfies four properties:
closure: the result of the operation on two elements in the set is also in the set
associativity: the operation is associative (a+b)+c = a+(b+c)
identity element: there exists an element in the set that acts as an identity for the operation
invertibility : for every element in the set, there exists an inverse element such that the operation on them yields the identity element
somes examplesยถ
consider the set of integers modulo 5, denoted as
the elements are {0, 1, 2, 3, 4}
the operation is addition modulo 5
this set satisfies the properties of a finite group
as we will see below, the same holds for multiplication modulo (denoted )
except we need to restrict ourselves to the integers that are coprime to
the set of permutations on a finite set of cardinal
together with the composition of functions as the operator
is also a finite group, denoted as ,
which is not commutative (i.e. in general)
multiplicative group ยถ
now we are looking at the multiplicative group of integers modulo n
two cases arise, letโs start with the simplest case
if n is primeยถ
we can take all the integers , with multiplication modulo n
in that case indeed, all these numbers are coprime to n
and so after Bรฉzout, , such that
in other words,
which establishes invertibility: a is the inverse of i
the other 2 properties are easy to verify
if n is not primeยถ
we can make the same construction, still with multiplication modulo ...
except that we consider only the set of all the integers from 1 to that are coprime to
the same Bรฉzout argument proves that multiplication is invertible in this structure
the other 2 properties are easy to verify
what is a little less trivial is that the operation is stable in this set, but:
assume and are coprime to
then their product is also coprime to
and so this subset is stable under multiplication modulo
this is what is called the multiplicative group of integers modulo n - denoted
letโs now see how to compute the order (the number of elements) of this group...
the order of ยถ
in the general caseยถ
the number of elements in this group (also called the order of this group)
is given by Eulerโs totient function ,
which, as we have just seen, amounts to the number of integers up to n that are coprime to n
if where , , ..., are distinct prime factors of , then:
also valid for primes
and so, if is prime, then , as we knew already
the RSA constructionยถ
to build an RSA keypair:
we pick too large primes and , and state
we do all the computations within , the multiplicative group of integers modulo
which, as we just saw, has order
Lagrangeโs theoremยถ
in any finite group of order , the following statements hold:
the set of all the powers of a given element g
is a subgroup of
it is called the cyclic subgroup generated by g
and by definition the order of is the order of that subgroupthe order of any subgroup of divides the order of (Lagrangeโs theorem)
Putting these two together, we can easily demonstrate that
where 1 is the identity element of the group (the usual notation for a multiplicative group)
RSA (finally!)ยถ
So letโs go back to our RSA construction
we pick 2 large primes and , and state
we consider the multiplicative group of integers modulo
it has order
and because it is a group: any element in , letโs call it for public, has an inverse - that we call for private
because they are inverse of one another, we have
which means that
Now, this means that:
is known to all parties: the public key is and the private key is
any message (provided it is in the group) can be encrypted with the public key,
and decrypted with the private key - or the other way around - by a simple exponentiation (mod )encrypted:
decrypted:
RSA in practiceยถ
key sizesยถ
for the whole thing to be of any practical use, it must be hard to find the private key v from the public key (n, u)
we will admit that it is the case, and that itโs hard to factor n into its prime factors p and qas an indication, a key size of 2048 bits means n has 2048 bits, so it is in the order of about 617 decimal digits
this size is considered secure for the next 10 years or so - except if quantum computers was to become a realityas a side note, of course all this crucially calls for fast exponentiation
message sizesยถ
also a concrete message may of course be larger than the modulus n
so its first needs to be encoded as a collection of integers, each smaller than n
all in all this encoding scheme is non trivial and rather compute-intensive
so itโs generally used once at the beginning of a session (SSH, TLS, etc..),
and used to agree on lightweight symmetric keys for the rest of the session
the careful reader will notice a hole in the construction: what if the message is a multiple of p or q ?
what is the likelyhood of that ?
other schemesยถ
the interested reader can check a construction called Ellipctic Curve Cryptography (ECC) that allows to build a similar public/private key pair;
you can easily create such a key using ssh-keygen with the -t ecdsa option
SSHยถ
the basics:
ssh (secure shell) is a protocol for secure remote login over an insecure network
it uses public key cryptography
its primary use is to let you connect to a remote server once it has been provisioned in the cloud
other uses:
you have used it many times when interacting with github
it can also be used to create tunnels (like a network pipe) to cross firewalls
you can use it with vs-code to edit files on a remote server
configuration:
~/.ssh/id_rsa: the private key (do not show it to anyone)~/.ssh/id_rsa.pub: the public key (this is the one you share)~/.ssh/config: allows to define shortcuts and predefined options per host~/.ssh/known_hosts: a list of known hosts and their public keys
๐ Public Key Infrastructure (PKI)ยถ
each browser and OS comes with a list of trusted CAs
CA = Certificate Authority
in practical terms, their public key
e.g. Verisign, Letโs Encrypt, etc..
a certificate is a chain of trust
signed by a CA
which in turn is signed by another CA
and so on until we reach a self-signed root CA
which should be trusted by the browser
like always, signing is based on a public/private key pair
the CA signs the certificate with its private key
and the browser verifies it with the CAโs public key
try it: in chrome, you can inspect the certificate chain by clicking on the lock icon in the address bar; then choose โConnection is secureโ and โCertificate is validโ