xref: /openssl/doc/man3/EVP_PKEY_decapsulate.pod (revision da1c088f)
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.
34If I<unwrapped> is NULL then the maximum size of the output secret buffer
35is written to I<*unwrappedlen>. If I<unwrapped> is not NULL and the
36call is successful then the decapsulated secret data is written to I<unwrapped>
37and the amount of data written to I<*unwrappedlen>.
38
39=head1 NOTES
40
41After the call to EVP_PKEY_decapsulate_init() algorithm-specific parameters
42for the operation may be set or modified using L<EVP_PKEY_CTX_set_params(3)>.
43
44=head1 RETURN VALUES
45
46EVP_PKEY_decapsulate_init(), EVP_PKEY_auth_decapsulate_init() and
47EVP_PKEY_decapsulate() return 1 for success and 0 or a negative value for
48failure. In particular a return value of -2 indicates the operation is not
49supported by the private key algorithm.
50
51=head1 EXAMPLES
52
53Decapsulate data using RSA:
54
55 #include <openssl/evp.h>
56
57 /*
58  * NB: assumes rsa_priv_key is an RSA private key,
59  * and that in, inlen are already set up to contain encapsulated data.
60  */
61
62 EVP_PKEY_CTX *ctx = NULL;
63 size_t secretlen = 0;
64 unsigned char *secret = NULL;;
65
66 ctx = EVP_PKEY_CTX_new_from_pkey(libctx, rsa_priv_key, NULL);
67 if (ctx = NULL)
68     /* Error */
69 if (EVP_PKEY_decapsulate_init(ctx, NULL) <= 0)
70     /* Error */
71
72 /* Set the mode - only 'RSASVE' is currently supported */
73 if (EVP_PKEY_CTX_set_kem_op(ctx, "RSASVE") <= 0)
74     /* Error */
75
76 /* Determine buffer length */
77 if (EVP_PKEY_decapsulate(ctx, NULL, &secretlen, in, inlen) <= 0)
78     /* Error */
79
80 secret = OPENSSL_malloc(secretlen);
81 if (secret == NULL)
82     /* malloc failure */
83
84 /* Decapsulated secret data is secretlen bytes long */
85 if (EVP_PKEY_decapsulate(ctx, secret, &secretlen, in, inlen) <= 0)
86     /* Error */
87
88
89=head1 SEE ALSO
90
91L<EVP_PKEY_CTX_new_from_pkey(3)>,
92L<EVP_PKEY_encapsulate(3)>,
93L<EVP_KEM-RSA(7)>, L<EVP_KEM-X25519(7)>, L<EVP_KEM-EC(7)>
94
95=head1 HISTORY
96
97The functions EVP_PKEY_decapsulate_init() and EVP_PKEY_decapsulate() were added
98in OpenSSL 3.0.
99
100The function EVP_PKEY_auth_decapsulate_init() was added in OpenSSL 3.2.
101
102=head1 COPYRIGHT
103
104Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
105
106Licensed under the Apache License 2.0 (the "License").  You may not use
107this file except in compliance with the License.  You can obtain a copy
108in the file LICENSE in the source distribution or at
109L<https://www.openssl.org/source/license.html>.
110
111=cut
112