1 /* crypto/ec/ec_mult.c */
3 * Originally written by Bodo Moeller and Nils Larsch for the OpenSSL project.
5 /* ====================================================================
6 * Copyright (c) 1998-2019 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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 * openssl-core@openssl.org.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
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.
66 #include <openssl/err.h>
71 * This file implements the wNAF-based interleaving multi-exponentiation method
73 * http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/moeller.html#multiexp
74 * You might now find it here:
75 * http://link.springer.com/chapter/10.1007%2F3-540-45537-X_13
76 * http://www.bmoeller.de/pdf/TI-01-08.multiexp.pdf
77 * For multiplication with precomputation, we use wNAF splitting, formerly at:
78 * http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/moeller.html#fastexp
81 /* structure for precomputed multiples of the generator */
82 typedef struct ec_pre_comp_st
{
83 const EC_GROUP
*group
; /* parent EC_GROUP object */
84 size_t blocksize
; /* block size for wNAF splitting */
85 size_t numblocks
; /* max. number of blocks for which we have
87 size_t w
; /* window size */
88 EC_POINT
**points
; /* array with pre-calculated multiples of
89 * generator: 'num' pointers to EC_POINT
90 * objects followed by a NULL */
91 size_t num
; /* numblocks * 2^(w-1) */
95 /* functions to manage EC_PRE_COMP within the EC_GROUP extra_data framework */
96 static void *ec_pre_comp_dup(void *);
97 static void ec_pre_comp_free(void *);
98 static void ec_pre_comp_clear_free(void *);
100 static EC_PRE_COMP
*ec_pre_comp_new(const EC_GROUP
*group
)
102 EC_PRE_COMP
*ret
= NULL
;
107 ret
= (EC_PRE_COMP
*)OPENSSL_malloc(sizeof(EC_PRE_COMP
));
109 ECerr(EC_F_EC_PRE_COMP_NEW
, ERR_R_MALLOC_FAILURE
);
113 ret
->blocksize
= 8; /* default */
115 ret
->w
= 4; /* default */
122 static void *ec_pre_comp_dup(void *src_
)
124 EC_PRE_COMP
*src
= src_
;
126 /* no need to actually copy, these objects never change! */
128 CRYPTO_add(&src
->references
, 1, CRYPTO_LOCK_EC_PRE_COMP
);
133 static void ec_pre_comp_free(void *pre_
)
136 EC_PRE_COMP
*pre
= pre_
;
141 i
= CRYPTO_add(&pre
->references
, -1, CRYPTO_LOCK_EC_PRE_COMP
);
148 for (p
= pre
->points
; *p
!= NULL
; p
++)
150 OPENSSL_free(pre
->points
);
155 static void ec_pre_comp_clear_free(void *pre_
)
158 EC_PRE_COMP
*pre
= pre_
;
163 i
= CRYPTO_add(&pre
->references
, -1, CRYPTO_LOCK_EC_PRE_COMP
);
170 for (p
= pre
->points
; *p
!= NULL
; p
++) {
171 EC_POINT_clear_free(*p
);
172 OPENSSL_cleanse(p
, sizeof(*p
));
174 OPENSSL_free(pre
->points
);
176 OPENSSL_cleanse(pre
, sizeof(*pre
));
181 * Determine the modified width-(w+1) Non-Adjacent Form (wNAF) of 'scalar'.
182 * This is an array r[] of values that are either zero or odd with an
183 * absolute value less than 2^w satisfying
184 * scalar = \sum_j r[j]*2^j
185 * where at most one of any w+1 consecutive digits is non-zero
186 * with the exception that the most significant digit may be only
187 * w-1 zeros away from that next non-zero digit.
189 static signed char *compute_wNAF(const BIGNUM
*scalar
, int w
, size_t *ret_len
)
193 signed char *r
= NULL
;
195 int bit
, next_bit
, mask
;
198 if (BN_is_zero(scalar
)) {
199 r
= OPENSSL_malloc(1);
201 ECerr(EC_F_COMPUTE_WNAF
, ERR_R_MALLOC_FAILURE
);
209 if (w
<= 0 || w
> 7) { /* 'signed char' can represent integers with
210 * absolute values less than 2^7 */
211 ECerr(EC_F_COMPUTE_WNAF
, ERR_R_INTERNAL_ERROR
);
214 bit
= 1 << w
; /* at most 128 */
215 next_bit
= bit
<< 1; /* at most 256 */
216 mask
= next_bit
- 1; /* at most 255 */
218 if (BN_is_negative(scalar
)) {
222 if (scalar
->d
== NULL
|| scalar
->top
== 0) {
223 ECerr(EC_F_COMPUTE_WNAF
, ERR_R_INTERNAL_ERROR
);
227 len
= BN_num_bits(scalar
);
228 r
= OPENSSL_malloc(len
+ 1); /* modified wNAF may be one digit longer
229 * than binary representation (*ret_len will
230 * be set to the actual length, i.e. at most
231 * BN_num_bits(scalar) + 1) */
233 ECerr(EC_F_COMPUTE_WNAF
, ERR_R_MALLOC_FAILURE
);
236 window_val
= scalar
->d
[0] & mask
;
238 while ((window_val
!= 0) || (j
+ w
+ 1 < len
)) { /* if j+w+1 >= len,
239 * window_val will not
243 /* 0 <= window_val <= 2^(w+1) */
245 if (window_val
& 1) {
246 /* 0 < window_val < 2^(w+1) */
248 if (window_val
& bit
) {
249 digit
= window_val
- next_bit
; /* -2^w < digit < 0 */
251 #if 1 /* modified wNAF */
252 if (j
+ w
+ 1 >= len
) {
254 * special case for generating modified wNAFs: no new
255 * bits will be added into window_val, so using a
256 * positive digit here will decrease the total length of
260 digit
= window_val
& (mask
>> 1); /* 0 < digit < 2^w */
264 digit
= window_val
; /* 0 < digit < 2^w */
267 if (digit
<= -bit
|| digit
>= bit
|| !(digit
& 1)) {
268 ECerr(EC_F_COMPUTE_WNAF
, ERR_R_INTERNAL_ERROR
);
275 * now window_val is 0 or 2^(w+1) in standard wNAF generation;
276 * for modified window NAFs, it may also be 2^w
278 if (window_val
!= 0 && window_val
!= next_bit
279 && window_val
!= bit
) {
280 ECerr(EC_F_COMPUTE_WNAF
, ERR_R_INTERNAL_ERROR
);
285 r
[j
++] = sign
* digit
;
288 window_val
+= bit
* BN_is_bit_set(scalar
, j
+ w
);
290 if (window_val
> next_bit
) {
291 ECerr(EC_F_COMPUTE_WNAF
, ERR_R_INTERNAL_ERROR
);
297 ECerr(EC_F_COMPUTE_WNAF
, ERR_R_INTERNAL_ERROR
);
313 #define EC_POINT_BN_set_flags(P, flags) do { \
314 BN_set_flags(&(P)->X, (flags)); \
315 BN_set_flags(&(P)->Y, (flags)); \
316 BN_set_flags(&(P)->Z, (flags)); \
320 * This functions computes (in constant time) a point multiplication over the
323 * At a high level, it is Montgomery ladder with conditional swaps.
325 * It performs either a fixed scalar point multiplication
326 * (scalar * generator)
327 * when point is NULL, or a generic scalar point multiplication
329 * when point is not NULL.
331 * scalar should be in the range [0,n) otherwise all constant time bets are off.
333 * NB: This says nothing about EC_POINT_add and EC_POINT_dbl,
334 * which of course are not constant time themselves.
336 * The product is stored in r.
338 * Returns 1 on success, 0 otherwise.
340 static int ec_mul_consttime(const EC_GROUP
*group
, EC_POINT
*r
,
341 const BIGNUM
*scalar
, const EC_POINT
*point
,
344 int i
, cardinality_bits
, group_top
, kbit
, pbit
, Z_is_one
;
347 BIGNUM
*lambda
= NULL
;
348 BIGNUM
*cardinality
= NULL
;
349 BN_CTX
*new_ctx
= NULL
;
352 if (ctx
== NULL
&& (ctx
= new_ctx
= BN_CTX_new()) == NULL
)
357 s
= EC_POINT_new(group
);
362 if (!EC_POINT_copy(s
, group
->generator
))
365 if (!EC_POINT_copy(s
, point
))
369 EC_POINT_BN_set_flags(s
, BN_FLG_CONSTTIME
);
371 cardinality
= BN_CTX_get(ctx
);
372 lambda
= BN_CTX_get(ctx
);
374 if (k
== NULL
|| !BN_mul(cardinality
, &group
->order
, &group
->cofactor
, ctx
))
378 * Group cardinalities are often on a word boundary.
379 * So when we pad the scalar, some timing diff might
380 * pop if it needs to be expanded due to carries.
381 * So expand ahead of time.
383 cardinality_bits
= BN_num_bits(cardinality
);
384 group_top
= cardinality
->top
;
385 if ((bn_wexpand(k
, group_top
+ 2) == NULL
)
386 || (bn_wexpand(lambda
, group_top
+ 2) == NULL
))
389 if (!BN_copy(k
, scalar
))
392 BN_set_flags(k
, BN_FLG_CONSTTIME
);
394 if ((BN_num_bits(k
) > cardinality_bits
) || (BN_is_negative(k
))) {
396 * this is an unusual input, and we don't guarantee
399 if (!BN_nnmod(k
, k
, cardinality
, ctx
))
403 if (!BN_add(lambda
, k
, cardinality
))
405 BN_set_flags(lambda
, BN_FLG_CONSTTIME
);
406 if (!BN_add(k
, lambda
, cardinality
))
409 * lambda := scalar + cardinality
410 * k := scalar + 2*cardinality
412 kbit
= BN_is_bit_set(lambda
, cardinality_bits
);
413 BN_consttime_swap(kbit
, k
, lambda
, group_top
+ 2);
415 group_top
= group
->field
.top
;
416 if ((bn_wexpand(&s
->X
, group_top
) == NULL
)
417 || (bn_wexpand(&s
->Y
, group_top
) == NULL
)
418 || (bn_wexpand(&s
->Z
, group_top
) == NULL
)
419 || (bn_wexpand(&r
->X
, group_top
) == NULL
)
420 || (bn_wexpand(&r
->Y
, group_top
) == NULL
)
421 || (bn_wexpand(&r
->Z
, group_top
) == NULL
))
424 /* top bit is a 1, in a fixed pos */
425 if (!EC_POINT_copy(r
, s
))
428 EC_POINT_BN_set_flags(r
, BN_FLG_CONSTTIME
);
430 if (!EC_POINT_dbl(group
, s
, s
, ctx
))
435 #define EC_POINT_CSWAP(c, a, b, w, t) do { \
436 BN_consttime_swap(c, &(a)->X, &(b)->X, w); \
437 BN_consttime_swap(c, &(a)->Y, &(b)->Y, w); \
438 BN_consttime_swap(c, &(a)->Z, &(b)->Z, w); \
439 t = ((a)->Z_is_one ^ (b)->Z_is_one) & (c); \
440 (a)->Z_is_one ^= (t); \
441 (b)->Z_is_one ^= (t); \
445 * The ladder step, with branches, is
447 * k[i] == 0: S = add(R, S), R = dbl(R)
448 * k[i] == 1: R = add(S, R), S = dbl(S)
450 * Swapping R, S conditionally on k[i] leaves you with state
452 * k[i] == 0: T, U = R, S
453 * k[i] == 1: T, U = S, R
455 * Then perform the ECC ops.
460 * Which leaves you with state
462 * k[i] == 0: U = add(R, S), T = dbl(R)
463 * k[i] == 1: U = add(S, R), T = dbl(S)
465 * Swapping T, U conditionally on k[i] leaves you with state
467 * k[i] == 0: R, S = T, U
468 * k[i] == 1: R, S = U, T
470 * Which leaves you with state
472 * k[i] == 0: S = add(R, S), R = dbl(R)
473 * k[i] == 1: R = add(S, R), S = dbl(S)
475 * So we get the same logic, but instead of a branch it's a
476 * conditional swap, followed by ECC ops, then another conditional swap.
478 * Optimization: The end of iteration i and start of i-1 looks like
485 * CSWAP(k[i-1], R, S)
487 * CSWAP(k[i-1], R, S)
490 * So instead of two contiguous swaps, you can merge the condition
491 * bits and do a single swap.
493 * k[i] k[i-1] Outcome
499 * This is XOR. pbit tracks the previous bit of k.
502 for (i
= cardinality_bits
- 1; i
>= 0; i
--) {
503 kbit
= BN_is_bit_set(k
, i
) ^ pbit
;
504 EC_POINT_CSWAP(kbit
, r
, s
, group_top
, Z_is_one
);
505 if (!EC_POINT_add(group
, s
, r
, s
, ctx
))
507 if (!EC_POINT_dbl(group
, r
, r
, ctx
))
510 * pbit logic merges this cswap with that of the
515 /* one final cswap to move the right value into r */
516 EC_POINT_CSWAP(pbit
, r
, s
, group_top
, Z_is_one
);
517 #undef EC_POINT_CSWAP
522 EC_POINT_clear_free(s
);
524 BN_CTX_free(new_ctx
);
529 #undef EC_POINT_BN_set_flags
532 * TODO: table should be optimised for the wNAF-based implementation,
533 * sometimes smaller windows will give better performance (thus the
534 * boundaries should be increased)
536 #define EC_window_bits_for_scalar_size(b) \
547 * \sum scalars[i]*points[i],
550 * in the addition if scalar != NULL
552 int ec_wNAF_mul(const EC_GROUP
*group
, EC_POINT
*r
, const BIGNUM
*scalar
,
553 size_t num
, const EC_POINT
*points
[], const BIGNUM
*scalars
[],
556 BN_CTX
*new_ctx
= NULL
;
557 const EC_POINT
*generator
= NULL
;
558 EC_POINT
*tmp
= NULL
;
560 size_t blocksize
= 0, numblocks
= 0; /* for wNAF splitting */
561 size_t pre_points_per_block
= 0;
564 int r_is_inverted
= 0;
565 int r_is_at_infinity
= 1;
566 size_t *wsize
= NULL
; /* individual window sizes */
567 signed char **wNAF
= NULL
; /* individual wNAFs */
568 size_t *wNAF_len
= NULL
;
571 EC_POINT
**val
= NULL
; /* precomputation */
573 EC_POINT
***val_sub
= NULL
; /* pointers to sub-arrays of 'val' or
574 * 'pre_comp->points' */
575 const EC_PRE_COMP
*pre_comp
= NULL
;
576 int num_scalar
= 0; /* flag: will be set to 1 if 'scalar' must be
577 * treated like other scalars, i.e.
578 * precomputation is not available */
581 if (group
->meth
!= r
->meth
) {
582 ECerr(EC_F_EC_WNAF_MUL
, EC_R_INCOMPATIBLE_OBJECTS
);
586 if ((scalar
== NULL
) && (num
== 0)) {
587 return EC_POINT_set_to_infinity(group
, r
);
590 if (!BN_is_zero(&group
->order
) && !BN_is_zero(&group
->cofactor
)) {
592 * Handle the common cases where the scalar is secret, enforcing a constant
593 * time scalar multiplication algorithm.
595 if ((scalar
!= NULL
) && (num
== 0)) {
597 * In this case we want to compute scalar * GeneratorPoint: this
598 * codepath is reached most prominently by (ephemeral) key generation
599 * of EC cryptosystems (i.e. ECDSA keygen and sign setup, ECDH
600 * keygen/first half), where the scalar is always secret. This is why
601 * we ignore if BN_FLG_CONSTTIME is actually set and we always call the
602 * constant time version.
604 return ec_mul_consttime(group
, r
, scalar
, NULL
, ctx
);
606 if ((scalar
== NULL
) && (num
== 1)) {
608 * In this case we want to compute scalar * GenericPoint: this codepath
609 * is reached most prominently by the second half of ECDH, where the
610 * secret scalar is multiplied by the peer's public point. To protect
611 * the secret scalar, we ignore if BN_FLG_CONSTTIME is actually set and
612 * we always call the constant time version.
614 return ec_mul_consttime(group
, r
, scalars
[0], points
[0], ctx
);
618 for (i
= 0; i
< num
; i
++) {
619 if (group
->meth
!= points
[i
]->meth
) {
620 ECerr(EC_F_EC_WNAF_MUL
, EC_R_INCOMPATIBLE_OBJECTS
);
626 ctx
= new_ctx
= BN_CTX_new();
631 if (scalar
!= NULL
) {
632 generator
= EC_GROUP_get0_generator(group
);
633 if (generator
== NULL
) {
634 ECerr(EC_F_EC_WNAF_MUL
, EC_R_UNDEFINED_GENERATOR
);
638 /* look if we can use precomputed multiples of generator */
641 EC_EX_DATA_get_data(group
->extra_data
, ec_pre_comp_dup
,
642 ec_pre_comp_free
, ec_pre_comp_clear_free
);
644 if (pre_comp
&& pre_comp
->numblocks
645 && (EC_POINT_cmp(group
, generator
, pre_comp
->points
[0], ctx
) ==
647 blocksize
= pre_comp
->blocksize
;
650 * determine maximum number of blocks that wNAF splitting may
651 * yield (NB: maximum wNAF length is bit length plus one)
653 numblocks
= (BN_num_bits(scalar
) / blocksize
) + 1;
656 * we cannot use more blocks than we have precomputation for
658 if (numblocks
> pre_comp
->numblocks
)
659 numblocks
= pre_comp
->numblocks
;
661 pre_points_per_block
= (size_t)1 << (pre_comp
->w
- 1);
663 /* check that pre_comp looks sane */
664 if (pre_comp
->num
!= (pre_comp
->numblocks
* pre_points_per_block
)) {
665 ECerr(EC_F_EC_WNAF_MUL
, ERR_R_INTERNAL_ERROR
);
669 /* can't use precomputation */
672 num_scalar
= 1; /* treat 'scalar' like 'num'-th element of
677 totalnum
= num
+ numblocks
;
679 wsize
= OPENSSL_malloc(totalnum
* sizeof(wsize
[0]));
680 wNAF_len
= OPENSSL_malloc(totalnum
* sizeof(wNAF_len
[0]));
681 /* include space for pivot */
682 wNAF
= OPENSSL_malloc((totalnum
+ 1) * sizeof(wNAF
[0]));
683 val_sub
= OPENSSL_malloc(totalnum
* sizeof(val_sub
[0]));
685 /* Ensure wNAF is initialised in case we end up going to err */
687 wNAF
[0] = NULL
; /* preliminary pivot */
689 if (!wsize
|| !wNAF_len
|| !wNAF
|| !val_sub
) {
690 ECerr(EC_F_EC_WNAF_MUL
, ERR_R_MALLOC_FAILURE
);
695 * num_val will be the total number of temporarily precomputed points
699 for (i
= 0; i
< num
+ num_scalar
; i
++) {
702 bits
= i
< num
? BN_num_bits(scalars
[i
]) : BN_num_bits(scalar
);
703 wsize
[i
] = EC_window_bits_for_scalar_size(bits
);
704 num_val
+= (size_t)1 << (wsize
[i
] - 1);
705 wNAF
[i
+ 1] = NULL
; /* make sure we always have a pivot */
707 compute_wNAF((i
< num
? scalars
[i
] : scalar
), wsize
[i
],
711 if (wNAF_len
[i
] > max_len
)
712 max_len
= wNAF_len
[i
];
716 /* we go here iff scalar != NULL */
718 if (pre_comp
== NULL
) {
719 if (num_scalar
!= 1) {
720 ECerr(EC_F_EC_WNAF_MUL
, ERR_R_INTERNAL_ERROR
);
723 /* we have already generated a wNAF for 'scalar' */
725 signed char *tmp_wNAF
= NULL
;
728 if (num_scalar
!= 0) {
729 ECerr(EC_F_EC_WNAF_MUL
, ERR_R_INTERNAL_ERROR
);
734 * use the window size for which we have precomputation
736 wsize
[num
] = pre_comp
->w
;
737 tmp_wNAF
= compute_wNAF(scalar
, wsize
[num
], &tmp_len
);
741 if (tmp_len
<= max_len
) {
743 * One of the other wNAFs is at least as long as the wNAF
744 * belonging to the generator, so wNAF splitting will not buy
749 totalnum
= num
+ 1; /* don't use wNAF splitting */
750 wNAF
[num
] = tmp_wNAF
;
751 wNAF
[num
+ 1] = NULL
;
752 wNAF_len
[num
] = tmp_len
;
753 if (tmp_len
> max_len
)
756 * pre_comp->points starts with the points that we need here:
758 val_sub
[num
] = pre_comp
->points
;
761 * don't include tmp_wNAF directly into wNAF array - use wNAF
762 * splitting and include the blocks
766 EC_POINT
**tmp_points
;
768 if (tmp_len
< numblocks
* blocksize
) {
770 * possibly we can do with fewer blocks than estimated
772 numblocks
= (tmp_len
+ blocksize
- 1) / blocksize
;
773 if (numblocks
> pre_comp
->numblocks
) {
774 ECerr(EC_F_EC_WNAF_MUL
, ERR_R_INTERNAL_ERROR
);
777 totalnum
= num
+ numblocks
;
780 /* split wNAF in 'numblocks' parts */
782 tmp_points
= pre_comp
->points
;
784 for (i
= num
; i
< totalnum
; i
++) {
785 if (i
< totalnum
- 1) {
786 wNAF_len
[i
] = blocksize
;
787 if (tmp_len
< blocksize
) {
788 ECerr(EC_F_EC_WNAF_MUL
, ERR_R_INTERNAL_ERROR
);
791 tmp_len
-= blocksize
;
794 * last block gets whatever is left (this could be
795 * more or less than 'blocksize'!)
797 wNAF_len
[i
] = tmp_len
;
800 wNAF
[i
] = OPENSSL_malloc(wNAF_len
[i
]);
801 if (wNAF
[i
] == NULL
) {
802 ECerr(EC_F_EC_WNAF_MUL
, ERR_R_MALLOC_FAILURE
);
803 OPENSSL_free(tmp_wNAF
);
806 memcpy(wNAF
[i
], pp
, wNAF_len
[i
]);
807 if (wNAF_len
[i
] > max_len
)
808 max_len
= wNAF_len
[i
];
810 if (*tmp_points
== NULL
) {
811 ECerr(EC_F_EC_WNAF_MUL
, ERR_R_INTERNAL_ERROR
);
812 OPENSSL_free(tmp_wNAF
);
815 val_sub
[i
] = tmp_points
;
816 tmp_points
+= pre_points_per_block
;
819 OPENSSL_free(tmp_wNAF
);
825 * All points we precompute now go into a single array 'val'.
826 * 'val_sub[i]' is a pointer to the subarray for the i-th point, or to a
827 * subarray of 'pre_comp->points' if we already have precomputation.
829 val
= OPENSSL_malloc((num_val
+ 1) * sizeof(val
[0]));
831 ECerr(EC_F_EC_WNAF_MUL
, ERR_R_MALLOC_FAILURE
);
834 val
[num_val
] = NULL
; /* pivot element */
836 /* allocate points for precomputation */
838 for (i
= 0; i
< num
+ num_scalar
; i
++) {
840 for (j
= 0; j
< ((size_t)1 << (wsize
[i
] - 1)); j
++) {
841 *v
= EC_POINT_new(group
);
847 if (!(v
== val
+ num_val
)) {
848 ECerr(EC_F_EC_WNAF_MUL
, ERR_R_INTERNAL_ERROR
);
852 if (!(tmp
= EC_POINT_new(group
)))
856 * prepare precomputed values:
857 * val_sub[i][0] := points[i]
858 * val_sub[i][1] := 3 * points[i]
859 * val_sub[i][2] := 5 * points[i]
862 for (i
= 0; i
< num
+ num_scalar
; i
++) {
864 if (!EC_POINT_copy(val_sub
[i
][0], points
[i
]))
867 if (!EC_POINT_copy(val_sub
[i
][0], generator
))
872 if (!EC_POINT_dbl(group
, tmp
, val_sub
[i
][0], ctx
))
874 for (j
= 1; j
< ((size_t)1 << (wsize
[i
] - 1)); j
++) {
876 (group
, val_sub
[i
][j
], val_sub
[i
][j
- 1], tmp
, ctx
))
882 #if 1 /* optional; EC_window_bits_for_scalar_size
883 * assumes we do this step */
884 if (!EC_POINTs_make_affine(group
, num_val
, val
, ctx
))
888 r_is_at_infinity
= 1;
890 for (k
= max_len
- 1; k
>= 0; k
--) {
891 if (!r_is_at_infinity
) {
892 if (!EC_POINT_dbl(group
, r
, r
, ctx
))
896 for (i
= 0; i
< totalnum
; i
++) {
897 if (wNAF_len
[i
] > (size_t)k
) {
898 int digit
= wNAF
[i
][k
];
907 if (is_neg
!= r_is_inverted
) {
908 if (!r_is_at_infinity
) {
909 if (!EC_POINT_invert(group
, r
, ctx
))
912 r_is_inverted
= !r_is_inverted
;
917 if (r_is_at_infinity
) {
918 if (!EC_POINT_copy(r
, val_sub
[i
][digit
>> 1]))
920 r_is_at_infinity
= 0;
923 (group
, r
, r
, val_sub
[i
][digit
>> 1], ctx
))
931 if (r_is_at_infinity
) {
932 if (!EC_POINT_set_to_infinity(group
, r
))
936 if (!EC_POINT_invert(group
, r
, ctx
))
944 BN_CTX_free(new_ctx
);
949 if (wNAF_len
!= NULL
)
950 OPENSSL_free(wNAF_len
);
954 for (w
= wNAF
; *w
!= NULL
; w
++)
960 for (v
= val
; *v
!= NULL
; v
++)
961 EC_POINT_clear_free(*v
);
965 if (val_sub
!= NULL
) {
966 OPENSSL_free(val_sub
);
972 * ec_wNAF_precompute_mult()
973 * creates an EC_PRE_COMP object with preprecomputed multiples of the generator
974 * for use with wNAF splitting as implemented in ec_wNAF_mul().
976 * 'pre_comp->points' is an array of multiples of the generator
977 * of the following form:
978 * points[0] = generator;
979 * points[1] = 3 * generator;
981 * points[2^(w-1)-1] = (2^(w-1)-1) * generator;
982 * points[2^(w-1)] = 2^blocksize * generator;
983 * points[2^(w-1)+1] = 3 * 2^blocksize * generator;
985 * points[2^(w-1)*(numblocks-1)-1] = (2^(w-1)) * 2^(blocksize*(numblocks-2)) * generator
986 * points[2^(w-1)*(numblocks-1)] = 2^(blocksize*(numblocks-1)) * generator
988 * points[2^(w-1)*numblocks-1] = (2^(w-1)) * 2^(blocksize*(numblocks-1)) * generator
989 * points[2^(w-1)*numblocks] = NULL
991 int ec_wNAF_precompute_mult(EC_GROUP
*group
, BN_CTX
*ctx
)
993 const EC_POINT
*generator
;
994 EC_POINT
*tmp_point
= NULL
, *base
= NULL
, **var
;
995 BN_CTX
*new_ctx
= NULL
;
997 size_t i
, bits
, w
, pre_points_per_block
, blocksize
, numblocks
, num
;
998 EC_POINT
**points
= NULL
;
999 EC_PRE_COMP
*pre_comp
;
1002 /* if there is an old EC_PRE_COMP object, throw it away */
1003 EC_EX_DATA_free_data(&group
->extra_data
, ec_pre_comp_dup
,
1004 ec_pre_comp_free
, ec_pre_comp_clear_free
);
1006 if ((pre_comp
= ec_pre_comp_new(group
)) == NULL
)
1009 generator
= EC_GROUP_get0_generator(group
);
1010 if (generator
== NULL
) {
1011 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT
, EC_R_UNDEFINED_GENERATOR
);
1016 ctx
= new_ctx
= BN_CTX_new();
1022 order
= BN_CTX_get(ctx
);
1026 if (!EC_GROUP_get_order(group
, order
, ctx
))
1028 if (BN_is_zero(order
)) {
1029 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT
, EC_R_UNKNOWN_ORDER
);
1033 bits
= BN_num_bits(order
);
1035 * The following parameters mean we precompute (approximately) one point
1036 * per bit. TBD: The combination 8, 4 is perfect for 160 bits; for other
1037 * bit lengths, other parameter combinations might provide better
1042 if (EC_window_bits_for_scalar_size(bits
) > w
) {
1043 /* let's not make the window too small ... */
1044 w
= EC_window_bits_for_scalar_size(bits
);
1047 numblocks
= (bits
+ blocksize
- 1) / blocksize
; /* max. number of blocks
1051 pre_points_per_block
= (size_t)1 << (w
- 1);
1052 num
= pre_points_per_block
* numblocks
; /* number of points to compute
1055 points
= OPENSSL_malloc(sizeof(EC_POINT
*) * (num
+ 1));
1057 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT
, ERR_R_MALLOC_FAILURE
);
1062 var
[num
] = NULL
; /* pivot */
1063 for (i
= 0; i
< num
; i
++) {
1064 if ((var
[i
] = EC_POINT_new(group
)) == NULL
) {
1065 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT
, ERR_R_MALLOC_FAILURE
);
1070 if (!(tmp_point
= EC_POINT_new(group
)) || !(base
= EC_POINT_new(group
))) {
1071 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT
, ERR_R_MALLOC_FAILURE
);
1075 if (!EC_POINT_copy(base
, generator
))
1078 /* do the precomputation */
1079 for (i
= 0; i
< numblocks
; i
++) {
1082 if (!EC_POINT_dbl(group
, tmp_point
, base
, ctx
))
1085 if (!EC_POINT_copy(*var
++, base
))
1088 for (j
= 1; j
< pre_points_per_block
; j
++, var
++) {
1090 * calculate odd multiples of the current base point
1092 if (!EC_POINT_add(group
, *var
, tmp_point
, *(var
- 1), ctx
))
1096 if (i
< numblocks
- 1) {
1098 * get the next base (multiply current one by 2^blocksize)
1102 if (blocksize
<= 2) {
1103 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT
, ERR_R_INTERNAL_ERROR
);
1107 if (!EC_POINT_dbl(group
, base
, tmp_point
, ctx
))
1109 for (k
= 2; k
< blocksize
; k
++) {
1110 if (!EC_POINT_dbl(group
, base
, base
, ctx
))
1116 if (!EC_POINTs_make_affine(group
, num
, points
, ctx
))
1119 pre_comp
->group
= group
;
1120 pre_comp
->blocksize
= blocksize
;
1121 pre_comp
->numblocks
= numblocks
;
1123 pre_comp
->points
= points
;
1125 pre_comp
->num
= num
;
1127 if (!EC_EX_DATA_set_data(&group
->extra_data
, pre_comp
,
1128 ec_pre_comp_dup
, ec_pre_comp_free
,
1129 ec_pre_comp_clear_free
))
1137 if (new_ctx
!= NULL
)
1138 BN_CTX_free(new_ctx
);
1140 ec_pre_comp_free(pre_comp
);
1144 for (p
= points
; *p
!= NULL
; p
++)
1146 OPENSSL_free(points
);
1149 EC_POINT_free(tmp_point
);
1151 EC_POINT_free(base
);
1155 int ec_wNAF_have_precompute_mult(const EC_GROUP
*group
)
1157 if (EC_EX_DATA_get_data
1158 (group
->extra_data
, ec_pre_comp_dup
, ec_pre_comp_free
,
1159 ec_pre_comp_clear_free
) != NULL
)