free NULL cleanup 7
[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-2003 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 "internal/bn_int.h"
73 #include "ec_lcl.h"
74
75 #ifndef OPENSSL_NO_EC2M
76
77 /*-
78  * Compute the x-coordinate x/z for the point 2*(x/z) in Montgomery projective
79  * coordinates.
80  * Uses algorithm Mdouble in appendix of
81  *     Lopez, J. and Dahab, R.  "Fast multiplication on elliptic curves over
82  *     GF(2^m) without precomputation" (CHES '99, LNCS 1717).
83  * modified to not require precomputation of c=b^{2^{m-1}}.
84  */
85 static int gf2m_Mdouble(const EC_GROUP *group, BIGNUM *x, BIGNUM *z,
86                         BN_CTX *ctx)
87 {
88     BIGNUM *t1;
89     int ret = 0;
90
91     /* Since Mdouble is static we can guarantee that ctx != NULL. */
92     BN_CTX_start(ctx);
93     t1 = BN_CTX_get(ctx);
94     if (t1 == NULL)
95         goto err;
96
97     if (!group->meth->field_sqr(group, x, x, ctx))
98         goto err;
99     if (!group->meth->field_sqr(group, t1, z, ctx))
100         goto err;
101     if (!group->meth->field_mul(group, z, x, t1, ctx))
102         goto err;
103     if (!group->meth->field_sqr(group, x, x, ctx))
104         goto err;
105     if (!group->meth->field_sqr(group, t1, t1, ctx))
106         goto err;
107     if (!group->meth->field_mul(group, t1, group->b, t1, ctx))
108         goto err;
109     if (!BN_GF2m_add(x, x, t1))
110         goto err;
111
112     ret = 1;
113
114  err:
115     BN_CTX_end(ctx);
116     return ret;
117 }
118
119 /*-
120  * Compute the x-coordinate x1/z1 for the point (x1/z1)+(x2/x2) in Montgomery
121  * projective coordinates.
122  * Uses algorithm Madd in appendix of
123  *     Lopez, J. and Dahab, R.  "Fast multiplication on elliptic curves over
124  *     GF(2^m) without precomputation" (CHES '99, LNCS 1717).
125  */
126 static int gf2m_Madd(const EC_GROUP *group, const BIGNUM *x, BIGNUM *x1,
127                      BIGNUM *z1, const BIGNUM *x2, const BIGNUM *z2,
128                      BN_CTX *ctx)
129 {
130     BIGNUM *t1, *t2;
131     int ret = 0;
132
133     /* Since Madd is static we can guarantee that ctx != NULL. */
134     BN_CTX_start(ctx);
135     t1 = BN_CTX_get(ctx);
136     t2 = BN_CTX_get(ctx);
137     if (t2 == NULL)
138         goto err;
139
140     if (!BN_copy(t1, x))
141         goto err;
142     if (!group->meth->field_mul(group, x1, x1, z2, ctx))
143         goto err;
144     if (!group->meth->field_mul(group, z1, z1, x2, ctx))
145         goto err;
146     if (!group->meth->field_mul(group, t2, x1, z1, ctx))
147         goto err;
148     if (!BN_GF2m_add(z1, z1, x1))
149         goto err;
150     if (!group->meth->field_sqr(group, z1, z1, ctx))
151         goto err;
152     if (!group->meth->field_mul(group, x1, z1, t1, ctx))
153         goto err;
154     if (!BN_GF2m_add(x1, x1, t2))
155         goto err;
156
157     ret = 1;
158
159  err:
160     BN_CTX_end(ctx);
161     return ret;
162 }
163
164 /*-
165  * Compute the x, y affine coordinates from the point (x1, z1) (x2, z2)
166  * using Montgomery point multiplication algorithm Mxy() in appendix of
167  *     Lopez, J. and Dahab, R.  "Fast multiplication on elliptic curves over
168  *     GF(2^m) without precomputation" (CHES '99, LNCS 1717).
169  * Returns:
170  *     0 on error
171  *     1 if return value should be the point at infinity
172  *     2 otherwise
173  */
174 static int gf2m_Mxy(const EC_GROUP *group, const BIGNUM *x, const BIGNUM *y,
175                     BIGNUM *x1, BIGNUM *z1, BIGNUM *x2, BIGNUM *z2,
176                     BN_CTX *ctx)
177 {
178     BIGNUM *t3, *t4, *t5;
179     int ret = 0;
180
181     if (BN_is_zero(z1)) {
182         BN_zero(x2);
183         BN_zero(z2);
184         return 1;
185     }
186
187     if (BN_is_zero(z2)) {
188         if (!BN_copy(x2, x))
189             return 0;
190         if (!BN_GF2m_add(z2, x, y))
191             return 0;
192         return 2;
193     }
194
195     /* Since Mxy is static we can guarantee that ctx != NULL. */
196     BN_CTX_start(ctx);
197     t3 = BN_CTX_get(ctx);
198     t4 = BN_CTX_get(ctx);
199     t5 = BN_CTX_get(ctx);
200     if (t5 == NULL)
201         goto err;
202
203     if (!BN_one(t5))
204         goto err;
205
206     if (!group->meth->field_mul(group, t3, z1, z2, ctx))
207         goto err;
208
209     if (!group->meth->field_mul(group, z1, z1, x, ctx))
210         goto err;
211     if (!BN_GF2m_add(z1, z1, x1))
212         goto err;
213     if (!group->meth->field_mul(group, z2, z2, x, ctx))
214         goto err;
215     if (!group->meth->field_mul(group, x1, z2, x1, ctx))
216         goto err;
217     if (!BN_GF2m_add(z2, z2, x2))
218         goto err;
219
220     if (!group->meth->field_mul(group, z2, z2, z1, ctx))
221         goto err;
222     if (!group->meth->field_sqr(group, t4, x, ctx))
223         goto err;
224     if (!BN_GF2m_add(t4, t4, y))
225         goto err;
226     if (!group->meth->field_mul(group, t4, t4, t3, ctx))
227         goto err;
228     if (!BN_GF2m_add(t4, t4, z2))
229         goto err;
230
231     if (!group->meth->field_mul(group, t3, t3, x, ctx))
232         goto err;
233     if (!group->meth->field_div(group, t3, t5, t3, ctx))
234         goto err;
235     if (!group->meth->field_mul(group, t4, t3, t4, ctx))
236         goto err;
237     if (!group->meth->field_mul(group, x2, x1, t3, ctx))
238         goto err;
239     if (!BN_GF2m_add(z2, x2, x))
240         goto err;
241
242     if (!group->meth->field_mul(group, z2, z2, t4, ctx))
243         goto err;
244     if (!BN_GF2m_add(z2, z2, y))
245         goto err;
246
247     ret = 2;
248
249  err:
250     BN_CTX_end(ctx);
251     return ret;
252 }
253
254 /*-
255  * Computes scalar*point and stores the result in r.
256  * point can not equal r.
257  * Uses a modified algorithm 2P of
258  *     Lopez, J. and Dahab, R.  "Fast multiplication on elliptic curves over
259  *     GF(2^m) without precomputation" (CHES '99, LNCS 1717).
260  *
261  * To protect against side-channel attack the function uses constant time swap,
262  * avoiding conditional branches.
263  */
264 static int ec_GF2m_montgomery_point_multiply(const EC_GROUP *group,
265                                              EC_POINT *r,
266                                              const BIGNUM *scalar,
267                                              const EC_POINT *point,
268                                              BN_CTX *ctx)
269 {
270     BIGNUM *x1, *x2, *z1, *z2;
271     int ret = 0, i;
272     BN_ULONG mask, word;
273
274     if (r == point) {
275         ECerr(EC_F_EC_GF2M_MONTGOMERY_POINT_MULTIPLY, EC_R_INVALID_ARGUMENT);
276         return 0;
277     }
278
279     /* if result should be point at infinity */
280     if ((scalar == NULL) || BN_is_zero(scalar) || (point == NULL) ||
281         EC_POINT_is_at_infinity(group, point)) {
282         return EC_POINT_set_to_infinity(group, r);
283     }
284
285     /* only support affine coordinates */
286     if (!point->Z_is_one)
287         return 0;
288
289     /*
290      * Since point_multiply is static we can guarantee that ctx != NULL.
291      */
292     BN_CTX_start(ctx);
293     x1 = BN_CTX_get(ctx);
294     z1 = BN_CTX_get(ctx);
295     if (z1 == NULL)
296         goto err;
297
298     x2 = r->X;
299     z2 = r->Y;
300
301     bn_wexpand(x1, bn_get_top(group->field));
302     bn_wexpand(z1, bn_get_top(group->field));
303     bn_wexpand(x2, bn_get_top(group->field));
304     bn_wexpand(z2, bn_get_top(group->field));
305
306     if (!BN_GF2m_mod_arr(x1, point->X, group->poly))
307         goto err;               /* x1 = x */
308     if (!BN_one(z1))
309         goto err;               /* z1 = 1 */
310     if (!group->meth->field_sqr(group, z2, x1, ctx))
311         goto err;               /* z2 = x1^2 = x^2 */
312     if (!group->meth->field_sqr(group, x2, z2, ctx))
313         goto err;
314     if (!BN_GF2m_add(x2, x2, group->b))
315         goto err;               /* x2 = x^4 + b */
316
317     /* find top most bit and go one past it */
318     i = bn_get_top(scalar) - 1;
319     mask = BN_TBIT;
320     word = bn_get_words(scalar)[i];
321     while (!(word & mask))
322         mask >>= 1;
323     mask >>= 1;
324     /* if top most bit was at word break, go to next word */
325     if (!mask) {
326         i--;
327         mask = BN_TBIT;
328     }
329
330     for (; i >= 0; i--) {
331         word = bn_get_words(scalar)[i];
332         while (mask) {
333             BN_consttime_swap(word & mask, x1, x2, bn_get_top(group->field));
334             BN_consttime_swap(word & mask, z1, z2, bn_get_top(group->field));
335             if (!gf2m_Madd(group, point->X, x2, z2, x1, z1, ctx))
336                 goto err;
337             if (!gf2m_Mdouble(group, x1, z1, ctx))
338                 goto err;
339             BN_consttime_swap(word & mask, x1, x2, bn_get_top(group->field));
340             BN_consttime_swap(word & mask, z1, z2, bn_get_top(group->field));
341             mask >>= 1;
342         }
343         mask = BN_TBIT;
344     }
345
346     /* convert out of "projective" coordinates */
347     i = gf2m_Mxy(group, point->X, point->Y, x1, z1, x2, z2, ctx);
348     if (i == 0)
349         goto err;
350     else if (i == 1) {
351         if (!EC_POINT_set_to_infinity(group, r))
352             goto err;
353     } else {
354         if (!BN_one(r->Z))
355             goto err;
356         r->Z_is_one = 1;
357     }
358
359     /* GF(2^m) field elements should always have BIGNUM::neg = 0 */
360     BN_set_negative(r->X, 0);
361     BN_set_negative(r->Y, 0);
362
363     ret = 1;
364
365  err:
366     BN_CTX_end(ctx);
367     return ret;
368 }
369
370 /*-
371  * Computes the sum
372  *     scalar*group->generator + scalars[0]*points[0] + ... + scalars[num-1]*points[num-1]
373  * gracefully ignoring NULL scalar values.
374  */
375 int ec_GF2m_simple_mul(const EC_GROUP *group, EC_POINT *r,
376                        const BIGNUM *scalar, size_t num,
377                        const EC_POINT *points[], const BIGNUM *scalars[],
378                        BN_CTX *ctx)
379 {
380     BN_CTX *new_ctx = NULL;
381     int ret = 0;
382     size_t i;
383     EC_POINT *p = NULL;
384     EC_POINT *acc = NULL;
385
386     if (ctx == NULL) {
387         ctx = new_ctx = BN_CTX_new();
388         if (ctx == NULL)
389             return 0;
390     }
391
392     /*
393      * This implementation is more efficient than the wNAF implementation for
394      * 2 or fewer points.  Use the ec_wNAF_mul implementation for 3 or more
395      * points, or if we can perform a fast multiplication based on
396      * precomputation.
397      */
398     if ((scalar && (num > 1)) || (num > 2)
399         || (num == 0 && EC_GROUP_have_precompute_mult(group))) {
400         ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
401         goto err;
402     }
403
404     if ((p = EC_POINT_new(group)) == NULL)
405         goto err;
406     if ((acc = EC_POINT_new(group)) == NULL)
407         goto err;
408
409     if (!EC_POINT_set_to_infinity(group, acc))
410         goto err;
411
412     if (scalar) {
413         if (!ec_GF2m_montgomery_point_multiply
414             (group, p, scalar, group->generator, ctx))
415             goto err;
416         if (BN_is_negative(scalar))
417             if (!group->meth->invert(group, p, ctx))
418                 goto err;
419         if (!group->meth->add(group, acc, acc, p, ctx))
420             goto err;
421     }
422
423     for (i = 0; i < num; i++) {
424         if (!ec_GF2m_montgomery_point_multiply
425             (group, p, scalars[i], points[i], ctx))
426             goto err;
427         if (BN_is_negative(scalars[i]))
428             if (!group->meth->invert(group, p, ctx))
429                 goto err;
430         if (!group->meth->add(group, acc, acc, p, ctx))
431             goto err;
432     }
433
434     if (!EC_POINT_copy(r, acc))
435         goto err;
436
437     ret = 1;
438
439  err:
440     EC_POINT_free(p);
441     EC_POINT_free(acc);
442     BN_CTX_free(new_ctx);
443     return ret;
444 }
445
446 /*
447  * Precomputation for point multiplication: fall back to wNAF methods because
448  * ec_GF2m_simple_mul() uses ec_wNAF_mul() if appropriate
449  */
450
451 int ec_GF2m_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
452 {
453     return ec_wNAF_precompute_mult(group, ctx);
454 }
455
456 int ec_GF2m_have_precompute_mult(const EC_GROUP *group)
457 {
458     return ec_wNAF_have_precompute_mult(group);
459 }
460
461 #endif