ec_mult.c 28 KB

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