Sun has agreed to removing the covenant language from most files.
[openssl.git] / crypto / ec / ec2_mult.c
1 /* crypto/ec/ec2_mult.c */
2 /* ====================================================================
3  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
4  *
5  * The Elliptic Curve Public-Key Crypto Library (ECC Code) included
6  * herein is developed by SUN MICROSYSTEMS, INC., and is contributed
7  * to the OpenSSL project.
8  *
9  * The ECC Code is licensed pursuant to the OpenSSL open source
10  * license provided below.
11  *
12  * The software is originally written by Sheueling Chang Shantz and
13  * Douglas Stebila of Sun Microsystems Laboratories.
14  *
15  */
16 /* ====================================================================
17  * Copyright (c) 1998-2002 The OpenSSL Project.  All rights reserved.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  *
23  * 1. Redistributions of source code must retain the above copyright
24  *    notice, this list of conditions and the following disclaimer. 
25  *
26  * 2. Redistributions in binary form must reproduce the above copyright
27  *    notice, this list of conditions and the following disclaimer in
28  *    the documentation and/or other materials provided with the
29  *    distribution.
30  *
31  * 3. All advertising materials mentioning features or use of this
32  *    software must display the following acknowledgment:
33  *    "This product includes software developed by the OpenSSL Project
34  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
35  *
36  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
37  *    endorse or promote products derived from this software without
38  *    prior written permission. For written permission, please contact
39  *    openssl-core@openssl.org.
40  *
41  * 5. Products derived from this software may not be called "OpenSSL"
42  *    nor may "OpenSSL" appear in their names without prior written
43  *    permission of the OpenSSL Project.
44  *
45  * 6. Redistributions of any form whatsoever must retain the following
46  *    acknowledgment:
47  *    "This product includes software developed by the OpenSSL Project
48  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
51  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
54  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
59  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
61  * OF THE POSSIBILITY OF SUCH DAMAGE.
62  * ====================================================================
63  *
64  * This product includes cryptographic software written by Eric Young
65  * (eay@cryptsoft.com).  This product includes software written by Tim
66  * Hudson (tjh@cryptsoft.com).
67  *
68  */
69
70 #include <openssl/err.h>
71
72 #include "ec_lcl.h"
73
74
75 /* Compute the x-coordinate x/z for the point 2*(x/z) in Montgomery projective 
76  * coordinates.
77  * Uses algorithm Mdouble in appendix of 
78  *     Lopez, J. and Dahab, R.  "Fast multiplication on elliptic curves over 
79  *     GF(2^m) without precomputation".
80  * modified to not require precomputation of c=b^{2^{m-1}}.
81  */
82 static int Mdouble(const EC_GROUP *group, BIGNUM *x, BIGNUM *z, BN_CTX *ctx)
83         {
84         BIGNUM *t1;
85         int ret = 0;
86         
87         /* Since Mdouble is static we can guarantee that ctx != NULL. */
88         BN_CTX_start(ctx);
89         t1 = BN_CTX_get(ctx);
90         if (t1 == NULL) goto err;
91
92         if (!group->meth->field_sqr(group, x, x, ctx)) goto err;
93         if (!group->meth->field_sqr(group, t1, z, ctx)) goto err;
94         if (!group->meth->field_mul(group, z, x, t1, ctx)) goto err;
95         if (!group->meth->field_sqr(group, x, x, ctx)) goto err;
96         if (!group->meth->field_sqr(group, t1, t1, ctx)) goto err;
97         if (!group->meth->field_mul(group, t1, &group->b, t1, ctx)) goto err;
98         if (!BN_GF2m_add(x, x, t1)) goto err;
99
100         ret = 1;
101
102  err:
103         BN_CTX_end(ctx);
104         return ret;
105         }
106
107 /* Compute the x-coordinate x1/z1 for the point (x1/z1)+(x2/x2) in Montgomery 
108  * projective coordinates.
109  * Uses algorithm Madd in appendix of 
110  *     Lopex, J. and Dahab, R.  "Fast multiplication on elliptic curves over 
111  *     GF(2^m) without precomputation".
112  */
113 static int Madd(const EC_GROUP *group, const BIGNUM *x, BIGNUM *x1, BIGNUM *z1, 
114         const BIGNUM *x2, const BIGNUM *z2, BN_CTX *ctx)
115         {
116         BIGNUM *t1, *t2;
117         int ret = 0;
118         
119         /* Since Madd is static we can guarantee that ctx != NULL. */
120         BN_CTX_start(ctx);
121         t1 = BN_CTX_get(ctx);
122         t2 = BN_CTX_get(ctx);
123         if (t2 == NULL) goto err;
124
125         if (!BN_copy(t1, x)) goto err;
126         if (!group->meth->field_mul(group, x1, x1, z2, ctx)) goto err;
127         if (!group->meth->field_mul(group, z1, z1, x2, ctx)) goto err;
128         if (!group->meth->field_mul(group, t2, x1, z1, ctx)) goto err;
129         if (!BN_GF2m_add(z1, z1, x1)) goto err;
130         if (!group->meth->field_sqr(group, z1, z1, ctx)) goto err;
131         if (!group->meth->field_mul(group, x1, z1, t1, ctx)) goto err;
132         if (!BN_GF2m_add(x1, x1, t2)) goto err;
133
134         ret = 1;
135
136  err:
137         BN_CTX_end(ctx);
138         return ret;
139         }
140
141 /* Compute the affine coordinates x2, y2=z2 for the point (x1/z1) and (x2/x2) in
142  * Montgomery projective coordinates.
143  * Uses algorithm Mxy in appendix of 
144  *     Lopex, J. and Dahab, R.  "Fast multiplication on elliptic curves over 
145  *     GF(2^m) without precomputation".
146  * Returns:
147  *     0 on error
148  *     1 if return value should be the point at infinity
149  *     2 otherwise
150  */
151 static int Mxy(const EC_GROUP *group, const BIGNUM *x, const BIGNUM *y, BIGNUM *x1, 
152         BIGNUM *z1, BIGNUM *x2, BIGNUM *z2, BN_CTX *ctx)
153         {
154         BIGNUM *t3, *t4, *t5;
155         int ret = 0;
156         
157         if (BN_is_zero(z1))
158                 {
159                 if (!BN_zero(x2)) return 0;
160                 if (!BN_zero(z2)) return 0;
161                 return 1;
162                 }
163         
164         if (BN_is_zero(z2))
165                 {
166                 if (!BN_copy(x2, x)) return 0;
167                 if (!BN_GF2m_add(z2, x, y)) return 0;
168                 return 2;
169                 }
170                 
171         /* Since Mxy is static we can guarantee that ctx != NULL. */
172         BN_CTX_start(ctx);
173         t3 = BN_CTX_get(ctx);
174         t4 = BN_CTX_get(ctx);
175         t5 = BN_CTX_get(ctx);
176         if (t5 == NULL) goto err;
177
178         if (!BN_one(t5)) goto err;
179
180         if (!group->meth->field_mul(group, t3, z1, z2, ctx)) goto err;
181
182         if (!group->meth->field_mul(group, z1, z1, x, ctx)) goto err;
183         if (!BN_GF2m_add(z1, z1, x1)) goto err;
184         if (!group->meth->field_mul(group, z2, z2, x, ctx)) goto err;
185         if (!group->meth->field_mul(group, x1, z2, x1, ctx)) goto err;
186         if (!BN_GF2m_add(z2, z2, x2)) goto err;
187
188         if (!group->meth->field_mul(group, z2, z2, z1, ctx)) goto err;
189         if (!group->meth->field_sqr(group, t4, x, ctx)) goto err;
190         if (!BN_GF2m_add(t4, t4, y)) goto err;
191         if (!group->meth->field_mul(group, t4, t4, t3, ctx)) goto err;
192         if (!BN_GF2m_add(t4, t4, z2)) goto err;
193
194         if (!group->meth->field_mul(group, t3, t3, x, ctx)) goto err;
195         if (!group->meth->field_div(group, t3, t5, t3, ctx)) goto err;
196         if (!group->meth->field_mul(group, t4, t3, t4, ctx)) goto err;
197         if (!group->meth->field_mul(group, x2, x1, t3, ctx)) goto err;
198         if (!BN_GF2m_add(z2, x2, x)) goto err;
199
200         if (!group->meth->field_mul(group, z2, z2, t4, ctx)) goto err;
201         if (!BN_GF2m_add(z2, z2, y)) goto err;
202
203         ret = 2;
204
205  err:
206         BN_CTX_end(ctx);
207         return ret;
208         }
209
210 /* Computes scalar*point and stores the result in r.
211  * point can not equal r.
212  * Uses algorithm 2P of
213  *     Lopex, J. and Dahab, R.  "Fast multiplication on elliptic curves over 
214  *     GF(2^m) without precomputation".
215  */
216 static int point_multiply(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
217         const EC_POINT *point, BN_CTX *ctx)
218         {
219         BIGNUM *x1, *x2, *z1, *z2;
220         int ret = 0, i, j;
221         BN_ULONG mask;
222
223         if (r == point)
224                 {
225                 ECerr(EC_F_EC_POINT_MUL, EC_R_INVALID_ARGUMENT);
226                 return 0;
227                 }
228         
229         /* if result should be point at infinity */
230         if ((scalar == NULL) || BN_is_zero(scalar) || (point == NULL) || 
231                 EC_POINT_is_at_infinity(group, point))
232                 {
233                 return EC_POINT_set_to_infinity(group, r);
234                 }
235
236         /* only support affine coordinates */
237         if (!point->Z_is_one) return 0;
238
239         /* Since point_multiply is static we can guarantee that ctx != NULL. */
240         BN_CTX_start(ctx);
241         x1 = BN_CTX_get(ctx);
242         z1 = BN_CTX_get(ctx);
243         if (z1 == NULL) goto err;
244
245         x2 = &r->X;
246         z2 = &r->Y;
247
248         if (!BN_GF2m_mod_arr(x1, &point->X, group->poly)) goto err; /* x1 = x */
249         if (!BN_one(z1)) goto err; /* z1 = 1 */
250         if (!group->meth->field_sqr(group, z2, x1, ctx)) goto err; /* z2 = x1^2 = x^2 */
251         if (!group->meth->field_sqr(group, x2, z2, ctx)) goto err;
252         if (!BN_GF2m_add(x2, x2, &group->b)) goto err; /* x2 = x^4 + b */
253
254         /* find top most bit and go one past it */
255         i = scalar->top - 1; j = BN_BITS2 - 1;
256         mask = BN_TBIT;
257         while (!(scalar->d[i] & mask)) { mask >>= 1; j--; }
258         mask >>= 1; j--;
259         /* if top most bit was at word break, go to next word */
260         if (!mask) 
261                 {
262                 i--; j = BN_BITS2 - 1;
263                 mask = BN_TBIT;
264                 }
265
266         for (; i >= 0; i--)
267                 {
268                 for (; j >= 0; j--)
269                         {
270                         if (scalar->d[i] & mask)
271                                 {
272                                 if (!Madd(group, &point->X, x1, z1, x2, z2, ctx)) goto err;
273                                 if (!Mdouble(group, x2, z2, ctx)) goto err;
274                                 }
275                         else
276                                 {
277                                 if (!Madd(group, &point->X, x2, z2, x1, z1, ctx)) goto err;
278                                 if (!Mdouble(group, x1, z1, ctx)) goto err;
279                                 }
280                         mask >>= 1;
281                         }
282                 j = BN_BITS2 - 1;
283                 mask = BN_TBIT;
284                 }
285
286         /* convert out of "projective" coordinates */
287         i = Mxy(group, &point->X, &point->Y, x1, z1, x2, z2, ctx);
288         if (i == 0) goto err;
289         else if (i == 1) 
290                 {
291                 if (!EC_POINT_set_to_infinity(group, r)) goto err;
292                 }
293         else
294                 {
295                 if (!BN_one(&r->Z)) goto err;
296                 r->Z_is_one = 1;
297                 }
298
299         /* GF(2^m) field elements should always have BIGNUM::neg = 0 */
300         r->X.neg = 0;
301         r->Y.neg = 0;
302
303         ret = 1;
304
305  err:
306         BN_CTX_end(ctx);
307         return ret;
308         }
309
310
311 /* Computes the sum
312  *     scalar*group->generator + scalars[0]*points[0] + ... + scalars[num-1]*points[num-1]
313  * gracefully ignoring NULL scalar values.
314  */
315 int ec_GF2m_mont_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
316         size_t num, const EC_POINT *points[], const BIGNUM *scalars[], BN_CTX *ctx)
317         {
318         BN_CTX *new_ctx = NULL;
319         int ret = 0, i;
320         EC_POINT *p=NULL;
321
322         if (ctx == NULL)
323                 {
324                 ctx = new_ctx = BN_CTX_new();
325                 if (ctx == NULL)
326                         return 0;
327                 }
328
329         /* This implementation is more efficient than the wNAF implementation for 2
330          * or fewer points.  Use the ec_wNAF_mul implementation for 3 or more points.
331          */
332         if ((scalar && (num > 1)) || (num > 2))
333                 {
334                 ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
335                 goto err;
336                 }
337
338         if ((p = EC_POINT_new(group)) == NULL) goto err;
339
340         if (!EC_POINT_set_to_infinity(group, r)) goto err;
341
342         if (scalar)
343                 {
344                 if (!point_multiply(group, p, scalar, group->generator, ctx)) goto err;
345                 if (scalar->neg) if (!group->meth->invert(group, p, ctx)) goto err;
346                 if (!group->meth->add(group, r, r, p, ctx)) goto err;
347                 }
348
349         for (i = 0; i < num; i++)
350                 {
351                 if (!point_multiply(group, p, scalars[i], points[i], ctx)) goto err;
352                 if (scalars[i]->neg) if (!group->meth->invert(group, p, ctx)) goto err;
353                 if (!group->meth->add(group, r, r, p, ctx)) goto err;
354                 }
355
356         ret = 1;
357
358   err:
359         if (p) EC_POINT_free(p);
360         if (new_ctx != NULL)
361                 BN_CTX_free(new_ctx);
362         return ret;
363         }
364
365
366 /* Precomputation for point multiplication. */  
367 int ec_GF2m_mont_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
368         {
369         /* There is no precomputation to do for Montgomery scalar multiplication but
370          * since this implementation falls back to the wNAF multiplication for more than
371          * two points, call the wNAF implementation's precompute.
372          */
373         return ec_wNAF_precompute_mult(group, ctx);
374         }