ec_mult.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /* crypto/ec/ec_mult.c */
  2. /*
  3. * Originally written by Bodo Moeller and Nils Larsch for the OpenSSL project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1998-2007 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. * openssl-core@openssl.org.
  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. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. /* ====================================================================
  59. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  60. * Portions of this software developed by SUN MICROSYSTEMS, INC.,
  61. * and contributed to the OpenSSL project.
  62. */
  63. #include <string.h>
  64. #include <openssl/err.h>
  65. #include "ec_lcl.h"
  66. /*
  67. * This file implements the wNAF-based interleaving multi-exponentiation method
  68. * Formerly at:
  69. * http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/moeller.html#multiexp
  70. * You might now find it here:
  71. * http://link.springer.com/chapter/10.1007%2F3-540-45537-X_13
  72. * http://www.bmoeller.de/pdf/TI-01-08.multiexp.pdf
  73. * For multiplication with precomputation, we use wNAF splitting, formerly at:
  74. * http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/moeller.html#fastexp
  75. */
  76. /* structure for precomputed multiples of the generator */
  77. typedef struct ec_pre_comp_st {
  78. const EC_GROUP *group; /* parent EC_GROUP object */
  79. size_t blocksize; /* block size for wNAF splitting */
  80. size_t numblocks; /* max. number of blocks for which we have
  81. * precomputation */
  82. size_t w; /* window size */
  83. EC_POINT **points; /* array with pre-calculated multiples of
  84. * generator: 'num' pointers to EC_POINT
  85. * objects followed by a NULL */
  86. size_t num; /* numblocks * 2^(w-1) */
  87. int references;
  88. } EC_PRE_COMP;
  89. /* functions to manage EC_PRE_COMP within the EC_GROUP extra_data framework */
  90. static void *ec_pre_comp_dup(void *);
  91. static void ec_pre_comp_free(void *);
  92. static void ec_pre_comp_clear_free(void *);
  93. static EC_PRE_COMP *ec_pre_comp_new(const EC_GROUP *group)
  94. {
  95. EC_PRE_COMP *ret = NULL;
  96. if (!group)
  97. return NULL;
  98. ret = (EC_PRE_COMP *)OPENSSL_malloc(sizeof(EC_PRE_COMP));
  99. if (!ret) {
  100. ECerr(EC_F_EC_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
  101. return ret;
  102. }
  103. ret->group = group;
  104. ret->blocksize = 8; /* default */
  105. ret->numblocks = 0;
  106. ret->w = 4; /* default */
  107. ret->points = NULL;
  108. ret->num = 0;
  109. ret->references = 1;
  110. return ret;
  111. }
  112. static void *ec_pre_comp_dup(void *src_)
  113. {
  114. EC_PRE_COMP *src = src_;
  115. /* no need to actually copy, these objects never change! */
  116. CRYPTO_add(&src->references, 1, CRYPTO_LOCK_EC_PRE_COMP);
  117. return src_;
  118. }
  119. static void ec_pre_comp_free(void *pre_)
  120. {
  121. int i;
  122. EC_PRE_COMP *pre = pre_;
  123. if (!pre)
  124. return;
  125. i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
  126. if (i > 0)
  127. return;
  128. if (pre->points) {
  129. EC_POINT **p;
  130. for (p = pre->points; *p != NULL; p++)
  131. EC_POINT_free(*p);
  132. OPENSSL_free(pre->points);
  133. }
  134. OPENSSL_free(pre);
  135. }
  136. static void ec_pre_comp_clear_free(void *pre_)
  137. {
  138. int i;
  139. EC_PRE_COMP *pre = pre_;
  140. if (!pre)
  141. return;
  142. i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
  143. if (i > 0)
  144. return;
  145. if (pre->points) {
  146. EC_POINT **p;
  147. for (p = pre->points; *p != NULL; p++) {
  148. EC_POINT_clear_free(*p);
  149. OPENSSL_cleanse(p, sizeof(*p));
  150. }
  151. OPENSSL_free(pre->points);
  152. }
  153. OPENSSL_cleanse(pre, sizeof(*pre));
  154. OPENSSL_free(pre);
  155. }
  156. /*-
  157. * Determine the modified width-(w+1) Non-Adjacent Form (wNAF) of 'scalar'.
  158. * This is an array r[] of values that are either zero or odd with an
  159. * absolute value less than 2^w satisfying
  160. * scalar = \sum_j r[j]*2^j
  161. * where at most one of any w+1 consecutive digits is non-zero
  162. * with the exception that the most significant digit may be only
  163. * w-1 zeros away from that next non-zero digit.
  164. */
  165. static signed char *compute_wNAF(const BIGNUM *scalar, int w, size_t *ret_len)
  166. {
  167. int window_val;
  168. int ok = 0;
  169. signed char *r = NULL;
  170. int sign = 1;
  171. int bit, next_bit, mask;
  172. size_t len = 0, j;
  173. if (BN_is_zero(scalar)) {
  174. r = OPENSSL_malloc(1);
  175. if (!r) {
  176. ECerr(EC_F_COMPUTE_WNAF, ERR_R_MALLOC_FAILURE);
  177. goto err;
  178. }
  179. r[0] = 0;
  180. *ret_len = 1;
  181. return r;
  182. }
  183. if (w <= 0 || w > 7) { /* 'signed char' can represent integers with
  184. * absolute values less than 2^7 */
  185. ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
  186. goto err;
  187. }
  188. bit = 1 << w; /* at most 128 */
  189. next_bit = bit << 1; /* at most 256 */
  190. mask = next_bit - 1; /* at most 255 */
  191. if (BN_is_negative(scalar)) {
  192. sign = -1;
  193. }
  194. if (scalar->d == NULL || scalar->top == 0) {
  195. ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
  196. goto err;
  197. }
  198. len = BN_num_bits(scalar);
  199. r = OPENSSL_malloc(len + 1); /* modified wNAF may be one digit longer
  200. * than binary representation (*ret_len will
  201. * be set to the actual length, i.e. at most
  202. * BN_num_bits(scalar) + 1) */
  203. if (r == NULL) {
  204. ECerr(EC_F_COMPUTE_WNAF, ERR_R_MALLOC_FAILURE);
  205. goto err;
  206. }
  207. window_val = scalar->d[0] & mask;
  208. j = 0;
  209. while ((window_val != 0) || (j + w + 1 < len)) { /* if j+w+1 >= len,
  210. * window_val will not
  211. * increase */
  212. int digit = 0;
  213. /* 0 <= window_val <= 2^(w+1) */
  214. if (window_val & 1) {
  215. /* 0 < window_val < 2^(w+1) */
  216. if (window_val & bit) {
  217. digit = window_val - next_bit; /* -2^w < digit < 0 */
  218. #if 1 /* modified wNAF */
  219. if (j + w + 1 >= len) {
  220. /*
  221. * special case for generating modified wNAFs: no new
  222. * bits will be added into window_val, so using a
  223. * positive digit here will decrease the total length of
  224. * the representation
  225. */
  226. digit = window_val & (mask >> 1); /* 0 < digit < 2^w */
  227. }
  228. #endif
  229. } else {
  230. digit = window_val; /* 0 < digit < 2^w */
  231. }
  232. if (digit <= -bit || digit >= bit || !(digit & 1)) {
  233. ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
  234. goto err;
  235. }
  236. window_val -= digit;
  237. /*
  238. * now window_val is 0 or 2^(w+1) in standard wNAF generation;
  239. * for modified window NAFs, it may also be 2^w
  240. */
  241. if (window_val != 0 && window_val != next_bit
  242. && window_val != bit) {
  243. ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
  244. goto err;
  245. }
  246. }
  247. r[j++] = sign * digit;
  248. window_val >>= 1;
  249. window_val += bit * BN_is_bit_set(scalar, j + w);
  250. if (window_val > next_bit) {
  251. ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
  252. goto err;
  253. }
  254. }
  255. if (j > len + 1) {
  256. ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
  257. goto err;
  258. }
  259. len = j;
  260. ok = 1;
  261. err:
  262. if (!ok) {
  263. OPENSSL_free(r);
  264. r = NULL;
  265. }
  266. if (ok)
  267. *ret_len = len;
  268. return r;
  269. }
  270. /*
  271. * TODO: table should be optimised for the wNAF-based implementation,
  272. * sometimes smaller windows will give better performance (thus the
  273. * boundaries should be increased)
  274. */
  275. #define EC_window_bits_for_scalar_size(b) \
  276. ((size_t) \
  277. ((b) >= 2000 ? 6 : \
  278. (b) >= 800 ? 5 : \
  279. (b) >= 300 ? 4 : \
  280. (b) >= 70 ? 3 : \
  281. (b) >= 20 ? 2 : \
  282. 1))
  283. /*-
  284. * Compute
  285. * \sum scalars[i]*points[i],
  286. * also including
  287. * scalar*generator
  288. * in the addition if scalar != NULL
  289. */
  290. int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
  291. size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
  292. BN_CTX *ctx)
  293. {
  294. BN_CTX *new_ctx = NULL;
  295. const EC_POINT *generator = NULL;
  296. EC_POINT *tmp = NULL;
  297. size_t totalnum;
  298. size_t blocksize = 0, numblocks = 0; /* for wNAF splitting */
  299. size_t pre_points_per_block = 0;
  300. size_t i, j;
  301. int k;
  302. int r_is_inverted = 0;
  303. int r_is_at_infinity = 1;
  304. size_t *wsize = NULL; /* individual window sizes */
  305. signed char **wNAF = NULL; /* individual wNAFs */
  306. size_t *wNAF_len = NULL;
  307. size_t max_len = 0;
  308. size_t num_val;
  309. EC_POINT **val = NULL; /* precomputation */
  310. EC_POINT **v;
  311. EC_POINT ***val_sub = NULL; /* pointers to sub-arrays of 'val' or
  312. * 'pre_comp->points' */
  313. const EC_PRE_COMP *pre_comp = NULL;
  314. int num_scalar = 0; /* flag: will be set to 1 if 'scalar' must be
  315. * treated like other scalars, i.e.
  316. * precomputation is not available */
  317. int ret = 0;
  318. if (group->meth != r->meth) {
  319. ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);
  320. return 0;
  321. }
  322. if ((scalar == NULL) && (num == 0)) {
  323. return EC_POINT_set_to_infinity(group, r);
  324. }
  325. for (i = 0; i < num; i++) {
  326. if (group->meth != points[i]->meth) {
  327. ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);
  328. return 0;
  329. }
  330. }
  331. if (ctx == NULL) {
  332. ctx = new_ctx = BN_CTX_new();
  333. if (ctx == NULL)
  334. goto err;
  335. }
  336. if (scalar != NULL) {
  337. generator = EC_GROUP_get0_generator(group);
  338. if (generator == NULL) {
  339. ECerr(EC_F_EC_WNAF_MUL, EC_R_UNDEFINED_GENERATOR);
  340. goto err;
  341. }
  342. /* look if we can use precomputed multiples of generator */
  343. pre_comp =
  344. EC_EX_DATA_get_data(group->extra_data, ec_pre_comp_dup,
  345. ec_pre_comp_free, ec_pre_comp_clear_free);
  346. if (pre_comp && pre_comp->numblocks
  347. && (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) ==
  348. 0)) {
  349. blocksize = pre_comp->blocksize;
  350. /*
  351. * determine maximum number of blocks that wNAF splitting may
  352. * yield (NB: maximum wNAF length is bit length plus one)
  353. */
  354. numblocks = (BN_num_bits(scalar) / blocksize) + 1;
  355. /*
  356. * we cannot use more blocks than we have precomputation for
  357. */
  358. if (numblocks > pre_comp->numblocks)
  359. numblocks = pre_comp->numblocks;
  360. pre_points_per_block = (size_t)1 << (pre_comp->w - 1);
  361. /* check that pre_comp looks sane */
  362. if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block)) {
  363. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  364. goto err;
  365. }
  366. } else {
  367. /* can't use precomputation */
  368. pre_comp = NULL;
  369. numblocks = 1;
  370. num_scalar = 1; /* treat 'scalar' like 'num'-th element of
  371. * 'scalars' */
  372. }
  373. }
  374. totalnum = num + numblocks;
  375. wsize = OPENSSL_malloc(totalnum * sizeof(wsize[0]));
  376. wNAF_len = OPENSSL_malloc(totalnum * sizeof(wNAF_len[0]));
  377. /* include space for pivot */
  378. wNAF = OPENSSL_malloc((totalnum + 1) * sizeof(wNAF[0]));
  379. val_sub = OPENSSL_malloc(totalnum * sizeof(val_sub[0]));
  380. /* Ensure wNAF is initialised in case we end up going to err */
  381. if (wNAF)
  382. wNAF[0] = NULL; /* preliminary pivot */
  383. if (!wsize || !wNAF_len || !wNAF || !val_sub) {
  384. ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
  385. goto err;
  386. }
  387. /*
  388. * num_val will be the total number of temporarily precomputed points
  389. */
  390. num_val = 0;
  391. for (i = 0; i < num + num_scalar; i++) {
  392. size_t bits;
  393. bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar);
  394. wsize[i] = EC_window_bits_for_scalar_size(bits);
  395. num_val += (size_t)1 << (wsize[i] - 1);
  396. wNAF[i + 1] = NULL; /* make sure we always have a pivot */
  397. wNAF[i] =
  398. compute_wNAF((i < num ? scalars[i] : scalar), wsize[i],
  399. &wNAF_len[i]);
  400. if (wNAF[i] == NULL)
  401. goto err;
  402. if (wNAF_len[i] > max_len)
  403. max_len = wNAF_len[i];
  404. }
  405. if (numblocks) {
  406. /* we go here iff scalar != NULL */
  407. if (pre_comp == NULL) {
  408. if (num_scalar != 1) {
  409. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  410. goto err;
  411. }
  412. /* we have already generated a wNAF for 'scalar' */
  413. } else {
  414. signed char *tmp_wNAF = NULL;
  415. size_t tmp_len = 0;
  416. if (num_scalar != 0) {
  417. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  418. goto err;
  419. }
  420. /*
  421. * use the window size for which we have precomputation
  422. */
  423. wsize[num] = pre_comp->w;
  424. tmp_wNAF = compute_wNAF(scalar, wsize[num], &tmp_len);
  425. if (!tmp_wNAF)
  426. goto err;
  427. if (tmp_len <= max_len) {
  428. /*
  429. * One of the other wNAFs is at least as long as the wNAF
  430. * belonging to the generator, so wNAF splitting will not buy
  431. * us anything.
  432. */
  433. numblocks = 1;
  434. totalnum = num + 1; /* don't use wNAF splitting */
  435. wNAF[num] = tmp_wNAF;
  436. wNAF[num + 1] = NULL;
  437. wNAF_len[num] = tmp_len;
  438. if (tmp_len > max_len)
  439. max_len = tmp_len;
  440. /*
  441. * pre_comp->points starts with the points that we need here:
  442. */
  443. val_sub[num] = pre_comp->points;
  444. } else {
  445. /*
  446. * don't include tmp_wNAF directly into wNAF array - use wNAF
  447. * splitting and include the blocks
  448. */
  449. signed char *pp;
  450. EC_POINT **tmp_points;
  451. if (tmp_len < numblocks * blocksize) {
  452. /*
  453. * possibly we can do with fewer blocks than estimated
  454. */
  455. numblocks = (tmp_len + blocksize - 1) / blocksize;
  456. if (numblocks > pre_comp->numblocks) {
  457. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  458. goto err;
  459. }
  460. totalnum = num + numblocks;
  461. }
  462. /* split wNAF in 'numblocks' parts */
  463. pp = tmp_wNAF;
  464. tmp_points = pre_comp->points;
  465. for (i = num; i < totalnum; i++) {
  466. if (i < totalnum - 1) {
  467. wNAF_len[i] = blocksize;
  468. if (tmp_len < blocksize) {
  469. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  470. goto err;
  471. }
  472. tmp_len -= blocksize;
  473. } else
  474. /*
  475. * last block gets whatever is left (this could be
  476. * more or less than 'blocksize'!)
  477. */
  478. wNAF_len[i] = tmp_len;
  479. wNAF[i + 1] = NULL;
  480. wNAF[i] = OPENSSL_malloc(wNAF_len[i]);
  481. if (wNAF[i] == NULL) {
  482. ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
  483. OPENSSL_free(tmp_wNAF);
  484. goto err;
  485. }
  486. memcpy(wNAF[i], pp, wNAF_len[i]);
  487. if (wNAF_len[i] > max_len)
  488. max_len = wNAF_len[i];
  489. if (*tmp_points == NULL) {
  490. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  491. OPENSSL_free(tmp_wNAF);
  492. goto err;
  493. }
  494. val_sub[i] = tmp_points;
  495. tmp_points += pre_points_per_block;
  496. pp += blocksize;
  497. }
  498. OPENSSL_free(tmp_wNAF);
  499. }
  500. }
  501. }
  502. /*
  503. * All points we precompute now go into a single array 'val'.
  504. * 'val_sub[i]' is a pointer to the subarray for the i-th point, or to a
  505. * subarray of 'pre_comp->points' if we already have precomputation.
  506. */
  507. val = OPENSSL_malloc((num_val + 1) * sizeof(val[0]));
  508. if (val == NULL) {
  509. ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
  510. goto err;
  511. }
  512. val[num_val] = NULL; /* pivot element */
  513. /* allocate points for precomputation */
  514. v = val;
  515. for (i = 0; i < num + num_scalar; i++) {
  516. val_sub[i] = v;
  517. for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) {
  518. *v = EC_POINT_new(group);
  519. if (*v == NULL)
  520. goto err;
  521. v++;
  522. }
  523. }
  524. if (!(v == val + num_val)) {
  525. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  526. goto err;
  527. }
  528. if (!(tmp = EC_POINT_new(group)))
  529. goto err;
  530. /*-
  531. * prepare precomputed values:
  532. * val_sub[i][0] := points[i]
  533. * val_sub[i][1] := 3 * points[i]
  534. * val_sub[i][2] := 5 * points[i]
  535. * ...
  536. */
  537. for (i = 0; i < num + num_scalar; i++) {
  538. if (i < num) {
  539. if (!EC_POINT_copy(val_sub[i][0], points[i]))
  540. goto err;
  541. } else {
  542. if (!EC_POINT_copy(val_sub[i][0], generator))
  543. goto err;
  544. }
  545. if (wsize[i] > 1) {
  546. if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx))
  547. goto err;
  548. for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) {
  549. if (!EC_POINT_add
  550. (group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx))
  551. goto err;
  552. }
  553. }
  554. }
  555. #if 1 /* optional; EC_window_bits_for_scalar_size
  556. * assumes we do this step */
  557. if (!EC_POINTs_make_affine(group, num_val, val, ctx))
  558. goto err;
  559. #endif
  560. r_is_at_infinity = 1;
  561. for (k = max_len - 1; k >= 0; k--) {
  562. if (!r_is_at_infinity) {
  563. if (!EC_POINT_dbl(group, r, r, ctx))
  564. goto err;
  565. }
  566. for (i = 0; i < totalnum; i++) {
  567. if (wNAF_len[i] > (size_t)k) {
  568. int digit = wNAF[i][k];
  569. int is_neg;
  570. if (digit) {
  571. is_neg = digit < 0;
  572. if (is_neg)
  573. digit = -digit;
  574. if (is_neg != r_is_inverted) {
  575. if (!r_is_at_infinity) {
  576. if (!EC_POINT_invert(group, r, ctx))
  577. goto err;
  578. }
  579. r_is_inverted = !r_is_inverted;
  580. }
  581. /* digit > 0 */
  582. if (r_is_at_infinity) {
  583. if (!EC_POINT_copy(r, val_sub[i][digit >> 1]))
  584. goto err;
  585. r_is_at_infinity = 0;
  586. } else {
  587. if (!EC_POINT_add
  588. (group, r, r, val_sub[i][digit >> 1], ctx))
  589. goto err;
  590. }
  591. }
  592. }
  593. }
  594. }
  595. if (r_is_at_infinity) {
  596. if (!EC_POINT_set_to_infinity(group, r))
  597. goto err;
  598. } else {
  599. if (r_is_inverted)
  600. if (!EC_POINT_invert(group, r, ctx))
  601. goto err;
  602. }
  603. ret = 1;
  604. err:
  605. if (new_ctx != NULL)
  606. BN_CTX_free(new_ctx);
  607. if (tmp != NULL)
  608. EC_POINT_free(tmp);
  609. if (wsize != NULL)
  610. OPENSSL_free(wsize);
  611. if (wNAF_len != NULL)
  612. OPENSSL_free(wNAF_len);
  613. if (wNAF != NULL) {
  614. signed char **w;
  615. for (w = wNAF; *w != NULL; w++)
  616. OPENSSL_free(*w);
  617. OPENSSL_free(wNAF);
  618. }
  619. if (val != NULL) {
  620. for (v = val; *v != NULL; v++)
  621. EC_POINT_clear_free(*v);
  622. OPENSSL_free(val);
  623. }
  624. if (val_sub != NULL) {
  625. OPENSSL_free(val_sub);
  626. }
  627. return ret;
  628. }
  629. /*-
  630. * ec_wNAF_precompute_mult()
  631. * creates an EC_PRE_COMP object with preprecomputed multiples of the generator
  632. * for use with wNAF splitting as implemented in ec_wNAF_mul().
  633. *
  634. * 'pre_comp->points' is an array of multiples of the generator
  635. * of the following form:
  636. * points[0] = generator;
  637. * points[1] = 3 * generator;
  638. * ...
  639. * points[2^(w-1)-1] = (2^(w-1)-1) * generator;
  640. * points[2^(w-1)] = 2^blocksize * generator;
  641. * points[2^(w-1)+1] = 3 * 2^blocksize * generator;
  642. * ...
  643. * points[2^(w-1)*(numblocks-1)-1] = (2^(w-1)) * 2^(blocksize*(numblocks-2)) * generator
  644. * points[2^(w-1)*(numblocks-1)] = 2^(blocksize*(numblocks-1)) * generator
  645. * ...
  646. * points[2^(w-1)*numblocks-1] = (2^(w-1)) * 2^(blocksize*(numblocks-1)) * generator
  647. * points[2^(w-1)*numblocks] = NULL
  648. */
  649. int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
  650. {
  651. const EC_POINT *generator;
  652. EC_POINT *tmp_point = NULL, *base = NULL, **var;
  653. BN_CTX *new_ctx = NULL;
  654. BIGNUM *order;
  655. size_t i, bits, w, pre_points_per_block, blocksize, numblocks, num;
  656. EC_POINT **points = NULL;
  657. EC_PRE_COMP *pre_comp;
  658. int ret = 0;
  659. /* if there is an old EC_PRE_COMP object, throw it away */
  660. EC_EX_DATA_free_data(&group->extra_data, ec_pre_comp_dup,
  661. ec_pre_comp_free, ec_pre_comp_clear_free);
  662. if ((pre_comp = ec_pre_comp_new(group)) == NULL)
  663. return 0;
  664. generator = EC_GROUP_get0_generator(group);
  665. if (generator == NULL) {
  666. ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, EC_R_UNDEFINED_GENERATOR);
  667. goto err;
  668. }
  669. if (ctx == NULL) {
  670. ctx = new_ctx = BN_CTX_new();
  671. if (ctx == NULL)
  672. goto err;
  673. }
  674. BN_CTX_start(ctx);
  675. order = BN_CTX_get(ctx);
  676. if (order == NULL)
  677. goto err;
  678. if (!EC_GROUP_get_order(group, order, ctx))
  679. goto err;
  680. if (BN_is_zero(order)) {
  681. ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, EC_R_UNKNOWN_ORDER);
  682. goto err;
  683. }
  684. bits = BN_num_bits(order);
  685. /*
  686. * The following parameters mean we precompute (approximately) one point
  687. * per bit. TBD: The combination 8, 4 is perfect for 160 bits; for other
  688. * bit lengths, other parameter combinations might provide better
  689. * efficiency.
  690. */
  691. blocksize = 8;
  692. w = 4;
  693. if (EC_window_bits_for_scalar_size(bits) > w) {
  694. /* let's not make the window too small ... */
  695. w = EC_window_bits_for_scalar_size(bits);
  696. }
  697. numblocks = (bits + blocksize - 1) / blocksize; /* max. number of blocks
  698. * to use for wNAF
  699. * splitting */
  700. pre_points_per_block = (size_t)1 << (w - 1);
  701. num = pre_points_per_block * numblocks; /* number of points to compute
  702. * and store */
  703. points = OPENSSL_malloc(sizeof(EC_POINT *) * (num + 1));
  704. if (!points) {
  705. ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
  706. goto err;
  707. }
  708. var = points;
  709. var[num] = NULL; /* pivot */
  710. for (i = 0; i < num; i++) {
  711. if ((var[i] = EC_POINT_new(group)) == NULL) {
  712. ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
  713. goto err;
  714. }
  715. }
  716. if (!(tmp_point = EC_POINT_new(group)) || !(base = EC_POINT_new(group))) {
  717. ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
  718. goto err;
  719. }
  720. if (!EC_POINT_copy(base, generator))
  721. goto err;
  722. /* do the precomputation */
  723. for (i = 0; i < numblocks; i++) {
  724. size_t j;
  725. if (!EC_POINT_dbl(group, tmp_point, base, ctx))
  726. goto err;
  727. if (!EC_POINT_copy(*var++, base))
  728. goto err;
  729. for (j = 1; j < pre_points_per_block; j++, var++) {
  730. /*
  731. * calculate odd multiples of the current base point
  732. */
  733. if (!EC_POINT_add(group, *var, tmp_point, *(var - 1), ctx))
  734. goto err;
  735. }
  736. if (i < numblocks - 1) {
  737. /*
  738. * get the next base (multiply current one by 2^blocksize)
  739. */
  740. size_t k;
  741. if (blocksize <= 2) {
  742. ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_INTERNAL_ERROR);
  743. goto err;
  744. }
  745. if (!EC_POINT_dbl(group, base, tmp_point, ctx))
  746. goto err;
  747. for (k = 2; k < blocksize; k++) {
  748. if (!EC_POINT_dbl(group, base, base, ctx))
  749. goto err;
  750. }
  751. }
  752. }
  753. if (!EC_POINTs_make_affine(group, num, points, ctx))
  754. goto err;
  755. pre_comp->group = group;
  756. pre_comp->blocksize = blocksize;
  757. pre_comp->numblocks = numblocks;
  758. pre_comp->w = w;
  759. pre_comp->points = points;
  760. points = NULL;
  761. pre_comp->num = num;
  762. if (!EC_EX_DATA_set_data(&group->extra_data, pre_comp,
  763. ec_pre_comp_dup, ec_pre_comp_free,
  764. ec_pre_comp_clear_free))
  765. goto err;
  766. pre_comp = NULL;
  767. ret = 1;
  768. err:
  769. if (ctx != NULL)
  770. BN_CTX_end(ctx);
  771. if (new_ctx != NULL)
  772. BN_CTX_free(new_ctx);
  773. if (pre_comp)
  774. ec_pre_comp_free(pre_comp);
  775. if (points) {
  776. EC_POINT **p;
  777. for (p = points; *p != NULL; p++)
  778. EC_POINT_free(*p);
  779. OPENSSL_free(points);
  780. }
  781. if (tmp_point)
  782. EC_POINT_free(tmp_point);
  783. if (base)
  784. EC_POINT_free(base);
  785. return ret;
  786. }
  787. int ec_wNAF_have_precompute_mult(const EC_GROUP *group)
  788. {
  789. if (EC_EX_DATA_get_data
  790. (group->extra_data, ec_pre_comp_dup, ec_pre_comp_free,
  791. ec_pre_comp_clear_free) != NULL)
  792. return 1;
  793. else
  794. return 0;
  795. }