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