Deleted Added
full compact
ecparam.c (1.1.1.7) ecparam.c (1.1.1.8)
1/* apps/ecparam.c */
2/*
1/*
3 * Written by Nils Larsch for the OpenSSL project.
4 */
5/* ====================================================================
6 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
2 * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
7 *
3 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@openssl.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
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
57 */
8 */
9
58/* ====================================================================
59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60 *
61 * Portions of the attached software ("Contribution") are developed by
62 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
63 *
64 * The Contribution is licensed pursuant to the OpenSSL open source
65 * license provided above.
66 *
67 * The elliptic curve binary polynomial software is originally written by
68 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
69 *
70 */
71
72#include <openssl/opensslconf.h>
10/* ====================================================================
11 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
12 *
13 * Portions of the attached software ("Contribution") are developed by
14 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
15 *
16 * The Contribution is licensed pursuant to the OpenSSL open source
17 * license provided above.
18 *
19 * The elliptic curve binary polynomial software is originally written by
20 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
21 *
22 */
23
24#include <openssl/opensslconf.h>
73#ifndef OPENSSL_NO_EC
74# include <assert.h>
25#ifdef OPENSSL_NO_EC
26NON_EMPTY_TRANSLATION_UNIT
27#else
28
75# include <stdio.h>
76# include <stdlib.h>
77# include <time.h>
78# include <string.h>
79# include "apps.h"
80# include <openssl/bio.h>
81# include <openssl/err.h>
82# include <openssl/bn.h>
83# include <openssl/ec.h>
84# include <openssl/x509.h>
85# include <openssl/pem.h>
86
29# include <stdio.h>
30# include <stdlib.h>
31# include <time.h>
32# include <string.h>
33# include "apps.h"
34# include <openssl/bio.h>
35# include <openssl/err.h>
36# include <openssl/bn.h>
37# include <openssl/ec.h>
38# include <openssl/x509.h>
39# include <openssl/pem.h>
40
87# undef PROG
88# define PROG ecparam_main
41typedef enum OPTION_choice {
42 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
43 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT, OPT_C,
44 OPT_CHECK, OPT_LIST_CURVES, OPT_NO_SEED, OPT_NOOUT, OPT_NAME,
45 OPT_CONV_FORM, OPT_PARAM_ENC, OPT_GENKEY, OPT_RAND, OPT_ENGINE
46} OPTION_CHOICE;
89
47
90/*-
91 * -inform arg - input format - default PEM (DER or PEM)
92 * -outform arg - output format - default PEM
93 * -in arg - input file - default stdin
94 * -out arg - output file - default stdout
95 * -noout - do not print the ec parameter
96 * -text - print the ec parameters in text form
97 * -check - validate the ec parameters
98 * -C - print a 'C' function creating the parameters
99 * -name arg - use the ec parameters with 'short name' name
100 * -list_curves - prints a list of all currently available curve 'short names'
101 * -conv_form arg - specifies the point conversion form
102 * - possible values: compressed
103 * uncompressed (default)
104 * hybrid
105 * -param_enc arg - specifies the way the ec parameters are encoded
106 * in the asn1 der encoding
107 * possible values: named_curve (default)
108 * explicit
109 * -no_seed - if 'explicit' parameters are chosen do not use the seed
110 * -genkey - generate ec key
111 * -rand file - files to use for random number input
112 * -engine e - use engine e, possibly a hardware device
113 */
48OPTIONS ecparam_options[] = {
49 {"help", OPT_HELP, '-', "Display this summary"},
50 {"inform", OPT_INFORM, 'F', "Input format - default PEM (DER or PEM)"},
51 {"outform", OPT_OUTFORM, 'F', "Output format - default PEM"},
52 {"in", OPT_IN, '<', "Input file - default stdin"},
53 {"out", OPT_OUT, '>', "Output file - default stdout"},
54 {"text", OPT_TEXT, '-', "Print the ec parameters in text form"},
55 {"C", OPT_C, '-', "Print a 'C' function creating the parameters"},
56 {"check", OPT_CHECK, '-', "Validate the ec parameters"},
57 {"list_curves", OPT_LIST_CURVES, '-',
58 "Prints a list of all curve 'short names'"},
59 {"no_seed", OPT_NO_SEED, '-',
60 "If 'explicit' parameters are chosen do not use the seed"},
61 {"noout", OPT_NOOUT, '-', "Do not print the ec parameter"},
62 {"name", OPT_NAME, 's',
63 "Use the ec parameters with specified 'short name'"},
64 {"conv_form", OPT_CONV_FORM, 's', "Specifies the point conversion form "},
65 {"param_enc", OPT_PARAM_ENC, 's',
66 "Specifies the way the ec parameters are encoded"},
67 {"genkey", OPT_GENKEY, '-', "Generate ec key"},
68 {"rand", OPT_RAND, 's', "Files to use for random number input"},
69# ifndef OPENSSL_NO_ENGINE
70 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
71# endif
72 {NULL}
73};
114
74
115static int ecparam_print_var(BIO *, BIGNUM *, const char *, int,
116 unsigned char *);
75static OPT_PAIR forms[] = {
76 {"compressed", POINT_CONVERSION_COMPRESSED},
77 {"uncompressed", POINT_CONVERSION_UNCOMPRESSED},
78 {"hybrid", POINT_CONVERSION_HYBRID},
79 {NULL}
80};
117
81
118int MAIN(int, char **);
82static OPT_PAIR encodings[] = {
83 {"named_curve", OPENSSL_EC_NAMED_CURVE},
84 {"explicit", 0},
85 {NULL}
86};
119
87
120int MAIN(int argc, char **argv)
88int ecparam_main(int argc, char **argv)
121{
89{
90 ENGINE *e = NULL;
91 BIGNUM *ec_gen = NULL, *ec_order = NULL, *ec_cofactor = NULL;
92 BIGNUM *ec_p = NULL, *ec_a = NULL, *ec_b = NULL;
93 BIO *in = NULL, *out = NULL;
122 EC_GROUP *group = NULL;
123 point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
94 EC_GROUP *group = NULL;
95 point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
124 int new_form = 0;
125 int asn1_flag = OPENSSL_EC_NAMED_CURVE;
126 int new_asn1_flag = 0;
127 char *curve_name = NULL, *inrand = NULL;
96 char *curve_name = NULL, *inrand = NULL;
128 int list_curves = 0, no_seed = 0, check = 0,
129 badops = 0, text = 0, i, need_rand = 0, genkey = 0;
130 char *infile = NULL, *outfile = NULL, *prog;
97 char *infile = NULL, *outfile = NULL, *prog;
131 BIO *in = NULL, *out = NULL;
132 int informat, outformat, noout = 0, C = 0, ret = 1;
133 char *engine = NULL;
134 ENGINE *e = NULL;
135
136 BIGNUM *ec_p = NULL, *ec_a = NULL, *ec_b = NULL,
137 *ec_gen = NULL, *ec_order = NULL, *ec_cofactor = NULL;
138 unsigned char *buffer = NULL;
98 unsigned char *buffer = NULL;
99 OPTION_CHOICE o;
100 int asn1_flag = OPENSSL_EC_NAMED_CURVE, new_asn1_flag = 0;
101 int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0;
102 int ret = 1, private = 0;
103 int list_curves = 0, no_seed = 0, check = 0, new_form = 0;
104 int text = 0, i, need_rand = 0, genkey = 0;
139
105
140 apps_startup();
141
142 if (bio_err == NULL)
143 if ((bio_err = BIO_new(BIO_s_file())) != NULL)
144 BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
145
146 if (!load_config(bio_err, NULL))
147 goto end;
148
149 informat = FORMAT_PEM;
150 outformat = FORMAT_PEM;
151
152 prog = argv[0];
153 argc--;
154 argv++;
155 while (argc >= 1) {
156 if (strcmp(*argv, "-inform") == 0) {
157 if (--argc < 1)
158 goto bad;
159 informat = str2fmt(*(++argv));
160 } else if (strcmp(*argv, "-outform") == 0) {
161 if (--argc < 1)
162 goto bad;
163 outformat = str2fmt(*(++argv));
164 } else if (strcmp(*argv, "-in") == 0) {
165 if (--argc < 1)
166 goto bad;
167 infile = *(++argv);
168 } else if (strcmp(*argv, "-out") == 0) {
169 if (--argc < 1)
170 goto bad;
171 outfile = *(++argv);
172 } else if (strcmp(*argv, "-text") == 0)
106 prog = opt_init(argc, argv, ecparam_options);
107 while ((o = opt_next()) != OPT_EOF) {
108 switch (o) {
109 case OPT_EOF:
110 case OPT_ERR:
111 opthelp:
112 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
113 goto end;
114 case OPT_HELP:
115 opt_help(ecparam_options);
116 ret = 0;
117 goto end;
118 case OPT_INFORM:
119 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
120 goto opthelp;
121 break;
122 case OPT_IN:
123 infile = opt_arg();
124 break;
125 case OPT_OUTFORM:
126 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
127 goto opthelp;
128 break;
129 case OPT_OUT:
130 outfile = opt_arg();
131 break;
132 case OPT_TEXT:
173 text = 1;
133 text = 1;
174 else if (strcmp(*argv, "-C") == 0)
134 break;
135 case OPT_C:
175 C = 1;
136 C = 1;
176 else if (strcmp(*argv, "-check") == 0)
137 break;
138 case OPT_CHECK:
177 check = 1;
139 check = 1;
178 else if (strcmp(*argv, "-name") == 0) {
179 if (--argc < 1)
180 goto bad;
181 curve_name = *(++argv);
182 } else if (strcmp(*argv, "-list_curves") == 0)
140 break;
141 case OPT_LIST_CURVES:
183 list_curves = 1;
142 list_curves = 1;
184 else if (strcmp(*argv, "-conv_form") == 0) {
185 if (--argc < 1)
186 goto bad;
187 ++argv;
188 new_form = 1;
189 if (strcmp(*argv, "compressed") == 0)
190 form = POINT_CONVERSION_COMPRESSED;
191 else if (strcmp(*argv, "uncompressed") == 0)
192 form = POINT_CONVERSION_UNCOMPRESSED;
193 else if (strcmp(*argv, "hybrid") == 0)
194 form = POINT_CONVERSION_HYBRID;
195 else
196 goto bad;
197 } else if (strcmp(*argv, "-param_enc") == 0) {
198 if (--argc < 1)
199 goto bad;
200 ++argv;
201 new_asn1_flag = 1;
202 if (strcmp(*argv, "named_curve") == 0)
203 asn1_flag = OPENSSL_EC_NAMED_CURVE;
204 else if (strcmp(*argv, "explicit") == 0)
205 asn1_flag = 0;
206 else
207 goto bad;
208 } else if (strcmp(*argv, "-no_seed") == 0)
143 break;
144 case OPT_NO_SEED:
209 no_seed = 1;
145 no_seed = 1;
210 else if (strcmp(*argv, "-noout") == 0)
146 break;
147 case OPT_NOOUT:
211 noout = 1;
148 noout = 1;
212 else if (strcmp(*argv, "-genkey") == 0) {
213 genkey = 1;
149 break;
150 case OPT_NAME:
151 curve_name = opt_arg();
152 break;
153 case OPT_CONV_FORM:
154 if (!opt_pair(opt_arg(), forms, &new_form))
155 goto opthelp;
156 form = new_form;
157 new_form = 1;
158 break;
159 case OPT_PARAM_ENC:
160 if (!opt_pair(opt_arg(), encodings, &asn1_flag))
161 goto opthelp;
162 new_asn1_flag = 1;
163 break;
164 case OPT_GENKEY:
165 genkey = need_rand = 1;
166 break;
167 case OPT_RAND:
168 inrand = opt_arg();
214 need_rand = 1;
169 need_rand = 1;
215 } else if (strcmp(*argv, "-rand") == 0) {
216 if (--argc < 1)
217 goto bad;
218 inrand = *(++argv);
219 need_rand = 1;
220 } else if (strcmp(*argv, "-engine") == 0) {
221 if (--argc < 1)
222 goto bad;
223 engine = *(++argv);
224 } else {
225 BIO_printf(bio_err, "unknown option %s\n", *argv);
226 badops = 1;
227 break;
170 break;
171 case OPT_ENGINE:
172 e = setup_engine(opt_arg(), 0);
173 break;
228 }
174 }
229 argc--;
230 argv++;
231 }
175 }
176 argc = opt_num_rest();
177 if (argc != 0)
178 goto opthelp;
232
179
233 if (badops) {
234 bad:
235 BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog);
236 BIO_printf(bio_err, "where options are\n");
237 BIO_printf(bio_err, " -inform arg input format - "
238 "default PEM (DER or PEM)\n");
239 BIO_printf(bio_err, " -outform arg output format - "
240 "default PEM\n");
241 BIO_printf(bio_err, " -in arg input file - "
242 "default stdin\n");
243 BIO_printf(bio_err, " -out arg output file - "
244 "default stdout\n");
245 BIO_printf(bio_err, " -noout do not print the "
246 "ec parameter\n");
247 BIO_printf(bio_err, " -text print the ec "
248 "parameters in text form\n");
249 BIO_printf(bio_err, " -check validate the ec "
250 "parameters\n");
251 BIO_printf(bio_err, " -C print a 'C' "
252 "function creating the parameters\n");
253 BIO_printf(bio_err, " -name arg use the "
254 "ec parameters with 'short name' name\n");
255 BIO_printf(bio_err, " -list_curves prints a list of "
256 "all currently available curve 'short names'\n");
257 BIO_printf(bio_err, " -conv_form arg specifies the "
258 "point conversion form \n");
259 BIO_printf(bio_err, " possible values:"
260 " compressed\n");
261 BIO_printf(bio_err, " "
262 " uncompressed (default)\n");
263 BIO_printf(bio_err, " "
264 " hybrid\n");
265 BIO_printf(bio_err, " -param_enc arg specifies the way"
266 " the ec parameters are encoded\n");
267 BIO_printf(bio_err, " in the asn1 der "
268 "encoding\n");
269 BIO_printf(bio_err, " possible values:"
270 " named_curve (default)\n");
271 BIO_printf(bio_err, " "
272 " explicit\n");
273 BIO_printf(bio_err, " -no_seed if 'explicit'"
274 " parameters are chosen do not" " use the seed\n");
275 BIO_printf(bio_err, " -genkey generate ec" " key\n");
276 BIO_printf(bio_err, " -rand file files to use for"
277 " random number input\n");
278 BIO_printf(bio_err, " -engine e use engine e, "
279 "possibly a hardware device\n");
280 goto end;
281 }
180 private = genkey ? 1 : 0;
282
181
283 ERR_load_crypto_strings();
284
285 in = BIO_new(BIO_s_file());
286 out = BIO_new(BIO_s_file());
287 if ((in == NULL) || (out == NULL)) {
288 ERR_print_errors(bio_err);
182 in = bio_open_default(infile, 'r', informat);
183 if (in == NULL)
289 goto end;
184 goto end;
290 }
185 out = bio_open_owner(outfile, outformat, private);
186 if (out == NULL)
187 goto end;
291
188
292 if (infile == NULL)
293 BIO_set_fp(in, stdin, BIO_NOCLOSE);
294 else {
295 if (BIO_read_filename(in, infile) <= 0) {
296 perror(infile);
297 goto end;
298 }
299 }
300 if (outfile == NULL) {
301 BIO_set_fp(out, stdout, BIO_NOCLOSE);
302# ifdef OPENSSL_SYS_VMS
303 {
304 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
305 out = BIO_push(tmpbio, out);
306 }
307# endif
308 } else {
309 if (BIO_write_filename(out, outfile) <= 0) {
310 perror(outfile);
311 goto end;
312 }
313 }
314
315 e = setup_engine(bio_err, engine, 0);
316
317 if (list_curves) {
318 EC_builtin_curve *curves = NULL;
189 if (list_curves) {
190 EC_builtin_curve *curves = NULL;
319 size_t crv_len = 0;
320 size_t n = 0;
191 size_t crv_len = EC_get_builtin_curves(NULL, 0);
192 size_t n;
321
193
322 crv_len = EC_get_builtin_curves(NULL, 0);
323
324 curves = OPENSSL_malloc((int)(sizeof(EC_builtin_curve) * crv_len));
325
326 if (curves == NULL)
327 goto end;
328
194 curves = app_malloc((int)sizeof(*curves) * crv_len, "list curves");
329 if (!EC_get_builtin_curves(curves, crv_len)) {
330 OPENSSL_free(curves);
331 goto end;
332 }
333
334 for (n = 0; n < crv_len; n++) {
335 const char *comment;
336 const char *sname;

--- 16 unchanged lines hidden (view full) ---

353 if (curve_name != NULL) {
354 int nid;
355
356 /*
357 * workaround for the SECG curve names secp192r1 and secp256r1 (which
358 * are the same as the curves prime192v1 and prime256v1 defined in
359 * X9.62)
360 */
195 if (!EC_get_builtin_curves(curves, crv_len)) {
196 OPENSSL_free(curves);
197 goto end;
198 }
199
200 for (n = 0; n < crv_len; n++) {
201 const char *comment;
202 const char *sname;

--- 16 unchanged lines hidden (view full) ---

219 if (curve_name != NULL) {
220 int nid;
221
222 /*
223 * workaround for the SECG curve names secp192r1 and secp256r1 (which
224 * are the same as the curves prime192v1 and prime256v1 defined in
225 * X9.62)
226 */
361 if (!strcmp(curve_name, "secp192r1")) {
227 if (strcmp(curve_name, "secp192r1") == 0) {
362 BIO_printf(bio_err, "using curve name prime192v1 "
363 "instead of secp192r1\n");
364 nid = NID_X9_62_prime192v1;
228 BIO_printf(bio_err, "using curve name prime192v1 "
229 "instead of secp192r1\n");
230 nid = NID_X9_62_prime192v1;
365 } else if (!strcmp(curve_name, "secp256r1")) {
231 } else if (strcmp(curve_name, "secp256r1") == 0) {
366 BIO_printf(bio_err, "using curve name prime256v1 "
367 "instead of secp256r1\n");
368 nid = NID_X9_62_prime256v1;
369 } else
370 nid = OBJ_sn2nid(curve_name);
371
372 if (nid == 0)
373 nid = EC_curve_nist2nid(curve_name);

--- 5 unchanged lines hidden (view full) ---

379
380 group = EC_GROUP_new_by_curve_name(nid);
381 if (group == NULL) {
382 BIO_printf(bio_err, "unable to create curve (%s)\n", curve_name);
383 goto end;
384 }
385 EC_GROUP_set_asn1_flag(group, asn1_flag);
386 EC_GROUP_set_point_conversion_form(group, form);
232 BIO_printf(bio_err, "using curve name prime256v1 "
233 "instead of secp256r1\n");
234 nid = NID_X9_62_prime256v1;
235 } else
236 nid = OBJ_sn2nid(curve_name);
237
238 if (nid == 0)
239 nid = EC_curve_nist2nid(curve_name);

--- 5 unchanged lines hidden (view full) ---

245
246 group = EC_GROUP_new_by_curve_name(nid);
247 if (group == NULL) {
248 BIO_printf(bio_err, "unable to create curve (%s)\n", curve_name);
249 goto end;
250 }
251 EC_GROUP_set_asn1_flag(group, asn1_flag);
252 EC_GROUP_set_point_conversion_form(group, form);
387 } else if (informat == FORMAT_ASN1) {
253 } else if (informat == FORMAT_ASN1)
388 group = d2i_ECPKParameters_bio(in, NULL);
254 group = d2i_ECPKParameters_bio(in, NULL);
389 } else if (informat == FORMAT_PEM) {
255 else
390 group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL);
256 group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL);
391 } else {
392 BIO_printf(bio_err, "bad input format specified\n");
393 goto end;
394 }
395
396 if (group == NULL) {
397 BIO_printf(bio_err, "unable to load elliptic curve parameters\n");
398 ERR_print_errors(bio_err);
399 goto end;
400 }
401
402 if (new_form)
403 EC_GROUP_set_point_conversion_form(group, form);

--- 22 unchanged lines hidden (view full) ---

426 }
427
428 if (C) {
429 size_t buf_len = 0, tmp_len = 0;
430 const EC_POINT *point;
431 int is_prime, len = 0;
432 const EC_METHOD *meth = EC_GROUP_method_of(group);
433
257 if (group == NULL) {
258 BIO_printf(bio_err, "unable to load elliptic curve parameters\n");
259 ERR_print_errors(bio_err);
260 goto end;
261 }
262
263 if (new_form)
264 EC_GROUP_set_point_conversion_form(group, form);

--- 22 unchanged lines hidden (view full) ---

287 }
288
289 if (C) {
290 size_t buf_len = 0, tmp_len = 0;
291 const EC_POINT *point;
292 int is_prime, len = 0;
293 const EC_METHOD *meth = EC_GROUP_method_of(group);
294
434 if ((ec_p = BN_new()) == NULL || (ec_a = BN_new()) == NULL ||
435 (ec_b = BN_new()) == NULL || (ec_gen = BN_new()) == NULL ||
436 (ec_order = BN_new()) == NULL ||
437 (ec_cofactor = BN_new()) == NULL) {
438 perror("OPENSSL_malloc");
295 if ((ec_p = BN_new()) == NULL
296 || (ec_a = BN_new()) == NULL
297 || (ec_b = BN_new()) == NULL
298 || (ec_gen = BN_new()) == NULL
299 || (ec_order = BN_new()) == NULL
300 || (ec_cofactor = BN_new()) == NULL) {
301 perror("Can't allocate BN");
439 goto end;
440 }
441
442 is_prime = (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field);
302 goto end;
303 }
304
305 is_prime = (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field);
443
444 if (is_prime) {
445 if (!EC_GROUP_get_curve_GFp(group, ec_p, ec_a, ec_b, NULL))
446 goto end;
447 } else {
448 /* TODO */
306 if (!is_prime) {
307 BIO_printf(bio_err, "Can only handle X9.62 prime fields\n");
449 goto end;
450 }
451
308 goto end;
309 }
310
311 if (!EC_GROUP_get_curve_GFp(group, ec_p, ec_a, ec_b, NULL))
312 goto end;
313
452 if ((point = EC_GROUP_get0_generator(group)) == NULL)
453 goto end;
454 if (!EC_POINT_point2bn(group, point,
455 EC_GROUP_get_point_conversion_form(group),
456 ec_gen, NULL))
457 goto end;
458 if (!EC_GROUP_get_order(group, ec_order, NULL))
459 goto end;

--- 13 unchanged lines hidden (view full) ---

473 buf_len = tmp_len;
474 if ((tmp_len = (size_t)BN_num_bytes(ec_gen)) > buf_len)
475 buf_len = tmp_len;
476 if ((tmp_len = (size_t)BN_num_bytes(ec_order)) > buf_len)
477 buf_len = tmp_len;
478 if ((tmp_len = (size_t)BN_num_bytes(ec_cofactor)) > buf_len)
479 buf_len = tmp_len;
480
314 if ((point = EC_GROUP_get0_generator(group)) == NULL)
315 goto end;
316 if (!EC_POINT_point2bn(group, point,
317 EC_GROUP_get_point_conversion_form(group),
318 ec_gen, NULL))
319 goto end;
320 if (!EC_GROUP_get_order(group, ec_order, NULL))
321 goto end;

--- 13 unchanged lines hidden (view full) ---

335 buf_len = tmp_len;
336 if ((tmp_len = (size_t)BN_num_bytes(ec_gen)) > buf_len)
337 buf_len = tmp_len;
338 if ((tmp_len = (size_t)BN_num_bytes(ec_order)) > buf_len)
339 buf_len = tmp_len;
340 if ((tmp_len = (size_t)BN_num_bytes(ec_cofactor)) > buf_len)
341 buf_len = tmp_len;
342
481 buffer = (unsigned char *)OPENSSL_malloc(buf_len);
343 buffer = app_malloc(buf_len, "BN buffer");
482
344
483 if (buffer == NULL) {
484 perror("OPENSSL_malloc");
485 goto end;
486 }
345 BIO_printf(out, "EC_GROUP *get_ec_group_%d(void)\n{\n", len);
346 print_bignum_var(out, ec_p, "ec_p", len, buffer);
347 print_bignum_var(out, ec_a, "ec_a", len, buffer);
348 print_bignum_var(out, ec_b, "ec_b", len, buffer);
349 print_bignum_var(out, ec_gen, "ec_gen", len, buffer);
350 print_bignum_var(out, ec_order, "ec_order", len, buffer);
351 print_bignum_var(out, ec_cofactor, "ec_cofactor", len, buffer);
352 BIO_printf(out, " int ok = 0;\n"
353 " EC_GROUP *group = NULL;\n"
354 " EC_POINT *point = NULL;\n"
355 " BIGNUM *tmp_1 = NULL;\n"
356 " BIGNUM *tmp_2 = NULL;\n"
357 " BIGNUM *tmp_3 = NULL;\n"
358 "\n");
487
359
488 ecparam_print_var(out, ec_p, "ec_p", len, buffer);
489 ecparam_print_var(out, ec_a, "ec_a", len, buffer);
490 ecparam_print_var(out, ec_b, "ec_b", len, buffer);
491 ecparam_print_var(out, ec_gen, "ec_gen", len, buffer);
492 ecparam_print_var(out, ec_order, "ec_order", len, buffer);
493 ecparam_print_var(out, ec_cofactor, "ec_cofactor", len, buffer);
494
495 BIO_printf(out, "\n\n");
496
497 BIO_printf(out, "EC_GROUP *get_ec_group_%d(void)\n\t{\n", len);
498 BIO_printf(out, "\tint ok=0;\n");
499 BIO_printf(out, "\tEC_GROUP *group = NULL;\n");
500 BIO_printf(out, "\tEC_POINT *point = NULL;\n");
501 BIO_printf(out, "\tBIGNUM *tmp_1 = NULL, *tmp_2 = NULL, "
502 "*tmp_3 = NULL;\n\n");
503 BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_p_%d, "
504 "sizeof(ec_p_%d), NULL)) == NULL)\n\t\t"
505 "goto err;\n", len, len);
506 BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_a_%d, "
507 "sizeof(ec_a_%d), NULL)) == NULL)\n\t\t"
508 "goto err;\n", len, len);
509 BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_b_%d, "
510 "sizeof(ec_b_%d), NULL)) == NULL)\n\t\t"
511 "goto err;\n", len, len);
512 if (is_prime) {
513 BIO_printf(out, "\tif ((group = EC_GROUP_new_curve_"
514 "GFp(tmp_1, tmp_2, tmp_3, NULL)) == NULL)"
515 "\n\t\tgoto err;\n\n");
516 } else {
517 /* TODO */
518 goto end;
519 }
520 BIO_printf(out, "\t/* build generator */\n");
521 BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_gen_%d, "
522 "sizeof(ec_gen_%d), tmp_1)) == NULL)"
523 "\n\t\tgoto err;\n", len, len);
524 BIO_printf(out, "\tpoint = EC_POINT_bn2point(group, tmp_1, "
525 "NULL, NULL);\n");
526 BIO_printf(out, "\tif (point == NULL)\n\t\tgoto err;\n");
527 BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_order_%d, "
528 "sizeof(ec_order_%d), tmp_2)) == NULL)"
529 "\n\t\tgoto err;\n", len, len);
530 BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_cofactor_%d, "
531 "sizeof(ec_cofactor_%d), tmp_3)) == NULL)"
532 "\n\t\tgoto err;\n", len, len);
533 BIO_printf(out, "\tif (!EC_GROUP_set_generator(group, point,"
534 " tmp_2, tmp_3))\n\t\tgoto err;\n");
535 BIO_printf(out, "\n\tok=1;\n");
536 BIO_printf(out, "err:\n");
537 BIO_printf(out, "\tif (tmp_1)\n\t\tBN_free(tmp_1);\n");
538 BIO_printf(out, "\tif (tmp_2)\n\t\tBN_free(tmp_2);\n");
539 BIO_printf(out, "\tif (tmp_3)\n\t\tBN_free(tmp_3);\n");
540 BIO_printf(out, "\tif (point)\n\t\tEC_POINT_free(point);\n");
541 BIO_printf(out, "\tif (!ok)\n");
542 BIO_printf(out, "\t\t{\n");
543 BIO_printf(out, "\t\tEC_GROUP_free(group);\n");
544 BIO_printf(out, "\t\tgroup = NULL;\n");
545 BIO_printf(out, "\t\t}\n");
546 BIO_printf(out, "\treturn(group);\n\t}\n");
360 BIO_printf(out, " if ((tmp_1 = BN_bin2bn(ec_p_%d, sizeof (ec_p_%d), NULL)) == NULL)\n"
361 " goto err;\n", len, len);
362 BIO_printf(out, " if ((tmp_2 = BN_bin2bn(ec_a_%d, sizeof (ec_a_%d), NULL)) == NULL)\n"
363 " goto err;\n", len, len);
364 BIO_printf(out, " if ((tmp_3 = BN_bin2bn(ec_b_%d, sizeof (ec_b_%d), NULL)) == NULL)\n"
365 " goto err;\n", len, len);
366 BIO_printf(out, " if ((group = EC_GROUP_new_curve_GFp(tmp_1, tmp_2, tmp_3, NULL)) == NULL)\n"
367 " goto err;\n"
368 "\n");
369 BIO_printf(out, " /* build generator */\n");
370 BIO_printf(out, " if ((tmp_1 = BN_bin2bn(ec_gen_%d, sizeof (ec_gen_%d), tmp_1)) == NULL)\n"
371 " goto err;\n", len, len);
372 BIO_printf(out, " point = EC_POINT_bn2point(group, tmp_1, NULL, NULL);\n");
373 BIO_printf(out, " if (point == NULL)\n"
374 " goto err;\n");
375 BIO_printf(out, " if ((tmp_2 = BN_bin2bn(ec_order_%d, sizeof (ec_order_%d), tmp_2)) == NULL)\n"
376 " goto err;\n", len, len);
377 BIO_printf(out, " if ((tmp_3 = BN_bin2bn(ec_cofactor_%d, sizeof (ec_cofactor_%d), tmp_3)) == NULL)\n"
378 " goto err;\n", len, len);
379 BIO_printf(out, " if (!EC_GROUP_set_generator(group, point, tmp_2, tmp_3))\n"
380 " goto err;\n"
381 "ok = 1;"
382 "\n");
383 BIO_printf(out, "err:\n"
384 " BN_free(tmp_1);\n"
385 " BN_free(tmp_2);\n"
386 " BN_free(tmp_3);\n"
387 " EC_POINT_free(point);\n"
388 " if (!ok) {\n"
389 " EC_GROUP_free(group);\n"
390 " return NULL;\n"
391 " }\n"
392 " return (group);\n"
393 "}\n");
547 }
548
549 if (!noout) {
550 if (outformat == FORMAT_ASN1)
551 i = i2d_ECPKParameters_bio(out, group);
394 }
395
396 if (!noout) {
397 if (outformat == FORMAT_ASN1)
398 i = i2d_ECPKParameters_bio(out, group);
552 else if (outformat == FORMAT_PEM)
399 else
553 i = PEM_write_bio_ECPKParameters(out, group);
400 i = PEM_write_bio_ECPKParameters(out, group);
554 else {
555 BIO_printf(bio_err, "bad output format specified for"
556 " outfile\n");
557 goto end;
558 }
559 if (!i) {
560 BIO_printf(bio_err, "unable to write elliptic "
561 "curve parameters\n");
562 ERR_print_errors(bio_err);
563 goto end;
564 }
565 }
566
567 if (need_rand) {
401 if (!i) {
402 BIO_printf(bio_err, "unable to write elliptic "
403 "curve parameters\n");
404 ERR_print_errors(bio_err);
405 goto end;
406 }
407 }
408
409 if (need_rand) {
568 app_RAND_load_file(NULL, bio_err, (inrand != NULL));
410 app_RAND_load_file(NULL, (inrand != NULL));
569 if (inrand != NULL)
570 BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
571 app_RAND_load_files(inrand));
572 }
573
574 if (genkey) {
575 EC_KEY *eckey = EC_KEY_new();
576
577 if (eckey == NULL)
578 goto end;
579
580 assert(need_rand);
581
411 if (inrand != NULL)
412 BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
413 app_RAND_load_files(inrand));
414 }
415
416 if (genkey) {
417 EC_KEY *eckey = EC_KEY_new();
418
419 if (eckey == NULL)
420 goto end;
421
422 assert(need_rand);
423
582 if (EC_KEY_set_group(eckey, group) == 0)
424 if (EC_KEY_set_group(eckey, group) == 0) {
425 BIO_printf(bio_err, "unable to set group when generating key\n");
426 EC_KEY_free(eckey);
427 ERR_print_errors(bio_err);
583 goto end;
428 goto end;
429 }
584
585 if (!EC_KEY_generate_key(eckey)) {
430
431 if (!EC_KEY_generate_key(eckey)) {
432 BIO_printf(bio_err, "unable to generate key\n");
586 EC_KEY_free(eckey);
433 EC_KEY_free(eckey);
434 ERR_print_errors(bio_err);
587 goto end;
588 }
435 goto end;
436 }
437 assert(private);
589 if (outformat == FORMAT_ASN1)
590 i = i2d_ECPrivateKey_bio(out, eckey);
438 if (outformat == FORMAT_ASN1)
439 i = i2d_ECPrivateKey_bio(out, eckey);
591 else if (outformat == FORMAT_PEM)
440 else
592 i = PEM_write_bio_ECPrivateKey(out, eckey, NULL,
593 NULL, 0, NULL, NULL);
441 i = PEM_write_bio_ECPrivateKey(out, eckey, NULL,
442 NULL, 0, NULL, NULL);
594 else {
595 BIO_printf(bio_err, "bad output format specified "
596 "for outfile\n");
597 EC_KEY_free(eckey);
598 goto end;
599 }
600 EC_KEY_free(eckey);
601 }
602
603 if (need_rand)
443 EC_KEY_free(eckey);
444 }
445
446 if (need_rand)
604 app_RAND_write_file(NULL, bio_err);
447 app_RAND_write_file(NULL);
605
606 ret = 0;
607 end:
448
449 ret = 0;
450 end:
608 if (ec_p)
609 BN_free(ec_p);
610 if (ec_a)
611 BN_free(ec_a);
612 if (ec_b)
613 BN_free(ec_b);
614 if (ec_gen)
615 BN_free(ec_gen);
616 if (ec_order)
617 BN_free(ec_order);
618 if (ec_cofactor)
619 BN_free(ec_cofactor);
620 if (buffer)
621 OPENSSL_free(buffer);
622 if (group != NULL)
623 EC_GROUP_free(group);
451 BN_free(ec_p);
452 BN_free(ec_a);
453 BN_free(ec_b);
454 BN_free(ec_gen);
455 BN_free(ec_order);
456 BN_free(ec_cofactor);
457 OPENSSL_free(buffer);
458 EC_GROUP_free(group);
624 release_engine(e);
459 release_engine(e);
625 if (in != NULL)
626 BIO_free(in);
627 if (out != NULL)
628 BIO_free_all(out);
629 apps_shutdown();
630 OPENSSL_EXIT(ret);
460 BIO_free(in);
461 BIO_free_all(out);
462 return (ret);
631}
632
463}
464
633static int ecparam_print_var(BIO *out, BIGNUM *in, const char *var,
634 int len, unsigned char *buffer)
635{
636 BIO_printf(out, "static unsigned char %s_%d[] = {", var, len);
637 if (BN_is_zero(in))
638 BIO_printf(out, "\n\t0x00");
639 else {
640 int i, l;
641
642 l = BN_bn2bin(in, buffer);
643 for (i = 0; i < l - 1; i++) {
644 if ((i % 12) == 0)
645 BIO_printf(out, "\n\t");
646 BIO_printf(out, "0x%02X,", buffer[i]);
647 }
648 if ((i % 12) == 0)
649 BIO_printf(out, "\n\t");
650 BIO_printf(out, "0x%02X", buffer[i]);
651 }
652 BIO_printf(out, "\n\t};\n\n");
653 return 1;
654}
655#else /* !OPENSSL_NO_EC */
656
657# if PEDANTIC
658static void *dummy = &dummy;
659# endif
660
661#endif
465#endif