ecparam.c revision 1.1.1.1.4.1
1285612Sdelphij/* apps/ecparam.c */
2132451Sroberto/*
354359Sroberto * Written by Nils Larsch for the OpenSSL project.
4285612Sdelphij */
5285612Sdelphij/* ====================================================================
654359Sroberto * Copyright (c) 1998-2005 The OpenSSL Project.  All rights reserved.
754359Sroberto *
854359Sroberto * Redistribution and use in source and binary forms, with or without
954359Sroberto * modification, are permitted provided that the following conditions
1054359Sroberto * are met:
1154359Sroberto *
1254359Sroberto * 1. Redistributions of source code must retain the above copyright
1354359Sroberto *    notice, this list of conditions and the following disclaimer.
1454359Sroberto *
15106163Sroberto * 2. Redistributions in binary form must reproduce the above copyright
1654359Sroberto *    notice, this list of conditions and the following disclaimer in
17285612Sdelphij *    the documentation and/or other materials provided with the
18285612Sdelphij *    distribution.
19285612Sdelphij *
20285612Sdelphij * 3. All advertising materials mentioning features or use of this
21285612Sdelphij *    software must display the following acknowledgment:
22285612Sdelphij *    "This product includes software developed by the OpenSSL Project
23285612Sdelphij *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24285612Sdelphij *
25285612Sdelphij * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26285612Sdelphij *    endorse or promote products derived from this software without
27285612Sdelphij *    prior written permission. For written permission, please contact
28285612Sdelphij *    openssl-core@openssl.org.
29285612Sdelphij *
30285612Sdelphij * 5. Products derived from this software may not be called "OpenSSL"
31285612Sdelphij *    nor may "OpenSSL" appear in their names without prior written
32285612Sdelphij *    permission of the OpenSSL Project.
33285612Sdelphij *
34285612Sdelphij * 6. Redistributions of any form whatsoever must retain the following
35285612Sdelphij *    acknowledgment:
36285612Sdelphij *    "This product includes software developed by the OpenSSL Project
37285612Sdelphij *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38285612Sdelphij *
39285612Sdelphij * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40285612Sdelphij * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41285612Sdelphij * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42285612Sdelphij * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43285612Sdelphij * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44285612Sdelphij * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45285612Sdelphij * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46285612Sdelphij * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47285612Sdelphij * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48285612Sdelphij * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49285612Sdelphij * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50285612Sdelphij * OF THE POSSIBILITY OF SUCH DAMAGE.
51285612Sdelphij * ====================================================================
52285612Sdelphij *
53285612Sdelphij * This product includes cryptographic software written by Eric Young
54285612Sdelphij * (eay@cryptsoft.com).  This product includes software written by Tim
55285612Sdelphij * Hudson (tjh@cryptsoft.com).
56285612Sdelphij *
57285612Sdelphij */
58285612Sdelphij/* ====================================================================
59285612Sdelphij * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60285612Sdelphij *
61285612Sdelphij * Portions of the attached software ("Contribution") are developed by
62285612Sdelphij * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
63285612Sdelphij *
64285612Sdelphij * The Contribution is licensed pursuant to the OpenSSL open source
65285612Sdelphij * license provided above.
66285612Sdelphij *
67285612Sdelphij * The elliptic curve binary polynomial software is originally written by
68285612Sdelphij * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
69285612Sdelphij *
70285612Sdelphij */
71285612Sdelphij
7254359Sroberto#include <openssl/opensslconf.h>
73200576Sroberto#ifndef OPENSSL_NO_EC
7454359Sroberto#include <assert.h>
75200576Sroberto#include <stdio.h>
76132451Sroberto#include <stdlib.h>
77132451Sroberto#include <time.h>
78132451Sroberto#include <string.h>
79132451Sroberto#include "apps.h"
8082498Sroberto#include <openssl/bio.h>
81132451Sroberto#include <openssl/err.h>
8254359Sroberto#include <openssl/bn.h>
8354359Sroberto#include <openssl/ec.h>
8454359Sroberto#include <openssl/x509.h>
8554359Sroberto#include <openssl/pem.h>
8654359Sroberto
8754359Sroberto#undef PROG
8854359Sroberto#define PROG	ecparam_main
8954359Sroberto
90200576Sroberto/* -inform arg      - input format - default PEM (DER or PEM)
91182007Sroberto * -outform arg     - output format - default PEM
92285612Sdelphij * -in  arg         - input file  - default stdin
93285612Sdelphij * -out arg         - output file - default stdout
94285612Sdelphij * -noout           - do not print the ec parameter
95285612Sdelphij * -text            - print the ec parameters in text form
96285612Sdelphij * -check           - validate the ec parameters
97285612Sdelphij * -C               - print a 'C' function creating the parameters
98285612Sdelphij * -name arg        - use the ec parameters with 'short name' name
99285612Sdelphij * -list_curves     - prints a list of all currently available curve 'short names'
100285612Sdelphij * -conv_form arg   - specifies the point conversion form
101285612Sdelphij *                  - possible values: compressed
102285612Sdelphij *                                     uncompressed (default)
103285612Sdelphij *                                     hybrid
104285612Sdelphij * -param_enc arg   - specifies the way the ec parameters are encoded
105285612Sdelphij *                    in the asn1 der encoding
106285612Sdelphij *                    possible values: named_curve (default)
107285612Sdelphij *                                     explicit
108285612Sdelphij * -no_seed         - if 'explicit' parameters are choosen do not use the seed
109285612Sdelphij * -genkey          - generate ec key
110285612Sdelphij * -rand file       - files to use for random number input
111285612Sdelphij * -engine e        - use engine e, possibly a hardware device
112285612Sdelphij */
113285612Sdelphij
114285612Sdelphij
115285612Sdelphijstatic int ecparam_print_var(BIO *,BIGNUM *,const char *,int,unsigned char *);
116285612Sdelphij
117285612Sdelphijint MAIN(int, char **);
118289997Sglebius
119285612Sdelphijint MAIN(int argc, char **argv)
120285612Sdelphij	{
121285612Sdelphij	EC_GROUP *group = NULL;
122285612Sdelphij	point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
123285612Sdelphij	int 	new_form = 0;
124285612Sdelphij	int 	asn1_flag = OPENSSL_EC_NAMED_CURVE;
125285612Sdelphij	int 	new_asn1_flag = 0;
126285612Sdelphij	char 	*curve_name = NULL, *inrand = NULL;
127285612Sdelphij	int	list_curves = 0, no_seed = 0, check = 0,
128285612Sdelphij		badops = 0, text = 0, i, need_rand = 0, genkey = 0;
129285612Sdelphij	char	*infile = NULL, *outfile = NULL, *prog;
130182007Sroberto	BIO 	*in = NULL, *out = NULL;
131182007Sroberto	int 	informat, outformat, noout = 0, C = 0, ret = 1;
132285612Sdelphij	char	*engine = NULL;
133285612Sdelphij
134182007Sroberto	BIGNUM	*ec_p = NULL, *ec_a = NULL, *ec_b = NULL,
135200576Sroberto		*ec_gen = NULL, *ec_order = NULL, *ec_cofactor = NULL;
136182007Sroberto	unsigned char *buffer = NULL;
137182007Sroberto
138182007Sroberto	apps_startup();
139200576Sroberto
140285612Sdelphij	if (bio_err == NULL)
141285612Sdelphij		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
142285612Sdelphij			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
143285612Sdelphij
144285612Sdelphij	if (!load_config(bio_err, NULL))
145285612Sdelphij		goto end;
146285612Sdelphij
147285612Sdelphij	informat=FORMAT_PEM;
148285612Sdelphij	outformat=FORMAT_PEM;
149285612Sdelphij
150285612Sdelphij	prog=argv[0];
151285612Sdelphij	argc--;
152182007Sroberto	argv++;
153182007Sroberto	while (argc >= 1)
154285612Sdelphij		{
155285612Sdelphij		if 	(strcmp(*argv,"-inform") == 0)
156285612Sdelphij			{
157285612Sdelphij			if (--argc < 1) goto bad;
158285612Sdelphij			informat=str2fmt(*(++argv));
159285612Sdelphij			}
160285612Sdelphij		else if (strcmp(*argv,"-outform") == 0)
161285612Sdelphij			{
162285612Sdelphij			if (--argc < 1) goto bad;
163285612Sdelphij			outformat=str2fmt(*(++argv));
164285612Sdelphij			}
165285612Sdelphij		else if (strcmp(*argv,"-in") == 0)
166285612Sdelphij			{
167200576Sroberto			if (--argc < 1) goto bad;
168200576Sroberto			infile= *(++argv);
169285612Sdelphij			}
170285612Sdelphij		else if (strcmp(*argv,"-out") == 0)
171285612Sdelphij			{
172285612Sdelphij			if (--argc < 1) goto bad;
173285612Sdelphij			outfile= *(++argv);
174285612Sdelphij			}
175285612Sdelphij		else if (strcmp(*argv,"-text") == 0)
176285612Sdelphij			text = 1;
177285612Sdelphij		else if (strcmp(*argv,"-C") == 0)
178285612Sdelphij			C = 1;
179285612Sdelphij		else if (strcmp(*argv,"-check") == 0)
180285612Sdelphij			check = 1;
181285612Sdelphij		else if (strcmp (*argv, "-name") == 0)
182285612Sdelphij			{
183285612Sdelphij			if (--argc < 1)
184285612Sdelphij				goto bad;
185285612Sdelphij			curve_name = *(++argv);
186285612Sdelphij			}
187285612Sdelphij		else if (strcmp(*argv, "-list_curves") == 0)
188285612Sdelphij			list_curves = 1;
189285612Sdelphij		else if (strcmp(*argv, "-conv_form") == 0)
190285612Sdelphij			{
191285612Sdelphij			if (--argc < 1)
192285612Sdelphij				goto bad;
193182007Sroberto			++argv;
194182007Sroberto			new_form = 1;
195285612Sdelphij			if (strcmp(*argv, "compressed") == 0)
196285612Sdelphij				form = POINT_CONVERSION_COMPRESSED;
197285612Sdelphij			else if (strcmp(*argv, "uncompressed") == 0)
198285612Sdelphij				form = POINT_CONVERSION_UNCOMPRESSED;
199285612Sdelphij			else if (strcmp(*argv, "hybrid") == 0)
200285612Sdelphij				form = POINT_CONVERSION_HYBRID;
201285612Sdelphij			else
202285612Sdelphij				goto bad;
203285612Sdelphij			}
204285612Sdelphij		else if (strcmp(*argv, "-param_enc") == 0)
205285612Sdelphij			{
206285612Sdelphij			if (--argc < 1)
207285612Sdelphij				goto bad;
208285612Sdelphij			++argv;
209285612Sdelphij			new_asn1_flag = 1;
210285612Sdelphij			if (strcmp(*argv, "named_curve") == 0)
211285612Sdelphij				asn1_flag = OPENSSL_EC_NAMED_CURVE;
212285612Sdelphij			else if (strcmp(*argv, "explicit") == 0)
213285612Sdelphij				asn1_flag = 0;
214285612Sdelphij			else
215182007Sroberto				goto bad;
216182007Sroberto			}
217182007Sroberto		else if (strcmp(*argv, "-no_seed") == 0)
218182007Sroberto			no_seed = 1;
219285612Sdelphij		else if (strcmp(*argv, "-noout") == 0)
220285612Sdelphij			noout=1;
221285612Sdelphij		else if (strcmp(*argv,"-genkey") == 0)
222285612Sdelphij			{
223285612Sdelphij			genkey=1;
224285612Sdelphij			need_rand=1;
225200576Sroberto			}
226200576Sroberto		else if (strcmp(*argv, "-rand") == 0)
227200576Sroberto			{
228200576Sroberto			if (--argc < 1) goto bad;
229200576Sroberto			inrand= *(++argv);
230200576Sroberto			need_rand=1;
231200576Sroberto			}
232200576Sroberto		else if(strcmp(*argv, "-engine") == 0)
233200576Sroberto			{
234200576Sroberto			if (--argc < 1) goto bad;
235200576Sroberto			engine = *(++argv);
236200576Sroberto			}
237200576Sroberto		else
238200576Sroberto			{
239200576Sroberto			BIO_printf(bio_err,"unknown option %s\n",*argv);
240200576Sroberto			badops=1;
241200576Sroberto			break;
242200576Sroberto			}
243200576Sroberto		argc--;
244200576Sroberto		argv++;
245200576Sroberto		}
246200576Sroberto
247200576Sroberto	if (badops)
248200576Sroberto		{
249200576Srobertobad:
250182007Sroberto		BIO_printf(bio_err, "%s [options] <infile >outfile\n",prog);
251182007Sroberto		BIO_printf(bio_err, "where options are\n");
252285612Sdelphij		BIO_printf(bio_err, " -inform arg       input format - "
253182007Sroberto				"default PEM (DER or PEM)\n");
254285612Sdelphij		BIO_printf(bio_err, " -outform arg      output format - "
255285612Sdelphij				"default PEM\n");
256182007Sroberto		BIO_printf(bio_err, " -in  arg          input file  - "
257132451Sroberto				"default stdin\n");
258285612Sdelphij		BIO_printf(bio_err, " -out arg          output file - "
25954359Sroberto				"default stdout\n");
260285612Sdelphij		BIO_printf(bio_err, " -noout            do not print the "
261182007Sroberto				"ec parameter\n");
262132451Sroberto		BIO_printf(bio_err, " -text             print the ec "
263132451Sroberto				"parameters in text form\n");
264132451Sroberto		BIO_printf(bio_err, " -check            validate the ec "
26554359Sroberto				"parameters\n");
266289997Sglebius		BIO_printf(bio_err, " -C                print a 'C' "
267285612Sdelphij				"function creating the parameters\n");
268285612Sdelphij		BIO_printf(bio_err, " -name arg         use the "
269285612Sdelphij				"ec parameters with 'short name' name\n");
270285612Sdelphij		BIO_printf(bio_err, " -list_curves      prints a list of "
271285612Sdelphij				"all currently available curve 'short names'\n");
27254359Sroberto		BIO_printf(bio_err, " -conv_form arg    specifies the "
273132451Sroberto				"point conversion form \n");
27454359Sroberto		BIO_printf(bio_err, "                   possible values:"
275289997Sglebius				" compressed\n");
276285612Sdelphij		BIO_printf(bio_err, "                                   "
27754359Sroberto				" uncompressed (default)\n");
278285612Sdelphij		BIO_printf(bio_err, "                                   "
27954359Sroberto				" hybrid\n");
280132451Sroberto		BIO_printf(bio_err, " -param_enc arg    specifies the way"
281285612Sdelphij				" the ec parameters are encoded\n");
282285612Sdelphij		BIO_printf(bio_err, "                   in the asn1 der "
283285612Sdelphij				"encoding\n");
284285612Sdelphij		BIO_printf(bio_err, "                   possible values:"
285285612Sdelphij				" named_curve (default)\n");
286285612Sdelphij		BIO_printf(bio_err, "                                   "
287132451Sroberto				" explicit\n");
28854359Sroberto		BIO_printf(bio_err, " -no_seed          if 'explicit'"
289132451Sroberto				" parameters are choosen do not"
29056746Sroberto				" use the seed\n");
291285612Sdelphij		BIO_printf(bio_err, " -genkey           generate ec"
292285612Sdelphij				" key\n");
293285612Sdelphij		BIO_printf(bio_err, " -rand file        files to use for"
294132451Sroberto				" random number input\n");
295132451Sroberto		BIO_printf(bio_err, " -engine e         use engine e, "
296132451Sroberto				"possibly a hardware device\n");
297285612Sdelphij		goto end;
298132451Sroberto		}
299106163Sroberto
300285612Sdelphij	ERR_load_crypto_strings();
301200576Sroberto
302285612Sdelphij	in=BIO_new(BIO_s_file());
303285612Sdelphij	out=BIO_new(BIO_s_file());
304285612Sdelphij	if ((in == NULL) || (out == NULL))
305285612Sdelphij		{
306285612Sdelphij		ERR_print_errors(bio_err);
307200576Sroberto		goto end;
308285612Sdelphij		}
309285612Sdelphij
310285612Sdelphij	if (infile == NULL)
311200576Sroberto		BIO_set_fp(in,stdin,BIO_NOCLOSE);
312132451Sroberto	else
313132451Sroberto		{
314132451Sroberto		if (BIO_read_filename(in,infile) <= 0)
31582498Sroberto			{
316285612Sdelphij			perror(infile);
317285612Sdelphij			goto end;
318285612Sdelphij			}
319285612Sdelphij		}
320285612Sdelphij	if (outfile == NULL)
321285612Sdelphij		{
32254359Sroberto		BIO_set_fp(out,stdout,BIO_NOCLOSE);
323285612Sdelphij#ifdef OPENSSL_SYS_VMS
324285612Sdelphij		{
325285612Sdelphij		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
326132451Sroberto		out = BIO_push(tmpbio, out);
327182007Sroberto		}
328182007Sroberto#endif
329182007Sroberto		}
33054359Sroberto	else
331132451Sroberto		{
332182007Sroberto		if (BIO_write_filename(out,outfile) <= 0)
333285612Sdelphij			{
334285612Sdelphij			perror(outfile);
33554359Sroberto			goto end;
336200576Sroberto			}
337289997Sglebius		}
338132451Sroberto
339132451Sroberto#ifndef OPENSSL_NO_ENGINE
34054359Sroberto	setup_engine(bio_err, engine, 0);
341182007Sroberto#endif
34254359Sroberto
343132451Sroberto	if (list_curves)
34454359Sroberto		{
34554359Sroberto		EC_builtin_curve *curves = NULL;
346132451Sroberto		size_t crv_len = 0;
347285612Sdelphij		size_t n = 0;
34854359Sroberto
34954359Sroberto		crv_len = EC_get_builtin_curves(NULL, 0);
35054359Sroberto
35182498Sroberto		curves = OPENSSL_malloc((int)(sizeof(EC_builtin_curve) * crv_len));
352285612Sdelphij
353285612Sdelphij		if (curves == NULL)
354200576Sroberto			goto end;
355285612Sdelphij
356285612Sdelphij		if (!EC_get_builtin_curves(curves, crv_len))
357285612Sdelphij			{
358285612Sdelphij			OPENSSL_free(curves);
359285612Sdelphij			goto end;
360285612Sdelphij			}
361285612Sdelphij
362285612Sdelphij
363285612Sdelphij		for (n = 0; n < crv_len; n++)
364285612Sdelphij			{
365285612Sdelphij			const char *comment;
366285612Sdelphij			const char *sname;
367285612Sdelphij			comment = curves[n].comment;
368285612Sdelphij			sname   = OBJ_nid2sn(curves[n].nid);
369285612Sdelphij			if (comment == NULL)
370285612Sdelphij				comment = "CURVE DESCRIPTION NOT AVAILABLE";
371285612Sdelphij			if (sname == NULL)
372285612Sdelphij				sname = "";
373285612Sdelphij
374285612Sdelphij			BIO_printf(out, "  %-10s: ", sname);
375285612Sdelphij			BIO_printf(out, "%s\n", comment);
376285612Sdelphij			}
377285612Sdelphij
378285612Sdelphij		OPENSSL_free(curves);
379285612Sdelphij		ret = 0;
380285612Sdelphij		goto end;
381285612Sdelphij		}
382285612Sdelphij
383285612Sdelphij	if (curve_name != NULL)
384285612Sdelphij		{
385285612Sdelphij		int nid;
386285612Sdelphij
387285612Sdelphij		/* workaround for the SECG curve names secp192r1
388285612Sdelphij		 * and secp256r1 (which are the same as the curves
389285612Sdelphij		 * prime192v1 and prime256v1 defined in X9.62)
390285612Sdelphij		 */
391285612Sdelphij		if (!strcmp(curve_name, "secp192r1"))
392285612Sdelphij			{
393285612Sdelphij			BIO_printf(bio_err, "using curve name prime192v1 "
394285612Sdelphij				"instead of secp192r1\n");
395285612Sdelphij			nid = NID_X9_62_prime192v1;
396285612Sdelphij			}
397285612Sdelphij		else if (!strcmp(curve_name, "secp256r1"))
398285612Sdelphij			{
399285612Sdelphij			BIO_printf(bio_err, "using curve name prime256v1 "
400285612Sdelphij				"instead of secp256r1\n");
401285612Sdelphij			nid = NID_X9_62_prime256v1;
402285612Sdelphij			}
403285612Sdelphij		else
404285612Sdelphij			nid = OBJ_sn2nid(curve_name);
405285612Sdelphij
406285612Sdelphij		if (nid == 0)
407285612Sdelphij			{
408285612Sdelphij			BIO_printf(bio_err, "unknown curve name (%s)\n",
409285612Sdelphij				curve_name);
410285612Sdelphij			goto end;
411285612Sdelphij			}
412285612Sdelphij
413285612Sdelphij		group = EC_GROUP_new_by_curve_name(nid);
414106163Sroberto		if (group == NULL)
415285612Sdelphij			{
416285612Sdelphij			BIO_printf(bio_err, "unable to create curve (%s)\n",
41754359Sroberto				curve_name);
418132451Sroberto			goto end;
419132451Sroberto			}
420132451Sroberto		EC_GROUP_set_asn1_flag(group, asn1_flag);
421132451Sroberto		EC_GROUP_set_point_conversion_form(group, form);
422200576Sroberto		}
423132451Sroberto	else if (informat == FORMAT_ASN1)
424285612Sdelphij		{
42582498Sroberto		group = d2i_ECPKParameters_bio(in, NULL);
426285612Sdelphij		}
427132451Sroberto	else if (informat == FORMAT_PEM)
428285612Sdelphij		{
429285612Sdelphij		group = PEM_read_bio_ECPKParameters(in,NULL,NULL,NULL);
430285612Sdelphij		}
431285612Sdelphij	else
43254359Sroberto		{
433285612Sdelphij		BIO_printf(bio_err, "bad input format specified\n");
43454359Sroberto		goto end;
435285612Sdelphij		}
436132451Sroberto
437132451Sroberto	if (group == NULL)
438285612Sdelphij		{
439285612Sdelphij		BIO_printf(bio_err,
440285612Sdelphij			"unable to load elliptic curve parameters\n");
441285612Sdelphij		ERR_print_errors(bio_err);
442285612Sdelphij		goto end;
443285612Sdelphij		}
444285612Sdelphij
445285612Sdelphij	if (new_form)
446285612Sdelphij		EC_GROUP_set_point_conversion_form(group, form);
447285612Sdelphij
448132451Sroberto	if (new_asn1_flag)
44954359Sroberto		EC_GROUP_set_asn1_flag(group, asn1_flag);
450285612Sdelphij
451285612Sdelphij	if (no_seed)
452285612Sdelphij		{
453285612Sdelphij		EC_GROUP_set_seed(group, NULL, 0);
454285612Sdelphij		}
455285612Sdelphij
456285612Sdelphij	if (text)
457285612Sdelphij		{
458285612Sdelphij		if (!ECPKParameters_print(out, group, 0))
459285612Sdelphij			goto end;
460285612Sdelphij		}
461285612Sdelphij
462285612Sdelphij	if (check)
463285612Sdelphij		{
464285612Sdelphij		if (group == NULL)
46554359Sroberto			BIO_printf(bio_err, "no elliptic curve parameters\n");
466285612Sdelphij		BIO_printf(bio_err, "checking elliptic curve parameters: ");
467285612Sdelphij		if (!EC_GROUP_check(group, NULL))
468285612Sdelphij			{
469200576Sroberto			BIO_printf(bio_err, "failed\n");
470200576Sroberto			ERR_print_errors(bio_err);
471200576Sroberto			}
472200576Sroberto		else
473285612Sdelphij			BIO_printf(bio_err, "ok\n");
474132451Sroberto
475285612Sdelphij		}
476285612Sdelphij
477106163Sroberto	if (C)
478132451Sroberto		{
479106163Sroberto		size_t	buf_len = 0, tmp_len = 0;
480182007Sroberto		const EC_POINT *point;
481182007Sroberto		int	is_prime, len = 0;
482132451Sroberto		const EC_METHOD *meth = EC_GROUP_method_of(group);
483132451Sroberto
484132451Sroberto		if ((ec_p = BN_new()) == NULL || (ec_a = BN_new()) == NULL ||
485132451Sroberto		    (ec_b = BN_new()) == NULL || (ec_gen = BN_new()) == NULL ||
486132451Sroberto		    (ec_order = BN_new()) == NULL ||
487132451Sroberto		    (ec_cofactor = BN_new()) == NULL )
488200576Sroberto			{
489132451Sroberto			perror("OPENSSL_malloc");
490200576Sroberto			goto end;
491200576Sroberto			}
492200576Sroberto
493132451Sroberto		is_prime = (EC_METHOD_get_field_type(meth) ==
494132451Sroberto			NID_X9_62_prime_field);
495132451Sroberto
496132451Sroberto		if (is_prime)
497132451Sroberto			{
498132451Sroberto			if (!EC_GROUP_get_curve_GFp(group, ec_p, ec_a,
499200576Sroberto				ec_b, NULL))
500132451Sroberto				goto end;
501132451Sroberto			}
50256746Sroberto		else
503132451Sroberto			{
504132451Sroberto			/* TODO */
505200576Sroberto			goto end;
506132451Sroberto			}
507132451Sroberto
508182007Sroberto		if ((point = EC_GROUP_get0_generator(group)) == NULL)
509132451Sroberto			goto end;
510200576Sroberto		if (!EC_POINT_point2bn(group, point,
511132451Sroberto			EC_GROUP_get_point_conversion_form(group), ec_gen,
512132451Sroberto			NULL))
513200576Sroberto			goto end;
514132451Sroberto		if (!EC_GROUP_get_order(group, ec_order, NULL))
515132451Sroberto			goto end;
516200576Sroberto		if (!EC_GROUP_get_cofactor(group, ec_cofactor, NULL))
517132451Sroberto			goto end;
518132451Sroberto
519132451Sroberto		if (!ec_p || !ec_a || !ec_b || !ec_gen ||
520200576Sroberto			!ec_order || !ec_cofactor)
521200576Sroberto			goto end;
522200576Sroberto
523285612Sdelphij		len = BN_num_bits(ec_order);
524285612Sdelphij
52554359Sroberto		if ((tmp_len = (size_t)BN_num_bytes(ec_p)) > buf_len)
526285612Sdelphij			buf_len = tmp_len;
527285612Sdelphij		if ((tmp_len = (size_t)BN_num_bytes(ec_a)) > buf_len)
528285612Sdelphij			buf_len = tmp_len;
529182007Sroberto		if ((tmp_len = (size_t)BN_num_bytes(ec_b)) > buf_len)
530182007Sroberto			buf_len = tmp_len;
531182007Sroberto		if ((tmp_len = (size_t)BN_num_bytes(ec_gen)) > buf_len)
532132451Sroberto			buf_len = tmp_len;
533182007Sroberto		if ((tmp_len = (size_t)BN_num_bytes(ec_order)) > buf_len)
534182007Sroberto			buf_len = tmp_len;
535182007Sroberto		if ((tmp_len = (size_t)BN_num_bytes(ec_cofactor)) > buf_len)
536182007Sroberto			buf_len = tmp_len;
537285612Sdelphij
538132451Sroberto		buffer = (unsigned char *)OPENSSL_malloc(buf_len);
539132451Sroberto
540132451Sroberto		if (buffer == NULL)
541182007Sroberto			{
542182007Sroberto			perror("OPENSSL_malloc");
543285612Sdelphij			goto end;
544182007Sroberto			}
545132451Sroberto
546289997Sglebius		ecparam_print_var(out, ec_p, "ec_p", len, buffer);
54782498Sroberto		ecparam_print_var(out, ec_a, "ec_a", len, buffer);
548285612Sdelphij		ecparam_print_var(out, ec_b, "ec_b", len, buffer);
549285612Sdelphij		ecparam_print_var(out, ec_gen, "ec_gen", len, buffer);
550285612Sdelphij		ecparam_print_var(out, ec_order, "ec_order", len, buffer);
551285612Sdelphij		ecparam_print_var(out, ec_cofactor, "ec_cofactor", len,
552285612Sdelphij			buffer);
553285612Sdelphij
554285612Sdelphij		BIO_printf(out, "\n\n");
555285612Sdelphij
556285612Sdelphij		BIO_printf(out, "EC_GROUP *get_ec_group_%d(void)\n\t{\n", len);
557285612Sdelphij		BIO_printf(out, "\tint ok=0;\n");
558298770Sdelphij		BIO_printf(out, "\tEC_GROUP *group = NULL;\n");
559285612Sdelphij		BIO_printf(out, "\tEC_POINT *point = NULL;\n");
560285612Sdelphij		BIO_printf(out, "\tBIGNUM   *tmp_1 = NULL, *tmp_2 = NULL, "
561285612Sdelphij				"*tmp_3 = NULL;\n\n");
562285612Sdelphij		BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_p_%d, "
563285612Sdelphij				"sizeof(ec_p_%d), NULL)) == NULL)\n\t\t"
564285612Sdelphij				"goto err;\n", len, len);
565285612Sdelphij		BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_a_%d, "
566285612Sdelphij				"sizeof(ec_a_%d), NULL)) == NULL)\n\t\t"
567285612Sdelphij				"goto err;\n", len, len);
568285612Sdelphij		BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_b_%d, "
569285612Sdelphij				"sizeof(ec_b_%d), NULL)) == NULL)\n\t\t"
570182007Sroberto				"goto err;\n", len, len);
571182007Sroberto		if (is_prime)
572285612Sdelphij			{
573285612Sdelphij			BIO_printf(out, "\tif ((group = EC_GROUP_new_curve_"
574285612Sdelphij				"GFp(tmp_1, tmp_2, tmp_3, NULL)) == NULL)"
575285612Sdelphij				"\n\t\tgoto err;\n\n");
576285612Sdelphij			}
577182007Sroberto		else
578285612Sdelphij			{
579182007Sroberto			/* TODO */
580182007Sroberto			goto end;
58182498Sroberto			}
582285612Sdelphij		BIO_printf(out, "\t/* build generator */\n");
583182007Sroberto		BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_gen_%d, "
58482498Sroberto				"sizeof(ec_gen_%d), tmp_1)) == NULL)"
585285612Sdelphij				"\n\t\tgoto err;\n", len, len);
586285612Sdelphij		BIO_printf(out, "\tpoint = EC_POINT_bn2point(group, tmp_1, "
587285612Sdelphij				"NULL, NULL);\n");
588285612Sdelphij		BIO_printf(out, "\tif (point == NULL)\n\t\tgoto err;\n");
589285612Sdelphij		BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_order_%d, "
590285612Sdelphij				"sizeof(ec_order_%d), tmp_2)) == NULL)"
591285612Sdelphij				"\n\t\tgoto err;\n", len, len);
592285612Sdelphij		BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_cofactor_%d, "
59382498Sroberto				"sizeof(ec_cofactor_%d), tmp_3)) == NULL)"
59454359Sroberto				"\n\t\tgoto err;\n", len, len);
595106163Sroberto		BIO_printf(out, "\tif (!EC_GROUP_set_generator(group, point,"
596285612Sdelphij				" tmp_2, tmp_3))\n\t\tgoto err;\n");
59754359Sroberto		BIO_printf(out, "\n\tok=1;\n");
59882498Sroberto		BIO_printf(out, "err:\n");
59982498Sroberto		BIO_printf(out, "\tif (tmp_1)\n\t\tBN_free(tmp_1);\n");
60054359Sroberto		BIO_printf(out, "\tif (tmp_2)\n\t\tBN_free(tmp_2);\n");
60154359Sroberto		BIO_printf(out, "\tif (tmp_3)\n\t\tBN_free(tmp_3);\n");
602285612Sdelphij		BIO_printf(out, "\tif (point)\n\t\tEC_POINT_free(point);\n");
603182007Sroberto		BIO_printf(out, "\tif (!ok)\n");
604182007Sroberto		BIO_printf(out, "\t\t{\n");
605182007Sroberto		BIO_printf(out, "\t\tEC_GROUP_free(group);\n");
606182007Sroberto		BIO_printf(out, "\t\tgroup = NULL;\n");
607182007Sroberto		BIO_printf(out, "\t\t}\n");
608200576Sroberto		BIO_printf(out, "\treturn(group);\n\t}\n");
609200576Sroberto	}
610182007Sroberto
611182007Sroberto	if (!noout)
612182007Sroberto		{
613182007Sroberto		if (outformat == FORMAT_ASN1)
614200576Sroberto			i = i2d_ECPKParameters_bio(out, group);
615200576Sroberto		else if (outformat == FORMAT_PEM)
616200576Sroberto			i = PEM_write_bio_ECPKParameters(out, group);
617182007Sroberto		else
618182007Sroberto			{
619182007Sroberto			BIO_printf(bio_err,"bad output format specified for"
620182007Sroberto				" outfile\n");
621182007Sroberto			goto end;
622182007Sroberto			}
623182007Sroberto		if (!i)
624182007Sroberto			{
625182007Sroberto			BIO_printf(bio_err, "unable to write elliptic "
62654359Sroberto				"curve parameters\n");
627182007Sroberto			ERR_print_errors(bio_err);
62854359Sroberto			goto end;
629182007Sroberto			}
630182007Sroberto		}
631200576Sroberto
632182007Sroberto	if (need_rand)
633200576Sroberto		{
634200576Sroberto		app_RAND_load_file(NULL, bio_err, (inrand != NULL));
635132451Sroberto		if (inrand != NULL)
636132451Sroberto			BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
637285612Sdelphij				app_RAND_load_files(inrand));
638285612Sdelphij		}
639132451Sroberto
640132451Sroberto	if (genkey)
641132451Sroberto		{
642132451Sroberto		EC_KEY *eckey = EC_KEY_new();
643182007Sroberto
644200576Sroberto		if (eckey == NULL)
645182007Sroberto			goto end;
646182007Sroberto
64754359Sroberto		assert(need_rand);
64854359Sroberto
649132451Sroberto		if (EC_KEY_set_group(eckey, group) == 0)
650182007Sroberto			goto end;
651182007Sroberto
652182007Sroberto		if (!EC_KEY_generate_key(eckey))
653182007Sroberto			{
654182007Sroberto			EC_KEY_free(eckey);
655182007Sroberto			goto end;
656182007Sroberto			}
657182007Sroberto		if (outformat == FORMAT_ASN1)
658200576Sroberto			i = i2d_ECPrivateKey_bio(out, eckey);
65954359Sroberto		else if (outformat == FORMAT_PEM)
66054359Sroberto			i = PEM_write_bio_ECPrivateKey(out, eckey, NULL,
661285612Sdelphij				NULL, 0, NULL, NULL);
662285612Sdelphij		else
663285612Sdelphij			{
664285612Sdelphij			BIO_printf(bio_err, "bad output format specified "
665285612Sdelphij				"for outfile\n");
666285612Sdelphij			EC_KEY_free(eckey);
667285612Sdelphij			goto end;
668285612Sdelphij			}
669285612Sdelphij		EC_KEY_free(eckey);
670285612Sdelphij		}
671285612Sdelphij
672285612Sdelphij	if (need_rand)
67354359Sroberto		app_RAND_write_file(NULL, bio_err);
67454359Sroberto
675285612Sdelphij	ret=0;
676285612Sdelphijend:
677285612Sdelphij	if (ec_p)
678285612Sdelphij		BN_free(ec_p);
679285612Sdelphij	if (ec_a)
68054359Sroberto		BN_free(ec_a);
68154359Sroberto	if (ec_b)
68254359Sroberto		BN_free(ec_b);
68354359Sroberto	if (ec_gen)
68454359Sroberto		BN_free(ec_gen);
68554359Sroberto	if (ec_order)
68654359Sroberto		BN_free(ec_order);
687200576Sroberto	if (ec_cofactor)
688182007Sroberto		BN_free(ec_cofactor);
68954359Sroberto	if (buffer)
69054359Sroberto		OPENSSL_free(buffer);
69154359Sroberto	if (in != NULL)
69254359Sroberto		BIO_free(in);
69354359Sroberto	if (out != NULL)
694285612Sdelphij		BIO_free_all(out);
695285612Sdelphij	if (group != NULL)
696285612Sdelphij		EC_GROUP_free(group);
697285612Sdelphij	apps_shutdown();
69854359Sroberto	OPENSSL_EXIT(ret);
699285612Sdelphij}
700200576Sroberto
70154359Srobertostatic int ecparam_print_var(BIO *out, BIGNUM *in, const char *var,
702182007Sroberto	int len, unsigned char *buffer)
703132451Sroberto	{
704182007Sroberto	BIO_printf(out, "static unsigned char %s_%d[] = {", var, len);
705132451Sroberto	if (BN_is_zero(in))
706132451Sroberto		BIO_printf(out, "\n\t0x00");
707182007Sroberto	else
708132451Sroberto		{
70954359Sroberto		int i, l;
710106163Sroberto
711182007Sroberto		l = BN_bn2bin(in, buffer);
712200576Sroberto		for (i=0; i<l-1; i++)
713106163Sroberto			{
71454359Sroberto			if ((i%12) == 0)
715285612Sdelphij				BIO_printf(out, "\n\t");
716200576Sroberto			BIO_printf(out, "0x%02X,", buffer[i]);
717200576Sroberto			}
718182007Sroberto		if ((i%12) == 0)
719200576Sroberto			BIO_printf(out, "\n\t");
720200576Sroberto		BIO_printf(out, "0x%02X", buffer[i]);
721200576Sroberto		}
722200576Sroberto	BIO_printf(out, "\n\t};\n\n");
723200576Sroberto	return 1;
724200576Sroberto	}
725200576Sroberto#else /* !OPENSSL_NO_EC */
726182007Sroberto
727285612Sdelphij# if PEDANTIC
728285612Sdelphijstatic void *dummy=&dummy;
729285612Sdelphij# endif
730285612Sdelphij
731285612Sdelphij#endif
732200576Sroberto