
#use wml::openssl-macros area=docs page=DH_generate_key

<title>Documents, DH_generate_key(3)</title>

<h1>DH_generate_key(3)</h1>

#use wml::imp::generic

{:
## What's this? [[s|(<STRONG>[^<].+?)</A>(</STRONG><DD>)|$1$2|sg]]
[[s|<P>\s+<P>|<P>|sg]]
[[s|<P>\s+</|</|sg]]
[[s|<DD>\s*<DT>|<DD>&nbsp;<DT>|sg]]
[[s|<DD>\s*</DL>|<DD>&nbsp;</DL>|sg]]
[[s|\[|&#91;|sg]]
[[s|\]|&#93;|sg]]

<!-- INDEX BEGIN -->

<UL>

	<LI><A HREF="#NAME">NAME</A>
	<LI><A HREF="#SYNOPSIS">SYNOPSIS</A>
	<LI><A HREF="#DESCRIPTION">DESCRIPTION</A>
	<LI><A HREF="#RETURN_VALUES">RETURN VALUES</A>
	<LI><A HREF="#SEE_ALSO">SEE ALSO</A>
	<LI><A HREF="#HISTORY">HISTORY</A>
</UL>
<!-- INDEX END -->

<HR>
<P>
<HR>
<H1><A NAME="NAME">NAME</A></H1>
<P>
DH_generate_key, DH_compute_key - perform Diffie-Hellman key exchange

</P>
<P>
<HR>
<H1><A NAME="SYNOPSIS">SYNOPSIS</A></H1>
<PRE> #include &lt;openssl/dh.h&gt;
</PRE>
<PRE> int DH_generate_key(DH *dh);
</PRE>
<PRE> int DH_compute_key(unsigned char *key, BIGNUM *pub_key, DH *dh);
</PRE>
<P>
<HR>
<H1><A NAME="DESCRIPTION">DESCRIPTION</A></H1>
<P>
<CODE>DH_generate_key()</CODE> performs the first step of a Diffie-Hellman
key exchange by generating private and public DH values. By calling
<CODE>DH_compute_key(),</CODE> these are combined with the other party's
public value to compute the shared key.

</P>
<P>
<CODE>DH_generate_key()</CODE> expects <STRONG>dh</STRONG> to contain the shared parameters
<STRONG>dh-&gt;p</STRONG> and <STRONG>dh-&gt;g</STRONG>. It generates a random private DH value unless <STRONG>dh-&gt;priv_key</STRONG> is already set, and computes the corresponding public value <STRONG>dh-&gt;pub_key</STRONG>, which can then be published.

</P>
<P>
<CODE>DH_compute_key()</CODE> computes the shared secret from the private
DH value in <STRONG>dh</STRONG> and the other party's public value in <STRONG>pub_key</STRONG> and stores it in <STRONG>key</STRONG>. <STRONG>key</STRONG> must point to <STRONG>DH_size(dh)</STRONG> bytes of memory.

</P>
<P>
<HR>
<H1><A NAME="RETURN_VALUES">RETURN VALUES</A></H1>
<P>
<CODE>DH_generate_key()</CODE> returns 1 on success, 0 otherwise.

</P>
<P>
<CODE>DH_compute_key()</CODE> returns the size of the shared secret on
success, -1 on error.

</P>
<P>
The error codes can be obtained by <A HREF="../crypto/ERR_get_error.html#">ERR_get_error(3)</A>.

</P>
<P>
<HR>
<H1><A NAME="SEE_ALSO">SEE ALSO</A></H1>
<P>
<A HREF="../crypto/dh.html#">dh(3)</A>, <A HREF="../crypto/ERR_get_error.html#">ERR_get_error(3)</A>, <A HREF="../crypto/rand.html#">rand(3)</A>, <A HREF="../crypto/DH_size.html#">DH_size(3)</A>



</P>
<P>
<HR>
<H1><A NAME="HISTORY">HISTORY</A></H1>
<P>
<CODE>DH_generate_key()</CODE> and <CODE>DH_compute_key()</CODE> are
available in all versions of SSLeay and OpenSSL.

</P>
:}


