xref: /openssl/doc/man3/EVP_PKEY_encapsulate.pod (revision da1c088f)
1=pod
2
3=head1 NAME
4
5EVP_PKEY_encapsulate_init, EVP_PKEY_auth_encapsulate_init, EVP_PKEY_encapsulate
6- Key encapsulation using a KEM algorithm with a public key
7
8=head1 SYNOPSIS
9
10 #include <openssl/evp.h>
11
12 int EVP_PKEY_encapsulate_init(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]);
13 int EVP_PKEY_auth_encapsulate_init(EVP_PKEY_CTX *ctx, EVP_PKEY *authpriv,
14                                   const OSSL_PARAM params[]);
15 int EVP_PKEY_encapsulate(EVP_PKEY_CTX *ctx,
16                          unsigned char *wrappedkey, size_t *wrappedkeylen,
17                          unsigned char *genkey, size_t *genkeylen);
18
19=head1 DESCRIPTION
20
21The EVP_PKEY_encapsulate_init() function initializes a public key algorithm
22context I<ctx> for an encapsulation 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> is usually is produced using L<EVP_PKEY_CTX_new_from_pkey(3)>,
25specifying the public key to use.
26
27The EVP_PKEY_auth_encapsulate_init() function is similar to
28EVP_PKEY_encapsulate_init() but also passes an I<authpriv> authentication private
29key that is used during encapsulation.
30
31The EVP_PKEY_encapsulate() function performs a public key encapsulation
32operation using I<ctx>.
33The symmetric secret generated in I<genkey> can be used as key material.
34The ciphertext in I<wrappedkey> is its encapsulated form, which can be sent
35to another party, who can use L<EVP_PKEY_decapsulate(3)> to retrieve it
36using their private key.
37If I<wrappedkey> is NULL then the maximum size of the output buffer
38is written to the I<*wrappedkeylen> parameter unless I<wrappedkeylen> is NULL
39and the maximum size of the generated key buffer is written to I<*genkeylen>
40unless I<genkeylen> is NULL.
41If I<wrappedkey> is not NULL and the call is successful then the
42internally generated key is written to I<genkey> and its size is written to
43I<*genkeylen>. The encapsulated version of the generated key is written to
44I<wrappedkey> and its size is written to I<*wrappedkeylen>.
45
46=head1 NOTES
47
48After the call to EVP_PKEY_encapsulate_init() algorithm-specific parameters
49for the operation may be set or modified using L<EVP_PKEY_CTX_set_params(3)>.
50
51=head1 RETURN VALUES
52
53EVP_PKEY_encapsulate_init(), EVP_PKEY_auth_encapsulate_init() and
54EVP_PKEY_encapsulate() return 1 for success and 0 or a negative value for
55failure. In particular a return value of -2 indicates the operation is not
56supported by the public key algorithm.
57
58=head1 EXAMPLES
59
60Encapsulate an RSASVE key (for RSA keys).
61
62 #include <openssl/evp.h>
63
64 /*
65  * NB: assumes rsa_pub_key is an public key of another party.
66  */
67
68 EVP_PKEY_CTX *ctx = NULL;
69 size_t secretlen = 0, outlen = 0;
70 unsigned char *out = NULL, *secret = NULL;
71
72 ctx = EVP_PKEY_CTX_new_from_pkey(libctx, rsa_pub_key, NULL);
73 if (ctx = NULL)
74     /* Error */
75 if (EVP_PKEY_encapsulate_init(ctx, NULL) <= 0)
76     /* Error */
77
78 /* Set the mode - only 'RSASVE' is currently supported */
79  if (EVP_PKEY_CTX_set_kem_op(ctx, "RSASVE") <= 0)
80     /* Error */
81 /* Determine buffer length */
82 if (EVP_PKEY_encapsulate(ctx, NULL, &outlen, NULL, &secretlen) <= 0)
83     /* Error */
84
85 out = OPENSSL_malloc(outlen);
86 secret = OPENSSL_malloc(secretlen);
87 if (out == NULL || secret == NULL)
88     /* malloc failure */
89
90 /*
91  * The generated 'secret' can be used as key material.
92  * The encapsulated 'out' can be sent to another party who can
93  * decapsulate it using their private key to retrieve the 'secret'.
94  */
95 if (EVP_PKEY_encapsulate(ctx, out, &outlen, secret, &secretlen) <= 0)
96     /* Error */
97
98=head1 SEE ALSO
99
100L<EVP_PKEY_CTX_new_from_pkey(3)>,
101L<EVP_PKEY_decapsulate(3)>,
102L<EVP_KEM-RSA(7)>, L<EVP_KEM-X25519(7)>, L<EVP_KEM-EC(7)>
103
104=head1 HISTORY
105
106These functions EVP_PKEY_encapsulate_init() and EVP_PKEY_encapsulate() were
107added in OpenSSL 3.0.
108The function EVP_PKEY_auth_encapsulate_init() was added in OpenSSL 3.2.
109
110=head1 COPYRIGHT
111
112Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
113
114Licensed under the Apache License 2.0 (the "License").  You may not use
115this file except in compliance with the License.  You can obtain a copy
116in the file LICENSE in the source distribution or at
117L<https://www.openssl.org/source/license.html>.
118
119=cut
120