ec_lib.c 32 KB

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