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