In TLS, each side's Finished message is a MAC over the whole handshake, keyed from the shared secret. A changed message makes the check fail.
What to build
Run a toy handshake: agree on a secret with Diffie-Hellman, then check the Finished values that arrived.
Requirements
- Line 1: p g a b (client secret a, server secret b). Line 2: the client share A that the server received. Line 3: the server share B that the client received. Line 4: the server Finished value the client received (8 hex digits).
- The client sent A = g^a mod p and the server sent B = g^b mod p, but an attacker may have changed them on the way. The client computes its secret as B_received^a mod p; the server computes A_received^b mod p.
- Each side's transcript is what it sent and received. The client's is
ClientHello share=A|ServerHello share=B_received(with the A it sent). The server made its Finished from its own view, so the client expects toyHash(clientSecret + "|server finished|" + clientTranscript). - toyHash is FNV-1a 32-bit (start 2166136261; for each character, XOR the code, then Math.imul by 16777619 and >>> 0), printed as 8 hex digits.
- Print
client secret X,server secret Y, thenhandshake completeif the received server Finished equals the client's expected value, otherwiseFinished mismatch: connection closed.
Input
Four lines.
Output
Three lines.
Constraints
p is a prime below 50000; 1 ≤ g, a, b, shares < p. Repeated squaring keeps products below 2³² here, so Numbers are safe.
Example
Input:
23 5 6 15
8
19
19005da4
Output:
client secret 2
server secret 2
handshake complete
Why it works
If no one changed a share, both sides compute the same secret and the same transcript, so the Finished values agree; a swapped share changes the secret on one side or the transcript, and the MAC no longer matches.
Examples
23 5 6 15 8 19 19005da4
client secret 2 server secret 2 handshake complete
23 5 6 15 8 4 19005da4
client secret 2 server secret 2 Finished mismatch: connection closed
Hints
Hint 1
Use modPow with repeated squaring.
Hint 2
Join numbers into strings with template literals, exactly in the format given.
Solution
Show one correct solution (try it yourself first)
function toyHash(text) {
let h = 2166136261;
for (let k = 0; k < text.length; k++) h = Math.imul(h ^ text.charCodeAt(k), 16777619) >>> 0;
return h.toString(16).padStart(8, "0");
}
function modPow(base, exp, m) {
let result = 1;
base %= m;
while (exp > 0) {
if (exp % 2 === 1) result = (result * base) % m;
base = (base * base) % m;
exp = Math.floor(exp / 2);
}
return result;
}
const [p, g, a, b] = prompt().trim().split(/\s+/).map(Number);
const A = Number(prompt());
const B = Number(prompt());
const finished = prompt().trim();
const clientSecret = modPow(B, a, p);
const serverSecret = modPow(A, b, p);
console.log(`client secret ${clientSecret}`);
console.log(`server secret ${serverSecret}`);
const transcript = `ClientHello share=${modPow(g, a, p)}|ServerHello share=${B}`;
const expected = toyHash(`${clientSecret}|server finished|${transcript}`);
console.log(finished === expected ? "handshake complete" : "Finished mismatch: connection closed");
▶ Open this solution in the compiler, then press Visualize to watch it run step by step