CCCamp CTF 2023  Baby RSA
This is the solution of the challenge “Baby RSA” from CCCamp 2023. It was a RSA whitebox implementation in Python with a left to right windowed exponentiation.
Description
I stumbled across this weird RSA implementation. However, it seems like it still works correctly. Can you figure out how? The flag is contained in one of the prime factors of N!
Challenge Files: babyrsa.zip
Details
Category: Crypto
Difficulty: Medium
Author: floesen
Solution
The implementation is quite compact. The encryption function is a standard RSA encryption with $N$ being 4094 bits and $e=65537$. We did not have any ciphertext nor plaintext since we could compute them ourselves. It directly indicated that the private key is encoded in the rsa_decrypt
function. The goal was clearly to recover the private exponent $d$ or to factor $N$. In the first part, we have a binomial expansion and thus, $s$ is a vector containing $s[i] = m_1^i (b+ac^h)^i$ for $i = 0,..,32$. Note that the computations do not happen on the ciphertext $c$ but on $c^h$. Then we have a linear combination of the vector $s$ with the matrix $M$ and a correction term to eliminate the term in degree 32. To convince myself that it is a cancelling term I computed:


Finally, the last part was hard to guess but I could understand it is a left to right kary exponentiation with window of 5 bits: I figure out that by seeing that $l$ is 819 elements long containing value on 5 bits wich make a total of $5\times 819 = 4095$ bits. Another clue is that the product is computed with a serie of power of 32 which is $2^5$. The final computation can be written as: $$m = m_2 \prod_i u[l[819i1]]^{32^i}$$ However, because of the linear step with matrix $M$ we cannot recover directly the bits of the private exponent bits of $d$ directly in $l$. Basically during the CTF I was stuck there.
Afterwards, a message in the Discord channel gave me the solution. The idea is to use symbolic computation of the first step of the precomputation in sage to guess how the bits are mixed but using an unknown $x=c^h$ to avoid complexity explosion during computation. That’s what I did:


It works, the coponements of u
where only monomial on $x$ meaning that the depends only on a single power of $x$:


So we know that u
is simply a substitution of the precomputed values and we can reconstruct the private exponent bits in the correct order:


Note that we must mutiply the result by h
to have the correct value of $d$ since we substitue it in $x$. Now we can check everything is correct:


We have to compute the factorization of $N$ from $d$ with a well known method. With my full script solve.sage I got the flag:

