1160814Ssimon/* apps/ecparam.c */ 2160814Ssimon/* 3160814Ssimon * Written by Nils Larsch for the OpenSSL project. 4160814Ssimon */ 5160814Ssimon/* ==================================================================== 6160814Ssimon * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. 7160814Ssimon * 8160814Ssimon * Redistribution and use in source and binary forms, with or without 9160814Ssimon * modification, are permitted provided that the following conditions 10160814Ssimon * are met: 11160814Ssimon * 12160814Ssimon * 1. Redistributions of source code must retain the above copyright 13160814Ssimon * notice, this list of conditions and the following disclaimer. 14160814Ssimon * 15160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright 16160814Ssimon * notice, this list of conditions and the following disclaimer in 17160814Ssimon * the documentation and/or other materials provided with the 18160814Ssimon * distribution. 19160814Ssimon * 20160814Ssimon * 3. All advertising materials mentioning features or use of this 21160814Ssimon * software must display the following acknowledgment: 22160814Ssimon * "This product includes software developed by the OpenSSL Project 23160814Ssimon * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 24160814Ssimon * 25160814Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26160814Ssimon * endorse or promote products derived from this software without 27160814Ssimon * prior written permission. For written permission, please contact 28160814Ssimon * openssl-core@openssl.org. 29160814Ssimon * 30160814Ssimon * 5. Products derived from this software may not be called "OpenSSL" 31160814Ssimon * nor may "OpenSSL" appear in their names without prior written 32160814Ssimon * permission of the OpenSSL Project. 33160814Ssimon * 34160814Ssimon * 6. Redistributions of any form whatsoever must retain the following 35160814Ssimon * acknowledgment: 36160814Ssimon * "This product includes software developed by the OpenSSL Project 37160814Ssimon * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 38160814Ssimon * 39160814Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40160814Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42160814Ssimon * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43160814Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44160814Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45160814Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46160814Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48160814Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49160814Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50160814Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE. 51160814Ssimon * ==================================================================== 52160814Ssimon * 53160814Ssimon * This product includes cryptographic software written by Eric Young 54160814Ssimon * (eay@cryptsoft.com). This product includes software written by Tim 55160814Ssimon * Hudson (tjh@cryptsoft.com). 56160814Ssimon * 57160814Ssimon */ 58160814Ssimon/* ==================================================================== 59160814Ssimon * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 60160814Ssimon * 61160814Ssimon * Portions of the attached software ("Contribution") are developed by 62160814Ssimon * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. 63160814Ssimon * 64160814Ssimon * The Contribution is licensed pursuant to the OpenSSL open source 65160814Ssimon * license provided above. 66160814Ssimon * 67160814Ssimon * The elliptic curve binary polynomial software is originally written by 68160814Ssimon * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories. 69160814Ssimon * 70160814Ssimon */ 71160814Ssimon 72160814Ssimon#include <openssl/opensslconf.h> 73160814Ssimon#ifndef OPENSSL_NO_EC 74160814Ssimon#include <assert.h> 75160814Ssimon#include <stdio.h> 76160814Ssimon#include <stdlib.h> 77160814Ssimon#include <time.h> 78160814Ssimon#include <string.h> 79160814Ssimon#include "apps.h" 80160814Ssimon#include <openssl/bio.h> 81160814Ssimon#include <openssl/err.h> 82160814Ssimon#include <openssl/bn.h> 83160814Ssimon#include <openssl/ec.h> 84160814Ssimon#include <openssl/x509.h> 85160814Ssimon#include <openssl/pem.h> 86160814Ssimon 87160814Ssimon#undef PROG 88160814Ssimon#define PROG ecparam_main 89160814Ssimon 90160814Ssimon/* -inform arg - input format - default PEM (DER or PEM) 91160814Ssimon * -outform arg - output format - default PEM 92160814Ssimon * -in arg - input file - default stdin 93160814Ssimon * -out arg - output file - default stdout 94160814Ssimon * -noout - do not print the ec parameter 95160814Ssimon * -text - print the ec parameters in text form 96160814Ssimon * -check - validate the ec parameters 97160814Ssimon * -C - print a 'C' function creating the parameters 98160814Ssimon * -name arg - use the ec parameters with 'short name' name 99160814Ssimon * -list_curves - prints a list of all currently available curve 'short names' 100160814Ssimon * -conv_form arg - specifies the point conversion form 101160814Ssimon * - possible values: compressed 102160814Ssimon * uncompressed (default) 103160814Ssimon * hybrid 104160814Ssimon * -param_enc arg - specifies the way the ec parameters are encoded 105160814Ssimon * in the asn1 der encoding 106160814Ssimon * possible values: named_curve (default) 107160814Ssimon * explicit 108279264Sdelphij * -no_seed - if 'explicit' parameters are chosen do not use the seed 109160814Ssimon * -genkey - generate ec key 110160814Ssimon * -rand file - files to use for random number input 111160814Ssimon * -engine e - use engine e, possibly a hardware device 112160814Ssimon */ 113160814Ssimon 114160814Ssimon 115160814Ssimonstatic int ecparam_print_var(BIO *,BIGNUM *,const char *,int,unsigned char *); 116160814Ssimon 117160814Ssimonint MAIN(int, char **); 118160814Ssimon 119160814Ssimonint MAIN(int argc, char **argv) 120160814Ssimon { 121160814Ssimon EC_GROUP *group = NULL; 122160814Ssimon point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED; 123160814Ssimon int new_form = 0; 124160814Ssimon int asn1_flag = OPENSSL_EC_NAMED_CURVE; 125160814Ssimon int new_asn1_flag = 0; 126160814Ssimon char *curve_name = NULL, *inrand = NULL; 127160814Ssimon int list_curves = 0, no_seed = 0, check = 0, 128160814Ssimon badops = 0, text = 0, i, need_rand = 0, genkey = 0; 129160814Ssimon char *infile = NULL, *outfile = NULL, *prog; 130160814Ssimon BIO *in = NULL, *out = NULL; 131160814Ssimon int informat, outformat, noout = 0, C = 0, ret = 1; 132160814Ssimon char *engine = NULL; 133160814Ssimon 134160814Ssimon BIGNUM *ec_p = NULL, *ec_a = NULL, *ec_b = NULL, 135160814Ssimon *ec_gen = NULL, *ec_order = NULL, *ec_cofactor = NULL; 136160814Ssimon unsigned char *buffer = NULL; 137160814Ssimon 138160814Ssimon apps_startup(); 139160814Ssimon 140160814Ssimon if (bio_err == NULL) 141160814Ssimon if ((bio_err=BIO_new(BIO_s_file())) != NULL) 142160814Ssimon BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); 143160814Ssimon 144160814Ssimon if (!load_config(bio_err, NULL)) 145160814Ssimon goto end; 146160814Ssimon 147160814Ssimon informat=FORMAT_PEM; 148160814Ssimon outformat=FORMAT_PEM; 149160814Ssimon 150160814Ssimon prog=argv[0]; 151160814Ssimon argc--; 152160814Ssimon argv++; 153160814Ssimon while (argc >= 1) 154160814Ssimon { 155160814Ssimon if (strcmp(*argv,"-inform") == 0) 156160814Ssimon { 157160814Ssimon if (--argc < 1) goto bad; 158160814Ssimon informat=str2fmt(*(++argv)); 159160814Ssimon } 160160814Ssimon else if (strcmp(*argv,"-outform") == 0) 161160814Ssimon { 162160814Ssimon if (--argc < 1) goto bad; 163160814Ssimon outformat=str2fmt(*(++argv)); 164160814Ssimon } 165160814Ssimon else if (strcmp(*argv,"-in") == 0) 166160814Ssimon { 167160814Ssimon if (--argc < 1) goto bad; 168160814Ssimon infile= *(++argv); 169160814Ssimon } 170160814Ssimon else if (strcmp(*argv,"-out") == 0) 171160814Ssimon { 172160814Ssimon if (--argc < 1) goto bad; 173160814Ssimon outfile= *(++argv); 174160814Ssimon } 175160814Ssimon else if (strcmp(*argv,"-text") == 0) 176160814Ssimon text = 1; 177160814Ssimon else if (strcmp(*argv,"-C") == 0) 178160814Ssimon C = 1; 179160814Ssimon else if (strcmp(*argv,"-check") == 0) 180160814Ssimon check = 1; 181160814Ssimon else if (strcmp (*argv, "-name") == 0) 182160814Ssimon { 183160814Ssimon if (--argc < 1) 184160814Ssimon goto bad; 185160814Ssimon curve_name = *(++argv); 186160814Ssimon } 187160814Ssimon else if (strcmp(*argv, "-list_curves") == 0) 188160814Ssimon list_curves = 1; 189160814Ssimon else if (strcmp(*argv, "-conv_form") == 0) 190160814Ssimon { 191160814Ssimon if (--argc < 1) 192160814Ssimon goto bad; 193160814Ssimon ++argv; 194160814Ssimon new_form = 1; 195160814Ssimon if (strcmp(*argv, "compressed") == 0) 196160814Ssimon form = POINT_CONVERSION_COMPRESSED; 197160814Ssimon else if (strcmp(*argv, "uncompressed") == 0) 198160814Ssimon form = POINT_CONVERSION_UNCOMPRESSED; 199160814Ssimon else if (strcmp(*argv, "hybrid") == 0) 200160814Ssimon form = POINT_CONVERSION_HYBRID; 201160814Ssimon else 202160814Ssimon goto bad; 203160814Ssimon } 204160814Ssimon else if (strcmp(*argv, "-param_enc") == 0) 205160814Ssimon { 206160814Ssimon if (--argc < 1) 207160814Ssimon goto bad; 208160814Ssimon ++argv; 209160814Ssimon new_asn1_flag = 1; 210160814Ssimon if (strcmp(*argv, "named_curve") == 0) 211160814Ssimon asn1_flag = OPENSSL_EC_NAMED_CURVE; 212160814Ssimon else if (strcmp(*argv, "explicit") == 0) 213160814Ssimon asn1_flag = 0; 214160814Ssimon else 215160814Ssimon goto bad; 216160814Ssimon } 217160814Ssimon else if (strcmp(*argv, "-no_seed") == 0) 218160814Ssimon no_seed = 1; 219160814Ssimon else if (strcmp(*argv, "-noout") == 0) 220160814Ssimon noout=1; 221160814Ssimon else if (strcmp(*argv,"-genkey") == 0) 222160814Ssimon { 223160814Ssimon genkey=1; 224160814Ssimon need_rand=1; 225160814Ssimon } 226160814Ssimon else if (strcmp(*argv, "-rand") == 0) 227160814Ssimon { 228160814Ssimon if (--argc < 1) goto bad; 229160814Ssimon inrand= *(++argv); 230160814Ssimon need_rand=1; 231160814Ssimon } 232160814Ssimon else if(strcmp(*argv, "-engine") == 0) 233160814Ssimon { 234160814Ssimon if (--argc < 1) goto bad; 235160814Ssimon engine = *(++argv); 236160814Ssimon } 237160814Ssimon else 238160814Ssimon { 239160814Ssimon BIO_printf(bio_err,"unknown option %s\n",*argv); 240160814Ssimon badops=1; 241160814Ssimon break; 242160814Ssimon } 243160814Ssimon argc--; 244160814Ssimon argv++; 245160814Ssimon } 246160814Ssimon 247160814Ssimon if (badops) 248160814Ssimon { 249160814Ssimonbad: 250160814Ssimon BIO_printf(bio_err, "%s [options] <infile >outfile\n",prog); 251160814Ssimon BIO_printf(bio_err, "where options are\n"); 252160814Ssimon BIO_printf(bio_err, " -inform arg input format - " 253160814Ssimon "default PEM (DER or PEM)\n"); 254160814Ssimon BIO_printf(bio_err, " -outform arg output format - " 255160814Ssimon "default PEM\n"); 256160814Ssimon BIO_printf(bio_err, " -in arg input file - " 257160814Ssimon "default stdin\n"); 258160814Ssimon BIO_printf(bio_err, " -out arg output file - " 259160814Ssimon "default stdout\n"); 260160814Ssimon BIO_printf(bio_err, " -noout do not print the " 261160814Ssimon "ec parameter\n"); 262160814Ssimon BIO_printf(bio_err, " -text print the ec " 263160814Ssimon "parameters in text form\n"); 264160814Ssimon BIO_printf(bio_err, " -check validate the ec " 265160814Ssimon "parameters\n"); 266160814Ssimon BIO_printf(bio_err, " -C print a 'C' " 267160814Ssimon "function creating the parameters\n"); 268160814Ssimon BIO_printf(bio_err, " -name arg use the " 269160814Ssimon "ec parameters with 'short name' name\n"); 270160814Ssimon BIO_printf(bio_err, " -list_curves prints a list of " 271160814Ssimon "all currently available curve 'short names'\n"); 272160814Ssimon BIO_printf(bio_err, " -conv_form arg specifies the " 273160814Ssimon "point conversion form \n"); 274160814Ssimon BIO_printf(bio_err, " possible values:" 275160814Ssimon " compressed\n"); 276160814Ssimon BIO_printf(bio_err, " " 277160814Ssimon " uncompressed (default)\n"); 278160814Ssimon BIO_printf(bio_err, " " 279160814Ssimon " hybrid\n"); 280160814Ssimon BIO_printf(bio_err, " -param_enc arg specifies the way" 281160814Ssimon " the ec parameters are encoded\n"); 282160814Ssimon BIO_printf(bio_err, " in the asn1 der " 283160814Ssimon "encoding\n"); 284160814Ssimon BIO_printf(bio_err, " possible values:" 285160814Ssimon " named_curve (default)\n"); 286160814Ssimon BIO_printf(bio_err, " " 287160814Ssimon " explicit\n"); 288160814Ssimon BIO_printf(bio_err, " -no_seed if 'explicit'" 289279264Sdelphij " parameters are chosen do not" 290160814Ssimon " use the seed\n"); 291160814Ssimon BIO_printf(bio_err, " -genkey generate ec" 292160814Ssimon " key\n"); 293160814Ssimon BIO_printf(bio_err, " -rand file files to use for" 294160814Ssimon " random number input\n"); 295160814Ssimon BIO_printf(bio_err, " -engine e use engine e, " 296160814Ssimon "possibly a hardware device\n"); 297160814Ssimon goto end; 298160814Ssimon } 299160814Ssimon 300160814Ssimon ERR_load_crypto_strings(); 301160814Ssimon 302160814Ssimon in=BIO_new(BIO_s_file()); 303160814Ssimon out=BIO_new(BIO_s_file()); 304160814Ssimon if ((in == NULL) || (out == NULL)) 305160814Ssimon { 306160814Ssimon ERR_print_errors(bio_err); 307160814Ssimon goto end; 308160814Ssimon } 309160814Ssimon 310160814Ssimon if (infile == NULL) 311160814Ssimon BIO_set_fp(in,stdin,BIO_NOCLOSE); 312160814Ssimon else 313160814Ssimon { 314160814Ssimon if (BIO_read_filename(in,infile) <= 0) 315160814Ssimon { 316160814Ssimon perror(infile); 317160814Ssimon goto end; 318160814Ssimon } 319160814Ssimon } 320160814Ssimon if (outfile == NULL) 321160814Ssimon { 322160814Ssimon BIO_set_fp(out,stdout,BIO_NOCLOSE); 323160814Ssimon#ifdef OPENSSL_SYS_VMS 324160814Ssimon { 325160814Ssimon BIO *tmpbio = BIO_new(BIO_f_linebuffer()); 326160814Ssimon out = BIO_push(tmpbio, out); 327160814Ssimon } 328160814Ssimon#endif 329160814Ssimon } 330160814Ssimon else 331160814Ssimon { 332160814Ssimon if (BIO_write_filename(out,outfile) <= 0) 333160814Ssimon { 334160814Ssimon perror(outfile); 335160814Ssimon goto end; 336160814Ssimon } 337160814Ssimon } 338160814Ssimon 339160814Ssimon#ifndef OPENSSL_NO_ENGINE 340215697Ssimon setup_engine(bio_err, engine, 0); 341160814Ssimon#endif 342160814Ssimon 343160814Ssimon if (list_curves) 344160814Ssimon { 345160814Ssimon EC_builtin_curve *curves = NULL; 346160814Ssimon size_t crv_len = 0; 347160814Ssimon size_t n = 0; 348160814Ssimon 349160814Ssimon crv_len = EC_get_builtin_curves(NULL, 0); 350160814Ssimon 351160814Ssimon curves = OPENSSL_malloc((int)(sizeof(EC_builtin_curve) * crv_len)); 352160814Ssimon 353160814Ssimon if (curves == NULL) 354160814Ssimon goto end; 355160814Ssimon 356160814Ssimon if (!EC_get_builtin_curves(curves, crv_len)) 357160814Ssimon { 358160814Ssimon OPENSSL_free(curves); 359160814Ssimon goto end; 360160814Ssimon } 361160814Ssimon 362160814Ssimon 363160814Ssimon for (n = 0; n < crv_len; n++) 364160814Ssimon { 365160814Ssimon const char *comment; 366160814Ssimon const char *sname; 367160814Ssimon comment = curves[n].comment; 368160814Ssimon sname = OBJ_nid2sn(curves[n].nid); 369160814Ssimon if (comment == NULL) 370160814Ssimon comment = "CURVE DESCRIPTION NOT AVAILABLE"; 371160814Ssimon if (sname == NULL) 372160814Ssimon sname = ""; 373160814Ssimon 374160814Ssimon BIO_printf(out, " %-10s: ", sname); 375160814Ssimon BIO_printf(out, "%s\n", comment); 376160814Ssimon } 377160814Ssimon 378160814Ssimon OPENSSL_free(curves); 379160814Ssimon ret = 0; 380160814Ssimon goto end; 381160814Ssimon } 382160814Ssimon 383160814Ssimon if (curve_name != NULL) 384160814Ssimon { 385160814Ssimon int nid; 386160814Ssimon 387160814Ssimon /* workaround for the SECG curve names secp192r1 388160814Ssimon * and secp256r1 (which are the same as the curves 389160814Ssimon * prime192v1 and prime256v1 defined in X9.62) 390160814Ssimon */ 391160814Ssimon if (!strcmp(curve_name, "secp192r1")) 392160814Ssimon { 393160814Ssimon BIO_printf(bio_err, "using curve name prime192v1 " 394160814Ssimon "instead of secp192r1\n"); 395160814Ssimon nid = NID_X9_62_prime192v1; 396160814Ssimon } 397160814Ssimon else if (!strcmp(curve_name, "secp256r1")) 398160814Ssimon { 399160814Ssimon BIO_printf(bio_err, "using curve name prime256v1 " 400160814Ssimon "instead of secp256r1\n"); 401160814Ssimon nid = NID_X9_62_prime256v1; 402160814Ssimon } 403160814Ssimon else 404160814Ssimon nid = OBJ_sn2nid(curve_name); 405160814Ssimon 406160814Ssimon if (nid == 0) 407160814Ssimon { 408160814Ssimon BIO_printf(bio_err, "unknown curve name (%s)\n", 409160814Ssimon curve_name); 410160814Ssimon goto end; 411160814Ssimon } 412160814Ssimon 413160814Ssimon group = EC_GROUP_new_by_curve_name(nid); 414160814Ssimon if (group == NULL) 415160814Ssimon { 416160814Ssimon BIO_printf(bio_err, "unable to create curve (%s)\n", 417160814Ssimon curve_name); 418160814Ssimon goto end; 419160814Ssimon } 420160814Ssimon EC_GROUP_set_asn1_flag(group, asn1_flag); 421160814Ssimon EC_GROUP_set_point_conversion_form(group, form); 422160814Ssimon } 423160814Ssimon else if (informat == FORMAT_ASN1) 424160814Ssimon { 425160814Ssimon group = d2i_ECPKParameters_bio(in, NULL); 426160814Ssimon } 427160814Ssimon else if (informat == FORMAT_PEM) 428160814Ssimon { 429160814Ssimon group = PEM_read_bio_ECPKParameters(in,NULL,NULL,NULL); 430160814Ssimon } 431160814Ssimon else 432160814Ssimon { 433160814Ssimon BIO_printf(bio_err, "bad input format specified\n"); 434160814Ssimon goto end; 435160814Ssimon } 436160814Ssimon 437160814Ssimon if (group == NULL) 438160814Ssimon { 439160814Ssimon BIO_printf(bio_err, 440160814Ssimon "unable to load elliptic curve parameters\n"); 441160814Ssimon ERR_print_errors(bio_err); 442160814Ssimon goto end; 443160814Ssimon } 444160814Ssimon 445160814Ssimon if (new_form) 446160814Ssimon EC_GROUP_set_point_conversion_form(group, form); 447160814Ssimon 448160814Ssimon if (new_asn1_flag) 449160814Ssimon EC_GROUP_set_asn1_flag(group, asn1_flag); 450160814Ssimon 451160814Ssimon if (no_seed) 452160814Ssimon { 453160814Ssimon EC_GROUP_set_seed(group, NULL, 0); 454160814Ssimon } 455160814Ssimon 456160814Ssimon if (text) 457160814Ssimon { 458160814Ssimon if (!ECPKParameters_print(out, group, 0)) 459160814Ssimon goto end; 460160814Ssimon } 461160814Ssimon 462160814Ssimon if (check) 463160814Ssimon { 464160814Ssimon if (group == NULL) 465160814Ssimon BIO_printf(bio_err, "no elliptic curve parameters\n"); 466160814Ssimon BIO_printf(bio_err, "checking elliptic curve parameters: "); 467160814Ssimon if (!EC_GROUP_check(group, NULL)) 468160814Ssimon { 469160814Ssimon BIO_printf(bio_err, "failed\n"); 470160814Ssimon ERR_print_errors(bio_err); 471160814Ssimon } 472160814Ssimon else 473160814Ssimon BIO_printf(bio_err, "ok\n"); 474160814Ssimon 475160814Ssimon } 476160814Ssimon 477160814Ssimon if (C) 478160814Ssimon { 479160814Ssimon size_t buf_len = 0, tmp_len = 0; 480160814Ssimon const EC_POINT *point; 481160814Ssimon int is_prime, len = 0; 482160814Ssimon const EC_METHOD *meth = EC_GROUP_method_of(group); 483160814Ssimon 484160814Ssimon if ((ec_p = BN_new()) == NULL || (ec_a = BN_new()) == NULL || 485160814Ssimon (ec_b = BN_new()) == NULL || (ec_gen = BN_new()) == NULL || 486160814Ssimon (ec_order = BN_new()) == NULL || 487160814Ssimon (ec_cofactor = BN_new()) == NULL ) 488160814Ssimon { 489160814Ssimon perror("OPENSSL_malloc"); 490160814Ssimon goto end; 491160814Ssimon } 492160814Ssimon 493160814Ssimon is_prime = (EC_METHOD_get_field_type(meth) == 494160814Ssimon NID_X9_62_prime_field); 495160814Ssimon 496160814Ssimon if (is_prime) 497160814Ssimon { 498160814Ssimon if (!EC_GROUP_get_curve_GFp(group, ec_p, ec_a, 499160814Ssimon ec_b, NULL)) 500160814Ssimon goto end; 501160814Ssimon } 502160814Ssimon else 503160814Ssimon { 504160814Ssimon /* TODO */ 505160814Ssimon goto end; 506160814Ssimon } 507160814Ssimon 508160814Ssimon if ((point = EC_GROUP_get0_generator(group)) == NULL) 509160814Ssimon goto end; 510160814Ssimon if (!EC_POINT_point2bn(group, point, 511160814Ssimon EC_GROUP_get_point_conversion_form(group), ec_gen, 512160814Ssimon NULL)) 513160814Ssimon goto end; 514160814Ssimon if (!EC_GROUP_get_order(group, ec_order, NULL)) 515160814Ssimon goto end; 516160814Ssimon if (!EC_GROUP_get_cofactor(group, ec_cofactor, NULL)) 517160814Ssimon goto end; 518160814Ssimon 519160814Ssimon if (!ec_p || !ec_a || !ec_b || !ec_gen || 520160814Ssimon !ec_order || !ec_cofactor) 521160814Ssimon goto end; 522160814Ssimon 523160814Ssimon len = BN_num_bits(ec_order); 524160814Ssimon 525160814Ssimon if ((tmp_len = (size_t)BN_num_bytes(ec_p)) > buf_len) 526160814Ssimon buf_len = tmp_len; 527160814Ssimon if ((tmp_len = (size_t)BN_num_bytes(ec_a)) > buf_len) 528160814Ssimon buf_len = tmp_len; 529160814Ssimon if ((tmp_len = (size_t)BN_num_bytes(ec_b)) > buf_len) 530160814Ssimon buf_len = tmp_len; 531160814Ssimon if ((tmp_len = (size_t)BN_num_bytes(ec_gen)) > buf_len) 532160814Ssimon buf_len = tmp_len; 533160814Ssimon if ((tmp_len = (size_t)BN_num_bytes(ec_order)) > buf_len) 534160814Ssimon buf_len = tmp_len; 535160814Ssimon if ((tmp_len = (size_t)BN_num_bytes(ec_cofactor)) > buf_len) 536160814Ssimon buf_len = tmp_len; 537160814Ssimon 538160814Ssimon buffer = (unsigned char *)OPENSSL_malloc(buf_len); 539160814Ssimon 540160814Ssimon if (buffer == NULL) 541160814Ssimon { 542160814Ssimon perror("OPENSSL_malloc"); 543160814Ssimon goto end; 544160814Ssimon } 545160814Ssimon 546160814Ssimon ecparam_print_var(out, ec_p, "ec_p", len, buffer); 547160814Ssimon ecparam_print_var(out, ec_a, "ec_a", len, buffer); 548160814Ssimon ecparam_print_var(out, ec_b, "ec_b", len, buffer); 549160814Ssimon ecparam_print_var(out, ec_gen, "ec_gen", len, buffer); 550160814Ssimon ecparam_print_var(out, ec_order, "ec_order", len, buffer); 551160814Ssimon ecparam_print_var(out, ec_cofactor, "ec_cofactor", len, 552160814Ssimon buffer); 553160814Ssimon 554160814Ssimon BIO_printf(out, "\n\n"); 555160814Ssimon 556160814Ssimon BIO_printf(out, "EC_GROUP *get_ec_group_%d(void)\n\t{\n", len); 557160814Ssimon BIO_printf(out, "\tint ok=0;\n"); 558160814Ssimon BIO_printf(out, "\tEC_GROUP *group = NULL;\n"); 559160814Ssimon BIO_printf(out, "\tEC_POINT *point = NULL;\n"); 560160814Ssimon BIO_printf(out, "\tBIGNUM *tmp_1 = NULL, *tmp_2 = NULL, " 561160814Ssimon "*tmp_3 = NULL;\n\n"); 562160814Ssimon BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_p_%d, " 563160814Ssimon "sizeof(ec_p_%d), NULL)) == NULL)\n\t\t" 564160814Ssimon "goto err;\n", len, len); 565160814Ssimon BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_a_%d, " 566160814Ssimon "sizeof(ec_a_%d), NULL)) == NULL)\n\t\t" 567160814Ssimon "goto err;\n", len, len); 568160814Ssimon BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_b_%d, " 569160814Ssimon "sizeof(ec_b_%d), NULL)) == NULL)\n\t\t" 570160814Ssimon "goto err;\n", len, len); 571160814Ssimon if (is_prime) 572160814Ssimon { 573160814Ssimon BIO_printf(out, "\tif ((group = EC_GROUP_new_curve_" 574160814Ssimon "GFp(tmp_1, tmp_2, tmp_3, NULL)) == NULL)" 575160814Ssimon "\n\t\tgoto err;\n\n"); 576160814Ssimon } 577160814Ssimon else 578160814Ssimon { 579160814Ssimon /* TODO */ 580160814Ssimon goto end; 581160814Ssimon } 582160814Ssimon BIO_printf(out, "\t/* build generator */\n"); 583160814Ssimon BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_gen_%d, " 584160814Ssimon "sizeof(ec_gen_%d), tmp_1)) == NULL)" 585160814Ssimon "\n\t\tgoto err;\n", len, len); 586160814Ssimon BIO_printf(out, "\tpoint = EC_POINT_bn2point(group, tmp_1, " 587160814Ssimon "NULL, NULL);\n"); 588160814Ssimon BIO_printf(out, "\tif (point == NULL)\n\t\tgoto err;\n"); 589160814Ssimon BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_order_%d, " 590160814Ssimon "sizeof(ec_order_%d), tmp_2)) == NULL)" 591160814Ssimon "\n\t\tgoto err;\n", len, len); 592160814Ssimon BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_cofactor_%d, " 593160814Ssimon "sizeof(ec_cofactor_%d), tmp_3)) == NULL)" 594160814Ssimon "\n\t\tgoto err;\n", len, len); 595160814Ssimon BIO_printf(out, "\tif (!EC_GROUP_set_generator(group, point," 596160814Ssimon " tmp_2, tmp_3))\n\t\tgoto err;\n"); 597160814Ssimon BIO_printf(out, "\n\tok=1;\n"); 598160814Ssimon BIO_printf(out, "err:\n"); 599160814Ssimon BIO_printf(out, "\tif (tmp_1)\n\t\tBN_free(tmp_1);\n"); 600160814Ssimon BIO_printf(out, "\tif (tmp_2)\n\t\tBN_free(tmp_2);\n"); 601160814Ssimon BIO_printf(out, "\tif (tmp_3)\n\t\tBN_free(tmp_3);\n"); 602160814Ssimon BIO_printf(out, "\tif (point)\n\t\tEC_POINT_free(point);\n"); 603160814Ssimon BIO_printf(out, "\tif (!ok)\n"); 604160814Ssimon BIO_printf(out, "\t\t{\n"); 605160814Ssimon BIO_printf(out, "\t\tEC_GROUP_free(group);\n"); 606160814Ssimon BIO_printf(out, "\t\tgroup = NULL;\n"); 607160814Ssimon BIO_printf(out, "\t\t}\n"); 608160814Ssimon BIO_printf(out, "\treturn(group);\n\t}\n"); 609160814Ssimon } 610160814Ssimon 611160814Ssimon if (!noout) 612160814Ssimon { 613160814Ssimon if (outformat == FORMAT_ASN1) 614160814Ssimon i = i2d_ECPKParameters_bio(out, group); 615160814Ssimon else if (outformat == FORMAT_PEM) 616160814Ssimon i = PEM_write_bio_ECPKParameters(out, group); 617160814Ssimon else 618160814Ssimon { 619160814Ssimon BIO_printf(bio_err,"bad output format specified for" 620160814Ssimon " outfile\n"); 621160814Ssimon goto end; 622160814Ssimon } 623160814Ssimon if (!i) 624160814Ssimon { 625160814Ssimon BIO_printf(bio_err, "unable to write elliptic " 626160814Ssimon "curve parameters\n"); 627160814Ssimon ERR_print_errors(bio_err); 628160814Ssimon goto end; 629160814Ssimon } 630160814Ssimon } 631160814Ssimon 632160814Ssimon if (need_rand) 633160814Ssimon { 634160814Ssimon app_RAND_load_file(NULL, bio_err, (inrand != NULL)); 635160814Ssimon if (inrand != NULL) 636160814Ssimon BIO_printf(bio_err,"%ld semi-random bytes loaded\n", 637160814Ssimon app_RAND_load_files(inrand)); 638160814Ssimon } 639160814Ssimon 640160814Ssimon if (genkey) 641160814Ssimon { 642160814Ssimon EC_KEY *eckey = EC_KEY_new(); 643160814Ssimon 644160814Ssimon if (eckey == NULL) 645160814Ssimon goto end; 646160814Ssimon 647160814Ssimon assert(need_rand); 648160814Ssimon 649160814Ssimon if (EC_KEY_set_group(eckey, group) == 0) 650160814Ssimon goto end; 651160814Ssimon 652160814Ssimon if (!EC_KEY_generate_key(eckey)) 653160814Ssimon { 654160814Ssimon EC_KEY_free(eckey); 655160814Ssimon goto end; 656160814Ssimon } 657160814Ssimon if (outformat == FORMAT_ASN1) 658160814Ssimon i = i2d_ECPrivateKey_bio(out, eckey); 659160814Ssimon else if (outformat == FORMAT_PEM) 660160814Ssimon i = PEM_write_bio_ECPrivateKey(out, eckey, NULL, 661160814Ssimon NULL, 0, NULL, NULL); 662160814Ssimon else 663160814Ssimon { 664160814Ssimon BIO_printf(bio_err, "bad output format specified " 665160814Ssimon "for outfile\n"); 666160814Ssimon EC_KEY_free(eckey); 667160814Ssimon goto end; 668160814Ssimon } 669160814Ssimon EC_KEY_free(eckey); 670160814Ssimon } 671160814Ssimon 672160814Ssimon if (need_rand) 673160814Ssimon app_RAND_write_file(NULL, bio_err); 674160814Ssimon 675160814Ssimon ret=0; 676160814Ssimonend: 677160814Ssimon if (ec_p) 678160814Ssimon BN_free(ec_p); 679160814Ssimon if (ec_a) 680160814Ssimon BN_free(ec_a); 681160814Ssimon if (ec_b) 682160814Ssimon BN_free(ec_b); 683160814Ssimon if (ec_gen) 684160814Ssimon BN_free(ec_gen); 685160814Ssimon if (ec_order) 686160814Ssimon BN_free(ec_order); 687160814Ssimon if (ec_cofactor) 688160814Ssimon BN_free(ec_cofactor); 689160814Ssimon if (buffer) 690160814Ssimon OPENSSL_free(buffer); 691160814Ssimon if (in != NULL) 692160814Ssimon BIO_free(in); 693160814Ssimon if (out != NULL) 694160814Ssimon BIO_free_all(out); 695160814Ssimon if (group != NULL) 696160814Ssimon EC_GROUP_free(group); 697160814Ssimon apps_shutdown(); 698160814Ssimon OPENSSL_EXIT(ret); 699160814Ssimon} 700160814Ssimon 701160814Ssimonstatic int ecparam_print_var(BIO *out, BIGNUM *in, const char *var, 702160814Ssimon int len, unsigned char *buffer) 703160814Ssimon { 704160814Ssimon BIO_printf(out, "static unsigned char %s_%d[] = {", var, len); 705160814Ssimon if (BN_is_zero(in)) 706160814Ssimon BIO_printf(out, "\n\t0x00"); 707160814Ssimon else 708160814Ssimon { 709160814Ssimon int i, l; 710160814Ssimon 711160814Ssimon l = BN_bn2bin(in, buffer); 712160814Ssimon for (i=0; i<l-1; i++) 713160814Ssimon { 714160814Ssimon if ((i%12) == 0) 715160814Ssimon BIO_printf(out, "\n\t"); 716160814Ssimon BIO_printf(out, "0x%02X,", buffer[i]); 717160814Ssimon } 718160814Ssimon if ((i%12) == 0) 719160814Ssimon BIO_printf(out, "\n\t"); 720160814Ssimon BIO_printf(out, "0x%02X", buffer[i]); 721160814Ssimon } 722160814Ssimon BIO_printf(out, "\n\t};\n\n"); 723160814Ssimon return 1; 724160814Ssimon } 725238405Sjkim#else /* !OPENSSL_NO_EC */ 726238405Sjkim 727238405Sjkim# if PEDANTIC 728238405Sjkimstatic void *dummy=&dummy; 729238405Sjkim# endif 730238405Sjkim 731160814Ssimon#endif 732