Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 38 ยท Network and Data Protection

Key Management: KMS, HSM and Envelope Encryption

Learn how cloud key management services work: key hierarchies, envelope encryption, rotation, key policies, HSMs, BYOK and crypto-shredding, with a runnable envelope encryption model.

Encryption is only as strong as the care taken with the keys. If the key sits next to the data, or everyone can use it, the encryption is theater. Key management is the discipline of creating, storing, using, rotating and retiring keys safely. Cloud providers offer a key management service (KMS) so you do not build this yourself, and understanding how it works helps you configure it correctly.

What a KMS does

A KMS is a managed service that:

  • Generates and stores keys inside protected hardware; the raw key material of the main keys never leaves.
  • Performs cryptographic operations on your behalf: you send data (or a small key) to the service and get an encrypted or decrypted result back.
  • Enforces key policies: which identities may use or manage each key.
  • Logs every use, so you can see who decrypted what.
  • Rotates keys automatically if you choose.

Two kinds of keys matter most: customer master keys (also called KEKs, key-encryption keys) that stay in the KMS, and data keys that encrypt your actual data and are themselves protected by a master key.

Envelope encryption

Sending gigabytes of data to the KMS for every operation would be slow and expensive. Instead, services use envelope encryption:

  1. Ask the KMS for a fresh data key. It returns the key in plain form and encrypted under the master key.
  2. Encrypt your data locally with the plain data key.
  3. Store the encrypted data key next to the encrypted data, and discard the plain key from memory.
  4. To read the data later, send the encrypted data key to the KMS (the call is logged and checked against the key policy), get the plain data key back, and decrypt locally.
sequenceDiagram
    participant App as Application
    participant K as KMS
    participant S as Storage
    App->>K: GenerateDataKey(master key id)
    K-->>App: plain data key + encrypted data key
    App->>App: Encrypt data with plain key, wipe plain key
    App->>S: Store ciphertext + encrypted data key
    Note over App,S: Later, to read
    App->>S: Fetch ciphertext + encrypted data key
    App->>K: Decrypt(encrypted data key)
    K-->>App: plain data key (policy checked and logged)
    App->>App: Decrypt data

Only small keys travel to the KMS; the master key never leaves it.

Benefits: speed (bulk work is local), a unique key per object or per file (so one leaked data key exposes one object), and central control: disabling the master key makes every wrapped data key unusable.

A model of envelope encryption

The program below simulates the flow with a tiny fake KMS class. The cipher is the same teaching construction as in the previous lesson (HMAC-based keystream plus an HMAC tag); in real systems you use AES-GCM from a vetted library, and the KMS is an actual service. What matters here is the structure.

Python Envelope encryption with a fake KMS

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice three properties of the design: the ciphertext and the wrapped key can sit together in ordinary storage without risk, the key policy decides who can unwrap the key, and disabling the master key renders the stored data permanently unreadable, a technique called crypto-shredding that is useful when you must delete data you cannot easily find everywhere (copies, backups).

Key policies and separation of duties

Key access is controlled separately from data access. Good practice:

  • Key administrators (who can create, rotate, disable keys) are not the same people as key users (who can encrypt and decrypt).
  • Allow a key to be used only by specific roles and through specific services, using conditions such as "only via the storage service in this account".
  • One key per data domain or application, not one key for the whole company; this limits blast radius and makes audit clearer.
  • Alert on key deletion, disabling and policy changes. Use deletion waiting periods, because deleting a key destroys all data it protects.

Rotation

  • Automatic rotation of master keys (for example yearly) is built into most KMS products. Old versions are kept to decrypt old data while new data uses the new version.
  • Rotating data keys happens naturally when you generate a new one per object.
  • Rotation after exposure is manual and urgent: re-encrypt data under a new key and retire the old one.

HSMs, BYOK and external keys

  • Hardware security modules (HSMs) are tamper-resistant devices that generate and hold keys and perform crypto inside the box. Cloud KMS products are backed by HSMs; dedicated single-tenant HSM services give you exclusive hardware for strict compliance needs (often described by standards such as FIPS 140).
  • Bring your own key (BYOK) means you generate key material yourself and import it, mainly for regulatory reasons. You then must protect the original and handle its lifecycle.
  • External key stores / hold your own key (HYOK) keep the keys outside the provider entirely and call out to them; control is maximum, but availability and performance now depend on your key service.
  • Confidential computing and enclaves let a workload decrypt data only inside attested, isolated hardware.

Start simple. Provider-managed keys are fine for low-sensitivity data. Move to customer-managed keys for regulated or high-value data, and reserve BYOK, HYOK and dedicated HSMs for cases with a clear requirement, since each adds operational risk.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In envelope encryption, what encrypts the actual data?

    Data keys encrypt data locally; the master key in the KMS wraps those data keys.
  2. 2. What is crypto-shredding?

    Without the key, ciphertext is useless, including in copies and backups.
  3. 3. Why separate key administrators from key users?

    Separation of duties limits insider and compromised-account risk.
  4. 4. What does a hardware security module do?

    HSMs protect key material so it is never exposed in normal memory.

Summary

  • A KMS generates, protects, uses and audits keys; master keys stay inside it.
  • Envelope encryption encrypts data locally with a data key and wraps that key with a master key, giving speed, per-object keys and a central kill switch.
  • Separate key administrators from key users, use one key per domain, alert on key changes and rotate regularly.
  • Use HSMs, BYOK or external keys only where a requirement justifies the extra complexity.

Next: Data Classification, DLP and Backup Protection