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

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

<h1>DH_set_method(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="#THE_DH_METHOD_STRUCTURE">THE DH_METHOD STRUCTURE</A>
	<LI><A HREF="#RETURN_VALUES">RETURN VALUES</A>
	<LI><A HREF="#NOTES">NOTES</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_set_default_method, DH_get_default_method, DH_set_method, DH_new_method,
DH_OpenSSL - select DH method

</P>
<P>
<HR>
<H1><A NAME="SYNOPSIS">SYNOPSIS</A></H1>
<PRE> #include &lt;openssl/dh.h&gt;
 \#include &lt;openssl/engine.h&gt;
</PRE>
<PRE> void DH_set_default_method(const DH_METHOD *meth);
</PRE>
<PRE> const DH_METHOD *DH_get_default_method(void);
</PRE>
<PRE> int DH_set_method(DH *dh, const DH_METHOD *meth);
</PRE>
<PRE> DH *DH_new_method(ENGINE *engine);
</PRE>
<PRE> const DH_METHOD *DH_OpenSSL(void);
</PRE>
<P>
<HR>
<H1><A NAME="DESCRIPTION">DESCRIPTION</A></H1>
<P>
A <STRONG>DH_METHOD</STRONG> specifies the functions that OpenSSL uses for Diffie-Hellman operations. By
modifying the method, alternative implementations such as hardware
accelerators may be used. IMPORTANT: See the NOTES section for important
information about how these DH API functions are affected by the use of <STRONG>ENGINE</STRONG> API calls.

</P>
<P>
Initially, the default DH_METHOD is the OpenSSL internal implementation, as
returned by <CODE>DH_OpenSSL().</CODE>

</P>
<P>
<CODE>DH_set_default_method()</CODE> makes <STRONG>meth</STRONG> the default method for all DH structures created later. <STRONG>NB</STRONG>: This is true only whilst no ENGINE has been set as a default for DH, so
this function is no longer recommended.

</P>
<P>
<CODE>DH_get_default_method()</CODE> returns a pointer to the current
default DH_METHOD. However, the meaningfulness of this result is dependent
on whether the ENGINE API is being used, so this function is no longer
recommended.

</P>
<P>
<CODE>DH_set_method()</CODE> selects <STRONG>meth</STRONG> to perform all operations using the key <STRONG>dh</STRONG>. This will replace the DH_METHOD used by the DH key and if the previous
method was supplied by an ENGINE, the handle to that ENGINE will be
released during the change. It is possible to have DH keys that only work
with certain DH_METHOD implementations (eg. from an ENGINE module that
supports embedded hardware-protected keys), and in such cases attempting to
change the DH_METHOD for the key can have unexpected results.

</P>
<P>
<CODE>DH_new_method()</CODE> allocates and initializes a DH structure so
that <STRONG>engine</STRONG> will be used for the DH operations. If <STRONG>engine</STRONG> is NULL, the default ENGINE for DH operations is used, and if no default
ENGINE is set, the DH_METHOD controlled by
<CODE>DH_set_default_method()</CODE> is used.

</P>
<P>
<HR>
<H1><A NAME="THE_DH_METHOD_STRUCTURE">THE DH_METHOD STRUCTURE</A></H1>
<PRE> typedef struct dh_meth_st
 {
     /* name of the implementation */
        const char *name;
</PRE>
<PRE>     /* generate private and public DH values for key agreement */
        int (*generate_key)(DH *dh);
</PRE>
<PRE>     /* compute shared secret */
        int (*compute_key)(unsigned char *key, BIGNUM *pub_key, DH *dh);
</PRE>
<PRE>     /* compute r = a ^ p mod m (May be NULL for some implementations) */
        int (*bn_mod_exp)(DH *dh, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
                                const BIGNUM *m, BN_CTX *ctx,
                                BN_MONT_CTX *m_ctx);
</PRE>
<PRE>     /* called at DH_new */
        int (*init)(DH *dh);
</PRE>
<PRE>     /* called at DH_free */
        int (*finish)(DH *dh);
</PRE>
<PRE>        int flags;
</PRE>
<PRE>        char *app_data; /* ?? */
</PRE>
<PRE> } DH_METHOD;
</PRE>
<P>
<HR>
<H1><A NAME="RETURN_VALUES">RETURN VALUES</A></H1>
<P>
<CODE>DH_OpenSSL()</CODE> and <CODE>DH_get_default_method()</CODE> return
pointers to the respective
<STRONG>DH_METHOD</STRONG>s.

</P>
<P>
<CODE>DH_set_default_method()</CODE> returns no value.

</P>
<P>
<CODE>DH_set_method()</CODE> returns non-zero if the provided <STRONG>meth</STRONG> was successfully set as the method for <STRONG>dh</STRONG> (including unloading the ENGINE handle if the previous method was supplied
by an ENGINE).

</P>
<P>
<CODE>DH_new_method()</CODE> returns NULL and sets an error code that can
be obtained by
<A HREF="../crypto/ERR_get_error.html#">ERR_get_error(3)</A> if the allocation fails. Otherwise it returns a pointer to the newly
allocated structure.

</P>
<P>
<HR>
<H1><A NAME="NOTES">NOTES</A></H1>
<P>
As of version 0.9.7, DH_METHOD implementations are grouped together with
other algorithmic APIs (eg. RSA_METHOD, EVP_CIPHER, etc) in <STRONG>ENGINE</STRONG> modules. If a default ENGINE is specified for DH functionality using an
ENGINE API function, that will override any DH defaults set using the DH
API (ie. <CODE>DH_set_default_method()).</CODE> For this reason, the ENGINE
API is the recommended way to control default implementations for use in DH
and other cryptographic algorithms.

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



</P>
<P>
<HR>
<H1><A NAME="HISTORY">HISTORY</A></H1>
<P>
<CODE>DH_set_default_method(),</CODE> <CODE>DH_get_default_method(),</CODE>
<CODE>DH_set_method(),</CODE> <CODE>DH_new_method()</CODE> and
<CODE>DH_OpenSSL()</CODE> were added in OpenSSL 0.9.4.

</P>
<P>
<CODE>DH_set_default_openssl_method()</CODE> and
<CODE>DH_get_default_openssl_method()</CODE> replaced
<CODE>DH_set_default_method()</CODE> and
<CODE>DH_get_default_method()</CODE> respectively, and
<CODE>DH_set_method()</CODE> and <CODE>DH_new_method()</CODE> were altered
to use <STRONG>ENGINE</STRONG>s rather than
<STRONG>DH_METHOD</STRONG>s during development of the engine version of OpenSSL 0.9.6. For 0.9.7, the
handling of defaults in the ENGINE API was restructured so that this change
was reversed, and behaviour of the other functions resembled more closely
the previous behaviour. The behaviour of defaults in the ENGINE API now
transparently overrides the behaviour of defaults in the DH API without
requiring changing these function prototypes.

</P>
:}


