Sub topic 1
Encrypting data with AWS KMS
Prerequisites
Assume you know basic IAM policies, the AWS CLI, and the difference between plaintext and ciphertext. You also need permission to create and use KMS keys in an AWS account.
What KMS is and what it protects
AWS Key Management Service (KMS) creates and controls cryptographic keys. KMS uses these keys to encrypt small values directly, generate data keys for local encryption, and authorize key use. It also records key-related API activity in AWS CloudTrail.
A KMS key is different from a data key. A KMS key is managed by KMS. A data key is a temporary encryption key that your application can use locally for larger data. KMS can encrypt the data key before your application stores it.
Who owns the key
KMS offers three ownership models:
| Key type | Who manages it | Typical use |
|---|---|---|
| AWS owned | AWS | Default encryption for many services |
| AWS managed | AWS, for an AWS service in your account | Service-specific encryption |
| Customer managed | You control policy and lifecycle | Application-specific access and compliance |
Customer managed keys provide the most control. You can define their policies, aliases, rotation settings, and deletion schedule.
Choosing a key type
Use a symmetric KMS key for most application and AWS service encryption. The same protected key material handles encryption and decryption.
Use an asymmetric key when one party encrypts or verifies with a public key and another uses the private key. Use an HMAC key for message authentication, not for recovering encrypted plaintext. These key types support different KMS operations.
Referring to keys
A key ID identifies a key within an account and Region. A key ARN includes the account and Region, so it is useful in policies. An alias is a readable name such as alias/tutorial-key. Aliases make configuration easier, but they can be changed to point to another key.
Encrypting and decrypting small payloads directly
The symmetric Encrypt API is intended for small plaintext values, up to 4,096 bytes. Larger data should use envelope encryption with a data key.
KEY_ID=$(aws kms create-key \
--description "Tutorial key" \
--query KeyMetadata.KeyId \
--output text)
aws kms create-alias \
--alias-name alias/tutorial-key \
--target-key-id "$KEY_ID"
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
aws kms decrypt \
--ciphertext-blob fileb://secret.enc \
--encryption-context purpose=demo \
--query Plaintext \
--output text | base64 --decodeExercise
Which KMS key type should normally protect application data, and why?
Show answer
Use a symmetric key because KMS supports direct encryption and decryption with it, and AWS services commonly integrate with symmetric keys.