ec2_mult.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /* crypto/ec/ec2_mult.c */
  2. /* ====================================================================
  3. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  4. *
  5. * The Elliptic Curve Public-Key Crypto Library (ECC Code) included
  6. * herein is developed by SUN MICROSYSTEMS, INC., and is contributed
  7. * to the OpenSSL project.
  8. *
  9. * The ECC Code is licensed pursuant to the OpenSSL open source
  10. * license provided below.
  11. *
  12. * The software is originally written by Sheueling Chang Shantz and
  13. * Douglas Stebila of Sun Microsystems Laboratories.
  14. *
  15. */
  16. /* ====================================================================
  17. * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved.
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions
  21. * are met:
  22. *
  23. * 1. Redistributions of source code must retain the above copyright
  24. * notice, this list of conditions and the following disclaimer.
  25. *
  26. * 2. Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. *
  31. * 3. All advertising materials mentioning features or use of this
  32. * software must display the following acknowledgment:
  33. * "This product includes software developed by the OpenSSL Project
  34. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  35. *
  36. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  37. * endorse or promote products derived from this software without
  38. * prior written permission. For written permission, please contact
  39. * openssl-core@openssl.org.
  40. *
  41. * 5. Products derived from this software may not be called "OpenSSL"
  42. * nor may "OpenSSL" appear in their names without prior written
  43. * permission of the OpenSSL Project.
  44. *
  45. * 6. Redistributions of any form whatsoever must retain the following
  46. * acknowledgment:
  47. * "This product includes software developed by the OpenSSL Project
  48. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  51. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  52. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  53. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  54. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  55. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  56. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  57. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  58. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  59. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  60. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  61. * OF THE POSSIBILITY OF SUCH DAMAGE.
  62. * ====================================================================
  63. *
  64. * This product includes cryptographic software written by Eric Young
  65. * (eay@cryptsoft.com). This product includes software written by Tim
  66. * Hudson (tjh@cryptsoft.com).
  67. *
  68. */
  69. #include <openssl/err.h>
  70. #include "ec_lcl.h"
  71. /*-
  72. * Compute the x-coordinate x/z for the point 2*(x/z) in Montgomery projective
  73. * coordinates.
  74. * Uses algorithm Mdouble in appendix of
  75. * Lopez, J. and Dahab, R. "Fast multiplication on elliptic curves over
  76. * GF(2^m) without precomputation" (CHES '99, LNCS 1717).
  77. * modified to not require precomputation of c=b^{2^{m-1}}.
  78. */
  79. static int gf2m_Mdouble(const EC_GROUP *group, BIGNUM *x, BIGNUM *z,
  80. BN_CTX *ctx)
  81. {
  82. BIGNUM *t1;
  83. int ret = 0;
  84. /* Since Mdouble is static we can guarantee that ctx != NULL. */
  85. BN_CTX_start(ctx);
  86. t1 = BN_CTX_get(ctx);
  87. if (t1 == NULL)
  88. goto err;
  89. if (!group->meth->field_sqr(group, x, x, ctx))
  90. goto err;
  91. if (!group->meth->field_sqr(group, t1, z, ctx))
  92. goto err;
  93. if (!group->meth->field_mul(group, z, x, t1, ctx))
  94. goto err;
  95. if (!group->meth->field_sqr(group, x, x, ctx))
  96. goto err;
  97. if (!group->meth->field_sqr(group, t1, t1, ctx))
  98. goto err;
  99. if (!group->meth->field_mul(group, t1, &group->b, t1, ctx))
  100. goto err;
  101. if (!BN_GF2m_add(x, x, t1))
  102. goto err;
  103. ret = 1;
  104. err:
  105. BN_CTX_end(ctx);
  106. return ret;
  107. }
  108. /*-
  109. * Compute the x-coordinate x1/z1 for the point (x1/z1)+(x2/x2) in Montgomery
  110. * projective coordinates.
  111. * Uses algorithm Madd in appendix of
  112. * Lopez, J. and Dahab, R. "Fast multiplication on elliptic curves over
  113. * GF(2^m) without precomputation" (CHES '99, LNCS 1717).
  114. */
  115. static int gf2m_Madd(const EC_GROUP *group, const BIGNUM *x, BIGNUM *x1,
  116. BIGNUM *z1, const BIGNUM *x2, const BIGNUM *z2,
  117. BN_CTX *ctx)
  118. {
  119. BIGNUM *t1, *t2;
  120. int ret = 0;
  121. /* Since Madd is static we can guarantee that ctx != NULL. */
  122. BN_CTX_start(ctx);
  123. t1 = BN_CTX_get(ctx);
  124. t2 = BN_CTX_get(ctx);
  125. if (t2 == NULL)
  126. goto err;
  127. if (!BN_copy(t1, x))
  128. goto err;
  129. if (!group->meth->field_mul(group, x1, x1, z2, ctx))
  130. goto err;
  131. if (!group->meth->field_mul(group, z1, z1, x2, ctx))
  132. goto err;
  133. if (!group->meth->field_mul(group, t2, x1, z1, ctx))
  134. goto err;
  135. if (!BN_GF2m_add(z1, z1, x1))
  136. goto err;
  137. if (!group->meth->field_sqr(group, z1, z1, ctx))
  138. goto err;
  139. if (!group->meth->field_mul(group, x1, z1, t1, ctx))
  140. goto err;
  141. if (!BN_GF2m_add(x1, x1, t2))
  142. goto err;
  143. ret = 1;
  144. err:
  145. BN_CTX_end(ctx);
  146. return ret;
  147. }
  148. /*-
  149. * Compute the x, y affine coordinates from the point (x1, z1) (x2, z2)
  150. * using Montgomery point multiplication algorithm Mxy() in appendix of
  151. * Lopez, J. and Dahab, R. "Fast multiplication on elliptic curves over
  152. * GF(2^m) without precomputation" (CHES '99, LNCS 1717).
  153. * Returns:
  154. * 0 on error
  155. * 1 if return value should be the point at infinity
  156. * 2 otherwise
  157. */
  158. static int gf2m_Mxy(const EC_GROUP *group, const BIGNUM *x, const BIGNUM *y,
  159. BIGNUM *x1, BIGNUM *z1, BIGNUM *x2, BIGNUM *z2,
  160. BN_CTX *ctx)
  161. {
  162. BIGNUM *t3, *t4, *t5;
  163. int ret = 0;
  164. if (BN_is_zero(z1)) {
  165. BN_zero(x2);
  166. BN_zero(z2);
  167. return 1;
  168. }
  169. if (BN_is_zero(z2)) {
  170. if (!BN_copy(x2, x))
  171. return 0;
  172. if (!BN_GF2m_add(z2, x, y))
  173. return 0;
  174. return 2;
  175. }
  176. /* Since Mxy is static we can guarantee that ctx != NULL. */
  177. BN_CTX_start(ctx);
  178. t3 = BN_CTX_get(ctx);
  179. t4 = BN_CTX_get(ctx);
  180. t5 = BN_CTX_get(ctx);
  181. if (t5 == NULL)
  182. goto err;
  183. if (!BN_one(t5))
  184. goto err;
  185. if (!group->meth->field_mul(group, t3, z1, z2, ctx))
  186. goto err;
  187. if (!group->meth->field_mul(group, z1, z1, x, ctx))
  188. goto err;
  189. if (!BN_GF2m_add(z1, z1, x1))
  190. goto err;
  191. if (!group->meth->field_mul(group, z2, z2, x, ctx))
  192. goto err;
  193. if (!group->meth->field_mul(group, x1, z2, x1, ctx))
  194. goto err;
  195. if (!BN_GF2m_add(z2, z2, x2))
  196. goto err;
  197. if (!group->meth->field_mul(group, z2, z2, z1, ctx))
  198. goto err;
  199. if (!group->meth->field_sqr(group, t4, x, ctx))
  200. goto err;
  201. if (!BN_GF2m_add(t4, t4, y))
  202. goto err;
  203. if (!group->meth->field_mul(group, t4, t4, t3, ctx))
  204. goto err;
  205. if (!BN_GF2m_add(t4, t4, z2))
  206. goto err;
  207. if (!group->meth->field_mul(group, t3, t3, x, ctx))
  208. goto err;
  209. if (!group->meth->field_div(group, t3, t5, t3, ctx))
  210. goto err;
  211. if (!group->meth->field_mul(group, t4, t3, t4, ctx))
  212. goto err;
  213. if (!group->meth->field_mul(group, x2, x1, t3, ctx))
  214. goto err;
  215. if (!BN_GF2m_add(z2, x2, x))
  216. goto err;
  217. if (!group->meth->field_mul(group, z2, z2, t4, ctx))
  218. goto err;
  219. if (!BN_GF2m_add(z2, z2, y))
  220. goto err;
  221. ret = 2;
  222. err:
  223. BN_CTX_end(ctx);
  224. return ret;
  225. }
  226. /*-
  227. * Computes scalar*point and stores the result in r.
  228. * point can not equal r.
  229. * Uses a modified algorithm 2P of
  230. * Lopez, J. and Dahab, R. "Fast multiplication on elliptic curves over
  231. * GF(2^m) without precomputation" (CHES '99, LNCS 1717).
  232. *
  233. * To protect against side-channel attack the function uses constant time swap,
  234. * avoiding conditional branches.
  235. */
  236. static int ec_GF2m_montgomery_point_multiply(const EC_GROUP *group,
  237. EC_POINT *r,
  238. const BIGNUM *scalar,
  239. const EC_POINT *point,
  240. BN_CTX *ctx)
  241. {
  242. BIGNUM *x1, *x2, *z1, *z2;
  243. int ret = 0, i;
  244. BN_ULONG mask, word;
  245. if (r == point) {
  246. ECerr(EC_F_EC_GF2M_MONTGOMERY_POINT_MULTIPLY, EC_R_INVALID_ARGUMENT);
  247. return 0;
  248. }
  249. /* if result should be point at infinity */
  250. if ((scalar == NULL) || BN_is_zero(scalar) || (point == NULL) ||
  251. EC_POINT_is_at_infinity(group, point)) {
  252. return EC_POINT_set_to_infinity(group, r);
  253. }
  254. /* only support affine coordinates */
  255. if (!point->Z_is_one)
  256. return 0;
  257. /*
  258. * Since point_multiply is static we can guarantee that ctx != NULL.
  259. */
  260. BN_CTX_start(ctx);
  261. x1 = BN_CTX_get(ctx);
  262. z1 = BN_CTX_get(ctx);
  263. if (z1 == NULL)
  264. goto err;
  265. x2 = &r->X;
  266. z2 = &r->Y;
  267. bn_wexpand(x1, group->field.top);
  268. bn_wexpand(z1, group->field.top);
  269. bn_wexpand(x2, group->field.top);
  270. bn_wexpand(z2, group->field.top);
  271. if (!BN_GF2m_mod_arr(x1, &point->X, group->poly))
  272. goto err; /* x1 = x */
  273. if (!BN_one(z1))
  274. goto err; /* z1 = 1 */
  275. if (!group->meth->field_sqr(group, z2, x1, ctx))
  276. goto err; /* z2 = x1^2 = x^2 */
  277. if (!group->meth->field_sqr(group, x2, z2, ctx))
  278. goto err;
  279. if (!BN_GF2m_add(x2, x2, &group->b))
  280. goto err; /* x2 = x^4 + b */
  281. /* find top most bit and go one past it */
  282. i = scalar->top - 1;
  283. mask = BN_TBIT;
  284. word = scalar->d[i];
  285. while (!(word & mask))
  286. mask >>= 1;
  287. mask >>= 1;
  288. /* if top most bit was at word break, go to next word */
  289. if (!mask) {
  290. i--;
  291. mask = BN_TBIT;
  292. }
  293. for (; i >= 0; i--) {
  294. word = scalar->d[i];
  295. while (mask) {
  296. BN_consttime_swap(word & mask, x1, x2, group->field.top);
  297. BN_consttime_swap(word & mask, z1, z2, group->field.top);
  298. if (!gf2m_Madd(group, &point->X, x2, z2, x1, z1, ctx))
  299. goto err;
  300. if (!gf2m_Mdouble(group, x1, z1, ctx))
  301. goto err;
  302. BN_consttime_swap(word & mask, x1, x2, group->field.top);
  303. BN_consttime_swap(word & mask, z1, z2, group->field.top);
  304. mask >>= 1;
  305. }
  306. mask = BN_TBIT;
  307. }
  308. /* convert out of "projective" coordinates */
  309. i = gf2m_Mxy(group, &point->X, &point->Y, x1, z1, x2, z2, ctx);
  310. if (i == 0)
  311. goto err;
  312. else if (i == 1) {
  313. if (!EC_POINT_set_to_infinity(group, r))
  314. goto err;
  315. } else {
  316. if (!BN_one(&r->Z))
  317. goto err;
  318. r->Z_is_one = 1;
  319. }
  320. /* GF(2^m) field elements should always have BIGNUM::neg = 0 */
  321. BN_set_negative(&r->X, 0);
  322. BN_set_negative(&r->Y, 0);
  323. ret = 1;
  324. err:
  325. BN_CTX_end(ctx);
  326. return ret;
  327. }
  328. /*-
  329. * Computes the sum
  330. * scalar*group->generator + scalars[0]*points[0] + ... + scalars[num-1]*points[num-1]
  331. * gracefully ignoring NULL scalar values.
  332. */
  333. int ec_GF2m_simple_mul(const EC_GROUP *group, EC_POINT *r,
  334. const BIGNUM *scalar, size_t num,
  335. const EC_POINT *points[], const BIGNUM *scalars[],
  336. BN_CTX *ctx)
  337. {
  338. BN_CTX *new_ctx = NULL;
  339. int ret = 0;
  340. size_t i;
  341. EC_POINT *p = NULL;
  342. EC_POINT *acc = NULL;
  343. if (ctx == NULL) {
  344. ctx = new_ctx = BN_CTX_new();
  345. if (ctx == NULL)
  346. return 0;
  347. }
  348. /*
  349. * This implementation is more efficient than the wNAF implementation for
  350. * 2 or fewer points. Use the ec_wNAF_mul implementation for 3 or more
  351. * points, or if we can perform a fast multiplication based on
  352. * precomputation.
  353. */
  354. if ((scalar && (num > 1)) || (num > 2)
  355. || (num == 0 && EC_GROUP_have_precompute_mult(group))) {
  356. ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
  357. goto err;
  358. }
  359. if ((p = EC_POINT_new(group)) == NULL)
  360. goto err;
  361. if ((acc = EC_POINT_new(group)) == NULL)
  362. goto err;
  363. if (!EC_POINT_set_to_infinity(group, acc))
  364. goto err;
  365. if (scalar) {
  366. if (!ec_GF2m_montgomery_point_multiply
  367. (group, p, scalar, group->generator, ctx))
  368. goto err;
  369. if (BN_is_negative(scalar))
  370. if (!group->meth->invert(group, p, ctx))
  371. goto err;
  372. if (!group->meth->add(group, acc, acc, p, ctx))
  373. goto err;
  374. }
  375. for (i = 0; i < num; i++) {
  376. if (!ec_GF2m_montgomery_point_multiply
  377. (group, p, scalars[i], points[i], ctx))
  378. goto err;
  379. if (BN_is_negative(scalars[i]))
  380. if (!group->meth->invert(group, p, ctx))
  381. goto err;
  382. if (!group->meth->add(group, acc, acc, p, ctx))
  383. goto err;
  384. }
  385. if (!EC_POINT_copy(r, acc))
  386. goto err;
  387. ret = 1;
  388. err:
  389. if (p)
  390. EC_POINT_free(p);
  391. if (acc)
  392. EC_POINT_free(acc);
  393. if (new_ctx != NULL)
  394. BN_CTX_free(new_ctx);
  395. return ret;
  396. }
  397. /*
  398. * Precomputation for point multiplication: fall back to wNAF methods because
  399. * ec_GF2m_simple_mul() uses ec_wNAF_mul() if appropriate
  400. */
  401. int ec_GF2m_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
  402. {
  403. return ec_wNAF_precompute_mult(group, ctx);
  404. }
  405. int ec_GF2m_have_precompute_mult(const EC_GROUP *group)
  406. {
  407. return ec_wNAF_have_precompute_mult(group);
  408. }