2 * Copyright 2002-2020 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
11 #include <openssl/opensslconf.h>
19 #include <openssl/bio.h>
20 #include <openssl/err.h>
21 #include <openssl/bn.h>
22 #include <openssl/ec.h>
23 #include <openssl/x509.h>
24 #include <openssl/pem.h>
26 typedef enum OPTION_choice {
27 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
28 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT, OPT_C,
29 OPT_CHECK, OPT_LIST_CURVES, OPT_NO_SEED, OPT_NOOUT, OPT_NAME,
30 OPT_CONV_FORM, OPT_PARAM_ENC, OPT_GENKEY, OPT_ENGINE, OPT_CHECK_NAMED,
31 OPT_R_ENUM, OPT_PROV_ENUM
34 const OPTIONS ecparam_options[] = {
35 OPT_SECTION("General"),
36 {"help", OPT_HELP, '-', "Display this summary"},
37 {"list_curves", OPT_LIST_CURVES, '-',
38 "Prints a list of all curve 'short names'"},
39 #ifndef OPENSSL_NO_ENGINE
40 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
43 {"genkey", OPT_GENKEY, '-', "Generate ec key"},
44 {"in", OPT_IN, '<', "Input file - default stdin"},
45 {"inform", OPT_INFORM, 'F', "Input format - default PEM (DER or PEM)"},
46 {"out", OPT_OUT, '>', "Output file - default stdout"},
47 {"outform", OPT_OUTFORM, 'F', "Output format - default PEM"},
49 OPT_SECTION("Output"),
50 {"text", OPT_TEXT, '-', "Print the ec parameters in text form"},
51 {"C", OPT_C, '-', "Print a 'C' function creating the parameters"},
52 {"noout", OPT_NOOUT, '-', "Do not print the ec parameter"},
53 {"param_enc", OPT_PARAM_ENC, 's',
54 "Specifies the way the ec parameters are encoded"},
56 OPT_SECTION("Parameter"),
57 {"check", OPT_CHECK, '-', "Validate the ec parameters"},
58 {"check_named", OPT_CHECK_NAMED, '-',
59 "Check that named EC curve parameters have not been modified"},
60 {"no_seed", OPT_NO_SEED, '-',
61 "If 'explicit' parameters are chosen do not use the seed"},
62 {"name", OPT_NAME, 's',
63 "Use the ec parameters with specified 'short name'"},
64 {"conv_form", OPT_CONV_FORM, 's', "Specifies the point conversion form "},
71 static OPT_PAIR forms[] = {
72 {"compressed", POINT_CONVERSION_COMPRESSED},
73 {"uncompressed", POINT_CONVERSION_UNCOMPRESSED},
74 {"hybrid", POINT_CONVERSION_HYBRID},
78 static OPT_PAIR encodings[] = {
79 {"named_curve", OPENSSL_EC_NAMED_CURVE},
84 int ecparam_main(int argc, char **argv)
87 BIGNUM *ec_gen = NULL, *ec_order = NULL, *ec_cofactor = NULL;
88 BIGNUM *ec_p = NULL, *ec_a = NULL, *ec_b = NULL;
89 BIO *in = NULL, *out = NULL;
90 EC_GROUP *group = NULL;
91 point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
92 char *curve_name = NULL;
93 char *infile = NULL, *outfile = NULL, *prog;
94 unsigned char *buffer = NULL;
96 int asn1_flag = OPENSSL_EC_NAMED_CURVE, new_asn1_flag = 0;
97 int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0;
98 int ret = 1, private = 0;
99 int list_curves = 0, no_seed = 0, check = 0, new_form = 0;
100 int text = 0, i, genkey = 0, check_named = 0;
102 prog = opt_init(argc, argv, ecparam_options);
103 while ((o = opt_next()) != OPT_EOF) {
108 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
111 opt_help(ecparam_options);
115 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
122 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
137 case OPT_CHECK_NAMED:
140 case OPT_LIST_CURVES:
150 curve_name = opt_arg();
153 if (!opt_pair(opt_arg(), forms, &new_form))
159 if (!opt_pair(opt_arg(), encodings, &asn1_flag))
171 if (!opt_provider(o))
175 e = setup_engine(opt_arg(), 0);
179 argc = opt_num_rest();
183 private = genkey ? 1 : 0;
185 in = bio_open_default(infile, 'r', informat);
188 out = bio_open_owner(outfile, outformat, private);
193 EC_builtin_curve *curves = NULL;
194 size_t crv_len = EC_get_builtin_curves(NULL, 0);
197 curves = app_malloc((int)sizeof(*curves) * crv_len, "list curves");
198 if (!EC_get_builtin_curves(curves, crv_len)) {
199 OPENSSL_free(curves);
203 for (n = 0; n < crv_len; n++) {
206 comment = curves[n].comment;
207 sname = OBJ_nid2sn(curves[n].nid);
209 comment = "CURVE DESCRIPTION NOT AVAILABLE";
213 BIO_printf(out, " %-10s: ", sname);
214 BIO_printf(out, "%s\n", comment);
217 OPENSSL_free(curves);
222 if (curve_name != NULL) {
226 * workaround for the SECG curve names secp192r1 and secp256r1 (which
227 * are the same as the curves prime192v1 and prime256v1 defined in
230 if (strcmp(curve_name, "secp192r1") == 0) {
231 BIO_printf(bio_err, "using curve name prime192v1 "
232 "instead of secp192r1\n");
233 nid = NID_X9_62_prime192v1;
234 } else if (strcmp(curve_name, "secp256r1") == 0) {
235 BIO_printf(bio_err, "using curve name prime256v1 "
236 "instead of secp256r1\n");
237 nid = NID_X9_62_prime256v1;
239 nid = OBJ_sn2nid(curve_name);
243 nid = EC_curve_nist2nid(curve_name);
246 BIO_printf(bio_err, "unknown curve name (%s)\n", curve_name);
250 group = EC_GROUP_new_by_curve_name(nid);
252 BIO_printf(bio_err, "unable to create curve (%s)\n", curve_name);
255 EC_GROUP_set_asn1_flag(group, asn1_flag);
256 EC_GROUP_set_point_conversion_form(group, form);
257 } else if (informat == FORMAT_ASN1) {
258 group = d2i_ECPKParameters_bio(in, NULL);
260 group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL);
263 BIO_printf(bio_err, "unable to load elliptic curve parameters\n");
264 ERR_print_errors(bio_err);
269 EC_GROUP_set_point_conversion_form(group, form);
272 EC_GROUP_set_asn1_flag(group, asn1_flag);
275 EC_GROUP_set_seed(group, NULL, 0);
279 if (!ECPKParameters_print(out, group, 0))
284 BIO_printf(bio_err, "validating named elliptic curve parameters: ");
285 if (EC_GROUP_check_named_curve(group, 0, NULL) <= 0) {
286 BIO_printf(bio_err, "failed\n");
287 ERR_print_errors(bio_err);
290 BIO_printf(bio_err, "ok\n");
294 BIO_printf(bio_err, "checking elliptic curve parameters: ");
295 if (!EC_GROUP_check(group, NULL)) {
296 BIO_printf(bio_err, "failed\n");
297 ERR_print_errors(bio_err);
300 BIO_printf(bio_err, "ok\n");
305 size_t buf_len = 0, tmp_len = 0;
306 const EC_POINT *point;
307 int is_prime, len = 0;
308 const EC_METHOD *meth = EC_GROUP_method_of(group);
310 if ((ec_p = BN_new()) == NULL
311 || (ec_a = BN_new()) == NULL
312 || (ec_b = BN_new()) == NULL
313 || (ec_gen = BN_new()) == NULL
314 || (ec_order = BN_new()) == NULL
315 || (ec_cofactor = BN_new()) == NULL) {
316 perror("Can't allocate BN");
320 is_prime = (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field);
322 BIO_printf(bio_err, "Can only handle X9.62 prime fields\n");
326 if (!EC_GROUP_get_curve(group, ec_p, ec_a, ec_b, NULL))
329 if ((point = EC_GROUP_get0_generator(group)) == NULL)
331 if (!EC_POINT_point2bn(group, point,
332 EC_GROUP_get_point_conversion_form(group),
335 if (!EC_GROUP_get_order(group, ec_order, NULL))
337 if (!EC_GROUP_get_cofactor(group, ec_cofactor, NULL))
340 if (!ec_p || !ec_a || !ec_b || !ec_gen || !ec_order || !ec_cofactor)
343 len = BN_num_bits(ec_order);
345 if ((tmp_len = (size_t)BN_num_bytes(ec_p)) > buf_len)
347 if ((tmp_len = (size_t)BN_num_bytes(ec_a)) > buf_len)
349 if ((tmp_len = (size_t)BN_num_bytes(ec_b)) > buf_len)
351 if ((tmp_len = (size_t)BN_num_bytes(ec_gen)) > buf_len)
353 if ((tmp_len = (size_t)BN_num_bytes(ec_order)) > buf_len)
355 if ((tmp_len = (size_t)BN_num_bytes(ec_cofactor)) > buf_len)
358 buffer = app_malloc(buf_len, "BN buffer");
360 BIO_printf(out, "EC_GROUP *get_ec_group_%d(void)\n{\n", len);
361 print_bignum_var(out, ec_p, "ec_p", len, buffer);
362 print_bignum_var(out, ec_a, "ec_a", len, buffer);
363 print_bignum_var(out, ec_b, "ec_b", len, buffer);
364 print_bignum_var(out, ec_gen, "ec_gen", len, buffer);
365 print_bignum_var(out, ec_order, "ec_order", len, buffer);
366 print_bignum_var(out, ec_cofactor, "ec_cofactor", len, buffer);
367 BIO_printf(out, " int ok = 0;\n"
368 " EC_GROUP *group = NULL;\n"
369 " EC_POINT *point = NULL;\n"
370 " BIGNUM *tmp_1 = NULL;\n"
371 " BIGNUM *tmp_2 = NULL;\n"
372 " BIGNUM *tmp_3 = NULL;\n"
375 BIO_printf(out, " if ((tmp_1 = BN_bin2bn(ec_p_%d, sizeof(ec_p_%d), NULL)) == NULL)\n"
376 " goto err;\n", len, len);
377 BIO_printf(out, " if ((tmp_2 = BN_bin2bn(ec_a_%d, sizeof(ec_a_%d), NULL)) == NULL)\n"
378 " goto err;\n", len, len);
379 BIO_printf(out, " if ((tmp_3 = BN_bin2bn(ec_b_%d, sizeof(ec_b_%d), NULL)) == NULL)\n"
380 " goto err;\n", len, len);
381 BIO_printf(out, " if ((group = EC_GROUP_new_curve_GFp(tmp_1, tmp_2, tmp_3, NULL)) == NULL)\n"
384 BIO_printf(out, " /* build generator */\n");
385 BIO_printf(out, " if ((tmp_1 = BN_bin2bn(ec_gen_%d, sizeof(ec_gen_%d), tmp_1)) == NULL)\n"
386 " goto err;\n", len, len);
387 BIO_printf(out, " point = EC_POINT_bn2point(group, tmp_1, NULL, NULL);\n");
388 BIO_printf(out, " if (point == NULL)\n"
390 BIO_printf(out, " if ((tmp_2 = BN_bin2bn(ec_order_%d, sizeof(ec_order_%d), tmp_2)) == NULL)\n"
391 " goto err;\n", len, len);
392 BIO_printf(out, " if ((tmp_3 = BN_bin2bn(ec_cofactor_%d, sizeof(ec_cofactor_%d), tmp_3)) == NULL)\n"
393 " goto err;\n", len, len);
394 BIO_printf(out, " if (!EC_GROUP_set_generator(group, point, tmp_2, tmp_3))\n"
398 BIO_printf(out, "err:\n"
402 " EC_POINT_free(point);\n"
404 " EC_GROUP_free(group);\n"
411 if (outformat == FORMAT_ASN1 && genkey)
415 if (outformat == FORMAT_ASN1)
416 i = i2d_ECPKParameters_bio(out, group);
418 i = PEM_write_bio_ECPKParameters(out, group);
420 BIO_printf(bio_err, "unable to write elliptic "
421 "curve parameters\n");
422 ERR_print_errors(bio_err);
428 EC_KEY *eckey = EC_KEY_new();
433 if (EC_KEY_set_group(eckey, group) == 0) {
434 BIO_printf(bio_err, "unable to set group when generating key\n");
436 ERR_print_errors(bio_err);
441 EC_KEY_set_conv_form(eckey, form);
443 if (!EC_KEY_generate_key(eckey)) {
444 BIO_printf(bio_err, "unable to generate key\n");
446 ERR_print_errors(bio_err);
450 if (outformat == FORMAT_ASN1)
451 i = i2d_ECPrivateKey_bio(out, eckey);
453 i = PEM_write_bio_ECPrivateKey(out, eckey, NULL,
454 NULL, 0, NULL, NULL);
465 BN_free(ec_cofactor);
466 OPENSSL_free(buffer);
467 EC_GROUP_free(group);