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