Sub topic 7
Envelope encryption with data keys
This chapter assumes you can run the AWS CLI and read Python, you know the difference between plaintext and ciphertext, and you have the alias/tutorial-key key from earlier chapters. You also need boto3 and cryptography installed.
Why direct encryption is not enough
The KMS Encrypt call accepts at most 4,096 bytes of plaintext. It also sends your data over the network to KMS. For anything larger, or for data you encrypt often, you use envelope encryption.
Envelope encryption uses two kinds of keys:
- A KMS key stays inside KMS and never leaves it.
- A data key is a normal AES key that KMS generates for you. You use it locally to encrypt your data.
You keep the data key's plaintext only while you work. What you store is the data encrypted with the data key, plus a copy of the data key encrypted under the KMS key.
How it works
GenerateDataKey returns two copies of the same key. Plaintext is for immediate use. CiphertextBlob is the same key encrypted under your KMS key, and it is safe to store.
A worked example
The code below encrypts any amount of data with AES-256-GCM. Only the small data key travels to KMS.
import os
import boto3
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
kms = boto3.client("kms")
CONTEXT = {"purpose": "demo"}
def encrypt(plaintext: bytes) -> dict:
resp = kms.generate_data_key(
KeyId="alias/tutorial-key",
KeySpec="AES_256",
EncryptionContext=CONTEXT,
)
nonce = os.urandom(12)
ciphertext = AESGCM(resp["Plaintext"]).encrypt(nonce, plaintext, None)
return {
"encrypted_key": resp["CiphertextBlob"],
"nonce": nonce,
"ciphertext": ciphertext,
}
def decrypt(blob: dict) -> bytes:
resp = kms.decrypt(
CiphertextBlob=blob["encrypted_key"],
EncryptionContext=CONTEXT,
)
return AESGCM(resp["Plaintext"]).decrypt(blob["nonce"], blob["ciphertext"], None)Try it:
blob = encrypt(b"a payload of any size")
print(decrypt(blob))Points to notice:
KeySpec="AES_256"asks for a 32-byte key, which is what AES-256 needs.- The nonce is random and unique for each encryption. It is not secret, so you store it with the ciphertext.
decryptpasses no key ID. The encrypted data key records which KMS key protects it.- The same encryption context must be supplied on both calls.
Common errors
| Symptom | Likely cause |
|---|---|
| `InvalidCiphertextException` on `kms.decrypt` | The encryption context differs from the one used at generation, or the blob was changed. |
| `AccessDeniedException` | The caller lacks `kms:GenerateDataKey` or `kms:Decrypt` on the key. |
| `InvalidTag` from `AESGCM` | The ciphertext, nonce, or data key does not match. |
| Decryption fails after a restart | The encrypted key, nonce, or ciphertext was not stored with the data. |
Exercise
Why does the stored result contain the encrypted data key, and why is it safe to store it next to the ciphertext?
Show answer
Without it you could not recover the data key later, so you could not decrypt the data. It is safe because it is encrypted under the KMS key, and only a caller allowed to use that key can ask KMS to decrypt it.
Exercise
Your application encrypts a 2 GB file. Which parts of the work go to KMS, and which stay local?
Show answer
Only the GenerateDataKey call and, later, the Decrypt call for the small data key go to KMS. The 2 GB of data is encrypted and decrypted locally with AES-GCM.