Skip to content

feat(crypto): support directory encryption key rotation#64

Merged
WhiteMuush merged 1 commit into
mainfrom
security/56-crypto-key-rotation
Jun 19, 2026
Merged

feat(crypto): support directory encryption key rotation#64
WhiteMuush merged 1 commit into
mainfrom
security/56-crypto-key-rotation

Conversation

@WhiteMuush

Copy link
Copy Markdown
Owner

What

Audit of lib/directory/crypto.ts plus 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_KEY would have made every existing row undecryptable.

How

  • decryptConfig tries the current key, then an optional DIRECTORY_ENCRYPTION_KEY_PREVIOUS → zero-downtime rotation. encryptConfig always 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.example documents DIRECTORY_ENCRYPTION_KEY_PREVIOUS and key generation.
  • Unit tests: round-trip, IV uniqueness, tamper rejection, missing-key failure, previous-key fallback.

Closes #56

🤖 Generated with Claude Code

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.
@WhiteMuush WhiteMuush merged commit 7152e46 into main Jun 19, 2026
11 checks passed
@WhiteMuush WhiteMuush deleted the security/56-crypto-key-rotation branch June 19, 2026 07:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Audit provider credentials encryption

1 participant