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