| 1 |
.\" -*- mode: troff; coding: utf-8 -*- |
| 2 |
.\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) |
| 3 |
.\" |
| 4 |
.\" Standard preamble: |
| 5 |
.\" ======================================================================== |
| 6 |
.de Sp \" Vertical space (when we can't use .PP) |
| 7 |
.if t .sp .5v |
| 8 |
.if n .sp |
| 9 |
.. |
| 10 |
.de Vb \" Begin verbatim text |
| 11 |
.ft CW |
| 12 |
.nf |
| 13 |
.ne \\$1 |
| 14 |
.. |
| 15 |
.de Ve \" End verbatim text |
| 16 |
.ft R |
| 17 |
.fi |
| 18 |
.. |
| 19 |
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. |
| 20 |
.ie n \{\ |
| 21 |
. ds C` "" |
| 22 |
. ds C' "" |
| 23 |
'br\} |
| 24 |
.el\{\ |
| 25 |
. ds C` |
| 26 |
. ds C' |
| 27 |
'br\} |
| 28 |
.\" |
| 29 |
.\" Escape single quotes in literal strings from groff's Unicode transform. |
| 30 |
.ie \n(.g .ds Aq \(aq |
| 31 |
.el .ds Aq ' |
| 32 |
.\" |
| 33 |
.\" If the F register is >0, we'll generate index entries on stderr for |
| 34 |
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index |
| 35 |
.\" entries marked with X<> in POD. Of course, you'll have to process the |
| 36 |
.\" output yourself in some meaningful fashion. |
| 37 |
.\" |
| 38 |
.\" Avoid warning from groff about undefined register 'F'. |
| 39 |
.de IX |
| 40 |
.. |
| 41 |
.nr rF 0 |
| 42 |
.if \n(.g .if rF .nr rF 1 |
| 43 |
.if (\n(rF:(\n(.g==0)) \{\ |
| 44 |
. if \nF \{\ |
| 45 |
. de IX |
| 46 |
. tm Index:\\$1\t\\n%\t"\\$2" |
| 47 |
.. |
| 48 |
. if !\nF==2 \{\ |
| 49 |
. nr % 0 |
| 50 |
. nr F 2 |
| 51 |
. \} |
| 52 |
. \} |
| 53 |
.\} |
| 54 |
.rr rF |
| 55 |
.\" ======================================================================== |
| 56 |
.\" |
| 57 |
.IX Title "EVP_PKEY_KEYGEN 3SSL" |
| 58 |
.TH EVP_PKEY_KEYGEN 3SSL 2026-04-07 3.6.2 OpenSSL |
| 59 |
.\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 60 |
.\" way too many mistakes in technical documents. |
| 61 |
.if n .ad l |
| 62 |
.nh |
| 63 |
.SH NAME |
| 64 |
EVP_PKEY_Q_keygen, |
| 65 |
EVP_PKEY_keygen_init, EVP_PKEY_paramgen_init, EVP_PKEY_generate, |
| 66 |
EVP_PKEY_CTX_set_cb, EVP_PKEY_CTX_get_cb, |
| 67 |
EVP_PKEY_CTX_get_keygen_info, EVP_PKEY_CTX_set_app_data, |
| 68 |
EVP_PKEY_CTX_get_app_data, |
| 69 |
EVP_PKEY_gen_cb, |
| 70 |
EVP_PKEY_paramgen, EVP_PKEY_keygen |
| 71 |
\&\- key and parameter generation and check functions |
| 72 |
.SH SYNOPSIS |
| 73 |
.IX Header "SYNOPSIS" |
| 74 |
.Vb 1 |
| 75 |
\& #include <openssl/evp.h> |
| 76 |
\& |
| 77 |
\& EVP_PKEY *EVP_PKEY_Q_keygen(OSSL_LIB_CTX *libctx, const char *propq, |
| 78 |
\& const char *type, ...); |
| 79 |
\& |
| 80 |
\& int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx); |
| 81 |
\& int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx); |
| 82 |
\& int EVP_PKEY_generate(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey); |
| 83 |
\& int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey); |
| 84 |
\& int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey); |
| 85 |
\& |
| 86 |
\& typedef int EVP_PKEY_gen_cb(EVP_PKEY_CTX *ctx); |
| 87 |
\& |
| 88 |
\& void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb); |
| 89 |
\& EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx); |
| 90 |
\& |
| 91 |
\& int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx); |
| 92 |
\& |
| 93 |
\& void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data); |
| 94 |
\& void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx); |
| 95 |
.Ve |
| 96 |
.SH DESCRIPTION |
| 97 |
.IX Header "DESCRIPTION" |
| 98 |
Generating keys is sometimes straight forward, just generate the key's |
| 99 |
numbers and be done with it. However, there are certain key types that need |
| 100 |
key parameters, often called domain parameters but not necessarily limited |
| 101 |
to that, that also need to be generated. In addition to this, the caller |
| 102 |
may want to set user provided generation parameters that further affect key |
| 103 |
parameter or key generation, such as the desired key size. |
| 104 |
.PP |
| 105 |
To flexibly allow all that's just been described, key parameter and key |
| 106 |
generation is divided into an initialization of a key algorithm context, |
| 107 |
functions to set user provided parameters, and finally the key parameter or |
| 108 |
key generation function itself. |
| 109 |
.PP |
| 110 |
The key algorithm context must be created using \fBEVP_PKEY_CTX_new\fR\|(3) or |
| 111 |
variants thereof, see that manual for details. |
| 112 |
.PP |
| 113 |
\&\fBEVP_PKEY_keygen_init()\fR initializes a public key algorithm context \fIctx\fR |
| 114 |
for a key generation operation. |
| 115 |
.PP |
| 116 |
\&\fBEVP_PKEY_paramgen_init()\fR is similar to \fBEVP_PKEY_keygen_init()\fR except key |
| 117 |
parameters are generated. |
| 118 |
.PP |
| 119 |
After initialization, generation parameters may be provided with |
| 120 |
\&\fBEVP_PKEY_CTX_ctrl\fR\|(3) or \fBEVP_PKEY_CTX_set_params\fR\|(3), or any other |
| 121 |
function described in those manuals. |
| 122 |
.PP |
| 123 |
\&\fBEVP_PKEY_generate()\fR performs the generation operation, the resulting key |
| 124 |
parameters or key are written to \fI*ppkey\fR. If \fI*ppkey\fR is NULL when this |
| 125 |
function is called, it will be allocated, and should be freed by the caller |
| 126 |
when no longer useful, using \fBEVP_PKEY_free\fR\|(3). |
| 127 |
.PP |
| 128 |
\&\fBEVP_PKEY_paramgen()\fR and \fBEVP_PKEY_keygen()\fR do exactly the same thing as |
| 129 |
\&\fBEVP_PKEY_generate()\fR, after checking that the corresponding \fBEVP_PKEY_paramgen_init()\fR |
| 130 |
or \fBEVP_PKEY_keygen_init()\fR was used to initialize \fIctx\fR. |
| 131 |
These are older functions that are kept for backward compatibility. |
| 132 |
It is safe to use \fBEVP_PKEY_generate()\fR instead. |
| 133 |
.PP |
| 134 |
The function \fBEVP_PKEY_set_cb()\fR sets the key or parameter generation callback |
| 135 |
to \fIcb\fR. The function \fBEVP_PKEY_CTX_get_cb()\fR returns the key or parameter |
| 136 |
generation callback. |
| 137 |
.PP |
| 138 |
The function \fBEVP_PKEY_CTX_get_keygen_info()\fR returns parameters associated |
| 139 |
with the generation operation. If \fIidx\fR is \-1 the total number of |
| 140 |
parameters available is returned. Any non negative value returns the value of |
| 141 |
that parameter. \fBEVP_PKEY_CTX_gen_keygen_info()\fR with a nonnegative value for |
| 142 |
\&\fIidx\fR should only be called within the generation callback. |
| 143 |
.PP |
| 144 |
If the callback returns 0 then the key generation operation is aborted and an |
| 145 |
error occurs. This might occur during a time consuming operation where |
| 146 |
a user clicks on a "cancel" button. |
| 147 |
.PP |
| 148 |
The functions \fBEVP_PKEY_CTX_set_app_data()\fR and \fBEVP_PKEY_CTX_get_app_data()\fR |
| 149 |
associate an opaque, application-defined pointer with an EVP_PKEY_CTX object. |
| 150 |
.PP |
| 151 |
This pointer is not interpreted by the library and is reserved entirely for use |
| 152 |
by the application. It may be used to store arbitrary context or state that |
| 153 |
needs to be accessible wherever the corresponding EVP_PKEY_CTX is available. |
| 154 |
.PP |
| 155 |
\&\fBEVP_PKEY_Q_keygen()\fR abstracts from the explicit use of \fBEVP_PKEY_CTX\fR while |
| 156 |
providing a 'quick' but limited way of generating a new asymmetric key pair. |
| 157 |
It provides shorthands for simple and common cases of key generation. |
| 158 |
As usual, the library context \fIlibctx\fR and property query \fIpropq\fR |
| 159 |
can be given for fetching algorithms from providers. |
| 160 |
If \fItype\fR is \f(CW\*(C`RSA\*(C'\fR, |
| 161 |
a \fBsize_t\fR parameter must be given to specify the size of the RSA key. |
| 162 |
If \fItype\fR is \f(CW\*(C`EC\*(C'\fR, |
| 163 |
a string parameter must be given to specify the name of the EC curve. |
| 164 |
If \fItype\fR is: |
| 165 |
\&\f(CW\*(C`ED25519\*(C'\fR, |
| 166 |
\&\f(CW\*(C`ED448\*(C'\fR, |
| 167 |
\&\f(CW\*(C`SM2\*(C'\fR, |
| 168 |
\&\f(CW\*(C`X25519\*(C'\fR, |
| 169 |
\&\f(CW\*(C`X448\*(C'\fR, |
| 170 |
\&\f(CW\*(C`ML\-DSA\-44\*(C'\fR, |
| 171 |
\&\f(CW\*(C`ML\-DSA\-65\*(C'\fR, |
| 172 |
\&\f(CW\*(C`ML\-DSA\-87\*(C'\fR, |
| 173 |
\&\f(CW\*(C`ML\-KEM\-512\*(C'\fR, |
| 174 |
\&\f(CW\*(C`ML\-KEM\-768\*(C'\fR, or |
| 175 |
\&\f(CW\*(C`ML\-KEM\-1024\*(C'\fR |
| 176 |
no further parameters are needed. Other key types may be possible if they are |
| 177 |
supplied by the loaded providers. \fBEVP_PKEY_Q_keygen()\fR may be usable with such |
| 178 |
key types as long as they do not require further parameters. |
| 179 |
.SH "RETURN VALUES" |
| 180 |
.IX Header "RETURN VALUES" |
| 181 |
\&\fBEVP_PKEY_keygen_init()\fR, \fBEVP_PKEY_paramgen_init()\fR, \fBEVP_PKEY_keygen()\fR and |
| 182 |
\&\fBEVP_PKEY_paramgen()\fR return 1 for success and 0 or a negative value for failure. |
| 183 |
In particular a return value of \-2 indicates the operation is not supported by |
| 184 |
the public key algorithm. |
| 185 |
.PP |
| 186 |
\&\fBEVP_PKEY_Q_keygen()\fR returns an \fBEVP_PKEY\fR, or NULL on failure. |
| 187 |
.SH NOTES |
| 188 |
.IX Header "NOTES" |
| 189 |
After the call to \fBEVP_PKEY_keygen_init()\fR or \fBEVP_PKEY_paramgen_init()\fR algorithm |
| 190 |
specific control operations can be performed to set any appropriate parameters |
| 191 |
for the operation. |
| 192 |
.PP |
| 193 |
The functions \fBEVP_PKEY_keygen()\fR and \fBEVP_PKEY_paramgen()\fR can be called more than |
| 194 |
once on the same context if several operations are performed using the same |
| 195 |
parameters. |
| 196 |
.PP |
| 197 |
The meaning of the parameters passed to the callback will depend on the |
| 198 |
algorithm and the specific implementation of the algorithm. Some might not |
| 199 |
give any useful information at all during key or parameter generation. Others |
| 200 |
might not even call the callback. |
| 201 |
.PP |
| 202 |
The operation performed by key or parameter generation depends on the algorithm |
| 203 |
used. In some cases (e.g. EC with a supplied named curve) the "generation" |
| 204 |
option merely sets the appropriate fields in an EVP_PKEY structure. |
| 205 |
.PP |
| 206 |
In OpenSSL an EVP_PKEY structure containing a private key also contains the |
| 207 |
public key components and parameters (if any). An OpenSSL private key is |
| 208 |
equivalent to what some libraries call a "key pair". A private key can be used |
| 209 |
in functions which require the use of a public key or parameters. |
| 210 |
.SH EXAMPLES |
| 211 |
.IX Header "EXAMPLES" |
| 212 |
Generate a 2048 bit RSA key: |
| 213 |
.PP |
| 214 |
.Vb 2 |
| 215 |
\& #include <openssl/evp.h> |
| 216 |
\& #include <openssl/rsa.h> |
| 217 |
\& |
| 218 |
\& EVP_PKEY_CTX *ctx; |
| 219 |
\& EVP_PKEY *pkey = NULL; |
| 220 |
\& |
| 221 |
\& ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL); |
| 222 |
\& if (!ctx) |
| 223 |
\& /* Error occurred */ |
| 224 |
\& if (EVP_PKEY_keygen_init(ctx) <= 0) |
| 225 |
\& /* Error */ |
| 226 |
\& if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048) <= 0) |
| 227 |
\& /* Error */ |
| 228 |
\& |
| 229 |
\& /* Generate key */ |
| 230 |
\& if (EVP_PKEY_keygen(ctx, &pkey) <= 0) |
| 231 |
\& /* Error */ |
| 232 |
.Ve |
| 233 |
.PP |
| 234 |
Generate a key from a set of parameters: |
| 235 |
.PP |
| 236 |
.Vb 2 |
| 237 |
\& #include <openssl/evp.h> |
| 238 |
\& #include <openssl/rsa.h> |
| 239 |
\& |
| 240 |
\& EVP_PKEY_CTX *ctx; |
| 241 |
\& ENGINE *eng; |
| 242 |
\& EVP_PKEY *pkey = NULL, *param; |
| 243 |
\& |
| 244 |
\& /* Assumed param, eng are set up already */ |
| 245 |
\& ctx = EVP_PKEY_CTX_new(param, eng); |
| 246 |
\& if (!ctx) |
| 247 |
\& /* Error occurred */ |
| 248 |
\& if (EVP_PKEY_keygen_init(ctx) <= 0) |
| 249 |
\& /* Error */ |
| 250 |
\& |
| 251 |
\& /* Generate key */ |
| 252 |
\& if (EVP_PKEY_keygen(ctx, &pkey) <= 0) |
| 253 |
\& /* Error */ |
| 254 |
.Ve |
| 255 |
.PP |
| 256 |
Example of generation callback for OpenSSL public key implementations: |
| 257 |
.PP |
| 258 |
.Vb 1 |
| 259 |
\& /* Application data is a BIO to output status to */ |
| 260 |
\& |
| 261 |
\& EVP_PKEY_CTX_set_app_data(ctx, status_bio); |
| 262 |
\& |
| 263 |
\& static int genpkey_cb(EVP_PKEY_CTX *ctx) |
| 264 |
\& { |
| 265 |
\& char c = \*(Aq*\*(Aq; |
| 266 |
\& BIO *b = EVP_PKEY_CTX_get_app_data(ctx); |
| 267 |
\& int p = EVP_PKEY_CTX_get_keygen_info(ctx, 0); |
| 268 |
\& |
| 269 |
\& if (p == 0) |
| 270 |
\& c = \*(Aq.\*(Aq; |
| 271 |
\& if (p == 1) |
| 272 |
\& c = \*(Aq+\*(Aq; |
| 273 |
\& if (p == 2) |
| 274 |
\& c = \*(Aq*\*(Aq; |
| 275 |
\& if (p == 3) |
| 276 |
\& c = \*(Aq\en\*(Aq; |
| 277 |
\& BIO_write(b, &c, 1); |
| 278 |
\& (void)BIO_flush(b); |
| 279 |
\& return 1; |
| 280 |
\& } |
| 281 |
.Ve |
| 282 |
.SH "SEE ALSO" |
| 283 |
.IX Header "SEE ALSO" |
| 284 |
\&\fBEVP_RSA_gen\fR\|(3), \fBEVP_EC_gen\fR\|(3), |
| 285 |
\&\fBEVP_PKEY_CTX_new\fR\|(3), |
| 286 |
\&\fBEVP_PKEY_encrypt\fR\|(3), |
| 287 |
\&\fBEVP_PKEY_decrypt\fR\|(3), |
| 288 |
\&\fBEVP_PKEY_sign\fR\|(3), |
| 289 |
\&\fBEVP_PKEY_verify\fR\|(3), |
| 290 |
\&\fBEVP_PKEY_verify_recover\fR\|(3), |
| 291 |
\&\fBEVP_PKEY_derive\fR\|(3) |
| 292 |
.SH HISTORY |
| 293 |
.IX Header "HISTORY" |
| 294 |
\&\fBEVP_PKEY_keygen_init()\fR, int \fBEVP_PKEY_paramgen_init()\fR, \fBEVP_PKEY_keygen()\fR, |
| 295 |
\&\fBEVP_PKEY_paramgen()\fR, \fBEVP_PKEY_gen_cb()\fR, \fBEVP_PKEY_CTX_set_cb()\fR, |
| 296 |
\&\fBEVP_PKEY_CTX_get_cb()\fR, \fBEVP_PKEY_CTX_get_keygen_info()\fR, |
| 297 |
\&\fBEVP_PKEY_CTX_set_app_data()\fR and \fBEVP_PKEY_CTX_get_app_data()\fR were added in |
| 298 |
OpenSSL 1.0.0. |
| 299 |
.PP |
| 300 |
\&\fBEVP_PKEY_Q_keygen()\fR and \fBEVP_PKEY_generate()\fR were added in OpenSSL 3.0. |
| 301 |
.SH COPYRIGHT |
| 302 |
.IX Header "COPYRIGHT" |
| 303 |
Copyright 2006\-2026 The OpenSSL Project Authors. All Rights Reserved. |
| 304 |
.PP |
| 305 |
Licensed under the Apache License 2.0 (the "License"). You may not use |
| 306 |
this file except in compliance with the License. You can obtain a copy |
| 307 |
in the file LICENSE in the source distribution or at |
| 308 |
<https://www.openssl.org/source/license.html>. |