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

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

<h1>EVP_SignInit(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="#NOTES">NOTES</A>
	<LI><A HREF="#BUGS">BUGS</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>
EVP_SignInit, EVP_SignUpdate, EVP_SignFinal - EVP signing functions

</P>
<P>
<HR>
<H1><A NAME="SYNOPSIS">SYNOPSIS</A></H1>
<PRE> #include &lt;openssl/evp.h&gt;
</PRE>
<PRE> int EVP_SignInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
 int EVP_SignUpdate(EVP_MD_CTX *ctx, const void *d, unsigned int cnt);
 int EVP_SignFinal(EVP_MD_CTX *ctx,unsigned char *sig,unsigned int *s, EVP_PKEY *pkey);
</PRE>
<PRE> void EVP_SignInit(EVP_MD_CTX *ctx, const EVP_MD *type);
</PRE>
<PRE> int EVP_PKEY_size(EVP_PKEY *pkey);
</PRE>
<P>
<HR>
<H1><A NAME="DESCRIPTION">DESCRIPTION</A></H1>
<P>
The EVP signature routines are a high level interface to digital
signatures.

</P>
<P>
<CODE>EVP_SignInit_ex()</CODE> sets up signing context <STRONG>ctx</STRONG> to use digest
<STRONG>type</STRONG> from ENGINE <STRONG>impl</STRONG>. <STRONG>ctx</STRONG> must be initialized with <CODE>EVP_MD_CTX_init()</CODE> before calling this
function.

</P>
<P>
<CODE>EVP_SignUpdate()</CODE> hashes <STRONG>cnt</STRONG> bytes of data at <STRONG>d</STRONG> into the signature context <STRONG>ctx</STRONG>. This function can be called several times on the same <STRONG>ctx</STRONG> to include additional data.

</P>
<P>
<CODE>EVP_SignFinal()</CODE> signs the data in <STRONG>ctx</STRONG> using the private key <STRONG>pkey</STRONG> and places the signature in <STRONG>sig</STRONG>. The number of bytes of data written (i.e. the length of the signature)
will be written to the integer at <STRONG>s</STRONG>, at most <CODE>EVP_PKEY_size(pkey)</CODE> bytes will be written. 

</P>
<P>
<CODE>EVP_SignInit()</CODE> initializes a signing context <STRONG>ctx</STRONG> to use the default implementation of digest <STRONG>type</STRONG>.

</P>
<P>
<CODE>EVP_PKEY_size()</CODE> returns the maximum size of a signature in
bytes. The actual signature returned by <CODE>EVP_SignFinal()</CODE> may be
smaller.

</P>
<P>
<HR>
<H1><A NAME="RETURN_VALUES">RETURN VALUES</A></H1>
<P>
<CODE>EVP_SignInit_ex(),</CODE> <CODE>EVP_SignUpdate()</CODE> and
<CODE>EVP_SignFinal()</CODE> return 1 for success and 0 for failure.

</P>
<P>
<CODE>EVP_PKEY_size()</CODE> returns the maximum size of a signature in
bytes.

</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="NOTES">NOTES</A></H1>
<P>
The <STRONG>EVP</STRONG> interface to digital signatures should almost always be used in preference
to the low level interfaces. This is because the code then becomes
transparent to the algorithm used and much more flexible.

</P>
<P>
Due to the link between message digests and public key algorithms the
correct digest algorithm must be used with the correct public key type. A
list of algorithms and associated public key algorithms appears in 
<A HREF="../crypto/EVP_DigestInit.html#">EVP_DigestInit(3)</A>.

</P>
<P>
When signing with DSA private keys the random number generator must be
seeded or the operation will fail. The random number generator does not
need to be seeded for RSA signatures.

</P>
<P>
The call to <CODE>EVP_SignFinal()</CODE> internally finalizes a copy of the
digest context. This means that calls to <CODE>EVP_SignUpdate()</CODE> and
<CODE>EVP_SignFinal()</CODE> can be called later to digest and sign
additional data.

</P>
<P>
Since only a copy of the digest context is ever finalized the context must
be cleaned up after use by calling <CODE>EVP_MD_CTX_cleanup()</CODE> or a
memory leak will occur.

</P>
<P>
<HR>
<H1><A NAME="BUGS">BUGS</A></H1>
<P>
Older versions of this documentation wrongly stated that calls to
<CODE>EVP_SignUpdate()</CODE> could not be made after calling
<CODE>EVP_SignFinal().</CODE>

</P>
<P>
Since the private key is passed in the call to <CODE>EVP_SignFinal()</CODE>
any error relating to the private key (for example an unsuitable key and
digest combination) will not be indicated until after potentially large
amounts of data have been passed through <CODE>EVP_SignUpdate().</CODE>

</P>
<P>
It is not possible to change the signing parameters using these function.

</P>
<P>
The previous two bugs are fixed in the newer EVP_SignDigest*() function.

</P>
<P>
<HR>
<H1><A NAME="SEE_ALSO">SEE ALSO</A></H1>
<P>
<A HREF="../crypto/EVP_VerifyInit.html#">EVP_VerifyInit(3)</A>,
<A HREF="../crypto/EVP_DigestInit.html#">EVP_DigestInit(3)</A>, <A HREF="../crypto/err.html#">err(3)</A>,
<A HREF="../crypto/evp.html#">evp(3)</A>, <A HREF="../crypto/hmac.html#">hmac(3)</A>, <A HREF="../crypto/md5.html#">md2(3)</A>,
<A HREF="../crypto/md5.html#">md5(3)</A>, <A HREF="../crypto/mdc2.html#">mdc2(3)</A>, <A HREF="../crypto/ripemd.html#">ripemd(3)</A>,
<A HREF="../crypto/sha.html#">sha(3)</A>, <A HREF="../apps/dgst.html#">dgst(1)</A>



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

</P>
<P>
<CODE>EVP_SignInit_ex()</CODE> was added in OpenSSL 0.9.7.

</P>
:}

