You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

877 lines
24 KiB

  1. /* Originally written by Bodo Moeller for the OpenSSL project.
  2. * ====================================================================
  3. * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * 3. All advertising materials mentioning features or use of this
  18. * software must display the following acknowledgment:
  19. * "This product includes software developed by the OpenSSL Project
  20. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  21. *
  22. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  23. * endorse or promote products derived from this software without
  24. * prior written permission. For written permission, please contact
  25. * openssl-core@openssl.org.
  26. *
  27. * 5. Products derived from this software may not be called "OpenSSL"
  28. * nor may "OpenSSL" appear in their names without prior written
  29. * permission of the OpenSSL Project.
  30. *
  31. * 6. Redistributions of any form whatsoever must retain the following
  32. * acknowledgment:
  33. * "This product includes software developed by the OpenSSL Project
  34. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  37. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  39. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  45. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  46. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  47. * OF THE POSSIBILITY OF SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This product includes cryptographic software written by Eric Young
  51. * (eay@cryptsoft.com). This product includes software written by Tim
  52. * Hudson (tjh@cryptsoft.com).
  53. *
  54. */
  55. /* ====================================================================
  56. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  57. *
  58. * Portions of the attached software ("Contribution") are developed by
  59. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  60. *
  61. * The Contribution is licensed pursuant to the OpenSSL open source
  62. * license provided above.
  63. *
  64. * The elliptic curve binary polynomial software is originally written by
  65. * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
  66. * Laboratories. */
  67. #include <openssl/ec.h>
  68. #include <string.h>
  69. #include <openssl/bn.h>
  70. #include <openssl/err.h>
  71. #include <openssl/mem.h>
  72. #include <openssl/thread.h>
  73. #include "internal.h"
  74. /* This file implements the wNAF-based interleaving multi-exponentation method
  75. * (<URL:http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/moeller.html#multiexp>);
  76. * for multiplication with precomputation, we use wNAF splitting
  77. * (<URL:http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/moeller.html#fastexp>).
  78. * */
  79. /* structure for precomputed multiples of the generator */
  80. typedef struct ec_pre_comp_st {
  81. size_t blocksize; /* block size for wNAF splitting */
  82. size_t numblocks; /* max. number of blocks for which we have precomputation */
  83. size_t w; /* window size */
  84. EC_POINT **points; /* array with pre-calculated multiples of generator:
  85. * 'num' pointers to EC_POINT objects followed by a NULL */
  86. size_t num; /* numblocks * 2^(w-1) */
  87. int references;
  88. } EC_PRE_COMP;
  89. static EC_PRE_COMP *ec_pre_comp_new(void) {
  90. EC_PRE_COMP *ret = NULL;
  91. ret = (EC_PRE_COMP *)OPENSSL_malloc(sizeof(EC_PRE_COMP));
  92. if (!ret) {
  93. OPENSSL_PUT_ERROR(EC, ec_pre_comp_new, ERR_R_MALLOC_FAILURE);
  94. return ret;
  95. }
  96. ret->blocksize = 8; /* default */
  97. ret->numblocks = 0;
  98. ret->w = 4; /* default */
  99. ret->points = NULL;
  100. ret->num = 0;
  101. ret->references = 1;
  102. return ret;
  103. }
  104. void *ec_pre_comp_dup(EC_PRE_COMP *pre_comp) {
  105. if (pre_comp == NULL) {
  106. return NULL;
  107. }
  108. CRYPTO_add(&pre_comp->references, 1, CRYPTO_LOCK_EC_PRE_COMP);
  109. return pre_comp;
  110. }
  111. void ec_pre_comp_free(EC_PRE_COMP *pre_comp) {
  112. int i;
  113. if (!pre_comp) {
  114. return;
  115. }
  116. i = CRYPTO_add(&pre_comp->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
  117. if (i > 0) {
  118. return;
  119. }
  120. if (pre_comp->points) {
  121. EC_POINT **p;
  122. for (p = pre_comp->points; *p != NULL; p++) {
  123. EC_POINT_free(*p);
  124. }
  125. OPENSSL_free(pre_comp->points);
  126. }
  127. OPENSSL_free(pre_comp);
  128. }
  129. /* Determine the modified width-(w+1) Non-Adjacent Form (wNAF) of 'scalar'.
  130. * This is an array r[] of values that are either zero or odd with an
  131. * absolute value less than 2^w satisfying
  132. * scalar = \sum_j r[j]*2^j
  133. * where at most one of any w+1 consecutive digits is non-zero
  134. * with the exception that the most significant digit may be only
  135. * w-1 zeros away from that next non-zero digit.
  136. */
  137. static signed char *compute_wNAF(const BIGNUM *scalar, int w, size_t *ret_len) {
  138. int window_val;
  139. int ok = 0;
  140. signed char *r = NULL;
  141. int sign = 1;
  142. int bit, next_bit, mask;
  143. size_t len = 0, j;
  144. if (BN_is_zero(scalar)) {
  145. r = OPENSSL_malloc(1);
  146. if (!r) {
  147. OPENSSL_PUT_ERROR(EC, compute_wNAF, ERR_R_MALLOC_FAILURE);
  148. goto err;
  149. }
  150. r[0] = 0;
  151. *ret_len = 1;
  152. return r;
  153. }
  154. if (w <= 0 || w > 7) /* 'signed char' can represent integers with absolute
  155. values less than 2^7 */
  156. {
  157. OPENSSL_PUT_ERROR(EC, compute_wNAF, ERR_R_INTERNAL_ERROR);
  158. goto err;
  159. }
  160. bit = 1 << w; /* at most 128 */
  161. next_bit = bit << 1; /* at most 256 */
  162. mask = next_bit - 1; /* at most 255 */
  163. if (BN_is_negative(scalar)) {
  164. sign = -1;
  165. }
  166. if (scalar->d == NULL || scalar->top == 0) {
  167. OPENSSL_PUT_ERROR(EC, compute_wNAF, ERR_R_INTERNAL_ERROR);
  168. goto err;
  169. }
  170. len = BN_num_bits(scalar);
  171. r = OPENSSL_malloc(
  172. len +
  173. 1); /* modified wNAF may be one digit longer than binary representation
  174. * (*ret_len will be set to the actual length, i.e. at most
  175. * BN_num_bits(scalar) + 1) */
  176. if (r == NULL) {
  177. OPENSSL_PUT_ERROR(EC, compute_wNAF, ERR_R_MALLOC_FAILURE);
  178. goto err;
  179. }
  180. window_val = scalar->d[0] & mask;
  181. j = 0;
  182. while ((window_val != 0) ||
  183. (j + w + 1 < len)) /* if j+w+1 >= len, window_val will not increase */
  184. {
  185. int digit = 0;
  186. /* 0 <= window_val <= 2^(w+1) */
  187. if (window_val & 1) {
  188. /* 0 < window_val < 2^(w+1) */
  189. if (window_val & bit) {
  190. digit = window_val - next_bit; /* -2^w < digit < 0 */
  191. #if 1 /* modified wNAF */
  192. if (j + w + 1 >= len) {
  193. /* special case for generating modified wNAFs:
  194. * no new bits will be added into window_val,
  195. * so using a positive digit here will decrease
  196. * the total length of the representation */
  197. digit = window_val & (mask >> 1); /* 0 < digit < 2^w */
  198. }
  199. #endif
  200. } else {
  201. digit = window_val; /* 0 < digit < 2^w */
  202. }
  203. if (digit <= -bit || digit >= bit || !(digit & 1)) {
  204. OPENSSL_PUT_ERROR(EC, compute_wNAF, ERR_R_INTERNAL_ERROR);
  205. goto err;
  206. }
  207. window_val -= digit;
  208. /* now window_val is 0 or 2^(w+1) in standard wNAF generation;
  209. * for modified window NAFs, it may also be 2^w
  210. */
  211. if (window_val != 0 && window_val != next_bit && window_val != bit) {
  212. OPENSSL_PUT_ERROR(EC, compute_wNAF, ERR_R_INTERNAL_ERROR);
  213. goto err;
  214. }
  215. }
  216. r[j++] = sign * digit;
  217. window_val >>= 1;
  218. window_val += bit * BN_is_bit_set(scalar, j + w);
  219. if (window_val > next_bit) {
  220. OPENSSL_PUT_ERROR(EC, compute_wNAF, ERR_R_INTERNAL_ERROR);
  221. goto err;
  222. }
  223. }
  224. if (j > len + 1) {
  225. OPENSSL_PUT_ERROR(EC, compute_wNAF, ERR_R_INTERNAL_ERROR);
  226. goto err;
  227. }
  228. len = j;
  229. ok = 1;
  230. err:
  231. if (!ok) {
  232. OPENSSL_free(r);
  233. r = NULL;
  234. }
  235. if (ok) {
  236. *ret_len = len;
  237. }
  238. return r;
  239. }
  240. /* TODO: table should be optimised for the wNAF-based implementation,
  241. * sometimes smaller windows will give better performance
  242. * (thus the boundaries should be increased)
  243. */
  244. #define EC_window_bits_for_scalar_size(b) \
  245. ((size_t)((b) >= 2000 ? 6 : (b) >= 800 ? 5 : (b) >= 300 \
  246. ? 4 \
  247. : (b) >= 70 ? 3 : (b) >= 20 \
  248. ? 2 \
  249. : 1))
  250. /* Compute
  251. * \sum scalars[i]*points[i],
  252. * also including
  253. * scalar*generator
  254. * in the addition if scalar != NULL
  255. */
  256. int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
  257. size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
  258. BN_CTX *ctx) {
  259. BN_CTX *new_ctx = NULL;
  260. const EC_POINT *generator = NULL;
  261. EC_POINT *tmp = NULL;
  262. size_t totalnum;
  263. size_t blocksize = 0, numblocks = 0; /* for wNAF splitting */
  264. size_t pre_points_per_block = 0;
  265. size_t i, j;
  266. int k;
  267. int r_is_inverted = 0;
  268. int r_is_at_infinity = 1;
  269. size_t *wsize = NULL; /* individual window sizes */
  270. signed char **wNAF = NULL; /* individual wNAFs */
  271. size_t *wNAF_len = NULL;
  272. size_t max_len = 0;
  273. size_t num_val;
  274. EC_POINT **val = NULL; /* precomputation */
  275. EC_POINT **v;
  276. EC_POINT ***val_sub =
  277. NULL; /* pointers to sub-arrays of 'val' or 'pre_comp->points' */
  278. const EC_PRE_COMP *pre_comp = NULL;
  279. int num_scalar = 0; /* flag: will be set to 1 if 'scalar' must be treated like
  280. * other scalars,
  281. * i.e. precomputation is not available */
  282. int ret = 0;
  283. if (group->meth != r->meth) {
  284. OPENSSL_PUT_ERROR(EC, ec_wNAF_mul, EC_R_INCOMPATIBLE_OBJECTS);
  285. return 0;
  286. }
  287. if ((scalar == NULL) && (num == 0)) {
  288. return EC_POINT_set_to_infinity(group, r);
  289. }
  290. for (i = 0; i < num; i++) {
  291. if (group->meth != points[i]->meth) {
  292. OPENSSL_PUT_ERROR(EC, ec_wNAF_mul, EC_R_INCOMPATIBLE_OBJECTS);
  293. return 0;
  294. }
  295. }
  296. if (ctx == NULL) {
  297. ctx = new_ctx = BN_CTX_new();
  298. if (ctx == NULL) {
  299. goto err;
  300. }
  301. }
  302. if (scalar != NULL) {
  303. generator = EC_GROUP_get0_generator(group);
  304. if (generator == NULL) {
  305. OPENSSL_PUT_ERROR(EC, ec_wNAF_mul, EC_R_UNDEFINED_GENERATOR);
  306. goto err;
  307. }
  308. /* look if we can use precomputed multiples of generator */
  309. pre_comp = group->pre_comp;
  310. if (pre_comp && pre_comp->numblocks &&
  311. (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) == 0)) {
  312. blocksize = pre_comp->blocksize;
  313. /* determine maximum number of blocks that wNAF splitting may yield
  314. * (NB: maximum wNAF length is bit length plus one) */
  315. numblocks = (BN_num_bits(scalar) / blocksize) + 1;
  316. /* we cannot use more blocks than we have precomputation for */
  317. if (numblocks > pre_comp->numblocks) {
  318. numblocks = pre_comp->numblocks;
  319. }
  320. pre_points_per_block = (size_t)1 << (pre_comp->w - 1);
  321. /* check that pre_comp looks sane */
  322. if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block)) {
  323. OPENSSL_PUT_ERROR(EC, ec_wNAF_mul, ERR_R_INTERNAL_ERROR);
  324. goto err;
  325. }
  326. } else {
  327. /* can't use precomputation */
  328. pre_comp = NULL;
  329. numblocks = 1;
  330. num_scalar = 1; /* treat 'scalar' like 'num'-th element of 'scalars' */
  331. }
  332. }
  333. totalnum = num + numblocks;
  334. wsize = OPENSSL_malloc(totalnum * sizeof wsize[0]);
  335. wNAF_len = OPENSSL_malloc(totalnum * sizeof wNAF_len[0]);
  336. wNAF = OPENSSL_malloc((totalnum + 1) *
  337. sizeof wNAF[0]); /* includes space for pivot */
  338. val_sub = OPENSSL_malloc(totalnum * sizeof val_sub[0]);
  339. /* Ensure wNAF is initialised in case we end up going to err. */
  340. if (wNAF) {
  341. wNAF[0] = NULL; /* preliminary pivot */
  342. }
  343. if (!wsize || !wNAF_len || !wNAF || !val_sub) {
  344. OPENSSL_PUT_ERROR(EC, ec_wNAF_mul, ERR_R_MALLOC_FAILURE);
  345. goto err;
  346. }
  347. /* num_val will be the total number of temporarily precomputed points */
  348. num_val = 0;
  349. for (i = 0; i < num + num_scalar; i++) {
  350. size_t bits;
  351. bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar);
  352. wsize[i] = EC_window_bits_for_scalar_size(bits);
  353. num_val += (size_t)1 << (wsize[i] - 1);
  354. wNAF[i + 1] = NULL; /* make sure we always have a pivot */
  355. wNAF[i] =
  356. compute_wNAF((i < num ? scalars[i] : scalar), wsize[i], &wNAF_len[i]);
  357. if (wNAF[i] == NULL) {
  358. goto err;
  359. }
  360. if (wNAF_len[i] > max_len) {
  361. max_len = wNAF_len[i];
  362. }
  363. }
  364. if (numblocks) {
  365. /* we go here iff scalar != NULL */
  366. if (pre_comp == NULL) {
  367. if (num_scalar != 1) {
  368. OPENSSL_PUT_ERROR(EC, ec_wNAF_mul, ERR_R_INTERNAL_ERROR);
  369. goto err;
  370. }
  371. /* we have already generated a wNAF for 'scalar' */
  372. } else {
  373. signed char *tmp_wNAF = NULL;
  374. size_t tmp_len = 0;
  375. if (num_scalar != 0) {
  376. OPENSSL_PUT_ERROR(EC, ec_wNAF_mul, ERR_R_INTERNAL_ERROR);
  377. goto err;
  378. }
  379. /* use the window size for which we have precomputation */
  380. wsize[num] = pre_comp->w;
  381. tmp_wNAF = compute_wNAF(scalar, wsize[num], &tmp_len);
  382. if (!tmp_wNAF) {
  383. goto err;
  384. }
  385. if (tmp_len <= max_len) {
  386. /* One of the other wNAFs is at least as long
  387. * as the wNAF belonging to the generator,
  388. * so wNAF splitting will not buy us anything. */
  389. numblocks = 1; /* don't use wNAF splitting */
  390. totalnum = num + numblocks;
  391. wNAF[num] = tmp_wNAF;
  392. wNAF[num + 1] = NULL;
  393. wNAF_len[num] = tmp_len;
  394. /* pre_comp->points starts with the points that we need here: */
  395. val_sub[num] = pre_comp->points;
  396. } else {
  397. /* don't include tmp_wNAF directly into wNAF array
  398. * - use wNAF splitting and include the blocks */
  399. signed char *pp;
  400. EC_POINT **tmp_points;
  401. if (tmp_len < numblocks * blocksize) {
  402. /* possibly we can do with fewer blocks than estimated */
  403. numblocks = (tmp_len + blocksize - 1) / blocksize;
  404. if (numblocks > pre_comp->numblocks) {
  405. OPENSSL_PUT_ERROR(EC, ec_wNAF_mul, ERR_R_INTERNAL_ERROR);
  406. goto err;
  407. }
  408. totalnum = num + numblocks;
  409. }
  410. /* split wNAF in 'numblocks' parts */
  411. pp = tmp_wNAF;
  412. tmp_points = pre_comp->points;
  413. for (i = num; i < totalnum; i++) {
  414. if (i < totalnum - 1) {
  415. wNAF_len[i] = blocksize;
  416. if (tmp_len < blocksize) {
  417. OPENSSL_PUT_ERROR(EC, ec_wNAF_mul, ERR_R_INTERNAL_ERROR);
  418. goto err;
  419. }
  420. tmp_len -= blocksize;
  421. } else {
  422. /* last block gets whatever is left
  423. * (this could be more or less than 'blocksize'!) */
  424. wNAF_len[i] = tmp_len;
  425. }
  426. wNAF[i + 1] = NULL;
  427. wNAF[i] = OPENSSL_malloc(wNAF_len[i]);
  428. if (wNAF[i] == NULL) {
  429. OPENSSL_PUT_ERROR(EC, ec_wNAF_mul, ERR_R_MALLOC_FAILURE);
  430. OPENSSL_free(tmp_wNAF);
  431. goto err;
  432. }
  433. memcpy(wNAF[i], pp, wNAF_len[i]);
  434. if (wNAF_len[i] > max_len) {
  435. max_len = wNAF_len[i];
  436. }
  437. if (*tmp_points == NULL) {
  438. OPENSSL_PUT_ERROR(EC, ec_wNAF_mul, ERR_R_INTERNAL_ERROR);
  439. OPENSSL_free(tmp_wNAF);
  440. goto err;
  441. }
  442. val_sub[i] = tmp_points;
  443. tmp_points += pre_points_per_block;
  444. pp += blocksize;
  445. }
  446. OPENSSL_free(tmp_wNAF);
  447. }
  448. }
  449. }
  450. /* All points we precompute now go into a single array 'val'.
  451. * 'val_sub[i]' is a pointer to the subarray for the i-th point,
  452. * or to a subarray of 'pre_comp->points' if we already have precomputation.
  453. */
  454. val = OPENSSL_malloc((num_val + 1) * sizeof val[0]);
  455. if (val == NULL) {
  456. OPENSSL_PUT_ERROR(EC, ec_wNAF_mul, ERR_R_MALLOC_FAILURE);
  457. goto err;
  458. }
  459. val[num_val] = NULL; /* pivot element */
  460. /* allocate points for precomputation */
  461. v = val;
  462. for (i = 0; i < num + num_scalar; i++) {
  463. val_sub[i] = v;
  464. for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) {
  465. *v = EC_POINT_new(group);
  466. if (*v == NULL) {
  467. goto err;
  468. }
  469. v++;
  470. }
  471. }
  472. if (!(v == val + num_val)) {
  473. OPENSSL_PUT_ERROR(EC, ec_wNAF_mul, ERR_R_INTERNAL_ERROR);
  474. goto err;
  475. }
  476. if (!(tmp = EC_POINT_new(group))) {
  477. goto err;
  478. }
  479. /* prepare precomputed values:
  480. * val_sub[i][0] := points[i]
  481. * val_sub[i][1] := 3 * points[i]
  482. * val_sub[i][2] := 5 * points[i]
  483. * ...
  484. */
  485. for (i = 0; i < num + num_scalar; i++) {
  486. if (i < num) {
  487. if (!EC_POINT_copy(val_sub[i][0], points[i])) {
  488. goto err;
  489. }
  490. } else if (!EC_POINT_copy(val_sub[i][0], generator)) {
  491. goto err;
  492. }
  493. if (wsize[i] > 1) {
  494. if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx)) {
  495. goto err;
  496. }
  497. for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) {
  498. if (!EC_POINT_add(group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx)) {
  499. goto err;
  500. }
  501. }
  502. }
  503. }
  504. #if 1 /* optional; EC_window_bits_for_scalar_size assumes we do this step */
  505. if (!EC_POINTs_make_affine(group, num_val, val, ctx)) {
  506. goto err;
  507. }
  508. #endif
  509. r_is_at_infinity = 1;
  510. for (k = max_len - 1; k >= 0; k--) {
  511. if (!r_is_at_infinity && !EC_POINT_dbl(group, r, r, ctx)) {
  512. goto err;
  513. }
  514. for (i = 0; i < totalnum; i++) {
  515. if (wNAF_len[i] > (size_t)k) {
  516. int digit = wNAF[i][k];
  517. int is_neg;
  518. if (digit) {
  519. is_neg = digit < 0;
  520. if (is_neg) {
  521. digit = -digit;
  522. }
  523. if (is_neg != r_is_inverted) {
  524. if (!r_is_at_infinity && !EC_POINT_invert(group, r, ctx)) {
  525. goto err;
  526. }
  527. r_is_inverted = !r_is_inverted;
  528. }
  529. /* digit > 0 */
  530. if (r_is_at_infinity) {
  531. if (!EC_POINT_copy(r, val_sub[i][digit >> 1])) {
  532. goto err;
  533. }
  534. r_is_at_infinity = 0;
  535. } else {
  536. if (!EC_POINT_add(group, r, r, val_sub[i][digit >> 1], ctx)) {
  537. goto err;
  538. }
  539. }
  540. }
  541. }
  542. }
  543. }
  544. if (r_is_at_infinity) {
  545. if (!EC_POINT_set_to_infinity(group, r)) {
  546. goto err;
  547. }
  548. } else if (r_is_inverted && !EC_POINT_invert(group, r, ctx)) {
  549. goto err;
  550. }
  551. ret = 1;
  552. err:
  553. if (new_ctx != NULL) {
  554. BN_CTX_free(new_ctx);
  555. }
  556. if (tmp != NULL) {
  557. EC_POINT_free(tmp);
  558. }
  559. if (wsize != NULL) {
  560. OPENSSL_free(wsize);
  561. }
  562. if (wNAF_len != NULL) {
  563. OPENSSL_free(wNAF_len);
  564. }
  565. if (wNAF != NULL) {
  566. signed char **w;
  567. for (w = wNAF; *w != NULL; w++) {
  568. OPENSSL_free(*w);
  569. }
  570. OPENSSL_free(wNAF);
  571. }
  572. if (val != NULL) {
  573. for (v = val; *v != NULL; v++) {
  574. EC_POINT_clear_free(*v);
  575. }
  576. OPENSSL_free(val);
  577. }
  578. if (val_sub != NULL) {
  579. OPENSSL_free(val_sub);
  580. }
  581. return ret;
  582. }
  583. /* ec_wNAF_precompute_mult()
  584. * creates an EC_PRE_COMP object with preprecomputed multiples of the generator
  585. * for use with wNAF splitting as implemented in ec_wNAF_mul().
  586. *
  587. * 'pre_comp->points' is an array of multiples of the generator
  588. * of the following form:
  589. * points[0] = generator;
  590. * points[1] = 3 * generator;
  591. * ...
  592. * points[2^(w-1)-1] = (2^(w-1)-1) * generator;
  593. * points[2^(w-1)] = 2^blocksize * generator;
  594. * points[2^(w-1)+1] = 3 * 2^blocksize * generator;
  595. * ...
  596. * points[2^(w-1)*(numblocks-1)-1] = (2^(w-1)) * 2^(blocksize*(numblocks-2)) *
  597. *generator
  598. * points[2^(w-1)*(numblocks-1)] = 2^(blocksize*(numblocks-1)) *
  599. *generator
  600. * ...
  601. * points[2^(w-1)*numblocks-1] = (2^(w-1)) * 2^(blocksize*(numblocks-1)) *
  602. *generator
  603. * points[2^(w-1)*numblocks] = NULL
  604. */
  605. int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *ctx) {
  606. const EC_POINT *generator;
  607. EC_POINT *tmp_point = NULL, *base = NULL, **var;
  608. BN_CTX *new_ctx = NULL;
  609. BIGNUM *order;
  610. size_t i, bits, w, pre_points_per_block, blocksize, numblocks, num;
  611. EC_POINT **points = NULL;
  612. EC_PRE_COMP *pre_comp;
  613. int ret = 0;
  614. /* if there is an old EC_PRE_COMP object, throw it away */
  615. if (group->pre_comp) {
  616. ec_pre_comp_free(group->pre_comp);
  617. group->pre_comp = NULL;
  618. }
  619. generator = EC_GROUP_get0_generator(group);
  620. if (generator == NULL) {
  621. OPENSSL_PUT_ERROR(EC, ec_wNAF_precompute_mult, EC_R_UNDEFINED_GENERATOR);
  622. return 0;
  623. }
  624. pre_comp = ec_pre_comp_new();
  625. if (pre_comp == NULL) {
  626. return 0;
  627. }
  628. if (ctx == NULL) {
  629. ctx = new_ctx = BN_CTX_new();
  630. if (ctx == NULL) {
  631. goto err;
  632. }
  633. }
  634. BN_CTX_start(ctx);
  635. order = BN_CTX_get(ctx);
  636. if (order == NULL) {
  637. goto err;
  638. }
  639. if (!EC_GROUP_get_order(group, order, ctx)) {
  640. goto err;
  641. }
  642. if (BN_is_zero(order)) {
  643. OPENSSL_PUT_ERROR(EC, ec_wNAF_precompute_mult, EC_R_UNKNOWN_ORDER);
  644. goto err;
  645. }
  646. bits = BN_num_bits(order);
  647. /* The following parameters mean we precompute (approximately)
  648. * one point per bit.
  649. *
  650. * TBD: The combination 8, 4 is perfect for 160 bits; for other
  651. * bit lengths, other parameter combinations might provide better
  652. * efficiency.
  653. */
  654. blocksize = 8;
  655. w = 4;
  656. if (EC_window_bits_for_scalar_size(bits) > w) {
  657. /* let's not make the window too small ... */
  658. w = EC_window_bits_for_scalar_size(bits);
  659. }
  660. numblocks = (bits + blocksize - 1) /
  661. blocksize; /* max. number of blocks to use for wNAF splitting */
  662. pre_points_per_block = (size_t)1 << (w - 1);
  663. num = pre_points_per_block *
  664. numblocks; /* number of points to compute and store */
  665. points = OPENSSL_malloc(sizeof(EC_POINT *) * (num + 1));
  666. if (!points) {
  667. OPENSSL_PUT_ERROR(EC, ec_wNAF_precompute_mult, ERR_R_MALLOC_FAILURE);
  668. goto err;
  669. }
  670. var = points;
  671. var[num] = NULL; /* pivot */
  672. for (i = 0; i < num; i++) {
  673. if ((var[i] = EC_POINT_new(group)) == NULL) {
  674. OPENSSL_PUT_ERROR(EC, ec_wNAF_precompute_mult, ERR_R_MALLOC_FAILURE);
  675. goto err;
  676. }
  677. }
  678. if (!(tmp_point = EC_POINT_new(group)) || !(base = EC_POINT_new(group))) {
  679. OPENSSL_PUT_ERROR(EC, ec_wNAF_precompute_mult, ERR_R_MALLOC_FAILURE);
  680. goto err;
  681. }
  682. if (!EC_POINT_copy(base, generator)) {
  683. goto err;
  684. }
  685. /* do the precomputation */
  686. for (i = 0; i < numblocks; i++) {
  687. size_t j;
  688. if (!EC_POINT_dbl(group, tmp_point, base, ctx)) {
  689. goto err;
  690. }
  691. if (!EC_POINT_copy(*var++, base)) {
  692. goto err;
  693. }
  694. for (j = 1; j < pre_points_per_block; j++, var++) {
  695. /* calculate odd multiples of the current base point */
  696. if (!EC_POINT_add(group, *var, tmp_point, *(var - 1), ctx)) {
  697. goto err;
  698. }
  699. }
  700. if (i < numblocks - 1) {
  701. /* get the next base (multiply current one by 2^blocksize) */
  702. size_t k;
  703. if (blocksize <= 2) {
  704. OPENSSL_PUT_ERROR(EC, ec_wNAF_precompute_mult, ERR_R_INTERNAL_ERROR);
  705. goto err;
  706. }
  707. if (!EC_POINT_dbl(group, base, tmp_point, ctx)) {
  708. goto err;
  709. }
  710. for (k = 2; k < blocksize; k++) {
  711. if (!EC_POINT_dbl(group, base, base, ctx)) {
  712. goto err;
  713. }
  714. }
  715. }
  716. }
  717. if (!EC_POINTs_make_affine(group, num, points, ctx)) {
  718. goto err;
  719. }
  720. pre_comp->blocksize = blocksize;
  721. pre_comp->numblocks = numblocks;
  722. pre_comp->w = w;
  723. pre_comp->points = points;
  724. points = NULL;
  725. pre_comp->num = num;
  726. group->pre_comp = pre_comp;
  727. pre_comp = NULL;
  728. ret = 1;
  729. err:
  730. if (ctx != NULL) {
  731. BN_CTX_end(ctx);
  732. }
  733. if (new_ctx != NULL) {
  734. BN_CTX_free(new_ctx);
  735. }
  736. if (pre_comp) {
  737. ec_pre_comp_free(pre_comp);
  738. }
  739. if (points) {
  740. EC_POINT **p;
  741. for (p = points; *p != NULL; p++) {
  742. EC_POINT_free(*p);
  743. }
  744. OPENSSL_free(points);
  745. }
  746. if (tmp_point) {
  747. EC_POINT_free(tmp_point);
  748. }
  749. if (base) {
  750. EC_POINT_free(base);
  751. }
  752. return ret;
  753. }
  754. int ec_wNAF_have_precompute_mult(const EC_GROUP *group) {
  755. return group->pre_comp != NULL;
  756. }