2
0

ec_mult.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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 "internal/bn_int.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. 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
  78. * precomputation */
  79. size_t w; /* window size */
  80. EC_POINT **points; /* array with pre-calculated multiples of
  81. * generator: 'num' pointers to EC_POINT
  82. * objects followed by a NULL */
  83. size_t num; /* numblocks * 2^(w-1) */
  84. int references;
  85. };
  86. static EC_PRE_COMP *ec_pre_comp_new(const EC_GROUP *group)
  87. {
  88. EC_PRE_COMP *ret = NULL;
  89. if (!group)
  90. return NULL;
  91. ret = OPENSSL_zalloc(sizeof(*ret));
  92. if (ret == NULL) {
  93. ECerr(EC_F_EC_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
  94. return ret;
  95. }
  96. ret->group = group;
  97. ret->blocksize = 8; /* default */
  98. ret->w = 4; /* default */
  99. ret->references = 1;
  100. return ret;
  101. }
  102. EC_PRE_COMP *EC_ec_pre_comp_dup(EC_PRE_COMP *pre)
  103. {
  104. if (pre != NULL)
  105. CRYPTO_add(&pre->references, 1, CRYPTO_LOCK_EC_PRE_COMP);
  106. return pre;
  107. }
  108. void EC_ec_pre_comp_free(EC_PRE_COMP *pre)
  109. {
  110. if (pre == NULL
  111. || CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP) > 0)
  112. return;
  113. if (pre->points != NULL) {
  114. EC_POINT **pts;
  115. for (pts = pre->points; *pts != NULL; pts++)
  116. EC_POINT_free(*pts);
  117. OPENSSL_free(pre->points);
  118. }
  119. OPENSSL_free(pre);
  120. }
  121. /*
  122. * TODO: table should be optimised for the wNAF-based implementation,
  123. * sometimes smaller windows will give better performance (thus the
  124. * boundaries should be increased)
  125. */
  126. #define EC_window_bits_for_scalar_size(b) \
  127. ((size_t) \
  128. ((b) >= 2000 ? 6 : \
  129. (b) >= 800 ? 5 : \
  130. (b) >= 300 ? 4 : \
  131. (b) >= 70 ? 3 : \
  132. (b) >= 20 ? 2 : \
  133. 1))
  134. /*-
  135. * Compute
  136. * \sum scalars[i]*points[i],
  137. * also including
  138. * scalar*generator
  139. * in the addition if scalar != NULL
  140. */
  141. int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
  142. size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
  143. BN_CTX *ctx)
  144. {
  145. BN_CTX *new_ctx = NULL;
  146. const EC_POINT *generator = NULL;
  147. EC_POINT *tmp = NULL;
  148. size_t totalnum;
  149. size_t blocksize = 0, numblocks = 0; /* for wNAF splitting */
  150. size_t pre_points_per_block = 0;
  151. size_t i, j;
  152. int k;
  153. int r_is_inverted = 0;
  154. int r_is_at_infinity = 1;
  155. size_t *wsize = NULL; /* individual window sizes */
  156. signed char **wNAF = NULL; /* individual wNAFs */
  157. size_t *wNAF_len = NULL;
  158. size_t max_len = 0;
  159. size_t num_val;
  160. EC_POINT **val = NULL; /* precomputation */
  161. EC_POINT **v;
  162. EC_POINT ***val_sub = NULL; /* pointers to sub-arrays of 'val' or
  163. * 'pre_comp->points' */
  164. const EC_PRE_COMP *pre_comp = NULL;
  165. int num_scalar = 0; /* flag: will be set to 1 if 'scalar' must be
  166. * treated like other scalars, i.e.
  167. * precomputation is not available */
  168. int ret = 0;
  169. if (group->meth != r->meth) {
  170. ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);
  171. return 0;
  172. }
  173. if ((scalar == NULL) && (num == 0)) {
  174. return EC_POINT_set_to_infinity(group, r);
  175. }
  176. for (i = 0; i < num; i++) {
  177. if (group->meth != points[i]->meth) {
  178. ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);
  179. return 0;
  180. }
  181. }
  182. if (ctx == NULL) {
  183. ctx = new_ctx = BN_CTX_new();
  184. if (ctx == NULL)
  185. goto err;
  186. }
  187. if (scalar != NULL) {
  188. generator = EC_GROUP_get0_generator(group);
  189. if (generator == NULL) {
  190. ECerr(EC_F_EC_WNAF_MUL, EC_R_UNDEFINED_GENERATOR);
  191. goto err;
  192. }
  193. /* look if we can use precomputed multiples of generator */
  194. pre_comp = group->pre_comp.ec;
  195. if (pre_comp && pre_comp->numblocks
  196. && (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) ==
  197. 0)) {
  198. blocksize = pre_comp->blocksize;
  199. /*
  200. * determine maximum number of blocks that wNAF splitting may
  201. * yield (NB: maximum wNAF length is bit length plus one)
  202. */
  203. numblocks = (BN_num_bits(scalar) / blocksize) + 1;
  204. /*
  205. * we cannot use more blocks than we have precomputation for
  206. */
  207. if (numblocks > pre_comp->numblocks)
  208. numblocks = pre_comp->numblocks;
  209. pre_points_per_block = (size_t)1 << (pre_comp->w - 1);
  210. /* check that pre_comp looks sane */
  211. if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block)) {
  212. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  213. goto err;
  214. }
  215. } else {
  216. /* can't use precomputation */
  217. pre_comp = NULL;
  218. numblocks = 1;
  219. num_scalar = 1; /* treat 'scalar' like 'num'-th element of
  220. * 'scalars' */
  221. }
  222. }
  223. totalnum = num + numblocks;
  224. wsize = OPENSSL_malloc(totalnum * sizeof wsize[0]);
  225. wNAF_len = OPENSSL_malloc(totalnum * sizeof wNAF_len[0]);
  226. wNAF = OPENSSL_malloc((totalnum + 1) * sizeof wNAF[0]); /* includes space
  227. * for pivot */
  228. val_sub = OPENSSL_malloc(totalnum * sizeof val_sub[0]);
  229. /* Ensure wNAF is initialised in case we end up going to err */
  230. if (wNAF != NULL)
  231. wNAF[0] = NULL; /* preliminary pivot */
  232. if (wsize == NULL || wNAF_len == NULL || wNAF == NULL || val_sub == NULL) {
  233. ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
  234. goto err;
  235. }
  236. /*
  237. * num_val will be the total number of temporarily precomputed points
  238. */
  239. num_val = 0;
  240. for (i = 0; i < num + num_scalar; i++) {
  241. size_t bits;
  242. bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar);
  243. wsize[i] = EC_window_bits_for_scalar_size(bits);
  244. num_val += (size_t)1 << (wsize[i] - 1);
  245. wNAF[i + 1] = NULL; /* make sure we always have a pivot */
  246. wNAF[i] =
  247. bn_compute_wNAF((i < num ? scalars[i] : scalar), wsize[i],
  248. &wNAF_len[i]);
  249. if (wNAF[i] == NULL)
  250. goto err;
  251. if (wNAF_len[i] > max_len)
  252. max_len = wNAF_len[i];
  253. }
  254. if (numblocks) {
  255. /* we go here iff scalar != NULL */
  256. if (pre_comp == NULL) {
  257. if (num_scalar != 1) {
  258. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  259. goto err;
  260. }
  261. /* we have already generated a wNAF for 'scalar' */
  262. } else {
  263. signed char *tmp_wNAF = NULL;
  264. size_t tmp_len = 0;
  265. if (num_scalar != 0) {
  266. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  267. goto err;
  268. }
  269. /*
  270. * use the window size for which we have precomputation
  271. */
  272. wsize[num] = pre_comp->w;
  273. tmp_wNAF = bn_compute_wNAF(scalar, wsize[num], &tmp_len);
  274. if (!tmp_wNAF)
  275. goto err;
  276. if (tmp_len <= max_len) {
  277. /*
  278. * One of the other wNAFs is at least as long as the wNAF
  279. * belonging to the generator, so wNAF splitting will not buy
  280. * us anything.
  281. */
  282. numblocks = 1;
  283. totalnum = num + 1; /* don't use wNAF splitting */
  284. wNAF[num] = tmp_wNAF;
  285. wNAF[num + 1] = NULL;
  286. wNAF_len[num] = tmp_len;
  287. if (tmp_len > max_len)
  288. max_len = tmp_len;
  289. /*
  290. * pre_comp->points starts with the points that we need here:
  291. */
  292. val_sub[num] = pre_comp->points;
  293. } else {
  294. /*
  295. * don't include tmp_wNAF directly into wNAF array - use wNAF
  296. * splitting and include the blocks
  297. */
  298. signed char *pp;
  299. EC_POINT **tmp_points;
  300. if (tmp_len < numblocks * blocksize) {
  301. /*
  302. * possibly we can do with fewer blocks than estimated
  303. */
  304. numblocks = (tmp_len + blocksize - 1) / blocksize;
  305. if (numblocks > pre_comp->numblocks) {
  306. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  307. goto err;
  308. }
  309. totalnum = num + numblocks;
  310. }
  311. /* split wNAF in 'numblocks' parts */
  312. pp = tmp_wNAF;
  313. tmp_points = pre_comp->points;
  314. for (i = num; i < totalnum; i++) {
  315. if (i < totalnum - 1) {
  316. wNAF_len[i] = blocksize;
  317. if (tmp_len < blocksize) {
  318. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  319. goto err;
  320. }
  321. tmp_len -= blocksize;
  322. } else
  323. /*
  324. * last block gets whatever is left (this could be
  325. * more or less than 'blocksize'!)
  326. */
  327. wNAF_len[i] = tmp_len;
  328. wNAF[i + 1] = NULL;
  329. wNAF[i] = OPENSSL_malloc(wNAF_len[i]);
  330. if (wNAF[i] == NULL) {
  331. ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
  332. OPENSSL_free(tmp_wNAF);
  333. goto err;
  334. }
  335. memcpy(wNAF[i], pp, wNAF_len[i]);
  336. if (wNAF_len[i] > max_len)
  337. max_len = wNAF_len[i];
  338. if (*tmp_points == NULL) {
  339. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  340. OPENSSL_free(tmp_wNAF);
  341. goto err;
  342. }
  343. val_sub[i] = tmp_points;
  344. tmp_points += pre_points_per_block;
  345. pp += blocksize;
  346. }
  347. OPENSSL_free(tmp_wNAF);
  348. }
  349. }
  350. }
  351. /*
  352. * All points we precompute now go into a single array 'val'.
  353. * 'val_sub[i]' is a pointer to the subarray for the i-th point, or to a
  354. * subarray of 'pre_comp->points' if we already have precomputation.
  355. */
  356. val = OPENSSL_malloc((num_val + 1) * sizeof val[0]);
  357. if (val == NULL) {
  358. ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
  359. goto err;
  360. }
  361. val[num_val] = NULL; /* pivot element */
  362. /* allocate points for precomputation */
  363. v = val;
  364. for (i = 0; i < num + num_scalar; i++) {
  365. val_sub[i] = v;
  366. for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) {
  367. *v = EC_POINT_new(group);
  368. if (*v == NULL)
  369. goto err;
  370. v++;
  371. }
  372. }
  373. if (!(v == val + num_val)) {
  374. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  375. goto err;
  376. }
  377. if ((tmp = EC_POINT_new(group)) == NULL)
  378. goto err;
  379. /*-
  380. * prepare precomputed values:
  381. * val_sub[i][0] := points[i]
  382. * val_sub[i][1] := 3 * points[i]
  383. * val_sub[i][2] := 5 * points[i]
  384. * ...
  385. */
  386. for (i = 0; i < num + num_scalar; i++) {
  387. if (i < num) {
  388. if (!EC_POINT_copy(val_sub[i][0], points[i]))
  389. goto err;
  390. } else {
  391. if (!EC_POINT_copy(val_sub[i][0], generator))
  392. goto err;
  393. }
  394. if (wsize[i] > 1) {
  395. if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx))
  396. goto err;
  397. for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) {
  398. if (!EC_POINT_add
  399. (group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx))
  400. goto err;
  401. }
  402. }
  403. }
  404. if (!EC_POINTs_make_affine(group, num_val, val, ctx))
  405. goto err;
  406. r_is_at_infinity = 1;
  407. for (k = max_len - 1; k >= 0; k--) {
  408. if (!r_is_at_infinity) {
  409. if (!EC_POINT_dbl(group, r, r, ctx))
  410. goto err;
  411. }
  412. for (i = 0; i < totalnum; i++) {
  413. if (wNAF_len[i] > (size_t)k) {
  414. int digit = wNAF[i][k];
  415. int is_neg;
  416. if (digit) {
  417. is_neg = digit < 0;
  418. if (is_neg)
  419. digit = -digit;
  420. if (is_neg != r_is_inverted) {
  421. if (!r_is_at_infinity) {
  422. if (!EC_POINT_invert(group, r, ctx))
  423. goto err;
  424. }
  425. r_is_inverted = !r_is_inverted;
  426. }
  427. /* digit > 0 */
  428. if (r_is_at_infinity) {
  429. if (!EC_POINT_copy(r, val_sub[i][digit >> 1]))
  430. goto err;
  431. r_is_at_infinity = 0;
  432. } else {
  433. if (!EC_POINT_add
  434. (group, r, r, val_sub[i][digit >> 1], ctx))
  435. goto err;
  436. }
  437. }
  438. }
  439. }
  440. }
  441. if (r_is_at_infinity) {
  442. if (!EC_POINT_set_to_infinity(group, r))
  443. goto err;
  444. } else {
  445. if (r_is_inverted)
  446. if (!EC_POINT_invert(group, r, ctx))
  447. goto err;
  448. }
  449. ret = 1;
  450. err:
  451. BN_CTX_free(new_ctx);
  452. EC_POINT_free(tmp);
  453. OPENSSL_free(wsize);
  454. OPENSSL_free(wNAF_len);
  455. if (wNAF != NULL) {
  456. signed char **w;
  457. for (w = wNAF; *w != NULL; w++)
  458. OPENSSL_free(*w);
  459. OPENSSL_free(wNAF);
  460. }
  461. if (val != NULL) {
  462. for (v = val; *v != NULL; v++)
  463. EC_POINT_clear_free(*v);
  464. OPENSSL_free(val);
  465. }
  466. OPENSSL_free(val_sub);
  467. return ret;
  468. }
  469. /*-
  470. * ec_wNAF_precompute_mult()
  471. * creates an EC_PRE_COMP object with preprecomputed multiples of the generator
  472. * for use with wNAF splitting as implemented in ec_wNAF_mul().
  473. *
  474. * 'pre_comp->points' is an array of multiples of the generator
  475. * of the following form:
  476. * points[0] = generator;
  477. * points[1] = 3 * generator;
  478. * ...
  479. * points[2^(w-1)-1] = (2^(w-1)-1) * generator;
  480. * points[2^(w-1)] = 2^blocksize * generator;
  481. * points[2^(w-1)+1] = 3 * 2^blocksize * generator;
  482. * ...
  483. * points[2^(w-1)*(numblocks-1)-1] = (2^(w-1)) * 2^(blocksize*(numblocks-2)) * generator
  484. * points[2^(w-1)*(numblocks-1)] = 2^(blocksize*(numblocks-1)) * generator
  485. * ...
  486. * points[2^(w-1)*numblocks-1] = (2^(w-1)) * 2^(blocksize*(numblocks-1)) * generator
  487. * points[2^(w-1)*numblocks] = NULL
  488. */
  489. int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
  490. {
  491. const EC_POINT *generator;
  492. EC_POINT *tmp_point = NULL, *base = NULL, **var;
  493. BN_CTX *new_ctx = NULL;
  494. BIGNUM *order;
  495. size_t i, bits, w, pre_points_per_block, blocksize, numblocks, num;
  496. EC_POINT **points = NULL;
  497. EC_PRE_COMP *pre_comp;
  498. int ret = 0;
  499. /* if there is an old EC_PRE_COMP object, throw it away */
  500. EC_pre_comp_free(group);
  501. if ((pre_comp = ec_pre_comp_new(group)) == NULL)
  502. return 0;
  503. generator = EC_GROUP_get0_generator(group);
  504. if (generator == NULL) {
  505. ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, EC_R_UNDEFINED_GENERATOR);
  506. goto err;
  507. }
  508. if (ctx == NULL) {
  509. ctx = new_ctx = BN_CTX_new();
  510. if (ctx == NULL)
  511. goto err;
  512. }
  513. BN_CTX_start(ctx);
  514. order = BN_CTX_get(ctx);
  515. if (order == NULL)
  516. goto err;
  517. if (!EC_GROUP_get_order(group, order, ctx))
  518. goto err;
  519. if (BN_is_zero(order)) {
  520. ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, EC_R_UNKNOWN_ORDER);
  521. goto err;
  522. }
  523. bits = BN_num_bits(order);
  524. /*
  525. * The following parameters mean we precompute (approximately) one point
  526. * per bit. TBD: The combination 8, 4 is perfect for 160 bits; for other
  527. * bit lengths, other parameter combinations might provide better
  528. * efficiency.
  529. */
  530. blocksize = 8;
  531. w = 4;
  532. if (EC_window_bits_for_scalar_size(bits) > w) {
  533. /* let's not make the window too small ... */
  534. w = EC_window_bits_for_scalar_size(bits);
  535. }
  536. numblocks = (bits + blocksize - 1) / blocksize; /* max. number of blocks
  537. * to use for wNAF
  538. * splitting */
  539. pre_points_per_block = (size_t)1 << (w - 1);
  540. num = pre_points_per_block * numblocks; /* number of points to compute
  541. * and store */
  542. points = OPENSSL_malloc(sizeof(*points) * (num + 1));
  543. if (points == NULL) {
  544. ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
  545. goto err;
  546. }
  547. var = points;
  548. var[num] = NULL; /* pivot */
  549. for (i = 0; i < num; i++) {
  550. if ((var[i] = EC_POINT_new(group)) == NULL) {
  551. ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
  552. goto err;
  553. }
  554. }
  555. if ((tmp_point = EC_POINT_new(group)) == NULL
  556. || (base = EC_POINT_new(group)) == NULL) {
  557. ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
  558. goto err;
  559. }
  560. if (!EC_POINT_copy(base, generator))
  561. goto err;
  562. /* do the precomputation */
  563. for (i = 0; i < numblocks; i++) {
  564. size_t j;
  565. if (!EC_POINT_dbl(group, tmp_point, base, ctx))
  566. goto err;
  567. if (!EC_POINT_copy(*var++, base))
  568. goto err;
  569. for (j = 1; j < pre_points_per_block; j++, var++) {
  570. /*
  571. * calculate odd multiples of the current base point
  572. */
  573. if (!EC_POINT_add(group, *var, tmp_point, *(var - 1), ctx))
  574. goto err;
  575. }
  576. if (i < numblocks - 1) {
  577. /*
  578. * get the next base (multiply current one by 2^blocksize)
  579. */
  580. size_t k;
  581. if (blocksize <= 2) {
  582. ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_INTERNAL_ERROR);
  583. goto err;
  584. }
  585. if (!EC_POINT_dbl(group, base, tmp_point, ctx))
  586. goto err;
  587. for (k = 2; k < blocksize; k++) {
  588. if (!EC_POINT_dbl(group, base, base, ctx))
  589. goto err;
  590. }
  591. }
  592. }
  593. if (!EC_POINTs_make_affine(group, num, points, ctx))
  594. goto err;
  595. pre_comp->group = group;
  596. pre_comp->blocksize = blocksize;
  597. pre_comp->numblocks = numblocks;
  598. pre_comp->w = w;
  599. pre_comp->points = points;
  600. points = NULL;
  601. pre_comp->num = num;
  602. SETPRECOMP(group, ec, pre_comp);
  603. pre_comp = NULL;
  604. ret = 1;
  605. err:
  606. if (ctx != NULL)
  607. BN_CTX_end(ctx);
  608. BN_CTX_free(new_ctx);
  609. EC_ec_pre_comp_free(pre_comp);
  610. if (points) {
  611. EC_POINT **p;
  612. for (p = points; *p != NULL; p++)
  613. EC_POINT_free(*p);
  614. OPENSSL_free(points);
  615. }
  616. EC_POINT_free(tmp_point);
  617. EC_POINT_free(base);
  618. return ret;
  619. }
  620. int ec_wNAF_have_precompute_mult(const EC_GROUP *group)
  621. {
  622. return HAVEPRECOMP(group, ec);
  623. }