Lesson 2 of 6 AWS KMS and CloudHSM

Protecting Large Data with Envelope Encryption

This lesson explains envelope encryption, which protects data larger than the 4 KB KMS encrypts directly: data keys encrypt the data, and KMS encrypts the data keys.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe how envelope encryption works.
  • Explain the purpose of an encryption context.
  • Explain how S3 Bucket Keys and data key caching reduce KMS requests.

How it works

KMS can encrypt at most 4 KB of data directly, and sending large amounts of data to KMS would be slow. Envelope encryption solves both problems by using two keys: a data key that encrypts the data locally, and a KMS key that protects the data key.

  1. Generate a data key

    The application calls GenerateDataKey, which returns a plaintext data key and the same data key encrypted under the KMS key.

  2. Encrypt locally

    The application encrypts the data with the plaintext data key, then discards that key from memory.

  3. Store the encrypted key

    The encrypted data key is stored alongside the encrypted data. To decrypt, the application asks KMS to decrypt the data key, then decrypts the data locally.

Only the small data key ever travels to KMS, so data of any size is encrypted quickly. And because the stored data key is itself encrypted, someone who obtains the stored data learns nothing without permission to decrypt the key in KMS. This is how services such as Amazon S3 and Amazon EBS encrypt data with KMS keys.

The following diagram shows the three steps above and the decryption path.

Envelope encryption. The application calls GenerateDataKey and receives a plaintext data key and the same key encrypted under the KMS key. It encrypts the data locally with the plaintext key, discards that key, and stores the encrypted data key with the encrypted data. To decrypt, it asks KMS to decrypt the data key.
The KMS key itself never leaves KMS; only the data key is ever returned in plaintext, and only to the caller that requested it.
Put in order

Envelope encryption

Exam skills 1.3.2

Put the steps of envelope encryption in order.

Log in to try this activity Interactive activities are free with an account.

Encryption context

An encryption context is non-secret key-value data supplied when encrypting and required again to decrypt. It is recorded in CloudTrail and binds ciphertext to its purpose, such as a bucket, table or tenant. Policies can require a particular context with the kms:EncryptionContext condition key.

Request quotas and cost

KMS enforces request quotas per account and Region, and requests beyond them are throttled; every request is also charged. Workloads that encrypt or decrypt at very high rates must therefore reduce the number of KMS calls.

For Amazon S3 with SSE-KMS, S3 Bucket Keys let S3 use a bucket-level key to generate data keys for many objects, which reduces KMS calls, and their cost, by up to 99%. For application data, the AWS Encryption SDK implements envelope encryption and can cache data keys, so many messages are encrypted with one data key instead of a KMS call for each.

Troubleshooting scenario

KMS throttling

Exam skills 1.3.2 · 4.4.6

Resolve KMS throttling and cost problems.

Log in to try this activity Interactive activities are free with an account.
Worked example

KMS throttling from a busy bucket

A data pipeline writes millions of small objects an hour to a bucket encrypted with SSE-KMS. Uploads start failing with KMS throttling errors, and the KMS bill has grown sharply.

CauseFix
Each object needs its own data key from KMSEnable S3 Bucket Keys, so S3 derives object keys from a bucket-level key
Request rate above the Regional KMS quotaBucket Keys cut KMS requests by up to 99%; a quota increase is the alternative

Envelope encryption still protects every object, but KMS is now called rarely. Applications that encrypt data themselves achieve the same effect with the AWS Encryption SDK's data key caching.

Check yourself

Why is data larger than 4 KB not sent to KMS for encryption?

Show answer

KMS encrypts at most 4 KB directly. Larger data is encrypted locally with a data key, and only the small data key is protected by KMS.

Exam Tips

Remember for the exam

  • To encrypt data larger than 4 KB, use envelope encryption with data keys.
  • For KMS throttling or cost from busy SSE-KMS buckets, use S3 Bucket Keys.
  • To bind ciphertext to a particular context and record it in logs, use an encryption context.
  • For client-side encryption of application data, use the AWS Encryption SDK.