Sub topic 3
Choosing a key type: symmetric, asymmetric or HMAC
This chapter assumes you know the basics of IAM, the AWS CLI, and the difference between plaintext and ciphertext. You should also know what a KMS key is and the difference between AWS owned, AWS managed and customer managed keys. Here you learn how to pick the right type of KMS key before you create one.
The three key types
Every KMS key has a key spec that fixes its type. You choose it at creation time and cannot change it later.
- Symmetric keys use one secret key for both encryption and decryption. The default spec is
SYMMETRIC_DEFAULT, which is AES-256-GCM. The key material never leaves KMS unencrypted. - Asymmetric keys are key pairs, one public and one private. You can download the public key. The private key never leaves KMS. They are used for encrypt/decrypt (RSA) or sign/verify (RSA or ECC).
- HMAC keys generate and verify hash-based message authentication codes. They do not encrypt data. They prove that a message is unchanged and came from someone who holds the key.
When to use each type
| Type | Key usage | Typical use |
|---|---|---|
| Symmetric | ENCRYPT_DECRYPT | Encrypting S3, EBS, RDS and data keys |
| Asymmetric RSA | ENCRYPT_DECRYPT | Outside parties encrypt with your public key |
| Asymmetric RSA or ECC | SIGN_VERIFY | Code signing, document signing |
| HMAC | GENERATE_VERIFY_MAC | Verifying webhooks or tokens |
Choose a symmetric key unless you have a specific reason not to. It is the only type that works with envelope encryption through GenerateDataKey, and it is the only type that AWS services use to encrypt your data. It also supports automatic key rotation. Choose an asymmetric key when the other party cannot call AWS. They can encrypt with the public key, and only your code that calls KMS can decrypt. Choose HMAC when two systems share a secret and need to authenticate messages.
Creating each type
The key spec and key usage are set with create-key.
# Symmetric (the default if you give no spec)
aws kms create-key --description "Symmetric tutorial key"
# Asymmetric RSA key for signing
aws kms create-key \
--key-spec RSA_2048 \
--key-usage SIGN_VERIFY \
--description "Signing key"
# HMAC key
aws kms create-key \
--key-spec HMAC_256 \
--key-usage GENERATE_VERIFY_MAC \
--description "HMAC key"For an asymmetric key, download the public key to share it:
aws kms get-public-key --key-id <key-id>Common errors
- Using the wrong key for an API call.
GenerateDataKeyfails on asymmetric and HMAC keys withInvalidKeyUsageExceptionorUnsupportedOperationException. Check the key spec first. - Expecting rotation on every key. Automatic rotation works only for symmetric encryption keys. Asymmetric and HMAC keys need manual rotation, where you create a new key and move an alias.
- Choosing RSA for bulk data. RSA can encrypt only a few hundred bytes, depending on the key size and algorithm. Use a symmetric key and envelope encryption for larger data.
- Picking a key spec too late. You cannot convert a key from one spec to another. You must create a new key.
Exercise
A partner outside AWS must send you encrypted files, and they cannot call the KMS API. Which key type and key usage do you create, and what do you give the partner?
Show answer
Create an asymmetric RSA key with key usage ENCRYPT_DECRYPT. Give the partner the public key from get-public-key. They encrypt with it, and you decrypt by calling KMS Decrypt, because the private key never leaves KMS. For large files, the partner should encrypt the file with a random AES key and encrypt only that AES key with the public key.