ec_mult.c 23 KB

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