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

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