Learn › Blockchain & Access Control › Module 2 · Public-key cryptography

RSA: key generation, square-and-multiply, Miller–Rabin and attacks

RSA key generation with a full numeric example, left-to-right square-and-multiply traced step by step, Fermat and Miller–Rabin primality tests on 561, factoring by congruence of squares, and timing attacks.

The RSA algorithm (Rivest, Shamir, Adleman, 1977)

Pick primes p, q; n = pq; φ(n) = (p − 1)(q − 1); choose e with gcd(e, φ(n)) = 1; d = e−1 mod φ(n).

Public {e, n}; private d. C = Me mod n; M = Cd mod n (works because ed ≡ 1 mod φ(n), by Euler’s theorem).

Worked example

p = 7, q = 17 ⇒ n = 119, φ(n) = 96. e = 5; d = 77 since 5·77 = 385 = 4·96 + 1. M = 19: 195 = 2,476,099 ≡ 66 (mod 119); 6677 mod 119 = 19.

Security rests on factoring n. Use p and q of at least 1,024 bits (n ≥ 2,048 bits). RSA does encryption, signatures and key transport (TLS certificates, SSH keys).

Square-and-multiply

Reduce mod n after every multiplication, and square repeatedly: x16 takes 4 squarings, not 15 multiplications.

z = 1; for each bit bi of the exponent from the most significant: z = z2 mod n; if bi = 1, z = z·a mod n.

Worked example: 97263533 mod 11413

3533 = 1101110011012.

Bit110111001101
z9726265956349167495877836298462910185105110255761

12 iterations instead of 3,532 multiplications.

Primality testing and Miller–Rabin

Key generation needs random large primes; trial division to √n is hopeless for 600-digit numbers. Proving n composite is far easier than factoring it.

  • If x2 ≡ y2 (mod n) but x ≢ ±y, n is composite and gcd(x − y, n) is a factor. Example: 122 ≡ 22 (mod 35), gcd(10, 35) = 5.
  • Fermat test, n = 35, a = 2: 234 ≡ 9 ≠ 1, so 35 is composite. Carmichael numbers (561 = 3·11·17) fool the Fermat test for every coprime base.

Miller–Rabin: write n − 1 = 2km (m odd); pick 1 < a < n − 1; b0 = am. If b0 = ±1, “probably prime”. Square up to k − 1 times: reaching −1 ⇒ probably prime; reaching 1 first ⇒ composite.

Worked example: n = 561

560 = 24·35. a = 2: b0 = 235 mod 561 = 263; b1 = 166; b2 = 67; b3 = 1. 67 is a non-trivial square root of 1, so 561 is composite, and gcd(66, 561) = 33 exposes factors.

One round errs with probability at most 1/4; 40 rounds give at most 2−80.

Factoring by congruence of squares

Quadratic-sieve idea: collect xi2 mod n that factor over a small base, combine them so every exponent is even, and get X2 ≡ Y2; then gcd(X + Y, n) is a factor.

Worked example: n = 539873

7352 ≡ 352 = 25·11; 7502 ≡ 113·17; 7832 ≡ 29·11·13; 8012 ≡ 25·11·172.
Rows 735 and 801 multiply to 210·112·172 = (25·11·17)2. X = 735·801 = 588735, Y = 5984.
gcd(594719, 539873) = 277; 539873 = 277 × 1949.

Timing side channels (Kocher, 1996)

In square-and-multiply, a 1 bit of d costs a squaring and a multiplication; a 0 bit only a squaring. Timing many decryptions of chosen ciphertexts reveals d bit by bit. Example: d = 20 = 101002 computing 520 mod 33 runs exponents 1 → 2 → 5 → 10 → 20 (values 5, 25, 23, 1, 1), with the slow steps at the 1 bits.

Brumley and Boneh (2003) extracted an OpenSSL key over a network this way. Countermeasures: constant-time code (Montgomery ladder), and blinding — decrypt C·re and divide out r afterwards.