1EVP_PKEY_derive(3)                  OpenSSL                 EVP_PKEY_derive(3)
2
3
4

NAME

6       EVP_PKEY_derive_init, EVP_PKEY_derive_set_peer, EVP_PKEY_derive -
7       derive public key algorithm shared secret.
8

SYNOPSIS

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

DESCRIPTION

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

NOTES

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

RETURN VALUES

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

EXAMPLE

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        unsigned char *skey;
51        size_t skeylen;
52        EVP_PKEY *pkey, *peerkey;
53        /* NB: assumes pkey, peerkey have been already set up */
54
55        ctx = EVP_PKEY_CTX_new(pkey);
56        if (!ctx)
57               /* Error occurred */
58        if (EVP_PKEY_derive_init(ctx) <= 0)
59               /* Error */
60        if (EVP_PKEY_derive_set_peer(ctx, peerkey) <= 0)
61               /* Error */
62
63        /* Determine buffer length */
64        if (EVP_PKEY_derive(ctx, NULL, &skeylen) <= 0)
65               /* Error */
66
67        skey = OPENSSL_malloc(skeylen);
68
69        if (!skey)
70               /* malloc failure */
71
72        if (EVP_PKEY_derive(ctx, skey, &skeylen) <= 0)
73               /* Error */
74
75        /* Shared secret is skey bytes written to buffer skey */
76

SEE ALSO

78       EVP_PKEY_CTX_new(3), EVP_PKEY_encrypt(3), EVP_PKEY_decrypt(3),
79       EVP_PKEY_sign(3), EVP_PKEY_verify(3), EVP_PKEY_verify_recover(3),
80

HISTORY

82       These functions were first added to OpenSSL 1.0.0.
83
84
85
861.0.2o                            2020-08-01                EVP_PKEY_derive(3)
Impressum