]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/ec_mult.c
Added an explicit yield (OP_SLEEP) to QUIC testing for cooperative threading.
[thirdparty/openssl.git] / crypto / ec / ec_mult.c
CommitLineData
35b73a1f 1/*
da1c088f 2 * Copyright 2001-2023 The OpenSSL Project Authors. All Rights Reserved.
aa8f3d76 3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
65e81670 4 *
a7f182b7 5 * Licensed under the Apache License 2.0 (the "License"). You may not use
4f22f405
RS
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
65e81670 9 */
4f22f405 10
579422c8
P
11/*
12 * ECDSA low level APIs are deprecated for public use, but still ok for
13 * internal use.
14 */
15#include "internal/deprecated.h"
16
28f573a2 17#include <string.h>
48fe4d62
BM
18#include <openssl/err.h>
19
9b398ef2 20#include "internal/cryptlib.h"
25f2138b 21#include "crypto/bn.h"
706457b7 22#include "ec_local.h"
cd420b0b 23#include "internal/refcount.h"
48fe4d62 24
37c660ff 25/*
0d4fb843 26 * This file implements the wNAF-based interleaving multi-exponentiation method
dea0eb2c
RS
27 * Formerly at:
28 * http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/moeller.html#multiexp
29 * You might now find it here:
30 * http://link.springer.com/chapter/10.1007%2F3-540-45537-X_13
31 * http://www.bmoeller.de/pdf/TI-01-08.multiexp.pdf
32 * For multiplication with precomputation, we use wNAF splitting, formerly at:
33 * http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/moeller.html#fastexp
37c660ff 34 */
48fe4d62 35
37c660ff 36/* structure for precomputed multiples of the generator */
3aef36ff 37struct ec_pre_comp_st {
0f113f3e
MC
38 const EC_GROUP *group; /* parent EC_GROUP object */
39 size_t blocksize; /* block size for wNAF splitting */
40 size_t numblocks; /* max. number of blocks for which we have
41 * precomputation */
42 size_t w; /* window size */
43 EC_POINT **points; /* array with pre-calculated multiples of
44 * generator: 'num' pointers to EC_POINT
45 * objects followed by a NULL */
46 size_t num; /* numblocks * 2^(w-1) */
2f545ae4 47 CRYPTO_REF_COUNT references;
3aef36ff 48};
37c660ff
BM
49
50static EC_PRE_COMP *ec_pre_comp_new(const EC_GROUP *group)
0f113f3e
MC
51{
52 EC_PRE_COMP *ret = NULL;
53
54 if (!group)
55 return NULL;
56
64b25758 57 ret = OPENSSL_zalloc(sizeof(*ret));
e077455e 58 if (ret == NULL)
0f113f3e 59 return ret;
9b398ef2 60
0f113f3e
MC
61 ret->group = group;
62 ret->blocksize = 8; /* default */
0f113f3e 63 ret->w = 4; /* default */
9b398ef2 64
1353736b 65 if (!CRYPTO_NEW_REF(&ret->references, 1)) {
9b398ef2
AG
66 OPENSSL_free(ret);
67 return NULL;
68 }
0f113f3e
MC
69 return ret;
70}
37c660ff 71
3aef36ff 72EC_PRE_COMP *EC_ec_pre_comp_dup(EC_PRE_COMP *pre)
0f113f3e 73{
9b398ef2 74 int i;
3aef36ff 75 if (pre != NULL)
1353736b 76 CRYPTO_UP_REF(&pre->references, &i);
3aef36ff 77 return pre;
0f113f3e 78}
37c660ff 79
3aef36ff 80void EC_ec_pre_comp_free(EC_PRE_COMP *pre)
0f113f3e 81{
9b398ef2
AG
82 int i;
83
84 if (pre == NULL)
85 return;
86
1353736b 87 CRYPTO_DOWN_REF(&pre->references, &i);
9b398ef2
AG
88 REF_PRINT_COUNT("EC_ec", pre);
89 if (i > 0)
0f113f3e 90 return;
9b398ef2 91 REF_ASSERT_ISNT(i < 0);
ba729265 92
3aef36ff
RS
93 if (pre->points != NULL) {
94 EC_POINT **pts;
37c660ff 95
3aef36ff
RS
96 for (pts = pre->points; *pts != NULL; pts++)
97 EC_POINT_free(*pts);
0f113f3e
MC
98 OPENSSL_free(pre->points);
99 }
1353736b 100 CRYPTO_FREE_REF(&pre->references);
0f113f3e
MC
101 OPENSSL_free(pre);
102}
37c660ff 103
36bed230 104#define EC_POINT_BN_set_flags(P, flags) do { \
40e48e54
BB
105 BN_set_flags((P)->X, (flags)); \
106 BN_set_flags((P)->Y, (flags)); \
107 BN_set_flags((P)->Z, (flags)); \
108} while(0)
109
f4675379 110/*-
37124360
NT
111 * This functions computes a single point multiplication over the EC group,
112 * using, at a high level, a Montgomery ladder with conditional swaps, with
113 * various timing attack defenses.
a067a870 114 *
fe2d3975 115 * It performs either a fixed point multiplication
40e48e54 116 * (scalar * generator)
fe2d3975 117 * when point is NULL, or a variable point multiplication
40e48e54
BB
118 * (scalar * point)
119 * when point is not NULL.
120 *
37124360
NT
121 * `scalar` cannot be NULL and should be in the range [0,n) otherwise all
122 * constant time bets are off (where n is the cardinality of the EC group).
40e48e54 123 *
01ad66f8
NT
124 * This function expects `group->order` and `group->cardinality` to be well
125 * defined and non-zero: it fails with an error code otherwise.
126 *
37124360
NT
127 * NB: This says nothing about the constant-timeness of the ladder step
128 * implementation (i.e., the default implementation is based on EC_POINT_add and
129 * EC_POINT_dbl, which of course are not constant time themselves) or the
130 * underlying multiprecision arithmetic.
40e48e54 131 *
37124360 132 * The product is stored in `r`.
40e48e54 133 *
01ad66f8
NT
134 * This is an internal function: callers are in charge of ensuring that the
135 * input parameters `group`, `r`, `scalar` and `ctx` are not NULL.
136 *
40e48e54
BB
137 * Returns 1 on success, 0 otherwise.
138 */
32ab57cb
SL
139int ossl_ec_scalar_mul_ladder(const EC_GROUP *group, EC_POINT *r,
140 const BIGNUM *scalar, const EC_POINT *point,
141 BN_CTX *ctx)
40e48e54 142{
a766aab9 143 int i, cardinality_bits, group_top, kbit, pbit, Z_is_one;
37124360 144 EC_POINT *p = NULL;
40e48e54
BB
145 EC_POINT *s = NULL;
146 BIGNUM *k = NULL;
147 BIGNUM *lambda = NULL;
a766aab9 148 BIGNUM *cardinality = NULL;
36bed230 149 int ret = 0;
40e48e54 150
37124360
NT
151 /* early exit if the input point is the point at infinity */
152 if (point != NULL && EC_POINT_is_at_infinity(group, point))
153 return EC_POINT_set_to_infinity(group, r);
154
01ad66f8 155 if (BN_is_zero(group->order)) {
9311d0c4 156 ERR_raise(ERR_LIB_EC, EC_R_UNKNOWN_ORDER);
7d859d1c 157 return 0;
01ad66f8 158 }
ac2b52c6 159 if (BN_is_zero(group->cofactor)) {
9311d0c4 160 ERR_raise(ERR_LIB_EC, EC_R_UNKNOWN_COFACTOR);
01ad66f8
NT
161 return 0;
162 }
7d859d1c
AP
163
164 BN_CTX_start(ctx);
40e48e54 165
37124360
NT
166 if (((p = EC_POINT_new(group)) == NULL)
167 || ((s = EC_POINT_new(group)) == NULL)) {
e077455e 168 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
40e48e54 169 goto err;
37124360 170 }
40e48e54
BB
171
172 if (point == NULL) {
37124360 173 if (!EC_POINT_copy(p, group->generator)) {
9311d0c4 174 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
40e48e54 175 goto err;
37124360 176 }
40e48e54 177 } else {
37124360 178 if (!EC_POINT_copy(p, point)) {
9311d0c4 179 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
40e48e54 180 goto err;
37124360 181 }
40e48e54
BB
182 }
183
37124360
NT
184 EC_POINT_BN_set_flags(p, BN_FLG_CONSTTIME);
185 EC_POINT_BN_set_flags(r, BN_FLG_CONSTTIME);
36bed230 186 EC_POINT_BN_set_flags(s, BN_FLG_CONSTTIME);
40e48e54 187
a766aab9 188 cardinality = BN_CTX_get(ctx);
40e48e54
BB
189 lambda = BN_CTX_get(ctx);
190 k = BN_CTX_get(ctx);
37124360 191 if (k == NULL) {
e077455e 192 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
37124360
NT
193 goto err;
194 }
195
196 if (!BN_mul(cardinality, group->order, group->cofactor, ctx)) {
9311d0c4 197 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
40e48e54 198 goto err;
37124360 199 }
40e48e54
BB
200
201 /*
a766aab9 202 * Group cardinalities are often on a word boundary.
40e48e54
BB
203 * So when we pad the scalar, some timing diff might
204 * pop if it needs to be expanded due to carries.
205 * So expand ahead of time.
206 */
a766aab9
BB
207 cardinality_bits = BN_num_bits(cardinality);
208 group_top = bn_get_top(cardinality);
99540ec7
P
209 if ((bn_wexpand(k, group_top + 2) == NULL)
210 || (bn_wexpand(lambda, group_top + 2) == NULL)) {
9311d0c4 211 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
40e48e54 212 goto err;
37124360 213 }
40e48e54 214
37124360 215 if (!BN_copy(k, scalar)) {
9311d0c4 216 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
40e48e54 217 goto err;
37124360 218 }
40e48e54
BB
219
220 BN_set_flags(k, BN_FLG_CONSTTIME);
221
a766aab9 222 if ((BN_num_bits(k) > cardinality_bits) || (BN_is_negative(k))) {
f4675379 223 /*-
40e48e54
BB
224 * this is an unusual input, and we don't guarantee
225 * constant-timeness
226 */
37124360 227 if (!BN_nnmod(k, k, cardinality, ctx)) {
9311d0c4 228 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
40e48e54 229 goto err;
37124360 230 }
40e48e54
BB
231 }
232
37124360 233 if (!BN_add(lambda, k, cardinality)) {
9311d0c4 234 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
40e48e54 235 goto err;
37124360 236 }
40e48e54 237 BN_set_flags(lambda, BN_FLG_CONSTTIME);
37124360 238 if (!BN_add(k, lambda, cardinality)) {
9311d0c4 239 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
40e48e54 240 goto err;
37124360 241 }
40e48e54 242 /*
a766aab9
BB
243 * lambda := scalar + cardinality
244 * k := scalar + 2*cardinality
40e48e54 245 */
a766aab9 246 kbit = BN_is_bit_set(lambda, cardinality_bits);
99540ec7 247 BN_consttime_swap(kbit, k, lambda, group_top + 2);
40e48e54
BB
248
249 group_top = bn_get_top(group->field);
250 if ((bn_wexpand(s->X, group_top) == NULL)
251 || (bn_wexpand(s->Y, group_top) == NULL)
252 || (bn_wexpand(s->Z, group_top) == NULL)
253 || (bn_wexpand(r->X, group_top) == NULL)
254 || (bn_wexpand(r->Y, group_top) == NULL)
37124360
NT
255 || (bn_wexpand(r->Z, group_top) == NULL)
256 || (bn_wexpand(p->X, group_top) == NULL)
257 || (bn_wexpand(p->Y, group_top) == NULL)
258 || (bn_wexpand(p->Z, group_top) == NULL)) {
9311d0c4 259 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
40e48e54 260 goto err;
37124360 261 }
40e48e54 262
a4a93bbf 263 /* ensure input point is in affine coords for ladder step efficiency */
c2f2db9b
BB
264 if (!p->Z_is_one && (group->meth->make_affine == NULL
265 || !group->meth->make_affine(group, p, ctx))) {
9311d0c4 266 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
a4a93bbf 267 goto err;
37124360 268 }
40e48e54 269
37124360
NT
270 /* Initialize the Montgomery ladder */
271 if (!ec_point_ladder_pre(group, r, s, p, ctx)) {
9311d0c4 272 ERR_raise(ERR_LIB_EC, EC_R_LADDER_PRE_FAILURE);
40e48e54 273 goto err;
37124360 274 }
40e48e54 275
37124360
NT
276 /* top bit is a 1, in a fixed pos */
277 pbit = 1;
40e48e54
BB
278
279#define EC_POINT_CSWAP(c, a, b, w, t) do { \
280 BN_consttime_swap(c, (a)->X, (b)->X, w); \
281 BN_consttime_swap(c, (a)->Y, (b)->Y, w); \
282 BN_consttime_swap(c, (a)->Z, (b)->Z, w); \
283 t = ((a)->Z_is_one ^ (b)->Z_is_one) & (c); \
284 (a)->Z_is_one ^= (t); \
285 (b)->Z_is_one ^= (t); \
286} while(0)
287
f4675379 288 /*-
a067a870
BB
289 * The ladder step, with branches, is
290 *
291 * k[i] == 0: S = add(R, S), R = dbl(R)
292 * k[i] == 1: R = add(S, R), S = dbl(S)
293 *
294 * Swapping R, S conditionally on k[i] leaves you with state
295 *
296 * k[i] == 0: T, U = R, S
297 * k[i] == 1: T, U = S, R
298 *
299 * Then perform the ECC ops.
300 *
301 * U = add(T, U)
302 * T = dbl(T)
303 *
304 * Which leaves you with state
305 *
306 * k[i] == 0: U = add(R, S), T = dbl(R)
307 * k[i] == 1: U = add(S, R), T = dbl(S)
308 *
309 * Swapping T, U conditionally on k[i] leaves you with state
310 *
311 * k[i] == 0: R, S = T, U
312 * k[i] == 1: R, S = U, T
313 *
314 * Which leaves you with state
315 *
316 * k[i] == 0: S = add(R, S), R = dbl(R)
317 * k[i] == 1: R = add(S, R), S = dbl(S)
318 *
319 * So we get the same logic, but instead of a branch it's a
320 * conditional swap, followed by ECC ops, then another conditional swap.
321 *
322 * Optimization: The end of iteration i and start of i-1 looks like
323 *
324 * ...
325 * CSWAP(k[i], R, S)
326 * ECC
327 * CSWAP(k[i], R, S)
328 * (next iteration)
329 * CSWAP(k[i-1], R, S)
330 * ECC
331 * CSWAP(k[i-1], R, S)
332 * ...
333 *
334 * So instead of two contiguous swaps, you can merge the condition
335 * bits and do a single swap.
336 *
f4675379
NT
337 * k[i] k[i-1] Outcome
338 * 0 0 No Swap
339 * 0 1 Swap
340 * 1 0 Swap
341 * 1 1 No Swap
a067a870
BB
342 *
343 * This is XOR. pbit tracks the previous bit of k.
344 */
345
a766aab9 346 for (i = cardinality_bits - 1; i >= 0; i--) {
40e48e54
BB
347 kbit = BN_is_bit_set(k, i) ^ pbit;
348 EC_POINT_CSWAP(kbit, r, s, group_top, Z_is_one);
37124360
NT
349
350 /* Perform a single step of the Montgomery ladder */
351 if (!ec_point_ladder_step(group, r, s, p, ctx)) {
9311d0c4 352 ERR_raise(ERR_LIB_EC, EC_R_LADDER_STEP_FAILURE);
40e48e54 353 goto err;
37124360 354 }
40e48e54
BB
355 /*
356 * pbit logic merges this cswap with that of the
357 * next iteration
358 */
359 pbit ^= kbit;
360 }
361 /* one final cswap to move the right value into r */
362 EC_POINT_CSWAP(pbit, r, s, group_top, Z_is_one);
363#undef EC_POINT_CSWAP
364
37124360
NT
365 /* Finalize ladder (and recover full point coordinates) */
366 if (!ec_point_ladder_post(group, r, s, p, ctx)) {
9311d0c4 367 ERR_raise(ERR_LIB_EC, EC_R_LADDER_POST_FAILURE);
37124360
NT
368 goto err;
369 }
370
40e48e54
BB
371 ret = 1;
372
f4675379 373 err:
37124360 374 EC_POINT_free(p);
8a74bb5c 375 EC_POINT_clear_free(s);
40e48e54 376 BN_CTX_end(ctx);
40e48e54
BB
377
378 return ret;
379}
f4675379 380
36bed230 381#undef EC_POINT_BN_set_flags
40e48e54 382
0f113f3e 383/*
4656d9ec 384 * Table could be optimised for the wNAF-based implementation,
0f113f3e
MC
385 * sometimes smaller windows will give better performance (thus the
386 * boundaries should be increased)
c05940ed 387 */
3ba1f111 388#define EC_window_bits_for_scalar_size(b) \
0f113f3e
MC
389 ((size_t) \
390 ((b) >= 2000 ? 6 : \
391 (b) >= 800 ? 5 : \
392 (b) >= 300 ? 4 : \
393 (b) >= 70 ? 3 : \
394 (b) >= 20 ? 2 : \
395 1))
3ba1f111 396
c80fd6b2
MC
397/*-
398 * Compute
3ba1f111
BM
399 * \sum scalars[i]*points[i],
400 * also including
401 * scalar*generator
402 * in the addition if scalar != NULL
403 */
32ab57cb
SL
404int ossl_ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
405 size_t num, const EC_POINT *points[],
406 const BIGNUM *scalars[], BN_CTX *ctx)
0f113f3e 407{
0f113f3e
MC
408 const EC_POINT *generator = NULL;
409 EC_POINT *tmp = NULL;
410 size_t totalnum;
411 size_t blocksize = 0, numblocks = 0; /* for wNAF splitting */
412 size_t pre_points_per_block = 0;
413 size_t i, j;
414 int k;
415 int r_is_inverted = 0;
416 int r_is_at_infinity = 1;
417 size_t *wsize = NULL; /* individual window sizes */
418 signed char **wNAF = NULL; /* individual wNAFs */
419 size_t *wNAF_len = NULL;
420 size_t max_len = 0;
421 size_t num_val;
422 EC_POINT **val = NULL; /* precomputation */
423 EC_POINT **v;
424 EC_POINT ***val_sub = NULL; /* pointers to sub-arrays of 'val' or
425 * 'pre_comp->points' */
426 const EC_PRE_COMP *pre_comp = NULL;
427 int num_scalar = 0; /* flag: will be set to 1 if 'scalar' must be
428 * treated like other scalars, i.e.
429 * precomputation is not available */
430 int ret = 0;
431
de72274d 432 if (!BN_is_zero(group->order) && !BN_is_zero(group->cofactor)) {
f4675379 433 /*-
37124360
NT
434 * Handle the common cases where the scalar is secret, enforcing a
435 * scalar multiplication implementation based on a Montgomery ladder,
436 * with various timing attack defenses.
36bed230 437 */
3051bf2a 438 if ((scalar != group->order) && (scalar != NULL) && (num == 0)) {
de72274d
BB
439 /*-
440 * In this case we want to compute scalar * GeneratorPoint: this
37124360
NT
441 * codepath is reached most prominently by (ephemeral) key
442 * generation of EC cryptosystems (i.e. ECDSA keygen and sign setup,
443 * ECDH keygen/first half), where the scalar is always secret. This
444 * is why we ignore if BN_FLG_CONSTTIME is actually set and we
445 * always call the ladder version.
de72274d 446 */
32ab57cb 447 return ossl_ec_scalar_mul_ladder(group, r, scalar, NULL, ctx);
de72274d 448 }
3051bf2a 449 if ((scalar == NULL) && (num == 1) && (scalars[0] != group->order)) {
de72274d 450 /*-
37124360
NT
451 * In this case we want to compute scalar * VariablePoint: this
452 * codepath is reached most prominently by the second half of ECDH,
453 * where the secret scalar is multiplied by the peer's public point.
454 * To protect the secret scalar, we ignore if BN_FLG_CONSTTIME is
455 * actually set and we always call the ladder version.
de72274d 456 */
32ab57cb
SL
457 return ossl_ec_scalar_mul_ladder(group, r, scalars[0], points[0],
458 ctx);
de72274d 459 }
36bed230
NT
460 }
461
0f113f3e
MC
462 if (scalar != NULL) {
463 generator = EC_GROUP_get0_generator(group);
464 if (generator == NULL) {
9311d0c4 465 ERR_raise(ERR_LIB_EC, EC_R_UNDEFINED_GENERATOR);
0f113f3e
MC
466 goto err;
467 }
468
469 /* look if we can use precomputed multiples of generator */
470
3aef36ff 471 pre_comp = group->pre_comp.ec;
0f113f3e
MC
472 if (pre_comp && pre_comp->numblocks
473 && (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) ==
474 0)) {
475 blocksize = pre_comp->blocksize;
476
477 /*
478 * determine maximum number of blocks that wNAF splitting may
479 * yield (NB: maximum wNAF length is bit length plus one)
480 */
481 numblocks = (BN_num_bits(scalar) / blocksize) + 1;
482
483 /*
484 * we cannot use more blocks than we have precomputation for
485 */
486 if (numblocks > pre_comp->numblocks)
487 numblocks = pre_comp->numblocks;
488
489 pre_points_per_block = (size_t)1 << (pre_comp->w - 1);
490
491 /* check that pre_comp looks sane */
492 if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block)) {
9311d0c4 493 ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
0f113f3e
MC
494 goto err;
495 }
496 } else {
497 /* can't use precomputation */
498 pre_comp = NULL;
499 numblocks = 1;
500 num_scalar = 1; /* treat 'scalar' like 'num'-th element of
501 * 'scalars' */
502 }
503 }
504
505 totalnum = num + numblocks;
506
cbe29648
RS
507 wsize = OPENSSL_malloc(totalnum * sizeof(wsize[0]));
508 wNAF_len = OPENSSL_malloc(totalnum * sizeof(wNAF_len[0]));
509 /* include space for pivot */
510 wNAF = OPENSSL_malloc((totalnum + 1) * sizeof(wNAF[0]));
511 val_sub = OPENSSL_malloc(totalnum * sizeof(val_sub[0]));
0f113f3e
MC
512
513 /* Ensure wNAF is initialised in case we end up going to err */
90945fa3 514 if (wNAF != NULL)
0f113f3e
MC
515 wNAF[0] = NULL; /* preliminary pivot */
516
e077455e 517 if (wsize == NULL || wNAF_len == NULL || wNAF == NULL || val_sub == NULL)
0f113f3e 518 goto err;
0f113f3e
MC
519
520 /*
521 * num_val will be the total number of temporarily precomputed points
522 */
523 num_val = 0;
524
525 for (i = 0; i < num + num_scalar; i++) {
526 size_t bits;
527
528 bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar);
529 wsize[i] = EC_window_bits_for_scalar_size(bits);
530 num_val += (size_t)1 << (wsize[i] - 1);
531 wNAF[i + 1] = NULL; /* make sure we always have a pivot */
532 wNAF[i] =
533 bn_compute_wNAF((i < num ? scalars[i] : scalar), wsize[i],
534 &wNAF_len[i]);
535 if (wNAF[i] == NULL)
536 goto err;
537 if (wNAF_len[i] > max_len)
538 max_len = wNAF_len[i];
539 }
540
541 if (numblocks) {
542 /* we go here iff scalar != NULL */
543
544 if (pre_comp == NULL) {
545 if (num_scalar != 1) {
9311d0c4 546 ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
0f113f3e
MC
547 goto err;
548 }
549 /* we have already generated a wNAF for 'scalar' */
550 } else {
551 signed char *tmp_wNAF = NULL;
552 size_t tmp_len = 0;
553
554 if (num_scalar != 0) {
9311d0c4 555 ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
0f113f3e
MC
556 goto err;
557 }
558
559 /*
560 * use the window size for which we have precomputation
561 */
562 wsize[num] = pre_comp->w;
563 tmp_wNAF = bn_compute_wNAF(scalar, wsize[num], &tmp_len);
564 if (!tmp_wNAF)
565 goto err;
566
567 if (tmp_len <= max_len) {
568 /*
569 * One of the other wNAFs is at least as long as the wNAF
570 * belonging to the generator, so wNAF splitting will not buy
571 * us anything.
572 */
573
574 numblocks = 1;
575 totalnum = num + 1; /* don't use wNAF splitting */
576 wNAF[num] = tmp_wNAF;
577 wNAF[num + 1] = NULL;
578 wNAF_len[num] = tmp_len;
0f113f3e
MC
579 /*
580 * pre_comp->points starts with the points that we need here:
581 */
582 val_sub[num] = pre_comp->points;
583 } else {
584 /*
585 * don't include tmp_wNAF directly into wNAF array - use wNAF
586 * splitting and include the blocks
587 */
588
589 signed char *pp;
590 EC_POINT **tmp_points;
591
592 if (tmp_len < numblocks * blocksize) {
593 /*
594 * possibly we can do with fewer blocks than estimated
595 */
596 numblocks = (tmp_len + blocksize - 1) / blocksize;
597 if (numblocks > pre_comp->numblocks) {
9311d0c4 598 ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
0e9eb1a5 599 OPENSSL_free(tmp_wNAF);
0f113f3e
MC
600 goto err;
601 }
602 totalnum = num + numblocks;
603 }
604
605 /* split wNAF in 'numblocks' parts */
606 pp = tmp_wNAF;
607 tmp_points = pre_comp->points;
608
609 for (i = num; i < totalnum; i++) {
610 if (i < totalnum - 1) {
611 wNAF_len[i] = blocksize;
612 if (tmp_len < blocksize) {
9311d0c4 613 ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
0e9eb1a5 614 OPENSSL_free(tmp_wNAF);
0f113f3e
MC
615 goto err;
616 }
617 tmp_len -= blocksize;
618 } else
619 /*
620 * last block gets whatever is left (this could be
621 * more or less than 'blocksize'!)
622 */
623 wNAF_len[i] = tmp_len;
624
625 wNAF[i + 1] = NULL;
626 wNAF[i] = OPENSSL_malloc(wNAF_len[i]);
627 if (wNAF[i] == NULL) {
0f113f3e
MC
628 OPENSSL_free(tmp_wNAF);
629 goto err;
630 }
631 memcpy(wNAF[i], pp, wNAF_len[i]);
632 if (wNAF_len[i] > max_len)
633 max_len = wNAF_len[i];
634
635 if (*tmp_points == NULL) {
9311d0c4 636 ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
0f113f3e
MC
637 OPENSSL_free(tmp_wNAF);
638 goto err;
639 }
640 val_sub[i] = tmp_points;
641 tmp_points += pre_points_per_block;
642 pp += blocksize;
643 }
644 OPENSSL_free(tmp_wNAF);
645 }
646 }
647 }
648
649 /*
650 * All points we precompute now go into a single array 'val'.
651 * 'val_sub[i]' is a pointer to the subarray for the i-th point, or to a
652 * subarray of 'pre_comp->points' if we already have precomputation.
653 */
cbe29648 654 val = OPENSSL_malloc((num_val + 1) * sizeof(val[0]));
e077455e 655 if (val == NULL)
0f113f3e 656 goto err;
0f113f3e
MC
657 val[num_val] = NULL; /* pivot element */
658
659 /* allocate points for precomputation */
660 v = val;
661 for (i = 0; i < num + num_scalar; i++) {
662 val_sub[i] = v;
663 for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) {
664 *v = EC_POINT_new(group);
665 if (*v == NULL)
666 goto err;
667 v++;
668 }
669 }
670 if (!(v == val + num_val)) {
9311d0c4 671 ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
0f113f3e
MC
672 goto err;
673 }
674
75ebbd9a 675 if ((tmp = EC_POINT_new(group)) == NULL)
0f113f3e
MC
676 goto err;
677
50e735f9
MC
678 /*-
679 * prepare precomputed values:
680 * val_sub[i][0] := points[i]
681 * val_sub[i][1] := 3 * points[i]
682 * val_sub[i][2] := 5 * points[i]
683 * ...
684 */
0f113f3e
MC
685 for (i = 0; i < num + num_scalar; i++) {
686 if (i < num) {
687 if (!EC_POINT_copy(val_sub[i][0], points[i]))
688 goto err;
689 } else {
690 if (!EC_POINT_copy(val_sub[i][0], generator))
691 goto err;
692 }
693
694 if (wsize[i] > 1) {
695 if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx))
696 goto err;
697 for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) {
698 if (!EC_POINT_add
699 (group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx))
700 goto err;
701 }
702 }
703 }
704
c2f2db9b
BB
705 if (group->meth->points_make_affine == NULL
706 || !group->meth->points_make_affine(group, num_val, val, ctx))
0f113f3e 707 goto err;
3ba1f111 708
0f113f3e
MC
709 r_is_at_infinity = 1;
710
711 for (k = max_len - 1; k >= 0; k--) {
712 if (!r_is_at_infinity) {
713 if (!EC_POINT_dbl(group, r, r, ctx))
714 goto err;
715 }
716
717 for (i = 0; i < totalnum; i++) {
718 if (wNAF_len[i] > (size_t)k) {
719 int digit = wNAF[i][k];
720 int is_neg;
721
722 if (digit) {
723 is_neg = digit < 0;
724
725 if (is_neg)
726 digit = -digit;
727
728 if (is_neg != r_is_inverted) {
729 if (!r_is_at_infinity) {
730 if (!EC_POINT_invert(group, r, ctx))
731 goto err;
732 }
733 r_is_inverted = !r_is_inverted;
734 }
735
736 /* digit > 0 */
737
738 if (r_is_at_infinity) {
739 if (!EC_POINT_copy(r, val_sub[i][digit >> 1]))
740 goto err;
c61ced5e
BB
741
742 /*-
743 * Apply coordinate blinding for EC_POINT.
744 *
745 * The underlying EC_METHOD can optionally implement this function:
32ab57cb 746 * ossl_ec_point_blind_coordinates() returns 0 in case of errors or 1 on
c61ced5e
BB
747 * success or if coordinate blinding is not implemented for this
748 * group.
749 */
32ab57cb 750 if (!ossl_ec_point_blind_coordinates(group, r, ctx)) {
9311d0c4 751 ERR_raise(ERR_LIB_EC, EC_R_POINT_COORDINATES_BLIND_FAILURE);
c61ced5e
BB
752 goto err;
753 }
754
0f113f3e
MC
755 r_is_at_infinity = 0;
756 } else {
757 if (!EC_POINT_add
758 (group, r, r, val_sub[i][digit >> 1], ctx))
759 goto err;
760 }
761 }
762 }
763 }
764 }
765
766 if (r_is_at_infinity) {
767 if (!EC_POINT_set_to_infinity(group, r))
768 goto err;
769 } else {
770 if (r_is_inverted)
771 if (!EC_POINT_invert(group, r, ctx))
772 goto err;
773 }
774
775 ret = 1;
3ba1f111
BM
776
777 err:
8fdc3734 778 EC_POINT_free(tmp);
b548a1f1
RS
779 OPENSSL_free(wsize);
780 OPENSSL_free(wNAF_len);
0f113f3e
MC
781 if (wNAF != NULL) {
782 signed char **w;
783
784 for (w = wNAF; *w != NULL; w++)
785 OPENSSL_free(*w);
786
787 OPENSSL_free(wNAF);
788 }
789 if (val != NULL) {
790 for (v = val; *v != NULL; v++)
791 EC_POINT_clear_free(*v);
792
793 OPENSSL_free(val);
794 }
b548a1f1 795 OPENSSL_free(val_sub);
0f113f3e
MC
796 return ret;
797}
38374911 798
1d97c843 799/*-
32ab57cb 800 * ossl_ec_wNAF_precompute_mult()
37c660ff 801 * creates an EC_PRE_COMP object with preprecomputed multiples of the generator
32ab57cb 802 * for use with wNAF splitting as implemented in ossl_ec_wNAF_mul().
0f113f3e 803 *
37c660ff
BM
804 * 'pre_comp->points' is an array of multiples of the generator
805 * of the following form:
806 * points[0] = generator;
807 * points[1] = 3 * generator;
808 * ...
809 * points[2^(w-1)-1] = (2^(w-1)-1) * generator;
810 * points[2^(w-1)] = 2^blocksize * generator;
811 * points[2^(w-1)+1] = 3 * 2^blocksize * generator;
812 * ...
813 * points[2^(w-1)*(numblocks-1)-1] = (2^(w-1)) * 2^(blocksize*(numblocks-2)) * generator
814 * points[2^(w-1)*(numblocks-1)] = 2^(blocksize*(numblocks-1)) * generator
815 * ...
816 * points[2^(w-1)*numblocks-1] = (2^(w-1)) * 2^(blocksize*(numblocks-1)) * generator
817 * points[2^(w-1)*numblocks] = NULL
7793f30e 818 */
32ab57cb 819int ossl_ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
0f113f3e
MC
820{
821 const EC_POINT *generator;
822 EC_POINT *tmp_point = NULL, *base = NULL, **var;
be2e334f 823 const BIGNUM *order;
0f113f3e
MC
824 size_t i, bits, w, pre_points_per_block, blocksize, numblocks, num;
825 EC_POINT **points = NULL;
826 EC_PRE_COMP *pre_comp;
827 int ret = 0;
48116c2d 828 int used_ctx = 0;
f844f9eb 829#ifndef FIPS_MODULE
a9612d6c
MC
830 BN_CTX *new_ctx = NULL;
831#endif
0f113f3e
MC
832
833 /* if there is an old EC_PRE_COMP object, throw it away */
2c52ac9b 834 EC_pre_comp_free(group);
0f113f3e
MC
835 if ((pre_comp = ec_pre_comp_new(group)) == NULL)
836 return 0;
837
838 generator = EC_GROUP_get0_generator(group);
839 if (generator == NULL) {
9311d0c4 840 ERR_raise(ERR_LIB_EC, EC_R_UNDEFINED_GENERATOR);
0f113f3e
MC
841 goto err;
842 }
843
f844f9eb 844#ifndef FIPS_MODULE
a9612d6c 845 if (ctx == NULL)
0f113f3e 846 ctx = new_ctx = BN_CTX_new();
a9612d6c
MC
847#endif
848 if (ctx == NULL)
849 goto err;
0f113f3e
MC
850
851 BN_CTX_start(ctx);
48116c2d 852 used_ctx = 1;
0f113f3e 853
be2e334f
DSH
854 order = EC_GROUP_get0_order(group);
855 if (order == NULL)
0f113f3e
MC
856 goto err;
857 if (BN_is_zero(order)) {
9311d0c4 858 ERR_raise(ERR_LIB_EC, EC_R_UNKNOWN_ORDER);
0f113f3e
MC
859 goto err;
860 }
861
862 bits = BN_num_bits(order);
863 /*
864 * The following parameters mean we precompute (approximately) one point
865 * per bit. TBD: The combination 8, 4 is perfect for 160 bits; for other
866 * bit lengths, other parameter combinations might provide better
867 * efficiency.
868 */
869 blocksize = 8;
870 w = 4;
871 if (EC_window_bits_for_scalar_size(bits) > w) {
872 /* let's not make the window too small ... */
873 w = EC_window_bits_for_scalar_size(bits);
874 }
875
876 numblocks = (bits + blocksize - 1) / blocksize; /* max. number of blocks
877 * to use for wNAF
878 * splitting */
879
880 pre_points_per_block = (size_t)1 << (w - 1);
881 num = pre_points_per_block * numblocks; /* number of points to compute
882 * and store */
883
b4faea50 884 points = OPENSSL_malloc(sizeof(*points) * (num + 1));
e077455e 885 if (points == NULL)
0f113f3e 886 goto err;
0f113f3e
MC
887
888 var = points;
889 var[num] = NULL; /* pivot */
890 for (i = 0; i < num; i++) {
891 if ((var[i] = EC_POINT_new(group)) == NULL) {
e077455e 892 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
0f113f3e
MC
893 goto err;
894 }
895 }
896
75ebbd9a
RS
897 if ((tmp_point = EC_POINT_new(group)) == NULL
898 || (base = EC_POINT_new(group)) == NULL) {
e077455e 899 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
0f113f3e
MC
900 goto err;
901 }
902
903 if (!EC_POINT_copy(base, generator))
904 goto err;
905
906 /* do the precomputation */
907 for (i = 0; i < numblocks; i++) {
908 size_t j;
909
910 if (!EC_POINT_dbl(group, tmp_point, base, ctx))
911 goto err;
912
913 if (!EC_POINT_copy(*var++, base))
914 goto err;
915
916 for (j = 1; j < pre_points_per_block; j++, var++) {
917 /*
918 * calculate odd multiples of the current base point
919 */
920 if (!EC_POINT_add(group, *var, tmp_point, *(var - 1), ctx))
921 goto err;
922 }
923
924 if (i < numblocks - 1) {
925 /*
926 * get the next base (multiply current one by 2^blocksize)
927 */
928 size_t k;
929
930 if (blocksize <= 2) {
9311d0c4 931 ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
0f113f3e
MC
932 goto err;
933 }
934
935 if (!EC_POINT_dbl(group, base, tmp_point, ctx))
936 goto err;
937 for (k = 2; k < blocksize; k++) {
938 if (!EC_POINT_dbl(group, base, base, ctx))
939 goto err;
940 }
941 }
942 }
943
c2f2db9b
BB
944 if (group->meth->points_make_affine == NULL
945 || !group->meth->points_make_affine(group, num, points, ctx))
0f113f3e
MC
946 goto err;
947
948 pre_comp->group = group;
949 pre_comp->blocksize = blocksize;
950 pre_comp->numblocks = numblocks;
951 pre_comp->w = w;
952 pre_comp->points = points;
953 points = NULL;
954 pre_comp->num = num;
3aef36ff 955 SETPRECOMP(group, ec, pre_comp);
0f113f3e 956 pre_comp = NULL;
0f113f3e 957 ret = 1;
3aef36ff 958
38374911 959 err:
48116c2d
AG
960 if (used_ctx)
961 BN_CTX_end(ctx);
f844f9eb 962#ifndef FIPS_MODULE
23a1d5e9 963 BN_CTX_free(new_ctx);
a9612d6c 964#endif
3aef36ff 965 EC_ec_pre_comp_free(pre_comp);
0f113f3e
MC
966 if (points) {
967 EC_POINT **p;
968
969 for (p = points; *p != NULL; p++)
970 EC_POINT_free(*p);
971 OPENSSL_free(points);
972 }
8fdc3734
RS
973 EC_POINT_free(tmp_point);
974 EC_POINT_free(base);
0f113f3e
MC
975 return ret;
976}
7793f30e 977
32ab57cb 978int ossl_ec_wNAF_have_precompute_mult(const EC_GROUP *group)
0f113f3e 979{
3aef36ff 980 return HAVEPRECOMP(group, ec);
0f113f3e 981}