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