Sub topic 2
Binding ciphertext to its use with AWS KMS
Assume you know basic IAM policies, AWS CLI or Python, and symmetric encryption. This chapter focuses on how KMS adds context to encryption and how access decisions combine.
Binding ciphertext to its use with encryption context
An encryption context is a set of non-secret key-value pairs that describes why ciphertext is being used. KMS authenticates the context, but does not encrypt it. Decryption succeeds only when the supplied context exactly matches the context used during encryption.
For example, purpose=invoice can prevent ciphertext created for one purpose from being reused for another. Do not put passwords or other secrets in the context.
The same context must be supplied when KMS decrypts a ciphertext or an encrypted data key.
Envelope encryption with data keys
KMS direct encryption is intended for small payloads. Envelope encryption handles larger data:
- KMS generates a random data key.
- Your application encrypts the plaintext locally with the plaintext data key.
- KMS returns an encrypted copy of the data key.
- Store the local ciphertext beside the encrypted data key and nonce.
- During decryption, send the encrypted data key and the same context to KMS.
import boto3
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
kms = boto3.client("kms")
context = {"purpose": "demo"}
def decrypt(blob):
response = kms.decrypt(
CiphertextBlob=blob["encrypted_key"],
EncryptionContext=context,
)
return AESGCM(response["Plaintext"]).decrypt(
blob["nonce"], blob["ciphertext"], None
)The application must protect the plaintext data key in memory and discard it after use.
Controlling access with key policies
A KMS key policy is a resource-based policy attached to a KMS key. It identifies principals that may administer or use that key. A common policy lets the account root principal enable IAM policies, then grants an application role only the required cryptographic actions.
{
"Effect": "Allow",
"Principal": {"AWS": "arn:aws:iam::111122223333:role/app-role"},
"Action": ["kms:Encrypt", "kms:Decrypt", "kms:GenerateDataKey"],
"Resource": "*",
"Condition": {
"StringEquals": {"kms:EncryptionContext:purpose": "demo"}
}
}The condition denies requests whose context does not contain the expected value.
Combining IAM policies and grants
For a same-account request, access normally requires an allow in the key policy and an applicable IAM identity policy, unless the key policy grants the principal access directly. An explicit deny overrides every allow.
A grant is a KMS permission delegation, often used by AWS services or applications that need temporary, narrowly scoped access. Grants can allow operations such as Decrypt without changing the main key policy. Plan for brief propagation delays after creating a grant.
Exercise
Why can a decrypt request fail even when the caller has an IAM Allow for kms:Decrypt?
Show answer
The key policy may not delegate access to IAM, the request may have the wrong encryption context, or another policy may contain an explicit Deny.