2 * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* ====================================================================
11 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
13 * The Elliptic Curve Public-Key Crypto Library (ECC Code) included
14 * herein is developed by SUN MICROSYSTEMS, INC., and is contributed
15 * to the OpenSSL project.
17 * The ECC Code is licensed pursuant to the OpenSSL open source
18 * license provided below.
20 * The software is originally written by Sheueling Chang Shantz and
21 * Douglas Stebila of Sun Microsystems Laboratories.
25 #include <openssl/err.h>
27 #include "internal/bn_int.h"
30 #ifndef OPENSSL_NO_EC2M
33 * Compute the x-coordinate x/z for the point 2*(x/z) in Montgomery projective
35 * Uses algorithm Mdouble in appendix of
36 * Lopez, J. and Dahab, R. "Fast multiplication on elliptic curves over
37 * GF(2^m) without precomputation" (CHES '99, LNCS 1717).
38 * modified to not require precomputation of c=b^{2^{m-1}}.
40 static int gf2m_Mdouble(const EC_GROUP *group, BIGNUM *x, BIGNUM *z,
46 /* Since Mdouble is static we can guarantee that ctx != NULL. */
52 if (!group->meth->field_sqr(group, x, x, ctx))
54 if (!group->meth->field_sqr(group, t1, z, ctx))
56 if (!group->meth->field_mul(group, z, x, t1, ctx))
58 if (!group->meth->field_sqr(group, x, x, ctx))
60 if (!group->meth->field_sqr(group, t1, t1, ctx))
62 if (!group->meth->field_mul(group, t1, group->b, t1, ctx))
64 if (!BN_GF2m_add(x, x, t1))
75 * Compute the x-coordinate x1/z1 for the point (x1/z1)+(x2/x2) in Montgomery
76 * projective coordinates.
77 * Uses algorithm Madd in appendix of
78 * Lopez, J. and Dahab, R. "Fast multiplication on elliptic curves over
79 * GF(2^m) without precomputation" (CHES '99, LNCS 1717).
81 static int gf2m_Madd(const EC_GROUP *group, const BIGNUM *x, BIGNUM *x1,
82 BIGNUM *z1, const BIGNUM *x2, const BIGNUM *z2,
88 /* Since Madd is static we can guarantee that ctx != NULL. */
97 if (!group->meth->field_mul(group, x1, x1, z2, ctx))
99 if (!group->meth->field_mul(group, z1, z1, x2, ctx))
101 if (!group->meth->field_mul(group, t2, x1, z1, ctx))
103 if (!BN_GF2m_add(z1, z1, x1))
105 if (!group->meth->field_sqr(group, z1, z1, ctx))
107 if (!group->meth->field_mul(group, x1, z1, t1, ctx))
109 if (!BN_GF2m_add(x1, x1, t2))
120 * Compute the x, y affine coordinates from the point (x1, z1) (x2, z2)
121 * using Montgomery point multiplication algorithm Mxy() in appendix of
122 * Lopez, J. and Dahab, R. "Fast multiplication on elliptic curves over
123 * GF(2^m) without precomputation" (CHES '99, LNCS 1717).
126 * 1 if return value should be the point at infinity
129 static int gf2m_Mxy(const EC_GROUP *group, const BIGNUM *x, const BIGNUM *y,
130 BIGNUM *x1, BIGNUM *z1, BIGNUM *x2, BIGNUM *z2,
133 BIGNUM *t3, *t4, *t5;
136 if (BN_is_zero(z1)) {
142 if (BN_is_zero(z2)) {
145 if (!BN_GF2m_add(z2, x, y))
150 /* Since Mxy is static we can guarantee that ctx != NULL. */
152 t3 = BN_CTX_get(ctx);
153 t4 = BN_CTX_get(ctx);
154 t5 = BN_CTX_get(ctx);
161 if (!group->meth->field_mul(group, t3, z1, z2, ctx))
164 if (!group->meth->field_mul(group, z1, z1, x, ctx))
166 if (!BN_GF2m_add(z1, z1, x1))
168 if (!group->meth->field_mul(group, z2, z2, x, ctx))
170 if (!group->meth->field_mul(group, x1, z2, x1, ctx))
172 if (!BN_GF2m_add(z2, z2, x2))
175 if (!group->meth->field_mul(group, z2, z2, z1, ctx))
177 if (!group->meth->field_sqr(group, t4, x, ctx))
179 if (!BN_GF2m_add(t4, t4, y))
181 if (!group->meth->field_mul(group, t4, t4, t3, ctx))
183 if (!BN_GF2m_add(t4, t4, z2))
186 if (!group->meth->field_mul(group, t3, t3, x, ctx))
188 if (!group->meth->field_div(group, t3, t5, t3, ctx))
190 if (!group->meth->field_mul(group, t4, t3, t4, ctx))
192 if (!group->meth->field_mul(group, x2, x1, t3, ctx))
194 if (!BN_GF2m_add(z2, x2, x))
197 if (!group->meth->field_mul(group, z2, z2, t4, ctx))
199 if (!BN_GF2m_add(z2, z2, y))
210 * Computes scalar*point and stores the result in r.
211 * point can not equal r.
212 * Uses a modified algorithm 2P of
213 * Lopez, J. and Dahab, R. "Fast multiplication on elliptic curves over
214 * GF(2^m) without precomputation" (CHES '99, LNCS 1717).
216 * To protect against side-channel attack the function uses constant time swap,
217 * avoiding conditional branches.
219 static int ec_GF2m_montgomery_point_multiply(const EC_GROUP *group,
221 const BIGNUM *scalar,
222 const EC_POINT *point,
225 BIGNUM *x1, *x2, *z1, *z2;
230 ECerr(EC_F_EC_GF2M_MONTGOMERY_POINT_MULTIPLY, EC_R_INVALID_ARGUMENT);
234 /* if result should be point at infinity */
235 if ((scalar == NULL) || BN_is_zero(scalar) || (point == NULL) ||
236 EC_POINT_is_at_infinity(group, point)) {
237 return EC_POINT_set_to_infinity(group, r);
240 /* only support affine coordinates */
241 if (!point->Z_is_one)
245 * Since point_multiply is static we can guarantee that ctx != NULL.
248 x1 = BN_CTX_get(ctx);
249 z1 = BN_CTX_get(ctx);
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));
261 if (!BN_GF2m_mod_arr(x1, point->X, group->poly))
262 goto err; /* x1 = x */
264 goto err; /* z1 = 1 */
265 if (!group->meth->field_sqr(group, z2, x1, ctx))
266 goto err; /* z2 = x1^2 = x^2 */
267 if (!group->meth->field_sqr(group, x2, z2, ctx))
269 if (!BN_GF2m_add(x2, x2, group->b))
270 goto err; /* x2 = x^4 + b */
272 /* find top most bit and go one past it */
273 i = bn_get_top(scalar) - 1;
275 word = bn_get_words(scalar)[i];
276 while (!(word & mask))
279 /* if top most bit was at word break, go to next word */
285 for (; i >= 0; i--) {
286 word = bn_get_words(scalar)[i];
288 BN_consttime_swap(word & mask, x1, x2, bn_get_top(group->field));
289 BN_consttime_swap(word & mask, z1, z2, bn_get_top(group->field));
290 if (!gf2m_Madd(group, point->X, x2, z2, x1, z1, ctx))
292 if (!gf2m_Mdouble(group, x1, z1, ctx))
294 BN_consttime_swap(word & mask, x1, x2, bn_get_top(group->field));
295 BN_consttime_swap(word & mask, z1, z2, bn_get_top(group->field));
301 /* convert out of "projective" coordinates */
302 i = gf2m_Mxy(group, point->X, point->Y, x1, z1, x2, z2, ctx);
306 if (!EC_POINT_set_to_infinity(group, r))
314 /* GF(2^m) field elements should always have BIGNUM::neg = 0 */
315 BN_set_negative(r->X, 0);
316 BN_set_negative(r->Y, 0);
327 * scalar*group->generator + scalars[0]*points[0] + ... + scalars[num-1]*points[num-1]
328 * gracefully ignoring NULL scalar values.
330 int ec_GF2m_simple_mul(const EC_GROUP *group, EC_POINT *r,
331 const BIGNUM *scalar, size_t num,
332 const EC_POINT *points[], const BIGNUM *scalars[],
335 BN_CTX *new_ctx = NULL;
339 EC_POINT *acc = NULL;
342 ctx = new_ctx = BN_CTX_new();
348 * This implementation is more efficient than the wNAF implementation for
349 * 2 or fewer points. Use the ec_wNAF_mul implementation for 3 or more
350 * points, or if we can perform a fast multiplication based on
353 if ((scalar && (num > 1)) || (num > 2)
354 || (num == 0 && EC_GROUP_have_precompute_mult(group))) {
355 ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
359 if ((p = EC_POINT_new(group)) == NULL)
361 if ((acc = EC_POINT_new(group)) == NULL)
364 if (!EC_POINT_set_to_infinity(group, acc))
368 if (!ec_GF2m_montgomery_point_multiply
369 (group, p, scalar, group->generator, ctx))
371 if (BN_is_negative(scalar))
372 if (!group->meth->invert(group, p, ctx))
374 if (!group->meth->add(group, acc, acc, p, ctx))
378 for (i = 0; i < num; i++) {
379 if (!ec_GF2m_montgomery_point_multiply
380 (group, p, scalars[i], points[i], ctx))
382 if (BN_is_negative(scalars[i]))
383 if (!group->meth->invert(group, p, ctx))
385 if (!group->meth->add(group, acc, acc, p, ctx))
389 if (!EC_POINT_copy(r, acc))
397 BN_CTX_free(new_ctx);
402 * Precomputation for point multiplication: fall back to wNAF methods because
403 * ec_GF2m_simple_mul() uses ec_wNAF_mul() if appropriate
406 int ec_GF2m_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
408 return ec_wNAF_precompute_mult(group, ctx);
411 int ec_GF2m_have_precompute_mult(const EC_GROUP *group)
413 return ec_wNAF_have_precompute_mult(group);