1=pod 2 3=head1 NAME 4 5EVP_PKEY_decapsulate_init, EVP_PKEY_auth_decapsulate_init, EVP_PKEY_decapsulate 6- Key decapsulation using a KEM algorithm with a private key 7 8=head1 SYNOPSIS 9 10 #include <openssl/evp.h> 11 12 int EVP_PKEY_decapsulate_init(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]); 13 int EVP_PKEY_auth_decapsulate_init(EVP_PKEY_CTX *ctx, EVP_PKEY *authpub, 14 const OSSL_PARAM params[]); 15 int EVP_PKEY_decapsulate(EVP_PKEY_CTX *ctx, 16 unsigned char *unwrapped, size_t *unwrappedlen, 17 const unsigned char *wrapped, size_t wrappedlen); 18 19=head1 DESCRIPTION 20 21The EVP_PKEY_decapsulate_init() function initializes a private key algorithm 22context I<ctx> for a decapsulation operation and then sets the I<params> 23on the context in the same way as calling L<EVP_PKEY_CTX_set_params(3)>. 24Note that I<ctx> usually is produced using L<EVP_PKEY_CTX_new_from_pkey(3)>, 25specifying the private key to use. 26 27The EVP_PKEY_auth_decapsulate_init() function is similar to 28EVP_PKEY_decapsulate_init() but also passes an I<authpub> authentication public 29key that is used during decapsulation. 30 31The EVP_PKEY_decapsulate() function performs a private key decapsulation 32operation using I<ctx>. The data to be decapsulated is specified using the 33I<wrapped> and I<wrappedlen> parameters (which must both non-NULL). 34 35The I<wrapped> parameter is an output argument, to which the decapsulated 36shared secret is written. 37The shared secret may not match the peer's value even when decapsulation 38returns success. 39Instead, the shared secret must be used to derive a key that is used to 40authenticate data subsequently received from the peer. 41If I<unwrapped> is NULL then the size of the output shared secret buffer is 42written to I<*unwrappedlen> and no decapsulation is performed, this makes it 43possible to determine the required buffer size at run time. Otherwise, the 44decapsulated secret data is written to I<unwrapped> and the length of shared 45secret is written to I<*unwrappedlen>. 46 47Note that the value pointed to by I<unwrappedlen> (which must NOT be B<NULL>) 48must be initialised to the length of I<unwrapped>, so that the call can 49validate it is of sufficient size to hold the result of the operation. 50 51Absent detailed prior knowledge of the internals of the specific KEM 52algorithm, callers SHOULD NOT assume that the returned shared secret 53is necessarily of the maximum possible length. 54The length returned via I<*unwrappedlen> SHOULD be used to determine the actual 55length of the output. 56 57=head1 NOTES 58 59After the call to EVP_PKEY_decapsulate_init() algorithm-specific parameters 60for the operation may be set or modified using L<EVP_PKEY_CTX_set_params(3)>. 61 62=head1 RETURN VALUES 63 64EVP_PKEY_decapsulate_init(), EVP_PKEY_auth_decapsulate_init() and 65EVP_PKEY_decapsulate() return 1 for success and 0 or a negative value for 66failure. In particular a return value of -2 indicates the operation is not 67supported by the private key algorithm. 68 69=head1 EXAMPLES 70 71Decapsulate data using RSA: 72 73 #include <openssl/evp.h> 74 75 /* 76 * NB: assumes rsa_priv_key is an RSA private key, 77 * and that in, inlen are already set up to contain encapsulated data. 78 */ 79 80 EVP_PKEY_CTX *ctx = NULL; 81 size_t secretlen = 0; 82 unsigned char *secret = NULL;; 83 84 ctx = EVP_PKEY_CTX_new_from_pkey(libctx, rsa_priv_key, NULL); 85 if (ctx == NULL) 86 /* Error */ 87 if (EVP_PKEY_decapsulate_init(ctx, NULL) <= 0) 88 /* Error */ 89 90 /* Set the mode - only 'RSASVE' is currently supported */ 91 if (EVP_PKEY_CTX_set_kem_op(ctx, "RSASVE") <= 0) 92 /* Error */ 93 94 /* Determine buffer length */ 95 if (EVP_PKEY_decapsulate(ctx, NULL, &secretlen, in, inlen) <= 0) 96 /* Error */ 97 98 secret = OPENSSL_malloc(secretlen); 99 if (secret == NULL) 100 /* malloc failure */ 101 102 /* Decapsulated secret data is secretlen bytes long */ 103 if (EVP_PKEY_decapsulate(ctx, secret, &secretlen, in, inlen) <= 0) 104 /* Error */ 105 106 107=head1 SEE ALSO 108 109L<EVP_PKEY_CTX_new_from_pkey(3)>, 110L<EVP_PKEY_encapsulate(3)>, 111L<EVP_KEM-RSA(7)>, 112L<EVP_KEM-X25519(7)>, 113L<EVP_KEM-EC(7)>, 114L<EVP_KEM-ML-KEM-512(7)>, 115L<EVP_KEM-ML-KEM-768(7)>, 116L<EVP_KEM-ML-KEM-1024(7)> 117 118=head1 HISTORY 119 120The functions EVP_PKEY_decapsulate_init() and EVP_PKEY_decapsulate() were added 121in OpenSSL 3.0. 122 123The function EVP_PKEY_auth_decapsulate_init() was added in OpenSSL 3.2. 124 125Support for B<ML-KEM> was added in OpenSSL 3.5. 126 127=head1 COPYRIGHT 128 129Copyright 2020-2025 The OpenSSL Project Authors. All Rights Reserved. 130 131Licensed under the Apache License 2.0 (the "License"). You may not use 132this file except in compliance with the License. You can obtain a copy 133in the file LICENSE in the source distribution or at 134L<https://www.openssl.org/source/license.html>. 135 136=cut 137