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
25 * (<URL:http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/moeller.html#multiexp>);
26 * for multiplication with precomputation, we use wNAF splitting
27 * (<URL: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) */
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_atomic_add(&pre->references, 1, &i, pre->lock);
80 void EC_ec_pre_comp_free(EC_PRE_COMP *pre)
87 CRYPTO_atomic_add(&pre->references, -1, &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);
105 * TODO: table should be optimised for the wNAF-based implementation,
106 * sometimes smaller windows will give better performance (thus the
107 * boundaries should be increased)
109 #define EC_window_bits_for_scalar_size(b) \
120 * \sum scalars[i]*points[i],
123 * in the addition if scalar != NULL
125 int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
126 size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
129 BN_CTX *new_ctx = NULL;
130 const EC_POINT *generator = NULL;
131 EC_POINT *tmp = NULL;
133 size_t blocksize = 0, numblocks = 0; /* for wNAF splitting */
134 size_t pre_points_per_block = 0;
137 int r_is_inverted = 0;
138 int r_is_at_infinity = 1;
139 size_t *wsize = NULL; /* individual window sizes */
140 signed char **wNAF = NULL; /* individual wNAFs */
141 size_t *wNAF_len = NULL;
144 EC_POINT **val = NULL; /* precomputation */
146 EC_POINT ***val_sub = NULL; /* pointers to sub-arrays of 'val' or
147 * 'pre_comp->points' */
148 const EC_PRE_COMP *pre_comp = NULL;
149 int num_scalar = 0; /* flag: will be set to 1 if 'scalar' must be
150 * treated like other scalars, i.e.
151 * precomputation is not available */
154 if (group->meth != r->meth) {
155 ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);
159 if ((scalar == NULL) && (num == 0)) {
160 return EC_POINT_set_to_infinity(group, r);
163 for (i = 0; i < num; i++) {
164 if (group->meth != points[i]->meth) {
165 ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);
171 ctx = new_ctx = BN_CTX_new();
176 if (scalar != NULL) {
177 generator = EC_GROUP_get0_generator(group);
178 if (generator == NULL) {
179 ECerr(EC_F_EC_WNAF_MUL, EC_R_UNDEFINED_GENERATOR);
183 /* look if we can use precomputed multiples of generator */
185 pre_comp = group->pre_comp.ec;
186 if (pre_comp && pre_comp->numblocks
187 && (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) ==
189 blocksize = pre_comp->blocksize;
192 * determine maximum number of blocks that wNAF splitting may
193 * yield (NB: maximum wNAF length is bit length plus one)
195 numblocks = (BN_num_bits(scalar) / blocksize) + 1;
198 * we cannot use more blocks than we have precomputation for
200 if (numblocks > pre_comp->numblocks)
201 numblocks = pre_comp->numblocks;
203 pre_points_per_block = (size_t)1 << (pre_comp->w - 1);
205 /* check that pre_comp looks sane */
206 if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block)) {
207 ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
211 /* can't use precomputation */
214 num_scalar = 1; /* treat 'scalar' like 'num'-th element of
219 totalnum = num + numblocks;
221 wsize = OPENSSL_malloc(totalnum * sizeof wsize[0]);
222 wNAF_len = OPENSSL_malloc(totalnum * sizeof wNAF_len[0]);
223 wNAF = OPENSSL_malloc((totalnum + 1) * sizeof wNAF[0]); /* includes space
225 val_sub = OPENSSL_malloc(totalnum * sizeof val_sub[0]);
227 /* Ensure wNAF is initialised in case we end up going to err */
229 wNAF[0] = NULL; /* preliminary pivot */
231 if (wsize == NULL || wNAF_len == NULL || wNAF == NULL || val_sub == NULL) {
232 ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
237 * num_val will be the total number of temporarily precomputed points
241 for (i = 0; i < num + num_scalar; i++) {
244 bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar);
245 wsize[i] = EC_window_bits_for_scalar_size(bits);
246 num_val += (size_t)1 << (wsize[i] - 1);
247 wNAF[i + 1] = NULL; /* make sure we always have a pivot */
249 bn_compute_wNAF((i < num ? scalars[i] : scalar), wsize[i],
253 if (wNAF_len[i] > max_len)
254 max_len = wNAF_len[i];
258 /* we go here iff scalar != NULL */
260 if (pre_comp == NULL) {
261 if (num_scalar != 1) {
262 ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
265 /* we have already generated a wNAF for 'scalar' */
267 signed char *tmp_wNAF = NULL;
270 if (num_scalar != 0) {
271 ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
276 * use the window size for which we have precomputation
278 wsize[num] = pre_comp->w;
279 tmp_wNAF = bn_compute_wNAF(scalar, wsize[num], &tmp_len);
283 if (tmp_len <= max_len) {
285 * One of the other wNAFs is at least as long as the wNAF
286 * belonging to the generator, so wNAF splitting will not buy
291 totalnum = num + 1; /* don't use wNAF splitting */
292 wNAF[num] = tmp_wNAF;
293 wNAF[num + 1] = NULL;
294 wNAF_len[num] = tmp_len;
296 * pre_comp->points starts with the points that we need here:
298 val_sub[num] = pre_comp->points;
301 * don't include tmp_wNAF directly into wNAF array - use wNAF
302 * splitting and include the blocks
306 EC_POINT **tmp_points;
308 if (tmp_len < numblocks * blocksize) {
310 * possibly we can do with fewer blocks than estimated
312 numblocks = (tmp_len + blocksize - 1) / blocksize;
313 if (numblocks > pre_comp->numblocks) {
314 ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
315 OPENSSL_free(tmp_wNAF);
318 totalnum = num + numblocks;
321 /* split wNAF in 'numblocks' parts */
323 tmp_points = pre_comp->points;
325 for (i = num; i < totalnum; i++) {
326 if (i < totalnum - 1) {
327 wNAF_len[i] = blocksize;
328 if (tmp_len < blocksize) {
329 ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
330 OPENSSL_free(tmp_wNAF);
333 tmp_len -= blocksize;
336 * last block gets whatever is left (this could be
337 * more or less than 'blocksize'!)
339 wNAF_len[i] = tmp_len;
342 wNAF[i] = OPENSSL_malloc(wNAF_len[i]);
343 if (wNAF[i] == NULL) {
344 ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
345 OPENSSL_free(tmp_wNAF);
348 memcpy(wNAF[i], pp, wNAF_len[i]);
349 if (wNAF_len[i] > max_len)
350 max_len = wNAF_len[i];
352 if (*tmp_points == NULL) {
353 ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
354 OPENSSL_free(tmp_wNAF);
357 val_sub[i] = tmp_points;
358 tmp_points += pre_points_per_block;
361 OPENSSL_free(tmp_wNAF);
367 * All points we precompute now go into a single array 'val'.
368 * 'val_sub[i]' is a pointer to the subarray for the i-th point, or to a
369 * subarray of 'pre_comp->points' if we already have precomputation.
371 val = OPENSSL_malloc((num_val + 1) * sizeof val[0]);
373 ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
376 val[num_val] = NULL; /* pivot element */
378 /* allocate points for precomputation */
380 for (i = 0; i < num + num_scalar; i++) {
382 for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) {
383 *v = EC_POINT_new(group);
389 if (!(v == val + num_val)) {
390 ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
394 if ((tmp = EC_POINT_new(group)) == NULL)
398 * prepare precomputed values:
399 * val_sub[i][0] := points[i]
400 * val_sub[i][1] := 3 * points[i]
401 * val_sub[i][2] := 5 * points[i]
404 for (i = 0; i < num + num_scalar; i++) {
406 if (!EC_POINT_copy(val_sub[i][0], points[i]))
409 if (!EC_POINT_copy(val_sub[i][0], generator))
414 if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx))
416 for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) {
418 (group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx))
424 if (!EC_POINTs_make_affine(group, num_val, val, ctx))
427 r_is_at_infinity = 1;
429 for (k = max_len - 1; k >= 0; k--) {
430 if (!r_is_at_infinity) {
431 if (!EC_POINT_dbl(group, r, r, ctx))
435 for (i = 0; i < totalnum; i++) {
436 if (wNAF_len[i] > (size_t)k) {
437 int digit = wNAF[i][k];
446 if (is_neg != r_is_inverted) {
447 if (!r_is_at_infinity) {
448 if (!EC_POINT_invert(group, r, ctx))
451 r_is_inverted = !r_is_inverted;
456 if (r_is_at_infinity) {
457 if (!EC_POINT_copy(r, val_sub[i][digit >> 1]))
459 r_is_at_infinity = 0;
462 (group, r, r, val_sub[i][digit >> 1], ctx))
470 if (r_is_at_infinity) {
471 if (!EC_POINT_set_to_infinity(group, r))
475 if (!EC_POINT_invert(group, r, ctx))
482 BN_CTX_free(new_ctx);
485 OPENSSL_free(wNAF_len);
489 for (w = wNAF; *w != NULL; w++)
495 for (v = val; *v != NULL; v++)
496 EC_POINT_clear_free(*v);
500 OPENSSL_free(val_sub);
505 * ec_wNAF_precompute_mult()
506 * creates an EC_PRE_COMP object with preprecomputed multiples of the generator
507 * for use with wNAF splitting as implemented in ec_wNAF_mul().
509 * 'pre_comp->points' is an array of multiples of the generator
510 * of the following form:
511 * points[0] = generator;
512 * points[1] = 3 * generator;
514 * points[2^(w-1)-1] = (2^(w-1)-1) * generator;
515 * points[2^(w-1)] = 2^blocksize * generator;
516 * points[2^(w-1)+1] = 3 * 2^blocksize * generator;
518 * points[2^(w-1)*(numblocks-1)-1] = (2^(w-1)) * 2^(blocksize*(numblocks-2)) * generator
519 * points[2^(w-1)*(numblocks-1)] = 2^(blocksize*(numblocks-1)) * generator
521 * points[2^(w-1)*numblocks-1] = (2^(w-1)) * 2^(blocksize*(numblocks-1)) * generator
522 * points[2^(w-1)*numblocks] = NULL
524 int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
526 const EC_POINT *generator;
527 EC_POINT *tmp_point = NULL, *base = NULL, **var;
528 BN_CTX *new_ctx = NULL;
530 size_t i, bits, w, pre_points_per_block, blocksize, numblocks, num;
531 EC_POINT **points = NULL;
532 EC_PRE_COMP *pre_comp;
535 /* if there is an old EC_PRE_COMP object, throw it away */
536 EC_pre_comp_free(group);
537 if ((pre_comp = ec_pre_comp_new(group)) == NULL)
540 generator = EC_GROUP_get0_generator(group);
541 if (generator == NULL) {
542 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, EC_R_UNDEFINED_GENERATOR);
547 ctx = new_ctx = BN_CTX_new();
554 order = EC_GROUP_get0_order(group);
557 if (BN_is_zero(order)) {
558 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, EC_R_UNKNOWN_ORDER);
562 bits = BN_num_bits(order);
564 * The following parameters mean we precompute (approximately) one point
565 * per bit. TBD: The combination 8, 4 is perfect for 160 bits; for other
566 * bit lengths, other parameter combinations might provide better
571 if (EC_window_bits_for_scalar_size(bits) > w) {
572 /* let's not make the window too small ... */
573 w = EC_window_bits_for_scalar_size(bits);
576 numblocks = (bits + blocksize - 1) / blocksize; /* max. number of blocks
580 pre_points_per_block = (size_t)1 << (w - 1);
581 num = pre_points_per_block * numblocks; /* number of points to compute
584 points = OPENSSL_malloc(sizeof(*points) * (num + 1));
585 if (points == NULL) {
586 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
591 var[num] = NULL; /* pivot */
592 for (i = 0; i < num; i++) {
593 if ((var[i] = EC_POINT_new(group)) == NULL) {
594 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
599 if ((tmp_point = EC_POINT_new(group)) == NULL
600 || (base = EC_POINT_new(group)) == NULL) {
601 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
605 if (!EC_POINT_copy(base, generator))
608 /* do the precomputation */
609 for (i = 0; i < numblocks; i++) {
612 if (!EC_POINT_dbl(group, tmp_point, base, ctx))
615 if (!EC_POINT_copy(*var++, base))
618 for (j = 1; j < pre_points_per_block; j++, var++) {
620 * calculate odd multiples of the current base point
622 if (!EC_POINT_add(group, *var, tmp_point, *(var - 1), ctx))
626 if (i < numblocks - 1) {
628 * get the next base (multiply current one by 2^blocksize)
632 if (blocksize <= 2) {
633 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_INTERNAL_ERROR);
637 if (!EC_POINT_dbl(group, base, tmp_point, ctx))
639 for (k = 2; k < blocksize; k++) {
640 if (!EC_POINT_dbl(group, base, base, ctx))
646 if (!EC_POINTs_make_affine(group, num, points, ctx))
649 pre_comp->group = group;
650 pre_comp->blocksize = blocksize;
651 pre_comp->numblocks = numblocks;
653 pre_comp->points = points;
656 SETPRECOMP(group, ec, pre_comp);
663 BN_CTX_free(new_ctx);
664 EC_ec_pre_comp_free(pre_comp);
668 for (p = points; *p != NULL; p++)
670 OPENSSL_free(points);
672 EC_POINT_free(tmp_point);
677 int ec_wNAF_have_precompute_mult(const EC_GROUP *group)
679 return HAVEPRECOMP(group, ec);