Ciphertext Plus Tag
An authenticated cipher returns the ciphertext, as long as the plaintext, followed by a 16-byte tag; ssl.seal gives both as one hex text, and ssl.open checks the tag and returns the plaintext.
The question
Encryption alone, as in the modes of aes-modes-of-operation, hides a message but does not notice a change. An authenticated cipher also produces a tag that covers the ciphertext and the associated data, so that a change can be refused. What does the output of such a cipher look like, and how long is it?
RFC 5116 section 2.1 describes the operation: the inputs are a key, a nonce, a plaintext and the associated data, and the output is a ciphertext that is at least as long as the plaintext. This lesson uses the call ssl.seal with real ciphers; the keys here are test material.
Work it by hand
RFC 8439 section 2.8.2 gives a test vector for ChaCha20-Poly1305: a 32-byte key 80 81 82 and so on up to 9f, the nonce 07 00 00 00 40 41 42 43 44 45 46 47, the associated data 50 51 52 53 c0 c1 c2 c3 c4 c5 c6 c7, and a plaintext of 114 bytes that begins Ladies and Gentlemen.
The ciphertext is as long as the plaintext, 114 bytes, and the tag adds 16 bytes: 114 + 16 = 130. The RFC prints the ciphertext starting d3 1a 8d 34 64 8e 60 db and the tag 1a e1 0b 59 4f 09 e2 6a 7e 90 2e cb d0 60 06 91. So the sealed value should be 130 bytes, which is 260 hex digits.
Run it
The program seals the RFC plaintext with ssl.seal and the cipher name chacha20-poly1305. The key, the nonce and the associated data header are hex text; the associated data is given as bytes with ssl.unhex (line 1). Lines 2 to 4 cut the sealed hex: the last 32 digits are the tag. Lines 5 and 6 call ssl.open and check that it returns the message. Line 7 returns the sealed length in bytes.
The picture shows the message, the start of the ciphertext, the tag and the length.

Public test material, generated for this lesson. Never use it for anything real.
let sealed = ssl.seal("chacha20-poly1305", key, nonce, ssl.unhex(header), text);
let cut = str_len(sealed) - 32;
lmcode_return_put("ciphertext", str_substr(sealed, 0, cut));
lmcode_return_put("tag", str_substr(sealed, cut, 32));
let back = ssl.open("chacha20-poly1305", key, nonce, ssl.unhex(header), sealed);
if (back != text) { out.fail("open did not return the message"); }
let answer = str_len(sealed) / 2;
{
"answer": 130,
"ciphertext": "d31a8d34648e60db7b86afbc53ef7ec2a4aded51296e08fea9e2b5a736ee62d63dbea45e8ca9671282fafb69da92728b1a71de0a9e060b2905d6a5b67ecd3b3692ddbd7f2d778b8c9803aee328091b58fab324e4fad675945585808b4831d7bc3ff4def08e4b7a9de576d26586cec64b6116",
"tag": "1ae10b594f09e26a7e902ecbd0600691"
}You can change the message to any text of up to 2048 bytes; the empty text is accepted and then the sealed value is the tag alone. The header may be any hex of whole bytes, also empty. The key must be 32 bytes, otherwise line 1 stops with "Ssl.seal: key must be 32 bytes"; the nonce must be 12 bytes, otherwise it stops with a message that starts "Ssl.seal: bad IV length".
Your turn
The program below is meant to return the tag of a sealed value as 32 hex digits: the last 16 bytes of what ssl.seal returns. cipher is aes-128-gcm, aes-256-gcm or chacha20-poly1305; key, nonce and aad are hex text of bytes, and message is text. Decide whether it does, and find the candidates that do. Check runs each candidate on the data shown and on five others.

Public test material, generated for this lesson. Never use it for anything real.
let sealed = ssl.seal(cipher, key, nonce, aad, message);
let cut = str_len(sealed) - 32;
let answer = str_substr(sealed, cut, 32);
{
"answer": "98037592f028b3ea6e71a83a47544c1b"
}Return the 16-byte tag, as 32 hex digits, of ssl.seal(cipher, key, nonce, aad, message), where aad is the associated data given as hex text of its bytes (it may be empty), key and nonce are hex text, and message is text. Check runs each candidate on the data shown and on five others.
Read the result carefully
The tag is computed from the key, the nonce, the associated data and the ciphertext; the program only cuts it out, and an independent implementation written for this page reproduces the values. A tag does not hide anything and does not say who made the message beyond "someone with the key"; a tag made from a hash and a key, as in hmac-uses-two-pads, authenticates without encrypting.
The keys, nonces and messages are test material. The program stops with the engine's message for a key or a nonce of the wrong size, and for a cipher name it does not know, for example "Ssl.seal: unknown cipher 'rot13'" followed by OpenSSL's reason. This page does not show how the tag is computed, key management or what happens when a nonce is used twice.
Recap
- An authenticated cipher outputs ciphertext and a 16-byte tag (RFC 5116, section 2.1; RFC 8439, section 2.8).
ssl.sealreturns both as hex: 2 x (message length + 16) digits.- The RFC 8439 section 2.8.2 vector gives a 130-byte sealed value for the 114-byte message, with the tag 1ae10b59 4f09e26a 7e902ecb d0600691.
- An empty message seals to the tag alone.