Designing key management strategy
I'm looking to propose a key management strategy. It seems to need an overall design that includes envelope encryption, the use of key encryption keys (KEK) and data encryption keys (DEK), along with policies for rotation and recovery.
I should mention using Hardware Security Modules (HSM) and Key Management Services (KMS), and things like key hierarchy, data key generation, and caching. Recovery plans should cover backups, key escrow, and disaster recovery as well. I'll also note best practices focusing on access controls and audits.Structuring recovery strategy
I need to handle recovery, especially regarding lost Key Encryption Keys (KEKs), which could be fatal. I'll implement KMS backups and replication, ensuring exportability if on-prem HSM is used. It’s crucial to store wrapped KEK backups in escrow and require an M-of-N split knowledge approach.
Rekeying is necessary if there's a compromise, involving incident response to revoke and rewrap. I’ll create a structured plan with sections on rotation schedules and recovery procedures. The algorithm choice should include AES-256-GCM with proper handling of IVs and context binding. Access policies are also important for principals and encryption operations.
1) Key hierarchy (envelope encryption)
Goal: never store or transmit raw data-encryption keys (DEKs) unprotected; keep key-encryption keys (KEKs) in a hardened key service (KMS/HSM) with strict access control and audit.
Recommended hierarchy
- Root of trust (KMS/HSM master key)
- Managed by cloud KMS or an HSM cluster. Non-exportable when possible.
- KEKs (wrapping keys)
- One per environment (prod/stage/dev) at minimum.
- Often also per tenant / per data domain if you need isolation and selective destruction.
- Stored and used inside KMS/HSM (wrap/unwrap operations).
- DEKs (data keys)
- Generated per object/file/rowset/batch (granularity depends on performance and blast radius).
- Used with an AEAD cipher (e.g., AES-256-GCM or ChaCha20-Poly1305) to encrypt the data.
- Immediately wrapped (encrypted) by a KEK; plaintext DEK kept only in memory briefly.
What to store with each ciphertext (minimum)
ciphertext
wrapped_dek (DEK encrypted under KEK)
kek_id + kek_version
alg (e.g., AES-256-GCM)
iv/nonce + tag (if applicable)
aad (optional but recommended): context bound into AEAD, e.g. {tenant_id, object_id, purpose, schema_version}
Encrypt flow (high level)
- App requests a new DEK (either locally via CSPRNG or via KMS “GenerateDataKey”).
- Encrypt data with DEK using AEAD (unique nonce per encryption).
- Wrap DEK with KEK in KMS/HSM.
- Persist ciphertext + wrapped DEK + key metadata.
Decrypt flow
- Read
kek_id/version + wrapped_dek.
- Ask KMS/HSM to unwrap DEK (policy-controlled).
- Decrypt ciphertext with DEK (verify tag/AAD).
- Zeroize DEK in memory promptly.
2) Rotation strategy (DEK and KEK)
A) DEK rotation (frequent, cheap, limits blast radius)
When: per object write, per file, per partition, or time-based (e.g., daily for log segments).
How:
- For new writes, always use a fresh DEK.
- For existing data, choose one:
- Rotate-on-rewrite (common): only re-encrypt when the record/object is modified.
- Background re-encryption (stronger): schedule re-encrypt of older data (expensive for large datasets).
Why it helps: even if a DEK leaks (memory compromise, bug), it exposes only that object/segment.
B) KEK rotation (less frequent, operationally sensitive)
Rotate KEKs to reduce risk of long-lived key exposure and to meet compliance.
Two common patterns:
Pattern 1: “Rewrap only” (recommended for large datasets)
- Create KEK v2 in KMS/HSM.
- New encrypt operations use v2 to wrap DEKs.
- Existing data:
- Do not re-encrypt data.
- Run a background job to unwrap DEK with old KEK v1 and rewrap DEK with KEK v2 (data ciphertext unchanged).
- Keep v1 enabled for decrypt during migration; later set v1 to “decrypt-only”, then disable.
Pros: fast and low-cost (rewrapping small DEKs, not huge data).
Cons: if you believe KEK v1 was compromised, you may need deeper response (see “compromise recovery”).
Pattern 2: “Full re-encrypt” (strongest, most expensive)
- Generate new DEKs and re-encrypt data, then wrap those DEKs under the new KEK.
- Use when you suspect DEKs or ciphertext integrity is at risk, or policy demands full re-encryption.
Operational rotation mechanics (avoid outages)
- Version everything:
kek_id + kek_version stored with each wrapped DEK.
- Dual-read, single-write:
- Reads: can unwrap with any allowed older KEK version.
- Writes: always use current KEK version.
- Phased retirement:
- Introduce new KEK version.
- Migrate (rewrap) in background with progress tracking.
- Enforce “no new wraps with old KEK”.
- After migration + safety window, disable old KEK version.
- Later, delete old KEK version only if you are certain no data depends on it.
Rotation cadence (example starting point)
- DEKs: per object/segment (effectively continuous)
- KEKs: every 90–180 days, or per compliance requirement
- Immediate rotation after staff changes, suspected compromise, or policy triggers
3) Recovery and resiliency (don’t lose keys, don’t overexpose them)
A) Prevent catastrophic loss
If KEKs are non-exportable and you lose KMS/HSM state, encrypted data may be unrecoverable. Mitigate with:
- Use managed KMS with multi-AZ/multi-region capability where possible.
- Replicate keys (or enable multi-region keys) so decrypt can proceed in DR region.
- For self-managed HSM:
- Use HA HSM clusters with secure backup/restore procedures.
- Store HSM backups in multiple secured locations.
- Test restores regularly.
B) Access recovery (people/process)
- Enforce separation of duties:
- Operators cannot both deploy code and change key policy.
- Use break-glass roles:
- Highly restricted, monitored, time-bound access to decrypt capabilities.
- Store break-glass credentials via:
- Hardware tokens, offline vault, and M-of-N approval.
C) Key escrow / quorum (only if you must)
If export of KEKs is required for business continuity (often discouraged), do it with strong controls:
- Use Shamir Secret Sharing (M-of-N) for escrow material.
- Store shares in independent systems/physical safes under separate custodians.
- Require documented ceremony + audit trail to reconstruct.
D) Recovery from compromise (incident response)
Define playbooks depending on what is suspected:
-
App/service credential compromise (but KEKs safe in KMS)
- Revoke service credentials/roles immediately.
- Rotate IAM policies, tokens, and redeploy.
- Consider DEK rotation on rewrite for high-value datasets.
-
KEK compromise suspected
- Create new KEK version immediately.
- Rewrap all DEKs under new KEK (priority job).
- If there’s a chance attackers obtained plaintext DEKs or ciphertext+DEK pairs, consider full data re-encryption.
-
DEK compromise for specific objects
- Re-encrypt those objects with new DEKs and update wrapped DEKs.
- Investigate nonce reuse, logging leaks, memory dumps, etc.
E) Backups of encrypted data
- Back up ciphertext + wrapped DEK + metadata together.
- Ensure DR restores include:
- Access to the same KMS key IDs/versions (or replicated equivalents),
- IAM policies allowing unwrap in DR,
- Configuration for key aliases and version mapping.
4) Guardrails and implementation details that matter
- Use AEAD (AES-GCM / ChaCha20-Poly1305) and verify tags; don’t use “encrypt-then-MAC” incorrectly.
- Nonce/IV uniqueness: enforce random or monotonic nonces per key; never reuse a nonce with the same DEK.
- AAD binding: include tenant/object identifiers so ciphertext can’t be swapped across contexts.
- Minimize plaintext key exposure:
- Keep DEKs in memory only briefly; zeroize buffers where possible.
- Avoid logging wrapped keys, ciphertext metadata that aids attackers, or plaintext.
- Strict KMS policies:
- Only the encrypt/decrypt service role can unwrap.
- Separate roles for “wrap-only” vs “unwrap” if your workflow allows.
- Audit + alerting:
- Alert on unusual unwrap volume, access from new networks, policy changes, disabled key versions.
- Key usage boundaries:
- Don’t reuse KEKs for unrelated purposes (e.g., signing + wrapping).
- Consider per-tenant KEKs if you need tenant-level kill switch and blast-radius isolation.
If you share your environment (cloud KMS vs on-prem HSM), data shape (objects/files/db rows/streams), multi-tenant needs, and RTO/RPO targets, I can propose a concrete rotation cadence, metadata schema, and a migration plan (rewrap job design, safety checks, and rollback strategy).