Skip to content

RSA

RSA is an asymetric cryptographic algorithm. A public key is used to encrypt data and a private key is used to decrypt data.

Textbook definition

The variables of textbook RSA are:

VariableDescription
NNThe product of two large primes
eeThe public exponent
ddThe private exponent

The public key is (N, e) and the private key is (N, d).

Key generation

  1. Choose two large primes pp and qq. Use a cryptographically secure random number generator.

  2. Compute the public modulus:

    N=pqN = p q
  3. Compute the “private” modulus:

    Φ(N)=(p1)(q1)\Phi(N) = (p - 1) (q - 1)
  4. Choose an integer ee such that

    1<e<Φ(N) and gcd(e,Φ(N))=11 < e < \Phi(N) \text{ and } \gcd(e, \Phi(N)) = 1


    Usually e=65537=0x10001e = 65537 = 0\text{x}10001.

  5. Compute dd such that de=1modΦ(N)de = 1 \mod \Phi(N)

    d=e1modΦ(N)d = e^-1 \mod \Phi(N)

    (for exemple with the Extended Euclidean algorithm)

Encryption (Textbook RSA)

To encrypt a message mm with the public key (N,e)(N, e), compute the ciphertext cc with:

c=memodNc = m^e \mod N

Decryption (Textbook RSA)

To decrypt a ciphertext cc with the private key (N,d)(N, d), compute m=cdmodNm = c^d \mod N.

m is the deciphered message.

Attacks

Several attacks exist on RSA depending on the circumstances.

  • RSA CTF Tool - GitHub

    Performs several attacks on RSA keys. Very useful for CTFs.

  • Known factors in databases

    Services such as FactorDB or Alpertron’s calculator provide a database of known factors. If you can find a factor of NN, you can compute pp and qq then dd.

  • RSA Fixed Point - StackExchange

    These challenges can be spotted when the input is not changed with encrypted/decrypted.

    There are 6 non-trivial fixed points in RSA encryption that are always there, caracterized by mm mod p{0,1,1}p \in \lbrace 0, 1, -1 \rbrace and mm mod q{0,1,1}q \in \lbrace 0, 1, -1 \rbrace .

    It is possible to deduce one of the prime factors of nn from the fixed point, since gcd(m1,n), gcd(m,n), gcd(m+1,n)\text{gcd}(m−1,n),\ \text{gcd}(m,n),\ \text{gcd}(m+1,n) are 1,p,q1, p, q in a different order depending on the values of mm mod pp and mm mod qq.

    However, it is also possible to find other fixed points that are not the 6 non-trivial ones. See this cryptohack challenge for writeups on how to deduce the prime factors of nn from these fixed points.

  • Decipher or signing oracle with blacklist

    A decipher oracle can not control the message that it decrypts. If it blocks the decryption of cipher cc, you can pass it cremodnc * r^e \mod n where rr is any number. It will then return

    (cre)d=cdr=mrmodn(c * r^e)^d = c^d * r = m * r \mod n

    You can then compute m=cdm = c^d by dividing by rr.

    This also applies to a signing oracle with a blacklist.

  • Bleichenbacher’s attack on PKCS#1 v1.5

    When the message is padded with PKCS#1 v1.5 and a padding oracle output an error when the decrypted ciphertext is not padded, it is possible to perform a Bleichenbacher attack (BB98). See this github script for an implementation of the attack.

    This attack is also known as the million message attack, as it require a lot of oracle queries.

  • Finding primes pp and qq from d

    This algorithm can be used to find pp and qq from (N,e)(N, e) and the private key dd

  • Coppersmith’s attack - Wikipedia

Bad parameters attacks

  • Wiener’s Attack - Wikipedia with continued fractions

    When ee is very large, that means dd is small and the system can be vulnerable to the Wiener’s attack. See this script for an implementation of the attack.

    This type of attack on small private exponents was improved by Boneh and Durfee. See this repository for an implementation of the attack.

  • Small ee, usually 3 in textbook RSA - StackExchange

    When ee is so small that c=me<Nc = m^e < N, you can compute mm with a regular root: m=cem = \sqrt[e]{c}.

    If ee is a bit larger, but still so small that c=me<kNc = m^e < kN for some small kk, you can compute mm with a kk-th root: m=c+kNem = \sqrt[e]{c + kN}.

    See this script for an implementation of the attack.

  • Many primes in the public modulus - CryptoHack

    When NN is the product of many primes (~30), it can be easily factored with the Elliptic Curve Method.

    See this script for an implementation of the attack.

  • Square-free 4p - 1 factorization and it’s RSA backdoor viability - Paper

    Square-free number

    If we have

    {N=pqT=4p1T=Ds2D=3mod8\begin{cases} N &= p \cdot q \\\\ T &= 4 \cdot p - 1 \\\\ T &= D \cdot s^2 \\\\ D &= 3 \mod 8 \end{cases}

    then DD is a square-free number and NN can be factored.

    See this GitHub repository for an implementation of the attack.

  • Fermat’s factorisation method - Wikipedia

    If the primes pp and qq are close to each other, it is possible to find them with Fermat’s factorisation method. See this script for an implementation of the attack.

  • ROCA vulnerability - Wikipedia

    The “Return of Coppersmith’s attack” vulnerability occurs when generated primes are in the form

    p=kM+(65537amodM)p = k * M * + (65537^a \mod M)

    where MM is the product of nn successive primes and nn.

    See this GitHub gist for an explaination of the attack.

    See this Gitlab repository for an implementation of the attack.

Bad implementations attacks

  • Håstad’s broadcast attack (Chinese Remainder Theorem) - Wikipedia

    When the same message mm is encrypted with the same small public exponent ee under multiple moduli N1,N2,,NkN_1, N_2, \dots, N_k (giving c1,c2,,ckc_1, c_2, \dots, c_k), you can recover mm as soon as kek \geq e.

    The Chinese Remainder Theorem gives a value cc such that c=memodN1N2Nkc = m^e \mod N_1 N_2 \cdots N_k. Since m<Nim < N_i for every ii, we have me<N1N2Nkm^e < N_1 N_2 \cdots N_k, so the modular reduction never happened and c=mec = m^e over the integers. The plaintext is then a plain integer ee-th root:

    m=cem = \sqrt[e]{c}

    If the messages are an affine function of each other (linear padding) rather than identical, the more general Håstad attack based on Coppersmith’s method still applies. See this script for an implementation of the identical-message case.

  • Multiple Recipients of the same message

    When there are multiple public exponents e1,e2e_1, e_2 for multiple c1,c2c_1, c_2 and the same moduli NN, you can use Bezout’s identity to compute mm.

    Using Bezout’s algorithm, you can find aa and bb such that ae1+be2=1a e_1 + b e_2 = 1. Then you can compute mm with:

    c1ac2b=mae1mbe2=mae1+be2=m1=mmodNc_1^a c_2^b = m^{a e_1} m^{b e_2} = m^{a e_1 + b e_2} = m^1 = m \mod N
  • Franklin-Reiter related-message attack

    When two messages are encrypted using the same key (e,N)(e, N) and one is a polynomial function of the other, it is possible to decipher the messages.

    A special case of this is when a message is encrypted two times with linear padding : c=(am+b)emodNc = (a*m +b)^e \mod N.

    See this GitHub repository for an explaination of the attack.

    See this script for an implementation of the attack.

  • Signature that only check for the last few bytes - CryptoHack

    When a signature is only checking the last few bytes, you can add 28n2^{8 * n} to the message and the signature will still be valid, where nn is the number of bytes checked. Consequently, finding the ee-th root of the signature will be easier. Check writeups of the cryptohack challenge for more details.