2 * Copyright 2001-2017 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_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 * It performs either a fixed scalar point multiplication
115 * (scalar * generator)
116 * when point is NULL, or a generic scalar point multiplication
118 * when point is not NULL.
120 * scalar should be in the range [0,n) otherwise all constant time bets are off.
122 * NB: This says nothing about EC_POINT_add and EC_POINT_dbl,
123 * which of course are not constant time themselves.
125 * The product is stored in r.
127 * Returns 1 on success, 0 otherwise.
129 static int ec_mul_consttime(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
130 const EC_POINT *point, BN_CTX *ctx)
132 int i, order_bits, group_top, kbit, pbit, Z_is_one, ret;
136 BIGNUM *lambda = NULL;
137 BN_CTX *new_ctx = NULL;
140 if ((ctx = new_ctx = BN_CTX_secure_new()) == NULL)
143 if ((group->order == NULL) || (group->field == NULL))
146 order_bits = BN_num_bits(group->order);
148 s = EC_POINT_new(group);
153 if (group->generator == NULL)
155 if (!EC_POINT_copy(s, group->generator))
158 if (!EC_POINT_copy(s, point))
162 EC_POINT_set_flags(s, BN_FLG_CONSTTIME);
165 lambda = BN_CTX_get(ctx);
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.
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))
181 if (!BN_copy(k, scalar))
184 BN_set_flags(k, BN_FLG_CONSTTIME);
186 if ((BN_num_bits(k) > order_bits) || (BN_is_negative(k))) {
188 * this is an unusual input, and we don't guarantee
191 if(!BN_nnmod(k, k, group->order, ctx))
195 if (!BN_add(lambda, k, group->order))
197 BN_set_flags(lambda, BN_FLG_CONSTTIME);
198 if (!BN_add(k, lambda, group->order))
201 * lambda := scalar + order
202 * k := scalar + 2*order
204 kbit = BN_is_bit_set(lambda, order_bits);
205 BN_consttime_swap(kbit, k, lambda, group_top + 1);
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))
216 /* top bit is a 1, in a fixed pos */
217 if (!EC_POINT_copy(r, s))
220 EC_POINT_set_flags(r, BN_FLG_CONSTTIME);
222 if (!EC_POINT_dbl(group, s, s, ctx))
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); \
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))
241 if (!EC_POINT_dbl(group, r, r, ctx))
244 * pbit logic merges this cswap with that of the
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
258 BN_CTX_free(new_ctx);
262 #undef EC_POINT_set_flags
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)
269 #define EC_window_bits_for_scalar_size(b) \
280 * \sum scalars[i]*points[i],
283 * in the addition if scalar != NULL
285 int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
286 size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
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.
297 return ec_mul_consttime(group, r, scalar, NULL, ctx);
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
305 * To protect the secret scalar, we ignore if BN_FLG_CONSTTIME is
306 * actually set and we always call the constant time version.
308 return ec_mul_consttime(group, r, scalars[0], points[0], ctx);
311 BN_CTX *new_ctx = NULL;
312 const EC_POINT *generator = NULL;
313 EC_POINT *tmp = NULL;
315 size_t blocksize = 0, numblocks = 0; /* for wNAF splitting */
316 size_t pre_points_per_block = 0;
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;
326 EC_POINT **val = NULL; /* precomputation */
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 */
336 if (group->meth != r->meth) {
337 ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);
341 if ((scalar == NULL) && (num == 0)) {
342 return EC_POINT_set_to_infinity(group, r);
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);
353 ctx = new_ctx = BN_CTX_new();
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);
365 /* look if we can use precomputed multiples of generator */
367 pre_comp = group->pre_comp.ec;
368 if (pre_comp && pre_comp->numblocks
369 && (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) ==
371 blocksize = pre_comp->blocksize;
374 * determine maximum number of blocks that wNAF splitting may
375 * yield (NB: maximum wNAF length is bit length plus one)
377 numblocks = (BN_num_bits(scalar) / blocksize) + 1;
380 * we cannot use more blocks than we have precomputation for
382 if (numblocks > pre_comp->numblocks)
383 numblocks = pre_comp->numblocks;
385 pre_points_per_block = (size_t)1 << (pre_comp->w - 1);
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);
393 /* can't use precomputation */
396 num_scalar = 1; /* treat 'scalar' like 'num'-th element of
401 totalnum = num + numblocks;
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]));
409 /* Ensure wNAF is initialised in case we end up going to err */
411 wNAF[0] = NULL; /* preliminary pivot */
413 if (wsize == NULL || wNAF_len == NULL || wNAF == NULL || val_sub == NULL) {
414 ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
419 * num_val will be the total number of temporarily precomputed points
423 for (i = 0; i < num + num_scalar; i++) {
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 */
431 bn_compute_wNAF((i < num ? scalars[i] : scalar), wsize[i],
435 if (wNAF_len[i] > max_len)
436 max_len = wNAF_len[i];
440 /* we go here iff scalar != NULL */
442 if (pre_comp == NULL) {
443 if (num_scalar != 1) {
444 ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
447 /* we have already generated a wNAF for 'scalar' */
449 signed char *tmp_wNAF = NULL;
452 if (num_scalar != 0) {
453 ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
458 * use the window size for which we have precomputation
460 wsize[num] = pre_comp->w;
461 tmp_wNAF = bn_compute_wNAF(scalar, wsize[num], &tmp_len);
465 if (tmp_len <= max_len) {
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
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;
478 * pre_comp->points starts with the points that we need here:
480 val_sub[num] = pre_comp->points;
483 * don't include tmp_wNAF directly into wNAF array - use wNAF
484 * splitting and include the blocks
488 EC_POINT **tmp_points;
490 if (tmp_len < numblocks * blocksize) {
492 * possibly we can do with fewer blocks than estimated
494 numblocks = (tmp_len + blocksize - 1) / blocksize;
495 if (numblocks > pre_comp->numblocks) {
496 ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
497 OPENSSL_free(tmp_wNAF);
500 totalnum = num + numblocks;
503 /* split wNAF in 'numblocks' parts */
505 tmp_points = pre_comp->points;
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);
512 OPENSSL_free(tmp_wNAF);
515 tmp_len -= blocksize;
518 * last block gets whatever is left (this could be
519 * more or less than 'blocksize'!)
521 wNAF_len[i] = tmp_len;
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);
530 memcpy(wNAF[i], pp, wNAF_len[i]);
531 if (wNAF_len[i] > max_len)
532 max_len = wNAF_len[i];
534 if (*tmp_points == NULL) {
535 ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
536 OPENSSL_free(tmp_wNAF);
539 val_sub[i] = tmp_points;
540 tmp_points += pre_points_per_block;
543 OPENSSL_free(tmp_wNAF);
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.
553 val = OPENSSL_malloc((num_val + 1) * sizeof(val[0]));
555 ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
558 val[num_val] = NULL; /* pivot element */
560 /* allocate points for precomputation */
562 for (i = 0; i < num + num_scalar; i++) {
564 for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) {
565 *v = EC_POINT_new(group);
571 if (!(v == val + num_val)) {
572 ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
576 if ((tmp = EC_POINT_new(group)) == NULL)
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]
586 for (i = 0; i < num + num_scalar; i++) {
588 if (!EC_POINT_copy(val_sub[i][0], points[i]))
591 if (!EC_POINT_copy(val_sub[i][0], generator))
596 if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx))
598 for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) {
600 (group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx))
606 if (!EC_POINTs_make_affine(group, num_val, val, ctx))
609 r_is_at_infinity = 1;
611 for (k = max_len - 1; k >= 0; k--) {
612 if (!r_is_at_infinity) {
613 if (!EC_POINT_dbl(group, r, r, ctx))
617 for (i = 0; i < totalnum; i++) {
618 if (wNAF_len[i] > (size_t)k) {
619 int digit = wNAF[i][k];
628 if (is_neg != r_is_inverted) {
629 if (!r_is_at_infinity) {
630 if (!EC_POINT_invert(group, r, ctx))
633 r_is_inverted = !r_is_inverted;
638 if (r_is_at_infinity) {
639 if (!EC_POINT_copy(r, val_sub[i][digit >> 1]))
641 r_is_at_infinity = 0;
644 (group, r, r, val_sub[i][digit >> 1], ctx))
652 if (r_is_at_infinity) {
653 if (!EC_POINT_set_to_infinity(group, r))
657 if (!EC_POINT_invert(group, r, ctx))
664 BN_CTX_free(new_ctx);
667 OPENSSL_free(wNAF_len);
671 for (w = wNAF; *w != NULL; w++)
677 for (v = val; *v != NULL; v++)
678 EC_POINT_clear_free(*v);
682 OPENSSL_free(val_sub);
687 * ec_wNAF_precompute_mult()
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().
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;
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;
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
703 * points[2^(w-1)*numblocks-1] = (2^(w-1)) * 2^(blocksize*(numblocks-1)) * generator
704 * points[2^(w-1)*numblocks] = NULL
706 int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
708 const EC_POINT *generator;
709 EC_POINT *tmp_point = NULL, *base = NULL, **var;
710 BN_CTX *new_ctx = NULL;
712 size_t i, bits, w, pre_points_per_block, blocksize, numblocks, num;
713 EC_POINT **points = NULL;
714 EC_PRE_COMP *pre_comp;
717 /* if there is an old EC_PRE_COMP object, throw it away */
718 EC_pre_comp_free(group);
719 if ((pre_comp = ec_pre_comp_new(group)) == NULL)
722 generator = EC_GROUP_get0_generator(group);
723 if (generator == NULL) {
724 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, EC_R_UNDEFINED_GENERATOR);
729 ctx = new_ctx = BN_CTX_new();
736 order = EC_GROUP_get0_order(group);
739 if (BN_is_zero(order)) {
740 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, EC_R_UNKNOWN_ORDER);
744 bits = BN_num_bits(order);
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
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);
758 numblocks = (bits + blocksize - 1) / blocksize; /* max. number of blocks
762 pre_points_per_block = (size_t)1 << (w - 1);
763 num = pre_points_per_block * numblocks; /* number of points to compute
766 points = OPENSSL_malloc(sizeof(*points) * (num + 1));
767 if (points == NULL) {
768 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
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);
781 if ((tmp_point = EC_POINT_new(group)) == NULL
782 || (base = EC_POINT_new(group)) == NULL) {
783 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
787 if (!EC_POINT_copy(base, generator))
790 /* do the precomputation */
791 for (i = 0; i < numblocks; i++) {
794 if (!EC_POINT_dbl(group, tmp_point, base, ctx))
797 if (!EC_POINT_copy(*var++, base))
800 for (j = 1; j < pre_points_per_block; j++, var++) {
802 * calculate odd multiples of the current base point
804 if (!EC_POINT_add(group, *var, tmp_point, *(var - 1), ctx))
808 if (i < numblocks - 1) {
810 * get the next base (multiply current one by 2^blocksize)
814 if (blocksize <= 2) {
815 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_INTERNAL_ERROR);
819 if (!EC_POINT_dbl(group, base, tmp_point, ctx))
821 for (k = 2; k < blocksize; k++) {
822 if (!EC_POINT_dbl(group, base, base, ctx))
828 if (!EC_POINTs_make_affine(group, num, points, ctx))
831 pre_comp->group = group;
832 pre_comp->blocksize = blocksize;
833 pre_comp->numblocks = numblocks;
835 pre_comp->points = points;
838 SETPRECOMP(group, ec, pre_comp);
845 BN_CTX_free(new_ctx);
846 EC_ec_pre_comp_free(pre_comp);
850 for (p = points; *p != NULL; p++)
852 OPENSSL_free(points);
854 EC_POINT_free(tmp_point);
859 int ec_wNAF_have_precompute_mult(const EC_GROUP *group)
861 return HAVEPRECOMP(group, ec);