1EVP_PKEY_KEYGEN(3ossl)              OpenSSL             EVP_PKEY_KEYGEN(3ossl)
2
3
4

NAME

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

SYNOPSIS

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

DESCRIPTION

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", or "ED448" no further parameter is needed.
102

RETURN VALUES

104       EVP_PKEY_keygen_init(), EVP_PKEY_paramgen_init(), EVP_PKEY_keygen() and
105       EVP_PKEY_paramgen() return 1 for success and 0 or a negative value for
106       failure.  In particular a return value of -2 indicates the operation is
107       not supported by the public key algorithm.
108
109       EVP_PKEY_Q_keygen() returns an EVP_PKEY, or NULL on failure.
110

NOTES

112       After the call to EVP_PKEY_keygen_init() or EVP_PKEY_paramgen_init()
113       algorithm specific control operations can be performed to set any
114       appropriate parameters for the operation.
115
116       The functions EVP_PKEY_keygen() and EVP_PKEY_paramgen() can be called
117       more than once on the same context if several operations are performed
118       using the same parameters.
119
120       The meaning of the parameters passed to the callback will depend on the
121       algorithm and the specific implementation of the algorithm. Some might
122       not give any useful information at all during key or parameter
123       generation. Others might not even call the callback.
124
125       The operation performed by key or parameter generation depends on the
126       algorithm used. In some cases (e.g. EC with a supplied named curve) the
127       "generation" option merely sets the appropriate fields in an EVP_PKEY
128       structure.
129
130       In OpenSSL an EVP_PKEY structure containing a private key also contains
131       the public key components and parameters (if any). An OpenSSL private
132       key is equivalent to what some libraries call a "key pair". A private
133       key can be used in functions which require the use of a public key or
134       parameters.
135

EXAMPLES

137       Generate a 2048 bit RSA key:
138
139        #include <openssl/evp.h>
140        #include <openssl/rsa.h>
141
142        EVP_PKEY_CTX *ctx;
143        EVP_PKEY *pkey = NULL;
144
145        ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
146        if (!ctx)
147            /* Error occurred */
148        if (EVP_PKEY_keygen_init(ctx) <= 0)
149            /* Error */
150        if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048) <= 0)
151            /* Error */
152
153        /* Generate key */
154        if (EVP_PKEY_keygen(ctx, &pkey) <= 0)
155            /* Error */
156
157       Generate a key from a set of parameters:
158
159        #include <openssl/evp.h>
160        #include <openssl/rsa.h>
161
162        EVP_PKEY_CTX *ctx;
163        ENGINE *eng;
164        EVP_PKEY *pkey = NULL, *param;
165
166        /* Assumed param, eng are set up already */
167        ctx = EVP_PKEY_CTX_new(param, eng);
168        if (!ctx)
169            /* Error occurred */
170        if (EVP_PKEY_keygen_init(ctx) <= 0)
171            /* Error */
172
173        /* Generate key */
174        if (EVP_PKEY_keygen(ctx, &pkey) <= 0)
175            /* Error */
176
177       Example of generation callback for OpenSSL public key implementations:
178
179        /* Application data is a BIO to output status to */
180
181        EVP_PKEY_CTX_set_app_data(ctx, status_bio);
182
183        static int genpkey_cb(EVP_PKEY_CTX *ctx)
184        {
185            char c = '*';
186            BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
187            int p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
188
189            if (p == 0)
190                c = '.';
191            if (p == 1)
192                c = '+';
193            if (p == 2)
194                c = '*';
195            if (p == 3)
196                c = '\n';
197            BIO_write(b, &c, 1);
198            (void)BIO_flush(b);
199            return 1;
200        }
201

SEE ALSO

203       EVP_RSA_gen(3), EVP_EC_gen(3), EVP_PKEY_CTX_new(3),
204       EVP_PKEY_encrypt(3), EVP_PKEY_decrypt(3), EVP_PKEY_sign(3),
205       EVP_PKEY_verify(3), EVP_PKEY_verify_recover(3), EVP_PKEY_derive(3)
206

HISTORY

208       EVP_PKEY_keygen_init(), int EVP_PKEY_paramgen_init(),
209       EVP_PKEY_keygen(), EVP_PKEY_paramgen(), EVP_PKEY_gen_cb(),
210       EVP_PKEY_CTX_set_cb(), EVP_PKEY_CTX_get_cb(),
211       EVP_PKEY_CTX_get_keygen_info(), EVP_PKEY_CTX_set_app_data() and
212       EVP_PKEY_CTX_get_app_data() were added in OpenSSL 1.0.0.
213
214       EVP_PKEY_Q_keygen() and EVP_PKEY_generate() were added in OpenSSL 3.0.
215
217       Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
218
219       Licensed under the Apache License 2.0 (the "License").  You may not use
220       this file except in compliance with the License.  You can obtain a copy
221       in the file LICENSE in the source distribution or at
222       <https://www.openssl.org/source/license.html>.
223
224
225
2263.0.5                             2022-11-01            EVP_PKEY_KEYGEN(3ossl)
Impressum