1=pod
2
3=head1 NAME
4
5EVP_PKEY_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_decapsulate(EVP_PKEY_CTX *ctx,
14                          unsigned char *unwrapped, size_t *unwrappedlen,
15                          const unsigned char *wrapped, size_t wrappedlen);
16
17=head1 DESCRIPTION
18
19The EVP_PKEY_decapsulate_init() function initializes a private key algorithm
20context I<ctx> for a decapsulation operation and then sets the I<params>
21on the context in the same way as calling L<EVP_PKEY_CTX_set_params(3)>.
22Note that I<ctx> usually is produced using L<EVP_PKEY_CTX_new_from_pkey(3)>,
23specifying the private key to use.
24
25The EVP_PKEY_decapsulate() function performs a private key decapsulation
26operation using I<ctx>. The data to be decapsulated is specified using the
27I<wrapped> and I<wrappedlen> parameters.
28If I<unwrapped> is NULL then the maximum size of the output secret buffer
29is written to I<*unwrappedlen>. If I<unwrapped> is not NULL and the
30call is successful then the decapsulated secret data is written to I<unwrapped>
31and the amount of data written to I<*unwrappedlen>.
32
33=head1 NOTES
34
35After the call to EVP_PKEY_decapsulate_init() algorithm-specific parameters
36for the operation may be set or modified using L<EVP_PKEY_CTX_set_params(3)>.
37
38=head1 RETURN VALUES
39
40EVP_PKEY_decapsulate_init() and EVP_PKEY_decapsulate() return 1 for
41success and 0 or a negative value for failure. In particular a return value of -2
42indicates the operation is not supported by the private key algorithm.
43
44=head1 EXAMPLES
45
46Decapsulate data using RSA:
47
48 #include <openssl/evp.h>
49
50 /*
51  * NB: assumes rsa_priv_key is an RSA private key,
52  * and that in, inlen are already set up to contain encapsulated data.
53  */
54
55 EVP_PKEY_CTX *ctx = NULL;
56 size_t secretlen = 0;
57 unsigned char *secret = NULL;;
58
59 ctx = EVP_PKEY_CTX_new_from_pkey(libctx, rsa_priv_key, NULL);
60 if (ctx = NULL)
61     /* Error */
62 if (EVP_PKEY_decapsulate_init(ctx, NULL) <= 0)
63     /* Error */
64
65 /* Set the mode - only 'RSASVE' is currently supported */
66 if (EVP_PKEY_CTX_set_kem_op(ctx, "RSASVE") <= 0)
67     /* Error */
68
69 /* Determine buffer length */
70 if (EVP_PKEY_decapsulate(ctx, NULL, &secretlen, in, inlen) <= 0)
71     /* Error */
72
73 secret = OPENSSL_malloc(secretlen);
74 if (secret == NULL)
75     /* malloc failure */
76
77 /* Decapsulated secret data is secretlen bytes long */
78 if (EVP_PKEY_decapsulaterctx, secret, &secretlen, in, inlen) <= 0)
79     /* Error */
80
81
82=head1 SEE ALSO
83
84L<EVP_PKEY_CTX_new_from_pkey(3)>,
85L<EVP_PKEY_encapsulate(3)>,
86L<EVP_KEM-RSA(7)>,
87
88=head1 HISTORY
89
90These functions were added in OpenSSL 3.0.
91
92=head1 COPYRIGHT
93
94Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
95
96Licensed under the Apache License 2.0 (the "License").  You may not use
97this file except in compliance with the License.  You can obtain a copy
98in the file LICENSE in the source distribution or at
99L<https://www.openssl.org/source/license.html>.
100
101=cut
102