We are given a server that acts as an oracle doing elliptic curve scalar point multiplication (ECSPM) with a given point. Using this oracle we must solve the elliptic curve discrete logarithm problem (ECDLP) for a constant $k$, which is our flag.

## Finding the Vulnerability

Checking out the source code we are given the coefficients $a$ and $b$, as well as the finite field $\mathbb{F_p}$ the elliptic curve is defined over.

If this curve looks a little suspicious it’s because it happens to be BrainpoolP256t1, a somewhat-rigid twisted curve, which has some overlap in its coefficients:

A = 0x7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9
B = 0x26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6

Unfortunately while certainly suspicious it doesn’t seem to make the curve cryptographically insecure.

(Technically this curve has a cryptographically low cost ($2^{44.5}$) for a combined attack using low order points and could also probably be exploited, but there is an easier and less expensive method.)

With no inherent weakness in the curve itself we need to check for insecurities elsewhere.

Reading through the code we can find that it does the ECSPM with custom functions multiply(), double(), and add(), and that they were used in the function handle():

Reading through we can see that the script takes a point $G$ and using the curve $\mathbb{E}\colon\quad y^2 = x^3 + Ax^2 + B$ where $A,B\in\mathbb{F_p}$ computes and returns $G' = kG$. Checking the server’s math functions we can see that mathematically they work, but they don’t use the point $B$ in their calculations! This is the standard for short Weierstrass curves like the one the server is using, however since the server also neglets to verify $G\in{E(\mathbb{F_p}})$ or $G'\in{E(\mathbb{F_p}})$ the server is vulnerable to an invalid-curve attack which we can use to recover $k$.

## The Vulnerability

Since the server doesn’t use $B$ in its calculations we can define a new curve using a coefficient $C$ instead of $B$ where $C\ne{B}$ in our short Weierstrass equation from earlier. Let’s call this new curve $\mathbb{E'}$.

Choosing $C$ such that this new curve’s order has a small prime divisor $r$ will ensure there exists a subgroup of $\mathbb{E'}$ of small order $r$. We can then send the server a point of this subgroup, and since neither $C$ nor $B$ is used by the server the ECSPM will actually occur over our new curve $\mathbb{E'}$. As the discrete logarithm is now in the subgroup of $\vert{r}\rvert$ generated by $G\in{\mathbb{E'}}$, the result will only have $r$ possible values! Since there is such a small keyspace and the multiplicative group’s order is a smooth integer, we can then easily solve the ECDLP using the Pohlig-Hellman algorithm. This will leak $k \mod{r}$. Repeating this for new curves and new $r$ values will create a system of linear congruences which can be solved with the Chinese Remainder Theorem, thus yielding the final value of $k$.

## Implementing and Executing the Vulnerability

Now comes the fun part, writing all this up in a Sage Script. Sage is crucial here as it will do all of the ECC work for us, as well as letting us communicate with the server and do logic in Python. Technically a script isn’t necessary as we could do everything by hand in the Sage terminal, however a script saves time and lowers the likelihood of error. This is especially useful since we will need enough linear congruences to find the correct value of $k$.

##The Code: Note: The server seems to not like points that are generated from primes that are too small or result in points that are too large. As far as I can tell this is a bug on their end.

Running this with a few values I arrived at:

We can now solve for $k$ in sage:

Giving our final value for $k$:

54815471905492393585931182194968733201189578373199788873886877824626330137714

Encoding this integer as hex and decoding that hex as ascii reverses the encoding shown in the redacted sever’s code:

We arrive at our final flag:

y0u're_th3_3llipt1c_curv3_mas7tr