ec_lib.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  1. /* crypto/ec/ec_lib.c */
  2. /*
  3. * Originally written by Bodo Moeller for the OpenSSL project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * openssl-core@openssl.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. /* ====================================================================
  59. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  60. * Binary polynomial ECC support in OpenSSL originally developed by
  61. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  62. */
  63. #include <string.h>
  64. #include <openssl/err.h>
  65. #include <openssl/opensslv.h>
  66. #include "ec_lcl.h"
  67. const char EC_version[] = "EC" OPENSSL_VERSION_PTEXT;
  68. /* functions for EC_GROUP objects */
  69. EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
  70. {
  71. EC_GROUP *ret;
  72. if (meth == NULL) {
  73. ECerr(EC_F_EC_GROUP_NEW, EC_R_SLOT_FULL);
  74. return NULL;
  75. }
  76. if (meth->group_init == 0) {
  77. ECerr(EC_F_EC_GROUP_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  78. return NULL;
  79. }
  80. ret = OPENSSL_malloc(sizeof *ret);
  81. if (ret == NULL) {
  82. ECerr(EC_F_EC_GROUP_NEW, ERR_R_MALLOC_FAILURE);
  83. return NULL;
  84. }
  85. ret->meth = meth;
  86. ret->extra_data = NULL;
  87. ret->mont_data = NULL;
  88. ret->generator = NULL;
  89. BN_init(&ret->order);
  90. BN_init(&ret->cofactor);
  91. ret->curve_name = 0;
  92. ret->asn1_flag = ~EC_GROUP_ASN1_FLAG_MASK;
  93. ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
  94. ret->seed = NULL;
  95. ret->seed_len = 0;
  96. if (!meth->group_init(ret)) {
  97. OPENSSL_free(ret);
  98. return NULL;
  99. }
  100. return ret;
  101. }
  102. void EC_GROUP_free(EC_GROUP *group)
  103. {
  104. if (!group)
  105. return;
  106. if (group->meth->group_finish != 0)
  107. group->meth->group_finish(group);
  108. EC_EX_DATA_free_all_data(&group->extra_data);
  109. if (EC_GROUP_VERSION(group) && group->mont_data)
  110. BN_MONT_CTX_free(group->mont_data);
  111. if (group->generator != NULL)
  112. EC_POINT_free(group->generator);
  113. BN_free(&group->order);
  114. BN_free(&group->cofactor);
  115. if (group->seed)
  116. OPENSSL_free(group->seed);
  117. OPENSSL_free(group);
  118. }
  119. void EC_GROUP_clear_free(EC_GROUP *group)
  120. {
  121. if (!group)
  122. return;
  123. if (group->meth->group_clear_finish != 0)
  124. group->meth->group_clear_finish(group);
  125. else if (group->meth->group_finish != 0)
  126. group->meth->group_finish(group);
  127. EC_EX_DATA_clear_free_all_data(&group->extra_data);
  128. if (EC_GROUP_VERSION(group) && group->mont_data)
  129. BN_MONT_CTX_free(group->mont_data);
  130. if (group->generator != NULL)
  131. EC_POINT_clear_free(group->generator);
  132. BN_clear_free(&group->order);
  133. BN_clear_free(&group->cofactor);
  134. if (group->seed) {
  135. OPENSSL_cleanse(group->seed, group->seed_len);
  136. OPENSSL_free(group->seed);
  137. }
  138. OPENSSL_cleanse(group, sizeof *group);
  139. OPENSSL_free(group);
  140. }
  141. int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
  142. {
  143. EC_EXTRA_DATA *d;
  144. if (dest->meth->group_copy == 0) {
  145. ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  146. return 0;
  147. }
  148. if (dest->meth != src->meth) {
  149. ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS);
  150. return 0;
  151. }
  152. if (dest == src)
  153. return 1;
  154. EC_EX_DATA_free_all_data(&dest->extra_data);
  155. for (d = src->extra_data; d != NULL; d = d->next) {
  156. void *t = d->dup_func(d->data);
  157. if (t == NULL)
  158. return 0;
  159. if (!EC_EX_DATA_set_data
  160. (&dest->extra_data, t, d->dup_func, d->free_func,
  161. d->clear_free_func))
  162. return 0;
  163. }
  164. if (EC_GROUP_VERSION(src) && src->mont_data != NULL) {
  165. if (dest->mont_data == NULL) {
  166. dest->mont_data = BN_MONT_CTX_new();
  167. if (dest->mont_data == NULL)
  168. return 0;
  169. }
  170. if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))
  171. return 0;
  172. } else {
  173. /* src->generator == NULL */
  174. if (EC_GROUP_VERSION(dest) && dest->mont_data != NULL) {
  175. BN_MONT_CTX_free(dest->mont_data);
  176. dest->mont_data = NULL;
  177. }
  178. }
  179. if (src->generator != NULL) {
  180. if (dest->generator == NULL) {
  181. dest->generator = EC_POINT_new(dest);
  182. if (dest->generator == NULL)
  183. return 0;
  184. }
  185. if (!EC_POINT_copy(dest->generator, src->generator))
  186. return 0;
  187. } else {
  188. /* src->generator == NULL */
  189. if (dest->generator != NULL) {
  190. EC_POINT_clear_free(dest->generator);
  191. dest->generator = NULL;
  192. }
  193. }
  194. if (!BN_copy(&dest->order, &src->order))
  195. return 0;
  196. if (!BN_copy(&dest->cofactor, &src->cofactor))
  197. return 0;
  198. dest->curve_name = src->curve_name;
  199. dest->asn1_flag = src->asn1_flag;
  200. dest->asn1_form = src->asn1_form;
  201. if (src->seed) {
  202. if (dest->seed)
  203. OPENSSL_free(dest->seed);
  204. dest->seed = OPENSSL_malloc(src->seed_len);
  205. if (dest->seed == NULL)
  206. return 0;
  207. if (!memcpy(dest->seed, src->seed, src->seed_len))
  208. return 0;
  209. dest->seed_len = src->seed_len;
  210. } else {
  211. if (dest->seed)
  212. OPENSSL_free(dest->seed);
  213. dest->seed = NULL;
  214. dest->seed_len = 0;
  215. }
  216. return dest->meth->group_copy(dest, src);
  217. }
  218. EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
  219. {
  220. EC_GROUP *t = NULL;
  221. int ok = 0;
  222. if (a == NULL)
  223. return NULL;
  224. if ((t = EC_GROUP_new(a->meth)) == NULL)
  225. return (NULL);
  226. if (!EC_GROUP_copy(t, a))
  227. goto err;
  228. ok = 1;
  229. err:
  230. if (!ok) {
  231. if (t)
  232. EC_GROUP_free(t);
  233. return NULL;
  234. } else
  235. return t;
  236. }
  237. const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
  238. {
  239. return group->meth;
  240. }
  241. int EC_METHOD_get_field_type(const EC_METHOD *meth)
  242. {
  243. return meth->field_type;
  244. }
  245. int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
  246. const BIGNUM *order, const BIGNUM *cofactor)
  247. {
  248. if (generator == NULL) {
  249. ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
  250. return 0;
  251. }
  252. if (group->generator == NULL) {
  253. group->generator = EC_POINT_new(group);
  254. if (group->generator == NULL)
  255. return 0;
  256. }
  257. if (!EC_POINT_copy(group->generator, generator))
  258. return 0;
  259. if (order != NULL) {
  260. if (!BN_copy(&group->order, order))
  261. return 0;
  262. } else
  263. BN_zero(&group->order);
  264. if (cofactor != NULL) {
  265. if (!BN_copy(&group->cofactor, cofactor))
  266. return 0;
  267. } else
  268. BN_zero(&group->cofactor);
  269. /*
  270. * We ignore the return value because some groups have an order with
  271. * factors of two, which makes the Montgomery setup fail.
  272. * |group->mont_data| will be NULL in this case.
  273. */
  274. ec_precompute_mont_data(group);
  275. return 1;
  276. }
  277. const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
  278. {
  279. return group->generator;
  280. }
  281. BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
  282. {
  283. return EC_GROUP_VERSION(group) ? group->mont_data : NULL;
  284. }
  285. int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
  286. {
  287. if (!BN_copy(order, &group->order))
  288. return 0;
  289. return !BN_is_zero(order);
  290. }
  291. int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
  292. BN_CTX *ctx)
  293. {
  294. if (!BN_copy(cofactor, &group->cofactor))
  295. return 0;
  296. return !BN_is_zero(&group->cofactor);
  297. }
  298. void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
  299. {
  300. group->curve_name = nid;
  301. }
  302. int EC_GROUP_get_curve_name(const EC_GROUP *group)
  303. {
  304. return group->curve_name;
  305. }
  306. void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
  307. {
  308. group->asn1_flag &= ~EC_GROUP_ASN1_FLAG_MASK;
  309. group->asn1_flag |= flag & EC_GROUP_ASN1_FLAG_MASK;
  310. }
  311. int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
  312. {
  313. return group->asn1_flag & EC_GROUP_ASN1_FLAG_MASK;
  314. }
  315. void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
  316. point_conversion_form_t form)
  317. {
  318. group->asn1_form = form;
  319. }
  320. point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
  321. *group)
  322. {
  323. return group->asn1_form;
  324. }
  325. size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
  326. {
  327. if (group->seed) {
  328. OPENSSL_free(group->seed);
  329. group->seed = NULL;
  330. group->seed_len = 0;
  331. }
  332. if (!len || !p)
  333. return 1;
  334. if ((group->seed = OPENSSL_malloc(len)) == NULL)
  335. return 0;
  336. memcpy(group->seed, p, len);
  337. group->seed_len = len;
  338. return len;
  339. }
  340. unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
  341. {
  342. return group->seed;
  343. }
  344. size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
  345. {
  346. return group->seed_len;
  347. }
  348. int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
  349. const BIGNUM *b, BN_CTX *ctx)
  350. {
  351. if (group->meth->group_set_curve == 0) {
  352. ECerr(EC_F_EC_GROUP_SET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  353. return 0;
  354. }
  355. return group->meth->group_set_curve(group, p, a, b, ctx);
  356. }
  357. int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
  358. BIGNUM *b, BN_CTX *ctx)
  359. {
  360. if (group->meth->group_get_curve == 0) {
  361. ECerr(EC_F_EC_GROUP_GET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  362. return 0;
  363. }
  364. return group->meth->group_get_curve(group, p, a, b, ctx);
  365. }
  366. #ifndef OPENSSL_NO_EC2M
  367. int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
  368. const BIGNUM *b, BN_CTX *ctx)
  369. {
  370. if (group->meth->group_set_curve == 0) {
  371. ECerr(EC_F_EC_GROUP_SET_CURVE_GF2M,
  372. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  373. return 0;
  374. }
  375. return group->meth->group_set_curve(group, p, a, b, ctx);
  376. }
  377. int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
  378. BIGNUM *b, BN_CTX *ctx)
  379. {
  380. if (group->meth->group_get_curve == 0) {
  381. ECerr(EC_F_EC_GROUP_GET_CURVE_GF2M,
  382. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  383. return 0;
  384. }
  385. return group->meth->group_get_curve(group, p, a, b, ctx);
  386. }
  387. #endif
  388. int EC_GROUP_get_degree(const EC_GROUP *group)
  389. {
  390. if (group->meth->group_get_degree == 0) {
  391. ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  392. return 0;
  393. }
  394. return group->meth->group_get_degree(group);
  395. }
  396. int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
  397. {
  398. if (group->meth->group_check_discriminant == 0) {
  399. ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
  400. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  401. return 0;
  402. }
  403. return group->meth->group_check_discriminant(group, ctx);
  404. }
  405. int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
  406. {
  407. int r = 0;
  408. BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
  409. BN_CTX *ctx_new = NULL;
  410. /* compare the field types */
  411. if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
  412. EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
  413. return 1;
  414. /* compare the curve name (if present in both) */
  415. if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
  416. EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
  417. return 1;
  418. if (!ctx)
  419. ctx_new = ctx = BN_CTX_new();
  420. if (!ctx)
  421. return -1;
  422. BN_CTX_start(ctx);
  423. a1 = BN_CTX_get(ctx);
  424. a2 = BN_CTX_get(ctx);
  425. a3 = BN_CTX_get(ctx);
  426. b1 = BN_CTX_get(ctx);
  427. b2 = BN_CTX_get(ctx);
  428. b3 = BN_CTX_get(ctx);
  429. if (!b3) {
  430. BN_CTX_end(ctx);
  431. if (ctx_new)
  432. BN_CTX_free(ctx);
  433. return -1;
  434. }
  435. /*
  436. * XXX This approach assumes that the external representation of curves
  437. * over the same field type is the same.
  438. */
  439. if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
  440. !b->meth->group_get_curve(b, b1, b2, b3, ctx))
  441. r = 1;
  442. if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
  443. r = 1;
  444. /* XXX EC_POINT_cmp() assumes that the methods are equal */
  445. if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
  446. EC_GROUP_get0_generator(b), ctx))
  447. r = 1;
  448. if (!r) {
  449. /* compare the order and cofactor */
  450. if (!EC_GROUP_get_order(a, a1, ctx) ||
  451. !EC_GROUP_get_order(b, b1, ctx) ||
  452. !EC_GROUP_get_cofactor(a, a2, ctx) ||
  453. !EC_GROUP_get_cofactor(b, b2, ctx)) {
  454. BN_CTX_end(ctx);
  455. if (ctx_new)
  456. BN_CTX_free(ctx);
  457. return -1;
  458. }
  459. if (BN_cmp(a1, b1) || BN_cmp(a2, b2))
  460. r = 1;
  461. }
  462. BN_CTX_end(ctx);
  463. if (ctx_new)
  464. BN_CTX_free(ctx);
  465. return r;
  466. }
  467. /* this has 'package' visibility */
  468. int EC_EX_DATA_set_data(EC_EXTRA_DATA **ex_data, void *data,
  469. void *(*dup_func) (void *),
  470. void (*free_func) (void *),
  471. void (*clear_free_func) (void *))
  472. {
  473. EC_EXTRA_DATA *d;
  474. if (ex_data == NULL)
  475. return 0;
  476. for (d = *ex_data; d != NULL; d = d->next) {
  477. if (d->dup_func == dup_func && d->free_func == free_func
  478. && d->clear_free_func == clear_free_func) {
  479. ECerr(EC_F_EC_EX_DATA_SET_DATA, EC_R_SLOT_FULL);
  480. return 0;
  481. }
  482. }
  483. if (data == NULL)
  484. /* no explicit entry needed */
  485. return 1;
  486. d = OPENSSL_malloc(sizeof *d);
  487. if (d == NULL)
  488. return 0;
  489. d->data = data;
  490. d->dup_func = dup_func;
  491. d->free_func = free_func;
  492. d->clear_free_func = clear_free_func;
  493. d->next = *ex_data;
  494. *ex_data = d;
  495. return 1;
  496. }
  497. /* this has 'package' visibility */
  498. void *EC_EX_DATA_get_data(const EC_EXTRA_DATA *ex_data,
  499. void *(*dup_func) (void *),
  500. void (*free_func) (void *),
  501. void (*clear_free_func) (void *))
  502. {
  503. const EC_EXTRA_DATA *d;
  504. for (d = ex_data; d != NULL; d = d->next) {
  505. if (d->dup_func == dup_func && d->free_func == free_func
  506. && d->clear_free_func == clear_free_func)
  507. return d->data;
  508. }
  509. return NULL;
  510. }
  511. /* this has 'package' visibility */
  512. void EC_EX_DATA_free_data(EC_EXTRA_DATA **ex_data,
  513. void *(*dup_func) (void *),
  514. void (*free_func) (void *),
  515. void (*clear_free_func) (void *))
  516. {
  517. EC_EXTRA_DATA **p;
  518. if (ex_data == NULL)
  519. return;
  520. for (p = ex_data; *p != NULL; p = &((*p)->next)) {
  521. if ((*p)->dup_func == dup_func && (*p)->free_func == free_func
  522. && (*p)->clear_free_func == clear_free_func) {
  523. EC_EXTRA_DATA *next = (*p)->next;
  524. (*p)->free_func((*p)->data);
  525. OPENSSL_free(*p);
  526. *p = next;
  527. return;
  528. }
  529. }
  530. }
  531. /* this has 'package' visibility */
  532. void EC_EX_DATA_clear_free_data(EC_EXTRA_DATA **ex_data,
  533. void *(*dup_func) (void *),
  534. void (*free_func) (void *),
  535. void (*clear_free_func) (void *))
  536. {
  537. EC_EXTRA_DATA **p;
  538. if (ex_data == NULL)
  539. return;
  540. for (p = ex_data; *p != NULL; p = &((*p)->next)) {
  541. if ((*p)->dup_func == dup_func && (*p)->free_func == free_func
  542. && (*p)->clear_free_func == clear_free_func) {
  543. EC_EXTRA_DATA *next = (*p)->next;
  544. (*p)->clear_free_func((*p)->data);
  545. OPENSSL_free(*p);
  546. *p = next;
  547. return;
  548. }
  549. }
  550. }
  551. /* this has 'package' visibility */
  552. void EC_EX_DATA_free_all_data(EC_EXTRA_DATA **ex_data)
  553. {
  554. EC_EXTRA_DATA *d;
  555. if (ex_data == NULL)
  556. return;
  557. d = *ex_data;
  558. while (d) {
  559. EC_EXTRA_DATA *next = d->next;
  560. d->free_func(d->data);
  561. OPENSSL_free(d);
  562. d = next;
  563. }
  564. *ex_data = NULL;
  565. }
  566. /* this has 'package' visibility */
  567. void EC_EX_DATA_clear_free_all_data(EC_EXTRA_DATA **ex_data)
  568. {
  569. EC_EXTRA_DATA *d;
  570. if (ex_data == NULL)
  571. return;
  572. d = *ex_data;
  573. while (d) {
  574. EC_EXTRA_DATA *next = d->next;
  575. d->clear_free_func(d->data);
  576. OPENSSL_free(d);
  577. d = next;
  578. }
  579. *ex_data = NULL;
  580. }
  581. /* functions for EC_POINT objects */
  582. EC_POINT *EC_POINT_new(const EC_GROUP *group)
  583. {
  584. EC_POINT *ret;
  585. if (group == NULL) {
  586. ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
  587. return NULL;
  588. }
  589. if (group->meth->point_init == 0) {
  590. ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  591. return NULL;
  592. }
  593. ret = OPENSSL_malloc(sizeof *ret);
  594. if (ret == NULL) {
  595. ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
  596. return NULL;
  597. }
  598. ret->meth = group->meth;
  599. if (!ret->meth->point_init(ret)) {
  600. OPENSSL_free(ret);
  601. return NULL;
  602. }
  603. return ret;
  604. }
  605. void EC_POINT_free(EC_POINT *point)
  606. {
  607. if (!point)
  608. return;
  609. if (point->meth->point_finish != 0)
  610. point->meth->point_finish(point);
  611. OPENSSL_free(point);
  612. }
  613. void EC_POINT_clear_free(EC_POINT *point)
  614. {
  615. if (!point)
  616. return;
  617. if (point->meth->point_clear_finish != 0)
  618. point->meth->point_clear_finish(point);
  619. else if (point->meth->point_finish != 0)
  620. point->meth->point_finish(point);
  621. OPENSSL_cleanse(point, sizeof *point);
  622. OPENSSL_free(point);
  623. }
  624. int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
  625. {
  626. if (dest->meth->point_copy == 0) {
  627. ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  628. return 0;
  629. }
  630. if (dest->meth != src->meth) {
  631. ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
  632. return 0;
  633. }
  634. if (dest == src)
  635. return 1;
  636. return dest->meth->point_copy(dest, src);
  637. }
  638. EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
  639. {
  640. EC_POINT *t;
  641. int r;
  642. if (a == NULL)
  643. return NULL;
  644. t = EC_POINT_new(group);
  645. if (t == NULL)
  646. return (NULL);
  647. r = EC_POINT_copy(t, a);
  648. if (!r) {
  649. EC_POINT_free(t);
  650. return NULL;
  651. } else
  652. return t;
  653. }
  654. const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
  655. {
  656. return point->meth;
  657. }
  658. int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
  659. {
  660. if (group->meth->point_set_to_infinity == 0) {
  661. ECerr(EC_F_EC_POINT_SET_TO_INFINITY,
  662. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  663. return 0;
  664. }
  665. if (group->meth != point->meth) {
  666. ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
  667. return 0;
  668. }
  669. return group->meth->point_set_to_infinity(group, point);
  670. }
  671. int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
  672. EC_POINT *point, const BIGNUM *x,
  673. const BIGNUM *y, const BIGNUM *z,
  674. BN_CTX *ctx)
  675. {
  676. if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
  677. ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
  678. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  679. return 0;
  680. }
  681. if (group->meth != point->meth) {
  682. ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
  683. EC_R_INCOMPATIBLE_OBJECTS);
  684. return 0;
  685. }
  686. return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x,
  687. y, z, ctx);
  688. }
  689. int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
  690. const EC_POINT *point, BIGNUM *x,
  691. BIGNUM *y, BIGNUM *z,
  692. BN_CTX *ctx)
  693. {
  694. if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
  695. ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
  696. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  697. return 0;
  698. }
  699. if (group->meth != point->meth) {
  700. ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
  701. EC_R_INCOMPATIBLE_OBJECTS);
  702. return 0;
  703. }
  704. return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x,
  705. y, z, ctx);
  706. }
  707. int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
  708. EC_POINT *point, const BIGNUM *x,
  709. const BIGNUM *y, BN_CTX *ctx)
  710. {
  711. if (group->meth->point_set_affine_coordinates == 0) {
  712. ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
  713. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  714. return 0;
  715. }
  716. if (group->meth != point->meth) {
  717. ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
  718. EC_R_INCOMPATIBLE_OBJECTS);
  719. return 0;
  720. }
  721. return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
  722. }
  723. #ifndef OPENSSL_NO_EC2M
  724. int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
  725. EC_POINT *point, const BIGNUM *x,
  726. const BIGNUM *y, BN_CTX *ctx)
  727. {
  728. if (group->meth->point_set_affine_coordinates == 0) {
  729. ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
  730. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  731. return 0;
  732. }
  733. if (group->meth != point->meth) {
  734. ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
  735. EC_R_INCOMPATIBLE_OBJECTS);
  736. return 0;
  737. }
  738. return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
  739. }
  740. #endif
  741. int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
  742. const EC_POINT *point, BIGNUM *x,
  743. BIGNUM *y, BN_CTX *ctx)
  744. {
  745. if (group->meth->point_get_affine_coordinates == 0) {
  746. ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP,
  747. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  748. return 0;
  749. }
  750. if (group->meth != point->meth) {
  751. ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP,
  752. EC_R_INCOMPATIBLE_OBJECTS);
  753. return 0;
  754. }
  755. return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
  756. }
  757. #ifndef OPENSSL_NO_EC2M
  758. int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
  759. const EC_POINT *point, BIGNUM *x,
  760. BIGNUM *y, BN_CTX *ctx)
  761. {
  762. if (group->meth->point_get_affine_coordinates == 0) {
  763. ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M,
  764. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  765. return 0;
  766. }
  767. if (group->meth != point->meth) {
  768. ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M,
  769. EC_R_INCOMPATIBLE_OBJECTS);
  770. return 0;
  771. }
  772. return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
  773. }
  774. #endif
  775. int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  776. const EC_POINT *b, BN_CTX *ctx)
  777. {
  778. if (group->meth->add == 0) {
  779. ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  780. return 0;
  781. }
  782. if ((group->meth != r->meth) || (r->meth != a->meth)
  783. || (a->meth != b->meth)) {
  784. ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
  785. return 0;
  786. }
  787. return group->meth->add(group, r, a, b, ctx);
  788. }
  789. int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  790. BN_CTX *ctx)
  791. {
  792. if (group->meth->dbl == 0) {
  793. ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  794. return 0;
  795. }
  796. if ((group->meth != r->meth) || (r->meth != a->meth)) {
  797. ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
  798. return 0;
  799. }
  800. return group->meth->dbl(group, r, a, ctx);
  801. }
  802. int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
  803. {
  804. if (group->meth->invert == 0) {
  805. ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  806. return 0;
  807. }
  808. if (group->meth != a->meth) {
  809. ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
  810. return 0;
  811. }
  812. return group->meth->invert(group, a, ctx);
  813. }
  814. int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
  815. {
  816. if (group->meth->is_at_infinity == 0) {
  817. ECerr(EC_F_EC_POINT_IS_AT_INFINITY,
  818. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  819. return 0;
  820. }
  821. if (group->meth != point->meth) {
  822. ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
  823. return 0;
  824. }
  825. return group->meth->is_at_infinity(group, point);
  826. }
  827. /*
  828. * Check whether an EC_POINT is on the curve or not. Note that the return
  829. * value for this function should NOT be treated as a boolean. Return values:
  830. * 1: The point is on the curve
  831. * 0: The point is not on the curve
  832. * -1: An error occurred
  833. */
  834. int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
  835. BN_CTX *ctx)
  836. {
  837. if (group->meth->is_on_curve == 0) {
  838. ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  839. return 0;
  840. }
  841. if (group->meth != point->meth) {
  842. ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
  843. return 0;
  844. }
  845. return group->meth->is_on_curve(group, point, ctx);
  846. }
  847. int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
  848. BN_CTX *ctx)
  849. {
  850. if (group->meth->point_cmp == 0) {
  851. ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  852. return -1;
  853. }
  854. if ((group->meth != a->meth) || (a->meth != b->meth)) {
  855. ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
  856. return -1;
  857. }
  858. return group->meth->point_cmp(group, a, b, ctx);
  859. }
  860. int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
  861. {
  862. if (group->meth->make_affine == 0) {
  863. ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  864. return 0;
  865. }
  866. if (group->meth != point->meth) {
  867. ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
  868. return 0;
  869. }
  870. return group->meth->make_affine(group, point, ctx);
  871. }
  872. int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
  873. EC_POINT *points[], BN_CTX *ctx)
  874. {
  875. size_t i;
  876. if (group->meth->points_make_affine == 0) {
  877. ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  878. return 0;
  879. }
  880. for (i = 0; i < num; i++) {
  881. if (group->meth != points[i]->meth) {
  882. ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
  883. return 0;
  884. }
  885. }
  886. return group->meth->points_make_affine(group, num, points, ctx);
  887. }
  888. /*
  889. * Functions for point multiplication. If group->meth->mul is 0, we use the
  890. * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
  891. * methods.
  892. */
  893. int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
  894. size_t num, const EC_POINT *points[],
  895. const BIGNUM *scalars[], BN_CTX *ctx)
  896. {
  897. if (group->meth->mul == 0)
  898. /* use default */
  899. return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
  900. return group->meth->mul(group, r, scalar, num, points, scalars, ctx);
  901. }
  902. int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
  903. const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
  904. {
  905. /* just a convenient interface to EC_POINTs_mul() */
  906. const EC_POINT *points[1];
  907. const BIGNUM *scalars[1];
  908. points[0] = point;
  909. scalars[0] = p_scalar;
  910. return EC_POINTs_mul(group, r, g_scalar,
  911. (point != NULL
  912. && p_scalar != NULL), points, scalars, ctx);
  913. }
  914. int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
  915. {
  916. if (group->meth->mul == 0)
  917. /* use default */
  918. return ec_wNAF_precompute_mult(group, ctx);
  919. if (group->meth->precompute_mult != 0)
  920. return group->meth->precompute_mult(group, ctx);
  921. else
  922. return 1; /* nothing to do, so report success */
  923. }
  924. int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
  925. {
  926. if (group->meth->mul == 0)
  927. /* use default */
  928. return ec_wNAF_have_precompute_mult(group);
  929. if (group->meth->have_precompute_mult != 0)
  930. return group->meth->have_precompute_mult(group);
  931. else
  932. return 0; /* cannot tell whether precomputation has
  933. * been performed */
  934. }
  935. /*
  936. * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
  937. * returns one on success. On error it returns zero.
  938. */
  939. int ec_precompute_mont_data(EC_GROUP *group)
  940. {
  941. BN_CTX *ctx = BN_CTX_new();
  942. int ret = 0;
  943. if (!EC_GROUP_VERSION(group))
  944. goto err;
  945. if (group->mont_data) {
  946. BN_MONT_CTX_free(group->mont_data);
  947. group->mont_data = NULL;
  948. }
  949. if (ctx == NULL)
  950. goto err;
  951. group->mont_data = BN_MONT_CTX_new();
  952. if (!group->mont_data)
  953. goto err;
  954. if (!BN_MONT_CTX_set(group->mont_data, &group->order, ctx)) {
  955. BN_MONT_CTX_free(group->mont_data);
  956. group->mont_data = NULL;
  957. goto err;
  958. }
  959. ret = 1;
  960. err:
  961. if (ctx)
  962. BN_CTX_free(ctx);
  963. return ret;
  964. }