1. introduction

There’s this great paper by Dan Boneh from 1998 about the RSA cryptosystem and its weaknesses. I found this paper to be a particularly interesting read (and interestingly enough, it’s been 20 years since that paper!), so here I’m going to reiterate some of the attacks described in the paper, but using examples with numbers in them. (Also please excuse the lack of proper formatting, I’ve yet to figure out how to get Gutenberg to accept Latex)

That being said, I am going to skip over the primer of how the RSA cryptosystem works, since there’s already a great number of resources on how to do that.

1.1 factoring large integers

Obviously this is a pretty bruteforce-ish way to crack the cryptosystem, and probably won’t work in time for you to see the result, but can still be considered an attack vector. This trick works by just factoring the modulus, N. With N, finding the private exponent d from the public exponent e is a piece of cake.

Let’s choose some small numbers to demonstrate this one (you can follow along in a Python REPL if you want):

>>> N = 881653369
>>> e = 17
>>> c = 875978376

N is clearly factorable in this case, and we can use resources like msieve or factordb to find smaller primes in this case. Since we know now that N = 20717 * 42557, we can find the totient of N:

>>> p = 20717
>>> q = 42557
>>> tot = (p - 1) * (q - 1)

Now all that’s left is to discover the private exponent and solve for the original message! (you can find the modular inverse function I used here)

>>> d = modinv(e, tot)
>>> pow(c, d, N)

And that’s it! Now let’s look at some more sophisticated attacks…

2. elementary attacks

These attacks are related to the misuse of the RSA system. (if you can’t tell, I’m mirroring the document structure of the original paper)

2.1 common modulus

My cryptography professor gave this example as well. Suppose there was a setup in which the modulus was reused, maybe for convenience (although I suppose with libraries today, it’d actually be more inconvenient to reuse the key). Key pairs would be issued to different users and they would share public keys with each other and keep private keys to themselves.

The problem here is if you have a key pair, and you got someone else’s public key, you could easily derive the private key by just factoring the modulus. Let’s see how this works with a real example now.

Since this is a big problem if you were to really use this cryptosystem, I’ll be using actual keys from an actual crypto library instead of the small numbers like in the first example to show that this works on 2048-bit RSA. The library is called PyCrypto, and if you’re planning on doing anything related to crypto with Python, it’s a good tool to have with you. For now, I’m going to generate a 2048-bit key (by the way, in practice you probably shouldn’t be using 2048-bit keys anymore, I’m just trying to spare my computer here).

>>> from Crypto.PublicKey import RSA
>>> k1 = RSA.generate(2048)
<_RSAobj @0x7f3d3226dfd0 n(2048),e,d,p,q,u,private>

Now, normally when you generate a new key, it’d generate a new modulus. For the sake of this common modulus attack, we’ll force the new key to use the same modulus. This also means we’ll have to choose an exponent e other than the default choice of 65537 (see this link for documentation):

>>> N = k1.p * k1.q
>>> e = k1.e
>>> d = k1.d
>>> e2 = 65539
>>> d2 = modinv(e2, (k1.p - 1) * (k1.q - 1))
>>> k2 = RSA.construct((N, e2, d2))
<_RSAobj @0x7f3d31c7c5f8 n(2048),e,d,p,q,u,private>

Ok, now we have two keys, k1 and k2. Now I’ll show how using only the public and private key of k1 (assuming this is the pair that we got legitimately from the crypto operator), and the public key of k2, which is tied to the same modulus, we can find the private key of k2.

To do this, we’ll try to find the roots of the equation f(x) = x^2 - (p + q)x + pq. You’ll find that for values of p and q, this will produce f(p) = p^2 - p^2 - qp + pq, and f(q) = q^2 - pq - q^2 + pq. We know that N = pq. How can we find p + q? Since phi(N) = (p - 1)(q - 1) = pq - p - q + 1, we can find that phi(N) = N - (p + q) + 1, so p + q = N - phi(N) + 1. Now we need to use e and d to estimate phi(N). Recall that ed = 1 mod phi(N). This is equivalent to saying ed = 1 + k*phi(N). Then (ed - 1) / phi(N) = k.

It turns out that k is extremely close to ed/N: ed/N = (1 + k*phi(N)) / N = 1/N + k*phi(N)/N. 1/N is basically 0, and phi(N) is very close to N, so it shouldn’t change the value of k by very much. We now use ed/N to estimate k: phi(N) = (ed - 1) / (ed / N).

>>> from decimal import Decimal, getcontext
>>> getcontext().prec = 1000
>>> k = round(Decimal(e) * Decimal(d) / Decimal(N))
>>> phi = (Decimal(e) * Decimal(d) - 1) / Decimal(k)

Then we can get p + q through the formula mentioend above:

>>> B = Decimal(N) - phi + 1
>>> C = Decimal(N)

Check to make sure B and C are integers. If they’re not, try using a higher precision in getcontext().prec. Now solve the quadratic equation:

>>> p = (B + (B * B - 4 * C).sqrt()) / Decimal(2)
>>> q = (B - (B * B - 4 * C).sqrt()) / Decimal(2)
>>> p * q == N

We’ve successfully recovered p and q from just N, e, and d!

2.2 blinding

This attack is actually about RSA signatures (which uses the opposite keys as encryption: private for signing and public for verifying), and shows how you can compute the signature of a message M using the signature of a derived message M'.

Suppose Marvin wants Bob to sign the following message: "I (Bob) owes Marvin $100,000 USD". Marvin hands this to Bob saying something like, “I’ll just need you to sign this with your private key.” Let’s generate Bob’s private key:

>>> from Crypto.Util.number import bytes_to_long, long_to_bytes
>>> from Crypto.PublicKey import RSA
>>> bob = RSA.generate(2048)
<_RSAobj @0x7f4309521128 n(2048),e,d,p,q,u,private>
>>> M = b"I (Bob) owes Marvin $100,000 USD"

Obviously, Bob, an intellectual, will refuse to sign the message. However, suppose Marvin now transforms his message into a more innocent looking one. He does this by turning M into M' = (r^e)*M mod N where r is an integer that’s coprime to N:

>>> from random import randint
>>> N = bob.p * bob.q # this is publicly available knowledge
>>> r = 19
>>> Mp = long_to_bytes((pow(r, bob.e, N) * bytes_to_long(M)) % N)

Now he asks Bob to sign this more… innocently-looking message. Without questioning, Bob, an intellectual, signs his life away. Let’s say he produces a signature S' = (M'^d) = (r^e * M)^d = r^(ed) * M^d = r * M^d mod N.

>>> Sp, = bob.sign(Mp, 0)

Now, all Marvin has to do is multiply by the modular inverse of r, to obtain M^d, the signature of the original message:

>>> S = (Sp * modinv(r, N)) % N

Sure enough, if you try to verify the “original” signature against the original message, it checks out.

>>> bob.verify(M, (S,))

Marvin has now successfully tricked Bob into signing his life away.

This post is a work in progress.. I’ll update it as I add more.