2
0

ec2_mult.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. #define OPENSSL_FIPSAPI
  70. #include <openssl/err.h>
  71. #include "ec_lcl.h"
  72. #ifndef OPENSSL_NO_EC2M
  73. /* Compute the x-coordinate x/z for the point 2*(x/z) in Montgomery projective
  74. * coordinates.
  75. * Uses algorithm Mdouble in appendix of
  76. * Lopez, J. and Dahab, R. "Fast multiplication on elliptic curves over
  77. * GF(2^m) without precomputation" (CHES '99, LNCS 1717).
  78. * modified to not require precomputation of c=b^{2^{m-1}}.
  79. */
  80. static int gf2m_Mdouble(const EC_GROUP *group, BIGNUM *x, BIGNUM *z, 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) goto err;
  88. if (!group->meth->field_sqr(group, x, x, ctx)) goto err;
  89. if (!group->meth->field_sqr(group, t1, z, ctx)) goto err;
  90. if (!group->meth->field_mul(group, z, x, t1, ctx)) goto err;
  91. if (!group->meth->field_sqr(group, x, x, ctx)) goto err;
  92. if (!group->meth->field_sqr(group, t1, t1, ctx)) goto err;
  93. if (!group->meth->field_mul(group, t1, &group->b, t1, ctx)) goto err;
  94. if (!BN_GF2m_add(x, x, t1)) goto err;
  95. ret = 1;
  96. err:
  97. BN_CTX_end(ctx);
  98. return ret;
  99. }
  100. /* Compute the x-coordinate x1/z1 for the point (x1/z1)+(x2/x2) in Montgomery
  101. * projective coordinates.
  102. * Uses algorithm Madd in appendix of
  103. * Lopez, J. and Dahab, R. "Fast multiplication on elliptic curves over
  104. * GF(2^m) without precomputation" (CHES '99, LNCS 1717).
  105. */
  106. static int gf2m_Madd(const EC_GROUP *group, const BIGNUM *x, BIGNUM *x1, BIGNUM *z1,
  107. const BIGNUM *x2, const BIGNUM *z2, BN_CTX *ctx)
  108. {
  109. BIGNUM *t1, *t2;
  110. int ret = 0;
  111. /* Since Madd is static we can guarantee that ctx != NULL. */
  112. BN_CTX_start(ctx);
  113. t1 = BN_CTX_get(ctx);
  114. t2 = BN_CTX_get(ctx);
  115. if (t2 == NULL) goto err;
  116. if (!BN_copy(t1, x)) goto err;
  117. if (!group->meth->field_mul(group, x1, x1, z2, ctx)) goto err;
  118. if (!group->meth->field_mul(group, z1, z1, x2, ctx)) goto err;
  119. if (!group->meth->field_mul(group, t2, x1, z1, ctx)) goto err;
  120. if (!BN_GF2m_add(z1, z1, x1)) goto err;
  121. if (!group->meth->field_sqr(group, z1, z1, ctx)) goto err;
  122. if (!group->meth->field_mul(group, x1, z1, t1, ctx)) goto err;
  123. if (!BN_GF2m_add(x1, x1, t2)) goto err;
  124. ret = 1;
  125. err:
  126. BN_CTX_end(ctx);
  127. return ret;
  128. }
  129. /* Compute the x, y affine coordinates from the point (x1, z1) (x2, z2)
  130. * using Montgomery point multiplication algorithm Mxy() in appendix of
  131. * Lopez, J. and Dahab, R. "Fast multiplication on elliptic curves over
  132. * GF(2^m) without precomputation" (CHES '99, LNCS 1717).
  133. * Returns:
  134. * 0 on error
  135. * 1 if return value should be the point at infinity
  136. * 2 otherwise
  137. */
  138. static int gf2m_Mxy(const EC_GROUP *group, const BIGNUM *x, const BIGNUM *y, BIGNUM *x1,
  139. BIGNUM *z1, BIGNUM *x2, BIGNUM *z2, BN_CTX *ctx)
  140. {
  141. BIGNUM *t3, *t4, *t5;
  142. int ret = 0;
  143. if (BN_is_zero(z1))
  144. {
  145. BN_zero(x2);
  146. BN_zero(z2);
  147. return 1;
  148. }
  149. if (BN_is_zero(z2))
  150. {
  151. if (!BN_copy(x2, x)) return 0;
  152. if (!BN_GF2m_add(z2, x, y)) return 0;
  153. return 2;
  154. }
  155. /* Since Mxy is static we can guarantee that ctx != NULL. */
  156. BN_CTX_start(ctx);
  157. t3 = BN_CTX_get(ctx);
  158. t4 = BN_CTX_get(ctx);
  159. t5 = BN_CTX_get(ctx);
  160. if (t5 == NULL) goto err;
  161. if (!BN_one(t5)) goto err;
  162. if (!group->meth->field_mul(group, t3, z1, z2, ctx)) goto err;
  163. if (!group->meth->field_mul(group, z1, z1, x, ctx)) goto err;
  164. if (!BN_GF2m_add(z1, z1, x1)) goto err;
  165. if (!group->meth->field_mul(group, z2, z2, x, ctx)) goto err;
  166. if (!group->meth->field_mul(group, x1, z2, x1, ctx)) goto err;
  167. if (!BN_GF2m_add(z2, z2, x2)) goto err;
  168. if (!group->meth->field_mul(group, z2, z2, z1, ctx)) goto err;
  169. if (!group->meth->field_sqr(group, t4, x, ctx)) goto err;
  170. if (!BN_GF2m_add(t4, t4, y)) goto err;
  171. if (!group->meth->field_mul(group, t4, t4, t3, ctx)) goto err;
  172. if (!BN_GF2m_add(t4, t4, z2)) goto err;
  173. if (!group->meth->field_mul(group, t3, t3, x, ctx)) goto err;
  174. if (!group->meth->field_div(group, t3, t5, t3, ctx)) goto err;
  175. if (!group->meth->field_mul(group, t4, t3, t4, ctx)) goto err;
  176. if (!group->meth->field_mul(group, x2, x1, t3, ctx)) goto err;
  177. if (!BN_GF2m_add(z2, x2, x)) goto err;
  178. if (!group->meth->field_mul(group, z2, z2, t4, ctx)) goto err;
  179. if (!BN_GF2m_add(z2, z2, y)) goto err;
  180. ret = 2;
  181. err:
  182. BN_CTX_end(ctx);
  183. return ret;
  184. }
  185. /* Computes scalar*point and stores the result in r.
  186. * point can not equal r.
  187. * Uses algorithm 2P of
  188. * Lopez, J. and Dahab, R. "Fast multiplication on elliptic curves over
  189. * GF(2^m) without precomputation" (CHES '99, LNCS 1717).
  190. */
  191. static int ec_GF2m_montgomery_point_multiply(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
  192. const EC_POINT *point, BN_CTX *ctx)
  193. {
  194. BIGNUM *x1, *x2, *z1, *z2;
  195. int ret = 0, i;
  196. BN_ULONG mask,word;
  197. if (r == point)
  198. {
  199. ECerr(EC_F_EC_GF2M_MONTGOMERY_POINT_MULTIPLY, EC_R_INVALID_ARGUMENT);
  200. return 0;
  201. }
  202. /* if result should be point at infinity */
  203. if ((scalar == NULL) || BN_is_zero(scalar) || (point == NULL) ||
  204. EC_POINT_is_at_infinity(group, point))
  205. {
  206. return EC_POINT_set_to_infinity(group, r);
  207. }
  208. /* only support affine coordinates */
  209. if (!point->Z_is_one) return 0;
  210. /* Since point_multiply is static we can guarantee that ctx != NULL. */
  211. BN_CTX_start(ctx);
  212. x1 = BN_CTX_get(ctx);
  213. z1 = BN_CTX_get(ctx);
  214. if (z1 == NULL) goto err;
  215. x2 = &r->X;
  216. z2 = &r->Y;
  217. if (!BN_GF2m_mod_arr(x1, &point->X, group->poly)) goto err; /* x1 = x */
  218. if (!BN_one(z1)) goto err; /* z1 = 1 */
  219. if (!group->meth->field_sqr(group, z2, x1, ctx)) goto err; /* z2 = x1^2 = x^2 */
  220. if (!group->meth->field_sqr(group, x2, z2, ctx)) goto err;
  221. if (!BN_GF2m_add(x2, x2, &group->b)) goto err; /* x2 = x^4 + b */
  222. /* find top most bit and go one past it */
  223. i = scalar->top - 1;
  224. mask = BN_TBIT;
  225. word = scalar->d[i];
  226. while (!(word & mask)) mask >>= 1;
  227. mask >>= 1;
  228. /* if top most bit was at word break, go to next word */
  229. if (!mask)
  230. {
  231. i--;
  232. mask = BN_TBIT;
  233. }
  234. for (; i >= 0; i--)
  235. {
  236. word = scalar->d[i];
  237. while (mask)
  238. {
  239. if (word & mask)
  240. {
  241. if (!gf2m_Madd(group, &point->X, x1, z1, x2, z2, ctx)) goto err;
  242. if (!gf2m_Mdouble(group, x2, z2, ctx)) goto err;
  243. }
  244. else
  245. {
  246. if (!gf2m_Madd(group, &point->X, x2, z2, x1, z1, ctx)) goto err;
  247. if (!gf2m_Mdouble(group, x1, z1, ctx)) goto err;
  248. }
  249. mask >>= 1;
  250. }
  251. mask = BN_TBIT;
  252. }
  253. /* convert out of "projective" coordinates */
  254. i = gf2m_Mxy(group, &point->X, &point->Y, x1, z1, x2, z2, ctx);
  255. if (i == 0) goto err;
  256. else if (i == 1)
  257. {
  258. if (!EC_POINT_set_to_infinity(group, r)) goto err;
  259. }
  260. else
  261. {
  262. if (!BN_one(&r->Z)) goto err;
  263. r->Z_is_one = 1;
  264. }
  265. /* GF(2^m) field elements should always have BIGNUM::neg = 0 */
  266. BN_set_negative(&r->X, 0);
  267. BN_set_negative(&r->Y, 0);
  268. ret = 1;
  269. err:
  270. BN_CTX_end(ctx);
  271. return ret;
  272. }
  273. /* Computes the sum
  274. * scalar*group->generator + scalars[0]*points[0] + ... + scalars[num-1]*points[num-1]
  275. * gracefully ignoring NULL scalar values.
  276. */
  277. int ec_GF2m_simple_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
  278. size_t num, const EC_POINT *points[], const BIGNUM *scalars[], BN_CTX *ctx)
  279. {
  280. BN_CTX *new_ctx = NULL;
  281. int ret = 0;
  282. size_t i;
  283. EC_POINT *p=NULL;
  284. EC_POINT *acc = NULL;
  285. if (ctx == NULL)
  286. {
  287. ctx = new_ctx = BN_CTX_new();
  288. if (ctx == NULL)
  289. return 0;
  290. }
  291. /* This implementation is more efficient than the wNAF implementation for 2
  292. * or fewer points. Use the ec_wNAF_mul implementation for 3 or more points,
  293. * or if we can perform a fast multiplication based on precomputation.
  294. */
  295. if ((scalar && (num > 1)) || (num > 2) || (num == 0 && EC_GROUP_have_precompute_mult(group)))
  296. {
  297. ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
  298. goto err;
  299. }
  300. if ((p = EC_POINT_new(group)) == NULL) goto err;
  301. if ((acc = EC_POINT_new(group)) == NULL) goto err;
  302. if (!EC_POINT_set_to_infinity(group, acc)) goto err;
  303. if (scalar)
  304. {
  305. if (!ec_GF2m_montgomery_point_multiply(group, p, scalar, group->generator, ctx)) goto err;
  306. if (BN_is_negative(scalar))
  307. if (!group->meth->invert(group, p, ctx)) goto err;
  308. if (!group->meth->add(group, acc, acc, p, ctx)) goto err;
  309. }
  310. for (i = 0; i < num; i++)
  311. {
  312. if (!ec_GF2m_montgomery_point_multiply(group, p, scalars[i], points[i], ctx)) goto err;
  313. if (BN_is_negative(scalars[i]))
  314. if (!group->meth->invert(group, p, ctx)) goto err;
  315. if (!group->meth->add(group, acc, acc, p, ctx)) goto err;
  316. }
  317. if (!EC_POINT_copy(r, acc)) goto err;
  318. ret = 1;
  319. err:
  320. if (p) EC_POINT_free(p);
  321. if (acc) EC_POINT_free(acc);
  322. if (new_ctx != NULL)
  323. BN_CTX_free(new_ctx);
  324. return ret;
  325. }
  326. /* Precomputation for point multiplication: fall back to wNAF methods
  327. * because ec_GF2m_simple_mul() uses ec_wNAF_mul() if appropriate */
  328. int ec_GF2m_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
  329. {
  330. return ec_wNAF_precompute_mult(group, ctx);
  331. }
  332. int ec_GF2m_have_precompute_mult(const EC_GROUP *group)
  333. {
  334. return ec_wNAF_have_precompute_mult(group);
  335. }
  336. #endif