5 EVP_PKEY_derive_init, EVP_PKEY_derive_init_ex, EVP_PKEY_derive_set_peer,
6 EVP_PKEY_derive - derive public key algorithm shared secret
10 #include <openssl/evp.h>
12 int EVP_PKEY_derive_init_ex(EVP_PKEY_CTX *ctx, EVP_KEYEXCH *exchange);
13 int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx);
14 int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer);
15 int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
19 The EVP_PKEY_derive_init_ex() function initializes a public key algorithm
20 context for shared secret derivation using the key exchange algorithm
22 The key exchange algorithm B<exchange> should be fetched using a call to
23 L<EVP_KEYEXCH_fetch(3)>.
24 The EVP_PKEY object associated with B<ctx> must be compatible with that
26 B<exchange> may be NULL in which case the EVP_KEYEXCH algorithm is fetched
27 implicitly based on the type of EVP_PKEY associated with B<ctx>.
28 See L<provider(7)/Implicit fetch> for more information about implict fetches.
30 The EVP_PKEY_derive_init() function is the same as EVP_PKEY_derive_init_ex()
31 except that the EVP_KEYEXCH algorithm is always implicitly fetched.
33 The EVP_PKEY_derive_set_peer() function sets the peer key: this will normally
36 The EVP_PKEY_derive() derives a shared secret using B<ctx>.
37 If B<key> is B<NULL> then the maximum size of the output buffer is written to
38 the B<keylen> parameter. If B<key> is not B<NULL> then before the call the
39 B<keylen> parameter should contain the length of the B<key> buffer, if the call
40 is successful the shared secret is written to B<key> and the amount of data
45 After the call to EVP_PKEY_derive_init() or EVP_PKEY_derive_init_ex() algorithm
46 specific control operations can be performed to set any appropriate parameters
49 The function EVP_PKEY_derive() can be called more than once on the same
50 context if several operations are performed using the same parameters.
54 EVP_PKEY_derive_init_ex(), EVP_PKEY_derive_init() and EVP_PKEY_derive() return 1
55 for success and 0 or a negative value for failure.
56 In particular a return value of -2 indicates the operation is not supported by
57 the public key algorithm.
61 Derive shared secret (for example DH or EC keys):
63 #include <openssl/evp.h>
64 #include <openssl/rsa.h>
70 EVP_PKEY *pkey, *peerkey;
71 /* NB: assumes pkey, eng, peerkey have been already set up */
73 ctx = EVP_PKEY_CTX_new(pkey, eng);
76 if (EVP_PKEY_derive_init(ctx) <= 0)
78 if (EVP_PKEY_derive_set_peer(ctx, peerkey) <= 0)
81 /* Determine buffer length */
82 if (EVP_PKEY_derive(ctx, NULL, &skeylen) <= 0)
85 skey = OPENSSL_malloc(skeylen);
90 if (EVP_PKEY_derive(ctx, skey, &skeylen) <= 0)
93 /* Shared secret is skey bytes written to buffer skey */
97 L<EVP_PKEY_CTX_new(3)>,
98 L<EVP_PKEY_encrypt(3)>,
99 L<EVP_PKEY_decrypt(3)>,
101 L<EVP_PKEY_verify(3)>,
102 L<EVP_PKEY_verify_recover(3)>,
103 L<EVP_KEYEXCH_fetch(3)>
107 These functions were added in OpenSSL 1.0.0. The EVP_PKEY_derive_init_ex()
108 function was added in OpenSSL 3.0.
112 Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
114 Licensed under the Apache License 2.0 (the "License"). You may not use
115 this file except in compliance with the License. You can obtain a copy
116 in the file LICENSE in the source distribution or at
117 L<https://www.openssl.org/source/license.html>.