5 EVP_PKEY_derive_init, EVP_PKEY_derive_set_peer, EVP_PKEY_derive
6 - derive public key algorithm shared secret
10 #include <openssl/evp.h>
12 int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx);
13 int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer);
14 int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
18 EVP_PKEY_derive_init() initializes a public key algorithm context I<ctx> for
19 shared secret derivation using the algorithm given when the context was created
20 using L<EVP_PKEY_CTX_new(3)> or variants thereof. The algorithm is used to
21 fetch a B<EVP_KEYEXCH> method implicitly, see L<provider(7)/Implicit fetch> for
22 more information about implict fetches.
24 EVP_PKEY_derive_set_peer() sets the peer key: this will normally
27 EVP_PKEY_derive() derives a shared secret using I<ctx>.
28 If I<key> is NULL then the maximum size of the output buffer is written to the
29 I<keylen> parameter. If I<key> is not NULL then before the call the I<keylen>
30 parameter should contain the length of the I<key> buffer, if the call is
31 successful the shared secret is written to I<key> and the amount of data
36 After the call to EVP_PKEY_derive_init(), algorithm
37 specific control operations can be performed to set any appropriate parameters
40 The function EVP_PKEY_derive() can be called more than once on the same
41 context if several operations are performed using the same parameters.
45 EVP_PKEY_derive_init() and EVP_PKEY_derive() return 1
46 for success and 0 or a negative value for failure.
47 In particular a return value of -2 indicates the operation is not supported by
48 the public key algorithm.
52 Derive shared secret (for example DH or EC keys):
54 #include <openssl/evp.h>
55 #include <openssl/rsa.h>
61 EVP_PKEY *pkey, *peerkey;
62 /* NB: assumes pkey, eng, peerkey have been already set up */
64 ctx = EVP_PKEY_CTX_new(pkey, eng);
67 if (EVP_PKEY_derive_init(ctx) <= 0)
69 if (EVP_PKEY_derive_set_peer(ctx, peerkey) <= 0)
72 /* Determine buffer length */
73 if (EVP_PKEY_derive(ctx, NULL, &skeylen) <= 0)
76 skey = OPENSSL_malloc(skeylen);
81 if (EVP_PKEY_derive(ctx, skey, &skeylen) <= 0)
84 /* Shared secret is skey bytes written to buffer skey */
88 L<EVP_PKEY_CTX_new(3)>,
89 L<EVP_PKEY_encrypt(3)>,
90 L<EVP_PKEY_decrypt(3)>,
92 L<EVP_PKEY_verify(3)>,
93 L<EVP_PKEY_verify_recover(3)>,
94 L<EVP_KEYEXCH_fetch(3)>
98 These functions were added in OpenSSL 1.0.0.
102 Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
104 Licensed under the Apache License 2.0 (the "License"). You may not use
105 this file except in compliance with the License. You can obtain a copy
106 in the file LICENSE in the source distribution or at
107 L<https://www.openssl.org/source/license.html>.