ec_lib.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. /* crypto/ec/ec_lib.c */
  2. /*
  3. * Originally written by Bodo Moeller for the OpenSSL project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1998-2018 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. * [email protected].
  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. * ([email protected]). This product includes software written by Tim
  55. * Hudson ([email protected]).
  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. * 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. if (BN_is_odd(&group->order)) {
  275. return ec_precompute_mont_data(group);
  276. }
  277. BN_MONT_CTX_free(group->mont_data);
  278. group->mont_data = NULL;
  279. return 1;
  280. }
  281. const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
  282. {
  283. return group->generator;
  284. }
  285. BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
  286. {
  287. return EC_GROUP_VERSION(group) ? group->mont_data : NULL;
  288. }
  289. int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
  290. {
  291. if (!BN_copy(order, &group->order))
  292. return 0;
  293. return !BN_is_zero(order);
  294. }
  295. int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
  296. BN_CTX *ctx)
  297. {
  298. if (!BN_copy(cofactor, &group->cofactor))
  299. return 0;
  300. return !BN_is_zero(&group->cofactor);
  301. }
  302. void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)
  303. {
  304. group->curve_name = nid;
  305. }
  306. int EC_GROUP_get_curve_name(const EC_GROUP *group)
  307. {
  308. return group->curve_name;
  309. }
  310. void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)
  311. {
  312. group->asn1_flag &= ~EC_GROUP_ASN1_FLAG_MASK;
  313. group->asn1_flag |= flag & EC_GROUP_ASN1_FLAG_MASK;
  314. }
  315. int EC_GROUP_get_asn1_flag(const EC_GROUP *group)
  316. {
  317. return group->asn1_flag & EC_GROUP_ASN1_FLAG_MASK;
  318. }
  319. void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
  320. point_conversion_form_t form)
  321. {
  322. group->asn1_form = form;
  323. }
  324. point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP
  325. *group)
  326. {
  327. return group->asn1_form;
  328. }
  329. size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
  330. {
  331. if (group->seed) {
  332. OPENSSL_free(group->seed);
  333. group->seed = NULL;
  334. group->seed_len = 0;
  335. }
  336. if (!len || !p)
  337. return 1;
  338. if ((group->seed = OPENSSL_malloc(len)) == NULL)
  339. return 0;
  340. memcpy(group->seed, p, len);
  341. group->seed_len = len;
  342. return len;
  343. }
  344. unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)
  345. {
  346. return group->seed;
  347. }
  348. size_t EC_GROUP_get_seed_len(const EC_GROUP *group)
  349. {
  350. return group->seed_len;
  351. }
  352. int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
  353. const BIGNUM *b, BN_CTX *ctx)
  354. {
  355. if (group->meth->group_set_curve == 0) {
  356. ECerr(EC_F_EC_GROUP_SET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  357. return 0;
  358. }
  359. return group->meth->group_set_curve(group, p, a, b, ctx);
  360. }
  361. int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
  362. BIGNUM *b, BN_CTX *ctx)
  363. {
  364. if (group->meth->group_get_curve == 0) {
  365. ECerr(EC_F_EC_GROUP_GET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  366. return 0;
  367. }
  368. return group->meth->group_get_curve(group, p, a, b, ctx);
  369. }
  370. #ifndef OPENSSL_NO_EC2M
  371. int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
  372. const BIGNUM *b, BN_CTX *ctx)
  373. {
  374. if (group->meth->group_set_curve == 0) {
  375. ECerr(EC_F_EC_GROUP_SET_CURVE_GF2M,
  376. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  377. return 0;
  378. }
  379. return group->meth->group_set_curve(group, p, a, b, ctx);
  380. }
  381. int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
  382. BIGNUM *b, BN_CTX *ctx)
  383. {
  384. if (group->meth->group_get_curve == 0) {
  385. ECerr(EC_F_EC_GROUP_GET_CURVE_GF2M,
  386. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  387. return 0;
  388. }
  389. return group->meth->group_get_curve(group, p, a, b, ctx);
  390. }
  391. #endif
  392. int EC_GROUP_get_degree(const EC_GROUP *group)
  393. {
  394. if (group->meth->group_get_degree == 0) {
  395. ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  396. return 0;
  397. }
  398. return group->meth->group_get_degree(group);
  399. }
  400. int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
  401. {
  402. if (group->meth->group_check_discriminant == 0) {
  403. ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
  404. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  405. return 0;
  406. }
  407. return group->meth->group_check_discriminant(group, ctx);
  408. }
  409. int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
  410. {
  411. int r = 0;
  412. BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
  413. BN_CTX *ctx_new = NULL;
  414. /* compare the field types */
  415. if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
  416. EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
  417. return 1;
  418. /* compare the curve name (if present in both) */
  419. if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&
  420. EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))
  421. return 1;
  422. if (!ctx)
  423. ctx_new = ctx = BN_CTX_new();
  424. if (!ctx)
  425. return -1;
  426. BN_CTX_start(ctx);
  427. a1 = BN_CTX_get(ctx);
  428. a2 = BN_CTX_get(ctx);
  429. a3 = BN_CTX_get(ctx);
  430. b1 = BN_CTX_get(ctx);
  431. b2 = BN_CTX_get(ctx);
  432. b3 = BN_CTX_get(ctx);
  433. if (!b3) {
  434. BN_CTX_end(ctx);
  435. if (ctx_new)
  436. BN_CTX_free(ctx);
  437. return -1;
  438. }
  439. /*
  440. * XXX This approach assumes that the external representation of curves
  441. * over the same field type is the same.
  442. */
  443. if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
  444. !b->meth->group_get_curve(b, b1, b2, b3, ctx))
  445. r = 1;
  446. if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
  447. r = 1;
  448. /* XXX EC_POINT_cmp() assumes that the methods are equal */
  449. if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
  450. EC_GROUP_get0_generator(b), ctx))
  451. r = 1;
  452. if (!r) {
  453. /* compare the order and cofactor */
  454. if (!EC_GROUP_get_order(a, a1, ctx) ||
  455. !EC_GROUP_get_order(b, b1, ctx) ||
  456. !EC_GROUP_get_cofactor(a, a2, ctx) ||
  457. !EC_GROUP_get_cofactor(b, b2, ctx)) {
  458. BN_CTX_end(ctx);
  459. if (ctx_new)
  460. BN_CTX_free(ctx);
  461. return -1;
  462. }
  463. if (BN_cmp(a1, b1) || BN_cmp(a2, b2))
  464. r = 1;
  465. }
  466. BN_CTX_end(ctx);
  467. if (ctx_new)
  468. BN_CTX_free(ctx);
  469. return r;
  470. }
  471. /* this has 'package' visibility */
  472. int EC_EX_DATA_set_data(EC_EXTRA_DATA **ex_data, void *data,
  473. void *(*dup_func) (void *),
  474. void (*free_func) (void *),
  475. void (*clear_free_func) (void *))
  476. {
  477. EC_EXTRA_DATA *d;
  478. if (ex_data == NULL)
  479. return 0;
  480. for (d = *ex_data; d != NULL; d = d->next) {
  481. if (d->dup_func == dup_func && d->free_func == free_func
  482. && d->clear_free_func == clear_free_func) {
  483. ECerr(EC_F_EC_EX_DATA_SET_DATA, EC_R_SLOT_FULL);
  484. return 0;
  485. }
  486. }
  487. if (data == NULL)
  488. /* no explicit entry needed */
  489. return 1;
  490. d = OPENSSL_malloc(sizeof(*d));
  491. if (d == NULL)
  492. return 0;
  493. d->data = data;
  494. d->dup_func = dup_func;
  495. d->free_func = free_func;
  496. d->clear_free_func = clear_free_func;
  497. d->next = *ex_data;
  498. *ex_data = d;
  499. return 1;
  500. }
  501. /* this has 'package' visibility */
  502. void *EC_EX_DATA_get_data(const EC_EXTRA_DATA *ex_data,
  503. void *(*dup_func) (void *),
  504. void (*free_func) (void *),
  505. void (*clear_free_func) (void *))
  506. {
  507. const EC_EXTRA_DATA *d;
  508. for (d = ex_data; d != NULL; d = d->next) {
  509. if (d->dup_func == dup_func && d->free_func == free_func
  510. && d->clear_free_func == clear_free_func)
  511. return d->data;
  512. }
  513. return NULL;
  514. }
  515. /* this has 'package' visibility */
  516. void EC_EX_DATA_free_data(EC_EXTRA_DATA **ex_data,
  517. void *(*dup_func) (void *),
  518. void (*free_func) (void *),
  519. void (*clear_free_func) (void *))
  520. {
  521. EC_EXTRA_DATA **p;
  522. if (ex_data == NULL)
  523. return;
  524. for (p = ex_data; *p != NULL; p = &((*p)->next)) {
  525. if ((*p)->dup_func == dup_func && (*p)->free_func == free_func
  526. && (*p)->clear_free_func == clear_free_func) {
  527. EC_EXTRA_DATA *next = (*p)->next;
  528. (*p)->free_func((*p)->data);
  529. OPENSSL_free(*p);
  530. *p = next;
  531. return;
  532. }
  533. }
  534. }
  535. /* this has 'package' visibility */
  536. void EC_EX_DATA_clear_free_data(EC_EXTRA_DATA **ex_data,
  537. void *(*dup_func) (void *),
  538. void (*free_func) (void *),
  539. void (*clear_free_func) (void *))
  540. {
  541. EC_EXTRA_DATA **p;
  542. if (ex_data == NULL)
  543. return;
  544. for (p = ex_data; *p != NULL; p = &((*p)->next)) {
  545. if ((*p)->dup_func == dup_func && (*p)->free_func == free_func
  546. && (*p)->clear_free_func == clear_free_func) {
  547. EC_EXTRA_DATA *next = (*p)->next;
  548. (*p)->clear_free_func((*p)->data);
  549. OPENSSL_free(*p);
  550. *p = next;
  551. return;
  552. }
  553. }
  554. }
  555. /* this has 'package' visibility */
  556. void EC_EX_DATA_free_all_data(EC_EXTRA_DATA **ex_data)
  557. {
  558. EC_EXTRA_DATA *d;
  559. if (ex_data == NULL)
  560. return;
  561. d = *ex_data;
  562. while (d) {
  563. EC_EXTRA_DATA *next = d->next;
  564. d->free_func(d->data);
  565. OPENSSL_free(d);
  566. d = next;
  567. }
  568. *ex_data = NULL;
  569. }
  570. /* this has 'package' visibility */
  571. void EC_EX_DATA_clear_free_all_data(EC_EXTRA_DATA **ex_data)
  572. {
  573. EC_EXTRA_DATA *d;
  574. if (ex_data == NULL)
  575. return;
  576. d = *ex_data;
  577. while (d) {
  578. EC_EXTRA_DATA *next = d->next;
  579. d->clear_free_func(d->data);
  580. OPENSSL_free(d);
  581. d = next;
  582. }
  583. *ex_data = NULL;
  584. }
  585. /* functions for EC_POINT objects */
  586. EC_POINT *EC_POINT_new(const EC_GROUP *group)
  587. {
  588. EC_POINT *ret;
  589. if (group == NULL) {
  590. ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
  591. return NULL;
  592. }
  593. if (group->meth->point_init == 0) {
  594. ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  595. return NULL;
  596. }
  597. ret = OPENSSL_malloc(sizeof(*ret));
  598. if (ret == NULL) {
  599. ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
  600. return NULL;
  601. }
  602. ret->meth = group->meth;
  603. if (!ret->meth->point_init(ret)) {
  604. OPENSSL_free(ret);
  605. return NULL;
  606. }
  607. return ret;
  608. }
  609. void EC_POINT_free(EC_POINT *point)
  610. {
  611. if (!point)
  612. return;
  613. if (point->meth->point_finish != 0)
  614. point->meth->point_finish(point);
  615. OPENSSL_free(point);
  616. }
  617. void EC_POINT_clear_free(EC_POINT *point)
  618. {
  619. if (!point)
  620. return;
  621. if (point->meth->point_clear_finish != 0)
  622. point->meth->point_clear_finish(point);
  623. else if (point->meth->point_finish != 0)
  624. point->meth->point_finish(point);
  625. OPENSSL_cleanse(point, sizeof(*point));
  626. OPENSSL_free(point);
  627. }
  628. int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)
  629. {
  630. if (dest->meth->point_copy == 0) {
  631. ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  632. return 0;
  633. }
  634. if (dest->meth != src->meth) {
  635. ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
  636. return 0;
  637. }
  638. if (dest == src)
  639. return 1;
  640. return dest->meth->point_copy(dest, src);
  641. }
  642. EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)
  643. {
  644. EC_POINT *t;
  645. int r;
  646. if (a == NULL)
  647. return NULL;
  648. t = EC_POINT_new(group);
  649. if (t == NULL)
  650. return (NULL);
  651. r = EC_POINT_copy(t, a);
  652. if (!r) {
  653. EC_POINT_free(t);
  654. return NULL;
  655. } else
  656. return t;
  657. }
  658. const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
  659. {
  660. return point->meth;
  661. }
  662. int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
  663. {
  664. if (group->meth->point_set_to_infinity == 0) {
  665. ECerr(EC_F_EC_POINT_SET_TO_INFINITY,
  666. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  667. return 0;
  668. }
  669. if (group->meth != point->meth) {
  670. ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
  671. return 0;
  672. }
  673. return group->meth->point_set_to_infinity(group, point);
  674. }
  675. int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
  676. EC_POINT *point, const BIGNUM *x,
  677. const BIGNUM *y, const BIGNUM *z,
  678. BN_CTX *ctx)
  679. {
  680. if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
  681. ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
  682. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  683. return 0;
  684. }
  685. if (group->meth != point->meth) {
  686. ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
  687. EC_R_INCOMPATIBLE_OBJECTS);
  688. return 0;
  689. }
  690. return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x,
  691. y, z, ctx);
  692. }
  693. int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
  694. const EC_POINT *point, BIGNUM *x,
  695. BIGNUM *y, BIGNUM *z,
  696. BN_CTX *ctx)
  697. {
  698. if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
  699. ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
  700. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  701. return 0;
  702. }
  703. if (group->meth != point->meth) {
  704. ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
  705. EC_R_INCOMPATIBLE_OBJECTS);
  706. return 0;
  707. }
  708. return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x,
  709. y, z, ctx);
  710. }
  711. int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
  712. EC_POINT *point, const BIGNUM *x,
  713. const BIGNUM *y, BN_CTX *ctx)
  714. {
  715. if (group->meth->point_set_affine_coordinates == 0) {
  716. ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
  717. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  718. return 0;
  719. }
  720. if (group->meth != point->meth) {
  721. ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP,
  722. EC_R_INCOMPATIBLE_OBJECTS);
  723. return 0;
  724. }
  725. return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
  726. }
  727. #ifndef OPENSSL_NO_EC2M
  728. int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,
  729. EC_POINT *point, const BIGNUM *x,
  730. const BIGNUM *y, BN_CTX *ctx)
  731. {
  732. if (group->meth->point_set_affine_coordinates == 0) {
  733. ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
  734. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  735. return 0;
  736. }
  737. if (group->meth != point->meth) {
  738. ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M,
  739. EC_R_INCOMPATIBLE_OBJECTS);
  740. return 0;
  741. }
  742. return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
  743. }
  744. #endif
  745. int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
  746. const EC_POINT *point, BIGNUM *x,
  747. BIGNUM *y, BN_CTX *ctx)
  748. {
  749. if (group->meth->point_get_affine_coordinates == 0) {
  750. ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP,
  751. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  752. return 0;
  753. }
  754. if (group->meth != point->meth) {
  755. ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP,
  756. EC_R_INCOMPATIBLE_OBJECTS);
  757. return 0;
  758. }
  759. return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
  760. }
  761. #ifndef OPENSSL_NO_EC2M
  762. int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
  763. const EC_POINT *point, BIGNUM *x,
  764. BIGNUM *y, BN_CTX *ctx)
  765. {
  766. if (group->meth->point_get_affine_coordinates == 0) {
  767. ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M,
  768. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  769. return 0;
  770. }
  771. if (group->meth != point->meth) {
  772. ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M,
  773. EC_R_INCOMPATIBLE_OBJECTS);
  774. return 0;
  775. }
  776. return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
  777. }
  778. #endif
  779. int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  780. const EC_POINT *b, BN_CTX *ctx)
  781. {
  782. if (group->meth->add == 0) {
  783. ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  784. return 0;
  785. }
  786. if ((group->meth != r->meth) || (r->meth != a->meth)
  787. || (a->meth != b->meth)) {
  788. ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
  789. return 0;
  790. }
  791. return group->meth->add(group, r, a, b, ctx);
  792. }
  793. int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  794. BN_CTX *ctx)
  795. {
  796. if (group->meth->dbl == 0) {
  797. ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  798. return 0;
  799. }
  800. if ((group->meth != r->meth) || (r->meth != a->meth)) {
  801. ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
  802. return 0;
  803. }
  804. return group->meth->dbl(group, r, a, ctx);
  805. }
  806. int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
  807. {
  808. if (group->meth->invert == 0) {
  809. ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  810. return 0;
  811. }
  812. if (group->meth != a->meth) {
  813. ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
  814. return 0;
  815. }
  816. return group->meth->invert(group, a, ctx);
  817. }
  818. int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
  819. {
  820. if (group->meth->is_at_infinity == 0) {
  821. ECerr(EC_F_EC_POINT_IS_AT_INFINITY,
  822. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  823. return 0;
  824. }
  825. if (group->meth != point->meth) {
  826. ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
  827. return 0;
  828. }
  829. return group->meth->is_at_infinity(group, point);
  830. }
  831. /*
  832. * Check whether an EC_POINT is on the curve or not. Note that the return
  833. * value for this function should NOT be treated as a boolean. Return values:
  834. * 1: The point is on the curve
  835. * 0: The point is not on the curve
  836. * -1: An error occurred
  837. */
  838. int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
  839. BN_CTX *ctx)
  840. {
  841. if (group->meth->is_on_curve == 0) {
  842. ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  843. return 0;
  844. }
  845. if (group->meth != point->meth) {
  846. ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
  847. return 0;
  848. }
  849. return group->meth->is_on_curve(group, point, ctx);
  850. }
  851. int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
  852. BN_CTX *ctx)
  853. {
  854. if (group->meth->point_cmp == 0) {
  855. ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  856. return -1;
  857. }
  858. if ((group->meth != a->meth) || (a->meth != b->meth)) {
  859. ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
  860. return -1;
  861. }
  862. return group->meth->point_cmp(group, a, b, ctx);
  863. }
  864. int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
  865. {
  866. if (group->meth->make_affine == 0) {
  867. ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  868. return 0;
  869. }
  870. if (group->meth != point->meth) {
  871. ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
  872. return 0;
  873. }
  874. return group->meth->make_affine(group, point, ctx);
  875. }
  876. int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
  877. EC_POINT *points[], BN_CTX *ctx)
  878. {
  879. size_t i;
  880. if (group->meth->points_make_affine == 0) {
  881. ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  882. return 0;
  883. }
  884. for (i = 0; i < num; i++) {
  885. if (group->meth != points[i]->meth) {
  886. ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
  887. return 0;
  888. }
  889. }
  890. return group->meth->points_make_affine(group, num, points, ctx);
  891. }
  892. /*
  893. * Functions for point multiplication. If group->meth->mul is 0, we use the
  894. * wNAF-based implementations in ec_mult.c; otherwise we dispatch through
  895. * methods.
  896. */
  897. int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
  898. size_t num, const EC_POINT *points[],
  899. const BIGNUM *scalars[], BN_CTX *ctx)
  900. {
  901. if (group->meth->mul == 0)
  902. /* use default */
  903. return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
  904. return group->meth->mul(group, r, scalar, num, points, scalars, ctx);
  905. }
  906. int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
  907. const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
  908. {
  909. /* just a convenient interface to EC_POINTs_mul() */
  910. const EC_POINT *points[1];
  911. const BIGNUM *scalars[1];
  912. points[0] = point;
  913. scalars[0] = p_scalar;
  914. return EC_POINTs_mul(group, r, g_scalar,
  915. (point != NULL
  916. && p_scalar != NULL), points, scalars, ctx);
  917. }
  918. int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
  919. {
  920. if (group->meth->mul == 0)
  921. /* use default */
  922. return ec_wNAF_precompute_mult(group, ctx);
  923. if (group->meth->precompute_mult != 0)
  924. return group->meth->precompute_mult(group, ctx);
  925. else
  926. return 1; /* nothing to do, so report success */
  927. }
  928. int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
  929. {
  930. if (group->meth->mul == 0)
  931. /* use default */
  932. return ec_wNAF_have_precompute_mult(group);
  933. if (group->meth->have_precompute_mult != 0)
  934. return group->meth->have_precompute_mult(group);
  935. else
  936. return 0; /* cannot tell whether precomputation has
  937. * been performed */
  938. }
  939. /*
  940. * ec_precompute_mont_data sets |group->mont_data| from |group->order| and
  941. * returns one on success. On error it returns zero.
  942. */
  943. int ec_precompute_mont_data(EC_GROUP *group)
  944. {
  945. BN_CTX *ctx = BN_CTX_new();
  946. int ret = 0;
  947. if (!EC_GROUP_VERSION(group))
  948. goto err;
  949. if (group->mont_data) {
  950. BN_MONT_CTX_free(group->mont_data);
  951. group->mont_data = NULL;
  952. }
  953. if (ctx == NULL)
  954. goto err;
  955. group->mont_data = BN_MONT_CTX_new();
  956. if (!group->mont_data)
  957. goto err;
  958. if (!BN_MONT_CTX_set(group->mont_data, &group->order, ctx)) {
  959. BN_MONT_CTX_free(group->mont_data);
  960. group->mont_data = NULL;
  961. goto err;
  962. }
  963. ret = 1;
  964. err:
  965. if (ctx)
  966. BN_CTX_free(ctx);
  967. return ret;
  968. }