DH_GENERATE_KEY(3ossl)

πŸ“¦ version 3.5.0, in opensuse-leap 16.0

πŸ’… DH_GENERATE_KEY β€” the 3ossl girlies' guide to Diffie-Hellman key exchange ✨

okay so bestie, real talk: this whole page is about perform Diffie-Hellman key exchange β€” yes that's the actual NAME section and yes it's a lot, but we're gonna get through it together 🫢


✨ SYNOPSIS ✨

#include <openssl/dh.h>

so first things first β€” huge heads up, no cap: the following functions have been deprecated since OpenSSL 3.0, and they can be hidden entirely by defining OPENSSL_API_COMPAT with a suitable version value, see openssl_user_macros(7) πŸ“œ

int DH_generate_key(DH *dh);

int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh);

int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh);

those three are the whole cast, iykyk πŸ’…


πŸ’– DESCRIPTION πŸ’–

okay so the vibe check first: all of the functions described on this page are deprecated. like, all of them. 😭 Applications should instead use EVP_PKEY_derive_init(3) and EVP_PKEY_derive(3). that's the glow-up, that's the main character energy, periodt.

so what did these icons even do? πŸŽ€ DH_generate_key() performs the first step of a Diffie-Hellman key exchange by generating private and public DH values. then, by calling DH_compute_key() or DH_compute_key_padded(), these are combined with the other party's public value to compute the shared key. it's giving two besties each holding half a friendship bracelet, fr fr πŸ¦‹

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. slay πŸ’…

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. no more, no less, don't be messy.

the padding style here is RFC 5246 (8.1.2) that strips leading zero bytes. and because of that leading-zero-stripping, it is not constant time πŸ’€ The return value should be considered public. like, public public. don't be out here treating it like a secret, bestie πŸ™ˆ

DH_compute_key_padded() is similar but stores a fixed number of bytes. the padding style is NIST SP 800-56A (C.1) that retains leading zero bytes. It is constant time due to the leading zero bytes being retained β€” iconic behavior honestly πŸ‘‘ The return value should be considered public (yes, again, still public!).


🌸 RETURN VALUES 🌸

  • DH_generate_key() returns 1 on success, 0 otherwise. ✨
  • DH_compute_key() returns the size of the shared secret on success, -1 on error. 😭
  • DH_compute_key_padded() returns DH_size(dh) on success, -1 on error. πŸ’€

The error codes can be obtained by ERR_get_error(3). so if something's giving nothing, that's where you look πŸ”


πŸ¦‹ SEE ALSO πŸ¦‹

EVP_PKEY_derive(3), DH_new(3), ERR_get_error(3), RAND_bytes(3), DH_size(3) β€” go read these too, they're all part of the lore πŸ’œ


πŸŽ€ HISTORY πŸŽ€

  • DH_compute_key_padded() was added in OpenSSL 1.0.2. a late arrival but we appreciate her 🌈
  • All of these functions were deprecated in OpenSSL 3.0. rip to the girlies but they had a good run ⭐

Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.

Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>. 🫢

πŸ’€ real talk though: deprecated means don't start new stuff with these. use EVP_PKEY_derive_init(3) and EVP_PKEY_derive(3), bestie.

and that's the page!! πŸ“ this lil' rewrite is from 3.5.0, dated 2026-09-25, and it's the DH_GENERATE_KEY(3ossl) manual β€” k byeeee πŸ’–βœ¨πŸ§š

πŸ“¦ where this page comes from