xref: /openssl/doc/man3/EVP_PKEY_encapsulate.pod (revision 1c1223ff)
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>.  Note that if
45I<wrappedlen> is not NULL, then the value it points to must initially hold the size of
46the  I<unwrapped> buffer so that its size can be validated by the call, ensuring
47it is large enough to hold the result written to I<wrapped>.
48
49=head1 NOTES
50
51After the call to EVP_PKEY_encapsulate_init() algorithm-specific parameters
52for the operation may be set or modified using L<EVP_PKEY_CTX_set_params(3)>.
53
54=head1 RETURN VALUES
55
56EVP_PKEY_encapsulate_init(), EVP_PKEY_auth_encapsulate_init() and
57EVP_PKEY_encapsulate() return 1 for success and 0 or a negative value for
58failure. In particular a return value of -2 indicates the operation is not
59supported by the public key algorithm.
60
61=head1 EXAMPLES
62
63Encapsulate an RSASVE key (for RSA keys).
64
65 #include <openssl/evp.h>
66
67 /*
68  * NB: assumes rsa_pub_key is an public key of another party.
69  */
70
71 EVP_PKEY_CTX *ctx = NULL;
72 size_t secretlen = 0, outlen = 0;
73 unsigned char *out = NULL, *secret = NULL;
74
75 ctx = EVP_PKEY_CTX_new_from_pkey(libctx, rsa_pub_key, NULL);
76 if (ctx == NULL)
77     /* Error */
78 if (EVP_PKEY_encapsulate_init(ctx, NULL) <= 0)
79     /* Error */
80
81 /* Set the mode - only 'RSASVE' is currently supported */
82  if (EVP_PKEY_CTX_set_kem_op(ctx, "RSASVE") <= 0)
83     /* Error */
84 /* Determine buffer length */
85 if (EVP_PKEY_encapsulate(ctx, NULL, &outlen, NULL, &secretlen) <= 0)
86     /* Error */
87
88 out = OPENSSL_malloc(outlen);
89 secret = OPENSSL_malloc(secretlen);
90 if (out == NULL || secret == NULL)
91     /* malloc failure */
92
93 /*
94  * The generated 'secret' can be used as key material.
95  * The encapsulated 'out' can be sent to another party who can
96  * decapsulate it using their private key to retrieve the 'secret'.
97  */
98 if (EVP_PKEY_encapsulate(ctx, out, &outlen, secret, &secretlen) <= 0)
99     /* Error */
100
101=head1 SEE ALSO
102
103L<EVP_PKEY_CTX_new_from_pkey(3)>,
104L<EVP_PKEY_decapsulate(3)>,
105L<EVP_KEM-RSA(7)>, L<EVP_KEM-X25519(7)>, L<EVP_KEM-EC(7)>
106
107=head1 HISTORY
108
109These functions EVP_PKEY_encapsulate_init() and EVP_PKEY_encapsulate() were
110added in OpenSSL 3.0.
111The function EVP_PKEY_auth_encapsulate_init() was added in OpenSSL 3.2.
112
113=head1 COPYRIGHT
114
115Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
116
117Licensed under the Apache License 2.0 (the "License").  You may not use
118this file except in compliance with the License.  You can obtain a copy
119in the file LICENSE in the source distribution or at
120L<https://www.openssl.org/source/license.html>.
121
122=cut
123