Encryption and managed keys
What encryption protects
Encryption converts readable plaintext into ciphertext using a cryptographic algorithm and key. A bookstore needs to protect customer records stored on disks and database backups (data at rest) and requests traveling between browsers, gateways and services (data in transit). TLS protects a connection between its endpoints; it does not automatically encrypt the destination disk. Disk encryption protects stored bytes, not every query made by an authorized application. Data in memory may be plaintext while the application processes it. Encryption must be paired with identity, authorization, integrity checks and audit controls.
A key management service (KMS) manages cryptographic keys and operations such as wrapping, unwrapping, encryption or signing, depending on the key type. A hardware security module (HSM) is a protected cryptographic processing boundary. HSM-backed keys can reduce key exposure, but an authorized malicious request may still misuse a key. Neither a KMS nor an HSM replaces access control.
Envelope encryption: follow one record
A data encryption key (DEK) protects application data. A key encryption key (KEK) protects the DEK. Rather than sending every large record through a remote key service, an application or integrated storage service can encrypt the data with an appropriate authenticated encryption implementation and store the wrapped DEK beside the ciphertext. The KEK is managed separately. The nonce, authentication tag, algorithm identifier and key reference are needed according to the selected format; they are not secret keys. Nonces must meet the algorithm's uniqueness requirements. Use reviewed libraries and provider SDKs, not homemade encryption.
write: record + DEK -> authenticated ciphertext DEK + KEK -> wrapped DEKstore: ciphertext + wrapped DEK + required metadataread: authorized unwrap -> DEK -> verify and decrypt recordFor example, imagine a bookstore creates an encrypted backup with a data key protected by key version A. A new backup later uses key version B. Restoring the first backup still depends on the older key material unless the wrapping or encryption has been migrated appropriately. This diagram is symbolic; no cryptographic operations are being performed. Plaintext data keys must be protected in memory and never logged or persisted in unprotected form. In production, prefer the managed service's documented integration or a vetted encryption SDK.
Provider-managed and customer-managed keys
Provider-managed encryption may reduce operational work; customer-managed keys give the customer more control over key permissions, rotation and lifecycle at the cost of additional dependencies. A customer-managed key does not necessarily mean the customer holds raw key bytes outside the provider. Imported or externally managed keys have separate custody and availability implications. Choose based on data classification, audit obligations, recovery requirements and supported service integration, not on the assumption that more control always improves reliability.
- AWS KMS manages keys; key policies, IAM policies and grants participate in authorization. A grant can delegate a limited set of operations to a service or principal. Limit the ability to create grants as well as the cryptographic operations themselves.
- Azure Key Vault handles keys, secrets and certificates. Managed HSM is a distinct service for protected cryptographic keys, not a general secret or certificate vault. Key types, operations and supported integrations differ by edition.
- Google Cloud KMS organizes cryptographic keys in key rings and tracks key versions. Customer-managed encryption keys (CMEK) are supported by selected services; each integration determines which resources can use them and how rotation affects existing data.
Separate a key administrator from a workload that unwraps keys. Give the runtime only the cryptographic permissions and resource scope it needs. Audit grants, key policy changes and denied requests. Review the provider's actual policy evaluation model from Chapter 31 before implementing any policy.
Rotation, rewrapping and retention
Key rotation introduces new key material for future operations according to the service and key type. Rewrapping replaces the protection of a DEK under a different KEK without necessarily encrypting the entire payload again. Re-encryption changes the encryption of the data itself. These are distinct operations. Automatic rotation for eligible AWS KMS keys retains prior key material and does not automatically re-encrypt data or replace existing DEKs. Google Cloud KMS rotation creates a new version; existing ciphertext may continue depending on older versions. Azure integrations can handle version changes differently; inspect the particular storage or database service.
The bookstore retains backups for 90 days in this fictional scenario. At day 30, the current KEK changes from A to B. An old backup created at day 20 remains retained until day 110. Removing A at day 31 would risk making that backup unusable. Record each dependency, including replicas, snapshots, archived copies and disaster recovery environments. Test restoration and access using approved non-production data. Protect keys against unintended deletion and configure supported recovery safeguards, such as Azure soft delete and purge protection. A disabled key or a key pending deletion may already interrupt operations. Permanent loss of necessary material can make data unrecoverable; do not perform deletion as a practice exercise.
Estimate KMS calls, not just storage bytes
Use hypothetical rates only: $1 per managed key per month and $0.03 per 10,000 billed operations, with no included allowance. A workload uses three keys and performs 120,000 billed operations in a month. Key cost = 3 × $1 = $3. Request cost = 120,000 / 10,000 × $0.03 = $0.36. Total = $3.36 before taxes and other service charges. Actual prices, included usage, key versions, HSM fees, request types and billing increments vary.
Envelope encryption can reduce remote calls for bulk data, but call volume depends on the implementation and caching policy. Do not cache plaintext keys indiscriminately or weaken isolation to reduce cost. A key service outage, denied permission, quota exhaustion or unavailable external key may block writes and restores. Plan for retries with bounds and monitor latency and errors. Do not treat an encrypted backup as recoverable until a restore has succeeded.
Practice and next steps
Trace a backup from plaintext through its DEK, wrapped key and stored ciphertext. Mark the identity allowed to unwrap, the key custodian, the backup owner and the evidence needed before retiring the older key. Compare the cost worksheet with your own calculation. Use the linked key-management reference and the chapter practice task. For cryptographic algorithm details, consult the Cybersecurity track when available. Next, distinguish keys from application secrets and TLS certificates.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Encryption and managed keys — knowledge check
Choose the best answer for each offline scenario.
What protects the DEK in envelope encryption?
What happens to an old AWS KMS-encrypted backup after eligible automatic key rotation?
Which control helps reduce accidental permanent key loss?
Under the fictional rates, what is the monthly cost of three $1 keys and 120,000 operations at $0.03 per 10,000?
Why is encryption at rest insufficient on its own?