ecl.c revision f9fbec18f5b458b560ecf45d3db8e8bd56bf6942
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi/*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * ***** BEGIN LICENSE BLOCK *****
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * The contents of this file are subject to the Mozilla Public License Version
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * 1.1 (the "License"); you may not use this file except in compliance with
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * the License. You may obtain a copy of the License at
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * http://www.mozilla.org/MPL/
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Software distributed under the License is distributed on an "AS IS" basis,
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * for the specific language governing rights and limitations under the
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * License.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * The Original Code is the elliptic curve math library.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * The Initial Developer of the Original Code is
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Sun Microsystems, Inc.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Portions created by the Initial Developer are Copyright (C) 2003
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * the Initial Developer. All Rights Reserved.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Contributor(s):
e5dcf7beb7c949f9234713d5818b581ec3825443Robert Johnston * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Alternatively, the contents of this file may be used under the terms of
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * either the GNU General Public License Version 2 or later (the "GPL"), or
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * in which case the provisions of the GPL or the LGPL are applicable instead
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * of those above. If you wish to allow use of your version of this file only
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * under the terms of either the GPL or the LGPL, and not to allow others to
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * use your version of this file under the terms of the MPL, indicate your
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * decision by deleting the provisions above and replace them with the notice
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * and other provisions required by the GPL or the LGPL. If you do not delete
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi * the provisions above, a recipient may use your version of this file under
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * the terms of any one of the MPL, the GPL or the LGPL.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * ***** END LICENSE BLOCK ***** */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi/*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi * Use is subject to license terms.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Sun elects to use this software under the MPL license.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#pragma ident "%Z%%M% %I% %E% SMI"
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include "mpi.h"
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include "mplogic.h"
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include "ecl.h"
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include "ecl-priv.h"
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include "ec2.h"
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include "ecp.h"
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#ifndef _KERNEL
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <stdlib.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <string.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#endif
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi/* Allocate memory for a new ECGroup object. */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindiECGroup *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindiECGroup_new(int kmflag)
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi mp_err res = MP_OKAY;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi ECGroup *group;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#ifdef _KERNEL
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi group = (ECGroup *) kmem_alloc(sizeof(ECGroup), kmflag);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#else
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi group = (ECGroup *) malloc(sizeof(ECGroup));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#endif
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (group == NULL)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return NULL;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi group->constructed = MP_YES;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi group->meth = NULL;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi group->text = NULL;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi MP_DIGITS(&group->curvea) = 0;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi MP_DIGITS(&group->curveb) = 0;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi MP_DIGITS(&group->genx) = 0;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi MP_DIGITS(&group->geny) = 0;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi MP_DIGITS(&group->order) = 0;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi group->base_point_mul = NULL;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi group->points_mul = NULL;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi group->validate_point = NULL;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi group->extra1 = NULL;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi group->extra2 = NULL;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi group->extra_free = NULL;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi MP_CHECKOK(mp_init(&group->curvea, kmflag));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi MP_CHECKOK(mp_init(&group->curveb, kmflag));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi MP_CHECKOK(mp_init(&group->genx, kmflag));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi MP_CHECKOK(mp_init(&group->geny, kmflag));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi MP_CHECKOK(mp_init(&group->order, kmflag));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi CLEANUP:
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (res != MP_OKAY) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi ECGroup_free(group);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return NULL;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return group;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi/* Construct a generic ECGroup for elliptic curves over prime fields. */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindiECGroup *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindiECGroup_consGFp(const mp_int *irr, const mp_int *curvea,
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi const mp_int *curveb, const mp_int *genx,
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi const mp_int *geny, const mp_int *order, int cofactor)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi mp_err res = MP_OKAY;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi ECGroup *group = NULL;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi group = ECGroup_new(FLAG(irr));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (group == NULL)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return NULL;
4557a2a1868181b517f5dfe61ba6eeba58edf4c0robj
4557a2a1868181b517f5dfe61ba6eeba58edf4c0robj group->meth = GFMethod_consGFp(irr);
4557a2a1868181b517f5dfe61ba6eeba58edf4c0robj if (group->meth == NULL) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi res = MP_MEM;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi goto CLEANUP;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi MP_CHECKOK(mp_copy(curvea, &group->curvea));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi MP_CHECKOK(mp_copy(curveb, &group->curveb));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi MP_CHECKOK(mp_copy(genx, &group->genx));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi MP_CHECKOK(mp_copy(geny, &group->geny));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi MP_CHECKOK(mp_copy(order, &group->order));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi group->cofactor = cofactor;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi group->point_add = &ec_GFp_pt_add_aff;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi group->point_sub = &ec_GFp_pt_sub_aff;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi group->point_dbl = &ec_GFp_pt_dbl_aff;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi group->point_mul = &ec_GFp_pt_mul_jm_wNAF;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi group->base_point_mul = NULL;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi group->points_mul = &ec_GFp_pts_mul_jac;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi group->validate_point = &ec_GFp_validate_point;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi CLEANUP:
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if (res != MP_OKAY) {
4557a2a1868181b517f5dfe61ba6eeba58edf4c0robj ECGroup_free(group);
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi return NULL;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi }
825ba0f20a74fd9c5d0d1ce2c195da2cc88a7f77robj return group;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi}
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi/* Construct a generic ECGroup for elliptic curves over prime fields with
825ba0f20a74fd9c5d0d1ce2c195da2cc88a7f77robj * field arithmetic implemented in Montgomery coordinates. */
0eb822a1c0c2bea495647510b75f77f0e57633ebcindiECGroup *
0eb822a1c0c2bea495647510b75f77f0e57633ebcindiECGroup_consGFp_mont(const mp_int *irr, const mp_int *curvea,
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi const mp_int *curveb, const mp_int *genx,
825ba0f20a74fd9c5d0d1ce2c195da2cc88a7f77robj const mp_int *geny, const mp_int *order, int cofactor)
825ba0f20a74fd9c5d0d1ce2c195da2cc88a7f77robj{
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi mp_err res = MP_OKAY;
e5dcf7beb7c949f9234713d5818b581ec3825443Robert Johnston ECGroup *group = NULL;
4557a2a1868181b517f5dfe61ba6eeba58edf4c0robj
4557a2a1868181b517f5dfe61ba6eeba58edf4c0robj group = ECGroup_new(FLAG(irr));
825ba0f20a74fd9c5d0d1ce2c195da2cc88a7f77robj if (group == NULL)
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi return NULL;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
4557a2a1868181b517f5dfe61ba6eeba58edf4c0robj group->meth = GFMethod_consGFp_mont(irr);
2eeaed14a5e2ed9bd811643ad5bffc3510ca0310robj if (group->meth == NULL) {
825ba0f20a74fd9c5d0d1ce2c195da2cc88a7f77robj res = MP_MEM;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi goto CLEANUP;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi }
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi MP_CHECKOK(group->meth->
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi field_enc(curvea, &group->curvea, group->meth));
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi MP_CHECKOK(group->meth->
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi field_enc(curveb, &group->curveb, group->meth));
eae2e508a8e70b1ec407b10bd068c080651bbe5ckrishnae MP_CHECKOK(group->meth->field_enc(genx, &group->genx, group->meth));
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi MP_CHECKOK(group->meth->field_enc(geny, &group->geny, group->meth));
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi MP_CHECKOK(mp_copy(order, &group->order));
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi group->cofactor = cofactor;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi group->point_add = &ec_GFp_pt_add_aff;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi group->point_sub = &ec_GFp_pt_sub_aff;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi group->point_dbl = &ec_GFp_pt_dbl_aff;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi group->point_mul = &ec_GFp_pt_mul_jm_wNAF;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi group->base_point_mul = NULL;
4557a2a1868181b517f5dfe61ba6eeba58edf4c0robj group->points_mul = &ec_GFp_pts_mul_jac;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi group->validate_point = &ec_GFp_validate_point;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
4557a2a1868181b517f5dfe61ba6eeba58edf4c0robj CLEANUP:
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if (res != MP_OKAY) {
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi ECGroup_free(group);
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi return NULL;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi }
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi return group;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi}
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi#ifdef NSS_ECC_MORE_THAN_SUITE_B
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi/* Construct a generic ECGroup for elliptic curves over binary polynomial
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi * fields. */
0eb822a1c0c2bea495647510b75f77f0e57633ebcindiECGroup *
0eb822a1c0c2bea495647510b75f77f0e57633ebcindiECGroup_consGF2m(const mp_int *irr, const unsigned int irr_arr[5],
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi const mp_int *curvea, const mp_int *curveb,
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi const mp_int *genx, const mp_int *geny,
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi const mp_int *order, int cofactor)
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi{
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi mp_err res = MP_OKAY;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi ECGroup *group = NULL;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi group = ECGroup_new(FLAG(irr));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (group == NULL)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return NULL;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi group->meth = GFMethod_consGF2m(irr, irr_arr);
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if (group->meth == NULL) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi res = MP_MEM;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi goto CLEANUP;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi MP_CHECKOK(mp_copy(curvea, &group->curvea));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi MP_CHECKOK(mp_copy(curveb, &group->curveb));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi MP_CHECKOK(mp_copy(genx, &group->genx));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi MP_CHECKOK(mp_copy(geny, &group->geny));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi MP_CHECKOK(mp_copy(order, &group->order));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi group->cofactor = cofactor;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi group->point_add = &ec_GF2m_pt_add_aff;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi group->point_sub = &ec_GF2m_pt_sub_aff;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi group->point_dbl = &ec_GF2m_pt_dbl_aff;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi group->point_mul = &ec_GF2m_pt_mul_mont;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi group->base_point_mul = NULL;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi group->points_mul = &ec_pts_mul_basic;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi group->validate_point = &ec_GF2m_validate_point;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi CLEANUP:
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (res != MP_OKAY) {
ECGroup_free(group);
return NULL;
}
return group;
}
#endif
/* Construct ECGroup from hex parameters and name, if any. Called by
* ECGroup_fromHex and ECGroup_fromName. */
ECGroup *
ecgroup_fromNameAndHex(const ECCurveName name,
const ECCurveParams * params, int kmflag)
{
mp_int irr, curvea, curveb, genx, geny, order;
int bits;
ECGroup *group = NULL;
mp_err res = MP_OKAY;
/* initialize values */
MP_DIGITS(&irr) = 0;
MP_DIGITS(&curvea) = 0;
MP_DIGITS(&curveb) = 0;
MP_DIGITS(&genx) = 0;
MP_DIGITS(&geny) = 0;
MP_DIGITS(&order) = 0;
MP_CHECKOK(mp_init(&irr, kmflag));
MP_CHECKOK(mp_init(&curvea, kmflag));
MP_CHECKOK(mp_init(&curveb, kmflag));
MP_CHECKOK(mp_init(&genx, kmflag));
MP_CHECKOK(mp_init(&geny, kmflag));
MP_CHECKOK(mp_init(&order, kmflag));
MP_CHECKOK(mp_read_radix(&irr, params->irr, 16));
MP_CHECKOK(mp_read_radix(&curvea, params->curvea, 16));
MP_CHECKOK(mp_read_radix(&curveb, params->curveb, 16));
MP_CHECKOK(mp_read_radix(&genx, params->genx, 16));
MP_CHECKOK(mp_read_radix(&geny, params->geny, 16));
MP_CHECKOK(mp_read_radix(&order, params->order, 16));
/* determine number of bits */
bits = mpl_significant_bits(&irr) - 1;
if (bits < MP_OKAY) {
res = bits;
goto CLEANUP;
}
/* determine which optimizations (if any) to use */
if (params->field == ECField_GFp) {
#ifdef NSS_ECC_MORE_THAN_SUITE_B
switch (name) {
#ifdef ECL_USE_FP
case ECCurve_SECG_PRIME_160R1:
group =
ECGroup_consGFp(&irr, &curvea, &curveb, &genx, &geny,
&order, params->cofactor);
if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
MP_CHECKOK(ec_group_set_secp160r1_fp(group));
break;
#endif
case ECCurve_SECG_PRIME_192R1:
#ifdef ECL_USE_FP
group =
ECGroup_consGFp(&irr, &curvea, &curveb, &genx, &geny,
&order, params->cofactor);
if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
MP_CHECKOK(ec_group_set_nistp192_fp(group));
#else
group =
ECGroup_consGFp(&irr, &curvea, &curveb, &genx, &geny,
&order, params->cofactor);
if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
MP_CHECKOK(ec_group_set_gfp192(group, name));
#endif
break;
case ECCurve_SECG_PRIME_224R1:
#ifdef ECL_USE_FP
group =
ECGroup_consGFp(&irr, &curvea, &curveb, &genx, &geny,
&order, params->cofactor);
if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
MP_CHECKOK(ec_group_set_nistp224_fp(group));
#else
group =
ECGroup_consGFp(&irr, &curvea, &curveb, &genx, &geny,
&order, params->cofactor);
if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
MP_CHECKOK(ec_group_set_gfp224(group, name));
#endif
break;
case ECCurve_SECG_PRIME_256R1:
group =
ECGroup_consGFp(&irr, &curvea, &curveb, &genx, &geny,
&order, params->cofactor);
if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
MP_CHECKOK(ec_group_set_gfp256(group, name));
break;
case ECCurve_SECG_PRIME_521R1:
group =
ECGroup_consGFp(&irr, &curvea, &curveb, &genx, &geny,
&order, params->cofactor);
if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
MP_CHECKOK(ec_group_set_gfp521(group, name));
break;
default:
/* use generic arithmetic */
#endif
group =
ECGroup_consGFp_mont(&irr, &curvea, &curveb, &genx, &geny,
&order, params->cofactor);
if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
#ifdef NSS_ECC_MORE_THAN_SUITE_B
}
} else if (params->field == ECField_GF2m) {
group = ECGroup_consGF2m(&irr, NULL, &curvea, &curveb, &genx, &geny, &order, params->cofactor);
if (group == NULL) { res = MP_UNDEF; goto CLEANUP; }
if ((name == ECCurve_NIST_K163) ||
(name == ECCurve_NIST_B163) ||
(name == ECCurve_SECG_CHAR2_163R1)) {
MP_CHECKOK(ec_group_set_gf2m163(group, name));
} else if ((name == ECCurve_SECG_CHAR2_193R1) ||
(name == ECCurve_SECG_CHAR2_193R2)) {
MP_CHECKOK(ec_group_set_gf2m193(group, name));
} else if ((name == ECCurve_NIST_K233) ||
(name == ECCurve_NIST_B233)) {
MP_CHECKOK(ec_group_set_gf2m233(group, name));
}
#endif
} else {
res = MP_UNDEF;
goto CLEANUP;
}
/* set name, if any */
if ((group != NULL) && (params->text != NULL)) {
#ifdef _KERNEL
int n = strlen(params->text) + 1;
group->text = kmem_alloc(n, kmflag);
if (group->text == NULL) {
res = MP_MEM;
goto CLEANUP;
}
bcopy(params->text, group->text, n);
group->text_len = n;
#else
group->text = strdup(params->text);
if (group->text == NULL) {
res = MP_MEM;
}
#endif
}
CLEANUP:
mp_clear(&irr);
mp_clear(&curvea);
mp_clear(&curveb);
mp_clear(&genx);
mp_clear(&geny);
mp_clear(&order);
if (res != MP_OKAY) {
ECGroup_free(group);
return NULL;
}
return group;
}
/* Construct ECGroup from hexadecimal representations of parameters. */
ECGroup *
ECGroup_fromHex(const ECCurveParams * params, int kmflag)
{
return ecgroup_fromNameAndHex(ECCurve_noName, params, kmflag);
}
/* Construct ECGroup from named parameters. */
ECGroup *
ECGroup_fromName(const ECCurveName name, int kmflag)
{
ECGroup *group = NULL;
ECCurveParams *params = NULL;
mp_err res = MP_OKAY;
params = EC_GetNamedCurveParams(name, kmflag);
if (params == NULL) {
res = MP_UNDEF;
goto CLEANUP;
}
/* construct actual group */
group = ecgroup_fromNameAndHex(name, params, kmflag);
if (group == NULL) {
res = MP_UNDEF;
goto CLEANUP;
}
CLEANUP:
EC_FreeCurveParams(params);
if (res != MP_OKAY) {
ECGroup_free(group);
return NULL;
}
return group;
}
/* Validates an EC public key as described in Section 5.2.2 of X9.62. */
mp_err ECPoint_validate(const ECGroup *group, const mp_int *px, const
mp_int *py)
{
/* 1: Verify that publicValue is not the point at infinity */
/* 2: Verify that the coordinates of publicValue are elements
* of the field.
*/
/* 3: Verify that publicValue is on the curve. */
/* 4: Verify that the order of the curve times the publicValue
* is the point at infinity.
*/
return group->validate_point(px, py, group);
}
/* Free the memory allocated (if any) to an ECGroup object. */
void
ECGroup_free(ECGroup *group)
{
if (group == NULL)
return;
GFMethod_free(group->meth);
if (group->constructed == MP_NO)
return;
mp_clear(&group->curvea);
mp_clear(&group->curveb);
mp_clear(&group->genx);
mp_clear(&group->geny);
mp_clear(&group->order);
if (group->text != NULL)
#ifdef _KERNEL
kmem_free(group->text, group->text_len);
#else
free(group->text);
#endif
if (group->extra_free != NULL)
group->extra_free(group);
#ifdef _KERNEL
kmem_free(group, sizeof (ECGroup));
#else
free(group);
#endif
}