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