5 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
9 #include <openssl/evp.h>
11 int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx);
12 int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
13 int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx);
14 int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
16 typedef int EVP_PKEY_gen_cb(EVP_PKEY_CTX *ctx);
18 void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb);
19 EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx);
21 int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx);
23 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data);
24 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx);
28 The EVP_PKEY_keygen_init() function initializes a public key algorithm
29 context using key B<pkey> for a key genration operation.
31 The EVP_PKEY_keygen() function performs a key generation operation, the
32 generated key is written to B<ppkey>.
34 The functions EVP_PKEY_paramgen_init() and EVP_PKEY_paramgen() are similar
35 except parameters are generated.
37 The function EVP_PKEY_set_cb() sets the key or parameter generation callback
38 to B<cb>. The function EVP_PKEY_CTX_get_cb() returns the key or parameter
41 The function EVP_PKEY_CTX_get_keygen_info() returns parameters associated
42 with the generation operation. If B<idx> is -1 the total number of
43 parameters available is returned. Any non negative value returns the value of
44 that parameter. EVP_PKEY_CTX_gen_keygen_info() with a non-negative value for
45 B<idx> should only be called within the generation callback.
47 If the callback returns 0 then the key genration operation is aborted and an
48 error occurs. This might occur during a time consuming operation where
49 a user clicks on a "cancel" button.
51 The functions EVP_PKEY_CTX_set_app_data() and EVP_PKEY_CTX_get_app_data() set
52 and retrieve an opaque pointer. This can be used to set some application
53 defined value which can be retrieved in the callback: for example a handle
54 which is used to update a "progress dialog".
58 After the call to EVP_PKEY_keygen_init() or EVP_PKEY_paramgen_init() algorithm
59 specific control operations can be performed to set any appropriate parameters
62 The functions EVP_PKEY_keygen() and EVP_PKEY_paramgen() can be called more than
63 once on the same context if several operations are performed using the same
66 The meaning of the parameters passed to the callback will depend on the
67 algorithm and the specifiic implementation of the algorithm. Some might not
68 give any useful information at all during key or parameter generation. Others
69 might not even call the callback.
71 The operation performed by key or parameter generation depends on the algorithm
72 used. In some cases (e.g. EC with a supplied named curve) the "generation"
73 option merely sets the appropriate fields in an EVP_PKEY structure.
75 In OpenSSL an EVP_PKEY structure containing a private key also contains the
76 public key components and parameters (if any). An OpenSSL private key is
77 equivalent to what some libraries call a "key pair". A private key can be used
78 in functions which require the use of a public key or parameters.
82 EVP_PKEY_keygen_init(), EVP_PKEY_paramgen_init(), EVP_PKEY_keygen() and
83 EVP_PKEY_paramgen() return 1 for success and 0 or a negative value for failure.
84 In particular a return value of -2 indicates the operation is not supported by
85 the public key algorithm.
89 Generate a 2048 bit RSA key:
91 #include <openssl/evp.h>
92 #include <openssl/rsa.h>
95 EVP_PKEY *pkey = NULL;
96 ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
99 if (EVP_PKEY_keygen_init(ctx) <= 0)
101 if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048) <= 0)
105 if (EVP_PKEY_keygen(ctx, &pkey) <= 0)
108 Generate a key from a set of parameters:
110 #include <openssl/evp.h>
111 #include <openssl/rsa.h>
114 EVP_PKEY *pkey = NULL, *param;
115 /* Assumed param is set up already */
116 ctx = EVP_PKEY_CTX_new(param);
119 if (EVP_PKEY_keygen_init(ctx) <= 0)
123 if (EVP_PKEY_keygen(ctx, &pkey) <= 0)
126 Example of generation callback for OpenSSL public key implementations:
128 /* Application data is a BIO to output status to */
130 EVP_PKEY_CTX_set_app_data(ctx, status_bio);
132 static int genpkey_cb(EVP_PKEY_CTX *ctx)
135 BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
137 p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
149 L<EVP_PKEY_CTX_new(3)|EVP_PKEY_CTX_new(3)>,
150 L<EVP_PKEY_encrypt(3)|EVP_PKEY_encrypt(3)>,
151 L<EVP_PKEY_decrypt(3)|EVP_PKEY_decrypt(3)>,
152 L<EVP_PKEY_sign(3)|EVP_PKEY_sign(3)>,
153 L<EVP_PKEY_verify(3)|EVP_PKEY_verify(3)>,
154 L<EVP_PKEY_verifyrecover(3)|EVP_PKEY_verifyrecover(3)>,
155 L<EVP_PKEY_derive(3)|EVP_PKEY_derive(3)>
159 These functions were first added to OpenSSL 1.0.0.