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