wnaf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /* Originally written by Bodo Moeller for the OpenSSL project.
  2. * ====================================================================
  3. * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * 3. All advertising materials mentioning features or use of this
  18. * software must display the following acknowledgment:
  19. * "This product includes software developed by the OpenSSL Project
  20. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  21. *
  22. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  23. * endorse or promote products derived from this software without
  24. * prior written permission. For written permission, please contact
  25. * openssl-core@openssl.org.
  26. *
  27. * 5. Products derived from this software may not be called "OpenSSL"
  28. * nor may "OpenSSL" appear in their names without prior written
  29. * permission of the OpenSSL Project.
  30. *
  31. * 6. Redistributions of any form whatsoever must retain the following
  32. * acknowledgment:
  33. * "This product includes software developed by the OpenSSL Project
  34. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  37. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  39. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  45. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  46. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  47. * OF THE POSSIBILITY OF SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This product includes cryptographic software written by Eric Young
  51. * (eay@cryptsoft.com). This product includes software written by Tim
  52. * Hudson (tjh@cryptsoft.com).
  53. *
  54. */
  55. /* ====================================================================
  56. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  57. *
  58. * Portions of the attached software ("Contribution") are developed by
  59. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  60. *
  61. * The Contribution is licensed pursuant to the OpenSSL open source
  62. * license provided above.
  63. *
  64. * The elliptic curve binary polynomial software is originally written by
  65. * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
  66. * Laboratories. */
  67. #include <openssl/ec.h>
  68. #include <string.h>
  69. #include <openssl/bn.h>
  70. #include <openssl/err.h>
  71. #include <openssl/mem.h>
  72. #include <openssl/thread.h>
  73. #include <openssl/type_check.h>
  74. #include "internal.h"
  75. #include "../bn/internal.h"
  76. #include "../../internal.h"
  77. // This file implements the wNAF-based interleaving multi-exponentiation method
  78. // at:
  79. // http://link.springer.com/chapter/10.1007%2F3-540-45537-X_13
  80. // http://www.bmoeller.de/pdf/TI-01-08.multiexp.pdf
  81. int ec_compute_wNAF(const EC_GROUP *group, int8_t *out, const EC_SCALAR *scalar,
  82. size_t bits, int w) {
  83. // 'int8_t' can represent integers with absolute values less than 2^7.
  84. if (w <= 0 || w > 7 || bits == 0) {
  85. OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
  86. return 0;
  87. }
  88. int bit = 1 << w; // at most 128
  89. int next_bit = bit << 1; // at most 256
  90. int mask = next_bit - 1; // at most 255
  91. int window_val = scalar->words[0] & mask;
  92. size_t j = 0;
  93. // If j+w+1 >= bits, window_val will not increase.
  94. while (window_val != 0 || j + w + 1 < bits) {
  95. int digit = 0;
  96. // 0 <= window_val <= 2^(w+1)
  97. if (window_val & 1) {
  98. // 0 < window_val < 2^(w+1)
  99. if (window_val & bit) {
  100. digit = window_val - next_bit; // -2^w < digit < 0
  101. #if 1 // modified wNAF
  102. if (j + w + 1 >= bits) {
  103. // special case for generating modified wNAFs:
  104. // no new bits will be added into window_val,
  105. // so using a positive digit here will decrease
  106. // the total length of the representation
  107. digit = window_val & (mask >> 1); // 0 < digit < 2^w
  108. }
  109. #endif
  110. } else {
  111. digit = window_val; // 0 < digit < 2^w
  112. }
  113. if (digit <= -bit || digit >= bit || !(digit & 1)) {
  114. OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
  115. return 0;
  116. }
  117. window_val -= digit;
  118. // Now window_val is 0 or 2^(w+1) in standard wNAF generation;
  119. // for modified window NAFs, it may also be 2^w.
  120. if (window_val != 0 && window_val != next_bit && window_val != bit) {
  121. OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
  122. return 0;
  123. }
  124. }
  125. out[j++] = digit;
  126. window_val >>= 1;
  127. window_val +=
  128. bit * bn_is_bit_set_words(scalar->words, group->order.width, j + w);
  129. if (window_val > next_bit) {
  130. OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
  131. return 0;
  132. }
  133. }
  134. // Fill the rest of the wNAF with zeros.
  135. if (j > bits + 1) {
  136. OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
  137. return 0;
  138. }
  139. for (size_t i = j; i < bits + 1; i++) {
  140. out[i] = 0;
  141. }
  142. return 1;
  143. }
  144. // TODO: table should be optimised for the wNAF-based implementation,
  145. // sometimes smaller windows will give better performance
  146. // (thus the boundaries should be increased)
  147. static size_t window_bits_for_scalar_size(size_t b) {
  148. if (b >= 300) {
  149. return 4;
  150. }
  151. if (b >= 70) {
  152. return 3;
  153. }
  154. if (b >= 20) {
  155. return 2;
  156. }
  157. return 1;
  158. }
  159. // EC_WNAF_MAX_WINDOW_BITS is the largest value returned by
  160. // |window_bits_for_scalar_size|.
  161. #define EC_WNAF_MAX_WINDOW_BITS 4
  162. // compute_precomp sets |out[i]| to a newly-allocated |EC_POINT| containing
  163. // (2*i+1)*p, for i from 0 to |len|. It returns one on success and
  164. // zero on error.
  165. static int compute_precomp(const EC_GROUP *group, EC_POINT **out,
  166. const EC_POINT *p, size_t len, BN_CTX *ctx) {
  167. out[0] = EC_POINT_new(group);
  168. if (out[0] == NULL ||
  169. !EC_POINT_copy(out[0], p)) {
  170. return 0;
  171. }
  172. int ret = 0;
  173. EC_POINT *two_p = EC_POINT_new(group);
  174. if (two_p == NULL ||
  175. !EC_POINT_dbl(group, two_p, p, ctx)) {
  176. goto err;
  177. }
  178. for (size_t i = 1; i < len; i++) {
  179. out[i] = EC_POINT_new(group);
  180. if (out[i] == NULL ||
  181. !EC_POINT_add(group, out[i], out[i - 1], two_p, ctx)) {
  182. goto err;
  183. }
  184. }
  185. ret = 1;
  186. err:
  187. EC_POINT_free(two_p);
  188. return ret;
  189. }
  190. static int lookup_precomp(const EC_GROUP *group, EC_POINT *out,
  191. EC_POINT *const *precomp, int digit, BN_CTX *ctx) {
  192. if (digit < 0) {
  193. digit = -digit;
  194. return EC_POINT_copy(out, precomp[digit >> 1]) &&
  195. EC_POINT_invert(group, out, ctx);
  196. }
  197. return EC_POINT_copy(out, precomp[digit >> 1]);
  198. }
  199. int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const EC_SCALAR *g_scalar,
  200. const EC_POINT *p, const EC_SCALAR *p_scalar, BN_CTX *ctx) {
  201. BN_CTX *new_ctx = NULL;
  202. EC_POINT *precomp_storage[2 * (1 << (EC_WNAF_MAX_WINDOW_BITS - 1))] = {NULL};
  203. EC_POINT **g_precomp = NULL, **p_precomp = NULL;
  204. int8_t g_wNAF[EC_MAX_SCALAR_BYTES * 8 + 1];
  205. int8_t p_wNAF[EC_MAX_SCALAR_BYTES * 8 + 1];
  206. EC_POINT *tmp = NULL;
  207. int ret = 0;
  208. if (ctx == NULL) {
  209. ctx = new_ctx = BN_CTX_new();
  210. if (ctx == NULL) {
  211. goto err;
  212. }
  213. }
  214. size_t bits = BN_num_bits(&group->order);
  215. size_t wsize = window_bits_for_scalar_size(bits);
  216. size_t wNAF_len = bits + 1;
  217. size_t precomp_len = (size_t)1 << (wsize - 1);
  218. OPENSSL_COMPILE_ASSERT(
  219. OPENSSL_ARRAY_SIZE(g_wNAF) == OPENSSL_ARRAY_SIZE(p_wNAF),
  220. g_wNAF_and_p_wNAF_are_different_sizes);
  221. if (wNAF_len > OPENSSL_ARRAY_SIZE(g_wNAF) ||
  222. 2 * precomp_len > OPENSSL_ARRAY_SIZE(precomp_storage)) {
  223. OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
  224. goto err;
  225. }
  226. // TODO(davidben): |mul_public| is for ECDSA verification which can assume
  227. // non-NULL inputs, but this code is also used for |mul| which cannot. It's
  228. // not constant-time, so replace the generic |mul| and remove the NULL checks.
  229. size_t total_precomp = 0;
  230. if (g_scalar != NULL) {
  231. const EC_POINT *g = EC_GROUP_get0_generator(group);
  232. if (g == NULL) {
  233. OPENSSL_PUT_ERROR(EC, EC_R_UNDEFINED_GENERATOR);
  234. goto err;
  235. }
  236. g_precomp = precomp_storage + total_precomp;
  237. total_precomp += precomp_len;
  238. if (!ec_compute_wNAF(group, g_wNAF, g_scalar, bits, wsize) ||
  239. !compute_precomp(group, g_precomp, g, precomp_len, ctx)) {
  240. goto err;
  241. }
  242. }
  243. if (p_scalar != NULL) {
  244. p_precomp = precomp_storage + total_precomp;
  245. total_precomp += precomp_len;
  246. if (!ec_compute_wNAF(group, p_wNAF, p_scalar, bits, wsize) ||
  247. !compute_precomp(group, p_precomp, p, precomp_len, ctx)) {
  248. goto err;
  249. }
  250. }
  251. tmp = EC_POINT_new(group);
  252. if (tmp == NULL ||
  253. // |window_bits_for_scalar_size| assumes we do this step.
  254. !EC_POINTs_make_affine(group, total_precomp, precomp_storage, ctx)) {
  255. goto err;
  256. }
  257. int r_is_at_infinity = 1;
  258. for (size_t k = wNAF_len - 1; k < wNAF_len; k--) {
  259. if (!r_is_at_infinity && !EC_POINT_dbl(group, r, r, ctx)) {
  260. goto err;
  261. }
  262. if (g_scalar != NULL) {
  263. if (g_wNAF[k] != 0) {
  264. if (!lookup_precomp(group, tmp, g_precomp, g_wNAF[k], ctx)) {
  265. goto err;
  266. }
  267. if (r_is_at_infinity) {
  268. if (!EC_POINT_copy(r, tmp)) {
  269. goto err;
  270. }
  271. r_is_at_infinity = 0;
  272. } else if (!EC_POINT_add(group, r, r, tmp, ctx)) {
  273. goto err;
  274. }
  275. }
  276. }
  277. if (p_scalar != NULL) {
  278. if (p_wNAF[k] != 0) {
  279. if (!lookup_precomp(group, tmp, p_precomp, p_wNAF[k], ctx)) {
  280. goto err;
  281. }
  282. if (r_is_at_infinity) {
  283. if (!EC_POINT_copy(r, tmp)) {
  284. goto err;
  285. }
  286. r_is_at_infinity = 0;
  287. } else if (!EC_POINT_add(group, r, r, tmp, ctx)) {
  288. goto err;
  289. }
  290. }
  291. }
  292. }
  293. if (r_is_at_infinity &&
  294. !EC_POINT_set_to_infinity(group, r)) {
  295. goto err;
  296. }
  297. ret = 1;
  298. err:
  299. BN_CTX_free(new_ctx);
  300. EC_POINT_free(tmp);
  301. OPENSSL_cleanse(&g_wNAF, sizeof(g_wNAF));
  302. OPENSSL_cleanse(&p_wNAF, sizeof(p_wNAF));
  303. for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(precomp_storage); i++) {
  304. EC_POINT_free(precomp_storage[i]);
  305. }
  306. return ret;
  307. }