Notes
Why rotate
Why rotate? It limits how much data one stolen key exposes. It removes access from people who have left and from systems that no longer need it. Some algorithms have real limits, such as the number of messages per key in GCM. Standards like PCI DSS and SOC two expect it. And the most practical reason: a team that never rotates has no working procedure, so on the day a key leaks, the emergency rotation causes an outage.
- limits the data exposed by one compromised key
- removes access from people who have left
- cryptographic limits: GCM nonces per key
- compliance: PCI DSS, SOC 2, ISO 27001 expect it
- practice: if you never rotate, you cannot do it in an emergency
The naive way fails
The naive way is to swap the key in the configuration. Immediately, every existing record fails to decrypt. So teams try to re-encrypt everything first, which takes hours or days for a large dataset, and during that time some records use the old key and some the new, with nothing recording which is which. After one painful attempt, rotation is postponed indefinitely. The fix is to plan for several keys existing at once.
- replace the key in config: all old data fails to decrypt
- re-encrypt everything first: hours or days, and both versions exist meanwhile
- no record of which key encrypted what
- so rotation gets postponed, for years
Key versioning
The design is key versioning. Every key has a version number, and every ciphertext stores the version it was encrypted with, in a small header. To encrypt, always use the newest version. To decrypt, read the tag and fetch that version. Old versions stay available, for decryption only. Rotation then means adding a new version and making it current. Nothing breaks, because old data still names its own key.
- every key has a version id
- every ciphertext stores the version it was made with encrypt: always the newest version
- decrypt: read the tag, use that version
- old versions stay available for decryption only rotation = add a version
Run it
Here it is with a real library. We encrypted a value with key one. Then we built a key ring with key two first and key one second. The old token still decrypts, because the ring tries both. Calling rotate re-encrypts it under key two. After that, key two alone reads the new token, and key one cannot. We rotated one hundred thousand tokens in under two seconds. Once every token is rotated, key one can be retired.
tok = Fernet(k1).encrypt(b"card=4111...") mf = MultiFernet([Fernet(k2), Fernet(k1)]) mf.decrypt(tok) # works: tries both new = mf.rotate(tok) # now under k2 Fernet(k2).decrypt(new) # works Fernet(k2).decrypt(tok) # InvalidToken Fernet(k1).decrypt(new) # InvalidToken # 100,000 tokens rotated in 1.84 s
Rewrap, not re-encrypt
With envelope encryption, rotation gets much cheaper. To rotate the master key, you unwrap each data key with the old master and wrap it with the new one. We rewrapped one hundred thousand data keys in zero point two three seconds. The data itself is not touched. Compare re-encrypting the data: a terabyte means reading and writing every byte. So master keys rotate often and cheaply, and data keys are replaced when data is rewritten.
# rewrap 100,000 data keys under # a new master key: 0.23 s # re-encrypt 100 MB of data # under a new data key: 0.15 s # 1 TB at that rate: ~25 minutes # of CPU, plus reading and # writing every byte # rewrap touches 40 bytes per object
KMS automatic rotation
Cloud key services do this for you. Turn on automatic rotation, and the service creates new key material on a schedule. The key identifier stays the same, old material is kept so old data still decrypts, and your code changes nothing. But understand the limit: it does not re-encrypt existing data. That is fine for routine rotation. After an actual compromise, it is not enough. You need a new key and you must re-encrypt what the old one protected.
- turn it on: new key material on a schedule (yearly by default, shorter possible)
- the key id stays the same; old material is kept to decrypt old data
- your code changes nothing
- it does NOT re-encrypt existing data
- after a leak: new key and re-encrypt, not just rotate
Passwords and API keys
Passwords, API keys and tokens rotate with the same idea: two valid at once. Create the new credential while the old still works. Deploy the new one to every client. Watch the logs to see whether the old one is still being used. Disable the old one, wait, then delete it. This needs systems that allow two active credentials per service, which is why database users and cloud access keys support exactly that. A secrets manager automates the whole sequence.
- 1. create the new credential; both old and new work
- 2. deploy the new one to all clients
- 3. watch: is the old one still being used?
- 4. disable the old; wait
- 5. delete it
- needs: two active keys per user or service
- a secrets manager automates it
A rotation policy
A typical policy by type. Master keys in a key service: yearly and automatic. Data keys: per object, replaced when data is rewritten. TLS certificates: ninety days or less. Database and service passwords: thirty to ninety days through a secrets manager. Cloud access keys: ninety days, or better, replace them with short-lived roles so there is nothing to rotate. Signing keys: one to two years. And on any suspicion of compromise: immediately.
- KMS master keys: yearly, automatic
- data keys: per object, or when data is rewritten
- TLS certificates: 90 days or less, automatic
- database and service passwords: 30 to 90 days
- cloud access keys: 90 days; better, none at all on any suspicion: now
When a key leaks
When a key leaks, treat it as an incident and follow a fixed order. Create the new key first. Switch every writer to it. Then revoke or disable the old one. Re-encrypt the data the old key protected. Search the logs for everything the old key accessed while it was exposed. And find how it leaked, so it does not happen again. One point matters most: deleting a secret from a git repository does not un-leak it. Only rotation does.
- 1. create the new key first
- 2. switch all writers to it
- 3. revoke or disable the old one
- 4. re-encrypt data the old key protected
- 5. search logs: what did the old key access?
- 6. find how it leaked; fix it
- removing it from git history is not rotation
Rotate less by design
The best rotation is the one you never have to do. Use short-lived credentials: cloud roles instead of access keys, OIDC tokens for pipelines, workload identity for services. Use dynamic secrets, where a tool like Vault creates a database user for each application, valid for an hour. Issue short-lived certificates from an internal authority. When nothing lives longer than a few hours, a leaked secret is already dead, and rotation happens by itself.
- short-lived credentials: cloud roles, OIDC tokens for CI, workload identity
- dynamic secrets: Vault makes a database user per app, valid for an hour
- short-lived certificates from an internal CA
- nothing long-lived means nothing to rotate or leak
Common mistakes
Mistakes. No key version stored with the data, so nobody knows which key reads what. Deleting the old key before every record is re-encrypted, which destroys data permanently. Rotating by hand, once, for an audit. A rotation that requires a deployment and downtime. A secret rotated in one place and still used in five others. A runbook that has never been tested. And no inventory of what secrets exist and who owns them.
- no key version stored with the data
- deleting the old key before everything is re-encrypted
- rotating by hand, once
- a rotation that needs a deploy and downtime
- rotated in one place, still used in five others
- never testing the runbook
- no inventory of secrets
Say it like this
In the interview: I version the keys and store the version with every ciphertext. New writes use the newest version, and reads use whichever version the record names, so nothing breaks at the moment of rotation. With envelope encryption I rewrap the data keys under the new master in the background, then retire the old version once nothing references it. For credentials I run old and new in parallel. After a compromise I also re-encrypt and review access logs.
- "Version the keys and tag
- every ciphertext with its version. Encrypt with the newest, decrypt with any.
- With envelope encryption, rewrap data keys under the new master in the background, then retire the old version.
- After a leak, re-encrypt."