Speed

ChaCha20-Poly1305 Beside GCM

The same call with another cipher name gives another authenticated cipher: both take a 32-byte key and a 12-byte nonce and return the ciphertext followed by a 16-byte tag, with different bytes. Only authenticated cipher names belong in ssl.seal.

The question

AES-GCM is one authenticated cipher; ChaCha20-Poly1305, defined in RFC 8439, is another. A program can use either through the same call, ssl.seal, by changing the cipher name. What stays the same, and what does not?

RFC 8439 section 2.8 describes the construction and section 2.8.2 gives a test vector, which the first lesson of this book used. This lesson seals one message with both ciphers under one key, nonce and associated data and compares the results. The keys are test material; no cipher is called better than the other here.

Work it by hand

Both ciphers are called with a 32-byte key, a 12-byte nonce, the associated data and the message. Both return the ciphertext, as long as the message, followed by a 16-byte tag. For the 16-byte message attack at dawn!! each sealed value is 16 + 16 = 32 bytes, 64 hex digits.

The bytes are produced by different algorithms, so there is no reason for the two sealed values to agree, and a byte that agrees at the same position can only be an accident. The run counts those bytes.

Run it

The program seals the message with aes-256-gcm (line 1) and with chacha20-poly1305 (line 2), both under the same key, nonce and associated data hdr. The loop of lines 4 to 6 counts the positions where the two sealed values have the same byte. Lines 7 and 8 return both values and line 9 returns the count.

The picture shows the AES-GCM value and the ChaCha20-Poly1305 value with the bytes that differ marked in red.

Two 32-byte sealed values, the AES-GCM one starting 6f c8 c1 bf and the ChaCha20-Poly1305 one starting 79 cc 36 50, with 30 of the 32 bytes marked red, and the count of equal bytes 2.

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

let a = ssl.seal("aes-256-gcm", key, nonce, "hdr", message);
let b = ssl.seal("chacha20-poly1305", key, nonce, "hdr", message);
let same = 0;
for (i in range(0, str_len(a) / 2)) {
  if (hex_slice(a, i, 1) == hex_slice(b, i, 1)) { same = same + 1; }
}
lmcode_return_put("aes", a);
lmcode_return_put("chacha", b);
let answer = same;
{
  "answer": 2,
  "aes": "6fc8c1bfd647a3dc7c88cd546f42b0b828376a986bc7358d6df733c541558c81",
  "chacha": "79cc3650ce8d86b067413800d82d6f0650f55bd50bc835ff2dcc9dce1b838c50"
}

You can change the key, the nonce and the message. The key must be 32 bytes, otherwise line 1 stops with "Ssl.seal: key must be 32 bytes". A nonce that is not 12 bytes stops line 2 with a message that starts "Ssl.seal: bad IV length", because ChaCha20-Poly1305 refuses it while AES-GCM on line 1 accepts it; an empty nonce stops line 1. The message may be empty; then both values are the 16-byte tag.

Your turn

The program below is meant to seal message with the cipher named in cipher, which is aes-256-gcm or chacha20-poly1305, under key (32 bytes, hex), nonce (12 bytes, hex) and the associated data hdr, and to return the 16-byte tag as 32 hex digits. Decide whether it does, and find the candidates that do. Check runs each candidate on the data shown and on six others.

The cipher aes-256-gcm, the message send help now, and the 16 bytes that the shown program returns.

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

let sealed = ssl.seal("chacha20-poly1305", key, nonce, "hdr", message);
let cut = str_len(sealed) - 32;
let answer = str_substr(sealed, cut, 32);
{
  "answer": "d0239f7fe03ca05648babe6e8cc1373e"
}

Return the 16-byte tag, as 32 hex digits, of ssl.seal(cipher, key, nonce, "hdr", message): cipher is aes-256-gcm or chacha20-poly1305, key is 32 bytes and nonce 12 bytes, both hex text, and message is text. Check runs each candidate on the data shown and on six others.

Read the result carefully

Only authenticated cipher names belong in ssl.seal. Measured in this page's engine: with aes-128-cbc the call returns without an error, and its last 16 bytes are not an authentication tag (they were all zero in the run); the name aes-128-ccm stopped with "Ssl.seal: aad: ..." and the reason "cipher operation failed" (with an empty associated data: "Ssl.seal: get tag (is this an AEAD cipher?)"), and an unknown name stops with "unknown cipher". This is a limit of the tool, not a property of the ciphers.

This page does not say which cipher to use, how fast either is, how the tags are computed, or how a key is chosen. The key and the nonce are test material, and an independent implementation written for this page reproduces the values.

Recap

  • ssl.seal takes the cipher name as its first argument; aes-256-gcm and chacha20-poly1305 both take a 32-byte key and a 12-byte nonce.
  • Both return the ciphertext followed by a 16-byte tag, so the sealed values have the same length.
  • The bytes differ: the ciphers are different algorithms (RFC 8439 section 2.8 for ChaCha20-Poly1305).
  • Only authenticated cipher names are meant for ssl.seal; the tool does not refuse every other name.