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