michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "ec2.h" michael@0: #include "mplogic.h" michael@0: #include "mp_gf2m.h" michael@0: #include michael@0: michael@0: /* Checks if point P(px, py) is at infinity. Uses affine coordinates. */ michael@0: mp_err michael@0: ec_GF2m_pt_is_inf_aff(const mp_int *px, const mp_int *py) michael@0: { michael@0: michael@0: if ((mp_cmp_z(px) == 0) && (mp_cmp_z(py) == 0)) { michael@0: return MP_YES; michael@0: } else { michael@0: return MP_NO; michael@0: } michael@0: michael@0: } michael@0: michael@0: /* Sets P(px, py) to be the point at infinity. Uses affine coordinates. */ michael@0: mp_err michael@0: ec_GF2m_pt_set_inf_aff(mp_int *px, mp_int *py) michael@0: { michael@0: mp_zero(px); michael@0: mp_zero(py); michael@0: return MP_OKAY; michael@0: } michael@0: michael@0: /* Computes R = P + Q based on IEEE P1363 A.10.2. Elliptic curve points P, michael@0: * Q, and R can all be identical. Uses affine coordinates. */ michael@0: mp_err michael@0: ec_GF2m_pt_add_aff(const mp_int *px, const mp_int *py, const mp_int *qx, michael@0: const mp_int *qy, mp_int *rx, mp_int *ry, michael@0: const ECGroup *group) michael@0: { michael@0: mp_err res = MP_OKAY; michael@0: mp_int lambda, tempx, tempy; michael@0: michael@0: MP_DIGITS(&lambda) = 0; michael@0: MP_DIGITS(&tempx) = 0; michael@0: MP_DIGITS(&tempy) = 0; michael@0: MP_CHECKOK(mp_init(&lambda)); michael@0: MP_CHECKOK(mp_init(&tempx)); michael@0: MP_CHECKOK(mp_init(&tempy)); michael@0: /* if P = inf, then R = Q */ michael@0: if (ec_GF2m_pt_is_inf_aff(px, py) == 0) { michael@0: MP_CHECKOK(mp_copy(qx, rx)); michael@0: MP_CHECKOK(mp_copy(qy, ry)); michael@0: res = MP_OKAY; michael@0: goto CLEANUP; michael@0: } michael@0: /* if Q = inf, then R = P */ michael@0: if (ec_GF2m_pt_is_inf_aff(qx, qy) == 0) { michael@0: MP_CHECKOK(mp_copy(px, rx)); michael@0: MP_CHECKOK(mp_copy(py, ry)); michael@0: res = MP_OKAY; michael@0: goto CLEANUP; michael@0: } michael@0: /* if px != qx, then lambda = (py+qy) / (px+qx), tempx = a + lambda^2 michael@0: * + lambda + px + qx */ michael@0: if (mp_cmp(px, qx) != 0) { michael@0: MP_CHECKOK(group->meth->field_add(py, qy, &tempy, group->meth)); michael@0: MP_CHECKOK(group->meth->field_add(px, qx, &tempx, group->meth)); michael@0: MP_CHECKOK(group->meth-> michael@0: field_div(&tempy, &tempx, &lambda, group->meth)); michael@0: MP_CHECKOK(group->meth->field_sqr(&lambda, &tempx, group->meth)); michael@0: MP_CHECKOK(group->meth-> michael@0: field_add(&tempx, &lambda, &tempx, group->meth)); michael@0: MP_CHECKOK(group->meth-> michael@0: field_add(&tempx, &group->curvea, &tempx, group->meth)); michael@0: MP_CHECKOK(group->meth-> michael@0: field_add(&tempx, px, &tempx, group->meth)); michael@0: MP_CHECKOK(group->meth-> michael@0: field_add(&tempx, qx, &tempx, group->meth)); michael@0: } else { michael@0: /* if py != qy or qx = 0, then R = inf */ michael@0: if (((mp_cmp(py, qy) != 0)) || (mp_cmp_z(qx) == 0)) { michael@0: mp_zero(rx); michael@0: mp_zero(ry); michael@0: res = MP_OKAY; michael@0: goto CLEANUP; michael@0: } michael@0: /* lambda = qx + qy / qx */ michael@0: MP_CHECKOK(group->meth->field_div(qy, qx, &lambda, group->meth)); michael@0: MP_CHECKOK(group->meth-> michael@0: field_add(&lambda, qx, &lambda, group->meth)); michael@0: /* tempx = a + lambda^2 + lambda */ michael@0: MP_CHECKOK(group->meth->field_sqr(&lambda, &tempx, group->meth)); michael@0: MP_CHECKOK(group->meth-> michael@0: field_add(&tempx, &lambda, &tempx, group->meth)); michael@0: MP_CHECKOK(group->meth-> michael@0: field_add(&tempx, &group->curvea, &tempx, group->meth)); michael@0: } michael@0: /* ry = (qx + tempx) * lambda + tempx + qy */ michael@0: MP_CHECKOK(group->meth->field_add(qx, &tempx, &tempy, group->meth)); michael@0: MP_CHECKOK(group->meth-> michael@0: field_mul(&tempy, &lambda, &tempy, group->meth)); michael@0: MP_CHECKOK(group->meth-> michael@0: field_add(&tempy, &tempx, &tempy, group->meth)); michael@0: MP_CHECKOK(group->meth->field_add(&tempy, qy, ry, group->meth)); michael@0: /* rx = tempx */ michael@0: MP_CHECKOK(mp_copy(&tempx, rx)); michael@0: michael@0: CLEANUP: michael@0: mp_clear(&lambda); michael@0: mp_clear(&tempx); michael@0: mp_clear(&tempy); michael@0: return res; michael@0: } michael@0: michael@0: /* Computes R = P - Q. Elliptic curve points P, Q, and R can all be michael@0: * identical. Uses affine coordinates. */ michael@0: mp_err michael@0: ec_GF2m_pt_sub_aff(const mp_int *px, const mp_int *py, const mp_int *qx, michael@0: const mp_int *qy, mp_int *rx, mp_int *ry, michael@0: const ECGroup *group) michael@0: { michael@0: mp_err res = MP_OKAY; michael@0: mp_int nqy; michael@0: michael@0: MP_DIGITS(&nqy) = 0; michael@0: MP_CHECKOK(mp_init(&nqy)); michael@0: /* nqy = qx+qy */ michael@0: MP_CHECKOK(group->meth->field_add(qx, qy, &nqy, group->meth)); michael@0: MP_CHECKOK(group->point_add(px, py, qx, &nqy, rx, ry, group)); michael@0: CLEANUP: michael@0: mp_clear(&nqy); michael@0: return res; michael@0: } michael@0: michael@0: /* Computes R = 2P. Elliptic curve points P and R can be identical. Uses michael@0: * affine coordinates. */ michael@0: mp_err michael@0: ec_GF2m_pt_dbl_aff(const mp_int *px, const mp_int *py, mp_int *rx, michael@0: mp_int *ry, const ECGroup *group) michael@0: { michael@0: return group->point_add(px, py, px, py, rx, ry, group); michael@0: } michael@0: michael@0: /* by default, this routine is unused and thus doesn't need to be compiled */ michael@0: #ifdef ECL_ENABLE_GF2M_PT_MUL_AFF michael@0: /* Computes R = nP based on IEEE P1363 A.10.3. Elliptic curve points P and michael@0: * R can be identical. Uses affine coordinates. */ michael@0: mp_err michael@0: ec_GF2m_pt_mul_aff(const mp_int *n, const mp_int *px, const mp_int *py, michael@0: mp_int *rx, mp_int *ry, const ECGroup *group) michael@0: { michael@0: mp_err res = MP_OKAY; michael@0: mp_int k, k3, qx, qy, sx, sy; michael@0: int b1, b3, i, l; michael@0: michael@0: MP_DIGITS(&k) = 0; michael@0: MP_DIGITS(&k3) = 0; michael@0: MP_DIGITS(&qx) = 0; michael@0: MP_DIGITS(&qy) = 0; michael@0: MP_DIGITS(&sx) = 0; michael@0: MP_DIGITS(&sy) = 0; michael@0: MP_CHECKOK(mp_init(&k)); michael@0: MP_CHECKOK(mp_init(&k3)); michael@0: MP_CHECKOK(mp_init(&qx)); michael@0: MP_CHECKOK(mp_init(&qy)); michael@0: MP_CHECKOK(mp_init(&sx)); michael@0: MP_CHECKOK(mp_init(&sy)); michael@0: michael@0: /* if n = 0 then r = inf */ michael@0: if (mp_cmp_z(n) == 0) { michael@0: mp_zero(rx); michael@0: mp_zero(ry); michael@0: res = MP_OKAY; michael@0: goto CLEANUP; michael@0: } michael@0: /* Q = P, k = n */ michael@0: MP_CHECKOK(mp_copy(px, &qx)); michael@0: MP_CHECKOK(mp_copy(py, &qy)); michael@0: MP_CHECKOK(mp_copy(n, &k)); michael@0: /* if n < 0 then Q = -Q, k = -k */ michael@0: if (mp_cmp_z(n) < 0) { michael@0: MP_CHECKOK(group->meth->field_add(&qx, &qy, &qy, group->meth)); michael@0: MP_CHECKOK(mp_neg(&k, &k)); michael@0: } michael@0: #ifdef ECL_DEBUG /* basic double and add method */ michael@0: l = mpl_significant_bits(&k) - 1; michael@0: MP_CHECKOK(mp_copy(&qx, &sx)); michael@0: MP_CHECKOK(mp_copy(&qy, &sy)); michael@0: for (i = l - 1; i >= 0; i--) { michael@0: /* S = 2S */ michael@0: MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group)); michael@0: /* if k_i = 1, then S = S + Q */ michael@0: if (mpl_get_bit(&k, i) != 0) { michael@0: MP_CHECKOK(group-> michael@0: point_add(&sx, &sy, &qx, &qy, &sx, &sy, group)); michael@0: } michael@0: } michael@0: #else /* double and add/subtract method from michael@0: * standard */ michael@0: /* k3 = 3 * k */ michael@0: MP_CHECKOK(mp_set_int(&k3, 3)); michael@0: MP_CHECKOK(mp_mul(&k, &k3, &k3)); michael@0: /* S = Q */ michael@0: MP_CHECKOK(mp_copy(&qx, &sx)); michael@0: MP_CHECKOK(mp_copy(&qy, &sy)); michael@0: /* l = index of high order bit in binary representation of 3*k */ michael@0: l = mpl_significant_bits(&k3) - 1; michael@0: /* for i = l-1 downto 1 */ michael@0: for (i = l - 1; i >= 1; i--) { michael@0: /* S = 2S */ michael@0: MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group)); michael@0: b3 = MP_GET_BIT(&k3, i); michael@0: b1 = MP_GET_BIT(&k, i); michael@0: /* if k3_i = 1 and k_i = 0, then S = S + Q */ michael@0: if ((b3 == 1) && (b1 == 0)) { michael@0: MP_CHECKOK(group-> michael@0: point_add(&sx, &sy, &qx, &qy, &sx, &sy, group)); michael@0: /* if k3_i = 0 and k_i = 1, then S = S - Q */ michael@0: } else if ((b3 == 0) && (b1 == 1)) { michael@0: MP_CHECKOK(group-> michael@0: point_sub(&sx, &sy, &qx, &qy, &sx, &sy, group)); michael@0: } michael@0: } michael@0: #endif michael@0: /* output S */ michael@0: MP_CHECKOK(mp_copy(&sx, rx)); michael@0: MP_CHECKOK(mp_copy(&sy, ry)); michael@0: michael@0: CLEANUP: michael@0: mp_clear(&k); michael@0: mp_clear(&k3); michael@0: mp_clear(&qx); michael@0: mp_clear(&qy); michael@0: mp_clear(&sx); michael@0: mp_clear(&sy); michael@0: return res; michael@0: } michael@0: #endif michael@0: michael@0: /* Validates a point on a GF2m curve. */ michael@0: mp_err michael@0: ec_GF2m_validate_point(const mp_int *px, const mp_int *py, const ECGroup *group) michael@0: { michael@0: mp_err res = MP_NO; michael@0: mp_int accl, accr, tmp, pxt, pyt; michael@0: michael@0: MP_DIGITS(&accl) = 0; michael@0: MP_DIGITS(&accr) = 0; michael@0: MP_DIGITS(&tmp) = 0; michael@0: MP_DIGITS(&pxt) = 0; michael@0: MP_DIGITS(&pyt) = 0; michael@0: MP_CHECKOK(mp_init(&accl)); michael@0: MP_CHECKOK(mp_init(&accr)); michael@0: MP_CHECKOK(mp_init(&tmp)); michael@0: MP_CHECKOK(mp_init(&pxt)); michael@0: MP_CHECKOK(mp_init(&pyt)); michael@0: michael@0: /* 1: Verify that publicValue is not the point at infinity */ michael@0: if (ec_GF2m_pt_is_inf_aff(px, py) == MP_YES) { michael@0: res = MP_NO; michael@0: goto CLEANUP; michael@0: } michael@0: /* 2: Verify that the coordinates of publicValue are elements michael@0: * of the field. michael@0: */ michael@0: if ((MP_SIGN(px) == MP_NEG) || (mp_cmp(px, &group->meth->irr) >= 0) || michael@0: (MP_SIGN(py) == MP_NEG) || (mp_cmp(py, &group->meth->irr) >= 0)) { michael@0: res = MP_NO; michael@0: goto CLEANUP; michael@0: } michael@0: /* 3: Verify that publicValue is on the curve. */ michael@0: if (group->meth->field_enc) { michael@0: group->meth->field_enc(px, &pxt, group->meth); michael@0: group->meth->field_enc(py, &pyt, group->meth); michael@0: } else { michael@0: mp_copy(px, &pxt); michael@0: mp_copy(py, &pyt); michael@0: } michael@0: /* left-hand side: y^2 + x*y */ michael@0: MP_CHECKOK( group->meth->field_sqr(&pyt, &accl, group->meth) ); michael@0: MP_CHECKOK( group->meth->field_mul(&pxt, &pyt, &tmp, group->meth) ); michael@0: MP_CHECKOK( group->meth->field_add(&accl, &tmp, &accl, group->meth) ); michael@0: /* right-hand side: x^3 + a*x^2 + b */ michael@0: MP_CHECKOK( group->meth->field_sqr(&pxt, &tmp, group->meth) ); michael@0: MP_CHECKOK( group->meth->field_mul(&pxt, &tmp, &accr, group->meth) ); michael@0: MP_CHECKOK( group->meth->field_mul(&group->curvea, &tmp, &tmp, group->meth) ); michael@0: MP_CHECKOK( group->meth->field_add(&tmp, &accr, &accr, group->meth) ); michael@0: MP_CHECKOK( group->meth->field_add(&accr, &group->curveb, &accr, group->meth) ); michael@0: /* check LHS - RHS == 0 */ michael@0: MP_CHECKOK( group->meth->field_add(&accl, &accr, &accr, group->meth) ); michael@0: if (mp_cmp_z(&accr) != 0) { michael@0: res = MP_NO; michael@0: goto CLEANUP; michael@0: } michael@0: /* 4: Verify that the order of the curve times the publicValue michael@0: * is the point at infinity. michael@0: */ michael@0: MP_CHECKOK( ECPoint_mul(group, &group->order, px, py, &pxt, &pyt) ); michael@0: if (ec_GF2m_pt_is_inf_aff(&pxt, &pyt) != MP_YES) { michael@0: res = MP_NO; michael@0: goto CLEANUP; michael@0: } michael@0: michael@0: res = MP_YES; michael@0: michael@0: CLEANUP: michael@0: mp_clear(&accl); michael@0: mp_clear(&accr); michael@0: mp_clear(&tmp); michael@0: mp_clear(&pxt); michael@0: mp_clear(&pyt); michael@0: return res; michael@0: }