MAT 3343, APPLIED ALGEBRA, FALL 2003

Handout 3: The RSA Cryptosystem

Peter Selinger

1 Background

1.1 The group of units

Let (R, +, ·) be a ring. Then R forms an abelian group under addition. R does not in general form a group under multiplication, because not every element has a multiplicative inverse. However, we obtain a multiplicative group if we restrict attention to the invertible elements. For a ring R, we define R^{∗} to be the set of invertible elements, i.e.,

R^{∗} = {a ∈ R | ∃a′ ∈ R . aa′ = 1 = a′a}.

An invertible element in a ring is also called a unit of the ring; thus, R^{∗} is called the group of units of R.

Lemma 1.1. R^{∗} is a group under multiplication.

Proof. First, we need to show that multiplication is well-defined on R^{∗}. Let a, b ∈ R^{∗}. Then ab ∈ R. We must show that ab is invertible. Since a, b ∈ R^{∗}, there exist a′, b′ ∈ R such that aa′ = 1 = a′a and bb′ = 1 = b′b. Then abb′a′ = a1a′ = aa′ = 1 and b′a′ab = b′1b = b′b = 1, hence ab is invertible, thus ab ∈ R^{∗}.

Next, we need to show that R^{∗} satisfies the group axioms. The fact that multiplication is associative with unit 1 follows from the fact that this is true in R (note that 1 ∈ R^{∗}). Finally, if a ∈ R^{∗}, then aa′ = 1 = a′a for some a′ ∈ R; but this implies that a′ ∈ R^{∗}, thus every a ∈ R^{∗} has an inverse in R^{∗}. □

Examples. The only invertible element in ℤ are 1 and −1; thus ℤ^{∗} = {−1, 1}. In ℚ, all elements except 0 are invertible, thus ℚ^{∗} = ℚ − {0}. Similarly, ℝ^{∗} = ℝ − {0} and ℂ^{∗} = ℂ − {0}. In ℤ_{5}, there are four invertible elements, namely ℤ_{5}^{∗} = {¯1, ¯2, ¯3, ¯4}. In ℤ_{6}, only ¯1 and ¯5 are invertible, so ℤ_{6}^{∗} = {¯1, ¯5}.

More generally, recall that ā is invertible in ℤ_{n} if and only if a and n are relatively prime (Theorem 5, p.54). Thus, the elements of ℤ_{n}^{∗} are in one-to-one correspondence with the numbers in {1, …, n − 1} which are relatively prime to n.

Definition (Euler’s φ function). Euler’s φ function is defined as follows: φ(n) is the number of invertible elements in ℤ_{n}, i.e., φ(n) = |ℤ_{n}^{∗}|. Equivalently, φ(n) is the number of integers in {1, …, n − 1} which are relatively prime to n.

Remark. Let n > 0. Then n is prime if and only if φ(n) = n − 1. Because: if n is prime, then all numbers in the set {1, …, n − 1} are relatively prime to n, thus φ(n) = n − 1. On the other hand, if n is not prime, then a|n for some 1 < a < n. Thus, a ∈ {1, …, n − 1} is not relatively prime to n, and φ(n) < n − 1.

Lemma 1.2. (1) If p is prime, then φ(p) = p − 1.

(2) If p ≠ q are two primes, then φ(pq) = (p − 1)(q − 1).

Proof. (1) See the previous remark. (2) Let n = pq, and let us count the numbers in {1, …, n − 1} which are not relatively prime to n. These numbers are: the multiples of p (there are q − 1 of them), and the multiples of q (there are p − 1 of them). Thus, the total number of integers in {1, …, n−1} which are not relatively prime to n is p + q − 2. The remaining numbers are relatively prime; there are n − 1 − (p + q − 2) = pq − p − q + 1 = (p − 1)(q − 1) such numbers. □

Next, we recall some important facts from group theory.

Lemma 1.3. Let G be a finite group. Then

(1) If H is a subgroup of G, then |H| divides |G|.

(2) If x ∈ G is any element, then H = {x^{i} | i ∈ ℤ} is a subgroup of G.

(3) If |G| = n and x ∈ G, then x^{n} = 1.

Proof. From group theory. (1) is proved by showing that the cosets aH form a partition of G. (2) It is clear that H is closed under multiplication and inverses.

