1*0Sstevel@tonic-gate /* crypto/ec/ec_mult.c */ 2*0Sstevel@tonic-gate /* ==================================================================== 3*0Sstevel@tonic-gate * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. 4*0Sstevel@tonic-gate * 5*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 6*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 7*0Sstevel@tonic-gate * are met: 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 10*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 11*0Sstevel@tonic-gate * 12*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 13*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in 14*0Sstevel@tonic-gate * the documentation and/or other materials provided with the 15*0Sstevel@tonic-gate * distribution. 16*0Sstevel@tonic-gate * 17*0Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this 18*0Sstevel@tonic-gate * software must display the following acknowledgment: 19*0Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project 20*0Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 21*0Sstevel@tonic-gate * 22*0Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 23*0Sstevel@tonic-gate * endorse or promote products derived from this software without 24*0Sstevel@tonic-gate * prior written permission. For written permission, please contact 25*0Sstevel@tonic-gate * openssl-core@openssl.org. 26*0Sstevel@tonic-gate * 27*0Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL" 28*0Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written 29*0Sstevel@tonic-gate * permission of the OpenSSL Project. 30*0Sstevel@tonic-gate * 31*0Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following 32*0Sstevel@tonic-gate * acknowledgment: 33*0Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project 34*0Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 35*0Sstevel@tonic-gate * 36*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 37*0Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 38*0Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 39*0Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 40*0Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 41*0Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 42*0Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 43*0Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 44*0Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 45*0Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 46*0Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 47*0Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE. 48*0Sstevel@tonic-gate * ==================================================================== 49*0Sstevel@tonic-gate * 50*0Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young 51*0Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim 52*0Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com). 53*0Sstevel@tonic-gate * 54*0Sstevel@tonic-gate */ 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate #include <openssl/err.h> 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gate #include "ec_lcl.h" 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate /* TODO: optional precomputation of multiples of the generator */ 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate 65*0Sstevel@tonic-gate /* 66*0Sstevel@tonic-gate * wNAF-based interleaving multi-exponentation method 67*0Sstevel@tonic-gate * (<URL:http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/moeller.html#multiexp>) 68*0Sstevel@tonic-gate */ 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gate /* Determine the width-(w+1) Non-Adjacent Form (wNAF) of 'scalar'. 72*0Sstevel@tonic-gate * This is an array r[] of values that are either zero or odd with an 73*0Sstevel@tonic-gate * absolute value less than 2^w satisfying 74*0Sstevel@tonic-gate * scalar = \sum_j r[j]*2^j 75*0Sstevel@tonic-gate * where at most one of any w+1 consecutive digits is non-zero. 76*0Sstevel@tonic-gate */ 77*0Sstevel@tonic-gate static signed char *compute_wNAF(const BIGNUM *scalar, int w, size_t *ret_len, BN_CTX *ctx) 78*0Sstevel@tonic-gate { 79*0Sstevel@tonic-gate BIGNUM *c; 80*0Sstevel@tonic-gate int ok = 0; 81*0Sstevel@tonic-gate signed char *r = NULL; 82*0Sstevel@tonic-gate int sign = 1; 83*0Sstevel@tonic-gate int bit, next_bit, mask; 84*0Sstevel@tonic-gate size_t len = 0, j; 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gate BN_CTX_start(ctx); 87*0Sstevel@tonic-gate c = BN_CTX_get(ctx); 88*0Sstevel@tonic-gate if (c == NULL) goto err; 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gate if (w <= 0 || w > 7) /* 'signed char' can represent integers with absolute values less than 2^7 */ 91*0Sstevel@tonic-gate { 92*0Sstevel@tonic-gate ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR); 93*0Sstevel@tonic-gate goto err; 94*0Sstevel@tonic-gate } 95*0Sstevel@tonic-gate bit = 1 << w; /* at most 128 */ 96*0Sstevel@tonic-gate next_bit = bit << 1; /* at most 256 */ 97*0Sstevel@tonic-gate mask = next_bit - 1; /* at most 255 */ 98*0Sstevel@tonic-gate 99*0Sstevel@tonic-gate if (!BN_copy(c, scalar)) goto err; 100*0Sstevel@tonic-gate if (c->neg) 101*0Sstevel@tonic-gate { 102*0Sstevel@tonic-gate sign = -1; 103*0Sstevel@tonic-gate c->neg = 0; 104*0Sstevel@tonic-gate } 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate len = BN_num_bits(c) + 1; /* wNAF may be one digit longer than binary representation */ 107*0Sstevel@tonic-gate r = OPENSSL_malloc(len); 108*0Sstevel@tonic-gate if (r == NULL) goto err; 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gate j = 0; 111*0Sstevel@tonic-gate while (!BN_is_zero(c)) 112*0Sstevel@tonic-gate { 113*0Sstevel@tonic-gate int u = 0; 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gate if (BN_is_odd(c)) 116*0Sstevel@tonic-gate { 117*0Sstevel@tonic-gate if (c->d == NULL || c->top == 0) 118*0Sstevel@tonic-gate { 119*0Sstevel@tonic-gate ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR); 120*0Sstevel@tonic-gate goto err; 121*0Sstevel@tonic-gate } 122*0Sstevel@tonic-gate u = c->d[0] & mask; 123*0Sstevel@tonic-gate if (u & bit) 124*0Sstevel@tonic-gate { 125*0Sstevel@tonic-gate u -= next_bit; 126*0Sstevel@tonic-gate /* u < 0 */ 127*0Sstevel@tonic-gate if (!BN_add_word(c, -u)) goto err; 128*0Sstevel@tonic-gate } 129*0Sstevel@tonic-gate else 130*0Sstevel@tonic-gate { 131*0Sstevel@tonic-gate /* u > 0 */ 132*0Sstevel@tonic-gate if (!BN_sub_word(c, u)) goto err; 133*0Sstevel@tonic-gate } 134*0Sstevel@tonic-gate 135*0Sstevel@tonic-gate if (u <= -bit || u >= bit || !(u & 1) || c->neg) 136*0Sstevel@tonic-gate { 137*0Sstevel@tonic-gate ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR); 138*0Sstevel@tonic-gate goto err; 139*0Sstevel@tonic-gate } 140*0Sstevel@tonic-gate } 141*0Sstevel@tonic-gate 142*0Sstevel@tonic-gate r[j++] = sign * u; 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gate if (BN_is_odd(c)) 145*0Sstevel@tonic-gate { 146*0Sstevel@tonic-gate ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR); 147*0Sstevel@tonic-gate goto err; 148*0Sstevel@tonic-gate } 149*0Sstevel@tonic-gate if (!BN_rshift1(c, c)) goto err; 150*0Sstevel@tonic-gate } 151*0Sstevel@tonic-gate 152*0Sstevel@tonic-gate if (j > len) 153*0Sstevel@tonic-gate { 154*0Sstevel@tonic-gate ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR); 155*0Sstevel@tonic-gate goto err; 156*0Sstevel@tonic-gate } 157*0Sstevel@tonic-gate len = j; 158*0Sstevel@tonic-gate ok = 1; 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gate err: 161*0Sstevel@tonic-gate BN_CTX_end(ctx); 162*0Sstevel@tonic-gate if (!ok) 163*0Sstevel@tonic-gate { 164*0Sstevel@tonic-gate OPENSSL_free(r); 165*0Sstevel@tonic-gate r = NULL; 166*0Sstevel@tonic-gate } 167*0Sstevel@tonic-gate if (ok) 168*0Sstevel@tonic-gate *ret_len = len; 169*0Sstevel@tonic-gate return r; 170*0Sstevel@tonic-gate } 171*0Sstevel@tonic-gate 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gate /* TODO: table should be optimised for the wNAF-based implementation, 174*0Sstevel@tonic-gate * sometimes smaller windows will give better performance 175*0Sstevel@tonic-gate * (thus the boundaries should be increased) 176*0Sstevel@tonic-gate */ 177*0Sstevel@tonic-gate #define EC_window_bits_for_scalar_size(b) \ 178*0Sstevel@tonic-gate ((size_t) \ 179*0Sstevel@tonic-gate ((b) >= 2000 ? 6 : \ 180*0Sstevel@tonic-gate (b) >= 800 ? 5 : \ 181*0Sstevel@tonic-gate (b) >= 300 ? 4 : \ 182*0Sstevel@tonic-gate (b) >= 70 ? 3 : \ 183*0Sstevel@tonic-gate (b) >= 20 ? 2 : \ 184*0Sstevel@tonic-gate 1)) 185*0Sstevel@tonic-gate 186*0Sstevel@tonic-gate /* Compute 187*0Sstevel@tonic-gate * \sum scalars[i]*points[i], 188*0Sstevel@tonic-gate * also including 189*0Sstevel@tonic-gate * scalar*generator 190*0Sstevel@tonic-gate * in the addition if scalar != NULL 191*0Sstevel@tonic-gate */ 192*0Sstevel@tonic-gate int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar, 193*0Sstevel@tonic-gate size_t num, const EC_POINT *points[], const BIGNUM *scalars[], BN_CTX *ctx) 194*0Sstevel@tonic-gate { 195*0Sstevel@tonic-gate BN_CTX *new_ctx = NULL; 196*0Sstevel@tonic-gate EC_POINT *generator = NULL; 197*0Sstevel@tonic-gate EC_POINT *tmp = NULL; 198*0Sstevel@tonic-gate size_t totalnum; 199*0Sstevel@tonic-gate size_t i, j; 200*0Sstevel@tonic-gate int k; 201*0Sstevel@tonic-gate int r_is_inverted = 0; 202*0Sstevel@tonic-gate int r_is_at_infinity = 1; 203*0Sstevel@tonic-gate size_t *wsize = NULL; /* individual window sizes */ 204*0Sstevel@tonic-gate signed char **wNAF = NULL; /* individual wNAFs */ 205*0Sstevel@tonic-gate size_t *wNAF_len = NULL; 206*0Sstevel@tonic-gate size_t max_len = 0; 207*0Sstevel@tonic-gate size_t num_val; 208*0Sstevel@tonic-gate EC_POINT **val = NULL; /* precomputation */ 209*0Sstevel@tonic-gate EC_POINT **v; 210*0Sstevel@tonic-gate EC_POINT ***val_sub = NULL; /* pointers to sub-arrays of 'val' */ 211*0Sstevel@tonic-gate int ret = 0; 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate if (group->meth != r->meth) 214*0Sstevel@tonic-gate { 215*0Sstevel@tonic-gate ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS); 216*0Sstevel@tonic-gate return 0; 217*0Sstevel@tonic-gate } 218*0Sstevel@tonic-gate 219*0Sstevel@tonic-gate if ((scalar == NULL) && (num == 0)) 220*0Sstevel@tonic-gate { 221*0Sstevel@tonic-gate return EC_POINT_set_to_infinity(group, r); 222*0Sstevel@tonic-gate } 223*0Sstevel@tonic-gate 224*0Sstevel@tonic-gate if (scalar != NULL) 225*0Sstevel@tonic-gate { 226*0Sstevel@tonic-gate generator = EC_GROUP_get0_generator(group); 227*0Sstevel@tonic-gate if (generator == NULL) 228*0Sstevel@tonic-gate { 229*0Sstevel@tonic-gate ECerr(EC_F_EC_POINTS_MUL, EC_R_UNDEFINED_GENERATOR); 230*0Sstevel@tonic-gate return 0; 231*0Sstevel@tonic-gate } 232*0Sstevel@tonic-gate } 233*0Sstevel@tonic-gate 234*0Sstevel@tonic-gate for (i = 0; i < num; i++) 235*0Sstevel@tonic-gate { 236*0Sstevel@tonic-gate if (group->meth != points[i]->meth) 237*0Sstevel@tonic-gate { 238*0Sstevel@tonic-gate ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS); 239*0Sstevel@tonic-gate return 0; 240*0Sstevel@tonic-gate } 241*0Sstevel@tonic-gate } 242*0Sstevel@tonic-gate 243*0Sstevel@tonic-gate totalnum = num + (scalar != NULL); 244*0Sstevel@tonic-gate 245*0Sstevel@tonic-gate wsize = OPENSSL_malloc(totalnum * sizeof wsize[0]); 246*0Sstevel@tonic-gate wNAF_len = OPENSSL_malloc(totalnum * sizeof wNAF_len[0]); 247*0Sstevel@tonic-gate wNAF = OPENSSL_malloc((totalnum + 1) * sizeof wNAF[0]); 248*0Sstevel@tonic-gate if (wNAF != NULL) 249*0Sstevel@tonic-gate { 250*0Sstevel@tonic-gate wNAF[0] = NULL; /* preliminary pivot */ 251*0Sstevel@tonic-gate } 252*0Sstevel@tonic-gate if (wsize == NULL || wNAF_len == NULL || wNAF == NULL) goto err; 253*0Sstevel@tonic-gate 254*0Sstevel@tonic-gate /* num_val := total number of points to precompute */ 255*0Sstevel@tonic-gate num_val = 0; 256*0Sstevel@tonic-gate for (i = 0; i < totalnum; i++) 257*0Sstevel@tonic-gate { 258*0Sstevel@tonic-gate size_t bits; 259*0Sstevel@tonic-gate 260*0Sstevel@tonic-gate bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar); 261*0Sstevel@tonic-gate wsize[i] = EC_window_bits_for_scalar_size(bits); 262*0Sstevel@tonic-gate num_val += 1u << (wsize[i] - 1); 263*0Sstevel@tonic-gate } 264*0Sstevel@tonic-gate 265*0Sstevel@tonic-gate /* all precomputed points go into a single array 'val', 266*0Sstevel@tonic-gate * 'val_sub[i]' is a pointer to the subarray for the i-th point */ 267*0Sstevel@tonic-gate val = OPENSSL_malloc((num_val + 1) * sizeof val[0]); 268*0Sstevel@tonic-gate if (val == NULL) goto err; 269*0Sstevel@tonic-gate val[num_val] = NULL; /* pivot element */ 270*0Sstevel@tonic-gate 271*0Sstevel@tonic-gate val_sub = OPENSSL_malloc(totalnum * sizeof val_sub[0]); 272*0Sstevel@tonic-gate if (val_sub == NULL) goto err; 273*0Sstevel@tonic-gate 274*0Sstevel@tonic-gate /* allocate points for precomputation */ 275*0Sstevel@tonic-gate v = val; 276*0Sstevel@tonic-gate for (i = 0; i < totalnum; i++) 277*0Sstevel@tonic-gate { 278*0Sstevel@tonic-gate val_sub[i] = v; 279*0Sstevel@tonic-gate for (j = 0; j < (1u << (wsize[i] - 1)); j++) 280*0Sstevel@tonic-gate { 281*0Sstevel@tonic-gate *v = EC_POINT_new(group); 282*0Sstevel@tonic-gate if (*v == NULL) goto err; 283*0Sstevel@tonic-gate v++; 284*0Sstevel@tonic-gate } 285*0Sstevel@tonic-gate } 286*0Sstevel@tonic-gate if (!(v == val + num_val)) 287*0Sstevel@tonic-gate { 288*0Sstevel@tonic-gate ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR); 289*0Sstevel@tonic-gate goto err; 290*0Sstevel@tonic-gate } 291*0Sstevel@tonic-gate 292*0Sstevel@tonic-gate if (ctx == NULL) 293*0Sstevel@tonic-gate { 294*0Sstevel@tonic-gate ctx = new_ctx = BN_CTX_new(); 295*0Sstevel@tonic-gate if (ctx == NULL) 296*0Sstevel@tonic-gate goto err; 297*0Sstevel@tonic-gate } 298*0Sstevel@tonic-gate 299*0Sstevel@tonic-gate tmp = EC_POINT_new(group); 300*0Sstevel@tonic-gate if (tmp == NULL) goto err; 301*0Sstevel@tonic-gate 302*0Sstevel@tonic-gate /* prepare precomputed values: 303*0Sstevel@tonic-gate * val_sub[i][0] := points[i] 304*0Sstevel@tonic-gate * val_sub[i][1] := 3 * points[i] 305*0Sstevel@tonic-gate * val_sub[i][2] := 5 * points[i] 306*0Sstevel@tonic-gate * ... 307*0Sstevel@tonic-gate */ 308*0Sstevel@tonic-gate for (i = 0; i < totalnum; i++) 309*0Sstevel@tonic-gate { 310*0Sstevel@tonic-gate if (i < num) 311*0Sstevel@tonic-gate { 312*0Sstevel@tonic-gate if (!EC_POINT_copy(val_sub[i][0], points[i])) goto err; 313*0Sstevel@tonic-gate } 314*0Sstevel@tonic-gate else 315*0Sstevel@tonic-gate { 316*0Sstevel@tonic-gate if (!EC_POINT_copy(val_sub[i][0], generator)) goto err; 317*0Sstevel@tonic-gate } 318*0Sstevel@tonic-gate 319*0Sstevel@tonic-gate if (wsize[i] > 1) 320*0Sstevel@tonic-gate { 321*0Sstevel@tonic-gate if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx)) goto err; 322*0Sstevel@tonic-gate for (j = 1; j < (1u << (wsize[i] - 1)); j++) 323*0Sstevel@tonic-gate { 324*0Sstevel@tonic-gate if (!EC_POINT_add(group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx)) goto err; 325*0Sstevel@tonic-gate } 326*0Sstevel@tonic-gate } 327*0Sstevel@tonic-gate 328*0Sstevel@tonic-gate wNAF[i + 1] = NULL; /* make sure we always have a pivot */ 329*0Sstevel@tonic-gate wNAF[i] = compute_wNAF((i < num ? scalars[i] : scalar), wsize[i], &wNAF_len[i], ctx); 330*0Sstevel@tonic-gate if (wNAF[i] == NULL) goto err; 331*0Sstevel@tonic-gate if (wNAF_len[i] > max_len) 332*0Sstevel@tonic-gate max_len = wNAF_len[i]; 333*0Sstevel@tonic-gate } 334*0Sstevel@tonic-gate 335*0Sstevel@tonic-gate #if 1 /* optional; EC_window_bits_for_scalar_size assumes we do this step */ 336*0Sstevel@tonic-gate if (!EC_POINTs_make_affine(group, num_val, val, ctx)) goto err; 337*0Sstevel@tonic-gate #endif 338*0Sstevel@tonic-gate 339*0Sstevel@tonic-gate r_is_at_infinity = 1; 340*0Sstevel@tonic-gate 341*0Sstevel@tonic-gate for (k = max_len - 1; k >= 0; k--) 342*0Sstevel@tonic-gate { 343*0Sstevel@tonic-gate if (!r_is_at_infinity) 344*0Sstevel@tonic-gate { 345*0Sstevel@tonic-gate if (!EC_POINT_dbl(group, r, r, ctx)) goto err; 346*0Sstevel@tonic-gate } 347*0Sstevel@tonic-gate 348*0Sstevel@tonic-gate for (i = 0; i < totalnum; i++) 349*0Sstevel@tonic-gate { 350*0Sstevel@tonic-gate if (wNAF_len[i] > (size_t)k) 351*0Sstevel@tonic-gate { 352*0Sstevel@tonic-gate int digit = wNAF[i][k]; 353*0Sstevel@tonic-gate int is_neg; 354*0Sstevel@tonic-gate 355*0Sstevel@tonic-gate if (digit) 356*0Sstevel@tonic-gate { 357*0Sstevel@tonic-gate is_neg = digit < 0; 358*0Sstevel@tonic-gate 359*0Sstevel@tonic-gate if (is_neg) 360*0Sstevel@tonic-gate digit = -digit; 361*0Sstevel@tonic-gate 362*0Sstevel@tonic-gate if (is_neg != r_is_inverted) 363*0Sstevel@tonic-gate { 364*0Sstevel@tonic-gate if (!r_is_at_infinity) 365*0Sstevel@tonic-gate { 366*0Sstevel@tonic-gate if (!EC_POINT_invert(group, r, ctx)) goto err; 367*0Sstevel@tonic-gate } 368*0Sstevel@tonic-gate r_is_inverted = !r_is_inverted; 369*0Sstevel@tonic-gate } 370*0Sstevel@tonic-gate 371*0Sstevel@tonic-gate /* digit > 0 */ 372*0Sstevel@tonic-gate 373*0Sstevel@tonic-gate if (r_is_at_infinity) 374*0Sstevel@tonic-gate { 375*0Sstevel@tonic-gate if (!EC_POINT_copy(r, val_sub[i][digit >> 1])) goto err; 376*0Sstevel@tonic-gate r_is_at_infinity = 0; 377*0Sstevel@tonic-gate } 378*0Sstevel@tonic-gate else 379*0Sstevel@tonic-gate { 380*0Sstevel@tonic-gate if (!EC_POINT_add(group, r, r, val_sub[i][digit >> 1], ctx)) goto err; 381*0Sstevel@tonic-gate } 382*0Sstevel@tonic-gate } 383*0Sstevel@tonic-gate } 384*0Sstevel@tonic-gate } 385*0Sstevel@tonic-gate } 386*0Sstevel@tonic-gate 387*0Sstevel@tonic-gate if (r_is_at_infinity) 388*0Sstevel@tonic-gate { 389*0Sstevel@tonic-gate if (!EC_POINT_set_to_infinity(group, r)) goto err; 390*0Sstevel@tonic-gate } 391*0Sstevel@tonic-gate else 392*0Sstevel@tonic-gate { 393*0Sstevel@tonic-gate if (r_is_inverted) 394*0Sstevel@tonic-gate if (!EC_POINT_invert(group, r, ctx)) goto err; 395*0Sstevel@tonic-gate } 396*0Sstevel@tonic-gate 397*0Sstevel@tonic-gate ret = 1; 398*0Sstevel@tonic-gate 399*0Sstevel@tonic-gate err: 400*0Sstevel@tonic-gate if (new_ctx != NULL) 401*0Sstevel@tonic-gate BN_CTX_free(new_ctx); 402*0Sstevel@tonic-gate if (tmp != NULL) 403*0Sstevel@tonic-gate EC_POINT_free(tmp); 404*0Sstevel@tonic-gate if (wsize != NULL) 405*0Sstevel@tonic-gate OPENSSL_free(wsize); 406*0Sstevel@tonic-gate if (wNAF_len != NULL) 407*0Sstevel@tonic-gate OPENSSL_free(wNAF_len); 408*0Sstevel@tonic-gate if (wNAF != NULL) 409*0Sstevel@tonic-gate { 410*0Sstevel@tonic-gate signed char **w; 411*0Sstevel@tonic-gate 412*0Sstevel@tonic-gate for (w = wNAF; *w != NULL; w++) 413*0Sstevel@tonic-gate OPENSSL_free(*w); 414*0Sstevel@tonic-gate 415*0Sstevel@tonic-gate OPENSSL_free(wNAF); 416*0Sstevel@tonic-gate } 417*0Sstevel@tonic-gate if (val != NULL) 418*0Sstevel@tonic-gate { 419*0Sstevel@tonic-gate for (v = val; *v != NULL; v++) 420*0Sstevel@tonic-gate EC_POINT_clear_free(*v); 421*0Sstevel@tonic-gate 422*0Sstevel@tonic-gate OPENSSL_free(val); 423*0Sstevel@tonic-gate } 424*0Sstevel@tonic-gate if (val_sub != NULL) 425*0Sstevel@tonic-gate { 426*0Sstevel@tonic-gate OPENSSL_free(val_sub); 427*0Sstevel@tonic-gate } 428*0Sstevel@tonic-gate return ret; 429*0Sstevel@tonic-gate } 430*0Sstevel@tonic-gate 431*0Sstevel@tonic-gate 432*0Sstevel@tonic-gate int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar, const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx) 433*0Sstevel@tonic-gate { 434*0Sstevel@tonic-gate const EC_POINT *points[1]; 435*0Sstevel@tonic-gate const BIGNUM *scalars[1]; 436*0Sstevel@tonic-gate 437*0Sstevel@tonic-gate points[0] = point; 438*0Sstevel@tonic-gate scalars[0] = p_scalar; 439*0Sstevel@tonic-gate 440*0Sstevel@tonic-gate return EC_POINTs_mul(group, r, g_scalar, (point != NULL && p_scalar != NULL), points, scalars, ctx); 441*0Sstevel@tonic-gate } 442*0Sstevel@tonic-gate 443*0Sstevel@tonic-gate 444*0Sstevel@tonic-gate int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx) 445*0Sstevel@tonic-gate { 446*0Sstevel@tonic-gate const EC_POINT *generator; 447*0Sstevel@tonic-gate BN_CTX *new_ctx = NULL; 448*0Sstevel@tonic-gate BIGNUM *order; 449*0Sstevel@tonic-gate int ret = 0; 450*0Sstevel@tonic-gate 451*0Sstevel@tonic-gate generator = EC_GROUP_get0_generator(group); 452*0Sstevel@tonic-gate if (generator == NULL) 453*0Sstevel@tonic-gate { 454*0Sstevel@tonic-gate ECerr(EC_F_EC_GROUP_PRECOMPUTE_MULT, EC_R_UNDEFINED_GENERATOR); 455*0Sstevel@tonic-gate return 0; 456*0Sstevel@tonic-gate } 457*0Sstevel@tonic-gate 458*0Sstevel@tonic-gate if (ctx == NULL) 459*0Sstevel@tonic-gate { 460*0Sstevel@tonic-gate ctx = new_ctx = BN_CTX_new(); 461*0Sstevel@tonic-gate if (ctx == NULL) 462*0Sstevel@tonic-gate return 0; 463*0Sstevel@tonic-gate } 464*0Sstevel@tonic-gate 465*0Sstevel@tonic-gate BN_CTX_start(ctx); 466*0Sstevel@tonic-gate order = BN_CTX_get(ctx); 467*0Sstevel@tonic-gate if (order == NULL) goto err; 468*0Sstevel@tonic-gate 469*0Sstevel@tonic-gate if (!EC_GROUP_get_order(group, order, ctx)) return 0; 470*0Sstevel@tonic-gate if (BN_is_zero(order)) 471*0Sstevel@tonic-gate { 472*0Sstevel@tonic-gate ECerr(EC_F_EC_GROUP_PRECOMPUTE_MULT, EC_R_UNKNOWN_ORDER); 473*0Sstevel@tonic-gate goto err; 474*0Sstevel@tonic-gate } 475*0Sstevel@tonic-gate 476*0Sstevel@tonic-gate /* TODO */ 477*0Sstevel@tonic-gate 478*0Sstevel@tonic-gate ret = 1; 479*0Sstevel@tonic-gate 480*0Sstevel@tonic-gate err: 481*0Sstevel@tonic-gate BN_CTX_end(ctx); 482*0Sstevel@tonic-gate if (new_ctx != NULL) 483*0Sstevel@tonic-gate BN_CTX_free(new_ctx); 484*0Sstevel@tonic-gate return ret; 485*0Sstevel@tonic-gate } 486