1 /* crypto/ec/ec2_mult.c */
2 /* ====================================================================
3 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
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.
9 * The ECC Code is licensed pursuant to the OpenSSL open source
10 * license provided below.
12 * The software is originally written by Sheueling Chang Shantz and
13 * Douglas Stebila of Sun Microsystems Laboratories.
16 /* ====================================================================
17 * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
45 * 6. Redistributions of any form whatsoever must retain the following
47 * "This product includes software developed by the OpenSSL Project
48 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
70 #include <openssl/err.h>
75 * Compute the x-coordinate x/z for the point 2*(x/z) in Montgomery projective
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}}.
82 static int gf2m_Mdouble(const EC_GROUP
*group
, BIGNUM
*x
, BIGNUM
*z
,
88 /* Since Mdouble is static we can guarantee that ctx != NULL. */
94 if (!group
->meth
->field_sqr(group
, x
, x
, ctx
))
96 if (!group
->meth
->field_sqr(group
, t1
, z
, ctx
))
98 if (!group
->meth
->field_mul(group
, z
, x
, t1
, ctx
))
100 if (!group
->meth
->field_sqr(group
, x
, x
, ctx
))
102 if (!group
->meth
->field_sqr(group
, t1
, t1
, ctx
))
104 if (!group
->meth
->field_mul(group
, t1
, &group
->b
, t1
, ctx
))
106 if (!BN_GF2m_add(x
, x
, t1
))
117 * Compute the x-coordinate x1/z1 for the point (x1/z1)+(x2/x2) in Montgomery
118 * projective coordinates.
119 * Uses algorithm Madd in appendix of
120 * Lopex, J. and Dahab, R. "Fast multiplication on elliptic curves over
121 * GF(2^m) without precomputation".
123 static int gf2m_Madd(const EC_GROUP
*group
, const BIGNUM
*x
, BIGNUM
*x1
,
124 BIGNUM
*z1
, const BIGNUM
*x2
, const BIGNUM
*z2
,
130 /* Since Madd is static we can guarantee that ctx != NULL. */
132 t1
= BN_CTX_get(ctx
);
133 t2
= BN_CTX_get(ctx
);
139 if (!group
->meth
->field_mul(group
, x1
, x1
, z2
, ctx
))
141 if (!group
->meth
->field_mul(group
, z1
, z1
, x2
, ctx
))
143 if (!group
->meth
->field_mul(group
, t2
, x1
, z1
, ctx
))
145 if (!BN_GF2m_add(z1
, z1
, x1
))
147 if (!group
->meth
->field_sqr(group
, z1
, z1
, ctx
))
149 if (!group
->meth
->field_mul(group
, x1
, z1
, t1
, ctx
))
151 if (!BN_GF2m_add(x1
, x1
, t2
))
162 * Compute the x, y affine coordinates from the point (x1, z1) (x2, z2)
163 * using Montgomery point multiplication algorithm Mxy() in appendix of
164 * Lopex, J. and Dahab, R. "Fast multiplication on elliptic curves over
165 * GF(2^m) without precomputation".
168 * 1 if return value should be the point at infinity
171 static int gf2m_Mxy(const EC_GROUP
*group
, const BIGNUM
*x
, const BIGNUM
*y
,
172 BIGNUM
*x1
, BIGNUM
*z1
, BIGNUM
*x2
, BIGNUM
*z2
,
175 BIGNUM
*t3
, *t4
, *t5
;
178 if (BN_is_zero(z1
)) {
184 if (BN_is_zero(z2
)) {
187 if (!BN_GF2m_add(z2
, x
, y
))
192 /* Since Mxy is static we can guarantee that ctx != NULL. */
194 t3
= BN_CTX_get(ctx
);
195 t4
= BN_CTX_get(ctx
);
196 t5
= BN_CTX_get(ctx
);
203 if (!group
->meth
->field_mul(group
, t3
, z1
, z2
, ctx
))
206 if (!group
->meth
->field_mul(group
, z1
, z1
, x
, ctx
))
208 if (!BN_GF2m_add(z1
, z1
, x1
))
210 if (!group
->meth
->field_mul(group
, z2
, z2
, x
, ctx
))
212 if (!group
->meth
->field_mul(group
, x1
, z2
, x1
, ctx
))
214 if (!BN_GF2m_add(z2
, z2
, x2
))
217 if (!group
->meth
->field_mul(group
, z2
, z2
, z1
, ctx
))
219 if (!group
->meth
->field_sqr(group
, t4
, x
, ctx
))
221 if (!BN_GF2m_add(t4
, t4
, y
))
223 if (!group
->meth
->field_mul(group
, t4
, t4
, t3
, ctx
))
225 if (!BN_GF2m_add(t4
, t4
, z2
))
228 if (!group
->meth
->field_mul(group
, t3
, t3
, x
, ctx
))
230 if (!group
->meth
->field_div(group
, t3
, t5
, t3
, ctx
))
232 if (!group
->meth
->field_mul(group
, t4
, t3
, t4
, ctx
))
234 if (!group
->meth
->field_mul(group
, x2
, x1
, t3
, ctx
))
236 if (!BN_GF2m_add(z2
, x2
, x
))
239 if (!group
->meth
->field_mul(group
, z2
, z2
, t4
, ctx
))
241 if (!BN_GF2m_add(z2
, z2
, y
))
252 * Computes scalar*point and stores the result in r.
253 * point can not equal r.
254 * Uses a modified algorithm 2P of
255 * Lopex, J. and Dahab, R. "Fast multiplication on elliptic curves over
256 * GF(2^m) without precomputation".
258 * To protect against side-channel attack the function uses constant time
259 * swap avoiding conditional branches.
261 static int ec_GF2m_montgomery_point_multiply(const EC_GROUP
*group
,
263 const BIGNUM
*scalar
,
264 const EC_POINT
*point
,
267 BIGNUM
*x1
, *x2
, *z1
, *z2
;
272 ECerr(EC_F_EC_GF2M_MONTGOMERY_POINT_MULTIPLY
, EC_R_INVALID_ARGUMENT
);
276 /* if result should be point at infinity */
277 if ((scalar
== NULL
) || BN_is_zero(scalar
) || (point
== NULL
) ||
278 EC_POINT_is_at_infinity(group
, point
)) {
279 return EC_POINT_set_to_infinity(group
, r
);
282 /* only support affine coordinates */
283 if (!point
->Z_is_one
)
287 * Since point_multiply is static we can guarantee that ctx != NULL.
290 x1
= BN_CTX_get(ctx
);
291 z1
= BN_CTX_get(ctx
);
298 bn_wexpand(x1
, group
->field
.top
);
299 bn_wexpand(z1
, group
->field
.top
);
300 bn_wexpand(x2
, group
->field
.top
);
301 bn_wexpand(z2
, group
->field
.top
);
303 if (!BN_GF2m_mod_arr(x1
, &point
->X
, group
->poly
))
304 goto err
; /* x1 = x */
306 goto err
; /* z1 = 1 */
307 if (!group
->meth
->field_sqr(group
, z2
, x1
, ctx
))
308 goto err
; /* z2 = x1^2 = x^2 */
309 if (!group
->meth
->field_sqr(group
, x2
, z2
, ctx
))
311 if (!BN_GF2m_add(x2
, x2
, &group
->b
))
312 goto err
; /* x2 = x^4 + b */
314 /* find top most bit and go one past it */
318 while (!(scalar
->d
[i
] & mask
)) {
324 /* if top most bit was at word break, go to next word */
331 for (; i
>= 0; i
--) {
332 for (; j
>= 0; j
--) {
333 BN_consttime_swap(scalar
->d
[i
] & mask
, x1
, x2
, group
->field
.top
);
334 BN_consttime_swap(scalar
->d
[i
] & mask
, z1
, z2
, group
->field
.top
);
335 if (!gf2m_Madd(group
, &point
->X
, x2
, z2
, x1
, z1
, ctx
))
337 if (!gf2m_Mdouble(group
, x1
, z1
, ctx
))
339 BN_consttime_swap(scalar
->d
[i
] & mask
, x1
, x2
, group
->field
.top
);
340 BN_consttime_swap(scalar
->d
[i
] & mask
, z1
, z2
, group
->field
.top
);
347 /* convert out of "projective" coordinates */
348 i
= gf2m_Mxy(group
, &point
->X
, &point
->Y
, x1
, z1
, x2
, z2
, ctx
);
352 if (!EC_POINT_set_to_infinity(group
, r
))
360 /* GF(2^m) field elements should always have BIGNUM::neg = 0 */
361 BN_set_negative(&r
->X
, 0);
362 BN_set_negative(&r
->Y
, 0);
373 * scalar*group->generator + scalars[0]*points[0] + ... + scalars[num-1]*points[num-1]
374 * gracefully ignoring NULL scalar values.
376 int ec_GF2m_simple_mul(const EC_GROUP
*group
, EC_POINT
*r
,
377 const BIGNUM
*scalar
, size_t num
,
378 const EC_POINT
*points
[], const BIGNUM
*scalars
[],
381 BN_CTX
*new_ctx
= NULL
;
385 EC_POINT
*acc
= NULL
;
388 ctx
= new_ctx
= BN_CTX_new();
394 * This implementation is more efficient than the wNAF implementation for
395 * 2 or fewer points. Use the ec_wNAF_mul implementation for 3 or more
396 * points, or if we can perform a fast multiplication based on
399 if ((scalar
&& (num
> 1)) || (num
> 2)
400 || (num
== 0 && EC_GROUP_have_precompute_mult(group
))) {
401 ret
= ec_wNAF_mul(group
, r
, scalar
, num
, points
, scalars
, ctx
);
405 if ((p
= EC_POINT_new(group
)) == NULL
)
407 if ((acc
= EC_POINT_new(group
)) == NULL
)
410 if (!EC_POINT_set_to_infinity(group
, acc
))
414 if (!ec_GF2m_montgomery_point_multiply
415 (group
, p
, scalar
, group
->generator
, ctx
))
417 if (BN_is_negative(scalar
))
418 if (!group
->meth
->invert(group
, p
, ctx
))
420 if (!group
->meth
->add(group
, acc
, acc
, p
, ctx
))
424 for (i
= 0; i
< num
; i
++) {
425 if (!ec_GF2m_montgomery_point_multiply
426 (group
, p
, scalars
[i
], points
[i
], ctx
))
428 if (BN_is_negative(scalars
[i
]))
429 if (!group
->meth
->invert(group
, p
, ctx
))
431 if (!group
->meth
->add(group
, acc
, acc
, p
, ctx
))
435 if (!EC_POINT_copy(r
, acc
))
446 BN_CTX_free(new_ctx
);
451 * Precomputation for point multiplication: fall back to wNAF methods because
452 * ec_GF2m_simple_mul() uses ec_wNAF_mul() if appropriate
455 int ec_GF2m_precompute_mult(EC_GROUP
*group
, BN_CTX
*ctx
)
457 return ec_wNAF_precompute_mult(group
, ctx
);
460 int ec_GF2m_have_precompute_mult(const EC_GROUP
*group
)
462 return ec_wNAF_have_precompute_mult(group
);