Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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