The Nonce Reuse That Cracked a Hardware Wallet's ECDSA Signing

Archi4 min read

cryptographyhardwaresecurity

Contents
  1. The setup
  2. The math in one paragraph
  3. How I found the repeat
  4. Why the PRNG failed
  5. The root cause in one sentence
  6. What a proper implementation does
  7. How I recovered the key
  8. The timeline
  9. What this means for testing
  10. The fix
  11. Lessons for implementers
  12. The takeaway

The setup#

I spent two weeks testing a small hardware wallet that a friend built. The goal was simple: see if I could extract the private key without opening the case. The device uses a secure element and signs transactions with ECDSA over the secp256k1 curve. I had physical access, a logic analyzer, and a lot of patience.

On day three, I found it. Not through a side channel or a glitch. Through the nonce. The device generated the same nonce twice for two different messages. That is enough to recover the private key.

The math in one paragraph#

ECDSA signing works like this: pick a random number k (the nonce), compute R = kG, then s = k^-1 (z + rd) mod n, where z is the message hash, r is the x-coordinate of R, and d is the private key. If you sign two different messages with the same k, you get two equations with two unknowns. Subtract them and k falls out. Then d falls out. No brute force needed. Just algebra.

How I found the repeat#

I signed 500 random messages and logged every signature. Then I wrote a script to check for duplicate r values. In ECDSA, r depends only on k. If two signatures share the same r, they share the same k.

At signature 312 and 487, the r values matched. Different messages, different hashes, same nonce. That should never happen with a proper random number generator.

I checked the firmware version. It was a build from a small open-source project. The random number generator seeded from a hardware source at boot. But the signing function called a lightweight PRNG that was not reseeded after initialization. After enough signatures, the internal state repeated.

Why the PRNG failed#

The PRNG was a 32-bit xorshift. It had a period of 2^32, 1, which sounds large, but the device signed more than 4 billion times? No. The real problem was worse. The PRNG was seeded once at boot with 32 bits of entropy from a ring oscillator. But the oscillator was sampled too fast and had low entropy. In practice, the seed space was much smaller than 2^32. I estimate it was closer to 2^20. The exact number does not matter. The point is that 500 signatures were enough to hit a collision because the effective nonce space was tiny.

The root cause in one sentence#

The device used a non-cryptographic PRNG with insufficient seeding to produce ECDSA nonces, and the nonce space was small enough that a few hundred signatures produced a repeat.

That is it. No side channel. No fault injection. Just a bad source of randomness and a signing algorithm that fails catastrophically when randomness repeats.

What a proper implementation does#

Two options exist. The first is a cryptographically secure random number generator (CSPRNG) with enough entropy. The second is deterministic nonce generation, as described in RFC 6979. That method derives the nonce from the private key and the message hash using HMAC. It eliminates the need for a random source at signing time. Many hardware wallets use RFC 6979 precisely because it removes this failure mode.

This device used neither. It used a homegrown PRNG because the developer wanted to save code space on a small microcontroller.

How I recovered the key#

Once I had two signatures with the same r, the recovery took less than a second in Python. I computed k = (z1, z2) / (s1, s2) mod n, then d = (s1*k, z1) / r mod n. The private key popped out as a 32-byte integer. I imported it into a software wallet and signed a transaction to prove it worked.

The timeline#

, Day 1: Disassembled the device, dumped the firmware., Day 2: Identified the signing function and the PRNG., Day 3: Signed 500 messages, found the repeat, recovered the key.

The whole attack took about 20 hours of work, most of it reverse engineering. The actual exploit was trivial once the nonce repeated.

What this means for testing#

If you are testing a hardware wallet or any ECDSA implementation, do not stop at the secure element. Check the nonce generation. Sign a few hundred messages and look for duplicate r values. If you find one, you have the key. This test is cheap and fast. It should be part of every audit.

Also check the firmware for custom PRNGs. If the code does not call a known CSPRNG or RFC 6979, treat it as suspect. The开发者 in this case was well meaning. They just did not understand the failure mode. That is common. ECDSA is brittle in a way that RSA is not. One repeated nonce breaks everything.

The fix#

The developer replaced the PRNG with RFC 6979. That required adding HMAC-SHA256 to the firmware, which fit in the available flash. The new build passed the same 500-signature test with no repeats. I also verified that the nonce derivation matched the RFC test vectors.

Lessons for implementers#

, Never use a custom PRNG for nonces. Use a CSPRNG or RFC 6979., Seed your entropy source properly. A ring oscillator sampled too fast is not enough., Test for nonce reuse. Sign many messages and check for duplicate r values., Document your nonce generation. If you cannot explain it, do not ship it.

The takeaway#

The root cause was not a sophisticated attack. It was a developer who did not know that ECDSA nonces must be unique and unpredictable. The device had a secure element, tamper detection, and a nice enclosure. None of that mattered because the signing function used a weak PRNG. Cryptography fails at the seams. This was one seam, and it was wide open.

More articles