ec_lib.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794
  1. /*
  2. * Copyright 2001-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. *
  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
  9. */
  10. /*
  11. * EC_GROUP low level APIs are deprecated for public use, but still ok for
  12. * internal use.
  13. */
  14. #include "internal/deprecated.h"
  15. #include <string.h>
  16. #include <openssl/params.h>
  17. #include <openssl/core_names.h>
  18. #include <openssl/err.h>
  19. #include <openssl/opensslv.h>
  20. #include <openssl/param_build.h>
  21. #include "crypto/ec.h"
  22. #include "internal/nelem.h"
  23. #include "ec_local.h"
  24. /* functions for EC_GROUP objects */
  25. EC_GROUP *ossl_ec_group_new_ex(OSSL_LIB_CTX *libctx, const char *propq,
  26. const EC_METHOD *meth)
  27. {
  28. EC_GROUP *ret;
  29. if (meth == NULL) {
  30. ERR_raise(ERR_LIB_EC, EC_R_SLOT_FULL);
  31. return NULL;
  32. }
  33. if (meth->group_init == 0) {
  34. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  35. return NULL;
  36. }
  37. ret = OPENSSL_zalloc(sizeof(*ret));
  38. if (ret == NULL)
  39. return NULL;
  40. ret->libctx = libctx;
  41. if (propq != NULL) {
  42. ret->propq = OPENSSL_strdup(propq);
  43. if (ret->propq == NULL)
  44. goto err;
  45. }
  46. ret->meth = meth;
  47. if ((ret->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
  48. ret->order = BN_new();
  49. if (ret->order == NULL)
  50. goto err;
  51. ret->cofactor = BN_new();
  52. if (ret->cofactor == NULL)
  53. goto err;
  54. }
  55. ret->asn1_flag = OPENSSL_EC_EXPLICIT_CURVE;
  56. ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
  57. if (!meth->group_init(ret))
  58. goto err;
  59. return ret;
  60. err:
  61. BN_free(ret->order);
  62. BN_free(ret->cofactor);
  63. OPENSSL_free(ret->propq);
  64. OPENSSL_free(ret);
  65. return NULL;
  66. }
  67. #ifndef OPENSSL_NO_DEPRECATED_3_0
  68. # ifndef FIPS_MODULE
  69. EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
  70. {
  71. return ossl_ec_group_new_ex(NULL, NULL, meth);
  72. }
  73. # endif
  74. #endif
  75. void EC_pre_comp_free(EC_GROUP *group)
  76. {
  77. switch (group->pre_comp_type) {
  78. case PCT_none:
  79. break;
  80. case PCT_nistz256:
  81. #ifdef ECP_NISTZ256_ASM
  82. EC_nistz256_pre_comp_free(group->pre_comp.nistz256);
  83. #endif
  84. break;
  85. #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  86. case PCT_nistp224:
  87. EC_nistp224_pre_comp_free(group->pre_comp.nistp224);
  88. break;
  89. case PCT_nistp256:
  90. EC_nistp256_pre_comp_free(group->pre_comp.nistp256);
  91. break;
  92. case PCT_nistp384:
  93. ossl_ec_nistp384_pre_comp_free(group->pre_comp.nistp384);
  94. break;
  95. case PCT_nistp521:
  96. EC_nistp521_pre_comp_free(group->pre_comp.nistp521);
  97. break;
  98. #else
  99. case PCT_nistp224:
  100. case PCT_nistp256:
  101. case PCT_nistp384:
  102. case PCT_nistp521:
  103. break;
  104. #endif
  105. case PCT_ec:
  106. EC_ec_pre_comp_free(group->pre_comp.ec);
  107. break;
  108. }
  109. group->pre_comp.ec = NULL;
  110. }
  111. void EC_GROUP_free(EC_GROUP *group)
  112. {
  113. if (!group)
  114. return;
  115. if (group->meth->group_finish != 0)
  116. group->meth->group_finish(group);
  117. EC_pre_comp_free(group);
  118. BN_MONT_CTX_free(group->mont_data);
  119. EC_POINT_free(group->generator);
  120. BN_free(group->order);
  121. BN_free(group->cofactor);
  122. OPENSSL_free(group->seed);
  123. OPENSSL_free(group->propq);
  124. OPENSSL_free(group);
  125. }
  126. #ifndef OPENSSL_NO_DEPRECATED_3_0
  127. void EC_GROUP_clear_free(EC_GROUP *group)
  128. {
  129. if (!group)
  130. return;
  131. if (group->meth->group_clear_finish != 0)
  132. group->meth->group_clear_finish(group);
  133. else if (group->meth->group_finish != 0)
  134. group->meth->group_finish(group);
  135. EC_pre_comp_free(group);
  136. BN_MONT_CTX_free(group->mont_data);
  137. EC_POINT_clear_free(group->generator);
  138. BN_clear_free(group->order);
  139. BN_clear_free(group->cofactor);
  140. OPENSSL_clear_free(group->seed, group->seed_len);
  141. OPENSSL_clear_free(group, sizeof(*group));
  142. }
  143. #endif
  144. int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
  145. {
  146. if (dest->meth->group_copy == 0) {
  147. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  148. return 0;
  149. }
  150. if (dest->meth != src->meth) {
  151. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  152. return 0;
  153. }
  154. if (dest == src)
  155. return 1;
  156. dest->libctx = src->libctx;
  157. dest->curve_name = src->curve_name;
  158. /* Copy precomputed */
  159. dest->pre_comp_type = src->pre_comp_type;
  160. switch (src->pre_comp_type) {
  161. case PCT_none:
  162. dest->pre_comp.ec = NULL;
  163. break;
  164. case PCT_nistz256:
  165. #ifdef ECP_NISTZ256_ASM
  166. dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);
  167. #endif
  168. break;
  169. #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  170. case PCT_nistp224:
  171. dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);
  172. break;
  173. case PCT_nistp256:
  174. dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);
  175. break;
  176. case PCT_nistp384:
  177. dest->pre_comp.nistp384 = ossl_ec_nistp384_pre_comp_dup(src->pre_comp.nistp384);
  178. break;
  179. case PCT_nistp521:
  180. dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);
  181. break;
  182. #else
  183. case PCT_nistp224:
  184. case PCT_nistp256:
  185. case PCT_nistp384:
  186. case PCT_nistp521:
  187. break;
  188. #endif
  189. case PCT_ec:
  190. dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);
  191. break;
  192. }
  193. if (src->mont_data != NULL) {
  194. if (dest->mont_data == NULL) {
  195. dest->mont_data = BN_MONT_CTX_new();
  196. if (dest->mont_data == NULL)
  197. return 0;
  198. }
  199. if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))
  200. return 0;
  201. } else {
  202. /* src->generator == NULL */
  203. BN_MONT_CTX_free(dest->mont_data);
  204. dest->mont_data = NULL;
  205. }
  206. if (src->generator != NULL) {
  207. if (dest->generator == NULL) {
  208. dest->generator = EC_POINT_new(dest);
  209. if (dest->generator == NULL)
  210. return 0;
  211. }
  212. if (!EC_POINT_copy(dest->generator, src->generator))
  213. return 0;
  214. } else {
  215. /* src->generator == NULL */
  216. EC_POINT_clear_free(dest->generator);
  217. dest->generator = NULL;
  218. }
  219. if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
  220. if (!BN_copy(dest->order, src->order))
  221. return 0;
  222. if (!BN_copy(dest->cofactor, src->cofactor))
  223. return 0;
  224. }
  225. dest->asn1_flag = src->asn1_flag;
  226. dest->asn1_form = src->asn1_form;
  227. dest->decoded_from_explicit_params = src->decoded_from_explicit_params;
  228. if (src->seed) {
  229. OPENSSL_free(dest->seed);
  230. if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL)
  231. return 0;
  232. if (!memcpy(dest->seed, src->seed, src->seed_len))
  233. return 0;
  234. dest->seed_len = src->seed_len;
  235. } else {
  236. OPENSSL_free(dest->seed);
  237. dest->seed = NULL;
  238. dest->seed_len = 0;
  239. }
  240. return dest->meth->group_copy(dest, src);
  241. }
  242. EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
  243. {
  244. EC_GROUP *t = NULL;
  245. int ok = 0;
  246. if (a == NULL)
  247. return NULL;
  248. if ((t = ossl_ec_group_new_ex(a->libctx, a->propq, a->meth)) == NULL)
  249. return NULL;
  250. if (!EC_GROUP_copy(t, a))
  251. goto err;
  252. ok = 1;
  253. err:
  254. if (!ok) {
  255. EC_GROUP_free(t);
  256. return NULL;
  257. }
  258. return t;
  259. }
  260. #ifndef OPENSSL_NO_DEPRECATED_3_0
  261. const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
  262. {
  263. return group->meth;
  264. }
  265. int EC_METHOD_get_field_type(const EC_METHOD *meth)
  266. {
  267. return meth->field_type;
  268. }
  269. #endif
  270. static int ec_precompute_mont_data(EC_GROUP *);
  271. /*-
  272. * Try computing cofactor from the generator order (n) and field cardinality (q).
  273. * This works for all curves of cryptographic interest.
  274. *
  275. * Hasse thm: q + 1 - 2*sqrt(q) <= n*h <= q + 1 + 2*sqrt(q)
  276. * h_min = (q + 1 - 2*sqrt(q))/n
  277. * h_max = (q + 1 + 2*sqrt(q))/n
  278. * h_max - h_min = 4*sqrt(q)/n
  279. * So if n > 4*sqrt(q) holds, there is only one possible value for h:
  280. * h = \lfloor (h_min + h_max)/2 \rceil = \lfloor (q + 1)/n \rceil
  281. *
  282. * Otherwise, zero cofactor and return success.
  283. */
  284. static int ec_guess_cofactor(EC_GROUP *group) {
  285. int ret = 0;
  286. BN_CTX *ctx = NULL;
  287. BIGNUM *q = NULL;
  288. /*-
  289. * If the cofactor is too large, we cannot guess it.
  290. * The RHS of below is a strict overestimate of lg(4 * sqrt(q))
  291. */
  292. if (BN_num_bits(group->order) <= (BN_num_bits(group->field) + 1) / 2 + 3) {
  293. /* default to 0 */
  294. BN_zero(group->cofactor);
  295. /* return success */
  296. return 1;
  297. }
  298. if ((ctx = BN_CTX_new_ex(group->libctx)) == NULL)
  299. return 0;
  300. BN_CTX_start(ctx);
  301. if ((q = BN_CTX_get(ctx)) == NULL)
  302. goto err;
  303. /* set q = 2**m for binary fields; q = p otherwise */
  304. if (group->meth->field_type == NID_X9_62_characteristic_two_field) {
  305. BN_zero(q);
  306. if (!BN_set_bit(q, BN_num_bits(group->field) - 1))
  307. goto err;
  308. } else {
  309. if (!BN_copy(q, group->field))
  310. goto err;
  311. }
  312. /* compute h = \lfloor (q + 1)/n \rceil = \lfloor (q + 1 + n/2)/n \rfloor */
  313. if (!BN_rshift1(group->cofactor, group->order) /* n/2 */
  314. || !BN_add(group->cofactor, group->cofactor, q) /* q + n/2 */
  315. /* q + 1 + n/2 */
  316. || !BN_add(group->cofactor, group->cofactor, BN_value_one())
  317. /* (q + 1 + n/2)/n */
  318. || !BN_div(group->cofactor, NULL, group->cofactor, group->order, ctx))
  319. goto err;
  320. ret = 1;
  321. err:
  322. BN_CTX_end(ctx);
  323. BN_CTX_free(ctx);
  324. return ret;
  325. }
  326. int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
  327. const BIGNUM *order, const BIGNUM *cofactor)
  328. {
  329. if (generator == NULL) {
  330. ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
  331. return 0;
  332. }
  333. /* require group->field >= 1 */
  334. if (group->field == NULL || BN_is_zero(group->field)
  335. || BN_is_negative(group->field)) {
  336. ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
  337. return 0;
  338. }
  339. /*-
  340. * - require order >= 1
  341. * - enforce upper bound due to Hasse thm: order can be no more than one bit
  342. * longer than field cardinality
  343. */
  344. if (order == NULL || BN_is_zero(order) || BN_is_negative(order)
  345. || BN_num_bits(order) > BN_num_bits(group->field) + 1) {
  346. ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
  347. return 0;
  348. }
  349. /*-
  350. * Unfortunately the cofactor is an optional field in many standards.
  351. * Internally, the lib uses 0 cofactor as a marker for "unknown cofactor".
  352. * So accept cofactor == NULL or cofactor >= 0.
  353. */
  354. if (cofactor != NULL && BN_is_negative(cofactor)) {
  355. ERR_raise(ERR_LIB_EC, EC_R_UNKNOWN_COFACTOR);
  356. return 0;
  357. }
  358. if (group->generator == NULL) {
  359. group->generator = EC_POINT_new(group);
  360. if (group->generator == NULL)
  361. return 0;
  362. }
  363. if (!EC_POINT_copy(group->generator, generator))
  364. return 0;
  365. if (!BN_copy(group->order, order))
  366. return 0;
  367. /* Either take the provided positive cofactor, or try to compute it */
  368. if (cofactor != NULL && !BN_is_zero(cofactor)) {
  369. if (!BN_copy(group->cofactor, cofactor))
  370. return 0;
  371. } else if (!ec_guess_cofactor(group)) {
  372. BN_zero(group->cofactor);
  373. return 0;
  374. }
  375. /*
  376. * Some groups have an order with
  377. * factors of two, which makes the Montgomery setup fail.
  378. * |group->mont_data| will be NULL in this case.
  379. */
  380. if (BN_is_odd(group->order)) {
  381. return ec_precompute_mont_data(group);
  382. }
  383. BN_MONT_CTX_free(group->mont_data);
  384. group->mont_data = NULL;
  385. return 1;
  386. }
  387. const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
  388. {
  389. return group->generator;
  390. }
  391. BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
  392. {
  393. return group->mont_data;
  394. }
  395. int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
  396. {
  397. if (group->order == NULL)
  398. return 0;
  399. if (!BN_copy(order, group->order))
  400. return 0;
  401. return !BN_is_zero(order);
  402. }
  403. const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group)
  404. {
  405. return group->order;
  406. }
  407. int EC_GROUP_order_bits(const EC_GROUP *group)
  408. {
  409. return group->meth->group_order_bits(group);
  410. }
  411. int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
  412. BN_CTX *ctx)
  413. {
  414. if (group->cofactor == NULL)
  415. return 0;
  416. if (!BN_copy(cofactor, group->cofactor))
  417. return 0;
  418. return !BN_is_zero(group->cofactor);
  419. }
  420. const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group)
  421. {
  422. return group->cofactor;
  423. }
  424. void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
  425. {
  426. group->curve_name = nid;
  427. group->asn1_flag =
  428. (nid != NID_undef)
  429. ? OPENSSL_EC_NAMED_CURVE
  430. : OPENSSL_EC_EXPLICIT_CURVE;
  431. }
  432. int EC_GROUP_get_curve_name(const EC_GROUP *group)
  433. {
  434. return group->curve_name;
  435. }
  436. const BIGNUM *EC_GROUP_get0_field(const EC_GROUP *group)
  437. {
  438. return group->field;
  439. }
  440. int EC_GROUP_get_field_type(const EC_GROUP *group)
  441. {
  442. return group->meth->field_type;
  443. }
  444. void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
  445. {
  446. group->asn1_flag = flag;
  447. }
  448. int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
  449. {
  450. return group->asn1_flag;
  451. }
  452. void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
  453. point_conversion_form_t form)
  454. {
  455. group->asn1_form = form;
  456. }
  457. point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
  458. *group)
  459. {
  460. return group->asn1_form;
  461. }
  462. size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
  463. {
  464. OPENSSL_free(group->seed);
  465. group->seed = NULL;
  466. group->seed_len = 0;
  467. if (!len || !p)
  468. return 1;
  469. if ((group->seed = OPENSSL_malloc(len)) == NULL)
  470. return 0;
  471. memcpy(group->seed, p, len);
  472. group->seed_len = len;
  473. return len;
  474. }
  475. unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
  476. {
  477. return group->seed;
  478. }
  479. size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
  480. {
  481. return group->seed_len;
  482. }
  483. int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
  484. const BIGNUM *b, BN_CTX *ctx)
  485. {
  486. if (group->meth->group_set_curve == 0) {
  487. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  488. return 0;
  489. }
  490. return group->meth->group_set_curve(group, p, a, b, ctx);
  491. }
  492. int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b,
  493. BN_CTX *ctx)
  494. {
  495. if (group->meth->group_get_curve == NULL) {
  496. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  497. return 0;
  498. }
  499. return group->meth->group_get_curve(group, p, a, b, ctx);
  500. }
  501. #ifndef OPENSSL_NO_DEPRECATED_3_0
  502. int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
  503. const BIGNUM *b, BN_CTX *ctx)
  504. {
  505. return EC_GROUP_set_curve(group, p, a, b, ctx);
  506. }
  507. int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
  508. BIGNUM *b, BN_CTX *ctx)
  509. {
  510. return EC_GROUP_get_curve(group, p, a, b, ctx);
  511. }
  512. # ifndef OPENSSL_NO_EC2M
  513. int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
  514. const BIGNUM *b, BN_CTX *ctx)
  515. {
  516. return EC_GROUP_set_curve(group, p, a, b, ctx);
  517. }
  518. int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
  519. BIGNUM *b, BN_CTX *ctx)
  520. {
  521. return EC_GROUP_get_curve(group, p, a, b, ctx);
  522. }
  523. # endif
  524. #endif
  525. int EC_GROUP_get_degree(const EC_GROUP *group)
  526. {
  527. if (group->meth->group_get_degree == 0) {
  528. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  529. return 0;
  530. }
  531. return group->meth->group_get_degree(group);
  532. }
  533. int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
  534. {
  535. if (group->meth->group_check_discriminant == 0) {
  536. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  537. return 0;
  538. }
  539. return group->meth->group_check_discriminant(group, ctx);
  540. }
  541. int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
  542. {
  543. int r = 0;
  544. BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
  545. #ifndef FIPS_MODULE
  546. BN_CTX *ctx_new = NULL;
  547. #endif
  548. /* compare the field types */
  549. if (EC_GROUP_get_field_type(a) != EC_GROUP_get_field_type(b))
  550. return 1;
  551. /* compare the curve name (if present in both) */
  552. if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
  553. EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
  554. return 1;
  555. if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE)
  556. return 0;
  557. #ifndef FIPS_MODULE
  558. if (ctx == NULL)
  559. ctx_new = ctx = BN_CTX_new();
  560. #endif
  561. if (ctx == NULL)
  562. return -1;
  563. BN_CTX_start(ctx);
  564. a1 = BN_CTX_get(ctx);
  565. a2 = BN_CTX_get(ctx);
  566. a3 = BN_CTX_get(ctx);
  567. b1 = BN_CTX_get(ctx);
  568. b2 = BN_CTX_get(ctx);
  569. b3 = BN_CTX_get(ctx);
  570. if (b3 == NULL) {
  571. BN_CTX_end(ctx);
  572. #ifndef FIPS_MODULE
  573. BN_CTX_free(ctx_new);
  574. #endif
  575. return -1;
  576. }
  577. /*
  578. * XXX This approach assumes that the external representation of curves
  579. * over the same field type is the same.
  580. */
  581. if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
  582. !b->meth->group_get_curve(b, b1, b2, b3, ctx))
  583. r = 1;
  584. /* return 1 if the curve parameters are different */
  585. if (r || BN_cmp(a1, b1) != 0 || BN_cmp(a2, b2) != 0 || BN_cmp(a3, b3) != 0)
  586. r = 1;
  587. /* XXX EC_POINT_cmp() assumes that the methods are equal */
  588. /* return 1 if the generators are different */
  589. if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
  590. EC_GROUP_get0_generator(b), ctx) != 0)
  591. r = 1;
  592. if (!r) {
  593. const BIGNUM *ao, *bo, *ac, *bc;
  594. /* compare the orders */
  595. ao = EC_GROUP_get0_order(a);
  596. bo = EC_GROUP_get0_order(b);
  597. if (ao == NULL || bo == NULL) {
  598. /* return an error if either order is NULL */
  599. r = -1;
  600. goto end;
  601. }
  602. if (BN_cmp(ao, bo) != 0) {
  603. /* return 1 if orders are different */
  604. r = 1;
  605. goto end;
  606. }
  607. /*
  608. * It gets here if the curve parameters and generator matched.
  609. * Now check the optional cofactors (if both are present).
  610. */
  611. ac = EC_GROUP_get0_cofactor(a);
  612. bc = EC_GROUP_get0_cofactor(b);
  613. /* Returns 1 (mismatch) if both cofactors are specified and different */
  614. if (!BN_is_zero(ac) && !BN_is_zero(bc) && BN_cmp(ac, bc) != 0)
  615. r = 1;
  616. /* Returns 0 if the parameters matched */
  617. }
  618. end:
  619. BN_CTX_end(ctx);
  620. #ifndef FIPS_MODULE
  621. BN_CTX_free(ctx_new);
  622. #endif
  623. return r;
  624. }
  625. /* functions for EC_POINT objects */
  626. EC_POINT *EC_POINT_new(const EC_GROUP *group)
  627. {
  628. EC_POINT *ret;
  629. if (group == NULL) {
  630. ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
  631. return NULL;
  632. }
  633. if (group->meth->point_init == NULL) {
  634. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  635. return NULL;
  636. }
  637. ret = OPENSSL_zalloc(sizeof(*ret));
  638. if (ret == NULL)
  639. return NULL;
  640. ret->meth = group->meth;
  641. ret->curve_name = group->curve_name;
  642. if (!ret->meth->point_init(ret)) {
  643. OPENSSL_free(ret);
  644. return NULL;
  645. }
  646. return ret;
  647. }
  648. void EC_POINT_free(EC_POINT *point)
  649. {
  650. if (point == NULL)
  651. return;
  652. if (point->meth->point_finish != 0)
  653. point->meth->point_finish(point);
  654. OPENSSL_free(point);
  655. }
  656. void EC_POINT_clear_free(EC_POINT *point)
  657. {
  658. if (point == NULL)
  659. return;
  660. if (point->meth->point_clear_finish != 0)
  661. point->meth->point_clear_finish(point);
  662. else if (point->meth->point_finish != 0)
  663. point->meth->point_finish(point);
  664. OPENSSL_clear_free(point, sizeof(*point));
  665. }
  666. int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
  667. {
  668. if (dest->meth->point_copy == 0) {
  669. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  670. return 0;
  671. }
  672. if (dest->meth != src->meth
  673. || (dest->curve_name != src->curve_name
  674. && dest->curve_name != 0
  675. && src->curve_name != 0)) {
  676. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  677. return 0;
  678. }
  679. if (dest == src)
  680. return 1;
  681. return dest->meth->point_copy(dest, src);
  682. }
  683. EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
  684. {
  685. EC_POINT *t;
  686. int r;
  687. if (a == NULL)
  688. return NULL;
  689. t = EC_POINT_new(group);
  690. if (t == NULL)
  691. return NULL;
  692. r = EC_POINT_copy(t, a);
  693. if (!r) {
  694. EC_POINT_free(t);
  695. return NULL;
  696. }
  697. return t;
  698. }
  699. #ifndef OPENSSL_NO_DEPRECATED_3_0
  700. const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
  701. {
  702. return point->meth;
  703. }
  704. #endif
  705. int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
  706. {
  707. if (group->meth->point_set_to_infinity == 0) {
  708. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  709. return 0;
  710. }
  711. if (group->meth != point->meth) {
  712. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  713. return 0;
  714. }
  715. return group->meth->point_set_to_infinity(group, point);
  716. }
  717. #ifndef OPENSSL_NO_DEPRECATED_3_0
  718. int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
  719. EC_POINT *point, const BIGNUM *x,
  720. const BIGNUM *y, const BIGNUM *z,
  721. BN_CTX *ctx)
  722. {
  723. if (group->meth->field_type != NID_X9_62_prime_field) {
  724. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  725. return 0;
  726. }
  727. if (!ec_point_is_compat(point, group)) {
  728. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  729. return 0;
  730. }
  731. return ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(group, point,
  732. x, y, z, ctx);
  733. }
  734. int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
  735. const EC_POINT *point, BIGNUM *x,
  736. BIGNUM *y, BIGNUM *z,
  737. BN_CTX *ctx)
  738. {
  739. if (group->meth->field_type != NID_X9_62_prime_field) {
  740. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  741. return 0;
  742. }
  743. if (!ec_point_is_compat(point, group)) {
  744. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  745. return 0;
  746. }
  747. return ossl_ec_GFp_simple_get_Jprojective_coordinates_GFp(group, point,
  748. x, y, z, ctx);
  749. }
  750. #endif
  751. int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
  752. const BIGNUM *x, const BIGNUM *y,
  753. BN_CTX *ctx)
  754. {
  755. if (group->meth->point_set_affine_coordinates == NULL) {
  756. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  757. return 0;
  758. }
  759. if (!ec_point_is_compat(point, group)) {
  760. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  761. return 0;
  762. }
  763. if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
  764. return 0;
  765. if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
  766. ERR_raise(ERR_LIB_EC, EC_R_POINT_IS_NOT_ON_CURVE);
  767. return 0;
  768. }
  769. return 1;
  770. }
  771. #ifndef OPENSSL_NO_DEPRECATED_3_0
  772. int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
  773. EC_POINT *point, const BIGNUM *x,
  774. const BIGNUM *y, BN_CTX *ctx)
  775. {
  776. return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
  777. }
  778. # ifndef OPENSSL_NO_EC2M
  779. int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
  780. EC_POINT *point, const BIGNUM *x,
  781. const BIGNUM *y, BN_CTX *ctx)
  782. {
  783. return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
  784. }
  785. # endif
  786. #endif
  787. int EC_POINT_get_affine_coordinates(const EC_GROUP *group,
  788. const EC_POINT *point, BIGNUM *x, BIGNUM *y,
  789. BN_CTX *ctx)
  790. {
  791. if (group->meth->point_get_affine_coordinates == NULL) {
  792. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  793. return 0;
  794. }
  795. if (!ec_point_is_compat(point, group)) {
  796. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  797. return 0;
  798. }
  799. if (EC_POINT_is_at_infinity(group, point)) {
  800. ERR_raise(ERR_LIB_EC, EC_R_POINT_AT_INFINITY);
  801. return 0;
  802. }
  803. return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
  804. }
  805. #ifndef OPENSSL_NO_DEPRECATED_3_0
  806. int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
  807. const EC_POINT *point, BIGNUM *x,
  808. BIGNUM *y, BN_CTX *ctx)
  809. {
  810. return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
  811. }
  812. # ifndef OPENSSL_NO_EC2M
  813. int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
  814. const EC_POINT *point, BIGNUM *x,
  815. BIGNUM *y, BN_CTX *ctx)
  816. {
  817. return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
  818. }
  819. # endif
  820. #endif
  821. int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  822. const EC_POINT *b, BN_CTX *ctx)
  823. {
  824. if (group->meth->add == 0) {
  825. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  826. return 0;
  827. }
  828. if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)
  829. || !ec_point_is_compat(b, group)) {
  830. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  831. return 0;
  832. }
  833. return group->meth->add(group, r, a, b, ctx);
  834. }
  835. int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  836. BN_CTX *ctx)
  837. {
  838. if (group->meth->dbl == 0) {
  839. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  840. return 0;
  841. }
  842. if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) {
  843. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  844. return 0;
  845. }
  846. return group->meth->dbl(group, r, a, ctx);
  847. }
  848. int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
  849. {
  850. if (group->meth->invert == 0) {
  851. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  852. return 0;
  853. }
  854. if (!ec_point_is_compat(a, group)) {
  855. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  856. return 0;
  857. }
  858. return group->meth->invert(group, a, ctx);
  859. }
  860. int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
  861. {
  862. if (group->meth->is_at_infinity == 0) {
  863. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  864. return 0;
  865. }
  866. if (!ec_point_is_compat(point, group)) {
  867. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  868. return 0;
  869. }
  870. return group->meth->is_at_infinity(group, point);
  871. }
  872. /*
  873. * Check whether an EC_POINT is on the curve or not. Note that the return
  874. * value for this function should NOT be treated as a boolean. Return values:
  875. * 1: The point is on the curve
  876. * 0: The point is not on the curve
  877. * -1: An error occurred
  878. */
  879. int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
  880. BN_CTX *ctx)
  881. {
  882. if (group->meth->is_on_curve == 0) {
  883. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  884. return 0;
  885. }
  886. if (!ec_point_is_compat(point, group)) {
  887. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  888. return 0;
  889. }
  890. return group->meth->is_on_curve(group, point, ctx);
  891. }
  892. int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
  893. BN_CTX *ctx)
  894. {
  895. if (group->meth->point_cmp == 0) {
  896. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  897. return -1;
  898. }
  899. if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) {
  900. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  901. return -1;
  902. }
  903. return group->meth->point_cmp(group, a, b, ctx);
  904. }
  905. #ifndef OPENSSL_NO_DEPRECATED_3_0
  906. int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
  907. {
  908. if (group->meth->make_affine == 0) {
  909. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  910. return 0;
  911. }
  912. if (!ec_point_is_compat(point, group)) {
  913. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  914. return 0;
  915. }
  916. return group->meth->make_affine(group, point, ctx);
  917. }
  918. int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
  919. EC_POINT *points[], BN_CTX *ctx)
  920. {
  921. size_t i;
  922. if (group->meth->points_make_affine == 0) {
  923. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  924. return 0;
  925. }
  926. for (i = 0; i < num; i++) {
  927. if (!ec_point_is_compat(points[i], group)) {
  928. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  929. return 0;
  930. }
  931. }
  932. return group->meth->points_make_affine(group, num, points, ctx);
  933. }
  934. #endif
  935. /*
  936. * Functions for point multiplication. If group->meth->mul is 0, we use the
  937. * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
  938. * methods.
  939. */
  940. #ifndef OPENSSL_NO_DEPRECATED_3_0
  941. int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
  942. size_t num, const EC_POINT *points[],
  943. const BIGNUM *scalars[], BN_CTX *ctx)
  944. {
  945. int ret = 0;
  946. size_t i = 0;
  947. #ifndef FIPS_MODULE
  948. BN_CTX *new_ctx = NULL;
  949. #endif
  950. if (!ec_point_is_compat(r, group)) {
  951. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  952. return 0;
  953. }
  954. if (scalar == NULL && num == 0)
  955. return EC_POINT_set_to_infinity(group, r);
  956. for (i = 0; i < num; i++) {
  957. if (!ec_point_is_compat(points[i], group)) {
  958. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  959. return 0;
  960. }
  961. }
  962. #ifndef FIPS_MODULE
  963. if (ctx == NULL)
  964. ctx = new_ctx = BN_CTX_secure_new();
  965. #endif
  966. if (ctx == NULL) {
  967. ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
  968. return 0;
  969. }
  970. if (group->meth->mul != NULL)
  971. ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);
  972. else
  973. /* use default */
  974. ret = ossl_ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
  975. #ifndef FIPS_MODULE
  976. BN_CTX_free(new_ctx);
  977. #endif
  978. return ret;
  979. }
  980. #endif
  981. int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
  982. const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
  983. {
  984. int ret = 0;
  985. size_t num;
  986. #ifndef FIPS_MODULE
  987. BN_CTX *new_ctx = NULL;
  988. #endif
  989. if (!ec_point_is_compat(r, group)
  990. || (point != NULL && !ec_point_is_compat(point, group))) {
  991. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  992. return 0;
  993. }
  994. if (g_scalar == NULL && p_scalar == NULL)
  995. return EC_POINT_set_to_infinity(group, r);
  996. #ifndef FIPS_MODULE
  997. if (ctx == NULL)
  998. ctx = new_ctx = BN_CTX_secure_new();
  999. #endif
  1000. if (ctx == NULL) {
  1001. ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
  1002. return 0;
  1003. }
  1004. num = (point != NULL && p_scalar != NULL) ? 1 : 0;
  1005. if (group->meth->mul != NULL)
  1006. ret = group->meth->mul(group, r, g_scalar, num, &point, &p_scalar, ctx);
  1007. else
  1008. /* use default */
  1009. ret = ossl_ec_wNAF_mul(group, r, g_scalar, num, &point, &p_scalar, ctx);
  1010. #ifndef FIPS_MODULE
  1011. BN_CTX_free(new_ctx);
  1012. #endif
  1013. return ret;
  1014. }
  1015. #ifndef OPENSSL_NO_DEPRECATED_3_0
  1016. int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
  1017. {
  1018. if (group->meth->mul == 0)
  1019. /* use default */
  1020. return ossl_ec_wNAF_precompute_mult(group, ctx);
  1021. if (group->meth->precompute_mult != 0)
  1022. return group->meth->precompute_mult(group, ctx);
  1023. else
  1024. return 1; /* nothing to do, so report success */
  1025. }
  1026. int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
  1027. {
  1028. if (group->meth->mul == 0)
  1029. /* use default */
  1030. return ossl_ec_wNAF_have_precompute_mult(group);
  1031. if (group->meth->have_precompute_mult != 0)
  1032. return group->meth->have_precompute_mult(group);
  1033. else
  1034. return 0; /* cannot tell whether precomputation has
  1035. * been performed */
  1036. }
  1037. #endif
  1038. /*
  1039. * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
  1040. * returns one on success. On error it returns zero.
  1041. */
  1042. static int ec_precompute_mont_data(EC_GROUP *group)
  1043. {
  1044. BN_CTX *ctx = BN_CTX_new_ex(group->libctx);
  1045. int ret = 0;
  1046. BN_MONT_CTX_free(group->mont_data);
  1047. group->mont_data = NULL;
  1048. if (ctx == NULL)
  1049. goto err;
  1050. group->mont_data = BN_MONT_CTX_new();
  1051. if (group->mont_data == NULL)
  1052. goto err;
  1053. if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
  1054. BN_MONT_CTX_free(group->mont_data);
  1055. group->mont_data = NULL;
  1056. goto err;
  1057. }
  1058. ret = 1;
  1059. err:
  1060. BN_CTX_free(ctx);
  1061. return ret;
  1062. }
  1063. #ifndef FIPS_MODULE
  1064. int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
  1065. {
  1066. return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
  1067. }
  1068. void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)
  1069. {
  1070. return CRYPTO_get_ex_data(&key->ex_data, idx);
  1071. }
  1072. #endif
  1073. int ossl_ec_group_simple_order_bits(const EC_GROUP *group)
  1074. {
  1075. if (group->order == NULL)
  1076. return 0;
  1077. return BN_num_bits(group->order);
  1078. }
  1079. static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r,
  1080. const BIGNUM *x, BN_CTX *ctx)
  1081. {
  1082. BIGNUM *e = NULL;
  1083. int ret = 0;
  1084. #ifndef FIPS_MODULE
  1085. BN_CTX *new_ctx = NULL;
  1086. #endif
  1087. if (group->mont_data == NULL)
  1088. return 0;
  1089. #ifndef FIPS_MODULE
  1090. if (ctx == NULL)
  1091. ctx = new_ctx = BN_CTX_secure_new();
  1092. #endif
  1093. if (ctx == NULL)
  1094. return 0;
  1095. BN_CTX_start(ctx);
  1096. if ((e = BN_CTX_get(ctx)) == NULL)
  1097. goto err;
  1098. /*-
  1099. * We want inverse in constant time, therefore we utilize the fact
  1100. * order must be prime and use Fermats Little Theorem instead.
  1101. */
  1102. if (!BN_set_word(e, 2))
  1103. goto err;
  1104. if (!BN_sub(e, group->order, e))
  1105. goto err;
  1106. /*-
  1107. * Exponent e is public.
  1108. * No need for scatter-gather or BN_FLG_CONSTTIME.
  1109. */
  1110. if (!BN_mod_exp_mont(r, x, e, group->order, ctx, group->mont_data))
  1111. goto err;
  1112. ret = 1;
  1113. err:
  1114. BN_CTX_end(ctx);
  1115. #ifndef FIPS_MODULE
  1116. BN_CTX_free(new_ctx);
  1117. #endif
  1118. return ret;
  1119. }
  1120. /*-
  1121. * Default behavior, if group->meth->field_inverse_mod_ord is NULL:
  1122. * - When group->order is even, this function returns an error.
  1123. * - When group->order is otherwise composite, the correctness
  1124. * of the output is not guaranteed.
  1125. * - When x is outside the range [1, group->order), the correctness
  1126. * of the output is not guaranteed.
  1127. * - Otherwise, this function returns the multiplicative inverse in the
  1128. * range [1, group->order).
  1129. *
  1130. * EC_METHODs must implement their own field_inverse_mod_ord for
  1131. * other functionality.
  1132. */
  1133. int ossl_ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,
  1134. const BIGNUM *x, BN_CTX *ctx)
  1135. {
  1136. if (group->meth->field_inverse_mod_ord != NULL)
  1137. return group->meth->field_inverse_mod_ord(group, res, x, ctx);
  1138. else
  1139. return ec_field_inverse_mod_ord(group, res, x, ctx);
  1140. }
  1141. /*-
  1142. * Coordinate blinding for EC_POINT.
  1143. *
  1144. * The underlying EC_METHOD can optionally implement this function:
  1145. * underlying implementations should return 0 on errors, or 1 on
  1146. * success.
  1147. *
  1148. * This wrapper returns 1 in case the underlying EC_METHOD does not
  1149. * support coordinate blinding.
  1150. */
  1151. int ossl_ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
  1152. BN_CTX *ctx)
  1153. {
  1154. if (group->meth->blind_coordinates == NULL)
  1155. return 1; /* ignore if not implemented */
  1156. return group->meth->blind_coordinates(group, p, ctx);
  1157. }
  1158. int EC_GROUP_get_basis_type(const EC_GROUP *group)
  1159. {
  1160. int i;
  1161. if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field)
  1162. /* everything else is currently not supported */
  1163. return 0;
  1164. /* Find the last non-zero element of group->poly[] */
  1165. for (i = 0;
  1166. i < (int)OSSL_NELEM(group->poly) && group->poly[i] != 0;
  1167. i++)
  1168. continue;
  1169. if (i == 4)
  1170. return NID_X9_62_ppBasis;
  1171. else if (i == 2)
  1172. return NID_X9_62_tpBasis;
  1173. else
  1174. /* everything else is currently not supported */
  1175. return 0;
  1176. }
  1177. #ifndef OPENSSL_NO_EC2M
  1178. int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k)
  1179. {
  1180. if (group == NULL)
  1181. return 0;
  1182. if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field
  1183. || !((group->poly[0] != 0) && (group->poly[1] != 0)
  1184. && (group->poly[2] == 0))) {
  1185. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1186. return 0;
  1187. }
  1188. if (k)
  1189. *k = group->poly[1];
  1190. return 1;
  1191. }
  1192. int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
  1193. unsigned int *k2, unsigned int *k3)
  1194. {
  1195. if (group == NULL)
  1196. return 0;
  1197. if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field
  1198. || !((group->poly[0] != 0) && (group->poly[1] != 0)
  1199. && (group->poly[2] != 0) && (group->poly[3] != 0)
  1200. && (group->poly[4] == 0))) {
  1201. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1202. return 0;
  1203. }
  1204. if (k1)
  1205. *k1 = group->poly[3];
  1206. if (k2)
  1207. *k2 = group->poly[2];
  1208. if (k3)
  1209. *k3 = group->poly[1];
  1210. return 1;
  1211. }
  1212. #endif
  1213. #ifndef FIPS_MODULE
  1214. /*
  1215. * Check if the explicit parameters group matches any built-in curves.
  1216. *
  1217. * We create a copy of the group just built, so that we can remove optional
  1218. * fields for the lookup: we do this to avoid the possibility that one of
  1219. * the optional parameters is used to force the library into using a less
  1220. * performant and less secure EC_METHOD instead of the specialized one.
  1221. * In any case, `seed` is not really used in any computation, while a
  1222. * cofactor different from the one in the built-in table is just
  1223. * mathematically wrong anyway and should not be used.
  1224. */
  1225. static EC_GROUP *ec_group_explicit_to_named(const EC_GROUP *group,
  1226. OSSL_LIB_CTX *libctx,
  1227. const char *propq,
  1228. BN_CTX *ctx)
  1229. {
  1230. EC_GROUP *ret_group = NULL, *dup = NULL;
  1231. int curve_name_nid;
  1232. const EC_POINT *point = EC_GROUP_get0_generator(group);
  1233. const BIGNUM *order = EC_GROUP_get0_order(group);
  1234. int no_seed = (EC_GROUP_get0_seed(group) == NULL);
  1235. if ((dup = EC_GROUP_dup(group)) == NULL
  1236. || EC_GROUP_set_seed(dup, NULL, 0) != 1
  1237. || !EC_GROUP_set_generator(dup, point, order, NULL))
  1238. goto err;
  1239. if ((curve_name_nid = ossl_ec_curve_nid_from_params(dup, ctx)) != NID_undef) {
  1240. /*
  1241. * The input explicit parameters successfully matched one of the
  1242. * built-in curves: often for built-in curves we have specialized
  1243. * methods with better performance and hardening.
  1244. *
  1245. * In this case we replace the `EC_GROUP` created through explicit
  1246. * parameters with one created from a named group.
  1247. */
  1248. # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  1249. /*
  1250. * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for
  1251. * the same curve, we prefer the SECP nid when matching explicit
  1252. * parameters as that is associated with a specialized EC_METHOD.
  1253. */
  1254. if (curve_name_nid == NID_wap_wsg_idm_ecid_wtls12)
  1255. curve_name_nid = NID_secp224r1;
  1256. # endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */
  1257. ret_group = EC_GROUP_new_by_curve_name_ex(libctx, propq, curve_name_nid);
  1258. if (ret_group == NULL)
  1259. goto err;
  1260. /*
  1261. * Set the flag so that EC_GROUPs created from explicit parameters are
  1262. * serialized using explicit parameters by default.
  1263. */
  1264. EC_GROUP_set_asn1_flag(ret_group, OPENSSL_EC_EXPLICIT_CURVE);
  1265. /*
  1266. * If the input params do not contain the optional seed field we make
  1267. * sure it is not added to the returned group.
  1268. *
  1269. * The seed field is not really used inside libcrypto anyway, and
  1270. * adding it to parsed explicit parameter keys would alter their DER
  1271. * encoding output (because of the extra field) which could impact
  1272. * applications fingerprinting keys by their DER encoding.
  1273. */
  1274. if (no_seed) {
  1275. if (EC_GROUP_set_seed(ret_group, NULL, 0) != 1)
  1276. goto err;
  1277. }
  1278. } else {
  1279. ret_group = (EC_GROUP *)group;
  1280. }
  1281. EC_GROUP_free(dup);
  1282. return ret_group;
  1283. err:
  1284. EC_GROUP_free(dup);
  1285. EC_GROUP_free(ret_group);
  1286. return NULL;
  1287. }
  1288. #endif /* FIPS_MODULE */
  1289. static EC_GROUP *group_new_from_name(const OSSL_PARAM *p,
  1290. OSSL_LIB_CTX *libctx, const char *propq)
  1291. {
  1292. int ok = 0, nid;
  1293. const char *curve_name = NULL;
  1294. switch (p->data_type) {
  1295. case OSSL_PARAM_UTF8_STRING:
  1296. /* The OSSL_PARAM functions have no support for this */
  1297. curve_name = p->data;
  1298. ok = (curve_name != NULL);
  1299. break;
  1300. case OSSL_PARAM_UTF8_PTR:
  1301. ok = OSSL_PARAM_get_utf8_ptr(p, &curve_name);
  1302. break;
  1303. }
  1304. if (ok) {
  1305. nid = ossl_ec_curve_name2nid(curve_name);
  1306. if (nid == NID_undef) {
  1307. ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
  1308. return NULL;
  1309. } else {
  1310. return EC_GROUP_new_by_curve_name_ex(libctx, propq, nid);
  1311. }
  1312. }
  1313. return NULL;
  1314. }
  1315. /* These parameters can be set directly into an EC_GROUP */
  1316. int ossl_ec_group_set_params(EC_GROUP *group, const OSSL_PARAM params[])
  1317. {
  1318. int encoding_flag = -1, format = -1;
  1319. const OSSL_PARAM *p;
  1320. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT);
  1321. if (p != NULL) {
  1322. if (!ossl_ec_pt_format_param2id(p, &format)) {
  1323. ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
  1324. return 0;
  1325. }
  1326. EC_GROUP_set_point_conversion_form(group, format);
  1327. }
  1328. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ENCODING);
  1329. if (p != NULL) {
  1330. if (!ossl_ec_encoding_param2id(p, &encoding_flag)) {
  1331. ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
  1332. return 0;
  1333. }
  1334. EC_GROUP_set_asn1_flag(group, encoding_flag);
  1335. }
  1336. /* Optional seed */
  1337. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED);
  1338. if (p != NULL) {
  1339. /* The seed is allowed to be NULL */
  1340. if (p->data_type != OSSL_PARAM_OCTET_STRING
  1341. || !EC_GROUP_set_seed(group, p->data, p->data_size)) {
  1342. ERR_raise(ERR_LIB_EC, EC_R_INVALID_SEED);
  1343. return 0;
  1344. }
  1345. }
  1346. return 1;
  1347. }
  1348. EC_GROUP *EC_GROUP_new_from_params(const OSSL_PARAM params[],
  1349. OSSL_LIB_CTX *libctx, const char *propq)
  1350. {
  1351. const OSSL_PARAM *ptmp;
  1352. EC_GROUP *group = NULL;
  1353. #ifndef FIPS_MODULE
  1354. const OSSL_PARAM *pa, *pb;
  1355. int ok = 0;
  1356. EC_GROUP *named_group = NULL;
  1357. BIGNUM *p = NULL, *a = NULL, *b = NULL, *order = NULL, *cofactor = NULL;
  1358. EC_POINT *point = NULL;
  1359. int field_bits = 0;
  1360. int is_prime_field = 1;
  1361. BN_CTX *bnctx = NULL;
  1362. const unsigned char *buf = NULL;
  1363. int encoding_flag = -1;
  1364. #endif
  1365. /* This is the simple named group case */
  1366. ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
  1367. if (ptmp != NULL) {
  1368. int decoded = 0;
  1369. if ((group = group_new_from_name(ptmp, libctx, propq)) == NULL)
  1370. return NULL;
  1371. if (!ossl_ec_group_set_params(group, params)) {
  1372. EC_GROUP_free(group);
  1373. return NULL;
  1374. }
  1375. ptmp = OSSL_PARAM_locate_const(params,
  1376. OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS);
  1377. if (ptmp != NULL && !OSSL_PARAM_get_int(ptmp, &decoded)) {
  1378. ERR_raise(ERR_LIB_EC, EC_R_WRONG_CURVE_PARAMETERS);
  1379. EC_GROUP_free(group);
  1380. return NULL;
  1381. }
  1382. group->decoded_from_explicit_params = decoded > 0;
  1383. return group;
  1384. }
  1385. #ifdef FIPS_MODULE
  1386. ERR_raise(ERR_LIB_EC, EC_R_EXPLICIT_PARAMS_NOT_SUPPORTED);
  1387. return NULL;
  1388. #else
  1389. /* If it gets here then we are trying explicit parameters */
  1390. bnctx = BN_CTX_new_ex(libctx);
  1391. if (bnctx == NULL) {
  1392. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  1393. return 0;
  1394. }
  1395. BN_CTX_start(bnctx);
  1396. p = BN_CTX_get(bnctx);
  1397. a = BN_CTX_get(bnctx);
  1398. b = BN_CTX_get(bnctx);
  1399. order = BN_CTX_get(bnctx);
  1400. if (order == NULL) {
  1401. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  1402. goto err;
  1403. }
  1404. ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE);
  1405. if (ptmp == NULL || ptmp->data_type != OSSL_PARAM_UTF8_STRING) {
  1406. ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
  1407. goto err;
  1408. }
  1409. if (OPENSSL_strcasecmp(ptmp->data, SN_X9_62_prime_field) == 0) {
  1410. is_prime_field = 1;
  1411. } else if (OPENSSL_strcasecmp(ptmp->data,
  1412. SN_X9_62_characteristic_two_field) == 0) {
  1413. is_prime_field = 0;
  1414. } else {
  1415. /* Invalid field */
  1416. ERR_raise(ERR_LIB_EC, EC_R_UNSUPPORTED_FIELD);
  1417. goto err;
  1418. }
  1419. pa = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_A);
  1420. if (!OSSL_PARAM_get_BN(pa, &a)) {
  1421. ERR_raise(ERR_LIB_EC, EC_R_INVALID_A);
  1422. goto err;
  1423. }
  1424. pb = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_B);
  1425. if (!OSSL_PARAM_get_BN(pb, &b)) {
  1426. ERR_raise(ERR_LIB_EC, EC_R_INVALID_B);
  1427. goto err;
  1428. }
  1429. /* extract the prime number or irreducible polynomial */
  1430. ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_P);
  1431. if (!OSSL_PARAM_get_BN(ptmp, &p)) {
  1432. ERR_raise(ERR_LIB_EC, EC_R_INVALID_P);
  1433. goto err;
  1434. }
  1435. if (is_prime_field) {
  1436. if (BN_is_negative(p) || BN_is_zero(p)) {
  1437. ERR_raise(ERR_LIB_EC, EC_R_INVALID_P);
  1438. goto err;
  1439. }
  1440. field_bits = BN_num_bits(p);
  1441. if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
  1442. ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE);
  1443. goto err;
  1444. }
  1445. /* create the EC_GROUP structure */
  1446. group = EC_GROUP_new_curve_GFp(p, a, b, bnctx);
  1447. } else {
  1448. # ifdef OPENSSL_NO_EC2M
  1449. ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
  1450. goto err;
  1451. # else
  1452. /* create the EC_GROUP structure */
  1453. group = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
  1454. if (group != NULL) {
  1455. field_bits = EC_GROUP_get_degree(group);
  1456. if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
  1457. ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE);
  1458. goto err;
  1459. }
  1460. }
  1461. # endif /* OPENSSL_NO_EC2M */
  1462. }
  1463. if (group == NULL) {
  1464. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  1465. goto err;
  1466. }
  1467. /* Optional seed */
  1468. ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED);
  1469. if (ptmp != NULL) {
  1470. if (ptmp->data_type != OSSL_PARAM_OCTET_STRING) {
  1471. ERR_raise(ERR_LIB_EC, EC_R_INVALID_SEED);
  1472. goto err;
  1473. }
  1474. if (!EC_GROUP_set_seed(group, ptmp->data, ptmp->data_size))
  1475. goto err;
  1476. }
  1477. /* generator base point */
  1478. ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GENERATOR);
  1479. if (ptmp == NULL
  1480. || ptmp->data_type != OSSL_PARAM_OCTET_STRING) {
  1481. ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
  1482. goto err;
  1483. }
  1484. buf = (const unsigned char *)(ptmp->data);
  1485. if ((point = EC_POINT_new(group)) == NULL)
  1486. goto err;
  1487. EC_GROUP_set_point_conversion_form(group,
  1488. (point_conversion_form_t)buf[0] & ~0x01);
  1489. if (!EC_POINT_oct2point(group, point, buf, ptmp->data_size, bnctx)) {
  1490. ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
  1491. goto err;
  1492. }
  1493. /* order */
  1494. ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ORDER);
  1495. if (!OSSL_PARAM_get_BN(ptmp, &order)
  1496. || (BN_is_negative(order) || BN_is_zero(order))
  1497. || (BN_num_bits(order) > (int)field_bits + 1)) { /* Hasse bound */
  1498. ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
  1499. goto err;
  1500. }
  1501. /* Optional cofactor */
  1502. ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_COFACTOR);
  1503. if (ptmp != NULL) {
  1504. cofactor = BN_CTX_get(bnctx);
  1505. if (cofactor == NULL || !OSSL_PARAM_get_BN(ptmp, &cofactor)) {
  1506. ERR_raise(ERR_LIB_EC, EC_R_INVALID_COFACTOR);
  1507. goto err;
  1508. }
  1509. }
  1510. /* set the generator, order and cofactor (if present) */
  1511. if (!EC_GROUP_set_generator(group, point, order, cofactor)) {
  1512. ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
  1513. goto err;
  1514. }
  1515. named_group = ec_group_explicit_to_named(group, libctx, propq, bnctx);
  1516. if (named_group == NULL) {
  1517. ERR_raise(ERR_LIB_EC, EC_R_INVALID_NAMED_GROUP_CONVERSION);
  1518. goto err;
  1519. }
  1520. if (named_group == group) {
  1521. /*
  1522. * If we did not find a named group then the encoding should be explicit
  1523. * if it was specified
  1524. */
  1525. ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ENCODING);
  1526. if (ptmp != NULL
  1527. && !ossl_ec_encoding_param2id(ptmp, &encoding_flag)) {
  1528. ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
  1529. goto err;
  1530. }
  1531. if (encoding_flag == OPENSSL_EC_NAMED_CURVE) {
  1532. ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
  1533. goto err;
  1534. }
  1535. EC_GROUP_set_asn1_flag(group, OPENSSL_EC_EXPLICIT_CURVE);
  1536. } else {
  1537. EC_GROUP_free(group);
  1538. group = named_group;
  1539. }
  1540. /* We've imported the group from explicit parameters, set it so. */
  1541. group->decoded_from_explicit_params = 1;
  1542. ok = 1;
  1543. err:
  1544. if (!ok) {
  1545. EC_GROUP_free(group);
  1546. group = NULL;
  1547. }
  1548. EC_POINT_free(point);
  1549. BN_CTX_end(bnctx);
  1550. BN_CTX_free(bnctx);
  1551. return group;
  1552. #endif /* FIPS_MODULE */
  1553. }
  1554. OSSL_PARAM *EC_GROUP_to_params(const EC_GROUP *group, OSSL_LIB_CTX *libctx,
  1555. const char *propq, BN_CTX *bnctx)
  1556. {
  1557. OSSL_PARAM_BLD *tmpl = NULL;
  1558. BN_CTX *new_bnctx = NULL;
  1559. unsigned char *gen_buf = NULL;
  1560. OSSL_PARAM *params = NULL;
  1561. if (group == NULL)
  1562. goto err;
  1563. tmpl = OSSL_PARAM_BLD_new();
  1564. if (tmpl == NULL)
  1565. goto err;
  1566. if (bnctx == NULL)
  1567. bnctx = new_bnctx = BN_CTX_new_ex(libctx);
  1568. if (bnctx == NULL)
  1569. goto err;
  1570. BN_CTX_start(bnctx);
  1571. if (!ossl_ec_group_todata(
  1572. group, tmpl, NULL, libctx, propq, bnctx, &gen_buf))
  1573. goto err;
  1574. params = OSSL_PARAM_BLD_to_param(tmpl);
  1575. err:
  1576. OSSL_PARAM_BLD_free(tmpl);
  1577. OPENSSL_free(gen_buf);
  1578. BN_CTX_end(bnctx);
  1579. BN_CTX_free(new_bnctx);
  1580. return params;
  1581. }