2 /* ====================================================================
3 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
55 #ifndef OPENSSL_NO_ECDSA
62 #include <openssl/bio.h>
63 #include <openssl/err.h>
64 #include <openssl/bn.h>
65 #include <openssl/ec.h>
66 #include <openssl/ecdsa.h>
67 #include <openssl/x509.h>
68 #include <openssl/pem.h>
71 #define PROG ecparam_main
73 /* -inform arg - input format - default PEM (DER or PEM)
74 * -outform arg - output format - default PEM
75 * -in arg - input file - default stdin
76 * -out arg - output file - default stdout
79 * -check - validate the ec parameters
82 * -name file - use the ecparameters with 'short name' name
83 * -list_curves - prints a list of all currently available curve
84 * 'short names' and exits
85 * -conv_form - specifies the point conversion form
86 * possible values : compressed
87 * uncompressed (default)
89 * -param_enc - specifies the way the ec parameters are encoded
90 * in the asn1 der encoding
91 * possilbe values : named_curve (default)
93 * -no_seed - if 'explicit' parameters are choosen do not
95 * -genkey - generates a ecdsa private key
97 * -engine e - use engine e, possible a hardware device
100 static const char *curve_list[20] = {
101 "prime192v1 - NIST recommended curve over a 192 bit prime field",
102 "prime192v2 - 192 bit prime curve from the X9.62 draft",
103 "prime192v3 - 192 bit prime curve from the X9.62 draft",
104 "prime239v1 - 239 bit prime curve from the X9.62 draft",
105 "prime239v2 - 239 bit prime curve from the X9.62 draft",
106 "prime239v3 - 239 bit prime curve from the X9.62 draft",
107 "prime256v1 - NIST recommended curve over a 256 bit prime field",
108 "secp112r1 - SECG recommended curve over a 112 bit prime field",
109 "secp112r2 - SECG recommended curve over a 112 bit prime field",
110 "secp128r1 - SECG recommended curve over a 128 bit prime field",
111 "secp128r2 - SECG recommended curve over a 128 bit prime field",
112 "secp160k1 - SECG recommended curve over a 160 bit prime field",
113 "secp160r1 - SECG recommended curve over a 160 bit prime field",
114 "secp160r2 - SECG recommended curve over a 160 bit prime field",
115 "secp192k1 - SECG recommended curve over a 192 bit prime field",
116 "secp224k1 - SECG recommended curve over a 224 bit prime field",
117 "secp224r1 - NIST recommended curve over a 224 bit prime field",
118 "secp256k1 - SECG recommended curve over a 256 bit prime field",
119 "secp384r1 - NIST recommended curve over a 384 bit prime field",
120 "secp521r1 - NIST recommended curve over a 521 bit prime field"
123 static int ecparam_print_var(BIO *,BIGNUM *,const char *,int,unsigned char *);
125 int MAIN(int, char **);
127 int MAIN(int argc, char **argv)
129 EC_GROUP *group = NULL;
130 point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
132 int asn1_flag = OPENSSL_EC_NAMED_CURVE;
133 int new_asn1_flag = 0;
134 char *curve_name = NULL, *inrand = NULL;
135 int list_curves = 0, no_seed = 0, check = 0,
136 badops = 0, text = 0, i, need_rand = 0, genkey = 0;
137 char *infile = NULL, *outfile = NULL, *prog;
138 BIO *in = NULL, *out = NULL;
139 int informat, outformat, noout = 0, C = 0, ret = 1;
143 BIGNUM *ec_p = NULL, *ec_a = NULL, *ec_b = NULL,
144 *ec_gen = NULL, *ec_order = NULL, *ec_cofactor = NULL;
145 unsigned char *buffer = NULL;
150 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
151 BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
153 if (!load_config(bio_err, NULL))
157 outformat=FORMAT_PEM;
164 if (strcmp(*argv,"-inform") == 0)
166 if (--argc < 1) goto bad;
167 informat=str2fmt(*(++argv));
169 else if (strcmp(*argv,"-outform") == 0)
171 if (--argc < 1) goto bad;
172 outformat=str2fmt(*(++argv));
174 else if (strcmp(*argv,"-in") == 0)
176 if (--argc < 1) goto bad;
179 else if (strcmp(*argv,"-out") == 0)
181 if (--argc < 1) goto bad;
184 else if (strcmp(*argv,"-text") == 0)
186 else if (strcmp(*argv,"-C") == 0)
188 else if (strcmp(*argv,"-check") == 0)
190 else if (strcmp (*argv, "-name") == 0)
194 curve_name = *(++argv);
196 else if (strcmp(*argv, "-list_curves") == 0)
198 else if (strcmp(*argv, "-conv_form") == 0)
204 if (strcmp(*argv, "compressed") == 0)
205 form = POINT_CONVERSION_COMPRESSED;
206 else if (strcmp(*argv, "uncompressed") == 0)
207 form = POINT_CONVERSION_UNCOMPRESSED;
208 else if (strcmp(*argv, "hybrid") == 0)
209 form = POINT_CONVERSION_HYBRID;
213 else if (strcmp(*argv, "-param_enc") == 0)
219 if (strcmp(*argv, "named_curve") == 0)
220 asn1_flag = OPENSSL_EC_NAMED_CURVE;
221 else if (strcmp(*argv, "explicit") == 0)
226 else if (strcmp(*argv, "-no_seed") == 0)
228 else if (strcmp(*argv, "-noout") == 0)
230 else if (strcmp(*argv,"-genkey") == 0)
235 else if (strcmp(*argv, "-rand") == 0)
237 if (--argc < 1) goto bad;
241 else if(strcmp(*argv, "-engine") == 0)
243 if (--argc < 1) goto bad;
248 BIO_printf(bio_err,"unknown option %s\n",*argv);
259 BIO_printf(bio_err, "%s [options] <infile >outfile\n",prog);
260 BIO_printf(bio_err, "where options are\n");
261 BIO_printf(bio_err, " -inform arg input format - "
262 "default PEM (DER or PEM)\n");
263 BIO_printf(bio_err, " -outform arg output format - "
265 BIO_printf(bio_err, " -in arg input file - "
267 BIO_printf(bio_err, " -out arg output file - "
269 BIO_printf(bio_err, " -noout do not print the "
271 BIO_printf(bio_err, " -text print the ec "
272 "parameters in text form\n");
273 BIO_printf(bio_err, " -check validate the ec "
275 BIO_printf(bio_err, " -C print a 'C' "
276 "function creating the parameters\n");
277 BIO_printf(bio_err, " -name arg use the "
278 "ec parameters with 'short name' name\n");
279 BIO_printf(bio_err, " -list_curves prints a list of "
280 "all currently available curve\n");
281 BIO_printf(bio_err, " 'short names'\n");
282 BIO_printf(bio_err, " -conv_form arg specifies the "
283 "point conversion form \n");
284 BIO_printf(bio_err, " possible values :"
286 BIO_printf(bio_err, " "
287 " uncompressed (default)\n");
288 BIO_printf(bio_err, " "
290 BIO_printf(bio_err, " -param_enc arg specifies the way"
291 " the ec parameters are encoded\n");
292 BIO_printf(bio_err, " in the asn1 der "
294 BIO_printf(bio_err, " possilbe values :"
295 " named_curve (default)\n");
296 BIO_printf(bio_err," "
298 BIO_printf(bio_err, " -no_seed if 'explicit'"
299 " parameters are choosen do not\n");
300 BIO_printf(bio_err, " use the seed\n");
301 BIO_printf(bio_err, " -genkey generate ecdsa"
303 BIO_printf(bio_err, " -rand file files to use for"
304 " random number input\n");
305 BIO_printf(bio_err, " -engine e use engine e, "
306 "possible a hardware device\n");
310 ERR_load_crypto_strings();
312 in=BIO_new(BIO_s_file());
313 out=BIO_new(BIO_s_file());
314 if ((in == NULL) || (out == NULL))
316 ERR_print_errors(bio_err);
321 BIO_set_fp(in,stdin,BIO_NOCLOSE);
324 if (BIO_read_filename(in,infile) <= 0)
332 BIO_set_fp(out,stdout,BIO_NOCLOSE);
333 #ifdef OPENSSL_SYS_VMS
335 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
336 out = BIO_push(tmpbio, out);
342 if (BIO_write_filename(out,outfile) <= 0)
349 e = setup_engine(bio_err, engine, 0);
355 for (; counter < sizeof(curve_list)/sizeof(char *); counter++)
356 if (BIO_printf(bio_err, " %s\n", curve_list[counter])
363 if (curve_name != NULL)
365 int nid = OBJ_sn2nid(curve_name);
369 BIO_printf(bio_err, "unknown curve name (%s)\n",
374 group = EC_GROUP_new_by_nid(nid);
377 BIO_printf(bio_err, "unable to create curve (%s)\n",
381 EC_GROUP_set_asn1_flag(group, asn1_flag);
382 EC_GROUP_set_point_conversion_form(group, form);
384 else if (informat == FORMAT_ASN1)
386 group = d2i_ECPKParameters_bio(in, NULL);
388 else if (informat == FORMAT_PEM)
390 group = PEM_read_bio_ECPKParameters(in,NULL,NULL,NULL);
394 BIO_printf(bio_err, "bad input format specified\n");
401 "unable to load elliptic curve parameters\n");
402 ERR_print_errors(bio_err);
407 EC_GROUP_set_point_conversion_form(group, form);
410 EC_GROUP_set_asn1_flag(group, asn1_flag);
414 EC_GROUP_set_seed(group, NULL, 0);
419 if (!ECPKParameters_print(out, group, 0))
426 BIO_printf(bio_err, "no elliptic curve parameters\n");
427 BIO_printf(bio_err, "checking elliptic curve parameters: ");
428 if (!EC_GROUP_check(group, NULL))
430 BIO_printf(bio_err, "failed\n");
431 ERR_print_errors(bio_err);
434 BIO_printf(bio_err, "ok\n");
440 size_t buf_len = 0, tmp_len = 0;
441 const EC_POINT *point;
442 int is_prime, len = 0;
443 const EC_METHOD *meth = EC_GROUP_method_of(group);
445 if ((ec_p = BN_new()) == NULL || (ec_a = BN_new()) == NULL ||
446 (ec_b = BN_new()) == NULL || (ec_gen = BN_new()) == NULL ||
447 (ec_order = BN_new()) == NULL ||
448 (ec_cofactor = BN_new()) == NULL )
450 perror("OPENSSL_malloc");
454 is_prime = (EC_METHOD_get_field_type(meth) ==
455 NID_X9_62_prime_field);
459 if (!EC_GROUP_get_curve_GFp(group, ec_p, ec_a,
469 if ((point = EC_GROUP_get0_generator(group)) == NULL)
471 if (!EC_POINT_point2bn(group, point,
472 EC_GROUP_get_point_conversion_form(group), ec_gen,
475 if (!EC_GROUP_get_order(group, ec_order, NULL))
477 if (!EC_GROUP_get_cofactor(group, ec_cofactor, NULL))
480 if (!ec_p || !ec_a || !ec_b || !ec_gen ||
481 !ec_order || !ec_cofactor)
484 len = BN_num_bits(ec_order);
486 if ((tmp_len = (size_t)BN_num_bytes(ec_p)) > buf_len)
488 if ((tmp_len = (size_t)BN_num_bytes(ec_a)) > buf_len)
490 if ((tmp_len = (size_t)BN_num_bytes(ec_b)) > buf_len)
492 if ((tmp_len = (size_t)BN_num_bytes(ec_gen)) > buf_len)
494 if ((tmp_len = (size_t)BN_num_bytes(ec_order)) > buf_len)
496 if ((tmp_len = (size_t)BN_num_bytes(ec_cofactor)) > buf_len)
499 buffer = (unsigned char *)OPENSSL_malloc(buf_len);
503 perror("OPENSSL_malloc");
507 ecparam_print_var(out, ec_p, "ec_p", len, buffer);
508 ecparam_print_var(out, ec_a, "ec_a", len, buffer);
509 ecparam_print_var(out, ec_b, "ec_b", len, buffer);
510 ecparam_print_var(out, ec_gen, "ec_gen", len, buffer);
511 ecparam_print_var(out, ec_order, "ec_order", len, buffer);
512 ecparam_print_var(out, ec_cofactor, "ec_cofactor", len,
515 BIO_printf(out, "\n\n");
517 BIO_printf(out, "EC_GROUP *get_ec_group_%d(void)\n\t{\n", len);
518 BIO_printf(out, "\tint ok=0;\n");
519 BIO_printf(out, "\tEC_GROUP *group = NULL;\n");
520 BIO_printf(out, "\tEC_POINT *point = NULL;\n");
521 BIO_printf(out, "\tBIGNUM *tmp_1 = NULL, *tmp_2 = NULL, "
522 "*tmp_3 = NULL;\n\n");
523 BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_p_%d, "
524 "sizeof(ec_p_%d), NULL)) == NULL)\n\t\t"
525 "goto err;\n", len, len);
526 BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_a_%d, "
527 "sizeof(ec_a_%d), NULL)) == NULL)\n\t\t"
528 "goto err;\n", len, len);
529 BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_b_%d, "
530 "sizeof(ec_b_%d), NULL)) == NULL)\n\t\t"
531 "goto err;\n", len, len);
534 BIO_printf(out, "\tif ((group = EC_GROUP_new_curve_"
535 "GFp(tmp_1, tmp_2, tmp_3, NULL)) == NULL)"
536 "\n\t\tgoto err;\n\n");
543 BIO_printf(out, "\t/* build generator */\n");
544 BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_gen_%d, "
545 "sizeof(ec_gen_%d), tmp_1)) == NULL)"
546 "\n\t\tgoto err;\n", len, len);
547 BIO_printf(out, "\tpoint = EC_POINT_bn2point(group, tmp_1, "
549 BIO_printf(out, "\tif (point == NULL)\n\t\tgoto err;\n");
550 BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_order_%d, "
551 "sizeof(ec_order_%d), tmp_2)) == NULL)"
552 "\n\t\tgoto err;\n", len, len);
553 BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_cofactor_%d, "
554 "sizeof(ec_cofactor_%d), tmp_3)) == NULL)"
555 "\n\t\tgoto err;\n", len, len);
556 BIO_printf(out, "\tif (!EC_GROUP_set_generator(group, point,"
557 " tmp_2, tmp_3))\n\t\tgoto err;\n");
558 BIO_printf(out, "\n\tok=1;\n");
559 BIO_printf(out, "err:\n");
560 BIO_printf(out, "\tif (tmp_1)\n\t\tBN_free(tmp_1);\n");
561 BIO_printf(out, "\tif (tmp_2)\n\t\tBN_free(tmp_2);\n");
562 BIO_printf(out, "\tif (tmp_3)\n\t\tBN_free(tmp_3);\n");
563 BIO_printf(out, "\tif (point)\n\t\tEC_POINT_free(point);\n");
564 BIO_printf(out, "\tif (!ok)\n");
565 BIO_printf(out, "\t\t{\n");
566 BIO_printf(out, "\t\tEC_GROUP_free(group);\n");
567 BIO_printf(out, "\t\tgroup = NULL;\n");
568 BIO_printf(out, "\t\t}\n");
569 BIO_printf(out, "\treturn(group);\n\t}\n");
574 if (outformat == FORMAT_ASN1)
575 i = i2d_ECPKParameters_bio(out, group);
576 else if (outformat == FORMAT_PEM)
577 i = PEM_write_bio_ECPKParameters(out, group);
580 BIO_printf(bio_err,"bad output format specified for"
586 BIO_printf(bio_err, "unable to write elliptic "
587 "curve parameters\n");
588 ERR_print_errors(bio_err);
595 app_RAND_load_file(NULL, bio_err, (inrand != NULL));
597 BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
598 app_RAND_load_files(inrand));
603 ECDSA *ecdsa = ECDSA_new();
610 ecdsa->group = group;
612 if (!ECDSA_generate_key(ecdsa))
618 if (outformat == FORMAT_ASN1)
619 i = i2d_ECDSAPrivateKey_bio(out, ecdsa);
620 else if (outformat == FORMAT_PEM)
621 i = PEM_write_bio_ECDSAPrivateKey(out, ecdsa, NULL,
622 NULL, 0, NULL, NULL);
625 BIO_printf(bio_err, "bad output format specified "
636 app_RAND_write_file(NULL, bio_err);
651 BN_free(ec_cofactor);
653 OPENSSL_free(buffer);
659 EC_GROUP_free(group);
664 int ecparam_print_var(BIO *out, BIGNUM *in, const char *var,
665 int len, unsigned char *buffer)
667 BIO_printf(out, "static unsigned char %s_%d[] = {", var, len);
669 BIO_printf(out, "\n\t0x00");
674 l = BN_bn2bin(in, buffer);
675 for (i=0; i<l-1; i++)
678 BIO_printf(out, "\n\t");
679 BIO_printf(out, "0x%02X,", buffer[i]);
682 BIO_printf(out, "\n\t");
683 BIO_printf(out, "0x%02X", buffer[i]);
685 BIO_printf(out, "\n\t};\n\n");