---
name: "libressl-DH_generate_key"
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-DH_generate_key"
---

# libressl-DH\_generate\_key(3)

perform Diffie-Hellman key exchange

## NAME

**DH\_generate\_key**, **DH\_compute\_key** – perform Diffie-Hellman key exchange

## SYNOPSIS

Crypto Library (libcrypto, -lcrypto)

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

*int*\

**DH\_generate\_key**(*DH \*dh*);

*int*\

**DH\_compute\_key**(*unsigned char \*key*, *BIGNUM \*pub\_key*, *DH \*dh*);

## DESCRIPTION

**DH\_generate\_key**() performs the first step of a Diffie-Hellman key exchange by generating private and public DH values. By calling **DH\_compute\_key**(), these are combined with the other party's public value to compute the shared key.

**DH\_generate\_key**() expects *dh* to contain the shared parameters **dh-\>p** and **dh-\>g**. It generates a random private DH value unless **dh-\>priv\_key** is already set, and computes the corresponding public value **dh-\>pub\_key**, which can then be published.

**DH\_compute\_key**() computes the shared secret from the private DH value in *dh* and the other party's public value in *pub\_key* and stores it in *key*. *key* must point to **DH\_size**(*dh*) bytes of memory.

## RETURN VALUES

**DH\_generate\_key**() returns 1 on success, or 0 otherwise.

**DH\_compute\_key**() returns the size of the shared secret on success, or -1 on error.

The error codes can be obtained by ERR\_get\_error(3).

## SEE ALSO

DH\_get0\_key(3), DH\_new(3), DH\_size(3), ECDH\_compute\_key(3)

## HISTORY

**DH\_generate\_key**() and **DH\_compute\_key**() first appeared in SSLeay 0.5.1 and have been available since OpenBSD 2.4.
