Key encapsulation mechanism

In cryptography, a key encapsulation mechanism, or KEM, is a public-key cryptosystem that allows a sender to generate a short secret key and transmit it to a receiver securely, in spite of eavesdropping and intercepting adversaries.

A KEM allows a sender who knows a public key to simultaneously generate a short random secret key and an encapsulation or ciphertext of the secret key by the KEM's encapsulation algorithm. The receiver who knows the private key corresponding to the public key can recover the same random secret key from the encapsulation by the KEM's decapsulation algorithm.

The security goal of a KEM is to prevent anyone who doesn't know the private key from recovering any information about the encapsulated secret keys, even after eavesdropping or submitting other encapsulations to the receiver to study how the receiver reacts.

Difference from public-key encryption
The difference between a public-key encryption scheme and a KEM is that a public-key encryption scheme allows a sender to choose an arbitrary message from some space of possible messages, while a KEM chooses a short secret key at random for the sender.

The sender may take the random secret key produced by a KEM and use it as a symmetric key for an authenticated cipher whose ciphertext is sent alongside the encapsulation to the receiver. This serves to compose a public-key encryption scheme out of a KEM and a symmetric-key authenticated cipher in a hybrid cryptosystem.

Most public-key encryption schemes such as RSAES-PKCS1-v1_5, RSAES-OAEP, and Elgamal encryption are limited to small messages and are almost always used to encrypt a short random secret key in a hybrid cryptosystem anyway. And although a public-key encryption scheme can conversely be converted to a KEM by choosing a random secret key and encrypting it as a message, it is easier to design and analyze a secure KEM than to design a secure public-key encryption scheme as a basis. So most modern public-key encryption schemes are based on KEMs rather than the other way around.

Syntax
A KEM consists of three algorithms:


 * 1) Key generation, $$(\mathit{pk}, \mathit{sk}) := \operatorname{Gen}$$, takes no inputs and returns a pair of a public key $$\mathit{pk}$$ and a private key $$\mathit{sk}$$.
 * 2) Encapsulation, $$(k, c) := \operatorname{Encap}(\mathit{pk})$$, takes a public key $$\mathit{pk}$$, randomly chooses a secret key $$k$$, and returns $$k$$ along with its encapsulation $$c$$.
 * 3) Decapsulation, $$k' := \operatorname{Decap}(\mathit{sk}, c')$$, takes a private key $$\mathit{sk}$$ and an encapsulation $$c'$$, and either returns an encapsulated secret key $$k'$$ or fails, sometimes denoted by returning $$\bot$$.

Correctness
A KEM is correct if, for any key pair $$(\mathit{pk}, \mathit{sk})$$ generated by $$\operatorname{Gen}$$, decapsulating an encapsulation $$c$$ returned by $$(k, c) := \operatorname{Encap}(\mathit{pk})$$ with high probability yields the same key $$k$$, that is, $$\operatorname{Decap}(\mathit{sk}, c) = k$$.

Security: IND-CCA
Security of a KEM is quantified by its indistinguishability against chosen-ciphertext attack, IND-CCA, which is loosely how much better an adversary can do than a coin toss to tell whether, given a random key and an encapsulation, the key is encapsulated by that encapsulation or is an independent random key.

Specifically, in the IND-CCA game:


 * 1) The key generation algorithm is run to generate $$(\mathit{pk}, \mathit{sk}) := \operatorname{Gen}$$.
 * 2) $$\mathit{pk}$$ is revealed to the adversary.
 * 3) The adversary can query $$\operatorname{Decap}(\mathit{sk}, c')$$ for arbitrary encapsulations $$c'$$ of the adversary's choice.
 * 4) The encapsulation algorithm is run to randomly generate a secret key and encapsulation $$(k_0, c) := \operatorname{Encap}(\mathit{pk})$$, and another secret key $$k_1$$ is generated independently at random.
 * 5) A fair coin is tossed, giving an outcome $$b \in \{0,1\}$$.
 * 6) The pair $$(k_b, c)$$ is revealed to the adversary.
 * 7) The adversary can again query $$\operatorname{Decap}(\mathit{sk}, c')$$ for arbitrary encapsulations $$c'$$ of the adversary's choice, except for $$c$$.
 * 8) The adversary returns a guess $$b' \in \{0,1\}$$, and wins the game if $$b = b'$$.

