TExcelTAD
Home › Number Theory & Cryptography › Why Textbook RSA Is Insecure

Why Textbook RSA Is Insecure

"Textbook RSA", encrypting mm as c=me mod nc = m^e \bmod n with nothing else, is a beautiful trapdoor permutation but a terrible encryption scheme. This lesson breaks it five ways, without ever factoring nn. Every attack below has hit real systems.

All examples use a toy key from fixed primes:

python · try it yourself
from sympy import nextprime
from math import lcm

p = nextprime(2**256 + 7919)
q = nextprime(2**256 + 2**200 + 104729)
n, e = p * q, 65537
d = pow(e, -1, lcm(p - 1, q - 1))
print(n.bit_length(), "bit modulus")
513 bit modulus

Note: Each block below repeats the key setup, because playground runs do not share variables.

Attack 1: deterministic means guessable

The same message always gives the same ciphertext, and anyone can encrypt with the public key. If the message comes from a small set ("yes"/"no", a vote, a salary band), the attacker encrypts each candidate and compares.

python · try it yourself
from sympy import nextprime
p, q = nextprime(2**256 + 7919), nextprime(2**256 + 2**200 + 104729)
n, e = p * q, 65537

enc = lambda m: pow(int.from_bytes(m.encode(), "big"), e, n)
intercepted = enc("NO")                        # someone's secret vote
for guess in ("YES", "NO", "ABSTAIN"):
    if enc(guess) == intercepted:
        print("the vote was", guess)
the vote was NO

Attack 2: malleability

(m1m2)e=m1em2e(m_1 m_2)^e = m_1^e m_2^e: multiplying ciphertexts multiplies plaintexts. Without the key, an attacker turns an encryption of a bid mm into one of 2m2m:

python · try it yourself
from sympy import nextprime
from math import lcm
p, q = nextprime(2**256 + 7919), nextprime(2**256 + 2**200 + 104729)
n, e = p * q, 65537
d = pow(e, -1, lcm(p - 1, q - 1))

bid = 5000
c = pow(bid, e, n)
c_forged = c * pow(2, e, n) % n                # attacker never sees the bid
print("victim decrypts:", pow(c_forged, d, n))
victim decrypts: 10000

The same property, combined with a decryption oracle, lets an attacker decrypt any target: ask for the decryption of c⋅rec \cdot r^e (which looks unrelated), get mrmr, divide by rr. That breaks IND-CCA security.

Attack 3: small message, small exponent

With e=3e = 3 and a short message, m3<nm^3 < n: no modular reduction happens, and an ordinary integer cube root recovers mm.

python · try it yourself
from sympy import nextprime
p, q = nextprime(2**256 + 7919), nextprime(2**256 + 2**200 + 104729)
n = p * q

