Why Textbook RSA Is Insecure
"Textbook RSA", encrypting as with nothing else, is a beautiful trapdoor permutation but a terrible encryption scheme. This lesson breaks it five ways, without ever factoring . Every attack below has hit real systems.
All examples use a toy key from fixed primes:
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.
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
: multiplying ciphertexts multiplies plaintexts. Without the key, an attacker turns an encryption of a bid into one of :
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 (which looks unrelated), get , divide by . That breaks IND-CCA security.
Attack 3: small message, small exponent
With and a short message, : no modular reduction happens, and an ordinary integer cube root recovers .
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 to three recipients with moduli : the CRT combines into , and since , , so again a plain cube root finishes.
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 is encrypted under one modulus with two exponents with , Bézout gives , and .
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'spadding.OAEPandpadding.PSS. Raw RSA ("NoPadding") exists for protocol implementers and is a frequent source of vulnerabilities. - Attack 3 resurfaced in 2006 as Bleichenbacher's 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 . Which attack recovers it, and how much work is needed?
Answer
Attack 1: encrypt all 10000 PINs and compare.
Solution
There are only candidates; encrypting each with the public key and comparing with the ciphertext takes milliseconds. The size of and is irrelevant: determinism is the flaw.
Exercise 2: Blinding the oracle
A decryption service refuses to decrypt the target ciphertext but decrypts anything else. How do you get ?
Answer
Ask for , receive , divide by 2 modulo .
Solution
, so the service decrypts it: . Multiply by . Any coprime to works in place of 2.
Exercise 3: When does the cube-root attack fail? (code)
With and a 512-bit modulus, how long can a message be (in bytes) before and Attack 3 stops working?
Answer
Up to 21 bytes ().
Solution
needs , that is 170 bits, so 21 whole bytes (168 bits) always work.
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 with random bytes, which defeats this attack.
Test yourself
Textbook RSA encryption is deterministic. What does that allow?
- faster decryption
- testing guesses by encrypting them with the public key
- factoring
- nothing
Malleability of RSA means…
- keys can be changed
- multiplying ciphertexts multiplies the plaintexts
- the modulus can be resized
- signatures are optional
Håstad's broadcast attack combines three ciphertexts with…
- the extended Euclidean algorithm only
- the Chinese remainder theorem and an integer cube root
- Pollard rho
- a hash function