[BACK]Return to ecparam.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / openssl

Annotation of src/usr.bin/openssl/ecparam.c, Revision 1.2

1.2     ! jsing       1: /* $OpenBSD: ecparam.c,v 1.1 2014/08/26 17:47:24 jsing Exp $ */
1.1       jsing       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:
                    266:        in = BIO_new(BIO_s_file());
                    267:        out = BIO_new(BIO_s_file());
                    268:        if ((in == NULL) || (out == NULL)) {
                    269:                ERR_print_errors(bio_err);
                    270:                goto end;
                    271:        }
                    272:        if (infile == NULL)
                    273:                BIO_set_fp(in, stdin, BIO_NOCLOSE);
                    274:        else {
                    275:                if (BIO_read_filename(in, infile) <= 0) {
                    276:                        perror(infile);
                    277:                        goto end;
                    278:                }
                    279:        }
                    280:        if (outfile == NULL) {
                    281:                BIO_set_fp(out, stdout, BIO_NOCLOSE);
                    282:        } else {
                    283:                if (BIO_write_filename(out, outfile) <= 0) {
                    284:                        perror(outfile);
                    285:                        goto end;
                    286:                }
                    287:        }
                    288:
                    289: #ifndef OPENSSL_NO_ENGINE
                    290:        setup_engine(bio_err, engine, 0);
                    291: #endif
                    292:
                    293:        if (list_curves) {
                    294:                EC_builtin_curve *curves = NULL;
                    295:                size_t crv_len = 0;
                    296:                size_t n = 0;
                    297:
                    298:                crv_len = EC_get_builtin_curves(NULL, 0);
                    299:
                    300:                curves = reallocarray(NULL, crv_len, sizeof(EC_builtin_curve));
                    301:
                    302:                if (curves == NULL)
                    303:                        goto end;
                    304:
                    305:                if (!EC_get_builtin_curves(curves, crv_len)) {
                    306:                        free(curves);
                    307:                        goto end;
                    308:                }
                    309:                for (n = 0; n < crv_len; n++) {
                    310:                        const char *comment;
                    311:                        const char *sname;
                    312:                        comment = curves[n].comment;
                    313:                        sname = OBJ_nid2sn(curves[n].nid);
                    314:                        if (comment == NULL)
                    315:                                comment = "CURVE DESCRIPTION NOT AVAILABLE";
                    316:                        if (sname == NULL)
                    317:                                sname = "";
                    318:
                    319:                        BIO_printf(out, "  %-10s: ", sname);
                    320:                        BIO_printf(out, "%s\n", comment);
                    321:                }
                    322:
                    323:                free(curves);
                    324:                ret = 0;
                    325:                goto end;
                    326:        }
                    327:        if (curve_name != NULL) {
                    328:                int nid;
                    329:
                    330:                /*
                    331:                 * workaround for the SECG curve names secp192r1 and
                    332:                 * secp256r1 (which are the same as the curves prime192v1 and
                    333:                 * prime256v1 defined in X9.62)
                    334:                 */
                    335:                if (!strcmp(curve_name, "secp192r1")) {
                    336:                        BIO_printf(bio_err, "using curve name prime192v1 "
                    337:                            "instead of secp192r1\n");
                    338:                        nid = NID_X9_62_prime192v1;
                    339:                } else if (!strcmp(curve_name, "secp256r1")) {
                    340:                        BIO_printf(bio_err, "using curve name prime256v1 "
                    341:                            "instead of secp256r1\n");
                    342:                        nid = NID_X9_62_prime256v1;
                    343:                } else
                    344:                        nid = OBJ_sn2nid(curve_name);
                    345:
                    346:                if (nid == 0) {
                    347:                        BIO_printf(bio_err, "unknown curve name (%s)\n",
                    348:                            curve_name);
                    349:                        goto end;
                    350:                }
                    351:                group = EC_GROUP_new_by_curve_name(nid);
                    352:                if (group == NULL) {
                    353:                        BIO_printf(bio_err, "unable to create curve (%s)\n",
                    354:                            curve_name);
                    355:                        goto end;
                    356:                }
                    357:                EC_GROUP_set_asn1_flag(group, asn1_flag);
                    358:                EC_GROUP_set_point_conversion_form(group, form);
                    359:        } else if (informat == FORMAT_ASN1) {
                    360:                group = d2i_ECPKParameters_bio(in, NULL);
                    361:        } else if (informat == FORMAT_PEM) {
                    362:                group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL);
                    363:        } else {
                    364:                BIO_printf(bio_err, "bad input format specified\n");
                    365:                goto end;
                    366:        }
                    367:
                    368:        if (group == NULL) {
                    369:                BIO_printf(bio_err,
                    370:                    "unable to load elliptic curve parameters\n");
                    371:                ERR_print_errors(bio_err);
                    372:                goto end;
                    373:        }
                    374:        if (new_form)
                    375:                EC_GROUP_set_point_conversion_form(group, form);
                    376:
                    377:        if (new_asn1_flag)
                    378:                EC_GROUP_set_asn1_flag(group, asn1_flag);
                    379:
                    380:        if (no_seed) {
                    381:                EC_GROUP_set_seed(group, NULL, 0);
                    382:        }
                    383:        if (text) {
                    384:                if (!ECPKParameters_print(out, group, 0))
                    385:                        goto end;
                    386:        }
                    387:        if (check) {
                    388:                if (group == NULL)
                    389:                        BIO_printf(bio_err, "no elliptic curve parameters\n");
                    390:                BIO_printf(bio_err, "checking elliptic curve parameters: ");
                    391:                if (!EC_GROUP_check(group, NULL)) {
                    392:                        BIO_printf(bio_err, "failed\n");
                    393:                        ERR_print_errors(bio_err);
                    394:                } else
                    395:                        BIO_printf(bio_err, "ok\n");
                    396:
                    397:        }
                    398:        if (C) {
                    399:                size_t buf_len = 0, tmp_len = 0;
                    400:                const EC_POINT *point;
                    401:                int is_prime, len = 0;
                    402:                const EC_METHOD *meth = EC_GROUP_method_of(group);
                    403:
                    404:                if ((ec_p = BN_new()) == NULL || (ec_a = BN_new()) == NULL ||
                    405:                    (ec_b = BN_new()) == NULL || (ec_gen = BN_new()) == NULL ||
                    406:                    (ec_order = BN_new()) == NULL ||
                    407:                    (ec_cofactor = BN_new()) == NULL) {
                    408:                        perror("malloc");
                    409:                        goto end;
                    410:                }
                    411:                is_prime = (EC_METHOD_get_field_type(meth) ==
                    412:                    NID_X9_62_prime_field);
                    413:
                    414:                if (is_prime) {
                    415:                        if (!EC_GROUP_get_curve_GFp(group, ec_p, ec_a,
                    416:                                ec_b, NULL))
                    417:                                goto end;
                    418:                } else {
                    419:                        /* TODO */
                    420:                        goto end;
                    421:                }
                    422:
                    423:                if ((point = EC_GROUP_get0_generator(group)) == NULL)
                    424:                        goto end;
                    425:                if (!EC_POINT_point2bn(group, point,
                    426:                        EC_GROUP_get_point_conversion_form(group), ec_gen,
                    427:                        NULL))
                    428:                        goto end;
                    429:                if (!EC_GROUP_get_order(group, ec_order, NULL))
                    430:                        goto end;
                    431:                if (!EC_GROUP_get_cofactor(group, ec_cofactor, NULL))
                    432:                        goto end;
                    433:
                    434:                if (!ec_p || !ec_a || !ec_b || !ec_gen ||
                    435:                    !ec_order || !ec_cofactor)
                    436:                        goto end;
                    437:
                    438:                len = BN_num_bits(ec_order);
                    439:
                    440:                if ((tmp_len = (size_t) BN_num_bytes(ec_p)) > buf_len)
                    441:                        buf_len = tmp_len;
                    442:                if ((tmp_len = (size_t) BN_num_bytes(ec_a)) > buf_len)
                    443:                        buf_len = tmp_len;
                    444:                if ((tmp_len = (size_t) BN_num_bytes(ec_b)) > buf_len)
                    445:                        buf_len = tmp_len;
                    446:                if ((tmp_len = (size_t) BN_num_bytes(ec_gen)) > buf_len)
                    447:                        buf_len = tmp_len;
                    448:                if ((tmp_len = (size_t) BN_num_bytes(ec_order)) > buf_len)
                    449:                        buf_len = tmp_len;
                    450:                if ((tmp_len = (size_t) BN_num_bytes(ec_cofactor)) > buf_len)
                    451:                        buf_len = tmp_len;
                    452:
                    453:                buffer = malloc(buf_len);
                    454:
                    455:                if (buffer == NULL) {
                    456:                        perror("malloc");
                    457:                        goto end;
                    458:                }
                    459:                ecparam_print_var(out, ec_p, "ec_p", len, buffer);
                    460:                ecparam_print_var(out, ec_a, "ec_a", len, buffer);
                    461:                ecparam_print_var(out, ec_b, "ec_b", len, buffer);
                    462:                ecparam_print_var(out, ec_gen, "ec_gen", len, buffer);
                    463:                ecparam_print_var(out, ec_order, "ec_order", len, buffer);
                    464:                ecparam_print_var(out, ec_cofactor, "ec_cofactor", len,
                    465:                    buffer);
                    466:
                    467:                BIO_printf(out, "\n\n");
                    468:
                    469:                BIO_printf(out, "EC_GROUP *get_ec_group_%d(void)\n\t{\n", len);
                    470:                BIO_printf(out, "\tint ok=0;\n");
                    471:                BIO_printf(out, "\tEC_GROUP *group = NULL;\n");
                    472:                BIO_printf(out, "\tEC_POINT *point = NULL;\n");
                    473:                BIO_printf(out, "\tBIGNUM   *tmp_1 = NULL, *tmp_2 = NULL, "
                    474:                    "*tmp_3 = NULL;\n\n");
                    475:                BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_p_%d, "
                    476:                    "sizeof(ec_p_%d), NULL)) == NULL)\n\t\t"
                    477:                    "goto err;\n", len, len);
                    478:                BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_a_%d, "
                    479:                    "sizeof(ec_a_%d), NULL)) == NULL)\n\t\t"
                    480:                    "goto err;\n", len, len);
                    481:                BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_b_%d, "
                    482:                    "sizeof(ec_b_%d), NULL)) == NULL)\n\t\t"
                    483:                    "goto err;\n", len, len);
                    484:                if (is_prime) {
                    485:                        BIO_printf(out, "\tif ((group = EC_GROUP_new_curve_"
                    486:                            "GFp(tmp_1, tmp_2, tmp_3, NULL)) == NULL)"
                    487:                            "\n\t\tgoto err;\n\n");
                    488:                } else {
                    489:                        /* TODO */
                    490:                        goto end;
                    491:                }
                    492:                BIO_printf(out, "\t/* build generator */\n");
                    493:                BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_gen_%d, "
                    494:                    "sizeof(ec_gen_%d), tmp_1)) == NULL)"
                    495:                    "\n\t\tgoto err;\n", len, len);
                    496:                BIO_printf(out, "\tpoint = EC_POINT_bn2point(group, tmp_1, "
                    497:                    "NULL, NULL);\n");
                    498:                BIO_printf(out, "\tif (point == NULL)\n\t\tgoto err;\n");
                    499:                BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_order_%d, "
                    500:                    "sizeof(ec_order_%d), tmp_2)) == NULL)"
                    501:                    "\n\t\tgoto err;\n", len, len);
                    502:                BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_cofactor_%d, "
                    503:                    "sizeof(ec_cofactor_%d), tmp_3)) == NULL)"
                    504:                    "\n\t\tgoto err;\n", len, len);
                    505:                BIO_printf(out, "\tif (!EC_GROUP_set_generator(group, point,"
                    506:                    " tmp_2, tmp_3))\n\t\tgoto err;\n");
                    507:                BIO_printf(out, "\n\tok=1;\n");
                    508:                BIO_printf(out, "err:\n");
                    509:                BIO_printf(out, "\tif (tmp_1)\n\t\tBN_free(tmp_1);\n");
                    510:                BIO_printf(out, "\tif (tmp_2)\n\t\tBN_free(tmp_2);\n");
                    511:                BIO_printf(out, "\tif (tmp_3)\n\t\tBN_free(tmp_3);\n");
                    512:                BIO_printf(out, "\tif (point)\n\t\tEC_POINT_free(point);\n");
                    513:                BIO_printf(out, "\tif (!ok)\n");
                    514:                BIO_printf(out, "\t\t{\n");
                    515:                BIO_printf(out, "\t\tEC_GROUP_free(group);\n");
                    516:                BIO_printf(out, "\t\tgroup = NULL;\n");
                    517:                BIO_printf(out, "\t\t}\n");
                    518:                BIO_printf(out, "\treturn(group);\n\t}\n");
                    519:        }
                    520:        if (!noout) {
                    521:                if (outformat == FORMAT_ASN1)
                    522:                        i = i2d_ECPKParameters_bio(out, group);
                    523:                else if (outformat == FORMAT_PEM)
                    524:                        i = PEM_write_bio_ECPKParameters(out, group);
                    525:                else {
                    526:                        BIO_printf(bio_err, "bad output format specified for"
                    527:                            " outfile\n");
                    528:                        goto end;
                    529:                }
                    530:                if (!i) {
                    531:                        BIO_printf(bio_err, "unable to write elliptic "
                    532:                            "curve parameters\n");
                    533:                        ERR_print_errors(bio_err);
                    534:                        goto end;
                    535:                }
                    536:        }
                    537:        if (genkey) {
                    538:                EC_KEY *eckey = EC_KEY_new();
                    539:
                    540:                if (eckey == NULL)
                    541:                        goto end;
                    542:
                    543:                if (EC_KEY_set_group(eckey, group) == 0) {
                    544:                        EC_KEY_free(eckey);
                    545:                        goto end;
                    546:                }
                    547:
                    548:                if (!EC_KEY_generate_key(eckey)) {
                    549:                        EC_KEY_free(eckey);
                    550:                        goto end;
                    551:                }
                    552:                if (outformat == FORMAT_ASN1)
                    553:                        i = i2d_ECPrivateKey_bio(out, eckey);
                    554:                else if (outformat == FORMAT_PEM)
                    555:                        i = PEM_write_bio_ECPrivateKey(out, eckey, NULL,
                    556:                            NULL, 0, NULL, NULL);
                    557:                else {
                    558:                        BIO_printf(bio_err, "bad output format specified "
                    559:                            "for outfile\n");
                    560:                        EC_KEY_free(eckey);
                    561:                        goto end;
                    562:                }
                    563:                EC_KEY_free(eckey);
                    564:        }
                    565:        ret = 0;
                    566: end:
                    567:        if (ec_p)
                    568:                BN_free(ec_p);
                    569:        if (ec_a)
                    570:                BN_free(ec_a);
                    571:        if (ec_b)
                    572:                BN_free(ec_b);
                    573:        if (ec_gen)
                    574:                BN_free(ec_gen);
                    575:        if (ec_order)
                    576:                BN_free(ec_order);
                    577:        if (ec_cofactor)
                    578:                BN_free(ec_cofactor);
                    579:        free(buffer);
                    580:        BIO_free(in);
                    581:        if (out != NULL)
                    582:                BIO_free_all(out);
                    583:        if (group != NULL)
                    584:                EC_GROUP_free(group);
                    585:
                    586:        return (ret);
                    587: }
                    588:
                    589: static int
                    590: ecparam_print_var(BIO * out, BIGNUM * in, const char *var,
                    591:     int len, unsigned char *buffer)
                    592: {
                    593:        BIO_printf(out, "static unsigned char %s_%d[] = {", var, len);
                    594:        if (BN_is_zero(in))
                    595:                BIO_printf(out, "\n\t0x00");
                    596:        else {
                    597:                int i, l;
                    598:
                    599:                l = BN_bn2bin(in, buffer);
                    600:                for (i = 0; i < l - 1; i++) {
                    601:                        if ((i % 12) == 0)
                    602:                                BIO_printf(out, "\n\t");
                    603:                        BIO_printf(out, "0x%02X,", buffer[i]);
                    604:                }
                    605:                if ((i % 12) == 0)
                    606:                        BIO_printf(out, "\n\t");
                    607:                BIO_printf(out, "0x%02X", buffer[i]);
                    608:        }
                    609:        BIO_printf(out, "\n\t};\n\n");
                    610:        return 1;
                    611: }
                    612: #endif