Home
Writeups Misc About
No Random, No Bias

No Random, No Bias

The vulnerability of the code (and yes, as somewhat hinted at by the challenge description), is these two lines:

sha1 produces a digest of only 160 bits (20 bytes). This is a big problem as it is required that the nonce is a number randomly generated in the range between 1 and the order of the elliptic curve. In the above code, the hash generated by sha1 is only 160 bits long. This short of a nonce will lead to a biased nonce attack, which essentially is a lattice attack against weak ECDSA signatures.

I would not dive into the math here, as this original paper, and the corresponding presentation video by Nadia Heninger should explain how this attack works and the real-life implications.

My implementation is based on the paper, combined with this repo from Github, and the Trail Of Bits article covering some of the flaws in ECDSA signature implementations. Kudos to zenobia on Cryptohack for shedding some new perspectives on how to implement the attack, as I was having troubles with the Github script I found, and also the Trail Of Bits article.

Again, as covered in the video by Nadia, with 3 messages, nonces under 170 bits will leak information about the private key. The length of sha1 hash is only 160 bits, which is surely in this confidence range. If we find the correct private key d, then Gd, where G is the given generator, will return back the public key point given.

With the private key figured out, recovering the flag should be a trivial task.

Sage Implementation: