[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.23

1.23    ! tb          1: /* $OpenBSD: ecparam.c,v 1.22 2022/11/11 17:07:38 joshua 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>
1.19      tb         89:
1.3       jsing      90: static int ecparam_print_var(BIO *, BIGNUM *, const char *, int,
                     91:     unsigned char *);
                     92:
                     93: static struct {
                     94:        int C;
                     95:        int asn1_flag;
                     96:        int check;
                     97:        char *curve_name;
                     98:        point_conversion_form_t form;
                     99:        int genkey;
                    100:        char *infile;
                    101:        int informat;
                    102:        int list_curves;
                    103:        int new_asn1_flag;
                    104:        int new_form;
                    105:        int no_seed;
                    106:        int noout;
                    107:        char *outfile;
                    108:        int outformat;
                    109:        int text;
1.23    ! tb        110: } cfg;
1.3       jsing     111:
                    112: static int
1.7       jsing     113: ecparam_opt_form(char *arg)
1.3       jsing     114: {
                    115:        if (strcmp(arg, "compressed") == 0)
1.23    ! tb        116:                cfg.form = POINT_CONVERSION_COMPRESSED;
1.3       jsing     117:        else if (strcmp(arg, "uncompressed") == 0)
1.23    ! tb        118:                cfg.form = POINT_CONVERSION_UNCOMPRESSED;
1.3       jsing     119:        else if (strcmp(arg, "hybrid") == 0)
1.23    ! tb        120:                cfg.form = POINT_CONVERSION_HYBRID;
1.3       jsing     121:        else
                    122:                return (1);
                    123:
1.23    ! tb        124:        cfg.new_form = 1;
1.3       jsing     125:        return (0);
                    126: }
1.1       jsing     127:
1.3       jsing     128: static int
1.7       jsing     129: ecparam_opt_enctype(char *arg)
1.3       jsing     130: {
                    131:        if (strcmp(arg, "explicit") == 0)
1.23    ! tb        132:                cfg.asn1_flag = 0;
1.3       jsing     133:        else if (strcmp(arg, "named_curve") == 0)
1.23    ! tb        134:                cfg.asn1_flag = OPENSSL_EC_NAMED_CURVE;
1.3       jsing     135:        else
                    136:                return (1);
                    137:
1.23    ! tb        138:        cfg.new_asn1_flag = 1;
1.3       jsing     139:        return (0);
                    140: }
                    141:
1.18      guenther  142: static const struct option ecparam_options[] = {
1.3       jsing     143:        {
                    144:                .name = "C",
                    145:                .desc = "Convert the EC parameters into C code",
                    146:                .type = OPTION_FLAG,
1.23    ! tb        147:                .opt.flag = &cfg.C,
1.3       jsing     148:        },
                    149:        {
                    150:                .name = "check",
                    151:                .desc = "Validate the elliptic curve parameters",
                    152:                .type = OPTION_FLAG,
1.23    ! tb        153:                .opt.flag = &cfg.check,
1.3       jsing     154:        },
                    155:        {
                    156:                .name = "conv_form",
                    157:                .argname = "form",
                    158:                .desc = "Specify point conversion form:\n"
                    159:                    "  compressed, uncompressed (default), hybrid",
                    160:                .type = OPTION_ARG_FUNC,
1.7       jsing     161:                .opt.argfunc = ecparam_opt_form,
1.3       jsing     162:        },
                    163:        {
                    164:                .name = "genkey",
                    165:                .desc = "Generate an EC private key using the specified "
                    166:                    "parameters",
                    167:                .type = OPTION_FLAG,
1.23    ! tb        168:                .opt.flag = &cfg.genkey,
1.3       jsing     169:        },
                    170:        {
                    171:                .name = "in",
                    172:                .argname = "file",
                    173:                .desc = "Input file to read parameters from (default stdin)",
                    174:                .type = OPTION_ARG,
1.23    ! tb        175:                .opt.arg = &cfg.infile,
1.3       jsing     176:        },
                    177:        {
                    178:                .name = "inform",
                    179:                .argname = "format",
                    180:                .desc = "Input format (DER or PEM)",
                    181:                .type = OPTION_ARG_FORMAT,
1.23    ! tb        182:                .opt.value = &cfg.informat,
1.3       jsing     183:        },
                    184:        {
                    185:                .name = "list_curves",
                    186:                .desc = "Print list of all currently implemented EC "
                    187:                    "parameter names",
                    188:                .type = OPTION_FLAG,
1.23    ! tb        189:                .opt.flag = &cfg.list_curves,
1.3       jsing     190:        },
                    191:        {
                    192:                .name = "name",
                    193:                .argname = "curve",
                    194:                .desc = "Use the EC parameters with the specified name",
                    195:                .type = OPTION_ARG,
1.23    ! tb        196:                .opt.arg = &cfg.curve_name,
1.3       jsing     197:        },
                    198:        {
                    199:                .name = "no_seed",
                    200:                .desc = "Do not output seed with explicit parameter encoding",
                    201:                .type = OPTION_FLAG,
1.23    ! tb        202:                .opt.flag = &cfg.no_seed,
1.3       jsing     203:        },
                    204:        {
                    205:                .name = "noout",
                    206:                .desc = "Do not output encoded version of EC parameters",
                    207:                .type = OPTION_FLAG,
1.23    ! tb        208:                .opt.flag = &cfg.noout,
1.3       jsing     209:        },
                    210:        {
                    211:                .name = "out",
                    212:                .argname = "file",
                    213:                .desc = "Output file to write parameters to (default stdout)",
                    214:                .type = OPTION_ARG,
1.23    ! tb        215:                .opt.arg = &cfg.outfile,
1.3       jsing     216:        },
                    217:        {
                    218:                .name = "outform",
                    219:                .argname = "format",
                    220:                .desc = "Output format (DER or PEM)",
                    221:                .type = OPTION_ARG_FORMAT,
1.23    ! tb        222:                .opt.value = &cfg.outformat,
1.3       jsing     223:        },
                    224:        {
                    225:                .name = "param_enc",
                    226:                .argname = "type",
                    227:                .desc = "Specify EC parameter ASN.1 encoding type:\n"
                    228:                    "  explicit, named_curve (default)",
                    229:                .type = OPTION_ARG_FUNC,
1.7       jsing     230:                .opt.argfunc = ecparam_opt_enctype,
1.3       jsing     231:        },
                    232:        {
                    233:                .name = "text",
                    234:                .desc = "Print out the EC parameters in human readable form",
                    235:                .type = OPTION_FLAG,
1.23    ! tb        236:                .opt.flag = &cfg.text,
1.3       jsing     237:        },
1.4       bcook     238:        {NULL},
1.3       jsing     239: };
1.1       jsing     240:
1.3       jsing     241: static void
                    242: ecparam_usage(void)
                    243: {
                    244:        fprintf(stderr, "usage: ecparam [-C] [-check] [-conv_form arg] "
1.13      bcook     245:            " [-genkey]\n"
1.3       jsing     246:            "    [-in file] [-inform DER | PEM] [-list_curves] [-name arg]\n"
                    247:            "    [-no_seed] [-noout] [-out file] [-outform DER | PEM]\n"
                    248:            "    [-param_enc arg] [-text]\n\n");
                    249:        options_usage(ecparam_options);
                    250: }
1.1       jsing     251:
                    252: int
                    253: ecparam_main(int argc, char **argv)
                    254: {
1.3       jsing     255:        BIGNUM *ec_p = NULL, *ec_a = NULL, *ec_b = NULL, *ec_gen = NULL;
                    256:        BIGNUM *ec_order = NULL, *ec_cofactor = NULL;
1.1       jsing     257:        EC_GROUP *group = NULL;
1.3       jsing     258:        unsigned char *buffer = NULL;
1.1       jsing     259:        BIO *in = NULL, *out = NULL;
1.3       jsing     260:        int i, ret = 1;
1.14      doug      261:
1.22      joshua    262:        if (pledge("stdio cpath wpath rpath", NULL) == -1) {
                    263:                perror("pledge");
                    264:                exit(1);
1.14      doug      265:        }
1.1       jsing     266:
1.23    ! tb        267:        memset(&cfg, 0, sizeof(cfg));
        !           268:        cfg.asn1_flag = OPENSSL_EC_NAMED_CURVE;
        !           269:        cfg.form = POINT_CONVERSION_UNCOMPRESSED;
        !           270:        cfg.informat = FORMAT_PEM;
        !           271:        cfg.outformat = FORMAT_PEM;
1.1       jsing     272:
1.8       jsing     273:        if (options_parse(argc, argv, ecparam_options, NULL, NULL) != 0) {
1.3       jsing     274:                ecparam_usage();
1.1       jsing     275:                goto end;
                    276:        }
                    277:
                    278:        in = BIO_new(BIO_s_file());
                    279:        out = BIO_new(BIO_s_file());
                    280:        if ((in == NULL) || (out == NULL)) {
                    281:                ERR_print_errors(bio_err);
                    282:                goto end;
                    283:        }
1.23    ! tb        284:        if (cfg.infile == NULL)
1.1       jsing     285:                BIO_set_fp(in, stdin, BIO_NOCLOSE);
                    286:        else {
1.23    ! tb        287:                if (BIO_read_filename(in, cfg.infile) <= 0) {
        !           288:                        perror(cfg.infile);
1.1       jsing     289:                        goto end;
                    290:                }
                    291:        }
1.23    ! tb        292:        if (cfg.outfile == NULL) {
1.1       jsing     293:                BIO_set_fp(out, stdout, BIO_NOCLOSE);
                    294:        } else {
1.23    ! tb        295:                if (BIO_write_filename(out, cfg.outfile) <= 0) {
        !           296:                        perror(cfg.outfile);
1.1       jsing     297:                        goto end;
                    298:                }
                    299:        }
                    300:
1.23    ! tb        301:        if (cfg.list_curves) {
1.1       jsing     302:                EC_builtin_curve *curves = NULL;
                    303:                size_t crv_len = 0;
                    304:                size_t n = 0;
                    305:
                    306:                crv_len = EC_get_builtin_curves(NULL, 0);
                    307:
                    308:                curves = reallocarray(NULL, crv_len, sizeof(EC_builtin_curve));
                    309:                if (curves == NULL)
                    310:                        goto end;
                    311:
                    312:                if (!EC_get_builtin_curves(curves, crv_len)) {
                    313:                        free(curves);
                    314:                        goto end;
                    315:                }
                    316:                for (n = 0; n < crv_len; n++) {
                    317:                        const char *comment;
                    318:                        const char *sname;
                    319:                        comment = curves[n].comment;
                    320:                        sname = OBJ_nid2sn(curves[n].nid);
                    321:                        if (comment == NULL)
                    322:                                comment = "CURVE DESCRIPTION NOT AVAILABLE";
                    323:                        if (sname == NULL)
                    324:                                sname = "";
                    325:
                    326:                        BIO_printf(out, "  %-10s: ", sname);
                    327:                        BIO_printf(out, "%s\n", comment);
                    328:                }
                    329:
                    330:                free(curves);
                    331:                ret = 0;
                    332:                goto end;
                    333:        }
1.23    ! tb        334:        if (cfg.curve_name != NULL) {
1.1       jsing     335:                int nid;
                    336:
                    337:                /*
                    338:                 * workaround for the SECG curve names secp192r1 and
                    339:                 * secp256r1 (which are the same as the curves prime192v1 and
                    340:                 * prime256v1 defined in X9.62)
                    341:                 */
1.23    ! tb        342:                if (!strcmp(cfg.curve_name, "secp192r1")) {
1.1       jsing     343:                        BIO_printf(bio_err, "using curve name prime192v1 "
                    344:                            "instead of secp192r1\n");
                    345:                        nid = NID_X9_62_prime192v1;
1.23    ! tb        346:                } else if (!strcmp(cfg.curve_name, "secp256r1")) {
1.1       jsing     347:                        BIO_printf(bio_err, "using curve name prime256v1 "
                    348:                            "instead of secp256r1\n");
                    349:                        nid = NID_X9_62_prime256v1;
                    350:                } else
1.23    ! tb        351:                        nid = OBJ_sn2nid(cfg.curve_name);
1.9       jsing     352:
                    353:                if (nid == 0)
1.23    ! tb        354:                        nid = EC_curve_nist2nid(cfg.curve_name);
1.1       jsing     355:
                    356:                if (nid == 0) {
                    357:                        BIO_printf(bio_err, "unknown curve name (%s)\n",
1.23    ! tb        358:                            cfg.curve_name);
1.1       jsing     359:                        goto end;
                    360:                }
                    361:                group = EC_GROUP_new_by_curve_name(nid);
                    362:                if (group == NULL) {
                    363:                        BIO_printf(bio_err, "unable to create curve (%s)\n",
1.23    ! tb        364:                            cfg.curve_name);
1.1       jsing     365:                        goto end;
                    366:                }
1.23    ! tb        367:                EC_GROUP_set_asn1_flag(group, cfg.asn1_flag);
        !           368:                EC_GROUP_set_point_conversion_form(group, cfg.form);
        !           369:        } else if (cfg.informat == FORMAT_ASN1) {
1.1       jsing     370:                group = d2i_ECPKParameters_bio(in, NULL);
1.23    ! tb        371:        } else if (cfg.informat == FORMAT_PEM) {
1.1       jsing     372:                group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL);
                    373:        } else {
                    374:                BIO_printf(bio_err, "bad input format specified\n");
                    375:                goto end;
                    376:        }
                    377:
                    378:        if (group == NULL) {
                    379:                BIO_printf(bio_err,
                    380:                    "unable to load elliptic curve parameters\n");
                    381:                ERR_print_errors(bio_err);
                    382:                goto end;
                    383:        }
1.23    ! tb        384:        if (cfg.new_form)
        !           385:                EC_GROUP_set_point_conversion_form(group, cfg.form);
1.1       jsing     386:
1.23    ! tb        387:        if (cfg.new_asn1_flag)
        !           388:                EC_GROUP_set_asn1_flag(group, cfg.asn1_flag);
1.1       jsing     389:
1.23    ! tb        390:        if (cfg.no_seed)
1.1       jsing     391:                EC_GROUP_set_seed(group, NULL, 0);
1.3       jsing     392:
1.23    ! tb        393:        if (cfg.text) {
1.1       jsing     394:                if (!ECPKParameters_print(out, group, 0))
                    395:                        goto end;
                    396:        }
1.23    ! tb        397:        if (cfg.check) {
1.1       jsing     398:                BIO_printf(bio_err, "checking elliptic curve parameters: ");
                    399:                if (!EC_GROUP_check(group, NULL)) {
                    400:                        BIO_printf(bio_err, "failed\n");
                    401:                        ERR_print_errors(bio_err);
                    402:                } else
                    403:                        BIO_printf(bio_err, "ok\n");
                    404:
                    405:        }
1.23    ! tb        406:        if (cfg.C) {
1.1       jsing     407:                size_t buf_len = 0, tmp_len = 0;
                    408:                const EC_POINT *point;
                    409:                int is_prime, len = 0;
                    410:                const EC_METHOD *meth = EC_GROUP_method_of(group);
                    411:
                    412:                if ((ec_p = BN_new()) == NULL || (ec_a = BN_new()) == NULL ||
                    413:                    (ec_b = BN_new()) == NULL || (ec_gen = BN_new()) == NULL ||
                    414:                    (ec_order = BN_new()) == NULL ||
                    415:                    (ec_cofactor = BN_new()) == NULL) {
                    416:                        perror("malloc");
                    417:                        goto end;
                    418:                }
                    419:                is_prime = (EC_METHOD_get_field_type(meth) ==
                    420:                    NID_X9_62_prime_field);
                    421:
1.21      tb        422:                if (!EC_GROUP_get_curve(group, ec_p, ec_a, ec_b, NULL))
                    423:                        goto end;
1.1       jsing     424:
                    425:                if ((point = EC_GROUP_get0_generator(group)) == NULL)
                    426:                        goto end;
                    427:                if (!EC_POINT_point2bn(group, point,
                    428:                        EC_GROUP_get_point_conversion_form(group), ec_gen,
                    429:                        NULL))
                    430:                        goto end;
                    431:                if (!EC_GROUP_get_order(group, ec_order, NULL))
                    432:                        goto end;
                    433:                if (!EC_GROUP_get_cofactor(group, ec_cofactor, NULL))
                    434:                        goto end;
                    435:
                    436:                len = BN_num_bits(ec_order);
                    437:
                    438:                if ((tmp_len = (size_t) BN_num_bytes(ec_p)) > buf_len)
                    439:                        buf_len = tmp_len;
                    440:                if ((tmp_len = (size_t) BN_num_bytes(ec_a)) > buf_len)
                    441:                        buf_len = tmp_len;
                    442:                if ((tmp_len = (size_t) BN_num_bytes(ec_b)) > buf_len)
                    443:                        buf_len = tmp_len;
                    444:                if ((tmp_len = (size_t) BN_num_bytes(ec_gen)) > buf_len)
                    445:                        buf_len = tmp_len;
                    446:                if ((tmp_len = (size_t) BN_num_bytes(ec_order)) > buf_len)
                    447:                        buf_len = tmp_len;
                    448:                if ((tmp_len = (size_t) BN_num_bytes(ec_cofactor)) > buf_len)
                    449:                        buf_len = tmp_len;
                    450:
                    451:                buffer = malloc(buf_len);
                    452:
                    453:                if (buffer == NULL) {
                    454:                        perror("malloc");
                    455:                        goto end;
                    456:                }
                    457:                ecparam_print_var(out, ec_p, "ec_p", len, buffer);
                    458:                ecparam_print_var(out, ec_a, "ec_a", len, buffer);
                    459:                ecparam_print_var(out, ec_b, "ec_b", len, buffer);
                    460:                ecparam_print_var(out, ec_gen, "ec_gen", len, buffer);
                    461:                ecparam_print_var(out, ec_order, "ec_order", len, buffer);
                    462:                ecparam_print_var(out, ec_cofactor, "ec_cofactor", len,
                    463:                    buffer);
                    464:
                    465:                BIO_printf(out, "\n\n");
                    466:
                    467:                BIO_printf(out, "EC_GROUP *get_ec_group_%d(void)\n\t{\n", len);
                    468:                BIO_printf(out, "\tint ok=0;\n");
                    469:                BIO_printf(out, "\tEC_GROUP *group = NULL;\n");
                    470:                BIO_printf(out, "\tEC_POINT *point = NULL;\n");
                    471:                BIO_printf(out, "\tBIGNUM   *tmp_1 = NULL, *tmp_2 = NULL, "
                    472:                    "*tmp_3 = NULL;\n\n");
                    473:                BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_p_%d, "
                    474:                    "sizeof(ec_p_%d), NULL)) == NULL)\n\t\t"
                    475:                    "goto err;\n", len, len);
                    476:                BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_a_%d, "
                    477:                    "sizeof(ec_a_%d), NULL)) == NULL)\n\t\t"
                    478:                    "goto err;\n", len, len);
                    479:                BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_b_%d, "
                    480:                    "sizeof(ec_b_%d), NULL)) == NULL)\n\t\t"
                    481:                    "goto err;\n", len, len);
                    482:                if (is_prime) {
                    483:                        BIO_printf(out, "\tif ((group = EC_GROUP_new_curve_"
                    484:                            "GFp(tmp_1, tmp_2, tmp_3, NULL)) == NULL)"
                    485:                            "\n\t\tgoto err;\n\n");
                    486:                } else {
1.5       jsing     487:                        BIO_printf(out, "\tif ((group = EC_GROUP_new_curve_"
                    488:                            "GF2m(tmp_1, tmp_2, tmp_3, NULL)) == NULL)"
                    489:                            "\n\t\tgoto err;\n\n");
1.1       jsing     490:                }
                    491:                BIO_printf(out, "\t/* build generator */\n");
                    492:                BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_gen_%d, "
                    493:                    "sizeof(ec_gen_%d), tmp_1)) == NULL)"
                    494:                    "\n\t\tgoto err;\n", len, len);
                    495:                BIO_printf(out, "\tpoint = EC_POINT_bn2point(group, tmp_1, "
                    496:                    "NULL, NULL);\n");
                    497:                BIO_printf(out, "\tif (point == NULL)\n\t\tgoto err;\n");
                    498:                BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_order_%d, "
                    499:                    "sizeof(ec_order_%d), tmp_2)) == NULL)"
                    500:                    "\n\t\tgoto err;\n", len, len);
                    501:                BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_cofactor_%d, "
                    502:                    "sizeof(ec_cofactor_%d), tmp_3)) == NULL)"
                    503:                    "\n\t\tgoto err;\n", len, len);
                    504:                BIO_printf(out, "\tif (!EC_GROUP_set_generator(group, point,"
                    505:                    " tmp_2, tmp_3))\n\t\tgoto err;\n");
                    506:                BIO_printf(out, "\n\tok=1;\n");
                    507:                BIO_printf(out, "err:\n");
                    508:                BIO_printf(out, "\tif (tmp_1)\n\t\tBN_free(tmp_1);\n");
                    509:                BIO_printf(out, "\tif (tmp_2)\n\t\tBN_free(tmp_2);\n");
                    510:                BIO_printf(out, "\tif (tmp_3)\n\t\tBN_free(tmp_3);\n");
                    511:                BIO_printf(out, "\tif (point)\n\t\tEC_POINT_free(point);\n");
                    512:                BIO_printf(out, "\tif (!ok)\n");
                    513:                BIO_printf(out, "\t\t{\n");
                    514:                BIO_printf(out, "\t\tEC_GROUP_free(group);\n");
                    515:                BIO_printf(out, "\t\tgroup = NULL;\n");
                    516:                BIO_printf(out, "\t\t}\n");
                    517:                BIO_printf(out, "\treturn(group);\n\t}\n");
                    518:        }
1.23    ! tb        519:        if (!cfg.noout) {
        !           520:                if (cfg.outformat == FORMAT_ASN1)
1.1       jsing     521:                        i = i2d_ECPKParameters_bio(out, group);
1.23    ! tb        522:                else if (cfg.outformat == FORMAT_PEM)
1.1       jsing     523:                        i = PEM_write_bio_ECPKParameters(out, group);
                    524:                else {
                    525:                        BIO_printf(bio_err, "bad output format specified for"
                    526:                            " outfile\n");
                    527:                        goto end;
                    528:                }
                    529:                if (!i) {
                    530:                        BIO_printf(bio_err, "unable to write elliptic "
                    531:                            "curve parameters\n");
                    532:                        ERR_print_errors(bio_err);
                    533:                        goto end;
                    534:                }
                    535:        }
1.23    ! tb        536:        if (cfg.genkey) {
1.1       jsing     537:                EC_KEY *eckey = EC_KEY_new();
                    538:
                    539:                if (eckey == NULL)
                    540:                        goto end;
                    541:
                    542:                if (EC_KEY_set_group(eckey, group) == 0) {
                    543:                        EC_KEY_free(eckey);
                    544:                        goto end;
                    545:                }
                    546:
                    547:                if (!EC_KEY_generate_key(eckey)) {
                    548:                        EC_KEY_free(eckey);
                    549:                        goto end;
                    550:                }
1.23    ! tb        551:                if (cfg.outformat == FORMAT_ASN1)
1.1       jsing     552:                        i = i2d_ECPrivateKey_bio(out, eckey);
1.23    ! tb        553:                else if (cfg.outformat == FORMAT_PEM)
1.1       jsing     554:                        i = PEM_write_bio_ECPrivateKey(out, eckey, NULL,
                    555:                            NULL, 0, NULL, NULL);
                    556:                else {
                    557:                        BIO_printf(bio_err, "bad output format specified "
                    558:                            "for outfile\n");
                    559:                        EC_KEY_free(eckey);
                    560:                        goto end;
                    561:                }
                    562:                EC_KEY_free(eckey);
                    563:        }
                    564:        ret = 0;
1.6       jsing     565:
1.17      jsing     566:  end:
1.6       jsing     567:        BN_free(ec_p);
                    568:        BN_free(ec_a);
                    569:        BN_free(ec_b);
                    570:        BN_free(ec_gen);
                    571:        BN_free(ec_order);
                    572:        BN_free(ec_cofactor);
                    573:
1.1       jsing     574:        free(buffer);
1.6       jsing     575:
1.1       jsing     576:        BIO_free(in);
1.6       jsing     577:        BIO_free_all(out);
                    578:        EC_GROUP_free(group);
1.1       jsing     579:
                    580:        return (ret);
                    581: }
                    582:
                    583: static int
                    584: ecparam_print_var(BIO * out, BIGNUM * in, const char *var,
                    585:     int len, unsigned char *buffer)
                    586: {
                    587:        BIO_printf(out, "static unsigned char %s_%d[] = {", var, len);
                    588:        if (BN_is_zero(in))
                    589:                BIO_printf(out, "\n\t0x00");
                    590:        else {
                    591:                int i, l;
                    592:
                    593:                l = BN_bn2bin(in, buffer);
                    594:                for (i = 0; i < l - 1; i++) {
                    595:                        if ((i % 12) == 0)
                    596:                                BIO_printf(out, "\n\t");
                    597:                        BIO_printf(out, "0x%02X,", buffer[i]);
                    598:                }
                    599:                if ((i % 12) == 0)
                    600:                        BIO_printf(out, "\n\t");
                    601:                BIO_printf(out, "0x%02X", buffer[i]);
                    602:        }
                    603:        BIO_printf(out, "\n\t};\n\n");
                    604:        return 1;
                    605: }
                    606: #endif