Lesson 3 of 3 Data Protection for Storage and Databases

Encrypting Data on the Client Before It Reaches AWS

This lesson explains when data must be encrypted before it leaves the application, so that it stays unreadable even to the storage service or database administrators, and the AWS libraries that make client-side encryption practical.

Learning outcomes

After completing this lesson, you will be able to:

  • Identify when client-side encryption is required.
  • Distinguish the AWS Encryption SDK from the Database Encryption SDK.
  • Bind ciphertext to its purpose with an encryption context.

When client-side encryption is required

Client-side encryption is necessary only in particular circumstances. The following list describes when it is required.

  • Data must be protected end to end, so even AWS services and database administrators see only ciphertext.
  • Particular fields (card numbers, national IDs) need protection beyond table-level encryption.
  • Data passes through several systems, and only the final consumer should read it.
  • Regulations require that the party storing data cannot decrypt it.

The libraries

Client-side encryption protects data before it leaves the application, so the service only ever stores ciphertext. The AWS Encryption SDK is the general-purpose library for data and files. It uses envelope encryption with keyrings, which decide how data keys are protected: with KMS keys, raw AES keys, or a multi-keyring that wraps the data key under keys in several Regions. It binds an encryption context to the ciphertext as authenticated data, uses key commitment so ciphertext decrypts to only one plaintext, and can cache data keys to reduce KMS calls.

The AWS Database Encryption SDK encrypts and signs DynamoDB items attribute by attribute. You choose which attributes are encrypted, which are only signed and which stay in plaintext, and searchable encryption with beacons allows queries on encrypted attributes. The Amazon S3 Encryption Client integrates with the S3 SDK to encrypt objects before upload, with KMS keys or your own. The following table summarises the libraries.

LibraryUseKey features
AWS Encryption SDKGeneral-purpose encryption of data and filesEnvelope encryption with keyrings (KMS, raw AES, multi-keyring for several Regions), encryption context as authenticated data, key commitment, data key caching
AWS Database Encryption SDKAttribute-level encryption for DynamoDBEncrypts and signs items; chooses which attributes are encrypted, signed only, or left plain; searchable encryption with beacons
Amazon S3 Encryption ClientEncrypting objects before upload to S3Integrates with the S3 SDK; KMS keys or your own
Match the pairs

Client-side encryption libraries

Exam skills 5.2.1

Match each library with its use.

Log in to try this activity Interactive activities are free with an account.

Encryption context

An encryption context is a set of non-secret key-value pairs bound to the ciphertext. The same context must be supplied to decrypt, it appears in CloudTrail for every KMS call, and key policies can require particular values (kms:EncryptionContext:tenant). Using the tenant or record ID as context means a ciphertext copied to another tenant's record cannot be decrypted there, and the audit log shows exactly whose data was accessed.

The following diagram shows who can read clinical notes protected with field-level encryption.

Field-level encryption with the AWS Encryption SDK. The clinical service encrypts each note with a KMS keyring and an encryption context naming the patient and purpose before writing it, so the shared database and its reporting tools hold only ciphertext; only the clinical service can decrypt.
The scenario is illustrative.
True or false

Encryption context

Exam skills 5.2.1 · 5.2.2

Check how an encryption context protects and audits data.

Log in to try this activity Interactive activities are free with an account.
Worked example

Encrypting fields with the Encryption SDK

A healthcare application stores clinical notes in a database it shares with reporting tools. Notes must be readable only by the clinical service. The service encrypts each note with the AWS Encryption SDK before writing it:

import aws_encryption_sdk
from aws_encryption_sdk import CommitmentPolicy
from aws_cryptographic_material_providers.mpl import AwsCryptographicMaterialProviders
from aws_cryptographic_material_providers.mpl.config import MaterialProvidersConfig
from aws_cryptographic_material_providers.mpl.models import CreateAwsKmsKeyringInput
import boto3

client = aws_encryption_sdk.EncryptionSDKClient(
    commitment_policy=CommitmentPolicy.REQUIRE_ENCRYPT_REQUIRE_DECRYPT)
mpl = AwsCryptographicMaterialProviders(config=MaterialProvidersConfig())
keyring = mpl.create_aws_kms_keyring(CreateAwsKmsKeyringInput(
    kms_key_id="arn:aws:kms:eu-west-1:111111111111:key/mrk-clinical",
    kms_client=boto3.client("kms", region_name="eu-west-1")))

def encrypt_note(patient_id, text):
    ciphertext, _ = client.encrypt(source=text.encode(), keyring=keyring,
                                   encryption_context={"patient": patient_id, "purpose": "clinical-note"})
    return ciphertext

def decrypt_note(patient_id, ciphertext):
    plaintext, header = client.decrypt(source=ciphertext, keyring=keyring,
                                       encryption_context={"patient": patient_id, "purpose": "clinical-note"})
    return plaintext.decode()

The reporting tools read the same rows but see only ciphertext, and they hold no permission to use the key. The key policy permits kms:Decrypt only for the clinical service's role, and only when the encryption context includes purpose=clinical-note. Every decryption appears in CloudTrail with the patient ID, giving a per-patient access log.

Check yourself

When is client-side encryption needed?

Show answer

When even the storage service or database administrators must not see the plaintext.

Exam Tips

Remember for the exam

  • When even the storage service or database administrators must not see plaintext, use client-side encryption.
  • For general-purpose client-side envelope encryption, use the AWS Encryption SDK.
  • For attribute-level encryption and signing in DynamoDB, use the AWS Database Encryption SDK.
  • Encryption context is authenticated, appears in CloudTrail, and can be required in key policies.
  • Use a multi-keyring or multi-Region keys when ciphertext must be decryptable in several Regions.