blob: d87a7d9767ba6a1cafcc22632a45d4f6fa247b85 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* Originally written by Bodo Moeller for the OpenSSL project.
2 * ====================================================================
3 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
55/* ====================================================================
56 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
57 *
58 * Portions of the attached software ("Contribution") are developed by
59 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
60 *
61 * The Contribution is licensed pursuant to the OpenSSL open source
62 * license provided above.
63 *
64 * The elliptic curve binary polynomial software is originally written by
65 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
66 * Laboratories. */
67
68#include <openssl/ec.h>
69
70#include <string.h>
71
72#include <openssl/bn.h>
73#include <openssl/err.h>
74#include <openssl/mem.h>
Adam Langleye9ada862015-05-11 17:20:37 -070075#include <openssl/thread.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080076
77#include "internal.h"
78
79
80/* This file implements the wNAF-based interleaving multi-exponentation method
81 * (<URL:http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/moeller.html#multiexp>);
82 * for multiplication with precomputation, we use wNAF splitting
83 * (<URL:http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/moeller.html#fastexp>).
84 * */
85
86/* structure for precomputed multiples of the generator */
87typedef struct ec_pre_comp_st {
Adam Langleyd9e397b2015-01-22 14:27:53 -080088 size_t blocksize; /* block size for wNAF splitting */
89 size_t numblocks; /* max. number of blocks for which we have precomputation */
90 size_t w; /* window size */
91 EC_POINT **points; /* array with pre-calculated multiples of generator:
92 * 'num' pointers to EC_POINT objects followed by a NULL */
93 size_t num; /* numblocks * 2^(w-1) */
94 int references;
95} EC_PRE_COMP;
96
Adam Langleye9ada862015-05-11 17:20:37 -070097static EC_PRE_COMP *ec_pre_comp_new(void) {
Adam Langleyd9e397b2015-01-22 14:27:53 -080098 EC_PRE_COMP *ret = NULL;
99
Adam Langleyd9e397b2015-01-22 14:27:53 -0800100 ret = (EC_PRE_COMP *)OPENSSL_malloc(sizeof(EC_PRE_COMP));
101 if (!ret) {
102 OPENSSL_PUT_ERROR(EC, ec_pre_comp_new, ERR_R_MALLOC_FAILURE);
103 return ret;
104 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800105 ret->blocksize = 8; /* default */
106 ret->numblocks = 0;
107 ret->w = 4; /* default */
108 ret->points = NULL;
109 ret->num = 0;
110 ret->references = 1;
111 return ret;
112}
113
114void *ec_pre_comp_dup(EC_PRE_COMP *pre_comp) {
115 if (pre_comp == NULL) {
116 return NULL;
117 }
118
119 CRYPTO_add(&pre_comp->references, 1, CRYPTO_LOCK_EC_PRE_COMP);
120 return pre_comp;
121}
122
123void ec_pre_comp_free(EC_PRE_COMP *pre_comp) {
Adam Langleye9ada862015-05-11 17:20:37 -0700124 if (pre_comp == NULL ||
125 CRYPTO_add(&pre_comp->references, -1, CRYPTO_LOCK_EC_PRE_COMP) > 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800126 return;
127 }
128
129 if (pre_comp->points) {
130 EC_POINT **p;
131
132 for (p = pre_comp->points; *p != NULL; p++) {
133 EC_POINT_free(*p);
134 }
135 OPENSSL_free(pre_comp->points);
136 }
137 OPENSSL_free(pre_comp);
138}
139
140
141/* Determine the modified width-(w+1) Non-Adjacent Form (wNAF) of 'scalar'.
142 * This is an array r[] of values that are either zero or odd with an
143 * absolute value less than 2^w satisfying
144 * scalar = \sum_j r[j]*2^j
145 * where at most one of any w+1 consecutive digits is non-zero
146 * with the exception that the most significant digit may be only
147 * w-1 zeros away from that next non-zero digit.
148 */
149static signed char *compute_wNAF(const BIGNUM *scalar, int w, size_t *ret_len) {
150 int window_val;
151 int ok = 0;
152 signed char *r = NULL;
153 int sign = 1;
154 int bit, next_bit, mask;
155 size_t len = 0, j;
156
157 if (BN_is_zero(scalar)) {
158 r = OPENSSL_malloc(1);
159 if (!r) {
160 OPENSSL_PUT_ERROR(EC, compute_wNAF, ERR_R_MALLOC_FAILURE);
161 goto err;
162 }
163 r[0] = 0;
164 *ret_len = 1;
165 return r;
166 }
167
168 if (w <= 0 || w > 7) /* 'signed char' can represent integers with absolute
169 values less than 2^7 */
170 {
171 OPENSSL_PUT_ERROR(EC, compute_wNAF, ERR_R_INTERNAL_ERROR);
172 goto err;
173 }
174 bit = 1 << w; /* at most 128 */
175 next_bit = bit << 1; /* at most 256 */
176 mask = next_bit - 1; /* at most 255 */
177
178 if (BN_is_negative(scalar)) {
179 sign = -1;
180 }
181
182 if (scalar->d == NULL || scalar->top == 0) {
183 OPENSSL_PUT_ERROR(EC, compute_wNAF, ERR_R_INTERNAL_ERROR);
184 goto err;
185 }
186
187 len = BN_num_bits(scalar);
188 r = OPENSSL_malloc(
189 len +
190 1); /* modified wNAF may be one digit longer than binary representation
191 * (*ret_len will be set to the actual length, i.e. at most
192 * BN_num_bits(scalar) + 1) */
193 if (r == NULL) {
194 OPENSSL_PUT_ERROR(EC, compute_wNAF, ERR_R_MALLOC_FAILURE);
195 goto err;
196 }
197 window_val = scalar->d[0] & mask;
198 j = 0;
199 while ((window_val != 0) ||
200 (j + w + 1 < len)) /* if j+w+1 >= len, window_val will not increase */
201 {
202 int digit = 0;
203
204 /* 0 <= window_val <= 2^(w+1) */
205
206 if (window_val & 1) {
207 /* 0 < window_val < 2^(w+1) */
208
209 if (window_val & bit) {
210 digit = window_val - next_bit; /* -2^w < digit < 0 */
211
212#if 1 /* modified wNAF */
213 if (j + w + 1 >= len) {
214 /* special case for generating modified wNAFs:
215 * no new bits will be added into window_val,
216 * so using a positive digit here will decrease
217 * the total length of the representation */
218
219 digit = window_val & (mask >> 1); /* 0 < digit < 2^w */
220 }
221#endif
222 } else {
223 digit = window_val; /* 0 < digit < 2^w */
224 }
225
226 if (digit <= -bit || digit >= bit || !(digit & 1)) {
227 OPENSSL_PUT_ERROR(EC, compute_wNAF, ERR_R_INTERNAL_ERROR);
228 goto err;
229 }
230
231 window_val -= digit;
232
233 /* now window_val is 0 or 2^(w+1) in standard wNAF generation;
234 * for modified window NAFs, it may also be 2^w
235 */
236 if (window_val != 0 && window_val != next_bit && window_val != bit) {
237 OPENSSL_PUT_ERROR(EC, compute_wNAF, ERR_R_INTERNAL_ERROR);
238 goto err;
239 }
240 }
241
242 r[j++] = sign * digit;
243
244 window_val >>= 1;
245 window_val += bit * BN_is_bit_set(scalar, j + w);
246
247 if (window_val > next_bit) {
248 OPENSSL_PUT_ERROR(EC, compute_wNAF, ERR_R_INTERNAL_ERROR);
249 goto err;
250 }
251 }
252
253 if (j > len + 1) {
254 OPENSSL_PUT_ERROR(EC, compute_wNAF, ERR_R_INTERNAL_ERROR);
255 goto err;
256 }
257 len = j;
258 ok = 1;
259
260err:
261 if (!ok) {
262 OPENSSL_free(r);
263 r = NULL;
264 }
Adam Langleye9ada862015-05-11 17:20:37 -0700265 if (ok) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800266 *ret_len = len;
Adam Langleye9ada862015-05-11 17:20:37 -0700267 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800268 return r;
269}
270
271
272/* TODO: table should be optimised for the wNAF-based implementation,
273 * sometimes smaller windows will give better performance
274 * (thus the boundaries should be increased)
275 */
276#define EC_window_bits_for_scalar_size(b) \
277 ((size_t)((b) >= 2000 ? 6 : (b) >= 800 ? 5 : (b) >= 300 \
278 ? 4 \
279 : (b) >= 70 ? 3 : (b) >= 20 \
280 ? 2 \
281 : 1))
282
283/* Compute
284 * \sum scalars[i]*points[i],
285 * also including
286 * scalar*generator
287 * in the addition if scalar != NULL
288 */
289int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
290 size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
291 BN_CTX *ctx) {
292 BN_CTX *new_ctx = NULL;
293 const EC_POINT *generator = NULL;
294 EC_POINT *tmp = NULL;
295 size_t totalnum;
296 size_t blocksize = 0, numblocks = 0; /* for wNAF splitting */
297 size_t pre_points_per_block = 0;
298 size_t i, j;
299 int k;
300 int r_is_inverted = 0;
301 int r_is_at_infinity = 1;
302 size_t *wsize = NULL; /* individual window sizes */
303 signed char **wNAF = NULL; /* individual wNAFs */
304 size_t *wNAF_len = NULL;
305 size_t max_len = 0;
306 size_t num_val;
307 EC_POINT **val = NULL; /* precomputation */
308 EC_POINT **v;
309 EC_POINT ***val_sub =
310 NULL; /* pointers to sub-arrays of 'val' or 'pre_comp->points' */
311 const EC_PRE_COMP *pre_comp = NULL;
312 int num_scalar = 0; /* flag: will be set to 1 if 'scalar' must be treated like
313 * other scalars,
314 * i.e. precomputation is not available */
315 int ret = 0;
316
317 if (group->meth != r->meth) {
318 OPENSSL_PUT_ERROR(EC, ec_wNAF_mul, EC_R_INCOMPATIBLE_OBJECTS);
319 return 0;
320 }
321
322 if ((scalar == NULL) && (num == 0)) {
323 return EC_POINT_set_to_infinity(group, r);
324 }
325
326 for (i = 0; i < num; i++) {
327 if (group->meth != points[i]->meth) {
328 OPENSSL_PUT_ERROR(EC, ec_wNAF_mul, EC_R_INCOMPATIBLE_OBJECTS);
329 return 0;
330 }
331 }
332
333 if (ctx == NULL) {
334 ctx = new_ctx = BN_CTX_new();
Adam Langleye9ada862015-05-11 17:20:37 -0700335 if (ctx == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800336 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700337 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800338 }
339
340 if (scalar != NULL) {
341 generator = EC_GROUP_get0_generator(group);
342 if (generator == NULL) {
343 OPENSSL_PUT_ERROR(EC, ec_wNAF_mul, EC_R_UNDEFINED_GENERATOR);
344 goto err;
345 }
346
347 /* look if we can use precomputed multiples of generator */
348
349 pre_comp = group->pre_comp;
350
351 if (pre_comp && pre_comp->numblocks &&
352 (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) == 0)) {
353 blocksize = pre_comp->blocksize;
354
355 /* determine maximum number of blocks that wNAF splitting may yield
356 * (NB: maximum wNAF length is bit length plus one) */
357 numblocks = (BN_num_bits(scalar) / blocksize) + 1;
358
359 /* we cannot use more blocks than we have precomputation for */
Adam Langleye9ada862015-05-11 17:20:37 -0700360 if (numblocks > pre_comp->numblocks) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800361 numblocks = pre_comp->numblocks;
Adam Langleye9ada862015-05-11 17:20:37 -0700362 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800363
364 pre_points_per_block = (size_t)1 << (pre_comp->w - 1);
365
366 /* check that pre_comp looks sane */
367 if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block)) {
368 OPENSSL_PUT_ERROR(EC, ec_wNAF_mul, ERR_R_INTERNAL_ERROR);
369 goto err;
370 }
371 } else {
372 /* can't use precomputation */
373 pre_comp = NULL;
374 numblocks = 1;
375 num_scalar = 1; /* treat 'scalar' like 'num'-th element of 'scalars' */
376 }
377 }
378
379 totalnum = num + numblocks;
380
381 wsize = OPENSSL_malloc(totalnum * sizeof wsize[0]);
382 wNAF_len = OPENSSL_malloc(totalnum * sizeof wNAF_len[0]);
383 wNAF = OPENSSL_malloc((totalnum + 1) *
384 sizeof wNAF[0]); /* includes space for pivot */
385 val_sub = OPENSSL_malloc(totalnum * sizeof val_sub[0]);
386
387 /* Ensure wNAF is initialised in case we end up going to err. */
388 if (wNAF) {
389 wNAF[0] = NULL; /* preliminary pivot */
390 }
391
392 if (!wsize || !wNAF_len || !wNAF || !val_sub) {
393 OPENSSL_PUT_ERROR(EC, ec_wNAF_mul, ERR_R_MALLOC_FAILURE);
394 goto err;
395 }
396
397 /* num_val will be the total number of temporarily precomputed points */
398 num_val = 0;
399
400 for (i = 0; i < num + num_scalar; i++) {
401 size_t bits;
402
403 bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar);
404 wsize[i] = EC_window_bits_for_scalar_size(bits);
405 num_val += (size_t)1 << (wsize[i] - 1);
406 wNAF[i + 1] = NULL; /* make sure we always have a pivot */
407 wNAF[i] =
408 compute_wNAF((i < num ? scalars[i] : scalar), wsize[i], &wNAF_len[i]);
Adam Langleye9ada862015-05-11 17:20:37 -0700409 if (wNAF[i] == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800410 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700411 }
412 if (wNAF_len[i] > max_len) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800413 max_len = wNAF_len[i];
Adam Langleye9ada862015-05-11 17:20:37 -0700414 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800415 }
416
417 if (numblocks) {
418 /* we go here iff scalar != NULL */
419
420 if (pre_comp == NULL) {
421 if (num_scalar != 1) {
422 OPENSSL_PUT_ERROR(EC, ec_wNAF_mul, ERR_R_INTERNAL_ERROR);
423 goto err;
424 }
425 /* we have already generated a wNAF for 'scalar' */
426 } else {
427 signed char *tmp_wNAF = NULL;
428 size_t tmp_len = 0;
429
430 if (num_scalar != 0) {
431 OPENSSL_PUT_ERROR(EC, ec_wNAF_mul, ERR_R_INTERNAL_ERROR);
432 goto err;
433 }
434
435 /* use the window size for which we have precomputation */
436 wsize[num] = pre_comp->w;
437 tmp_wNAF = compute_wNAF(scalar, wsize[num], &tmp_len);
Adam Langleye9ada862015-05-11 17:20:37 -0700438 if (!tmp_wNAF) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800439 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700440 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800441
442 if (tmp_len <= max_len) {
443 /* One of the other wNAFs is at least as long
444 * as the wNAF belonging to the generator,
445 * so wNAF splitting will not buy us anything. */
446
447 numblocks = 1; /* don't use wNAF splitting */
448 totalnum = num + numblocks;
449 wNAF[num] = tmp_wNAF;
450 wNAF[num + 1] = NULL;
451 wNAF_len[num] = tmp_len;
452 /* pre_comp->points starts with the points that we need here: */
453 val_sub[num] = pre_comp->points;
454 } else {
455 /* don't include tmp_wNAF directly into wNAF array
456 * - use wNAF splitting and include the blocks */
457
458 signed char *pp;
459 EC_POINT **tmp_points;
460
461 if (tmp_len < numblocks * blocksize) {
462 /* possibly we can do with fewer blocks than estimated */
463 numblocks = (tmp_len + blocksize - 1) / blocksize;
464 if (numblocks > pre_comp->numblocks) {
465 OPENSSL_PUT_ERROR(EC, ec_wNAF_mul, ERR_R_INTERNAL_ERROR);
466 goto err;
467 }
468 totalnum = num + numblocks;
469 }
470
471 /* split wNAF in 'numblocks' parts */
472 pp = tmp_wNAF;
473 tmp_points = pre_comp->points;
474
475 for (i = num; i < totalnum; i++) {
476 if (i < totalnum - 1) {
477 wNAF_len[i] = blocksize;
478 if (tmp_len < blocksize) {
479 OPENSSL_PUT_ERROR(EC, ec_wNAF_mul, ERR_R_INTERNAL_ERROR);
480 goto err;
481 }
482 tmp_len -= blocksize;
Adam Langleye9ada862015-05-11 17:20:37 -0700483 } else {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800484 /* last block gets whatever is left
485 * (this could be more or less than 'blocksize'!) */
486 wNAF_len[i] = tmp_len;
Adam Langleye9ada862015-05-11 17:20:37 -0700487 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800488
489 wNAF[i + 1] = NULL;
490 wNAF[i] = OPENSSL_malloc(wNAF_len[i]);
491 if (wNAF[i] == NULL) {
492 OPENSSL_PUT_ERROR(EC, ec_wNAF_mul, ERR_R_MALLOC_FAILURE);
493 OPENSSL_free(tmp_wNAF);
494 goto err;
495 }
496 memcpy(wNAF[i], pp, wNAF_len[i]);
Adam Langleye9ada862015-05-11 17:20:37 -0700497 if (wNAF_len[i] > max_len) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800498 max_len = wNAF_len[i];
Adam Langleye9ada862015-05-11 17:20:37 -0700499 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800500
501 if (*tmp_points == NULL) {
502 OPENSSL_PUT_ERROR(EC, ec_wNAF_mul, ERR_R_INTERNAL_ERROR);
503 OPENSSL_free(tmp_wNAF);
504 goto err;
505 }
506 val_sub[i] = tmp_points;
507 tmp_points += pre_points_per_block;
508 pp += blocksize;
509 }
510 OPENSSL_free(tmp_wNAF);
511 }
512 }
513 }
514
515 /* All points we precompute now go into a single array 'val'.
516 * 'val_sub[i]' is a pointer to the subarray for the i-th point,
517 * or to a subarray of 'pre_comp->points' if we already have precomputation.
518 */
519 val = OPENSSL_malloc((num_val + 1) * sizeof val[0]);
520 if (val == NULL) {
521 OPENSSL_PUT_ERROR(EC, ec_wNAF_mul, ERR_R_MALLOC_FAILURE);
522 goto err;
523 }
524 val[num_val] = NULL; /* pivot element */
525
526 /* allocate points for precomputation */
527 v = val;
528 for (i = 0; i < num + num_scalar; i++) {
529 val_sub[i] = v;
530 for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) {
531 *v = EC_POINT_new(group);
Adam Langleye9ada862015-05-11 17:20:37 -0700532 if (*v == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800533 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700534 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800535 v++;
536 }
537 }
538 if (!(v == val + num_val)) {
539 OPENSSL_PUT_ERROR(EC, ec_wNAF_mul, ERR_R_INTERNAL_ERROR);
540 goto err;
541 }
542
Adam Langleye9ada862015-05-11 17:20:37 -0700543 if (!(tmp = EC_POINT_new(group))) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800544 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700545 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800546
547 /* prepare precomputed values:
548 * val_sub[i][0] := points[i]
549 * val_sub[i][1] := 3 * points[i]
550 * val_sub[i][2] := 5 * points[i]
551 * ...
552 */
553 for (i = 0; i < num + num_scalar; i++) {
554 if (i < num) {
Adam Langleye9ada862015-05-11 17:20:37 -0700555 if (!EC_POINT_copy(val_sub[i][0], points[i])) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800556 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700557 }
558 } else if (!EC_POINT_copy(val_sub[i][0], generator)) {
559 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800560 }
561
562 if (wsize[i] > 1) {
Adam Langleye9ada862015-05-11 17:20:37 -0700563 if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800564 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700565 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800566 for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) {
Adam Langleye9ada862015-05-11 17:20:37 -0700567 if (!EC_POINT_add(group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800568 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700569 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800570 }
571 }
572 }
573
574#if 1 /* optional; EC_window_bits_for_scalar_size assumes we do this step */
Adam Langleye9ada862015-05-11 17:20:37 -0700575 if (!EC_POINTs_make_affine(group, num_val, val, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800576 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700577 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800578#endif
579
580 r_is_at_infinity = 1;
581
582 for (k = max_len - 1; k >= 0; k--) {
Adam Langleye9ada862015-05-11 17:20:37 -0700583 if (!r_is_at_infinity && !EC_POINT_dbl(group, r, r, ctx)) {
584 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800585 }
586
587 for (i = 0; i < totalnum; i++) {
588 if (wNAF_len[i] > (size_t)k) {
589 int digit = wNAF[i][k];
590 int is_neg;
591
592 if (digit) {
593 is_neg = digit < 0;
594
Adam Langleye9ada862015-05-11 17:20:37 -0700595 if (is_neg) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800596 digit = -digit;
Adam Langleye9ada862015-05-11 17:20:37 -0700597 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800598
599 if (is_neg != r_is_inverted) {
Adam Langleye9ada862015-05-11 17:20:37 -0700600 if (!r_is_at_infinity && !EC_POINT_invert(group, r, ctx)) {
601 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800602 }
603 r_is_inverted = !r_is_inverted;
604 }
605
606 /* digit > 0 */
607
608 if (r_is_at_infinity) {
Adam Langleye9ada862015-05-11 17:20:37 -0700609 if (!EC_POINT_copy(r, val_sub[i][digit >> 1])) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800610 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700611 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800612 r_is_at_infinity = 0;
613 } else {
Adam Langleye9ada862015-05-11 17:20:37 -0700614 if (!EC_POINT_add(group, r, r, val_sub[i][digit >> 1], ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800615 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700616 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800617 }
618 }
619 }
620 }
621 }
622
623 if (r_is_at_infinity) {
Adam Langleye9ada862015-05-11 17:20:37 -0700624 if (!EC_POINT_set_to_infinity(group, r)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800625 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700626 }
627 } else if (r_is_inverted && !EC_POINT_invert(group, r, ctx)) {
628 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800629 }
630
631 ret = 1;
632
633err:
Adam Langleye9ada862015-05-11 17:20:37 -0700634 BN_CTX_free(new_ctx);
635 EC_POINT_free(tmp);
636 OPENSSL_free(wsize);
637 OPENSSL_free(wNAF_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800638 if (wNAF != NULL) {
639 signed char **w;
640
Adam Langleye9ada862015-05-11 17:20:37 -0700641 for (w = wNAF; *w != NULL; w++) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800642 OPENSSL_free(*w);
Adam Langleye9ada862015-05-11 17:20:37 -0700643 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800644
645 OPENSSL_free(wNAF);
646 }
647 if (val != NULL) {
Adam Langleye9ada862015-05-11 17:20:37 -0700648 for (v = val; *v != NULL; v++) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800649 EC_POINT_clear_free(*v);
Adam Langleye9ada862015-05-11 17:20:37 -0700650 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800651
652 OPENSSL_free(val);
653 }
Adam Langleye9ada862015-05-11 17:20:37 -0700654 OPENSSL_free(val_sub);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800655 return ret;
656}
657
658
659/* ec_wNAF_precompute_mult()
660 * creates an EC_PRE_COMP object with preprecomputed multiples of the generator
661 * for use with wNAF splitting as implemented in ec_wNAF_mul().
662 *
663 * 'pre_comp->points' is an array of multiples of the generator
664 * of the following form:
665 * points[0] = generator;
666 * points[1] = 3 * generator;
667 * ...
668 * points[2^(w-1)-1] = (2^(w-1)-1) * generator;
669 * points[2^(w-1)] = 2^blocksize * generator;
670 * points[2^(w-1)+1] = 3 * 2^blocksize * generator;
671 * ...
672 * points[2^(w-1)*(numblocks-1)-1] = (2^(w-1)) * 2^(blocksize*(numblocks-2)) *
673 *generator
674 * points[2^(w-1)*(numblocks-1)] = 2^(blocksize*(numblocks-1)) *
675 *generator
676 * ...
677 * points[2^(w-1)*numblocks-1] = (2^(w-1)) * 2^(blocksize*(numblocks-1)) *
678 *generator
679 * points[2^(w-1)*numblocks] = NULL
680 */
681int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *ctx) {
682 const EC_POINT *generator;
683 EC_POINT *tmp_point = NULL, *base = NULL, **var;
684 BN_CTX *new_ctx = NULL;
685 BIGNUM *order;
686 size_t i, bits, w, pre_points_per_block, blocksize, numblocks, num;
687 EC_POINT **points = NULL;
688 EC_PRE_COMP *pre_comp;
689 int ret = 0;
690
691 /* if there is an old EC_PRE_COMP object, throw it away */
Adam Langleye9ada862015-05-11 17:20:37 -0700692 ec_pre_comp_free(group->pre_comp);
693 group->pre_comp = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800694
695 generator = EC_GROUP_get0_generator(group);
696 if (generator == NULL) {
697 OPENSSL_PUT_ERROR(EC, ec_wNAF_precompute_mult, EC_R_UNDEFINED_GENERATOR);
Adam Langleye9ada862015-05-11 17:20:37 -0700698 return 0;
699 }
700
701 pre_comp = ec_pre_comp_new();
702 if (pre_comp == NULL) {
703 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800704 }
705
706 if (ctx == NULL) {
707 ctx = new_ctx = BN_CTX_new();
Adam Langleye9ada862015-05-11 17:20:37 -0700708 if (ctx == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800709 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700710 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800711 }
712
713 BN_CTX_start(ctx);
714 order = BN_CTX_get(ctx);
Adam Langleye9ada862015-05-11 17:20:37 -0700715 if (order == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800716 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700717 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800718
Adam Langleye9ada862015-05-11 17:20:37 -0700719 if (!EC_GROUP_get_order(group, order, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800720 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700721 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800722 if (BN_is_zero(order)) {
723 OPENSSL_PUT_ERROR(EC, ec_wNAF_precompute_mult, EC_R_UNKNOWN_ORDER);
724 goto err;
725 }
726
727 bits = BN_num_bits(order);
728 /* The following parameters mean we precompute (approximately)
729 * one point per bit.
730 *
731 * TBD: The combination 8, 4 is perfect for 160 bits; for other
732 * bit lengths, other parameter combinations might provide better
733 * efficiency.
734 */
735 blocksize = 8;
736 w = 4;
737 if (EC_window_bits_for_scalar_size(bits) > w) {
738 /* let's not make the window too small ... */
739 w = EC_window_bits_for_scalar_size(bits);
740 }
741
742 numblocks = (bits + blocksize - 1) /
743 blocksize; /* max. number of blocks to use for wNAF splitting */
744
745 pre_points_per_block = (size_t)1 << (w - 1);
746 num = pre_points_per_block *
747 numblocks; /* number of points to compute and store */
748
749 points = OPENSSL_malloc(sizeof(EC_POINT *) * (num + 1));
750 if (!points) {
751 OPENSSL_PUT_ERROR(EC, ec_wNAF_precompute_mult, ERR_R_MALLOC_FAILURE);
752 goto err;
753 }
754
755 var = points;
756 var[num] = NULL; /* pivot */
757 for (i = 0; i < num; i++) {
758 if ((var[i] = EC_POINT_new(group)) == NULL) {
759 OPENSSL_PUT_ERROR(EC, ec_wNAF_precompute_mult, ERR_R_MALLOC_FAILURE);
760 goto err;
761 }
762 }
763
764 if (!(tmp_point = EC_POINT_new(group)) || !(base = EC_POINT_new(group))) {
765 OPENSSL_PUT_ERROR(EC, ec_wNAF_precompute_mult, ERR_R_MALLOC_FAILURE);
766 goto err;
767 }
768
Adam Langleye9ada862015-05-11 17:20:37 -0700769 if (!EC_POINT_copy(base, generator)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800770 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700771 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800772
773 /* do the precomputation */
774 for (i = 0; i < numblocks; i++) {
775 size_t j;
776
Adam Langleye9ada862015-05-11 17:20:37 -0700777 if (!EC_POINT_dbl(group, tmp_point, base, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800778 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700779 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800780
Adam Langleye9ada862015-05-11 17:20:37 -0700781 if (!EC_POINT_copy(*var++, base)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800782 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700783 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800784
785 for (j = 1; j < pre_points_per_block; j++, var++) {
786 /* calculate odd multiples of the current base point */
Adam Langleye9ada862015-05-11 17:20:37 -0700787 if (!EC_POINT_add(group, *var, tmp_point, *(var - 1), ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800788 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700789 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800790 }
791
792 if (i < numblocks - 1) {
793 /* get the next base (multiply current one by 2^blocksize) */
794 size_t k;
795
796 if (blocksize <= 2) {
797 OPENSSL_PUT_ERROR(EC, ec_wNAF_precompute_mult, ERR_R_INTERNAL_ERROR);
798 goto err;
799 }
800
Adam Langleye9ada862015-05-11 17:20:37 -0700801 if (!EC_POINT_dbl(group, base, tmp_point, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800802 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700803 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800804 for (k = 2; k < blocksize; k++) {
Adam Langleye9ada862015-05-11 17:20:37 -0700805 if (!EC_POINT_dbl(group, base, base, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800806 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700807 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800808 }
809 }
810 }
811
Adam Langleye9ada862015-05-11 17:20:37 -0700812 if (!EC_POINTs_make_affine(group, num, points, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800813 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700814 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800815
Adam Langleyd9e397b2015-01-22 14:27:53 -0800816 pre_comp->blocksize = blocksize;
817 pre_comp->numblocks = numblocks;
818 pre_comp->w = w;
819 pre_comp->points = points;
820 points = NULL;
821 pre_comp->num = num;
822
823 group->pre_comp = pre_comp;
824 pre_comp = NULL;
825
826 ret = 1;
827
828err:
Adam Langleye9ada862015-05-11 17:20:37 -0700829 if (ctx != NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800830 BN_CTX_end(ctx);
Adam Langleye9ada862015-05-11 17:20:37 -0700831 }
832 BN_CTX_free(new_ctx);
833 ec_pre_comp_free(pre_comp);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800834 if (points) {
835 EC_POINT **p;
836
Adam Langleye9ada862015-05-11 17:20:37 -0700837 for (p = points; *p != NULL; p++) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800838 EC_POINT_free(*p);
Adam Langleye9ada862015-05-11 17:20:37 -0700839 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800840 OPENSSL_free(points);
841 }
Adam Langleye9ada862015-05-11 17:20:37 -0700842 EC_POINT_free(tmp_point);
843 EC_POINT_free(base);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800844 return ret;
845}
846
847
848int ec_wNAF_have_precompute_mult(const EC_GROUP *group) {
849 return group->pre_comp != NULL;
850}