ec_lib.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  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. /*-
  235. * Try computing cofactor from the generator order (n) and field cardinality (q).
  236. * This works for all curves of cryptographic interest.
  237. *
  238. * Hasse thm: q + 1 - 2*sqrt(q) <= n*h <= q + 1 + 2*sqrt(q)
  239. * h_min = (q + 1 - 2*sqrt(q))/n
  240. * h_max = (q + 1 + 2*sqrt(q))/n
  241. * h_max - h_min = 4*sqrt(q)/n
  242. * So if n > 4*sqrt(q) holds, there is only one possible value for h:
  243. * h = \lfloor (h_min + h_max)/2 \rceil = \lfloor (q + 1)/n \rceil
  244. *
  245. * Otherwise, zero cofactor and return success.
  246. */
  247. static int ec_guess_cofactor(EC_GROUP *group) {
  248. int ret = 0;
  249. BN_CTX *ctx = NULL;
  250. BIGNUM *q = NULL;
  251. /*-
  252. * If the cofactor is too large, we cannot guess it.
  253. * The RHS of below is a strict overestimate of lg(4 * sqrt(q))
  254. */
  255. if (BN_num_bits(group->order) <= (BN_num_bits(group->field) + 1) / 2 + 3) {
  256. /* default to 0 */
  257. BN_zero(group->cofactor);
  258. /* return success */
  259. return 1;
  260. }
  261. if ((ctx = BN_CTX_new()) == NULL)
  262. return 0;
  263. BN_CTX_start(ctx);
  264. if ((q = BN_CTX_get(ctx)) == NULL)
  265. goto err;
  266. /* set q = 2**m for binary fields; q = p otherwise */
  267. if (group->meth->field_type == NID_X9_62_characteristic_two_field) {
  268. BN_zero(q);
  269. if (!BN_set_bit(q, BN_num_bits(group->field) - 1))
  270. goto err;
  271. } else {
  272. if (!BN_copy(q, group->field))
  273. goto err;
  274. }
  275. /* compute h = \lfloor (q + 1)/n \rceil = \lfloor (q + 1 + n/2)/n \rfloor */
  276. if (!BN_rshift1(group->cofactor, group->order) /* n/2 */
  277. || !BN_add(group->cofactor, group->cofactor, q) /* q + n/2 */
  278. /* q + 1 + n/2 */
  279. || !BN_add(group->cofactor, group->cofactor, BN_value_one())
  280. /* (q + 1 + n/2)/n */
  281. || !BN_div(group->cofactor, NULL, group->cofactor, group->order, ctx))
  282. goto err;
  283. ret = 1;
  284. err:
  285. BN_CTX_end(ctx);
  286. BN_CTX_free(ctx);
  287. return ret;
  288. }
  289. int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
  290. const BIGNUM *order, const BIGNUM *cofactor)
  291. {
  292. if (generator == NULL) {
  293. ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
  294. return 0;
  295. }
  296. /* require group->field >= 1 */
  297. if (group->field == NULL || BN_is_zero(group->field)
  298. || BN_is_negative(group->field)) {
  299. ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_INVALID_FIELD);
  300. return 0;
  301. }
  302. /*-
  303. * - require order >= 1
  304. * - enforce upper bound due to Hasse thm: order can be no more than one bit
  305. * longer than field cardinality
  306. */
  307. if (order == NULL || BN_is_zero(order) || BN_is_negative(order)
  308. || BN_num_bits(order) > BN_num_bits(group->field) + 1) {
  309. ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_INVALID_GROUP_ORDER);
  310. return 0;
  311. }
  312. /*-
  313. * Unfortunately the cofactor is an optional field in many standards.
  314. * Internally, the lib uses 0 cofactor as a marker for "unknown cofactor".
  315. * So accept cofactor == NULL or cofactor >= 0.
  316. */
  317. if (cofactor != NULL && BN_is_negative(cofactor)) {
  318. ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_UNKNOWN_COFACTOR);
  319. return 0;
  320. }
  321. if (group->generator == NULL) {
  322. group->generator = EC_POINT_new(group);
  323. if (group->generator == NULL)
  324. return 0;
  325. }
  326. if (!EC_POINT_copy(group->generator, generator))
  327. return 0;
  328. if (!BN_copy(group->order, order))
  329. return 0;
  330. /* Either take the provided positive cofactor, or try to compute it */
  331. if (cofactor != NULL && !BN_is_zero(cofactor)) {
  332. if (!BN_copy(group->cofactor, cofactor))
  333. return 0;
  334. } else if (!ec_guess_cofactor(group)) {
  335. BN_zero(group->cofactor);
  336. return 0;
  337. }
  338. /*
  339. * Some groups have an order with
  340. * factors of two, which makes the Montgomery setup fail.
  341. * |group->mont_data| will be NULL in this case.
  342. */
  343. if (BN_is_odd(group->order)) {
  344. return ec_precompute_mont_data(group);
  345. }
  346. BN_MONT_CTX_free(group->mont_data);
  347. group->mont_data = NULL;
  348. return 1;
  349. }
  350. const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
  351. {
  352. return group->generator;
  353. }
  354. BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
  355. {
  356. return group->mont_data;
  357. }
  358. int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
  359. {
  360. if (group->order == NULL)
  361. return 0;
  362. if (!BN_copy(order, group->order))
  363. return 0;
  364. return !BN_is_zero(order);
  365. }
  366. const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group)
  367. {
  368. return group->order;
  369. }
  370. int EC_GROUP_order_bits(const EC_GROUP *group)
  371. {
  372. return group->meth->group_order_bits(group);
  373. }
  374. int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
  375. BN_CTX *ctx)
  376. {
  377. if (group->cofactor == NULL)
  378. return 0;
  379. if (!BN_copy(cofactor, group->cofactor))
  380. return 0;
  381. return !BN_is_zero(group->cofactor);
  382. }
  383. const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group)
  384. {
  385. return group->cofactor;
  386. }
  387. void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
  388. {
  389. group->curve_name = nid;
  390. }
  391. int EC_GROUP_get_curve_name(const EC_GROUP *group)
  392. {
  393. return group->curve_name;
  394. }
  395. void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
  396. {
  397. group->asn1_flag = flag;
  398. }
  399. int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
  400. {
  401. return group->asn1_flag;
  402. }
  403. void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
  404. point_conversion_form_t form)
  405. {
  406. group->asn1_form = form;
  407. }
  408. point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
  409. *group)
  410. {
  411. return group->asn1_form;
  412. }
  413. size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
  414. {
  415. OPENSSL_free(group->seed);
  416. group->seed = NULL;
  417. group->seed_len = 0;
  418. if (!len || !p)
  419. return 1;
  420. if ((group->seed = OPENSSL_malloc(len)) == NULL) {
  421. ECerr(EC_F_EC_GROUP_SET_SEED, ERR_R_MALLOC_FAILURE);
  422. return 0;
  423. }
  424. memcpy(group->seed, p, len);
  425. group->seed_len = len;
  426. return len;
  427. }
  428. unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
  429. {
  430. return group->seed;
  431. }
  432. size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
  433. {
  434. return group->seed_len;
  435. }
  436. int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
  437. const BIGNUM *b, BN_CTX *ctx)
  438. {
  439. if (group->meth->group_set_curve == 0) {
  440. ECerr(EC_F_EC_GROUP_SET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  441. return 0;
  442. }
  443. return group->meth->group_set_curve(group, p, a, b, ctx);
  444. }
  445. int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b,
  446. BN_CTX *ctx)
  447. {
  448. if (group->meth->group_get_curve == NULL) {
  449. ECerr(EC_F_EC_GROUP_GET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  450. return 0;
  451. }
  452. return group->meth->group_get_curve(group, p, a, b, ctx);
  453. }
  454. #if OPENSSL_API_COMPAT < 0x10200000L
  455. int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
  456. const BIGNUM *b, BN_CTX *ctx)
  457. {
  458. return EC_GROUP_set_curve(group, p, a, b, ctx);
  459. }
  460. int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
  461. BIGNUM *b, BN_CTX *ctx)
  462. {
  463. return EC_GROUP_get_curve(group, p, a, b, ctx);
  464. }
  465. # ifndef OPENSSL_NO_EC2M
  466. int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
  467. const BIGNUM *b, BN_CTX *ctx)
  468. {
  469. return EC_GROUP_set_curve(group, p, a, b, ctx);
  470. }
  471. int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
  472. BIGNUM *b, BN_CTX *ctx)
  473. {
  474. return EC_GROUP_get_curve(group, p, a, b, ctx);
  475. }
  476. # endif
  477. #endif
  478. int EC_GROUP_get_degree(const EC_GROUP *group)
  479. {
  480. if (group->meth->group_get_degree == 0) {
  481. ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  482. return 0;
  483. }
  484. return group->meth->group_get_degree(group);
  485. }
  486. int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
  487. {
  488. if (group->meth->group_check_discriminant == 0) {
  489. ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
  490. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  491. return 0;
  492. }
  493. return group->meth->group_check_discriminant(group, ctx);
  494. }
  495. int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
  496. {
  497. int r = 0;
  498. BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
  499. BN_CTX *ctx_new = NULL;
  500. /* compare the field types */
  501. if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
  502. EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
  503. return 1;
  504. /* compare the curve name (if present in both) */
  505. if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
  506. EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
  507. return 1;
  508. if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE)
  509. return 0;
  510. if (ctx == NULL)
  511. ctx_new = ctx = BN_CTX_new();
  512. if (ctx == NULL)
  513. return -1;
  514. BN_CTX_start(ctx);
  515. a1 = BN_CTX_get(ctx);
  516. a2 = BN_CTX_get(ctx);
  517. a3 = BN_CTX_get(ctx);
  518. b1 = BN_CTX_get(ctx);
  519. b2 = BN_CTX_get(ctx);
  520. b3 = BN_CTX_get(ctx);
  521. if (b3 == NULL) {
  522. BN_CTX_end(ctx);
  523. BN_CTX_free(ctx_new);
  524. return -1;
  525. }
  526. /*
  527. * XXX This approach assumes that the external representation of curves
  528. * over the same field type is the same.
  529. */
  530. if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
  531. !b->meth->group_get_curve(b, b1, b2, b3, ctx))
  532. r = 1;
  533. if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
  534. r = 1;
  535. /* XXX EC_POINT_cmp() assumes that the methods are equal */
  536. if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
  537. EC_GROUP_get0_generator(b), ctx))
  538. r = 1;
  539. if (!r) {
  540. const BIGNUM *ao, *bo, *ac, *bc;
  541. /* compare the order and cofactor */
  542. ao = EC_GROUP_get0_order(a);
  543. bo = EC_GROUP_get0_order(b);
  544. ac = EC_GROUP_get0_cofactor(a);
  545. bc = EC_GROUP_get0_cofactor(b);
  546. if (ao == NULL || bo == NULL) {
  547. BN_CTX_end(ctx);
  548. BN_CTX_free(ctx_new);
  549. return -1;
  550. }
  551. if (BN_cmp(ao, bo) || BN_cmp(ac, bc))
  552. r = 1;
  553. }
  554. BN_CTX_end(ctx);
  555. BN_CTX_free(ctx_new);
  556. return r;
  557. }
  558. /* functions for EC_POINT objects */
  559. EC_POINT *EC_POINT_new(const EC_GROUP *group)
  560. {
  561. EC_POINT *ret;
  562. if (group == NULL) {
  563. ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
  564. return NULL;
  565. }
  566. if (group->meth->point_init == NULL) {
  567. ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  568. return NULL;
  569. }
  570. ret = OPENSSL_zalloc(sizeof(*ret));
  571. if (ret == NULL) {
  572. ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
  573. return NULL;
  574. }
  575. ret->meth = group->meth;
  576. ret->curve_name = group->curve_name;
  577. if (!ret->meth->point_init(ret)) {
  578. OPENSSL_free(ret);
  579. return NULL;
  580. }
  581. return ret;
  582. }
  583. void EC_POINT_free(EC_POINT *point)
  584. {
  585. if (!point)
  586. return;
  587. if (point->meth->point_finish != 0)
  588. point->meth->point_finish(point);
  589. OPENSSL_free(point);
  590. }
  591. void EC_POINT_clear_free(EC_POINT *point)
  592. {
  593. if (!point)
  594. return;
  595. if (point->meth->point_clear_finish != 0)
  596. point->meth->point_clear_finish(point);
  597. else if (point->meth->point_finish != 0)
  598. point->meth->point_finish(point);
  599. OPENSSL_clear_free(point, sizeof(*point));
  600. }
  601. int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
  602. {
  603. if (dest->meth->point_copy == 0) {
  604. ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  605. return 0;
  606. }
  607. if (dest->meth != src->meth
  608. || (dest->curve_name != src->curve_name
  609. && dest->curve_name != 0
  610. && src->curve_name != 0)) {
  611. ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
  612. return 0;
  613. }
  614. if (dest == src)
  615. return 1;
  616. return dest->meth->point_copy(dest, src);
  617. }
  618. EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
  619. {
  620. EC_POINT *t;
  621. int r;
  622. if (a == NULL)
  623. return NULL;
  624. t = EC_POINT_new(group);
  625. if (t == NULL)
  626. return NULL;
  627. r = EC_POINT_copy(t, a);
  628. if (!r) {
  629. EC_POINT_free(t);
  630. return NULL;
  631. }
  632. return t;
  633. }
  634. const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
  635. {
  636. return point->meth;
  637. }
  638. int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
  639. {
  640. if (group->meth->point_set_to_infinity == 0) {
  641. ECerr(EC_F_EC_POINT_SET_TO_INFINITY,
  642. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  643. return 0;
  644. }
  645. if (group->meth != point->meth) {
  646. ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
  647. return 0;
  648. }
  649. return group->meth->point_set_to_infinity(group, point);
  650. }
  651. int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
  652. EC_POINT *point, const BIGNUM *x,
  653. const BIGNUM *y, const BIGNUM *z,
  654. BN_CTX *ctx)
  655. {
  656. if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
  657. ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
  658. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  659. return 0;
  660. }
  661. if (!ec_point_is_compat(point, group)) {
  662. ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
  663. EC_R_INCOMPATIBLE_OBJECTS);
  664. return 0;
  665. }
  666. return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x,
  667. y, z, ctx);
  668. }
  669. int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
  670. const EC_POINT *point, BIGNUM *x,
  671. BIGNUM *y, BIGNUM *z,
  672. BN_CTX *ctx)
  673. {
  674. if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
  675. ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
  676. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  677. return 0;
  678. }
  679. if (!ec_point_is_compat(point, group)) {
  680. ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
  681. EC_R_INCOMPATIBLE_OBJECTS);
  682. return 0;
  683. }
  684. return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x,
  685. y, z, ctx);
  686. }
  687. int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
  688. const BIGNUM *x, const BIGNUM *y,
  689. BN_CTX *ctx)
  690. {
  691. if (group->meth->point_set_affine_coordinates == NULL) {
  692. ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES,
  693. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  694. return 0;
  695. }
  696. if (!ec_point_is_compat(point, group)) {
  697. ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
  698. return 0;
  699. }
  700. if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))
  701. return 0;
  702. if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
  703. ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_POINT_IS_NOT_ON_CURVE);
  704. return 0;
  705. }
  706. return 1;
  707. }
  708. #if OPENSSL_API_COMPAT < 0x10200000L
  709. int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
  710. EC_POINT *point, const BIGNUM *x,
  711. const BIGNUM *y, BN_CTX *ctx)
  712. {
  713. return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
  714. }
  715. # ifndef OPENSSL_NO_EC2M
  716. int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
  717. EC_POINT *point, const BIGNUM *x,
  718. const BIGNUM *y, BN_CTX *ctx)
  719. {
  720. return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);
  721. }
  722. # endif
  723. #endif
  724. int EC_POINT_get_affine_coordinates(const EC_GROUP *group,
  725. const EC_POINT *point, BIGNUM *x, BIGNUM *y,
  726. BN_CTX *ctx)
  727. {
  728. if (group->meth->point_get_affine_coordinates == NULL) {
  729. ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES,
  730. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  731. return 0;
  732. }
  733. if (!ec_point_is_compat(point, group)) {
  734. ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
  735. return 0;
  736. }
  737. if (EC_POINT_is_at_infinity(group, point)) {
  738. ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_POINT_AT_INFINITY);
  739. return 0;
  740. }
  741. return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
  742. }
  743. #if OPENSSL_API_COMPAT < 0x10200000L
  744. int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
  745. const EC_POINT *point, BIGNUM *x,
  746. BIGNUM *y, BN_CTX *ctx)
  747. {
  748. return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
  749. }
  750. # ifndef OPENSSL_NO_EC2M
  751. int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
  752. const EC_POINT *point, BIGNUM *x,
  753. BIGNUM *y, BN_CTX *ctx)
  754. {
  755. return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);
  756. }
  757. # endif
  758. #endif
  759. int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  760. const EC_POINT *b, BN_CTX *ctx)
  761. {
  762. if (group->meth->add == 0) {
  763. ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  764. return 0;
  765. }
  766. if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)
  767. || !ec_point_is_compat(b, group)) {
  768. ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
  769. return 0;
  770. }
  771. return group->meth->add(group, r, a, b, ctx);
  772. }
  773. int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  774. BN_CTX *ctx)
  775. {
  776. if (group->meth->dbl == 0) {
  777. ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  778. return 0;
  779. }
  780. if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) {
  781. ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
  782. return 0;
  783. }
  784. return group->meth->dbl(group, r, a, ctx);
  785. }
  786. int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
  787. {
  788. if (group->meth->invert == 0) {
  789. ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  790. return 0;
  791. }
  792. if (!ec_point_is_compat(a, group)) {
  793. ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
  794. return 0;
  795. }
  796. return group->meth->invert(group, a, ctx);
  797. }
  798. int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
  799. {
  800. if (group->meth->is_at_infinity == 0) {
  801. ECerr(EC_F_EC_POINT_IS_AT_INFINITY,
  802. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  803. return 0;
  804. }
  805. if (!ec_point_is_compat(point, group)) {
  806. ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
  807. return 0;
  808. }
  809. return group->meth->is_at_infinity(group, point);
  810. }
  811. /*
  812. * Check whether an EC_POINT is on the curve or not. Note that the return
  813. * value for this function should NOT be treated as a boolean. Return values:
  814. * 1: The point is on the curve
  815. * 0: The point is not on the curve
  816. * -1: An error occurred
  817. */
  818. int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
  819. BN_CTX *ctx)
  820. {
  821. if (group->meth->is_on_curve == 0) {
  822. ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  823. return 0;
  824. }
  825. if (!ec_point_is_compat(point, group)) {
  826. ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
  827. return 0;
  828. }
  829. return group->meth->is_on_curve(group, point, ctx);
  830. }
  831. int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
  832. BN_CTX *ctx)
  833. {
  834. if (group->meth->point_cmp == 0) {
  835. ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  836. return -1;
  837. }
  838. if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) {
  839. ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
  840. return -1;
  841. }
  842. return group->meth->point_cmp(group, a, b, ctx);
  843. }
  844. int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
  845. {
  846. if (group->meth->make_affine == 0) {
  847. ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  848. return 0;
  849. }
  850. if (!ec_point_is_compat(point, group)) {
  851. ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
  852. return 0;
  853. }
  854. return group->meth->make_affine(group, point, ctx);
  855. }
  856. int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
  857. EC_POINT *points[], BN_CTX *ctx)
  858. {
  859. size_t i;
  860. if (group->meth->points_make_affine == 0) {
  861. ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  862. return 0;
  863. }
  864. for (i = 0; i < num; i++) {
  865. if (!ec_point_is_compat(points[i], group)) {
  866. ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
  867. return 0;
  868. }
  869. }
  870. return group->meth->points_make_affine(group, num, points, ctx);
  871. }
  872. /*
  873. * Functions for point multiplication. If group->meth->mul is 0, we use the
  874. * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
  875. * methods.
  876. */
  877. int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
  878. size_t num, const EC_POINT *points[],
  879. const BIGNUM *scalars[], BN_CTX *ctx)
  880. {
  881. int ret = 0;
  882. size_t i = 0;
  883. BN_CTX *new_ctx = NULL;
  884. if ((scalar == NULL) && (num == 0)) {
  885. return EC_POINT_set_to_infinity(group, r);
  886. }
  887. if (!ec_point_is_compat(r, group)) {
  888. ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
  889. return 0;
  890. }
  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. }