2
0

ec_mult.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. /* crypto/ec/ec_mult.c */
  2. /*
  3. * Originally written by Bodo Moeller and Nils Larsch for the OpenSSL project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1998-2019 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * [email protected].
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * ([email protected]). This product includes software written by Tim
  55. * Hudson ([email protected]).
  56. *
  57. */
  58. /* ====================================================================
  59. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  60. * Portions of this software developed by SUN MICROSYSTEMS, INC.,
  61. * and contributed to the OpenSSL project.
  62. */
  63. #include <string.h>
  64. #include <openssl/err.h>
  65. #include "ec_lcl.h"
  66. /*
  67. * This file implements the wNAF-based interleaving multi-exponentiation method
  68. * Formerly at:
  69. * http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/moeller.html#multiexp
  70. * You might now find it here:
  71. * http://link.springer.com/chapter/10.1007%2F3-540-45537-X_13
  72. * http://www.bmoeller.de/pdf/TI-01-08.multiexp.pdf
  73. * For multiplication with precomputation, we use wNAF splitting, formerly at:
  74. * http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/moeller.html#fastexp
  75. */
  76. /* structure for precomputed multiples of the generator */
  77. typedef struct ec_pre_comp_st {
  78. const EC_GROUP *group; /* parent EC_GROUP object */
  79. size_t blocksize; /* block size for wNAF splitting */
  80. size_t numblocks; /* max. number of blocks for which we have
  81. * precomputation */
  82. size_t w; /* window size */
  83. EC_POINT **points; /* array with pre-calculated multiples of
  84. * generator: 'num' pointers to EC_POINT
  85. * objects followed by a NULL */
  86. size_t num; /* numblocks * 2^(w-1) */
  87. int references;
  88. } EC_PRE_COMP;
  89. /* functions to manage EC_PRE_COMP within the EC_GROUP extra_data framework */
  90. static void *ec_pre_comp_dup(void *);
  91. static void ec_pre_comp_free(void *);
  92. static void ec_pre_comp_clear_free(void *);
  93. static EC_PRE_COMP *ec_pre_comp_new(const EC_GROUP *group)
  94. {
  95. EC_PRE_COMP *ret = NULL;
  96. if (!group)
  97. return NULL;
  98. ret = (EC_PRE_COMP *)OPENSSL_malloc(sizeof(EC_PRE_COMP));
  99. if (!ret) {
  100. ECerr(EC_F_EC_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
  101. return ret;
  102. }
  103. ret->group = group;
  104. ret->blocksize = 8; /* default */
  105. ret->numblocks = 0;
  106. ret->w = 4; /* default */
  107. ret->points = NULL;
  108. ret->num = 0;
  109. ret->references = 1;
  110. return ret;
  111. }
  112. static void *ec_pre_comp_dup(void *src_)
  113. {
  114. EC_PRE_COMP *src = src_;
  115. /* no need to actually copy, these objects never change! */
  116. CRYPTO_add(&src->references, 1, CRYPTO_LOCK_EC_PRE_COMP);
  117. return src_;
  118. }
  119. static void ec_pre_comp_free(void *pre_)
  120. {
  121. int i;
  122. EC_PRE_COMP *pre = pre_;
  123. if (!pre)
  124. return;
  125. i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
  126. if (i > 0)
  127. return;
  128. if (pre->points) {
  129. EC_POINT **p;
  130. for (p = pre->points; *p != NULL; p++)
  131. EC_POINT_free(*p);
  132. OPENSSL_free(pre->points);
  133. }
  134. OPENSSL_free(pre);
  135. }
  136. static void ec_pre_comp_clear_free(void *pre_)
  137. {
  138. int i;
  139. EC_PRE_COMP *pre = pre_;
  140. if (!pre)
  141. return;
  142. i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
  143. if (i > 0)
  144. return;
  145. if (pre->points) {
  146. EC_POINT **p;
  147. for (p = pre->points; *p != NULL; p++) {
  148. EC_POINT_clear_free(*p);
  149. OPENSSL_cleanse(p, sizeof(*p));
  150. }
  151. OPENSSL_free(pre->points);
  152. }
  153. OPENSSL_cleanse(pre, sizeof(*pre));
  154. OPENSSL_free(pre);
  155. }
  156. /*-
  157. * Determine the modified width-(w+1) Non-Adjacent Form (wNAF) of 'scalar'.
  158. * This is an array r[] of values that are either zero or odd with an
  159. * absolute value less than 2^w satisfying
  160. * scalar = \sum_j r[j]*2^j
  161. * where at most one of any w+1 consecutive digits is non-zero
  162. * with the exception that the most significant digit may be only
  163. * w-1 zeros away from that next non-zero digit.
  164. */
  165. static signed char *compute_wNAF(const BIGNUM *scalar, int w, size_t *ret_len)
  166. {
  167. int window_val;
  168. int ok = 0;
  169. signed char *r = NULL;
  170. int sign = 1;
  171. int bit, next_bit, mask;
  172. size_t len = 0, j;
  173. if (BN_is_zero(scalar)) {
  174. r = OPENSSL_malloc(1);
  175. if (!r) {
  176. ECerr(EC_F_COMPUTE_WNAF, ERR_R_MALLOC_FAILURE);
  177. goto err;
  178. }
  179. r[0] = 0;
  180. *ret_len = 1;
  181. return r;
  182. }
  183. if (w <= 0 || w > 7) { /* 'signed char' can represent integers with
  184. * absolute values less than 2^7 */
  185. ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
  186. goto err;
  187. }
  188. bit = 1 << w; /* at most 128 */
  189. next_bit = bit << 1; /* at most 256 */
  190. mask = next_bit - 1; /* at most 255 */
  191. if (BN_is_negative(scalar)) {
  192. sign = -1;
  193. }
  194. if (scalar->d == NULL || scalar->top == 0) {
  195. ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
  196. goto err;
  197. }
  198. len = BN_num_bits(scalar);
  199. r = OPENSSL_malloc(len + 1); /* modified wNAF may be one digit longer
  200. * than binary representation (*ret_len will
  201. * be set to the actual length, i.e. at most
  202. * BN_num_bits(scalar) + 1) */
  203. if (r == NULL) {
  204. ECerr(EC_F_COMPUTE_WNAF, ERR_R_MALLOC_FAILURE);
  205. goto err;
  206. }
  207. window_val = scalar->d[0] & mask;
  208. j = 0;
  209. while ((window_val != 0) || (j + w + 1 < len)) { /* if j+w+1 >= len,
  210. * window_val will not
  211. * increase */
  212. int digit = 0;
  213. /* 0 <= window_val <= 2^(w+1) */
  214. if (window_val & 1) {
  215. /* 0 < window_val < 2^(w+1) */
  216. if (window_val & bit) {
  217. digit = window_val - next_bit; /* -2^w < digit < 0 */
  218. #if 1 /* modified wNAF */
  219. if (j + w + 1 >= len) {
  220. /*
  221. * special case for generating modified wNAFs: no new
  222. * bits will be added into window_val, so using a
  223. * positive digit here will decrease the total length of
  224. * the representation
  225. */
  226. digit = window_val & (mask >> 1); /* 0 < digit < 2^w */
  227. }
  228. #endif
  229. } else {
  230. digit = window_val; /* 0 < digit < 2^w */
  231. }
  232. if (digit <= -bit || digit >= bit || !(digit & 1)) {
  233. ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
  234. goto err;
  235. }
  236. window_val -= digit;
  237. /*
  238. * now window_val is 0 or 2^(w+1) in standard wNAF generation;
  239. * for modified window NAFs, it may also be 2^w
  240. */
  241. if (window_val != 0 && window_val != next_bit
  242. && window_val != bit) {
  243. ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
  244. goto err;
  245. }
  246. }
  247. r[j++] = sign * digit;
  248. window_val >>= 1;
  249. window_val += bit * BN_is_bit_set(scalar, j + w);
  250. if (window_val > next_bit) {
  251. ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
  252. goto err;
  253. }
  254. }
  255. if (j > len + 1) {
  256. ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
  257. goto err;
  258. }
  259. len = j;
  260. ok = 1;
  261. err:
  262. if (!ok) {
  263. OPENSSL_free(r);
  264. r = NULL;
  265. }
  266. if (ok)
  267. *ret_len = len;
  268. return r;
  269. }
  270. #define EC_POINT_BN_set_flags(P, flags) do { \
  271. BN_set_flags(&(P)->X, (flags)); \
  272. BN_set_flags(&(P)->Y, (flags)); \
  273. BN_set_flags(&(P)->Z, (flags)); \
  274. } while(0)
  275. /*-
  276. * This functions computes (in constant time) a point multiplication over the
  277. * EC group.
  278. *
  279. * At a high level, it is Montgomery ladder with conditional swaps.
  280. *
  281. * It performs either a fixed scalar point multiplication
  282. * (scalar * generator)
  283. * when point is NULL, or a generic scalar point multiplication
  284. * (scalar * point)
  285. * when point is not NULL.
  286. *
  287. * scalar should be in the range [0,n) otherwise all constant time bets are off.
  288. *
  289. * NB: This says nothing about EC_POINT_add and EC_POINT_dbl,
  290. * which of course are not constant time themselves.
  291. *
  292. * The product is stored in r.
  293. *
  294. * Returns 1 on success, 0 otherwise.
  295. */
  296. static int ec_mul_consttime(const EC_GROUP *group, EC_POINT *r,
  297. const BIGNUM *scalar, const EC_POINT *point,
  298. BN_CTX *ctx)
  299. {
  300. int i, cardinality_bits, group_top, kbit, pbit, Z_is_one;
  301. EC_POINT *s = NULL;
  302. BIGNUM *k = NULL;
  303. BIGNUM *lambda = NULL;
  304. BIGNUM *cardinality = NULL;
  305. BN_CTX *new_ctx = NULL;
  306. int ret = 0;
  307. if (ctx == NULL && (ctx = new_ctx = BN_CTX_new()) == NULL)
  308. return 0;
  309. BN_CTX_start(ctx);
  310. s = EC_POINT_new(group);
  311. if (s == NULL)
  312. goto err;
  313. if (point == NULL) {
  314. if (!EC_POINT_copy(s, group->generator))
  315. goto err;
  316. } else {
  317. if (!EC_POINT_copy(s, point))
  318. goto err;
  319. }
  320. EC_POINT_BN_set_flags(s, BN_FLG_CONSTTIME);
  321. cardinality = BN_CTX_get(ctx);
  322. lambda = BN_CTX_get(ctx);
  323. k = BN_CTX_get(ctx);
  324. if (k == NULL || !BN_mul(cardinality, &group->order, &group->cofactor, ctx))
  325. goto err;
  326. /*
  327. * Group cardinalities are often on a word boundary.
  328. * So when we pad the scalar, some timing diff might
  329. * pop if it needs to be expanded due to carries.
  330. * So expand ahead of time.
  331. */
  332. cardinality_bits = BN_num_bits(cardinality);
  333. group_top = cardinality->top;
  334. if ((bn_wexpand(k, group_top + 2) == NULL)
  335. || (bn_wexpand(lambda, group_top + 2) == NULL))
  336. goto err;
  337. if (!BN_copy(k, scalar))
  338. goto err;
  339. BN_set_flags(k, BN_FLG_CONSTTIME);
  340. if ((BN_num_bits(k) > cardinality_bits) || (BN_is_negative(k))) {
  341. /*-
  342. * this is an unusual input, and we don't guarantee
  343. * constant-timeness
  344. */
  345. if (!BN_nnmod(k, k, cardinality, ctx))
  346. goto err;
  347. }
  348. if (!BN_add(lambda, k, cardinality))
  349. goto err;
  350. BN_set_flags(lambda, BN_FLG_CONSTTIME);
  351. if (!BN_add(k, lambda, cardinality))
  352. goto err;
  353. /*
  354. * lambda := scalar + cardinality
  355. * k := scalar + 2*cardinality
  356. */
  357. kbit = BN_is_bit_set(lambda, cardinality_bits);
  358. BN_consttime_swap(kbit, k, lambda, group_top + 2);
  359. group_top = group->field.top;
  360. if ((bn_wexpand(&s->X, group_top) == NULL)
  361. || (bn_wexpand(&s->Y, group_top) == NULL)
  362. || (bn_wexpand(&s->Z, group_top) == NULL)
  363. || (bn_wexpand(&r->X, group_top) == NULL)
  364. || (bn_wexpand(&r->Y, group_top) == NULL)
  365. || (bn_wexpand(&r->Z, group_top) == NULL))
  366. goto err;
  367. /* top bit is a 1, in a fixed pos */
  368. if (!EC_POINT_copy(r, s))
  369. goto err;
  370. EC_POINT_BN_set_flags(r, BN_FLG_CONSTTIME);
  371. if (!EC_POINT_dbl(group, s, s, ctx))
  372. goto err;
  373. pbit = 0;
  374. #define EC_POINT_CSWAP(c, a, b, w, t) do { \
  375. BN_consttime_swap(c, &(a)->X, &(b)->X, w); \
  376. BN_consttime_swap(c, &(a)->Y, &(b)->Y, w); \
  377. BN_consttime_swap(c, &(a)->Z, &(b)->Z, w); \
  378. t = ((a)->Z_is_one ^ (b)->Z_is_one) & (c); \
  379. (a)->Z_is_one ^= (t); \
  380. (b)->Z_is_one ^= (t); \
  381. } while(0)
  382. /*-
  383. * The ladder step, with branches, is
  384. *
  385. * k[i] == 0: S = add(R, S), R = dbl(R)
  386. * k[i] == 1: R = add(S, R), S = dbl(S)
  387. *
  388. * Swapping R, S conditionally on k[i] leaves you with state
  389. *
  390. * k[i] == 0: T, U = R, S
  391. * k[i] == 1: T, U = S, R
  392. *
  393. * Then perform the ECC ops.
  394. *
  395. * U = add(T, U)
  396. * T = dbl(T)
  397. *
  398. * Which leaves you with state
  399. *
  400. * k[i] == 0: U = add(R, S), T = dbl(R)
  401. * k[i] == 1: U = add(S, R), T = dbl(S)
  402. *
  403. * Swapping T, U conditionally on k[i] leaves you with state
  404. *
  405. * k[i] == 0: R, S = T, U
  406. * k[i] == 1: R, S = U, T
  407. *
  408. * Which leaves you with state
  409. *
  410. * k[i] == 0: S = add(R, S), R = dbl(R)
  411. * k[i] == 1: R = add(S, R), S = dbl(S)
  412. *
  413. * So we get the same logic, but instead of a branch it's a
  414. * conditional swap, followed by ECC ops, then another conditional swap.
  415. *
  416. * Optimization: The end of iteration i and start of i-1 looks like
  417. *
  418. * ...
  419. * CSWAP(k[i], R, S)
  420. * ECC
  421. * CSWAP(k[i], R, S)
  422. * (next iteration)
  423. * CSWAP(k[i-1], R, S)
  424. * ECC
  425. * CSWAP(k[i-1], R, S)
  426. * ...
  427. *
  428. * So instead of two contiguous swaps, you can merge the condition
  429. * bits and do a single swap.
  430. *
  431. * k[i] k[i-1] Outcome
  432. * 0 0 No Swap
  433. * 0 1 Swap
  434. * 1 0 Swap
  435. * 1 1 No Swap
  436. *
  437. * This is XOR. pbit tracks the previous bit of k.
  438. */
  439. for (i = cardinality_bits - 1; i >= 0; i--) {
  440. kbit = BN_is_bit_set(k, i) ^ pbit;
  441. EC_POINT_CSWAP(kbit, r, s, group_top, Z_is_one);
  442. if (!EC_POINT_add(group, s, r, s, ctx))
  443. goto err;
  444. if (!EC_POINT_dbl(group, r, r, ctx))
  445. goto err;
  446. /*
  447. * pbit logic merges this cswap with that of the
  448. * next iteration
  449. */
  450. pbit ^= kbit;
  451. }
  452. /* one final cswap to move the right value into r */
  453. EC_POINT_CSWAP(pbit, r, s, group_top, Z_is_one);
  454. #undef EC_POINT_CSWAP
  455. ret = 1;
  456. err:
  457. EC_POINT_clear_free(s);
  458. BN_CTX_end(ctx);
  459. BN_CTX_free(new_ctx);
  460. return ret;
  461. }
  462. #undef EC_POINT_BN_set_flags
  463. /*
  464. * TODO: table should be optimised for the wNAF-based implementation,
  465. * sometimes smaller windows will give better performance (thus the
  466. * boundaries should be increased)
  467. */
  468. #define EC_window_bits_for_scalar_size(b) \
  469. ((size_t) \
  470. ((b) >= 2000 ? 6 : \
  471. (b) >= 800 ? 5 : \
  472. (b) >= 300 ? 4 : \
  473. (b) >= 70 ? 3 : \
  474. (b) >= 20 ? 2 : \
  475. 1))
  476. /*-
  477. * Compute
  478. * \sum scalars[i]*points[i],
  479. * also including
  480. * scalar*generator
  481. * in the addition if scalar != NULL
  482. */
  483. int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
  484. size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
  485. BN_CTX *ctx)
  486. {
  487. BN_CTX *new_ctx = NULL;
  488. const EC_POINT *generator = NULL;
  489. EC_POINT *tmp = NULL;
  490. size_t totalnum;
  491. size_t blocksize = 0, numblocks = 0; /* for wNAF splitting */
  492. size_t pre_points_per_block = 0;
  493. size_t i, j;
  494. int k;
  495. int r_is_inverted = 0;
  496. int r_is_at_infinity = 1;
  497. size_t *wsize = NULL; /* individual window sizes */
  498. signed char **wNAF = NULL; /* individual wNAFs */
  499. size_t *wNAF_len = NULL;
  500. size_t max_len = 0;
  501. size_t num_val;
  502. EC_POINT **val = NULL; /* precomputation */
  503. EC_POINT **v;
  504. EC_POINT ***val_sub = NULL; /* pointers to sub-arrays of 'val' or
  505. * 'pre_comp->points' */
  506. const EC_PRE_COMP *pre_comp = NULL;
  507. int num_scalar = 0; /* flag: will be set to 1 if 'scalar' must be
  508. * treated like other scalars, i.e.
  509. * precomputation is not available */
  510. int ret = 0;
  511. if (group->meth != r->meth) {
  512. ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);
  513. return 0;
  514. }
  515. if ((scalar == NULL) && (num == 0)) {
  516. return EC_POINT_set_to_infinity(group, r);
  517. }
  518. if (!BN_is_zero(&group->order) && !BN_is_zero(&group->cofactor)) {
  519. /*-
  520. * Handle the common cases where the scalar is secret, enforcing a constant
  521. * time scalar multiplication algorithm.
  522. */
  523. if ((scalar != NULL) && (num == 0)) {
  524. /*-
  525. * In this case we want to compute scalar * GeneratorPoint: this
  526. * codepath is reached most prominently by (ephemeral) key generation
  527. * of EC cryptosystems (i.e. ECDSA keygen and sign setup, ECDH
  528. * keygen/first half), where the scalar is always secret. This is why
  529. * we ignore if BN_FLG_CONSTTIME is actually set and we always call the
  530. * constant time version.
  531. */
  532. return ec_mul_consttime(group, r, scalar, NULL, ctx);
  533. }
  534. if ((scalar == NULL) && (num == 1)) {
  535. /*-
  536. * In this case we want to compute scalar * GenericPoint: this codepath
  537. * is reached most prominently by the second half of ECDH, where the
  538. * secret scalar is multiplied by the peer's public point. To protect
  539. * the secret scalar, we ignore if BN_FLG_CONSTTIME is actually set and
  540. * we always call the constant time version.
  541. */
  542. return ec_mul_consttime(group, r, scalars[0], points[0], ctx);
  543. }
  544. }
  545. for (i = 0; i < num; i++) {
  546. if (group->meth != points[i]->meth) {
  547. ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);
  548. return 0;
  549. }
  550. }
  551. if (ctx == NULL) {
  552. ctx = new_ctx = BN_CTX_new();
  553. if (ctx == NULL)
  554. goto err;
  555. }
  556. if (scalar != NULL) {
  557. generator = EC_GROUP_get0_generator(group);
  558. if (generator == NULL) {
  559. ECerr(EC_F_EC_WNAF_MUL, EC_R_UNDEFINED_GENERATOR);
  560. goto err;
  561. }
  562. /* look if we can use precomputed multiples of generator */
  563. pre_comp =
  564. EC_EX_DATA_get_data(group->extra_data, ec_pre_comp_dup,
  565. ec_pre_comp_free, ec_pre_comp_clear_free);
  566. if (pre_comp && pre_comp->numblocks
  567. && (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) ==
  568. 0)) {
  569. blocksize = pre_comp->blocksize;
  570. /*
  571. * determine maximum number of blocks that wNAF splitting may
  572. * yield (NB: maximum wNAF length is bit length plus one)
  573. */
  574. numblocks = (BN_num_bits(scalar) / blocksize) + 1;
  575. /*
  576. * we cannot use more blocks than we have precomputation for
  577. */
  578. if (numblocks > pre_comp->numblocks)
  579. numblocks = pre_comp->numblocks;
  580. pre_points_per_block = (size_t)1 << (pre_comp->w - 1);
  581. /* check that pre_comp looks sane */
  582. if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block)) {
  583. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  584. goto err;
  585. }
  586. } else {
  587. /* can't use precomputation */
  588. pre_comp = NULL;
  589. numblocks = 1;
  590. num_scalar = 1; /* treat 'scalar' like 'num'-th element of
  591. * 'scalars' */
  592. }
  593. }
  594. totalnum = num + numblocks;
  595. wsize = OPENSSL_malloc(totalnum * sizeof(wsize[0]));
  596. wNAF_len = OPENSSL_malloc(totalnum * sizeof(wNAF_len[0]));
  597. /* include space for pivot */
  598. wNAF = OPENSSL_malloc((totalnum + 1) * sizeof(wNAF[0]));
  599. val_sub = OPENSSL_malloc(totalnum * sizeof(val_sub[0]));
  600. /* Ensure wNAF is initialised in case we end up going to err */
  601. if (wNAF)
  602. wNAF[0] = NULL; /* preliminary pivot */
  603. if (!wsize || !wNAF_len || !wNAF || !val_sub) {
  604. ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
  605. goto err;
  606. }
  607. /*
  608. * num_val will be the total number of temporarily precomputed points
  609. */
  610. num_val = 0;
  611. for (i = 0; i < num + num_scalar; i++) {
  612. size_t bits;
  613. bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar);
  614. wsize[i] = EC_window_bits_for_scalar_size(bits);
  615. num_val += (size_t)1 << (wsize[i] - 1);
  616. wNAF[i + 1] = NULL; /* make sure we always have a pivot */
  617. wNAF[i] =
  618. compute_wNAF((i < num ? scalars[i] : scalar), wsize[i],
  619. &wNAF_len[i]);
  620. if (wNAF[i] == NULL)
  621. goto err;
  622. if (wNAF_len[i] > max_len)
  623. max_len = wNAF_len[i];
  624. }
  625. if (numblocks) {
  626. /* we go here iff scalar != NULL */
  627. if (pre_comp == NULL) {
  628. if (num_scalar != 1) {
  629. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  630. goto err;
  631. }
  632. /* we have already generated a wNAF for 'scalar' */
  633. } else {
  634. signed char *tmp_wNAF = NULL;
  635. size_t tmp_len = 0;
  636. if (num_scalar != 0) {
  637. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  638. goto err;
  639. }
  640. /*
  641. * use the window size for which we have precomputation
  642. */
  643. wsize[num] = pre_comp->w;
  644. tmp_wNAF = compute_wNAF(scalar, wsize[num], &tmp_len);
  645. if (!tmp_wNAF)
  646. goto err;
  647. if (tmp_len <= max_len) {
  648. /*
  649. * One of the other wNAFs is at least as long as the wNAF
  650. * belonging to the generator, so wNAF splitting will not buy
  651. * us anything.
  652. */
  653. numblocks = 1;
  654. totalnum = num + 1; /* don't use wNAF splitting */
  655. wNAF[num] = tmp_wNAF;
  656. wNAF[num + 1] = NULL;
  657. wNAF_len[num] = tmp_len;
  658. if (tmp_len > max_len)
  659. max_len = tmp_len;
  660. /*
  661. * pre_comp->points starts with the points that we need here:
  662. */
  663. val_sub[num] = pre_comp->points;
  664. } else {
  665. /*
  666. * don't include tmp_wNAF directly into wNAF array - use wNAF
  667. * splitting and include the blocks
  668. */
  669. signed char *pp;
  670. EC_POINT **tmp_points;
  671. if (tmp_len < numblocks * blocksize) {
  672. /*
  673. * possibly we can do with fewer blocks than estimated
  674. */
  675. numblocks = (tmp_len + blocksize - 1) / blocksize;
  676. if (numblocks > pre_comp->numblocks) {
  677. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  678. goto err;
  679. }
  680. totalnum = num + numblocks;
  681. }
  682. /* split wNAF in 'numblocks' parts */
  683. pp = tmp_wNAF;
  684. tmp_points = pre_comp->points;
  685. for (i = num; i < totalnum; i++) {
  686. if (i < totalnum - 1) {
  687. wNAF_len[i] = blocksize;
  688. if (tmp_len < blocksize) {
  689. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  690. goto err;
  691. }
  692. tmp_len -= blocksize;
  693. } else
  694. /*
  695. * last block gets whatever is left (this could be
  696. * more or less than 'blocksize'!)
  697. */
  698. wNAF_len[i] = tmp_len;
  699. wNAF[i + 1] = NULL;
  700. wNAF[i] = OPENSSL_malloc(wNAF_len[i]);
  701. if (wNAF[i] == NULL) {
  702. ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
  703. OPENSSL_free(tmp_wNAF);
  704. goto err;
  705. }
  706. memcpy(wNAF[i], pp, wNAF_len[i]);
  707. if (wNAF_len[i] > max_len)
  708. max_len = wNAF_len[i];
  709. if (*tmp_points == NULL) {
  710. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  711. OPENSSL_free(tmp_wNAF);
  712. goto err;
  713. }
  714. val_sub[i] = tmp_points;
  715. tmp_points += pre_points_per_block;
  716. pp += blocksize;
  717. }
  718. OPENSSL_free(tmp_wNAF);
  719. }
  720. }
  721. }
  722. /*
  723. * All points we precompute now go into a single array 'val'.
  724. * 'val_sub[i]' is a pointer to the subarray for the i-th point, or to a
  725. * subarray of 'pre_comp->points' if we already have precomputation.
  726. */
  727. val = OPENSSL_malloc((num_val + 1) * sizeof(val[0]));
  728. if (val == NULL) {
  729. ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
  730. goto err;
  731. }
  732. val[num_val] = NULL; /* pivot element */
  733. /* allocate points for precomputation */
  734. v = val;
  735. for (i = 0; i < num + num_scalar; i++) {
  736. val_sub[i] = v;
  737. for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) {
  738. *v = EC_POINT_new(group);
  739. if (*v == NULL)
  740. goto err;
  741. v++;
  742. }
  743. }
  744. if (!(v == val + num_val)) {
  745. ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
  746. goto err;
  747. }
  748. if (!(tmp = EC_POINT_new(group)))
  749. goto err;
  750. /*-
  751. * prepare precomputed values:
  752. * val_sub[i][0] := points[i]
  753. * val_sub[i][1] := 3 * points[i]
  754. * val_sub[i][2] := 5 * points[i]
  755. * ...
  756. */
  757. for (i = 0; i < num + num_scalar; i++) {
  758. if (i < num) {
  759. if (!EC_POINT_copy(val_sub[i][0], points[i]))
  760. goto err;
  761. } else {
  762. if (!EC_POINT_copy(val_sub[i][0], generator))
  763. goto err;
  764. }
  765. if (wsize[i] > 1) {
  766. if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx))
  767. goto err;
  768. for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) {
  769. if (!EC_POINT_add
  770. (group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx))
  771. goto err;
  772. }
  773. }
  774. }
  775. #if 1 /* optional; EC_window_bits_for_scalar_size
  776. * assumes we do this step */
  777. if (!EC_POINTs_make_affine(group, num_val, val, ctx))
  778. goto err;
  779. #endif
  780. r_is_at_infinity = 1;
  781. for (k = max_len - 1; k >= 0; k--) {
  782. if (!r_is_at_infinity) {
  783. if (!EC_POINT_dbl(group, r, r, ctx))
  784. goto err;
  785. }
  786. for (i = 0; i < totalnum; i++) {
  787. if (wNAF_len[i] > (size_t)k) {
  788. int digit = wNAF[i][k];
  789. int is_neg;
  790. if (digit) {
  791. is_neg = digit < 0;
  792. if (is_neg)
  793. digit = -digit;
  794. if (is_neg != r_is_inverted) {
  795. if (!r_is_at_infinity) {
  796. if (!EC_POINT_invert(group, r, ctx))
  797. goto err;
  798. }
  799. r_is_inverted = !r_is_inverted;
  800. }
  801. /* digit > 0 */
  802. if (r_is_at_infinity) {
  803. if (!EC_POINT_copy(r, val_sub[i][digit >> 1]))
  804. goto err;
  805. r_is_at_infinity = 0;
  806. } else {
  807. if (!EC_POINT_add
  808. (group, r, r, val_sub[i][digit >> 1], ctx))
  809. goto err;
  810. }
  811. }
  812. }
  813. }
  814. }
  815. if (r_is_at_infinity) {
  816. if (!EC_POINT_set_to_infinity(group, r))
  817. goto err;
  818. } else {
  819. if (r_is_inverted)
  820. if (!EC_POINT_invert(group, r, ctx))
  821. goto err;
  822. }
  823. ret = 1;
  824. err:
  825. if (new_ctx != NULL)
  826. BN_CTX_free(new_ctx);
  827. if (tmp != NULL)
  828. EC_POINT_free(tmp);
  829. if (wsize != NULL)
  830. OPENSSL_free(wsize);
  831. if (wNAF_len != NULL)
  832. OPENSSL_free(wNAF_len);
  833. if (wNAF != NULL) {
  834. signed char **w;
  835. for (w = wNAF; *w != NULL; w++)
  836. OPENSSL_free(*w);
  837. OPENSSL_free(wNAF);
  838. }
  839. if (val != NULL) {
  840. for (v = val; *v != NULL; v++)
  841. EC_POINT_clear_free(*v);
  842. OPENSSL_free(val);
  843. }
  844. if (val_sub != NULL) {
  845. OPENSSL_free(val_sub);
  846. }
  847. return ret;
  848. }
  849. /*-
  850. * ec_wNAF_precompute_mult()
  851. * creates an EC_PRE_COMP object with preprecomputed multiples of the generator
  852. * for use with wNAF splitting as implemented in ec_wNAF_mul().
  853. *
  854. * 'pre_comp->points' is an array of multiples of the generator
  855. * of the following form:
  856. * points[0] = generator;
  857. * points[1] = 3 * generator;
  858. * ...
  859. * points[2^(w-1)-1] = (2^(w-1)-1) * generator;
  860. * points[2^(w-1)] = 2^blocksize * generator;
  861. * points[2^(w-1)+1] = 3 * 2^blocksize * generator;
  862. * ...
  863. * points[2^(w-1)*(numblocks-1)-1] = (2^(w-1)) * 2^(blocksize*(numblocks-2)) * generator
  864. * points[2^(w-1)*(numblocks-1)] = 2^(blocksize*(numblocks-1)) * generator
  865. * ...
  866. * points[2^(w-1)*numblocks-1] = (2^(w-1)) * 2^(blocksize*(numblocks-1)) * generator
  867. * points[2^(w-1)*numblocks] = NULL
  868. */
  869. int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
  870. {
  871. const EC_POINT *generator;
  872. EC_POINT *tmp_point = NULL, *base = NULL, **var;
  873. BN_CTX *new_ctx = NULL;
  874. BIGNUM *order;
  875. size_t i, bits, w, pre_points_per_block, blocksize, numblocks, num;
  876. EC_POINT **points = NULL;
  877. EC_PRE_COMP *pre_comp;
  878. int ret = 0;
  879. /* if there is an old EC_PRE_COMP object, throw it away */
  880. EC_EX_DATA_free_data(&group->extra_data, ec_pre_comp_dup,
  881. ec_pre_comp_free, ec_pre_comp_clear_free);
  882. if ((pre_comp = ec_pre_comp_new(group)) == NULL)
  883. return 0;
  884. generator = EC_GROUP_get0_generator(group);
  885. if (generator == NULL) {
  886. ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, EC_R_UNDEFINED_GENERATOR);
  887. goto err;
  888. }
  889. if (ctx == NULL) {
  890. ctx = new_ctx = BN_CTX_new();
  891. if (ctx == NULL)
  892. goto err;
  893. }
  894. BN_CTX_start(ctx);
  895. order = BN_CTX_get(ctx);
  896. if (order == NULL)
  897. goto err;
  898. if (!EC_GROUP_get_order(group, order, ctx))
  899. goto err;
  900. if (BN_is_zero(order)) {
  901. ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, EC_R_UNKNOWN_ORDER);
  902. goto err;
  903. }
  904. bits = BN_num_bits(order);
  905. /*
  906. * The following parameters mean we precompute (approximately) one point
  907. * per bit. TBD: The combination 8, 4 is perfect for 160 bits; for other
  908. * bit lengths, other parameter combinations might provide better
  909. * efficiency.
  910. */
  911. blocksize = 8;
  912. w = 4;
  913. if (EC_window_bits_for_scalar_size(bits) > w) {
  914. /* let's not make the window too small ... */
  915. w = EC_window_bits_for_scalar_size(bits);
  916. }
  917. numblocks = (bits + blocksize - 1) / blocksize; /* max. number of blocks
  918. * to use for wNAF
  919. * splitting */
  920. pre_points_per_block = (size_t)1 << (w - 1);
  921. num = pre_points_per_block * numblocks; /* number of points to compute
  922. * and store */
  923. points = OPENSSL_malloc(sizeof(EC_POINT *) * (num + 1));
  924. if (!points) {
  925. ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
  926. goto err;
  927. }
  928. var = points;
  929. var[num] = NULL; /* pivot */
  930. for (i = 0; i < num; i++) {
  931. if ((var[i] = EC_POINT_new(group)) == NULL) {
  932. ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
  933. goto err;
  934. }
  935. }
  936. if (!(tmp_point = EC_POINT_new(group)) || !(base = EC_POINT_new(group))) {
  937. ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
  938. goto err;
  939. }
  940. if (!EC_POINT_copy(base, generator))
  941. goto err;
  942. /* do the precomputation */
  943. for (i = 0; i < numblocks; i++) {
  944. size_t j;
  945. if (!EC_POINT_dbl(group, tmp_point, base, ctx))
  946. goto err;
  947. if (!EC_POINT_copy(*var++, base))
  948. goto err;
  949. for (j = 1; j < pre_points_per_block; j++, var++) {
  950. /*
  951. * calculate odd multiples of the current base point
  952. */
  953. if (!EC_POINT_add(group, *var, tmp_point, *(var - 1), ctx))
  954. goto err;
  955. }
  956. if (i < numblocks - 1) {
  957. /*
  958. * get the next base (multiply current one by 2^blocksize)
  959. */
  960. size_t k;
  961. if (blocksize <= 2) {
  962. ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_INTERNAL_ERROR);
  963. goto err;
  964. }
  965. if (!EC_POINT_dbl(group, base, tmp_point, ctx))
  966. goto err;
  967. for (k = 2; k < blocksize; k++) {
  968. if (!EC_POINT_dbl(group, base, base, ctx))
  969. goto err;
  970. }
  971. }
  972. }
  973. if (!EC_POINTs_make_affine(group, num, points, ctx))
  974. goto err;
  975. pre_comp->group = group;
  976. pre_comp->blocksize = blocksize;
  977. pre_comp->numblocks = numblocks;
  978. pre_comp->w = w;
  979. pre_comp->points = points;
  980. points = NULL;
  981. pre_comp->num = num;
  982. if (!EC_EX_DATA_set_data(&group->extra_data, pre_comp,
  983. ec_pre_comp_dup, ec_pre_comp_free,
  984. ec_pre_comp_clear_free))
  985. goto err;
  986. pre_comp = NULL;
  987. ret = 1;
  988. err:
  989. if (ctx != NULL)
  990. BN_CTX_end(ctx);
  991. if (new_ctx != NULL)
  992. BN_CTX_free(new_ctx);
  993. if (pre_comp)
  994. ec_pre_comp_free(pre_comp);
  995. if (points) {
  996. EC_POINT **p;
  997. for (p = points; *p != NULL; p++)
  998. EC_POINT_free(*p);
  999. OPENSSL_free(points);
  1000. }
  1001. if (tmp_point)
  1002. EC_POINT_free(tmp_point);
  1003. if (base)
  1004. EC_POINT_free(base);
  1005. return ret;
  1006. }
  1007. int ec_wNAF_have_precompute_mult(const EC_GROUP *group)
  1008. {
  1009. if (EC_EX_DATA_get_data
  1010. (group->extra_data, ec_pre_comp_dup, ec_pre_comp_free,
  1011. ec_pre_comp_clear_free) != NULL)
  1012. return 1;
  1013. else
  1014. return 0;
  1015. }