# Concepts
## Envelope encryption
Every envelope is encrypted under its own fresh 256-bit *data key*. The data
key is never stored in the clear. Instead, each *recipient* key wraps it, and
the wrapped copies travel inside the envelope:
```{mermaid}
flowchart LR
P[plaintext] --> E[AES-256-GCM records]
D[fresh data key] --> E
D --> W1[wrapped by the primary key
e.g. a KMS key]
D --> W2[wrapped by an escrow key
e.g. an offline HPKE key]
C[encryption context] -. authenticated, never stored .-> E
E --> V[(envelope)]
W1 --> V
W2 --> V
```
Opening needs any one recipient key. Because only the small data key is
wrapped, the key service sees one request per envelope, never the data, and
moving an envelope to a new key rewrites only its recipient section, never
the ciphertext.
## The parts of the API
| Term | What it is |
| --- | --- |
| `Protector` | The object you call: `seal`, `open`, `inspect`, `rewrap`, their streaming forms, and `open_range`. |
| Provider | A `KeyProvider` that wraps and unwraps data keys: AWS KMS, Google Cloud KMS, Azure Key Vault, Vault Transit, the built-in HPKE provider, a quorum of custodians, or hierarchical branch keys. |
| `KeyRef` | A key of one provider, named the way the provider names it, optionally with a version. |
| Recipient key | A key every envelope is sealed to: the `primary_key`, then each of `additional_keys`. |
| Decryption key | A key trusted for opening only, such as a retired primary key. |
| `EncryptionContext` | What the data is and whom it belongs to. Authenticated, never stored. |
| `Policy` | The limits and permissions every operation is checked against. Immutable, with a content-derived identifier. |
| `Envelope` | The sealed bytes. Not secret. |
## Versions
A key service rotates a key by adding a new version. Envelopes always
record the exact, immutable version that wrapped them, so they open after any
number of rotations. A `KeyRef` without a version means "the current
version" when sealing; `rewrap` moves an envelope to the current version.
Versions also decide trust. A key named without a version trusts every
version of that key, so a protector keeps opening envelopes after a rotation.
A key pinned to a version trusts that version only, which suits a key that
must not change under you, such as an audited escrow key.
## Which keys may open an envelope
The envelope names the keys that wrapped its data key, but the reader decides
which of them it trusts. A `Protector` unwraps only with its recipient keys
and its decryption keys. A recipient that names any other key is never sent
to a key service.
This matters because a key service decrypts with any key the caller's
credentials allow. Without the rule, anyone who controls such a key (a key in
another account that grants your role decrypt, or another tenant's key)
could seal arbitrary data that your service would accept as its own.
## The encryption context
The context is a small mapping of text or bytes, such as
`{"tenant": "acme", "table": "invoices", "row": "42"}`. Sealing binds the
envelope to it through a commitment keyed from the data key; the context
itself is never stored. Opening with a different context fails.
Use it to bind data to where it belongs: the tenant, the purpose, the record
it was written for. That stops a stolen or misrouted envelope from being
opened in the wrong place. Do not put secrets in it: it is authenticated,
not encrypted, and you need it again to open.
## What is guaranteed
- **Confidentiality.** Without a trusted recipient key, an envelope reveals
nothing about its plaintext beyond its size.
- **Integrity.** Any change, truncation, extension, or reordering of an
envelope's bytes is detected; streaming output becomes visible only after
the last record verifies.
- **Key commitment.** Every recipient recovers the same data key, so an
envelope cannot decrypt to different plaintexts for different recipients.
- **Context binding.** An envelope opens only with the context it was sealed
with.
The [threat model](../v4/security/threat-model.md) states each guarantee
precisely, with the tests that check it.
## What is not
- **Who sealed it.** Anyone who can wrap to a recipient key can seal to it.
With a public HPKE key, that is anyone who has the public key. When the
origin matters, sign the data or the envelope as well.
- **Metadata.** Key identifiers and versions, the number of recipients, the
record size, and (for envelopes sealed in one call) the plaintext size are
visible. `inspect` shows exactly what is.
- **A compromised host.** Code running in your process can read what the SDK
can read.
## The format
The [envelope format](../v4/spec/envelope-format.md) is specified byte by
byte, with test vectors and a
[conformance suite](https://github.com/Psychevus/cryptography-suite/tree/main/conformance)
that any implementation can run. A [JavaScript implementation](javascript.md)
reads and writes the same envelopes.