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