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