ec2_mult.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. *
  5. * Licensed under the OpenSSL license (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include <openssl/err.h>
  11. #include "internal/bn_int.h"
  12. #include "ec_lcl.h"
  13. #ifndef OPENSSL_NO_EC2M
  14. /*-
  15. * Compute the x-coordinate x/z for the point 2*(x/z) in Montgomery projective
  16. * coordinates.
  17. * Uses algorithm Mdouble in appendix of
  18. * Lopez, J. and Dahab, R. "Fast multiplication on elliptic curves over
  19. * GF(2^m) without precomputation" (CHES '99, LNCS 1717).
  20. * modified to not require precomputation of c=b^{2^{m-1}}.
  21. */
  22. static int gf2m_Mdouble(const EC_GROUP *group, BIGNUM *x, BIGNUM *z,
  23. BN_CTX *ctx)
  24. {
  25. BIGNUM *t1;
  26. int ret = 0;
  27. /* Since Mdouble is static we can guarantee that ctx != NULL. */
  28. BN_CTX_start(ctx);
  29. t1 = BN_CTX_get(ctx);
  30. if (t1 == NULL)
  31. goto err;
  32. if (!group->meth->field_sqr(group, x, x, ctx))
  33. goto err;
  34. if (!group->meth->field_sqr(group, t1, z, ctx))
  35. goto err;
  36. if (!group->meth->field_mul(group, z, x, t1, ctx))
  37. goto err;
  38. if (!group->meth->field_sqr(group, x, x, ctx))
  39. goto err;
  40. if (!group->meth->field_sqr(group, t1, t1, ctx))
  41. goto err;
  42. if (!group->meth->field_mul(group, t1, group->b, t1, ctx))
  43. goto err;
  44. if (!BN_GF2m_add(x, x, t1))
  45. goto err;
  46. ret = 1;
  47. err:
  48. BN_CTX_end(ctx);
  49. return ret;
  50. }
  51. /*-
  52. * Compute the x-coordinate x1/z1 for the point (x1/z1)+(x2/x2) in Montgomery
  53. * projective coordinates.
  54. * Uses algorithm Madd in appendix of
  55. * Lopez, J. and Dahab, R. "Fast multiplication on elliptic curves over
  56. * GF(2^m) without precomputation" (CHES '99, LNCS 1717).
  57. */
  58. static int gf2m_Madd(const EC_GROUP *group, const BIGNUM *x, BIGNUM *x1,
  59. BIGNUM *z1, const BIGNUM *x2, const BIGNUM *z2,
  60. BN_CTX *ctx)
  61. {
  62. BIGNUM *t1, *t2;
  63. int ret = 0;
  64. /* Since Madd is static we can guarantee that ctx != NULL. */
  65. BN_CTX_start(ctx);
  66. t1 = BN_CTX_get(ctx);
  67. t2 = BN_CTX_get(ctx);
  68. if (t2 == NULL)
  69. goto err;
  70. if (!BN_copy(t1, x))
  71. goto err;
  72. if (!group->meth->field_mul(group, x1, x1, z2, ctx))
  73. goto err;
  74. if (!group->meth->field_mul(group, z1, z1, x2, ctx))
  75. goto err;
  76. if (!group->meth->field_mul(group, t2, x1, z1, ctx))
  77. goto err;
  78. if (!BN_GF2m_add(z1, z1, x1))
  79. goto err;
  80. if (!group->meth->field_sqr(group, z1, z1, ctx))
  81. goto err;
  82. if (!group->meth->field_mul(group, x1, z1, t1, ctx))
  83. goto err;
  84. if (!BN_GF2m_add(x1, x1, t2))
  85. goto err;
  86. ret = 1;
  87. err:
  88. BN_CTX_end(ctx);
  89. return ret;
  90. }
  91. /*-
  92. * Compute the x, y affine coordinates from the point (x1, z1) (x2, z2)
  93. * using Montgomery point multiplication algorithm Mxy() in appendix of
  94. * Lopez, J. and Dahab, R. "Fast multiplication on elliptic curves over
  95. * GF(2^m) without precomputation" (CHES '99, LNCS 1717).
  96. * Returns:
  97. * 0 on error
  98. * 1 if return value should be the point at infinity
  99. * 2 otherwise
  100. */
  101. static int gf2m_Mxy(const EC_GROUP *group, const BIGNUM *x, const BIGNUM *y,
  102. BIGNUM *x1, BIGNUM *z1, BIGNUM *x2, BIGNUM *z2,
  103. BN_CTX *ctx)
  104. {
  105. BIGNUM *t3, *t4, *t5;
  106. int ret = 0;
  107. if (BN_is_zero(z1)) {
  108. BN_zero(x2);
  109. BN_zero(z2);
  110. return 1;
  111. }
  112. if (BN_is_zero(z2)) {
  113. if (!BN_copy(x2, x))
  114. return 0;
  115. if (!BN_GF2m_add(z2, x, y))
  116. return 0;
  117. return 2;
  118. }
  119. /* Since Mxy is static we can guarantee that ctx != NULL. */
  120. BN_CTX_start(ctx);
  121. t3 = BN_CTX_get(ctx);
  122. t4 = BN_CTX_get(ctx);
  123. t5 = BN_CTX_get(ctx);
  124. if (t5 == NULL)
  125. goto err;
  126. if (!BN_one(t5))
  127. goto err;
  128. if (!group->meth->field_mul(group, t3, z1, z2, ctx))
  129. goto err;
  130. if (!group->meth->field_mul(group, z1, z1, x, ctx))
  131. goto err;
  132. if (!BN_GF2m_add(z1, z1, x1))
  133. goto err;
  134. if (!group->meth->field_mul(group, z2, z2, x, ctx))
  135. goto err;
  136. if (!group->meth->field_mul(group, x1, z2, x1, ctx))
  137. goto err;
  138. if (!BN_GF2m_add(z2, z2, x2))
  139. goto err;
  140. if (!group->meth->field_mul(group, z2, z2, z1, ctx))
  141. goto err;
  142. if (!group->meth->field_sqr(group, t4, x, ctx))
  143. goto err;
  144. if (!BN_GF2m_add(t4, t4, y))
  145. goto err;
  146. if (!group->meth->field_mul(group, t4, t4, t3, ctx))
  147. goto err;
  148. if (!BN_GF2m_add(t4, t4, z2))
  149. goto err;
  150. if (!group->meth->field_mul(group, t3, t3, x, ctx))
  151. goto err;
  152. if (!group->meth->field_div(group, t3, t5, t3, ctx))
  153. goto err;
  154. if (!group->meth->field_mul(group, t4, t3, t4, ctx))
  155. goto err;
  156. if (!group->meth->field_mul(group, x2, x1, t3, ctx))
  157. goto err;
  158. if (!BN_GF2m_add(z2, x2, x))
  159. goto err;
  160. if (!group->meth->field_mul(group, z2, z2, t4, ctx))
  161. goto err;
  162. if (!BN_GF2m_add(z2, z2, y))
  163. goto err;
  164. ret = 2;
  165. err:
  166. BN_CTX_end(ctx);
  167. return ret;
  168. }
  169. /*-
  170. * Computes scalar*point and stores the result in r.
  171. * point can not equal r.
  172. * Uses a modified algorithm 2P of
  173. * Lopez, J. and Dahab, R. "Fast multiplication on elliptic curves over
  174. * GF(2^m) without precomputation" (CHES '99, LNCS 1717).
  175. *
  176. * To protect against side-channel attack the function uses constant time swap,
  177. * avoiding conditional branches.
  178. */
  179. static int ec_GF2m_montgomery_point_multiply(const EC_GROUP *group,
  180. EC_POINT *r,
  181. const BIGNUM *scalar,
  182. const EC_POINT *point,
  183. BN_CTX *ctx)
  184. {
  185. BIGNUM *x1, *x2, *z1, *z2;
  186. int ret = 0, i, group_top;
  187. BN_ULONG mask, word;
  188. if (r == point) {
  189. ECerr(EC_F_EC_GF2M_MONTGOMERY_POINT_MULTIPLY, EC_R_INVALID_ARGUMENT);
  190. return 0;
  191. }
  192. /* if result should be point at infinity */
  193. if ((scalar == NULL) || BN_is_zero(scalar) || (point == NULL) ||
  194. EC_POINT_is_at_infinity(group, point)) {
  195. return EC_POINT_set_to_infinity(group, r);
  196. }
  197. /* only support affine coordinates */
  198. if (!point->Z_is_one)
  199. return 0;
  200. /*
  201. * Since point_multiply is static we can guarantee that ctx != NULL.
  202. */
  203. BN_CTX_start(ctx);
  204. x1 = BN_CTX_get(ctx);
  205. z1 = BN_CTX_get(ctx);
  206. if (z1 == NULL)
  207. goto err;
  208. x2 = r->X;
  209. z2 = r->Y;
  210. group_top = bn_get_top(group->field);
  211. if (bn_wexpand(x1, group_top) == NULL
  212. || bn_wexpand(z1, group_top) == NULL
  213. || bn_wexpand(x2, group_top) == NULL
  214. || bn_wexpand(z2, group_top) == NULL)
  215. goto err;
  216. if (!BN_GF2m_mod_arr(x1, point->X, group->poly))
  217. goto err; /* x1 = x */
  218. if (!BN_one(z1))
  219. goto err; /* z1 = 1 */
  220. if (!group->meth->field_sqr(group, z2, x1, ctx))
  221. goto err; /* z2 = x1^2 = x^2 */
  222. if (!group->meth->field_sqr(group, x2, z2, ctx))
  223. goto err;
  224. if (!BN_GF2m_add(x2, x2, group->b))
  225. goto err; /* x2 = x^4 + b */
  226. /* find top most bit and go one past it */
  227. i = bn_get_top(scalar) - 1;
  228. mask = BN_TBIT;
  229. word = bn_get_words(scalar)[i];
  230. while (!(word & mask))
  231. mask >>= 1;
  232. mask >>= 1;
  233. /* if top most bit was at word break, go to next word */
  234. if (!mask) {
  235. i--;
  236. mask = BN_TBIT;
  237. }
  238. for (; i >= 0; i--) {
  239. word = bn_get_words(scalar)[i];
  240. while (mask) {
  241. BN_consttime_swap(word & mask, x1, x2, group_top);
  242. BN_consttime_swap(word & mask, z1, z2, group_top);
  243. if (!gf2m_Madd(group, point->X, x2, z2, x1, z1, ctx))
  244. goto err;
  245. if (!gf2m_Mdouble(group, x1, z1, ctx))
  246. goto err;
  247. BN_consttime_swap(word & mask, x1, x2, group_top);
  248. BN_consttime_swap(word & mask, z1, z2, group_top);
  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)
  256. goto err;
  257. else if (i == 1) {
  258. if (!EC_POINT_set_to_infinity(group, r))
  259. goto err;
  260. } else {
  261. if (!BN_one(r->Z))
  262. 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. /*-
  274. * Computes the sum
  275. * scalar*group->generator + scalars[0]*points[0] + ... + scalars[num-1]*points[num-1]
  276. * gracefully ignoring NULL scalar values.
  277. */
  278. int ec_GF2m_simple_mul(const EC_GROUP *group, EC_POINT *r,
  279. const BIGNUM *scalar, size_t num,
  280. const EC_POINT *points[], const BIGNUM *scalars[],
  281. BN_CTX *ctx)
  282. {
  283. BN_CTX *new_ctx = NULL;
  284. int ret = 0;
  285. size_t i;
  286. EC_POINT *p = NULL;
  287. EC_POINT *acc = NULL;
  288. if (ctx == NULL) {
  289. ctx = new_ctx = BN_CTX_new();
  290. if (ctx == NULL)
  291. return 0;
  292. }
  293. /*
  294. * This implementation is more efficient than the wNAF implementation for
  295. * 2 or fewer points. Use the ec_wNAF_mul implementation for 3 or more
  296. * points, or if we can perform a fast multiplication based on
  297. * precomputation.
  298. */
  299. if ((scalar && (num > 1)) || (num > 2)
  300. || (num == 0 && EC_GROUP_have_precompute_mult(group))) {
  301. ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
  302. goto err;
  303. }
  304. if ((p = EC_POINT_new(group)) == NULL)
  305. goto err;
  306. if ((acc = EC_POINT_new(group)) == NULL)
  307. goto err;
  308. if (!EC_POINT_set_to_infinity(group, acc))
  309. goto err;
  310. if (scalar) {
  311. if (!ec_GF2m_montgomery_point_multiply
  312. (group, p, scalar, group->generator, ctx))
  313. goto err;
  314. if (BN_is_negative(scalar))
  315. if (!group->meth->invert(group, p, ctx))
  316. goto err;
  317. if (!group->meth->add(group, acc, acc, p, ctx))
  318. goto err;
  319. }
  320. for (i = 0; i < num; i++) {
  321. if (!ec_GF2m_montgomery_point_multiply
  322. (group, p, scalars[i], points[i], ctx))
  323. goto err;
  324. if (BN_is_negative(scalars[i]))
  325. if (!group->meth->invert(group, p, ctx))
  326. goto err;
  327. if (!group->meth->add(group, acc, acc, p, ctx))
  328. goto err;
  329. }
  330. if (!EC_POINT_copy(r, acc))
  331. goto err;
  332. ret = 1;
  333. err:
  334. EC_POINT_free(p);
  335. EC_POINT_free(acc);
  336. BN_CTX_free(new_ctx);
  337. return ret;
  338. }
  339. /*
  340. * Precomputation for point multiplication: fall back to wNAF methods because
  341. * ec_GF2m_simple_mul() uses ec_wNAF_mul() if appropriate
  342. */
  343. int ec_GF2m_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
  344. {
  345. return ec_wNAF_precompute_mult(group, ctx);
  346. }
  347. int ec_GF2m_have_precompute_mult(const EC_GROUP *group)
  348. {
  349. return ec_wNAF_have_precompute_mult(group);
  350. }
  351. #endif