michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "ecl.h" michael@0: #include "ecl-curve.h" michael@0: #include "ecl-priv.h" michael@0: #include michael@0: #include michael@0: michael@0: #define CHECK(func) if ((func) == NULL) { res = 0; goto CLEANUP; } michael@0: michael@0: /* Duplicates an ECCurveParams */ michael@0: ECCurveParams * michael@0: ECCurveParams_dup(const ECCurveParams * params) michael@0: { michael@0: int res = 1; michael@0: ECCurveParams *ret = NULL; michael@0: michael@0: CHECK(ret = (ECCurveParams *) calloc(1, sizeof(ECCurveParams))); michael@0: if (params->text != NULL) { michael@0: CHECK(ret->text = strdup(params->text)); michael@0: } michael@0: ret->field = params->field; michael@0: ret->size = params->size; michael@0: if (params->irr != NULL) { michael@0: CHECK(ret->irr = strdup(params->irr)); michael@0: } michael@0: if (params->curvea != NULL) { michael@0: CHECK(ret->curvea = strdup(params->curvea)); michael@0: } michael@0: if (params->curveb != NULL) { michael@0: CHECK(ret->curveb = strdup(params->curveb)); michael@0: } michael@0: if (params->genx != NULL) { michael@0: CHECK(ret->genx = strdup(params->genx)); michael@0: } michael@0: if (params->geny != NULL) { michael@0: CHECK(ret->geny = strdup(params->geny)); michael@0: } michael@0: if (params->order != NULL) { michael@0: CHECK(ret->order = strdup(params->order)); michael@0: } michael@0: ret->cofactor = params->cofactor; michael@0: michael@0: CLEANUP: michael@0: if (res != 1) { michael@0: EC_FreeCurveParams(ret); michael@0: return NULL; michael@0: } michael@0: return ret; michael@0: } michael@0: michael@0: #undef CHECK michael@0: michael@0: /* Construct ECCurveParams from an ECCurveName */ michael@0: ECCurveParams * michael@0: EC_GetNamedCurveParams(const ECCurveName name) michael@0: { michael@0: if ((name <= ECCurve_noName) || (ECCurve_pastLastCurve <= name) || michael@0: (ecCurve_map[name] == NULL)) { michael@0: return NULL; michael@0: } else { michael@0: return ECCurveParams_dup(ecCurve_map[name]); michael@0: } michael@0: } michael@0: michael@0: /* Free the memory allocated (if any) to an ECCurveParams object. */ michael@0: void michael@0: EC_FreeCurveParams(ECCurveParams * params) michael@0: { michael@0: if (params == NULL) michael@0: return; michael@0: if (params->text != NULL) michael@0: free(params->text); michael@0: if (params->irr != NULL) michael@0: free(params->irr); michael@0: if (params->curvea != NULL) michael@0: free(params->curvea); michael@0: if (params->curveb != NULL) michael@0: free(params->curveb); michael@0: if (params->genx != NULL) michael@0: free(params->genx); michael@0: if (params->geny != NULL) michael@0: free(params->geny); michael@0: if (params->order != NULL) michael@0: free(params->order); michael@0: free(params); michael@0: }