Lesson 2 of 4 AWS KMS

Encrypting Large Data with Envelope Encryption

This lesson explains envelope encryption, the technique used for data larger than the 4 KB that the KMS Encrypt API accepts. Data is encrypted locally with a data key, and only the data key is encrypted by KMS.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain how GenerateDataKey and Decrypt are used to encrypt and decrypt large data.
  • Distinguish Encrypt, GenerateDataKey and GenerateDataKeyWithoutPlaintext.
  • Explain how an encryption context binds ciphertext to its purpose.
  • Describe how the AWS Encryption SDK and data key caching help with KMS request quotas.

How envelope encryption works

The Encrypt API handles at most 4 KB, which suits small secrets such as passwords. Larger data uses envelope encryption:

  1. Generate a data key

    Call GenerateDataKey to get a plaintext data key and an encrypted copy of it.

  2. Encrypt locally

    Encrypt the data with the plaintext key, then discard the plaintext key.

  3. Store the encrypted key

    Store the encrypted data key alongside the data.

  4. Decrypt later

    Call Decrypt on the encrypted data key and use the result to decrypt the data.

import os, boto3
from cryptography.hazmat.primitives.ciphers.aead import AESGCM

kms = boto3.client("kms")

# Encrypt
key = kms.generate_data_key(KeyId="alias/app-data", KeySpec="AES_256")
nonce = os.urandom(12)
ciphertext = AESGCM(key["Plaintext"]).encrypt(nonce, document_bytes, None)
save(ciphertext, nonce, encrypted_key=key["CiphertextBlob"])

# Decrypt: KMS finds the key from metadata inside the encrypted data key
plaintext_key = kms.decrypt(CiphertextBlob=stored["encrypted_key"])["Plaintext"]
document_bytes = AESGCM(plaintext_key).decrypt(stored["nonce"], stored["ciphertext"], None)

Only the small data key travels to KMS, so files of any size encrypt quickly and the data never leaves the application unencrypted.

4KB

Maximum data for the Encrypt API

Related KMS operations

Besides GenerateDataKey, envelope encryption uses a small set of related operations. Encrypt and Decrypt work directly on data of up to 4 KB and suit small secrets; Decrypt is also how an encrypted data key is turned back into a plaintext key, and for symmetric keys the ciphertext identifies the KMS key, so it need not be specified.

GenerateDataKeyWithoutPlaintext returns only the encrypted copy of a new data key. It suits a component that prepares keys for later use but should never handle plaintext keys itself; whichever component later encrypts the data calls Decrypt first. ReEncrypt decrypts ciphertext under one KMS key and encrypts it under another entirely inside KMS, so the plaintext is never exposed to the caller, which is useful when moving data to a new key. The following table summarises the operations.

OperationUse it to
Encrypt / DecryptEncrypt or decrypt up to 4 KB directly
GenerateDataKeyGet a data key to use now, in plaintext and encrypted
GenerateDataKeyWithoutPlaintextGet only the encrypted data key, for a component that will encrypt later
ReEncryptRe-encrypt ciphertext under a different key without exposing the plaintext

Encryption context

An encryption context is a set of non-secret key-value pairs passed to Encrypt, GenerateDataKey and Decrypt. Decryption fails unless the same context is supplied, and the context appears in CloudTrail. This binds ciphertext to its purpose, such as a particular tenant or record, and makes its use auditable.

key = kms.generate_data_key(KeyId="alias/app-data", KeySpec="AES_256",
                            EncryptionContext={"tenant": "acme", "record": "invoice-9017"})

The AWS Encryption SDK and request quotas

The AWS Encryption SDK implements envelope encryption, a standard message format that stores the encrypted data keys with the ciphertext, and data key caching, so you do not need to write cryptographic code yourself.

KMS enforces request quotas per Region and operation; exceeding them returns ThrottlingException. Reduce calls by caching data keys with the Encryption SDK, enabling S3 Bucket Keys for SSE-KMS, retrying with backoff, or requesting a quota increase.

Worked example

Encrypting a 50 MB file

A developer tries to encrypt a 50 MB file with the KMS Encrypt API and receives an error. KMS encrypts at most 4 KB of data directly; larger data uses envelope encryption:

StepWhat happens
1Call GenerateDataKey: KMS returns a plaintext data key and the same key encrypted
2Encrypt the file locally with the plaintext data key (AES-GCM)
3Discard the plaintext key; store the encrypted key with the file
4To decrypt: send the encrypted key to KMS Decrypt, then decrypt the file locally

Only a few hundred bytes ever travel to KMS, so large files are fast to encrypt and the KMS request quota is not a bottleneck. The AWS Encryption SDK implements these steps for you.

Check yourself

What does an encryption context add to an Encrypt or GenerateDataKey call?

Show answer

Additional authenticated data: the same context must be supplied to decrypt, so the ciphertext cannot be used out of context. The context is logged in CloudTrail, so it must not contain secrets.

Exam Tips

Remember for the exam

  • To encrypt data larger than 4 KB, use envelope encryption with GenerateDataKey.
  • A component that needs a data key only for later use calls GenerateDataKeyWithoutPlaintext.
  • To bind ciphertext to a tenant or record and log its use, supply an encryption context.
  • KMS throttling is solved with data key caching, S3 Bucket Keys or a quota increase.
  • For envelope encryption without writing cryptographic code, use the AWS Encryption SDK.