sha/asm/keccak1600-avx2.pl: optimized remodelled version.
[openssl.git] / apps / dsaparam.c
1 /*
2  * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 #ifdef OPENSSL_NO_DSA
12 NON_EMPTY_TRANSLATION_UNIT
13 #else
14
15 # include <stdio.h>
16 # include <stdlib.h>
17 # include <time.h>
18 # include <string.h>
19 # include "apps.h"
20 # include <openssl/bio.h>
21 # include <openssl/err.h>
22 # include <openssl/bn.h>
23 # include <openssl/dsa.h>
24 # include <openssl/x509.h>
25 # include <openssl/pem.h>
26
27 static int dsa_cb(int p, int n, BN_GENCB *cb);
28
29 typedef enum OPTION_choice {
30     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
31     OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT, OPT_C,
32     OPT_NOOUT, OPT_GENKEY, OPT_RAND, OPT_ENGINE
33 } OPTION_CHOICE;
34
35 const OPTIONS dsaparam_options[] = {
36     {"help", OPT_HELP, '-', "Display this summary"},
37     {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
38     {"in", OPT_IN, '<', "Input file"},
39     {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
40     {"out", OPT_OUT, '>', "Output file"},
41     {"text", OPT_TEXT, '-', "Print as text"},
42     {"C", OPT_C, '-', "Output C code"},
43     {"noout", OPT_NOOUT, '-', "No output"},
44     {"genkey", OPT_GENKEY, '-', "Generate a DSA key"},
45     {"rand", OPT_RAND, 's', "Files to use for random number input"},
46 # ifndef OPENSSL_NO_ENGINE
47     {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
48 # endif
49     {NULL}
50 };
51
52 int dsaparam_main(int argc, char **argv)
53 {
54     ENGINE *e = NULL;
55     DSA *dsa = NULL;
56     BIO *in = NULL, *out = NULL;
57     BN_GENCB *cb = NULL;
58     int numbits = -1, num = 0, genkey = 0, need_rand = 0;
59     int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0;
60     int ret = 1, i, text = 0, private = 0;
61     char *infile = NULL, *outfile = NULL, *prog, *inrand = NULL;
62     OPTION_CHOICE o;
63
64     prog = opt_init(argc, argv, dsaparam_options);
65     while ((o = opt_next()) != OPT_EOF) {
66         switch (o) {
67         case OPT_EOF:
68         case OPT_ERR:
69  opthelp:
70             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
71             goto end;
72         case OPT_HELP:
73             opt_help(dsaparam_options);
74             ret = 0;
75             goto end;
76         case OPT_INFORM:
77             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
78                 goto opthelp;
79             break;
80         case OPT_IN:
81             infile = opt_arg();
82             break;
83         case OPT_OUTFORM:
84             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
85                 goto opthelp;
86             break;
87         case OPT_OUT:
88             outfile = opt_arg();
89             break;
90         case OPT_ENGINE:
91             e = setup_engine(opt_arg(), 0);
92             break;
93         case OPT_TEXT:
94             text = 1;
95             break;
96         case OPT_C:
97             C = 1;
98             break;
99         case OPT_GENKEY:
100             genkey = need_rand = 1;
101             break;
102         case OPT_RAND:
103             inrand = opt_arg();
104             need_rand = 1;
105             break;
106         case OPT_NOOUT:
107             noout = 1;
108             break;
109         }
110     }
111     argc = opt_num_rest();
112     argv = opt_rest();
113
114     if (argc == 1) {
115         if (!opt_int(argv[0], &num) || num < 0)
116             goto end;
117         /* generate a key */
118         numbits = num;
119         need_rand = 1;
120     }
121     private = genkey ? 1 : 0;
122
123     in = bio_open_default(infile, 'r', informat);
124     if (in == NULL)
125         goto end;
126     out = bio_open_owner(outfile, outformat, private);
127     if (out == NULL)
128         goto end;
129
130     if (need_rand) {
131         app_RAND_load_file(NULL, (inrand != NULL));
132         if (inrand != NULL)
133             BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
134                        app_RAND_load_files(inrand));
135     }
136
137     if (numbits > 0) {
138         cb = BN_GENCB_new();
139         if (cb == NULL) {
140             BIO_printf(bio_err, "Error allocating BN_GENCB object\n");
141             goto end;
142         }
143         BN_GENCB_set(cb, dsa_cb, bio_err);
144         assert(need_rand);
145         dsa = DSA_new();
146         if (dsa == NULL) {
147             BIO_printf(bio_err, "Error allocating DSA object\n");
148             goto end;
149         }
150         BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
151                    num);
152         BIO_printf(bio_err, "This could take some time\n");
153         if (!DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, cb)) {
154             ERR_print_errors(bio_err);
155             BIO_printf(bio_err, "Error, DSA key generation failed\n");
156             goto end;
157         }
158     } else if (informat == FORMAT_ASN1) {
159         dsa = d2i_DSAparams_bio(in, NULL);
160     } else {
161         dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
162     }
163     if (dsa == NULL) {
164         BIO_printf(bio_err, "unable to load DSA parameters\n");
165         ERR_print_errors(bio_err);
166         goto end;
167     }
168
169     if (text) {
170         DSAparams_print(out, dsa);
171     }
172
173     if (C) {
174         const BIGNUM *p = NULL, *q = NULL, *g = NULL;
175         unsigned char *data;
176         int len, bits_p;
177
178         DSA_get0_pqg(dsa, &p, &q, &g);
179         len = BN_num_bytes(p);
180         bits_p = BN_num_bits(p);
181
182         data = app_malloc(len + 20, "BN space");
183
184         BIO_printf(bio_out, "DSA *get_dsa%d()\n{\n", bits_p);
185         print_bignum_var(bio_out, p, "dsap", len, data);
186         print_bignum_var(bio_out, q, "dsaq", len, data);
187         print_bignum_var(bio_out, g, "dsag", len, data);
188         BIO_printf(bio_out, "    DSA *dsa = DSA_new();\n"
189                             "\n");
190         BIO_printf(bio_out, "    if (dsa == NULL)\n"
191                             "        return NULL;\n");
192         BIO_printf(bio_out, "    dsa->p = BN_bin2bn(dsap_%d, sizeof (dsap_%d), NULL);\n",
193                bits_p, bits_p);
194         BIO_printf(bio_out, "    dsa->q = BN_bin2bn(dsaq_%d, sizeof (dsaq_%d), NULL);\n",
195                bits_p, bits_p);
196         BIO_printf(bio_out, "    dsa->g = BN_bin2bn(dsag_%d, sizeof (dsag_%d), NULL);\n",
197                bits_p, bits_p);
198         BIO_printf(bio_out, "    if (!dsa->p || !dsa->q || !dsa->g) {\n"
199                             "        DSA_free(dsa);\n"
200                             "        return NULL;\n"
201                             "    }\n"
202                             "    return(dsa);\n}\n");
203         OPENSSL_free(data);
204     }
205
206     if (!noout) {
207         if (outformat == FORMAT_ASN1)
208             i = i2d_DSAparams_bio(out, dsa);
209         else
210             i = PEM_write_bio_DSAparams(out, dsa);
211         if (!i) {
212             BIO_printf(bio_err, "unable to write DSA parameters\n");
213             ERR_print_errors(bio_err);
214             goto end;
215         }
216     }
217     if (genkey) {
218         DSA *dsakey;
219
220         assert(need_rand);
221         if ((dsakey = DSAparams_dup(dsa)) == NULL)
222             goto end;
223         if (!DSA_generate_key(dsakey)) {
224             ERR_print_errors(bio_err);
225             DSA_free(dsakey);
226             goto end;
227         }
228         assert(private);
229         if (outformat == FORMAT_ASN1)
230             i = i2d_DSAPrivateKey_bio(out, dsakey);
231         else
232             i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL,
233                                             NULL);
234         DSA_free(dsakey);
235     }
236     if (need_rand)
237         app_RAND_write_file(NULL);
238     ret = 0;
239  end:
240     BN_GENCB_free(cb);
241     BIO_free(in);
242     BIO_free_all(out);
243     DSA_free(dsa);
244     release_engine(e);
245     return (ret);
246 }
247
248 static int dsa_cb(int p, int n, BN_GENCB *cb)
249 {
250     char c = '*';
251
252     if (p == 0)
253         c = '.';
254     if (p == 1)
255         c = '+';
256     if (p == 2)
257         c = '*';
258     if (p == 3)
259         c = '\n';
260     BIO_write(BN_GENCB_get_arg(cb), &c, 1);
261     (void)BIO_flush(BN_GENCB_get_arg(cb));
262     return 1;
263 }
264 #endif