def icbrt(x):                                  # integer cube root by Newton's method
    r = 1 << ((x.bit_length() + 2) // 3)
    while True:
        s = (2 * r + x // (r * r)) // 3
        if s >= r:
            return r
        r = s

m = int.from_bytes(b"key=0x1f2e3d", "big")    # 96-bit message
c = pow(m, 3, n)                               # e = 3, no padding
print(c == m**3, icbrt(c).to_bytes(12, "big"))
True b'key=0x1f2e3d'

Attack 4: Håstad's broadcast

The same message sent with e=3e = 3 to three recipients with moduli n1,n2,n3n_1, n_2, n_3: the CRT combines m3 mod nim^3 \bmod n_i into m3 mod n1n2n3m^3 \bmod n_1 n_2 n_3, and since m<nim < n_i, m3<n1n2n3m^3 < n_1 n_2 n_3, so again a plain cube root finishes.

python · try it yourself
from sympy import nextprime
from math import prod

def icbrt(x):
    r = 1 << ((x.bit_length() + 2) // 3)
    while True:
        s = (2 * r + x // (r * r)) // 3
        if s >= r:
            return r
        r = s

ns = [nextprime(2**120 + k * 10**9) * nextprime(2**121 + k * 10**9) for k in (1, 2, 3)]
m = int.from_bytes(b"meet at noon", "big")
cs = [pow(m, 3, n) for n in ns]
N = prod(ns)
x = sum(c * (N // n) * pow(N // n, -1, n) for c, n in zip(cs, ns)) % N
print(icbrt(x).to_bytes(12, "big"))
b'meet at noon'

Attack 5: common modulus

If the same mm is encrypted under one modulus with two exponents e1,e2e_1, e_2 with gcd⁡(e1,e2)=1\gcd(e_1, e_2) = 1, Bézout gives ae1+be2=1ae_1 + be_2 = 1, and c1ac2b=mae1+be2=mc_1^a c_2^b = m^{ae_1 + be_2} = m.

python · try it yourself
from sympy import nextprime
p, q = nextprime(2**256 + 7919), nextprime(2**256 + 2**200 + 104729)
n = p * q
e1, e2 = 65537, 17
m = int.from_bytes(b"same message", "big")
c1, c2 = pow(m, e1, n), pow(m, e2, n)

def ext_gcd(a, b):
    r0, x0, y0, r1, x1, y1 = a, 1, 0, b, 0, 1
    while r1:
        q_ = r0 // r1
        r0, r1, x0, x1, y0, y1 = r1, r0 - q_ * r1, x1, x0 - q_ * x1, y1, y0 - q_ * y1
    return x0, y0

a, b = ext_gcd(e1, e2)
rec = pow(c1, a, n) * pow(c2, b, n) % n       # negative exponents use modular inverses
print(a, b, rec.to_bytes(12, "big"))
-8 30841 b'same message'

The lesson

Textbook RSA is a trapdoor permutation, a building block, not an encryption scheme. Turning it into one requires randomized padding (OAEP), and turning it into a signature scheme requires hashing and padding (PSS). The next two lessons show how.

In software

  • Libraries expose RSA only with padding: cryptography's padding.OAEP and padding.PSS. Raw RSA ("NoPadding") exists for protocol implementers and is a frequent source of vulnerabilities.
  • Attack 3 resurfaced in 2006 as Bleichenbacher's e=3e = 3 signature forgery against lax PKCS #1 parsers (Firefox, OpenSSL) and again in later library bugs.

Exercises

Exercise 1: Identify the attack

A server encrypts a 4-digit PIN with textbook RSA and e=65537e = 65537. Which attack recovers it, and how much work is needed?

Answer

Attack 1: encrypt all 10000 PINs and compare.

Solution

There are only 10410^4 candidates; encrypting each with the public key and comparing with the ciphertext takes milliseconds. The size of ee and nn is irrelevant: determinism is the flaw.

Exercise 2: Blinding the oracle

A decryption service refuses to decrypt the target ciphertext cc but decrypts anything else. How do you get mm?

Answer

Ask for c′=c⋅2ec' = c \cdot 2^e, receive 2m2m, divide by 2 modulo nn.

Solution

c′≠cc' \ne c, so the service decrypts it: (c⋅2e)d=m⋅2(c \cdot 2^e)^d = m \cdot 2. Multiply by 2−1 mod n2^{-1} \bmod n. Any rr coprime to nn works in place of 2.

Exercise 3: When does the cube-root attack fail? (code)

With e=3e = 3 and a 512-bit modulus, how long can a message be (in bytes) before m3≥nm^3 \ge n and Attack 3 stops working?

Answer

Up to 21 bytes (m<2170.3m < 2^{170.3}).

Solution

m3<2511m^3 < 2^{511} needs m<2170.3m < 2^{170.3}, that is 170 bits, so 21 whole bytes (168 bits) always work.

python · try it yourself
bits = 511                      # n is at least 2^511
print(bits / 3, int(bits / 3) // 8)
170.33333333333334 21

Padding fills the message up to nearly the size of nn with random bytes, which defeats this attack.

Test yourself

Textbook RSA encryption is deterministic. What does that allow?

  1. faster decryption
  2. testing guesses by encrypting them with the public key
  3. factoring nn
  4. nothing

Malleability of RSA means…

  1. keys can be changed
  2. multiplying ciphertexts multiplies the plaintexts
  3. the modulus can be resized
  4. signatures are optional

Håstad's broadcast attack combines three ciphertexts with…

  1. the extended Euclidean algorithm only
  2. the Chinese remainder theorem and an integer cube root
  3. Pollard rho
  4. a hash function