EVP_PKEY_DECAPSULATE(3SSL)

✨ EVP_PKEY_DECAPSULATE πŸ’… β€” the OpenSSL manual, section 3SSL

okay bestie, gather round, because this one is the key decapsulation using a KEM algorithm with a private key page and it's lowkey iconic πŸ”₯. yes, it lives in the OpenSSL docs, yes it's section 3SSL, and yes, we're gonna get through it together 🫢


🌸 NAME 🌸

EVP_PKEY_decapsulate_init, EVP_PKEY_auth_decapsulate_init, EVP_PKEY_decapsulate β€” decapsulating keys with a KEM algorithm using a private key πŸ’–


πŸŽ€ 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 πŸ’œ

so, EVP_PKEY_decapsulate_init() sets up a private key algorithm context ctx for a decapsulation operation, and then it sets the params on that context the exact same way calling EVP_PKEY_CTX_set_params(3) would. and pookie, note this: ctx is usually the one you got from EVP_PKEY_CTX_new_from_pkey(3), where you specified which private key you want to use πŸ”‘

then EVP_PKEY_auth_decapsulate_init() is basically the same vibe as EVP_PKEY_decapsulate_init(), except it also hands over an authpub authentication public key that gets used during decapsulation 🫢

and now the main character: EVP_PKEY_decapsulate() does the actual private key decapsulation operation using ctx. the data you're decapsulating comes in through the wrapped and wrappedlen parameters (and bestie, both of those must be non-NULL, no cap).

here's the part that trips people up πŸ™ˆ β€” the doc literally says wrapped is an output argument, and the decapsulated shared secret gets written there. and the shared secret may not match the peer's value even when decapsulation returns success 😭. so instead, that shared secret has to be used to derive a key, and that derived key is what authenticates data subsequently received from the peer. periodt.

if unwrapped is NULL, then the size of the output shared secret buffer gets written to *unwrappedlen and no decapsulation is performed β€” which is actually so convenient because it lets you figure out the buffer size you need at run time ✨. otherwise, the decapsulated secret data goes into unwrapped, and the length of the shared secret gets written to *unwrappedlen.

⚠️ heads up: the value pointed to by unwrappedlen (which must NOT be NULL, like, ever) has to be initialised to the length of unwrapped first, so the call can validate that it's big enough to hold the result of the operation. skip that and it's giving chaos πŸ’€

and unless you have detailed prior knowledge of the internals of the specific KEM algorithm, callers SHOULD NOT assume 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 πŸ‘‘


πŸ¦‹ NOTES πŸ¦‹

after you've called EVP_PKEY_decapsulate_init(), algorithm-specific parameters for the operation can be set or modified using EVP_PKEY_CTX_set_params(3) 🌈


⭐ 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 means the operation is not supported by the private key algorithm 🚫


πŸ“ EXAMPLES πŸ“

decapsulating data using RSA, look at her go:

#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 */

πŸ‘‘ 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)


🌈 HISTORY 🌈

  • EVP_PKEY_decapsulate_init() and EVP_PKEY_decapsulate() were added in OpenSSL 3.0.
  • EVP_PKEY_auth_decapsulate_init() was added in OpenSSL 3.2.
  • Support for ML-KEM was added in OpenSSL 3.5.

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, you ate that πŸŽ€ this was OpenSSL 4.0.3, dated 2026-09-29, and it's the EVP_PKEY_DECAPSULATE(3SSL) manual β€” go forth and decapsulate, bestie πŸ’…βœ¨πŸ«Ά

πŸ“¦ where this page comes from