ec_mult.c 21 KB

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