ec_lib.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799
  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. #ifdef OPENSSL_PEDANTIC_ZEROIZATION
  654. EC_POINT_clear_free(point);
  655. #else
  656. if (point->meth->point_finish != 0)
  657. point->meth->point_finish(point);
  658. OPENSSL_free(point);
  659. #endif
  660. }
  661. void EC_POINT_clear_free(EC_POINT *point)
  662. {
  663. if (point == NULL)
  664. return;
  665. if (point->meth->point_clear_finish != 0)
  666. point->meth->point_clear_finish(point);
  667. else if (point->meth->point_finish != 0)
  668. point->meth->point_finish(point);
  669. OPENSSL_clear_free(point, sizeof(*point));
  670. }
  671. int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
  672. {
  673. if (dest->meth->point_copy == 0) {
  674. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  675. return 0;
  676. }
  677. if (dest->meth != src->meth
  678. || (dest->curve_name != src->curve_name
  679. && dest->curve_name != 0
  680. && src->curve_name != 0)) {
  681. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  682. return 0;
  683. }
  684. if (dest == src)
  685. return 1;
  686. return dest->meth->point_copy(dest, src);
  687. }
  688. EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
  689. {
  690. EC_POINT *t;
  691. int r;
  692. if (a == NULL)
  693. return NULL;
  694. t = EC_POINT_new(group);
  695. if (t == NULL)
  696. return NULL;
  697. r = EC_POINT_copy(t, a);
  698. if (!r) {
  699. EC_POINT_free(t);
  700. return NULL;
  701. }
  702. return t;
  703. }
  704. #ifndef OPENSSL_NO_DEPRECATED_3_0
  705. const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
  706. {
  707. return point->meth;
  708. }
  709. #endif
  710. int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
  711. {
  712. if (group->meth->point_set_to_infinity == 0) {
  713. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  714. return 0;
  715. }
  716. if (group->meth != point->meth) {
  717. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  718. return 0;
  719. }
  720. return group->meth->point_set_to_infinity(group, point);
  721. }
  722. #ifndef OPENSSL_NO_DEPRECATED_3_0
  723. int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
  724. EC_POINT *point, const BIGNUM *x,
  725. const BIGNUM *y, const BIGNUM *z,
  726. BN_CTX *ctx)
  727. {
  728. if (group->meth->field_type != NID_X9_62_prime_field) {
  729. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  730. return 0;
  731. }
  732. if (!ec_point_is_compat(point, group)) {
  733. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  734. return 0;
  735. }
  736. return ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(group, point,
  737. x, y, z, ctx);
  738. }
  739. int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
  740. const EC_POINT *point, BIGNUM *x,
  741. BIGNUM *y, BIGNUM *z,
  742. BN_CTX *ctx)
  743. {
  744. if (group->meth->field_type != NID_X9_62_prime_field) {
  745. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  746. return 0;
  747. }
  748. if (!ec_point_is_compat(point, group)) {
  749. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  750. return 0;
  751. }
  752. return ossl_ec_GFp_simple_get_Jprojective_coordinates_GFp(group, point,
  753. x, y, z, ctx);
  754. }
  755. #endif
  756. int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
  757. const BIGNUM *x, const BIGNUM *y,
  758. BN_CTX *ctx)
  759. {
  760. if (group->meth->point_set_affine_coordinates == NULL) {
  761. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  762. return 0;
  763. }
  764. if (!ec_point_is_compat(point, group)) {
  765. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  766. return 0;
  767. }
  768. if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
  769. return 0;
  770. if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
  771. ERR_raise(ERR_LIB_EC, EC_R_POINT_IS_NOT_ON_CURVE);
  772. return 0;
  773. }
  774. return 1;
  775. }
  776. #ifndef OPENSSL_NO_DEPRECATED_3_0
  777. int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
  778. EC_POINT *point, const BIGNUM *x,
  779. const BIGNUM *y, BN_CTX *ctx)
  780. {
  781. return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
  782. }
  783. # ifndef OPENSSL_NO_EC2M
  784. int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
  785. EC_POINT *point, const BIGNUM *x,
  786. const BIGNUM *y, BN_CTX *ctx)
  787. {
  788. return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
  789. }
  790. # endif
  791. #endif
  792. int EC_POINT_get_affine_coordinates(const EC_GROUP *group,
  793. const EC_POINT *point, BIGNUM *x, BIGNUM *y,
  794. BN_CTX *ctx)
  795. {
  796. if (group->meth->point_get_affine_coordinates == NULL) {
  797. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  798. return 0;
  799. }
  800. if (!ec_point_is_compat(point, group)) {
  801. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  802. return 0;
  803. }
  804. if (EC_POINT_is_at_infinity(group, point)) {
  805. ERR_raise(ERR_LIB_EC, EC_R_POINT_AT_INFINITY);
  806. return 0;
  807. }
  808. return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
  809. }
  810. #ifndef OPENSSL_NO_DEPRECATED_3_0
  811. int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
  812. const EC_POINT *point, BIGNUM *x,
  813. BIGNUM *y, BN_CTX *ctx)
  814. {
  815. return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
  816. }
  817. # ifndef OPENSSL_NO_EC2M
  818. int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
  819. const EC_POINT *point, BIGNUM *x,
  820. BIGNUM *y, BN_CTX *ctx)
  821. {
  822. return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
  823. }
  824. # endif
  825. #endif
  826. int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  827. const EC_POINT *b, BN_CTX *ctx)
  828. {
  829. if (group->meth->add == 0) {
  830. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  831. return 0;
  832. }
  833. if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)
  834. || !ec_point_is_compat(b, group)) {
  835. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  836. return 0;
  837. }
  838. return group->meth->add(group, r, a, b, ctx);
  839. }
  840. int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  841. BN_CTX *ctx)
  842. {
  843. if (group->meth->dbl == 0) {
  844. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  845. return 0;
  846. }
  847. if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) {
  848. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  849. return 0;
  850. }
  851. return group->meth->dbl(group, r, a, ctx);
  852. }
  853. int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
  854. {
  855. if (group->meth->invert == 0) {
  856. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  857. return 0;
  858. }
  859. if (!ec_point_is_compat(a, group)) {
  860. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  861. return 0;
  862. }
  863. return group->meth->invert(group, a, ctx);
  864. }
  865. int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
  866. {
  867. if (group->meth->is_at_infinity == 0) {
  868. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  869. return 0;
  870. }
  871. if (!ec_point_is_compat(point, group)) {
  872. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  873. return 0;
  874. }
  875. return group->meth->is_at_infinity(group, point);
  876. }
  877. /*
  878. * Check whether an EC_POINT is on the curve or not. Note that the return
  879. * value for this function should NOT be treated as a boolean. Return values:
  880. * 1: The point is on the curve
  881. * 0: The point is not on the curve
  882. * -1: An error occurred
  883. */
  884. int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
  885. BN_CTX *ctx)
  886. {
  887. if (group->meth->is_on_curve == 0) {
  888. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  889. return 0;
  890. }
  891. if (!ec_point_is_compat(point, group)) {
  892. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  893. return 0;
  894. }
  895. return group->meth->is_on_curve(group, point, ctx);
  896. }
  897. int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
  898. BN_CTX *ctx)
  899. {
  900. if (group->meth->point_cmp == 0) {
  901. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  902. return -1;
  903. }
  904. if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) {
  905. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  906. return -1;
  907. }
  908. return group->meth->point_cmp(group, a, b, ctx);
  909. }
  910. #ifndef OPENSSL_NO_DEPRECATED_3_0
  911. int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
  912. {
  913. if (group->meth->make_affine == 0) {
  914. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  915. return 0;
  916. }
  917. if (!ec_point_is_compat(point, group)) {
  918. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  919. return 0;
  920. }
  921. return group->meth->make_affine(group, point, ctx);
  922. }
  923. int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
  924. EC_POINT *points[], BN_CTX *ctx)
  925. {
  926. size_t i;
  927. if (group->meth->points_make_affine == 0) {
  928. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  929. return 0;
  930. }
  931. for (i = 0; i < num; i++) {
  932. if (!ec_point_is_compat(points[i], group)) {
  933. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  934. return 0;
  935. }
  936. }
  937. return group->meth->points_make_affine(group, num, points, ctx);
  938. }
  939. #endif
  940. /*
  941. * Functions for point multiplication. If group->meth->mul is 0, we use the
  942. * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
  943. * methods.
  944. */
  945. #ifndef OPENSSL_NO_DEPRECATED_3_0
  946. int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
  947. size_t num, const EC_POINT *points[],
  948. const BIGNUM *scalars[], BN_CTX *ctx)
  949. {
  950. int ret = 0;
  951. size_t i = 0;
  952. #ifndef FIPS_MODULE
  953. BN_CTX *new_ctx = NULL;
  954. #endif
  955. if (!ec_point_is_compat(r, group)) {
  956. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  957. return 0;
  958. }
  959. if (scalar == NULL && num == 0)
  960. return EC_POINT_set_to_infinity(group, r);
  961. for (i = 0; i < num; i++) {
  962. if (!ec_point_is_compat(points[i], group)) {
  963. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  964. return 0;
  965. }
  966. }
  967. #ifndef FIPS_MODULE
  968. if (ctx == NULL)
  969. ctx = new_ctx = BN_CTX_secure_new();
  970. #endif
  971. if (ctx == NULL) {
  972. ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
  973. return 0;
  974. }
  975. if (group->meth->mul != NULL)
  976. ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);
  977. else
  978. /* use default */
  979. ret = ossl_ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
  980. #ifndef FIPS_MODULE
  981. BN_CTX_free(new_ctx);
  982. #endif
  983. return ret;
  984. }
  985. #endif
  986. int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
  987. const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
  988. {
  989. int ret = 0;
  990. size_t num;
  991. #ifndef FIPS_MODULE
  992. BN_CTX *new_ctx = NULL;
  993. #endif
  994. if (!ec_point_is_compat(r, group)
  995. || (point != NULL && !ec_point_is_compat(point, group))) {
  996. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  997. return 0;
  998. }
  999. if (g_scalar == NULL && p_scalar == NULL)
  1000. return EC_POINT_set_to_infinity(group, r);
  1001. #ifndef FIPS_MODULE
  1002. if (ctx == NULL)
  1003. ctx = new_ctx = BN_CTX_secure_new();
  1004. #endif
  1005. if (ctx == NULL) {
  1006. ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
  1007. return 0;
  1008. }
  1009. num = (point != NULL && p_scalar != NULL) ? 1 : 0;
  1010. if (group->meth->mul != NULL)
  1011. ret = group->meth->mul(group, r, g_scalar, num, &point, &p_scalar, ctx);
  1012. else
  1013. /* use default */
  1014. ret = ossl_ec_wNAF_mul(group, r, g_scalar, num, &point, &p_scalar, ctx);
  1015. #ifndef FIPS_MODULE
  1016. BN_CTX_free(new_ctx);
  1017. #endif
  1018. return ret;
  1019. }
  1020. #ifndef OPENSSL_NO_DEPRECATED_3_0
  1021. int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
  1022. {
  1023. if (group->meth->mul == 0)
  1024. /* use default */
  1025. return ossl_ec_wNAF_precompute_mult(group, ctx);
  1026. if (group->meth->precompute_mult != 0)
  1027. return group->meth->precompute_mult(group, ctx);
  1028. else
  1029. return 1; /* nothing to do, so report success */
  1030. }
  1031. int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
  1032. {
  1033. if (group->meth->mul == 0)
  1034. /* use default */
  1035. return ossl_ec_wNAF_have_precompute_mult(group);
  1036. if (group->meth->have_precompute_mult != 0)
  1037. return group->meth->have_precompute_mult(group);
  1038. else
  1039. return 0; /* cannot tell whether precomputation has
  1040. * been performed */
  1041. }
  1042. #endif
  1043. /*
  1044. * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
  1045. * returns one on success. On error it returns zero.
  1046. */
  1047. static int ec_precompute_mont_data(EC_GROUP *group)
  1048. {
  1049. BN_CTX *ctx = BN_CTX_new_ex(group->libctx);
  1050. int ret = 0;
  1051. BN_MONT_CTX_free(group->mont_data);
  1052. group->mont_data = NULL;
  1053. if (ctx == NULL)
  1054. goto err;
  1055. group->mont_data = BN_MONT_CTX_new();
  1056. if (group->mont_data == NULL)
  1057. goto err;
  1058. if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
  1059. BN_MONT_CTX_free(group->mont_data);
  1060. group->mont_data = NULL;
  1061. goto err;
  1062. }
  1063. ret = 1;
  1064. err:
  1065. BN_CTX_free(ctx);
  1066. return ret;
  1067. }
  1068. #ifndef FIPS_MODULE
  1069. int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
  1070. {
  1071. return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
  1072. }
  1073. void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)
  1074. {
  1075. return CRYPTO_get_ex_data(&key->ex_data, idx);
  1076. }
  1077. #endif
  1078. int ossl_ec_group_simple_order_bits(const EC_GROUP *group)
  1079. {
  1080. if (group->order == NULL)
  1081. return 0;
  1082. return BN_num_bits(group->order);
  1083. }
  1084. static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r,
  1085. const BIGNUM *x, BN_CTX *ctx)
  1086. {
  1087. BIGNUM *e = NULL;
  1088. int ret = 0;
  1089. #ifndef FIPS_MODULE
  1090. BN_CTX *new_ctx = NULL;
  1091. #endif
  1092. if (group->mont_data == NULL)
  1093. return 0;
  1094. #ifndef FIPS_MODULE
  1095. if (ctx == NULL)
  1096. ctx = new_ctx = BN_CTX_secure_new();
  1097. #endif
  1098. if (ctx == NULL)
  1099. return 0;
  1100. BN_CTX_start(ctx);
  1101. if ((e = BN_CTX_get(ctx)) == NULL)
  1102. goto err;
  1103. /*-
  1104. * We want inverse in constant time, therefore we utilize the fact
  1105. * order must be prime and use Fermats Little Theorem instead.
  1106. */
  1107. if (!BN_set_word(e, 2))
  1108. goto err;
  1109. if (!BN_sub(e, group->order, e))
  1110. goto err;
  1111. /*-
  1112. * Although the exponent is public we want the result to be
  1113. * fixed top.
  1114. */
  1115. if (!bn_mod_exp_mont_fixed_top(r, x, e, group->order, ctx, group->mont_data))
  1116. goto err;
  1117. ret = 1;
  1118. err:
  1119. BN_CTX_end(ctx);
  1120. #ifndef FIPS_MODULE
  1121. BN_CTX_free(new_ctx);
  1122. #endif
  1123. return ret;
  1124. }
  1125. /*-
  1126. * Default behavior, if group->meth->field_inverse_mod_ord is NULL:
  1127. * - When group->order is even, this function returns an error.
  1128. * - When group->order is otherwise composite, the correctness
  1129. * of the output is not guaranteed.
  1130. * - When x is outside the range [1, group->order), the correctness
  1131. * of the output is not guaranteed.
  1132. * - Otherwise, this function returns the multiplicative inverse in the
  1133. * range [1, group->order).
  1134. *
  1135. * EC_METHODs must implement their own field_inverse_mod_ord for
  1136. * other functionality.
  1137. */
  1138. int ossl_ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,
  1139. const BIGNUM *x, BN_CTX *ctx)
  1140. {
  1141. if (group->meth->field_inverse_mod_ord != NULL)
  1142. return group->meth->field_inverse_mod_ord(group, res, x, ctx);
  1143. else
  1144. return ec_field_inverse_mod_ord(group, res, x, ctx);
  1145. }
  1146. /*-
  1147. * Coordinate blinding for EC_POINT.
  1148. *
  1149. * The underlying EC_METHOD can optionally implement this function:
  1150. * underlying implementations should return 0 on errors, or 1 on
  1151. * success.
  1152. *
  1153. * This wrapper returns 1 in case the underlying EC_METHOD does not
  1154. * support coordinate blinding.
  1155. */
  1156. int ossl_ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
  1157. BN_CTX *ctx)
  1158. {
  1159. if (group->meth->blind_coordinates == NULL)
  1160. return 1; /* ignore if not implemented */
  1161. return group->meth->blind_coordinates(group, p, ctx);
  1162. }
  1163. int EC_GROUP_get_basis_type(const EC_GROUP *group)
  1164. {
  1165. int i;
  1166. if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field)
  1167. /* everything else is currently not supported */
  1168. return 0;
  1169. /* Find the last non-zero element of group->poly[] */
  1170. for (i = 0;
  1171. i < (int)OSSL_NELEM(group->poly) && group->poly[i] != 0;
  1172. i++)
  1173. continue;
  1174. if (i == 4)
  1175. return NID_X9_62_ppBasis;
  1176. else if (i == 2)
  1177. return NID_X9_62_tpBasis;
  1178. else
  1179. /* everything else is currently not supported */
  1180. return 0;
  1181. }
  1182. #ifndef OPENSSL_NO_EC2M
  1183. int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k)
  1184. {
  1185. if (group == NULL)
  1186. return 0;
  1187. if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field
  1188. || !((group->poly[0] != 0) && (group->poly[1] != 0)
  1189. && (group->poly[2] == 0))) {
  1190. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1191. return 0;
  1192. }
  1193. if (k)
  1194. *k = group->poly[1];
  1195. return 1;
  1196. }
  1197. int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
  1198. unsigned int *k2, unsigned int *k3)
  1199. {
  1200. if (group == NULL)
  1201. return 0;
  1202. if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field
  1203. || !((group->poly[0] != 0) && (group->poly[1] != 0)
  1204. && (group->poly[2] != 0) && (group->poly[3] != 0)
  1205. && (group->poly[4] == 0))) {
  1206. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1207. return 0;
  1208. }
  1209. if (k1)
  1210. *k1 = group->poly[3];
  1211. if (k2)
  1212. *k2 = group->poly[2];
  1213. if (k3)
  1214. *k3 = group->poly[1];
  1215. return 1;
  1216. }
  1217. #endif
  1218. #ifndef FIPS_MODULE
  1219. /*
  1220. * Check if the explicit parameters group matches any built-in curves.
  1221. *
  1222. * We create a copy of the group just built, so that we can remove optional
  1223. * fields for the lookup: we do this to avoid the possibility that one of
  1224. * the optional parameters is used to force the library into using a less
  1225. * performant and less secure EC_METHOD instead of the specialized one.
  1226. * In any case, `seed` is not really used in any computation, while a
  1227. * cofactor different from the one in the built-in table is just
  1228. * mathematically wrong anyway and should not be used.
  1229. */
  1230. static EC_GROUP *ec_group_explicit_to_named(const EC_GROUP *group,
  1231. OSSL_LIB_CTX *libctx,
  1232. const char *propq,
  1233. BN_CTX *ctx)
  1234. {
  1235. EC_GROUP *ret_group = NULL, *dup = NULL;
  1236. int curve_name_nid;
  1237. const EC_POINT *point = EC_GROUP_get0_generator(group);
  1238. const BIGNUM *order = EC_GROUP_get0_order(group);
  1239. int no_seed = (EC_GROUP_get0_seed(group) == NULL);
  1240. if ((dup = EC_GROUP_dup(group)) == NULL
  1241. || EC_GROUP_set_seed(dup, NULL, 0) != 1
  1242. || !EC_GROUP_set_generator(dup, point, order, NULL))
  1243. goto err;
  1244. if ((curve_name_nid = ossl_ec_curve_nid_from_params(dup, ctx)) != NID_undef) {
  1245. /*
  1246. * The input explicit parameters successfully matched one of the
  1247. * built-in curves: often for built-in curves we have specialized
  1248. * methods with better performance and hardening.
  1249. *
  1250. * In this case we replace the `EC_GROUP` created through explicit
  1251. * parameters with one created from a named group.
  1252. */
  1253. # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  1254. /*
  1255. * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for
  1256. * the same curve, we prefer the SECP nid when matching explicit
  1257. * parameters as that is associated with a specialized EC_METHOD.
  1258. */
  1259. if (curve_name_nid == NID_wap_wsg_idm_ecid_wtls12)
  1260. curve_name_nid = NID_secp224r1;
  1261. # endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */
  1262. ret_group = EC_GROUP_new_by_curve_name_ex(libctx, propq, curve_name_nid);
  1263. if (ret_group == NULL)
  1264. goto err;
  1265. /*
  1266. * Set the flag so that EC_GROUPs created from explicit parameters are
  1267. * serialized using explicit parameters by default.
  1268. */
  1269. EC_GROUP_set_asn1_flag(ret_group, OPENSSL_EC_EXPLICIT_CURVE);
  1270. /*
  1271. * If the input params do not contain the optional seed field we make
  1272. * sure it is not added to the returned group.
  1273. *
  1274. * The seed field is not really used inside libcrypto anyway, and
  1275. * adding it to parsed explicit parameter keys would alter their DER
  1276. * encoding output (because of the extra field) which could impact
  1277. * applications fingerprinting keys by their DER encoding.
  1278. */
  1279. if (no_seed) {
  1280. if (EC_GROUP_set_seed(ret_group, NULL, 0) != 1)
  1281. goto err;
  1282. }
  1283. } else {
  1284. ret_group = (EC_GROUP *)group;
  1285. }
  1286. EC_GROUP_free(dup);
  1287. return ret_group;
  1288. err:
  1289. EC_GROUP_free(dup);
  1290. EC_GROUP_free(ret_group);
  1291. return NULL;
  1292. }
  1293. #endif /* FIPS_MODULE */
  1294. static EC_GROUP *group_new_from_name(const OSSL_PARAM *p,
  1295. OSSL_LIB_CTX *libctx, const char *propq)
  1296. {
  1297. int ok = 0, nid;
  1298. const char *curve_name = NULL;
  1299. switch (p->data_type) {
  1300. case OSSL_PARAM_UTF8_STRING:
  1301. /* The OSSL_PARAM functions have no support for this */
  1302. curve_name = p->data;
  1303. ok = (curve_name != NULL);
  1304. break;
  1305. case OSSL_PARAM_UTF8_PTR:
  1306. ok = OSSL_PARAM_get_utf8_ptr(p, &curve_name);
  1307. break;
  1308. }
  1309. if (ok) {
  1310. nid = ossl_ec_curve_name2nid(curve_name);
  1311. if (nid == NID_undef) {
  1312. ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
  1313. return NULL;
  1314. } else {
  1315. return EC_GROUP_new_by_curve_name_ex(libctx, propq, nid);
  1316. }
  1317. }
  1318. return NULL;
  1319. }
  1320. /* These parameters can be set directly into an EC_GROUP */
  1321. int ossl_ec_group_set_params(EC_GROUP *group, const OSSL_PARAM params[])
  1322. {
  1323. int encoding_flag = -1, format = -1;
  1324. const OSSL_PARAM *p;
  1325. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT);
  1326. if (p != NULL) {
  1327. if (!ossl_ec_pt_format_param2id(p, &format)) {
  1328. ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
  1329. return 0;
  1330. }
  1331. EC_GROUP_set_point_conversion_form(group, format);
  1332. }
  1333. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ENCODING);
  1334. if (p != NULL) {
  1335. if (!ossl_ec_encoding_param2id(p, &encoding_flag)) {
  1336. ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
  1337. return 0;
  1338. }
  1339. EC_GROUP_set_asn1_flag(group, encoding_flag);
  1340. }
  1341. /* Optional seed */
  1342. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED);
  1343. if (p != NULL) {
  1344. /* The seed is allowed to be NULL */
  1345. if (p->data_type != OSSL_PARAM_OCTET_STRING
  1346. || !EC_GROUP_set_seed(group, p->data, p->data_size)) {
  1347. ERR_raise(ERR_LIB_EC, EC_R_INVALID_SEED);
  1348. return 0;
  1349. }
  1350. }
  1351. return 1;
  1352. }
  1353. EC_GROUP *EC_GROUP_new_from_params(const OSSL_PARAM params[],
  1354. OSSL_LIB_CTX *libctx, const char *propq)
  1355. {
  1356. const OSSL_PARAM *ptmp;
  1357. EC_GROUP *group = NULL;
  1358. #ifndef FIPS_MODULE
  1359. const OSSL_PARAM *pa, *pb;
  1360. int ok = 0;
  1361. EC_GROUP *named_group = NULL;
  1362. BIGNUM *p = NULL, *a = NULL, *b = NULL, *order = NULL, *cofactor = NULL;
  1363. EC_POINT *point = NULL;
  1364. int field_bits = 0;
  1365. int is_prime_field = 1;
  1366. BN_CTX *bnctx = NULL;
  1367. const unsigned char *buf = NULL;
  1368. int encoding_flag = -1;
  1369. #endif
  1370. /* This is the simple named group case */
  1371. ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
  1372. if (ptmp != NULL) {
  1373. int decoded = 0;
  1374. if ((group = group_new_from_name(ptmp, libctx, propq)) == NULL)
  1375. return NULL;
  1376. if (!ossl_ec_group_set_params(group, params)) {
  1377. EC_GROUP_free(group);
  1378. return NULL;
  1379. }
  1380. ptmp = OSSL_PARAM_locate_const(params,
  1381. OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS);
  1382. if (ptmp != NULL && !OSSL_PARAM_get_int(ptmp, &decoded)) {
  1383. ERR_raise(ERR_LIB_EC, EC_R_WRONG_CURVE_PARAMETERS);
  1384. EC_GROUP_free(group);
  1385. return NULL;
  1386. }
  1387. group->decoded_from_explicit_params = decoded > 0;
  1388. return group;
  1389. }
  1390. #ifdef FIPS_MODULE
  1391. ERR_raise(ERR_LIB_EC, EC_R_EXPLICIT_PARAMS_NOT_SUPPORTED);
  1392. return NULL;
  1393. #else
  1394. /* If it gets here then we are trying explicit parameters */
  1395. bnctx = BN_CTX_new_ex(libctx);
  1396. if (bnctx == NULL) {
  1397. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  1398. return 0;
  1399. }
  1400. BN_CTX_start(bnctx);
  1401. p = BN_CTX_get(bnctx);
  1402. a = BN_CTX_get(bnctx);
  1403. b = BN_CTX_get(bnctx);
  1404. order = BN_CTX_get(bnctx);
  1405. if (order == NULL) {
  1406. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  1407. goto err;
  1408. }
  1409. ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE);
  1410. if (ptmp == NULL || ptmp->data_type != OSSL_PARAM_UTF8_STRING) {
  1411. ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
  1412. goto err;
  1413. }
  1414. if (OPENSSL_strcasecmp(ptmp->data, SN_X9_62_prime_field) == 0) {
  1415. is_prime_field = 1;
  1416. } else if (OPENSSL_strcasecmp(ptmp->data,
  1417. SN_X9_62_characteristic_two_field) == 0) {
  1418. is_prime_field = 0;
  1419. } else {
  1420. /* Invalid field */
  1421. ERR_raise(ERR_LIB_EC, EC_R_UNSUPPORTED_FIELD);
  1422. goto err;
  1423. }
  1424. pa = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_A);
  1425. if (!OSSL_PARAM_get_BN(pa, &a)) {
  1426. ERR_raise(ERR_LIB_EC, EC_R_INVALID_A);
  1427. goto err;
  1428. }
  1429. pb = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_B);
  1430. if (!OSSL_PARAM_get_BN(pb, &b)) {
  1431. ERR_raise(ERR_LIB_EC, EC_R_INVALID_B);
  1432. goto err;
  1433. }
  1434. /* extract the prime number or irreducible polynomial */
  1435. ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_P);
  1436. if (!OSSL_PARAM_get_BN(ptmp, &p)) {
  1437. ERR_raise(ERR_LIB_EC, EC_R_INVALID_P);
  1438. goto err;
  1439. }
  1440. if (is_prime_field) {
  1441. if (BN_is_negative(p) || BN_is_zero(p)) {
  1442. ERR_raise(ERR_LIB_EC, EC_R_INVALID_P);
  1443. goto err;
  1444. }
  1445. field_bits = BN_num_bits(p);
  1446. if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
  1447. ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE);
  1448. goto err;
  1449. }
  1450. /* create the EC_GROUP structure */
  1451. group = EC_GROUP_new_curve_GFp(p, a, b, bnctx);
  1452. } else {
  1453. # ifdef OPENSSL_NO_EC2M
  1454. ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
  1455. goto err;
  1456. # else
  1457. /* create the EC_GROUP structure */
  1458. group = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
  1459. if (group != NULL) {
  1460. field_bits = EC_GROUP_get_degree(group);
  1461. if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
  1462. ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE);
  1463. goto err;
  1464. }
  1465. }
  1466. # endif /* OPENSSL_NO_EC2M */
  1467. }
  1468. if (group == NULL) {
  1469. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  1470. goto err;
  1471. }
  1472. /* Optional seed */
  1473. ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED);
  1474. if (ptmp != NULL) {
  1475. if (ptmp->data_type != OSSL_PARAM_OCTET_STRING) {
  1476. ERR_raise(ERR_LIB_EC, EC_R_INVALID_SEED);
  1477. goto err;
  1478. }
  1479. if (!EC_GROUP_set_seed(group, ptmp->data, ptmp->data_size))
  1480. goto err;
  1481. }
  1482. /* generator base point */
  1483. ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GENERATOR);
  1484. if (ptmp == NULL
  1485. || ptmp->data_type != OSSL_PARAM_OCTET_STRING) {
  1486. ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
  1487. goto err;
  1488. }
  1489. buf = (const unsigned char *)(ptmp->data);
  1490. if ((point = EC_POINT_new(group)) == NULL)
  1491. goto err;
  1492. EC_GROUP_set_point_conversion_form(group,
  1493. (point_conversion_form_t)buf[0] & ~0x01);
  1494. if (!EC_POINT_oct2point(group, point, buf, ptmp->data_size, bnctx)) {
  1495. ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
  1496. goto err;
  1497. }
  1498. /* order */
  1499. ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ORDER);
  1500. if (!OSSL_PARAM_get_BN(ptmp, &order)
  1501. || (BN_is_negative(order) || BN_is_zero(order))
  1502. || (BN_num_bits(order) > (int)field_bits + 1)) { /* Hasse bound */
  1503. ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
  1504. goto err;
  1505. }
  1506. /* Optional cofactor */
  1507. ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_COFACTOR);
  1508. if (ptmp != NULL) {
  1509. cofactor = BN_CTX_get(bnctx);
  1510. if (cofactor == NULL || !OSSL_PARAM_get_BN(ptmp, &cofactor)) {
  1511. ERR_raise(ERR_LIB_EC, EC_R_INVALID_COFACTOR);
  1512. goto err;
  1513. }
  1514. }
  1515. /* set the generator, order and cofactor (if present) */
  1516. if (!EC_GROUP_set_generator(group, point, order, cofactor)) {
  1517. ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
  1518. goto err;
  1519. }
  1520. named_group = ec_group_explicit_to_named(group, libctx, propq, bnctx);
  1521. if (named_group == NULL) {
  1522. ERR_raise(ERR_LIB_EC, EC_R_INVALID_NAMED_GROUP_CONVERSION);
  1523. goto err;
  1524. }
  1525. if (named_group == group) {
  1526. /*
  1527. * If we did not find a named group then the encoding should be explicit
  1528. * if it was specified
  1529. */
  1530. ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ENCODING);
  1531. if (ptmp != NULL
  1532. && !ossl_ec_encoding_param2id(ptmp, &encoding_flag)) {
  1533. ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
  1534. goto err;
  1535. }
  1536. if (encoding_flag == OPENSSL_EC_NAMED_CURVE) {
  1537. ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
  1538. goto err;
  1539. }
  1540. EC_GROUP_set_asn1_flag(group, OPENSSL_EC_EXPLICIT_CURVE);
  1541. } else {
  1542. EC_GROUP_free(group);
  1543. group = named_group;
  1544. }
  1545. /* We've imported the group from explicit parameters, set it so. */
  1546. group->decoded_from_explicit_params = 1;
  1547. ok = 1;
  1548. err:
  1549. if (!ok) {
  1550. EC_GROUP_free(group);
  1551. group = NULL;
  1552. }
  1553. EC_POINT_free(point);
  1554. BN_CTX_end(bnctx);
  1555. BN_CTX_free(bnctx);
  1556. return group;
  1557. #endif /* FIPS_MODULE */
  1558. }
  1559. OSSL_PARAM *EC_GROUP_to_params(const EC_GROUP *group, OSSL_LIB_CTX *libctx,
  1560. const char *propq, BN_CTX *bnctx)
  1561. {
  1562. OSSL_PARAM_BLD *tmpl = NULL;
  1563. BN_CTX *new_bnctx = NULL;
  1564. unsigned char *gen_buf = NULL;
  1565. OSSL_PARAM *params = NULL;
  1566. if (group == NULL)
  1567. goto err;
  1568. tmpl = OSSL_PARAM_BLD_new();
  1569. if (tmpl == NULL)
  1570. goto err;
  1571. if (bnctx == NULL)
  1572. bnctx = new_bnctx = BN_CTX_new_ex(libctx);
  1573. if (bnctx == NULL)
  1574. goto err;
  1575. BN_CTX_start(bnctx);
  1576. if (!ossl_ec_group_todata(
  1577. group, tmpl, NULL, libctx, propq, bnctx, &gen_buf))
  1578. goto err;
  1579. params = OSSL_PARAM_BLD_to_param(tmpl);
  1580. err:
  1581. OSSL_PARAM_BLD_free(tmpl);
  1582. OPENSSL_free(gen_buf);
  1583. BN_CTX_end(bnctx);
  1584. BN_CTX_free(new_bnctx);
  1585. return params;
  1586. }