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