The IND-CCA advantage of the adversary is $$\left|\Pr[b' = b] - 1/2\right|$$, that is, the probability beyond a fair coin toss at correctly distinguishing an encapsulated key from an independently randomly chosen key.

RSA
Traditional RSA encryption, with $$t$$-bit moduli and exponent $$e$$, is defined as follows:


 * 1) Key generation, $$(\mathit{pk}, \mathit{sk}) := \operatorname{Gen}$$: Generate a $$t$$-bit semiprime $$2^{t - 1} < n < 2^t$$ at random with $$\gcd(e, \lambda(n)) = 1$$, compute $$d = e^{-1} \bmod \lambda(n)$$ (where $$\lambda(n)$$ is the Carmichael function), return $$\mathit{pk} := n$$ as the public key, and return $$\mathit{sk} := (n, d)$$ as the private key.  (Many variations on key generation algorithms and private key formats are available. )
 * 2) Encryption of $$(t - 1)$$-bit message $$m$$ to public key $$\mathit{pk} = n$$, giving $$c := \operatorname{Encrypt}(\mathit{pk}, m)$$: Compute $$c := m^e \bmod n$$.
 * 3) Decryption of ciphertext $$c'$$ with private key $$\mathit{sk} = (n, d)$$, giving $$m' := \operatorname{Decrypt}(\mathit{sk}, c')$$: Return $$m' := (c')^d \bmod n$$.

This naive approach is totally insecure. For example, since it is nonrandomized, it cannot be secure against even chosen-plaintext attack. Even if $$m$$ is always a random secret key, such as a 256-bit AES key, when $$e$$ is chosen to optimize efficiency as $$e = 3$$, the message $$m$$ can be computed from the ciphertext $$c$$ simply by taking real number cube roots, and there are many other attacks against plain RSA. Various randomized padding schemes such as RSAES-PKCS1-v1_5 and RSAES-OAEP have been devised in attempts—sometimes failed, like RSAES-PKCS1-v1_5 —to make it secure for arbitrary short messages $$m$$.

Since the message $$m$$ is almost always a short secret key for a symmetric-key authenticated ciphers used to encrypt an arbitrary bit string message, a simpler approach called RSA-KEM is to choose an element of $$\mathbb Z/n\mathbb Z$$ at random and use that to derive a secret key using a key derivation function $$H$$, roughly as follows:


 * 1) Key generation: As above.
 * 2) Encapsulation for a public key $$\mathit{pk} = n$$, giving $$(k, c) := \operatorname{Encap}(\mathit{pk})$$: Choose $$r \in \mathbb Z/n\mathbb Z$$ uniformly at random, compute $$k = H(r)$$ and $$c := r^e \bmod n$$, and return $$(k, c)$$ with $$c$$ as the encapsulation of the secret key $$k$$.
 * 3) Decapsulation of $$c'$$ with private key $$\mathit{sk} = (n, d)$$, giving $$m' := \operatorname{Decap}(\mathit{sk}, c')$$: Compute $$r' := (c')^d \bmod n$$ and return $$k := H(r')$$.

This approach is simpler to implement, and provides a tighter reduction to the RSA problem, than padding schemes like RSAES-OAEP.

Elgamal
Traditional Elgamal encryption is defined over a multiplicative subgroup of the finite field $$\mathbb Z/p\mathbb Z$$ with generator $$g$$ of order $$q$$ as follows:


 * 1) Key generation, $$\operatorname{Gen}$$: Choose $$x \in \mathbb Z/q\mathbb Z$$ uniformly at random, and return $$\mathit{sk} := x$$ as the private key and $$\mathit{pk} := y := g^x \bmod p$$ as the public key.
 * 2) Encryption of a message $$m \in \mathbb Z/p\mathbb Z$$ to public key $$\mathit{pk}$$, giving $$\operatorname{Encrypt}(\mathit{pk}, m)$$: Choose $$r \in \mathbb Z/q\mathbb Z$$ uniformly at random, compute $$\begin{align} t &:= y^r \bmod p \\ c_1 &:= g^r \bmod p \\ c_2 &:= (t \cdot m) \bmod p,\end{align}$$ and return the ciphertext $$c := (c_1, c_2)$$.
 * 3) Decryption of a ciphertext $$c' = (c'_1, c'_2)$$ for a private key $$\mathit{sk} = x$$, giving $$\operatorname{Decrypt}(\mathit{sk}, c')$$: Compute $$t' := (c'_1)^x \bmod p$$ and return $$m' := (t^{-1} c'_2) \bmod p$$.

Although this meets the syntax of a public-key encryption scheme, restricted to messages in the space $$\mathbb Z/p\mathbb Z$$ (which limits it to message of a few hundred bytes for typical values of $$p$$), it fails to achieve indistinguishability against chosen ciphertext attack. For example, an adversary knowing a ciphertext $$c = (c_1, c_2)$$ for a (possibly unknown) message $$m$$ can easily predict the ciphertext $$c' = (c'_1, c'_2)$$ for the related message $$m' = 2m \bmod p$$, by the relation $$c'_2 = c_2 \bmod p$$.

Since the message $$m$$ is almost always a short secret key for a symmetric-key authenticated cipher used to encrypt an arbitrary bit string message, a simpler approach is to derive the secret key from $$t$$ and dispense with $$m$$ and $$c_2$$ altogether, as a KEM, using a key derivation function $$H$$:


 * 1) Key generation: As above.
 * 2) Encapsulation for a public key $$\mathit{pk} = y$$, giving $$(k, c) := \operatorname{Encap}(\mathit{pk})$$: Choose $$r \in \mathbb Z/q\mathbb Z$$ uniformly at random, compute $$\begin{align} t &:= y^r \bmod p \\ k &:= H(t) \\ c &:= g^r \bmod p,\end{align}$$ and return $$(k, c)$$ with $$c$$ as the encapsulation of the secret key $$k$$.
 * 3) Decapsulation of $$c'$$ with private key $$\mathit{sk} = x$$, giving $$k' := \operatorname{Decap}(\mathit{sk}, c')$$: Compute $$t' := (c')^x \bmod p$$ and return $$k' := H(t')$$.

When combined with an authenticated cipher to encrypt arbitrary bit string messages, this KEM is essentially a subroutine of the Integrated Encryption Scheme.