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.

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