ecparam.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * Written by Nils Larsch for the OpenSSL project.
  3. */
  4. /* ====================================================================
  5. * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
  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
  12. * notice, this list of conditions and the following disclaimer.
  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. */
  57. /* ====================================================================
  58. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  59. *
  60. * Portions of the attached software ("Contribution") are developed by
  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. *
  66. * The elliptic curve binary polynomial software is originally written by
  67. * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
  68. *
  69. */
  70. #include <openssl/opensslconf.h>
  71. #ifndef OPENSSL_NO_EC
  72. # include <assert.h>
  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>
  84. typedef enum OPTION_choice {
  85. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  86. OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT, OPT_C,
  87. OPT_CHECK, OPT_LIST_CURVES, OPT_NO_SEED, OPT_NOOUT, OPT_NAME,
  88. OPT_CONV_FORM, OPT_PARAM_ENC, OPT_GENKEY, OPT_RAND, OPT_ENGINE
  89. } OPTION_CHOICE;
  90. OPTIONS ecparam_options[] = {
  91. {"help", OPT_HELP, '-', "Display this summary"},
  92. {"inform", OPT_INFORM, 'F', "Input format - default PEM (DER or PEM)"},
  93. {"outform", OPT_OUTFORM, 'F', "Output format - default PEM"},
  94. {"in", OPT_IN, '<', "Input file - default stdin"},
  95. {"out", OPT_OUT, '>', "Output file - default stdout"},
  96. {"text", OPT_TEXT, '-', "Print the ec parameters in text form"},
  97. {"C", OPT_C, '-', "Print a 'C' function creating the parameters"},
  98. {"check", OPT_CHECK, '-', "Validate the ec parameters"},
  99. {"list_curves", OPT_LIST_CURVES, '-',
  100. "Prints a list of all curve 'short names'"},
  101. {"no_seed", OPT_NO_SEED, '-',
  102. "If 'explicit' parameters are chosen do not use the seed"},
  103. {"noout", OPT_NOOUT, '-', "Do not print the ec parameter"},
  104. {"name", OPT_NAME, 's',
  105. "Use the ec parameters with specified 'short name'"},
  106. {"conv_form", OPT_CONV_FORM, 's', "Specifies the point conversion form "},
  107. {"param_enc", OPT_PARAM_ENC, 's',
  108. "Specifies the way the ec parameters are encoded"},
  109. {"genkey", OPT_GENKEY, '-', "Generate ec key"},
  110. {"rand", OPT_RAND, 's', "Files to use for random number input"},
  111. # ifndef OPENSSL_NO_ENGINE
  112. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  113. # endif
  114. {NULL}
  115. };
  116. OPT_PAIR forms[] = {
  117. {"compressed", POINT_CONVERSION_COMPRESSED},
  118. {"uncompressed", POINT_CONVERSION_UNCOMPRESSED},
  119. {"hybrid", POINT_CONVERSION_HYBRID},
  120. {NULL}
  121. };
  122. OPT_PAIR encodings[] = {
  123. {"named_curve", OPENSSL_EC_NAMED_CURVE},
  124. {"explicit", 0},
  125. {NULL}
  126. };
  127. int ecparam_main(int argc, char **argv)
  128. {
  129. BIGNUM *ec_gen = NULL, *ec_order = NULL, *ec_cofactor = NULL;
  130. BIGNUM *ec_p = NULL, *ec_a = NULL, *ec_b = NULL;
  131. BIO *in = NULL, *out = NULL;
  132. EC_GROUP *group = NULL;
  133. point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
  134. char *curve_name = NULL, *inrand = NULL;
  135. char *infile = NULL, *outfile = NULL, *prog;
  136. unsigned char *buffer = NULL;
  137. OPTION_CHOICE o;
  138. int asn1_flag = OPENSSL_EC_NAMED_CURVE, new_asn1_flag = 0;
  139. int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0, ret =
  140. 1;
  141. int list_curves = 0, no_seed = 0, check = 0, new_form = 0;
  142. int text = 0, i, need_rand = 0, genkey = 0;
  143. prog = opt_init(argc, argv, ecparam_options);
  144. while ((o = opt_next()) != OPT_EOF) {
  145. switch (o) {
  146. case OPT_EOF:
  147. case OPT_ERR:
  148. opthelp:
  149. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  150. goto end;
  151. case OPT_HELP:
  152. opt_help(ecparam_options);
  153. ret = 0;
  154. goto end;
  155. case OPT_INFORM:
  156. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  157. goto opthelp;
  158. break;
  159. case OPT_IN:
  160. infile = opt_arg();
  161. break;
  162. case OPT_OUTFORM:
  163. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  164. goto opthelp;
  165. break;
  166. case OPT_OUT:
  167. outfile = opt_arg();
  168. break;
  169. case OPT_TEXT:
  170. text = 1;
  171. break;
  172. case OPT_C:
  173. C = 1;
  174. break;
  175. case OPT_CHECK:
  176. check = 1;
  177. break;
  178. case OPT_LIST_CURVES:
  179. list_curves = 1;
  180. break;
  181. case OPT_NO_SEED:
  182. no_seed = 1;
  183. break;
  184. case OPT_NOOUT:
  185. noout = 1;
  186. break;
  187. case OPT_NAME:
  188. curve_name = opt_arg();
  189. break;
  190. case OPT_CONV_FORM:
  191. if (!opt_pair(opt_arg(), forms, &new_form))
  192. goto opthelp;
  193. form = new_form;
  194. new_form = 1;
  195. break;
  196. case OPT_PARAM_ENC:
  197. if (!opt_pair(opt_arg(), encodings, &asn1_flag))
  198. goto opthelp;
  199. new_asn1_flag = 1;
  200. break;
  201. case OPT_GENKEY:
  202. genkey = need_rand = 1;
  203. break;
  204. case OPT_RAND:
  205. inrand = opt_arg();
  206. need_rand = 1;
  207. break;
  208. case OPT_ENGINE:
  209. (void)setup_engine(opt_arg(), 0);
  210. break;
  211. }
  212. }
  213. argc = opt_num_rest();
  214. argv = opt_rest();
  215. in = bio_open_default(infile, RB(informat));
  216. if (in == NULL)
  217. goto end;
  218. out = bio_open_default(outfile, WB(outformat));
  219. if (out == NULL)
  220. goto end;
  221. if (list_curves) {
  222. EC_builtin_curve *curves = NULL;
  223. size_t crv_len = EC_get_builtin_curves(NULL, 0);
  224. size_t n;
  225. curves = app_malloc((int)sizeof(*curves) * crv_len, "list curves");
  226. if (!EC_get_builtin_curves(curves, crv_len)) {
  227. OPENSSL_free(curves);
  228. goto end;
  229. }
  230. for (n = 0; n < crv_len; n++) {
  231. const char *comment;
  232. const char *sname;
  233. comment = curves[n].comment;
  234. sname = OBJ_nid2sn(curves[n].nid);
  235. if (comment == NULL)
  236. comment = "CURVE DESCRIPTION NOT AVAILABLE";
  237. if (sname == NULL)
  238. sname = "";
  239. BIO_printf(out, " %-10s: ", sname);
  240. BIO_printf(out, "%s\n", comment);
  241. }
  242. OPENSSL_free(curves);
  243. ret = 0;
  244. goto end;
  245. }
  246. if (curve_name != NULL) {
  247. int nid;
  248. /*
  249. * workaround for the SECG curve names secp192r1 and secp256r1 (which
  250. * are the same as the curves prime192v1 and prime256v1 defined in
  251. * X9.62)
  252. */
  253. if (strcmp(curve_name, "secp192r1") == 0) {
  254. BIO_printf(bio_err, "using curve name prime192v1 "
  255. "instead of secp192r1\n");
  256. nid = NID_X9_62_prime192v1;
  257. } else if (strcmp(curve_name, "secp256r1") == 0) {
  258. BIO_printf(bio_err, "using curve name prime256v1 "
  259. "instead of secp256r1\n");
  260. nid = NID_X9_62_prime256v1;
  261. } else
  262. nid = OBJ_sn2nid(curve_name);
  263. if (nid == 0)
  264. nid = EC_curve_nist2nid(curve_name);
  265. if (nid == 0) {
  266. BIO_printf(bio_err, "unknown curve name (%s)\n", curve_name);
  267. goto end;
  268. }
  269. group = EC_GROUP_new_by_curve_name(nid);
  270. if (group == NULL) {
  271. BIO_printf(bio_err, "unable to create curve (%s)\n", curve_name);
  272. goto end;
  273. }
  274. EC_GROUP_set_asn1_flag(group, asn1_flag);
  275. EC_GROUP_set_point_conversion_form(group, form);
  276. } else if (informat == FORMAT_ASN1)
  277. group = d2i_ECPKParameters_bio(in, NULL);
  278. else
  279. group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL);
  280. if (group == NULL) {
  281. BIO_printf(bio_err, "unable to load elliptic curve parameters\n");
  282. ERR_print_errors(bio_err);
  283. goto end;
  284. }
  285. if (new_form)
  286. EC_GROUP_set_point_conversion_form(group, form);
  287. if (new_asn1_flag)
  288. EC_GROUP_set_asn1_flag(group, asn1_flag);
  289. if (no_seed) {
  290. EC_GROUP_set_seed(group, NULL, 0);
  291. }
  292. if (text) {
  293. if (!ECPKParameters_print(out, group, 0))
  294. goto end;
  295. }
  296. if (check) {
  297. if (group == NULL)
  298. BIO_printf(bio_err, "no elliptic curve parameters\n");
  299. BIO_printf(bio_err, "checking elliptic curve parameters: ");
  300. if (!EC_GROUP_check(group, NULL)) {
  301. BIO_printf(bio_err, "failed\n");
  302. ERR_print_errors(bio_err);
  303. } else
  304. BIO_printf(bio_err, "ok\n");
  305. }
  306. if (C) {
  307. size_t buf_len = 0, tmp_len = 0;
  308. const EC_POINT *point;
  309. int is_prime, len = 0;
  310. const EC_METHOD *meth = EC_GROUP_method_of(group);
  311. if ((ec_p = BN_new()) == NULL
  312. || (ec_a = BN_new()) == NULL
  313. || (ec_b = BN_new()) == NULL
  314. || (ec_gen = BN_new()) == NULL
  315. || (ec_order = BN_new()) == NULL
  316. || (ec_cofactor = BN_new()) == NULL) {
  317. perror("Can't allocate BN");
  318. goto end;
  319. }
  320. is_prime = (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field);
  321. if (!is_prime) {
  322. BIO_printf(bio_err, "Can only handle X9.62 prime fields\n");
  323. goto end;
  324. }
  325. if (!EC_GROUP_get_curve_GFp(group, ec_p, ec_a, ec_b, NULL))
  326. goto end;
  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. if (!ec_p || !ec_a || !ec_b || !ec_gen || !ec_order || !ec_cofactor)
  338. goto end;
  339. len = BN_num_bits(ec_order);
  340. if ((tmp_len = (size_t)BN_num_bytes(ec_p)) > buf_len)
  341. buf_len = tmp_len;
  342. if ((tmp_len = (size_t)BN_num_bytes(ec_a)) > buf_len)
  343. buf_len = tmp_len;
  344. if ((tmp_len = (size_t)BN_num_bytes(ec_b)) > buf_len)
  345. buf_len = tmp_len;
  346. if ((tmp_len = (size_t)BN_num_bytes(ec_gen)) > buf_len)
  347. buf_len = tmp_len;
  348. if ((tmp_len = (size_t)BN_num_bytes(ec_order)) > buf_len)
  349. buf_len = tmp_len;
  350. if ((tmp_len = (size_t)BN_num_bytes(ec_cofactor)) > buf_len)
  351. buf_len = tmp_len;
  352. buffer = app_malloc(buf_len, "BN buffer");
  353. BIO_printf(out, "EC_GROUP *get_ec_group_%d(void)\n{\n", len);
  354. print_bignum_var(out, ec_p, "ec_p", len, buffer);
  355. print_bignum_var(out, ec_a, "ec_a", len, buffer);
  356. print_bignum_var(out, ec_b, "ec_b", len, buffer);
  357. print_bignum_var(out, ec_gen, "ec_gen", len, buffer);
  358. print_bignum_var(out, ec_order, "ec_order", len, buffer);
  359. print_bignum_var(out, ec_cofactor, "ec_cofactor", len, buffer);
  360. BIO_printf(out, " int ok = 0;\n"
  361. " EC_GROUP *group = NULL;\n"
  362. " EC_POINT *point = NULL;\n"
  363. " BIGNUM *tmp_1 = NULL;\n"
  364. " BIGNUM *tmp_2 = NULL;\n"
  365. " BIGNUM *tmp_3 = NULL;\n"
  366. "\n");
  367. BIO_printf(out, " if ((tmp_1 = BN_bin2bn(ec_p_%d, sizeof (ec_p_%d), NULL)) == NULL)\n"
  368. " goto err;\n", len, len);
  369. BIO_printf(out, " if ((tmp_2 = BN_bin2bn(ec_a_%d, sizeof (ec_a_%d), NULL)) == NULL)\n"
  370. " goto err;\n", len, len);
  371. BIO_printf(out, " if ((tmp_3 = BN_bin2bn(ec_b_%d, sizeof (ec_b_%d), NULL)) == NULL)\n"
  372. " goto err;\n", len, len);
  373. BIO_printf(out, " if ((group = EC_GROUP_new_curve_GFp(tmp_1, tmp_2, tmp_3, NULL)) == NULL)\n"
  374. " goto err;\n"
  375. "\n");
  376. BIO_printf(out, " /* build generator */\n");
  377. BIO_printf(out, " if ((tmp_1 = BN_bin2bn(ec_gen_%d, sizeof (ec_gen_%d), tmp_1)) == NULL)\n"
  378. " goto err;\n", len, len);
  379. BIO_printf(out, " point = EC_POINT_bn2point(group, tmp_1, NULL, NULL);\n");
  380. BIO_printf(out, " if (point == NULL)\n"
  381. " goto err;\n");
  382. BIO_printf(out, " if ((tmp_2 = BN_bin2bn(ec_order_%d, sizeof (ec_order_%d), tmp_2)) == NULL)\n"
  383. " goto err;\n", len, len);
  384. BIO_printf(out, " if ((tmp_3 = BN_bin2bn(ec_cofactor_%d, sizeof (ec_cofactor_%d), tmp_3)) == NULL)\n"
  385. " goto err;\n", len, len);
  386. BIO_printf(out, " if (!EC_GROUP_set_generator(group, point, tmp_2, tmp_3))\n"
  387. " goto err;\n"
  388. "ok = 1;"
  389. "\n");
  390. BIO_printf(out, "err:\n"
  391. " BN_free(tmp_1);\n"
  392. " BN_free(tmp_2);\n"
  393. " BN_free(tmp_3);\n"
  394. " EC_POINT_free(point);\n"
  395. " if (!ok) {\n"
  396. " EC_GROUP_free(group);\n"
  397. " return NULL;\n"
  398. " }\n"
  399. " return (group);\n"
  400. "}\n");
  401. }
  402. if (!noout) {
  403. if (outformat == FORMAT_ASN1)
  404. i = i2d_ECPKParameters_bio(out, group);
  405. else
  406. i = PEM_write_bio_ECPKParameters(out, group);
  407. if (!i) {
  408. BIO_printf(bio_err, "unable to write elliptic "
  409. "curve parameters\n");
  410. ERR_print_errors(bio_err);
  411. goto end;
  412. }
  413. }
  414. if (need_rand) {
  415. app_RAND_load_file(NULL, (inrand != NULL));
  416. if (inrand != NULL)
  417. BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
  418. app_RAND_load_files(inrand));
  419. }
  420. if (genkey) {
  421. EC_KEY *eckey = EC_KEY_new();
  422. if (eckey == NULL)
  423. goto end;
  424. assert(need_rand);
  425. if (EC_KEY_set_group(eckey, group) == 0)
  426. goto end;
  427. if (!EC_KEY_generate_key(eckey)) {
  428. EC_KEY_free(eckey);
  429. goto end;
  430. }
  431. if (outformat == FORMAT_ASN1)
  432. i = i2d_ECPrivateKey_bio(out, eckey);
  433. else
  434. i = PEM_write_bio_ECPrivateKey(out, eckey, NULL,
  435. NULL, 0, NULL, NULL);
  436. EC_KEY_free(eckey);
  437. }
  438. if (need_rand)
  439. app_RAND_write_file(NULL);
  440. ret = 0;
  441. end:
  442. BN_free(ec_p);
  443. BN_free(ec_a);
  444. BN_free(ec_b);
  445. BN_free(ec_gen);
  446. BN_free(ec_order);
  447. BN_free(ec_cofactor);
  448. OPENSSL_free(buffer);
  449. BIO_free(in);
  450. BIO_free_all(out);
  451. EC_GROUP_free(group);
  452. return (ret);
  453. }
  454. #else /* !OPENSSL_NO_EC */
  455. # if PEDANTIC
  456. static void *dummy = &dummy;
  457. # endif
  458. #endif