xref: /freebsd/crypto/openssl/demos/pkey/EVP_PKEY_RSA_keygen.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*-
2*e0c4386eSCy Schubert  * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  */
9*e0c4386eSCy Schubert 
10*e0c4386eSCy Schubert /*
11*e0c4386eSCy Schubert  * Example showing how to generate an RSA key pair.
12*e0c4386eSCy Schubert  *
13*e0c4386eSCy Schubert  * When generating an RSA key, you must specify the number of bits in the key. A
14*e0c4386eSCy Schubert  * reasonable value would be 4096. Avoid using values below 2048. These values
15*e0c4386eSCy Schubert  * are reasonable as of 2022.
16*e0c4386eSCy Schubert  */
17*e0c4386eSCy Schubert 
18*e0c4386eSCy Schubert #include <string.h>
19*e0c4386eSCy Schubert #include <stdio.h>
20*e0c4386eSCy Schubert #include <openssl/err.h>
21*e0c4386eSCy Schubert #include <openssl/evp.h>
22*e0c4386eSCy Schubert #include <openssl/rsa.h>
23*e0c4386eSCy Schubert #include <openssl/core_names.h>
24*e0c4386eSCy Schubert #include <openssl/pem.h>
25*e0c4386eSCy Schubert 
26*e0c4386eSCy Schubert /* A property query used for selecting algorithm implementations. */
27*e0c4386eSCy Schubert static const char *propq = NULL;
28*e0c4386eSCy Schubert 
29*e0c4386eSCy Schubert /*
30*e0c4386eSCy Schubert  * Generates an RSA public-private key pair and returns it.
31*e0c4386eSCy Schubert  * The number of bits is specified by the bits argument.
32*e0c4386eSCy Schubert  *
33*e0c4386eSCy Schubert  * This uses the long way of generating an RSA key.
34*e0c4386eSCy Schubert  */
generate_rsa_key_long(OSSL_LIB_CTX * libctx,unsigned int bits)35*e0c4386eSCy Schubert static EVP_PKEY *generate_rsa_key_long(OSSL_LIB_CTX *libctx, unsigned int bits)
36*e0c4386eSCy Schubert {
37*e0c4386eSCy Schubert     EVP_PKEY_CTX *genctx = NULL;
38*e0c4386eSCy Schubert     EVP_PKEY *pkey = NULL;
39*e0c4386eSCy Schubert     unsigned int primes = 2;
40*e0c4386eSCy Schubert 
41*e0c4386eSCy Schubert     /* Create context using RSA algorithm. "RSA-PSS" could also be used here. */
42*e0c4386eSCy Schubert     genctx = EVP_PKEY_CTX_new_from_name(libctx, "RSA", propq);
43*e0c4386eSCy Schubert     if (genctx == NULL) {
44*e0c4386eSCy Schubert         fprintf(stderr, "EVP_PKEY_CTX_new_from_name() failed\n");
45*e0c4386eSCy Schubert         goto cleanup;
46*e0c4386eSCy Schubert     }
47*e0c4386eSCy Schubert 
48*e0c4386eSCy Schubert     /* Initialize context for key generation purposes. */
49*e0c4386eSCy Schubert     if (EVP_PKEY_keygen_init(genctx) <= 0) {
50*e0c4386eSCy Schubert         fprintf(stderr, "EVP_PKEY_keygen_init() failed\n");
51*e0c4386eSCy Schubert         goto cleanup;
52*e0c4386eSCy Schubert     }
53*e0c4386eSCy Schubert 
54*e0c4386eSCy Schubert     /*
55*e0c4386eSCy Schubert      * Here we set the number of bits to use in the RSA key.
56*e0c4386eSCy Schubert      * See comment at top of file for information on appropriate values.
57*e0c4386eSCy Schubert      */
58*e0c4386eSCy Schubert     if (EVP_PKEY_CTX_set_rsa_keygen_bits(genctx, bits) <= 0) {
59*e0c4386eSCy Schubert         fprintf(stderr, "EVP_PKEY_CTX_set_rsa_keygen_bits() failed\n");
60*e0c4386eSCy Schubert         goto cleanup;
61*e0c4386eSCy Schubert     }
62*e0c4386eSCy Schubert 
63*e0c4386eSCy Schubert     /*
64*e0c4386eSCy Schubert      * It is possible to create an RSA key using more than two primes.
65*e0c4386eSCy Schubert      * Do not do this unless you know why you need this.
66*e0c4386eSCy Schubert      * You ordinarily do not need to specify this, as the default is two.
67*e0c4386eSCy Schubert      *
68*e0c4386eSCy Schubert      * Both of these parameters can also be set via EVP_PKEY_CTX_set_params, but
69*e0c4386eSCy Schubert      * these functions provide a more concise way to do so.
70*e0c4386eSCy Schubert      */
71*e0c4386eSCy Schubert     if (EVP_PKEY_CTX_set_rsa_keygen_primes(genctx, primes) <= 0) {
72*e0c4386eSCy Schubert         fprintf(stderr, "EVP_PKEY_CTX_set_rsa_keygen_primes() failed\n");
73*e0c4386eSCy Schubert         goto cleanup;
74*e0c4386eSCy Schubert     }
75*e0c4386eSCy Schubert 
76*e0c4386eSCy Schubert     /*
77*e0c4386eSCy Schubert      * Generating an RSA key with a number of bits large enough to be secure for
78*e0c4386eSCy Schubert      * modern applications can take a fairly substantial amount of time (e.g.
79*e0c4386eSCy Schubert      * one second). If you require fast key generation, consider using an EC key
80*e0c4386eSCy Schubert      * instead.
81*e0c4386eSCy Schubert      *
82*e0c4386eSCy Schubert      * If you require progress information during the key generation process,
83*e0c4386eSCy Schubert      * you can set a progress callback using EVP_PKEY_set_cb; see the example in
84*e0c4386eSCy Schubert      * EVP_PKEY_generate(3).
85*e0c4386eSCy Schubert      */
86*e0c4386eSCy Schubert     fprintf(stderr, "Generating RSA key, this may take some time...\n");
87*e0c4386eSCy Schubert     if (EVP_PKEY_generate(genctx, &pkey) <= 0) {
88*e0c4386eSCy Schubert         fprintf(stderr, "EVP_PKEY_generate() failed\n");
89*e0c4386eSCy Schubert         goto cleanup;
90*e0c4386eSCy Schubert     }
91*e0c4386eSCy Schubert 
92*e0c4386eSCy Schubert     /* pkey is now set to an object representing the generated key pair. */
93*e0c4386eSCy Schubert 
94*e0c4386eSCy Schubert cleanup:
95*e0c4386eSCy Schubert     EVP_PKEY_CTX_free(genctx);
96*e0c4386eSCy Schubert     return pkey;
97*e0c4386eSCy Schubert }
98*e0c4386eSCy Schubert 
99*e0c4386eSCy Schubert /*
100*e0c4386eSCy Schubert  * Generates an RSA public-private key pair and returns it.
101*e0c4386eSCy Schubert  * The number of bits is specified by the bits argument.
102*e0c4386eSCy Schubert  *
103*e0c4386eSCy Schubert  * This uses a more concise way of generating an RSA key, which is suitable for
104*e0c4386eSCy Schubert  * simple cases. It is used if -s is passed on the command line, otherwise the
105*e0c4386eSCy Schubert  * long method above is used. The ability to choose between these two methods is
106*e0c4386eSCy Schubert  * shown here only for demonstration; the results are equivalent.
107*e0c4386eSCy Schubert  */
generate_rsa_key_short(OSSL_LIB_CTX * libctx,unsigned int bits)108*e0c4386eSCy Schubert static EVP_PKEY *generate_rsa_key_short(OSSL_LIB_CTX *libctx, unsigned int bits)
109*e0c4386eSCy Schubert {
110*e0c4386eSCy Schubert     EVP_PKEY *pkey = NULL;
111*e0c4386eSCy Schubert 
112*e0c4386eSCy Schubert     fprintf(stderr, "Generating RSA key, this may take some time...\n");
113*e0c4386eSCy Schubert     pkey = EVP_PKEY_Q_keygen(libctx, propq, "RSA", (size_t)bits);
114*e0c4386eSCy Schubert 
115*e0c4386eSCy Schubert     if (pkey == NULL)
116*e0c4386eSCy Schubert         fprintf(stderr, "EVP_PKEY_Q_keygen() failed\n");
117*e0c4386eSCy Schubert 
118*e0c4386eSCy Schubert     return pkey;
119*e0c4386eSCy Schubert }
120*e0c4386eSCy Schubert 
121*e0c4386eSCy Schubert /*
122*e0c4386eSCy Schubert  * Prints information on an EVP_PKEY object representing an RSA key pair.
123*e0c4386eSCy Schubert  */
dump_key(const EVP_PKEY * pkey)124*e0c4386eSCy Schubert static int dump_key(const EVP_PKEY *pkey)
125*e0c4386eSCy Schubert {
126*e0c4386eSCy Schubert     int rv = 0;
127*e0c4386eSCy Schubert     int bits = 0;
128*e0c4386eSCy Schubert     BIGNUM *n = NULL, *e = NULL, *d = NULL, *p = NULL, *q = NULL;
129*e0c4386eSCy Schubert 
130*e0c4386eSCy Schubert     /*
131*e0c4386eSCy Schubert      * Retrieve value of n. This value is not secret and forms part of the
132*e0c4386eSCy Schubert      * public key.
133*e0c4386eSCy Schubert      *
134*e0c4386eSCy Schubert      * Calling EVP_PKEY_get_bn_param with a NULL BIGNUM pointer causes
135*e0c4386eSCy Schubert      * a new BIGNUM to be allocated, so these must be freed subsequently.
136*e0c4386eSCy Schubert      */
137*e0c4386eSCy Schubert     if (EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_N, &n) == 0) {
138*e0c4386eSCy Schubert         fprintf(stderr, "Failed to retrieve n\n");
139*e0c4386eSCy Schubert         goto cleanup;
140*e0c4386eSCy Schubert     }
141*e0c4386eSCy Schubert 
142*e0c4386eSCy Schubert     /*
143*e0c4386eSCy Schubert      * Retrieve value of e. This value is not secret and forms part of the
144*e0c4386eSCy Schubert      * public key. It is typically 65537 and need not be changed.
145*e0c4386eSCy Schubert      */
146*e0c4386eSCy Schubert     if (EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_E, &e) == 0) {
147*e0c4386eSCy Schubert         fprintf(stderr, "Failed to retrieve e\n");
148*e0c4386eSCy Schubert         goto cleanup;
149*e0c4386eSCy Schubert     }
150*e0c4386eSCy Schubert 
151*e0c4386eSCy Schubert     /*
152*e0c4386eSCy Schubert      * Retrieve value of d. This value is secret and forms part of the private
153*e0c4386eSCy Schubert      * key. It must not be published.
154*e0c4386eSCy Schubert      */
155*e0c4386eSCy Schubert     if (EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_D, &d) == 0) {
156*e0c4386eSCy Schubert         fprintf(stderr, "Failed to retrieve d\n");
157*e0c4386eSCy Schubert         goto cleanup;
158*e0c4386eSCy Schubert     }
159*e0c4386eSCy Schubert 
160*e0c4386eSCy Schubert     /*
161*e0c4386eSCy Schubert      * Retrieve value of the first prime factor, commonly known as p. This value
162*e0c4386eSCy Schubert      * is secret and forms part of the private key. It must not be published.
163*e0c4386eSCy Schubert      */
164*e0c4386eSCy Schubert     if (EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_FACTOR1, &p) == 0) {
165*e0c4386eSCy Schubert         fprintf(stderr, "Failed to retrieve p\n");
166*e0c4386eSCy Schubert         goto cleanup;
167*e0c4386eSCy Schubert     }
168*e0c4386eSCy Schubert 
169*e0c4386eSCy Schubert     /*
170*e0c4386eSCy Schubert      * Retrieve value of the second prime factor, commonly known as q. This value
171*e0c4386eSCy Schubert      * is secret and forms part of the private key. It must not be published.
172*e0c4386eSCy Schubert      *
173*e0c4386eSCy Schubert      * If you are creating an RSA key with more than two primes for special
174*e0c4386eSCy Schubert      * applications, you can retrieve these primes with
175*e0c4386eSCy Schubert      * OSSL_PKEY_PARAM_RSA_FACTOR3, etc.
176*e0c4386eSCy Schubert      */
177*e0c4386eSCy Schubert     if (EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_FACTOR2, &q) == 0) {
178*e0c4386eSCy Schubert         fprintf(stderr, "Failed to retrieve q\n");
179*e0c4386eSCy Schubert         goto cleanup;
180*e0c4386eSCy Schubert     }
181*e0c4386eSCy Schubert 
182*e0c4386eSCy Schubert     /*
183*e0c4386eSCy Schubert      * We can also retrieve the key size in bits for informational purposes.
184*e0c4386eSCy Schubert      */
185*e0c4386eSCy Schubert     if (EVP_PKEY_get_int_param(pkey, OSSL_PKEY_PARAM_BITS, &bits) == 0) {
186*e0c4386eSCy Schubert         fprintf(stderr, "Failed to retrieve bits\n");
187*e0c4386eSCy Schubert         goto cleanup;
188*e0c4386eSCy Schubert     }
189*e0c4386eSCy Schubert 
190*e0c4386eSCy Schubert     /* Output hexadecimal representations of the BIGNUM objects. */
191*e0c4386eSCy Schubert     fprintf(stdout, "\nNumber of bits: %d\n\n", bits);
192*e0c4386eSCy Schubert     fprintf(stderr, "Public values:\n");
193*e0c4386eSCy Schubert     fprintf(stdout, "  n = 0x");
194*e0c4386eSCy Schubert     BN_print_fp(stdout, n);
195*e0c4386eSCy Schubert     fprintf(stdout, "\n");
196*e0c4386eSCy Schubert 
197*e0c4386eSCy Schubert     fprintf(stdout, "  e = 0x");
198*e0c4386eSCy Schubert     BN_print_fp(stdout, e);
199*e0c4386eSCy Schubert     fprintf(stdout, "\n\n");
200*e0c4386eSCy Schubert 
201*e0c4386eSCy Schubert     fprintf(stdout, "Private values:\n");
202*e0c4386eSCy Schubert     fprintf(stdout, "  d = 0x");
203*e0c4386eSCy Schubert     BN_print_fp(stdout, d);
204*e0c4386eSCy Schubert     fprintf(stdout, "\n");
205*e0c4386eSCy Schubert 
206*e0c4386eSCy Schubert     fprintf(stdout, "  p = 0x");
207*e0c4386eSCy Schubert     BN_print_fp(stdout, p);
208*e0c4386eSCy Schubert     fprintf(stdout, "\n");
209*e0c4386eSCy Schubert 
210*e0c4386eSCy Schubert     fprintf(stdout, "  q = 0x");
211*e0c4386eSCy Schubert     BN_print_fp(stdout, q);
212*e0c4386eSCy Schubert     fprintf(stdout, "\n\n");
213*e0c4386eSCy Schubert 
214*e0c4386eSCy Schubert     /* Output a PEM encoding of the public key. */
215*e0c4386eSCy Schubert     if (PEM_write_PUBKEY(stdout, pkey) == 0) {
216*e0c4386eSCy Schubert         fprintf(stderr, "Failed to output PEM-encoded public key\n");
217*e0c4386eSCy Schubert         goto cleanup;
218*e0c4386eSCy Schubert     }
219*e0c4386eSCy Schubert 
220*e0c4386eSCy Schubert     /*
221*e0c4386eSCy Schubert      * Output a PEM encoding of the private key. Please note that this output is
222*e0c4386eSCy Schubert      * not encrypted. You may wish to use the arguments to specify encryption of
223*e0c4386eSCy Schubert      * the key if you are storing it on disk. See PEM_write_PrivateKey(3).
224*e0c4386eSCy Schubert      */
225*e0c4386eSCy Schubert     if (PEM_write_PrivateKey(stdout, pkey, NULL, NULL, 0, NULL, NULL) == 0) {
226*e0c4386eSCy Schubert         fprintf(stderr, "Failed to output PEM-encoded private key\n");
227*e0c4386eSCy Schubert         goto cleanup;
228*e0c4386eSCy Schubert     }
229*e0c4386eSCy Schubert 
230*e0c4386eSCy Schubert     rv = 1;
231*e0c4386eSCy Schubert cleanup:
232*e0c4386eSCy Schubert     BN_free(n); /* not secret */
233*e0c4386eSCy Schubert     BN_free(e); /* not secret */
234*e0c4386eSCy Schubert     BN_clear_free(d); /* secret - scrub before freeing */
235*e0c4386eSCy Schubert     BN_clear_free(p); /* secret - scrub before freeing */
236*e0c4386eSCy Schubert     BN_clear_free(q); /* secret - scrub before freeing */
237*e0c4386eSCy Schubert     return rv;
238*e0c4386eSCy Schubert }
239*e0c4386eSCy Schubert 
main(int argc,char ** argv)240*e0c4386eSCy Schubert int main(int argc, char **argv)
241*e0c4386eSCy Schubert {
242*e0c4386eSCy Schubert     int rv = 1;
243*e0c4386eSCy Schubert     OSSL_LIB_CTX *libctx = NULL;
244*e0c4386eSCy Schubert     EVP_PKEY *pkey = NULL;
245*e0c4386eSCy Schubert     unsigned int bits = 4096;
246*e0c4386eSCy Schubert     int bits_i, use_short = 0;
247*e0c4386eSCy Schubert 
248*e0c4386eSCy Schubert     /* usage: [-s] [<bits>] */
249*e0c4386eSCy Schubert     if (argc > 1 && strcmp(argv[1], "-s") == 0) {
250*e0c4386eSCy Schubert         --argc;
251*e0c4386eSCy Schubert         ++argv;
252*e0c4386eSCy Schubert         use_short = 1;
253*e0c4386eSCy Schubert     }
254*e0c4386eSCy Schubert 
255*e0c4386eSCy Schubert     if (argc > 1) {
256*e0c4386eSCy Schubert         bits_i = atoi(argv[1]);
257*e0c4386eSCy Schubert         if (bits < 512) {
258*e0c4386eSCy Schubert             fprintf(stderr, "Invalid RSA key size\n");
259*e0c4386eSCy Schubert             return 1;
260*e0c4386eSCy Schubert         }
261*e0c4386eSCy Schubert 
262*e0c4386eSCy Schubert         bits = (unsigned int)bits_i;
263*e0c4386eSCy Schubert     }
264*e0c4386eSCy Schubert 
265*e0c4386eSCy Schubert     /* Avoid using key sizes less than 2048 bits; see comment at top of file. */
266*e0c4386eSCy Schubert     if (bits < 2048)
267*e0c4386eSCy Schubert         fprintf(stderr, "Warning: very weak key size\n\n");
268*e0c4386eSCy Schubert 
269*e0c4386eSCy Schubert     /* Generate RSA key. */
270*e0c4386eSCy Schubert     if (use_short)
271*e0c4386eSCy Schubert         pkey = generate_rsa_key_short(libctx, bits);
272*e0c4386eSCy Schubert     else
273*e0c4386eSCy Schubert         pkey = generate_rsa_key_long(libctx, bits);
274*e0c4386eSCy Schubert 
275*e0c4386eSCy Schubert     if (pkey == NULL)
276*e0c4386eSCy Schubert         goto cleanup;
277*e0c4386eSCy Schubert 
278*e0c4386eSCy Schubert     /* Dump the integers comprising the key. */
279*e0c4386eSCy Schubert     if (dump_key(pkey) == 0) {
280*e0c4386eSCy Schubert         fprintf(stderr, "Failed to dump key\n");
281*e0c4386eSCy Schubert         goto cleanup;
282*e0c4386eSCy Schubert     }
283*e0c4386eSCy Schubert 
284*e0c4386eSCy Schubert     rv = 0;
285*e0c4386eSCy Schubert cleanup:
286*e0c4386eSCy Schubert     EVP_PKEY_free(pkey);
287*e0c4386eSCy Schubert     OSSL_LIB_CTX_free(libctx);
288*e0c4386eSCy Schubert     return rv;
289*e0c4386eSCy Schubert }
290