4272N/A/*
4272N/A * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
4272N/A * Use is subject to license terms.
4272N/A *
4272N/A * This library is free software; you can redistribute it and/or
4272N/A * modify it under the terms of the GNU Lesser General Public
4272N/A * License as published by the Free Software Foundation; either
4272N/A * version 2.1 of the License, or (at your option) any later version.
1674N/A *
4272N/A * This library is distributed in the hope that it will be useful,
4272N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
4272N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4272N/A * Lesser General Public License for more details.
1674N/A *
4272N/A * You should have received a copy of the GNU Lesser General Public License
4272N/A * along with this library; if not, write to the Free Software Foundation,
4272N/A * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1674N/A *
4272N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4272N/A * or visit www.oracle.com if you need additional information or have any
4272N/A * questions.
4272N/A */
4272N/A
4272N/A/* *********************************************************************
1674N/A *
1674N/A * The Original Code is the elliptic curve math library for binary polynomial field curves.
1674N/A *
1674N/A * The Initial Developer of the Original Code is
1674N/A * Sun Microsystems, Inc.
1674N/A * Portions created by the Initial Developer are Copyright (C) 2003
1674N/A * the Initial Developer. All Rights Reserved.
1674N/A *
1674N/A * Contributor(s):
1674N/A * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
1674N/A *
1674N/A *********************************************************************** */
1674N/A
1674N/A#include "ec2.h"
1674N/A#include "mplogic.h"
1674N/A#include "mp_gf2m.h"
1674N/A#ifndef _KERNEL
1674N/A#include <stdlib.h>
1674N/A#endif
1674N/A
1674N/A/* Checks if point P(px, py) is at infinity. Uses affine coordinates. */
1674N/Amp_err
1674N/Aec_GF2m_pt_is_inf_aff(const mp_int *px, const mp_int *py)
1674N/A{
1674N/A
1674N/A if ((mp_cmp_z(px) == 0) && (mp_cmp_z(py) == 0)) {
1674N/A return MP_YES;
1674N/A } else {
1674N/A return MP_NO;
1674N/A }
1674N/A
1674N/A}
1674N/A
1674N/A/* Sets P(px, py) to be the point at infinity. Uses affine coordinates. */
1674N/Amp_err
1674N/Aec_GF2m_pt_set_inf_aff(mp_int *px, mp_int *py)
1674N/A{
1674N/A mp_zero(px);
1674N/A mp_zero(py);
1674N/A return MP_OKAY;
1674N/A}
1674N/A
1674N/A/* Computes R = P + Q based on IEEE P1363 A.10.2. Elliptic curve points P,
1674N/A * Q, and R can all be identical. Uses affine coordinates. */
1674N/Amp_err
1674N/Aec_GF2m_pt_add_aff(const mp_int *px, const mp_int *py, const mp_int *qx,
1674N/A const mp_int *qy, mp_int *rx, mp_int *ry,
1674N/A const ECGroup *group)
1674N/A{
1674N/A mp_err res = MP_OKAY;
1674N/A mp_int lambda, tempx, tempy;
1674N/A
1674N/A MP_DIGITS(&lambda) = 0;
1674N/A MP_DIGITS(&tempx) = 0;
1674N/A MP_DIGITS(&tempy) = 0;
1674N/A MP_CHECKOK(mp_init(&lambda, FLAG(px)));
1674N/A MP_CHECKOK(mp_init(&tempx, FLAG(px)));
1674N/A MP_CHECKOK(mp_init(&tempy, FLAG(px)));
1674N/A /* if P = inf, then R = Q */
1674N/A if (ec_GF2m_pt_is_inf_aff(px, py) == 0) {
1674N/A MP_CHECKOK(mp_copy(qx, rx));
1674N/A MP_CHECKOK(mp_copy(qy, ry));
1674N/A res = MP_OKAY;
1674N/A goto CLEANUP;
1674N/A }
1674N/A /* if Q = inf, then R = P */
1674N/A if (ec_GF2m_pt_is_inf_aff(qx, qy) == 0) {
1674N/A MP_CHECKOK(mp_copy(px, rx));
1674N/A MP_CHECKOK(mp_copy(py, ry));
1674N/A res = MP_OKAY;
1674N/A goto CLEANUP;
1674N/A }
1674N/A /* if px != qx, then lambda = (py+qy) / (px+qx), tempx = a + lambda^2
1674N/A * + lambda + px + qx */
1674N/A if (mp_cmp(px, qx) != 0) {
1674N/A MP_CHECKOK(group->meth->field_add(py, qy, &tempy, group->meth));
1674N/A MP_CHECKOK(group->meth->field_add(px, qx, &tempx, group->meth));
1674N/A MP_CHECKOK(group->meth->
1674N/A field_div(&tempy, &tempx, &lambda, group->meth));
1674N/A MP_CHECKOK(group->meth->field_sqr(&lambda, &tempx, group->meth));
1674N/A MP_CHECKOK(group->meth->
1674N/A field_add(&tempx, &lambda, &tempx, group->meth));
1674N/A MP_CHECKOK(group->meth->
1674N/A field_add(&tempx, &group->curvea, &tempx, group->meth));
1674N/A MP_CHECKOK(group->meth->
1674N/A field_add(&tempx, px, &tempx, group->meth));
1674N/A MP_CHECKOK(group->meth->
1674N/A field_add(&tempx, qx, &tempx, group->meth));
1674N/A } else {
1674N/A /* if py != qy or qx = 0, then R = inf */
1674N/A if (((mp_cmp(py, qy) != 0)) || (mp_cmp_z(qx) == 0)) {
1674N/A mp_zero(rx);
1674N/A mp_zero(ry);
1674N/A res = MP_OKAY;
1674N/A goto CLEANUP;
1674N/A }
1674N/A /* lambda = qx + qy / qx */
1674N/A MP_CHECKOK(group->meth->field_div(qy, qx, &lambda, group->meth));
1674N/A MP_CHECKOK(group->meth->
1674N/A field_add(&lambda, qx, &lambda, group->meth));
1674N/A /* tempx = a + lambda^2 + lambda */
1674N/A MP_CHECKOK(group->meth->field_sqr(&lambda, &tempx, group->meth));
1674N/A MP_CHECKOK(group->meth->
1674N/A field_add(&tempx, &lambda, &tempx, group->meth));
1674N/A MP_CHECKOK(group->meth->
1674N/A field_add(&tempx, &group->curvea, &tempx, group->meth));
1674N/A }
1674N/A /* ry = (qx + tempx) * lambda + tempx + qy */
1674N/A MP_CHECKOK(group->meth->field_add(qx, &tempx, &tempy, group->meth));
1674N/A MP_CHECKOK(group->meth->
1674N/A field_mul(&tempy, &lambda, &tempy, group->meth));
1674N/A MP_CHECKOK(group->meth->
1674N/A field_add(&tempy, &tempx, &tempy, group->meth));
1674N/A MP_CHECKOK(group->meth->field_add(&tempy, qy, ry, group->meth));
1674N/A /* rx = tempx */
1674N/A MP_CHECKOK(mp_copy(&tempx, rx));
1674N/A
1674N/A CLEANUP:
1674N/A mp_clear(&lambda);
1674N/A mp_clear(&tempx);
1674N/A mp_clear(&tempy);
1674N/A return res;
1674N/A}
1674N/A
1674N/A/* Computes R = P - Q. Elliptic curve points P, Q, and R can all be
1674N/A * identical. Uses affine coordinates. */
1674N/Amp_err
1674N/Aec_GF2m_pt_sub_aff(const mp_int *px, const mp_int *py, const mp_int *qx,
1674N/A const mp_int *qy, mp_int *rx, mp_int *ry,
1674N/A const ECGroup *group)
1674N/A{
1674N/A mp_err res = MP_OKAY;
1674N/A mp_int nqy;
1674N/A
1674N/A MP_DIGITS(&nqy) = 0;
1674N/A MP_CHECKOK(mp_init(&nqy, FLAG(px)));
1674N/A /* nqy = qx+qy */
1674N/A MP_CHECKOK(group->meth->field_add(qx, qy, &nqy, group->meth));
1674N/A MP_CHECKOK(group->point_add(px, py, qx, &nqy, rx, ry, group));
1674N/A CLEANUP:
1674N/A mp_clear(&nqy);
1674N/A return res;
1674N/A}
1674N/A
1674N/A/* Computes R = 2P. Elliptic curve points P and R can be identical. Uses
1674N/A * affine coordinates. */
1674N/Amp_err
1674N/Aec_GF2m_pt_dbl_aff(const mp_int *px, const mp_int *py, mp_int *rx,
1674N/A mp_int *ry, const ECGroup *group)
1674N/A{
1674N/A return group->point_add(px, py, px, py, rx, ry, group);
1674N/A}
1674N/A
1674N/A/* by default, this routine is unused and thus doesn't need to be compiled */
1674N/A#ifdef ECL_ENABLE_GF2M_PT_MUL_AFF
1674N/A/* Computes R = nP based on IEEE P1363 A.10.3. Elliptic curve points P and
1674N/A * R can be identical. Uses affine coordinates. */
1674N/Amp_err
1674N/Aec_GF2m_pt_mul_aff(const mp_int *n, const mp_int *px, const mp_int *py,
1674N/A mp_int *rx, mp_int *ry, const ECGroup *group)
1674N/A{
1674N/A mp_err res = MP_OKAY;
1674N/A mp_int k, k3, qx, qy, sx, sy;
1674N/A int b1, b3, i, l;
1674N/A
1674N/A MP_DIGITS(&k) = 0;
1674N/A MP_DIGITS(&k3) = 0;
1674N/A MP_DIGITS(&qx) = 0;
1674N/A MP_DIGITS(&qy) = 0;
1674N/A MP_DIGITS(&sx) = 0;
1674N/A MP_DIGITS(&sy) = 0;
1674N/A MP_CHECKOK(mp_init(&k));
1674N/A MP_CHECKOK(mp_init(&k3));
1674N/A MP_CHECKOK(mp_init(&qx));
1674N/A MP_CHECKOK(mp_init(&qy));
1674N/A MP_CHECKOK(mp_init(&sx));
1674N/A MP_CHECKOK(mp_init(&sy));
1674N/A
1674N/A /* if n = 0 then r = inf */
1674N/A if (mp_cmp_z(n) == 0) {
1674N/A mp_zero(rx);
1674N/A mp_zero(ry);
1674N/A res = MP_OKAY;
1674N/A goto CLEANUP;
1674N/A }
1674N/A /* Q = P, k = n */
1674N/A MP_CHECKOK(mp_copy(px, &qx));
1674N/A MP_CHECKOK(mp_copy(py, &qy));
1674N/A MP_CHECKOK(mp_copy(n, &k));
1674N/A /* if n < 0 then Q = -Q, k = -k */
1674N/A if (mp_cmp_z(n) < 0) {
1674N/A MP_CHECKOK(group->meth->field_add(&qx, &qy, &qy, group->meth));
1674N/A MP_CHECKOK(mp_neg(&k, &k));
1674N/A }
1674N/A#ifdef ECL_DEBUG /* basic double and add method */
1674N/A l = mpl_significant_bits(&k) - 1;
1674N/A MP_CHECKOK(mp_copy(&qx, &sx));
1674N/A MP_CHECKOK(mp_copy(&qy, &sy));
1674N/A for (i = l - 1; i >= 0; i--) {
1674N/A /* S = 2S */
1674N/A MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group));
1674N/A /* if k_i = 1, then S = S + Q */
1674N/A if (mpl_get_bit(&k, i) != 0) {
1674N/A MP_CHECKOK(group->
1674N/A point_add(&sx, &sy, &qx, &qy, &sx, &sy, group));
1674N/A }
1674N/A }
1674N/A#else /* double and add/subtract method from
1674N/A * standard */
1674N/A /* k3 = 3 * k */
1674N/A MP_CHECKOK(mp_set_int(&k3, 3));
1674N/A MP_CHECKOK(mp_mul(&k, &k3, &k3));
1674N/A /* S = Q */
1674N/A MP_CHECKOK(mp_copy(&qx, &sx));
1674N/A MP_CHECKOK(mp_copy(&qy, &sy));
1674N/A /* l = index of high order bit in binary representation of 3*k */
1674N/A l = mpl_significant_bits(&k3) - 1;
1674N/A /* for i = l-1 downto 1 */
1674N/A for (i = l - 1; i >= 1; i--) {
1674N/A /* S = 2S */
1674N/A MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group));
1674N/A b3 = MP_GET_BIT(&k3, i);
1674N/A b1 = MP_GET_BIT(&k, i);
1674N/A /* if k3_i = 1 and k_i = 0, then S = S + Q */
1674N/A if ((b3 == 1) && (b1 == 0)) {
1674N/A MP_CHECKOK(group->
1674N/A point_add(&sx, &sy, &qx, &qy, &sx, &sy, group));
1674N/A /* if k3_i = 0 and k_i = 1, then S = S - Q */
1674N/A } else if ((b3 == 0) && (b1 == 1)) {
1674N/A MP_CHECKOK(group->
1674N/A point_sub(&sx, &sy, &qx, &qy, &sx, &sy, group));
1674N/A }
1674N/A }
1674N/A#endif
1674N/A /* output S */
1674N/A MP_CHECKOK(mp_copy(&sx, rx));
1674N/A MP_CHECKOK(mp_copy(&sy, ry));
1674N/A
1674N/A CLEANUP:
1674N/A mp_clear(&k);
1674N/A mp_clear(&k3);
1674N/A mp_clear(&qx);
1674N/A mp_clear(&qy);
1674N/A mp_clear(&sx);
1674N/A mp_clear(&sy);
1674N/A return res;
1674N/A}
1674N/A#endif
1674N/A
1674N/A/* Validates a point on a GF2m curve. */
1674N/Amp_err
1674N/Aec_GF2m_validate_point(const mp_int *px, const mp_int *py, const ECGroup *group)
1674N/A{
1674N/A mp_err res = MP_NO;
1674N/A mp_int accl, accr, tmp, pxt, pyt;
1674N/A
1674N/A MP_DIGITS(&accl) = 0;
1674N/A MP_DIGITS(&accr) = 0;
1674N/A MP_DIGITS(&tmp) = 0;
1674N/A MP_DIGITS(&pxt) = 0;
1674N/A MP_DIGITS(&pyt) = 0;
1674N/A MP_CHECKOK(mp_init(&accl, FLAG(px)));
1674N/A MP_CHECKOK(mp_init(&accr, FLAG(px)));
1674N/A MP_CHECKOK(mp_init(&tmp, FLAG(px)));
1674N/A MP_CHECKOK(mp_init(&pxt, FLAG(px)));
1674N/A MP_CHECKOK(mp_init(&pyt, FLAG(px)));
1674N/A
1674N/A /* 1: Verify that publicValue is not the point at infinity */
1674N/A if (ec_GF2m_pt_is_inf_aff(px, py) == MP_YES) {
1674N/A res = MP_NO;
1674N/A goto CLEANUP;
1674N/A }
1674N/A /* 2: Verify that the coordinates of publicValue are elements
1674N/A * of the field.
1674N/A */
1674N/A if ((MP_SIGN(px) == MP_NEG) || (mp_cmp(px, &group->meth->irr) >= 0) ||
1674N/A (MP_SIGN(py) == MP_NEG) || (mp_cmp(py, &group->meth->irr) >= 0)) {
1674N/A res = MP_NO;
1674N/A goto CLEANUP;
1674N/A }
1674N/A /* 3: Verify that publicValue is on the curve. */
1674N/A if (group->meth->field_enc) {
1674N/A group->meth->field_enc(px, &pxt, group->meth);
1674N/A group->meth->field_enc(py, &pyt, group->meth);
1674N/A } else {
1674N/A mp_copy(px, &pxt);
1674N/A mp_copy(py, &pyt);
1674N/A }
1674N/A /* left-hand side: y^2 + x*y */
1674N/A MP_CHECKOK( group->meth->field_sqr(&pyt, &accl, group->meth) );
1674N/A MP_CHECKOK( group->meth->field_mul(&pxt, &pyt, &tmp, group->meth) );
1674N/A MP_CHECKOK( group->meth->field_add(&accl, &tmp, &accl, group->meth) );
1674N/A /* right-hand side: x^3 + a*x^2 + b */
1674N/A MP_CHECKOK( group->meth->field_sqr(&pxt, &tmp, group->meth) );
1674N/A MP_CHECKOK( group->meth->field_mul(&pxt, &tmp, &accr, group->meth) );
1674N/A MP_CHECKOK( group->meth->field_mul(&group->curvea, &tmp, &tmp, group->meth) );
1674N/A MP_CHECKOK( group->meth->field_add(&tmp, &accr, &accr, group->meth) );
1674N/A MP_CHECKOK( group->meth->field_add(&accr, &group->curveb, &accr, group->meth) );
1674N/A /* check LHS - RHS == 0 */
1674N/A MP_CHECKOK( group->meth->field_add(&accl, &accr, &accr, group->meth) );
1674N/A if (mp_cmp_z(&accr) != 0) {
1674N/A res = MP_NO;
1674N/A goto CLEANUP;
1674N/A }
1674N/A /* 4: Verify that the order of the curve times the publicValue
1674N/A * is the point at infinity.
1674N/A */
1674N/A MP_CHECKOK( ECPoint_mul(group, &group->order, px, py, &pxt, &pyt) );
1674N/A if (ec_GF2m_pt_is_inf_aff(&pxt, &pyt) != MP_YES) {
1674N/A res = MP_NO;
1674N/A goto CLEANUP;
1674N/A }
1674N/A
1674N/A res = MP_YES;
1674N/A
1674N/ACLEANUP:
1674N/A mp_clear(&accl);
1674N/A mp_clear(&accr);
1674N/A mp_clear(&tmp);
1674N/A mp_clear(&pxt);
1674N/A mp_clear(&pyt);
1674N/A return res;
1674N/A}