ec_lib.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  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. static 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. {
  74. ECerr(EC_F_EC_GROUP_NEW, EC_R_SLOT_FULL);
  75. return NULL;
  76. }
  77. if (meth->group_init == 0)
  78. {
  79. ECerr(EC_F_EC_GROUP_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  80. return NULL;
  81. }
  82. ret = OPENSSL_malloc(sizeof *ret);
  83. if (ret == NULL)
  84. {
  85. ECerr(EC_F_EC_GROUP_NEW, ERR_R_MALLOC_FAILURE);
  86. return NULL;
  87. }
  88. ret->meth = meth;
  89. ret->extra_data = NULL;
  90. ret->generator = NULL;
  91. BN_init(&ret->order);
  92. BN_init(&ret->cofactor);
  93. ret->curve_name = 0;
  94. ret->asn1_flag = 0;
  95. ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
  96. ret->seed = NULL;
  97. ret->seed_len = 0;
  98. if (!meth->group_init(ret))
  99. {
  100. OPENSSL_free(ret);
  101. return NULL;
  102. }
  103. return ret;
  104. }
  105. void EC_GROUP_free(EC_GROUP *group)
  106. {
  107. if (!group) return;
  108. if (group->meth->group_finish != 0)
  109. group->meth->group_finish(group);
  110. EC_EX_DATA_free_all_data(&group->extra_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) return;
  122. if (group->meth->group_clear_finish != 0)
  123. group->meth->group_clear_finish(group);
  124. else if (group->meth->group_finish != 0)
  125. group->meth->group_finish(group);
  126. EC_EX_DATA_clear_free_all_data(&group->extra_data);
  127. if (group->generator != NULL)
  128. EC_POINT_clear_free(group->generator);
  129. BN_clear_free(&group->order);
  130. BN_clear_free(&group->cofactor);
  131. if (group->seed)
  132. {
  133. OPENSSL_cleanse(group->seed, group->seed_len);
  134. OPENSSL_free(group->seed);
  135. }
  136. OPENSSL_cleanse(group, sizeof *group);
  137. OPENSSL_free(group);
  138. }
  139. int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
  140. {
  141. EC_EXTRA_DATA *d;
  142. if (dest->meth->group_copy == 0)
  143. {
  144. ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  145. return 0;
  146. }
  147. if (dest->meth != src->meth)
  148. {
  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. {
  157. void *t = d->dup_func(d->data);
  158. if (t == NULL)
  159. return 0;
  160. if (!EC_EX_DATA_set_data(&dest->extra_data, t, d->dup_func, d->free_func, d->clear_free_func))
  161. return 0;
  162. }
  163. if (src->generator != NULL)
  164. {
  165. if (dest->generator == NULL)
  166. {
  167. dest->generator = EC_POINT_new(dest);
  168. if (dest->generator == NULL) return 0;
  169. }
  170. if (!EC_POINT_copy(dest->generator, src->generator)) return 0;
  171. }
  172. else
  173. {
  174. /* src->generator == NULL */
  175. if (dest->generator != NULL)
  176. {
  177. EC_POINT_clear_free(dest->generator);
  178. dest->generator = NULL;
  179. }
  180. }
  181. if (!BN_copy(&dest->order, &src->order)) return 0;
  182. if (!BN_copy(&dest->cofactor, &src->cofactor)) return 0;
  183. dest->curve_name = src->curve_name;
  184. dest->asn1_flag = src->asn1_flag;
  185. dest->asn1_form = src->asn1_form;
  186. if (src->seed)
  187. {
  188. if (dest->seed)
  189. OPENSSL_free(dest->seed);
  190. dest->seed = OPENSSL_malloc(src->seed_len);
  191. if (dest->seed == NULL)
  192. return 0;
  193. if (!memcpy(dest->seed, src->seed, src->seed_len))
  194. return 0;
  195. dest->seed_len = src->seed_len;
  196. }
  197. else
  198. {
  199. if (dest->seed)
  200. OPENSSL_free(dest->seed);
  201. dest->seed = NULL;
  202. dest->seed_len = 0;
  203. }
  204. return dest->meth->group_copy(dest, src);
  205. }
  206. EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)
  207. {
  208. EC_GROUP *t = NULL;
  209. int ok = 0;
  210. if (a == NULL) return NULL;
  211. if ((t = EC_GROUP_new(a->meth)) == NULL) return(NULL);
  212. if (!EC_GROUP_copy(t, a)) goto err;
  213. ok = 1;
  214. err:
  215. if (!ok)
  216. {
  217. if (t) EC_GROUP_free(t);
  218. return NULL;
  219. }
  220. else return t;
  221. }
  222. const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
  223. {
  224. return group->meth;
  225. }
  226. int EC_METHOD_get_field_type(const EC_METHOD *meth)
  227. {
  228. return meth->field_type;
  229. }
  230. int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator, const BIGNUM *order, const BIGNUM *cofactor)
  231. {
  232. if (generator == NULL)
  233. {
  234. ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
  235. return 0 ;
  236. }
  237. if (group->generator == NULL)
  238. {
  239. group->generator = EC_POINT_new(group);
  240. if (group->generator == NULL) return 0;
  241. }
  242. if (!EC_POINT_copy(group->generator, generator)) return 0;
  243. if (order != NULL)
  244. { if (!BN_copy(&group->order, order)) return 0; }
  245. else
  246. BN_zero(&group->order);
  247. if (cofactor != NULL)
  248. { if (!BN_copy(&group->cofactor, cofactor)) return 0; }
  249. else
  250. BN_zero(&group->cofactor);
  251. return 1;
  252. }
  253. const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
  254. {
  255. return group->generator;
  256. }
  257. int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
  258. {
  259. if (!BN_copy(order, &group->order))
  260. return 0;
  261. return !BN_is_zero(order);
  262. }
  263. int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor, BN_CTX *ctx)
  264. {
  265. if (!BN_copy(cofactor, &group->cofactor))
  266. return 0;
  267. return !BN_is_zero(&group->cofactor);
  268. }
  269. void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
  270. {
  271. group->curve_name = nid;
  272. }
  273. int EC_GROUP_get_curve_name(const EC_GROUP *group)
  274. {
  275. return group->curve_name;
  276. }
  277. void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
  278. {
  279. group->asn1_flag = flag;
  280. }
  281. int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
  282. {
  283. return group->asn1_flag;
  284. }
  285. void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
  286. point_conversion_form_t form)
  287. {
  288. group->asn1_form = form;
  289. }
  290. point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP *group)
  291. {
  292. return group->asn1_form;
  293. }
  294. size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
  295. {
  296. if (group->seed)
  297. {
  298. OPENSSL_free(group->seed);
  299. group->seed = NULL;
  300. group->seed_len = 0;
  301. }
  302. if (!len || !p)
  303. return 1;
  304. if ((group->seed = OPENSSL_malloc(len)) == NULL)
  305. return 0;
  306. memcpy(group->seed, p, len);
  307. group->seed_len = len;
  308. return len;
  309. }
  310. unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
  311. {
  312. return group->seed;
  313. }
  314. size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
  315. {
  316. return group->seed_len;
  317. }
  318. int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  319. {
  320. if (group->meth->group_set_curve == 0)
  321. {
  322. ECerr(EC_F_EC_GROUP_SET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  323. return 0;
  324. }
  325. return group->meth->group_set_curve(group, p, a, b, ctx);
  326. }
  327. int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
  328. {
  329. if (group->meth->group_get_curve == 0)
  330. {
  331. ECerr(EC_F_EC_GROUP_GET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  332. return 0;
  333. }
  334. return group->meth->group_get_curve(group, p, a, b, ctx);
  335. }
  336. int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  337. {
  338. if (group->meth->group_set_curve == 0)
  339. {
  340. ECerr(EC_F_EC_GROUP_SET_CURVE_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  341. return 0;
  342. }
  343. return group->meth->group_set_curve(group, p, a, b, ctx);
  344. }
  345. int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
  346. {
  347. if (group->meth->group_get_curve == 0)
  348. {
  349. ECerr(EC_F_EC_GROUP_GET_CURVE_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  350. return 0;
  351. }
  352. return group->meth->group_get_curve(group, p, a, b, ctx);
  353. }
  354. int EC_GROUP_get_degree(const EC_GROUP *group)
  355. {
  356. if (group->meth->group_get_degree == 0)
  357. {
  358. ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  359. return 0;
  360. }
  361. return group->meth->group_get_degree(group);
  362. }
  363. int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
  364. {
  365. if (group->meth->group_check_discriminant == 0)
  366. {
  367. ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  368. return 0;
  369. }
  370. return group->meth->group_check_discriminant(group, ctx);
  371. }
  372. int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
  373. {
  374. int r = 0;
  375. BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
  376. BN_CTX *ctx_new = NULL;
  377. /* compare the field types*/
  378. if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
  379. EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
  380. return 1;
  381. /* compare the curve name (if present) */
  382. if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
  383. EC_GROUP_get_curve_name(a) == EC_GROUP_get_curve_name(b))
  384. return 0;
  385. if (!ctx)
  386. ctx_new = ctx = BN_CTX_new();
  387. if (!ctx)
  388. return -1;
  389. BN_CTX_start(ctx);
  390. a1 = BN_CTX_get(ctx);
  391. a2 = BN_CTX_get(ctx);
  392. a3 = BN_CTX_get(ctx);
  393. b1 = BN_CTX_get(ctx);
  394. b2 = BN_CTX_get(ctx);
  395. b3 = BN_CTX_get(ctx);
  396. if (!b3)
  397. {
  398. BN_CTX_end(ctx);
  399. if (ctx_new)
  400. BN_CTX_free(ctx);
  401. return -1;
  402. }
  403. /* XXX This approach assumes that the external representation
  404. * of curves over the same field type is the same.
  405. */
  406. if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
  407. !b->meth->group_get_curve(b, b1, b2, b3, ctx))
  408. r = 1;
  409. if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
  410. r = 1;
  411. /* XXX EC_POINT_cmp() assumes that the methods are equal */
  412. if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
  413. EC_GROUP_get0_generator(b), ctx))
  414. r = 1;
  415. if (!r)
  416. {
  417. /* compare the order and cofactor */
  418. if (!EC_GROUP_get_order(a, a1, ctx) ||
  419. !EC_GROUP_get_order(b, b1, ctx) ||
  420. !EC_GROUP_get_cofactor(a, a2, ctx) ||
  421. !EC_GROUP_get_cofactor(b, b2, ctx))
  422. {
  423. BN_CTX_end(ctx);
  424. if (ctx_new)
  425. BN_CTX_free(ctx);
  426. return -1;
  427. }
  428. if (BN_cmp(a1, b1) || BN_cmp(a2, b2))
  429. r = 1;
  430. }
  431. BN_CTX_end(ctx);
  432. if (ctx_new)
  433. BN_CTX_free(ctx);
  434. return r;
  435. }
  436. /* this has 'package' visibility */
  437. int EC_EX_DATA_set_data(EC_EXTRA_DATA **ex_data, void *data,
  438. void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *))
  439. {
  440. EC_EXTRA_DATA *d;
  441. if (ex_data == NULL)
  442. return 0;
  443. for (d = *ex_data; d != NULL; d = d->next)
  444. {
  445. if (d->dup_func == dup_func && d->free_func == free_func && d->clear_free_func == clear_free_func)
  446. {
  447. ECerr(EC_F_EC_EX_DATA_SET_DATA, EC_R_SLOT_FULL);
  448. return 0;
  449. }
  450. }
  451. if (data == NULL)
  452. /* no explicit entry needed */
  453. return 1;
  454. d = OPENSSL_malloc(sizeof *d);
  455. if (d == NULL)
  456. return 0;
  457. d->data = data;
  458. d->dup_func = dup_func;
  459. d->free_func = free_func;
  460. d->clear_free_func = clear_free_func;
  461. d->next = *ex_data;
  462. *ex_data = d;
  463. return 1;
  464. }
  465. /* this has 'package' visibility */
  466. void *EC_EX_DATA_get_data(const EC_EXTRA_DATA *ex_data,
  467. void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *))
  468. {
  469. const EC_EXTRA_DATA *d;
  470. for (d = ex_data; d != NULL; d = d->next)
  471. {
  472. if (d->dup_func == dup_func && d->free_func == free_func && d->clear_free_func == clear_free_func)
  473. return d->data;
  474. }
  475. return NULL;
  476. }
  477. /* this has 'package' visibility */
  478. void EC_EX_DATA_free_data(EC_EXTRA_DATA **ex_data,
  479. void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *))
  480. {
  481. EC_EXTRA_DATA **p;
  482. if (ex_data == NULL)
  483. return;
  484. for (p = ex_data; *p != NULL; p = &((*p)->next))
  485. {
  486. if ((*p)->dup_func == dup_func && (*p)->free_func == free_func && (*p)->clear_free_func == clear_free_func)
  487. {
  488. EC_EXTRA_DATA *next = (*p)->next;
  489. (*p)->free_func((*p)->data);
  490. OPENSSL_free(*p);
  491. *p = next;
  492. return;
  493. }
  494. }
  495. }
  496. /* this has 'package' visibility */
  497. void EC_EX_DATA_clear_free_data(EC_EXTRA_DATA **ex_data,
  498. void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *))
  499. {
  500. EC_EXTRA_DATA **p;
  501. if (ex_data == NULL)
  502. return;
  503. for (p = ex_data; *p != NULL; p = &((*p)->next))
  504. {
  505. if ((*p)->dup_func == dup_func && (*p)->free_func == free_func && (*p)->clear_free_func == clear_free_func)
  506. {
  507. EC_EXTRA_DATA *next = (*p)->next;
  508. (*p)->clear_free_func((*p)->data);
  509. OPENSSL_free(*p);
  510. *p = next;
  511. return;
  512. }
  513. }
  514. }
  515. /* this has 'package' visibility */
  516. void EC_EX_DATA_free_all_data(EC_EXTRA_DATA **ex_data)
  517. {
  518. EC_EXTRA_DATA *d;
  519. if (ex_data == NULL)
  520. return;
  521. d = *ex_data;
  522. while (d)
  523. {
  524. EC_EXTRA_DATA *next = d->next;
  525. d->free_func(d->data);
  526. OPENSSL_free(d);
  527. d = next;
  528. }
  529. *ex_data = NULL;
  530. }
  531. /* this has 'package' visibility */
  532. void EC_EX_DATA_clear_free_all_data(EC_EXTRA_DATA **ex_data)
  533. {
  534. EC_EXTRA_DATA *d;
  535. if (ex_data == NULL)
  536. return;
  537. d = *ex_data;
  538. while (d)
  539. {
  540. EC_EXTRA_DATA *next = d->next;
  541. d->clear_free_func(d->data);
  542. OPENSSL_free(d);
  543. d = next;
  544. }
  545. *ex_data = NULL;
  546. }
  547. /* functions for EC_POINT objects */
  548. EC_POINT *EC_POINT_new(const EC_GROUP *group)
  549. {
  550. EC_POINT *ret;
  551. if (group == NULL)
  552. {
  553. ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
  554. return NULL;
  555. }
  556. if (group->meth->point_init == 0)
  557. {
  558. ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  559. return NULL;
  560. }
  561. ret = OPENSSL_malloc(sizeof *ret);
  562. if (ret == NULL)
  563. {
  564. ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
  565. return NULL;
  566. }
  567. ret->meth = group->meth;
  568. if (!ret->meth->point_init(ret))
  569. {
  570. OPENSSL_free(ret);
  571. return NULL;
  572. }
  573. return ret;
  574. }
  575. void EC_POINT_free(EC_POINT *point)
  576. {
  577. if (!point) return;
  578. if (point->meth->point_finish != 0)
  579. point->meth->point_finish(point);
  580. OPENSSL_free(point);
  581. }
  582. void EC_POINT_clear_free(EC_POINT *point)
  583. {
  584. if (!point) return;
  585. if (point->meth->point_clear_finish != 0)
  586. point->meth->point_clear_finish(point);
  587. else if (point->meth->point_finish != 0)
  588. point->meth->point_finish(point);
  589. OPENSSL_cleanse(point, sizeof *point);
  590. OPENSSL_free(point);
  591. }
  592. int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
  593. {
  594. if (dest->meth->point_copy == 0)
  595. {
  596. ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  597. return 0;
  598. }
  599. if (dest->meth != src->meth)
  600. {
  601. ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
  602. return 0;
  603. }
  604. if (dest == src)
  605. return 1;
  606. return dest->meth->point_copy(dest, src);
  607. }
  608. EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
  609. {
  610. EC_POINT *t;
  611. int r;
  612. if (a == NULL) return NULL;
  613. t = EC_POINT_new(group);
  614. if (t == NULL) return(NULL);
  615. r = EC_POINT_copy(t, a);
  616. if (!r)
  617. {
  618. EC_POINT_free(t);
  619. return NULL;
  620. }
  621. else return t;
  622. }
  623. const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
  624. {
  625. return point->meth;
  626. }
  627. int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
  628. {
  629. if (group->meth->point_set_to_infinity == 0)
  630. {
  631. ECerr(EC_F_EC_POINT_SET_TO_INFINITY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  632. return 0;
  633. }
  634. if (group->meth != point->meth)
  635. {
  636. ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
  637. return 0;
  638. }
  639. return group->meth->point_set_to_infinity(group, point);
  640. }
  641. int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
  642. const BIGNUM *x, const BIGNUM *y, const BIGNUM *z, BN_CTX *ctx)
  643. {
  644. if (group->meth->point_set_Jprojective_coordinates_GFp == 0)
  645. {
  646. ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  647. return 0;
  648. }
  649. if (group->meth != point->meth)
  650. {
  651. ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS);
  652. return 0;
  653. }
  654. return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x, y, z, ctx);
  655. }
  656. int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point,
  657. BIGNUM *x, BIGNUM *y, BIGNUM *z, BN_CTX *ctx)
  658. {
  659. if (group->meth->point_get_Jprojective_coordinates_GFp == 0)
  660. {
  661. ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  662. return 0;
  663. }
  664. if (group->meth != point->meth)
  665. {
  666. ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS);
  667. return 0;
  668. }
  669. return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x, y, z, ctx);
  670. }
  671. int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
  672. const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx)
  673. {
  674. if (group->meth->point_set_affine_coordinates == 0)
  675. {
  676. ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  677. return 0;
  678. }
  679. if (group->meth != point->meth)
  680. {
  681. ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS);
  682. return 0;
  683. }
  684. return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
  685. }
  686. int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group, EC_POINT *point,
  687. const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx)
  688. {
  689. if (group->meth->point_set_affine_coordinates == 0)
  690. {
  691. ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  692. return 0;
  693. }
  694. if (group->meth != point->meth)
  695. {
  696. ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M, EC_R_INCOMPATIBLE_OBJECTS);
  697. return 0;
  698. }
  699. return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
  700. }
  701. int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point,
  702. BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
  703. {
  704. if (group->meth->point_get_affine_coordinates == 0)
  705. {
  706. ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  707. return 0;
  708. }
  709. if (group->meth != point->meth)
  710. {
  711. ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS);
  712. return 0;
  713. }
  714. return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
  715. }
  716. int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group, const EC_POINT *point,
  717. BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
  718. {
  719. if (group->meth->point_get_affine_coordinates == 0)
  720. {
  721. ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  722. return 0;
  723. }
  724. if (group->meth != point->meth)
  725. {
  726. ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M, EC_R_INCOMPATIBLE_OBJECTS);
  727. return 0;
  728. }
  729. return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
  730. }
  731. int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
  732. const BIGNUM *x, int y_bit, BN_CTX *ctx)
  733. {
  734. if (group->meth->point_set_compressed_coordinates == 0)
  735. {
  736. ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  737. return 0;
  738. }
  739. if (group->meth != point->meth)
  740. {
  741. ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS);
  742. return 0;
  743. }
  744. return group->meth->point_set_compressed_coordinates(group, point, x, y_bit, ctx);
  745. }
  746. int EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group, EC_POINT *point,
  747. const BIGNUM *x, int y_bit, BN_CTX *ctx)
  748. {
  749. if (group->meth->point_set_compressed_coordinates == 0)
  750. {
  751. ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  752. return 0;
  753. }
  754. if (group->meth != point->meth)
  755. {
  756. ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M, EC_R_INCOMPATIBLE_OBJECTS);
  757. return 0;
  758. }
  759. return group->meth->point_set_compressed_coordinates(group, point, x, y_bit, ctx);
  760. }
  761. size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point, point_conversion_form_t form,
  762. unsigned char *buf, size_t len, BN_CTX *ctx)
  763. {
  764. if (group->meth->point2oct == 0)
  765. {
  766. ECerr(EC_F_EC_POINT_POINT2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  767. return 0;
  768. }
  769. if (group->meth != point->meth)
  770. {
  771. ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_INCOMPATIBLE_OBJECTS);
  772. return 0;
  773. }
  774. return group->meth->point2oct(group, point, form, buf, len, ctx);
  775. }
  776. int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
  777. const unsigned char *buf, size_t len, BN_CTX *ctx)
  778. {
  779. if (group->meth->oct2point == 0)
  780. {
  781. ECerr(EC_F_EC_POINT_OCT2POINT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  782. return 0;
  783. }
  784. if (group->meth != point->meth)
  785. {
  786. ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_INCOMPATIBLE_OBJECTS);
  787. return 0;
  788. }
  789. return group->meth->oct2point(group, point, buf, len, ctx);
  790. }
  791. int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
  792. {
  793. if (group->meth->add == 0)
  794. {
  795. ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  796. return 0;
  797. }
  798. if ((group->meth != r->meth) || (r->meth != a->meth) || (a->meth != b->meth))
  799. {
  800. ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
  801. return 0;
  802. }
  803. return group->meth->add(group, r, a, b, ctx);
  804. }
  805. int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, BN_CTX *ctx)
  806. {
  807. if (group->meth->dbl == 0)
  808. {
  809. ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  810. return 0;
  811. }
  812. if ((group->meth != r->meth) || (r->meth != a->meth))
  813. {
  814. ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
  815. return 0;
  816. }
  817. return group->meth->dbl(group, r, a, ctx);
  818. }
  819. int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
  820. {
  821. if (group->meth->dbl == 0)
  822. {
  823. ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  824. return 0;
  825. }
  826. if (group->meth != a->meth)
  827. {
  828. ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
  829. return 0;
  830. }
  831. return group->meth->invert(group, a, ctx);
  832. }
  833. int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
  834. {
  835. if (group->meth->is_at_infinity == 0)
  836. {
  837. ECerr(EC_F_EC_POINT_IS_AT_INFINITY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  838. return 0;
  839. }
  840. if (group->meth != point->meth)
  841. {
  842. ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
  843. return 0;
  844. }
  845. return group->meth->is_at_infinity(group, point);
  846. }
  847. int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point, BN_CTX *ctx)
  848. {
  849. if (group->meth->is_on_curve == 0)
  850. {
  851. ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  852. return 0;
  853. }
  854. if (group->meth != point->meth)
  855. {
  856. ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
  857. return 0;
  858. }
  859. return group->meth->is_on_curve(group, point, ctx);
  860. }
  861. int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
  862. {
  863. if (group->meth->point_cmp == 0)
  864. {
  865. ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  866. return 0;
  867. }
  868. if ((group->meth != a->meth) || (a->meth != b->meth))
  869. {
  870. ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
  871. return 0;
  872. }
  873. return group->meth->point_cmp(group, a, b, ctx);
  874. }
  875. int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
  876. {
  877. if (group->meth->make_affine == 0)
  878. {
  879. ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  880. return 0;
  881. }
  882. if (group->meth != point->meth)
  883. {
  884. ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
  885. return 0;
  886. }
  887. return group->meth->make_affine(group, point, ctx);
  888. }
  889. int EC_POINTs_make_affine(const EC_GROUP *group, size_t num, EC_POINT *points[], BN_CTX *ctx)
  890. {
  891. size_t i;
  892. if (group->meth->points_make_affine == 0)
  893. {
  894. ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  895. return 0;
  896. }
  897. for (i = 0; i < num; i++)
  898. {
  899. if (group->meth != points[i]->meth)
  900. {
  901. ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
  902. return 0;
  903. }
  904. }
  905. return group->meth->points_make_affine(group, num, points, ctx);
  906. }
  907. /* Functions for point multiplication.
  908. *
  909. * If group->meth->mul is 0, we use the wNAF-based implementations in ec_mult.c;
  910. * otherwise we dispatch through methods.
  911. */
  912. int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
  913. size_t num, const EC_POINT *points[], const BIGNUM *scalars[], BN_CTX *ctx)
  914. {
  915. if (group->meth->mul == 0)
  916. /* use default */
  917. return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
  918. return group->meth->mul(group, r, scalar, num, points, scalars, ctx);
  919. }
  920. int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
  921. const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
  922. {
  923. /* just a convenient interface to EC_POINTs_mul() */
  924. const EC_POINT *points[1];
  925. const BIGNUM *scalars[1];
  926. points[0] = point;
  927. scalars[0] = p_scalar;
  928. return EC_POINTs_mul(group, r, g_scalar, (point != NULL && p_scalar != NULL), points, scalars, ctx);
  929. }
  930. int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
  931. {
  932. if (group->meth->mul == 0)
  933. /* use default */
  934. return ec_wNAF_precompute_mult(group, ctx);
  935. if (group->meth->precompute_mult != 0)
  936. return group->meth->precompute_mult(group, ctx);
  937. else
  938. return 1; /* nothing to do, so report success */
  939. }
  940. int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
  941. {
  942. if (group->meth->mul == 0)
  943. /* use default */
  944. return ec_wNAF_have_precompute_mult(group);
  945. if (group->meth->have_precompute_mult != 0)
  946. return group->meth->have_precompute_mult(group);
  947. else
  948. return 0; /* cannot tell whether precomputation has been performed */
  949. }