TExcelTAD
Home › Applied Cryptography: Ciphers, Hashes, Signatures, PKI & TLS › Elliptic Curves, ECDSA and the Nonce-Reuse Failure

Elliptic Curves, ECDSA and the Nonce-Reuse Failure

In December 2010 a group called fail0verflow showed at the Chaos Communication Congress that Sony had signed every PlayStation 3 software update with the same ECDSA "random" number. From two signatures they computed Sony's private signing key with high-school algebra, and anyone could then sign code the console would trust. Elliptic-curve signatures are short, fast and strong, but ECDSA has this one sharp edge, and it has cut Bitcoin wallets, Android apps and hardware tokens too.

Elliptic curves in one page

An elliptic curve over a prime field is the set of points (x,y)(x, y) with

y2=x3+ax+b(modp)y^2 = x^3 + ax + b \pmod p

plus a "point at infinity". Points can be added with a geometric rule (draw the line through two points, take the third intersection, reflect), and adding a point GG to itself kk times gives the scalar multiple kGkG. Computing Q=kGQ = kG from kk is fast (double-and-add, about 256 steps for a 256-bit kk). Recovering kk from QQ and GG, the elliptic-curve discrete logarithm problem, has no known algorithm much faster than about n\sqrt{n} steps for a group of order nn. That is why a 256-bit curve gives about 128-bit security while RSA needs 3,072 bits for the same. The algebra is developed in Elliptic Curves and Scalar Multiplication.

A key pair is a random scalar dd (private) and the point Q=dGQ = dG (public). Common curves: P-256 (secp256r1, NIST, the usual curve for ECDSA TLS certificates and WebAuthn), P-384, secp256k1 (Bitcoin, Ethereum), and Curve25519/edwards25519 (X25519 and Ed25519, the next lessons).

ECDSA

To sign a message hash zz with private key dd on a curve with base point GG of order nn:

k∈[1,n−1] chosen fresh and secret for every signaturer=(kG)x mod ns=k−1(z+rd) mod n\begin{aligned} k &\in [1, n-1] \text{ chosen fresh and secret for every signature} \\ r &= (kG)_x \bmod n \\ s &= k^{-1}(z + r d) \bmod n \end{aligned}

The signature is (r,s)(r, s). Verification uses only the public key QQ: compute u1=zs−1u_1 = z s^{-1}, u2=rs−1u_2 = r s^{-1} and check that (u1G+u2Q)x≡r(u_1 G + u_2 Q)_x \equiv r.

Signing with the library

The private scalar below is fixed so the public key is the same on every run; real keys come from ec.generate_private_key(ec.SECP256R1()). The signatures themselves use a fresh kk each time, so the example prints verification results instead of signature bytes.

python · try it yourself
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives.asymmetric.utils import decode_dss_signature
from cryptography.hazmat.primitives import hashes
from cryptography.exceptions import InvalidSignature

priv = ec.derive_private_key(0x1D2C3B4A_5968_7766_5544_3322_1100_FFEE, ec.SECP256R1())   # fixed toy scalar
pub = priv.public_key()
nums = pub.public_numbers()
print("curve:", pub.curve.name, " public point x:", hex(nums.x)[:18] + "...")
msg = b"transfer 10 tokens to 10.10.0.20"
sig1 = priv.sign(msg, ec.ECDSA(hashes.SHA256()))
sig2 = priv.sign(msg, ec.ECDSA(hashes.SHA256()))
r1, _ = decode_dss_signature(sig1); r2, _ = decode_dss_signature(sig2)
print("two signatures of the same message differ (fresh k each time):", r1 != r2)
pub.verify(sig1, msg, ec.ECDSA(hashes.SHA256())); print("verify: OK")
try:
    pub.verify(sig1, msg + b"0", ec.ECDSA(hashes.SHA256()))
except InvalidSignature:
    print("verify altered message: InvalidSignature")

# The nonce-reuse algebra on P-256's group order n, with toy numbers chosen by this example:
n = 0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551
x, k, r = 123456789, 987654321, 55555           # toy private key, reused nonce, the shared r
z1, z2 = 1111, 2222                              # two message hashes
s1 = pow(k, -1, n) * (z1 + r * x) % n
s2 = pow(k, -1, n) * (z2 + r * x) % n
k_rec = (z1 - z2) * pow(s1 - s2, -1, n) % n
x_rec = (s1 * k_rec - z1) * pow(r, -1, n) % n
print("recovered k:", k_rec == k, " recovered private key:", x_rec == x)
curve: secp256r1  public point x: 0xf5256fa64b8b1f34...
two signatures of the same message differ (fresh k each time): True
verify: OK
verify altered message: InvalidSignature
recovered k: True  recovered private key: True

