ec_lib.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. /*
  2. * Copyright 2001-2018 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. /* Copy precomputed */
  126. dest->pre_comp_type = src->pre_comp_type;
  127. switch (src->pre_comp_type) {
  128. case PCT_none:
  129. dest->pre_comp.ec = NULL;
  130. break;
  131. case PCT_nistz256:
  132. #ifdef ECP_NISTZ256_ASM
  133. dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);
  134. #endif
  135. break;
  136. #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  137. case PCT_nistp224:
  138. dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);
  139. break;
  140. case PCT_nistp256:
  141. dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);
  142. break;
  143. case PCT_nistp521:
  144. dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);
  145. break;
  146. #else
  147. case PCT_nistp224:
  148. case PCT_nistp256:
  149. case PCT_nistp521:
  150. break;
  151. #endif
  152. case PCT_ec:
  153. dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);
  154. break;
  155. }
  156. if (src->mont_data != NULL) {
  157. if (dest->mont_data == NULL) {
  158. dest->mont_data = BN_MONT_CTX_new();
  159. if (dest->mont_data == NULL)
  160. return 0;
  161. }
  162. if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))
  163. return 0;
  164. } else {
  165. /* src->generator == NULL */
  166. BN_MONT_CTX_free(dest->mont_data);
  167. dest->mont_data = NULL;
  168. }
  169. if (src->generator != NULL) {
  170. if (dest->generator == NULL) {
  171. dest->generator = EC_POINT_new(dest);
  172. if (dest->generator == NULL)
  173. return 0;
  174. }
  175. if (!EC_POINT_copy(dest->generator, src->generator))
  176. return 0;
  177. } else {
  178. /* src->generator == NULL */
  179. EC_POINT_clear_free(dest->generator);
  180. dest->generator = NULL;
  181. }
  182. if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
  183. if (!BN_copy(dest->order, src->order))
  184. return 0;
  185. if (!BN_copy(dest->cofactor, src->cofactor))
  186. return 0;
  187. }
  188. dest->curve_name = src->curve_name;
  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. return 0;
  342. memcpy(group->seed, p, len);
  343. group->seed_len = len;
  344. return len;
  345. }
  346. unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
  347. {
  348. return group->seed;
  349. }
  350. size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
  351. {
  352. return group->seed_len;
  353. }
  354. int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
  355. const BIGNUM *b, BN_CTX *ctx)
  356. {
  357. if (group->meth->group_set_curve == 0) {
  358. ECerr(EC_F_EC_GROUP_SET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  359. return 0;
  360. }
  361. return group->meth->group_set_curve(group, p, a, b, ctx);
  362. }
  363. int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
  364. BIGNUM *b, BN_CTX *ctx)
  365. {
  366. if (group->meth->group_get_curve == 0) {
  367. ECerr(EC_F_EC_GROUP_GET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  368. return 0;
  369. }
  370. return group->meth->group_get_curve(group, p, a, b, ctx);
  371. }
  372. #ifndef OPENSSL_NO_EC2M
  373. int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
  374. const BIGNUM *b, BN_CTX *ctx)
  375. {
  376. if (group->meth->group_set_curve == 0) {
  377. ECerr(EC_F_EC_GROUP_SET_CURVE_GF2M,
  378. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  379. return 0;
  380. }
  381. return group->meth->group_set_curve(group, p, a, b, ctx);
  382. }
  383. int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
  384. BIGNUM *b, BN_CTX *ctx)
  385. {
  386. if (group->meth->group_get_curve == 0) {
  387. ECerr(EC_F_EC_GROUP_GET_CURVE_GF2M,
  388. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  389. return 0;
  390. }
  391. return group->meth->group_get_curve(group, p, a, b, ctx);
  392. }
  393. #endif
  394. int EC_GROUP_get_degree(const EC_GROUP *group)
  395. {
  396. if (group->meth->group_get_degree == 0) {
  397. ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  398. return 0;
  399. }
  400. return group->meth->group_get_degree(group);
  401. }
  402. int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
  403. {
  404. if (group->meth->group_check_discriminant == 0) {
  405. ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
  406. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  407. return 0;
  408. }
  409. return group->meth->group_check_discriminant(group, ctx);
  410. }
  411. int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
  412. {
  413. int r = 0;
  414. BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
  415. BN_CTX *ctx_new = NULL;
  416. /* compare the field types */
  417. if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
  418. EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
  419. return 1;
  420. /* compare the curve name (if present in both) */
  421. if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
  422. EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
  423. return 1;
  424. if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE)
  425. return 0;
  426. if (ctx == NULL)
  427. ctx_new = ctx = BN_CTX_new();
  428. if (ctx == NULL)
  429. return -1;
  430. BN_CTX_start(ctx);
  431. a1 = BN_CTX_get(ctx);
  432. a2 = BN_CTX_get(ctx);
  433. a3 = BN_CTX_get(ctx);
  434. b1 = BN_CTX_get(ctx);
  435. b2 = BN_CTX_get(ctx);
  436. b3 = BN_CTX_get(ctx);
  437. if (b3 == NULL) {
  438. BN_CTX_end(ctx);
  439. BN_CTX_free(ctx_new);
  440. return -1;
  441. }
  442. /*
  443. * XXX This approach assumes that the external representation of curves
  444. * over the same field type is the same.
  445. */
  446. if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
  447. !b->meth->group_get_curve(b, b1, b2, b3, ctx))
  448. r = 1;
  449. if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
  450. r = 1;
  451. /* XXX EC_POINT_cmp() assumes that the methods are equal */
  452. if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
  453. EC_GROUP_get0_generator(b), ctx))
  454. r = 1;
  455. if (!r) {
  456. const BIGNUM *ao, *bo, *ac, *bc;
  457. /* compare the order and cofactor */
  458. ao = EC_GROUP_get0_order(a);
  459. bo = EC_GROUP_get0_order(b);
  460. ac = EC_GROUP_get0_cofactor(a);
  461. bc = EC_GROUP_get0_cofactor(b);
  462. if (ao == NULL || bo == NULL) {
  463. BN_CTX_end(ctx);
  464. BN_CTX_free(ctx_new);
  465. return -1;
  466. }
  467. if (BN_cmp(ao, bo) || BN_cmp(ac, bc))
  468. r = 1;
  469. }
  470. BN_CTX_end(ctx);
  471. BN_CTX_free(ctx_new);
  472. return r;
  473. }
  474. /* functions for EC_POINT objects */
  475. EC_POINT *EC_POINT_new(const EC_GROUP *group)
  476. {
  477. EC_POINT *ret;
  478. if (group == NULL) {
  479. ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
  480. return NULL;
  481. }
  482. if (group->meth->point_init == 0) {
  483. ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  484. return NULL;
  485. }
  486. ret = OPENSSL_zalloc(sizeof(*ret));
  487. if (ret == NULL) {
  488. ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
  489. return NULL;
  490. }
  491. ret->meth = group->meth;
  492. if (!ret->meth->point_init(ret)) {
  493. OPENSSL_free(ret);
  494. return NULL;
  495. }
  496. return ret;
  497. }
  498. void EC_POINT_free(EC_POINT *point)
  499. {
  500. if (!point)
  501. return;
  502. if (point->meth->point_finish != 0)
  503. point->meth->point_finish(point);
  504. OPENSSL_free(point);
  505. }
  506. void EC_POINT_clear_free(EC_POINT *point)
  507. {
  508. if (!point)
  509. return;
  510. if (point->meth->point_clear_finish != 0)
  511. point->meth->point_clear_finish(point);
  512. else if (point->meth->point_finish != 0)
  513. point->meth->point_finish(point);
  514. OPENSSL_clear_free(point, sizeof(*point));
  515. }
  516. int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
  517. {
  518. if (dest->meth->point_copy == 0) {
  519. ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  520. return 0;
  521. }
  522. if (dest->meth != src->meth) {
  523. ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
  524. return 0;
  525. }
  526. if (dest == src)
  527. return 1;
  528. return dest->meth->point_copy(dest, src);
  529. }
  530. EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
  531. {
  532. EC_POINT *t;
  533. int r;
  534. if (a == NULL)
  535. return NULL;
  536. t = EC_POINT_new(group);
  537. if (t == NULL)
  538. return NULL;
  539. r = EC_POINT_copy(t, a);
  540. if (!r) {
  541. EC_POINT_free(t);
  542. return NULL;
  543. }
  544. return t;
  545. }
  546. const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
  547. {
  548. return point->meth;
  549. }
  550. int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
  551. {
  552. if (group->meth->point_set_to_infinity == 0) {
  553. ECerr(EC_F_EC_POINT_SET_TO_INFINITY,
  554. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  555. return 0;
  556. }
  557. if (group->meth != point->meth) {
  558. ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
  559. return 0;
  560. }
  561. return group->meth->point_set_to_infinity(group, point);
  562. }
  563. int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
  564. EC_POINT *point, const BIGNUM *x,
  565. const BIGNUM *y, const BIGNUM *z,
  566. BN_CTX *ctx)
  567. {
  568. if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
  569. ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
  570. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  571. return 0;
  572. }
  573. if (group->meth != point->meth) {
  574. ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
  575. EC_R_INCOMPATIBLE_OBJECTS);
  576. return 0;
  577. }
  578. return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x,
  579. y, z, ctx);
  580. }
  581. int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
  582. const EC_POINT *point, BIGNUM *x,
  583. BIGNUM *y, BIGNUM *z,
  584. BN_CTX *ctx)
  585. {
  586. if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
  587. ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
  588. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  589. return 0;
  590. }
  591. if (group->meth != point->meth) {
  592. ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
  593. EC_R_INCOMPATIBLE_OBJECTS);
  594. return 0;
  595. }
  596. return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x,
  597. y, z, ctx);
  598. }
  599. int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
  600. EC_POINT *point, const BIGNUM *x,
  601. const BIGNUM *y, BN_CTX *ctx)
  602. {
  603. if (group->meth->point_set_affine_coordinates == 0) {
  604. ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
  605. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  606. return 0;
  607. }
  608. if (group->meth != point->meth) {
  609. ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
  610. EC_R_INCOMPATIBLE_OBJECTS);
  611. return 0;
  612. }
  613. if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
  614. return 0;
  615. if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
  616. ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
  617. EC_R_POINT_IS_NOT_ON_CURVE);
  618. return 0;
  619. }
  620. return 1;
  621. }
  622. #ifndef OPENSSL_NO_EC2M
  623. int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
  624. EC_POINT *point, const BIGNUM *x,
  625. const BIGNUM *y, BN_CTX *ctx)
  626. {
  627. if (group->meth->point_set_affine_coordinates == 0) {
  628. ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
  629. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  630. return 0;
  631. }
  632. if (group->meth != point->meth) {
  633. ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
  634. EC_R_INCOMPATIBLE_OBJECTS);
  635. return 0;
  636. }
  637. if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
  638. return 0;
  639. if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
  640. ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
  641. EC_R_POINT_IS_NOT_ON_CURVE);
  642. return 0;
  643. }
  644. return 1;
  645. }
  646. #endif
  647. int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
  648. const EC_POINT *point, BIGNUM *x,
  649. BIGNUM *y, BN_CTX *ctx)
  650. {
  651. if (group->meth->point_get_affine_coordinates == 0) {
  652. ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP,
  653. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  654. return 0;
  655. }
  656. if (group->meth != point->meth) {
  657. ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP,
  658. EC_R_INCOMPATIBLE_OBJECTS);
  659. return 0;
  660. }
  661. return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
  662. }
  663. #ifndef OPENSSL_NO_EC2M
  664. int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
  665. const EC_POINT *point, BIGNUM *x,
  666. BIGNUM *y, BN_CTX *ctx)
  667. {
  668. if (group->meth->point_get_affine_coordinates == 0) {
  669. ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M,
  670. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  671. return 0;
  672. }
  673. if (group->meth != point->meth) {
  674. ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M,
  675. EC_R_INCOMPATIBLE_OBJECTS);
  676. return 0;
  677. }
  678. return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
  679. }
  680. #endif
  681. int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  682. const EC_POINT *b, BN_CTX *ctx)
  683. {
  684. if (group->meth->add == 0) {
  685. ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  686. return 0;
  687. }
  688. if ((group->meth != r->meth) || (r->meth != a->meth)
  689. || (a->meth != b->meth)) {
  690. ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
  691. return 0;
  692. }
  693. return group->meth->add(group, r, a, b, ctx);
  694. }
  695. int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  696. BN_CTX *ctx)
  697. {
  698. if (group->meth->dbl == 0) {
  699. ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  700. return 0;
  701. }
  702. if ((group->meth != r->meth) || (r->meth != a->meth)) {
  703. ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
  704. return 0;
  705. }
  706. return group->meth->dbl(group, r, a, ctx);
  707. }
  708. int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
  709. {
  710. if (group->meth->invert == 0) {
  711. ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  712. return 0;
  713. }
  714. if (group->meth != a->meth) {
  715. ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
  716. return 0;
  717. }
  718. return group->meth->invert(group, a, ctx);
  719. }
  720. int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
  721. {
  722. if (group->meth->is_at_infinity == 0) {
  723. ECerr(EC_F_EC_POINT_IS_AT_INFINITY,
  724. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  725. return 0;
  726. }
  727. if (group->meth != point->meth) {
  728. ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
  729. return 0;
  730. }
  731. return group->meth->is_at_infinity(group, point);
  732. }
  733. /*
  734. * Check whether an EC_POINT is on the curve or not. Note that the return
  735. * value for this function should NOT be treated as a boolean. Return values:
  736. * 1: The point is on the curve
  737. * 0: The point is not on the curve
  738. * -1: An error occurred
  739. */
  740. int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
  741. BN_CTX *ctx)
  742. {
  743. if (group->meth->is_on_curve == 0) {
  744. ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  745. return 0;
  746. }
  747. if (group->meth != point->meth) {
  748. ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
  749. return 0;
  750. }
  751. return group->meth->is_on_curve(group, point, ctx);
  752. }
  753. int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
  754. BN_CTX *ctx)
  755. {
  756. if (group->meth->point_cmp == 0) {
  757. ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  758. return -1;
  759. }
  760. if ((group->meth != a->meth) || (a->meth != b->meth)) {
  761. ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
  762. return -1;
  763. }
  764. return group->meth->point_cmp(group, a, b, ctx);
  765. }
  766. int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
  767. {
  768. if (group->meth->make_affine == 0) {
  769. ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  770. return 0;
  771. }
  772. if (group->meth != point->meth) {
  773. ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
  774. return 0;
  775. }
  776. return group->meth->make_affine(group, point, ctx);
  777. }
  778. int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
  779. EC_POINT *points[], BN_CTX *ctx)
  780. {
  781. size_t i;
  782. if (group->meth->points_make_affine == 0) {
  783. ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  784. return 0;
  785. }
  786. for (i = 0; i < num; i++) {
  787. if (group->meth != points[i]->meth) {
  788. ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
  789. return 0;
  790. }
  791. }
  792. return group->meth->points_make_affine(group, num, points, ctx);
  793. }
  794. /*
  795. * Functions for point multiplication. If group->meth->mul is 0, we use the
  796. * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
  797. * methods.
  798. */
  799. int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
  800. size_t num, const EC_POINT *points[],
  801. const BIGNUM *scalars[], BN_CTX *ctx)
  802. {
  803. if (group->meth->mul == 0)
  804. /* use default */
  805. return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
  806. return group->meth->mul(group, r, scalar, num, points, scalars, ctx);
  807. }
  808. int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
  809. const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
  810. {
  811. /* just a convenient interface to EC_POINTs_mul() */
  812. const EC_POINT *points[1];
  813. const BIGNUM *scalars[1];
  814. points[0] = point;
  815. scalars[0] = p_scalar;
  816. return EC_POINTs_mul(group, r, g_scalar,
  817. (point != NULL
  818. && p_scalar != NULL), points, scalars, ctx);
  819. }
  820. int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
  821. {
  822. if (group->meth->mul == 0)
  823. /* use default */
  824. return ec_wNAF_precompute_mult(group, ctx);
  825. if (group->meth->precompute_mult != 0)
  826. return group->meth->precompute_mult(group, ctx);
  827. else
  828. return 1; /* nothing to do, so report success */
  829. }
  830. int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
  831. {
  832. if (group->meth->mul == 0)
  833. /* use default */
  834. return ec_wNAF_have_precompute_mult(group);
  835. if (group->meth->have_precompute_mult != 0)
  836. return group->meth->have_precompute_mult(group);
  837. else
  838. return 0; /* cannot tell whether precomputation has
  839. * been performed */
  840. }
  841. /*
  842. * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
  843. * returns one on success. On error it returns zero.
  844. */
  845. static int ec_precompute_mont_data(EC_GROUP *group)
  846. {
  847. BN_CTX *ctx = BN_CTX_new();
  848. int ret = 0;
  849. BN_MONT_CTX_free(group->mont_data);
  850. group->mont_data = NULL;
  851. if (ctx == NULL)
  852. goto err;
  853. group->mont_data = BN_MONT_CTX_new();
  854. if (group->mont_data == NULL)
  855. goto err;
  856. if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {
  857. BN_MONT_CTX_free(group->mont_data);
  858. group->mont_data = NULL;
  859. goto err;
  860. }
  861. ret = 1;
  862. err:
  863. BN_CTX_free(ctx);
  864. return ret;
  865. }
  866. int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
  867. {
  868. return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
  869. }
  870. void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)
  871. {
  872. return CRYPTO_get_ex_data(&key->ex_data, idx);
  873. }
  874. int ec_group_simple_order_bits(const EC_GROUP *group)
  875. {
  876. if (group->order == NULL)
  877. return 0;
  878. return BN_num_bits(group->order);
  879. }
  880. int EC_GROUP_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,
  881. BIGNUM *x, BN_CTX *ctx)
  882. {
  883. if (group->meth->field_inverse_mod_ord != NULL)
  884. return group->meth->field_inverse_mod_ord(group, res, x, ctx);
  885. else
  886. return 0;
  887. }