extend curve list (additional curves over binary fields)
[openssl.git] / apps / ecparam.c
1 /* apps/ecparam.c */
2 /* ====================================================================
3  * Copyright (c) 1998-2002 The OpenSSL Project.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer. 
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the
15  *    distribution.
16  *
17  * 3. All advertising materials mentioning features or use of this
18  *    software must display the following acknowledgment:
19  *    "This product includes software developed by the OpenSSL Project
20  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21  *
22  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23  *    endorse or promote products derived from this software without
24  *    prior written permission. For written permission, please contact
25  *    openssl-core@openssl.org.
26  *
27  * 5. Products derived from this software may not be called "OpenSSL"
28  *    nor may "OpenSSL" appear in their names without prior written
29  *    permission of the OpenSSL Project.
30  *
31  * 6. Redistributions of any form whatsoever must retain the following
32  *    acknowledgment:
33  *    "This product includes software developed by the OpenSSL Project
34  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47  * OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This product includes cryptographic software written by Eric Young
51  * (eay@cryptsoft.com).  This product includes software written by Tim
52  * Hudson (tjh@cryptsoft.com).
53  *
54  */
55 /* ====================================================================
56  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
57  *
58  * Portions of the attached software ("Contribution") are developed by 
59  * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
60  *
61  * The Contribution is licensed pursuant to the OpenSSL open source
62  * license provided above.
63  *
64  * In addition, Sun covenants to all licensees who provide a reciprocal
65  * covenant with respect to their own patents if any, not to sue under
66  * current and future patent claims necessarily infringed by the making,
67  * using, practicing, selling, offering for sale and/or otherwise
68  * disposing of the Contribution as delivered hereunder 
69  * (or portions thereof), provided that such covenant shall not apply:
70  *  1) for code that a licensee deletes from the Contribution;
71  *  2) separates from the Contribution; or
72  *  3) for infringements caused by:
73  *       i) the modification of the Contribution or
74  *      ii) the combination of the Contribution with other software or
75  *          devices where such combination causes the infringement.
76  *
77  * The elliptic curve binary polynomial software is originally written by 
78  * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
79  *
80  */
81 #ifndef OPENSSL_NO_ECDSA
82 #include <assert.h>
83 #include <stdio.h>
84 #include <stdlib.h>
85 #include <time.h>
86 #include <string.h>
87 #include "apps.h"
88 #include <openssl/bio.h>
89 #include <openssl/err.h>
90 #include <openssl/bn.h>
91 #include <openssl/ec.h>
92 #include <openssl/ecdsa.h>
93 #include <openssl/x509.h>
94 #include <openssl/pem.h>
95
96 #undef PROG
97 #define PROG    ecparam_main
98
99 /* -inform arg            - input format - default PEM (DER or PEM)
100  * -outform arg           - output format - default PEM
101  * -in arg                - input file  - default stdin
102  * -out arg               - output file - default stdout
103  * -noout
104  * -text
105  * -check                 - validate the ec parameters
106  * -C
107  * -noout
108  * -name file             - use the ecparameters with 'short name' name
109  * -list_curves           - prints a list of all currently available curve
110  *                          'short names' and exits
111  * -conv_form                  - specifies the point conversion form 
112  *                          possible values : compressed
113  *                                            uncompressed (default)
114  *                                            hybrid
115  * -param_enc             - specifies the way the ec parameters are encoded
116  *                          in the asn1 der encoding
117  *                          possilbe values : named_curve (default)
118  *                                            explicit
119  * -no_seed               - if 'explicit' parameters are choosen do not
120  *                          use the seed
121  * -genkey                - generates a ecdsa private key
122  * -rand file
123  * -engine e              - use engine e, possible a hardware device
124  */
125
126 static const char *curve_list[67] = {
127         "prime192v1   - 192 bit prime curve from the X9.62 draft",
128         "prime192v2   - 192 bit prime curve from the X9.62 draft",
129         "prime192v3   - 192 bit prime curve from the X9.62 draft",
130         "prime239v1   - 239 bit prime curve from the X9.62 draft",
131         "prime239v2   - 239 bit prime curve from the X9.62 draft",
132         "prime239v3   - 239 bit prime curve from the X9.62 draft", 
133         "prime256v1   - 256 bit prime curve from the X9.62 draft", 
134         "secp112r1    - SECG recommended curve over a 112 bit prime field", 
135         "secp112r2    - SECG recommended curve over a 112 bit prime field", 
136         "secp128r1    - SECG recommended curve over a 128 bit prime field",
137         "secp128r2    - SECG recommended curve over a 128 bit prime field", 
138         "secp160k1    - SECG recommended curve over a 160 bit prime field", 
139         "secp160r1    - SECG recommended curve over a 160 bit prime field", 
140         "secp160r2    - SECG recommended curve over a 160 bit prime field", 
141         "secp192k1    - SECG recommended curve over a 192 bit prime field",
142         "prime192v1   - SECG recommended curve over a 192 bit prime field (aka secp192r1)",
143         "secp224k1    - SECG recommended curve over a 224 bit prime field", 
144         "secp224r1    - SECG/NIST recommended curve over a 224 bit prime field", 
145         "secp256k1    - SECG recommended curve over a 256 bit prime field",
146         "prime256v1   - SECG recommended curve over a 256 bit prime field (aka secp256r1)",
147         "secp384r1    - SECG/NIST recommended curve over a 384 bit prime field", 
148         "secp521r1    - SECG/NIST recommended curve over a 521 bit prime field",
149         "wap-wsg-idm-ecid-wtls6  - 112 bit prime curve from the WTLS standard",
150         "wap-wsg-idm-ecid-wtls8  - 112 bit prime curve from the WTLS standard",
151         "wap-wsg-idm-ecid-wtls7  - 160 bit prime curve from the WTLS standard",
152         "wap-wsg-idm-ecid-wtls9  - 160 bit prime curve from the WTLS standard",
153         "wap-wsg-idm-ecid-wtls12 - 224 bit prime curve from the WTLS standard",
154         "c2pnb163v1   - 163 bit binary curve from the X9.62 draft",
155         "c2pnb163v2   - 163 bit binary curve from the X9.62 draft",
156         "c2pnb163v3   - 163 bit binary curve from the X9.62 draft",
157         "c2pnb176v1   - 176 bit binary curve from the X9.62 draft",
158         "c2tnb191v1   - 191 bit binary curve from the X9.62 draft",
159         "c2tnb191v2   - 191 bit binary curve from the X9.62 draft",
160         "c2tnb191v3   - 191 bit binary curve from the X9.62 draft",
161         "c2pnb208w1   - 208 bit binary curve from the X9.62 draft",
162         "c2tnb239v1   - 239 bit binary curve from the X9.62 draft",
163         "c2tnb239v2   - 239 bit binary curve from the X9.62 draft",
164         "c2tnb239v3   - 239 bit binary curve from the X9.62 draft",
165         "c2pnb272w1   - 272 bit binary curve from the X9.62 draft",
166         "c2pnb304w1   - 304 bit binary curve from the X9.62 draft",
167         "c2tnb359v1   - 359 bit binary curve from the X9.62 draft",
168         "c2pnb368w1   - 368 bit binary curve from the X9.62 draft",
169         "c2tnb431r1   - 431 bit binary curve from the X9.62 draft",
170         "sect113r1    - SECG recommended curve over a 113 bit binary field",
171         "sect113r2    - SECG recommended curve over a 113 bit binary field",
172         "sect131r1    - SECG recommended curve over a 131 bit binary field",
173         "sect131r2    - SECG recommended curve over a 131 bit binary field",
174         "sect163k1    - SECG/NIST recommended curve over a 163 bit binary field",
175         "sect163r1    - SECG recommended curve over a 163 bit binary field",
176         "sect163r2    - SECG/NIST recommended curve over a 163 bit binary field",
177         "sect193r1    - SECG recommended curve over a 193 bit binary field",
178         "sect193r2    - SECG recommended curve over a 193 bit binary field",
179         "sect233k1    - SECG/NIST recommended curve over a 233 bit binary field",
180         "sect233r1    - SECG/NIST recommended curve over a 233 bit binary field",
181         "sect239k1    - SECG recommended curve over a 239 bit binary field",
182         "sect283k1    - SECG/NIST recommended curve over a 283 bit binary field",
183         "sect283r1    - SECG/NIST recommended curve over a 283 bit binary field",
184         "sect409k1    - SECG/NIST recommended curve over a 409 bit binary field",
185         "sect409r1    - SECG/NIST recommended curve over a 409 bit binary field",
186         "sect571k1    - SECG/NIST recommended curve over a 571 bit binary field",
187         "sect571r1    - SECG/NIST recommended curve over a 571 bit binary field",
188         "wap-wsg-idm-ecid-wtls1  - 113 bit binary curve from the WTLS standard",
189         "wap-wsg-idm-ecid-wtls4  - 113 bit binary curve from the WTLS standard",
190         "wap-wsg-idm-ecid-wtls3  - 163 bit binary curve from the WTLS standard",
191         "wap-wsg-idm-ecid-wtls5  - 163 bit binary curve from the WTLS standard",
192         "wap-wsg-idm-ecid-wtls10 - 233 bit binary curve from the WTLS standard",
193         "wap-wsg-idm-ecid-wtls11 - 233 bit binary curve from the WTLS standard"
194 };
195
196 static int ecparam_print_var(BIO *,BIGNUM *,const char *,int,unsigned char *);
197
198 int MAIN(int, char **);
199
200 int MAIN(int argc, char **argv)
201         {
202         EC_GROUP *group = NULL;
203         point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED; 
204         int     new_form = 0;
205         int     asn1_flag = OPENSSL_EC_NAMED_CURVE;
206         int     new_asn1_flag = 0;
207         char    *curve_name = NULL, *inrand = NULL;
208         int     list_curves = 0, no_seed = 0, check = 0,
209                 badops = 0, text = 0, i, need_rand = 0, genkey = 0;
210         char    *infile = NULL, *outfile = NULL, *prog;
211         BIO     *in = NULL, *out = NULL;
212         int     informat, outformat, noout = 0, C = 0, ret = 1;
213         ENGINE  *e = NULL;
214         char    *engine = NULL;
215
216         BIGNUM  *ec_p = NULL, *ec_a = NULL, *ec_b = NULL,
217                 *ec_gen = NULL, *ec_order = NULL, *ec_cofactor = NULL;
218         unsigned char *buffer = NULL;
219
220         apps_startup();
221
222         if (bio_err == NULL)
223                 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
224                         BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
225
226         if (!load_config(bio_err, NULL))
227                 goto end;
228
229         informat=FORMAT_PEM;
230         outformat=FORMAT_PEM;
231
232         prog=argv[0];
233         argc--;
234         argv++;
235         while (argc >= 1)
236                 {
237                 if      (strcmp(*argv,"-inform") == 0)
238                         {
239                         if (--argc < 1) goto bad;
240                         informat=str2fmt(*(++argv));
241                         }
242                 else if (strcmp(*argv,"-outform") == 0)
243                         {
244                         if (--argc < 1) goto bad;
245                         outformat=str2fmt(*(++argv));
246                         }
247                 else if (strcmp(*argv,"-in") == 0)
248                         {
249                         if (--argc < 1) goto bad;
250                         infile= *(++argv);
251                         }
252                 else if (strcmp(*argv,"-out") == 0)
253                         {
254                         if (--argc < 1) goto bad;
255                         outfile= *(++argv);
256                         }
257                 else if (strcmp(*argv,"-text") == 0)
258                         text = 1;
259                 else if (strcmp(*argv,"-C") == 0)
260                         C = 1;
261                 else if (strcmp(*argv,"-check") == 0)
262                         check = 1;
263                 else if (strcmp (*argv, "-name") == 0)
264                         {
265                         if (--argc < 1)
266                                 goto bad;
267                         curve_name = *(++argv);
268                         }
269                 else if (strcmp(*argv, "-list_curves") == 0)
270                         list_curves = 1;
271                 else if (strcmp(*argv, "-conv_form") == 0)
272                         {
273                         if (--argc < 1)
274                                 goto bad;
275                         ++argv;
276                         new_form = 1;
277                         if (strcmp(*argv, "compressed") == 0)
278                                 form = POINT_CONVERSION_COMPRESSED;
279                         else if (strcmp(*argv, "uncompressed") == 0)
280                                 form = POINT_CONVERSION_UNCOMPRESSED;
281                         else if (strcmp(*argv, "hybrid") == 0)
282                                 form = POINT_CONVERSION_HYBRID;
283                         else
284                                 goto bad;
285                         }
286                 else if (strcmp(*argv, "-param_enc") == 0)
287                         {
288                         if (--argc < 1)
289                                 goto bad;
290                         ++argv;
291                         new_asn1_flag = 1;
292                         if (strcmp(*argv, "named_curve") == 0)
293                                 asn1_flag = OPENSSL_EC_NAMED_CURVE;
294                         else if (strcmp(*argv, "explicit") == 0)
295                                 asn1_flag = 0;
296                         else
297                                 goto bad;
298                         }
299                 else if (strcmp(*argv, "-no_seed") == 0)
300                         no_seed = 1;
301                 else if (strcmp(*argv, "-noout") == 0)
302                         noout=1;
303                 else if (strcmp(*argv,"-genkey") == 0)
304                         {
305                         genkey=1;
306                         need_rand=1;
307                         }
308                 else if (strcmp(*argv, "-rand") == 0)
309                         {
310                         if (--argc < 1) goto bad;
311                         inrand= *(++argv);
312                         need_rand=1;
313                         }
314                 else if(strcmp(*argv, "-engine") == 0)
315                         {
316                         if (--argc < 1) goto bad;
317                         engine = *(++argv);
318                         }       
319                 else
320                         {
321                         BIO_printf(bio_err,"unknown option %s\n",*argv);
322                         badops=1;
323                         break;
324                         }
325                 argc--;
326                 argv++;
327                 }
328
329         if (badops)
330                 {
331 bad:
332                 BIO_printf(bio_err, "%s [options] <infile >outfile\n",prog);
333                 BIO_printf(bio_err, "where options are\n");
334                 BIO_printf(bio_err, " -inform arg             input format - "
335                                 "default PEM (DER or PEM)\n");
336                 BIO_printf(bio_err, " -outform arg            output format - "
337                                 "default PEM\n");
338                 BIO_printf(bio_err, " -in  arg                input file  - "
339                                 "default stdin\n");
340                 BIO_printf(bio_err, " -out arg                output file - "
341                                 "default stdout\n");
342                 BIO_printf(bio_err, " -noout                  do not print the "
343                                 "ec parameter\n");
344                 BIO_printf(bio_err, " -text                   print the ec "
345                                 "parameters in text form\n");
346                 BIO_printf(bio_err, " -check                  validate the ec "
347                                 "parameters\n");
348                 BIO_printf(bio_err, " -C                      print a 'C' "
349                                 "function creating the parameters\n");
350                 BIO_printf(bio_err, " -name arg               use the "
351                                 "ec parameters with 'short name' name\n");
352                 BIO_printf(bio_err, " -list_curves            prints a list of "
353                                 "all currently available curve\n");
354                 BIO_printf(bio_err, "                         'short names'\n");
355                 BIO_printf(bio_err, " -conv_form arg          specifies the "
356                                 "point conversion form \n");
357                 BIO_printf(bio_err, "                         possible values :"
358                                 " compressed\n");
359                 BIO_printf(bio_err, "                                          "
360                                 " uncompressed (default)\n");
361                 BIO_printf(bio_err, "                                          "
362                                 " hybrid\n");
363                 BIO_printf(bio_err, " -param_enc arg          specifies the way"
364                                 " the ec parameters are encoded\n");
365                 BIO_printf(bio_err, "                         in the asn1 der "
366                                 "encoding\n");
367                 BIO_printf(bio_err, "                         possilbe values :"
368                                 " named_curve (default)\n");
369                 BIO_printf(bio_err,"                                      "
370                                 "     explicit\n");
371                 BIO_printf(bio_err, " -no_seed                if 'explicit'"
372                                 " parameters are choosen do not\n");
373                 BIO_printf(bio_err, "                         use the seed\n");
374                 BIO_printf(bio_err, " -genkey                 generate ecdsa"
375                                 " key\n");
376                 BIO_printf(bio_err, " -rand file              files to use for"
377                                 " random number input\n");
378                 BIO_printf(bio_err, " -engine e               use engine e, "
379                                 "possible a hardware device\n");
380                 goto end;
381                 }
382
383         ERR_load_crypto_strings();
384
385         in=BIO_new(BIO_s_file());
386         out=BIO_new(BIO_s_file());
387         if ((in == NULL) || (out == NULL))
388                 {
389                 ERR_print_errors(bio_err);
390                 goto end;
391                 }
392
393         if (infile == NULL)
394                 BIO_set_fp(in,stdin,BIO_NOCLOSE);
395         else
396                 {
397                 if (BIO_read_filename(in,infile) <= 0)
398                         {
399                         perror(infile);
400                         goto end;
401                         }
402                 }
403         if (outfile == NULL)
404                 {
405                 BIO_set_fp(out,stdout,BIO_NOCLOSE);
406 #ifdef OPENSSL_SYS_VMS
407                 {
408                 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
409                 out = BIO_push(tmpbio, out);
410                 }
411 #endif
412                 }
413         else
414                 {
415                 if (BIO_write_filename(out,outfile) <= 0)
416                         {
417                         perror(outfile);
418                         goto end;
419                         }
420                 }
421
422         e = setup_engine(bio_err, engine, 0);
423
424         if (list_curves)
425                 {
426                 int counter=0;
427
428                 for (; counter < sizeof(curve_list)/sizeof(char *); counter++)
429                         if (BIO_printf(bio_err, " %s\n", curve_list[counter]) 
430                                 <= 0) 
431                                 goto end;
432                 ret = 0;
433                 goto end;
434                 }
435
436         if (curve_name != NULL)
437                 {
438                 int nid = OBJ_sn2nid(curve_name);
439         
440                 if (nid == 0)
441                         {
442                         BIO_printf(bio_err, "unknown curve name (%s)\n", 
443                                 curve_name);
444                         goto end;
445                         }
446
447                 group = EC_GROUP_new_by_nid(nid);
448                 if (group == NULL)
449                         {
450                         BIO_printf(bio_err, "unable to create curve (%s)\n", 
451                                 curve_name);
452                         goto end;
453                         }
454                 EC_GROUP_set_asn1_flag(group, asn1_flag);
455                 EC_GROUP_set_point_conversion_form(group, form);
456                 }
457         else if (informat == FORMAT_ASN1)
458                 {
459                 group = d2i_ECPKParameters_bio(in, NULL);
460                 }
461         else if (informat == FORMAT_PEM)
462                 {
463                 group = PEM_read_bio_ECPKParameters(in,NULL,NULL,NULL);
464                 }
465         else
466                 {
467                 BIO_printf(bio_err, "bad input format specified\n");
468                 goto end;
469                 }
470
471         if (group == NULL)
472                 {
473                 BIO_printf(bio_err, 
474                         "unable to load elliptic curve parameters\n");
475                 ERR_print_errors(bio_err);
476                 goto end;
477                 }
478
479         if (new_form)
480                 EC_GROUP_set_point_conversion_form(group, form);
481
482         if (new_asn1_flag)
483                 EC_GROUP_set_asn1_flag(group, asn1_flag);
484
485         if (no_seed)
486                 {
487                 EC_GROUP_set_seed(group, NULL, 0);
488                 }
489
490         if (text)
491                 {
492                 if (!ECPKParameters_print(out, group, 0))
493                         goto end;
494                 }
495
496         if (check)
497                 {
498                 if (group == NULL)
499                         BIO_printf(bio_err, "no elliptic curve parameters\n");
500                 BIO_printf(bio_err, "checking elliptic curve parameters: ");
501                 if (!EC_GROUP_check(group, NULL))
502                         {
503                         BIO_printf(bio_err, "failed\n");
504                         ERR_print_errors(bio_err);
505                         }
506                 else
507                         BIO_printf(bio_err, "ok\n");
508                         
509                 }
510
511         if (C)
512                 {
513                 size_t  buf_len = 0, tmp_len = 0;
514                 const EC_POINT *point;
515                 int     is_prime, len = 0;
516                 const EC_METHOD *meth = EC_GROUP_method_of(group);
517
518                 if ((ec_p = BN_new()) == NULL || (ec_a = BN_new()) == NULL ||
519                     (ec_b = BN_new()) == NULL || (ec_gen = BN_new()) == NULL ||
520                     (ec_order = BN_new()) == NULL || 
521                     (ec_cofactor = BN_new()) == NULL )
522                         {
523                         perror("OPENSSL_malloc");
524                         goto end;
525                         }
526
527                 is_prime = (EC_METHOD_get_field_type(meth) == 
528                         NID_X9_62_prime_field);
529
530                 if (is_prime)
531                         {
532                         if (!EC_GROUP_get_curve_GFp(group, ec_p, ec_a,
533                                 ec_b, NULL))
534                                 goto end;
535                         }
536                 else
537                         {
538                         /* TODO */
539                         goto end;
540                         }
541
542                 if ((point = EC_GROUP_get0_generator(group)) == NULL)
543                         goto end;
544                 if (!EC_POINT_point2bn(group, point, 
545                         EC_GROUP_get_point_conversion_form(group), ec_gen, 
546                         NULL))
547                         goto end;
548                 if (!EC_GROUP_get_order(group, ec_order, NULL))
549                         goto end;
550                 if (!EC_GROUP_get_cofactor(group, ec_cofactor, NULL))
551                         goto end;
552
553                 if (!ec_p || !ec_a || !ec_b || !ec_gen || 
554                         !ec_order || !ec_cofactor)
555                         goto end;
556
557                 len = BN_num_bits(ec_order);
558
559                 if ((tmp_len = (size_t)BN_num_bytes(ec_p)) > buf_len)
560                         buf_len = tmp_len;
561                 if ((tmp_len = (size_t)BN_num_bytes(ec_a)) > buf_len)
562                         buf_len = tmp_len;
563                 if ((tmp_len = (size_t)BN_num_bytes(ec_b)) > buf_len)
564                         buf_len = tmp_len;
565                 if ((tmp_len = (size_t)BN_num_bytes(ec_gen)) > buf_len)
566                         buf_len = tmp_len;
567                 if ((tmp_len = (size_t)BN_num_bytes(ec_order)) > buf_len)
568                         buf_len = tmp_len;
569                 if ((tmp_len = (size_t)BN_num_bytes(ec_cofactor)) > buf_len)
570                         buf_len = tmp_len;
571
572                 buffer = (unsigned char *)OPENSSL_malloc(buf_len);
573
574                 if (buffer == NULL)
575                         {
576                         perror("OPENSSL_malloc");
577                         goto end;
578                         }
579
580                 ecparam_print_var(out, ec_p, "ec_p", len, buffer);
581                 ecparam_print_var(out, ec_a, "ec_a", len, buffer);
582                 ecparam_print_var(out, ec_b, "ec_b", len, buffer);
583                 ecparam_print_var(out, ec_gen, "ec_gen", len, buffer);
584                 ecparam_print_var(out, ec_order, "ec_order", len, buffer);
585                 ecparam_print_var(out, ec_cofactor, "ec_cofactor", len, 
586                         buffer);
587
588                 BIO_printf(out, "\n\n");
589
590                 BIO_printf(out, "EC_GROUP *get_ec_group_%d(void)\n\t{\n", len);
591                 BIO_printf(out, "\tint ok=0;\n");
592                 BIO_printf(out, "\tEC_GROUP *group = NULL;\n");
593                 BIO_printf(out, "\tEC_POINT *point = NULL;\n");
594                 BIO_printf(out, "\tBIGNUM   *tmp_1 = NULL, *tmp_2 = NULL, "
595                                 "*tmp_3 = NULL;\n\n");
596                 BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_p_%d, "
597                                 "sizeof(ec_p_%d), NULL)) == NULL)\n\t\t"
598                                 "goto err;\n", len, len);
599                 BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_a_%d, "
600                                 "sizeof(ec_a_%d), NULL)) == NULL)\n\t\t"
601                                 "goto err;\n", len, len);
602                 BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_b_%d, "
603                                 "sizeof(ec_b_%d), NULL)) == NULL)\n\t\t"
604                                 "goto err;\n", len, len);
605                 if (is_prime)
606                         {
607                         BIO_printf(out, "\tif ((group = EC_GROUP_new_curve_"
608                                 "GFp(tmp_1, tmp_2, tmp_3, NULL)) == NULL)"
609                                 "\n\t\tgoto err;\n\n");
610                         }
611                 else
612                         {
613                         /* TODO */
614                         goto end;
615                         }
616                 BIO_printf(out, "\t/* build generator */\n");
617                 BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_gen_%d, "
618                                 "sizeof(ec_gen_%d), tmp_1)) == NULL)"
619                                 "\n\t\tgoto err;\n", len, len);
620                 BIO_printf(out, "\tpoint = EC_POINT_bn2point(group, tmp_1, "
621                                 "NULL, NULL);\n");
622                 BIO_printf(out, "\tif (point == NULL)\n\t\tgoto err;\n");
623                 BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_order_%d, "
624                                 "sizeof(ec_order_%d), tmp_2)) == NULL)"
625                                 "\n\t\tgoto err;\n", len, len);
626                 BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_cofactor_%d, "
627                                 "sizeof(ec_cofactor_%d), tmp_3)) == NULL)"
628                                 "\n\t\tgoto err;\n", len, len);
629                 BIO_printf(out, "\tif (!EC_GROUP_set_generator(group, point,"
630                                 " tmp_2, tmp_3))\n\t\tgoto err;\n");
631                 BIO_printf(out, "\n\tok=1;\n");
632                 BIO_printf(out, "err:\n");
633                 BIO_printf(out, "\tif (tmp_1)\n\t\tBN_free(tmp_1);\n");
634                 BIO_printf(out, "\tif (tmp_2)\n\t\tBN_free(tmp_2);\n");
635                 BIO_printf(out, "\tif (tmp_3)\n\t\tBN_free(tmp_3);\n");
636                 BIO_printf(out, "\tif (point)\n\t\tEC_POINT_free(point);\n");
637                 BIO_printf(out, "\tif (!ok)\n");
638                 BIO_printf(out, "\t\t{\n");
639                 BIO_printf(out, "\t\tEC_GROUP_free(group);\n");
640                 BIO_printf(out, "\t\tgroup = NULL;\n");
641                 BIO_printf(out, "\t\t}\n");
642                 BIO_printf(out, "\treturn(group);\n\t}\n");
643         }
644
645         if (!noout)
646                 {
647                 if (outformat == FORMAT_ASN1)
648                         i = i2d_ECPKParameters_bio(out, group);
649                 else if (outformat == FORMAT_PEM)
650                         i = PEM_write_bio_ECPKParameters(out, group);
651                 else    
652                         {
653                         BIO_printf(bio_err,"bad output format specified for"
654                                 " outfile\n");
655                         goto end;
656                         }
657                 if (!i)
658                         {
659                         BIO_printf(bio_err, "unable to write elliptic "
660                                 "curve parameters\n");
661                         ERR_print_errors(bio_err);
662                         goto end;
663                         }
664                 }
665         
666         if (need_rand)
667                 {
668                 app_RAND_load_file(NULL, bio_err, (inrand != NULL));
669                 if (inrand != NULL)
670                         BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
671                                 app_RAND_load_files(inrand));
672                 }
673
674         if (genkey)
675                 {
676                 ECDSA *ecdsa = ECDSA_new();
677
678                 if (ecdsa == NULL)
679                         goto end;
680
681                 assert(need_rand);
682
683                 ecdsa->group = group;
684                 
685                 if (!ECDSA_generate_key(ecdsa))
686                         {
687                         ecdsa->group = NULL;
688                         ECDSA_free(ecdsa);
689                         goto end;
690                         }
691                 if (outformat == FORMAT_ASN1)
692                         i = i2d_ECDSAPrivateKey_bio(out, ecdsa);
693                 else if (outformat == FORMAT_PEM)
694                         i = PEM_write_bio_ECDSAPrivateKey(out, ecdsa, NULL,
695                                 NULL, 0, NULL, NULL);
696                 else    
697                         {
698                         BIO_printf(bio_err, "bad output format specified "
699                                 "for outfile\n");
700                         ecdsa->group = NULL;
701                         ECDSA_free(ecdsa);
702                         goto end;
703                         }
704                 ecdsa->group = NULL;
705                 ECDSA_free(ecdsa);
706                 }
707
708         if (need_rand)
709                 app_RAND_write_file(NULL, bio_err);
710
711         ret=0;
712 end:
713         if (ec_p)
714                 BN_free(ec_p);
715         if (ec_a)
716                 BN_free(ec_a);
717         if (ec_b)
718                 BN_free(ec_b);
719         if (ec_gen)
720                 BN_free(ec_gen);
721         if (ec_order)
722                 BN_free(ec_order);
723         if (ec_cofactor)
724                 BN_free(ec_cofactor);
725         if (buffer)
726                 OPENSSL_free(buffer);
727         if (in != NULL)
728                 BIO_free(in);
729         if (out != NULL)
730                 BIO_free_all(out);
731         if (group != NULL)
732                 EC_GROUP_free(group);
733         apps_shutdown();
734         EXIT(ret);
735 }
736
737 int ecparam_print_var(BIO *out, BIGNUM *in, const char *var,
738         int len, unsigned char *buffer)
739         {
740         BIO_printf(out, "static unsigned char %s_%d[] = {", var, len);
741         if (BN_is_zero(in))
742                 BIO_printf(out, "\n\t0x00");
743         else 
744                 {
745                 int i, l;
746
747                 l = BN_bn2bin(in, buffer);
748                 for (i=0; i<l-1; i++)
749                         {
750                         if ((i%12) == 0) 
751                                 BIO_printf(out, "\n\t");
752                         BIO_printf(out, "0x%02X,", buffer[i]);
753                         }
754                 if ((i%12) == 0) 
755                         BIO_printf(out, "\n\t");
756                 BIO_printf(out, "0x%02X", buffer[i]);
757                 }
758         BIO_printf(out, "\n\t};\n\n");
759         return 1;
760         }
761 #endif