How to Rotate an age Key When Old Backups Still Need the Old One
Changing the recipient in your backup job takes a minute, and does nothing at all to the files you already have. They still open only with the identities they were encrypted to. Bin the old key too early and you own a tidy archive of random-looking bytes.
So rotation is four jobs:
- Point new backups at a new key.
- Keep track of which key opens which file.
- Re-encrypt old files, or let them expire.
- Retire the old key only once nothing needs it.
I tried the commands below with age v1.4.0. Distribution packages can be older, so run age --version first.
New keys may refuse to sit beside old ones
In v1.4.0, age-keygen makes a post-quantum hybrid key (ML-KEM-768 plus X25519) by default. Its public half starts age1pq1 and is nearly 2,000 characters long.
umask 077
age-keygen -o new.txt
age-keygen -y new.txt > recipients.txt
The obvious overlap plan is to list the old and new public keys together for a few weeks. If the old one is a classic age1... key, age rejects the mixture:
age: error: incompatible recipients: can't mix post-quantum and classic recipients, or the file would be vulnerable to quantum computers
The refusal is fair, since a file is only as quantum-resistant as its weakest recipient. You have two choices:
- Cut over cleanly, so new files go to the post-quantum recipient only. This is my preference.
- Generate a classic replacement with
age-keygen -pq=falseif some restore machine or tool cannot handle the new type yet. age will warn you about classic keys.
Switch new backups, but keep both identities for restores
The encryption and decryption sides can change at different times. The backup job needs only the new recipients.txt. The restore side needs every private identity that any surviving file might require.
You can pass -i more than once, and age ignores identities it does not need:
age -R recipients.txt -o backup.tar.age backup.tar
age -d -i new.txt -i old.txt -o restored.tar some-backup.tar.age
If you keep an offline recovery key, put its public half in recipients.txt too. Each listed recipient can decrypt the file alone. It must be post-quantum if the new primary key is.
For the same reason, leave the old recipient out if you suspect it has leaked. An overlap would hand the person holding it your new backups as well.
Before trusting the switch, take one freshly made backup and restore it using only new.txt.
The file will not tell you which key it needs
age-inspect reads a file's metadata without decrypting it. It reports recipient types, such as X25519 or mlkem768x25519, which helps you find classic files.
age-inspect some-backup.tar.age
Quick detour, because this is rather neat. A native X25519 header carries an ephemeral share, not the recipient's public key. Your identity simply tries to unwrap it. Two files for two different X25519 keys therefore give you no label to tell them apart. That is good for privacy and mildly annoying for archivists. (SSH recipients differ: their header entries include a key tag.)
So you will be keeping your own manifest. For each backup, record:
- its path and every place a copy lives
- the SHA-256 of the encrypted file
- the public keys it was encrypted to, or a short name for each
- the date it is due to expire
No manifest for the old files? Try decrypting each with one identity at a time, sending the output to /dev/null, and record what works. Reading the whole file makes this slow, but it also means the whole file gets checked.
Decide whether old files need re-encrypting at all
If the rotation is routine and old backups expire in 90 days, waiting is reasonable. Keep the old identity safe until the last one goes. Re-encryption is worth its risk when files are kept for years, when you are moving them to post-quantum recipients, or when someone should no longer have access.
Re-encrypting cannot take back a copy somebody already has. The old ciphertext still opens with the old key, including copies in snapshots, object-lock buckets and that USB disk in a drawer. After a leak, assume anything the attacker could have copied is readable, and change any credentials stored inside it.
Never trust the output of a bare pipe
The age CLI has no "change recipients" command, so you decrypt and encrypt again. The tempting version avoids writing plaintext to disk:
age -d -i old.txt old.tar.age | age -R recipients.txt -o new.tar.age
I fed this a deliberately truncated input. Decryption failed with an error, as it should. The encrypting process saw only the end of its input, and wrote a perfectly valid file containing a shortened backup. It decrypted with the new key and exit status 0.
Hang on, doesn't age authenticate everything? It does, in 64 KiB chunks, so earlier good chunks can be released before a later one fails. The second age has no idea that its input was cut short. You only find out from the first command's exit status.
This script handles that. Save it as reencrypt.sh:
#!/usr/bin/env bash
# usage: reencrypt.sh OLD_IDENTITY NEW_IDENTITY NEW_RECIPIENTS SRC DST
set -euo pipefail
old=$1 new=$2 recipients=$3 src=$4 dst=$5
tmp=$(mktemp "$dst.XXXXXX")
trap 'rm -f "$tmp"' EXIT
age -d -i "$old" "$src" | age -R "$recipients" > "$tmp"
before=$(age -d -i "$old" "$src" | sha256sum)
after=$(age -d -i "$new" "$tmp" | sha256sum)
[[ $before == "$after" ]]
sync "$tmp"
ln "$tmp" "$dst" # fails if DST already exists
sync "$(dirname "$dst")"
What each safeguard buys you:
pipefailmakes a failed decryption fail the script.- The two hashes show that the new key recovers the same bytes as the old file.
- The temporary name keeps partial output away from the real destination.
lnrefuses to overwrite, whereasage -oreplaced an existing file in my test. It needs a filesystem with hard links.
The script leaves the source alone. It also needs both private identities on one machine, so use one you trust, not the server that merely creates backups. With an offline recovery recipient, test that identity separately on a sample.
The hash check proves the copy matches its source, but it cannot prove the original backup was any good. Restore a few into a scratch directory and open the data with the application that owns it.
Retire the old key when the inventory says so
Upload each new file under a new name, verify the stored copy against its SHA-256, and update the manifest. Then remove the old object according to your retention rules.
The old identity can go when all of these are true:
- No retained file in the manifest depends on it, including offsite and offline copies.
- Backup jobs have stopped using its recipient. Check actual recent output, not just the config in Git.
- A restore drill has succeeded on a machine that does not have the old identity at all.
- You have checked for other uses, such as secrets files or configuration encrypted to the same recipient.
For a routine rotation, I would move the old identity offline for one more retention cycle before destroying it. For a compromised one, stop using it for anything new immediately, but remember that you may still need it to recover your own data.