1N/A/*
1N/A * ***** BEGIN LICENSE BLOCK *****
1N/A * Version: MPL 1.1/GPL 2.0/LGPL 2.1
1N/A *
1N/A * The contents of this file are subject to the Mozilla Public License Version
1N/A * 1.1 (the "License"); you may not use this file except in compliance with
1N/A * the License. You may obtain a copy of the License at
1N/A * http://www.mozilla.org/MPL/
1N/A *
1N/A * Software distributed under the License is distributed on an "AS IS" basis,
1N/A * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
1N/A * for the specific language governing rights and limitations under the
1N/A * License.
1N/A *
1N/A * The Original Code is the elliptic curve math library for binary polynomial field curves.
1N/A *
1N/A * The Initial Developer of the Original Code is
1N/A * Sun Microsystems, Inc.
1N/A * Portions created by the Initial Developer are Copyright (C) 2003
1N/A * the Initial Developer. All Rights Reserved.
1N/A *
1N/A * Contributor(s):
1N/A * Sheueling Chang-Shantz <sheueling.chang@sun.com>,
1N/A * Stephen Fung <fungstep@hotmail.com>, and
1N/A * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories.
1N/A *
1N/A * Alternatively, the contents of this file may be used under the terms of
1N/A * either the GNU General Public License Version 2 or later (the "GPL"), or
1N/A * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
1N/A * in which case the provisions of the GPL or the LGPL are applicable instead
1N/A * of those above. If you wish to allow use of your version of this file only
1N/A * under the terms of either the GPL or the LGPL, and not to allow others to
1N/A * use your version of this file under the terms of the MPL, indicate your
1N/A * decision by deleting the provisions above and replace them with the notice
1N/A * and other provisions required by the GPL or the LGPL. If you do not delete
1N/A * the provisions above, a recipient may use your version of this file under
1N/A * the terms of any one of the MPL, the GPL or the LGPL.
1N/A *
1N/A * ***** END LICENSE BLOCK ***** */
1N/A/*
1N/A * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
1N/A * Use is subject to license terms.
1N/A *
1N/A * Sun elects to use this software under the MPL license.
1N/A */
1N/A
1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A#include "ec2.h"
1N/A#include "mplogic.h"
1N/A#include "mp_gf2m.h"
1N/A#ifndef _KERNEL
1N/A#include <stdlib.h>
1N/A#endif
1N/A
1N/A/* Compute the x-coordinate x/z for the point 2*(x/z) in Montgomery
1N/A * projective coordinates. Uses algorithm Mdouble in appendix of Lopez, J.
1N/A * and Dahab, R. "Fast multiplication on elliptic curves over GF(2^m)
1N/A * without precomputation". modified to not require precomputation of
1N/A * c=b^{2^{m-1}}. */
1N/Astatic mp_err
1N/Agf2m_Mdouble(mp_int *x, mp_int *z, const ECGroup *group, int kmflag)
1N/A{
1N/A mp_err res = MP_OKAY;
1N/A mp_int t1;
1N/A
1N/A MP_DIGITS(&t1) = 0;
1N/A MP_CHECKOK(mp_init(&t1, kmflag));
1N/A
1N/A MP_CHECKOK(group->meth->field_sqr(x, x, group->meth));
1N/A MP_CHECKOK(group->meth->field_sqr(z, &t1, group->meth));
1N/A MP_CHECKOK(group->meth->field_mul(x, &t1, z, group->meth));
1N/A MP_CHECKOK(group->meth->field_sqr(x, x, group->meth));
1N/A MP_CHECKOK(group->meth->field_sqr(&t1, &t1, group->meth));
1N/A MP_CHECKOK(group->meth->
1N/A field_mul(&group->curveb, &t1, &t1, group->meth));
1N/A MP_CHECKOK(group->meth->field_add(x, &t1, x, group->meth));
1N/A
1N/A CLEANUP:
1N/A mp_clear(&t1);
1N/A return res;
1N/A}
1N/A
1N/A/* Compute the x-coordinate x1/z1 for the point (x1/z1)+(x2/x2) in
1N/A * Montgomery projective coordinates. Uses algorithm Madd in appendix of
1N/A * Lopex, J. and Dahab, R. "Fast multiplication on elliptic curves over
1N/A * GF(2^m) without precomputation". */
1N/Astatic mp_err
1N/Agf2m_Madd(const mp_int *x, mp_int *x1, mp_int *z1, mp_int *x2, mp_int *z2,
1N/A const ECGroup *group, int kmflag)
1N/A{
1N/A mp_err res = MP_OKAY;
1N/A mp_int t1, t2;
1N/A
1N/A MP_DIGITS(&t1) = 0;
1N/A MP_DIGITS(&t2) = 0;
1N/A MP_CHECKOK(mp_init(&t1, kmflag));
1N/A MP_CHECKOK(mp_init(&t2, kmflag));
1N/A
1N/A MP_CHECKOK(mp_copy(x, &t1));
1N/A MP_CHECKOK(group->meth->field_mul(x1, z2, x1, group->meth));
1N/A MP_CHECKOK(group->meth->field_mul(z1, x2, z1, group->meth));
1N/A MP_CHECKOK(group->meth->field_mul(x1, z1, &t2, group->meth));
1N/A MP_CHECKOK(group->meth->field_add(z1, x1, z1, group->meth));
1N/A MP_CHECKOK(group->meth->field_sqr(z1, z1, group->meth));
1N/A MP_CHECKOK(group->meth->field_mul(z1, &t1, x1, group->meth));
1N/A MP_CHECKOK(group->meth->field_add(x1, &t2, x1, group->meth));
1N/A
1N/A CLEANUP:
1N/A mp_clear(&t1);
1N/A mp_clear(&t2);
1N/A return res;
1N/A}
1N/A
1N/A/* Compute the x, y affine coordinates from the point (x1, z1) (x2, z2)
1N/A * using Montgomery point multiplication algorithm Mxy() in appendix of
1N/A * Lopex, J. and Dahab, R. "Fast multiplication on elliptic curves over
1N/A * GF(2^m) without precomputation". Returns: 0 on error 1 if return value
1N/A * should be the point at infinity 2 otherwise */
1N/Astatic int
1N/Agf2m_Mxy(const mp_int *x, const mp_int *y, mp_int *x1, mp_int *z1,
1N/A mp_int *x2, mp_int *z2, const ECGroup *group)
1N/A{
1N/A mp_err res = MP_OKAY;
1N/A int ret = 0;
1N/A mp_int t3, t4, t5;
1N/A
1N/A MP_DIGITS(&t3) = 0;
1N/A MP_DIGITS(&t4) = 0;
1N/A MP_DIGITS(&t5) = 0;
1N/A MP_CHECKOK(mp_init(&t3, FLAG(x2)));
1N/A MP_CHECKOK(mp_init(&t4, FLAG(x2)));
1N/A MP_CHECKOK(mp_init(&t5, FLAG(x2)));
1N/A
1N/A if (mp_cmp_z(z1) == 0) {
1N/A mp_zero(x2);
1N/A mp_zero(z2);
1N/A ret = 1;
1N/A goto CLEANUP;
1N/A }
1N/A
1N/A if (mp_cmp_z(z2) == 0) {
1N/A MP_CHECKOK(mp_copy(x, x2));
1N/A MP_CHECKOK(group->meth->field_add(x, y, z2, group->meth));
1N/A ret = 2;
1N/A goto CLEANUP;
1N/A }
1N/A
1N/A MP_CHECKOK(mp_set_int(&t5, 1));
1N/A if (group->meth->field_enc) {
1N/A MP_CHECKOK(group->meth->field_enc(&t5, &t5, group->meth));
1N/A }
1N/A
1N/A MP_CHECKOK(group->meth->field_mul(z1, z2, &t3, group->meth));
1N/A
1N/A MP_CHECKOK(group->meth->field_mul(z1, x, z1, group->meth));
1N/A MP_CHECKOK(group->meth->field_add(z1, x1, z1, group->meth));
1N/A MP_CHECKOK(group->meth->field_mul(z2, x, z2, group->meth));
1N/A MP_CHECKOK(group->meth->field_mul(z2, x1, x1, group->meth));
1N/A MP_CHECKOK(group->meth->field_add(z2, x2, z2, group->meth));
1N/A
1N/A MP_CHECKOK(group->meth->field_mul(z2, z1, z2, group->meth));
1N/A MP_CHECKOK(group->meth->field_sqr(x, &t4, group->meth));
1N/A MP_CHECKOK(group->meth->field_add(&t4, y, &t4, group->meth));
1N/A MP_CHECKOK(group->meth->field_mul(&t4, &t3, &t4, group->meth));
1N/A MP_CHECKOK(group->meth->field_add(&t4, z2, &t4, group->meth));
1N/A
1N/A MP_CHECKOK(group->meth->field_mul(&t3, x, &t3, group->meth));
1N/A MP_CHECKOK(group->meth->field_div(&t5, &t3, &t3, group->meth));
1N/A MP_CHECKOK(group->meth->field_mul(&t3, &t4, &t4, group->meth));
1N/A MP_CHECKOK(group->meth->field_mul(x1, &t3, x2, group->meth));
1N/A MP_CHECKOK(group->meth->field_add(x2, x, z2, group->meth));
1N/A
1N/A MP_CHECKOK(group->meth->field_mul(z2, &t4, z2, group->meth));
1N/A MP_CHECKOK(group->meth->field_add(z2, y, z2, group->meth));
1N/A
1N/A ret = 2;
1N/A
1N/A CLEANUP:
1N/A mp_clear(&t3);
1N/A mp_clear(&t4);
1N/A mp_clear(&t5);
1N/A if (res == MP_OKAY) {
1N/A return ret;
1N/A } else {
1N/A return 0;
1N/A }
1N/A}
1N/A
1N/A/* Computes R = nP based on algorithm 2P of Lopex, J. and Dahab, R. "Fast
1N/A * multiplication on elliptic curves over GF(2^m) without
1N/A * precomputation". Elliptic curve points P and R can be identical. Uses
1N/A * Montgomery projective coordinates. */
1N/Amp_err
1N/Aec_GF2m_pt_mul_mont(const mp_int *n, const mp_int *px, const mp_int *py,
1N/A mp_int *rx, mp_int *ry, const ECGroup *group)
1N/A{
1N/A mp_err res = MP_OKAY;
1N/A mp_int x1, x2, z1, z2;
1N/A int i, j;
1N/A mp_digit top_bit, mask;
1N/A
1N/A MP_DIGITS(&x1) = 0;
1N/A MP_DIGITS(&x2) = 0;
1N/A MP_DIGITS(&z1) = 0;
1N/A MP_DIGITS(&z2) = 0;
1N/A MP_CHECKOK(mp_init(&x1, FLAG(n)));
1N/A MP_CHECKOK(mp_init(&x2, FLAG(n)));
1N/A MP_CHECKOK(mp_init(&z1, FLAG(n)));
1N/A MP_CHECKOK(mp_init(&z2, FLAG(n)));
1N/A
1N/A /* if result should be point at infinity */
1N/A if ((mp_cmp_z(n) == 0) || (ec_GF2m_pt_is_inf_aff(px, py) == MP_YES)) {
1N/A MP_CHECKOK(ec_GF2m_pt_set_inf_aff(rx, ry));
1N/A goto CLEANUP;
1N/A }
1N/A
1N/A MP_CHECKOK(mp_copy(px, &x1)); /* x1 = px */
1N/A MP_CHECKOK(mp_set_int(&z1, 1)); /* z1 = 1 */
1N/A MP_CHECKOK(group->meth->field_sqr(&x1, &z2, group->meth)); /* z2 =
1N/A * x1^2 =
1N/A * px^2 */
1N/A MP_CHECKOK(group->meth->field_sqr(&z2, &x2, group->meth));
1N/A MP_CHECKOK(group->meth->field_add(&x2, &group->curveb, &x2, group->meth)); /* x2
1N/A * =
1N/A * px^4
1N/A * +
1N/A * b
1N/A */
1N/A
1N/A /* find top-most bit and go one past it */
1N/A i = MP_USED(n) - 1;
1N/A j = MP_DIGIT_BIT - 1;
1N/A top_bit = 1;
1N/A top_bit <<= MP_DIGIT_BIT - 1;
1N/A mask = top_bit;
1N/A while (!(MP_DIGITS(n)[i] & mask)) {
1N/A mask >>= 1;
1N/A j--;
1N/A }
1N/A mask >>= 1;
1N/A j--;
1N/A
1N/A /* if top most bit was at word break, go to next word */
1N/A if (!mask) {
1N/A i--;
1N/A j = MP_DIGIT_BIT - 1;
1N/A mask = top_bit;
1N/A }
1N/A
1N/A for (; i >= 0; i--) {
1N/A for (; j >= 0; j--) {
1N/A if (MP_DIGITS(n)[i] & mask) {
1N/A MP_CHECKOK(gf2m_Madd(px, &x1, &z1, &x2, &z2, group, FLAG(n)));
1N/A MP_CHECKOK(gf2m_Mdouble(&x2, &z2, group, FLAG(n)));
1N/A } else {
1N/A MP_CHECKOK(gf2m_Madd(px, &x2, &z2, &x1, &z1, group, FLAG(n)));
1N/A MP_CHECKOK(gf2m_Mdouble(&x1, &z1, group, FLAG(n)));
1N/A }
1N/A mask >>= 1;
1N/A }
1N/A j = MP_DIGIT_BIT - 1;
1N/A mask = top_bit;
1N/A }
1N/A
1N/A /* convert out of "projective" coordinates */
1N/A i = gf2m_Mxy(px, py, &x1, &z1, &x2, &z2, group);
1N/A if (i == 0) {
1N/A res = MP_BADARG;
1N/A goto CLEANUP;
1N/A } else if (i == 1) {
1N/A MP_CHECKOK(ec_GF2m_pt_set_inf_aff(rx, ry));
1N/A } else {
1N/A MP_CHECKOK(mp_copy(&x2, rx));
1N/A MP_CHECKOK(mp_copy(&z2, ry));
1N/A }
1N/A
1N/A CLEANUP:
1N/A mp_clear(&x1);
1N/A mp_clear(&x2);
1N/A mp_clear(&z1);
1N/A mp_clear(&z2);
1N/A return res;
1N/A}