(3) Let H be as in (2). Since G is finite, H must be finite, so x^{i} = x^{j} for some i < j. Dividing by i, we find that 1 = x^{j−i}. Let k be smallest such that x^{k} = 1. Then H = {1, …, x^{k−1}} has exactly k elements. By (1), k|n. It follows that x^{n} = 1. □

Lemma 1.4 (Fermat’s Little Theorem). If p, x ∈ ℤ, p is prime, and p|̸x, then

x^{p−1} ≡ 1(mod p).

Proof. By Lemma 1.2(1), |ℤ_{p}^{∗}| = p − 1. By Lemma 1.3(3), x^{p−1} = 1 in ℤ_{p}. □

Lemma 1.5 (Fermat’s Little Theorem generalized). If n, x ∈ ℤ and n, x are relatively prime, then

x^{φ(n)} ≡ 1(mod n).

Proof. This follows directly from Lemma 1.3(3), applied to ℤ_{n}^{∗}. □

1.2 The Chinese Remainder Theorem

Theorem 1.6 (Chinese Remainder Theorem). Let n_{1}, n_{2} be integers such that gcd(n_{1}, n_{2}) = 1, and let n = n_{1}n_{2}. Given integers a_{1}, a_{2}, there exists a unique a ∈ ℤ_{n} such that a ≡ a_{1}(mod n_{1}) and a ≡ a_{2}(mod n_{2}).

Proof. Consider the function f : ℤ_{n} → ℤ_{n_{1}} × ℤ_{n_{2}} defined by

f([a]_{n}) = ⟨[a]_{n_{1}}, [a]_{n_{2}}⟩.

Since n_{1}, n_{2}|n, this is a well-defined function. We show that f is one-to-one: suppose f([a]_{n}) = f([b]_{n}). Then [a]_{n_{i}} = [b]_{n_{i}}, thus n_{i}|(a − b) for i = 1, 2. Since n_{1}, n_{2} are relatively prime, it follows that n_{1}n_{2}|(a − b) by Theorem 5(1), p.41. Thus a ≡ b(mod n), hence [a]_{n} = [b]_{n}. Therefore, f is one-to-one. But f is a function between finite sets of equal cardinality, and therefore if it is one-to-one, it is also onto. It follows that for any ⟨a_{1}, a_{2}⟩ ∈ ℤ_{n_{1}} × ℤ_{n_{2}}, there exists a unique a ∈ ℤ_{n} with f(a) = ⟨a_{1}, a_{2}⟩. This is equivalent to the claim. □

Exercise 1.1. As a matter of fact, there is a more general version of the Chinese Remainder Theorem, using integers n_{1}, …, n_{k}, pairwise relatively prime, and a_{1}, …, a_{k}. State and prove this more general version.

Corollary 1.7. Let p ≠ q be two different primes, and let x ∈ ℤ. If x ≡ 1(mod p) and x ≡ 1(mod q), then x ≡ 1(mod pq).

Proof. By the Chinese Remainder Theorem, there exists a unique number x in ℤ_{pq} such that x ≡ 1(mod p) and x ≡ 1(mod q). Since 1 is such a number, it follows that x = 1 in ℤ_{pq}. □

1.3 Square roots of unity

A square root of unity in a ring R is an element b ∈ R such that b^{2} = 1.

Lemma 1.8. (1) If p is an odd prime, then there are exactly two roots of unity in ℤ_{p}, namely b = ±1.

(2) If p, q are two different odd primes, then there are exactly four roots of unity in ℤ_{pq}, namely b = ±1, and b = ±x, where x is the unique element such that x ≡ 1(mod p) and x ≡ −1(mod q).

Proof. (1) Suppose that b is a square root of unity in ℤ_{p}. Then b^{2} ≡ 1(mod p), or equivalently, b^{2} − 1 ≡ 0(mod p). But b^{2} − 1 = (b − 1)(b + 1), thus it follows that p|(b − 1)(b + 1). By Theorem 6(1), p.41, we have p|b − 1 or p|b + 1, thus b ≡ ±1(mod p).

(2) Let b ∈ ℤ. Then b^{2} ≡ 1(mod pq) iff b^{2} ≡ 1(mod p) and b^{2} ≡ 1(mod q) by Corollary 1.7. Thus, b is a square root of unity in ℤ_{pq} iff it is a square root of unity in ℤ_{p} and in ℤ_{q}. Using (1) and the Chinese Remainder Theorem, this leaves the following four possibilities:

b ≡ +1(mod p) and b ≡ +1(mod q), or
b ≡ +1(mod p) and b ≡ −1(mod q), or
b ≡ −1(mod p) and b ≡ +1(mod q), or
b ≡ −1(mod p) and b ≡ −1(mod q), or
□

2 Public key cryptography

The idea of public key cryptography was first (publicly) introduced in 1976 by Diffie and Hellman. The idea is to work with two separate keys e and d. The key e is used for encryption, and the key d is used for decryption. A person who wishes to receive encrypted messages generates a pair of keys (e, d), publishes e (the “public key”) and keeps d secret (the “private key”). Therefore, everybody who wishes to do so may use e to encrypt a message, but only the authorized recipient who has the key d is able to decrypt the message.

For this scheme to be secure, it needs to be designed in such a way that the secret key d cannot easily be computed from the public key e. The encryption function must be a “one-way function”, which is a function that is easily computable, and also invertible, but whose inverse is not easy to compute. It is not known whether such functions exist; however, there are some functions which are believed to have this property.

Several mathematical schemes have been proposed for implementing public key cryptography. The best-known (and most widely used) is the RSA cryptosystem, developed by Rivest, Shamir, and Adleman in 1978. Its security rests on the fact that it is difficult to factor large numbers into primes. Another system, the ElGamal cipher, was proposed in 1985 by ElGamal. Its security rests on the difficulty of computing discrete logarithms. This means, given numbers x, y and a prime p, it is difficult to compute an exponent e such that x^{e} ≡ y(mod p). A third system that has been proposed early in 1978, the so-called Knapsack cipher by Merkle and Hellman, was later found to be insecure (“cracked”).

There are a number of other systems that have been proposed over the years. McEliece proposed a cipher in 1978 based on algebraic coding theory. More recently, a class of ciphers has been proposed which is based on hard problems in the theory of elliptic curves.

3 The RSA cryptosystem

3.1 Description of the cipher

To generate a key pair, do the following: let p and q be two different, large primes chosen at random (typically p and q will have 500 binary digits each). Let N = pq. Recall that φ(N) = (p − 1)(q − 1) from Lemma 1.2. Let e be a randomly chosen number with 1 < e < N, and compute d such that ed ≡ 1(mod φ(N)) (this amounts to finding the inverse of e in ℤ_{φ(N)}, which can be done efficiently by Euclid’s algorithm. In the unlikely event that e has no inverse, just pick a different e at random).

The public key is the pair (N, e).

The private key is the pair (N, d).

The number N is called the modulus, and it is public. The primes p and q must be kept secret, or else it is possible to compute φ(N), and thus d, from (N, e).

A message is an element M ∈ ℤ_{N}. To encrypt M, compute

C ≡ M^{e}(mod N).

The encrypted message C is also called the ciphertext, and the original message M is called the plaintext. Note that, to perform the encryption, one only needs to know (N, e), and of course, the message M.

To decrypt a ciphertext C, compute

M′ ≡ C^{d}(mod N).

3.2 Correctness

Theorem 3.1. The decrypted message M′ is the same as the original message M ∈ ℤ_{N}.

Proof. We have

M′ ≡ C^{d} ≡ M^{ed}(mod N).

Thus, we need to show M^{ed} ≡ M(mod N). We first prove that M^{ed} ≡ M(mod p). First, in case p|M, this is trivial. In case p|̸M, we have M^{p−1} ≡ 1(mod p) by Fermat’s Little Theorem. But (p − 1)|φ(N)|(ed − 1), so M^{ed−1} ≡ 1(mod p), hence M^{ed} ≡ M(mod p) as desired. By a similar argument, M^{ed} ≡ M(mod q). Finally, by Corollary 1.7, M^{ed} ≡ M(mod pq). □

3.3 Feasibility

The operation of the RSA cipher rests on the fact that the following problems are computationally easy to solve:

1. exponentiation modulo N, i.e., calculating x^{e} in ℤ_{N}, for large N, x, and e,

2. finding large prime numbers.

The second problem will be the subject of the next lecture. For the first problem, note that the naive algorithm for calculating x^{e}, namely multiplying x by itself e times, is not feasible when e is very large. Instead, we use the following method of repeated squaring: Suppose e = 2^{a_{0}} + 2^{a_{1}} + …, 2^{a_{m}}, where a_{0} < a_{1} < … < a_{m}. Such a representation of e can always be found by looking at the binary expansion of e. Then

x^{e} = x^{2^{a_{0}}} · x^{2^{a_{1}}} ⋯ x^{2^{am}} .

The individual factors x^{2^{a_{i}}} can be found by repeated squaring, i.e., by computing the sequence x, x^{2}, x^{4}, x^{16} and so forth in ℤ_{N}, each member being the square of the previous one. What is the computational cost of computing x^{e} in this way? Assuming that N ≈ 2^{1000}, then we have m ⩽ a_{m} ⩽ 1000, thus the calculation of x^{e} involves computing at most 1000 squares and at most 1000 multiplications modulo N, or a total of 2000 multiplications. On a modern computer, this can be computed very fast.

3.4 Security

The security of the RSA cryptosystem has not been proven for sure; it rests on the assumption that certain mathematical problems are computationally difficult to solve. The particular problem in question is the problem of factoring the number N into the two primes p, q.

We note the following: As ⟨N, e⟩ is the public key, the number N will be publicly known. If the factors p, q were also known, then one could easily compute φ(N) = (p − 1)(q − 1), and thus one could compute d from the given information by using the algorithm for finding inverses in ℤ_{N}. Thus, the cipher would be broken. It follows that the RSA cipher is only secure as long as an attacker cannot efficiently find the prime factors of N.

In a certain sense, a converse to this statement also holds. Given N and e, knowing d is at least as hard as knowing p, q, as shown by the following theorem:

Theorem 3.2. Given N, e, and d as above, one can efficiently find p and q.

Proof. It is known that N = pq for some unknown primes p and q. It is also known that ed ≡ 1(mod φ(N)), where φ(N) = (p − 1)(q − 1). Compute k = ed − 1. Then φ(N)|k. Because φ(N) is even, we have k = 2^{t}r for some odd r and t ⩾ 1. Then for all g ∈ ℤ_{N}^{∗}, we have g^{k} = ¯1. It follows that g^{k/2} is a square root of unity in ℤ_{N}. By Lemma 1.8(2), ¯1 has exactly four square roots in ℤ_{N}, of which two are ±1. The other two are ±x, where x ≡ 1(mod p) and x ≡ −1(mod q). Using either one of these roots, compute gcd(x − 1, N). Since either p|x − 1 or q|x − 1 (but not both), this gcd reveals either p or q.

If g is chosen at random from ℤ_{N}^{∗}, then with probability at least 1/2, one of the elements in g^{k/2}, g^{k/4}, …, g^{k/2^{t}} is a square root of unity which reveals the factorization of N. Thus, the factorization of N can be found by repeatedly choosing g at random.

The last claim about probabilities can be seen as follows. Let s be the smallest integer such that there exists some g ∈ ℤ_{N}^{∗} with g^{k/2^{s}} ≠ 1. Note that since k/2^{t} is odd, we always have (−1)^{k/2^{t}} ≠ 1 in ℤ_{N}^{∗}, thus, such an integer s always exists. Now let G = {g ∈ ℤ_{N}^{∗} | g^{k/2^{s}} = ±1}. Then G is a subgroup of ℤ_{N}^{∗}. We show that G ≠ ℤ_{N}^{∗}: by definition of s, there exists some g ∈ ℤ_{N}^{∗} such that g^{k/2^{s}} ≠ 1.

