Sub topic 3
Choosing a key type: symmetric, asymmetric or HMAC
This chapter assumes you can run AWS CLI commands, have read the earlier chapters on KMS keys and key ownership, and know the difference between encrypting, signing and hashing. By the end you should be able to pick the right KMS key type for a job and create it correctly the first time.
Key spec and key usage are fixed at creation
Every KMS key has two properties that you choose when you create it and can never change:
- The key spec is the cryptographic algorithm family and size, such as
SYMMETRIC_DEFAULT,RSA_2048,ECC_NIST_P256orHMAC_256. - The key usage is the set of operations the key may perform:
ENCRYPT_DECRYPT,SIGN_VERIFY,GENERATE_VERIFY_MACorKEY_AGREEMENT.
If you pick the wrong spec or usage, you create a new key and migrate to it. Because of this, choosing the key type is a design decision, not a detail to fix later.
If you omit both, create-key makes a symmetric encryption key. That is the key from the earlier examples in this tutorial.
The three families
| Family | Example key specs | Key usage | Where the key material lives |
|---|---|---|---|
| Symmetric | `SYMMETRIC_DEFAULT` (AES-256-GCM) | `ENCRYPT_DECRYPT` | Inside KMS only |
| Asymmetric | `RSA_2048`, `RSA_3072`, `RSA_4096`, `ECC_NIST_P256`, `ECC_SECG_P256K1`, `SM2` (China Regions) | `ENCRYPT_DECRYPT`, `SIGN_VERIFY` or `KEY_AGREEMENT`, depending on the spec | Private key inside KMS, public key downloadable |
| HMAC | `HMAC_224`, `HMAC_256`, `HMAC_384`, `HMAC_512` | `GENERATE_VERIFY_MAC` | Inside KMS only |
In every family the secret key material never leaves KMS unencrypted. What differs is which operations you can ask KMS to perform and who else can take part.
Symmetric keys: the default for almost everything
A symmetric key uses one secret for both encryption and decryption. It is the only type that works with the whole set of KMS features this tutorial builds on:
GenerateDataKeyand envelope encryption need a symmetric key.- AWS services such as S3, EBS, RDS and Secrets Manager encrypt with symmetric keys. Most service integrations reject asymmetric and HMAC keys.
- Encryption context is supported, because the algorithm is authenticated encryption (AES-GCM) and the context is bound in as additional authenticated data.
- Automatic rotation is available for customer managed symmetric keys.
The direct Encrypt call accepts at most 4,096 bytes of plaintext. For anything larger, use envelope encryption.
Unless you have a specific reason listed below, choose a symmetric key.
Asymmetric keys: when the other party is not in AWS
An asymmetric KMS key is a public/private key pair. The private key stays in KMS. You can download the public key with GetPublicKey and give it to anyone. Choose this type when a party that cannot call KMS must take part in the operation.
Three usages exist, and which are allowed depends on the key spec:
SIGN_VERIFY(RSA and ECC). KMS signs with the private key, and anyone with the public key verifies the signature offline. This suits code signing, signed documents and tokens.ENCRYPT_DECRYPT(RSA and SM2). A remote party encrypts with the public key, and only KMS can decrypt. This suits a partner who sends you data but has no AWS account.KEY_AGREEMENT(ECC). KMS derives a shared secret from your private key and another party's public key, using theDeriveSharedSecretoperation.
A key can have only one usage. An RSA key created for signing cannot also decrypt.
Size limits of asymmetric operations
RSA encryption can only encrypt a payload smaller than the key. With OAEP and SHA-256, the maximum plaintext is 190 bytes for RSA_2048, 318 bytes for RSA_3072 and 446 bytes for RSA_4096. This is why RSA is used to wrap a small symmetric key, not bulk data. Sign accepts a message of up to 4,096 bytes. For larger content, hash it yourself and pass --message-type DIGEST.
Creating and using a signing key
The following commands create an RSA signing key, sign a file, and verify the signature through KMS.
aws kms create-key \
--key-spec RSA_2048 \
--key-usage SIGN_VERIFY \
--description "Tutorial signing key"
aws kms create-alias \
--alias-name alias/tutorial-signing \
--target-key-id <key-id>
echo -n "release-1.4.2" > msg.txt
aws kms sign \
--key-id alias/tutorial-signing \
--message fileb://msg.txt \
--message-type RAW \
--signing-algorithm RSASSA_PSS_SHA_256 \
--query Signature \
--output text | base64 --decode > msg.sig
aws kms verify \
--key-id alias/tutorial-signing \
--message fileb://msg.txt \
--message-type RAW \
--signing-algorithm RSASSA_PSS_SHA_256 \
--signature fileb://msg.sigverify prints "SignatureValid": true. If the signature does not match, the call fails with a KMSInvalidSignatureException error.
The advantage of asymmetric keys is that verification needs no KMS call. This Python script verifies msg.sig using only the public key:
import boto3
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import padding
kms = boto3.client("kms")
public_der = kms.get_public_key(KeyId="alias/tutorial-signing")["PublicKey"]
public_key = serialization.load_der_public_key(public_der)
with open("msg.txt", "rb") as f:
message = f.read()
with open("msg.sig", "rb") as f:
signature = f.read()
# Raises cryptography.exceptions.InvalidSignature if the check fails
public_key.verify(
signature,
message,
padding.PSS(mgf=padding.MGF1(hashes.SHA256()), salt_length=32),
hashes.SHA256(),
)
print("signature valid")The salt length of 32 bytes matches RSASSA_PSS_SHA_256. A mismatch here is a common reason for a valid signature to fail locally.
HMAC keys: message authentication with a shared secret
An HMAC key produces a message authentication code (MAC): a short tag that proves a message has not changed and was produced by someone who holds the key. HMAC keys have only one usage, GENERATE_VERIFY_MAC. You cannot encrypt with them.
Unlike an asymmetric signature, a MAC cannot be verified by an outsider, because the key never leaves KMS. Every verifier must call VerifyMac. Use HMAC keys when all parties are your own services, for example to sign webhook payloads or session cookies, or to create keyed hashes of identifiers for lookups.
aws kms create-key \
--key-spec HMAC_256 \
--key-usage GENERATE_VERIFY_MAC \
--description "Tutorial HMAC key"
aws kms create-alias \
--alias-name alias/tutorial-hmac \
--target-key-id <key-id>
aws kms generate-mac \
--key-id alias/tutorial-hmac \
--message fileb://msg.txt \
--mac-algorithm HMAC_SHA_256 \
--query Mac \
--output text | base64 --decode > msg.mac
aws kms verify-mac \
--key-id alias/tutorial-hmac \
--message fileb://msg.txt \
--mac-algorithm HMAC_SHA_256 \
--mac fileb://msg.macThe key spec fixes the algorithm. An HMAC_256 key accepts only HMAC_SHA_256.
Gotchas and trade-offs
- Every operation is a network call. A symmetric
Encrypt, aSignand aGenerateMaceach count against your KMS request quota and add latency. Offline verification with a public key avoids this on the verifying side. - Fewer features on non-symmetric keys. Encryption context is not supported for asymmetric keys. Policy conditions on
kms:EncryptionContexttherefore have no effect on them. - No
GenerateDataKeyon asymmetric keys. To produce a key pair for envelope-style use, callGenerateDataKeyPair. It needs an asymmetric key. - Service support is narrow. If you plan to use S3, EBS or RDS encryption with the key, create a symmetric key. Check the service documentation before you create anything else.
- Public keys are public.
GetPublicKeyis controlled by thekms:GetPublicKeypermission, but the key it returns is meant to be shared. Protect the private-key operations (kms:Sign,kms:Decrypt) with your key policy instead. - Multi-Region keys. You can make Multi-Region replicas of any key type, but the type of a replica always equals the primary's. A later chapter covers this.
Exercise
A partner company with no AWS account must send you confidential files of about 5 MB. You want only your KMS-protected key to be able to read them. Which key type do you create, and how do you handle the 5 MB size?
Show answer
Create a symmetric key for the files and an asymmetric RSA key with ENCRYPT_DECRYPT usage to protect the file key. The partner downloads the RSA public key, generates a random AES key, encrypts the 5 MB file locally with it, and encrypts that small AES key with the RSA public key (it fits within the 190 to 446 byte limit). You call KMS Decrypt on the RSA key to recover the AES key and decrypt the file locally. Alternatively, you can call GenerateDataKeyPair on a symmetric key and share the public half. RSA alone cannot encrypt 5 MB directly.
Exercise
You need to sign JSON web tokens in one service and verify them in many services, including some that run outside AWS. Should you use an HMAC key or an asymmetric key, and why?
Show answer
Use an asymmetric key with SIGN_VERIFY usage. Verifiers outside AWS can check signatures offline with the public key from GetPublicKey. An HMAC key cannot be exported, so every verifier would have to call KMS VerifyMac, which is impossible without AWS credentials and adds a request per verification.