2
0

ec_lib.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760
  1. /*
  2. * Copyright 2001-2022 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 "crypto/ec.h"
  21. #include "internal/nelem.h"
  22. #include "ec_local.h"
  23. /* functions for EC_GROUP objects */
  24. EC_GROUP *ossl_ec_group_new_ex(OSSL_LIB_CTX *libctx, const char *propq,
  25. const EC_METHOD *meth)
  26. {
  27. EC_GROUP *ret;
  28. if (meth == NULL) {
  29. ERR_raise(ERR_LIB_EC, EC_R_SLOT_FULL);
  30. return NULL;
  31. }
  32. if (meth->group_init == 0) {
  33. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  34. return NULL;
  35. }
  36. ret = OPENSSL_zalloc(sizeof(*ret));
  37. if (ret == NULL) {
  38. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  39. return NULL;
  40. }
  41. ret->libctx = libctx;
  42. if (propq != NULL) {
  43. ret->propq = OPENSSL_strdup(propq);
  44. if (ret->propq == NULL) {
  45. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  46. goto err;
  47. }
  48. }
  49. ret->meth = meth;
  50. if ((ret->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
  51. ret->order = BN_new();
  52. if (ret->order == NULL)
  53. goto err;
  54. ret->cofactor = BN_new();
  55. if (ret->cofactor == NULL)
  56. goto err;
  57. }
  58. ret->asn1_flag = OPENSSL_EC_EXPLICIT_CURVE;
  59. ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
  60. if (!meth->group_init(ret))
  61. goto err;
  62. return ret;
  63. err:
  64. BN_free(ret->order);
  65. BN_free(ret->cofactor);
  66. OPENSSL_free(ret->propq);
  67. OPENSSL_free(ret);
  68. return NULL;
  69. }
  70. #ifndef OPENSSL_NO_DEPRECATED_3_0
  71. # ifndef FIPS_MODULE
  72. EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
  73. {
  74. return ossl_ec_group_new_ex(NULL, NULL, meth);
  75. }
  76. # endif
  77. #endif
  78. void EC_pre_comp_free(EC_GROUP *group)
  79. {
  80. switch (group->pre_comp_type) {
  81. case PCT_none:
  82. break;
  83. case PCT_nistz256:
  84. #ifdef ECP_NISTZ256_ASM
  85. EC_nistz256_pre_comp_free(group->pre_comp.nistz256);
  86. #endif
  87. break;
  88. #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  89. case PCT_nistp224:
  90. EC_nistp224_pre_comp_free(group->pre_comp.nistp224);
  91. break;
  92. case PCT_nistp256:
  93. EC_nistp256_pre_comp_free(group->pre_comp.nistp256);
  94. break;
  95. case PCT_nistp521:
  96. EC_nistp521_pre_comp_free(group->pre_comp.nistp521);
  97. break;
  98. #else
  99. case PCT_nistp224:
  100. case PCT_nistp256:
  101. case PCT_nistp521:
  102. break;
  103. #endif
  104. case PCT_ec:
  105. EC_ec_pre_comp_free(group->pre_comp.ec);
  106. break;
  107. }
  108. group->pre_comp.ec = NULL;
  109. }
  110. void EC_GROUP_free(EC_GROUP *group)
  111. {
  112. if (!group)
  113. return;
  114. if (group->meth->group_finish != 0)
  115. group->meth->group_finish(group);
  116. EC_pre_comp_free(group);
  117. BN_MONT_CTX_free(group->mont_data);
  118. EC_POINT_free(group->generator);
  119. BN_free(group->order);
  120. BN_free(group->cofactor);
  121. OPENSSL_free(group->seed);
  122. OPENSSL_free(group->propq);
  123. OPENSSL_free(group);
  124. }
  125. #ifndef OPENSSL_NO_DEPRECATED_3_0
  126. void EC_GROUP_clear_free(EC_GROUP *group)
  127. {
  128. if (!group)
  129. return;
  130. if (group->meth->group_clear_finish != 0)
  131. group->meth->group_clear_finish(group);
  132. else if (group->meth->group_finish != 0)
  133. group->meth->group_finish(group);
  134. EC_pre_comp_free(group);
  135. BN_MONT_CTX_free(group->mont_data);
  136. EC_POINT_clear_free(group->generator);
  137. BN_clear_free(group->order);
  138. BN_clear_free(group->cofactor);
  139. OPENSSL_clear_free(group->seed, group->seed_len);
  140. OPENSSL_clear_free(group, sizeof(*group));
  141. }
  142. #endif
  143. int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
  144. {
  145. if (dest->meth->group_copy == 0) {
  146. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  147. return 0;
  148. }
  149. if (dest->meth != src->meth) {
  150. ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
  151. return 0;
  152. }
  153. if (dest == src)
  154. return 1;
  155. dest->libctx = src->libctx;
  156. dest->curve_name = src->curve_name;
  157. /* Copy precomputed */
  158. dest->pre_comp_type = src->pre_comp_type;
  159. switch (src->pre_comp_type) {
  160. case PCT_none:
  161. dest->pre_comp.ec = NULL;
  162. break;
  163. case PCT_nistz256:
  164. #ifdef ECP_NISTZ256_ASM
  165. dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);
  166. #endif
  167. break;
  168. #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  169. case PCT_nistp224:
  170. dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);
  171. break;
  172. case PCT_nistp256:
  173. dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);
  174. break;
  175. case PCT_nistp521:
  176. dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);
  177. break;
  178. #else
  179. case PCT_nistp224:
  180. case PCT_nistp256:
  181. case PCT_nistp521:
  182. break;
  183. #endif
  184. case PCT_ec:
  185. dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);
  186. break;
  187. }
  188. if (src->mont_data != NULL) {
  189. if (dest->mont_data == NULL) {
  190. dest->mont_data = BN_MONT_CTX_new();
  191. if (dest->mont_data == NULL)
  192. return 0;
  193. }
  194. if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))
  195. return 0;
  196. } else {
  197. /* src->generator == NULL */
  198. BN_MONT_CTX_free(dest->mont_data);
  199. dest->mont_data = NULL;
  200. }
  201. if (src->generator != NULL) {
  202. if (dest->generator == NULL) {
  203. dest->generator = EC_POINT_new(dest);
  204. if (dest->generator == NULL)
  205. return 0;
  206. }
  207. if (!EC_POINT_copy(dest->generator, src->generator))
  208. return 0;
  209. } else {
  210. /* src->generator == NULL */
  211. EC_POINT_clear_free(dest->generator);
  212. dest->generator = NULL;
  213. }
  214. if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
  215. if (!BN_copy(dest->order, src->order))
  216. return 0;
  217. if (!BN_copy(dest->cofactor, src->cofactor))
  218. return 0;
  219. }
  220. dest->asn1_flag = src->asn1_flag;
  221. dest->asn1_form = src->asn1_form;
  222. dest->decoded_from_explicit_params = src->decoded_from_explicit_params;
  223. if (src->seed) {
  224. OPENSSL_free(dest->seed);
  225. if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL) {
  226. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  227. return 0;
  228. }
  229. if (!memcpy(dest->seed, src->seed, src->seed_len))
  230. return 0;
  231. dest->seed_len = src->seed_len;
  232. } else {
  233. OPENSSL_free(dest->seed);
  234. dest->seed = NULL;
  235. dest->seed_len = 0;
  236. }
  237. return dest->meth->group_copy(dest, src);
  238. }
  239. EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
  240. {
  241. EC_GROUP *t = NULL;
  242. int ok = 0;
  243. if (a == NULL)
  244. return NULL;
  245. if ((t = ossl_ec_group_new_ex(a->libctx, a->propq, a->meth)) == NULL)
  246. return NULL;
  247. if (!EC_GROUP_copy(t, a))
  248. goto err;
  249. ok = 1;
  250. err:
  251. if (!ok) {
  252. EC_GROUP_free(t);
  253. return NULL;
  254. }
  255. return t;
  256. }
  257. #ifndef OPENSSL_NO_DEPRECATED_3_0
  258. const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
  259. {
  260. return group->meth;
  261. }
  262. int EC_METHOD_get_field_type(const EC_METHOD *meth)
  263. {
  264. return meth->field_type;
  265. }
  266. #endif
  267. static int ec_precompute_mont_data(EC_GROUP *);
  268. /*-
  269. * Try computing cofactor from the generator order (n) and field cardinality (q).
  270. * This works for all curves of cryptographic interest.
  271. *
  272. * Hasse thm: q + 1 - 2*sqrt(q) <= n*h <= q + 1 + 2*sqrt(q)
  273. * h_min = (q + 1 - 2*sqrt(q))/n
  274. * h_max = (q + 1 + 2*sqrt(q))/n
  275. * h_max - h_min = 4*sqrt(q)/n
  276. * So if n > 4*sqrt(q) holds, there is only one possible value for h:
  277. * h = \lfloor (h_min + h_max)/2 \rceil = \lfloor (q + 1)/n \rceil
  278. *
  279. * Otherwise, zero cofactor and return success.
  280. */
  281. static int ec_guess_cofactor(EC_GROUP *group) {
  282. int ret = 0;
  283. BN_CTX *ctx = NULL;
  284. BIGNUM *q = NULL;
  285. /*-
  286. * If the cofactor is too large, we cannot guess it.
  287. * The RHS of below is a strict overestimate of lg(4 * sqrt(q))
  288. */
  289. if (BN_num_bits(group->order) <= (BN_num_bits(group->field) + 1) / 2 + 3) {
  290. /* default to 0 */
  291. BN_zero(group->cofactor);
  292. /* return success */
  293. return 1;
  294. }
  295. if ((ctx = BN_CTX_new_ex(group->libctx)) == NULL)
  296. return 0;
  297. BN_CTX_start(ctx);
  298. if ((q = BN_CTX_get(ctx)) == NULL)
  299. goto err;
  300. /* set q = 2**m for binary fields; q = p otherwise */
  301. if (group->meth->field_type == NID_X9_62_characteristic_two_field) {
  302. BN_zero(q);
  303. if (!BN_set_bit(q, BN_num_bits(group->field) - 1))
  304. goto err;
  305. } else {
  306. if (!BN_copy(q, group->field))
  307. goto err;
  308. }
  309. /* compute h = \lfloor (q + 1)/n \rceil = \lfloor (q + 1 + n/2)/n \rfloor */
  310. if (!BN_rshift1(group->cofactor, group->order) /* n/2 */
  311. || !BN_add(group->cofactor, group->cofactor, q) /* q + n/2 */
  312. /* q + 1 + n/2 */
  313. || !BN_add(group->cofactor, group->cofactor, BN_value_one())
  314. /* (q + 1 + n/2)/n */
  315. || !BN_div(group->cofactor, NULL, group->cofactor, group->order, ctx))
  316. goto err;
  317. ret = 1;
  318. err:
  319. BN_CTX_end(ctx);
  320. BN_CTX_free(ctx);
  321. return ret;
  322. }
  323. int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
  324. const BIGNUM *order, const BIGNUM *cofactor)
  325. {
  326. if (generator == NULL) {
  327. ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
  328. return 0;
  329. }
  330. /* require group->field >= 1 */
  331. if (group->field == NULL || BN_is_zero(group->field)
  332. || BN_is_negative(group->field)) {
  333. ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
  334. return 0;
  335. }
  336. /*-
  337. * - require order >= 1
  338. * - enforce upper bound due to Hasse thm: order can be no more than one bit
  339. * longer than field cardinality
  340. */
  341. if (order == NULL || BN_is_zero(order) || BN_is_negative(order)
  342. || BN_num_bits(order) > BN_num_bits(group->field) + 1) {
  343. ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
  344. return 0;
  345. }
  346. /*-
  347. * Unfortunately the cofactor is an optional field in many standards.
  348. * Internally, the lib uses 0 cofactor as a marker for "unknown cofactor".
  349. * So accept cofactor == NULL or cofactor >= 0.
  350. */
  351. if (cofactor != NULL && BN_is_negative(cofactor)) {
  352. ERR_raise(ERR_LIB_EC, EC_R_UNKNOWN_COFACTOR);
  353. return 0;
  354. }
  355. if (group->generator == NULL) {
  356. group->generator = EC_POINT_new(group);
  357. if (group->generator == NULL)
  358. return 0;
  359. }
  360. if (!EC_POINT_copy(group->generator, generator))
  361. return 0;
  362. if (!BN_copy(group->order, order))
  363. return 0;
  364. /* Either take the provided positive cofactor, or try to compute it */
  365. if (cofactor != NULL && !BN_is_zero(cofactor)) {
  366. if (!BN_copy(group->cofactor, cofactor))
  367. return 0;
  368. } else if (!ec_guess_cofactor(group)) {
  369. BN_zero(group->cofactor);
  370. return 0;
  371. }
  372. /*
  373. * Some groups have an order with
  374. * factors of two, which makes the Montgomery setup fail.
  375. * |group->mont_data| will be NULL in this case.
  376. */
  377. if (BN_is_odd(group->order)) {
  378. return ec_precompute_mont_data(group);
  379. }
  380. BN_MONT_CTX_free(group->mont_data);
  381. group->mont_data = NULL;
  382. return 1;
  383. }
  384. const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
  385. {
  386. return group->generator;
  387. }
  388. BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
  389. {
  390. return group->mont_data;
  391. }
  392. int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
  393. {
  394. if (group->order == NULL)
  395. return 0;
  396. if (!BN_copy(order, group->order))
  397. return 0;
  398. return !BN_is_zero(order);
  399. }
  400. const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group)
  401. {
  402. return group->order;
  403. }
  404. int EC_GROUP_order_bits(const EC_GROUP *group)
  405. {
  406. return group->meth->group_order_bits(group);
  407. }
  408. int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
  409. BN_CTX *ctx)
  410. {
  411. if (group->cofactor == NULL)
  412. return 0;
  413. if (!BN_copy(cofactor, group->cofactor))
  414. return 0;
  415. return !BN_is_zero(group->cofactor);
  416. }
  417. const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group)
  418. {
  419. return group->cofactor;
  420. }
  421. void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
  422. {
  423. group->curve_name = nid;
  424. group->asn1_flag =
  425. (nid != NID_undef)
  426. ? OPENSSL_EC_NAMED_CURVE
  427. : OPENSSL_EC_EXPLICIT_CURVE;
  428. }
  429. int EC_GROUP_get_curve_name(const EC_GROUP *group)
  430. {
  431. return group->curve_name;
  432. }
  433. const BIGNUM *EC_GROUP_get0_field(const EC_GROUP *group)
  434. {
  435. return group->field;
  436. }
  437. int EC_GROUP_get_field_type(const EC_GROUP *group)
  438. {
  439. return group->meth->field_type;
  440. }
  441. void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
  442. {
  443. group->asn1_flag = flag;
  444. }
  445. int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
  446. {
  447. return group->asn1_flag;
  448. }
  449. void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
  450. point_conversion_form_t form)
  451. {
  452. group->asn1_form = form;
  453. }
  454. point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
  455. *group)
  456. {
  457. return group->asn1_form;
  458. }
  459. size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
  460. {
  461. OPENSSL_free(group->seed);
  462. group->seed = NULL;
  463. group->seed_len = 0;
  464. if (!len || !p)
  465. return 1;
  466. if ((group->seed = OPENSSL_malloc(len)) == NULL) {
  467. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  468. return 0;
  469. }
  470. memcpy(group->seed, p, len);
  471. group->seed_len = len;
  472. return len;
  473. }
  474. unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
  475. {
  476. return group->seed;
  477. }
  478. size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
  479. {
  480. return group->seed_len;
  481. }
  482. int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
  483. const BIGNUM *b, BN_CTX *ctx)
  484. {
  485. if (group->meth->group_set_curve == 0) {
  486. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  487. return 0;
  488. }
  489. return group->meth->group_set_curve(group, p, a, b, ctx);
  490. }
  491. int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b,
  492. BN_CTX *ctx)
  493. {
  494. if (group->meth->group_get_curve == NULL) {
  495. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  496. return 0;
  497. }
  498. return group->meth->group_get_curve(group, p, a, b, ctx);
  499. }
  500. #ifndef OPENSSL_NO_DEPRECATED_3_0
  501. int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
  502. const BIGNUM *b, BN_CTX *ctx)
  503. {
  504. return EC_GROUP_set_curve(group, p, a, b, ctx);
  505. }
  506. int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
  507. BIGNUM *b, BN_CTX *ctx)
  508. {
  509. return EC_GROUP_get_curve(group, p, a, b, ctx);
  510. }
  511. # ifndef OPENSSL_NO_EC2M
  512. int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
  513. const BIGNUM *b, BN_CTX *ctx)
  514. {
  515. return EC_GROUP_set_curve(group, p, a, b, ctx);
  516. }
  517. int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
  518. BIGNUM *b, BN_CTX *ctx)
  519. {
  520. return EC_GROUP_get_curve(group, p, a, b, ctx);
  521. }
  522. # endif
  523. #endif
  524. int EC_GROUP_get_degree(const EC_GROUP *group)
  525. {
  526. if (group->meth->group_get_degree == 0) {
  527. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  528. return 0;
  529. }
  530. return group->meth->group_get_degree(group);
  531. }
  532. int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
  533. {
  534. if (group->meth->group_check_discriminant == 0) {
  535. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  536. return 0;
  537. }
  538. return group->meth->group_check_discriminant(group, ctx);
  539. }
  540. int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
  541. {
  542. int r = 0;
  543. BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
  544. #ifndef FIPS_MODULE
  545. BN_CTX *ctx_new = NULL;
  546. #endif
  547. /* compare the field types */
  548. if (EC_GROUP_get_field_type(a) != EC_GROUP_get_field_type(b))
  549. return 1;
  550. /* compare the curve name (if present in both) */
  551. if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
  552. EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
  553. return 1;
  554. if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE)
  555. return 0;
  556. #ifndef FIPS_MODULE
  557. if (ctx == NULL)
  558. ctx_new = ctx = BN_CTX_new();
  559. #endif
  560. if (ctx == NULL)
  561. return -1;
  562. BN_CTX_start(ctx);
  563. a1 = BN_CTX_get(ctx);
  564. a2 = BN_CTX_get(ctx);
  565. a3 = BN_CTX_get(ctx);
  566. b1 = BN_CTX_get(ctx);
  567. b2 = BN_CTX_get(ctx);
  568. b3 = BN_CTX_get(ctx);
  569. if (b3 == NULL) {
  570. BN_CTX_end(ctx);
  571. #ifndef FIPS_MODULE
  572. BN_CTX_free(ctx_new);
  573. #endif
  574. return -1;
  575. }
  576. /*
  577. * XXX This approach assumes that the external representation of curves
  578. * over the same field type is the same.
  579. */
  580. if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
  581. !b->meth->group_get_curve(b, b1, b2, b3, ctx))
  582. r = 1;
  583. /* return 1 if the curve parameters are different */
  584. if (r || BN_cmp(a1, b1) != 0 || BN_cmp(a2, b2) != 0 || BN_cmp(a3, b3) != 0)
  585. r = 1;
  586. /* XXX EC_POINT_cmp() assumes that the methods are equal */
  587. /* return 1 if the generators are different */
  588. if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
  589. EC_GROUP_get0_generator(b), ctx) != 0)
  590. r = 1;
  591. if (!r) {
  592. const BIGNUM *ao, *bo, *ac, *bc;
  593. /* compare the orders */
  594. ao = EC_GROUP_get0_order(a);
  595. bo = EC_GROUP_get0_order(b);
  596. if (ao == NULL || bo == NULL) {
  597. /* return an error if either order is NULL */
  598. r = -1;
  599. goto end;
  600. }
  601. if (BN_cmp(ao, bo) != 0) {
  602. /* return 1 if orders are different */
  603. r = 1;
  604. goto end;
  605. }
  606. /*
  607. * It gets here if the curve parameters and generator matched.
  608. * Now check the optional cofactors (if both are present).
  609. */
  610. ac = EC_GROUP_get0_cofactor(a);
  611. bc = EC_GROUP_get0_cofactor(b);
  612. /* Returns 1 (mismatch) if both cofactors are specified and different */
  613. if (!BN_is_zero(ac) && !BN_is_zero(bc) && BN_cmp(ac, bc) != 0)
  614. r = 1;
  615. /* Returns 0 if the parameters matched */
  616. }
  617. end:
  618. BN_CTX_end(ctx);
  619. #ifndef FIPS_MODULE
  620. BN_CTX_free(ctx_new);
  621. #endif
  622. return r;
  623. }
  624. /* functions for EC_POINT objects */
  625. EC_POINT *EC_POINT_new(const EC_GROUP *group)
  626. {
  627. EC_POINT *ret;
  628. if (group == NULL) {
  629. ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
  630. return NULL;
  631. }
  632. if (group->meth->point_init == NULL) {
  633. ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  634. return NULL;
  635. }
  636. ret = OPENSSL_zalloc(sizeof(*ret));
  637. if (ret == NULL) {
  638. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  639. return NULL;
  640. }
  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. * Exponent e is public.
  1109. * No need for scatter-gather or BN_FLG_CONSTTIME.
  1110. */
  1111. if (!BN_mod_exp_mont(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. ECerr(0, 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. ECerr(0, 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. ECerr(0, 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_MALLOC_FAILURE);
  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_MALLOC_FAILURE);
  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. }