]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/ecparam.c
Add a comment to explain the use of |num_recs|
[thirdparty/openssl.git] / apps / ecparam.c
CommitLineData
14a7cfb3 1/*
7eb18f12 2 * Written by Nils Larsch for the OpenSSL project.
14a7cfb3 3 */
5dbd3efc 4/* ====================================================================
9dd84053 5 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
5dbd3efc
BM
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
0f113f3e 12 * notice, this list of conditions and the following disclaimer.
5dbd3efc
BM
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
23 *
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * openssl-core@openssl.org.
28 *
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
32 *
33 * 6. Redistributions of any form whatsoever must retain the following
34 * acknowledgment:
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
37 *
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
51 *
52 * This product includes cryptographic software written by Eric Young
53 * (eay@cryptsoft.com). This product includes software written by Tim
54 * Hudson (tjh@cryptsoft.com).
55 *
56 */
714df32e
BM
57/* ====================================================================
58 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
59 *
0f113f3e 60 * Portions of the attached software ("Contribution") are developed by
714df32e
BM
61 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
62 *
63 * The Contribution is licensed pursuant to the OpenSSL open source
64 * license provided above.
65 *
0f113f3e 66 * The elliptic curve binary polynomial software is originally written by
714df32e
BM
67 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
68 *
69 */
6e04afb8
NL
70
71#include <openssl/opensslconf.h>
effaf4de
RS
72#ifdef OPENSSL_NO_EC
73NON_EMPTY_TRANSLATION_UNIT
74#else
75
0f113f3e
MC
76# include <stdio.h>
77# include <stdlib.h>
78# include <time.h>
79# include <string.h>
80# include "apps.h"
81# include <openssl/bio.h>
82# include <openssl/err.h>
83# include <openssl/bn.h>
84# include <openssl/ec.h>
85# include <openssl/x509.h>
86# include <openssl/pem.h>
87
7e1b7485
RS
88typedef enum OPTION_choice {
89 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
90 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT, OPT_C,
91 OPT_CHECK, OPT_LIST_CURVES, OPT_NO_SEED, OPT_NOOUT, OPT_NAME,
92 OPT_CONV_FORM, OPT_PARAM_ENC, OPT_GENKEY, OPT_RAND, OPT_ENGINE
93} OPTION_CHOICE;
94
95OPTIONS ecparam_options[] = {
96 {"help", OPT_HELP, '-', "Display this summary"},
97 {"inform", OPT_INFORM, 'F', "Input format - default PEM (DER or PEM)"},
98 {"outform", OPT_OUTFORM, 'F', "Output format - default PEM"},
99 {"in", OPT_IN, '<', "Input file - default stdin"},
100 {"out", OPT_OUT, '>', "Output file - default stdout"},
101 {"text", OPT_TEXT, '-', "Print the ec parameters in text form"},
102 {"C", OPT_C, '-', "Print a 'C' function creating the parameters"},
103 {"check", OPT_CHECK, '-', "Validate the ec parameters"},
104 {"list_curves", OPT_LIST_CURVES, '-',
105 "Prints a list of all curve 'short names'"},
106 {"no_seed", OPT_NO_SEED, '-',
107 "If 'explicit' parameters are chosen do not use the seed"},
108 {"noout", OPT_NOOUT, '-', "Do not print the ec parameter"},
109 {"name", OPT_NAME, 's',
110 "Use the ec parameters with specified 'short name'"},
111 {"conv_form", OPT_CONV_FORM, 's', "Specifies the point conversion form "},
112 {"param_enc", OPT_PARAM_ENC, 's',
113 "Specifies the way the ec parameters are encoded"},
114 {"genkey", OPT_GENKEY, '-', "Generate ec key"},
115 {"rand", OPT_RAND, 's', "Files to use for random number input"},
116# ifndef OPENSSL_NO_ENGINE
117 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
118# endif
119 {NULL}
120};
121
df2ee0e2 122static OPT_PAIR forms[] = {
7e1b7485
RS
123 {"compressed", POINT_CONVERSION_COMPRESSED},
124 {"uncompressed", POINT_CONVERSION_UNCOMPRESSED},
125 {"hybrid", POINT_CONVERSION_HYBRID},
126 {NULL}
127};
128
df2ee0e2 129static OPT_PAIR encodings[] = {
7e1b7485
RS
130 {"named_curve", OPENSSL_EC_NAMED_CURVE},
131 {"explicit", 0},
132 {NULL}
133};
134
135int ecparam_main(int argc, char **argv)
0f113f3e 136{
7e1b7485
RS
137 BIGNUM *ec_gen = NULL, *ec_order = NULL, *ec_cofactor = NULL;
138 BIGNUM *ec_p = NULL, *ec_a = NULL, *ec_b = NULL;
139 BIO *in = NULL, *out = NULL;
0f113f3e
MC
140 EC_GROUP *group = NULL;
141 point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
0f113f3e 142 char *curve_name = NULL, *inrand = NULL;
333b070e 143 char *infile = NULL, *outfile = NULL, *prog;
0f113f3e 144 unsigned char *buffer = NULL;
7e1b7485
RS
145 OPTION_CHOICE o;
146 int asn1_flag = OPENSSL_EC_NAMED_CURVE, new_asn1_flag = 0;
3b061a00
RS
147 int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0;
148 int ret = 1, private = 0;
7e1b7485
RS
149 int list_curves = 0, no_seed = 0, check = 0, new_form = 0;
150 int text = 0, i, need_rand = 0, genkey = 0;
151
152 prog = opt_init(argc, argv, ecparam_options);
153 while ((o = opt_next()) != OPT_EOF) {
154 switch (o) {
155 case OPT_EOF:
156 case OPT_ERR:
157 opthelp:
158 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
159 goto end;
160 case OPT_HELP:
161 opt_help(ecparam_options);
162 ret = 0;
163 goto end;
164 case OPT_INFORM:
165 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
166 goto opthelp;
167 break;
168 case OPT_IN:
169 infile = opt_arg();
170 break;
171 case OPT_OUTFORM:
172 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
173 goto opthelp;
174 break;
175 case OPT_OUT:
176 outfile = opt_arg();
177 break;
178 case OPT_TEXT:
0f113f3e 179 text = 1;
7e1b7485
RS
180 break;
181 case OPT_C:
0f113f3e 182 C = 1;
7e1b7485
RS
183 break;
184 case OPT_CHECK:
0f113f3e 185 check = 1;
7e1b7485
RS
186 break;
187 case OPT_LIST_CURVES:
0f113f3e 188 list_curves = 1;
7e1b7485
RS
189 break;
190 case OPT_NO_SEED:
0f113f3e 191 no_seed = 1;
7e1b7485
RS
192 break;
193 case OPT_NOOUT:
0f113f3e 194 noout = 1;
7e1b7485
RS
195 break;
196 case OPT_NAME:
197 curve_name = opt_arg();
198 break;
199 case OPT_CONV_FORM:
200 if (!opt_pair(opt_arg(), forms, &new_form))
201 goto opthelp;
202 form = new_form;
203 new_form = 1;
204 break;
205 case OPT_PARAM_ENC:
206 if (!opt_pair(opt_arg(), encodings, &asn1_flag))
207 goto opthelp;
208 new_asn1_flag = 1;
209 break;
210 case OPT_GENKEY:
211 genkey = need_rand = 1;
212 break;
213 case OPT_RAND:
214 inrand = opt_arg();
0f113f3e 215 need_rand = 1;
7e1b7485
RS
216 break;
217 case OPT_ENGINE:
333b070e 218 (void)setup_engine(opt_arg(), 0);
0f113f3e
MC
219 break;
220 }
0f113f3e 221 }
7e1b7485 222 argc = opt_num_rest();
03358517
KR
223 if (argc != 0)
224 goto opthelp;
225
3b061a00 226 private = genkey ? 1 : 0;
0f113f3e 227
bdd58d98 228 in = bio_open_default(infile, 'r', informat);
7e1b7485 229 if (in == NULL)
0f113f3e 230 goto end;
bdd58d98 231 out = bio_open_owner(outfile, outformat, private);
7e1b7485 232 if (out == NULL)
0f113f3e 233 goto end;
0f113f3e 234
0f113f3e
MC
235 if (list_curves) {
236 EC_builtin_curve *curves = NULL;
68dc6824
RS
237 size_t crv_len = EC_get_builtin_curves(NULL, 0);
238 size_t n;
0f113f3e 239
b4faea50 240 curves = app_malloc((int)sizeof(*curves) * crv_len, "list curves");
0f113f3e
MC
241 if (!EC_get_builtin_curves(curves, crv_len)) {
242 OPENSSL_free(curves);
243 goto end;
244 }
245
246 for (n = 0; n < crv_len; n++) {
247 const char *comment;
248 const char *sname;
249 comment = curves[n].comment;
250 sname = OBJ_nid2sn(curves[n].nid);
251 if (comment == NULL)
252 comment = "CURVE DESCRIPTION NOT AVAILABLE";
253 if (sname == NULL)
254 sname = "";
255
256 BIO_printf(out, " %-10s: ", sname);
257 BIO_printf(out, "%s\n", comment);
258 }
259
260 OPENSSL_free(curves);
261 ret = 0;
262 goto end;
263 }
264
265 if (curve_name != NULL) {
266 int nid;
267
268 /*
269 * workaround for the SECG curve names secp192r1 and secp256r1 (which
270 * are the same as the curves prime192v1 and prime256v1 defined in
271 * X9.62)
272 */
86885c28 273 if (strcmp(curve_name, "secp192r1") == 0) {
0f113f3e
MC
274 BIO_printf(bio_err, "using curve name prime192v1 "
275 "instead of secp192r1\n");
276 nid = NID_X9_62_prime192v1;
86885c28 277 } else if (strcmp(curve_name, "secp256r1") == 0) {
0f113f3e
MC
278 BIO_printf(bio_err, "using curve name prime256v1 "
279 "instead of secp256r1\n");
280 nid = NID_X9_62_prime256v1;
281 } else
282 nid = OBJ_sn2nid(curve_name);
283
284 if (nid == 0)
285 nid = EC_curve_nist2nid(curve_name);
286
287 if (nid == 0) {
288 BIO_printf(bio_err, "unknown curve name (%s)\n", curve_name);
289 goto end;
290 }
291
292 group = EC_GROUP_new_by_curve_name(nid);
293 if (group == NULL) {
294 BIO_printf(bio_err, "unable to create curve (%s)\n", curve_name);
295 goto end;
296 }
297 EC_GROUP_set_asn1_flag(group, asn1_flag);
298 EC_GROUP_set_point_conversion_form(group, form);
7e1b7485 299 } else if (informat == FORMAT_ASN1)
0f113f3e 300 group = d2i_ECPKParameters_bio(in, NULL);
7e1b7485 301 else
0f113f3e 302 group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL);
0f113f3e
MC
303 if (group == NULL) {
304 BIO_printf(bio_err, "unable to load elliptic curve parameters\n");
305 ERR_print_errors(bio_err);
306 goto end;
307 }
308
309 if (new_form)
310 EC_GROUP_set_point_conversion_form(group, form);
311
312 if (new_asn1_flag)
313 EC_GROUP_set_asn1_flag(group, asn1_flag);
314
315 if (no_seed) {
316 EC_GROUP_set_seed(group, NULL, 0);
317 }
318
319 if (text) {
320 if (!ECPKParameters_print(out, group, 0))
321 goto end;
322 }
323
324 if (check) {
0f113f3e
MC
325 BIO_printf(bio_err, "checking elliptic curve parameters: ");
326 if (!EC_GROUP_check(group, NULL)) {
327 BIO_printf(bio_err, "failed\n");
328 ERR_print_errors(bio_err);
99dcd880
PW
329 goto end;
330 }
331 BIO_printf(bio_err, "ok\n");
0f113f3e
MC
332
333 }
334
335 if (C) {
336 size_t buf_len = 0, tmp_len = 0;
337 const EC_POINT *point;
338 int is_prime, len = 0;
339 const EC_METHOD *meth = EC_GROUP_method_of(group);
340
7e1b7485
RS
341 if ((ec_p = BN_new()) == NULL
342 || (ec_a = BN_new()) == NULL
343 || (ec_b = BN_new()) == NULL
344 || (ec_gen = BN_new()) == NULL
345 || (ec_order = BN_new()) == NULL
346 || (ec_cofactor = BN_new()) == NULL) {
68dc6824 347 perror("Can't allocate BN");
0f113f3e
MC
348 goto end;
349 }
350
351 is_prime = (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field);
7e1b7485
RS
352 if (!is_prime) {
353 BIO_printf(bio_err, "Can only handle X9.62 prime fields\n");
0f113f3e
MC
354 goto end;
355 }
356
7e1b7485
RS
357 if (!EC_GROUP_get_curve_GFp(group, ec_p, ec_a, ec_b, NULL))
358 goto end;
359
0f113f3e
MC
360 if ((point = EC_GROUP_get0_generator(group)) == NULL)
361 goto end;
362 if (!EC_POINT_point2bn(group, point,
363 EC_GROUP_get_point_conversion_form(group),
364 ec_gen, NULL))
365 goto end;
366 if (!EC_GROUP_get_order(group, ec_order, NULL))
367 goto end;
368 if (!EC_GROUP_get_cofactor(group, ec_cofactor, NULL))
369 goto end;
370
371 if (!ec_p || !ec_a || !ec_b || !ec_gen || !ec_order || !ec_cofactor)
372 goto end;
373
374 len = BN_num_bits(ec_order);
375
376 if ((tmp_len = (size_t)BN_num_bytes(ec_p)) > buf_len)
377 buf_len = tmp_len;
378 if ((tmp_len = (size_t)BN_num_bytes(ec_a)) > buf_len)
379 buf_len = tmp_len;
380 if ((tmp_len = (size_t)BN_num_bytes(ec_b)) > buf_len)
381 buf_len = tmp_len;
382 if ((tmp_len = (size_t)BN_num_bytes(ec_gen)) > buf_len)
383 buf_len = tmp_len;
384 if ((tmp_len = (size_t)BN_num_bytes(ec_order)) > buf_len)
385 buf_len = tmp_len;
386 if ((tmp_len = (size_t)BN_num_bytes(ec_cofactor)) > buf_len)
387 buf_len = tmp_len;
388
68dc6824 389 buffer = app_malloc(buf_len, "BN buffer");
0f113f3e 390
7e1b7485
RS
391 BIO_printf(out, "EC_GROUP *get_ec_group_%d(void)\n{\n", len);
392 print_bignum_var(out, ec_p, "ec_p", len, buffer);
393 print_bignum_var(out, ec_a, "ec_a", len, buffer);
394 print_bignum_var(out, ec_b, "ec_b", len, buffer);
395 print_bignum_var(out, ec_gen, "ec_gen", len, buffer);
396 print_bignum_var(out, ec_order, "ec_order", len, buffer);
397 print_bignum_var(out, ec_cofactor, "ec_cofactor", len, buffer);
398 BIO_printf(out, " int ok = 0;\n"
399 " EC_GROUP *group = NULL;\n"
400 " EC_POINT *point = NULL;\n"
401 " BIGNUM *tmp_1 = NULL;\n"
402 " BIGNUM *tmp_2 = NULL;\n"
403 " BIGNUM *tmp_3 = NULL;\n"
404 "\n");
405
406 BIO_printf(out, " if ((tmp_1 = BN_bin2bn(ec_p_%d, sizeof (ec_p_%d), NULL)) == NULL)\n"
407 " goto err;\n", len, len);
408 BIO_printf(out, " if ((tmp_2 = BN_bin2bn(ec_a_%d, sizeof (ec_a_%d), NULL)) == NULL)\n"
409 " goto err;\n", len, len);
410 BIO_printf(out, " if ((tmp_3 = BN_bin2bn(ec_b_%d, sizeof (ec_b_%d), NULL)) == NULL)\n"
411 " goto err;\n", len, len);
412 BIO_printf(out, " if ((group = EC_GROUP_new_curve_GFp(tmp_1, tmp_2, tmp_3, NULL)) == NULL)\n"
413 " goto err;\n"
414 "\n");
415 BIO_printf(out, " /* build generator */\n");
416 BIO_printf(out, " if ((tmp_1 = BN_bin2bn(ec_gen_%d, sizeof (ec_gen_%d), tmp_1)) == NULL)\n"
417 " goto err;\n", len, len);
418 BIO_printf(out, " point = EC_POINT_bn2point(group, tmp_1, NULL, NULL);\n");
419 BIO_printf(out, " if (point == NULL)\n"
420 " goto err;\n");
421 BIO_printf(out, " if ((tmp_2 = BN_bin2bn(ec_order_%d, sizeof (ec_order_%d), tmp_2)) == NULL)\n"
422 " goto err;\n", len, len);
423 BIO_printf(out, " if ((tmp_3 = BN_bin2bn(ec_cofactor_%d, sizeof (ec_cofactor_%d), tmp_3)) == NULL)\n"
424 " goto err;\n", len, len);
425 BIO_printf(out, " if (!EC_GROUP_set_generator(group, point, tmp_2, tmp_3))\n"
426 " goto err;\n"
427 "ok = 1;"
428 "\n");
429 BIO_printf(out, "err:\n"
430 " BN_free(tmp_1);\n"
431 " BN_free(tmp_2);\n"
432 " BN_free(tmp_3);\n"
433 " EC_POINT_free(point);\n"
434 " if (!ok) {\n"
435 " EC_GROUP_free(group);\n"
436 " return NULL;\n"
437 " }\n"
438 " return (group);\n"
439 "}\n");
0f113f3e
MC
440 }
441
442 if (!noout) {
443 if (outformat == FORMAT_ASN1)
444 i = i2d_ECPKParameters_bio(out, group);
7e1b7485 445 else
0f113f3e 446 i = PEM_write_bio_ECPKParameters(out, group);
0f113f3e
MC
447 if (!i) {
448 BIO_printf(bio_err, "unable to write elliptic "
449 "curve parameters\n");
450 ERR_print_errors(bio_err);
451 goto end;
452 }
453 }
454
455 if (need_rand) {
7e1b7485 456 app_RAND_load_file(NULL, (inrand != NULL));
0f113f3e
MC
457 if (inrand != NULL)
458 BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
459 app_RAND_load_files(inrand));
460 }
461
462 if (genkey) {
463 EC_KEY *eckey = EC_KEY_new();
464
465 if (eckey == NULL)
466 goto end;
467
468 assert(need_rand);
469
f6de4eb7
DSH
470 if (EC_KEY_set_group(eckey, group) == 0) {
471 BIO_printf(bio_err, "unable to set group when generating key\n");
472 EC_KEY_free(eckey);
473 ERR_print_errors(bio_err);
0f113f3e 474 goto end;
f6de4eb7 475 }
0f113f3e
MC
476
477 if (!EC_KEY_generate_key(eckey)) {
f6de4eb7 478 BIO_printf(bio_err, "unable to generate key\n");
0f113f3e 479 EC_KEY_free(eckey);
f6de4eb7 480 ERR_print_errors(bio_err);
0f113f3e
MC
481 goto end;
482 }
3b061a00 483 assert(private);
0f113f3e
MC
484 if (outformat == FORMAT_ASN1)
485 i = i2d_ECPrivateKey_bio(out, eckey);
7e1b7485 486 else
0f113f3e
MC
487 i = PEM_write_bio_ECPrivateKey(out, eckey, NULL,
488 NULL, 0, NULL, NULL);
0f113f3e
MC
489 EC_KEY_free(eckey);
490 }
491
492 if (need_rand)
7e1b7485 493 app_RAND_write_file(NULL);
0f113f3e
MC
494
495 ret = 0;
496 end:
23a1d5e9
RS
497 BN_free(ec_p);
498 BN_free(ec_a);
499 BN_free(ec_b);
500 BN_free(ec_gen);
501 BN_free(ec_order);
502 BN_free(ec_cofactor);
b548a1f1 503 OPENSSL_free(buffer);
ca3a82c3
RS
504 BIO_free(in);
505 BIO_free_all(out);
8fdc3734 506 EC_GROUP_free(group);
7e1b7485 507 return (ret);
5dbd3efc
BM
508}
509
5dbd3efc 510#endif