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:
| Variable | Description |
|---|---|
| The product of two large primes | |
| The public exponent | |
| The private exponent |
The public key is (N, e) and the private key is (N, d).
Key generation
Choose two large primes and . Use a cryptographically secure random number generator.
Compute the public modulus:
Compute the “private” modulus:
Choose an integer such that
Usually .
Compute such that
(for exemple with the Extended Euclidean algorithm)
Encryption (Textbook RSA)
To encrypt a message with the public key , compute the ciphertext with:
Decryption (Textbook RSA)
To decrypt a ciphertext with the private key , compute .
m is the deciphered message.
Attacks
Several attacks exist on RSA depending on the circumstances.
RSA CTF Tool- GitHubPerforms 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 , you can compute and then .
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 mod and mod .
It is possible to deduce one of the prime factors of from the fixed point, since are in a different order depending on the values of mod and mod .
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 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 , you can pass it where is any number. It will then return
You can then compute by dividing by .
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 and from d
This algorithm can be used to find and from and the private key
Coppersmith’s attack - Wikipedia
Bad parameters attacks
Wiener’s Attack - Wikipedia with continued fractions
When is very large, that means 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 , usually 3 in textbook RSA - StackExchange
When is so small that , you can compute with a regular root: .
If is a bit larger, but still so small that for some small , you can compute with a -th root: .
See this script for an implementation of the attack.
Many primes in the public modulus - CryptoHack
When 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 numberIf we have
then is a square-free number and can be factored.
See this GitHub repository for an implementation of the attack.
Fermat’s factorisation method - Wikipedia
If the primes and 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
where is the product of successive primes and .
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 is encrypted with the same small public exponent under multiple moduli (giving ), you can recover as soon as .
The Chinese Remainder Theorem gives a value such that . Since for every , we have , so the modular reduction never happened and over the integers. The plaintext is then a plain integer -th root:
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 for multiple and the same moduli , you can use Bezout’s identity to compute .
Using Bezout’s algorithm, you can find and such that . Then you can compute with:
Franklin-Reiter related-message attack
When two messages are encrypted using the same key 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 : .
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 to the message and the signature will still be valid, where is the number of bytes checked. Consequently, finding the -th root of the signature will be easier. Check writeups of the cryptohack challenge for more details.