Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

KMS envelope encryption for Rust

The library provides envelope encryption with a key encryption key held by a KMS, and AEAD encryption without a KMS. It covers:

To start using the library, see Getting started.

Envelope encryption

A value is encrypted with an AEAD algorithm under a data encryption key (DEK). The DEK is then encrypted by a key encryption key (KEK) that never leaves the KMS, and the encrypted DEK is stored next to the cipher text. KMS documentation, and Azure Key Vault’s wrapKey and unwrapKey operations, call this wrapping the DEK. To read the value, the KMS decrypts the DEK, and the DEK decrypts the value locally.

So the KMS sees only DEKs, never the data, and a KMS call is needed only to encrypt or decrypt a DEK. Access to the data is controlled by access to the KEK: revoke the KMS permission, or destroy the key, and the stored values cannot be decrypted.

The additional authenticated data (AAD) is authenticated but not encrypted: a value decrypts only with the AAD it was encrypted with. Use it to tie a value to where it is stored, such as a table and a row id, so that a value copied to another row fails to decrypt.

Choosing a mode

The ways to use a KMS differ in how many DEKs exist, how often the KMS is called and what a stored value holds. The table below sets the four modes side by side. Pick one by what the data needs:

  • Per-call envelope, the default of KmsAeadEnvelope. Use it when values are written rarely, such as secrets and configuration, or when one DEK per value is a requirement. See Envelope encryption;
  • Store-less DEK reuse, a KmsAeadEnvelope with a DekReusePolicy. Use it for high write rates where a KMS call per value costs too much. See DEK caching and reuse and Cut KMS calls with a DEK cache and DEK reuse;
  • One fixed key, encrypt of AeadCipher or DeterministicCipher. Use it for a column that BigQuery reads, with no key rotation. See One fixed key;
  • Keyring, the kms-aead-keyring crate. Use it when keys need central rotation and retirement, or when BigQuery and Tink read values across rotations. See Keyring and field encryption.
Per-call envelopeStore-less DEK reuseOne fixed keyKeyring
Cratekms-aeadkms-aeadkms-aeadkms-aead-keyring
DEKsone per valueone per process at a time, renewed by age or countone, encrypted once by an administrative jobone per key version of a scope
KMS calls on writeone encryption per valueone encryption per renewed DEKno encryption, and one decryption per DEK cache TTLno encryption, and one decryption per key version per DEK cache TTL
KMS calls on readone decryption per value, or per DEK cache missone decryption per DEK per cache TTLone decryption per cache TTLone decryption per key version per cache TTL
A value carriesits encrypted DEKits encrypted DEKa 4-byte key ida 4-byte key id
Infrastructurea KMSa KMSa KMS, and the encrypted DEK in the application’s settingsa KMS and a keyring store
Rotationthe KEK in the KMSthe KEK in the KMS, and DEK renewal per processthe KEK in the KMS; the DEK is not rotatedcentral: staged rotate and promote, retire, destroy, and a compromised flag every process sees
Deterministic fieldswith keys the application stores, see Deterministic encryptionnoyes, with DeterministicCipheryes, with queries across rotations
BigQuery readersnonoyes, with a keyset of the key encrypted by a Cloud KMS KEKyes, with a Cloud KMS KEK
Tink readersyes, with KmsEnvelopeAead for GCP and AWS keysyes, with KmsEnvelopeAead for GCP and AWS keysyes, with a keyset of the keyyes, with a KEK in Cloud KMS, AWS KMS by key ARN or Vault Transit
Crypto-shreddingKEK destructionKEK destructionKEK destructionKEK destruction, or dangerously_shred() and the deletion of the record’s backups

No mode needs another. Values of the envelope modes move to a keyring with a sweep, see Move envelope values to a keyring.

Streamed values come in the same modes: under a keyring, under an envelope, or with a DEK the application holds, see Streaming encryption.

Both envelope modes encrypt a value of a Rust type, such as an application newtype, into an Encrypted<T, Envelope> that carries its encrypted DEK, and serde, tokio-postgres and sqlx store it as bytes, so a column of secrets needs no store of its own. See Stored values.

AeadCipher and DeterministicCipher work without a KMS at all, with keys the application holds.

Where to start

The setup is under Getting started: Choose a provider and a backend, FIPS included, and Develop and test without a cloud.

Use cases are in five groups, each a situation and the way through it:

ToRead
Encrypt columns, make fewer KMS calls, give each customer its own keys, look a row up by an encrypted value or erase a customerProtect data in your database
Let BigQuery or Tink read encrypted columns, or let partners and devices encrypt without being able to decryptShare encrypted data with other systems
Encrypt uploads and exports of any size, and read byte ranges of themFiles and streams
Publish a file anyone can verify, or detect tampering between your own servicesProve where data came from
Log a payload that fails to parse, with no encryptionKeep sensitive data out of logs

Running in production, one page per task:

ToRead
Give the applications and the administration their own rightsPermissions: applications and administration
Alert on bothMonitoring and alerts
Rotate the DEKs and the KEK without downtimeRotate keys and re-encrypt values
Respond to a leaked key or a damaged keyring recordIncident runbook
Stop a writer of the keyring store from changing which keys are usedProtect the keyring record
Move envelope values to a keyringMove envelope values to a keyring

Moving to a newer version is covered by the guides in the Upgrading section at the end of the book: Migrating from 0.30 and Migrating from 0.29. Apply them in order, newest first.

Licence

Apache Software License (ASL)