The nonce-reuse failure

If the same kk signs two different hashes z1z_1 and z2z_2, both signatures have the same rr (it depends only on kk), and an observer sees it immediately. Subtracting the two signing equations eliminates the private key:

s1−s2≡k−1(z1−z2)(modn)k≡z1−z2s1−s2(modn)d≡s1k−z1r(modn)\begin{aligned} s_1 - s_2 &\equiv k^{-1}(z_1 - z_2) \pmod n \\ k &\equiv \frac{z_1 - z_2}{s_1 - s_2} \pmod n \\ d &\equiv \frac{s_1 k - z_1}{r} \pmod n \end{aligned}

The last part of the example does exactly this arithmetic on numbers it chose (it is the computation, not an attack on anyone's key) and recovers both kk and the private key. That is the PS3 break: Sony's code used a constant kk. The same algebra emptied Bitcoin wallets in 2013 when Android's SecureRandom produced repeated values, and partial leaks are enough too: if a few bits of each kk are biased or leak through timing, lattice methods (the "hidden number problem") recover the key from hundreds to tens of thousands of signatures, as in the 2019 TPM-Fail and Minerva attacks on TPMs, smart cards and crypto libraries; the 2024 EUCLEAK side channel on Infineon-based security keys leaked the nonce through electromagnetic measurements during signing.

This lesson keeps the attack conceptual: the algebra above, on numbers the example made up. The defence matters more than the attack.

How ECDSA weaknesses are detected

  • Repeated rr values: any two signatures under one key with equal rr mean a reused nonce; scanning your own signature logs, transaction histories or firmware-signing records for duplicates is cheap. Researchers find vulnerable keys on public blockchains this way.
  • Code review: custom ECDSA implementations, kk drawn from a non-cryptographic generator, from a timestamp or a counter, or from a hash of the message alone (without the private key).
  • Side channels: non-constant-time scalar multiplication in firmware; vendor advisories for TPMs, HSMs and security keys.
  • Curve and parameter checks: unusual curves, missing validation that received points are on the curve (invalid-curve attacks on ECDH).

How it is fixed

  • Deterministic nonces (RFC 6979): derive kk from the private key and the message hash with HMAC-DRBG, so it is unique per message without any randomness at signing time. Many libraries do this, and FIPS 186-5 (2023) approves deterministic ECDSA. Some add extra randomness on top ("hedged" signatures) as a defence against fault attacks.
  • Prefer Ed25519, whose design makes the nonce deterministic by construction (Ed25519 Signatures).
  • Use maintained, constant-time libraries and certified hardware; rotate keys from affected devices after advisories.
  • If a reuse is found: treat the key as compromised, revoke it, re-sign with a new key.

Pitfalls

  • "Our random generator is fine" without checking it on the actual hardware at first boot.
  • Signing a message hash that is longer than the curve order without the standard truncation, or a hash the attacker controls without the message.
  • Accepting both (r,s)(r, s) and (r,n−s)(r, n - s) as distinct signatures in systems that use signature bytes as identifiers (ECDSA signature malleability; Bitcoin made the high-ss form non-standard for this reason).

Practice

1. A log of firmware signatures shows two entries with identical $r$ but different $s$. What do you conclude and do?

The same nonce kk signed two different images, so the private signing key can be computed from public data. Revoke the key, ship a new verification key through a trusted channel, re-sign, and fix the nonce generation (RFC 6979 or Ed25519).

2. Why does RFC 6979 include the private key in the nonce derivation and not only the message?

If kk depended only on the message, anyone could compute it from public data and then solve for dd from a single signature. Mixing in the private key keeps kk secret and unique per message.

3. Why is a 256-bit elliptic-curve key about as strong as a 3,072-bit RSA key?

The best attacks on the elliptic-curve discrete logarithm take about n=2128\sqrt{n} = 2^{128} steps for a 256-bit group, while factoring has sub-exponential algorithms, so RSA needs a much larger modulus for 128-bit security.

Test yourself

What happens if ECDSA signs two different messages with the same nonce kk?

  1. Nothing, kk is public anyway
  2. The private key can be computed from the two signatures
  3. Only the messages leak
  4. The signatures become invalid

How can a defender detect nonce reuse in their own ECDSA signatures?

  1. Look for equal ss values
  2. Look for equal rr values under the same key
  3. Count signatures
  4. Check the curve name

What does RFC 6979 specify?

  1. A new curve
  2. Deterministic generation of ECDSA nonces from the key and message hash
  3. Certificate formats
  4. A padding scheme for RSA

Which famous break used a constant ECDSA nonce?

  1. SHAttered
  2. The PlayStation 3 code-signing key (2010)
  3. Heartbleed
  4. Sweet32