1855 Words

✏️ RSA: exercises and in depth

Overview · Belongs to 1. The mathematics, section 3 · Previous: ← Prime numbers · Next: Diffie–Hellman in depth →

Part 1 made one RSA key with $p = 47$ and $q = 59$. Here you make several yourself, prove why it works, see how real software makes it four times faster, and break it in the ways real attackers have. You need clock arithmetic (inverses) and prime numbers (Euler’s φ). Answers are hidden under Answer.


1. The recipe

StepExample ($p = 61$, $q = 53$)
1Pick two different primes $p$, $q$$61$, $53$
2$n = p \cdot q$ (public)$3233$
3$\varphi(n) = (p-1)(q-1)$ (secret)$60 \cdot 52 = 3120$
4Pick $e$ with $\gcd(e, \varphi(n)) = 1$ (public)$17$
5$d = e^{-1} \bmod \varphi(n)$ (secret)$2753$
Encrypt: $c = m^e \bmod n$$65^{17} \bmod 3233 = 2790$
Decrypt: $m = c^d \bmod n$$2790^{2753} \bmod 3233 = 65$
Sign: $s = h^d \bmod n$$100^{2753} \bmod 3233 = 1391$
Verify: $s^e \bmod n \overset{?}{=} h$$1391^{17} \bmod 3233 = 100$ ✓

Check step 5: $17 \cdot 2753 = 46801 = 15 \cdot 3120 + 1$ ✓


2. Make your own keys

Exercise 2.1. $p = 11$, $q = 13$, $e = 7$. Calculate $n$, $\varphi(n)$ and $d$. Then encrypt $m = 9$ and decrypt the result again.

Answer

$n = 143$, $\varphi(n) = 10 \cdot 12 = 120$.

$d = 7^{-1} \bmod 120$: $120 = 17 \cdot 7 + 1$, so $1 = 120 - 17 \cdot 7$ and $d = -17 \equiv$ 103. Check: $7 \cdot 103 = 721 = 6 \cdot 120 + 1$ ✓

Encrypt with square-and-multiply: $9^2 = 81$, $9^4 = 81^2 = 6561 \equiv 126$, $9^7 = 9^4 \cdot 9^2 \cdot 9 \equiv 126 \cdot 81 \cdot 9 \equiv$ 48.

Decrypt: $48^{103} \bmod 143 = $ 9 ✓ (by hand this is long; use pow(48, 103, 143)).

Exercise 2.2. $p = 5$, $q = 11$, $e = 3$. Find $d$, encrypt $m = 8$, and decrypt the ciphertext $c = 13$.

Answer

$n = 55$, $\varphi = 40$, $d = 3^{-1} \bmod 40 = $ 27 ($81 = 2 \cdot 40 + 1$).

$8^3 = 512 = 9 \cdot 55 + 17$, so $c = $ 17.

$13^{27} \bmod 55 = $ 7. Check: $7^3 = 343 = 6 \cdot 55 + 13$ ✓

Exercise 2.3. Bob’s public key is $(n, e) = (221, 5)$. You intercept $c = 172$. Bob was careless and his $n$ is tiny. Break it: factor $n$, find $d$ and read the message.

Answer

$221 = 13 \cdot 17$, so $\varphi = 12 \cdot 16 = 192$ and $d = 5^{-1} \bmod 192 = $ 77 ($5 \cdot 77 = 385 = 2 \cdot 192 + 1$).

$172^{77} \bmod 221 = $ 100. Check: $100^5 \bmod 221 = 172$ ✓. This is the whole attack on RSA: factor $n$. Everything after that is easy.

Exercise 2.4. With $p = 61$, $q = 53$: which of $e = 3, 5, 7, 11$ are allowed? Why must $\gcd(e, \varphi(n))$ be 1?

Answer

$\varphi = 3120 = 2^4 \cdot 3 \cdot 5 \cdot 13$. So 3 and 5 are not allowed, 7 and 11 are. If $e$ shared a factor with $\varphi(n)$, it would have no inverse modulo $\varphi(n)$, so there would be no $d$. Worse: different messages would then encrypt to the same ciphertext, so decryption would be impossible even in principle. This is also why real keys use $e = 65537$: it is prime, so it only fails if $65537$ divides $p - 1$ or $q - 1$, and key generators simply pick other primes in that case.


