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 13280304Sjkim * 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 * 61280304Sjkim * 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 * 67280304Sjkim * 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 74280304Sjkim# include <assert.h> 75280304Sjkim# include <stdio.h> 76280304Sjkim# include <stdlib.h> 77280304Sjkim# include <time.h> 78280304Sjkim# include <string.h> 79280304Sjkim# include "apps.h" 80280304Sjkim# include <openssl/bio.h> 81280304Sjkim# include <openssl/err.h> 82280304Sjkim# include <openssl/bn.h> 83280304Sjkim# include <openssl/ec.h> 84280304Sjkim# include <openssl/x509.h> 85280304Sjkim# include <openssl/pem.h> 86160814Ssimon 87280304Sjkim# undef PROG 88280304Sjkim# define PROG ecparam_main 89160814Ssimon 90280304Sjkim/*- 91280304Sjkim * -inform arg - input format - default PEM (DER or PEM) 92160814Ssimon * -outform arg - output format - default PEM 93160814Ssimon * -in arg - input file - default stdin 94160814Ssimon * -out arg - output file - default stdout 95160814Ssimon * -noout - do not print the ec parameter 96160814Ssimon * -text - print the ec parameters in text form 97160814Ssimon * -check - validate the ec parameters 98160814Ssimon * -C - print a 'C' function creating the parameters 99160814Ssimon * -name arg - use the ec parameters with 'short name' name 100160814Ssimon * -list_curves - prints a list of all currently available curve 'short names' 101280304Sjkim * -conv_form arg - specifies the point conversion form 102160814Ssimon * - possible values: compressed 103160814Ssimon * uncompressed (default) 104160814Ssimon * hybrid 105160814Ssimon * -param_enc arg - specifies the way the ec parameters are encoded 106160814Ssimon * in the asn1 der encoding 107160814Ssimon * possible values: named_curve (default) 108160814Ssimon * explicit 109264331Sjkim * -no_seed - if 'explicit' parameters are chosen do not use the seed 110160814Ssimon * -genkey - generate ec key 111160814Ssimon * -rand file - files to use for random number input 112160814Ssimon * -engine e - use engine e, possibly a hardware device 113160814Ssimon */ 114160814Ssimon 115280304Sjkimstatic int ecparam_print_var(BIO *, BIGNUM *, const char *, int, 116280304Sjkim unsigned char *); 117160814Ssimon 118160814Ssimonint MAIN(int, char **); 119160814Ssimon 120160814Ssimonint MAIN(int argc, char **argv) 121280304Sjkim{ 122280304Sjkim EC_GROUP *group = NULL; 123280304Sjkim point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED; 124280304Sjkim int new_form = 0; 125280304Sjkim int asn1_flag = OPENSSL_EC_NAMED_CURVE; 126280304Sjkim int new_asn1_flag = 0; 127280304Sjkim char *curve_name = NULL, *inrand = NULL; 128280304Sjkim int list_curves = 0, no_seed = 0, check = 0, 129280304Sjkim badops = 0, text = 0, i, need_rand = 0, genkey = 0; 130280304Sjkim char *infile = NULL, *outfile = NULL, *prog; 131280304Sjkim BIO *in = NULL, *out = NULL; 132280304Sjkim int informat, outformat, noout = 0, C = 0, ret = 1; 133280304Sjkim char *engine = NULL; 134160814Ssimon 135280304Sjkim BIGNUM *ec_p = NULL, *ec_a = NULL, *ec_b = NULL, 136280304Sjkim *ec_gen = NULL, *ec_order = NULL, *ec_cofactor = NULL; 137280304Sjkim unsigned char *buffer = NULL; 138160814Ssimon 139280304Sjkim apps_startup(); 140160814Ssimon 141280304Sjkim if (bio_err == NULL) 142280304Sjkim if ((bio_err = BIO_new(BIO_s_file())) != NULL) 143280304Sjkim BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT); 144160814Ssimon 145280304Sjkim if (!load_config(bio_err, NULL)) 146280304Sjkim goto end; 147160814Ssimon 148280304Sjkim informat = FORMAT_PEM; 149280304Sjkim outformat = FORMAT_PEM; 150160814Ssimon 151280304Sjkim prog = argv[0]; 152280304Sjkim argc--; 153280304Sjkim argv++; 154280304Sjkim while (argc >= 1) { 155280304Sjkim if (strcmp(*argv, "-inform") == 0) { 156280304Sjkim if (--argc < 1) 157280304Sjkim goto bad; 158280304Sjkim informat = str2fmt(*(++argv)); 159280304Sjkim } else if (strcmp(*argv, "-outform") == 0) { 160280304Sjkim if (--argc < 1) 161280304Sjkim goto bad; 162280304Sjkim outformat = str2fmt(*(++argv)); 163280304Sjkim } else if (strcmp(*argv, "-in") == 0) { 164280304Sjkim if (--argc < 1) 165280304Sjkim goto bad; 166280304Sjkim infile = *(++argv); 167280304Sjkim } else if (strcmp(*argv, "-out") == 0) { 168280304Sjkim if (--argc < 1) 169280304Sjkim goto bad; 170280304Sjkim outfile = *(++argv); 171280304Sjkim } else if (strcmp(*argv, "-text") == 0) 172280304Sjkim text = 1; 173280304Sjkim else if (strcmp(*argv, "-C") == 0) 174280304Sjkim C = 1; 175280304Sjkim else if (strcmp(*argv, "-check") == 0) 176280304Sjkim check = 1; 177280304Sjkim else if (strcmp(*argv, "-name") == 0) { 178280304Sjkim if (--argc < 1) 179280304Sjkim goto bad; 180280304Sjkim curve_name = *(++argv); 181280304Sjkim } else if (strcmp(*argv, "-list_curves") == 0) 182280304Sjkim list_curves = 1; 183280304Sjkim else if (strcmp(*argv, "-conv_form") == 0) { 184280304Sjkim if (--argc < 1) 185280304Sjkim goto bad; 186280304Sjkim ++argv; 187280304Sjkim new_form = 1; 188280304Sjkim if (strcmp(*argv, "compressed") == 0) 189280304Sjkim form = POINT_CONVERSION_COMPRESSED; 190280304Sjkim else if (strcmp(*argv, "uncompressed") == 0) 191280304Sjkim form = POINT_CONVERSION_UNCOMPRESSED; 192280304Sjkim else if (strcmp(*argv, "hybrid") == 0) 193280304Sjkim form = POINT_CONVERSION_HYBRID; 194280304Sjkim else 195280304Sjkim goto bad; 196280304Sjkim } else if (strcmp(*argv, "-param_enc") == 0) { 197280304Sjkim if (--argc < 1) 198280304Sjkim goto bad; 199280304Sjkim ++argv; 200280304Sjkim new_asn1_flag = 1; 201280304Sjkim if (strcmp(*argv, "named_curve") == 0) 202280304Sjkim asn1_flag = OPENSSL_EC_NAMED_CURVE; 203280304Sjkim else if (strcmp(*argv, "explicit") == 0) 204280304Sjkim asn1_flag = 0; 205280304Sjkim else 206280304Sjkim goto bad; 207280304Sjkim } else if (strcmp(*argv, "-no_seed") == 0) 208280304Sjkim no_seed = 1; 209280304Sjkim else if (strcmp(*argv, "-noout") == 0) 210280304Sjkim noout = 1; 211280304Sjkim else if (strcmp(*argv, "-genkey") == 0) { 212280304Sjkim genkey = 1; 213280304Sjkim need_rand = 1; 214280304Sjkim } else if (strcmp(*argv, "-rand") == 0) { 215280304Sjkim if (--argc < 1) 216280304Sjkim goto bad; 217280304Sjkim inrand = *(++argv); 218280304Sjkim need_rand = 1; 219280304Sjkim } else if (strcmp(*argv, "-engine") == 0) { 220280304Sjkim if (--argc < 1) 221280304Sjkim goto bad; 222280304Sjkim engine = *(++argv); 223280304Sjkim } else { 224280304Sjkim BIO_printf(bio_err, "unknown option %s\n", *argv); 225280304Sjkim badops = 1; 226280304Sjkim break; 227280304Sjkim } 228280304Sjkim argc--; 229280304Sjkim argv++; 230280304Sjkim } 231160814Ssimon 232280304Sjkim if (badops) { 233280304Sjkim bad: 234280304Sjkim BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog); 235280304Sjkim BIO_printf(bio_err, "where options are\n"); 236280304Sjkim BIO_printf(bio_err, " -inform arg input format - " 237280304Sjkim "default PEM (DER or PEM)\n"); 238280304Sjkim BIO_printf(bio_err, " -outform arg output format - " 239280304Sjkim "default PEM\n"); 240280304Sjkim BIO_printf(bio_err, " -in arg input file - " 241280304Sjkim "default stdin\n"); 242280304Sjkim BIO_printf(bio_err, " -out arg output file - " 243280304Sjkim "default stdout\n"); 244280304Sjkim BIO_printf(bio_err, " -noout do not print the " 245280304Sjkim "ec parameter\n"); 246280304Sjkim BIO_printf(bio_err, " -text print the ec " 247280304Sjkim "parameters in text form\n"); 248280304Sjkim BIO_printf(bio_err, " -check validate the ec " 249280304Sjkim "parameters\n"); 250280304Sjkim BIO_printf(bio_err, " -C print a 'C' " 251280304Sjkim "function creating the parameters\n"); 252280304Sjkim BIO_printf(bio_err, " -name arg use the " 253280304Sjkim "ec parameters with 'short name' name\n"); 254280304Sjkim BIO_printf(bio_err, " -list_curves prints a list of " 255280304Sjkim "all currently available curve 'short names'\n"); 256280304Sjkim BIO_printf(bio_err, " -conv_form arg specifies the " 257280304Sjkim "point conversion form \n"); 258280304Sjkim BIO_printf(bio_err, " possible values:" 259280304Sjkim " compressed\n"); 260280304Sjkim BIO_printf(bio_err, " " 261280304Sjkim " uncompressed (default)\n"); 262280304Sjkim BIO_printf(bio_err, " " 263280304Sjkim " hybrid\n"); 264280304Sjkim BIO_printf(bio_err, " -param_enc arg specifies the way" 265280304Sjkim " the ec parameters are encoded\n"); 266280304Sjkim BIO_printf(bio_err, " in the asn1 der " 267280304Sjkim "encoding\n"); 268280304Sjkim BIO_printf(bio_err, " possible values:" 269280304Sjkim " named_curve (default)\n"); 270280304Sjkim BIO_printf(bio_err, " " 271280304Sjkim " explicit\n"); 272280304Sjkim BIO_printf(bio_err, " -no_seed if 'explicit'" 273280304Sjkim " parameters are chosen do not" " use the seed\n"); 274280304Sjkim BIO_printf(bio_err, " -genkey generate ec" " key\n"); 275280304Sjkim BIO_printf(bio_err, " -rand file files to use for" 276280304Sjkim " random number input\n"); 277280304Sjkim BIO_printf(bio_err, " -engine e use engine e, " 278280304Sjkim "possibly a hardware device\n"); 279280304Sjkim goto end; 280280304Sjkim } 281160814Ssimon 282280304Sjkim ERR_load_crypto_strings(); 283160814Ssimon 284280304Sjkim in = BIO_new(BIO_s_file()); 285280304Sjkim out = BIO_new(BIO_s_file()); 286280304Sjkim if ((in == NULL) || (out == NULL)) { 287280304Sjkim ERR_print_errors(bio_err); 288280304Sjkim goto end; 289280304Sjkim } 290160814Ssimon 291280304Sjkim if (infile == NULL) 292280304Sjkim BIO_set_fp(in, stdin, BIO_NOCLOSE); 293280304Sjkim else { 294280304Sjkim if (BIO_read_filename(in, infile) <= 0) { 295280304Sjkim perror(infile); 296280304Sjkim goto end; 297280304Sjkim } 298280304Sjkim } 299280304Sjkim if (outfile == NULL) { 300280304Sjkim BIO_set_fp(out, stdout, BIO_NOCLOSE); 301280304Sjkim# ifdef OPENSSL_SYS_VMS 302280304Sjkim { 303280304Sjkim BIO *tmpbio = BIO_new(BIO_f_linebuffer()); 304280304Sjkim out = BIO_push(tmpbio, out); 305280304Sjkim } 306280304Sjkim# endif 307280304Sjkim } else { 308280304Sjkim if (BIO_write_filename(out, outfile) <= 0) { 309280304Sjkim perror(outfile); 310280304Sjkim goto end; 311280304Sjkim } 312280304Sjkim } 313160814Ssimon 314280304Sjkim# ifndef OPENSSL_NO_ENGINE 315280304Sjkim setup_engine(bio_err, engine, 0); 316280304Sjkim# endif 317160814Ssimon 318280304Sjkim if (list_curves) { 319280304Sjkim EC_builtin_curve *curves = NULL; 320280304Sjkim size_t crv_len = 0; 321280304Sjkim size_t n = 0; 322160814Ssimon 323280304Sjkim crv_len = EC_get_builtin_curves(NULL, 0); 324160814Ssimon 325280304Sjkim curves = OPENSSL_malloc((int)(sizeof(EC_builtin_curve) * crv_len)); 326160814Ssimon 327280304Sjkim if (curves == NULL) 328280304Sjkim goto end; 329160814Ssimon 330280304Sjkim if (!EC_get_builtin_curves(curves, crv_len)) { 331280304Sjkim OPENSSL_free(curves); 332280304Sjkim goto end; 333280304Sjkim } 334160814Ssimon 335280304Sjkim for (n = 0; n < crv_len; n++) { 336280304Sjkim const char *comment; 337280304Sjkim const char *sname; 338280304Sjkim comment = curves[n].comment; 339280304Sjkim sname = OBJ_nid2sn(curves[n].nid); 340280304Sjkim if (comment == NULL) 341280304Sjkim comment = "CURVE DESCRIPTION NOT AVAILABLE"; 342280304Sjkim if (sname == NULL) 343280304Sjkim sname = ""; 344160814Ssimon 345280304Sjkim BIO_printf(out, " %-10s: ", sname); 346280304Sjkim BIO_printf(out, "%s\n", comment); 347280304Sjkim } 348160814Ssimon 349280304Sjkim OPENSSL_free(curves); 350280304Sjkim ret = 0; 351280304Sjkim goto end; 352280304Sjkim } 353160814Ssimon 354280304Sjkim if (curve_name != NULL) { 355280304Sjkim int nid; 356160814Ssimon 357280304Sjkim /* 358280304Sjkim * workaround for the SECG curve names secp192r1 and secp256r1 (which 359280304Sjkim * are the same as the curves prime192v1 and prime256v1 defined in 360280304Sjkim * X9.62) 361280304Sjkim */ 362280304Sjkim if (!strcmp(curve_name, "secp192r1")) { 363280304Sjkim BIO_printf(bio_err, "using curve name prime192v1 " 364280304Sjkim "instead of secp192r1\n"); 365280304Sjkim nid = NID_X9_62_prime192v1; 366280304Sjkim } else if (!strcmp(curve_name, "secp256r1")) { 367280304Sjkim BIO_printf(bio_err, "using curve name prime256v1 " 368280304Sjkim "instead of secp256r1\n"); 369280304Sjkim nid = NID_X9_62_prime256v1; 370280304Sjkim } else 371280304Sjkim nid = OBJ_sn2nid(curve_name); 372160814Ssimon 373280304Sjkim if (nid == 0) { 374280304Sjkim BIO_printf(bio_err, "unknown curve name (%s)\n", curve_name); 375280304Sjkim goto end; 376280304Sjkim } 377160814Ssimon 378280304Sjkim group = EC_GROUP_new_by_curve_name(nid); 379280304Sjkim if (group == NULL) { 380280304Sjkim BIO_printf(bio_err, "unable to create curve (%s)\n", curve_name); 381280304Sjkim goto end; 382280304Sjkim } 383280304Sjkim EC_GROUP_set_asn1_flag(group, asn1_flag); 384280304Sjkim EC_GROUP_set_point_conversion_form(group, form); 385280304Sjkim } else if (informat == FORMAT_ASN1) { 386280304Sjkim group = d2i_ECPKParameters_bio(in, NULL); 387280304Sjkim } else if (informat == FORMAT_PEM) { 388280304Sjkim group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL); 389280304Sjkim } else { 390280304Sjkim BIO_printf(bio_err, "bad input format specified\n"); 391280304Sjkim goto end; 392280304Sjkim } 393160814Ssimon 394280304Sjkim if (group == NULL) { 395280304Sjkim BIO_printf(bio_err, "unable to load elliptic curve parameters\n"); 396280304Sjkim ERR_print_errors(bio_err); 397280304Sjkim goto end; 398280304Sjkim } 399160814Ssimon 400280304Sjkim if (new_form) 401280304Sjkim EC_GROUP_set_point_conversion_form(group, form); 402160814Ssimon 403280304Sjkim if (new_asn1_flag) 404280304Sjkim EC_GROUP_set_asn1_flag(group, asn1_flag); 405160814Ssimon 406280304Sjkim if (no_seed) { 407280304Sjkim EC_GROUP_set_seed(group, NULL, 0); 408280304Sjkim } 409160814Ssimon 410280304Sjkim if (text) { 411280304Sjkim if (!ECPKParameters_print(out, group, 0)) 412280304Sjkim goto end; 413280304Sjkim } 414160814Ssimon 415280304Sjkim if (check) { 416280304Sjkim BIO_printf(bio_err, "checking elliptic curve parameters: "); 417280304Sjkim if (!EC_GROUP_check(group, NULL)) { 418280304Sjkim BIO_printf(bio_err, "failed\n"); 419280304Sjkim ERR_print_errors(bio_err); 420291721Sjkim goto end; 421291721Sjkim } 422291721Sjkim BIO_printf(bio_err, "ok\n"); 423160814Ssimon 424280304Sjkim } 425160814Ssimon 426280304Sjkim if (C) { 427280304Sjkim size_t buf_len = 0, tmp_len = 0; 428280304Sjkim const EC_POINT *point; 429280304Sjkim int is_prime, len = 0; 430280304Sjkim const EC_METHOD *meth = EC_GROUP_method_of(group); 431160814Ssimon 432280304Sjkim if ((ec_p = BN_new()) == NULL || (ec_a = BN_new()) == NULL || 433280304Sjkim (ec_b = BN_new()) == NULL || (ec_gen = BN_new()) == NULL || 434280304Sjkim (ec_order = BN_new()) == NULL || 435280304Sjkim (ec_cofactor = BN_new()) == NULL) { 436280304Sjkim perror("OPENSSL_malloc"); 437280304Sjkim goto end; 438280304Sjkim } 439160814Ssimon 440280304Sjkim is_prime = (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field); 441160814Ssimon 442280304Sjkim if (is_prime) { 443280304Sjkim if (!EC_GROUP_get_curve_GFp(group, ec_p, ec_a, ec_b, NULL)) 444280304Sjkim goto end; 445280304Sjkim } else { 446280304Sjkim /* TODO */ 447280304Sjkim goto end; 448280304Sjkim } 449160814Ssimon 450280304Sjkim if ((point = EC_GROUP_get0_generator(group)) == NULL) 451280304Sjkim goto end; 452280304Sjkim if (!EC_POINT_point2bn(group, point, 453280304Sjkim EC_GROUP_get_point_conversion_form(group), 454280304Sjkim ec_gen, NULL)) 455280304Sjkim goto end; 456280304Sjkim if (!EC_GROUP_get_order(group, ec_order, NULL)) 457280304Sjkim goto end; 458280304Sjkim if (!EC_GROUP_get_cofactor(group, ec_cofactor, NULL)) 459280304Sjkim goto end; 460160814Ssimon 461280304Sjkim if (!ec_p || !ec_a || !ec_b || !ec_gen || !ec_order || !ec_cofactor) 462280304Sjkim goto end; 463160814Ssimon 464280304Sjkim len = BN_num_bits(ec_order); 465160814Ssimon 466280304Sjkim if ((tmp_len = (size_t)BN_num_bytes(ec_p)) > buf_len) 467280304Sjkim buf_len = tmp_len; 468280304Sjkim if ((tmp_len = (size_t)BN_num_bytes(ec_a)) > buf_len) 469280304Sjkim buf_len = tmp_len; 470280304Sjkim if ((tmp_len = (size_t)BN_num_bytes(ec_b)) > buf_len) 471280304Sjkim buf_len = tmp_len; 472280304Sjkim if ((tmp_len = (size_t)BN_num_bytes(ec_gen)) > buf_len) 473280304Sjkim buf_len = tmp_len; 474280304Sjkim if ((tmp_len = (size_t)BN_num_bytes(ec_order)) > buf_len) 475280304Sjkim buf_len = tmp_len; 476280304Sjkim if ((tmp_len = (size_t)BN_num_bytes(ec_cofactor)) > buf_len) 477280304Sjkim buf_len = tmp_len; 478160814Ssimon 479280304Sjkim buffer = (unsigned char *)OPENSSL_malloc(buf_len); 480160814Ssimon 481280304Sjkim if (buffer == NULL) { 482280304Sjkim perror("OPENSSL_malloc"); 483280304Sjkim goto end; 484280304Sjkim } 485160814Ssimon 486280304Sjkim ecparam_print_var(out, ec_p, "ec_p", len, buffer); 487280304Sjkim ecparam_print_var(out, ec_a, "ec_a", len, buffer); 488280304Sjkim ecparam_print_var(out, ec_b, "ec_b", len, buffer); 489280304Sjkim ecparam_print_var(out, ec_gen, "ec_gen", len, buffer); 490280304Sjkim ecparam_print_var(out, ec_order, "ec_order", len, buffer); 491280304Sjkim ecparam_print_var(out, ec_cofactor, "ec_cofactor", len, buffer); 492160814Ssimon 493280304Sjkim BIO_printf(out, "\n\n"); 494160814Ssimon 495280304Sjkim BIO_printf(out, "EC_GROUP *get_ec_group_%d(void)\n\t{\n", len); 496280304Sjkim BIO_printf(out, "\tint ok=0;\n"); 497280304Sjkim BIO_printf(out, "\tEC_GROUP *group = NULL;\n"); 498280304Sjkim BIO_printf(out, "\tEC_POINT *point = NULL;\n"); 499280304Sjkim BIO_printf(out, "\tBIGNUM *tmp_1 = NULL, *tmp_2 = NULL, " 500280304Sjkim "*tmp_3 = NULL;\n\n"); 501280304Sjkim BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_p_%d, " 502280304Sjkim "sizeof(ec_p_%d), NULL)) == NULL)\n\t\t" 503280304Sjkim "goto err;\n", len, len); 504280304Sjkim BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_a_%d, " 505280304Sjkim "sizeof(ec_a_%d), NULL)) == NULL)\n\t\t" 506280304Sjkim "goto err;\n", len, len); 507280304Sjkim BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_b_%d, " 508280304Sjkim "sizeof(ec_b_%d), NULL)) == NULL)\n\t\t" 509280304Sjkim "goto err;\n", len, len); 510280304Sjkim if (is_prime) { 511280304Sjkim BIO_printf(out, "\tif ((group = EC_GROUP_new_curve_" 512280304Sjkim "GFp(tmp_1, tmp_2, tmp_3, NULL)) == NULL)" 513280304Sjkim "\n\t\tgoto err;\n\n"); 514280304Sjkim } else { 515280304Sjkim /* TODO */ 516280304Sjkim goto end; 517280304Sjkim } 518280304Sjkim BIO_printf(out, "\t/* build generator */\n"); 519280304Sjkim BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_gen_%d, " 520280304Sjkim "sizeof(ec_gen_%d), tmp_1)) == NULL)" 521280304Sjkim "\n\t\tgoto err;\n", len, len); 522280304Sjkim BIO_printf(out, "\tpoint = EC_POINT_bn2point(group, tmp_1, " 523280304Sjkim "NULL, NULL);\n"); 524280304Sjkim BIO_printf(out, "\tif (point == NULL)\n\t\tgoto err;\n"); 525280304Sjkim BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_order_%d, " 526280304Sjkim "sizeof(ec_order_%d), tmp_2)) == NULL)" 527280304Sjkim "\n\t\tgoto err;\n", len, len); 528280304Sjkim BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_cofactor_%d, " 529280304Sjkim "sizeof(ec_cofactor_%d), tmp_3)) == NULL)" 530280304Sjkim "\n\t\tgoto err;\n", len, len); 531280304Sjkim BIO_printf(out, "\tif (!EC_GROUP_set_generator(group, point," 532280304Sjkim " tmp_2, tmp_3))\n\t\tgoto err;\n"); 533280304Sjkim BIO_printf(out, "\n\tok=1;\n"); 534280304Sjkim BIO_printf(out, "err:\n"); 535280304Sjkim BIO_printf(out, "\tif (tmp_1)\n\t\tBN_free(tmp_1);\n"); 536280304Sjkim BIO_printf(out, "\tif (tmp_2)\n\t\tBN_free(tmp_2);\n"); 537280304Sjkim BIO_printf(out, "\tif (tmp_3)\n\t\tBN_free(tmp_3);\n"); 538280304Sjkim BIO_printf(out, "\tif (point)\n\t\tEC_POINT_free(point);\n"); 539280304Sjkim BIO_printf(out, "\tif (!ok)\n"); 540280304Sjkim BIO_printf(out, "\t\t{\n"); 541280304Sjkim BIO_printf(out, "\t\tEC_GROUP_free(group);\n"); 542280304Sjkim BIO_printf(out, "\t\tgroup = NULL;\n"); 543280304Sjkim BIO_printf(out, "\t\t}\n"); 544280304Sjkim BIO_printf(out, "\treturn(group);\n\t}\n"); 545280304Sjkim } 546160814Ssimon 547280304Sjkim if (!noout) { 548280304Sjkim if (outformat == FORMAT_ASN1) 549280304Sjkim i = i2d_ECPKParameters_bio(out, group); 550280304Sjkim else if (outformat == FORMAT_PEM) 551280304Sjkim i = PEM_write_bio_ECPKParameters(out, group); 552280304Sjkim else { 553280304Sjkim BIO_printf(bio_err, "bad output format specified for" 554280304Sjkim " outfile\n"); 555280304Sjkim goto end; 556280304Sjkim } 557280304Sjkim if (!i) { 558280304Sjkim BIO_printf(bio_err, "unable to write elliptic " 559280304Sjkim "curve parameters\n"); 560280304Sjkim ERR_print_errors(bio_err); 561280304Sjkim goto end; 562280304Sjkim } 563280304Sjkim } 564160814Ssimon 565280304Sjkim if (need_rand) { 566280304Sjkim app_RAND_load_file(NULL, bio_err, (inrand != NULL)); 567280304Sjkim if (inrand != NULL) 568280304Sjkim BIO_printf(bio_err, "%ld semi-random bytes loaded\n", 569280304Sjkim app_RAND_load_files(inrand)); 570280304Sjkim } 571160814Ssimon 572280304Sjkim if (genkey) { 573280304Sjkim EC_KEY *eckey = EC_KEY_new(); 574160814Ssimon 575280304Sjkim if (eckey == NULL) 576280304Sjkim goto end; 577160814Ssimon 578280304Sjkim assert(need_rand); 579280304Sjkim 580280304Sjkim if (EC_KEY_set_group(eckey, group) == 0) 581280304Sjkim goto end; 582280304Sjkim 583280304Sjkim if (!EC_KEY_generate_key(eckey)) { 584280304Sjkim EC_KEY_free(eckey); 585280304Sjkim goto end; 586280304Sjkim } 587280304Sjkim if (outformat == FORMAT_ASN1) 588280304Sjkim i = i2d_ECPrivateKey_bio(out, eckey); 589280304Sjkim else if (outformat == FORMAT_PEM) 590280304Sjkim i = PEM_write_bio_ECPrivateKey(out, eckey, NULL, 591280304Sjkim NULL, 0, NULL, NULL); 592280304Sjkim else { 593280304Sjkim BIO_printf(bio_err, "bad output format specified " 594280304Sjkim "for outfile\n"); 595280304Sjkim EC_KEY_free(eckey); 596280304Sjkim goto end; 597280304Sjkim } 598280304Sjkim EC_KEY_free(eckey); 599280304Sjkim } 600280304Sjkim 601280304Sjkim if (need_rand) 602280304Sjkim app_RAND_write_file(NULL, bio_err); 603280304Sjkim 604280304Sjkim ret = 0; 605280304Sjkim end: 606280304Sjkim if (ec_p) 607280304Sjkim BN_free(ec_p); 608280304Sjkim if (ec_a) 609280304Sjkim BN_free(ec_a); 610280304Sjkim if (ec_b) 611280304Sjkim BN_free(ec_b); 612280304Sjkim if (ec_gen) 613280304Sjkim BN_free(ec_gen); 614280304Sjkim if (ec_order) 615280304Sjkim BN_free(ec_order); 616280304Sjkim if (ec_cofactor) 617280304Sjkim BN_free(ec_cofactor); 618280304Sjkim if (buffer) 619280304Sjkim OPENSSL_free(buffer); 620280304Sjkim if (in != NULL) 621280304Sjkim BIO_free(in); 622280304Sjkim if (out != NULL) 623280304Sjkim BIO_free_all(out); 624280304Sjkim if (group != NULL) 625280304Sjkim EC_GROUP_free(group); 626280304Sjkim apps_shutdown(); 627280304Sjkim OPENSSL_EXIT(ret); 628160814Ssimon} 629160814Ssimon 630160814Ssimonstatic int ecparam_print_var(BIO *out, BIGNUM *in, const char *var, 631280304Sjkim int len, unsigned char *buffer) 632280304Sjkim{ 633280304Sjkim BIO_printf(out, "static unsigned char %s_%d[] = {", var, len); 634280304Sjkim if (BN_is_zero(in)) 635280304Sjkim BIO_printf(out, "\n\t0x00"); 636280304Sjkim else { 637280304Sjkim int i, l; 638160814Ssimon 639280304Sjkim l = BN_bn2bin(in, buffer); 640280304Sjkim for (i = 0; i < l - 1; i++) { 641280304Sjkim if ((i % 12) == 0) 642280304Sjkim BIO_printf(out, "\n\t"); 643280304Sjkim BIO_printf(out, "0x%02X,", buffer[i]); 644280304Sjkim } 645280304Sjkim if ((i % 12) == 0) 646280304Sjkim BIO_printf(out, "\n\t"); 647280304Sjkim BIO_printf(out, "0x%02X", buffer[i]); 648280304Sjkim } 649280304Sjkim BIO_printf(out, "\n\t};\n\n"); 650280304Sjkim return 1; 651280304Sjkim} 652280304Sjkim#else /* !OPENSSL_NO_EC */ 653238405Sjkim 654238405Sjkim# if PEDANTIC 655280304Sjkimstatic void *dummy = &dummy; 656238405Sjkim# endif 657238405Sjkim 658160814Ssimon#endif 659