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