1EVP_PKEY_KEYGEN(3ossl) OpenSSL EVP_PKEY_KEYGEN(3ossl)
2
3
4
6 EVP_PKEY_Q_keygen, EVP_PKEY_keygen_init, EVP_PKEY_paramgen_init,
7 EVP_PKEY_generate, EVP_PKEY_CTX_set_cb, EVP_PKEY_CTX_get_cb,
8 EVP_PKEY_CTX_get_keygen_info, EVP_PKEY_CTX_set_app_data,
9 EVP_PKEY_CTX_get_app_data, EVP_PKEY_gen_cb, EVP_PKEY_paramgen,
10 EVP_PKEY_keygen - key and parameter generation and check functions
11
13 #include <openssl/evp.h>
14
15 EVP_PKEY *EVP_PKEY_Q_keygen(OSSL_LIB_CTX *libctx, const char *propq,
16 const char *type, ...);
17
18 int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx);
19 int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx);
20 int EVP_PKEY_generate(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
21 int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
22 int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
23
24 typedef int EVP_PKEY_gen_cb(EVP_PKEY_CTX *ctx);
25
26 void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb);
27 EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx);
28
29 int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx);
30
31 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data);
32 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx);
33
35 Generating keys is sometimes straight forward, just generate the key's
36 numbers and be done with it. However, there are certain key types that
37 need key parameters, often called domain parameters but not necessarily
38 limited to that, that also need to be generated. In addition to this,
39 the caller may want to set user provided generation parameters that
40 further affect key parameter or key generation, such as the desired key
41 size.
42
43 To flexibly allow all that's just been described, key parameter and key
44 generation is divided into an initialization of a key algorithm
45 context, functions to set user provided parameters, and finally the key
46 parameter or key generation function itself.
47
48 The key algorithm context must be created using EVP_PKEY_CTX_new(3) or
49 variants thereof, see that manual for details.
50
51 EVP_PKEY_keygen_init() initializes a public key algorithm context ctx
52 for a key generation operation.
53
54 EVP_PKEY_paramgen_init() is similar to EVP_PKEY_keygen_init() except
55 key parameters are generated.
56
57 After initialization, generation parameters may be provided with
58 EVP_PKEY_CTX_ctrl(3) or EVP_PKEY_CTX_set_params(3), or any other
59 function described in those manuals.
60
61 EVP_PKEY_generate() performs the generation operation, the resulting
62 key parameters or key are written to *ppkey. If *ppkey is NULL when
63 this function is called, it will be allocated, and should be freed by
64 the caller when no longer useful, using EVP_PKEY_free(3).
65
66 EVP_PKEY_paramgen() and EVP_PKEY_keygen() do exactly the same thing as
67 EVP_PKEY_generate(), after checking that the corresponding
68 EVP_PKEY_paramgen_init() or EVP_PKEY_keygen_init() was used to
69 initialize ctx. These are older functions that are kept for backward
70 compatibility. It is safe to use EVP_PKEY_generate() instead.
71
72 The function EVP_PKEY_set_cb() sets the key or parameter generation
73 callback to cb. The function EVP_PKEY_CTX_get_cb() returns the key or
74 parameter generation callback.
75
76 The function EVP_PKEY_CTX_get_keygen_info() returns parameters
77 associated with the generation operation. If idx is -1 the total number
78 of parameters available is returned. Any non negative value returns the
79 value of that parameter. EVP_PKEY_CTX_gen_keygen_info() with a
80 nonnegative value for idx should only be called within the generation
81 callback.
82
83 If the callback returns 0 then the key generation operation is aborted
84 and an error occurs. This might occur during a time consuming operation
85 where a user clicks on a "cancel" button.
86
87 The functions EVP_PKEY_CTX_set_app_data() and
88 EVP_PKEY_CTX_get_app_data() set and retrieve an opaque pointer. This
89 can be used to set some application defined value which can be
90 retrieved in the callback: for example a handle which is used to update
91 a "progress dialog".
92
93 EVP_PKEY_Q_keygen() abstracts from the explicit use of EVP_PKEY_CTX
94 while providing a 'quick' but limited way of generating a new
95 asymmetric key pair. It provides shorthands for simple and common
96 cases of key generation. As usual, the library context libctx and
97 property query propq can be given for fetching algorithms from
98 providers. If type is "RSA", a size_t parameter must be given to
99 specify the size of the RSA key. If type is "EC", a string parameter
100 must be given to specify the name of the EC curve. If type is
101 "X25519", "X448", "ED25519", "ED448", or "SM2" no further parameter is
102 needed.
103
105 EVP_PKEY_keygen_init(), EVP_PKEY_paramgen_init(), EVP_PKEY_keygen() and
106 EVP_PKEY_paramgen() return 1 for success and 0 or a negative value for
107 failure. In particular a return value of -2 indicates the operation is
108 not supported by the public key algorithm.
109
110 EVP_PKEY_Q_keygen() returns an EVP_PKEY, or NULL on failure.
111
113 After the call to EVP_PKEY_keygen_init() or EVP_PKEY_paramgen_init()
114 algorithm specific control operations can be performed to set any
115 appropriate parameters for the operation.
116
117 The functions EVP_PKEY_keygen() and EVP_PKEY_paramgen() can be called
118 more than once on the same context if several operations are performed
119 using the same parameters.
120
121 The meaning of the parameters passed to the callback will depend on the
122 algorithm and the specific implementation of the algorithm. Some might
123 not give any useful information at all during key or parameter
124 generation. Others might not even call the callback.
125
126 The operation performed by key or parameter generation depends on the
127 algorithm used. In some cases (e.g. EC with a supplied named curve) the
128 "generation" option merely sets the appropriate fields in an EVP_PKEY
129 structure.
130
131 In OpenSSL an EVP_PKEY structure containing a private key also contains
132 the public key components and parameters (if any). An OpenSSL private
133 key is equivalent to what some libraries call a "key pair". A private
134 key can be used in functions which require the use of a public key or
135 parameters.
136
138 Generate a 2048 bit RSA key:
139
140 #include <openssl/evp.h>
141 #include <openssl/rsa.h>
142
143 EVP_PKEY_CTX *ctx;
144 EVP_PKEY *pkey = NULL;
145
146 ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
147 if (!ctx)
148 /* Error occurred */
149 if (EVP_PKEY_keygen_init(ctx) <= 0)
150 /* Error */
151 if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048) <= 0)
152 /* Error */
153
154 /* Generate key */
155 if (EVP_PKEY_keygen(ctx, &pkey) <= 0)
156 /* Error */
157
158 Generate a key from a set of parameters:
159
160 #include <openssl/evp.h>
161 #include <openssl/rsa.h>
162
163 EVP_PKEY_CTX *ctx;
164 ENGINE *eng;
165 EVP_PKEY *pkey = NULL, *param;
166
167 /* Assumed param, eng are set up already */
168 ctx = EVP_PKEY_CTX_new(param, eng);
169 if (!ctx)
170 /* Error occurred */
171 if (EVP_PKEY_keygen_init(ctx) <= 0)
172 /* Error */
173
174 /* Generate key */
175 if (EVP_PKEY_keygen(ctx, &pkey) <= 0)
176 /* Error */
177
178 Example of generation callback for OpenSSL public key implementations:
179
180 /* Application data is a BIO to output status to */
181
182 EVP_PKEY_CTX_set_app_data(ctx, status_bio);
183
184 static int genpkey_cb(EVP_PKEY_CTX *ctx)
185 {
186 char c = '*';
187 BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
188 int p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
189
190 if (p == 0)
191 c = '.';
192 if (p == 1)
193 c = '+';
194 if (p == 2)
195 c = '*';
196 if (p == 3)
197 c = '\n';
198 BIO_write(b, &c, 1);
199 (void)BIO_flush(b);
200 return 1;
201 }
202
204 EVP_RSA_gen(3), EVP_EC_gen(3), EVP_PKEY_CTX_new(3),
205 EVP_PKEY_encrypt(3), EVP_PKEY_decrypt(3), EVP_PKEY_sign(3),
206 EVP_PKEY_verify(3), EVP_PKEY_verify_recover(3), EVP_PKEY_derive(3)
207
209 EVP_PKEY_keygen_init(), int EVP_PKEY_paramgen_init(),
210 EVP_PKEY_keygen(), EVP_PKEY_paramgen(), EVP_PKEY_gen_cb(),
211 EVP_PKEY_CTX_set_cb(), EVP_PKEY_CTX_get_cb(),
212 EVP_PKEY_CTX_get_keygen_info(), EVP_PKEY_CTX_set_app_data() and
213 EVP_PKEY_CTX_get_app_data() were added in OpenSSL 1.0.0.
214
215 EVP_PKEY_Q_keygen() and EVP_PKEY_generate() were added in OpenSSL 3.0.
216
218 Copyright 2006-2022 The OpenSSL Project Authors. All Rights Reserved.
219
220 Licensed under the Apache License 2.0 (the "License"). You may not use
221 this file except in compliance with the License. You can obtain a copy
222 in the file LICENSE in the source distribution or at
223 <https://www.openssl.org/source/license.html>.
224
225
226
2273.1.1 2023-08-31 EVP_PKEY_KEYGEN(3ossl)