---
name: "libressl-EVP_PKEY_derive"
section: "3"
platform: "linux"
package: "libressl"
version: "4.4.0-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/libressl-EVP_PKEY_derive"
---

# libressl-EVP\_PKEY\_derive(3)

derive public key algorithm shared secret

## NAME

**EVP\_PKEY\_derive\_init**, **EVP\_PKEY\_derive\_set\_peer**, **EVP\_PKEY\_CTX\_get0\_peerkey**, **EVP\_PKEY\_derive** – derive public key algorithm shared secret

## SYNOPSIS

Crypto Library (libcrypto, -lcrypto)

**#include \<openssl/evp.h\>**

*int*\

**EVP\_PKEY\_derive\_init**(*EVP\_PKEY\_CTX \*ctx*);

*int*\

**EVP\_PKEY\_derive\_set\_peer**(*EVP\_PKEY\_CTX \*ctx*, *EVP\_PKEY \*peerkey*);

*EVP\_PKEY* *\**\

**EVP\_PKEY\_CTX\_get0\_peerkey**(*EVP\_PKEY\_CTX \*ctx*);

*int*\

**EVP\_PKEY\_derive**(*EVP\_PKEY\_CTX \*ctx*, *unsigned char \*key*, *size\_t \*keylen*);

## DESCRIPTION

**EVP\_PKEY\_derive\_init**() initializes the public key algorithm context *ctx* for shared secret derivation using the *EVP\_PKEY* object already stored in *ctx*. The library provides built-in support for keys with an EVP\_PKEY\_base\_id(3) of EVP\_PKEY\_DH, EVP\_PKEY\_EC, EVP\_PKEY\_HKDF, and EVP\_PKEY\_X25519.

After the call to **EVP\_PKEY\_derive\_init**(), algorithm specific control operations can optionally be performed to set any appropriate parameters for the operation.

**EVP\_PKEY\_derive\_set\_peer**() configures the *ctx*, which already needs to be initialized with **EVP\_PKEY\_derive\_init**(), EVP\_PKEY\_encrypt\_init(3), or EVP\_PKEY\_decrypt\_init(3), to use the *peerkey*, which is normally a public key. In case of success, the reference count of the *peerkey* is incremented by one. Consequently, the caller needs to call EVP\_PKEY\_free(3) on the *peerkey* when the caller no longer needs it, even if it is still in use by *ctx*.

**EVP\_PKEY\_derive**() derives a shared secret using *ctx*. If *key* is NULL, then the maximum size of the output buffer is written to the *keylen* parameter. If *key* is not NULL then before the call the *keylen* parameter should contain the length of the *key* buffer. If the call is successful, the shared secret is written to *key* and the amount of data written to *keylen*.

The function **EVP\_PKEY\_derive**() can be called more than once on the same context if several operations are performed using the same parameters.

## RETURN VALUES

**EVP\_PKEY\_derive\_init**(), **EVP\_PKEY\_derive\_set\_peer**(), and **EVP\_PKEY\_derive**() return 1 for success and 0 or a negative value for failure. In particular, a return value of -2 indicates the operation is not supported by the public key algorithm.

For **EVP\_PKEY\_derive\_set\_peer**(), a return value of -1 can for example occur if *ctx* is not properly initialized, does not contain an *EVP\_PKEY* that can be retrieved with EVP\_PKEY\_CTX\_get0\_pkey(3), the EVP\_PKEY\_id(3) of both keys mismatch, or EVP\_PKEY\_cmp\_parameters(3) reports mismatching key parameters.

**EVP\_PKEY\_derive**() fails with a return value of -1 for example if *ctx* has not been successfully initialized with **EVP\_PKEY\_derive\_init**().

**EVP\_PKEY\_CTX\_get0\_peerkey**() returns an internal pointer to the *peerkey* used by *ctx* without incrementing its reference count.

## EXAMPLES

Derive shared secret (for example DH or EC keys):

```
#include <openssl/evp.h>
#include <openssl/rsa.h>
EVP_PKEY_CTX *ctx;
unsigned char *skey;
size_t skeylen;
EVP_PKEY *pkey, *peerkey;
/* Assumes that pkey and peerkey have already been set up. */
ctx = EVP_PKEY_CTX_new(pkey, NULL);
if (!ctx)
	/* Error occurred */
if (EVP_PKEY_derive_init(ctx) <= 0)
	/* Error */
if (EVP_PKEY_derive_set_peer(ctx, peerkey) <= 0)
	/* Error */
/* Determine buffer length */
if (EVP_PKEY_derive(ctx, NULL, &skeylen) <= 0)
	/* Error */
skey = malloc(skeylen);
if (!skey)
	/* malloc failure */
if (EVP_PKEY_derive(ctx, skey, &skeylen) <= 0)
	/* Error */
/* Shared secret is skey bytes written to buffer skey */
```

## SEE ALSO

EVP\_PKEY\_CTX\_new(3), EVP\_PKEY\_decrypt(3), EVP\_PKEY\_encrypt(3), EVP\_PKEY\_sign(3), EVP\_PKEY\_verify(3), EVP\_PKEY\_verify\_recover(3), X25519(3)

## HISTORY

**EVP\_PKEY\_derive\_init**(), **EVP\_PKEY\_derive\_set\_peer**(), **EVP\_PKEY\_CTX\_get0\_peerkey**(), and **EVP\_PKEY\_derive**() first appeared in OpenSSL 1.0.0 and have been available since OpenBSD 4.9.
