ec2_mult.c revision 160815
1321369Sdim/* crypto/ec/ec2_mult.c */
2193323Sed/* ====================================================================
3353358Sdim * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
4353358Sdim *
5353358Sdim * The Elliptic Curve Public-Key Crypto Library (ECC Code) included
6193323Sed * herein is developed by SUN MICROSYSTEMS, INC., and is contributed
7193323Sed * to the OpenSSL project.
8193323Sed *
9309124Sdim * The ECC Code is licensed pursuant to the OpenSSL open source
10309124Sdim * license provided below.
11309124Sdim *
12309124Sdim * The software is originally written by Sheueling Chang Shantz and
13309124Sdim * Douglas Stebila of Sun Microsystems Laboratories.
14193323Sed *
15193323Sed */
16193323Sed/* ====================================================================
17193323Sed * Copyright (c) 1998-2003 The OpenSSL Project.  All rights reserved.
18327952Sdim *
19321369Sdim * Redistribution and use in source and binary forms, with or without
20321369Sdim * modification, are permitted provided that the following conditions
21321369Sdim * are met:
22327952Sdim *
23249423Sdim * 1. Redistributions of source code must retain the above copyright
24198090Srdivacky *    notice, this list of conditions and the following disclaimer.
25193323Sed *
26327952Sdim * 2. Redistributions in binary form must reproduce the above copyright
27193323Sed *    notice, this list of conditions and the following disclaimer in
28321369Sdim *    the documentation and/or other materials provided with the
29193323Sed *    distribution.
30327952Sdim *
31327952Sdim * 3. All advertising materials mentioning features or use of this
32327952Sdim *    software must display the following acknowledgment:
33327952Sdim *    "This product includes software developed by the OpenSSL Project
34360784Sdim *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
35321369Sdim *
36327952Sdim * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
37212904Sdim *    endorse or promote products derived from this software without
38198090Srdivacky *    prior written permission. For written permission, please contact
39212904Sdim *    openssl-core@openssl.org.
40321369Sdim *
41321369Sdim * 5. Products derived from this software may not be called "OpenSSL"
42321369Sdim *    nor may "OpenSSL" appear in their names without prior written
43321369Sdim *    permission of the OpenSSL Project.
44321369Sdim *
45327952Sdim * 6. Redistributions of any form whatsoever must retain the following
46321369Sdim *    acknowledgment:
47193323Sed *    "This product includes software developed by the OpenSSL Project
48193323Sed *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
49276479Sdim *
50276479Sdim * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
51321369Sdim * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52321369Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53360784Sdim * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
54321369Sdim * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55193323Sed * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56193323Sed * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57314564Sdim * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58309124Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
59280031Sdim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60280031Sdim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
61193323Sed * OF THE POSSIBILITY OF SUCH DAMAGE.
62321369Sdim * ====================================================================
63193323Sed *
64280031Sdim * This product includes cryptographic software written by Eric Young
65193323Sed * (eay@cryptsoft.com).  This product includes software written by Tim
66193323Sed * Hudson (tjh@cryptsoft.com).
67198090Srdivacky *
68309124Sdim */
69280031Sdim
70280031Sdim#include <openssl/err.h>
71280031Sdim
72280031Sdim#include "ec_lcl.h"
73193323Sed
74309124Sdim
75193323Sed/* Compute the x-coordinate x/z for the point 2*(x/z) in Montgomery projective
76321369Sdim * coordinates.
77321369Sdim * Uses algorithm Mdouble in appendix of
78321369Sdim *     Lopez, J. and Dahab, R.  "Fast multiplication on elliptic curves over
79314564Sdim *     GF(2^m) without precomputation".
80193323Sed * modified to not require precomputation of c=b^{2^{m-1}}.
81193323Sed */
82193323Sedstatic int gf2m_Mdouble(const EC_GROUP *group, BIGNUM *x, BIGNUM *z, BN_CTX *ctx)
83193323Sed	{
84314564Sdim	BIGNUM *t1;
85314564Sdim	int ret = 0;
86321369Sdim
87314564Sdim	/* Since Mdouble is static we can guarantee that ctx != NULL. */
88314564Sdim	BN_CTX_start(ctx);
89314564Sdim	t1 = BN_CTX_get(ctx);
90314564Sdim	if (t1 == NULL) goto err;
91321369Sdim
92314564Sdim	if (!group->meth->field_sqr(group, x, x, ctx)) goto err;
93314564Sdim	if (!group->meth->field_sqr(group, t1, z, ctx)) goto err;
94314564Sdim	if (!group->meth->field_mul(group, z, x, t1, ctx)) goto err;
95314564Sdim	if (!group->meth->field_sqr(group, x, x, ctx)) goto err;
96314564Sdim	if (!group->meth->field_sqr(group, t1, t1, ctx)) goto err;
97314564Sdim	if (!group->meth->field_mul(group, t1, &group->b, t1, ctx)) goto err;
98314564Sdim	if (!BN_GF2m_add(x, x, t1)) goto err;
99314564Sdim
100360784Sdim	ret = 1;
101280031Sdim
102280031Sdim err:
103193323Sed	BN_CTX_end(ctx);
104193323Sed	return ret;
105360784Sdim	}
106314564Sdim
107314564Sdim/* Compute the x-coordinate x1/z1 for the point (x1/z1)+(x2/x2) in Montgomery
108314564Sdim * projective coordinates.
109314564Sdim * Uses algorithm Madd in appendix of
110249423Sdim *     Lopex, J. and Dahab, R.  "Fast multiplication on elliptic curves over
111249423Sdim *     GF(2^m) without precomputation".
112193323Sed */
113309124Sdimstatic int gf2m_Madd(const EC_GROUP *group, const BIGNUM *x, BIGNUM *x1, BIGNUM *z1,
114341825Sdim	const BIGNUM *x2, const BIGNUM *z2, BN_CTX *ctx)
115193323Sed	{
116198090Srdivacky	BIGNUM *t1, *t2;
117198090Srdivacky	int ret = 0;
118280031Sdim
119280031Sdim	/* Since Madd is static we can guarantee that ctx != NULL. */
120309124Sdim	BN_CTX_start(ctx);
121280031Sdim	t1 = BN_CTX_get(ctx);
122280031Sdim	t2 = BN_CTX_get(ctx);
123280031Sdim	if (t2 == NULL) goto err;
124280031Sdim
125280031Sdim	if (!BN_copy(t1, x)) goto err;
126280031Sdim	if (!group->meth->field_mul(group, x1, x1, z2, ctx)) goto err;
127280031Sdim	if (!group->meth->field_mul(group, z1, z1, x2, ctx)) goto err;
128280031Sdim	if (!group->meth->field_mul(group, t2, x1, z1, ctx)) goto err;
129280031Sdim	if (!BN_GF2m_add(z1, z1, x1)) goto err;
130280031Sdim	if (!group->meth->field_sqr(group, z1, z1, ctx)) goto err;
131309124Sdim	if (!group->meth->field_mul(group, x1, z1, t1, ctx)) goto err;
132280031Sdim	if (!BN_GF2m_add(x1, x1, t2)) goto err;
133288943Sdim
134280031Sdim	ret = 1;
135223017Sdim
136193323Sed err:
137360784Sdim	BN_CTX_end(ctx);
138360784Sdim	return ret;
139195340Sed	}
140193323Sed
141198090Srdivacky/* Compute the x, y affine coordinates from the point (x1, z1) (x2, z2)
142223017Sdim * using Montgomery point multiplication algorithm Mxy() in appendix of
143223017Sdim *     Lopex, J. and Dahab, R.  "Fast multiplication on elliptic curves over
144223017Sdim *     GF(2^m) without precomputation".
145288943Sdim * Returns:
146280031Sdim *     0 on error
147198090Srdivacky *     1 if return value should be the point at infinity
148198090Srdivacky *     2 otherwise
149288943Sdim */
150280031Sdimstatic int gf2m_Mxy(const EC_GROUP *group, const BIGNUM *x, const BIGNUM *y, BIGNUM *x1,
151198090Srdivacky	BIGNUM *z1, BIGNUM *x2, BIGNUM *z2, BN_CTX *ctx)
152280031Sdim	{
153193323Sed	BIGNUM *t3, *t4, *t5;
154193323Sed	int ret = 0;
155249423Sdim
156193323Sed	if (BN_is_zero(z1))
157249423Sdim		{
158249423Sdim		BN_zero(x2);
159249423Sdim		BN_zero(z2);
160309124Sdim		return 1;
161341825Sdim		}
162251662Sdim
163249423Sdim	if (BN_is_zero(z2))
164251662Sdim		{
165341825Sdim		if (!BN_copy(x2, x)) return 0;
166280031Sdim		if (!BN_GF2m_add(z2, x, y)) return 0;
167251662Sdim		return 2;
168249423Sdim		}
169249423Sdim
170276479Sdim	/* Since Mxy is static we can guarantee that ctx != NULL. */
171249423Sdim	BN_CTX_start(ctx);
172249423Sdim	t3 = BN_CTX_get(ctx);
173249423Sdim	t4 = BN_CTX_get(ctx);
174249423Sdim	t5 = BN_CTX_get(ctx);
175249423Sdim	if (t5 == NULL) goto err;
176249423Sdim
177249423Sdim	if (!BN_one(t5)) goto err;
178249423Sdim
179249423Sdim	if (!group->meth->field_mul(group, t3, z1, z2, ctx)) goto err;
180249423Sdim
181249423Sdim	if (!group->meth->field_mul(group, z1, z1, x, ctx)) goto err;
182249423Sdim	if (!BN_GF2m_add(z1, z1, x1)) goto err;
183276479Sdim	if (!group->meth->field_mul(group, z2, z2, x, ctx)) goto err;
184249423Sdim	if (!group->meth->field_mul(group, x1, z2, x1, ctx)) goto err;
185249423Sdim	if (!BN_GF2m_add(z2, z2, x2)) goto err;
186249423Sdim
187309124Sdim	if (!group->meth->field_mul(group, z2, z2, z1, ctx)) goto err;
188249423Sdim	if (!group->meth->field_sqr(group, t4, x, ctx)) goto err;
189261991Sdim	if (!BN_GF2m_add(t4, t4, y)) goto err;
190261991Sdim	if (!group->meth->field_mul(group, t4, t4, t3, ctx)) goto err;
191309124Sdim	if (!BN_GF2m_add(t4, t4, z2)) goto err;
192249423Sdim
193249423Sdim	if (!group->meth->field_mul(group, t3, t3, x, ctx)) goto err;
194249423Sdim	if (!group->meth->field_div(group, t3, t5, t3, ctx)) goto err;
195276479Sdim	if (!group->meth->field_mul(group, t4, t3, t4, ctx)) goto err;
196249423Sdim	if (!group->meth->field_mul(group, x2, x1, t3, ctx)) goto err;
197249423Sdim	if (!BN_GF2m_add(z2, x2, x)) goto err;
198341825Sdim
199249423Sdim	if (!group->meth->field_mul(group, z2, z2, t4, ctx)) goto err;
200249423Sdim	if (!BN_GF2m_add(z2, z2, y)) goto err;
201249423Sdim
202249423Sdim	ret = 2;
203198090Srdivacky
204234353Sdim err:
205309124Sdim	BN_CTX_end(ctx);
206223017Sdim	return ret;
207193323Sed	}
208360784Sdim
209360784Sdim/* Computes scalar*point and stores the result in r.
210195340Sed * point can not equal r.
211198090Srdivacky * Uses algorithm 2P of
212223017Sdim *     Lopex, J. and Dahab, R.  "Fast multiplication on elliptic curves over
213223017Sdim *     GF(2^m) without precomputation".
214280031Sdim */
215198892Srdivackystatic int ec_GF2m_montgomery_point_multiply(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
216327952Sdim	const EC_POINT *point, BN_CTX *ctx)
217198892Srdivacky	{
218198892Srdivacky	BIGNUM *x1, *x2, *z1, *z2;
219198892Srdivacky	int ret = 0, i, j;
220198892Srdivacky	BN_ULONG mask;
221198892Srdivacky
222198892Srdivacky	if (r == point)
223198892Srdivacky		{
224360784Sdim		ECerr(EC_F_EC_GF2M_MONTGOMERY_POINT_MULTIPLY, EC_R_INVALID_ARGUMENT);
225360784Sdim		return 0;
226198892Srdivacky		}
227198892Srdivacky
228198892Srdivacky	/* if result should be point at infinity */
229280031Sdim	if ((scalar == NULL) || BN_is_zero(scalar) || (point == NULL) ||
230280031Sdim		EC_POINT_is_at_infinity(group, point))
231280031Sdim		{
232280031Sdim		return EC_POINT_set_to_infinity(group, r);
233280031Sdim		}
234280031Sdim
235280031Sdim	/* only support affine coordinates */
236280031Sdim	if (!point->Z_is_one) return 0;
237276479Sdim
238234353Sdim	/* Since point_multiply is static we can guarantee that ctx != NULL. */
239234353Sdim	BN_CTX_start(ctx);
240226633Sdim	x1 = BN_CTX_get(ctx);
241280031Sdim	z1 = BN_CTX_get(ctx);
242198892Srdivacky	if (z1 == NULL) goto err;
243198090Srdivacky
244198090Srdivacky	x2 = &r->X;
245198090Srdivacky	z2 = &r->Y;
246198090Srdivacky
247198090Srdivacky	if (!BN_GF2m_mod_arr(x1, &point->X, group->poly)) goto err; /* x1 = x */
248198090Srdivacky	if (!BN_one(z1)) goto err; /* z1 = 1 */
249198090Srdivacky	if (!group->meth->field_sqr(group, z2, x1, ctx)) goto err; /* z2 = x1^2 = x^2 */
250198090Srdivacky	if (!group->meth->field_sqr(group, x2, z2, ctx)) goto err;
251198090Srdivacky	if (!BN_GF2m_add(x2, x2, &group->b)) goto err; /* x2 = x^4 + b */
252198090Srdivacky
253193323Sed	/* find top most bit and go one past it */
254234353Sdim	i = scalar->top - 1; j = BN_BITS2 - 1;
255193323Sed	mask = BN_TBIT;
256198090Srdivacky	while (!(scalar->d[i] & mask)) { mask >>= 1; j--; }
257280031Sdim	mask >>= 1; j--;
258280031Sdim	/* if top most bit was at word break, go to next word */
259193323Sed	if (!mask)
260193323Sed		{
261314564Sdim		i--; j = BN_BITS2 - 1;
262314564Sdim		mask = BN_TBIT;
263314564Sdim		}
264314564Sdim
265321369Sdim	for (; i >= 0; i--)
266321369Sdim		{
267321369Sdim		for (; j >= 0; j--)
268321369Sdim			{
269321369Sdim			if (scalar->d[i] & mask)
270321369Sdim				{
271321369Sdim				if (!gf2m_Madd(group, &point->X, x1, z1, x2, z2, ctx)) goto err;
272314564Sdim				if (!gf2m_Mdouble(group, x2, z2, ctx)) goto err;
273314564Sdim				}
274314564Sdim			else
275314564Sdim				{
276314564Sdim				if (!gf2m_Madd(group, &point->X, x2, z2, x1, z1, ctx)) goto err;
277314564Sdim				if (!gf2m_Mdouble(group, x1, z1, ctx)) goto err;
278314564Sdim				}
279314564Sdim			mask >>= 1;
280314564Sdim			}
281314564Sdim		j = BN_BITS2 - 1;
282360784Sdim		mask = BN_TBIT;
283321369Sdim		}
284321369Sdim
285321369Sdim	/* convert out of "projective" coordinates */
286193323Sed	i = gf2m_Mxy(group, &point->X, &point->Y, x1, z1, x2, z2, ctx);
287193323Sed	if (i == 0) goto err;
288360784Sdim	else if (i == 1)
289360784Sdim		{
290309124Sdim		if (!EC_POINT_set_to_infinity(group, r)) goto err;
291341825Sdim		}
292341825Sdim	else
293309124Sdim		{
294212904Sdim		if (!BN_one(&r->Z)) goto err;
295309124Sdim		r->Z_is_one = 1;
296198090Srdivacky		}
297198090Srdivacky
298193323Sed	/* GF(2^m) field elements should always have BIGNUM::neg = 0 */
299221345Sdim	BN_set_negative(&r->X, 0);
300221345Sdim	BN_set_negative(&r->Y, 0);
301360784Sdim
302309124Sdim	ret = 1;
303309124Sdim
304221345Sdim err:
305210299Sed	BN_CTX_end(ctx);
306210299Sed	return ret;
307360784Sdim	}
308198090Srdivacky
309198090Srdivacky
310198090Srdivacky/* Computes the sum
311198090Srdivacky *     scalar*group->generator + scalars[0]*points[0] + ... + scalars[num-1]*points[num-1]
312198090Srdivacky * gracefully ignoring NULL scalar values.
313193323Sed */
314198090Srdivackyint ec_GF2m_simple_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
315198892Srdivacky	size_t num, const EC_POINT *points[], const BIGNUM *scalars[], BN_CTX *ctx)
316198892Srdivacky	{
317198892Srdivacky	BN_CTX *new_ctx = NULL;
318193323Sed	int ret = 0;
319198892Srdivacky	size_t i;
320198090Srdivacky	EC_POINT *p=NULL;
321341825Sdim
322210299Sed	if (ctx == NULL)
323210299Sed		{
324210299Sed		ctx = new_ctx = BN_CTX_new();
325198892Srdivacky		if (ctx == NULL)
326198892Srdivacky			return 0;
327198090Srdivacky		}
328309124Sdim
329234353Sdim	/* This implementation is more efficient than the wNAF implementation for 2
330234353Sdim	 * or fewer points.  Use the ec_wNAF_mul implementation for 3 or more points,
331198892Srdivacky	 * or if we can perform a fast multiplication based on precomputation.
332198090Srdivacky	 */
333360784Sdim	if ((scalar && (num > 1)) || (num > 2) || (num == 0 && EC_GROUP_have_precompute_mult(group)))
334198892Srdivacky		{
335198892Srdivacky		ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
336198892Srdivacky		goto err;
337198892Srdivacky		}
338198892Srdivacky
339198892Srdivacky	if ((p = EC_POINT_new(group)) == NULL) goto err;
340239462Sdim
341239462Sdim	if (!EC_POINT_set_to_infinity(group, r)) goto err;
342198090Srdivacky
343198892Srdivacky	if (scalar)
344198892Srdivacky		{
345198892Srdivacky		if (!ec_GF2m_montgomery_point_multiply(group, p, scalar, group->generator, ctx)) goto err;
346193323Sed		if (BN_is_negative(scalar))
347198892Srdivacky			if (!group->meth->invert(group, p, ctx)) goto err;
348198892Srdivacky		if (!group->meth->add(group, r, r, p, ctx)) goto err;
349198892Srdivacky		}
350198892Srdivacky
351198090Srdivacky	for (i = 0; i < num; i++)
352198090Srdivacky		{
353198090Srdivacky		if (!ec_GF2m_montgomery_point_multiply(group, p, scalars[i], points[i], ctx)) goto err;
354193323Sed		if (BN_is_negative(scalars[i]))
355198090Srdivacky			if (!group->meth->invert(group, p, ctx)) goto err;
356198090Srdivacky		if (!group->meth->add(group, r, r, p, ctx)) goto err;
357198090Srdivacky		}
358193323Sed
359198090Srdivacky	ret = 1;
360193323Sed
361198892Srdivacky  err:
362198892Srdivacky	if (p) EC_POINT_free(p);
363309124Sdim	if (new_ctx != NULL)
364309124Sdim		BN_CTX_free(new_ctx);
365198090Srdivacky	return ret;
366198090Srdivacky	}
367198892Srdivacky
368198090Srdivacky
369193323Sed/* Precomputation for point multiplication: fall back to wNAF methods
370193323Sed * because ec_GF2m_simple_mul() uses ec_wNAF_mul() if appropriate */
371321369Sdim
372321369Sdimint ec_GF2m_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
373321369Sdim	{
374321369Sdim	return ec_wNAF_precompute_mult(group, ctx);
375321369Sdim 	}
376321369Sdim
377321369Sdimint ec_GF2m_have_precompute_mult(const EC_GROUP *group)
378321369Sdim	{
379321369Sdim	return ec_wNAF_have_precompute_mult(group);
380321369Sdim 	}
381321369Sdim