@@ -255,6 +255,98 @@ class BOTAN_PUBLIC_API(2, 0) Callbacks {
255255 const std::vector<uint8_t >& msg,
256256 const std::vector<uint8_t >& sig);
257257
258+ struct Encapsulation_Result {
259+ std::vector<uint8_t > encapsulated_bytes;
260+ secure_vector<uint8_t > shared_secret;
261+ };
262+
263+ /* *
264+ * Generate an ephemeral KEM key for a TLS 1.3 handshake
265+ *
266+ * Applications may use this to add custom KEM algorithms or entirely
267+ * different key exchange schemes to the TLS 1.3 handshake. For instance,
268+ * this could provide an entry point to implement a hybrid key exchange
269+ * with both a traditional algorithm like ECDH and a quantum-secure KEM.
270+ * Typical use cases of the library don't need to do that and serious
271+ * security risks are associated with customizing TLS's key encapsulation
272+ * mechanism.
273+ *
274+ * Note that the KEM interface is usable for TLS 1.3 handshakes, only.
275+ *
276+ * The default implementation simply delegates this to the
277+ * tls_generate_ephemeral_key() call when appropriate.
278+ *
279+ * @param group the group identifier to generate an ephemeral keypair for
280+ * @param rng a random number generator
281+ *
282+ * @returns a keypair whose public key will be provided to the peer and
283+ * the private key will be provided to tls_kem_decapsulate later
284+ * in the handshake.
285+ */
286+ virtual std::unique_ptr<Private_Key> tls_kem_generate_key (TLS::Group_Params group, RandomNumberGenerator& rng);
287+
288+ /* *
289+ * Performs a key encapsulation operation (used for TLS 1.3 servers)
290+ *
291+ * Applications may use this to add custom KEM algorithms or entirely
292+ * different key exchange schemes to the TLS 1.3 handshake. For instance,
293+ * this could provide an entry point to implement a hybrid key exchange
294+ * with both a traditional algorithm like ECDH and a quantum-secure KEM.
295+ * Typical use cases of the library don't need to do that and serious
296+ * security risks are associated with customizing TLS's key encapsulation
297+ * mechanism.
298+ *
299+ * Note that the KEM interface is usable for TLS 1.3 handshakes, only.
300+ *
301+ * The default implementation implements this key encapsulation as a
302+ * combination of tls_generate_ephemeral_key() followed by
303+ * tls_ephemeral_key_agreement() with the provided @p encoded_public_key.
304+ * The just-generated ephemeral private key is destroyed immediately.
305+ *
306+ * @param group the group identifier of the KEM/KEX algorithm
307+ * @param encoded_public_key the public key used for encapsulation/KEX
308+ * @param rng a random number generator
309+ * @param policy a TLS policy object
310+ *
311+ * @returns the shared secret both in plaintext and encapsulated with
312+ * @p encoded_public_key.
313+ */
314+ virtual Encapsulation_Result tls_kem_encapsulate (TLS::Group_Params group,
315+ const std::vector<uint8_t >& encoded_public_key,
316+ RandomNumberGenerator& rng,
317+ const Policy& policy);
318+
319+ /* *
320+ * Performs a key decapsulation operation (used for TLS 1.3 clients).
321+ *
322+ * Applications may use this to add custom KEM algorithms or entirely
323+ * different key exchange schemes to the TLS 1.3 handshake. For instance,
324+ * this could provide an entry point to implement a hybrid key exchange
325+ * with both a traditional algorithm like ECDH and a quantum-secure KEM.
326+ * Typical use cases of the library don't need to do that and serious
327+ * security risks are associated with customizing TLS's key encapsulation
328+ * mechanism.
329+ *
330+ * Note that the KEM interface is usable for TLS 1.3 handshakes, only.
331+ *
332+ * The default implementation simply delegates this to the
333+ * tls_ephemeral_key_agreement() callback to obtain the shared secret.
334+ *
335+ * @param group the group identifier of the KEM/KEX algorithm
336+ * @param private_key the private key used for decapsulation/KEX
337+ * @param encapsulated_bytes the content to decapsulate (or the public key share)
338+ * @param rng a random number generator
339+ * @param policy a TLS policy object
340+ *
341+ * @returns the plaintext shared secret from @p encapsulated_bytes after
342+ * decapsulation with @p private_key.
343+ */
344+ virtual secure_vector<uint8_t > tls_kem_decapsulate (TLS::Group_Params group,
345+ const Private_Key& private_key,
346+ const std::vector<uint8_t >& encapsulated_bytes,
347+ RandomNumberGenerator& rng,
348+ const Policy& policy);
349+
258350 /* *
259351 * Generate an ephemeral key pair for the TLS handshake.
260352 *
0 commit comments