Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

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