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

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

<h1>EVP_PKEY_keygen(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="#NOTES">NOTES</A>
	<LI><A HREF="#RETURN_VALUES">RETURN VALUES</A>
	<LI><A HREF="#EXAMPLES">EXAMPLES</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_PKEY_keygen_init, EVP_PKEY_keygen, EVP_PKEY_paramgen_init,
EVP_PKEY_paramgen, EVP_PKEY_CTX_set_cb, EVP_PKEY_CTX_get_cb,
EVP_PKEY_CTX_get_keygen_info, EVP_PKEVP_PKEY_CTX_set_app_data,
EVP_PKEY_CTX_get_app_data - key and parameter generation functions

</P>
<P>
<HR>
<H1><A NAME="SYNOPSIS">SYNOPSIS</A></H1>
<PRE> #include &lt;openssl/evp.h&gt;
</PRE>
<PRE> int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx);
 int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
 int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx);
 int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
</PRE>
<PRE> typedef int EVP_PKEY_gen_cb(EVP_PKEY_CTX *ctx);
</PRE>
<PRE> void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb);
 EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx);
</PRE>
<PRE> int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx);
</PRE>
<PRE> void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data);
 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx);
</PRE>
<P>
<HR>
<H1><A NAME="DESCRIPTION">DESCRIPTION</A></H1>
<P>
The <CODE>EVP_PKEY_keygen_init()</CODE> function initializes a public key
algorithm context using key <STRONG>pkey</STRONG> for a key genration operation.

</P>
<P>
The <CODE>EVP_PKEY_keygen()</CODE> function performs a key generation
operation, the generated key is written to <STRONG>ppkey</STRONG>.

</P>
<P>
The functions <CODE>EVP_PKEY_paramgen_init()</CODE> and
<CODE>EVP_PKEY_paramgen()</CODE> are similar except parameters are
generated.

</P>
<P>
The function <CODE>EVP_PKEY_set_cb()</CODE> sets the key or parameter
generation callback to <STRONG>cb</STRONG>. The function <CODE>EVP_PKEY_CTX_get_cb()</CODE> returns the key or
parameter generation callback.

</P>
<P>
The function <CODE>EVP_PKEY_CTX_get_keygen_info()</CODE> returns parameters
associated with the generation operation. If <STRONG>idx</STRONG> is -1 the total number of parameters available is returned. Any non
negative value returns the value of that parameter.
<CODE>EVP_PKEY_CTX_gen_keygen_info()</CODE> with a non-negative value for
<STRONG>idx</STRONG> should only be called within the generation callback.

</P>
<P>
If the callback returns 0 then the key genration operation is aborted and
an error occurs. This might occur during a time consuming operation where a
user clicks on a ``cancel'' button.

</P>
<P>
The functions <CODE>EVP_PKEY_CTX_set_app_data()</CODE> and
<CODE>EVP_PKEY_CTX_get_app_data()</CODE> set and retrieve an opaque
pointer. This can be used to set some application defined value which can
be retrieved in the callback: for example a handle which is used to update
a ``progress dialog''.

</P>
<P>
<HR>
<H1><A NAME="NOTES">NOTES</A></H1>
<P>
After the call to <CODE>EVP_PKEY_keygen_init()</CODE> or
<CODE>EVP_PKEY_paramgen_init()</CODE> algorithm specific control operations
can be performed to set any appropriate parameters for the operation.

</P>
<P>
The functions <CODE>EVP_PKEY_keygen()</CODE> and
<CODE>EVP_PKEY_paramgen()</CODE> can be called more than once on the same
context if several operations are performed using the same parameters.

</P>
<P>
The meaning of the parameters passed to the callback will depend on the
algorithm and the specifiic implementation of the algorithm. Some might not
give any useful information at all during key or parameter generation.
Others might not even call the callback.

</P>
<P>
The operation performed by key or parameter generation depends on the
algorithm used. In some cases (e.g. EC with a supplied named curve) the
``generation'' option merely sets the appropriate fields in an EVP_PKEY
structure.

