Lost Modulus

Attachmentscrypto - 23042 solves

I lost my modulus. Can you find it for me?

Time

2 hours
First blood

Behavior

The server implement paillier cryptosystem. It gives us a encrypted flag at start.

There are two operations, A and B, Operation A return the ciphertext of our input. Operation B gives us the last byte of plaintext. We can run atmost 2048 operations.

Unfortunately, we know nothing about n,p,q,gn, p, q, g.

Solution

TL;DR

  1. Leak 8 ~ 1024 bits of n bit-by-bit (1008 OPs).
  2. Last byte is -ret (0 OPs).
  3. Leak 16 bytes of flag (16 OPs).
  4. Repeat with a new connection.

We can calculate m mod  nm\ \mod n by encrypting it and then decrypting, If m<nm < n, we get the last byte of $m$. However, if m≥nm \ge n, we get the last byte of m mod  nm\ \mod n. We can leak nn bit-by-bit by selecting 2b2^b as mm. Moreover, the return value is −n mod  256-n \ \mod 256 if m≥nm \ge n, which means we have 16 operations left for leaking 16 bytes of flag.

For example, Let n:=233∗263=61279=0b01110⋯n \coloneqq 233 * 263 = 61279 = 0b01110 \cdots 216 mod  n=161215 mod  n=0215+214 mod  n=0215+214+213 mod  n=0215+214+213+212 mod  n=161⋯−nmod  256=161\begin{aligned} 2^{16}\ \mod n &= 161 \\ 2^{15}\ \mod n &= 0 \\ 2^{15} + 2^{14}\ \mod n &= 0 \\ 2^{15} + 2^{14} + 2^{13}\ \mod n &= 0 \\ 2^{15} + 2^{14} + 2^{13} + 2^{12}\ \mod n &= 161 \\ & \cdots \\ -n \mod 256 &= 161 \\ \end{aligned}

To decrypt the flag, we can use the homomorphic property of paillier: c1×c2 mod  n2=Enc(m1+m2 mod  n)c1m2 mod  n2=Enc(m1∗m2 mod  n)\begin{aligned} c_1 \times c_2\ \mod n^2 &= \text{Enc}(m_1 + m_2\ \mod n) \\ c_1^{m_2}\ \mod n^2 &= \text{Enc}(m_1 * m_2\ \mod n) \\ \end{aligned} Decrypt it with following recursive equation: ct=(ct−1/Enc(mt−1))1/256 mod  n2mt=Dec(ct) mod  256\begin{aligned} c_t &= (c_{t-1} / \text{Enc}(m_{t-1}))^{1/256}\ \mod n^2 \\ m_t &= \text{Dec}(c_t)\ \mod 256 \\ \end{aligned}