3. Why decryption gives the message back

We need $m^{ed} \equiv m \pmod n$. By construction $ed = 1 + k \cdot \varphi(n)$ for some whole number $k$.

Case 1: $\gcd(m, n) = 1$ (almost always). By Euler’s theorem $m^{\varphi(n)} \equiv 1$:

$$m^{ed} = m^{1 + k\varphi(n)} = m \cdot \left(m^{\varphi(n)}\right)^k \equiv m \cdot 1 = m \pmod n$$

Case 2: $m$ is a multiple of $p$ (or $q$). Euler does not apply, but it still works. Look at it modulo $p$ and modulo $q$ separately:

  • Modulo $p$: $m \equiv 0$, so $m^{ed} \equiv 0 \equiv m$.
  • Modulo $q$: $q$ does not divide $m$, so Fermat gives $m^{q-1} \equiv 1$, and since $(q-1)$ divides $\varphi(n)$: $m^{ed} = m \cdot (m^{q-1})^{k(p-1)} \equiv m$.

A number that is correct modulo $p$ and modulo $q$ is correct modulo $pq$ (the Chinese remainder theorem, next section). ∎

Exercise 3.1. With $n = 2773$, $e = 17$, $d = 157$: encrypt and decrypt $m = 47$, a multiple of $p$. Does it come back?

Answer

Yes: pow(pow(47, 17, 2773), 157, 2773) gives 47. (But such a message reveals $p$: $\gcd(c, n) = 47$. With real 1536-bit primes the chance of hitting a multiple by accident is about $2^{-1536}$.)

A detail you will see in real keys. Instead of $\varphi(n)$, modern software calculates $d$ modulo $\lambda(n) = \operatorname{lcm}(p-1, q-1)$, the Carmichael function. That gives a smaller $d$ that works just as well. For our $n = 2773$: $\lambda = \operatorname{lcm}(46, 58) = 1334$, and $17^{-1} \bmod 1334$ happens to be 157 again.


4. Four times faster: the Chinese remainder theorem

The owner of the private key knows $p$ and $q$, so they can calculate modulo $p$ and modulo $q$ separately (numbers half the size, and exponents half the size), and glue the results together. Each half costs about 1/8 of the full calculation, so the two together are about 4 times faster.

Precompute (these are stored in every RSA private key file):

$$d_p = d \bmod (p-1), \qquad d_q = d \bmod (q-1), \qquad q_{\text{inv}} = q^{-1} \bmod p$$

Then to decrypt $c$:

$$m_p = c^{d_p} \bmod p, \qquad m_q = c^{d_q} \bmod q, \qquad h = q_{\text{inv}}(m_p - m_q) \bmod p, \qquad m = m_q + h \cdot q$$

Worked example with part 1’s key ($p = 47$, $q = 59$, $d = 157$) and $c = 1793$:

$d_p = 157 \bmod 46$$19$
$d_q = 157 \bmod 58$$41$
$q_{\text{inv}} = 59^{-1} \bmod 47$$4$ (because $59 \cdot 4 = 236 = 5 \cdot 47 + 1$)
$m_p = 1793^{19} \bmod 47 = 7^{19} \bmod 47$$12$
$m_q = 1793^{41} \bmod 59 = 23^{41} \bmod 59$$54$
$h = 4 \cdot (12 - 54) \bmod 47 = -168 \bmod 47$$20$
$m = 54 + 20 \cdot 59$1234 ✓

⚠️ The Bellcore attack (1996). If a hardware fault makes one of the two halves wrong while signing, the faulty signature $\hat s$ is still correct modulo the other prime. Then $\gcd(\hat s^e - h, n)$ is that prime, and the key is broken from one bad signature. Real implementations therefore verify each signature before releasing it.


5. Breaking textbook RSA

Part 1 warned that RSA without padding is unsafe. Here are five ways to see why, each with numbers.

5.1 Same message, same ciphertext

Textbook RSA is deterministic. Key $(3233, 17)$; a vote is either $m = 10$ (“yes”) or $m = 20$ (“no”).

Exercise. You see the encrypted vote $c = 3023$. How did they vote?

Answer

