Speed

ECDSA Signatures Are Random

An ECDSA signature uses a fresh random number for every signature, so signing one message twice gives two different signatures, both of which verify; the page checks signatures of a fixed test key and never returns a fresh one.

The question

Sign the same message twice with the same RSA key and PKCS#1 v1.5, and you get the same bytes. Do the same with ECDSA, and you do not. Is something wrong? No: an ECDSA signature includes a fresh random number, and a new one is drawn every time. So a signature cannot be compared byte by byte with another; the only test is to verify it.

The key p256-signer is test material, a P-256 key made for these lessons. The signatures the page checks were made once with it and are fixed; the page never signs a case itself, because every new signature would differ.

Work it by hand

FIPS 186-5 section 6.4.1 describes the signing steps. With the private number d, the order n of the curve and e the integer made from the message's hash, the signer

  • generates a per-message secret number k, using a procedure of section 6.3;
  • computes the point k times G, and sets r to the x-coordinate of that point modulo n;
  • computes s as the inverse of k modulo n, times e plus r times d, modulo n.

The standard says that a value k shall be generated at each invocation of the signature generation algorithm. A different k gives a different point, so a different r, and a different s. The pair (r, s) is sent as the ASN.1 structure of RFC 3279 section 2.2.3, a sequence of two INTEGER values.

The length of the structure varies. Each of r and s is a 32-byte number, and an INTEGER whose first bit is set gets an extra leading zero byte, so each takes 32 or 33 bytes. With the two headers of 2 bytes each and the sequence header of 2 bytes, the whole signature is 70, 71 or 72 bytes (shorter only when r or s has a leading zero byte, which is rare).

One run of 30 signatures of the message meet at noon with the test key gave 7 of 70 bytes, 14 of 71 and 9 of 72 (stated here, not run on this page; another run gives other counts).

Run it

The program makes a fresh P-256 key with ssl.gen_ec, signs the message twice with SHA-256 and returns three yes-or-no values as 1 or 0. Lines 2 and 3 sign; line 6 asks whether the two signatures differ, and lines 8 and 10 verify each of them with the key.

The answer is 1, 1, 1: the signatures differ, and both verify. The signatures themselves are not returned: they change on every run, and so does their length.

The message pay 10 to bob, the answer 1, 1, 1 (the signatures differ, the first verifies, the second verifies) and the curve prime256v1.

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

let k = ssl.gen_ec("P-256");
let first = k.sign("SHA256", message);
let second = k.sign("SHA256", message);
let answer = int_list();
answer.add(0);
if (first != second) { answer.set(0, 1); }
answer.add(0);
if (k.verify("SHA256", message, first)) { answer.set(1, 1); }
answer.add(0);
if (k.verify("SHA256", message, second)) { answer.set(2, 1); }
lmcode_return_put("curve", k.curve());
{
  "answer": [
    1,
    1,
    1
  ],
  "curve": "prime256v1"
}

You can change the message to any text. An empty message works. The answer stays 1, 1, 1 on every run, because the chance that two random numbers coincide is far too small to meet; the page does not compute it.

Your turn

The program below should check signatures made by the key in key: pairs holds one line for each pair, a message, a bar and the signature as hexadecimal DER, and the program returns a list with one number per line, 1 if the signature is a valid ECDSA signature of that message with SHA-256 and 0 if not. Decide whether it does, and find the candidates that do. Check runs each candidate on the data shown and on five others.

Two lines, each a message, a bar and a long signature in hexadecimal, and the list of 1 and 0 that the shown program returns.

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

let k = ssl.parse_key(key);
let lines = str_split(pairs, "\n");
let answer = int_list();
for (i in range(0, lines.len())) {
  let parts = str_split(lines.at(i), "|");
  let valid = k.verify("SHA1", parts.at(0), parts.at(1));
  answer.add(0);
  if (valid) { answer.set(i, 1); }
}
{
  "answer": [
    0,
    0
  ]
}

key is the text of a P-256 private key; pairs is text with one line per pair, message, a bar, the DER signature in hexadecimal; the signatures were made with ECDSA and SHA-256. Return a list with one whole number per line: 1 if the signature is a valid signature of that line's message under that key, 0 if not. A message contains no bar.

Read the result carefully

A valid signature shows that someone holding the private key signed exactly this message. Because signatures are random, the same message can have many valid signatures, and a program must never decide validity by comparing a new signature with an old one. It must verify.

The key and the signatures of the cases are test material, made once and kept; the page shows no key generation for them and no timing. It does not show how the random number is chosen, which is the subject of the lesson on a repeated number, nor the variant in which k is derived from the message instead of drawn at random, nor how the curve is chosen.

Recap

  • ECDSA draws a fresh random number k for every signature, so two signatures of one message differ.
  • Every one of them verifies; verification is the only test.
  • The signature is two INTEGERs in a sequence, usually 70 to 72 bytes on P-256.
  • A program must not return or compare a freshly made signature.