Sub topic 6
Binding ciphertext to its use with encryption context
This chapter assumes you can run the AWS CLI, know what IAM roles and policies are, and have created the alias/tutorial-key key from earlier chapters.
What encryption context is
Encryption context is a set of non-secret key-value pairs that you pass to KMS when you encrypt. KMS binds the pairs to the ciphertext as additional authenticated data (AAD). To decrypt, you must supply exactly the same pairs. If any pair differs, is missing, or is extra, decryption fails.
This ties a ciphertext to its intended use. A value encrypted for purpose=demo cannot be decrypted by code that asks for purpose=billing, even if that code has permission to use the key.
KMS does not store the context. You must keep it, or be able to rebuild it, when you decrypt. Typical values are a tenant ID, a table name, or a file ID.
Seeing it work
This example reuses the CLI calls from the direct encrypt chapter. It encrypts with one context and then tries to decrypt with another.
echo -n "my secret" > secret.txt
aws kms encrypt \
--key-id alias/tutorial-key \
--plaintext fileb://secret.txt \
--encryption-context purpose=demo \
--query CiphertextBlob \
--output text | base64 --decode > secret.enc
# Correct context: succeeds
aws kms decrypt \
--ciphertext-blob fileb://secret.enc \
--encryption-context purpose=demo \
--query Plaintext \
--output text | base64 --decode
# Wrong context: fails with InvalidCiphertextException
aws kms decrypt \
--ciphertext-blob fileb://secret.enc \
--encryption-context purpose=billingThe second call returns an InvalidCiphertextException. KMS does not reveal which part of the context was wrong.
Using context in policies
The context is also visible to KMS when it evaluates policies. You can require it with a condition key. The key policy shown earlier did this:
{
"Condition": {
"StringEquals": { "kms:EncryptionContext:purpose": "demo" }
}
}With this condition, the role can call Encrypt, Decrypt and GenerateDataKey only when the request carries purpose=demo. If the role sends no context, or a different one, KMS denies the request.
Two related condition keys are worth knowing:
kms:EncryptionContextKeyschecks which context keys are present, without checking their values.kms:EncryptionContext:<key>checks the value of one specific key, as above.
Context in logs
CloudTrail records the encryption context for every KMS call. This helps you answer questions such as which tenant's data was decrypted. It also means you must never put secrets in the context. Treat it as a label, not as data.
Common errors
- Forgetting the context on decrypt. The call fails even though the ciphertext and key are correct. Store the context, or derive it from data you already hold.
- Using a changing value. A timestamp or random ID in the context is lost unless you save it. Use stable identifiers.
- Putting sensitive data in it. The context appears in CloudTrail in plaintext.
- Expecting it to work as a secret. It adds integrity and authorization checks. It does not add confidentiality.
Exercise
A role is allowed to call kms:Decrypt only when kms:EncryptionContext:tenant equals "acme". Your code encrypted a value with the context tenant=acme, then calls Decrypt with no context. What happens, and why?
Show answer
The call fails. The policy condition is not met because the request has no tenant value, so KMS denies it. Even if the policy allowed it, KMS would reject the call with InvalidCiphertextException, because the context at decryption must match the context used at encryption.