Encrypt both options yourself with the public key: $10^{17} \bmod 3233 = 1096$ and $20^{17} \bmod 3233 = 3023$. So the vote was “no”. Padding (OAEP) adds fresh randomness to every encryption so that the same message never gives the same ciphertext.

5.2 Small message, small $e$: just take the cube root

Key $(n, e) = (391, 3)$ ($= 17 \cdot 23$). Someone encrypts $m = 7$: $c = 7^3 = 343$. Since $343 < 391$, the “modulo” never did anything. Exercise: recover $m$ from $c = 343$ without the private key.

Answer

$\sqrt[3]{343} = $ 7. Ordinary cube roots are easy; only modular ones are hard. With $e = 3$ and an unpadded 128-bit AES key as message, $m^3$ is 384 bits, far below a 3072-bit $n$, so this would break real systems. Padding makes $m$ as big as $n$.

5.3 Malleability: changing a message you cannot read

Multiplying ciphertexts multiplies the messages: $(m_1^e)(m_2^e) = (m_1 m_2)^e$.

Exercise. Key $(2773, 17)$. The ciphertext of an amount is $c = 1793$. Without knowing the amount, make a ciphertext of twice that amount. ($2^{17} \bmod 2773 = 741$.)

Answer

$\hat c = 1793 \cdot 741 \bmod 2773 = $ 346. The owner decrypts it to $346^{157} \bmod 2773 = 2468 = 2 \cdot 1234$. You doubled a payment you could not even read. The same property against signatures, combined with error messages, is the basis of Bleichenbacher’s attack on PKCS #1 v1.5 (1998), which keeps coming back in TLS (the ROBOT attack, 2017).

5.4 Primes that are too close

See Fermat’s trick: $2773 = 53^2 - 6^2$ factors in one step, because 47 and 59 are close.

5.5 Two keys with a shared prime

See the gcd attack: $\gcd(2773, 2491) = 47$ breaks both keys.


6. Signatures in more detail

You never sign the document itself but its hash, padded to the size of $n$ (PSS or PKCS #1 v1.5). Without the hash, anybody could forge: pick any $s$, calculate $m = s^e \bmod n$, and $s$ is a valid signature on that (meaningless) $m$.

Exercise 6.1. Key $(3233, 17)$, $d = 2753$. (a) Sign $h = 100$. (b) Somebody gives you the “signature” $s = 1391$ on $h = 101$. Is it valid?

Answer

(a) $s = 100^{2753} \bmod 3233 = $ 1391.

(b) $1391^{17} \bmod 3233 = 100 \ne 101$: invalid. One changed bit in the document changes the hash, and the old signature no longer fits.

Exercise 6.2. Signatures also multiply: $s_1 s_2$ is a valid signature on $h_1 h_2$. Why is that harmless when you sign hashes?

Answer

To abuse it, the attacker would need a document whose hash is exactly $h_1 h_2 \bmod n$. Finding a document for a given hash is precisely what a secure hash function prevents (preimage resistance).


7. A real key

Generate a key and print every number in it:

openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out /tmp/k.pem
openssl pkey -in /tmp/k.pem -text -noout | less

You will see exactly the numbers from this page:

OpenSSL nameOur symbolExample value
modulus$n$2048 bits
publicExponent$e$65537 (0x10001)
privateExponent$d$about 2048 bits
prime1, prime2$p$, $q$1024 bits each
exponent1, exponent2$d_p$, $d_q$for the CRT speed-up (section 4)
coefficient$q_{\text{inv}}$for the CRT speed-up

$e = 65537 = 2^{16} + 1$ is chosen because it is prime and has only two 1-bits: encrypting and verifying take 16 squarings and 1 multiplication. Your SSH public key ~/.ssh/id_rsa.pub contains nothing but $e$ and $n$.

Why 2048 or 3072 bits and not 1024 or 15360: Why 77 to 925 digits?


Check yourself in Python

p, q, e = 61, 53, 17
n, phi = p * q, (p - 1) * (q - 1)
d = pow(e, -1, phi)          # 2753
c = pow(65, e, n)            # 2790
pow(c, d, n)                 # 65
pow(1793 * 741, 157, 2773)   # 2468: malleability

Next: Diffie–Hellman in depth →