andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #include "mpi.h" andre@0: #include "mplogic.h" andre@0: #include "ecl.h" andre@0: #include "ecl-priv.h" andre@0: #include andre@0: andre@0: /* Elliptic curve scalar-point multiplication. Computes R(x, y) = k * P(x, andre@0: * y). If x, y = NULL, then P is assumed to be the generator (base point) andre@0: * of the group of points on the elliptic curve. Input and output values andre@0: * are assumed to be NOT field-encoded. */ andre@0: mp_err andre@0: ECPoint_mul(const ECGroup *group, const mp_int *k, const mp_int *px, andre@0: const mp_int *py, mp_int *rx, mp_int *ry) andre@0: { andre@0: mp_err res = MP_OKAY; andre@0: mp_int kt; andre@0: andre@0: ARGCHK((k != NULL) && (group != NULL), MP_BADARG); andre@0: MP_DIGITS(&kt) = 0; andre@0: andre@0: /* want scalar to be less than or equal to group order */ andre@0: if (mp_cmp(k, &group->order) > 0) { andre@0: MP_CHECKOK(mp_init(&kt)); andre@0: MP_CHECKOK(mp_mod(k, &group->order, &kt)); andre@0: } else { andre@0: MP_SIGN(&kt) = MP_ZPOS; andre@0: MP_USED(&kt) = MP_USED(k); andre@0: MP_ALLOC(&kt) = MP_ALLOC(k); andre@0: MP_DIGITS(&kt) = MP_DIGITS(k); andre@0: } andre@0: andre@0: if ((px == NULL) || (py == NULL)) { andre@0: if (group->base_point_mul) { andre@0: MP_CHECKOK(group->base_point_mul(&kt, rx, ry, group)); andre@0: } else { andre@0: MP_CHECKOK(group-> andre@0: point_mul(&kt, &group->genx, &group->geny, rx, ry, andre@0: group)); andre@0: } andre@0: } else { andre@0: if (group->meth->field_enc) { andre@0: MP_CHECKOK(group->meth->field_enc(px, rx, group->meth)); andre@0: MP_CHECKOK(group->meth->field_enc(py, ry, group->meth)); andre@0: MP_CHECKOK(group->point_mul(&kt, rx, ry, rx, ry, group)); andre@0: } else { andre@0: MP_CHECKOK(group->point_mul(&kt, px, py, rx, ry, group)); andre@0: } andre@0: } andre@0: if (group->meth->field_dec) { andre@0: MP_CHECKOK(group->meth->field_dec(rx, rx, group->meth)); andre@0: MP_CHECKOK(group->meth->field_dec(ry, ry, group->meth)); andre@0: } andre@0: andre@0: CLEANUP: andre@0: if (MP_DIGITS(&kt) != MP_DIGITS(k)) { andre@0: mp_clear(&kt); andre@0: } andre@0: return res; andre@0: } andre@0: andre@0: /* Elliptic curve scalar-point multiplication. Computes R(x, y) = k1 * G + andre@0: * k2 * P(x, y), where G is the generator (base point) of the group of andre@0: * points on the elliptic curve. Allows k1 = NULL or { k2, P } = NULL. andre@0: * Input and output values are assumed to be NOT field-encoded. */ andre@0: mp_err andre@0: ec_pts_mul_basic(const mp_int *k1, const mp_int *k2, const mp_int *px, andre@0: const mp_int *py, mp_int *rx, mp_int *ry, andre@0: const ECGroup *group) andre@0: { andre@0: mp_err res = MP_OKAY; andre@0: mp_int sx, sy; andre@0: andre@0: ARGCHK(group != NULL, MP_BADARG); andre@0: ARGCHK(!((k1 == NULL) andre@0: && ((k2 == NULL) || (px == NULL) andre@0: || (py == NULL))), MP_BADARG); andre@0: andre@0: /* if some arguments are not defined used ECPoint_mul */ andre@0: if (k1 == NULL) { andre@0: return ECPoint_mul(group, k2, px, py, rx, ry); andre@0: } else if ((k2 == NULL) || (px == NULL) || (py == NULL)) { andre@0: return ECPoint_mul(group, k1, NULL, NULL, rx, ry); andre@0: } andre@0: andre@0: MP_DIGITS(&sx) = 0; andre@0: MP_DIGITS(&sy) = 0; andre@0: MP_CHECKOK(mp_init(&sx)); andre@0: MP_CHECKOK(mp_init(&sy)); andre@0: andre@0: MP_CHECKOK(ECPoint_mul(group, k1, NULL, NULL, &sx, &sy)); andre@0: MP_CHECKOK(ECPoint_mul(group, k2, px, py, rx, ry)); andre@0: andre@0: if (group->meth->field_enc) { andre@0: MP_CHECKOK(group->meth->field_enc(&sx, &sx, group->meth)); andre@0: MP_CHECKOK(group->meth->field_enc(&sy, &sy, group->meth)); andre@0: MP_CHECKOK(group->meth->field_enc(rx, rx, group->meth)); andre@0: MP_CHECKOK(group->meth->field_enc(ry, ry, group->meth)); andre@0: } andre@0: andre@0: MP_CHECKOK(group->point_add(&sx, &sy, rx, ry, rx, ry, group)); andre@0: andre@0: if (group->meth->field_dec) { andre@0: MP_CHECKOK(group->meth->field_dec(rx, rx, group->meth)); andre@0: MP_CHECKOK(group->meth->field_dec(ry, ry, group->meth)); andre@0: } andre@0: andre@0: CLEANUP: andre@0: mp_clear(&sx); andre@0: mp_clear(&sy); andre@0: return res; andre@0: } andre@0: andre@0: /* Elliptic curve scalar-point multiplication. Computes R(x, y) = k1 * G + andre@0: * k2 * P(x, y), where G is the generator (base point) of the group of andre@0: * points on the elliptic curve. Allows k1 = NULL or { k2, P } = NULL. andre@0: * Input and output values are assumed to be NOT field-encoded. Uses andre@0: * algorithm 15 (simultaneous multiple point multiplication) from Brown, andre@0: * Hankerson, Lopez, Menezes. Software Implementation of the NIST andre@0: * Elliptic Curves over Prime Fields. */ andre@0: mp_err andre@0: ec_pts_mul_simul_w2(const mp_int *k1, const mp_int *k2, const mp_int *px, andre@0: const mp_int *py, mp_int *rx, mp_int *ry, andre@0: const ECGroup *group) andre@0: { andre@0: mp_err res = MP_OKAY; andre@0: mp_int precomp[4][4][2]; andre@0: const mp_int *a, *b; andre@0: int i, j; andre@0: int ai, bi, d; andre@0: andre@0: ARGCHK(group != NULL, MP_BADARG); andre@0: ARGCHK(!((k1 == NULL) andre@0: && ((k2 == NULL) || (px == NULL) andre@0: || (py == NULL))), MP_BADARG); andre@0: andre@0: /* if some arguments are not defined used ECPoint_mul */ andre@0: if (k1 == NULL) { andre@0: return ECPoint_mul(group, k2, px, py, rx, ry); andre@0: } else if ((k2 == NULL) || (px == NULL) || (py == NULL)) { andre@0: return ECPoint_mul(group, k1, NULL, NULL, rx, ry); andre@0: } andre@0: andre@0: /* initialize precomputation table */ andre@0: for (i = 0; i < 4; i++) { andre@0: for (j = 0; j < 4; j++) { andre@0: MP_DIGITS(&precomp[i][j][0]) = 0; andre@0: MP_DIGITS(&precomp[i][j][1]) = 0; andre@0: } andre@0: } andre@0: for (i = 0; i < 4; i++) { andre@0: for (j = 0; j < 4; j++) { andre@0: MP_CHECKOK( mp_init_size(&precomp[i][j][0], andre@0: ECL_MAX_FIELD_SIZE_DIGITS) ); andre@0: MP_CHECKOK( mp_init_size(&precomp[i][j][1], andre@0: ECL_MAX_FIELD_SIZE_DIGITS) ); andre@0: } andre@0: } andre@0: andre@0: /* fill precomputation table */ andre@0: /* assign {k1, k2} = {a, b} such that len(a) >= len(b) */ andre@0: if (mpl_significant_bits(k1) < mpl_significant_bits(k2)) { andre@0: a = k2; andre@0: b = k1; andre@0: if (group->meth->field_enc) { andre@0: MP_CHECKOK(group->meth-> andre@0: field_enc(px, &precomp[1][0][0], group->meth)); andre@0: MP_CHECKOK(group->meth-> andre@0: field_enc(py, &precomp[1][0][1], group->meth)); andre@0: } else { andre@0: MP_CHECKOK(mp_copy(px, &precomp[1][0][0])); andre@0: MP_CHECKOK(mp_copy(py, &precomp[1][0][1])); andre@0: } andre@0: MP_CHECKOK(mp_copy(&group->genx, &precomp[0][1][0])); andre@0: MP_CHECKOK(mp_copy(&group->geny, &precomp[0][1][1])); andre@0: } else { andre@0: a = k1; andre@0: b = k2; andre@0: MP_CHECKOK(mp_copy(&group->genx, &precomp[1][0][0])); andre@0: MP_CHECKOK(mp_copy(&group->geny, &precomp[1][0][1])); andre@0: if (group->meth->field_enc) { andre@0: MP_CHECKOK(group->meth-> andre@0: field_enc(px, &precomp[0][1][0], group->meth)); andre@0: MP_CHECKOK(group->meth-> andre@0: field_enc(py, &precomp[0][1][1], group->meth)); andre@0: } else { andre@0: MP_CHECKOK(mp_copy(px, &precomp[0][1][0])); andre@0: MP_CHECKOK(mp_copy(py, &precomp[0][1][1])); andre@0: } andre@0: } andre@0: /* precompute [*][0][*] */ andre@0: mp_zero(&precomp[0][0][0]); andre@0: mp_zero(&precomp[0][0][1]); andre@0: MP_CHECKOK(group-> andre@0: point_dbl(&precomp[1][0][0], &precomp[1][0][1], andre@0: &precomp[2][0][0], &precomp[2][0][1], group)); andre@0: MP_CHECKOK(group-> andre@0: point_add(&precomp[1][0][0], &precomp[1][0][1], andre@0: &precomp[2][0][0], &precomp[2][0][1], andre@0: &precomp[3][0][0], &precomp[3][0][1], group)); andre@0: /* precompute [*][1][*] */ andre@0: for (i = 1; i < 4; i++) { andre@0: MP_CHECKOK(group-> andre@0: point_add(&precomp[0][1][0], &precomp[0][1][1], andre@0: &precomp[i][0][0], &precomp[i][0][1], andre@0: &precomp[i][1][0], &precomp[i][1][1], group)); andre@0: } andre@0: /* precompute [*][2][*] */ andre@0: MP_CHECKOK(group-> andre@0: point_dbl(&precomp[0][1][0], &precomp[0][1][1], andre@0: &precomp[0][2][0], &precomp[0][2][1], group)); andre@0: for (i = 1; i < 4; i++) { andre@0: MP_CHECKOK(group-> andre@0: point_add(&precomp[0][2][0], &precomp[0][2][1], andre@0: &precomp[i][0][0], &precomp[i][0][1], andre@0: &precomp[i][2][0], &precomp[i][2][1], group)); andre@0: } andre@0: /* precompute [*][3][*] */ andre@0: MP_CHECKOK(group-> andre@0: point_add(&precomp[0][1][0], &precomp[0][1][1], andre@0: &precomp[0][2][0], &precomp[0][2][1], andre@0: &precomp[0][3][0], &precomp[0][3][1], group)); andre@0: for (i = 1; i < 4; i++) { andre@0: MP_CHECKOK(group-> andre@0: point_add(&precomp[0][3][0], &precomp[0][3][1], andre@0: &precomp[i][0][0], &precomp[i][0][1], andre@0: &precomp[i][3][0], &precomp[i][3][1], group)); andre@0: } andre@0: andre@0: d = (mpl_significant_bits(a) + 1) / 2; andre@0: andre@0: /* R = inf */ andre@0: mp_zero(rx); andre@0: mp_zero(ry); andre@0: andre@0: for (i = d - 1; i >= 0; i--) { andre@0: ai = MP_GET_BIT(a, 2 * i + 1); andre@0: ai <<= 1; andre@0: ai |= MP_GET_BIT(a, 2 * i); andre@0: bi = MP_GET_BIT(b, 2 * i + 1); andre@0: bi <<= 1; andre@0: bi |= MP_GET_BIT(b, 2 * i); andre@0: /* R = 2^2 * R */ andre@0: MP_CHECKOK(group->point_dbl(rx, ry, rx, ry, group)); andre@0: MP_CHECKOK(group->point_dbl(rx, ry, rx, ry, group)); andre@0: /* R = R + (ai * A + bi * B) */ andre@0: MP_CHECKOK(group-> andre@0: point_add(rx, ry, &precomp[ai][bi][0], andre@0: &precomp[ai][bi][1], rx, ry, group)); andre@0: } andre@0: andre@0: if (group->meth->field_dec) { andre@0: MP_CHECKOK(group->meth->field_dec(rx, rx, group->meth)); andre@0: MP_CHECKOK(group->meth->field_dec(ry, ry, group->meth)); andre@0: } andre@0: andre@0: CLEANUP: andre@0: for (i = 0; i < 4; i++) { andre@0: for (j = 0; j < 4; j++) { andre@0: mp_clear(&precomp[i][j][0]); andre@0: mp_clear(&precomp[i][j][1]); andre@0: } andre@0: } andre@0: return res; andre@0: } andre@0: andre@0: /* Elliptic curve scalar-point multiplication. Computes R(x, y) = k1 * G + andre@0: * k2 * P(x, y), where G is the generator (base point) of the group of andre@0: * points on the elliptic curve. Allows k1 = NULL or { k2, P } = NULL. andre@0: * Input and output values are assumed to be NOT field-encoded. */ andre@0: mp_err andre@0: ECPoints_mul(const ECGroup *group, const mp_int *k1, const mp_int *k2, andre@0: const mp_int *px, const mp_int *py, mp_int *rx, mp_int *ry) andre@0: { andre@0: mp_err res = MP_OKAY; andre@0: mp_int k1t, k2t; andre@0: const mp_int *k1p, *k2p; andre@0: andre@0: MP_DIGITS(&k1t) = 0; andre@0: MP_DIGITS(&k2t) = 0; andre@0: andre@0: ARGCHK(group != NULL, MP_BADARG); andre@0: andre@0: /* want scalar to be less than or equal to group order */ andre@0: if (k1 != NULL) { andre@0: if (mp_cmp(k1, &group->order) >= 0) { andre@0: MP_CHECKOK(mp_init(&k1t)); andre@0: MP_CHECKOK(mp_mod(k1, &group->order, &k1t)); andre@0: k1p = &k1t; andre@0: } else { andre@0: k1p = k1; andre@0: } andre@0: } else { andre@0: k1p = k1; andre@0: } andre@0: if (k2 != NULL) { andre@0: if (mp_cmp(k2, &group->order) >= 0) { andre@0: MP_CHECKOK(mp_init(&k2t)); andre@0: MP_CHECKOK(mp_mod(k2, &group->order, &k2t)); andre@0: k2p = &k2t; andre@0: } else { andre@0: k2p = k2; andre@0: } andre@0: } else { andre@0: k2p = k2; andre@0: } andre@0: andre@0: /* if points_mul is defined, then use it */ andre@0: if (group->points_mul) { andre@0: res = group->points_mul(k1p, k2p, px, py, rx, ry, group); andre@0: } else { andre@0: res = ec_pts_mul_simul_w2(k1p, k2p, px, py, rx, ry, group); andre@0: } andre@0: andre@0: CLEANUP: andre@0: mp_clear(&k1t); andre@0: mp_clear(&k2t); andre@0: return res; andre@0: }