Skip to main content
RSA Attacks (Small e, Wiener, Hastad) & Weak PRNGs
Lesson 13 of 16

RSA Attacks (Small e, Wiener, Hastad) & Weak PRNGs

Exploit structural RSA misconfigurations including small e, Hastad's broadcast, common modulus, Wiener's attack and Fermat factoring, and recover state from weak PRNGs such as MT19937.

Advanced 36 min Cryptography

Learning objectives

  • Identify when unpadded RSA parameters allow algebraic recovery without factoring N
  • Demonstrate small-e root extraction, Hastad's broadcast attack, common-modulus recovery, Fermat factoring and Wiener's continued-fraction attack
  • Explain why Mersenne Twister (MT19937) is unsuitable for cryptography and recover its internal state from 624 outputs
  • Select safe RSA parameters, padding schemes (OAEP/PSS) and OS-backed CSPRNG APIs

Recommended first: Math Foundations: Modular Arithmetic, GCD & CongruencesAsymmetric Cryptography: RSA, Diffie-Hellman & ECCCryptographic Attack Models and Implementation Failures

#Why this matters

Properly generated 2048-bit RSA moduli cannot be factored practically, yet RSA systems still fail when implementations omit randomized padding, choose extreme exponents, generate close primes, or seed keys from weak pseudorandom number generators (PRNGs). A predictable PRNG undermines every layer at once, exposing RSA primes, ECDSA nonces, and session tokens.

#Algebraic RSA attacks without factoring

Textbook RSA computes c = m^e mod N and m = c^d mod N. Without OAEP padding (RFC 8017), misconfigured exponents or shared moduli let an attacker recover m without factoring N.

#Small public exponent (e = 3)

When e = 3 and the unpadded message satisfies m^3 < N, modular reduction never wraps around. Because c = m^3 over the integers, an integer cube root recovers m directly.

Integer cube-root recovery when m^3 < N
N = 1000000016000000063
c = 2392232797554488  # Encrypted with e = 3, no padding

lo, hi = 0, c
while lo < hi:
    mid = (lo + hi) // 2
    if mid ** 3 < c:
        lo = mid + 1
    else:
        hi = mid

print("Recovered plaintext integer:", lo)  # 133742

#Hastad's broadcast attack

Sending the same unpadded message m to e recipients with pairwise coprime moduli N_1, N_2, ..., N_e defeats m^3 >= N. For e = 3, the Chinese Remainder Theorem (CRT) combines (c_1, N_1), (c_2, N_2), and (c_3, N_3) into C = m^3 mod (N_1 * N_2 * N_3). Since m < N_i for every recipient, m^3 < N_1 * N_2 * N_3, so an exact cube root of C yields m.

#Common modulus attack

If two recipients share modulus N with coprime public exponents gcd(e_1, e_2) = 1 and receive the same message m (c_1 = m^e_1 mod N, c_2 = m^e_2 mod N), Extended GCD finds integers u, v with u * e_1 + v * e_2 = 1. The attacker computes m = (c_1^u * c_2^v) mod N, substituting c_i^(-1) mod N whenever u or v is negative.

#Factoring traps: Wiener and Fermat

#Wiener's small private exponent attack

Choosing a tiny private exponent d for fast decryption forces e = d^(-1) mod phi(N) to satisfy |e / phi(N) - k / d| = 1 / (d * phi(N)). Wiener's theorem proves that when q < p < 2q and d < (1 / 3) * N^(1/4), k / d appears among the continued fraction convergents of e / N. Each convergent yields a candidate phi(N) = (e * d - 1) / k, letting you factor N via x^2 - (N - phi(N) + 1)x + N = 0. (Boneh-Durfee lattice attacks extend this to d < N^0.292.)

#Fermat's factorization for close primes

Every odd composite N = p * q equals a^2 - b^2 = (a + b)(a - b) where a = (p + q) / 2 and b = |p - q| / 2. If p and q are chosen close together, b is tiny. Starting at a = ceil(sqrt(N)) and incrementing a until a^2 - N is a square b^2 factors N rapidly.

Attack Vulnerable condition Technique Primary defence
Small e root m^e < N, no padding Integer e-th root Use OAEP or PSS padding
Hastad broadcast Same m sent to e moduli CRT + integer e-th root Randomized OAEP per ciphertext
Common modulus Shared N, gcd(e_1, e_2) = 1 Extended GCD on exponents Unique (p, q) per keypair
Wiener's attack d < (1/3) * N^(1/4) Continued fractions of e/N Standard e = 65537, large d
Fermat factoring Small ` p - q `
  1. Inspect RSA inputsCheck e, N, ciphertexts
  2. Same N, coprime e1/e2Run Extended GCD common-modulus
  3. Multiple (ci, Ni), small eRun CRT + integer root (Hastad)
  4. Single ciphertext, small e or close primesTry iroot, Fermat, Wiener
  5. Automated triageVerify findings in SageMath or RsaCtfTool

#Weak PRNGs and MT19937 state recovery

Non-cryptographic PRNGs optimize for speed, not unpredictability. Seeding from time.time() reduces a secret to a small boot-time window. Worse, Python's random and Ruby's Random implement Mersenne Twister (MT19937), which is linear over GF(2).

MT19937 keeps 624 32-bit state words and passes each output through four invertible tempering steps (shifts, XORs, and bitmasks). Observing 624 consecutive 32-bit outputs lets an analyst untemper every word, load the state via setstate, and predict all future outputs. Even on truncated outputs (such as random.randint(0, 255)), SMT solvers like z3 recover the state from bit-vector constraints.

Inverting MT19937 tempering to clone generator state
import random

def untemper(y):
    y ^= (y >> 18)
    y ^= (y << 15) & 0xefc60000
    tmp = y
    for _ in range(4):
        tmp = y ^ ((tmp << 7) & 0x9d2c5680)
    y = tmp
    tmp = y
    for _ in range(3):
        tmp = y ^ (tmp >> 11)
    return tmp & 0xffffffff

victim = random.Random(12345)
observed = [victim.getrandbits(32) for _ in range(624)]
cloned_state = tuple(untemper(v) for v in observed)

clone = random.Random()
clone.setstate((3, cloned_state + (624,), None))
assert clone.getrandbits(32) == victim.getrandbits(32)

#Defending and common mistakes

  • Always use standardized padding: Encrypt with RSA-OAEP (SHA-256) and sign with RSA-PSS (RFC 8017), or adopt X25519 / Ed25519.
  • Follow safe RSA key rules: Use at least 2048-bit (or 3072-bit) moduli, e = 65537, independent CSPRNG primes, and unique keys without shrinking d.
  • Use an OS-backed CSPRNG: Use secrets, os.urandom(), crypto.getRandomValues(), or getrandom() instead of random, Math.random(), or time.time() seeds.
  • Common mistakes: Assuming e = 3 is safe without OAEP on 2048-bit keys; truncating MT19937 outputs and assuming z3 cannot invert them; running RsaCtfTool without diagnosing the underlying key-generation flaw.

#Summary

  • Unpadded RSA fails under small e (m^e < N), Hastad's CRT broadcast across e moduli, and common-modulus reuse with coprime exponents.
  • Small d < (1/3) * N^(1/4) enables Wiener's continued-fraction attack; close primes enable Fermat factoring.
  • MT19937 leaks its 624-word state after 624 32-bit outputs (or via z3 on partial bits).
  • Prevent these flaws with RSA-OAEP/PSS, unique independent keys, and OS-backed CSPRNGs (secrets).

Knowledge check

Sign in to take the test, complete lessons and save your progress. Log in

Practice: test your knowledge