ec_lib.c 49 KB

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