ec_mult.c 23 KB

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