.\" $OpenBSD: DH_generate_key.3,v 1.2 2016/11/06 15:52:50 jmc Exp $ .\" .Dd $Mdocdate: November 6 2016 $ .Dt DH_GENERATE_KEY 3 .Os .Sh NAME .Nm DH_generate_key , .Nm DH_compute_key .Nd perform Diffie-Hellman key exchange .Sh SYNOPSIS .In openssl/dh.h .Ft int .Fo DH_generate_key .Fa "DH *dh" .Fc .Ft int .Fo DH_compute_key .Fa "unsigned char *key" .Fa "BIGNUM *pub_key" .Fa "DH *dh" .Fc .Sh DESCRIPTION .Fn DH_generate_key performs the first step of a Diffie-Hellman key exchange by generating private and public DH values. By calling .Fn DH_compute_key , these are combined with the other party's public value to compute the shared key. .Pp .Fn DH_generate_key expects .Fa dh to contain the shared parameters .Sy dh->p and .Sy dh->g . It generates a random private DH value unless .Sy dh->priv_key is already set, and computes the corresponding public value .Sy dh->pub_key , which can then be published. .Pp .Fn DH_compute_key computes the shared secret from the private DH value in .Fa dh and the other party's public value in .Fa pub_key and stores it in .Fa key . .Fa key must point to .Fn DH_size dh bytes of memory. .Sh RETURN VALUES .Fn DH_generate_key returns 1 on success, 0 otherwise. .Pp .Fn DH_compute_key returns the size of the shared secret on success, -1 on error. .Pp The error codes can be obtained by .Xr ERR_get_error 3 . .Sh SEE ALSO .Xr dh 3 , .Xr DH_size 3 , .Xr ERR_get_error 3 , .Xr rand 3 .Sh HISTORY .Fn DH_generate_key and .Fn DH_compute_key are available in all versions of SSLeay and OpenSSL.