2
0

ec_lib.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. /*
  2. * Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. *
  5. * Licensed under the OpenSSL license (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. #include <string.h>
  11. #include <openssl/err.h>
  12. #include <openssl/opensslv.h>
  13. #include "ec_local.h"
  14. /* functions for EC_GROUP objects */
  15. EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
  16. {
  17. EC_GROUP *ret;
  18. if (meth == NULL) {
  19. ECerr(EC_F_EC_GROUP_NEW, EC_R_SLOT_FULL);
  20. return NULL;
  21. }
  22. if (meth->group_init == 0) {
  23. ECerr(EC_F_EC_GROUP_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  24. return NULL;
  25. }
  26. ret = OPENSSL_zalloc(sizeof(*ret));
  27. if (ret == NULL) {
  28. ECerr(EC_F_EC_GROUP_NEW, ERR_R_MALLOC_FAILURE);
  29. return NULL;
  30. }
  31. ret->meth = meth;
  32. if ((ret->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
  33. ret->order = BN_new();
  34. if (ret->order == NULL)
  35. goto err;
  36. ret->cofactor = BN_new();
  37. if (ret->cofactor == NULL)
  38. goto err;
  39. }
  40. ret->asn1_flag = OPENSSL_EC_NAMED_CURVE;
  41. ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
  42. if (!meth->group_init(ret))
  43. goto err;
  44. return ret;
  45. err:
  46. BN_free(ret->order);
  47. BN_free(ret->cofactor);
  48. OPENSSL_free(ret);
  49. return NULL;
  50. }
  51. void EC_pre_comp_free(EC_GROUP *group)
  52. {
  53. switch (group->pre_comp_type) {
  54. case PCT_none:
  55. break;
  56. case PCT_nistz256:
  57. #ifdef ECP_NISTZ256_ASM
  58. EC_nistz256_pre_comp_free(group->pre_comp.nistz256);
  59. #endif
  60. break;
  61. #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  62. case PCT_nistp224:
  63. EC_nistp224_pre_comp_free(group->pre_comp.nistp224);
  64. break;
  65. case PCT_nistp256:
  66. EC_nistp256_pre_comp_free(group->pre_comp.nistp256);
  67. break;
  68. case PCT_nistp521:
  69. EC_nistp521_pre_comp_free(group->pre_comp.nistp521);
  70. break;
  71. #else
  72. case PCT_nistp224:
  73. case PCT_nistp256:
  74. case PCT_nistp521:
  75. break;
  76. #endif
  77. case PCT_ec:
  78. EC_ec_pre_comp_free(group->pre_comp.ec);
  79. break;
  80. }
  81. group->pre_comp.ec = NULL;
  82. }
  83. void EC_GROUP_free(EC_GROUP *group)
  84. {
  85. if (!group)
  86. return;
  87. if (group->meth->group_finish != 0)
  88. group->meth->group_finish(group);
  89. EC_pre_comp_free(group);
  90. BN_MONT_CTX_free(group->mont_data);
  91. EC_POINT_free(group->generator);
  92. BN_free(group->order);
  93. BN_free(group->cofactor);
  94. OPENSSL_free(group->seed);
  95. OPENSSL_free(group);
  96. }
  97. void EC_GROUP_clear_free(EC_GROUP *group)
  98. {
  99. if (!group)
  100. return;
  101. if (group->meth->group_clear_finish != 0)
  102. group->meth->group_clear_finish(group);
  103. else if (group->meth->group_finish != 0)
  104. group->meth->group_finish(group);
  105. EC_pre_comp_free(group);
  106. BN_MONT_CTX_free(group->mont_data);
  107. EC_POINT_clear_free(group->generator);
  108. BN_clear_free(group->order);
  109. BN_clear_free(group->cofactor);
  110. OPENSSL_clear_free(group->seed, group->seed_len);
  111. OPENSSL_clear_free(group, sizeof(*group));
  112. }
  113. int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
  114. {
  115. if (dest->meth->group_copy == 0) {
  116. ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  117. return 0;
  118. }
  119. if (dest->meth != src->meth) {
  120. ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS);
  121. return 0;
  122. }
  123. if (dest == src)
  124. return 1;
  125. dest->curve_name = src->curve_name;
  126. /* Copy precomputed */
  127. dest->pre_comp_type = src->pre_comp_type;
  128. switch (src->pre_comp_type) {
  129. case PCT_none:
  130. dest->pre_comp.ec = NULL;
  131. break;
  132. case PCT_nistz256:
  133. #ifdef ECP_NISTZ256_ASM
  134. dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);
  135. #endif
  136. break;
  137. #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  138. case PCT_nistp224:
  139. dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);
  140. break;
  141. case PCT_nistp256:
  142. dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);
  143. break;
  144. case PCT_nistp521:
  145. dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);
  146. break;
  147. #else
  148. case PCT_nistp224:
  149. case PCT_nistp256:
  150. case PCT_nistp521:
  151. break;
  152. #endif
  153. case PCT_ec:
  154. dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);
  155. break;
  156. }
  157. if (src->mont_data != NULL) {
  158. if (dest->mont_data == NULL) {
  159. dest->mont_data = BN_MONT_CTX_new();
  160. if (dest->mont_data == NULL)
  161. return 0;
  162. }
  163. if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))
  164. return 0;
  165. } else {
  166. /* src->generator == NULL */
  167. BN_MONT_CTX_free(dest->mont_data);
  168. dest->mont_data = NULL;
  169. }
  170. if (src->generator != NULL) {
  171. if (dest->generator == NULL) {
  172. dest->generator = EC_POINT_new(dest);
  173. if (dest->generator == NULL)
  174. return 0;
  175. }
  176. if (!EC_POINT_copy(dest->generator, src->generator))
  177. return 0;
  178. } else {
  179. /* src->generator == NULL */
  180. EC_POINT_clear_free(dest->generator);
  181. dest->generator = NULL;
  182. }
  183. if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
  184. if (!BN_copy(dest->order, src->order))
  185. return 0;
  186. if (!BN_copy(dest->cofactor, src->cofactor))
  187. return 0;
  188. }
  189. dest->asn1_flag = src->asn1_flag;
  190. dest->asn1_form = src->asn1_form;
  191. dest->decoded_from_explicit_params = src->decoded_from_explicit_params;
  192. if (src->seed) {
  193. OPENSSL_free(dest->seed);
  194. if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL) {
  195. ECerr(EC_F_EC_GROUP_COPY, ERR_R_MALLOC_FAILURE);
  196. return 0;
  197. }
  198. if (!memcpy(dest->seed, src->seed, src->seed_len))
  199. return 0;
  200. dest->seed_len = src->seed_len;
  201. } else {
  202. OPENSSL_free(dest->seed);
  203. dest->seed = NULL;
  204. dest->seed_len = 0;
  205. }
  206. return dest->meth->group_copy(dest, src);
  207. }
  208. EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
  209. {
  210. EC_GROUP *t = NULL;
  211. int ok = 0;
  212. if (a == NULL)
  213. return NULL;
  214. if ((t = EC_GROUP_new(a->meth)) == NULL)
  215. return NULL;
  216. if (!EC_GROUP_copy(t, a))
  217. goto err;
  218. ok = 1;
  219. err:
  220. if (!ok) {
  221. EC_GROUP_free(t);
  222. return NULL;
  223. }
  224. return t;
  225. }
  226. const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
  227. {
  228. return group->meth;
  229. }
  230. int EC_METHOD_get_field_type(const EC_METHOD *meth)
  231. {
  232. return meth->field_type;
  233. }
  234. static int ec_precompute_mont_data(EC_GROUP *);
  235. /*-
  236. * Try computing cofactor from the generator order (n) and field cardinality (q).
  237. * This works for all curves of cryptographic interest.
  238. *
  239. * Hasse thm: q + 1 - 2*sqrt(q) <= n*h <= q + 1 + 2*sqrt(q)
  240. * h_min = (q + 1 - 2*sqrt(q))/n
  241. * h_max = (q + 1 + 2*sqrt(q))/n
  242. * h_max - h_min = 4*sqrt(q)/n
  243. * So if n > 4*sqrt(q) holds, there is only one possible value for h:
  244. * h = \lfloor (h_min + h_max)/2 \rceil = \lfloor (q + 1)/n \rceil
  245. *
  246. * Otherwise, zero cofactor and return success.
  247. */
  248. static int ec_guess_cofactor(EC_GROUP *group) {
  249. int ret = 0;
  250. BN_CTX *ctx = NULL;
  251. BIGNUM *q = NULL;
  252. /*-
  253. * If the cofactor is too large, we cannot guess it.
  254. * The RHS of below is a strict overestimate of lg(4 * sqrt(q))
  255. */
  256. if (BN_num_bits(group->order) <= (BN_num_bits(group->field) + 1) / 2 + 3) {
  257. /* default to 0 */
  258. BN_zero(group->cofactor);
  259. /* return success */
  260. return 1;
  261. }
  262. if ((ctx = BN_CTX_new()) == NULL)
  263. return 0;
  264. BN_CTX_start(ctx);
  265. if ((q = BN_CTX_get(ctx)) == NULL)
  266. goto err;
  267. /* set q = 2**m for binary fields; q = p otherwise */
  268. if (group->meth->field_type == NID_X9_62_characteristic_two_field) {
  269. BN_zero(q);
  270. if (!BN_set_bit(q, BN_num_bits(group->field) - 1))
  271. goto err;
  272. } else {
  273. if (!BN_copy(q, group->field))
  274. goto err;
  275. }
  276. /* compute h = \lfloor (q + 1)/n \rceil = \lfloor (q + 1 + n/2)/n \rfloor */
  277. if (!BN_rshift1(group->cofactor, group->order) /* n/2 */
  278. || !BN_add(group->cofactor, group->cofactor, q) /* q + n/2 */
  279. /* q + 1 + n/2 */
  280. || !BN_add(group->cofactor, group->cofactor, BN_value_one())
  281. /* (q + 1 + n/2)/n */
  282. || !BN_div(group->cofactor, NULL, group->cofactor, group->order, ctx))
  283. goto err;
  284. ret = 1;
  285. err:
  286. BN_CTX_end(ctx);
  287. BN_CTX_free(ctx);
  288. return ret;
  289. }
  290. int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
  291. const BIGNUM *order, const BIGNUM *cofactor)
  292. {
  293. if (generator == NULL) {
  294. ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
  295. return 0;
  296. }
  297. /* require group->field >= 1 */
  298. if (group->field == NULL || BN_is_zero(group->field)
  299. || BN_is_negative(group->field)) {
  300. ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_INVALID_FIELD);
  301. return 0;
  302. }
  303. /*-
  304. * - require order >= 1
  305. * - enforce upper bound due to Hasse thm: order can be no more than one bit
  306. * longer than field cardinality
  307. */
  308. if (order == NULL || BN_is_zero(order) || BN_is_negative(order)
  309. || BN_num_bits(order) > BN_num_bits(group->field) + 1) {
  310. ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_INVALID_GROUP_ORDER);
  311. return 0;
  312. }
  313. /*-
  314. * Unfortunately the cofactor is an optional field in many standards.
  315. * Internally, the lib uses 0 cofactor as a marker for "unknown cofactor".
  316. * So accept cofactor == NULL or cofactor >= 0.
  317. */
  318. if (cofactor != NULL && BN_is_negative(cofactor)) {
  319. ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_UNKNOWN_COFACTOR);
  320. return 0;
  321. }
  322. if (group->generator == NULL) {
  323. group->generator = EC_POINT_new(group);
  324. if (group->generator == NULL)
  325. return 0;
  326. }
  327. if (!EC_POINT_copy(group->generator, generator))
  328. return 0;
  329. if (!BN_copy(group->order, order))
  330. return 0;
  331. /* Either take the provided positive cofactor, or try to compute it */
  332. if (cofactor != NULL && !BN_is_zero(cofactor)) {
  333. if (!BN_copy(group->cofactor, cofactor))
  334. return 0;
  335. } else if (!ec_guess_cofactor(group)) {
  336. BN_zero(group->cofactor);
  337. return 0;
  338. }
  339. /*
  340. * Some groups have an order with
  341. * factors of two, which makes the Montgomery setup fail.
  342. * |group->mont_data| will be NULL in this case.
  343. */
  344. if (BN_is_odd(group->order)) {
  345. return ec_precompute_mont_data(group);
  346. }
  347. BN_MONT_CTX_free(group->mont_data);
  348. group->mont_data = NULL;
  349. return 1;
  350. }
  351. const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
  352. {
  353. return group->generator;
  354. }
  355. BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
  356. {
  357. return group->mont_data;
  358. }
  359. int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
  360. {
  361. if (group->order == NULL)
  362. return 0;
  363. if (!BN_copy(order, group->order))
  364. return 0;
  365. return !BN_is_zero(order);
  366. }
  367. const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group)
  368. {
  369. return group->order;
  370. }
  371. int EC_GROUP_order_bits(const EC_GROUP *group)
  372. {
  373. return group->meth->group_order_bits(group);
  374. }
  375. int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
  376. BN_CTX *ctx)
  377. {
  378. if (group->cofactor == NULL)
  379. return 0;
  380. if (!BN_copy(cofactor, group->cofactor))
  381. return 0;
  382. return !BN_is_zero(group->cofactor);
  383. }
  384. const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group)
  385. {
  386. return group->cofactor;
  387. }
  388. void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
  389. {
  390. group->curve_name = nid;
  391. }
  392. int EC_GROUP_get_curve_name(const EC_GROUP *group)
  393. {
  394. return group->curve_name;
  395. }
  396. void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
  397. {
  398. group->asn1_flag = flag;
  399. }
  400. int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
  401. {
  402. return group->asn1_flag;
  403. }
  404. void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
  405. point_conversion_form_t form)
  406. {
  407. group->asn1_form = form;
  408. }
  409. point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
  410. *group)
  411. {
  412. return group->asn1_form;
  413. }
  414. size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
  415. {
  416. OPENSSL_free(group->seed);
  417. group->seed = NULL;
  418. group->seed_len = 0;
  419. if (!len || !p)
  420. return 1;
  421. if ((group->seed = OPENSSL_malloc(len)) == NULL) {
  422. ECerr(EC_F_EC_GROUP_SET_SEED, ERR_R_MALLOC_FAILURE);
  423. return 0;
  424. }
  425. memcpy(group->seed, p, len);
  426. group->seed_len = len;
  427. return len;
  428. }
  429. unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
  430. {
  431. return group->seed;
  432. }
  433. size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
  434. {
  435. return group->seed_len;
  436. }
  437. int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
  438. const BIGNUM *b, BN_CTX *ctx)
  439. {
  440. if (group->meth->group_set_curve == 0) {
  441. ECerr(EC_F_EC_GROUP_SET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  442. return 0;
  443. }
  444. return group->meth->group_set_curve(group, p, a, b, ctx);
  445. }
  446. int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b,
  447. BN_CTX *ctx)
  448. {
  449. if (group->meth->group_get_curve == NULL) {
  450. ECerr(EC_F_EC_GROUP_GET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  451. return 0;
  452. }
  453. return group->meth->group_get_curve(group, p, a, b, ctx);
  454. }
  455. #if OPENSSL_API_COMPAT < 0x10200000L
  456. int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
  457. const BIGNUM *b, BN_CTX *ctx)
  458. {
  459. return EC_GROUP_set_curve(group, p, a, b, ctx);
  460. }
  461. int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
  462. BIGNUM *b, BN_CTX *ctx)
  463. {
  464. return EC_GROUP_get_curve(group, p, a, b, ctx);
  465. }
  466. # ifndef OPENSSL_NO_EC2M
  467. int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
  468. const BIGNUM *b, BN_CTX *ctx)
  469. {
  470. return EC_GROUP_set_curve(group, p, a, b, ctx);
  471. }
  472. int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
  473. BIGNUM *b, BN_CTX *ctx)
  474. {
  475. return EC_GROUP_get_curve(group, p, a, b, ctx);
  476. }
  477. # endif
  478. #endif
  479. int EC_GROUP_get_degree(const EC_GROUP *group)
  480. {
  481. if (group->meth->group_get_degree == 0) {
  482. ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  483. return 0;
  484. }
  485. return group->meth->group_get_degree(group);
  486. }
  487. int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
  488. {
  489. if (group->meth->group_check_discriminant == 0) {
  490. ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
  491. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  492. return 0;
  493. }
  494. return group->meth->group_check_discriminant(group, ctx);
  495. }
  496. int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
  497. {
  498. int r = 0;
  499. BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
  500. BN_CTX *ctx_new = NULL;
  501. /* compare the field types */
  502. if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
  503. EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
  504. return 1;
  505. /* compare the curve name (if present in both) */
  506. if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
  507. EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
  508. return 1;
  509. if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE)
  510. return 0;
  511. if (ctx == NULL)
  512. ctx_new = ctx = BN_CTX_new();
  513. if (ctx == NULL)
  514. return -1;
  515. BN_CTX_start(ctx);
  516. a1 = BN_CTX_get(ctx);
  517. a2 = BN_CTX_get(ctx);
  518. a3 = BN_CTX_get(ctx);
  519. b1 = BN_CTX_get(ctx);
  520. b2 = BN_CTX_get(ctx);
  521. b3 = BN_CTX_get(ctx);
  522. if (b3 == NULL) {
  523. BN_CTX_end(ctx);
  524. BN_CTX_free(ctx_new);
  525. return -1;
  526. }
  527. /*
  528. * XXX This approach assumes that the external representation of curves
  529. * over the same field type is the same.
  530. */
  531. if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
  532. !b->meth->group_get_curve(b, b1, b2, b3, ctx))
  533. r = 1;
  534. if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
  535. r = 1;
  536. /* XXX EC_POINT_cmp() assumes that the methods are equal */
  537. if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
  538. EC_GROUP_get0_generator(b), ctx))
  539. r = 1;
  540. if (!r) {
  541. const BIGNUM *ao, *bo, *ac, *bc;
  542. /* compare the order and cofactor */
  543. ao = EC_GROUP_get0_order(a);
  544. bo = EC_GROUP_get0_order(b);
  545. ac = EC_GROUP_get0_cofactor(a);
  546. bc = EC_GROUP_get0_cofactor(b);
  547. if (ao == NULL || bo == NULL) {
  548. BN_CTX_end(ctx);
  549. BN_CTX_free(ctx_new);
  550. return -1;
  551. }
  552. if (BN_cmp(ao, bo) || BN_cmp(ac, bc))
  553. r = 1;
  554. }
  555. BN_CTX_end(ctx);
  556. BN_CTX_free(ctx_new);
  557. return r;
  558. }
  559. /* functions for EC_POINT objects */
  560. EC_POINT *EC_POINT_new(const EC_GROUP *group)
  561. {
  562. EC_POINT *ret;
  563. if (group == NULL) {
  564. ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
  565. return NULL;
  566. }
  567. if (group->meth->point_init == NULL) {
  568. ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  569. return NULL;
  570. }
  571. ret = OPENSSL_zalloc(sizeof(*ret));
  572. if (ret == NULL) {
  573. ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
  574. return NULL;
  575. }
  576. ret->meth = group->meth;
  577. ret->curve_name = group->curve_name;
  578. if (!ret->meth->point_init(ret)) {
  579. OPENSSL_free(ret);
  580. return NULL;
  581. }
  582. return ret;
  583. }
  584. void EC_POINT_free(EC_POINT *point)
  585. {
  586. if (!point)
  587. return;
  588. if (point->meth->point_finish != 0)
  589. point->meth->point_finish(point);
  590. OPENSSL_free(point);
  591. }
  592. void EC_POINT_clear_free(EC_POINT *point)
  593. {
  594. if (!point)
  595. return;
  596. if (point->meth->point_clear_finish != 0)
  597. point->meth->point_clear_finish(point);
  598. else if (point->meth->point_finish != 0)
  599. point->meth->point_finish(point);
  600. OPENSSL_clear_free(point, sizeof(*point));
  601. }
  602. int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
  603. {
  604. if (dest->meth->point_copy == 0) {
  605. ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  606. return 0;
  607. }
  608. if (dest->meth != src->meth
  609. || (dest->curve_name != src->curve_name
  610. && dest->curve_name != 0
  611. && src->curve_name != 0)) {
  612. ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
  613. return 0;
  614. }
  615. if (dest == src)
  616. return 1;
  617. return dest->meth->point_copy(dest, src);
  618. }
  619. EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
  620. {
  621. EC_POINT *t;
  622. int r;
  623. if (a == NULL)
  624. return NULL;
  625. t = EC_POINT_new(group);
  626. if (t == NULL)
  627. return NULL;
  628. r = EC_POINT_copy(t, a);
  629. if (!r) {
  630. EC_POINT_free(t);
  631. return NULL;
  632. }
  633. return t;
  634. }
  635. const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
  636. {
  637. return point->meth;
  638. }
  639. int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
  640. {
  641. if (group->meth->point_set_to_infinity == 0) {
  642. ECerr(EC_F_EC_POINT_SET_TO_INFINITY,
  643. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  644. return 0;
  645. }
  646. if (group->meth != point->meth) {
  647. ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
  648. return 0;
  649. }
  650. return group->meth->point_set_to_infinity(group, point);
  651. }
  652. int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
  653. EC_POINT *point, const BIGNUM *x,
  654. const BIGNUM *y, const BIGNUM *z,
  655. BN_CTX *ctx)
  656. {
  657. if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
  658. ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
  659. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  660. return 0;
  661. }
  662. if (!ec_point_is_compat(point, group)) {
  663. ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
  664. EC_R_INCOMPATIBLE_OBJECTS);
  665. return 0;
  666. }
  667. return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x,
  668. y, z, ctx);
  669. }
  670. int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
  671. const EC_POINT *point, BIGNUM *x,
  672. BIGNUM *y, BIGNUM *z,
  673. BN_CTX *ctx)
  674. {
  675. if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
  676. ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
  677. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  678. return 0;
  679. }
  680. if (!ec_point_is_compat(point, group)) {
  681. ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
  682. EC_R_INCOMPATIBLE_OBJECTS);
  683. return 0;
  684. }
  685. return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x,
  686. y, z, ctx);
  687. }
  688. int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
  689. const BIGNUM *x, const BIGNUM *y,
  690. BN_CTX *ctx)
  691. {
  692. if (group->meth->point_set_affine_coordinates == NULL) {
  693. ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES,
  694. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  695. return 0;
  696. }
  697. if (!ec_point_is_compat(point, group)) {
  698. ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
  699. return 0;
  700. }
  701. if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
  702. return 0;
  703. if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
  704. ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_POINT_IS_NOT_ON_CURVE);
  705. return 0;
  706. }
  707. return 1;
  708. }
  709. #if OPENSSL_API_COMPAT < 0x10200000L
  710. int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
  711. EC_POINT *point, const BIGNUM *x,
  712. const BIGNUM *y, BN_CTX *ctx)
  713. {
  714. return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
  715. }
  716. # ifndef OPENSSL_NO_EC2M
  717. int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
  718. EC_POINT *point, const BIGNUM *x,
  719. const BIGNUM *y, BN_CTX *ctx)
  720. {
  721. return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
  722. }
  723. # endif
  724. #endif
  725. int EC_POINT_get_affine_coordinates(const EC_GROUP *group,
  726. const EC_POINT *point, BIGNUM *x, BIGNUM *y,
  727. BN_CTX *ctx)
  728. {
  729. if (group->meth->point_get_affine_coordinates == NULL) {
  730. ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES,
  731. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  732. return 0;
  733. }
  734. if (!ec_point_is_compat(point, group)) {
  735. ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
  736. return 0;
  737. }
  738. if (EC_POINT_is_at_infinity(group, point)) {
  739. ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_POINT_AT_INFINITY);
  740. return 0;
  741. }
  742. return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
  743. }
  744. #if OPENSSL_API_COMPAT < 0x10200000L
  745. int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
  746. const EC_POINT *point, BIGNUM *x,
  747. BIGNUM *y, BN_CTX *ctx)
  748. {
  749. return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
  750. }
  751. # ifndef OPENSSL_NO_EC2M
  752. int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
  753. const EC_POINT *point, BIGNUM *x,
  754. BIGNUM *y, BN_CTX *ctx)
  755. {
  756. return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
  757. }
  758. # endif
  759. #endif
  760. int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  761. const EC_POINT *b, BN_CTX *ctx)
  762. {
  763. if (group->meth->add == 0) {
  764. ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  765. return 0;
  766. }
  767. if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)
  768. || !ec_point_is_compat(b, group)) {
  769. ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
  770. return 0;
  771. }
  772. return group->meth->add(group, r, a, b, ctx);
  773. }
  774. int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  775. BN_CTX *ctx)
  776. {
  777. if (group->meth->dbl == 0) {
  778. ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  779. return 0;
  780. }
  781. if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) {
  782. ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
  783. return 0;
  784. }
  785. return group->meth->dbl(group, r, a, ctx);
  786. }
  787. int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
  788. {
  789. if (group->meth->invert == 0) {
  790. ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  791. return 0;
  792. }
  793. if (!ec_point_is_compat(a, group)) {
  794. ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
  795. return 0;
  796. }
  797. return group->meth->invert(group, a, ctx);
  798. }
  799. int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
  800. {
  801. if (group->meth->is_at_infinity == 0) {
  802. ECerr(EC_F_EC_POINT_IS_AT_INFINITY,
  803. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  804. return 0;
  805. }
  806. if (!ec_point_is_compat(point, group)) {
  807. ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
  808. return 0;
  809. }
  810. return group->meth->is_at_infinity(group, point);
  811. }
  812. /*
  813. * Check whether an EC_POINT is on the curve or not. Note that the return
  814. * value for this function should NOT be treated as a boolean. Return values:
  815. * 1: The point is on the curve
  816. * 0: The point is not on the curve
  817. * -1: An error occurred
  818. */
  819. int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
  820. BN_CTX *ctx)
  821. {
  822. if (group->meth->is_on_curve == 0) {
  823. ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  824. return 0;
  825. }
  826. if (!ec_point_is_compat(point, group)) {
  827. ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
  828. return 0;
  829. }
  830. return group->meth->is_on_curve(group, point, ctx);
  831. }
  832. int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
  833. BN_CTX *ctx)
  834. {
  835. if (group->meth->point_cmp == 0) {
  836. ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  837. return -1;
  838. }
  839. if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) {
  840. ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
  841. return -1;
  842. }
  843. return group->meth->point_cmp(group, a, b, ctx);
  844. }
  845. int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
  846. {
  847. if (group->meth->make_affine == 0) {
  848. ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  849. return 0;
  850. }
  851. if (!ec_point_is_compat(point, group)) {
  852. ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
  853. return 0;
  854. }
  855. return group->meth->make_affine(group, point, ctx);
  856. }
  857. int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
  858. EC_POINT *points[], BN_CTX *ctx)
  859. {
  860. size_t i;
  861. if (group->meth->points_make_affine == 0) {
  862. ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  863. return 0;
  864. }
  865. for (i = 0; i < num; i++) {
  866. if (!ec_point_is_compat(points[i], group)) {
  867. ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
  868. return 0;
  869. }
  870. }
  871. return group->meth->points_make_affine(group, num, points, ctx);
  872. }
  873. /*
  874. * Functions for point multiplication. If group->meth->mul is 0, we use the
  875. * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
  876. * methods.
  877. */
  878. int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
  879. size_t num, const EC_POINT *points[],
  880. const BIGNUM *scalars[], BN_CTX *ctx)
  881. {
  882. int ret = 0;
  883. size_t i = 0;
  884. BN_CTX *new_ctx = NULL;
  885. if (!ec_point_is_compat(r, group)) {
  886. ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
  887. return 0;
  888. }
  889. if (scalar == NULL && num == 0)
  890. return EC_POINT_set_to_infinity(group, r);
  891. for (i = 0; i < num; i++) {
  892. if (!ec_point_is_compat(points[i], group)) {
  893. ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
  894. return 0;
  895. }
  896. }
  897. if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL) {
  898. ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR);
  899. return 0;
  900. }
  901. if (group->meth->mul != NULL)
  902. ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);
  903. else
  904. /* use default */
  905. ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
  906. BN_CTX_free(new_ctx);
  907. return ret;
  908. }
  909. int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
  910. const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
  911. {
  912. /* just a convenient interface to EC_POINTs_mul() */
  913. const EC_POINT *points[1];
  914. const BIGNUM *scalars[1];
  915. points[0] = point;
  916. scalars[0] = p_scalar;
  917. return EC_POINTs_mul(group, r, g_scalar,
  918. (point != NULL
  919. && p_scalar != NULL), points, scalars, ctx);
  920. }
  921. int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
  922. {
  923. if (group->meth->mul == 0)
  924. /* use default */
  925. return ec_wNAF_precompute_mult(group, ctx);
  926. if (group->meth->precompute_mult != 0)
  927. return group->meth->precompute_mult(group, ctx);
  928. else
  929. return 1; /* nothing to do, so report success */
  930. }
  931. int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
  932. {
  933. if (group->meth->mul == 0)
  934. /* use default */
  935. return ec_wNAF_have_precompute_mult(group);
  936. if (group->meth->have_precompute_mult != 0)
  937. return group->meth->have_precompute_mult(group);
  938. else
  939. return 0; /* cannot tell whether precomputation has
  940. * been performed */
  941. }
  942. /*
  943. * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
  944. * returns one on success. On error it returns zero.
  945. */
  946. static int ec_precompute_mont_data(EC_GROUP *group)
  947. {
  948. BN_CTX *ctx = BN_CTX_new();
  949. int ret = 0;
  950. BN_MONT_CTX_free(group->mont_data);
  951. group->mont_data = NULL;
  952. if (ctx == NULL)
  953. goto err;
  954. group->mont_data = BN_MONT_CTX_new();
  955. if (group->mont_data == NULL)
  956. goto err;
  957. if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
  958. BN_MONT_CTX_free(group->mont_data);
  959. group->mont_data = NULL;
  960. goto err;
  961. }
  962. ret = 1;
  963. err:
  964. BN_CTX_free(ctx);
  965. return ret;
  966. }
  967. int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
  968. {
  969. return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
  970. }
  971. void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)
  972. {
  973. return CRYPTO_get_ex_data(&key->ex_data, idx);
  974. }
  975. int ec_group_simple_order_bits(const EC_GROUP *group)
  976. {
  977. if (group->order == NULL)
  978. return 0;
  979. return BN_num_bits(group->order);
  980. }
  981. static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r,
  982. const BIGNUM *x, BN_CTX *ctx)
  983. {
  984. BIGNUM *e = NULL;
  985. BN_CTX *new_ctx = NULL;
  986. int ret = 0;
  987. if (group->mont_data == NULL)
  988. return 0;
  989. if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL)
  990. return 0;
  991. BN_CTX_start(ctx);
  992. if ((e = BN_CTX_get(ctx)) == NULL)
  993. goto err;
  994. /*-
  995. * We want inverse in constant time, therefore we utilize the fact
  996. * order must be prime and use Fermats Little Theorem instead.
  997. */
  998. if (!BN_set_word(e, 2))
  999. goto err;
  1000. if (!BN_sub(e, group->order, e))
  1001. goto err;
  1002. /*-
  1003. * Exponent e is public.
  1004. * No need for scatter-gather or BN_FLG_CONSTTIME.
  1005. */
  1006. if (!BN_mod_exp_mont(r, x, e, group->order, ctx, group->mont_data))
  1007. goto err;
  1008. ret = 1;
  1009. err:
  1010. BN_CTX_end(ctx);
  1011. BN_CTX_free(new_ctx);
  1012. return ret;
  1013. }
  1014. /*-
  1015. * Default behavior, if group->meth->field_inverse_mod_ord is NULL:
  1016. * - When group->order is even, this function returns an error.
  1017. * - When group->order is otherwise composite, the correctness
  1018. * of the output is not guaranteed.
  1019. * - When x is outside the range [1, group->order), the correctness
  1020. * of the output is not guaranteed.
  1021. * - Otherwise, this function returns the multiplicative inverse in the
  1022. * range [1, group->order).
  1023. *
  1024. * EC_METHODs must implement their own field_inverse_mod_ord for
  1025. * other functionality.
  1026. */
  1027. int ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,
  1028. const BIGNUM *x, BN_CTX *ctx)
  1029. {
  1030. if (group->meth->field_inverse_mod_ord != NULL)
  1031. return group->meth->field_inverse_mod_ord(group, res, x, ctx);
  1032. else
  1033. return ec_field_inverse_mod_ord(group, res, x, ctx);
  1034. }
  1035. /*-
  1036. * Coordinate blinding for EC_POINT.
  1037. *
  1038. * The underlying EC_METHOD can optionally implement this function:
  1039. * underlying implementations should return 0 on errors, or 1 on
  1040. * success.
  1041. *
  1042. * This wrapper returns 1 in case the underlying EC_METHOD does not
  1043. * support coordinate blinding.
  1044. */
  1045. int ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx)
  1046. {
  1047. if (group->meth->blind_coordinates == NULL)
  1048. return 1; /* ignore if not implemented */
  1049. return group->meth->blind_coordinates(group, p, ctx);
  1050. }