LostKey
LostKey
Explanation
The provided code mixes elliptic curve operations with AES encryption. The goal is to figure out how the secret flag was encrypted and how we can recover it.
The script works as follows:
- An elliptic curve class
ECis defined with some unusual parameters. It has:- A point addition function
add - A point multiplication function
mul(scalar multiply)
- A point addition function
- A base point
Gis given explicitly with fixedxandy. - A secret scalar
n(smaller than 3.8e25, so it’s relatively small compared to the large prime field) is used to computeGn = n * G. - The
encryptfunction then takes this secret integernand uses it to derive an AES key:- It converts
ninto a string - Hashes it with SHA1
- Truncates to 16 bytes
- Uses that as the AES key in CBC mode
- It converts
- Finally, it encrypts the
flagwith that key and outputs the ciphertext and IV.
The challenge output looks something like this:
1
2
3
4
G = coord(<x>, <y>)
Gn = <some big x coordinate>
Ciphertext: <hex>
IV: <hex>
What’s Going On
Gis the generator.nis the secret scalar.- The public value is
Gn(but they only reveal the x-coordinate). - The AES key is derived directly from the integer
n.
So the “hard problem” here is supposed to be: given G and Gn, find n. That’s basically the Elliptic Curve Discrete Logarithm Problem (ECDLP). Normally, this is very hard.
Why It’s Broken
The code includes an assertion:
1
assert(n < 38685626227668133590597631)
That number is about 2^85. In other words, n is at most 85 bits long. That’s tiny in cryptographic terms.
This makes the discrete log problem completely solvable with a brute-force or baby-step giant-step algorithm, because the search space is only about 2^85 (feasible with optimized algorithms and enough compute).
Exploitation Strategy
- From the challenge output, we know
Gand the x-coordinate ofGn. We need to recovern. - Use a discrete log algorithm (baby-step giant-step, Pollard’s Rho for ECDLP, or even direct brute-force if
nis small enough) to solve forn. - Once we have
n, we derive the AES key exactly like the script:- Convert
nto string - Hash with SHA1
- Take the first 16 bytes
- Convert
- Decrypt the ciphertext with AES-CBC using that key and the given IV.
- The plaintext will be the flag.
This post is licensed under CC BY 4.0 by the author.