ec_lib.c 25 KB

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