feat(crypto): support directory encryption key rotation#64
Merged
Conversation
Audit of lib/directory/crypto (closes #56): AES-256-GCM with a random per-call 12-byte IV and verified auth tag is sound. The gap was rotation: ciphertexts carried no way to decrypt under more than one key, so rotating DIRECTORY_ENCRYPTION_KEY would have bricked existing rows. - decryptConfig now tries the current key then an optional DIRECTORY_ENCRYPTION_KEY_PREVIOUS, enabling zero-downtime rotation. - Add scripts/reencrypt-directory-configs.ts (npm run reencrypt:directory) to re-encrypt all stored configs under the current key. - Document the algorithm review, known limitations (sha256 is a normalizer not a KDF; no AAD), and the rotation runbook in docs/encryption.md; note the key-generation guidance in .env.example. - Add crypto unit tests: round-trip, IV uniqueness, tamper rejection, missing-key failure, and previous-key fallback.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Audit of
lib/directory/crypto.tsplus the missing key-rotation support. Closes #56.Audit result
AES-256-GCM, random 12-byte IV per call, verified 16-byte auth tag, stored as
base64(iv|tag|ciphertext). Confidentiality + integrity are sound. Documented limitations:sha256(key)is a normalizer not a password KDF (safe only with a high-entropy random key), and no AAD binding ciphertext to its row.The gap
Ciphertexts had no provision for more than one key, so rotating
DIRECTORY_ENCRYPTION_KEYwould have made every existing row undecryptable.How
decryptConfigtries the current key, then an optionalDIRECTORY_ENCRYPTION_KEY_PREVIOUS→ zero-downtime rotation.encryptConfigalways uses the current key.scripts/reencrypt-directory-configs.ts(npm run reencrypt:directory): idempotent re-encryption of all stored configs under the current key.docs/encryption.md: algorithm review, limitations, and the step-by-step rotation runbook..env.exampledocumentsDIRECTORY_ENCRYPTION_KEY_PREVIOUSand key generation.Closes #56
🤖 Generated with Claude Code