mark all block comments that need format preserving so that
[openssl.git] / apps / ecparam.c
1 /* apps/ecparam.c */
2 /*
3  * Written by Nils Larsch for the OpenSSL project.
4  */
5 /* ====================================================================
6  * Copyright (c) 1998-2005 The OpenSSL Project.  All rights reserved.
7  *
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  *
57  */
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>
73 #ifndef OPENSSL_NO_EC
74 #include <assert.h>
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
87 #undef PROG
88 #define PROG    ecparam_main
89
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  */
114
115
116 static int ecparam_print_var(BIO *,BIGNUM *,const char *,int,unsigned char *);
117
118 int MAIN(int, char **);
119
120 int MAIN(int argc, char **argv)
121         {
122         EC_GROUP *group = NULL;
123         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;
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;
131         BIO     *in = NULL, *out = NULL;
132         int     informat, outformat, noout = 0, C = 0, ret = 1;
133         char    *engine = NULL;
134
135         BIGNUM  *ec_p = NULL, *ec_a = NULL, *ec_b = NULL,
136                 *ec_gen = NULL, *ec_order = NULL, *ec_cofactor = NULL;
137         unsigned char *buffer = NULL;
138
139         apps_startup();
140
141         if (bio_err == NULL)
142                 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
143                         BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
144
145         if (!load_config(bio_err, NULL))
146                 goto end;
147
148         informat=FORMAT_PEM;
149         outformat=FORMAT_PEM;
150
151         prog=argv[0];
152         argc--;
153         argv++;
154         while (argc >= 1)
155                 {
156                 if      (strcmp(*argv,"-inform") == 0)
157                         {
158                         if (--argc < 1) goto bad;
159                         informat=str2fmt(*(++argv));
160                         }
161                 else if (strcmp(*argv,"-outform") == 0)
162                         {
163                         if (--argc < 1) goto bad;
164                         outformat=str2fmt(*(++argv));
165                         }
166                 else if (strcmp(*argv,"-in") == 0)
167                         {
168                         if (--argc < 1) goto bad;
169                         infile= *(++argv);
170                         }
171                 else if (strcmp(*argv,"-out") == 0)
172                         {
173                         if (--argc < 1) goto bad;
174                         outfile= *(++argv);
175                         }
176                 else if (strcmp(*argv,"-text") == 0)
177                         text = 1;
178                 else if (strcmp(*argv,"-C") == 0)
179                         C = 1;
180                 else if (strcmp(*argv,"-check") == 0)
181                         check = 1;
182                 else if (strcmp (*argv, "-name") == 0)
183                         {
184                         if (--argc < 1)
185                                 goto bad;
186                         curve_name = *(++argv);
187                         }
188                 else if (strcmp(*argv, "-list_curves") == 0)
189                         list_curves = 1;
190                 else if (strcmp(*argv, "-conv_form") == 0)
191                         {
192                         if (--argc < 1)
193                                 goto bad;
194                         ++argv;
195                         new_form = 1;
196                         if (strcmp(*argv, "compressed") == 0)
197                                 form = POINT_CONVERSION_COMPRESSED;
198                         else if (strcmp(*argv, "uncompressed") == 0)
199                                 form = POINT_CONVERSION_UNCOMPRESSED;
200                         else if (strcmp(*argv, "hybrid") == 0)
201                                 form = POINT_CONVERSION_HYBRID;
202                         else
203                                 goto bad;
204                         }
205                 else if (strcmp(*argv, "-param_enc") == 0)
206                         {
207                         if (--argc < 1)
208                                 goto bad;
209                         ++argv;
210                         new_asn1_flag = 1;
211                         if (strcmp(*argv, "named_curve") == 0)
212                                 asn1_flag = OPENSSL_EC_NAMED_CURVE;
213                         else if (strcmp(*argv, "explicit") == 0)
214                                 asn1_flag = 0;
215                         else
216                                 goto bad;
217                         }
218                 else if (strcmp(*argv, "-no_seed") == 0)
219                         no_seed = 1;
220                 else if (strcmp(*argv, "-noout") == 0)
221                         noout=1;
222                 else if (strcmp(*argv,"-genkey") == 0)
223                         {
224                         genkey=1;
225                         need_rand=1;
226                         }
227                 else if (strcmp(*argv, "-rand") == 0)
228                         {
229                         if (--argc < 1) goto bad;
230                         inrand= *(++argv);
231                         need_rand=1;
232                         }
233                 else if(strcmp(*argv, "-engine") == 0)
234                         {
235                         if (--argc < 1) goto bad;
236                         engine = *(++argv);
237                         }       
238                 else
239                         {
240                         BIO_printf(bio_err,"unknown option %s\n",*argv);
241                         badops=1;
242                         break;
243                         }
244                 argc--;
245                 argv++;
246                 }
247
248         if (badops)
249                 {
250 bad:
251                 BIO_printf(bio_err, "%s [options] <infile >outfile\n",prog);
252                 BIO_printf(bio_err, "where options are\n");
253                 BIO_printf(bio_err, " -inform arg       input format - "
254                                 "default PEM (DER or PEM)\n");
255                 BIO_printf(bio_err, " -outform arg      output format - "
256                                 "default PEM\n");
257                 BIO_printf(bio_err, " -in  arg          input file  - "
258                                 "default stdin\n");
259                 BIO_printf(bio_err, " -out arg          output file - "
260                                 "default stdout\n");
261                 BIO_printf(bio_err, " -noout            do not print the "
262                                 "ec parameter\n");
263                 BIO_printf(bio_err, " -text             print the ec "
264                                 "parameters in text form\n");
265                 BIO_printf(bio_err, " -check            validate the ec "
266                                 "parameters\n");
267                 BIO_printf(bio_err, " -C                print a 'C' "
268                                 "function creating the parameters\n");
269                 BIO_printf(bio_err, " -name arg         use the "
270                                 "ec parameters with 'short name' name\n");
271                 BIO_printf(bio_err, " -list_curves      prints a list of "
272                                 "all currently available curve 'short names'\n");
273                 BIO_printf(bio_err, " -conv_form arg    specifies the "
274                                 "point conversion form \n");
275                 BIO_printf(bio_err, "                   possible values:"
276                                 " compressed\n");
277                 BIO_printf(bio_err, "                                   "
278                                 " uncompressed (default)\n");
279                 BIO_printf(bio_err, "                                   "
280                                 " hybrid\n");
281                 BIO_printf(bio_err, " -param_enc arg    specifies the way"
282                                 " the ec parameters are encoded\n");
283                 BIO_printf(bio_err, "                   in the asn1 der "
284                                 "encoding\n");
285                 BIO_printf(bio_err, "                   possible values:"
286                                 " named_curve (default)\n");
287                 BIO_printf(bio_err, "                                   "
288                                 " explicit\n");
289                 BIO_printf(bio_err, " -no_seed          if 'explicit'"
290                                 " parameters are chosen do not"
291                                 " use the seed\n");
292                 BIO_printf(bio_err, " -genkey           generate ec"
293                                 " key\n");
294                 BIO_printf(bio_err, " -rand file        files to use for"
295                                 " random number input\n");
296                 BIO_printf(bio_err, " -engine e         use engine e, "
297                                 "possibly a hardware device\n");
298                 goto end;
299                 }
300
301         ERR_load_crypto_strings();
302
303         in=BIO_new(BIO_s_file());
304         out=BIO_new(BIO_s_file());
305         if ((in == NULL) || (out == NULL))
306                 {
307                 ERR_print_errors(bio_err);
308                 goto end;
309                 }
310
311         if (infile == NULL)
312                 BIO_set_fp(in,stdin,BIO_NOCLOSE);
313         else
314                 {
315                 if (BIO_read_filename(in,infile) <= 0)
316                         {
317                         perror(infile);
318                         goto end;
319                         }
320                 }
321         if (outfile == NULL)
322                 {
323                 BIO_set_fp(out,stdout,BIO_NOCLOSE);
324 #ifdef OPENSSL_SYS_VMS
325                 {
326                 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
327                 out = BIO_push(tmpbio, out);
328                 }
329 #endif
330                 }
331         else
332                 {
333                 if (BIO_write_filename(out,outfile) <= 0)
334                         {
335                         perror(outfile);
336                         goto end;
337                         }
338                 }
339
340 #ifndef OPENSSL_NO_ENGINE
341         setup_engine(bio_err, engine, 0);
342 #endif
343
344         if (list_curves)
345                 {
346                 EC_builtin_curve *curves = NULL;
347                 size_t crv_len = 0;
348                 size_t n = 0;
349
350                 crv_len = EC_get_builtin_curves(NULL, 0);
351
352                 curves = OPENSSL_malloc((int)(sizeof(EC_builtin_curve) * crv_len));
353
354                 if (curves == NULL)
355                         goto end;
356
357                 if (!EC_get_builtin_curves(curves, crv_len))
358                         {
359                         OPENSSL_free(curves);
360                         goto end;
361                         }
362
363                 
364                 for (n = 0; n < crv_len; n++)
365                         {
366                         const char *comment;
367                         const char *sname;
368                         comment = curves[n].comment;
369                         sname   = OBJ_nid2sn(curves[n].nid);
370                         if (comment == NULL)
371                                 comment = "CURVE DESCRIPTION NOT AVAILABLE";
372                         if (sname == NULL)
373                                 sname = "";
374
375                         BIO_printf(out, "  %-10s: ", sname);
376                         BIO_printf(out, "%s\n", comment);
377                         } 
378
379                 OPENSSL_free(curves);
380                 ret = 0;
381                 goto end;
382                 }
383
384         if (curve_name != NULL)
385                 {
386                 int nid;
387
388                 /* workaround for the SECG curve names secp192r1
389                  * and secp256r1 (which are the same as the curves
390                  * prime192v1 and prime256v1 defined in X9.62)
391                  */
392                 if (!strcmp(curve_name, "secp192r1"))
393                         {
394                         BIO_printf(bio_err, "using curve name prime192v1 "
395                                 "instead of secp192r1\n");
396                         nid = NID_X9_62_prime192v1;
397                         }
398                 else if (!strcmp(curve_name, "secp256r1"))
399                         {
400                         BIO_printf(bio_err, "using curve name prime256v1 "
401                                 "instead of secp256r1\n");
402                         nid = NID_X9_62_prime256v1;
403                         }
404                 else
405                         nid = OBJ_sn2nid(curve_name);
406
407                 if (nid == 0)
408                         nid = EC_curve_nist2nid(curve_name);
409         
410                 if (nid == 0)
411                         {
412                         BIO_printf(bio_err, "unknown curve name (%s)\n", 
413                                 curve_name);
414                         goto end;
415                         }
416
417                 group = EC_GROUP_new_by_curve_name(nid);
418                 if (group == NULL)
419                         {
420                         BIO_printf(bio_err, "unable to create curve (%s)\n", 
421                                 curve_name);
422                         goto end;
423                         }
424                 EC_GROUP_set_asn1_flag(group, asn1_flag);
425                 EC_GROUP_set_point_conversion_form(group, form);
426                 }
427         else if (informat == FORMAT_ASN1)
428                 {
429                 group = d2i_ECPKParameters_bio(in, NULL);
430                 }
431         else if (informat == FORMAT_PEM)
432                 {
433                 group = PEM_read_bio_ECPKParameters(in,NULL,NULL,NULL);
434                 }
435         else
436                 {
437                 BIO_printf(bio_err, "bad input format specified\n");
438                 goto end;
439                 }
440
441         if (group == NULL)
442                 {
443                 BIO_printf(bio_err, 
444                         "unable to load elliptic curve parameters\n");
445                 ERR_print_errors(bio_err);
446                 goto end;
447                 }
448
449         if (new_form)
450                 EC_GROUP_set_point_conversion_form(group, form);
451
452         if (new_asn1_flag)
453                 EC_GROUP_set_asn1_flag(group, asn1_flag);
454
455         if (no_seed)
456                 {
457                 EC_GROUP_set_seed(group, NULL, 0);
458                 }
459
460         if (text)
461                 {
462                 if (!ECPKParameters_print(out, group, 0))
463                         goto end;
464                 }
465
466         if (check)
467                 {
468                 if (group == NULL)
469                         BIO_printf(bio_err, "no elliptic curve parameters\n");
470                 BIO_printf(bio_err, "checking elliptic curve parameters: ");
471                 if (!EC_GROUP_check(group, NULL))
472                         {
473                         BIO_printf(bio_err, "failed\n");
474                         ERR_print_errors(bio_err);
475                         }
476                 else
477                         BIO_printf(bio_err, "ok\n");
478                         
479                 }
480
481         if (C)
482                 {
483                 size_t  buf_len = 0, tmp_len = 0;
484                 const EC_POINT *point;
485                 int     is_prime, len = 0;
486                 const EC_METHOD *meth = EC_GROUP_method_of(group);
487
488                 if ((ec_p = BN_new()) == NULL || (ec_a = BN_new()) == NULL ||
489                     (ec_b = BN_new()) == NULL || (ec_gen = BN_new()) == NULL ||
490                     (ec_order = BN_new()) == NULL || 
491                     (ec_cofactor = BN_new()) == NULL )
492                         {
493                         perror("OPENSSL_malloc");
494                         goto end;
495                         }
496
497                 is_prime = (EC_METHOD_get_field_type(meth) == 
498                         NID_X9_62_prime_field);
499
500                 if (is_prime)
501                         {
502                         if (!EC_GROUP_get_curve_GFp(group, ec_p, ec_a,
503                                 ec_b, NULL))
504                                 goto end;
505                         }
506                 else
507                         {
508                         /* TODO */
509                         goto end;
510                         }
511
512                 if ((point = EC_GROUP_get0_generator(group)) == NULL)
513                         goto end;
514                 if (!EC_POINT_point2bn(group, point, 
515                         EC_GROUP_get_point_conversion_form(group), ec_gen, 
516                         NULL))
517                         goto end;
518                 if (!EC_GROUP_get_order(group, ec_order, NULL))
519                         goto end;
520                 if (!EC_GROUP_get_cofactor(group, ec_cofactor, NULL))
521                         goto end;
522
523                 if (!ec_p || !ec_a || !ec_b || !ec_gen || 
524                         !ec_order || !ec_cofactor)
525                         goto end;
526
527                 len = BN_num_bits(ec_order);
528
529                 if ((tmp_len = (size_t)BN_num_bytes(ec_p)) > buf_len)
530                         buf_len = tmp_len;
531                 if ((tmp_len = (size_t)BN_num_bytes(ec_a)) > buf_len)
532                         buf_len = tmp_len;
533                 if ((tmp_len = (size_t)BN_num_bytes(ec_b)) > buf_len)
534                         buf_len = tmp_len;
535                 if ((tmp_len = (size_t)BN_num_bytes(ec_gen)) > buf_len)
536                         buf_len = tmp_len;
537                 if ((tmp_len = (size_t)BN_num_bytes(ec_order)) > buf_len)
538                         buf_len = tmp_len;
539                 if ((tmp_len = (size_t)BN_num_bytes(ec_cofactor)) > buf_len)
540                         buf_len = tmp_len;
541
542                 buffer = (unsigned char *)OPENSSL_malloc(buf_len);
543
544                 if (buffer == NULL)
545                         {
546                         perror("OPENSSL_malloc");
547                         goto end;
548                         }
549
550                 ecparam_print_var(out, ec_p, "ec_p", len, buffer);
551                 ecparam_print_var(out, ec_a, "ec_a", len, buffer);
552                 ecparam_print_var(out, ec_b, "ec_b", len, buffer);
553                 ecparam_print_var(out, ec_gen, "ec_gen", len, buffer);
554                 ecparam_print_var(out, ec_order, "ec_order", len, buffer);
555                 ecparam_print_var(out, ec_cofactor, "ec_cofactor", len, 
556                         buffer);
557
558                 BIO_printf(out, "\n\n");
559
560                 BIO_printf(out, "EC_GROUP *get_ec_group_%d(void)\n\t{\n", len);
561                 BIO_printf(out, "\tint ok=0;\n");
562                 BIO_printf(out, "\tEC_GROUP *group = NULL;\n");
563                 BIO_printf(out, "\tEC_POINT *point = NULL;\n");
564                 BIO_printf(out, "\tBIGNUM   *tmp_1 = NULL, *tmp_2 = NULL, "
565                                 "*tmp_3 = NULL;\n\n");
566                 BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_p_%d, "
567                                 "sizeof(ec_p_%d), NULL)) == NULL)\n\t\t"
568                                 "goto err;\n", len, len);
569                 BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_a_%d, "
570                                 "sizeof(ec_a_%d), NULL)) == NULL)\n\t\t"
571                                 "goto err;\n", len, len);
572                 BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_b_%d, "
573                                 "sizeof(ec_b_%d), NULL)) == NULL)\n\t\t"
574                                 "goto err;\n", len, len);
575                 if (is_prime)
576                         {
577                         BIO_printf(out, "\tif ((group = EC_GROUP_new_curve_"
578                                 "GFp(tmp_1, tmp_2, tmp_3, NULL)) == NULL)"
579                                 "\n\t\tgoto err;\n\n");
580                         }
581                 else
582                         {
583                         /* TODO */
584                         goto end;
585                         }
586                 BIO_printf(out, "\t/* build generator */\n");
587                 BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_gen_%d, "
588                                 "sizeof(ec_gen_%d), tmp_1)) == NULL)"
589                                 "\n\t\tgoto err;\n", len, len);
590                 BIO_printf(out, "\tpoint = EC_POINT_bn2point(group, tmp_1, "
591                                 "NULL, NULL);\n");
592                 BIO_printf(out, "\tif (point == NULL)\n\t\tgoto err;\n");
593                 BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_order_%d, "
594                                 "sizeof(ec_order_%d), tmp_2)) == NULL)"
595                                 "\n\t\tgoto err;\n", len, len);
596                 BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_cofactor_%d, "
597                                 "sizeof(ec_cofactor_%d), tmp_3)) == NULL)"
598                                 "\n\t\tgoto err;\n", len, len);
599                 BIO_printf(out, "\tif (!EC_GROUP_set_generator(group, point,"
600                                 " tmp_2, tmp_3))\n\t\tgoto err;\n");
601                 BIO_printf(out, "\n\tok=1;\n");
602                 BIO_printf(out, "err:\n");
603                 BIO_printf(out, "\tif (tmp_1)\n\t\tBN_free(tmp_1);\n");
604                 BIO_printf(out, "\tif (tmp_2)\n\t\tBN_free(tmp_2);\n");
605                 BIO_printf(out, "\tif (tmp_3)\n\t\tBN_free(tmp_3);\n");
606                 BIO_printf(out, "\tif (point)\n\t\tEC_POINT_free(point);\n");
607                 BIO_printf(out, "\tif (!ok)\n");
608                 BIO_printf(out, "\t\t{\n");
609                 BIO_printf(out, "\t\tEC_GROUP_free(group);\n");
610                 BIO_printf(out, "\t\tgroup = NULL;\n");
611                 BIO_printf(out, "\t\t}\n");
612                 BIO_printf(out, "\treturn(group);\n\t}\n");
613         }
614
615         if (!noout)
616                 {
617                 if (outformat == FORMAT_ASN1)
618                         i = i2d_ECPKParameters_bio(out, group);
619                 else if (outformat == FORMAT_PEM)
620                         i = PEM_write_bio_ECPKParameters(out, group);
621                 else    
622                         {
623                         BIO_printf(bio_err,"bad output format specified for"
624                                 " outfile\n");
625                         goto end;
626                         }
627                 if (!i)
628                         {
629                         BIO_printf(bio_err, "unable to write elliptic "
630                                 "curve parameters\n");
631                         ERR_print_errors(bio_err);
632                         goto end;
633                         }
634                 }
635         
636         if (need_rand)
637                 {
638                 app_RAND_load_file(NULL, bio_err, (inrand != NULL));
639                 if (inrand != NULL)
640                         BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
641                                 app_RAND_load_files(inrand));
642                 }
643
644         if (genkey)
645                 {
646                 EC_KEY *eckey = EC_KEY_new();
647
648                 if (eckey == NULL)
649                         goto end;
650
651                 assert(need_rand);
652
653                 if (EC_KEY_set_group(eckey, group) == 0)
654                         goto end;
655                 
656                 if (!EC_KEY_generate_key(eckey))
657                         {
658                         EC_KEY_free(eckey);
659                         goto end;
660                         }
661                 if (outformat == FORMAT_ASN1)
662                         i = i2d_ECPrivateKey_bio(out, eckey);
663                 else if (outformat == FORMAT_PEM)
664                         i = PEM_write_bio_ECPrivateKey(out, eckey, NULL,
665                                 NULL, 0, NULL, NULL);
666                 else    
667                         {
668                         BIO_printf(bio_err, "bad output format specified "
669                                 "for outfile\n");
670                         EC_KEY_free(eckey);
671                         goto end;
672                         }
673                 EC_KEY_free(eckey);
674                 }
675
676         if (need_rand)
677                 app_RAND_write_file(NULL, bio_err);
678
679         ret=0;
680 end:
681         if (ec_p)
682                 BN_free(ec_p);
683         if (ec_a)
684                 BN_free(ec_a);
685         if (ec_b)
686                 BN_free(ec_b);
687         if (ec_gen)
688                 BN_free(ec_gen);
689         if (ec_order)
690                 BN_free(ec_order);
691         if (ec_cofactor)
692                 BN_free(ec_cofactor);
693         if (buffer)
694                 OPENSSL_free(buffer);
695         if (in != NULL)
696                 BIO_free(in);
697         if (out != NULL)
698                 BIO_free_all(out);
699         if (group != NULL)
700                 EC_GROUP_free(group);
701         apps_shutdown();
702         OPENSSL_EXIT(ret);
703 }
704
705 static int ecparam_print_var(BIO *out, BIGNUM *in, const char *var,
706         int len, unsigned char *buffer)
707         {
708         BIO_printf(out, "static unsigned char %s_%d[] = {", var, len);
709         if (BN_is_zero(in))
710                 BIO_printf(out, "\n\t0x00");
711         else 
712                 {
713                 int i, l;
714
715                 l = BN_bn2bin(in, buffer);
716                 for (i=0; i<l-1; i++)
717                         {
718                         if ((i%12) == 0) 
719                                 BIO_printf(out, "\n\t");
720                         BIO_printf(out, "0x%02X,", buffer[i]);
721                         }
722                 if ((i%12) == 0) 
723                         BIO_printf(out, "\n\t");
724                 BIO_printf(out, "0x%02X", buffer[i]);
725                 }
726         BIO_printf(out, "\n\t};\n\n");
727         return 1;
728         }
729 #else /* !OPENSSL_NO_EC */
730
731 # if PEDANTIC
732 static void *dummy=&dummy;
733 # endif
734
735 #endif