Introduction
RDS and Aurora support storage encryption, but if someone gains database access privileges, there's a risk they could view the data in plaintext.
This article introduces how to encrypt specific columns before storing them in a database using AWS KMS and AWS Encryption SDK.
1. How Encryption Works
We'll use envelope encryption in this approach.
We generate a data key with KMS and then use that data key to encrypt data on the application side.
- Generate a data key with KMS
- Encrypt the target data with the data key
- Store both the encrypted data and encrypted data key in the database
- On read, decrypt the data key with KMS, then decrypt the data itself
The key point here is that KMS protects the data key rather than directly encrypting the data itself.
2. Creating a KMS Key
$ aws kms create-key \
--description "Column encryption key" \
--key-usage ENCRYPT_DECRYPT \
--origin AWS_KMS
Set the created key's ARN as an environment variable.
export KMS_KEY_ARN="<ARN of the created KMS key>"
Create an alias for the key.
$ aws kms create-alias \
--alias-name alias/column-encryption \
--target-key-id "$KMS_KEY_ARN"
3. Encryption and Decryption
Install AWS Encryption CLI and prepare test data.
$ python3 -m pip install aws-encryption-sdk-cli
$ aws-encryption-cli --version
aws-encryption-sdk-cli/4.3.0 aws-encryption-sdk/3.3.1
$ echo -n "4111111111111111" > card.txt
Encryption
$ aws-encryption-cli \
--encrypt \
--input card.txt \
--output card.enc \
--wrapping-keys key="$KMS_KEY_ARN" \
--encryption-context purpose=payment-card environment=production \
--commitment-policy require-encrypt-require-decrypt \
--suppress-metadata
The encrypted data contains not just the ciphertext, but also the encrypted data key, encryption context, and more.
You can store this output in a database column.
Decryption
$ aws-encryption-cli \
--decrypt \
--input card.enc \
--output card.dec \
--wrapping-keys key="$KMS_KEY_ARN" \
--encryption-context purpose=payment-card environment=production \
--commitment-policy require-encrypt-require-decrypt \
--suppress-metadata
$ cat card.dec
4111111111111111
The encryption context is additional authentication information attached to the encrypted data. It's not secret information and is also used for integrity verification during decryption.
4. Considerations for Production
In actual applications, you'd use a configuration like this:
- On registration: Encrypt in the application before storing in the database
- On retrieval: Decrypt data fetched from the database in the application
- Access control: Grant
kms:GenerateDataKeyfor encryption andkms:Decryptfor decryption as needed - Audit: Review KMS usage history with CloudTrail
By separating database access permissions from KMS decryption permissions, you can create a configuration where users who can access the database cannot necessarily obtain plaintext data.
You can also use the encryption context as a condition in KMS policies. However, the encryption context itself is not secret information and cannot serve as a standalone access control mechanism.
Warning: When you encrypt columns, you can no longer perform partial match searches or range searches using pre-encryption values. If search requirements exist, you need to separately design a search HMAC or similar mechanism.
5. Protection Scope When Database Is Compromised
What's important about this approach is distinguishing between database compromise and application compromise.
| Compromised Target | Expected Impact |
|---|---|
| Database or backups | Difficult to obtain plaintext with just the ciphertext |
| KMS decryption permissions | Data key could potentially be decrypted |
| Application runtime environment | Legitimate decryption processes could be misused |
In other words, this method is effective when only database data is leaked, but if the application with decryption permissions is also compromised, you may not be able to prevent information leakage.
For this reason, IAM least privilege, application-level authorization, and audit trails via CloudTrail are critical.
Warning: In actual payment systems, consider using tokenization from a payment processor rather than storing card information yourself. Security codes like CVV/CVC are not permitted to be stored even after encryption/authorization in many compliance frameworks.
Summary
By combining AWS KMS and AWS Encryption SDK, you can encrypt specific columns before storing them in a database.
While this reduces plaintext leak risk when only database data is compromised, it's a different story if the application with decryption permissions is also compromised.
It's important to combine encryption with KMS access control and application-level authorization.
Top comments (0)