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