2
0

ec_mult.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. /*
  2. * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. *
  5. * Licensed under the OpenSSL license (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include <string.h>
  11. #include <openssl/err.h>
  12. #include "internal/cryptlib.h"
  13. #include "internal/bn_int.h"
  14. #include "ec_lcl.h"
  15. #include "internal/refcount.h"
  16. /*
  17. * This file implements the wNAF-based interleaving multi-exponentiation method
  18. * Formerly at:
  19. * http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/moeller.html#multiexp
  20. * You might now find it here:
  21. * http://link.springer.com/chapter/10.1007%2F3-540-45537-X_13
  22. * http://www.bmoeller.de/pdf/TI-01-08.multiexp.pdf
  23. * For multiplication with precomputation, we use wNAF splitting, formerly at:
  24. * http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/moeller.html#fastexp
  25. */
  26. /* structure for precomputed multiples of the generator */
  27. struct ec_pre_comp_st {
  28. const EC_GROUP *group; /* parent EC_GROUP object */
  29. size_t blocksize; /* block size for wNAF splitting */
  30. size_t numblocks; /* max. number of blocks for which we have
  31. * precomputation */
  32. size_t w; /* window size */
  33. EC_POINT **points; /* array with pre-calculated multiples of
  34. * generator: 'num' pointers to EC_POINT
  35. * objects followed by a NULL */
  36. size_t num; /* numblocks * 2^(w-1) */
  37. CRYPTO_REF_COUNT references;
  38. CRYPTO_RWLOCK *lock;
  39. };
  40. static EC_PRE_COMP *ec_pre_comp_new(const EC_GROUP *group)
  41. {
  42. EC_PRE_COMP *ret = NULL;
  43. if (!group)
  44. return NULL;
  45. ret = OPENSSL_zalloc(sizeof(*ret));
  46. if (ret == NULL) {
  47. ECerr(EC_F_EC_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
  48. return ret;
  49. }
  50. ret->group = group;
  51. ret->blocksize = 8; /* default */
  52. ret->w = 4; /* default */
  53. ret->references = 1;
  54. ret->lock = CRYPTO_THREAD_lock_new();
  55. if (ret->lock == NULL) {
  56. ECerr(EC_F_EC_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
  57. OPENSSL_free(ret);
  58. return NULL;
  59. }
  60. return ret;
  61. }
  62. EC_PRE_COMP *EC_ec_pre_comp_dup(EC_PRE_COMP *pre)
  63. {
  64. int i;
  65. if (pre != NULL)
  66. CRYPTO_UP_REF(&pre->references, &i, pre->lock);
  67. return pre;
  68. }
  69. void EC_ec_pre_comp_free(EC_PRE_COMP *pre)
  70. {
  71. int i;
  72. if (pre == NULL)
  73. return;
  74. CRYPTO_DOWN_REF(&pre->references, &i, pre->lock);
  75. REF_PRINT_COUNT("EC_ec", pre);
  76. if (i > 0)
  77. return;
  78. REF_ASSERT_ISNT(i < 0);
  79. if (pre->points != NULL) {
  80. EC_POINT **pts;
  81. for (pts = pre->points; *pts != NULL; pts++)
  82. EC_POINT_free(*pts);
  83. OPENSSL_free(pre->points);
  84. }
  85. CRYPTO_THREAD_lock_free(pre->lock);
  86. OPENSSL_free(pre);
  87. }
  88. #define EC_POINT_BN_set_flags(P, flags) do { \
  89. BN_set_flags((P)->X, (flags)); \
  90. BN_set_flags((P)->Y, (flags)); \
  91. BN_set_flags((P)->Z, (flags)); \
  92. } while(0)
  93. /*-
  94. * This functions computes (in constant time) a point multiplication over the
  95. * EC group.
  96. *
  97. * At a high level, it is Montgomery ladder with conditional swaps.
  98. *
  99. * It performs either a fixed point multiplication
  100. * (scalar * generator)
  101. * when point is NULL, or a variable point multiplication
  102. * (scalar * point)
  103. * when point is not NULL.
  104. *
  105. * scalar should be in the range [0,n) otherwise all constant time bets are off.
  106. *
  107. * NB: This says nothing about EC_POINT_add and EC_POINT_dbl,
  108. * which of course are not constant time themselves.
  109. *
  110. * The product is stored in r.
  111. *
  112. * Returns 1 on success, 0 otherwise.
  113. */
  114. static int ec_mul_consttime(const EC_GROUP *group, EC_POINT *r,
  115. const BIGNUM *scalar, const EC_POINT *point,
  116. BN_CTX *ctx)
  117. {
  118. int i, order_bits, group_top, kbit, pbit, Z_is_one;
  119. EC_POINT *s = NULL;
  120. BIGNUM *k = NULL;
  121. BIGNUM *lambda = NULL;
  122. BN_CTX *new_ctx = NULL;
  123. int ret = 0;
  124. if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL)
  125. return 0;
  126. BN_CTX_start(ctx);
  127. order_bits = BN_num_bits(group->order);
  128. s = EC_POINT_new(group);
  129. if (s == NULL)
  130. goto err;
  131. if (point == NULL) {
  132. if (!EC_POINT_copy(s, group->generator))
  133. goto err;
  134. } else {
  135. if (!EC_POINT_copy(s, point))
  136. goto err;
  137. }
  138. EC_POINT_BN_set_flags(s, BN_FLG_CONSTTIME);
  139. lambda = BN_CTX_get(ctx);
  140. k = BN_CTX_get(ctx);
  141. if (k == NULL)
  142. goto err;
  143. /*
  144. * Group orders are often on a word boundary.
  145. * So when we pad the scalar, some timing diff might
  146. * pop if it needs to be expanded due to carries.
  147. * So expand ahead of time.
  148. */
  149. group_top = bn_get_top(group->order);
  150. if ((bn_wexpand(k, group_top + 1) == NULL)
  151. || (bn_wexpand(lambda, group_top + 1) == NULL))
  152. goto err;
  153. if (!BN_copy(k, scalar))
  154. goto err;
  155. BN_set_flags(k, BN_FLG_CONSTTIME);
  156. if ((BN_num_bits(k) > order_bits) || (BN_is_negative(k))) {
  157. /*-
  158. * this is an unusual input, and we don't guarantee
  159. * constant-timeness
  160. */
  161. if (!BN_nnmod(k, k, group->order, ctx))
  162. goto err;
  163. }
  164. if (!BN_add(lambda, k, group->order))
  165. goto err;
  166. BN_set_flags(lambda, BN_FLG_CONSTTIME);
  167. if (!BN_add(k, lambda, group->order))
  168. goto err;
  169. /*
  170. * lambda := scalar + order
  171. * k := scalar + 2*order
  172. */
  173. kbit = BN_is_bit_set(lambda, order_bits);
  174. BN_consttime_swap(kbit, k, lambda, group_top + 1);
  175. group_top = bn_get_top(group->field);
  176. if ((bn_wexpand(s->X, group_top) == NULL)
  177. || (bn_wexpand(s->Y, group_top) == NULL)
  178. || (bn_wexpand(s->Z, group_top) == NULL)
  179. || (bn_wexpand(r->X, group_top) == NULL)
  180. || (bn_wexpand(r->Y, group_top) == NULL)
  181. || (bn_wexpand(r->Z, group_top) == NULL))
  182. goto err;
  183. /*-
  184. * Apply coordinate blinding for EC_POINT.
  185. *
  186. * The underlying EC_METHOD can optionally implement this function:
  187. * ec_point_blind_coordinates() returns 0 in case of errors or 1 on
  188. * success or if coordinate blinding is not implemented for this
  189. * group.
  190. */
  191. if (!ec_point_blind_coordinates(group, s, ctx))
  192. goto err;
  193. /* top bit is a 1, in a fixed pos */
  194. if (!EC_POINT_copy(r, s))
  195. goto err;
  196. EC_POINT_BN_set_flags(r, BN_FLG_CONSTTIME);
  197. if (!EC_POINT_dbl(group, s, s, ctx))
  198. goto err;
  199. pbit = 0;
  200. #define EC_POINT_CSWAP(c, a, b, w, t) do { \
  201. BN_consttime_swap(c, (a)->X, (b)->X, w); \
  202. BN_consttime_swap(c, (a)->Y, (b)->Y, w); \
  203. BN_consttime_swap(c, (a)->Z, (b)->Z, w); \
  204. t = ((a)->Z_is_one ^ (b)->Z_is_one) & (c); \
  205. (a)->Z_is_one ^= (t); \
  206. (b)->Z_is_one ^= (t); \
  207. } while(0)
  208. /*-
  209. * The ladder step, with branches, is
  210. *
  211. * k[i] == 0: S = add(R, S), R = dbl(R)
  212. * k[i] == 1: R = add(S, R), S = dbl(S)
  213. *
  214. * Swapping R, S conditionally on k[i] leaves you with state
  215. *
  216. * k[i] == 0: T, U = R, S
  217. * k[i] == 1: T, U = S, R
  218. *
  219. * Then perform the ECC ops.
  220. *
  221. * U = add(T, U)
  222. * T = dbl(T)
  223. *
  224. * Which leaves you with state
  225. *
  226. * k[i] == 0: U = add(R, S), T = dbl(R)
  227. * k[i] == 1: U = add(S, R), T = dbl(S)
  228. *
  229. * Swapping T, U conditionally on k[i] leaves you with state
  230. *
  231. * k[i] == 0: R, S = T, U
  232. * k[i] == 1: R, S = U, T
  233. *
  234. * Which leaves you with state
  235. *
  236. * k[i] == 0: S = add(R, S), R = dbl(R)
  237. * k[i] == 1: R = add(S, R), S = dbl(S)
  238. *
  239. * So we get the same logic, but instead of a branch it's a
  240. * conditional swap, followed by ECC ops, then another conditional swap.
  241. *
  242. * Optimization: The end of iteration i and start of i-1 looks like
  243. *
  244. * ...
  245. * CSWAP(k[i], R, S)
  246. * ECC
  247. * CSWAP(k[i], R, S)
  248. * (next iteration)
  249. * CSWAP(k[i-1], R, S)
  250. * ECC
  251. * CSWAP(k[i-1], R, S)
  252. * ...
  253. *
  254. * So instead of two contiguous swaps, you can merge the condition
  255. * bits and do a single swap.
  256. *
  257. * k[i] k[i-1] Outcome
  258. * 0 0 No Swap
  259. * 0 1 Swap
  260. * 1 0 Swap
  261. * 1 1 No Swap
  262. *
  263. * This is XOR. pbit tracks the previous bit of k.
  264. */
  265. for (i = order_bits - 1; i >= 0; i--) {
  266. kbit = BN_is_bit_set(k, i) ^ pbit;
  267. EC_POINT_CSWAP(kbit, r, s, group_top, Z_is_one);
  268. if (!EC_POINT_add(group, s, r, s, ctx))
  269. goto err;
  270. if (!EC_POINT_dbl(group, r, r, ctx))
  271. goto err;
  272. /*
  273. * pbit logic merges this cswap with that of the
  274. * next iteration
  275. */
  276. pbit ^= kbit;
  277. }
  278. /* one final cswap to move the right value into r */
  279. EC_POINT_CSWAP(pbit, r, s, group_top, Z_is_one);
  280. #undef EC_POINT_CSWAP
  281. ret = 1;
  282. err:
  283. EC_POINT_free(s);
  284. BN_CTX_end(ctx);
  285. BN_CTX_free(new_ctx);
  286. return ret;
  287. }
  288. #undef EC_POINT_BN_set_flags
  289. /*
  290. * TODO: table should be optimised for the wNAF-based implementation,
  291. * sometimes smaller windows will give better performance (thus the
  292. * boundaries should be increased)
  293. */
  294. #define EC_window_bits_for_scalar_size(b) \
  295. ((size_t) \
  296. ((b) >= 2000 ? 6 : \
  297. (b) >= 800 ? 5 : \
  298. (b) >= 300 ? 4 : \
  299. (b) >= 70 ? 3 : \
  300. (b) >= 20 ? 2 : \
  301. 1))
  302. /*-
  303. * Compute
  304. * \sum scalars[i]*points[i],
  305. * also including
  306. * scalar*generator
  307. * in the addition if scalar != NULL
  308. */
  309. int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
  310. size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
  311. BN_CTX *ctx)
  312. {
  313. BN_CTX *new_ctx = NULL;
  314. const EC_POINT *generator = NULL;
  315. EC_POINT *tmp = NULL;
  316. size_t totalnum;
  317. size_t blocksize = 0, numblocks = 0; /* for wNAF splitting */
  318. size_t pre_points_per_block = 0;
  319. size_t i, j;
  320. int k;
  321. int r_is_inverted = 0;
  322. int r_is_at_infinity = 1;
  323. size_t *wsize = NULL; /* individual window sizes */
  324. signed char **wNAF = NULL; /* individual wNAFs */
  325. size_t *wNAF_len = NULL;
  326. size_t max_len = 0;
  327. size_t num_val;
  328. EC_POINT **val = NULL; /* precomputation */
  329. EC_POINT **v;
  330. EC_POINT ***val_sub = NULL; /* pointers to sub-arrays of 'val' or
  331. * 'pre_comp->points' */
  332. const EC_PRE_COMP *pre_comp = NULL;
  333. int num_scalar = 0; /* flag: will be set to 1 if 'scalar' must be
  334. * treated like other scalars, i.e.
  335. * precomputation is not available */
  336. int ret = 0;
  337. if (!ec_point_is_compat(r, group)) {
  338. ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);
  339. return 0;
  340. }
  341. if ((scalar == NULL) && (num == 0)) {
  342. return EC_POINT_set_to_infinity(group, r);
  343. }
  344. /*-
  345. * Handle the common cases where the scalar is secret, enforcing a constant
  346. * time scalar multiplication algorithm.
  347. */
  348. if ((scalar != NULL) && (num == 0)) {
  349. /*-
  350. * In this case we want to compute scalar * GeneratorPoint: this
  351. * codepath is reached most prominently by (ephemeral) key generation
  352. * of EC cryptosystems (i.e. ECDSA keygen and sign setup, ECDH
  353. * keygen/first half), where the scalar is always secret. This is why
  354. * we ignore if BN_FLG_CONSTTIME is actually set and we always call the
  355. * constant time version.
  356. */
  357. return ec_mul_consttime(group, r, scalar, NULL, ctx);
  358. }
  359. if ((scalar == NULL) && (num == 1)) {
  360. /*-
  361. * In this case we want to compute scalar * GenericPoint: this codepath
  362. * is reached most prominently by the second half of ECDH, where the
  363. * secret scalar is multiplied by the peer's public point. To protect
  364. * the secret scalar, we ignore if BN_FLG_CONSTTIME is actually set and
  365. * we always call the constant time version.
  366. */
  367. return ec_mul_consttime(group, r, scalars[0], points[0], ctx);
  368. }
  369. for (i = 0; i < num; i++) {
  370. if (!ec_point_is_compat(points[i], group)) {
  371. ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);
  372. return 0;
  373. }
  374. }
  375. if (ctx == NULL) {
  376. ctx = new_ctx = BN_CTX_new();
  377. if (ctx == NULL)
  378. goto err;
  379. }
  380. if (scalar != NULL) {
  381. generator = EC_GROUP_get0_generator(group);
  382. if (generator == NULL) {
  383. ECerr(EC_F_EC_WNAF_MUL, EC_R_UNDEFINED_GENERATOR);
  384. goto err;
  385. }
  386. /* look if we can use precomputed multiples of generator */
  387. pre_comp = group->pre_comp.ec;
  388. if (pre_comp && pre_comp->numblocks
  389. && (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) ==
  390. 0)) {
  391. blocksize = pre_comp->blocksize;
  392. /*
  393. * determine maximum number of blocks that wNAF splitting may
  394. * yield (NB: maximum wNAF length is bit length plus one)
  395. */
  396. numblocks = (BN_num_bits(scalar) / blocksize) + 1;
  397. /*
  398. * we cannot use more blocks than we have precomputation for
  399. */
  400. if (numblocks > pre_comp->numblocks)
  401. numblocks = pre_comp->numblocks;
  402. pre_points_per_block = (size_t)1 << (pre_comp->w - 1);
  403. /* check that pre_comp looks sane */
  404. if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block)) {
  405. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  406. goto err;
  407. }
  408. } else {
  409. /* can't use precomputation */
  410. pre_comp = NULL;
  411. numblocks = 1;
  412. num_scalar = 1; /* treat 'scalar' like 'num'-th element of
  413. * 'scalars' */
  414. }
  415. }
  416. totalnum = num + numblocks;
  417. wsize = OPENSSL_malloc(totalnum * sizeof(wsize[0]));
  418. wNAF_len = OPENSSL_malloc(totalnum * sizeof(wNAF_len[0]));
  419. /* include space for pivot */
  420. wNAF = OPENSSL_malloc((totalnum + 1) * sizeof(wNAF[0]));
  421. val_sub = OPENSSL_malloc(totalnum * sizeof(val_sub[0]));
  422. /* Ensure wNAF is initialised in case we end up going to err */
  423. if (wNAF != NULL)
  424. wNAF[0] = NULL; /* preliminary pivot */
  425. if (wsize == NULL || wNAF_len == NULL || wNAF == NULL || val_sub == NULL) {
  426. ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
  427. goto err;
  428. }
  429. /*
  430. * num_val will be the total number of temporarily precomputed points
  431. */
  432. num_val = 0;
  433. for (i = 0; i < num + num_scalar; i++) {
  434. size_t bits;
  435. bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar);
  436. wsize[i] = EC_window_bits_for_scalar_size(bits);
  437. num_val += (size_t)1 << (wsize[i] - 1);
  438. wNAF[i + 1] = NULL; /* make sure we always have a pivot */
  439. wNAF[i] =
  440. bn_compute_wNAF((i < num ? scalars[i] : scalar), wsize[i],
  441. &wNAF_len[i]);
  442. if (wNAF[i] == NULL)
  443. goto err;
  444. if (wNAF_len[i] > max_len)
  445. max_len = wNAF_len[i];
  446. }
  447. if (numblocks) {
  448. /* we go here iff scalar != NULL */
  449. if (pre_comp == NULL) {
  450. if (num_scalar != 1) {
  451. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  452. goto err;
  453. }
  454. /* we have already generated a wNAF for 'scalar' */
  455. } else {
  456. signed char *tmp_wNAF = NULL;
  457. size_t tmp_len = 0;
  458. if (num_scalar != 0) {
  459. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  460. goto err;
  461. }
  462. /*
  463. * use the window size for which we have precomputation
  464. */
  465. wsize[num] = pre_comp->w;
  466. tmp_wNAF = bn_compute_wNAF(scalar, wsize[num], &tmp_len);
  467. if (!tmp_wNAF)
  468. goto err;
  469. if (tmp_len <= max_len) {
  470. /*
  471. * One of the other wNAFs is at least as long as the wNAF
  472. * belonging to the generator, so wNAF splitting will not buy
  473. * us anything.
  474. */
  475. numblocks = 1;
  476. totalnum = num + 1; /* don't use wNAF splitting */
  477. wNAF[num] = tmp_wNAF;
  478. wNAF[num + 1] = NULL;
  479. wNAF_len[num] = tmp_len;
  480. /*
  481. * pre_comp->points starts with the points that we need here:
  482. */
  483. val_sub[num] = pre_comp->points;
  484. } else {
  485. /*
  486. * don't include tmp_wNAF directly into wNAF array - use wNAF
  487. * splitting and include the blocks
  488. */
  489. signed char *pp;
  490. EC_POINT **tmp_points;
  491. if (tmp_len < numblocks * blocksize) {
  492. /*
  493. * possibly we can do with fewer blocks than estimated
  494. */
  495. numblocks = (tmp_len + blocksize - 1) / blocksize;
  496. if (numblocks > pre_comp->numblocks) {
  497. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  498. OPENSSL_free(tmp_wNAF);
  499. goto err;
  500. }
  501. totalnum = num + numblocks;
  502. }
  503. /* split wNAF in 'numblocks' parts */
  504. pp = tmp_wNAF;
  505. tmp_points = pre_comp->points;
  506. for (i = num; i < totalnum; i++) {
  507. if (i < totalnum - 1) {
  508. wNAF_len[i] = blocksize;
  509. if (tmp_len < blocksize) {
  510. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  511. OPENSSL_free(tmp_wNAF);
  512. goto err;
  513. }
  514. tmp_len -= blocksize;
  515. } else
  516. /*
  517. * last block gets whatever is left (this could be
  518. * more or less than 'blocksize'!)
  519. */
  520. wNAF_len[i] = tmp_len;
  521. wNAF[i + 1] = NULL;
  522. wNAF[i] = OPENSSL_malloc(wNAF_len[i]);
  523. if (wNAF[i] == NULL) {
  524. ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
  525. OPENSSL_free(tmp_wNAF);
  526. goto err;
  527. }
  528. memcpy(wNAF[i], pp, wNAF_len[i]);
  529. if (wNAF_len[i] > max_len)
  530. max_len = wNAF_len[i];
  531. if (*tmp_points == NULL) {
  532. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  533. OPENSSL_free(tmp_wNAF);
  534. goto err;
  535. }
  536. val_sub[i] = tmp_points;
  537. tmp_points += pre_points_per_block;
  538. pp += blocksize;
  539. }
  540. OPENSSL_free(tmp_wNAF);
  541. }
  542. }
  543. }
  544. /*
  545. * All points we precompute now go into a single array 'val'.
  546. * 'val_sub[i]' is a pointer to the subarray for the i-th point, or to a
  547. * subarray of 'pre_comp->points' if we already have precomputation.
  548. */
  549. val = OPENSSL_malloc((num_val + 1) * sizeof(val[0]));
  550. if (val == NULL) {
  551. ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
  552. goto err;
  553. }
  554. val[num_val] = NULL; /* pivot element */
  555. /* allocate points for precomputation */
  556. v = val;
  557. for (i = 0; i < num + num_scalar; i++) {
  558. val_sub[i] = v;
  559. for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) {
  560. *v = EC_POINT_new(group);
  561. if (*v == NULL)
  562. goto err;
  563. v++;
  564. }
  565. }
  566. if (!(v == val + num_val)) {
  567. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  568. goto err;
  569. }
  570. if ((tmp = EC_POINT_new(group)) == NULL)
  571. goto err;
  572. /*-
  573. * prepare precomputed values:
  574. * val_sub[i][0] := points[i]
  575. * val_sub[i][1] := 3 * points[i]
  576. * val_sub[i][2] := 5 * points[i]
  577. * ...
  578. */
  579. for (i = 0; i < num + num_scalar; i++) {
  580. if (i < num) {
  581. if (!EC_POINT_copy(val_sub[i][0], points[i]))
  582. goto err;
  583. } else {
  584. if (!EC_POINT_copy(val_sub[i][0], generator))
  585. goto err;
  586. }
  587. if (wsize[i] > 1) {
  588. if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx))
  589. goto err;
  590. for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) {
  591. if (!EC_POINT_add
  592. (group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx))
  593. goto err;
  594. }
  595. }
  596. }
  597. if (!EC_POINTs_make_affine(group, num_val, val, ctx))
  598. goto err;
  599. r_is_at_infinity = 1;
  600. for (k = max_len - 1; k >= 0; k--) {
  601. if (!r_is_at_infinity) {
  602. if (!EC_POINT_dbl(group, r, r, ctx))
  603. goto err;
  604. }
  605. for (i = 0; i < totalnum; i++) {
  606. if (wNAF_len[i] > (size_t)k) {
  607. int digit = wNAF[i][k];
  608. int is_neg;
  609. if (digit) {
  610. is_neg = digit < 0;
  611. if (is_neg)
  612. digit = -digit;
  613. if (is_neg != r_is_inverted) {
  614. if (!r_is_at_infinity) {
  615. if (!EC_POINT_invert(group, r, ctx))
  616. goto err;
  617. }
  618. r_is_inverted = !r_is_inverted;
  619. }
  620. /* digit > 0 */
  621. if (r_is_at_infinity) {
  622. if (!EC_POINT_copy(r, val_sub[i][digit >> 1]))
  623. goto err;
  624. r_is_at_infinity = 0;
  625. } else {
  626. if (!EC_POINT_add
  627. (group, r, r, val_sub[i][digit >> 1], ctx))
  628. goto err;
  629. }
  630. }
  631. }
  632. }
  633. }
  634. if (r_is_at_infinity) {
  635. if (!EC_POINT_set_to_infinity(group, r))
  636. goto err;
  637. } else {
  638. if (r_is_inverted)
  639. if (!EC_POINT_invert(group, r, ctx))
  640. goto err;
  641. }
  642. ret = 1;
  643. err:
  644. BN_CTX_free(new_ctx);
  645. EC_POINT_free(tmp);
  646. OPENSSL_free(wsize);
  647. OPENSSL_free(wNAF_len);
  648. if (wNAF != NULL) {
  649. signed char **w;
  650. for (w = wNAF; *w != NULL; w++)
  651. OPENSSL_free(*w);
  652. OPENSSL_free(wNAF);
  653. }
  654. if (val != NULL) {
  655. for (v = val; *v != NULL; v++)
  656. EC_POINT_clear_free(*v);
  657. OPENSSL_free(val);
  658. }
  659. OPENSSL_free(val_sub);
  660. return ret;
  661. }
  662. /*-
  663. * ec_wNAF_precompute_mult()
  664. * creates an EC_PRE_COMP object with preprecomputed multiples of the generator
  665. * for use with wNAF splitting as implemented in ec_wNAF_mul().
  666. *
  667. * 'pre_comp->points' is an array of multiples of the generator
  668. * of the following form:
  669. * points[0] = generator;
  670. * points[1] = 3 * generator;
  671. * ...
  672. * points[2^(w-1)-1] = (2^(w-1)-1) * generator;
  673. * points[2^(w-1)] = 2^blocksize * generator;
  674. * points[2^(w-1)+1] = 3 * 2^blocksize * generator;
  675. * ...
  676. * points[2^(w-1)*(numblocks-1)-1] = (2^(w-1)) * 2^(blocksize*(numblocks-2)) * generator
  677. * points[2^(w-1)*(numblocks-1)] = 2^(blocksize*(numblocks-1)) * generator
  678. * ...
  679. * points[2^(w-1)*numblocks-1] = (2^(w-1)) * 2^(blocksize*(numblocks-1)) * generator
  680. * points[2^(w-1)*numblocks] = NULL
  681. */
  682. int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
  683. {
  684. const EC_POINT *generator;
  685. EC_POINT *tmp_point = NULL, *base = NULL, **var;
  686. BN_CTX *new_ctx = NULL;
  687. const BIGNUM *order;
  688. size_t i, bits, w, pre_points_per_block, blocksize, numblocks, num;
  689. EC_POINT **points = NULL;
  690. EC_PRE_COMP *pre_comp;
  691. int ret = 0;
  692. /* if there is an old EC_PRE_COMP object, throw it away */
  693. EC_pre_comp_free(group);
  694. if ((pre_comp = ec_pre_comp_new(group)) == NULL)
  695. return 0;
  696. generator = EC_GROUP_get0_generator(group);
  697. if (generator == NULL) {
  698. ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, EC_R_UNDEFINED_GENERATOR);
  699. goto err;
  700. }
  701. if (ctx == NULL) {
  702. ctx = new_ctx = BN_CTX_new();
  703. if (ctx == NULL)
  704. goto err;
  705. }
  706. BN_CTX_start(ctx);
  707. order = EC_GROUP_get0_order(group);
  708. if (order == NULL)
  709. goto err;
  710. if (BN_is_zero(order)) {
  711. ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, EC_R_UNKNOWN_ORDER);
  712. goto err;
  713. }
  714. bits = BN_num_bits(order);
  715. /*
  716. * The following parameters mean we precompute (approximately) one point
  717. * per bit. TBD: The combination 8, 4 is perfect for 160 bits; for other
  718. * bit lengths, other parameter combinations might provide better
  719. * efficiency.
  720. */
  721. blocksize = 8;
  722. w = 4;
  723. if (EC_window_bits_for_scalar_size(bits) > w) {
  724. /* let's not make the window too small ... */
  725. w = EC_window_bits_for_scalar_size(bits);
  726. }
  727. numblocks = (bits + blocksize - 1) / blocksize; /* max. number of blocks
  728. * to use for wNAF
  729. * splitting */
  730. pre_points_per_block = (size_t)1 << (w - 1);
  731. num = pre_points_per_block * numblocks; /* number of points to compute
  732. * and store */
  733. points = OPENSSL_malloc(sizeof(*points) * (num + 1));
  734. if (points == NULL) {
  735. ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
  736. goto err;
  737. }
  738. var = points;
  739. var[num] = NULL; /* pivot */
  740. for (i = 0; i < num; i++) {
  741. if ((var[i] = EC_POINT_new(group)) == NULL) {
  742. ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
  743. goto err;
  744. }
  745. }
  746. if ((tmp_point = EC_POINT_new(group)) == NULL
  747. || (base = EC_POINT_new(group)) == NULL) {
  748. ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
  749. goto err;
  750. }
  751. if (!EC_POINT_copy(base, generator))
  752. goto err;
  753. /* do the precomputation */
  754. for (i = 0; i < numblocks; i++) {
  755. size_t j;
  756. if (!EC_POINT_dbl(group, tmp_point, base, ctx))
  757. goto err;
  758. if (!EC_POINT_copy(*var++, base))
  759. goto err;
  760. for (j = 1; j < pre_points_per_block; j++, var++) {
  761. /*
  762. * calculate odd multiples of the current base point
  763. */
  764. if (!EC_POINT_add(group, *var, tmp_point, *(var - 1), ctx))
  765. goto err;
  766. }
  767. if (i < numblocks - 1) {
  768. /*
  769. * get the next base (multiply current one by 2^blocksize)
  770. */
  771. size_t k;
  772. if (blocksize <= 2) {
  773. ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_INTERNAL_ERROR);
  774. goto err;
  775. }
  776. if (!EC_POINT_dbl(group, base, tmp_point, ctx))
  777. goto err;
  778. for (k = 2; k < blocksize; k++) {
  779. if (!EC_POINT_dbl(group, base, base, ctx))
  780. goto err;
  781. }
  782. }
  783. }
  784. if (!EC_POINTs_make_affine(group, num, points, ctx))
  785. goto err;
  786. pre_comp->group = group;
  787. pre_comp->blocksize = blocksize;
  788. pre_comp->numblocks = numblocks;
  789. pre_comp->w = w;
  790. pre_comp->points = points;
  791. points = NULL;
  792. pre_comp->num = num;
  793. SETPRECOMP(group, ec, pre_comp);
  794. pre_comp = NULL;
  795. ret = 1;
  796. err:
  797. if (ctx != NULL)
  798. BN_CTX_end(ctx);
  799. BN_CTX_free(new_ctx);
  800. EC_ec_pre_comp_free(pre_comp);
  801. if (points) {
  802. EC_POINT **p;
  803. for (p = points; *p != NULL; p++)
  804. EC_POINT_free(*p);
  805. OPENSSL_free(points);
  806. }
  807. EC_POINT_free(tmp_point);
  808. EC_POINT_free(base);
  809. return ret;
  810. }
  811. int ec_wNAF_have_precompute_mult(const EC_GROUP *group)
  812. {
  813. return HAVEPRECOMP(group, ec);
  814. }