Then either g^{k/2^{s}} ≠ −1, in which case g ∉ G, or else, g^{k/2^{s}} = −1. In this latter case, by the Chinese Remainder Theorem, there exists some g′ ∈ ℤ_{N}^{∗} such that g′ ≡ g(mod p) and g′ ≡ 1(mod q). This implies (g′)^{k/2^{s}} ≡ −1(mod p) and (g′)^{k/2^{s}} ≡ 1(mod q), thus (g′)^{k/2^{s}} ≢ ±1(mod N). It follows that g′ ∉ G. So in either case, G is a proper subgroup of ℤ_{N}^{∗}. By Lemma 1.3(1), it follows that |G| divides ℤ_{N}^{∗}, thus |G| ⩽ \frac{1}{2}|ℤ_{N}^{∗}|. It follows that if we pick a random element g ∈ ℤ_{N}^{∗}, then g ∉ G with probability at least 1/2, and in this case, g^{k/2^{s}} will be a root of unity other than ±1. □

Remark. The above argument shows that computing d is at least as difficult as factoring N, if N and e are given, and N is the product of two primes. As factoring is assumed to be difficult, this provides some circumstantial evidence about the security of RSA.

It is important to note, however, that this does not provide conclusive proof of the security of RSA, even if one takes for granted that factoring is difficult. First, it might be possible to break the RSA cryptosystem without first computing d. Second, even if factoring is difficult in general, it is possible that it is easier in special cases, for instance, if N is known to be a product of exactly two primes. Third, even if the RSA cryptosystem is secure in the general case, there might be some special cases, such as badly chosen parameters, for which the system is insecure. There are in fact several known weaknesses of RSA of this latter kind. We will discuss them in the next section.

3.5 Known weaknesses of RSA, and how to avoid them

While the RSA cryptosystem is believed to be secure in the general case, there are several known attacks which work in special cases, i.e., for certain badly chosen parameters N, e, and/or d. When implementing the RSA cryptosystem in practice, it is therefore necessary to be acquainted with these well-known exploits and to avoid them. In other words, RSA is not secure unless it is properly implemented.

Common modulus. Since finding large prime numbers can be time-consuming, it is tempting to fix p and q once and for all, and to re-use the same modulus N, only changing e and d when generating new key pairs. This, however, is not secure. If a user knows one such pair ⟨e, d⟩, this is enough to recover p and q, and thus to find the private keys to all public keys using the same modulus.

Badly chosen parameters. In order for RSA to be secure, one should chose p, q such that p, q ≡ 3(mod 4). Otherwise, there is a known attack.

The numbers p, q must be chosen so that p − 1 and q − 1 must not have odd small prime factors (note that if p is chosen at random, it is quite likely that p − 1 has a small odd prime factor; such situations give rise to an attack and are to be avoided).

The private key d must not be chosen too small. It should be at least \frac{1}{3}N^{1/4}, or else there is a known attack.

The public key e must be at least 65537.

Blinding attacks. So-called blinding attacks are based on the idea that an attacker might attempt to decrypt a message by fooling the legitimate holder of the secret key into decrypting it without that person’s knowledge. Suppose that Bob has generated a public/private key pair e, d with some modulus N. Suppose Alice wants to fool Bob into decrypting some ciphertext C, where C ≡ M^{e}(mod N), and C, but not M, is known to Alice. She can pick a random number r ∈ ℤ_{N}^{∗} and compute C′ ≡ r^{e}C. She can then ask Bob to decrypt C′. Bob will compute M′ ≡ (C′)^{d} ≡ (r^{e}C)^{d} ≡ r^{ed}C^{d} ≡ rM(mod N). Since the number r is random, the message M′ will appear as a meaningless string of random data to Bob. Bob may foolishly decide to let Alice have this apparently worthless data. But now Alice can use M′ to compute the real original message M: namely, she computes r^{−1}M′ ≡ r^{−1}rM ≡ M(mod N).

Blinding attacks are possible because of algebraic properties of the RSA cryptosystem, namely the property that multiplication of plaintexts corresponds to multiplication of ciphertexts.

Further Reading

The above list of known weaknesses of the RSA cryptosystem is incomplete. Because from time to time, additional potential attacks are discovered, it is important that real-life implementations of RSA are done by experts who are familiar with these weaknesses and who know how to avoid them. More information on known attacks on the RSA cryptosystem can be found in the following article:

Dan Boneh, Twenty years of attacks on the RSA cryptosystem. In Notices of the American Mathematical Society (AMS), Vol. 46, No. 2, pp. 203–213, 1999. Also available from http://crypto.stanford.edu/˜ dabo/abstracts/RSAattack-survey.html