1
0

ec_lib.c 50 KB

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