Bodo Möller | 14a7cfb | 2002-08-07 10:49:54 +0000 | [diff] [blame] | 1 | /* |
Bodo Möller | 7eb18f1 | 2002-08-15 09:21:31 +0000 | [diff] [blame] | 2 | * Written by Nils Larsch for the OpenSSL project. |
Bodo Möller | 14a7cfb | 2002-08-07 10:49:54 +0000 | [diff] [blame] | 3 | */ |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 4 | /* ==================================================================== |
Nils Larsch | 9dd8405 | 2005-05-16 10:11:04 +0000 | [diff] [blame] | 5 | * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * |
| 11 | * 1. Redistributions of source code must retain the above copyright |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 12 | * notice, this list of conditions and the following disclaimer. |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 13 | * |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in |
| 16 | * the documentation and/or other materials provided with the |
| 17 | * distribution. |
| 18 | * |
| 19 | * 3. All advertising materials mentioning features or use of this |
| 20 | * software must display the following acknowledgment: |
| 21 | * "This product includes software developed by the OpenSSL Project |
| 22 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 23 | * |
| 24 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 25 | * endorse or promote products derived from this software without |
| 26 | * prior written permission. For written permission, please contact |
| 27 | * openssl-core@openssl.org. |
| 28 | * |
| 29 | * 5. Products derived from this software may not be called "OpenSSL" |
| 30 | * nor may "OpenSSL" appear in their names without prior written |
| 31 | * permission of the OpenSSL Project. |
| 32 | * |
| 33 | * 6. Redistributions of any form whatsoever must retain the following |
| 34 | * acknowledgment: |
| 35 | * "This product includes software developed by the OpenSSL Project |
| 36 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 37 | * |
| 38 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 39 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 40 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 41 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 42 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 43 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 44 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 45 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 46 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 47 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 48 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 49 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 50 | * ==================================================================== |
| 51 | * |
| 52 | * This product includes cryptographic software written by Eric Young |
| 53 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 54 | * Hudson (tjh@cryptsoft.com). |
| 55 | * |
| 56 | */ |
Bodo Möller | 714df32 | 2002-08-02 13:06:17 +0000 | [diff] [blame] | 57 | /* ==================================================================== |
| 58 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 59 | * |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 60 | * Portions of the attached software ("Contribution") are developed by |
Bodo Möller | 714df32 | 2002-08-02 13:06:17 +0000 | [diff] [blame] | 61 | * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. |
| 62 | * |
| 63 | * The Contribution is licensed pursuant to the OpenSSL open source |
| 64 | * license provided above. |
| 65 | * |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 66 | * The elliptic curve binary polynomial software is originally written by |
Bodo Möller | 714df32 | 2002-08-02 13:06:17 +0000 | [diff] [blame] | 67 | * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories. |
| 68 | * |
| 69 | */ |
Nils Larsch | 6e04afb | 2005-05-31 17:36:06 +0000 | [diff] [blame] | 70 | |
| 71 | #include <openssl/opensslconf.h> |
Bodo Möller | 14a7cfb | 2002-08-07 10:49:54 +0000 | [diff] [blame] | 72 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 73 | # include <stdio.h> |
| 74 | # include <stdlib.h> |
| 75 | # include <time.h> |
| 76 | # include <string.h> |
| 77 | # include "apps.h" |
| 78 | # include <openssl/bio.h> |
| 79 | # include <openssl/err.h> |
| 80 | # include <openssl/bn.h> |
| 81 | # include <openssl/ec.h> |
| 82 | # include <openssl/x509.h> |
| 83 | # include <openssl/pem.h> |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 84 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 85 | typedef enum OPTION_choice { |
| 86 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, |
| 87 | OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT, OPT_C, |
| 88 | OPT_CHECK, OPT_LIST_CURVES, OPT_NO_SEED, OPT_NOOUT, OPT_NAME, |
| 89 | OPT_CONV_FORM, OPT_PARAM_ENC, OPT_GENKEY, OPT_RAND, OPT_ENGINE |
| 90 | } OPTION_CHOICE; |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 91 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 92 | OPTIONS ecparam_options[] = { |
| 93 | {"help", OPT_HELP, '-', "Display this summary"}, |
| 94 | {"inform", OPT_INFORM, 'F', "Input format - default PEM (DER or PEM)"}, |
| 95 | {"outform", OPT_OUTFORM, 'F', "Output format - default PEM"}, |
| 96 | {"in", OPT_IN, '<', "Input file - default stdin"}, |
| 97 | {"out", OPT_OUT, '>', "Output file - default stdout"}, |
| 98 | {"text", OPT_TEXT, '-', "Print the ec parameters in text form"}, |
| 99 | {"C", OPT_C, '-', "Print a 'C' function creating the parameters"}, |
| 100 | {"check", OPT_CHECK, '-', "Validate the ec parameters"}, |
| 101 | {"list_curves", OPT_LIST_CURVES, '-', |
| 102 | "Prints a list of all curve 'short names'"}, |
| 103 | {"no_seed", OPT_NO_SEED, '-', |
| 104 | "If 'explicit' parameters are chosen do not use the seed"}, |
| 105 | {"noout", OPT_NOOUT, '-', "Do not print the ec parameter"}, |
| 106 | {"name", OPT_NAME, 's', |
| 107 | "Use the ec parameters with specified 'short name'"}, |
| 108 | {"conv_form", OPT_CONV_FORM, 's', "Specifies the point conversion form "}, |
| 109 | {"param_enc", OPT_PARAM_ENC, 's', |
| 110 | "Specifies the way the ec parameters are encoded"}, |
| 111 | {"genkey", OPT_GENKEY, '-', "Generate ec key"}, |
| 112 | {"rand", OPT_RAND, 's', "Files to use for random number input"}, |
| 113 | # ifndef OPENSSL_NO_ENGINE |
| 114 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, |
| 115 | # endif |
| 116 | {NULL} |
| 117 | }; |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 118 | |
Ben Laurie | df2ee0e | 2015-09-05 13:32:58 +0100 | [diff] [blame] | 119 | static OPT_PAIR forms[] = { |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 120 | {"compressed", POINT_CONVERSION_COMPRESSED}, |
| 121 | {"uncompressed", POINT_CONVERSION_UNCOMPRESSED}, |
| 122 | {"hybrid", POINT_CONVERSION_HYBRID}, |
| 123 | {NULL} |
| 124 | }; |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 125 | |
Ben Laurie | df2ee0e | 2015-09-05 13:32:58 +0100 | [diff] [blame] | 126 | static OPT_PAIR encodings[] = { |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 127 | {"named_curve", OPENSSL_EC_NAMED_CURVE}, |
| 128 | {"explicit", 0}, |
| 129 | {NULL} |
| 130 | }; |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 131 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 132 | int ecparam_main(int argc, char **argv) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 133 | { |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 134 | BIGNUM *ec_gen = NULL, *ec_order = NULL, *ec_cofactor = NULL; |
| 135 | BIGNUM *ec_p = NULL, *ec_a = NULL, *ec_b = NULL; |
| 136 | BIO *in = NULL, *out = NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 137 | EC_GROUP *group = NULL; |
| 138 | point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 139 | char *curve_name = NULL, *inrand = NULL; |
Rich Salz | 333b070 | 2015-04-25 15:41:29 -0400 | [diff] [blame] | 140 | char *infile = NULL, *outfile = NULL, *prog; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 141 | unsigned char *buffer = NULL; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 142 | OPTION_CHOICE o; |
| 143 | int asn1_flag = OPENSSL_EC_NAMED_CURVE, new_asn1_flag = 0; |
Rich Salz | 3b061a0 | 2015-05-02 10:01:33 -0400 | [diff] [blame] | 144 | int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0; |
| 145 | int ret = 1, private = 0; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 146 | int list_curves = 0, no_seed = 0, check = 0, new_form = 0; |
| 147 | int text = 0, i, need_rand = 0, genkey = 0; |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 148 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 149 | prog = opt_init(argc, argv, ecparam_options); |
| 150 | while ((o = opt_next()) != OPT_EOF) { |
| 151 | switch (o) { |
| 152 | case OPT_EOF: |
| 153 | case OPT_ERR: |
| 154 | opthelp: |
| 155 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); |
| 156 | goto end; |
| 157 | case OPT_HELP: |
| 158 | opt_help(ecparam_options); |
| 159 | ret = 0; |
| 160 | goto end; |
| 161 | case OPT_INFORM: |
| 162 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat)) |
| 163 | goto opthelp; |
| 164 | break; |
| 165 | case OPT_IN: |
| 166 | infile = opt_arg(); |
| 167 | break; |
| 168 | case OPT_OUTFORM: |
| 169 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat)) |
| 170 | goto opthelp; |
| 171 | break; |
| 172 | case OPT_OUT: |
| 173 | outfile = opt_arg(); |
| 174 | break; |
| 175 | case OPT_TEXT: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 176 | text = 1; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 177 | break; |
| 178 | case OPT_C: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 179 | C = 1; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 180 | break; |
| 181 | case OPT_CHECK: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 182 | check = 1; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 183 | break; |
| 184 | case OPT_LIST_CURVES: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 185 | list_curves = 1; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 186 | break; |
| 187 | case OPT_NO_SEED: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 188 | no_seed = 1; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 189 | break; |
| 190 | case OPT_NOOUT: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 191 | noout = 1; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 192 | break; |
| 193 | case OPT_NAME: |
| 194 | curve_name = opt_arg(); |
| 195 | break; |
| 196 | case OPT_CONV_FORM: |
| 197 | if (!opt_pair(opt_arg(), forms, &new_form)) |
| 198 | goto opthelp; |
| 199 | form = new_form; |
| 200 | new_form = 1; |
| 201 | break; |
| 202 | case OPT_PARAM_ENC: |
| 203 | if (!opt_pair(opt_arg(), encodings, &asn1_flag)) |
| 204 | goto opthelp; |
| 205 | new_asn1_flag = 1; |
| 206 | break; |
| 207 | case OPT_GENKEY: |
| 208 | genkey = need_rand = 1; |
| 209 | break; |
| 210 | case OPT_RAND: |
| 211 | inrand = opt_arg(); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 212 | need_rand = 1; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 213 | break; |
| 214 | case OPT_ENGINE: |
Rich Salz | 333b070 | 2015-04-25 15:41:29 -0400 | [diff] [blame] | 215 | (void)setup_engine(opt_arg(), 0); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 216 | break; |
| 217 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 218 | } |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 219 | argc = opt_num_rest(); |
| 220 | argv = opt_rest(); |
Rich Salz | 3b061a0 | 2015-05-02 10:01:33 -0400 | [diff] [blame] | 221 | private = genkey ? 1 : 0; |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 222 | |
Richard Levitte | bdd58d9 | 2015-09-04 12:49:06 +0200 | [diff] [blame] | 223 | in = bio_open_default(infile, 'r', informat); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 224 | if (in == NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 225 | goto end; |
Richard Levitte | bdd58d9 | 2015-09-04 12:49:06 +0200 | [diff] [blame] | 226 | out = bio_open_owner(outfile, outformat, private); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 227 | if (out == NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 228 | goto end; |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 229 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 230 | if (list_curves) { |
| 231 | EC_builtin_curve *curves = NULL; |
Rich Salz | 68dc682 | 2015-04-30 17:48:31 -0400 | [diff] [blame] | 232 | size_t crv_len = EC_get_builtin_curves(NULL, 0); |
| 233 | size_t n; |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 234 | |
Rich Salz | b4faea5 | 2015-05-01 23:10:31 -0400 | [diff] [blame] | 235 | curves = app_malloc((int)sizeof(*curves) * crv_len, "list curves"); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 236 | if (!EC_get_builtin_curves(curves, crv_len)) { |
| 237 | OPENSSL_free(curves); |
| 238 | goto end; |
| 239 | } |
Bodo Möller | 65b1d31 | 2002-09-02 07:08:33 +0000 | [diff] [blame] | 240 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 241 | for (n = 0; n < crv_len; n++) { |
| 242 | const char *comment; |
| 243 | const char *sname; |
| 244 | comment = curves[n].comment; |
| 245 | sname = OBJ_nid2sn(curves[n].nid); |
| 246 | if (comment == NULL) |
| 247 | comment = "CURVE DESCRIPTION NOT AVAILABLE"; |
| 248 | if (sname == NULL) |
| 249 | sname = ""; |
Bodo Möller | 7eb18f1 | 2002-08-15 09:21:31 +0000 | [diff] [blame] | 250 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 251 | BIO_printf(out, " %-10s: ", sname); |
| 252 | BIO_printf(out, "%s\n", comment); |
| 253 | } |
Bodo Möller | 7eb18f1 | 2002-08-15 09:21:31 +0000 | [diff] [blame] | 254 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 255 | OPENSSL_free(curves); |
| 256 | ret = 0; |
| 257 | goto end; |
| 258 | } |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 259 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 260 | if (curve_name != NULL) { |
| 261 | int nid; |
Bodo Möller | d745af4 | 2003-01-16 16:05:23 +0000 | [diff] [blame] | 262 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 263 | /* |
| 264 | * workaround for the SECG curve names secp192r1 and secp256r1 (which |
| 265 | * are the same as the curves prime192v1 and prime256v1 defined in |
| 266 | * X9.62) |
| 267 | */ |
Rich Salz | 86885c2 | 2015-05-06 14:56:14 -0400 | [diff] [blame] | 268 | if (strcmp(curve_name, "secp192r1") == 0) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 269 | BIO_printf(bio_err, "using curve name prime192v1 " |
| 270 | "instead of secp192r1\n"); |
| 271 | nid = NID_X9_62_prime192v1; |
Rich Salz | 86885c2 | 2015-05-06 14:56:14 -0400 | [diff] [blame] | 272 | } else if (strcmp(curve_name, "secp256r1") == 0) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 273 | BIO_printf(bio_err, "using curve name prime256v1 " |
| 274 | "instead of secp256r1\n"); |
| 275 | nid = NID_X9_62_prime256v1; |
| 276 | } else |
| 277 | nid = OBJ_sn2nid(curve_name); |
Dr. Stephen Henson | 64095ce | 2012-02-21 14:41:13 +0000 | [diff] [blame] | 278 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 279 | if (nid == 0) |
| 280 | nid = EC_curve_nist2nid(curve_name); |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 281 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 282 | if (nid == 0) { |
| 283 | BIO_printf(bio_err, "unknown curve name (%s)\n", curve_name); |
| 284 | goto end; |
| 285 | } |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 286 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 287 | group = EC_GROUP_new_by_curve_name(nid); |
| 288 | if (group == NULL) { |
| 289 | BIO_printf(bio_err, "unable to create curve (%s)\n", curve_name); |
| 290 | goto end; |
| 291 | } |
| 292 | EC_GROUP_set_asn1_flag(group, asn1_flag); |
| 293 | EC_GROUP_set_point_conversion_form(group, form); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 294 | } else if (informat == FORMAT_ASN1) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 295 | group = d2i_ECPKParameters_bio(in, NULL); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 296 | else |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 297 | group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 298 | if (group == NULL) { |
| 299 | BIO_printf(bio_err, "unable to load elliptic curve parameters\n"); |
| 300 | ERR_print_errors(bio_err); |
| 301 | goto end; |
| 302 | } |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 303 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 304 | if (new_form) |
| 305 | EC_GROUP_set_point_conversion_form(group, form); |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 306 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 307 | if (new_asn1_flag) |
| 308 | EC_GROUP_set_asn1_flag(group, asn1_flag); |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 309 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 310 | if (no_seed) { |
| 311 | EC_GROUP_set_seed(group, NULL, 0); |
| 312 | } |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 313 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 314 | if (text) { |
| 315 | if (!ECPKParameters_print(out, group, 0)) |
| 316 | goto end; |
| 317 | } |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 318 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 319 | if (check) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 320 | BIO_printf(bio_err, "checking elliptic curve parameters: "); |
| 321 | if (!EC_GROUP_check(group, NULL)) { |
| 322 | BIO_printf(bio_err, "failed\n"); |
| 323 | ERR_print_errors(bio_err); |
Peter Waltenberg | 99dcd88 | 2015-07-09 14:57:30 -0400 | [diff] [blame] | 324 | goto end; |
| 325 | } |
| 326 | BIO_printf(bio_err, "ok\n"); |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 327 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 328 | } |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 329 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 330 | if (C) { |
| 331 | size_t buf_len = 0, tmp_len = 0; |
| 332 | const EC_POINT *point; |
| 333 | int is_prime, len = 0; |
| 334 | const EC_METHOD *meth = EC_GROUP_method_of(group); |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 335 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 336 | if ((ec_p = BN_new()) == NULL |
| 337 | || (ec_a = BN_new()) == NULL |
| 338 | || (ec_b = BN_new()) == NULL |
| 339 | || (ec_gen = BN_new()) == NULL |
| 340 | || (ec_order = BN_new()) == NULL |
| 341 | || (ec_cofactor = BN_new()) == NULL) { |
Rich Salz | 68dc682 | 2015-04-30 17:48:31 -0400 | [diff] [blame] | 342 | perror("Can't allocate BN"); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 343 | goto end; |
| 344 | } |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 345 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 346 | is_prime = (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 347 | if (!is_prime) { |
| 348 | BIO_printf(bio_err, "Can only handle X9.62 prime fields\n"); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 349 | goto end; |
| 350 | } |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 351 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 352 | if (!EC_GROUP_get_curve_GFp(group, ec_p, ec_a, ec_b, NULL)) |
| 353 | goto end; |
| 354 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 355 | if ((point = EC_GROUP_get0_generator(group)) == NULL) |
| 356 | goto end; |
| 357 | if (!EC_POINT_point2bn(group, point, |
| 358 | EC_GROUP_get_point_conversion_form(group), |
| 359 | ec_gen, NULL)) |
| 360 | goto end; |
| 361 | if (!EC_GROUP_get_order(group, ec_order, NULL)) |
| 362 | goto end; |
| 363 | if (!EC_GROUP_get_cofactor(group, ec_cofactor, NULL)) |
| 364 | goto end; |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 365 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 366 | if (!ec_p || !ec_a || !ec_b || !ec_gen || !ec_order || !ec_cofactor) |
| 367 | goto end; |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 368 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 369 | len = BN_num_bits(ec_order); |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 370 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 371 | if ((tmp_len = (size_t)BN_num_bytes(ec_p)) > buf_len) |
| 372 | buf_len = tmp_len; |
| 373 | if ((tmp_len = (size_t)BN_num_bytes(ec_a)) > buf_len) |
| 374 | buf_len = tmp_len; |
| 375 | if ((tmp_len = (size_t)BN_num_bytes(ec_b)) > buf_len) |
| 376 | buf_len = tmp_len; |
| 377 | if ((tmp_len = (size_t)BN_num_bytes(ec_gen)) > buf_len) |
| 378 | buf_len = tmp_len; |
| 379 | if ((tmp_len = (size_t)BN_num_bytes(ec_order)) > buf_len) |
| 380 | buf_len = tmp_len; |
| 381 | if ((tmp_len = (size_t)BN_num_bytes(ec_cofactor)) > buf_len) |
| 382 | buf_len = tmp_len; |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 383 | |
Rich Salz | 68dc682 | 2015-04-30 17:48:31 -0400 | [diff] [blame] | 384 | buffer = app_malloc(buf_len, "BN buffer"); |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 385 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 386 | BIO_printf(out, "EC_GROUP *get_ec_group_%d(void)\n{\n", len); |
| 387 | print_bignum_var(out, ec_p, "ec_p", len, buffer); |
| 388 | print_bignum_var(out, ec_a, "ec_a", len, buffer); |
| 389 | print_bignum_var(out, ec_b, "ec_b", len, buffer); |
| 390 | print_bignum_var(out, ec_gen, "ec_gen", len, buffer); |
| 391 | print_bignum_var(out, ec_order, "ec_order", len, buffer); |
| 392 | print_bignum_var(out, ec_cofactor, "ec_cofactor", len, buffer); |
| 393 | BIO_printf(out, " int ok = 0;\n" |
| 394 | " EC_GROUP *group = NULL;\n" |
| 395 | " EC_POINT *point = NULL;\n" |
| 396 | " BIGNUM *tmp_1 = NULL;\n" |
| 397 | " BIGNUM *tmp_2 = NULL;\n" |
| 398 | " BIGNUM *tmp_3 = NULL;\n" |
| 399 | "\n"); |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 400 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 401 | BIO_printf(out, " if ((tmp_1 = BN_bin2bn(ec_p_%d, sizeof (ec_p_%d), NULL)) == NULL)\n" |
| 402 | " goto err;\n", len, len); |
| 403 | BIO_printf(out, " if ((tmp_2 = BN_bin2bn(ec_a_%d, sizeof (ec_a_%d), NULL)) == NULL)\n" |
| 404 | " goto err;\n", len, len); |
| 405 | BIO_printf(out, " if ((tmp_3 = BN_bin2bn(ec_b_%d, sizeof (ec_b_%d), NULL)) == NULL)\n" |
| 406 | " goto err;\n", len, len); |
| 407 | BIO_printf(out, " if ((group = EC_GROUP_new_curve_GFp(tmp_1, tmp_2, tmp_3, NULL)) == NULL)\n" |
| 408 | " goto err;\n" |
| 409 | "\n"); |
| 410 | BIO_printf(out, " /* build generator */\n"); |
| 411 | BIO_printf(out, " if ((tmp_1 = BN_bin2bn(ec_gen_%d, sizeof (ec_gen_%d), tmp_1)) == NULL)\n" |
| 412 | " goto err;\n", len, len); |
| 413 | BIO_printf(out, " point = EC_POINT_bn2point(group, tmp_1, NULL, NULL);\n"); |
| 414 | BIO_printf(out, " if (point == NULL)\n" |
| 415 | " goto err;\n"); |
| 416 | BIO_printf(out, " if ((tmp_2 = BN_bin2bn(ec_order_%d, sizeof (ec_order_%d), tmp_2)) == NULL)\n" |
| 417 | " goto err;\n", len, len); |
| 418 | BIO_printf(out, " if ((tmp_3 = BN_bin2bn(ec_cofactor_%d, sizeof (ec_cofactor_%d), tmp_3)) == NULL)\n" |
| 419 | " goto err;\n", len, len); |
| 420 | BIO_printf(out, " if (!EC_GROUP_set_generator(group, point, tmp_2, tmp_3))\n" |
| 421 | " goto err;\n" |
| 422 | "ok = 1;" |
| 423 | "\n"); |
| 424 | BIO_printf(out, "err:\n" |
| 425 | " BN_free(tmp_1);\n" |
| 426 | " BN_free(tmp_2);\n" |
| 427 | " BN_free(tmp_3);\n" |
| 428 | " EC_POINT_free(point);\n" |
| 429 | " if (!ok) {\n" |
| 430 | " EC_GROUP_free(group);\n" |
| 431 | " return NULL;\n" |
| 432 | " }\n" |
| 433 | " return (group);\n" |
| 434 | "}\n"); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 435 | } |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 436 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 437 | if (!noout) { |
| 438 | if (outformat == FORMAT_ASN1) |
| 439 | i = i2d_ECPKParameters_bio(out, group); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 440 | else |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 441 | i = PEM_write_bio_ECPKParameters(out, group); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 442 | if (!i) { |
| 443 | BIO_printf(bio_err, "unable to write elliptic " |
| 444 | "curve parameters\n"); |
| 445 | ERR_print_errors(bio_err); |
| 446 | goto end; |
| 447 | } |
| 448 | } |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 449 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 450 | if (need_rand) { |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 451 | app_RAND_load_file(NULL, (inrand != NULL)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 452 | if (inrand != NULL) |
| 453 | BIO_printf(bio_err, "%ld semi-random bytes loaded\n", |
| 454 | app_RAND_load_files(inrand)); |
| 455 | } |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 456 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 457 | if (genkey) { |
| 458 | EC_KEY *eckey = EC_KEY_new(); |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 459 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 460 | if (eckey == NULL) |
| 461 | goto end; |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 462 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 463 | assert(need_rand); |
| 464 | |
| 465 | if (EC_KEY_set_group(eckey, group) == 0) |
| 466 | goto end; |
| 467 | |
| 468 | if (!EC_KEY_generate_key(eckey)) { |
| 469 | EC_KEY_free(eckey); |
| 470 | goto end; |
| 471 | } |
Rich Salz | 3b061a0 | 2015-05-02 10:01:33 -0400 | [diff] [blame] | 472 | assert(private); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 473 | if (outformat == FORMAT_ASN1) |
| 474 | i = i2d_ECPrivateKey_bio(out, eckey); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 475 | else |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 476 | i = PEM_write_bio_ECPrivateKey(out, eckey, NULL, |
| 477 | NULL, 0, NULL, NULL); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 478 | EC_KEY_free(eckey); |
| 479 | } |
| 480 | |
| 481 | if (need_rand) |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 482 | app_RAND_write_file(NULL); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 483 | |
| 484 | ret = 0; |
| 485 | end: |
Rich Salz | 23a1d5e | 2015-04-30 21:37:06 -0400 | [diff] [blame] | 486 | BN_free(ec_p); |
| 487 | BN_free(ec_a); |
| 488 | BN_free(ec_b); |
| 489 | BN_free(ec_gen); |
| 490 | BN_free(ec_order); |
| 491 | BN_free(ec_cofactor); |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 492 | OPENSSL_free(buffer); |
Rich Salz | ca3a82c | 2015-03-25 11:31:18 -0400 | [diff] [blame] | 493 | BIO_free(in); |
| 494 | BIO_free_all(out); |
Rich Salz | 8fdc373 | 2015-03-25 18:35:24 -0400 | [diff] [blame] | 495 | EC_GROUP_free(group); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 496 | return (ret); |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 499 | #else /* !OPENSSL_NO_EC */ |
Dr. Stephen Henson | ef236ec | 2009-04-23 16:32:42 +0000 | [diff] [blame] | 500 | |
| 501 | # if PEDANTIC |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 502 | static void *dummy = &dummy; |
Dr. Stephen Henson | ef236ec | 2009-04-23 16:32:42 +0000 | [diff] [blame] | 503 | # endif |
| 504 | |
Bodo Möller | 5dbd3ef | 2002-07-14 16:54:31 +0000 | [diff] [blame] | 505 | #endif |