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);
317 totalnum = num + numblocks;
320 /* split wNAF in 'numblocks' parts */
322 tmp_points = pre_comp->points;
324 for (i = num; i < totalnum; i++) {
325 if (i < totalnum - 1) {
326 wNAF_len[i] = blocksize;
327 if (tmp_len < blocksize) {
328 ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
331 tmp_len -= blocksize;
334 * last block gets whatever is left (this could be
335 * more or less than 'blocksize'!)
337 wNAF_len[i] = tmp_len;
340 wNAF[i] = OPENSSL_malloc(wNAF_len[i]);
341 if (wNAF[i] == NULL) {
342 ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
343 OPENSSL_free(tmp_wNAF);
346 memcpy(wNAF[i], pp, wNAF_len[i]);
347 if (wNAF_len[i] > max_len)
348 max_len = wNAF_len[i];
350 if (*tmp_points == NULL) {
351 ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
352 OPENSSL_free(tmp_wNAF);
355 val_sub[i] = tmp_points;
356 tmp_points += pre_points_per_block;
359 OPENSSL_free(tmp_wNAF);
365 * All points we precompute now go into a single array 'val'.
366 * 'val_sub[i]' is a pointer to the subarray for the i-th point, or to a
367 * subarray of 'pre_comp->points' if we already have precomputation.
369 val = OPENSSL_malloc((num_val + 1) * sizeof val[0]);
371 ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
374 val[num_val] = NULL; /* pivot element */
376 /* allocate points for precomputation */
378 for (i = 0; i < num + num_scalar; i++) {
380 for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) {
381 *v = EC_POINT_new(group);
387 if (!(v == val + num_val)) {
388 ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
392 if ((tmp = EC_POINT_new(group)) == NULL)
396 * prepare precomputed values:
397 * val_sub[i][0] := points[i]
398 * val_sub[i][1] := 3 * points[i]
399 * val_sub[i][2] := 5 * points[i]
402 for (i = 0; i < num + num_scalar; i++) {
404 if (!EC_POINT_copy(val_sub[i][0], points[i]))
407 if (!EC_POINT_copy(val_sub[i][0], generator))
412 if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx))
414 for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) {
416 (group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx))
422 if (!EC_POINTs_make_affine(group, num_val, val, ctx))
425 r_is_at_infinity = 1;
427 for (k = max_len - 1; k >= 0; k--) {
428 if (!r_is_at_infinity) {
429 if (!EC_POINT_dbl(group, r, r, ctx))
433 for (i = 0; i < totalnum; i++) {
434 if (wNAF_len[i] > (size_t)k) {
435 int digit = wNAF[i][k];
444 if (is_neg != r_is_inverted) {
445 if (!r_is_at_infinity) {
446 if (!EC_POINT_invert(group, r, ctx))
449 r_is_inverted = !r_is_inverted;
454 if (r_is_at_infinity) {
455 if (!EC_POINT_copy(r, val_sub[i][digit >> 1]))
457 r_is_at_infinity = 0;
460 (group, r, r, val_sub[i][digit >> 1], ctx))
468 if (r_is_at_infinity) {
469 if (!EC_POINT_set_to_infinity(group, r))
473 if (!EC_POINT_invert(group, r, ctx))
480 BN_CTX_free(new_ctx);
483 OPENSSL_free(wNAF_len);
487 for (w = wNAF; *w != NULL; w++)
493 for (v = val; *v != NULL; v++)
494 EC_POINT_clear_free(*v);
498 OPENSSL_free(val_sub);
503 * ec_wNAF_precompute_mult()
504 * creates an EC_PRE_COMP object with preprecomputed multiples of the generator
505 * for use with wNAF splitting as implemented in ec_wNAF_mul().
507 * 'pre_comp->points' is an array of multiples of the generator
508 * of the following form:
509 * points[0] = generator;
510 * points[1] = 3 * generator;
512 * points[2^(w-1)-1] = (2^(w-1)-1) * generator;
513 * points[2^(w-1)] = 2^blocksize * generator;
514 * points[2^(w-1)+1] = 3 * 2^blocksize * generator;
516 * points[2^(w-1)*(numblocks-1)-1] = (2^(w-1)) * 2^(blocksize*(numblocks-2)) * generator
517 * points[2^(w-1)*(numblocks-1)] = 2^(blocksize*(numblocks-1)) * generator
519 * points[2^(w-1)*numblocks-1] = (2^(w-1)) * 2^(blocksize*(numblocks-1)) * generator
520 * points[2^(w-1)*numblocks] = NULL
522 int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
524 const EC_POINT *generator;
525 EC_POINT *tmp_point = NULL, *base = NULL, **var;
526 BN_CTX *new_ctx = NULL;
528 size_t i, bits, w, pre_points_per_block, blocksize, numblocks, num;
529 EC_POINT **points = NULL;
530 EC_PRE_COMP *pre_comp;
533 /* if there is an old EC_PRE_COMP object, throw it away */
534 EC_pre_comp_free(group);
535 if ((pre_comp = ec_pre_comp_new(group)) == NULL)
538 generator = EC_GROUP_get0_generator(group);
539 if (generator == NULL) {
540 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, EC_R_UNDEFINED_GENERATOR);
545 ctx = new_ctx = BN_CTX_new();
552 order = EC_GROUP_get0_order(group);
555 if (BN_is_zero(order)) {
556 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, EC_R_UNKNOWN_ORDER);
560 bits = BN_num_bits(order);
562 * The following parameters mean we precompute (approximately) one point
563 * per bit. TBD: The combination 8, 4 is perfect for 160 bits; for other
564 * bit lengths, other parameter combinations might provide better
569 if (EC_window_bits_for_scalar_size(bits) > w) {
570 /* let's not make the window too small ... */
571 w = EC_window_bits_for_scalar_size(bits);
574 numblocks = (bits + blocksize - 1) / blocksize; /* max. number of blocks
578 pre_points_per_block = (size_t)1 << (w - 1);
579 num = pre_points_per_block * numblocks; /* number of points to compute
582 points = OPENSSL_malloc(sizeof(*points) * (num + 1));
583 if (points == NULL) {
584 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
589 var[num] = NULL; /* pivot */
590 for (i = 0; i < num; i++) {
591 if ((var[i] = EC_POINT_new(group)) == NULL) {
592 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
597 if ((tmp_point = EC_POINT_new(group)) == NULL
598 || (base = EC_POINT_new(group)) == NULL) {
599 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
603 if (!EC_POINT_copy(base, generator))
606 /* do the precomputation */
607 for (i = 0; i < numblocks; i++) {
610 if (!EC_POINT_dbl(group, tmp_point, base, ctx))
613 if (!EC_POINT_copy(*var++, base))
616 for (j = 1; j < pre_points_per_block; j++, var++) {
618 * calculate odd multiples of the current base point
620 if (!EC_POINT_add(group, *var, tmp_point, *(var - 1), ctx))
624 if (i < numblocks - 1) {
626 * get the next base (multiply current one by 2^blocksize)
630 if (blocksize <= 2) {
631 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_INTERNAL_ERROR);
635 if (!EC_POINT_dbl(group, base, tmp_point, ctx))
637 for (k = 2; k < blocksize; k++) {
638 if (!EC_POINT_dbl(group, base, base, ctx))
644 if (!EC_POINTs_make_affine(group, num, points, ctx))
647 pre_comp->group = group;
648 pre_comp->blocksize = blocksize;
649 pre_comp->numblocks = numblocks;
651 pre_comp->points = points;
654 SETPRECOMP(group, ec, pre_comp);
661 BN_CTX_free(new_ctx);
662 EC_ec_pre_comp_free(pre_comp);
666 for (p = points; *p != NULL; p++)
668 OPENSSL_free(points);
670 EC_POINT_free(tmp_point);
675 int ec_wNAF_have_precompute_mult(const EC_GROUP *group)
677 return HAVEPRECOMP(group, ec);