Skip to content

safekeys/envelope-encryption [built] ​

Envelope encryption separates the key that protects a secret from the key that protects that key. Each secret is encrypted with a random data encryption key (DEK); the DEK is wrapped by a key-encryption key (KEK) held in the key store. The sidecar unwraps a DEK only for the duration of a resolve, then zeroises both the DEK and the plaintext.

Status: built

Why this exists ​

Rotating or revoking a master key must not require re-encrypting every secret ever stored, and a compromised DEK must expose only one object. Envelope encryption gives per-object key isolation and a single small set of KEKs to protect, which is also the shape that later lets the KEK live in a secure element or be reconstructed under threshold policy without touching the data.

Design decisions ​

openbao-or-vault [accepted] ​

Decision: Use OpenBao or Vault Transit/KV as the MVP key store.

Rationale: Mature, self-hostable, and auditable, with wrapping primitives already available. Auto-unseal comes later. It avoids building a key store before proving the protocol.

Alternatives considered:

  • AWS KMS / cloud provider KMS — rejected: It puts the root of trust in a provider the customer may not control, and a multi-tenant AI-cloud KMS in particular could be reachable by the model provider — defeating the product's core property.
  • Build a bespoke key store — rejected: It slows the MVP and adds an unreviewed crypto component on the most sensitive path.

envelope-not-direct [accepted] ​

Decision: Wrap keys rather than encrypting secrets directly under a master key.

Rationale: Envelope encryption keeps per-object isolation and makes master key rotation cheap, which is also the structure required to move the KEK into hardware or threshold custody without changing the data layer.

How it works ​

Wrap a data encryption key (wrap-dek) ​

A new DEK is generated locally and wrapped by the KEK without leaving the key store's trust boundary.

  1. Generate — Generate a random DEK for the object.
  2. Encrypt — Encrypt the plaintext object with the DEK.
  3. Send — Send the DEK to the key store for wrapping under the KEK; only the wrapped form is persisted.
  4. Observe — Plaintext is discarded from memory after encryption.

Unwrap a DEK for a resolve (unwrap-dek) ​

The sidecar requests unwrap only after a token has passed every verification step.

  1. Transmit — Sidecar sends the wrapped DEK and token context to the key store after token verification.
  2. Receive — Key store returns the unwrapped DEK over an authenticated channel.
  3. Decrypt — Sidecar decrypts the target object in memory.
  4. Delete — Zeroise the DEK and the plaintext buffer once injection completes.

Guarantees ​

kek-never-leaves-store ​

The long-term key-encryption key must never be exported from the key store in the MVP, and never leave the secure element in later phases.

Acceptance criteria:

  • [ ] Only wrapped DEKs are persisted outside the key store
  • [ ] The sidecar receives unwrapped DEKs transiently, never KEKs
  • [ ] No code path writes a KEK to disk, a log, or a response

per-object-dek ​

Every object is encrypted under its own random DEK.

Acceptance criteria:

  • [ ] A DEK is generated fresh per object, never reused across objects
  • [ ] Compromise of one DEK exposes exactly one object
  • [ ] DEKs are high-entropy random values from a CSPRNG

transient-and-zeroised ​

Unwrapped key material and plaintext exist only for the duration of a resolve.

Acceptance criteria:

  • [ ] The DEK is zeroised immediately after use
  • [ ] The plaintext buffer is zeroised immediately after injection
  • [ ] Neither is written to logs, traces, or crash dumps by design

no-ai-cloud-kms-root ​

A multi-tenant AI-cloud KMS must not serve as the root of trust.

Acceptance criteria:

  • [ ] The root key store is self-hosted, then an HSM or secure element
  • [ ] The MVP key store is OpenBao or Vault, not a model provider's KMS
  • [ ] Documented explicitly so an integrator does not substitute a provider KMS

Test specifications ​

kek-not-exportable ​

Given:

  • keystore: "openbao-dev"

When: wrap-dek

Then:

  • assertion: the created KEK cannot be read back in plaintext through the API
  • assertion: only wrapped DEKs are persisted

Contracts verified: kek-never-leaves-store

unique-dek-per-object ​

Given:

  • objects: 100

When: wrap-dek

Then:

  • assertion: all 100 wrapped DEKs differ
  • assertion: no DEK is reused across objects

Contracts verified: per-object-dek

dek-zeroised-after-resolve ​

Given:

  • single_resolve: true

When: unwrap-dek

Then:

  • assertion: the plaintext buffer is cleared after injection
  • assertion: no plaintext persists in the sidecar after the resolve returns

Contracts verified: transient-and-zeroised

deny-by-default-when-store-unreachable ​

Given:

  • keystore_reachable: false

When: unwrap-dek

Then:

  • assertion: resolution fails closed
  • assertion: no cached or fallback plaintext is used

Contracts verified: transient-and-zeroised

Implementation ​

  • Primary: pkg/protocol/envelope.go
  • Test code: pkg/protocol/protocol_test.go
  • Test code status: manual

See Also ​

  • safekeys/encrypted-folder
  • safekeys/sidecar-resolution
  • safekeys/hardware-root