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