EVP_PKEY_DECAPSULATE(3ossl)
π EVP_PKEY_DECAPSULATE β okay bestie, let's talk key decapsulation
Sooo this is a page from OpenSSL (the 3ossl manual, iykyk) and it's all about taking encapsulated data and unwrapping it with a private key using a KEM algorithm. It's giving cryptography main character energy, fr fr. π¦
β¨ NAME β¨
EVP_PKEY_decapsulate_init, EVP_PKEY_auth_decapsulate_init, and EVP_PKEY_decapsulate β key decapsulation using a KEM algorithm with a private key. Three functions, one vibe. π
πΈ SYNOPSIS πΈ
#include <openssl/evp.h>
int EVP_PKEY_decapsulate_init(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]);
int EVP_PKEY_auth_decapsulate_init(EVP_PKEY_CTX *ctx, EVP_PKEY *authpub,
const OSSL_PARAM params[]);
int EVP_PKEY_decapsulate(EVP_PKEY_CTX *ctx,
unsigned char *unwrapped, size_t *unwrappedlen,
const unsigned char *wrapped, size_t wrappedlen);
π DESCRIPTION π
Okay so first up: EVP_PKEY_decapsulate_init() initializes a private key algorithm context ctx for a decapsulation operation, and then sets the params on the context the exact same way as calling EVP_PKEY_CTX_set_params(3) would. Little heads up: ctx is usually produced using EVP_PKEY_CTX_new_from_pkey(3), where you specify the private key you wanna use. π
Then there's EVP_PKEY_auth_decapsulate_init(), which is basically the same as EVP_PKEY_decapsulate_init() except it also passes an authpub authentication public key that gets used during decapsulation. Extra key, extra slay. π«Ά
EVP_PKEY_decapsulate() is the one that actually does the private key decapsulation operation using ctx. The data to be decapsulated is specified with the wrapped and wrappedlen parameters (and both of these must be non-NULL, no exceptions bestie).
The wrapped parameter is an output argument, to which the decapsulated shared secret is written. β οΈ And here's the important part, so listen up: the shared secret may not match the peer's value even when decapsulation returns success. Instead, the shared secret must be used to derive a key that is used to authenticate data subsequently received from the peer. That's the whole move, that's the assignment.
If unwrapped is NULL, then the size of the output shared secret buffer is written to *unwrappedlen and no decapsulation is performed β which is lowkey iconic because it lets you figure out the required buffer size at run time. Otherwise, the decapsulated secret data is written to unwrapped and the length of the shared secret is written to *unwrappedlen.
Not me warning you again but: the value pointed to byunwrappedlen(which must NOT beNULL) must be initialised to the length ofunwrapped, so that the call can validate it's of sufficient size to hold the result of the operation. Do it. Please. π
And unless you have detailed prior knowledge of the internals of the specific KEM algorithm, callers SHOULD NOT assume that the returned shared secret is necessarily of the maximum possible length. Use the length returned via *unwrappedlen to determine the actual length of the output. That's the whole mood. π
π§ NOTES π§
After the call to EVP_PKEY_decapsulate_init(), algorithm-specific parameters for the operation may be set or modified using EVP_PKEY_CTX_set_params(3). Customize your little heart out. β¨
π₯ RETURN VALUES π₯
EVP_PKEY_decapsulate_init(), EVP_PKEY_auth_decapsulate_init() and EVP_PKEY_decapsulate() all return 1 for success and 0 or a negative value for failure. π And specifically, a return value of -2 indicates the operation is not supported by the private key algorithm.
π EXAMPLES π
Decapsulate data using RSA, bestie:
#include <openssl/evp.h>
/*
* NB: assumes rsa_priv_key is an RSA private key,
* and that in, inlen are already set up to contain encapsulated data.
*/
EVP_PKEY_CTX *ctx = NULL;
size_t secretlen = 0;
unsigned char *secret = NULL;;
ctx = EVP_PKEY_CTX_new_from_pkey(libctx, rsa_priv_key, NULL);
if (ctx == NULL)
/* Error */
if (EVP_PKEY_decapsulate_init(ctx, NULL) <= 0)
/* Error */
/* Set the mode - only 'RSASVE' is currently supported */
if (EVP_PKEY_CTX_set_kem_op(ctx, "RSASVE") <= 0)
/* Error */
/* Determine buffer length */
if (EVP_PKEY_decapsulate(ctx, NULL, &secretlen, in, inlen) <= 0)
/* Error */
secret = OPENSSL_malloc(secretlen);
if (secret == NULL)
/* malloc failure */
/* Decapsulated secret data is secretlen bytes long */
if (EVP_PKEY_decapsulate(ctx, secret, &secretlen, in, inlen) <= 0)
/* Error */
(Yes, the double semicolon after secret = NULL is really in there. Ate and left no crumbs. π)
π SEE ALSO π
EVP_PKEY_CTX_new_from_pkey(3), EVP_PKEY_encapsulate(3), EVP_KEM-RSA(7), EVP_KEM-X25519(7), EVP_KEM-EC(7), EVP_KEM-ML-KEM-512(7), EVP_KEM-ML-KEM-768(7), EVP_KEM-ML-KEM-1024(7)
Go read them, they're literally all so good. π
π HISTORY π
- The functions
EVP_PKEY_decapsulate_init()andEVP_PKEY_decapsulate()were added in OpenSSL 3.0. - The function
EVP_PKEY_auth_decapsulate_init()was added in OpenSSL 3.2. - Support for ML-KEM was added in OpenSSL 3.5.
A whole glow-up timeline, honestly. β
π COPYRIGHT π
Copyright 2020-2025 The OpenSSL Project Authors. All Rights Reserved.
Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.
Okay that's the whole page bestie, you're officially a decapsulation expert π π¦ This was EVP_PKEY_DECAPSULATE(3ossl) from OpenSSL 3.5.6, dated 2026-04-07 β mwah, go unwrap something responsibly π«Άβ¨
π¦ where this page comes from
- freebsd 15.1: base 15.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms