TExcelTAD
Home › Number Theory & Cryptography › RSA Encryption, Decryption and CRT Speed-Up

RSA Encryption, Decryption and CRT Speed-Up

RSA's public operation me mod nm^e \bmod n is fast because ee is tiny. The private operation cd mod nc^d \bmod n uses a full-size exponent and is the bottleneck. Working modulo pp and qq 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 0≤m<n0 \le m < n. 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

dP=d mod (p−1),dQ=d mod (q−1),qinv=q−1 mod pd_P = d \bmod (p - 1), \qquad d_Q = d \bmod (q - 1), \qquad q_{\text{inv}} = q^{-1} \bmod p

Then decrypt with two half-size exponentiations and Garner's recombination:

m1=cdP mod p,m2=cdQ mod qh=qinv(m1−m2) mod pm=m2+hq\begin{aligned} m_1 &= c^{d_P} \bmod p, \qquad m_2 = c^{d_Q} \bmod q \\ h &= q_{\text{inv}} (m_1 - m_2) \bmod p \\ m &= m_2 + h q \end{aligned}

Why it works: m≡cd(modp)m \equiv c^d \pmod p, and by Fermat the exponent can be reduced modulo p−1p - 1. Each half-size exponentiation costs about 1/81/8 of a full one (half the exponent bits, a quarter of the cost per multiplication), so two of them cost about 1/41/4.

python · try it yourself
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 kk-bit exponent and a bb-bit modulus is about 1.5k1.5k multiplications of cost b2b^2 each. Comparing:

python · try it yourself
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 s=mds = m^d, the faulty s′s' is still correct modulo qq but wrong modulo pp. Then

gcd⁡(s′e−m, n)=q\gcd(s'^e - m,\ n) = q

and a single faulty signature factors the key (Boneh, DeMillo and Lipton, 1997).

python · try it yourself
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 (se≡ms^e \equiv m) before releasing it. Every serious RSA implementation does so.

In software

  • OpenSSL, BoringSSL and Go all use CRT for private-key operations, with blinding (c⋅rec \cdot r^e before decrypting, multiply by r−1r^{-1} 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 p=5p = 5, q=11q = 11, d=27d = 27, decrypt c=14c = 14 using CRT.

Answer

m=9m = 9.

Solution

dP=27 mod 4=3d_P = 27 \bmod 4 = 3, dQ=27 mod 10=7d_Q = 27 \bmod 10 = 7. m1=143 mod 5=43=64≡4m_1 = 14^3 \bmod 5 = 4^3 = 64 \equiv 4. m2=147 mod 11=37=2187≡9m_2 = 14^7 \bmod 11 = 3^7 = 2187 \equiv 9 (2187=198⋅11+92187 = 198 \cdot 11 + 9). qinv=11−1 mod 5=1q_{\text{inv}} = 11^{-1} \bmod 5 = 1. h=1⋅(4−9) mod 5=0h = 1 \cdot (4 - 9) \bmod 5 = 0. So m=9+0⋅11=9m = 9 + 0 \cdot 11 = 9.

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 b/2b/2-bit numbers costs about a quarter of a bb-bit one (schoolbook cost grows like b2b^2), and the exponent is half as long. So each half costs 1/81/8, both halves 1/41/4.

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
python · try it yourself
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…

  1. cd mod nc^d \bmod n directly
  2. cdP mod pc^{d_P} \bmod p and cdQ mod qc^{d_Q} \bmod q, then recombines
  3. ce mod nc^e \bmod n
  4. gcd⁡(c,n)\gcd(c, n)

The CRT speed-up for RSA decryption is about…

  1. 2x
  2. 4x
  3. 16x
  4. none

How do implementations stop the Bellcore fault attack?

  1. by using a larger ee
  2. by verifying the signature with the public key before output
  3. by not using CRT for encryption
  4. by hashing twice