ec_lib.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /*
  2. * Copyright 2001-2019 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_lcl.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. if (src->seed) {
  192. OPENSSL_free(dest->seed);
  193. if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL) {
  194. ECerr(EC_F_EC_GROUP_COPY, ERR_R_MALLOC_FAILURE);
  195. return 0;
  196. }
  197. if (!memcpy(dest->seed, src->seed, src->seed_len))
  198. return 0;
  199. dest->seed_len = src->seed_len;
  200. } else {
  201. OPENSSL_free(dest->seed);
  202. dest->seed = NULL;
  203. dest->seed_len = 0;
  204. }
  205. return dest->meth->group_copy(dest, src);
  206. }
  207. EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
  208. {
  209. EC_GROUP *t = NULL;
  210. int ok = 0;
  211. if (a == NULL)
  212. return NULL;
  213. if ((t = EC_GROUP_new(a->meth)) == NULL)
  214. return NULL;
  215. if (!EC_GROUP_copy(t, a))
  216. goto err;
  217. ok = 1;
  218. err:
  219. if (!ok) {
  220. EC_GROUP_free(t);
  221. return NULL;
  222. }
  223. return t;
  224. }
  225. const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
  226. {
  227. return group->meth;
  228. }
  229. int EC_METHOD_get_field_type(const EC_METHOD *meth)
  230. {
  231. return meth->field_type;
  232. }
  233. static int ec_precompute_mont_data(EC_GROUP *);
  234. int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
  235. const BIGNUM *order, const BIGNUM *cofactor)
  236. {
  237. if (generator == NULL) {
  238. ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
  239. return 0;
  240. }
  241. if (group->generator == NULL) {
  242. group->generator = EC_POINT_new(group);
  243. if (group->generator == NULL)
  244. return 0;
  245. }
  246. if (!EC_POINT_copy(group->generator, generator))
  247. return 0;
  248. if (order != NULL) {
  249. if (!BN_copy(group->order, order))
  250. return 0;
  251. } else
  252. BN_zero(group->order);
  253. if (cofactor != NULL) {
  254. if (!BN_copy(group->cofactor, cofactor))
  255. return 0;
  256. } else
  257. BN_zero(group->cofactor);
  258. /*
  259. * Some groups have an order with
  260. * factors of two, which makes the Montgomery setup fail.
  261. * |group->mont_data| will be NULL in this case.
  262. */
  263. if (BN_is_odd(group->order)) {
  264. return ec_precompute_mont_data(group);
  265. }
  266. BN_MONT_CTX_free(group->mont_data);
  267. group->mont_data = NULL;
  268. return 1;
  269. }
  270. const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
  271. {
  272. return group->generator;
  273. }
  274. BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
  275. {
  276. return group->mont_data;
  277. }
  278. int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
  279. {
  280. if (group->order == NULL)
  281. return 0;
  282. if (!BN_copy(order, group->order))
  283. return 0;
  284. return !BN_is_zero(order);
  285. }
  286. const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group)
  287. {
  288. return group->order;
  289. }
  290. int EC_GROUP_order_bits(const EC_GROUP *group)
  291. {
  292. return group->meth->group_order_bits(group);
  293. }
  294. int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
  295. BN_CTX *ctx)
  296. {
  297. if (group->cofactor == NULL)
  298. return 0;
  299. if (!BN_copy(cofactor, group->cofactor))
  300. return 0;
  301. return !BN_is_zero(group->cofactor);
  302. }
  303. const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group)
  304. {
  305. return group->cofactor;
  306. }
  307. void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
  308. {
  309. group->curve_name = nid;
  310. }
  311. int EC_GROUP_get_curve_name(const EC_GROUP *group)
  312. {
  313. return group->curve_name;
  314. }
  315. void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
  316. {
  317. group->asn1_flag = flag;
  318. }
  319. int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
  320. {
  321. return group->asn1_flag;
  322. }
  323. void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
  324. point_conversion_form_t form)
  325. {
  326. group->asn1_form = form;
  327. }
  328. point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
  329. *group)
  330. {
  331. return group->asn1_form;
  332. }
  333. size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
  334. {
  335. OPENSSL_free(group->seed);
  336. group->seed = NULL;
  337. group->seed_len = 0;
  338. if (!len || !p)
  339. return 1;
  340. if ((group->seed = OPENSSL_malloc(len)) == NULL) {
  341. ECerr(EC_F_EC_GROUP_SET_SEED, ERR_R_MALLOC_FAILURE);
  342. return 0;
  343. }
  344. memcpy(group->seed, p, len);
  345. group->seed_len = len;
  346. return len;
  347. }
  348. unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
  349. {
  350. return group->seed;
  351. }
  352. size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
  353. {
  354. return group->seed_len;
  355. }
  356. int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
  357. const BIGNUM *b, BN_CTX *ctx)
  358. {
  359. if (group->meth->group_set_curve == 0) {
  360. ECerr(EC_F_EC_GROUP_SET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  361. return 0;
  362. }
  363. return group->meth->group_set_curve(group, p, a, b, ctx);
  364. }
  365. int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b,
  366. BN_CTX *ctx)
  367. {
  368. if (group->meth->group_get_curve == NULL) {
  369. ECerr(EC_F_EC_GROUP_GET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  370. return 0;
  371. }
  372. return group->meth->group_get_curve(group, p, a, b, ctx);
  373. }
  374. #if OPENSSL_API_COMPAT < 0x10200000L
  375. int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
  376. const BIGNUM *b, BN_CTX *ctx)
  377. {
  378. return EC_GROUP_set_curve(group, p, a, b, ctx);
  379. }
  380. int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
  381. BIGNUM *b, BN_CTX *ctx)
  382. {
  383. return EC_GROUP_get_curve(group, p, a, b, ctx);
  384. }
  385. # ifndef OPENSSL_NO_EC2M
  386. int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
  387. const BIGNUM *b, BN_CTX *ctx)
  388. {
  389. return EC_GROUP_set_curve(group, p, a, b, ctx);
  390. }
  391. int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
  392. BIGNUM *b, BN_CTX *ctx)
  393. {
  394. return EC_GROUP_get_curve(group, p, a, b, ctx);
  395. }
  396. # endif
  397. #endif
  398. int EC_GROUP_get_degree(const EC_GROUP *group)
  399. {
  400. if (group->meth->group_get_degree == 0) {
  401. ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  402. return 0;
  403. }
  404. return group->meth->group_get_degree(group);
  405. }
  406. int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
  407. {
  408. if (group->meth->group_check_discriminant == 0) {
  409. ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
  410. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  411. return 0;
  412. }
  413. return group->meth->group_check_discriminant(group, ctx);
  414. }
  415. int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
  416. {
  417. int r = 0;
  418. BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
  419. BN_CTX *ctx_new = NULL;
  420. /* compare the field types */
  421. if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
  422. EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
  423. return 1;
  424. /* compare the curve name (if present in both) */
  425. if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
  426. EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
  427. return 1;
  428. if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE)
  429. return 0;
  430. if (ctx == NULL)
  431. ctx_new = ctx = BN_CTX_new();
  432. if (ctx == NULL)
  433. return -1;
  434. BN_CTX_start(ctx);
  435. a1 = BN_CTX_get(ctx);
  436. a2 = BN_CTX_get(ctx);
  437. a3 = BN_CTX_get(ctx);
  438. b1 = BN_CTX_get(ctx);
  439. b2 = BN_CTX_get(ctx);
  440. b3 = BN_CTX_get(ctx);
  441. if (b3 == NULL) {
  442. BN_CTX_end(ctx);
  443. BN_CTX_free(ctx_new);
  444. return -1;
  445. }
  446. /*
  447. * XXX This approach assumes that the external representation of curves
  448. * over the same field type is the same.
  449. */
  450. if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
  451. !b->meth->group_get_curve(b, b1, b2, b3, ctx))
  452. r = 1;
  453. if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
  454. r = 1;
  455. /* XXX EC_POINT_cmp() assumes that the methods are equal */
  456. if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
  457. EC_GROUP_get0_generator(b), ctx))
  458. r = 1;
  459. if (!r) {
  460. const BIGNUM *ao, *bo, *ac, *bc;
  461. /* compare the order and cofactor */
  462. ao = EC_GROUP_get0_order(a);
  463. bo = EC_GROUP_get0_order(b);
  464. ac = EC_GROUP_get0_cofactor(a);
  465. bc = EC_GROUP_get0_cofactor(b);
  466. if (ao == NULL || bo == NULL) {
  467. BN_CTX_end(ctx);
  468. BN_CTX_free(ctx_new);
  469. return -1;
  470. }
  471. if (BN_cmp(ao, bo) || BN_cmp(ac, bc))
  472. r = 1;
  473. }
  474. BN_CTX_end(ctx);
  475. BN_CTX_free(ctx_new);
  476. return r;
  477. }
  478. /* functions for EC_POINT objects */
  479. EC_POINT *EC_POINT_new(const EC_GROUP *group)
  480. {
  481. EC_POINT *ret;
  482. if (group == NULL) {
  483. ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
  484. return NULL;
  485. }
  486. if (group->meth->point_init == NULL) {
  487. ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  488. return NULL;
  489. }
  490. ret = OPENSSL_zalloc(sizeof(*ret));
  491. if (ret == NULL) {
  492. ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
  493. return NULL;
  494. }
  495. ret->meth = group->meth;
  496. ret->curve_name = group->curve_name;
  497. if (!ret->meth->point_init(ret)) {
  498. OPENSSL_free(ret);
  499. return NULL;
  500. }
  501. return ret;
  502. }
  503. void EC_POINT_free(EC_POINT *point)
  504. {
  505. if (!point)
  506. return;
  507. if (point->meth->point_finish != 0)
  508. point->meth->point_finish(point);
  509. OPENSSL_free(point);
  510. }
  511. void EC_POINT_clear_free(EC_POINT *point)
  512. {
  513. if (!point)
  514. return;
  515. if (point->meth->point_clear_finish != 0)
  516. point->meth->point_clear_finish(point);
  517. else if (point->meth->point_finish != 0)
  518. point->meth->point_finish(point);
  519. OPENSSL_clear_free(point, sizeof(*point));
  520. }
  521. int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
  522. {
  523. if (dest->meth->point_copy == 0) {
  524. ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  525. return 0;
  526. }
  527. if (dest->meth != src->meth
  528. || (dest->curve_name != src->curve_name
  529. && dest->curve_name != 0
  530. && src->curve_name != 0)) {
  531. ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
  532. return 0;
  533. }
  534. if (dest == src)
  535. return 1;
  536. return dest->meth->point_copy(dest, src);
  537. }
  538. EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
  539. {
  540. EC_POINT *t;
  541. int r;
  542. if (a == NULL)
  543. return NULL;
  544. t = EC_POINT_new(group);
  545. if (t == NULL)
  546. return NULL;
  547. r = EC_POINT_copy(t, a);
  548. if (!r) {
  549. EC_POINT_free(t);
  550. return NULL;
  551. }
  552. return t;
  553. }
  554. const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
  555. {
  556. return point->meth;
  557. }
  558. int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
  559. {
  560. if (group->meth->point_set_to_infinity == 0) {
  561. ECerr(EC_F_EC_POINT_SET_TO_INFINITY,
  562. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  563. return 0;
  564. }
  565. if (group->meth != point->meth) {
  566. ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
  567. return 0;
  568. }
  569. return group->meth->point_set_to_infinity(group, point);
  570. }
  571. int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
  572. EC_POINT *point, const BIGNUM *x,
  573. const BIGNUM *y, const BIGNUM *z,
  574. BN_CTX *ctx)
  575. {
  576. if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
  577. ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
  578. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  579. return 0;
  580. }
  581. if (!ec_point_is_compat(point, group)) {
  582. ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
  583. EC_R_INCOMPATIBLE_OBJECTS);
  584. return 0;
  585. }
  586. return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x,
  587. y, z, ctx);
  588. }
  589. int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
  590. const EC_POINT *point, BIGNUM *x,
  591. BIGNUM *y, BIGNUM *z,
  592. BN_CTX *ctx)
  593. {
  594. if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
  595. ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
  596. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  597. return 0;
  598. }
  599. if (!ec_point_is_compat(point, group)) {
  600. ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
  601. EC_R_INCOMPATIBLE_OBJECTS);
  602. return 0;
  603. }
  604. return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x,
  605. y, z, ctx);
  606. }
  607. int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
  608. const BIGNUM *x, const BIGNUM *y,
  609. BN_CTX *ctx)
  610. {
  611. if (group->meth->point_set_affine_coordinates == NULL) {
  612. ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES,
  613. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  614. return 0;
  615. }
  616. if (!ec_point_is_compat(point, group)) {
  617. ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
  618. return 0;
  619. }
  620. if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
  621. return 0;
  622. if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
  623. ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_POINT_IS_NOT_ON_CURVE);
  624. return 0;
  625. }
  626. return 1;
  627. }
  628. #if OPENSSL_API_COMPAT < 0x10200000L
  629. int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
  630. EC_POINT *point, const BIGNUM *x,
  631. const BIGNUM *y, BN_CTX *ctx)
  632. {
  633. return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
  634. }
  635. # ifndef OPENSSL_NO_EC2M
  636. int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
  637. EC_POINT *point, const BIGNUM *x,
  638. const BIGNUM *y, BN_CTX *ctx)
  639. {
  640. return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
  641. }
  642. # endif
  643. #endif
  644. int EC_POINT_get_affine_coordinates(const EC_GROUP *group,
  645. const EC_POINT *point, BIGNUM *x, BIGNUM *y,
  646. BN_CTX *ctx)
  647. {
  648. if (group->meth->point_get_affine_coordinates == NULL) {
  649. ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES,
  650. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  651. return 0;
  652. }
  653. if (!ec_point_is_compat(point, group)) {
  654. ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
  655. return 0;
  656. }
  657. if (EC_POINT_is_at_infinity(group, point)) {
  658. ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_POINT_AT_INFINITY);
  659. return 0;
  660. }
  661. return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
  662. }
  663. #if OPENSSL_API_COMPAT < 0x10200000L
  664. int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
  665. const EC_POINT *point, BIGNUM *x,
  666. BIGNUM *y, BN_CTX *ctx)
  667. {
  668. return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
  669. }
  670. # ifndef OPENSSL_NO_EC2M
  671. int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
  672. const EC_POINT *point, BIGNUM *x,
  673. BIGNUM *y, BN_CTX *ctx)
  674. {
  675. return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
  676. }
  677. # endif
  678. #endif
  679. int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  680. const EC_POINT *b, BN_CTX *ctx)
  681. {
  682. if (group->meth->add == 0) {
  683. ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  684. return 0;
  685. }
  686. if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)
  687. || !ec_point_is_compat(b, group)) {
  688. ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
  689. return 0;
  690. }
  691. return group->meth->add(group, r, a, b, ctx);
  692. }
  693. int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  694. BN_CTX *ctx)
  695. {
  696. if (group->meth->dbl == 0) {
  697. ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  698. return 0;
  699. }
  700. if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) {
  701. ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
  702. return 0;
  703. }
  704. return group->meth->dbl(group, r, a, ctx);
  705. }
  706. int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
  707. {
  708. if (group->meth->invert == 0) {
  709. ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  710. return 0;
  711. }
  712. if (!ec_point_is_compat(a, group)) {
  713. ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
  714. return 0;
  715. }
  716. return group->meth->invert(group, a, ctx);
  717. }
  718. int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
  719. {
  720. if (group->meth->is_at_infinity == 0) {
  721. ECerr(EC_F_EC_POINT_IS_AT_INFINITY,
  722. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  723. return 0;
  724. }
  725. if (!ec_point_is_compat(point, group)) {
  726. ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
  727. return 0;
  728. }
  729. return group->meth->is_at_infinity(group, point);
  730. }
  731. /*
  732. * Check whether an EC_POINT is on the curve or not. Note that the return
  733. * value for this function should NOT be treated as a boolean. Return values:
  734. * 1: The point is on the curve
  735. * 0: The point is not on the curve
  736. * -1: An error occurred
  737. */
  738. int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
  739. BN_CTX *ctx)
  740. {
  741. if (group->meth->is_on_curve == 0) {
  742. ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  743. return 0;
  744. }
  745. if (!ec_point_is_compat(point, group)) {
  746. ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
  747. return 0;
  748. }
  749. return group->meth->is_on_curve(group, point, ctx);
  750. }
  751. int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
  752. BN_CTX *ctx)
  753. {
  754. if (group->meth->point_cmp == 0) {
  755. ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  756. return -1;
  757. }
  758. if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) {
  759. ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
  760. return -1;
  761. }
  762. return group->meth->point_cmp(group, a, b, ctx);
  763. }
  764. int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
  765. {
  766. if (group->meth->make_affine == 0) {
  767. ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  768. return 0;
  769. }
  770. if (!ec_point_is_compat(point, group)) {
  771. ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
  772. return 0;
  773. }
  774. return group->meth->make_affine(group, point, ctx);
  775. }
  776. int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
  777. EC_POINT *points[], BN_CTX *ctx)
  778. {
  779. size_t i;
  780. if (group->meth->points_make_affine == 0) {
  781. ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  782. return 0;
  783. }
  784. for (i = 0; i < num; i++) {
  785. if (!ec_point_is_compat(points[i], group)) {
  786. ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
  787. return 0;
  788. }
  789. }
  790. return group->meth->points_make_affine(group, num, points, ctx);
  791. }
  792. /*
  793. * Functions for point multiplication. If group->meth->mul is 0, we use the
  794. * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
  795. * methods.
  796. */
  797. int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
  798. size_t num, const EC_POINT *points[],
  799. const BIGNUM *scalars[], BN_CTX *ctx)
  800. {
  801. int ret = 0;
  802. size_t i = 0;
  803. BN_CTX *new_ctx = NULL;
  804. if ((scalar == NULL) && (num == 0)) {
  805. return EC_POINT_set_to_infinity(group, r);
  806. }
  807. if (!ec_point_is_compat(r, group)) {
  808. ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
  809. return 0;
  810. }
  811. for (i = 0; i < num; i++) {
  812. if (!ec_point_is_compat(points[i], group)) {
  813. ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
  814. return 0;
  815. }
  816. }
  817. if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL) {
  818. ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR);
  819. return 0;
  820. }
  821. if (group->meth->mul != NULL)
  822. ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);
  823. else
  824. /* use default */
  825. ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
  826. BN_CTX_free(new_ctx);
  827. return ret;
  828. }
  829. int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
  830. const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
  831. {
  832. /* just a convenient interface to EC_POINTs_mul() */
  833. const EC_POINT *points[1];
  834. const BIGNUM *scalars[1];
  835. points[0] = point;
  836. scalars[0] = p_scalar;
  837. return EC_POINTs_mul(group, r, g_scalar,
  838. (point != NULL
  839. && p_scalar != NULL), points, scalars, ctx);
  840. }
  841. int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
  842. {
  843. if (group->meth->mul == 0)
  844. /* use default */
  845. return ec_wNAF_precompute_mult(group, ctx);
  846. if (group->meth->precompute_mult != 0)
  847. return group->meth->precompute_mult(group, ctx);
  848. else
  849. return 1; /* nothing to do, so report success */
  850. }
  851. int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
  852. {
  853. if (group->meth->mul == 0)
  854. /* use default */
  855. return ec_wNAF_have_precompute_mult(group);
  856. if (group->meth->have_precompute_mult != 0)
  857. return group->meth->have_precompute_mult(group);
  858. else
  859. return 0; /* cannot tell whether precomputation has
  860. * been performed */
  861. }
  862. /*
  863. * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
  864. * returns one on success. On error it returns zero.
  865. */
  866. static int ec_precompute_mont_data(EC_GROUP *group)
  867. {
  868. BN_CTX *ctx = BN_CTX_new();
  869. int ret = 0;
  870. BN_MONT_CTX_free(group->mont_data);
  871. group->mont_data = NULL;
  872. if (ctx == NULL)
  873. goto err;
  874. group->mont_data = BN_MONT_CTX_new();
  875. if (group->mont_data == NULL)
  876. goto err;
  877. if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
  878. BN_MONT_CTX_free(group->mont_data);
  879. group->mont_data = NULL;
  880. goto err;
  881. }
  882. ret = 1;
  883. err:
  884. BN_CTX_free(ctx);
  885. return ret;
  886. }
  887. int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
  888. {
  889. return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
  890. }
  891. void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)
  892. {
  893. return CRYPTO_get_ex_data(&key->ex_data, idx);
  894. }
  895. int ec_group_simple_order_bits(const EC_GROUP *group)
  896. {
  897. if (group->order == NULL)
  898. return 0;
  899. return BN_num_bits(group->order);
  900. }
  901. static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r,
  902. const BIGNUM *x, BN_CTX *ctx)
  903. {
  904. BIGNUM *e = NULL;
  905. BN_CTX *new_ctx = NULL;
  906. int ret = 0;
  907. if (group->mont_data == NULL)
  908. return 0;
  909. if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL)
  910. return 0;
  911. BN_CTX_start(ctx);
  912. if ((e = BN_CTX_get(ctx)) == NULL)
  913. goto err;
  914. /*-
  915. * We want inverse in constant time, therefore we utilize the fact
  916. * order must be prime and use Fermats Little Theorem instead.
  917. */
  918. if (!BN_set_word(e, 2))
  919. goto err;
  920. if (!BN_sub(e, group->order, e))
  921. goto err;
  922. /*-
  923. * Exponent e is public.
  924. * No need for scatter-gather or BN_FLG_CONSTTIME.
  925. */
  926. if (!BN_mod_exp_mont(r, x, e, group->order, ctx, group->mont_data))
  927. goto err;
  928. ret = 1;
  929. err:
  930. BN_CTX_end(ctx);
  931. BN_CTX_free(new_ctx);
  932. return ret;
  933. }
  934. /*-
  935. * Default behavior, if group->meth->field_inverse_mod_ord is NULL:
  936. * - When group->order is even, this function returns an error.
  937. * - When group->order is otherwise composite, the correctness
  938. * of the output is not guaranteed.
  939. * - When x is outside the range [1, group->order), the correctness
  940. * of the output is not guaranteed.
  941. * - Otherwise, this function returns the multiplicative inverse in the
  942. * range [1, group->order).
  943. *
  944. * EC_METHODs must implement their own field_inverse_mod_ord for
  945. * other functionality.
  946. */
  947. int ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,
  948. const BIGNUM *x, BN_CTX *ctx)
  949. {
  950. if (group->meth->field_inverse_mod_ord != NULL)
  951. return group->meth->field_inverse_mod_ord(group, res, x, ctx);
  952. else
  953. return ec_field_inverse_mod_ord(group, res, x, ctx);
  954. }
  955. /*-
  956. * Coordinate blinding for EC_POINT.
  957. *
  958. * The underlying EC_METHOD can optionally implement this function:
  959. * underlying implementations should return 0 on errors, or 1 on
  960. * success.
  961. *
  962. * This wrapper returns 1 in case the underlying EC_METHOD does not
  963. * support coordinate blinding.
  964. */
  965. int ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx)
  966. {
  967. if (group->meth->blind_coordinates == NULL)
  968. return 1; /* ignore if not implemented */
  969. return group->meth->blind_coordinates(group, p, ctx);
  970. }