1 /* apps/ecdsaparam.c */
2 /* ====================================================================
3 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
55 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
56 * All rights reserved.
58 * This package is an SSL implementation written
59 * by Eric Young (eay@cryptsoft.com).
60 * The implementation was written so as to conform with Netscapes SSL.
62 * This library is free for commercial and non-commercial use as long as
63 * the following conditions are aheared to. The following conditions
64 * apply to all code found in this distribution, be it the RC4, RSA,
65 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
66 * included with this distribution is covered by the same copyright terms
67 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
69 * Copyright remains Eric Young's, and as such any Copyright notices in
70 * the code are not to be removed.
71 * If this package is used in a product, Eric Young should be given attribution
72 * as the author of the parts of the library used.
73 * This can be in the form of a textual message at program startup or
74 * in documentation (online or textual) provided with the package.
76 * Redistribution and use in source and binary forms, with or without
77 * modification, are permitted provided that the following conditions
79 * 1. Redistributions of source code must retain the copyright
80 * notice, this list of conditions and the following disclaimer.
81 * 2. Redistributions in binary form must reproduce the above copyright
82 * notice, this list of conditions and the following disclaimer in the
83 * documentation and/or other materials provided with the distribution.
84 * 3. All advertising materials mentioning features or use of this software
85 * must display the following acknowledgement:
86 * "This product includes cryptographic software written by
87 * Eric Young (eay@cryptsoft.com)"
88 * The word 'cryptographic' can be left out if the rouines from the library
89 * being used are not cryptographic related :-).
90 * 4. If you include any Windows specific code (or a derivative thereof) from
91 * the apps directory (application code) you must include an acknowledgement:
92 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
94 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
95 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
97 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
98 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
99 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
100 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
101 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
102 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
103 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
106 * The licence and distribution terms for any publically available version or
107 * derivative of this code cannot be changed. i.e. this code cannot simply be
108 * copied and put under another distribution licence
109 * [including the GNU Public Licence.]
112 #ifndef OPENSSL_NO_ECDSA
119 #include <openssl/bio.h>
120 #include <openssl/err.h>
121 #include <openssl/bn.h>
122 #include <openssl/ec.h>
123 #include <openssl/ecdsa.h>
124 #include <openssl/x509.h>
125 #include <openssl/pem.h>
128 #define PROG ecdsaparam_main
130 /* -inform arg - input format - default PEM (DER or PEM)
131 * -outform arg - output format - default PEM
132 * -in arg - input file - default stdin
133 * -out arg - output file - default stdout
138 * -genkey - generate a private public keypair based on the supplied curve
139 * -named_curve - use the curve oid instead of the parameters
140 * -NIST_192 - use the NIST recommeded curve parameters over a 192 bit prime field
141 * -NIST_224 - use the NIST recommeded curve parameters over a 224 bit prime field
142 * -NIST_256 - use the NIST recommeded curve parameters over a 256 bit prime field
143 * -NIST_384 - use the NIST recommeded curve parameters over a 384 bit prime field
144 * -NIST_521 - use the NIST recommeded curve parameters over a 521 bit prime field
145 * -X9_62_192v1 - use the X9_62 192v1 example curve over a 192 bit prime field
146 * -X9_62_192v2 - use the X9_62 192v2 example curve over a 192 bit prime field
147 * -X9_62_192v3 - use the X9_62 192v3 example curve over a 192 bit prime field
148 * -X9_62_239v1 - use the X9_62 239v1 example curve over a 239 bit prime field
149 * -X9_62_239v2 - use the X9_62 239v2 example curve over a 239 bit prime field
150 * -X9_62_239v3 - use the X9_62 239v3 example curve over a 239 bit prime field
151 * -X9_62_256v1 - use the X9_62 239v1 example curve over a 256 bit prime field
152 * -SECG_PRIME_112R1 - use the SECG 112r1 recommeded curve over a 112 bit prime field
153 * -SECG_PRIME_112R2 - use the SECG 112r2 recommeded curve over a 112 bit prime field
154 * -SECG_PRIME_128R1 - use the SECG 128r1 recommeded curve over a 128 bit prime field
155 * -SECG_PRIME_128R2 - use the SECG 128r2 recommeded curve over a 128 bit prime field
156 * -SECG_PRIME_160K1 - use the SECG 160k1 recommeded curve over a 160 bit prime field
157 * -SECG_PRIME_160R1 - use the SECG 160r1 recommeded curve over a 160 bit prime field
158 * -SECG_PRIME_160R2 - use the SECG 160r2 recommeded curve over a 160 bit prime field
159 * -SECG_PRIME_192K1 - use the SECG 192k1 recommeded curve over a 192 bit prime field
160 * -SECG_PRIME_192R1 - use the SECG 192r1 recommeded curve over a 192 bit prime field
161 * -SECG_PRIME_224K1 - use the SECG 224k1 recommeded curve over a 224 bit prime field
162 * -SECG_PRIME_224R1 - use the SECG 224r1 recommeded curve over a 224 bit prime field
163 * -SECG_PRIME_256K1 - use the SECG 256k1 recommeded curve over a 256 bit prime field
164 * -SECG_PRIME_256R1 - use the SECG 256r1 recommeded curve over a 256 bit prime field
165 * -SECG_PRIME_384R1 - use the SECG 384r1 recommeded curve over a 384 bit prime field
166 * -SECG_PRIME_521R1 - use the SECG 521r1 recommeded curve over a 521 bit prime field
169 int MAIN(int, char **);
171 int MAIN(int argc, char **argv)
175 int i, badops = 0, text = 0;
176 BIO *in = NULL, *out = NULL;
177 int informat, outformat, noout = 0, C = 0, ret = 1;
178 char *infile, *outfile, *prog, *inrand = NULL;
182 int curve_type = EC_GROUP_NO_CURVE;
184 BIGNUM *tmp_1 = NULL, *tmp_2 = NULL, *tmp_3 = NULL, *tmp_4 = NULL, *tmp_5 = NULL,
185 *tmp_6 = NULL, *tmp_7 = NULL;
187 EC_POINT *point = NULL;
188 unsigned char *data = NULL;
193 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
194 BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
196 if (!load_config(bio_err, NULL))
202 outformat=FORMAT_PEM;
209 if (strcmp(*argv,"-inform") == 0)
211 if (--argc < 1) goto bad;
212 informat=str2fmt(*(++argv));
214 else if (strcmp(*argv,"-outform") == 0)
216 if (--argc < 1) goto bad;
217 outformat=str2fmt(*(++argv));
219 else if (strcmp(*argv,"-in") == 0)
221 if (--argc < 1) goto bad;
224 else if (strcmp(*argv,"-out") == 0)
226 if (--argc < 1) goto bad;
229 else if(strcmp(*argv, "-engine") == 0)
231 if (--argc < 1) goto bad;
234 else if (strcmp(*argv,"-text") == 0)
236 else if (strcmp(*argv,"-C") == 0)
238 else if (strcmp(*argv,"-genkey") == 0)
243 else if (strcmp(*argv,"-rand") == 0)
245 if (--argc < 1) goto bad;
249 else if (strcmp(*argv, "-named_curve") == 0)
251 else if (strcmp(*argv, "-NIST_192") == 0)
252 curve_type = EC_GROUP_NIST_PRIME_192;
253 else if (strcmp(*argv, "-NIST_224") == 0)
254 curve_type = EC_GROUP_NIST_PRIME_224;
255 else if (strcmp(*argv, "-NIST_256") == 0)
256 curve_type = EC_GROUP_NIST_PRIME_256;
257 else if (strcmp(*argv, "-NIST_384") == 0)
258 curve_type = EC_GROUP_NIST_PRIME_384;
259 else if (strcmp(*argv, "-NIST_521") == 0)
260 curve_type = EC_GROUP_NIST_PRIME_521;
261 else if (strcmp(*argv, "-X9_62_192v1") == 0)
262 curve_type = EC_GROUP_X9_62_PRIME_192V1;
263 else if (strcmp(*argv, "-X9_62_192v2") == 0)
264 curve_type = EC_GROUP_X9_62_PRIME_192V2;
265 else if (strcmp(*argv, "-X9_62_192v3") == 0)
266 curve_type = EC_GROUP_X9_62_PRIME_192V3;
267 else if (strcmp(*argv, "-X9_62_239v1") == 0)
268 curve_type = EC_GROUP_X9_62_PRIME_239V1;
269 else if (strcmp(*argv, "-X9_62_239v2") == 0)
270 curve_type = EC_GROUP_X9_62_PRIME_239V2;
271 else if (strcmp(*argv, "-X9_62_239v3") == 0)
272 curve_type = EC_GROUP_X9_62_PRIME_239V3;
273 else if (strcmp(*argv, "-X9_62_256v1") == 0)
274 curve_type = EC_GROUP_X9_62_PRIME_256V1;
275 else if (strcmp(*argv, "-SECG_PRIME_112R1") == 0)
276 curve_type = EC_GROUP_SECG_PRIME_112R1;
277 else if (strcmp(*argv, "-SECG_PRIME_112R2") == 0)
278 curve_type = EC_GROUP_SECG_PRIME_112R2;
279 else if (strcmp(*argv, "-SECG_PRIME_128R1") == 0)
280 curve_type = EC_GROUP_SECG_PRIME_128R1;
281 else if (strcmp(*argv, "-SECG_PRIME_128R2") == 0)
282 curve_type = EC_GROUP_SECG_PRIME_128R2;
283 else if (strcmp(*argv, "-SECG_PRIME_160K1") == 0)
284 curve_type = EC_GROUP_SECG_PRIME_160K1;
285 else if (strcmp(*argv, "-SECG_PRIME_160R1") == 0)
286 curve_type = EC_GROUP_SECG_PRIME_160R1;
287 else if (strcmp(*argv, "-SECG_PRIME_160R2") == 0)
288 curve_type = EC_GROUP_SECG_PRIME_160R2;
289 else if (strcmp(*argv, "-SECG_PRIME_192K1") == 0)
290 curve_type = EC_GROUP_SECG_PRIME_192K1;
291 else if (strcmp(*argv, "-SECG_PRIME_192R1") == 0)
292 curve_type = EC_GROUP_SECG_PRIME_192R1;
293 else if (strcmp(*argv, "-SECG_PRIME_224K1") == 0)
294 curve_type = EC_GROUP_SECG_PRIME_224K1;
295 else if (strcmp(*argv, "-SECG_PRIME_224R1") == 0)
296 curve_type = EC_GROUP_SECG_PRIME_224R1;
297 else if (strcmp(*argv, "-SECG_PRIME_256K1") == 0)
298 curve_type = EC_GROUP_SECG_PRIME_256K1;
299 else if (strcmp(*argv, "-SECG_PRIME_256R1") == 0)
300 curve_type = EC_GROUP_SECG_PRIME_256R1;
301 else if (strcmp(*argv, "-SECG_PRIME_384R1") == 0)
302 curve_type = EC_GROUP_SECG_PRIME_384R1;
303 else if (strcmp(*argv, "-SECG_PRIME_521R1") == 0)
304 curve_type = EC_GROUP_SECG_PRIME_521R1;
305 else if (strcmp(*argv, "-noout") == 0)
309 BIO_printf(bio_err,"unknown option %s\n",*argv);
320 BIO_printf(bio_err,"%s [options] [bits] <infile >outfile\n",prog);
321 BIO_printf(bio_err,"where options are\n");
322 BIO_printf(bio_err," -inform arg input format - DER or PEM\n");
323 BIO_printf(bio_err," -outform arg output format - DER or PEM\n");
324 BIO_printf(bio_err," -in arg input file\n");
325 BIO_printf(bio_err," -out arg output file\n");
326 BIO_printf(bio_err," -text print the key in text\n");
327 BIO_printf(bio_err," -C Output C code\n");
328 BIO_printf(bio_err," -noout no output\n");
329 BIO_printf(bio_err," -rand files to use for random number input\n");
330 BIO_printf(bio_err," -engine e use engine e, possibly a hardware device.\n");
331 BIO_printf(bio_err," -named_curve use the curve oid instead of the parameters\n");
332 BIO_printf(bio_err," -NIST_192 use the NIST recommeded curve parameters over a 192 bit prime field\n");
333 BIO_printf(bio_err," -NIST_224 use the NIST recommeded curve parameters over a 224 bit prime field\n");
334 BIO_printf(bio_err," -NIST_256 use the NIST recommeded curve parameters over a 256 bit prime field\n");
335 BIO_printf(bio_err," -NIST_384 use the NIST recommeded curve parameters over a 384 bit prime field\n");
336 BIO_printf(bio_err," -NIST_521 use the NIST recommeded curve parameters over a 521 bit prime field\n");
337 BIO_printf(bio_err," -X9_62_192v1 use the X9_62 192v1 example curve over a 192 bit prime field\n");
338 BIO_printf(bio_err," -X9_62_192v2 use the X9_62 192v2 example curve over a 192 bit prime field\n");
339 BIO_printf(bio_err," -X9_62_192v3 use the X9_62 192v3 example curve over a 192 bit prime field\n");
340 BIO_printf(bio_err," -X9_62_239v1 use the X9_62 239v1 example curve over a 239 bit prime field\n");
341 BIO_printf(bio_err," -X9_62_239v2 use the X9_62 239v2 example curve over a 239 bit prime field\n");
342 BIO_printf(bio_err," -X9_62_239v3 use the X9_62 239v3 example curve over a 239 bit prime field\n");
343 BIO_printf(bio_err," -X9_62_256v1 use the X9_62 239v1 example curve over a 256 bit prime field\n");
344 BIO_printf(bio_err," -SECG_PRIME_112R1 use the SECG 112r1 recommeded curve over a 112 bit prime field\n");
345 BIO_printf(bio_err," -SECG_PRIME_112R2 use the SECG 112r2 recommeded curve over a 112 bit prime field\n");
346 BIO_printf(bio_err," -SECG_PRIME_128R1 use the SECG 128r1 recommeded curve over a 128 bit prime field\n");
347 BIO_printf(bio_err," -SECG_PRIME_128R2 use the SECG 128r2 recommeded curve over a 128 bit prime field\n");
348 BIO_printf(bio_err," -SECG_PRIME_160K1 use the SECG 160k1 recommeded curve over a 160 bit prime field\n");
349 BIO_printf(bio_err," -SECG_PRIME_160R1 use the SECG 160r1 recommeded curve over a 160 bit prime field\n");
350 BIO_printf(bio_err," -SECG_PRIME_160R2 use the SECG 160r2 recommeded curve over a 160 bit prime field\n");
351 BIO_printf(bio_err," -SECG_PRIME_192K1 use the SECG 192k1 recommeded curve over a 192 bit prime field\n");
352 BIO_printf(bio_err," -SECG_PRIME_192R1 use the SECG 192r1 recommeded curve over a 192 bit prime field\n");
353 BIO_printf(bio_err," -SECG_PRIME_224K1 use the SECG 224k1 recommeded curve over a 224 bit prime field\n");
354 BIO_printf(bio_err," -SECG_PRIME_224R1 use the SECG 224r1 recommeded curve over a 224 bit prime field\n");
355 BIO_printf(bio_err," -SECG_PRIME_256K1 use the SECG 256k1 recommeded curve over a 256 bit prime field\n");
356 BIO_printf(bio_err," -SECG_PRIME_256R1 use the SECG 256r1 recommeded curve over a 256 bit prime field\n");
357 BIO_printf(bio_err," -SECG_PRIME_384R1 use the SECG 384r1 recommeded curve over a 384 bit prime field\n");
358 BIO_printf(bio_err," -SECG_PRIME_521R1 use the SECG 521r1 recommeded curve over a 521 bit prime field\n");
362 ERR_load_crypto_strings();
364 in=BIO_new(BIO_s_file());
365 out=BIO_new(BIO_s_file());
366 if ((in == NULL) || (out == NULL))
368 ERR_print_errors(bio_err);
373 BIO_set_fp(in,stdin,BIO_NOCLOSE);
376 if (BIO_read_filename(in,infile) <= 0)
384 BIO_set_fp(out,stdout,BIO_NOCLOSE);
385 #ifdef OPENSSL_SYS_VMS
387 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
388 out = BIO_push(tmpbio, out);
394 if (BIO_write_filename(out,outfile) <= 0)
401 e = setup_engine(bio_err, engine, 0);
405 app_RAND_load_file(NULL, bio_err, (inrand != NULL));
407 BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
408 app_RAND_load_files(inrand));
411 if (curve_type != EC_GROUP_NO_CURVE)
413 if ((ecdsa = ECDSA_new()) == NULL)
415 ecdsa->group = EC_GROUP_new_by_name(curve_type);
417 ECDSA_set_parameter_flags(ecdsa, ECDSA_FLAG_NAMED_CURVE);
419 else if (informat == FORMAT_ASN1)
420 ecdsa = d2i_ECDSAParameters_bio(in,NULL);
421 else if (informat == FORMAT_PEM)
422 ecdsa = PEM_read_bio_ECDSAParameters(in, NULL, NULL, NULL);
425 BIO_printf(bio_err, "bad input format specified\n");
430 BIO_printf(bio_err, "unable to load ECDSA parameters\n");
431 ERR_print_errors(bio_err);
437 ECDSAParameters_print(out, ecdsa);
441 { /* TODO : characteristic two */
443 if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL ||
444 (tmp_3 = BN_new()) == NULL || (tmp_4 = BN_new()) == NULL ||
445 (tmp_5 = BN_new()) == NULL || (tmp_6 = BN_new()) == NULL ||
446 (tmp_7 = BN_new()) == NULL || (ctx = BN_CTX_new()) == NULL)
448 perror("OPENSSL_malloc");
451 if (!EC_GROUP_get_curve_GFp(ecdsa->group, tmp_1, tmp_2, tmp_3, ctx))
453 if ((point = EC_GROUP_get0_generator(ecdsa->group)) == NULL)
455 if (!EC_POINT_get_affine_coordinates_GFp(ecdsa->group, point, tmp_4, tmp_5, ctx))
457 if (!EC_GROUP_get_order(ecdsa->group, tmp_6, ctx))
459 if (!EC_GROUP_get_cofactor(ecdsa->group, tmp_7, ctx))
462 len = BN_num_bytes(tmp_1);
463 bits_p = BN_num_bits(tmp_1);
464 data=(unsigned char *)OPENSSL_malloc(len+20);
467 perror("OPENSSL_malloc");
470 l = BN_bn2bin(tmp_1, data);
471 printf("static unsigned char ecdsa%d_p[]={", bits_p);
474 if ((i%12) == 0) printf("\n\t");
475 printf("0x%02X,",data[i]);
477 printf("\n\t};\n\n");
479 l = BN_bn2bin(tmp_2, data);
480 printf("static unsigned char ecdsa%d_a[]={",bits_p);
483 if ((i%12) == 0) printf("\n\t");
484 printf("0x%02X,",data[i]);
488 l = BN_bn2bin(tmp_3, data);
489 printf("static unsigned char ecdsa%d_b[]={", bits_p);
492 if ((i%12) == 0) printf("\n\t");
493 printf("0x%02X,",data[i]);
495 printf("\n\t};\n\n");
497 l = BN_bn2bin(tmp_4, data);
498 printf("static unsigned char ecdsa%d_x[]={", bits_p);
501 if ((i%12) == 0) printf("\n\t");
502 printf("0x%02X,",data[i]);
506 l = BN_bn2bin(tmp_5, data);
507 printf("static unsigned char ecdsa%d_y[]={", bits_p);
510 if ((i%12) == 0) printf("\n\t");
511 printf("0x%02X,",data[i]);
515 l = BN_bn2bin(tmp_6, data);
516 printf("static unsigned char ecdsa%d_o[]={", bits_p);
519 if ((i%12) == 0) printf("\n\t");
520 printf("0x%02X,",data[i]);
524 l = BN_bn2bin(tmp_7, data);
525 printf("static unsigned char ecdsa%d_c[]={", bits_p);
528 if ((i%12) == 0) printf("\n\t");
529 printf("0x%02X,",data[i]);
531 printf("\n\t};\n\n");
534 * generated code should check for errors
537 printf("ECDSA *get_ecdsa%d(void)\n\t{\n",bits_p);
538 printf("\tint ok=0;\n");
539 printf("\tECDSA *ecdsa=NULL;\n");
540 printf("\tEC_POINT *point=NULL;\n");
541 printf("\tBIGNUM *tmp_1=NULL,*tmp_2=NULL,*tmp_3=NULL;\n\n");
542 printf("\tif ((ecdsa=ECDSA_new()) == NULL)\n");
543 printf("\t\treturn(NULL);\n\n");
544 printf("\t/* generate EC_GROUP structure */\n");
545 printf("\tif ((tmp_1 = BN_bin2bn(ecdsa%d_p, sizeof(ecdsa%d_p), NULL)) == NULL) goto err;\n", bits_p, bits_p);
546 printf("\tif ((tmp_2 = BN_bin2bn(ecdsa%d_a, sizeof(ecdsa%d_a), NULL)) == NULL) goto err;\n", bits_p, bits_p);
547 printf("\tif ((tmp_3 = BN_bin2bn(ecdsa%d_b, sizeof(ecdsa%d_b), NULL)) == NULL) goto err;\n", bits_p, bits_p);
548 printf("\tif ((ecdsa->group = EC_GROUP_new_curve_GFp(tmp_1, tmp_2, tmp_3, NULL)) == NULL) goto err;\n\n");
549 printf("\t/* build generator */\n");
550 printf("\tif (!BN_bin2bn(ecdsa%d_x, sizeof(ecdsa%d_x), tmp_1)) goto err;\n", bits_p, bits_p);
551 printf("\tif (!BN_bin2bn(ecdsa%d_y, sizeof(ecdsa%d_y), tmp_2)) goto err;\n", bits_p, bits_p);
552 printf("\tif ((point = EC_POINT_new(ecdsa->group)) == NULL) goto err;\n");
553 printf("\tif (!EC_POINT_set_affine_coordinates_GFp(ecdsa->group, point, tmp_1, tmp_2, NULL)) goto err;\n");
554 printf("\t/* set generator, order and cofactor */\n");
555 printf("\tif (!BN_bin2bn(ecdsa%d_o, sizeof(ecdsa%d_o), tmp_1)) goto err;\n", bits_p, bits_p);
556 printf("\tif (!BN_bin2bn(ecdsa%d_c, sizeof(ecdsa%d_c), tmp_2)) goto err;\n", bits_p, bits_p);
557 printf("\tif (!EC_GROUP_set_generator(ecdsa->group, point, tmp_1, tmp_2)) goto err;\n");
558 printf("\n\tok=1;\n");
560 printf("\tif (tmp_1) BN_free(tmp_1);\n");
561 printf("\tif (tmp_2) BN_free(tmp_2);\n");
562 printf("\tif (tmp_3) BN_free(tmp_3);\n");
563 printf("\tif (point) EC_POINT_free(point);\n");
564 printf("\tif (!ok)\n");
566 printf("\t\tECDSA_free(ecdsa);\n");
567 printf("\t\tecdsa = NULL;\n");
569 printf("\treturn(ecdsa);\n\t}\n");
575 if (outformat == FORMAT_ASN1)
576 i = i2d_ECDSAParameters_bio(out, ecdsa);
577 else if (outformat == FORMAT_PEM)
578 i = PEM_write_bio_ECDSAParameters(out, ecdsa);
581 BIO_printf(bio_err,"bad output format specified for outfile\n");
586 BIO_printf(bio_err, "unable to write ECDSA parameters\n");
587 ERR_print_errors(bio_err);
596 if ((ecdsakey = ECDSAParameters_dup(ecdsa)) == NULL) goto end;
597 if (!ECDSA_generate_key(ecdsakey)) goto end;
598 if (outformat == FORMAT_ASN1)
599 i = i2d_ECDSAPrivateKey_bio(out, ecdsakey);
600 else if (outformat == FORMAT_PEM)
601 i = PEM_write_bio_ECDSAPrivateKey(out, ecdsakey, NULL, NULL, 0, NULL, NULL);
604 BIO_printf(bio_err, "bad output format specified for outfile\n");
607 ECDSA_free(ecdsakey);
610 app_RAND_write_file(NULL, bio_err);
613 if (in != NULL) BIO_free(in);
614 if (out != NULL) BIO_free_all(out);
615 if (ecdsa != NULL) ECDSA_free(ecdsa);
616 if (tmp_1) BN_free(tmp_1);
617 if (tmp_2) BN_free(tmp_2);
618 if (tmp_3) BN_free(tmp_3);
619 if (tmp_3) BN_free(tmp_4);
620 if (tmp_3) BN_free(tmp_5);
621 if (tmp_3) BN_free(tmp_6);
622 if (tmp_3) BN_free(tmp_7);
623 if (ctx) BN_CTX_free(ctx);
624 if (data) OPENSSL_free(data);