2 * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
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
12 #include <openssl/err.h>
14 #include "internal/cryptlib.h"
15 #include "internal/bn_int.h"
17 #include "internal/refcount.h"
20 * This file implements the wNAF-based interleaving multi-exponentiation method
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
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
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
;
45 static EC_PRE_COMP
*ec_pre_comp_new(const EC_GROUP
*group
)
47 EC_PRE_COMP
*ret
= NULL
;
52 ret
= OPENSSL_zalloc(sizeof(*ret
));
54 ECerr(EC_F_EC_PRE_COMP_NEW
, ERR_R_MALLOC_FAILURE
);
59 ret
->blocksize
= 8; /* default */
60 ret
->w
= 4; /* default */
63 ret
->lock
= CRYPTO_THREAD_lock_new();
64 if (ret
->lock
== NULL
) {
65 ECerr(EC_F_EC_PRE_COMP_NEW
, ERR_R_MALLOC_FAILURE
);
72 EC_PRE_COMP
*EC_ec_pre_comp_dup(EC_PRE_COMP
*pre
)
76 CRYPTO_UP_REF(&pre
->references
, &i
, pre
->lock
);
80 void EC_ec_pre_comp_free(EC_PRE_COMP
*pre
)
87 CRYPTO_DOWN_REF(&pre
->references
, &i
, pre
->lock
);
88 REF_PRINT_COUNT("EC_ec", pre
);
91 REF_ASSERT_ISNT(i
< 0);
93 if (pre
->points
!= NULL
) {
96 for (pts
= pre
->points
; *pts
!= NULL
; pts
++)
98 OPENSSL_free(pre
->points
);
100 CRYPTO_THREAD_lock_free(pre
->lock
);
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)); \
111 * This functions computes (in constant time) a point multiplication over the
114 * At a high level, it is Montgomery ladder with conditional swaps.
116 * It performs either a fixed scalar point multiplication
117 * (scalar * generator)
118 * when point is NULL, or a generic scalar point multiplication
120 * when point is not NULL.
122 * scalar should be in the range [0,n) otherwise all constant time bets are off.
124 * NB: This says nothing about EC_POINT_add and EC_POINT_dbl,
125 * which of course are not constant time themselves.
127 * The product is stored in r.
129 * Returns 1 on success, 0 otherwise.
131 static int ec_mul_consttime(const EC_GROUP
*group
, EC_POINT
*r
,
132 const BIGNUM
*scalar
, const EC_POINT
*point
,
135 int i
, order_bits
, group_top
, kbit
, pbit
, Z_is_one
;
138 BIGNUM
*lambda
= NULL
;
139 BN_CTX
*new_ctx
= NULL
;
142 if (ctx
== NULL
&& (ctx
= new_ctx
= BN_CTX_secure_new()) == NULL
)
147 order_bits
= BN_num_bits(group
->order
);
149 s
= EC_POINT_new(group
);
154 if (!EC_POINT_copy(s
, group
->generator
))
157 if (!EC_POINT_copy(s
, point
))
161 EC_POINT_BN_set_flags(s
, BN_FLG_CONSTTIME
);
163 lambda
= BN_CTX_get(ctx
);
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.
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
))
179 if (!BN_copy(k
, scalar
))
182 BN_set_flags(k
, BN_FLG_CONSTTIME
);
184 if ((BN_num_bits(k
) > order_bits
) || (BN_is_negative(k
))) {
186 * this is an unusual input, and we don't guarantee
189 if (!BN_nnmod(k
, k
, group
->order
, ctx
))
193 if (!BN_add(lambda
, k
, group
->order
))
195 BN_set_flags(lambda
, BN_FLG_CONSTTIME
);
196 if (!BN_add(k
, lambda
, group
->order
))
199 * lambda := scalar + order
200 * k := scalar + 2*order
202 kbit
= BN_is_bit_set(lambda
, order_bits
);
203 BN_consttime_swap(kbit
, k
, lambda
, group_top
+ 1);
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
))
214 /* top bit is a 1, in a fixed pos */
215 if (!EC_POINT_copy(r
, s
))
218 EC_POINT_BN_set_flags(r
, BN_FLG_CONSTTIME
);
220 if (!EC_POINT_dbl(group
, s
, s
, ctx
))
225 #define EC_POINT_CSWAP(c, a, b, w, t) do { \
226 BN_consttime_swap(c, (a)->X, (b)->X, w); \
227 BN_consttime_swap(c, (a)->Y, (b)->Y, w); \
228 BN_consttime_swap(c, (a)->Z, (b)->Z, w); \
229 t = ((a)->Z_is_one ^ (b)->Z_is_one) & (c); \
230 (a)->Z_is_one ^= (t); \
231 (b)->Z_is_one ^= (t); \
235 * The ladder step, with branches, is
237 * k[i] == 0: S = add(R, S), R = dbl(R)
238 * k[i] == 1: R = add(S, R), S = dbl(S)
240 * Swapping R, S conditionally on k[i] leaves you with state
242 * k[i] == 0: T, U = R, S
243 * k[i] == 1: T, U = S, R
245 * Then perform the ECC ops.
250 * Which leaves you with state
252 * k[i] == 0: U = add(R, S), T = dbl(R)
253 * k[i] == 1: U = add(S, R), T = dbl(S)
255 * Swapping T, U conditionally on k[i] leaves you with state
257 * k[i] == 0: R, S = T, U
258 * k[i] == 1: R, S = U, T
260 * Which leaves you with state
262 * k[i] == 0: S = add(R, S), R = dbl(R)
263 * k[i] == 1: R = add(S, R), S = dbl(S)
265 * So we get the same logic, but instead of a branch it's a
266 * conditional swap, followed by ECC ops, then another conditional swap.
268 * Optimization: The end of iteration i and start of i-1 looks like
275 * CSWAP(k[i-1], R, S)
277 * CSWAP(k[i-1], R, S)
280 * So instead of two contiguous swaps, you can merge the condition
281 * bits and do a single swap.
283 * k[i] k[i-1] Outcome
289 * This is XOR. pbit tracks the previous bit of k.
292 for (i
= order_bits
- 1; i
>= 0; i
--) {
293 kbit
= BN_is_bit_set(k
, i
) ^ pbit
;
294 EC_POINT_CSWAP(kbit
, r
, s
, group_top
, Z_is_one
);
295 if (!EC_POINT_add(group
, s
, r
, s
, ctx
))
297 if (!EC_POINT_dbl(group
, r
, r
, ctx
))
300 * pbit logic merges this cswap with that of the
305 /* one final cswap to move the right value into r */
306 EC_POINT_CSWAP(pbit
, r
, s
, group_top
, Z_is_one
);
307 #undef EC_POINT_CSWAP
314 BN_CTX_free(new_ctx
);
319 #undef EC_POINT_BN_set_flags
322 * TODO: table should be optimised for the wNAF-based implementation,
323 * sometimes smaller windows will give better performance (thus the
324 * boundaries should be increased)
326 #define EC_window_bits_for_scalar_size(b) \
337 * \sum scalars[i]*points[i],
340 * in the addition if scalar != NULL
342 int ec_wNAF_mul(const EC_GROUP
*group
, EC_POINT
*r
, const BIGNUM
*scalar
,
343 size_t num
, const EC_POINT
*points
[], const BIGNUM
*scalars
[],
346 BN_CTX
*new_ctx
= NULL
;
347 const EC_POINT
*generator
= NULL
;
348 EC_POINT
*tmp
= NULL
;
350 size_t blocksize
= 0, numblocks
= 0; /* for wNAF splitting */
351 size_t pre_points_per_block
= 0;
354 int r_is_inverted
= 0;
355 int r_is_at_infinity
= 1;
356 size_t *wsize
= NULL
; /* individual window sizes */
357 signed char **wNAF
= NULL
; /* individual wNAFs */
358 size_t *wNAF_len
= NULL
;
361 EC_POINT
**val
= NULL
; /* precomputation */
363 EC_POINT
***val_sub
= NULL
; /* pointers to sub-arrays of 'val' or
364 * 'pre_comp->points' */
365 const EC_PRE_COMP
*pre_comp
= NULL
;
366 int num_scalar
= 0; /* flag: will be set to 1 if 'scalar' must be
367 * treated like other scalars, i.e.
368 * precomputation is not available */
371 if (group
->meth
!= r
->meth
) {
372 ECerr(EC_F_EC_WNAF_MUL
, EC_R_INCOMPATIBLE_OBJECTS
);
376 if ((scalar
== NULL
) && (num
== 0)) {
377 return EC_POINT_set_to_infinity(group
, r
);
381 * Handle the common cases where the scalar is secret, enforcing a constant
382 * time scalar multiplication algorithm.
384 if ((scalar
!= NULL
) && (num
== 0)) {
386 * In this case we want to compute scalar * GeneratorPoint: this
387 * codepath is reached most prominently by (ephemeral) key generation
388 * of EC cryptosystems (i.e. ECDSA keygen and sign setup, ECDH
389 * keygen/first half), where the scalar is always secret. This is why
390 * we ignore if BN_FLG_CONSTTIME is actually set and we always call the
391 * constant time version.
393 return ec_mul_consttime(group
, r
, scalar
, NULL
, ctx
);
395 if ((scalar
== NULL
) && (num
== 1)) {
397 * In this case we want to compute scalar * GenericPoint: this codepath
398 * is reached most prominently by the second half of ECDH, where the
399 * secret scalar is multiplied by the peer's public point. To protect
400 * the secret scalar, we ignore if BN_FLG_CONSTTIME is actually set and
401 * we always call the constant time version.
403 return ec_mul_consttime(group
, r
, scalars
[0], points
[0], ctx
);
406 for (i
= 0; i
< num
; i
++) {
407 if (group
->meth
!= points
[i
]->meth
) {
408 ECerr(EC_F_EC_WNAF_MUL
, EC_R_INCOMPATIBLE_OBJECTS
);
414 ctx
= new_ctx
= BN_CTX_new();
419 if (scalar
!= NULL
) {
420 generator
= EC_GROUP_get0_generator(group
);
421 if (generator
== NULL
) {
422 ECerr(EC_F_EC_WNAF_MUL
, EC_R_UNDEFINED_GENERATOR
);
426 /* look if we can use precomputed multiples of generator */
428 pre_comp
= group
->pre_comp
.ec
;
429 if (pre_comp
&& pre_comp
->numblocks
430 && (EC_POINT_cmp(group
, generator
, pre_comp
->points
[0], ctx
) ==
432 blocksize
= pre_comp
->blocksize
;
435 * determine maximum number of blocks that wNAF splitting may
436 * yield (NB: maximum wNAF length is bit length plus one)
438 numblocks
= (BN_num_bits(scalar
) / blocksize
) + 1;
441 * we cannot use more blocks than we have precomputation for
443 if (numblocks
> pre_comp
->numblocks
)
444 numblocks
= pre_comp
->numblocks
;
446 pre_points_per_block
= (size_t)1 << (pre_comp
->w
- 1);
448 /* check that pre_comp looks sane */
449 if (pre_comp
->num
!= (pre_comp
->numblocks
* pre_points_per_block
)) {
450 ECerr(EC_F_EC_WNAF_MUL
, ERR_R_INTERNAL_ERROR
);
454 /* can't use precomputation */
457 num_scalar
= 1; /* treat 'scalar' like 'num'-th element of
462 totalnum
= num
+ numblocks
;
464 wsize
= OPENSSL_malloc(totalnum
* sizeof(wsize
[0]));
465 wNAF_len
= OPENSSL_malloc(totalnum
* sizeof(wNAF_len
[0]));
466 /* include space for pivot */
467 wNAF
= OPENSSL_malloc((totalnum
+ 1) * sizeof(wNAF
[0]));
468 val_sub
= OPENSSL_malloc(totalnum
* sizeof(val_sub
[0]));
470 /* Ensure wNAF is initialised in case we end up going to err */
472 wNAF
[0] = NULL
; /* preliminary pivot */
474 if (wsize
== NULL
|| wNAF_len
== NULL
|| wNAF
== NULL
|| val_sub
== NULL
) {
475 ECerr(EC_F_EC_WNAF_MUL
, ERR_R_MALLOC_FAILURE
);
480 * num_val will be the total number of temporarily precomputed points
484 for (i
= 0; i
< num
+ num_scalar
; i
++) {
487 bits
= i
< num
? BN_num_bits(scalars
[i
]) : BN_num_bits(scalar
);
488 wsize
[i
] = EC_window_bits_for_scalar_size(bits
);
489 num_val
+= (size_t)1 << (wsize
[i
] - 1);
490 wNAF
[i
+ 1] = NULL
; /* make sure we always have a pivot */
492 bn_compute_wNAF((i
< num
? scalars
[i
] : scalar
), wsize
[i
],
496 if (wNAF_len
[i
] > max_len
)
497 max_len
= wNAF_len
[i
];
501 /* we go here iff scalar != NULL */
503 if (pre_comp
== NULL
) {
504 if (num_scalar
!= 1) {
505 ECerr(EC_F_EC_WNAF_MUL
, ERR_R_INTERNAL_ERROR
);
508 /* we have already generated a wNAF for 'scalar' */
510 signed char *tmp_wNAF
= NULL
;
513 if (num_scalar
!= 0) {
514 ECerr(EC_F_EC_WNAF_MUL
, ERR_R_INTERNAL_ERROR
);
519 * use the window size for which we have precomputation
521 wsize
[num
] = pre_comp
->w
;
522 tmp_wNAF
= bn_compute_wNAF(scalar
, wsize
[num
], &tmp_len
);
526 if (tmp_len
<= max_len
) {
528 * One of the other wNAFs is at least as long as the wNAF
529 * belonging to the generator, so wNAF splitting will not buy
534 totalnum
= num
+ 1; /* don't use wNAF splitting */
535 wNAF
[num
] = tmp_wNAF
;
536 wNAF
[num
+ 1] = NULL
;
537 wNAF_len
[num
] = tmp_len
;
539 * pre_comp->points starts with the points that we need here:
541 val_sub
[num
] = pre_comp
->points
;
544 * don't include tmp_wNAF directly into wNAF array - use wNAF
545 * splitting and include the blocks
549 EC_POINT
**tmp_points
;
551 if (tmp_len
< numblocks
* blocksize
) {
553 * possibly we can do with fewer blocks than estimated
555 numblocks
= (tmp_len
+ blocksize
- 1) / blocksize
;
556 if (numblocks
> pre_comp
->numblocks
) {
557 ECerr(EC_F_EC_WNAF_MUL
, ERR_R_INTERNAL_ERROR
);
558 OPENSSL_free(tmp_wNAF
);
561 totalnum
= num
+ numblocks
;
564 /* split wNAF in 'numblocks' parts */
566 tmp_points
= pre_comp
->points
;
568 for (i
= num
; i
< totalnum
; i
++) {
569 if (i
< totalnum
- 1) {
570 wNAF_len
[i
] = blocksize
;
571 if (tmp_len
< blocksize
) {
572 ECerr(EC_F_EC_WNAF_MUL
, ERR_R_INTERNAL_ERROR
);
573 OPENSSL_free(tmp_wNAF
);
576 tmp_len
-= blocksize
;
579 * last block gets whatever is left (this could be
580 * more or less than 'blocksize'!)
582 wNAF_len
[i
] = tmp_len
;
585 wNAF
[i
] = OPENSSL_malloc(wNAF_len
[i
]);
586 if (wNAF
[i
] == NULL
) {
587 ECerr(EC_F_EC_WNAF_MUL
, ERR_R_MALLOC_FAILURE
);
588 OPENSSL_free(tmp_wNAF
);
591 memcpy(wNAF
[i
], pp
, wNAF_len
[i
]);
592 if (wNAF_len
[i
] > max_len
)
593 max_len
= wNAF_len
[i
];
595 if (*tmp_points
== NULL
) {
596 ECerr(EC_F_EC_WNAF_MUL
, ERR_R_INTERNAL_ERROR
);
597 OPENSSL_free(tmp_wNAF
);
600 val_sub
[i
] = tmp_points
;
601 tmp_points
+= pre_points_per_block
;
604 OPENSSL_free(tmp_wNAF
);
610 * All points we precompute now go into a single array 'val'.
611 * 'val_sub[i]' is a pointer to the subarray for the i-th point, or to a
612 * subarray of 'pre_comp->points' if we already have precomputation.
614 val
= OPENSSL_malloc((num_val
+ 1) * sizeof(val
[0]));
616 ECerr(EC_F_EC_WNAF_MUL
, ERR_R_MALLOC_FAILURE
);
619 val
[num_val
] = NULL
; /* pivot element */
621 /* allocate points for precomputation */
623 for (i
= 0; i
< num
+ num_scalar
; i
++) {
625 for (j
= 0; j
< ((size_t)1 << (wsize
[i
] - 1)); j
++) {
626 *v
= EC_POINT_new(group
);
632 if (!(v
== val
+ num_val
)) {
633 ECerr(EC_F_EC_WNAF_MUL
, ERR_R_INTERNAL_ERROR
);
637 if ((tmp
= EC_POINT_new(group
)) == NULL
)
641 * prepare precomputed values:
642 * val_sub[i][0] := points[i]
643 * val_sub[i][1] := 3 * points[i]
644 * val_sub[i][2] := 5 * points[i]
647 for (i
= 0; i
< num
+ num_scalar
; i
++) {
649 if (!EC_POINT_copy(val_sub
[i
][0], points
[i
]))
652 if (!EC_POINT_copy(val_sub
[i
][0], generator
))
657 if (!EC_POINT_dbl(group
, tmp
, val_sub
[i
][0], ctx
))
659 for (j
= 1; j
< ((size_t)1 << (wsize
[i
] - 1)); j
++) {
661 (group
, val_sub
[i
][j
], val_sub
[i
][j
- 1], tmp
, ctx
))
667 if (!EC_POINTs_make_affine(group
, num_val
, val
, ctx
))
670 r_is_at_infinity
= 1;
672 for (k
= max_len
- 1; k
>= 0; k
--) {
673 if (!r_is_at_infinity
) {
674 if (!EC_POINT_dbl(group
, r
, r
, ctx
))
678 for (i
= 0; i
< totalnum
; i
++) {
679 if (wNAF_len
[i
] > (size_t)k
) {
680 int digit
= wNAF
[i
][k
];
689 if (is_neg
!= r_is_inverted
) {
690 if (!r_is_at_infinity
) {
691 if (!EC_POINT_invert(group
, r
, ctx
))
694 r_is_inverted
= !r_is_inverted
;
699 if (r_is_at_infinity
) {
700 if (!EC_POINT_copy(r
, val_sub
[i
][digit
>> 1]))
702 r_is_at_infinity
= 0;
705 (group
, r
, r
, val_sub
[i
][digit
>> 1], ctx
))
713 if (r_is_at_infinity
) {
714 if (!EC_POINT_set_to_infinity(group
, r
))
718 if (!EC_POINT_invert(group
, r
, ctx
))
725 BN_CTX_free(new_ctx
);
728 OPENSSL_free(wNAF_len
);
732 for (w
= wNAF
; *w
!= NULL
; w
++)
738 for (v
= val
; *v
!= NULL
; v
++)
739 EC_POINT_clear_free(*v
);
743 OPENSSL_free(val_sub
);
748 * ec_wNAF_precompute_mult()
749 * creates an EC_PRE_COMP object with preprecomputed multiples of the generator
750 * for use with wNAF splitting as implemented in ec_wNAF_mul().
752 * 'pre_comp->points' is an array of multiples of the generator
753 * of the following form:
754 * points[0] = generator;
755 * points[1] = 3 * generator;
757 * points[2^(w-1)-1] = (2^(w-1)-1) * generator;
758 * points[2^(w-1)] = 2^blocksize * generator;
759 * points[2^(w-1)+1] = 3 * 2^blocksize * generator;
761 * points[2^(w-1)*(numblocks-1)-1] = (2^(w-1)) * 2^(blocksize*(numblocks-2)) * generator
762 * points[2^(w-1)*(numblocks-1)] = 2^(blocksize*(numblocks-1)) * generator
764 * points[2^(w-1)*numblocks-1] = (2^(w-1)) * 2^(blocksize*(numblocks-1)) * generator
765 * points[2^(w-1)*numblocks] = NULL
767 int ec_wNAF_precompute_mult(EC_GROUP
*group
, BN_CTX
*ctx
)
769 const EC_POINT
*generator
;
770 EC_POINT
*tmp_point
= NULL
, *base
= NULL
, **var
;
771 BN_CTX
*new_ctx
= NULL
;
773 size_t i
, bits
, w
, pre_points_per_block
, blocksize
, numblocks
, num
;
774 EC_POINT
**points
= NULL
;
775 EC_PRE_COMP
*pre_comp
;
778 /* if there is an old EC_PRE_COMP object, throw it away */
779 EC_pre_comp_free(group
);
780 if ((pre_comp
= ec_pre_comp_new(group
)) == NULL
)
783 generator
= EC_GROUP_get0_generator(group
);
784 if (generator
== NULL
) {
785 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT
, EC_R_UNDEFINED_GENERATOR
);
790 ctx
= new_ctx
= BN_CTX_new();
797 order
= EC_GROUP_get0_order(group
);
800 if (BN_is_zero(order
)) {
801 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT
, EC_R_UNKNOWN_ORDER
);
805 bits
= BN_num_bits(order
);
807 * The following parameters mean we precompute (approximately) one point
808 * per bit. TBD: The combination 8, 4 is perfect for 160 bits; for other
809 * bit lengths, other parameter combinations might provide better
814 if (EC_window_bits_for_scalar_size(bits
) > w
) {
815 /* let's not make the window too small ... */
816 w
= EC_window_bits_for_scalar_size(bits
);
819 numblocks
= (bits
+ blocksize
- 1) / blocksize
; /* max. number of blocks
823 pre_points_per_block
= (size_t)1 << (w
- 1);
824 num
= pre_points_per_block
* numblocks
; /* number of points to compute
827 points
= OPENSSL_malloc(sizeof(*points
) * (num
+ 1));
828 if (points
== NULL
) {
829 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT
, ERR_R_MALLOC_FAILURE
);
834 var
[num
] = NULL
; /* pivot */
835 for (i
= 0; i
< num
; i
++) {
836 if ((var
[i
] = EC_POINT_new(group
)) == NULL
) {
837 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT
, ERR_R_MALLOC_FAILURE
);
842 if ((tmp_point
= EC_POINT_new(group
)) == NULL
843 || (base
= EC_POINT_new(group
)) == NULL
) {
844 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT
, ERR_R_MALLOC_FAILURE
);
848 if (!EC_POINT_copy(base
, generator
))
851 /* do the precomputation */
852 for (i
= 0; i
< numblocks
; i
++) {
855 if (!EC_POINT_dbl(group
, tmp_point
, base
, ctx
))
858 if (!EC_POINT_copy(*var
++, base
))
861 for (j
= 1; j
< pre_points_per_block
; j
++, var
++) {
863 * calculate odd multiples of the current base point
865 if (!EC_POINT_add(group
, *var
, tmp_point
, *(var
- 1), ctx
))
869 if (i
< numblocks
- 1) {
871 * get the next base (multiply current one by 2^blocksize)
875 if (blocksize
<= 2) {
876 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT
, ERR_R_INTERNAL_ERROR
);
880 if (!EC_POINT_dbl(group
, base
, tmp_point
, ctx
))
882 for (k
= 2; k
< blocksize
; k
++) {
883 if (!EC_POINT_dbl(group
, base
, base
, ctx
))
889 if (!EC_POINTs_make_affine(group
, num
, points
, ctx
))
892 pre_comp
->group
= group
;
893 pre_comp
->blocksize
= blocksize
;
894 pre_comp
->numblocks
= numblocks
;
896 pre_comp
->points
= points
;
899 SETPRECOMP(group
, ec
, pre_comp
);
906 BN_CTX_free(new_ctx
);
907 EC_ec_pre_comp_free(pre_comp
);
911 for (p
= points
; *p
!= NULL
; p
++)
913 OPENSSL_free(points
);
915 EC_POINT_free(tmp_point
);
920 int ec_wNAF_have_precompute_mult(const EC_GROUP
*group
)
922 return HAVEPRECOMP(group
, ec
);