xref: /onnv-gate/usr/src/common/openssl/crypto/ec/ec_mult.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/ec/ec_mult.c */
2*2139Sjp161948 /*
3*2139Sjp161948  * Originally written by Bodo Moeller and Nils Larsch for the OpenSSL project.
4*2139Sjp161948  */
50Sstevel@tonic-gate /* ====================================================================
6*2139Sjp161948  * Copyright (c) 1998-2003 The OpenSSL Project.  All rights reserved.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate  * are met:
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate  *
150Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
180Sstevel@tonic-gate  *    distribution.
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate  *    software must display the following acknowledgment:
220Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate  *    endorse or promote products derived from this software without
270Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
280Sstevel@tonic-gate  *    openssl-core@openssl.org.
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate  *    permission of the OpenSSL Project.
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate  *    acknowledgment:
360Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
380Sstevel@tonic-gate  *
390Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate  * ====================================================================
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
550Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  */
58*2139Sjp161948 /* ====================================================================
59*2139Sjp161948  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60*2139Sjp161948  * Portions of this software developed by SUN MICROSYSTEMS, INC.,
61*2139Sjp161948  * and contributed to the OpenSSL project.
62*2139Sjp161948  */
63*2139Sjp161948 
64*2139Sjp161948 #include <string.h>
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #include <openssl/err.h>
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #include "ec_lcl.h"
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /*
72*2139Sjp161948  * This file implements the wNAF-based interleaving multi-exponentation method
73*2139Sjp161948  * (<URL:http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/moeller.html#multiexp>);
74*2139Sjp161948  * for multiplication with precomputation, we use wNAF splitting
75*2139Sjp161948  * (<URL:http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/moeller.html#fastexp>).
760Sstevel@tonic-gate  */
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 
79*2139Sjp161948 
80*2139Sjp161948 
81*2139Sjp161948 /* structure for precomputed multiples of the generator */
82*2139Sjp161948 typedef struct ec_pre_comp_st {
83*2139Sjp161948 	const EC_GROUP *group; /* parent EC_GROUP object */
84*2139Sjp161948 	size_t blocksize;      /* block size for wNAF splitting */
85*2139Sjp161948 	size_t numblocks;      /* max. number of blocks for which we have precomputation */
86*2139Sjp161948 	size_t w;              /* window size */
87*2139Sjp161948 	EC_POINT **points;     /* array with pre-calculated multiples of generator:
88*2139Sjp161948 	                        * 'num' pointers to EC_POINT objects followed by a NULL */
89*2139Sjp161948 	size_t num;            /* numblocks * 2^(w-1) */
90*2139Sjp161948 	int references;
91*2139Sjp161948 } EC_PRE_COMP;
92*2139Sjp161948 
93*2139Sjp161948 /* functions to manage EC_PRE_COMP within the EC_GROUP extra_data framework */
94*2139Sjp161948 static void *ec_pre_comp_dup(void *);
95*2139Sjp161948 static void ec_pre_comp_free(void *);
96*2139Sjp161948 static void ec_pre_comp_clear_free(void *);
97*2139Sjp161948 
ec_pre_comp_new(const EC_GROUP * group)98*2139Sjp161948 static EC_PRE_COMP *ec_pre_comp_new(const EC_GROUP *group)
99*2139Sjp161948 	{
100*2139Sjp161948 	EC_PRE_COMP *ret = NULL;
101*2139Sjp161948 
102*2139Sjp161948 	if (!group)
103*2139Sjp161948 		return NULL;
104*2139Sjp161948 
105*2139Sjp161948 	ret = (EC_PRE_COMP *)OPENSSL_malloc(sizeof(EC_PRE_COMP));
106*2139Sjp161948 	if (!ret)
107*2139Sjp161948 		return ret;
108*2139Sjp161948 	ret->group = group;
109*2139Sjp161948 	ret->blocksize = 8; /* default */
110*2139Sjp161948 	ret->numblocks = 0;
111*2139Sjp161948 	ret->w = 4; /* default */
112*2139Sjp161948 	ret->points = NULL;
113*2139Sjp161948 	ret->num = 0;
114*2139Sjp161948 	ret->references = 1;
115*2139Sjp161948 	return ret;
116*2139Sjp161948 	}
117*2139Sjp161948 
ec_pre_comp_dup(void * src_)118*2139Sjp161948 static void *ec_pre_comp_dup(void *src_)
119*2139Sjp161948 	{
120*2139Sjp161948 	EC_PRE_COMP *src = src_;
121*2139Sjp161948 
122*2139Sjp161948 	/* no need to actually copy, these objects never change! */
123*2139Sjp161948 
124*2139Sjp161948 	CRYPTO_add(&src->references, 1, CRYPTO_LOCK_EC_PRE_COMP);
125*2139Sjp161948 
126*2139Sjp161948 	return src_;
127*2139Sjp161948 	}
128*2139Sjp161948 
ec_pre_comp_free(void * pre_)129*2139Sjp161948 static void ec_pre_comp_free(void *pre_)
130*2139Sjp161948 	{
131*2139Sjp161948 	int i;
132*2139Sjp161948 	EC_PRE_COMP *pre = pre_;
133*2139Sjp161948 
134*2139Sjp161948 	if (!pre)
135*2139Sjp161948 		return;
136*2139Sjp161948 
137*2139Sjp161948 	i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
138*2139Sjp161948 	if (i > 0)
139*2139Sjp161948 		return;
140*2139Sjp161948 
141*2139Sjp161948 	if (pre->points)
142*2139Sjp161948 		{
143*2139Sjp161948 		EC_POINT **p;
144*2139Sjp161948 
145*2139Sjp161948 		for (p = pre->points; *p != NULL; p++)
146*2139Sjp161948 			EC_POINT_free(*p);
147*2139Sjp161948 		OPENSSL_free(pre->points);
148*2139Sjp161948 		}
149*2139Sjp161948 	OPENSSL_free(pre);
150*2139Sjp161948 	}
151*2139Sjp161948 
ec_pre_comp_clear_free(void * pre_)152*2139Sjp161948 static void ec_pre_comp_clear_free(void *pre_)
153*2139Sjp161948 	{
154*2139Sjp161948 	int i;
155*2139Sjp161948 	EC_PRE_COMP *pre = pre_;
156*2139Sjp161948 
157*2139Sjp161948 	if (!pre)
158*2139Sjp161948 		return;
159*2139Sjp161948 
160*2139Sjp161948 	i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
161*2139Sjp161948 	if (i > 0)
162*2139Sjp161948 		return;
163*2139Sjp161948 
164*2139Sjp161948 	if (pre->points)
165*2139Sjp161948 		{
166*2139Sjp161948 		EC_POINT **p;
167*2139Sjp161948 
168*2139Sjp161948 		for (p = pre->points; *p != NULL; p++)
169*2139Sjp161948 			EC_POINT_clear_free(*p);
170*2139Sjp161948 		OPENSSL_cleanse(pre->points, sizeof pre->points);
171*2139Sjp161948 		OPENSSL_free(pre->points);
172*2139Sjp161948 		}
173*2139Sjp161948 	OPENSSL_cleanse(pre, sizeof pre);
174*2139Sjp161948 	OPENSSL_free(pre);
175*2139Sjp161948 	}
176*2139Sjp161948 
177*2139Sjp161948 
178*2139Sjp161948 
179*2139Sjp161948 
180*2139Sjp161948 /* Determine the modified width-(w+1) Non-Adjacent Form (wNAF) of 'scalar'.
1810Sstevel@tonic-gate  * This is an array  r[]  of values that are either zero or odd with an
1820Sstevel@tonic-gate  * absolute value less than  2^w  satisfying
1830Sstevel@tonic-gate  *     scalar = \sum_j r[j]*2^j
184*2139Sjp161948  * where at most one of any  w+1  consecutive digits is non-zero
185*2139Sjp161948  * with the exception that the most significant digit may be only
186*2139Sjp161948  * w-1 zeros away from that next non-zero digit.
1870Sstevel@tonic-gate  */
compute_wNAF(const BIGNUM * scalar,int w,size_t * ret_len)188*2139Sjp161948 static signed char *compute_wNAF(const BIGNUM *scalar, int w, size_t *ret_len)
1890Sstevel@tonic-gate 	{
190*2139Sjp161948 	int window_val;
1910Sstevel@tonic-gate 	int ok = 0;
1920Sstevel@tonic-gate 	signed char *r = NULL;
1930Sstevel@tonic-gate 	int sign = 1;
1940Sstevel@tonic-gate 	int bit, next_bit, mask;
1950Sstevel@tonic-gate 	size_t len = 0, j;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	if (w <= 0 || w > 7) /* 'signed char' can represent integers with absolute values less than 2^7 */
1980Sstevel@tonic-gate 		{
1990Sstevel@tonic-gate 		ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
2000Sstevel@tonic-gate 		goto err;
2010Sstevel@tonic-gate 		}
2020Sstevel@tonic-gate 	bit = 1 << w; /* at most 128 */
2030Sstevel@tonic-gate 	next_bit = bit << 1; /* at most 256 */
2040Sstevel@tonic-gate 	mask = next_bit - 1; /* at most 255 */
2050Sstevel@tonic-gate 
206*2139Sjp161948 	if (BN_is_negative(scalar))
2070Sstevel@tonic-gate 		{
2080Sstevel@tonic-gate 		sign = -1;
2090Sstevel@tonic-gate 		}
2100Sstevel@tonic-gate 
211*2139Sjp161948 	len = BN_num_bits(scalar);
212*2139Sjp161948 	r = OPENSSL_malloc(len + 1); /* modified wNAF may be one digit longer than binary representation
213*2139Sjp161948 	                              * (*ret_len will be set to the actual length, i.e. at most
214*2139Sjp161948 	                              * BN_num_bits(scalar) + 1) */
2150Sstevel@tonic-gate 	if (r == NULL) goto err;
2160Sstevel@tonic-gate 
217*2139Sjp161948 	if (scalar->d == NULL || scalar->top == 0)
218*2139Sjp161948 		{
219*2139Sjp161948 		ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
220*2139Sjp161948 		goto err;
221*2139Sjp161948 		}
222*2139Sjp161948 	window_val = scalar->d[0] & mask;
2230Sstevel@tonic-gate 	j = 0;
224*2139Sjp161948 	while ((window_val != 0) || (j + w + 1 < len)) /* if j+w+1 >= len, window_val will not increase */
2250Sstevel@tonic-gate 		{
226*2139Sjp161948 		int digit = 0;
227*2139Sjp161948 
228*2139Sjp161948 		/* 0 <= window_val <= 2^(w+1) */
229*2139Sjp161948 
230*2139Sjp161948 		if (window_val & 1)
231*2139Sjp161948 			{
232*2139Sjp161948 			/* 0 < window_val < 2^(w+1) */
233*2139Sjp161948 
234*2139Sjp161948 			if (window_val & bit)
235*2139Sjp161948 				{
236*2139Sjp161948 				digit = window_val - next_bit; /* -2^w < digit < 0 */
2370Sstevel@tonic-gate 
238*2139Sjp161948 #if 1 /* modified wNAF */
239*2139Sjp161948 				if (j + w + 1 >= len)
240*2139Sjp161948 					{
241*2139Sjp161948 					/* special case for generating modified wNAFs:
242*2139Sjp161948 					 * no new bits will be added into window_val,
243*2139Sjp161948 					 * so using a positive digit here will decrease
244*2139Sjp161948 					 * the total length of the representation */
245*2139Sjp161948 
246*2139Sjp161948 					digit = window_val & (mask >> 1); /* 0 < digit < 2^w */
247*2139Sjp161948 					}
248*2139Sjp161948 #endif
249*2139Sjp161948 				}
250*2139Sjp161948 			else
251*2139Sjp161948 				{
252*2139Sjp161948 				digit = window_val; /* 0 < digit < 2^w */
253*2139Sjp161948 				}
254*2139Sjp161948 
255*2139Sjp161948 			if (digit <= -bit || digit >= bit || !(digit & 1))
2560Sstevel@tonic-gate 				{
2570Sstevel@tonic-gate 				ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
2580Sstevel@tonic-gate 				goto err;
2590Sstevel@tonic-gate 				}
260*2139Sjp161948 
261*2139Sjp161948 			window_val -= digit;
2620Sstevel@tonic-gate 
263*2139Sjp161948 			/* now window_val is 0 or 2^(w+1) in standard wNAF generation;
264*2139Sjp161948 			 * for modified window NAFs, it may also be 2^w
265*2139Sjp161948 			 */
266*2139Sjp161948 			if (window_val != 0 && window_val != next_bit && window_val != bit)
2670Sstevel@tonic-gate 				{
2680Sstevel@tonic-gate 				ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
2690Sstevel@tonic-gate 				goto err;
2700Sstevel@tonic-gate 				}
2710Sstevel@tonic-gate 			}
2720Sstevel@tonic-gate 
273*2139Sjp161948 		r[j++] = sign * digit;
274*2139Sjp161948 
275*2139Sjp161948 		window_val >>= 1;
276*2139Sjp161948 		window_val += bit * BN_is_bit_set(scalar, j + w);
277*2139Sjp161948 
278*2139Sjp161948 		if (window_val > next_bit)
2790Sstevel@tonic-gate 			{
2800Sstevel@tonic-gate 			ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
2810Sstevel@tonic-gate 			goto err;
2820Sstevel@tonic-gate 			}
2830Sstevel@tonic-gate 		}
2840Sstevel@tonic-gate 
285*2139Sjp161948 	if (j > len + 1)
2860Sstevel@tonic-gate 		{
2870Sstevel@tonic-gate 		ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
2880Sstevel@tonic-gate 		goto err;
2890Sstevel@tonic-gate 		}
2900Sstevel@tonic-gate 	len = j;
2910Sstevel@tonic-gate 	ok = 1;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate  err:
2940Sstevel@tonic-gate 	if (!ok)
2950Sstevel@tonic-gate 		{
2960Sstevel@tonic-gate 		OPENSSL_free(r);
2970Sstevel@tonic-gate 		r = NULL;
2980Sstevel@tonic-gate 		}
2990Sstevel@tonic-gate 	if (ok)
3000Sstevel@tonic-gate 		*ret_len = len;
3010Sstevel@tonic-gate 	return r;
3020Sstevel@tonic-gate 	}
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate /* TODO: table should be optimised for the wNAF-based implementation,
3060Sstevel@tonic-gate  *       sometimes smaller windows will give better performance
3070Sstevel@tonic-gate  *       (thus the boundaries should be increased)
3080Sstevel@tonic-gate  */
3090Sstevel@tonic-gate #define EC_window_bits_for_scalar_size(b) \
3100Sstevel@tonic-gate 		((size_t) \
3110Sstevel@tonic-gate 		 ((b) >= 2000 ? 6 : \
3120Sstevel@tonic-gate 		  (b) >=  800 ? 5 : \
3130Sstevel@tonic-gate 		  (b) >=  300 ? 4 : \
3140Sstevel@tonic-gate 		  (b) >=   70 ? 3 : \
3150Sstevel@tonic-gate 		  (b) >=   20 ? 2 : \
316*2139Sjp161948 		  1))
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate /* Compute
3190Sstevel@tonic-gate  *      \sum scalars[i]*points[i],
3200Sstevel@tonic-gate  * also including
3210Sstevel@tonic-gate  *      scalar*generator
3220Sstevel@tonic-gate  * in the addition if scalar != NULL
3230Sstevel@tonic-gate  */
ec_wNAF_mul(const EC_GROUP * group,EC_POINT * r,const BIGNUM * scalar,size_t num,const EC_POINT * points[],const BIGNUM * scalars[],BN_CTX * ctx)324*2139Sjp161948 int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
3250Sstevel@tonic-gate 	size_t num, const EC_POINT *points[], const BIGNUM *scalars[], BN_CTX *ctx)
3260Sstevel@tonic-gate 	{
3270Sstevel@tonic-gate 	BN_CTX *new_ctx = NULL;
328*2139Sjp161948 	const EC_POINT *generator = NULL;
3290Sstevel@tonic-gate 	EC_POINT *tmp = NULL;
3300Sstevel@tonic-gate 	size_t totalnum;
331*2139Sjp161948 	size_t blocksize = 0, numblocks = 0; /* for wNAF splitting */
332*2139Sjp161948 	size_t pre_points_per_block = 0;
3330Sstevel@tonic-gate 	size_t i, j;
3340Sstevel@tonic-gate 	int k;
3350Sstevel@tonic-gate 	int r_is_inverted = 0;
3360Sstevel@tonic-gate 	int r_is_at_infinity = 1;
3370Sstevel@tonic-gate 	size_t *wsize = NULL; /* individual window sizes */
3380Sstevel@tonic-gate 	signed char **wNAF = NULL; /* individual wNAFs */
3390Sstevel@tonic-gate 	size_t *wNAF_len = NULL;
3400Sstevel@tonic-gate 	size_t max_len = 0;
3410Sstevel@tonic-gate 	size_t num_val;
3420Sstevel@tonic-gate 	EC_POINT **val = NULL; /* precomputation */
3430Sstevel@tonic-gate 	EC_POINT **v;
344*2139Sjp161948 	EC_POINT ***val_sub = NULL; /* pointers to sub-arrays of 'val' or 'pre_comp->points' */
345*2139Sjp161948 	const EC_PRE_COMP *pre_comp = NULL;
346*2139Sjp161948 	int num_scalar = 0; /* flag: will be set to 1 if 'scalar' must be treated like other scalars,
347*2139Sjp161948 	                     * i.e. precomputation is not available */
3480Sstevel@tonic-gate 	int ret = 0;
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	if (group->meth != r->meth)
3510Sstevel@tonic-gate 		{
352*2139Sjp161948 		ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);
3530Sstevel@tonic-gate 		return 0;
3540Sstevel@tonic-gate 		}
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	if ((scalar == NULL) && (num == 0))
3570Sstevel@tonic-gate 		{
3580Sstevel@tonic-gate 		return EC_POINT_set_to_infinity(group, r);
3590Sstevel@tonic-gate 		}
3600Sstevel@tonic-gate 
361*2139Sjp161948 	for (i = 0; i < num; i++)
362*2139Sjp161948 		{
363*2139Sjp161948 		if (group->meth != points[i]->meth)
364*2139Sjp161948 			{
365*2139Sjp161948 			ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);
366*2139Sjp161948 			return 0;
367*2139Sjp161948 			}
368*2139Sjp161948 		}
369*2139Sjp161948 
370*2139Sjp161948 	if (ctx == NULL)
371*2139Sjp161948 		{
372*2139Sjp161948 		ctx = new_ctx = BN_CTX_new();
373*2139Sjp161948 		if (ctx == NULL)
374*2139Sjp161948 			goto err;
375*2139Sjp161948 		}
376*2139Sjp161948 
3770Sstevel@tonic-gate 	if (scalar != NULL)
3780Sstevel@tonic-gate 		{
3790Sstevel@tonic-gate 		generator = EC_GROUP_get0_generator(group);
3800Sstevel@tonic-gate 		if (generator == NULL)
3810Sstevel@tonic-gate 			{
382*2139Sjp161948 			ECerr(EC_F_EC_WNAF_MUL, EC_R_UNDEFINED_GENERATOR);
383*2139Sjp161948 			goto err;
384*2139Sjp161948 			}
385*2139Sjp161948 
386*2139Sjp161948 		/* look if we can use precomputed multiples of generator */
387*2139Sjp161948 
388*2139Sjp161948 		pre_comp = EC_EX_DATA_get_data(group->extra_data, ec_pre_comp_dup, ec_pre_comp_free, ec_pre_comp_clear_free);
389*2139Sjp161948 
390*2139Sjp161948 		if (pre_comp && pre_comp->numblocks && (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) == 0))
391*2139Sjp161948 			{
392*2139Sjp161948 			blocksize = pre_comp->blocksize;
393*2139Sjp161948 
394*2139Sjp161948 			/* determine maximum number of blocks that wNAF splitting may yield
395*2139Sjp161948 			 * (NB: maximum wNAF length is bit length plus one) */
396*2139Sjp161948 			numblocks = (BN_num_bits(scalar) / blocksize) + 1;
397*2139Sjp161948 
398*2139Sjp161948 			/* we cannot use more blocks than we have precomputation for */
399*2139Sjp161948 			if (numblocks > pre_comp->numblocks)
400*2139Sjp161948 				numblocks = pre_comp->numblocks;
401*2139Sjp161948 
402*2139Sjp161948 			pre_points_per_block = 1u << (pre_comp->w - 1);
403*2139Sjp161948 
404*2139Sjp161948 			/* check that pre_comp looks sane */
405*2139Sjp161948 			if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block))
406*2139Sjp161948 				{
407*2139Sjp161948 				ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
408*2139Sjp161948 				goto err;
409*2139Sjp161948 				}
410*2139Sjp161948 			}
411*2139Sjp161948 		else
412*2139Sjp161948 			{
413*2139Sjp161948 			/* can't use precomputation */
414*2139Sjp161948 			pre_comp = NULL;
415*2139Sjp161948 			numblocks = 1;
416*2139Sjp161948 			num_scalar = 1; /* treat 'scalar' like 'num'-th element of 'scalars' */
4170Sstevel@tonic-gate 			}
4180Sstevel@tonic-gate 		}
4190Sstevel@tonic-gate 
420*2139Sjp161948 	totalnum = num + numblocks;
4210Sstevel@tonic-gate 
422*2139Sjp161948 	wsize    = OPENSSL_malloc(totalnum * sizeof wsize[0]);
4230Sstevel@tonic-gate 	wNAF_len = OPENSSL_malloc(totalnum * sizeof wNAF_len[0]);
424*2139Sjp161948 	wNAF     = OPENSSL_malloc((totalnum + 1) * sizeof wNAF[0]); /* includes space for pivot */
425*2139Sjp161948 	val_sub  = OPENSSL_malloc(totalnum * sizeof val_sub[0]);
426*2139Sjp161948 
427*2139Sjp161948 	if (!wsize || !wNAF_len || !wNAF || !val_sub)
428*2139Sjp161948 		goto err;
4290Sstevel@tonic-gate 
430*2139Sjp161948 	wNAF[0] = NULL;	/* preliminary pivot */
431*2139Sjp161948 
432*2139Sjp161948 	/* num_val will be the total number of temporarily precomputed points */
4330Sstevel@tonic-gate 	num_val = 0;
434*2139Sjp161948 
435*2139Sjp161948 	for (i = 0; i < num + num_scalar; i++)
4360Sstevel@tonic-gate 		{
4370Sstevel@tonic-gate 		size_t bits;
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 		bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar);
4400Sstevel@tonic-gate 		wsize[i] = EC_window_bits_for_scalar_size(bits);
4410Sstevel@tonic-gate 		num_val += 1u << (wsize[i] - 1);
442*2139Sjp161948 		wNAF[i + 1] = NULL; /* make sure we always have a pivot */
443*2139Sjp161948 		wNAF[i] = compute_wNAF((i < num ? scalars[i] : scalar), wsize[i], &wNAF_len[i]);
444*2139Sjp161948 		if (wNAF[i] == NULL)
445*2139Sjp161948 			goto err;
446*2139Sjp161948 		if (wNAF_len[i] > max_len)
447*2139Sjp161948 			max_len = wNAF_len[i];
4480Sstevel@tonic-gate 		}
4490Sstevel@tonic-gate 
450*2139Sjp161948 	if (numblocks)
451*2139Sjp161948 		{
452*2139Sjp161948 		/* we go here iff scalar != NULL */
453*2139Sjp161948 
454*2139Sjp161948 		if (pre_comp == NULL)
455*2139Sjp161948 			{
456*2139Sjp161948 			if (num_scalar != 1)
457*2139Sjp161948 				{
458*2139Sjp161948 				ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
459*2139Sjp161948 				goto err;
460*2139Sjp161948 				}
461*2139Sjp161948 			/* we have already generated a wNAF for 'scalar' */
462*2139Sjp161948 			}
463*2139Sjp161948 		else
464*2139Sjp161948 			{
465*2139Sjp161948 			signed char *tmp_wNAF = NULL;
466*2139Sjp161948 			size_t tmp_len = 0;
467*2139Sjp161948 
468*2139Sjp161948 			if (num_scalar != 0)
469*2139Sjp161948 				{
470*2139Sjp161948 				ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
471*2139Sjp161948 				goto err;
472*2139Sjp161948 				}
473*2139Sjp161948 
474*2139Sjp161948 			/* use the window size for which we have precomputation */
475*2139Sjp161948 			wsize[num] = pre_comp->w;
476*2139Sjp161948 			tmp_wNAF = compute_wNAF(scalar, wsize[num], &tmp_len);
477*2139Sjp161948 			if (!tmp_wNAF)
478*2139Sjp161948 				goto err;
479*2139Sjp161948 
480*2139Sjp161948 			if (tmp_len <= max_len)
481*2139Sjp161948 				{
482*2139Sjp161948 				/* One of the other wNAFs is at least as long
483*2139Sjp161948 				 * as the wNAF belonging to the generator,
484*2139Sjp161948 				 * so wNAF splitting will not buy us anything. */
485*2139Sjp161948 
486*2139Sjp161948 				numblocks = 1;
487*2139Sjp161948 				totalnum = num + 1; /* don't use wNAF splitting */
488*2139Sjp161948 				wNAF[num] = tmp_wNAF;
489*2139Sjp161948 				wNAF[num + 1] = NULL;
490*2139Sjp161948 				wNAF_len[num] = tmp_len;
491*2139Sjp161948 				if (tmp_len > max_len)
492*2139Sjp161948 					max_len = tmp_len;
493*2139Sjp161948 				/* pre_comp->points starts with the points that we need here: */
494*2139Sjp161948 				val_sub[num] = pre_comp->points;
495*2139Sjp161948 				}
496*2139Sjp161948 			else
497*2139Sjp161948 				{
498*2139Sjp161948 				/* don't include tmp_wNAF directly into wNAF array
499*2139Sjp161948 				 * - use wNAF splitting and include the blocks */
500*2139Sjp161948 
501*2139Sjp161948 				signed char *pp;
502*2139Sjp161948 				EC_POINT **tmp_points;
503*2139Sjp161948 
504*2139Sjp161948 				if (tmp_len < numblocks * blocksize)
505*2139Sjp161948 					{
506*2139Sjp161948 					/* possibly we can do with fewer blocks than estimated */
507*2139Sjp161948 					numblocks = (tmp_len + blocksize - 1) / blocksize;
508*2139Sjp161948 					if (numblocks > pre_comp->numblocks)
509*2139Sjp161948 						{
510*2139Sjp161948 						ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
511*2139Sjp161948 						goto err;
512*2139Sjp161948 						}
513*2139Sjp161948 					totalnum = num + numblocks;
514*2139Sjp161948 					}
515*2139Sjp161948 
516*2139Sjp161948 				/* split wNAF in 'numblocks' parts */
517*2139Sjp161948 				pp = tmp_wNAF;
518*2139Sjp161948 				tmp_points = pre_comp->points;
519*2139Sjp161948 
520*2139Sjp161948 				for (i = num; i < totalnum; i++)
521*2139Sjp161948 					{
522*2139Sjp161948 					if (i < totalnum - 1)
523*2139Sjp161948 						{
524*2139Sjp161948 						wNAF_len[i] = blocksize;
525*2139Sjp161948 						if (tmp_len < blocksize)
526*2139Sjp161948 							{
527*2139Sjp161948 							ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
528*2139Sjp161948 							goto err;
529*2139Sjp161948 							}
530*2139Sjp161948 						tmp_len -= blocksize;
531*2139Sjp161948 						}
532*2139Sjp161948 					else
533*2139Sjp161948 						/* last block gets whatever is left
534*2139Sjp161948 						 * (this could be more or less than 'blocksize'!) */
535*2139Sjp161948 						wNAF_len[i] = tmp_len;
536*2139Sjp161948 
537*2139Sjp161948 					wNAF[i + 1] = NULL;
538*2139Sjp161948 					wNAF[i] = OPENSSL_malloc(wNAF_len[i]);
539*2139Sjp161948 					if (wNAF[i] == NULL)
540*2139Sjp161948 						{
541*2139Sjp161948 						OPENSSL_free(tmp_wNAF);
542*2139Sjp161948 						goto err;
543*2139Sjp161948 						}
544*2139Sjp161948 					memcpy(wNAF[i], pp, wNAF_len[i]);
545*2139Sjp161948 					if (wNAF_len[i] > max_len)
546*2139Sjp161948 						max_len = wNAF_len[i];
547*2139Sjp161948 
548*2139Sjp161948 					if (*tmp_points == NULL)
549*2139Sjp161948 						{
550*2139Sjp161948 						ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
551*2139Sjp161948 						OPENSSL_free(tmp_wNAF);
552*2139Sjp161948 						goto err;
553*2139Sjp161948 						}
554*2139Sjp161948 					val_sub[i] = tmp_points;
555*2139Sjp161948 					tmp_points += pre_points_per_block;
556*2139Sjp161948 					pp += blocksize;
557*2139Sjp161948 					}
558*2139Sjp161948 				OPENSSL_free(tmp_wNAF);
559*2139Sjp161948 				}
560*2139Sjp161948 			}
561*2139Sjp161948 		}
562*2139Sjp161948 
563*2139Sjp161948 	/* All points we precompute now go into a single array 'val'.
564*2139Sjp161948 	 * 'val_sub[i]' is a pointer to the subarray for the i-th point,
565*2139Sjp161948 	 * or to a subarray of 'pre_comp->points' if we already have precomputation. */
5660Sstevel@tonic-gate 	val = OPENSSL_malloc((num_val + 1) * sizeof val[0]);
5670Sstevel@tonic-gate 	if (val == NULL) goto err;
5680Sstevel@tonic-gate 	val[num_val] = NULL; /* pivot element */
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	/* allocate points for precomputation */
5710Sstevel@tonic-gate 	v = val;
572*2139Sjp161948 	for (i = 0; i < num + num_scalar; i++)
5730Sstevel@tonic-gate 		{
5740Sstevel@tonic-gate 		val_sub[i] = v;
5750Sstevel@tonic-gate 		for (j = 0; j < (1u << (wsize[i] - 1)); j++)
5760Sstevel@tonic-gate 			{
5770Sstevel@tonic-gate 			*v = EC_POINT_new(group);
5780Sstevel@tonic-gate 			if (*v == NULL) goto err;
5790Sstevel@tonic-gate 			v++;
5800Sstevel@tonic-gate 			}
5810Sstevel@tonic-gate 		}
5820Sstevel@tonic-gate 	if (!(v == val + num_val))
5830Sstevel@tonic-gate 		{
584*2139Sjp161948 		ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
5850Sstevel@tonic-gate 		goto err;
5860Sstevel@tonic-gate 		}
5870Sstevel@tonic-gate 
588*2139Sjp161948 	if (!(tmp = EC_POINT_new(group)))
589*2139Sjp161948 		goto err;
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	/* prepare precomputed values:
5920Sstevel@tonic-gate 	 *    val_sub[i][0] :=     points[i]
5930Sstevel@tonic-gate 	 *    val_sub[i][1] := 3 * points[i]
5940Sstevel@tonic-gate 	 *    val_sub[i][2] := 5 * points[i]
5950Sstevel@tonic-gate 	 *    ...
5960Sstevel@tonic-gate 	 */
597*2139Sjp161948 	for (i = 0; i < num + num_scalar; i++)
5980Sstevel@tonic-gate 		{
5990Sstevel@tonic-gate 		if (i < num)
6000Sstevel@tonic-gate 			{
6010Sstevel@tonic-gate 			if (!EC_POINT_copy(val_sub[i][0], points[i])) goto err;
6020Sstevel@tonic-gate 			}
6030Sstevel@tonic-gate 		else
6040Sstevel@tonic-gate 			{
6050Sstevel@tonic-gate 			if (!EC_POINT_copy(val_sub[i][0], generator)) goto err;
6060Sstevel@tonic-gate 			}
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 		if (wsize[i] > 1)
6090Sstevel@tonic-gate 			{
6100Sstevel@tonic-gate 			if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx)) goto err;
6110Sstevel@tonic-gate 			for (j = 1; j < (1u << (wsize[i] - 1)); j++)
6120Sstevel@tonic-gate 				{
6130Sstevel@tonic-gate 				if (!EC_POINT_add(group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx)) goto err;
6140Sstevel@tonic-gate 				}
6150Sstevel@tonic-gate 			}
6160Sstevel@tonic-gate 		}
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate #if 1 /* optional; EC_window_bits_for_scalar_size assumes we do this step */
619*2139Sjp161948 	if (!EC_POINTs_make_affine(group, num_val, val, ctx))
620*2139Sjp161948 		goto err;
6210Sstevel@tonic-gate #endif
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	r_is_at_infinity = 1;
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	for (k = max_len - 1; k >= 0; k--)
6260Sstevel@tonic-gate 		{
6270Sstevel@tonic-gate 		if (!r_is_at_infinity)
6280Sstevel@tonic-gate 			{
6290Sstevel@tonic-gate 			if (!EC_POINT_dbl(group, r, r, ctx)) goto err;
6300Sstevel@tonic-gate 			}
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 		for (i = 0; i < totalnum; i++)
6330Sstevel@tonic-gate 			{
6340Sstevel@tonic-gate 			if (wNAF_len[i] > (size_t)k)
6350Sstevel@tonic-gate 				{
6360Sstevel@tonic-gate 				int digit = wNAF[i][k];
6370Sstevel@tonic-gate 				int is_neg;
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 				if (digit)
6400Sstevel@tonic-gate 					{
6410Sstevel@tonic-gate 					is_neg = digit < 0;
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 					if (is_neg)
6440Sstevel@tonic-gate 						digit = -digit;
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 					if (is_neg != r_is_inverted)
6470Sstevel@tonic-gate 						{
6480Sstevel@tonic-gate 						if (!r_is_at_infinity)
6490Sstevel@tonic-gate 							{
6500Sstevel@tonic-gate 							if (!EC_POINT_invert(group, r, ctx)) goto err;
6510Sstevel@tonic-gate 							}
6520Sstevel@tonic-gate 						r_is_inverted = !r_is_inverted;
6530Sstevel@tonic-gate 						}
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 					/* digit > 0 */
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 					if (r_is_at_infinity)
6580Sstevel@tonic-gate 						{
6590Sstevel@tonic-gate 						if (!EC_POINT_copy(r, val_sub[i][digit >> 1])) goto err;
6600Sstevel@tonic-gate 						r_is_at_infinity = 0;
6610Sstevel@tonic-gate 						}
6620Sstevel@tonic-gate 					else
6630Sstevel@tonic-gate 						{
6640Sstevel@tonic-gate 						if (!EC_POINT_add(group, r, r, val_sub[i][digit >> 1], ctx)) goto err;
6650Sstevel@tonic-gate 						}
6660Sstevel@tonic-gate 					}
6670Sstevel@tonic-gate 				}
6680Sstevel@tonic-gate 			}
6690Sstevel@tonic-gate 		}
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	if (r_is_at_infinity)
6720Sstevel@tonic-gate 		{
6730Sstevel@tonic-gate 		if (!EC_POINT_set_to_infinity(group, r)) goto err;
6740Sstevel@tonic-gate 		}
6750Sstevel@tonic-gate 	else
6760Sstevel@tonic-gate 		{
6770Sstevel@tonic-gate 		if (r_is_inverted)
6780Sstevel@tonic-gate 			if (!EC_POINT_invert(group, r, ctx)) goto err;
6790Sstevel@tonic-gate 		}
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	ret = 1;
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate  err:
6840Sstevel@tonic-gate 	if (new_ctx != NULL)
6850Sstevel@tonic-gate 		BN_CTX_free(new_ctx);
6860Sstevel@tonic-gate 	if (tmp != NULL)
6870Sstevel@tonic-gate 		EC_POINT_free(tmp);
6880Sstevel@tonic-gate 	if (wsize != NULL)
6890Sstevel@tonic-gate 		OPENSSL_free(wsize);
6900Sstevel@tonic-gate 	if (wNAF_len != NULL)
6910Sstevel@tonic-gate 		OPENSSL_free(wNAF_len);
6920Sstevel@tonic-gate 	if (wNAF != NULL)
6930Sstevel@tonic-gate 		{
6940Sstevel@tonic-gate 		signed char **w;
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 		for (w = wNAF; *w != NULL; w++)
6970Sstevel@tonic-gate 			OPENSSL_free(*w);
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 		OPENSSL_free(wNAF);
7000Sstevel@tonic-gate 		}
7010Sstevel@tonic-gate 	if (val != NULL)
7020Sstevel@tonic-gate 		{
7030Sstevel@tonic-gate 		for (v = val; *v != NULL; v++)
7040Sstevel@tonic-gate 			EC_POINT_clear_free(*v);
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 		OPENSSL_free(val);
7070Sstevel@tonic-gate 		}
7080Sstevel@tonic-gate 	if (val_sub != NULL)
7090Sstevel@tonic-gate 		{
7100Sstevel@tonic-gate 		OPENSSL_free(val_sub);
7110Sstevel@tonic-gate 		}
7120Sstevel@tonic-gate 	return ret;
7130Sstevel@tonic-gate 	}
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 
716*2139Sjp161948 /* ec_wNAF_precompute_mult()
717*2139Sjp161948  * creates an EC_PRE_COMP object with preprecomputed multiples of the generator
718*2139Sjp161948  * for use with wNAF splitting as implemented in ec_wNAF_mul().
719*2139Sjp161948  *
720*2139Sjp161948  * 'pre_comp->points' is an array of multiples of the generator
721*2139Sjp161948  * of the following form:
722*2139Sjp161948  * points[0] =     generator;
723*2139Sjp161948  * points[1] = 3 * generator;
724*2139Sjp161948  * ...
725*2139Sjp161948  * points[2^(w-1)-1] =     (2^(w-1)-1) * generator;
726*2139Sjp161948  * points[2^(w-1)]   =     2^blocksize * generator;
727*2139Sjp161948  * points[2^(w-1)+1] = 3 * 2^blocksize * generator;
728*2139Sjp161948  * ...
729*2139Sjp161948  * points[2^(w-1)*(numblocks-1)-1] = (2^(w-1)) *  2^(blocksize*(numblocks-2)) * generator
730*2139Sjp161948  * points[2^(w-1)*(numblocks-1)]   =              2^(blocksize*(numblocks-1)) * generator
731*2139Sjp161948  * ...
732*2139Sjp161948  * points[2^(w-1)*numblocks-1]     = (2^(w-1)) *  2^(blocksize*(numblocks-1)) * generator
733*2139Sjp161948  * points[2^(w-1)*numblocks]       = NULL
734*2139Sjp161948  */
ec_wNAF_precompute_mult(EC_GROUP * group,BN_CTX * ctx)735*2139Sjp161948 int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
7360Sstevel@tonic-gate 	{
7370Sstevel@tonic-gate 	const EC_POINT *generator;
738*2139Sjp161948 	EC_POINT *tmp_point = NULL, *base = NULL, **var;
7390Sstevel@tonic-gate 	BN_CTX *new_ctx = NULL;
7400Sstevel@tonic-gate 	BIGNUM *order;
741*2139Sjp161948 	size_t i, bits, w, pre_points_per_block, blocksize, numblocks, num;
742*2139Sjp161948 	EC_POINT **points = NULL;
743*2139Sjp161948 	EC_PRE_COMP *pre_comp;
7440Sstevel@tonic-gate 	int ret = 0;
7450Sstevel@tonic-gate 
746*2139Sjp161948 	/* if there is an old EC_PRE_COMP object, throw it away */
747*2139Sjp161948 	EC_EX_DATA_free_data(&group->extra_data, ec_pre_comp_dup, ec_pre_comp_free, ec_pre_comp_clear_free);
748*2139Sjp161948 
749*2139Sjp161948 	if ((pre_comp = ec_pre_comp_new(group)) == NULL)
750*2139Sjp161948 		return 0;
751*2139Sjp161948 
7520Sstevel@tonic-gate 	generator = EC_GROUP_get0_generator(group);
7530Sstevel@tonic-gate 	if (generator == NULL)
7540Sstevel@tonic-gate 		{
755*2139Sjp161948 		ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, EC_R_UNDEFINED_GENERATOR);
756*2139Sjp161948 		goto err;
7570Sstevel@tonic-gate 		}
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	if (ctx == NULL)
7600Sstevel@tonic-gate 		{
7610Sstevel@tonic-gate 		ctx = new_ctx = BN_CTX_new();
7620Sstevel@tonic-gate 		if (ctx == NULL)
763*2139Sjp161948 			goto err;
7640Sstevel@tonic-gate 		}
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	BN_CTX_start(ctx);
7670Sstevel@tonic-gate 	order = BN_CTX_get(ctx);
7680Sstevel@tonic-gate 	if (order == NULL) goto err;
7690Sstevel@tonic-gate 
770*2139Sjp161948 	if (!EC_GROUP_get_order(group, order, ctx)) goto err;
7710Sstevel@tonic-gate 	if (BN_is_zero(order))
7720Sstevel@tonic-gate 		{
773*2139Sjp161948 		ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, EC_R_UNKNOWN_ORDER);
774*2139Sjp161948 		goto err;
775*2139Sjp161948 		}
776*2139Sjp161948 
777*2139Sjp161948 	bits = BN_num_bits(order);
778*2139Sjp161948 	/* The following parameters mean we precompute (approximately)
779*2139Sjp161948 	 * one point per bit.
780*2139Sjp161948 	 *
781*2139Sjp161948 	 * TBD: The combination  8, 4  is perfect for 160 bits; for other
782*2139Sjp161948 	 * bit lengths, other parameter combinations might provide better
783*2139Sjp161948 	 * efficiency.
784*2139Sjp161948 	 */
785*2139Sjp161948 	blocksize = 8;
786*2139Sjp161948 	w = 4;
787*2139Sjp161948 	if (EC_window_bits_for_scalar_size(bits) > w)
788*2139Sjp161948 		{
789*2139Sjp161948 		/* let's not make the window too small ... */
790*2139Sjp161948 		w = EC_window_bits_for_scalar_size(bits);
791*2139Sjp161948 		}
792*2139Sjp161948 
793*2139Sjp161948 	numblocks = (bits + blocksize - 1) / blocksize; /* max. number of blocks to use for wNAF splitting */
794*2139Sjp161948 
795*2139Sjp161948 	pre_points_per_block = 1u << (w - 1);
796*2139Sjp161948 	num = pre_points_per_block * numblocks; /* number of points to compute and store */
797*2139Sjp161948 
798*2139Sjp161948 	points = OPENSSL_malloc(sizeof (EC_POINT*)*(num + 1));
799*2139Sjp161948 	if (!points)
800*2139Sjp161948 		{
801*2139Sjp161948 		ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
8020Sstevel@tonic-gate 		goto err;
8030Sstevel@tonic-gate 		}
8040Sstevel@tonic-gate 
805*2139Sjp161948 	var = points;
806*2139Sjp161948 	var[num] = NULL; /* pivot */
807*2139Sjp161948 	for (i = 0; i < num; i++)
808*2139Sjp161948 		{
809*2139Sjp161948 		if ((var[i] = EC_POINT_new(group)) == NULL)
810*2139Sjp161948 			{
811*2139Sjp161948 			ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
812*2139Sjp161948 			goto err;
813*2139Sjp161948 			}
814*2139Sjp161948 		}
815*2139Sjp161948 
816*2139Sjp161948 	if (!(tmp_point = EC_POINT_new(group)) || !(base = EC_POINT_new(group)))
817*2139Sjp161948 		{
818*2139Sjp161948 		ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
819*2139Sjp161948 		goto err;
820*2139Sjp161948 		}
821*2139Sjp161948 
822*2139Sjp161948 	if (!EC_POINT_copy(base, generator))
823*2139Sjp161948 		goto err;
824*2139Sjp161948 
825*2139Sjp161948 	/* do the precomputation */
826*2139Sjp161948 	for (i = 0; i < numblocks; i++)
827*2139Sjp161948 		{
828*2139Sjp161948 		size_t j;
829*2139Sjp161948 
830*2139Sjp161948 		if (!EC_POINT_dbl(group, tmp_point, base, ctx))
831*2139Sjp161948 			goto err;
832*2139Sjp161948 
833*2139Sjp161948 		if (!EC_POINT_copy(*var++, base))
834*2139Sjp161948 			goto err;
835*2139Sjp161948 
836*2139Sjp161948 		for (j = 1; j < pre_points_per_block; j++, var++)
837*2139Sjp161948 			{
838*2139Sjp161948 			/* calculate odd multiples of the current base point */
839*2139Sjp161948 			if (!EC_POINT_add(group, *var, tmp_point, *(var - 1), ctx))
840*2139Sjp161948 				goto err;
841*2139Sjp161948 			}
842*2139Sjp161948 
843*2139Sjp161948 		if (i < numblocks - 1)
844*2139Sjp161948 			{
845*2139Sjp161948 			/* get the next base (multiply current one by 2^blocksize) */
846*2139Sjp161948 			size_t k;
847*2139Sjp161948 
848*2139Sjp161948 			if (blocksize <= 2)
849*2139Sjp161948 				{
850*2139Sjp161948 				ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_INTERNAL_ERROR);
851*2139Sjp161948 				goto err;
852*2139Sjp161948 				}
853*2139Sjp161948 
854*2139Sjp161948 			if (!EC_POINT_dbl(group, base, tmp_point, ctx))
855*2139Sjp161948 				goto err;
856*2139Sjp161948 			for (k = 2; k < blocksize; k++)
857*2139Sjp161948 				{
858*2139Sjp161948 				if (!EC_POINT_dbl(group,base,base,ctx))
859*2139Sjp161948 					goto err;
860*2139Sjp161948 				}
861*2139Sjp161948 			}
862*2139Sjp161948  		}
863*2139Sjp161948 
864*2139Sjp161948 	if (!EC_POINTs_make_affine(group, num, points, ctx))
865*2139Sjp161948 		goto err;
866*2139Sjp161948 
867*2139Sjp161948 	pre_comp->group = group;
868*2139Sjp161948 	pre_comp->blocksize = blocksize;
869*2139Sjp161948 	pre_comp->numblocks = numblocks;
870*2139Sjp161948 	pre_comp->w = w;
871*2139Sjp161948 	pre_comp->points = points;
872*2139Sjp161948 	points = NULL;
873*2139Sjp161948 	pre_comp->num = num;
874*2139Sjp161948 
875*2139Sjp161948 	if (!EC_EX_DATA_set_data(&group->extra_data, pre_comp,
876*2139Sjp161948 		ec_pre_comp_dup, ec_pre_comp_free, ec_pre_comp_clear_free))
877*2139Sjp161948 		goto err;
878*2139Sjp161948 	pre_comp = NULL;
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	ret = 1;
8810Sstevel@tonic-gate  err:
8820Sstevel@tonic-gate 	BN_CTX_end(ctx);
8830Sstevel@tonic-gate 	if (new_ctx != NULL)
8840Sstevel@tonic-gate 		BN_CTX_free(new_ctx);
885*2139Sjp161948 	if (pre_comp)
886*2139Sjp161948 		ec_pre_comp_free(pre_comp);
887*2139Sjp161948 	if (points)
888*2139Sjp161948 		{
889*2139Sjp161948 		EC_POINT **p;
890*2139Sjp161948 
891*2139Sjp161948 		for (p = points; *p != NULL; p++)
892*2139Sjp161948 			EC_POINT_free(*p);
893*2139Sjp161948 		OPENSSL_free(points);
894*2139Sjp161948 		}
895*2139Sjp161948 	if (tmp_point)
896*2139Sjp161948 		EC_POINT_free(tmp_point);
897*2139Sjp161948 	if (base)
898*2139Sjp161948 		EC_POINT_free(base);
8990Sstevel@tonic-gate 	return ret;
9000Sstevel@tonic-gate 	}
901*2139Sjp161948 
902*2139Sjp161948 
ec_wNAF_have_precompute_mult(const EC_GROUP * group)903*2139Sjp161948 int ec_wNAF_have_precompute_mult(const EC_GROUP *group)
904*2139Sjp161948 	{
905*2139Sjp161948 	if (EC_EX_DATA_get_data(group->extra_data, ec_pre_comp_dup, ec_pre_comp_free, ec_pre_comp_clear_free) != NULL)
906*2139Sjp161948 		return 1;
907*2139Sjp161948 	else
908*2139Sjp161948 		return 0;
909*2139Sjp161948 	}
910