---
title: "Envelope encryption and KMS"
description: "Per-organization data keys wrapped by AWS KMS, Cloud KMS, Vault Transit or a local key, customer-managed keys, and migrating existing secrets."
keywords: ["KMS", "envelope encryption", "data key", "DEK", "KEK", "customer-managed key", "CMK", "BYOK", "Vault Transit"]
---

Every secret TRUSTIVAN stores for an organization (registry, git, cloud and
Kubernetes credentials, single sign-on client secrets, webhook and
notification secrets, AI provider keys, authenticated-scan credentials) is
sealed with AES-256-GCM under a key derived from **that organization's data
key**, and bound to its row. The data key is a random 256-bit key, one per
organization, stored only **wrapped** by a key encryption key held in a KMS.
Nothing unwrapped is written anywhere; an unwrapped data key lives in process
memory for at most five minutes.

Secrets that belong to a person rather than an organization (a TOTP seed, a
linked GitHub token, a staff account's seed) stay under `TOKEN_ENCRYPTION_KEY`
directly, as before.

## Choose where the key encryption key lives

| `KMS_PROVIDER` | Key encryption key | Credentials |
|---|---|---|
| `local` (default) | Derived from `TOKEN_ENCRYPTION_KEY` | None |
| `aws` | An AWS KMS symmetric key, `KMS_KEY_ID` an ARN or `alias/…` | The default chain: instance role, IRSA, environment |
| `gcp` | A Cloud KMS symmetric key, `KMS_KEY_ID` its resource name | Application Default Credentials |
| `vault` | A Vault Transit key, `KMS_KEY_ID` its name | `VAULT_ADDR`, `VAULT_TOKEN` |

`local` still gives every organization its own data key; what it does not give
is a key that lives outside the application's environment. The process needs
`encrypt` and `decrypt` on the key (`kms:Encrypt`, `kms:Decrypt`;
`cloudkms.cryptoKeyVersions.useToEncrypt` and `useToDecrypt`; the Transit
`encrypt` and `decrypt` paths). Each wrapped key is bound to its organization
and version: AWS through the encryption context, Cloud KMS through additional
authenticated data, Vault inside the wrapped value. A wrapped key copied onto
another organization does not unwrap there.

The provider and key are read at start. A misconfigured provider stops the
process at start rather than at the first secret.

## Move existing secrets onto data keys

Secrets written before this release are on the master key (`v2:`). They keep
opening. To move them, run the re-encryption sweep once the release is
deployed:

```bash
reencrypt plan    # what would change; creates each organization's first data key
reencrypt apply   # rewrites every organization secret under its data key (v3:)
reencrypt report  # every row should report bound
```

With the production Compose file, prefix each with
`docker compose -f infra/docker/docker-compose.prod.yml --env-file infra/docker/.env run --rm`.

The sweep is idempotent and takes rows one organization at a time under that
organization's row-level security context. See
[`reencrypt`](/cli-reference/reencrypt).

## Rotate a data key

An owner rotates their organization's data key under **Settings → Identity →
Encryption keys**, or `POST /api/v1/settings/encryption/rotate`. New secrets
use the new version at once. Older versions stay readable; the next
`reencrypt apply` moves every row onto the new one.

Rotating `TOKEN_ENCRYPTION_KEY` with `KMS_PROVIDER=local` follows
[Rotate the encryption key](/deployment/key-rotation): data keys wrapped under
the old master key keep unwrapping while it is listed in
`TOKEN_ENCRYPTION_KEYS_PREVIOUS`.

## Customer-managed keys (Enterprise)

With `KMS_CUSTOMER_PROVIDERS=aws` (or `gcp`), an owner of an organization on
the Enterprise plan can name a key in **their own** cloud account under
**Settings → Identity → Encryption keys**, or
`PUT /api/v1/settings/encryption/customer-key`. The key must grant this
deployment's identity encrypt and decrypt (a key policy or grant in AWS, an IAM
binding in Google Cloud).

TRUSTIVAN first proves the key works with a wrap and unwrap of a test value,
then rewraps every version of the organization's data key under it. No secret
is re-encrypted, so the change takes seconds. From then on the organization's
secrets depend on that key:

- **Disabling or deleting the key** makes the organization's stored secrets
  unreadable within five minutes (the unwrapped-key cache). Scans that need a
  registry credential and deliveries that need a signing secret fail until it
  is re-enabled.
- **Stopping use** (`DELETE /api/v1/settings/encryption/customer-key`) rewraps
  the data keys back under the deployment key. The customer key must still be
  enabled for that.

Every change is recorded in the organization's audit trail as
`encryption_key.changed` or `encryption_key.rotated`.

## What this does not do

- **No automatic rotation.** A data key rotates when an owner asks; the sweep
  runs when an operator runs it.
- **No hardware module of our own.** Keys live in the KMS you choose.
- **Person-level secrets are not on data keys.** They stay under
  `TOKEN_ENCRYPTION_KEY`.
- **Customer-managed keys from Vault are not offered.** Vault is available as
  the deployment's own key encryption key only.

## Related

- [Rotate the encryption key](/deployment/key-rotation)
- [Credentials](/security/credentials): what is encrypted and what is only hashed.
- [Environment](/configuration/environment#envelope-encryption-and-kms)
