]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/ecparam.c
Deprecate the low level RC5 functions
[thirdparty/openssl.git] / apps / ecparam.c
CommitLineData
14a7cfb3 1/*
6738bf14 2 * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
aa8f3d76 3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
5dbd3efc 4 *
dffa7520 5 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
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
5dbd3efc 9 */
846e33c7 10
6e04afb8 11#include <openssl/opensslconf.h>
effaf4de
RS
12#ifdef OPENSSL_NO_EC
13NON_EMPTY_TRANSLATION_UNIT
14#else
15
0f113f3e
MC
16# include <stdio.h>
17# include <stdlib.h>
18# include <time.h>
19# include <string.h>
20# include "apps.h"
dab2cd68 21# include "progs.h"
0f113f3e
MC
22# include <openssl/bio.h>
23# include <openssl/err.h>
24# include <openssl/bn.h>
25# include <openssl/ec.h>
26# include <openssl/x509.h>
27# include <openssl/pem.h>
28
7e1b7485
RS
29typedef enum OPTION_choice {
30 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
31 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT, OPT_C,
32 OPT_CHECK, OPT_LIST_CURVES, OPT_NO_SEED, OPT_NOOUT, OPT_NAME,
8402cd5f 33 OPT_CONV_FORM, OPT_PARAM_ENC, OPT_GENKEY, OPT_ENGINE, OPT_CHECK_NAMED,
3ee1eac2 34 OPT_R_ENUM
7e1b7485
RS
35} OPTION_CHOICE;
36
44c83ebd 37const OPTIONS ecparam_options[] = {
5388f986 38 OPT_SECTION("General"),
7e1b7485 39 {"help", OPT_HELP, '-', "Display this summary"},
5388f986
RS
40 {"list_curves", OPT_LIST_CURVES, '-',
41 "Prints a list of all curve 'short names'"},
42# ifndef OPENSSL_NO_ENGINE
43 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
44# endif
45
46 {"genkey", OPT_GENKEY, '-', "Generate ec key"},
7e1b7485 47 {"in", OPT_IN, '<', "Input file - default stdin"},
5388f986 48 {"inform", OPT_INFORM, 'F', "Input format - default PEM (DER or PEM)"},
7e1b7485 49 {"out", OPT_OUT, '>', "Output file - default stdout"},
5388f986
RS
50 {"outform", OPT_OUTFORM, 'F', "Output format - default PEM"},
51
52 OPT_SECTION("Output"),
7e1b7485
RS
53 {"text", OPT_TEXT, '-', "Print the ec parameters in text form"},
54 {"C", OPT_C, '-', "Print a 'C' function creating the parameters"},
5388f986
RS
55 {"noout", OPT_NOOUT, '-', "Do not print the ec parameter"},
56 {"param_enc", OPT_PARAM_ENC, 's',
57 "Specifies the way the ec parameters are encoded"},
58
59 OPT_SECTION("Parameter"),
7e1b7485 60 {"check", OPT_CHECK, '-', "Validate the ec parameters"},
8402cd5f
SL
61 {"check_named", OPT_CHECK_NAMED, '-',
62 "Check that named EC curve parameters have not been modified"},
7e1b7485
RS
63 {"no_seed", OPT_NO_SEED, '-',
64 "If 'explicit' parameters are chosen do not use the seed"},
7e1b7485
RS
65 {"name", OPT_NAME, 's',
66 "Use the ec parameters with specified 'short name'"},
67 {"conv_form", OPT_CONV_FORM, 's', "Specifies the point conversion form "},
5388f986 68
3ee1eac2 69 OPT_R_OPTIONS,
7e1b7485
RS
70 {NULL}
71};
72
df2ee0e2 73static OPT_PAIR forms[] = {
7e1b7485
RS
74 {"compressed", POINT_CONVERSION_COMPRESSED},
75 {"uncompressed", POINT_CONVERSION_UNCOMPRESSED},
76 {"hybrid", POINT_CONVERSION_HYBRID},
77 {NULL}
78};
79
df2ee0e2 80static OPT_PAIR encodings[] = {
7e1b7485
RS
81 {"named_curve", OPENSSL_EC_NAMED_CURVE},
82 {"explicit", 0},
83 {NULL}
84};
85
86int ecparam_main(int argc, char **argv)
0f113f3e 87{
dd1abd44 88 ENGINE *e = NULL;
7e1b7485
RS
89 BIGNUM *ec_gen = NULL, *ec_order = NULL, *ec_cofactor = NULL;
90 BIGNUM *ec_p = NULL, *ec_a = NULL, *ec_b = NULL;
91 BIO *in = NULL, *out = NULL;
0f113f3e
MC
92 EC_GROUP *group = NULL;
93 point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
3ee1eac2 94 char *curve_name = NULL;
333b070e 95 char *infile = NULL, *outfile = NULL, *prog;
0f113f3e 96 unsigned char *buffer = NULL;
7e1b7485
RS
97 OPTION_CHOICE o;
98 int asn1_flag = OPENSSL_EC_NAMED_CURVE, new_asn1_flag = 0;
3b061a00
RS
99 int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0;
100 int ret = 1, private = 0;
7e1b7485 101 int list_curves = 0, no_seed = 0, check = 0, new_form = 0;
8402cd5f 102 int text = 0, i, genkey = 0, check_named = 0;
7e1b7485
RS
103
104 prog = opt_init(argc, argv, ecparam_options);
105 while ((o = opt_next()) != OPT_EOF) {
106 switch (o) {
107 case OPT_EOF:
108 case OPT_ERR:
109 opthelp:
110 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
111 goto end;
112 case OPT_HELP:
113 opt_help(ecparam_options);
114 ret = 0;
115 goto end;
116 case OPT_INFORM:
117 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
118 goto opthelp;
119 break;
120 case OPT_IN:
121 infile = opt_arg();
122 break;
123 case OPT_OUTFORM:
124 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
125 goto opthelp;
126 break;
127 case OPT_OUT:
128 outfile = opt_arg();
129 break;
130 case OPT_TEXT:
0f113f3e 131 text = 1;
7e1b7485
RS
132 break;
133 case OPT_C:
0f113f3e 134 C = 1;
7e1b7485
RS
135 break;
136 case OPT_CHECK:
0f113f3e 137 check = 1;
7e1b7485 138 break;
8402cd5f
SL
139 case OPT_CHECK_NAMED:
140 check_named = 1;
141 break;
7e1b7485 142 case OPT_LIST_CURVES:
0f113f3e 143 list_curves = 1;
7e1b7485
RS
144 break;
145 case OPT_NO_SEED:
0f113f3e 146 no_seed = 1;
7e1b7485
RS
147 break;
148 case OPT_NOOUT:
0f113f3e 149 noout = 1;
7e1b7485
RS
150 break;
151 case OPT_NAME:
152 curve_name = opt_arg();
153 break;
154 case OPT_CONV_FORM:
155 if (!opt_pair(opt_arg(), forms, &new_form))
156 goto opthelp;
157 form = new_form;
158 new_form = 1;
159 break;
160 case OPT_PARAM_ENC:
161 if (!opt_pair(opt_arg(), encodings, &asn1_flag))
162 goto opthelp;
163 new_asn1_flag = 1;
164 break;
165 case OPT_GENKEY:
3ee1eac2 166 genkey = 1;
7e1b7485 167 break;
3ee1eac2
RS
168 case OPT_R_CASES:
169 if (!opt_rand(o))
170 goto end;
7e1b7485
RS
171 break;
172 case OPT_ENGINE:
dd1abd44 173 e = setup_engine(opt_arg(), 0);
0f113f3e
MC
174 break;
175 }
0f113f3e 176 }
7e1b7485 177 argc = opt_num_rest();
03358517
KR
178 if (argc != 0)
179 goto opthelp;
180
3b061a00 181 private = genkey ? 1 : 0;
0f113f3e 182
bdd58d98 183 in = bio_open_default(infile, 'r', informat);
7e1b7485 184 if (in == NULL)
0f113f3e 185 goto end;
bdd58d98 186 out = bio_open_owner(outfile, outformat, private);
7e1b7485 187 if (out == NULL)
0f113f3e 188 goto end;
0f113f3e 189
0f113f3e
MC
190 if (list_curves) {
191 EC_builtin_curve *curves = NULL;
68dc6824
RS
192 size_t crv_len = EC_get_builtin_curves(NULL, 0);
193 size_t n;
0f113f3e 194
b4faea50 195 curves = app_malloc((int)sizeof(*curves) * crv_len, "list curves");
0f113f3e
MC
196 if (!EC_get_builtin_curves(curves, crv_len)) {
197 OPENSSL_free(curves);
198 goto end;
199 }
200
201 for (n = 0; n < crv_len; n++) {
202 const char *comment;
203 const char *sname;
204 comment = curves[n].comment;
205 sname = OBJ_nid2sn(curves[n].nid);
206 if (comment == NULL)
207 comment = "CURVE DESCRIPTION NOT AVAILABLE";
208 if (sname == NULL)
209 sname = "";
210
211 BIO_printf(out, " %-10s: ", sname);
212 BIO_printf(out, "%s\n", comment);
213 }
214
215 OPENSSL_free(curves);
216 ret = 0;
217 goto end;
218 }
219
220 if (curve_name != NULL) {
221 int nid;
222
223 /*
224 * workaround for the SECG curve names secp192r1 and secp256r1 (which
225 * are the same as the curves prime192v1 and prime256v1 defined in
226 * X9.62)
227 */
86885c28 228 if (strcmp(curve_name, "secp192r1") == 0) {
0f113f3e
MC
229 BIO_printf(bio_err, "using curve name prime192v1 "
230 "instead of secp192r1\n");
231 nid = NID_X9_62_prime192v1;
86885c28 232 } else if (strcmp(curve_name, "secp256r1") == 0) {
0f113f3e
MC
233 BIO_printf(bio_err, "using curve name prime256v1 "
234 "instead of secp256r1\n");
235 nid = NID_X9_62_prime256v1;
2234212c 236 } else {
0f113f3e 237 nid = OBJ_sn2nid(curve_name);
2234212c 238 }
0f113f3e
MC
239
240 if (nid == 0)
241 nid = EC_curve_nist2nid(curve_name);
242
243 if (nid == 0) {
244 BIO_printf(bio_err, "unknown curve name (%s)\n", curve_name);
245 goto end;
246 }
247
248 group = EC_GROUP_new_by_curve_name(nid);
249 if (group == NULL) {
250 BIO_printf(bio_err, "unable to create curve (%s)\n", curve_name);
251 goto end;
252 }
253 EC_GROUP_set_asn1_flag(group, asn1_flag);
254 EC_GROUP_set_point_conversion_form(group, form);
2234212c 255 } else if (informat == FORMAT_ASN1) {
0f113f3e 256 group = d2i_ECPKParameters_bio(in, NULL);
2234212c 257 } else {
0f113f3e 258 group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL);
2234212c 259 }
0f113f3e
MC
260 if (group == NULL) {
261 BIO_printf(bio_err, "unable to load elliptic curve parameters\n");
262 ERR_print_errors(bio_err);
263 goto end;
264 }
265
266 if (new_form)
267 EC_GROUP_set_point_conversion_form(group, form);
268
269 if (new_asn1_flag)
270 EC_GROUP_set_asn1_flag(group, asn1_flag);
271
272 if (no_seed) {
273 EC_GROUP_set_seed(group, NULL, 0);
274 }
275
276 if (text) {
277 if (!ECPKParameters_print(out, group, 0))
278 goto end;
279 }
280
8402cd5f
SL
281 if (check_named) {
282 BIO_printf(bio_err, "validating named elliptic curve parameters: ");
a9612d6c 283 if (EC_GROUP_check_named_curve(group, 0, NULL) <= 0) {
8402cd5f
SL
284 BIO_printf(bio_err, "failed\n");
285 ERR_print_errors(bio_err);
286 goto end;
287 }
288 BIO_printf(bio_err, "ok\n");
289 }
290
0f113f3e 291 if (check) {
0f113f3e
MC
292 BIO_printf(bio_err, "checking elliptic curve parameters: ");
293 if (!EC_GROUP_check(group, NULL)) {
294 BIO_printf(bio_err, "failed\n");
295 ERR_print_errors(bio_err);
99dcd880
PW
296 goto end;
297 }
298 BIO_printf(bio_err, "ok\n");
0f113f3e
MC
299
300 }
301
302 if (C) {
303 size_t buf_len = 0, tmp_len = 0;
304 const EC_POINT *point;
305 int is_prime, len = 0;
306 const EC_METHOD *meth = EC_GROUP_method_of(group);
307
7e1b7485
RS
308 if ((ec_p = BN_new()) == NULL
309 || (ec_a = BN_new()) == NULL
310 || (ec_b = BN_new()) == NULL
311 || (ec_gen = BN_new()) == NULL
312 || (ec_order = BN_new()) == NULL
313 || (ec_cofactor = BN_new()) == NULL) {
68dc6824 314 perror("Can't allocate BN");
0f113f3e
MC
315 goto end;
316 }
317
318 is_prime = (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field);
7e1b7485
RS
319 if (!is_prime) {
320 BIO_printf(bio_err, "Can only handle X9.62 prime fields\n");
0f113f3e
MC
321 goto end;
322 }
323
9cc570d4 324 if (!EC_GROUP_get_curve(group, ec_p, ec_a, ec_b, NULL))
7e1b7485
RS
325 goto end;
326
0f113f3e
MC
327 if ((point = EC_GROUP_get0_generator(group)) == NULL)
328 goto end;
329 if (!EC_POINT_point2bn(group, point,
330 EC_GROUP_get_point_conversion_form(group),
331 ec_gen, NULL))
332 goto end;
333 if (!EC_GROUP_get_order(group, ec_order, NULL))
334 goto end;
335 if (!EC_GROUP_get_cofactor(group, ec_cofactor, NULL))
336 goto end;
337
338 if (!ec_p || !ec_a || !ec_b || !ec_gen || !ec_order || !ec_cofactor)
339 goto end;
340
341 len = BN_num_bits(ec_order);
342
343 if ((tmp_len = (size_t)BN_num_bytes(ec_p)) > buf_len)
344 buf_len = tmp_len;
345 if ((tmp_len = (size_t)BN_num_bytes(ec_a)) > buf_len)
346 buf_len = tmp_len;
347 if ((tmp_len = (size_t)BN_num_bytes(ec_b)) > buf_len)
348 buf_len = tmp_len;
349 if ((tmp_len = (size_t)BN_num_bytes(ec_gen)) > buf_len)
350 buf_len = tmp_len;
351 if ((tmp_len = (size_t)BN_num_bytes(ec_order)) > buf_len)
352 buf_len = tmp_len;
353 if ((tmp_len = (size_t)BN_num_bytes(ec_cofactor)) > buf_len)
354 buf_len = tmp_len;
355
68dc6824 356 buffer = app_malloc(buf_len, "BN buffer");
0f113f3e 357
7e1b7485
RS
358 BIO_printf(out, "EC_GROUP *get_ec_group_%d(void)\n{\n", len);
359 print_bignum_var(out, ec_p, "ec_p", len, buffer);
360 print_bignum_var(out, ec_a, "ec_a", len, buffer);
361 print_bignum_var(out, ec_b, "ec_b", len, buffer);
362 print_bignum_var(out, ec_gen, "ec_gen", len, buffer);
363 print_bignum_var(out, ec_order, "ec_order", len, buffer);
364 print_bignum_var(out, ec_cofactor, "ec_cofactor", len, buffer);
365 BIO_printf(out, " int ok = 0;\n"
366 " EC_GROUP *group = NULL;\n"
367 " EC_POINT *point = NULL;\n"
368 " BIGNUM *tmp_1 = NULL;\n"
369 " BIGNUM *tmp_2 = NULL;\n"
370 " BIGNUM *tmp_3 = NULL;\n"
371 "\n");
372
cbe29648 373 BIO_printf(out, " if ((tmp_1 = BN_bin2bn(ec_p_%d, sizeof(ec_p_%d), NULL)) == NULL)\n"
7e1b7485 374 " goto err;\n", len, len);
cbe29648 375 BIO_printf(out, " if ((tmp_2 = BN_bin2bn(ec_a_%d, sizeof(ec_a_%d), NULL)) == NULL)\n"
7e1b7485 376 " goto err;\n", len, len);
cbe29648 377 BIO_printf(out, " if ((tmp_3 = BN_bin2bn(ec_b_%d, sizeof(ec_b_%d), NULL)) == NULL)\n"
7e1b7485
RS
378 " goto err;\n", len, len);
379 BIO_printf(out, " if ((group = EC_GROUP_new_curve_GFp(tmp_1, tmp_2, tmp_3, NULL)) == NULL)\n"
380 " goto err;\n"
381 "\n");
382 BIO_printf(out, " /* build generator */\n");
cbe29648 383 BIO_printf(out, " if ((tmp_1 = BN_bin2bn(ec_gen_%d, sizeof(ec_gen_%d), tmp_1)) == NULL)\n"
7e1b7485
RS
384 " goto err;\n", len, len);
385 BIO_printf(out, " point = EC_POINT_bn2point(group, tmp_1, NULL, NULL);\n");
386 BIO_printf(out, " if (point == NULL)\n"
387 " goto err;\n");
cbe29648 388 BIO_printf(out, " if ((tmp_2 = BN_bin2bn(ec_order_%d, sizeof(ec_order_%d), tmp_2)) == NULL)\n"
7e1b7485 389 " goto err;\n", len, len);
cbe29648 390 BIO_printf(out, " if ((tmp_3 = BN_bin2bn(ec_cofactor_%d, sizeof(ec_cofactor_%d), tmp_3)) == NULL)\n"
7e1b7485
RS
391 " goto err;\n", len, len);
392 BIO_printf(out, " if (!EC_GROUP_set_generator(group, point, tmp_2, tmp_3))\n"
393 " goto err;\n"
394 "ok = 1;"
395 "\n");
396 BIO_printf(out, "err:\n"
397 " BN_free(tmp_1);\n"
398 " BN_free(tmp_2);\n"
399 " BN_free(tmp_3);\n"
400 " EC_POINT_free(point);\n"
401 " if (!ok) {\n"
402 " EC_GROUP_free(group);\n"
403 " return NULL;\n"
404 " }\n"
405 " return (group);\n"
406 "}\n");
0f113f3e
MC
407 }
408
4bdc25b0
BE
409 if (outformat == FORMAT_ASN1 && genkey)
410 noout = 1;
411
0f113f3e
MC
412 if (!noout) {
413 if (outformat == FORMAT_ASN1)
414 i = i2d_ECPKParameters_bio(out, group);
7e1b7485 415 else
0f113f3e 416 i = PEM_write_bio_ECPKParameters(out, group);
0f113f3e
MC
417 if (!i) {
418 BIO_printf(bio_err, "unable to write elliptic "
419 "curve parameters\n");
420 ERR_print_errors(bio_err);
421 goto end;
422 }
423 }
424
0f113f3e
MC
425 if (genkey) {
426 EC_KEY *eckey = EC_KEY_new();
427
428 if (eckey == NULL)
429 goto end;
430
f6de4eb7
DSH
431 if (EC_KEY_set_group(eckey, group) == 0) {
432 BIO_printf(bio_err, "unable to set group when generating key\n");
433 EC_KEY_free(eckey);
434 ERR_print_errors(bio_err);
0f113f3e 435 goto end;
f6de4eb7 436 }
0f113f3e 437
4bdc25b0
BE
438 if (new_form)
439 EC_KEY_set_conv_form(eckey, form);
440
0f113f3e 441 if (!EC_KEY_generate_key(eckey)) {
f6de4eb7 442 BIO_printf(bio_err, "unable to generate key\n");
0f113f3e 443 EC_KEY_free(eckey);
f6de4eb7 444 ERR_print_errors(bio_err);
0f113f3e
MC
445 goto end;
446 }
3b061a00 447 assert(private);
0f113f3e
MC
448 if (outformat == FORMAT_ASN1)
449 i = i2d_ECPrivateKey_bio(out, eckey);
7e1b7485 450 else
0f113f3e
MC
451 i = PEM_write_bio_ECPrivateKey(out, eckey, NULL,
452 NULL, 0, NULL, NULL);
0f113f3e
MC
453 EC_KEY_free(eckey);
454 }
455
0f113f3e
MC
456 ret = 0;
457 end:
23a1d5e9
RS
458 BN_free(ec_p);
459 BN_free(ec_a);
460 BN_free(ec_b);
461 BN_free(ec_gen);
462 BN_free(ec_order);
463 BN_free(ec_cofactor);
b548a1f1 464 OPENSSL_free(buffer);
dd1abd44
RL
465 EC_GROUP_free(group);
466 release_engine(e);
ca3a82c3
RS
467 BIO_free(in);
468 BIO_free_all(out);
26a7d938 469 return ret;
5dbd3efc
BM
470}
471
5dbd3efc 472#endif