099cd0cf8fc506d01af67fc626c824ca70c12929
[openssl.git] / doc / man3 / EVP_PKEY_keygen.pod
1 =pod
2
3 =head1 NAME
4
5 EVP_PKEY_keygen_init, EVP_PKEY_keygen, EVP_PKEY_paramgen_init,
6 EVP_PKEY_paramgen, EVP_PKEY_CTX_set_cb, EVP_PKEY_CTX_get_cb,
7 EVP_PKEY_CTX_get_keygen_info, EVP_PKEY_CTX_set_app_data,
8 EVP_PKEY_CTX_get_app_data,
9 EVP_PKEY_gen_cb, EVP_PKEY_check
10 - key and parameter generation and check functions
11
12 =head1 SYNOPSIS
13
14  #include <openssl/evp.h>
15
16  int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx);
17  int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
18  int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx);
19  int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
20
21  typedef int EVP_PKEY_gen_cb(EVP_PKEY_CTX *ctx);
22
23  void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb);
24  EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx);
25
26  int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx);
27
28  void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data);
29  void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx);
30
31  int EVP_PKEY_check(EVP_PKEY_CTX *ctx);
32
33 =head1 DESCRIPTION
34
35 The EVP_PKEY_keygen_init() function initializes a public key algorithm
36 context using key B<pkey> for a key generation operation.
37
38 The EVP_PKEY_keygen() function performs a key generation operation, the
39 generated key is written to B<ppkey>.
40
41 The functions EVP_PKEY_paramgen_init() and EVP_PKEY_paramgen() are similar
42 except parameters are generated.
43
44 The function EVP_PKEY_set_cb() sets the key or parameter generation callback
45 to B<cb>. The function EVP_PKEY_CTX_get_cb() returns the key or parameter
46 generation callback.
47
48 The function EVP_PKEY_CTX_get_keygen_info() returns parameters associated
49 with the generation operation. If B<idx> is -1 the total number of
50 parameters available is returned. Any non negative value returns the value of
51 that parameter. EVP_PKEY_CTX_gen_keygen_info() with a non-negative value for
52 B<idx> should only be called within the generation callback.
53
54 If the callback returns 0 then the key generation operation is aborted and an
55 error occurs. This might occur during a time consuming operation where
56 a user clicks on a "cancel" button.
57
58 The functions EVP_PKEY_CTX_set_app_data() and EVP_PKEY_CTX_get_app_data() set
59 and retrieve an opaque pointer. This can be used to set some application
60 defined value which can be retrieved in the callback: for example a handle
61 which is used to update a "progress dialog".
62
63 EVP_PKEY_check() validates the key-pair given by B<ctx>. This function first tries
64 to use customized key check method in B<EVP_PKEY_METHOD> if it's present; otherwise
65 it calls a default one defined in B<EVP_PKEY_ASN1_METHOD>.
66
67 =head1 NOTES
68
69 After the call to EVP_PKEY_keygen_init() or EVP_PKEY_paramgen_init() algorithm
70 specific control operations can be performed to set any appropriate parameters
71 for the operation.
72
73 The functions EVP_PKEY_keygen() and EVP_PKEY_paramgen() can be called more than
74 once on the same context if several operations are performed using the same
75 parameters.
76
77 The meaning of the parameters passed to the callback will depend on the
78 algorithm and the specific implementation of the algorithm. Some might not
79 give any useful information at all during key or parameter generation. Others
80 might not even call the callback.
81
82 The operation performed by key or parameter generation depends on the algorithm
83 used. In some cases (e.g. EC with a supplied named curve) the "generation"
84 option merely sets the appropriate fields in an EVP_PKEY structure.
85
86 In OpenSSL an EVP_PKEY structure containing a private key also contains the
87 public key components and parameters (if any). An OpenSSL private key is
88 equivalent to what some libraries call a "key pair". A private key can be used
89 in functions which require the use of a public key or parameters.
90
91 =head1 RETURN VALUES
92
93 EVP_PKEY_keygen_init(), EVP_PKEY_paramgen_init(), EVP_PKEY_keygen() and
94 EVP_PKEY_paramgen() return 1 for success and 0 or a negative value for failure.
95 In particular a return value of -2 indicates the operation is not supported by
96 the public key algorithm.
97
98 EVP_PKEY_check() returns 1 for success or others for failure. It
99 returns -2 if the operation is not supported for the specific algorithm.
100
101 =head1 EXAMPLES
102
103 Generate a 2048 bit RSA key:
104
105  #include <openssl/evp.h>
106  #include <openssl/rsa.h>
107
108  EVP_PKEY_CTX *ctx;
109  EVP_PKEY *pkey = NULL;
110
111  ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
112  if (!ctx)
113      /* Error occurred */
114  if (EVP_PKEY_keygen_init(ctx) <= 0)
115      /* Error */
116  if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048) <= 0)
117      /* Error */
118
119  /* Generate key */
120  if (EVP_PKEY_keygen(ctx, &pkey) <= 0)
121      /* Error */
122
123 Generate a key from a set of parameters:
124
125  #include <openssl/evp.h>
126  #include <openssl/rsa.h>
127
128  EVP_PKEY_CTX *ctx;
129  EVP_PKEY *pkey = NULL, *param;
130
131  /* Assumed param is set up already */
132  ctx = EVP_PKEY_CTX_new(param);
133  if (!ctx)
134      /* Error occurred */
135  if (EVP_PKEY_keygen_init(ctx) <= 0)
136      /* Error */
137
138  /* Generate key */
139  if (EVP_PKEY_keygen(ctx, &pkey) <= 0)
140      /* Error */
141
142 Example of generation callback for OpenSSL public key implementations:
143
144  /* Application data is a BIO to output status to */
145
146  EVP_PKEY_CTX_set_app_data(ctx, status_bio);
147
148  static int genpkey_cb(EVP_PKEY_CTX *ctx)
149  {
150      char c = '*';
151      BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
152      int p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
153
154      if (p == 0)
155          c = '.';
156      if (p == 1)
157          c = '+';
158      if (p == 2)
159          c = '*';
160      if (p == 3)
161          c = '\n';
162      BIO_write(b, &c, 1);
163      (void)BIO_flush(b);
164      return 1;
165  }
166
167 =head1 SEE ALSO
168
169 L<EVP_PKEY_CTX_new(3)>,
170 L<EVP_PKEY_encrypt(3)>,
171 L<EVP_PKEY_decrypt(3)>,
172 L<EVP_PKEY_sign(3)>,
173 L<EVP_PKEY_verify(3)>,
174 L<EVP_PKEY_verify_recover(3)>,
175 L<EVP_PKEY_derive(3)>
176
177 =head1 HISTORY
178
179 These functions were first added to OpenSSL 1.0.0.
180
181 =head1 COPYRIGHT
182
183 Copyright 2006-2017 The OpenSSL Project Authors. All Rights Reserved.
184
185 Licensed under the OpenSSL license (the "License").  You may not use
186 this file except in compliance with the License.  You can obtain a copy
187 in the file LICENSE in the source distribution or at
188 L<https://www.openssl.org/source/license.html>.
189
190 =cut