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.
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 "ecl.h"
1674N/A#include "ecl-curve.h"
1674N/A#include "ecl-priv.h"
1674N/A#ifndef _KERNEL
1674N/A#include <stdlib.h>
1674N/A#include <string.h>
1674N/A#endif
1674N/A
1674N/A#define CHECK(func) if ((func) == NULL) { res = 0; goto CLEANUP; }
1674N/A
1674N/A/* Duplicates an ECCurveParams */
1674N/AECCurveParams *
1674N/AECCurveParams_dup(const ECCurveParams * params, int kmflag)
1674N/A{
1674N/A int res = 1;
1674N/A ECCurveParams *ret = NULL;
1674N/A
1674N/A#ifdef _KERNEL
1674N/A ret = (ECCurveParams *) kmem_zalloc(sizeof(ECCurveParams), kmflag);
1674N/A#else
1674N/A CHECK(ret = (ECCurveParams *) calloc(1, sizeof(ECCurveParams)));
1674N/A#endif
1674N/A if (params->text != NULL) {
1674N/A#ifdef _KERNEL
1674N/A ret->text = kmem_alloc(strlen(params->text) + 1, kmflag);
1674N/A bcopy(params->text, ret->text, strlen(params->text) + 1);
1674N/A#else
1674N/A CHECK(ret->text = strdup(params->text));
1674N/A#endif
1674N/A }
1674N/A ret->field = params->field;
1674N/A ret->size = params->size;
1674N/A if (params->irr != NULL) {
1674N/A#ifdef _KERNEL
1674N/A ret->irr = kmem_alloc(strlen(params->irr) + 1, kmflag);
1674N/A bcopy(params->irr, ret->irr, strlen(params->irr) + 1);
1674N/A#else
1674N/A CHECK(ret->irr = strdup(params->irr));
1674N/A#endif
1674N/A }
1674N/A if (params->curvea != NULL) {
1674N/A#ifdef _KERNEL
1674N/A ret->curvea = kmem_alloc(strlen(params->curvea) + 1, kmflag);
1674N/A bcopy(params->curvea, ret->curvea, strlen(params->curvea) + 1);
1674N/A#else
1674N/A CHECK(ret->curvea = strdup(params->curvea));
1674N/A#endif
1674N/A }
1674N/A if (params->curveb != NULL) {
1674N/A#ifdef _KERNEL
1674N/A ret->curveb = kmem_alloc(strlen(params->curveb) + 1, kmflag);
1674N/A bcopy(params->curveb, ret->curveb, strlen(params->curveb) + 1);
1674N/A#else
1674N/A CHECK(ret->curveb = strdup(params->curveb));
1674N/A#endif
1674N/A }
1674N/A if (params->genx != NULL) {
1674N/A#ifdef _KERNEL
1674N/A ret->genx = kmem_alloc(strlen(params->genx) + 1, kmflag);
1674N/A bcopy(params->genx, ret->genx, strlen(params->genx) + 1);
1674N/A#else
1674N/A CHECK(ret->genx = strdup(params->genx));
1674N/A#endif
1674N/A }
1674N/A if (params->geny != NULL) {
1674N/A#ifdef _KERNEL
1674N/A ret->geny = kmem_alloc(strlen(params->geny) + 1, kmflag);
1674N/A bcopy(params->geny, ret->geny, strlen(params->geny) + 1);
1674N/A#else
1674N/A CHECK(ret->geny = strdup(params->geny));
1674N/A#endif
1674N/A }
1674N/A if (params->order != NULL) {
1674N/A#ifdef _KERNEL
1674N/A ret->order = kmem_alloc(strlen(params->order) + 1, kmflag);
1674N/A bcopy(params->order, ret->order, strlen(params->order) + 1);
1674N/A#else
1674N/A CHECK(ret->order = strdup(params->order));
1674N/A#endif
1674N/A }
1674N/A ret->cofactor = params->cofactor;
1674N/A
1674N/A CLEANUP:
1674N/A if (res != 1) {
1674N/A EC_FreeCurveParams(ret);
1674N/A return NULL;
1674N/A }
1674N/A return ret;
1674N/A}
1674N/A
1674N/A#undef CHECK
1674N/A
1674N/A/* Construct ECCurveParams from an ECCurveName */
1674N/AECCurveParams *
1674N/AEC_GetNamedCurveParams(const ECCurveName name, int kmflag)
1674N/A{
1674N/A if ((name <= ECCurve_noName) || (ECCurve_pastLastCurve <= name) ||
1674N/A (ecCurve_map[name] == NULL)) {
1674N/A return NULL;
1674N/A } else {
1674N/A return ECCurveParams_dup(ecCurve_map[name], kmflag);
1674N/A }
1674N/A}
1674N/A
1674N/A/* Free the memory allocated (if any) to an ECCurveParams object. */
1674N/Avoid
1674N/AEC_FreeCurveParams(ECCurveParams * params)
1674N/A{
1674N/A if (params == NULL)
1674N/A return;
1674N/A if (params->text != NULL)
1674N/A#ifdef _KERNEL
1674N/A kmem_free(params->text, strlen(params->text) + 1);
1674N/A#else
1674N/A free(params->text);
1674N/A#endif
1674N/A if (params->irr != NULL)
1674N/A#ifdef _KERNEL
1674N/A kmem_free(params->irr, strlen(params->irr) + 1);
1674N/A#else
1674N/A free(params->irr);
1674N/A#endif
1674N/A if (params->curvea != NULL)
1674N/A#ifdef _KERNEL
1674N/A kmem_free(params->curvea, strlen(params->curvea) + 1);
1674N/A#else
1674N/A free(params->curvea);
1674N/A#endif
1674N/A if (params->curveb != NULL)
1674N/A#ifdef _KERNEL
1674N/A kmem_free(params->curveb, strlen(params->curveb) + 1);
1674N/A#else
1674N/A free(params->curveb);
1674N/A#endif
1674N/A if (params->genx != NULL)
1674N/A#ifdef _KERNEL
1674N/A kmem_free(params->genx, strlen(params->genx) + 1);
1674N/A#else
1674N/A free(params->genx);
1674N/A#endif
1674N/A if (params->geny != NULL)
1674N/A#ifdef _KERNEL
1674N/A kmem_free(params->geny, strlen(params->geny) + 1);
1674N/A#else
1674N/A free(params->geny);
1674N/A#endif
1674N/A if (params->order != NULL)
1674N/A#ifdef _KERNEL
1674N/A kmem_free(params->order, strlen(params->order) + 1);
1674N/A#else
1674N/A free(params->order);
1674N/A#endif
1674N/A#ifdef _KERNEL
1674N/A kmem_free(params, sizeof(ECCurveParams));
1674N/A#else
1674N/A free(params);
1674N/A#endif
1674N/A}