Keyboard shortcuts

Press ← or → to navigate between chapters

Press ? to show this help

Press Esc to hide this help

How does ChaCha20-Poly1305 protect messages with addition and rotation?

For a short message or a long attachment, a sender and recipient who can produce the same secret byte sequence can combine it with the message body to encrypt it and apply it again to decrypt. ChaCha20 generates this sequence using integer operations that general-purpose CPUs handle efficiently. Poly1305 produces a tag that lets the recipient check that the ciphertext and the public associated data match the pair the sender sent. RFC 8439

Protect a 65-byte message with ChaCha20-Poly1305
  1. Shared inputs Both parties hold the secret key. The protocol provides both with the public 96-bit nonce (unique per key) and AAD.
  2. Sender encrypts the body XOR the first 64 bytes with ChaCha20 block 1 and the final byte with the first byte of block 2 to get ciphertext C.
    Sender derives a one-time key Read the first 32 bytes of ChaCha20 block 0 as two 16-byte little-endian integers r and s. Clamp r by clearing the specified bits.
  3. Sender makes the tag Run Poly1305 over the formatted AAD and C with the one-time key.
  4. To the recipient The sender sends C and the tag.
  5. Recipient checks the tag Reproduce the one-time key from block 0. Recompute the tag from the AAD and C and compare it with the received tag in constant time.
  6. Tags match Reproduce the keystream from counter 1. XOR it with C and return the body.
    Tags differ Return an authentication error.

Why identical plaintext blocks produce different ciphertext

ChaCha20 arranges sixteen 32-bit words in a four-by-four grid. The state holds constants, a 256-bit key, a block counter, and a 96-bit nonce. It mixes four words at a time with repeated additions that wrap on overflow, XORs, and bit rotations. A rotation wraps bits that move past one end of a word around to the other end. ChaCha20 alternates column and diagonal rounds for a total of 20 rounds, adds the initial state word by word, and produces 64 bytes.

For a 65-byte message, XOR the first 64 bytes with the output for block counter 1 and the final byte with the first output byte for counter 2. The recipient can reproduce the same keystream with the same key, nonce, and counter values. The first 32 output bytes for counter 0 become the one-time Poly1305 key for the packet. RFC 8439 §§2.3–2.6

What Poly1305 computes

Poly1305 authenticates the AAD, its padding to a 16-byte boundary, the ciphertext, its padding, and the original byte lengths of the AAD and ciphertext, in that order. Read the input in 16-byte blocks as little-endian integers and set a bit just above the 128 bits of each block to obtain n. Start the accumulator at zero and use the secret coefficient r to update it as accumulator ← (accumulator + n) × r mod (2¹³⁰ − 5). For two blocks, for example, the first value is multiplied by r twice and the second value once, so the order affects the result.

Poly1305 masks specific bits of r before using it. At the end, it adds s, the other half of the key, and takes the low 128 bits as the tag. The recipient runs the same calculation to verify the tag. Reusing a nonce with the same key repeats both the message keystream and the one-time values r and s. In the SDK, specify aead="chacha20-poly1305" to create an envelope with this algorithm. The SDK derives the key and nonce as part of the operation. Authenticated encryption API example · RFC 8439 §2.8