Speed

A Tampered Message Is Refused

ssl.open either returns the plaintext or stops the run with an authentication failure; a changed digit, another associated data or another nonce all give the same refusal and no plaintext, and the page cannot go on after it.

The question

The previous lessons made a sealed value, the answer to the change that a-bit-flip-changes-the-message makes without being noticed. What does the other half of the cipher do with one it receives? SP 800-38D section 7.2 (Algorithm 5) computes the plaintext, recomputes the tag from the key, the nonce, the associated data and the ciphertext, and returns the plaintext only if the recomputed tag equals the received tag; otherwise it returns FAIL.

This lesson opens a real AES-128-GCM sealed value with ssl.open and shows what a refusal looks like in this page. The key is test material. The exercise is a stand-in for the check, for a reason that Read the result carefully explains.

Work it by hand

The sealed value in the demo holds the message pay 10 to bob under the associated data invoice 17. It has 13 bytes of ciphertext and a 16-byte tag, so 29 bytes: 58 hex digits.

Opening it recomputes the tag from the ciphertext, the associated data invoice 17, the key and the nonce. If all four are the ones that were used for sealing, the tag is the same and the 13 message bytes come out. If one digit of the sealed value is changed, or the associated data is another text, or the nonce is another, the recomputed tag differs from the received one, and the answer is FAIL: no message, not even a wrong one.

Run it

The program opens the sealed value sealed under the key, the nonce and the associated data header (line 1). Line 2 returns the sealed length in bytes and line 3 returns the plaintext. The picture shows the sealed hex and the message that came out.

The 29-byte sealed value, the header invoice 17, and the message pay 10 to bob that opening returns.

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

let message = ssl.open("aes-128-gcm", key, nonce, header, sealed);
lmcode_return_put("bytes", str_len(sealed) / 2);
let answer = message;
{
  "answer": "pay 10 to bob",
  "bytes": 29
}

You can change header, key, nonce and sealed. Try it: change one hex digit of sealed, or the text of header, or one digit of nonce. The run stops at line 1 with "Ssl.open: authentication failed (wrong key, IV, AAD or tampered data)" and returns nothing; measured for a changed first digit, another header and another nonce. The page cannot show a rejection and go on: after the stop there is no later line.

A sealed of fewer than 16 bytes stops with "Ssl.open: sealed data is shorter than the 16-byte tag".

Your turn

A receiver that already knows what message to expect can check a value without calling ssl.open, by sealing the claimed message again and comparing. The program below is meant to return true if received is exactly the value that ssl.seal makes of claimed under key, nonce and aad with aes-128-gcm, and false otherwise. Decide whether it does, and find the candidates that do. Check runs each candidate on the data shown and on six others.

The claimed message send the key, the associated data order 7 and the first 16 bytes of the received value, which is 28 bytes long.

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

let size = str_len(received);
let want = 2 * (str_len(claimed) + 16);
let answer = size == want;
{
  "answer": true
}

received is the hex text of a value that may have been made by ssl.seal with aes-128-gcm; key and nonce are hex text, aad and claimed are text. Return true if received is exactly what ssl.seal("aes-128-gcm", key, nonce, aad, claimed) returns, and false otherwise. Check runs each candidate on the data shown and on six others.

Read the result carefully

The exercise is a stand-in. A real receiver does not have the plaintext; it has the sealed value and calls an open operation, which fails by itself. The page cannot do that in an exercise, because a failing ssl.open ends the program, so the exercise recomputes the sealed value from a claimed message, which only a receiver that knows the message could do.

The refusal says that something differs, not what: a wrong key, another nonce, other associated data and a changed digit look the same. The key and the nonce are test material. The page does not show replay, key management or what happens when a nonce is used twice.

Recap

  • Opening recomputes the tag and returns the plaintext only if it equals the received tag (SP 800-38D, section 7.2).
  • A changed digit, other associated data or another nonce are refused the same way.
  • In this page a refusal stops the run with "Ssl.open: authentication failed ..." and no plaintext.
  • Sealing the claimed message again and comparing the values is a check that needs the message.