2 * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* ====================================================================
11 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
12 * Portions of this software developed by SUN MICROSYSTEMS, INC.,
13 * and contributed to the OpenSSL project.
17 #include <openssl/err.h>
19 #include "internal/cryptlib.h"
20 #include "internal/bn_int.h"
24 * This file implements the wNAF-based interleaving multi-exponentiation method
26 * http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/moeller.html#multiexp
27 * You might now find it here:
28 * http://link.springer.com/chapter/10.1007%2F3-540-45537-X_13
29 * http://www.bmoeller.de/pdf/TI-01-08.multiexp.pdf
30 * For multiplication with precomputation, we use wNAF splitting, formerly at:
31 * http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/moeller.html#fastexp
34 /* structure for precomputed multiples of the generator */
35 struct ec_pre_comp_st {
36 const EC_GROUP *group; /* parent EC_GROUP object */
37 size_t blocksize; /* block size for wNAF splitting */
38 size_t numblocks; /* max. number of blocks for which we have
40 size_t w; /* window size */
41 EC_POINT **points; /* array with pre-calculated multiples of
42 * generator: 'num' pointers to EC_POINT
43 * objects followed by a NULL */
44 size_t num; /* numblocks * 2^(w-1) */
49 static EC_PRE_COMP *ec_pre_comp_new(const EC_GROUP *group)
51 EC_PRE_COMP *ret = NULL;
56 ret = OPENSSL_zalloc(sizeof(*ret));
58 ECerr(EC_F_EC_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
63 ret->blocksize = 8; /* default */
64 ret->w = 4; /* default */
67 ret->lock = CRYPTO_THREAD_lock_new();
68 if (ret->lock == NULL) {
69 ECerr(EC_F_EC_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
76 EC_PRE_COMP *EC_ec_pre_comp_dup(EC_PRE_COMP *pre)
80 CRYPTO_atomic_add(&pre->references, 1, &i, pre->lock);
84 void EC_ec_pre_comp_free(EC_PRE_COMP *pre)
91 CRYPTO_atomic_add(&pre->references, -1, &i, pre->lock);
92 REF_PRINT_COUNT("EC_ec", pre);
95 REF_ASSERT_ISNT(i < 0);
97 if (pre->points != NULL) {
100 for (pts = pre->points; *pts != NULL; pts++)
102 OPENSSL_free(pre->points);
104 CRYPTO_THREAD_lock_free(pre->lock);
109 * TODO: table should be optimised for the wNAF-based implementation,
110 * sometimes smaller windows will give better performance (thus the
111 * boundaries should be increased)
113 #define EC_window_bits_for_scalar_size(b) \
124 * \sum scalars[i]*points[i],
127 * in the addition if scalar != NULL
129 int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
130 size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
133 BN_CTX *new_ctx = NULL;
134 const EC_POINT *generator = NULL;
135 EC_POINT *tmp = NULL;
137 size_t blocksize = 0, numblocks = 0; /* for wNAF splitting */
138 size_t pre_points_per_block = 0;
141 int r_is_inverted = 0;
142 int r_is_at_infinity = 1;
143 size_t *wsize = NULL; /* individual window sizes */
144 signed char **wNAF = NULL; /* individual wNAFs */
145 size_t *wNAF_len = NULL;
148 EC_POINT **val = NULL; /* precomputation */
150 EC_POINT ***val_sub = NULL; /* pointers to sub-arrays of 'val' or
151 * 'pre_comp->points' */
152 const EC_PRE_COMP *pre_comp = NULL;
153 int num_scalar = 0; /* flag: will be set to 1 if 'scalar' must be
154 * treated like other scalars, i.e.
155 * precomputation is not available */
158 if (group->meth != r->meth) {
159 ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);
163 if ((scalar == NULL) && (num == 0)) {
164 return EC_POINT_set_to_infinity(group, r);
167 for (i = 0; i < num; i++) {
168 if (group->meth != points[i]->meth) {
169 ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);
175 ctx = new_ctx = BN_CTX_new();
180 if (scalar != NULL) {
181 generator = EC_GROUP_get0_generator(group);
182 if (generator == NULL) {
183 ECerr(EC_F_EC_WNAF_MUL, EC_R_UNDEFINED_GENERATOR);
187 /* look if we can use precomputed multiples of generator */
189 pre_comp = group->pre_comp.ec;
190 if (pre_comp && pre_comp->numblocks
191 && (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) ==
193 blocksize = pre_comp->blocksize;
196 * determine maximum number of blocks that wNAF splitting may
197 * yield (NB: maximum wNAF length is bit length plus one)
199 numblocks = (BN_num_bits(scalar) / blocksize) + 1;
202 * we cannot use more blocks than we have precomputation for
204 if (numblocks > pre_comp->numblocks)
205 numblocks = pre_comp->numblocks;
207 pre_points_per_block = (size_t)1 << (pre_comp->w - 1);
209 /* check that pre_comp looks sane */
210 if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block)) {
211 ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
215 /* can't use precomputation */
218 num_scalar = 1; /* treat 'scalar' like 'num'-th element of
223 totalnum = num + numblocks;
225 wsize = OPENSSL_malloc(totalnum * sizeof wsize[0]);
226 wNAF_len = OPENSSL_malloc(totalnum * sizeof wNAF_len[0]);
227 wNAF = OPENSSL_malloc((totalnum + 1) * sizeof wNAF[0]); /* includes space
229 val_sub = OPENSSL_malloc(totalnum * sizeof val_sub[0]);
231 /* Ensure wNAF is initialised in case we end up going to err */
233 wNAF[0] = NULL; /* preliminary pivot */
235 if (wsize == NULL || wNAF_len == NULL || wNAF == NULL || val_sub == NULL) {
236 ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
241 * num_val will be the total number of temporarily precomputed points
245 for (i = 0; i < num + num_scalar; i++) {
248 bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar);
249 wsize[i] = EC_window_bits_for_scalar_size(bits);
250 num_val += (size_t)1 << (wsize[i] - 1);
251 wNAF[i + 1] = NULL; /* make sure we always have a pivot */
253 bn_compute_wNAF((i < num ? scalars[i] : scalar), wsize[i],
257 if (wNAF_len[i] > max_len)
258 max_len = wNAF_len[i];
262 /* we go here iff scalar != NULL */
264 if (pre_comp == NULL) {
265 if (num_scalar != 1) {
266 ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
269 /* we have already generated a wNAF for 'scalar' */
271 signed char *tmp_wNAF = NULL;
274 if (num_scalar != 0) {
275 ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
280 * use the window size for which we have precomputation
282 wsize[num] = pre_comp->w;
283 tmp_wNAF = bn_compute_wNAF(scalar, wsize[num], &tmp_len);
287 if (tmp_len <= max_len) {
289 * One of the other wNAFs is at least as long as the wNAF
290 * belonging to the generator, so wNAF splitting will not buy
295 totalnum = num + 1; /* don't use wNAF splitting */
296 wNAF[num] = tmp_wNAF;
297 wNAF[num + 1] = NULL;
298 wNAF_len[num] = tmp_len;
300 * pre_comp->points starts with the points that we need here:
302 val_sub[num] = pre_comp->points;
305 * don't include tmp_wNAF directly into wNAF array - use wNAF
306 * splitting and include the blocks
310 EC_POINT **tmp_points;
312 if (tmp_len < numblocks * blocksize) {
314 * possibly we can do with fewer blocks than estimated
316 numblocks = (tmp_len + blocksize - 1) / blocksize;
317 if (numblocks > pre_comp->numblocks) {
318 ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
319 OPENSSL_free(tmp_wNAF);
322 totalnum = num + numblocks;
325 /* split wNAF in 'numblocks' parts */
327 tmp_points = pre_comp->points;
329 for (i = num; i < totalnum; i++) {
330 if (i < totalnum - 1) {
331 wNAF_len[i] = blocksize;
332 if (tmp_len < blocksize) {
333 ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
334 OPENSSL_free(tmp_wNAF);
337 tmp_len -= blocksize;
340 * last block gets whatever is left (this could be
341 * more or less than 'blocksize'!)
343 wNAF_len[i] = tmp_len;
346 wNAF[i] = OPENSSL_malloc(wNAF_len[i]);
347 if (wNAF[i] == NULL) {
348 ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
349 OPENSSL_free(tmp_wNAF);
352 memcpy(wNAF[i], pp, wNAF_len[i]);
353 if (wNAF_len[i] > max_len)
354 max_len = wNAF_len[i];
356 if (*tmp_points == NULL) {
357 ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
358 OPENSSL_free(tmp_wNAF);
361 val_sub[i] = tmp_points;
362 tmp_points += pre_points_per_block;
365 OPENSSL_free(tmp_wNAF);
371 * All points we precompute now go into a single array 'val'.
372 * 'val_sub[i]' is a pointer to the subarray for the i-th point, or to a
373 * subarray of 'pre_comp->points' if we already have precomputation.
375 val = OPENSSL_malloc((num_val + 1) * sizeof val[0]);
377 ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
380 val[num_val] = NULL; /* pivot element */
382 /* allocate points for precomputation */
384 for (i = 0; i < num + num_scalar; i++) {
386 for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) {
387 *v = EC_POINT_new(group);
393 if (!(v == val + num_val)) {
394 ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
398 if ((tmp = EC_POINT_new(group)) == NULL)
402 * prepare precomputed values:
403 * val_sub[i][0] := points[i]
404 * val_sub[i][1] := 3 * points[i]
405 * val_sub[i][2] := 5 * points[i]
408 for (i = 0; i < num + num_scalar; i++) {
410 if (!EC_POINT_copy(val_sub[i][0], points[i]))
413 if (!EC_POINT_copy(val_sub[i][0], generator))
418 if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx))
420 for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) {
422 (group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx))
428 if (!EC_POINTs_make_affine(group, num_val, val, ctx))
431 r_is_at_infinity = 1;
433 for (k = max_len - 1; k >= 0; k--) {
434 if (!r_is_at_infinity) {
435 if (!EC_POINT_dbl(group, r, r, ctx))
439 for (i = 0; i < totalnum; i++) {
440 if (wNAF_len[i] > (size_t)k) {
441 int digit = wNAF[i][k];
450 if (is_neg != r_is_inverted) {
451 if (!r_is_at_infinity) {
452 if (!EC_POINT_invert(group, r, ctx))
455 r_is_inverted = !r_is_inverted;
460 if (r_is_at_infinity) {
461 if (!EC_POINT_copy(r, val_sub[i][digit >> 1]))
463 r_is_at_infinity = 0;
466 (group, r, r, val_sub[i][digit >> 1], ctx))
474 if (r_is_at_infinity) {
475 if (!EC_POINT_set_to_infinity(group, r))
479 if (!EC_POINT_invert(group, r, ctx))
486 BN_CTX_free(new_ctx);
489 OPENSSL_free(wNAF_len);
493 for (w = wNAF; *w != NULL; w++)
499 for (v = val; *v != NULL; v++)
500 EC_POINT_clear_free(*v);
504 OPENSSL_free(val_sub);
509 * ec_wNAF_precompute_mult()
510 * creates an EC_PRE_COMP object with preprecomputed multiples of the generator
511 * for use with wNAF splitting as implemented in ec_wNAF_mul().
513 * 'pre_comp->points' is an array of multiples of the generator
514 * of the following form:
515 * points[0] = generator;
516 * points[1] = 3 * generator;
518 * points[2^(w-1)-1] = (2^(w-1)-1) * generator;
519 * points[2^(w-1)] = 2^blocksize * generator;
520 * points[2^(w-1)+1] = 3 * 2^blocksize * generator;
522 * points[2^(w-1)*(numblocks-1)-1] = (2^(w-1)) * 2^(blocksize*(numblocks-2)) * generator
523 * points[2^(w-1)*(numblocks-1)] = 2^(blocksize*(numblocks-1)) * generator
525 * points[2^(w-1)*numblocks-1] = (2^(w-1)) * 2^(blocksize*(numblocks-1)) * generator
526 * points[2^(w-1)*numblocks] = NULL
528 int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
530 const EC_POINT *generator;
531 EC_POINT *tmp_point = NULL, *base = NULL, **var;
532 BN_CTX *new_ctx = NULL;
534 size_t i, bits, w, pre_points_per_block, blocksize, numblocks, num;
535 EC_POINT **points = NULL;
536 EC_PRE_COMP *pre_comp;
539 /* if there is an old EC_PRE_COMP object, throw it away */
540 EC_pre_comp_free(group);
541 if ((pre_comp = ec_pre_comp_new(group)) == NULL)
544 generator = EC_GROUP_get0_generator(group);
545 if (generator == NULL) {
546 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, EC_R_UNDEFINED_GENERATOR);
551 ctx = new_ctx = BN_CTX_new();
558 order = EC_GROUP_get0_order(group);
561 if (BN_is_zero(order)) {
562 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, EC_R_UNKNOWN_ORDER);
566 bits = BN_num_bits(order);
568 * The following parameters mean we precompute (approximately) one point
569 * per bit. TBD: The combination 8, 4 is perfect for 160 bits; for other
570 * bit lengths, other parameter combinations might provide better
575 if (EC_window_bits_for_scalar_size(bits) > w) {
576 /* let's not make the window too small ... */
577 w = EC_window_bits_for_scalar_size(bits);
580 numblocks = (bits + blocksize - 1) / blocksize; /* max. number of blocks
584 pre_points_per_block = (size_t)1 << (w - 1);
585 num = pre_points_per_block * numblocks; /* number of points to compute
588 points = OPENSSL_malloc(sizeof(*points) * (num + 1));
589 if (points == NULL) {
590 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
595 var[num] = NULL; /* pivot */
596 for (i = 0; i < num; i++) {
597 if ((var[i] = EC_POINT_new(group)) == NULL) {
598 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
603 if ((tmp_point = EC_POINT_new(group)) == NULL
604 || (base = EC_POINT_new(group)) == NULL) {
605 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
609 if (!EC_POINT_copy(base, generator))
612 /* do the precomputation */
613 for (i = 0; i < numblocks; i++) {
616 if (!EC_POINT_dbl(group, tmp_point, base, ctx))
619 if (!EC_POINT_copy(*var++, base))
622 for (j = 1; j < pre_points_per_block; j++, var++) {
624 * calculate odd multiples of the current base point
626 if (!EC_POINT_add(group, *var, tmp_point, *(var - 1), ctx))
630 if (i < numblocks - 1) {
632 * get the next base (multiply current one by 2^blocksize)
636 if (blocksize <= 2) {
637 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_INTERNAL_ERROR);
641 if (!EC_POINT_dbl(group, base, tmp_point, ctx))
643 for (k = 2; k < blocksize; k++) {
644 if (!EC_POINT_dbl(group, base, base, ctx))
650 if (!EC_POINTs_make_affine(group, num, points, ctx))
653 pre_comp->group = group;
654 pre_comp->blocksize = blocksize;
655 pre_comp->numblocks = numblocks;
657 pre_comp->points = points;
660 SETPRECOMP(group, ec, pre_comp);
667 BN_CTX_free(new_ctx);
668 EC_ec_pre_comp_free(pre_comp);
672 for (p = points; *p != NULL; p++)
674 OPENSSL_free(points);
676 EC_POINT_free(tmp_point);
681 int ec_wNAF_have_precompute_mult(const EC_GROUP *group)
683 return HAVEPRECOMP(group, ec);