Speed

A Reused Nonce Reveals the Key

If an ECDSA signer uses the same random number for two messages, the two signatures share r, and simple algebra turns the two signatures into the private key; the page does it on a toy curve of order 9851, and says no more than that about real systems.

The question

The lesson on ECDSA said that every signature uses a fresh random number k, and that the standard says a value k shall be generated at each invocation. What if it is not? Suppose a signer uses the same k for two different messages. What can someone who sees only the two signatures and the two messages work out?

The answer is the private key, and the work is a few lines of arithmetic. This page shows it on a toy curve, a curve small enough for the page's programs, with a private key of 14 bits. The toy is called a toy because it is one: it protects nothing, and its private key and nonce are made-up test material.

Work it by hand

The toy curve is y squared equal to x cubed plus x plus 28, modulo 10007. Its base point G is (2, 4582), and the order of G is n equal to 9851, a prime. FIPS 186-5 section 6.4.1 gives the signing steps. With private key d, nonce k and message number h:

  • r is the x-coordinate of the point k times G, modulo n.
  • s is the inverse of k modulo n, times h plus r times d, modulo n.

Two messages h1 and h2 signed with the same k give the same r and two values s1 and s2. Subtract: s1 minus s2 is the inverse of k times h1 minus h2. So k equals h1 minus h2, divided by s1 minus s2, all modulo n. Once k is known, s1 times k equals h1 plus r times d, and d equals s1 times k minus h1, divided by r.

For the demo's numbers, r is 2411, s1 is 8723 and s2 is 3094, with h1 equal to 111 and h2 equal to 222. The formulas give k equal to 777 and d equal to 1234, the numbers the signer started with; the oracle of this lesson recomputes them from r, s1, s2, h1 and h2. The same algebra, with a chosen repeated k, recovers a P-256 private key in that oracle too. The page says no more about real systems.

Run it

The program signs two messages, h1 and h2, with the same nonce k and private key d on the toy curve, using the helpers of the curve set. Line 5 computes k times G and line 6 takes r. Lines 8 and 9 compute the two values of s with the same inverse of k.

The program returns r, 2411, and the extra values give s1, 8723, s2, 3094, and the public point d times G, (1002, 7726). Both signatures have the same r: that repetition is what an observer looks for.

Both signatures have r equal to 2411. The value s is 8723 for the first message and 3094 for the second. The public point is (1002, 7726).

Public test material, generated for this lesson. Never use it for anything real.

let p = 10007;
let a = 1;
let n = 9851;
let g = ec_point(2, 4582);
let kg = ec_mul(p, a, k, g);
let r = kg.at(0) % n;
let kinv = mod_inv(k, n);
let s1 = mod_mul(kinv, (h1 + mod_mul(r, d, n)) % n, n);
let s2 = mod_mul(kinv, (h2 + mod_mul(r, d, n)) % n, n);
lmcode_return_put("s1", s1);
lmcode_return_put("s2", s2);
lmcode_return_put("public_point", ec_text(ec_mul(p, a, d, g)));
let answer = r;
{
  "answer": 2411,
  "s1": 8723,
  "s2": 3094,
  "public_point": "(1002, 7726)"
}

You can change the four numbers, with limits. The nonce k must not be 0 or a multiple of 9851: the point k times G is then the point at infinity and line 6 stops with "IntList.at: index out of range: 0". A private key of 0 gives the public point infinity and the run goes on. A negative nonce stops the run with "ec_mul: the multiplier must not be negative".

If h1 and h2 differ by a multiple of 9851, s1 and s2 are equal and nothing can be recovered. A nonce above 9851 is the same as the nonce reduced modulo 9851.

Your turn

The program below should return the private key d from two signatures that share r. The numbers are whole numbers: r, s1 and s2 of the two signatures and the message numbers h1 and h2, on the toy curve with n equal to 9851. The inputs keep h1 and h2 different modulo 9851 and r not 0. Decide whether the program does, and find the candidates that do. Check runs each candidate on the data shown and on three others.

The values r 4052, s1 7288 and h1 500 of the data shown, and the whole number that the shown program returns.
let n = 9851;
let k = mod_mul(h1 - h2, mod_inv(s2 - s1, n), n);
let top = mod_norm(mod_mul(s1, k, n) - h1, n);
let answer = mod_mul(top, mod_inv(r, n), n);
{
  "answer": 7922
}

On the toy curve with group order n = 9851 two messages with the numbers h1 and h2 were signed with the same nonce: the signatures (r, s1) and (r, s2). The inputs keep h1 and h2 different modulo 9851 and r not 0. Return the private key d, a whole number from 1 to 9850, using that s is k to the power minus 1 times h plus r times d, modulo 9851.

Read the result carefully

The attack needs two signatures with the same nonce and nothing else: no private data. That is why the standard requires a new value for each signature, and why other schemes derive the number from the key and the message instead, as an earlier lesson of this book shows for Ed25519. The page does not show how a nonce is generated well, how a repeat could happen, or any real system, and it names none.

The curve here has 9851 points in its group, a 14-bit order. A real curve has an order of 256 bits or more, far beyond what a program on this page can compute, and the page makes no claim about it. The numbers are made up. The h values are small whole numbers instead of hashes, to keep the arithmetic small.

Recap

  • ECDSA gives r as the x-coordinate of k G modulo n and s as k to the power minus 1 times h plus r d.
  • Two messages signed with the same k share r.
  • Then k equals the difference of the messages over the difference of the s values, and d follows from one s.
  • The nonce must be new for every signature; Ed25519 derives it from the key and the message.