# Envelope Encryption

URL: https://eoxlabs.dev/glossary/envelope-encryption
Last updated: 2026-11-26
Publisher: EOXLABS (https://eoxlabs.dev)

Section: [Glossary](https://eoxlabs.dev/glossary)

Envelope encryption is the practice of encrypting data with a data encryption key (DEK) and then encrypting that DEK with a separate key encryption key (KEK). The wrapped DEK is stored next to the ciphertext, while the KEK stays in a key management service or another protected store and never travels with the data.

## Why does envelope encryption matter?

AWS describes it in [AWS KMS cryptography essentials](https://docs.aws.amazon.com/kms/latest/developerguide/kms-cryptography.html) as "the practice of encrypting plaintext data with a data key, and then encrypting the data key under another key." The structure has three practical effects:

- **A smaller blast radius.** A copied database contains ciphertext and wrapped keys, which are useless without the KEK.
- **Cheaper re-keying.** In AWS's words, "Instead of re-encrypting raw data multiple times with different keys, you can re-encrypt only the data keys that protect the raw data."
- **Separate keys per item or tenant.** Google Cloud's [envelope encryption guidance](https://docs.cloud.google.com/kms/docs/envelope-encryption) advises: "Do not use the same DEK to encrypt data from two different users."

## How does EOXLABS apply it?

EOXLABS applies envelope encryption to secrets an application must store and later use, such as third-party OAuth refresh tokens. Each token is encrypted with AES-GCM under its own DEK. The DEK is wrapped with a KEK held outside the database, and the row stores the ciphertext, the wrapped DEK, the nonce and a key-version identifier. On Cloudflare Workers, both steps use the [Web Crypto API](https://developers.cloudflare.com/workers/runtime-apis/web-crypto/), which supports AES-GCM for encryption and AES-KW for `wrapKey()` and `unwrapKey()`:

```ts
const dek = await crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, true, ['encrypt', 'decrypt']);
const iv = crypto.getRandomValues(new Uint8Array(12));
const ciphertext = await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, dek, plaintext);
const wrappedDek = await crypto.subtle.wrapKey('raw', dek, kek, 'AES-KW');
```

An engineering note applies the idea to one specific case: [envelope encryption for OAuth tokens](https://eoxlabs.dev/engineering/envelope-encryption-oauth-tokens). Envelope encryption complements a zero trust design: access control decides who may ask for a secret, and encryption limits the damage if that control fails.

## What do teams get wrong about envelope encryption?

- **Rotating the KEK does not re-encrypt data.** AWS's [key rotation documentation](https://docs.aws.amazon.com/kms/latest/developerguide/rotate-keys.html) is explicit: rotation "does not rotate the data keys that the KMS key generated or re-encrypt any data protected by the KMS key", and it "will not mitigate the effect of a compromised data key." A leaked DEK requires the affected data to be re-encrypted under a new one.
- **Disk encryption is not the same control.** Storage-level encryption at rest protects lost disks. It does nothing about a valid database credential reading rows, which envelope encryption at the application layer does address.
- **Key wrapping is not a homemade construction.** NIST [SP 800-38F](https://csrc.nist.gov/pubs/sp/800/38/f/final) specifies approved key-wrap modes, AES Key Wrap (KW) and Key Wrap with Padding (KWP), that protect both the confidentiality and the integrity of keys.
- **Old key versions still matter.** An immutable backup of an envelope-encrypted table can only be restored while the KEK versions that wrapped its DEKs still exist, so key retention must match backup retention.

Key management is part of EOXLABS's broader [security and resilience work](https://eoxlabs.dev/capabilities/security-resilience). Further terms are in the [glossary](https://eoxlabs.dev/glossary).

## Related

- [Envelope encryption for OAuth tokens on Workers](https://eoxlabs.dev/engineering/envelope-encryption-oauth-tokens): Per-token AES-GCM data keys wrapped by a key-encryption key in a Worker secret, associated data binding each ciphertext to tenant and provider, cheap rotation, and a threat model.
- [Security and Resilience](https://eoxlabs.dev/capabilities/security-resilience): Zero trust access, envelope encryption with rotatable keys, backups locked against deletion, and recovery rehearsed in advance: how EOXLABS designs systems to withstand a breach.
- [Zero Trust](https://eoxlabs.dev/glossary/zero-trust): Zero trust removes trust based on network location. Every user, device and service proves who it is and what it may do on each request, as described in NIST SP 800-207.
- [Immutable Backup](https://eoxlabs.dev/glossary/immutable-backup): An immutable backup cannot be altered or deleted by anyone until its retention period ends. It is the copy ransomware cannot reach, provided the lock is one an attacker cannot lift.

## More glossary

- [WORM Storage (Write Once, Read Many)](https://eoxlabs.dev/glossary/worm-storage)
- [Entropy Analysis](https://eoxlabs.dev/glossary/entropy-analysis)
