RSA Key Generation
RSA (Rivest, Shamir, Adleman, 1977) was the first practical public-key cryptosystem and is still used for signatures and key transport. Its key generation is a tour of the course so far: random primes, Euler's or Carmichael's function, a gcd check and a modular inverse.
The recipe
- Choose two random primes of equal size (1024 bits each for 2048-bit RSA).
- Compute (the modulus) and (or ).
- Choose the public exponent with . Almost everyone uses .
- Compute the private exponent .
Public key: . Private key: (plus , and the CRT values for speed).
Encryption is and decryption is ; correctness is Euler's theorem.
from math import gcd, lcm
from sympy import nextprime
# Toy 128-bit primes from fixed starting points (real keys use unpredictable 1024+-bit primes)
p = nextprime(0xC3A5_9F12_7E44_0B8D_19A2_77C0_5D3E_F001)
q = nextprime(0xE1F0_3B6C_A925_D47E_8B01_C3F2_6A78_9002)
n = p * q
lam = lcm(p - 1, q - 1)
e = 65537
assert gcd(e, lam) == 1
d = pow(e, -1, lam)
print("n bits:", n.bit_length())
print("e * d mod lambda:", e * d % lam)
m = int.from_bytes(b"hello RSA", "big")
c = pow(m, e, n)
print("decrypts:", pow(c, d, n).to_bytes(9, "big"))n bits: 256
e * d mod lambda: 1
decrypts: b'hello RSA'
Why ?
- It is prime, so unless divides or , which key generators simply avoid.
- It has only two 1-bits, so encryption and signature verification cost 17 multiplications (Fast Modular Exponentiation).
- It is large enough to avoid the attacks on with poor padding.
What must stay secret
Anyone who learns any one of , , , or can compute all the others. Knowing gives a multiple of , namely , and that is enough to factor with a randomized algorithm that looks for a non-trivial square root of 1 (the same trick as in Miller-Rabin).
from math import gcd
def factor_from_d(n, e, d):
k = e * d - 1 # a multiple of lambda(n)
t, s = k, 0
while t % 2 == 0:
t, s = t // 2, s + 1
for g in range(2, 100):
x = pow(g, t, n)
for _ in range(s):
y = x * x % n
if y == 1 and x not in (1, n - 1):
return gcd(x - 1, n)
x = y
return None
p, q, e = 1000003, 1000033, 65537
n = p * q
d = pow(e, -1, (p - 1) * (q - 1))
f = factor_from_d(n, e, d)
print(f, n // f)1000003 1000033
Consequence: never share a modulus between users, even with different exponents. Each user's factors the common .
Key sizes
| Modulus | Security | Status |
|---|---|---|
| 512 bits | none (factored in hours) | broken |
| 1024 bits | about 80 bits | deprecated |
| 2048 bits | about 112 bits | minimum today |
| 3072 bits | about 128 bits | recommended for long-term use |
| 4096 bits | about 140 bits | common for root CAs |
In software
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:3072, Pythoncryptography'srsa.generate_private_key(public_exponent=65537, key_size=3072).- The private-key file (PKCS #1 / PKCS #8) stores : the extra numbers are for CRT decryption.
Exercises
Exercise 1: Tiny key by hand
With , and , compute , and (use ). Encrypt .
Answer
, , , .
Solution
, , , and , so . , so . Check decryption with pow(14, 27, 55), which returns 9.
Exercise 2: A bad exponent
With and , why is not allowed?
Answer
.
Solution
(and ). Both are divisible by 3, so 3 has no inverse and decryption is impossible: is not even one-to-one modulo 91.
Exercise 3: versus (code)
For , and , compute with and with and check that both decrypt.
Answer
Different values of , both correct.
Solution
from math import lcm
p, q, e = 1000003, 1000033, 65537
n = p * q
d_phi = pow(e, -1, (p - 1) * (q - 1))
d_lam = pow(e, -1, lcm(p - 1, q - 1))
c = pow(424242, e, n)
print(d_phi, d_lam, pow(c, d_phi, n), pow(c, d_lam, n))983264276609 149902609889 424242 424242
Any with works; the version is the smallest.
Test yourself
In RSA, the public key is…
The private exponent is…
- (or mod )
- a random number
Why must two users never share an RSA modulus?
- it wastes memory
- either user's key pair lets them factor and compute the other's
- it changes
- it is slower