3 * Written by Nils Larsch for the OpenSSL project.
5 /* ====================================================================
6 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
58 /* ====================================================================
59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
61 * Portions of the attached software ("Contribution") are developed by
62 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
64 * The Contribution is licensed pursuant to the OpenSSL open source
65 * license provided above.
67 * In addition, Sun covenants to all licensees who provide a reciprocal
68 * covenant with respect to their own patents if any, not to sue under
69 * current and future patent claims necessarily infringed by the making,
70 * using, practicing, selling, offering for sale and/or otherwise
71 * disposing of the Contribution as delivered hereunder
72 * (or portions thereof), provided that such covenant shall not apply:
73 * 1) for code that a licensee deletes from the Contribution;
74 * 2) separates from the Contribution; or
75 * 3) for infringements caused by:
76 * i) the modification of the Contribution or
77 * ii) the combination of the Contribution with other software or
78 * devices where such combination causes the infringement.
80 * The elliptic curve binary polynomial software is originally written by
81 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
91 #include <openssl/bio.h>
92 #include <openssl/err.h>
93 #include <openssl/bn.h>
94 #include <openssl/ec.h>
95 #include <openssl/x509.h>
96 #include <openssl/pem.h>
99 #define PROG ecparam_main
101 /* -inform arg - input format - default PEM (DER or PEM)
102 * -outform arg - output format - default PEM
103 * -in arg - input file - default stdin
104 * -out arg - output file - default stdout
105 * -noout - do not print the ec parameter
106 * -text - print the ec parameters in text form
107 * -check - validate the ec parameters
108 * -C - print a 'C' function creating the parameters
109 * -name arg - use the ec parameters with 'short name' name
110 * -list_curves - prints a list of all currently available curve 'short names'
111 * -conv_form arg - specifies the point conversion form
112 * - possible values: compressed
113 * uncompressed (default)
115 * -param_enc arg - specifies the way the ec parameters are encoded
116 * in the asn1 der encoding
117 * possible values: named_curve (default)
119 * -no_seed - if 'explicit' parameters are choosen do not use the seed
120 * -genkey - generate ec key
121 * -rand file - files to use for random number input
122 * -engine e - use engine e, possibly a hardware device
126 static int ecparam_print_var(BIO
*,BIGNUM
*,const char *,int,unsigned char *);
128 int MAIN(int, char **);
130 int MAIN(int argc
, char **argv
)
132 EC_GROUP
*group
= NULL
;
133 point_conversion_form_t form
= POINT_CONVERSION_UNCOMPRESSED
;
135 int asn1_flag
= OPENSSL_EC_NAMED_CURVE
;
136 int new_asn1_flag
= 0;
137 char *curve_name
= NULL
, *inrand
= NULL
;
138 int list_curves
= 0, no_seed
= 0, check
= 0,
139 badops
= 0, text
= 0, i
, need_rand
= 0, genkey
= 0;
140 char *infile
= NULL
, *outfile
= NULL
, *prog
;
141 BIO
*in
= NULL
, *out
= NULL
;
142 int informat
, outformat
, noout
= 0, C
= 0, ret
= 1;
146 BIGNUM
*ec_p
= NULL
, *ec_a
= NULL
, *ec_b
= NULL
,
147 *ec_gen
= NULL
, *ec_order
= NULL
, *ec_cofactor
= NULL
;
148 unsigned char *buffer
= NULL
;
153 if ((bio_err
=BIO_new(BIO_s_file())) != NULL
)
154 BIO_set_fp(bio_err
,stderr
,BIO_NOCLOSE
|BIO_FP_TEXT
);
156 if (!load_config(bio_err
, NULL
))
160 outformat
=FORMAT_PEM
;
167 if (strcmp(*argv
,"-inform") == 0)
169 if (--argc
< 1) goto bad
;
170 informat
=str2fmt(*(++argv
));
172 else if (strcmp(*argv
,"-outform") == 0)
174 if (--argc
< 1) goto bad
;
175 outformat
=str2fmt(*(++argv
));
177 else if (strcmp(*argv
,"-in") == 0)
179 if (--argc
< 1) goto bad
;
182 else if (strcmp(*argv
,"-out") == 0)
184 if (--argc
< 1) goto bad
;
187 else if (strcmp(*argv
,"-text") == 0)
189 else if (strcmp(*argv
,"-C") == 0)
191 else if (strcmp(*argv
,"-check") == 0)
193 else if (strcmp (*argv
, "-name") == 0)
197 curve_name
= *(++argv
);
199 else if (strcmp(*argv
, "-list_curves") == 0)
201 else if (strcmp(*argv
, "-conv_form") == 0)
207 if (strcmp(*argv
, "compressed") == 0)
208 form
= POINT_CONVERSION_COMPRESSED
;
209 else if (strcmp(*argv
, "uncompressed") == 0)
210 form
= POINT_CONVERSION_UNCOMPRESSED
;
211 else if (strcmp(*argv
, "hybrid") == 0)
212 form
= POINT_CONVERSION_HYBRID
;
216 else if (strcmp(*argv
, "-param_enc") == 0)
222 if (strcmp(*argv
, "named_curve") == 0)
223 asn1_flag
= OPENSSL_EC_NAMED_CURVE
;
224 else if (strcmp(*argv
, "explicit") == 0)
229 else if (strcmp(*argv
, "-no_seed") == 0)
231 else if (strcmp(*argv
, "-noout") == 0)
233 else if (strcmp(*argv
,"-genkey") == 0)
238 else if (strcmp(*argv
, "-rand") == 0)
240 if (--argc
< 1) goto bad
;
244 else if(strcmp(*argv
, "-engine") == 0)
246 if (--argc
< 1) goto bad
;
251 BIO_printf(bio_err
,"unknown option %s\n",*argv
);
262 BIO_printf(bio_err
, "%s [options] <infile >outfile\n",prog
);
263 BIO_printf(bio_err
, "where options are\n");
264 BIO_printf(bio_err
, " -inform arg input format - "
265 "default PEM (DER or PEM)\n");
266 BIO_printf(bio_err
, " -outform arg output format - "
268 BIO_printf(bio_err
, " -in arg input file - "
270 BIO_printf(bio_err
, " -out arg output file - "
272 BIO_printf(bio_err
, " -noout do not print the "
274 BIO_printf(bio_err
, " -text print the ec "
275 "parameters in text form\n");
276 BIO_printf(bio_err
, " -check validate the ec "
278 BIO_printf(bio_err
, " -C print a 'C' "
279 "function creating the parameters\n");
280 BIO_printf(bio_err
, " -name arg use the "
281 "ec parameters with 'short name' name\n");
282 BIO_printf(bio_err
, " -list_curves prints a list of "
283 "all currently available curve 'short names'\n");
284 BIO_printf(bio_err
, " -conv_form arg specifies the "
285 "point conversion form \n");
286 BIO_printf(bio_err
, " possible values:"
288 BIO_printf(bio_err
, " "
289 " uncompressed (default)\n");
290 BIO_printf(bio_err
, " "
292 BIO_printf(bio_err
, " -param_enc arg specifies the way"
293 " the ec parameters are encoded\n");
294 BIO_printf(bio_err
, " in the asn1 der "
296 BIO_printf(bio_err
, " possible values:"
297 " named_curve (default)\n");
298 BIO_printf(bio_err
, " "
300 BIO_printf(bio_err
, " -no_seed if 'explicit'"
301 " parameters are choosen do not"
303 BIO_printf(bio_err
, " -genkey generate ec"
305 BIO_printf(bio_err
, " -rand file files to use for"
306 " random number input\n");
307 BIO_printf(bio_err
, " -engine e use engine e, "
308 "possibly a hardware device\n");
312 ERR_load_crypto_strings();
314 in
=BIO_new(BIO_s_file());
315 out
=BIO_new(BIO_s_file());
316 if ((in
== NULL
) || (out
== NULL
))
318 ERR_print_errors(bio_err
);
323 BIO_set_fp(in
,stdin
,BIO_NOCLOSE
);
326 if (BIO_read_filename(in
,infile
) <= 0)
334 BIO_set_fp(out
,stdout
,BIO_NOCLOSE
);
335 #ifdef OPENSSL_SYS_VMS
337 BIO
*tmpbio
= BIO_new(BIO_f_linebuffer());
338 out
= BIO_push(tmpbio
, out
);
344 if (BIO_write_filename(out
,outfile
) <= 0)
351 e
= setup_engine(bio_err
, engine
, 0);
361 int len
, nid
= ec_group_index2nid(counter
++);
364 comment
= EC_GROUP_get0_comment(nid
);
365 sname
= OBJ_nid2sn(nid
);
371 len
= BIO_printf(out
, " %-10s: ", sname
);
372 if (len
+ strlen(comment
) > 80)
373 BIO_printf(out
, "\n%80s\n", comment
);
375 BIO_printf(out
, "%s\n", comment
);
382 if (curve_name
!= NULL
)
384 int nid
= OBJ_sn2nid(curve_name
);
388 BIO_printf(bio_err
, "unknown curve name (%s)\n",
393 group
= EC_GROUP_new_by_nid(nid
);
396 BIO_printf(bio_err
, "unable to create curve (%s)\n",
400 EC_GROUP_set_asn1_flag(group
, asn1_flag
);
401 EC_GROUP_set_point_conversion_form(group
, form
);
403 else if (informat
== FORMAT_ASN1
)
405 group
= d2i_ECPKParameters_bio(in
, NULL
);
407 else if (informat
== FORMAT_PEM
)
409 group
= PEM_read_bio_ECPKParameters(in
,NULL
,NULL
,NULL
);
413 BIO_printf(bio_err
, "bad input format specified\n");
420 "unable to load elliptic curve parameters\n");
421 ERR_print_errors(bio_err
);
426 EC_GROUP_set_point_conversion_form(group
, form
);
429 EC_GROUP_set_asn1_flag(group
, asn1_flag
);
433 EC_GROUP_set_seed(group
, NULL
, 0);
438 if (!ECPKParameters_print(out
, group
, 0))
445 BIO_printf(bio_err
, "no elliptic curve parameters\n");
446 BIO_printf(bio_err
, "checking elliptic curve parameters: ");
447 if (!EC_GROUP_check(group
, NULL
))
449 BIO_printf(bio_err
, "failed\n");
450 ERR_print_errors(bio_err
);
453 BIO_printf(bio_err
, "ok\n");
459 size_t buf_len
= 0, tmp_len
= 0;
460 const EC_POINT
*point
;
461 int is_prime
, len
= 0;
462 const EC_METHOD
*meth
= EC_GROUP_method_of(group
);
464 if ((ec_p
= BN_new()) == NULL
|| (ec_a
= BN_new()) == NULL
||
465 (ec_b
= BN_new()) == NULL
|| (ec_gen
= BN_new()) == NULL
||
466 (ec_order
= BN_new()) == NULL
||
467 (ec_cofactor
= BN_new()) == NULL
)
469 perror("OPENSSL_malloc");
473 is_prime
= (EC_METHOD_get_field_type(meth
) ==
474 NID_X9_62_prime_field
);
478 if (!EC_GROUP_get_curve_GFp(group
, ec_p
, ec_a
,
488 if ((point
= EC_GROUP_get0_generator(group
)) == NULL
)
490 if (!EC_POINT_point2bn(group
, point
,
491 EC_GROUP_get_point_conversion_form(group
), ec_gen
,
494 if (!EC_GROUP_get_order(group
, ec_order
, NULL
))
496 if (!EC_GROUP_get_cofactor(group
, ec_cofactor
, NULL
))
499 if (!ec_p
|| !ec_a
|| !ec_b
|| !ec_gen
||
500 !ec_order
|| !ec_cofactor
)
503 len
= BN_num_bits(ec_order
);
505 if ((tmp_len
= (size_t)BN_num_bytes(ec_p
)) > buf_len
)
507 if ((tmp_len
= (size_t)BN_num_bytes(ec_a
)) > buf_len
)
509 if ((tmp_len
= (size_t)BN_num_bytes(ec_b
)) > buf_len
)
511 if ((tmp_len
= (size_t)BN_num_bytes(ec_gen
)) > buf_len
)
513 if ((tmp_len
= (size_t)BN_num_bytes(ec_order
)) > buf_len
)
515 if ((tmp_len
= (size_t)BN_num_bytes(ec_cofactor
)) > buf_len
)
518 buffer
= (unsigned char *)OPENSSL_malloc(buf_len
);
522 perror("OPENSSL_malloc");
526 ecparam_print_var(out
, ec_p
, "ec_p", len
, buffer
);
527 ecparam_print_var(out
, ec_a
, "ec_a", len
, buffer
);
528 ecparam_print_var(out
, ec_b
, "ec_b", len
, buffer
);
529 ecparam_print_var(out
, ec_gen
, "ec_gen", len
, buffer
);
530 ecparam_print_var(out
, ec_order
, "ec_order", len
, buffer
);
531 ecparam_print_var(out
, ec_cofactor
, "ec_cofactor", len
,
534 BIO_printf(out
, "\n\n");
536 BIO_printf(out
, "EC_GROUP *get_ec_group_%d(void)\n\t{\n", len
);
537 BIO_printf(out
, "\tint ok=0;\n");
538 BIO_printf(out
, "\tEC_GROUP *group = NULL;\n");
539 BIO_printf(out
, "\tEC_POINT *point = NULL;\n");
540 BIO_printf(out
, "\tBIGNUM *tmp_1 = NULL, *tmp_2 = NULL, "
541 "*tmp_3 = NULL;\n\n");
542 BIO_printf(out
, "\tif ((tmp_1 = BN_bin2bn(ec_p_%d, "
543 "sizeof(ec_p_%d), NULL)) == NULL)\n\t\t"
544 "goto err;\n", len
, len
);
545 BIO_printf(out
, "\tif ((tmp_2 = BN_bin2bn(ec_a_%d, "
546 "sizeof(ec_a_%d), NULL)) == NULL)\n\t\t"
547 "goto err;\n", len
, len
);
548 BIO_printf(out
, "\tif ((tmp_3 = BN_bin2bn(ec_b_%d, "
549 "sizeof(ec_b_%d), NULL)) == NULL)\n\t\t"
550 "goto err;\n", len
, len
);
553 BIO_printf(out
, "\tif ((group = EC_GROUP_new_curve_"
554 "GFp(tmp_1, tmp_2, tmp_3, NULL)) == NULL)"
555 "\n\t\tgoto err;\n\n");
562 BIO_printf(out
, "\t/* build generator */\n");
563 BIO_printf(out
, "\tif ((tmp_1 = BN_bin2bn(ec_gen_%d, "
564 "sizeof(ec_gen_%d), tmp_1)) == NULL)"
565 "\n\t\tgoto err;\n", len
, len
);
566 BIO_printf(out
, "\tpoint = EC_POINT_bn2point(group, tmp_1, "
568 BIO_printf(out
, "\tif (point == NULL)\n\t\tgoto err;\n");
569 BIO_printf(out
, "\tif ((tmp_2 = BN_bin2bn(ec_order_%d, "
570 "sizeof(ec_order_%d), tmp_2)) == NULL)"
571 "\n\t\tgoto err;\n", len
, len
);
572 BIO_printf(out
, "\tif ((tmp_3 = BN_bin2bn(ec_cofactor_%d, "
573 "sizeof(ec_cofactor_%d), tmp_3)) == NULL)"
574 "\n\t\tgoto err;\n", len
, len
);
575 BIO_printf(out
, "\tif (!EC_GROUP_set_generator(group, point,"
576 " tmp_2, tmp_3))\n\t\tgoto err;\n");
577 BIO_printf(out
, "\n\tok=1;\n");
578 BIO_printf(out
, "err:\n");
579 BIO_printf(out
, "\tif (tmp_1)\n\t\tBN_free(tmp_1);\n");
580 BIO_printf(out
, "\tif (tmp_2)\n\t\tBN_free(tmp_2);\n");
581 BIO_printf(out
, "\tif (tmp_3)\n\t\tBN_free(tmp_3);\n");
582 BIO_printf(out
, "\tif (point)\n\t\tEC_POINT_free(point);\n");
583 BIO_printf(out
, "\tif (!ok)\n");
584 BIO_printf(out
, "\t\t{\n");
585 BIO_printf(out
, "\t\tEC_GROUP_free(group);\n");
586 BIO_printf(out
, "\t\tgroup = NULL;\n");
587 BIO_printf(out
, "\t\t}\n");
588 BIO_printf(out
, "\treturn(group);\n\t}\n");
593 if (outformat
== FORMAT_ASN1
)
594 i
= i2d_ECPKParameters_bio(out
, group
);
595 else if (outformat
== FORMAT_PEM
)
596 i
= PEM_write_bio_ECPKParameters(out
, group
);
599 BIO_printf(bio_err
,"bad output format specified for"
605 BIO_printf(bio_err
, "unable to write elliptic "
606 "curve parameters\n");
607 ERR_print_errors(bio_err
);
614 app_RAND_load_file(NULL
, bio_err
, (inrand
!= NULL
));
616 BIO_printf(bio_err
,"%ld semi-random bytes loaded\n",
617 app_RAND_load_files(inrand
));
622 EC_KEY
*eckey
= EC_KEY_new();
629 eckey
->group
= group
;
631 if (!EC_KEY_generate_key(eckey
))
637 if (outformat
== FORMAT_ASN1
)
638 i
= i2d_ECPrivateKey_bio(out
, eckey
);
639 else if (outformat
== FORMAT_PEM
)
640 i
= PEM_write_bio_ECPrivateKey(out
, eckey
, NULL
,
641 NULL
, 0, NULL
, NULL
);
644 BIO_printf(bio_err
, "bad output format specified "
655 app_RAND_write_file(NULL
, bio_err
);
670 BN_free(ec_cofactor
);
672 OPENSSL_free(buffer
);
678 EC_GROUP_free(group
);
683 int ecparam_print_var(BIO
*out
, BIGNUM
*in
, const char *var
,
684 int len
, unsigned char *buffer
)
686 BIO_printf(out
, "static unsigned char %s_%d[] = {", var
, len
);
688 BIO_printf(out
, "\n\t0x00");
693 l
= BN_bn2bin(in
, buffer
);
694 for (i
=0; i
<l
-1; i
++)
697 BIO_printf(out
, "\n\t");
698 BIO_printf(out
, "0x%02X,", buffer
[i
]);
701 BIO_printf(out
, "\n\t");
702 BIO_printf(out
, "0x%02X", buffer
[i
]);
704 BIO_printf(out
, "\n\t};\n\n");