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