SM4 optimization for ARM by HW instruction
[openssl.git] / apps / genrsa.c
1 /*
2  * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <openssl/opensslconf.h>
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include "apps.h"
17 #include "progs.h"
18 #include <openssl/bio.h>
19 #include <openssl/err.h>
20 #include <openssl/bn.h>
21 #include <openssl/rsa.h>
22 #include <openssl/evp.h>
23 #include <openssl/x509.h>
24 #include <openssl/pem.h>
25 #include <openssl/rand.h>
26
27 #define DEFBITS 2048
28 #define DEFPRIMES 2
29
30 static int verbose = 0;
31
32 static int genrsa_cb(EVP_PKEY_CTX *ctx);
33
34 typedef enum OPTION_choice {
35     OPT_COMMON,
36 #ifndef OPENSSL_NO_DEPRECATED_3_0
37     OPT_3,
38 #endif
39     OPT_F4, OPT_ENGINE,
40     OPT_OUT, OPT_PASSOUT, OPT_CIPHER, OPT_PRIMES, OPT_VERBOSE,
41     OPT_R_ENUM, OPT_PROV_ENUM, OPT_TRADITIONAL
42 } OPTION_CHOICE;
43
44 const OPTIONS genrsa_options[] = {
45     {OPT_HELP_STR, 1, '-', "Usage: %s [options] numbits\n"},
46
47     OPT_SECTION("General"),
48     {"help", OPT_HELP, '-', "Display this summary"},
49 #ifndef OPENSSL_NO_ENGINE
50     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
51 #endif
52
53     OPT_SECTION("Input"),
54 #ifndef OPENSSL_NO_DEPRECATED_3_0
55     {"3", OPT_3, '-', "(deprecated) Use 3 for the E value"},
56 #endif
57     {"F4", OPT_F4, '-', "Use the Fermat number F4 (0x10001) for the E value"},
58     {"f4", OPT_F4, '-', "Use the Fermat number F4 (0x10001) for the E value"},
59
60     OPT_SECTION("Output"),
61     {"out", OPT_OUT, '>', "Output the key to specified file"},
62     {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
63     {"primes", OPT_PRIMES, 'p', "Specify number of primes"},
64     {"verbose", OPT_VERBOSE, '-', "Verbose output"},
65     {"traditional", OPT_TRADITIONAL, '-',
66      "Use traditional format for private keys"},
67     {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
68
69     OPT_R_OPTIONS,
70     OPT_PROV_OPTIONS,
71
72     OPT_PARAMETERS(),
73     {"numbits", 0, 0, "Size of key in bits"},
74     {NULL}
75 };
76
77 int genrsa_main(int argc, char **argv)
78 {
79     BN_GENCB *cb = BN_GENCB_new();
80     ENGINE *eng = NULL;
81     BIGNUM *bn = BN_new();
82     BIO *out = NULL;
83     EVP_PKEY *pkey = NULL;
84     EVP_PKEY_CTX *ctx = NULL;
85     EVP_CIPHER *enc = NULL;
86     int ret = 1, num = DEFBITS, private = 0, primes = DEFPRIMES;
87     unsigned long f4 = RSA_F4;
88     char *outfile = NULL, *passoutarg = NULL, *passout = NULL;
89     char *prog, *hexe, *dece, *ciphername = NULL;
90     OPTION_CHOICE o;
91     int traditional = 0;
92
93     if (bn == NULL || cb == NULL)
94         goto end;
95
96     opt_set_unknown_name("cipher");
97     prog = opt_init(argc, argv, genrsa_options);
98     while ((o = opt_next()) != OPT_EOF) {
99         switch (o) {
100         case OPT_EOF:
101         case OPT_ERR:
102 opthelp:
103             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
104             goto end;
105         case OPT_HELP:
106             ret = 0;
107             opt_help(genrsa_options);
108             goto end;
109 #ifndef OPENSSL_NO_DEPRECATED_3_0
110         case OPT_3:
111             f4 = RSA_3;
112             break;
113 #endif
114         case OPT_F4:
115             f4 = RSA_F4;
116             break;
117         case OPT_OUT:
118             outfile = opt_arg();
119             break;
120         case OPT_ENGINE:
121             eng = setup_engine(opt_arg(), 0);
122             break;
123         case OPT_R_CASES:
124             if (!opt_rand(o))
125                 goto end;
126             break;
127         case OPT_PROV_CASES:
128             if (!opt_provider(o))
129                 goto end;
130             break;
131         case OPT_PASSOUT:
132             passoutarg = opt_arg();
133             break;
134         case OPT_CIPHER:
135             ciphername = opt_unknown();
136             break;
137         case OPT_PRIMES:
138             primes = opt_int_arg();
139             break;
140         case OPT_VERBOSE:
141             verbose = 1;
142             break;
143         case OPT_TRADITIONAL:
144             traditional = 1;
145             break;
146         }
147     }
148
149     /* One optional argument, the bitsize. */
150     argc = opt_num_rest();
151     argv = opt_rest();
152
153     if (argc == 1) {
154         if (!opt_int(argv[0], &num) || num <= 0)
155             goto end;
156         if (num > OPENSSL_RSA_MAX_MODULUS_BITS)
157             BIO_printf(bio_err,
158                        "Warning: It is not recommended to use more than %d bit for RSA keys.\n"
159                        "         Your key size is %d! Larger key size may behave not as expected.\n",
160                        OPENSSL_RSA_MAX_MODULUS_BITS, num);
161     } else if (!opt_check_rest_arg(NULL)) {
162         goto opthelp;
163     }
164
165     if (!app_RAND_load())
166         goto end;
167
168     private = 1;
169     if (!opt_cipher(ciphername, &enc))
170         goto end;
171     if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
172         BIO_printf(bio_err, "Error getting password\n");
173         goto end;
174     }
175
176     out = bio_open_owner(outfile, FORMAT_PEM, private);
177     if (out == NULL)
178         goto end;
179
180     if (!init_gen_str(&ctx, "RSA", eng, 0, NULL, NULL))
181         goto end;
182
183     EVP_PKEY_CTX_set_cb(ctx, genrsa_cb);
184     EVP_PKEY_CTX_set_app_data(ctx, bio_err);
185
186     if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, num) <= 0) {
187         BIO_printf(bio_err, "Error setting RSA length\n");
188         goto end;
189     }
190     if (!BN_set_word(bn, f4)) {
191         BIO_printf(bio_err, "Error allocating RSA public exponent\n");
192         goto end;
193     }
194     if (EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx, bn) <= 0) {
195         BIO_printf(bio_err, "Error setting RSA public exponent\n");
196         goto end;
197     }
198     if (EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, primes) <= 0) {
199         BIO_printf(bio_err, "Error setting number of primes\n");
200         goto end;
201     }
202     pkey = app_keygen(ctx, "RSA", num, verbose);
203
204     if (verbose) {
205         BIGNUM *e = NULL;
206
207         /* Every RSA key has an 'e' */
208         EVP_PKEY_get_bn_param(pkey, "e", &e);
209         if (e == NULL) {
210             BIO_printf(bio_err, "Error cannot access RSA e\n");
211             goto end;
212         }
213         hexe = BN_bn2hex(e);
214         dece = BN_bn2dec(e);
215         if (hexe && dece) {
216             BIO_printf(bio_err, "e is %s (0x%s)\n", dece, hexe);
217         }
218         OPENSSL_free(hexe);
219         OPENSSL_free(dece);
220         BN_free(e);
221     }
222     if (traditional) {
223         if (!PEM_write_bio_PrivateKey_traditional(out, pkey, enc, NULL, 0,
224                                                   NULL, passout))
225             goto end;
226     } else {
227         if (!PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, passout))
228             goto end;
229     }
230
231     ret = 0;
232  end:
233     BN_free(bn);
234     BN_GENCB_free(cb);
235     EVP_PKEY_CTX_free(ctx);
236     EVP_PKEY_free(pkey);
237     EVP_CIPHER_free(enc);
238     BIO_free_all(out);
239     release_engine(eng);
240     OPENSSL_free(passout);
241     if (ret != 0)
242         ERR_print_errors(bio_err);
243     return ret;
244 }
245
246 static int genrsa_cb(EVP_PKEY_CTX *ctx)
247 {
248     char c = '*';
249     BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
250     int p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
251
252     if (!verbose)
253         return 1;
254
255     if (p == 0)
256         c = '.';
257     if (p == 1)
258         c = '+';
259     if (p == 2)
260         c = '*';
261     if (p == 3)
262         c = '\n';
263     BIO_write(b, &c, 1);
264     (void)BIO_flush(b);
265     return 1;
266 }