</P>
<P>
In OpenSSL an EVP_PKEY structure containing a private key also contains the
public key components and parameters (if any). An OpenSSL private key is
equivalent to what some libraries call a ``key pair''. A private key can be
used in functions which require the use of a public key or parameters.

</P>
<P>
<HR>
<H1><A NAME="RETURN_VALUES">RETURN VALUES</A></H1>
<P>
<CODE>EVP_PKEY_keygen_init(),</CODE> <CODE>EVP_PKEY_paramgen_init(),</CODE>
<CODE>EVP_PKEY_keygen()</CODE> and <CODE>EVP_PKEY_paramgen()</CODE> return
1 for success and 0 or a negative value for failure. In particular a return
value of -2 indicates the operation is not supported by the public key
algorithm.

</P>
<P>
<HR>
<H1><A NAME="EXAMPLES">EXAMPLES</A></H1>
<P>
Generate a 2048 bit RSA key:

</P>
<PRE> #include &lt;openssl/evp.h&gt;
 \#include &lt;openssl/rsa.h&gt;
</PRE>
<PRE> EVP_PKEY_CTX *ctx;
 EVP_PKEY *pkey = NULL;
 ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
 if (!ctx)
        /* Error occurred */
 if (EVP_PKEY_keygen_init(ctx) &lt;= 0)
        /* Error */
 if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048) &lt;= 0)
        /* Error */
</PRE>
<PRE> /* Generate key */
 if (EVP_PKEY_keygen(ctx, &amp;pkey) &lt;= 0)
        /* Error */
</PRE>
<P>
Generate a key from a set of parameters:

</P>
<PRE> #include &lt;openssl/evp.h&gt;
 \#include &lt;openssl/rsa.h&gt;
</PRE>
<PRE> EVP_PKEY_CTX *ctx;
 EVP_PKEY *pkey = NULL, *param;
 /* Assumed param is set up already */
 ctx = EVP_PKEY_CTX_new(param);
 if (!ctx)
        /* Error occurred */
 if (EVP_PKEY_keygen_init(ctx) &lt;= 0)
        /* Error */
</PRE>
<PRE> /* Generate key */
 if (EVP_PKEY_keygen(ctx, &amp;pkey) &lt;= 0)
        /* Error */
</PRE>
<P>
Example of generation callback for OpenSSL public key implementations:

</P>
<PRE> /* Application data is a BIO to output status to */
</PRE>
<PRE> EVP_PKEY_CTX_set_app_data(ctx, status_bio);
</PRE>
<PRE> static int genpkey_cb(EVP_PKEY_CTX *ctx)
        {
        char c='*';
        BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
        int p;
        p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
        if (p == 0) c='.';
        if (p == 1) c='+';
        if (p == 2) c='*';
        if (p == 3) c='\n';
        BIO_write(b,&amp;c,1);
        (void)BIO_flush(b);
        return 1;
        }
</PRE>
<P>
<HR>
<H1><A NAME="SEE_ALSO">SEE ALSO</A></H1>
<P>
<A HREF="../crypto/EVP_PKEY_CTX_new.html#">EVP_PKEY_CTX_new(3)</A>,
<A HREF="../crypto/EVP_PKEY_encrypt.html#">EVP_PKEY_encrypt(3)</A>,
<A HREF="../crypto/EVP_PKEY_decrypt.html#">EVP_PKEY_decrypt(3)</A>,
<A HREF="../crypto/EVP_PKEY_sign.html#">EVP_PKEY_sign(3)</A>,
<A HREF="../crypto/EVP_PKEY_verify.html#">EVP_PKEY_verify(3)</A>,
<A HREF="../crypto/EVP_PKEY_verifyrecover.html#">EVP_PKEY_verifyrecover(3)</A>,
<A HREF="../crypto/EVP_PKEY_derive.html#">EVP_PKEY_derive(3)</A> 

 

</P>
<P>
<HR>
<H1><A NAME="HISTORY">HISTORY</A></H1>
<P>
These functions were first added to OpenSSL 1.0.0.

</P>
:}


