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