RSA Encryption, Decryption and CRT Speed-Up
RSA's public operation is fast because is tiny. The private operation uses a full-size exponent and is the bottleneck. Working modulo and separately and recombining with the Chinese remainder theorem makes it about four times faster, and also introduces one of the most elegant attacks in cryptography.
Messages as numbers
RSA works on integers . A byte string is converted with big-endian int.from_bytes (the I2OSP/OS2IP primitives of PKCS #1). Real systems never encrypt raw data this way: they encrypt a random symmetric key, with padding.
The CRT decryption
Precompute
Then decrypt with two half-size exponentiations and Garner's recombination:
Why it works: , and by Fermat the exponent can be reduced modulo . Each half-size exponentiation costs about of a full one (half the exponent bits, a quarter of the cost per multiplication), so two of them cost about .
from sympy import nextprime
from math import lcm
p = nextprime(2**200 + 2**150 + 12345)
q = nextprime(2**200 + 2**170 + 67890)
n, e = p * q, 65537
d = pow(e, -1, lcm(p - 1, q - 1))
dP, dQ, qinv = d % (p - 1), d % (q - 1), pow(q, -1, p)
m = int.from_bytes(b"CRT makes RSA fast", "big")
c = pow(m, e, n)
m1, m2 = pow(c, dP, p), pow(c, dQ, q)
h = qinv * (m1 - m2) % p
m_crt = m2 + h * q
print(m_crt == pow(c, d, n) == m)
print("exponent bits: d", d.bit_length(), " dP", dP.bit_length(), " dQ", dQ.bit_length())
print("modulus bits: n", n.bit_length(), " p", p.bit_length())True
exponent bits: d 395 dP 200 dQ 198
modulus bits: n 401 p 201
Counting the work
The cost of an exponentiation with a -bit exponent and a -bit modulus is about multiplications of cost each. Comparing:
def cost(exp_bits, mod_bits):
return 1.5 * exp_bits * mod_bits**2
for bits in (2048, 3072, 4096):
full = cost(bits, bits)
crt = 2 * cost(bits // 2, bits // 2)
print(f"{bits}-bit RSA: CRT does {crt / full:.3f} of the work, a {full / crt:.0f}x speed-up")2048-bit RSA: CRT does 0.250 of the work, a 4x speed-up
3072-bit RSA: CRT does 0.250 of the work, a 4x speed-up
4096-bit RSA: CRT does 0.250 of the work, a 4x speed-up
The Bellcore fault attack
If a glitch (a voltage spike, a cosmic ray, a software bug) corrupts one of the two half computations of a CRT signature , the faulty is still correct modulo but wrong modulo . Then
and a single faulty signature factors the key (Boneh, DeMillo and Lipton, 1997).
from math import gcd, lcm
p, q, e = 1000003, 1000033, 65537
n = p * q
d = pow(e, -1, lcm(p - 1, q - 1))
m = 123456789
s1 = pow(m, d % (p - 1), p) ^ 1 # fault: one bit flipped in the mod-p half
s2 = pow(m, d % (q - 1), q)
h = pow(q, -1, p) * (s1 - s2) % p
s_bad = s2 + h * q
print("signature verifies:", pow(s_bad, e, n) == m)
print("gcd reveals:", gcd(pow(s_bad, e, n) - m, n), " q =", q)signature verifies: False
gcd reveals: 1000033 q = 1000033
The fix is cheap: verify the signature with the public key () before releasing it. Every serious RSA implementation does so.
In software
- OpenSSL, BoringSSL and Go all use CRT for private-key operations, with blinding ( before decrypting, multiply by after) to defeat timing attacks.
- Hardware security modules and smart cards add fault detection, since physical glitching attacks on them are practical.
Exercises
Exercise 1: CRT by hand
With , , , decrypt using CRT.
Answer
.
Solution
, . . (). . . So .
Exercise 2: Speed-up factor
Why is the CRT speed-up about 4 and not 2?
Answer
Each half has half the exponent bits and a quarter of the multiplication cost.
Solution
A multiplication of -bit numbers costs about a quarter of a -bit one (schoolbook cost grows like ), and the exponent is half as long. So each half costs , both halves .
Exercise 3: Guard against faults (code)
Modify the faulty-signature code to verify before releasing, and print what is released.
Answer
The faulty signature is caught and nothing is released.
Solution
from math import lcm
p, q, e = 1000003, 1000033, 65537
n = p * q
d = pow(e, -1, lcm(p - 1, q - 1))
def sign(m, fault=False):
s1 = pow(m, d % (p - 1), p) ^ (1 if fault else 0)
s2 = pow(m, d % (q - 1), q)
s = s2 + (pow(q, -1, p) * (s1 - s2) % p) * q
return s if pow(s, e, n) == m else None
print(sign(123456789), sign(123456789, fault=True))497552266575 None
Test yourself
RSA-CRT decryption computes…
- directly
- and , then recombines
The CRT speed-up for RSA decryption is about…
- 2x
- 4x
- 16x
- none
How do implementations stop the Bellcore fault attack?
- by using a larger
- by verifying the signature with the public key before output
- by not using CRT for encryption
- by hashing twice