ecparam.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /* apps/ecparam.c */
  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. #include <openssl/opensslconf.h>
  72. #ifndef OPENSSL_NO_EC
  73. #include <assert.h>
  74. #include <stdio.h>
  75. #include <stdlib.h>
  76. #include <time.h>
  77. #include <string.h>
  78. #include "apps.h"
  79. #include <openssl/bio.h>
  80. #include <openssl/err.h>
  81. #include <openssl/bn.h>
  82. #include <openssl/ec.h>
  83. #include <openssl/x509.h>
  84. #include <openssl/pem.h>
  85. #undef PROG
  86. #define PROG ecparam_main
  87. /* -inform arg - input format - default PEM (DER or PEM)
  88. * -outform arg - output format - default PEM
  89. * -in arg - input file - default stdin
  90. * -out arg - output file - default stdout
  91. * -noout - do not print the ec parameter
  92. * -text - print the ec parameters in text form
  93. * -check - validate the ec parameters
  94. * -C - print a 'C' function creating the parameters
  95. * -name arg - use the ec parameters with 'short name' name
  96. * -list_curves - prints a list of all currently available curve 'short names'
  97. * -conv_form arg - specifies the point conversion form
  98. * - possible values: compressed
  99. * uncompressed (default)
  100. * hybrid
  101. * -param_enc arg - specifies the way the ec parameters are encoded
  102. * in the asn1 der encoding
  103. * possible values: named_curve (default)
  104. * explicit
  105. * -no_seed - if 'explicit' parameters are choosen do not use the seed
  106. * -genkey - generate ec key
  107. * -rand file - files to use for random number input
  108. * -engine e - use engine e, possibly a hardware device
  109. */
  110. static int ecparam_print_var(BIO *,BIGNUM *,const char *,int,unsigned char *);
  111. int MAIN(int, char **);
  112. int MAIN(int argc, char **argv)
  113. {
  114. EC_GROUP *group = NULL;
  115. point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
  116. int new_form = 0;
  117. int asn1_flag = OPENSSL_EC_NAMED_CURVE;
  118. int new_asn1_flag = 0;
  119. char *curve_name = NULL, *inrand = NULL;
  120. int list_curves = 0, no_seed = 0, check = 0,
  121. badops = 0, text = 0, i, need_rand = 0, genkey = 0;
  122. char *infile = NULL, *outfile = NULL, *prog;
  123. BIO *in = NULL, *out = NULL;
  124. int informat, outformat, noout = 0, C = 0, ret = 1;
  125. char *engine = NULL;
  126. BIGNUM *ec_p = NULL, *ec_a = NULL, *ec_b = NULL,
  127. *ec_gen = NULL, *ec_order = NULL, *ec_cofactor = NULL;
  128. unsigned char *buffer = NULL;
  129. apps_startup();
  130. if (bio_err == NULL)
  131. if ((bio_err=BIO_new(BIO_s_file())) != NULL)
  132. BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
  133. if (!load_config(bio_err, NULL))
  134. goto end;
  135. informat=FORMAT_PEM;
  136. outformat=FORMAT_PEM;
  137. prog=argv[0];
  138. argc--;
  139. argv++;
  140. while (argc >= 1)
  141. {
  142. if (strcmp(*argv,"-inform") == 0)
  143. {
  144. if (--argc < 1) goto bad;
  145. informat=str2fmt(*(++argv));
  146. }
  147. else if (strcmp(*argv,"-outform") == 0)
  148. {
  149. if (--argc < 1) goto bad;
  150. outformat=str2fmt(*(++argv));
  151. }
  152. else if (strcmp(*argv,"-in") == 0)
  153. {
  154. if (--argc < 1) goto bad;
  155. infile= *(++argv);
  156. }
  157. else if (strcmp(*argv,"-out") == 0)
  158. {
  159. if (--argc < 1) goto bad;
  160. outfile= *(++argv);
  161. }
  162. else if (strcmp(*argv,"-text") == 0)
  163. text = 1;
  164. else if (strcmp(*argv,"-C") == 0)
  165. C = 1;
  166. else if (strcmp(*argv,"-check") == 0)
  167. check = 1;
  168. else if (strcmp (*argv, "-name") == 0)
  169. {
  170. if (--argc < 1)
  171. goto bad;
  172. curve_name = *(++argv);
  173. }
  174. else if (strcmp(*argv, "-list_curves") == 0)
  175. list_curves = 1;
  176. else if (strcmp(*argv, "-conv_form") == 0)
  177. {
  178. if (--argc < 1)
  179. goto bad;
  180. ++argv;
  181. new_form = 1;
  182. if (strcmp(*argv, "compressed") == 0)
  183. form = POINT_CONVERSION_COMPRESSED;
  184. else if (strcmp(*argv, "uncompressed") == 0)
  185. form = POINT_CONVERSION_UNCOMPRESSED;
  186. else if (strcmp(*argv, "hybrid") == 0)
  187. form = POINT_CONVERSION_HYBRID;
  188. else
  189. goto bad;
  190. }
  191. else if (strcmp(*argv, "-param_enc") == 0)
  192. {
  193. if (--argc < 1)
  194. goto bad;
  195. ++argv;
  196. new_asn1_flag = 1;
  197. if (strcmp(*argv, "named_curve") == 0)
  198. asn1_flag = OPENSSL_EC_NAMED_CURVE;
  199. else if (strcmp(*argv, "explicit") == 0)
  200. asn1_flag = 0;
  201. else
  202. goto bad;
  203. }
  204. else if (strcmp(*argv, "-no_seed") == 0)
  205. no_seed = 1;
  206. else if (strcmp(*argv, "-noout") == 0)
  207. noout=1;
  208. else if (strcmp(*argv,"-genkey") == 0)
  209. {
  210. genkey=1;
  211. need_rand=1;
  212. }
  213. else if (strcmp(*argv, "-rand") == 0)
  214. {
  215. if (--argc < 1) goto bad;
  216. inrand= *(++argv);
  217. need_rand=1;
  218. }
  219. else if(strcmp(*argv, "-engine") == 0)
  220. {
  221. if (--argc < 1) goto bad;
  222. engine = *(++argv);
  223. }
  224. else
  225. {
  226. BIO_printf(bio_err,"unknown option %s\n",*argv);
  227. badops=1;
  228. break;
  229. }
  230. argc--;
  231. argv++;
  232. }
  233. if (badops)
  234. {
  235. bad:
  236. BIO_printf(bio_err, "%s [options] <infile >outfile\n",prog);
  237. BIO_printf(bio_err, "where options are\n");
  238. BIO_printf(bio_err, " -inform arg input format - "
  239. "default PEM (DER or PEM)\n");
  240. BIO_printf(bio_err, " -outform arg output format - "
  241. "default PEM\n");
  242. BIO_printf(bio_err, " -in arg input file - "
  243. "default stdin\n");
  244. BIO_printf(bio_err, " -out arg output file - "
  245. "default stdout\n");
  246. BIO_printf(bio_err, " -noout do not print the "
  247. "ec parameter\n");
  248. BIO_printf(bio_err, " -text print the ec "
  249. "parameters in text form\n");
  250. BIO_printf(bio_err, " -check validate the ec "
  251. "parameters\n");
  252. BIO_printf(bio_err, " -C print a 'C' "
  253. "function creating the parameters\n");
  254. BIO_printf(bio_err, " -name arg use the "
  255. "ec parameters with 'short name' name\n");
  256. BIO_printf(bio_err, " -list_curves prints a list of "
  257. "all currently available curve 'short names'\n");
  258. BIO_printf(bio_err, " -conv_form arg specifies the "
  259. "point conversion form \n");
  260. BIO_printf(bio_err, " possible values:"
  261. " compressed\n");
  262. BIO_printf(bio_err, " "
  263. " uncompressed (default)\n");
  264. BIO_printf(bio_err, " "
  265. " hybrid\n");
  266. BIO_printf(bio_err, " -param_enc arg specifies the way"
  267. " the ec parameters are encoded\n");
  268. BIO_printf(bio_err, " in the asn1 der "
  269. "encoding\n");
  270. BIO_printf(bio_err, " possible values:"
  271. " named_curve (default)\n");
  272. BIO_printf(bio_err, " "
  273. " explicit\n");
  274. BIO_printf(bio_err, " -no_seed if 'explicit'"
  275. " parameters are choosen do not"
  276. " use the seed\n");
  277. BIO_printf(bio_err, " -genkey generate ec"
  278. " key\n");
  279. BIO_printf(bio_err, " -rand file files to use for"
  280. " random number input\n");
  281. BIO_printf(bio_err, " -engine e use engine e, "
  282. "possibly a hardware device\n");
  283. goto end;
  284. }
  285. ERR_load_crypto_strings();
  286. in=BIO_new(BIO_s_file());
  287. out=BIO_new(BIO_s_file());
  288. if ((in == NULL) || (out == NULL))
  289. {
  290. ERR_print_errors(bio_err);
  291. goto end;
  292. }
  293. if (infile == NULL)
  294. BIO_set_fp(in,stdin,BIO_NOCLOSE);
  295. else
  296. {
  297. if (BIO_read_filename(in,infile) <= 0)
  298. {
  299. perror(infile);
  300. goto end;
  301. }
  302. }
  303. if (outfile == NULL)
  304. {
  305. BIO_set_fp(out,stdout,BIO_NOCLOSE);
  306. #ifdef OPENSSL_SYS_VMS
  307. {
  308. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  309. out = BIO_push(tmpbio, out);
  310. }
  311. #endif
  312. }
  313. else
  314. {
  315. if (BIO_write_filename(out,outfile) <= 0)
  316. {
  317. perror(outfile);
  318. goto end;
  319. }
  320. }
  321. #ifndef OPENSSL_NO_ENGINE
  322. setup_engine(bio_err, engine, 0);
  323. #endif
  324. if (list_curves)
  325. {
  326. EC_builtin_curve *curves = NULL;
  327. size_t crv_len = 0;
  328. size_t n = 0;
  329. crv_len = EC_get_builtin_curves(NULL, 0);
  330. curves = OPENSSL_malloc((int)(sizeof(EC_builtin_curve) * crv_len));
  331. if (curves == NULL)
  332. goto end;
  333. if (!EC_get_builtin_curves(curves, crv_len))
  334. {
  335. OPENSSL_free(curves);
  336. goto end;
  337. }
  338. for (n = 0; n < crv_len; n++)
  339. {
  340. const char *comment;
  341. const char *sname;
  342. comment = curves[n].comment;
  343. sname = OBJ_nid2sn(curves[n].nid);
  344. if (comment == NULL)
  345. comment = "CURVE DESCRIPTION NOT AVAILABLE";
  346. if (sname == NULL)
  347. sname = "";
  348. BIO_printf(out, " %-10s: ", sname);
  349. BIO_printf(out, "%s\n", comment);
  350. }
  351. OPENSSL_free(curves);
  352. ret = 0;
  353. goto end;
  354. }
  355. if (curve_name != NULL)
  356. {
  357. int nid;
  358. /* workaround for the SECG curve names secp192r1
  359. * and secp256r1 (which are the same as the curves
  360. * prime192v1 and prime256v1 defined in X9.62)
  361. */
  362. if (!strcmp(curve_name, "secp192r1"))
  363. {
  364. BIO_printf(bio_err, "using curve name prime192v1 "
  365. "instead of secp192r1\n");
  366. nid = NID_X9_62_prime192v1;
  367. }
  368. else if (!strcmp(curve_name, "secp256r1"))
  369. {
  370. BIO_printf(bio_err, "using curve name prime256v1 "
  371. "instead of secp256r1\n");
  372. nid = NID_X9_62_prime256v1;
  373. }
  374. else
  375. nid = OBJ_sn2nid(curve_name);
  376. if (nid == 0)
  377. {
  378. BIO_printf(bio_err, "unknown curve name (%s)\n",
  379. curve_name);
  380. goto end;
  381. }
  382. group = EC_GROUP_new_by_curve_name(nid);
  383. if (group == NULL)
  384. {
  385. BIO_printf(bio_err, "unable to create curve (%s)\n",
  386. curve_name);
  387. goto end;
  388. }
  389. EC_GROUP_set_asn1_flag(group, asn1_flag);
  390. EC_GROUP_set_point_conversion_form(group, form);
  391. }
  392. else if (informat == FORMAT_ASN1)
  393. {
  394. group = d2i_ECPKParameters_bio(in, NULL);
  395. }
  396. else if (informat == FORMAT_PEM)
  397. {
  398. group = PEM_read_bio_ECPKParameters(in,NULL,NULL,NULL);
  399. }
  400. else
  401. {
  402. BIO_printf(bio_err, "bad input format specified\n");
  403. goto end;
  404. }
  405. if (group == NULL)
  406. {
  407. BIO_printf(bio_err,
  408. "unable to load elliptic curve parameters\n");
  409. ERR_print_errors(bio_err);
  410. goto end;
  411. }
  412. if (new_form)
  413. EC_GROUP_set_point_conversion_form(group, form);
  414. if (new_asn1_flag)
  415. EC_GROUP_set_asn1_flag(group, asn1_flag);
  416. if (no_seed)
  417. {
  418. EC_GROUP_set_seed(group, NULL, 0);
  419. }
  420. if (text)
  421. {
  422. if (!ECPKParameters_print(out, group, 0))
  423. goto end;
  424. }
  425. if (check)
  426. {
  427. if (group == NULL)
  428. BIO_printf(bio_err, "no elliptic curve parameters\n");
  429. BIO_printf(bio_err, "checking elliptic curve parameters: ");
  430. if (!EC_GROUP_check(group, NULL))
  431. {
  432. BIO_printf(bio_err, "failed\n");
  433. ERR_print_errors(bio_err);
  434. }
  435. else
  436. BIO_printf(bio_err, "ok\n");
  437. }
  438. if (C)
  439. {
  440. size_t buf_len = 0, tmp_len = 0;
  441. const EC_POINT *point;
  442. int is_prime, len = 0;
  443. const EC_METHOD *meth = EC_GROUP_method_of(group);
  444. if ((ec_p = BN_new()) == NULL || (ec_a = BN_new()) == NULL ||
  445. (ec_b = BN_new()) == NULL || (ec_gen = BN_new()) == NULL ||
  446. (ec_order = BN_new()) == NULL ||
  447. (ec_cofactor = BN_new()) == NULL )
  448. {
  449. perror("OPENSSL_malloc");
  450. goto end;
  451. }
  452. is_prime = (EC_METHOD_get_field_type(meth) ==
  453. NID_X9_62_prime_field);
  454. if (is_prime)
  455. {
  456. if (!EC_GROUP_get_curve_GFp(group, ec_p, ec_a,
  457. ec_b, NULL))
  458. goto end;
  459. }
  460. else
  461. {
  462. /* TODO */
  463. goto end;
  464. }
  465. if ((point = EC_GROUP_get0_generator(group)) == NULL)
  466. goto end;
  467. if (!EC_POINT_point2bn(group, point,
  468. EC_GROUP_get_point_conversion_form(group), ec_gen,
  469. NULL))
  470. goto end;
  471. if (!EC_GROUP_get_order(group, ec_order, NULL))
  472. goto end;
  473. if (!EC_GROUP_get_cofactor(group, ec_cofactor, NULL))
  474. goto end;
  475. if (!ec_p || !ec_a || !ec_b || !ec_gen ||
  476. !ec_order || !ec_cofactor)
  477. goto end;
  478. len = BN_num_bits(ec_order);
  479. if ((tmp_len = (size_t)BN_num_bytes(ec_p)) > buf_len)
  480. buf_len = tmp_len;
  481. if ((tmp_len = (size_t)BN_num_bytes(ec_a)) > buf_len)
  482. buf_len = tmp_len;
  483. if ((tmp_len = (size_t)BN_num_bytes(ec_b)) > buf_len)
  484. buf_len = tmp_len;
  485. if ((tmp_len = (size_t)BN_num_bytes(ec_gen)) > buf_len)
  486. buf_len = tmp_len;
  487. if ((tmp_len = (size_t)BN_num_bytes(ec_order)) > buf_len)
  488. buf_len = tmp_len;
  489. if ((tmp_len = (size_t)BN_num_bytes(ec_cofactor)) > buf_len)
  490. buf_len = tmp_len;
  491. buffer = (unsigned char *)OPENSSL_malloc(buf_len);
  492. if (buffer == NULL)
  493. {
  494. perror("OPENSSL_malloc");
  495. goto end;
  496. }
  497. ecparam_print_var(out, ec_p, "ec_p", len, buffer);
  498. ecparam_print_var(out, ec_a, "ec_a", len, buffer);
  499. ecparam_print_var(out, ec_b, "ec_b", len, buffer);
  500. ecparam_print_var(out, ec_gen, "ec_gen", len, buffer);
  501. ecparam_print_var(out, ec_order, "ec_order", len, buffer);
  502. ecparam_print_var(out, ec_cofactor, "ec_cofactor", len,
  503. buffer);
  504. BIO_printf(out, "\n\n");
  505. BIO_printf(out, "EC_GROUP *get_ec_group_%d(void)\n\t{\n", len);
  506. BIO_printf(out, "\tint ok=0;\n");
  507. BIO_printf(out, "\tEC_GROUP *group = NULL;\n");
  508. BIO_printf(out, "\tEC_POINT *point = NULL;\n");
  509. BIO_printf(out, "\tBIGNUM *tmp_1 = NULL, *tmp_2 = NULL, "
  510. "*tmp_3 = NULL;\n\n");
  511. BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_p_%d, "
  512. "sizeof(ec_p_%d), NULL)) == NULL)\n\t\t"
  513. "goto err;\n", len, len);
  514. BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_a_%d, "
  515. "sizeof(ec_a_%d), NULL)) == NULL)\n\t\t"
  516. "goto err;\n", len, len);
  517. BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_b_%d, "
  518. "sizeof(ec_b_%d), NULL)) == NULL)\n\t\t"
  519. "goto err;\n", len, len);
  520. if (is_prime)
  521. {
  522. BIO_printf(out, "\tif ((group = EC_GROUP_new_curve_"
  523. "GFp(tmp_1, tmp_2, tmp_3, NULL)) == NULL)"
  524. "\n\t\tgoto err;\n\n");
  525. }
  526. else
  527. {
  528. /* TODO */
  529. goto end;
  530. }
  531. BIO_printf(out, "\t/* build generator */\n");
  532. BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_gen_%d, "
  533. "sizeof(ec_gen_%d), tmp_1)) == NULL)"
  534. "\n\t\tgoto err;\n", len, len);
  535. BIO_printf(out, "\tpoint = EC_POINT_bn2point(group, tmp_1, "
  536. "NULL, NULL);\n");
  537. BIO_printf(out, "\tif (point == NULL)\n\t\tgoto err;\n");
  538. BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_order_%d, "
  539. "sizeof(ec_order_%d), tmp_2)) == NULL)"
  540. "\n\t\tgoto err;\n", len, len);
  541. BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_cofactor_%d, "
  542. "sizeof(ec_cofactor_%d), tmp_3)) == NULL)"
  543. "\n\t\tgoto err;\n", len, len);
  544. BIO_printf(out, "\tif (!EC_GROUP_set_generator(group, point,"
  545. " tmp_2, tmp_3))\n\t\tgoto err;\n");
  546. BIO_printf(out, "\n\tok=1;\n");
  547. BIO_printf(out, "err:\n");
  548. BIO_printf(out, "\tif (tmp_1)\n\t\tBN_free(tmp_1);\n");
  549. BIO_printf(out, "\tif (tmp_2)\n\t\tBN_free(tmp_2);\n");
  550. BIO_printf(out, "\tif (tmp_3)\n\t\tBN_free(tmp_3);\n");
  551. BIO_printf(out, "\tif (point)\n\t\tEC_POINT_free(point);\n");
  552. BIO_printf(out, "\tif (!ok)\n");
  553. BIO_printf(out, "\t\t{\n");
  554. BIO_printf(out, "\t\tEC_GROUP_free(group);\n");
  555. BIO_printf(out, "\t\tgroup = NULL;\n");
  556. BIO_printf(out, "\t\t}\n");
  557. BIO_printf(out, "\treturn(group);\n\t}\n");
  558. }
  559. if (!noout)
  560. {
  561. if (outformat == FORMAT_ASN1)
  562. i = i2d_ECPKParameters_bio(out, group);
  563. else if (outformat == FORMAT_PEM)
  564. i = PEM_write_bio_ECPKParameters(out, group);
  565. else
  566. {
  567. BIO_printf(bio_err,"bad output format specified for"
  568. " outfile\n");
  569. goto end;
  570. }
  571. if (!i)
  572. {
  573. BIO_printf(bio_err, "unable to write elliptic "
  574. "curve parameters\n");
  575. ERR_print_errors(bio_err);
  576. goto end;
  577. }
  578. }
  579. if (need_rand)
  580. {
  581. app_RAND_load_file(NULL, bio_err, (inrand != NULL));
  582. if (inrand != NULL)
  583. BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
  584. app_RAND_load_files(inrand));
  585. }
  586. if (genkey)
  587. {
  588. EC_KEY *eckey = EC_KEY_new();
  589. if (eckey == NULL)
  590. goto end;
  591. assert(need_rand);
  592. if (EC_KEY_set_group(eckey, group) == 0)
  593. goto end;
  594. if (!EC_KEY_generate_key(eckey))
  595. {
  596. EC_KEY_free(eckey);
  597. goto end;
  598. }
  599. if (outformat == FORMAT_ASN1)
  600. i = i2d_ECPrivateKey_bio(out, eckey);
  601. else if (outformat == FORMAT_PEM)
  602. i = PEM_write_bio_ECPrivateKey(out, eckey, NULL,
  603. NULL, 0, NULL, NULL);
  604. else
  605. {
  606. BIO_printf(bio_err, "bad output format specified "
  607. "for outfile\n");
  608. EC_KEY_free(eckey);
  609. goto end;
  610. }
  611. EC_KEY_free(eckey);
  612. }
  613. if (need_rand)
  614. app_RAND_write_file(NULL, bio_err);
  615. ret=0;
  616. end:
  617. if (ec_p)
  618. BN_free(ec_p);
  619. if (ec_a)
  620. BN_free(ec_a);
  621. if (ec_b)
  622. BN_free(ec_b);
  623. if (ec_gen)
  624. BN_free(ec_gen);
  625. if (ec_order)
  626. BN_free(ec_order);
  627. if (ec_cofactor)
  628. BN_free(ec_cofactor);
  629. if (buffer)
  630. OPENSSL_free(buffer);
  631. if (in != NULL)
  632. BIO_free(in);
  633. if (out != NULL)
  634. BIO_free_all(out);
  635. if (group != NULL)
  636. EC_GROUP_free(group);
  637. apps_shutdown();
  638. OPENSSL_EXIT(ret);
  639. }
  640. static int ecparam_print_var(BIO *out, BIGNUM *in, const char *var,
  641. int len, unsigned char *buffer)
  642. {
  643. BIO_printf(out, "static unsigned char %s_%d[] = {", var, len);
  644. if (BN_is_zero(in))
  645. BIO_printf(out, "\n\t0x00");
  646. else
  647. {
  648. int i, l;
  649. l = BN_bn2bin(in, buffer);
  650. for (i=0; i<l-1; i++)
  651. {
  652. if ((i%12) == 0)
  653. BIO_printf(out, "\n\t");
  654. BIO_printf(out, "0x%02X,", buffer[i]);
  655. }
  656. if ((i%12) == 0)
  657. BIO_printf(out, "\n\t");
  658. BIO_printf(out, "0x%02X", buffer[i]);
  659. }
  660. BIO_printf(out, "\n\t};\n\n");
  661. return 1;
  662. }
  663. #else /* !OPENSSL_NO_EC */
  664. # if PEDANTIC
  665. static void *dummy=&dummy;
  666. # endif
  667. #endif