Authenticated encryption¶
Read the implementations in Python and C.
AES-256-GCM and ChaCha20-Poly1305 protect the contents of a message and detect changes to both the ciphertext and any associated data. Here the associated data is a visible label supplied separately by both parties. The recipient must know its exact UTF-8 bytes. Use a UTF-8 terminal when supplying a non-ASCII label. Both C commands reject malformed UTF-8 before opening files.
mkdir -p lab
printf 'A message with a trailing newline\n' > lab/plain
uv run python examples/pce.py symkey lab/aead.key
uv run python examples/pce.py encrypt aes256gcm lab/aead.key lab/plain lab/envelope 'document v1'
build/pce decrypt aes256gcm lab/aead.key lab/envelope lab/recovered 'document v1'
cmp lab/plain lab/recovered
Try decrypting again with the label document v2 and a fresh output path.
Authentication fails and the program leaves the output path unused. To try
ChaCha20-Poly1305, select chacha20poly1305 on both commands, generate a
separate key, and choose fresh output files.
| Field, in file order | Size | Purpose |
|---|---|---|
| Nonce | 12 bytes | Generated by the encryptor for each message |
| Ciphertext | Same length as the plaintext | Encrypted message |
| Authentication tag | 16 bytes | Checked before plaintext is released |
Generate a fresh 32-byte key with symkey. A nonce may be public, but reusing
one with the same key breaks the security of these algorithms. These
low-volume examples use random nonces. A high-volume or distributed system
needs a per-key nonce allocation and rotation policy. The envelope carries
neither an algorithm identifier nor a key identifier. The receiver selects
both from its trusted context.
See the PyCA AEAD API and RFC 8439.