Speed

ECDH Both Sides Agree

Two parties each combine their own private number with the other's public point and get the same 32 bytes, the x-coordinate of one shared point; the page compares the two recorded results and derives a secret with one command.

The question

Two people, A and B, each have an elliptic-curve key pair. They swap public keys over any channel. Can each of them end up with the same secret number that nobody who only saw the public keys can compute? Elliptic-curve Diffie-Hellman does exactly that, and the two sides do different work and get the same bytes.

The keys of this lesson, p256-a and p256-b, and the other pairs of the Your turn, are test material: P-256 keys made for these lessons. A real exchange uses fresh keys for each session; these are fixed so that the page can show the results.

Work it by hand

Write G for the curve's base point. A has the private number a and the public point Q_a, which is a times G. B has b and Q_b, which is b times G.

  • A takes B's public point and multiplies it by a: a times Q_b is a times b times G.
  • B takes A's public point and multiplies it by b: b times Q_a is b times a times G.
  • Multiplication of numbers does not care about the order, so both sides reach the same point, a b G.
  • The shared secret is the x-coordinate of that point, 32 bytes on P-256.

An observer sees Q_a and Q_b, which are a G and b G. Getting a b G from them without knowing a or b is the problem the scheme rests on. For these 256-bit keys no program of this page can attempt it, and the page makes no claim about how hard it is beyond what a standard says. The oracle of this lesson recomputes each secret with Python integers, multiplying a point by a number on P-256, and it matches what OpenSSL writes.

Run it

OpenSSL writes the secret with pkeyutl -derive: A runs it with A's private key and B's public key, and B with B's private key and A's public key. The 32 bytes were derived both ways for the pair a and b, found equal and stored once; the two values below are that one result, entered for each side (stated here, not run on this page).

The program compares the bytes of A's side with the bytes of B's side. It returns true when they are equal. The extra values give the length, 32 bytes, and the first four bytes of the secret.

The 32 bytes derived on A's side and the same 32 bytes derived on B's side, with no byte marked as different, and the result true.

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

let same = from_a == from_b;
lmcode_return_put("bytes_a", str_len(from_a) / 2);
lmcode_return_put("first_four", str_substr(from_a, 0, 8));
let answer = same;
{
  "answer": true,
  "bytes_a": 32,
  "first_four": "8e4800e5"
}

You can change both values. The program only compares two texts. The page lowercases the hexadecimal and drops spaces before the program sees it, so two texts of different bytes give false, and so does a text of a different length; bytes_a shows the length of the first.

Your turn

The command below should derive the shared secret of the private key in in/key.txt and the public key in in/peer.txt and write the 32 bytes to out.bin. Decide whether it does, and edit it so that it does. Check runs your command on the data shown and on three others: two other pairs, and the shown pair seen from the other side.

The first two lines of the private key file, a label line and the line that opens the private key block, the first two lines of the peer's public key file, and the 32 bytes that the starting command wrote.

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

# Reads your private key in in/key.txt and the other side's public key
# in in/peer.txt. Writes the 32 bytes of the shared secret to out.bin.
pkeyutl -derive -inkey in/key.txt -peerkey in/key.txt -out out.bin
{
  "answer": "81c94732bafab91b958253ba284dbac4f78cb5c3bfc1592d3452006e1877c6a8",
  "file": "out.bin",
  "bytes": 32
}

Write one command that derives the ECDH shared secret from the P-256 private key in in/key.txt and the P-256 public key in in/peer.txt and writes the 32 bytes to out.bin. Check runs your command with other pairs of keys in those files.

Read the result carefully

The shared secret is not yet an encryption key. It is a number both sides know, and turning it into one is not shown here. The exchange also does not tell either side who the other is: a third party who sits in the middle and runs one exchange with each side gets two secrets of its own. Certificates and signatures address that; this page does not.

Keys on different curves do not combine: a P-256 key with a 112-bit peer stops with "pkeyutl: Error setting up peer key: mismatching domain parameters". The pairs here are fixed test keys. They must never protect anything real, and the page shows no key generation.

Turning one shared secret into separate keys is the job of a key derivation function; the book Keys From Passwords has a lesson on HKDF, which does that.

Recap

  • With a, b, Q_a equal to a G and Q_b equal to b G, A computes a Q_b and B computes b Q_a: the same point a b G.
  • The secret is the x-coordinate of that point, 32 bytes on P-256.
  • A key tool derives the secret on each side, and with the roles swapped the other side gets the same bytes.
  • The secret authenticates nobody and is not yet a key.