2
0

ecparam.c 19 KB

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