1EVP_PKEY_DERIVE(3) OpenSSL EVP_PKEY_DERIVE(3)
2
3
4
6 EVP_PKEY_derive_init, EVP_PKEY_derive_set_peer, EVP_PKEY_derive -
7 derive public key algorithm shared secret
8
10 #include <openssl/evp.h>
11
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);
15
17 The EVP_PKEY_derive_init() function initializes a public key algorithm
18 context using key pkey for shared secret derivation.
19
20 The EVP_PKEY_derive_set_peer() function sets the peer key: this will
21 normally be a public key.
22
23 The EVP_PKEY_derive() derives a shared secret using ctx. If key is
24 NULL then the maximum size of the output buffer is written to the
25 keylen parameter. If key is not NULL then before the call the keylen
26 parameter should contain the length of the key buffer, if the call is
27 successful the shared secret is written to key and the amount of data
28 written to keylen.
29
31 After the call to EVP_PKEY_derive_init() algorithm specific control
32 operations can be performed to set any appropriate parameters for the
33 operation.
34
35 The function EVP_PKEY_derive() can be called more than once on the same
36 context if several operations are performed using the same parameters.
37
39 EVP_PKEY_derive_init() and EVP_PKEY_derive() return 1 for success and 0
40 or a negative value for failure. In particular a return value of -2
41 indicates the operation is not supported by the public key algorithm.
42
44 Derive shared secret (for example DH or EC keys):
45
46 #include <openssl/evp.h>
47 #include <openssl/rsa.h>
48
49 EVP_PKEY_CTX *ctx;
50 ENGINE *eng;
51 unsigned char *skey;
52 size_t skeylen;
53 EVP_PKEY *pkey, *peerkey;
54 /* NB: assumes pkey, eng, peerkey have been already set up */
55
56 ctx = EVP_PKEY_CTX_new(pkey, eng);
57 if (!ctx)
58 /* Error occurred */
59 if (EVP_PKEY_derive_init(ctx) <= 0)
60 /* Error */
61 if (EVP_PKEY_derive_set_peer(ctx, peerkey) <= 0)
62 /* Error */
63
64 /* Determine buffer length */
65 if (EVP_PKEY_derive(ctx, NULL, &skeylen) <= 0)
66 /* Error */
67
68 skey = OPENSSL_malloc(skeylen);
69
70 if (!skey)
71 /* malloc failure */
72
73 if (EVP_PKEY_derive(ctx, skey, &skeylen) <= 0)
74 /* Error */
75
76 /* Shared secret is skey bytes written to buffer skey */
77
79 EVP_PKEY_CTX_new(3), EVP_PKEY_encrypt(3), EVP_PKEY_decrypt(3),
80 EVP_PKEY_sign(3), EVP_PKEY_verify(3), EVP_PKEY_verify_recover(3),
81
83 These functions were added in OpenSSL 1.0.0.
84
86 Copyright 2006-2019 The OpenSSL Project Authors. All Rights Reserved.
87
88 Licensed under the OpenSSL license (the "License"). You may not use
89 this file except in compliance with the License. You can obtain a copy
90 in the file LICENSE in the source distribution or at
91 <https://www.openssl.org/source/license.html>.
92
93
94
951.1.1q 2023-02-06 EVP_PKEY_DERIVE(3)