ec_lib.c 49 KB

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