Skip to article frontmatterSkip to article content

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)


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ยถ


somes examplesยถ


multiplicative group Znโˆ—\mathbb{Z}_n^*ยถ

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ยถ


if n is not primeยถ

we can make the same construction, still with multiplication modulo nn ...
except that we consider only the set of all the integers from 1 to nโˆ’1n-1 that are coprime to nn

this is what is called the multiplicative group of integers modulo n - denoted Znโˆ—\mathbb{Z}_n^*
letโ€™s now see how to compute the order (the number of elements) of this group...


the order of Znโˆ—\mathbb{Z}_n^*ยถ

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 ฯ†(n)ฯ†(n),
which, as we have just seen, amounts to the number of integers up to n that are coprime to n

if n=p1e1.p2e2.....pkekn = p_1^{e_1} . p_2^{e_2} . ... . p_k^{e_k} where p1p_1, p2p_2, ..., pkp_k are distinct prime factors of nn, then:

ฯ†(n)=n(1โˆ’1p1)(1โˆ’1p2)...(1โˆ’1pk)ฯ†(n) = n (1 - \frac{1}{p_1}) (1 - \frac{1}{p_2}) ... (1 - \frac{1}{p_k})


the RSA constructionยถ

to build an RSA keypair:


Lagrangeโ€™s theoremยถ

in any finite group GG of order nn, the following statements hold:

  1. the set of all the powers of a given element g
    Cg={gkโˆฃkโˆˆZ} C_g = \{ g^k | k \in \mathbb{Z} \} is a subgroup of GG
    it is called the cyclic subgroup generated by g
    and by definition the order of gg is the order of that subgroup

  2. the order of any subgroup of GG divides the order of GG (Lagrangeโ€™s theorem)

Putting these two together, we can easily demonstrate that
โˆ€gโˆˆG,gn=1โˆ€gโˆˆG, g^n = 1
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

Now, this means that:


RSA in practiceยถ

key sizesยถ

message sizesยถ

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ยถ


๐Ÿ”‘ 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โ€