Caloogy Article
中文

Envelope Encryption: Asymmetric for the Key, Symmetric for the Data

Date
Mar 2, 2025
Series
Cryptography
Reading
5 min
Topics
Envelope encryptionHybrid encryptionHpkeEcdh

The one-sentence version

Real-world encryption systems almost never use RSA or Elliptic Curves to encrypt an entire file directly, because they are slow and have length limitations. The standard approach is hybrid encryption, also known as envelope encryption: a random symmetric key is generated to encrypt the data (fast), and then the receiver's public key is used to "seal" this symmetric key (delivering it securely to the recipient). It consists of three components: asymmetric (key delivery), symmetric (data encryption and integrity), and hash / key derivation (turning the shared secret into a valid key). HTTPS, PGP email, Cloud KMS, and libsodium's sealed box all follow this structure.

This article has three layers:

  1. Layer 1: The Intuition: Putting the key in an envelope
  2. Layer 2: Real-World Use: Cloud KMS, PGP, HPKE, client-side encryption
  3. Layer 3: Principles and Engineering details: The full ECDH + HKDF + AEAD workflow, large file chunking, and common mistakes

Layer 1: The Intuition

1.1 Putting the key in an envelope

Imagine you want to send a box of valuables to a friend:

  1. Buy a new padlock and lock the box (symmetric encryption of the data; it is fast and can lock large items);
  2. Put the key to this padlock inside a special envelope that only your friend can open (encrypt the small key using your friend's public key);
  3. Send the box and the envelope together.

When your friend opens the envelope to get the key, they can then open the box. No one in transit can get the key, so no one can open the box.

1.2 Why not encrypt the entire file directly with a public key?

Asymmetric (RSA, Elliptic Curves) Symmetric (AES, ChaCha20)
Speed Slow; every operation involves large numbers or curve math Extremely fast; features hardware acceleration
Max length RSA can only encrypt data shorter than the key Arbitrary length
Strength Securely delivers a small secret between strangers Encrypts large amounts of data

Thus, each plays to its strengths: asymmetric encryption is only responsible for the few dozen bytes of the key, while symmetric encryption handles everything else.


Layer 2: Real-World Use

Scenario How Envelope Encryption is used
Cloud KMS (AWS KMS, Google Cloud KMS, etc.) Data is encrypted with a "Data Encryption Key" (DEK). The DEK is then encrypted with a "Key Encryption Key" (KEK) from the KMS and stored alongside the data. The KEK never leaves the KMS.
PGP / GPG Email A random session key encrypts the email body, and the session key is then encrypted separately for each recipient using their respective public keys.
HTTPS During the handshake, (EC)DH is used to negotiate a shared secret, from which a symmetric key is derived to encrypt subsequent traffic.
HPKE (RFC 9180, 2022) Standardizes "KEM + Key Derivation + AEAD" into a universal primitive, adopted by TLS Encrypted Client Hello (ECH), MLS group chat protocols, etc.
Client-side Encryption Upload The browser encrypts the file using the server owner's public key before uploading; the server only stores the ciphertext, and only the holder of the private key can decrypt it.

Key rotation becomes very cheap: When rotating the master key, you only need to re-seal each DEK (each only a few dozen bytes), rather than re-encrypting terabytes of data. This is a major reason why envelope encryption is widely adopted in cloud services.


Layer 3: Principles and Engineering

3.1 Full workflow based on ECDH

The receiver pre-generates a long-term key pair: private key xx, public key X=xGX = xG.

Sender:

  1. Generates a pair of ephemeral keys yy, Y=yGY = yG;
  2. Computes the shared secret S=yX=xyGS = yX = xyG (ECDH, see "Diffie–Hellman and Discrete Logarithms", Elliptic Curves);
  3. Uses HKDF to derive a symmetric key KK from SS;
  4. Uses AEAD (such as AES-GCM, ChaCha20-Poly1305) and a random nonce to encrypt the data, resulting in ciphertext and an authentication tag;
  5. Sends (Y, nonce, ciphertext)(Y,\ \text{nonce},\ \text{ciphertext}).

Receiver: Uses their own private key to calculate xY=xyG=SxY = xyG = S, derives the same KK, and then decrypts and verifies the tag.

An eavesdropper who only sees XX and YY would have to solve the Elliptic Curve Discrete Logarithm Problem to calculate xyGxyG.

3.2 Why HKDF is necessary

The shared secret SS obtained via ECDH is the coordinate of a point on a curve; it is not a uniformly random bitstring (for example, it must satisfy the curve equation) and cannot be used directly as an AES key. HKDF consists of two steps:

The info parameter also enables domain separation: the same shared secret can derive unrelated keys for different purposes.

3.3 Why AEAD is necessary

Encryption without authentication is insufficient: although an attacker cannot read the ciphertext, they might modify it to make the decryption result change according to their intent (see malleability in "One-Time Pad"). AEAD (Authenticated Encryption with Associated Data) generates an authentication tag during encryption; if even a single bit of the ciphertext is altered, decryption will fail immediately.

3.4 Large files: Chunked encryption

It is impractical to read a multi-gigabyte audio file into memory all at once for encryption. The standard approach is:

  1. Generate a random content key and seal it using the envelope method;
  2. Use this content key to perform AEAD on the file in chunks, where each chunk includes a sequence number and the final chunk includes an "end" marker;
  3. The sequence numbers and end markers prevent an attacker from reordering, deleting, or truncating data blocks.

The secretstream interface in libsodium is designed this way, allowing for encryption while recording.

3.5 Common mistakes

Mistake Consequence
Reusing a nonce under the same key (AES-GCM) Catastrophic: XOR of two plaintexts is leaked, and authentication can be forged
Using RSA without OAEP padding Textbook RSA can be broken in many ways (see "RSA and Euler's Theorem")
Using the shared secret directly as a key without a KDF Key distribution is non-uniform, and there is no domain separation
Encryption without authentication Ciphertext can be tampered with
Failing to verify if the recipient's public key is valid Subgroup attacks (see "Diffie–Hellman and Discrete Logarithms")

Therefore, the safest choice in practice is to use high-level abstractions (libsodium's sealed box, HPKE libraries, or Cloud KMS SDKs) rather than assembling low-level primitives yourself.

Envelope encryption does not automatically provide forward secrecy

If the receiver always uses the same long-term private key xx, then if xx is compromised, all previously received envelopes can be opened. This is the inherent cost of storage scenarios that "must allow retrospective decryption"; see "Forward Secrecy" for details.


Summary

Layer Key points
Intuition Symmetric for the box, Asymmetric for the key
Three components Asymmetric (ECDH / RSA-OAEP), KDF (HKDF), AEAD (AES-GCM / ChaCha20-Poly1305)
Benefits Fast, supports arbitrary lengths, rotating master keys only requires re-sealing DEKs
Practice Use high-level abstractions; never reuse nonces; chunk large files and prevent truncation